xref: /linux/drivers/vfio/platform/vfio_amba.c (revision 36fe431f2811fa3b5fed15d272c585d5a47977aa)
1*36fe431fSAntonios Motakis /*
2*36fe431fSAntonios Motakis  * Copyright (C) 2013 - Virtual Open Systems
3*36fe431fSAntonios Motakis  * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
4*36fe431fSAntonios Motakis  *
5*36fe431fSAntonios Motakis  * This program is free software; you can redistribute it and/or modify
6*36fe431fSAntonios Motakis  * it under the terms of the GNU General Public License, version 2, as
7*36fe431fSAntonios Motakis  * published by the Free Software Foundation.
8*36fe431fSAntonios Motakis  *
9*36fe431fSAntonios Motakis  * This program is distributed in the hope that it will be useful,
10*36fe431fSAntonios Motakis  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*36fe431fSAntonios Motakis  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*36fe431fSAntonios Motakis  * GNU General Public License for more details.
13*36fe431fSAntonios Motakis  */
14*36fe431fSAntonios Motakis 
15*36fe431fSAntonios Motakis #include <linux/module.h>
16*36fe431fSAntonios Motakis #include <linux/slab.h>
17*36fe431fSAntonios Motakis #include <linux/vfio.h>
18*36fe431fSAntonios Motakis #include <linux/amba/bus.h>
19*36fe431fSAntonios Motakis 
20*36fe431fSAntonios Motakis #include "vfio_platform_private.h"
21*36fe431fSAntonios Motakis 
22*36fe431fSAntonios Motakis #define DRIVER_VERSION  "0.10"
23*36fe431fSAntonios Motakis #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis@virtualopensystems.com>"
24*36fe431fSAntonios Motakis #define DRIVER_DESC     "VFIO for AMBA devices - User Level meta-driver"
25*36fe431fSAntonios Motakis 
26*36fe431fSAntonios Motakis /* probing devices from the AMBA bus */
27*36fe431fSAntonios Motakis 
28*36fe431fSAntonios Motakis static struct resource *get_amba_resource(struct vfio_platform_device *vdev,
29*36fe431fSAntonios Motakis 					  int i)
30*36fe431fSAntonios Motakis {
31*36fe431fSAntonios Motakis 	struct amba_device *adev = (struct amba_device *) vdev->opaque;
32*36fe431fSAntonios Motakis 
33*36fe431fSAntonios Motakis 	if (i == 0)
34*36fe431fSAntonios Motakis 		return &adev->res;
35*36fe431fSAntonios Motakis 
36*36fe431fSAntonios Motakis 	return NULL;
37*36fe431fSAntonios Motakis }
38*36fe431fSAntonios Motakis 
39*36fe431fSAntonios Motakis static int get_amba_irq(struct vfio_platform_device *vdev, int i)
40*36fe431fSAntonios Motakis {
41*36fe431fSAntonios Motakis 	struct amba_device *adev = (struct amba_device *) vdev->opaque;
42*36fe431fSAntonios Motakis 	int ret = 0;
43*36fe431fSAntonios Motakis 
44*36fe431fSAntonios Motakis 	if (i < AMBA_NR_IRQS)
45*36fe431fSAntonios Motakis 		ret = adev->irq[i];
46*36fe431fSAntonios Motakis 
47*36fe431fSAntonios Motakis 	/* zero is an unset IRQ for AMBA devices */
48*36fe431fSAntonios Motakis 	return ret ? ret : -ENXIO;
49*36fe431fSAntonios Motakis }
50*36fe431fSAntonios Motakis 
51*36fe431fSAntonios Motakis static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
52*36fe431fSAntonios Motakis {
53*36fe431fSAntonios Motakis 	struct vfio_platform_device *vdev;
54*36fe431fSAntonios Motakis 	int ret;
55*36fe431fSAntonios Motakis 
56*36fe431fSAntonios Motakis 	vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
57*36fe431fSAntonios Motakis 	if (!vdev)
58*36fe431fSAntonios Motakis 		return -ENOMEM;
59*36fe431fSAntonios Motakis 
60*36fe431fSAntonios Motakis 	vdev->name = kasprintf(GFP_KERNEL, "vfio-amba-%08x", adev->periphid);
61*36fe431fSAntonios Motakis 	if (!vdev->name) {
62*36fe431fSAntonios Motakis 		kfree(vdev);
63*36fe431fSAntonios Motakis 		return -ENOMEM;
64*36fe431fSAntonios Motakis 	}
65*36fe431fSAntonios Motakis 
66*36fe431fSAntonios Motakis 	vdev->opaque = (void *) adev;
67*36fe431fSAntonios Motakis 	vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
68*36fe431fSAntonios Motakis 	vdev->get_resource = get_amba_resource;
69*36fe431fSAntonios Motakis 	vdev->get_irq = get_amba_irq;
70*36fe431fSAntonios Motakis 
71*36fe431fSAntonios Motakis 	ret = vfio_platform_probe_common(vdev, &adev->dev);
72*36fe431fSAntonios Motakis 	if (ret) {
73*36fe431fSAntonios Motakis 		kfree(vdev->name);
74*36fe431fSAntonios Motakis 		kfree(vdev);
75*36fe431fSAntonios Motakis 	}
76*36fe431fSAntonios Motakis 
77*36fe431fSAntonios Motakis 	return ret;
78*36fe431fSAntonios Motakis }
79*36fe431fSAntonios Motakis 
80*36fe431fSAntonios Motakis static int vfio_amba_remove(struct amba_device *adev)
81*36fe431fSAntonios Motakis {
82*36fe431fSAntonios Motakis 	struct vfio_platform_device *vdev;
83*36fe431fSAntonios Motakis 
84*36fe431fSAntonios Motakis 	vdev = vfio_platform_remove_common(&adev->dev);
85*36fe431fSAntonios Motakis 	if (vdev) {
86*36fe431fSAntonios Motakis 		kfree(vdev->name);
87*36fe431fSAntonios Motakis 		kfree(vdev);
88*36fe431fSAntonios Motakis 		return 0;
89*36fe431fSAntonios Motakis 	}
90*36fe431fSAntonios Motakis 
91*36fe431fSAntonios Motakis 	return -EINVAL;
92*36fe431fSAntonios Motakis }
93*36fe431fSAntonios Motakis 
94*36fe431fSAntonios Motakis static struct amba_id pl330_ids[] = {
95*36fe431fSAntonios Motakis 	{ 0, 0 },
96*36fe431fSAntonios Motakis };
97*36fe431fSAntonios Motakis 
98*36fe431fSAntonios Motakis MODULE_DEVICE_TABLE(amba, pl330_ids);
99*36fe431fSAntonios Motakis 
100*36fe431fSAntonios Motakis static struct amba_driver vfio_amba_driver = {
101*36fe431fSAntonios Motakis 	.probe = vfio_amba_probe,
102*36fe431fSAntonios Motakis 	.remove = vfio_amba_remove,
103*36fe431fSAntonios Motakis 	.id_table = pl330_ids,
104*36fe431fSAntonios Motakis 	.drv = {
105*36fe431fSAntonios Motakis 		.name = "vfio-amba",
106*36fe431fSAntonios Motakis 		.owner = THIS_MODULE,
107*36fe431fSAntonios Motakis 	},
108*36fe431fSAntonios Motakis };
109*36fe431fSAntonios Motakis 
110*36fe431fSAntonios Motakis module_amba_driver(vfio_amba_driver);
111*36fe431fSAntonios Motakis 
112*36fe431fSAntonios Motakis MODULE_VERSION(DRIVER_VERSION);
113*36fe431fSAntonios Motakis MODULE_LICENSE("GPL v2");
114*36fe431fSAntonios Motakis MODULE_AUTHOR(DRIVER_AUTHOR);
115*36fe431fSAntonios Motakis MODULE_DESCRIPTION(DRIVER_DESC);
116