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, \
84 struct pci_endpoint_test {
88 struct completion irq_raised;
90 /* mutex to protect the ioctls */
92 struct miscdevice miscdev;
95 static int bar_size[] = { 4, 512, 1024, 16384, 131072, 1048576 };
97 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
100 return readl(test->base + offset);
103 static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
104 u32 offset, u32 value)
106 writel(value, test->base + offset);
109 static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
112 return readl(test->bar[bar] + offset);
115 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
116 int bar, u32 offset, u32 value)
118 writel(value, test->bar[bar] + offset);
121 static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
123 struct pci_endpoint_test *test = dev_id;
126 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
127 if (reg & STATUS_IRQ_RAISED) {
128 test->last_irq = irq;
129 complete(&test->irq_raised);
130 reg &= ~STATUS_IRQ_RAISED;
132 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
138 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
139 enum pci_barno barno)
145 if (!test->bar[barno])
148 size = bar_size[barno];
150 for (j = 0; j < size; j += 4)
151 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
153 for (j = 0; j < size; j += 4) {
154 val = pci_endpoint_test_bar_readl(test, barno, j);
155 if (val != 0xA0A0A0A0)
162 static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
166 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
167 COMMAND_RAISE_LEGACY_IRQ);
168 val = wait_for_completion_timeout(&test->irq_raised,
169 msecs_to_jiffies(1000));
176 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
180 struct pci_dev *pdev = test->pdev;
182 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
183 msi_num << MSI_NUMBER_SHIFT |
184 COMMAND_RAISE_MSI_IRQ);
185 val = wait_for_completion_timeout(&test->irq_raised,
186 msecs_to_jiffies(1000));
190 if (test->last_irq - pdev->irq == msi_num - 1)
196 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
201 dma_addr_t src_phys_addr;
202 dma_addr_t dst_phys_addr;
203 struct pci_dev *pdev = test->pdev;
204 struct device *dev = &pdev->dev;
208 src_addr = dma_alloc_coherent(dev, size, &src_phys_addr, GFP_KERNEL);
210 dev_err(dev, "failed to allocate source buffer\n");
215 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
216 lower_32_bits(src_phys_addr));
218 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
219 upper_32_bits(src_phys_addr));
221 get_random_bytes(src_addr, size);
222 src_crc32 = crc32_le(~0, src_addr, size);
224 dst_addr = dma_alloc_coherent(dev, size, &dst_phys_addr, GFP_KERNEL);
226 dev_err(dev, "failed to allocate destination address\n");
231 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
232 lower_32_bits(dst_phys_addr));
233 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
234 upper_32_bits(dst_phys_addr));
236 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
239 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
240 1 << MSI_NUMBER_SHIFT | COMMAND_COPY);
242 wait_for_completion(&test->irq_raised);
244 dst_crc32 = crc32_le(~0, dst_addr, size);
245 if (dst_crc32 == src_crc32)
248 dma_free_coherent(dev, size, dst_addr, dst_phys_addr);
251 dma_free_coherent(dev, size, src_addr, src_phys_addr);
257 static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
262 dma_addr_t phys_addr;
263 struct pci_dev *pdev = test->pdev;
264 struct device *dev = &pdev->dev;
267 addr = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
269 dev_err(dev, "failed to allocate address\n");
274 get_random_bytes(addr, size);
276 crc32 = crc32_le(~0, addr, size);
277 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
280 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
281 lower_32_bits(phys_addr));
282 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
283 upper_32_bits(phys_addr));
285 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
287 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
288 1 << MSI_NUMBER_SHIFT | COMMAND_READ);
290 wait_for_completion(&test->irq_raised);
292 reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
293 if (reg & STATUS_READ_SUCCESS)
296 dma_free_coherent(dev, size, addr, phys_addr);
302 static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
306 dma_addr_t phys_addr;
307 struct pci_dev *pdev = test->pdev;
308 struct device *dev = &pdev->dev;
311 addr = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
313 dev_err(dev, "failed to allocate destination address\n");
318 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
319 lower_32_bits(phys_addr));
320 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
321 upper_32_bits(phys_addr));
323 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
325 pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
326 1 << MSI_NUMBER_SHIFT | COMMAND_WRITE);
328 wait_for_completion(&test->irq_raised);
330 crc32 = crc32_le(~0, addr, size);
331 if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
334 dma_free_coherent(dev, size, addr, phys_addr);
339 static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
344 struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
346 mutex_lock(&test->mutex);
350 if (bar < 0 || bar > 5)
352 ret = pci_endpoint_test_bar(test, bar);
354 case PCITEST_LEGACY_IRQ:
355 ret = pci_endpoint_test_legacy_irq(test);
358 ret = pci_endpoint_test_msi_irq(test, arg);
361 ret = pci_endpoint_test_write(test, arg);
364 ret = pci_endpoint_test_read(test, arg);
367 ret = pci_endpoint_test_copy(test, arg);
372 mutex_unlock(&test->mutex);
376 static const struct file_operations pci_endpoint_test_fops = {
377 .owner = THIS_MODULE,
378 .unlocked_ioctl = pci_endpoint_test_ioctl,
381 static int pci_endpoint_test_probe(struct pci_dev *pdev,
382 const struct pci_device_id *ent)
391 struct device *dev = &pdev->dev;
392 struct pci_endpoint_test *test;
393 struct miscdevice *misc_device;
395 if (pci_is_bridge(pdev))
398 test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
403 init_completion(&test->irq_raised);
404 mutex_init(&test->mutex);
406 err = pci_enable_device(pdev);
408 dev_err(dev, "Cannot enable PCI device\n");
412 err = pci_request_regions(pdev, DRV_MODULE_NAME);
414 dev_err(dev, "Cannot obtain PCI resources\n");
415 goto err_disable_pdev;
418 pci_set_master(pdev);
420 irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
422 dev_err(dev, "failed to get MSI interrupts\n");
424 err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
425 IRQF_SHARED, DRV_MODULE_NAME, test);
427 dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
428 goto err_disable_msi;
431 for (i = 1; i < irq; i++) {
432 err = devm_request_irq(dev, pdev->irq + i,
433 pci_endpoint_test_irqhandler,
434 IRQF_SHARED, DRV_MODULE_NAME, test);
436 dev_err(dev, "failed to request IRQ %d for MSI %d\n",
437 pdev->irq + i, i + 1);
440 for (bar = BAR_0; bar <= BAR_5; bar++) {
441 base = pci_ioremap_bar(pdev, bar);
443 dev_err(dev, "failed to read BAR%d\n", bar);
444 WARN_ON(bar == BAR_0);
446 test->bar[bar] = base;
449 test->base = test->bar[0];
451 dev_err(dev, "Cannot perform PCI test without BAR0\n");
455 pci_set_drvdata(pdev, test);
457 id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
459 dev_err(dev, "unable to get id\n");
463 snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
464 misc_device = &test->miscdev;
465 misc_device->minor = MISC_DYNAMIC_MINOR;
466 misc_device->name = name;
467 misc_device->fops = &pci_endpoint_test_fops,
469 err = misc_register(misc_device);
471 dev_err(dev, "failed to register device\n");
478 ida_simple_remove(&pci_endpoint_test_ida, id);
481 for (bar = BAR_0; bar <= BAR_5; bar++) {
483 pci_iounmap(pdev, test->bar[bar]);
487 pci_disable_msi(pdev);
488 pci_release_regions(pdev);
491 pci_disable_device(pdev);
496 static void pci_endpoint_test_remove(struct pci_dev *pdev)
500 struct pci_endpoint_test *test = pci_get_drvdata(pdev);
501 struct miscdevice *misc_device = &test->miscdev;
503 if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
506 misc_deregister(&test->miscdev);
507 ida_simple_remove(&pci_endpoint_test_ida, id);
508 for (bar = BAR_0; bar <= BAR_5; bar++) {
510 pci_iounmap(pdev, test->bar[bar]);
512 pci_disable_msi(pdev);
513 pci_release_regions(pdev);
514 pci_disable_device(pdev);
517 static const struct pci_device_id pci_endpoint_test_tbl[] = {
518 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
519 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
522 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
524 static struct pci_driver pci_endpoint_test_driver = {
525 .name = DRV_MODULE_NAME,
526 .id_table = pci_endpoint_test_tbl,
527 .probe = pci_endpoint_test_probe,
528 .remove = pci_endpoint_test_remove,
530 module_pci_driver(pci_endpoint_test_driver);
532 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
533 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
534 MODULE_LICENSE("GPL v2");