2 * Host side test driver to test endpoint functionality
4 * Copyright (C) 2017 Texas Instruments
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/crc32.h>
21 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/random.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <linux/pci_ids.h>
34 #include <linux/pci_regs.h>
36 #include <uapi/linux/pcitest.h>
38 #define DRV_MODULE_NAME "pci-endpoint-test"
40 #define PCI_ENDPOINT_TEST_MAGIC 0x0
42 #define PCI_ENDPOINT_TEST_COMMAND 0x4
43 #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
44 #define COMMAND_RAISE_MSI_IRQ BIT(1)
45 #define MSI_NUMBER_SHIFT 2
46 /* 6 bits for MSI number */
47 #define COMMAND_READ BIT(8)
48 #define COMMAND_WRITE BIT(9)
49 #define COMMAND_COPY BIT(10)
51 #define PCI_ENDPOINT_TEST_STATUS 0x8
52 #define STATUS_READ_SUCCESS BIT(0)
53 #define STATUS_READ_FAIL BIT(1)
54 #define STATUS_WRITE_SUCCESS BIT(2)
55 #define STATUS_WRITE_FAIL BIT(3)
56 #define STATUS_COPY_SUCCESS BIT(4)
57 #define STATUS_COPY_FAIL BIT(5)
58 #define STATUS_IRQ_RAISED BIT(6)
59 #define STATUS_SRC_ADDR_INVALID BIT(7)
60 #define STATUS_DST_ADDR_INVALID BIT(8)
62 #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0xc
63 #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
65 #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
66 #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
68 #define PCI_ENDPOINT_TEST_SIZE 0x1c
69 #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
71 static DEFINE_IDA(pci_endpoint_test_ida);
73 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
77 module_param(no_msi, bool, 0444);
78 MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
89 struct pci_endpoint_test {
93 struct completion irq_raised;
96 /* mutex to protect the ioctls */
98 struct miscdevice miscdev;
99 enum pci_barno test_reg_bar;
103 struct pci_endpoint_test_data {
104 enum pci_barno test_reg_bar;
109 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
112 return readl(test->base + offset);
115 static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
116 u32 offset, u32 value)
118 writel(value, test->base + offset);
121 static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
124 return readl(test->bar[bar] + offset);
127 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
128 int bar, u32 offset, u32 value)
130 writel(value, test->bar[bar] + offset);
133 static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
135 struct pci_endpoint_test *test = dev_id;
138 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
139 if (reg & STATUS_IRQ_RAISED) {
140 test->last_irq = irq;
141 complete(&test->irq_raised);
142 reg &= ~STATUS_IRQ_RAISED;
144 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
150 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
151 enum pci_barno barno)
156 struct pci_dev *pdev = test->pdev;
158 if (!test->bar[barno])
161 size = pci_resource_len(pdev, barno);
163 if (barno == test->test_reg_bar)
166 for (j = 0; j < size; j += 4)
167 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
169 for (j = 0; j < size; j += 4) {
170 val = pci_endpoint_test_bar_readl(test, barno, j);
171 if (val != 0xA0A0A0A0)
178 static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
182 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
183 COMMAND_RAISE_LEGACY_IRQ);
184 val = wait_for_completion_timeout(&test->irq_raised,
185 msecs_to_jiffies(1000));
192 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
196 struct pci_dev *pdev = test->pdev;
198 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
199 msi_num << MSI_NUMBER_SHIFT |
200 COMMAND_RAISE_MSI_IRQ);
201 val = wait_for_completion_timeout(&test->irq_raised,
202 msecs_to_jiffies(1000));
206 if (test->last_irq - pdev->irq == msi_num - 1)
212 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
217 dma_addr_t src_phys_addr;
218 dma_addr_t dst_phys_addr;
219 struct pci_dev *pdev = test->pdev;
220 struct device *dev = &pdev->dev;
222 dma_addr_t orig_src_phys_addr;
224 dma_addr_t orig_dst_phys_addr;
226 size_t alignment = test->alignment;
230 if (size > SIZE_MAX - alignment)
233 orig_src_addr = dma_alloc_coherent(dev, size + alignment,
234 &orig_src_phys_addr, GFP_KERNEL);
235 if (!orig_src_addr) {
236 dev_err(dev, "failed to allocate source buffer\n");
241 if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
242 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
243 offset = src_phys_addr - orig_src_phys_addr;
244 src_addr = orig_src_addr + offset;
246 src_phys_addr = orig_src_phys_addr;
247 src_addr = orig_src_addr;
250 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
251 lower_32_bits(src_phys_addr));
253 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
254 upper_32_bits(src_phys_addr));
256 get_random_bytes(src_addr, size);
257 src_crc32 = crc32_le(~0, src_addr, size);
259 orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
260 &orig_dst_phys_addr, GFP_KERNEL);
261 if (!orig_dst_addr) {
262 dev_err(dev, "failed to allocate destination address\n");
264 goto err_orig_src_addr;
267 if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
268 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
269 offset = dst_phys_addr - orig_dst_phys_addr;
270 dst_addr = orig_dst_addr + offset;
272 dst_phys_addr = orig_dst_phys_addr;
273 dst_addr = orig_dst_addr;
276 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
277 lower_32_bits(dst_phys_addr));
278 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
279 upper_32_bits(dst_phys_addr));
281 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
284 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
285 1 << MSI_NUMBER_SHIFT | COMMAND_COPY);
287 wait_for_completion(&test->irq_raised);
289 dst_crc32 = crc32_le(~0, dst_addr, size);
290 if (dst_crc32 == src_crc32)
293 dma_free_coherent(dev, size + alignment, orig_dst_addr,
297 dma_free_coherent(dev, size + alignment, orig_src_addr,
304 static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
309 dma_addr_t phys_addr;
310 struct pci_dev *pdev = test->pdev;
311 struct device *dev = &pdev->dev;
313 dma_addr_t orig_phys_addr;
315 size_t alignment = test->alignment;
318 if (size > SIZE_MAX - alignment)
321 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
324 dev_err(dev, "failed to allocate address\n");
329 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
330 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
331 offset = phys_addr - orig_phys_addr;
332 addr = orig_addr + offset;
334 phys_addr = orig_phys_addr;
338 get_random_bytes(addr, size);
340 crc32 = crc32_le(~0, addr, size);
341 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
344 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
345 lower_32_bits(phys_addr));
346 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
347 upper_32_bits(phys_addr));
349 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
351 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
352 1 << MSI_NUMBER_SHIFT | COMMAND_READ);
354 wait_for_completion(&test->irq_raised);
356 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
357 if (reg & STATUS_READ_SUCCESS)
360 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
366 static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
370 dma_addr_t phys_addr;
371 struct pci_dev *pdev = test->pdev;
372 struct device *dev = &pdev->dev;
374 dma_addr_t orig_phys_addr;
376 size_t alignment = test->alignment;
379 if (size > SIZE_MAX - alignment)
382 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
385 dev_err(dev, "failed to allocate destination address\n");
390 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
391 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
392 offset = phys_addr - orig_phys_addr;
393 addr = orig_addr + offset;
395 phys_addr = orig_phys_addr;
399 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
400 lower_32_bits(phys_addr));
401 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
402 upper_32_bits(phys_addr));
404 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
406 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
407 1 << MSI_NUMBER_SHIFT | COMMAND_WRITE);
409 wait_for_completion(&test->irq_raised);
411 crc32 = crc32_le(~0, addr, size);
412 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
415 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
420 static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
425 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
427 mutex_lock(&test->mutex);
431 if (bar < 0 || bar > 5)
433 ret = pci_endpoint_test_bar(test, bar);
435 case PCITEST_LEGACY_IRQ:
436 ret = pci_endpoint_test_legacy_irq(test);
439 ret = pci_endpoint_test_msi_irq(test, arg);
442 ret = pci_endpoint_test_write(test, arg);
445 ret = pci_endpoint_test_read(test, arg);
448 ret = pci_endpoint_test_copy(test, arg);
453 mutex_unlock(&test->mutex);
457 static const struct file_operations pci_endpoint_test_fops = {
458 .owner = THIS_MODULE,
459 .unlocked_ioctl = pci_endpoint_test_ioctl,
462 static int pci_endpoint_test_probe(struct pci_dev *pdev,
463 const struct pci_device_id *ent)
472 struct device *dev = &pdev->dev;
473 struct pci_endpoint_test *test;
474 struct pci_endpoint_test_data *data;
475 enum pci_barno test_reg_bar = BAR_0;
476 struct miscdevice *misc_device;
478 if (pci_is_bridge(pdev))
481 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
485 test->test_reg_bar = 0;
489 data = (struct pci_endpoint_test_data *)ent->driver_data;
491 test_reg_bar = data->test_reg_bar;
492 test->alignment = data->alignment;
493 no_msi = data->no_msi;
496 init_completion(&test->irq_raised);
497 mutex_init(&test->mutex);
499 err = pci_enable_device(pdev);
501 dev_err(dev, "Cannot enable PCI device\n");
505 err = pci_request_regions(pdev, DRV_MODULE_NAME);
507 dev_err(dev, "Cannot obtain PCI resources\n");
508 goto err_disable_pdev;
511 pci_set_master(pdev);
514 irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
516 dev_err(dev, "failed to get MSI interrupts\n");
517 test->num_irqs = irq;
520 err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
521 IRQF_SHARED, DRV_MODULE_NAME, test);
523 dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
524 goto err_disable_msi;
527 for (i = 1; i < irq; i++) {
528 err = devm_request_irq(dev, pdev->irq + i,
529 pci_endpoint_test_irqhandler,
530 IRQF_SHARED, DRV_MODULE_NAME, test);
532 dev_err(dev, "failed to request IRQ %d for MSI %d\n",
533 pdev->irq + i, i + 1);
536 for (bar = BAR_0; bar <= BAR_5; bar++) {
537 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
538 base = pci_ioremap_bar(pdev, bar);
540 dev_err(dev, "failed to read BAR%d\n", bar);
541 WARN_ON(bar == test_reg_bar);
543 test->bar[bar] = base;
547 test->base = test->bar[test_reg_bar];
550 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
555 pci_set_drvdata(pdev, test);
557 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
560 dev_err(dev, "unable to get id\n");
564 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
565 misc_device = &test->miscdev;
566 misc_device->minor = MISC_DYNAMIC_MINOR;
567 misc_device->name = kstrdup(name, GFP_KERNEL);
568 if (!misc_device->name) {
572 misc_device->fops = &pci_endpoint_test_fops,
574 err = misc_register(misc_device);
576 dev_err(dev, "failed to register device\n");
583 kfree(misc_device->name);
586 ida_simple_remove(&pci_endpoint_test_ida, id);
589 for (bar = BAR_0; bar <= BAR_5; bar++) {
591 pci_iounmap(pdev, test->bar[bar]);
594 for (i = 0; i < irq; i++)
595 devm_free_irq(dev, pdev->irq + i, test);
598 pci_disable_msi(pdev);
599 pci_release_regions(pdev);
602 pci_disable_device(pdev);
607 static void pci_endpoint_test_remove(struct pci_dev *pdev)
612 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
613 struct miscdevice *misc_device = &test->miscdev;
615 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
620 misc_deregister(&test->miscdev);
621 kfree(misc_device->name);
622 ida_simple_remove(&pci_endpoint_test_ida, id);
623 for (bar = BAR_0; bar <= BAR_5; bar++) {
625 pci_iounmap(pdev, test->bar[bar]);
627 for (i = 0; i < test->num_irqs; i++)
628 devm_free_irq(&pdev->dev, pdev->irq + i, test);
629 pci_disable_msi(pdev);
630 pci_release_regions(pdev);
631 pci_disable_device(pdev);
634 static const struct pci_device_id pci_endpoint_test_tbl[] = {
635 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
636 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
639 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
641 static struct pci_driver pci_endpoint_test_driver = {
642 .name = DRV_MODULE_NAME,
643 .id_table = pci_endpoint_test_tbl,
644 .probe = pci_endpoint_test_probe,
645 .remove = pci_endpoint_test_remove,
647 module_pci_driver(pci_endpoint_test_driver);
649 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
651 MODULE_LICENSE("GPL v2");