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 int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
77 const char **extra_dbg)
80 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81 struct device *dev = vdev->device;
82 acpi_handle handle = ACPI_HANDLE(dev);
85 acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, &buffer);
86 if (ACPI_FAILURE(acpi_ret)) {
88 *extra_dbg = acpi_format_exception(acpi_ret);
98 bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
101 struct device *dev = vdev->device;
102 acpi_handle handle = ACPI_HANDLE(dev);
104 return acpi_has_method(handle, "_RST");
110 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
112 if (VFIO_PLATFORM_IS_ACPI(vdev))
113 return vfio_platform_acpi_has_reset(vdev);
115 return vdev->of_reset ? true : false;
118 static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
120 if (VFIO_PLATFORM_IS_ACPI(vdev))
123 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
124 &vdev->reset_module);
125 if (!vdev->of_reset) {
126 request_module("vfio-reset:%s", vdev->compat);
127 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
128 &vdev->reset_module);
132 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
134 if (VFIO_PLATFORM_IS_ACPI(vdev))
138 module_put(vdev->reset_module);
141 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
145 while (vdev->get_resource(vdev, cnt))
148 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
153 for (i = 0; i < cnt; i++) {
154 struct resource *res =
155 vdev->get_resource(vdev, i);
160 vdev->regions[i].addr = res->start;
161 vdev->regions[i].size = resource_size(res);
162 vdev->regions[i].flags = 0;
164 switch (resource_type(res)) {
166 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
167 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
168 if (!(res->flags & IORESOURCE_READONLY))
169 vdev->regions[i].flags |=
170 VFIO_REGION_INFO_FLAG_WRITE;
173 * Only regions addressed with PAGE granularity may be
176 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
177 !(vdev->regions[i].size & ~PAGE_MASK))
178 vdev->regions[i].flags |=
179 VFIO_REGION_INFO_FLAG_MMAP;
183 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
190 vdev->num_regions = cnt;
194 kfree(vdev->regions);
198 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
202 for (i = 0; i < vdev->num_regions; i++)
203 iounmap(vdev->regions[i].ioaddr);
205 vdev->num_regions = 0;
206 kfree(vdev->regions);
209 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
210 const char **extra_dbg)
212 if (VFIO_PLATFORM_IS_ACPI(vdev)) {
213 dev_info(vdev->device, "reset\n");
214 return vfio_platform_acpi_call_reset(vdev, extra_dbg);
215 } else if (vdev->of_reset) {
216 dev_info(vdev->device, "reset\n");
217 return vdev->of_reset(vdev);
220 dev_warn(vdev->device, "no reset function found!\n");
224 static void vfio_platform_release(void *device_data)
226 struct vfio_platform_device *vdev = device_data;
228 mutex_lock(&driver_lock);
230 if (!(--vdev->refcnt)) {
231 vfio_platform_call_reset(vdev, NULL);
232 vfio_platform_regions_cleanup(vdev);
233 vfio_platform_irq_cleanup(vdev);
236 mutex_unlock(&driver_lock);
238 module_put(vdev->parent_module);
241 static int vfio_platform_open(void *device_data)
243 struct vfio_platform_device *vdev = device_data;
246 if (!try_module_get(vdev->parent_module))
249 mutex_lock(&driver_lock);
252 ret = vfio_platform_regions_init(vdev);
256 ret = vfio_platform_irq_init(vdev);
260 vfio_platform_call_reset(vdev, NULL);
265 mutex_unlock(&driver_lock);
269 vfio_platform_regions_cleanup(vdev);
271 mutex_unlock(&driver_lock);
272 module_put(THIS_MODULE);
276 static long vfio_platform_ioctl(void *device_data,
277 unsigned int cmd, unsigned long arg)
279 struct vfio_platform_device *vdev = device_data;
282 if (cmd == VFIO_DEVICE_GET_INFO) {
283 struct vfio_device_info info;
285 minsz = offsetofend(struct vfio_device_info, num_irqs);
287 if (copy_from_user(&info, (void __user *)arg, minsz))
290 if (info.argsz < minsz)
293 if (vfio_platform_has_reset(vdev))
294 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
295 info.flags = vdev->flags;
296 info.num_regions = vdev->num_regions;
297 info.num_irqs = vdev->num_irqs;
299 return copy_to_user((void __user *)arg, &info, minsz) ?
302 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
303 struct vfio_region_info info;
305 minsz = offsetofend(struct vfio_region_info, offset);
307 if (copy_from_user(&info, (void __user *)arg, minsz))
310 if (info.argsz < minsz)
313 if (info.index >= vdev->num_regions)
316 /* map offset to the physical address */
317 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
318 info.size = vdev->regions[info.index].size;
319 info.flags = vdev->regions[info.index].flags;
321 return copy_to_user((void __user *)arg, &info, minsz) ?
324 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
325 struct vfio_irq_info info;
327 minsz = offsetofend(struct vfio_irq_info, count);
329 if (copy_from_user(&info, (void __user *)arg, minsz))
332 if (info.argsz < minsz)
335 if (info.index >= vdev->num_irqs)
338 info.flags = vdev->irqs[info.index].flags;
339 info.count = vdev->irqs[info.index].count;
341 return copy_to_user((void __user *)arg, &info, minsz) ?
344 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
345 struct vfio_irq_set hdr;
349 minsz = offsetofend(struct vfio_irq_set, count);
351 if (copy_from_user(&hdr, (void __user *)arg, minsz))
354 if (hdr.argsz < minsz)
357 if (hdr.index >= vdev->num_irqs)
360 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
361 VFIO_IRQ_SET_ACTION_TYPE_MASK))
364 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
367 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
368 size = sizeof(uint8_t);
369 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
370 size = sizeof(int32_t);
374 if (hdr.argsz - minsz < size)
377 data = memdup_user((void __user *)(arg + minsz), size);
379 return PTR_ERR(data);
382 mutex_lock(&vdev->igate);
384 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
385 hdr.start, hdr.count, data);
386 mutex_unlock(&vdev->igate);
391 } else if (cmd == VFIO_DEVICE_RESET) {
392 return vfio_platform_call_reset(vdev, NULL);
398 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
399 char __user *buf, size_t count,
402 unsigned int done = 0;
406 ioremap_nocache(reg->addr, reg->size);
415 if (count >= 4 && !(off % 4)) {
418 val = ioread32(reg->ioaddr + off);
419 if (copy_to_user(buf, &val, 4))
423 } else if (count >= 2 && !(off % 2)) {
426 val = ioread16(reg->ioaddr + off);
427 if (copy_to_user(buf, &val, 2))
434 val = ioread8(reg->ioaddr + off);
435 if (copy_to_user(buf, &val, 1))
453 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
454 size_t count, loff_t *ppos)
456 struct vfio_platform_device *vdev = device_data;
457 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
458 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
460 if (index >= vdev->num_regions)
463 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
466 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
467 return vfio_platform_read_mmio(&vdev->regions[index],
469 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
470 return -EINVAL; /* not implemented */
475 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
476 const char __user *buf, size_t count,
479 unsigned int done = 0;
483 ioremap_nocache(reg->addr, reg->size);
492 if (count >= 4 && !(off % 4)) {
495 if (copy_from_user(&val, buf, 4))
497 iowrite32(val, reg->ioaddr + off);
500 } else if (count >= 2 && !(off % 2)) {
503 if (copy_from_user(&val, buf, 2))
505 iowrite16(val, reg->ioaddr + off);
511 if (copy_from_user(&val, buf, 1))
513 iowrite8(val, reg->ioaddr + off);
529 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
530 size_t count, loff_t *ppos)
532 struct vfio_platform_device *vdev = device_data;
533 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
534 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
536 if (index >= vdev->num_regions)
539 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
542 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
543 return vfio_platform_write_mmio(&vdev->regions[index],
545 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
546 return -EINVAL; /* not implemented */
551 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
552 struct vm_area_struct *vma)
554 u64 req_len, pgoff, req_start;
556 req_len = vma->vm_end - vma->vm_start;
557 pgoff = vma->vm_pgoff &
558 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
559 req_start = pgoff << PAGE_SHIFT;
561 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
564 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
565 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
567 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
568 req_len, vma->vm_page_prot);
571 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
573 struct vfio_platform_device *vdev = device_data;
576 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
578 if (vma->vm_end < vma->vm_start)
580 if (!(vma->vm_flags & VM_SHARED))
582 if (index >= vdev->num_regions)
584 if (vma->vm_start & ~PAGE_MASK)
586 if (vma->vm_end & ~PAGE_MASK)
589 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
592 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
593 && (vma->vm_flags & VM_READ))
596 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
597 && (vma->vm_flags & VM_WRITE))
600 vma->vm_private_data = vdev;
602 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
603 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
605 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
606 return -EINVAL; /* not implemented */
611 static const struct vfio_device_ops vfio_platform_ops = {
612 .name = "vfio-platform",
613 .open = vfio_platform_open,
614 .release = vfio_platform_release,
615 .ioctl = vfio_platform_ioctl,
616 .read = vfio_platform_read,
617 .write = vfio_platform_write,
618 .mmap = vfio_platform_mmap,
621 int vfio_platform_of_probe(struct vfio_platform_device *vdev,
626 ret = device_property_read_string(dev, "compatible",
629 pr_err("VFIO: cannot retrieve compat for %s\n",
636 * There can be two kernel build combinations. One build where
637 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
639 * In the first case, vfio_platform_acpi_probe will return since
640 * acpi_disabled is 1. DT user will not see any kind of messages from
643 * In the second case, both DT and ACPI is compiled in but the system is
644 * booting with any of these combinations.
646 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
647 * terminates immediately without any messages.
649 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
650 * valid checks. We cannot claim that this system is DT.
652 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
655 struct iommu_group *group;
661 ret = vfio_platform_acpi_probe(vdev, dev);
663 ret = vfio_platform_of_probe(vdev, dev);
670 group = vfio_iommu_group_get(dev);
672 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
676 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
678 vfio_iommu_group_put(group, dev);
682 vfio_platform_get_reset(vdev);
684 mutex_init(&vdev->igate);
688 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
690 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
692 struct vfio_platform_device *vdev;
694 vdev = vfio_del_group_dev(dev);
697 vfio_platform_put_reset(vdev);
698 vfio_iommu_group_put(dev->iommu_group, dev);
703 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
705 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
707 mutex_lock(&driver_lock);
708 list_add(&node->link, &reset_list);
709 mutex_unlock(&driver_lock);
711 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
713 void vfio_platform_unregister_reset(const char *compat,
714 vfio_platform_reset_fn_t fn)
716 struct vfio_platform_reset_node *iter, *temp;
718 mutex_lock(&driver_lock);
719 list_for_each_entry_safe(iter, temp, &reset_list, link) {
720 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
721 list_del(&iter->link);
726 mutex_unlock(&driver_lock);
729 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
731 MODULE_VERSION(DRIVER_VERSION);
732 MODULE_LICENSE("GPL v2");
733 MODULE_AUTHOR(DRIVER_AUTHOR);
734 MODULE_DESCRIPTION(DRIVER_DESC);