blob: 908d5101e0531d4ebcc993149c90be26ce4fa552 [file] [log] [blame]
Antonios Motakisde49fc02015-03-16 14:08:42 -06001/*
2 * Copyright (C) 2013 - Virtual Open Systems
3 * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
4 *
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.
8 *
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.
13 */
14
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>
Antonios Motakis2e8567b2015-03-16 14:08:46 -060021#include <linux/uaccess.h>
Antonios Motakisde49fc02015-03-16 14:08:42 -060022#include <linux/vfio.h>
23
24#include "vfio_platform_private.h"
25
Antonios Motakise8909e62015-03-16 14:08:46 -060026static DEFINE_MUTEX(driver_lock);
27
28static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
29{
30 int cnt = 0, i;
31
32 while (vdev->get_resource(vdev, cnt))
33 cnt++;
34
35 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
36 GFP_KERNEL);
37 if (!vdev->regions)
38 return -ENOMEM;
39
40 for (i = 0; i < cnt; i++) {
41 struct resource *res =
42 vdev->get_resource(vdev, i);
43
44 if (!res)
45 goto err;
46
47 vdev->regions[i].addr = res->start;
48 vdev->regions[i].size = resource_size(res);
49 vdev->regions[i].flags = 0;
50
51 switch (resource_type(res)) {
52 case IORESOURCE_MEM:
53 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
Antonios Motakis6e3f2642015-03-16 14:08:47 -060054 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
55 if (!(res->flags & IORESOURCE_READONLY))
56 vdev->regions[i].flags |=
57 VFIO_REGION_INFO_FLAG_WRITE;
Antonios Motakisfad4d5b2015-03-16 14:08:48 -060058
59 /*
60 * Only regions addressed with PAGE granularity may be
61 * MMAPed securely.
62 */
63 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
64 !(vdev->regions[i].size & ~PAGE_MASK))
65 vdev->regions[i].flags |=
66 VFIO_REGION_INFO_FLAG_MMAP;
67
Antonios Motakise8909e62015-03-16 14:08:46 -060068 break;
69 case IORESOURCE_IO:
70 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
71 break;
72 default:
73 goto err;
74 }
75 }
76
77 vdev->num_regions = cnt;
78
79 return 0;
80err:
81 kfree(vdev->regions);
82 return -EINVAL;
83}
84
85static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
86{
Antonios Motakis6e3f2642015-03-16 14:08:47 -060087 int i;
88
89 for (i = 0; i < vdev->num_regions; i++)
90 iounmap(vdev->regions[i].ioaddr);
91
Antonios Motakise8909e62015-03-16 14:08:46 -060092 vdev->num_regions = 0;
93 kfree(vdev->regions);
94}
95
Antonios Motakisde49fc02015-03-16 14:08:42 -060096static void vfio_platform_release(void *device_data)
97{
Antonios Motakise8909e62015-03-16 14:08:46 -060098 struct vfio_platform_device *vdev = device_data;
99
100 mutex_lock(&driver_lock);
101
102 if (!(--vdev->refcnt)) {
103 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600104 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600105 }
106
107 mutex_unlock(&driver_lock);
108
Antonios Motakisde49fc02015-03-16 14:08:42 -0600109 module_put(THIS_MODULE);
110}
111
112static int vfio_platform_open(void *device_data)
113{
Antonios Motakise8909e62015-03-16 14:08:46 -0600114 struct vfio_platform_device *vdev = device_data;
115 int ret;
116
Antonios Motakisde49fc02015-03-16 14:08:42 -0600117 if (!try_module_get(THIS_MODULE))
118 return -ENODEV;
119
Antonios Motakise8909e62015-03-16 14:08:46 -0600120 mutex_lock(&driver_lock);
121
122 if (!vdev->refcnt) {
123 ret = vfio_platform_regions_init(vdev);
124 if (ret)
125 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600126
127 ret = vfio_platform_irq_init(vdev);
128 if (ret)
129 goto err_irq;
Antonios Motakise8909e62015-03-16 14:08:46 -0600130 }
131
132 vdev->refcnt++;
133
134 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600135 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600136
Antonios Motakis682704c2015-03-16 14:08:48 -0600137err_irq:
138 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600139err_reg:
140 mutex_unlock(&driver_lock);
141 module_put(THIS_MODULE);
142 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600143}
144
145static long vfio_platform_ioctl(void *device_data,
146 unsigned int cmd, unsigned long arg)
147{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600148 struct vfio_platform_device *vdev = device_data;
149 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600150
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600151 if (cmd == VFIO_DEVICE_GET_INFO) {
152 struct vfio_device_info info;
153
154 minsz = offsetofend(struct vfio_device_info, num_irqs);
155
156 if (copy_from_user(&info, (void __user *)arg, minsz))
157 return -EFAULT;
158
159 if (info.argsz < minsz)
160 return -EINVAL;
161
162 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600163 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600164 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600165
166 return copy_to_user((void __user *)arg, &info, minsz);
167
Antonios Motakise8909e62015-03-16 14:08:46 -0600168 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
169 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600170
Antonios Motakise8909e62015-03-16 14:08:46 -0600171 minsz = offsetofend(struct vfio_region_info, offset);
172
173 if (copy_from_user(&info, (void __user *)arg, minsz))
174 return -EFAULT;
175
176 if (info.argsz < minsz)
177 return -EINVAL;
178
179 if (info.index >= vdev->num_regions)
180 return -EINVAL;
181
182 /* map offset to the physical address */
183 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
184 info.size = vdev->regions[info.index].size;
185 info.flags = vdev->regions[info.index].flags;
186
187 return copy_to_user((void __user *)arg, &info, minsz);
188
Antonios Motakis682704c2015-03-16 14:08:48 -0600189 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
190 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600191
Antonios Motakis682704c2015-03-16 14:08:48 -0600192 minsz = offsetofend(struct vfio_irq_info, count);
193
194 if (copy_from_user(&info, (void __user *)arg, minsz))
195 return -EFAULT;
196
197 if (info.argsz < minsz)
198 return -EINVAL;
199
200 if (info.index >= vdev->num_irqs)
201 return -EINVAL;
202
203 info.flags = vdev->irqs[info.index].flags;
204 info.count = vdev->irqs[info.index].count;
205
206 return copy_to_user((void __user *)arg, &info, minsz);
207
208 } else if (cmd == VFIO_DEVICE_SET_IRQS)
Antonios Motakisde49fc02015-03-16 14:08:42 -0600209 return -EINVAL;
210
211 else if (cmd == VFIO_DEVICE_RESET)
212 return -EINVAL;
213
214 return -ENOTTY;
215}
216
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600217static ssize_t vfio_platform_read_mmio(struct vfio_platform_region reg,
218 char __user *buf, size_t count,
219 loff_t off)
220{
221 unsigned int done = 0;
222
223 if (!reg.ioaddr) {
224 reg.ioaddr =
225 ioremap_nocache(reg.addr, reg.size);
226
227 if (!reg.ioaddr)
228 return -ENOMEM;
229 }
230
231 while (count) {
232 size_t filled;
233
234 if (count >= 4 && !(off % 4)) {
235 u32 val;
236
237 val = ioread32(reg.ioaddr + off);
238 if (copy_to_user(buf, &val, 4))
239 goto err;
240
241 filled = 4;
242 } else if (count >= 2 && !(off % 2)) {
243 u16 val;
244
245 val = ioread16(reg.ioaddr + off);
246 if (copy_to_user(buf, &val, 2))
247 goto err;
248
249 filled = 2;
250 } else {
251 u8 val;
252
253 val = ioread8(reg.ioaddr + off);
254 if (copy_to_user(buf, &val, 1))
255 goto err;
256
257 filled = 1;
258 }
259
260
261 count -= filled;
262 done += filled;
263 off += filled;
264 buf += filled;
265 }
266
267 return done;
268err:
269 return -EFAULT;
270}
271
Antonios Motakisde49fc02015-03-16 14:08:42 -0600272static ssize_t vfio_platform_read(void *device_data, char __user *buf,
273 size_t count, loff_t *ppos)
274{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600275 struct vfio_platform_device *vdev = device_data;
276 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
277 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
278
279 if (index >= vdev->num_regions)
280 return -EINVAL;
281
282 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
283 return -EINVAL;
284
285 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
286 return vfio_platform_read_mmio(vdev->regions[index],
287 buf, count, off);
288 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
289 return -EINVAL; /* not implemented */
290
Antonios Motakisde49fc02015-03-16 14:08:42 -0600291 return -EINVAL;
292}
293
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600294static ssize_t vfio_platform_write_mmio(struct vfio_platform_region reg,
295 const char __user *buf, size_t count,
296 loff_t off)
297{
298 unsigned int done = 0;
299
300 if (!reg.ioaddr) {
301 reg.ioaddr =
302 ioremap_nocache(reg.addr, reg.size);
303
304 if (!reg.ioaddr)
305 return -ENOMEM;
306 }
307
308 while (count) {
309 size_t filled;
310
311 if (count >= 4 && !(off % 4)) {
312 u32 val;
313
314 if (copy_from_user(&val, buf, 4))
315 goto err;
316 iowrite32(val, reg.ioaddr + off);
317
318 filled = 4;
319 } else if (count >= 2 && !(off % 2)) {
320 u16 val;
321
322 if (copy_from_user(&val, buf, 2))
323 goto err;
324 iowrite16(val, reg.ioaddr + off);
325
326 filled = 2;
327 } else {
328 u8 val;
329
330 if (copy_from_user(&val, buf, 1))
331 goto err;
332 iowrite8(val, reg.ioaddr + off);
333
334 filled = 1;
335 }
336
337 count -= filled;
338 done += filled;
339 off += filled;
340 buf += filled;
341 }
342
343 return done;
344err:
345 return -EFAULT;
346}
347
Antonios Motakisde49fc02015-03-16 14:08:42 -0600348static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
349 size_t count, loff_t *ppos)
350{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600351 struct vfio_platform_device *vdev = device_data;
352 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
353 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
354
355 if (index >= vdev->num_regions)
356 return -EINVAL;
357
358 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
359 return -EINVAL;
360
361 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
362 return vfio_platform_write_mmio(vdev->regions[index],
363 buf, count, off);
364 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
365 return -EINVAL; /* not implemented */
366
Antonios Motakisde49fc02015-03-16 14:08:42 -0600367 return -EINVAL;
368}
369
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600370static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
371 struct vm_area_struct *vma)
372{
373 u64 req_len, pgoff, req_start;
374
375 req_len = vma->vm_end - vma->vm_start;
376 pgoff = vma->vm_pgoff &
377 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
378 req_start = pgoff << PAGE_SHIFT;
379
380 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
381 return -EINVAL;
382
383 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
384 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
385
386 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
387 req_len, vma->vm_page_prot);
388}
389
Antonios Motakisde49fc02015-03-16 14:08:42 -0600390static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
391{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600392 struct vfio_platform_device *vdev = device_data;
393 unsigned int index;
394
395 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
396
397 if (vma->vm_end < vma->vm_start)
398 return -EINVAL;
399 if (!(vma->vm_flags & VM_SHARED))
400 return -EINVAL;
401 if (index >= vdev->num_regions)
402 return -EINVAL;
403 if (vma->vm_start & ~PAGE_MASK)
404 return -EINVAL;
405 if (vma->vm_end & ~PAGE_MASK)
406 return -EINVAL;
407
408 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
409 return -EINVAL;
410
411 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
412 && (vma->vm_flags & VM_READ))
413 return -EINVAL;
414
415 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
416 && (vma->vm_flags & VM_WRITE))
417 return -EINVAL;
418
419 vma->vm_private_data = vdev;
420
421 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
422 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
423
424 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
425 return -EINVAL; /* not implemented */
426
Antonios Motakisde49fc02015-03-16 14:08:42 -0600427 return -EINVAL;
428}
429
430static const struct vfio_device_ops vfio_platform_ops = {
431 .name = "vfio-platform",
432 .open = vfio_platform_open,
433 .release = vfio_platform_release,
434 .ioctl = vfio_platform_ioctl,
435 .read = vfio_platform_read,
436 .write = vfio_platform_write,
437 .mmap = vfio_platform_mmap,
438};
439
440int vfio_platform_probe_common(struct vfio_platform_device *vdev,
441 struct device *dev)
442{
443 struct iommu_group *group;
444 int ret;
445
446 if (!vdev)
447 return -EINVAL;
448
449 group = iommu_group_get(dev);
450 if (!group) {
451 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
452 return -EINVAL;
453 }
454
455 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
456 if (ret) {
457 iommu_group_put(group);
458 return ret;
459 }
460
461 return 0;
462}
463EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
464
465struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
466{
467 struct vfio_platform_device *vdev;
468
469 vdev = vfio_del_group_dev(dev);
470 if (vdev)
471 iommu_group_put(dev->iommu_group);
472
473 return vdev;
474}
475EXPORT_SYMBOL_GPL(vfio_platform_remove_common);