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/cleanup.h>
11 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/miscdevice.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/random.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/pci.h>
23 #include <linux/pci_ids.h>
25 #include <linux/pci_regs.h>
27 #include <uapi/linux/pcitest.h>
29 #define DRV_MODULE_NAME "pci-endpoint-test"
31 #define IRQ_TYPE_UNDEFINED -1
32 #define IRQ_TYPE_INTX 0
33 #define IRQ_TYPE_MSI 1
34 #define IRQ_TYPE_MSIX 2
36 #define PCI_ENDPOINT_TEST_MAGIC 0x0
38 #define PCI_ENDPOINT_TEST_COMMAND 0x4
39 #define COMMAND_RAISE_INTX_IRQ BIT(0)
40 #define COMMAND_RAISE_MSI_IRQ BIT(1)
41 #define COMMAND_RAISE_MSIX_IRQ BIT(2)
42 #define COMMAND_READ BIT(3)
43 #define COMMAND_WRITE BIT(4)
44 #define COMMAND_COPY BIT(5)
46 #define PCI_ENDPOINT_TEST_STATUS 0x8
47 #define STATUS_READ_SUCCESS BIT(0)
48 #define STATUS_READ_FAIL BIT(1)
49 #define STATUS_WRITE_SUCCESS BIT(2)
50 #define STATUS_WRITE_FAIL BIT(3)
51 #define STATUS_COPY_SUCCESS BIT(4)
52 #define STATUS_COPY_FAIL BIT(5)
53 #define STATUS_IRQ_RAISED BIT(6)
54 #define STATUS_SRC_ADDR_INVALID BIT(7)
55 #define STATUS_DST_ADDR_INVALID BIT(8)
57 #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
58 #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
60 #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
61 #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
63 #define PCI_ENDPOINT_TEST_SIZE 0x1c
64 #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
66 #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
67 #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
69 #define PCI_ENDPOINT_TEST_FLAGS 0x2c
70 #define FLAG_USE_DMA BIT(0)
72 #define PCI_DEVICE_ID_TI_AM654 0xb00c
73 #define PCI_DEVICE_ID_TI_J7200 0xb00f
74 #define PCI_DEVICE_ID_TI_AM64 0xb010
75 #define PCI_DEVICE_ID_TI_J721S2 0xb013
76 #define PCI_DEVICE_ID_LS1088A 0x80c0
77 #define PCI_DEVICE_ID_IMX8 0x0808
79 #define is_am654_pci_dev(pdev) \
80 ((pdev)->device == PCI_DEVICE_ID_TI_AM654)
82 #define PCI_DEVICE_ID_RENESAS_R8A774A1 0x0028
83 #define PCI_DEVICE_ID_RENESAS_R8A774B1 0x002b
84 #define PCI_DEVICE_ID_RENESAS_R8A774C0 0x002d
85 #define PCI_DEVICE_ID_RENESAS_R8A774E1 0x0025
86 #define PCI_DEVICE_ID_RENESAS_R8A779F0 0x0031
88 #define PCI_VENDOR_ID_ROCKCHIP 0x1d87
89 #define PCI_DEVICE_ID_ROCKCHIP_RK3588 0x3588
91 static DEFINE_IDA(pci_endpoint_test_ida
);
93 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
97 module_param(no_msi
, bool, 0444);
98 MODULE_PARM_DESC(no_msi
, "Disable MSI interrupt in pci_endpoint_test");
100 static int irq_type
= IRQ_TYPE_MSI
;
101 module_param(irq_type
, int, 0444);
102 MODULE_PARM_DESC(irq_type
, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
113 struct pci_endpoint_test
{
114 struct pci_dev
*pdev
;
116 void __iomem
*bar
[PCI_STD_NUM_BARS
];
117 struct completion irq_raised
;
121 /* mutex to protect the ioctls */
123 struct miscdevice miscdev
;
124 enum pci_barno test_reg_bar
;
129 struct pci_endpoint_test_data
{
130 enum pci_barno test_reg_bar
;
135 static inline u32
pci_endpoint_test_readl(struct pci_endpoint_test
*test
,
138 return readl(test
->base
+ offset
);
141 static inline void pci_endpoint_test_writel(struct pci_endpoint_test
*test
,
142 u32 offset
, u32 value
)
144 writel(value
, test
->base
+ offset
);
147 static irqreturn_t
pci_endpoint_test_irqhandler(int irq
, void *dev_id
)
149 struct pci_endpoint_test
*test
= dev_id
;
152 reg
= pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_STATUS
);
153 if (reg
& STATUS_IRQ_RAISED
) {
154 test
->last_irq
= irq
;
155 complete(&test
->irq_raised
);
161 static void pci_endpoint_test_free_irq_vectors(struct pci_endpoint_test
*test
)
163 struct pci_dev
*pdev
= test
->pdev
;
165 pci_free_irq_vectors(pdev
);
166 test
->irq_type
= IRQ_TYPE_UNDEFINED
;
169 static bool pci_endpoint_test_alloc_irq_vectors(struct pci_endpoint_test
*test
,
173 struct pci_dev
*pdev
= test
->pdev
;
174 struct device
*dev
= &pdev
->dev
;
179 irq
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_INTX
);
181 dev_err(dev
, "Failed to get Legacy interrupt\n");
184 irq
= pci_alloc_irq_vectors(pdev
, 1, 32, PCI_IRQ_MSI
);
186 dev_err(dev
, "Failed to get MSI interrupts\n");
189 irq
= pci_alloc_irq_vectors(pdev
, 1, 2048, PCI_IRQ_MSIX
);
191 dev_err(dev
, "Failed to get MSI-X interrupts\n");
194 dev_err(dev
, "Invalid IRQ type selected\n");
202 test
->irq_type
= type
;
203 test
->num_irqs
= irq
;
208 static void pci_endpoint_test_release_irq(struct pci_endpoint_test
*test
)
211 struct pci_dev
*pdev
= test
->pdev
;
212 struct device
*dev
= &pdev
->dev
;
214 for (i
= 0; i
< test
->num_irqs
; i
++)
215 devm_free_irq(dev
, pci_irq_vector(pdev
, i
), test
);
220 static bool pci_endpoint_test_request_irq(struct pci_endpoint_test
*test
)
224 struct pci_dev
*pdev
= test
->pdev
;
225 struct device
*dev
= &pdev
->dev
;
227 for (i
= 0; i
< test
->num_irqs
; i
++) {
228 err
= devm_request_irq(dev
, pci_irq_vector(pdev
, i
),
229 pci_endpoint_test_irqhandler
,
230 IRQF_SHARED
, test
->name
, test
);
240 dev_err(dev
, "Failed to request IRQ %d for Legacy\n",
241 pci_irq_vector(pdev
, i
));
244 dev_err(dev
, "Failed to request IRQ %d for MSI %d\n",
245 pci_irq_vector(pdev
, i
),
249 dev_err(dev
, "Failed to request IRQ %d for MSI-X %d\n",
250 pci_irq_vector(pdev
, i
),
258 static const u32 bar_test_pattern
[] = {
267 static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test
*test
,
268 enum pci_barno barno
, int offset
,
269 void *write_buf
, void *read_buf
,
272 memset(write_buf
, bar_test_pattern
[barno
], size
);
273 memcpy_toio(test
->bar
[barno
] + offset
, write_buf
, size
);
275 memcpy_fromio(read_buf
, test
->bar
[barno
] + offset
, size
);
277 return memcmp(write_buf
, read_buf
, size
);
280 static bool pci_endpoint_test_bar(struct pci_endpoint_test
*test
,
281 enum pci_barno barno
)
283 int j
, bar_size
, buf_size
, iters
, remain
;
284 void *write_buf
__free(kfree
) = NULL
;
285 void *read_buf
__free(kfree
) = NULL
;
286 struct pci_dev
*pdev
= test
->pdev
;
288 if (!test
->bar
[barno
])
291 bar_size
= pci_resource_len(pdev
, barno
);
293 if (barno
== test
->test_reg_bar
)
297 * Allocate a buffer of max size 1MB, and reuse that buffer while
298 * iterating over the whole BAR size (which might be much larger).
300 buf_size
= min(SZ_1M
, bar_size
);
302 write_buf
= kmalloc(buf_size
, GFP_KERNEL
);
306 read_buf
= kmalloc(buf_size
, GFP_KERNEL
);
310 iters
= bar_size
/ buf_size
;
311 for (j
= 0; j
< iters
; j
++)
312 if (pci_endpoint_test_bar_memcmp(test
, barno
, buf_size
* j
,
313 write_buf
, read_buf
, buf_size
))
316 remain
= bar_size
% buf_size
;
318 if (pci_endpoint_test_bar_memcmp(test
, barno
, buf_size
* iters
,
319 write_buf
, read_buf
, remain
))
325 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test
*test
)
329 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
,
331 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 0);
332 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
333 COMMAND_RAISE_INTX_IRQ
);
334 val
= wait_for_completion_timeout(&test
->irq_raised
,
335 msecs_to_jiffies(1000));
342 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test
*test
,
343 u16 msi_num
, bool msix
)
346 struct pci_dev
*pdev
= test
->pdev
;
348 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
,
349 msix
? IRQ_TYPE_MSIX
: IRQ_TYPE_MSI
);
350 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, msi_num
);
351 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
352 msix
? COMMAND_RAISE_MSIX_IRQ
:
353 COMMAND_RAISE_MSI_IRQ
);
354 val
= wait_for_completion_timeout(&test
->irq_raised
,
355 msecs_to_jiffies(1000));
359 return pci_irq_vector(pdev
, msi_num
- 1) == test
->last_irq
;
362 static int pci_endpoint_test_validate_xfer_params(struct device
*dev
,
363 struct pci_endpoint_test_xfer_param
*param
, size_t alignment
)
366 dev_dbg(dev
, "Data size is zero\n");
370 if (param
->size
> SIZE_MAX
- alignment
) {
371 dev_dbg(dev
, "Maximum transfer data size exceeded\n");
378 static bool pci_endpoint_test_copy(struct pci_endpoint_test
*test
,
381 struct pci_endpoint_test_xfer_param param
;
388 dma_addr_t src_phys_addr
;
389 dma_addr_t dst_phys_addr
;
390 struct pci_dev
*pdev
= test
->pdev
;
391 struct device
*dev
= &pdev
->dev
;
393 dma_addr_t orig_src_phys_addr
;
395 dma_addr_t orig_dst_phys_addr
;
397 size_t alignment
= test
->alignment
;
398 int irq_type
= test
->irq_type
;
403 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
405 dev_err(dev
, "Failed to get transfer param\n");
409 err
= pci_endpoint_test_validate_xfer_params(dev
, ¶m
, alignment
);
415 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
417 flags
|= FLAG_USE_DMA
;
419 if (irq_type
< IRQ_TYPE_INTX
|| irq_type
> IRQ_TYPE_MSIX
) {
420 dev_err(dev
, "Invalid IRQ type option\n");
424 orig_src_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
425 if (!orig_src_addr
) {
426 dev_err(dev
, "Failed to allocate source buffer\n");
431 get_random_bytes(orig_src_addr
, size
+ alignment
);
432 orig_src_phys_addr
= dma_map_single(dev
, orig_src_addr
,
433 size
+ alignment
, DMA_TO_DEVICE
);
434 if (dma_mapping_error(dev
, orig_src_phys_addr
)) {
435 dev_err(dev
, "failed to map source buffer address\n");
437 goto err_src_phys_addr
;
440 if (alignment
&& !IS_ALIGNED(orig_src_phys_addr
, alignment
)) {
441 src_phys_addr
= PTR_ALIGN(orig_src_phys_addr
, alignment
);
442 offset
= src_phys_addr
- orig_src_phys_addr
;
443 src_addr
= orig_src_addr
+ offset
;
445 src_phys_addr
= orig_src_phys_addr
;
446 src_addr
= orig_src_addr
;
449 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR
,
450 lower_32_bits(src_phys_addr
));
452 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR
,
453 upper_32_bits(src_phys_addr
));
455 src_crc32
= crc32_le(~0, src_addr
, size
);
457 orig_dst_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
458 if (!orig_dst_addr
) {
459 dev_err(dev
, "Failed to allocate destination address\n");
464 orig_dst_phys_addr
= dma_map_single(dev
, orig_dst_addr
,
465 size
+ alignment
, DMA_FROM_DEVICE
);
466 if (dma_mapping_error(dev
, orig_dst_phys_addr
)) {
467 dev_err(dev
, "failed to map destination buffer address\n");
469 goto err_dst_phys_addr
;
472 if (alignment
&& !IS_ALIGNED(orig_dst_phys_addr
, alignment
)) {
473 dst_phys_addr
= PTR_ALIGN(orig_dst_phys_addr
, alignment
);
474 offset
= dst_phys_addr
- orig_dst_phys_addr
;
475 dst_addr
= orig_dst_addr
+ offset
;
477 dst_phys_addr
= orig_dst_phys_addr
;
478 dst_addr
= orig_dst_addr
;
481 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_DST_ADDR
,
482 lower_32_bits(dst_phys_addr
));
483 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_DST_ADDR
,
484 upper_32_bits(dst_phys_addr
));
486 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
,
489 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
490 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
491 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
492 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
495 wait_for_completion(&test
->irq_raised
);
497 dma_unmap_single(dev
, orig_dst_phys_addr
, size
+ alignment
,
500 dst_crc32
= crc32_le(~0, dst_addr
, size
);
501 if (dst_crc32
== src_crc32
)
505 kfree(orig_dst_addr
);
508 dma_unmap_single(dev
, orig_src_phys_addr
, size
+ alignment
,
512 kfree(orig_src_addr
);
518 static bool pci_endpoint_test_write(struct pci_endpoint_test
*test
,
521 struct pci_endpoint_test_xfer_param param
;
527 dma_addr_t phys_addr
;
528 struct pci_dev
*pdev
= test
->pdev
;
529 struct device
*dev
= &pdev
->dev
;
531 dma_addr_t orig_phys_addr
;
533 size_t alignment
= test
->alignment
;
534 int irq_type
= test
->irq_type
;
539 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
541 dev_err(dev
, "Failed to get transfer param\n");
545 err
= pci_endpoint_test_validate_xfer_params(dev
, ¶m
, alignment
);
551 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
553 flags
|= FLAG_USE_DMA
;
555 if (irq_type
< IRQ_TYPE_INTX
|| irq_type
> IRQ_TYPE_MSIX
) {
556 dev_err(dev
, "Invalid IRQ type option\n");
560 orig_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
562 dev_err(dev
, "Failed to allocate address\n");
567 get_random_bytes(orig_addr
, size
+ alignment
);
569 orig_phys_addr
= dma_map_single(dev
, orig_addr
, size
+ alignment
,
571 if (dma_mapping_error(dev
, orig_phys_addr
)) {
572 dev_err(dev
, "failed to map source buffer address\n");
577 if (alignment
&& !IS_ALIGNED(orig_phys_addr
, alignment
)) {
578 phys_addr
= PTR_ALIGN(orig_phys_addr
, alignment
);
579 offset
= phys_addr
- orig_phys_addr
;
580 addr
= orig_addr
+ offset
;
582 phys_addr
= orig_phys_addr
;
586 crc32
= crc32_le(~0, addr
, size
);
587 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_CHECKSUM
,
590 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR
,
591 lower_32_bits(phys_addr
));
592 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR
,
593 upper_32_bits(phys_addr
));
595 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
, size
);
597 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
598 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
599 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
600 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
603 wait_for_completion(&test
->irq_raised
);
605 reg
= pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_STATUS
);
606 if (reg
& STATUS_READ_SUCCESS
)
609 dma_unmap_single(dev
, orig_phys_addr
, size
+ alignment
,
619 static bool pci_endpoint_test_read(struct pci_endpoint_test
*test
,
622 struct pci_endpoint_test_xfer_param param
;
628 dma_addr_t phys_addr
;
629 struct pci_dev
*pdev
= test
->pdev
;
630 struct device
*dev
= &pdev
->dev
;
632 dma_addr_t orig_phys_addr
;
634 size_t alignment
= test
->alignment
;
635 int irq_type
= test
->irq_type
;
639 err
= copy_from_user(¶m
, (void __user
*)arg
, sizeof(param
));
641 dev_err(dev
, "Failed to get transfer param\n");
645 err
= pci_endpoint_test_validate_xfer_params(dev
, ¶m
, alignment
);
651 use_dma
= !!(param
.flags
& PCITEST_FLAGS_USE_DMA
);
653 flags
|= FLAG_USE_DMA
;
655 if (irq_type
< IRQ_TYPE_INTX
|| irq_type
> IRQ_TYPE_MSIX
) {
656 dev_err(dev
, "Invalid IRQ type option\n");
660 orig_addr
= kzalloc(size
+ alignment
, GFP_KERNEL
);
662 dev_err(dev
, "Failed to allocate destination address\n");
667 orig_phys_addr
= dma_map_single(dev
, orig_addr
, size
+ alignment
,
669 if (dma_mapping_error(dev
, orig_phys_addr
)) {
670 dev_err(dev
, "failed to map source buffer address\n");
675 if (alignment
&& !IS_ALIGNED(orig_phys_addr
, alignment
)) {
676 phys_addr
= PTR_ALIGN(orig_phys_addr
, alignment
);
677 offset
= phys_addr
- orig_phys_addr
;
678 addr
= orig_addr
+ offset
;
680 phys_addr
= orig_phys_addr
;
684 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_LOWER_DST_ADDR
,
685 lower_32_bits(phys_addr
));
686 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_UPPER_DST_ADDR
,
687 upper_32_bits(phys_addr
));
689 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_SIZE
, size
);
691 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_FLAGS
, flags
);
692 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_TYPE
, irq_type
);
693 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_IRQ_NUMBER
, 1);
694 pci_endpoint_test_writel(test
, PCI_ENDPOINT_TEST_COMMAND
,
697 wait_for_completion(&test
->irq_raised
);
699 dma_unmap_single(dev
, orig_phys_addr
, size
+ alignment
,
702 crc32
= crc32_le(~0, addr
, size
);
703 if (crc32
== pci_endpoint_test_readl(test
, PCI_ENDPOINT_TEST_CHECKSUM
))
712 static bool pci_endpoint_test_clear_irq(struct pci_endpoint_test
*test
)
714 pci_endpoint_test_release_irq(test
);
715 pci_endpoint_test_free_irq_vectors(test
);
719 static bool pci_endpoint_test_set_irq(struct pci_endpoint_test
*test
,
722 struct pci_dev
*pdev
= test
->pdev
;
723 struct device
*dev
= &pdev
->dev
;
725 if (req_irq_type
< IRQ_TYPE_INTX
|| req_irq_type
> IRQ_TYPE_MSIX
) {
726 dev_err(dev
, "Invalid IRQ type option\n");
730 if (test
->irq_type
== req_irq_type
)
733 pci_endpoint_test_release_irq(test
);
734 pci_endpoint_test_free_irq_vectors(test
);
736 if (!pci_endpoint_test_alloc_irq_vectors(test
, req_irq_type
))
739 if (!pci_endpoint_test_request_irq(test
))
745 pci_endpoint_test_free_irq_vectors(test
);
749 static long pci_endpoint_test_ioctl(struct file
*file
, unsigned int cmd
,
754 struct pci_endpoint_test
*test
= to_endpoint_test(file
->private_data
);
755 struct pci_dev
*pdev
= test
->pdev
;
757 mutex_lock(&test
->mutex
);
759 reinit_completion(&test
->irq_raised
);
760 test
->last_irq
= -ENODATA
;
767 if (is_am654_pci_dev(pdev
) && bar
== BAR_0
)
769 ret
= pci_endpoint_test_bar(test
, bar
);
771 case PCITEST_INTX_IRQ
:
772 ret
= pci_endpoint_test_intx_irq(test
);
776 ret
= pci_endpoint_test_msi_irq(test
, arg
, cmd
== PCITEST_MSIX
);
779 ret
= pci_endpoint_test_write(test
, arg
);
782 ret
= pci_endpoint_test_read(test
, arg
);
785 ret
= pci_endpoint_test_copy(test
, arg
);
787 case PCITEST_SET_IRQTYPE
:
788 ret
= pci_endpoint_test_set_irq(test
, arg
);
790 case PCITEST_GET_IRQTYPE
:
793 case PCITEST_CLEAR_IRQ
:
794 ret
= pci_endpoint_test_clear_irq(test
);
799 mutex_unlock(&test
->mutex
);
803 static const struct file_operations pci_endpoint_test_fops
= {
804 .owner
= THIS_MODULE
,
805 .unlocked_ioctl
= pci_endpoint_test_ioctl
,
808 static int pci_endpoint_test_probe(struct pci_dev
*pdev
,
809 const struct pci_device_id
*ent
)
816 struct device
*dev
= &pdev
->dev
;
817 struct pci_endpoint_test
*test
;
818 struct pci_endpoint_test_data
*data
;
819 enum pci_barno test_reg_bar
= BAR_0
;
820 struct miscdevice
*misc_device
;
822 if (pci_is_bridge(pdev
))
825 test
= devm_kzalloc(dev
, sizeof(*test
), GFP_KERNEL
);
829 test
->test_reg_bar
= 0;
832 test
->irq_type
= IRQ_TYPE_UNDEFINED
;
835 irq_type
= IRQ_TYPE_INTX
;
837 data
= (struct pci_endpoint_test_data
*)ent
->driver_data
;
839 test_reg_bar
= data
->test_reg_bar
;
840 test
->test_reg_bar
= test_reg_bar
;
841 test
->alignment
= data
->alignment
;
842 irq_type
= data
->irq_type
;
845 init_completion(&test
->irq_raised
);
846 mutex_init(&test
->mutex
);
848 dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(48));
850 err
= pci_enable_device(pdev
);
852 dev_err(dev
, "Cannot enable PCI device\n");
856 err
= pci_request_regions(pdev
, DRV_MODULE_NAME
);
858 dev_err(dev
, "Cannot obtain PCI resources\n");
859 goto err_disable_pdev
;
862 pci_set_master(pdev
);
864 if (!pci_endpoint_test_alloc_irq_vectors(test
, irq_type
)) {
866 goto err_disable_irq
;
869 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
870 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
) {
871 base
= pci_ioremap_bar(pdev
, bar
);
873 dev_err(dev
, "Failed to read BAR%d\n", bar
);
874 WARN_ON(bar
== test_reg_bar
);
876 test
->bar
[bar
] = base
;
880 test
->base
= test
->bar
[test_reg_bar
];
883 dev_err(dev
, "Cannot perform PCI test without BAR%d\n",
888 pci_set_drvdata(pdev
, test
);
890 id
= ida_alloc(&pci_endpoint_test_ida
, GFP_KERNEL
);
893 dev_err(dev
, "Unable to get id\n");
897 snprintf(name
, sizeof(name
), DRV_MODULE_NAME
".%d", id
);
898 test
->name
= kstrdup(name
, GFP_KERNEL
);
904 if (!pci_endpoint_test_request_irq(test
)) {
906 goto err_kfree_test_name
;
909 misc_device
= &test
->miscdev
;
910 misc_device
->minor
= MISC_DYNAMIC_MINOR
;
911 misc_device
->name
= kstrdup(name
, GFP_KERNEL
);
912 if (!misc_device
->name
) {
914 goto err_release_irq
;
916 misc_device
->parent
= &pdev
->dev
;
917 misc_device
->fops
= &pci_endpoint_test_fops
;
919 err
= misc_register(misc_device
);
921 dev_err(dev
, "Failed to register device\n");
928 kfree(misc_device
->name
);
931 pci_endpoint_test_release_irq(test
);
937 ida_free(&pci_endpoint_test_ida
, id
);
940 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
942 pci_iounmap(pdev
, test
->bar
[bar
]);
946 pci_endpoint_test_free_irq_vectors(test
);
947 pci_release_regions(pdev
);
950 pci_disable_device(pdev
);
955 static void pci_endpoint_test_remove(struct pci_dev
*pdev
)
959 struct pci_endpoint_test
*test
= pci_get_drvdata(pdev
);
960 struct miscdevice
*misc_device
= &test
->miscdev
;
962 if (sscanf(misc_device
->name
, DRV_MODULE_NAME
".%d", &id
) != 1)
967 pci_endpoint_test_release_irq(test
);
968 pci_endpoint_test_free_irq_vectors(test
);
970 misc_deregister(&test
->miscdev
);
971 kfree(misc_device
->name
);
973 ida_free(&pci_endpoint_test_ida
, id
);
974 for (bar
= 0; bar
< PCI_STD_NUM_BARS
; bar
++) {
976 pci_iounmap(pdev
, test
->bar
[bar
]);
979 pci_release_regions(pdev
);
980 pci_disable_device(pdev
);
983 static const struct pci_endpoint_test_data default_data
= {
984 .test_reg_bar
= BAR_0
,
986 .irq_type
= IRQ_TYPE_MSI
,
989 static const struct pci_endpoint_test_data am654_data
= {
990 .test_reg_bar
= BAR_2
,
992 .irq_type
= IRQ_TYPE_MSI
,
995 static const struct pci_endpoint_test_data j721e_data
= {
997 .irq_type
= IRQ_TYPE_MSI
,
1000 static const struct pci_endpoint_test_data rk3588_data
= {
1001 .alignment
= SZ_64K
,
1002 .irq_type
= IRQ_TYPE_MSI
,
1006 * If the controller's Vendor/Device ID are programmable, you may be able to
1007 * use one of the existing entries for testing instead of adding a new one.
1009 static const struct pci_device_id pci_endpoint_test_tbl
[] = {
1010 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA74x
),
1011 .driver_data
= (kernel_ulong_t
)&default_data
,
1013 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA72x
),
1014 .driver_data
= (kernel_ulong_t
)&default_data
,
1016 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE
, 0x81c0),
1017 .driver_data
= (kernel_ulong_t
)&default_data
,
1019 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE
, PCI_DEVICE_ID_IMX8
),},
1020 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE
, PCI_DEVICE_ID_LS1088A
),
1021 .driver_data
= (kernel_ulong_t
)&default_data
,
1023 { PCI_DEVICE_DATA(SYNOPSYS
, EDDA
, NULL
) },
1024 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_AM654
),
1025 .driver_data
= (kernel_ulong_t
)&am654_data
1027 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774A1
),},
1028 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774B1
),},
1029 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774C0
),},
1030 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A774E1
),},
1031 { PCI_DEVICE(PCI_VENDOR_ID_RENESAS
, PCI_DEVICE_ID_RENESAS_R8A779F0
),
1032 .driver_data
= (kernel_ulong_t
)&default_data
,
1034 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_J721E
),
1035 .driver_data
= (kernel_ulong_t
)&j721e_data
,
1037 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_J7200
),
1038 .driver_data
= (kernel_ulong_t
)&j721e_data
,
1040 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_AM64
),
1041 .driver_data
= (kernel_ulong_t
)&j721e_data
,
1043 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_J721S2
),
1044 .driver_data
= (kernel_ulong_t
)&j721e_data
,
1046 { PCI_DEVICE(PCI_VENDOR_ID_ROCKCHIP
, PCI_DEVICE_ID_ROCKCHIP_RK3588
),
1047 .driver_data
= (kernel_ulong_t
)&rk3588_data
,
1051 MODULE_DEVICE_TABLE(pci
, pci_endpoint_test_tbl
);
1053 static struct pci_driver pci_endpoint_test_driver
= {
1054 .name
= DRV_MODULE_NAME
,
1055 .id_table
= pci_endpoint_test_tbl
,
1056 .probe
= pci_endpoint_test_probe
,
1057 .remove
= pci_endpoint_test_remove
,
1058 .sriov_configure
= pci_sriov_configure_simple
,
1060 module_pci_driver(pci_endpoint_test_driver
);
1062 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
1063 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
1064 MODULE_LICENSE("GPL v2");