blob: a9760c2e68ad4eda324e61130c797ad60ceb5475 [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>
Sinan Kayaa12a9362016-07-19 09:01:44 -040016#include <linux/acpi.h>
Antonios Motakisde49fc02015-03-16 14:08:42 -060017#include <linux/iommu.h>
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/slab.h>
21#include <linux/types.h>
Antonios Motakis2e8567b2015-03-16 14:08:46 -060022#include <linux/uaccess.h>
Antonios Motakisde49fc02015-03-16 14:08:42 -060023#include <linux/vfio.h>
24
25#include "vfio_platform_private.h"
26
Eric Auger32a2d712015-11-03 18:12:12 +000027#define DRIVER_VERSION "0.10"
28#define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
29#define DRIVER_DESC "VFIO platform base module"
30
Eric Augere0864972015-11-03 18:12:13 +000031static LIST_HEAD(reset_list);
Antonios Motakise8909e62015-03-16 14:08:46 -060032static DEFINE_MUTEX(driver_lock);
33
Eric Augere9e05062015-11-03 18:12:17 +000034static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
35 struct module **module)
Eric Auger3eeb0d52015-06-15 11:09:44 +020036{
Eric Augere9e05062015-11-03 18:12:17 +000037 struct vfio_platform_reset_node *iter;
38 vfio_platform_reset_fn_t reset_fn = NULL;
Eric Auger3eeb0d52015-06-15 11:09:44 +020039
Eric Augere9e05062015-11-03 18:12:17 +000040 mutex_lock(&driver_lock);
41 list_for_each_entry(iter, &reset_list, link) {
42 if (!strcmp(iter->compat, compat) &&
43 try_module_get(iter->owner)) {
44 *module = iter->owner;
Sinan Kaya7aef80c2016-07-19 09:01:41 -040045 reset_fn = iter->of_reset;
Eric Augere9e05062015-11-03 18:12:17 +000046 break;
Eric Auger3eeb0d52015-06-15 11:09:44 +020047 }
48 }
Eric Augere9e05062015-11-03 18:12:17 +000049 mutex_unlock(&driver_lock);
50 return reset_fn;
51}
52
Sinan Kayaa12a9362016-07-19 09:01:44 -040053static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
54 struct device *dev)
55{
56 struct acpi_device *adev;
57
58 if (acpi_disabled)
59 return -ENOENT;
60
61 adev = ACPI_COMPANION(dev);
62 if (!adev) {
63 pr_err("VFIO: ACPI companion device not found for %s\n",
64 vdev->name);
65 return -ENODEV;
66 }
67
68#ifdef CONFIG_ACPI
69 vdev->acpihid = acpi_device_hid(adev);
70#endif
71 return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
72}
73
Sinan Kayadc5542f2016-07-19 09:01:43 -040074static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
75{
76 return vdev->of_reset ? true : false;
77}
78
Eric Augere9e05062015-11-03 18:12:17 +000079static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
80{
Sinan Kaya7aef80c2016-07-19 09:01:41 -040081 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
82 &vdev->reset_module);
83 if (!vdev->of_reset) {
Kees Cook7200be72015-11-19 16:17:24 -080084 request_module("vfio-reset:%s", vdev->compat);
Sinan Kaya7aef80c2016-07-19 09:01:41 -040085 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
86 &vdev->reset_module);
Eric Augere9e05062015-11-03 18:12:17 +000087 }
Eric Auger3eeb0d52015-06-15 11:09:44 +020088}
89
90static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
91{
Sinan Kaya7aef80c2016-07-19 09:01:41 -040092 if (vdev->of_reset)
Eric Augere9e05062015-11-03 18:12:17 +000093 module_put(vdev->reset_module);
Eric Auger3eeb0d52015-06-15 11:09:44 +020094}
95
Antonios Motakise8909e62015-03-16 14:08:46 -060096static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
97{
98 int cnt = 0, i;
99
100 while (vdev->get_resource(vdev, cnt))
101 cnt++;
102
103 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
104 GFP_KERNEL);
105 if (!vdev->regions)
106 return -ENOMEM;
107
108 for (i = 0; i < cnt; i++) {
109 struct resource *res =
110 vdev->get_resource(vdev, i);
111
112 if (!res)
113 goto err;
114
115 vdev->regions[i].addr = res->start;
116 vdev->regions[i].size = resource_size(res);
117 vdev->regions[i].flags = 0;
118
119 switch (resource_type(res)) {
120 case IORESOURCE_MEM:
121 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600122 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
123 if (!(res->flags & IORESOURCE_READONLY))
124 vdev->regions[i].flags |=
125 VFIO_REGION_INFO_FLAG_WRITE;
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600126
127 /*
128 * Only regions addressed with PAGE granularity may be
129 * MMAPed securely.
130 */
131 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
132 !(vdev->regions[i].size & ~PAGE_MASK))
133 vdev->regions[i].flags |=
134 VFIO_REGION_INFO_FLAG_MMAP;
135
Antonios Motakise8909e62015-03-16 14:08:46 -0600136 break;
137 case IORESOURCE_IO:
138 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
139 break;
140 default:
141 goto err;
142 }
143 }
144
145 vdev->num_regions = cnt;
146
147 return 0;
148err:
149 kfree(vdev->regions);
150 return -EINVAL;
151}
152
153static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
154{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600155 int i;
156
157 for (i = 0; i < vdev->num_regions; i++)
158 iounmap(vdev->regions[i].ioaddr);
159
Antonios Motakise8909e62015-03-16 14:08:46 -0600160 vdev->num_regions = 0;
161 kfree(vdev->regions);
162}
163
Sinan Kaya5afec272016-07-19 09:01:45 -0400164static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
165 const char **extra_dbg)
Sinan Kayaf084aa72016-07-19 09:01:42 -0400166{
167 if (vdev->of_reset) {
168 dev_info(vdev->device, "reset\n");
169 return vdev->of_reset(vdev);
170 }
171
172 dev_warn(vdev->device, "no reset function found!\n");
173 return -EINVAL;
174}
175
Antonios Motakisde49fc02015-03-16 14:08:42 -0600176static void vfio_platform_release(void *device_data)
177{
Antonios Motakise8909e62015-03-16 14:08:46 -0600178 struct vfio_platform_device *vdev = device_data;
179
180 mutex_lock(&driver_lock);
181
182 if (!(--vdev->refcnt)) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400183 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600184 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600185 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600186 }
187
188 mutex_unlock(&driver_lock);
189
Eric Auger32a2d712015-11-03 18:12:12 +0000190 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600191}
192
193static int vfio_platform_open(void *device_data)
194{
Antonios Motakise8909e62015-03-16 14:08:46 -0600195 struct vfio_platform_device *vdev = device_data;
196 int ret;
197
Eric Auger32a2d712015-11-03 18:12:12 +0000198 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600199 return -ENODEV;
200
Antonios Motakise8909e62015-03-16 14:08:46 -0600201 mutex_lock(&driver_lock);
202
203 if (!vdev->refcnt) {
204 ret = vfio_platform_regions_init(vdev);
205 if (ret)
206 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600207
208 ret = vfio_platform_irq_init(vdev);
209 if (ret)
210 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200211
Sinan Kaya5afec272016-07-19 09:01:45 -0400212 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600213 }
214
215 vdev->refcnt++;
216
217 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600218 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600219
Antonios Motakis682704c2015-03-16 14:08:48 -0600220err_irq:
221 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600222err_reg:
223 mutex_unlock(&driver_lock);
224 module_put(THIS_MODULE);
225 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600226}
227
228static long vfio_platform_ioctl(void *device_data,
229 unsigned int cmd, unsigned long arg)
230{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600231 struct vfio_platform_device *vdev = device_data;
232 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600233
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600234 if (cmd == VFIO_DEVICE_GET_INFO) {
235 struct vfio_device_info info;
236
237 minsz = offsetofend(struct vfio_device_info, num_irqs);
238
239 if (copy_from_user(&info, (void __user *)arg, minsz))
240 return -EFAULT;
241
242 if (info.argsz < minsz)
243 return -EINVAL;
244
Sinan Kayadc5542f2016-07-19 09:01:43 -0400245 if (vfio_platform_has_reset(vdev))
Eric Auger813ae6602015-06-15 11:09:43 +0200246 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600247 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600248 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600249 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600250
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200251 return copy_to_user((void __user *)arg, &info, minsz) ?
252 -EFAULT : 0;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600253
Antonios Motakise8909e62015-03-16 14:08:46 -0600254 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
255 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600256
Antonios Motakise8909e62015-03-16 14:08:46 -0600257 minsz = offsetofend(struct vfio_region_info, offset);
258
259 if (copy_from_user(&info, (void __user *)arg, minsz))
260 return -EFAULT;
261
262 if (info.argsz < minsz)
263 return -EINVAL;
264
265 if (info.index >= vdev->num_regions)
266 return -EINVAL;
267
268 /* map offset to the physical address */
269 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
270 info.size = vdev->regions[info.index].size;
271 info.flags = vdev->regions[info.index].flags;
272
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200273 return copy_to_user((void __user *)arg, &info, minsz) ?
274 -EFAULT : 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600275
Antonios Motakis682704c2015-03-16 14:08:48 -0600276 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
277 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600278
Antonios Motakis682704c2015-03-16 14:08:48 -0600279 minsz = offsetofend(struct vfio_irq_info, count);
280
281 if (copy_from_user(&info, (void __user *)arg, minsz))
282 return -EFAULT;
283
284 if (info.argsz < minsz)
285 return -EINVAL;
286
287 if (info.index >= vdev->num_irqs)
288 return -EINVAL;
289
290 info.flags = vdev->irqs[info.index].flags;
291 info.count = vdev->irqs[info.index].count;
292
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200293 return copy_to_user((void __user *)arg, &info, minsz) ?
294 -EFAULT : 0;
Antonios Motakis682704c2015-03-16 14:08:48 -0600295
Antonios Motakis9a363212015-03-16 14:08:49 -0600296 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
297 struct vfio_irq_set hdr;
298 u8 *data = NULL;
299 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600300
Antonios Motakis9a363212015-03-16 14:08:49 -0600301 minsz = offsetofend(struct vfio_irq_set, count);
302
303 if (copy_from_user(&hdr, (void __user *)arg, minsz))
304 return -EFAULT;
305
306 if (hdr.argsz < minsz)
307 return -EINVAL;
308
309 if (hdr.index >= vdev->num_irqs)
310 return -EINVAL;
311
312 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
313 VFIO_IRQ_SET_ACTION_TYPE_MASK))
314 return -EINVAL;
315
316 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
317 size_t size;
318
319 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
320 size = sizeof(uint8_t);
321 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
322 size = sizeof(int32_t);
323 else
324 return -EINVAL;
325
326 if (hdr.argsz - minsz < size)
327 return -EINVAL;
328
329 data = memdup_user((void __user *)(arg + minsz), size);
330 if (IS_ERR(data))
331 return PTR_ERR(data);
332 }
333
334 mutex_lock(&vdev->igate);
335
336 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
337 hdr.start, hdr.count, data);
338 mutex_unlock(&vdev->igate);
339 kfree(data);
340
341 return ret;
342
Eric Auger813ae6602015-06-15 11:09:43 +0200343 } else if (cmd == VFIO_DEVICE_RESET) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400344 return vfio_platform_call_reset(vdev, NULL);
Eric Auger813ae6602015-06-15 11:09:43 +0200345 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600346
347 return -ENOTTY;
348}
349
James Morse1b4bb2e2015-10-29 16:50:43 +0000350static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600351 char __user *buf, size_t count,
352 loff_t off)
353{
354 unsigned int done = 0;
355
James Morse1b4bb2e2015-10-29 16:50:43 +0000356 if (!reg->ioaddr) {
357 reg->ioaddr =
358 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600359
James Morse1b4bb2e2015-10-29 16:50:43 +0000360 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600361 return -ENOMEM;
362 }
363
364 while (count) {
365 size_t filled;
366
367 if (count >= 4 && !(off % 4)) {
368 u32 val;
369
James Morse1b4bb2e2015-10-29 16:50:43 +0000370 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600371 if (copy_to_user(buf, &val, 4))
372 goto err;
373
374 filled = 4;
375 } else if (count >= 2 && !(off % 2)) {
376 u16 val;
377
James Morse1b4bb2e2015-10-29 16:50:43 +0000378 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600379 if (copy_to_user(buf, &val, 2))
380 goto err;
381
382 filled = 2;
383 } else {
384 u8 val;
385
James Morse1b4bb2e2015-10-29 16:50:43 +0000386 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600387 if (copy_to_user(buf, &val, 1))
388 goto err;
389
390 filled = 1;
391 }
392
393
394 count -= filled;
395 done += filled;
396 off += filled;
397 buf += filled;
398 }
399
400 return done;
401err:
402 return -EFAULT;
403}
404
Antonios Motakisde49fc02015-03-16 14:08:42 -0600405static ssize_t vfio_platform_read(void *device_data, char __user *buf,
406 size_t count, loff_t *ppos)
407{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600408 struct vfio_platform_device *vdev = device_data;
409 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
410 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
411
412 if (index >= vdev->num_regions)
413 return -EINVAL;
414
415 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
416 return -EINVAL;
417
418 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000419 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600420 buf, count, off);
421 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
422 return -EINVAL; /* not implemented */
423
Antonios Motakisde49fc02015-03-16 14:08:42 -0600424 return -EINVAL;
425}
426
James Morse1b4bb2e2015-10-29 16:50:43 +0000427static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600428 const char __user *buf, size_t count,
429 loff_t off)
430{
431 unsigned int done = 0;
432
James Morse1b4bb2e2015-10-29 16:50:43 +0000433 if (!reg->ioaddr) {
434 reg->ioaddr =
435 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600436
James Morse1b4bb2e2015-10-29 16:50:43 +0000437 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600438 return -ENOMEM;
439 }
440
441 while (count) {
442 size_t filled;
443
444 if (count >= 4 && !(off % 4)) {
445 u32 val;
446
447 if (copy_from_user(&val, buf, 4))
448 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000449 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600450
451 filled = 4;
452 } else if (count >= 2 && !(off % 2)) {
453 u16 val;
454
455 if (copy_from_user(&val, buf, 2))
456 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000457 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600458
459 filled = 2;
460 } else {
461 u8 val;
462
463 if (copy_from_user(&val, buf, 1))
464 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000465 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600466
467 filled = 1;
468 }
469
470 count -= filled;
471 done += filled;
472 off += filled;
473 buf += filled;
474 }
475
476 return done;
477err:
478 return -EFAULT;
479}
480
Antonios Motakisde49fc02015-03-16 14:08:42 -0600481static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
482 size_t count, loff_t *ppos)
483{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600484 struct vfio_platform_device *vdev = device_data;
485 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
486 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
487
488 if (index >= vdev->num_regions)
489 return -EINVAL;
490
491 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
492 return -EINVAL;
493
494 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000495 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600496 buf, count, off);
497 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
498 return -EINVAL; /* not implemented */
499
Antonios Motakisde49fc02015-03-16 14:08:42 -0600500 return -EINVAL;
501}
502
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600503static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
504 struct vm_area_struct *vma)
505{
506 u64 req_len, pgoff, req_start;
507
508 req_len = vma->vm_end - vma->vm_start;
509 pgoff = vma->vm_pgoff &
510 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
511 req_start = pgoff << PAGE_SHIFT;
512
513 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
514 return -EINVAL;
515
516 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
517 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
518
519 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
520 req_len, vma->vm_page_prot);
521}
522
Antonios Motakisde49fc02015-03-16 14:08:42 -0600523static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
524{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600525 struct vfio_platform_device *vdev = device_data;
526 unsigned int index;
527
528 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
529
530 if (vma->vm_end < vma->vm_start)
531 return -EINVAL;
532 if (!(vma->vm_flags & VM_SHARED))
533 return -EINVAL;
534 if (index >= vdev->num_regions)
535 return -EINVAL;
536 if (vma->vm_start & ~PAGE_MASK)
537 return -EINVAL;
538 if (vma->vm_end & ~PAGE_MASK)
539 return -EINVAL;
540
541 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
542 return -EINVAL;
543
544 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
545 && (vma->vm_flags & VM_READ))
546 return -EINVAL;
547
548 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
549 && (vma->vm_flags & VM_WRITE))
550 return -EINVAL;
551
552 vma->vm_private_data = vdev;
553
554 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
555 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
556
557 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
558 return -EINVAL; /* not implemented */
559
Antonios Motakisde49fc02015-03-16 14:08:42 -0600560 return -EINVAL;
561}
562
563static const struct vfio_device_ops vfio_platform_ops = {
564 .name = "vfio-platform",
565 .open = vfio_platform_open,
566 .release = vfio_platform_release,
567 .ioctl = vfio_platform_ioctl,
568 .read = vfio_platform_read,
569 .write = vfio_platform_write,
570 .mmap = vfio_platform_mmap,
571};
572
Sinan Kayaa12a9362016-07-19 09:01:44 -0400573int vfio_platform_of_probe(struct vfio_platform_device *vdev,
574 struct device *dev)
575{
576 int ret;
577
578 ret = device_property_read_string(dev, "compatible",
579 &vdev->compat);
580 if (ret)
581 pr_err("VFIO: cannot retrieve compat for %s\n",
582 vdev->name);
583
584 return ret;
585}
586
587/*
588 * There can be two kernel build combinations. One build where
589 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
590 *
591 * In the first case, vfio_platform_acpi_probe will return since
592 * acpi_disabled is 1. DT user will not see any kind of messages from
593 * ACPI.
594 *
595 * In the second case, both DT and ACPI is compiled in but the system is
596 * booting with any of these combinations.
597 *
598 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
599 * terminates immediately without any messages.
600 *
601 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
602 * valid checks. We cannot claim that this system is DT.
603 */
Antonios Motakisde49fc02015-03-16 14:08:42 -0600604int vfio_platform_probe_common(struct vfio_platform_device *vdev,
605 struct device *dev)
606{
607 struct iommu_group *group;
608 int ret;
609
610 if (!vdev)
611 return -EINVAL;
612
Sinan Kayaa12a9362016-07-19 09:01:44 -0400613 ret = vfio_platform_acpi_probe(vdev, dev);
614 if (ret)
615 ret = vfio_platform_of_probe(vdev, dev);
616
617 if (ret)
618 return ret;
Eric Auger0628c4d2015-11-03 18:12:16 +0000619
Eric Auger705e60b2015-11-03 18:12:18 +0000620 vdev->device = dev;
621
Peng Fan9698cbf2016-05-23 17:47:30 +0800622 group = vfio_iommu_group_get(dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600623 if (!group) {
624 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
625 return -EINVAL;
626 }
627
628 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
629 if (ret) {
Peng Fan9698cbf2016-05-23 17:47:30 +0800630 vfio_iommu_group_put(group, dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600631 return ret;
632 }
633
Eric Augere9e05062015-11-03 18:12:17 +0000634 vfio_platform_get_reset(vdev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200635
Antonios Motakis9a363212015-03-16 14:08:49 -0600636 mutex_init(&vdev->igate);
637
Antonios Motakisde49fc02015-03-16 14:08:42 -0600638 return 0;
639}
640EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
641
642struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
643{
644 struct vfio_platform_device *vdev;
645
646 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200647
648 if (vdev) {
649 vfio_platform_put_reset(vdev);
Peng Fan9698cbf2016-05-23 17:47:30 +0800650 vfio_iommu_group_put(dev->iommu_group, dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200651 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600652
653 return vdev;
654}
655EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000656
Eric Augere0864972015-11-03 18:12:13 +0000657void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
658{
659 mutex_lock(&driver_lock);
660 list_add(&node->link, &reset_list);
661 mutex_unlock(&driver_lock);
662}
663EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
664
665void vfio_platform_unregister_reset(const char *compat,
666 vfio_platform_reset_fn_t fn)
667{
668 struct vfio_platform_reset_node *iter, *temp;
669
670 mutex_lock(&driver_lock);
671 list_for_each_entry_safe(iter, temp, &reset_list, link) {
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400672 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
Eric Augere0864972015-11-03 18:12:13 +0000673 list_del(&iter->link);
674 break;
675 }
676 }
677
678 mutex_unlock(&driver_lock);
679
680}
681EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
682
Eric Auger32a2d712015-11-03 18:12:12 +0000683MODULE_VERSION(DRIVER_VERSION);
684MODULE_LICENSE("GPL v2");
685MODULE_AUTHOR(DRIVER_AUTHOR);
686MODULE_DESCRIPTION(DRIVER_DESC);