2
0

ap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * VFIO based AP matrix device assignment
  3. *
  4. * Copyright 2018 IBM Corp.
  5. * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  6. * Halil Pasic <pasic@linux.ibm.com>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  9. * your option) any later version. See the COPYING file in the top-level
  10. * directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  14. #include <linux/vfio.h>
  15. #include <sys/ioctl.h>
  16. #include "qapi/error.h"
  17. #include "hw/vfio/vfio-common.h"
  18. #include "system/iommufd.h"
  19. #include "hw/s390x/ap-device.h"
  20. #include "qemu/error-report.h"
  21. #include "qemu/event_notifier.h"
  22. #include "qemu/main-loop.h"
  23. #include "qemu/module.h"
  24. #include "qemu/option.h"
  25. #include "qemu/config-file.h"
  26. #include "kvm/kvm_s390x.h"
  27. #include "migration/vmstate.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/s390x/ap-bridge.h"
  30. #include "exec/address-spaces.h"
  31. #include "qom/object.h"
  32. #define TYPE_VFIO_AP_DEVICE "vfio-ap"
  33. struct VFIOAPDevice {
  34. APDevice apdev;
  35. VFIODevice vdev;
  36. EventNotifier req_notifier;
  37. };
  38. OBJECT_DECLARE_SIMPLE_TYPE(VFIOAPDevice, VFIO_AP_DEVICE)
  39. static void vfio_ap_compute_needs_reset(VFIODevice *vdev)
  40. {
  41. vdev->needs_reset = false;
  42. }
  43. /*
  44. * We don't need vfio_hot_reset_multi and vfio_eoi operations for
  45. * vfio-ap device now.
  46. */
  47. struct VFIODeviceOps vfio_ap_ops = {
  48. .vfio_compute_needs_reset = vfio_ap_compute_needs_reset,
  49. };
  50. static void vfio_ap_req_notifier_handler(void *opaque)
  51. {
  52. VFIOAPDevice *vapdev = opaque;
  53. Error *err = NULL;
  54. if (!event_notifier_test_and_clear(&vapdev->req_notifier)) {
  55. return;
  56. }
  57. qdev_unplug(DEVICE(vapdev), &err);
  58. if (err) {
  59. warn_reportf_err(err, VFIO_MSG_PREFIX, vapdev->vdev.name);
  60. }
  61. }
  62. static bool vfio_ap_register_irq_notifier(VFIOAPDevice *vapdev,
  63. unsigned int irq, Error **errp)
  64. {
  65. int fd;
  66. size_t argsz;
  67. IOHandler *fd_read;
  68. EventNotifier *notifier;
  69. g_autofree struct vfio_irq_info *irq_info = NULL;
  70. VFIODevice *vdev = &vapdev->vdev;
  71. switch (irq) {
  72. case VFIO_AP_REQ_IRQ_INDEX:
  73. notifier = &vapdev->req_notifier;
  74. fd_read = vfio_ap_req_notifier_handler;
  75. break;
  76. default:
  77. error_setg(errp, "vfio: Unsupported device irq(%d)", irq);
  78. return false;
  79. }
  80. if (vdev->num_irqs < irq + 1) {
  81. error_setg(errp, "vfio: IRQ %u not available (number of irqs %u)",
  82. irq, vdev->num_irqs);
  83. return false;
  84. }
  85. argsz = sizeof(*irq_info);
  86. irq_info = g_malloc0(argsz);
  87. irq_info->index = irq;
  88. irq_info->argsz = argsz;
  89. if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
  90. irq_info) < 0 || irq_info->count < 1) {
  91. error_setg_errno(errp, errno, "vfio: Error getting irq info");
  92. return false;
  93. }
  94. if (event_notifier_init(notifier, 0)) {
  95. error_setg_errno(errp, errno,
  96. "vfio: Unable to init event notifier for irq (%d)",
  97. irq);
  98. return false;
  99. }
  100. fd = event_notifier_get_fd(notifier);
  101. qemu_set_fd_handler(fd, fd_read, NULL, vapdev);
  102. if (!vfio_set_irq_signaling(vdev, irq, 0, VFIO_IRQ_SET_ACTION_TRIGGER, fd,
  103. errp)) {
  104. qemu_set_fd_handler(fd, NULL, NULL, vapdev);
  105. event_notifier_cleanup(notifier);
  106. }
  107. return true;
  108. }
  109. static void vfio_ap_unregister_irq_notifier(VFIOAPDevice *vapdev,
  110. unsigned int irq)
  111. {
  112. Error *err = NULL;
  113. EventNotifier *notifier;
  114. switch (irq) {
  115. case VFIO_AP_REQ_IRQ_INDEX:
  116. notifier = &vapdev->req_notifier;
  117. break;
  118. default:
  119. error_report("vfio: Unsupported device irq(%d)", irq);
  120. return;
  121. }
  122. if (!vfio_set_irq_signaling(&vapdev->vdev, irq, 0,
  123. VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
  124. warn_reportf_err(err, VFIO_MSG_PREFIX, vapdev->vdev.name);
  125. }
  126. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  127. NULL, NULL, vapdev);
  128. event_notifier_cleanup(notifier);
  129. }
  130. static void vfio_ap_realize(DeviceState *dev, Error **errp)
  131. {
  132. ERRP_GUARD();
  133. Error *err = NULL;
  134. VFIOAPDevice *vapdev = VFIO_AP_DEVICE(dev);
  135. VFIODevice *vbasedev = &vapdev->vdev;
  136. if (!vfio_device_get_name(vbasedev, errp)) {
  137. return;
  138. }
  139. if (!vfio_attach_device(vbasedev->name, vbasedev,
  140. &address_space_memory, errp)) {
  141. goto error;
  142. }
  143. if (!vfio_ap_register_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX, &err)) {
  144. /*
  145. * Report this error, but do not make it a failing condition.
  146. * Lack of this IRQ in the host does not prevent normal operation.
  147. */
  148. warn_report_err(err);
  149. }
  150. return;
  151. error:
  152. error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
  153. g_free(vbasedev->name);
  154. }
  155. static void vfio_ap_unrealize(DeviceState *dev)
  156. {
  157. VFIOAPDevice *vapdev = VFIO_AP_DEVICE(dev);
  158. vfio_ap_unregister_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX);
  159. vfio_detach_device(&vapdev->vdev);
  160. g_free(vapdev->vdev.name);
  161. }
  162. static const Property vfio_ap_properties[] = {
  163. DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice, vdev.sysfsdev),
  164. #ifdef CONFIG_IOMMUFD
  165. DEFINE_PROP_LINK("iommufd", VFIOAPDevice, vdev.iommufd,
  166. TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
  167. #endif
  168. };
  169. static void vfio_ap_reset(DeviceState *dev)
  170. {
  171. int ret;
  172. VFIOAPDevice *vapdev = VFIO_AP_DEVICE(dev);
  173. ret = ioctl(vapdev->vdev.fd, VFIO_DEVICE_RESET);
  174. if (ret) {
  175. error_report("%s: failed to reset %s device: %s", __func__,
  176. vapdev->vdev.name, strerror(errno));
  177. }
  178. }
  179. static const VMStateDescription vfio_ap_vmstate = {
  180. .name = "vfio-ap",
  181. .unmigratable = 1,
  182. };
  183. static void vfio_ap_instance_init(Object *obj)
  184. {
  185. VFIOAPDevice *vapdev = VFIO_AP_DEVICE(obj);
  186. VFIODevice *vbasedev = &vapdev->vdev;
  187. /*
  188. * vfio-ap devices operate in a way compatible with discarding of
  189. * memory in RAM blocks, as no pages are pinned in the host.
  190. * This needs to be set before vfio_get_device() for vfio common to
  191. * handle ram_block_discard_disable().
  192. */
  193. vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_AP, &vfio_ap_ops,
  194. DEVICE(vapdev), true);
  195. /* AP device is mdev type device */
  196. vbasedev->mdev = true;
  197. }
  198. #ifdef CONFIG_IOMMUFD
  199. static void vfio_ap_set_fd(Object *obj, const char *str, Error **errp)
  200. {
  201. vfio_device_set_fd(&VFIO_AP_DEVICE(obj)->vdev, str, errp);
  202. }
  203. #endif
  204. static void vfio_ap_class_init(ObjectClass *klass, void *data)
  205. {
  206. DeviceClass *dc = DEVICE_CLASS(klass);
  207. device_class_set_props(dc, vfio_ap_properties);
  208. #ifdef CONFIG_IOMMUFD
  209. object_class_property_add_str(klass, "fd", NULL, vfio_ap_set_fd);
  210. #endif
  211. dc->vmsd = &vfio_ap_vmstate;
  212. dc->desc = "VFIO-based AP device assignment";
  213. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  214. dc->realize = vfio_ap_realize;
  215. dc->unrealize = vfio_ap_unrealize;
  216. dc->hotpluggable = true;
  217. device_class_set_legacy_reset(dc, vfio_ap_reset);
  218. dc->bus_type = TYPE_AP_BUS;
  219. object_class_property_set_description(klass, /* 3.1 */
  220. "sysfsdev",
  221. "Host sysfs path of assigned device");
  222. #ifdef CONFIG_IOMMUFD
  223. object_class_property_set_description(klass, /* 9.0 */
  224. "iommufd",
  225. "Set host IOMMUFD backend device");
  226. #endif
  227. }
  228. static const TypeInfo vfio_ap_info = {
  229. .name = TYPE_VFIO_AP_DEVICE,
  230. .parent = TYPE_AP_DEVICE,
  231. .instance_size = sizeof(VFIOAPDevice),
  232. .instance_init = vfio_ap_instance_init,
  233. .class_init = vfio_ap_class_init,
  234. };
  235. static void vfio_ap_type_init(void)
  236. {
  237. type_register_static(&vfio_ap_info);
  238. }
  239. type_init(vfio_ap_type_init)