blob: d7fe2c71e8bc75e60afbac453882df5a8120d313 [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);
104 }
105
106 mutex_unlock(&driver_lock);
107
Antonios Motakisde49fc02015-03-16 14:08:42 -0600108 module_put(THIS_MODULE);
109}
110
111static int vfio_platform_open(void *device_data)
112{
Antonios Motakise8909e62015-03-16 14:08:46 -0600113 struct vfio_platform_device *vdev = device_data;
114 int ret;
115
Antonios Motakisde49fc02015-03-16 14:08:42 -0600116 if (!try_module_get(THIS_MODULE))
117 return -ENODEV;
118
Antonios Motakise8909e62015-03-16 14:08:46 -0600119 mutex_lock(&driver_lock);
120
121 if (!vdev->refcnt) {
122 ret = vfio_platform_regions_init(vdev);
123 if (ret)
124 goto err_reg;
125 }
126
127 vdev->refcnt++;
128
129 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600130 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600131
132err_reg:
133 mutex_unlock(&driver_lock);
134 module_put(THIS_MODULE);
135 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600136}
137
138static long vfio_platform_ioctl(void *device_data,
139 unsigned int cmd, unsigned long arg)
140{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600141 struct vfio_platform_device *vdev = device_data;
142 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600143
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600144 if (cmd == VFIO_DEVICE_GET_INFO) {
145 struct vfio_device_info info;
146
147 minsz = offsetofend(struct vfio_device_info, num_irqs);
148
149 if (copy_from_user(&info, (void __user *)arg, minsz))
150 return -EFAULT;
151
152 if (info.argsz < minsz)
153 return -EINVAL;
154
155 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600156 info.num_regions = vdev->num_regions;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600157 info.num_irqs = 0;
158
159 return copy_to_user((void __user *)arg, &info, minsz);
160
Antonios Motakise8909e62015-03-16 14:08:46 -0600161 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
162 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600163
Antonios Motakise8909e62015-03-16 14:08:46 -0600164 minsz = offsetofend(struct vfio_region_info, offset);
165
166 if (copy_from_user(&info, (void __user *)arg, minsz))
167 return -EFAULT;
168
169 if (info.argsz < minsz)
170 return -EINVAL;
171
172 if (info.index >= vdev->num_regions)
173 return -EINVAL;
174
175 /* map offset to the physical address */
176 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
177 info.size = vdev->regions[info.index].size;
178 info.flags = vdev->regions[info.index].flags;
179
180 return copy_to_user((void __user *)arg, &info, minsz);
181
182 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO)
Antonios Motakisde49fc02015-03-16 14:08:42 -0600183 return -EINVAL;
184
185 else if (cmd == VFIO_DEVICE_SET_IRQS)
186 return -EINVAL;
187
188 else if (cmd == VFIO_DEVICE_RESET)
189 return -EINVAL;
190
191 return -ENOTTY;
192}
193
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600194static ssize_t vfio_platform_read_mmio(struct vfio_platform_region reg,
195 char __user *buf, size_t count,
196 loff_t off)
197{
198 unsigned int done = 0;
199
200 if (!reg.ioaddr) {
201 reg.ioaddr =
202 ioremap_nocache(reg.addr, reg.size);
203
204 if (!reg.ioaddr)
205 return -ENOMEM;
206 }
207
208 while (count) {
209 size_t filled;
210
211 if (count >= 4 && !(off % 4)) {
212 u32 val;
213
214 val = ioread32(reg.ioaddr + off);
215 if (copy_to_user(buf, &val, 4))
216 goto err;
217
218 filled = 4;
219 } else if (count >= 2 && !(off % 2)) {
220 u16 val;
221
222 val = ioread16(reg.ioaddr + off);
223 if (copy_to_user(buf, &val, 2))
224 goto err;
225
226 filled = 2;
227 } else {
228 u8 val;
229
230 val = ioread8(reg.ioaddr + off);
231 if (copy_to_user(buf, &val, 1))
232 goto err;
233
234 filled = 1;
235 }
236
237
238 count -= filled;
239 done += filled;
240 off += filled;
241 buf += filled;
242 }
243
244 return done;
245err:
246 return -EFAULT;
247}
248
Antonios Motakisde49fc02015-03-16 14:08:42 -0600249static ssize_t vfio_platform_read(void *device_data, char __user *buf,
250 size_t count, loff_t *ppos)
251{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600252 struct vfio_platform_device *vdev = device_data;
253 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
254 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
255
256 if (index >= vdev->num_regions)
257 return -EINVAL;
258
259 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
260 return -EINVAL;
261
262 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
263 return vfio_platform_read_mmio(vdev->regions[index],
264 buf, count, off);
265 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
266 return -EINVAL; /* not implemented */
267
Antonios Motakisde49fc02015-03-16 14:08:42 -0600268 return -EINVAL;
269}
270
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600271static ssize_t vfio_platform_write_mmio(struct vfio_platform_region reg,
272 const char __user *buf, size_t count,
273 loff_t off)
274{
275 unsigned int done = 0;
276
277 if (!reg.ioaddr) {
278 reg.ioaddr =
279 ioremap_nocache(reg.addr, reg.size);
280
281 if (!reg.ioaddr)
282 return -ENOMEM;
283 }
284
285 while (count) {
286 size_t filled;
287
288 if (count >= 4 && !(off % 4)) {
289 u32 val;
290
291 if (copy_from_user(&val, buf, 4))
292 goto err;
293 iowrite32(val, reg.ioaddr + off);
294
295 filled = 4;
296 } else if (count >= 2 && !(off % 2)) {
297 u16 val;
298
299 if (copy_from_user(&val, buf, 2))
300 goto err;
301 iowrite16(val, reg.ioaddr + off);
302
303 filled = 2;
304 } else {
305 u8 val;
306
307 if (copy_from_user(&val, buf, 1))
308 goto err;
309 iowrite8(val, reg.ioaddr + off);
310
311 filled = 1;
312 }
313
314 count -= filled;
315 done += filled;
316 off += filled;
317 buf += filled;
318 }
319
320 return done;
321err:
322 return -EFAULT;
323}
324
Antonios Motakisde49fc02015-03-16 14:08:42 -0600325static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
326 size_t count, loff_t *ppos)
327{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600328 struct vfio_platform_device *vdev = device_data;
329 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
330 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
331
332 if (index >= vdev->num_regions)
333 return -EINVAL;
334
335 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
336 return -EINVAL;
337
338 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
339 return vfio_platform_write_mmio(vdev->regions[index],
340 buf, count, off);
341 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
342 return -EINVAL; /* not implemented */
343
Antonios Motakisde49fc02015-03-16 14:08:42 -0600344 return -EINVAL;
345}
346
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600347static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
348 struct vm_area_struct *vma)
349{
350 u64 req_len, pgoff, req_start;
351
352 req_len = vma->vm_end - vma->vm_start;
353 pgoff = vma->vm_pgoff &
354 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
355 req_start = pgoff << PAGE_SHIFT;
356
357 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
358 return -EINVAL;
359
360 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
361 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
362
363 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
364 req_len, vma->vm_page_prot);
365}
366
Antonios Motakisde49fc02015-03-16 14:08:42 -0600367static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
368{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600369 struct vfio_platform_device *vdev = device_data;
370 unsigned int index;
371
372 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
373
374 if (vma->vm_end < vma->vm_start)
375 return -EINVAL;
376 if (!(vma->vm_flags & VM_SHARED))
377 return -EINVAL;
378 if (index >= vdev->num_regions)
379 return -EINVAL;
380 if (vma->vm_start & ~PAGE_MASK)
381 return -EINVAL;
382 if (vma->vm_end & ~PAGE_MASK)
383 return -EINVAL;
384
385 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
386 return -EINVAL;
387
388 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
389 && (vma->vm_flags & VM_READ))
390 return -EINVAL;
391
392 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
393 && (vma->vm_flags & VM_WRITE))
394 return -EINVAL;
395
396 vma->vm_private_data = vdev;
397
398 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
399 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
400
401 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
402 return -EINVAL; /* not implemented */
403
Antonios Motakisde49fc02015-03-16 14:08:42 -0600404 return -EINVAL;
405}
406
407static const struct vfio_device_ops vfio_platform_ops = {
408 .name = "vfio-platform",
409 .open = vfio_platform_open,
410 .release = vfio_platform_release,
411 .ioctl = vfio_platform_ioctl,
412 .read = vfio_platform_read,
413 .write = vfio_platform_write,
414 .mmap = vfio_platform_mmap,
415};
416
417int vfio_platform_probe_common(struct vfio_platform_device *vdev,
418 struct device *dev)
419{
420 struct iommu_group *group;
421 int ret;
422
423 if (!vdev)
424 return -EINVAL;
425
426 group = iommu_group_get(dev);
427 if (!group) {
428 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
429 return -EINVAL;
430 }
431
432 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
433 if (ret) {
434 iommu_group_put(group);
435 return ret;
436 }
437
438 return 0;
439}
440EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
441
442struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
443{
444 struct vfio_platform_device *vdev;
445
446 vdev = vfio_del_group_dev(dev);
447 if (vdev)
448 iommu_group_put(dev->iommu_group);
449
450 return vdev;
451}
452EXPORT_SYMBOL_GPL(vfio_platform_remove_common);