vmwgfx_execbuf.c 37 KB

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