vfio_platform.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/platform_device.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 platform devices - User Level meta-driver"
  22. /* probing devices from the linux platform bus */
  23. static struct resource *get_platform_resource(struct vfio_platform_device *vdev,
  24. int num)
  25. {
  26. struct platform_device *dev = (struct platform_device *) vdev->opaque;
  27. int i;
  28. for (i = 0; i < dev->num_resources; i++) {
  29. struct resource *r = &dev->resource[i];
  30. if (resource_type(r) & (IORESOURCE_MEM|IORESOURCE_IO)) {
  31. if (!num)
  32. return r;
  33. num--;
  34. }
  35. }
  36. return NULL;
  37. }
  38. static int get_platform_irq(struct vfio_platform_device *vdev, int i)
  39. {
  40. struct platform_device *pdev = (struct platform_device *) vdev->opaque;
  41. return platform_get_irq(pdev, i);
  42. }
  43. static int vfio_platform_probe(struct platform_device *pdev)
  44. {
  45. struct vfio_platform_device *vdev;
  46. int ret;
  47. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  48. if (!vdev)
  49. return -ENOMEM;
  50. vdev->opaque = (void *) pdev;
  51. vdev->name = pdev->name;
  52. vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
  53. vdev->get_resource = get_platform_resource;
  54. vdev->get_irq = get_platform_irq;
  55. vdev->parent_module = THIS_MODULE;
  56. ret = vfio_platform_probe_common(vdev, &pdev->dev);
  57. if (ret)
  58. kfree(vdev);
  59. return ret;
  60. }
  61. static int vfio_platform_remove(struct platform_device *pdev)
  62. {
  63. struct vfio_platform_device *vdev;
  64. vdev = vfio_platform_remove_common(&pdev->dev);
  65. if (vdev) {
  66. kfree(vdev);
  67. return 0;
  68. }
  69. return -EINVAL;
  70. }
  71. static struct platform_driver vfio_platform_driver = {
  72. .probe = vfio_platform_probe,
  73. .remove = vfio_platform_remove,
  74. .driver = {
  75. .name = "vfio-platform",
  76. },
  77. };
  78. module_platform_driver(vfio_platform_driver);
  79. MODULE_VERSION(DRIVER_VERSION);
  80. MODULE_LICENSE("GPL v2");
  81. MODULE_AUTHOR(DRIVER_AUTHOR);
  82. MODULE_DESCRIPTION(DRIVER_DESC);