blob: d8755d2c60538f2888357090f2232bf3cc335af9 [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
Sinan Kayad30daa32016-07-19 09:01:46 -040031#define VFIO_PLATFORM_IS_ACPI(vdev) ((vdev)->acpihid != NULL)
32
Eric Augere0864972015-11-03 18:12:13 +000033static LIST_HEAD(reset_list);
Antonios Motakise8909e62015-03-16 14:08:46 -060034static DEFINE_MUTEX(driver_lock);
35
Eric Augere9e05062015-11-03 18:12:17 +000036static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
37 struct module **module)
Eric Auger3eeb0d52015-06-15 11:09:44 +020038{
Eric Augere9e05062015-11-03 18:12:17 +000039 struct vfio_platform_reset_node *iter;
40 vfio_platform_reset_fn_t reset_fn = NULL;
Eric Auger3eeb0d52015-06-15 11:09:44 +020041
Eric Augere9e05062015-11-03 18:12:17 +000042 mutex_lock(&driver_lock);
43 list_for_each_entry(iter, &reset_list, link) {
44 if (!strcmp(iter->compat, compat) &&
45 try_module_get(iter->owner)) {
46 *module = iter->owner;
Sinan Kaya7aef80c2016-07-19 09:01:41 -040047 reset_fn = iter->of_reset;
Eric Augere9e05062015-11-03 18:12:17 +000048 break;
Eric Auger3eeb0d52015-06-15 11:09:44 +020049 }
50 }
Eric Augere9e05062015-11-03 18:12:17 +000051 mutex_unlock(&driver_lock);
52 return reset_fn;
53}
54
Sinan Kayaa12a9362016-07-19 09:01:44 -040055static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
56 struct device *dev)
57{
58 struct acpi_device *adev;
59
60 if (acpi_disabled)
61 return -ENOENT;
62
63 adev = ACPI_COMPANION(dev);
64 if (!adev) {
65 pr_err("VFIO: ACPI companion device not found for %s\n",
66 vdev->name);
67 return -ENODEV;
68 }
69
70#ifdef CONFIG_ACPI
71 vdev->acpihid = acpi_device_hid(adev);
72#endif
73 return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
74}
75
Sinan Kayad30daa32016-07-19 09:01:46 -040076int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
77 const char **extra_dbg)
78{
79#ifdef CONFIG_ACPI
80 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81 struct device *dev = vdev->device;
82 acpi_handle handle = ACPI_HANDLE(dev);
83 acpi_status acpi_ret;
84
85 acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, &buffer);
86 if (ACPI_FAILURE(acpi_ret)) {
87 if (extra_dbg)
88 *extra_dbg = acpi_format_exception(acpi_ret);
89 return -EINVAL;
90 }
91
92 return 0;
93#else
94 return -ENOENT;
95#endif
96}
97
98bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
99{
100#ifdef CONFIG_ACPI
101 struct device *dev = vdev->device;
102 acpi_handle handle = ACPI_HANDLE(dev);
103
104 return acpi_has_method(handle, "_RST");
105#else
106 return false;
107#endif
108}
109
Sinan Kayadc5542f2016-07-19 09:01:43 -0400110static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
111{
Sinan Kayad30daa32016-07-19 09:01:46 -0400112 if (VFIO_PLATFORM_IS_ACPI(vdev))
113 return vfio_platform_acpi_has_reset(vdev);
114
Sinan Kayadc5542f2016-07-19 09:01:43 -0400115 return vdev->of_reset ? true : false;
116}
117
Sinan Kayab5add542016-07-19 09:01:47 -0400118static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
Eric Augere9e05062015-11-03 18:12:17 +0000119{
Sinan Kayad30daa32016-07-19 09:01:46 -0400120 if (VFIO_PLATFORM_IS_ACPI(vdev))
Sinan Kayab5add542016-07-19 09:01:47 -0400121 return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
Sinan Kayad30daa32016-07-19 09:01:46 -0400122
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400123 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
124 &vdev->reset_module);
125 if (!vdev->of_reset) {
Kees Cook7200be72015-11-19 16:17:24 -0800126 request_module("vfio-reset:%s", vdev->compat);
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400127 vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
128 &vdev->reset_module);
Eric Augere9e05062015-11-03 18:12:17 +0000129 }
Sinan Kayab5add542016-07-19 09:01:47 -0400130
131 return vdev->of_reset ? 0 : -ENOENT;
Eric Auger3eeb0d52015-06-15 11:09:44 +0200132}
133
134static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
135{
Sinan Kayad30daa32016-07-19 09:01:46 -0400136 if (VFIO_PLATFORM_IS_ACPI(vdev))
137 return;
138
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400139 if (vdev->of_reset)
Eric Augere9e05062015-11-03 18:12:17 +0000140 module_put(vdev->reset_module);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200141}
142
Antonios Motakise8909e62015-03-16 14:08:46 -0600143static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
144{
145 int cnt = 0, i;
146
147 while (vdev->get_resource(vdev, cnt))
148 cnt++;
149
150 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
151 GFP_KERNEL);
152 if (!vdev->regions)
153 return -ENOMEM;
154
155 for (i = 0; i < cnt; i++) {
156 struct resource *res =
157 vdev->get_resource(vdev, i);
158
159 if (!res)
160 goto err;
161
162 vdev->regions[i].addr = res->start;
163 vdev->regions[i].size = resource_size(res);
164 vdev->regions[i].flags = 0;
165
166 switch (resource_type(res)) {
167 case IORESOURCE_MEM:
168 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600169 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
170 if (!(res->flags & IORESOURCE_READONLY))
171 vdev->regions[i].flags |=
172 VFIO_REGION_INFO_FLAG_WRITE;
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600173
174 /*
175 * Only regions addressed with PAGE granularity may be
176 * MMAPed securely.
177 */
178 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
179 !(vdev->regions[i].size & ~PAGE_MASK))
180 vdev->regions[i].flags |=
181 VFIO_REGION_INFO_FLAG_MMAP;
182
Antonios Motakise8909e62015-03-16 14:08:46 -0600183 break;
184 case IORESOURCE_IO:
185 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
186 break;
187 default:
188 goto err;
189 }
190 }
191
192 vdev->num_regions = cnt;
193
194 return 0;
195err:
196 kfree(vdev->regions);
197 return -EINVAL;
198}
199
200static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
201{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600202 int i;
203
204 for (i = 0; i < vdev->num_regions; i++)
205 iounmap(vdev->regions[i].ioaddr);
206
Antonios Motakise8909e62015-03-16 14:08:46 -0600207 vdev->num_regions = 0;
208 kfree(vdev->regions);
209}
210
Sinan Kaya5afec272016-07-19 09:01:45 -0400211static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
212 const char **extra_dbg)
Sinan Kayaf084aa72016-07-19 09:01:42 -0400213{
Sinan Kayad30daa32016-07-19 09:01:46 -0400214 if (VFIO_PLATFORM_IS_ACPI(vdev)) {
215 dev_info(vdev->device, "reset\n");
216 return vfio_platform_acpi_call_reset(vdev, extra_dbg);
217 } else if (vdev->of_reset) {
Sinan Kayaf084aa72016-07-19 09:01:42 -0400218 dev_info(vdev->device, "reset\n");
219 return vdev->of_reset(vdev);
220 }
221
222 dev_warn(vdev->device, "no reset function found!\n");
223 return -EINVAL;
224}
225
Antonios Motakisde49fc02015-03-16 14:08:42 -0600226static void vfio_platform_release(void *device_data)
227{
Antonios Motakise8909e62015-03-16 14:08:46 -0600228 struct vfio_platform_device *vdev = device_data;
229
230 mutex_lock(&driver_lock);
231
232 if (!(--vdev->refcnt)) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400233 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600234 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600235 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600236 }
237
238 mutex_unlock(&driver_lock);
239
Eric Auger32a2d712015-11-03 18:12:12 +0000240 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600241}
242
243static int vfio_platform_open(void *device_data)
244{
Antonios Motakise8909e62015-03-16 14:08:46 -0600245 struct vfio_platform_device *vdev = device_data;
246 int ret;
247
Eric Auger32a2d712015-11-03 18:12:12 +0000248 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600249 return -ENODEV;
250
Antonios Motakise8909e62015-03-16 14:08:46 -0600251 mutex_lock(&driver_lock);
252
253 if (!vdev->refcnt) {
254 ret = vfio_platform_regions_init(vdev);
255 if (ret)
256 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600257
258 ret = vfio_platform_irq_init(vdev);
259 if (ret)
260 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200261
Sinan Kaya5afec272016-07-19 09:01:45 -0400262 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600263 }
264
265 vdev->refcnt++;
266
267 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600268 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600269
Antonios Motakis682704c2015-03-16 14:08:48 -0600270err_irq:
271 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600272err_reg:
273 mutex_unlock(&driver_lock);
274 module_put(THIS_MODULE);
275 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600276}
277
278static long vfio_platform_ioctl(void *device_data,
279 unsigned int cmd, unsigned long arg)
280{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600281 struct vfio_platform_device *vdev = device_data;
282 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600283
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600284 if (cmd == VFIO_DEVICE_GET_INFO) {
285 struct vfio_device_info info;
286
287 minsz = offsetofend(struct vfio_device_info, num_irqs);
288
289 if (copy_from_user(&info, (void __user *)arg, minsz))
290 return -EFAULT;
291
292 if (info.argsz < minsz)
293 return -EINVAL;
294
Sinan Kayadc5542f2016-07-19 09:01:43 -0400295 if (vfio_platform_has_reset(vdev))
Eric Auger813ae6602015-06-15 11:09:43 +0200296 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600297 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600298 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600299 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600300
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200301 return copy_to_user((void __user *)arg, &info, minsz) ?
302 -EFAULT : 0;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600303
Antonios Motakise8909e62015-03-16 14:08:46 -0600304 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
305 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600306
Antonios Motakise8909e62015-03-16 14:08:46 -0600307 minsz = offsetofend(struct vfio_region_info, offset);
308
309 if (copy_from_user(&info, (void __user *)arg, minsz))
310 return -EFAULT;
311
312 if (info.argsz < minsz)
313 return -EINVAL;
314
315 if (info.index >= vdev->num_regions)
316 return -EINVAL;
317
318 /* map offset to the physical address */
319 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
320 info.size = vdev->regions[info.index].size;
321 info.flags = vdev->regions[info.index].flags;
322
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200323 return copy_to_user((void __user *)arg, &info, minsz) ?
324 -EFAULT : 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600325
Antonios Motakis682704c2015-03-16 14:08:48 -0600326 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
327 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600328
Antonios Motakis682704c2015-03-16 14:08:48 -0600329 minsz = offsetofend(struct vfio_irq_info, count);
330
331 if (copy_from_user(&info, (void __user *)arg, minsz))
332 return -EFAULT;
333
334 if (info.argsz < minsz)
335 return -EINVAL;
336
337 if (info.index >= vdev->num_irqs)
338 return -EINVAL;
339
340 info.flags = vdev->irqs[info.index].flags;
341 info.count = vdev->irqs[info.index].count;
342
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200343 return copy_to_user((void __user *)arg, &info, minsz) ?
344 -EFAULT : 0;
Antonios Motakis682704c2015-03-16 14:08:48 -0600345
Antonios Motakis9a363212015-03-16 14:08:49 -0600346 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
347 struct vfio_irq_set hdr;
348 u8 *data = NULL;
349 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600350
Antonios Motakis9a363212015-03-16 14:08:49 -0600351 minsz = offsetofend(struct vfio_irq_set, count);
352
353 if (copy_from_user(&hdr, (void __user *)arg, minsz))
354 return -EFAULT;
355
356 if (hdr.argsz < minsz)
357 return -EINVAL;
358
359 if (hdr.index >= vdev->num_irqs)
360 return -EINVAL;
361
362 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
363 VFIO_IRQ_SET_ACTION_TYPE_MASK))
364 return -EINVAL;
365
366 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
367 size_t size;
368
369 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
370 size = sizeof(uint8_t);
371 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
372 size = sizeof(int32_t);
373 else
374 return -EINVAL;
375
376 if (hdr.argsz - minsz < size)
377 return -EINVAL;
378
379 data = memdup_user((void __user *)(arg + minsz), size);
380 if (IS_ERR(data))
381 return PTR_ERR(data);
382 }
383
384 mutex_lock(&vdev->igate);
385
386 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
387 hdr.start, hdr.count, data);
388 mutex_unlock(&vdev->igate);
389 kfree(data);
390
391 return ret;
392
Eric Auger813ae6602015-06-15 11:09:43 +0200393 } else if (cmd == VFIO_DEVICE_RESET) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400394 return vfio_platform_call_reset(vdev, NULL);
Eric Auger813ae6602015-06-15 11:09:43 +0200395 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600396
397 return -ENOTTY;
398}
399
James Morse1b4bb2e2015-10-29 16:50:43 +0000400static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600401 char __user *buf, size_t count,
402 loff_t off)
403{
404 unsigned int done = 0;
405
James Morse1b4bb2e2015-10-29 16:50:43 +0000406 if (!reg->ioaddr) {
407 reg->ioaddr =
408 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600409
James Morse1b4bb2e2015-10-29 16:50:43 +0000410 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600411 return -ENOMEM;
412 }
413
414 while (count) {
415 size_t filled;
416
417 if (count >= 4 && !(off % 4)) {
418 u32 val;
419
James Morse1b4bb2e2015-10-29 16:50:43 +0000420 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600421 if (copy_to_user(buf, &val, 4))
422 goto err;
423
424 filled = 4;
425 } else if (count >= 2 && !(off % 2)) {
426 u16 val;
427
James Morse1b4bb2e2015-10-29 16:50:43 +0000428 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600429 if (copy_to_user(buf, &val, 2))
430 goto err;
431
432 filled = 2;
433 } else {
434 u8 val;
435
James Morse1b4bb2e2015-10-29 16:50:43 +0000436 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600437 if (copy_to_user(buf, &val, 1))
438 goto err;
439
440 filled = 1;
441 }
442
443
444 count -= filled;
445 done += filled;
446 off += filled;
447 buf += filled;
448 }
449
450 return done;
451err:
452 return -EFAULT;
453}
454
Antonios Motakisde49fc02015-03-16 14:08:42 -0600455static ssize_t vfio_platform_read(void *device_data, char __user *buf,
456 size_t count, loff_t *ppos)
457{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600458 struct vfio_platform_device *vdev = device_data;
459 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
460 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
461
462 if (index >= vdev->num_regions)
463 return -EINVAL;
464
465 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
466 return -EINVAL;
467
468 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000469 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600470 buf, count, off);
471 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
472 return -EINVAL; /* not implemented */
473
Antonios Motakisde49fc02015-03-16 14:08:42 -0600474 return -EINVAL;
475}
476
James Morse1b4bb2e2015-10-29 16:50:43 +0000477static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600478 const char __user *buf, size_t count,
479 loff_t off)
480{
481 unsigned int done = 0;
482
James Morse1b4bb2e2015-10-29 16:50:43 +0000483 if (!reg->ioaddr) {
484 reg->ioaddr =
485 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600486
James Morse1b4bb2e2015-10-29 16:50:43 +0000487 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600488 return -ENOMEM;
489 }
490
491 while (count) {
492 size_t filled;
493
494 if (count >= 4 && !(off % 4)) {
495 u32 val;
496
497 if (copy_from_user(&val, buf, 4))
498 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000499 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600500
501 filled = 4;
502 } else if (count >= 2 && !(off % 2)) {
503 u16 val;
504
505 if (copy_from_user(&val, buf, 2))
506 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000507 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600508
509 filled = 2;
510 } else {
511 u8 val;
512
513 if (copy_from_user(&val, buf, 1))
514 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000515 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600516
517 filled = 1;
518 }
519
520 count -= filled;
521 done += filled;
522 off += filled;
523 buf += filled;
524 }
525
526 return done;
527err:
528 return -EFAULT;
529}
530
Antonios Motakisde49fc02015-03-16 14:08:42 -0600531static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
532 size_t count, loff_t *ppos)
533{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600534 struct vfio_platform_device *vdev = device_data;
535 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
536 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
537
538 if (index >= vdev->num_regions)
539 return -EINVAL;
540
541 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
542 return -EINVAL;
543
544 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000545 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600546 buf, count, off);
547 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
548 return -EINVAL; /* not implemented */
549
Antonios Motakisde49fc02015-03-16 14:08:42 -0600550 return -EINVAL;
551}
552
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600553static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
554 struct vm_area_struct *vma)
555{
556 u64 req_len, pgoff, req_start;
557
558 req_len = vma->vm_end - vma->vm_start;
559 pgoff = vma->vm_pgoff &
560 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
561 req_start = pgoff << PAGE_SHIFT;
562
563 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
564 return -EINVAL;
565
566 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
567 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
568
569 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
570 req_len, vma->vm_page_prot);
571}
572
Antonios Motakisde49fc02015-03-16 14:08:42 -0600573static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
574{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600575 struct vfio_platform_device *vdev = device_data;
576 unsigned int index;
577
578 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
579
580 if (vma->vm_end < vma->vm_start)
581 return -EINVAL;
582 if (!(vma->vm_flags & VM_SHARED))
583 return -EINVAL;
584 if (index >= vdev->num_regions)
585 return -EINVAL;
586 if (vma->vm_start & ~PAGE_MASK)
587 return -EINVAL;
588 if (vma->vm_end & ~PAGE_MASK)
589 return -EINVAL;
590
591 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
592 return -EINVAL;
593
594 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
595 && (vma->vm_flags & VM_READ))
596 return -EINVAL;
597
598 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
599 && (vma->vm_flags & VM_WRITE))
600 return -EINVAL;
601
602 vma->vm_private_data = vdev;
603
604 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
605 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
606
607 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
608 return -EINVAL; /* not implemented */
609
Antonios Motakisde49fc02015-03-16 14:08:42 -0600610 return -EINVAL;
611}
612
613static const struct vfio_device_ops vfio_platform_ops = {
614 .name = "vfio-platform",
615 .open = vfio_platform_open,
616 .release = vfio_platform_release,
617 .ioctl = vfio_platform_ioctl,
618 .read = vfio_platform_read,
619 .write = vfio_platform_write,
620 .mmap = vfio_platform_mmap,
621};
622
Sinan Kayaa12a9362016-07-19 09:01:44 -0400623int vfio_platform_of_probe(struct vfio_platform_device *vdev,
624 struct device *dev)
625{
626 int ret;
627
628 ret = device_property_read_string(dev, "compatible",
629 &vdev->compat);
630 if (ret)
631 pr_err("VFIO: cannot retrieve compat for %s\n",
632 vdev->name);
633
634 return ret;
635}
636
637/*
638 * There can be two kernel build combinations. One build where
639 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
640 *
641 * In the first case, vfio_platform_acpi_probe will return since
642 * acpi_disabled is 1. DT user will not see any kind of messages from
643 * ACPI.
644 *
645 * In the second case, both DT and ACPI is compiled in but the system is
646 * booting with any of these combinations.
647 *
648 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
649 * terminates immediately without any messages.
650 *
651 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
652 * valid checks. We cannot claim that this system is DT.
653 */
Antonios Motakisde49fc02015-03-16 14:08:42 -0600654int vfio_platform_probe_common(struct vfio_platform_device *vdev,
655 struct device *dev)
656{
657 struct iommu_group *group;
658 int ret;
659
660 if (!vdev)
661 return -EINVAL;
662
Sinan Kayaa12a9362016-07-19 09:01:44 -0400663 ret = vfio_platform_acpi_probe(vdev, dev);
664 if (ret)
665 ret = vfio_platform_of_probe(vdev, dev);
666
667 if (ret)
668 return ret;
Eric Auger0628c4d2015-11-03 18:12:16 +0000669
Eric Auger705e60b2015-11-03 18:12:18 +0000670 vdev->device = dev;
671
Sinan Kayab5add542016-07-19 09:01:47 -0400672 ret = vfio_platform_get_reset(vdev);
673 if (ret && vdev->reset_required) {
674 pr_err("vfio: no reset function found for device %s\n",
675 vdev->name);
676 return ret;
677 }
678
Peng Fan9698cbf2016-05-23 17:47:30 +0800679 group = vfio_iommu_group_get(dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600680 if (!group) {
681 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
682 return -EINVAL;
683 }
684
685 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
686 if (ret) {
Peng Fan9698cbf2016-05-23 17:47:30 +0800687 vfio_iommu_group_put(group, dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600688 return ret;
689 }
690
Antonios Motakis9a363212015-03-16 14:08:49 -0600691 mutex_init(&vdev->igate);
692
Antonios Motakisde49fc02015-03-16 14:08:42 -0600693 return 0;
694}
695EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
696
697struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
698{
699 struct vfio_platform_device *vdev;
700
701 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200702
703 if (vdev) {
704 vfio_platform_put_reset(vdev);
Peng Fan9698cbf2016-05-23 17:47:30 +0800705 vfio_iommu_group_put(dev->iommu_group, dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200706 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600707
708 return vdev;
709}
710EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000711
Eric Augere0864972015-11-03 18:12:13 +0000712void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
713{
714 mutex_lock(&driver_lock);
715 list_add(&node->link, &reset_list);
716 mutex_unlock(&driver_lock);
717}
718EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
719
720void vfio_platform_unregister_reset(const char *compat,
721 vfio_platform_reset_fn_t fn)
722{
723 struct vfio_platform_reset_node *iter, *temp;
724
725 mutex_lock(&driver_lock);
726 list_for_each_entry_safe(iter, temp, &reset_list, link) {
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400727 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
Eric Augere0864972015-11-03 18:12:13 +0000728 list_del(&iter->link);
729 break;
730 }
731 }
732
733 mutex_unlock(&driver_lock);
734
735}
736EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
737
Eric Auger32a2d712015-11-03 18:12:12 +0000738MODULE_VERSION(DRIVER_VERSION);
739MODULE_LICENSE("GPL v2");
740MODULE_AUTHOR(DRIVER_AUTHOR);
741MODULE_DESCRIPTION(DRIVER_DESC);