vmwgfx_execbuf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "vmwgfx_reg.h"
  29. #include "ttm/ttm_bo_api.h"
  30. #include "ttm/ttm_placement.h"
  31. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  32. struct vmw_sw_context *sw_context,
  33. SVGA3dCmdHeader *header)
  34. {
  35. return capable(CAP_SYS_ADMIN) ? : -EINVAL;
  36. }
  37. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  38. struct vmw_sw_context *sw_context,
  39. SVGA3dCmdHeader *header)
  40. {
  41. return 0;
  42. }
  43. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  44. struct vmw_sw_context *sw_context,
  45. SVGA3dCmdHeader *header)
  46. {
  47. struct vmw_cid_cmd {
  48. SVGA3dCmdHeader header;
  49. __le32 cid;
  50. } *cmd;
  51. int ret;
  52. cmd = container_of(header, struct vmw_cid_cmd, header);
  53. if (likely(sw_context->cid_valid && cmd->cid == sw_context->last_cid))
  54. return 0;
  55. ret = vmw_context_check(dev_priv, sw_context->tfile, cmd->cid);
  56. if (unlikely(ret != 0)) {
  57. DRM_ERROR("Could not find or use context %u\n",
  58. (unsigned) cmd->cid);
  59. return ret;
  60. }
  61. sw_context->last_cid = cmd->cid;
  62. sw_context->cid_valid = true;
  63. return 0;
  64. }
  65. static int vmw_cmd_sid_check(struct vmw_private *dev_priv,
  66. struct vmw_sw_context *sw_context,
  67. uint32_t sid)
  68. {
  69. if (unlikely((!sw_context->sid_valid || sid != sw_context->last_sid) &&
  70. sid != SVGA3D_INVALID_ID)) {
  71. int ret = vmw_surface_check(dev_priv, sw_context->tfile, sid);
  72. if (unlikely(ret != 0)) {
  73. DRM_ERROR("Could ot find or use surface %u\n",
  74. (unsigned) sid);
  75. return ret;
  76. }
  77. sw_context->last_sid = sid;
  78. sw_context->sid_valid = true;
  79. }
  80. return 0;
  81. }
  82. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  83. struct vmw_sw_context *sw_context,
  84. SVGA3dCmdHeader *header)
  85. {
  86. struct vmw_sid_cmd {
  87. SVGA3dCmdHeader header;
  88. SVGA3dCmdSetRenderTarget body;
  89. } *cmd;
  90. int ret;
  91. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  92. if (unlikely(ret != 0))
  93. return ret;
  94. cmd = container_of(header, struct vmw_sid_cmd, header);
  95. return vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.target.sid);
  96. }
  97. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  98. struct vmw_sw_context *sw_context,
  99. SVGA3dCmdHeader *header)
  100. {
  101. struct vmw_sid_cmd {
  102. SVGA3dCmdHeader header;
  103. SVGA3dCmdSurfaceCopy body;
  104. } *cmd;
  105. int ret;
  106. cmd = container_of(header, struct vmw_sid_cmd, header);
  107. ret = vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.src.sid);
  108. if (unlikely(ret != 0))
  109. return ret;
  110. return vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.dest.sid);
  111. }
  112. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  113. struct vmw_sw_context *sw_context,
  114. SVGA3dCmdHeader *header)
  115. {
  116. struct vmw_sid_cmd {
  117. SVGA3dCmdHeader header;
  118. SVGA3dCmdSurfaceStretchBlt body;
  119. } *cmd;
  120. int ret;
  121. cmd = container_of(header, struct vmw_sid_cmd, header);
  122. ret = vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.src.sid);
  123. if (unlikely(ret != 0))
  124. return ret;
  125. return vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.dest.sid);
  126. }
  127. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  128. struct vmw_sw_context *sw_context,
  129. SVGA3dCmdHeader *header)
  130. {
  131. struct vmw_sid_cmd {
  132. SVGA3dCmdHeader header;
  133. SVGA3dCmdBlitSurfaceToScreen body;
  134. } *cmd;
  135. cmd = container_of(header, struct vmw_sid_cmd, header);
  136. return vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.srcImage.sid);
  137. }
  138. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  139. struct vmw_sw_context *sw_context,
  140. SVGA3dCmdHeader *header)
  141. {
  142. struct vmw_sid_cmd {
  143. SVGA3dCmdHeader header;
  144. SVGA3dCmdPresent body;
  145. } *cmd;
  146. cmd = container_of(header, struct vmw_sid_cmd, header);
  147. return vmw_cmd_sid_check(dev_priv, sw_context, cmd->body.sid);
  148. }
  149. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  150. struct vmw_sw_context *sw_context,
  151. SVGA3dCmdHeader *header)
  152. {
  153. uint32_t handle;
  154. struct vmw_dma_buffer *vmw_bo = NULL;
  155. struct ttm_buffer_object *bo;
  156. struct vmw_surface *srf = NULL;
  157. struct vmw_dma_cmd {
  158. SVGA3dCmdHeader header;
  159. SVGA3dCmdSurfaceDMA dma;
  160. } *cmd;
  161. struct vmw_relocation *reloc;
  162. int ret;
  163. uint32_t cur_validate_node;
  164. struct ttm_validate_buffer *val_buf;
  165. cmd = container_of(header, struct vmw_dma_cmd, header);
  166. ret = vmw_cmd_sid_check(dev_priv, sw_context, cmd->dma.host.sid);
  167. if (unlikely(ret != 0))
  168. return ret;
  169. handle = cmd->dma.guest.ptr.gmrId;
  170. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  171. if (unlikely(ret != 0)) {
  172. DRM_ERROR("Could not find or use GMR region.\n");
  173. return -EINVAL;
  174. }
  175. bo = &vmw_bo->base;
  176. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  177. DRM_ERROR("Max number of DMA commands per submission"
  178. " exceeded\n");
  179. ret = -EINVAL;
  180. goto out_no_reloc;
  181. }
  182. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  183. reloc->location = &cmd->dma.guest.ptr;
  184. cur_validate_node = vmw_dmabuf_validate_node(bo, sw_context->cur_val_buf);
  185. if (unlikely(cur_validate_node >= VMWGFX_MAX_GMRS)) {
  186. DRM_ERROR("Max number of DMA buffers per submission"
  187. " exceeded.\n");
  188. ret = -EINVAL;
  189. goto out_no_reloc;
  190. }
  191. reloc->index = cur_validate_node;
  192. if (unlikely(cur_validate_node == sw_context->cur_val_buf)) {
  193. val_buf = &sw_context->val_bufs[cur_validate_node];
  194. val_buf->bo = ttm_bo_reference(bo);
  195. val_buf->new_sync_obj_arg = (void *) dev_priv;
  196. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  197. ++sw_context->cur_val_buf;
  198. }
  199. ret = vmw_user_surface_lookup(dev_priv, sw_context->tfile,
  200. cmd->dma.host.sid, &srf);
  201. if (ret) {
  202. DRM_ERROR("could not find surface\n");
  203. goto out_no_reloc;
  204. }
  205. vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
  206. vmw_surface_unreference(&srf);
  207. out_no_reloc:
  208. vmw_dmabuf_unreference(&vmw_bo);
  209. return ret;
  210. }
  211. typedef int (*vmw_cmd_func) (struct vmw_private *,
  212. struct vmw_sw_context *,
  213. SVGA3dCmdHeader *);
  214. #define VMW_CMD_DEF(cmd, func) \
  215. [cmd - SVGA_3D_CMD_BASE] = func
  216. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  217. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  218. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  219. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  220. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  221. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  222. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  223. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  224. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  225. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  226. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  227. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  228. &vmw_cmd_set_render_target_check),
  229. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_cid_check),
  230. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  231. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  232. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  233. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  234. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  235. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  236. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  237. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  238. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  239. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
  240. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  241. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_cid_check),
  242. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  243. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
  244. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_cid_check),
  245. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_cid_check),
  246. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  247. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  248. &vmw_cmd_blt_surf_screen_check)
  249. };
  250. static int vmw_cmd_check(struct vmw_private *dev_priv,
  251. struct vmw_sw_context *sw_context,
  252. void *buf, uint32_t *size)
  253. {
  254. uint32_t cmd_id;
  255. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  256. int ret;
  257. cmd_id = ((uint32_t *)buf)[0];
  258. if (cmd_id == SVGA_CMD_UPDATE) {
  259. *size = 5 << 2;
  260. return 0;
  261. }
  262. cmd_id = le32_to_cpu(header->id);
  263. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  264. cmd_id -= SVGA_3D_CMD_BASE;
  265. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  266. goto out_err;
  267. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  268. if (unlikely(ret != 0))
  269. goto out_err;
  270. return 0;
  271. out_err:
  272. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  273. cmd_id + SVGA_3D_CMD_BASE);
  274. return -EINVAL;
  275. }
  276. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  277. struct vmw_sw_context *sw_context,
  278. void *buf, uint32_t size)
  279. {
  280. int32_t cur_size = size;
  281. int ret;
  282. while (cur_size > 0) {
  283. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  284. if (unlikely(ret != 0))
  285. return ret;
  286. buf = (void *)((unsigned long) buf + size);
  287. cur_size -= size;
  288. }
  289. if (unlikely(cur_size != 0)) {
  290. DRM_ERROR("Command verifier out of sync.\n");
  291. return -EINVAL;
  292. }
  293. return 0;
  294. }
  295. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  296. {
  297. sw_context->cur_reloc = 0;
  298. }
  299. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  300. {
  301. uint32_t i;
  302. struct vmw_relocation *reloc;
  303. struct ttm_validate_buffer *validate;
  304. struct ttm_buffer_object *bo;
  305. for (i = 0; i < sw_context->cur_reloc; ++i) {
  306. reloc = &sw_context->relocs[i];
  307. validate = &sw_context->val_bufs[reloc->index];
  308. bo = validate->bo;
  309. reloc->location->offset += bo->offset;
  310. reloc->location->gmrId = vmw_dmabuf_gmr(bo);
  311. }
  312. vmw_free_relocations(sw_context);
  313. }
  314. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  315. {
  316. struct ttm_validate_buffer *entry, *next;
  317. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  318. head) {
  319. list_del(&entry->head);
  320. vmw_dmabuf_validate_clear(entry->bo);
  321. ttm_bo_unref(&entry->bo);
  322. sw_context->cur_val_buf--;
  323. }
  324. BUG_ON(sw_context->cur_val_buf != 0);
  325. }
  326. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  327. struct ttm_buffer_object *bo)
  328. {
  329. int ret;
  330. if (vmw_dmabuf_gmr(bo) != SVGA_GMR_NULL)
  331. return 0;
  332. ret = vmw_gmr_bind(dev_priv, bo);
  333. if (likely(ret == 0 || ret == -ERESTART))
  334. return ret;
  335. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
  336. return ret;
  337. }
  338. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  339. struct vmw_sw_context *sw_context)
  340. {
  341. struct ttm_validate_buffer *entry;
  342. int ret;
  343. list_for_each_entry(entry, &sw_context->validate_nodes, head) {
  344. ret = vmw_validate_single_buffer(dev_priv, entry->bo);
  345. if (unlikely(ret != 0))
  346. return ret;
  347. }
  348. return 0;
  349. }
  350. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  351. struct drm_file *file_priv)
  352. {
  353. struct vmw_private *dev_priv = vmw_priv(dev);
  354. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  355. struct drm_vmw_fence_rep fence_rep;
  356. struct drm_vmw_fence_rep __user *user_fence_rep;
  357. int ret;
  358. void *user_cmd;
  359. void *cmd;
  360. uint32_t sequence;
  361. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  362. struct vmw_master *vmaster = vmw_master(file_priv->master);
  363. ret = ttm_read_lock(&vmaster->lock, true);
  364. if (unlikely(ret != 0))
  365. return ret;
  366. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  367. if (unlikely(ret != 0)) {
  368. ret = -ERESTART;
  369. goto out_no_cmd_mutex;
  370. }
  371. cmd = vmw_fifo_reserve(dev_priv, arg->command_size);
  372. if (unlikely(cmd == NULL)) {
  373. DRM_ERROR("Failed reserving fifo space for commands.\n");
  374. ret = -ENOMEM;
  375. goto out_unlock;
  376. }
  377. user_cmd = (void __user *)(unsigned long)arg->commands;
  378. ret = copy_from_user(cmd, user_cmd, arg->command_size);
  379. if (unlikely(ret != 0)) {
  380. DRM_ERROR("Failed copying commands.\n");
  381. goto out_commit;
  382. }
  383. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  384. sw_context->cid_valid = false;
  385. sw_context->sid_valid = false;
  386. sw_context->cur_reloc = 0;
  387. sw_context->cur_val_buf = 0;
  388. INIT_LIST_HEAD(&sw_context->validate_nodes);
  389. ret = vmw_cmd_check_all(dev_priv, sw_context, cmd, arg->command_size);
  390. if (unlikely(ret != 0))
  391. goto out_err;
  392. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes,
  393. dev_priv->val_seq++);
  394. if (unlikely(ret != 0))
  395. goto out_err;
  396. ret = vmw_validate_buffers(dev_priv, sw_context);
  397. if (unlikely(ret != 0))
  398. goto out_err;
  399. vmw_apply_relocations(sw_context);
  400. vmw_fifo_commit(dev_priv, arg->command_size);
  401. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  402. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  403. (void *)(unsigned long) sequence);
  404. vmw_clear_validations(sw_context);
  405. mutex_unlock(&dev_priv->cmdbuf_mutex);
  406. /*
  407. * This error is harmless, because if fence submission fails,
  408. * vmw_fifo_send_fence will sync.
  409. */
  410. if (ret != 0)
  411. DRM_ERROR("Fence submission error. Syncing.\n");
  412. fence_rep.error = ret;
  413. fence_rep.fence_seq = (uint64_t) sequence;
  414. user_fence_rep = (struct drm_vmw_fence_rep __user *)
  415. (unsigned long)arg->fence_rep;
  416. /*
  417. * copy_to_user errors will be detected by user space not
  418. * seeing fence_rep::error filled in.
  419. */
  420. ret = copy_to_user(user_fence_rep, &fence_rep, sizeof(fence_rep));
  421. vmw_kms_cursor_post_execbuf(dev_priv);
  422. ttm_read_unlock(&vmaster->lock);
  423. return 0;
  424. out_err:
  425. vmw_free_relocations(sw_context);
  426. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  427. vmw_clear_validations(sw_context);
  428. out_commit:
  429. vmw_fifo_commit(dev_priv, 0);
  430. out_unlock:
  431. mutex_unlock(&dev_priv->cmdbuf_mutex);
  432. out_no_cmd_mutex:
  433. ttm_read_unlock(&vmaster->lock);
  434. return ret;
  435. }