vmwgfx_execbuf.c 132 KB

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