2 * Copyright (C) 2013 - Virtual Open Systems
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/iommu.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/uaccess.h>
22 #include <linux/vfio.h>
24 #include "vfio_platform_private.h"
26 #define DRIVER_VERSION "0.10"
28 #define DRIVER_DESC "VFIO platform base module"
30 static LIST_HEAD(reset_list);
31 static DEFINE_MUTEX(driver_lock);
33 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
34 struct module **module)
36 struct vfio_platform_reset_node *iter;
37 vfio_platform_reset_fn_t reset_fn = NULL;
39 mutex_lock(&driver_lock);
40 list_for_each_entry(iter, &reset_list, link) {
41 if (!strcmp(iter->compat, compat) &&
42 try_module_get(iter->owner)) {
43 *module = iter->owner;
44 reset_fn = iter->reset;
48 mutex_unlock(&driver_lock);
52 static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
54 vdev->reset = vfio_platform_lookup_reset(vdev->compat,
57 request_module("vfio-reset:%s", vdev->compat);
58 vdev->reset = vfio_platform_lookup_reset(vdev->compat,
63 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
66 module_put(vdev->reset_module);
69 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
73 while (vdev->get_resource(vdev, cnt))
76 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
81 for (i = 0; i < cnt; i++) {
82 struct resource *res =
83 vdev->get_resource(vdev, i);
88 vdev->regions[i].addr = res->start;
89 vdev->regions[i].size = resource_size(res);
90 vdev->regions[i].flags = 0;
92 switch (resource_type(res)) {
94 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
95 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
96 if (!(res->flags & IORESOURCE_READONLY))
97 vdev->regions[i].flags |=
98 VFIO_REGION_INFO_FLAG_WRITE;
101 * Only regions addressed with PAGE granularity may be
104 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
105 !(vdev->regions[i].size & ~PAGE_MASK))
106 vdev->regions[i].flags |=
107 VFIO_REGION_INFO_FLAG_MMAP;
111 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
118 vdev->num_regions = cnt;
122 kfree(vdev->regions);
126 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
130 for (i = 0; i < vdev->num_regions; i++)
131 iounmap(vdev->regions[i].ioaddr);
133 vdev->num_regions = 0;
134 kfree(vdev->regions);
137 static void vfio_platform_release(void *device_data)
139 struct vfio_platform_device *vdev = device_data;
141 mutex_lock(&driver_lock);
143 if (!(--vdev->refcnt)) {
145 dev_info(vdev->device, "reset\n");
148 dev_warn(vdev->device, "no reset function found!\n");
150 vfio_platform_regions_cleanup(vdev);
151 vfio_platform_irq_cleanup(vdev);
154 mutex_unlock(&driver_lock);
156 module_put(vdev->parent_module);
159 static int vfio_platform_open(void *device_data)
161 struct vfio_platform_device *vdev = device_data;
164 if (!try_module_get(vdev->parent_module))
167 mutex_lock(&driver_lock);
170 ret = vfio_platform_regions_init(vdev);
174 ret = vfio_platform_irq_init(vdev);
179 dev_info(vdev->device, "reset\n");
182 dev_warn(vdev->device, "no reset function found!\n");
188 mutex_unlock(&driver_lock);
192 vfio_platform_regions_cleanup(vdev);
194 mutex_unlock(&driver_lock);
195 module_put(THIS_MODULE);
199 static long vfio_platform_ioctl(void *device_data,
200 unsigned int cmd, unsigned long arg)
202 struct vfio_platform_device *vdev = device_data;
205 if (cmd == VFIO_DEVICE_GET_INFO) {
206 struct vfio_device_info info;
208 minsz = offsetofend(struct vfio_device_info, num_irqs);
210 if (copy_from_user(&info, (void __user *)arg, minsz))
213 if (info.argsz < minsz)
217 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
218 info.flags = vdev->flags;
219 info.num_regions = vdev->num_regions;
220 info.num_irqs = vdev->num_irqs;
222 return copy_to_user((void __user *)arg, &info, minsz) ?
225 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
226 struct vfio_region_info info;
228 minsz = offsetofend(struct vfio_region_info, offset);
230 if (copy_from_user(&info, (void __user *)arg, minsz))
233 if (info.argsz < minsz)
236 if (info.index >= vdev->num_regions)
239 /* map offset to the physical address */
240 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
241 info.size = vdev->regions[info.index].size;
242 info.flags = vdev->regions[info.index].flags;
244 return copy_to_user((void __user *)arg, &info, minsz) ?
247 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
248 struct vfio_irq_info info;
250 minsz = offsetofend(struct vfio_irq_info, count);
252 if (copy_from_user(&info, (void __user *)arg, minsz))
255 if (info.argsz < minsz)
258 if (info.index >= vdev->num_irqs)
261 info.flags = vdev->irqs[info.index].flags;
262 info.count = vdev->irqs[info.index].count;
264 return copy_to_user((void __user *)arg, &info, minsz) ?
267 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
268 struct vfio_irq_set hdr;
272 minsz = offsetofend(struct vfio_irq_set, count);
274 if (copy_from_user(&hdr, (void __user *)arg, minsz))
277 if (hdr.argsz < minsz)
280 if (hdr.index >= vdev->num_irqs)
283 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
284 VFIO_IRQ_SET_ACTION_TYPE_MASK))
287 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
290 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
291 size = sizeof(uint8_t);
292 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
293 size = sizeof(int32_t);
297 if (hdr.argsz - minsz < size)
300 data = memdup_user((void __user *)(arg + minsz), size);
302 return PTR_ERR(data);
305 mutex_lock(&vdev->igate);
307 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
308 hdr.start, hdr.count, data);
309 mutex_unlock(&vdev->igate);
314 } else if (cmd == VFIO_DEVICE_RESET) {
316 return vdev->reset(vdev);
324 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
325 char __user *buf, size_t count,
328 unsigned int done = 0;
332 ioremap_nocache(reg->addr, reg->size);
341 if (count >= 4 && !(off % 4)) {
344 val = ioread32(reg->ioaddr + off);
345 if (copy_to_user(buf, &val, 4))
349 } else if (count >= 2 && !(off % 2)) {
352 val = ioread16(reg->ioaddr + off);
353 if (copy_to_user(buf, &val, 2))
360 val = ioread8(reg->ioaddr + off);
361 if (copy_to_user(buf, &val, 1))
379 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
380 size_t count, loff_t *ppos)
382 struct vfio_platform_device *vdev = device_data;
383 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
384 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
386 if (index >= vdev->num_regions)
389 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
392 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
393 return vfio_platform_read_mmio(&vdev->regions[index],
395 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
396 return -EINVAL; /* not implemented */
401 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
402 const 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 if (copy_from_user(&val, buf, 4))
423 iowrite32(val, reg->ioaddr + off);
426 } else if (count >= 2 && !(off % 2)) {
429 if (copy_from_user(&val, buf, 2))
431 iowrite16(val, reg->ioaddr + off);
437 if (copy_from_user(&val, buf, 1))
439 iowrite8(val, reg->ioaddr + off);
455 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
456 size_t count, loff_t *ppos)
458 struct vfio_platform_device *vdev = device_data;
459 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
460 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
462 if (index >= vdev->num_regions)
465 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
468 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
469 return vfio_platform_write_mmio(&vdev->regions[index],
471 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
472 return -EINVAL; /* not implemented */
477 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
478 struct vm_area_struct *vma)
480 u64 req_len, pgoff, req_start;
482 req_len = vma->vm_end - vma->vm_start;
483 pgoff = vma->vm_pgoff &
484 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
485 req_start = pgoff << PAGE_SHIFT;
487 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
490 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
491 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
493 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
494 req_len, vma->vm_page_prot);
497 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
499 struct vfio_platform_device *vdev = device_data;
502 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
504 if (vma->vm_end < vma->vm_start)
506 if (!(vma->vm_flags & VM_SHARED))
508 if (index >= vdev->num_regions)
510 if (vma->vm_start & ~PAGE_MASK)
512 if (vma->vm_end & ~PAGE_MASK)
515 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
518 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
519 && (vma->vm_flags & VM_READ))
522 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
523 && (vma->vm_flags & VM_WRITE))
526 vma->vm_private_data = vdev;
528 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
529 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
531 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
532 return -EINVAL; /* not implemented */
537 static const struct vfio_device_ops vfio_platform_ops = {
538 .name = "vfio-platform",
539 .open = vfio_platform_open,
540 .release = vfio_platform_release,
541 .ioctl = vfio_platform_ioctl,
542 .read = vfio_platform_read,
543 .write = vfio_platform_write,
544 .mmap = vfio_platform_mmap,
547 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
550 struct iommu_group *group;
556 ret = device_property_read_string(dev, "compatible", &vdev->compat);
558 pr_err("VFIO: cannot retrieve compat for %s\n", vdev->name);
564 group = iommu_group_get(dev);
566 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
570 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
572 iommu_group_put(group);
576 vfio_platform_get_reset(vdev);
578 mutex_init(&vdev->igate);
582 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
584 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
586 struct vfio_platform_device *vdev;
588 vdev = vfio_del_group_dev(dev);
591 vfio_platform_put_reset(vdev);
592 iommu_group_put(dev->iommu_group);
597 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
599 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
601 mutex_lock(&driver_lock);
602 list_add(&node->link, &reset_list);
603 mutex_unlock(&driver_lock);
605 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
607 void vfio_platform_unregister_reset(const char *compat,
608 vfio_platform_reset_fn_t fn)
610 struct vfio_platform_reset_node *iter, *temp;
612 mutex_lock(&driver_lock);
613 list_for_each_entry_safe(iter, temp, &reset_list, link) {
614 if (!strcmp(iter->compat, compat) && (iter->reset == fn)) {
615 list_del(&iter->link);
620 mutex_unlock(&driver_lock);
623 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
625 MODULE_VERSION(DRIVER_VERSION);
626 MODULE_LICENSE("GPL v2");
627 MODULE_AUTHOR(DRIVER_AUTHOR);
628 MODULE_DESCRIPTION(DRIVER_DESC);