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/slab.h>
21 #include <linux/types.h>
22 #include <linux/uaccess.h>
23 #include <linux/vfio.h>
25 #include "vfio_platform_private.h"
27 #define DRIVER_VERSION "0.10"
28 #define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
29 #define DRIVER_DESC "VFIO platform base module"
31 #define VFIO_PLATFORM_IS_ACPI(vdev) ((vdev)->acpihid != NULL)
33 static LIST_HEAD(reset_list);
34 static DEFINE_MUTEX(driver_lock);
36 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
37 struct module **module)
39 struct vfio_platform_reset_node *iter;
40 vfio_platform_reset_fn_t reset_fn = NULL;
42 mutex_lock(&driver_lock);
43 list_for_each_entry(iter, &reset_list, link) {
44 if (!strcmp(iter->compat, compat) &&
45 try_module_get(iter->owner)) {
46 *module = iter->owner;
47 reset_fn = iter->of_reset;
51 mutex_unlock(&driver_lock);
55 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
58 struct acpi_device *adev;
63 adev = ACPI_COMPANION(dev);
65 pr_err("VFIO: ACPI companion device not found for %s\n",
71 vdev->acpihid = acpi_device_hid(adev);
73 return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
76 static int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
77 const char **extra_dbg)
80 struct device *dev = vdev->device;
81 acpi_handle handle = ACPI_HANDLE(dev);
84 acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, NULL);
85 if (ACPI_FAILURE(acpi_ret)) {
87 *extra_dbg = acpi_format_exception(acpi_ret);
97 static bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
100 struct device *dev = vdev->device;
101 acpi_handle handle = ACPI_HANDLE(dev);
103 return acpi_has_method(handle, "_RST");
109 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
111 if (VFIO_PLATFORM_IS_ACPI(vdev))
112 return vfio_platform_acpi_has_reset(vdev);
114 return vdev->of_reset ? true : false;
117 static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
119 if (VFIO_PLATFORM_IS_ACPI(vdev))
120 return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
122 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
123 &vdev->reset_module);
124 if (!vdev->of_reset) {
125 request_module("vfio-reset:%s", vdev->compat);
126 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
127 &vdev->reset_module);
130 return vdev->of_reset ? 0 : -ENOENT;
133 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
135 if (VFIO_PLATFORM_IS_ACPI(vdev))
139 module_put(vdev->reset_module);
142 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
146 while (vdev->get_resource(vdev, cnt))
149 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
154 for (i = 0; i < cnt; i++) {
155 struct resource *res =
156 vdev->get_resource(vdev, i);
161 vdev->regions[i].addr = res->start;
162 vdev->regions[i].size = resource_size(res);
163 vdev->regions[i].flags = 0;
165 switch (resource_type(res)) {
167 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
168 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
169 if (!(res->flags & IORESOURCE_READONLY))
170 vdev->regions[i].flags |=
171 VFIO_REGION_INFO_FLAG_WRITE;
174 * Only regions addressed with PAGE granularity may be
177 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
178 !(vdev->regions[i].size & ~PAGE_MASK))
179 vdev->regions[i].flags |=
180 VFIO_REGION_INFO_FLAG_MMAP;
184 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
191 vdev->num_regions = cnt;
195 kfree(vdev->regions);
199 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
203 for (i = 0; i < vdev->num_regions; i++)
204 iounmap(vdev->regions[i].ioaddr);
206 vdev->num_regions = 0;
207 kfree(vdev->regions);
210 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
211 const char **extra_dbg)
213 if (VFIO_PLATFORM_IS_ACPI(vdev)) {
214 dev_info(vdev->device, "reset\n");
215 return vfio_platform_acpi_call_reset(vdev, extra_dbg);
216 } else if (vdev->of_reset) {
217 dev_info(vdev->device, "reset\n");
218 return vdev->of_reset(vdev);
221 dev_warn(vdev->device, "no reset function found!\n");
225 static void vfio_platform_release(void *device_data)
227 struct vfio_platform_device *vdev = device_data;
229 mutex_lock(&driver_lock);
231 if (!(--vdev->refcnt)) {
232 const char *extra_dbg = NULL;
235 ret = vfio_platform_call_reset(vdev, &extra_dbg);
236 if (ret && vdev->reset_required) {
237 dev_warn(vdev->device, "reset driver is required and reset call failed in release (%d) %s\n",
238 ret, extra_dbg ? extra_dbg : "");
241 vfio_platform_regions_cleanup(vdev);
242 vfio_platform_irq_cleanup(vdev);
245 mutex_unlock(&driver_lock);
247 module_put(vdev->parent_module);
250 static int vfio_platform_open(void *device_data)
252 struct vfio_platform_device *vdev = device_data;
255 if (!try_module_get(vdev->parent_module))
258 mutex_lock(&driver_lock);
261 const char *extra_dbg = NULL;
263 ret = vfio_platform_regions_init(vdev);
267 ret = vfio_platform_irq_init(vdev);
271 ret = vfio_platform_call_reset(vdev, &extra_dbg);
272 if (ret && vdev->reset_required) {
273 dev_warn(vdev->device, "reset driver is required and reset call failed in open (%d) %s\n",
274 ret, extra_dbg ? extra_dbg : "");
281 mutex_unlock(&driver_lock);
285 vfio_platform_irq_cleanup(vdev);
287 vfio_platform_regions_cleanup(vdev);
289 mutex_unlock(&driver_lock);
290 module_put(vdev->parent_module);
294 static long vfio_platform_ioctl(void *device_data,
295 unsigned int cmd, unsigned long arg)
297 struct vfio_platform_device *vdev = device_data;
300 if (cmd == VFIO_DEVICE_GET_INFO) {
301 struct vfio_device_info info;
303 minsz = offsetofend(struct vfio_device_info, num_irqs);
305 if (copy_from_user(&info, (void __user *)arg, minsz))
308 if (info.argsz < minsz)
311 if (vfio_platform_has_reset(vdev))
312 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
313 info.flags = vdev->flags;
314 info.num_regions = vdev->num_regions;
315 info.num_irqs = vdev->num_irqs;
317 return copy_to_user((void __user *)arg, &info, minsz) ?
320 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
321 struct vfio_region_info info;
323 minsz = offsetofend(struct vfio_region_info, offset);
325 if (copy_from_user(&info, (void __user *)arg, minsz))
328 if (info.argsz < minsz)
331 if (info.index >= vdev->num_regions)
334 /* map offset to the physical address */
335 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
336 info.size = vdev->regions[info.index].size;
337 info.flags = vdev->regions[info.index].flags;
339 return copy_to_user((void __user *)arg, &info, minsz) ?
342 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
343 struct vfio_irq_info info;
345 minsz = offsetofend(struct vfio_irq_info, count);
347 if (copy_from_user(&info, (void __user *)arg, minsz))
350 if (info.argsz < minsz)
353 if (info.index >= vdev->num_irqs)
356 info.flags = vdev->irqs[info.index].flags;
357 info.count = vdev->irqs[info.index].count;
359 return copy_to_user((void __user *)arg, &info, minsz) ?
362 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
363 struct vfio_irq_set hdr;
366 size_t data_size = 0;
368 minsz = offsetofend(struct vfio_irq_set, count);
370 if (copy_from_user(&hdr, (void __user *)arg, minsz))
373 ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
374 vdev->num_irqs, &data_size);
379 data = memdup_user((void __user *)(arg + minsz),
382 return PTR_ERR(data);
385 mutex_lock(&vdev->igate);
387 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
388 hdr.start, hdr.count, data);
389 mutex_unlock(&vdev->igate);
394 } else if (cmd == VFIO_DEVICE_RESET) {
395 return vfio_platform_call_reset(vdev, NULL);
401 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
402 char __user *buf, size_t count,
405 unsigned int done = 0;
409 ioremap_nocache(reg->addr, reg->size);
418 if (count >= 4 && !(off % 4)) {
421 val = ioread32(reg->ioaddr + off);
422 if (copy_to_user(buf, &val, 4))
426 } else if (count >= 2 && !(off % 2)) {
429 val = ioread16(reg->ioaddr + off);
430 if (copy_to_user(buf, &val, 2))
437 val = ioread8(reg->ioaddr + off);
438 if (copy_to_user(buf, &val, 1))
456 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
457 size_t count, loff_t *ppos)
459 struct vfio_platform_device *vdev = device_data;
460 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
461 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
463 if (index >= vdev->num_regions)
466 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
469 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
470 return vfio_platform_read_mmio(&vdev->regions[index],
472 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
473 return -EINVAL; /* not implemented */
478 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
479 const char __user *buf, size_t count,
482 unsigned int done = 0;
486 ioremap_nocache(reg->addr, reg->size);
495 if (count >= 4 && !(off % 4)) {
498 if (copy_from_user(&val, buf, 4))
500 iowrite32(val, reg->ioaddr + off);
503 } else if (count >= 2 && !(off % 2)) {
506 if (copy_from_user(&val, buf, 2))
508 iowrite16(val, reg->ioaddr + off);
514 if (copy_from_user(&val, buf, 1))
516 iowrite8(val, reg->ioaddr + off);
532 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
533 size_t count, loff_t *ppos)
535 struct vfio_platform_device *vdev = device_data;
536 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
537 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
539 if (index >= vdev->num_regions)
542 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
545 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
546 return vfio_platform_write_mmio(&vdev->regions[index],
548 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
549 return -EINVAL; /* not implemented */
554 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
555 struct vm_area_struct *vma)
557 u64 req_len, pgoff, req_start;
559 req_len = vma->vm_end - vma->vm_start;
560 pgoff = vma->vm_pgoff &
561 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
562 req_start = pgoff << PAGE_SHIFT;
564 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
567 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
568 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
570 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
571 req_len, vma->vm_page_prot);
574 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
576 struct vfio_platform_device *vdev = device_data;
579 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
581 if (vma->vm_end < vma->vm_start)
583 if (!(vma->vm_flags & VM_SHARED))
585 if (index >= vdev->num_regions)
587 if (vma->vm_start & ~PAGE_MASK)
589 if (vma->vm_end & ~PAGE_MASK)
592 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
595 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
596 && (vma->vm_flags & VM_READ))
599 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
600 && (vma->vm_flags & VM_WRITE))
603 vma->vm_private_data = vdev;
605 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
606 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
608 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
609 return -EINVAL; /* not implemented */
614 static const struct vfio_device_ops vfio_platform_ops = {
615 .name = "vfio-platform",
616 .open = vfio_platform_open,
617 .release = vfio_platform_release,
618 .ioctl = vfio_platform_ioctl,
619 .read = vfio_platform_read,
620 .write = vfio_platform_write,
621 .mmap = vfio_platform_mmap,
624 static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
629 ret = device_property_read_string(dev, "compatible",
632 pr_err("VFIO: cannot retrieve compat for %s\n",
639 * There can be two kernel build combinations. One build where
640 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
642 * In the first case, vfio_platform_acpi_probe will return since
643 * acpi_disabled is 1. DT user will not see any kind of messages from
646 * In the second case, both DT and ACPI is compiled in but the system is
647 * booting with any of these combinations.
649 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
650 * terminates immediately without any messages.
652 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
653 * valid checks. We cannot claim that this system is DT.
655 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
658 struct iommu_group *group;
664 ret = vfio_platform_acpi_probe(vdev, dev);
666 ret = vfio_platform_of_probe(vdev, dev);
673 ret = vfio_platform_get_reset(vdev);
674 if (ret && vdev->reset_required) {
675 pr_err("vfio: no reset function found for device %s\n",
680 group = vfio_iommu_group_get(dev);
682 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
687 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
691 mutex_init(&vdev->igate);
696 vfio_iommu_group_put(group, dev);
698 vfio_platform_put_reset(vdev);
701 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
703 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
705 struct vfio_platform_device *vdev;
707 vdev = vfio_del_group_dev(dev);
710 vfio_platform_put_reset(vdev);
711 vfio_iommu_group_put(dev->iommu_group, dev);
716 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
718 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
720 mutex_lock(&driver_lock);
721 list_add(&node->link, &reset_list);
722 mutex_unlock(&driver_lock);
724 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
726 void vfio_platform_unregister_reset(const char *compat,
727 vfio_platform_reset_fn_t fn)
729 struct vfio_platform_reset_node *iter, *temp;
731 mutex_lock(&driver_lock);
732 list_for_each_entry_safe(iter, temp, &reset_list, link) {
733 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
734 list_del(&iter->link);
739 mutex_unlock(&driver_lock);
742 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
744 MODULE_VERSION(DRIVER_VERSION);
745 MODULE_LICENSE("GPL v2");
746 MODULE_AUTHOR(DRIVER_AUTHOR);
747 MODULE_DESCRIPTION(DRIVER_DESC);