misc: pci_endpoint_test: Add support to provide aligned buffer addresses
[linux-2.6-block.git] / drivers / misc / pci_endpoint_test.c
1 /**
2  * Host side test driver to test endpoint functionality
3  *
4  * Copyright (C) 2017 Texas Instruments
5  * Author: Kishon Vijay Abraham I <kishon@ti.com>
6  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <linux/crc32.h>
21 #include <linux/delay.h>
22 #include <linux/fs.h>
23 #include <linux/io.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>
33
34 #include <linux/pci_regs.h>
35
36 #include <uapi/linux/pcitest.h>
37
38 #define DRV_MODULE_NAME                 "pci-endpoint-test"
39
40 #define PCI_ENDPOINT_TEST_MAGIC         0x0
41
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)
50
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)
61
62 #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR        0xc
63 #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR        0x10
64
65 #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR        0x14
66 #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR        0x18
67
68 #define PCI_ENDPOINT_TEST_SIZE          0x1c
69 #define PCI_ENDPOINT_TEST_CHECKSUM      0x20
70
71 static DEFINE_IDA(pci_endpoint_test_ida);
72
73 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
74                                             miscdev)
75 enum pci_barno {
76         BAR_0,
77         BAR_1,
78         BAR_2,
79         BAR_3,
80         BAR_4,
81         BAR_5,
82 };
83
84 struct pci_endpoint_test {
85         struct pci_dev  *pdev;
86         void __iomem    *base;
87         void __iomem    *bar[6];
88         struct completion irq_raised;
89         int             last_irq;
90         /* mutex to protect the ioctls */
91         struct mutex    mutex;
92         struct miscdevice miscdev;
93         enum pci_barno test_reg_bar;
94         size_t alignment;
95 };
96
97 struct pci_endpoint_test_data {
98         enum pci_barno test_reg_bar;
99         size_t alignment;
100 };
101
102 static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
103
104 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
105                                           u32 offset)
106 {
107         return readl(test->base + offset);
108 }
109
110 static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
111                                             u32 offset, u32 value)
112 {
113         writel(value, test->base + offset);
114 }
115
116 static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
117                                               int bar, int offset)
118 {
119         return readl(test->bar[bar] + offset);
120 }
121
122 static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
123                                                 int bar, u32 offset, u32 value)
124 {
125         writel(value, test->bar[bar] + offset);
126 }
127
128 static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
129 {
130         struct pci_endpoint_test *test = dev_id;
131         u32 reg;
132
133         reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
134         if (reg & STATUS_IRQ_RAISED) {
135                 test->last_irq = irq;
136                 complete(&test->irq_raised);
137                 reg &= ~STATUS_IRQ_RAISED;
138         }
139         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
140                                  reg);
141
142         return IRQ_HANDLED;
143 }
144
145 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
146                                   enum pci_barno barno)
147 {
148         int j;
149         u32 val;
150         int size;
151
152         if (!test->bar[barno])
153                 return false;
154
155         size = bar_size[barno];
156
157         if (barno == test->test_reg_bar)
158                 size = 0x4;
159
160         for (j = 0; j < size; j += 4)
161                 pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
162
163         for (j = 0; j < size; j += 4) {
164                 val = pci_endpoint_test_bar_readl(test, barno, j);
165                 if (val != 0xA0A0A0A0)
166                         return false;
167         }
168
169         return true;
170 }
171
172 static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
173 {
174         u32 val;
175
176         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
177                                  COMMAND_RAISE_LEGACY_IRQ);
178         val = wait_for_completion_timeout(&test->irq_raised,
179                                           msecs_to_jiffies(1000));
180         if (!val)
181                 return false;
182
183         return true;
184 }
185
186 static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
187                                       u8 msi_num)
188 {
189         u32 val;
190         struct pci_dev *pdev = test->pdev;
191
192         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
193                                  msi_num << MSI_NUMBER_SHIFT |
194                                  COMMAND_RAISE_MSI_IRQ);
195         val = wait_for_completion_timeout(&test->irq_raised,
196                                           msecs_to_jiffies(1000));
197         if (!val)
198                 return false;
199
200         if (test->last_irq - pdev->irq == msi_num - 1)
201                 return true;
202
203         return false;
204 }
205
206 static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
207 {
208         bool ret = false;
209         void *src_addr;
210         void *dst_addr;
211         dma_addr_t src_phys_addr;
212         dma_addr_t dst_phys_addr;
213         struct pci_dev *pdev = test->pdev;
214         struct device *dev = &pdev->dev;
215         void *orig_src_addr;
216         dma_addr_t orig_src_phys_addr;
217         void *orig_dst_addr;
218         dma_addr_t orig_dst_phys_addr;
219         size_t offset;
220         size_t alignment = test->alignment;
221         u32 src_crc32;
222         u32 dst_crc32;
223
224         orig_src_addr = dma_alloc_coherent(dev, size + alignment,
225                                            &orig_src_phys_addr, GFP_KERNEL);
226         if (!orig_src_addr) {
227                 dev_err(dev, "failed to allocate source buffer\n");
228                 ret = false;
229                 goto err;
230         }
231
232         if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
233                 src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
234                 offset = src_phys_addr - orig_src_phys_addr;
235                 src_addr = orig_src_addr + offset;
236         } else {
237                 src_phys_addr = orig_src_phys_addr;
238                 src_addr = orig_src_addr;
239         }
240
241         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
242                                  lower_32_bits(src_phys_addr));
243
244         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
245                                  upper_32_bits(src_phys_addr));
246
247         get_random_bytes(src_addr, size);
248         src_crc32 = crc32_le(~0, src_addr, size);
249
250         orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
251                                            &orig_dst_phys_addr, GFP_KERNEL);
252         if (!orig_dst_addr) {
253                 dev_err(dev, "failed to allocate destination address\n");
254                 ret = false;
255                 goto err_orig_src_addr;
256         }
257
258         if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
259                 dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
260                 offset = dst_phys_addr - orig_dst_phys_addr;
261                 dst_addr = orig_dst_addr + offset;
262         } else {
263                 dst_phys_addr = orig_dst_phys_addr;
264                 dst_addr = orig_dst_addr;
265         }
266
267         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
268                                  lower_32_bits(dst_phys_addr));
269         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
270                                  upper_32_bits(dst_phys_addr));
271
272         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
273                                  size);
274
275         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
276                                  1 << MSI_NUMBER_SHIFT | COMMAND_COPY);
277
278         wait_for_completion(&test->irq_raised);
279
280         dst_crc32 = crc32_le(~0, dst_addr, size);
281         if (dst_crc32 == src_crc32)
282                 ret = true;
283
284         dma_free_coherent(dev, size + alignment, orig_dst_addr,
285                           orig_dst_phys_addr);
286
287 err_orig_src_addr:
288         dma_free_coherent(dev, size + alignment, orig_src_addr,
289                           orig_src_phys_addr);
290
291 err:
292         return ret;
293 }
294
295 static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
296 {
297         bool ret = false;
298         u32 reg;
299         void *addr;
300         dma_addr_t phys_addr;
301         struct pci_dev *pdev = test->pdev;
302         struct device *dev = &pdev->dev;
303         void *orig_addr;
304         dma_addr_t orig_phys_addr;
305         size_t offset;
306         size_t alignment = test->alignment;
307         u32 crc32;
308
309         orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
310                                        GFP_KERNEL);
311         if (!orig_addr) {
312                 dev_err(dev, "failed to allocate address\n");
313                 ret = false;
314                 goto err;
315         }
316
317         if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
318                 phys_addr =  PTR_ALIGN(orig_phys_addr, alignment);
319                 offset = phys_addr - orig_phys_addr;
320                 addr = orig_addr + offset;
321         } else {
322                 phys_addr = orig_phys_addr;
323                 addr = orig_addr;
324         }
325
326         get_random_bytes(addr, size);
327
328         crc32 = crc32_le(~0, addr, size);
329         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
330                                  crc32);
331
332         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
333                                  lower_32_bits(phys_addr));
334         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
335                                  upper_32_bits(phys_addr));
336
337         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
338
339         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
340                                  1 << MSI_NUMBER_SHIFT | COMMAND_READ);
341
342         wait_for_completion(&test->irq_raised);
343
344         reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
345         if (reg & STATUS_READ_SUCCESS)
346                 ret = true;
347
348         dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
349
350 err:
351         return ret;
352 }
353
354 static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
355 {
356         bool ret = false;
357         void *addr;
358         dma_addr_t phys_addr;
359         struct pci_dev *pdev = test->pdev;
360         struct device *dev = &pdev->dev;
361         void *orig_addr;
362         dma_addr_t orig_phys_addr;
363         size_t offset;
364         size_t alignment = test->alignment;
365         u32 crc32;
366
367         orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
368                                        GFP_KERNEL);
369         if (!orig_addr) {
370                 dev_err(dev, "failed to allocate destination address\n");
371                 ret = false;
372                 goto err;
373         }
374
375         if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
376                 phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
377                 offset = phys_addr - orig_phys_addr;
378                 addr = orig_addr + offset;
379         } else {
380                 phys_addr = orig_phys_addr;
381                 addr = orig_addr;
382         }
383
384         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
385                                  lower_32_bits(phys_addr));
386         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
387                                  upper_32_bits(phys_addr));
388
389         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
390
391         pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
392                                  1 << MSI_NUMBER_SHIFT | COMMAND_WRITE);
393
394         wait_for_completion(&test->irq_raised);
395
396         crc32 = crc32_le(~0, addr, size);
397         if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
398                 ret = true;
399
400         dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
401 err:
402         return ret;
403 }
404
405 static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
406                                     unsigned long arg)
407 {
408         int ret = -EINVAL;
409         enum pci_barno bar;
410         struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
411
412         mutex_lock(&test->mutex);
413         switch (cmd) {
414         case PCITEST_BAR:
415                 bar = arg;
416                 if (bar < 0 || bar > 5)
417                         goto ret;
418                 ret = pci_endpoint_test_bar(test, bar);
419                 break;
420         case PCITEST_LEGACY_IRQ:
421                 ret = pci_endpoint_test_legacy_irq(test);
422                 break;
423         case PCITEST_MSI:
424                 ret = pci_endpoint_test_msi_irq(test, arg);
425                 break;
426         case PCITEST_WRITE:
427                 ret = pci_endpoint_test_write(test, arg);
428                 break;
429         case PCITEST_READ:
430                 ret = pci_endpoint_test_read(test, arg);
431                 break;
432         case PCITEST_COPY:
433                 ret = pci_endpoint_test_copy(test, arg);
434                 break;
435         }
436
437 ret:
438         mutex_unlock(&test->mutex);
439         return ret;
440 }
441
442 static const struct file_operations pci_endpoint_test_fops = {
443         .owner = THIS_MODULE,
444         .unlocked_ioctl = pci_endpoint_test_ioctl,
445 };
446
447 static int pci_endpoint_test_probe(struct pci_dev *pdev,
448                                    const struct pci_device_id *ent)
449 {
450         int i;
451         int err;
452         int irq;
453         int id;
454         char name[20];
455         enum pci_barno bar;
456         void __iomem *base;
457         struct device *dev = &pdev->dev;
458         struct pci_endpoint_test *test;
459         struct pci_endpoint_test_data *data;
460         enum pci_barno test_reg_bar = BAR_0;
461         struct miscdevice *misc_device;
462
463         if (pci_is_bridge(pdev))
464                 return -ENODEV;
465
466         test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
467         if (!test)
468                 return -ENOMEM;
469
470         test->test_reg_bar = 0;
471         test->alignment = 0;
472         test->pdev = pdev;
473
474         data = (struct pci_endpoint_test_data *)ent->driver_data;
475         if (data) {
476                 test_reg_bar = data->test_reg_bar;
477                 test->alignment = data->alignment;
478         }
479
480         init_completion(&test->irq_raised);
481         mutex_init(&test->mutex);
482
483         err = pci_enable_device(pdev);
484         if (err) {
485                 dev_err(dev, "Cannot enable PCI device\n");
486                 return err;
487         }
488
489         err = pci_request_regions(pdev, DRV_MODULE_NAME);
490         if (err) {
491                 dev_err(dev, "Cannot obtain PCI resources\n");
492                 goto err_disable_pdev;
493         }
494
495         pci_set_master(pdev);
496
497         irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
498         if (irq < 0)
499                 dev_err(dev, "failed to get MSI interrupts\n");
500
501         err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
502                                IRQF_SHARED, DRV_MODULE_NAME, test);
503         if (err) {
504                 dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
505                 goto err_disable_msi;
506         }
507
508         for (i = 1; i < irq; i++) {
509                 err = devm_request_irq(dev, pdev->irq + i,
510                                        pci_endpoint_test_irqhandler,
511                                        IRQF_SHARED, DRV_MODULE_NAME, test);
512                 if (err)
513                         dev_err(dev, "failed to request IRQ %d for MSI %d\n",
514                                 pdev->irq + i, i + 1);
515         }
516
517         for (bar = BAR_0; bar <= BAR_5; bar++) {
518                 base = pci_ioremap_bar(pdev, bar);
519                 if (!base) {
520                         dev_err(dev, "failed to read BAR%d\n", bar);
521                         WARN_ON(bar == test_reg_bar);
522                 }
523                 test->bar[bar] = base;
524         }
525
526         test->base = test->bar[test_reg_bar];
527         if (!test->base) {
528                 dev_err(dev, "Cannot perform PCI test without BAR%d\n",
529                         test_reg_bar);
530                 goto err_iounmap;
531         }
532
533         pci_set_drvdata(pdev, test);
534
535         id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
536         if (id < 0) {
537                 dev_err(dev, "unable to get id\n");
538                 goto err_iounmap;
539         }
540
541         snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
542         misc_device = &test->miscdev;
543         misc_device->minor = MISC_DYNAMIC_MINOR;
544         misc_device->name = name;
545         misc_device->fops = &pci_endpoint_test_fops,
546
547         err = misc_register(misc_device);
548         if (err) {
549                 dev_err(dev, "failed to register device\n");
550                 goto err_ida_remove;
551         }
552
553         return 0;
554
555 err_ida_remove:
556         ida_simple_remove(&pci_endpoint_test_ida, id);
557
558 err_iounmap:
559         for (bar = BAR_0; bar <= BAR_5; bar++) {
560                 if (test->bar[bar])
561                         pci_iounmap(pdev, test->bar[bar]);
562         }
563
564 err_disable_msi:
565         pci_disable_msi(pdev);
566         pci_release_regions(pdev);
567
568 err_disable_pdev:
569         pci_disable_device(pdev);
570
571         return err;
572 }
573
574 static void pci_endpoint_test_remove(struct pci_dev *pdev)
575 {
576         int id;
577         enum pci_barno bar;
578         struct pci_endpoint_test *test = pci_get_drvdata(pdev);
579         struct miscdevice *misc_device = &test->miscdev;
580
581         if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
582                 return;
583
584         misc_deregister(&test->miscdev);
585         ida_simple_remove(&pci_endpoint_test_ida, id);
586         for (bar = BAR_0; bar <= BAR_5; bar++) {
587                 if (test->bar[bar])
588                         pci_iounmap(pdev, test->bar[bar]);
589         }
590         pci_disable_msi(pdev);
591         pci_release_regions(pdev);
592         pci_disable_device(pdev);
593 }
594
595 static const struct pci_device_id pci_endpoint_test_tbl[] = {
596         { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
597         { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
598         { }
599 };
600 MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
601
602 static struct pci_driver pci_endpoint_test_driver = {
603         .name           = DRV_MODULE_NAME,
604         .id_table       = pci_endpoint_test_tbl,
605         .probe          = pci_endpoint_test_probe,
606         .remove         = pci_endpoint_test_remove,
607 };
608 module_pci_driver(pci_endpoint_test_driver);
609
610 MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
611 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
612 MODULE_LICENSE("GPL v2");