vfio_platform.c 2.7 KB

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