display.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 "sysemu/sysemu.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 int 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 0;
  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. return 0;
  107. }
  108. static void vfio_display_edid_init(VFIOPCIDevice *vdev)
  109. {
  110. VFIODisplay *dpy = vdev->dpy;
  111. int fd = vdev->vbasedev.fd;
  112. int ret;
  113. ret = vfio_get_dev_region_info(&vdev->vbasedev,
  114. VFIO_REGION_TYPE_GFX,
  115. VFIO_REGION_SUBTYPE_GFX_EDID,
  116. &dpy->edid_info);
  117. if (ret) {
  118. return;
  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;
  146. err:
  147. trace_vfio_display_edid_write_error();
  148. g_free(dpy->edid_regs);
  149. dpy->edid_regs = NULL;
  150. return;
  151. }
  152. static void vfio_display_edid_exit(VFIODisplay *dpy)
  153. {
  154. if (!dpy->edid_regs) {
  155. return;
  156. }
  157. g_free(dpy->edid_regs);
  158. g_free(dpy->edid_blob);
  159. timer_del(dpy->edid_link_timer);
  160. timer_free(dpy->edid_link_timer);
  161. }
  162. static void vfio_display_update_cursor(VFIODMABuf *dmabuf,
  163. struct vfio_device_gfx_plane_info *plane)
  164. {
  165. if (dmabuf->pos_x != plane->x_pos || dmabuf->pos_y != plane->y_pos) {
  166. dmabuf->pos_x = plane->x_pos;
  167. dmabuf->pos_y = plane->y_pos;
  168. dmabuf->pos_updates++;
  169. }
  170. if (dmabuf->hot_x != plane->x_hot || dmabuf->hot_y != plane->y_hot) {
  171. dmabuf->hot_x = plane->x_hot;
  172. dmabuf->hot_y = plane->y_hot;
  173. dmabuf->hot_updates++;
  174. }
  175. }
  176. static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
  177. uint32_t plane_type)
  178. {
  179. VFIODisplay *dpy = vdev->dpy;
  180. struct vfio_device_gfx_plane_info plane;
  181. VFIODMABuf *dmabuf;
  182. int fd, ret;
  183. memset(&plane, 0, sizeof(plane));
  184. plane.argsz = sizeof(plane);
  185. plane.flags = VFIO_GFX_PLANE_TYPE_DMABUF;
  186. plane.drm_plane_type = plane_type;
  187. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
  188. if (ret < 0) {
  189. return NULL;
  190. }
  191. if (!plane.drm_format || !plane.size) {
  192. return NULL;
  193. }
  194. QTAILQ_FOREACH(dmabuf, &dpy->dmabuf.bufs, next) {
  195. if (dmabuf->dmabuf_id == plane.dmabuf_id) {
  196. /* found in list, move to head, return it */
  197. QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
  198. QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
  199. if (plane_type == DRM_PLANE_TYPE_CURSOR) {
  200. vfio_display_update_cursor(dmabuf, &plane);
  201. }
  202. return dmabuf;
  203. }
  204. }
  205. fd = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &plane.dmabuf_id);
  206. if (fd < 0) {
  207. return NULL;
  208. }
  209. dmabuf = g_new0(VFIODMABuf, 1);
  210. dmabuf->dmabuf_id = plane.dmabuf_id;
  211. dmabuf->buf.width = plane.width;
  212. dmabuf->buf.height = plane.height;
  213. dmabuf->buf.stride = plane.stride;
  214. dmabuf->buf.fourcc = plane.drm_format;
  215. dmabuf->buf.modifier = plane.drm_format_mod;
  216. dmabuf->buf.fd = fd;
  217. if (plane_type == DRM_PLANE_TYPE_CURSOR) {
  218. vfio_display_update_cursor(dmabuf, &plane);
  219. }
  220. QTAILQ_INSERT_HEAD(&dpy->dmabuf.bufs, dmabuf, next);
  221. return dmabuf;
  222. }
  223. static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
  224. {
  225. QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
  226. dpy_gl_release_dmabuf(dpy->con, &dmabuf->buf);
  227. close(dmabuf->buf.fd);
  228. g_free(dmabuf);
  229. }
  230. static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev)
  231. {
  232. VFIODisplay *dpy = vdev->dpy;
  233. VFIODMABuf *dmabuf, *tmp;
  234. uint32_t keep = 5;
  235. QTAILQ_FOREACH_SAFE(dmabuf, &dpy->dmabuf.bufs, next, tmp) {
  236. if (keep > 0) {
  237. keep--;
  238. continue;
  239. }
  240. assert(dmabuf != dpy->dmabuf.primary);
  241. vfio_display_free_one_dmabuf(dpy, dmabuf);
  242. }
  243. }
  244. static void vfio_display_dmabuf_update(void *opaque)
  245. {
  246. VFIOPCIDevice *vdev = opaque;
  247. VFIODisplay *dpy = vdev->dpy;
  248. VFIODMABuf *primary, *cursor;
  249. bool free_bufs = false, new_cursor = false;;
  250. primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY);
  251. if (primary == NULL) {
  252. if (dpy->ramfb) {
  253. ramfb_display_update(dpy->con, dpy->ramfb);
  254. }
  255. return;
  256. }
  257. if (dpy->dmabuf.primary != primary) {
  258. dpy->dmabuf.primary = primary;
  259. qemu_console_resize(dpy->con,
  260. primary->buf.width, primary->buf.height);
  261. dpy_gl_scanout_dmabuf(dpy->con, &primary->buf);
  262. free_bufs = true;
  263. }
  264. cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR);
  265. if (dpy->dmabuf.cursor != cursor) {
  266. dpy->dmabuf.cursor = cursor;
  267. new_cursor = true;
  268. free_bufs = true;
  269. }
  270. if (cursor && (new_cursor || cursor->hot_updates)) {
  271. bool have_hot = (cursor->hot_x != 0xffffffff &&
  272. cursor->hot_y != 0xffffffff);
  273. dpy_gl_cursor_dmabuf(dpy->con, &cursor->buf, have_hot,
  274. cursor->hot_x, cursor->hot_y);
  275. cursor->hot_updates = 0;
  276. } else if (!cursor && new_cursor) {
  277. dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0);
  278. }
  279. if (cursor && cursor->pos_updates) {
  280. dpy_gl_cursor_position(dpy->con,
  281. cursor->pos_x,
  282. cursor->pos_y);
  283. cursor->pos_updates = 0;
  284. }
  285. dpy_gl_update(dpy->con, 0, 0, primary->buf.width, primary->buf.height);
  286. if (free_bufs) {
  287. vfio_display_free_dmabufs(vdev);
  288. }
  289. }
  290. static const GraphicHwOps vfio_display_dmabuf_ops = {
  291. .gfx_update = vfio_display_dmabuf_update,
  292. .ui_info = vfio_display_edid_ui_info,
  293. };
  294. static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp)
  295. {
  296. if (!display_opengl) {
  297. error_setg(errp, "vfio-display-dmabuf: opengl not available");
  298. return -1;
  299. }
  300. vdev->dpy = g_new0(VFIODisplay, 1);
  301. vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
  302. &vfio_display_dmabuf_ops,
  303. vdev);
  304. if (vdev->enable_ramfb) {
  305. vdev->dpy->ramfb = ramfb_setup(DEVICE(vdev), errp);
  306. }
  307. vfio_display_edid_init(vdev);
  308. return 0;
  309. }
  310. static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
  311. {
  312. VFIODMABuf *dmabuf;
  313. if (QTAILQ_EMPTY(&dpy->dmabuf.bufs)) {
  314. return;
  315. }
  316. while ((dmabuf = QTAILQ_FIRST(&dpy->dmabuf.bufs)) != NULL) {
  317. vfio_display_free_one_dmabuf(dpy, dmabuf);
  318. }
  319. }
  320. /* ---------------------------------------------------------------------- */
  321. void vfio_display_reset(VFIOPCIDevice *vdev)
  322. {
  323. if (!vdev || !vdev->dpy || !vdev->dpy->con ||
  324. !vdev->dpy->dmabuf.primary) {
  325. return;
  326. }
  327. dpy_gl_scanout_disable(vdev->dpy->con);
  328. vfio_display_dmabuf_exit(vdev->dpy);
  329. dpy_gfx_update_full(vdev->dpy->con);
  330. }
  331. static void vfio_display_region_update(void *opaque)
  332. {
  333. VFIOPCIDevice *vdev = opaque;
  334. VFIODisplay *dpy = vdev->dpy;
  335. struct vfio_device_gfx_plane_info plane = {
  336. .argsz = sizeof(plane),
  337. .flags = VFIO_GFX_PLANE_TYPE_REGION
  338. };
  339. pixman_format_code_t format;
  340. int ret;
  341. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane);
  342. if (ret < 0) {
  343. error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s",
  344. strerror(errno));
  345. return;
  346. }
  347. if (!plane.drm_format || !plane.size) {
  348. if (dpy->ramfb) {
  349. ramfb_display_update(dpy->con, dpy->ramfb);
  350. }
  351. return;
  352. }
  353. format = qemu_drm_format_to_pixman(plane.drm_format);
  354. if (!format) {
  355. return;
  356. }
  357. if (dpy->region.buffer.size &&
  358. dpy->region.buffer.nr != plane.region_index) {
  359. /* region changed */
  360. vfio_region_exit(&dpy->region.buffer);
  361. vfio_region_finalize(&dpy->region.buffer);
  362. dpy->region.surface = NULL;
  363. }
  364. if (dpy->region.surface &&
  365. (surface_width(dpy->region.surface) != plane.width ||
  366. surface_height(dpy->region.surface) != plane.height ||
  367. surface_format(dpy->region.surface) != format)) {
  368. /* size changed */
  369. dpy->region.surface = NULL;
  370. }
  371. if (!dpy->region.buffer.size) {
  372. /* mmap region */
  373. ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev,
  374. &dpy->region.buffer,
  375. plane.region_index,
  376. "display");
  377. if (ret != 0) {
  378. error_report("%s: vfio_region_setup(%d): %s",
  379. __func__, plane.region_index, strerror(-ret));
  380. goto err;
  381. }
  382. ret = vfio_region_mmap(&dpy->region.buffer);
  383. if (ret != 0) {
  384. error_report("%s: vfio_region_mmap(%d): %s", __func__,
  385. plane.region_index, strerror(-ret));
  386. goto err;
  387. }
  388. assert(dpy->region.buffer.mmaps[0].mmap != NULL);
  389. }
  390. if (dpy->region.surface == NULL) {
  391. /* create surface */
  392. dpy->region.surface = qemu_create_displaysurface_from
  393. (plane.width, plane.height, format,
  394. plane.stride, dpy->region.buffer.mmaps[0].mmap);
  395. dpy_gfx_replace_surface(dpy->con, dpy->region.surface);
  396. }
  397. /* full screen update */
  398. dpy_gfx_update(dpy->con, 0, 0,
  399. surface_width(dpy->region.surface),
  400. surface_height(dpy->region.surface));
  401. return;
  402. err:
  403. vfio_region_exit(&dpy->region.buffer);
  404. vfio_region_finalize(&dpy->region.buffer);
  405. }
  406. static const GraphicHwOps vfio_display_region_ops = {
  407. .gfx_update = vfio_display_region_update,
  408. };
  409. static int vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp)
  410. {
  411. vdev->dpy = g_new0(VFIODisplay, 1);
  412. vdev->dpy->con = graphic_console_init(DEVICE(vdev), 0,
  413. &vfio_display_region_ops,
  414. vdev);
  415. if (vdev->enable_ramfb) {
  416. vdev->dpy->ramfb = ramfb_setup(DEVICE(vdev), errp);
  417. }
  418. return 0;
  419. }
  420. static void vfio_display_region_exit(VFIODisplay *dpy)
  421. {
  422. if (!dpy->region.buffer.size) {
  423. return;
  424. }
  425. vfio_region_exit(&dpy->region.buffer);
  426. vfio_region_finalize(&dpy->region.buffer);
  427. }
  428. /* ---------------------------------------------------------------------- */
  429. int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp)
  430. {
  431. struct vfio_device_gfx_plane_info probe;
  432. int ret;
  433. memset(&probe, 0, sizeof(probe));
  434. probe.argsz = sizeof(probe);
  435. probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF;
  436. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
  437. if (ret == 0) {
  438. return vfio_display_dmabuf_init(vdev, errp);
  439. }
  440. memset(&probe, 0, sizeof(probe));
  441. probe.argsz = sizeof(probe);
  442. probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION;
  443. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
  444. if (ret == 0) {
  445. return vfio_display_region_init(vdev, errp);
  446. }
  447. if (vdev->display == ON_OFF_AUTO_AUTO) {
  448. /* not an error in automatic mode */
  449. return 0;
  450. }
  451. error_setg(errp, "vfio: device doesn't support any (known) display method");
  452. return -1;
  453. }
  454. void vfio_display_finalize(VFIOPCIDevice *vdev)
  455. {
  456. if (!vdev->dpy) {
  457. return;
  458. }
  459. graphic_console_close(vdev->dpy->con);
  460. vfio_display_dmabuf_exit(vdev->dpy);
  461. vfio_display_region_exit(vdev->dpy);
  462. vfio_display_edid_exit(vdev->dpy);
  463. g_free(vdev->dpy);
  464. }