blob: ac0229622c1f603216afd1d4a3076ae66f6cf352 [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>
16#include <linux/iommu.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19#include <linux/slab.h>
20#include <linux/types.h>
Antonios Motakis2e8567b2015-03-16 14:08:46 -060021#include <linux/uaccess.h>
Antonios Motakisde49fc02015-03-16 14:08:42 -060022#include <linux/vfio.h>
23
24#include "vfio_platform_private.h"
25
Eric Auger32a2d712015-11-03 18:12:12 +000026#define DRIVER_VERSION "0.10"
27#define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
28#define DRIVER_DESC "VFIO platform base module"
29
Eric Augere0864972015-11-03 18:12:13 +000030static LIST_HEAD(reset_list);
Antonios Motakise8909e62015-03-16 14:08:46 -060031static DEFINE_MUTEX(driver_lock);
32
Eric Augere9e05062015-11-03 18:12:17 +000033static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
34 struct module **module)
Eric Auger3eeb0d52015-06-15 11:09:44 +020035{
Eric Augere9e05062015-11-03 18:12:17 +000036 struct vfio_platform_reset_node *iter;
37 vfio_platform_reset_fn_t reset_fn = NULL;
Eric Auger3eeb0d52015-06-15 11:09:44 +020038
Eric Augere9e05062015-11-03 18:12:17 +000039 mutex_lock(&driver_lock);
40 list_for_each_entry(iter, &reset_list, link) {
41 if (!strcmp(iter->compat, compat) &&
42 try_module_get(iter->owner)) {
43 *module = iter->owner;
44 reset_fn = iter->reset;
45 break;
Eric Auger3eeb0d52015-06-15 11:09:44 +020046 }
47 }
Eric Augere9e05062015-11-03 18:12:17 +000048 mutex_unlock(&driver_lock);
49 return reset_fn;
50}
51
52static void vfio_platform_get_reset(struct vfio_platform_device *vdev)
53{
54 char modname[256];
55
56 vdev->reset = vfio_platform_lookup_reset(vdev->compat,
57 &vdev->reset_module);
58 if (!vdev->reset) {
59 snprintf(modname, 256, "vfio-reset:%s", vdev->compat);
60 request_module(modname);
61 vdev->reset = vfio_platform_lookup_reset(vdev->compat,
62 &vdev->reset_module);
63 }
Eric Auger3eeb0d52015-06-15 11:09:44 +020064}
65
66static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
67{
68 if (vdev->reset)
Eric Augere9e05062015-11-03 18:12:17 +000069 module_put(vdev->reset_module);
Eric Auger3eeb0d52015-06-15 11:09:44 +020070}
71
Antonios Motakise8909e62015-03-16 14:08:46 -060072static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
73{
74 int cnt = 0, i;
75
76 while (vdev->get_resource(vdev, cnt))
77 cnt++;
78
79 vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
80 GFP_KERNEL);
81 if (!vdev->regions)
82 return -ENOMEM;
83
84 for (i = 0; i < cnt; i++) {
85 struct resource *res =
86 vdev->get_resource(vdev, i);
87
88 if (!res)
89 goto err;
90
91 vdev->regions[i].addr = res->start;
92 vdev->regions[i].size = resource_size(res);
93 vdev->regions[i].flags = 0;
94
95 switch (resource_type(res)) {
96 case IORESOURCE_MEM:
97 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
Antonios Motakis6e3f2642015-03-16 14:08:47 -060098 vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
99 if (!(res->flags & IORESOURCE_READONLY))
100 vdev->regions[i].flags |=
101 VFIO_REGION_INFO_FLAG_WRITE;
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600102
103 /*
104 * Only regions addressed with PAGE granularity may be
105 * MMAPed securely.
106 */
107 if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
108 !(vdev->regions[i].size & ~PAGE_MASK))
109 vdev->regions[i].flags |=
110 VFIO_REGION_INFO_FLAG_MMAP;
111
Antonios Motakise8909e62015-03-16 14:08:46 -0600112 break;
113 case IORESOURCE_IO:
114 vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
115 break;
116 default:
117 goto err;
118 }
119 }
120
121 vdev->num_regions = cnt;
122
123 return 0;
124err:
125 kfree(vdev->regions);
126 return -EINVAL;
127}
128
129static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
130{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600131 int i;
132
133 for (i = 0; i < vdev->num_regions; i++)
134 iounmap(vdev->regions[i].ioaddr);
135
Antonios Motakise8909e62015-03-16 14:08:46 -0600136 vdev->num_regions = 0;
137 kfree(vdev->regions);
138}
139
Antonios Motakisde49fc02015-03-16 14:08:42 -0600140static void vfio_platform_release(void *device_data)
141{
Antonios Motakise8909e62015-03-16 14:08:46 -0600142 struct vfio_platform_device *vdev = device_data;
143
144 mutex_lock(&driver_lock);
145
146 if (!(--vdev->refcnt)) {
Eric Auger813ae6602015-06-15 11:09:43 +0200147 if (vdev->reset)
148 vdev->reset(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600149 vfio_platform_regions_cleanup(vdev);
Antonios Motakis682704c2015-03-16 14:08:48 -0600150 vfio_platform_irq_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600151 }
152
153 mutex_unlock(&driver_lock);
154
Eric Auger32a2d712015-11-03 18:12:12 +0000155 module_put(vdev->parent_module);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600156}
157
158static int vfio_platform_open(void *device_data)
159{
Antonios Motakise8909e62015-03-16 14:08:46 -0600160 struct vfio_platform_device *vdev = device_data;
161 int ret;
162
Eric Auger32a2d712015-11-03 18:12:12 +0000163 if (!try_module_get(vdev->parent_module))
Antonios Motakisde49fc02015-03-16 14:08:42 -0600164 return -ENODEV;
165
Antonios Motakise8909e62015-03-16 14:08:46 -0600166 mutex_lock(&driver_lock);
167
168 if (!vdev->refcnt) {
169 ret = vfio_platform_regions_init(vdev);
170 if (ret)
171 goto err_reg;
Antonios Motakis682704c2015-03-16 14:08:48 -0600172
173 ret = vfio_platform_irq_init(vdev);
174 if (ret)
175 goto err_irq;
Eric Auger813ae6602015-06-15 11:09:43 +0200176
177 if (vdev->reset)
178 vdev->reset(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600179 }
180
181 vdev->refcnt++;
182
183 mutex_unlock(&driver_lock);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600184 return 0;
Antonios Motakise8909e62015-03-16 14:08:46 -0600185
Antonios Motakis682704c2015-03-16 14:08:48 -0600186err_irq:
187 vfio_platform_regions_cleanup(vdev);
Antonios Motakise8909e62015-03-16 14:08:46 -0600188err_reg:
189 mutex_unlock(&driver_lock);
190 module_put(THIS_MODULE);
191 return ret;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600192}
193
194static long vfio_platform_ioctl(void *device_data,
195 unsigned int cmd, unsigned long arg)
196{
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600197 struct vfio_platform_device *vdev = device_data;
198 unsigned long minsz;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600199
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600200 if (cmd == VFIO_DEVICE_GET_INFO) {
201 struct vfio_device_info info;
202
203 minsz = offsetofend(struct vfio_device_info, num_irqs);
204
205 if (copy_from_user(&info, (void __user *)arg, minsz))
206 return -EFAULT;
207
208 if (info.argsz < minsz)
209 return -EINVAL;
210
Eric Auger813ae6602015-06-15 11:09:43 +0200211 if (vdev->reset)
212 vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600213 info.flags = vdev->flags;
Antonios Motakise8909e62015-03-16 14:08:46 -0600214 info.num_regions = vdev->num_regions;
Antonios Motakis682704c2015-03-16 14:08:48 -0600215 info.num_irqs = vdev->num_irqs;
Antonios Motakis2e8567b2015-03-16 14:08:46 -0600216
217 return copy_to_user((void __user *)arg, &info, minsz);
218
Antonios Motakise8909e62015-03-16 14:08:46 -0600219 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
220 struct vfio_region_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600221
Antonios Motakise8909e62015-03-16 14:08:46 -0600222 minsz = offsetofend(struct vfio_region_info, offset);
223
224 if (copy_from_user(&info, (void __user *)arg, minsz))
225 return -EFAULT;
226
227 if (info.argsz < minsz)
228 return -EINVAL;
229
230 if (info.index >= vdev->num_regions)
231 return -EINVAL;
232
233 /* map offset to the physical address */
234 info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
235 info.size = vdev->regions[info.index].size;
236 info.flags = vdev->regions[info.index].flags;
237
238 return copy_to_user((void __user *)arg, &info, minsz);
239
Antonios Motakis682704c2015-03-16 14:08:48 -0600240 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
241 struct vfio_irq_info info;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600242
Antonios Motakis682704c2015-03-16 14:08:48 -0600243 minsz = offsetofend(struct vfio_irq_info, count);
244
245 if (copy_from_user(&info, (void __user *)arg, minsz))
246 return -EFAULT;
247
248 if (info.argsz < minsz)
249 return -EINVAL;
250
251 if (info.index >= vdev->num_irqs)
252 return -EINVAL;
253
254 info.flags = vdev->irqs[info.index].flags;
255 info.count = vdev->irqs[info.index].count;
256
257 return copy_to_user((void __user *)arg, &info, minsz);
258
Antonios Motakis9a363212015-03-16 14:08:49 -0600259 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
260 struct vfio_irq_set hdr;
261 u8 *data = NULL;
262 int ret = 0;
Antonios Motakisde49fc02015-03-16 14:08:42 -0600263
Antonios Motakis9a363212015-03-16 14:08:49 -0600264 minsz = offsetofend(struct vfio_irq_set, count);
265
266 if (copy_from_user(&hdr, (void __user *)arg, minsz))
267 return -EFAULT;
268
269 if (hdr.argsz < minsz)
270 return -EINVAL;
271
272 if (hdr.index >= vdev->num_irqs)
273 return -EINVAL;
274
275 if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
276 VFIO_IRQ_SET_ACTION_TYPE_MASK))
277 return -EINVAL;
278
279 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
280 size_t size;
281
282 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
283 size = sizeof(uint8_t);
284 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
285 size = sizeof(int32_t);
286 else
287 return -EINVAL;
288
289 if (hdr.argsz - minsz < size)
290 return -EINVAL;
291
292 data = memdup_user((void __user *)(arg + minsz), size);
293 if (IS_ERR(data))
294 return PTR_ERR(data);
295 }
296
297 mutex_lock(&vdev->igate);
298
299 ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
300 hdr.start, hdr.count, data);
301 mutex_unlock(&vdev->igate);
302 kfree(data);
303
304 return ret;
305
Eric Auger813ae6602015-06-15 11:09:43 +0200306 } else if (cmd == VFIO_DEVICE_RESET) {
307 if (vdev->reset)
308 return vdev->reset(vdev);
309 else
310 return -EINVAL;
311 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600312
313 return -ENOTTY;
314}
315
James Morse1b4bb2e2015-10-29 16:50:43 +0000316static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600317 char __user *buf, size_t count,
318 loff_t off)
319{
320 unsigned int done = 0;
321
James Morse1b4bb2e2015-10-29 16:50:43 +0000322 if (!reg->ioaddr) {
323 reg->ioaddr =
324 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600325
James Morse1b4bb2e2015-10-29 16:50:43 +0000326 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600327 return -ENOMEM;
328 }
329
330 while (count) {
331 size_t filled;
332
333 if (count >= 4 && !(off % 4)) {
334 u32 val;
335
James Morse1b4bb2e2015-10-29 16:50:43 +0000336 val = ioread32(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600337 if (copy_to_user(buf, &val, 4))
338 goto err;
339
340 filled = 4;
341 } else if (count >= 2 && !(off % 2)) {
342 u16 val;
343
James Morse1b4bb2e2015-10-29 16:50:43 +0000344 val = ioread16(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600345 if (copy_to_user(buf, &val, 2))
346 goto err;
347
348 filled = 2;
349 } else {
350 u8 val;
351
James Morse1b4bb2e2015-10-29 16:50:43 +0000352 val = ioread8(reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600353 if (copy_to_user(buf, &val, 1))
354 goto err;
355
356 filled = 1;
357 }
358
359
360 count -= filled;
361 done += filled;
362 off += filled;
363 buf += filled;
364 }
365
366 return done;
367err:
368 return -EFAULT;
369}
370
Antonios Motakisde49fc02015-03-16 14:08:42 -0600371static ssize_t vfio_platform_read(void *device_data, char __user *buf,
372 size_t count, loff_t *ppos)
373{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600374 struct vfio_platform_device *vdev = device_data;
375 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
376 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
377
378 if (index >= vdev->num_regions)
379 return -EINVAL;
380
381 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
382 return -EINVAL;
383
384 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000385 return vfio_platform_read_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600386 buf, count, off);
387 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
388 return -EINVAL; /* not implemented */
389
Antonios Motakisde49fc02015-03-16 14:08:42 -0600390 return -EINVAL;
391}
392
James Morse1b4bb2e2015-10-29 16:50:43 +0000393static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600394 const char __user *buf, size_t count,
395 loff_t off)
396{
397 unsigned int done = 0;
398
James Morse1b4bb2e2015-10-29 16:50:43 +0000399 if (!reg->ioaddr) {
400 reg->ioaddr =
401 ioremap_nocache(reg->addr, reg->size);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600402
James Morse1b4bb2e2015-10-29 16:50:43 +0000403 if (!reg->ioaddr)
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600404 return -ENOMEM;
405 }
406
407 while (count) {
408 size_t filled;
409
410 if (count >= 4 && !(off % 4)) {
411 u32 val;
412
413 if (copy_from_user(&val, buf, 4))
414 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000415 iowrite32(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600416
417 filled = 4;
418 } else if (count >= 2 && !(off % 2)) {
419 u16 val;
420
421 if (copy_from_user(&val, buf, 2))
422 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000423 iowrite16(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600424
425 filled = 2;
426 } else {
427 u8 val;
428
429 if (copy_from_user(&val, buf, 1))
430 goto err;
James Morse1b4bb2e2015-10-29 16:50:43 +0000431 iowrite8(val, reg->ioaddr + off);
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600432
433 filled = 1;
434 }
435
436 count -= filled;
437 done += filled;
438 off += filled;
439 buf += filled;
440 }
441
442 return done;
443err:
444 return -EFAULT;
445}
446
Antonios Motakisde49fc02015-03-16 14:08:42 -0600447static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
448 size_t count, loff_t *ppos)
449{
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600450 struct vfio_platform_device *vdev = device_data;
451 unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
452 loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
453
454 if (index >= vdev->num_regions)
455 return -EINVAL;
456
457 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
458 return -EINVAL;
459
460 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
James Morse1b4bb2e2015-10-29 16:50:43 +0000461 return vfio_platform_write_mmio(&vdev->regions[index],
Antonios Motakis6e3f2642015-03-16 14:08:47 -0600462 buf, count, off);
463 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
464 return -EINVAL; /* not implemented */
465
Antonios Motakisde49fc02015-03-16 14:08:42 -0600466 return -EINVAL;
467}
468
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600469static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
470 struct vm_area_struct *vma)
471{
472 u64 req_len, pgoff, req_start;
473
474 req_len = vma->vm_end - vma->vm_start;
475 pgoff = vma->vm_pgoff &
476 ((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
477 req_start = pgoff << PAGE_SHIFT;
478
479 if (region.size < PAGE_SIZE || req_start + req_len > region.size)
480 return -EINVAL;
481
482 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
483 vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
484
485 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
486 req_len, vma->vm_page_prot);
487}
488
Antonios Motakisde49fc02015-03-16 14:08:42 -0600489static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
490{
Antonios Motakisfad4d5b2015-03-16 14:08:48 -0600491 struct vfio_platform_device *vdev = device_data;
492 unsigned int index;
493
494 index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
495
496 if (vma->vm_end < vma->vm_start)
497 return -EINVAL;
498 if (!(vma->vm_flags & VM_SHARED))
499 return -EINVAL;
500 if (index >= vdev->num_regions)
501 return -EINVAL;
502 if (vma->vm_start & ~PAGE_MASK)
503 return -EINVAL;
504 if (vma->vm_end & ~PAGE_MASK)
505 return -EINVAL;
506
507 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
508 return -EINVAL;
509
510 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
511 && (vma->vm_flags & VM_READ))
512 return -EINVAL;
513
514 if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
515 && (vma->vm_flags & VM_WRITE))
516 return -EINVAL;
517
518 vma->vm_private_data = vdev;
519
520 if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
521 return vfio_platform_mmap_mmio(vdev->regions[index], vma);
522
523 else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
524 return -EINVAL; /* not implemented */
525
Antonios Motakisde49fc02015-03-16 14:08:42 -0600526 return -EINVAL;
527}
528
529static const struct vfio_device_ops vfio_platform_ops = {
530 .name = "vfio-platform",
531 .open = vfio_platform_open,
532 .release = vfio_platform_release,
533 .ioctl = vfio_platform_ioctl,
534 .read = vfio_platform_read,
535 .write = vfio_platform_write,
536 .mmap = vfio_platform_mmap,
537};
538
539int vfio_platform_probe_common(struct vfio_platform_device *vdev,
540 struct device *dev)
541{
542 struct iommu_group *group;
543 int ret;
544
545 if (!vdev)
546 return -EINVAL;
547
Eric Auger0628c4d2015-11-03 18:12:16 +0000548 ret = device_property_read_string(dev, "compatible", &vdev->compat);
549 if (ret) {
550 pr_err("VFIO: cannot retrieve compat for %s\n", vdev->name);
551 return -EINVAL;
552 }
553
Antonios Motakisde49fc02015-03-16 14:08:42 -0600554 group = iommu_group_get(dev);
555 if (!group) {
556 pr_err("VFIO: No IOMMU group for device %s\n", vdev->name);
557 return -EINVAL;
558 }
559
560 ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
561 if (ret) {
562 iommu_group_put(group);
563 return ret;
564 }
565
Eric Augere9e05062015-11-03 18:12:17 +0000566 vfio_platform_get_reset(vdev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200567
Antonios Motakis9a363212015-03-16 14:08:49 -0600568 mutex_init(&vdev->igate);
569
Antonios Motakisde49fc02015-03-16 14:08:42 -0600570 return 0;
571}
572EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
573
574struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
575{
576 struct vfio_platform_device *vdev;
577
578 vdev = vfio_del_group_dev(dev);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200579
580 if (vdev) {
581 vfio_platform_put_reset(vdev);
Antonios Motakisde49fc02015-03-16 14:08:42 -0600582 iommu_group_put(dev->iommu_group);
Eric Auger3eeb0d52015-06-15 11:09:44 +0200583 }
Antonios Motakisde49fc02015-03-16 14:08:42 -0600584
585 return vdev;
586}
587EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
Eric Auger32a2d712015-11-03 18:12:12 +0000588
Eric Augere0864972015-11-03 18:12:13 +0000589void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
590{
591 mutex_lock(&driver_lock);
592 list_add(&node->link, &reset_list);
593 mutex_unlock(&driver_lock);
594}
595EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
596
597void vfio_platform_unregister_reset(const char *compat,
598 vfio_platform_reset_fn_t fn)
599{
600 struct vfio_platform_reset_node *iter, *temp;
601
602 mutex_lock(&driver_lock);
603 list_for_each_entry_safe(iter, temp, &reset_list, link) {
604 if (!strcmp(iter->compat, compat) && (iter->reset == fn)) {
605 list_del(&iter->link);
606 break;
607 }
608 }
609
610 mutex_unlock(&driver_lock);
611
612}
613EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
614
Eric Auger32a2d712015-11-03 18:12:12 +0000615MODULE_VERSION(DRIVER_VERSION);
616MODULE_LICENSE("GPL v2");
617MODULE_AUTHOR(DRIVER_AUTHOR);
618MODULE_DESCRIPTION(DRIVER_DESC);