2
0

display.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * display support for mdev based vgpu devices
  3. *
  4. * Copyright Red Hat, Inc. 2017
  5. *
  6. * Authors:
  7. * Gerd Hoffmann
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include <linux/vfio.h>
  14. #include <sys/ioctl.h>
  15. #include "qemu/error-report.h"
  16. #include "hw/display/edid.h"
  17. #include "ui/console.h"
  18. #include "qapi/error.h"
  19. #include "pci.h"
  20. #include "trace.h"
  21. #ifndef DRM_PLANE_TYPE_PRIMARY
  22. # define DRM_PLANE_TYPE_PRIMARY 1
  23. # define DRM_PLANE_TYPE_CURSOR 2
  24. #endif
  25. #define pread_field(_fd, _reg, _ptr, _fld) \
  26. (sizeof(_ptr->_fld) != \
  27. pread(_fd, &(_ptr->_fld), sizeof(_ptr->_fld), \
  28. _reg->offset + offsetof(typeof(*_ptr), _fld)))
  29. #define pwrite_field(_fd, _reg, _ptr, _fld) \
  30. (sizeof(_ptr->_fld) != \
  31. pwrite(_fd, &(_ptr->_fld), sizeof(_ptr->_fld), \
  32. _reg->offset + offsetof(typeof(*_ptr), _fld)))
  33. static void vfio_display_edid_link_up(void *opaque)
  34. {
  35. VFIOPCIDevice *vdev = opaque;
  36. VFIODisplay *dpy = vdev->dpy;
  37. int fd = vdev->vbasedev.fd;
  38. dpy->edid_regs->link_state = VFIO_DEVICE_GFX_LINK_STATE_UP;
  39. if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, link_state)) {
  40. goto err;
  41. }
  42. trace_vfio_display_edid_link_up();
  43. return;
  44. err:
  45. trace_vfio_display_edid_write_error();
  46. }
  47. static void vfio_display_edid_update(VFIOPCIDevice *vdev, bool enabled,
  48. int prefx, int prefy)
  49. {
  50. VFIODisplay *dpy = vdev->dpy;
  51. int fd = vdev->vbasedev.fd;
  52. qemu_edid_info edid = {
  53. .maxx = dpy->edid_regs->max_xres,
  54. .maxy = dpy->edid_regs->max_yres,
  55. .prefx = prefx ?: vdev->display_xres,
  56. .prefy = prefy ?: vdev->display_yres,
  57. };
  58. timer_del(dpy->edid_link_timer);
  59. dpy->edid_regs->link_state = VFIO_DEVICE_GFX_LINK_STATE_DOWN;
  60. if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, link_state)) {
  61. goto err;
  62. }
  63. trace_vfio_display_edid_link_down();
  64. if (!enabled) {
  65. return;
  66. }
  67. if (edid.maxx && edid.prefx > edid.maxx) {
  68. edid.prefx = edid.maxx;
  69. }
  70. if (edid.maxy && edid.prefy > edid.maxy) {
  71. edid.prefy = edid.maxy;
  72. }
  73. qemu_edid_generate(dpy->edid_blob,
  74. dpy->edid_regs->edid_max_size,
  75. &edid);
  76. trace_vfio_display_edid_update(edid.prefx, edid.prefy);
  77. dpy->edid_regs->edid_size = qemu_edid_size(dpy->edid_blob);
  78. if (pwrite_field(fd, dpy->edid_info, dpy->edid_regs, edid_size)) {
  79. goto err;
  80. }
  81. if (pwrite(fd, dpy->edid_blob, dpy->edid_regs->edid_size,
  82. dpy->edid_info->offset + dpy->edid_regs->edid_offset)
  83. != dpy->edid_regs->edid_size) {
  84. goto err;
  85. }
  86. timer_mod(dpy->edid_link_timer,
  87. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 100);
  88. return;
  89. err:
  90. trace_vfio_display_edid_write_error();
  91. return;
  92. }
  93. static void vfio_display_edid_ui_info(void *opaque, uint32_t idx,
  94. QemuUIInfo *info)
  95. {
  96. VFIOPCIDevice *vdev = opaque;
  97. VFIODisplay *dpy = vdev->dpy;
  98. if (!dpy->edid_regs) {
  99. return;
  100. }
  101. if (info->width && info->height) {
  102. vfio_display_edid_update(vdev, true, info->width, info->height);
  103. } else {
  104. vfio_display_edid_update(vdev, false, 0, 0);
  105. }
  106. }
  107. static bool vfio_display_edid_init(VFIOPCIDevice *vdev, Error **errp)
  108. {
  109. VFIODisplay *dpy = vdev->dpy;
  110. int fd = vdev->vbasedev.fd;
  111. int ret;
  112. ret = vfio_get_dev_region_info(&vdev->vbasedev,
  113. VFIO_REGION_TYPE_GFX,
  114. VFIO_REGION_SUBTYPE_GFX_EDID,
  115. &dpy->edid_info);
  116. if (ret) {
  117. /* Failed to get GFX edid info, allow to go through without edid. */
  118. return true;
  119. }
  120. trace_vfio_display_edid_available();
  121. dpy->edid_regs = g_new0(struct vfio_region_gfx_edid, 1);
  122. if (pread_field(fd, dpy->edid_info, dpy->edid_regs, edid_offset)) {
  123. goto err;
  124. }
  125. if (pread_field(fd, dpy->edid_info, dpy->edid_regs, edid_max_size)) {
  126. goto err;
  127. }
  128. if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_xres)) {
  129. goto err;
  130. }
  131. if (pread_field(fd, dpy->edid_info, dpy->edid_regs, max_yres)) {
  132. goto err;
  133. }
  134. dpy->edid_blob = g_malloc0(dpy->edid_regs->edid_max_size);
  135. /* if xres + yres properties are unset use the maximum resolution */
  136. if (!vdev->display_xres) {
  137. vdev->display_xres = dpy->edid_regs->max_xres;
  138. }
  139. if (!vdev->display_yres) {
  140. vdev->display_yres = dpy->edid_regs->max_yres;
  141. }
  142. dpy->edid_link_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  143. vfio_display_edid_link_up, vdev);
  144. vfio_display_edid_update(vdev, true, 0, 0);
  145. return true;
  146. err:
  147. error_setg(errp, "vfio: failed to read GFX edid field");
  148. trace_vfio_display_edid_write_error();
  149. g_free(dpy->edid_info);
  150. g_free(dpy->edid_regs);
  151. dpy->edid_info = NULL;
  152. dpy->edid_regs = NULL;
  153. return false;
  154. }
  155. static void vfio_display_edid_exit(VFIODisplay *dpy)
  156. {
  157. if (!dpy->edid_regs) {
  158. return;
  159. }
  160. g_free(dpy->edid_info);
  161. g_free(dpy->edid_regs);
  162. g_free(dpy->edid_blob);
  163. timer_free(dpy->edid_link_timer);
  164. }
  165. static void vfio_display_update_cursor(VFIODMABuf *dmabuf,
  166. struct vfio_device_gfx_plane_info *plane)
  167. {
  168. if (dmabuf->pos_x != plane->x_pos || dmabuf->pos_y != plane->y_pos) {
  169. dmabuf->pos_x = plane->x_pos;
  170. dmabuf->pos_y = plane->y_pos;
  171. dmabuf->pos_updates++;
  172. }
  173. if (dmabuf->hot_x != plane->x_hot || dmabuf->hot_y != plane->y_hot) {
  174. dmabuf->hot_x = plane->x_hot;
  175. dmabuf->hot_y = plane->y_hot;
  176. dmabuf->hot_updates++;
  177. }
  178. }
  179. static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
  180. uint32_t plane_type)
  181. {
  182. VFIODisplay *dpy = vdev->dpy;
  183. struct vfio_device_gfx_plane_info plane;
  184. VFIODMABuf *dmabuf;
  185. int fd, ret;
  186. memset(&plane, 0, sizeof(plane));
  187. plane.argsz = sizeof(plane);
  188. plane.flags = VFIO_GFX_PLANE_TYPE_DMABUF;
  189. plane.drm_plane_type = plane_type;
  190. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
  191. if (ret < 0) {
  192. return NULL;
  193. }
  194. if (!plane.drm_format || !plane.size) {
  195. return NULL;
  196. }
  197. QTAILQ_FOREACH(dmabuf, &dpy->dmabuf.bufs, next) {
  198. if (dmabuf->dmabuf_id == plane.dmabuf_id) {
  199. /* found in list, move to head, return it */
  200. QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
  201. QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
  202. if (plane_type == DRM_PLANE_TYPE_CURSOR) {
  203. vfio_display_update_cursor(dmabuf, &plane);
  204. }
  205. return dmabuf;
  206. }
  207. }
  208. fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id);
  209. if (fd < 0) {
  210. return NULL;
  211. }
  212. dmabuf = g_new0(VFIODMABuf, 1);
  213. dmabuf->dmabuf_id = plane.dmabuf_id;
  214. dmabuf->buf = qemu_dmabuf_new(plane.width, plane.height,
  215. plane.stride, 0, 0, plane.width,
  216. plane.height, plane.drm_format,
  217. plane.drm_format_mod, fd, false, false);
  218. if (plane_type == DRM_PLANE_TYPE_CURSOR) {
  219. vfio_display_update_cursor(dmabuf, &plane);
  220. }
  221. QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
  222. return dmabuf;
  223. }
  224. static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
  225. {
  226. QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
  227. qemu_dmabuf_close(dmabuf->buf);
  228. dpy_gl_release_dmabuf(dpy->con, dmabuf->buf);
  229. g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
  230. g_free(dmabuf);
  231. }
  232. static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev)
  233. {
  234. VFIODisplay *dpy = vdev->dpy;
  235. VFIODMABuf *dmabuf, *tmp;
  236. uint32_t keep = 5;
  237. QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) {
  238. if (keep > 0) {
  239. keep--;
  240. continue;
  241. }
  242. assert(dmabuf != dpy->dmabuf.primary);
  243. vfio_display_free_one_dmabuf(dpy, dmabuf);
  244. }
  245. }
  246. static void vfio_display_dmabuf_update(void *opaque)
  247. {
  248. VFIOPCIDevice *vdev = opaque;
  249. VFIODisplay *dpy = vdev->dpy;
  250. VFIODMABuf *primary, *cursor;
  251. uint32_t width, height;
  252. bool free_bufs = false, new_cursor = false;
  253. primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY);
  254. if (primary == NULL) {
  255. if (dpy->ramfb) {
  256. ramfb_display_update(dpy->con, dpy->ramfb);
  257. }
  258. return;
  259. }
  260. width = qemu_dmabuf_get_width(primary->buf);
  261. height = qemu_dmabuf_get_height(primary->buf);
  262. if (dpy->dmabuf.primary != primary) {
  263. dpy->dmabuf.primary = primary;
  264. qemu_console_resize(dpy->con, width, height);
  265. dpy_gl_scanout_dmabuf(dpy->con, primary->buf);
  266. free_bufs = true;
  267. }
  268. cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR);
  269. if (dpy->dmabuf.cursor != cursor) {
  270. dpy->dmabuf.cursor = cursor;
  271. new_cursor = true;
  272. free_bufs = true;
  273. }
  274. if (cursor && (new_cursor || cursor->hot_updates)) {
  275. bool have_hot = (cursor->hot_x != 0xffffffff &&
  276. cursor->hot_y != 0xffffffff);
  277. dpy_gl_cursor_dmabuf(dpy->con, cursor->buf, have_hot,
  278. cursor->hot_x, cursor->hot_y);
  279. cursor->hot_updates = 0;
  280. } else if (!cursor && new_cursor) {
  281. dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0);
  282. }
  283. if (cursor && cursor->pos_updates) {
  284. dpy_gl_cursor_position(dpy->con,
  285. cursor->pos_x,
  286. cursor->pos_y);
  287. cursor->pos_updates = 0;
  288. }
  289. dpy_gl_update(dpy->con, 0, 0, width, height);
  290. if (free_bufs) {
  291. vfio_display_free_dmabufs(vdev);
  292. }
  293. }
  294. static int vfio_display_get_flags(void *opaque)
  295. {
  296. return GRAPHIC_FLAGS_GL | GRAPHIC_FLAGS_DMABUF;
  297. }
  298. static const GraphicHwOps vfio_display_dmabuf_ops = {
  299. .get_flags = vfio_display_get_flags,
  300. .gfx_update = vfio_display_dmabuf_update,
  301. .ui_info = vfio_display_edid_ui_info,
  302. };
  303. static bool vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp)
  304. {
  305. if (!display_opengl) {
  306. error_setg(errp, "vfio-display-dmabuf: opengl not available");
  307. return false;
  308. }
  309. vdev->dpy = g_new0(VFIODisplay, 1);
  310. vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
  311. &vfio_display_dmabuf_ops,
  312. vdev);
  313. if (vdev->enable_ramfb) {
  314. vdev->dpy->ramfb = ramfb_setup(errp);
  315. if (!vdev->dpy->ramfb) {
  316. return false;
  317. }
  318. }
  319. return vfio_display_edid_init(vdev, errp);
  320. }
  321. static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
  322. {
  323. VFIODMABuf *dmabuf;
  324. if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) {
  325. return;
  326. }
  327. while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) {
  328. vfio_display_free_one_dmabuf(dpy, dmabuf);
  329. }
  330. }
  331. /* ---------------------------------------------------------------------- */
  332. void vfio_display_reset(VFIOPCIDevice *vdev)
  333. {
  334. if (!vdev || !vdev->dpy || !vdev->dpy->con ||
  335. !vdev->dpy->dmabuf.primary) {
  336. return;
  337. }
  338. dpy_gl_scanout_disable(vdev->dpy->con);
  339. vfio_display_dmabuf_exit(vdev->dpy);
  340. dpy_gfx_update_full(vdev->dpy->con);
  341. }
  342. static void vfio_display_region_update(void *opaque)
  343. {
  344. VFIOPCIDevice *vdev = opaque;
  345. VFIODisplay *dpy = vdev->dpy;
  346. struct vfio_device_gfx_plane_info plane = {
  347. .argsz = sizeof(plane),
  348. .flags = VFIO_GFX_PLANE_TYPE_REGION
  349. };
  350. pixman_format_code_t format;
  351. int ret;
  352. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
  353. if (ret < 0) {
  354. error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s",
  355. strerror(errno));
  356. return;
  357. }
  358. if (!plane.drm_format || !plane.size) {
  359. if (dpy->ramfb) {
  360. ramfb_display_update(dpy->con, dpy->ramfb);
  361. dpy->region.surface = NULL;
  362. }
  363. return;
  364. }
  365. format = qemu_drm_format_to_pixman(plane.drm_format);
  366. if (!format) {
  367. return;
  368. }
  369. if (dpy->region.buffer.size &&
  370. dpy->region.buffer.nr != plane.region_index) {
  371. /* region changed */
  372. vfio_region_exit(&dpy->region.buffer);
  373. vfio_region_finalize(&dpy->region.buffer);
  374. dpy->region.surface = NULL;
  375. }
  376. if (dpy->region.surface &&
  377. (surface_width(dpy->region.surface) != plane.width ||
  378. surface_height(dpy->region.surface) != plane.height ||
  379. surface_format(dpy->region.surface) != format)) {
  380. /* size changed */
  381. dpy->region.surface = NULL;
  382. }
  383. if (!dpy->region.buffer.size) {
  384. /* mmap region */
  385. ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev,
  386. &dpy->region.buffer,
  387. plane.region_index,
  388. "display");
  389. if (ret != 0) {
  390. error_report("%s: vfio_region_setup(%d): %s",
  391. __func__, plane.region_index, strerror(-ret));
  392. goto err;
  393. }
  394. ret = vfio_region_mmap(&dpy->region.buffer);
  395. if (ret != 0) {
  396. error_report("%s: vfio_region_mmap(%d): %s", __func__,
  397. plane.region_index, strerror(-ret));
  398. goto err;
  399. }
  400. assert(dpy->region.buffer.mmaps[0].mmap != NULL);
  401. }
  402. if (dpy->region.surface == NULL) {
  403. /* create surface */
  404. dpy->region.surface = qemu_create_displaysurface_from
  405. (plane.width, plane.height, format,
  406. plane.stride, dpy->region.buffer.mmaps[0].mmap);
  407. dpy_gfx_replace_surface(dpy->con, dpy->region.surface);
  408. }
  409. /* full screen update */
  410. dpy_gfx_update(dpy->con, 0, 0,
  411. surface_width(dpy->region.surface),
  412. surface_height(dpy->region.surface));
  413. return;
  414. err:
  415. vfio_region_exit(&dpy->region.buffer);
  416. vfio_region_finalize(&dpy->region.buffer);
  417. }
  418. static const GraphicHwOps vfio_display_region_ops = {
  419. .gfx_update = vfio_display_region_update,
  420. };
  421. static bool vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp)
  422. {
  423. vdev->dpy = g_new0(VFIODisplay, 1);
  424. vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
  425. &vfio_display_region_ops,
  426. vdev);
  427. if (vdev->enable_ramfb) {
  428. vdev->dpy->ramfb = ramfb_setup(errp);
  429. if (!vdev->dpy->ramfb) {
  430. return false;
  431. }
  432. }
  433. return true;
  434. }
  435. static void vfio_display_region_exit(VFIODisplay *dpy)
  436. {
  437. if (!dpy->region.buffer.size) {
  438. return;
  439. }
  440. vfio_region_exit(&dpy->region.buffer);
  441. vfio_region_finalize(&dpy->region.buffer);
  442. }
  443. /* ---------------------------------------------------------------------- */
  444. bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp)
  445. {
  446. struct vfio_device_gfx_plane_info probe;
  447. int ret;
  448. memset(&probe, 0, sizeof(probe));
  449. probe.argsz = sizeof(probe);
  450. probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF;
  451. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
  452. if (ret == 0) {
  453. return vfio_display_dmabuf_init(vdev, errp);
  454. }
  455. memset(&probe, 0, sizeof(probe));
  456. probe.argsz = sizeof(probe);
  457. probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION;
  458. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
  459. if (ret == 0) {
  460. return vfio_display_region_init(vdev, errp);
  461. }
  462. if (vdev->display == ON_OFF_AUTO_AUTO) {
  463. /* not an error in automatic mode */
  464. return true;
  465. }
  466. error_setg(errp, "vfio: device doesn't support any (known) display method");
  467. return false;
  468. }
  469. void vfio_display_finalize(VFIOPCIDevice *vdev)
  470. {
  471. if (!vdev->dpy) {
  472. return;
  473. }
  474. graphic_console_close(vdev->dpy->con);
  475. vfio_display_dmabuf_exit(vdev->dpy);
  476. vfio_display_region_exit(vdev->dpy);
  477. vfio_display_edid_exit(vdev->dpy);
  478. g_free(vdev->dpy);
  479. }
  480. static bool migrate_needed(void *opaque)
  481. {
  482. VFIODisplay *dpy = opaque;
  483. bool ramfb_exists = dpy->ramfb != NULL;
  484. /* see vfio_display_migration_needed() */
  485. assert(ramfb_exists);
  486. return ramfb_exists;
  487. }
  488. const VMStateDescription vfio_display_vmstate = {
  489. .name = "VFIODisplay",
  490. .version_id = 1,
  491. .minimum_version_id = 1,
  492. .needed = migrate_needed,
  493. .fields = (const VMStateField[]) {
  494. VMSTATE_STRUCT_POINTER(ramfb, VFIODisplay, ramfb_vmstate, RAMFBState),
  495. VMSTATE_END_OF_LIST(),
  496. }
  497. };