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 void vfio_platform_get_reset(struct vfio_platform_device *vdev,
35 int (*reset)(struct vfio_platform_device *);
38 ret = device_property_read_string(dev, "compatible", &compat);
42 for (i = 0 ; i < ARRAY_SIZE(reset_lookup_table); i++) {
43 if (!strcmp(reset_lookup_table[i].compat, compat)) {
44 request_module(reset_lookup_table[i].module_name);
46 reset_lookup_table[i].reset_function_name);
55 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
58 symbol_put_addr(vdev->reset);
61 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
65 while (vdev->get_resource(vdev, cnt))
68 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
73 for (i = 0; i < cnt; i++) {
74 struct resource *res =
75 vdev->get_resource(vdev, i);
80 vdev->regions[i].addr = res->start;
81 vdev->regions[i].size = resource_size(res);
82 vdev->regions[i].flags = 0;
84 switch (resource_type(res)) {
86 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
87 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
88 if (!(res->flags & IORESOURCE_READONLY))
89 vdev->regions[i].flags |=
90 VFIO_REGION_INFO_FLAG_WRITE;
93 * Only regions addressed with PAGE granularity may be
96 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
97 !(vdev->regions[i].size & ~PAGE_MASK))
98 vdev->regions[i].flags |=
99 VFIO_REGION_INFO_FLAG_MMAP;
103 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
110 vdev->num_regions = cnt;
114 kfree(vdev->regions);
118 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
122 for (i = 0; i < vdev->num_regions; i++)
123 iounmap(vdev->regions[i].ioaddr);
125 vdev->num_regions = 0;
126 kfree(vdev->regions);
129 static void vfio_platform_release(void *device_data)
131 struct vfio_platform_device *vdev = device_data;
133 mutex_lock(&driver_lock);
135 if (!(--vdev->refcnt)) {
138 vfio_platform_regions_cleanup(vdev);
139 vfio_platform_irq_cleanup(vdev);
142 mutex_unlock(&driver_lock);
144 module_put(THIS_MODULE);
147 static int vfio_platform_open(void *device_data)
149 struct vfio_platform_device *vdev = device_data;
152 if (!try_module_get(THIS_MODULE))
155 mutex_lock(&driver_lock);
158 ret = vfio_platform_regions_init(vdev);
162 ret = vfio_platform_irq_init(vdev);
172 mutex_unlock(&driver_lock);
176 vfio_platform_regions_cleanup(vdev);
178 mutex_unlock(&driver_lock);
179 module_put(THIS_MODULE);
183 static long vfio_platform_ioctl(void *device_data,
184 unsigned int cmd, unsigned long arg)
186 struct vfio_platform_device *vdev = device_data;
189 if (cmd == VFIO_DEVICE_GET_INFO) {
190 struct vfio_device_info info;
192 minsz = offsetofend(struct vfio_device_info, num_irqs);
194 if (copy_from_user(&info, (void __user *)arg, minsz))
197 if (info.argsz < minsz)
201 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
202 info.flags = vdev->flags;
203 info.num_regions = vdev->num_regions;
204 info.num_irqs = vdev->num_irqs;
206 return copy_to_user((void __user *)arg, &info, minsz);
208 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
209 struct vfio_region_info info;
211 minsz = offsetofend(struct vfio_region_info, offset);
213 if (copy_from_user(&info, (void __user *)arg, minsz))
216 if (info.argsz < minsz)
219 if (info.index >= vdev->num_regions)
222 /* map offset to the physical address */
223 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
224 info.size = vdev->regions[info.index].size;
225 info.flags = vdev->regions[info.index].flags;
227 return copy_to_user((void __user *)arg, &info, minsz);
229 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
230 struct vfio_irq_info info;
232 minsz = offsetofend(struct vfio_irq_info, count);
234 if (copy_from_user(&info, (void __user *)arg, minsz))
237 if (info.argsz < minsz)
240 if (info.index >= vdev->num_irqs)
243 info.flags = vdev->irqs[info.index].flags;
244 info.count = vdev->irqs[info.index].count;
246 return copy_to_user((void __user *)arg, &info, minsz);
248 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
249 struct vfio_irq_set hdr;
253 minsz = offsetofend(struct vfio_irq_set, count);
255 if (copy_from_user(&hdr, (void __user *)arg, minsz))
258 if (hdr.argsz < minsz)
261 if (hdr.index >= vdev->num_irqs)
264 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
265 VFIO_IRQ_SET_ACTION_TYPE_MASK))
268 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
271 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
272 size = sizeof(uint8_t);
273 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
274 size = sizeof(int32_t);
278 if (hdr.argsz - minsz < size)
281 data = memdup_user((void __user *)(arg + minsz), size);
283 return PTR_ERR(data);
286 mutex_lock(&vdev->igate);
288 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
289 hdr.start, hdr.count, data);
290 mutex_unlock(&vdev->igate);
295 } else if (cmd == VFIO_DEVICE_RESET) {
297 return vdev->reset(vdev);
305 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region reg,
306 char __user *buf, size_t count,
309 unsigned int done = 0;
313 ioremap_nocache(reg.addr, reg.size);
322 if (count >= 4 && !(off % 4)) {
325 val = ioread32(reg.ioaddr + off);
326 if (copy_to_user(buf, &val, 4))
330 } else if (count >= 2 && !(off % 2)) {
333 val = ioread16(reg.ioaddr + off);
334 if (copy_to_user(buf, &val, 2))
341 val = ioread8(reg.ioaddr + off);
342 if (copy_to_user(buf, &val, 1))
360 static ssize_t vfio_platform_read(void *device_data, char __user *buf,
361 size_t count, loff_t *ppos)
363 struct vfio_platform_device *vdev = device_data;
364 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
365 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
367 if (index >= vdev->num_regions)
370 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
373 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
374 return vfio_platform_read_mmio(vdev->regions[index],
376 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
377 return -EINVAL; /* not implemented */
382 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region reg,
383 const char __user *buf, size_t count,
386 unsigned int done = 0;
390 ioremap_nocache(reg.addr, reg.size);
399 if (count >= 4 && !(off % 4)) {
402 if (copy_from_user(&val, buf, 4))
404 iowrite32(val, reg.ioaddr + off);
407 } else if (count >= 2 && !(off % 2)) {
410 if (copy_from_user(&val, buf, 2))
412 iowrite16(val, reg.ioaddr + off);
418 if (copy_from_user(&val, buf, 1))
420 iowrite8(val, reg.ioaddr + off);
436 static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
437 size_t count, loff_t *ppos)
439 struct vfio_platform_device *vdev = device_data;
440 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
441 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
443 if (index >= vdev->num_regions)
446 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
449 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
450 return vfio_platform_write_mmio(vdev->regions[index],
452 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
453 return -EINVAL; /* not implemented */
458 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
459 struct vm_area_struct *vma)
461 u64 req_len, pgoff, req_start;
463 req_len = vma->vm_end - vma->vm_start;
464 pgoff = vma->vm_pgoff &
465 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
466 req_start = pgoff << PAGE_SHIFT;
468 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
471 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
472 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
474 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
475 req_len, vma->vm_page_prot);
478 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
480 struct vfio_platform_device *vdev = device_data;
483 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
485 if (vma->vm_end < vma->vm_start)
487 if (!(vma->vm_flags & VM_SHARED))
489 if (index >= vdev->num_regions)
491 if (vma->vm_start & ~PAGE_MASK)
493 if (vma->vm_end & ~PAGE_MASK)
496 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
499 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
500 && (vma->vm_flags & VM_READ))
503 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
504 && (vma->vm_flags & VM_WRITE))
507 vma->vm_private_data = vdev;
509 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
510 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
512 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
513 return -EINVAL; /* not implemented */
518 static const struct vfio_device_ops vfio_platform_ops = {
519 .name = "vfio-platform",
520 .open = vfio_platform_open,
521 .release = vfio_platform_release,
522 .ioctl = vfio_platform_ioctl,
523 .read = vfio_platform_read,
524 .write = vfio_platform_write,
525 .mmap = vfio_platform_mmap,
528 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
531 struct iommu_group *group;
537 group = iommu_group_get(dev);
539 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
543 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
545 iommu_group_put(group);
549 vfio_platform_get_reset(vdev, dev);
551 mutex_init(&vdev->igate);
555 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
557 struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
559 struct vfio_platform_device *vdev;
561 vdev = vfio_del_group_dev(dev);
564 vfio_platform_put_reset(vdev);
565 iommu_group_put(dev->iommu_group);
570 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);