vmwgfx_execbuf.c 123 KB

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