blob: d143d08c4f0fef1d8a54ab1ca87c57b8b59c2b8f [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
Baoyou Xie2e062852016-09-01 19:15:35 +080076static int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
Sinan Kayad30daa32016-07-19 09:01:46 -040077 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
Baoyou Xie2e062852016-09-01 19:15:35 +080098static bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
Sinan Kayad30daa32016-07-19 09:01:46 -040099{
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 Kaya0991bbd2016-07-19 09:01:49 -0400233 const char *extra_dbg = NULL;
234 int ret;
235
236 ret = vfio_platform_call_reset(vdev, &extra_dbg);
237 if (ret && vdev->reset_required) {
238 dev_warn(vdev->device, "reset driver is required and reset call failed in release (%d) %s\n",
239 ret, extra_dbg ? extra_dbg : "");
240 WARN_ON(1);
241 }
Antonios Motakise8909e62015-03-16 14:08:46 -0600242 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600243 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600244 }
245
246 mutex_unlock(&driver_lock);
247
Eric Auger32a2d712015-11-03 18:12:12 +0000248 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600249}
250
251static int vfio_platform_open(void *device_data)
252{
Antonios Motakise8909e62015-03-16 14:08:46 -0600253 struct vfio_platform_device *vdev = device_data;
254 int ret;
255
Eric Auger32a2d712015-11-03 18:12:12 +0000256 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600257 return -ENODEV;
258
Antonios Motakise8909e62015-03-16 14:08:46 -0600259 mutex_lock(&driver_lock);
260
261 if (!vdev->refcnt) {
Sinan Kayae9944232016-07-19 09:01:48 -0400262 const char *extra_dbg = NULL;
263
Antonios Motakise8909e62015-03-16 14:08:46 -0600264 ret = vfio_platform_regions_init(vdev);
265 if (ret)
266 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600267
268 ret = vfio_platform_irq_init(vdev);
269 if (ret)
270 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200271
Sinan Kayae9944232016-07-19 09:01:48 -0400272 ret = vfio_platform_call_reset(vdev, &extra_dbg);
273 if (ret && vdev->reset_required) {
274 dev_warn(vdev->device, "reset driver is required and reset call failed in open (%d) %s\n",
275 ret, extra_dbg ? extra_dbg : "");
276 goto err_rst;
277 }
Antonios Motakise8909e62015-03-16 14:08:46 -0600278 }
279
280 vdev->refcnt++;
281
282 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600283 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600284
Sinan Kayae9944232016-07-19 09:01:48 -0400285err_rst:
286 vfio_platform_irq_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600287err_irq:
288 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600289err_reg:
290 mutex_unlock(&driver_lock);
291 module_put(THIS_MODULE);
292 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600293}
294
295static long vfio_platform_ioctl(void *device_data,
296 unsigned int cmd, unsigned long arg)
297{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600298 struct vfio_platform_device *vdev = device_data;
299 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600300
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600301 if (cmd == VFIO_DEVICE_GET_INFO) {
302 struct vfio_device_info info;
303
304 minsz = offsetofend(struct vfio_device_info, num_irqs);
305
306 if (copy_from_user(&info, (void __user *)arg, minsz))
307 return -EFAULT;
308
309 if (info.argsz < minsz)
310 return -EINVAL;
311
Sinan Kayadc5542f2016-07-19 09:01:43 -0400312 if (vfio_platform_has_reset(vdev))
Eric Auger813ae6602015-06-15 11:09:43 +0200313 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600314 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600315 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600316 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600317
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200318 return copy_to_user((void __user *)arg, &info, minsz) ?
319 -EFAULT : 0;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600320
Antonios Motakise8909e62015-03-16 14:08:46 -0600321 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
322 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600323
Antonios Motakise8909e62015-03-16 14:08:46 -0600324 minsz = offsetofend(struct vfio_region_info, offset);
325
326 if (copy_from_user(&info, (void __user *)arg, minsz))
327 return -EFAULT;
328
329 if (info.argsz < minsz)
330 return -EINVAL;
331
332 if (info.index >= vdev->num_regions)
333 return -EINVAL;
334
335 /* map offset to the physical address */
336 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
337 info.size = vdev->regions[info.index].size;
338 info.flags = vdev->regions[info.index].flags;
339
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200340 return copy_to_user((void __user *)arg, &info, minsz) ?
341 -EFAULT : 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600342
Antonios Motakis682704c2015-03-16 14:08:48 -0600343 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
344 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600345
Antonios Motakis682704c2015-03-16 14:08:48 -0600346 minsz = offsetofend(struct vfio_irq_info, count);
347
348 if (copy_from_user(&info, (void __user *)arg, minsz))
349 return -EFAULT;
350
351 if (info.argsz < minsz)
352 return -EINVAL;
353
354 if (info.index >= vdev->num_irqs)
355 return -EINVAL;
356
357 info.flags = vdev->irqs[info.index].flags;
358 info.count = vdev->irqs[info.index].count;
359
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200360 return copy_to_user((void __user *)arg, &info, minsz) ?
361 -EFAULT : 0;
Antonios Motakis682704c2015-03-16 14:08:48 -0600362
Antonios Motakis9a363212015-03-16 14:08:49 -0600363 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
364 struct vfio_irq_set hdr;
365 u8 *data = NULL;
366 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600367
Antonios Motakis9a363212015-03-16 14:08:49 -0600368 minsz = offsetofend(struct vfio_irq_set, count);
369
370 if (copy_from_user(&hdr, (void __user *)arg, minsz))
371 return -EFAULT;
372
373 if (hdr.argsz < minsz)
374 return -EINVAL;
375
376 if (hdr.index >= vdev->num_irqs)
377 return -EINVAL;
378
379 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
380 VFIO_IRQ_SET_ACTION_TYPE_MASK))
381 return -EINVAL;
382
383 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
384 size_t size;
385
386 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
387 size = sizeof(uint8_t);
388 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
389 size = sizeof(int32_t);
390 else
391 return -EINVAL;
392
393 if (hdr.argsz - minsz < size)
394 return -EINVAL;
395
396 data = memdup_user((void __user *)(arg + minsz), size);
397 if (IS_ERR(data))
398 return PTR_ERR(data);
399 }
400
401 mutex_lock(&vdev->igate);
402
403 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
404 hdr.start, hdr.count, data);
405 mutex_unlock(&vdev->igate);
406 kfree(data);
407
408 return ret;
409
Eric Auger813ae6602015-06-15 11:09:43 +0200410 } else if (cmd == VFIO_DEVICE_RESET) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400411 return vfio_platform_call_reset(vdev, NULL);
Eric Auger813ae6602015-06-15 11:09:43 +0200412 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600413
414 return -ENOTTY;
415}
416
James Morse1b4bb2e2015-10-29 16:50:43 +0000417static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600418 char __user *buf, size_t count,
419 loff_t off)
420{
421 unsigned int done = 0;
422
James Morse1b4bb2e2015-10-29 16:50:43 +0000423 if (!reg->ioaddr) {
424 reg->ioaddr =
425 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600426
James Morse1b4bb2e2015-10-29 16:50:43 +0000427 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600428 return -ENOMEM;
429 }
430
431 while (count) {
432 size_t filled;
433
434 if (count >= 4 && !(off % 4)) {
435 u32 val;
436
James Morse1b4bb2e2015-10-29 16:50:43 +0000437 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600438 if (copy_to_user(buf, &val, 4))
439 goto err;
440
441 filled = 4;
442 } else if (count >= 2 && !(off % 2)) {
443 u16 val;
444
James Morse1b4bb2e2015-10-29 16:50:43 +0000445 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600446 if (copy_to_user(buf, &val, 2))
447 goto err;
448
449 filled = 2;
450 } else {
451 u8 val;
452
James Morse1b4bb2e2015-10-29 16:50:43 +0000453 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600454 if (copy_to_user(buf, &val, 1))
455 goto err;
456
457 filled = 1;
458 }
459
460
461 count -= filled;
462 done += filled;
463 off += filled;
464 buf += filled;
465 }
466
467 return done;
468err:
469 return -EFAULT;
470}
471
Antonios Motakisde49fc02015-03-16 14:08:42 -0600472static ssize_t vfio_platform_read(void *device_data, char __user *buf,
473 size_t count, loff_t *ppos)
474{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600475 struct vfio_platform_device *vdev = device_data;
476 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
477 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
478
479 if (index >= vdev->num_regions)
480 return -EINVAL;
481
482 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
483 return -EINVAL;
484
485 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000486 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600487 buf, count, off);
488 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
489 return -EINVAL; /* not implemented */
490
Antonios Motakisde49fc02015-03-16 14:08:42 -0600491 return -EINVAL;
492}
493
James Morse1b4bb2e2015-10-29 16:50:43 +0000494static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600495 const char __user *buf, size_t count,
496 loff_t off)
497{
498 unsigned int done = 0;
499
James Morse1b4bb2e2015-10-29 16:50:43 +0000500 if (!reg->ioaddr) {
501 reg->ioaddr =
502 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600503
James Morse1b4bb2e2015-10-29 16:50:43 +0000504 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600505 return -ENOMEM;
506 }
507
508 while (count) {
509 size_t filled;
510
511 if (count >= 4 && !(off % 4)) {
512 u32 val;
513
514 if (copy_from_user(&val, buf, 4))
515 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000516 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600517
518 filled = 4;
519 } else if (count >= 2 && !(off % 2)) {
520 u16 val;
521
522 if (copy_from_user(&val, buf, 2))
523 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000524 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600525
526 filled = 2;
527 } else {
528 u8 val;
529
530 if (copy_from_user(&val, buf, 1))
531 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000532 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600533
534 filled = 1;
535 }
536
537 count -= filled;
538 done += filled;
539 off += filled;
540 buf += filled;
541 }
542
543 return done;
544err:
545 return -EFAULT;
546}
547
Antonios Motakisde49fc02015-03-16 14:08:42 -0600548static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
549 size_t count, loff_t *ppos)
550{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600551 struct vfio_platform_device *vdev = device_data;
552 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
553 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
554
555 if (index >= vdev->num_regions)
556 return -EINVAL;
557
558 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
559 return -EINVAL;
560
561 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000562 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600563 buf, count, off);
564 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
565 return -EINVAL; /* not implemented */
566
Antonios Motakisde49fc02015-03-16 14:08:42 -0600567 return -EINVAL;
568}
569
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600570static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
571 struct vm_area_struct *vma)
572{
573 u64 req_len, pgoff, req_start;
574
575 req_len = vma->vm_end - vma->vm_start;
576 pgoff = vma->vm_pgoff &
577 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
578 req_start = pgoff << PAGE_SHIFT;
579
580 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
581 return -EINVAL;
582
583 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
584 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
585
586 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
587 req_len, vma->vm_page_prot);
588}
589
Antonios Motakisde49fc02015-03-16 14:08:42 -0600590static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
591{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600592 struct vfio_platform_device *vdev = device_data;
593 unsigned int index;
594
595 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
596
597 if (vma->vm_end < vma->vm_start)
598 return -EINVAL;
599 if (!(vma->vm_flags & VM_SHARED))
600 return -EINVAL;
601 if (index >= vdev->num_regions)
602 return -EINVAL;
603 if (vma->vm_start & ~PAGE_MASK)
604 return -EINVAL;
605 if (vma->vm_end & ~PAGE_MASK)
606 return -EINVAL;
607
608 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
609 return -EINVAL;
610
611 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
612 && (vma->vm_flags & VM_READ))
613 return -EINVAL;
614
615 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
616 && (vma->vm_flags & VM_WRITE))
617 return -EINVAL;
618
619 vma->vm_private_data = vdev;
620
621 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
622 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
623
624 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
625 return -EINVAL; /* not implemented */
626
Antonios Motakisde49fc02015-03-16 14:08:42 -0600627 return -EINVAL;
628}
629
630static const struct vfio_device_ops vfio_platform_ops = {
631 .name = "vfio-platform",
632 .open = vfio_platform_open,
633 .release = vfio_platform_release,
634 .ioctl = vfio_platform_ioctl,
635 .read = vfio_platform_read,
636 .write = vfio_platform_write,
637 .mmap = vfio_platform_mmap,
638};
639
Baoyou Xie2e062852016-09-01 19:15:35 +0800640static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
Sinan Kayaa12a9362016-07-19 09:01:44 -0400641 struct device *dev)
642{
643 int ret;
644
645 ret = device_property_read_string(dev, "compatible",
646 &vdev->compat);
647 if (ret)
648 pr_err("VFIO: cannot retrieve compat for %s\n",
649 vdev->name);
650
651 return ret;
652}
653
654/*
655 * There can be two kernel build combinations. One build where
656 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
657 *
658 * In the first case, vfio_platform_acpi_probe will return since
659 * acpi_disabled is 1. DT user will not see any kind of messages from
660 * ACPI.
661 *
662 * In the second case, both DT and ACPI is compiled in but the system is
663 * booting with any of these combinations.
664 *
665 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
666 * terminates immediately without any messages.
667 *
668 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
669 * valid checks. We cannot claim that this system is DT.
670 */
Antonios Motakisde49fc02015-03-16 14:08:42 -0600671int vfio_platform_probe_common(struct vfio_platform_device *vdev,
672 struct device *dev)
673{
674 struct iommu_group *group;
675 int ret;
676
677 if (!vdev)
678 return -EINVAL;
679
Sinan Kayaa12a9362016-07-19 09:01:44 -0400680 ret = vfio_platform_acpi_probe(vdev, dev);
681 if (ret)
682 ret = vfio_platform_of_probe(vdev, dev);
683
684 if (ret)
685 return ret;
Eric Auger0628c4d2015-11-03 18:12:16 +0000686
Eric Auger705e60b2015-11-03 18:12:18 +0000687 vdev->device = dev;
688
Sinan Kayab5add542016-07-19 09:01:47 -0400689 ret = vfio_platform_get_reset(vdev);
690 if (ret && vdev->reset_required) {
691 pr_err("vfio: no reset function found for device %s\n",
692 vdev->name);
693 return ret;
694 }
695
Peng Fan9698cbf2016-05-23 17:47:30 +0800696 group = vfio_iommu_group_get(dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600697 if (!group) {
698 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
Geert Uytterhoevenc6e81162018-04-11 11:15:48 +0200699 ret = -EINVAL;
700 goto put_reset;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600701 }
702
703 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
Geert Uytterhoevenc6e81162018-04-11 11:15:48 +0200704 if (ret)
705 goto put_iommu;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600706
Antonios Motakis9a363212015-03-16 14:08:49 -0600707 mutex_init(&vdev->igate);
708
Antonios Motakisde49fc02015-03-16 14:08:42 -0600709 return 0;
Geert Uytterhoevenc6e81162018-04-11 11:15:48 +0200710
711put_iommu:
712 vfio_iommu_group_put(group, dev);
713put_reset:
714 vfio_platform_put_reset(vdev);
715 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600716}
717EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
718
719struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
720{
721 struct vfio_platform_device *vdev;
722
723 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200724
725 if (vdev) {
726 vfio_platform_put_reset(vdev);
Peng Fan9698cbf2016-05-23 17:47:30 +0800727 vfio_iommu_group_put(dev->iommu_group, dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200728 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600729
730 return vdev;
731}
732EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000733
Eric Augere0864972015-11-03 18:12:13 +0000734void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
735{
736 mutex_lock(&driver_lock);
737 list_add(&node->link, &reset_list);
738 mutex_unlock(&driver_lock);
739}
740EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
741
742void vfio_platform_unregister_reset(const char *compat,
743 vfio_platform_reset_fn_t fn)
744{
745 struct vfio_platform_reset_node *iter, *temp;
746
747 mutex_lock(&driver_lock);
748 list_for_each_entry_safe(iter, temp, &reset_list, link) {
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400749 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
Eric Augere0864972015-11-03 18:12:13 +0000750 list_del(&iter->link);
751 break;
752 }
753 }
754
755 mutex_unlock(&driver_lock);
756
757}
758EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
759
Eric Auger32a2d712015-11-03 18:12:12 +0000760MODULE_VERSION(DRIVER_VERSION);
761MODULE_LICENSE("GPL v2");
762MODULE_AUTHOR(DRIVER_AUTHOR);
763MODULE_DESCRIPTION(DRIVER_DESC);