Linux 4.19.133
[linux/fpc-iii.git] / drivers / pci / endpoint / functions / pci-epf-test.c
blob4bbd26e8a9e2fcdfb334e271644a0dccbc023654
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * Test driver to test endpoint functionality
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/io.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;
46 struct pci_epf_test {
47 void *reg[6];
48 struct pci_epf *epf;
49 enum pci_barno test_reg_bar;
50 bool linkup_notifier;
51 bool msix_available;
52 struct delayed_work cmd_handler;
55 struct pci_epf_test_reg {
56 u32 magic;
57 u32 command;
58 u32 status;
59 u64 src_addr;
60 u64 dst_addr;
61 u32 size;
62 u32 checksum;
63 u32 irq_type;
64 u32 irq_number;
65 } __packed;
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;
76 bool linkup_notifier;
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)
83 int ret;
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);
95 if (!src_addr) {
96 dev_err(dev, "Failed to allocate source address\n");
97 reg->status = STATUS_SRC_ADDR_INVALID;
98 ret = -ENOMEM;
99 goto err;
102 ret = pci_epc_map_addr(epc, epf->func_no, src_phys_addr, reg->src_addr,
103 reg->size);
104 if (ret) {
105 dev_err(dev, "Failed to map source address\n");
106 reg->status = STATUS_SRC_ADDR_INVALID;
107 goto err_src_addr;
110 dst_addr = pci_epc_mem_alloc_addr(epc, &dst_phys_addr, reg->size);
111 if (!dst_addr) {
112 dev_err(dev, "Failed to allocate destination address\n");
113 reg->status = STATUS_DST_ADDR_INVALID;
114 ret = -ENOMEM;
115 goto err_src_map_addr;
118 ret = pci_epc_map_addr(epc, epf->func_no, dst_phys_addr, reg->dst_addr,
119 reg->size);
120 if (ret) {
121 dev_err(dev, "Failed to map destination address\n");
122 reg->status = STATUS_DST_ADDR_INVALID;
123 goto err_dst_addr;
126 memcpy(dst_addr, src_addr, reg->size);
128 pci_epc_unmap_addr(epc, epf->func_no, dst_phys_addr);
130 err_dst_addr:
131 pci_epc_mem_free_addr(epc, dst_phys_addr, dst_addr, reg->size);
133 err_src_map_addr:
134 pci_epc_unmap_addr(epc, epf->func_no, src_phys_addr);
136 err_src_addr:
137 pci_epc_mem_free_addr(epc, src_phys_addr, src_addr, reg->size);
139 err:
140 return ret;
143 static int pci_epf_test_read(struct pci_epf_test *epf_test)
145 int ret;
146 void __iomem *src_addr;
147 void *buf;
148 u32 crc32;
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);
157 if (!src_addr) {
158 dev_err(dev, "Failed to allocate address\n");
159 reg->status = STATUS_SRC_ADDR_INVALID;
160 ret = -ENOMEM;
161 goto err;
164 ret = pci_epc_map_addr(epc, epf->func_no, phys_addr, reg->src_addr,
165 reg->size);
166 if (ret) {
167 dev_err(dev, "Failed to map address\n");
168 reg->status = STATUS_SRC_ADDR_INVALID;
169 goto err_addr;
172 buf = kzalloc(reg->size, GFP_KERNEL);
173 if (!buf) {
174 ret = -ENOMEM;
175 goto err_map_addr;
178 memcpy_fromio(buf, src_addr, reg->size);
180 crc32 = crc32_le(~0, buf, reg->size);
181 if (crc32 != reg->checksum)
182 ret = -EIO;
184 kfree(buf);
186 err_map_addr:
187 pci_epc_unmap_addr(epc, epf->func_no, phys_addr);
189 err_addr:
190 pci_epc_mem_free_addr(epc, phys_addr, src_addr, reg->size);
192 err:
193 return ret;
196 static int pci_epf_test_write(struct pci_epf_test *epf_test)
198 int ret;
199 void __iomem *dst_addr;
200 void *buf;
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);
209 if (!dst_addr) {
210 dev_err(dev, "Failed to allocate address\n");
211 reg->status = STATUS_DST_ADDR_INVALID;
212 ret = -ENOMEM;
213 goto err;
216 ret = pci_epc_map_addr(epc, epf->func_no, phys_addr, reg->dst_addr,
217 reg->size);
218 if (ret) {
219 dev_err(dev, "Failed to map address\n");
220 reg->status = STATUS_DST_ADDR_INVALID;
221 goto err_addr;
224 buf = kzalloc(reg->size, GFP_KERNEL);
225 if (!buf) {
226 ret = -ENOMEM;
227 goto err_map_addr;
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);
241 kfree(buf);
243 err_map_addr:
244 pci_epc_unmap_addr(epc, epf->func_no, phys_addr);
246 err_addr:
247 pci_epc_mem_free_addr(epc, phys_addr, dst_addr, reg->size);
249 err:
250 return ret;
253 static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test, u8 irq_type,
254 u16 irq)
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;
264 switch (irq_type) {
265 case IRQ_TYPE_LEGACY:
266 pci_epc_raise_irq(epc, epf->func_no, PCI_EPC_IRQ_LEGACY, 0);
267 break;
268 case IRQ_TYPE_MSI:
269 pci_epc_raise_irq(epc, epf->func_no, PCI_EPC_IRQ_MSI, irq);
270 break;
271 case IRQ_TYPE_MSIX:
272 pci_epc_raise_irq(epc, epf->func_no, PCI_EPC_IRQ_MSIX, irq);
273 break;
274 default:
275 dev_err(dev, "Failed to raise IRQ, unknown type\n");
276 break;
280 static void pci_epf_test_cmd_handler(struct work_struct *work)
282 int ret;
283 int count;
284 u32 command;
285 struct pci_epf_test *epf_test = container_of(work, struct pci_epf_test,
286 cmd_handler.work);
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;
294 if (!command)
295 goto reset_handler;
297 reg->command = 0;
298 reg->status = 0;
300 if (reg->irq_type > IRQ_TYPE_MSIX) {
301 dev_err(dev, "Failed to detect IRQ type\n");
302 goto reset_handler;
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);
308 goto reset_handler;
311 if (command & COMMAND_WRITE) {
312 ret = pci_epf_test_write(epf_test);
313 if (ret)
314 reg->status |= STATUS_WRITE_FAIL;
315 else
316 reg->status |= STATUS_WRITE_SUCCESS;
317 pci_epf_test_raise_irq(epf_test, reg->irq_type,
318 reg->irq_number);
319 goto reset_handler;
322 if (command & COMMAND_READ) {
323 ret = pci_epf_test_read(epf_test);
324 if (!ret)
325 reg->status |= STATUS_READ_SUCCESS;
326 else
327 reg->status |= STATUS_READ_FAIL;
328 pci_epf_test_raise_irq(epf_test, reg->irq_type,
329 reg->irq_number);
330 goto reset_handler;
333 if (command & COMMAND_COPY) {
334 ret = pci_epf_test_copy(epf_test);
335 if (!ret)
336 reg->status |= STATUS_COPY_SUCCESS;
337 else
338 reg->status |= STATUS_COPY_FAIL;
339 pci_epf_test_raise_irq(epf_test, reg->irq_type,
340 reg->irq_number);
341 goto reset_handler;
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)
347 goto reset_handler;
348 reg->status = STATUS_IRQ_RAISED;
349 pci_epc_raise_irq(epc, epf->func_no, PCI_EPC_IRQ_MSI,
350 reg->irq_number);
351 goto reset_handler;
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)
357 goto reset_handler;
358 reg->status = STATUS_IRQ_RAISED;
359 pci_epc_raise_irq(epc, epf->func_no, PCI_EPC_IRQ_MSIX,
360 reg->irq_number);
361 goto reset_handler;
364 reset_handler:
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;
382 int bar;
384 cancel_delayed_work(&epf_test->cmd_handler);
385 pci_epc_stop(epc);
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)
398 int bar;
399 int ret;
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);
414 if (ret) {
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)
418 return ret;
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)
426 bar++;
429 return 0;
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;
436 void *base;
437 int bar;
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),
441 test_reg_bar);
442 if (!base) {
443 dev_err(dev, "Failed to allocated register space\n");
444 return -ENOMEM;
446 epf_test->reg[test_reg_bar] = base;
448 for (bar = BAR_0; bar <= BAR_5; bar++) {
449 if (bar == test_reg_bar)
450 continue;
451 base = pci_epf_alloc_space(epf, bar_size[bar], bar);
452 if (!base)
453 dev_err(dev, "Failed to allocate space for BAR%d\n",
454 bar);
455 epf_test->reg[bar] = base;
458 return 0;
461 static int pci_epf_test_bind(struct pci_epf *epf)
463 int ret;
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))
470 return -EINVAL;
472 if (epc->features & EPC_FEATURE_NO_LINKUP_NOTIFIER)
473 epf_test->linkup_notifier = false;
474 else
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);
482 if (ret) {
483 dev_err(dev, "Configuration header write failed\n");
484 return ret;
487 ret = pci_epf_test_alloc_space(epf);
488 if (ret)
489 return ret;
491 ret = pci_epf_test_set_bar(epf);
492 if (ret)
493 return ret;
495 ret = pci_epc_set_msi(epc, epf->func_no, epf->msi_interrupts);
496 if (ret) {
497 dev_err(dev, "MSI configuration failed\n");
498 return ret;
501 if (epf_test->msix_available) {
502 ret = pci_epc_set_msix(epc, epf->func_no, epf->msix_interrupts);
503 if (ret) {
504 dev_err(dev, "MSI-X configuration failed\n");
505 return ret;
509 if (!epf_test->linkup_notifier)
510 queue_work(kpcitest_workqueue, &epf_test->cmd_handler.work);
512 return 0;
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;
533 if (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);
539 if (!epf_test)
540 return -ENOMEM;
542 epf->header = &test_header;
543 epf_test->epf = epf;
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);
550 return 0;
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,
563 .ops = &ops,
564 .owner = THIS_MODULE,
567 static int __init pci_epf_test_init(void)
569 int ret;
571 kpcitest_workqueue = alloc_workqueue("kpcitest",
572 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
573 ret = pci_epf_register_driver(&test_driver);
574 if (ret) {
575 pr_err("Failed to register pci epf test driver --> %d\n", ret);
576 return ret;
579 return 0;
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");