1 // SPDX-License-Identifier: GPL-2.0-only
3 * Host side test driver to test endpoint functionality
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/miscdevice.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/random.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/pci.h>
22 #include <linux/pci_ids.h>
24 #include <linux/pci_regs.h>
26 #include <uapi/linux/pcitest.h>
28 #define DRV_MODULE_NAME "pci-endpoint-test"
30 #define IRQ_TYPE_UNDEFINED -1
31 #define IRQ_TYPE_LEGACY 0
32 #define IRQ_TYPE_MSI 1
33 #define IRQ_TYPE_MSIX 2
35 #define PCI_ENDPOINT_TEST_MAGIC 0x0
37 #define PCI_ENDPOINT_TEST_COMMAND 0x4
38 #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
39 #define COMMAND_RAISE_MSI_IRQ BIT(1)
40 #define COMMAND_RAISE_MSIX_IRQ BIT(2)
41 #define COMMAND_READ BIT(3)
42 #define COMMAND_WRITE BIT(4)
43 #define COMMAND_COPY BIT(5)
45 #define PCI_ENDPOINT_TEST_STATUS 0x8
46 #define STATUS_READ_SUCCESS BIT(0)
47 #define STATUS_READ_FAIL BIT(1)
48 #define STATUS_WRITE_SUCCESS BIT(2)
49 #define STATUS_WRITE_FAIL BIT(3)
50 #define STATUS_COPY_SUCCESS BIT(4)
51 #define STATUS_COPY_FAIL BIT(5)
52 #define STATUS_IRQ_RAISED BIT(6)
53 #define STATUS_SRC_ADDR_INVALID BIT(7)
54 #define STATUS_DST_ADDR_INVALID BIT(8)
56 #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
57 #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
59 #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
60 #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
62 #define PCI_ENDPOINT_TEST_SIZE 0x1c
63 #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
65 #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
66 #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
68 #define PCI_ENDPOINT_TEST_FLAGS 0x2c
69 #define FLAG_USE_DMA BIT(0)
71 #define PCI_DEVICE_ID_TI_AM654 0xb00c
72 #define PCI_DEVICE_ID_TI_J7200 0xb00f
73 #define PCI_DEVICE_ID_TI_AM64 0xb010
74 #define PCI_DEVICE_ID_LS1088A 0x80c0
75 #define PCI_DEVICE_ID_IMX8 0x0808
77 #define is_am654_pci_dev(pdev) \
78 ((pdev)->device == PCI_DEVICE_ID_TI_AM654)
80 #define PCI_DEVICE_ID_RENESAS_R8A774A1 0x0028
81 #define PCI_DEVICE_ID_RENESAS_R8A774B1 0x002b
82 #define PCI_DEVICE_ID_RENESAS_R8A774C0 0x002d
83 #define PCI_DEVICE_ID_RENESAS_R8A774E1 0x0025
85 static DEFINE_IDA(pci_endpoint_test_ida);
87 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
91 module_param(no_msi, bool, 0444);
92 MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
94 static int irq_type = IRQ_TYPE_MSI;
95 module_param(irq_type, int, 0444);
96 MODULE_PARM_DESC(irq_type, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
107 struct pci_endpoint_test {
108 struct pci_dev *pdev;
110 void __iomem *bar[PCI_STD_NUM_BARS];
111 struct completion irq_raised;
115 /* mutex to protect the ioctls */
117 struct miscdevice miscdev;
118 enum pci_barno test_reg_bar;
123 struct pci_endpoint_test_data {
124 enum pci_barno test_reg_bar;
129 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
132 return readl(test->base + offset);
135 static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
136 u32 offset, u32 value)
138 writel(value, test->base + offset);
141 static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
144 return readl(test->bar[bar] + offset);
147 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
148 int bar, u32 offset, u32 value)
150 writel(value, test->bar[bar] + offset);
153 static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
155 struct pci_endpoint_test *test = dev_id;
158 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
159 if (reg & STATUS_IRQ_RAISED) {
160 test->last_irq = irq;
161 complete(&test->irq_raised);
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, test->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 ? IRQ_TYPE_MSIX : IRQ_TYPE_MSI);
317 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num);
318 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
319 msix ? COMMAND_RAISE_MSIX_IRQ :
320 COMMAND_RAISE_MSI_IRQ);
321 val = wait_for_completion_timeout(&test->irq_raised,
322 msecs_to_jiffies(1000));
326 return pci_irq_vector(pdev, msi_num - 1) == test->last_irq;
329 static int pci_endpoint_test_validate_xfer_params(struct device *dev,
330 struct pci_endpoint_test_xfer_param *param, size_t alignment)
333 dev_dbg(dev, "Data size is zero\n");
337 if (param->size > SIZE_MAX - alignment) {
338 dev_dbg(dev, "Maximum transfer data size exceeded\n");
345 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
348 struct pci_endpoint_test_xfer_param param;
355 dma_addr_t src_phys_addr;
356 dma_addr_t dst_phys_addr;
357 struct pci_dev *pdev = test->pdev;
358 struct device *dev = &pdev->dev;
360 dma_addr_t orig_src_phys_addr;
362 dma_addr_t orig_dst_phys_addr;
364 size_t alignment = test->alignment;
365 int irq_type = test->irq_type;
370 err = copy_from_user(¶m, (void __user *)arg, sizeof(param));
372 dev_err(dev, "Failed to get transfer param\n");
376 err = pci_endpoint_test_validate_xfer_params(dev, ¶m, alignment);
382 use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
384 flags |= FLAG_USE_DMA;
386 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
387 dev_err(dev, "Invalid IRQ type option\n");
391 orig_src_addr = kzalloc(size + alignment, GFP_KERNEL);
392 if (!orig_src_addr) {
393 dev_err(dev, "Failed to allocate source buffer\n");
398 get_random_bytes(orig_src_addr, size + alignment);
399 orig_src_phys_addr = dma_map_single(dev, orig_src_addr,
400 size + alignment, DMA_TO_DEVICE);
401 if (dma_mapping_error(dev, orig_src_phys_addr)) {
402 dev_err(dev, "failed to map source buffer address\n");
404 goto err_src_phys_addr;
407 if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
408 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
409 offset = src_phys_addr - orig_src_phys_addr;
410 src_addr = orig_src_addr + offset;
412 src_phys_addr = orig_src_phys_addr;
413 src_addr = orig_src_addr;
416 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
417 lower_32_bits(src_phys_addr));
419 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
420 upper_32_bits(src_phys_addr));
422 src_crc32 = crc32_le(~0, src_addr, size);
424 orig_dst_addr = kzalloc(size + alignment, GFP_KERNEL);
425 if (!orig_dst_addr) {
426 dev_err(dev, "Failed to allocate destination address\n");
431 orig_dst_phys_addr = dma_map_single(dev, orig_dst_addr,
432 size + alignment, DMA_FROM_DEVICE);
433 if (dma_mapping_error(dev, orig_dst_phys_addr)) {
434 dev_err(dev, "failed to map destination buffer address\n");
436 goto err_dst_phys_addr;
439 if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
440 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
441 offset = dst_phys_addr - orig_dst_phys_addr;
442 dst_addr = orig_dst_addr + offset;
444 dst_phys_addr = orig_dst_phys_addr;
445 dst_addr = orig_dst_addr;
448 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
449 lower_32_bits(dst_phys_addr));
450 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
451 upper_32_bits(dst_phys_addr));
453 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
456 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
457 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
458 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
459 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
462 wait_for_completion(&test->irq_raised);
464 dma_unmap_single(dev, orig_dst_phys_addr, size + alignment,
467 dst_crc32 = crc32_le(~0, dst_addr, size);
468 if (dst_crc32 == src_crc32)
472 kfree(orig_dst_addr);
475 dma_unmap_single(dev, orig_src_phys_addr, size + alignment,
479 kfree(orig_src_addr);
485 static bool pci_endpoint_test_write(struct pci_endpoint_test *test,
488 struct pci_endpoint_test_xfer_param param;
494 dma_addr_t phys_addr;
495 struct pci_dev *pdev = test->pdev;
496 struct device *dev = &pdev->dev;
498 dma_addr_t orig_phys_addr;
500 size_t alignment = test->alignment;
501 int irq_type = test->irq_type;
506 err = copy_from_user(¶m, (void __user *)arg, sizeof(param));
508 dev_err(dev, "Failed to get transfer param\n");
512 err = pci_endpoint_test_validate_xfer_params(dev, ¶m, alignment);
518 use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
520 flags |= FLAG_USE_DMA;
522 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
523 dev_err(dev, "Invalid IRQ type option\n");
527 orig_addr = kzalloc(size + alignment, GFP_KERNEL);
529 dev_err(dev, "Failed to allocate address\n");
534 get_random_bytes(orig_addr, size + alignment);
536 orig_phys_addr = dma_map_single(dev, orig_addr, size + alignment,
538 if (dma_mapping_error(dev, orig_phys_addr)) {
539 dev_err(dev, "failed to map source buffer address\n");
544 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
545 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
546 offset = phys_addr - orig_phys_addr;
547 addr = orig_addr + offset;
549 phys_addr = orig_phys_addr;
553 crc32 = crc32_le(~0, addr, size);
554 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
557 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
558 lower_32_bits(phys_addr));
559 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
560 upper_32_bits(phys_addr));
562 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
564 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
565 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
566 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
567 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
570 wait_for_completion(&test->irq_raised);
572 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
573 if (reg & STATUS_READ_SUCCESS)
576 dma_unmap_single(dev, orig_phys_addr, size + alignment,
586 static bool pci_endpoint_test_read(struct pci_endpoint_test *test,
589 struct pci_endpoint_test_xfer_param param;
595 dma_addr_t phys_addr;
596 struct pci_dev *pdev = test->pdev;
597 struct device *dev = &pdev->dev;
599 dma_addr_t orig_phys_addr;
601 size_t alignment = test->alignment;
602 int irq_type = test->irq_type;
606 err = copy_from_user(¶m, (void __user *)arg, sizeof(param));
608 dev_err(dev, "Failed to get transfer param\n");
612 err = pci_endpoint_test_validate_xfer_params(dev, ¶m, alignment);
618 use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
620 flags |= FLAG_USE_DMA;
622 if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
623 dev_err(dev, "Invalid IRQ type option\n");
627 orig_addr = kzalloc(size + alignment, GFP_KERNEL);
629 dev_err(dev, "Failed to allocate destination address\n");
634 orig_phys_addr = dma_map_single(dev, orig_addr, size + alignment,
636 if (dma_mapping_error(dev, orig_phys_addr)) {
637 dev_err(dev, "failed to map source buffer address\n");
642 if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
643 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
644 offset = phys_addr - orig_phys_addr;
645 addr = orig_addr + offset;
647 phys_addr = orig_phys_addr;
651 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
652 lower_32_bits(phys_addr));
653 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
654 upper_32_bits(phys_addr));
656 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
658 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
659 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
660 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
661 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
664 wait_for_completion(&test->irq_raised);
666 dma_unmap_single(dev, orig_phys_addr, size + alignment,
669 crc32 = crc32_le(~0, addr, size);
670 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
679 static bool pci_endpoint_test_clear_irq(struct pci_endpoint_test *test)
681 pci_endpoint_test_release_irq(test);
682 pci_endpoint_test_free_irq_vectors(test);
686 static bool pci_endpoint_test_set_irq(struct pci_endpoint_test *test,
689 struct pci_dev *pdev = test->pdev;
690 struct device *dev = &pdev->dev;
692 if (req_irq_type < IRQ_TYPE_LEGACY || req_irq_type > IRQ_TYPE_MSIX) {
693 dev_err(dev, "Invalid IRQ type option\n");
697 if (test->irq_type == req_irq_type)
700 pci_endpoint_test_release_irq(test);
701 pci_endpoint_test_free_irq_vectors(test);
703 if (!pci_endpoint_test_alloc_irq_vectors(test, req_irq_type))
706 if (!pci_endpoint_test_request_irq(test))
712 pci_endpoint_test_free_irq_vectors(test);
716 static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
721 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
722 struct pci_dev *pdev = test->pdev;
724 mutex_lock(&test->mutex);
726 reinit_completion(&test->irq_raised);
727 test->last_irq = -ENODATA;
734 if (is_am654_pci_dev(pdev) && bar == BAR_0)
736 ret = pci_endpoint_test_bar(test, bar);
738 case PCITEST_LEGACY_IRQ:
739 ret = pci_endpoint_test_legacy_irq(test);
743 ret = pci_endpoint_test_msi_irq(test, arg, cmd == PCITEST_MSIX);
746 ret = pci_endpoint_test_write(test, arg);
749 ret = pci_endpoint_test_read(test, arg);
752 ret = pci_endpoint_test_copy(test, arg);
754 case PCITEST_SET_IRQTYPE:
755 ret = pci_endpoint_test_set_irq(test, arg);
757 case PCITEST_GET_IRQTYPE:
760 case PCITEST_CLEAR_IRQ:
761 ret = pci_endpoint_test_clear_irq(test);
766 mutex_unlock(&test->mutex);
770 static const struct file_operations pci_endpoint_test_fops = {
771 .owner = THIS_MODULE,
772 .unlocked_ioctl = pci_endpoint_test_ioctl,
775 static int pci_endpoint_test_probe(struct pci_dev *pdev,
776 const struct pci_device_id *ent)
783 struct device *dev = &pdev->dev;
784 struct pci_endpoint_test *test;
785 struct pci_endpoint_test_data *data;
786 enum pci_barno test_reg_bar = BAR_0;
787 struct miscdevice *misc_device;
789 if (pci_is_bridge(pdev))
792 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
796 test->test_reg_bar = 0;
799 test->irq_type = IRQ_TYPE_UNDEFINED;
802 irq_type = IRQ_TYPE_LEGACY;
804 data = (struct pci_endpoint_test_data *)ent->driver_data;
806 test_reg_bar = data->test_reg_bar;
807 test->test_reg_bar = test_reg_bar;
808 test->alignment = data->alignment;
809 irq_type = data->irq_type;
812 init_completion(&test->irq_raised);
813 mutex_init(&test->mutex);
815 if ((dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48)) != 0) &&
816 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
817 dev_err(dev, "Cannot set DMA mask\n");
821 err = pci_enable_device(pdev);
823 dev_err(dev, "Cannot enable PCI device\n");
827 err = pci_request_regions(pdev, DRV_MODULE_NAME);
829 dev_err(dev, "Cannot obtain PCI resources\n");
830 goto err_disable_pdev;
833 pci_set_master(pdev);
835 if (!pci_endpoint_test_alloc_irq_vectors(test, irq_type)) {
837 goto err_disable_irq;
840 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
841 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
842 base = pci_ioremap_bar(pdev, bar);
844 dev_err(dev, "Failed to read BAR%d\n", bar);
845 WARN_ON(bar == test_reg_bar);
847 test->bar[bar] = base;
851 test->base = test->bar[test_reg_bar];
854 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
859 pci_set_drvdata(pdev, test);
861 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
864 dev_err(dev, "Unable to get id\n");
868 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
869 test->name = kstrdup(name, GFP_KERNEL);
875 if (!pci_endpoint_test_request_irq(test)) {
877 goto err_kfree_test_name;
880 misc_device = &test->miscdev;
881 misc_device->minor = MISC_DYNAMIC_MINOR;
882 misc_device->name = kstrdup(name, GFP_KERNEL);
883 if (!misc_device->name) {
885 goto err_release_irq;
887 misc_device->parent = &pdev->dev;
888 misc_device->fops = &pci_endpoint_test_fops;
890 err = misc_register(misc_device);
892 dev_err(dev, "Failed to register device\n");
899 kfree(misc_device->name);
902 pci_endpoint_test_release_irq(test);
908 ida_simple_remove(&pci_endpoint_test_ida, id);
911 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
913 pci_iounmap(pdev, test->bar[bar]);
917 pci_endpoint_test_free_irq_vectors(test);
918 pci_release_regions(pdev);
921 pci_disable_device(pdev);
926 static void pci_endpoint_test_remove(struct pci_dev *pdev)
930 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
931 struct miscdevice *misc_device = &test->miscdev;
933 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
938 pci_endpoint_test_release_irq(test);
939 pci_endpoint_test_free_irq_vectors(test);
941 misc_deregister(&test->miscdev);
942 kfree(misc_device->name);
944 ida_simple_remove(&pci_endpoint_test_ida, id);
945 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
947 pci_iounmap(pdev, test->bar[bar]);
950 pci_release_regions(pdev);
951 pci_disable_device(pdev);
954 static const struct pci_endpoint_test_data default_data = {
955 .test_reg_bar = BAR_0,
957 .irq_type = IRQ_TYPE_MSI,
960 static const struct pci_endpoint_test_data am654_data = {
961 .test_reg_bar = BAR_2,
963 .irq_type = IRQ_TYPE_MSI,
966 static const struct pci_endpoint_test_data j721e_data = {
968 .irq_type = IRQ_TYPE_MSI,
971 static const struct pci_device_id pci_endpoint_test_tbl[] = {
972 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x),
973 .driver_data = (kernel_ulong_t)&default_data,
975 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x),
976 .driver_data = (kernel_ulong_t)&default_data,
978 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0x81c0),
979 .driver_data = (kernel_ulong_t)&default_data,
981 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_IMX8),},
982 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_LS1088A),
983 .driver_data = (kernel_ulong_t)&default_data,
985 { PCI_DEVICE_DATA(SYNOPSYS, EDDA, NULL) },
986 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM654),
987 .driver_data = (kernel_ulong_t)&am654_data
989 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774A1),},
990 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774B1),},
991 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774C0),},
992 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774E1),},
993 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_J721E),
994 .driver_data = (kernel_ulong_t)&j721e_data,
996 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_J7200),
997 .driver_data = (kernel_ulong_t)&j721e_data,
999 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM64),
1000 .driver_data = (kernel_ulong_t)&j721e_data,
1004 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
1006 static struct pci_driver pci_endpoint_test_driver = {
1007 .name = DRV_MODULE_NAME,
1008 .id_table = pci_endpoint_test_tbl,
1009 .probe = pci_endpoint_test_probe,
1010 .remove = pci_endpoint_test_remove,
1011 .sriov_configure = pci_sriov_configure_simple,
1013 module_pci_driver(pci_endpoint_test_driver);
1015 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
1016 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
1017 MODULE_LICENSE("GPL v2");