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#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
18static bool reset_required = true;
19module_param(reset_required, bool, 0444);
20MODULE_PARM_DESC(reset_required, "override reset requirement (default: 1)");
21
22/* probing devices from the linux platform bus */
23
24static 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 int i;
29
30 for (i = 0; i < dev->num_resources; i++) {
31 struct resource *r = &dev->resource[i];
32
33 if (resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) {
34 if (!num)
35 return r;
36
37 num--;
38 }
39 }
40 return NULL;
41}
42
43static int get_platform_irq(struct vfio_platform_device *vdev, int i)
44{
45 struct platform_device *pdev = (struct platform_device *) vdev->opaque;
46
47 return platform_get_irq(pdev, i);
48}
49
50static int vfio_platform_probe(struct platform_device *pdev)
51{
52 struct vfio_platform_device *vdev;
53 int ret;
54
55 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
56 if (!vdev)
57 return -ENOMEM;
58
59 vdev->opaque = (void *) pdev;
60 vdev->name = pdev->name;
61 vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
62 vdev->get_resource = get_platform_resource;
63 vdev->get_irq = get_platform_irq;
64 vdev->parent_module = THIS_MODULE;
65 vdev->reset_required = reset_required;
66
67 ret = vfio_platform_probe_common(vdev, &pdev->dev);
68 if (ret)
69 kfree(vdev);
70
71 return ret;
72}
73
74static int vfio_platform_remove(struct platform_device *pdev)
75{
76 struct vfio_platform_device *vdev;
77
78 vdev = vfio_platform_remove_common(&pdev->dev);
79 if (vdev) {
80 kfree(vdev);
81 return 0;
82 }
83
84 return -EINVAL;
85}
86
87static struct platform_driver vfio_platform_driver = {
88 .probe = vfio_platform_probe,
89 .remove = vfio_platform_remove,
90 .driver = {
91 .name = "vfio-platform",
92 },
93};
94
95module_platform_driver(vfio_platform_driver);
96
97MODULE_VERSION(DRIVER_VERSION);
98MODULE_LICENSE("GPL v2");
99MODULE_AUTHOR(DRIVER_AUTHOR);
100MODULE_DESCRIPTION(DRIVER_DESC);
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#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/vfio.h>
18#include <linux/platform_device.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 for platform devices - User Level meta-driver"
25
26static bool reset_required = true;
27module_param(reset_required, bool, 0444);
28MODULE_PARM_DESC(reset_required, "override reset requirement (default: 1)");
29
30/* probing devices from the linux platform bus */
31
32static struct resource *get_platform_resource(struct vfio_platform_device *vdev,
33 int num)
34{
35 struct platform_device *dev = (struct platform_device *) vdev->opaque;
36 int i;
37
38 for (i = 0; i < dev->num_resources; i++) {
39 struct resource *r = &dev->resource[i];
40
41 if (resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) {
42 if (!num)
43 return r;
44
45 num--;
46 }
47 }
48 return NULL;
49}
50
51static int get_platform_irq(struct vfio_platform_device *vdev, int i)
52{
53 struct platform_device *pdev = (struct platform_device *) vdev->opaque;
54
55 return platform_get_irq(pdev, i);
56}
57
58static int vfio_platform_probe(struct platform_device *pdev)
59{
60 struct vfio_platform_device *vdev;
61 int ret;
62
63 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
64 if (!vdev)
65 return -ENOMEM;
66
67 vdev->opaque = (void *) pdev;
68 vdev->name = pdev->name;
69 vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
70 vdev->get_resource = get_platform_resource;
71 vdev->get_irq = get_platform_irq;
72 vdev->parent_module = THIS_MODULE;
73 vdev->reset_required = reset_required;
74
75 ret = vfio_platform_probe_common(vdev, &pdev->dev);
76 if (ret)
77 kfree(vdev);
78
79 return ret;
80}
81
82static int vfio_platform_remove(struct platform_device *pdev)
83{
84 struct vfio_platform_device *vdev;
85
86 vdev = vfio_platform_remove_common(&pdev->dev);
87 if (vdev) {
88 kfree(vdev);
89 return 0;
90 }
91
92 return -EINVAL;
93}
94
95static struct platform_driver vfio_platform_driver = {
96 .probe = vfio_platform_probe,
97 .remove = vfio_platform_remove,
98 .driver = {
99 .name = "vfio-platform",
100 },
101};
102
103module_platform_driver(vfio_platform_driver);
104
105MODULE_VERSION(DRIVER_VERSION);
106MODULE_LICENSE("GPL v2");
107MODULE_AUTHOR(DRIVER_AUTHOR);
108MODULE_DESCRIPTION(DRIVER_DESC);