vmwgfx_execbuf.c 131 KB

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