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 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 base
= pci_ioremap_bar(pdev
, bar
);
539 dev_err(dev
, "failed to read BAR%d\n", bar
);
540 WARN_ON(bar
== test_reg_bar
);
542 test
->bar
[bar
] = base
;
545 test
->base
= test
->bar
[test_reg_bar
];
548 dev_err(dev
, "Cannot perform PCI test without BAR%d\n",
553 pci_set_drvdata(pdev
, test
);
555 id
= ida_simple_get(&pci_endpoint_test_ida
, 0, 0, GFP_KERNEL
);
558 dev_err(dev
, "unable to get id\n");
562 snprintf(name
, sizeof(name
), DRV_MODULE_NAME
".%d", id
);
563 misc_device
= &test
->miscdev
;
564 misc_device
->minor
= MISC_DYNAMIC_MINOR
;
565 misc_device
->name
= kstrdup(name
, GFP_KERNEL
);
566 if (!misc_device
->name
) {
570 misc_device
->fops
= &pci_endpoint_test_fops
,
572 err
= misc_register(misc_device
);
574 dev_err(dev
, "failed to register device\n");
581 kfree(misc_device
->name
);
584 ida_simple_remove(&pci_endpoint_test_ida
, id
);
587 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
589 pci_iounmap(pdev
, test
->bar
[bar
]);
592 for (i
= 0; i
< irq
; i
++)
593 devm_free_irq(dev
, pdev
->irq
+ i
, test
);
596 pci_disable_msi(pdev
);
597 pci_release_regions(pdev
);
600 pci_disable_device(pdev
);
605 static void pci_endpoint_test_remove(struct pci_dev
*pdev
)
610 struct pci_endpoint_test
*test
= pci_get_drvdata(pdev
);
611 struct miscdevice
*misc_device
= &test
->miscdev
;
613 if (sscanf(misc_device
->name
, DRV_MODULE_NAME
".%d", &id
) != 1)
618 misc_deregister(&test
->miscdev
);
619 kfree(misc_device
->name
);
620 ida_simple_remove(&pci_endpoint_test_ida
, id
);
621 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
623 pci_iounmap(pdev
, test
->bar
[bar
]);
625 for (i
= 0; i
< test
->num_irqs
; i
++)
626 devm_free_irq(&pdev
->dev
, pdev
->irq
+ i
, test
);
627 pci_disable_msi(pdev
);
628 pci_release_regions(pdev
);
629 pci_disable_device(pdev
);
632 static const struct pci_device_id pci_endpoint_test_tbl
[] = {
633 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA74x
) },
634 { PCI_DEVICE(PCI_VENDOR_ID_TI
, PCI_DEVICE_ID_TI_DRA72x
) },
637 MODULE_DEVICE_TABLE(pci
, pci_endpoint_test_tbl
);
639 static struct pci_driver pci_endpoint_test_driver
= {
640 .name
= DRV_MODULE_NAME
,
641 .id_table
= pci_endpoint_test_tbl
,
642 .probe
= pci_endpoint_test_probe
,
643 .remove
= pci_endpoint_test_remove
,
645 module_pci_driver(pci_endpoint_test_driver
);
647 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
648 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
649 MODULE_LICENSE("GPL v2");