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