• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 - Virtual Open Systems
4  * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
5  */
6 
7 #define dev_fmt(fmt)	"VFIO: " fmt
8 
9 #include <linux/device.h>
10 #include <linux/acpi.h>
11 #include <linux/iommu.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/uaccess.h>
18 #include <linux/vfio.h>
19 
20 #include "vfio_platform_private.h"
21 
22 #define DRIVER_VERSION  "0.10"
23 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis@virtualopensystems.com>"
24 #define DRIVER_DESC     "VFIO platform base module"
25 
26 #define VFIO_PLATFORM_IS_ACPI(vdev) ((vdev)->acpihid != NULL)
27 
28 static LIST_HEAD(reset_list);
29 static DEFINE_MUTEX(driver_lock);
30 
vfio_platform_lookup_reset(const char * compat,struct module ** module)31 static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
32 					struct module **module)
33 {
34 	struct vfio_platform_reset_node *iter;
35 	vfio_platform_reset_fn_t reset_fn = NULL;
36 
37 	mutex_lock(&driver_lock);
38 	list_for_each_entry(iter, &reset_list, link) {
39 		if (!strcmp(iter->compat, compat) &&
40 			try_module_get(iter->owner)) {
41 			*module = iter->owner;
42 			reset_fn = iter->of_reset;
43 			break;
44 		}
45 	}
46 	mutex_unlock(&driver_lock);
47 	return reset_fn;
48 }
49 
vfio_platform_acpi_probe(struct vfio_platform_device * vdev,struct device * dev)50 static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
51 				    struct device *dev)
52 {
53 	struct acpi_device *adev;
54 
55 	if (acpi_disabled)
56 		return -ENOENT;
57 
58 	adev = ACPI_COMPANION(dev);
59 	if (!adev) {
60 		dev_err(dev, "ACPI companion device not found for %s\n",
61 			vdev->name);
62 		return -ENODEV;
63 	}
64 
65 #ifdef CONFIG_ACPI
66 	vdev->acpihid = acpi_device_hid(adev);
67 #endif
68 	return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
69 }
70 
vfio_platform_acpi_call_reset(struct vfio_platform_device * vdev,const char ** extra_dbg)71 static int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
72 				  const char **extra_dbg)
73 {
74 #ifdef CONFIG_ACPI
75 	struct device *dev = vdev->device;
76 	acpi_handle handle = ACPI_HANDLE(dev);
77 	acpi_status acpi_ret;
78 
79 	acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, NULL);
80 	if (ACPI_FAILURE(acpi_ret)) {
81 		if (extra_dbg)
82 			*extra_dbg = acpi_format_exception(acpi_ret);
83 		return -EINVAL;
84 	}
85 
86 	return 0;
87 #else
88 	return -ENOENT;
89 #endif
90 }
91 
vfio_platform_acpi_has_reset(struct vfio_platform_device * vdev)92 static bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
93 {
94 #ifdef CONFIG_ACPI
95 	struct device *dev = vdev->device;
96 	acpi_handle handle = ACPI_HANDLE(dev);
97 
98 	return acpi_has_method(handle, "_RST");
99 #else
100 	return false;
101 #endif
102 }
103 
vfio_platform_has_reset(struct vfio_platform_device * vdev)104 static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
105 {
106 	if (VFIO_PLATFORM_IS_ACPI(vdev))
107 		return vfio_platform_acpi_has_reset(vdev);
108 
109 	return vdev->of_reset ? true : false;
110 }
111 
vfio_platform_get_reset(struct vfio_platform_device * vdev)112 static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
113 {
114 	if (VFIO_PLATFORM_IS_ACPI(vdev))
115 		return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
116 
117 	vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
118 						    &vdev->reset_module);
119 	if (!vdev->of_reset) {
120 		request_module("vfio-reset:%s", vdev->compat);
121 		vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
122 							&vdev->reset_module);
123 	}
124 
125 	return vdev->of_reset ? 0 : -ENOENT;
126 }
127 
vfio_platform_put_reset(struct vfio_platform_device * vdev)128 static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
129 {
130 	if (VFIO_PLATFORM_IS_ACPI(vdev))
131 		return;
132 
133 	if (vdev->of_reset)
134 		module_put(vdev->reset_module);
135 }
136 
vfio_platform_regions_init(struct vfio_platform_device * vdev)137 static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
138 {
139 	int cnt = 0, i;
140 
141 	while (vdev->get_resource(vdev, cnt))
142 		cnt++;
143 
144 	vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
145 				GFP_KERNEL);
146 	if (!vdev->regions)
147 		return -ENOMEM;
148 
149 	for (i = 0; i < cnt;  i++) {
150 		struct resource *res =
151 			vdev->get_resource(vdev, i);
152 
153 		if (!res)
154 			goto err;
155 
156 		vdev->regions[i].addr = res->start;
157 		vdev->regions[i].size = resource_size(res);
158 		vdev->regions[i].flags = 0;
159 
160 		switch (resource_type(res)) {
161 		case IORESOURCE_MEM:
162 			vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
163 			vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
164 			if (!(res->flags & IORESOURCE_READONLY))
165 				vdev->regions[i].flags |=
166 					VFIO_REGION_INFO_FLAG_WRITE;
167 
168 			/*
169 			 * Only regions addressed with PAGE granularity may be
170 			 * MMAPed securely.
171 			 */
172 			if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
173 					!(vdev->regions[i].size & ~PAGE_MASK))
174 				vdev->regions[i].flags |=
175 					VFIO_REGION_INFO_FLAG_MMAP;
176 
177 			break;
178 		case IORESOURCE_IO:
179 			vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
180 			break;
181 		default:
182 			goto err;
183 		}
184 	}
185 
186 	vdev->num_regions = cnt;
187 
188 	return 0;
189 err:
190 	kfree(vdev->regions);
191 	return -EINVAL;
192 }
193 
vfio_platform_regions_cleanup(struct vfio_platform_device * vdev)194 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
195 {
196 	int i;
197 
198 	for (i = 0; i < vdev->num_regions; i++)
199 		iounmap(vdev->regions[i].ioaddr);
200 
201 	vdev->num_regions = 0;
202 	kfree(vdev->regions);
203 }
204 
vfio_platform_call_reset(struct vfio_platform_device * vdev,const char ** extra_dbg)205 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
206 				    const char **extra_dbg)
207 {
208 	if (VFIO_PLATFORM_IS_ACPI(vdev)) {
209 		dev_info(vdev->device, "reset\n");
210 		return vfio_platform_acpi_call_reset(vdev, extra_dbg);
211 	} else if (vdev->of_reset) {
212 		dev_info(vdev->device, "reset\n");
213 		return vdev->of_reset(vdev);
214 	}
215 
216 	dev_warn(vdev->device, "no reset function found!\n");
217 	return -EINVAL;
218 }
219 
vfio_platform_close_device(struct vfio_device * core_vdev)220 static void vfio_platform_close_device(struct vfio_device *core_vdev)
221 {
222 	struct vfio_platform_device *vdev =
223 		container_of(core_vdev, struct vfio_platform_device, vdev);
224 	const char *extra_dbg = NULL;
225 	int ret;
226 
227 	ret = vfio_platform_call_reset(vdev, &extra_dbg);
228 	if (WARN_ON(ret && vdev->reset_required)) {
229 		dev_warn(
230 			vdev->device,
231 			"reset driver is required and reset call failed in release (%d) %s\n",
232 			ret, extra_dbg ? extra_dbg : "");
233 	}
234 	pm_runtime_put(vdev->device);
235 	vfio_platform_regions_cleanup(vdev);
236 	vfio_platform_irq_cleanup(vdev);
237 }
238 
vfio_platform_open_device(struct vfio_device * core_vdev)239 static int vfio_platform_open_device(struct vfio_device *core_vdev)
240 {
241 	struct vfio_platform_device *vdev =
242 		container_of(core_vdev, struct vfio_platform_device, vdev);
243 	const char *extra_dbg = NULL;
244 	int ret;
245 
246 	ret = vfio_platform_regions_init(vdev);
247 	if (ret)
248 		return ret;
249 
250 	ret = vfio_platform_irq_init(vdev);
251 	if (ret)
252 		goto err_irq;
253 
254 	ret = pm_runtime_get_sync(vdev->device);
255 	if (ret < 0)
256 		goto err_rst;
257 
258 	ret = vfio_platform_call_reset(vdev, &extra_dbg);
259 	if (ret && vdev->reset_required) {
260 		dev_warn(
261 			vdev->device,
262 			"reset driver is required and reset call failed in open (%d) %s\n",
263 			ret, extra_dbg ? extra_dbg : "");
264 		goto err_rst;
265 	}
266 	return 0;
267 
268 err_rst:
269 	pm_runtime_put(vdev->device);
270 	vfio_platform_irq_cleanup(vdev);
271 err_irq:
272 	vfio_platform_regions_cleanup(vdev);
273 	return ret;
274 }
275 
vfio_platform_ioctl(struct vfio_device * core_vdev,unsigned int cmd,unsigned long arg)276 static long vfio_platform_ioctl(struct vfio_device *core_vdev,
277 				unsigned int cmd, unsigned long arg)
278 {
279 	struct vfio_platform_device *vdev =
280 		container_of(core_vdev, struct vfio_platform_device, vdev);
281 
282 	unsigned long minsz;
283 
284 	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 
295 		if (vfio_platform_has_reset(vdev))
296 			vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
297 		info.flags = vdev->flags;
298 		info.num_regions = vdev->num_regions;
299 		info.num_irqs = vdev->num_irqs;
300 
301 		return copy_to_user((void __user *)arg, &info, minsz) ?
302 			-EFAULT : 0;
303 
304 	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
305 		struct vfio_region_info info;
306 
307 		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 
323 		return copy_to_user((void __user *)arg, &info, minsz) ?
324 			-EFAULT : 0;
325 
326 	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
327 		struct vfio_irq_info info;
328 
329 		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 
343 		return copy_to_user((void __user *)arg, &info, minsz) ?
344 			-EFAULT : 0;
345 
346 	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
347 		struct vfio_irq_set hdr;
348 		u8 *data = NULL;
349 		int ret = 0;
350 		size_t data_size = 0;
351 
352 		minsz = offsetofend(struct vfio_irq_set, count);
353 
354 		if (copy_from_user(&hdr, (void __user *)arg, minsz))
355 			return -EFAULT;
356 
357 		ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
358 						 vdev->num_irqs, &data_size);
359 		if (ret)
360 			return ret;
361 
362 		if (data_size) {
363 			data = memdup_user((void __user *)(arg + minsz),
364 					    data_size);
365 			if (IS_ERR(data))
366 				return PTR_ERR(data);
367 		}
368 
369 		mutex_lock(&vdev->igate);
370 
371 		ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
372 						   hdr.start, hdr.count, data);
373 		mutex_unlock(&vdev->igate);
374 		kfree(data);
375 
376 		return ret;
377 
378 	} else if (cmd == VFIO_DEVICE_RESET) {
379 		return vfio_platform_call_reset(vdev, NULL);
380 	}
381 
382 	return -ENOTTY;
383 }
384 
vfio_platform_read_mmio(struct vfio_platform_region * reg,char __user * buf,size_t count,loff_t off)385 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
386 				       char __user *buf, size_t count,
387 				       loff_t off)
388 {
389 	unsigned int done = 0;
390 
391 	if (!reg->ioaddr) {
392 		reg->ioaddr =
393 			ioremap(reg->addr, reg->size);
394 
395 		if (!reg->ioaddr)
396 			return -ENOMEM;
397 	}
398 
399 	while (count) {
400 		size_t filled;
401 
402 		if (count >= 4 && !(off % 4)) {
403 			u32 val;
404 
405 			val = ioread32(reg->ioaddr + off);
406 			if (copy_to_user(buf, &val, 4))
407 				goto err;
408 
409 			filled = 4;
410 		} else if (count >= 2 && !(off % 2)) {
411 			u16 val;
412 
413 			val = ioread16(reg->ioaddr + off);
414 			if (copy_to_user(buf, &val, 2))
415 				goto err;
416 
417 			filled = 2;
418 		} else {
419 			u8 val;
420 
421 			val = ioread8(reg->ioaddr + off);
422 			if (copy_to_user(buf, &val, 1))
423 				goto err;
424 
425 			filled = 1;
426 		}
427 
428 
429 		count -= filled;
430 		done += filled;
431 		off += filled;
432 		buf += filled;
433 	}
434 
435 	return done;
436 err:
437 	return -EFAULT;
438 }
439 
vfio_platform_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)440 static ssize_t vfio_platform_read(struct vfio_device *core_vdev,
441 				  char __user *buf, size_t count, loff_t *ppos)
442 {
443 	struct vfio_platform_device *vdev =
444 		container_of(core_vdev, struct vfio_platform_device, vdev);
445 	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
446 	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
447 
448 	if (index >= vdev->num_regions)
449 		return -EINVAL;
450 
451 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
452 		return -EINVAL;
453 
454 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
455 		return vfio_platform_read_mmio(&vdev->regions[index],
456 							buf, count, off);
457 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
458 		return -EINVAL; /* not implemented */
459 
460 	return -EINVAL;
461 }
462 
vfio_platform_write_mmio(struct vfio_platform_region * reg,const char __user * buf,size_t count,loff_t off)463 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
464 					const char __user *buf, size_t count,
465 					loff_t off)
466 {
467 	unsigned int done = 0;
468 
469 	if (!reg->ioaddr) {
470 		reg->ioaddr =
471 			ioremap(reg->addr, reg->size);
472 
473 		if (!reg->ioaddr)
474 			return -ENOMEM;
475 	}
476 
477 	while (count) {
478 		size_t filled;
479 
480 		if (count >= 4 && !(off % 4)) {
481 			u32 val;
482 
483 			if (copy_from_user(&val, buf, 4))
484 				goto err;
485 			iowrite32(val, reg->ioaddr + off);
486 
487 			filled = 4;
488 		} else if (count >= 2 && !(off % 2)) {
489 			u16 val;
490 
491 			if (copy_from_user(&val, buf, 2))
492 				goto err;
493 			iowrite16(val, reg->ioaddr + off);
494 
495 			filled = 2;
496 		} else {
497 			u8 val;
498 
499 			if (copy_from_user(&val, buf, 1))
500 				goto err;
501 			iowrite8(val, reg->ioaddr + off);
502 
503 			filled = 1;
504 		}
505 
506 		count -= filled;
507 		done += filled;
508 		off += filled;
509 		buf += filled;
510 	}
511 
512 	return done;
513 err:
514 	return -EFAULT;
515 }
516 
vfio_platform_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)517 static ssize_t vfio_platform_write(struct vfio_device *core_vdev, const char __user *buf,
518 				   size_t count, loff_t *ppos)
519 {
520 	struct vfio_platform_device *vdev =
521 		container_of(core_vdev, struct vfio_platform_device, vdev);
522 	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
523 	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
524 
525 	if (index >= vdev->num_regions)
526 		return -EINVAL;
527 
528 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
529 		return -EINVAL;
530 
531 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
532 		return vfio_platform_write_mmio(&vdev->regions[index],
533 							buf, count, off);
534 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
535 		return -EINVAL; /* not implemented */
536 
537 	return -EINVAL;
538 }
539 
vfio_platform_mmap_mmio(struct vfio_platform_region region,struct vm_area_struct * vma)540 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
541 				   struct vm_area_struct *vma)
542 {
543 	u64 req_len, pgoff, req_start;
544 
545 	req_len = vma->vm_end - vma->vm_start;
546 	pgoff = vma->vm_pgoff &
547 		((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
548 	req_start = pgoff << PAGE_SHIFT;
549 
550 	if (region.size < PAGE_SIZE || req_start + req_len > region.size)
551 		return -EINVAL;
552 
553 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
554 	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
555 
556 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
557 			       req_len, vma->vm_page_prot);
558 }
559 
vfio_platform_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)560 static int vfio_platform_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
561 {
562 	struct vfio_platform_device *vdev =
563 		container_of(core_vdev, struct vfio_platform_device, vdev);
564 	unsigned int index;
565 
566 	index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
567 
568 	if (vma->vm_end < vma->vm_start)
569 		return -EINVAL;
570 	if (!(vma->vm_flags & VM_SHARED))
571 		return -EINVAL;
572 	if (index >= vdev->num_regions)
573 		return -EINVAL;
574 	if (vma->vm_start & ~PAGE_MASK)
575 		return -EINVAL;
576 	if (vma->vm_end & ~PAGE_MASK)
577 		return -EINVAL;
578 
579 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
580 		return -EINVAL;
581 
582 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
583 			&& (vma->vm_flags & VM_READ))
584 		return -EINVAL;
585 
586 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
587 			&& (vma->vm_flags & VM_WRITE))
588 		return -EINVAL;
589 
590 	vma->vm_private_data = vdev;
591 
592 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
593 		return vfio_platform_mmap_mmio(vdev->regions[index], vma);
594 
595 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
596 		return -EINVAL; /* not implemented */
597 
598 	return -EINVAL;
599 }
600 
601 static const struct vfio_device_ops vfio_platform_ops = {
602 	.name		= "vfio-platform",
603 	.open_device	= vfio_platform_open_device,
604 	.close_device	= vfio_platform_close_device,
605 	.ioctl		= vfio_platform_ioctl,
606 	.read		= vfio_platform_read,
607 	.write		= vfio_platform_write,
608 	.mmap		= vfio_platform_mmap,
609 };
610 
vfio_platform_of_probe(struct vfio_platform_device * vdev,struct device * dev)611 static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
612 			   struct device *dev)
613 {
614 	int ret;
615 
616 	ret = device_property_read_string(dev, "compatible",
617 					  &vdev->compat);
618 	if (ret)
619 		dev_err(dev, "Cannot retrieve compat for %s\n", vdev->name);
620 
621 	return ret;
622 }
623 
624 /*
625  * There can be two kernel build combinations. One build where
626  * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
627  *
628  * In the first case, vfio_platform_acpi_probe will return since
629  * acpi_disabled is 1. DT user will not see any kind of messages from
630  * ACPI.
631  *
632  * In the second case, both DT and ACPI is compiled in but the system is
633  * booting with any of these combinations.
634  *
635  * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
636  * terminates immediately without any messages.
637  *
638  * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
639  * valid checks. We cannot claim that this system is DT.
640  */
vfio_platform_probe_common(struct vfio_platform_device * vdev,struct device * dev)641 int vfio_platform_probe_common(struct vfio_platform_device *vdev,
642 			       struct device *dev)
643 {
644 	struct iommu_group *group;
645 	int ret;
646 
647 	vfio_init_group_dev(&vdev->vdev, dev, &vfio_platform_ops);
648 
649 	ret = vfio_platform_acpi_probe(vdev, dev);
650 	if (ret)
651 		ret = vfio_platform_of_probe(vdev, dev);
652 
653 	if (ret)
654 		goto out_uninit;
655 
656 	vdev->device = dev;
657 
658 	ret = vfio_platform_get_reset(vdev);
659 	if (ret && vdev->reset_required) {
660 		dev_err(dev, "No reset function found for device %s\n",
661 			vdev->name);
662 		goto out_uninit;
663 	}
664 
665 	group = vfio_iommu_group_get(dev);
666 	if (!group) {
667 		dev_err(dev, "No IOMMU group for device %s\n", vdev->name);
668 		ret = -EINVAL;
669 		goto put_reset;
670 	}
671 
672 	ret = vfio_register_group_dev(&vdev->vdev);
673 	if (ret)
674 		goto put_iommu;
675 
676 	mutex_init(&vdev->igate);
677 
678 	pm_runtime_enable(dev);
679 	return 0;
680 
681 put_iommu:
682 	vfio_iommu_group_put(group, dev);
683 put_reset:
684 	vfio_platform_put_reset(vdev);
685 out_uninit:
686 	vfio_uninit_group_dev(&vdev->vdev);
687 	return ret;
688 }
689 EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
690 
vfio_platform_remove_common(struct vfio_platform_device * vdev)691 void vfio_platform_remove_common(struct vfio_platform_device *vdev)
692 {
693 	vfio_unregister_group_dev(&vdev->vdev);
694 
695 	pm_runtime_disable(vdev->device);
696 	vfio_platform_put_reset(vdev);
697 	vfio_uninit_group_dev(&vdev->vdev);
698 	vfio_iommu_group_put(vdev->vdev.dev->iommu_group, vdev->vdev.dev);
699 }
700 EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
701 
__vfio_platform_register_reset(struct vfio_platform_reset_node * node)702 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
703 {
704 	mutex_lock(&driver_lock);
705 	list_add(&node->link, &reset_list);
706 	mutex_unlock(&driver_lock);
707 }
708 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
709 
vfio_platform_unregister_reset(const char * compat,vfio_platform_reset_fn_t fn)710 void vfio_platform_unregister_reset(const char *compat,
711 				    vfio_platform_reset_fn_t fn)
712 {
713 	struct vfio_platform_reset_node *iter, *temp;
714 
715 	mutex_lock(&driver_lock);
716 	list_for_each_entry_safe(iter, temp, &reset_list, link) {
717 		if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
718 			list_del(&iter->link);
719 			break;
720 		}
721 	}
722 
723 	mutex_unlock(&driver_lock);
724 
725 }
726 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
727 
728 MODULE_VERSION(DRIVER_VERSION);
729 MODULE_LICENSE("GPL v2");
730 MODULE_AUTHOR(DRIVER_AUTHOR);
731 MODULE_DESCRIPTION(DRIVER_DESC);
732