virtgpu_ioctl.c 15 KB

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