vmwgfx_execbuf.c 125 KB

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