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 static LIST_HEAD(reset_list);
32 static DEFINE_MUTEX(driver_lock);
34 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
35 struct module **module)
37 struct vfio_platform_reset_node *iter;
38 vfio_platform_reset_fn_t reset_fn = NULL;
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;
49 mutex_unlock(&driver_lock);
53 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
56 struct acpi_device *adev;
61 adev = ACPI_COMPANION(dev);
63 pr_err("VFIO: ACPI companion device not found for %s\n",
69 vdev->acpihid = acpi_device_hid(adev);
71 return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
74 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
76 return vdev->of_reset ? true : false;
79 static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
81 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
83 if (!vdev->of_reset) {
84 request_module("vfio-reset:%s", vdev->compat);
85 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
90 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
93 module_put(vdev->reset_module);
96 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
100 while (vdev->get_resource(vdev, cnt))
103 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
108 for (i = 0; i < cnt; i++) {
109 struct resource *res =
110 vdev->get_resource(vdev, i);
115 vdev->regions[i].addr = res->start;
116 vdev->regions[i].size = resource_size(res);
117 vdev->regions[i].flags = 0;
119 switch (resource_type(res)) {
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;
128 * Only regions addressed with PAGE granularity may be
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;
138 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
145 vdev->num_regions = cnt;
149 kfree(vdev->regions);
153 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
157 for (i = 0; i < vdev->num_regions; i++)
158 iounmap(vdev->regions[i].ioaddr);
160 vdev->num_regions = 0;
161 kfree(vdev->regions);
164 static int vfio_platform_call_reset(struct vfio_platform_device *vdev)
166 if (vdev->of_reset) {
167 dev_info(vdev->device, "reset\n");
168 return vdev->of_reset(vdev);
171 dev_warn(vdev->device, "no reset function found!\n");
175 static void vfio_platform_release(void *device_data)
177 struct vfio_platform_device *vdev = device_data;
179 mutex_lock(&driver_lock);
181 if (!(--vdev->refcnt)) {
182 vfio_platform_call_reset(vdev);
183 vfio_platform_regions_cleanup(vdev);
184 vfio_platform_irq_cleanup(vdev);
187 mutex_unlock(&driver_lock);
189 module_put(vdev->parent_module);
192 static int vfio_platform_open(void *device_data)
194 struct vfio_platform_device *vdev = device_data;
197 if (!try_module_get(vdev->parent_module))
200 mutex_lock(&driver_lock);
203 ret = vfio_platform_regions_init(vdev);
207 ret = vfio_platform_irq_init(vdev);
211 vfio_platform_call_reset(vdev);
216 mutex_unlock(&driver_lock);
220 vfio_platform_regions_cleanup(vdev);
222 mutex_unlock(&driver_lock);
223 module_put(THIS_MODULE);
227 static long vfio_platform_ioctl(void *device_data,
228 unsigned int cmd, unsigned long arg)
230 struct vfio_platform_device *vdev = device_data;
233 if (cmd == VFIO_DEVICE_GET_INFO) {
234 struct vfio_device_info info;
236 minsz = offsetofend(struct vfio_device_info, num_irqs);
238 if (copy_from_user(&info, (void __user *)arg, minsz))
241 if (info.argsz < minsz)
244 if (vfio_platform_has_reset(vdev))
245 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
246 info.flags = vdev->flags;
247 info.num_regions = vdev->num_regions;
248 info.num_irqs = vdev->num_irqs;
250 return copy_to_user((void __user *)arg, &info, minsz) ?
253 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
254 struct vfio_region_info info;
256 minsz = offsetofend(struct vfio_region_info, offset);
258 if (copy_from_user(&info, (void __user *)arg, minsz))
261 if (info.argsz < minsz)
264 if (info.index >= vdev->num_regions)
267 /* map offset to the physical address */
268 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
269 info.size = vdev->regions[info.index].size;
270 info.flags = vdev->regions[info.index].flags;
272 return copy_to_user((void __user *)arg, &info, minsz) ?
275 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
276 struct vfio_irq_info info;
278 minsz = offsetofend(struct vfio_irq_info, count);
280 if (copy_from_user(&info, (void __user *)arg, minsz))
283 if (info.argsz < minsz)
286 if (info.index >= vdev->num_irqs)
289 info.flags = vdev->irqs[info.index].flags;
290 info.count = vdev->irqs[info.index].count;
292 return copy_to_user((void __user *)arg, &info, minsz) ?
295 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
296 struct vfio_irq_set hdr;
300 minsz = offsetofend(struct vfio_irq_set, count);
302 if (copy_from_user(&hdr, (void __user *)arg, minsz))
305 if (hdr.argsz < minsz)
308 if (hdr.index >= vdev->num_irqs)
311 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
312 VFIO_IRQ_SET_ACTION_TYPE_MASK))
315 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
318 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
319 size = sizeof(uint8_t);
320 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
321 size = sizeof(int32_t);
325 if (hdr.argsz - minsz < size)
328 data = memdup_user((void __user *)(arg + minsz), size);
330 return PTR_ERR(data);
333 mutex_lock(&vdev->igate);
335 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
336 hdr.start, hdr.count, data);
337 mutex_unlock(&vdev->igate);
342 } else if (cmd == VFIO_DEVICE_RESET) {
343 return vfio_platform_call_reset(vdev);
349 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
350 char __user *buf, size_t count,
353 unsigned int done = 0;
357 ioremap_nocache(reg->addr, reg->size);
366 if (count >= 4 && !(off % 4)) {
369 val = ioread32(reg->ioaddr + off);
370 if (copy_to_user(buf, &val, 4))
374 } else if (count >= 2 && !(off % 2)) {
377 val = ioread16(reg->ioaddr + off);
378 if (copy_to_user(buf, &val, 2))
385 val = ioread8(reg->ioaddr + off);
386 if (copy_to_user(buf, &val, 1))
404 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
405 size_t count, loff_t *ppos)
407 struct vfio_platform_device *vdev = device_data;
408 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
409 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
411 if (index >= vdev->num_regions)
414 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
417 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
418 return vfio_platform_read_mmio(&vdev->regions[index],
420 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
421 return -EINVAL; /* not implemented */
426 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
427 const char __user *buf, size_t count,
430 unsigned int done = 0;
434 ioremap_nocache(reg->addr, reg->size);
443 if (count >= 4 && !(off % 4)) {
446 if (copy_from_user(&val, buf, 4))
448 iowrite32(val, reg->ioaddr + off);
451 } else if (count >= 2 && !(off % 2)) {
454 if (copy_from_user(&val, buf, 2))
456 iowrite16(val, reg->ioaddr + off);
462 if (copy_from_user(&val, buf, 1))
464 iowrite8(val, reg->ioaddr + off);
480 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
481 size_t count, loff_t *ppos)
483 struct vfio_platform_device *vdev = device_data;
484 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
485 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
487 if (index >= vdev->num_regions)
490 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
493 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
494 return vfio_platform_write_mmio(&vdev->regions[index],
496 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
497 return -EINVAL; /* not implemented */
502 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
503 struct vm_area_struct *vma)
505 u64 req_len, pgoff, req_start;
507 req_len = vma->vm_end - vma->vm_start;
508 pgoff = vma->vm_pgoff &
509 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
510 req_start = pgoff << PAGE_SHIFT;
512 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
515 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
516 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
518 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
519 req_len, vma->vm_page_prot);
522 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
524 struct vfio_platform_device *vdev = device_data;
527 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
529 if (vma->vm_end < vma->vm_start)
531 if (!(vma->vm_flags & VM_SHARED))
533 if (index >= vdev->num_regions)
535 if (vma->vm_start & ~PAGE_MASK)
537 if (vma->vm_end & ~PAGE_MASK)
540 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
543 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
544 && (vma->vm_flags & VM_READ))
547 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
548 && (vma->vm_flags & VM_WRITE))
551 vma->vm_private_data = vdev;
553 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
554 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
556 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
557 return -EINVAL; /* not implemented */
562 static const struct vfio_device_ops vfio_platform_ops = {
563 .name = "vfio-platform",
564 .open = vfio_platform_open,
565 .release = vfio_platform_release,
566 .ioctl = vfio_platform_ioctl,
567 .read = vfio_platform_read,
568 .write = vfio_platform_write,
569 .mmap = vfio_platform_mmap,
572 int vfio_platform_of_probe(struct vfio_platform_device *vdev,
577 ret = device_property_read_string(dev, "compatible",
580 pr_err("VFIO: cannot retrieve compat for %s\n",
587 * There can be two kernel build combinations. One build where
588 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
590 * In the first case, vfio_platform_acpi_probe will return since
591 * acpi_disabled is 1. DT user will not see any kind of messages from
594 * In the second case, both DT and ACPI is compiled in but the system is
595 * booting with any of these combinations.
597 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
598 * terminates immediately without any messages.
600 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
601 * valid checks. We cannot claim that this system is DT.
603 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
606 struct iommu_group *group;
612 ret = vfio_platform_acpi_probe(vdev, dev);
614 ret = vfio_platform_of_probe(vdev, dev);
621 group = vfio_iommu_group_get(dev);
623 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
627 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
629 vfio_iommu_group_put(group, dev);
633 vfio_platform_get_reset(vdev);
635 mutex_init(&vdev->igate);
639 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
641 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
643 struct vfio_platform_device *vdev;
645 vdev = vfio_del_group_dev(dev);
648 vfio_platform_put_reset(vdev);
649 vfio_iommu_group_put(dev->iommu_group, dev);
654 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
656 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
658 mutex_lock(&driver_lock);
659 list_add(&node->link, &reset_list);
660 mutex_unlock(&driver_lock);
662 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
664 void vfio_platform_unregister_reset(const char *compat,
665 vfio_platform_reset_fn_t fn)
667 struct vfio_platform_reset_node *iter, *temp;
669 mutex_lock(&driver_lock);
670 list_for_each_entry_safe(iter, temp, &reset_list, link) {
671 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
672 list_del(&iter->link);
677 mutex_unlock(&driver_lock);
680 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
682 MODULE_VERSION(DRIVER_VERSION);
683 MODULE_LICENSE("GPL v2");
684 MODULE_AUTHOR(DRIVER_AUTHOR);
685 MODULE_DESCRIPTION(DRIVER_DESC);