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