1 // SPDX-License-Identifier: GPL-2.0
3 * 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>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/pci_ids.h>
15 #include <linux/random.h>
17 #include <linux/pci-epc.h>
18 #include <linux/pci-epf.h>
19 #include <linux/pci_regs.h>
21 #define IRQ_TYPE_LEGACY 0
22 #define IRQ_TYPE_MSI 1
23 #define IRQ_TYPE_MSIX 2
25 #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
26 #define COMMAND_RAISE_MSI_IRQ BIT(1)
27 #define COMMAND_RAISE_MSIX_IRQ BIT(2)
28 #define COMMAND_READ BIT(3)
29 #define COMMAND_WRITE BIT(4)
30 #define COMMAND_COPY BIT(5)
32 #define STATUS_READ_SUCCESS BIT(0)
33 #define STATUS_READ_FAIL BIT(1)
34 #define STATUS_WRITE_SUCCESS BIT(2)
35 #define STATUS_WRITE_FAIL BIT(3)
36 #define STATUS_COPY_SUCCESS BIT(4)
37 #define STATUS_COPY_FAIL BIT(5)
38 #define STATUS_IRQ_RAISED BIT(6)
39 #define STATUS_SRC_ADDR_INVALID BIT(7)
40 #define STATUS_DST_ADDR_INVALID BIT(8)
42 #define TIMER_RESOLUTION 1
44 static struct workqueue_struct
*kpcitest_workqueue
;
49 enum pci_barno test_reg_bar
;
52 struct delayed_work cmd_handler
;
55 struct pci_epf_test_reg
{
67 static struct pci_epf_header test_header
= {
68 .vendorid
= PCI_ANY_ID
,
69 .deviceid
= PCI_ANY_ID
,
70 .baseclass_code
= PCI_CLASS_OTHERS
,
71 .interrupt_pin
= PCI_INTERRUPT_INTA
,
74 struct pci_epf_test_data
{
75 enum pci_barno test_reg_bar
;
79 static size_t bar_size
[] = { 512, 512, 1024, 16384, 131072, 1048576 };
81 static int pci_epf_test_copy(struct pci_epf_test
*epf_test
)
84 void __iomem
*src_addr
;
85 void __iomem
*dst_addr
;
86 phys_addr_t src_phys_addr
;
87 phys_addr_t dst_phys_addr
;
88 struct pci_epf
*epf
= epf_test
->epf
;
89 struct device
*dev
= &epf
->dev
;
90 struct pci_epc
*epc
= epf
->epc
;
91 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
92 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
94 src_addr
= pci_epc_mem_alloc_addr(epc
, &src_phys_addr
, reg
->size
);
96 dev_err(dev
, "Failed to allocate source address\n");
97 reg
->status
= STATUS_SRC_ADDR_INVALID
;
102 ret
= pci_epc_map_addr(epc
, epf
->func_no
, src_phys_addr
, reg
->src_addr
,
105 dev_err(dev
, "Failed to map source address\n");
106 reg
->status
= STATUS_SRC_ADDR_INVALID
;
110 dst_addr
= pci_epc_mem_alloc_addr(epc
, &dst_phys_addr
, reg
->size
);
112 dev_err(dev
, "Failed to allocate destination address\n");
113 reg
->status
= STATUS_DST_ADDR_INVALID
;
115 goto err_src_map_addr
;
118 ret
= pci_epc_map_addr(epc
, epf
->func_no
, dst_phys_addr
, reg
->dst_addr
,
121 dev_err(dev
, "Failed to map destination address\n");
122 reg
->status
= STATUS_DST_ADDR_INVALID
;
126 memcpy(dst_addr
, src_addr
, reg
->size
);
128 pci_epc_unmap_addr(epc
, epf
->func_no
, dst_phys_addr
);
131 pci_epc_mem_free_addr(epc
, dst_phys_addr
, dst_addr
, reg
->size
);
134 pci_epc_unmap_addr(epc
, epf
->func_no
, src_phys_addr
);
137 pci_epc_mem_free_addr(epc
, src_phys_addr
, src_addr
, reg
->size
);
143 static int pci_epf_test_read(struct pci_epf_test
*epf_test
)
146 void __iomem
*src_addr
;
149 phys_addr_t phys_addr
;
150 struct pci_epf
*epf
= epf_test
->epf
;
151 struct device
*dev
= &epf
->dev
;
152 struct pci_epc
*epc
= epf
->epc
;
153 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
154 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
156 src_addr
= pci_epc_mem_alloc_addr(epc
, &phys_addr
, reg
->size
);
158 dev_err(dev
, "Failed to allocate address\n");
159 reg
->status
= STATUS_SRC_ADDR_INVALID
;
164 ret
= pci_epc_map_addr(epc
, epf
->func_no
, phys_addr
, reg
->src_addr
,
167 dev_err(dev
, "Failed to map address\n");
168 reg
->status
= STATUS_SRC_ADDR_INVALID
;
172 buf
= kzalloc(reg
->size
, GFP_KERNEL
);
178 memcpy_fromio(buf
, src_addr
, reg
->size
);
180 crc32
= crc32_le(~0, buf
, reg
->size
);
181 if (crc32
!= reg
->checksum
)
187 pci_epc_unmap_addr(epc
, epf
->func_no
, phys_addr
);
190 pci_epc_mem_free_addr(epc
, phys_addr
, src_addr
, reg
->size
);
196 static int pci_epf_test_write(struct pci_epf_test
*epf_test
)
199 void __iomem
*dst_addr
;
201 phys_addr_t phys_addr
;
202 struct pci_epf
*epf
= epf_test
->epf
;
203 struct device
*dev
= &epf
->dev
;
204 struct pci_epc
*epc
= epf
->epc
;
205 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
206 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
208 dst_addr
= pci_epc_mem_alloc_addr(epc
, &phys_addr
, reg
->size
);
210 dev_err(dev
, "Failed to allocate address\n");
211 reg
->status
= STATUS_DST_ADDR_INVALID
;
216 ret
= pci_epc_map_addr(epc
, epf
->func_no
, phys_addr
, reg
->dst_addr
,
219 dev_err(dev
, "Failed to map address\n");
220 reg
->status
= STATUS_DST_ADDR_INVALID
;
224 buf
= kzalloc(reg
->size
, GFP_KERNEL
);
230 get_random_bytes(buf
, reg
->size
);
231 reg
->checksum
= crc32_le(~0, buf
, reg
->size
);
233 memcpy_toio(dst_addr
, buf
, reg
->size
);
236 * wait 1ms inorder for the write to complete. Without this delay L3
237 * error in observed in the host system.
239 usleep_range(1000, 2000);
244 pci_epc_unmap_addr(epc
, epf
->func_no
, phys_addr
);
247 pci_epc_mem_free_addr(epc
, phys_addr
, dst_addr
, reg
->size
);
253 static void pci_epf_test_raise_irq(struct pci_epf_test
*epf_test
, u8 irq_type
,
256 struct pci_epf
*epf
= epf_test
->epf
;
257 struct device
*dev
= &epf
->dev
;
258 struct pci_epc
*epc
= epf
->epc
;
259 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
260 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
262 reg
->status
|= STATUS_IRQ_RAISED
;
265 case IRQ_TYPE_LEGACY
:
266 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_LEGACY
, 0);
269 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSI
, irq
);
272 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSIX
, irq
);
275 dev_err(dev
, "Failed to raise IRQ, unknown type\n");
280 static void pci_epf_test_cmd_handler(struct work_struct
*work
)
285 struct pci_epf_test
*epf_test
= container_of(work
, struct pci_epf_test
,
287 struct pci_epf
*epf
= epf_test
->epf
;
288 struct device
*dev
= &epf
->dev
;
289 struct pci_epc
*epc
= epf
->epc
;
290 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
291 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
293 command
= reg
->command
;
300 if (reg
->irq_type
> IRQ_TYPE_MSIX
) {
301 dev_err(dev
, "Failed to detect IRQ type\n");
305 if (command
& COMMAND_RAISE_LEGACY_IRQ
) {
306 reg
->status
= STATUS_IRQ_RAISED
;
307 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_LEGACY
, 0);
311 if (command
& COMMAND_WRITE
) {
312 ret
= pci_epf_test_write(epf_test
);
314 reg
->status
|= STATUS_WRITE_FAIL
;
316 reg
->status
|= STATUS_WRITE_SUCCESS
;
317 pci_epf_test_raise_irq(epf_test
, reg
->irq_type
,
322 if (command
& COMMAND_READ
) {
323 ret
= pci_epf_test_read(epf_test
);
325 reg
->status
|= STATUS_READ_SUCCESS
;
327 reg
->status
|= STATUS_READ_FAIL
;
328 pci_epf_test_raise_irq(epf_test
, reg
->irq_type
,
333 if (command
& COMMAND_COPY
) {
334 ret
= pci_epf_test_copy(epf_test
);
336 reg
->status
|= STATUS_COPY_SUCCESS
;
338 reg
->status
|= STATUS_COPY_FAIL
;
339 pci_epf_test_raise_irq(epf_test
, reg
->irq_type
,
344 if (command
& COMMAND_RAISE_MSI_IRQ
) {
345 count
= pci_epc_get_msi(epc
, epf
->func_no
);
346 if (reg
->irq_number
> count
|| count
<= 0)
348 reg
->status
= STATUS_IRQ_RAISED
;
349 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSI
,
354 if (command
& COMMAND_RAISE_MSIX_IRQ
) {
355 count
= pci_epc_get_msix(epc
, epf
->func_no
);
356 if (reg
->irq_number
> count
|| count
<= 0)
358 reg
->status
= STATUS_IRQ_RAISED
;
359 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSIX
,
365 queue_delayed_work(kpcitest_workqueue
, &epf_test
->cmd_handler
,
366 msecs_to_jiffies(1));
369 static void pci_epf_test_linkup(struct pci_epf
*epf
)
371 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
373 queue_delayed_work(kpcitest_workqueue
, &epf_test
->cmd_handler
,
374 msecs_to_jiffies(1));
377 static void pci_epf_test_unbind(struct pci_epf
*epf
)
379 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
380 struct pci_epc
*epc
= epf
->epc
;
381 struct pci_epf_bar
*epf_bar
;
384 cancel_delayed_work(&epf_test
->cmd_handler
);
386 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
387 epf_bar
= &epf
->bar
[bar
];
389 if (epf_test
->reg
[bar
]) {
390 pci_epf_free_space(epf
, epf_test
->reg
[bar
], bar
);
391 pci_epc_clear_bar(epc
, epf
->func_no
, epf_bar
);
396 static int pci_epf_test_set_bar(struct pci_epf
*epf
)
400 struct pci_epf_bar
*epf_bar
;
401 struct pci_epc
*epc
= epf
->epc
;
402 struct device
*dev
= &epf
->dev
;
403 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
404 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
406 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
407 epf_bar
= &epf
->bar
[bar
];
409 epf_bar
->flags
|= upper_32_bits(epf_bar
->size
) ?
410 PCI_BASE_ADDRESS_MEM_TYPE_64
:
411 PCI_BASE_ADDRESS_MEM_TYPE_32
;
413 ret
= pci_epc_set_bar(epc
, epf
->func_no
, epf_bar
);
415 pci_epf_free_space(epf
, epf_test
->reg
[bar
], bar
);
416 dev_err(dev
, "Failed to set BAR%d\n", bar
);
417 if (bar
== test_reg_bar
)
421 * pci_epc_set_bar() sets PCI_BASE_ADDRESS_MEM_TYPE_64
422 * if the specific implementation required a 64-bit BAR,
423 * even if we only requested a 32-bit BAR.
425 if (epf_bar
->flags
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
432 static int pci_epf_test_alloc_space(struct pci_epf
*epf
)
434 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
435 struct device
*dev
= &epf
->dev
;
438 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
440 base
= pci_epf_alloc_space(epf
, sizeof(struct pci_epf_test_reg
),
443 dev_err(dev
, "Failed to allocated register space\n");
446 epf_test
->reg
[test_reg_bar
] = base
;
448 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
449 if (bar
== test_reg_bar
)
451 base
= pci_epf_alloc_space(epf
, bar_size
[bar
], bar
);
453 dev_err(dev
, "Failed to allocate space for BAR%d\n",
455 epf_test
->reg
[bar
] = base
;
461 static int pci_epf_test_bind(struct pci_epf
*epf
)
464 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
465 struct pci_epf_header
*header
= epf
->header
;
466 struct pci_epc
*epc
= epf
->epc
;
467 struct device
*dev
= &epf
->dev
;
469 if (WARN_ON_ONCE(!epc
))
472 if (epc
->features
& EPC_FEATURE_NO_LINKUP_NOTIFIER
)
473 epf_test
->linkup_notifier
= false;
475 epf_test
->linkup_notifier
= true;
477 epf_test
->msix_available
= epc
->features
& EPC_FEATURE_MSIX_AVAILABLE
;
479 epf_test
->test_reg_bar
= EPC_FEATURE_GET_BAR(epc
->features
);
481 ret
= pci_epc_write_header(epc
, epf
->func_no
, header
);
483 dev_err(dev
, "Configuration header write failed\n");
487 ret
= pci_epf_test_alloc_space(epf
);
491 ret
= pci_epf_test_set_bar(epf
);
495 ret
= pci_epc_set_msi(epc
, epf
->func_no
, epf
->msi_interrupts
);
497 dev_err(dev
, "MSI configuration failed\n");
501 if (epf_test
->msix_available
) {
502 ret
= pci_epc_set_msix(epc
, epf
->func_no
, epf
->msix_interrupts
);
504 dev_err(dev
, "MSI-X configuration failed\n");
509 if (!epf_test
->linkup_notifier
)
510 queue_work(kpcitest_workqueue
, &epf_test
->cmd_handler
.work
);
515 static const struct pci_epf_device_id pci_epf_test_ids
[] = {
517 .name
= "pci_epf_test",
522 static int pci_epf_test_probe(struct pci_epf
*epf
)
524 struct pci_epf_test
*epf_test
;
525 struct device
*dev
= &epf
->dev
;
526 const struct pci_epf_device_id
*match
;
527 struct pci_epf_test_data
*data
;
528 enum pci_barno test_reg_bar
= BAR_0
;
529 bool linkup_notifier
= true;
531 match
= pci_epf_match_device(pci_epf_test_ids
, epf
);
532 data
= (struct pci_epf_test_data
*)match
->driver_data
;
534 test_reg_bar
= data
->test_reg_bar
;
535 linkup_notifier
= data
->linkup_notifier
;
538 epf_test
= devm_kzalloc(dev
, sizeof(*epf_test
), GFP_KERNEL
);
542 epf
->header
= &test_header
;
544 epf_test
->test_reg_bar
= test_reg_bar
;
545 epf_test
->linkup_notifier
= linkup_notifier
;
547 INIT_DELAYED_WORK(&epf_test
->cmd_handler
, pci_epf_test_cmd_handler
);
549 epf_set_drvdata(epf
, epf_test
);
553 static struct pci_epf_ops ops
= {
554 .unbind
= pci_epf_test_unbind
,
555 .bind
= pci_epf_test_bind
,
556 .linkup
= pci_epf_test_linkup
,
559 static struct pci_epf_driver test_driver
= {
560 .driver
.name
= "pci_epf_test",
561 .probe
= pci_epf_test_probe
,
562 .id_table
= pci_epf_test_ids
,
564 .owner
= THIS_MODULE
,
567 static int __init
pci_epf_test_init(void)
571 kpcitest_workqueue
= alloc_workqueue("kpcitest",
572 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
573 ret
= pci_epf_register_driver(&test_driver
);
575 pr_err("Failed to register pci epf test driver --> %d\n", ret
);
581 module_init(pci_epf_test_init
);
583 static void __exit
pci_epf_test_exit(void)
585 pci_epf_unregister_driver(&test_driver
);
587 module_exit(pci_epf_test_exit
);
589 MODULE_DESCRIPTION("PCI EPF TEST DRIVER");
590 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
591 MODULE_LICENSE("GPL v2");