vmwgfx_execbuf.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. struct vmw_fence_obj **out_fence)
  939. {
  940. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  941. struct vmw_fence_obj *fence = NULL;
  942. uint32_t handle;
  943. void *cmd;
  944. int ret;
  945. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  946. if (unlikely(ret != 0))
  947. return -ERESTARTSYS;
  948. if (kernel_commands == NULL) {
  949. sw_context->kernel = false;
  950. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  951. if (unlikely(ret != 0))
  952. goto out_unlock;
  953. ret = copy_from_user(sw_context->cmd_bounce,
  954. user_commands, command_size);
  955. if (unlikely(ret != 0)) {
  956. ret = -EFAULT;
  957. DRM_ERROR("Failed copying commands.\n");
  958. goto out_unlock;
  959. }
  960. kernel_commands = sw_context->cmd_bounce;
  961. } else
  962. sw_context->kernel = true;
  963. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  964. sw_context->cid_valid = false;
  965. sw_context->sid_valid = false;
  966. sw_context->cur_reloc = 0;
  967. sw_context->cur_val_buf = 0;
  968. sw_context->fence_flags = 0;
  969. INIT_LIST_HEAD(&sw_context->query_list);
  970. INIT_LIST_HEAD(&sw_context->resource_list);
  971. sw_context->cur_query_bo = dev_priv->pinned_bo;
  972. sw_context->cur_query_cid = dev_priv->query_cid;
  973. sw_context->query_cid_valid = (dev_priv->pinned_bo != NULL);
  974. INIT_LIST_HEAD(&sw_context->validate_nodes);
  975. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  976. command_size);
  977. if (unlikely(ret != 0))
  978. goto out_err;
  979. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes);
  980. if (unlikely(ret != 0))
  981. goto out_err;
  982. ret = vmw_validate_buffers(dev_priv, sw_context);
  983. if (unlikely(ret != 0))
  984. goto out_err;
  985. vmw_apply_relocations(sw_context);
  986. if (throttle_us) {
  987. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  988. throttle_us);
  989. if (unlikely(ret != 0))
  990. goto out_throttle;
  991. }
  992. cmd = vmw_fifo_reserve(dev_priv, command_size);
  993. if (unlikely(cmd == NULL)) {
  994. DRM_ERROR("Failed reserving fifo space for commands.\n");
  995. ret = -ENOMEM;
  996. goto out_throttle;
  997. }
  998. memcpy(cmd, kernel_commands, command_size);
  999. vmw_fifo_commit(dev_priv, command_size);
  1000. vmw_query_bo_switch_commit(dev_priv, sw_context);
  1001. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  1002. &fence,
  1003. (user_fence_rep) ? &handle : NULL);
  1004. /*
  1005. * This error is harmless, because if fence submission fails,
  1006. * vmw_fifo_send_fence will sync. The error will be propagated to
  1007. * user-space in @fence_rep
  1008. */
  1009. if (ret != 0)
  1010. DRM_ERROR("Fence submission error. Syncing.\n");
  1011. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  1012. (void *) fence);
  1013. vmw_clear_validations(sw_context);
  1014. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  1015. user_fence_rep, fence, handle);
  1016. /* Don't unreference when handing fence out */
  1017. if (unlikely(out_fence != NULL)) {
  1018. *out_fence = fence;
  1019. fence = NULL;
  1020. } else if (likely(fence != NULL)) {
  1021. vmw_fence_obj_unreference(&fence);
  1022. }
  1023. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1024. return 0;
  1025. out_err:
  1026. vmw_free_relocations(sw_context);
  1027. out_throttle:
  1028. vmw_query_switch_backoff(sw_context);
  1029. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  1030. vmw_clear_validations(sw_context);
  1031. out_unlock:
  1032. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1033. return ret;
  1034. }
  1035. /**
  1036. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  1037. *
  1038. * @dev_priv: The device private structure.
  1039. *
  1040. * This function is called to idle the fifo and unpin the query buffer
  1041. * if the normal way to do this hits an error, which should typically be
  1042. * extremely rare.
  1043. */
  1044. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  1045. {
  1046. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  1047. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  1048. vmw_bo_pin(dev_priv->pinned_bo, false);
  1049. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1050. dev_priv->dummy_query_bo_pinned = false;
  1051. }
  1052. /**
  1053. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1054. * query bo.
  1055. *
  1056. * @dev_priv: The device private structure.
  1057. * @only_on_cid_match: Only flush and unpin if the current active query cid
  1058. * matches @cid.
  1059. * @cid: Optional context id to match.
  1060. *
  1061. * This function should be used to unpin the pinned query bo, or
  1062. * as a query barrier when we need to make sure that all queries have
  1063. * finished before the next fifo command. (For example on hardware
  1064. * context destructions where the hardware may otherwise leak unfinished
  1065. * queries).
  1066. *
  1067. * This function does not return any failure codes, but make attempts
  1068. * to do safe unpinning in case of errors.
  1069. *
  1070. * The function will synchronize on the previous query barrier, and will
  1071. * thus not finish until that barrier has executed.
  1072. */
  1073. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  1074. bool only_on_cid_match, uint32_t cid)
  1075. {
  1076. int ret = 0;
  1077. struct list_head validate_list;
  1078. struct ttm_validate_buffer pinned_val, query_val;
  1079. struct vmw_fence_obj *fence;
  1080. mutex_lock(&dev_priv->cmdbuf_mutex);
  1081. if (dev_priv->pinned_bo == NULL)
  1082. goto out_unlock;
  1083. if (only_on_cid_match && cid != dev_priv->query_cid)
  1084. goto out_unlock;
  1085. INIT_LIST_HEAD(&validate_list);
  1086. pinned_val.new_sync_obj_arg = (void *)(unsigned long)
  1087. DRM_VMW_FENCE_FLAG_EXEC;
  1088. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  1089. list_add_tail(&pinned_val.head, &validate_list);
  1090. query_val.new_sync_obj_arg = pinned_val.new_sync_obj_arg;
  1091. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  1092. list_add_tail(&query_val.head, &validate_list);
  1093. do {
  1094. ret = ttm_eu_reserve_buffers(&validate_list);
  1095. } while (ret == -ERESTARTSYS);
  1096. if (unlikely(ret != 0)) {
  1097. vmw_execbuf_unpin_panic(dev_priv);
  1098. goto out_no_reserve;
  1099. }
  1100. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  1101. if (unlikely(ret != 0)) {
  1102. vmw_execbuf_unpin_panic(dev_priv);
  1103. goto out_no_emit;
  1104. }
  1105. vmw_bo_pin(dev_priv->pinned_bo, false);
  1106. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1107. dev_priv->dummy_query_bo_pinned = false;
  1108. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1109. ttm_eu_fence_buffer_objects(&validate_list, (void *) fence);
  1110. ttm_bo_unref(&query_val.bo);
  1111. ttm_bo_unref(&pinned_val.bo);
  1112. ttm_bo_unref(&dev_priv->pinned_bo);
  1113. out_unlock:
  1114. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1115. return;
  1116. out_no_emit:
  1117. ttm_eu_backoff_reservation(&validate_list);
  1118. out_no_reserve:
  1119. ttm_bo_unref(&query_val.bo);
  1120. ttm_bo_unref(&pinned_val.bo);
  1121. ttm_bo_unref(&dev_priv->pinned_bo);
  1122. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1123. }
  1124. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  1125. struct drm_file *file_priv)
  1126. {
  1127. struct vmw_private *dev_priv = vmw_priv(dev);
  1128. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  1129. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1130. int ret;
  1131. /*
  1132. * This will allow us to extend the ioctl argument while
  1133. * maintaining backwards compatibility:
  1134. * We take different code paths depending on the value of
  1135. * arg->version.
  1136. */
  1137. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  1138. DRM_ERROR("Incorrect execbuf version.\n");
  1139. DRM_ERROR("You're running outdated experimental "
  1140. "vmwgfx user-space drivers.");
  1141. return -EINVAL;
  1142. }
  1143. ret = ttm_read_lock(&vmaster->lock, true);
  1144. if (unlikely(ret != 0))
  1145. return ret;
  1146. ret = vmw_execbuf_process(file_priv, dev_priv,
  1147. (void __user *)(unsigned long)arg->commands,
  1148. NULL, arg->command_size, arg->throttle_us,
  1149. (void __user *)(unsigned long)arg->fence_rep,
  1150. NULL);
  1151. if (unlikely(ret != 0))
  1152. goto out_unlock;
  1153. vmw_kms_cursor_post_execbuf(dev_priv);
  1154. out_unlock:
  1155. ttm_read_unlock(&vmaster->lock);
  1156. return ret;
  1157. }