vmwgfx_execbuf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 void vmw_resource_to_validate_list(struct vmw_sw_context *sw_context,
  44. struct vmw_resource **p_res)
  45. {
  46. struct vmw_resource *res = *p_res;
  47. if (list_empty(&res->validate_head)) {
  48. list_add_tail(&res->validate_head, &sw_context->resource_list);
  49. *p_res = NULL;
  50. } else
  51. vmw_resource_unreference(p_res);
  52. }
  53. /**
  54. * vmw_bo_to_validate_list - add a bo to a validate list
  55. *
  56. * @sw_context: The software context used for this command submission batch.
  57. * @bo: The buffer object to add.
  58. * @fence_flags: Fence flags to be or'ed with any other fence flags for
  59. * this buffer on this submission batch.
  60. * @p_val_node: If non-NULL Will be updated with the validate node number
  61. * on return.
  62. *
  63. * Returns -EINVAL if the limit of number of buffer objects per command
  64. * submission is reached.
  65. */
  66. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  67. struct ttm_buffer_object *bo,
  68. uint32_t fence_flags,
  69. uint32_t *p_val_node)
  70. {
  71. uint32_t val_node;
  72. struct ttm_validate_buffer *val_buf;
  73. val_node = vmw_dmabuf_validate_node(bo, sw_context->cur_val_buf);
  74. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  75. DRM_ERROR("Max number of DMA buffers per submission"
  76. " exceeded.\n");
  77. return -EINVAL;
  78. }
  79. val_buf = &sw_context->val_bufs[val_node];
  80. if (unlikely(val_node == sw_context->cur_val_buf)) {
  81. val_buf->new_sync_obj_arg = NULL;
  82. val_buf->bo = ttm_bo_reference(bo);
  83. val_buf->usage = TTM_USAGE_READWRITE;
  84. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  85. ++sw_context->cur_val_buf;
  86. }
  87. val_buf->new_sync_obj_arg = (void *)
  88. ((unsigned long) val_buf->new_sync_obj_arg | fence_flags);
  89. sw_context->fence_flags |= fence_flags;
  90. if (p_val_node)
  91. *p_val_node = val_node;
  92. return 0;
  93. }
  94. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  95. struct vmw_sw_context *sw_context,
  96. SVGA3dCmdHeader *header)
  97. {
  98. struct vmw_resource *ctx;
  99. struct vmw_cid_cmd {
  100. SVGA3dCmdHeader header;
  101. __le32 cid;
  102. } *cmd;
  103. int ret;
  104. cmd = container_of(header, struct vmw_cid_cmd, header);
  105. if (likely(sw_context->cid_valid && cmd->cid == sw_context->last_cid))
  106. return 0;
  107. ret = vmw_context_check(dev_priv, sw_context->tfile, cmd->cid,
  108. &ctx);
  109. if (unlikely(ret != 0)) {
  110. DRM_ERROR("Could not find or use context %u\n",
  111. (unsigned) cmd->cid);
  112. return ret;
  113. }
  114. sw_context->last_cid = cmd->cid;
  115. sw_context->cid_valid = true;
  116. sw_context->cur_ctx = ctx;
  117. vmw_resource_to_validate_list(sw_context, &ctx);
  118. return 0;
  119. }
  120. static int vmw_cmd_sid_check(struct vmw_private *dev_priv,
  121. struct vmw_sw_context *sw_context,
  122. uint32_t *sid)
  123. {
  124. struct vmw_surface *srf;
  125. int ret;
  126. struct vmw_resource *res;
  127. if (*sid == SVGA3D_INVALID_ID)
  128. return 0;
  129. if (likely((sw_context->sid_valid &&
  130. *sid == sw_context->last_sid))) {
  131. *sid = sw_context->sid_translation;
  132. return 0;
  133. }
  134. ret = vmw_user_surface_lookup_handle(dev_priv,
  135. sw_context->tfile,
  136. *sid, &srf);
  137. if (unlikely(ret != 0)) {
  138. DRM_ERROR("Could ot find or use surface 0x%08x "
  139. "address 0x%08lx\n",
  140. (unsigned int) *sid,
  141. (unsigned long) sid);
  142. return ret;
  143. }
  144. sw_context->last_sid = *sid;
  145. sw_context->sid_valid = true;
  146. sw_context->sid_translation = srf->res.id;
  147. *sid = sw_context->sid_translation;
  148. res = &srf->res;
  149. vmw_resource_to_validate_list(sw_context, &res);
  150. return 0;
  151. }
  152. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  153. struct vmw_sw_context *sw_context,
  154. SVGA3dCmdHeader *header)
  155. {
  156. struct vmw_sid_cmd {
  157. SVGA3dCmdHeader header;
  158. SVGA3dCmdSetRenderTarget body;
  159. } *cmd;
  160. int ret;
  161. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  162. if (unlikely(ret != 0))
  163. return ret;
  164. cmd = container_of(header, struct vmw_sid_cmd, header);
  165. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.target.sid);
  166. return ret;
  167. }
  168. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  169. struct vmw_sw_context *sw_context,
  170. SVGA3dCmdHeader *header)
  171. {
  172. struct vmw_sid_cmd {
  173. SVGA3dCmdHeader header;
  174. SVGA3dCmdSurfaceCopy body;
  175. } *cmd;
  176. int ret;
  177. cmd = container_of(header, struct vmw_sid_cmd, header);
  178. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  179. if (unlikely(ret != 0))
  180. return ret;
  181. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  182. }
  183. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  184. struct vmw_sw_context *sw_context,
  185. SVGA3dCmdHeader *header)
  186. {
  187. struct vmw_sid_cmd {
  188. SVGA3dCmdHeader header;
  189. SVGA3dCmdSurfaceStretchBlt body;
  190. } *cmd;
  191. int ret;
  192. cmd = container_of(header, struct vmw_sid_cmd, header);
  193. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  194. if (unlikely(ret != 0))
  195. return ret;
  196. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  197. }
  198. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  199. struct vmw_sw_context *sw_context,
  200. SVGA3dCmdHeader *header)
  201. {
  202. struct vmw_sid_cmd {
  203. SVGA3dCmdHeader header;
  204. SVGA3dCmdBlitSurfaceToScreen body;
  205. } *cmd;
  206. cmd = container_of(header, struct vmw_sid_cmd, header);
  207. if (unlikely(!sw_context->kernel)) {
  208. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  209. return -EPERM;
  210. }
  211. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.srcImage.sid);
  212. }
  213. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  214. struct vmw_sw_context *sw_context,
  215. SVGA3dCmdHeader *header)
  216. {
  217. struct vmw_sid_cmd {
  218. SVGA3dCmdHeader header;
  219. SVGA3dCmdPresent body;
  220. } *cmd;
  221. cmd = container_of(header, struct vmw_sid_cmd, header);
  222. if (unlikely(!sw_context->kernel)) {
  223. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  224. return -EPERM;
  225. }
  226. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.sid);
  227. }
  228. /**
  229. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  230. *
  231. * @dev_priv: The device private structure.
  232. * @cid: The hardware context for the next query.
  233. * @new_query_bo: The new buffer holding query results.
  234. * @sw_context: The software context used for this command submission.
  235. *
  236. * This function checks whether @new_query_bo is suitable for holding
  237. * query results, and if another buffer currently is pinned for query
  238. * results. If so, the function prepares the state of @sw_context for
  239. * switching pinned buffers after successful submission of the current
  240. * command batch. It also checks whether we're using a new query context.
  241. * In that case, it makes sure we emit a query barrier for the old
  242. * context before the current query buffer is fenced.
  243. */
  244. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  245. uint32_t cid,
  246. struct ttm_buffer_object *new_query_bo,
  247. struct vmw_sw_context *sw_context)
  248. {
  249. int ret;
  250. bool add_cid = false;
  251. uint32_t cid_to_add;
  252. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  253. if (unlikely(new_query_bo->num_pages > 4)) {
  254. DRM_ERROR("Query buffer too large.\n");
  255. return -EINVAL;
  256. }
  257. if (unlikely(sw_context->cur_query_bo != NULL)) {
  258. BUG_ON(!sw_context->query_cid_valid);
  259. add_cid = true;
  260. cid_to_add = sw_context->cur_query_cid;
  261. ret = vmw_bo_to_validate_list(sw_context,
  262. sw_context->cur_query_bo,
  263. DRM_VMW_FENCE_FLAG_EXEC,
  264. NULL);
  265. if (unlikely(ret != 0))
  266. return ret;
  267. }
  268. sw_context->cur_query_bo = new_query_bo;
  269. ret = vmw_bo_to_validate_list(sw_context,
  270. dev_priv->dummy_query_bo,
  271. DRM_VMW_FENCE_FLAG_EXEC,
  272. NULL);
  273. if (unlikely(ret != 0))
  274. return ret;
  275. }
  276. if (unlikely(cid != sw_context->cur_query_cid &&
  277. sw_context->query_cid_valid)) {
  278. add_cid = true;
  279. cid_to_add = sw_context->cur_query_cid;
  280. }
  281. sw_context->cur_query_cid = cid;
  282. sw_context->query_cid_valid = true;
  283. if (add_cid) {
  284. struct vmw_resource *ctx = sw_context->cur_ctx;
  285. if (list_empty(&ctx->query_head))
  286. list_add_tail(&ctx->query_head,
  287. &sw_context->query_list);
  288. ret = vmw_bo_to_validate_list(sw_context,
  289. dev_priv->dummy_query_bo,
  290. DRM_VMW_FENCE_FLAG_EXEC,
  291. NULL);
  292. if (unlikely(ret != 0))
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. /**
  298. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  299. *
  300. * @dev_priv: The device private structure.
  301. * @sw_context: The software context used for this command submission batch.
  302. *
  303. * This function will check if we're switching query buffers, and will then,
  304. * if no other query waits are issued this command submission batch,
  305. * issue a dummy occlusion query wait used as a query barrier. When the fence
  306. * object following that query wait has signaled, we are sure that all
  307. * preseding queries have finished, and the old query buffer can be unpinned.
  308. * However, since both the new query buffer and the old one are fenced with
  309. * that fence, we can do an asynchronus unpin now, and be sure that the
  310. * old query buffer won't be moved until the fence has signaled.
  311. *
  312. * As mentioned above, both the new - and old query buffers need to be fenced
  313. * using a sequence emitted *after* calling this function.
  314. */
  315. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  316. struct vmw_sw_context *sw_context)
  317. {
  318. struct vmw_resource *ctx, *next_ctx;
  319. int ret;
  320. /*
  321. * The validate list should still hold references to all
  322. * contexts here.
  323. */
  324. list_for_each_entry_safe(ctx, next_ctx, &sw_context->query_list,
  325. query_head) {
  326. list_del_init(&ctx->query_head);
  327. BUG_ON(list_empty(&ctx->validate_head));
  328. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  329. if (unlikely(ret != 0))
  330. DRM_ERROR("Out of fifo space for dummy query.\n");
  331. }
  332. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  333. if (dev_priv->pinned_bo) {
  334. vmw_bo_pin(dev_priv->pinned_bo, false);
  335. ttm_bo_unref(&dev_priv->pinned_bo);
  336. }
  337. vmw_bo_pin(sw_context->cur_query_bo, true);
  338. /*
  339. * We pin also the dummy_query_bo buffer so that we
  340. * don't need to validate it when emitting
  341. * dummy queries in context destroy paths.
  342. */
  343. vmw_bo_pin(dev_priv->dummy_query_bo, true);
  344. dev_priv->dummy_query_bo_pinned = true;
  345. dev_priv->query_cid = sw_context->cur_query_cid;
  346. dev_priv->pinned_bo =
  347. ttm_bo_reference(sw_context->cur_query_bo);
  348. }
  349. }
  350. /**
  351. * vmw_query_switch_backoff - clear query barrier list
  352. * @sw_context: The sw context used for this submission batch.
  353. *
  354. * This function is used as part of an error path, where a previously
  355. * set up list of query barriers needs to be cleared.
  356. *
  357. */
  358. static void vmw_query_switch_backoff(struct vmw_sw_context *sw_context)
  359. {
  360. struct list_head *list, *next;
  361. list_for_each_safe(list, next, &sw_context->query_list) {
  362. list_del_init(list);
  363. }
  364. }
  365. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  366. struct vmw_sw_context *sw_context,
  367. SVGAGuestPtr *ptr,
  368. struct vmw_dma_buffer **vmw_bo_p)
  369. {
  370. struct vmw_dma_buffer *vmw_bo = NULL;
  371. struct ttm_buffer_object *bo;
  372. uint32_t handle = ptr->gmrId;
  373. struct vmw_relocation *reloc;
  374. int ret;
  375. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  376. if (unlikely(ret != 0)) {
  377. DRM_ERROR("Could not find or use GMR region.\n");
  378. return -EINVAL;
  379. }
  380. bo = &vmw_bo->base;
  381. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  382. DRM_ERROR("Max number relocations per submission"
  383. " exceeded\n");
  384. ret = -EINVAL;
  385. goto out_no_reloc;
  386. }
  387. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  388. reloc->location = ptr;
  389. ret = vmw_bo_to_validate_list(sw_context, bo, DRM_VMW_FENCE_FLAG_EXEC,
  390. &reloc->index);
  391. if (unlikely(ret != 0))
  392. goto out_no_reloc;
  393. *vmw_bo_p = vmw_bo;
  394. return 0;
  395. out_no_reloc:
  396. vmw_dmabuf_unreference(&vmw_bo);
  397. vmw_bo_p = NULL;
  398. return ret;
  399. }
  400. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  401. struct vmw_sw_context *sw_context,
  402. SVGA3dCmdHeader *header)
  403. {
  404. struct vmw_dma_buffer *vmw_bo;
  405. struct vmw_query_cmd {
  406. SVGA3dCmdHeader header;
  407. SVGA3dCmdEndQuery q;
  408. } *cmd;
  409. int ret;
  410. cmd = container_of(header, struct vmw_query_cmd, header);
  411. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  412. if (unlikely(ret != 0))
  413. return ret;
  414. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  415. &cmd->q.guestResult,
  416. &vmw_bo);
  417. if (unlikely(ret != 0))
  418. return ret;
  419. ret = vmw_query_bo_switch_prepare(dev_priv, cmd->q.cid,
  420. &vmw_bo->base, sw_context);
  421. vmw_dmabuf_unreference(&vmw_bo);
  422. return ret;
  423. }
  424. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  425. struct vmw_sw_context *sw_context,
  426. SVGA3dCmdHeader *header)
  427. {
  428. struct vmw_dma_buffer *vmw_bo;
  429. struct vmw_query_cmd {
  430. SVGA3dCmdHeader header;
  431. SVGA3dCmdWaitForQuery q;
  432. } *cmd;
  433. int ret;
  434. struct vmw_resource *ctx;
  435. cmd = container_of(header, struct vmw_query_cmd, header);
  436. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  437. if (unlikely(ret != 0))
  438. return ret;
  439. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  440. &cmd->q.guestResult,
  441. &vmw_bo);
  442. if (unlikely(ret != 0))
  443. return ret;
  444. vmw_dmabuf_unreference(&vmw_bo);
  445. /*
  446. * This wait will act as a barrier for previous waits for this
  447. * context.
  448. */
  449. ctx = sw_context->cur_ctx;
  450. if (!list_empty(&ctx->query_head))
  451. list_del_init(&ctx->query_head);
  452. return 0;
  453. }
  454. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  455. struct vmw_sw_context *sw_context,
  456. SVGA3dCmdHeader *header)
  457. {
  458. struct vmw_dma_buffer *vmw_bo = NULL;
  459. struct ttm_buffer_object *bo;
  460. struct vmw_surface *srf = NULL;
  461. struct vmw_dma_cmd {
  462. SVGA3dCmdHeader header;
  463. SVGA3dCmdSurfaceDMA dma;
  464. } *cmd;
  465. int ret;
  466. struct vmw_resource *res;
  467. cmd = container_of(header, struct vmw_dma_cmd, header);
  468. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  469. &cmd->dma.guest.ptr,
  470. &vmw_bo);
  471. if (unlikely(ret != 0))
  472. return ret;
  473. bo = &vmw_bo->base;
  474. ret = vmw_user_surface_lookup_handle(dev_priv, sw_context->tfile,
  475. cmd->dma.host.sid, &srf);
  476. if (ret) {
  477. DRM_ERROR("could not find surface\n");
  478. goto out_no_reloc;
  479. }
  480. /*
  481. * Patch command stream with device SID.
  482. */
  483. cmd->dma.host.sid = srf->res.id;
  484. vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
  485. vmw_dmabuf_unreference(&vmw_bo);
  486. res = &srf->res;
  487. vmw_resource_to_validate_list(sw_context, &res);
  488. return 0;
  489. out_no_reloc:
  490. vmw_dmabuf_unreference(&vmw_bo);
  491. return ret;
  492. }
  493. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  494. struct vmw_sw_context *sw_context,
  495. SVGA3dCmdHeader *header)
  496. {
  497. struct vmw_draw_cmd {
  498. SVGA3dCmdHeader header;
  499. SVGA3dCmdDrawPrimitives body;
  500. } *cmd;
  501. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  502. (unsigned long)header + sizeof(*cmd));
  503. SVGA3dPrimitiveRange *range;
  504. uint32_t i;
  505. uint32_t maxnum;
  506. int ret;
  507. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  508. if (unlikely(ret != 0))
  509. return ret;
  510. cmd = container_of(header, struct vmw_draw_cmd, header);
  511. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  512. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  513. DRM_ERROR("Illegal number of vertex declarations.\n");
  514. return -EINVAL;
  515. }
  516. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  517. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  518. &decl->array.surfaceId);
  519. if (unlikely(ret != 0))
  520. return ret;
  521. }
  522. maxnum = (header->size - sizeof(cmd->body) -
  523. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  524. if (unlikely(cmd->body.numRanges > maxnum)) {
  525. DRM_ERROR("Illegal number of index ranges.\n");
  526. return -EINVAL;
  527. }
  528. range = (SVGA3dPrimitiveRange *) decl;
  529. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  530. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  531. &range->indexArray.surfaceId);
  532. if (unlikely(ret != 0))
  533. return ret;
  534. }
  535. return 0;
  536. }
  537. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  538. struct vmw_sw_context *sw_context,
  539. SVGA3dCmdHeader *header)
  540. {
  541. struct vmw_tex_state_cmd {
  542. SVGA3dCmdHeader header;
  543. SVGA3dCmdSetTextureState state;
  544. };
  545. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  546. ((unsigned long) header + header->size + sizeof(header));
  547. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  548. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  549. int ret;
  550. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  551. if (unlikely(ret != 0))
  552. return ret;
  553. for (; cur_state < last_state; ++cur_state) {
  554. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  555. continue;
  556. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  557. &cur_state->value);
  558. if (unlikely(ret != 0))
  559. return ret;
  560. }
  561. return 0;
  562. }
  563. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  564. struct vmw_sw_context *sw_context,
  565. void *buf)
  566. {
  567. struct vmw_dma_buffer *vmw_bo;
  568. int ret;
  569. struct {
  570. uint32_t header;
  571. SVGAFifoCmdDefineGMRFB body;
  572. } *cmd = buf;
  573. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  574. &cmd->body.ptr,
  575. &vmw_bo);
  576. if (unlikely(ret != 0))
  577. return ret;
  578. vmw_dmabuf_unreference(&vmw_bo);
  579. return ret;
  580. }
  581. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  582. struct vmw_sw_context *sw_context,
  583. void *buf, uint32_t *size)
  584. {
  585. uint32_t size_remaining = *size;
  586. uint32_t cmd_id;
  587. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  588. switch (cmd_id) {
  589. case SVGA_CMD_UPDATE:
  590. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  591. break;
  592. case SVGA_CMD_DEFINE_GMRFB:
  593. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  594. break;
  595. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  596. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  597. break;
  598. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  599. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  600. break;
  601. default:
  602. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  603. return -EINVAL;
  604. }
  605. if (*size > size_remaining) {
  606. DRM_ERROR("Invalid SVGA command (size mismatch):"
  607. " %u.\n", cmd_id);
  608. return -EINVAL;
  609. }
  610. if (unlikely(!sw_context->kernel)) {
  611. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  612. return -EPERM;
  613. }
  614. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  615. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  616. return 0;
  617. }
  618. typedef int (*vmw_cmd_func) (struct vmw_private *,
  619. struct vmw_sw_context *,
  620. SVGA3dCmdHeader *);
  621. #define VMW_CMD_DEF(cmd, func) \
  622. [cmd - SVGA_3D_CMD_BASE] = func
  623. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  624. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  625. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  626. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  627. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  628. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  629. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  630. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  631. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  632. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  633. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  634. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  635. &vmw_cmd_set_render_target_check),
  636. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  637. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  638. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  639. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  640. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  641. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  642. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  643. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  644. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  645. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  646. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
  647. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  648. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  649. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  650. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
  651. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
  652. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
  653. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  654. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  655. &vmw_cmd_blt_surf_screen_check)
  656. };
  657. static int vmw_cmd_check(struct vmw_private *dev_priv,
  658. struct vmw_sw_context *sw_context,
  659. void *buf, uint32_t *size)
  660. {
  661. uint32_t cmd_id;
  662. uint32_t size_remaining = *size;
  663. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  664. int ret;
  665. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  666. /* Handle any none 3D commands */
  667. if (unlikely(cmd_id < SVGA_CMD_MAX))
  668. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  669. cmd_id = le32_to_cpu(header->id);
  670. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  671. cmd_id -= SVGA_3D_CMD_BASE;
  672. if (unlikely(*size > size_remaining))
  673. goto out_err;
  674. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  675. goto out_err;
  676. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  677. if (unlikely(ret != 0))
  678. goto out_err;
  679. return 0;
  680. out_err:
  681. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  682. cmd_id + SVGA_3D_CMD_BASE);
  683. return -EINVAL;
  684. }
  685. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  686. struct vmw_sw_context *sw_context,
  687. void *buf,
  688. uint32_t size)
  689. {
  690. int32_t cur_size = size;
  691. int ret;
  692. while (cur_size > 0) {
  693. size = cur_size;
  694. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  695. if (unlikely(ret != 0))
  696. return ret;
  697. buf = (void *)((unsigned long) buf + size);
  698. cur_size -= size;
  699. }
  700. if (unlikely(cur_size != 0)) {
  701. DRM_ERROR("Command verifier out of sync.\n");
  702. return -EINVAL;
  703. }
  704. return 0;
  705. }
  706. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  707. {
  708. sw_context->cur_reloc = 0;
  709. }
  710. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  711. {
  712. uint32_t i;
  713. struct vmw_relocation *reloc;
  714. struct ttm_validate_buffer *validate;
  715. struct ttm_buffer_object *bo;
  716. for (i = 0; i < sw_context->cur_reloc; ++i) {
  717. reloc = &sw_context->relocs[i];
  718. validate = &sw_context->val_bufs[reloc->index];
  719. bo = validate->bo;
  720. if (bo->mem.mem_type == TTM_PL_VRAM) {
  721. reloc->location->offset += bo->offset;
  722. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  723. } else
  724. reloc->location->gmrId = bo->mem.start;
  725. }
  726. vmw_free_relocations(sw_context);
  727. }
  728. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  729. {
  730. struct ttm_validate_buffer *entry, *next;
  731. struct vmw_resource *res, *res_next;
  732. /*
  733. * Drop references to DMA buffers held during command submission.
  734. */
  735. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  736. head) {
  737. list_del(&entry->head);
  738. vmw_dmabuf_validate_clear(entry->bo);
  739. ttm_bo_unref(&entry->bo);
  740. sw_context->cur_val_buf--;
  741. }
  742. BUG_ON(sw_context->cur_val_buf != 0);
  743. /*
  744. * Drop references to resources held during command submission.
  745. */
  746. list_for_each_entry_safe(res, res_next, &sw_context->resource_list,
  747. validate_head) {
  748. list_del_init(&res->validate_head);
  749. vmw_resource_unreference(&res);
  750. }
  751. }
  752. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  753. struct ttm_buffer_object *bo)
  754. {
  755. int ret;
  756. /*
  757. * Don't validate pinned buffers.
  758. */
  759. if (bo == dev_priv->pinned_bo ||
  760. (bo == dev_priv->dummy_query_bo &&
  761. dev_priv->dummy_query_bo_pinned))
  762. return 0;
  763. /**
  764. * Put BO in VRAM if there is space, otherwise as a GMR.
  765. * If there is no space in VRAM and GMR ids are all used up,
  766. * start evicting GMRs to make room. If the DMA buffer can't be
  767. * used as a GMR, this will return -ENOMEM.
  768. */
  769. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false, false);
  770. if (likely(ret == 0 || ret == -ERESTARTSYS))
  771. return ret;
  772. /**
  773. * If that failed, try VRAM again, this time evicting
  774. * previous contents.
  775. */
  776. DRM_INFO("Falling through to VRAM.\n");
  777. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false, false);
  778. return ret;
  779. }
  780. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  781. struct vmw_sw_context *sw_context)
  782. {
  783. struct ttm_validate_buffer *entry;
  784. int ret;
  785. list_for_each_entry(entry, &sw_context->validate_nodes, head) {
  786. ret = vmw_validate_single_buffer(dev_priv, entry->bo);
  787. if (unlikely(ret != 0))
  788. return ret;
  789. }
  790. return 0;
  791. }
  792. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  793. uint32_t size)
  794. {
  795. if (likely(sw_context->cmd_bounce_size >= size))
  796. return 0;
  797. if (sw_context->cmd_bounce_size == 0)
  798. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  799. while (sw_context->cmd_bounce_size < size) {
  800. sw_context->cmd_bounce_size =
  801. PAGE_ALIGN(sw_context->cmd_bounce_size +
  802. (sw_context->cmd_bounce_size >> 1));
  803. }
  804. if (sw_context->cmd_bounce != NULL)
  805. vfree(sw_context->cmd_bounce);
  806. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  807. if (sw_context->cmd_bounce == NULL) {
  808. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  809. sw_context->cmd_bounce_size = 0;
  810. return -ENOMEM;
  811. }
  812. return 0;
  813. }
  814. /**
  815. * vmw_execbuf_fence_commands - create and submit a command stream fence
  816. *
  817. * Creates a fence object and submits a command stream marker.
  818. * If this fails for some reason, We sync the fifo and return NULL.
  819. * It is then safe to fence buffers with a NULL pointer.
  820. *
  821. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  822. * a userspace handle if @p_handle is not NULL, otherwise not.
  823. */
  824. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  825. struct vmw_private *dev_priv,
  826. struct vmw_fence_obj **p_fence,
  827. uint32_t *p_handle)
  828. {
  829. uint32_t sequence;
  830. int ret;
  831. bool synced = false;
  832. /* p_handle implies file_priv. */
  833. BUG_ON(p_handle != NULL && file_priv == NULL);
  834. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  835. if (unlikely(ret != 0)) {
  836. DRM_ERROR("Fence submission error. Syncing.\n");
  837. synced = true;
  838. }
  839. if (p_handle != NULL)
  840. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  841. sequence,
  842. DRM_VMW_FENCE_FLAG_EXEC,
  843. p_fence, p_handle);
  844. else
  845. ret = vmw_fence_create(dev_priv->fman, sequence,
  846. DRM_VMW_FENCE_FLAG_EXEC,
  847. p_fence);
  848. if (unlikely(ret != 0 && !synced)) {
  849. (void) vmw_fallback_wait(dev_priv, false, false,
  850. sequence, false,
  851. VMW_FENCE_WAIT_TIMEOUT);
  852. *p_fence = NULL;
  853. }
  854. return 0;
  855. }
  856. int vmw_execbuf_process(struct drm_file *file_priv,
  857. struct vmw_private *dev_priv,
  858. void __user *user_commands,
  859. void *kernel_commands,
  860. uint32_t command_size,
  861. uint64_t throttle_us,
  862. struct drm_vmw_fence_rep __user *user_fence_rep)
  863. {
  864. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  865. struct drm_vmw_fence_rep fence_rep;
  866. struct vmw_fence_obj *fence;
  867. uint32_t handle;
  868. void *cmd;
  869. int ret;
  870. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  871. if (unlikely(ret != 0))
  872. return -ERESTARTSYS;
  873. if (kernel_commands == NULL) {
  874. sw_context->kernel = false;
  875. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  876. if (unlikely(ret != 0))
  877. goto out_unlock;
  878. ret = copy_from_user(sw_context->cmd_bounce,
  879. user_commands, command_size);
  880. if (unlikely(ret != 0)) {
  881. ret = -EFAULT;
  882. DRM_ERROR("Failed copying commands.\n");
  883. goto out_unlock;
  884. }
  885. kernel_commands = sw_context->cmd_bounce;
  886. } else
  887. sw_context->kernel = true;
  888. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  889. sw_context->cid_valid = false;
  890. sw_context->sid_valid = false;
  891. sw_context->cur_reloc = 0;
  892. sw_context->cur_val_buf = 0;
  893. sw_context->fence_flags = 0;
  894. INIT_LIST_HEAD(&sw_context->query_list);
  895. INIT_LIST_HEAD(&sw_context->resource_list);
  896. sw_context->cur_query_bo = dev_priv->pinned_bo;
  897. sw_context->cur_query_cid = dev_priv->query_cid;
  898. sw_context->query_cid_valid = (dev_priv->pinned_bo != NULL);
  899. INIT_LIST_HEAD(&sw_context->validate_nodes);
  900. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  901. command_size);
  902. if (unlikely(ret != 0))
  903. goto out_err;
  904. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes);
  905. if (unlikely(ret != 0))
  906. goto out_err;
  907. ret = vmw_validate_buffers(dev_priv, sw_context);
  908. if (unlikely(ret != 0))
  909. goto out_err;
  910. vmw_apply_relocations(sw_context);
  911. if (throttle_us) {
  912. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  913. throttle_us);
  914. if (unlikely(ret != 0))
  915. goto out_throttle;
  916. }
  917. cmd = vmw_fifo_reserve(dev_priv, command_size);
  918. if (unlikely(cmd == NULL)) {
  919. DRM_ERROR("Failed reserving fifo space for commands.\n");
  920. ret = -ENOMEM;
  921. goto out_throttle;
  922. }
  923. memcpy(cmd, kernel_commands, command_size);
  924. vmw_fifo_commit(dev_priv, command_size);
  925. vmw_query_bo_switch_commit(dev_priv, sw_context);
  926. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  927. &fence,
  928. (user_fence_rep) ? &handle : NULL);
  929. /*
  930. * This error is harmless, because if fence submission fails,
  931. * vmw_fifo_send_fence will sync. The error will be propagated to
  932. * user-space in @fence_rep
  933. */
  934. if (ret != 0)
  935. DRM_ERROR("Fence submission error. Syncing.\n");
  936. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  937. (void *) fence);
  938. vmw_clear_validations(sw_context);
  939. if (user_fence_rep) {
  940. fence_rep.error = ret;
  941. fence_rep.handle = handle;
  942. fence_rep.seqno = fence->seqno;
  943. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  944. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  945. /*
  946. * copy_to_user errors will be detected by user space not
  947. * seeing fence_rep::error filled in. Typically
  948. * user-space would have pre-set that member to -EFAULT.
  949. */
  950. ret = copy_to_user(user_fence_rep, &fence_rep,
  951. sizeof(fence_rep));
  952. /*
  953. * User-space lost the fence object. We need to sync
  954. * and unreference the handle.
  955. */
  956. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  957. BUG_ON(fence == NULL);
  958. ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  959. handle, TTM_REF_USAGE);
  960. DRM_ERROR("Fence copy error. Syncing.\n");
  961. (void) vmw_fence_obj_wait(fence,
  962. fence->signal_mask,
  963. false, false,
  964. VMW_FENCE_WAIT_TIMEOUT);
  965. }
  966. }
  967. if (likely(fence != NULL))
  968. vmw_fence_obj_unreference(&fence);
  969. mutex_unlock(&dev_priv->cmdbuf_mutex);
  970. return 0;
  971. out_err:
  972. vmw_free_relocations(sw_context);
  973. out_throttle:
  974. vmw_query_switch_backoff(sw_context);
  975. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  976. vmw_clear_validations(sw_context);
  977. out_unlock:
  978. mutex_unlock(&dev_priv->cmdbuf_mutex);
  979. return ret;
  980. }
  981. /**
  982. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  983. *
  984. * @dev_priv: The device private structure.
  985. *
  986. * This function is called to idle the fifo and unpin the query buffer
  987. * if the normal way to do this hits an error, which should typically be
  988. * extremely rare.
  989. */
  990. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  991. {
  992. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  993. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  994. vmw_bo_pin(dev_priv->pinned_bo, false);
  995. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  996. dev_priv->dummy_query_bo_pinned = false;
  997. }
  998. /**
  999. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1000. * query bo.
  1001. *
  1002. * @dev_priv: The device private structure.
  1003. * @only_on_cid_match: Only flush and unpin if the current active query cid
  1004. * matches @cid.
  1005. * @cid: Optional context id to match.
  1006. *
  1007. * This function should be used to unpin the pinned query bo, or
  1008. * as a query barrier when we need to make sure that all queries have
  1009. * finished before the next fifo command. (For example on hardware
  1010. * context destructions where the hardware may otherwise leak unfinished
  1011. * queries).
  1012. *
  1013. * This function does not return any failure codes, but make attempts
  1014. * to do safe unpinning in case of errors.
  1015. *
  1016. * The function will synchronize on the previous query barrier, and will
  1017. * thus not finish until that barrier has executed.
  1018. */
  1019. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  1020. bool only_on_cid_match, uint32_t cid)
  1021. {
  1022. int ret = 0;
  1023. struct list_head validate_list;
  1024. struct ttm_validate_buffer pinned_val, query_val;
  1025. struct vmw_fence_obj *fence;
  1026. mutex_lock(&dev_priv->cmdbuf_mutex);
  1027. if (dev_priv->pinned_bo == NULL)
  1028. goto out_unlock;
  1029. if (only_on_cid_match && cid != dev_priv->query_cid)
  1030. goto out_unlock;
  1031. INIT_LIST_HEAD(&validate_list);
  1032. pinned_val.new_sync_obj_arg = (void *)(unsigned long)
  1033. DRM_VMW_FENCE_FLAG_EXEC;
  1034. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  1035. list_add_tail(&pinned_val.head, &validate_list);
  1036. query_val.new_sync_obj_arg = pinned_val.new_sync_obj_arg;
  1037. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  1038. list_add_tail(&query_val.head, &validate_list);
  1039. do {
  1040. ret = ttm_eu_reserve_buffers(&validate_list);
  1041. } while (ret == -ERESTARTSYS);
  1042. if (unlikely(ret != 0)) {
  1043. vmw_execbuf_unpin_panic(dev_priv);
  1044. goto out_no_reserve;
  1045. }
  1046. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  1047. if (unlikely(ret != 0)) {
  1048. vmw_execbuf_unpin_panic(dev_priv);
  1049. goto out_no_emit;
  1050. }
  1051. vmw_bo_pin(dev_priv->pinned_bo, false);
  1052. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1053. dev_priv->dummy_query_bo_pinned = false;
  1054. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1055. ttm_eu_fence_buffer_objects(&validate_list, (void *) fence);
  1056. ttm_bo_unref(&query_val.bo);
  1057. ttm_bo_unref(&pinned_val.bo);
  1058. ttm_bo_unref(&dev_priv->pinned_bo);
  1059. out_unlock:
  1060. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1061. return;
  1062. out_no_emit:
  1063. ttm_eu_backoff_reservation(&validate_list);
  1064. out_no_reserve:
  1065. ttm_bo_unref(&query_val.bo);
  1066. ttm_bo_unref(&pinned_val.bo);
  1067. ttm_bo_unref(&dev_priv->pinned_bo);
  1068. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1069. }
  1070. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  1071. struct drm_file *file_priv)
  1072. {
  1073. struct vmw_private *dev_priv = vmw_priv(dev);
  1074. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  1075. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1076. int ret;
  1077. /*
  1078. * This will allow us to extend the ioctl argument while
  1079. * maintaining backwards compatibility:
  1080. * We take different code paths depending on the value of
  1081. * arg->version.
  1082. */
  1083. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  1084. DRM_ERROR("Incorrect execbuf version.\n");
  1085. DRM_ERROR("You're running outdated experimental "
  1086. "vmwgfx user-space drivers.");
  1087. return -EINVAL;
  1088. }
  1089. ret = ttm_read_lock(&vmaster->lock, true);
  1090. if (unlikely(ret != 0))
  1091. return ret;
  1092. ret = vmw_execbuf_process(file_priv, dev_priv,
  1093. (void __user *)(unsigned long)arg->commands,
  1094. NULL, arg->command_size, arg->throttle_us,
  1095. (void __user *)(unsigned long)arg->fence_rep);
  1096. if (unlikely(ret != 0))
  1097. goto out_unlock;
  1098. vmw_kms_cursor_post_execbuf(dev_priv);
  1099. out_unlock:
  1100. ttm_read_unlock(&vmaster->lock);
  1101. return ret;
  1102. }