| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2013 - Virtual Open Systems |
| 4 |
* Author: Antonios Motakis <[email protected]> |
| 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 <[email protected]>" |
| 17 |
#define DRIVER_DESC "VFIO for AMBA devices - User Level meta-driver" |
| 18 |
|
| 19 |
/* probing devices from the AMBA bus */ |
| 20 |
|
| 21 |
static 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 |
|
| 32 |
static 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 |
|
| 44 |
static 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 |
|
| 67 |
static const struct vfio_device_ops vfio_amba_ops; |
| 68 |
static 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 |
dev_err_once(&adev->dev, "DEPRECATION: vfio-amba is deprecated and will be removed in a future kernel release\n"); |
| 74 |
|
| 75 |
vdev = vfio_alloc_device(vfio_platform_device, vdev, &adev->dev, |
| 76 |
&vfio_amba_ops); |
| 77 |
if (IS_ERR(vdev)) |
| 78 |
return PTR_ERR(vdev); |
| 79 |
|
| 80 |
ret = vfio_register_group_dev(&vdev->vdev); |
| 81 |
if (ret) |
| 82 |
goto out_put_vdev; |
| 83 |
|
| 84 |
pm_runtime_enable(&adev->dev); |
| 85 |
dev_set_drvdata(&adev->dev, vdev); |
| 86 |
return 0; |
| 87 |
|
| 88 |
out_put_vdev: |
| 89 |
vfio_put_device(&vdev->vdev); |
| 90 |
return ret; |
| 91 |
} |
| 92 |
|
| 93 |
static void vfio_amba_release_dev(struct vfio_device *core_vdev) |
| 94 |
{ |
| 95 |
struct vfio_platform_device *vdev = |
| 96 |
container_of(core_vdev, struct vfio_platform_device, vdev); |
| 97 |
|
| 98 |
vfio_platform_release_common(vdev); |
| 99 |
kfree(vdev->name); |
| 100 |
} |
| 101 |
|
| 102 |
static void vfio_amba_remove(struct amba_device *adev) |
| 103 |
{ |
| 104 |
struct vfio_platform_device *vdev = dev_get_drvdata(&adev->dev); |
| 105 |
|
| 106 |
vfio_unregister_group_dev(&vdev->vdev); |
| 107 |
pm_runtime_disable(vdev->device); |
| 108 |
vfio_put_device(&vdev->vdev); |
| 109 |
} |
| 110 |
|
| 111 |
static const struct vfio_device_ops vfio_amba_ops = { |
| 112 |
.name = "vfio-amba", |
| 113 |
.init = vfio_amba_init_dev, |
| 114 |
.release = vfio_amba_release_dev, |
| 115 |
.open_device = vfio_platform_open_device, |
| 116 |
.close_device = vfio_platform_close_device, |
| 117 |
.ioctl = vfio_platform_ioctl, |
| 118 |
.get_region_info_caps = vfio_platform_ioctl_get_region_info, |
| 119 |
.read = vfio_platform_read, |
| 120 |
.write = vfio_platform_write, |
| 121 |
.mmap = vfio_platform_mmap, |
| 122 |
.bind_iommufd = vfio_iommufd_physical_bind, |
| 123 |
.unbind_iommufd = vfio_iommufd_physical_unbind, |
| 124 |
.attach_ioas = vfio_iommufd_physical_attach_ioas, |
| 125 |
.detach_ioas = vfio_iommufd_physical_detach_ioas, |
| 126 |
}; |
| 127 |
|
| 128 |
static const struct amba_id vfio_amba_ids[] = { |
| 129 |
{ 0, 0 }, |
| 130 |
}; |
| 131 |
|
| 132 |
MODULE_DEVICE_TABLE(amba, vfio_amba_ids); |
| 133 |
|
| 134 |
static struct amba_driver vfio_amba_driver = { |
| 135 |
.probe = vfio_amba_probe, |
| 136 |
.remove = vfio_amba_remove, |
| 137 |
.id_table = vfio_amba_ids, |
| 138 |
.drv = { |
| 139 |
.name = "vfio-amba", |
| 140 |
}, |
| 141 |
.driver_managed_dma = true, |
| 142 |
}; |
| 143 |
|
| 144 |
module_amba_driver(vfio_amba_driver); |
| 145 |
|
| 146 |
MODULE_VERSION(DRIVER_VERSION); |
| 147 |
MODULE_LICENSE("GPL v2"); |
| 148 |
MODULE_AUTHOR(DRIVER_AUTHOR); |
| 149 |
MODULE_DESCRIPTION(DRIVER_DESC); |