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 COMMAND_RAISE_LEGACY_IRQ BIT(0)
22 #define COMMAND_RAISE_MSI_IRQ BIT(1)
23 #define MSI_NUMBER_SHIFT 2
24 #define MSI_NUMBER_MASK (0x3f << MSI_NUMBER_SHIFT)
25 #define COMMAND_READ BIT(8)
26 #define COMMAND_WRITE BIT(9)
27 #define COMMAND_COPY BIT(10)
29 #define STATUS_READ_SUCCESS BIT(0)
30 #define STATUS_READ_FAIL BIT(1)
31 #define STATUS_WRITE_SUCCESS BIT(2)
32 #define STATUS_WRITE_FAIL BIT(3)
33 #define STATUS_COPY_SUCCESS BIT(4)
34 #define STATUS_COPY_FAIL BIT(5)
35 #define STATUS_IRQ_RAISED BIT(6)
36 #define STATUS_SRC_ADDR_INVALID BIT(7)
37 #define STATUS_DST_ADDR_INVALID BIT(8)
39 #define TIMER_RESOLUTION 1
41 static struct workqueue_struct
*kpcitest_workqueue
;
46 enum pci_barno test_reg_bar
;
48 struct delayed_work cmd_handler
;
51 struct pci_epf_test_reg
{
61 static struct pci_epf_header test_header
= {
62 .vendorid
= PCI_ANY_ID
,
63 .deviceid
= PCI_ANY_ID
,
64 .baseclass_code
= PCI_CLASS_OTHERS
,
65 .interrupt_pin
= PCI_INTERRUPT_INTA
,
68 struct pci_epf_test_data
{
69 enum pci_barno test_reg_bar
;
73 static int bar_size
[] = { 512, 512, 1024, 16384, 131072, 1048576 };
75 static int pci_epf_test_copy(struct pci_epf_test
*epf_test
)
78 void __iomem
*src_addr
;
79 void __iomem
*dst_addr
;
80 phys_addr_t src_phys_addr
;
81 phys_addr_t dst_phys_addr
;
82 struct pci_epf
*epf
= epf_test
->epf
;
83 struct device
*dev
= &epf
->dev
;
84 struct pci_epc
*epc
= epf
->epc
;
85 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
86 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
88 src_addr
= pci_epc_mem_alloc_addr(epc
, &src_phys_addr
, reg
->size
);
90 dev_err(dev
, "failed to allocate source address\n");
91 reg
->status
= STATUS_SRC_ADDR_INVALID
;
96 ret
= pci_epc_map_addr(epc
, epf
->func_no
, src_phys_addr
, reg
->src_addr
,
99 dev_err(dev
, "failed to map source address\n");
100 reg
->status
= STATUS_SRC_ADDR_INVALID
;
104 dst_addr
= pci_epc_mem_alloc_addr(epc
, &dst_phys_addr
, reg
->size
);
106 dev_err(dev
, "failed to allocate destination address\n");
107 reg
->status
= STATUS_DST_ADDR_INVALID
;
109 goto err_src_map_addr
;
112 ret
= pci_epc_map_addr(epc
, epf
->func_no
, dst_phys_addr
, reg
->dst_addr
,
115 dev_err(dev
, "failed to map destination address\n");
116 reg
->status
= STATUS_DST_ADDR_INVALID
;
120 memcpy(dst_addr
, src_addr
, reg
->size
);
122 pci_epc_unmap_addr(epc
, epf
->func_no
, dst_phys_addr
);
125 pci_epc_mem_free_addr(epc
, dst_phys_addr
, dst_addr
, reg
->size
);
128 pci_epc_unmap_addr(epc
, epf
->func_no
, src_phys_addr
);
131 pci_epc_mem_free_addr(epc
, src_phys_addr
, src_addr
, reg
->size
);
137 static int pci_epf_test_read(struct pci_epf_test
*epf_test
)
140 void __iomem
*src_addr
;
143 phys_addr_t phys_addr
;
144 struct pci_epf
*epf
= epf_test
->epf
;
145 struct device
*dev
= &epf
->dev
;
146 struct pci_epc
*epc
= epf
->epc
;
147 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
148 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
150 src_addr
= pci_epc_mem_alloc_addr(epc
, &phys_addr
, reg
->size
);
152 dev_err(dev
, "failed to allocate address\n");
153 reg
->status
= STATUS_SRC_ADDR_INVALID
;
158 ret
= pci_epc_map_addr(epc
, epf
->func_no
, phys_addr
, reg
->src_addr
,
161 dev_err(dev
, "failed to map address\n");
162 reg
->status
= STATUS_SRC_ADDR_INVALID
;
166 buf
= kzalloc(reg
->size
, GFP_KERNEL
);
172 memcpy(buf
, src_addr
, reg
->size
);
174 crc32
= crc32_le(~0, buf
, reg
->size
);
175 if (crc32
!= reg
->checksum
)
181 pci_epc_unmap_addr(epc
, epf
->func_no
, phys_addr
);
184 pci_epc_mem_free_addr(epc
, phys_addr
, src_addr
, reg
->size
);
190 static int pci_epf_test_write(struct pci_epf_test
*epf_test
)
193 void __iomem
*dst_addr
;
195 phys_addr_t phys_addr
;
196 struct pci_epf
*epf
= epf_test
->epf
;
197 struct device
*dev
= &epf
->dev
;
198 struct pci_epc
*epc
= epf
->epc
;
199 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
200 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
202 dst_addr
= pci_epc_mem_alloc_addr(epc
, &phys_addr
, reg
->size
);
204 dev_err(dev
, "failed to allocate address\n");
205 reg
->status
= STATUS_DST_ADDR_INVALID
;
210 ret
= pci_epc_map_addr(epc
, epf
->func_no
, phys_addr
, reg
->dst_addr
,
213 dev_err(dev
, "failed to map address\n");
214 reg
->status
= STATUS_DST_ADDR_INVALID
;
218 buf
= kzalloc(reg
->size
, GFP_KERNEL
);
224 get_random_bytes(buf
, reg
->size
);
225 reg
->checksum
= crc32_le(~0, buf
, reg
->size
);
227 memcpy(dst_addr
, buf
, reg
->size
);
230 * wait 1ms inorder for the write to complete. Without this delay L3
231 * error in observed in the host system.
238 pci_epc_unmap_addr(epc
, epf
->func_no
, phys_addr
);
241 pci_epc_mem_free_addr(epc
, phys_addr
, dst_addr
, reg
->size
);
247 static void pci_epf_test_raise_irq(struct pci_epf_test
*epf_test
, u8 irq
)
250 struct pci_epf
*epf
= epf_test
->epf
;
251 struct pci_epc
*epc
= epf
->epc
;
252 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
253 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
255 reg
->status
|= STATUS_IRQ_RAISED
;
256 msi_count
= pci_epc_get_msi(epc
, epf
->func_no
);
257 if (irq
> msi_count
|| msi_count
<= 0)
258 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_LEGACY
, 0);
260 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSI
, irq
);
263 static void pci_epf_test_cmd_handler(struct work_struct
*work
)
269 struct pci_epf_test
*epf_test
= container_of(work
, struct pci_epf_test
,
271 struct pci_epf
*epf
= epf_test
->epf
;
272 struct pci_epc
*epc
= epf
->epc
;
273 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
274 struct pci_epf_test_reg
*reg
= epf_test
->reg
[test_reg_bar
];
276 command
= reg
->command
;
283 irq
= (command
& MSI_NUMBER_MASK
) >> MSI_NUMBER_SHIFT
;
285 if (command
& COMMAND_RAISE_LEGACY_IRQ
) {
286 reg
->status
= STATUS_IRQ_RAISED
;
287 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_LEGACY
, 0);
291 if (command
& COMMAND_WRITE
) {
292 ret
= pci_epf_test_write(epf_test
);
294 reg
->status
|= STATUS_WRITE_FAIL
;
296 reg
->status
|= STATUS_WRITE_SUCCESS
;
297 pci_epf_test_raise_irq(epf_test
, irq
);
301 if (command
& COMMAND_READ
) {
302 ret
= pci_epf_test_read(epf_test
);
304 reg
->status
|= STATUS_READ_SUCCESS
;
306 reg
->status
|= STATUS_READ_FAIL
;
307 pci_epf_test_raise_irq(epf_test
, irq
);
311 if (command
& COMMAND_COPY
) {
312 ret
= pci_epf_test_copy(epf_test
);
314 reg
->status
|= STATUS_COPY_SUCCESS
;
316 reg
->status
|= STATUS_COPY_FAIL
;
317 pci_epf_test_raise_irq(epf_test
, irq
);
321 if (command
& COMMAND_RAISE_MSI_IRQ
) {
322 msi_count
= pci_epc_get_msi(epc
, epf
->func_no
);
323 if (irq
> msi_count
|| msi_count
<= 0)
325 reg
->status
= STATUS_IRQ_RAISED
;
326 pci_epc_raise_irq(epc
, epf
->func_no
, PCI_EPC_IRQ_MSI
, irq
);
331 queue_delayed_work(kpcitest_workqueue
, &epf_test
->cmd_handler
,
332 msecs_to_jiffies(1));
335 static void pci_epf_test_linkup(struct pci_epf
*epf
)
337 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
339 queue_delayed_work(kpcitest_workqueue
, &epf_test
->cmd_handler
,
340 msecs_to_jiffies(1));
343 static void pci_epf_test_unbind(struct pci_epf
*epf
)
345 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
346 struct pci_epc
*epc
= epf
->epc
;
349 cancel_delayed_work(&epf_test
->cmd_handler
);
351 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
352 if (epf_test
->reg
[bar
]) {
353 pci_epf_free_space(epf
, epf_test
->reg
[bar
], bar
);
354 pci_epc_clear_bar(epc
, epf
->func_no
, bar
);
359 static int pci_epf_test_set_bar(struct pci_epf
*epf
)
364 struct pci_epf_bar
*epf_bar
;
365 struct pci_epc
*epc
= epf
->epc
;
366 struct device
*dev
= &epf
->dev
;
367 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
368 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
370 flags
= PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_32
;
371 if (sizeof(dma_addr_t
) == 0x8)
372 flags
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
374 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
375 epf_bar
= &epf
->bar
[bar
];
376 ret
= pci_epc_set_bar(epc
, epf
->func_no
, bar
,
378 epf_bar
->size
, flags
);
380 pci_epf_free_space(epf
, epf_test
->reg
[bar
], bar
);
381 dev_err(dev
, "failed to set BAR%d\n", bar
);
382 if (bar
== test_reg_bar
)
390 static int pci_epf_test_alloc_space(struct pci_epf
*epf
)
392 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
393 struct device
*dev
= &epf
->dev
;
396 enum pci_barno test_reg_bar
= epf_test
->test_reg_bar
;
398 base
= pci_epf_alloc_space(epf
, sizeof(struct pci_epf_test_reg
),
401 dev_err(dev
, "failed to allocated register space\n");
404 epf_test
->reg
[test_reg_bar
] = base
;
406 for (bar
= BAR_0
; bar
<= BAR_5
; bar
++) {
407 if (bar
== test_reg_bar
)
409 base
= pci_epf_alloc_space(epf
, bar_size
[bar
], bar
);
411 dev_err(dev
, "failed to allocate space for BAR%d\n",
413 epf_test
->reg
[bar
] = base
;
419 static int pci_epf_test_bind(struct pci_epf
*epf
)
422 struct pci_epf_test
*epf_test
= epf_get_drvdata(epf
);
423 struct pci_epf_header
*header
= epf
->header
;
424 struct pci_epc
*epc
= epf
->epc
;
425 struct device
*dev
= &epf
->dev
;
427 if (WARN_ON_ONCE(!epc
))
430 ret
= pci_epc_write_header(epc
, epf
->func_no
, header
);
432 dev_err(dev
, "configuration header write failed\n");
436 ret
= pci_epf_test_alloc_space(epf
);
440 ret
= pci_epf_test_set_bar(epf
);
444 ret
= pci_epc_set_msi(epc
, epf
->func_no
, epf
->msi_interrupts
);
448 if (!epf_test
->linkup_notifier
)
449 queue_work(kpcitest_workqueue
, &epf_test
->cmd_handler
.work
);
454 static const struct pci_epf_device_id pci_epf_test_ids
[] = {
456 .name
= "pci_epf_test",
461 static int pci_epf_test_probe(struct pci_epf
*epf
)
463 struct pci_epf_test
*epf_test
;
464 struct device
*dev
= &epf
->dev
;
465 const struct pci_epf_device_id
*match
;
466 struct pci_epf_test_data
*data
;
467 enum pci_barno test_reg_bar
= BAR_0
;
468 bool linkup_notifier
= true;
470 match
= pci_epf_match_device(pci_epf_test_ids
, epf
);
471 data
= (struct pci_epf_test_data
*)match
->driver_data
;
473 test_reg_bar
= data
->test_reg_bar
;
474 linkup_notifier
= data
->linkup_notifier
;
477 epf_test
= devm_kzalloc(dev
, sizeof(*epf_test
), GFP_KERNEL
);
481 epf
->header
= &test_header
;
483 epf_test
->test_reg_bar
= test_reg_bar
;
484 epf_test
->linkup_notifier
= linkup_notifier
;
486 INIT_DELAYED_WORK(&epf_test
->cmd_handler
, pci_epf_test_cmd_handler
);
488 epf_set_drvdata(epf
, epf_test
);
492 static struct pci_epf_ops ops
= {
493 .unbind
= pci_epf_test_unbind
,
494 .bind
= pci_epf_test_bind
,
495 .linkup
= pci_epf_test_linkup
,
498 static struct pci_epf_driver test_driver
= {
499 .driver
.name
= "pci_epf_test",
500 .probe
= pci_epf_test_probe
,
501 .id_table
= pci_epf_test_ids
,
503 .owner
= THIS_MODULE
,
506 static int __init
pci_epf_test_init(void)
510 kpcitest_workqueue
= alloc_workqueue("kpcitest",
511 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
512 ret
= pci_epf_register_driver(&test_driver
);
514 pr_err("failed to register pci epf test driver --> %d\n", ret
);
520 module_init(pci_epf_test_init
);
522 static void __exit
pci_epf_test_exit(void)
524 pci_epf_unregister_driver(&test_driver
);
526 module_exit(pci_epf_test_exit
);
528 MODULE_DESCRIPTION("PCI EPF TEST DRIVER");
529 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
530 MODULE_LICENSE("GPL v2");