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/pm_runtime.h>
11#include <linux/amba/bus.h>
12
13#include "vfio_platform_private.h"
14
15#define DRIVER_VERSION "0.10"
16#define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
17#define DRIVER_DESC "VFIO for AMBA devices - User Level meta-driver"
18
19/* probing devices from the AMBA bus */
20
21static struct resource *get_amba_resource(struct vfio_platform_device *vdev,
22 int i)
23{
24 struct amba_device *adev = (struct amba_device *) vdev->opaque;
25
26 if (i == 0)
27 return &adev->res;
28
29 return NULL;
30}
31
32static int get_amba_irq(struct vfio_platform_device *vdev, int i)
33{
34 struct amba_device *adev = (struct amba_device *) vdev->opaque;
35 int ret = 0;
36
37 if (i < AMBA_NR_IRQS)
38 ret = adev->irq[i];
39
40 /* zero is an unset IRQ for AMBA devices */
41 return ret ? ret : -ENXIO;
42}
43
44static int vfio_amba_init_dev(struct vfio_device *core_vdev)
45{
46 struct vfio_platform_device *vdev =
47 container_of(core_vdev, struct vfio_platform_device, vdev);
48 struct amba_device *adev = to_amba_device(core_vdev->dev);
49 int ret;
50
51 vdev->name = kasprintf(GFP_KERNEL, "vfio-amba-%08x", adev->periphid);
52 if (!vdev->name)
53 return -ENOMEM;
54
55 vdev->opaque = (void *) adev;
56 vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
57 vdev->get_resource = get_amba_resource;
58 vdev->get_irq = get_amba_irq;
59 vdev->reset_required = false;
60
61 ret = vfio_platform_init_common(vdev);
62 if (ret)
63 kfree(vdev->name);
64 return ret;
65}
66
67static const struct vfio_device_ops vfio_amba_ops;
68static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
69{
70 struct vfio_platform_device *vdev;
71 int ret;
72
73 vdev = vfio_alloc_device(vfio_platform_device, vdev, &adev->dev,
74 &vfio_amba_ops);
75 if (IS_ERR(vdev))
76 return PTR_ERR(vdev);
77
78 ret = vfio_register_group_dev(&vdev->vdev);
79 if (ret)
80 goto out_put_vdev;
81
82 pm_runtime_enable(&adev->dev);
83 dev_set_drvdata(&adev->dev, vdev);
84 return 0;
85
86out_put_vdev:
87 vfio_put_device(&vdev->vdev);
88 return ret;
89}
90
91static void vfio_amba_release_dev(struct vfio_device *core_vdev)
92{
93 struct vfio_platform_device *vdev =
94 container_of(core_vdev, struct vfio_platform_device, vdev);
95
96 vfio_platform_release_common(vdev);
97 kfree(vdev->name);
98}
99
100static void vfio_amba_remove(struct amba_device *adev)
101{
102 struct vfio_platform_device *vdev = dev_get_drvdata(&adev->dev);
103
104 vfio_unregister_group_dev(&vdev->vdev);
105 pm_runtime_disable(vdev->device);
106 vfio_put_device(&vdev->vdev);
107}
108
109static const struct vfio_device_ops vfio_amba_ops = {
110 .name = "vfio-amba",
111 .init = vfio_amba_init_dev,
112 .release = vfio_amba_release_dev,
113 .open_device = vfio_platform_open_device,
114 .close_device = vfio_platform_close_device,
115 .ioctl = vfio_platform_ioctl,
116 .read = vfio_platform_read,
117 .write = vfio_platform_write,
118 .mmap = vfio_platform_mmap,
119 .bind_iommufd = vfio_iommufd_physical_bind,
120 .unbind_iommufd = vfio_iommufd_physical_unbind,
121 .attach_ioas = vfio_iommufd_physical_attach_ioas,
122 .detach_ioas = vfio_iommufd_physical_detach_ioas,
123};
124
125static const struct amba_id pl330_ids[] = {
126 { 0, 0 },
127};
128
129MODULE_DEVICE_TABLE(amba, pl330_ids);
130
131static struct amba_driver vfio_amba_driver = {
132 .probe = vfio_amba_probe,
133 .remove = vfio_amba_remove,
134 .id_table = pl330_ids,
135 .drv = {
136 .name = "vfio-amba",
137 .owner = THIS_MODULE,
138 },
139 .driver_managed_dma = true,
140};
141
142module_amba_driver(vfio_amba_driver);
143
144MODULE_VERSION(DRIVER_VERSION);
145MODULE_LICENSE("GPL v2");
146MODULE_AUTHOR(DRIVER_AUTHOR);
147MODULE_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/amba/bus.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 AMBA devices - User Level meta-driver"
25
26/* probing devices from the AMBA bus */
27
28static struct resource *get_amba_resource(struct vfio_platform_device *vdev,
29 int i)
30{
31 struct amba_device *adev = (struct amba_device *) vdev->opaque;
32
33 if (i == 0)
34 return &adev->res;
35
36 return NULL;
37}
38
39static int get_amba_irq(struct vfio_platform_device *vdev, int i)
40{
41 struct amba_device *adev = (struct amba_device *) vdev->opaque;
42 int ret = 0;
43
44 if (i < AMBA_NR_IRQS)
45 ret = adev->irq[i];
46
47 /* zero is an unset IRQ for AMBA devices */
48 return ret ? ret : -ENXIO;
49}
50
51static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
52{
53 struct vfio_platform_device *vdev;
54 int ret;
55
56 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
57 if (!vdev)
58 return -ENOMEM;
59
60 vdev->name = kasprintf(GFP_KERNEL, "vfio-amba-%08x", adev->periphid);
61 if (!vdev->name) {
62 kfree(vdev);
63 return -ENOMEM;
64 }
65
66 vdev->opaque = (void *) adev;
67 vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
68 vdev->get_resource = get_amba_resource;
69 vdev->get_irq = get_amba_irq;
70 vdev->parent_module = THIS_MODULE;
71
72 ret = vfio_platform_probe_common(vdev, &adev->dev);
73 if (ret) {
74 kfree(vdev->name);
75 kfree(vdev);
76 }
77
78 return ret;
79}
80
81static int vfio_amba_remove(struct amba_device *adev)
82{
83 struct vfio_platform_device *vdev;
84
85 vdev = vfio_platform_remove_common(&adev->dev);
86 if (vdev) {
87 kfree(vdev->name);
88 kfree(vdev);
89 return 0;
90 }
91
92 return -EINVAL;
93}
94
95static struct amba_id pl330_ids[] = {
96 { 0, 0 },
97};
98
99MODULE_DEVICE_TABLE(amba, pl330_ids);
100
101static struct amba_driver vfio_amba_driver = {
102 .probe = vfio_amba_probe,
103 .remove = vfio_amba_remove,
104 .id_table = pl330_ids,
105 .drv = {
106 .name = "vfio-amba",
107 .owner = THIS_MODULE,
108 },
109};
110
111module_amba_driver(vfio_amba_driver);
112
113MODULE_VERSION(DRIVER_VERSION);
114MODULE_LICENSE("GPL v2");
115MODULE_AUTHOR(DRIVER_AUTHOR);
116MODULE_DESCRIPTION(DRIVER_DESC);