perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / vfio / platform / vfio_platform_common.c
blobc0cd824be2b767bea76dbea2d8137125762836c2
1 /*
2 * Copyright (C) 2013 - Virtual Open Systems
3 * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
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.
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.
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/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 #include <linux/uaccess.h>
24 #include <linux/vfio.h>
26 #include "vfio_platform_private.h"
28 #define DRIVER_VERSION "0.10"
29 #define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
30 #define DRIVER_DESC "VFIO platform base module"
32 #define VFIO_PLATFORM_IS_ACPI(vdev) ((vdev)->acpihid != NULL)
34 static LIST_HEAD(reset_list);
35 static DEFINE_MUTEX(driver_lock);
37 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
38 struct module **module)
40 struct vfio_platform_reset_node *iter;
41 vfio_platform_reset_fn_t reset_fn = NULL;
43 mutex_lock(&driver_lock);
44 list_for_each_entry(iter, &reset_list, link) {
45 if (!strcmp(iter->compat, compat) &&
46 try_module_get(iter->owner)) {
47 *module = iter->owner;
48 reset_fn = iter->of_reset;
49 break;
52 mutex_unlock(&driver_lock);
53 return reset_fn;
56 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
57 struct device *dev)
59 struct acpi_device *adev;
61 if (acpi_disabled)
62 return -ENOENT;
64 adev = ACPI_COMPANION(dev);
65 if (!adev) {
66 pr_err("VFIO: ACPI companion device not found for %s\n",
67 vdev->name);
68 return -ENODEV;
71 #ifdef CONFIG_ACPI
72 vdev->acpihid = acpi_device_hid(adev);
73 #endif
74 return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
77 static int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
78 const char **extra_dbg)
80 #ifdef CONFIG_ACPI
81 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
82 struct device *dev = vdev->device;
83 acpi_handle handle = ACPI_HANDLE(dev);
84 acpi_status acpi_ret;
86 acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, &buffer);
87 if (ACPI_FAILURE(acpi_ret)) {
88 if (extra_dbg)
89 *extra_dbg = acpi_format_exception(acpi_ret);
90 return -EINVAL;
93 return 0;
94 #else
95 return -ENOENT;
96 #endif
99 static bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
101 #ifdef CONFIG_ACPI
102 struct device *dev = vdev->device;
103 acpi_handle handle = ACPI_HANDLE(dev);
105 return acpi_has_method(handle, "_RST");
106 #else
107 return false;
108 #endif
111 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
113 if (VFIO_PLATFORM_IS_ACPI(vdev))
114 return vfio_platform_acpi_has_reset(vdev);
116 return vdev->of_reset ? true : false;
119 static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
121 if (VFIO_PLATFORM_IS_ACPI(vdev))
122 return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
124 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
125 &vdev->reset_module);
126 if (!vdev->of_reset) {
127 request_module("vfio-reset:%s", vdev->compat);
128 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
129 &vdev->reset_module);
132 return vdev->of_reset ? 0 : -ENOENT;
135 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
137 if (VFIO_PLATFORM_IS_ACPI(vdev))
138 return;
140 if (vdev->of_reset)
141 module_put(vdev->reset_module);
144 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
146 int cnt = 0, i;
148 while (vdev->get_resource(vdev, cnt))
149 cnt++;
151 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
152 GFP_KERNEL);
153 if (!vdev->regions)
154 return -ENOMEM;
156 for (i = 0; i < cnt; i++) {
157 struct resource *res =
158 vdev->get_resource(vdev, i);
160 if (!res)
161 goto err;
163 vdev->regions[i].addr = res->start;
164 vdev->regions[i].size = resource_size(res);
165 vdev->regions[i].flags = 0;
167 switch (resource_type(res)) {
168 case IORESOURCE_MEM:
169 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
170 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
171 if (!(res->flags & IORESOURCE_READONLY))
172 vdev->regions[i].flags |=
173 VFIO_REGION_INFO_FLAG_WRITE;
176 * Only regions addressed with PAGE granularity may be
177 * MMAPed securely.
179 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
180 !(vdev->regions[i].size & ~PAGE_MASK))
181 vdev->regions[i].flags |=
182 VFIO_REGION_INFO_FLAG_MMAP;
184 break;
185 case IORESOURCE_IO:
186 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
187 break;
188 default:
189 goto err;
193 vdev->num_regions = cnt;
195 return 0;
196 err:
197 kfree(vdev->regions);
198 return -EINVAL;
201 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
203 int i;
205 for (i = 0; i < vdev->num_regions; i++)
206 iounmap(vdev->regions[i].ioaddr);
208 vdev->num_regions = 0;
209 kfree(vdev->regions);
212 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
213 const char **extra_dbg)
215 if (VFIO_PLATFORM_IS_ACPI(vdev)) {
216 dev_info(vdev->device, "reset\n");
217 return vfio_platform_acpi_call_reset(vdev, extra_dbg);
218 } else if (vdev->of_reset) {
219 dev_info(vdev->device, "reset\n");
220 return vdev->of_reset(vdev);
223 dev_warn(vdev->device, "no reset function found!\n");
224 return -EINVAL;
227 static void vfio_platform_release(void *device_data)
229 struct vfio_platform_device *vdev = device_data;
231 mutex_lock(&driver_lock);
233 if (!(--vdev->refcnt)) {
234 const char *extra_dbg = NULL;
235 int ret;
237 ret = vfio_platform_call_reset(vdev, &extra_dbg);
238 if (ret && vdev->reset_required) {
239 dev_warn(vdev->device, "reset driver is required and reset call failed in release (%d) %s\n",
240 ret, extra_dbg ? extra_dbg : "");
241 WARN_ON(1);
243 pm_runtime_put(vdev->device);
244 vfio_platform_regions_cleanup(vdev);
245 vfio_platform_irq_cleanup(vdev);
248 mutex_unlock(&driver_lock);
250 module_put(vdev->parent_module);
253 static int vfio_platform_open(void *device_data)
255 struct vfio_platform_device *vdev = device_data;
256 int ret;
258 if (!try_module_get(vdev->parent_module))
259 return -ENODEV;
261 mutex_lock(&driver_lock);
263 if (!vdev->refcnt) {
264 const char *extra_dbg = NULL;
266 ret = vfio_platform_regions_init(vdev);
267 if (ret)
268 goto err_reg;
270 ret = vfio_platform_irq_init(vdev);
271 if (ret)
272 goto err_irq;
274 ret = pm_runtime_get_sync(vdev->device);
275 if (ret < 0)
276 goto err_pm;
278 ret = vfio_platform_call_reset(vdev, &extra_dbg);
279 if (ret && vdev->reset_required) {
280 dev_warn(vdev->device, "reset driver is required and reset call failed in open (%d) %s\n",
281 ret, extra_dbg ? extra_dbg : "");
282 goto err_rst;
286 vdev->refcnt++;
288 mutex_unlock(&driver_lock);
289 return 0;
291 err_rst:
292 pm_runtime_put(vdev->device);
293 err_pm:
294 vfio_platform_irq_cleanup(vdev);
295 err_irq:
296 vfio_platform_regions_cleanup(vdev);
297 err_reg:
298 mutex_unlock(&driver_lock);
299 module_put(THIS_MODULE);
300 return ret;
303 static long vfio_platform_ioctl(void *device_data,
304 unsigned int cmd, unsigned long arg)
306 struct vfio_platform_device *vdev = device_data;
307 unsigned long minsz;
309 if (cmd == VFIO_DEVICE_GET_INFO) {
310 struct vfio_device_info info;
312 minsz = offsetofend(struct vfio_device_info, num_irqs);
314 if (copy_from_user(&info, (void __user *)arg, minsz))
315 return -EFAULT;
317 if (info.argsz < minsz)
318 return -EINVAL;
320 if (vfio_platform_has_reset(vdev))
321 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
322 info.flags = vdev->flags;
323 info.num_regions = vdev->num_regions;
324 info.num_irqs = vdev->num_irqs;
326 return copy_to_user((void __user *)arg, &info, minsz) ?
327 -EFAULT : 0;
329 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
330 struct vfio_region_info info;
332 minsz = offsetofend(struct vfio_region_info, offset);
334 if (copy_from_user(&info, (void __user *)arg, minsz))
335 return -EFAULT;
337 if (info.argsz < minsz)
338 return -EINVAL;
340 if (info.index >= vdev->num_regions)
341 return -EINVAL;
343 /* map offset to the physical address */
344 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
345 info.size = vdev->regions[info.index].size;
346 info.flags = vdev->regions[info.index].flags;
348 return copy_to_user((void __user *)arg, &info, minsz) ?
349 -EFAULT : 0;
351 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
352 struct vfio_irq_info info;
354 minsz = offsetofend(struct vfio_irq_info, count);
356 if (copy_from_user(&info, (void __user *)arg, minsz))
357 return -EFAULT;
359 if (info.argsz < minsz)
360 return -EINVAL;
362 if (info.index >= vdev->num_irqs)
363 return -EINVAL;
365 info.flags = vdev->irqs[info.index].flags;
366 info.count = vdev->irqs[info.index].count;
368 return copy_to_user((void __user *)arg, &info, minsz) ?
369 -EFAULT : 0;
371 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
372 struct vfio_irq_set hdr;
373 u8 *data = NULL;
374 int ret = 0;
375 size_t data_size = 0;
377 minsz = offsetofend(struct vfio_irq_set, count);
379 if (copy_from_user(&hdr, (void __user *)arg, minsz))
380 return -EFAULT;
382 ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
383 vdev->num_irqs, &data_size);
384 if (ret)
385 return ret;
387 if (data_size) {
388 data = memdup_user((void __user *)(arg + minsz),
389 data_size);
390 if (IS_ERR(data))
391 return PTR_ERR(data);
394 mutex_lock(&vdev->igate);
396 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
397 hdr.start, hdr.count, data);
398 mutex_unlock(&vdev->igate);
399 kfree(data);
401 return ret;
403 } else if (cmd == VFIO_DEVICE_RESET) {
404 return vfio_platform_call_reset(vdev, NULL);
407 return -ENOTTY;
410 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
411 char __user *buf, size_t count,
412 loff_t off)
414 unsigned int done = 0;
416 if (!reg->ioaddr) {
417 reg->ioaddr =
418 ioremap_nocache(reg->addr, reg->size);
420 if (!reg->ioaddr)
421 return -ENOMEM;
424 while (count) {
425 size_t filled;
427 if (count >= 4 && !(off % 4)) {
428 u32 val;
430 val = ioread32(reg->ioaddr + off);
431 if (copy_to_user(buf, &val, 4))
432 goto err;
434 filled = 4;
435 } else if (count >= 2 && !(off % 2)) {
436 u16 val;
438 val = ioread16(reg->ioaddr + off);
439 if (copy_to_user(buf, &val, 2))
440 goto err;
442 filled = 2;
443 } else {
444 u8 val;
446 val = ioread8(reg->ioaddr + off);
447 if (copy_to_user(buf, &val, 1))
448 goto err;
450 filled = 1;
454 count -= filled;
455 done += filled;
456 off += filled;
457 buf += filled;
460 return done;
461 err:
462 return -EFAULT;
465 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
466 size_t count, loff_t *ppos)
468 struct vfio_platform_device *vdev = device_data;
469 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
470 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
472 if (index >= vdev->num_regions)
473 return -EINVAL;
475 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
476 return -EINVAL;
478 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
479 return vfio_platform_read_mmio(&vdev->regions[index],
480 buf, count, off);
481 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
482 return -EINVAL; /* not implemented */
484 return -EINVAL;
487 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
488 const char __user *buf, size_t count,
489 loff_t off)
491 unsigned int done = 0;
493 if (!reg->ioaddr) {
494 reg->ioaddr =
495 ioremap_nocache(reg->addr, reg->size);
497 if (!reg->ioaddr)
498 return -ENOMEM;
501 while (count) {
502 size_t filled;
504 if (count >= 4 && !(off % 4)) {
505 u32 val;
507 if (copy_from_user(&val, buf, 4))
508 goto err;
509 iowrite32(val, reg->ioaddr + off);
511 filled = 4;
512 } else if (count >= 2 && !(off % 2)) {
513 u16 val;
515 if (copy_from_user(&val, buf, 2))
516 goto err;
517 iowrite16(val, reg->ioaddr + off);
519 filled = 2;
520 } else {
521 u8 val;
523 if (copy_from_user(&val, buf, 1))
524 goto err;
525 iowrite8(val, reg->ioaddr + off);
527 filled = 1;
530 count -= filled;
531 done += filled;
532 off += filled;
533 buf += filled;
536 return done;
537 err:
538 return -EFAULT;
541 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
542 size_t count, loff_t *ppos)
544 struct vfio_platform_device *vdev = device_data;
545 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
546 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
548 if (index >= vdev->num_regions)
549 return -EINVAL;
551 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
552 return -EINVAL;
554 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
555 return vfio_platform_write_mmio(&vdev->regions[index],
556 buf, count, off);
557 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
558 return -EINVAL; /* not implemented */
560 return -EINVAL;
563 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
564 struct vm_area_struct *vma)
566 u64 req_len, pgoff, req_start;
568 req_len = vma->vm_end - vma->vm_start;
569 pgoff = vma->vm_pgoff &
570 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
571 req_start = pgoff << PAGE_SHIFT;
573 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
574 return -EINVAL;
576 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
577 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
579 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
580 req_len, vma->vm_page_prot);
583 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
585 struct vfio_platform_device *vdev = device_data;
586 unsigned int index;
588 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
590 if (vma->vm_end < vma->vm_start)
591 return -EINVAL;
592 if (!(vma->vm_flags & VM_SHARED))
593 return -EINVAL;
594 if (index >= vdev->num_regions)
595 return -EINVAL;
596 if (vma->vm_start & ~PAGE_MASK)
597 return -EINVAL;
598 if (vma->vm_end & ~PAGE_MASK)
599 return -EINVAL;
601 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
602 return -EINVAL;
604 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
605 && (vma->vm_flags & VM_READ))
606 return -EINVAL;
608 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
609 && (vma->vm_flags & VM_WRITE))
610 return -EINVAL;
612 vma->vm_private_data = vdev;
614 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
615 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
617 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
618 return -EINVAL; /* not implemented */
620 return -EINVAL;
623 static const struct vfio_device_ops vfio_platform_ops = {
624 .name = "vfio-platform",
625 .open = vfio_platform_open,
626 .release = vfio_platform_release,
627 .ioctl = vfio_platform_ioctl,
628 .read = vfio_platform_read,
629 .write = vfio_platform_write,
630 .mmap = vfio_platform_mmap,
633 static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
634 struct device *dev)
636 int ret;
638 ret = device_property_read_string(dev, "compatible",
639 &vdev->compat);
640 if (ret)
641 pr_err("VFIO: Cannot retrieve compat for %s\n", vdev->name);
643 return ret;
647 * There can be two kernel build combinations. One build where
648 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
650 * In the first case, vfio_platform_acpi_probe will return since
651 * acpi_disabled is 1. DT user will not see any kind of messages from
652 * ACPI.
654 * In the second case, both DT and ACPI is compiled in but the system is
655 * booting with any of these combinations.
657 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
658 * terminates immediately without any messages.
660 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
661 * valid checks. We cannot claim that this system is DT.
663 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
664 struct device *dev)
666 struct iommu_group *group;
667 int ret;
669 if (!vdev)
670 return -EINVAL;
672 ret = vfio_platform_acpi_probe(vdev, dev);
673 if (ret)
674 ret = vfio_platform_of_probe(vdev, dev);
676 if (ret)
677 return ret;
679 vdev->device = dev;
681 ret = vfio_platform_get_reset(vdev);
682 if (ret && vdev->reset_required) {
683 pr_err("VFIO: No reset function found for device %s\n",
684 vdev->name);
685 return ret;
688 group = vfio_iommu_group_get(dev);
689 if (!group) {
690 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
691 ret = -EINVAL;
692 goto put_reset;
695 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
696 if (ret)
697 goto put_iommu;
699 mutex_init(&vdev->igate);
701 pm_runtime_enable(vdev->device);
702 return 0;
704 put_iommu:
705 vfio_iommu_group_put(group, dev);
706 put_reset:
707 vfio_platform_put_reset(vdev);
708 return ret;
710 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
712 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
714 struct vfio_platform_device *vdev;
716 vdev = vfio_del_group_dev(dev);
718 if (vdev) {
719 pm_runtime_disable(vdev->device);
720 vfio_platform_put_reset(vdev);
721 vfio_iommu_group_put(dev->iommu_group, dev);
724 return vdev;
726 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
728 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
730 mutex_lock(&driver_lock);
731 list_add(&node->link, &reset_list);
732 mutex_unlock(&driver_lock);
734 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
736 void vfio_platform_unregister_reset(const char *compat,
737 vfio_platform_reset_fn_t fn)
739 struct vfio_platform_reset_node *iter, *temp;
741 mutex_lock(&driver_lock);
742 list_for_each_entry_safe(iter, temp, &reset_list, link) {
743 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
744 list_del(&iter->link);
745 break;
749 mutex_unlock(&driver_lock);
752 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
754 MODULE_VERSION(DRIVER_VERSION);
755 MODULE_LICENSE("GPL v2");
756 MODULE_AUTHOR(DRIVER_AUTHOR);
757 MODULE_DESCRIPTION(DRIVER_DESC);