blob: 716f421d1b013fb1cac1aec8ab55fa4754db6fd9 [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 Kayaf084aa72016-07-19 09:01:42 -0400164static int vfio_platform_call_reset(struct vfio_platform_device *vdev)
165{
166 if (vdev->of_reset) {
167 dev_info(vdev->device, "reset\n");
168 return vdev->of_reset(vdev);
169 }
170
171 dev_warn(vdev->device, "no reset function found!\n");
172 return -EINVAL;
173}
174
Antonios Motakisde49fc02015-03-16 14:08:42 -0600175static void vfio_platform_release(void *device_data)
176{
Antonios Motakise8909e62015-03-16 14:08:46 -0600177 struct vfio_platform_device *vdev = device_data;
178
179 mutex_lock(&driver_lock);
180
181 if (!(--vdev->refcnt)) {
Sinan Kayaf084aa72016-07-19 09:01:42 -0400182 vfio_platform_call_reset(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600183 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600184 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600185 }
186
187 mutex_unlock(&driver_lock);
188
Eric Auger32a2d712015-11-03 18:12:12 +0000189 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600190}
191
192static int vfio_platform_open(void *device_data)
193{
Antonios Motakise8909e62015-03-16 14:08:46 -0600194 struct vfio_platform_device *vdev = device_data;
195 int ret;
196
Eric Auger32a2d712015-11-03 18:12:12 +0000197 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600198 return -ENODEV;
199
Antonios Motakise8909e62015-03-16 14:08:46 -0600200 mutex_lock(&driver_lock);
201
202 if (!vdev->refcnt) {
203 ret = vfio_platform_regions_init(vdev);
204 if (ret)
205 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600206
207 ret = vfio_platform_irq_init(vdev);
208 if (ret)
209 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200210
Sinan Kayaf084aa72016-07-19 09:01:42 -0400211 vfio_platform_call_reset(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600212 }
213
214 vdev->refcnt++;
215
216 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600217 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600218
Antonios Motakis682704c2015-03-16 14:08:48 -0600219err_irq:
220 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600221err_reg:
222 mutex_unlock(&driver_lock);
223 module_put(THIS_MODULE);
224 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600225}
226
227static long vfio_platform_ioctl(void *device_data,
228 unsigned int cmd, unsigned long arg)
229{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600230 struct vfio_platform_device *vdev = device_data;
231 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600232
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600233 if (cmd == VFIO_DEVICE_GET_INFO) {
234 struct vfio_device_info info;
235
236 minsz = offsetofend(struct vfio_device_info, num_irqs);
237
238 if (copy_from_user(&info, (void __user *)arg, minsz))
239 return -EFAULT;
240
241 if (info.argsz < minsz)
242 return -EINVAL;
243
Sinan Kayadc5542f2016-07-19 09:01:43 -0400244 if (vfio_platform_has_reset(vdev))
Eric Auger813ae6602015-06-15 11:09:43 +0200245 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600246 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600247 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600248 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600249
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200250 return copy_to_user((void __user *)arg, &info, minsz) ?
251 -EFAULT : 0;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600252
Antonios Motakise8909e62015-03-16 14:08:46 -0600253 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
254 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600255
Antonios Motakise8909e62015-03-16 14:08:46 -0600256 minsz = offsetofend(struct vfio_region_info, offset);
257
258 if (copy_from_user(&info, (void __user *)arg, minsz))
259 return -EFAULT;
260
261 if (info.argsz < minsz)
262 return -EINVAL;
263
264 if (info.index >= vdev->num_regions)
265 return -EINVAL;
266
267 /* map offset to the physical address */
268 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
269 info.size = vdev->regions[info.index].size;
270 info.flags = vdev->regions[info.index].flags;
271
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200272 return copy_to_user((void __user *)arg, &info, minsz) ?
273 -EFAULT : 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600274
Antonios Motakis682704c2015-03-16 14:08:48 -0600275 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
276 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600277
Antonios Motakis682704c2015-03-16 14:08:48 -0600278 minsz = offsetofend(struct vfio_irq_info, count);
279
280 if (copy_from_user(&info, (void __user *)arg, minsz))
281 return -EFAULT;
282
283 if (info.argsz < minsz)
284 return -EINVAL;
285
286 if (info.index >= vdev->num_irqs)
287 return -EINVAL;
288
289 info.flags = vdev->irqs[info.index].flags;
290 info.count = vdev->irqs[info.index].count;
291
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200292 return copy_to_user((void __user *)arg, &info, minsz) ?
293 -EFAULT : 0;
Antonios Motakis682704c2015-03-16 14:08:48 -0600294
Antonios Motakis9a363212015-03-16 14:08:49 -0600295 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
296 struct vfio_irq_set hdr;
297 u8 *data = NULL;
298 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600299
Antonios Motakis9a363212015-03-16 14:08:49 -0600300 minsz = offsetofend(struct vfio_irq_set, count);
301
302 if (copy_from_user(&hdr, (void __user *)arg, minsz))
303 return -EFAULT;
304
305 if (hdr.argsz < minsz)
306 return -EINVAL;
307
308 if (hdr.index >= vdev->num_irqs)
309 return -EINVAL;
310
311 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
312 VFIO_IRQ_SET_ACTION_TYPE_MASK))
313 return -EINVAL;
314
315 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
316 size_t size;
317
318 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
319 size = sizeof(uint8_t);
320 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
321 size = sizeof(int32_t);
322 else
323 return -EINVAL;
324
325 if (hdr.argsz - minsz < size)
326 return -EINVAL;
327
328 data = memdup_user((void __user *)(arg + minsz), size);
329 if (IS_ERR(data))
330 return PTR_ERR(data);
331 }
332
333 mutex_lock(&vdev->igate);
334
335 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
336 hdr.start, hdr.count, data);
337 mutex_unlock(&vdev->igate);
338 kfree(data);
339
340 return ret;
341
Eric Auger813ae6602015-06-15 11:09:43 +0200342 } else if (cmd == VFIO_DEVICE_RESET) {
Sinan Kayaf084aa72016-07-19 09:01:42 -0400343 return vfio_platform_call_reset(vdev);
Eric Auger813ae6602015-06-15 11:09:43 +0200344 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600345
346 return -ENOTTY;
347}
348
James Morse1b4bb2e2015-10-29 16:50:43 +0000349static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600350 char __user *buf, size_t count,
351 loff_t off)
352{
353 unsigned int done = 0;
354
James Morse1b4bb2e2015-10-29 16:50:43 +0000355 if (!reg->ioaddr) {
356 reg->ioaddr =
357 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600358
James Morse1b4bb2e2015-10-29 16:50:43 +0000359 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600360 return -ENOMEM;
361 }
362
363 while (count) {
364 size_t filled;
365
366 if (count >= 4 && !(off % 4)) {
367 u32 val;
368
James Morse1b4bb2e2015-10-29 16:50:43 +0000369 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600370 if (copy_to_user(buf, &val, 4))
371 goto err;
372
373 filled = 4;
374 } else if (count >= 2 && !(off % 2)) {
375 u16 val;
376
James Morse1b4bb2e2015-10-29 16:50:43 +0000377 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600378 if (copy_to_user(buf, &val, 2))
379 goto err;
380
381 filled = 2;
382 } else {
383 u8 val;
384
James Morse1b4bb2e2015-10-29 16:50:43 +0000385 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600386 if (copy_to_user(buf, &val, 1))
387 goto err;
388
389 filled = 1;
390 }
391
392
393 count -= filled;
394 done += filled;
395 off += filled;
396 buf += filled;
397 }
398
399 return done;
400err:
401 return -EFAULT;
402}
403
Antonios Motakisde49fc02015-03-16 14:08:42 -0600404static ssize_t vfio_platform_read(void *device_data, char __user *buf,
405 size_t count, loff_t *ppos)
406{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600407 struct vfio_platform_device *vdev = device_data;
408 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
409 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
410
411 if (index >= vdev->num_regions)
412 return -EINVAL;
413
414 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
415 return -EINVAL;
416
417 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000418 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600419 buf, count, off);
420 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
421 return -EINVAL; /* not implemented */
422
Antonios Motakisde49fc02015-03-16 14:08:42 -0600423 return -EINVAL;
424}
425
James Morse1b4bb2e2015-10-29 16:50:43 +0000426static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600427 const char __user *buf, size_t count,
428 loff_t off)
429{
430 unsigned int done = 0;
431
James Morse1b4bb2e2015-10-29 16:50:43 +0000432 if (!reg->ioaddr) {
433 reg->ioaddr =
434 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600435
James Morse1b4bb2e2015-10-29 16:50:43 +0000436 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600437 return -ENOMEM;
438 }
439
440 while (count) {
441 size_t filled;
442
443 if (count >= 4 && !(off % 4)) {
444 u32 val;
445
446 if (copy_from_user(&val, buf, 4))
447 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000448 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600449
450 filled = 4;
451 } else if (count >= 2 && !(off % 2)) {
452 u16 val;
453
454 if (copy_from_user(&val, buf, 2))
455 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000456 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600457
458 filled = 2;
459 } else {
460 u8 val;
461
462 if (copy_from_user(&val, buf, 1))
463 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000464 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600465
466 filled = 1;
467 }
468
469 count -= filled;
470 done += filled;
471 off += filled;
472 buf += filled;
473 }
474
475 return done;
476err:
477 return -EFAULT;
478}
479
Antonios Motakisde49fc02015-03-16 14:08:42 -0600480static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
481 size_t count, loff_t *ppos)
482{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600483 struct vfio_platform_device *vdev = device_data;
484 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
485 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
486
487 if (index >= vdev->num_regions)
488 return -EINVAL;
489
490 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
491 return -EINVAL;
492
493 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000494 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600495 buf, count, off);
496 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
497 return -EINVAL; /* not implemented */
498
Antonios Motakisde49fc02015-03-16 14:08:42 -0600499 return -EINVAL;
500}
501
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600502static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
503 struct vm_area_struct *vma)
504{
505 u64 req_len, pgoff, req_start;
506
507 req_len = vma->vm_end - vma->vm_start;
508 pgoff = vma->vm_pgoff &
509 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
510 req_start = pgoff << PAGE_SHIFT;
511
512 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
513 return -EINVAL;
514
515 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
516 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
517
518 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
519 req_len, vma->vm_page_prot);
520}
521
Antonios Motakisde49fc02015-03-16 14:08:42 -0600522static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
523{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600524 struct vfio_platform_device *vdev = device_data;
525 unsigned int index;
526
527 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
528
529 if (vma->vm_end < vma->vm_start)
530 return -EINVAL;
531 if (!(vma->vm_flags & VM_SHARED))
532 return -EINVAL;
533 if (index >= vdev->num_regions)
534 return -EINVAL;
535 if (vma->vm_start & ~PAGE_MASK)
536 return -EINVAL;
537 if (vma->vm_end & ~PAGE_MASK)
538 return -EINVAL;
539
540 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
541 return -EINVAL;
542
543 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
544 && (vma->vm_flags & VM_READ))
545 return -EINVAL;
546
547 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
548 && (vma->vm_flags & VM_WRITE))
549 return -EINVAL;
550
551 vma->vm_private_data = vdev;
552
553 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
554 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
555
556 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
557 return -EINVAL; /* not implemented */
558
Antonios Motakisde49fc02015-03-16 14:08:42 -0600559 return -EINVAL;
560}
561
562static const struct vfio_device_ops vfio_platform_ops = {
563 .name = "vfio-platform",
564 .open = vfio_platform_open,
565 .release = vfio_platform_release,
566 .ioctl = vfio_platform_ioctl,
567 .read = vfio_platform_read,
568 .write = vfio_platform_write,
569 .mmap = vfio_platform_mmap,
570};
571
Sinan Kayaa12a9362016-07-19 09:01:44 -0400572int vfio_platform_of_probe(struct vfio_platform_device *vdev,
573 struct device *dev)
574{
575 int ret;
576
577 ret = device_property_read_string(dev, "compatible",
578 &vdev->compat);
579 if (ret)
580 pr_err("VFIO: cannot retrieve compat for %s\n",
581 vdev->name);
582
583 return ret;
584}
585
586/*
587 * There can be two kernel build combinations. One build where
588 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
589 *
590 * In the first case, vfio_platform_acpi_probe will return since
591 * acpi_disabled is 1. DT user will not see any kind of messages from
592 * ACPI.
593 *
594 * In the second case, both DT and ACPI is compiled in but the system is
595 * booting with any of these combinations.
596 *
597 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
598 * terminates immediately without any messages.
599 *
600 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
601 * valid checks. We cannot claim that this system is DT.
602 */
Antonios Motakisde49fc02015-03-16 14:08:42 -0600603int vfio_platform_probe_common(struct vfio_platform_device *vdev,
604 struct device *dev)
605{
606 struct iommu_group *group;
607 int ret;
608
609 if (!vdev)
610 return -EINVAL;
611
Sinan Kayaa12a9362016-07-19 09:01:44 -0400612 ret = vfio_platform_acpi_probe(vdev, dev);
613 if (ret)
614 ret = vfio_platform_of_probe(vdev, dev);
615
616 if (ret)
617 return ret;
Eric Auger0628c4d2015-11-03 18:12:16 +0000618
Eric Auger705e60b2015-11-03 18:12:18 +0000619 vdev->device = dev;
620
Peng Fan9698cbf2016-05-23 17:47:30 +0800621 group = vfio_iommu_group_get(dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600622 if (!group) {
623 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
624 return -EINVAL;
625 }
626
627 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
628 if (ret) {
Peng Fan9698cbf2016-05-23 17:47:30 +0800629 vfio_iommu_group_put(group, dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600630 return ret;
631 }
632
Eric Augere9e05062015-11-03 18:12:17 +0000633 vfio_platform_get_reset(vdev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200634
Antonios Motakis9a363212015-03-16 14:08:49 -0600635 mutex_init(&vdev->igate);
636
Antonios Motakisde49fc02015-03-16 14:08:42 -0600637 return 0;
638}
639EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
640
641struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
642{
643 struct vfio_platform_device *vdev;
644
645 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200646
647 if (vdev) {
648 vfio_platform_put_reset(vdev);
Peng Fan9698cbf2016-05-23 17:47:30 +0800649 vfio_iommu_group_put(dev->iommu_group, dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200650 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600651
652 return vdev;
653}
654EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000655
Eric Augere0864972015-11-03 18:12:13 +0000656void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
657{
658 mutex_lock(&driver_lock);
659 list_add(&node->link, &reset_list);
660 mutex_unlock(&driver_lock);
661}
662EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
663
664void vfio_platform_unregister_reset(const char *compat,
665 vfio_platform_reset_fn_t fn)
666{
667 struct vfio_platform_reset_node *iter, *temp;
668
669 mutex_lock(&driver_lock);
670 list_for_each_entry_safe(iter, temp, &reset_list, link) {
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400671 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
Eric Augere0864972015-11-03 18:12:13 +0000672 list_del(&iter->link);
673 break;
674 }
675 }
676
677 mutex_unlock(&driver_lock);
678
679}
680EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
681
Eric Auger32a2d712015-11-03 18:12:12 +0000682MODULE_VERSION(DRIVER_VERSION);
683MODULE_LICENSE("GPL v2");
684MODULE_AUTHOR(DRIVER_AUTHOR);
685MODULE_DESCRIPTION(DRIVER_DESC);