i915_gem_execbuffer.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2008,2010 Intel Corporation
  5. */
  6. #include <linux/dma-resv.h>
  7. #include <linux/highmem.h>
  8. #include <linux/sync_file.h>
  9. #include <linux/uaccess.h>
  10. #include <drm/drm_auth.h>
  11. #include <drm/drm_syncobj.h>
  12. #include "gem/i915_gem_ioctls.h"
  13. #include "gt/intel_context.h"
  14. #include "gt/intel_gpu_commands.h"
  15. #include "gt/intel_gt.h"
  16. #include "gt/intel_gt_buffer_pool.h"
  17. #include "gt/intel_gt_pm.h"
  18. #include "gt/intel_ring.h"
  19. #include "pxp/intel_pxp.h"
  20. #include "i915_cmd_parser.h"
  21. #include "i915_drv.h"
  22. #include "i915_file_private.h"
  23. #include "i915_gem_clflush.h"
  24. #include "i915_gem_context.h"
  25. #include "i915_gem_evict.h"
  26. #include "i915_gem_ioctls.h"
  27. #include "i915_reg.h"
  28. #include "i915_trace.h"
  29. #include "i915_user_extensions.h"
  30. struct eb_vma {
  31. struct i915_vma *vma;
  32. unsigned int flags;
  33. /** This vma's place in the execbuf reservation list */
  34. struct drm_i915_gem_exec_object2 *exec;
  35. struct list_head bind_link;
  36. struct list_head reloc_link;
  37. struct hlist_node node;
  38. u32 handle;
  39. };
  40. enum {
  41. FORCE_CPU_RELOC = 1,
  42. FORCE_GTT_RELOC,
  43. FORCE_GPU_RELOC,
  44. #define DBG_FORCE_RELOC 0 /* choose one of the above! */
  45. };
  46. /* __EXEC_OBJECT_ flags > BIT(29) defined in i915_vma.h */
  47. #define __EXEC_OBJECT_HAS_PIN BIT(29)
  48. #define __EXEC_OBJECT_HAS_FENCE BIT(28)
  49. #define __EXEC_OBJECT_USERPTR_INIT BIT(27)
  50. #define __EXEC_OBJECT_NEEDS_MAP BIT(26)
  51. #define __EXEC_OBJECT_NEEDS_BIAS BIT(25)
  52. #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 25) /* all of the above + */
  53. #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
  54. #define __EXEC_HAS_RELOC BIT(31)
  55. #define __EXEC_ENGINE_PINNED BIT(30)
  56. #define __EXEC_USERPTR_USED BIT(29)
  57. #define __EXEC_INTERNAL_FLAGS (~0u << 29)
  58. #define UPDATE PIN_OFFSET_FIXED
  59. #define BATCH_OFFSET_BIAS (256*1024)
  60. #define __I915_EXEC_ILLEGAL_FLAGS \
  61. (__I915_EXEC_UNKNOWN_FLAGS | \
  62. I915_EXEC_CONSTANTS_MASK | \
  63. I915_EXEC_RESOURCE_STREAMER)
  64. /* Catch emission of unexpected errors for CI! */
  65. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
  66. #undef EINVAL
  67. #define EINVAL ({ \
  68. DRM_DEBUG_DRIVER("EINVAL at %s:%d\n", __func__, __LINE__); \
  69. 22; \
  70. })
  71. #endif
  72. /**
  73. * DOC: User command execution
  74. *
  75. * Userspace submits commands to be executed on the GPU as an instruction
  76. * stream within a GEM object we call a batchbuffer. This instructions may
  77. * refer to other GEM objects containing auxiliary state such as kernels,
  78. * samplers, render targets and even secondary batchbuffers. Userspace does
  79. * not know where in the GPU memory these objects reside and so before the
  80. * batchbuffer is passed to the GPU for execution, those addresses in the
  81. * batchbuffer and auxiliary objects are updated. This is known as relocation,
  82. * or patching. To try and avoid having to relocate each object on the next
  83. * execution, userspace is told the location of those objects in this pass,
  84. * but this remains just a hint as the kernel may choose a new location for
  85. * any object in the future.
  86. *
  87. * At the level of talking to the hardware, submitting a batchbuffer for the
  88. * GPU to execute is to add content to a buffer from which the HW
  89. * command streamer is reading.
  90. *
  91. * 1. Add a command to load the HW context. For Logical Ring Contexts, i.e.
  92. * Execlists, this command is not placed on the same buffer as the
  93. * remaining items.
  94. *
  95. * 2. Add a command to invalidate caches to the buffer.
  96. *
  97. * 3. Add a batchbuffer start command to the buffer; the start command is
  98. * essentially a token together with the GPU address of the batchbuffer
  99. * to be executed.
  100. *
  101. * 4. Add a pipeline flush to the buffer.
  102. *
  103. * 5. Add a memory write command to the buffer to record when the GPU
  104. * is done executing the batchbuffer. The memory write writes the
  105. * global sequence number of the request, ``i915_request::global_seqno``;
  106. * the i915 driver uses the current value in the register to determine
  107. * if the GPU has completed the batchbuffer.
  108. *
  109. * 6. Add a user interrupt command to the buffer. This command instructs
  110. * the GPU to issue an interrupt when the command, pipeline flush and
  111. * memory write are completed.
  112. *
  113. * 7. Inform the hardware of the additional commands added to the buffer
  114. * (by updating the tail pointer).
  115. *
  116. * Processing an execbuf ioctl is conceptually split up into a few phases.
  117. *
  118. * 1. Validation - Ensure all the pointers, handles and flags are valid.
  119. * 2. Reservation - Assign GPU address space for every object
  120. * 3. Relocation - Update any addresses to point to the final locations
  121. * 4. Serialisation - Order the request with respect to its dependencies
  122. * 5. Construction - Construct a request to execute the batchbuffer
  123. * 6. Submission (at some point in the future execution)
  124. *
  125. * Reserving resources for the execbuf is the most complicated phase. We
  126. * neither want to have to migrate the object in the address space, nor do
  127. * we want to have to update any relocations pointing to this object. Ideally,
  128. * we want to leave the object where it is and for all the existing relocations
  129. * to match. If the object is given a new address, or if userspace thinks the
  130. * object is elsewhere, we have to parse all the relocation entries and update
  131. * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
  132. * all the target addresses in all of its objects match the value in the
  133. * relocation entries and that they all match the presumed offsets given by the
  134. * list of execbuffer objects. Using this knowledge, we know that if we haven't
  135. * moved any buffers, all the relocation entries are valid and we can skip
  136. * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
  137. * hang.) The requirement for using I915_EXEC_NO_RELOC are:
  138. *
  139. * The addresses written in the objects must match the corresponding
  140. * reloc.presumed_offset which in turn must match the corresponding
  141. * execobject.offset.
  142. *
  143. * Any render targets written to in the batch must be flagged with
  144. * EXEC_OBJECT_WRITE.
  145. *
  146. * To avoid stalling, execobject.offset should match the current
  147. * address of that object within the active context.
  148. *
  149. * The reservation is done is multiple phases. First we try and keep any
  150. * object already bound in its current location - so as long as meets the
  151. * constraints imposed by the new execbuffer. Any object left unbound after the
  152. * first pass is then fitted into any available idle space. If an object does
  153. * not fit, all objects are removed from the reservation and the process rerun
  154. * after sorting the objects into a priority order (more difficult to fit
  155. * objects are tried first). Failing that, the entire VM is cleared and we try
  156. * to fit the execbuf once last time before concluding that it simply will not
  157. * fit.
  158. *
  159. * A small complication to all of this is that we allow userspace not only to
  160. * specify an alignment and a size for the object in the address space, but
  161. * we also allow userspace to specify the exact offset. This objects are
  162. * simpler to place (the location is known a priori) all we have to do is make
  163. * sure the space is available.
  164. *
  165. * Once all the objects are in place, patching up the buried pointers to point
  166. * to the final locations is a fairly simple job of walking over the relocation
  167. * entry arrays, looking up the right address and rewriting the value into
  168. * the object. Simple! ... The relocation entries are stored in user memory
  169. * and so to access them we have to copy them into a local buffer. That copy
  170. * has to avoid taking any pagefaults as they may lead back to a GEM object
  171. * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
  172. * the relocation into multiple passes. First we try to do everything within an
  173. * atomic context (avoid the pagefaults) which requires that we never wait. If
  174. * we detect that we may wait, or if we need to fault, then we have to fallback
  175. * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
  176. * bells yet?) Dropping the mutex means that we lose all the state we have
  177. * built up so far for the execbuf and we must reset any global data. However,
  178. * we do leave the objects pinned in their final locations - which is a
  179. * potential issue for concurrent execbufs. Once we have left the mutex, we can
  180. * allocate and copy all the relocation entries into a large array at our
  181. * leisure, reacquire the mutex, reclaim all the objects and other state and
  182. * then proceed to update any incorrect addresses with the objects.
  183. *
  184. * As we process the relocation entries, we maintain a record of whether the
  185. * object is being written to. Using NORELOC, we expect userspace to provide
  186. * this information instead. We also check whether we can skip the relocation
  187. * by comparing the expected value inside the relocation entry with the target's
  188. * final address. If they differ, we have to map the current object and rewrite
  189. * the 4 or 8 byte pointer within.
  190. *
  191. * Serialising an execbuf is quite simple according to the rules of the GEM
  192. * ABI. Execution within each context is ordered by the order of submission.
  193. * Writes to any GEM object are in order of submission and are exclusive. Reads
  194. * from a GEM object are unordered with respect to other reads, but ordered by
  195. * writes. A write submitted after a read cannot occur before the read, and
  196. * similarly any read submitted after a write cannot occur before the write.
  197. * Writes are ordered between engines such that only one write occurs at any
  198. * time (completing any reads beforehand) - using semaphores where available
  199. * and CPU serialisation otherwise. Other GEM access obey the same rules, any
  200. * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
  201. * reads before starting, and any read (either using set-domain or pread) must
  202. * flush all GPU writes before starting. (Note we only employ a barrier before,
  203. * we currently rely on userspace not concurrently starting a new execution
  204. * whilst reading or writing to an object. This may be an advantage or not
  205. * depending on how much you trust userspace not to shoot themselves in the
  206. * foot.) Serialisation may just result in the request being inserted into
  207. * a DAG awaiting its turn, but most simple is to wait on the CPU until
  208. * all dependencies are resolved.
  209. *
  210. * After all of that, is just a matter of closing the request and handing it to
  211. * the hardware (well, leaving it in a queue to be executed). However, we also
  212. * offer the ability for batchbuffers to be run with elevated privileges so
  213. * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
  214. * Before any batch is given extra privileges we first must check that it
  215. * contains no nefarious instructions, we check that each instruction is from
  216. * our whitelist and all registers are also from an allowed list. We first
  217. * copy the user's batchbuffer to a shadow (so that the user doesn't have
  218. * access to it, either by the CPU or GPU as we scan it) and then parse each
  219. * instruction. If everything is ok, we set a flag telling the hardware to run
  220. * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
  221. */
  222. struct eb_fence {
  223. struct drm_syncobj *syncobj; /* Use with ptr_mask_bits() */
  224. struct dma_fence *dma_fence;
  225. u64 value;
  226. struct dma_fence_chain *chain_fence;
  227. };
  228. struct i915_execbuffer {
  229. struct drm_i915_private *i915; /** i915 backpointer */
  230. struct drm_file *file; /** per-file lookup tables and limits */
  231. struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
  232. struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
  233. struct eb_vma *vma;
  234. struct intel_gt *gt; /* gt for the execbuf */
  235. struct intel_context *context; /* logical state for the request */
  236. struct i915_gem_context *gem_context; /** caller's context */
  237. intel_wakeref_t wakeref;
  238. intel_wakeref_t wakeref_gt0;
  239. /** our requests to build */
  240. struct i915_request *requests[MAX_ENGINE_INSTANCE + 1];
  241. /** identity of the batch obj/vma */
  242. struct eb_vma *batches[MAX_ENGINE_INSTANCE + 1];
  243. struct i915_vma *trampoline; /** trampoline used for chaining */
  244. /** used for excl fence in dma_resv objects when > 1 BB submitted */
  245. struct dma_fence *composite_fence;
  246. /** actual size of execobj[] as we may extend it for the cmdparser */
  247. unsigned int buffer_count;
  248. /* number of batches in execbuf IOCTL */
  249. unsigned int num_batches;
  250. /** list of vma not yet bound during reservation phase */
  251. struct list_head unbound;
  252. /** list of vma that have execobj.relocation_count */
  253. struct list_head relocs;
  254. struct i915_gem_ww_ctx ww;
  255. /**
  256. * Track the most recently used object for relocations, as we
  257. * frequently have to perform multiple relocations within the same
  258. * obj/page
  259. */
  260. struct reloc_cache {
  261. struct drm_mm_node node; /** temporary GTT binding */
  262. unsigned long vaddr; /** Current kmap address */
  263. unsigned long page; /** Currently mapped page index */
  264. unsigned int graphics_ver; /** Cached value of GRAPHICS_VER */
  265. bool use_64bit_reloc : 1;
  266. bool has_llc : 1;
  267. bool has_fence : 1;
  268. bool needs_unfenced : 1;
  269. } reloc_cache;
  270. u64 invalid_flags; /** Set of execobj.flags that are invalid */
  271. /** Length of batch within object */
  272. u64 batch_len[MAX_ENGINE_INSTANCE + 1];
  273. u32 batch_start_offset; /** Location within object of batch */
  274. u32 batch_flags; /** Flags composed for emit_bb_start() */
  275. struct intel_gt_buffer_pool_node *batch_pool; /** pool node for batch buffer */
  276. /**
  277. * Indicate either the size of the hastable used to resolve
  278. * relocation handles, or if negative that we are using a direct
  279. * index into the execobj[].
  280. */
  281. int lut_size;
  282. struct hlist_head *buckets; /** ht for relocation handles */
  283. struct eb_fence *fences;
  284. unsigned long num_fences;
  285. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  286. struct i915_capture_list *capture_lists[MAX_ENGINE_INSTANCE + 1];
  287. #endif
  288. };
  289. static int eb_parse(struct i915_execbuffer *eb);
  290. static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle);
  291. static void eb_unpin_engine(struct i915_execbuffer *eb);
  292. static void eb_capture_release(struct i915_execbuffer *eb);
  293. static bool eb_use_cmdparser(const struct i915_execbuffer *eb)
  294. {
  295. return intel_engine_requires_cmd_parser(eb->context->engine) ||
  296. (intel_engine_using_cmd_parser(eb->context->engine) &&
  297. eb->args->batch_len);
  298. }
  299. static int eb_create(struct i915_execbuffer *eb)
  300. {
  301. if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
  302. unsigned int size = 1 + ilog2(eb->buffer_count);
  303. /*
  304. * Without a 1:1 association between relocation handles and
  305. * the execobject[] index, we instead create a hashtable.
  306. * We size it dynamically based on available memory, starting
  307. * first with 1:1 associative hash and scaling back until
  308. * the allocation succeeds.
  309. *
  310. * Later on we use a positive lut_size to indicate we are
  311. * using this hashtable, and a negative value to indicate a
  312. * direct lookup.
  313. */
  314. do {
  315. gfp_t flags;
  316. /* While we can still reduce the allocation size, don't
  317. * raise a warning and allow the allocation to fail.
  318. * On the last pass though, we want to try as hard
  319. * as possible to perform the allocation and warn
  320. * if it fails.
  321. */
  322. flags = GFP_KERNEL;
  323. if (size > 1)
  324. flags |= __GFP_NORETRY | __GFP_NOWARN;
  325. eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
  326. flags);
  327. if (eb->buckets)
  328. break;
  329. } while (--size);
  330. if (unlikely(!size))
  331. return -ENOMEM;
  332. eb->lut_size = size;
  333. } else {
  334. eb->lut_size = -eb->buffer_count;
  335. }
  336. return 0;
  337. }
  338. static bool
  339. eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
  340. const struct i915_vma *vma,
  341. unsigned int flags)
  342. {
  343. const u64 start = i915_vma_offset(vma);
  344. const u64 size = i915_vma_size(vma);
  345. if (size < entry->pad_to_size)
  346. return true;
  347. if (entry->alignment && !IS_ALIGNED(start, entry->alignment))
  348. return true;
  349. if (flags & EXEC_OBJECT_PINNED &&
  350. start != entry->offset)
  351. return true;
  352. if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
  353. start < BATCH_OFFSET_BIAS)
  354. return true;
  355. if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
  356. (start + size + 4095) >> 32)
  357. return true;
  358. if (flags & __EXEC_OBJECT_NEEDS_MAP &&
  359. !i915_vma_is_map_and_fenceable(vma))
  360. return true;
  361. return false;
  362. }
  363. static u64 eb_pin_flags(const struct drm_i915_gem_exec_object2 *entry,
  364. unsigned int exec_flags)
  365. {
  366. u64 pin_flags = 0;
  367. if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
  368. pin_flags |= PIN_GLOBAL;
  369. /*
  370. * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  371. * limit address to the first 4GBs for unflagged objects.
  372. */
  373. if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
  374. pin_flags |= PIN_ZONE_4G;
  375. if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
  376. pin_flags |= PIN_MAPPABLE;
  377. if (exec_flags & EXEC_OBJECT_PINNED)
  378. pin_flags |= entry->offset | PIN_OFFSET_FIXED;
  379. else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS)
  380. pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  381. return pin_flags;
  382. }
  383. static int
  384. eb_pin_vma(struct i915_execbuffer *eb,
  385. const struct drm_i915_gem_exec_object2 *entry,
  386. struct eb_vma *ev)
  387. {
  388. struct i915_vma *vma = ev->vma;
  389. u64 pin_flags;
  390. int err;
  391. if (vma->node.size)
  392. pin_flags = __i915_vma_offset(vma);
  393. else
  394. pin_flags = entry->offset & PIN_OFFSET_MASK;
  395. pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED | PIN_VALIDATE;
  396. if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_GTT))
  397. pin_flags |= PIN_GLOBAL;
  398. /* Attempt to reuse the current location if available */
  399. err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, pin_flags);
  400. if (err == -EDEADLK)
  401. return err;
  402. if (unlikely(err)) {
  403. if (entry->flags & EXEC_OBJECT_PINNED)
  404. return err;
  405. /* Failing that pick any _free_ space if suitable */
  406. err = i915_vma_pin_ww(vma, &eb->ww,
  407. entry->pad_to_size,
  408. entry->alignment,
  409. eb_pin_flags(entry, ev->flags) |
  410. PIN_USER | PIN_NOEVICT | PIN_VALIDATE);
  411. if (unlikely(err))
  412. return err;
  413. }
  414. if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) {
  415. err = i915_vma_pin_fence(vma);
  416. if (unlikely(err))
  417. return err;
  418. if (vma->fence)
  419. ev->flags |= __EXEC_OBJECT_HAS_FENCE;
  420. }
  421. ev->flags |= __EXEC_OBJECT_HAS_PIN;
  422. if (eb_vma_misplaced(entry, vma, ev->flags))
  423. return -EBADSLT;
  424. return 0;
  425. }
  426. static void
  427. eb_unreserve_vma(struct eb_vma *ev)
  428. {
  429. if (unlikely(ev->flags & __EXEC_OBJECT_HAS_FENCE))
  430. __i915_vma_unpin_fence(ev->vma);
  431. ev->flags &= ~__EXEC_OBJECT_RESERVED;
  432. }
  433. static int
  434. eb_validate_vma(struct i915_execbuffer *eb,
  435. struct drm_i915_gem_exec_object2 *entry,
  436. struct i915_vma *vma)
  437. {
  438. /* Relocations are disallowed for all platforms after TGL-LP. This
  439. * also covers all platforms with local memory.
  440. */
  441. if (entry->relocation_count &&
  442. GRAPHICS_VER(eb->i915) >= 12 && !IS_TIGERLAKE(eb->i915))
  443. return -EINVAL;
  444. if (unlikely(entry->flags & eb->invalid_flags))
  445. return -EINVAL;
  446. if (unlikely(entry->alignment &&
  447. !is_power_of_2_u64(entry->alignment)))
  448. return -EINVAL;
  449. /*
  450. * Offset can be used as input (EXEC_OBJECT_PINNED), reject
  451. * any non-page-aligned or non-canonical addresses.
  452. */
  453. if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
  454. entry->offset != gen8_canonical_addr(entry->offset & I915_GTT_PAGE_MASK)))
  455. return -EINVAL;
  456. /* pad_to_size was once a reserved field, so sanitize it */
  457. if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
  458. if (unlikely(offset_in_page(entry->pad_to_size)))
  459. return -EINVAL;
  460. } else {
  461. entry->pad_to_size = 0;
  462. }
  463. /*
  464. * From drm_mm perspective address space is continuous,
  465. * so from this point we're always using non-canonical
  466. * form internally.
  467. */
  468. entry->offset = gen8_noncanonical_addr(entry->offset);
  469. if (!eb->reloc_cache.has_fence) {
  470. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  471. } else {
  472. if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
  473. eb->reloc_cache.needs_unfenced) &&
  474. i915_gem_object_is_tiled(vma->obj))
  475. entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
  476. }
  477. return 0;
  478. }
  479. static bool
  480. is_batch_buffer(struct i915_execbuffer *eb, unsigned int buffer_idx)
  481. {
  482. return eb->args->flags & I915_EXEC_BATCH_FIRST ?
  483. buffer_idx < eb->num_batches :
  484. buffer_idx >= eb->args->buffer_count - eb->num_batches;
  485. }
  486. static int
  487. eb_add_vma(struct i915_execbuffer *eb,
  488. unsigned int *current_batch,
  489. unsigned int i,
  490. struct i915_vma *vma)
  491. {
  492. struct drm_i915_private *i915 = eb->i915;
  493. struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
  494. struct eb_vma *ev = &eb->vma[i];
  495. ev->vma = vma;
  496. ev->exec = entry;
  497. ev->flags = entry->flags;
  498. if (eb->lut_size > 0) {
  499. ev->handle = entry->handle;
  500. hlist_add_head(&ev->node,
  501. &eb->buckets[hash_32(entry->handle,
  502. eb->lut_size)]);
  503. }
  504. if (entry->relocation_count)
  505. list_add_tail(&ev->reloc_link, &eb->relocs);
  506. /*
  507. * SNA is doing fancy tricks with compressing batch buffers, which leads
  508. * to negative relocation deltas. Usually that works out ok since the
  509. * relocate address is still positive, except when the batch is placed
  510. * very low in the GTT. Ensure this doesn't happen.
  511. *
  512. * Note that actual hangs have only been observed on gen7, but for
  513. * paranoia do it everywhere.
  514. */
  515. if (is_batch_buffer(eb, i)) {
  516. if (entry->relocation_count &&
  517. !(ev->flags & EXEC_OBJECT_PINNED))
  518. ev->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  519. if (eb->reloc_cache.has_fence)
  520. ev->flags |= EXEC_OBJECT_NEEDS_FENCE;
  521. eb->batches[*current_batch] = ev;
  522. if (unlikely(ev->flags & EXEC_OBJECT_WRITE)) {
  523. drm_dbg(&i915->drm,
  524. "Attempting to use self-modifying batch buffer\n");
  525. return -EINVAL;
  526. }
  527. if (range_overflows_t(u64,
  528. eb->batch_start_offset,
  529. eb->args->batch_len,
  530. ev->vma->size)) {
  531. drm_dbg(&i915->drm, "Attempting to use out-of-bounds batch\n");
  532. return -EINVAL;
  533. }
  534. if (eb->args->batch_len == 0)
  535. eb->batch_len[*current_batch] = ev->vma->size -
  536. eb->batch_start_offset;
  537. else
  538. eb->batch_len[*current_batch] = eb->args->batch_len;
  539. if (unlikely(eb->batch_len[*current_batch] == 0)) { /* impossible! */
  540. drm_dbg(&i915->drm, "Invalid batch length\n");
  541. return -EINVAL;
  542. }
  543. ++*current_batch;
  544. }
  545. return 0;
  546. }
  547. static int use_cpu_reloc(const struct reloc_cache *cache,
  548. const struct drm_i915_gem_object *obj)
  549. {
  550. if (!i915_gem_object_has_struct_page(obj))
  551. return false;
  552. if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
  553. return true;
  554. if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
  555. return false;
  556. /*
  557. * For objects created by userspace through GEM_CREATE with pat_index
  558. * set by set_pat extension, i915_gem_object_has_cache_level() always
  559. * return true, otherwise the call would fall back to checking whether
  560. * the object is un-cached.
  561. */
  562. return (cache->has_llc ||
  563. obj->cache_dirty ||
  564. !i915_gem_object_has_cache_level(obj, I915_CACHE_NONE));
  565. }
  566. static int eb_reserve_vma(struct i915_execbuffer *eb,
  567. struct eb_vma *ev,
  568. u64 pin_flags)
  569. {
  570. struct drm_i915_gem_exec_object2 *entry = ev->exec;
  571. struct i915_vma *vma = ev->vma;
  572. int err;
  573. if (drm_mm_node_allocated(&vma->node) &&
  574. eb_vma_misplaced(entry, vma, ev->flags)) {
  575. err = i915_vma_unbind(vma);
  576. if (err)
  577. return err;
  578. }
  579. err = i915_vma_pin_ww(vma, &eb->ww,
  580. entry->pad_to_size, entry->alignment,
  581. eb_pin_flags(entry, ev->flags) | pin_flags);
  582. if (err)
  583. return err;
  584. if (entry->offset != i915_vma_offset(vma)) {
  585. entry->offset = i915_vma_offset(vma) | UPDATE;
  586. eb->args->flags |= __EXEC_HAS_RELOC;
  587. }
  588. if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) {
  589. err = i915_vma_pin_fence(vma);
  590. if (unlikely(err))
  591. return err;
  592. if (vma->fence)
  593. ev->flags |= __EXEC_OBJECT_HAS_FENCE;
  594. }
  595. ev->flags |= __EXEC_OBJECT_HAS_PIN;
  596. GEM_BUG_ON(eb_vma_misplaced(entry, vma, ev->flags));
  597. return 0;
  598. }
  599. static bool eb_unbind(struct i915_execbuffer *eb, bool force)
  600. {
  601. const unsigned int count = eb->buffer_count;
  602. unsigned int i;
  603. struct list_head last;
  604. bool unpinned = false;
  605. /* Resort *all* the objects into priority order */
  606. INIT_LIST_HEAD(&eb->unbound);
  607. INIT_LIST_HEAD(&last);
  608. for (i = 0; i < count; i++) {
  609. struct eb_vma *ev = &eb->vma[i];
  610. unsigned int flags = ev->flags;
  611. if (!force && flags & EXEC_OBJECT_PINNED &&
  612. flags & __EXEC_OBJECT_HAS_PIN)
  613. continue;
  614. unpinned = true;
  615. eb_unreserve_vma(ev);
  616. if (flags & EXEC_OBJECT_PINNED)
  617. /* Pinned must have their slot */
  618. list_add(&ev->bind_link, &eb->unbound);
  619. else if (flags & __EXEC_OBJECT_NEEDS_MAP)
  620. /* Map require the lowest 256MiB (aperture) */
  621. list_add_tail(&ev->bind_link, &eb->unbound);
  622. else if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
  623. /* Prioritise 4GiB region for restricted bo */
  624. list_add(&ev->bind_link, &last);
  625. else
  626. list_add_tail(&ev->bind_link, &last);
  627. }
  628. list_splice_tail(&last, &eb->unbound);
  629. return unpinned;
  630. }
  631. static int eb_reserve(struct i915_execbuffer *eb)
  632. {
  633. struct eb_vma *ev;
  634. unsigned int pass;
  635. int err = 0;
  636. /*
  637. * We have one more buffers that we couldn't bind, which could be due to
  638. * various reasons. To resolve this we have 4 passes, with every next
  639. * level turning the screws tighter:
  640. *
  641. * 0. Unbind all objects that do not match the GTT constraints for the
  642. * execbuffer (fenceable, mappable, alignment etc). Bind all new
  643. * objects. This avoids unnecessary unbinding of later objects in order
  644. * to make room for the earlier objects *unless* we need to defragment.
  645. *
  646. * 1. Reorder the buffers, where objects with the most restrictive
  647. * placement requirements go first (ignoring fixed location buffers for
  648. * now). For example, objects needing the mappable aperture (the first
  649. * 256M of GTT), should go first vs objects that can be placed just
  650. * about anywhere. Repeat the previous pass.
  651. *
  652. * 2. Consider buffers that are pinned at a fixed location. Also try to
  653. * evict the entire VM this time, leaving only objects that we were
  654. * unable to lock. Try again to bind the buffers. (still using the new
  655. * buffer order).
  656. *
  657. * 3. We likely have object lock contention for one or more stubborn
  658. * objects in the VM, for which we need to evict to make forward
  659. * progress (perhaps we are fighting the shrinker?). When evicting the
  660. * VM this time around, anything that we can't lock we now track using
  661. * the busy_bo, using the full lock (after dropping the vm->mutex to
  662. * prevent deadlocks), instead of trylock. We then continue to evict the
  663. * VM, this time with the stubborn object locked, which we can now
  664. * hopefully unbind (if still bound in the VM). Repeat until the VM is
  665. * evicted. Finally we should be able bind everything.
  666. */
  667. for (pass = 0; pass <= 3; pass++) {
  668. int pin_flags = PIN_USER | PIN_VALIDATE;
  669. if (pass == 0)
  670. pin_flags |= PIN_NONBLOCK;
  671. if (pass >= 1)
  672. eb_unbind(eb, pass >= 2);
  673. if (pass == 2) {
  674. err = mutex_lock_interruptible(&eb->context->vm->mutex);
  675. if (!err) {
  676. err = i915_gem_evict_vm(eb->context->vm, &eb->ww, NULL);
  677. mutex_unlock(&eb->context->vm->mutex);
  678. }
  679. if (err)
  680. return err;
  681. }
  682. if (pass == 3) {
  683. retry:
  684. err = mutex_lock_interruptible(&eb->context->vm->mutex);
  685. if (!err) {
  686. struct drm_i915_gem_object *busy_bo = NULL;
  687. err = i915_gem_evict_vm(eb->context->vm, &eb->ww, &busy_bo);
  688. mutex_unlock(&eb->context->vm->mutex);
  689. if (err && busy_bo) {
  690. err = i915_gem_object_lock(busy_bo, &eb->ww);
  691. i915_gem_object_put(busy_bo);
  692. if (!err)
  693. goto retry;
  694. }
  695. }
  696. if (err)
  697. return err;
  698. }
  699. list_for_each_entry(ev, &eb->unbound, bind_link) {
  700. err = eb_reserve_vma(eb, ev, pin_flags);
  701. if (err)
  702. break;
  703. }
  704. if (err != -ENOSPC)
  705. break;
  706. }
  707. return err;
  708. }
  709. static int eb_select_context(struct i915_execbuffer *eb)
  710. {
  711. struct i915_gem_context *ctx;
  712. ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
  713. if (IS_ERR(ctx))
  714. return PTR_ERR(ctx);
  715. eb->gem_context = ctx;
  716. if (i915_gem_context_has_full_ppgtt(ctx))
  717. eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  718. return 0;
  719. }
  720. static int __eb_add_lut(struct i915_execbuffer *eb,
  721. u32 handle, struct i915_vma *vma)
  722. {
  723. struct i915_gem_context *ctx = eb->gem_context;
  724. struct i915_lut_handle *lut;
  725. int err;
  726. lut = i915_lut_handle_alloc();
  727. if (unlikely(!lut))
  728. return -ENOMEM;
  729. i915_vma_get(vma);
  730. if (!atomic_fetch_inc(&vma->open_count))
  731. i915_vma_reopen(vma);
  732. lut->handle = handle;
  733. lut->ctx = ctx;
  734. /* Check that the context hasn't been closed in the meantime */
  735. err = -EINTR;
  736. if (!mutex_lock_interruptible(&ctx->lut_mutex)) {
  737. if (likely(!i915_gem_context_is_closed(ctx)))
  738. err = radix_tree_insert(&ctx->handles_vma, handle, vma);
  739. else
  740. err = -ENOENT;
  741. if (err == 0) { /* And nor has this handle */
  742. struct drm_i915_gem_object *obj = vma->obj;
  743. spin_lock(&obj->lut_lock);
  744. if (idr_find(&eb->file->object_idr, handle) == obj) {
  745. list_add(&lut->obj_link, &obj->lut_list);
  746. } else {
  747. radix_tree_delete(&ctx->handles_vma, handle);
  748. err = -ENOENT;
  749. }
  750. spin_unlock(&obj->lut_lock);
  751. }
  752. mutex_unlock(&ctx->lut_mutex);
  753. }
  754. if (unlikely(err))
  755. goto err;
  756. return 0;
  757. err:
  758. i915_vma_close(vma);
  759. i915_vma_put(vma);
  760. i915_lut_handle_free(lut);
  761. return err;
  762. }
  763. static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle)
  764. {
  765. struct i915_address_space *vm = eb->context->vm;
  766. do {
  767. struct drm_i915_gem_object *obj;
  768. struct i915_vma *vma;
  769. int err;
  770. rcu_read_lock();
  771. vma = radix_tree_lookup(&eb->gem_context->handles_vma, handle);
  772. if (likely(vma && vma->vm == vm))
  773. vma = i915_vma_tryget(vma);
  774. rcu_read_unlock();
  775. if (likely(vma))
  776. return vma;
  777. obj = i915_gem_object_lookup(eb->file, handle);
  778. if (unlikely(!obj))
  779. return ERR_PTR(-ENOENT);
  780. /*
  781. * If the user has opted-in for protected-object tracking, make
  782. * sure the object encryption can be used.
  783. * We only need to do this when the object is first used with
  784. * this context, because the context itself will be banned when
  785. * the protected objects become invalid.
  786. */
  787. if (i915_gem_context_uses_protected_content(eb->gem_context) &&
  788. i915_gem_object_is_protected(obj)) {
  789. err = intel_pxp_key_check(eb->i915->pxp, obj, true);
  790. if (err) {
  791. i915_gem_object_put(obj);
  792. return ERR_PTR(err);
  793. }
  794. }
  795. vma = i915_vma_instance(obj, vm, NULL);
  796. if (IS_ERR(vma)) {
  797. i915_gem_object_put(obj);
  798. return vma;
  799. }
  800. err = __eb_add_lut(eb, handle, vma);
  801. if (likely(!err))
  802. return vma;
  803. i915_gem_object_put(obj);
  804. if (err != -EEXIST)
  805. return ERR_PTR(err);
  806. } while (1);
  807. }
  808. static int eb_lookup_vmas(struct i915_execbuffer *eb)
  809. {
  810. unsigned int i, current_batch = 0;
  811. int err = 0;
  812. INIT_LIST_HEAD(&eb->relocs);
  813. for (i = 0; i < eb->buffer_count; i++) {
  814. struct i915_vma *vma;
  815. vma = eb_lookup_vma(eb, eb->exec[i].handle);
  816. if (IS_ERR(vma)) {
  817. err = PTR_ERR(vma);
  818. goto err;
  819. }
  820. err = eb_validate_vma(eb, &eb->exec[i], vma);
  821. if (unlikely(err)) {
  822. i915_vma_put(vma);
  823. goto err;
  824. }
  825. err = eb_add_vma(eb, &current_batch, i, vma);
  826. if (err)
  827. return err;
  828. if (i915_gem_object_is_userptr(vma->obj)) {
  829. err = i915_gem_object_userptr_submit_init(vma->obj);
  830. if (err) {
  831. if (i + 1 < eb->buffer_count) {
  832. /*
  833. * Execbuffer code expects last vma entry to be NULL,
  834. * since we already initialized this entry,
  835. * set the next value to NULL or we mess up
  836. * cleanup handling.
  837. */
  838. eb->vma[i + 1].vma = NULL;
  839. }
  840. return err;
  841. }
  842. eb->vma[i].flags |= __EXEC_OBJECT_USERPTR_INIT;
  843. eb->args->flags |= __EXEC_USERPTR_USED;
  844. }
  845. }
  846. return 0;
  847. err:
  848. eb->vma[i].vma = NULL;
  849. return err;
  850. }
  851. static int eb_lock_vmas(struct i915_execbuffer *eb)
  852. {
  853. unsigned int i;
  854. int err;
  855. for (i = 0; i < eb->buffer_count; i++) {
  856. struct eb_vma *ev = &eb->vma[i];
  857. struct i915_vma *vma = ev->vma;
  858. err = i915_gem_object_lock(vma->obj, &eb->ww);
  859. if (err)
  860. return err;
  861. }
  862. return 0;
  863. }
  864. static int eb_validate_vmas(struct i915_execbuffer *eb)
  865. {
  866. unsigned int i;
  867. int err;
  868. INIT_LIST_HEAD(&eb->unbound);
  869. err = eb_lock_vmas(eb);
  870. if (err)
  871. return err;
  872. for (i = 0; i < eb->buffer_count; i++) {
  873. struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
  874. struct eb_vma *ev = &eb->vma[i];
  875. struct i915_vma *vma = ev->vma;
  876. err = eb_pin_vma(eb, entry, ev);
  877. if (err == -EDEADLK)
  878. return err;
  879. if (!err) {
  880. if (entry->offset != i915_vma_offset(vma)) {
  881. entry->offset = i915_vma_offset(vma) | UPDATE;
  882. eb->args->flags |= __EXEC_HAS_RELOC;
  883. }
  884. } else {
  885. eb_unreserve_vma(ev);
  886. list_add_tail(&ev->bind_link, &eb->unbound);
  887. if (drm_mm_node_allocated(&vma->node)) {
  888. err = i915_vma_unbind(vma);
  889. if (err)
  890. return err;
  891. }
  892. }
  893. /* Reserve enough slots to accommodate composite fences */
  894. err = dma_resv_reserve_fences(vma->obj->base.resv, eb->num_batches);
  895. if (err)
  896. return err;
  897. GEM_BUG_ON(drm_mm_node_allocated(&vma->node) &&
  898. eb_vma_misplaced(&eb->exec[i], vma, ev->flags));
  899. }
  900. if (!list_empty(&eb->unbound))
  901. return eb_reserve(eb);
  902. return 0;
  903. }
  904. static struct eb_vma *
  905. eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
  906. {
  907. if (eb->lut_size < 0) {
  908. if (handle >= -eb->lut_size)
  909. return NULL;
  910. return &eb->vma[handle];
  911. } else {
  912. struct hlist_head *head;
  913. struct eb_vma *ev;
  914. head = &eb->buckets[hash_32(handle, eb->lut_size)];
  915. hlist_for_each_entry(ev, head, node) {
  916. if (ev->handle == handle)
  917. return ev;
  918. }
  919. return NULL;
  920. }
  921. }
  922. static void eb_release_vmas(struct i915_execbuffer *eb, bool final)
  923. {
  924. const unsigned int count = eb->buffer_count;
  925. unsigned int i;
  926. for (i = 0; i < count; i++) {
  927. struct eb_vma *ev = &eb->vma[i];
  928. struct i915_vma *vma = ev->vma;
  929. if (!vma)
  930. break;
  931. eb_unreserve_vma(ev);
  932. if (final)
  933. i915_vma_put(vma);
  934. }
  935. eb_capture_release(eb);
  936. eb_unpin_engine(eb);
  937. }
  938. static void eb_destroy(const struct i915_execbuffer *eb)
  939. {
  940. if (eb->lut_size > 0)
  941. kfree(eb->buckets);
  942. }
  943. static u64
  944. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  945. const struct i915_vma *target)
  946. {
  947. return gen8_canonical_addr((int)reloc->delta + i915_vma_offset(target));
  948. }
  949. static void reloc_cache_init(struct reloc_cache *cache,
  950. struct drm_i915_private *i915)
  951. {
  952. cache->page = -1;
  953. cache->vaddr = 0;
  954. /* Must be a variable in the struct to allow GCC to unroll. */
  955. cache->graphics_ver = GRAPHICS_VER(i915);
  956. cache->has_llc = HAS_LLC(i915);
  957. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  958. cache->has_fence = cache->graphics_ver < 4;
  959. cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
  960. cache->node.flags = 0;
  961. }
  962. static void *unmask_page(unsigned long p)
  963. {
  964. return (void *)(uintptr_t)(p & PAGE_MASK);
  965. }
  966. static unsigned int unmask_flags(unsigned long p)
  967. {
  968. return p & ~PAGE_MASK;
  969. }
  970. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  971. static struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
  972. {
  973. struct drm_i915_private *i915 =
  974. container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
  975. return to_gt(i915)->ggtt;
  976. }
  977. static void reloc_cache_unmap(struct reloc_cache *cache)
  978. {
  979. void *vaddr;
  980. if (!cache->vaddr)
  981. return;
  982. vaddr = unmask_page(cache->vaddr);
  983. if (cache->vaddr & KMAP)
  984. kunmap_local(vaddr);
  985. else
  986. io_mapping_unmap_atomic((void __iomem *)vaddr);
  987. }
  988. static void reloc_cache_remap(struct reloc_cache *cache,
  989. struct drm_i915_gem_object *obj)
  990. {
  991. void *vaddr;
  992. if (!cache->vaddr)
  993. return;
  994. if (cache->vaddr & KMAP) {
  995. struct page *page = i915_gem_object_get_page(obj, cache->page);
  996. vaddr = kmap_local_page(page);
  997. cache->vaddr = unmask_flags(cache->vaddr) |
  998. (unsigned long)vaddr;
  999. } else {
  1000. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  1001. unsigned long offset;
  1002. offset = cache->node.start;
  1003. if (!drm_mm_node_allocated(&cache->node))
  1004. offset += cache->page << PAGE_SHIFT;
  1005. cache->vaddr = (unsigned long)
  1006. io_mapping_map_atomic_wc(&ggtt->iomap, offset);
  1007. }
  1008. }
  1009. static void reloc_cache_reset(struct reloc_cache *cache, struct i915_execbuffer *eb)
  1010. {
  1011. void *vaddr;
  1012. if (!cache->vaddr)
  1013. return;
  1014. vaddr = unmask_page(cache->vaddr);
  1015. if (cache->vaddr & KMAP) {
  1016. struct drm_i915_gem_object *obj =
  1017. (struct drm_i915_gem_object *)cache->node.mm;
  1018. if (cache->vaddr & CLFLUSH_AFTER)
  1019. mb();
  1020. kunmap_local(vaddr);
  1021. i915_gem_object_finish_access(obj);
  1022. } else {
  1023. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  1024. intel_gt_flush_ggtt_writes(ggtt->vm.gt);
  1025. io_mapping_unmap_atomic((void __iomem *)vaddr);
  1026. if (drm_mm_node_allocated(&cache->node)) {
  1027. ggtt->vm.clear_range(&ggtt->vm,
  1028. cache->node.start,
  1029. cache->node.size);
  1030. mutex_lock(&ggtt->vm.mutex);
  1031. drm_mm_remove_node(&cache->node);
  1032. mutex_unlock(&ggtt->vm.mutex);
  1033. } else {
  1034. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  1035. }
  1036. }
  1037. cache->vaddr = 0;
  1038. cache->page = -1;
  1039. }
  1040. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  1041. struct reloc_cache *cache,
  1042. unsigned long pageno)
  1043. {
  1044. void *vaddr;
  1045. struct page *page;
  1046. if (cache->vaddr) {
  1047. kunmap_local(unmask_page(cache->vaddr));
  1048. } else {
  1049. unsigned int flushes;
  1050. int err;
  1051. err = i915_gem_object_prepare_write(obj, &flushes);
  1052. if (err)
  1053. return ERR_PTR(err);
  1054. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  1055. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  1056. cache->vaddr = flushes | KMAP;
  1057. cache->node.mm = (void *)obj;
  1058. if (flushes)
  1059. mb();
  1060. }
  1061. page = i915_gem_object_get_page(obj, pageno);
  1062. if (!obj->mm.dirty)
  1063. set_page_dirty(page);
  1064. vaddr = kmap_local_page(page);
  1065. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  1066. cache->page = pageno;
  1067. return vaddr;
  1068. }
  1069. static void *reloc_iomap(struct i915_vma *batch,
  1070. struct i915_execbuffer *eb,
  1071. unsigned long page)
  1072. {
  1073. struct drm_i915_gem_object *obj = batch->obj;
  1074. struct reloc_cache *cache = &eb->reloc_cache;
  1075. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  1076. unsigned long offset;
  1077. void *vaddr;
  1078. if (cache->vaddr) {
  1079. intel_gt_flush_ggtt_writes(ggtt->vm.gt);
  1080. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  1081. } else {
  1082. struct i915_vma *vma = ERR_PTR(-ENODEV);
  1083. int err;
  1084. if (i915_gem_object_is_tiled(obj))
  1085. return ERR_PTR(-EINVAL);
  1086. if (use_cpu_reloc(cache, obj))
  1087. return NULL;
  1088. err = i915_gem_object_set_to_gtt_domain(obj, true);
  1089. if (err)
  1090. return ERR_PTR(err);
  1091. /*
  1092. * i915_gem_object_ggtt_pin_ww may attempt to remove the batch
  1093. * VMA from the object list because we no longer pin.
  1094. *
  1095. * Only attempt to pin the batch buffer to ggtt if the current batch
  1096. * is not inside ggtt, or the batch buffer is not misplaced.
  1097. */
  1098. if (!i915_is_ggtt(batch->vm) ||
  1099. !i915_vma_misplaced(batch, 0, 0, PIN_MAPPABLE)) {
  1100. vma = i915_gem_object_ggtt_pin_ww(obj, &eb->ww, NULL, 0, 0,
  1101. PIN_MAPPABLE |
  1102. PIN_NONBLOCK /* NOWARN */ |
  1103. PIN_NOEVICT);
  1104. }
  1105. if (vma == ERR_PTR(-EDEADLK))
  1106. return vma;
  1107. if (IS_ERR(vma)) {
  1108. memset(&cache->node, 0, sizeof(cache->node));
  1109. mutex_lock(&ggtt->vm.mutex);
  1110. err = drm_mm_insert_node_in_range
  1111. (&ggtt->vm.mm, &cache->node,
  1112. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  1113. 0, ggtt->mappable_end,
  1114. DRM_MM_INSERT_LOW);
  1115. mutex_unlock(&ggtt->vm.mutex);
  1116. if (err) /* no inactive aperture space, use cpu reloc */
  1117. return NULL;
  1118. } else {
  1119. cache->node.start = i915_ggtt_offset(vma);
  1120. cache->node.mm = (void *)vma;
  1121. }
  1122. }
  1123. offset = cache->node.start;
  1124. if (drm_mm_node_allocated(&cache->node)) {
  1125. ggtt->vm.insert_page(&ggtt->vm,
  1126. i915_gem_object_get_dma_address(obj, page),
  1127. offset,
  1128. i915_gem_get_pat_index(ggtt->vm.i915,
  1129. I915_CACHE_NONE),
  1130. 0);
  1131. } else {
  1132. offset += page << PAGE_SHIFT;
  1133. }
  1134. vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
  1135. offset);
  1136. cache->page = page;
  1137. cache->vaddr = (unsigned long)vaddr;
  1138. return vaddr;
  1139. }
  1140. static void *reloc_vaddr(struct i915_vma *vma,
  1141. struct i915_execbuffer *eb,
  1142. unsigned long page)
  1143. {
  1144. struct reloc_cache *cache = &eb->reloc_cache;
  1145. void *vaddr;
  1146. if (cache->page == page) {
  1147. vaddr = unmask_page(cache->vaddr);
  1148. } else {
  1149. vaddr = NULL;
  1150. if ((cache->vaddr & KMAP) == 0)
  1151. vaddr = reloc_iomap(vma, eb, page);
  1152. if (!vaddr)
  1153. vaddr = reloc_kmap(vma->obj, cache, page);
  1154. }
  1155. return vaddr;
  1156. }
  1157. static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  1158. {
  1159. if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
  1160. if (flushes & CLFLUSH_BEFORE)
  1161. drm_clflush_virt_range(addr, sizeof(*addr));
  1162. *addr = value;
  1163. /*
  1164. * Writes to the same cacheline are serialised by the CPU
  1165. * (including clflush). On the write path, we only require
  1166. * that it hits memory in an orderly fashion and place
  1167. * mb barriers at the start and end of the relocation phase
  1168. * to ensure ordering of clflush wrt to the system.
  1169. */
  1170. if (flushes & CLFLUSH_AFTER)
  1171. drm_clflush_virt_range(addr, sizeof(*addr));
  1172. } else
  1173. *addr = value;
  1174. }
  1175. static u64
  1176. relocate_entry(struct i915_vma *vma,
  1177. const struct drm_i915_gem_relocation_entry *reloc,
  1178. struct i915_execbuffer *eb,
  1179. const struct i915_vma *target)
  1180. {
  1181. u64 target_addr = relocation_target(reloc, target);
  1182. u64 offset = reloc->offset;
  1183. bool wide = eb->reloc_cache.use_64bit_reloc;
  1184. void *vaddr;
  1185. repeat:
  1186. vaddr = reloc_vaddr(vma, eb,
  1187. offset >> PAGE_SHIFT);
  1188. if (IS_ERR(vaddr))
  1189. return PTR_ERR(vaddr);
  1190. GEM_BUG_ON(!IS_ALIGNED(offset, sizeof(u32)));
  1191. clflush_write32(vaddr + offset_in_page(offset),
  1192. lower_32_bits(target_addr),
  1193. eb->reloc_cache.vaddr);
  1194. if (wide) {
  1195. offset += sizeof(u32);
  1196. target_addr >>= 32;
  1197. wide = false;
  1198. goto repeat;
  1199. }
  1200. return target->node.start | UPDATE;
  1201. }
  1202. static u64
  1203. eb_relocate_entry(struct i915_execbuffer *eb,
  1204. struct eb_vma *ev,
  1205. const struct drm_i915_gem_relocation_entry *reloc)
  1206. {
  1207. struct drm_i915_private *i915 = eb->i915;
  1208. struct eb_vma *target;
  1209. int err;
  1210. /* we've already hold a reference to all valid objects */
  1211. target = eb_get_vma(eb, reloc->target_handle);
  1212. if (unlikely(!target))
  1213. return -ENOENT;
  1214. /* Validate that the target is in a valid r/w GPU domain */
  1215. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  1216. drm_dbg(&i915->drm, "reloc with multiple write domains: "
  1217. "target %d offset %d "
  1218. "read %08x write %08x\n",
  1219. reloc->target_handle,
  1220. (int) reloc->offset,
  1221. reloc->read_domains,
  1222. reloc->write_domain);
  1223. return -EINVAL;
  1224. }
  1225. if (unlikely((reloc->write_domain | reloc->read_domains)
  1226. & ~I915_GEM_GPU_DOMAINS)) {
  1227. drm_dbg(&i915->drm, "reloc with read/write non-GPU domains: "
  1228. "target %d offset %d "
  1229. "read %08x write %08x\n",
  1230. reloc->target_handle,
  1231. (int) reloc->offset,
  1232. reloc->read_domains,
  1233. reloc->write_domain);
  1234. return -EINVAL;
  1235. }
  1236. if (reloc->write_domain) {
  1237. target->flags |= EXEC_OBJECT_WRITE;
  1238. /*
  1239. * Sandybridge PPGTT errata: We need a global gtt mapping
  1240. * for MI and pipe_control writes because the gpu doesn't
  1241. * properly redirect them through the ppgtt for non_secure
  1242. * batchbuffers.
  1243. */
  1244. if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  1245. GRAPHICS_VER(eb->i915) == 6 &&
  1246. !i915_vma_is_bound(target->vma, I915_VMA_GLOBAL_BIND)) {
  1247. struct i915_vma *vma = target->vma;
  1248. reloc_cache_unmap(&eb->reloc_cache);
  1249. mutex_lock(&vma->vm->mutex);
  1250. err = i915_vma_bind(target->vma,
  1251. target->vma->obj->pat_index,
  1252. PIN_GLOBAL, NULL, NULL);
  1253. mutex_unlock(&vma->vm->mutex);
  1254. reloc_cache_remap(&eb->reloc_cache, ev->vma->obj);
  1255. if (err)
  1256. return err;
  1257. }
  1258. }
  1259. /*
  1260. * If the relocation already has the right value in it, no
  1261. * more work needs to be done.
  1262. */
  1263. if (!DBG_FORCE_RELOC &&
  1264. gen8_canonical_addr(i915_vma_offset(target->vma)) == reloc->presumed_offset)
  1265. return 0;
  1266. /* Check that the relocation address is valid... */
  1267. if (unlikely(reloc->offset >
  1268. ev->vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
  1269. drm_dbg(&i915->drm, "Relocation beyond object bounds: "
  1270. "target %d offset %d size %d.\n",
  1271. reloc->target_handle,
  1272. (int)reloc->offset,
  1273. (int)ev->vma->size);
  1274. return -EINVAL;
  1275. }
  1276. if (unlikely(reloc->offset & 3)) {
  1277. drm_dbg(&i915->drm, "Relocation not 4-byte aligned: "
  1278. "target %d offset %d.\n",
  1279. reloc->target_handle,
  1280. (int)reloc->offset);
  1281. return -EINVAL;
  1282. }
  1283. /*
  1284. * If we write into the object, we need to force the synchronisation
  1285. * barrier, either with an asynchronous clflush or if we executed the
  1286. * patching using the GPU (though that should be serialised by the
  1287. * timeline). To be completely sure, and since we are required to
  1288. * do relocations we are already stalling, disable the user's opt
  1289. * out of our synchronisation.
  1290. */
  1291. ev->flags &= ~EXEC_OBJECT_ASYNC;
  1292. /* and update the user's relocation entry */
  1293. return relocate_entry(ev->vma, reloc, eb, target->vma);
  1294. }
  1295. static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev)
  1296. {
  1297. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  1298. struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
  1299. const struct drm_i915_gem_exec_object2 *entry = ev->exec;
  1300. struct drm_i915_gem_relocation_entry __user *urelocs =
  1301. u64_to_user_ptr(entry->relocs_ptr);
  1302. unsigned long remain = entry->relocation_count;
  1303. if (unlikely(remain > N_RELOC(INT_MAX)))
  1304. return -EINVAL;
  1305. /*
  1306. * We must check that the entire relocation array is safe
  1307. * to read. However, if the array is not writable the user loses
  1308. * the updated relocation values.
  1309. */
  1310. if (unlikely(!access_ok(urelocs, remain * sizeof(*urelocs))))
  1311. return -EFAULT;
  1312. do {
  1313. struct drm_i915_gem_relocation_entry *r = stack;
  1314. unsigned int count =
  1315. min_t(unsigned long, remain, ARRAY_SIZE(stack));
  1316. unsigned int copied;
  1317. /*
  1318. * This is the fast path and we cannot handle a pagefault
  1319. * whilst holding the struct mutex lest the user pass in the
  1320. * relocations contained within a mmaped bo. For in such a case
  1321. * we, the page fault handler would call i915_gem_fault() and
  1322. * we would try to acquire the struct mutex again. Obviously
  1323. * this is bad and so lockdep complains vehemently.
  1324. */
  1325. pagefault_disable();
  1326. copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
  1327. pagefault_enable();
  1328. if (unlikely(copied)) {
  1329. remain = -EFAULT;
  1330. goto out;
  1331. }
  1332. remain -= count;
  1333. do {
  1334. u64 offset = eb_relocate_entry(eb, ev, r);
  1335. if (likely(offset == 0)) {
  1336. } else if ((s64)offset < 0) {
  1337. remain = (int)offset;
  1338. goto out;
  1339. } else {
  1340. /*
  1341. * Note that reporting an error now
  1342. * leaves everything in an inconsistent
  1343. * state as we have *already* changed
  1344. * the relocation value inside the
  1345. * object. As we have not changed the
  1346. * reloc.presumed_offset or will not
  1347. * change the execobject.offset, on the
  1348. * call we may not rewrite the value
  1349. * inside the object, leaving it
  1350. * dangling and causing a GPU hang. Unless
  1351. * userspace dynamically rebuilds the
  1352. * relocations on each execbuf rather than
  1353. * presume a static tree.
  1354. *
  1355. * We did previously check if the relocations
  1356. * were writable (access_ok), an error now
  1357. * would be a strange race with mprotect,
  1358. * having already demonstrated that we
  1359. * can read from this userspace address.
  1360. */
  1361. offset = gen8_canonical_addr(offset & ~UPDATE);
  1362. __put_user(offset,
  1363. &urelocs[r - stack].presumed_offset);
  1364. }
  1365. } while (r++, --count);
  1366. urelocs += ARRAY_SIZE(stack);
  1367. } while (remain);
  1368. out:
  1369. reloc_cache_reset(&eb->reloc_cache, eb);
  1370. return remain;
  1371. }
  1372. static int
  1373. eb_relocate_vma_slow(struct i915_execbuffer *eb, struct eb_vma *ev)
  1374. {
  1375. const struct drm_i915_gem_exec_object2 *entry = ev->exec;
  1376. struct drm_i915_gem_relocation_entry *relocs =
  1377. u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1378. unsigned int i;
  1379. int err;
  1380. for (i = 0; i < entry->relocation_count; i++) {
  1381. u64 offset = eb_relocate_entry(eb, ev, &relocs[i]);
  1382. if ((s64)offset < 0) {
  1383. err = (int)offset;
  1384. goto err;
  1385. }
  1386. }
  1387. err = 0;
  1388. err:
  1389. reloc_cache_reset(&eb->reloc_cache, eb);
  1390. return err;
  1391. }
  1392. static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
  1393. {
  1394. const char __user *addr, *end;
  1395. unsigned long size;
  1396. char __maybe_unused c;
  1397. size = entry->relocation_count;
  1398. if (size == 0)
  1399. return 0;
  1400. if (size > N_RELOC(INT_MAX))
  1401. return -EINVAL;
  1402. addr = u64_to_user_ptr(entry->relocs_ptr);
  1403. size *= sizeof(struct drm_i915_gem_relocation_entry);
  1404. if (!access_ok(addr, size))
  1405. return -EFAULT;
  1406. end = addr + size;
  1407. for (; addr < end; addr += PAGE_SIZE) {
  1408. int err = __get_user(c, addr);
  1409. if (err)
  1410. return err;
  1411. }
  1412. return __get_user(c, end - 1);
  1413. }
  1414. static int eb_copy_relocations(const struct i915_execbuffer *eb)
  1415. {
  1416. struct drm_i915_gem_relocation_entry *relocs;
  1417. const unsigned int count = eb->buffer_count;
  1418. unsigned int i;
  1419. int err;
  1420. for (i = 0; i < count; i++) {
  1421. const unsigned int nreloc = eb->exec[i].relocation_count;
  1422. struct drm_i915_gem_relocation_entry __user *urelocs;
  1423. unsigned long size;
  1424. unsigned long copied;
  1425. if (nreloc == 0)
  1426. continue;
  1427. err = check_relocations(&eb->exec[i]);
  1428. if (err)
  1429. goto err;
  1430. urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
  1431. size = nreloc * sizeof(*relocs);
  1432. relocs = kvmalloc_array(1, size, GFP_KERNEL);
  1433. if (!relocs) {
  1434. err = -ENOMEM;
  1435. goto err;
  1436. }
  1437. /* copy_from_user is limited to < 4GiB */
  1438. copied = 0;
  1439. do {
  1440. unsigned int len =
  1441. min_t(u64, BIT_ULL(31), size - copied);
  1442. if (__copy_from_user((char *)relocs + copied,
  1443. (char __user *)urelocs + copied,
  1444. len))
  1445. goto end;
  1446. copied += len;
  1447. } while (copied < size);
  1448. /*
  1449. * As we do not update the known relocation offsets after
  1450. * relocating (due to the complexities in lock handling),
  1451. * we need to mark them as invalid now so that we force the
  1452. * relocation processing next time. Just in case the target
  1453. * object is evicted and then rebound into its old
  1454. * presumed_offset before the next execbuffer - if that
  1455. * happened we would make the mistake of assuming that the
  1456. * relocations were valid.
  1457. */
  1458. if (!user_access_begin(urelocs, size))
  1459. goto end;
  1460. for (copied = 0; copied < nreloc; copied++)
  1461. unsafe_put_user(-1,
  1462. &urelocs[copied].presumed_offset,
  1463. end_user);
  1464. user_access_end();
  1465. eb->exec[i].relocs_ptr = (uintptr_t)relocs;
  1466. }
  1467. return 0;
  1468. end_user:
  1469. user_access_end();
  1470. end:
  1471. kvfree(relocs);
  1472. err = -EFAULT;
  1473. err:
  1474. while (i--) {
  1475. relocs = u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
  1476. if (eb->exec[i].relocation_count)
  1477. kvfree(relocs);
  1478. }
  1479. return err;
  1480. }
  1481. static int eb_prefault_relocations(const struct i915_execbuffer *eb)
  1482. {
  1483. const unsigned int count = eb->buffer_count;
  1484. unsigned int i;
  1485. for (i = 0; i < count; i++) {
  1486. int err;
  1487. err = check_relocations(&eb->exec[i]);
  1488. if (err)
  1489. return err;
  1490. }
  1491. return 0;
  1492. }
  1493. static int eb_reinit_userptr(struct i915_execbuffer *eb)
  1494. {
  1495. const unsigned int count = eb->buffer_count;
  1496. unsigned int i;
  1497. int ret;
  1498. if (likely(!(eb->args->flags & __EXEC_USERPTR_USED)))
  1499. return 0;
  1500. for (i = 0; i < count; i++) {
  1501. struct eb_vma *ev = &eb->vma[i];
  1502. if (!i915_gem_object_is_userptr(ev->vma->obj))
  1503. continue;
  1504. ret = i915_gem_object_userptr_submit_init(ev->vma->obj);
  1505. if (ret)
  1506. return ret;
  1507. ev->flags |= __EXEC_OBJECT_USERPTR_INIT;
  1508. }
  1509. return 0;
  1510. }
  1511. static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb)
  1512. {
  1513. bool have_copy = false;
  1514. struct eb_vma *ev;
  1515. int err = 0;
  1516. repeat:
  1517. if (signal_pending(current)) {
  1518. err = -ERESTARTSYS;
  1519. goto out;
  1520. }
  1521. /* We may process another execbuffer during the unlock... */
  1522. eb_release_vmas(eb, false);
  1523. i915_gem_ww_ctx_fini(&eb->ww);
  1524. /*
  1525. * We take 3 passes through the slowpatch.
  1526. *
  1527. * 1 - we try to just prefault all the user relocation entries and
  1528. * then attempt to reuse the atomic pagefault disabled fast path again.
  1529. *
  1530. * 2 - we copy the user entries to a local buffer here outside of the
  1531. * local and allow ourselves to wait upon any rendering before
  1532. * relocations
  1533. *
  1534. * 3 - we already have a local copy of the relocation entries, but
  1535. * were interrupted (EAGAIN) whilst waiting for the objects, try again.
  1536. */
  1537. if (!err) {
  1538. err = eb_prefault_relocations(eb);
  1539. } else if (!have_copy) {
  1540. err = eb_copy_relocations(eb);
  1541. have_copy = err == 0;
  1542. } else {
  1543. cond_resched();
  1544. err = 0;
  1545. }
  1546. if (!err)
  1547. err = eb_reinit_userptr(eb);
  1548. i915_gem_ww_ctx_init(&eb->ww, true);
  1549. if (err)
  1550. goto out;
  1551. /* reacquire the objects */
  1552. repeat_validate:
  1553. err = eb_pin_engine(eb, false);
  1554. if (err)
  1555. goto err;
  1556. err = eb_validate_vmas(eb);
  1557. if (err)
  1558. goto err;
  1559. GEM_BUG_ON(!eb->batches[0]);
  1560. list_for_each_entry(ev, &eb->relocs, reloc_link) {
  1561. if (!have_copy) {
  1562. err = eb_relocate_vma(eb, ev);
  1563. if (err)
  1564. break;
  1565. } else {
  1566. err = eb_relocate_vma_slow(eb, ev);
  1567. if (err)
  1568. break;
  1569. }
  1570. }
  1571. if (err == -EDEADLK)
  1572. goto err;
  1573. if (err && !have_copy)
  1574. goto repeat;
  1575. if (err)
  1576. goto err;
  1577. /* as last step, parse the command buffer */
  1578. err = eb_parse(eb);
  1579. if (err)
  1580. goto err;
  1581. /*
  1582. * Leave the user relocations as are, this is the painfully slow path,
  1583. * and we want to avoid the complication of dropping the lock whilst
  1584. * having buffers reserved in the aperture and so causing spurious
  1585. * ENOSPC for random operations.
  1586. */
  1587. err:
  1588. if (err == -EDEADLK) {
  1589. eb_release_vmas(eb, false);
  1590. err = i915_gem_ww_ctx_backoff(&eb->ww);
  1591. if (!err)
  1592. goto repeat_validate;
  1593. }
  1594. if (err == -EAGAIN)
  1595. goto repeat;
  1596. out:
  1597. if (have_copy) {
  1598. const unsigned int count = eb->buffer_count;
  1599. unsigned int i;
  1600. for (i = 0; i < count; i++) {
  1601. const struct drm_i915_gem_exec_object2 *entry =
  1602. &eb->exec[i];
  1603. struct drm_i915_gem_relocation_entry *relocs;
  1604. if (!entry->relocation_count)
  1605. continue;
  1606. relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1607. kvfree(relocs);
  1608. }
  1609. }
  1610. return err;
  1611. }
  1612. static int eb_relocate_parse(struct i915_execbuffer *eb)
  1613. {
  1614. int err;
  1615. bool throttle = true;
  1616. retry:
  1617. err = eb_pin_engine(eb, throttle);
  1618. if (err) {
  1619. if (err != -EDEADLK)
  1620. return err;
  1621. goto err;
  1622. }
  1623. /* only throttle once, even if we didn't need to throttle */
  1624. throttle = false;
  1625. err = eb_validate_vmas(eb);
  1626. if (err == -EAGAIN)
  1627. goto slow;
  1628. else if (err)
  1629. goto err;
  1630. /* The objects are in their final locations, apply the relocations. */
  1631. if (eb->args->flags & __EXEC_HAS_RELOC) {
  1632. struct eb_vma *ev;
  1633. list_for_each_entry(ev, &eb->relocs, reloc_link) {
  1634. err = eb_relocate_vma(eb, ev);
  1635. if (err)
  1636. break;
  1637. }
  1638. if (err == -EDEADLK)
  1639. goto err;
  1640. else if (err)
  1641. goto slow;
  1642. }
  1643. if (!err)
  1644. err = eb_parse(eb);
  1645. err:
  1646. if (err == -EDEADLK) {
  1647. eb_release_vmas(eb, false);
  1648. err = i915_gem_ww_ctx_backoff(&eb->ww);
  1649. if (!err)
  1650. goto retry;
  1651. }
  1652. return err;
  1653. slow:
  1654. err = eb_relocate_parse_slow(eb);
  1655. if (err)
  1656. /*
  1657. * If the user expects the execobject.offset and
  1658. * reloc.presumed_offset to be an exact match,
  1659. * as for using NO_RELOC, then we cannot update
  1660. * the execobject.offset until we have completed
  1661. * relocation.
  1662. */
  1663. eb->args->flags &= ~__EXEC_HAS_RELOC;
  1664. return err;
  1665. }
  1666. /*
  1667. * Using two helper loops for the order of which requests / batches are created
  1668. * and added the to backend. Requests are created in order from the parent to
  1669. * the last child. Requests are added in the reverse order, from the last child
  1670. * to parent. This is done for locking reasons as the timeline lock is acquired
  1671. * during request creation and released when the request is added to the
  1672. * backend. To make lockdep happy (see intel_context_timeline_lock) this must be
  1673. * the ordering.
  1674. */
  1675. #define for_each_batch_create_order(_eb, _i) \
  1676. for ((_i) = 0; (_i) < (_eb)->num_batches; ++(_i))
  1677. #define for_each_batch_add_order(_eb, _i) \
  1678. BUILD_BUG_ON(!typecheck(int, _i)); \
  1679. for ((_i) = (_eb)->num_batches - 1; (_i) >= 0; --(_i))
  1680. static struct i915_request *
  1681. eb_find_first_request_added(struct i915_execbuffer *eb)
  1682. {
  1683. int i;
  1684. for_each_batch_add_order(eb, i)
  1685. if (eb->requests[i])
  1686. return eb->requests[i];
  1687. GEM_BUG_ON("Request not found");
  1688. return NULL;
  1689. }
  1690. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  1691. /* Stage with GFP_KERNEL allocations before we enter the signaling critical path */
  1692. static int eb_capture_stage(struct i915_execbuffer *eb)
  1693. {
  1694. const unsigned int count = eb->buffer_count;
  1695. unsigned int i = count, j;
  1696. while (i--) {
  1697. struct eb_vma *ev = &eb->vma[i];
  1698. struct i915_vma *vma = ev->vma;
  1699. unsigned int flags = ev->flags;
  1700. if (!(flags & EXEC_OBJECT_CAPTURE))
  1701. continue;
  1702. if (i915_gem_context_is_recoverable(eb->gem_context) &&
  1703. (IS_DGFX(eb->i915) || GRAPHICS_VER_FULL(eb->i915) > IP_VER(12, 0)))
  1704. return -EINVAL;
  1705. for_each_batch_create_order(eb, j) {
  1706. struct i915_capture_list *capture;
  1707. capture = kmalloc(sizeof(*capture), GFP_KERNEL);
  1708. if (!capture)
  1709. continue;
  1710. capture->next = eb->capture_lists[j];
  1711. capture->vma_res = i915_vma_resource_get(vma->resource);
  1712. eb->capture_lists[j] = capture;
  1713. }
  1714. }
  1715. return 0;
  1716. }
  1717. /* Commit once we're in the critical path */
  1718. static void eb_capture_commit(struct i915_execbuffer *eb)
  1719. {
  1720. unsigned int j;
  1721. for_each_batch_create_order(eb, j) {
  1722. struct i915_request *rq = eb->requests[j];
  1723. if (!rq)
  1724. break;
  1725. rq->capture_list = eb->capture_lists[j];
  1726. eb->capture_lists[j] = NULL;
  1727. }
  1728. }
  1729. /*
  1730. * Release anything that didn't get committed due to errors.
  1731. * The capture_list will otherwise be freed at request retire.
  1732. */
  1733. static void eb_capture_release(struct i915_execbuffer *eb)
  1734. {
  1735. unsigned int j;
  1736. for_each_batch_create_order(eb, j) {
  1737. if (eb->capture_lists[j]) {
  1738. i915_request_free_capture_list(eb->capture_lists[j]);
  1739. eb->capture_lists[j] = NULL;
  1740. }
  1741. }
  1742. }
  1743. static void eb_capture_list_clear(struct i915_execbuffer *eb)
  1744. {
  1745. memset(eb->capture_lists, 0, sizeof(eb->capture_lists));
  1746. }
  1747. #else
  1748. static int eb_capture_stage(struct i915_execbuffer *eb)
  1749. {
  1750. return 0;
  1751. }
  1752. static void eb_capture_commit(struct i915_execbuffer *eb)
  1753. {
  1754. }
  1755. static void eb_capture_release(struct i915_execbuffer *eb)
  1756. {
  1757. }
  1758. static void eb_capture_list_clear(struct i915_execbuffer *eb)
  1759. {
  1760. }
  1761. #endif
  1762. static int eb_move_to_gpu(struct i915_execbuffer *eb)
  1763. {
  1764. const unsigned int count = eb->buffer_count;
  1765. unsigned int i = count;
  1766. int err = 0, j;
  1767. while (i--) {
  1768. struct eb_vma *ev = &eb->vma[i];
  1769. struct i915_vma *vma = ev->vma;
  1770. unsigned int flags = ev->flags;
  1771. struct drm_i915_gem_object *obj = vma->obj;
  1772. assert_vma_held(vma);
  1773. /*
  1774. * If the GPU is not _reading_ through the CPU cache, we need
  1775. * to make sure that any writes (both previous GPU writes from
  1776. * before a change in snooping levels and normal CPU writes)
  1777. * caught in that cache are flushed to main memory.
  1778. *
  1779. * We want to say
  1780. * obj->cache_dirty &&
  1781. * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
  1782. * but gcc's optimiser doesn't handle that as well and emits
  1783. * two jumps instead of one. Maybe one day...
  1784. *
  1785. * FIXME: There is also sync flushing in set_pages(), which
  1786. * serves a different purpose(some of the time at least).
  1787. *
  1788. * We should consider:
  1789. *
  1790. * 1. Rip out the async flush code.
  1791. *
  1792. * 2. Or make the sync flushing use the async clflush path
  1793. * using mandatory fences underneath. Currently the below
  1794. * async flush happens after we bind the object.
  1795. */
  1796. if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
  1797. if (i915_gem_clflush_object(obj, 0))
  1798. flags &= ~EXEC_OBJECT_ASYNC;
  1799. }
  1800. /* We only need to await on the first request */
  1801. if (err == 0 && !(flags & EXEC_OBJECT_ASYNC)) {
  1802. err = i915_request_await_object
  1803. (eb_find_first_request_added(eb), obj,
  1804. flags & EXEC_OBJECT_WRITE);
  1805. }
  1806. for_each_batch_add_order(eb, j) {
  1807. if (err)
  1808. break;
  1809. if (!eb->requests[j])
  1810. continue;
  1811. err = _i915_vma_move_to_active(vma, eb->requests[j],
  1812. j ? NULL :
  1813. eb->composite_fence ?
  1814. eb->composite_fence :
  1815. &eb->requests[j]->fence,
  1816. flags | __EXEC_OBJECT_NO_RESERVE |
  1817. __EXEC_OBJECT_NO_REQUEST_AWAIT);
  1818. }
  1819. }
  1820. #ifdef CONFIG_MMU_NOTIFIER
  1821. if (!err && (eb->args->flags & __EXEC_USERPTR_USED)) {
  1822. for (i = 0; i < count; i++) {
  1823. struct eb_vma *ev = &eb->vma[i];
  1824. struct drm_i915_gem_object *obj = ev->vma->obj;
  1825. if (!i915_gem_object_is_userptr(obj))
  1826. continue;
  1827. err = i915_gem_object_userptr_submit_done(obj);
  1828. if (err)
  1829. break;
  1830. }
  1831. }
  1832. #endif
  1833. if (unlikely(err))
  1834. goto err_skip;
  1835. /* Unconditionally flush any chipset caches (for streaming writes). */
  1836. intel_gt_chipset_flush(eb->gt);
  1837. eb_capture_commit(eb);
  1838. return 0;
  1839. err_skip:
  1840. for_each_batch_create_order(eb, j) {
  1841. if (!eb->requests[j])
  1842. break;
  1843. i915_request_set_error_once(eb->requests[j], err);
  1844. }
  1845. return err;
  1846. }
  1847. static int i915_gem_check_execbuffer(struct drm_i915_private *i915,
  1848. struct drm_i915_gem_execbuffer2 *exec)
  1849. {
  1850. if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
  1851. return -EINVAL;
  1852. /* Kernel clipping was a DRI1 misfeature */
  1853. if (!(exec->flags & (I915_EXEC_FENCE_ARRAY |
  1854. I915_EXEC_USE_EXTENSIONS))) {
  1855. if (exec->num_cliprects || exec->cliprects_ptr)
  1856. return -EINVAL;
  1857. }
  1858. if (exec->DR4 == 0xffffffff) {
  1859. drm_dbg(&i915->drm, "UXA submitting garbage DR4, fixing up\n");
  1860. exec->DR4 = 0;
  1861. }
  1862. if (exec->DR1 || exec->DR4)
  1863. return -EINVAL;
  1864. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  1865. return -EINVAL;
  1866. return 0;
  1867. }
  1868. static int i915_reset_gen7_sol_offsets(struct i915_request *rq)
  1869. {
  1870. u32 *cs;
  1871. int i;
  1872. if (GRAPHICS_VER(rq->i915) != 7 || rq->engine->id != RCS0) {
  1873. drm_dbg(&rq->i915->drm, "sol reset is gen7/rcs only\n");
  1874. return -EINVAL;
  1875. }
  1876. cs = intel_ring_begin(rq, 4 * 2 + 2);
  1877. if (IS_ERR(cs))
  1878. return PTR_ERR(cs);
  1879. *cs++ = MI_LOAD_REGISTER_IMM(4);
  1880. for (i = 0; i < 4; i++) {
  1881. *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
  1882. *cs++ = 0;
  1883. }
  1884. *cs++ = MI_NOOP;
  1885. intel_ring_advance(rq, cs);
  1886. return 0;
  1887. }
  1888. static struct i915_vma *
  1889. shadow_batch_pin(struct i915_execbuffer *eb,
  1890. struct drm_i915_gem_object *obj,
  1891. struct i915_address_space *vm,
  1892. unsigned int flags)
  1893. {
  1894. struct i915_vma *vma;
  1895. int err;
  1896. vma = i915_vma_instance(obj, vm, NULL);
  1897. if (IS_ERR(vma))
  1898. return vma;
  1899. err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, flags | PIN_VALIDATE);
  1900. if (err)
  1901. return ERR_PTR(err);
  1902. return vma;
  1903. }
  1904. static struct i915_vma *eb_dispatch_secure(struct i915_execbuffer *eb, struct i915_vma *vma)
  1905. {
  1906. /*
  1907. * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1908. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1909. * hsw should have this fixed, but bdw mucks it up again. */
  1910. if (eb->batch_flags & I915_DISPATCH_SECURE)
  1911. return i915_gem_object_ggtt_pin_ww(vma->obj, &eb->ww, NULL, 0, 0, PIN_VALIDATE);
  1912. return NULL;
  1913. }
  1914. static int eb_parse(struct i915_execbuffer *eb)
  1915. {
  1916. struct drm_i915_private *i915 = eb->i915;
  1917. struct intel_gt_buffer_pool_node *pool = eb->batch_pool;
  1918. struct i915_vma *shadow, *trampoline, *batch;
  1919. unsigned long len;
  1920. int err;
  1921. if (!eb_use_cmdparser(eb)) {
  1922. batch = eb_dispatch_secure(eb, eb->batches[0]->vma);
  1923. if (IS_ERR(batch))
  1924. return PTR_ERR(batch);
  1925. goto secure_batch;
  1926. }
  1927. if (intel_context_is_parallel(eb->context))
  1928. return -EINVAL;
  1929. len = eb->batch_len[0];
  1930. if (!CMDPARSER_USES_GGTT(eb->i915)) {
  1931. /*
  1932. * ppGTT backed shadow buffers must be mapped RO, to prevent
  1933. * post-scan tampering
  1934. */
  1935. if (!eb->context->vm->has_read_only) {
  1936. drm_dbg(&i915->drm,
  1937. "Cannot prevent post-scan tampering without RO capable vm\n");
  1938. return -EINVAL;
  1939. }
  1940. } else {
  1941. len += I915_CMD_PARSER_TRAMPOLINE_SIZE;
  1942. }
  1943. if (unlikely(len < eb->batch_len[0])) /* last paranoid check of overflow */
  1944. return -EINVAL;
  1945. if (!pool) {
  1946. pool = intel_gt_get_buffer_pool(eb->gt, len,
  1947. I915_MAP_WB);
  1948. if (IS_ERR(pool))
  1949. return PTR_ERR(pool);
  1950. eb->batch_pool = pool;
  1951. }
  1952. err = i915_gem_object_lock(pool->obj, &eb->ww);
  1953. if (err)
  1954. return err;
  1955. shadow = shadow_batch_pin(eb, pool->obj, eb->context->vm, PIN_USER);
  1956. if (IS_ERR(shadow))
  1957. return PTR_ERR(shadow);
  1958. intel_gt_buffer_pool_mark_used(pool);
  1959. i915_gem_object_set_readonly(shadow->obj);
  1960. shadow->private = pool;
  1961. trampoline = NULL;
  1962. if (CMDPARSER_USES_GGTT(eb->i915)) {
  1963. trampoline = shadow;
  1964. shadow = shadow_batch_pin(eb, pool->obj,
  1965. &eb->gt->ggtt->vm,
  1966. PIN_GLOBAL);
  1967. if (IS_ERR(shadow))
  1968. return PTR_ERR(shadow);
  1969. shadow->private = pool;
  1970. eb->batch_flags |= I915_DISPATCH_SECURE;
  1971. }
  1972. batch = eb_dispatch_secure(eb, shadow);
  1973. if (IS_ERR(batch))
  1974. return PTR_ERR(batch);
  1975. err = dma_resv_reserve_fences(shadow->obj->base.resv, 1);
  1976. if (err)
  1977. return err;
  1978. err = intel_engine_cmd_parser(eb->context->engine,
  1979. eb->batches[0]->vma,
  1980. eb->batch_start_offset,
  1981. eb->batch_len[0],
  1982. shadow, trampoline);
  1983. if (err)
  1984. return err;
  1985. eb->batches[0] = &eb->vma[eb->buffer_count++];
  1986. eb->batches[0]->vma = i915_vma_get(shadow);
  1987. eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN;
  1988. eb->trampoline = trampoline;
  1989. eb->batch_start_offset = 0;
  1990. secure_batch:
  1991. if (batch) {
  1992. if (intel_context_is_parallel(eb->context))
  1993. return -EINVAL;
  1994. eb->batches[0] = &eb->vma[eb->buffer_count++];
  1995. eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN;
  1996. eb->batches[0]->vma = i915_vma_get(batch);
  1997. }
  1998. return 0;
  1999. }
  2000. static int eb_request_submit(struct i915_execbuffer *eb,
  2001. struct i915_request *rq,
  2002. struct i915_vma *batch,
  2003. u64 batch_len)
  2004. {
  2005. int err;
  2006. if (intel_context_nopreempt(rq->context))
  2007. __set_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags);
  2008. if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
  2009. err = i915_reset_gen7_sol_offsets(rq);
  2010. if (err)
  2011. return err;
  2012. }
  2013. /*
  2014. * After we completed waiting for other engines (using HW semaphores)
  2015. * then we can signal that this request/batch is ready to run. This
  2016. * allows us to determine if the batch is still waiting on the GPU
  2017. * or actually running by checking the breadcrumb.
  2018. */
  2019. if (rq->context->engine->emit_init_breadcrumb) {
  2020. err = rq->context->engine->emit_init_breadcrumb(rq);
  2021. if (err)
  2022. return err;
  2023. }
  2024. err = rq->context->engine->emit_bb_start(rq,
  2025. i915_vma_offset(batch) +
  2026. eb->batch_start_offset,
  2027. batch_len,
  2028. eb->batch_flags);
  2029. if (err)
  2030. return err;
  2031. if (eb->trampoline) {
  2032. GEM_BUG_ON(intel_context_is_parallel(rq->context));
  2033. GEM_BUG_ON(eb->batch_start_offset);
  2034. err = rq->context->engine->emit_bb_start(rq,
  2035. i915_vma_offset(eb->trampoline) +
  2036. batch_len, 0, 0);
  2037. if (err)
  2038. return err;
  2039. }
  2040. return 0;
  2041. }
  2042. static int eb_submit(struct i915_execbuffer *eb)
  2043. {
  2044. unsigned int i;
  2045. int err;
  2046. err = eb_move_to_gpu(eb);
  2047. for_each_batch_create_order(eb, i) {
  2048. if (!eb->requests[i])
  2049. break;
  2050. trace_i915_request_queue(eb->requests[i], eb->batch_flags);
  2051. if (!err)
  2052. err = eb_request_submit(eb, eb->requests[i],
  2053. eb->batches[i]->vma,
  2054. eb->batch_len[i]);
  2055. }
  2056. return err;
  2057. }
  2058. /*
  2059. * Find one BSD ring to dispatch the corresponding BSD command.
  2060. * The engine index is returned.
  2061. */
  2062. static unsigned int
  2063. gen8_dispatch_bsd_engine(struct drm_i915_private *i915,
  2064. struct drm_file *file)
  2065. {
  2066. struct drm_i915_file_private *file_priv = file->driver_priv;
  2067. /* Check whether the file_priv has already selected one ring. */
  2068. if ((int)file_priv->bsd_engine < 0)
  2069. file_priv->bsd_engine =
  2070. get_random_u32_below(i915->engine_uabi_class_count[I915_ENGINE_CLASS_VIDEO]);
  2071. return file_priv->bsd_engine;
  2072. }
  2073. static const enum intel_engine_id user_ring_map[] = {
  2074. [I915_EXEC_DEFAULT] = RCS0,
  2075. [I915_EXEC_RENDER] = RCS0,
  2076. [I915_EXEC_BLT] = BCS0,
  2077. [I915_EXEC_BSD] = VCS0,
  2078. [I915_EXEC_VEBOX] = VECS0
  2079. };
  2080. static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel_context *ce)
  2081. {
  2082. struct intel_ring *ring = ce->ring;
  2083. struct intel_timeline *tl = ce->timeline;
  2084. struct i915_request *rq;
  2085. /*
  2086. * Completely unscientific finger-in-the-air estimates for suitable
  2087. * maximum user request size (to avoid blocking) and then backoff.
  2088. */
  2089. if (intel_ring_update_space(ring) >= PAGE_SIZE)
  2090. return NULL;
  2091. /*
  2092. * Find a request that after waiting upon, there will be at least half
  2093. * the ring available. The hysteresis allows us to compete for the
  2094. * shared ring and should mean that we sleep less often prior to
  2095. * claiming our resources, but not so long that the ring completely
  2096. * drains before we can submit our next request.
  2097. */
  2098. list_for_each_entry(rq, &tl->requests, link) {
  2099. if (rq->ring != ring)
  2100. continue;
  2101. if (__intel_ring_space(rq->postfix,
  2102. ring->emit, ring->size) > ring->size / 2)
  2103. break;
  2104. }
  2105. if (&rq->link == &tl->requests)
  2106. return NULL; /* weird, we will check again later for real */
  2107. return i915_request_get(rq);
  2108. }
  2109. static int eb_pin_timeline(struct i915_execbuffer *eb, struct intel_context *ce,
  2110. bool throttle)
  2111. {
  2112. struct intel_timeline *tl;
  2113. struct i915_request *rq = NULL;
  2114. /*
  2115. * Take a local wakeref for preparing to dispatch the execbuf as
  2116. * we expect to access the hardware fairly frequently in the
  2117. * process, and require the engine to be kept awake between accesses.
  2118. * Upon dispatch, we acquire another prolonged wakeref that we hold
  2119. * until the timeline is idle, which in turn releases the wakeref
  2120. * taken on the engine, and the parent device.
  2121. */
  2122. tl = intel_context_timeline_lock(ce);
  2123. if (IS_ERR(tl))
  2124. return PTR_ERR(tl);
  2125. intel_context_enter(ce);
  2126. if (throttle)
  2127. rq = eb_throttle(eb, ce);
  2128. intel_context_timeline_unlock(tl);
  2129. if (rq) {
  2130. bool nonblock = eb->file->filp->f_flags & O_NONBLOCK;
  2131. long timeout = nonblock ? 0 : MAX_SCHEDULE_TIMEOUT;
  2132. if (i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE,
  2133. timeout) < 0) {
  2134. i915_request_put(rq);
  2135. /*
  2136. * Error path, cannot use intel_context_timeline_lock as
  2137. * that is user interruptable and this clean up step
  2138. * must be done.
  2139. */
  2140. mutex_lock(&ce->timeline->mutex);
  2141. intel_context_exit(ce);
  2142. mutex_unlock(&ce->timeline->mutex);
  2143. if (nonblock)
  2144. return -EWOULDBLOCK;
  2145. else
  2146. return -EINTR;
  2147. }
  2148. i915_request_put(rq);
  2149. }
  2150. return 0;
  2151. }
  2152. static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle)
  2153. {
  2154. struct intel_context *ce = eb->context, *child;
  2155. int err;
  2156. int i = 0, j = 0;
  2157. GEM_BUG_ON(eb->args->flags & __EXEC_ENGINE_PINNED);
  2158. if (unlikely(intel_context_is_banned(ce)))
  2159. return -EIO;
  2160. /*
  2161. * Pinning the contexts may generate requests in order to acquire
  2162. * GGTT space, so do this first before we reserve a seqno for
  2163. * ourselves.
  2164. */
  2165. err = intel_context_pin_ww(ce, &eb->ww);
  2166. if (err)
  2167. return err;
  2168. for_each_child(ce, child) {
  2169. err = intel_context_pin_ww(child, &eb->ww);
  2170. GEM_BUG_ON(err); /* perma-pinned should incr a counter */
  2171. }
  2172. for_each_child(ce, child) {
  2173. err = eb_pin_timeline(eb, child, throttle);
  2174. if (err)
  2175. goto unwind;
  2176. ++i;
  2177. }
  2178. err = eb_pin_timeline(eb, ce, throttle);
  2179. if (err)
  2180. goto unwind;
  2181. eb->args->flags |= __EXEC_ENGINE_PINNED;
  2182. return 0;
  2183. unwind:
  2184. for_each_child(ce, child) {
  2185. if (j++ < i) {
  2186. mutex_lock(&child->timeline->mutex);
  2187. intel_context_exit(child);
  2188. mutex_unlock(&child->timeline->mutex);
  2189. }
  2190. }
  2191. for_each_child(ce, child)
  2192. intel_context_unpin(child);
  2193. intel_context_unpin(ce);
  2194. return err;
  2195. }
  2196. static void eb_unpin_engine(struct i915_execbuffer *eb)
  2197. {
  2198. struct intel_context *ce = eb->context, *child;
  2199. if (!(eb->args->flags & __EXEC_ENGINE_PINNED))
  2200. return;
  2201. eb->args->flags &= ~__EXEC_ENGINE_PINNED;
  2202. for_each_child(ce, child) {
  2203. mutex_lock(&child->timeline->mutex);
  2204. intel_context_exit(child);
  2205. mutex_unlock(&child->timeline->mutex);
  2206. intel_context_unpin(child);
  2207. }
  2208. mutex_lock(&ce->timeline->mutex);
  2209. intel_context_exit(ce);
  2210. mutex_unlock(&ce->timeline->mutex);
  2211. intel_context_unpin(ce);
  2212. }
  2213. static unsigned int
  2214. eb_select_legacy_ring(struct i915_execbuffer *eb)
  2215. {
  2216. struct drm_i915_private *i915 = eb->i915;
  2217. struct drm_i915_gem_execbuffer2 *args = eb->args;
  2218. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  2219. if (user_ring_id != I915_EXEC_BSD &&
  2220. (args->flags & I915_EXEC_BSD_MASK)) {
  2221. drm_dbg(&i915->drm,
  2222. "execbuf with non bsd ring but with invalid "
  2223. "bsd dispatch flags: %d\n", (int)(args->flags));
  2224. return -1;
  2225. }
  2226. if (user_ring_id == I915_EXEC_BSD &&
  2227. i915->engine_uabi_class_count[I915_ENGINE_CLASS_VIDEO] > 1) {
  2228. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  2229. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  2230. bsd_idx = gen8_dispatch_bsd_engine(i915, eb->file);
  2231. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  2232. bsd_idx <= I915_EXEC_BSD_RING2) {
  2233. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  2234. bsd_idx--;
  2235. } else {
  2236. drm_dbg(&i915->drm,
  2237. "execbuf with unknown bsd ring: %u\n",
  2238. bsd_idx);
  2239. return -1;
  2240. }
  2241. return _VCS(bsd_idx);
  2242. }
  2243. if (user_ring_id >= ARRAY_SIZE(user_ring_map)) {
  2244. drm_dbg(&i915->drm, "execbuf with unknown ring: %u\n",
  2245. user_ring_id);
  2246. return -1;
  2247. }
  2248. return user_ring_map[user_ring_id];
  2249. }
  2250. static int
  2251. eb_select_engine(struct i915_execbuffer *eb)
  2252. {
  2253. struct intel_context *ce, *child;
  2254. struct intel_gt *gt;
  2255. unsigned int idx;
  2256. int err;
  2257. if (i915_gem_context_user_engines(eb->gem_context))
  2258. idx = eb->args->flags & I915_EXEC_RING_MASK;
  2259. else
  2260. idx = eb_select_legacy_ring(eb);
  2261. ce = i915_gem_context_get_engine(eb->gem_context, idx);
  2262. if (IS_ERR(ce))
  2263. return PTR_ERR(ce);
  2264. if (intel_context_is_parallel(ce)) {
  2265. if (eb->buffer_count < ce->parallel.number_children + 1) {
  2266. intel_context_put(ce);
  2267. return -EINVAL;
  2268. }
  2269. if (eb->batch_start_offset || eb->args->batch_len) {
  2270. intel_context_put(ce);
  2271. return -EINVAL;
  2272. }
  2273. }
  2274. eb->num_batches = ce->parallel.number_children + 1;
  2275. gt = ce->engine->gt;
  2276. for_each_child(ce, child)
  2277. intel_context_get(child);
  2278. eb->wakeref = intel_gt_pm_get(ce->engine->gt);
  2279. /*
  2280. * Keep GT0 active on MTL so that i915_vma_parked() doesn't
  2281. * free VMAs while execbuf ioctl is validating VMAs.
  2282. */
  2283. if (gt->info.id)
  2284. eb->wakeref_gt0 = intel_gt_pm_get(to_gt(gt->i915));
  2285. if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
  2286. err = intel_context_alloc_state(ce);
  2287. if (err)
  2288. goto err;
  2289. }
  2290. for_each_child(ce, child) {
  2291. if (!test_bit(CONTEXT_ALLOC_BIT, &child->flags)) {
  2292. err = intel_context_alloc_state(child);
  2293. if (err)
  2294. goto err;
  2295. }
  2296. }
  2297. /*
  2298. * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  2299. * EIO if the GPU is already wedged.
  2300. */
  2301. err = intel_gt_terminally_wedged(ce->engine->gt);
  2302. if (err)
  2303. goto err;
  2304. if (!i915_vm_tryget(ce->vm)) {
  2305. err = -ENOENT;
  2306. goto err;
  2307. }
  2308. eb->context = ce;
  2309. eb->gt = ce->engine->gt;
  2310. /*
  2311. * Make sure engine pool stays alive even if we call intel_context_put
  2312. * during ww handling. The pool is destroyed when last pm reference
  2313. * is dropped, which breaks our -EDEADLK handling.
  2314. */
  2315. return err;
  2316. err:
  2317. if (gt->info.id)
  2318. intel_gt_pm_put(to_gt(gt->i915), eb->wakeref_gt0);
  2319. intel_gt_pm_put(ce->engine->gt, eb->wakeref);
  2320. for_each_child(ce, child)
  2321. intel_context_put(child);
  2322. intel_context_put(ce);
  2323. return err;
  2324. }
  2325. static void
  2326. eb_put_engine(struct i915_execbuffer *eb)
  2327. {
  2328. struct intel_context *child;
  2329. i915_vm_put(eb->context->vm);
  2330. /*
  2331. * This works in conjunction with eb_select_engine() to prevent
  2332. * i915_vma_parked() from interfering while execbuf validates vmas.
  2333. */
  2334. if (eb->gt->info.id)
  2335. intel_gt_pm_put(to_gt(eb->gt->i915), eb->wakeref_gt0);
  2336. intel_gt_pm_put(eb->context->engine->gt, eb->wakeref);
  2337. for_each_child(eb->context, child)
  2338. intel_context_put(child);
  2339. intel_context_put(eb->context);
  2340. }
  2341. static void
  2342. __free_fence_array(struct eb_fence *fences, unsigned int n)
  2343. {
  2344. while (n--) {
  2345. drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2));
  2346. dma_fence_put(fences[n].dma_fence);
  2347. dma_fence_chain_free(fences[n].chain_fence);
  2348. }
  2349. kvfree(fences);
  2350. }
  2351. static int
  2352. add_timeline_fence_array(struct i915_execbuffer *eb,
  2353. const struct drm_i915_gem_execbuffer_ext_timeline_fences *timeline_fences)
  2354. {
  2355. struct drm_i915_gem_exec_fence __user *user_fences;
  2356. u64 __user *user_values;
  2357. struct eb_fence *f;
  2358. u64 nfences;
  2359. int err = 0;
  2360. nfences = timeline_fences->fence_count;
  2361. if (!nfences)
  2362. return 0;
  2363. /* Check multiplication overflow for access_ok() and kvmalloc_array() */
  2364. BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
  2365. if (nfences > min_t(unsigned long,
  2366. ULONG_MAX / sizeof(*user_fences),
  2367. SIZE_MAX / sizeof(*f)) - eb->num_fences)
  2368. return -EINVAL;
  2369. user_fences = u64_to_user_ptr(timeline_fences->handles_ptr);
  2370. if (!access_ok(user_fences, nfences * sizeof(*user_fences)))
  2371. return -EFAULT;
  2372. user_values = u64_to_user_ptr(timeline_fences->values_ptr);
  2373. if (!access_ok(user_values, nfences * sizeof(*user_values)))
  2374. return -EFAULT;
  2375. f = krealloc(eb->fences,
  2376. (eb->num_fences + nfences) * sizeof(*f),
  2377. __GFP_NOWARN | GFP_KERNEL);
  2378. if (!f)
  2379. return -ENOMEM;
  2380. eb->fences = f;
  2381. f += eb->num_fences;
  2382. BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
  2383. ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
  2384. while (nfences--) {
  2385. struct drm_i915_gem_exec_fence user_fence;
  2386. struct drm_syncobj *syncobj;
  2387. struct dma_fence *fence = NULL;
  2388. u64 point;
  2389. if (__copy_from_user(&user_fence,
  2390. user_fences++,
  2391. sizeof(user_fence)))
  2392. return -EFAULT;
  2393. if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS)
  2394. return -EINVAL;
  2395. if (__get_user(point, user_values++))
  2396. return -EFAULT;
  2397. syncobj = drm_syncobj_find(eb->file, user_fence.handle);
  2398. if (!syncobj) {
  2399. drm_dbg(&eb->i915->drm,
  2400. "Invalid syncobj handle provided\n");
  2401. return -ENOENT;
  2402. }
  2403. fence = drm_syncobj_fence_get(syncobj);
  2404. if (!fence && user_fence.flags &&
  2405. !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
  2406. drm_dbg(&eb->i915->drm,
  2407. "Syncobj handle has no fence\n");
  2408. drm_syncobj_put(syncobj);
  2409. return -EINVAL;
  2410. }
  2411. if (fence)
  2412. err = dma_fence_chain_find_seqno(&fence, point);
  2413. if (err && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
  2414. drm_dbg(&eb->i915->drm,
  2415. "Syncobj handle missing requested point %llu\n",
  2416. point);
  2417. dma_fence_put(fence);
  2418. drm_syncobj_put(syncobj);
  2419. return err;
  2420. }
  2421. /*
  2422. * A point might have been signaled already and
  2423. * garbage collected from the timeline. In this case
  2424. * just ignore the point and carry on.
  2425. */
  2426. if (!fence && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
  2427. drm_syncobj_put(syncobj);
  2428. continue;
  2429. }
  2430. /*
  2431. * For timeline syncobjs we need to preallocate chains for
  2432. * later signaling.
  2433. */
  2434. if (point != 0 && user_fence.flags & I915_EXEC_FENCE_SIGNAL) {
  2435. /*
  2436. * Waiting and signaling the same point (when point !=
  2437. * 0) would break the timeline.
  2438. */
  2439. if (user_fence.flags & I915_EXEC_FENCE_WAIT) {
  2440. drm_dbg(&eb->i915->drm,
  2441. "Trying to wait & signal the same timeline point.\n");
  2442. dma_fence_put(fence);
  2443. drm_syncobj_put(syncobj);
  2444. return -EINVAL;
  2445. }
  2446. f->chain_fence = dma_fence_chain_alloc();
  2447. if (!f->chain_fence) {
  2448. drm_syncobj_put(syncobj);
  2449. dma_fence_put(fence);
  2450. return -ENOMEM;
  2451. }
  2452. } else {
  2453. f->chain_fence = NULL;
  2454. }
  2455. f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2);
  2456. f->dma_fence = fence;
  2457. f->value = point;
  2458. f++;
  2459. eb->num_fences++;
  2460. }
  2461. return 0;
  2462. }
  2463. static int add_fence_array(struct i915_execbuffer *eb)
  2464. {
  2465. struct drm_i915_gem_execbuffer2 *args = eb->args;
  2466. struct drm_i915_gem_exec_fence __user *user;
  2467. unsigned long num_fences = args->num_cliprects;
  2468. struct eb_fence *f;
  2469. if (!(args->flags & I915_EXEC_FENCE_ARRAY))
  2470. return 0;
  2471. if (!num_fences)
  2472. return 0;
  2473. /* Check multiplication overflow for access_ok() and kvmalloc_array() */
  2474. BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
  2475. if (num_fences > min_t(unsigned long,
  2476. ULONG_MAX / sizeof(*user),
  2477. SIZE_MAX / sizeof(*f) - eb->num_fences))
  2478. return -EINVAL;
  2479. user = u64_to_user_ptr(args->cliprects_ptr);
  2480. if (!access_ok(user, num_fences * sizeof(*user)))
  2481. return -EFAULT;
  2482. f = krealloc(eb->fences,
  2483. (eb->num_fences + num_fences) * sizeof(*f),
  2484. __GFP_NOWARN | GFP_KERNEL);
  2485. if (!f)
  2486. return -ENOMEM;
  2487. eb->fences = f;
  2488. f += eb->num_fences;
  2489. while (num_fences--) {
  2490. struct drm_i915_gem_exec_fence user_fence;
  2491. struct drm_syncobj *syncobj;
  2492. struct dma_fence *fence = NULL;
  2493. if (__copy_from_user(&user_fence, user++, sizeof(user_fence)))
  2494. return -EFAULT;
  2495. if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS)
  2496. return -EINVAL;
  2497. syncobj = drm_syncobj_find(eb->file, user_fence.handle);
  2498. if (!syncobj) {
  2499. drm_dbg(&eb->i915->drm,
  2500. "Invalid syncobj handle provided\n");
  2501. return -ENOENT;
  2502. }
  2503. if (user_fence.flags & I915_EXEC_FENCE_WAIT) {
  2504. fence = drm_syncobj_fence_get(syncobj);
  2505. if (!fence) {
  2506. drm_dbg(&eb->i915->drm,
  2507. "Syncobj handle has no fence\n");
  2508. drm_syncobj_put(syncobj);
  2509. return -EINVAL;
  2510. }
  2511. }
  2512. BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
  2513. ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
  2514. f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2);
  2515. f->dma_fence = fence;
  2516. f->value = 0;
  2517. f->chain_fence = NULL;
  2518. f++;
  2519. eb->num_fences++;
  2520. }
  2521. return 0;
  2522. }
  2523. static void put_fence_array(struct eb_fence *fences, int num_fences)
  2524. {
  2525. if (fences)
  2526. __free_fence_array(fences, num_fences);
  2527. }
  2528. static int
  2529. await_fence_array(struct i915_execbuffer *eb,
  2530. struct i915_request *rq)
  2531. {
  2532. unsigned int n;
  2533. int err;
  2534. for (n = 0; n < eb->num_fences; n++) {
  2535. if (!eb->fences[n].dma_fence)
  2536. continue;
  2537. err = i915_request_await_dma_fence(rq, eb->fences[n].dma_fence);
  2538. if (err < 0)
  2539. return err;
  2540. }
  2541. return 0;
  2542. }
  2543. static void signal_fence_array(const struct i915_execbuffer *eb,
  2544. struct dma_fence * const fence)
  2545. {
  2546. unsigned int n;
  2547. for (n = 0; n < eb->num_fences; n++) {
  2548. struct drm_syncobj *syncobj;
  2549. unsigned int flags;
  2550. syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2);
  2551. if (!(flags & I915_EXEC_FENCE_SIGNAL))
  2552. continue;
  2553. if (eb->fences[n].chain_fence) {
  2554. drm_syncobj_add_point(syncobj,
  2555. eb->fences[n].chain_fence,
  2556. fence,
  2557. eb->fences[n].value);
  2558. /*
  2559. * The chain's ownership is transferred to the
  2560. * timeline.
  2561. */
  2562. eb->fences[n].chain_fence = NULL;
  2563. } else {
  2564. drm_syncobj_replace_fence(syncobj, fence);
  2565. }
  2566. }
  2567. }
  2568. static int
  2569. parse_timeline_fences(struct i915_user_extension __user *ext, void *data)
  2570. {
  2571. struct i915_execbuffer *eb = data;
  2572. struct drm_i915_gem_execbuffer_ext_timeline_fences timeline_fences;
  2573. if (copy_from_user(&timeline_fences, ext, sizeof(timeline_fences)))
  2574. return -EFAULT;
  2575. return add_timeline_fence_array(eb, &timeline_fences);
  2576. }
  2577. static void retire_requests(struct intel_timeline *tl, struct i915_request *end)
  2578. {
  2579. struct i915_request *rq, *rn;
  2580. list_for_each_entry_safe(rq, rn, &tl->requests, link)
  2581. if (rq == end || !i915_request_retire(rq))
  2582. break;
  2583. }
  2584. static int eb_request_add(struct i915_execbuffer *eb, struct i915_request *rq,
  2585. int err, bool last_parallel)
  2586. {
  2587. struct intel_timeline * const tl = i915_request_timeline(rq);
  2588. struct i915_sched_attr attr = {};
  2589. struct i915_request *prev;
  2590. lockdep_assert_held(&tl->mutex);
  2591. lockdep_unpin_lock(&tl->mutex, rq->cookie);
  2592. trace_i915_request_add(rq);
  2593. prev = __i915_request_commit(rq);
  2594. /* Check that the context wasn't destroyed before submission */
  2595. if (likely(!intel_context_is_closed(eb->context))) {
  2596. attr = eb->gem_context->sched;
  2597. } else {
  2598. /* Serialise with context_close via the add_to_timeline */
  2599. i915_request_set_error_once(rq, -ENOENT);
  2600. __i915_request_skip(rq);
  2601. err = -ENOENT; /* override any transient errors */
  2602. }
  2603. if (intel_context_is_parallel(eb->context)) {
  2604. if (err) {
  2605. __i915_request_skip(rq);
  2606. set_bit(I915_FENCE_FLAG_SKIP_PARALLEL,
  2607. &rq->fence.flags);
  2608. }
  2609. if (last_parallel)
  2610. set_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL,
  2611. &rq->fence.flags);
  2612. }
  2613. __i915_request_queue(rq, &attr);
  2614. /* Try to clean up the client's timeline after submitting the request */
  2615. if (prev)
  2616. retire_requests(tl, prev);
  2617. mutex_unlock(&tl->mutex);
  2618. return err;
  2619. }
  2620. static int eb_requests_add(struct i915_execbuffer *eb, int err)
  2621. {
  2622. int i;
  2623. /*
  2624. * We iterate in reverse order of creation to release timeline mutexes in
  2625. * same order.
  2626. */
  2627. for_each_batch_add_order(eb, i) {
  2628. struct i915_request *rq = eb->requests[i];
  2629. if (!rq)
  2630. continue;
  2631. err |= eb_request_add(eb, rq, err, i == 0);
  2632. }
  2633. return err;
  2634. }
  2635. static const i915_user_extension_fn execbuf_extensions[] = {
  2636. [DRM_I915_GEM_EXECBUFFER_EXT_TIMELINE_FENCES] = parse_timeline_fences,
  2637. };
  2638. static int
  2639. parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 *args,
  2640. struct i915_execbuffer *eb)
  2641. {
  2642. if (!(args->flags & I915_EXEC_USE_EXTENSIONS))
  2643. return 0;
  2644. /* The execbuf2 extension mechanism reuses cliprects_ptr. So we cannot
  2645. * have another flag also using it at the same time.
  2646. */
  2647. if (eb->args->flags & I915_EXEC_FENCE_ARRAY)
  2648. return -EINVAL;
  2649. if (args->num_cliprects != 0)
  2650. return -EINVAL;
  2651. return i915_user_extensions(u64_to_user_ptr(args->cliprects_ptr),
  2652. execbuf_extensions,
  2653. ARRAY_SIZE(execbuf_extensions),
  2654. eb);
  2655. }
  2656. static void eb_requests_get(struct i915_execbuffer *eb)
  2657. {
  2658. unsigned int i;
  2659. for_each_batch_create_order(eb, i) {
  2660. if (!eb->requests[i])
  2661. break;
  2662. i915_request_get(eb->requests[i]);
  2663. }
  2664. }
  2665. static void eb_requests_put(struct i915_execbuffer *eb)
  2666. {
  2667. unsigned int i;
  2668. for_each_batch_create_order(eb, i) {
  2669. if (!eb->requests[i])
  2670. break;
  2671. i915_request_put(eb->requests[i]);
  2672. }
  2673. }
  2674. static struct sync_file *
  2675. eb_composite_fence_create(struct i915_execbuffer *eb, int out_fence_fd)
  2676. {
  2677. struct sync_file *out_fence = NULL;
  2678. struct dma_fence_array *fence_array;
  2679. struct dma_fence **fences;
  2680. unsigned int i;
  2681. GEM_BUG_ON(!intel_context_is_parent(eb->context));
  2682. fences = kmalloc_array(eb->num_batches, sizeof(*fences), GFP_KERNEL);
  2683. if (!fences)
  2684. return ERR_PTR(-ENOMEM);
  2685. for_each_batch_create_order(eb, i) {
  2686. fences[i] = &eb->requests[i]->fence;
  2687. __set_bit(I915_FENCE_FLAG_COMPOSITE,
  2688. &eb->requests[i]->fence.flags);
  2689. }
  2690. fence_array = dma_fence_array_create(eb->num_batches,
  2691. fences,
  2692. eb->context->parallel.fence_context,
  2693. eb->context->parallel.seqno++,
  2694. false);
  2695. if (!fence_array) {
  2696. kfree(fences);
  2697. return ERR_PTR(-ENOMEM);
  2698. }
  2699. /* Move ownership to the dma_fence_array created above */
  2700. for_each_batch_create_order(eb, i)
  2701. dma_fence_get(fences[i]);
  2702. if (out_fence_fd != -1) {
  2703. out_fence = sync_file_create(&fence_array->base);
  2704. /* sync_file now owns fence_arry, drop creation ref */
  2705. dma_fence_put(&fence_array->base);
  2706. if (!out_fence)
  2707. return ERR_PTR(-ENOMEM);
  2708. }
  2709. eb->composite_fence = &fence_array->base;
  2710. return out_fence;
  2711. }
  2712. static struct sync_file *
  2713. eb_fences_add(struct i915_execbuffer *eb, struct i915_request *rq,
  2714. struct dma_fence *in_fence, int out_fence_fd)
  2715. {
  2716. struct sync_file *out_fence = NULL;
  2717. int err;
  2718. if (unlikely(eb->gem_context->syncobj)) {
  2719. struct dma_fence *fence;
  2720. fence = drm_syncobj_fence_get(eb->gem_context->syncobj);
  2721. err = i915_request_await_dma_fence(rq, fence);
  2722. dma_fence_put(fence);
  2723. if (err)
  2724. return ERR_PTR(err);
  2725. }
  2726. if (in_fence) {
  2727. if (eb->args->flags & I915_EXEC_FENCE_SUBMIT)
  2728. err = i915_request_await_execution(rq, in_fence);
  2729. else
  2730. err = i915_request_await_dma_fence(rq, in_fence);
  2731. if (err < 0)
  2732. return ERR_PTR(err);
  2733. }
  2734. if (eb->fences) {
  2735. err = await_fence_array(eb, rq);
  2736. if (err)
  2737. return ERR_PTR(err);
  2738. }
  2739. if (intel_context_is_parallel(eb->context)) {
  2740. out_fence = eb_composite_fence_create(eb, out_fence_fd);
  2741. if (IS_ERR(out_fence))
  2742. return ERR_PTR(-ENOMEM);
  2743. } else if (out_fence_fd != -1) {
  2744. out_fence = sync_file_create(&rq->fence);
  2745. if (!out_fence)
  2746. return ERR_PTR(-ENOMEM);
  2747. }
  2748. return out_fence;
  2749. }
  2750. static struct intel_context *
  2751. eb_find_context(struct i915_execbuffer *eb, unsigned int context_number)
  2752. {
  2753. struct intel_context *child;
  2754. if (likely(context_number == 0))
  2755. return eb->context;
  2756. for_each_child(eb->context, child)
  2757. if (!--context_number)
  2758. return child;
  2759. GEM_BUG_ON("Context not found");
  2760. return NULL;
  2761. }
  2762. static struct sync_file *
  2763. eb_requests_create(struct i915_execbuffer *eb, struct dma_fence *in_fence,
  2764. int out_fence_fd)
  2765. {
  2766. struct sync_file *out_fence = NULL;
  2767. unsigned int i;
  2768. for_each_batch_create_order(eb, i) {
  2769. /* Allocate a request for this batch buffer nice and early. */
  2770. eb->requests[i] = i915_request_create(eb_find_context(eb, i));
  2771. if (IS_ERR(eb->requests[i])) {
  2772. out_fence = ERR_CAST(eb->requests[i]);
  2773. eb->requests[i] = NULL;
  2774. return out_fence;
  2775. }
  2776. /*
  2777. * Only the first request added (committed to backend) has to
  2778. * take the in fences into account as all subsequent requests
  2779. * will have fences inserted inbetween them.
  2780. */
  2781. if (i + 1 == eb->num_batches) {
  2782. out_fence = eb_fences_add(eb, eb->requests[i],
  2783. in_fence, out_fence_fd);
  2784. if (IS_ERR(out_fence))
  2785. return out_fence;
  2786. }
  2787. /*
  2788. * Not really on stack, but we don't want to call
  2789. * kfree on the batch_snapshot when we put it, so use the
  2790. * _onstack interface.
  2791. */
  2792. if (eb->batches[i]->vma)
  2793. eb->requests[i]->batch_res =
  2794. i915_vma_resource_get(eb->batches[i]->vma->resource);
  2795. if (eb->batch_pool) {
  2796. GEM_BUG_ON(intel_context_is_parallel(eb->context));
  2797. intel_gt_buffer_pool_mark_active(eb->batch_pool,
  2798. eb->requests[i]);
  2799. }
  2800. }
  2801. return out_fence;
  2802. }
  2803. static int
  2804. i915_gem_do_execbuffer(struct drm_device *dev,
  2805. struct drm_file *file,
  2806. struct drm_i915_gem_execbuffer2 *args,
  2807. struct drm_i915_gem_exec_object2 *exec)
  2808. {
  2809. struct drm_i915_private *i915 = to_i915(dev);
  2810. struct i915_execbuffer eb;
  2811. struct dma_fence *in_fence = NULL;
  2812. struct sync_file *out_fence = NULL;
  2813. int out_fence_fd = -1;
  2814. int err;
  2815. BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
  2816. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
  2817. ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  2818. eb.i915 = i915;
  2819. eb.file = file;
  2820. eb.args = args;
  2821. if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
  2822. args->flags |= __EXEC_HAS_RELOC;
  2823. eb.exec = exec;
  2824. eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1);
  2825. eb.vma[0].vma = NULL;
  2826. eb.batch_pool = NULL;
  2827. eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  2828. reloc_cache_init(&eb.reloc_cache, eb.i915);
  2829. eb.buffer_count = args->buffer_count;
  2830. eb.batch_start_offset = args->batch_start_offset;
  2831. eb.trampoline = NULL;
  2832. eb.fences = NULL;
  2833. eb.num_fences = 0;
  2834. eb_capture_list_clear(&eb);
  2835. memset(eb.requests, 0, sizeof(struct i915_request *) *
  2836. ARRAY_SIZE(eb.requests));
  2837. eb.composite_fence = NULL;
  2838. eb.batch_flags = 0;
  2839. if (args->flags & I915_EXEC_SECURE) {
  2840. if (GRAPHICS_VER(i915) >= 11)
  2841. return -ENODEV;
  2842. /* Return -EPERM to trigger fallback code on old binaries. */
  2843. if (!HAS_SECURE_BATCHES(i915))
  2844. return -EPERM;
  2845. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  2846. return -EPERM;
  2847. eb.batch_flags |= I915_DISPATCH_SECURE;
  2848. }
  2849. if (args->flags & I915_EXEC_IS_PINNED)
  2850. eb.batch_flags |= I915_DISPATCH_PINNED;
  2851. err = parse_execbuf2_extensions(args, &eb);
  2852. if (err)
  2853. goto err_ext;
  2854. err = add_fence_array(&eb);
  2855. if (err)
  2856. goto err_ext;
  2857. #define IN_FENCES (I915_EXEC_FENCE_IN | I915_EXEC_FENCE_SUBMIT)
  2858. if (args->flags & IN_FENCES) {
  2859. if ((args->flags & IN_FENCES) == IN_FENCES)
  2860. return -EINVAL;
  2861. in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
  2862. if (!in_fence) {
  2863. err = -EINVAL;
  2864. goto err_ext;
  2865. }
  2866. }
  2867. #undef IN_FENCES
  2868. if (args->flags & I915_EXEC_FENCE_OUT) {
  2869. out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  2870. if (out_fence_fd < 0) {
  2871. err = out_fence_fd;
  2872. goto err_in_fence;
  2873. }
  2874. }
  2875. err = eb_create(&eb);
  2876. if (err)
  2877. goto err_out_fence;
  2878. GEM_BUG_ON(!eb.lut_size);
  2879. err = eb_select_context(&eb);
  2880. if (unlikely(err))
  2881. goto err_destroy;
  2882. err = eb_select_engine(&eb);
  2883. if (unlikely(err))
  2884. goto err_context;
  2885. err = eb_lookup_vmas(&eb);
  2886. if (err) {
  2887. eb_release_vmas(&eb, true);
  2888. goto err_engine;
  2889. }
  2890. i915_gem_ww_ctx_init(&eb.ww, true);
  2891. err = eb_relocate_parse(&eb);
  2892. if (err) {
  2893. /*
  2894. * If the user expects the execobject.offset and
  2895. * reloc.presumed_offset to be an exact match,
  2896. * as for using NO_RELOC, then we cannot update
  2897. * the execobject.offset until we have completed
  2898. * relocation.
  2899. */
  2900. args->flags &= ~__EXEC_HAS_RELOC;
  2901. goto err_vma;
  2902. }
  2903. ww_acquire_done(&eb.ww.ctx);
  2904. err = eb_capture_stage(&eb);
  2905. if (err)
  2906. goto err_vma;
  2907. out_fence = eb_requests_create(&eb, in_fence, out_fence_fd);
  2908. if (IS_ERR(out_fence)) {
  2909. err = PTR_ERR(out_fence);
  2910. out_fence = NULL;
  2911. if (eb.requests[0])
  2912. goto err_request;
  2913. else
  2914. goto err_vma;
  2915. }
  2916. err = eb_submit(&eb);
  2917. err_request:
  2918. eb_requests_get(&eb);
  2919. err = eb_requests_add(&eb, err);
  2920. if (eb.fences)
  2921. signal_fence_array(&eb, eb.composite_fence ?
  2922. eb.composite_fence :
  2923. &eb.requests[0]->fence);
  2924. if (unlikely(eb.gem_context->syncobj)) {
  2925. drm_syncobj_replace_fence(eb.gem_context->syncobj,
  2926. eb.composite_fence ?
  2927. eb.composite_fence :
  2928. &eb.requests[0]->fence);
  2929. }
  2930. if (out_fence) {
  2931. if (err == 0) {
  2932. fd_install(out_fence_fd, out_fence->file);
  2933. args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */
  2934. args->rsvd2 |= (u64)out_fence_fd << 32;
  2935. out_fence_fd = -1;
  2936. } else {
  2937. fput(out_fence->file);
  2938. }
  2939. }
  2940. if (!out_fence && eb.composite_fence)
  2941. dma_fence_put(eb.composite_fence);
  2942. eb_requests_put(&eb);
  2943. err_vma:
  2944. eb_release_vmas(&eb, true);
  2945. WARN_ON(err == -EDEADLK);
  2946. i915_gem_ww_ctx_fini(&eb.ww);
  2947. if (eb.batch_pool)
  2948. intel_gt_buffer_pool_put(eb.batch_pool);
  2949. err_engine:
  2950. eb_put_engine(&eb);
  2951. err_context:
  2952. i915_gem_context_put(eb.gem_context);
  2953. err_destroy:
  2954. eb_destroy(&eb);
  2955. err_out_fence:
  2956. if (out_fence_fd != -1)
  2957. put_unused_fd(out_fence_fd);
  2958. err_in_fence:
  2959. dma_fence_put(in_fence);
  2960. err_ext:
  2961. put_fence_array(eb.fences, eb.num_fences);
  2962. return err;
  2963. }
  2964. static size_t eb_element_size(void)
  2965. {
  2966. return sizeof(struct drm_i915_gem_exec_object2) + sizeof(struct eb_vma);
  2967. }
  2968. static bool check_buffer_count(size_t count)
  2969. {
  2970. const size_t sz = eb_element_size();
  2971. /*
  2972. * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
  2973. * array size (see eb_create()). Otherwise, we can accept an array as
  2974. * large as can be addressed (though use large arrays at your peril)!
  2975. */
  2976. return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
  2977. }
  2978. int
  2979. i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
  2980. struct drm_file *file)
  2981. {
  2982. struct drm_i915_private *i915 = to_i915(dev);
  2983. struct drm_i915_gem_execbuffer2 *args = data;
  2984. struct drm_i915_gem_exec_object2 *exec2_list;
  2985. const size_t count = args->buffer_count;
  2986. int err;
  2987. if (!check_buffer_count(count)) {
  2988. drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count);
  2989. return -EINVAL;
  2990. }
  2991. err = i915_gem_check_execbuffer(i915, args);
  2992. if (err)
  2993. return err;
  2994. /* Allocate extra slots for use by the command parser */
  2995. exec2_list = kvmalloc_array(count + 2, eb_element_size(),
  2996. __GFP_NOWARN | GFP_KERNEL);
  2997. if (exec2_list == NULL) {
  2998. drm_dbg(&i915->drm, "Failed to allocate exec list for %zd buffers\n",
  2999. count);
  3000. return -ENOMEM;
  3001. }
  3002. if (copy_from_user(exec2_list,
  3003. u64_to_user_ptr(args->buffers_ptr),
  3004. sizeof(*exec2_list) * count)) {
  3005. drm_dbg(&i915->drm, "copy %zd exec entries failed\n", count);
  3006. kvfree(exec2_list);
  3007. return -EFAULT;
  3008. }
  3009. err = i915_gem_do_execbuffer(dev, file, args, exec2_list);
  3010. /*
  3011. * Now that we have begun execution of the batchbuffer, we ignore
  3012. * any new error after this point. Also given that we have already
  3013. * updated the associated relocations, we try to write out the current
  3014. * object locations irrespective of any error.
  3015. */
  3016. if (args->flags & __EXEC_HAS_RELOC) {
  3017. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  3018. u64_to_user_ptr(args->buffers_ptr);
  3019. unsigned int i;
  3020. /* Copy the new buffer offsets back to the user's exec list. */
  3021. /*
  3022. * Note: count * sizeof(*user_exec_list) does not overflow,
  3023. * because we checked 'count' in check_buffer_count().
  3024. *
  3025. * And this range already got effectively checked earlier
  3026. * when we did the "copy_from_user()" above.
  3027. */
  3028. if (!user_write_access_begin(user_exec_list,
  3029. count * sizeof(*user_exec_list)))
  3030. goto end;
  3031. for (i = 0; i < args->buffer_count; i++) {
  3032. if (!(exec2_list[i].offset & UPDATE))
  3033. continue;
  3034. exec2_list[i].offset =
  3035. gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
  3036. unsafe_put_user(exec2_list[i].offset,
  3037. &user_exec_list[i].offset,
  3038. end_user);
  3039. }
  3040. end_user:
  3041. user_write_access_end();
  3042. end:;
  3043. }
  3044. args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
  3045. kvfree(exec2_list);
  3046. return err;
  3047. }