vmwgfx_execbuf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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_translate_guest_ptr(struct vmw_private *dev_priv,
  160. struct vmw_sw_context *sw_context,
  161. SVGAGuestPtr *ptr,
  162. struct vmw_dma_buffer **vmw_bo_p)
  163. {
  164. struct vmw_dma_buffer *vmw_bo = NULL;
  165. struct ttm_buffer_object *bo;
  166. uint32_t handle = ptr->gmrId;
  167. struct vmw_relocation *reloc;
  168. uint32_t cur_validate_node;
  169. struct ttm_validate_buffer *val_buf;
  170. int ret;
  171. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  172. if (unlikely(ret != 0)) {
  173. DRM_ERROR("Could not find or use GMR region.\n");
  174. return -EINVAL;
  175. }
  176. bo = &vmw_bo->base;
  177. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  178. DRM_ERROR("Max number relocations per submission"
  179. " exceeded\n");
  180. ret = -EINVAL;
  181. goto out_no_reloc;
  182. }
  183. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  184. reloc->location = ptr;
  185. cur_validate_node = vmw_dmabuf_validate_node(bo, sw_context->cur_val_buf);
  186. if (unlikely(cur_validate_node >= VMWGFX_MAX_GMRS)) {
  187. DRM_ERROR("Max number of DMA buffers per submission"
  188. " exceeded.\n");
  189. ret = -EINVAL;
  190. goto out_no_reloc;
  191. }
  192. reloc->index = cur_validate_node;
  193. if (unlikely(cur_validate_node == sw_context->cur_val_buf)) {
  194. val_buf = &sw_context->val_bufs[cur_validate_node];
  195. val_buf->bo = ttm_bo_reference(bo);
  196. val_buf->usage = TTM_USAGE_READWRITE;
  197. val_buf->new_sync_obj_arg = (void *) dev_priv;
  198. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  199. ++sw_context->cur_val_buf;
  200. }
  201. *vmw_bo_p = vmw_bo;
  202. return 0;
  203. out_no_reloc:
  204. vmw_dmabuf_unreference(&vmw_bo);
  205. vmw_bo_p = NULL;
  206. return ret;
  207. }
  208. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  209. struct vmw_sw_context *sw_context,
  210. SVGA3dCmdHeader *header)
  211. {
  212. struct vmw_dma_buffer *vmw_bo;
  213. struct vmw_query_cmd {
  214. SVGA3dCmdHeader header;
  215. SVGA3dCmdEndQuery q;
  216. } *cmd;
  217. int ret;
  218. cmd = container_of(header, struct vmw_query_cmd, header);
  219. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  220. if (unlikely(ret != 0))
  221. return ret;
  222. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  223. &cmd->q.guestResult,
  224. &vmw_bo);
  225. if (unlikely(ret != 0))
  226. return ret;
  227. vmw_dmabuf_unreference(&vmw_bo);
  228. return 0;
  229. }
  230. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  231. struct vmw_sw_context *sw_context,
  232. SVGA3dCmdHeader *header)
  233. {
  234. struct vmw_dma_buffer *vmw_bo;
  235. struct vmw_query_cmd {
  236. SVGA3dCmdHeader header;
  237. SVGA3dCmdWaitForQuery q;
  238. } *cmd;
  239. int ret;
  240. cmd = container_of(header, struct vmw_query_cmd, header);
  241. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  242. if (unlikely(ret != 0))
  243. return ret;
  244. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  245. &cmd->q.guestResult,
  246. &vmw_bo);
  247. if (unlikely(ret != 0))
  248. return ret;
  249. vmw_dmabuf_unreference(&vmw_bo);
  250. return 0;
  251. }
  252. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  253. struct vmw_sw_context *sw_context,
  254. SVGA3dCmdHeader *header)
  255. {
  256. struct vmw_dma_buffer *vmw_bo = NULL;
  257. struct ttm_buffer_object *bo;
  258. struct vmw_surface *srf = NULL;
  259. struct vmw_dma_cmd {
  260. SVGA3dCmdHeader header;
  261. SVGA3dCmdSurfaceDMA dma;
  262. } *cmd;
  263. int ret;
  264. cmd = container_of(header, struct vmw_dma_cmd, header);
  265. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  266. &cmd->dma.guest.ptr,
  267. &vmw_bo);
  268. if (unlikely(ret != 0))
  269. return ret;
  270. bo = &vmw_bo->base;
  271. ret = vmw_user_surface_lookup_handle(dev_priv, sw_context->tfile,
  272. cmd->dma.host.sid, &srf);
  273. if (ret) {
  274. DRM_ERROR("could not find surface\n");
  275. goto out_no_reloc;
  276. }
  277. /**
  278. * Patch command stream with device SID.
  279. */
  280. cmd->dma.host.sid = srf->res.id;
  281. vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
  282. /**
  283. * FIXME: May deadlock here when called from the
  284. * command parsing code.
  285. */
  286. vmw_surface_unreference(&srf);
  287. out_no_reloc:
  288. vmw_dmabuf_unreference(&vmw_bo);
  289. return ret;
  290. }
  291. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  292. struct vmw_sw_context *sw_context,
  293. SVGA3dCmdHeader *header)
  294. {
  295. struct vmw_draw_cmd {
  296. SVGA3dCmdHeader header;
  297. SVGA3dCmdDrawPrimitives body;
  298. } *cmd;
  299. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  300. (unsigned long)header + sizeof(*cmd));
  301. SVGA3dPrimitiveRange *range;
  302. uint32_t i;
  303. uint32_t maxnum;
  304. int ret;
  305. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  306. if (unlikely(ret != 0))
  307. return ret;
  308. cmd = container_of(header, struct vmw_draw_cmd, header);
  309. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  310. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  311. DRM_ERROR("Illegal number of vertex declarations.\n");
  312. return -EINVAL;
  313. }
  314. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  315. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  316. &decl->array.surfaceId);
  317. if (unlikely(ret != 0))
  318. return ret;
  319. }
  320. maxnum = (header->size - sizeof(cmd->body) -
  321. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  322. if (unlikely(cmd->body.numRanges > maxnum)) {
  323. DRM_ERROR("Illegal number of index ranges.\n");
  324. return -EINVAL;
  325. }
  326. range = (SVGA3dPrimitiveRange *) decl;
  327. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  328. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  329. &range->indexArray.surfaceId);
  330. if (unlikely(ret != 0))
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  336. struct vmw_sw_context *sw_context,
  337. SVGA3dCmdHeader *header)
  338. {
  339. struct vmw_tex_state_cmd {
  340. SVGA3dCmdHeader header;
  341. SVGA3dCmdSetTextureState state;
  342. };
  343. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  344. ((unsigned long) header + header->size + sizeof(header));
  345. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  346. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  347. int ret;
  348. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  349. if (unlikely(ret != 0))
  350. return ret;
  351. for (; cur_state < last_state; ++cur_state) {
  352. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  353. continue;
  354. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  355. &cur_state->value);
  356. if (unlikely(ret != 0))
  357. return ret;
  358. }
  359. return 0;
  360. }
  361. typedef int (*vmw_cmd_func) (struct vmw_private *,
  362. struct vmw_sw_context *,
  363. SVGA3dCmdHeader *);
  364. #define VMW_CMD_DEF(cmd, func) \
  365. [cmd - SVGA_3D_CMD_BASE] = func
  366. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  367. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  368. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  369. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  370. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  371. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  372. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  373. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  374. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  375. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  376. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  377. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  378. &vmw_cmd_set_render_target_check),
  379. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  380. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  381. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  382. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  383. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  384. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  385. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  386. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  387. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  388. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  389. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
  390. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  391. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  392. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  393. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
  394. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
  395. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
  396. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  397. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  398. &vmw_cmd_blt_surf_screen_check)
  399. };
  400. static int vmw_cmd_check(struct vmw_private *dev_priv,
  401. struct vmw_sw_context *sw_context,
  402. void *buf, uint32_t *size)
  403. {
  404. uint32_t cmd_id;
  405. uint32_t size_remaining = *size;
  406. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  407. int ret;
  408. cmd_id = ((uint32_t *)buf)[0];
  409. if (cmd_id == SVGA_CMD_UPDATE) {
  410. *size = 5 << 2;
  411. return 0;
  412. }
  413. cmd_id = le32_to_cpu(header->id);
  414. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  415. cmd_id -= SVGA_3D_CMD_BASE;
  416. if (unlikely(*size > size_remaining))
  417. goto out_err;
  418. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  419. goto out_err;
  420. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  421. if (unlikely(ret != 0))
  422. goto out_err;
  423. return 0;
  424. out_err:
  425. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  426. cmd_id + SVGA_3D_CMD_BASE);
  427. return -EINVAL;
  428. }
  429. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  430. struct vmw_sw_context *sw_context,
  431. void *buf, uint32_t size)
  432. {
  433. int32_t cur_size = size;
  434. int ret;
  435. while (cur_size > 0) {
  436. size = cur_size;
  437. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  438. if (unlikely(ret != 0))
  439. return ret;
  440. buf = (void *)((unsigned long) buf + size);
  441. cur_size -= size;
  442. }
  443. if (unlikely(cur_size != 0)) {
  444. DRM_ERROR("Command verifier out of sync.\n");
  445. return -EINVAL;
  446. }
  447. return 0;
  448. }
  449. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  450. {
  451. sw_context->cur_reloc = 0;
  452. }
  453. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  454. {
  455. uint32_t i;
  456. struct vmw_relocation *reloc;
  457. struct ttm_validate_buffer *validate;
  458. struct ttm_buffer_object *bo;
  459. for (i = 0; i < sw_context->cur_reloc; ++i) {
  460. reloc = &sw_context->relocs[i];
  461. validate = &sw_context->val_bufs[reloc->index];
  462. bo = validate->bo;
  463. if (bo->mem.mem_type == TTM_PL_VRAM) {
  464. reloc->location->offset += bo->offset;
  465. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  466. } else
  467. reloc->location->gmrId = bo->mem.start;
  468. }
  469. vmw_free_relocations(sw_context);
  470. }
  471. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  472. {
  473. struct ttm_validate_buffer *entry, *next;
  474. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  475. head) {
  476. list_del(&entry->head);
  477. vmw_dmabuf_validate_clear(entry->bo);
  478. ttm_bo_unref(&entry->bo);
  479. sw_context->cur_val_buf--;
  480. }
  481. BUG_ON(sw_context->cur_val_buf != 0);
  482. }
  483. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  484. struct ttm_buffer_object *bo)
  485. {
  486. int ret;
  487. /**
  488. * Put BO in VRAM if there is space, otherwise as a GMR.
  489. * If there is no space in VRAM and GMR ids are all used up,
  490. * start evicting GMRs to make room. If the DMA buffer can't be
  491. * used as a GMR, this will return -ENOMEM.
  492. */
  493. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false, false);
  494. if (likely(ret == 0 || ret == -ERESTARTSYS))
  495. return ret;
  496. /**
  497. * If that failed, try VRAM again, this time evicting
  498. * previous contents.
  499. */
  500. DRM_INFO("Falling through to VRAM.\n");
  501. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false, false);
  502. return ret;
  503. }
  504. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  505. struct vmw_sw_context *sw_context)
  506. {
  507. struct ttm_validate_buffer *entry;
  508. int ret;
  509. list_for_each_entry(entry, &sw_context->validate_nodes, head) {
  510. ret = vmw_validate_single_buffer(dev_priv, entry->bo);
  511. if (unlikely(ret != 0))
  512. return ret;
  513. }
  514. return 0;
  515. }
  516. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  517. struct drm_file *file_priv)
  518. {
  519. struct vmw_private *dev_priv = vmw_priv(dev);
  520. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  521. struct drm_vmw_fence_rep fence_rep;
  522. struct drm_vmw_fence_rep __user *user_fence_rep;
  523. int ret;
  524. void *user_cmd;
  525. void *cmd;
  526. uint32_t sequence;
  527. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  528. struct vmw_master *vmaster = vmw_master(file_priv->master);
  529. ret = ttm_read_lock(&vmaster->lock, true);
  530. if (unlikely(ret != 0))
  531. return ret;
  532. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  533. if (unlikely(ret != 0)) {
  534. ret = -ERESTARTSYS;
  535. goto out_no_cmd_mutex;
  536. }
  537. cmd = vmw_fifo_reserve(dev_priv, arg->command_size);
  538. if (unlikely(cmd == NULL)) {
  539. DRM_ERROR("Failed reserving fifo space for commands.\n");
  540. ret = -ENOMEM;
  541. goto out_unlock;
  542. }
  543. user_cmd = (void __user *)(unsigned long)arg->commands;
  544. ret = copy_from_user(cmd, user_cmd, arg->command_size);
  545. if (unlikely(ret != 0)) {
  546. ret = -EFAULT;
  547. DRM_ERROR("Failed copying commands.\n");
  548. goto out_commit;
  549. }
  550. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  551. sw_context->cid_valid = false;
  552. sw_context->sid_valid = false;
  553. sw_context->cur_reloc = 0;
  554. sw_context->cur_val_buf = 0;
  555. INIT_LIST_HEAD(&sw_context->validate_nodes);
  556. ret = vmw_cmd_check_all(dev_priv, sw_context, cmd, arg->command_size);
  557. if (unlikely(ret != 0))
  558. goto out_err;
  559. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes);
  560. if (unlikely(ret != 0))
  561. goto out_err;
  562. ret = vmw_validate_buffers(dev_priv, sw_context);
  563. if (unlikely(ret != 0))
  564. goto out_err;
  565. vmw_apply_relocations(sw_context);
  566. if (arg->throttle_us) {
  567. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.fence_queue,
  568. arg->throttle_us);
  569. if (unlikely(ret != 0))
  570. goto out_err;
  571. }
  572. vmw_fifo_commit(dev_priv, arg->command_size);
  573. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  574. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  575. (void *)(unsigned long) sequence);
  576. vmw_clear_validations(sw_context);
  577. mutex_unlock(&dev_priv->cmdbuf_mutex);
  578. /*
  579. * This error is harmless, because if fence submission fails,
  580. * vmw_fifo_send_fence will sync.
  581. */
  582. if (ret != 0)
  583. DRM_ERROR("Fence submission error. Syncing.\n");
  584. fence_rep.error = ret;
  585. fence_rep.fence_seq = (uint64_t) sequence;
  586. fence_rep.pad64 = 0;
  587. user_fence_rep = (struct drm_vmw_fence_rep __user *)
  588. (unsigned long)arg->fence_rep;
  589. /*
  590. * copy_to_user errors will be detected by user space not
  591. * seeing fence_rep::error filled in.
  592. */
  593. ret = copy_to_user(user_fence_rep, &fence_rep, sizeof(fence_rep));
  594. vmw_kms_cursor_post_execbuf(dev_priv);
  595. ttm_read_unlock(&vmaster->lock);
  596. return 0;
  597. out_err:
  598. vmw_free_relocations(sw_context);
  599. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  600. vmw_clear_validations(sw_context);
  601. out_commit:
  602. vmw_fifo_commit(dev_priv, 0);
  603. out_unlock:
  604. mutex_unlock(&dev_priv->cmdbuf_mutex);
  605. out_no_cmd_mutex:
  606. ttm_read_unlock(&vmaster->lock);
  607. return ret;
  608. }