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 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/vfio.h>
10 #include <linux/platform_device.h>
11 
12 #include "vfio_platform_private.h"
13 
14 #define DRIVER_VERSION  "0.10"
15 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis@virtualopensystems.com>"
16 #define DRIVER_DESC     "VFIO for platform devices - User Level meta-driver"
17 
18 static bool reset_required = true;
19 module_param(reset_required, bool, 0444);
20 MODULE_PARM_DESC(reset_required, "override reset requirement (default: 1)");
21 
22 /* probing devices from the linux platform bus */
23 
24 static struct resource *get_platform_resource(struct vfio_platform_device *vdev,
25 					      int num)
26 {
27 	struct platform_device *dev = (struct platform_device *) vdev->opaque;
28 
29 	return platform_get_mem_or_io(dev, num);
30 }
31 
32 static int get_platform_irq(struct vfio_platform_device *vdev, int i)
33 {
34 	struct platform_device *pdev = (struct platform_device *) vdev->opaque;
35 
36 	return platform_get_irq_optional(pdev, i);
37 }
38 
39 static int vfio_platform_probe(struct platform_device *pdev)
40 {
41 	struct vfio_platform_device *vdev;
42 	int ret;
43 
44 	vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
45 	if (!vdev)
46 		return -ENOMEM;
47 
48 	vdev->opaque = (void *) pdev;
49 	vdev->name = pdev->name;
50 	vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
51 	vdev->get_resource = get_platform_resource;
52 	vdev->get_irq = get_platform_irq;
53 	vdev->parent_module = THIS_MODULE;
54 	vdev->reset_required = reset_required;
55 
56 	ret = vfio_platform_probe_common(vdev, &pdev->dev);
57 	if (ret)
58 		kfree(vdev);
59 
60 	return ret;
61 }
62 
63 static int vfio_platform_remove(struct platform_device *pdev)
64 {
65 	struct vfio_platform_device *vdev;
66 
67 	vdev = vfio_platform_remove_common(&pdev->dev);
68 	if (vdev) {
69 		kfree(vdev);
70 		return 0;
71 	}
72 
73 	return -EINVAL;
74 }
75 
76 static struct platform_driver vfio_platform_driver = {
77 	.probe		= vfio_platform_probe,
78 	.remove		= vfio_platform_remove,
79 	.driver	= {
80 		.name	= "vfio-platform",
81 	},
82 };
83 
84 module_platform_driver(vfio_platform_driver);
85 
86 MODULE_VERSION(DRIVER_VERSION);
87 MODULE_LICENSE("GPL v2");
88 MODULE_AUTHOR(DRIVER_AUTHOR);
89 MODULE_DESCRIPTION(DRIVER_DESC);
90