vfio_amba.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/vfio.h>
  17. #include <linux/amba/bus.h>
  18. #include "vfio_platform_private.h"
  19. #define DRIVER_VERSION "0.10"
  20. #define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
  21. #define DRIVER_DESC "VFIO for AMBA devices - User Level meta-driver"
  22. /* probing devices from the AMBA bus */
  23. static struct resource *get_amba_resource(struct vfio_platform_device *vdev,
  24. int i)
  25. {
  26. struct amba_device *adev = (struct amba_device *) vdev->opaque;
  27. if (i == 0)
  28. return &adev->res;
  29. return NULL;
  30. }
  31. static int get_amba_irq(struct vfio_platform_device *vdev, int i)
  32. {
  33. struct amba_device *adev = (struct amba_device *) vdev->opaque;
  34. int ret = 0;
  35. if (i < AMBA_NR_IRQS)
  36. ret = adev->irq[i];
  37. /* zero is an unset IRQ for AMBA devices */
  38. return ret ? ret : -ENXIO;
  39. }
  40. static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
  41. {
  42. struct vfio_platform_device *vdev;
  43. int ret;
  44. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  45. if (!vdev)
  46. return -ENOMEM;
  47. vdev->name = kasprintf(GFP_KERNEL, "vfio-amba-%08x", adev->periphid);
  48. if (!vdev->name) {
  49. kfree(vdev);
  50. return -ENOMEM;
  51. }
  52. vdev->opaque = (void *) adev;
  53. vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
  54. vdev->get_resource = get_amba_resource;
  55. vdev->get_irq = get_amba_irq;
  56. vdev->parent_module = THIS_MODULE;
  57. ret = vfio_platform_probe_common(vdev, &adev->dev);
  58. if (ret) {
  59. kfree(vdev->name);
  60. kfree(vdev);
  61. }
  62. return ret;
  63. }
  64. static int vfio_amba_remove(struct amba_device *adev)
  65. {
  66. struct vfio_platform_device *vdev;
  67. vdev = vfio_platform_remove_common(&adev->dev);
  68. if (vdev) {
  69. kfree(vdev->name);
  70. kfree(vdev);
  71. return 0;
  72. }
  73. return -EINVAL;
  74. }
  75. static struct amba_id pl330_ids[] = {
  76. { 0, 0 },
  77. };
  78. MODULE_DEVICE_TABLE(amba, pl330_ids);
  79. static struct amba_driver vfio_amba_driver = {
  80. .probe = vfio_amba_probe,
  81. .remove = vfio_amba_remove,
  82. .id_table = pl330_ids,
  83. .drv = {
  84. .name = "vfio-amba",
  85. .owner = THIS_MODULE,
  86. },
  87. };
  88. module_amba_driver(vfio_amba_driver);
  89. MODULE_VERSION(DRIVER_VERSION);
  90. MODULE_LICENSE("GPL v2");
  91. MODULE_AUTHOR(DRIVER_AUTHOR);
  92. MODULE_DESCRIPTION(DRIVER_DESC);