vfio: platform: add extra debug info argument to call reset
[cascardo/linux.git] / drivers / vfio / platform / vfio_platform_common.c
1 /*
2  * Copyright (C) 2013 - Virtual Open Systems
3  * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 2, as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/device.h>
16 #include <linux/acpi.h>
17 #include <linux/iommu.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <linux/uaccess.h>
23 #include <linux/vfio.h>
24
25 #include "vfio_platform_private.h"
26
27 #define DRIVER_VERSION  "0.10"
28 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis@virtualopensystems.com>"
29 #define DRIVER_DESC     "VFIO platform base module"
30
31 static LIST_HEAD(reset_list);
32 static DEFINE_MUTEX(driver_lock);
33
34 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
35                                         struct module **module)
36 {
37         struct vfio_platform_reset_node *iter;
38         vfio_platform_reset_fn_t reset_fn = NULL;
39
40         mutex_lock(&driver_lock);
41         list_for_each_entry(iter, &reset_list, link) {
42                 if (!strcmp(iter->compat, compat) &&
43                         try_module_get(iter->owner)) {
44                         *module = iter->owner;
45                         reset_fn = iter->of_reset;
46                         break;
47                 }
48         }
49         mutex_unlock(&driver_lock);
50         return reset_fn;
51 }
52
53 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
54                                     struct device *dev)
55 {
56         struct acpi_device *adev;
57
58         if (acpi_disabled)
59                 return -ENOENT;
60
61         adev = ACPI_COMPANION(dev);
62         if (!adev) {
63                 pr_err("VFIO: ACPI companion device not found for %s\n",
64                         vdev->name);
65                 return -ENODEV;
66         }
67
68 #ifdef CONFIG_ACPI
69         vdev->acpihid = acpi_device_hid(adev);
70 #endif
71         return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
72 }
73
74 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
75 {
76         return vdev->of_reset ? true : false;
77 }
78
79 static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
80 {
81         vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
82                                                     &vdev->reset_module);
83         if (!vdev->of_reset) {
84                 request_module("vfio-reset:%s", vdev->compat);
85                 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
86                                                         &vdev->reset_module);
87         }
88 }
89
90 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
91 {
92         if (vdev->of_reset)
93                 module_put(vdev->reset_module);
94 }
95
96 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
97 {
98         int cnt = 0, i;
99
100         while (vdev->get_resource(vdev, cnt))
101                 cnt++;
102
103         vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
104                                 GFP_KERNEL);
105         if (!vdev->regions)
106                 return -ENOMEM;
107
108         for (i = 0; i < cnt;  i++) {
109                 struct resource *res =
110                         vdev->get_resource(vdev, i);
111
112                 if (!res)
113                         goto err;
114
115                 vdev->regions[i].addr = res->start;
116                 vdev->regions[i].size = resource_size(res);
117                 vdev->regions[i].flags = 0;
118
119                 switch (resource_type(res)) {
120                 case IORESOURCE_MEM:
121                         vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
122                         vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
123                         if (!(res->flags & IORESOURCE_READONLY))
124                                 vdev->regions[i].flags |=
125                                         VFIO_REGION_INFO_FLAG_WRITE;
126
127                         /*
128                          * Only regions addressed with PAGE granularity may be
129                          * MMAPed securely.
130                          */
131                         if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
132                                         !(vdev->regions[i].size & ~PAGE_MASK))
133                                 vdev->regions[i].flags |=
134                                         VFIO_REGION_INFO_FLAG_MMAP;
135
136                         break;
137                 case IORESOURCE_IO:
138                         vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
139                         break;
140                 default:
141                         goto err;
142                 }
143         }
144
145         vdev->num_regions = cnt;
146
147         return 0;
148 err:
149         kfree(vdev->regions);
150         return -EINVAL;
151 }
152
153 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
154 {
155         int i;
156
157         for (i = 0; i < vdev->num_regions; i++)
158                 iounmap(vdev->regions[i].ioaddr);
159
160         vdev->num_regions = 0;
161         kfree(vdev->regions);
162 }
163
164 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
165                                     const char **extra_dbg)
166 {
167         if (vdev->of_reset) {
168                 dev_info(vdev->device, "reset\n");
169                 return vdev->of_reset(vdev);
170         }
171
172         dev_warn(vdev->device, "no reset function found!\n");
173         return -EINVAL;
174 }
175
176 static void vfio_platform_release(void *device_data)
177 {
178         struct vfio_platform_device *vdev = device_data;
179
180         mutex_lock(&driver_lock);
181
182         if (!(--vdev->refcnt)) {
183                 vfio_platform_call_reset(vdev, NULL);
184                 vfio_platform_regions_cleanup(vdev);
185                 vfio_platform_irq_cleanup(vdev);
186         }
187
188         mutex_unlock(&driver_lock);
189
190         module_put(vdev->parent_module);
191 }
192
193 static int vfio_platform_open(void *device_data)
194 {
195         struct vfio_platform_device *vdev = device_data;
196         int ret;
197
198         if (!try_module_get(vdev->parent_module))
199                 return -ENODEV;
200
201         mutex_lock(&driver_lock);
202
203         if (!vdev->refcnt) {
204                 ret = vfio_platform_regions_init(vdev);
205                 if (ret)
206                         goto err_reg;
207
208                 ret = vfio_platform_irq_init(vdev);
209                 if (ret)
210                         goto err_irq;
211
212                 vfio_platform_call_reset(vdev, NULL);
213         }
214
215         vdev->refcnt++;
216
217         mutex_unlock(&driver_lock);
218         return 0;
219
220 err_irq:
221         vfio_platform_regions_cleanup(vdev);
222 err_reg:
223         mutex_unlock(&driver_lock);
224         module_put(THIS_MODULE);
225         return ret;
226 }
227
228 static long vfio_platform_ioctl(void *device_data,
229                                 unsigned int cmd, unsigned long arg)
230 {
231         struct vfio_platform_device *vdev = device_data;
232         unsigned long minsz;
233
234         if (cmd == VFIO_DEVICE_GET_INFO) {
235                 struct vfio_device_info info;
236
237                 minsz = offsetofend(struct vfio_device_info, num_irqs);
238
239                 if (copy_from_user(&info, (void __user *)arg, minsz))
240                         return -EFAULT;
241
242                 if (info.argsz < minsz)
243                         return -EINVAL;
244
245                 if (vfio_platform_has_reset(vdev))
246                         vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
247                 info.flags = vdev->flags;
248                 info.num_regions = vdev->num_regions;
249                 info.num_irqs = vdev->num_irqs;
250
251                 return copy_to_user((void __user *)arg, &info, minsz) ?
252                         -EFAULT : 0;
253
254         } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
255                 struct vfio_region_info info;
256
257                 minsz = offsetofend(struct vfio_region_info, offset);
258
259                 if (copy_from_user(&info, (void __user *)arg, minsz))
260                         return -EFAULT;
261
262                 if (info.argsz < minsz)
263                         return -EINVAL;
264
265                 if (info.index >= vdev->num_regions)
266                         return -EINVAL;
267
268                 /* map offset to the physical address  */
269                 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
270                 info.size = vdev->regions[info.index].size;
271                 info.flags = vdev->regions[info.index].flags;
272
273                 return copy_to_user((void __user *)arg, &info, minsz) ?
274                         -EFAULT : 0;
275
276         } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
277                 struct vfio_irq_info info;
278
279                 minsz = offsetofend(struct vfio_irq_info, count);
280
281                 if (copy_from_user(&info, (void __user *)arg, minsz))
282                         return -EFAULT;
283
284                 if (info.argsz < minsz)
285                         return -EINVAL;
286
287                 if (info.index >= vdev->num_irqs)
288                         return -EINVAL;
289
290                 info.flags = vdev->irqs[info.index].flags;
291                 info.count = vdev->irqs[info.index].count;
292
293                 return copy_to_user((void __user *)arg, &info, minsz) ?
294                         -EFAULT : 0;
295
296         } else if (cmd == VFIO_DEVICE_SET_IRQS) {
297                 struct vfio_irq_set hdr;
298                 u8 *data = NULL;
299                 int ret = 0;
300
301                 minsz = offsetofend(struct vfio_irq_set, count);
302
303                 if (copy_from_user(&hdr, (void __user *)arg, minsz))
304                         return -EFAULT;
305
306                 if (hdr.argsz < minsz)
307                         return -EINVAL;
308
309                 if (hdr.index >= vdev->num_irqs)
310                         return -EINVAL;
311
312                 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
313                                   VFIO_IRQ_SET_ACTION_TYPE_MASK))
314                         return -EINVAL;
315
316                 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
317                         size_t size;
318
319                         if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
320                                 size = sizeof(uint8_t);
321                         else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
322                                 size = sizeof(int32_t);
323                         else
324                                 return -EINVAL;
325
326                         if (hdr.argsz - minsz < size)
327                                 return -EINVAL;
328
329                         data = memdup_user((void __user *)(arg + minsz), size);
330                         if (IS_ERR(data))
331                                 return PTR_ERR(data);
332                 }
333
334                 mutex_lock(&vdev->igate);
335
336                 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
337                                                    hdr.start, hdr.count, data);
338                 mutex_unlock(&vdev->igate);
339                 kfree(data);
340
341                 return ret;
342
343         } else if (cmd == VFIO_DEVICE_RESET) {
344                 return vfio_platform_call_reset(vdev, NULL);
345         }
346
347         return -ENOTTY;
348 }
349
350 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
351                                        char __user *buf, size_t count,
352                                        loff_t off)
353 {
354         unsigned int done = 0;
355
356         if (!reg->ioaddr) {
357                 reg->ioaddr =
358                         ioremap_nocache(reg->addr, reg->size);
359
360                 if (!reg->ioaddr)
361                         return -ENOMEM;
362         }
363
364         while (count) {
365                 size_t filled;
366
367                 if (count >= 4 && !(off % 4)) {
368                         u32 val;
369
370                         val = ioread32(reg->ioaddr + off);
371                         if (copy_to_user(buf, &val, 4))
372                                 goto err;
373
374                         filled = 4;
375                 } else if (count >= 2 && !(off % 2)) {
376                         u16 val;
377
378                         val = ioread16(reg->ioaddr + off);
379                         if (copy_to_user(buf, &val, 2))
380                                 goto err;
381
382                         filled = 2;
383                 } else {
384                         u8 val;
385
386                         val = ioread8(reg->ioaddr + off);
387                         if (copy_to_user(buf, &val, 1))
388                                 goto err;
389
390                         filled = 1;
391                 }
392
393
394                 count -= filled;
395                 done += filled;
396                 off += filled;
397                 buf += filled;
398         }
399
400         return done;
401 err:
402         return -EFAULT;
403 }
404
405 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
406                                   size_t count, loff_t *ppos)
407 {
408         struct vfio_platform_device *vdev = device_data;
409         unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
410         loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
411
412         if (index >= vdev->num_regions)
413                 return -EINVAL;
414
415         if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
416                 return -EINVAL;
417
418         if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
419                 return vfio_platform_read_mmio(&vdev->regions[index],
420                                                         buf, count, off);
421         else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
422                 return -EINVAL; /* not implemented */
423
424         return -EINVAL;
425 }
426
427 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
428                                         const char __user *buf, size_t count,
429                                         loff_t off)
430 {
431         unsigned int done = 0;
432
433         if (!reg->ioaddr) {
434                 reg->ioaddr =
435                         ioremap_nocache(reg->addr, reg->size);
436
437                 if (!reg->ioaddr)
438                         return -ENOMEM;
439         }
440
441         while (count) {
442                 size_t filled;
443
444                 if (count >= 4 && !(off % 4)) {
445                         u32 val;
446
447                         if (copy_from_user(&val, buf, 4))
448                                 goto err;
449                         iowrite32(val, reg->ioaddr + off);
450
451                         filled = 4;
452                 } else if (count >= 2 && !(off % 2)) {
453                         u16 val;
454
455                         if (copy_from_user(&val, buf, 2))
456                                 goto err;
457                         iowrite16(val, reg->ioaddr + off);
458
459                         filled = 2;
460                 } else {
461                         u8 val;
462
463                         if (copy_from_user(&val, buf, 1))
464                                 goto err;
465                         iowrite8(val, reg->ioaddr + off);
466
467                         filled = 1;
468                 }
469
470                 count -= filled;
471                 done += filled;
472                 off += filled;
473                 buf += filled;
474         }
475
476         return done;
477 err:
478         return -EFAULT;
479 }
480
481 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
482                                    size_t count, loff_t *ppos)
483 {
484         struct vfio_platform_device *vdev = device_data;
485         unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
486         loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
487
488         if (index >= vdev->num_regions)
489                 return -EINVAL;
490
491         if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
492                 return -EINVAL;
493
494         if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
495                 return vfio_platform_write_mmio(&vdev->regions[index],
496                                                         buf, count, off);
497         else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
498                 return -EINVAL; /* not implemented */
499
500         return -EINVAL;
501 }
502
503 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
504                                    struct vm_area_struct *vma)
505 {
506         u64 req_len, pgoff, req_start;
507
508         req_len = vma->vm_end - vma->vm_start;
509         pgoff = vma->vm_pgoff &
510                 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
511         req_start = pgoff << PAGE_SHIFT;
512
513         if (region.size < PAGE_SIZE || req_start + req_len > region.size)
514                 return -EINVAL;
515
516         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
517         vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
518
519         return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
520                                req_len, vma->vm_page_prot);
521 }
522
523 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
524 {
525         struct vfio_platform_device *vdev = device_data;
526         unsigned int index;
527
528         index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
529
530         if (vma->vm_end < vma->vm_start)
531                 return -EINVAL;
532         if (!(vma->vm_flags & VM_SHARED))
533                 return -EINVAL;
534         if (index >= vdev->num_regions)
535                 return -EINVAL;
536         if (vma->vm_start & ~PAGE_MASK)
537                 return -EINVAL;
538         if (vma->vm_end & ~PAGE_MASK)
539                 return -EINVAL;
540
541         if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
542                 return -EINVAL;
543
544         if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
545                         && (vma->vm_flags & VM_READ))
546                 return -EINVAL;
547
548         if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
549                         && (vma->vm_flags & VM_WRITE))
550                 return -EINVAL;
551
552         vma->vm_private_data = vdev;
553
554         if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
555                 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
556
557         else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
558                 return -EINVAL; /* not implemented */
559
560         return -EINVAL;
561 }
562
563 static const struct vfio_device_ops vfio_platform_ops = {
564         .name           = "vfio-platform",
565         .open           = vfio_platform_open,
566         .release        = vfio_platform_release,
567         .ioctl          = vfio_platform_ioctl,
568         .read           = vfio_platform_read,
569         .write          = vfio_platform_write,
570         .mmap           = vfio_platform_mmap,
571 };
572
573 int vfio_platform_of_probe(struct vfio_platform_device *vdev,
574                            struct device *dev)
575 {
576         int ret;
577
578         ret = device_property_read_string(dev, "compatible",
579                                           &vdev->compat);
580         if (ret)
581                 pr_err("VFIO: cannot retrieve compat for %s\n",
582                         vdev->name);
583
584         return ret;
585 }
586
587 /*
588  * There can be two kernel build combinations. One build where
589  * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
590  *
591  * In the first case, vfio_platform_acpi_probe will return since
592  * acpi_disabled is 1. DT user will not see any kind of messages from
593  * ACPI.
594  *
595  * In the second case, both DT and ACPI is compiled in but the system is
596  * booting with any of these combinations.
597  *
598  * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
599  * terminates immediately without any messages.
600  *
601  * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
602  * valid checks. We cannot claim that this system is DT.
603  */
604 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
605                                struct device *dev)
606 {
607         struct iommu_group *group;
608         int ret;
609
610         if (!vdev)
611                 return -EINVAL;
612
613         ret = vfio_platform_acpi_probe(vdev, dev);
614         if (ret)
615                 ret = vfio_platform_of_probe(vdev, dev);
616
617         if (ret)
618                 return ret;
619
620         vdev->device = dev;
621
622         group = vfio_iommu_group_get(dev);
623         if (!group) {
624                 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
625                 return -EINVAL;
626         }
627
628         ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
629         if (ret) {
630                 vfio_iommu_group_put(group, dev);
631                 return ret;
632         }
633
634         vfio_platform_get_reset(vdev);
635
636         mutex_init(&vdev->igate);
637
638         return 0;
639 }
640 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
641
642 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
643 {
644         struct vfio_platform_device *vdev;
645
646         vdev = vfio_del_group_dev(dev);
647
648         if (vdev) {
649                 vfio_platform_put_reset(vdev);
650                 vfio_iommu_group_put(dev->iommu_group, dev);
651         }
652
653         return vdev;
654 }
655 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
656
657 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
658 {
659         mutex_lock(&driver_lock);
660         list_add(&node->link, &reset_list);
661         mutex_unlock(&driver_lock);
662 }
663 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
664
665 void vfio_platform_unregister_reset(const char *compat,
666                                     vfio_platform_reset_fn_t fn)
667 {
668         struct vfio_platform_reset_node *iter, *temp;
669
670         mutex_lock(&driver_lock);
671         list_for_each_entry_safe(iter, temp, &reset_list, link) {
672                 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
673                         list_del(&iter->link);
674                         break;
675                 }
676         }
677
678         mutex_unlock(&driver_lock);
679
680 }
681 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
682
683 MODULE_VERSION(DRIVER_VERSION);
684 MODULE_LICENSE("GPL v2");
685 MODULE_AUTHOR(DRIVER_AUTHOR);
686 MODULE_DESCRIPTION(DRIVER_DESC);