vmwgfx_execbuf.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 - 2015 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. #include "vmwgfx_so.h"
  32. #include "vmwgfx_binding.h"
  33. #define VMW_RES_HT_ORDER 12
  34. /**
  35. * struct vmw_resource_relocation - Relocation info for resources
  36. *
  37. * @head: List head for the software context's relocation list.
  38. * @res: Non-ref-counted pointer to the resource.
  39. * @offset: Offset of 4 byte entries into the command buffer where the
  40. * id that needs fixup is located.
  41. */
  42. struct vmw_resource_relocation {
  43. struct list_head head;
  44. const struct vmw_resource *res;
  45. unsigned long offset;
  46. };
  47. /**
  48. * struct vmw_resource_val_node - Validation info for resources
  49. *
  50. * @head: List head for the software context's resource list.
  51. * @hash: Hash entry for quick resouce to val_node lookup.
  52. * @res: Ref-counted pointer to the resource.
  53. * @switch_backup: Boolean whether to switch backup buffer on unreserve.
  54. * @new_backup: Refcounted pointer to the new backup buffer.
  55. * @staged_bindings: If @res is a context, tracks bindings set up during
  56. * the command batch. Otherwise NULL.
  57. * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
  58. * @first_usage: Set to true the first time the resource is referenced in
  59. * the command stream.
  60. * @switching_backup: The command stream provides a new backup buffer for a
  61. * resource.
  62. * @no_buffer_needed: This means @switching_backup is true on first buffer
  63. * reference. So resource reservation does not need to allocate a backup
  64. * buffer for the resource.
  65. */
  66. struct vmw_resource_val_node {
  67. struct list_head head;
  68. struct drm_hash_item hash;
  69. struct vmw_resource *res;
  70. struct vmw_dma_buffer *new_backup;
  71. struct vmw_ctx_binding_state *staged_bindings;
  72. unsigned long new_backup_offset;
  73. u32 first_usage : 1;
  74. u32 switching_backup : 1;
  75. u32 no_buffer_needed : 1;
  76. };
  77. /**
  78. * struct vmw_cmd_entry - Describe a command for the verifier
  79. *
  80. * @user_allow: Whether allowed from the execbuf ioctl.
  81. * @gb_disable: Whether disabled if guest-backed objects are available.
  82. * @gb_enable: Whether enabled iff guest-backed objects are available.
  83. */
  84. struct vmw_cmd_entry {
  85. int (*func) (struct vmw_private *, struct vmw_sw_context *,
  86. SVGA3dCmdHeader *);
  87. bool user_allow;
  88. bool gb_disable;
  89. bool gb_enable;
  90. };
  91. #define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
  92. [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
  93. (_gb_disable), (_gb_enable)}
  94. static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
  95. struct vmw_sw_context *sw_context,
  96. struct vmw_resource *ctx);
  97. static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
  98. struct vmw_sw_context *sw_context,
  99. SVGAMobId *id,
  100. struct vmw_dma_buffer **vmw_bo_p);
  101. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  102. struct vmw_dma_buffer *vbo,
  103. bool validate_as_mob,
  104. uint32_t *p_val_node);
  105. /**
  106. * vmw_resources_unreserve - unreserve resources previously reserved for
  107. * command submission.
  108. *
  109. * @sw_context: pointer to the software context
  110. * @backoff: Whether command submission failed.
  111. */
  112. static void vmw_resources_unreserve(struct vmw_sw_context *sw_context,
  113. bool backoff)
  114. {
  115. struct vmw_resource_val_node *val;
  116. struct list_head *list = &sw_context->resource_list;
  117. if (sw_context->dx_query_mob && !backoff)
  118. vmw_context_bind_dx_query(sw_context->dx_query_ctx,
  119. sw_context->dx_query_mob);
  120. list_for_each_entry(val, list, head) {
  121. struct vmw_resource *res = val->res;
  122. bool switch_backup =
  123. (backoff) ? false : val->switching_backup;
  124. /*
  125. * Transfer staged context bindings to the
  126. * persistent context binding tracker.
  127. */
  128. if (unlikely(val->staged_bindings)) {
  129. if (!backoff) {
  130. vmw_binding_state_commit
  131. (vmw_context_binding_state(val->res),
  132. val->staged_bindings);
  133. }
  134. if (val->staged_bindings != sw_context->staged_bindings)
  135. vmw_binding_state_free(val->staged_bindings);
  136. else
  137. sw_context->staged_bindings_inuse = false;
  138. val->staged_bindings = NULL;
  139. }
  140. vmw_resource_unreserve(res, switch_backup, val->new_backup,
  141. val->new_backup_offset);
  142. vmw_dmabuf_unreference(&val->new_backup);
  143. }
  144. }
  145. /**
  146. * vmw_cmd_ctx_first_setup - Perform the setup needed when a context is
  147. * added to the validate list.
  148. *
  149. * @dev_priv: Pointer to the device private:
  150. * @sw_context: The validation context:
  151. * @node: The validation node holding this context.
  152. */
  153. static int vmw_cmd_ctx_first_setup(struct vmw_private *dev_priv,
  154. struct vmw_sw_context *sw_context,
  155. struct vmw_resource_val_node *node)
  156. {
  157. int ret;
  158. ret = vmw_resource_context_res_add(dev_priv, sw_context, node->res);
  159. if (unlikely(ret != 0))
  160. goto out_err;
  161. if (!sw_context->staged_bindings) {
  162. sw_context->staged_bindings =
  163. vmw_binding_state_alloc(dev_priv);
  164. if (IS_ERR(sw_context->staged_bindings)) {
  165. DRM_ERROR("Failed to allocate context binding "
  166. "information.\n");
  167. ret = PTR_ERR(sw_context->staged_bindings);
  168. sw_context->staged_bindings = NULL;
  169. goto out_err;
  170. }
  171. }
  172. if (sw_context->staged_bindings_inuse) {
  173. node->staged_bindings = vmw_binding_state_alloc(dev_priv);
  174. if (IS_ERR(node->staged_bindings)) {
  175. DRM_ERROR("Failed to allocate context binding "
  176. "information.\n");
  177. ret = PTR_ERR(node->staged_bindings);
  178. node->staged_bindings = NULL;
  179. goto out_err;
  180. }
  181. } else {
  182. node->staged_bindings = sw_context->staged_bindings;
  183. sw_context->staged_bindings_inuse = true;
  184. }
  185. return 0;
  186. out_err:
  187. return ret;
  188. }
  189. /**
  190. * vmw_resource_val_add - Add a resource to the software context's
  191. * resource list if it's not already on it.
  192. *
  193. * @sw_context: Pointer to the software context.
  194. * @res: Pointer to the resource.
  195. * @p_node On successful return points to a valid pointer to a
  196. * struct vmw_resource_val_node, if non-NULL on entry.
  197. */
  198. static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
  199. struct vmw_resource *res,
  200. struct vmw_resource_val_node **p_node)
  201. {
  202. struct vmw_private *dev_priv = res->dev_priv;
  203. struct vmw_resource_val_node *node;
  204. struct drm_hash_item *hash;
  205. int ret;
  206. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
  207. &hash) == 0)) {
  208. node = container_of(hash, struct vmw_resource_val_node, hash);
  209. node->first_usage = false;
  210. if (unlikely(p_node != NULL))
  211. *p_node = node;
  212. return 0;
  213. }
  214. node = kzalloc(sizeof(*node), GFP_KERNEL);
  215. if (unlikely(node == NULL)) {
  216. DRM_ERROR("Failed to allocate a resource validation "
  217. "entry.\n");
  218. return -ENOMEM;
  219. }
  220. node->hash.key = (unsigned long) res;
  221. ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
  222. if (unlikely(ret != 0)) {
  223. DRM_ERROR("Failed to initialize a resource validation "
  224. "entry.\n");
  225. kfree(node);
  226. return ret;
  227. }
  228. node->res = vmw_resource_reference(res);
  229. node->first_usage = true;
  230. if (unlikely(p_node != NULL))
  231. *p_node = node;
  232. if (!dev_priv->has_mob) {
  233. list_add_tail(&node->head, &sw_context->resource_list);
  234. return 0;
  235. }
  236. switch (vmw_res_type(res)) {
  237. case vmw_res_context:
  238. case vmw_res_dx_context:
  239. list_add(&node->head, &sw_context->ctx_resource_list);
  240. ret = vmw_cmd_ctx_first_setup(dev_priv, sw_context, node);
  241. break;
  242. case vmw_res_cotable:
  243. list_add_tail(&node->head, &sw_context->ctx_resource_list);
  244. break;
  245. default:
  246. list_add_tail(&node->head, &sw_context->resource_list);
  247. break;
  248. }
  249. return ret;
  250. }
  251. /**
  252. * vmw_view_res_val_add - Add a view and the surface it's pointing to
  253. * to the validation list
  254. *
  255. * @sw_context: The software context holding the validation list.
  256. * @view: Pointer to the view resource.
  257. *
  258. * Returns 0 if success, negative error code otherwise.
  259. */
  260. static int vmw_view_res_val_add(struct vmw_sw_context *sw_context,
  261. struct vmw_resource *view)
  262. {
  263. int ret;
  264. /*
  265. * First add the resource the view is pointing to, otherwise
  266. * it may be swapped out when the view is validated.
  267. */
  268. ret = vmw_resource_val_add(sw_context, vmw_view_srf(view), NULL);
  269. if (ret)
  270. return ret;
  271. return vmw_resource_val_add(sw_context, view, NULL);
  272. }
  273. /**
  274. * vmw_view_id_val_add - Look up a view and add it and the surface it's
  275. * pointing to to the validation list.
  276. *
  277. * @sw_context: The software context holding the validation list.
  278. * @view_type: The view type to look up.
  279. * @id: view id of the view.
  280. *
  281. * The view is represented by a view id and the DX context it's created on,
  282. * or scheduled for creation on. If there is no DX context set, the function
  283. * will return -EINVAL. Otherwise returns 0 on success and -EINVAL on failure.
  284. */
  285. static int vmw_view_id_val_add(struct vmw_sw_context *sw_context,
  286. enum vmw_view_type view_type, u32 id)
  287. {
  288. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  289. struct vmw_resource *view;
  290. int ret;
  291. if (!ctx_node) {
  292. DRM_ERROR("DX Context not set.\n");
  293. return -EINVAL;
  294. }
  295. view = vmw_view_lookup(sw_context->man, view_type, id);
  296. if (IS_ERR(view))
  297. return PTR_ERR(view);
  298. ret = vmw_view_res_val_add(sw_context, view);
  299. vmw_resource_unreference(&view);
  300. return ret;
  301. }
  302. /**
  303. * vmw_resource_context_res_add - Put resources previously bound to a context on
  304. * the validation list
  305. *
  306. * @dev_priv: Pointer to a device private structure
  307. * @sw_context: Pointer to a software context used for this command submission
  308. * @ctx: Pointer to the context resource
  309. *
  310. * This function puts all resources that were previously bound to @ctx on
  311. * the resource validation list. This is part of the context state reemission
  312. */
  313. static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
  314. struct vmw_sw_context *sw_context,
  315. struct vmw_resource *ctx)
  316. {
  317. struct list_head *binding_list;
  318. struct vmw_ctx_bindinfo *entry;
  319. int ret = 0;
  320. struct vmw_resource *res;
  321. u32 i;
  322. /* Add all cotables to the validation list. */
  323. if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
  324. for (i = 0; i < SVGA_COTABLE_DX10_MAX; ++i) {
  325. res = vmw_context_cotable(ctx, i);
  326. if (IS_ERR(res))
  327. continue;
  328. ret = vmw_resource_val_add(sw_context, res, NULL);
  329. vmw_resource_unreference(&res);
  330. if (unlikely(ret != 0))
  331. return ret;
  332. }
  333. }
  334. /* Add all resources bound to the context to the validation list */
  335. mutex_lock(&dev_priv->binding_mutex);
  336. binding_list = vmw_context_binding_list(ctx);
  337. list_for_each_entry(entry, binding_list, ctx_list) {
  338. /* entry->res is not refcounted */
  339. res = vmw_resource_reference_unless_doomed(entry->res);
  340. if (unlikely(res == NULL))
  341. continue;
  342. if (vmw_res_type(entry->res) == vmw_res_view)
  343. ret = vmw_view_res_val_add(sw_context, entry->res);
  344. else
  345. ret = vmw_resource_val_add(sw_context, entry->res,
  346. NULL);
  347. vmw_resource_unreference(&res);
  348. if (unlikely(ret != 0))
  349. break;
  350. }
  351. if (dev_priv->has_dx && vmw_res_type(ctx) == vmw_res_dx_context) {
  352. struct vmw_dma_buffer *dx_query_mob;
  353. dx_query_mob = vmw_context_get_dx_query_mob(ctx);
  354. if (dx_query_mob)
  355. ret = vmw_bo_to_validate_list(sw_context,
  356. dx_query_mob,
  357. true, NULL);
  358. }
  359. mutex_unlock(&dev_priv->binding_mutex);
  360. return ret;
  361. }
  362. /**
  363. * vmw_resource_relocation_add - Add a relocation to the relocation list
  364. *
  365. * @list: Pointer to head of relocation list.
  366. * @res: The resource.
  367. * @offset: Offset into the command buffer currently being parsed where the
  368. * id that needs fixup is located. Granularity is 4 bytes.
  369. */
  370. static int vmw_resource_relocation_add(struct list_head *list,
  371. const struct vmw_resource *res,
  372. unsigned long offset)
  373. {
  374. struct vmw_resource_relocation *rel;
  375. rel = kmalloc(sizeof(*rel), GFP_KERNEL);
  376. if (unlikely(rel == NULL)) {
  377. DRM_ERROR("Failed to allocate a resource relocation.\n");
  378. return -ENOMEM;
  379. }
  380. rel->res = res;
  381. rel->offset = offset;
  382. list_add_tail(&rel->head, list);
  383. return 0;
  384. }
  385. /**
  386. * vmw_resource_relocations_free - Free all relocations on a list
  387. *
  388. * @list: Pointer to the head of the relocation list.
  389. */
  390. static void vmw_resource_relocations_free(struct list_head *list)
  391. {
  392. struct vmw_resource_relocation *rel, *n;
  393. list_for_each_entry_safe(rel, n, list, head) {
  394. list_del(&rel->head);
  395. kfree(rel);
  396. }
  397. }
  398. /**
  399. * vmw_resource_relocations_apply - Apply all relocations on a list
  400. *
  401. * @cb: Pointer to the start of the command buffer bein patch. This need
  402. * not be the same buffer as the one being parsed when the relocation
  403. * list was built, but the contents must be the same modulo the
  404. * resource ids.
  405. * @list: Pointer to the head of the relocation list.
  406. */
  407. static void vmw_resource_relocations_apply(uint32_t *cb,
  408. struct list_head *list)
  409. {
  410. struct vmw_resource_relocation *rel;
  411. list_for_each_entry(rel, list, head) {
  412. if (likely(rel->res != NULL))
  413. cb[rel->offset] = rel->res->id;
  414. else
  415. cb[rel->offset] = SVGA_3D_CMD_NOP;
  416. }
  417. }
  418. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  419. struct vmw_sw_context *sw_context,
  420. SVGA3dCmdHeader *header)
  421. {
  422. return -EINVAL;
  423. }
  424. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  425. struct vmw_sw_context *sw_context,
  426. SVGA3dCmdHeader *header)
  427. {
  428. return 0;
  429. }
  430. /**
  431. * vmw_bo_to_validate_list - add a bo to a validate list
  432. *
  433. * @sw_context: The software context used for this command submission batch.
  434. * @bo: The buffer object to add.
  435. * @validate_as_mob: Validate this buffer as a MOB.
  436. * @p_val_node: If non-NULL Will be updated with the validate node number
  437. * on return.
  438. *
  439. * Returns -EINVAL if the limit of number of buffer objects per command
  440. * submission is reached.
  441. */
  442. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  443. struct vmw_dma_buffer *vbo,
  444. bool validate_as_mob,
  445. uint32_t *p_val_node)
  446. {
  447. uint32_t val_node;
  448. struct vmw_validate_buffer *vval_buf;
  449. struct ttm_validate_buffer *val_buf;
  450. struct drm_hash_item *hash;
  451. int ret;
  452. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) vbo,
  453. &hash) == 0)) {
  454. vval_buf = container_of(hash, struct vmw_validate_buffer,
  455. hash);
  456. if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
  457. DRM_ERROR("Inconsistent buffer usage.\n");
  458. return -EINVAL;
  459. }
  460. val_buf = &vval_buf->base;
  461. val_node = vval_buf - sw_context->val_bufs;
  462. } else {
  463. val_node = sw_context->cur_val_buf;
  464. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  465. DRM_ERROR("Max number of DMA buffers per submission "
  466. "exceeded.\n");
  467. return -EINVAL;
  468. }
  469. vval_buf = &sw_context->val_bufs[val_node];
  470. vval_buf->hash.key = (unsigned long) vbo;
  471. ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
  472. if (unlikely(ret != 0)) {
  473. DRM_ERROR("Failed to initialize a buffer validation "
  474. "entry.\n");
  475. return ret;
  476. }
  477. ++sw_context->cur_val_buf;
  478. val_buf = &vval_buf->base;
  479. val_buf->bo = ttm_bo_reference(&vbo->base);
  480. val_buf->shared = false;
  481. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  482. vval_buf->validate_as_mob = validate_as_mob;
  483. }
  484. if (p_val_node)
  485. *p_val_node = val_node;
  486. return 0;
  487. }
  488. /**
  489. * vmw_resources_reserve - Reserve all resources on the sw_context's
  490. * resource list.
  491. *
  492. * @sw_context: Pointer to the software context.
  493. *
  494. * Note that since vmware's command submission currently is protected by
  495. * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
  496. * since only a single thread at once will attempt this.
  497. */
  498. static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
  499. {
  500. struct vmw_resource_val_node *val;
  501. int ret = 0;
  502. list_for_each_entry(val, &sw_context->resource_list, head) {
  503. struct vmw_resource *res = val->res;
  504. ret = vmw_resource_reserve(res, true, val->no_buffer_needed);
  505. if (unlikely(ret != 0))
  506. return ret;
  507. if (res->backup) {
  508. struct vmw_dma_buffer *vbo = res->backup;
  509. ret = vmw_bo_to_validate_list
  510. (sw_context, vbo,
  511. vmw_resource_needs_backup(res), NULL);
  512. if (unlikely(ret != 0))
  513. return ret;
  514. }
  515. }
  516. if (sw_context->dx_query_mob) {
  517. struct vmw_dma_buffer *expected_dx_query_mob;
  518. expected_dx_query_mob =
  519. vmw_context_get_dx_query_mob(sw_context->dx_query_ctx);
  520. if (expected_dx_query_mob &&
  521. expected_dx_query_mob != sw_context->dx_query_mob) {
  522. ret = -EINVAL;
  523. }
  524. }
  525. return ret;
  526. }
  527. /**
  528. * vmw_resources_validate - Validate all resources on the sw_context's
  529. * resource list.
  530. *
  531. * @sw_context: Pointer to the software context.
  532. *
  533. * Before this function is called, all resource backup buffers must have
  534. * been validated.
  535. */
  536. static int vmw_resources_validate(struct vmw_sw_context *sw_context)
  537. {
  538. struct vmw_resource_val_node *val;
  539. int ret;
  540. list_for_each_entry(val, &sw_context->resource_list, head) {
  541. struct vmw_resource *res = val->res;
  542. struct vmw_dma_buffer *backup = res->backup;
  543. ret = vmw_resource_validate(res);
  544. if (unlikely(ret != 0)) {
  545. if (ret != -ERESTARTSYS)
  546. DRM_ERROR("Failed to validate resource.\n");
  547. return ret;
  548. }
  549. /* Check if the resource switched backup buffer */
  550. if (backup && res->backup && (backup != res->backup)) {
  551. struct vmw_dma_buffer *vbo = res->backup;
  552. ret = vmw_bo_to_validate_list
  553. (sw_context, vbo,
  554. vmw_resource_needs_backup(res), NULL);
  555. if (ret) {
  556. ttm_bo_unreserve(&vbo->base);
  557. return ret;
  558. }
  559. }
  560. }
  561. return 0;
  562. }
  563. /**
  564. * vmw_cmd_res_reloc_add - Add a resource to a software context's
  565. * relocation- and validation lists.
  566. *
  567. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  568. * @sw_context: Pointer to the software context.
  569. * @id_loc: Pointer to where the id that needs translation is located.
  570. * @res: Valid pointer to a struct vmw_resource.
  571. * @p_val: If non null, a pointer to the struct vmw_resource_validate_node
  572. * used for this resource is returned here.
  573. */
  574. static int vmw_cmd_res_reloc_add(struct vmw_private *dev_priv,
  575. struct vmw_sw_context *sw_context,
  576. uint32_t *id_loc,
  577. struct vmw_resource *res,
  578. struct vmw_resource_val_node **p_val)
  579. {
  580. int ret;
  581. struct vmw_resource_val_node *node;
  582. *p_val = NULL;
  583. ret = vmw_resource_relocation_add(&sw_context->res_relocations,
  584. res,
  585. id_loc - sw_context->buf_start);
  586. if (unlikely(ret != 0))
  587. return ret;
  588. ret = vmw_resource_val_add(sw_context, res, &node);
  589. if (unlikely(ret != 0))
  590. return ret;
  591. if (p_val)
  592. *p_val = node;
  593. return 0;
  594. }
  595. /**
  596. * vmw_cmd_res_check - Check that a resource is present and if so, put it
  597. * on the resource validate list unless it's already there.
  598. *
  599. * @dev_priv: Pointer to a device private structure.
  600. * @sw_context: Pointer to the software context.
  601. * @res_type: Resource type.
  602. * @converter: User-space visisble type specific information.
  603. * @id_loc: Pointer to the location in the command buffer currently being
  604. * parsed from where the user-space resource id handle is located.
  605. * @p_val: Pointer to pointer to resource validalidation node. Populated
  606. * on exit.
  607. */
  608. static int
  609. vmw_cmd_res_check(struct vmw_private *dev_priv,
  610. struct vmw_sw_context *sw_context,
  611. enum vmw_res_type res_type,
  612. const struct vmw_user_resource_conv *converter,
  613. uint32_t *id_loc,
  614. struct vmw_resource_val_node **p_val)
  615. {
  616. struct vmw_res_cache_entry *rcache =
  617. &sw_context->res_cache[res_type];
  618. struct vmw_resource *res;
  619. struct vmw_resource_val_node *node;
  620. int ret;
  621. if (*id_loc == SVGA3D_INVALID_ID) {
  622. if (p_val)
  623. *p_val = NULL;
  624. if (res_type == vmw_res_context) {
  625. DRM_ERROR("Illegal context invalid id.\n");
  626. return -EINVAL;
  627. }
  628. return 0;
  629. }
  630. /*
  631. * Fastpath in case of repeated commands referencing the same
  632. * resource
  633. */
  634. if (likely(rcache->valid && *id_loc == rcache->handle)) {
  635. const struct vmw_resource *res = rcache->res;
  636. rcache->node->first_usage = false;
  637. if (p_val)
  638. *p_val = rcache->node;
  639. return vmw_resource_relocation_add
  640. (&sw_context->res_relocations, res,
  641. id_loc - sw_context->buf_start);
  642. }
  643. ret = vmw_user_resource_lookup_handle(dev_priv,
  644. sw_context->fp->tfile,
  645. *id_loc,
  646. converter,
  647. &res);
  648. if (unlikely(ret != 0)) {
  649. DRM_ERROR("Could not find or use resource 0x%08x.\n",
  650. (unsigned) *id_loc);
  651. dump_stack();
  652. return ret;
  653. }
  654. rcache->valid = true;
  655. rcache->res = res;
  656. rcache->handle = *id_loc;
  657. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context, id_loc,
  658. res, &node);
  659. if (unlikely(ret != 0))
  660. goto out_no_reloc;
  661. rcache->node = node;
  662. if (p_val)
  663. *p_val = node;
  664. vmw_resource_unreference(&res);
  665. return 0;
  666. out_no_reloc:
  667. BUG_ON(sw_context->error_resource != NULL);
  668. sw_context->error_resource = res;
  669. return ret;
  670. }
  671. /**
  672. * vmw_rebind_dx_query - Rebind DX query associated with the context
  673. *
  674. * @ctx_res: context the query belongs to
  675. *
  676. * This function assumes binding_mutex is held.
  677. */
  678. static int vmw_rebind_all_dx_query(struct vmw_resource *ctx_res)
  679. {
  680. struct vmw_private *dev_priv = ctx_res->dev_priv;
  681. struct vmw_dma_buffer *dx_query_mob;
  682. struct {
  683. SVGA3dCmdHeader header;
  684. SVGA3dCmdDXBindAllQuery body;
  685. } *cmd;
  686. dx_query_mob = vmw_context_get_dx_query_mob(ctx_res);
  687. if (!dx_query_mob || dx_query_mob->dx_query_ctx)
  688. return 0;
  689. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), ctx_res->id);
  690. if (cmd == NULL) {
  691. DRM_ERROR("Failed to rebind queries.\n");
  692. return -ENOMEM;
  693. }
  694. cmd->header.id = SVGA_3D_CMD_DX_BIND_ALL_QUERY;
  695. cmd->header.size = sizeof(cmd->body);
  696. cmd->body.cid = ctx_res->id;
  697. cmd->body.mobid = dx_query_mob->base.mem.start;
  698. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  699. vmw_context_bind_dx_query(ctx_res, dx_query_mob);
  700. return 0;
  701. }
  702. /**
  703. * vmw_rebind_contexts - Rebind all resources previously bound to
  704. * referenced contexts.
  705. *
  706. * @sw_context: Pointer to the software context.
  707. *
  708. * Rebind context binding points that have been scrubbed because of eviction.
  709. */
  710. static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
  711. {
  712. struct vmw_resource_val_node *val;
  713. int ret;
  714. list_for_each_entry(val, &sw_context->resource_list, head) {
  715. if (unlikely(!val->staged_bindings))
  716. break;
  717. ret = vmw_binding_rebind_all
  718. (vmw_context_binding_state(val->res));
  719. if (unlikely(ret != 0)) {
  720. if (ret != -ERESTARTSYS)
  721. DRM_ERROR("Failed to rebind context.\n");
  722. return ret;
  723. }
  724. ret = vmw_rebind_all_dx_query(val->res);
  725. if (ret != 0)
  726. return ret;
  727. }
  728. return 0;
  729. }
  730. /**
  731. * vmw_view_bindings_add - Add an array of view bindings to a context
  732. * binding state tracker.
  733. *
  734. * @sw_context: The execbuf state used for this command.
  735. * @view_type: View type for the bindings.
  736. * @binding_type: Binding type for the bindings.
  737. * @shader_slot: The shader slot to user for the bindings.
  738. * @view_ids: Array of view ids to be bound.
  739. * @num_views: Number of view ids in @view_ids.
  740. * @first_slot: The binding slot to be used for the first view id in @view_ids.
  741. */
  742. static int vmw_view_bindings_add(struct vmw_sw_context *sw_context,
  743. enum vmw_view_type view_type,
  744. enum vmw_ctx_binding_type binding_type,
  745. uint32 shader_slot,
  746. uint32 view_ids[], u32 num_views,
  747. u32 first_slot)
  748. {
  749. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  750. struct vmw_cmdbuf_res_manager *man;
  751. u32 i;
  752. int ret;
  753. if (!ctx_node) {
  754. DRM_ERROR("DX Context not set.\n");
  755. return -EINVAL;
  756. }
  757. man = sw_context->man;
  758. for (i = 0; i < num_views; ++i) {
  759. struct vmw_ctx_bindinfo_view binding;
  760. struct vmw_resource *view = NULL;
  761. if (view_ids[i] != SVGA3D_INVALID_ID) {
  762. view = vmw_view_lookup(man, view_type, view_ids[i]);
  763. if (IS_ERR(view)) {
  764. DRM_ERROR("View not found.\n");
  765. return PTR_ERR(view);
  766. }
  767. ret = vmw_view_res_val_add(sw_context, view);
  768. if (ret) {
  769. DRM_ERROR("Could not add view to "
  770. "validation list.\n");
  771. vmw_resource_unreference(&view);
  772. return ret;
  773. }
  774. }
  775. binding.bi.ctx = ctx_node->res;
  776. binding.bi.res = view;
  777. binding.bi.bt = binding_type;
  778. binding.shader_slot = shader_slot;
  779. binding.slot = first_slot + i;
  780. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  781. shader_slot, binding.slot);
  782. if (view)
  783. vmw_resource_unreference(&view);
  784. }
  785. return 0;
  786. }
  787. /**
  788. * vmw_cmd_cid_check - Check a command header for valid context information.
  789. *
  790. * @dev_priv: Pointer to a device private structure.
  791. * @sw_context: Pointer to the software context.
  792. * @header: A command header with an embedded user-space context handle.
  793. *
  794. * Convenience function: Call vmw_cmd_res_check with the user-space context
  795. * handle embedded in @header.
  796. */
  797. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  798. struct vmw_sw_context *sw_context,
  799. SVGA3dCmdHeader *header)
  800. {
  801. struct vmw_cid_cmd {
  802. SVGA3dCmdHeader header;
  803. uint32_t cid;
  804. } *cmd;
  805. cmd = container_of(header, struct vmw_cid_cmd, header);
  806. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  807. user_context_converter, &cmd->cid, NULL);
  808. }
  809. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  810. struct vmw_sw_context *sw_context,
  811. SVGA3dCmdHeader *header)
  812. {
  813. struct vmw_sid_cmd {
  814. SVGA3dCmdHeader header;
  815. SVGA3dCmdSetRenderTarget body;
  816. } *cmd;
  817. struct vmw_resource_val_node *ctx_node;
  818. struct vmw_resource_val_node *res_node;
  819. int ret;
  820. cmd = container_of(header, struct vmw_sid_cmd, header);
  821. if (cmd->body.type >= SVGA3D_RT_MAX) {
  822. DRM_ERROR("Illegal render target type %u.\n",
  823. (unsigned) cmd->body.type);
  824. return -EINVAL;
  825. }
  826. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  827. user_context_converter, &cmd->body.cid,
  828. &ctx_node);
  829. if (unlikely(ret != 0))
  830. return ret;
  831. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  832. user_surface_converter,
  833. &cmd->body.target.sid, &res_node);
  834. if (unlikely(ret != 0))
  835. return ret;
  836. if (dev_priv->has_mob) {
  837. struct vmw_ctx_bindinfo_view binding;
  838. binding.bi.ctx = ctx_node->res;
  839. binding.bi.res = res_node ? res_node->res : NULL;
  840. binding.bi.bt = vmw_ctx_binding_rt;
  841. binding.slot = cmd->body.type;
  842. vmw_binding_add(ctx_node->staged_bindings,
  843. &binding.bi, 0, binding.slot);
  844. }
  845. return 0;
  846. }
  847. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  848. struct vmw_sw_context *sw_context,
  849. SVGA3dCmdHeader *header)
  850. {
  851. struct vmw_sid_cmd {
  852. SVGA3dCmdHeader header;
  853. SVGA3dCmdSurfaceCopy body;
  854. } *cmd;
  855. int ret;
  856. cmd = container_of(header, struct vmw_sid_cmd, header);
  857. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  858. user_surface_converter,
  859. &cmd->body.src.sid, NULL);
  860. if (ret)
  861. return ret;
  862. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  863. user_surface_converter,
  864. &cmd->body.dest.sid, NULL);
  865. }
  866. static int vmw_cmd_buffer_copy_check(struct vmw_private *dev_priv,
  867. struct vmw_sw_context *sw_context,
  868. SVGA3dCmdHeader *header)
  869. {
  870. struct {
  871. SVGA3dCmdHeader header;
  872. SVGA3dCmdDXBufferCopy body;
  873. } *cmd;
  874. int ret;
  875. cmd = container_of(header, typeof(*cmd), header);
  876. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  877. user_surface_converter,
  878. &cmd->body.src, NULL);
  879. if (ret != 0)
  880. return ret;
  881. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  882. user_surface_converter,
  883. &cmd->body.dest, NULL);
  884. }
  885. static int vmw_cmd_pred_copy_check(struct vmw_private *dev_priv,
  886. struct vmw_sw_context *sw_context,
  887. SVGA3dCmdHeader *header)
  888. {
  889. struct {
  890. SVGA3dCmdHeader header;
  891. SVGA3dCmdDXPredCopyRegion body;
  892. } *cmd;
  893. int ret;
  894. cmd = container_of(header, typeof(*cmd), header);
  895. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  896. user_surface_converter,
  897. &cmd->body.srcSid, NULL);
  898. if (ret != 0)
  899. return ret;
  900. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  901. user_surface_converter,
  902. &cmd->body.dstSid, NULL);
  903. }
  904. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  905. struct vmw_sw_context *sw_context,
  906. SVGA3dCmdHeader *header)
  907. {
  908. struct vmw_sid_cmd {
  909. SVGA3dCmdHeader header;
  910. SVGA3dCmdSurfaceStretchBlt body;
  911. } *cmd;
  912. int ret;
  913. cmd = container_of(header, struct vmw_sid_cmd, header);
  914. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  915. user_surface_converter,
  916. &cmd->body.src.sid, NULL);
  917. if (unlikely(ret != 0))
  918. return ret;
  919. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  920. user_surface_converter,
  921. &cmd->body.dest.sid, NULL);
  922. }
  923. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  924. struct vmw_sw_context *sw_context,
  925. SVGA3dCmdHeader *header)
  926. {
  927. struct vmw_sid_cmd {
  928. SVGA3dCmdHeader header;
  929. SVGA3dCmdBlitSurfaceToScreen body;
  930. } *cmd;
  931. cmd = container_of(header, struct vmw_sid_cmd, header);
  932. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  933. user_surface_converter,
  934. &cmd->body.srcImage.sid, NULL);
  935. }
  936. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  937. struct vmw_sw_context *sw_context,
  938. SVGA3dCmdHeader *header)
  939. {
  940. struct vmw_sid_cmd {
  941. SVGA3dCmdHeader header;
  942. SVGA3dCmdPresent body;
  943. } *cmd;
  944. cmd = container_of(header, struct vmw_sid_cmd, header);
  945. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  946. user_surface_converter, &cmd->body.sid,
  947. NULL);
  948. }
  949. /**
  950. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  951. *
  952. * @dev_priv: The device private structure.
  953. * @new_query_bo: The new buffer holding query results.
  954. * @sw_context: The software context used for this command submission.
  955. *
  956. * This function checks whether @new_query_bo is suitable for holding
  957. * query results, and if another buffer currently is pinned for query
  958. * results. If so, the function prepares the state of @sw_context for
  959. * switching pinned buffers after successful submission of the current
  960. * command batch.
  961. */
  962. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  963. struct vmw_dma_buffer *new_query_bo,
  964. struct vmw_sw_context *sw_context)
  965. {
  966. struct vmw_res_cache_entry *ctx_entry =
  967. &sw_context->res_cache[vmw_res_context];
  968. int ret;
  969. BUG_ON(!ctx_entry->valid);
  970. sw_context->last_query_ctx = ctx_entry->res;
  971. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  972. if (unlikely(new_query_bo->base.num_pages > 4)) {
  973. DRM_ERROR("Query buffer too large.\n");
  974. return -EINVAL;
  975. }
  976. if (unlikely(sw_context->cur_query_bo != NULL)) {
  977. sw_context->needs_post_query_barrier = true;
  978. ret = vmw_bo_to_validate_list(sw_context,
  979. sw_context->cur_query_bo,
  980. dev_priv->has_mob, NULL);
  981. if (unlikely(ret != 0))
  982. return ret;
  983. }
  984. sw_context->cur_query_bo = new_query_bo;
  985. ret = vmw_bo_to_validate_list(sw_context,
  986. dev_priv->dummy_query_bo,
  987. dev_priv->has_mob, NULL);
  988. if (unlikely(ret != 0))
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. /**
  994. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  995. *
  996. * @dev_priv: The device private structure.
  997. * @sw_context: The software context used for this command submission batch.
  998. *
  999. * This function will check if we're switching query buffers, and will then,
  1000. * issue a dummy occlusion query wait used as a query barrier. When the fence
  1001. * object following that query wait has signaled, we are sure that all
  1002. * preceding queries have finished, and the old query buffer can be unpinned.
  1003. * However, since both the new query buffer and the old one are fenced with
  1004. * that fence, we can do an asynchronus unpin now, and be sure that the
  1005. * old query buffer won't be moved until the fence has signaled.
  1006. *
  1007. * As mentioned above, both the new - and old query buffers need to be fenced
  1008. * using a sequence emitted *after* calling this function.
  1009. */
  1010. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  1011. struct vmw_sw_context *sw_context)
  1012. {
  1013. /*
  1014. * The validate list should still hold references to all
  1015. * contexts here.
  1016. */
  1017. if (sw_context->needs_post_query_barrier) {
  1018. struct vmw_res_cache_entry *ctx_entry =
  1019. &sw_context->res_cache[vmw_res_context];
  1020. struct vmw_resource *ctx;
  1021. int ret;
  1022. BUG_ON(!ctx_entry->valid);
  1023. ctx = ctx_entry->res;
  1024. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  1025. if (unlikely(ret != 0))
  1026. DRM_ERROR("Out of fifo space for dummy query.\n");
  1027. }
  1028. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  1029. if (dev_priv->pinned_bo) {
  1030. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  1031. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  1032. }
  1033. if (!sw_context->needs_post_query_barrier) {
  1034. vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
  1035. /*
  1036. * We pin also the dummy_query_bo buffer so that we
  1037. * don't need to validate it when emitting
  1038. * dummy queries in context destroy paths.
  1039. */
  1040. if (!dev_priv->dummy_query_bo_pinned) {
  1041. vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
  1042. true);
  1043. dev_priv->dummy_query_bo_pinned = true;
  1044. }
  1045. BUG_ON(sw_context->last_query_ctx == NULL);
  1046. dev_priv->query_cid = sw_context->last_query_ctx->id;
  1047. dev_priv->query_cid_valid = true;
  1048. dev_priv->pinned_bo =
  1049. vmw_dmabuf_reference(sw_context->cur_query_bo);
  1050. }
  1051. }
  1052. }
  1053. /**
  1054. * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
  1055. * handle to a MOB id.
  1056. *
  1057. * @dev_priv: Pointer to a device private structure.
  1058. * @sw_context: The software context used for this command batch validation.
  1059. * @id: Pointer to the user-space handle to be translated.
  1060. * @vmw_bo_p: Points to a location that, on successful return will carry
  1061. * a reference-counted pointer to the DMA buffer identified by the
  1062. * user-space handle in @id.
  1063. *
  1064. * This function saves information needed to translate a user-space buffer
  1065. * handle to a MOB id. The translation does not take place immediately, but
  1066. * during a call to vmw_apply_relocations(). This function builds a relocation
  1067. * list and a list of buffers to validate. The former needs to be freed using
  1068. * either vmw_apply_relocations() or vmw_free_relocations(). The latter
  1069. * needs to be freed using vmw_clear_validations.
  1070. */
  1071. static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
  1072. struct vmw_sw_context *sw_context,
  1073. SVGAMobId *id,
  1074. struct vmw_dma_buffer **vmw_bo_p)
  1075. {
  1076. struct vmw_dma_buffer *vmw_bo = NULL;
  1077. uint32_t handle = *id;
  1078. struct vmw_relocation *reloc;
  1079. int ret;
  1080. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo,
  1081. NULL);
  1082. if (unlikely(ret != 0)) {
  1083. DRM_ERROR("Could not find or use MOB buffer.\n");
  1084. ret = -EINVAL;
  1085. goto out_no_reloc;
  1086. }
  1087. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  1088. DRM_ERROR("Max number relocations per submission"
  1089. " exceeded\n");
  1090. ret = -EINVAL;
  1091. goto out_no_reloc;
  1092. }
  1093. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  1094. reloc->mob_loc = id;
  1095. reloc->location = NULL;
  1096. ret = vmw_bo_to_validate_list(sw_context, vmw_bo, true, &reloc->index);
  1097. if (unlikely(ret != 0))
  1098. goto out_no_reloc;
  1099. *vmw_bo_p = vmw_bo;
  1100. return 0;
  1101. out_no_reloc:
  1102. vmw_dmabuf_unreference(&vmw_bo);
  1103. *vmw_bo_p = NULL;
  1104. return ret;
  1105. }
  1106. /**
  1107. * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
  1108. * handle to a valid SVGAGuestPtr
  1109. *
  1110. * @dev_priv: Pointer to a device private structure.
  1111. * @sw_context: The software context used for this command batch validation.
  1112. * @ptr: Pointer to the user-space handle to be translated.
  1113. * @vmw_bo_p: Points to a location that, on successful return will carry
  1114. * a reference-counted pointer to the DMA buffer identified by the
  1115. * user-space handle in @id.
  1116. *
  1117. * This function saves information needed to translate a user-space buffer
  1118. * handle to a valid SVGAGuestPtr. The translation does not take place
  1119. * immediately, but during a call to vmw_apply_relocations().
  1120. * This function builds a relocation list and a list of buffers to validate.
  1121. * The former needs to be freed using either vmw_apply_relocations() or
  1122. * vmw_free_relocations(). The latter needs to be freed using
  1123. * vmw_clear_validations.
  1124. */
  1125. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  1126. struct vmw_sw_context *sw_context,
  1127. SVGAGuestPtr *ptr,
  1128. struct vmw_dma_buffer **vmw_bo_p)
  1129. {
  1130. struct vmw_dma_buffer *vmw_bo = NULL;
  1131. uint32_t handle = ptr->gmrId;
  1132. struct vmw_relocation *reloc;
  1133. int ret;
  1134. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo,
  1135. NULL);
  1136. if (unlikely(ret != 0)) {
  1137. DRM_ERROR("Could not find or use GMR region.\n");
  1138. ret = -EINVAL;
  1139. goto out_no_reloc;
  1140. }
  1141. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  1142. DRM_ERROR("Max number relocations per submission"
  1143. " exceeded\n");
  1144. ret = -EINVAL;
  1145. goto out_no_reloc;
  1146. }
  1147. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  1148. reloc->location = ptr;
  1149. ret = vmw_bo_to_validate_list(sw_context, vmw_bo, false, &reloc->index);
  1150. if (unlikely(ret != 0))
  1151. goto out_no_reloc;
  1152. *vmw_bo_p = vmw_bo;
  1153. return 0;
  1154. out_no_reloc:
  1155. vmw_dmabuf_unreference(&vmw_bo);
  1156. *vmw_bo_p = NULL;
  1157. return ret;
  1158. }
  1159. /**
  1160. * vmw_cmd_dx_define_query - validate a SVGA_3D_CMD_DX_DEFINE_QUERY command.
  1161. *
  1162. * @dev_priv: Pointer to a device private struct.
  1163. * @sw_context: The software context used for this command submission.
  1164. * @header: Pointer to the command header in the command stream.
  1165. *
  1166. * This function adds the new query into the query COTABLE
  1167. */
  1168. static int vmw_cmd_dx_define_query(struct vmw_private *dev_priv,
  1169. struct vmw_sw_context *sw_context,
  1170. SVGA3dCmdHeader *header)
  1171. {
  1172. struct vmw_dx_define_query_cmd {
  1173. SVGA3dCmdHeader header;
  1174. SVGA3dCmdDXDefineQuery q;
  1175. } *cmd;
  1176. int ret;
  1177. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  1178. struct vmw_resource *cotable_res;
  1179. if (ctx_node == NULL) {
  1180. DRM_ERROR("DX Context not set for query.\n");
  1181. return -EINVAL;
  1182. }
  1183. cmd = container_of(header, struct vmw_dx_define_query_cmd, header);
  1184. if (cmd->q.type < SVGA3D_QUERYTYPE_MIN ||
  1185. cmd->q.type >= SVGA3D_QUERYTYPE_MAX)
  1186. return -EINVAL;
  1187. cotable_res = vmw_context_cotable(ctx_node->res, SVGA_COTABLE_DXQUERY);
  1188. ret = vmw_cotable_notify(cotable_res, cmd->q.queryId);
  1189. vmw_resource_unreference(&cotable_res);
  1190. return ret;
  1191. }
  1192. /**
  1193. * vmw_cmd_dx_bind_query - validate a SVGA_3D_CMD_DX_BIND_QUERY command.
  1194. *
  1195. * @dev_priv: Pointer to a device private struct.
  1196. * @sw_context: The software context used for this command submission.
  1197. * @header: Pointer to the command header in the command stream.
  1198. *
  1199. * The query bind operation will eventually associate the query ID
  1200. * with its backing MOB. In this function, we take the user mode
  1201. * MOB ID and use vmw_translate_mob_ptr() to translate it to its
  1202. * kernel mode equivalent.
  1203. */
  1204. static int vmw_cmd_dx_bind_query(struct vmw_private *dev_priv,
  1205. struct vmw_sw_context *sw_context,
  1206. SVGA3dCmdHeader *header)
  1207. {
  1208. struct vmw_dx_bind_query_cmd {
  1209. SVGA3dCmdHeader header;
  1210. SVGA3dCmdDXBindQuery q;
  1211. } *cmd;
  1212. struct vmw_dma_buffer *vmw_bo;
  1213. int ret;
  1214. cmd = container_of(header, struct vmw_dx_bind_query_cmd, header);
  1215. /*
  1216. * Look up the buffer pointed to by q.mobid, put it on the relocation
  1217. * list so its kernel mode MOB ID can be filled in later
  1218. */
  1219. ret = vmw_translate_mob_ptr(dev_priv, sw_context, &cmd->q.mobid,
  1220. &vmw_bo);
  1221. if (ret != 0)
  1222. return ret;
  1223. sw_context->dx_query_mob = vmw_bo;
  1224. sw_context->dx_query_ctx = sw_context->dx_ctx_node->res;
  1225. vmw_dmabuf_unreference(&vmw_bo);
  1226. return ret;
  1227. }
  1228. /**
  1229. * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
  1230. *
  1231. * @dev_priv: Pointer to a device private struct.
  1232. * @sw_context: The software context used for this command submission.
  1233. * @header: Pointer to the command header in the command stream.
  1234. */
  1235. static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
  1236. struct vmw_sw_context *sw_context,
  1237. SVGA3dCmdHeader *header)
  1238. {
  1239. struct vmw_begin_gb_query_cmd {
  1240. SVGA3dCmdHeader header;
  1241. SVGA3dCmdBeginGBQuery q;
  1242. } *cmd;
  1243. cmd = container_of(header, struct vmw_begin_gb_query_cmd,
  1244. header);
  1245. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1246. user_context_converter, &cmd->q.cid,
  1247. NULL);
  1248. }
  1249. /**
  1250. * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
  1251. *
  1252. * @dev_priv: Pointer to a device private struct.
  1253. * @sw_context: The software context used for this command submission.
  1254. * @header: Pointer to the command header in the command stream.
  1255. */
  1256. static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
  1257. struct vmw_sw_context *sw_context,
  1258. SVGA3dCmdHeader *header)
  1259. {
  1260. struct vmw_begin_query_cmd {
  1261. SVGA3dCmdHeader header;
  1262. SVGA3dCmdBeginQuery q;
  1263. } *cmd;
  1264. cmd = container_of(header, struct vmw_begin_query_cmd,
  1265. header);
  1266. if (unlikely(dev_priv->has_mob)) {
  1267. struct {
  1268. SVGA3dCmdHeader header;
  1269. SVGA3dCmdBeginGBQuery q;
  1270. } gb_cmd;
  1271. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  1272. gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
  1273. gb_cmd.header.size = cmd->header.size;
  1274. gb_cmd.q.cid = cmd->q.cid;
  1275. gb_cmd.q.type = cmd->q.type;
  1276. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  1277. return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
  1278. }
  1279. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1280. user_context_converter, &cmd->q.cid,
  1281. NULL);
  1282. }
  1283. /**
  1284. * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
  1285. *
  1286. * @dev_priv: Pointer to a device private struct.
  1287. * @sw_context: The software context used for this command submission.
  1288. * @header: Pointer to the command header in the command stream.
  1289. */
  1290. static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
  1291. struct vmw_sw_context *sw_context,
  1292. SVGA3dCmdHeader *header)
  1293. {
  1294. struct vmw_dma_buffer *vmw_bo;
  1295. struct vmw_query_cmd {
  1296. SVGA3dCmdHeader header;
  1297. SVGA3dCmdEndGBQuery q;
  1298. } *cmd;
  1299. int ret;
  1300. cmd = container_of(header, struct vmw_query_cmd, header);
  1301. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1302. if (unlikely(ret != 0))
  1303. return ret;
  1304. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  1305. &cmd->q.mobid,
  1306. &vmw_bo);
  1307. if (unlikely(ret != 0))
  1308. return ret;
  1309. ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
  1310. vmw_dmabuf_unreference(&vmw_bo);
  1311. return ret;
  1312. }
  1313. /**
  1314. * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
  1315. *
  1316. * @dev_priv: Pointer to a device private struct.
  1317. * @sw_context: The software context used for this command submission.
  1318. * @header: Pointer to the command header in the command stream.
  1319. */
  1320. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  1321. struct vmw_sw_context *sw_context,
  1322. SVGA3dCmdHeader *header)
  1323. {
  1324. struct vmw_dma_buffer *vmw_bo;
  1325. struct vmw_query_cmd {
  1326. SVGA3dCmdHeader header;
  1327. SVGA3dCmdEndQuery q;
  1328. } *cmd;
  1329. int ret;
  1330. cmd = container_of(header, struct vmw_query_cmd, header);
  1331. if (dev_priv->has_mob) {
  1332. struct {
  1333. SVGA3dCmdHeader header;
  1334. SVGA3dCmdEndGBQuery q;
  1335. } gb_cmd;
  1336. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  1337. gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
  1338. gb_cmd.header.size = cmd->header.size;
  1339. gb_cmd.q.cid = cmd->q.cid;
  1340. gb_cmd.q.type = cmd->q.type;
  1341. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  1342. gb_cmd.q.offset = cmd->q.guestResult.offset;
  1343. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  1344. return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
  1345. }
  1346. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1347. if (unlikely(ret != 0))
  1348. return ret;
  1349. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1350. &cmd->q.guestResult,
  1351. &vmw_bo);
  1352. if (unlikely(ret != 0))
  1353. return ret;
  1354. ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
  1355. vmw_dmabuf_unreference(&vmw_bo);
  1356. return ret;
  1357. }
  1358. /**
  1359. * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
  1360. *
  1361. * @dev_priv: Pointer to a device private struct.
  1362. * @sw_context: The software context used for this command submission.
  1363. * @header: Pointer to the command header in the command stream.
  1364. */
  1365. static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
  1366. struct vmw_sw_context *sw_context,
  1367. SVGA3dCmdHeader *header)
  1368. {
  1369. struct vmw_dma_buffer *vmw_bo;
  1370. struct vmw_query_cmd {
  1371. SVGA3dCmdHeader header;
  1372. SVGA3dCmdWaitForGBQuery q;
  1373. } *cmd;
  1374. int ret;
  1375. cmd = container_of(header, struct vmw_query_cmd, header);
  1376. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1377. if (unlikely(ret != 0))
  1378. return ret;
  1379. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  1380. &cmd->q.mobid,
  1381. &vmw_bo);
  1382. if (unlikely(ret != 0))
  1383. return ret;
  1384. vmw_dmabuf_unreference(&vmw_bo);
  1385. return 0;
  1386. }
  1387. /**
  1388. * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
  1389. *
  1390. * @dev_priv: Pointer to a device private struct.
  1391. * @sw_context: The software context used for this command submission.
  1392. * @header: Pointer to the command header in the command stream.
  1393. */
  1394. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  1395. struct vmw_sw_context *sw_context,
  1396. SVGA3dCmdHeader *header)
  1397. {
  1398. struct vmw_dma_buffer *vmw_bo;
  1399. struct vmw_query_cmd {
  1400. SVGA3dCmdHeader header;
  1401. SVGA3dCmdWaitForQuery q;
  1402. } *cmd;
  1403. int ret;
  1404. cmd = container_of(header, struct vmw_query_cmd, header);
  1405. if (dev_priv->has_mob) {
  1406. struct {
  1407. SVGA3dCmdHeader header;
  1408. SVGA3dCmdWaitForGBQuery q;
  1409. } gb_cmd;
  1410. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  1411. gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  1412. gb_cmd.header.size = cmd->header.size;
  1413. gb_cmd.q.cid = cmd->q.cid;
  1414. gb_cmd.q.type = cmd->q.type;
  1415. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  1416. gb_cmd.q.offset = cmd->q.guestResult.offset;
  1417. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  1418. return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
  1419. }
  1420. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1421. if (unlikely(ret != 0))
  1422. return ret;
  1423. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1424. &cmd->q.guestResult,
  1425. &vmw_bo);
  1426. if (unlikely(ret != 0))
  1427. return ret;
  1428. vmw_dmabuf_unreference(&vmw_bo);
  1429. return 0;
  1430. }
  1431. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  1432. struct vmw_sw_context *sw_context,
  1433. SVGA3dCmdHeader *header)
  1434. {
  1435. struct vmw_dma_buffer *vmw_bo = NULL;
  1436. struct vmw_surface *srf = NULL;
  1437. struct vmw_dma_cmd {
  1438. SVGA3dCmdHeader header;
  1439. SVGA3dCmdSurfaceDMA dma;
  1440. } *cmd;
  1441. int ret;
  1442. SVGA3dCmdSurfaceDMASuffix *suffix;
  1443. uint32_t bo_size;
  1444. cmd = container_of(header, struct vmw_dma_cmd, header);
  1445. suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->dma +
  1446. header->size - sizeof(*suffix));
  1447. /* Make sure device and verifier stays in sync. */
  1448. if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
  1449. DRM_ERROR("Invalid DMA suffix size.\n");
  1450. return -EINVAL;
  1451. }
  1452. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1453. &cmd->dma.guest.ptr,
  1454. &vmw_bo);
  1455. if (unlikely(ret != 0))
  1456. return ret;
  1457. /* Make sure DMA doesn't cross BO boundaries. */
  1458. bo_size = vmw_bo->base.num_pages * PAGE_SIZE;
  1459. if (unlikely(cmd->dma.guest.ptr.offset > bo_size)) {
  1460. DRM_ERROR("Invalid DMA offset.\n");
  1461. return -EINVAL;
  1462. }
  1463. bo_size -= cmd->dma.guest.ptr.offset;
  1464. if (unlikely(suffix->maximumOffset > bo_size))
  1465. suffix->maximumOffset = bo_size;
  1466. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1467. user_surface_converter, &cmd->dma.host.sid,
  1468. NULL);
  1469. if (unlikely(ret != 0)) {
  1470. if (unlikely(ret != -ERESTARTSYS))
  1471. DRM_ERROR("could not find surface for DMA.\n");
  1472. goto out_no_surface;
  1473. }
  1474. srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
  1475. vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base,
  1476. header);
  1477. out_no_surface:
  1478. vmw_dmabuf_unreference(&vmw_bo);
  1479. return ret;
  1480. }
  1481. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  1482. struct vmw_sw_context *sw_context,
  1483. SVGA3dCmdHeader *header)
  1484. {
  1485. struct vmw_draw_cmd {
  1486. SVGA3dCmdHeader header;
  1487. SVGA3dCmdDrawPrimitives body;
  1488. } *cmd;
  1489. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  1490. (unsigned long)header + sizeof(*cmd));
  1491. SVGA3dPrimitiveRange *range;
  1492. uint32_t i;
  1493. uint32_t maxnum;
  1494. int ret;
  1495. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1496. if (unlikely(ret != 0))
  1497. return ret;
  1498. cmd = container_of(header, struct vmw_draw_cmd, header);
  1499. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  1500. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  1501. DRM_ERROR("Illegal number of vertex declarations.\n");
  1502. return -EINVAL;
  1503. }
  1504. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  1505. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1506. user_surface_converter,
  1507. &decl->array.surfaceId, NULL);
  1508. if (unlikely(ret != 0))
  1509. return ret;
  1510. }
  1511. maxnum = (header->size - sizeof(cmd->body) -
  1512. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  1513. if (unlikely(cmd->body.numRanges > maxnum)) {
  1514. DRM_ERROR("Illegal number of index ranges.\n");
  1515. return -EINVAL;
  1516. }
  1517. range = (SVGA3dPrimitiveRange *) decl;
  1518. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  1519. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1520. user_surface_converter,
  1521. &range->indexArray.surfaceId, NULL);
  1522. if (unlikely(ret != 0))
  1523. return ret;
  1524. }
  1525. return 0;
  1526. }
  1527. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  1528. struct vmw_sw_context *sw_context,
  1529. SVGA3dCmdHeader *header)
  1530. {
  1531. struct vmw_tex_state_cmd {
  1532. SVGA3dCmdHeader header;
  1533. SVGA3dCmdSetTextureState state;
  1534. } *cmd;
  1535. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  1536. ((unsigned long) header + header->size + sizeof(header));
  1537. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  1538. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  1539. struct vmw_resource_val_node *ctx_node;
  1540. struct vmw_resource_val_node *res_node;
  1541. int ret;
  1542. cmd = container_of(header, struct vmw_tex_state_cmd,
  1543. header);
  1544. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1545. user_context_converter, &cmd->state.cid,
  1546. &ctx_node);
  1547. if (unlikely(ret != 0))
  1548. return ret;
  1549. for (; cur_state < last_state; ++cur_state) {
  1550. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  1551. continue;
  1552. if (cur_state->stage >= SVGA3D_NUM_TEXTURE_UNITS) {
  1553. DRM_ERROR("Illegal texture/sampler unit %u.\n",
  1554. (unsigned) cur_state->stage);
  1555. return -EINVAL;
  1556. }
  1557. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1558. user_surface_converter,
  1559. &cur_state->value, &res_node);
  1560. if (unlikely(ret != 0))
  1561. return ret;
  1562. if (dev_priv->has_mob) {
  1563. struct vmw_ctx_bindinfo_tex binding;
  1564. binding.bi.ctx = ctx_node->res;
  1565. binding.bi.res = res_node ? res_node->res : NULL;
  1566. binding.bi.bt = vmw_ctx_binding_tex;
  1567. binding.texture_stage = cur_state->stage;
  1568. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  1569. 0, binding.texture_stage);
  1570. }
  1571. }
  1572. return 0;
  1573. }
  1574. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  1575. struct vmw_sw_context *sw_context,
  1576. void *buf)
  1577. {
  1578. struct vmw_dma_buffer *vmw_bo;
  1579. int ret;
  1580. struct {
  1581. uint32_t header;
  1582. SVGAFifoCmdDefineGMRFB body;
  1583. } *cmd = buf;
  1584. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1585. &cmd->body.ptr,
  1586. &vmw_bo);
  1587. if (unlikely(ret != 0))
  1588. return ret;
  1589. vmw_dmabuf_unreference(&vmw_bo);
  1590. return ret;
  1591. }
  1592. /**
  1593. * vmw_cmd_res_switch_backup - Utility function to handle backup buffer
  1594. * switching
  1595. *
  1596. * @dev_priv: Pointer to a device private struct.
  1597. * @sw_context: The software context being used for this batch.
  1598. * @val_node: The validation node representing the resource.
  1599. * @buf_id: Pointer to the user-space backup buffer handle in the command
  1600. * stream.
  1601. * @backup_offset: Offset of backup into MOB.
  1602. *
  1603. * This function prepares for registering a switch of backup buffers
  1604. * in the resource metadata just prior to unreserving. It's basically a wrapper
  1605. * around vmw_cmd_res_switch_backup with a different interface.
  1606. */
  1607. static int vmw_cmd_res_switch_backup(struct vmw_private *dev_priv,
  1608. struct vmw_sw_context *sw_context,
  1609. struct vmw_resource_val_node *val_node,
  1610. uint32_t *buf_id,
  1611. unsigned long backup_offset)
  1612. {
  1613. struct vmw_dma_buffer *dma_buf;
  1614. int ret;
  1615. ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &dma_buf);
  1616. if (ret)
  1617. return ret;
  1618. val_node->switching_backup = true;
  1619. if (val_node->first_usage)
  1620. val_node->no_buffer_needed = true;
  1621. vmw_dmabuf_unreference(&val_node->new_backup);
  1622. val_node->new_backup = dma_buf;
  1623. val_node->new_backup_offset = backup_offset;
  1624. return 0;
  1625. }
  1626. /**
  1627. * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
  1628. *
  1629. * @dev_priv: Pointer to a device private struct.
  1630. * @sw_context: The software context being used for this batch.
  1631. * @res_type: The resource type.
  1632. * @converter: Information about user-space binding for this resource type.
  1633. * @res_id: Pointer to the user-space resource handle in the command stream.
  1634. * @buf_id: Pointer to the user-space backup buffer handle in the command
  1635. * stream.
  1636. * @backup_offset: Offset of backup into MOB.
  1637. *
  1638. * This function prepares for registering a switch of backup buffers
  1639. * in the resource metadata just prior to unreserving. It's basically a wrapper
  1640. * around vmw_cmd_res_switch_backup with a different interface.
  1641. */
  1642. static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
  1643. struct vmw_sw_context *sw_context,
  1644. enum vmw_res_type res_type,
  1645. const struct vmw_user_resource_conv
  1646. *converter,
  1647. uint32_t *res_id,
  1648. uint32_t *buf_id,
  1649. unsigned long backup_offset)
  1650. {
  1651. struct vmw_resource_val_node *val_node;
  1652. int ret;
  1653. ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
  1654. converter, res_id, &val_node);
  1655. if (ret)
  1656. return ret;
  1657. return vmw_cmd_res_switch_backup(dev_priv, sw_context, val_node,
  1658. buf_id, backup_offset);
  1659. }
  1660. /**
  1661. * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
  1662. * command
  1663. *
  1664. * @dev_priv: Pointer to a device private struct.
  1665. * @sw_context: The software context being used for this batch.
  1666. * @header: Pointer to the command header in the command stream.
  1667. */
  1668. static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
  1669. struct vmw_sw_context *sw_context,
  1670. SVGA3dCmdHeader *header)
  1671. {
  1672. struct vmw_bind_gb_surface_cmd {
  1673. SVGA3dCmdHeader header;
  1674. SVGA3dCmdBindGBSurface body;
  1675. } *cmd;
  1676. cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
  1677. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
  1678. user_surface_converter,
  1679. &cmd->body.sid, &cmd->body.mobid,
  1680. 0);
  1681. }
  1682. /**
  1683. * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
  1684. * command
  1685. *
  1686. * @dev_priv: Pointer to a device private struct.
  1687. * @sw_context: The software context being used for this batch.
  1688. * @header: Pointer to the command header in the command stream.
  1689. */
  1690. static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
  1691. struct vmw_sw_context *sw_context,
  1692. SVGA3dCmdHeader *header)
  1693. {
  1694. struct vmw_gb_surface_cmd {
  1695. SVGA3dCmdHeader header;
  1696. SVGA3dCmdUpdateGBImage body;
  1697. } *cmd;
  1698. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1699. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1700. user_surface_converter,
  1701. &cmd->body.image.sid, NULL);
  1702. }
  1703. /**
  1704. * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
  1705. * command
  1706. *
  1707. * @dev_priv: Pointer to a device private struct.
  1708. * @sw_context: The software context being used for this batch.
  1709. * @header: Pointer to the command header in the command stream.
  1710. */
  1711. static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
  1712. struct vmw_sw_context *sw_context,
  1713. SVGA3dCmdHeader *header)
  1714. {
  1715. struct vmw_gb_surface_cmd {
  1716. SVGA3dCmdHeader header;
  1717. SVGA3dCmdUpdateGBSurface body;
  1718. } *cmd;
  1719. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1720. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1721. user_surface_converter,
  1722. &cmd->body.sid, NULL);
  1723. }
  1724. /**
  1725. * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
  1726. * command
  1727. *
  1728. * @dev_priv: Pointer to a device private struct.
  1729. * @sw_context: The software context being used for this batch.
  1730. * @header: Pointer to the command header in the command stream.
  1731. */
  1732. static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
  1733. struct vmw_sw_context *sw_context,
  1734. SVGA3dCmdHeader *header)
  1735. {
  1736. struct vmw_gb_surface_cmd {
  1737. SVGA3dCmdHeader header;
  1738. SVGA3dCmdReadbackGBImage body;
  1739. } *cmd;
  1740. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1741. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1742. user_surface_converter,
  1743. &cmd->body.image.sid, NULL);
  1744. }
  1745. /**
  1746. * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
  1747. * command
  1748. *
  1749. * @dev_priv: Pointer to a device private struct.
  1750. * @sw_context: The software context being used for this batch.
  1751. * @header: Pointer to the command header in the command stream.
  1752. */
  1753. static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
  1754. struct vmw_sw_context *sw_context,
  1755. SVGA3dCmdHeader *header)
  1756. {
  1757. struct vmw_gb_surface_cmd {
  1758. SVGA3dCmdHeader header;
  1759. SVGA3dCmdReadbackGBSurface body;
  1760. } *cmd;
  1761. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1762. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1763. user_surface_converter,
  1764. &cmd->body.sid, NULL);
  1765. }
  1766. /**
  1767. * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
  1768. * command
  1769. *
  1770. * @dev_priv: Pointer to a device private struct.
  1771. * @sw_context: The software context being used for this batch.
  1772. * @header: Pointer to the command header in the command stream.
  1773. */
  1774. static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
  1775. struct vmw_sw_context *sw_context,
  1776. SVGA3dCmdHeader *header)
  1777. {
  1778. struct vmw_gb_surface_cmd {
  1779. SVGA3dCmdHeader header;
  1780. SVGA3dCmdInvalidateGBImage body;
  1781. } *cmd;
  1782. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1783. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1784. user_surface_converter,
  1785. &cmd->body.image.sid, NULL);
  1786. }
  1787. /**
  1788. * vmw_cmd_invalidate_gb_surface - Validate an
  1789. * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
  1790. *
  1791. * @dev_priv: Pointer to a device private struct.
  1792. * @sw_context: The software context being used for this batch.
  1793. * @header: Pointer to the command header in the command stream.
  1794. */
  1795. static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
  1796. struct vmw_sw_context *sw_context,
  1797. SVGA3dCmdHeader *header)
  1798. {
  1799. struct vmw_gb_surface_cmd {
  1800. SVGA3dCmdHeader header;
  1801. SVGA3dCmdInvalidateGBSurface body;
  1802. } *cmd;
  1803. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1804. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1805. user_surface_converter,
  1806. &cmd->body.sid, NULL);
  1807. }
  1808. /**
  1809. * vmw_cmd_shader_define - Validate an SVGA_3D_CMD_SHADER_DEFINE
  1810. * command
  1811. *
  1812. * @dev_priv: Pointer to a device private struct.
  1813. * @sw_context: The software context being used for this batch.
  1814. * @header: Pointer to the command header in the command stream.
  1815. */
  1816. static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
  1817. struct vmw_sw_context *sw_context,
  1818. SVGA3dCmdHeader *header)
  1819. {
  1820. struct vmw_shader_define_cmd {
  1821. SVGA3dCmdHeader header;
  1822. SVGA3dCmdDefineShader body;
  1823. } *cmd;
  1824. int ret;
  1825. size_t size;
  1826. struct vmw_resource_val_node *val;
  1827. cmd = container_of(header, struct vmw_shader_define_cmd,
  1828. header);
  1829. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1830. user_context_converter, &cmd->body.cid,
  1831. &val);
  1832. if (unlikely(ret != 0))
  1833. return ret;
  1834. if (unlikely(!dev_priv->has_mob))
  1835. return 0;
  1836. size = cmd->header.size - sizeof(cmd->body);
  1837. ret = vmw_compat_shader_add(dev_priv,
  1838. vmw_context_res_man(val->res),
  1839. cmd->body.shid, cmd + 1,
  1840. cmd->body.type, size,
  1841. &sw_context->staged_cmd_res);
  1842. if (unlikely(ret != 0))
  1843. return ret;
  1844. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1845. NULL, &cmd->header.id -
  1846. sw_context->buf_start);
  1847. return 0;
  1848. }
  1849. /**
  1850. * vmw_cmd_shader_destroy - Validate an SVGA_3D_CMD_SHADER_DESTROY
  1851. * command
  1852. *
  1853. * @dev_priv: Pointer to a device private struct.
  1854. * @sw_context: The software context being used for this batch.
  1855. * @header: Pointer to the command header in the command stream.
  1856. */
  1857. static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
  1858. struct vmw_sw_context *sw_context,
  1859. SVGA3dCmdHeader *header)
  1860. {
  1861. struct vmw_shader_destroy_cmd {
  1862. SVGA3dCmdHeader header;
  1863. SVGA3dCmdDestroyShader body;
  1864. } *cmd;
  1865. int ret;
  1866. struct vmw_resource_val_node *val;
  1867. cmd = container_of(header, struct vmw_shader_destroy_cmd,
  1868. header);
  1869. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1870. user_context_converter, &cmd->body.cid,
  1871. &val);
  1872. if (unlikely(ret != 0))
  1873. return ret;
  1874. if (unlikely(!dev_priv->has_mob))
  1875. return 0;
  1876. ret = vmw_shader_remove(vmw_context_res_man(val->res),
  1877. cmd->body.shid,
  1878. cmd->body.type,
  1879. &sw_context->staged_cmd_res);
  1880. if (unlikely(ret != 0))
  1881. return ret;
  1882. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1883. NULL, &cmd->header.id -
  1884. sw_context->buf_start);
  1885. return 0;
  1886. }
  1887. /**
  1888. * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
  1889. * command
  1890. *
  1891. * @dev_priv: Pointer to a device private struct.
  1892. * @sw_context: The software context being used for this batch.
  1893. * @header: Pointer to the command header in the command stream.
  1894. */
  1895. static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
  1896. struct vmw_sw_context *sw_context,
  1897. SVGA3dCmdHeader *header)
  1898. {
  1899. struct vmw_set_shader_cmd {
  1900. SVGA3dCmdHeader header;
  1901. SVGA3dCmdSetShader body;
  1902. } *cmd;
  1903. struct vmw_resource_val_node *ctx_node, *res_node = NULL;
  1904. struct vmw_ctx_bindinfo_shader binding;
  1905. struct vmw_resource *res = NULL;
  1906. int ret;
  1907. cmd = container_of(header, struct vmw_set_shader_cmd,
  1908. header);
  1909. if (cmd->body.type >= SVGA3D_SHADERTYPE_PREDX_MAX) {
  1910. DRM_ERROR("Illegal shader type %u.\n",
  1911. (unsigned) cmd->body.type);
  1912. return -EINVAL;
  1913. }
  1914. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1915. user_context_converter, &cmd->body.cid,
  1916. &ctx_node);
  1917. if (unlikely(ret != 0))
  1918. return ret;
  1919. if (!dev_priv->has_mob)
  1920. return 0;
  1921. if (cmd->body.shid != SVGA3D_INVALID_ID) {
  1922. res = vmw_shader_lookup(vmw_context_res_man(ctx_node->res),
  1923. cmd->body.shid,
  1924. cmd->body.type);
  1925. if (!IS_ERR(res)) {
  1926. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context,
  1927. &cmd->body.shid, res,
  1928. &res_node);
  1929. vmw_resource_unreference(&res);
  1930. if (unlikely(ret != 0))
  1931. return ret;
  1932. }
  1933. }
  1934. if (!res_node) {
  1935. ret = vmw_cmd_res_check(dev_priv, sw_context,
  1936. vmw_res_shader,
  1937. user_shader_converter,
  1938. &cmd->body.shid, &res_node);
  1939. if (unlikely(ret != 0))
  1940. return ret;
  1941. }
  1942. binding.bi.ctx = ctx_node->res;
  1943. binding.bi.res = res_node ? res_node->res : NULL;
  1944. binding.bi.bt = vmw_ctx_binding_shader;
  1945. binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
  1946. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  1947. binding.shader_slot, 0);
  1948. return 0;
  1949. }
  1950. /**
  1951. * vmw_cmd_set_shader_const - Validate an SVGA_3D_CMD_SET_SHADER_CONST
  1952. * command
  1953. *
  1954. * @dev_priv: Pointer to a device private struct.
  1955. * @sw_context: The software context being used for this batch.
  1956. * @header: Pointer to the command header in the command stream.
  1957. */
  1958. static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
  1959. struct vmw_sw_context *sw_context,
  1960. SVGA3dCmdHeader *header)
  1961. {
  1962. struct vmw_set_shader_const_cmd {
  1963. SVGA3dCmdHeader header;
  1964. SVGA3dCmdSetShaderConst body;
  1965. } *cmd;
  1966. int ret;
  1967. cmd = container_of(header, struct vmw_set_shader_const_cmd,
  1968. header);
  1969. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1970. user_context_converter, &cmd->body.cid,
  1971. NULL);
  1972. if (unlikely(ret != 0))
  1973. return ret;
  1974. if (dev_priv->has_mob)
  1975. header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
  1976. return 0;
  1977. }
  1978. /**
  1979. * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
  1980. * command
  1981. *
  1982. * @dev_priv: Pointer to a device private struct.
  1983. * @sw_context: The software context being used for this batch.
  1984. * @header: Pointer to the command header in the command stream.
  1985. */
  1986. static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
  1987. struct vmw_sw_context *sw_context,
  1988. SVGA3dCmdHeader *header)
  1989. {
  1990. struct vmw_bind_gb_shader_cmd {
  1991. SVGA3dCmdHeader header;
  1992. SVGA3dCmdBindGBShader body;
  1993. } *cmd;
  1994. cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
  1995. header);
  1996. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
  1997. user_shader_converter,
  1998. &cmd->body.shid, &cmd->body.mobid,
  1999. cmd->body.offsetInBytes);
  2000. }
  2001. /**
  2002. * vmw_cmd_dx_set_single_constant_buffer - Validate an
  2003. * SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER command.
  2004. *
  2005. * @dev_priv: Pointer to a device private struct.
  2006. * @sw_context: The software context being used for this batch.
  2007. * @header: Pointer to the command header in the command stream.
  2008. */
  2009. static int
  2010. vmw_cmd_dx_set_single_constant_buffer(struct vmw_private *dev_priv,
  2011. struct vmw_sw_context *sw_context,
  2012. SVGA3dCmdHeader *header)
  2013. {
  2014. struct {
  2015. SVGA3dCmdHeader header;
  2016. SVGA3dCmdDXSetSingleConstantBuffer body;
  2017. } *cmd;
  2018. struct vmw_resource_val_node *res_node = NULL;
  2019. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2020. struct vmw_ctx_bindinfo_cb binding;
  2021. int ret;
  2022. if (unlikely(ctx_node == NULL)) {
  2023. DRM_ERROR("DX Context not set.\n");
  2024. return -EINVAL;
  2025. }
  2026. cmd = container_of(header, typeof(*cmd), header);
  2027. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2028. user_surface_converter,
  2029. &cmd->body.sid, &res_node);
  2030. if (unlikely(ret != 0))
  2031. return ret;
  2032. binding.bi.ctx = ctx_node->res;
  2033. binding.bi.res = res_node ? res_node->res : NULL;
  2034. binding.bi.bt = vmw_ctx_binding_cb;
  2035. binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
  2036. binding.offset = cmd->body.offsetInBytes;
  2037. binding.size = cmd->body.sizeInBytes;
  2038. binding.slot = cmd->body.slot;
  2039. if (binding.shader_slot >= SVGA3D_NUM_SHADERTYPE_DX10 ||
  2040. binding.slot >= SVGA3D_DX_MAX_CONSTBUFFERS) {
  2041. DRM_ERROR("Illegal const buffer shader %u slot %u.\n",
  2042. (unsigned) cmd->body.type,
  2043. (unsigned) binding.slot);
  2044. return -EINVAL;
  2045. }
  2046. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  2047. binding.shader_slot, binding.slot);
  2048. return 0;
  2049. }
  2050. /**
  2051. * vmw_cmd_dx_set_shader_res - Validate an
  2052. * SVGA_3D_CMD_DX_SET_SHADER_RESOURCES command
  2053. *
  2054. * @dev_priv: Pointer to a device private struct.
  2055. * @sw_context: The software context being used for this batch.
  2056. * @header: Pointer to the command header in the command stream.
  2057. */
  2058. static int vmw_cmd_dx_set_shader_res(struct vmw_private *dev_priv,
  2059. struct vmw_sw_context *sw_context,
  2060. SVGA3dCmdHeader *header)
  2061. {
  2062. struct {
  2063. SVGA3dCmdHeader header;
  2064. SVGA3dCmdDXSetShaderResources body;
  2065. } *cmd = container_of(header, typeof(*cmd), header);
  2066. u32 num_sr_view = (cmd->header.size - sizeof(cmd->body)) /
  2067. sizeof(SVGA3dShaderResourceViewId);
  2068. if ((u64) cmd->body.startView + (u64) num_sr_view >
  2069. (u64) SVGA3D_DX_MAX_SRVIEWS ||
  2070. cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
  2071. DRM_ERROR("Invalid shader binding.\n");
  2072. return -EINVAL;
  2073. }
  2074. return vmw_view_bindings_add(sw_context, vmw_view_sr,
  2075. vmw_ctx_binding_sr,
  2076. cmd->body.type - SVGA3D_SHADERTYPE_MIN,
  2077. (void *) &cmd[1], num_sr_view,
  2078. cmd->body.startView);
  2079. }
  2080. /**
  2081. * vmw_cmd_dx_set_shader - Validate an SVGA_3D_CMD_DX_SET_SHADER
  2082. * command
  2083. *
  2084. * @dev_priv: Pointer to a device private struct.
  2085. * @sw_context: The software context being used for this batch.
  2086. * @header: Pointer to the command header in the command stream.
  2087. */
  2088. static int vmw_cmd_dx_set_shader(struct vmw_private *dev_priv,
  2089. struct vmw_sw_context *sw_context,
  2090. SVGA3dCmdHeader *header)
  2091. {
  2092. struct {
  2093. SVGA3dCmdHeader header;
  2094. SVGA3dCmdDXSetShader body;
  2095. } *cmd;
  2096. struct vmw_resource *res = NULL;
  2097. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2098. struct vmw_ctx_bindinfo_shader binding;
  2099. int ret = 0;
  2100. if (unlikely(ctx_node == NULL)) {
  2101. DRM_ERROR("DX Context not set.\n");
  2102. return -EINVAL;
  2103. }
  2104. cmd = container_of(header, typeof(*cmd), header);
  2105. if (cmd->body.type >= SVGA3D_SHADERTYPE_DX10_MAX) {
  2106. DRM_ERROR("Illegal shader type %u.\n",
  2107. (unsigned) cmd->body.type);
  2108. return -EINVAL;
  2109. }
  2110. if (cmd->body.shaderId != SVGA3D_INVALID_ID) {
  2111. res = vmw_shader_lookup(sw_context->man, cmd->body.shaderId, 0);
  2112. if (IS_ERR(res)) {
  2113. DRM_ERROR("Could not find shader for binding.\n");
  2114. return PTR_ERR(res);
  2115. }
  2116. ret = vmw_resource_val_add(sw_context, res, NULL);
  2117. if (ret)
  2118. goto out_unref;
  2119. }
  2120. binding.bi.ctx = ctx_node->res;
  2121. binding.bi.res = res;
  2122. binding.bi.bt = vmw_ctx_binding_dx_shader;
  2123. binding.shader_slot = cmd->body.type - SVGA3D_SHADERTYPE_MIN;
  2124. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  2125. binding.shader_slot, 0);
  2126. out_unref:
  2127. if (res)
  2128. vmw_resource_unreference(&res);
  2129. return ret;
  2130. }
  2131. /**
  2132. * vmw_cmd_dx_set_vertex_buffers - Validates an
  2133. * SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS command
  2134. *
  2135. * @dev_priv: Pointer to a device private struct.
  2136. * @sw_context: The software context being used for this batch.
  2137. * @header: Pointer to the command header in the command stream.
  2138. */
  2139. static int vmw_cmd_dx_set_vertex_buffers(struct vmw_private *dev_priv,
  2140. struct vmw_sw_context *sw_context,
  2141. SVGA3dCmdHeader *header)
  2142. {
  2143. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2144. struct vmw_ctx_bindinfo_vb binding;
  2145. struct vmw_resource_val_node *res_node;
  2146. struct {
  2147. SVGA3dCmdHeader header;
  2148. SVGA3dCmdDXSetVertexBuffers body;
  2149. SVGA3dVertexBuffer buf[];
  2150. } *cmd;
  2151. int i, ret, num;
  2152. if (unlikely(ctx_node == NULL)) {
  2153. DRM_ERROR("DX Context not set.\n");
  2154. return -EINVAL;
  2155. }
  2156. cmd = container_of(header, typeof(*cmd), header);
  2157. num = (cmd->header.size - sizeof(cmd->body)) /
  2158. sizeof(SVGA3dVertexBuffer);
  2159. if ((u64)num + (u64)cmd->body.startBuffer >
  2160. (u64)SVGA3D_DX_MAX_VERTEXBUFFERS) {
  2161. DRM_ERROR("Invalid number of vertex buffers.\n");
  2162. return -EINVAL;
  2163. }
  2164. for (i = 0; i < num; i++) {
  2165. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2166. user_surface_converter,
  2167. &cmd->buf[i].sid, &res_node);
  2168. if (unlikely(ret != 0))
  2169. return ret;
  2170. binding.bi.ctx = ctx_node->res;
  2171. binding.bi.bt = vmw_ctx_binding_vb;
  2172. binding.bi.res = ((res_node) ? res_node->res : NULL);
  2173. binding.offset = cmd->buf[i].offset;
  2174. binding.stride = cmd->buf[i].stride;
  2175. binding.slot = i + cmd->body.startBuffer;
  2176. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  2177. 0, binding.slot);
  2178. }
  2179. return 0;
  2180. }
  2181. /**
  2182. * vmw_cmd_dx_ia_set_vertex_buffers - Validate an
  2183. * SVGA_3D_CMD_DX_IA_SET_VERTEX_BUFFERS command.
  2184. *
  2185. * @dev_priv: Pointer to a device private struct.
  2186. * @sw_context: The software context being used for this batch.
  2187. * @header: Pointer to the command header in the command stream.
  2188. */
  2189. static int vmw_cmd_dx_set_index_buffer(struct vmw_private *dev_priv,
  2190. struct vmw_sw_context *sw_context,
  2191. SVGA3dCmdHeader *header)
  2192. {
  2193. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2194. struct vmw_ctx_bindinfo_ib binding;
  2195. struct vmw_resource_val_node *res_node;
  2196. struct {
  2197. SVGA3dCmdHeader header;
  2198. SVGA3dCmdDXSetIndexBuffer body;
  2199. } *cmd;
  2200. int ret;
  2201. if (unlikely(ctx_node == NULL)) {
  2202. DRM_ERROR("DX Context not set.\n");
  2203. return -EINVAL;
  2204. }
  2205. cmd = container_of(header, typeof(*cmd), header);
  2206. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2207. user_surface_converter,
  2208. &cmd->body.sid, &res_node);
  2209. if (unlikely(ret != 0))
  2210. return ret;
  2211. binding.bi.ctx = ctx_node->res;
  2212. binding.bi.res = ((res_node) ? res_node->res : NULL);
  2213. binding.bi.bt = vmw_ctx_binding_ib;
  2214. binding.offset = cmd->body.offset;
  2215. binding.format = cmd->body.format;
  2216. vmw_binding_add(ctx_node->staged_bindings, &binding.bi, 0, 0);
  2217. return 0;
  2218. }
  2219. /**
  2220. * vmw_cmd_dx_set_rendertarget - Validate an
  2221. * SVGA_3D_CMD_DX_SET_RENDERTARGETS command
  2222. *
  2223. * @dev_priv: Pointer to a device private struct.
  2224. * @sw_context: The software context being used for this batch.
  2225. * @header: Pointer to the command header in the command stream.
  2226. */
  2227. static int vmw_cmd_dx_set_rendertargets(struct vmw_private *dev_priv,
  2228. struct vmw_sw_context *sw_context,
  2229. SVGA3dCmdHeader *header)
  2230. {
  2231. struct {
  2232. SVGA3dCmdHeader header;
  2233. SVGA3dCmdDXSetRenderTargets body;
  2234. } *cmd = container_of(header, typeof(*cmd), header);
  2235. int ret;
  2236. u32 num_rt_view = (cmd->header.size - sizeof(cmd->body)) /
  2237. sizeof(SVGA3dRenderTargetViewId);
  2238. if (num_rt_view > SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS) {
  2239. DRM_ERROR("Invalid DX Rendertarget binding.\n");
  2240. return -EINVAL;
  2241. }
  2242. ret = vmw_view_bindings_add(sw_context, vmw_view_ds,
  2243. vmw_ctx_binding_ds, 0,
  2244. &cmd->body.depthStencilViewId, 1, 0);
  2245. if (ret)
  2246. return ret;
  2247. return vmw_view_bindings_add(sw_context, vmw_view_rt,
  2248. vmw_ctx_binding_dx_rt, 0,
  2249. (void *)&cmd[1], num_rt_view, 0);
  2250. }
  2251. /**
  2252. * vmw_cmd_dx_clear_rendertarget_view - Validate an
  2253. * SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW command
  2254. *
  2255. * @dev_priv: Pointer to a device private struct.
  2256. * @sw_context: The software context being used for this batch.
  2257. * @header: Pointer to the command header in the command stream.
  2258. */
  2259. static int vmw_cmd_dx_clear_rendertarget_view(struct vmw_private *dev_priv,
  2260. struct vmw_sw_context *sw_context,
  2261. SVGA3dCmdHeader *header)
  2262. {
  2263. struct {
  2264. SVGA3dCmdHeader header;
  2265. SVGA3dCmdDXClearRenderTargetView body;
  2266. } *cmd = container_of(header, typeof(*cmd), header);
  2267. return vmw_view_id_val_add(sw_context, vmw_view_rt,
  2268. cmd->body.renderTargetViewId);
  2269. }
  2270. /**
  2271. * vmw_cmd_dx_clear_rendertarget_view - Validate an
  2272. * SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW command
  2273. *
  2274. * @dev_priv: Pointer to a device private struct.
  2275. * @sw_context: The software context being used for this batch.
  2276. * @header: Pointer to the command header in the command stream.
  2277. */
  2278. static int vmw_cmd_dx_clear_depthstencil_view(struct vmw_private *dev_priv,
  2279. struct vmw_sw_context *sw_context,
  2280. SVGA3dCmdHeader *header)
  2281. {
  2282. struct {
  2283. SVGA3dCmdHeader header;
  2284. SVGA3dCmdDXClearDepthStencilView body;
  2285. } *cmd = container_of(header, typeof(*cmd), header);
  2286. return vmw_view_id_val_add(sw_context, vmw_view_ds,
  2287. cmd->body.depthStencilViewId);
  2288. }
  2289. static int vmw_cmd_dx_view_define(struct vmw_private *dev_priv,
  2290. struct vmw_sw_context *sw_context,
  2291. SVGA3dCmdHeader *header)
  2292. {
  2293. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2294. struct vmw_resource_val_node *srf_node;
  2295. struct vmw_resource *res;
  2296. enum vmw_view_type view_type;
  2297. int ret;
  2298. /*
  2299. * This is based on the fact that all affected define commands have
  2300. * the same initial command body layout.
  2301. */
  2302. struct {
  2303. SVGA3dCmdHeader header;
  2304. uint32 defined_id;
  2305. uint32 sid;
  2306. } *cmd;
  2307. if (unlikely(ctx_node == NULL)) {
  2308. DRM_ERROR("DX Context not set.\n");
  2309. return -EINVAL;
  2310. }
  2311. view_type = vmw_view_cmd_to_type(header->id);
  2312. if (view_type == vmw_view_max)
  2313. return -EINVAL;
  2314. cmd = container_of(header, typeof(*cmd), header);
  2315. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2316. user_surface_converter,
  2317. &cmd->sid, &srf_node);
  2318. if (unlikely(ret != 0))
  2319. return ret;
  2320. res = vmw_context_cotable(ctx_node->res, vmw_view_cotables[view_type]);
  2321. ret = vmw_cotable_notify(res, cmd->defined_id);
  2322. vmw_resource_unreference(&res);
  2323. if (unlikely(ret != 0))
  2324. return ret;
  2325. return vmw_view_add(sw_context->man,
  2326. ctx_node->res,
  2327. srf_node->res,
  2328. view_type,
  2329. cmd->defined_id,
  2330. header,
  2331. header->size + sizeof(*header),
  2332. &sw_context->staged_cmd_res);
  2333. }
  2334. /**
  2335. * vmw_cmd_dx_set_so_targets - Validate an
  2336. * SVGA_3D_CMD_DX_SET_SOTARGETS command.
  2337. *
  2338. * @dev_priv: Pointer to a device private struct.
  2339. * @sw_context: The software context being used for this batch.
  2340. * @header: Pointer to the command header in the command stream.
  2341. */
  2342. static int vmw_cmd_dx_set_so_targets(struct vmw_private *dev_priv,
  2343. struct vmw_sw_context *sw_context,
  2344. SVGA3dCmdHeader *header)
  2345. {
  2346. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2347. struct vmw_ctx_bindinfo_so binding;
  2348. struct vmw_resource_val_node *res_node;
  2349. struct {
  2350. SVGA3dCmdHeader header;
  2351. SVGA3dCmdDXSetSOTargets body;
  2352. SVGA3dSoTarget targets[];
  2353. } *cmd;
  2354. int i, ret, num;
  2355. if (unlikely(ctx_node == NULL)) {
  2356. DRM_ERROR("DX Context not set.\n");
  2357. return -EINVAL;
  2358. }
  2359. cmd = container_of(header, typeof(*cmd), header);
  2360. num = (cmd->header.size - sizeof(cmd->body)) /
  2361. sizeof(SVGA3dSoTarget);
  2362. if (num > SVGA3D_DX_MAX_SOTARGETS) {
  2363. DRM_ERROR("Invalid DX SO binding.\n");
  2364. return -EINVAL;
  2365. }
  2366. for (i = 0; i < num; i++) {
  2367. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2368. user_surface_converter,
  2369. &cmd->targets[i].sid, &res_node);
  2370. if (unlikely(ret != 0))
  2371. return ret;
  2372. binding.bi.ctx = ctx_node->res;
  2373. binding.bi.res = ((res_node) ? res_node->res : NULL);
  2374. binding.bi.bt = vmw_ctx_binding_so,
  2375. binding.offset = cmd->targets[i].offset;
  2376. binding.size = cmd->targets[i].sizeInBytes;
  2377. binding.slot = i;
  2378. vmw_binding_add(ctx_node->staged_bindings, &binding.bi,
  2379. 0, binding.slot);
  2380. }
  2381. return 0;
  2382. }
  2383. static int vmw_cmd_dx_so_define(struct vmw_private *dev_priv,
  2384. struct vmw_sw_context *sw_context,
  2385. SVGA3dCmdHeader *header)
  2386. {
  2387. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2388. struct vmw_resource *res;
  2389. /*
  2390. * This is based on the fact that all affected define commands have
  2391. * the same initial command body layout.
  2392. */
  2393. struct {
  2394. SVGA3dCmdHeader header;
  2395. uint32 defined_id;
  2396. } *cmd;
  2397. enum vmw_so_type so_type;
  2398. int ret;
  2399. if (unlikely(ctx_node == NULL)) {
  2400. DRM_ERROR("DX Context not set.\n");
  2401. return -EINVAL;
  2402. }
  2403. so_type = vmw_so_cmd_to_type(header->id);
  2404. res = vmw_context_cotable(ctx_node->res, vmw_so_cotables[so_type]);
  2405. cmd = container_of(header, typeof(*cmd), header);
  2406. ret = vmw_cotable_notify(res, cmd->defined_id);
  2407. vmw_resource_unreference(&res);
  2408. return ret;
  2409. }
  2410. /**
  2411. * vmw_cmd_dx_check_subresource - Validate an
  2412. * SVGA_3D_CMD_DX_[X]_SUBRESOURCE command
  2413. *
  2414. * @dev_priv: Pointer to a device private struct.
  2415. * @sw_context: The software context being used for this batch.
  2416. * @header: Pointer to the command header in the command stream.
  2417. */
  2418. static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv,
  2419. struct vmw_sw_context *sw_context,
  2420. SVGA3dCmdHeader *header)
  2421. {
  2422. struct {
  2423. SVGA3dCmdHeader header;
  2424. union {
  2425. SVGA3dCmdDXReadbackSubResource r_body;
  2426. SVGA3dCmdDXInvalidateSubResource i_body;
  2427. SVGA3dCmdDXUpdateSubResource u_body;
  2428. SVGA3dSurfaceId sid;
  2429. };
  2430. } *cmd;
  2431. BUILD_BUG_ON(offsetof(typeof(*cmd), r_body.sid) !=
  2432. offsetof(typeof(*cmd), sid));
  2433. BUILD_BUG_ON(offsetof(typeof(*cmd), i_body.sid) !=
  2434. offsetof(typeof(*cmd), sid));
  2435. BUILD_BUG_ON(offsetof(typeof(*cmd), u_body.sid) !=
  2436. offsetof(typeof(*cmd), sid));
  2437. cmd = container_of(header, typeof(*cmd), header);
  2438. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  2439. user_surface_converter,
  2440. &cmd->sid, NULL);
  2441. }
  2442. static int vmw_cmd_dx_cid_check(struct vmw_private *dev_priv,
  2443. struct vmw_sw_context *sw_context,
  2444. SVGA3dCmdHeader *header)
  2445. {
  2446. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2447. if (unlikely(ctx_node == NULL)) {
  2448. DRM_ERROR("DX Context not set.\n");
  2449. return -EINVAL;
  2450. }
  2451. return 0;
  2452. }
  2453. /**
  2454. * vmw_cmd_dx_view_remove - validate a view remove command and
  2455. * schedule the view resource for removal.
  2456. *
  2457. * @dev_priv: Pointer to a device private struct.
  2458. * @sw_context: The software context being used for this batch.
  2459. * @header: Pointer to the command header in the command stream.
  2460. *
  2461. * Check that the view exists, and if it was not created using this
  2462. * command batch, make sure it's validated (present in the device) so that
  2463. * the remove command will not confuse the device.
  2464. */
  2465. static int vmw_cmd_dx_view_remove(struct vmw_private *dev_priv,
  2466. struct vmw_sw_context *sw_context,
  2467. SVGA3dCmdHeader *header)
  2468. {
  2469. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2470. struct {
  2471. SVGA3dCmdHeader header;
  2472. union vmw_view_destroy body;
  2473. } *cmd = container_of(header, typeof(*cmd), header);
  2474. enum vmw_view_type view_type = vmw_view_cmd_to_type(header->id);
  2475. struct vmw_resource *view;
  2476. int ret;
  2477. if (!ctx_node) {
  2478. DRM_ERROR("DX Context not set.\n");
  2479. return -EINVAL;
  2480. }
  2481. ret = vmw_view_remove(sw_context->man,
  2482. cmd->body.view_id, view_type,
  2483. &sw_context->staged_cmd_res,
  2484. &view);
  2485. if (ret || !view)
  2486. return ret;
  2487. /*
  2488. * Add view to the validate list iff it was not created using this
  2489. * command batch.
  2490. */
  2491. return vmw_view_res_val_add(sw_context, view);
  2492. }
  2493. /**
  2494. * vmw_cmd_dx_define_shader - Validate an SVGA_3D_CMD_DX_DEFINE_SHADER
  2495. * command
  2496. *
  2497. * @dev_priv: Pointer to a device private struct.
  2498. * @sw_context: The software context being used for this batch.
  2499. * @header: Pointer to the command header in the command stream.
  2500. */
  2501. static int vmw_cmd_dx_define_shader(struct vmw_private *dev_priv,
  2502. struct vmw_sw_context *sw_context,
  2503. SVGA3dCmdHeader *header)
  2504. {
  2505. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2506. struct vmw_resource *res;
  2507. struct {
  2508. SVGA3dCmdHeader header;
  2509. SVGA3dCmdDXDefineShader body;
  2510. } *cmd = container_of(header, typeof(*cmd), header);
  2511. int ret;
  2512. if (!ctx_node) {
  2513. DRM_ERROR("DX Context not set.\n");
  2514. return -EINVAL;
  2515. }
  2516. res = vmw_context_cotable(ctx_node->res, SVGA_COTABLE_DXSHADER);
  2517. ret = vmw_cotable_notify(res, cmd->body.shaderId);
  2518. vmw_resource_unreference(&res);
  2519. if (ret)
  2520. return ret;
  2521. return vmw_dx_shader_add(sw_context->man, ctx_node->res,
  2522. cmd->body.shaderId, cmd->body.type,
  2523. &sw_context->staged_cmd_res);
  2524. }
  2525. /**
  2526. * vmw_cmd_dx_destroy_shader - Validate an SVGA_3D_CMD_DX_DESTROY_SHADER
  2527. * command
  2528. *
  2529. * @dev_priv: Pointer to a device private struct.
  2530. * @sw_context: The software context being used for this batch.
  2531. * @header: Pointer to the command header in the command stream.
  2532. */
  2533. static int vmw_cmd_dx_destroy_shader(struct vmw_private *dev_priv,
  2534. struct vmw_sw_context *sw_context,
  2535. SVGA3dCmdHeader *header)
  2536. {
  2537. struct vmw_resource_val_node *ctx_node = sw_context->dx_ctx_node;
  2538. struct {
  2539. SVGA3dCmdHeader header;
  2540. SVGA3dCmdDXDestroyShader body;
  2541. } *cmd = container_of(header, typeof(*cmd), header);
  2542. int ret;
  2543. if (!ctx_node) {
  2544. DRM_ERROR("DX Context not set.\n");
  2545. return -EINVAL;
  2546. }
  2547. ret = vmw_shader_remove(sw_context->man, cmd->body.shaderId, 0,
  2548. &sw_context->staged_cmd_res);
  2549. if (ret)
  2550. DRM_ERROR("Could not find shader to remove.\n");
  2551. return ret;
  2552. }
  2553. /**
  2554. * vmw_cmd_dx_bind_shader - Validate an SVGA_3D_CMD_DX_BIND_SHADER
  2555. * command
  2556. *
  2557. * @dev_priv: Pointer to a device private struct.
  2558. * @sw_context: The software context being used for this batch.
  2559. * @header: Pointer to the command header in the command stream.
  2560. */
  2561. static int vmw_cmd_dx_bind_shader(struct vmw_private *dev_priv,
  2562. struct vmw_sw_context *sw_context,
  2563. SVGA3dCmdHeader *header)
  2564. {
  2565. struct vmw_resource_val_node *ctx_node;
  2566. struct vmw_resource_val_node *res_node;
  2567. struct vmw_resource *res;
  2568. struct {
  2569. SVGA3dCmdHeader header;
  2570. SVGA3dCmdDXBindShader body;
  2571. } *cmd = container_of(header, typeof(*cmd), header);
  2572. int ret;
  2573. if (cmd->body.cid != SVGA3D_INVALID_ID) {
  2574. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  2575. user_context_converter,
  2576. &cmd->body.cid, &ctx_node);
  2577. if (ret)
  2578. return ret;
  2579. } else {
  2580. ctx_node = sw_context->dx_ctx_node;
  2581. if (!ctx_node) {
  2582. DRM_ERROR("DX Context not set.\n");
  2583. return -EINVAL;
  2584. }
  2585. }
  2586. res = vmw_shader_lookup(vmw_context_res_man(ctx_node->res),
  2587. cmd->body.shid, 0);
  2588. if (IS_ERR(res)) {
  2589. DRM_ERROR("Could not find shader to bind.\n");
  2590. return PTR_ERR(res);
  2591. }
  2592. ret = vmw_resource_val_add(sw_context, res, &res_node);
  2593. if (ret) {
  2594. DRM_ERROR("Error creating resource validation node.\n");
  2595. goto out_unref;
  2596. }
  2597. ret = vmw_cmd_res_switch_backup(dev_priv, sw_context, res_node,
  2598. &cmd->body.mobid,
  2599. cmd->body.offsetInBytes);
  2600. out_unref:
  2601. vmw_resource_unreference(&res);
  2602. return ret;
  2603. }
  2604. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  2605. struct vmw_sw_context *sw_context,
  2606. void *buf, uint32_t *size)
  2607. {
  2608. uint32_t size_remaining = *size;
  2609. uint32_t cmd_id;
  2610. cmd_id = ((uint32_t *)buf)[0];
  2611. switch (cmd_id) {
  2612. case SVGA_CMD_UPDATE:
  2613. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  2614. break;
  2615. case SVGA_CMD_DEFINE_GMRFB:
  2616. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  2617. break;
  2618. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  2619. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  2620. break;
  2621. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  2622. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  2623. break;
  2624. default:
  2625. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  2626. return -EINVAL;
  2627. }
  2628. if (*size > size_remaining) {
  2629. DRM_ERROR("Invalid SVGA command (size mismatch):"
  2630. " %u.\n", cmd_id);
  2631. return -EINVAL;
  2632. }
  2633. if (unlikely(!sw_context->kernel)) {
  2634. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  2635. return -EPERM;
  2636. }
  2637. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  2638. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  2639. return 0;
  2640. }
  2641. static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
  2642. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
  2643. false, false, false),
  2644. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
  2645. false, false, false),
  2646. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
  2647. true, false, false),
  2648. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
  2649. true, false, false),
  2650. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
  2651. true, false, false),
  2652. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
  2653. false, false, false),
  2654. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
  2655. false, false, false),
  2656. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
  2657. true, false, false),
  2658. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
  2659. true, false, false),
  2660. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
  2661. true, false, false),
  2662. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  2663. &vmw_cmd_set_render_target_check, true, false, false),
  2664. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
  2665. true, false, false),
  2666. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
  2667. true, false, false),
  2668. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
  2669. true, false, false),
  2670. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
  2671. true, false, false),
  2672. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
  2673. true, false, false),
  2674. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
  2675. true, false, false),
  2676. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
  2677. true, false, false),
  2678. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
  2679. false, false, false),
  2680. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
  2681. true, false, false),
  2682. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
  2683. true, false, false),
  2684. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
  2685. true, false, false),
  2686. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
  2687. true, false, false),
  2688. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
  2689. true, false, false),
  2690. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
  2691. true, false, false),
  2692. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
  2693. true, false, false),
  2694. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
  2695. true, false, false),
  2696. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
  2697. true, false, false),
  2698. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
  2699. true, false, false),
  2700. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  2701. &vmw_cmd_blt_surf_screen_check, false, false, false),
  2702. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
  2703. false, false, false),
  2704. VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
  2705. false, false, false),
  2706. VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
  2707. false, false, false),
  2708. VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
  2709. false, false, false),
  2710. VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
  2711. false, false, false),
  2712. VMW_CMD_DEF(SVGA_3D_CMD_SET_UNITY_SURFACE_COOKIE, &vmw_cmd_invalid,
  2713. false, false, false),
  2714. VMW_CMD_DEF(SVGA_3D_CMD_OPEN_CONTEXT_SURFACE, &vmw_cmd_invalid,
  2715. false, false, false),
  2716. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
  2717. false, false, false),
  2718. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
  2719. false, false, false),
  2720. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
  2721. false, false, false),
  2722. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
  2723. false, false, false),
  2724. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
  2725. false, false, false),
  2726. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
  2727. false, false, false),
  2728. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
  2729. false, false, true),
  2730. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
  2731. false, false, true),
  2732. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
  2733. false, false, true),
  2734. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
  2735. false, false, true),
  2736. VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB64, &vmw_cmd_invalid,
  2737. false, false, true),
  2738. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
  2739. false, false, true),
  2740. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
  2741. false, false, true),
  2742. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
  2743. false, false, true),
  2744. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
  2745. true, false, true),
  2746. VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
  2747. false, false, true),
  2748. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
  2749. true, false, true),
  2750. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
  2751. &vmw_cmd_update_gb_surface, true, false, true),
  2752. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
  2753. &vmw_cmd_readback_gb_image, true, false, true),
  2754. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
  2755. &vmw_cmd_readback_gb_surface, true, false, true),
  2756. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
  2757. &vmw_cmd_invalidate_gb_image, true, false, true),
  2758. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
  2759. &vmw_cmd_invalidate_gb_surface, true, false, true),
  2760. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
  2761. false, false, true),
  2762. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
  2763. false, false, true),
  2764. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
  2765. false, false, true),
  2766. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
  2767. false, false, true),
  2768. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
  2769. false, false, true),
  2770. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
  2771. false, false, true),
  2772. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
  2773. true, false, true),
  2774. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
  2775. false, false, true),
  2776. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
  2777. false, false, false),
  2778. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
  2779. true, false, true),
  2780. VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
  2781. true, false, true),
  2782. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
  2783. true, false, true),
  2784. VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
  2785. true, false, true),
  2786. VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
  2787. false, false, true),
  2788. VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
  2789. false, false, true),
  2790. VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
  2791. false, false, true),
  2792. VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
  2793. false, false, true),
  2794. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
  2795. false, false, true),
  2796. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
  2797. false, false, true),
  2798. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
  2799. false, false, true),
  2800. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
  2801. false, false, true),
  2802. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  2803. false, false, true),
  2804. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  2805. false, false, true),
  2806. VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
  2807. true, false, true),
  2808. VMW_CMD_DEF(SVGA_3D_CMD_GB_SCREEN_DMA, &vmw_cmd_invalid,
  2809. false, false, true),
  2810. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE_WITH_PITCH, &vmw_cmd_invalid,
  2811. false, false, true),
  2812. VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
  2813. false, false, true),
  2814. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
  2815. false, false, true),
  2816. /*
  2817. * DX commands
  2818. */
  2819. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
  2820. false, false, true),
  2821. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_CONTEXT, &vmw_cmd_invalid,
  2822. false, false, true),
  2823. VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_CONTEXT, &vmw_cmd_invalid,
  2824. false, false, true),
  2825. VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_CONTEXT, &vmw_cmd_invalid,
  2826. false, false, true),
  2827. VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_CONTEXT, &vmw_cmd_invalid,
  2828. false, false, true),
  2829. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER,
  2830. &vmw_cmd_dx_set_single_constant_buffer, true, false, true),
  2831. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER_RESOURCES,
  2832. &vmw_cmd_dx_set_shader_res, true, false, true),
  2833. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SHADER, &vmw_cmd_dx_set_shader,
  2834. true, false, true),
  2835. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SAMPLERS, &vmw_cmd_dx_cid_check,
  2836. true, false, true),
  2837. VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW, &vmw_cmd_dx_cid_check,
  2838. true, false, true),
  2839. VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED, &vmw_cmd_dx_cid_check,
  2840. true, false, true),
  2841. VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INSTANCED, &vmw_cmd_dx_cid_check,
  2842. true, false, true),
  2843. VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_INDEXED_INSTANCED,
  2844. &vmw_cmd_dx_cid_check, true, false, true),
  2845. VMW_CMD_DEF(SVGA_3D_CMD_DX_DRAW_AUTO, &vmw_cmd_dx_cid_check,
  2846. true, false, true),
  2847. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS,
  2848. &vmw_cmd_dx_set_vertex_buffers, true, false, true),
  2849. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INDEX_BUFFER,
  2850. &vmw_cmd_dx_set_index_buffer, true, false, true),
  2851. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RENDERTARGETS,
  2852. &vmw_cmd_dx_set_rendertargets, true, false, true),
  2853. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_BLEND_STATE, &vmw_cmd_dx_cid_check,
  2854. true, false, true),
  2855. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_DEPTHSTENCIL_STATE,
  2856. &vmw_cmd_dx_cid_check, true, false, true),
  2857. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_RASTERIZER_STATE,
  2858. &vmw_cmd_dx_cid_check, true, false, true),
  2859. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_QUERY, &vmw_cmd_dx_define_query,
  2860. true, false, true),
  2861. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_QUERY, &vmw_cmd_dx_cid_check,
  2862. true, false, true),
  2863. VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_QUERY, &vmw_cmd_dx_bind_query,
  2864. true, false, true),
  2865. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_QUERY_OFFSET,
  2866. &vmw_cmd_dx_cid_check, true, false, true),
  2867. VMW_CMD_DEF(SVGA_3D_CMD_DX_BEGIN_QUERY, &vmw_cmd_dx_cid_check,
  2868. true, false, true),
  2869. VMW_CMD_DEF(SVGA_3D_CMD_DX_END_QUERY, &vmw_cmd_dx_cid_check,
  2870. true, false, true),
  2871. VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_QUERY, &vmw_cmd_invalid,
  2872. true, false, true),
  2873. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_PREDICATION, &vmw_cmd_dx_cid_check,
  2874. true, false, true),
  2875. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_VIEWPORTS, &vmw_cmd_dx_cid_check,
  2876. true, false, true),
  2877. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SCISSORRECTS, &vmw_cmd_dx_cid_check,
  2878. true, false, true),
  2879. VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_RENDERTARGET_VIEW,
  2880. &vmw_cmd_dx_clear_rendertarget_view, true, false, true),
  2881. VMW_CMD_DEF(SVGA_3D_CMD_DX_CLEAR_DEPTHSTENCIL_VIEW,
  2882. &vmw_cmd_dx_clear_depthstencil_view, true, false, true),
  2883. VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY, &vmw_cmd_invalid,
  2884. true, false, true),
  2885. VMW_CMD_DEF(SVGA_3D_CMD_DX_GENMIPS, &vmw_cmd_invalid,
  2886. true, false, true),
  2887. VMW_CMD_DEF(SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE,
  2888. &vmw_cmd_dx_check_subresource, true, false, true),
  2889. VMW_CMD_DEF(SVGA_3D_CMD_DX_READBACK_SUBRESOURCE,
  2890. &vmw_cmd_dx_check_subresource, true, false, true),
  2891. VMW_CMD_DEF(SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE,
  2892. &vmw_cmd_dx_check_subresource, true, false, true),
  2893. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW,
  2894. &vmw_cmd_dx_view_define, true, false, true),
  2895. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
  2896. &vmw_cmd_dx_view_remove, true, false, true),
  2897. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW,
  2898. &vmw_cmd_dx_view_define, true, false, true),
  2899. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
  2900. &vmw_cmd_dx_view_remove, true, false, true),
  2901. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW,
  2902. &vmw_cmd_dx_view_define, true, false, true),
  2903. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
  2904. &vmw_cmd_dx_view_remove, true, false, true),
  2905. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_ELEMENTLAYOUT,
  2906. &vmw_cmd_dx_so_define, true, false, true),
  2907. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_ELEMENTLAYOUT,
  2908. &vmw_cmd_dx_cid_check, true, false, true),
  2909. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_BLEND_STATE,
  2910. &vmw_cmd_dx_so_define, true, false, true),
  2911. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_BLEND_STATE,
  2912. &vmw_cmd_dx_cid_check, true, false, true),
  2913. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_STATE,
  2914. &vmw_cmd_dx_so_define, true, false, true),
  2915. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_STATE,
  2916. &vmw_cmd_dx_cid_check, true, false, true),
  2917. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_RASTERIZER_STATE,
  2918. &vmw_cmd_dx_so_define, true, false, true),
  2919. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_RASTERIZER_STATE,
  2920. &vmw_cmd_dx_cid_check, true, false, true),
  2921. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SAMPLER_STATE,
  2922. &vmw_cmd_dx_so_define, true, false, true),
  2923. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SAMPLER_STATE,
  2924. &vmw_cmd_dx_cid_check, true, false, true),
  2925. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_SHADER,
  2926. &vmw_cmd_dx_define_shader, true, false, true),
  2927. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_SHADER,
  2928. &vmw_cmd_dx_destroy_shader, true, false, true),
  2929. VMW_CMD_DEF(SVGA_3D_CMD_DX_BIND_SHADER,
  2930. &vmw_cmd_dx_bind_shader, true, false, true),
  2931. VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_STREAMOUTPUT,
  2932. &vmw_cmd_dx_so_define, true, false, true),
  2933. VMW_CMD_DEF(SVGA_3D_CMD_DX_DESTROY_STREAMOUTPUT,
  2934. &vmw_cmd_dx_cid_check, true, false, true),
  2935. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_STREAMOUTPUT, &vmw_cmd_dx_cid_check,
  2936. true, false, true),
  2937. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_SOTARGETS,
  2938. &vmw_cmd_dx_set_so_targets, true, false, true),
  2939. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_INPUT_LAYOUT,
  2940. &vmw_cmd_dx_cid_check, true, false, true),
  2941. VMW_CMD_DEF(SVGA_3D_CMD_DX_SET_TOPOLOGY,
  2942. &vmw_cmd_dx_cid_check, true, false, true),
  2943. VMW_CMD_DEF(SVGA_3D_CMD_DX_BUFFER_COPY,
  2944. &vmw_cmd_buffer_copy_check, true, false, true),
  2945. VMW_CMD_DEF(SVGA_3D_CMD_DX_PRED_COPY_REGION,
  2946. &vmw_cmd_pred_copy_check, true, false, true),
  2947. };
  2948. static int vmw_cmd_check(struct vmw_private *dev_priv,
  2949. struct vmw_sw_context *sw_context,
  2950. void *buf, uint32_t *size)
  2951. {
  2952. uint32_t cmd_id;
  2953. uint32_t size_remaining = *size;
  2954. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  2955. int ret;
  2956. const struct vmw_cmd_entry *entry;
  2957. bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
  2958. cmd_id = ((uint32_t *)buf)[0];
  2959. /* Handle any none 3D commands */
  2960. if (unlikely(cmd_id < SVGA_CMD_MAX))
  2961. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  2962. cmd_id = header->id;
  2963. *size = header->size + sizeof(SVGA3dCmdHeader);
  2964. cmd_id -= SVGA_3D_CMD_BASE;
  2965. if (unlikely(*size > size_remaining))
  2966. goto out_invalid;
  2967. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  2968. goto out_invalid;
  2969. entry = &vmw_cmd_entries[cmd_id];
  2970. if (unlikely(!entry->func))
  2971. goto out_invalid;
  2972. if (unlikely(!entry->user_allow && !sw_context->kernel))
  2973. goto out_privileged;
  2974. if (unlikely(entry->gb_disable && gb))
  2975. goto out_old;
  2976. if (unlikely(entry->gb_enable && !gb))
  2977. goto out_new;
  2978. ret = entry->func(dev_priv, sw_context, header);
  2979. if (unlikely(ret != 0))
  2980. goto out_invalid;
  2981. return 0;
  2982. out_invalid:
  2983. DRM_ERROR("Invalid SVGA3D command: %d\n",
  2984. cmd_id + SVGA_3D_CMD_BASE);
  2985. return -EINVAL;
  2986. out_privileged:
  2987. DRM_ERROR("Privileged SVGA3D command: %d\n",
  2988. cmd_id + SVGA_3D_CMD_BASE);
  2989. return -EPERM;
  2990. out_old:
  2991. DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
  2992. cmd_id + SVGA_3D_CMD_BASE);
  2993. return -EINVAL;
  2994. out_new:
  2995. DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
  2996. cmd_id + SVGA_3D_CMD_BASE);
  2997. return -EINVAL;
  2998. }
  2999. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  3000. struct vmw_sw_context *sw_context,
  3001. void *buf,
  3002. uint32_t size)
  3003. {
  3004. int32_t cur_size = size;
  3005. int ret;
  3006. sw_context->buf_start = buf;
  3007. while (cur_size > 0) {
  3008. size = cur_size;
  3009. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  3010. if (unlikely(ret != 0))
  3011. return ret;
  3012. buf = (void *)((unsigned long) buf + size);
  3013. cur_size -= size;
  3014. }
  3015. if (unlikely(cur_size != 0)) {
  3016. DRM_ERROR("Command verifier out of sync.\n");
  3017. return -EINVAL;
  3018. }
  3019. return 0;
  3020. }
  3021. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  3022. {
  3023. sw_context->cur_reloc = 0;
  3024. }
  3025. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  3026. {
  3027. uint32_t i;
  3028. struct vmw_relocation *reloc;
  3029. struct ttm_validate_buffer *validate;
  3030. struct ttm_buffer_object *bo;
  3031. for (i = 0; i < sw_context->cur_reloc; ++i) {
  3032. reloc = &sw_context->relocs[i];
  3033. validate = &sw_context->val_bufs[reloc->index].base;
  3034. bo = validate->bo;
  3035. switch (bo->mem.mem_type) {
  3036. case TTM_PL_VRAM:
  3037. reloc->location->offset += bo->offset;
  3038. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  3039. break;
  3040. case VMW_PL_GMR:
  3041. reloc->location->gmrId = bo->mem.start;
  3042. break;
  3043. case VMW_PL_MOB:
  3044. *reloc->mob_loc = bo->mem.start;
  3045. break;
  3046. default:
  3047. BUG();
  3048. }
  3049. }
  3050. vmw_free_relocations(sw_context);
  3051. }
  3052. /**
  3053. * vmw_resource_list_unrefererence - Free up a resource list and unreference
  3054. * all resources referenced by it.
  3055. *
  3056. * @list: The resource list.
  3057. */
  3058. static void vmw_resource_list_unreference(struct vmw_sw_context *sw_context,
  3059. struct list_head *list)
  3060. {
  3061. struct vmw_resource_val_node *val, *val_next;
  3062. /*
  3063. * Drop references to resources held during command submission.
  3064. */
  3065. list_for_each_entry_safe(val, val_next, list, head) {
  3066. list_del_init(&val->head);
  3067. vmw_resource_unreference(&val->res);
  3068. if (val->staged_bindings) {
  3069. if (val->staged_bindings != sw_context->staged_bindings)
  3070. vmw_binding_state_free(val->staged_bindings);
  3071. else
  3072. sw_context->staged_bindings_inuse = false;
  3073. val->staged_bindings = NULL;
  3074. }
  3075. kfree(val);
  3076. }
  3077. }
  3078. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  3079. {
  3080. struct vmw_validate_buffer *entry, *next;
  3081. struct vmw_resource_val_node *val;
  3082. /*
  3083. * Drop references to DMA buffers held during command submission.
  3084. */
  3085. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  3086. base.head) {
  3087. list_del(&entry->base.head);
  3088. ttm_bo_unref(&entry->base.bo);
  3089. (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
  3090. sw_context->cur_val_buf--;
  3091. }
  3092. BUG_ON(sw_context->cur_val_buf != 0);
  3093. list_for_each_entry(val, &sw_context->resource_list, head)
  3094. (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
  3095. }
  3096. int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  3097. struct ttm_buffer_object *bo,
  3098. bool interruptible,
  3099. bool validate_as_mob)
  3100. {
  3101. struct vmw_dma_buffer *vbo = container_of(bo, struct vmw_dma_buffer,
  3102. base);
  3103. int ret;
  3104. if (vbo->pin_count > 0)
  3105. return 0;
  3106. if (validate_as_mob)
  3107. return ttm_bo_validate(bo, &vmw_mob_placement, interruptible,
  3108. false);
  3109. /**
  3110. * Put BO in VRAM if there is space, otherwise as a GMR.
  3111. * If there is no space in VRAM and GMR ids are all used up,
  3112. * start evicting GMRs to make room. If the DMA buffer can't be
  3113. * used as a GMR, this will return -ENOMEM.
  3114. */
  3115. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
  3116. false);
  3117. if (likely(ret == 0 || ret == -ERESTARTSYS))
  3118. return ret;
  3119. /**
  3120. * If that failed, try VRAM again, this time evicting
  3121. * previous contents.
  3122. */
  3123. ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
  3124. return ret;
  3125. }
  3126. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  3127. struct vmw_sw_context *sw_context)
  3128. {
  3129. struct vmw_validate_buffer *entry;
  3130. int ret;
  3131. list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
  3132. ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
  3133. true,
  3134. entry->validate_as_mob);
  3135. if (unlikely(ret != 0))
  3136. return ret;
  3137. }
  3138. return 0;
  3139. }
  3140. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  3141. uint32_t size)
  3142. {
  3143. if (likely(sw_context->cmd_bounce_size >= size))
  3144. return 0;
  3145. if (sw_context->cmd_bounce_size == 0)
  3146. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  3147. while (sw_context->cmd_bounce_size < size) {
  3148. sw_context->cmd_bounce_size =
  3149. PAGE_ALIGN(sw_context->cmd_bounce_size +
  3150. (sw_context->cmd_bounce_size >> 1));
  3151. }
  3152. if (sw_context->cmd_bounce != NULL)
  3153. vfree(sw_context->cmd_bounce);
  3154. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  3155. if (sw_context->cmd_bounce == NULL) {
  3156. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  3157. sw_context->cmd_bounce_size = 0;
  3158. return -ENOMEM;
  3159. }
  3160. return 0;
  3161. }
  3162. /**
  3163. * vmw_execbuf_fence_commands - create and submit a command stream fence
  3164. *
  3165. * Creates a fence object and submits a command stream marker.
  3166. * If this fails for some reason, We sync the fifo and return NULL.
  3167. * It is then safe to fence buffers with a NULL pointer.
  3168. *
  3169. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  3170. * a userspace handle if @p_handle is not NULL, otherwise not.
  3171. */
  3172. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  3173. struct vmw_private *dev_priv,
  3174. struct vmw_fence_obj **p_fence,
  3175. uint32_t *p_handle)
  3176. {
  3177. uint32_t sequence;
  3178. int ret;
  3179. bool synced = false;
  3180. /* p_handle implies file_priv. */
  3181. BUG_ON(p_handle != NULL && file_priv == NULL);
  3182. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  3183. if (unlikely(ret != 0)) {
  3184. DRM_ERROR("Fence submission error. Syncing.\n");
  3185. synced = true;
  3186. }
  3187. if (p_handle != NULL)
  3188. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  3189. sequence, p_fence, p_handle);
  3190. else
  3191. ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
  3192. if (unlikely(ret != 0 && !synced)) {
  3193. (void) vmw_fallback_wait(dev_priv, false, false,
  3194. sequence, false,
  3195. VMW_FENCE_WAIT_TIMEOUT);
  3196. *p_fence = NULL;
  3197. }
  3198. return ret;
  3199. }
  3200. /**
  3201. * vmw_execbuf_copy_fence_user - copy fence object information to
  3202. * user-space.
  3203. *
  3204. * @dev_priv: Pointer to a vmw_private struct.
  3205. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  3206. * @ret: Return value from fence object creation.
  3207. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  3208. * which the information should be copied.
  3209. * @fence: Pointer to the fenc object.
  3210. * @fence_handle: User-space fence handle.
  3211. *
  3212. * This function copies fence information to user-space. If copying fails,
  3213. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  3214. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  3215. * the error will hopefully be detected.
  3216. * Also if copying fails, user-space will be unable to signal the fence
  3217. * object so we wait for it immediately, and then unreference the
  3218. * user-space reference.
  3219. */
  3220. void
  3221. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  3222. struct vmw_fpriv *vmw_fp,
  3223. int ret,
  3224. struct drm_vmw_fence_rep __user *user_fence_rep,
  3225. struct vmw_fence_obj *fence,
  3226. uint32_t fence_handle)
  3227. {
  3228. struct drm_vmw_fence_rep fence_rep;
  3229. if (user_fence_rep == NULL)
  3230. return;
  3231. memset(&fence_rep, 0, sizeof(fence_rep));
  3232. fence_rep.error = ret;
  3233. if (ret == 0) {
  3234. BUG_ON(fence == NULL);
  3235. fence_rep.handle = fence_handle;
  3236. fence_rep.seqno = fence->base.seqno;
  3237. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  3238. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  3239. }
  3240. /*
  3241. * copy_to_user errors will be detected by user space not
  3242. * seeing fence_rep::error filled in. Typically
  3243. * user-space would have pre-set that member to -EFAULT.
  3244. */
  3245. ret = copy_to_user(user_fence_rep, &fence_rep,
  3246. sizeof(fence_rep));
  3247. /*
  3248. * User-space lost the fence object. We need to sync
  3249. * and unreference the handle.
  3250. */
  3251. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  3252. ttm_ref_object_base_unref(vmw_fp->tfile,
  3253. fence_handle, TTM_REF_USAGE);
  3254. DRM_ERROR("Fence copy error. Syncing.\n");
  3255. (void) vmw_fence_obj_wait(fence, false, false,
  3256. VMW_FENCE_WAIT_TIMEOUT);
  3257. }
  3258. }
  3259. /**
  3260. * vmw_execbuf_submit_fifo - Patch a command batch and submit it using
  3261. * the fifo.
  3262. *
  3263. * @dev_priv: Pointer to a device private structure.
  3264. * @kernel_commands: Pointer to the unpatched command batch.
  3265. * @command_size: Size of the unpatched command batch.
  3266. * @sw_context: Structure holding the relocation lists.
  3267. *
  3268. * Side effects: If this function returns 0, then the command batch
  3269. * pointed to by @kernel_commands will have been modified.
  3270. */
  3271. static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
  3272. void *kernel_commands,
  3273. u32 command_size,
  3274. struct vmw_sw_context *sw_context)
  3275. {
  3276. void *cmd;
  3277. if (sw_context->dx_ctx_node)
  3278. cmd = vmw_fifo_reserve_dx(dev_priv, command_size,
  3279. sw_context->dx_ctx_node->res->id);
  3280. else
  3281. cmd = vmw_fifo_reserve(dev_priv, command_size);
  3282. if (!cmd) {
  3283. DRM_ERROR("Failed reserving fifo space for commands.\n");
  3284. return -ENOMEM;
  3285. }
  3286. vmw_apply_relocations(sw_context);
  3287. memcpy(cmd, kernel_commands, command_size);
  3288. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  3289. vmw_resource_relocations_free(&sw_context->res_relocations);
  3290. vmw_fifo_commit(dev_priv, command_size);
  3291. return 0;
  3292. }
  3293. /**
  3294. * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using
  3295. * the command buffer manager.
  3296. *
  3297. * @dev_priv: Pointer to a device private structure.
  3298. * @header: Opaque handle to the command buffer allocation.
  3299. * @command_size: Size of the unpatched command batch.
  3300. * @sw_context: Structure holding the relocation lists.
  3301. *
  3302. * Side effects: If this function returns 0, then the command buffer
  3303. * represented by @header will have been modified.
  3304. */
  3305. static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
  3306. struct vmw_cmdbuf_header *header,
  3307. u32 command_size,
  3308. struct vmw_sw_context *sw_context)
  3309. {
  3310. u32 id = ((sw_context->dx_ctx_node) ? sw_context->dx_ctx_node->res->id :
  3311. SVGA3D_INVALID_ID);
  3312. void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size,
  3313. id, false, header);
  3314. vmw_apply_relocations(sw_context);
  3315. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  3316. vmw_resource_relocations_free(&sw_context->res_relocations);
  3317. vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
  3318. return 0;
  3319. }
  3320. /**
  3321. * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
  3322. * submission using a command buffer.
  3323. *
  3324. * @dev_priv: Pointer to a device private structure.
  3325. * @user_commands: User-space pointer to the commands to be submitted.
  3326. * @command_size: Size of the unpatched command batch.
  3327. * @header: Out parameter returning the opaque pointer to the command buffer.
  3328. *
  3329. * This function checks whether we can use the command buffer manager for
  3330. * submission and if so, creates a command buffer of suitable size and
  3331. * copies the user data into that buffer.
  3332. *
  3333. * On successful return, the function returns a pointer to the data in the
  3334. * command buffer and *@header is set to non-NULL.
  3335. * If command buffers could not be used, the function will return the value
  3336. * of @kernel_commands on function call. That value may be NULL. In that case,
  3337. * the value of *@header will be set to NULL.
  3338. * If an error is encountered, the function will return a pointer error value.
  3339. * If the function is interrupted by a signal while sleeping, it will return
  3340. * -ERESTARTSYS casted to a pointer error value.
  3341. */
  3342. static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
  3343. void __user *user_commands,
  3344. void *kernel_commands,
  3345. u32 command_size,
  3346. struct vmw_cmdbuf_header **header)
  3347. {
  3348. size_t cmdbuf_size;
  3349. int ret;
  3350. *header = NULL;
  3351. if (command_size > SVGA_CB_MAX_SIZE) {
  3352. DRM_ERROR("Command buffer is too large.\n");
  3353. return ERR_PTR(-EINVAL);
  3354. }
  3355. if (!dev_priv->cman || kernel_commands)
  3356. return kernel_commands;
  3357. /* If possible, add a little space for fencing. */
  3358. cmdbuf_size = command_size + 512;
  3359. cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
  3360. kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size,
  3361. true, header);
  3362. if (IS_ERR(kernel_commands))
  3363. return kernel_commands;
  3364. ret = copy_from_user(kernel_commands, user_commands,
  3365. command_size);
  3366. if (ret) {
  3367. DRM_ERROR("Failed copying commands.\n");
  3368. vmw_cmdbuf_header_free(*header);
  3369. *header = NULL;
  3370. return ERR_PTR(-EFAULT);
  3371. }
  3372. return kernel_commands;
  3373. }
  3374. static int vmw_execbuf_tie_context(struct vmw_private *dev_priv,
  3375. struct vmw_sw_context *sw_context,
  3376. uint32_t handle)
  3377. {
  3378. struct vmw_resource_val_node *ctx_node;
  3379. struct vmw_resource *res;
  3380. int ret;
  3381. if (handle == SVGA3D_INVALID_ID)
  3382. return 0;
  3383. ret = vmw_user_resource_lookup_handle(dev_priv, sw_context->fp->tfile,
  3384. handle, user_context_converter,
  3385. &res);
  3386. if (unlikely(ret != 0)) {
  3387. DRM_ERROR("Could not find or user DX context 0x%08x.\n",
  3388. (unsigned) handle);
  3389. return ret;
  3390. }
  3391. ret = vmw_resource_val_add(sw_context, res, &ctx_node);
  3392. if (unlikely(ret != 0))
  3393. goto out_err;
  3394. sw_context->dx_ctx_node = ctx_node;
  3395. sw_context->man = vmw_context_res_man(res);
  3396. out_err:
  3397. vmw_resource_unreference(&res);
  3398. return ret;
  3399. }
  3400. int vmw_execbuf_process(struct drm_file *file_priv,
  3401. struct vmw_private *dev_priv,
  3402. void __user *user_commands,
  3403. void *kernel_commands,
  3404. uint32_t command_size,
  3405. uint64_t throttle_us,
  3406. uint32_t dx_context_handle,
  3407. struct drm_vmw_fence_rep __user *user_fence_rep,
  3408. struct vmw_fence_obj **out_fence)
  3409. {
  3410. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  3411. struct vmw_fence_obj *fence = NULL;
  3412. struct vmw_resource *error_resource;
  3413. struct list_head resource_list;
  3414. struct vmw_cmdbuf_header *header;
  3415. struct ww_acquire_ctx ticket;
  3416. uint32_t handle;
  3417. int ret;
  3418. if (throttle_us) {
  3419. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  3420. throttle_us);
  3421. if (ret)
  3422. return ret;
  3423. }
  3424. kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
  3425. kernel_commands, command_size,
  3426. &header);
  3427. if (IS_ERR(kernel_commands))
  3428. return PTR_ERR(kernel_commands);
  3429. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  3430. if (ret) {
  3431. ret = -ERESTARTSYS;
  3432. goto out_free_header;
  3433. }
  3434. sw_context->kernel = false;
  3435. if (kernel_commands == NULL) {
  3436. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  3437. if (unlikely(ret != 0))
  3438. goto out_unlock;
  3439. ret = copy_from_user(sw_context->cmd_bounce,
  3440. user_commands, command_size);
  3441. if (unlikely(ret != 0)) {
  3442. ret = -EFAULT;
  3443. DRM_ERROR("Failed copying commands.\n");
  3444. goto out_unlock;
  3445. }
  3446. kernel_commands = sw_context->cmd_bounce;
  3447. } else if (!header)
  3448. sw_context->kernel = true;
  3449. sw_context->fp = vmw_fpriv(file_priv);
  3450. sw_context->cur_reloc = 0;
  3451. sw_context->cur_val_buf = 0;
  3452. INIT_LIST_HEAD(&sw_context->resource_list);
  3453. INIT_LIST_HEAD(&sw_context->ctx_resource_list);
  3454. sw_context->cur_query_bo = dev_priv->pinned_bo;
  3455. sw_context->last_query_ctx = NULL;
  3456. sw_context->needs_post_query_barrier = false;
  3457. sw_context->dx_ctx_node = NULL;
  3458. sw_context->dx_query_mob = NULL;
  3459. sw_context->dx_query_ctx = NULL;
  3460. memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
  3461. INIT_LIST_HEAD(&sw_context->validate_nodes);
  3462. INIT_LIST_HEAD(&sw_context->res_relocations);
  3463. if (sw_context->staged_bindings)
  3464. vmw_binding_state_reset(sw_context->staged_bindings);
  3465. if (!sw_context->res_ht_initialized) {
  3466. ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
  3467. if (unlikely(ret != 0))
  3468. goto out_unlock;
  3469. sw_context->res_ht_initialized = true;
  3470. }
  3471. INIT_LIST_HEAD(&sw_context->staged_cmd_res);
  3472. INIT_LIST_HEAD(&resource_list);
  3473. ret = vmw_execbuf_tie_context(dev_priv, sw_context, dx_context_handle);
  3474. if (unlikely(ret != 0)) {
  3475. list_splice_init(&sw_context->ctx_resource_list,
  3476. &sw_context->resource_list);
  3477. goto out_err_nores;
  3478. }
  3479. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  3480. command_size);
  3481. /*
  3482. * Merge the resource lists before checking the return status
  3483. * from vmd_cmd_check_all so that all the open hashtabs will
  3484. * be handled properly even if vmw_cmd_check_all fails.
  3485. */
  3486. list_splice_init(&sw_context->ctx_resource_list,
  3487. &sw_context->resource_list);
  3488. if (unlikely(ret != 0))
  3489. goto out_err_nores;
  3490. ret = vmw_resources_reserve(sw_context);
  3491. if (unlikely(ret != 0))
  3492. goto out_err_nores;
  3493. ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes,
  3494. true, NULL);
  3495. if (unlikely(ret != 0))
  3496. goto out_err_nores;
  3497. ret = vmw_validate_buffers(dev_priv, sw_context);
  3498. if (unlikely(ret != 0))
  3499. goto out_err;
  3500. ret = vmw_resources_validate(sw_context);
  3501. if (unlikely(ret != 0))
  3502. goto out_err;
  3503. ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
  3504. if (unlikely(ret != 0)) {
  3505. ret = -ERESTARTSYS;
  3506. goto out_err;
  3507. }
  3508. if (dev_priv->has_mob) {
  3509. ret = vmw_rebind_contexts(sw_context);
  3510. if (unlikely(ret != 0))
  3511. goto out_unlock_binding;
  3512. }
  3513. if (!header) {
  3514. ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
  3515. command_size, sw_context);
  3516. } else {
  3517. ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
  3518. sw_context);
  3519. header = NULL;
  3520. }
  3521. mutex_unlock(&dev_priv->binding_mutex);
  3522. if (ret)
  3523. goto out_err;
  3524. vmw_query_bo_switch_commit(dev_priv, sw_context);
  3525. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  3526. &fence,
  3527. (user_fence_rep) ? &handle : NULL);
  3528. /*
  3529. * This error is harmless, because if fence submission fails,
  3530. * vmw_fifo_send_fence will sync. The error will be propagated to
  3531. * user-space in @fence_rep
  3532. */
  3533. if (ret != 0)
  3534. DRM_ERROR("Fence submission error. Syncing.\n");
  3535. vmw_resources_unreserve(sw_context, false);
  3536. ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
  3537. (void *) fence);
  3538. if (unlikely(dev_priv->pinned_bo != NULL &&
  3539. !dev_priv->query_cid_valid))
  3540. __vmw_execbuf_release_pinned_bo(dev_priv, fence);
  3541. vmw_clear_validations(sw_context);
  3542. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  3543. user_fence_rep, fence, handle);
  3544. /* Don't unreference when handing fence out */
  3545. if (unlikely(out_fence != NULL)) {
  3546. *out_fence = fence;
  3547. fence = NULL;
  3548. } else if (likely(fence != NULL)) {
  3549. vmw_fence_obj_unreference(&fence);
  3550. }
  3551. list_splice_init(&sw_context->resource_list, &resource_list);
  3552. vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
  3553. mutex_unlock(&dev_priv->cmdbuf_mutex);
  3554. /*
  3555. * Unreference resources outside of the cmdbuf_mutex to
  3556. * avoid deadlocks in resource destruction paths.
  3557. */
  3558. vmw_resource_list_unreference(sw_context, &resource_list);
  3559. return 0;
  3560. out_unlock_binding:
  3561. mutex_unlock(&dev_priv->binding_mutex);
  3562. out_err:
  3563. ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
  3564. out_err_nores:
  3565. vmw_resources_unreserve(sw_context, true);
  3566. vmw_resource_relocations_free(&sw_context->res_relocations);
  3567. vmw_free_relocations(sw_context);
  3568. vmw_clear_validations(sw_context);
  3569. if (unlikely(dev_priv->pinned_bo != NULL &&
  3570. !dev_priv->query_cid_valid))
  3571. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  3572. out_unlock:
  3573. list_splice_init(&sw_context->resource_list, &resource_list);
  3574. error_resource = sw_context->error_resource;
  3575. sw_context->error_resource = NULL;
  3576. vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
  3577. mutex_unlock(&dev_priv->cmdbuf_mutex);
  3578. /*
  3579. * Unreference resources outside of the cmdbuf_mutex to
  3580. * avoid deadlocks in resource destruction paths.
  3581. */
  3582. vmw_resource_list_unreference(sw_context, &resource_list);
  3583. if (unlikely(error_resource != NULL))
  3584. vmw_resource_unreference(&error_resource);
  3585. out_free_header:
  3586. if (header)
  3587. vmw_cmdbuf_header_free(header);
  3588. return ret;
  3589. }
  3590. /**
  3591. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  3592. *
  3593. * @dev_priv: The device private structure.
  3594. *
  3595. * This function is called to idle the fifo and unpin the query buffer
  3596. * if the normal way to do this hits an error, which should typically be
  3597. * extremely rare.
  3598. */
  3599. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  3600. {
  3601. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  3602. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  3603. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  3604. if (dev_priv->dummy_query_bo_pinned) {
  3605. vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
  3606. dev_priv->dummy_query_bo_pinned = false;
  3607. }
  3608. }
  3609. /**
  3610. * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  3611. * query bo.
  3612. *
  3613. * @dev_priv: The device private structure.
  3614. * @fence: If non-NULL should point to a struct vmw_fence_obj issued
  3615. * _after_ a query barrier that flushes all queries touching the current
  3616. * buffer pointed to by @dev_priv->pinned_bo
  3617. *
  3618. * This function should be used to unpin the pinned query bo, or
  3619. * as a query barrier when we need to make sure that all queries have
  3620. * finished before the next fifo command. (For example on hardware
  3621. * context destructions where the hardware may otherwise leak unfinished
  3622. * queries).
  3623. *
  3624. * This function does not return any failure codes, but make attempts
  3625. * to do safe unpinning in case of errors.
  3626. *
  3627. * The function will synchronize on the previous query barrier, and will
  3628. * thus not finish until that barrier has executed.
  3629. *
  3630. * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
  3631. * before calling this function.
  3632. */
  3633. void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  3634. struct vmw_fence_obj *fence)
  3635. {
  3636. int ret = 0;
  3637. struct list_head validate_list;
  3638. struct ttm_validate_buffer pinned_val, query_val;
  3639. struct vmw_fence_obj *lfence = NULL;
  3640. struct ww_acquire_ctx ticket;
  3641. if (dev_priv->pinned_bo == NULL)
  3642. goto out_unlock;
  3643. INIT_LIST_HEAD(&validate_list);
  3644. pinned_val.bo = ttm_bo_reference(&dev_priv->pinned_bo->base);
  3645. pinned_val.shared = false;
  3646. list_add_tail(&pinned_val.head, &validate_list);
  3647. query_val.bo = ttm_bo_reference(&dev_priv->dummy_query_bo->base);
  3648. query_val.shared = false;
  3649. list_add_tail(&query_val.head, &validate_list);
  3650. ret = ttm_eu_reserve_buffers(&ticket, &validate_list,
  3651. false, NULL);
  3652. if (unlikely(ret != 0)) {
  3653. vmw_execbuf_unpin_panic(dev_priv);
  3654. goto out_no_reserve;
  3655. }
  3656. if (dev_priv->query_cid_valid) {
  3657. BUG_ON(fence != NULL);
  3658. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  3659. if (unlikely(ret != 0)) {
  3660. vmw_execbuf_unpin_panic(dev_priv);
  3661. goto out_no_emit;
  3662. }
  3663. dev_priv->query_cid_valid = false;
  3664. }
  3665. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  3666. if (dev_priv->dummy_query_bo_pinned) {
  3667. vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
  3668. dev_priv->dummy_query_bo_pinned = false;
  3669. }
  3670. if (fence == NULL) {
  3671. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
  3672. NULL);
  3673. fence = lfence;
  3674. }
  3675. ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
  3676. if (lfence != NULL)
  3677. vmw_fence_obj_unreference(&lfence);
  3678. ttm_bo_unref(&query_val.bo);
  3679. ttm_bo_unref(&pinned_val.bo);
  3680. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  3681. DRM_INFO("Dummy query bo pin count: %d\n",
  3682. dev_priv->dummy_query_bo->pin_count);
  3683. out_unlock:
  3684. return;
  3685. out_no_emit:
  3686. ttm_eu_backoff_reservation(&ticket, &validate_list);
  3687. out_no_reserve:
  3688. ttm_bo_unref(&query_val.bo);
  3689. ttm_bo_unref(&pinned_val.bo);
  3690. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  3691. }
  3692. /**
  3693. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  3694. * query bo.
  3695. *
  3696. * @dev_priv: The device private structure.
  3697. *
  3698. * This function should be used to unpin the pinned query bo, or
  3699. * as a query barrier when we need to make sure that all queries have
  3700. * finished before the next fifo command. (For example on hardware
  3701. * context destructions where the hardware may otherwise leak unfinished
  3702. * queries).
  3703. *
  3704. * This function does not return any failure codes, but make attempts
  3705. * to do safe unpinning in case of errors.
  3706. *
  3707. * The function will synchronize on the previous query barrier, and will
  3708. * thus not finish until that barrier has executed.
  3709. */
  3710. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
  3711. {
  3712. mutex_lock(&dev_priv->cmdbuf_mutex);
  3713. if (dev_priv->query_cid_valid)
  3714. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  3715. mutex_unlock(&dev_priv->cmdbuf_mutex);
  3716. }
  3717. int vmw_execbuf_ioctl(struct drm_device *dev, unsigned long data,
  3718. struct drm_file *file_priv, size_t size)
  3719. {
  3720. struct vmw_private *dev_priv = vmw_priv(dev);
  3721. struct drm_vmw_execbuf_arg arg;
  3722. int ret;
  3723. static const size_t copy_offset[] = {
  3724. offsetof(struct drm_vmw_execbuf_arg, context_handle),
  3725. sizeof(struct drm_vmw_execbuf_arg)};
  3726. if (unlikely(size < copy_offset[0])) {
  3727. DRM_ERROR("Invalid command size, ioctl %d\n",
  3728. DRM_VMW_EXECBUF);
  3729. return -EINVAL;
  3730. }
  3731. if (copy_from_user(&arg, (void __user *) data, copy_offset[0]) != 0)
  3732. return -EFAULT;
  3733. /*
  3734. * Extend the ioctl argument while
  3735. * maintaining backwards compatibility:
  3736. * We take different code paths depending on the value of
  3737. * arg.version.
  3738. */
  3739. if (unlikely(arg.version > DRM_VMW_EXECBUF_VERSION ||
  3740. arg.version == 0)) {
  3741. DRM_ERROR("Incorrect execbuf version.\n");
  3742. return -EINVAL;
  3743. }
  3744. if (arg.version > 1 &&
  3745. copy_from_user(&arg.context_handle,
  3746. (void __user *) (data + copy_offset[0]),
  3747. copy_offset[arg.version - 1] -
  3748. copy_offset[0]) != 0)
  3749. return -EFAULT;
  3750. switch (arg.version) {
  3751. case 1:
  3752. arg.context_handle = (uint32_t) -1;
  3753. break;
  3754. case 2:
  3755. if (arg.pad64 != 0) {
  3756. DRM_ERROR("Unused IOCTL data not set to zero.\n");
  3757. return -EINVAL;
  3758. }
  3759. break;
  3760. default:
  3761. break;
  3762. }
  3763. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  3764. if (unlikely(ret != 0))
  3765. return ret;
  3766. ret = vmw_execbuf_process(file_priv, dev_priv,
  3767. (void __user *)(unsigned long)arg.commands,
  3768. NULL, arg.command_size, arg.throttle_us,
  3769. arg.context_handle,
  3770. (void __user *)(unsigned long)arg.fence_rep,
  3771. NULL);
  3772. ttm_read_unlock(&dev_priv->reservation_sem);
  3773. if (unlikely(ret != 0))
  3774. return ret;
  3775. vmw_kms_cursor_post_execbuf(dev_priv);
  3776. return 0;
  3777. }