blob: a15a69b5208039b0f69f7a09b1c60b15e7e84d72 [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
Eric Augere9e05062015-11-03 18:12:17 +0000118static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
119{
Sinan Kayad30daa32016-07-19 09:01:46 -0400120 if (VFIO_PLATFORM_IS_ACPI(vdev))
121 return;
122
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 }
Eric Auger3eeb0d52015-06-15 11:09:44 +0200130}
131
132static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
133{
Sinan Kayad30daa32016-07-19 09:01:46 -0400134 if (VFIO_PLATFORM_IS_ACPI(vdev))
135 return;
136
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400137 if (vdev->of_reset)
Eric Augere9e05062015-11-03 18:12:17 +0000138 module_put(vdev->reset_module);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200139}
140
Antonios Motakise8909e62015-03-16 14:08:46 -0600141static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
142{
143 int cnt = 0, i;
144
145 while (vdev->get_resource(vdev, cnt))
146 cnt++;
147
148 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
149 GFP_KERNEL);
150 if (!vdev->regions)
151 return -ENOMEM;
152
153 for (i = 0; i < cnt; i++) {
154 struct resource *res =
155 vdev->get_resource(vdev, i);
156
157 if (!res)
158 goto err;
159
160 vdev->regions[i].addr = res->start;
161 vdev->regions[i].size = resource_size(res);
162 vdev->regions[i].flags = 0;
163
164 switch (resource_type(res)) {
165 case IORESOURCE_MEM:
166 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600167 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
168 if (!(res->flags & IORESOURCE_READONLY))
169 vdev->regions[i].flags |=
170 VFIO_REGION_INFO_FLAG_WRITE;
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600171
172 /*
173 * Only regions addressed with PAGE granularity may be
174 * MMAPed securely.
175 */
176 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
177 !(vdev->regions[i].size & ~PAGE_MASK))
178 vdev->regions[i].flags |=
179 VFIO_REGION_INFO_FLAG_MMAP;
180
Antonios Motakise8909e62015-03-16 14:08:46 -0600181 break;
182 case IORESOURCE_IO:
183 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
184 break;
185 default:
186 goto err;
187 }
188 }
189
190 vdev->num_regions = cnt;
191
192 return 0;
193err:
194 kfree(vdev->regions);
195 return -EINVAL;
196}
197
198static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
199{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600200 int i;
201
202 for (i = 0; i < vdev->num_regions; i++)
203 iounmap(vdev->regions[i].ioaddr);
204
Antonios Motakise8909e62015-03-16 14:08:46 -0600205 vdev->num_regions = 0;
206 kfree(vdev->regions);
207}
208
Sinan Kaya5afec272016-07-19 09:01:45 -0400209static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
210 const char **extra_dbg)
Sinan Kayaf084aa72016-07-19 09:01:42 -0400211{
Sinan Kayad30daa32016-07-19 09:01:46 -0400212 if (VFIO_PLATFORM_IS_ACPI(vdev)) {
213 dev_info(vdev->device, "reset\n");
214 return vfio_platform_acpi_call_reset(vdev, extra_dbg);
215 } else if (vdev->of_reset) {
Sinan Kayaf084aa72016-07-19 09:01:42 -0400216 dev_info(vdev->device, "reset\n");
217 return vdev->of_reset(vdev);
218 }
219
220 dev_warn(vdev->device, "no reset function found!\n");
221 return -EINVAL;
222}
223
Antonios Motakisde49fc02015-03-16 14:08:42 -0600224static void vfio_platform_release(void *device_data)
225{
Antonios Motakise8909e62015-03-16 14:08:46 -0600226 struct vfio_platform_device *vdev = device_data;
227
228 mutex_lock(&driver_lock);
229
230 if (!(--vdev->refcnt)) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400231 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600232 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600233 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600234 }
235
236 mutex_unlock(&driver_lock);
237
Eric Auger32a2d712015-11-03 18:12:12 +0000238 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600239}
240
241static int vfio_platform_open(void *device_data)
242{
Antonios Motakise8909e62015-03-16 14:08:46 -0600243 struct vfio_platform_device *vdev = device_data;
244 int ret;
245
Eric Auger32a2d712015-11-03 18:12:12 +0000246 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600247 return -ENODEV;
248
Antonios Motakise8909e62015-03-16 14:08:46 -0600249 mutex_lock(&driver_lock);
250
251 if (!vdev->refcnt) {
252 ret = vfio_platform_regions_init(vdev);
253 if (ret)
254 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600255
256 ret = vfio_platform_irq_init(vdev);
257 if (ret)
258 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200259
Sinan Kaya5afec272016-07-19 09:01:45 -0400260 vfio_platform_call_reset(vdev, NULL);
Antonios Motakise8909e62015-03-16 14:08:46 -0600261 }
262
263 vdev->refcnt++;
264
265 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600266 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600267
Antonios Motakis682704c2015-03-16 14:08:48 -0600268err_irq:
269 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600270err_reg:
271 mutex_unlock(&driver_lock);
272 module_put(THIS_MODULE);
273 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600274}
275
276static long vfio_platform_ioctl(void *device_data,
277 unsigned int cmd, unsigned long arg)
278{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600279 struct vfio_platform_device *vdev = device_data;
280 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600281
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600282 if (cmd == VFIO_DEVICE_GET_INFO) {
283 struct vfio_device_info info;
284
285 minsz = offsetofend(struct vfio_device_info, num_irqs);
286
287 if (copy_from_user(&info, (void __user *)arg, minsz))
288 return -EFAULT;
289
290 if (info.argsz < minsz)
291 return -EINVAL;
292
Sinan Kayadc5542f2016-07-19 09:01:43 -0400293 if (vfio_platform_has_reset(vdev))
Eric Auger813ae6602015-06-15 11:09:43 +0200294 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600295 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600296 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600297 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600298
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200299 return copy_to_user((void __user *)arg, &info, minsz) ?
300 -EFAULT : 0;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600301
Antonios Motakise8909e62015-03-16 14:08:46 -0600302 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
303 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600304
Antonios Motakise8909e62015-03-16 14:08:46 -0600305 minsz = offsetofend(struct vfio_region_info, offset);
306
307 if (copy_from_user(&info, (void __user *)arg, minsz))
308 return -EFAULT;
309
310 if (info.argsz < minsz)
311 return -EINVAL;
312
313 if (info.index >= vdev->num_regions)
314 return -EINVAL;
315
316 /* map offset to the physical address */
317 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
318 info.size = vdev->regions[info.index].size;
319 info.flags = vdev->regions[info.index].flags;
320
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200321 return copy_to_user((void __user *)arg, &info, minsz) ?
322 -EFAULT : 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600323
Antonios Motakis682704c2015-03-16 14:08:48 -0600324 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
325 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600326
Antonios Motakis682704c2015-03-16 14:08:48 -0600327 minsz = offsetofend(struct vfio_irq_info, count);
328
329 if (copy_from_user(&info, (void __user *)arg, minsz))
330 return -EFAULT;
331
332 if (info.argsz < minsz)
333 return -EINVAL;
334
335 if (info.index >= vdev->num_irqs)
336 return -EINVAL;
337
338 info.flags = vdev->irqs[info.index].flags;
339 info.count = vdev->irqs[info.index].count;
340
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +0200341 return copy_to_user((void __user *)arg, &info, minsz) ?
342 -EFAULT : 0;
Antonios Motakis682704c2015-03-16 14:08:48 -0600343
Antonios Motakis9a363212015-03-16 14:08:49 -0600344 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
345 struct vfio_irq_set hdr;
346 u8 *data = NULL;
347 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600348
Antonios Motakis9a363212015-03-16 14:08:49 -0600349 minsz = offsetofend(struct vfio_irq_set, count);
350
351 if (copy_from_user(&hdr, (void __user *)arg, minsz))
352 return -EFAULT;
353
354 if (hdr.argsz < minsz)
355 return -EINVAL;
356
357 if (hdr.index >= vdev->num_irqs)
358 return -EINVAL;
359
360 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
361 VFIO_IRQ_SET_ACTION_TYPE_MASK))
362 return -EINVAL;
363
364 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
365 size_t size;
366
367 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
368 size = sizeof(uint8_t);
369 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
370 size = sizeof(int32_t);
371 else
372 return -EINVAL;
373
374 if (hdr.argsz - minsz < size)
375 return -EINVAL;
376
377 data = memdup_user((void __user *)(arg + minsz), size);
378 if (IS_ERR(data))
379 return PTR_ERR(data);
380 }
381
382 mutex_lock(&vdev->igate);
383
384 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
385 hdr.start, hdr.count, data);
386 mutex_unlock(&vdev->igate);
387 kfree(data);
388
389 return ret;
390
Eric Auger813ae6602015-06-15 11:09:43 +0200391 } else if (cmd == VFIO_DEVICE_RESET) {
Sinan Kaya5afec272016-07-19 09:01:45 -0400392 return vfio_platform_call_reset(vdev, NULL);
Eric Auger813ae6602015-06-15 11:09:43 +0200393 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600394
395 return -ENOTTY;
396}
397
James Morse1b4bb2e2015-10-29 16:50:43 +0000398static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600399 char __user *buf, size_t count,
400 loff_t off)
401{
402 unsigned int done = 0;
403
James Morse1b4bb2e2015-10-29 16:50:43 +0000404 if (!reg->ioaddr) {
405 reg->ioaddr =
406 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600407
James Morse1b4bb2e2015-10-29 16:50:43 +0000408 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600409 return -ENOMEM;
410 }
411
412 while (count) {
413 size_t filled;
414
415 if (count >= 4 && !(off % 4)) {
416 u32 val;
417
James Morse1b4bb2e2015-10-29 16:50:43 +0000418 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600419 if (copy_to_user(buf, &val, 4))
420 goto err;
421
422 filled = 4;
423 } else if (count >= 2 && !(off % 2)) {
424 u16 val;
425
James Morse1b4bb2e2015-10-29 16:50:43 +0000426 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600427 if (copy_to_user(buf, &val, 2))
428 goto err;
429
430 filled = 2;
431 } else {
432 u8 val;
433
James Morse1b4bb2e2015-10-29 16:50:43 +0000434 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600435 if (copy_to_user(buf, &val, 1))
436 goto err;
437
438 filled = 1;
439 }
440
441
442 count -= filled;
443 done += filled;
444 off += filled;
445 buf += filled;
446 }
447
448 return done;
449err:
450 return -EFAULT;
451}
452
Antonios Motakisde49fc02015-03-16 14:08:42 -0600453static ssize_t vfio_platform_read(void *device_data, char __user *buf,
454 size_t count, loff_t *ppos)
455{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600456 struct vfio_platform_device *vdev = device_data;
457 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
458 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
459
460 if (index >= vdev->num_regions)
461 return -EINVAL;
462
463 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
464 return -EINVAL;
465
466 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000467 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600468 buf, count, off);
469 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
470 return -EINVAL; /* not implemented */
471
Antonios Motakisde49fc02015-03-16 14:08:42 -0600472 return -EINVAL;
473}
474
James Morse1b4bb2e2015-10-29 16:50:43 +0000475static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600476 const char __user *buf, size_t count,
477 loff_t off)
478{
479 unsigned int done = 0;
480
James Morse1b4bb2e2015-10-29 16:50:43 +0000481 if (!reg->ioaddr) {
482 reg->ioaddr =
483 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600484
James Morse1b4bb2e2015-10-29 16:50:43 +0000485 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600486 return -ENOMEM;
487 }
488
489 while (count) {
490 size_t filled;
491
492 if (count >= 4 && !(off % 4)) {
493 u32 val;
494
495 if (copy_from_user(&val, buf, 4))
496 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000497 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600498
499 filled = 4;
500 } else if (count >= 2 && !(off % 2)) {
501 u16 val;
502
503 if (copy_from_user(&val, buf, 2))
504 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000505 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600506
507 filled = 2;
508 } else {
509 u8 val;
510
511 if (copy_from_user(&val, buf, 1))
512 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000513 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600514
515 filled = 1;
516 }
517
518 count -= filled;
519 done += filled;
520 off += filled;
521 buf += filled;
522 }
523
524 return done;
525err:
526 return -EFAULT;
527}
528
Antonios Motakisde49fc02015-03-16 14:08:42 -0600529static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
530 size_t count, loff_t *ppos)
531{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600532 struct vfio_platform_device *vdev = device_data;
533 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
534 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
535
536 if (index >= vdev->num_regions)
537 return -EINVAL;
538
539 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
540 return -EINVAL;
541
542 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000543 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600544 buf, count, off);
545 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
546 return -EINVAL; /* not implemented */
547
Antonios Motakisde49fc02015-03-16 14:08:42 -0600548 return -EINVAL;
549}
550
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600551static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
552 struct vm_area_struct *vma)
553{
554 u64 req_len, pgoff, req_start;
555
556 req_len = vma->vm_end - vma->vm_start;
557 pgoff = vma->vm_pgoff &
558 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
559 req_start = pgoff << PAGE_SHIFT;
560
561 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
562 return -EINVAL;
563
564 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
565 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
566
567 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
568 req_len, vma->vm_page_prot);
569}
570
Antonios Motakisde49fc02015-03-16 14:08:42 -0600571static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
572{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600573 struct vfio_platform_device *vdev = device_data;
574 unsigned int index;
575
576 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
577
578 if (vma->vm_end < vma->vm_start)
579 return -EINVAL;
580 if (!(vma->vm_flags & VM_SHARED))
581 return -EINVAL;
582 if (index >= vdev->num_regions)
583 return -EINVAL;
584 if (vma->vm_start & ~PAGE_MASK)
585 return -EINVAL;
586 if (vma->vm_end & ~PAGE_MASK)
587 return -EINVAL;
588
589 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
590 return -EINVAL;
591
592 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
593 && (vma->vm_flags & VM_READ))
594 return -EINVAL;
595
596 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
597 && (vma->vm_flags & VM_WRITE))
598 return -EINVAL;
599
600 vma->vm_private_data = vdev;
601
602 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
603 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
604
605 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
606 return -EINVAL; /* not implemented */
607
Antonios Motakisde49fc02015-03-16 14:08:42 -0600608 return -EINVAL;
609}
610
611static const struct vfio_device_ops vfio_platform_ops = {
612 .name = "vfio-platform",
613 .open = vfio_platform_open,
614 .release = vfio_platform_release,
615 .ioctl = vfio_platform_ioctl,
616 .read = vfio_platform_read,
617 .write = vfio_platform_write,
618 .mmap = vfio_platform_mmap,
619};
620
Sinan Kayaa12a9362016-07-19 09:01:44 -0400621int vfio_platform_of_probe(struct vfio_platform_device *vdev,
622 struct device *dev)
623{
624 int ret;
625
626 ret = device_property_read_string(dev, "compatible",
627 &vdev->compat);
628 if (ret)
629 pr_err("VFIO: cannot retrieve compat for %s\n",
630 vdev->name);
631
632 return ret;
633}
634
635/*
636 * There can be two kernel build combinations. One build where
637 * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
638 *
639 * In the first case, vfio_platform_acpi_probe will return since
640 * acpi_disabled is 1. DT user will not see any kind of messages from
641 * ACPI.
642 *
643 * In the second case, both DT and ACPI is compiled in but the system is
644 * booting with any of these combinations.
645 *
646 * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
647 * terminates immediately without any messages.
648 *
649 * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
650 * valid checks. We cannot claim that this system is DT.
651 */
Antonios Motakisde49fc02015-03-16 14:08:42 -0600652int vfio_platform_probe_common(struct vfio_platform_device *vdev,
653 struct device *dev)
654{
655 struct iommu_group *group;
656 int ret;
657
658 if (!vdev)
659 return -EINVAL;
660
Sinan Kayaa12a9362016-07-19 09:01:44 -0400661 ret = vfio_platform_acpi_probe(vdev, dev);
662 if (ret)
663 ret = vfio_platform_of_probe(vdev, dev);
664
665 if (ret)
666 return ret;
Eric Auger0628c4d2015-11-03 18:12:16 +0000667
Eric Auger705e60b2015-11-03 18:12:18 +0000668 vdev->device = dev;
669
Peng Fan9698cbf2016-05-23 17:47:30 +0800670 group = vfio_iommu_group_get(dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600671 if (!group) {
672 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
673 return -EINVAL;
674 }
675
676 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
677 if (ret) {
Peng Fan9698cbf2016-05-23 17:47:30 +0800678 vfio_iommu_group_put(group, dev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600679 return ret;
680 }
681
Eric Augere9e05062015-11-03 18:12:17 +0000682 vfio_platform_get_reset(vdev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200683
Antonios Motakis9a363212015-03-16 14:08:49 -0600684 mutex_init(&vdev->igate);
685
Antonios Motakisde49fc02015-03-16 14:08:42 -0600686 return 0;
687}
688EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
689
690struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
691{
692 struct vfio_platform_device *vdev;
693
694 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200695
696 if (vdev) {
697 vfio_platform_put_reset(vdev);
Peng Fan9698cbf2016-05-23 17:47:30 +0800698 vfio_iommu_group_put(dev->iommu_group, dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200699 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600700
701 return vdev;
702}
703EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000704
Eric Augere0864972015-11-03 18:12:13 +0000705void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
706{
707 mutex_lock(&driver_lock);
708 list_add(&node->link, &reset_list);
709 mutex_unlock(&driver_lock);
710}
711EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
712
713void vfio_platform_unregister_reset(const char *compat,
714 vfio_platform_reset_fn_t fn)
715{
716 struct vfio_platform_reset_node *iter, *temp;
717
718 mutex_lock(&driver_lock);
719 list_for_each_entry_safe(iter, temp, &reset_list, link) {
Sinan Kaya7aef80c2016-07-19 09:01:41 -0400720 if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
Eric Augere0864972015-11-03 18:12:13 +0000721 list_del(&iter->link);
722 break;
723 }
724 }
725
726 mutex_unlock(&driver_lock);
727
728}
729EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
730
Eric Auger32a2d712015-11-03 18:12:12 +0000731MODULE_VERSION(DRIVER_VERSION);
732MODULE_LICENSE("GPL v2");
733MODULE_AUTHOR(DRIVER_AUTHOR);
734MODULE_DESCRIPTION(DRIVER_DESC);