vmwgfx_execbuf.c 129 KB

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