2 * 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>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/pci_ids.h>
26 #include <linux/random.h>
28 #include <linux/pci-epc.h>
29 #include <linux/pci-epf.h>
30 #include <linux/pci_regs.h>
32 #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
33 #define COMMAND_RAISE_MSI_IRQ BIT(1)
34 #define MSI_NUMBER_SHIFT 2
35 #define MSI_NUMBER_MASK (0x3f << MSI_NUMBER_SHIFT)
36 #define COMMAND_READ BIT(8)
37 #define COMMAND_WRITE BIT(9)
38 #define COMMAND_COPY BIT(10)
40 #define STATUS_READ_SUCCESS BIT(0)
41 #define STATUS_READ_FAIL BIT(1)
42 #define STATUS_WRITE_SUCCESS BIT(2)
43 #define STATUS_WRITE_FAIL BIT(3)
44 #define STATUS_COPY_SUCCESS BIT(4)
45 #define STATUS_COPY_FAIL BIT(5)
46 #define STATUS_IRQ_RAISED BIT(6)
47 #define STATUS_SRC_ADDR_INVALID BIT(7)
48 #define STATUS_DST_ADDR_INVALID BIT(8)
50 #define TIMER_RESOLUTION 1
52 static struct workqueue_struct *kpcitest_workqueue;
57 enum pci_barno test_reg_bar;
59 struct delayed_work cmd_handler;
62 struct pci_epf_test_reg {
72 static struct pci_epf_header test_header = {
73 .vendorid = PCI_ANY_ID,
74 .deviceid = PCI_ANY_ID,
75 .baseclass_code = PCI_CLASS_OTHERS,
76 .interrupt_pin = PCI_INTERRUPT_INTA,
79 struct pci_epf_test_data {
80 enum pci_barno test_reg_bar;
84 static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
86 static int pci_epf_test_copy(struct pci_epf_test *epf_test)
89 void __iomem *src_addr;
90 void __iomem *dst_addr;
91 phys_addr_t src_phys_addr;
92 phys_addr_t dst_phys_addr;
93 struct pci_epf *epf = epf_test->epf;
94 struct device *dev = &epf->dev;
95 struct pci_epc *epc = epf->epc;
96 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
97 struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
99 src_addr = pci_epc_mem_alloc_addr(epc, &src_phys_addr, reg->size);
101 dev_err(dev, "failed to allocate source address\n");
102 reg->status = STATUS_SRC_ADDR_INVALID;
107 ret = pci_epc_map_addr(epc, src_phys_addr, reg->src_addr, reg->size);
109 dev_err(dev, "failed to map source address\n");
110 reg->status = STATUS_SRC_ADDR_INVALID;
114 dst_addr = pci_epc_mem_alloc_addr(epc, &dst_phys_addr, reg->size);
116 dev_err(dev, "failed to allocate destination address\n");
117 reg->status = STATUS_DST_ADDR_INVALID;
119 goto err_src_map_addr;
122 ret = pci_epc_map_addr(epc, dst_phys_addr, reg->dst_addr, reg->size);
124 dev_err(dev, "failed to map destination address\n");
125 reg->status = STATUS_DST_ADDR_INVALID;
129 memcpy(dst_addr, src_addr, reg->size);
131 pci_epc_unmap_addr(epc, dst_phys_addr);
134 pci_epc_mem_free_addr(epc, dst_phys_addr, dst_addr, reg->size);
137 pci_epc_unmap_addr(epc, src_phys_addr);
140 pci_epc_mem_free_addr(epc, src_phys_addr, src_addr, reg->size);
146 static int pci_epf_test_read(struct pci_epf_test *epf_test)
149 void __iomem *src_addr;
152 phys_addr_t phys_addr;
153 struct pci_epf *epf = epf_test->epf;
154 struct device *dev = &epf->dev;
155 struct pci_epc *epc = epf->epc;
156 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
157 struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
159 src_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
161 dev_err(dev, "failed to allocate address\n");
162 reg->status = STATUS_SRC_ADDR_INVALID;
167 ret = pci_epc_map_addr(epc, phys_addr, reg->src_addr, reg->size);
169 dev_err(dev, "failed to map address\n");
170 reg->status = STATUS_SRC_ADDR_INVALID;
174 buf = kzalloc(reg->size, GFP_KERNEL);
180 memcpy_fromio(buf, src_addr, reg->size);
182 crc32 = crc32_le(~0, buf, reg->size);
183 if (crc32 != reg->checksum)
189 pci_epc_unmap_addr(epc, phys_addr);
192 pci_epc_mem_free_addr(epc, phys_addr, src_addr, reg->size);
198 static int pci_epf_test_write(struct pci_epf_test *epf_test)
201 void __iomem *dst_addr;
203 phys_addr_t phys_addr;
204 struct pci_epf *epf = epf_test->epf;
205 struct device *dev = &epf->dev;
206 struct pci_epc *epc = epf->epc;
207 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
208 struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
210 dst_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
212 dev_err(dev, "failed to allocate address\n");
213 reg->status = STATUS_DST_ADDR_INVALID;
218 ret = pci_epc_map_addr(epc, phys_addr, reg->dst_addr, reg->size);
220 dev_err(dev, "failed to map address\n");
221 reg->status = STATUS_DST_ADDR_INVALID;
225 buf = kzalloc(reg->size, GFP_KERNEL);
231 get_random_bytes(buf, reg->size);
232 reg->checksum = crc32_le(~0, buf, reg->size);
234 memcpy_toio(dst_addr, buf, reg->size);
237 * wait 1ms inorder for the write to complete. Without this delay L3
238 * error in observed in the host system.
245 pci_epc_unmap_addr(epc, phys_addr);
248 pci_epc_mem_free_addr(epc, phys_addr, dst_addr, reg->size);
254 static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test, u8 irq)
257 struct pci_epf *epf = epf_test->epf;
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;
263 msi_count = pci_epc_get_msi(epc);
264 if (irq > msi_count || msi_count <= 0)
265 pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
267 pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
270 static void pci_epf_test_cmd_handler(struct work_struct *work)
276 struct pci_epf_test *epf_test = container_of(work, struct pci_epf_test,
278 struct pci_epf *epf = epf_test->epf;
279 struct pci_epc *epc = epf->epc;
280 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
281 struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
283 command = reg->command;
290 irq = (command & MSI_NUMBER_MASK) >> MSI_NUMBER_SHIFT;
292 if (command & COMMAND_RAISE_LEGACY_IRQ) {
293 reg->status = STATUS_IRQ_RAISED;
294 pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
298 if (command & COMMAND_WRITE) {
299 ret = pci_epf_test_write(epf_test);
301 reg->status |= STATUS_WRITE_FAIL;
303 reg->status |= STATUS_WRITE_SUCCESS;
304 pci_epf_test_raise_irq(epf_test, irq);
308 if (command & COMMAND_READ) {
309 ret = pci_epf_test_read(epf_test);
311 reg->status |= STATUS_READ_SUCCESS;
313 reg->status |= STATUS_READ_FAIL;
314 pci_epf_test_raise_irq(epf_test, irq);
318 if (command & COMMAND_COPY) {
319 ret = pci_epf_test_copy(epf_test);
321 reg->status |= STATUS_COPY_SUCCESS;
323 reg->status |= STATUS_COPY_FAIL;
324 pci_epf_test_raise_irq(epf_test, irq);
328 if (command & COMMAND_RAISE_MSI_IRQ) {
329 msi_count = pci_epc_get_msi(epc);
330 if (irq > msi_count || msi_count <= 0)
332 reg->status = STATUS_IRQ_RAISED;
333 pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
338 queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
339 msecs_to_jiffies(1));
342 static void pci_epf_test_linkup(struct pci_epf *epf)
344 struct pci_epf_test *epf_test = epf_get_drvdata(epf);
346 queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
347 msecs_to_jiffies(1));
350 static void pci_epf_test_unbind(struct pci_epf *epf)
352 struct pci_epf_test *epf_test = epf_get_drvdata(epf);
353 struct pci_epc *epc = epf->epc;
356 cancel_delayed_work(&epf_test->cmd_handler);
358 for (bar = BAR_0; bar <= BAR_5; bar++) {
359 if (epf_test->reg[bar]) {
360 pci_epf_free_space(epf, epf_test->reg[bar], bar);
361 pci_epc_clear_bar(epc, bar);
366 static int pci_epf_test_set_bar(struct pci_epf *epf)
371 struct pci_epf_bar *epf_bar;
372 struct pci_epc *epc = epf->epc;
373 struct device *dev = &epf->dev;
374 struct pci_epf_test *epf_test = epf_get_drvdata(epf);
375 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
377 flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
378 if (sizeof(dma_addr_t) == 0x8)
379 flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
381 for (bar = BAR_0; bar <= BAR_5; bar++) {
382 epf_bar = &epf->bar[bar];
383 ret = pci_epc_set_bar(epc, bar, epf_bar->phys_addr,
384 epf_bar->size, flags);
386 pci_epf_free_space(epf, epf_test->reg[bar], bar);
387 dev_err(dev, "failed to set BAR%d\n", bar);
388 if (bar == test_reg_bar)
396 static int pci_epf_test_alloc_space(struct pci_epf *epf)
398 struct pci_epf_test *epf_test = epf_get_drvdata(epf);
399 struct device *dev = &epf->dev;
402 enum pci_barno test_reg_bar = epf_test->test_reg_bar;
404 base = pci_epf_alloc_space(epf, sizeof(struct pci_epf_test_reg),
407 dev_err(dev, "failed to allocated register space\n");
410 epf_test->reg[test_reg_bar] = base;
412 for (bar = BAR_0; bar <= BAR_5; bar++) {
413 if (bar == test_reg_bar)
415 base = pci_epf_alloc_space(epf, bar_size[bar], bar);
417 dev_err(dev, "failed to allocate space for BAR%d\n",
419 epf_test->reg[bar] = base;
425 static int pci_epf_test_bind(struct pci_epf *epf)
428 struct pci_epf_test *epf_test = epf_get_drvdata(epf);
429 struct pci_epf_header *header = epf->header;
430 struct pci_epc *epc = epf->epc;
431 struct device *dev = &epf->dev;
433 if (WARN_ON_ONCE(!epc))
436 ret = pci_epc_write_header(epc, header);
438 dev_err(dev, "configuration header write failed\n");
442 ret = pci_epf_test_alloc_space(epf);
446 ret = pci_epf_test_set_bar(epf);
450 ret = pci_epc_set_msi(epc, epf->msi_interrupts);
454 if (!epf_test->linkup_notifier)
455 queue_work(kpcitest_workqueue, &epf_test->cmd_handler.work);
460 static const struct pci_epf_device_id pci_epf_test_ids[] = {
462 .name = "pci_epf_test",
467 static int pci_epf_test_probe(struct pci_epf *epf)
469 struct pci_epf_test *epf_test;
470 struct device *dev = &epf->dev;
471 const struct pci_epf_device_id *match;
472 struct pci_epf_test_data *data;
473 enum pci_barno test_reg_bar = BAR_0;
474 bool linkup_notifier = true;
476 match = pci_epf_match_device(pci_epf_test_ids, epf);
477 data = (struct pci_epf_test_data *)match->driver_data;
479 test_reg_bar = data->test_reg_bar;
480 linkup_notifier = data->linkup_notifier;
483 epf_test = devm_kzalloc(dev, sizeof(*epf_test), GFP_KERNEL);
487 epf->header = &test_header;
489 epf_test->test_reg_bar = test_reg_bar;
490 epf_test->linkup_notifier = linkup_notifier;
492 INIT_DELAYED_WORK(&epf_test->cmd_handler, pci_epf_test_cmd_handler);
494 epf_set_drvdata(epf, epf_test);
498 static struct pci_epf_ops ops = {
499 .unbind = pci_epf_test_unbind,
500 .bind = pci_epf_test_bind,
501 .linkup = pci_epf_test_linkup,
504 static struct pci_epf_driver test_driver = {
505 .driver.name = "pci_epf_test",
506 .probe = pci_epf_test_probe,
507 .id_table = pci_epf_test_ids,
509 .owner = THIS_MODULE,
512 static int __init pci_epf_test_init(void)
516 kpcitest_workqueue = alloc_workqueue("kpcitest",
517 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
518 ret = pci_epf_register_driver(&test_driver);
520 pr_err("failed to register pci epf test driver --> %d\n", ret);
526 module_init(pci_epf_test_init);
528 static void __exit pci_epf_test_exit(void)
530 pci_epf_unregister_driver(&test_driver);
532 module_exit(pci_epf_test_exit);
534 MODULE_DESCRIPTION("PCI EPF TEST DRIVER");
535 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
536 MODULE_LICENSE("GPL v2");