2 * Host side test driver to test endpoint functionality
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
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 IRQ_TYPE_UNDEFINED -1
41 #define IRQ_TYPE_LEGACY 0
42 #define IRQ_TYPE_MSI 1
43 #define IRQ_TYPE_MSIX 2
45 #define PCI_ENDPOINT_TEST_MAGIC 0x0
47 #define PCI_ENDPOINT_TEST_COMMAND 0x4
48 #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
49 #define COMMAND_RAISE_MSI_IRQ BIT(1)
50 #define COMMAND_RAISE_MSIX_IRQ BIT(2)
51 #define COMMAND_READ BIT(3)
52 #define COMMAND_WRITE BIT(4)
53 #define COMMAND_COPY BIT(5)
55 #define PCI_ENDPOINT_TEST_STATUS 0x8
56 #define STATUS_READ_SUCCESS BIT(0)
57 #define STATUS_READ_FAIL BIT(1)
58 #define STATUS_WRITE_SUCCESS BIT(2)
59 #define STATUS_WRITE_FAIL BIT(3)
60 #define STATUS_COPY_SUCCESS BIT(4)
61 #define STATUS_COPY_FAIL BIT(5)
62 #define STATUS_IRQ_RAISED BIT(6)
63 #define STATUS_SRC_ADDR_INVALID BIT(7)
64 #define STATUS_DST_ADDR_INVALID BIT(8)
66 #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
67 #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
69 #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
70 #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
72 #define PCI_ENDPOINT_TEST_SIZE 0x1c
73 #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
75 #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
76 #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
78 #define PCI_DEVICE_ID_TI_AM654 0xb00c
80 #define is_am654_pci_dev(pdev) \
81 ((pdev)->device == PCI_DEVICE_ID_TI_AM654)
83 static DEFINE_IDA(pci_endpoint_test_ida);
85 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
89 module_param(no_msi, bool, 0444);
90 MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
92 static int irq_type = IRQ_TYPE_MSI;
93 module_param(irq_type, int, 0444);
94 MODULE_PARM_DESC(irq_type, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
105 struct pci_endpoint_test {
106 struct pci_dev *pdev;
108 void __iomem *bar[6];
109 struct completion irq_raised;
113 /* mutex to protect the ioctls */
115 struct miscdevice miscdev;
116 enum pci_barno test_reg_bar;
120 struct pci_endpoint_test_data {
121 enum pci_barno test_reg_bar;
126 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
129 return readl(test->base + offset);
132 static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
133 u32 offset, u32 value)
135 writel(value, test->base + offset);
138 static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
141 return readl(test->bar[bar] + offset);
144 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
145 int bar, u32 offset, u32 value)
147 writel(value, test->bar[bar] + offset);
150 static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
152 struct pci_endpoint_test *test = dev_id;
155 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
156 if (reg & STATUS_IRQ_RAISED) {
157 test->last_irq = irq;
158 complete(&test->irq_raised);
159 reg &= ~STATUS_IRQ_RAISED;
161 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
167 static void pci_endpoint_test_free_irq_vectors(struct pci_endpoint_test *test)
169 struct pci_dev *pdev = test->pdev;
171 pci_free_irq_vectors(pdev);
172 test->irq_type = IRQ_TYPE_UNDEFINED;
175 static bool pci_endpoint_test_alloc_irq_vectors(struct pci_endpoint_test *test,
179 struct pci_dev *pdev = test->pdev;
180 struct device *dev = &pdev->dev;
184 case IRQ_TYPE_LEGACY:
185 irq = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_LEGACY);
187 dev_err(dev, "Failed to get Legacy interrupt\n");
190 irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
192 dev_err(dev, "Failed to get MSI interrupts\n");
195 irq = pci_alloc_irq_vectors(pdev, 1, 2048, PCI_IRQ_MSIX);
197 dev_err(dev, "Failed to get MSI-X interrupts\n");
200 dev_err(dev, "Invalid IRQ type selected\n");
208 test->irq_type = type;
209 test->num_irqs = irq;
214 static void pci_endpoint_test_release_irq(struct pci_endpoint_test *test)
217 struct pci_dev *pdev = test->pdev;
218 struct device *dev = &pdev->dev;
220 for (i = 0; i < test->num_irqs; i++)
221 devm_free_irq(dev, pci_irq_vector(pdev, i), test);
226 static bool pci_endpoint_test_request_irq(struct pci_endpoint_test *test)
230 struct pci_dev *pdev = test->pdev;
231 struct device *dev = &pdev->dev;
233 for (i = 0; i < test->num_irqs; i++) {
234 err = devm_request_irq(dev, pci_irq_vector(pdev, i),
235 pci_endpoint_test_irqhandler,
236 IRQF_SHARED, DRV_MODULE_NAME, test);
245 case IRQ_TYPE_LEGACY:
246 dev_err(dev, "Failed to request IRQ %d for Legacy\n",
247 pci_irq_vector(pdev, i));
250 dev_err(dev, "Failed to request IRQ %d for MSI %d\n",
251 pci_irq_vector(pdev, i),
255 dev_err(dev, "Failed to request IRQ %d for MSI-X %d\n",
256 pci_irq_vector(pdev, i),
264 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
265 enum pci_barno barno)
270 struct pci_dev *pdev = test->pdev;
272 if (!test->bar[barno])
275 size = pci_resource_len(pdev, barno);
277 if (barno == test->test_reg_bar)
280 for (j = 0; j < size; j += 4)
281 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
283 for (j = 0; j < size; j += 4) {
284 val = pci_endpoint_test_bar_readl(test, barno, j);
285 if (val != 0xA0A0A0A0)
292 static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
296 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
298 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 0);
299 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
300 COMMAND_RAISE_LEGACY_IRQ);
301 val = wait_for_completion_timeout(&test->irq_raised,
302 msecs_to_jiffies(1000));
309 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
310 u16 msi_num, bool msix)
313 struct pci_dev *pdev = test->pdev;
315 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
316 msix == false ? IRQ_TYPE_MSI :
318 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num);
319 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
320 msix == false ? COMMAND_RAISE_MSI_IRQ :
321 COMMAND_RAISE_MSIX_IRQ);
322 val = wait_for_completion_timeout(&test->irq_raised,
323 msecs_to_jiffies(1000));
327 if (pci_irq_vector(pdev, msi_num - 1) == test->last_irq)
333 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
338 dma_addr_t src_phys_addr;
339 dma_addr_t dst_phys_addr;
340 struct pci_dev *pdev = test->pdev;
341 struct device *dev = &pdev->dev;
343 dma_addr_t orig_src_phys_addr;
345 dma_addr_t orig_dst_phys_addr;
347 size_t alignment = test->alignment;
348 int irq_type = test->irq_type;
352 if (size > SIZE_MAX - alignment)
355 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
356 dev_err(dev, "Invalid IRQ type option\n");
360 orig_src_addr = dma_alloc_coherent(dev, size + alignment,
361 &orig_src_phys_addr, GFP_KERNEL);
362 if (!orig_src_addr) {
363 dev_err(dev, "Failed to allocate source buffer\n");
368 if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
369 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
370 offset = src_phys_addr - orig_src_phys_addr;
371 src_addr = orig_src_addr + offset;
373 src_phys_addr = orig_src_phys_addr;
374 src_addr = orig_src_addr;
377 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
378 lower_32_bits(src_phys_addr));
380 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
381 upper_32_bits(src_phys_addr));
383 get_random_bytes(src_addr, size);
384 src_crc32 = crc32_le(~0, src_addr, size);
386 orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
387 &orig_dst_phys_addr, GFP_KERNEL);
388 if (!orig_dst_addr) {
389 dev_err(dev, "Failed to allocate destination address\n");
391 goto err_orig_src_addr;
394 if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
395 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
396 offset = dst_phys_addr - orig_dst_phys_addr;
397 dst_addr = orig_dst_addr + offset;
399 dst_phys_addr = orig_dst_phys_addr;
400 dst_addr = orig_dst_addr;
403 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
404 lower_32_bits(dst_phys_addr));
405 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
406 upper_32_bits(dst_phys_addr));
408 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
411 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
412 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
413 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
416 wait_for_completion(&test->irq_raised);
418 dst_crc32 = crc32_le(~0, dst_addr, size);
419 if (dst_crc32 == src_crc32)
422 dma_free_coherent(dev, size + alignment, orig_dst_addr,
426 dma_free_coherent(dev, size + alignment, orig_src_addr,
433 static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
438 dma_addr_t phys_addr;
439 struct pci_dev *pdev = test->pdev;
440 struct device *dev = &pdev->dev;
442 dma_addr_t orig_phys_addr;
444 size_t alignment = test->alignment;
445 int irq_type = test->irq_type;
448 if (size > SIZE_MAX - alignment)
451 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
452 dev_err(dev, "Invalid IRQ type option\n");
456 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
459 dev_err(dev, "Failed to allocate address\n");
464 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
465 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
466 offset = phys_addr - orig_phys_addr;
467 addr = orig_addr + offset;
469 phys_addr = orig_phys_addr;
473 get_random_bytes(addr, size);
475 crc32 = crc32_le(~0, addr, size);
476 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
479 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
480 lower_32_bits(phys_addr));
481 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
482 upper_32_bits(phys_addr));
484 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
486 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
487 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
488 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
491 wait_for_completion(&test->irq_raised);
493 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
494 if (reg & STATUS_READ_SUCCESS)
497 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
503 static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
507 dma_addr_t phys_addr;
508 struct pci_dev *pdev = test->pdev;
509 struct device *dev = &pdev->dev;
511 dma_addr_t orig_phys_addr;
513 size_t alignment = test->alignment;
514 int irq_type = test->irq_type;
517 if (size > SIZE_MAX - alignment)
520 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
521 dev_err(dev, "Invalid IRQ type option\n");
525 orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
528 dev_err(dev, "Failed to allocate destination address\n");
533 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
534 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
535 offset = phys_addr - orig_phys_addr;
536 addr = orig_addr + offset;
538 phys_addr = orig_phys_addr;
542 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
543 lower_32_bits(phys_addr));
544 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
545 upper_32_bits(phys_addr));
547 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
549 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
550 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
551 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
554 wait_for_completion(&test->irq_raised);
556 crc32 = crc32_le(~0, addr, size);
557 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
560 dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
565 static bool pci_endpoint_test_set_irq(struct pci_endpoint_test *test,
568 struct pci_dev *pdev = test->pdev;
569 struct device *dev = &pdev->dev;
571 if (req_irq_type < IRQ_TYPE_LEGACY || req_irq_type > IRQ_TYPE_MSIX) {
572 dev_err(dev, "Invalid IRQ type option\n");
576 if (test->irq_type == req_irq_type)
579 pci_endpoint_test_release_irq(test);
580 pci_endpoint_test_free_irq_vectors(test);
582 if (!pci_endpoint_test_alloc_irq_vectors(test, req_irq_type))
585 if (!pci_endpoint_test_request_irq(test))
591 pci_endpoint_test_free_irq_vectors(test);
595 static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
600 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
601 struct pci_dev *pdev = test->pdev;
603 mutex_lock(&test->mutex);
607 if (bar < 0 || bar > 5)
609 if (is_am654_pci_dev(pdev) && bar == BAR_0)
611 ret = pci_endpoint_test_bar(test, bar);
613 case PCITEST_LEGACY_IRQ:
614 ret = pci_endpoint_test_legacy_irq(test);
618 ret = pci_endpoint_test_msi_irq(test, arg, cmd == PCITEST_MSIX);
621 ret = pci_endpoint_test_write(test, arg);
624 ret = pci_endpoint_test_read(test, arg);
627 ret = pci_endpoint_test_copy(test, arg);
629 case PCITEST_SET_IRQTYPE:
630 ret = pci_endpoint_test_set_irq(test, arg);
632 case PCITEST_GET_IRQTYPE:
638 mutex_unlock(&test->mutex);
642 static const struct file_operations pci_endpoint_test_fops = {
643 .owner = THIS_MODULE,
644 .unlocked_ioctl = pci_endpoint_test_ioctl,
647 static int pci_endpoint_test_probe(struct pci_dev *pdev,
648 const struct pci_device_id *ent)
655 struct device *dev = &pdev->dev;
656 struct pci_endpoint_test *test;
657 struct pci_endpoint_test_data *data;
658 enum pci_barno test_reg_bar = BAR_0;
659 struct miscdevice *misc_device;
661 if (pci_is_bridge(pdev))
664 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
668 test->test_reg_bar = 0;
671 test->irq_type = IRQ_TYPE_UNDEFINED;
674 irq_type = IRQ_TYPE_LEGACY;
676 data = (struct pci_endpoint_test_data *)ent->driver_data;
678 test_reg_bar = data->test_reg_bar;
679 test->test_reg_bar = test_reg_bar;
680 test->alignment = data->alignment;
681 irq_type = data->irq_type;
684 init_completion(&test->irq_raised);
685 mutex_init(&test->mutex);
687 err = pci_enable_device(pdev);
689 dev_err(dev, "Cannot enable PCI device\n");
693 err = pci_request_regions(pdev, DRV_MODULE_NAME);
695 dev_err(dev, "Cannot obtain PCI resources\n");
696 goto err_disable_pdev;
699 pci_set_master(pdev);
701 if (!pci_endpoint_test_alloc_irq_vectors(test, irq_type))
702 goto err_disable_irq;
704 if (!pci_endpoint_test_request_irq(test))
705 goto err_disable_irq;
707 for (bar = BAR_0; bar <= BAR_5; bar++) {
708 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
709 base = pci_ioremap_bar(pdev, bar);
711 dev_err(dev, "Failed to read BAR%d\n", bar);
712 WARN_ON(bar == test_reg_bar);
714 test->bar[bar] = base;
718 test->base = test->bar[test_reg_bar];
721 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
726 pci_set_drvdata(pdev, test);
728 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
731 dev_err(dev, "Unable to get id\n");
735 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
736 misc_device = &test->miscdev;
737 misc_device->minor = MISC_DYNAMIC_MINOR;
738 misc_device->name = kstrdup(name, GFP_KERNEL);
739 if (!misc_device->name) {
743 misc_device->fops = &pci_endpoint_test_fops,
745 err = misc_register(misc_device);
747 dev_err(dev, "Failed to register device\n");
754 kfree(misc_device->name);
757 ida_simple_remove(&pci_endpoint_test_ida, id);
760 for (bar = BAR_0; bar <= BAR_5; bar++) {
762 pci_iounmap(pdev, test->bar[bar]);
764 pci_endpoint_test_release_irq(test);
767 pci_endpoint_test_free_irq_vectors(test);
768 pci_release_regions(pdev);
771 pci_disable_device(pdev);
776 static void pci_endpoint_test_remove(struct pci_dev *pdev)
780 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
781 struct miscdevice *misc_device = &test->miscdev;
783 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
788 misc_deregister(&test->miscdev);
789 kfree(misc_device->name);
790 ida_simple_remove(&pci_endpoint_test_ida, id);
791 for (bar = BAR_0; bar <= BAR_5; bar++) {
793 pci_iounmap(pdev, test->bar[bar]);
796 pci_endpoint_test_release_irq(test);
797 pci_endpoint_test_free_irq_vectors(test);
799 pci_release_regions(pdev);
800 pci_disable_device(pdev);
803 static const struct pci_endpoint_test_data am654_data = {
804 .test_reg_bar = BAR_2,
806 .irq_type = IRQ_TYPE_MSI,
809 static const struct pci_device_id pci_endpoint_test_tbl[] = {
810 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
811 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
812 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0x81c0) },
813 { PCI_DEVICE_DATA(SYNOPSYS, EDDA, NULL) },
814 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM654),
815 .driver_data = (kernel_ulong_t)&am654_data
819 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
821 static struct pci_driver pci_endpoint_test_driver = {
822 .name = DRV_MODULE_NAME,
823 .id_table = pci_endpoint_test_tbl,
824 .probe = pci_endpoint_test_probe,
825 .remove = pci_endpoint_test_remove,
827 module_pci_driver(pci_endpoint_test_driver);
829 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
830 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
831 MODULE_LICENSE("GPL v2");