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
76 #define is_am654_pci_dev(pdev) \
77 ((pdev)->device == PCI_DEVICE_ID_TI_AM654)
79 #define PCI_DEVICE_ID_RENESAS_R8A774A1 0x0028
80 #define PCI_DEVICE_ID_RENESAS_R8A774B1 0x002b
81 #define PCI_DEVICE_ID_RENESAS_R8A774C0 0x002d
82 #define PCI_DEVICE_ID_RENESAS_R8A774E1 0x0025
84 static DEFINE_IDA(pci_endpoint_test_ida
);
86 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
90 module_param(no_msi
, bool, 0444);
91 MODULE_PARM_DESC(no_msi
, "Disable MSI interrupt in pci_endpoint_test");
93 static int irq_type
= IRQ_TYPE_MSI
;
94 module_param(irq_type
, int, 0444);
95 MODULE_PARM_DESC(irq_type
, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
106 struct pci_endpoint_test
{
107 struct pci_dev
*pdev
;
109 void __iomem
*bar
[PCI_STD_NUM_BARS
];
110 struct completion irq_raised
;
114 /* mutex to protect the ioctls */
116 struct miscdevice miscdev
;
117 enum pci_barno test_reg_bar
;
122 struct pci_endpoint_test_data
{
123 enum pci_barno test_reg_bar
;
128 static inline u32
pci_endpoint_test_readl(struct pci_endpoint_test
*test
,
131 return readl(test
->base
+ offset
);
134 static inline void pci_endpoint_test_writel(struct pci_endpoint_test
*test
,
135 u32 offset
, u32 value
)
137 writel(value
, test
->base
+ offset
);
140 static inline u32
pci_endpoint_test_bar_readl(struct pci_endpoint_test
*test
,
143 return readl(test
->bar
[bar
] + offset
);
146 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test
*test
,
147 int bar
, u32 offset
, u32 value
)
149 writel(value
, test
->bar
[bar
] + offset
);
152 static irqreturn_t
pci_endpoint_test_irqhandler(int irq
, void *dev_id
)
154 struct pci_endpoint_test
*test
= dev_id
;
157 reg
= pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_STATUS
);
158 if (reg
& STATUS_IRQ_RAISED
) {
159 test
->last_irq
= irq
;
160 complete(&test
->irq_raised
);
161 reg
&= ~STATUS_IRQ_RAISED
;
163 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_STATUS
,
169 static void pci_endpoint_test_free_irq_vectors(struct pci_endpoint_test
*test
)
171 struct pci_dev
*pdev
= test
->pdev
;
173 pci_free_irq_vectors(pdev
);
174 test
->irq_type
= IRQ_TYPE_UNDEFINED
;
177 static bool pci_endpoint_test_alloc_irq_vectors(struct pci_endpoint_test
*test
,
181 struct pci_dev
*pdev
= test
->pdev
;
182 struct device
*dev
= &pdev
->dev
;
186 case IRQ_TYPE_LEGACY
:
187 irq
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_LEGACY
);
189 dev_err(dev
, "Failed to get Legacy interrupt\n");
192 irq
= pci_alloc_irq_vectors(pdev
, 1, 32, PCI_IRQ_MSI
);
194 dev_err(dev
, "Failed to get MSI interrupts\n");
197 irq
= pci_alloc_irq_vectors(pdev
, 1, 2048, PCI_IRQ_MSIX
);
199 dev_err(dev
, "Failed to get MSI-X interrupts\n");
202 dev_err(dev
, "Invalid IRQ type selected\n");
210 test
->irq_type
= type
;
211 test
->num_irqs
= irq
;
216 static void pci_endpoint_test_release_irq(struct pci_endpoint_test
*test
)
219 struct pci_dev
*pdev
= test
->pdev
;
220 struct device
*dev
= &pdev
->dev
;
222 for (i
= 0; i
< test
->num_irqs
; i
++)
223 devm_free_irq(dev
, pci_irq_vector(pdev
, i
), test
);
228 static bool pci_endpoint_test_request_irq(struct pci_endpoint_test
*test
)
232 struct pci_dev
*pdev
= test
->pdev
;
233 struct device
*dev
= &pdev
->dev
;
235 for (i
= 0; i
< test
->num_irqs
; i
++) {
236 err
= devm_request_irq(dev
, pci_irq_vector(pdev
, i
),
237 pci_endpoint_test_irqhandler
,
238 IRQF_SHARED
, test
->name
, test
);
247 case IRQ_TYPE_LEGACY
:
248 dev_err(dev
, "Failed to request IRQ %d for Legacy\n",
249 pci_irq_vector(pdev
, i
));
252 dev_err(dev
, "Failed to request IRQ %d for MSI %d\n",
253 pci_irq_vector(pdev
, i
),
257 dev_err(dev
, "Failed to request IRQ %d for MSI-X %d\n",
258 pci_irq_vector(pdev
, i
),
266 static bool pci_endpoint_test_bar(struct pci_endpoint_test
*test
,
267 enum pci_barno barno
)
272 struct pci_dev
*pdev
= test
->pdev
;
274 if (!test
->bar
[barno
])
277 size
= pci_resource_len(pdev
, barno
);
279 if (barno
== test
->test_reg_bar
)
282 for (j
= 0; j
< size
; j
+= 4)
283 pci_endpoint_test_bar_writel(test
, barno
, j
, 0xA0A0A0A0);
285 for (j
= 0; j
< size
; j
+= 4) {
286 val
= pci_endpoint_test_bar_readl(test
, barno
, j
);
287 if (val
!= 0xA0A0A0A0)
294 static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test
*test
)
298 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
,
300 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 0);
301 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
302 COMMAND_RAISE_LEGACY_IRQ
);
303 val
= wait_for_completion_timeout(&test
->irq_raised
,
304 msecs_to_jiffies(1000));
311 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test
*test
,
312 u16 msi_num
, bool msix
)
315 struct pci_dev
*pdev
= test
->pdev
;
317 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
,
318 msix
== false ? IRQ_TYPE_MSI
:
320 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, msi_num
);
321 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
322 msix
== false ? COMMAND_RAISE_MSI_IRQ
:
323 COMMAND_RAISE_MSIX_IRQ
);
324 val
= wait_for_completion_timeout(&test
->irq_raised
,
325 msecs_to_jiffies(1000));
329 if (pci_irq_vector(pdev
, msi_num
- 1) == test
->last_irq
)
335 static bool pci_endpoint_test_copy(struct pci_endpoint_test
*test
,
338 struct pci_endpoint_test_xfer_param param
;
345 dma_addr_t src_phys_addr
;
346 dma_addr_t dst_phys_addr
;
347 struct pci_dev
*pdev
= test
->pdev
;
348 struct device
*dev
= &pdev
->dev
;
350 dma_addr_t orig_src_phys_addr
;
352 dma_addr_t orig_dst_phys_addr
;
354 size_t alignment
= test
->alignment
;
355 int irq_type
= test
->irq_type
;
360 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
362 dev_err(dev
, "Failed to get transfer param\n");
367 if (size
> SIZE_MAX
- alignment
)
370 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
372 flags
|= FLAG_USE_DMA
;
374 if (irq_type
< IRQ_TYPE_LEGACY
|| irq_type
> IRQ_TYPE_MSIX
) {
375 dev_err(dev
, "Invalid IRQ type option\n");
379 orig_src_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
380 if (!orig_src_addr
) {
381 dev_err(dev
, "Failed to allocate source buffer\n");
386 get_random_bytes(orig_src_addr
, size
+ alignment
);
387 orig_src_phys_addr
= dma_map_single(dev
, orig_src_addr
,
388 size
+ alignment
, DMA_TO_DEVICE
);
389 if (dma_mapping_error(dev
, orig_src_phys_addr
)) {
390 dev_err(dev
, "failed to map source buffer address\n");
392 goto err_src_phys_addr
;
395 if (alignment
&& !IS_ALIGNED(orig_src_phys_addr
, alignment
)) {
396 src_phys_addr
= PTR_ALIGN(orig_src_phys_addr
, alignment
);
397 offset
= src_phys_addr
- orig_src_phys_addr
;
398 src_addr
= orig_src_addr
+ offset
;
400 src_phys_addr
= orig_src_phys_addr
;
401 src_addr
= orig_src_addr
;
404 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR
,
405 lower_32_bits(src_phys_addr
));
407 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR
,
408 upper_32_bits(src_phys_addr
));
410 src_crc32
= crc32_le(~0, src_addr
, size
);
412 orig_dst_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
413 if (!orig_dst_addr
) {
414 dev_err(dev
, "Failed to allocate destination address\n");
419 orig_dst_phys_addr
= dma_map_single(dev
, orig_dst_addr
,
420 size
+ alignment
, DMA_FROM_DEVICE
);
421 if (dma_mapping_error(dev
, orig_dst_phys_addr
)) {
422 dev_err(dev
, "failed to map destination buffer address\n");
424 goto err_dst_phys_addr
;
427 if (alignment
&& !IS_ALIGNED(orig_dst_phys_addr
, alignment
)) {
428 dst_phys_addr
= PTR_ALIGN(orig_dst_phys_addr
, alignment
);
429 offset
= dst_phys_addr
- orig_dst_phys_addr
;
430 dst_addr
= orig_dst_addr
+ offset
;
432 dst_phys_addr
= orig_dst_phys_addr
;
433 dst_addr
= orig_dst_addr
;
436 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_DST_ADDR
,
437 lower_32_bits(dst_phys_addr
));
438 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_DST_ADDR
,
439 upper_32_bits(dst_phys_addr
));
441 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
,
444 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
445 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
446 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
447 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
450 wait_for_completion(&test
->irq_raised
);
452 dma_unmap_single(dev
, orig_dst_phys_addr
, size
+ alignment
,
455 dst_crc32
= crc32_le(~0, dst_addr
, size
);
456 if (dst_crc32
== src_crc32
)
460 kfree(orig_dst_addr
);
463 dma_unmap_single(dev
, orig_src_phys_addr
, size
+ alignment
,
467 kfree(orig_src_addr
);
473 static bool pci_endpoint_test_write(struct pci_endpoint_test
*test
,
476 struct pci_endpoint_test_xfer_param param
;
482 dma_addr_t phys_addr
;
483 struct pci_dev
*pdev
= test
->pdev
;
484 struct device
*dev
= &pdev
->dev
;
486 dma_addr_t orig_phys_addr
;
488 size_t alignment
= test
->alignment
;
489 int irq_type
= test
->irq_type
;
494 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
496 dev_err(dev
, "Failed to get transfer param\n");
501 if (size
> SIZE_MAX
- alignment
)
504 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
506 flags
|= FLAG_USE_DMA
;
508 if (irq_type
< IRQ_TYPE_LEGACY
|| irq_type
> IRQ_TYPE_MSIX
) {
509 dev_err(dev
, "Invalid IRQ type option\n");
513 orig_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
515 dev_err(dev
, "Failed to allocate address\n");
520 get_random_bytes(orig_addr
, size
+ alignment
);
522 orig_phys_addr
= dma_map_single(dev
, orig_addr
, size
+ alignment
,
524 if (dma_mapping_error(dev
, orig_phys_addr
)) {
525 dev_err(dev
, "failed to map source buffer address\n");
530 if (alignment
&& !IS_ALIGNED(orig_phys_addr
, alignment
)) {
531 phys_addr
= PTR_ALIGN(orig_phys_addr
, alignment
);
532 offset
= phys_addr
- orig_phys_addr
;
533 addr
= orig_addr
+ offset
;
535 phys_addr
= orig_phys_addr
;
539 crc32
= crc32_le(~0, addr
, size
);
540 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_CHECKSUM
,
543 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR
,
544 lower_32_bits(phys_addr
));
545 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR
,
546 upper_32_bits(phys_addr
));
548 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
, size
);
550 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
551 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
552 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
553 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
556 wait_for_completion(&test
->irq_raised
);
558 reg
= pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_STATUS
);
559 if (reg
& STATUS_READ_SUCCESS
)
562 dma_unmap_single(dev
, orig_phys_addr
, size
+ alignment
,
572 static bool pci_endpoint_test_read(struct pci_endpoint_test
*test
,
575 struct pci_endpoint_test_xfer_param param
;
581 dma_addr_t phys_addr
;
582 struct pci_dev
*pdev
= test
->pdev
;
583 struct device
*dev
= &pdev
->dev
;
585 dma_addr_t orig_phys_addr
;
587 size_t alignment
= test
->alignment
;
588 int irq_type
= test
->irq_type
;
592 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
594 dev_err(dev
, "Failed to get transfer param\n");
599 if (size
> SIZE_MAX
- alignment
)
602 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
604 flags
|= FLAG_USE_DMA
;
606 if (irq_type
< IRQ_TYPE_LEGACY
|| irq_type
> IRQ_TYPE_MSIX
) {
607 dev_err(dev
, "Invalid IRQ type option\n");
611 orig_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
613 dev_err(dev
, "Failed to allocate destination address\n");
618 orig_phys_addr
= dma_map_single(dev
, orig_addr
, size
+ alignment
,
620 if (dma_mapping_error(dev
, orig_phys_addr
)) {
621 dev_err(dev
, "failed to map source buffer address\n");
626 if (alignment
&& !IS_ALIGNED(orig_phys_addr
, alignment
)) {
627 phys_addr
= PTR_ALIGN(orig_phys_addr
, alignment
);
628 offset
= phys_addr
- orig_phys_addr
;
629 addr
= orig_addr
+ offset
;
631 phys_addr
= orig_phys_addr
;
635 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_DST_ADDR
,
636 lower_32_bits(phys_addr
));
637 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_DST_ADDR
,
638 upper_32_bits(phys_addr
));
640 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
, size
);
642 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
643 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
644 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
645 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
648 wait_for_completion(&test
->irq_raised
);
650 dma_unmap_single(dev
, orig_phys_addr
, size
+ alignment
,
653 crc32
= crc32_le(~0, addr
, size
);
654 if (crc32
== pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_CHECKSUM
))
663 static bool pci_endpoint_test_clear_irq(struct pci_endpoint_test
*test
)
665 pci_endpoint_test_release_irq(test
);
666 pci_endpoint_test_free_irq_vectors(test
);
670 static bool pci_endpoint_test_set_irq(struct pci_endpoint_test
*test
,
673 struct pci_dev
*pdev
= test
->pdev
;
674 struct device
*dev
= &pdev
->dev
;
676 if (req_irq_type
< IRQ_TYPE_LEGACY
|| req_irq_type
> IRQ_TYPE_MSIX
) {
677 dev_err(dev
, "Invalid IRQ type option\n");
681 if (test
->irq_type
== req_irq_type
)
684 pci_endpoint_test_release_irq(test
);
685 pci_endpoint_test_free_irq_vectors(test
);
687 if (!pci_endpoint_test_alloc_irq_vectors(test
, req_irq_type
))
690 if (!pci_endpoint_test_request_irq(test
))
696 pci_endpoint_test_free_irq_vectors(test
);
700 static long pci_endpoint_test_ioctl(struct file
*file
, unsigned int cmd
,
705 struct pci_endpoint_test
*test
= to_endpoint_test(file
->private_data
);
706 struct pci_dev
*pdev
= test
->pdev
;
708 mutex_lock(&test
->mutex
);
714 if (is_am654_pci_dev(pdev
) && bar
== BAR_0
)
716 ret
= pci_endpoint_test_bar(test
, bar
);
718 case PCITEST_LEGACY_IRQ
:
719 ret
= pci_endpoint_test_legacy_irq(test
);
723 ret
= pci_endpoint_test_msi_irq(test
, arg
, cmd
== PCITEST_MSIX
);
726 ret
= pci_endpoint_test_write(test
, arg
);
729 ret
= pci_endpoint_test_read(test
, arg
);
732 ret
= pci_endpoint_test_copy(test
, arg
);
734 case PCITEST_SET_IRQTYPE
:
735 ret
= pci_endpoint_test_set_irq(test
, arg
);
737 case PCITEST_GET_IRQTYPE
:
740 case PCITEST_CLEAR_IRQ
:
741 ret
= pci_endpoint_test_clear_irq(test
);
746 mutex_unlock(&test
->mutex
);
750 static const struct file_operations pci_endpoint_test_fops
= {
751 .owner
= THIS_MODULE
,
752 .unlocked_ioctl
= pci_endpoint_test_ioctl
,
755 static int pci_endpoint_test_probe(struct pci_dev
*pdev
,
756 const struct pci_device_id
*ent
)
763 struct device
*dev
= &pdev
->dev
;
764 struct pci_endpoint_test
*test
;
765 struct pci_endpoint_test_data
*data
;
766 enum pci_barno test_reg_bar
= BAR_0
;
767 struct miscdevice
*misc_device
;
769 if (pci_is_bridge(pdev
))
772 test
= devm_kzalloc(dev
, sizeof(*test
), GFP_KERNEL
);
776 test
->test_reg_bar
= 0;
779 test
->irq_type
= IRQ_TYPE_UNDEFINED
;
782 irq_type
= IRQ_TYPE_LEGACY
;
784 data
= (struct pci_endpoint_test_data
*)ent
->driver_data
;
786 test_reg_bar
= data
->test_reg_bar
;
787 test
->test_reg_bar
= test_reg_bar
;
788 test
->alignment
= data
->alignment
;
789 irq_type
= data
->irq_type
;
792 init_completion(&test
->irq_raised
);
793 mutex_init(&test
->mutex
);
795 if ((dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(48)) != 0) &&
796 dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32)) != 0) {
797 dev_err(dev
, "Cannot set DMA mask\n");
801 err
= pci_enable_device(pdev
);
803 dev_err(dev
, "Cannot enable PCI device\n");
807 err
= pci_request_regions(pdev
, DRV_MODULE_NAME
);
809 dev_err(dev
, "Cannot obtain PCI resources\n");
810 goto err_disable_pdev
;
813 pci_set_master(pdev
);
815 if (!pci_endpoint_test_alloc_irq_vectors(test
, irq_type
)) {
817 goto err_disable_irq
;
820 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
821 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
) {
822 base
= pci_ioremap_bar(pdev
, bar
);
824 dev_err(dev
, "Failed to read BAR%d\n", bar
);
825 WARN_ON(bar
== test_reg_bar
);
827 test
->bar
[bar
] = base
;
831 test
->base
= test
->bar
[test_reg_bar
];
834 dev_err(dev
, "Cannot perform PCI test without BAR%d\n",
839 pci_set_drvdata(pdev
, test
);
841 id
= ida_simple_get(&pci_endpoint_test_ida
, 0, 0, GFP_KERNEL
);
844 dev_err(dev
, "Unable to get id\n");
848 snprintf(name
, sizeof(name
), DRV_MODULE_NAME
".%d", id
);
849 test
->name
= kstrdup(name
, GFP_KERNEL
);
855 if (!pci_endpoint_test_request_irq(test
)) {
857 goto err_kfree_test_name
;
860 misc_device
= &test
->miscdev
;
861 misc_device
->minor
= MISC_DYNAMIC_MINOR
;
862 misc_device
->name
= kstrdup(name
, GFP_KERNEL
);
863 if (!misc_device
->name
) {
865 goto err_release_irq
;
867 misc_device
->parent
= &pdev
->dev
;
868 misc_device
->fops
= &pci_endpoint_test_fops
,
870 err
= misc_register(misc_device
);
872 dev_err(dev
, "Failed to register device\n");
879 kfree(misc_device
->name
);
882 pci_endpoint_test_release_irq(test
);
888 ida_simple_remove(&pci_endpoint_test_ida
, id
);
891 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
893 pci_iounmap(pdev
, test
->bar
[bar
]);
897 pci_endpoint_test_free_irq_vectors(test
);
898 pci_release_regions(pdev
);
901 pci_disable_device(pdev
);
906 static void pci_endpoint_test_remove(struct pci_dev
*pdev
)
910 struct pci_endpoint_test
*test
= pci_get_drvdata(pdev
);
911 struct miscdevice
*misc_device
= &test
->miscdev
;
913 if (sscanf(misc_device
->name
, DRV_MODULE_NAME
".%d", &id
) != 1)
918 misc_deregister(&test
->miscdev
);
919 kfree(misc_device
->name
);
921 ida_simple_remove(&pci_endpoint_test_ida
, id
);
922 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
924 pci_iounmap(pdev
, test
->bar
[bar
]);
927 pci_endpoint_test_release_irq(test
);
928 pci_endpoint_test_free_irq_vectors(test
);
930 pci_release_regions(pdev
);
931 pci_disable_device(pdev
);
934 static const struct pci_endpoint_test_data default_data
= {
935 .test_reg_bar
= BAR_0
,
937 .irq_type
= IRQ_TYPE_MSI
,
940 static const struct pci_endpoint_test_data am654_data
= {
941 .test_reg_bar
= BAR_2
,
943 .irq_type
= IRQ_TYPE_MSI
,
946 static const struct pci_endpoint_test_data j721e_data
= {
948 .irq_type
= IRQ_TYPE_MSI
,
951 static const struct pci_device_id pci_endpoint_test_tbl
[] = {
952 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA74x
),
953 .driver_data
= (kernel_ulong_t
)&default_data
,
955 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA72x
),
956 .driver_data
= (kernel_ulong_t
)&default_data
,
958 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE
, 0x81c0),
959 .driver_data
= (kernel_ulong_t
)&default_data
,
961 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE
, PCI_DEVICE_ID_LS1088A
),
962 .driver_data
= (kernel_ulong_t
)&default_data
,
964 { PCI_DEVICE_DATA(SYNOPSYS
, EDDA
, NULL
) },
965 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_AM654
),
966 .driver_data
= (kernel_ulong_t
)&am654_data
968 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774A1
),},
969 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774B1
),},
970 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774C0
),},
971 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774E1
),},
972 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_J721E
),
973 .driver_data
= (kernel_ulong_t
)&j721e_data
,
975 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_J7200
),
976 .driver_data
= (kernel_ulong_t
)&j721e_data
,
978 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_AM64
),
979 .driver_data
= (kernel_ulong_t
)&j721e_data
,
983 MODULE_DEVICE_TABLE(pci
, pci_endpoint_test_tbl
);
985 static struct pci_driver pci_endpoint_test_driver
= {
986 .name
= DRV_MODULE_NAME
,
987 .id_table
= pci_endpoint_test_tbl
,
988 .probe
= pci_endpoint_test_probe
,
989 .remove
= pci_endpoint_test_remove
,
990 .sriov_configure
= pci_sriov_configure_simple
,
992 module_pci_driver(pci_endpoint_test_driver
);
994 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
995 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
996 MODULE_LICENSE("GPL v2");