platform.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 "qapi/error.h"
  18. #include <sys/ioctl.h>
  19. #include <linux/vfio.h>
  20. #include "hw/vfio/vfio-platform.h"
  21. #include "migration/vmstate.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/lockable.h"
  24. #include "qemu/main-loop.h"
  25. #include "qemu/module.h"
  26. #include "qemu/range.h"
  27. #include "exec/memory.h"
  28. #include "exec/address-spaces.h"
  29. #include "qemu/queue.h"
  30. #include "hw/sysbus.h"
  31. #include "trace.h"
  32. #include "hw/irq.h"
  33. #include "hw/platform-bus.h"
  34. #include "hw/qdev-properties.h"
  35. #include "sysemu/kvm.h"
  36. /*
  37. * Functions used whatever the injection method
  38. */
  39. static inline bool vfio_irq_is_automasked(VFIOINTp *intp)
  40. {
  41. return intp->flags & VFIO_IRQ_INFO_AUTOMASKED;
  42. }
  43. /**
  44. * vfio_init_intp - allocate, initialize the IRQ struct pointer
  45. * and add it into the list of IRQs
  46. * @vbasedev: the VFIO device handle
  47. * @info: irq info struct retrieved from VFIO driver
  48. * @errp: error object
  49. */
  50. static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
  51. struct vfio_irq_info info, Error **errp)
  52. {
  53. int ret;
  54. VFIOPlatformDevice *vdev =
  55. container_of(vbasedev, VFIOPlatformDevice, vbasedev);
  56. SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev);
  57. VFIOINTp *intp;
  58. intp = g_malloc0(sizeof(*intp));
  59. intp->vdev = vdev;
  60. intp->pin = info.index;
  61. intp->flags = info.flags;
  62. intp->state = VFIO_IRQ_INACTIVE;
  63. intp->kvm_accel = false;
  64. sysbus_init_irq(sbdev, &intp->qemuirq);
  65. /* Get an eventfd for trigger */
  66. intp->interrupt = g_new0(EventNotifier, 1);
  67. ret = event_notifier_init(intp->interrupt, 0);
  68. if (ret) {
  69. g_free(intp->interrupt);
  70. g_free(intp);
  71. error_setg_errno(errp, -ret,
  72. "failed to initialize trigger eventfd notifier");
  73. return NULL;
  74. }
  75. if (vfio_irq_is_automasked(intp)) {
  76. /* Get an eventfd for resample/unmask */
  77. intp->unmask = g_new0(EventNotifier, 1);
  78. ret = event_notifier_init(intp->unmask, 0);
  79. if (ret) {
  80. g_free(intp->interrupt);
  81. g_free(intp->unmask);
  82. g_free(intp);
  83. error_setg_errno(errp, -ret,
  84. "failed to initialize resample eventfd notifier");
  85. return NULL;
  86. }
  87. }
  88. QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
  89. return intp;
  90. }
  91. /**
  92. * vfio_set_trigger_eventfd - set VFIO eventfd handling
  93. *
  94. * @intp: IRQ struct handle
  95. * @handler: handler to be called on eventfd signaling
  96. *
  97. * Setup VFIO signaling and attach an optional user-side handler
  98. * to the eventfd
  99. */
  100. static int vfio_set_trigger_eventfd(VFIOINTp *intp,
  101. eventfd_user_side_handler_t handler)
  102. {
  103. VFIODevice *vbasedev = &intp->vdev->vbasedev;
  104. int32_t fd = event_notifier_get_fd(intp->interrupt);
  105. Error *err = NULL;
  106. int ret;
  107. qemu_set_fd_handler(fd, (IOHandler *)handler, NULL, intp);
  108. ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
  109. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err);
  110. if (ret) {
  111. error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
  112. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  113. }
  114. return ret;
  115. }
  116. /*
  117. * Functions only used when eventfds are handled on user-side
  118. * ie. without irqfd
  119. */
  120. /**
  121. * vfio_mmap_set_enabled - enable/disable the fast path mode
  122. * @vdev: the VFIO platform device
  123. * @enabled: the target mmap state
  124. *
  125. * enabled = true ~ fast path = MMIO region is mmaped (no KVM TRAP);
  126. * enabled = false ~ slow path = MMIO region is trapped and region callbacks
  127. * are called; slow path enables to trap the device IRQ status register reset
  128. */
  129. static void vfio_mmap_set_enabled(VFIOPlatformDevice *vdev, bool enabled)
  130. {
  131. int i;
  132. for (i = 0; i < vdev->vbasedev.num_regions; i++) {
  133. vfio_region_mmaps_set_enabled(vdev->regions[i], enabled);
  134. }
  135. }
  136. /**
  137. * vfio_intp_mmap_enable - timer function, restores the fast path
  138. * if there is no more active IRQ
  139. * @opaque: actually points to the VFIO platform device
  140. *
  141. * Called on mmap timer timeout, this function checks whether the
  142. * IRQ is still active and if not, restores the fast path.
  143. * by construction a single eventfd is handled at a time.
  144. * if the IRQ is still active, the timer is re-programmed.
  145. */
  146. static void vfio_intp_mmap_enable(void *opaque)
  147. {
  148. VFIOINTp *tmp;
  149. VFIOPlatformDevice *vdev = (VFIOPlatformDevice *)opaque;
  150. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  151. QLIST_FOREACH(tmp, &vdev->intp_list, next) {
  152. if (tmp->state == VFIO_IRQ_ACTIVE) {
  153. trace_vfio_platform_intp_mmap_enable(tmp->pin);
  154. /* re-program the timer to check active status later */
  155. timer_mod(vdev->mmap_timer,
  156. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  157. vdev->mmap_timeout);
  158. return;
  159. }
  160. }
  161. vfio_mmap_set_enabled(vdev, true);
  162. }
  163. /**
  164. * vfio_intp_inject_pending_lockheld - Injects a pending IRQ
  165. * @opaque: opaque pointer, in practice the VFIOINTp handle
  166. *
  167. * The function is called on a previous IRQ completion, from
  168. * vfio_platform_eoi, while the intp_mutex is locked.
  169. * Also in such situation, the slow path already is set and
  170. * the mmap timer was already programmed.
  171. */
  172. static void vfio_intp_inject_pending_lockheld(VFIOINTp *intp)
  173. {
  174. trace_vfio_platform_intp_inject_pending_lockheld(intp->pin,
  175. event_notifier_get_fd(intp->interrupt));
  176. intp->state = VFIO_IRQ_ACTIVE;
  177. /* trigger the virtual IRQ */
  178. qemu_set_irq(intp->qemuirq, 1);
  179. }
  180. /**
  181. * vfio_intp_interrupt - The user-side eventfd handler
  182. * @opaque: opaque pointer which in practice is the VFIOINTp handle
  183. *
  184. * the function is entered in event handler context:
  185. * the vIRQ is injected into the guest if there is no other active
  186. * or pending IRQ.
  187. */
  188. static void vfio_intp_interrupt(VFIOINTp *intp)
  189. {
  190. int ret;
  191. VFIOINTp *tmp;
  192. VFIOPlatformDevice *vdev = intp->vdev;
  193. bool delay_handling = false;
  194. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  195. if (intp->state == VFIO_IRQ_INACTIVE) {
  196. QLIST_FOREACH(tmp, &vdev->intp_list, next) {
  197. if (tmp->state == VFIO_IRQ_ACTIVE ||
  198. tmp->state == VFIO_IRQ_PENDING) {
  199. delay_handling = true;
  200. break;
  201. }
  202. }
  203. }
  204. if (delay_handling) {
  205. /*
  206. * the new IRQ gets a pending status and is pushed in
  207. * the pending queue
  208. */
  209. intp->state = VFIO_IRQ_PENDING;
  210. trace_vfio_intp_interrupt_set_pending(intp->pin);
  211. QSIMPLEQ_INSERT_TAIL(&vdev->pending_intp_queue,
  212. intp, pqnext);
  213. event_notifier_test_and_clear(intp->interrupt);
  214. return;
  215. }
  216. trace_vfio_platform_intp_interrupt(intp->pin,
  217. event_notifier_get_fd(intp->interrupt));
  218. ret = event_notifier_test_and_clear(intp->interrupt);
  219. if (!ret) {
  220. error_report("Error when clearing fd=%d (ret = %d)",
  221. event_notifier_get_fd(intp->interrupt), ret);
  222. }
  223. intp->state = VFIO_IRQ_ACTIVE;
  224. /* sets slow path */
  225. vfio_mmap_set_enabled(vdev, false);
  226. /* trigger the virtual IRQ */
  227. qemu_set_irq(intp->qemuirq, 1);
  228. /*
  229. * Schedule the mmap timer which will restore fastpath when no IRQ
  230. * is active anymore
  231. */
  232. if (vdev->mmap_timeout) {
  233. timer_mod(vdev->mmap_timer,
  234. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  235. vdev->mmap_timeout);
  236. }
  237. }
  238. /**
  239. * vfio_platform_eoi - IRQ completion routine
  240. * @vbasedev: the VFIO device handle
  241. *
  242. * De-asserts the active virtual IRQ and unmasks the physical IRQ
  243. * (effective for level sensitive IRQ auto-masked by the VFIO driver).
  244. * Then it handles next pending IRQ if any.
  245. * eoi function is called on the first access to any MMIO region
  246. * after an IRQ was triggered, trapped since slow path was set.
  247. * It is assumed this access corresponds to the IRQ status
  248. * register reset. With such a mechanism, a single IRQ can be
  249. * handled at a time since there is no way to know which IRQ
  250. * was completed by the guest (we would need additional details
  251. * about the IRQ status register mask).
  252. */
  253. static void vfio_platform_eoi(VFIODevice *vbasedev)
  254. {
  255. VFIOINTp *intp;
  256. VFIOPlatformDevice *vdev =
  257. container_of(vbasedev, VFIOPlatformDevice, vbasedev);
  258. QEMU_LOCK_GUARD(&vdev->intp_mutex);
  259. QLIST_FOREACH(intp, &vdev->intp_list, next) {
  260. if (intp->state == VFIO_IRQ_ACTIVE) {
  261. trace_vfio_platform_eoi(intp->pin,
  262. event_notifier_get_fd(intp->interrupt));
  263. intp->state = VFIO_IRQ_INACTIVE;
  264. /* deassert the virtual IRQ */
  265. qemu_set_irq(intp->qemuirq, 0);
  266. if (vfio_irq_is_automasked(intp)) {
  267. /* unmasks the physical level-sensitive IRQ */
  268. vfio_unmask_single_irqindex(vbasedev, intp->pin);
  269. }
  270. /* a single IRQ can be active at a time */
  271. break;
  272. }
  273. }
  274. /* in case there are pending IRQs, handle the first one */
  275. if (!QSIMPLEQ_EMPTY(&vdev->pending_intp_queue)) {
  276. intp = QSIMPLEQ_FIRST(&vdev->pending_intp_queue);
  277. vfio_intp_inject_pending_lockheld(intp);
  278. QSIMPLEQ_REMOVE_HEAD(&vdev->pending_intp_queue, pqnext);
  279. }
  280. }
  281. /**
  282. * vfio_start_eventfd_injection - starts the virtual IRQ injection using
  283. * user-side handled eventfds
  284. * @sbdev: the sysbus device handle
  285. * @irq: the qemu irq handle
  286. */
  287. static void vfio_start_eventfd_injection(SysBusDevice *sbdev, qemu_irq irq)
  288. {
  289. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
  290. VFIOINTp *intp;
  291. QLIST_FOREACH(intp, &vdev->intp_list, next) {
  292. if (intp->qemuirq == irq) {
  293. break;
  294. }
  295. }
  296. assert(intp);
  297. if (vfio_set_trigger_eventfd(intp, vfio_intp_interrupt)) {
  298. abort();
  299. }
  300. }
  301. /*
  302. * Functions used for irqfd
  303. */
  304. /**
  305. * vfio_set_resample_eventfd - sets the resamplefd for an IRQ
  306. * @intp: the IRQ struct handle
  307. * programs the VFIO driver to unmask this IRQ when the
  308. * intp->unmask eventfd is triggered
  309. */
  310. static int vfio_set_resample_eventfd(VFIOINTp *intp)
  311. {
  312. int32_t fd = event_notifier_get_fd(intp->unmask);
  313. VFIODevice *vbasedev = &intp->vdev->vbasedev;
  314. Error *err = NULL;
  315. int ret;
  316. qemu_set_fd_handler(fd, NULL, NULL, NULL);
  317. ret = vfio_set_irq_signaling(vbasedev, intp->pin, 0,
  318. VFIO_IRQ_SET_ACTION_UNMASK, fd, &err);
  319. if (ret) {
  320. error_reportf_err(err, VFIO_MSG_PREFIX, vbasedev->name);
  321. }
  322. return ret;
  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 int 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 ret;
  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. ret = -1;
  430. goto irq_err;
  431. }
  432. }
  433. }
  434. return 0;
  435. irq_err:
  436. timer_del(vdev->mmap_timer);
  437. QLIST_FOREACH_SAFE(intp, &vdev->intp_list, next, tmp) {
  438. QLIST_REMOVE(intp, next);
  439. g_free(intp);
  440. }
  441. reg_error:
  442. for (i = 0; i < vbasedev->num_regions; i++) {
  443. if (vdev->regions[i]) {
  444. vfio_region_finalize(vdev->regions[i]);
  445. }
  446. g_free(vdev->regions[i]);
  447. }
  448. g_free(vdev->regions);
  449. return ret;
  450. }
  451. /* specialized functions for VFIO Platform devices */
  452. static VFIODeviceOps vfio_platform_ops = {
  453. .vfio_compute_needs_reset = vfio_platform_compute_needs_reset,
  454. .vfio_hot_reset_multi = vfio_platform_hot_reset_multi,
  455. .vfio_eoi = vfio_platform_eoi,
  456. };
  457. /**
  458. * vfio_base_device_init - perform preliminary VFIO setup
  459. * @vbasedev: the VFIO device handle
  460. * @errp: error object
  461. *
  462. * Implement the VFIO command sequence that allows to discover
  463. * assigned device resources: group extraction, device
  464. * fd retrieval, resource query.
  465. * Precondition: the device name must be initialized
  466. */
  467. static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
  468. {
  469. VFIOGroup *group;
  470. VFIODevice *vbasedev_iter;
  471. char *tmp, group_path[PATH_MAX], *group_name;
  472. ssize_t len;
  473. struct stat st;
  474. int groupid;
  475. int ret;
  476. /* @sysfsdev takes precedence over @host */
  477. if (vbasedev->sysfsdev) {
  478. g_free(vbasedev->name);
  479. vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
  480. } else {
  481. if (!vbasedev->name || strchr(vbasedev->name, '/')) {
  482. error_setg(errp, "wrong host device name");
  483. return -EINVAL;
  484. }
  485. vbasedev->sysfsdev = g_strdup_printf("/sys/bus/platform/devices/%s",
  486. vbasedev->name);
  487. }
  488. if (stat(vbasedev->sysfsdev, &st) < 0) {
  489. error_setg_errno(errp, errno,
  490. "failed to get the sysfs host device file status");
  491. return -errno;
  492. }
  493. tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
  494. len = readlink(tmp, group_path, sizeof(group_path));
  495. g_free(tmp);
  496. if (len < 0 || len >= sizeof(group_path)) {
  497. ret = len < 0 ? -errno : -ENAMETOOLONG;
  498. error_setg_errno(errp, -ret, "no iommu_group found");
  499. return ret;
  500. }
  501. group_path[len] = 0;
  502. group_name = basename(group_path);
  503. if (sscanf(group_name, "%d", &groupid) != 1) {
  504. error_setg_errno(errp, errno, "failed to read %s", group_path);
  505. return -errno;
  506. }
  507. trace_vfio_platform_base_device_init(vbasedev->name, groupid);
  508. group = vfio_get_group(groupid, &address_space_memory, errp);
  509. if (!group) {
  510. return -ENOENT;
  511. }
  512. QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
  513. if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) {
  514. error_setg(errp, "device is already attached");
  515. vfio_put_group(group);
  516. return -EBUSY;
  517. }
  518. }
  519. ret = vfio_get_device(group, vbasedev->name, vbasedev, errp);
  520. if (ret) {
  521. vfio_put_group(group);
  522. return ret;
  523. }
  524. ret = vfio_populate_device(vbasedev, errp);
  525. if (ret) {
  526. vfio_put_group(group);
  527. }
  528. return ret;
  529. }
  530. /**
  531. * vfio_platform_realize - the device realize function
  532. * @dev: device state pointer
  533. * @errp: error
  534. *
  535. * initialize the device, its memory regions and IRQ structures
  536. * IRQ are started separately
  537. */
  538. static void vfio_platform_realize(DeviceState *dev, Error **errp)
  539. {
  540. VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(dev);
  541. SysBusDevice *sbdev = SYS_BUS_DEVICE(dev);
  542. VFIODevice *vbasedev = &vdev->vbasedev;
  543. int i, ret;
  544. vbasedev->type = VFIO_DEVICE_TYPE_PLATFORM;
  545. vbasedev->dev = dev;
  546. vbasedev->ops = &vfio_platform_ops;
  547. qemu_mutex_init(&vdev->intp_mutex);
  548. trace_vfio_platform_realize(vbasedev->sysfsdev ?
  549. vbasedev->sysfsdev : vbasedev->name,
  550. vdev->compat);
  551. ret = vfio_base_device_init(vbasedev, errp);
  552. if (ret) {
  553. goto out;
  554. }
  555. if (!vdev->compat) {
  556. GError *gerr = NULL;
  557. gchar *contents;
  558. gsize length;
  559. char *path;
  560. path = g_strdup_printf("%s/of_node/compatible", vbasedev->sysfsdev);
  561. if (!g_file_get_contents(path, &contents, &length, &gerr)) {
  562. error_setg(errp, "%s", gerr->message);
  563. g_error_free(gerr);
  564. g_free(path);
  565. return;
  566. }
  567. g_free(path);
  568. vdev->compat = contents;
  569. for (vdev->num_compat = 0; length; vdev->num_compat++) {
  570. size_t skip = strlen(contents) + 1;
  571. contents += skip;
  572. length -= skip;
  573. }
  574. }
  575. for (i = 0; i < vbasedev->num_regions; i++) {
  576. if (vfio_region_mmap(vdev->regions[i])) {
  577. warn_report("%s mmap unsupported, performance may be slow",
  578. memory_region_name(vdev->regions[i]->mem));
  579. }
  580. sysbus_init_mmio(sbdev, vdev->regions[i]->mem);
  581. }
  582. out:
  583. if (!ret) {
  584. return;
  585. }
  586. if (vdev->vbasedev.name) {
  587. error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  588. } else {
  589. error_prepend(errp, "vfio error: ");
  590. }
  591. }
  592. static const VMStateDescription vfio_platform_vmstate = {
  593. .name = "vfio-platform",
  594. .unmigratable = 1,
  595. };
  596. static Property vfio_platform_dev_properties[] = {
  597. DEFINE_PROP_STRING("host", VFIOPlatformDevice, vbasedev.name),
  598. DEFINE_PROP_STRING("sysfsdev", VFIOPlatformDevice, vbasedev.sysfsdev),
  599. DEFINE_PROP_BOOL("x-no-mmap", VFIOPlatformDevice, vbasedev.no_mmap, false),
  600. DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
  601. mmap_timeout, 1100),
  602. DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
  603. DEFINE_PROP_END_OF_LIST(),
  604. };
  605. static void vfio_platform_class_init(ObjectClass *klass, void *data)
  606. {
  607. DeviceClass *dc = DEVICE_CLASS(klass);
  608. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  609. dc->realize = vfio_platform_realize;
  610. device_class_set_props(dc, vfio_platform_dev_properties);
  611. dc->vmsd = &vfio_platform_vmstate;
  612. dc->desc = "VFIO-based platform device assignment";
  613. sbc->connect_irq_notifier = vfio_start_irqfd_injection;
  614. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  615. /* Supported by TYPE_VIRT_MACHINE */
  616. dc->user_creatable = true;
  617. }
  618. static const TypeInfo vfio_platform_dev_info = {
  619. .name = TYPE_VFIO_PLATFORM,
  620. .parent = TYPE_SYS_BUS_DEVICE,
  621. .instance_size = sizeof(VFIOPlatformDevice),
  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)