virtgpu_vq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie <airlied@redhat.com>
  7. * Gerd Hoffmann <kraxel@redhat.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <drm/drmP.h>
  29. #include "virtgpu_drv.h"
  30. #include <linux/virtio.h>
  31. #include <linux/virtio_config.h>
  32. #include <linux/virtio_ring.h>
  33. #define MAX_INLINE_CMD_SIZE 96
  34. #define MAX_INLINE_RESP_SIZE 24
  35. #define VBUFFER_SIZE (sizeof(struct virtio_gpu_vbuffer) \
  36. + MAX_INLINE_CMD_SIZE \
  37. + MAX_INLINE_RESP_SIZE)
  38. void virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
  39. uint32_t *resid)
  40. {
  41. int handle;
  42. idr_preload(GFP_KERNEL);
  43. spin_lock(&vgdev->resource_idr_lock);
  44. handle = idr_alloc(&vgdev->resource_idr, NULL, 1, 0, GFP_NOWAIT);
  45. spin_unlock(&vgdev->resource_idr_lock);
  46. idr_preload_end();
  47. *resid = handle;
  48. }
  49. void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
  50. {
  51. spin_lock(&vgdev->resource_idr_lock);
  52. idr_remove(&vgdev->resource_idr, id);
  53. spin_unlock(&vgdev->resource_idr_lock);
  54. }
  55. void virtio_gpu_ctrl_ack(struct virtqueue *vq)
  56. {
  57. struct drm_device *dev = vq->vdev->priv;
  58. struct virtio_gpu_device *vgdev = dev->dev_private;
  59. schedule_work(&vgdev->ctrlq.dequeue_work);
  60. }
  61. void virtio_gpu_cursor_ack(struct virtqueue *vq)
  62. {
  63. struct drm_device *dev = vq->vdev->priv;
  64. struct virtio_gpu_device *vgdev = dev->dev_private;
  65. schedule_work(&vgdev->cursorq.dequeue_work);
  66. }
  67. int virtio_gpu_alloc_vbufs(struct virtio_gpu_device *vgdev)
  68. {
  69. vgdev->vbufs = kmem_cache_create("virtio-gpu-vbufs",
  70. VBUFFER_SIZE,
  71. __alignof__(struct virtio_gpu_vbuffer),
  72. 0, NULL);
  73. if (!vgdev->vbufs)
  74. return -ENOMEM;
  75. return 0;
  76. }
  77. void virtio_gpu_free_vbufs(struct virtio_gpu_device *vgdev)
  78. {
  79. kmem_cache_destroy(vgdev->vbufs);
  80. vgdev->vbufs = NULL;
  81. }
  82. static struct virtio_gpu_vbuffer*
  83. virtio_gpu_get_vbuf(struct virtio_gpu_device *vgdev,
  84. int size, int resp_size, void *resp_buf,
  85. virtio_gpu_resp_cb resp_cb)
  86. {
  87. struct virtio_gpu_vbuffer *vbuf;
  88. vbuf = kmem_cache_alloc(vgdev->vbufs, GFP_KERNEL);
  89. if (!vbuf)
  90. return ERR_PTR(-ENOMEM);
  91. memset(vbuf, 0, VBUFFER_SIZE);
  92. BUG_ON(size > MAX_INLINE_CMD_SIZE);
  93. vbuf->buf = (void *)vbuf + sizeof(*vbuf);
  94. vbuf->size = size;
  95. vbuf->resp_cb = resp_cb;
  96. vbuf->resp_size = resp_size;
  97. if (resp_size <= MAX_INLINE_RESP_SIZE)
  98. vbuf->resp_buf = (void *)vbuf->buf + size;
  99. else
  100. vbuf->resp_buf = resp_buf;
  101. BUG_ON(!vbuf->resp_buf);
  102. return vbuf;
  103. }
  104. static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
  105. struct virtio_gpu_vbuffer **vbuffer_p,
  106. int size)
  107. {
  108. struct virtio_gpu_vbuffer *vbuf;
  109. vbuf = virtio_gpu_get_vbuf(vgdev, size,
  110. sizeof(struct virtio_gpu_ctrl_hdr),
  111. NULL, NULL);
  112. if (IS_ERR(vbuf)) {
  113. *vbuffer_p = NULL;
  114. return ERR_CAST(vbuf);
  115. }
  116. *vbuffer_p = vbuf;
  117. return vbuf->buf;
  118. }
  119. static struct virtio_gpu_update_cursor*
  120. virtio_gpu_alloc_cursor(struct virtio_gpu_device *vgdev,
  121. struct virtio_gpu_vbuffer **vbuffer_p)
  122. {
  123. struct virtio_gpu_vbuffer *vbuf;
  124. vbuf = virtio_gpu_get_vbuf
  125. (vgdev, sizeof(struct virtio_gpu_update_cursor),
  126. 0, NULL, NULL);
  127. if (IS_ERR(vbuf)) {
  128. *vbuffer_p = NULL;
  129. return ERR_CAST(vbuf);
  130. }
  131. *vbuffer_p = vbuf;
  132. return (struct virtio_gpu_update_cursor *)vbuf->buf;
  133. }
  134. static void *virtio_gpu_alloc_cmd_resp(struct virtio_gpu_device *vgdev,
  135. virtio_gpu_resp_cb cb,
  136. struct virtio_gpu_vbuffer **vbuffer_p,
  137. int cmd_size, int resp_size,
  138. void *resp_buf)
  139. {
  140. struct virtio_gpu_vbuffer *vbuf;
  141. vbuf = virtio_gpu_get_vbuf(vgdev, cmd_size,
  142. resp_size, resp_buf, cb);
  143. if (IS_ERR(vbuf)) {
  144. *vbuffer_p = NULL;
  145. return ERR_CAST(vbuf);
  146. }
  147. *vbuffer_p = vbuf;
  148. return (struct virtio_gpu_command *)vbuf->buf;
  149. }
  150. static void free_vbuf(struct virtio_gpu_device *vgdev,
  151. struct virtio_gpu_vbuffer *vbuf)
  152. {
  153. if (vbuf->resp_size > MAX_INLINE_RESP_SIZE)
  154. kfree(vbuf->resp_buf);
  155. kfree(vbuf->data_buf);
  156. kmem_cache_free(vgdev->vbufs, vbuf);
  157. }
  158. static void reclaim_vbufs(struct virtqueue *vq, struct list_head *reclaim_list)
  159. {
  160. struct virtio_gpu_vbuffer *vbuf;
  161. unsigned int len;
  162. int freed = 0;
  163. while ((vbuf = virtqueue_get_buf(vq, &len))) {
  164. list_add_tail(&vbuf->list, reclaim_list);
  165. freed++;
  166. }
  167. if (freed == 0)
  168. DRM_DEBUG("Huh? zero vbufs reclaimed");
  169. }
  170. void virtio_gpu_dequeue_ctrl_func(struct work_struct *work)
  171. {
  172. struct virtio_gpu_device *vgdev =
  173. container_of(work, struct virtio_gpu_device,
  174. ctrlq.dequeue_work);
  175. struct list_head reclaim_list;
  176. struct virtio_gpu_vbuffer *entry, *tmp;
  177. struct virtio_gpu_ctrl_hdr *resp;
  178. u64 fence_id = 0;
  179. INIT_LIST_HEAD(&reclaim_list);
  180. spin_lock(&vgdev->ctrlq.qlock);
  181. do {
  182. virtqueue_disable_cb(vgdev->ctrlq.vq);
  183. reclaim_vbufs(vgdev->ctrlq.vq, &reclaim_list);
  184. } while (!virtqueue_enable_cb(vgdev->ctrlq.vq));
  185. spin_unlock(&vgdev->ctrlq.qlock);
  186. list_for_each_entry_safe(entry, tmp, &reclaim_list, list) {
  187. resp = (struct virtio_gpu_ctrl_hdr *)entry->resp_buf;
  188. if (resp->type != cpu_to_le32(VIRTIO_GPU_RESP_OK_NODATA))
  189. DRM_DEBUG("response 0x%x\n", le32_to_cpu(resp->type));
  190. if (resp->flags & cpu_to_le32(VIRTIO_GPU_FLAG_FENCE)) {
  191. u64 f = le64_to_cpu(resp->fence_id);
  192. if (fence_id > f) {
  193. DRM_ERROR("%s: Oops: fence %llx -> %llx\n",
  194. __func__, fence_id, f);
  195. } else {
  196. fence_id = f;
  197. }
  198. }
  199. if (entry->resp_cb)
  200. entry->resp_cb(vgdev, entry);
  201. list_del(&entry->list);
  202. free_vbuf(vgdev, entry);
  203. }
  204. wake_up(&vgdev->ctrlq.ack_queue);
  205. if (fence_id)
  206. virtio_gpu_fence_event_process(vgdev, fence_id);
  207. }
  208. void virtio_gpu_dequeue_cursor_func(struct work_struct *work)
  209. {
  210. struct virtio_gpu_device *vgdev =
  211. container_of(work, struct virtio_gpu_device,
  212. cursorq.dequeue_work);
  213. struct list_head reclaim_list;
  214. struct virtio_gpu_vbuffer *entry, *tmp;
  215. INIT_LIST_HEAD(&reclaim_list);
  216. spin_lock(&vgdev->cursorq.qlock);
  217. do {
  218. virtqueue_disable_cb(vgdev->cursorq.vq);
  219. reclaim_vbufs(vgdev->cursorq.vq, &reclaim_list);
  220. } while (!virtqueue_enable_cb(vgdev->cursorq.vq));
  221. spin_unlock(&vgdev->cursorq.qlock);
  222. list_for_each_entry_safe(entry, tmp, &reclaim_list, list) {
  223. list_del(&entry->list);
  224. free_vbuf(vgdev, entry);
  225. }
  226. wake_up(&vgdev->cursorq.ack_queue);
  227. }
  228. static int virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev,
  229. struct virtio_gpu_vbuffer *vbuf)
  230. __releases(&vgdev->ctrlq.qlock)
  231. __acquires(&vgdev->ctrlq.qlock)
  232. {
  233. struct virtqueue *vq = vgdev->ctrlq.vq;
  234. struct scatterlist *sgs[3], vcmd, vout, vresp;
  235. int outcnt = 0, incnt = 0;
  236. int ret;
  237. if (!vgdev->vqs_ready)
  238. return -ENODEV;
  239. sg_init_one(&vcmd, vbuf->buf, vbuf->size);
  240. sgs[outcnt + incnt] = &vcmd;
  241. outcnt++;
  242. if (vbuf->data_size) {
  243. sg_init_one(&vout, vbuf->data_buf, vbuf->data_size);
  244. sgs[outcnt + incnt] = &vout;
  245. outcnt++;
  246. }
  247. if (vbuf->resp_size) {
  248. sg_init_one(&vresp, vbuf->resp_buf, vbuf->resp_size);
  249. sgs[outcnt + incnt] = &vresp;
  250. incnt++;
  251. }
  252. retry:
  253. ret = virtqueue_add_sgs(vq, sgs, outcnt, incnt, vbuf, GFP_ATOMIC);
  254. if (ret == -ENOSPC) {
  255. spin_unlock(&vgdev->ctrlq.qlock);
  256. wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= outcnt + incnt);
  257. spin_lock(&vgdev->ctrlq.qlock);
  258. goto retry;
  259. } else {
  260. virtqueue_kick(vq);
  261. }
  262. if (!ret)
  263. ret = vq->num_free;
  264. return ret;
  265. }
  266. static int virtio_gpu_queue_ctrl_buffer(struct virtio_gpu_device *vgdev,
  267. struct virtio_gpu_vbuffer *vbuf)
  268. {
  269. int rc;
  270. spin_lock(&vgdev->ctrlq.qlock);
  271. rc = virtio_gpu_queue_ctrl_buffer_locked(vgdev, vbuf);
  272. spin_unlock(&vgdev->ctrlq.qlock);
  273. return rc;
  274. }
  275. static int virtio_gpu_queue_fenced_ctrl_buffer(struct virtio_gpu_device *vgdev,
  276. struct virtio_gpu_vbuffer *vbuf,
  277. struct virtio_gpu_ctrl_hdr *hdr,
  278. struct virtio_gpu_fence **fence)
  279. {
  280. struct virtqueue *vq = vgdev->ctrlq.vq;
  281. int rc;
  282. again:
  283. spin_lock(&vgdev->ctrlq.qlock);
  284. /*
  285. * Make sure we have enouth space in the virtqueue. If not
  286. * wait here until we have.
  287. *
  288. * Without that virtio_gpu_queue_ctrl_buffer_nolock might have
  289. * to wait for free space, which can result in fence ids being
  290. * submitted out-of-order.
  291. */
  292. if (vq->num_free < 3) {
  293. spin_unlock(&vgdev->ctrlq.qlock);
  294. wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= 3);
  295. goto again;
  296. }
  297. if (fence)
  298. virtio_gpu_fence_emit(vgdev, hdr, fence);
  299. rc = virtio_gpu_queue_ctrl_buffer_locked(vgdev, vbuf);
  300. spin_unlock(&vgdev->ctrlq.qlock);
  301. return rc;
  302. }
  303. static int virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev,
  304. struct virtio_gpu_vbuffer *vbuf)
  305. {
  306. struct virtqueue *vq = vgdev->cursorq.vq;
  307. struct scatterlist *sgs[1], ccmd;
  308. int ret;
  309. int outcnt;
  310. if (!vgdev->vqs_ready)
  311. return -ENODEV;
  312. sg_init_one(&ccmd, vbuf->buf, vbuf->size);
  313. sgs[0] = &ccmd;
  314. outcnt = 1;
  315. spin_lock(&vgdev->cursorq.qlock);
  316. retry:
  317. ret = virtqueue_add_sgs(vq, sgs, outcnt, 0, vbuf, GFP_ATOMIC);
  318. if (ret == -ENOSPC) {
  319. spin_unlock(&vgdev->cursorq.qlock);
  320. wait_event(vgdev->cursorq.ack_queue, vq->num_free >= outcnt);
  321. spin_lock(&vgdev->cursorq.qlock);
  322. goto retry;
  323. } else {
  324. virtqueue_kick(vq);
  325. }
  326. spin_unlock(&vgdev->cursorq.qlock);
  327. if (!ret)
  328. ret = vq->num_free;
  329. return ret;
  330. }
  331. /* just create gem objects for userspace and long lived objects,
  332. * just use dma_alloced pages for the queue objects?
  333. */
  334. /* create a basic resource */
  335. void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
  336. uint32_t resource_id,
  337. uint32_t format,
  338. uint32_t width,
  339. uint32_t height)
  340. {
  341. struct virtio_gpu_resource_create_2d *cmd_p;
  342. struct virtio_gpu_vbuffer *vbuf;
  343. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  344. memset(cmd_p, 0, sizeof(*cmd_p));
  345. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_CREATE_2D);
  346. cmd_p->resource_id = cpu_to_le32(resource_id);
  347. cmd_p->format = cpu_to_le32(format);
  348. cmd_p->width = cpu_to_le32(width);
  349. cmd_p->height = cpu_to_le32(height);
  350. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  351. }
  352. void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
  353. uint32_t resource_id)
  354. {
  355. struct virtio_gpu_resource_unref *cmd_p;
  356. struct virtio_gpu_vbuffer *vbuf;
  357. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  358. memset(cmd_p, 0, sizeof(*cmd_p));
  359. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_UNREF);
  360. cmd_p->resource_id = cpu_to_le32(resource_id);
  361. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  362. }
  363. static void virtio_gpu_cmd_resource_inval_backing(struct virtio_gpu_device *vgdev,
  364. uint32_t resource_id,
  365. struct virtio_gpu_fence **fence)
  366. {
  367. struct virtio_gpu_resource_detach_backing *cmd_p;
  368. struct virtio_gpu_vbuffer *vbuf;
  369. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  370. memset(cmd_p, 0, sizeof(*cmd_p));
  371. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING);
  372. cmd_p->resource_id = cpu_to_le32(resource_id);
  373. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  374. }
  375. void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
  376. uint32_t scanout_id, uint32_t resource_id,
  377. uint32_t width, uint32_t height,
  378. uint32_t x, uint32_t y)
  379. {
  380. struct virtio_gpu_set_scanout *cmd_p;
  381. struct virtio_gpu_vbuffer *vbuf;
  382. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  383. memset(cmd_p, 0, sizeof(*cmd_p));
  384. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_SET_SCANOUT);
  385. cmd_p->resource_id = cpu_to_le32(resource_id);
  386. cmd_p->scanout_id = cpu_to_le32(scanout_id);
  387. cmd_p->r.width = cpu_to_le32(width);
  388. cmd_p->r.height = cpu_to_le32(height);
  389. cmd_p->r.x = cpu_to_le32(x);
  390. cmd_p->r.y = cpu_to_le32(y);
  391. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  392. }
  393. void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device *vgdev,
  394. uint32_t resource_id,
  395. uint32_t x, uint32_t y,
  396. uint32_t width, uint32_t height)
  397. {
  398. struct virtio_gpu_resource_flush *cmd_p;
  399. struct virtio_gpu_vbuffer *vbuf;
  400. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  401. memset(cmd_p, 0, sizeof(*cmd_p));
  402. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_FLUSH);
  403. cmd_p->resource_id = cpu_to_le32(resource_id);
  404. cmd_p->r.width = cpu_to_le32(width);
  405. cmd_p->r.height = cpu_to_le32(height);
  406. cmd_p->r.x = cpu_to_le32(x);
  407. cmd_p->r.y = cpu_to_le32(y);
  408. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  409. }
  410. void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
  411. struct virtio_gpu_object *bo,
  412. uint64_t offset,
  413. __le32 width, __le32 height,
  414. __le32 x, __le32 y,
  415. struct virtio_gpu_fence **fence)
  416. {
  417. struct virtio_gpu_transfer_to_host_2d *cmd_p;
  418. struct virtio_gpu_vbuffer *vbuf;
  419. bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
  420. if (use_dma_api)
  421. dma_sync_sg_for_device(vgdev->vdev->dev.parent,
  422. bo->pages->sgl, bo->pages->nents,
  423. DMA_TO_DEVICE);
  424. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  425. memset(cmd_p, 0, sizeof(*cmd_p));
  426. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D);
  427. cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
  428. cmd_p->offset = cpu_to_le64(offset);
  429. cmd_p->r.width = width;
  430. cmd_p->r.height = height;
  431. cmd_p->r.x = x;
  432. cmd_p->r.y = y;
  433. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  434. }
  435. static void
  436. virtio_gpu_cmd_resource_attach_backing(struct virtio_gpu_device *vgdev,
  437. uint32_t resource_id,
  438. struct virtio_gpu_mem_entry *ents,
  439. uint32_t nents,
  440. struct virtio_gpu_fence **fence)
  441. {
  442. struct virtio_gpu_resource_attach_backing *cmd_p;
  443. struct virtio_gpu_vbuffer *vbuf;
  444. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  445. memset(cmd_p, 0, sizeof(*cmd_p));
  446. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING);
  447. cmd_p->resource_id = cpu_to_le32(resource_id);
  448. cmd_p->nr_entries = cpu_to_le32(nents);
  449. vbuf->data_buf = ents;
  450. vbuf->data_size = sizeof(*ents) * nents;
  451. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  452. }
  453. static void virtio_gpu_cmd_get_display_info_cb(struct virtio_gpu_device *vgdev,
  454. struct virtio_gpu_vbuffer *vbuf)
  455. {
  456. struct virtio_gpu_resp_display_info *resp =
  457. (struct virtio_gpu_resp_display_info *)vbuf->resp_buf;
  458. int i;
  459. spin_lock(&vgdev->display_info_lock);
  460. for (i = 0; i < vgdev->num_scanouts; i++) {
  461. vgdev->outputs[i].info = resp->pmodes[i];
  462. if (resp->pmodes[i].enabled) {
  463. DRM_DEBUG("output %d: %dx%d+%d+%d", i,
  464. le32_to_cpu(resp->pmodes[i].r.width),
  465. le32_to_cpu(resp->pmodes[i].r.height),
  466. le32_to_cpu(resp->pmodes[i].r.x),
  467. le32_to_cpu(resp->pmodes[i].r.y));
  468. } else {
  469. DRM_DEBUG("output %d: disabled", i);
  470. }
  471. }
  472. vgdev->display_info_pending = false;
  473. spin_unlock(&vgdev->display_info_lock);
  474. wake_up(&vgdev->resp_wq);
  475. if (!drm_helper_hpd_irq_event(vgdev->ddev))
  476. drm_kms_helper_hotplug_event(vgdev->ddev);
  477. }
  478. static void virtio_gpu_cmd_get_capset_info_cb(struct virtio_gpu_device *vgdev,
  479. struct virtio_gpu_vbuffer *vbuf)
  480. {
  481. struct virtio_gpu_get_capset_info *cmd =
  482. (struct virtio_gpu_get_capset_info *)vbuf->buf;
  483. struct virtio_gpu_resp_capset_info *resp =
  484. (struct virtio_gpu_resp_capset_info *)vbuf->resp_buf;
  485. int i = le32_to_cpu(cmd->capset_index);
  486. spin_lock(&vgdev->display_info_lock);
  487. vgdev->capsets[i].id = le32_to_cpu(resp->capset_id);
  488. vgdev->capsets[i].max_version = le32_to_cpu(resp->capset_max_version);
  489. vgdev->capsets[i].max_size = le32_to_cpu(resp->capset_max_size);
  490. spin_unlock(&vgdev->display_info_lock);
  491. wake_up(&vgdev->resp_wq);
  492. }
  493. static void virtio_gpu_cmd_capset_cb(struct virtio_gpu_device *vgdev,
  494. struct virtio_gpu_vbuffer *vbuf)
  495. {
  496. struct virtio_gpu_get_capset *cmd =
  497. (struct virtio_gpu_get_capset *)vbuf->buf;
  498. struct virtio_gpu_resp_capset *resp =
  499. (struct virtio_gpu_resp_capset *)vbuf->resp_buf;
  500. struct virtio_gpu_drv_cap_cache *cache_ent;
  501. spin_lock(&vgdev->display_info_lock);
  502. list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
  503. if (cache_ent->version == le32_to_cpu(cmd->capset_version) &&
  504. cache_ent->id == le32_to_cpu(cmd->capset_id)) {
  505. memcpy(cache_ent->caps_cache, resp->capset_data,
  506. cache_ent->size);
  507. atomic_set(&cache_ent->is_valid, 1);
  508. break;
  509. }
  510. }
  511. spin_unlock(&vgdev->display_info_lock);
  512. wake_up(&vgdev->resp_wq);
  513. }
  514. int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev)
  515. {
  516. struct virtio_gpu_ctrl_hdr *cmd_p;
  517. struct virtio_gpu_vbuffer *vbuf;
  518. void *resp_buf;
  519. resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_display_info),
  520. GFP_KERNEL);
  521. if (!resp_buf)
  522. return -ENOMEM;
  523. cmd_p = virtio_gpu_alloc_cmd_resp
  524. (vgdev, &virtio_gpu_cmd_get_display_info_cb, &vbuf,
  525. sizeof(*cmd_p), sizeof(struct virtio_gpu_resp_display_info),
  526. resp_buf);
  527. memset(cmd_p, 0, sizeof(*cmd_p));
  528. vgdev->display_info_pending = true;
  529. cmd_p->type = cpu_to_le32(VIRTIO_GPU_CMD_GET_DISPLAY_INFO);
  530. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  531. return 0;
  532. }
  533. int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device *vgdev, int idx)
  534. {
  535. struct virtio_gpu_get_capset_info *cmd_p;
  536. struct virtio_gpu_vbuffer *vbuf;
  537. void *resp_buf;
  538. resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_capset_info),
  539. GFP_KERNEL);
  540. if (!resp_buf)
  541. return -ENOMEM;
  542. cmd_p = virtio_gpu_alloc_cmd_resp
  543. (vgdev, &virtio_gpu_cmd_get_capset_info_cb, &vbuf,
  544. sizeof(*cmd_p), sizeof(struct virtio_gpu_resp_capset_info),
  545. resp_buf);
  546. memset(cmd_p, 0, sizeof(*cmd_p));
  547. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_GET_CAPSET_INFO);
  548. cmd_p->capset_index = cpu_to_le32(idx);
  549. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  550. return 0;
  551. }
  552. int virtio_gpu_cmd_get_capset(struct virtio_gpu_device *vgdev,
  553. int idx, int version,
  554. struct virtio_gpu_drv_cap_cache **cache_p)
  555. {
  556. struct virtio_gpu_get_capset *cmd_p;
  557. struct virtio_gpu_vbuffer *vbuf;
  558. int max_size;
  559. struct virtio_gpu_drv_cap_cache *cache_ent;
  560. void *resp_buf;
  561. if (idx >= vgdev->num_capsets)
  562. return -EINVAL;
  563. if (version > vgdev->capsets[idx].max_version)
  564. return -EINVAL;
  565. cache_ent = kzalloc(sizeof(*cache_ent), GFP_KERNEL);
  566. if (!cache_ent)
  567. return -ENOMEM;
  568. max_size = vgdev->capsets[idx].max_size;
  569. cache_ent->caps_cache = kmalloc(max_size, GFP_KERNEL);
  570. if (!cache_ent->caps_cache) {
  571. kfree(cache_ent);
  572. return -ENOMEM;
  573. }
  574. resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_capset) + max_size,
  575. GFP_KERNEL);
  576. if (!resp_buf) {
  577. kfree(cache_ent->caps_cache);
  578. kfree(cache_ent);
  579. return -ENOMEM;
  580. }
  581. cache_ent->version = version;
  582. cache_ent->id = vgdev->capsets[idx].id;
  583. atomic_set(&cache_ent->is_valid, 0);
  584. cache_ent->size = max_size;
  585. spin_lock(&vgdev->display_info_lock);
  586. list_add_tail(&cache_ent->head, &vgdev->cap_cache);
  587. spin_unlock(&vgdev->display_info_lock);
  588. cmd_p = virtio_gpu_alloc_cmd_resp
  589. (vgdev, &virtio_gpu_cmd_capset_cb, &vbuf, sizeof(*cmd_p),
  590. sizeof(struct virtio_gpu_resp_capset) + max_size,
  591. resp_buf);
  592. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_GET_CAPSET);
  593. cmd_p->capset_id = cpu_to_le32(vgdev->capsets[idx].id);
  594. cmd_p->capset_version = cpu_to_le32(version);
  595. *cache_p = cache_ent;
  596. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  597. return 0;
  598. }
  599. void virtio_gpu_cmd_context_create(struct virtio_gpu_device *vgdev, uint32_t id,
  600. uint32_t nlen, const char *name)
  601. {
  602. struct virtio_gpu_ctx_create *cmd_p;
  603. struct virtio_gpu_vbuffer *vbuf;
  604. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  605. memset(cmd_p, 0, sizeof(*cmd_p));
  606. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_CREATE);
  607. cmd_p->hdr.ctx_id = cpu_to_le32(id);
  608. cmd_p->nlen = cpu_to_le32(nlen);
  609. strncpy(cmd_p->debug_name, name, sizeof(cmd_p->debug_name) - 1);
  610. cmd_p->debug_name[sizeof(cmd_p->debug_name) - 1] = 0;
  611. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  612. }
  613. void virtio_gpu_cmd_context_destroy(struct virtio_gpu_device *vgdev,
  614. uint32_t id)
  615. {
  616. struct virtio_gpu_ctx_destroy *cmd_p;
  617. struct virtio_gpu_vbuffer *vbuf;
  618. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  619. memset(cmd_p, 0, sizeof(*cmd_p));
  620. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_DESTROY);
  621. cmd_p->hdr.ctx_id = cpu_to_le32(id);
  622. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  623. }
  624. void virtio_gpu_cmd_context_attach_resource(struct virtio_gpu_device *vgdev,
  625. uint32_t ctx_id,
  626. uint32_t resource_id)
  627. {
  628. struct virtio_gpu_ctx_resource *cmd_p;
  629. struct virtio_gpu_vbuffer *vbuf;
  630. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  631. memset(cmd_p, 0, sizeof(*cmd_p));
  632. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE);
  633. cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
  634. cmd_p->resource_id = cpu_to_le32(resource_id);
  635. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  636. }
  637. void virtio_gpu_cmd_context_detach_resource(struct virtio_gpu_device *vgdev,
  638. uint32_t ctx_id,
  639. uint32_t resource_id)
  640. {
  641. struct virtio_gpu_ctx_resource *cmd_p;
  642. struct virtio_gpu_vbuffer *vbuf;
  643. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  644. memset(cmd_p, 0, sizeof(*cmd_p));
  645. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE);
  646. cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
  647. cmd_p->resource_id = cpu_to_le32(resource_id);
  648. virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
  649. }
  650. void
  651. virtio_gpu_cmd_resource_create_3d(struct virtio_gpu_device *vgdev,
  652. struct virtio_gpu_resource_create_3d *rc_3d,
  653. struct virtio_gpu_fence **fence)
  654. {
  655. struct virtio_gpu_resource_create_3d *cmd_p;
  656. struct virtio_gpu_vbuffer *vbuf;
  657. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  658. memset(cmd_p, 0, sizeof(*cmd_p));
  659. *cmd_p = *rc_3d;
  660. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_CREATE_3D);
  661. cmd_p->hdr.flags = 0;
  662. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  663. }
  664. void virtio_gpu_cmd_transfer_to_host_3d(struct virtio_gpu_device *vgdev,
  665. struct virtio_gpu_object *bo,
  666. uint32_t ctx_id,
  667. uint64_t offset, uint32_t level,
  668. struct virtio_gpu_box *box,
  669. struct virtio_gpu_fence **fence)
  670. {
  671. struct virtio_gpu_transfer_host_3d *cmd_p;
  672. struct virtio_gpu_vbuffer *vbuf;
  673. bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
  674. if (use_dma_api)
  675. dma_sync_sg_for_device(vgdev->vdev->dev.parent,
  676. bo->pages->sgl, bo->pages->nents,
  677. DMA_TO_DEVICE);
  678. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  679. memset(cmd_p, 0, sizeof(*cmd_p));
  680. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D);
  681. cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
  682. cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
  683. cmd_p->box = *box;
  684. cmd_p->offset = cpu_to_le64(offset);
  685. cmd_p->level = cpu_to_le32(level);
  686. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  687. }
  688. void virtio_gpu_cmd_transfer_from_host_3d(struct virtio_gpu_device *vgdev,
  689. uint32_t resource_id, uint32_t ctx_id,
  690. uint64_t offset, uint32_t level,
  691. struct virtio_gpu_box *box,
  692. struct virtio_gpu_fence **fence)
  693. {
  694. struct virtio_gpu_transfer_host_3d *cmd_p;
  695. struct virtio_gpu_vbuffer *vbuf;
  696. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  697. memset(cmd_p, 0, sizeof(*cmd_p));
  698. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D);
  699. cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
  700. cmd_p->resource_id = cpu_to_le32(resource_id);
  701. cmd_p->box = *box;
  702. cmd_p->offset = cpu_to_le64(offset);
  703. cmd_p->level = cpu_to_le32(level);
  704. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  705. }
  706. void virtio_gpu_cmd_submit(struct virtio_gpu_device *vgdev,
  707. void *data, uint32_t data_size,
  708. uint32_t ctx_id, struct virtio_gpu_fence **fence)
  709. {
  710. struct virtio_gpu_cmd_submit *cmd_p;
  711. struct virtio_gpu_vbuffer *vbuf;
  712. cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
  713. memset(cmd_p, 0, sizeof(*cmd_p));
  714. vbuf->data_buf = data;
  715. vbuf->data_size = data_size;
  716. cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_SUBMIT_3D);
  717. cmd_p->hdr.ctx_id = cpu_to_le32(ctx_id);
  718. cmd_p->size = cpu_to_le32(data_size);
  719. virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, &cmd_p->hdr, fence);
  720. }
  721. int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
  722. struct virtio_gpu_object *obj,
  723. uint32_t resource_id,
  724. struct virtio_gpu_fence **fence)
  725. {
  726. bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
  727. struct virtio_gpu_mem_entry *ents;
  728. struct scatterlist *sg;
  729. int si, nents;
  730. if (!obj->pages) {
  731. int ret;
  732. ret = virtio_gpu_object_get_sg_table(vgdev, obj);
  733. if (ret)
  734. return ret;
  735. }
  736. if (use_dma_api) {
  737. obj->mapped = dma_map_sg(vgdev->vdev->dev.parent,
  738. obj->pages->sgl, obj->pages->nents,
  739. DMA_TO_DEVICE);
  740. nents = obj->mapped;
  741. } else {
  742. nents = obj->pages->nents;
  743. }
  744. /* gets freed when the ring has consumed it */
  745. ents = kmalloc_array(nents, sizeof(struct virtio_gpu_mem_entry),
  746. GFP_KERNEL);
  747. if (!ents) {
  748. DRM_ERROR("failed to allocate ent list\n");
  749. return -ENOMEM;
  750. }
  751. for_each_sg(obj->pages->sgl, sg, nents, si) {
  752. ents[si].addr = cpu_to_le64(use_dma_api
  753. ? sg_dma_address(sg)
  754. : sg_phys(sg));
  755. ents[si].length = cpu_to_le32(sg->length);
  756. ents[si].padding = 0;
  757. }
  758. virtio_gpu_cmd_resource_attach_backing(vgdev, resource_id,
  759. ents, nents,
  760. fence);
  761. obj->hw_res_handle = resource_id;
  762. return 0;
  763. }
  764. void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev,
  765. struct virtio_gpu_object *obj)
  766. {
  767. bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
  768. struct virtio_gpu_fence *fence;
  769. if (use_dma_api && obj->mapped) {
  770. /* detach backing and wait for the host process it ... */
  771. virtio_gpu_cmd_resource_inval_backing(vgdev, obj->hw_res_handle, &fence);
  772. dma_fence_wait(&fence->f, true);
  773. dma_fence_put(&fence->f);
  774. /* ... then tear down iommu mappings */
  775. dma_unmap_sg(vgdev->vdev->dev.parent,
  776. obj->pages->sgl, obj->mapped,
  777. DMA_TO_DEVICE);
  778. obj->mapped = 0;
  779. } else {
  780. virtio_gpu_cmd_resource_inval_backing(vgdev, obj->hw_res_handle, NULL);
  781. }
  782. }
  783. void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
  784. struct virtio_gpu_output *output)
  785. {
  786. struct virtio_gpu_vbuffer *vbuf;
  787. struct virtio_gpu_update_cursor *cur_p;
  788. output->cursor.pos.scanout_id = cpu_to_le32(output->index);
  789. cur_p = virtio_gpu_alloc_cursor(vgdev, &vbuf);
  790. memcpy(cur_p, &output->cursor, sizeof(output->cursor));
  791. virtio_gpu_queue_cursor(vgdev, vbuf);
  792. }