virtgpu_ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie
  7. * Alon Levy
  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 shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/virtgpu_drm.h>
  29. #include <drm/ttm/ttm_execbuf_util.h>
  30. #include "virtgpu_drv.h"
  31. static void convert_to_hw_box(struct virtio_gpu_box *dst,
  32. const struct drm_virtgpu_3d_box *src)
  33. {
  34. dst->x = cpu_to_le32(src->x);
  35. dst->y = cpu_to_le32(src->y);
  36. dst->z = cpu_to_le32(src->z);
  37. dst->w = cpu_to_le32(src->w);
  38. dst->h = cpu_to_le32(src->h);
  39. dst->d = cpu_to_le32(src->d);
  40. }
  41. static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data,
  42. struct drm_file *file_priv)
  43. {
  44. struct virtio_gpu_device *vgdev = dev->dev_private;
  45. struct drm_virtgpu_map *virtio_gpu_map = data;
  46. return virtio_gpu_mode_dumb_mmap(file_priv, vgdev->ddev,
  47. virtio_gpu_map->handle,
  48. &virtio_gpu_map->offset);
  49. }
  50. static int virtio_gpu_object_list_validate(struct ww_acquire_ctx *ticket,
  51. struct list_head *head)
  52. {
  53. struct ttm_operation_ctx ctx = { false, false };
  54. struct ttm_validate_buffer *buf;
  55. struct ttm_buffer_object *bo;
  56. struct virtio_gpu_object *qobj;
  57. int ret;
  58. ret = ttm_eu_reserve_buffers(ticket, head, true, NULL);
  59. if (ret != 0)
  60. return ret;
  61. list_for_each_entry(buf, head, head) {
  62. bo = buf->bo;
  63. qobj = container_of(bo, struct virtio_gpu_object, tbo);
  64. ret = ttm_bo_validate(bo, &qobj->placement, &ctx);
  65. if (ret) {
  66. ttm_eu_backoff_reservation(ticket, head);
  67. return ret;
  68. }
  69. }
  70. return 0;
  71. }
  72. static void virtio_gpu_unref_list(struct list_head *head)
  73. {
  74. struct ttm_validate_buffer *buf;
  75. struct ttm_buffer_object *bo;
  76. struct virtio_gpu_object *qobj;
  77. list_for_each_entry(buf, head, head) {
  78. bo = buf->bo;
  79. qobj = container_of(bo, struct virtio_gpu_object, tbo);
  80. drm_gem_object_put_unlocked(&qobj->gem_base);
  81. }
  82. }
  83. /*
  84. * Usage of execbuffer:
  85. * Relocations need to take into account the full VIRTIO_GPUDrawable size.
  86. * However, the command as passed from user space must *not* contain the initial
  87. * VIRTIO_GPUReleaseInfo struct (first XXX bytes)
  88. */
  89. static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
  90. struct drm_file *drm_file)
  91. {
  92. struct drm_virtgpu_execbuffer *exbuf = data;
  93. struct virtio_gpu_device *vgdev = dev->dev_private;
  94. struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv;
  95. struct drm_gem_object *gobj;
  96. struct virtio_gpu_fence *fence;
  97. struct virtio_gpu_object *qobj;
  98. int ret;
  99. uint32_t *bo_handles = NULL;
  100. void __user *user_bo_handles = NULL;
  101. struct list_head validate_list;
  102. struct ttm_validate_buffer *buflist = NULL;
  103. int i;
  104. struct ww_acquire_ctx ticket;
  105. void *buf;
  106. if (vgdev->has_virgl_3d == false)
  107. return -ENOSYS;
  108. INIT_LIST_HEAD(&validate_list);
  109. if (exbuf->num_bo_handles) {
  110. bo_handles = kvmalloc_array(exbuf->num_bo_handles,
  111. sizeof(uint32_t), GFP_KERNEL);
  112. buflist = kvmalloc_array(exbuf->num_bo_handles,
  113. sizeof(struct ttm_validate_buffer),
  114. GFP_KERNEL | __GFP_ZERO);
  115. if (!bo_handles || !buflist) {
  116. kvfree(bo_handles);
  117. kvfree(buflist);
  118. return -ENOMEM;
  119. }
  120. user_bo_handles = (void __user *)(uintptr_t)exbuf->bo_handles;
  121. if (copy_from_user(bo_handles, user_bo_handles,
  122. exbuf->num_bo_handles * sizeof(uint32_t))) {
  123. ret = -EFAULT;
  124. kvfree(bo_handles);
  125. kvfree(buflist);
  126. return ret;
  127. }
  128. for (i = 0; i < exbuf->num_bo_handles; i++) {
  129. gobj = drm_gem_object_lookup(drm_file, bo_handles[i]);
  130. if (!gobj) {
  131. kvfree(bo_handles);
  132. kvfree(buflist);
  133. return -ENOENT;
  134. }
  135. qobj = gem_to_virtio_gpu_obj(gobj);
  136. buflist[i].bo = &qobj->tbo;
  137. list_add(&buflist[i].head, &validate_list);
  138. }
  139. kvfree(bo_handles);
  140. }
  141. ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
  142. if (ret)
  143. goto out_free;
  144. buf = memdup_user((void __user *)(uintptr_t)exbuf->command,
  145. exbuf->size);
  146. if (IS_ERR(buf)) {
  147. ret = PTR_ERR(buf);
  148. goto out_unresv;
  149. }
  150. virtio_gpu_cmd_submit(vgdev, buf, exbuf->size,
  151. vfpriv->ctx_id, &fence);
  152. ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
  153. /* fence the command bo */
  154. virtio_gpu_unref_list(&validate_list);
  155. kvfree(buflist);
  156. dma_fence_put(&fence->f);
  157. return 0;
  158. out_unresv:
  159. ttm_eu_backoff_reservation(&ticket, &validate_list);
  160. out_free:
  161. virtio_gpu_unref_list(&validate_list);
  162. kvfree(buflist);
  163. return ret;
  164. }
  165. static int virtio_gpu_getparam_ioctl(struct drm_device *dev, void *data,
  166. struct drm_file *file_priv)
  167. {
  168. struct virtio_gpu_device *vgdev = dev->dev_private;
  169. struct drm_virtgpu_getparam *param = data;
  170. int value;
  171. switch (param->param) {
  172. case VIRTGPU_PARAM_3D_FEATURES:
  173. value = vgdev->has_virgl_3d == true ? 1 : 0;
  174. break;
  175. case VIRTGPU_PARAM_CAPSET_QUERY_FIX:
  176. value = 1;
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. if (copy_to_user((void __user *)(unsigned long)param->value,
  182. &value, sizeof(int))) {
  183. return -EFAULT;
  184. }
  185. return 0;
  186. }
  187. static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
  188. struct drm_file *file_priv)
  189. {
  190. struct virtio_gpu_device *vgdev = dev->dev_private;
  191. struct drm_virtgpu_resource_create *rc = data;
  192. int ret;
  193. uint32_t res_id;
  194. struct virtio_gpu_object *qobj;
  195. struct drm_gem_object *obj;
  196. uint32_t handle = 0;
  197. uint32_t size;
  198. struct list_head validate_list;
  199. struct ttm_validate_buffer mainbuf;
  200. struct virtio_gpu_fence *fence = NULL;
  201. struct ww_acquire_ctx ticket;
  202. struct virtio_gpu_resource_create_3d rc_3d;
  203. if (vgdev->has_virgl_3d == false) {
  204. if (rc->depth > 1)
  205. return -EINVAL;
  206. if (rc->nr_samples > 1)
  207. return -EINVAL;
  208. if (rc->last_level > 1)
  209. return -EINVAL;
  210. if (rc->target != 2)
  211. return -EINVAL;
  212. if (rc->array_size > 1)
  213. return -EINVAL;
  214. }
  215. INIT_LIST_HEAD(&validate_list);
  216. memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer));
  217. virtio_gpu_resource_id_get(vgdev, &res_id);
  218. size = rc->size;
  219. /* allocate a single page size object */
  220. if (size == 0)
  221. size = PAGE_SIZE;
  222. qobj = virtio_gpu_alloc_object(dev, size, false, false);
  223. if (IS_ERR(qobj)) {
  224. ret = PTR_ERR(qobj);
  225. goto fail_id;
  226. }
  227. obj = &qobj->gem_base;
  228. if (!vgdev->has_virgl_3d) {
  229. virtio_gpu_cmd_create_resource(vgdev, res_id, rc->format,
  230. rc->width, rc->height);
  231. ret = virtio_gpu_object_attach(vgdev, qobj, res_id, NULL);
  232. } else {
  233. /* use a gem reference since unref list undoes them */
  234. drm_gem_object_get(&qobj->gem_base);
  235. mainbuf.bo = &qobj->tbo;
  236. list_add(&mainbuf.head, &validate_list);
  237. ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
  238. if (ret) {
  239. DRM_DEBUG("failed to validate\n");
  240. goto fail_unref;
  241. }
  242. rc_3d.resource_id = cpu_to_le32(res_id);
  243. rc_3d.target = cpu_to_le32(rc->target);
  244. rc_3d.format = cpu_to_le32(rc->format);
  245. rc_3d.bind = cpu_to_le32(rc->bind);
  246. rc_3d.width = cpu_to_le32(rc->width);
  247. rc_3d.height = cpu_to_le32(rc->height);
  248. rc_3d.depth = cpu_to_le32(rc->depth);
  249. rc_3d.array_size = cpu_to_le32(rc->array_size);
  250. rc_3d.last_level = cpu_to_le32(rc->last_level);
  251. rc_3d.nr_samples = cpu_to_le32(rc->nr_samples);
  252. rc_3d.flags = cpu_to_le32(rc->flags);
  253. virtio_gpu_cmd_resource_create_3d(vgdev, &rc_3d, NULL);
  254. ret = virtio_gpu_object_attach(vgdev, qobj, res_id, &fence);
  255. if (ret) {
  256. ttm_eu_backoff_reservation(&ticket, &validate_list);
  257. goto fail_unref;
  258. }
  259. ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
  260. }
  261. qobj->hw_res_handle = res_id;
  262. ret = drm_gem_handle_create(file_priv, obj, &handle);
  263. if (ret) {
  264. drm_gem_object_release(obj);
  265. if (vgdev->has_virgl_3d) {
  266. virtio_gpu_unref_list(&validate_list);
  267. dma_fence_put(&fence->f);
  268. }
  269. return ret;
  270. }
  271. drm_gem_object_put_unlocked(obj);
  272. rc->res_handle = res_id; /* similiar to a VM address */
  273. rc->bo_handle = handle;
  274. if (vgdev->has_virgl_3d) {
  275. virtio_gpu_unref_list(&validate_list);
  276. dma_fence_put(&fence->f);
  277. }
  278. return 0;
  279. fail_unref:
  280. if (vgdev->has_virgl_3d) {
  281. virtio_gpu_unref_list(&validate_list);
  282. dma_fence_put(&fence->f);
  283. }
  284. //fail_obj:
  285. // drm_gem_object_handle_unreference_unlocked(obj);
  286. fail_id:
  287. virtio_gpu_resource_id_put(vgdev, res_id);
  288. return ret;
  289. }
  290. static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
  291. struct drm_file *file_priv)
  292. {
  293. struct drm_virtgpu_resource_info *ri = data;
  294. struct drm_gem_object *gobj = NULL;
  295. struct virtio_gpu_object *qobj = NULL;
  296. gobj = drm_gem_object_lookup(file_priv, ri->bo_handle);
  297. if (gobj == NULL)
  298. return -ENOENT;
  299. qobj = gem_to_virtio_gpu_obj(gobj);
  300. ri->size = qobj->gem_base.size;
  301. ri->res_handle = qobj->hw_res_handle;
  302. drm_gem_object_put_unlocked(gobj);
  303. return 0;
  304. }
  305. static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
  306. void *data,
  307. struct drm_file *file)
  308. {
  309. struct virtio_gpu_device *vgdev = dev->dev_private;
  310. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  311. struct drm_virtgpu_3d_transfer_from_host *args = data;
  312. struct ttm_operation_ctx ctx = { true, false };
  313. struct drm_gem_object *gobj = NULL;
  314. struct virtio_gpu_object *qobj = NULL;
  315. struct virtio_gpu_fence *fence;
  316. int ret;
  317. u32 offset = args->offset;
  318. struct virtio_gpu_box box;
  319. if (vgdev->has_virgl_3d == false)
  320. return -ENOSYS;
  321. gobj = drm_gem_object_lookup(file, args->bo_handle);
  322. if (gobj == NULL)
  323. return -ENOENT;
  324. qobj = gem_to_virtio_gpu_obj(gobj);
  325. ret = virtio_gpu_object_reserve(qobj, false);
  326. if (ret)
  327. goto out;
  328. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
  329. if (unlikely(ret))
  330. goto out_unres;
  331. convert_to_hw_box(&box, &args->box);
  332. virtio_gpu_cmd_transfer_from_host_3d
  333. (vgdev, qobj->hw_res_handle,
  334. vfpriv->ctx_id, offset, args->level,
  335. &box, &fence);
  336. reservation_object_add_excl_fence(qobj->tbo.resv,
  337. &fence->f);
  338. dma_fence_put(&fence->f);
  339. out_unres:
  340. virtio_gpu_object_unreserve(qobj);
  341. out:
  342. drm_gem_object_put_unlocked(gobj);
  343. return ret;
  344. }
  345. static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
  346. struct drm_file *file)
  347. {
  348. struct virtio_gpu_device *vgdev = dev->dev_private;
  349. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  350. struct drm_virtgpu_3d_transfer_to_host *args = data;
  351. struct ttm_operation_ctx ctx = { true, false };
  352. struct drm_gem_object *gobj = NULL;
  353. struct virtio_gpu_object *qobj = NULL;
  354. struct virtio_gpu_fence *fence;
  355. struct virtio_gpu_box box;
  356. int ret;
  357. u32 offset = args->offset;
  358. gobj = drm_gem_object_lookup(file, args->bo_handle);
  359. if (gobj == NULL)
  360. return -ENOENT;
  361. qobj = gem_to_virtio_gpu_obj(gobj);
  362. ret = virtio_gpu_object_reserve(qobj, false);
  363. if (ret)
  364. goto out;
  365. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
  366. if (unlikely(ret))
  367. goto out_unres;
  368. convert_to_hw_box(&box, &args->box);
  369. if (!vgdev->has_virgl_3d) {
  370. virtio_gpu_cmd_transfer_to_host_2d
  371. (vgdev, qobj, offset,
  372. box.w, box.h, box.x, box.y, NULL);
  373. } else {
  374. virtio_gpu_cmd_transfer_to_host_3d
  375. (vgdev, qobj,
  376. vfpriv ? vfpriv->ctx_id : 0, offset,
  377. args->level, &box, &fence);
  378. reservation_object_add_excl_fence(qobj->tbo.resv,
  379. &fence->f);
  380. dma_fence_put(&fence->f);
  381. }
  382. out_unres:
  383. virtio_gpu_object_unreserve(qobj);
  384. out:
  385. drm_gem_object_put_unlocked(gobj);
  386. return ret;
  387. }
  388. static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
  389. struct drm_file *file)
  390. {
  391. struct drm_virtgpu_3d_wait *args = data;
  392. struct drm_gem_object *gobj = NULL;
  393. struct virtio_gpu_object *qobj = NULL;
  394. int ret;
  395. bool nowait = false;
  396. gobj = drm_gem_object_lookup(file, args->handle);
  397. if (gobj == NULL)
  398. return -ENOENT;
  399. qobj = gem_to_virtio_gpu_obj(gobj);
  400. if (args->flags & VIRTGPU_WAIT_NOWAIT)
  401. nowait = true;
  402. ret = virtio_gpu_object_wait(qobj, nowait);
  403. drm_gem_object_put_unlocked(gobj);
  404. return ret;
  405. }
  406. static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
  407. void *data, struct drm_file *file)
  408. {
  409. struct virtio_gpu_device *vgdev = dev->dev_private;
  410. struct drm_virtgpu_get_caps *args = data;
  411. unsigned size, host_caps_size;
  412. int i;
  413. int found_valid = -1;
  414. int ret;
  415. struct virtio_gpu_drv_cap_cache *cache_ent;
  416. void *ptr;
  417. if (vgdev->num_capsets == 0)
  418. return -ENOSYS;
  419. /* don't allow userspace to pass 0 */
  420. if (args->size == 0)
  421. return -EINVAL;
  422. spin_lock(&vgdev->display_info_lock);
  423. for (i = 0; i < vgdev->num_capsets; i++) {
  424. if (vgdev->capsets[i].id == args->cap_set_id) {
  425. if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
  426. found_valid = i;
  427. break;
  428. }
  429. }
  430. }
  431. if (found_valid == -1) {
  432. spin_unlock(&vgdev->display_info_lock);
  433. return -EINVAL;
  434. }
  435. host_caps_size = vgdev->capsets[found_valid].max_size;
  436. /* only copy to user the minimum of the host caps size or the guest caps size */
  437. size = min(args->size, host_caps_size);
  438. list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
  439. if (cache_ent->id == args->cap_set_id &&
  440. cache_ent->version == args->cap_set_ver) {
  441. ptr = cache_ent->caps_cache;
  442. spin_unlock(&vgdev->display_info_lock);
  443. goto copy_exit;
  444. }
  445. }
  446. spin_unlock(&vgdev->display_info_lock);
  447. /* not in cache - need to talk to hw */
  448. virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
  449. &cache_ent);
  450. ret = wait_event_timeout(vgdev->resp_wq,
  451. atomic_read(&cache_ent->is_valid), 5 * HZ);
  452. if (!ret)
  453. return -EBUSY;
  454. ptr = cache_ent->caps_cache;
  455. copy_exit:
  456. if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size))
  457. return -EFAULT;
  458. return 0;
  459. }
  460. struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
  461. DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
  462. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  463. DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
  464. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  465. DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
  466. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  467. DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
  468. virtio_gpu_resource_create_ioctl,
  469. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  470. DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
  471. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  472. /* make transfer async to the main ring? - no sure, can we
  473. * thread these in the underlying GL
  474. */
  475. DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
  476. virtio_gpu_transfer_from_host_ioctl,
  477. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  478. DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
  479. virtio_gpu_transfer_to_host_ioctl,
  480. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  481. DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
  482. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  483. DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
  484. DRM_AUTH | DRM_UNLOCKED | DRM_RENDER_ALLOW),
  485. };