virtgpu_ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. default:
  176. return -EINVAL;
  177. }
  178. if (copy_to_user((void __user *)(unsigned long)param->value,
  179. &value, sizeof(int))) {
  180. return -EFAULT;
  181. }
  182. return 0;
  183. }
  184. static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,
  185. struct drm_file *file_priv)
  186. {
  187. struct virtio_gpu_device *vgdev = dev->dev_private;
  188. struct drm_virtgpu_resource_create *rc = data;
  189. int ret;
  190. uint32_t res_id;
  191. struct virtio_gpu_object *qobj;
  192. struct drm_gem_object *obj;
  193. uint32_t handle = 0;
  194. uint32_t size;
  195. struct list_head validate_list;
  196. struct ttm_validate_buffer mainbuf;
  197. struct virtio_gpu_fence *fence = NULL;
  198. struct ww_acquire_ctx ticket;
  199. struct virtio_gpu_resource_create_3d rc_3d;
  200. if (vgdev->has_virgl_3d == false) {
  201. if (rc->depth > 1)
  202. return -EINVAL;
  203. if (rc->nr_samples > 1)
  204. return -EINVAL;
  205. if (rc->last_level > 1)
  206. return -EINVAL;
  207. if (rc->target != 2)
  208. return -EINVAL;
  209. if (rc->array_size > 1)
  210. return -EINVAL;
  211. }
  212. INIT_LIST_HEAD(&validate_list);
  213. memset(&mainbuf, 0, sizeof(struct ttm_validate_buffer));
  214. virtio_gpu_resource_id_get(vgdev, &res_id);
  215. size = rc->size;
  216. /* allocate a single page size object */
  217. if (size == 0)
  218. size = PAGE_SIZE;
  219. qobj = virtio_gpu_alloc_object(dev, size, false, false);
  220. if (IS_ERR(qobj)) {
  221. ret = PTR_ERR(qobj);
  222. goto fail_id;
  223. }
  224. obj = &qobj->gem_base;
  225. if (!vgdev->has_virgl_3d) {
  226. virtio_gpu_cmd_create_resource(vgdev, res_id, rc->format,
  227. rc->width, rc->height);
  228. ret = virtio_gpu_object_attach(vgdev, qobj, res_id, NULL);
  229. } else {
  230. /* use a gem reference since unref list undoes them */
  231. drm_gem_object_get(&qobj->gem_base);
  232. mainbuf.bo = &qobj->tbo;
  233. list_add(&mainbuf.head, &validate_list);
  234. ret = virtio_gpu_object_list_validate(&ticket, &validate_list);
  235. if (ret) {
  236. DRM_DEBUG("failed to validate\n");
  237. goto fail_unref;
  238. }
  239. rc_3d.resource_id = cpu_to_le32(res_id);
  240. rc_3d.target = cpu_to_le32(rc->target);
  241. rc_3d.format = cpu_to_le32(rc->format);
  242. rc_3d.bind = cpu_to_le32(rc->bind);
  243. rc_3d.width = cpu_to_le32(rc->width);
  244. rc_3d.height = cpu_to_le32(rc->height);
  245. rc_3d.depth = cpu_to_le32(rc->depth);
  246. rc_3d.array_size = cpu_to_le32(rc->array_size);
  247. rc_3d.last_level = cpu_to_le32(rc->last_level);
  248. rc_3d.nr_samples = cpu_to_le32(rc->nr_samples);
  249. rc_3d.flags = cpu_to_le32(rc->flags);
  250. virtio_gpu_cmd_resource_create_3d(vgdev, &rc_3d, NULL);
  251. ret = virtio_gpu_object_attach(vgdev, qobj, res_id, &fence);
  252. if (ret) {
  253. ttm_eu_backoff_reservation(&ticket, &validate_list);
  254. goto fail_unref;
  255. }
  256. ttm_eu_fence_buffer_objects(&ticket, &validate_list, &fence->f);
  257. }
  258. qobj->hw_res_handle = res_id;
  259. ret = drm_gem_handle_create(file_priv, obj, &handle);
  260. if (ret) {
  261. drm_gem_object_release(obj);
  262. if (vgdev->has_virgl_3d) {
  263. virtio_gpu_unref_list(&validate_list);
  264. dma_fence_put(&fence->f);
  265. }
  266. return ret;
  267. }
  268. drm_gem_object_put_unlocked(obj);
  269. rc->res_handle = res_id; /* similiar to a VM address */
  270. rc->bo_handle = handle;
  271. if (vgdev->has_virgl_3d) {
  272. virtio_gpu_unref_list(&validate_list);
  273. dma_fence_put(&fence->f);
  274. }
  275. return 0;
  276. fail_unref:
  277. if (vgdev->has_virgl_3d) {
  278. virtio_gpu_unref_list(&validate_list);
  279. dma_fence_put(&fence->f);
  280. }
  281. //fail_obj:
  282. // drm_gem_object_handle_unreference_unlocked(obj);
  283. fail_id:
  284. virtio_gpu_resource_id_put(vgdev, res_id);
  285. return ret;
  286. }
  287. static int virtio_gpu_resource_info_ioctl(struct drm_device *dev, void *data,
  288. struct drm_file *file_priv)
  289. {
  290. struct drm_virtgpu_resource_info *ri = data;
  291. struct drm_gem_object *gobj = NULL;
  292. struct virtio_gpu_object *qobj = NULL;
  293. gobj = drm_gem_object_lookup(file_priv, ri->bo_handle);
  294. if (gobj == NULL)
  295. return -ENOENT;
  296. qobj = gem_to_virtio_gpu_obj(gobj);
  297. ri->size = qobj->gem_base.size;
  298. ri->res_handle = qobj->hw_res_handle;
  299. drm_gem_object_put_unlocked(gobj);
  300. return 0;
  301. }
  302. static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev,
  303. void *data,
  304. struct drm_file *file)
  305. {
  306. struct virtio_gpu_device *vgdev = dev->dev_private;
  307. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  308. struct drm_virtgpu_3d_transfer_from_host *args = data;
  309. struct ttm_operation_ctx ctx = { true, false };
  310. struct drm_gem_object *gobj = NULL;
  311. struct virtio_gpu_object *qobj = NULL;
  312. struct virtio_gpu_fence *fence;
  313. int ret;
  314. u32 offset = args->offset;
  315. struct virtio_gpu_box box;
  316. if (vgdev->has_virgl_3d == false)
  317. return -ENOSYS;
  318. gobj = drm_gem_object_lookup(file, args->bo_handle);
  319. if (gobj == NULL)
  320. return -ENOENT;
  321. qobj = gem_to_virtio_gpu_obj(gobj);
  322. ret = virtio_gpu_object_reserve(qobj, false);
  323. if (ret)
  324. goto out;
  325. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
  326. if (unlikely(ret))
  327. goto out_unres;
  328. convert_to_hw_box(&box, &args->box);
  329. virtio_gpu_cmd_transfer_from_host_3d
  330. (vgdev, qobj->hw_res_handle,
  331. vfpriv->ctx_id, offset, args->level,
  332. &box, &fence);
  333. reservation_object_add_excl_fence(qobj->tbo.resv,
  334. &fence->f);
  335. dma_fence_put(&fence->f);
  336. out_unres:
  337. virtio_gpu_object_unreserve(qobj);
  338. out:
  339. drm_gem_object_put_unlocked(gobj);
  340. return ret;
  341. }
  342. static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data,
  343. struct drm_file *file)
  344. {
  345. struct virtio_gpu_device *vgdev = dev->dev_private;
  346. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  347. struct drm_virtgpu_3d_transfer_to_host *args = data;
  348. struct ttm_operation_ctx ctx = { true, false };
  349. struct drm_gem_object *gobj = NULL;
  350. struct virtio_gpu_object *qobj = NULL;
  351. struct virtio_gpu_fence *fence;
  352. struct virtio_gpu_box box;
  353. int ret;
  354. u32 offset = args->offset;
  355. gobj = drm_gem_object_lookup(file, args->bo_handle);
  356. if (gobj == NULL)
  357. return -ENOENT;
  358. qobj = gem_to_virtio_gpu_obj(gobj);
  359. ret = virtio_gpu_object_reserve(qobj, false);
  360. if (ret)
  361. goto out;
  362. ret = ttm_bo_validate(&qobj->tbo, &qobj->placement, &ctx);
  363. if (unlikely(ret))
  364. goto out_unres;
  365. convert_to_hw_box(&box, &args->box);
  366. if (!vgdev->has_virgl_3d) {
  367. virtio_gpu_cmd_transfer_to_host_2d
  368. (vgdev, qobj->hw_res_handle, offset,
  369. box.w, box.h, box.x, box.y, NULL);
  370. } else {
  371. virtio_gpu_cmd_transfer_to_host_3d
  372. (vgdev, qobj->hw_res_handle,
  373. vfpriv ? vfpriv->ctx_id : 0, offset,
  374. args->level, &box, &fence);
  375. reservation_object_add_excl_fence(qobj->tbo.resv,
  376. &fence->f);
  377. dma_fence_put(&fence->f);
  378. }
  379. out_unres:
  380. virtio_gpu_object_unreserve(qobj);
  381. out:
  382. drm_gem_object_put_unlocked(gobj);
  383. return ret;
  384. }
  385. static int virtio_gpu_wait_ioctl(struct drm_device *dev, void *data,
  386. struct drm_file *file)
  387. {
  388. struct drm_virtgpu_3d_wait *args = data;
  389. struct drm_gem_object *gobj = NULL;
  390. struct virtio_gpu_object *qobj = NULL;
  391. int ret;
  392. bool nowait = false;
  393. gobj = drm_gem_object_lookup(file, args->handle);
  394. if (gobj == NULL)
  395. return -ENOENT;
  396. qobj = gem_to_virtio_gpu_obj(gobj);
  397. if (args->flags & VIRTGPU_WAIT_NOWAIT)
  398. nowait = true;
  399. ret = virtio_gpu_object_wait(qobj, nowait);
  400. drm_gem_object_put_unlocked(gobj);
  401. return ret;
  402. }
  403. static int virtio_gpu_get_caps_ioctl(struct drm_device *dev,
  404. void *data, struct drm_file *file)
  405. {
  406. struct virtio_gpu_device *vgdev = dev->dev_private;
  407. struct drm_virtgpu_get_caps *args = data;
  408. int size;
  409. int i;
  410. int found_valid = -1;
  411. int ret;
  412. struct virtio_gpu_drv_cap_cache *cache_ent;
  413. void *ptr;
  414. if (vgdev->num_capsets == 0)
  415. return -ENOSYS;
  416. spin_lock(&vgdev->display_info_lock);
  417. for (i = 0; i < vgdev->num_capsets; i++) {
  418. if (vgdev->capsets[i].id == args->cap_set_id) {
  419. if (vgdev->capsets[i].max_version >= args->cap_set_ver) {
  420. found_valid = i;
  421. break;
  422. }
  423. }
  424. }
  425. if (found_valid == -1) {
  426. spin_unlock(&vgdev->display_info_lock);
  427. return -EINVAL;
  428. }
  429. size = vgdev->capsets[found_valid].max_size;
  430. if (args->size > size) {
  431. spin_unlock(&vgdev->display_info_lock);
  432. return -EINVAL;
  433. }
  434. list_for_each_entry(cache_ent, &vgdev->cap_cache, head) {
  435. if (cache_ent->id == args->cap_set_id &&
  436. cache_ent->version == args->cap_set_ver) {
  437. ptr = cache_ent->caps_cache;
  438. spin_unlock(&vgdev->display_info_lock);
  439. goto copy_exit;
  440. }
  441. }
  442. spin_unlock(&vgdev->display_info_lock);
  443. /* not in cache - need to talk to hw */
  444. virtio_gpu_cmd_get_capset(vgdev, found_valid, args->cap_set_ver,
  445. &cache_ent);
  446. ret = wait_event_timeout(vgdev->resp_wq,
  447. atomic_read(&cache_ent->is_valid), 5 * HZ);
  448. ptr = cache_ent->caps_cache;
  449. copy_exit:
  450. if (copy_to_user((void __user *)(unsigned long)args->addr, ptr, size))
  451. return -EFAULT;
  452. return 0;
  453. }
  454. struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS] = {
  455. DRM_IOCTL_DEF_DRV(VIRTGPU_MAP, virtio_gpu_map_ioctl,
  456. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  457. DRM_IOCTL_DEF_DRV(VIRTGPU_EXECBUFFER, virtio_gpu_execbuffer_ioctl,
  458. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  459. DRM_IOCTL_DEF_DRV(VIRTGPU_GETPARAM, virtio_gpu_getparam_ioctl,
  460. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  461. DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_CREATE,
  462. virtio_gpu_resource_create_ioctl,
  463. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  464. DRM_IOCTL_DEF_DRV(VIRTGPU_RESOURCE_INFO, virtio_gpu_resource_info_ioctl,
  465. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  466. /* make transfer async to the main ring? - no sure, can we
  467. thread these in the underlying GL */
  468. DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_FROM_HOST,
  469. virtio_gpu_transfer_from_host_ioctl,
  470. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  471. DRM_IOCTL_DEF_DRV(VIRTGPU_TRANSFER_TO_HOST,
  472. virtio_gpu_transfer_to_host_ioctl,
  473. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  474. DRM_IOCTL_DEF_DRV(VIRTGPU_WAIT, virtio_gpu_wait_ioctl,
  475. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  476. DRM_IOCTL_DEF_DRV(VIRTGPU_GET_CAPS, virtio_gpu_get_caps_ioctl,
  477. DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  478. };