platform.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * vfio based device assignment support - platform devices
  3. *
  4. * Copyright Linaro Limited, 2014
  5. *
  6. * Authors:
  7. * Kim Phillips <kim.phillips@linaro.org>
  8. * Eric Auger <eric.auger@linaro.org>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2. See
  11. * the COPYING file in the top-level directory.
  12. *
  13. * Based on vfio based PCI device assignment support:
  14. * Copyright Red Hat, Inc. 2012
  15. */
  16. #include "qemu/osdep.h"
  17. #include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  18. #include "qapi/error.h"
  19. #include <sys/ioctl.h>
  20. #include <linux/vfio.h>
  21. #include "hw/vfio/vfio-platform.h"
  22. #include "system/iommufd.h"
  23. #include "migration/vmstate.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/lockable.h"
  26. #include "qemu/main-loop.h"
  27. #include "qemu/module.h"
  28. #include "qemu/range.h"
  29. #include "exec/memory.h"
  30. #include "exec/address-spaces.h"
  31. #include "qemu/queue.h"
  32. #include "hw/sysbus.h"
  33. #include "trace.h"
  34. #include "hw/irq.h"
  35. #include "hw/platform-bus.h"
  36. #include "hw/qdev-properties.h"
  37. #include "system/kvm.h"
  38. /*
  39. * Functions used whatever the injection method
  40. */
  41. static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
  42. {
  43. return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
  44. }
  45. /**
  46. * vfio_init_intp - allocate, initialize the IRQ struct pointer
  47. * and add it into the list of IRQs
  48. * @vbasedev: the VFIO device handle
  49. * @info: irq info struct retrieved from VFIO driver
  50. * @errp: error object
  51. */
  52. static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
  53. struct vfio_irq_info info, Error **errp)
  54. {
  55. int ret;
  56. VFIOPlatformDevice *vdev =
  57. container_of(vbasedev, VFIOPlatformDevice, vbasedev);
  58. SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
  59. VFIOINTp *intp;
  60. intp = g_malloc0(sizeof(*intp));
  61. intp->vdev = vdev;
  62. intp->pin = info.index;
  63. intp->flags = info.flags;
  64. intp->state = VFIO_IRQ_INACTIVE;
  65. intp->kvm_accel = false;
  66. sysbus_init_irq(sbdev, &intp->qemuirq);
  67. /* Get an eventfd for trigger */
  68. intp->interrupt = g_new0(EventNotifier, 1);
  69. ret = event_notifier_init(intp->interrupt, 0);
  70. if (ret) {
  71. g_free(intp->interrupt);
  72. g_free(intp);
  73. error_setg_errno(errp, -ret,
  74. "failed to initialize trigger eventfd notifier");
  75. return NULL;
  76. }
  77. if (vfio_irq_is_automasked(intp)) {
  78. /* Get an eventfd for resample/unmask */
  79. intp->unmask = g_new0(EventNotifier, 1);
  80. ret = event_notifier_init(intp->unmask, 0);
  81. if (ret) {
  82. g_free(intp->interrupt);
  83. g_free(intp->unmask);
  84. g_free(intp);
  85. error_setg_errno(errp, -ret,
  86. "failed to initialize resample eventfd notifier");
  87. return NULL;
  88. }
  89. }
  90. QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
  91. return intp;
  92. }
  93. /**
  94. * vfio_set_trigger_eventfd - set VFIO eventfd handling
  95. *
  96. * @intp: IRQ struct handle
  97. * @handler: handler to be called on eventfd signaling
  98. *
  99. * Setup VFIO signaling and attach an optional user-side handler
  100. * to the eventfd
  101. */
  102. static int vfio_set_trigger_eventfd(VFIOINTp *intp,
  103. eventfd_user_side_handler_t handler)
  104. {
  105. VFIODevice *vbasedev = &intp->vdev->vbasedev;
  106. int32_t fd = event_notifier_get_fd(intp->interrupt);
  107. Error *err = NULL;
  108. qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp);
  109. if (!vfio_set_irq_signaling(vbasedev, intp->pin, 0,
  110. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
  111. error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
  112. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  113. return -EINVAL;
  114. }
  115. return 0;
  116. }
  117. /*
  118. * Functions only used when eventfds are handled on user-side
  119. * ie. without irqfd
  120. */
  121. /**
  122. * vfio_mmap_set_enabled - enable/disable the fast path mode
  123. * @vdev: the VFIO platform device
  124. * @enabled: the target mmap state
  125. *
  126. * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
  127. * enabled = false ~ slow path = MMIO region is trapped and region callbacks
  128. * are called; slow path enables to trap the device IRQ status register reset
  129. */
  130. static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
  131. {
  132. int i;
  133. for (i = 0; i < vdev->vbasedev.num_regions; i++) {
  134. vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
  135. }
  136. }
  137. /**
  138. * vfio_intp_mmap_enable - timer function, restores the fast path
  139. * if there is no more active IRQ
  140. * @opaque: actually points to the VFIO platform device
  141. *
  142. * Called on mmap timer timeout, this function checks whether the
  143. * IRQ is still active and if not, restores the fast path.
  144. * by construction a single eventfd is handled at a time.
  145. * if the IRQ is still active, the timer is re-programmed.
  146. */
  147. static void vfio_intp_mmap_enable(void *opaque)
  148. {
  149. VFIOINTp *tmp;
  150. VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
  151. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  152. QLIST_FOREACH(tmp, &vdev->intp_list, next) {
  153. if (tmp->state == VFIO_IRQ_ACTIVE) {
  154. trace_vfio_platform_intp_mmap_enable(tmp->pin);
  155. /* re-program the timer to check active status later */
  156. timer_mod(vdev->mmap_timer,
  157. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  158. vdev->mmap_timeout);
  159. return;
  160. }
  161. }
  162. vfio_mmap_set_enabled(vdev, true);
  163. }
  164. /**
  165. * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
  166. * @opaque: opaque pointer, in practice the VFIOINTp handle
  167. *
  168. * The function is called on a previous IRQ completion, from
  169. * vfio_platform_eoi, while the intp_mutex is locked.
  170. * Also in such situation, the slow path already is set and
  171. * the mmap timer was already programmed.
  172. */
  173. static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
  174. {
  175. trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
  176. event_notifier_get_fd(intp->interrupt));
  177. intp->state = VFIO_IRQ_ACTIVE;
  178. /* trigger the virtual IRQ */
  179. qemu_set_irq(intp->qemuirq, 1);
  180. }
  181. /**
  182. * vfio_intp_interrupt - The user-side eventfd handler
  183. * @opaque: opaque pointer which in practice is the VFIOINTp handle
  184. *
  185. * the function is entered in event handler context:
  186. * the vIRQ is injected into the guest if there is no other active
  187. * or pending IRQ.
  188. */
  189. static void vfio_intp_interrupt(VFIOINTp *intp)
  190. {
  191. int ret;
  192. VFIOINTp *tmp;
  193. VFIOPlatformDevice *vdev = intp->vdev;
  194. bool delay_handling = false;
  195. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  196. if (intp->state == VFIO_IRQ_INACTIVE) {
  197. QLIST_FOREACH(tmp, &vdev->intp_list, next) {
  198. if (tmp->state == VFIO_IRQ_ACTIVE ||
  199. tmp->state == VFIO_IRQ_PENDING) {
  200. delay_handling = true;
  201. break;
  202. }
  203. }
  204. }
  205. if (delay_handling) {
  206. /*
  207. * the new IRQ gets a pending status and is pushed in
  208. * the pending queue
  209. */
  210. intp->state = VFIO_IRQ_PENDING;
  211. trace_vfio_intp_interrupt_set_pending(intp->pin);
  212. QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
  213. intp, pqnext);
  214. event_notifier_test_and_clear(intp->interrupt);
  215. return;
  216. }
  217. trace_vfio_platform_intp_interrupt(intp->pin,
  218. event_notifier_get_fd(intp->interrupt));
  219. ret = event_notifier_test_and_clear(intp->interrupt);
  220. if (!ret) {
  221. error_report("Error when clearing fd=%d (ret = %d)",
  222. event_notifier_get_fd(intp->interrupt), ret);
  223. }
  224. intp->state = VFIO_IRQ_ACTIVE;
  225. /* sets slow path */
  226. vfio_mmap_set_enabled(vdev, false);
  227. /* trigger the virtual IRQ */
  228. qemu_set_irq(intp->qemuirq, 1);
  229. /*
  230. * Schedule the mmap timer which will restore fastpath when no IRQ
  231. * is active anymore
  232. */
  233. if (vdev->mmap_timeout) {
  234. timer_mod(vdev->mmap_timer,
  235. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  236. vdev->mmap_timeout);
  237. }
  238. }
  239. /**
  240. * vfio_platform_eoi - IRQ completion routine
  241. * @vbasedev: the VFIO device handle
  242. *
  243. * De-asserts the active virtual IRQ and unmasks the physical IRQ
  244. * (effective for level sensitive IRQ auto-masked by the VFIO driver).
  245. * Then it handles next pending IRQ if any.
  246. * eoi function is called on the first access to any MMIO region
  247. * after an IRQ was triggered, trapped since slow path was set.
  248. * It is assumed this access corresponds to the IRQ status
  249. * register reset. With such a mechanism, a single IRQ can be
  250. * handled at a time since there is no way to know which IRQ
  251. * was completed by the guest (we would need additional details
  252. * about the IRQ status register mask).
  253. */
  254. static void vfio_platform_eoi(VFIODevice *vbasedev)
  255. {
  256. VFIOINTp *intp;
  257. VFIOPlatformDevice *vdev =
  258. container_of(vbasedev, VFIOPlatformDevice, vbasedev);
  259. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  260. QLIST_FOREACH(intp, &vdev->intp_list, next) {
  261. if (intp->state == VFIO_IRQ_ACTIVE) {
  262. trace_vfio_platform_eoi(intp->pin,
  263. event_notifier_get_fd(intp->interrupt));
  264. intp->state = VFIO_IRQ_INACTIVE;
  265. /* deassert the virtual IRQ */
  266. qemu_set_irq(intp->qemuirq, 0);
  267. if (vfio_irq_is_automasked(intp)) {
  268. /* unmasks the physical level-sensitive IRQ */
  269. vfio_unmask_single_irqindex(vbasedev, intp->pin);
  270. }
  271. /* a single IRQ can be active at a time */
  272. break;
  273. }
  274. }
  275. /* in case there are pending IRQs, handle the first one */
  276. if (!QSIMPLEQ_EMPTY(&vdev->pending_intp_queue)) {
  277. intp = QSIMPLEQ_FIRST(&vdev->pending_intp_queue);
  278. vfio_intp_inject_pending_lockheld(intp);
  279. QSIMPLEQ_REMOVE_HEAD(&vdev->pending_intp_queue, pqnext);
  280. }
  281. }
  282. /**
  283. * vfio_start_eventfd_injection - starts the virtual IRQ injection using
  284. * user-side handled eventfds
  285. * @sbdev: the sysbus device handle
  286. * @irq: the qemu irq handle
  287. */
  288. static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
  289. {
  290. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
  291. VFIOINTp *intp;
  292. QLIST_FOREACH(intp, &vdev->intp_list, next) {
  293. if (intp->qemuirq == irq) {
  294. break;
  295. }
  296. }
  297. assert(intp);
  298. if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) {
  299. abort();
  300. }
  301. }
  302. /*
  303. * Functions used for irqfd
  304. */
  305. /**
  306. * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
  307. * @intp: the IRQ struct handle
  308. * programs the VFIO driver to unmask this IRQ when the
  309. * intp->unmask eventfd is triggered
  310. */
  311. static int vfio_set_resample_eventfd(VFIOINTp *intp)
  312. {
  313. int32_t fd = event_notifier_get_fd(intp->unmask);
  314. VFIODevice *vbasedev = &intp->vdev->vbasedev;
  315. Error *err = NULL;
  316. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  317. if (!vfio_set_irq_signaling(vbasedev, intp->pin, 0,
  318. VFIO_IRQ_SET_ACTION_UNMASK, fd, &err)) {
  319. error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
  320. return -EINVAL;
  321. }
  322. return 0;
  323. }
  324. /**
  325. * vfio_start_irqfd_injection - starts the virtual IRQ injection using
  326. * irqfd
  327. *
  328. * @sbdev: the sysbus device handle
  329. * @irq: the qemu irq handle
  330. *
  331. * In case the irqfd setup fails, we fallback to userspace handled eventfd
  332. */
  333. static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)
  334. {
  335. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
  336. VFIOINTp *intp;
  337. if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
  338. !vdev->irqfd_allowed) {
  339. goto fail_irqfd;
  340. }
  341. QLIST_FOREACH(intp, &vdev->intp_list, next) {
  342. if (intp->qemuirq == irq) {
  343. break;
  344. }
  345. }
  346. assert(intp);
  347. if (kvm_irqchip_add_irqfd_notifier(kvm_state, intp->interrupt,
  348. intp->unmask, irq) < 0) {
  349. goto fail_irqfd;
  350. }
  351. if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
  352. goto fail_vfio;
  353. }
  354. if (vfio_irq_is_automasked(intp)) {
  355. if (vfio_set_resample_eventfd(intp) < 0) {
  356. goto fail_vfio;
  357. }
  358. trace_vfio_platform_start_level_irqfd_injection(intp->pin,
  359. event_notifier_get_fd(intp->interrupt),
  360. event_notifier_get_fd(intp->unmask));
  361. } else {
  362. trace_vfio_platform_start_edge_irqfd_injection(intp->pin,
  363. event_notifier_get_fd(intp->interrupt));
  364. }
  365. intp->kvm_accel = true;
  366. return;
  367. fail_vfio:
  368. kvm_irqchip_remove_irqfd_notifier(kvm_state, intp->interrupt, irq);
  369. abort();
  370. fail_irqfd:
  371. vfio_start_eventfd_injection(sbdev, irq);
  372. return;
  373. }
  374. /* VFIO skeleton */
  375. static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
  376. {
  377. vbasedev->needs_reset = true;
  378. }
  379. /* not implemented yet */
  380. static int vfio_platform_hot_reset_multi(VFIODevice *vbasedev)
  381. {
  382. return -1;
  383. }
  384. /**
  385. * vfio_populate_device - Allocate and populate MMIO region
  386. * and IRQ structs according to driver returned information
  387. * @vbasedev: the VFIO device handle
  388. * @errp: error object
  389. *
  390. */
  391. static bool vfio_populate_device(VFIODevice *vbasedev, Error **errp)
  392. {
  393. VFIOINTp *intp, *tmp;
  394. int i, ret = -1;
  395. VFIOPlatformDevice *vdev =
  396. container_of(vbasedev, VFIOPlatformDevice, vbasedev);
  397. if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
  398. error_setg(errp, "this isn't a platform device");
  399. return false;
  400. }
  401. vdev->regions = g_new0(VFIORegion *, vbasedev->num_regions);
  402. for (i = 0; i < vbasedev->num_regions; i++) {
  403. char *name = g_strdup_printf("VFIO %s region %d\n", vbasedev->name, i);
  404. vdev->regions[i] = g_new0(VFIORegion, 1);
  405. ret = vfio_region_setup(OBJECT(vdev), vbasedev,
  406. vdev->regions[i], i, name);
  407. g_free(name);
  408. if (ret) {
  409. error_setg_errno(errp, -ret, "failed to get region %d info", i);
  410. goto reg_error;
  411. }
  412. }
  413. vdev->mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  414. vfio_intp_mmap_enable, vdev);
  415. QSIMPLEQ_INIT(&vdev->pending_intp_queue);
  416. for (i = 0; i < vbasedev->num_irqs; i++) {
  417. struct vfio_irq_info irq = { .argsz = sizeof(irq) };
  418. irq.index = i;
  419. ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
  420. if (ret) {
  421. error_setg_errno(errp, -ret, "failed to get device irq info");
  422. goto irq_err;
  423. } else {
  424. trace_vfio_platform_populate_interrupts(irq.index,
  425. irq.count,
  426. irq.flags);
  427. intp = vfio_init_intp(vbasedev, irq, errp);
  428. if (!intp) {
  429. goto irq_err;
  430. }
  431. }
  432. }
  433. return true;
  434. irq_err:
  435. timer_del(vdev->mmap_timer);
  436. QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
  437. QLIST_REMOVE(intp, next);
  438. g_free(intp);
  439. }
  440. reg_error:
  441. for (i = 0; i < vbasedev->num_regions; i++) {
  442. if (vdev->regions[i]) {
  443. vfio_region_finalize(vdev->regions[i]);
  444. }
  445. g_free(vdev->regions[i]);
  446. }
  447. g_free(vdev->regions);
  448. return false;
  449. }
  450. /* specialized functions for VFIO Platform devices */
  451. static VFIODeviceOps vfio_platform_ops = {
  452. .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
  453. .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
  454. .vfio_eoi = vfio_platform_eoi,
  455. };
  456. /**
  457. * vfio_base_device_init - perform preliminary VFIO setup
  458. * @vbasedev: the VFIO device handle
  459. * @errp: error object
  460. *
  461. * Implement the VFIO command sequence that allows to discover
  462. * assigned device resources: group extraction, device
  463. * fd retrieval, resource query.
  464. * Precondition: the device name must be initialized
  465. */
  466. static bool vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
  467. {
  468. /* @fd takes precedence over @sysfsdev which takes precedence over @host */
  469. if (vbasedev->fd < 0 && vbasedev->sysfsdev) {
  470. g_free(vbasedev->name);
  471. vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
  472. } else if (vbasedev->fd < 0) {
  473. if (!vbasedev->name || strchr(vbasedev->name, '/')) {
  474. error_setg(errp, "wrong host device name");
  475. return false;
  476. }
  477. vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
  478. vbasedev->name);
  479. }
  480. if (!vfio_device_get_name(vbasedev, errp)) {
  481. return false;
  482. }
  483. if (!vfio_attach_device(vbasedev->name, vbasedev,
  484. &address_space_memory, errp)) {
  485. return false;
  486. }
  487. if (vfio_populate_device(vbasedev, errp)) {
  488. return true;
  489. }
  490. vfio_detach_device(vbasedev);
  491. return false;
  492. }
  493. /**
  494. * vfio_platform_realize - the device realize function
  495. * @dev: device state pointer
  496. * @errp: error
  497. *
  498. * initialize the device, its memory regions and IRQ structures
  499. * IRQ are started separately
  500. */
  501. static void vfio_platform_realize(DeviceState *dev, Error **errp)
  502. {
  503. ERRP_GUARD();
  504. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
  505. SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
  506. VFIODevice *vbasedev = &vdev->vbasedev;
  507. int i;
  508. warn_report("-device vfio-platform is deprecated");
  509. qemu_mutex_init(&vdev->intp_mutex);
  510. trace_vfio_platform_realize(vbasedev->sysfsdev ?
  511. vbasedev->sysfsdev : vbasedev->name,
  512. vdev->compat);
  513. if (!vfio_base_device_init(vbasedev, errp)) {
  514. goto init_err;
  515. }
  516. if (!vdev->compat) {
  517. GError *gerr = NULL;
  518. gchar *contents;
  519. gsize length;
  520. char *path;
  521. path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
  522. if (!g_file_get_contents(path, &contents, &length, &gerr)) {
  523. error_setg(errp, "%s", gerr->message);
  524. g_error_free(gerr);
  525. g_free(path);
  526. return;
  527. }
  528. g_free(path);
  529. vdev->compat = contents;
  530. for (vdev->num_compat = 0; length; vdev->num_compat++) {
  531. size_t skip = strlen(contents) + 1;
  532. contents += skip;
  533. length -= skip;
  534. }
  535. }
  536. for (i = 0; i < vbasedev->num_regions; i++) {
  537. if (vfio_region_mmap(vdev->regions[i])) {
  538. warn_report("%s mmap unsupported, performance may be slow",
  539. memory_region_name(vdev->regions[i]->mem));
  540. }
  541. sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
  542. }
  543. return;
  544. init_err:
  545. if (vdev->vbasedev.name) {
  546. error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  547. } else {
  548. error_prepend(errp, "vfio error: ");
  549. }
  550. }
  551. static const VMStateDescription vfio_platform_vmstate = {
  552. .name = "vfio-platform",
  553. .unmigratable = 1,
  554. };
  555. static const Property vfio_platform_dev_properties[] = {
  556. DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
  557. DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
  558. DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
  559. DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
  560. mmap_timeout, 1100),
  561. DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
  562. #ifdef CONFIG_IOMMUFD
  563. DEFINE_PROP_LINK("iommufd", VFIOPlatformDevice, vbasedev.iommufd,
  564. TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
  565. #endif
  566. };
  567. static void vfio_platform_instance_init(Object *obj)
  568. {
  569. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
  570. VFIODevice *vbasedev = &vdev->vbasedev;
  571. vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PLATFORM, &vfio_platform_ops,
  572. DEVICE(vdev), false);
  573. }
  574. #ifdef CONFIG_IOMMUFD
  575. static void vfio_platform_set_fd(Object *obj, const char *str, Error **errp)
  576. {
  577. vfio_device_set_fd(&VFIO_PLATFORM_DEVICE(obj)->vbasedev, str, errp);
  578. }
  579. #endif
  580. static void vfio_platform_class_init(ObjectClass *klass, void *data)
  581. {
  582. DeviceClass *dc = DEVICE_CLASS(klass);
  583. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  584. dc->realize = vfio_platform_realize;
  585. device_class_set_props(dc, vfio_platform_dev_properties);
  586. #ifdef CONFIG_IOMMUFD
  587. object_class_property_add_str(klass, "fd", NULL, vfio_platform_set_fd);
  588. #endif
  589. dc->vmsd = &vfio_platform_vmstate;
  590. dc->desc = "VFIO-based platform device assignment";
  591. sbc->connect_irq_notifier = vfio_start_irqfd_injection;
  592. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  593. object_class_property_set_description(klass, /* 2.4 */
  594. "host",
  595. "Host device name of assigned device");
  596. object_class_property_set_description(klass, /* 2.4 and 2.5 */
  597. "x-no-mmap",
  598. "Disable MMAP for device. Allows to trace MMIO "
  599. "accesses (DEBUG)");
  600. object_class_property_set_description(klass, /* 2.4 */
  601. "mmap-timeout-ms",
  602. "When EOI is not provided by KVM/QEMU, wait time "
  603. "(milliseconds) to re-enable device direct access "
  604. "after level interrupt (DEBUG)");
  605. object_class_property_set_description(klass, /* 2.4 */
  606. "x-irqfd",
  607. "Allow disabling irqfd support (DEBUG)");
  608. object_class_property_set_description(klass, /* 2.6 */
  609. "sysfsdev",
  610. "Host sysfs path of assigned device");
  611. #ifdef CONFIG_IOMMUFD
  612. object_class_property_set_description(klass, /* 9.0 */
  613. "iommufd",
  614. "Set host IOMMUFD backend device");
  615. #endif
  616. }
  617. static const TypeInfo vfio_platform_dev_info = {
  618. .name = TYPE_VFIO_PLATFORM,
  619. .parent = TYPE_DYNAMIC_SYS_BUS_DEVICE,
  620. .instance_size = sizeof(VFIOPlatformDevice),
  621. .instance_init = vfio_platform_instance_init,
  622. .class_init = vfio_platform_class_init,
  623. .class_size = sizeof(VFIOPlatformDeviceClass),
  624. };
  625. static void register_vfio_platform_dev_type(void)
  626. {
  627. type_register_static(&vfio_platform_dev_info);
  628. }
  629. type_init(register_vfio_platform_dev_type)