main.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * Virtio vhost-user GPU Device
  3. *
  4. * Copyright Red Hat, Inc. 2013-2018
  5. *
  6. * Authors:
  7. * Dave Airlie <airlied@redhat.com>
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. * Marc-André Lureau <marcandre.lureau@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/drm.h"
  16. #include "qapi/error.h"
  17. #include "qemu/sockets.h"
  18. #include <pixman.h>
  19. #include <glib-unix.h>
  20. #include "vugpu.h"
  21. #include "hw/virtio/virtio-gpu-bswap.h"
  22. #include "hw/virtio/virtio-gpu-pixman.h"
  23. #include "virgl.h"
  24. #include "vugbm.h"
  25. enum {
  26. VHOST_USER_GPU_MAX_QUEUES = 2,
  27. };
  28. struct virtio_gpu_simple_resource {
  29. uint32_t resource_id;
  30. uint32_t width;
  31. uint32_t height;
  32. uint32_t format;
  33. struct iovec *iov;
  34. unsigned int iov_cnt;
  35. uint32_t scanout_bitmask;
  36. pixman_image_t *image;
  37. struct vugbm_buffer buffer;
  38. QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
  39. };
  40. static gboolean opt_print_caps;
  41. static int opt_fdnum = -1;
  42. static char *opt_socket_path;
  43. static char *opt_render_node;
  44. static gboolean opt_virgl;
  45. static void vg_handle_ctrl(VuDev *dev, int qidx);
  46. static const char *
  47. vg_cmd_to_string(int cmd)
  48. {
  49. #define CMD(cmd) [cmd] = #cmd
  50. static const char *vg_cmd_str[] = {
  51. CMD(VIRTIO_GPU_UNDEFINED),
  52. /* 2d commands */
  53. CMD(VIRTIO_GPU_CMD_GET_DISPLAY_INFO),
  54. CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_2D),
  55. CMD(VIRTIO_GPU_CMD_RESOURCE_UNREF),
  56. CMD(VIRTIO_GPU_CMD_SET_SCANOUT),
  57. CMD(VIRTIO_GPU_CMD_RESOURCE_FLUSH),
  58. CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D),
  59. CMD(VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING),
  60. CMD(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING),
  61. CMD(VIRTIO_GPU_CMD_GET_CAPSET_INFO),
  62. CMD(VIRTIO_GPU_CMD_GET_CAPSET),
  63. /* 3d commands */
  64. CMD(VIRTIO_GPU_CMD_CTX_CREATE),
  65. CMD(VIRTIO_GPU_CMD_CTX_DESTROY),
  66. CMD(VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE),
  67. CMD(VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE),
  68. CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_3D),
  69. CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D),
  70. CMD(VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D),
  71. CMD(VIRTIO_GPU_CMD_SUBMIT_3D),
  72. /* cursor commands */
  73. CMD(VIRTIO_GPU_CMD_UPDATE_CURSOR),
  74. CMD(VIRTIO_GPU_CMD_MOVE_CURSOR),
  75. };
  76. #undef REQ
  77. if (cmd >= 0 && cmd < G_N_ELEMENTS(vg_cmd_str)) {
  78. return vg_cmd_str[cmd];
  79. } else {
  80. return "unknown";
  81. }
  82. }
  83. static int
  84. vg_sock_fd_read(int sock, void *buf, ssize_t buflen)
  85. {
  86. int ret;
  87. do {
  88. ret = read(sock, buf, buflen);
  89. } while (ret < 0 && (errno == EINTR || errno == EAGAIN));
  90. g_warn_if_fail(ret == buflen);
  91. return ret;
  92. }
  93. static void
  94. vg_sock_fd_close(VuGpu *g)
  95. {
  96. if (g->sock_fd >= 0) {
  97. close(g->sock_fd);
  98. g->sock_fd = -1;
  99. }
  100. }
  101. static gboolean
  102. source_wait_cb(gint fd, GIOCondition condition, gpointer user_data)
  103. {
  104. VuGpu *g = user_data;
  105. if (!vg_recv_msg(g, VHOST_USER_GPU_DMABUF_UPDATE, 0, NULL)) {
  106. return G_SOURCE_CONTINUE;
  107. }
  108. /* resume */
  109. g->wait_ok = 0;
  110. vg_handle_ctrl(&g->dev.parent, 0);
  111. return G_SOURCE_REMOVE;
  112. }
  113. void
  114. vg_wait_ok(VuGpu *g)
  115. {
  116. assert(g->wait_ok == 0);
  117. g->wait_ok = g_unix_fd_add(g->sock_fd, G_IO_IN | G_IO_HUP,
  118. source_wait_cb, g);
  119. }
  120. static int
  121. vg_sock_fd_write(int sock, const void *buf, ssize_t buflen, int fd)
  122. {
  123. ssize_t ret;
  124. struct iovec iov = {
  125. .iov_base = (void *)buf,
  126. .iov_len = buflen,
  127. };
  128. struct msghdr msg = {
  129. .msg_iov = &iov,
  130. .msg_iovlen = 1,
  131. };
  132. union {
  133. struct cmsghdr cmsghdr;
  134. char control[CMSG_SPACE(sizeof(int))];
  135. } cmsgu;
  136. struct cmsghdr *cmsg;
  137. if (fd != -1) {
  138. msg.msg_control = cmsgu.control;
  139. msg.msg_controllen = sizeof(cmsgu.control);
  140. cmsg = CMSG_FIRSTHDR(&msg);
  141. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  142. cmsg->cmsg_level = SOL_SOCKET;
  143. cmsg->cmsg_type = SCM_RIGHTS;
  144. *((int *)CMSG_DATA(cmsg)) = fd;
  145. }
  146. do {
  147. ret = sendmsg(sock, &msg, 0);
  148. } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  149. g_warn_if_fail(ret == buflen);
  150. return ret;
  151. }
  152. void
  153. vg_send_msg(VuGpu *vg, const VhostUserGpuMsg *msg, int fd)
  154. {
  155. if (vg_sock_fd_write(vg->sock_fd, msg,
  156. VHOST_USER_GPU_HDR_SIZE + msg->size, fd) < 0) {
  157. vg_sock_fd_close(vg);
  158. }
  159. }
  160. bool
  161. vg_recv_msg(VuGpu *g, uint32_t expect_req, uint32_t expect_size,
  162. gpointer payload)
  163. {
  164. uint32_t req, flags, size;
  165. if (vg_sock_fd_read(g->sock_fd, &req, sizeof(req)) < 0 ||
  166. vg_sock_fd_read(g->sock_fd, &flags, sizeof(flags)) < 0 ||
  167. vg_sock_fd_read(g->sock_fd, &size, sizeof(size)) < 0) {
  168. goto err;
  169. }
  170. g_return_val_if_fail(req == expect_req, false);
  171. g_return_val_if_fail(flags & VHOST_USER_GPU_MSG_FLAG_REPLY, false);
  172. g_return_val_if_fail(size == expect_size, false);
  173. if (size && vg_sock_fd_read(g->sock_fd, payload, size) != size) {
  174. goto err;
  175. }
  176. return true;
  177. err:
  178. vg_sock_fd_close(g);
  179. return false;
  180. }
  181. static struct virtio_gpu_simple_resource *
  182. virtio_gpu_find_resource(VuGpu *g, uint32_t resource_id)
  183. {
  184. struct virtio_gpu_simple_resource *res;
  185. QTAILQ_FOREACH(res, &g->reslist, next) {
  186. if (res->resource_id == resource_id) {
  187. return res;
  188. }
  189. }
  190. return NULL;
  191. }
  192. void
  193. vg_ctrl_response(VuGpu *g,
  194. struct virtio_gpu_ctrl_command *cmd,
  195. struct virtio_gpu_ctrl_hdr *resp,
  196. size_t resp_len)
  197. {
  198. size_t s;
  199. if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
  200. resp->flags |= VIRTIO_GPU_FLAG_FENCE;
  201. resp->fence_id = cmd->cmd_hdr.fence_id;
  202. resp->ctx_id = cmd->cmd_hdr.ctx_id;
  203. }
  204. virtio_gpu_ctrl_hdr_bswap(resp);
  205. s = iov_from_buf(cmd->elem.in_sg, cmd->elem.in_num, 0, resp, resp_len);
  206. if (s != resp_len) {
  207. g_critical("%s: response size incorrect %zu vs %zu",
  208. __func__, s, resp_len);
  209. }
  210. vu_queue_push(&g->dev.parent, cmd->vq, &cmd->elem, s);
  211. vu_queue_notify(&g->dev.parent, cmd->vq);
  212. cmd->finished = true;
  213. }
  214. void
  215. vg_ctrl_response_nodata(VuGpu *g,
  216. struct virtio_gpu_ctrl_command *cmd,
  217. enum virtio_gpu_ctrl_type type)
  218. {
  219. struct virtio_gpu_ctrl_hdr resp = {
  220. .type = type,
  221. };
  222. vg_ctrl_response(g, cmd, &resp, sizeof(resp));
  223. }
  224. void
  225. vg_get_display_info(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
  226. {
  227. struct virtio_gpu_resp_display_info dpy_info = { {} };
  228. VhostUserGpuMsg msg = {
  229. .request = VHOST_USER_GPU_GET_DISPLAY_INFO,
  230. .size = 0,
  231. };
  232. assert(vg->wait_ok == 0);
  233. vg_send_msg(vg, &msg, -1);
  234. if (!vg_recv_msg(vg, msg.request, sizeof(dpy_info), &dpy_info)) {
  235. return;
  236. }
  237. vg_ctrl_response(vg, cmd, &dpy_info.hdr, sizeof(dpy_info));
  238. }
  239. static void
  240. vg_resource_create_2d(VuGpu *g,
  241. struct virtio_gpu_ctrl_command *cmd)
  242. {
  243. pixman_format_code_t pformat;
  244. struct virtio_gpu_simple_resource *res;
  245. struct virtio_gpu_resource_create_2d c2d;
  246. VUGPU_FILL_CMD(c2d);
  247. virtio_gpu_bswap_32(&c2d, sizeof(c2d));
  248. if (c2d.resource_id == 0) {
  249. g_critical("%s: resource id 0 is not allowed", __func__);
  250. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  251. return;
  252. }
  253. res = virtio_gpu_find_resource(g, c2d.resource_id);
  254. if (res) {
  255. g_critical("%s: resource already exists %d", __func__, c2d.resource_id);
  256. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  257. return;
  258. }
  259. res = g_new0(struct virtio_gpu_simple_resource, 1);
  260. res->width = c2d.width;
  261. res->height = c2d.height;
  262. res->format = c2d.format;
  263. res->resource_id = c2d.resource_id;
  264. pformat = virtio_gpu_get_pixman_format(c2d.format);
  265. if (!pformat) {
  266. g_critical("%s: host couldn't handle guest format %d",
  267. __func__, c2d.format);
  268. g_free(res);
  269. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
  270. return;
  271. }
  272. vugbm_buffer_create(&res->buffer, &g->gdev, c2d.width, c2d.height);
  273. res->image = pixman_image_create_bits(pformat,
  274. c2d.width,
  275. c2d.height,
  276. (uint32_t *)res->buffer.mmap,
  277. res->buffer.stride);
  278. if (!res->image) {
  279. g_critical("%s: resource creation failed %d %d %d",
  280. __func__, c2d.resource_id, c2d.width, c2d.height);
  281. g_free(res);
  282. cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
  283. return;
  284. }
  285. QTAILQ_INSERT_HEAD(&g->reslist, res, next);
  286. }
  287. static void
  288. vg_disable_scanout(VuGpu *g, int scanout_id)
  289. {
  290. struct virtio_gpu_scanout *scanout = &g->scanout[scanout_id];
  291. struct virtio_gpu_simple_resource *res;
  292. if (scanout->resource_id == 0) {
  293. return;
  294. }
  295. res = virtio_gpu_find_resource(g, scanout->resource_id);
  296. if (res) {
  297. res->scanout_bitmask &= ~(1 << scanout_id);
  298. }
  299. scanout->width = 0;
  300. scanout->height = 0;
  301. if (g->sock_fd >= 0) {
  302. VhostUserGpuMsg msg = {
  303. .request = VHOST_USER_GPU_SCANOUT,
  304. .size = sizeof(VhostUserGpuScanout),
  305. .payload.scanout.scanout_id = scanout_id,
  306. };
  307. vg_send_msg(g, &msg, -1);
  308. }
  309. }
  310. static void
  311. vg_resource_destroy(VuGpu *g,
  312. struct virtio_gpu_simple_resource *res)
  313. {
  314. int i;
  315. if (res->scanout_bitmask) {
  316. for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
  317. if (res->scanout_bitmask & (1 << i)) {
  318. vg_disable_scanout(g, i);
  319. }
  320. }
  321. }
  322. vugbm_buffer_destroy(&res->buffer);
  323. pixman_image_unref(res->image);
  324. QTAILQ_REMOVE(&g->reslist, res, next);
  325. g_free(res);
  326. }
  327. static void
  328. vg_resource_unref(VuGpu *g,
  329. struct virtio_gpu_ctrl_command *cmd)
  330. {
  331. struct virtio_gpu_simple_resource *res;
  332. struct virtio_gpu_resource_unref unref;
  333. VUGPU_FILL_CMD(unref);
  334. virtio_gpu_bswap_32(&unref, sizeof(unref));
  335. res = virtio_gpu_find_resource(g, unref.resource_id);
  336. if (!res) {
  337. g_critical("%s: illegal resource specified %d",
  338. __func__, unref.resource_id);
  339. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  340. return;
  341. }
  342. vg_resource_destroy(g, res);
  343. }
  344. int
  345. vg_create_mapping_iov(VuGpu *g,
  346. struct virtio_gpu_resource_attach_backing *ab,
  347. struct virtio_gpu_ctrl_command *cmd,
  348. struct iovec **iov)
  349. {
  350. struct virtio_gpu_mem_entry *ents;
  351. size_t esize, s;
  352. int i;
  353. if (ab->nr_entries > 16384) {
  354. g_critical("%s: nr_entries is too big (%d > 16384)",
  355. __func__, ab->nr_entries);
  356. return -1;
  357. }
  358. esize = sizeof(*ents) * ab->nr_entries;
  359. ents = g_malloc(esize);
  360. s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
  361. sizeof(*ab), ents, esize);
  362. if (s != esize) {
  363. g_critical("%s: command data size incorrect %zu vs %zu",
  364. __func__, s, esize);
  365. g_free(ents);
  366. return -1;
  367. }
  368. *iov = g_malloc0(sizeof(struct iovec) * ab->nr_entries);
  369. for (i = 0; i < ab->nr_entries; i++) {
  370. uint64_t len = ents[i].length;
  371. (*iov)[i].iov_len = ents[i].length;
  372. (*iov)[i].iov_base = vu_gpa_to_va(&g->dev.parent, &len, ents[i].addr);
  373. if (!(*iov)[i].iov_base || len != ents[i].length) {
  374. g_critical("%s: resource %d element %d",
  375. __func__, ab->resource_id, i);
  376. g_free(*iov);
  377. g_free(ents);
  378. *iov = NULL;
  379. return -1;
  380. }
  381. }
  382. g_free(ents);
  383. return 0;
  384. }
  385. static void
  386. vg_resource_attach_backing(VuGpu *g,
  387. struct virtio_gpu_ctrl_command *cmd)
  388. {
  389. struct virtio_gpu_simple_resource *res;
  390. struct virtio_gpu_resource_attach_backing ab;
  391. int ret;
  392. VUGPU_FILL_CMD(ab);
  393. virtio_gpu_bswap_32(&ab, sizeof(ab));
  394. res = virtio_gpu_find_resource(g, ab.resource_id);
  395. if (!res) {
  396. g_critical("%s: illegal resource specified %d",
  397. __func__, ab.resource_id);
  398. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  399. return;
  400. }
  401. ret = vg_create_mapping_iov(g, &ab, cmd, &res->iov);
  402. if (ret != 0) {
  403. cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
  404. return;
  405. }
  406. res->iov_cnt = ab.nr_entries;
  407. }
  408. static void
  409. vg_resource_detach_backing(VuGpu *g,
  410. struct virtio_gpu_ctrl_command *cmd)
  411. {
  412. struct virtio_gpu_simple_resource *res;
  413. struct virtio_gpu_resource_detach_backing detach;
  414. VUGPU_FILL_CMD(detach);
  415. virtio_gpu_bswap_32(&detach, sizeof(detach));
  416. res = virtio_gpu_find_resource(g, detach.resource_id);
  417. if (!res || !res->iov) {
  418. g_critical("%s: illegal resource specified %d",
  419. __func__, detach.resource_id);
  420. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  421. return;
  422. }
  423. g_free(res->iov);
  424. res->iov = NULL;
  425. res->iov_cnt = 0;
  426. }
  427. static void
  428. vg_transfer_to_host_2d(VuGpu *g,
  429. struct virtio_gpu_ctrl_command *cmd)
  430. {
  431. struct virtio_gpu_simple_resource *res;
  432. int h;
  433. uint32_t src_offset, dst_offset, stride;
  434. int bpp;
  435. pixman_format_code_t format;
  436. struct virtio_gpu_transfer_to_host_2d t2d;
  437. VUGPU_FILL_CMD(t2d);
  438. virtio_gpu_t2d_bswap(&t2d);
  439. res = virtio_gpu_find_resource(g, t2d.resource_id);
  440. if (!res || !res->iov) {
  441. g_critical("%s: illegal resource specified %d",
  442. __func__, t2d.resource_id);
  443. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  444. return;
  445. }
  446. if (t2d.r.x > res->width ||
  447. t2d.r.y > res->height ||
  448. t2d.r.width > res->width ||
  449. t2d.r.height > res->height ||
  450. t2d.r.x + t2d.r.width > res->width ||
  451. t2d.r.y + t2d.r.height > res->height) {
  452. g_critical("%s: transfer bounds outside resource"
  453. " bounds for resource %d: %d %d %d %d vs %d %d",
  454. __func__, t2d.resource_id, t2d.r.x, t2d.r.y,
  455. t2d.r.width, t2d.r.height, res->width, res->height);
  456. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
  457. return;
  458. }
  459. format = pixman_image_get_format(res->image);
  460. bpp = (PIXMAN_FORMAT_BPP(format) + 7) / 8;
  461. stride = pixman_image_get_stride(res->image);
  462. if (t2d.offset || t2d.r.x || t2d.r.y ||
  463. t2d.r.width != pixman_image_get_width(res->image)) {
  464. void *img_data = pixman_image_get_data(res->image);
  465. for (h = 0; h < t2d.r.height; h++) {
  466. src_offset = t2d.offset + stride * h;
  467. dst_offset = (t2d.r.y + h) * stride + (t2d.r.x * bpp);
  468. iov_to_buf(res->iov, res->iov_cnt, src_offset,
  469. img_data
  470. + dst_offset, t2d.r.width * bpp);
  471. }
  472. } else {
  473. iov_to_buf(res->iov, res->iov_cnt, 0,
  474. pixman_image_get_data(res->image),
  475. pixman_image_get_stride(res->image)
  476. * pixman_image_get_height(res->image));
  477. }
  478. }
  479. static void
  480. vg_set_scanout(VuGpu *g,
  481. struct virtio_gpu_ctrl_command *cmd)
  482. {
  483. struct virtio_gpu_simple_resource *res, *ores;
  484. struct virtio_gpu_scanout *scanout;
  485. struct virtio_gpu_set_scanout ss;
  486. int fd;
  487. VUGPU_FILL_CMD(ss);
  488. virtio_gpu_bswap_32(&ss, sizeof(ss));
  489. if (ss.scanout_id >= VIRTIO_GPU_MAX_SCANOUTS) {
  490. g_critical("%s: illegal scanout id specified %d",
  491. __func__, ss.scanout_id);
  492. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
  493. return;
  494. }
  495. if (ss.resource_id == 0) {
  496. vg_disable_scanout(g, ss.scanout_id);
  497. return;
  498. }
  499. /* create a surface for this scanout */
  500. res = virtio_gpu_find_resource(g, ss.resource_id);
  501. if (!res) {
  502. g_critical("%s: illegal resource specified %d",
  503. __func__, ss.resource_id);
  504. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  505. return;
  506. }
  507. if (ss.r.x > res->width ||
  508. ss.r.y > res->height ||
  509. ss.r.width > res->width ||
  510. ss.r.height > res->height ||
  511. ss.r.x + ss.r.width > res->width ||
  512. ss.r.y + ss.r.height > res->height) {
  513. g_critical("%s: illegal scanout %d bounds for"
  514. " resource %d, (%d,%d)+%d,%d vs %d %d",
  515. __func__, ss.scanout_id, ss.resource_id, ss.r.x, ss.r.y,
  516. ss.r.width, ss.r.height, res->width, res->height);
  517. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
  518. return;
  519. }
  520. scanout = &g->scanout[ss.scanout_id];
  521. ores = virtio_gpu_find_resource(g, scanout->resource_id);
  522. if (ores) {
  523. ores->scanout_bitmask &= ~(1 << ss.scanout_id);
  524. }
  525. res->scanout_bitmask |= (1 << ss.scanout_id);
  526. scanout->resource_id = ss.resource_id;
  527. scanout->x = ss.r.x;
  528. scanout->y = ss.r.y;
  529. scanout->width = ss.r.width;
  530. scanout->height = ss.r.height;
  531. struct vugbm_buffer *buffer = &res->buffer;
  532. if (vugbm_buffer_can_get_dmabuf_fd(buffer)) {
  533. VhostUserGpuMsg msg = {
  534. .request = VHOST_USER_GPU_DMABUF_SCANOUT,
  535. .size = sizeof(VhostUserGpuDMABUFScanout),
  536. .payload.dmabuf_scanout = (VhostUserGpuDMABUFScanout) {
  537. .scanout_id = ss.scanout_id,
  538. .x = ss.r.x,
  539. .y = ss.r.y,
  540. .width = ss.r.width,
  541. .height = ss.r.height,
  542. .fd_width = buffer->width,
  543. .fd_height = buffer->height,
  544. .fd_stride = buffer->stride,
  545. .fd_drm_fourcc = buffer->format
  546. }
  547. };
  548. if (vugbm_buffer_get_dmabuf_fd(buffer, &fd)) {
  549. vg_send_msg(g, &msg, fd);
  550. close(fd);
  551. }
  552. } else {
  553. VhostUserGpuMsg msg = {
  554. .request = VHOST_USER_GPU_SCANOUT,
  555. .size = sizeof(VhostUserGpuScanout),
  556. .payload.scanout = (VhostUserGpuScanout) {
  557. .scanout_id = ss.scanout_id,
  558. .width = scanout->width,
  559. .height = scanout->height
  560. }
  561. };
  562. vg_send_msg(g, &msg, -1);
  563. }
  564. }
  565. static void
  566. vg_resource_flush(VuGpu *g,
  567. struct virtio_gpu_ctrl_command *cmd)
  568. {
  569. struct virtio_gpu_simple_resource *res;
  570. struct virtio_gpu_resource_flush rf;
  571. pixman_region16_t flush_region;
  572. int i;
  573. VUGPU_FILL_CMD(rf);
  574. virtio_gpu_bswap_32(&rf, sizeof(rf));
  575. res = virtio_gpu_find_resource(g, rf.resource_id);
  576. if (!res) {
  577. g_critical("%s: illegal resource specified %d\n",
  578. __func__, rf.resource_id);
  579. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
  580. return;
  581. }
  582. if (rf.r.x > res->width ||
  583. rf.r.y > res->height ||
  584. rf.r.width > res->width ||
  585. rf.r.height > res->height ||
  586. rf.r.x + rf.r.width > res->width ||
  587. rf.r.y + rf.r.height > res->height) {
  588. g_critical("%s: flush bounds outside resource"
  589. " bounds for resource %d: %d %d %d %d vs %d %d\n",
  590. __func__, rf.resource_id, rf.r.x, rf.r.y,
  591. rf.r.width, rf.r.height, res->width, res->height);
  592. cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
  593. return;
  594. }
  595. pixman_region_init_rect(&flush_region,
  596. rf.r.x, rf.r.y, rf.r.width, rf.r.height);
  597. for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
  598. struct virtio_gpu_scanout *scanout;
  599. pixman_region16_t region, finalregion;
  600. pixman_box16_t *extents;
  601. if (!(res->scanout_bitmask & (1 << i))) {
  602. continue;
  603. }
  604. scanout = &g->scanout[i];
  605. pixman_region_init(&finalregion);
  606. pixman_region_init_rect(&region, scanout->x, scanout->y,
  607. scanout->width, scanout->height);
  608. pixman_region_intersect(&finalregion, &flush_region, &region);
  609. extents = pixman_region_extents(&finalregion);
  610. size_t width = extents->x2 - extents->x1;
  611. size_t height = extents->y2 - extents->y1;
  612. if (vugbm_buffer_can_get_dmabuf_fd(&res->buffer)) {
  613. VhostUserGpuMsg vmsg = {
  614. .request = VHOST_USER_GPU_DMABUF_UPDATE,
  615. .size = sizeof(VhostUserGpuUpdate),
  616. .payload.update = (VhostUserGpuUpdate) {
  617. .scanout_id = i,
  618. .x = extents->x1,
  619. .y = extents->y1,
  620. .width = width,
  621. .height = height,
  622. }
  623. };
  624. vg_send_msg(g, &vmsg, -1);
  625. vg_wait_ok(g);
  626. } else {
  627. size_t bpp =
  628. PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) / 8;
  629. size_t size = width * height * bpp;
  630. void *p = g_malloc(VHOST_USER_GPU_HDR_SIZE +
  631. sizeof(VhostUserGpuUpdate) + size);
  632. VhostUserGpuMsg *msg = p;
  633. msg->request = VHOST_USER_GPU_UPDATE;
  634. msg->size = sizeof(VhostUserGpuUpdate) + size;
  635. msg->payload.update = (VhostUserGpuUpdate) {
  636. .scanout_id = i,
  637. .x = extents->x1,
  638. .y = extents->y1,
  639. .width = width,
  640. .height = height,
  641. };
  642. pixman_image_t *i =
  643. pixman_image_create_bits(pixman_image_get_format(res->image),
  644. msg->payload.update.width,
  645. msg->payload.update.height,
  646. p + offsetof(VhostUserGpuMsg,
  647. payload.update.data),
  648. width * bpp);
  649. pixman_image_composite(PIXMAN_OP_SRC,
  650. res->image, NULL, i,
  651. extents->x1, extents->y1,
  652. 0, 0, 0, 0,
  653. width, height);
  654. pixman_image_unref(i);
  655. vg_send_msg(g, msg, -1);
  656. g_free(msg);
  657. }
  658. pixman_region_fini(&region);
  659. pixman_region_fini(&finalregion);
  660. }
  661. pixman_region_fini(&flush_region);
  662. }
  663. static void
  664. vg_process_cmd(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
  665. {
  666. switch (cmd->cmd_hdr.type) {
  667. case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
  668. vg_get_display_info(vg, cmd);
  669. break;
  670. case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
  671. vg_resource_create_2d(vg, cmd);
  672. break;
  673. case VIRTIO_GPU_CMD_RESOURCE_UNREF:
  674. vg_resource_unref(vg, cmd);
  675. break;
  676. case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
  677. vg_resource_flush(vg, cmd);
  678. break;
  679. case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
  680. vg_transfer_to_host_2d(vg, cmd);
  681. break;
  682. case VIRTIO_GPU_CMD_SET_SCANOUT:
  683. vg_set_scanout(vg, cmd);
  684. break;
  685. case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
  686. vg_resource_attach_backing(vg, cmd);
  687. break;
  688. case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
  689. vg_resource_detach_backing(vg, cmd);
  690. break;
  691. /* case VIRTIO_GPU_CMD_GET_EDID: */
  692. /* break */
  693. default:
  694. g_warning("TODO handle ctrl %x\n", cmd->cmd_hdr.type);
  695. cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
  696. break;
  697. }
  698. if (!cmd->finished) {
  699. vg_ctrl_response_nodata(vg, cmd, cmd->error ? cmd->error :
  700. VIRTIO_GPU_RESP_OK_NODATA);
  701. }
  702. }
  703. static void
  704. vg_handle_ctrl(VuDev *dev, int qidx)
  705. {
  706. VuGpu *vg = container_of(dev, VuGpu, dev.parent);
  707. VuVirtq *vq = vu_get_queue(dev, qidx);
  708. struct virtio_gpu_ctrl_command *cmd = NULL;
  709. size_t len;
  710. for (;;) {
  711. if (vg->wait_ok != 0) {
  712. return;
  713. }
  714. cmd = vu_queue_pop(dev, vq, sizeof(struct virtio_gpu_ctrl_command));
  715. if (!cmd) {
  716. break;
  717. }
  718. cmd->vq = vq;
  719. cmd->error = 0;
  720. cmd->finished = false;
  721. len = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
  722. 0, &cmd->cmd_hdr, sizeof(cmd->cmd_hdr));
  723. if (len != sizeof(cmd->cmd_hdr)) {
  724. g_warning("%s: command size incorrect %zu vs %zu\n",
  725. __func__, len, sizeof(cmd->cmd_hdr));
  726. }
  727. virtio_gpu_ctrl_hdr_bswap(&cmd->cmd_hdr);
  728. g_debug("%d %s\n", cmd->cmd_hdr.type,
  729. vg_cmd_to_string(cmd->cmd_hdr.type));
  730. if (vg->virgl) {
  731. vg_virgl_process_cmd(vg, cmd);
  732. } else {
  733. vg_process_cmd(vg, cmd);
  734. }
  735. if (!cmd->finished) {
  736. QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
  737. vg->inflight++;
  738. } else {
  739. g_free(cmd);
  740. }
  741. }
  742. }
  743. static void
  744. update_cursor_data_simple(VuGpu *g, uint32_t resource_id, gpointer data)
  745. {
  746. struct virtio_gpu_simple_resource *res;
  747. res = virtio_gpu_find_resource(g, resource_id);
  748. g_return_if_fail(res != NULL);
  749. g_return_if_fail(pixman_image_get_width(res->image) == 64);
  750. g_return_if_fail(pixman_image_get_height(res->image) == 64);
  751. g_return_if_fail(
  752. PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) == 32);
  753. memcpy(data, pixman_image_get_data(res->image), 64 * 64 * sizeof(uint32_t));
  754. }
  755. static void
  756. vg_process_cursor_cmd(VuGpu *g, struct virtio_gpu_update_cursor *cursor)
  757. {
  758. bool move = cursor->hdr.type != VIRTIO_GPU_CMD_MOVE_CURSOR;
  759. g_debug("%s move:%d\n", G_STRFUNC, move);
  760. if (move) {
  761. VhostUserGpuMsg msg = {
  762. .request = cursor->resource_id ?
  763. VHOST_USER_GPU_CURSOR_POS : VHOST_USER_GPU_CURSOR_POS_HIDE,
  764. .size = sizeof(VhostUserGpuCursorPos),
  765. .payload.cursor_pos = {
  766. .scanout_id = cursor->pos.scanout_id,
  767. .x = cursor->pos.x,
  768. .y = cursor->pos.y,
  769. }
  770. };
  771. vg_send_msg(g, &msg, -1);
  772. } else {
  773. VhostUserGpuMsg msg = {
  774. .request = VHOST_USER_GPU_CURSOR_UPDATE,
  775. .size = sizeof(VhostUserGpuCursorUpdate),
  776. .payload.cursor_update = {
  777. .pos = {
  778. .scanout_id = cursor->pos.scanout_id,
  779. .x = cursor->pos.x,
  780. .y = cursor->pos.y,
  781. },
  782. .hot_x = cursor->hot_x,
  783. .hot_y = cursor->hot_y,
  784. }
  785. };
  786. if (g->virgl) {
  787. vg_virgl_update_cursor_data(g, cursor->resource_id,
  788. msg.payload.cursor_update.data);
  789. } else {
  790. update_cursor_data_simple(g, cursor->resource_id,
  791. msg.payload.cursor_update.data);
  792. }
  793. vg_send_msg(g, &msg, -1);
  794. }
  795. }
  796. static void
  797. vg_handle_cursor(VuDev *dev, int qidx)
  798. {
  799. VuGpu *g = container_of(dev, VuGpu, dev.parent);
  800. VuVirtq *vq = vu_get_queue(dev, qidx);
  801. VuVirtqElement *elem;
  802. size_t len;
  803. struct virtio_gpu_update_cursor cursor;
  804. for (;;) {
  805. elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
  806. if (!elem) {
  807. break;
  808. }
  809. g_debug("cursor out:%d in:%d\n", elem->out_num, elem->in_num);
  810. len = iov_to_buf(elem->out_sg, elem->out_num,
  811. 0, &cursor, sizeof(cursor));
  812. if (len != sizeof(cursor)) {
  813. g_warning("%s: cursor size incorrect %zu vs %zu\n",
  814. __func__, len, sizeof(cursor));
  815. } else {
  816. virtio_gpu_bswap_32(&cursor, sizeof(cursor));
  817. vg_process_cursor_cmd(g, &cursor);
  818. }
  819. vu_queue_push(dev, vq, elem, 0);
  820. vu_queue_notify(dev, vq);
  821. g_free(elem);
  822. }
  823. }
  824. static void
  825. vg_panic(VuDev *dev, const char *msg)
  826. {
  827. g_critical("%s\n", msg);
  828. exit(1);
  829. }
  830. static void
  831. vg_queue_set_started(VuDev *dev, int qidx, bool started)
  832. {
  833. VuVirtq *vq = vu_get_queue(dev, qidx);
  834. g_debug("queue started %d:%d\n", qidx, started);
  835. switch (qidx) {
  836. case 0:
  837. vu_set_queue_handler(dev, vq, started ? vg_handle_ctrl : NULL);
  838. break;
  839. case 1:
  840. vu_set_queue_handler(dev, vq, started ? vg_handle_cursor : NULL);
  841. break;
  842. default:
  843. break;
  844. }
  845. }
  846. static void
  847. set_gpu_protocol_features(VuGpu *g)
  848. {
  849. uint64_t u64;
  850. VhostUserGpuMsg msg = {
  851. .request = VHOST_USER_GPU_GET_PROTOCOL_FEATURES
  852. };
  853. assert(g->wait_ok == 0);
  854. vg_send_msg(g, &msg, -1);
  855. if (!vg_recv_msg(g, msg.request, sizeof(u64), &u64)) {
  856. return;
  857. }
  858. msg = (VhostUserGpuMsg) {
  859. .request = VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
  860. .size = sizeof(uint64_t),
  861. .payload.u64 = 0
  862. };
  863. vg_send_msg(g, &msg, -1);
  864. }
  865. static int
  866. vg_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply)
  867. {
  868. VuGpu *g = container_of(dev, VuGpu, dev.parent);
  869. switch (msg->request) {
  870. case VHOST_USER_GPU_SET_SOCKET: {
  871. g_return_val_if_fail(msg->fd_num == 1, 1);
  872. g_return_val_if_fail(g->sock_fd == -1, 1);
  873. g->sock_fd = msg->fds[0];
  874. set_gpu_protocol_features(g);
  875. return 1;
  876. }
  877. default:
  878. return 0;
  879. }
  880. return 0;
  881. }
  882. static uint64_t
  883. vg_get_features(VuDev *dev)
  884. {
  885. uint64_t features = 0;
  886. if (opt_virgl) {
  887. features |= 1 << VIRTIO_GPU_F_VIRGL;
  888. }
  889. return features;
  890. }
  891. static void
  892. vg_set_features(VuDev *dev, uint64_t features)
  893. {
  894. VuGpu *g = container_of(dev, VuGpu, dev.parent);
  895. bool virgl = features & (1 << VIRTIO_GPU_F_VIRGL);
  896. if (virgl && !g->virgl_inited) {
  897. if (!vg_virgl_init(g)) {
  898. vg_panic(dev, "Failed to initialize virgl");
  899. }
  900. g->virgl_inited = true;
  901. }
  902. g->virgl = virgl;
  903. }
  904. static int
  905. vg_get_config(VuDev *dev, uint8_t *config, uint32_t len)
  906. {
  907. VuGpu *g = container_of(dev, VuGpu, dev.parent);
  908. g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
  909. if (opt_virgl) {
  910. g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
  911. }
  912. memcpy(config, &g->virtio_config, len);
  913. return 0;
  914. }
  915. static int
  916. vg_set_config(VuDev *dev, const uint8_t *data,
  917. uint32_t offset, uint32_t size,
  918. uint32_t flags)
  919. {
  920. VuGpu *g = container_of(dev, VuGpu, dev.parent);
  921. struct virtio_gpu_config *config = (struct virtio_gpu_config *)data;
  922. if (config->events_clear) {
  923. g->virtio_config.events_read &= ~config->events_clear;
  924. }
  925. return 0;
  926. }
  927. static const VuDevIface vuiface = {
  928. .set_features = vg_set_features,
  929. .get_features = vg_get_features,
  930. .queue_set_started = vg_queue_set_started,
  931. .process_msg = vg_process_msg,
  932. .get_config = vg_get_config,
  933. .set_config = vg_set_config,
  934. };
  935. static void
  936. vg_destroy(VuGpu *g)
  937. {
  938. struct virtio_gpu_simple_resource *res, *tmp;
  939. vug_deinit(&g->dev);
  940. vg_sock_fd_close(g);
  941. QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) {
  942. vg_resource_destroy(g, res);
  943. }
  944. vugbm_device_destroy(&g->gdev);
  945. }
  946. static GOptionEntry entries[] = {
  947. { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps,
  948. "Print capabilities", NULL },
  949. { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum,
  950. "Use inherited fd socket", "FDNUM" },
  951. { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path,
  952. "Use UNIX socket path", "PATH" },
  953. { "render-node", 'r', 0, G_OPTION_ARG_FILENAME, &opt_render_node,
  954. "Specify DRM render node", "PATH" },
  955. { "virgl", 'v', 0, G_OPTION_ARG_NONE, &opt_virgl,
  956. "Turn virgl rendering on", NULL },
  957. { NULL, }
  958. };
  959. int
  960. main(int argc, char *argv[])
  961. {
  962. GOptionContext *context;
  963. GError *error = NULL;
  964. GMainLoop *loop = NULL;
  965. int fd;
  966. VuGpu g = { .sock_fd = -1, .drm_rnode_fd = -1 };
  967. QTAILQ_INIT(&g.reslist);
  968. QTAILQ_INIT(&g.fenceq);
  969. context = g_option_context_new("QEMU vhost-user-gpu");
  970. g_option_context_add_main_entries(context, entries, NULL);
  971. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  972. g_printerr("Option parsing failed: %s\n", error->message);
  973. exit(EXIT_FAILURE);
  974. }
  975. g_option_context_free(context);
  976. if (opt_print_caps) {
  977. g_print("{\n");
  978. g_print(" \"type\": \"gpu\",\n");
  979. g_print(" \"features\": [\n");
  980. g_print(" \"render-node\",\n");
  981. g_print(" \"virgl\"\n");
  982. g_print(" ]\n");
  983. g_print("}\n");
  984. exit(EXIT_SUCCESS);
  985. }
  986. g.drm_rnode_fd = qemu_drm_rendernode_open(opt_render_node);
  987. if (opt_render_node && g.drm_rnode_fd == -1) {
  988. g_printerr("Failed to open DRM rendernode.\n");
  989. exit(EXIT_FAILURE);
  990. }
  991. if (g.drm_rnode_fd >= 0) {
  992. if (!vugbm_device_init(&g.gdev, g.drm_rnode_fd)) {
  993. g_warning("Failed to init DRM device, using fallback path");
  994. }
  995. }
  996. if ((!!opt_socket_path + (opt_fdnum != -1)) != 1) {
  997. g_printerr("Please specify either --fd or --socket-path\n");
  998. exit(EXIT_FAILURE);
  999. }
  1000. if (opt_socket_path) {
  1001. int lsock = unix_listen(opt_socket_path, &error_fatal);
  1002. if (lsock < 0) {
  1003. g_printerr("Failed to listen on %s.\n", opt_socket_path);
  1004. exit(EXIT_FAILURE);
  1005. }
  1006. fd = accept(lsock, NULL, NULL);
  1007. close(lsock);
  1008. } else {
  1009. fd = opt_fdnum;
  1010. }
  1011. if (fd == -1) {
  1012. g_printerr("Invalid vhost-user socket.\n");
  1013. exit(EXIT_FAILURE);
  1014. }
  1015. if (!vug_init(&g.dev, VHOST_USER_GPU_MAX_QUEUES, fd, vg_panic, &vuiface)) {
  1016. g_printerr("Failed to initialize libvhost-user-glib.\n");
  1017. exit(EXIT_FAILURE);
  1018. }
  1019. loop = g_main_loop_new(NULL, FALSE);
  1020. g_main_loop_run(loop);
  1021. g_main_loop_unref(loop);
  1022. vg_destroy(&g);
  1023. if (g.drm_rnode_fd >= 0) {
  1024. close(g.drm_rnode_fd);
  1025. }
  1026. return 0;
  1027. }