vmwgfx_execbuf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 (*sid == SVGA3D_INVALID_ID)
  70. return 0;
  71. if (unlikely((!sw_context->sid_valid ||
  72. *sid != sw_context->last_sid))) {
  73. int real_id;
  74. int ret = vmw_surface_check(dev_priv, sw_context->tfile,
  75. *sid, &real_id);
  76. if (unlikely(ret != 0)) {
  77. DRM_ERROR("Could ot find or use surface 0x%08x "
  78. "address 0x%08lx\n",
  79. (unsigned int) *sid,
  80. (unsigned long) sid);
  81. return ret;
  82. }
  83. sw_context->last_sid = *sid;
  84. sw_context->sid_valid = true;
  85. *sid = real_id;
  86. sw_context->sid_translation = real_id;
  87. } else
  88. *sid = sw_context->sid_translation;
  89. return 0;
  90. }
  91. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  92. struct vmw_sw_context *sw_context,
  93. SVGA3dCmdHeader *header)
  94. {
  95. struct vmw_sid_cmd {
  96. SVGA3dCmdHeader header;
  97. SVGA3dCmdSetRenderTarget body;
  98. } *cmd;
  99. int ret;
  100. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  101. if (unlikely(ret != 0))
  102. return ret;
  103. cmd = container_of(header, struct vmw_sid_cmd, header);
  104. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.target.sid);
  105. return ret;
  106. }
  107. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  108. struct vmw_sw_context *sw_context,
  109. SVGA3dCmdHeader *header)
  110. {
  111. struct vmw_sid_cmd {
  112. SVGA3dCmdHeader header;
  113. SVGA3dCmdSurfaceCopy body;
  114. } *cmd;
  115. int ret;
  116. cmd = container_of(header, struct vmw_sid_cmd, header);
  117. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  118. if (unlikely(ret != 0))
  119. return ret;
  120. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  121. }
  122. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  123. struct vmw_sw_context *sw_context,
  124. SVGA3dCmdHeader *header)
  125. {
  126. struct vmw_sid_cmd {
  127. SVGA3dCmdHeader header;
  128. SVGA3dCmdSurfaceStretchBlt body;
  129. } *cmd;
  130. int ret;
  131. cmd = container_of(header, struct vmw_sid_cmd, header);
  132. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  133. if (unlikely(ret != 0))
  134. return ret;
  135. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  136. }
  137. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  138. struct vmw_sw_context *sw_context,
  139. SVGA3dCmdHeader *header)
  140. {
  141. struct vmw_sid_cmd {
  142. SVGA3dCmdHeader header;
  143. SVGA3dCmdBlitSurfaceToScreen body;
  144. } *cmd;
  145. cmd = container_of(header, struct vmw_sid_cmd, header);
  146. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.srcImage.sid);
  147. }
  148. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  149. struct vmw_sw_context *sw_context,
  150. SVGA3dCmdHeader *header)
  151. {
  152. struct vmw_sid_cmd {
  153. SVGA3dCmdHeader header;
  154. SVGA3dCmdPresent body;
  155. } *cmd;
  156. cmd = container_of(header, struct vmw_sid_cmd, header);
  157. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.sid);
  158. }
  159. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  160. struct vmw_sw_context *sw_context,
  161. SVGA3dCmdHeader *header)
  162. {
  163. uint32_t handle;
  164. struct vmw_dma_buffer *vmw_bo = NULL;
  165. struct ttm_buffer_object *bo;
  166. struct vmw_surface *srf = NULL;
  167. struct vmw_dma_cmd {
  168. SVGA3dCmdHeader header;
  169. SVGA3dCmdSurfaceDMA dma;
  170. } *cmd;
  171. struct vmw_relocation *reloc;
  172. int ret;
  173. uint32_t cur_validate_node;
  174. struct ttm_validate_buffer *val_buf;
  175. cmd = container_of(header, struct vmw_dma_cmd, header);
  176. handle = cmd->dma.guest.ptr.gmrId;
  177. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  178. if (unlikely(ret != 0)) {
  179. DRM_ERROR("Could not find or use GMR region.\n");
  180. return -EINVAL;
  181. }
  182. bo = &vmw_bo->base;
  183. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  184. DRM_ERROR("Max number of DMA commands per submission"
  185. " exceeded\n");
  186. ret = -EINVAL;
  187. goto out_no_reloc;
  188. }
  189. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  190. reloc->location = &cmd->dma.guest.ptr;
  191. cur_validate_node = vmw_dmabuf_validate_node(bo, sw_context->cur_val_buf);
  192. if (unlikely(cur_validate_node >= VMWGFX_MAX_GMRS)) {
  193. DRM_ERROR("Max number of DMA buffers per submission"
  194. " exceeded.\n");
  195. ret = -EINVAL;
  196. goto out_no_reloc;
  197. }
  198. reloc->index = cur_validate_node;
  199. if (unlikely(cur_validate_node == sw_context->cur_val_buf)) {
  200. val_buf = &sw_context->val_bufs[cur_validate_node];
  201. val_buf->bo = ttm_bo_reference(bo);
  202. val_buf->new_sync_obj_arg = (void *) dev_priv;
  203. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  204. ++sw_context->cur_val_buf;
  205. }
  206. ret = vmw_user_surface_lookup_handle(dev_priv, sw_context->tfile,
  207. cmd->dma.host.sid, &srf);
  208. if (ret) {
  209. DRM_ERROR("could not find surface\n");
  210. goto out_no_reloc;
  211. }
  212. /**
  213. * Patch command stream with device SID.
  214. */
  215. cmd->dma.host.sid = srf->res.id;
  216. vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
  217. /**
  218. * FIXME: May deadlock here when called from the
  219. * command parsing code.
  220. */
  221. vmw_surface_unreference(&srf);
  222. out_no_reloc:
  223. vmw_dmabuf_unreference(&vmw_bo);
  224. return ret;
  225. }
  226. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  227. struct vmw_sw_context *sw_context,
  228. SVGA3dCmdHeader *header)
  229. {
  230. struct vmw_draw_cmd {
  231. SVGA3dCmdHeader header;
  232. SVGA3dCmdDrawPrimitives body;
  233. } *cmd;
  234. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  235. (unsigned long)header + sizeof(*cmd));
  236. SVGA3dPrimitiveRange *range;
  237. uint32_t i;
  238. uint32_t maxnum;
  239. int ret;
  240. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  241. if (unlikely(ret != 0))
  242. return ret;
  243. cmd = container_of(header, struct vmw_draw_cmd, header);
  244. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  245. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  246. DRM_ERROR("Illegal number of vertex declarations.\n");
  247. return -EINVAL;
  248. }
  249. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  250. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  251. &decl->array.surfaceId);
  252. if (unlikely(ret != 0))
  253. return ret;
  254. }
  255. maxnum = (header->size - sizeof(cmd->body) -
  256. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  257. if (unlikely(cmd->body.numRanges > maxnum)) {
  258. DRM_ERROR("Illegal number of index ranges.\n");
  259. return -EINVAL;
  260. }
  261. range = (SVGA3dPrimitiveRange *) decl;
  262. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  263. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  264. &range->indexArray.surfaceId);
  265. if (unlikely(ret != 0))
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  271. struct vmw_sw_context *sw_context,
  272. SVGA3dCmdHeader *header)
  273. {
  274. struct vmw_tex_state_cmd {
  275. SVGA3dCmdHeader header;
  276. SVGA3dCmdSetTextureState state;
  277. };
  278. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  279. ((unsigned long) header + header->size + sizeof(header));
  280. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  281. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  282. int ret;
  283. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  284. if (unlikely(ret != 0))
  285. return ret;
  286. for (; cur_state < last_state; ++cur_state) {
  287. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  288. continue;
  289. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  290. &cur_state->value);
  291. if (unlikely(ret != 0))
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. typedef int (*vmw_cmd_func) (struct vmw_private *,
  297. struct vmw_sw_context *,
  298. SVGA3dCmdHeader *);
  299. #define VMW_CMD_DEF(cmd, func) \
  300. [cmd - SVGA_3D_CMD_BASE] = func
  301. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  302. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  303. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  304. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  305. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  306. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  307. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  308. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  309. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  310. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  311. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  312. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  313. &vmw_cmd_set_render_target_check),
  314. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  315. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  316. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  317. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  318. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  319. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  320. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  321. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  322. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  323. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  324. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
  325. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  326. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  327. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  328. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
  329. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_cid_check),
  330. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_cid_check),
  331. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  332. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  333. &vmw_cmd_blt_surf_screen_check)
  334. };
  335. static int vmw_cmd_check(struct vmw_private *dev_priv,
  336. struct vmw_sw_context *sw_context,
  337. void *buf, uint32_t *size)
  338. {
  339. uint32_t cmd_id;
  340. uint32_t size_remaining = *size;
  341. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  342. int ret;
  343. cmd_id = ((uint32_t *)buf)[0];
  344. if (cmd_id == SVGA_CMD_UPDATE) {
  345. *size = 5 << 2;
  346. return 0;
  347. }
  348. cmd_id = le32_to_cpu(header->id);
  349. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  350. cmd_id -= SVGA_3D_CMD_BASE;
  351. if (unlikely(*size > size_remaining))
  352. goto out_err;
  353. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  354. goto out_err;
  355. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  356. if (unlikely(ret != 0))
  357. goto out_err;
  358. return 0;
  359. out_err:
  360. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  361. cmd_id + SVGA_3D_CMD_BASE);
  362. return -EINVAL;
  363. }
  364. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  365. struct vmw_sw_context *sw_context,
  366. void *buf, uint32_t size)
  367. {
  368. int32_t cur_size = size;
  369. int ret;
  370. while (cur_size > 0) {
  371. size = cur_size;
  372. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  373. if (unlikely(ret != 0))
  374. return ret;
  375. buf = (void *)((unsigned long) buf + size);
  376. cur_size -= size;
  377. }
  378. if (unlikely(cur_size != 0)) {
  379. DRM_ERROR("Command verifier out of sync.\n");
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  385. {
  386. sw_context->cur_reloc = 0;
  387. }
  388. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  389. {
  390. uint32_t i;
  391. struct vmw_relocation *reloc;
  392. struct ttm_validate_buffer *validate;
  393. struct ttm_buffer_object *bo;
  394. for (i = 0; i < sw_context->cur_reloc; ++i) {
  395. reloc = &sw_context->relocs[i];
  396. validate = &sw_context->val_bufs[reloc->index];
  397. bo = validate->bo;
  398. reloc->location->offset += bo->offset;
  399. reloc->location->gmrId = vmw_dmabuf_gmr(bo);
  400. }
  401. vmw_free_relocations(sw_context);
  402. }
  403. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  404. {
  405. struct ttm_validate_buffer *entry, *next;
  406. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  407. head) {
  408. list_del(&entry->head);
  409. vmw_dmabuf_validate_clear(entry->bo);
  410. ttm_bo_unref(&entry->bo);
  411. sw_context->cur_val_buf--;
  412. }
  413. BUG_ON(sw_context->cur_val_buf != 0);
  414. }
  415. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  416. struct ttm_buffer_object *bo)
  417. {
  418. int ret;
  419. if (vmw_dmabuf_gmr(bo) != SVGA_GMR_NULL)
  420. return 0;
  421. ret = vmw_gmr_bind(dev_priv, bo);
  422. if (likely(ret == 0 || ret == -ERESTARTSYS))
  423. return ret;
  424. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
  425. return ret;
  426. }
  427. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  428. struct vmw_sw_context *sw_context)
  429. {
  430. struct ttm_validate_buffer *entry;
  431. int ret;
  432. list_for_each_entry(entry, &sw_context->validate_nodes, head) {
  433. ret = vmw_validate_single_buffer(dev_priv, entry->bo);
  434. if (unlikely(ret != 0))
  435. return ret;
  436. }
  437. return 0;
  438. }
  439. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  440. struct drm_file *file_priv)
  441. {
  442. struct vmw_private *dev_priv = vmw_priv(dev);
  443. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  444. struct drm_vmw_fence_rep fence_rep;
  445. struct drm_vmw_fence_rep __user *user_fence_rep;
  446. int ret;
  447. void *user_cmd;
  448. void *cmd;
  449. uint32_t sequence;
  450. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  451. struct vmw_master *vmaster = vmw_master(file_priv->master);
  452. ret = ttm_read_lock(&vmaster->lock, true);
  453. if (unlikely(ret != 0))
  454. return ret;
  455. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  456. if (unlikely(ret != 0)) {
  457. ret = -ERESTARTSYS;
  458. goto out_no_cmd_mutex;
  459. }
  460. cmd = vmw_fifo_reserve(dev_priv, arg->command_size);
  461. if (unlikely(cmd == NULL)) {
  462. DRM_ERROR("Failed reserving fifo space for commands.\n");
  463. ret = -ENOMEM;
  464. goto out_unlock;
  465. }
  466. user_cmd = (void __user *)(unsigned long)arg->commands;
  467. ret = copy_from_user(cmd, user_cmd, arg->command_size);
  468. if (unlikely(ret != 0)) {
  469. DRM_ERROR("Failed copying commands.\n");
  470. goto out_commit;
  471. }
  472. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  473. sw_context->cid_valid = false;
  474. sw_context->sid_valid = false;
  475. sw_context->cur_reloc = 0;
  476. sw_context->cur_val_buf = 0;
  477. INIT_LIST_HEAD(&sw_context->validate_nodes);
  478. ret = vmw_cmd_check_all(dev_priv, sw_context, cmd, arg->command_size);
  479. if (unlikely(ret != 0))
  480. goto out_err;
  481. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes,
  482. dev_priv->val_seq++);
  483. if (unlikely(ret != 0))
  484. goto out_err;
  485. ret = vmw_validate_buffers(dev_priv, sw_context);
  486. if (unlikely(ret != 0))
  487. goto out_err;
  488. vmw_apply_relocations(sw_context);
  489. vmw_fifo_commit(dev_priv, arg->command_size);
  490. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  491. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  492. (void *)(unsigned long) sequence);
  493. vmw_clear_validations(sw_context);
  494. mutex_unlock(&dev_priv->cmdbuf_mutex);
  495. /*
  496. * This error is harmless, because if fence submission fails,
  497. * vmw_fifo_send_fence will sync.
  498. */
  499. if (ret != 0)
  500. DRM_ERROR("Fence submission error. Syncing.\n");
  501. fence_rep.error = ret;
  502. fence_rep.fence_seq = (uint64_t) sequence;
  503. user_fence_rep = (struct drm_vmw_fence_rep __user *)
  504. (unsigned long)arg->fence_rep;
  505. /*
  506. * copy_to_user errors will be detected by user space not
  507. * seeing fence_rep::error filled in.
  508. */
  509. ret = copy_to_user(user_fence_rep, &fence_rep, sizeof(fence_rep));
  510. vmw_kms_cursor_post_execbuf(dev_priv);
  511. ttm_read_unlock(&vmaster->lock);
  512. return 0;
  513. out_err:
  514. vmw_free_relocations(sw_context);
  515. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  516. vmw_clear_validations(sw_context);
  517. out_commit:
  518. vmw_fifo_commit(dev_priv, 0);
  519. out_unlock:
  520. mutex_unlock(&dev_priv->cmdbuf_mutex);
  521. out_no_cmd_mutex:
  522. ttm_read_unlock(&vmaster->lock);
  523. return ret;
  524. }