xref: /linux/drivers/vfio/platform/vfio_platform_common.c (revision a3ebb59eee2e558e8f8f27fc3f75cd367f17cd8e)
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_ACCOUNT);
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 		vdev->regions[i].addr = res->start;
154 		vdev->regions[i].size = resource_size(res);
155 		vdev->regions[i].flags = 0;
156 
157 		switch (resource_type(res)) {
158 		case IORESOURCE_MEM:
159 			vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
160 			vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
161 			if (!(res->flags & IORESOURCE_READONLY))
162 				vdev->regions[i].flags |=
163 					VFIO_REGION_INFO_FLAG_WRITE;
164 
165 			/*
166 			 * Only regions addressed with PAGE granularity may be
167 			 * MMAPed securely.
168 			 */
169 			if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
170 					!(vdev->regions[i].size & ~PAGE_MASK))
171 				vdev->regions[i].flags |=
172 					VFIO_REGION_INFO_FLAG_MMAP;
173 
174 			break;
175 		case IORESOURCE_IO:
176 			vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
177 			break;
178 		default:
179 			goto err;
180 		}
181 	}
182 
183 	vdev->num_regions = cnt;
184 
185 	return 0;
186 err:
187 	kfree(vdev->regions);
188 	return -EINVAL;
189 }
190 
vfio_platform_regions_cleanup(struct vfio_platform_device * vdev)191 static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
192 {
193 	int i;
194 
195 	for (i = 0; i < vdev->num_regions; i++)
196 		iounmap(vdev->regions[i].ioaddr);
197 
198 	vdev->num_regions = 0;
199 	kfree(vdev->regions);
200 }
201 
vfio_platform_call_reset(struct vfio_platform_device * vdev,const char ** extra_dbg)202 static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
203 				    const char **extra_dbg)
204 {
205 	if (VFIO_PLATFORM_IS_ACPI(vdev)) {
206 		dev_info(vdev->device, "reset\n");
207 		return vfio_platform_acpi_call_reset(vdev, extra_dbg);
208 	} else if (vdev->of_reset) {
209 		dev_info(vdev->device, "reset\n");
210 		return vdev->of_reset(vdev);
211 	}
212 
213 	dev_warn(vdev->device, "no reset function found!\n");
214 	return -EINVAL;
215 }
216 
vfio_platform_close_device(struct vfio_device * core_vdev)217 void vfio_platform_close_device(struct vfio_device *core_vdev)
218 {
219 	struct vfio_platform_device *vdev =
220 		container_of(core_vdev, struct vfio_platform_device, vdev);
221 	const char *extra_dbg = NULL;
222 	int ret;
223 
224 	ret = vfio_platform_call_reset(vdev, &extra_dbg);
225 	if (WARN_ON(ret && vdev->reset_required)) {
226 		dev_warn(
227 			vdev->device,
228 			"reset driver is required and reset call failed in release (%d) %s\n",
229 			ret, extra_dbg ? extra_dbg : "");
230 	}
231 	pm_runtime_put(vdev->device);
232 	vfio_platform_regions_cleanup(vdev);
233 	vfio_platform_irq_cleanup(vdev);
234 }
235 EXPORT_SYMBOL_GPL(vfio_platform_close_device);
236 
vfio_platform_open_device(struct vfio_device * core_vdev)237 int vfio_platform_open_device(struct vfio_device *core_vdev)
238 {
239 	struct vfio_platform_device *vdev =
240 		container_of(core_vdev, struct vfio_platform_device, vdev);
241 	const char *extra_dbg = NULL;
242 	int ret;
243 
244 	ret = vfio_platform_regions_init(vdev);
245 	if (ret)
246 		return ret;
247 
248 	ret = vfio_platform_irq_init(vdev);
249 	if (ret)
250 		goto err_irq;
251 
252 	ret = pm_runtime_get_sync(vdev->device);
253 	if (ret < 0)
254 		goto err_rst;
255 
256 	ret = vfio_platform_call_reset(vdev, &extra_dbg);
257 	if (ret && vdev->reset_required) {
258 		dev_warn(
259 			vdev->device,
260 			"reset driver is required and reset call failed in open (%d) %s\n",
261 			ret, extra_dbg ? extra_dbg : "");
262 		goto err_rst;
263 	}
264 	return 0;
265 
266 err_rst:
267 	pm_runtime_put(vdev->device);
268 	vfio_platform_irq_cleanup(vdev);
269 err_irq:
270 	vfio_platform_regions_cleanup(vdev);
271 	return ret;
272 }
273 EXPORT_SYMBOL_GPL(vfio_platform_open_device);
274 
vfio_platform_ioctl_get_region_info(struct vfio_device * core_vdev,struct vfio_region_info * info,struct vfio_info_cap * caps)275 int vfio_platform_ioctl_get_region_info(struct vfio_device *core_vdev,
276 					struct vfio_region_info *info,
277 					struct vfio_info_cap *caps)
278 {
279 	struct vfio_platform_device *vdev =
280 		container_of(core_vdev, struct vfio_platform_device, vdev);
281 
282 	if (info->index >= vdev->num_regions)
283 		return -EINVAL;
284 
285 	/* map offset to the physical address  */
286 	info->offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info->index);
287 	info->size = vdev->regions[info->index].size;
288 	info->flags = vdev->regions[info->index].flags;
289 	return 0;
290 }
291 EXPORT_SYMBOL_GPL(vfio_platform_ioctl_get_region_info);
292 
vfio_platform_ioctl(struct vfio_device * core_vdev,unsigned int cmd,unsigned long arg)293 long vfio_platform_ioctl(struct vfio_device *core_vdev,
294 			 unsigned int cmd, unsigned long arg)
295 {
296 	struct vfio_platform_device *vdev =
297 		container_of(core_vdev, struct vfio_platform_device, vdev);
298 
299 	unsigned long minsz;
300 
301 	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 
312 		if (vfio_platform_has_reset(vdev))
313 			vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
314 		info.flags = vdev->flags;
315 		info.num_regions = vdev->num_regions;
316 		info.num_irqs = vdev->num_irqs;
317 
318 		return copy_to_user((void __user *)arg, &info, minsz) ?
319 			-EFAULT : 0;
320 
321 	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
322 		struct vfio_irq_info info;
323 
324 		minsz = offsetofend(struct vfio_irq_info, count);
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_irqs)
333 			return -EINVAL;
334 
335 		info.flags = vdev->irqs[info.index].flags;
336 		info.count = vdev->irqs[info.index].count;
337 
338 		return copy_to_user((void __user *)arg, &info, minsz) ?
339 			-EFAULT : 0;
340 
341 	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
342 		struct vfio_irq_set hdr;
343 		u8 *data = NULL;
344 		int ret = 0;
345 		size_t data_size = 0;
346 
347 		minsz = offsetofend(struct vfio_irq_set, count);
348 
349 		if (copy_from_user(&hdr, (void __user *)arg, minsz))
350 			return -EFAULT;
351 
352 		ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
353 						 vdev->num_irqs, &data_size);
354 		if (ret)
355 			return ret;
356 
357 		if (data_size) {
358 			data = memdup_user((void __user *)(arg + minsz),
359 					    data_size);
360 			if (IS_ERR(data))
361 				return PTR_ERR(data);
362 		}
363 
364 		mutex_lock(&vdev->igate);
365 
366 		ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
367 						   hdr.start, hdr.count, data);
368 		mutex_unlock(&vdev->igate);
369 		kfree(data);
370 
371 		return ret;
372 
373 	} else if (cmd == VFIO_DEVICE_RESET) {
374 		return vfio_platform_call_reset(vdev, NULL);
375 	}
376 
377 	return -ENOTTY;
378 }
379 EXPORT_SYMBOL_GPL(vfio_platform_ioctl);
380 
vfio_platform_read_mmio(struct vfio_platform_region * reg,char __user * buf,size_t count,loff_t off)381 static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
382 				       char __user *buf, size_t count,
383 				       loff_t off)
384 {
385 	unsigned int done = 0;
386 
387 	if (off >= reg->size)
388 		return -EINVAL;
389 
390 	count = min_t(size_t, count, reg->size - off);
391 
392 	if (!reg->ioaddr) {
393 		reg->ioaddr =
394 			ioremap(reg->addr, reg->size);
395 
396 		if (!reg->ioaddr)
397 			return -ENOMEM;
398 	}
399 
400 	while (count) {
401 		size_t filled;
402 
403 		if (count >= 4 && !(off % 4)) {
404 			u32 val;
405 
406 			val = ioread32(reg->ioaddr + off);
407 			if (copy_to_user(buf, &val, 4))
408 				goto err;
409 
410 			filled = 4;
411 		} else if (count >= 2 && !(off % 2)) {
412 			u16 val;
413 
414 			val = ioread16(reg->ioaddr + off);
415 			if (copy_to_user(buf, &val, 2))
416 				goto err;
417 
418 			filled = 2;
419 		} else {
420 			u8 val;
421 
422 			val = ioread8(reg->ioaddr + off);
423 			if (copy_to_user(buf, &val, 1))
424 				goto err;
425 
426 			filled = 1;
427 		}
428 
429 
430 		count -= filled;
431 		done += filled;
432 		off += filled;
433 		buf += filled;
434 	}
435 
436 	return done;
437 err:
438 	return -EFAULT;
439 }
440 
vfio_platform_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)441 ssize_t vfio_platform_read(struct vfio_device *core_vdev,
442 			   char __user *buf, size_t count, loff_t *ppos)
443 {
444 	struct vfio_platform_device *vdev =
445 		container_of(core_vdev, struct vfio_platform_device, vdev);
446 	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
447 	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
448 
449 	if (index >= vdev->num_regions)
450 		return -EINVAL;
451 
452 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
453 		return -EINVAL;
454 
455 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
456 		return vfio_platform_read_mmio(&vdev->regions[index],
457 							buf, count, off);
458 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
459 		return -EINVAL; /* not implemented */
460 
461 	return -EINVAL;
462 }
463 EXPORT_SYMBOL_GPL(vfio_platform_read);
464 
vfio_platform_write_mmio(struct vfio_platform_region * reg,const char __user * buf,size_t count,loff_t off)465 static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
466 					const char __user *buf, size_t count,
467 					loff_t off)
468 {
469 	unsigned int done = 0;
470 
471 	if (off >= reg->size)
472 		return -EINVAL;
473 
474 	count = min_t(size_t, count, reg->size - off);
475 
476 	if (!reg->ioaddr) {
477 		reg->ioaddr =
478 			ioremap(reg->addr, reg->size);
479 
480 		if (!reg->ioaddr)
481 			return -ENOMEM;
482 	}
483 
484 	while (count) {
485 		size_t filled;
486 
487 		if (count >= 4 && !(off % 4)) {
488 			u32 val;
489 
490 			if (copy_from_user(&val, buf, 4))
491 				goto err;
492 			iowrite32(val, reg->ioaddr + off);
493 
494 			filled = 4;
495 		} else if (count >= 2 && !(off % 2)) {
496 			u16 val;
497 
498 			if (copy_from_user(&val, buf, 2))
499 				goto err;
500 			iowrite16(val, reg->ioaddr + off);
501 
502 			filled = 2;
503 		} else {
504 			u8 val;
505 
506 			if (copy_from_user(&val, buf, 1))
507 				goto err;
508 			iowrite8(val, reg->ioaddr + off);
509 
510 			filled = 1;
511 		}
512 
513 		count -= filled;
514 		done += filled;
515 		off += filled;
516 		buf += filled;
517 	}
518 
519 	return done;
520 err:
521 	return -EFAULT;
522 }
523 
vfio_platform_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)524 ssize_t vfio_platform_write(struct vfio_device *core_vdev, const char __user *buf,
525 			    size_t count, loff_t *ppos)
526 {
527 	struct vfio_platform_device *vdev =
528 		container_of(core_vdev, struct vfio_platform_device, vdev);
529 	unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
530 	loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
531 
532 	if (index >= vdev->num_regions)
533 		return -EINVAL;
534 
535 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
536 		return -EINVAL;
537 
538 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
539 		return vfio_platform_write_mmio(&vdev->regions[index],
540 							buf, count, off);
541 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
542 		return -EINVAL; /* not implemented */
543 
544 	return -EINVAL;
545 }
546 EXPORT_SYMBOL_GPL(vfio_platform_write);
547 
vfio_platform_mmap_mmio(struct vfio_platform_region region,struct vm_area_struct * vma)548 static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
549 				   struct vm_area_struct *vma)
550 {
551 	u64 req_len, pgoff, req_start;
552 
553 	req_len = vma->vm_end - vma->vm_start;
554 	pgoff = vma->vm_pgoff &
555 		((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
556 	req_start = pgoff << PAGE_SHIFT;
557 
558 	if (region.size < PAGE_SIZE || req_start + req_len > region.size)
559 		return -EINVAL;
560 
561 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
562 	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
563 
564 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
565 			       req_len, vma->vm_page_prot);
566 }
567 
vfio_platform_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)568 int vfio_platform_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)
569 {
570 	struct vfio_platform_device *vdev =
571 		container_of(core_vdev, struct vfio_platform_device, vdev);
572 	unsigned int index;
573 
574 	index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
575 
576 	if (vma->vm_end < vma->vm_start)
577 		return -EINVAL;
578 	if (!(vma->vm_flags & VM_SHARED))
579 		return -EINVAL;
580 	if (index >= vdev->num_regions)
581 		return -EINVAL;
582 	if (vma->vm_start & ~PAGE_MASK)
583 		return -EINVAL;
584 	if (vma->vm_end & ~PAGE_MASK)
585 		return -EINVAL;
586 
587 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
588 		return -EINVAL;
589 
590 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
591 			&& (vma->vm_flags & VM_READ))
592 		return -EINVAL;
593 
594 	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
595 			&& (vma->vm_flags & VM_WRITE))
596 		return -EINVAL;
597 
598 	vma->vm_private_data = vdev;
599 
600 	if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
601 		return vfio_platform_mmap_mmio(vdev->regions[index], vma);
602 
603 	else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
604 		return -EINVAL; /* not implemented */
605 
606 	return -EINVAL;
607 }
608 EXPORT_SYMBOL_GPL(vfio_platform_mmap);
609 
vfio_platform_of_probe(struct vfio_platform_device * vdev,struct device * dev)610 static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
611 			   struct device *dev)
612 {
613 	int ret;
614 
615 	ret = device_property_read_string(dev, "compatible",
616 					  &vdev->compat);
617 	if (ret)
618 		dev_err(dev, "Cannot retrieve compat for %s\n", vdev->name);
619 
620 	return ret;
621 }
622 
623 /*
624  * There can be two kernel build combinations. One build where
625  * ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
626  *
627  * In the first case, vfio_platform_acpi_probe will return since
628  * acpi_disabled is 1. DT user will not see any kind of messages from
629  * ACPI.
630  *
631  * In the second case, both DT and ACPI is compiled in but the system is
632  * booting with any of these combinations.
633  *
634  * If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
635  * terminates immediately without any messages.
636  *
637  * If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
638  * valid checks. We cannot claim that this system is DT.
639  */
vfio_platform_init_common(struct vfio_platform_device * vdev)640 int vfio_platform_init_common(struct vfio_platform_device *vdev)
641 {
642 	int ret;
643 	struct device *dev = vdev->vdev.dev;
644 
645 	ret = vfio_platform_acpi_probe(vdev, dev);
646 	if (ret)
647 		ret = vfio_platform_of_probe(vdev, dev);
648 
649 	if (ret)
650 		return ret;
651 
652 	vdev->device = dev;
653 	mutex_init(&vdev->igate);
654 
655 	ret = vfio_platform_get_reset(vdev);
656 	if (ret && vdev->reset_required) {
657 		dev_err(dev, "No reset function found for device %s\n",
658 			vdev->name);
659 		return ret;
660 	}
661 
662 	return 0;
663 }
664 EXPORT_SYMBOL_GPL(vfio_platform_init_common);
665 
vfio_platform_release_common(struct vfio_platform_device * vdev)666 void vfio_platform_release_common(struct vfio_platform_device *vdev)
667 {
668 	vfio_platform_put_reset(vdev);
669 }
670 EXPORT_SYMBOL_GPL(vfio_platform_release_common);
671 
__vfio_platform_register_reset(struct vfio_platform_reset_node * node)672 void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
673 {
674 	mutex_lock(&driver_lock);
675 	list_add(&node->link, &reset_list);
676 	mutex_unlock(&driver_lock);
677 }
678 EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
679 
vfio_platform_unregister_reset(const char * compat,vfio_platform_reset_fn_t fn)680 void vfio_platform_unregister_reset(const char *compat,
681 				    vfio_platform_reset_fn_t fn)
682 {
683 	struct vfio_platform_reset_node *iter, *temp;
684 
685 	mutex_lock(&driver_lock);
686 	list_for_each_entry_safe(iter, temp, &reset_list, link) {
687 		if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
688 			list_del(&iter->link);
689 			break;
690 		}
691 	}
692 
693 	mutex_unlock(&driver_lock);
694 
695 }
696 EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
697 
698 MODULE_VERSION(DRIVER_VERSION);
699 MODULE_LICENSE("GPL v2");
700 MODULE_AUTHOR(DRIVER_AUTHOR);
701 MODULE_DESCRIPTION(DRIVER_DESC);
702