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