vmwgfx_execbuf.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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 <drm/ttm/ttm_bo_api.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #define VMW_RES_HT_ORDER 12
  32. /**
  33. * struct vmw_resource_relocation - Relocation info for resources
  34. *
  35. * @head: List head for the software context's relocation list.
  36. * @res: Non-ref-counted pointer to the resource.
  37. * @offset: Offset of 4 byte entries into the command buffer where the
  38. * id that needs fixup is located.
  39. */
  40. struct vmw_resource_relocation {
  41. struct list_head head;
  42. const struct vmw_resource *res;
  43. unsigned long offset;
  44. };
  45. /**
  46. * struct vmw_resource_val_node - Validation info for resources
  47. *
  48. * @head: List head for the software context's resource list.
  49. * @hash: Hash entry for quick resouce to val_node lookup.
  50. * @res: Ref-counted pointer to the resource.
  51. * @switch_backup: Boolean whether to switch backup buffer on unreserve.
  52. * @new_backup: Refcounted pointer to the new backup buffer.
  53. * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
  54. * @first_usage: Set to true the first time the resource is referenced in
  55. * the command stream.
  56. * @no_buffer_needed: Resources do not need to allocate buffer backup on
  57. * reservation. The command stream will provide one.
  58. */
  59. struct vmw_resource_val_node {
  60. struct list_head head;
  61. struct drm_hash_item hash;
  62. struct vmw_resource *res;
  63. struct vmw_dma_buffer *new_backup;
  64. unsigned long new_backup_offset;
  65. bool first_usage;
  66. bool no_buffer_needed;
  67. };
  68. /**
  69. * vmw_resource_unreserve - unreserve resources previously reserved for
  70. * command submission.
  71. *
  72. * @list_head: list of resources to unreserve.
  73. * @backoff: Whether command submission failed.
  74. */
  75. static void vmw_resource_list_unreserve(struct list_head *list,
  76. bool backoff)
  77. {
  78. struct vmw_resource_val_node *val;
  79. list_for_each_entry(val, list, head) {
  80. struct vmw_resource *res = val->res;
  81. struct vmw_dma_buffer *new_backup =
  82. backoff ? NULL : val->new_backup;
  83. vmw_resource_unreserve(res, new_backup,
  84. val->new_backup_offset);
  85. vmw_dmabuf_unreference(&val->new_backup);
  86. }
  87. }
  88. /**
  89. * vmw_resource_val_add - Add a resource to the software context's
  90. * resource list if it's not already on it.
  91. *
  92. * @sw_context: Pointer to the software context.
  93. * @res: Pointer to the resource.
  94. * @p_node On successful return points to a valid pointer to a
  95. * struct vmw_resource_val_node, if non-NULL on entry.
  96. */
  97. static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
  98. struct vmw_resource *res,
  99. struct vmw_resource_val_node **p_node)
  100. {
  101. struct vmw_resource_val_node *node;
  102. struct drm_hash_item *hash;
  103. int ret;
  104. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
  105. &hash) == 0)) {
  106. node = container_of(hash, struct vmw_resource_val_node, hash);
  107. node->first_usage = false;
  108. if (unlikely(p_node != NULL))
  109. *p_node = node;
  110. return 0;
  111. }
  112. node = kzalloc(sizeof(*node), GFP_KERNEL);
  113. if (unlikely(node == NULL)) {
  114. DRM_ERROR("Failed to allocate a resource validation "
  115. "entry.\n");
  116. return -ENOMEM;
  117. }
  118. node->hash.key = (unsigned long) res;
  119. ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
  120. if (unlikely(ret != 0)) {
  121. DRM_ERROR("Failed to initialize a resource validation "
  122. "entry.\n");
  123. kfree(node);
  124. return ret;
  125. }
  126. list_add_tail(&node->head, &sw_context->resource_list);
  127. node->res = vmw_resource_reference(res);
  128. node->first_usage = true;
  129. if (unlikely(p_node != NULL))
  130. *p_node = node;
  131. return 0;
  132. }
  133. /**
  134. * vmw_resource_relocation_add - Add a relocation to the relocation list
  135. *
  136. * @list: Pointer to head of relocation list.
  137. * @res: The resource.
  138. * @offset: Offset into the command buffer currently being parsed where the
  139. * id that needs fixup is located. Granularity is 4 bytes.
  140. */
  141. static int vmw_resource_relocation_add(struct list_head *list,
  142. const struct vmw_resource *res,
  143. unsigned long offset)
  144. {
  145. struct vmw_resource_relocation *rel;
  146. rel = kmalloc(sizeof(*rel), GFP_KERNEL);
  147. if (unlikely(rel == NULL)) {
  148. DRM_ERROR("Failed to allocate a resource relocation.\n");
  149. return -ENOMEM;
  150. }
  151. rel->res = res;
  152. rel->offset = offset;
  153. list_add_tail(&rel->head, list);
  154. return 0;
  155. }
  156. /**
  157. * vmw_resource_relocations_free - Free all relocations on a list
  158. *
  159. * @list: Pointer to the head of the relocation list.
  160. */
  161. static void vmw_resource_relocations_free(struct list_head *list)
  162. {
  163. struct vmw_resource_relocation *rel, *n;
  164. list_for_each_entry_safe(rel, n, list, head) {
  165. list_del(&rel->head);
  166. kfree(rel);
  167. }
  168. }
  169. /**
  170. * vmw_resource_relocations_apply - Apply all relocations on a list
  171. *
  172. * @cb: Pointer to the start of the command buffer bein patch. This need
  173. * not be the same buffer as the one being parsed when the relocation
  174. * list was built, but the contents must be the same modulo the
  175. * resource ids.
  176. * @list: Pointer to the head of the relocation list.
  177. */
  178. static void vmw_resource_relocations_apply(uint32_t *cb,
  179. struct list_head *list)
  180. {
  181. struct vmw_resource_relocation *rel;
  182. list_for_each_entry(rel, list, head)
  183. cb[rel->offset] = rel->res->id;
  184. }
  185. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  186. struct vmw_sw_context *sw_context,
  187. SVGA3dCmdHeader *header)
  188. {
  189. return capable(CAP_SYS_ADMIN) ? : -EINVAL;
  190. }
  191. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  192. struct vmw_sw_context *sw_context,
  193. SVGA3dCmdHeader *header)
  194. {
  195. return 0;
  196. }
  197. /**
  198. * vmw_bo_to_validate_list - add a bo to a validate list
  199. *
  200. * @sw_context: The software context used for this command submission batch.
  201. * @bo: The buffer object to add.
  202. * @p_val_node: If non-NULL Will be updated with the validate node number
  203. * on return.
  204. *
  205. * Returns -EINVAL if the limit of number of buffer objects per command
  206. * submission is reached.
  207. */
  208. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  209. struct ttm_buffer_object *bo,
  210. uint32_t *p_val_node)
  211. {
  212. uint32_t val_node;
  213. struct vmw_validate_buffer *vval_buf;
  214. struct ttm_validate_buffer *val_buf;
  215. struct drm_hash_item *hash;
  216. int ret;
  217. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) bo,
  218. &hash) == 0)) {
  219. vval_buf = container_of(hash, struct vmw_validate_buffer,
  220. hash);
  221. val_buf = &vval_buf->base;
  222. val_node = vval_buf - sw_context->val_bufs;
  223. } else {
  224. val_node = sw_context->cur_val_buf;
  225. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  226. DRM_ERROR("Max number of DMA buffers per submission "
  227. "exceeded.\n");
  228. return -EINVAL;
  229. }
  230. vval_buf = &sw_context->val_bufs[val_node];
  231. vval_buf->hash.key = (unsigned long) bo;
  232. ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
  233. if (unlikely(ret != 0)) {
  234. DRM_ERROR("Failed to initialize a buffer validation "
  235. "entry.\n");
  236. return ret;
  237. }
  238. ++sw_context->cur_val_buf;
  239. val_buf = &vval_buf->base;
  240. val_buf->bo = ttm_bo_reference(bo);
  241. val_buf->reserved = false;
  242. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  243. }
  244. sw_context->fence_flags |= DRM_VMW_FENCE_FLAG_EXEC;
  245. if (p_val_node)
  246. *p_val_node = val_node;
  247. return 0;
  248. }
  249. /**
  250. * vmw_resources_reserve - Reserve all resources on the sw_context's
  251. * resource list.
  252. *
  253. * @sw_context: Pointer to the software context.
  254. *
  255. * Note that since vmware's command submission currently is protected by
  256. * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
  257. * since only a single thread at once will attempt this.
  258. */
  259. static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
  260. {
  261. struct vmw_resource_val_node *val;
  262. int ret;
  263. list_for_each_entry(val, &sw_context->resource_list, head) {
  264. struct vmw_resource *res = val->res;
  265. ret = vmw_resource_reserve(res, val->no_buffer_needed);
  266. if (unlikely(ret != 0))
  267. return ret;
  268. if (res->backup) {
  269. struct ttm_buffer_object *bo = &res->backup->base;
  270. ret = vmw_bo_to_validate_list
  271. (sw_context, bo, NULL);
  272. if (unlikely(ret != 0))
  273. return ret;
  274. }
  275. }
  276. return 0;
  277. }
  278. /**
  279. * vmw_resources_validate - Validate all resources on the sw_context's
  280. * resource list.
  281. *
  282. * @sw_context: Pointer to the software context.
  283. *
  284. * Before this function is called, all resource backup buffers must have
  285. * been validated.
  286. */
  287. static int vmw_resources_validate(struct vmw_sw_context *sw_context)
  288. {
  289. struct vmw_resource_val_node *val;
  290. int ret;
  291. list_for_each_entry(val, &sw_context->resource_list, head) {
  292. struct vmw_resource *res = val->res;
  293. ret = vmw_resource_validate(res);
  294. if (unlikely(ret != 0)) {
  295. if (ret != -ERESTARTSYS)
  296. DRM_ERROR("Failed to validate resource.\n");
  297. return ret;
  298. }
  299. }
  300. return 0;
  301. }
  302. /**
  303. * vmw_cmd_res_check - Check that a resource is present and if so, put it
  304. * on the resource validate list unless it's already there.
  305. *
  306. * @dev_priv: Pointer to a device private structure.
  307. * @sw_context: Pointer to the software context.
  308. * @res_type: Resource type.
  309. * @converter: User-space visisble type specific information.
  310. * @id: Pointer to the location in the command buffer currently being
  311. * parsed from where the user-space resource id handle is located.
  312. */
  313. static int vmw_cmd_res_check(struct vmw_private *dev_priv,
  314. struct vmw_sw_context *sw_context,
  315. enum vmw_res_type res_type,
  316. const struct vmw_user_resource_conv *converter,
  317. uint32_t *id,
  318. struct vmw_resource_val_node **p_val)
  319. {
  320. struct vmw_res_cache_entry *rcache =
  321. &sw_context->res_cache[res_type];
  322. struct vmw_resource *res;
  323. struct vmw_resource_val_node *node;
  324. int ret;
  325. if (*id == SVGA3D_INVALID_ID)
  326. return 0;
  327. /*
  328. * Fastpath in case of repeated commands referencing the same
  329. * resource
  330. */
  331. if (likely(rcache->valid && *id == rcache->handle)) {
  332. const struct vmw_resource *res = rcache->res;
  333. rcache->node->first_usage = false;
  334. if (p_val)
  335. *p_val = rcache->node;
  336. return vmw_resource_relocation_add
  337. (&sw_context->res_relocations, res,
  338. id - sw_context->buf_start);
  339. }
  340. ret = vmw_user_resource_lookup_handle(dev_priv,
  341. sw_context->tfile,
  342. *id,
  343. converter,
  344. &res);
  345. if (unlikely(ret != 0)) {
  346. DRM_ERROR("Could not find or use resource 0x%08x.\n",
  347. (unsigned) *id);
  348. dump_stack();
  349. return ret;
  350. }
  351. rcache->valid = true;
  352. rcache->res = res;
  353. rcache->handle = *id;
  354. ret = vmw_resource_relocation_add(&sw_context->res_relocations,
  355. res,
  356. id - sw_context->buf_start);
  357. if (unlikely(ret != 0))
  358. goto out_no_reloc;
  359. ret = vmw_resource_val_add(sw_context, res, &node);
  360. if (unlikely(ret != 0))
  361. goto out_no_reloc;
  362. rcache->node = node;
  363. if (p_val)
  364. *p_val = node;
  365. vmw_resource_unreference(&res);
  366. return 0;
  367. out_no_reloc:
  368. BUG_ON(sw_context->error_resource != NULL);
  369. sw_context->error_resource = res;
  370. return ret;
  371. }
  372. /**
  373. * vmw_cmd_cid_check - Check a command header for valid context information.
  374. *
  375. * @dev_priv: Pointer to a device private structure.
  376. * @sw_context: Pointer to the software context.
  377. * @header: A command header with an embedded user-space context handle.
  378. *
  379. * Convenience function: Call vmw_cmd_res_check with the user-space context
  380. * handle embedded in @header.
  381. */
  382. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  383. struct vmw_sw_context *sw_context,
  384. SVGA3dCmdHeader *header)
  385. {
  386. struct vmw_cid_cmd {
  387. SVGA3dCmdHeader header;
  388. __le32 cid;
  389. } *cmd;
  390. cmd = container_of(header, struct vmw_cid_cmd, header);
  391. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  392. user_context_converter, &cmd->cid, NULL);
  393. }
  394. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  395. struct vmw_sw_context *sw_context,
  396. SVGA3dCmdHeader *header)
  397. {
  398. struct vmw_sid_cmd {
  399. SVGA3dCmdHeader header;
  400. SVGA3dCmdSetRenderTarget body;
  401. } *cmd;
  402. int ret;
  403. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  404. if (unlikely(ret != 0))
  405. return ret;
  406. cmd = container_of(header, struct vmw_sid_cmd, header);
  407. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  408. user_surface_converter,
  409. &cmd->body.target.sid, NULL);
  410. return ret;
  411. }
  412. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  413. struct vmw_sw_context *sw_context,
  414. SVGA3dCmdHeader *header)
  415. {
  416. struct vmw_sid_cmd {
  417. SVGA3dCmdHeader header;
  418. SVGA3dCmdSurfaceCopy body;
  419. } *cmd;
  420. int ret;
  421. cmd = container_of(header, struct vmw_sid_cmd, header);
  422. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  423. user_surface_converter,
  424. &cmd->body.src.sid, NULL);
  425. if (unlikely(ret != 0))
  426. return ret;
  427. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  428. user_surface_converter,
  429. &cmd->body.dest.sid, NULL);
  430. }
  431. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  432. struct vmw_sw_context *sw_context,
  433. SVGA3dCmdHeader *header)
  434. {
  435. struct vmw_sid_cmd {
  436. SVGA3dCmdHeader header;
  437. SVGA3dCmdSurfaceStretchBlt body;
  438. } *cmd;
  439. int ret;
  440. cmd = container_of(header, struct vmw_sid_cmd, header);
  441. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  442. user_surface_converter,
  443. &cmd->body.src.sid, NULL);
  444. if (unlikely(ret != 0))
  445. return ret;
  446. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  447. user_surface_converter,
  448. &cmd->body.dest.sid, NULL);
  449. }
  450. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  451. struct vmw_sw_context *sw_context,
  452. SVGA3dCmdHeader *header)
  453. {
  454. struct vmw_sid_cmd {
  455. SVGA3dCmdHeader header;
  456. SVGA3dCmdBlitSurfaceToScreen body;
  457. } *cmd;
  458. cmd = container_of(header, struct vmw_sid_cmd, header);
  459. if (unlikely(!sw_context->kernel)) {
  460. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  461. return -EPERM;
  462. }
  463. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  464. user_surface_converter,
  465. &cmd->body.srcImage.sid, NULL);
  466. }
  467. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  468. struct vmw_sw_context *sw_context,
  469. SVGA3dCmdHeader *header)
  470. {
  471. struct vmw_sid_cmd {
  472. SVGA3dCmdHeader header;
  473. SVGA3dCmdPresent body;
  474. } *cmd;
  475. cmd = container_of(header, struct vmw_sid_cmd, header);
  476. if (unlikely(!sw_context->kernel)) {
  477. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  478. return -EPERM;
  479. }
  480. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  481. user_surface_converter, &cmd->body.sid,
  482. NULL);
  483. }
  484. /**
  485. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  486. *
  487. * @dev_priv: The device private structure.
  488. * @new_query_bo: The new buffer holding query results.
  489. * @sw_context: The software context used for this command submission.
  490. *
  491. * This function checks whether @new_query_bo is suitable for holding
  492. * query results, and if another buffer currently is pinned for query
  493. * results. If so, the function prepares the state of @sw_context for
  494. * switching pinned buffers after successful submission of the current
  495. * command batch.
  496. */
  497. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  498. struct ttm_buffer_object *new_query_bo,
  499. struct vmw_sw_context *sw_context)
  500. {
  501. struct vmw_res_cache_entry *ctx_entry =
  502. &sw_context->res_cache[vmw_res_context];
  503. int ret;
  504. BUG_ON(!ctx_entry->valid);
  505. sw_context->last_query_ctx = ctx_entry->res;
  506. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  507. if (unlikely(new_query_bo->num_pages > 4)) {
  508. DRM_ERROR("Query buffer too large.\n");
  509. return -EINVAL;
  510. }
  511. if (unlikely(sw_context->cur_query_bo != NULL)) {
  512. sw_context->needs_post_query_barrier = true;
  513. ret = vmw_bo_to_validate_list(sw_context,
  514. sw_context->cur_query_bo,
  515. NULL);
  516. if (unlikely(ret != 0))
  517. return ret;
  518. }
  519. sw_context->cur_query_bo = new_query_bo;
  520. ret = vmw_bo_to_validate_list(sw_context,
  521. dev_priv->dummy_query_bo,
  522. NULL);
  523. if (unlikely(ret != 0))
  524. return ret;
  525. }
  526. return 0;
  527. }
  528. /**
  529. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  530. *
  531. * @dev_priv: The device private structure.
  532. * @sw_context: The software context used for this command submission batch.
  533. *
  534. * This function will check if we're switching query buffers, and will then,
  535. * issue a dummy occlusion query wait used as a query barrier. When the fence
  536. * object following that query wait has signaled, we are sure that all
  537. * preceding queries have finished, and the old query buffer can be unpinned.
  538. * However, since both the new query buffer and the old one are fenced with
  539. * that fence, we can do an asynchronus unpin now, and be sure that the
  540. * old query buffer won't be moved until the fence has signaled.
  541. *
  542. * As mentioned above, both the new - and old query buffers need to be fenced
  543. * using a sequence emitted *after* calling this function.
  544. */
  545. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  546. struct vmw_sw_context *sw_context)
  547. {
  548. /*
  549. * The validate list should still hold references to all
  550. * contexts here.
  551. */
  552. if (sw_context->needs_post_query_barrier) {
  553. struct vmw_res_cache_entry *ctx_entry =
  554. &sw_context->res_cache[vmw_res_context];
  555. struct vmw_resource *ctx;
  556. int ret;
  557. BUG_ON(!ctx_entry->valid);
  558. ctx = ctx_entry->res;
  559. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  560. if (unlikely(ret != 0))
  561. DRM_ERROR("Out of fifo space for dummy query.\n");
  562. }
  563. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  564. if (dev_priv->pinned_bo) {
  565. vmw_bo_pin(dev_priv->pinned_bo, false);
  566. ttm_bo_unref(&dev_priv->pinned_bo);
  567. }
  568. if (!sw_context->needs_post_query_barrier) {
  569. vmw_bo_pin(sw_context->cur_query_bo, true);
  570. /*
  571. * We pin also the dummy_query_bo buffer so that we
  572. * don't need to validate it when emitting
  573. * dummy queries in context destroy paths.
  574. */
  575. vmw_bo_pin(dev_priv->dummy_query_bo, true);
  576. dev_priv->dummy_query_bo_pinned = true;
  577. BUG_ON(sw_context->last_query_ctx == NULL);
  578. dev_priv->query_cid = sw_context->last_query_ctx->id;
  579. dev_priv->query_cid_valid = true;
  580. dev_priv->pinned_bo =
  581. ttm_bo_reference(sw_context->cur_query_bo);
  582. }
  583. }
  584. }
  585. /**
  586. * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
  587. * handle to a valid SVGAGuestPtr
  588. *
  589. * @dev_priv: Pointer to a device private structure.
  590. * @sw_context: The software context used for this command batch validation.
  591. * @ptr: Pointer to the user-space handle to be translated.
  592. * @vmw_bo_p: Points to a location that, on successful return will carry
  593. * a reference-counted pointer to the DMA buffer identified by the
  594. * user-space handle in @id.
  595. *
  596. * This function saves information needed to translate a user-space buffer
  597. * handle to a valid SVGAGuestPtr. The translation does not take place
  598. * immediately, but during a call to vmw_apply_relocations().
  599. * This function builds a relocation list and a list of buffers to validate.
  600. * The former needs to be freed using either vmw_apply_relocations() or
  601. * vmw_free_relocations(). The latter needs to be freed using
  602. * vmw_clear_validations.
  603. */
  604. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  605. struct vmw_sw_context *sw_context,
  606. SVGAGuestPtr *ptr,
  607. struct vmw_dma_buffer **vmw_bo_p)
  608. {
  609. struct vmw_dma_buffer *vmw_bo = NULL;
  610. struct ttm_buffer_object *bo;
  611. uint32_t handle = ptr->gmrId;
  612. struct vmw_relocation *reloc;
  613. int ret;
  614. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  615. if (unlikely(ret != 0)) {
  616. DRM_ERROR("Could not find or use GMR region.\n");
  617. return -EINVAL;
  618. }
  619. bo = &vmw_bo->base;
  620. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  621. DRM_ERROR("Max number relocations per submission"
  622. " exceeded\n");
  623. ret = -EINVAL;
  624. goto out_no_reloc;
  625. }
  626. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  627. reloc->location = ptr;
  628. ret = vmw_bo_to_validate_list(sw_context, bo, &reloc->index);
  629. if (unlikely(ret != 0))
  630. goto out_no_reloc;
  631. *vmw_bo_p = vmw_bo;
  632. return 0;
  633. out_no_reloc:
  634. vmw_dmabuf_unreference(&vmw_bo);
  635. vmw_bo_p = NULL;
  636. return ret;
  637. }
  638. /**
  639. * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
  640. *
  641. * @dev_priv: Pointer to a device private struct.
  642. * @sw_context: The software context used for this command submission.
  643. * @header: Pointer to the command header in the command stream.
  644. */
  645. static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
  646. struct vmw_sw_context *sw_context,
  647. SVGA3dCmdHeader *header)
  648. {
  649. struct vmw_begin_query_cmd {
  650. SVGA3dCmdHeader header;
  651. SVGA3dCmdBeginQuery q;
  652. } *cmd;
  653. cmd = container_of(header, struct vmw_begin_query_cmd,
  654. header);
  655. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  656. user_context_converter, &cmd->q.cid,
  657. NULL);
  658. }
  659. /**
  660. * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
  661. *
  662. * @dev_priv: Pointer to a device private struct.
  663. * @sw_context: The software context used for this command submission.
  664. * @header: Pointer to the command header in the command stream.
  665. */
  666. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  667. struct vmw_sw_context *sw_context,
  668. SVGA3dCmdHeader *header)
  669. {
  670. struct vmw_dma_buffer *vmw_bo;
  671. struct vmw_query_cmd {
  672. SVGA3dCmdHeader header;
  673. SVGA3dCmdEndQuery q;
  674. } *cmd;
  675. int ret;
  676. cmd = container_of(header, struct vmw_query_cmd, header);
  677. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  678. if (unlikely(ret != 0))
  679. return ret;
  680. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  681. &cmd->q.guestResult,
  682. &vmw_bo);
  683. if (unlikely(ret != 0))
  684. return ret;
  685. ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
  686. vmw_dmabuf_unreference(&vmw_bo);
  687. return ret;
  688. }
  689. /*
  690. * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
  691. *
  692. * @dev_priv: Pointer to a device private struct.
  693. * @sw_context: The software context used for this command submission.
  694. * @header: Pointer to the command header in the command stream.
  695. */
  696. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  697. struct vmw_sw_context *sw_context,
  698. SVGA3dCmdHeader *header)
  699. {
  700. struct vmw_dma_buffer *vmw_bo;
  701. struct vmw_query_cmd {
  702. SVGA3dCmdHeader header;
  703. SVGA3dCmdWaitForQuery q;
  704. } *cmd;
  705. int ret;
  706. cmd = container_of(header, struct vmw_query_cmd, header);
  707. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  708. if (unlikely(ret != 0))
  709. return ret;
  710. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  711. &cmd->q.guestResult,
  712. &vmw_bo);
  713. if (unlikely(ret != 0))
  714. return ret;
  715. vmw_dmabuf_unreference(&vmw_bo);
  716. return 0;
  717. }
  718. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  719. struct vmw_sw_context *sw_context,
  720. SVGA3dCmdHeader *header)
  721. {
  722. struct vmw_dma_buffer *vmw_bo = NULL;
  723. struct vmw_surface *srf = NULL;
  724. struct vmw_dma_cmd {
  725. SVGA3dCmdHeader header;
  726. SVGA3dCmdSurfaceDMA dma;
  727. } *cmd;
  728. int ret;
  729. cmd = container_of(header, struct vmw_dma_cmd, header);
  730. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  731. &cmd->dma.guest.ptr,
  732. &vmw_bo);
  733. if (unlikely(ret != 0))
  734. return ret;
  735. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  736. user_surface_converter, &cmd->dma.host.sid,
  737. NULL);
  738. if (unlikely(ret != 0)) {
  739. if (unlikely(ret != -ERESTARTSYS))
  740. DRM_ERROR("could not find surface for DMA.\n");
  741. goto out_no_surface;
  742. }
  743. srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
  744. vmw_kms_cursor_snoop(srf, sw_context->tfile, &vmw_bo->base, header);
  745. out_no_surface:
  746. vmw_dmabuf_unreference(&vmw_bo);
  747. return ret;
  748. }
  749. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  750. struct vmw_sw_context *sw_context,
  751. SVGA3dCmdHeader *header)
  752. {
  753. struct vmw_draw_cmd {
  754. SVGA3dCmdHeader header;
  755. SVGA3dCmdDrawPrimitives body;
  756. } *cmd;
  757. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  758. (unsigned long)header + sizeof(*cmd));
  759. SVGA3dPrimitiveRange *range;
  760. uint32_t i;
  761. uint32_t maxnum;
  762. int ret;
  763. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  764. if (unlikely(ret != 0))
  765. return ret;
  766. cmd = container_of(header, struct vmw_draw_cmd, header);
  767. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  768. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  769. DRM_ERROR("Illegal number of vertex declarations.\n");
  770. return -EINVAL;
  771. }
  772. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  773. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  774. user_surface_converter,
  775. &decl->array.surfaceId, NULL);
  776. if (unlikely(ret != 0))
  777. return ret;
  778. }
  779. maxnum = (header->size - sizeof(cmd->body) -
  780. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  781. if (unlikely(cmd->body.numRanges > maxnum)) {
  782. DRM_ERROR("Illegal number of index ranges.\n");
  783. return -EINVAL;
  784. }
  785. range = (SVGA3dPrimitiveRange *) decl;
  786. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  787. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  788. user_surface_converter,
  789. &range->indexArray.surfaceId, NULL);
  790. if (unlikely(ret != 0))
  791. return ret;
  792. }
  793. return 0;
  794. }
  795. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  796. struct vmw_sw_context *sw_context,
  797. SVGA3dCmdHeader *header)
  798. {
  799. struct vmw_tex_state_cmd {
  800. SVGA3dCmdHeader header;
  801. SVGA3dCmdSetTextureState state;
  802. };
  803. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  804. ((unsigned long) header + header->size + sizeof(header));
  805. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  806. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  807. int ret;
  808. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  809. if (unlikely(ret != 0))
  810. return ret;
  811. for (; cur_state < last_state; ++cur_state) {
  812. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  813. continue;
  814. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  815. user_surface_converter,
  816. &cur_state->value, NULL);
  817. if (unlikely(ret != 0))
  818. return ret;
  819. }
  820. return 0;
  821. }
  822. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  823. struct vmw_sw_context *sw_context,
  824. void *buf)
  825. {
  826. struct vmw_dma_buffer *vmw_bo;
  827. int ret;
  828. struct {
  829. uint32_t header;
  830. SVGAFifoCmdDefineGMRFB body;
  831. } *cmd = buf;
  832. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  833. &cmd->body.ptr,
  834. &vmw_bo);
  835. if (unlikely(ret != 0))
  836. return ret;
  837. vmw_dmabuf_unreference(&vmw_bo);
  838. return ret;
  839. }
  840. /**
  841. * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
  842. * command
  843. *
  844. * @dev_priv: Pointer to a device private struct.
  845. * @sw_context: The software context being used for this batch.
  846. * @header: Pointer to the command header in the command stream.
  847. */
  848. static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
  849. struct vmw_sw_context *sw_context,
  850. SVGA3dCmdHeader *header)
  851. {
  852. struct vmw_set_shader_cmd {
  853. SVGA3dCmdHeader header;
  854. SVGA3dCmdSetShader body;
  855. } *cmd;
  856. int ret;
  857. cmd = container_of(header, struct vmw_set_shader_cmd,
  858. header);
  859. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  860. if (unlikely(ret != 0))
  861. return ret;
  862. return 0;
  863. }
  864. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  865. struct vmw_sw_context *sw_context,
  866. void *buf, uint32_t *size)
  867. {
  868. uint32_t size_remaining = *size;
  869. uint32_t cmd_id;
  870. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  871. switch (cmd_id) {
  872. case SVGA_CMD_UPDATE:
  873. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  874. break;
  875. case SVGA_CMD_DEFINE_GMRFB:
  876. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  877. break;
  878. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  879. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  880. break;
  881. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  882. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  883. break;
  884. default:
  885. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  886. return -EINVAL;
  887. }
  888. if (*size > size_remaining) {
  889. DRM_ERROR("Invalid SVGA command (size mismatch):"
  890. " %u.\n", cmd_id);
  891. return -EINVAL;
  892. }
  893. if (unlikely(!sw_context->kernel)) {
  894. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  895. return -EPERM;
  896. }
  897. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  898. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  899. return 0;
  900. }
  901. typedef int (*vmw_cmd_func) (struct vmw_private *,
  902. struct vmw_sw_context *,
  903. SVGA3dCmdHeader *);
  904. #define VMW_CMD_DEF(cmd, func) \
  905. [cmd - SVGA_3D_CMD_BASE] = func
  906. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  907. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  908. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  909. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  910. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  911. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  912. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  913. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  914. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  915. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  916. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  917. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  918. &vmw_cmd_set_render_target_check),
  919. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  920. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  921. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  922. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  923. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  924. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  925. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  926. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  927. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  928. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  929. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader),
  930. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  931. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  932. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  933. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query),
  934. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
  935. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
  936. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  937. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  938. &vmw_cmd_blt_surf_screen_check),
  939. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid),
  940. VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid),
  941. VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid),
  942. VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid),
  943. };
  944. static int vmw_cmd_check(struct vmw_private *dev_priv,
  945. struct vmw_sw_context *sw_context,
  946. void *buf, uint32_t *size)
  947. {
  948. uint32_t cmd_id;
  949. uint32_t size_remaining = *size;
  950. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  951. int ret;
  952. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  953. /* Handle any none 3D commands */
  954. if (unlikely(cmd_id < SVGA_CMD_MAX))
  955. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  956. cmd_id = le32_to_cpu(header->id);
  957. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  958. cmd_id -= SVGA_3D_CMD_BASE;
  959. if (unlikely(*size > size_remaining))
  960. goto out_err;
  961. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  962. goto out_err;
  963. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  964. if (unlikely(ret != 0))
  965. goto out_err;
  966. return 0;
  967. out_err:
  968. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  969. cmd_id + SVGA_3D_CMD_BASE);
  970. return -EINVAL;
  971. }
  972. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  973. struct vmw_sw_context *sw_context,
  974. void *buf,
  975. uint32_t size)
  976. {
  977. int32_t cur_size = size;
  978. int ret;
  979. sw_context->buf_start = buf;
  980. while (cur_size > 0) {
  981. size = cur_size;
  982. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  983. if (unlikely(ret != 0))
  984. return ret;
  985. buf = (void *)((unsigned long) buf + size);
  986. cur_size -= size;
  987. }
  988. if (unlikely(cur_size != 0)) {
  989. DRM_ERROR("Command verifier out of sync.\n");
  990. return -EINVAL;
  991. }
  992. return 0;
  993. }
  994. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  995. {
  996. sw_context->cur_reloc = 0;
  997. }
  998. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  999. {
  1000. uint32_t i;
  1001. struct vmw_relocation *reloc;
  1002. struct ttm_validate_buffer *validate;
  1003. struct ttm_buffer_object *bo;
  1004. for (i = 0; i < sw_context->cur_reloc; ++i) {
  1005. reloc = &sw_context->relocs[i];
  1006. validate = &sw_context->val_bufs[reloc->index].base;
  1007. bo = validate->bo;
  1008. switch (bo->mem.mem_type) {
  1009. case TTM_PL_VRAM:
  1010. reloc->location->offset += bo->offset;
  1011. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  1012. break;
  1013. case VMW_PL_GMR:
  1014. reloc->location->gmrId = bo->mem.start;
  1015. break;
  1016. default:
  1017. BUG();
  1018. }
  1019. }
  1020. vmw_free_relocations(sw_context);
  1021. }
  1022. /**
  1023. * vmw_resource_list_unrefererence - Free up a resource list and unreference
  1024. * all resources referenced by it.
  1025. *
  1026. * @list: The resource list.
  1027. */
  1028. static void vmw_resource_list_unreference(struct list_head *list)
  1029. {
  1030. struct vmw_resource_val_node *val, *val_next;
  1031. /*
  1032. * Drop references to resources held during command submission.
  1033. */
  1034. list_for_each_entry_safe(val, val_next, list, head) {
  1035. list_del_init(&val->head);
  1036. vmw_resource_unreference(&val->res);
  1037. kfree(val);
  1038. }
  1039. }
  1040. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  1041. {
  1042. struct vmw_validate_buffer *entry, *next;
  1043. struct vmw_resource_val_node *val;
  1044. /*
  1045. * Drop references to DMA buffers held during command submission.
  1046. */
  1047. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  1048. base.head) {
  1049. list_del(&entry->base.head);
  1050. ttm_bo_unref(&entry->base.bo);
  1051. (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
  1052. sw_context->cur_val_buf--;
  1053. }
  1054. BUG_ON(sw_context->cur_val_buf != 0);
  1055. list_for_each_entry(val, &sw_context->resource_list, head)
  1056. (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
  1057. }
  1058. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  1059. struct ttm_buffer_object *bo)
  1060. {
  1061. int ret;
  1062. /*
  1063. * Don't validate pinned buffers.
  1064. */
  1065. if (bo == dev_priv->pinned_bo ||
  1066. (bo == dev_priv->dummy_query_bo &&
  1067. dev_priv->dummy_query_bo_pinned))
  1068. return 0;
  1069. /**
  1070. * Put BO in VRAM if there is space, otherwise as a GMR.
  1071. * If there is no space in VRAM and GMR ids are all used up,
  1072. * start evicting GMRs to make room. If the DMA buffer can't be
  1073. * used as a GMR, this will return -ENOMEM.
  1074. */
  1075. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false);
  1076. if (likely(ret == 0 || ret == -ERESTARTSYS))
  1077. return ret;
  1078. /**
  1079. * If that failed, try VRAM again, this time evicting
  1080. * previous contents.
  1081. */
  1082. DRM_INFO("Falling through to VRAM.\n");
  1083. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
  1084. return ret;
  1085. }
  1086. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  1087. struct vmw_sw_context *sw_context)
  1088. {
  1089. struct vmw_validate_buffer *entry;
  1090. int ret;
  1091. list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
  1092. ret = vmw_validate_single_buffer(dev_priv, entry->base.bo);
  1093. if (unlikely(ret != 0))
  1094. return ret;
  1095. }
  1096. return 0;
  1097. }
  1098. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  1099. uint32_t size)
  1100. {
  1101. if (likely(sw_context->cmd_bounce_size >= size))
  1102. return 0;
  1103. if (sw_context->cmd_bounce_size == 0)
  1104. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  1105. while (sw_context->cmd_bounce_size < size) {
  1106. sw_context->cmd_bounce_size =
  1107. PAGE_ALIGN(sw_context->cmd_bounce_size +
  1108. (sw_context->cmd_bounce_size >> 1));
  1109. }
  1110. if (sw_context->cmd_bounce != NULL)
  1111. vfree(sw_context->cmd_bounce);
  1112. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  1113. if (sw_context->cmd_bounce == NULL) {
  1114. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  1115. sw_context->cmd_bounce_size = 0;
  1116. return -ENOMEM;
  1117. }
  1118. return 0;
  1119. }
  1120. /**
  1121. * vmw_execbuf_fence_commands - create and submit a command stream fence
  1122. *
  1123. * Creates a fence object and submits a command stream marker.
  1124. * If this fails for some reason, We sync the fifo and return NULL.
  1125. * It is then safe to fence buffers with a NULL pointer.
  1126. *
  1127. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  1128. * a userspace handle if @p_handle is not NULL, otherwise not.
  1129. */
  1130. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  1131. struct vmw_private *dev_priv,
  1132. struct vmw_fence_obj **p_fence,
  1133. uint32_t *p_handle)
  1134. {
  1135. uint32_t sequence;
  1136. int ret;
  1137. bool synced = false;
  1138. /* p_handle implies file_priv. */
  1139. BUG_ON(p_handle != NULL && file_priv == NULL);
  1140. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  1141. if (unlikely(ret != 0)) {
  1142. DRM_ERROR("Fence submission error. Syncing.\n");
  1143. synced = true;
  1144. }
  1145. if (p_handle != NULL)
  1146. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  1147. sequence,
  1148. DRM_VMW_FENCE_FLAG_EXEC,
  1149. p_fence, p_handle);
  1150. else
  1151. ret = vmw_fence_create(dev_priv->fman, sequence,
  1152. DRM_VMW_FENCE_FLAG_EXEC,
  1153. p_fence);
  1154. if (unlikely(ret != 0 && !synced)) {
  1155. (void) vmw_fallback_wait(dev_priv, false, false,
  1156. sequence, false,
  1157. VMW_FENCE_WAIT_TIMEOUT);
  1158. *p_fence = NULL;
  1159. }
  1160. return 0;
  1161. }
  1162. /**
  1163. * vmw_execbuf_copy_fence_user - copy fence object information to
  1164. * user-space.
  1165. *
  1166. * @dev_priv: Pointer to a vmw_private struct.
  1167. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  1168. * @ret: Return value from fence object creation.
  1169. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  1170. * which the information should be copied.
  1171. * @fence: Pointer to the fenc object.
  1172. * @fence_handle: User-space fence handle.
  1173. *
  1174. * This function copies fence information to user-space. If copying fails,
  1175. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  1176. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  1177. * the error will hopefully be detected.
  1178. * Also if copying fails, user-space will be unable to signal the fence
  1179. * object so we wait for it immediately, and then unreference the
  1180. * user-space reference.
  1181. */
  1182. void
  1183. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  1184. struct vmw_fpriv *vmw_fp,
  1185. int ret,
  1186. struct drm_vmw_fence_rep __user *user_fence_rep,
  1187. struct vmw_fence_obj *fence,
  1188. uint32_t fence_handle)
  1189. {
  1190. struct drm_vmw_fence_rep fence_rep;
  1191. if (user_fence_rep == NULL)
  1192. return;
  1193. memset(&fence_rep, 0, sizeof(fence_rep));
  1194. fence_rep.error = ret;
  1195. if (ret == 0) {
  1196. BUG_ON(fence == NULL);
  1197. fence_rep.handle = fence_handle;
  1198. fence_rep.seqno = fence->seqno;
  1199. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  1200. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  1201. }
  1202. /*
  1203. * copy_to_user errors will be detected by user space not
  1204. * seeing fence_rep::error filled in. Typically
  1205. * user-space would have pre-set that member to -EFAULT.
  1206. */
  1207. ret = copy_to_user(user_fence_rep, &fence_rep,
  1208. sizeof(fence_rep));
  1209. /*
  1210. * User-space lost the fence object. We need to sync
  1211. * and unreference the handle.
  1212. */
  1213. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  1214. ttm_ref_object_base_unref(vmw_fp->tfile,
  1215. fence_handle, TTM_REF_USAGE);
  1216. DRM_ERROR("Fence copy error. Syncing.\n");
  1217. (void) vmw_fence_obj_wait(fence, fence->signal_mask,
  1218. false, false,
  1219. VMW_FENCE_WAIT_TIMEOUT);
  1220. }
  1221. }
  1222. int vmw_execbuf_process(struct drm_file *file_priv,
  1223. struct vmw_private *dev_priv,
  1224. void __user *user_commands,
  1225. void *kernel_commands,
  1226. uint32_t command_size,
  1227. uint64_t throttle_us,
  1228. struct drm_vmw_fence_rep __user *user_fence_rep,
  1229. struct vmw_fence_obj **out_fence)
  1230. {
  1231. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  1232. struct vmw_fence_obj *fence = NULL;
  1233. struct vmw_resource *error_resource;
  1234. struct list_head resource_list;
  1235. uint32_t handle;
  1236. void *cmd;
  1237. int ret;
  1238. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  1239. if (unlikely(ret != 0))
  1240. return -ERESTARTSYS;
  1241. if (kernel_commands == NULL) {
  1242. sw_context->kernel = false;
  1243. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  1244. if (unlikely(ret != 0))
  1245. goto out_unlock;
  1246. ret = copy_from_user(sw_context->cmd_bounce,
  1247. user_commands, command_size);
  1248. if (unlikely(ret != 0)) {
  1249. ret = -EFAULT;
  1250. DRM_ERROR("Failed copying commands.\n");
  1251. goto out_unlock;
  1252. }
  1253. kernel_commands = sw_context->cmd_bounce;
  1254. } else
  1255. sw_context->kernel = true;
  1256. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  1257. sw_context->cur_reloc = 0;
  1258. sw_context->cur_val_buf = 0;
  1259. sw_context->fence_flags = 0;
  1260. INIT_LIST_HEAD(&sw_context->resource_list);
  1261. sw_context->cur_query_bo = dev_priv->pinned_bo;
  1262. sw_context->last_query_ctx = NULL;
  1263. sw_context->needs_post_query_barrier = false;
  1264. memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
  1265. INIT_LIST_HEAD(&sw_context->validate_nodes);
  1266. INIT_LIST_HEAD(&sw_context->res_relocations);
  1267. if (!sw_context->res_ht_initialized) {
  1268. ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
  1269. if (unlikely(ret != 0))
  1270. goto out_unlock;
  1271. sw_context->res_ht_initialized = true;
  1272. }
  1273. INIT_LIST_HEAD(&resource_list);
  1274. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  1275. command_size);
  1276. if (unlikely(ret != 0))
  1277. goto out_err;
  1278. ret = vmw_resources_reserve(sw_context);
  1279. if (unlikely(ret != 0))
  1280. goto out_err;
  1281. ret = ttm_eu_reserve_buffers(&sw_context->validate_nodes);
  1282. if (unlikely(ret != 0))
  1283. goto out_err;
  1284. ret = vmw_validate_buffers(dev_priv, sw_context);
  1285. if (unlikely(ret != 0))
  1286. goto out_err;
  1287. ret = vmw_resources_validate(sw_context);
  1288. if (unlikely(ret != 0))
  1289. goto out_err;
  1290. if (throttle_us) {
  1291. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  1292. throttle_us);
  1293. if (unlikely(ret != 0))
  1294. goto out_err;
  1295. }
  1296. cmd = vmw_fifo_reserve(dev_priv, command_size);
  1297. if (unlikely(cmd == NULL)) {
  1298. DRM_ERROR("Failed reserving fifo space for commands.\n");
  1299. ret = -ENOMEM;
  1300. goto out_err;
  1301. }
  1302. vmw_apply_relocations(sw_context);
  1303. memcpy(cmd, kernel_commands, command_size);
  1304. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  1305. vmw_resource_relocations_free(&sw_context->res_relocations);
  1306. vmw_fifo_commit(dev_priv, command_size);
  1307. vmw_query_bo_switch_commit(dev_priv, sw_context);
  1308. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  1309. &fence,
  1310. (user_fence_rep) ? &handle : NULL);
  1311. /*
  1312. * This error is harmless, because if fence submission fails,
  1313. * vmw_fifo_send_fence will sync. The error will be propagated to
  1314. * user-space in @fence_rep
  1315. */
  1316. if (ret != 0)
  1317. DRM_ERROR("Fence submission error. Syncing.\n");
  1318. vmw_resource_list_unreserve(&sw_context->resource_list, false);
  1319. ttm_eu_fence_buffer_objects(&sw_context->validate_nodes,
  1320. (void *) fence);
  1321. if (unlikely(dev_priv->pinned_bo != NULL &&
  1322. !dev_priv->query_cid_valid))
  1323. __vmw_execbuf_release_pinned_bo(dev_priv, fence);
  1324. vmw_clear_validations(sw_context);
  1325. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  1326. user_fence_rep, fence, handle);
  1327. /* Don't unreference when handing fence out */
  1328. if (unlikely(out_fence != NULL)) {
  1329. *out_fence = fence;
  1330. fence = NULL;
  1331. } else if (likely(fence != NULL)) {
  1332. vmw_fence_obj_unreference(&fence);
  1333. }
  1334. list_splice_init(&sw_context->resource_list, &resource_list);
  1335. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1336. /*
  1337. * Unreference resources outside of the cmdbuf_mutex to
  1338. * avoid deadlocks in resource destruction paths.
  1339. */
  1340. vmw_resource_list_unreference(&resource_list);
  1341. return 0;
  1342. out_err:
  1343. vmw_resource_relocations_free(&sw_context->res_relocations);
  1344. vmw_free_relocations(sw_context);
  1345. ttm_eu_backoff_reservation(&sw_context->validate_nodes);
  1346. vmw_resource_list_unreserve(&sw_context->resource_list, true);
  1347. vmw_clear_validations(sw_context);
  1348. if (unlikely(dev_priv->pinned_bo != NULL &&
  1349. !dev_priv->query_cid_valid))
  1350. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  1351. out_unlock:
  1352. list_splice_init(&sw_context->resource_list, &resource_list);
  1353. error_resource = sw_context->error_resource;
  1354. sw_context->error_resource = NULL;
  1355. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1356. /*
  1357. * Unreference resources outside of the cmdbuf_mutex to
  1358. * avoid deadlocks in resource destruction paths.
  1359. */
  1360. vmw_resource_list_unreference(&resource_list);
  1361. if (unlikely(error_resource != NULL))
  1362. vmw_resource_unreference(&error_resource);
  1363. return ret;
  1364. }
  1365. /**
  1366. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  1367. *
  1368. * @dev_priv: The device private structure.
  1369. *
  1370. * This function is called to idle the fifo and unpin the query buffer
  1371. * if the normal way to do this hits an error, which should typically be
  1372. * extremely rare.
  1373. */
  1374. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  1375. {
  1376. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  1377. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  1378. vmw_bo_pin(dev_priv->pinned_bo, false);
  1379. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1380. dev_priv->dummy_query_bo_pinned = false;
  1381. }
  1382. /**
  1383. * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1384. * query bo.
  1385. *
  1386. * @dev_priv: The device private structure.
  1387. * @fence: If non-NULL should point to a struct vmw_fence_obj issued
  1388. * _after_ a query barrier that flushes all queries touching the current
  1389. * buffer pointed to by @dev_priv->pinned_bo
  1390. *
  1391. * This function should be used to unpin the pinned query bo, or
  1392. * as a query barrier when we need to make sure that all queries have
  1393. * finished before the next fifo command. (For example on hardware
  1394. * context destructions where the hardware may otherwise leak unfinished
  1395. * queries).
  1396. *
  1397. * This function does not return any failure codes, but make attempts
  1398. * to do safe unpinning in case of errors.
  1399. *
  1400. * The function will synchronize on the previous query barrier, and will
  1401. * thus not finish until that barrier has executed.
  1402. *
  1403. * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
  1404. * before calling this function.
  1405. */
  1406. void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  1407. struct vmw_fence_obj *fence)
  1408. {
  1409. int ret = 0;
  1410. struct list_head validate_list;
  1411. struct ttm_validate_buffer pinned_val, query_val;
  1412. struct vmw_fence_obj *lfence = NULL;
  1413. if (dev_priv->pinned_bo == NULL)
  1414. goto out_unlock;
  1415. INIT_LIST_HEAD(&validate_list);
  1416. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  1417. list_add_tail(&pinned_val.head, &validate_list);
  1418. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  1419. list_add_tail(&query_val.head, &validate_list);
  1420. do {
  1421. ret = ttm_eu_reserve_buffers(&validate_list);
  1422. } while (ret == -ERESTARTSYS);
  1423. if (unlikely(ret != 0)) {
  1424. vmw_execbuf_unpin_panic(dev_priv);
  1425. goto out_no_reserve;
  1426. }
  1427. if (dev_priv->query_cid_valid) {
  1428. BUG_ON(fence != NULL);
  1429. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  1430. if (unlikely(ret != 0)) {
  1431. vmw_execbuf_unpin_panic(dev_priv);
  1432. goto out_no_emit;
  1433. }
  1434. dev_priv->query_cid_valid = false;
  1435. }
  1436. vmw_bo_pin(dev_priv->pinned_bo, false);
  1437. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1438. dev_priv->dummy_query_bo_pinned = false;
  1439. if (fence == NULL) {
  1440. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
  1441. NULL);
  1442. fence = lfence;
  1443. }
  1444. ttm_eu_fence_buffer_objects(&validate_list, (void *) fence);
  1445. if (lfence != NULL)
  1446. vmw_fence_obj_unreference(&lfence);
  1447. ttm_bo_unref(&query_val.bo);
  1448. ttm_bo_unref(&pinned_val.bo);
  1449. ttm_bo_unref(&dev_priv->pinned_bo);
  1450. out_unlock:
  1451. return;
  1452. out_no_emit:
  1453. ttm_eu_backoff_reservation(&validate_list);
  1454. out_no_reserve:
  1455. ttm_bo_unref(&query_val.bo);
  1456. ttm_bo_unref(&pinned_val.bo);
  1457. ttm_bo_unref(&dev_priv->pinned_bo);
  1458. }
  1459. /**
  1460. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1461. * query bo.
  1462. *
  1463. * @dev_priv: The device private structure.
  1464. *
  1465. * This function should be used to unpin the pinned query bo, or
  1466. * as a query barrier when we need to make sure that all queries have
  1467. * finished before the next fifo command. (For example on hardware
  1468. * context destructions where the hardware may otherwise leak unfinished
  1469. * queries).
  1470. *
  1471. * This function does not return any failure codes, but make attempts
  1472. * to do safe unpinning in case of errors.
  1473. *
  1474. * The function will synchronize on the previous query barrier, and will
  1475. * thus not finish until that barrier has executed.
  1476. */
  1477. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
  1478. {
  1479. mutex_lock(&dev_priv->cmdbuf_mutex);
  1480. if (dev_priv->query_cid_valid)
  1481. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  1482. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1483. }
  1484. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  1485. struct drm_file *file_priv)
  1486. {
  1487. struct vmw_private *dev_priv = vmw_priv(dev);
  1488. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  1489. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1490. int ret;
  1491. /*
  1492. * This will allow us to extend the ioctl argument while
  1493. * maintaining backwards compatibility:
  1494. * We take different code paths depending on the value of
  1495. * arg->version.
  1496. */
  1497. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  1498. DRM_ERROR("Incorrect execbuf version.\n");
  1499. DRM_ERROR("You're running outdated experimental "
  1500. "vmwgfx user-space drivers.");
  1501. return -EINVAL;
  1502. }
  1503. ret = ttm_read_lock(&vmaster->lock, true);
  1504. if (unlikely(ret != 0))
  1505. return ret;
  1506. ret = vmw_execbuf_process(file_priv, dev_priv,
  1507. (void __user *)(unsigned long)arg->commands,
  1508. NULL, arg->command_size, arg->throttle_us,
  1509. (void __user *)(unsigned long)arg->fence_rep,
  1510. NULL);
  1511. if (unlikely(ret != 0))
  1512. goto out_unlock;
  1513. vmw_kms_cursor_post_execbuf(dev_priv);
  1514. out_unlock:
  1515. ttm_read_unlock(&vmaster->lock);
  1516. return ret;
  1517. }