vmwgfx_execbuf.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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. memset(&fence_rep, 0, sizeof(fence_rep));
  904. fence_rep.error = ret;
  905. if (ret == 0) {
  906. BUG_ON(fence == NULL);
  907. fence_rep.handle = fence_handle;
  908. fence_rep.seqno = fence->seqno;
  909. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  910. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  911. }
  912. /*
  913. * copy_to_user errors will be detected by user space not
  914. * seeing fence_rep::error filled in. Typically
  915. * user-space would have pre-set that member to -EFAULT.
  916. */
  917. ret = copy_to_user(user_fence_rep, &fence_rep,
  918. sizeof(fence_rep));
  919. /*
  920. * User-space lost the fence object. We need to sync
  921. * and unreference the handle.
  922. */
  923. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  924. ttm_ref_object_base_unref(vmw_fp->tfile,
  925. fence_handle, TTM_REF_USAGE);
  926. DRM_ERROR("Fence copy error. Syncing.\n");
  927. (void) vmw_fence_obj_wait(fence, fence->signal_mask,
  928. false, false,
  929. VMW_FENCE_WAIT_TIMEOUT);
  930. }
  931. }
  932. int vmw_execbuf_process(struct drm_file *file_priv,
  933. struct vmw_private *dev_priv,
  934. void __user *user_commands,
  935. void *kernel_commands,
  936. uint32_t command_size,
  937. uint64_t throttle_us,
  938. struct drm_vmw_fence_rep __user *user_fence_rep)
  939. {
  940. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  941. struct vmw_fence_obj *fence;
  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. if (likely(fence != NULL))
  1017. vmw_fence_obj_unreference(&fence);
  1018. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1019. return 0;
  1020. out_err:
  1021. vmw_free_relocations(sw_context);
  1022. out_throttle:
  1023. vmw_query_switch_backoff(sw_context);
  1024. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  1025. vmw_clear_validations(sw_context);
  1026. out_unlock:
  1027. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1028. return ret;
  1029. }
  1030. /**
  1031. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  1032. *
  1033. * @dev_priv: The device private structure.
  1034. *
  1035. * This function is called to idle the fifo and unpin the query buffer
  1036. * if the normal way to do this hits an error, which should typically be
  1037. * extremely rare.
  1038. */
  1039. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  1040. {
  1041. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  1042. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  1043. vmw_bo_pin(dev_priv->pinned_bo, false);
  1044. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1045. dev_priv->dummy_query_bo_pinned = false;
  1046. }
  1047. /**
  1048. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1049. * query bo.
  1050. *
  1051. * @dev_priv: The device private structure.
  1052. * @only_on_cid_match: Only flush and unpin if the current active query cid
  1053. * matches @cid.
  1054. * @cid: Optional context id to match.
  1055. *
  1056. * This function should be used to unpin the pinned query bo, or
  1057. * as a query barrier when we need to make sure that all queries have
  1058. * finished before the next fifo command. (For example on hardware
  1059. * context destructions where the hardware may otherwise leak unfinished
  1060. * queries).
  1061. *
  1062. * This function does not return any failure codes, but make attempts
  1063. * to do safe unpinning in case of errors.
  1064. *
  1065. * The function will synchronize on the previous query barrier, and will
  1066. * thus not finish until that barrier has executed.
  1067. */
  1068. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  1069. bool only_on_cid_match, uint32_t cid)
  1070. {
  1071. int ret = 0;
  1072. struct list_head validate_list;
  1073. struct ttm_validate_buffer pinned_val, query_val;
  1074. struct vmw_fence_obj *fence;
  1075. mutex_lock(&dev_priv->cmdbuf_mutex);
  1076. if (dev_priv->pinned_bo == NULL)
  1077. goto out_unlock;
  1078. if (only_on_cid_match && cid != dev_priv->query_cid)
  1079. goto out_unlock;
  1080. INIT_LIST_HEAD(&validate_list);
  1081. pinned_val.new_sync_obj_arg = (void *)(unsigned long)
  1082. DRM_VMW_FENCE_FLAG_EXEC;
  1083. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  1084. list_add_tail(&pinned_val.head, &validate_list);
  1085. query_val.new_sync_obj_arg = pinned_val.new_sync_obj_arg;
  1086. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  1087. list_add_tail(&query_val.head, &validate_list);
  1088. do {
  1089. ret = ttm_eu_reserve_buffers(&validate_list);
  1090. } while (ret == -ERESTARTSYS);
  1091. if (unlikely(ret != 0)) {
  1092. vmw_execbuf_unpin_panic(dev_priv);
  1093. goto out_no_reserve;
  1094. }
  1095. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  1096. if (unlikely(ret != 0)) {
  1097. vmw_execbuf_unpin_panic(dev_priv);
  1098. goto out_no_emit;
  1099. }
  1100. vmw_bo_pin(dev_priv->pinned_bo, false);
  1101. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1102. dev_priv->dummy_query_bo_pinned = false;
  1103. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1104. ttm_eu_fence_buffer_objects(&validate_list, (void *) fence);
  1105. ttm_bo_unref(&query_val.bo);
  1106. ttm_bo_unref(&pinned_val.bo);
  1107. ttm_bo_unref(&dev_priv->pinned_bo);
  1108. out_unlock:
  1109. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1110. return;
  1111. out_no_emit:
  1112. ttm_eu_backoff_reservation(&validate_list);
  1113. out_no_reserve:
  1114. ttm_bo_unref(&query_val.bo);
  1115. ttm_bo_unref(&pinned_val.bo);
  1116. ttm_bo_unref(&dev_priv->pinned_bo);
  1117. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1118. }
  1119. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  1120. struct drm_file *file_priv)
  1121. {
  1122. struct vmw_private *dev_priv = vmw_priv(dev);
  1123. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  1124. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1125. int ret;
  1126. /*
  1127. * This will allow us to extend the ioctl argument while
  1128. * maintaining backwards compatibility:
  1129. * We take different code paths depending on the value of
  1130. * arg->version.
  1131. */
  1132. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  1133. DRM_ERROR("Incorrect execbuf version.\n");
  1134. DRM_ERROR("You're running outdated experimental "
  1135. "vmwgfx user-space drivers.");
  1136. return -EINVAL;
  1137. }
  1138. ret = ttm_read_lock(&vmaster->lock, true);
  1139. if (unlikely(ret != 0))
  1140. return ret;
  1141. ret = vmw_execbuf_process(file_priv, dev_priv,
  1142. (void __user *)(unsigned long)arg->commands,
  1143. NULL, arg->command_size, arg->throttle_us,
  1144. (void __user *)(unsigned long)arg->fence_rep);
  1145. if (unlikely(ret != 0))
  1146. goto out_unlock;
  1147. vmw_kms_cursor_post_execbuf(dev_priv);
  1148. out_unlock:
  1149. ttm_read_unlock(&vmaster->lock);
  1150. return ret;
  1151. }