vmwgfx_execbuf.c 123 KB

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