vmwgfx_execbuf.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. ret = vmw_surface_validate(dev_priv, srf);
  145. if (unlikely(ret != 0)) {
  146. if (ret != -ERESTARTSYS)
  147. DRM_ERROR("Could not validate surface.\n");
  148. vmw_surface_unreference(&srf);
  149. return ret;
  150. }
  151. sw_context->last_sid = *sid;
  152. sw_context->sid_valid = true;
  153. sw_context->sid_translation = srf->res.id;
  154. *sid = sw_context->sid_translation;
  155. res = &srf->res;
  156. vmw_resource_to_validate_list(sw_context, &res);
  157. return 0;
  158. }
  159. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  160. struct vmw_sw_context *sw_context,
  161. SVGA3dCmdHeader *header)
  162. {
  163. struct vmw_sid_cmd {
  164. SVGA3dCmdHeader header;
  165. SVGA3dCmdSetRenderTarget body;
  166. } *cmd;
  167. int ret;
  168. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  169. if (unlikely(ret != 0))
  170. return ret;
  171. cmd = container_of(header, struct vmw_sid_cmd, header);
  172. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.target.sid);
  173. return ret;
  174. }
  175. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  176. struct vmw_sw_context *sw_context,
  177. SVGA3dCmdHeader *header)
  178. {
  179. struct vmw_sid_cmd {
  180. SVGA3dCmdHeader header;
  181. SVGA3dCmdSurfaceCopy body;
  182. } *cmd;
  183. int ret;
  184. cmd = container_of(header, struct vmw_sid_cmd, header);
  185. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  186. if (unlikely(ret != 0))
  187. return ret;
  188. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  189. }
  190. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  191. struct vmw_sw_context *sw_context,
  192. SVGA3dCmdHeader *header)
  193. {
  194. struct vmw_sid_cmd {
  195. SVGA3dCmdHeader header;
  196. SVGA3dCmdSurfaceStretchBlt body;
  197. } *cmd;
  198. int ret;
  199. cmd = container_of(header, struct vmw_sid_cmd, header);
  200. ret = vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.src.sid);
  201. if (unlikely(ret != 0))
  202. return ret;
  203. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.dest.sid);
  204. }
  205. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  206. struct vmw_sw_context *sw_context,
  207. SVGA3dCmdHeader *header)
  208. {
  209. struct vmw_sid_cmd {
  210. SVGA3dCmdHeader header;
  211. SVGA3dCmdBlitSurfaceToScreen body;
  212. } *cmd;
  213. cmd = container_of(header, struct vmw_sid_cmd, header);
  214. if (unlikely(!sw_context->kernel)) {
  215. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  216. return -EPERM;
  217. }
  218. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.srcImage.sid);
  219. }
  220. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  221. struct vmw_sw_context *sw_context,
  222. SVGA3dCmdHeader *header)
  223. {
  224. struct vmw_sid_cmd {
  225. SVGA3dCmdHeader header;
  226. SVGA3dCmdPresent body;
  227. } *cmd;
  228. cmd = container_of(header, struct vmw_sid_cmd, header);
  229. if (unlikely(!sw_context->kernel)) {
  230. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  231. return -EPERM;
  232. }
  233. return vmw_cmd_sid_check(dev_priv, sw_context, &cmd->body.sid);
  234. }
  235. /**
  236. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  237. *
  238. * @dev_priv: The device private structure.
  239. * @cid: The hardware context for the next query.
  240. * @new_query_bo: The new buffer holding query results.
  241. * @sw_context: The software context used for this command submission.
  242. *
  243. * This function checks whether @new_query_bo is suitable for holding
  244. * query results, and if another buffer currently is pinned for query
  245. * results. If so, the function prepares the state of @sw_context for
  246. * switching pinned buffers after successful submission of the current
  247. * command batch. It also checks whether we're using a new query context.
  248. * In that case, it makes sure we emit a query barrier for the old
  249. * context before the current query buffer is fenced.
  250. */
  251. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  252. uint32_t cid,
  253. struct ttm_buffer_object *new_query_bo,
  254. struct vmw_sw_context *sw_context)
  255. {
  256. int ret;
  257. bool add_cid = false;
  258. uint32_t cid_to_add;
  259. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  260. if (unlikely(new_query_bo->num_pages > 4)) {
  261. DRM_ERROR("Query buffer too large.\n");
  262. return -EINVAL;
  263. }
  264. if (unlikely(sw_context->cur_query_bo != NULL)) {
  265. BUG_ON(!sw_context->query_cid_valid);
  266. add_cid = true;
  267. cid_to_add = sw_context->cur_query_cid;
  268. ret = vmw_bo_to_validate_list(sw_context,
  269. sw_context->cur_query_bo,
  270. DRM_VMW_FENCE_FLAG_EXEC,
  271. NULL);
  272. if (unlikely(ret != 0))
  273. return ret;
  274. }
  275. sw_context->cur_query_bo = new_query_bo;
  276. ret = vmw_bo_to_validate_list(sw_context,
  277. dev_priv->dummy_query_bo,
  278. DRM_VMW_FENCE_FLAG_EXEC,
  279. NULL);
  280. if (unlikely(ret != 0))
  281. return ret;
  282. }
  283. if (unlikely(cid != sw_context->cur_query_cid &&
  284. sw_context->query_cid_valid)) {
  285. add_cid = true;
  286. cid_to_add = sw_context->cur_query_cid;
  287. }
  288. sw_context->cur_query_cid = cid;
  289. sw_context->query_cid_valid = true;
  290. if (add_cid) {
  291. struct vmw_resource *ctx = sw_context->cur_ctx;
  292. if (list_empty(&ctx->query_head))
  293. list_add_tail(&ctx->query_head,
  294. &sw_context->query_list);
  295. ret = vmw_bo_to_validate_list(sw_context,
  296. dev_priv->dummy_query_bo,
  297. DRM_VMW_FENCE_FLAG_EXEC,
  298. NULL);
  299. if (unlikely(ret != 0))
  300. return ret;
  301. }
  302. return 0;
  303. }
  304. /**
  305. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  306. *
  307. * @dev_priv: The device private structure.
  308. * @sw_context: The software context used for this command submission batch.
  309. *
  310. * This function will check if we're switching query buffers, and will then,
  311. * if no other query waits are issued this command submission batch,
  312. * issue a dummy occlusion query wait used as a query barrier. When the fence
  313. * object following that query wait has signaled, we are sure that all
  314. * preseding queries have finished, and the old query buffer can be unpinned.
  315. * However, since both the new query buffer and the old one are fenced with
  316. * that fence, we can do an asynchronus unpin now, and be sure that the
  317. * old query buffer won't be moved until the fence has signaled.
  318. *
  319. * As mentioned above, both the new - and old query buffers need to be fenced
  320. * using a sequence emitted *after* calling this function.
  321. */
  322. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  323. struct vmw_sw_context *sw_context)
  324. {
  325. struct vmw_resource *ctx, *next_ctx;
  326. int ret;
  327. /*
  328. * The validate list should still hold references to all
  329. * contexts here.
  330. */
  331. list_for_each_entry_safe(ctx, next_ctx, &sw_context->query_list,
  332. query_head) {
  333. list_del_init(&ctx->query_head);
  334. BUG_ON(list_empty(&ctx->validate_head));
  335. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  336. if (unlikely(ret != 0))
  337. DRM_ERROR("Out of fifo space for dummy query.\n");
  338. }
  339. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  340. if (dev_priv->pinned_bo) {
  341. vmw_bo_pin(dev_priv->pinned_bo, false);
  342. ttm_bo_unref(&dev_priv->pinned_bo);
  343. }
  344. vmw_bo_pin(sw_context->cur_query_bo, true);
  345. /*
  346. * We pin also the dummy_query_bo buffer so that we
  347. * don't need to validate it when emitting
  348. * dummy queries in context destroy paths.
  349. */
  350. vmw_bo_pin(dev_priv->dummy_query_bo, true);
  351. dev_priv->dummy_query_bo_pinned = true;
  352. dev_priv->query_cid = sw_context->cur_query_cid;
  353. dev_priv->pinned_bo =
  354. ttm_bo_reference(sw_context->cur_query_bo);
  355. }
  356. }
  357. /**
  358. * vmw_query_switch_backoff - clear query barrier list
  359. * @sw_context: The sw context used for this submission batch.
  360. *
  361. * This function is used as part of an error path, where a previously
  362. * set up list of query barriers needs to be cleared.
  363. *
  364. */
  365. static void vmw_query_switch_backoff(struct vmw_sw_context *sw_context)
  366. {
  367. struct list_head *list, *next;
  368. list_for_each_safe(list, next, &sw_context->query_list) {
  369. list_del_init(list);
  370. }
  371. }
  372. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  373. struct vmw_sw_context *sw_context,
  374. SVGAGuestPtr *ptr,
  375. struct vmw_dma_buffer **vmw_bo_p)
  376. {
  377. struct vmw_dma_buffer *vmw_bo = NULL;
  378. struct ttm_buffer_object *bo;
  379. uint32_t handle = ptr->gmrId;
  380. struct vmw_relocation *reloc;
  381. int ret;
  382. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  383. if (unlikely(ret != 0)) {
  384. DRM_ERROR("Could not find or use GMR region.\n");
  385. return -EINVAL;
  386. }
  387. bo = &vmw_bo->base;
  388. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  389. DRM_ERROR("Max number relocations per submission"
  390. " exceeded\n");
  391. ret = -EINVAL;
  392. goto out_no_reloc;
  393. }
  394. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  395. reloc->location = ptr;
  396. ret = vmw_bo_to_validate_list(sw_context, bo, DRM_VMW_FENCE_FLAG_EXEC,
  397. &reloc->index);
  398. if (unlikely(ret != 0))
  399. goto out_no_reloc;
  400. *vmw_bo_p = vmw_bo;
  401. return 0;
  402. out_no_reloc:
  403. vmw_dmabuf_unreference(&vmw_bo);
  404. vmw_bo_p = NULL;
  405. return ret;
  406. }
  407. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  408. struct vmw_sw_context *sw_context,
  409. SVGA3dCmdHeader *header)
  410. {
  411. struct vmw_dma_buffer *vmw_bo;
  412. struct vmw_query_cmd {
  413. SVGA3dCmdHeader header;
  414. SVGA3dCmdEndQuery q;
  415. } *cmd;
  416. int ret;
  417. cmd = container_of(header, struct vmw_query_cmd, header);
  418. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  419. if (unlikely(ret != 0))
  420. return ret;
  421. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  422. &cmd->q.guestResult,
  423. &vmw_bo);
  424. if (unlikely(ret != 0))
  425. return ret;
  426. ret = vmw_query_bo_switch_prepare(dev_priv, cmd->q.cid,
  427. &vmw_bo->base, sw_context);
  428. vmw_dmabuf_unreference(&vmw_bo);
  429. return ret;
  430. }
  431. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  432. struct vmw_sw_context *sw_context,
  433. SVGA3dCmdHeader *header)
  434. {
  435. struct vmw_dma_buffer *vmw_bo;
  436. struct vmw_query_cmd {
  437. SVGA3dCmdHeader header;
  438. SVGA3dCmdWaitForQuery q;
  439. } *cmd;
  440. int ret;
  441. struct vmw_resource *ctx;
  442. cmd = container_of(header, struct vmw_query_cmd, header);
  443. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  444. if (unlikely(ret != 0))
  445. return ret;
  446. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  447. &cmd->q.guestResult,
  448. &vmw_bo);
  449. if (unlikely(ret != 0))
  450. return ret;
  451. vmw_dmabuf_unreference(&vmw_bo);
  452. /*
  453. * This wait will act as a barrier for previous waits for this
  454. * context.
  455. */
  456. ctx = sw_context->cur_ctx;
  457. if (!list_empty(&ctx->query_head))
  458. list_del_init(&ctx->query_head);
  459. return 0;
  460. }
  461. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  462. struct vmw_sw_context *sw_context,
  463. SVGA3dCmdHeader *header)
  464. {
  465. struct vmw_dma_buffer *vmw_bo = NULL;
  466. struct ttm_buffer_object *bo;
  467. struct vmw_surface *srf = NULL;
  468. struct vmw_dma_cmd {
  469. SVGA3dCmdHeader header;
  470. SVGA3dCmdSurfaceDMA dma;
  471. } *cmd;
  472. int ret;
  473. struct vmw_resource *res;
  474. cmd = container_of(header, struct vmw_dma_cmd, header);
  475. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  476. &cmd->dma.guest.ptr,
  477. &vmw_bo);
  478. if (unlikely(ret != 0))
  479. return ret;
  480. bo = &vmw_bo->base;
  481. ret = vmw_user_surface_lookup_handle(dev_priv, sw_context->tfile,
  482. cmd->dma.host.sid, &srf);
  483. if (ret) {
  484. DRM_ERROR("could not find surface\n");
  485. goto out_no_reloc;
  486. }
  487. ret = vmw_surface_validate(dev_priv, srf);
  488. if (unlikely(ret != 0)) {
  489. if (ret != -ERESTARTSYS)
  490. DRM_ERROR("Culd not validate surface.\n");
  491. goto out_no_validate;
  492. }
  493. /*
  494. * Patch command stream with device SID.
  495. */
  496. cmd->dma.host.sid = srf->res.id;
  497. vmw_kms_cursor_snoop(srf, sw_context->tfile, bo, header);
  498. vmw_dmabuf_unreference(&vmw_bo);
  499. res = &srf->res;
  500. vmw_resource_to_validate_list(sw_context, &res);
  501. return 0;
  502. out_no_validate:
  503. vmw_surface_unreference(&srf);
  504. out_no_reloc:
  505. vmw_dmabuf_unreference(&vmw_bo);
  506. return ret;
  507. }
  508. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  509. struct vmw_sw_context *sw_context,
  510. SVGA3dCmdHeader *header)
  511. {
  512. struct vmw_draw_cmd {
  513. SVGA3dCmdHeader header;
  514. SVGA3dCmdDrawPrimitives body;
  515. } *cmd;
  516. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  517. (unsigned long)header + sizeof(*cmd));
  518. SVGA3dPrimitiveRange *range;
  519. uint32_t i;
  520. uint32_t maxnum;
  521. int ret;
  522. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  523. if (unlikely(ret != 0))
  524. return ret;
  525. cmd = container_of(header, struct vmw_draw_cmd, header);
  526. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  527. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  528. DRM_ERROR("Illegal number of vertex declarations.\n");
  529. return -EINVAL;
  530. }
  531. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  532. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  533. &decl->array.surfaceId);
  534. if (unlikely(ret != 0))
  535. return ret;
  536. }
  537. maxnum = (header->size - sizeof(cmd->body) -
  538. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  539. if (unlikely(cmd->body.numRanges > maxnum)) {
  540. DRM_ERROR("Illegal number of index ranges.\n");
  541. return -EINVAL;
  542. }
  543. range = (SVGA3dPrimitiveRange *) decl;
  544. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  545. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  546. &range->indexArray.surfaceId);
  547. if (unlikely(ret != 0))
  548. return ret;
  549. }
  550. return 0;
  551. }
  552. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  553. struct vmw_sw_context *sw_context,
  554. SVGA3dCmdHeader *header)
  555. {
  556. struct vmw_tex_state_cmd {
  557. SVGA3dCmdHeader header;
  558. SVGA3dCmdSetTextureState state;
  559. };
  560. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  561. ((unsigned long) header + header->size + sizeof(header));
  562. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  563. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  564. int ret;
  565. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  566. if (unlikely(ret != 0))
  567. return ret;
  568. for (; cur_state < last_state; ++cur_state) {
  569. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  570. continue;
  571. ret = vmw_cmd_sid_check(dev_priv, sw_context,
  572. &cur_state->value);
  573. if (unlikely(ret != 0))
  574. return ret;
  575. }
  576. return 0;
  577. }
  578. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  579. struct vmw_sw_context *sw_context,
  580. void *buf)
  581. {
  582. struct vmw_dma_buffer *vmw_bo;
  583. int ret;
  584. struct {
  585. uint32_t header;
  586. SVGAFifoCmdDefineGMRFB body;
  587. } *cmd = buf;
  588. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  589. &cmd->body.ptr,
  590. &vmw_bo);
  591. if (unlikely(ret != 0))
  592. return ret;
  593. vmw_dmabuf_unreference(&vmw_bo);
  594. return ret;
  595. }
  596. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  597. struct vmw_sw_context *sw_context,
  598. void *buf, uint32_t *size)
  599. {
  600. uint32_t size_remaining = *size;
  601. uint32_t cmd_id;
  602. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  603. switch (cmd_id) {
  604. case SVGA_CMD_UPDATE:
  605. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  606. break;
  607. case SVGA_CMD_DEFINE_GMRFB:
  608. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  609. break;
  610. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  611. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  612. break;
  613. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  614. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  615. break;
  616. default:
  617. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  618. return -EINVAL;
  619. }
  620. if (*size > size_remaining) {
  621. DRM_ERROR("Invalid SVGA command (size mismatch):"
  622. " %u.\n", cmd_id);
  623. return -EINVAL;
  624. }
  625. if (unlikely(!sw_context->kernel)) {
  626. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  627. return -EPERM;
  628. }
  629. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  630. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  631. return 0;
  632. }
  633. typedef int (*vmw_cmd_func) (struct vmw_private *,
  634. struct vmw_sw_context *,
  635. SVGA3dCmdHeader *);
  636. #define VMW_CMD_DEF(cmd, func) \
  637. [cmd - SVGA_3D_CMD_BASE] = func
  638. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  639. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  640. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  641. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  642. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  643. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  644. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  645. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  646. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  647. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  648. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  649. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  650. &vmw_cmd_set_render_target_check),
  651. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  652. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  653. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  654. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  655. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  656. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  657. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  658. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  659. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  660. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  661. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_cid_check),
  662. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  663. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  664. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  665. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_cid_check),
  666. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
  667. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
  668. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  669. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  670. &vmw_cmd_blt_surf_screen_check)
  671. };
  672. static int vmw_cmd_check(struct vmw_private *dev_priv,
  673. struct vmw_sw_context *sw_context,
  674. void *buf, uint32_t *size)
  675. {
  676. uint32_t cmd_id;
  677. uint32_t size_remaining = *size;
  678. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  679. int ret;
  680. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  681. /* Handle any none 3D commands */
  682. if (unlikely(cmd_id < SVGA_CMD_MAX))
  683. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  684. cmd_id = le32_to_cpu(header->id);
  685. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  686. cmd_id -= SVGA_3D_CMD_BASE;
  687. if (unlikely(*size > size_remaining))
  688. goto out_err;
  689. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  690. goto out_err;
  691. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  692. if (unlikely(ret != 0))
  693. goto out_err;
  694. return 0;
  695. out_err:
  696. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  697. cmd_id + SVGA_3D_CMD_BASE);
  698. return -EINVAL;
  699. }
  700. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  701. struct vmw_sw_context *sw_context,
  702. void *buf,
  703. uint32_t size)
  704. {
  705. int32_t cur_size = size;
  706. int ret;
  707. while (cur_size > 0) {
  708. size = cur_size;
  709. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  710. if (unlikely(ret != 0))
  711. return ret;
  712. buf = (void *)((unsigned long) buf + size);
  713. cur_size -= size;
  714. }
  715. if (unlikely(cur_size != 0)) {
  716. DRM_ERROR("Command verifier out of sync.\n");
  717. return -EINVAL;
  718. }
  719. return 0;
  720. }
  721. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  722. {
  723. sw_context->cur_reloc = 0;
  724. }
  725. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  726. {
  727. uint32_t i;
  728. struct vmw_relocation *reloc;
  729. struct ttm_validate_buffer *validate;
  730. struct ttm_buffer_object *bo;
  731. for (i = 0; i < sw_context->cur_reloc; ++i) {
  732. reloc = &sw_context->relocs[i];
  733. validate = &sw_context->val_bufs[reloc->index];
  734. bo = validate->bo;
  735. if (bo->mem.mem_type == TTM_PL_VRAM) {
  736. reloc->location->offset += bo->offset;
  737. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  738. } else
  739. reloc->location->gmrId = bo->mem.start;
  740. }
  741. vmw_free_relocations(sw_context);
  742. }
  743. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  744. {
  745. struct ttm_validate_buffer *entry, *next;
  746. struct vmw_resource *res, *res_next;
  747. /*
  748. * Drop references to DMA buffers held during command submission.
  749. */
  750. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  751. head) {
  752. list_del(&entry->head);
  753. vmw_dmabuf_validate_clear(entry->bo);
  754. ttm_bo_unref(&entry->bo);
  755. sw_context->cur_val_buf--;
  756. }
  757. BUG_ON(sw_context->cur_val_buf != 0);
  758. /*
  759. * Drop references to resources held during command submission.
  760. */
  761. vmw_resource_unreserve(&sw_context->resource_list);
  762. list_for_each_entry_safe(res, res_next, &sw_context->resource_list,
  763. validate_head) {
  764. list_del_init(&res->validate_head);
  765. vmw_resource_unreference(&res);
  766. }
  767. }
  768. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  769. struct ttm_buffer_object *bo)
  770. {
  771. int ret;
  772. /*
  773. * Don't validate pinned buffers.
  774. */
  775. if (bo == dev_priv->pinned_bo ||
  776. (bo == dev_priv->dummy_query_bo &&
  777. dev_priv->dummy_query_bo_pinned))
  778. return 0;
  779. /**
  780. * Put BO in VRAM if there is space, otherwise as a GMR.
  781. * If there is no space in VRAM and GMR ids are all used up,
  782. * start evicting GMRs to make room. If the DMA buffer can't be
  783. * used as a GMR, this will return -ENOMEM.
  784. */
  785. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false, false);
  786. if (likely(ret == 0 || ret == -ERESTARTSYS))
  787. return ret;
  788. /**
  789. * If that failed, try VRAM again, this time evicting
  790. * previous contents.
  791. */
  792. DRM_INFO("Falling through to VRAM.\n");
  793. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false, false);
  794. return ret;
  795. }
  796. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  797. struct vmw_sw_context *sw_context)
  798. {
  799. struct ttm_validate_buffer *entry;
  800. int ret;
  801. list_for_each_entry(entry, &sw_context->validate_nodes, head) {
  802. ret = vmw_validate_single_buffer(dev_priv, entry->bo);
  803. if (unlikely(ret != 0))
  804. return ret;
  805. }
  806. return 0;
  807. }
  808. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  809. uint32_t size)
  810. {
  811. if (likely(sw_context->cmd_bounce_size >= size))
  812. return 0;
  813. if (sw_context->cmd_bounce_size == 0)
  814. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  815. while (sw_context->cmd_bounce_size < size) {
  816. sw_context->cmd_bounce_size =
  817. PAGE_ALIGN(sw_context->cmd_bounce_size +
  818. (sw_context->cmd_bounce_size >> 1));
  819. }
  820. if (sw_context->cmd_bounce != NULL)
  821. vfree(sw_context->cmd_bounce);
  822. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  823. if (sw_context->cmd_bounce == NULL) {
  824. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  825. sw_context->cmd_bounce_size = 0;
  826. return -ENOMEM;
  827. }
  828. return 0;
  829. }
  830. /**
  831. * vmw_execbuf_fence_commands - create and submit a command stream fence
  832. *
  833. * Creates a fence object and submits a command stream marker.
  834. * If this fails for some reason, We sync the fifo and return NULL.
  835. * It is then safe to fence buffers with a NULL pointer.
  836. *
  837. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  838. * a userspace handle if @p_handle is not NULL, otherwise not.
  839. */
  840. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  841. struct vmw_private *dev_priv,
  842. struct vmw_fence_obj **p_fence,
  843. uint32_t *p_handle)
  844. {
  845. uint32_t sequence;
  846. int ret;
  847. bool synced = false;
  848. /* p_handle implies file_priv. */
  849. BUG_ON(p_handle != NULL && file_priv == NULL);
  850. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  851. if (unlikely(ret != 0)) {
  852. DRM_ERROR("Fence submission error. Syncing.\n");
  853. synced = true;
  854. }
  855. if (p_handle != NULL)
  856. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  857. sequence,
  858. DRM_VMW_FENCE_FLAG_EXEC,
  859. p_fence, p_handle);
  860. else
  861. ret = vmw_fence_create(dev_priv->fman, sequence,
  862. DRM_VMW_FENCE_FLAG_EXEC,
  863. p_fence);
  864. if (unlikely(ret != 0 && !synced)) {
  865. (void) vmw_fallback_wait(dev_priv, false, false,
  866. sequence, false,
  867. VMW_FENCE_WAIT_TIMEOUT);
  868. *p_fence = NULL;
  869. }
  870. return 0;
  871. }
  872. /**
  873. * vmw_execbuf_copy_fence_user - copy fence object information to
  874. * user-space.
  875. *
  876. * @dev_priv: Pointer to a vmw_private struct.
  877. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  878. * @ret: Return value from fence object creation.
  879. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  880. * which the information should be copied.
  881. * @fence: Pointer to the fenc object.
  882. * @fence_handle: User-space fence handle.
  883. *
  884. * This function copies fence information to user-space. If copying fails,
  885. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  886. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  887. * the error will hopefully be detected.
  888. * Also if copying fails, user-space will be unable to signal the fence
  889. * object so we wait for it immediately, and then unreference the
  890. * user-space reference.
  891. */
  892. void
  893. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  894. struct vmw_fpriv *vmw_fp,
  895. int ret,
  896. struct drm_vmw_fence_rep __user *user_fence_rep,
  897. struct vmw_fence_obj *fence,
  898. uint32_t fence_handle)
  899. {
  900. struct drm_vmw_fence_rep fence_rep;
  901. if (user_fence_rep == NULL)
  902. return;
  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. }