Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2013 - Virtual Open Systems
0004  * Author: Antonios Motakis <a.motakis@virtualopensystems.com>
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/slab.h>
0009 #include <linux/vfio.h>
0010 #include <linux/amba/bus.h>
0011 
0012 #include "vfio_platform_private.h"
0013 
0014 #define DRIVER_VERSION  "0.10"
0015 #define DRIVER_AUTHOR   "Antonios Motakis <a.motakis@virtualopensystems.com>"
0016 #define DRIVER_DESC     "VFIO for AMBA devices - User Level meta-driver"
0017 
0018 /* probing devices from the AMBA bus */
0019 
0020 static struct resource *get_amba_resource(struct vfio_platform_device *vdev,
0021                       int i)
0022 {
0023     struct amba_device *adev = (struct amba_device *) vdev->opaque;
0024 
0025     if (i == 0)
0026         return &adev->res;
0027 
0028     return NULL;
0029 }
0030 
0031 static int get_amba_irq(struct vfio_platform_device *vdev, int i)
0032 {
0033     struct amba_device *adev = (struct amba_device *) vdev->opaque;
0034     int ret = 0;
0035 
0036     if (i < AMBA_NR_IRQS)
0037         ret = adev->irq[i];
0038 
0039     /* zero is an unset IRQ for AMBA devices */
0040     return ret ? ret : -ENXIO;
0041 }
0042 
0043 static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
0044 {
0045     struct vfio_platform_device *vdev;
0046     int ret;
0047 
0048     vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
0049     if (!vdev)
0050         return -ENOMEM;
0051 
0052     vdev->name = kasprintf(GFP_KERNEL, "vfio-amba-%08x", adev->periphid);
0053     if (!vdev->name) {
0054         kfree(vdev);
0055         return -ENOMEM;
0056     }
0057 
0058     vdev->opaque = (void *) adev;
0059     vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
0060     vdev->get_resource = get_amba_resource;
0061     vdev->get_irq = get_amba_irq;
0062     vdev->reset_required = false;
0063 
0064     ret = vfio_platform_probe_common(vdev, &adev->dev);
0065     if (ret) {
0066         kfree(vdev->name);
0067         kfree(vdev);
0068         return ret;
0069     }
0070 
0071     dev_set_drvdata(&adev->dev, vdev);
0072     return 0;
0073 }
0074 
0075 static void vfio_amba_remove(struct amba_device *adev)
0076 {
0077     struct vfio_platform_device *vdev = dev_get_drvdata(&adev->dev);
0078 
0079     vfio_platform_remove_common(vdev);
0080     kfree(vdev->name);
0081     kfree(vdev);
0082 }
0083 
0084 static const struct amba_id pl330_ids[] = {
0085     { 0, 0 },
0086 };
0087 
0088 MODULE_DEVICE_TABLE(amba, pl330_ids);
0089 
0090 static struct amba_driver vfio_amba_driver = {
0091     .probe = vfio_amba_probe,
0092     .remove = vfio_amba_remove,
0093     .id_table = pl330_ids,
0094     .drv = {
0095         .name = "vfio-amba",
0096         .owner = THIS_MODULE,
0097     },
0098     .driver_managed_dma = true,
0099 };
0100 
0101 module_amba_driver(vfio_amba_driver);
0102 
0103 MODULE_VERSION(DRIVER_VERSION);
0104 MODULE_LICENSE("GPL v2");
0105 MODULE_AUTHOR(DRIVER_AUTHOR);
0106 MODULE_DESCRIPTION(DRIVER_DESC);