vmwgfx_execbuf.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793
  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. * @staged_bindings: If @res is a context, tracks bindings set up during
  54. * the command batch. Otherwise NULL.
  55. * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
  56. * @first_usage: Set to true the first time the resource is referenced in
  57. * the command stream.
  58. * @no_buffer_needed: Resources do not need to allocate buffer backup on
  59. * reservation. The command stream will provide one.
  60. */
  61. struct vmw_resource_val_node {
  62. struct list_head head;
  63. struct drm_hash_item hash;
  64. struct vmw_resource *res;
  65. struct vmw_dma_buffer *new_backup;
  66. struct vmw_ctx_binding_state *staged_bindings;
  67. unsigned long new_backup_offset;
  68. bool first_usage;
  69. bool no_buffer_needed;
  70. };
  71. /**
  72. * struct vmw_cmd_entry - Describe a command for the verifier
  73. *
  74. * @user_allow: Whether allowed from the execbuf ioctl.
  75. * @gb_disable: Whether disabled if guest-backed objects are available.
  76. * @gb_enable: Whether enabled iff guest-backed objects are available.
  77. */
  78. struct vmw_cmd_entry {
  79. int (*func) (struct vmw_private *, struct vmw_sw_context *,
  80. SVGA3dCmdHeader *);
  81. bool user_allow;
  82. bool gb_disable;
  83. bool gb_enable;
  84. };
  85. #define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
  86. [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
  87. (_gb_disable), (_gb_enable)}
  88. /**
  89. * vmw_resource_unreserve - unreserve resources previously reserved for
  90. * command submission.
  91. *
  92. * @list_head: list of resources to unreserve.
  93. * @backoff: Whether command submission failed.
  94. */
  95. static void vmw_resource_list_unreserve(struct list_head *list,
  96. bool backoff)
  97. {
  98. struct vmw_resource_val_node *val;
  99. list_for_each_entry(val, list, head) {
  100. struct vmw_resource *res = val->res;
  101. struct vmw_dma_buffer *new_backup =
  102. backoff ? NULL : val->new_backup;
  103. /*
  104. * Transfer staged context bindings to the
  105. * persistent context binding tracker.
  106. */
  107. if (unlikely(val->staged_bindings)) {
  108. if (!backoff) {
  109. vmw_context_binding_state_transfer
  110. (val->res, val->staged_bindings);
  111. }
  112. kfree(val->staged_bindings);
  113. val->staged_bindings = NULL;
  114. }
  115. vmw_resource_unreserve(res, new_backup,
  116. val->new_backup_offset);
  117. vmw_dmabuf_unreference(&val->new_backup);
  118. }
  119. }
  120. /**
  121. * vmw_resource_val_add - Add a resource to the software context's
  122. * resource list if it's not already on it.
  123. *
  124. * @sw_context: Pointer to the software context.
  125. * @res: Pointer to the resource.
  126. * @p_node On successful return points to a valid pointer to a
  127. * struct vmw_resource_val_node, if non-NULL on entry.
  128. */
  129. static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
  130. struct vmw_resource *res,
  131. struct vmw_resource_val_node **p_node)
  132. {
  133. struct vmw_resource_val_node *node;
  134. struct drm_hash_item *hash;
  135. int ret;
  136. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
  137. &hash) == 0)) {
  138. node = container_of(hash, struct vmw_resource_val_node, hash);
  139. node->first_usage = false;
  140. if (unlikely(p_node != NULL))
  141. *p_node = node;
  142. return 0;
  143. }
  144. node = kzalloc(sizeof(*node), GFP_KERNEL);
  145. if (unlikely(node == NULL)) {
  146. DRM_ERROR("Failed to allocate a resource validation "
  147. "entry.\n");
  148. return -ENOMEM;
  149. }
  150. node->hash.key = (unsigned long) res;
  151. ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
  152. if (unlikely(ret != 0)) {
  153. DRM_ERROR("Failed to initialize a resource validation "
  154. "entry.\n");
  155. kfree(node);
  156. return ret;
  157. }
  158. list_add_tail(&node->head, &sw_context->resource_list);
  159. node->res = vmw_resource_reference(res);
  160. node->first_usage = true;
  161. if (unlikely(p_node != NULL))
  162. *p_node = node;
  163. return 0;
  164. }
  165. /**
  166. * vmw_resource_context_res_add - Put resources previously bound to a context on
  167. * the validation list
  168. *
  169. * @dev_priv: Pointer to a device private structure
  170. * @sw_context: Pointer to a software context used for this command submission
  171. * @ctx: Pointer to the context resource
  172. *
  173. * This function puts all resources that were previously bound to @ctx on
  174. * the resource validation list. This is part of the context state reemission
  175. */
  176. static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
  177. struct vmw_sw_context *sw_context,
  178. struct vmw_resource *ctx)
  179. {
  180. struct list_head *binding_list;
  181. struct vmw_ctx_binding *entry;
  182. int ret = 0;
  183. struct vmw_resource *res;
  184. mutex_lock(&dev_priv->binding_mutex);
  185. binding_list = vmw_context_binding_list(ctx);
  186. list_for_each_entry(entry, binding_list, ctx_list) {
  187. res = vmw_resource_reference_unless_doomed(entry->bi.res);
  188. if (unlikely(res == NULL))
  189. continue;
  190. ret = vmw_resource_val_add(sw_context, entry->bi.res, NULL);
  191. vmw_resource_unreference(&res);
  192. if (unlikely(ret != 0))
  193. break;
  194. }
  195. mutex_unlock(&dev_priv->binding_mutex);
  196. return ret;
  197. }
  198. /**
  199. * vmw_resource_relocation_add - Add a relocation to the relocation list
  200. *
  201. * @list: Pointer to head of relocation list.
  202. * @res: The resource.
  203. * @offset: Offset into the command buffer currently being parsed where the
  204. * id that needs fixup is located. Granularity is 4 bytes.
  205. */
  206. static int vmw_resource_relocation_add(struct list_head *list,
  207. const struct vmw_resource *res,
  208. unsigned long offset)
  209. {
  210. struct vmw_resource_relocation *rel;
  211. rel = kmalloc(sizeof(*rel), GFP_KERNEL);
  212. if (unlikely(rel == NULL)) {
  213. DRM_ERROR("Failed to allocate a resource relocation.\n");
  214. return -ENOMEM;
  215. }
  216. rel->res = res;
  217. rel->offset = offset;
  218. list_add_tail(&rel->head, list);
  219. return 0;
  220. }
  221. /**
  222. * vmw_resource_relocations_free - Free all relocations on a list
  223. *
  224. * @list: Pointer to the head of the relocation list.
  225. */
  226. static void vmw_resource_relocations_free(struct list_head *list)
  227. {
  228. struct vmw_resource_relocation *rel, *n;
  229. list_for_each_entry_safe(rel, n, list, head) {
  230. list_del(&rel->head);
  231. kfree(rel);
  232. }
  233. }
  234. /**
  235. * vmw_resource_relocations_apply - Apply all relocations on a list
  236. *
  237. * @cb: Pointer to the start of the command buffer bein patch. This need
  238. * not be the same buffer as the one being parsed when the relocation
  239. * list was built, but the contents must be the same modulo the
  240. * resource ids.
  241. * @list: Pointer to the head of the relocation list.
  242. */
  243. static void vmw_resource_relocations_apply(uint32_t *cb,
  244. struct list_head *list)
  245. {
  246. struct vmw_resource_relocation *rel;
  247. list_for_each_entry(rel, list, head) {
  248. if (likely(rel->res != NULL))
  249. cb[rel->offset] = rel->res->id;
  250. else
  251. cb[rel->offset] = SVGA_3D_CMD_NOP;
  252. }
  253. }
  254. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  255. struct vmw_sw_context *sw_context,
  256. SVGA3dCmdHeader *header)
  257. {
  258. return capable(CAP_SYS_ADMIN) ? : -EINVAL;
  259. }
  260. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  261. struct vmw_sw_context *sw_context,
  262. SVGA3dCmdHeader *header)
  263. {
  264. return 0;
  265. }
  266. /**
  267. * vmw_bo_to_validate_list - add a bo to a validate list
  268. *
  269. * @sw_context: The software context used for this command submission batch.
  270. * @bo: The buffer object to add.
  271. * @validate_as_mob: Validate this buffer as a MOB.
  272. * @p_val_node: If non-NULL Will be updated with the validate node number
  273. * on return.
  274. *
  275. * Returns -EINVAL if the limit of number of buffer objects per command
  276. * submission is reached.
  277. */
  278. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  279. struct ttm_buffer_object *bo,
  280. bool validate_as_mob,
  281. uint32_t *p_val_node)
  282. {
  283. uint32_t val_node;
  284. struct vmw_validate_buffer *vval_buf;
  285. struct ttm_validate_buffer *val_buf;
  286. struct drm_hash_item *hash;
  287. int ret;
  288. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) bo,
  289. &hash) == 0)) {
  290. vval_buf = container_of(hash, struct vmw_validate_buffer,
  291. hash);
  292. if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
  293. DRM_ERROR("Inconsistent buffer usage.\n");
  294. return -EINVAL;
  295. }
  296. val_buf = &vval_buf->base;
  297. val_node = vval_buf - sw_context->val_bufs;
  298. } else {
  299. val_node = sw_context->cur_val_buf;
  300. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  301. DRM_ERROR("Max number of DMA buffers per submission "
  302. "exceeded.\n");
  303. return -EINVAL;
  304. }
  305. vval_buf = &sw_context->val_bufs[val_node];
  306. vval_buf->hash.key = (unsigned long) bo;
  307. ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
  308. if (unlikely(ret != 0)) {
  309. DRM_ERROR("Failed to initialize a buffer validation "
  310. "entry.\n");
  311. return ret;
  312. }
  313. ++sw_context->cur_val_buf;
  314. val_buf = &vval_buf->base;
  315. val_buf->bo = ttm_bo_reference(bo);
  316. val_buf->shared = false;
  317. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  318. vval_buf->validate_as_mob = validate_as_mob;
  319. }
  320. if (p_val_node)
  321. *p_val_node = val_node;
  322. return 0;
  323. }
  324. /**
  325. * vmw_resources_reserve - Reserve all resources on the sw_context's
  326. * resource list.
  327. *
  328. * @sw_context: Pointer to the software context.
  329. *
  330. * Note that since vmware's command submission currently is protected by
  331. * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
  332. * since only a single thread at once will attempt this.
  333. */
  334. static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
  335. {
  336. struct vmw_resource_val_node *val;
  337. int ret;
  338. list_for_each_entry(val, &sw_context->resource_list, head) {
  339. struct vmw_resource *res = val->res;
  340. ret = vmw_resource_reserve(res, val->no_buffer_needed);
  341. if (unlikely(ret != 0))
  342. return ret;
  343. if (res->backup) {
  344. struct ttm_buffer_object *bo = &res->backup->base;
  345. ret = vmw_bo_to_validate_list
  346. (sw_context, bo,
  347. vmw_resource_needs_backup(res), NULL);
  348. if (unlikely(ret != 0))
  349. return ret;
  350. }
  351. }
  352. return 0;
  353. }
  354. /**
  355. * vmw_resources_validate - Validate all resources on the sw_context's
  356. * resource list.
  357. *
  358. * @sw_context: Pointer to the software context.
  359. *
  360. * Before this function is called, all resource backup buffers must have
  361. * been validated.
  362. */
  363. static int vmw_resources_validate(struct vmw_sw_context *sw_context)
  364. {
  365. struct vmw_resource_val_node *val;
  366. int ret;
  367. list_for_each_entry(val, &sw_context->resource_list, head) {
  368. struct vmw_resource *res = val->res;
  369. ret = vmw_resource_validate(res);
  370. if (unlikely(ret != 0)) {
  371. if (ret != -ERESTARTSYS)
  372. DRM_ERROR("Failed to validate resource.\n");
  373. return ret;
  374. }
  375. }
  376. return 0;
  377. }
  378. /**
  379. * vmw_cmd_res_reloc_add - Add a resource to a software context's
  380. * relocation- and validation lists.
  381. *
  382. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  383. * @sw_context: Pointer to the software context.
  384. * @res_type: Resource type.
  385. * @id_loc: Pointer to where the id that needs translation is located.
  386. * @res: Valid pointer to a struct vmw_resource.
  387. * @p_val: If non null, a pointer to the struct vmw_resource_validate_node
  388. * used for this resource is returned here.
  389. */
  390. static int vmw_cmd_res_reloc_add(struct vmw_private *dev_priv,
  391. struct vmw_sw_context *sw_context,
  392. enum vmw_res_type res_type,
  393. uint32_t *id_loc,
  394. struct vmw_resource *res,
  395. struct vmw_resource_val_node **p_val)
  396. {
  397. int ret;
  398. struct vmw_resource_val_node *node;
  399. *p_val = NULL;
  400. ret = vmw_resource_relocation_add(&sw_context->res_relocations,
  401. res,
  402. id_loc - sw_context->buf_start);
  403. if (unlikely(ret != 0))
  404. return ret;
  405. ret = vmw_resource_val_add(sw_context, res, &node);
  406. if (unlikely(ret != 0))
  407. return ret;
  408. if (res_type == vmw_res_context && dev_priv->has_mob &&
  409. node->first_usage) {
  410. /*
  411. * Put contexts first on the list to be able to exit
  412. * list traversal for contexts early.
  413. */
  414. list_del(&node->head);
  415. list_add(&node->head, &sw_context->resource_list);
  416. ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
  417. if (unlikely(ret != 0))
  418. return ret;
  419. node->staged_bindings =
  420. kzalloc(sizeof(*node->staged_bindings), GFP_KERNEL);
  421. if (node->staged_bindings == NULL) {
  422. DRM_ERROR("Failed to allocate context binding "
  423. "information.\n");
  424. return -ENOMEM;
  425. }
  426. INIT_LIST_HEAD(&node->staged_bindings->list);
  427. }
  428. if (p_val)
  429. *p_val = node;
  430. return 0;
  431. }
  432. /**
  433. * vmw_cmd_res_check - Check that a resource is present and if so, put it
  434. * on the resource validate list unless it's already there.
  435. *
  436. * @dev_priv: Pointer to a device private structure.
  437. * @sw_context: Pointer to the software context.
  438. * @res_type: Resource type.
  439. * @converter: User-space visisble type specific information.
  440. * @id_loc: Pointer to the location in the command buffer currently being
  441. * parsed from where the user-space resource id handle is located.
  442. * @p_val: Pointer to pointer to resource validalidation node. Populated
  443. * on exit.
  444. */
  445. static int
  446. vmw_cmd_res_check(struct vmw_private *dev_priv,
  447. struct vmw_sw_context *sw_context,
  448. enum vmw_res_type res_type,
  449. const struct vmw_user_resource_conv *converter,
  450. uint32_t *id_loc,
  451. struct vmw_resource_val_node **p_val)
  452. {
  453. struct vmw_res_cache_entry *rcache =
  454. &sw_context->res_cache[res_type];
  455. struct vmw_resource *res;
  456. struct vmw_resource_val_node *node;
  457. int ret;
  458. if (*id_loc == SVGA3D_INVALID_ID) {
  459. if (p_val)
  460. *p_val = NULL;
  461. if (res_type == vmw_res_context) {
  462. DRM_ERROR("Illegal context invalid id.\n");
  463. return -EINVAL;
  464. }
  465. return 0;
  466. }
  467. /*
  468. * Fastpath in case of repeated commands referencing the same
  469. * resource
  470. */
  471. if (likely(rcache->valid && *id_loc == rcache->handle)) {
  472. const struct vmw_resource *res = rcache->res;
  473. rcache->node->first_usage = false;
  474. if (p_val)
  475. *p_val = rcache->node;
  476. return vmw_resource_relocation_add
  477. (&sw_context->res_relocations, res,
  478. id_loc - sw_context->buf_start);
  479. }
  480. ret = vmw_user_resource_lookup_handle(dev_priv,
  481. sw_context->fp->tfile,
  482. *id_loc,
  483. converter,
  484. &res);
  485. if (unlikely(ret != 0)) {
  486. DRM_ERROR("Could not find or use resource 0x%08x.\n",
  487. (unsigned) *id_loc);
  488. dump_stack();
  489. return ret;
  490. }
  491. rcache->valid = true;
  492. rcache->res = res;
  493. rcache->handle = *id_loc;
  494. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context, res_type, id_loc,
  495. res, &node);
  496. if (unlikely(ret != 0))
  497. goto out_no_reloc;
  498. rcache->node = node;
  499. if (p_val)
  500. *p_val = node;
  501. vmw_resource_unreference(&res);
  502. return 0;
  503. out_no_reloc:
  504. BUG_ON(sw_context->error_resource != NULL);
  505. sw_context->error_resource = res;
  506. return ret;
  507. }
  508. /**
  509. * vmw_rebind_contexts - Rebind all resources previously bound to
  510. * referenced contexts.
  511. *
  512. * @sw_context: Pointer to the software context.
  513. *
  514. * Rebind context binding points that have been scrubbed because of eviction.
  515. */
  516. static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
  517. {
  518. struct vmw_resource_val_node *val;
  519. int ret;
  520. list_for_each_entry(val, &sw_context->resource_list, head) {
  521. if (unlikely(!val->staged_bindings))
  522. break;
  523. ret = vmw_context_rebind_all(val->res);
  524. if (unlikely(ret != 0)) {
  525. if (ret != -ERESTARTSYS)
  526. DRM_ERROR("Failed to rebind context.\n");
  527. return ret;
  528. }
  529. }
  530. return 0;
  531. }
  532. /**
  533. * vmw_cmd_cid_check - Check a command header for valid context information.
  534. *
  535. * @dev_priv: Pointer to a device private structure.
  536. * @sw_context: Pointer to the software context.
  537. * @header: A command header with an embedded user-space context handle.
  538. *
  539. * Convenience function: Call vmw_cmd_res_check with the user-space context
  540. * handle embedded in @header.
  541. */
  542. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  543. struct vmw_sw_context *sw_context,
  544. SVGA3dCmdHeader *header)
  545. {
  546. struct vmw_cid_cmd {
  547. SVGA3dCmdHeader header;
  548. uint32_t cid;
  549. } *cmd;
  550. cmd = container_of(header, struct vmw_cid_cmd, header);
  551. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  552. user_context_converter, &cmd->cid, NULL);
  553. }
  554. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  555. struct vmw_sw_context *sw_context,
  556. SVGA3dCmdHeader *header)
  557. {
  558. struct vmw_sid_cmd {
  559. SVGA3dCmdHeader header;
  560. SVGA3dCmdSetRenderTarget body;
  561. } *cmd;
  562. struct vmw_resource_val_node *ctx_node;
  563. struct vmw_resource_val_node *res_node;
  564. int ret;
  565. cmd = container_of(header, struct vmw_sid_cmd, header);
  566. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  567. user_context_converter, &cmd->body.cid,
  568. &ctx_node);
  569. if (unlikely(ret != 0))
  570. return ret;
  571. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  572. user_surface_converter,
  573. &cmd->body.target.sid, &res_node);
  574. if (unlikely(ret != 0))
  575. return ret;
  576. if (dev_priv->has_mob) {
  577. struct vmw_ctx_bindinfo bi;
  578. bi.ctx = ctx_node->res;
  579. bi.res = res_node ? res_node->res : NULL;
  580. bi.bt = vmw_ctx_binding_rt;
  581. bi.i1.rt_type = cmd->body.type;
  582. return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
  583. }
  584. return 0;
  585. }
  586. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  587. struct vmw_sw_context *sw_context,
  588. SVGA3dCmdHeader *header)
  589. {
  590. struct vmw_sid_cmd {
  591. SVGA3dCmdHeader header;
  592. SVGA3dCmdSurfaceCopy body;
  593. } *cmd;
  594. int ret;
  595. cmd = container_of(header, struct vmw_sid_cmd, header);
  596. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  597. user_surface_converter,
  598. &cmd->body.src.sid, NULL);
  599. if (unlikely(ret != 0))
  600. return ret;
  601. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  602. user_surface_converter,
  603. &cmd->body.dest.sid, NULL);
  604. }
  605. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  606. struct vmw_sw_context *sw_context,
  607. SVGA3dCmdHeader *header)
  608. {
  609. struct vmw_sid_cmd {
  610. SVGA3dCmdHeader header;
  611. SVGA3dCmdSurfaceStretchBlt body;
  612. } *cmd;
  613. int ret;
  614. cmd = container_of(header, struct vmw_sid_cmd, header);
  615. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  616. user_surface_converter,
  617. &cmd->body.src.sid, NULL);
  618. if (unlikely(ret != 0))
  619. return ret;
  620. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  621. user_surface_converter,
  622. &cmd->body.dest.sid, NULL);
  623. }
  624. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  625. struct vmw_sw_context *sw_context,
  626. SVGA3dCmdHeader *header)
  627. {
  628. struct vmw_sid_cmd {
  629. SVGA3dCmdHeader header;
  630. SVGA3dCmdBlitSurfaceToScreen body;
  631. } *cmd;
  632. cmd = container_of(header, struct vmw_sid_cmd, header);
  633. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  634. user_surface_converter,
  635. &cmd->body.srcImage.sid, NULL);
  636. }
  637. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  638. struct vmw_sw_context *sw_context,
  639. SVGA3dCmdHeader *header)
  640. {
  641. struct vmw_sid_cmd {
  642. SVGA3dCmdHeader header;
  643. SVGA3dCmdPresent body;
  644. } *cmd;
  645. cmd = container_of(header, struct vmw_sid_cmd, header);
  646. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  647. user_surface_converter, &cmd->body.sid,
  648. NULL);
  649. }
  650. /**
  651. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  652. *
  653. * @dev_priv: The device private structure.
  654. * @new_query_bo: The new buffer holding query results.
  655. * @sw_context: The software context used for this command submission.
  656. *
  657. * This function checks whether @new_query_bo is suitable for holding
  658. * query results, and if another buffer currently is pinned for query
  659. * results. If so, the function prepares the state of @sw_context for
  660. * switching pinned buffers after successful submission of the current
  661. * command batch.
  662. */
  663. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  664. struct ttm_buffer_object *new_query_bo,
  665. struct vmw_sw_context *sw_context)
  666. {
  667. struct vmw_res_cache_entry *ctx_entry =
  668. &sw_context->res_cache[vmw_res_context];
  669. int ret;
  670. BUG_ON(!ctx_entry->valid);
  671. sw_context->last_query_ctx = ctx_entry->res;
  672. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  673. if (unlikely(new_query_bo->num_pages > 4)) {
  674. DRM_ERROR("Query buffer too large.\n");
  675. return -EINVAL;
  676. }
  677. if (unlikely(sw_context->cur_query_bo != NULL)) {
  678. sw_context->needs_post_query_barrier = true;
  679. ret = vmw_bo_to_validate_list(sw_context,
  680. sw_context->cur_query_bo,
  681. dev_priv->has_mob, NULL);
  682. if (unlikely(ret != 0))
  683. return ret;
  684. }
  685. sw_context->cur_query_bo = new_query_bo;
  686. ret = vmw_bo_to_validate_list(sw_context,
  687. dev_priv->dummy_query_bo,
  688. dev_priv->has_mob, NULL);
  689. if (unlikely(ret != 0))
  690. return ret;
  691. }
  692. return 0;
  693. }
  694. /**
  695. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  696. *
  697. * @dev_priv: The device private structure.
  698. * @sw_context: The software context used for this command submission batch.
  699. *
  700. * This function will check if we're switching query buffers, and will then,
  701. * issue a dummy occlusion query wait used as a query barrier. When the fence
  702. * object following that query wait has signaled, we are sure that all
  703. * preceding queries have finished, and the old query buffer can be unpinned.
  704. * However, since both the new query buffer and the old one are fenced with
  705. * that fence, we can do an asynchronus unpin now, and be sure that the
  706. * old query buffer won't be moved until the fence has signaled.
  707. *
  708. * As mentioned above, both the new - and old query buffers need to be fenced
  709. * using a sequence emitted *after* calling this function.
  710. */
  711. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  712. struct vmw_sw_context *sw_context)
  713. {
  714. /*
  715. * The validate list should still hold references to all
  716. * contexts here.
  717. */
  718. if (sw_context->needs_post_query_barrier) {
  719. struct vmw_res_cache_entry *ctx_entry =
  720. &sw_context->res_cache[vmw_res_context];
  721. struct vmw_resource *ctx;
  722. int ret;
  723. BUG_ON(!ctx_entry->valid);
  724. ctx = ctx_entry->res;
  725. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  726. if (unlikely(ret != 0))
  727. DRM_ERROR("Out of fifo space for dummy query.\n");
  728. }
  729. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  730. if (dev_priv->pinned_bo) {
  731. vmw_bo_pin(dev_priv->pinned_bo, false);
  732. ttm_bo_unref(&dev_priv->pinned_bo);
  733. }
  734. if (!sw_context->needs_post_query_barrier) {
  735. vmw_bo_pin(sw_context->cur_query_bo, true);
  736. /*
  737. * We pin also the dummy_query_bo buffer so that we
  738. * don't need to validate it when emitting
  739. * dummy queries in context destroy paths.
  740. */
  741. vmw_bo_pin(dev_priv->dummy_query_bo, true);
  742. dev_priv->dummy_query_bo_pinned = true;
  743. BUG_ON(sw_context->last_query_ctx == NULL);
  744. dev_priv->query_cid = sw_context->last_query_ctx->id;
  745. dev_priv->query_cid_valid = true;
  746. dev_priv->pinned_bo =
  747. ttm_bo_reference(sw_context->cur_query_bo);
  748. }
  749. }
  750. }
  751. /**
  752. * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
  753. * handle to a MOB id.
  754. *
  755. * @dev_priv: Pointer to a device private structure.
  756. * @sw_context: The software context used for this command batch validation.
  757. * @id: Pointer to the user-space handle to be translated.
  758. * @vmw_bo_p: Points to a location that, on successful return will carry
  759. * a reference-counted pointer to the DMA buffer identified by the
  760. * user-space handle in @id.
  761. *
  762. * This function saves information needed to translate a user-space buffer
  763. * handle to a MOB id. The translation does not take place immediately, but
  764. * during a call to vmw_apply_relocations(). This function builds a relocation
  765. * list and a list of buffers to validate. The former needs to be freed using
  766. * either vmw_apply_relocations() or vmw_free_relocations(). The latter
  767. * needs to be freed using vmw_clear_validations.
  768. */
  769. static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
  770. struct vmw_sw_context *sw_context,
  771. SVGAMobId *id,
  772. struct vmw_dma_buffer **vmw_bo_p)
  773. {
  774. struct vmw_dma_buffer *vmw_bo = NULL;
  775. struct ttm_buffer_object *bo;
  776. uint32_t handle = *id;
  777. struct vmw_relocation *reloc;
  778. int ret;
  779. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo);
  780. if (unlikely(ret != 0)) {
  781. DRM_ERROR("Could not find or use MOB buffer.\n");
  782. ret = -EINVAL;
  783. goto out_no_reloc;
  784. }
  785. bo = &vmw_bo->base;
  786. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  787. DRM_ERROR("Max number relocations per submission"
  788. " exceeded\n");
  789. ret = -EINVAL;
  790. goto out_no_reloc;
  791. }
  792. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  793. reloc->mob_loc = id;
  794. reloc->location = NULL;
  795. ret = vmw_bo_to_validate_list(sw_context, bo, true, &reloc->index);
  796. if (unlikely(ret != 0))
  797. goto out_no_reloc;
  798. *vmw_bo_p = vmw_bo;
  799. return 0;
  800. out_no_reloc:
  801. vmw_dmabuf_unreference(&vmw_bo);
  802. *vmw_bo_p = NULL;
  803. return ret;
  804. }
  805. /**
  806. * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
  807. * handle to a valid SVGAGuestPtr
  808. *
  809. * @dev_priv: Pointer to a device private structure.
  810. * @sw_context: The software context used for this command batch validation.
  811. * @ptr: Pointer to the user-space handle to be translated.
  812. * @vmw_bo_p: Points to a location that, on successful return will carry
  813. * a reference-counted pointer to the DMA buffer identified by the
  814. * user-space handle in @id.
  815. *
  816. * This function saves information needed to translate a user-space buffer
  817. * handle to a valid SVGAGuestPtr. The translation does not take place
  818. * immediately, but during a call to vmw_apply_relocations().
  819. * This function builds a relocation list and a list of buffers to validate.
  820. * The former needs to be freed using either vmw_apply_relocations() or
  821. * vmw_free_relocations(). The latter needs to be freed using
  822. * vmw_clear_validations.
  823. */
  824. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  825. struct vmw_sw_context *sw_context,
  826. SVGAGuestPtr *ptr,
  827. struct vmw_dma_buffer **vmw_bo_p)
  828. {
  829. struct vmw_dma_buffer *vmw_bo = NULL;
  830. struct ttm_buffer_object *bo;
  831. uint32_t handle = ptr->gmrId;
  832. struct vmw_relocation *reloc;
  833. int ret;
  834. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo);
  835. if (unlikely(ret != 0)) {
  836. DRM_ERROR("Could not find or use GMR region.\n");
  837. ret = -EINVAL;
  838. goto out_no_reloc;
  839. }
  840. bo = &vmw_bo->base;
  841. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  842. DRM_ERROR("Max number relocations per submission"
  843. " exceeded\n");
  844. ret = -EINVAL;
  845. goto out_no_reloc;
  846. }
  847. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  848. reloc->location = ptr;
  849. ret = vmw_bo_to_validate_list(sw_context, bo, false, &reloc->index);
  850. if (unlikely(ret != 0))
  851. goto out_no_reloc;
  852. *vmw_bo_p = vmw_bo;
  853. return 0;
  854. out_no_reloc:
  855. vmw_dmabuf_unreference(&vmw_bo);
  856. *vmw_bo_p = NULL;
  857. return ret;
  858. }
  859. /**
  860. * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
  861. *
  862. * @dev_priv: Pointer to a device private struct.
  863. * @sw_context: The software context used for this command submission.
  864. * @header: Pointer to the command header in the command stream.
  865. */
  866. static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
  867. struct vmw_sw_context *sw_context,
  868. SVGA3dCmdHeader *header)
  869. {
  870. struct vmw_begin_gb_query_cmd {
  871. SVGA3dCmdHeader header;
  872. SVGA3dCmdBeginGBQuery q;
  873. } *cmd;
  874. cmd = container_of(header, struct vmw_begin_gb_query_cmd,
  875. header);
  876. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  877. user_context_converter, &cmd->q.cid,
  878. NULL);
  879. }
  880. /**
  881. * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
  882. *
  883. * @dev_priv: Pointer to a device private struct.
  884. * @sw_context: The software context used for this command submission.
  885. * @header: Pointer to the command header in the command stream.
  886. */
  887. static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
  888. struct vmw_sw_context *sw_context,
  889. SVGA3dCmdHeader *header)
  890. {
  891. struct vmw_begin_query_cmd {
  892. SVGA3dCmdHeader header;
  893. SVGA3dCmdBeginQuery q;
  894. } *cmd;
  895. cmd = container_of(header, struct vmw_begin_query_cmd,
  896. header);
  897. if (unlikely(dev_priv->has_mob)) {
  898. struct {
  899. SVGA3dCmdHeader header;
  900. SVGA3dCmdBeginGBQuery q;
  901. } gb_cmd;
  902. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  903. gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
  904. gb_cmd.header.size = cmd->header.size;
  905. gb_cmd.q.cid = cmd->q.cid;
  906. gb_cmd.q.type = cmd->q.type;
  907. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  908. return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
  909. }
  910. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  911. user_context_converter, &cmd->q.cid,
  912. NULL);
  913. }
  914. /**
  915. * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
  916. *
  917. * @dev_priv: Pointer to a device private struct.
  918. * @sw_context: The software context used for this command submission.
  919. * @header: Pointer to the command header in the command stream.
  920. */
  921. static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
  922. struct vmw_sw_context *sw_context,
  923. SVGA3dCmdHeader *header)
  924. {
  925. struct vmw_dma_buffer *vmw_bo;
  926. struct vmw_query_cmd {
  927. SVGA3dCmdHeader header;
  928. SVGA3dCmdEndGBQuery q;
  929. } *cmd;
  930. int ret;
  931. cmd = container_of(header, struct vmw_query_cmd, header);
  932. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  933. if (unlikely(ret != 0))
  934. return ret;
  935. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  936. &cmd->q.mobid,
  937. &vmw_bo);
  938. if (unlikely(ret != 0))
  939. return ret;
  940. ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
  941. vmw_dmabuf_unreference(&vmw_bo);
  942. return ret;
  943. }
  944. /**
  945. * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
  946. *
  947. * @dev_priv: Pointer to a device private struct.
  948. * @sw_context: The software context used for this command submission.
  949. * @header: Pointer to the command header in the command stream.
  950. */
  951. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  952. struct vmw_sw_context *sw_context,
  953. SVGA3dCmdHeader *header)
  954. {
  955. struct vmw_dma_buffer *vmw_bo;
  956. struct vmw_query_cmd {
  957. SVGA3dCmdHeader header;
  958. SVGA3dCmdEndQuery q;
  959. } *cmd;
  960. int ret;
  961. cmd = container_of(header, struct vmw_query_cmd, header);
  962. if (dev_priv->has_mob) {
  963. struct {
  964. SVGA3dCmdHeader header;
  965. SVGA3dCmdEndGBQuery q;
  966. } gb_cmd;
  967. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  968. gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
  969. gb_cmd.header.size = cmd->header.size;
  970. gb_cmd.q.cid = cmd->q.cid;
  971. gb_cmd.q.type = cmd->q.type;
  972. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  973. gb_cmd.q.offset = cmd->q.guestResult.offset;
  974. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  975. return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
  976. }
  977. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  978. if (unlikely(ret != 0))
  979. return ret;
  980. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  981. &cmd->q.guestResult,
  982. &vmw_bo);
  983. if (unlikely(ret != 0))
  984. return ret;
  985. ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
  986. vmw_dmabuf_unreference(&vmw_bo);
  987. return ret;
  988. }
  989. /**
  990. * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
  991. *
  992. * @dev_priv: Pointer to a device private struct.
  993. * @sw_context: The software context used for this command submission.
  994. * @header: Pointer to the command header in the command stream.
  995. */
  996. static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
  997. struct vmw_sw_context *sw_context,
  998. SVGA3dCmdHeader *header)
  999. {
  1000. struct vmw_dma_buffer *vmw_bo;
  1001. struct vmw_query_cmd {
  1002. SVGA3dCmdHeader header;
  1003. SVGA3dCmdWaitForGBQuery q;
  1004. } *cmd;
  1005. int ret;
  1006. cmd = container_of(header, struct vmw_query_cmd, header);
  1007. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1008. if (unlikely(ret != 0))
  1009. return ret;
  1010. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  1011. &cmd->q.mobid,
  1012. &vmw_bo);
  1013. if (unlikely(ret != 0))
  1014. return ret;
  1015. vmw_dmabuf_unreference(&vmw_bo);
  1016. return 0;
  1017. }
  1018. /**
  1019. * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
  1020. *
  1021. * @dev_priv: Pointer to a device private struct.
  1022. * @sw_context: The software context used for this command submission.
  1023. * @header: Pointer to the command header in the command stream.
  1024. */
  1025. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  1026. struct vmw_sw_context *sw_context,
  1027. SVGA3dCmdHeader *header)
  1028. {
  1029. struct vmw_dma_buffer *vmw_bo;
  1030. struct vmw_query_cmd {
  1031. SVGA3dCmdHeader header;
  1032. SVGA3dCmdWaitForQuery q;
  1033. } *cmd;
  1034. int ret;
  1035. cmd = container_of(header, struct vmw_query_cmd, header);
  1036. if (dev_priv->has_mob) {
  1037. struct {
  1038. SVGA3dCmdHeader header;
  1039. SVGA3dCmdWaitForGBQuery q;
  1040. } gb_cmd;
  1041. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  1042. gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  1043. gb_cmd.header.size = cmd->header.size;
  1044. gb_cmd.q.cid = cmd->q.cid;
  1045. gb_cmd.q.type = cmd->q.type;
  1046. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  1047. gb_cmd.q.offset = cmd->q.guestResult.offset;
  1048. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  1049. return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
  1050. }
  1051. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1052. if (unlikely(ret != 0))
  1053. return ret;
  1054. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1055. &cmd->q.guestResult,
  1056. &vmw_bo);
  1057. if (unlikely(ret != 0))
  1058. return ret;
  1059. vmw_dmabuf_unreference(&vmw_bo);
  1060. return 0;
  1061. }
  1062. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  1063. struct vmw_sw_context *sw_context,
  1064. SVGA3dCmdHeader *header)
  1065. {
  1066. struct vmw_dma_buffer *vmw_bo = NULL;
  1067. struct vmw_surface *srf = NULL;
  1068. struct vmw_dma_cmd {
  1069. SVGA3dCmdHeader header;
  1070. SVGA3dCmdSurfaceDMA dma;
  1071. } *cmd;
  1072. int ret;
  1073. SVGA3dCmdSurfaceDMASuffix *suffix;
  1074. uint32_t bo_size;
  1075. cmd = container_of(header, struct vmw_dma_cmd, header);
  1076. suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->dma +
  1077. header->size - sizeof(*suffix));
  1078. /* Make sure device and verifier stays in sync. */
  1079. if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
  1080. DRM_ERROR("Invalid DMA suffix size.\n");
  1081. return -EINVAL;
  1082. }
  1083. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1084. &cmd->dma.guest.ptr,
  1085. &vmw_bo);
  1086. if (unlikely(ret != 0))
  1087. return ret;
  1088. /* Make sure DMA doesn't cross BO boundaries. */
  1089. bo_size = vmw_bo->base.num_pages * PAGE_SIZE;
  1090. if (unlikely(cmd->dma.guest.ptr.offset > bo_size)) {
  1091. DRM_ERROR("Invalid DMA offset.\n");
  1092. return -EINVAL;
  1093. }
  1094. bo_size -= cmd->dma.guest.ptr.offset;
  1095. if (unlikely(suffix->maximumOffset > bo_size))
  1096. suffix->maximumOffset = bo_size;
  1097. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1098. user_surface_converter, &cmd->dma.host.sid,
  1099. NULL);
  1100. if (unlikely(ret != 0)) {
  1101. if (unlikely(ret != -ERESTARTSYS))
  1102. DRM_ERROR("could not find surface for DMA.\n");
  1103. goto out_no_surface;
  1104. }
  1105. srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
  1106. vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base,
  1107. header);
  1108. out_no_surface:
  1109. vmw_dmabuf_unreference(&vmw_bo);
  1110. return ret;
  1111. }
  1112. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  1113. struct vmw_sw_context *sw_context,
  1114. SVGA3dCmdHeader *header)
  1115. {
  1116. struct vmw_draw_cmd {
  1117. SVGA3dCmdHeader header;
  1118. SVGA3dCmdDrawPrimitives body;
  1119. } *cmd;
  1120. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  1121. (unsigned long)header + sizeof(*cmd));
  1122. SVGA3dPrimitiveRange *range;
  1123. uint32_t i;
  1124. uint32_t maxnum;
  1125. int ret;
  1126. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1127. if (unlikely(ret != 0))
  1128. return ret;
  1129. cmd = container_of(header, struct vmw_draw_cmd, header);
  1130. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  1131. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  1132. DRM_ERROR("Illegal number of vertex declarations.\n");
  1133. return -EINVAL;
  1134. }
  1135. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  1136. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1137. user_surface_converter,
  1138. &decl->array.surfaceId, NULL);
  1139. if (unlikely(ret != 0))
  1140. return ret;
  1141. }
  1142. maxnum = (header->size - sizeof(cmd->body) -
  1143. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  1144. if (unlikely(cmd->body.numRanges > maxnum)) {
  1145. DRM_ERROR("Illegal number of index ranges.\n");
  1146. return -EINVAL;
  1147. }
  1148. range = (SVGA3dPrimitiveRange *) decl;
  1149. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  1150. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1151. user_surface_converter,
  1152. &range->indexArray.surfaceId, NULL);
  1153. if (unlikely(ret != 0))
  1154. return ret;
  1155. }
  1156. return 0;
  1157. }
  1158. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  1159. struct vmw_sw_context *sw_context,
  1160. SVGA3dCmdHeader *header)
  1161. {
  1162. struct vmw_tex_state_cmd {
  1163. SVGA3dCmdHeader header;
  1164. SVGA3dCmdSetTextureState state;
  1165. } *cmd;
  1166. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  1167. ((unsigned long) header + header->size + sizeof(header));
  1168. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  1169. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  1170. struct vmw_resource_val_node *ctx_node;
  1171. struct vmw_resource_val_node *res_node;
  1172. int ret;
  1173. cmd = container_of(header, struct vmw_tex_state_cmd,
  1174. header);
  1175. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1176. user_context_converter, &cmd->state.cid,
  1177. &ctx_node);
  1178. if (unlikely(ret != 0))
  1179. return ret;
  1180. for (; cur_state < last_state; ++cur_state) {
  1181. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  1182. continue;
  1183. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1184. user_surface_converter,
  1185. &cur_state->value, &res_node);
  1186. if (unlikely(ret != 0))
  1187. return ret;
  1188. if (dev_priv->has_mob) {
  1189. struct vmw_ctx_bindinfo bi;
  1190. bi.ctx = ctx_node->res;
  1191. bi.res = res_node ? res_node->res : NULL;
  1192. bi.bt = vmw_ctx_binding_tex;
  1193. bi.i1.texture_stage = cur_state->stage;
  1194. vmw_context_binding_add(ctx_node->staged_bindings,
  1195. &bi);
  1196. }
  1197. }
  1198. return 0;
  1199. }
  1200. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  1201. struct vmw_sw_context *sw_context,
  1202. void *buf)
  1203. {
  1204. struct vmw_dma_buffer *vmw_bo;
  1205. int ret;
  1206. struct {
  1207. uint32_t header;
  1208. SVGAFifoCmdDefineGMRFB body;
  1209. } *cmd = buf;
  1210. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1211. &cmd->body.ptr,
  1212. &vmw_bo);
  1213. if (unlikely(ret != 0))
  1214. return ret;
  1215. vmw_dmabuf_unreference(&vmw_bo);
  1216. return ret;
  1217. }
  1218. /**
  1219. * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
  1220. *
  1221. * @dev_priv: Pointer to a device private struct.
  1222. * @sw_context: The software context being used for this batch.
  1223. * @res_type: The resource type.
  1224. * @converter: Information about user-space binding for this resource type.
  1225. * @res_id: Pointer to the user-space resource handle in the command stream.
  1226. * @buf_id: Pointer to the user-space backup buffer handle in the command
  1227. * stream.
  1228. * @backup_offset: Offset of backup into MOB.
  1229. *
  1230. * This function prepares for registering a switch of backup buffers
  1231. * in the resource metadata just prior to unreserving.
  1232. */
  1233. static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
  1234. struct vmw_sw_context *sw_context,
  1235. enum vmw_res_type res_type,
  1236. const struct vmw_user_resource_conv
  1237. *converter,
  1238. uint32_t *res_id,
  1239. uint32_t *buf_id,
  1240. unsigned long backup_offset)
  1241. {
  1242. int ret;
  1243. struct vmw_dma_buffer *dma_buf;
  1244. struct vmw_resource_val_node *val_node;
  1245. ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
  1246. converter, res_id, &val_node);
  1247. if (unlikely(ret != 0))
  1248. return ret;
  1249. ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &dma_buf);
  1250. if (unlikely(ret != 0))
  1251. return ret;
  1252. if (val_node->first_usage)
  1253. val_node->no_buffer_needed = true;
  1254. vmw_dmabuf_unreference(&val_node->new_backup);
  1255. val_node->new_backup = dma_buf;
  1256. val_node->new_backup_offset = backup_offset;
  1257. return 0;
  1258. }
  1259. /**
  1260. * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
  1261. * command
  1262. *
  1263. * @dev_priv: Pointer to a device private struct.
  1264. * @sw_context: The software context being used for this batch.
  1265. * @header: Pointer to the command header in the command stream.
  1266. */
  1267. static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
  1268. struct vmw_sw_context *sw_context,
  1269. SVGA3dCmdHeader *header)
  1270. {
  1271. struct vmw_bind_gb_surface_cmd {
  1272. SVGA3dCmdHeader header;
  1273. SVGA3dCmdBindGBSurface body;
  1274. } *cmd;
  1275. cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
  1276. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
  1277. user_surface_converter,
  1278. &cmd->body.sid, &cmd->body.mobid,
  1279. 0);
  1280. }
  1281. /**
  1282. * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
  1283. * command
  1284. *
  1285. * @dev_priv: Pointer to a device private struct.
  1286. * @sw_context: The software context being used for this batch.
  1287. * @header: Pointer to the command header in the command stream.
  1288. */
  1289. static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
  1290. struct vmw_sw_context *sw_context,
  1291. SVGA3dCmdHeader *header)
  1292. {
  1293. struct vmw_gb_surface_cmd {
  1294. SVGA3dCmdHeader header;
  1295. SVGA3dCmdUpdateGBImage body;
  1296. } *cmd;
  1297. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1298. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1299. user_surface_converter,
  1300. &cmd->body.image.sid, NULL);
  1301. }
  1302. /**
  1303. * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
  1304. * command
  1305. *
  1306. * @dev_priv: Pointer to a device private struct.
  1307. * @sw_context: The software context being used for this batch.
  1308. * @header: Pointer to the command header in the command stream.
  1309. */
  1310. static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
  1311. struct vmw_sw_context *sw_context,
  1312. SVGA3dCmdHeader *header)
  1313. {
  1314. struct vmw_gb_surface_cmd {
  1315. SVGA3dCmdHeader header;
  1316. SVGA3dCmdUpdateGBSurface body;
  1317. } *cmd;
  1318. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1319. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1320. user_surface_converter,
  1321. &cmd->body.sid, NULL);
  1322. }
  1323. /**
  1324. * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
  1325. * command
  1326. *
  1327. * @dev_priv: Pointer to a device private struct.
  1328. * @sw_context: The software context being used for this batch.
  1329. * @header: Pointer to the command header in the command stream.
  1330. */
  1331. static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
  1332. struct vmw_sw_context *sw_context,
  1333. SVGA3dCmdHeader *header)
  1334. {
  1335. struct vmw_gb_surface_cmd {
  1336. SVGA3dCmdHeader header;
  1337. SVGA3dCmdReadbackGBImage body;
  1338. } *cmd;
  1339. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1340. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1341. user_surface_converter,
  1342. &cmd->body.image.sid, NULL);
  1343. }
  1344. /**
  1345. * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
  1346. * command
  1347. *
  1348. * @dev_priv: Pointer to a device private struct.
  1349. * @sw_context: The software context being used for this batch.
  1350. * @header: Pointer to the command header in the command stream.
  1351. */
  1352. static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
  1353. struct vmw_sw_context *sw_context,
  1354. SVGA3dCmdHeader *header)
  1355. {
  1356. struct vmw_gb_surface_cmd {
  1357. SVGA3dCmdHeader header;
  1358. SVGA3dCmdReadbackGBSurface body;
  1359. } *cmd;
  1360. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1361. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1362. user_surface_converter,
  1363. &cmd->body.sid, NULL);
  1364. }
  1365. /**
  1366. * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
  1367. * command
  1368. *
  1369. * @dev_priv: Pointer to a device private struct.
  1370. * @sw_context: The software context being used for this batch.
  1371. * @header: Pointer to the command header in the command stream.
  1372. */
  1373. static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
  1374. struct vmw_sw_context *sw_context,
  1375. SVGA3dCmdHeader *header)
  1376. {
  1377. struct vmw_gb_surface_cmd {
  1378. SVGA3dCmdHeader header;
  1379. SVGA3dCmdInvalidateGBImage body;
  1380. } *cmd;
  1381. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1382. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1383. user_surface_converter,
  1384. &cmd->body.image.sid, NULL);
  1385. }
  1386. /**
  1387. * vmw_cmd_invalidate_gb_surface - Validate an
  1388. * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
  1389. *
  1390. * @dev_priv: Pointer to a device private struct.
  1391. * @sw_context: The software context being used for this batch.
  1392. * @header: Pointer to the command header in the command stream.
  1393. */
  1394. static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
  1395. struct vmw_sw_context *sw_context,
  1396. SVGA3dCmdHeader *header)
  1397. {
  1398. struct vmw_gb_surface_cmd {
  1399. SVGA3dCmdHeader header;
  1400. SVGA3dCmdInvalidateGBSurface body;
  1401. } *cmd;
  1402. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1403. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1404. user_surface_converter,
  1405. &cmd->body.sid, NULL);
  1406. }
  1407. /**
  1408. * vmw_cmd_shader_define - Validate an SVGA_3D_CMD_SHADER_DEFINE
  1409. * command
  1410. *
  1411. * @dev_priv: Pointer to a device private struct.
  1412. * @sw_context: The software context being used for this batch.
  1413. * @header: Pointer to the command header in the command stream.
  1414. */
  1415. static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
  1416. struct vmw_sw_context *sw_context,
  1417. SVGA3dCmdHeader *header)
  1418. {
  1419. struct vmw_shader_define_cmd {
  1420. SVGA3dCmdHeader header;
  1421. SVGA3dCmdDefineShader body;
  1422. } *cmd;
  1423. int ret;
  1424. size_t size;
  1425. struct vmw_resource_val_node *val;
  1426. cmd = container_of(header, struct vmw_shader_define_cmd,
  1427. header);
  1428. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1429. user_context_converter, &cmd->body.cid,
  1430. &val);
  1431. if (unlikely(ret != 0))
  1432. return ret;
  1433. if (unlikely(!dev_priv->has_mob))
  1434. return 0;
  1435. size = cmd->header.size - sizeof(cmd->body);
  1436. ret = vmw_compat_shader_add(dev_priv,
  1437. vmw_context_res_man(val->res),
  1438. cmd->body.shid, cmd + 1,
  1439. cmd->body.type, size,
  1440. &sw_context->staged_cmd_res);
  1441. if (unlikely(ret != 0))
  1442. return ret;
  1443. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1444. NULL, &cmd->header.id -
  1445. sw_context->buf_start);
  1446. return 0;
  1447. }
  1448. /**
  1449. * vmw_cmd_shader_destroy - Validate an SVGA_3D_CMD_SHADER_DESTROY
  1450. * command
  1451. *
  1452. * @dev_priv: Pointer to a device private struct.
  1453. * @sw_context: The software context being used for this batch.
  1454. * @header: Pointer to the command header in the command stream.
  1455. */
  1456. static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
  1457. struct vmw_sw_context *sw_context,
  1458. SVGA3dCmdHeader *header)
  1459. {
  1460. struct vmw_shader_destroy_cmd {
  1461. SVGA3dCmdHeader header;
  1462. SVGA3dCmdDestroyShader body;
  1463. } *cmd;
  1464. int ret;
  1465. struct vmw_resource_val_node *val;
  1466. cmd = container_of(header, struct vmw_shader_destroy_cmd,
  1467. header);
  1468. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1469. user_context_converter, &cmd->body.cid,
  1470. &val);
  1471. if (unlikely(ret != 0))
  1472. return ret;
  1473. if (unlikely(!dev_priv->has_mob))
  1474. return 0;
  1475. ret = vmw_compat_shader_remove(vmw_context_res_man(val->res),
  1476. cmd->body.shid,
  1477. cmd->body.type,
  1478. &sw_context->staged_cmd_res);
  1479. if (unlikely(ret != 0))
  1480. return ret;
  1481. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1482. NULL, &cmd->header.id -
  1483. sw_context->buf_start);
  1484. return 0;
  1485. }
  1486. /**
  1487. * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
  1488. * command
  1489. *
  1490. * @dev_priv: Pointer to a device private struct.
  1491. * @sw_context: The software context being used for this batch.
  1492. * @header: Pointer to the command header in the command stream.
  1493. */
  1494. static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
  1495. struct vmw_sw_context *sw_context,
  1496. SVGA3dCmdHeader *header)
  1497. {
  1498. struct vmw_set_shader_cmd {
  1499. SVGA3dCmdHeader header;
  1500. SVGA3dCmdSetShader body;
  1501. } *cmd;
  1502. struct vmw_resource_val_node *ctx_node, *res_node = NULL;
  1503. struct vmw_ctx_bindinfo bi;
  1504. struct vmw_resource *res = NULL;
  1505. int ret;
  1506. cmd = container_of(header, struct vmw_set_shader_cmd,
  1507. header);
  1508. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1509. user_context_converter, &cmd->body.cid,
  1510. &ctx_node);
  1511. if (unlikely(ret != 0))
  1512. return ret;
  1513. if (!dev_priv->has_mob)
  1514. return 0;
  1515. if (cmd->body.shid != SVGA3D_INVALID_ID) {
  1516. res = vmw_compat_shader_lookup
  1517. (vmw_context_res_man(ctx_node->res),
  1518. cmd->body.shid,
  1519. cmd->body.type);
  1520. if (!IS_ERR(res)) {
  1521. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context,
  1522. vmw_res_shader,
  1523. &cmd->body.shid, res,
  1524. &res_node);
  1525. vmw_resource_unreference(&res);
  1526. if (unlikely(ret != 0))
  1527. return ret;
  1528. }
  1529. }
  1530. if (!res_node) {
  1531. ret = vmw_cmd_res_check(dev_priv, sw_context,
  1532. vmw_res_shader,
  1533. user_shader_converter,
  1534. &cmd->body.shid, &res_node);
  1535. if (unlikely(ret != 0))
  1536. return ret;
  1537. }
  1538. bi.ctx = ctx_node->res;
  1539. bi.res = res_node ? res_node->res : NULL;
  1540. bi.bt = vmw_ctx_binding_shader;
  1541. bi.i1.shader_type = cmd->body.type;
  1542. return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
  1543. }
  1544. /**
  1545. * vmw_cmd_set_shader_const - Validate an SVGA_3D_CMD_SET_SHADER_CONST
  1546. * command
  1547. *
  1548. * @dev_priv: Pointer to a device private struct.
  1549. * @sw_context: The software context being used for this batch.
  1550. * @header: Pointer to the command header in the command stream.
  1551. */
  1552. static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
  1553. struct vmw_sw_context *sw_context,
  1554. SVGA3dCmdHeader *header)
  1555. {
  1556. struct vmw_set_shader_const_cmd {
  1557. SVGA3dCmdHeader header;
  1558. SVGA3dCmdSetShaderConst body;
  1559. } *cmd;
  1560. int ret;
  1561. cmd = container_of(header, struct vmw_set_shader_const_cmd,
  1562. header);
  1563. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1564. user_context_converter, &cmd->body.cid,
  1565. NULL);
  1566. if (unlikely(ret != 0))
  1567. return ret;
  1568. if (dev_priv->has_mob)
  1569. header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
  1570. return 0;
  1571. }
  1572. /**
  1573. * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
  1574. * command
  1575. *
  1576. * @dev_priv: Pointer to a device private struct.
  1577. * @sw_context: The software context being used for this batch.
  1578. * @header: Pointer to the command header in the command stream.
  1579. */
  1580. static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
  1581. struct vmw_sw_context *sw_context,
  1582. SVGA3dCmdHeader *header)
  1583. {
  1584. struct vmw_bind_gb_shader_cmd {
  1585. SVGA3dCmdHeader header;
  1586. SVGA3dCmdBindGBShader body;
  1587. } *cmd;
  1588. cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
  1589. header);
  1590. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
  1591. user_shader_converter,
  1592. &cmd->body.shid, &cmd->body.mobid,
  1593. cmd->body.offsetInBytes);
  1594. }
  1595. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  1596. struct vmw_sw_context *sw_context,
  1597. void *buf, uint32_t *size)
  1598. {
  1599. uint32_t size_remaining = *size;
  1600. uint32_t cmd_id;
  1601. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  1602. switch (cmd_id) {
  1603. case SVGA_CMD_UPDATE:
  1604. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  1605. break;
  1606. case SVGA_CMD_DEFINE_GMRFB:
  1607. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  1608. break;
  1609. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  1610. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1611. break;
  1612. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  1613. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1614. break;
  1615. default:
  1616. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  1617. return -EINVAL;
  1618. }
  1619. if (*size > size_remaining) {
  1620. DRM_ERROR("Invalid SVGA command (size mismatch):"
  1621. " %u.\n", cmd_id);
  1622. return -EINVAL;
  1623. }
  1624. if (unlikely(!sw_context->kernel)) {
  1625. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  1626. return -EPERM;
  1627. }
  1628. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  1629. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  1630. return 0;
  1631. }
  1632. static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
  1633. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
  1634. false, false, false),
  1635. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
  1636. false, false, false),
  1637. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
  1638. true, false, false),
  1639. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
  1640. true, false, false),
  1641. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
  1642. true, false, false),
  1643. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
  1644. false, false, false),
  1645. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
  1646. false, false, false),
  1647. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
  1648. true, false, false),
  1649. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
  1650. true, false, false),
  1651. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
  1652. true, false, false),
  1653. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  1654. &vmw_cmd_set_render_target_check, true, false, false),
  1655. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
  1656. true, false, false),
  1657. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
  1658. true, false, false),
  1659. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
  1660. true, false, false),
  1661. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
  1662. true, false, false),
  1663. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
  1664. true, false, false),
  1665. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
  1666. true, false, false),
  1667. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
  1668. true, false, false),
  1669. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
  1670. false, false, false),
  1671. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
  1672. true, false, false),
  1673. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
  1674. true, false, false),
  1675. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
  1676. true, false, false),
  1677. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
  1678. true, false, false),
  1679. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
  1680. true, false, false),
  1681. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
  1682. true, false, false),
  1683. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
  1684. true, false, false),
  1685. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
  1686. true, false, false),
  1687. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
  1688. true, false, false),
  1689. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
  1690. true, false, false),
  1691. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  1692. &vmw_cmd_blt_surf_screen_check, false, false, false),
  1693. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
  1694. false, false, false),
  1695. VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
  1696. false, false, false),
  1697. VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
  1698. false, false, false),
  1699. VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
  1700. false, false, false),
  1701. VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
  1702. false, false, false),
  1703. VMW_CMD_DEF(SVGA_3D_CMD_SET_UNITY_SURFACE_COOKIE, &vmw_cmd_invalid,
  1704. false, false, false),
  1705. VMW_CMD_DEF(SVGA_3D_CMD_OPEN_CONTEXT_SURFACE, &vmw_cmd_invalid,
  1706. false, false, false),
  1707. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
  1708. false, false, false),
  1709. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
  1710. false, false, false),
  1711. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
  1712. false, false, false),
  1713. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
  1714. false, false, false),
  1715. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
  1716. false, false, false),
  1717. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
  1718. false, false, false),
  1719. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
  1720. false, false, true),
  1721. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
  1722. false, false, true),
  1723. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
  1724. false, false, true),
  1725. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
  1726. false, false, true),
  1727. VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB, &vmw_cmd_invalid,
  1728. false, false, true),
  1729. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
  1730. false, false, true),
  1731. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
  1732. false, false, true),
  1733. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
  1734. false, false, true),
  1735. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
  1736. true, false, true),
  1737. VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
  1738. false, false, true),
  1739. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
  1740. true, false, true),
  1741. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
  1742. &vmw_cmd_update_gb_surface, true, false, true),
  1743. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
  1744. &vmw_cmd_readback_gb_image, true, false, true),
  1745. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
  1746. &vmw_cmd_readback_gb_surface, true, false, true),
  1747. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
  1748. &vmw_cmd_invalidate_gb_image, true, false, true),
  1749. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
  1750. &vmw_cmd_invalidate_gb_surface, true, false, true),
  1751. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
  1752. false, false, true),
  1753. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
  1754. false, false, true),
  1755. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
  1756. false, false, true),
  1757. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
  1758. false, false, true),
  1759. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
  1760. false, false, true),
  1761. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
  1762. false, false, true),
  1763. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
  1764. true, false, true),
  1765. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
  1766. false, false, true),
  1767. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
  1768. false, false, false),
  1769. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
  1770. true, false, true),
  1771. VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
  1772. true, false, true),
  1773. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
  1774. true, false, true),
  1775. VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
  1776. true, false, true),
  1777. VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
  1778. false, false, true),
  1779. VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
  1780. false, false, true),
  1781. VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
  1782. false, false, true),
  1783. VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
  1784. false, false, true),
  1785. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
  1786. false, false, true),
  1787. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
  1788. false, false, true),
  1789. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
  1790. false, false, true),
  1791. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
  1792. false, false, true),
  1793. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  1794. false, false, true),
  1795. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  1796. false, false, true),
  1797. VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
  1798. true, false, true)
  1799. };
  1800. static int vmw_cmd_check(struct vmw_private *dev_priv,
  1801. struct vmw_sw_context *sw_context,
  1802. void *buf, uint32_t *size)
  1803. {
  1804. uint32_t cmd_id;
  1805. uint32_t size_remaining = *size;
  1806. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  1807. int ret;
  1808. const struct vmw_cmd_entry *entry;
  1809. bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
  1810. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  1811. /* Handle any none 3D commands */
  1812. if (unlikely(cmd_id < SVGA_CMD_MAX))
  1813. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  1814. cmd_id = le32_to_cpu(header->id);
  1815. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  1816. cmd_id -= SVGA_3D_CMD_BASE;
  1817. if (unlikely(*size > size_remaining))
  1818. goto out_invalid;
  1819. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  1820. goto out_invalid;
  1821. entry = &vmw_cmd_entries[cmd_id];
  1822. if (unlikely(!entry->func))
  1823. goto out_invalid;
  1824. if (unlikely(!entry->user_allow && !sw_context->kernel))
  1825. goto out_privileged;
  1826. if (unlikely(entry->gb_disable && gb))
  1827. goto out_old;
  1828. if (unlikely(entry->gb_enable && !gb))
  1829. goto out_new;
  1830. ret = entry->func(dev_priv, sw_context, header);
  1831. if (unlikely(ret != 0))
  1832. goto out_invalid;
  1833. return 0;
  1834. out_invalid:
  1835. DRM_ERROR("Invalid SVGA3D command: %d\n",
  1836. cmd_id + SVGA_3D_CMD_BASE);
  1837. return -EINVAL;
  1838. out_privileged:
  1839. DRM_ERROR("Privileged SVGA3D command: %d\n",
  1840. cmd_id + SVGA_3D_CMD_BASE);
  1841. return -EPERM;
  1842. out_old:
  1843. DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
  1844. cmd_id + SVGA_3D_CMD_BASE);
  1845. return -EINVAL;
  1846. out_new:
  1847. DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
  1848. cmd_id + SVGA_3D_CMD_BASE);
  1849. return -EINVAL;
  1850. }
  1851. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  1852. struct vmw_sw_context *sw_context,
  1853. void *buf,
  1854. uint32_t size)
  1855. {
  1856. int32_t cur_size = size;
  1857. int ret;
  1858. sw_context->buf_start = buf;
  1859. while (cur_size > 0) {
  1860. size = cur_size;
  1861. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  1862. if (unlikely(ret != 0))
  1863. return ret;
  1864. buf = (void *)((unsigned long) buf + size);
  1865. cur_size -= size;
  1866. }
  1867. if (unlikely(cur_size != 0)) {
  1868. DRM_ERROR("Command verifier out of sync.\n");
  1869. return -EINVAL;
  1870. }
  1871. return 0;
  1872. }
  1873. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  1874. {
  1875. sw_context->cur_reloc = 0;
  1876. }
  1877. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  1878. {
  1879. uint32_t i;
  1880. struct vmw_relocation *reloc;
  1881. struct ttm_validate_buffer *validate;
  1882. struct ttm_buffer_object *bo;
  1883. for (i = 0; i < sw_context->cur_reloc; ++i) {
  1884. reloc = &sw_context->relocs[i];
  1885. validate = &sw_context->val_bufs[reloc->index].base;
  1886. bo = validate->bo;
  1887. switch (bo->mem.mem_type) {
  1888. case TTM_PL_VRAM:
  1889. reloc->location->offset += bo->offset;
  1890. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  1891. break;
  1892. case VMW_PL_GMR:
  1893. reloc->location->gmrId = bo->mem.start;
  1894. break;
  1895. case VMW_PL_MOB:
  1896. *reloc->mob_loc = bo->mem.start;
  1897. break;
  1898. default:
  1899. BUG();
  1900. }
  1901. }
  1902. vmw_free_relocations(sw_context);
  1903. }
  1904. /**
  1905. * vmw_resource_list_unrefererence - Free up a resource list and unreference
  1906. * all resources referenced by it.
  1907. *
  1908. * @list: The resource list.
  1909. */
  1910. static void vmw_resource_list_unreference(struct list_head *list)
  1911. {
  1912. struct vmw_resource_val_node *val, *val_next;
  1913. /*
  1914. * Drop references to resources held during command submission.
  1915. */
  1916. list_for_each_entry_safe(val, val_next, list, head) {
  1917. list_del_init(&val->head);
  1918. vmw_resource_unreference(&val->res);
  1919. if (unlikely(val->staged_bindings))
  1920. kfree(val->staged_bindings);
  1921. kfree(val);
  1922. }
  1923. }
  1924. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  1925. {
  1926. struct vmw_validate_buffer *entry, *next;
  1927. struct vmw_resource_val_node *val;
  1928. /*
  1929. * Drop references to DMA buffers held during command submission.
  1930. */
  1931. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  1932. base.head) {
  1933. list_del(&entry->base.head);
  1934. ttm_bo_unref(&entry->base.bo);
  1935. (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
  1936. sw_context->cur_val_buf--;
  1937. }
  1938. BUG_ON(sw_context->cur_val_buf != 0);
  1939. list_for_each_entry(val, &sw_context->resource_list, head)
  1940. (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
  1941. }
  1942. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  1943. struct ttm_buffer_object *bo,
  1944. bool validate_as_mob)
  1945. {
  1946. int ret;
  1947. /*
  1948. * Don't validate pinned buffers.
  1949. */
  1950. if (bo == dev_priv->pinned_bo ||
  1951. (bo == dev_priv->dummy_query_bo &&
  1952. dev_priv->dummy_query_bo_pinned))
  1953. return 0;
  1954. if (validate_as_mob)
  1955. return ttm_bo_validate(bo, &vmw_mob_placement, true, false);
  1956. /**
  1957. * Put BO in VRAM if there is space, otherwise as a GMR.
  1958. * If there is no space in VRAM and GMR ids are all used up,
  1959. * start evicting GMRs to make room. If the DMA buffer can't be
  1960. * used as a GMR, this will return -ENOMEM.
  1961. */
  1962. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false);
  1963. if (likely(ret == 0 || ret == -ERESTARTSYS))
  1964. return ret;
  1965. /**
  1966. * If that failed, try VRAM again, this time evicting
  1967. * previous contents.
  1968. */
  1969. DRM_INFO("Falling through to VRAM.\n");
  1970. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
  1971. return ret;
  1972. }
  1973. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  1974. struct vmw_sw_context *sw_context)
  1975. {
  1976. struct vmw_validate_buffer *entry;
  1977. int ret;
  1978. list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
  1979. ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
  1980. entry->validate_as_mob);
  1981. if (unlikely(ret != 0))
  1982. return ret;
  1983. }
  1984. return 0;
  1985. }
  1986. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  1987. uint32_t size)
  1988. {
  1989. if (likely(sw_context->cmd_bounce_size >= size))
  1990. return 0;
  1991. if (sw_context->cmd_bounce_size == 0)
  1992. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  1993. while (sw_context->cmd_bounce_size < size) {
  1994. sw_context->cmd_bounce_size =
  1995. PAGE_ALIGN(sw_context->cmd_bounce_size +
  1996. (sw_context->cmd_bounce_size >> 1));
  1997. }
  1998. if (sw_context->cmd_bounce != NULL)
  1999. vfree(sw_context->cmd_bounce);
  2000. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  2001. if (sw_context->cmd_bounce == NULL) {
  2002. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  2003. sw_context->cmd_bounce_size = 0;
  2004. return -ENOMEM;
  2005. }
  2006. return 0;
  2007. }
  2008. /**
  2009. * vmw_execbuf_fence_commands - create and submit a command stream fence
  2010. *
  2011. * Creates a fence object and submits a command stream marker.
  2012. * If this fails for some reason, We sync the fifo and return NULL.
  2013. * It is then safe to fence buffers with a NULL pointer.
  2014. *
  2015. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  2016. * a userspace handle if @p_handle is not NULL, otherwise not.
  2017. */
  2018. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  2019. struct vmw_private *dev_priv,
  2020. struct vmw_fence_obj **p_fence,
  2021. uint32_t *p_handle)
  2022. {
  2023. uint32_t sequence;
  2024. int ret;
  2025. bool synced = false;
  2026. /* p_handle implies file_priv. */
  2027. BUG_ON(p_handle != NULL && file_priv == NULL);
  2028. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  2029. if (unlikely(ret != 0)) {
  2030. DRM_ERROR("Fence submission error. Syncing.\n");
  2031. synced = true;
  2032. }
  2033. if (p_handle != NULL)
  2034. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  2035. sequence, p_fence, p_handle);
  2036. else
  2037. ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
  2038. if (unlikely(ret != 0 && !synced)) {
  2039. (void) vmw_fallback_wait(dev_priv, false, false,
  2040. sequence, false,
  2041. VMW_FENCE_WAIT_TIMEOUT);
  2042. *p_fence = NULL;
  2043. }
  2044. return 0;
  2045. }
  2046. /**
  2047. * vmw_execbuf_copy_fence_user - copy fence object information to
  2048. * user-space.
  2049. *
  2050. * @dev_priv: Pointer to a vmw_private struct.
  2051. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  2052. * @ret: Return value from fence object creation.
  2053. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  2054. * which the information should be copied.
  2055. * @fence: Pointer to the fenc object.
  2056. * @fence_handle: User-space fence handle.
  2057. *
  2058. * This function copies fence information to user-space. If copying fails,
  2059. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  2060. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  2061. * the error will hopefully be detected.
  2062. * Also if copying fails, user-space will be unable to signal the fence
  2063. * object so we wait for it immediately, and then unreference the
  2064. * user-space reference.
  2065. */
  2066. void
  2067. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  2068. struct vmw_fpriv *vmw_fp,
  2069. int ret,
  2070. struct drm_vmw_fence_rep __user *user_fence_rep,
  2071. struct vmw_fence_obj *fence,
  2072. uint32_t fence_handle)
  2073. {
  2074. struct drm_vmw_fence_rep fence_rep;
  2075. if (user_fence_rep == NULL)
  2076. return;
  2077. memset(&fence_rep, 0, sizeof(fence_rep));
  2078. fence_rep.error = ret;
  2079. if (ret == 0) {
  2080. BUG_ON(fence == NULL);
  2081. fence_rep.handle = fence_handle;
  2082. fence_rep.seqno = fence->base.seqno;
  2083. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  2084. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  2085. }
  2086. /*
  2087. * copy_to_user errors will be detected by user space not
  2088. * seeing fence_rep::error filled in. Typically
  2089. * user-space would have pre-set that member to -EFAULT.
  2090. */
  2091. ret = copy_to_user(user_fence_rep, &fence_rep,
  2092. sizeof(fence_rep));
  2093. /*
  2094. * User-space lost the fence object. We need to sync
  2095. * and unreference the handle.
  2096. */
  2097. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  2098. ttm_ref_object_base_unref(vmw_fp->tfile,
  2099. fence_handle, TTM_REF_USAGE);
  2100. DRM_ERROR("Fence copy error. Syncing.\n");
  2101. (void) vmw_fence_obj_wait(fence, false, false,
  2102. VMW_FENCE_WAIT_TIMEOUT);
  2103. }
  2104. }
  2105. int vmw_execbuf_process(struct drm_file *file_priv,
  2106. struct vmw_private *dev_priv,
  2107. void __user *user_commands,
  2108. void *kernel_commands,
  2109. uint32_t command_size,
  2110. uint64_t throttle_us,
  2111. struct drm_vmw_fence_rep __user *user_fence_rep,
  2112. struct vmw_fence_obj **out_fence)
  2113. {
  2114. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  2115. struct vmw_fence_obj *fence = NULL;
  2116. struct vmw_resource *error_resource;
  2117. struct list_head resource_list;
  2118. struct ww_acquire_ctx ticket;
  2119. uint32_t handle;
  2120. void *cmd;
  2121. int ret;
  2122. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  2123. if (unlikely(ret != 0))
  2124. return -ERESTARTSYS;
  2125. if (kernel_commands == NULL) {
  2126. sw_context->kernel = false;
  2127. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  2128. if (unlikely(ret != 0))
  2129. goto out_unlock;
  2130. ret = copy_from_user(sw_context->cmd_bounce,
  2131. user_commands, command_size);
  2132. if (unlikely(ret != 0)) {
  2133. ret = -EFAULT;
  2134. DRM_ERROR("Failed copying commands.\n");
  2135. goto out_unlock;
  2136. }
  2137. kernel_commands = sw_context->cmd_bounce;
  2138. } else
  2139. sw_context->kernel = true;
  2140. sw_context->fp = vmw_fpriv(file_priv);
  2141. sw_context->cur_reloc = 0;
  2142. sw_context->cur_val_buf = 0;
  2143. INIT_LIST_HEAD(&sw_context->resource_list);
  2144. sw_context->cur_query_bo = dev_priv->pinned_bo;
  2145. sw_context->last_query_ctx = NULL;
  2146. sw_context->needs_post_query_barrier = false;
  2147. memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
  2148. INIT_LIST_HEAD(&sw_context->validate_nodes);
  2149. INIT_LIST_HEAD(&sw_context->res_relocations);
  2150. if (!sw_context->res_ht_initialized) {
  2151. ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
  2152. if (unlikely(ret != 0))
  2153. goto out_unlock;
  2154. sw_context->res_ht_initialized = true;
  2155. }
  2156. INIT_LIST_HEAD(&sw_context->staged_cmd_res);
  2157. INIT_LIST_HEAD(&resource_list);
  2158. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  2159. command_size);
  2160. if (unlikely(ret != 0))
  2161. goto out_err_nores;
  2162. ret = vmw_resources_reserve(sw_context);
  2163. if (unlikely(ret != 0))
  2164. goto out_err_nores;
  2165. ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes,
  2166. true, NULL);
  2167. if (unlikely(ret != 0))
  2168. goto out_err;
  2169. ret = vmw_validate_buffers(dev_priv, sw_context);
  2170. if (unlikely(ret != 0))
  2171. goto out_err;
  2172. ret = vmw_resources_validate(sw_context);
  2173. if (unlikely(ret != 0))
  2174. goto out_err;
  2175. if (throttle_us) {
  2176. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  2177. throttle_us);
  2178. if (unlikely(ret != 0))
  2179. goto out_err;
  2180. }
  2181. ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
  2182. if (unlikely(ret != 0)) {
  2183. ret = -ERESTARTSYS;
  2184. goto out_err;
  2185. }
  2186. if (dev_priv->has_mob) {
  2187. ret = vmw_rebind_contexts(sw_context);
  2188. if (unlikely(ret != 0))
  2189. goto out_unlock_binding;
  2190. }
  2191. cmd = vmw_fifo_reserve(dev_priv, command_size);
  2192. if (unlikely(cmd == NULL)) {
  2193. DRM_ERROR("Failed reserving fifo space for commands.\n");
  2194. ret = -ENOMEM;
  2195. goto out_unlock_binding;
  2196. }
  2197. vmw_apply_relocations(sw_context);
  2198. memcpy(cmd, kernel_commands, command_size);
  2199. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  2200. vmw_resource_relocations_free(&sw_context->res_relocations);
  2201. vmw_fifo_commit(dev_priv, command_size);
  2202. vmw_query_bo_switch_commit(dev_priv, sw_context);
  2203. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  2204. &fence,
  2205. (user_fence_rep) ? &handle : NULL);
  2206. /*
  2207. * This error is harmless, because if fence submission fails,
  2208. * vmw_fifo_send_fence will sync. The error will be propagated to
  2209. * user-space in @fence_rep
  2210. */
  2211. if (ret != 0)
  2212. DRM_ERROR("Fence submission error. Syncing.\n");
  2213. vmw_resource_list_unreserve(&sw_context->resource_list, false);
  2214. mutex_unlock(&dev_priv->binding_mutex);
  2215. ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
  2216. (void *) fence);
  2217. if (unlikely(dev_priv->pinned_bo != NULL &&
  2218. !dev_priv->query_cid_valid))
  2219. __vmw_execbuf_release_pinned_bo(dev_priv, fence);
  2220. vmw_clear_validations(sw_context);
  2221. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  2222. user_fence_rep, fence, handle);
  2223. /* Don't unreference when handing fence out */
  2224. if (unlikely(out_fence != NULL)) {
  2225. *out_fence = fence;
  2226. fence = NULL;
  2227. } else if (likely(fence != NULL)) {
  2228. vmw_fence_obj_unreference(&fence);
  2229. }
  2230. list_splice_init(&sw_context->resource_list, &resource_list);
  2231. vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
  2232. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2233. /*
  2234. * Unreference resources outside of the cmdbuf_mutex to
  2235. * avoid deadlocks in resource destruction paths.
  2236. */
  2237. vmw_resource_list_unreference(&resource_list);
  2238. return 0;
  2239. out_unlock_binding:
  2240. mutex_unlock(&dev_priv->binding_mutex);
  2241. out_err:
  2242. ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
  2243. out_err_nores:
  2244. vmw_resource_list_unreserve(&sw_context->resource_list, true);
  2245. vmw_resource_relocations_free(&sw_context->res_relocations);
  2246. vmw_free_relocations(sw_context);
  2247. vmw_clear_validations(sw_context);
  2248. if (unlikely(dev_priv->pinned_bo != NULL &&
  2249. !dev_priv->query_cid_valid))
  2250. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  2251. out_unlock:
  2252. list_splice_init(&sw_context->resource_list, &resource_list);
  2253. error_resource = sw_context->error_resource;
  2254. sw_context->error_resource = NULL;
  2255. vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
  2256. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2257. /*
  2258. * Unreference resources outside of the cmdbuf_mutex to
  2259. * avoid deadlocks in resource destruction paths.
  2260. */
  2261. vmw_resource_list_unreference(&resource_list);
  2262. if (unlikely(error_resource != NULL))
  2263. vmw_resource_unreference(&error_resource);
  2264. return ret;
  2265. }
  2266. /**
  2267. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  2268. *
  2269. * @dev_priv: The device private structure.
  2270. *
  2271. * This function is called to idle the fifo and unpin the query buffer
  2272. * if the normal way to do this hits an error, which should typically be
  2273. * extremely rare.
  2274. */
  2275. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  2276. {
  2277. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  2278. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  2279. vmw_bo_pin(dev_priv->pinned_bo, false);
  2280. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  2281. dev_priv->dummy_query_bo_pinned = false;
  2282. }
  2283. /**
  2284. * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  2285. * query bo.
  2286. *
  2287. * @dev_priv: The device private structure.
  2288. * @fence: If non-NULL should point to a struct vmw_fence_obj issued
  2289. * _after_ a query barrier that flushes all queries touching the current
  2290. * buffer pointed to by @dev_priv->pinned_bo
  2291. *
  2292. * This function should be used to unpin the pinned query bo, or
  2293. * as a query barrier when we need to make sure that all queries have
  2294. * finished before the next fifo command. (For example on hardware
  2295. * context destructions where the hardware may otherwise leak unfinished
  2296. * queries).
  2297. *
  2298. * This function does not return any failure codes, but make attempts
  2299. * to do safe unpinning in case of errors.
  2300. *
  2301. * The function will synchronize on the previous query barrier, and will
  2302. * thus not finish until that barrier has executed.
  2303. *
  2304. * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
  2305. * before calling this function.
  2306. */
  2307. void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  2308. struct vmw_fence_obj *fence)
  2309. {
  2310. int ret = 0;
  2311. struct list_head validate_list;
  2312. struct ttm_validate_buffer pinned_val, query_val;
  2313. struct vmw_fence_obj *lfence = NULL;
  2314. struct ww_acquire_ctx ticket;
  2315. if (dev_priv->pinned_bo == NULL)
  2316. goto out_unlock;
  2317. INIT_LIST_HEAD(&validate_list);
  2318. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  2319. pinned_val.shared = false;
  2320. list_add_tail(&pinned_val.head, &validate_list);
  2321. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  2322. query_val.shared = false;
  2323. list_add_tail(&query_val.head, &validate_list);
  2324. ret = ttm_eu_reserve_buffers(&ticket, &validate_list,
  2325. false, NULL);
  2326. if (unlikely(ret != 0)) {
  2327. vmw_execbuf_unpin_panic(dev_priv);
  2328. goto out_no_reserve;
  2329. }
  2330. if (dev_priv->query_cid_valid) {
  2331. BUG_ON(fence != NULL);
  2332. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  2333. if (unlikely(ret != 0)) {
  2334. vmw_execbuf_unpin_panic(dev_priv);
  2335. goto out_no_emit;
  2336. }
  2337. dev_priv->query_cid_valid = false;
  2338. }
  2339. vmw_bo_pin(dev_priv->pinned_bo, false);
  2340. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  2341. dev_priv->dummy_query_bo_pinned = false;
  2342. if (fence == NULL) {
  2343. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
  2344. NULL);
  2345. fence = lfence;
  2346. }
  2347. ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
  2348. if (lfence != NULL)
  2349. vmw_fence_obj_unreference(&lfence);
  2350. ttm_bo_unref(&query_val.bo);
  2351. ttm_bo_unref(&pinned_val.bo);
  2352. ttm_bo_unref(&dev_priv->pinned_bo);
  2353. out_unlock:
  2354. return;
  2355. out_no_emit:
  2356. ttm_eu_backoff_reservation(&ticket, &validate_list);
  2357. out_no_reserve:
  2358. ttm_bo_unref(&query_val.bo);
  2359. ttm_bo_unref(&pinned_val.bo);
  2360. ttm_bo_unref(&dev_priv->pinned_bo);
  2361. }
  2362. /**
  2363. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  2364. * query bo.
  2365. *
  2366. * @dev_priv: The device private structure.
  2367. *
  2368. * This function should be used to unpin the pinned query bo, or
  2369. * as a query barrier when we need to make sure that all queries have
  2370. * finished before the next fifo command. (For example on hardware
  2371. * context destructions where the hardware may otherwise leak unfinished
  2372. * queries).
  2373. *
  2374. * This function does not return any failure codes, but make attempts
  2375. * to do safe unpinning in case of errors.
  2376. *
  2377. * The function will synchronize on the previous query barrier, and will
  2378. * thus not finish until that barrier has executed.
  2379. */
  2380. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
  2381. {
  2382. mutex_lock(&dev_priv->cmdbuf_mutex);
  2383. if (dev_priv->query_cid_valid)
  2384. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  2385. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2386. }
  2387. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  2388. struct drm_file *file_priv)
  2389. {
  2390. struct vmw_private *dev_priv = vmw_priv(dev);
  2391. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  2392. int ret;
  2393. /*
  2394. * This will allow us to extend the ioctl argument while
  2395. * maintaining backwards compatibility:
  2396. * We take different code paths depending on the value of
  2397. * arg->version.
  2398. */
  2399. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  2400. DRM_ERROR("Incorrect execbuf version.\n");
  2401. DRM_ERROR("You're running outdated experimental "
  2402. "vmwgfx user-space drivers.");
  2403. return -EINVAL;
  2404. }
  2405. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  2406. if (unlikely(ret != 0))
  2407. return ret;
  2408. ret = vmw_execbuf_process(file_priv, dev_priv,
  2409. (void __user *)(unsigned long)arg->commands,
  2410. NULL, arg->command_size, arg->throttle_us,
  2411. (void __user *)(unsigned long)arg->fence_rep,
  2412. NULL);
  2413. ttm_read_unlock(&dev_priv->reservation_sem);
  2414. if (unlikely(ret != 0))
  2415. return ret;
  2416. vmw_kms_cursor_post_execbuf(dev_priv);
  2417. return 0;
  2418. }