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/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 static DEFINE_MUTEX(driver_lock);
28 static const struct vfio_platform_reset_combo reset_lookup_table[] = {
31 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
35 while (vdev->get_resource(vdev, cnt))
38 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
43 for (i = 0; i < cnt; i++) {
44 struct resource *res =
45 vdev->get_resource(vdev, i);
50 vdev->regions[i].addr = res->start;
51 vdev->regions[i].size = resource_size(res);
52 vdev->regions[i].flags = 0;
54 switch (resource_type(res)) {
56 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
57 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
58 if (!(res->flags & IORESOURCE_READONLY))
59 vdev->regions[i].flags |=
60 VFIO_REGION_INFO_FLAG_WRITE;
63 * Only regions addressed with PAGE granularity may be
66 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
67 !(vdev->regions[i].size & ~PAGE_MASK))
68 vdev->regions[i].flags |=
69 VFIO_REGION_INFO_FLAG_MMAP;
73 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
80 vdev->num_regions = cnt;
88 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
92 for (i = 0; i < vdev->num_regions; i++)
93 iounmap(vdev->regions[i].ioaddr);
95 vdev->num_regions = 0;
99 static void vfio_platform_release(void *device_data)
101 struct vfio_platform_device *vdev = device_data;
103 mutex_lock(&driver_lock);
105 if (!(--vdev->refcnt)) {
106 vfio_platform_regions_cleanup(vdev);
107 vfio_platform_irq_cleanup(vdev);
110 mutex_unlock(&driver_lock);
112 module_put(THIS_MODULE);
115 static int vfio_platform_open(void *device_data)
117 struct vfio_platform_device *vdev = device_data;
120 if (!try_module_get(THIS_MODULE))
123 mutex_lock(&driver_lock);
126 ret = vfio_platform_regions_init(vdev);
130 ret = vfio_platform_irq_init(vdev);
137 mutex_unlock(&driver_lock);
141 vfio_platform_regions_cleanup(vdev);
143 mutex_unlock(&driver_lock);
144 module_put(THIS_MODULE);
148 static long vfio_platform_ioctl(void *device_data,
149 unsigned int cmd, unsigned long arg)
151 struct vfio_platform_device *vdev = device_data;
154 if (cmd == VFIO_DEVICE_GET_INFO) {
155 struct vfio_device_info info;
157 minsz = offsetofend(struct vfio_device_info, num_irqs);
159 if (copy_from_user(&info, (void __user *)arg, minsz))
162 if (info.argsz < minsz)
165 info.flags = vdev->flags;
166 info.num_regions = vdev->num_regions;
167 info.num_irqs = vdev->num_irqs;
169 return copy_to_user((void __user *)arg, &info, minsz);
171 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
172 struct vfio_region_info info;
174 minsz = offsetofend(struct vfio_region_info, offset);
176 if (copy_from_user(&info, (void __user *)arg, minsz))
179 if (info.argsz < minsz)
182 if (info.index >= vdev->num_regions)
185 /* map offset to the physical address */
186 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
187 info.size = vdev->regions[info.index].size;
188 info.flags = vdev->regions[info.index].flags;
190 return copy_to_user((void __user *)arg, &info, minsz);
192 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
193 struct vfio_irq_info info;
195 minsz = offsetofend(struct vfio_irq_info, count);
197 if (copy_from_user(&info, (void __user *)arg, minsz))
200 if (info.argsz < minsz)
203 if (info.index >= vdev->num_irqs)
206 info.flags = vdev->irqs[info.index].flags;
207 info.count = vdev->irqs[info.index].count;
209 return copy_to_user((void __user *)arg, &info, minsz);
211 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
212 struct vfio_irq_set hdr;
216 minsz = offsetofend(struct vfio_irq_set, count);
218 if (copy_from_user(&hdr, (void __user *)arg, minsz))
221 if (hdr.argsz < minsz)
224 if (hdr.index >= vdev->num_irqs)
227 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
228 VFIO_IRQ_SET_ACTION_TYPE_MASK))
231 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
234 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
235 size = sizeof(uint8_t);
236 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
237 size = sizeof(int32_t);
241 if (hdr.argsz - minsz < size)
244 data = memdup_user((void __user *)(arg + minsz), size);
246 return PTR_ERR(data);
249 mutex_lock(&vdev->igate);
251 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
252 hdr.start, hdr.count, data);
253 mutex_unlock(&vdev->igate);
258 } else if (cmd == VFIO_DEVICE_RESET)
264 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region reg,
265 char __user *buf, size_t count,
268 unsigned int done = 0;
272 ioremap_nocache(reg.addr, reg.size);
281 if (count >= 4 && !(off % 4)) {
284 val = ioread32(reg.ioaddr + off);
285 if (copy_to_user(buf, &val, 4))
289 } else if (count >= 2 && !(off % 2)) {
292 val = ioread16(reg.ioaddr + off);
293 if (copy_to_user(buf, &val, 2))
300 val = ioread8(reg.ioaddr + off);
301 if (copy_to_user(buf, &val, 1))
319 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
320 size_t count, loff_t *ppos)
322 struct vfio_platform_device *vdev = device_data;
323 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
324 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
326 if (index >= vdev->num_regions)
329 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
332 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
333 return vfio_platform_read_mmio(vdev->regions[index],
335 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
336 return -EINVAL; /* not implemented */
341 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region reg,
342 const char __user *buf, size_t count,
345 unsigned int done = 0;
349 ioremap_nocache(reg.addr, reg.size);
358 if (count >= 4 && !(off % 4)) {
361 if (copy_from_user(&val, buf, 4))
363 iowrite32(val, reg.ioaddr + off);
366 } else if (count >= 2 && !(off % 2)) {
369 if (copy_from_user(&val, buf, 2))
371 iowrite16(val, reg.ioaddr + off);
377 if (copy_from_user(&val, buf, 1))
379 iowrite8(val, reg.ioaddr + off);
395 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
396 size_t count, loff_t *ppos)
398 struct vfio_platform_device *vdev = device_data;
399 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
400 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
402 if (index >= vdev->num_regions)
405 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
408 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
409 return vfio_platform_write_mmio(vdev->regions[index],
411 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
412 return -EINVAL; /* not implemented */
417 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
418 struct vm_area_struct *vma)
420 u64 req_len, pgoff, req_start;
422 req_len = vma->vm_end - vma->vm_start;
423 pgoff = vma->vm_pgoff &
424 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
425 req_start = pgoff << PAGE_SHIFT;
427 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
430 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
431 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
433 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
434 req_len, vma->vm_page_prot);
437 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
439 struct vfio_platform_device *vdev = device_data;
442 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
444 if (vma->vm_end < vma->vm_start)
446 if (!(vma->vm_flags & VM_SHARED))
448 if (index >= vdev->num_regions)
450 if (vma->vm_start & ~PAGE_MASK)
452 if (vma->vm_end & ~PAGE_MASK)
455 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
458 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
459 && (vma->vm_flags & VM_READ))
462 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
463 && (vma->vm_flags & VM_WRITE))
466 vma->vm_private_data = vdev;
468 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
469 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
471 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
472 return -EINVAL; /* not implemented */
477 static const struct vfio_device_ops vfio_platform_ops = {
478 .name = "vfio-platform",
479 .open = vfio_platform_open,
480 .release = vfio_platform_release,
481 .ioctl = vfio_platform_ioctl,
482 .read = vfio_platform_read,
483 .write = vfio_platform_write,
484 .mmap = vfio_platform_mmap,
487 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
490 struct iommu_group *group;
496 group = iommu_group_get(dev);
498 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
502 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
504 iommu_group_put(group);
508 mutex_init(&vdev->igate);
512 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
514 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
516 struct vfio_platform_device *vdev;
518 vdev = vfio_del_group_dev(dev);
520 iommu_group_put(dev->iommu_group);
524 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);