i915_gem_execbuffer.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  1. /*
  2. * Copyright © 2008,2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <linux/dma_remapping.h>
  29. #include <linux/reservation.h>
  30. #include <linux/sync_file.h>
  31. #include <linux/uaccess.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm_syncobj.h>
  34. #include <drm/i915_drm.h>
  35. #include "i915_drv.h"
  36. #include "i915_gem_clflush.h"
  37. #include "i915_trace.h"
  38. #include "intel_drv.h"
  39. #include "intel_frontbuffer.h"
  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. #define __EXEC_OBJECT_HAS_REF BIT(31)
  47. #define __EXEC_OBJECT_HAS_PIN BIT(30)
  48. #define __EXEC_OBJECT_HAS_FENCE BIT(29)
  49. #define __EXEC_OBJECT_NEEDS_MAP BIT(28)
  50. #define __EXEC_OBJECT_NEEDS_BIAS BIT(27)
  51. #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */
  52. #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
  53. #define __EXEC_HAS_RELOC BIT(31)
  54. #define __EXEC_VALIDATED BIT(30)
  55. #define __EXEC_INTERNAL_FLAGS (~0u << 30)
  56. #define UPDATE PIN_OFFSET_FIXED
  57. #define BATCH_OFFSET_BIAS (256*1024)
  58. #define __I915_EXEC_ILLEGAL_FLAGS \
  59. (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
  60. /**
  61. * DOC: User command execution
  62. *
  63. * Userspace submits commands to be executed on the GPU as an instruction
  64. * stream within a GEM object we call a batchbuffer. This instructions may
  65. * refer to other GEM objects containing auxiliary state such as kernels,
  66. * samplers, render targets and even secondary batchbuffers. Userspace does
  67. * not know where in the GPU memory these objects reside and so before the
  68. * batchbuffer is passed to the GPU for execution, those addresses in the
  69. * batchbuffer and auxiliary objects are updated. This is known as relocation,
  70. * or patching. To try and avoid having to relocate each object on the next
  71. * execution, userspace is told the location of those objects in this pass,
  72. * but this remains just a hint as the kernel may choose a new location for
  73. * any object in the future.
  74. *
  75. * Processing an execbuf ioctl is conceptually split up into a few phases.
  76. *
  77. * 1. Validation - Ensure all the pointers, handles and flags are valid.
  78. * 2. Reservation - Assign GPU address space for every object
  79. * 3. Relocation - Update any addresses to point to the final locations
  80. * 4. Serialisation - Order the request with respect to its dependencies
  81. * 5. Construction - Construct a request to execute the batchbuffer
  82. * 6. Submission (at some point in the future execution)
  83. *
  84. * Reserving resources for the execbuf is the most complicated phase. We
  85. * neither want to have to migrate the object in the address space, nor do
  86. * we want to have to update any relocations pointing to this object. Ideally,
  87. * we want to leave the object where it is and for all the existing relocations
  88. * to match. If the object is given a new address, or if userspace thinks the
  89. * object is elsewhere, we have to parse all the relocation entries and update
  90. * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
  91. * all the target addresses in all of its objects match the value in the
  92. * relocation entries and that they all match the presumed offsets given by the
  93. * list of execbuffer objects. Using this knowledge, we know that if we haven't
  94. * moved any buffers, all the relocation entries are valid and we can skip
  95. * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
  96. * hang.) The requirement for using I915_EXEC_NO_RELOC are:
  97. *
  98. * The addresses written in the objects must match the corresponding
  99. * reloc.presumed_offset which in turn must match the corresponding
  100. * execobject.offset.
  101. *
  102. * Any render targets written to in the batch must be flagged with
  103. * EXEC_OBJECT_WRITE.
  104. *
  105. * To avoid stalling, execobject.offset should match the current
  106. * address of that object within the active context.
  107. *
  108. * The reservation is done is multiple phases. First we try and keep any
  109. * object already bound in its current location - so as long as meets the
  110. * constraints imposed by the new execbuffer. Any object left unbound after the
  111. * first pass is then fitted into any available idle space. If an object does
  112. * not fit, all objects are removed from the reservation and the process rerun
  113. * after sorting the objects into a priority order (more difficult to fit
  114. * objects are tried first). Failing that, the entire VM is cleared and we try
  115. * to fit the execbuf once last time before concluding that it simply will not
  116. * fit.
  117. *
  118. * A small complication to all of this is that we allow userspace not only to
  119. * specify an alignment and a size for the object in the address space, but
  120. * we also allow userspace to specify the exact offset. This objects are
  121. * simpler to place (the location is known a priori) all we have to do is make
  122. * sure the space is available.
  123. *
  124. * Once all the objects are in place, patching up the buried pointers to point
  125. * to the final locations is a fairly simple job of walking over the relocation
  126. * entry arrays, looking up the right address and rewriting the value into
  127. * the object. Simple! ... The relocation entries are stored in user memory
  128. * and so to access them we have to copy them into a local buffer. That copy
  129. * has to avoid taking any pagefaults as they may lead back to a GEM object
  130. * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
  131. * the relocation into multiple passes. First we try to do everything within an
  132. * atomic context (avoid the pagefaults) which requires that we never wait. If
  133. * we detect that we may wait, or if we need to fault, then we have to fallback
  134. * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
  135. * bells yet?) Dropping the mutex means that we lose all the state we have
  136. * built up so far for the execbuf and we must reset any global data. However,
  137. * we do leave the objects pinned in their final locations - which is a
  138. * potential issue for concurrent execbufs. Once we have left the mutex, we can
  139. * allocate and copy all the relocation entries into a large array at our
  140. * leisure, reacquire the mutex, reclaim all the objects and other state and
  141. * then proceed to update any incorrect addresses with the objects.
  142. *
  143. * As we process the relocation entries, we maintain a record of whether the
  144. * object is being written to. Using NORELOC, we expect userspace to provide
  145. * this information instead. We also check whether we can skip the relocation
  146. * by comparing the expected value inside the relocation entry with the target's
  147. * final address. If they differ, we have to map the current object and rewrite
  148. * the 4 or 8 byte pointer within.
  149. *
  150. * Serialising an execbuf is quite simple according to the rules of the GEM
  151. * ABI. Execution within each context is ordered by the order of submission.
  152. * Writes to any GEM object are in order of submission and are exclusive. Reads
  153. * from a GEM object are unordered with respect to other reads, but ordered by
  154. * writes. A write submitted after a read cannot occur before the read, and
  155. * similarly any read submitted after a write cannot occur before the write.
  156. * Writes are ordered between engines such that only one write occurs at any
  157. * time (completing any reads beforehand) - using semaphores where available
  158. * and CPU serialisation otherwise. Other GEM access obey the same rules, any
  159. * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
  160. * reads before starting, and any read (either using set-domain or pread) must
  161. * flush all GPU writes before starting. (Note we only employ a barrier before,
  162. * we currently rely on userspace not concurrently starting a new execution
  163. * whilst reading or writing to an object. This may be an advantage or not
  164. * depending on how much you trust userspace not to shoot themselves in the
  165. * foot.) Serialisation may just result in the request being inserted into
  166. * a DAG awaiting its turn, but most simple is to wait on the CPU until
  167. * all dependencies are resolved.
  168. *
  169. * After all of that, is just a matter of closing the request and handing it to
  170. * the hardware (well, leaving it in a queue to be executed). However, we also
  171. * offer the ability for batchbuffers to be run with elevated privileges so
  172. * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
  173. * Before any batch is given extra privileges we first must check that it
  174. * contains no nefarious instructions, we check that each instruction is from
  175. * our whitelist and all registers are also from an allowed list. We first
  176. * copy the user's batchbuffer to a shadow (so that the user doesn't have
  177. * access to it, either by the CPU or GPU as we scan it) and then parse each
  178. * instruction. If everything is ok, we set a flag telling the hardware to run
  179. * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
  180. */
  181. struct i915_execbuffer {
  182. struct drm_i915_private *i915; /** i915 backpointer */
  183. struct drm_file *file; /** per-file lookup tables and limits */
  184. struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
  185. struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
  186. struct i915_vma **vma;
  187. unsigned int *flags;
  188. struct intel_engine_cs *engine; /** engine to queue the request to */
  189. struct i915_gem_context *ctx; /** context for building the request */
  190. struct i915_address_space *vm; /** GTT and vma for the request */
  191. struct drm_i915_gem_request *request; /** our request to build */
  192. struct i915_vma *batch; /** identity of the batch obj/vma */
  193. /** actual size of execobj[] as we may extend it for the cmdparser */
  194. unsigned int buffer_count;
  195. /** list of vma not yet bound during reservation phase */
  196. struct list_head unbound;
  197. /** list of vma that have execobj.relocation_count */
  198. struct list_head relocs;
  199. /**
  200. * Track the most recently used object for relocations, as we
  201. * frequently have to perform multiple relocations within the same
  202. * obj/page
  203. */
  204. struct reloc_cache {
  205. struct drm_mm_node node; /** temporary GTT binding */
  206. unsigned long vaddr; /** Current kmap address */
  207. unsigned long page; /** Currently mapped page index */
  208. unsigned int gen; /** Cached value of INTEL_GEN */
  209. bool use_64bit_reloc : 1;
  210. bool has_llc : 1;
  211. bool has_fence : 1;
  212. bool needs_unfenced : 1;
  213. struct drm_i915_gem_request *rq;
  214. u32 *rq_cmd;
  215. unsigned int rq_size;
  216. } reloc_cache;
  217. u64 invalid_flags; /** Set of execobj.flags that are invalid */
  218. u32 context_flags; /** Set of execobj.flags to insert from the ctx */
  219. u32 batch_start_offset; /** Location within object of batch */
  220. u32 batch_len; /** Length of batch within object */
  221. u32 batch_flags; /** Flags composed for emit_bb_start() */
  222. /**
  223. * Indicate either the size of the hastable used to resolve
  224. * relocation handles, or if negative that we are using a direct
  225. * index into the execobj[].
  226. */
  227. int lut_size;
  228. struct hlist_head *buckets; /** ht for relocation handles */
  229. };
  230. #define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags])
  231. /*
  232. * Used to convert any address to canonical form.
  233. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  234. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  235. * addresses to be in a canonical form:
  236. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  237. * canonical form [63:48] == [47]."
  238. */
  239. #define GEN8_HIGH_ADDRESS_BIT 47
  240. static inline u64 gen8_canonical_addr(u64 address)
  241. {
  242. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  243. }
  244. static inline u64 gen8_noncanonical_addr(u64 address)
  245. {
  246. return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
  247. }
  248. static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb)
  249. {
  250. return intel_engine_needs_cmd_parser(eb->engine) && eb->batch_len;
  251. }
  252. static int eb_create(struct i915_execbuffer *eb)
  253. {
  254. if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
  255. unsigned int size = 1 + ilog2(eb->buffer_count);
  256. /*
  257. * Without a 1:1 association between relocation handles and
  258. * the execobject[] index, we instead create a hashtable.
  259. * We size it dynamically based on available memory, starting
  260. * first with 1:1 assocative hash and scaling back until
  261. * the allocation succeeds.
  262. *
  263. * Later on we use a positive lut_size to indicate we are
  264. * using this hashtable, and a negative value to indicate a
  265. * direct lookup.
  266. */
  267. do {
  268. gfp_t flags;
  269. /* While we can still reduce the allocation size, don't
  270. * raise a warning and allow the allocation to fail.
  271. * On the last pass though, we want to try as hard
  272. * as possible to perform the allocation and warn
  273. * if it fails.
  274. */
  275. flags = GFP_KERNEL;
  276. if (size > 1)
  277. flags |= __GFP_NORETRY | __GFP_NOWARN;
  278. eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
  279. flags);
  280. if (eb->buckets)
  281. break;
  282. } while (--size);
  283. if (unlikely(!size))
  284. return -ENOMEM;
  285. eb->lut_size = size;
  286. } else {
  287. eb->lut_size = -eb->buffer_count;
  288. }
  289. return 0;
  290. }
  291. static bool
  292. eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
  293. const struct i915_vma *vma,
  294. unsigned int flags)
  295. {
  296. if (vma->node.size < entry->pad_to_size)
  297. return true;
  298. if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
  299. return true;
  300. if (flags & EXEC_OBJECT_PINNED &&
  301. vma->node.start != entry->offset)
  302. return true;
  303. if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
  304. vma->node.start < BATCH_OFFSET_BIAS)
  305. return true;
  306. if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
  307. (vma->node.start + vma->node.size - 1) >> 32)
  308. return true;
  309. if (flags & __EXEC_OBJECT_NEEDS_MAP &&
  310. !i915_vma_is_map_and_fenceable(vma))
  311. return true;
  312. return false;
  313. }
  314. static inline bool
  315. eb_pin_vma(struct i915_execbuffer *eb,
  316. const struct drm_i915_gem_exec_object2 *entry,
  317. struct i915_vma *vma)
  318. {
  319. unsigned int exec_flags = *vma->exec_flags;
  320. u64 pin_flags;
  321. if (vma->node.size)
  322. pin_flags = vma->node.start;
  323. else
  324. pin_flags = entry->offset & PIN_OFFSET_MASK;
  325. pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
  326. if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT))
  327. pin_flags |= PIN_GLOBAL;
  328. if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags)))
  329. return false;
  330. if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
  331. if (unlikely(i915_vma_pin_fence(vma))) {
  332. i915_vma_unpin(vma);
  333. return false;
  334. }
  335. if (vma->fence)
  336. exec_flags |= __EXEC_OBJECT_HAS_FENCE;
  337. }
  338. *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
  339. return !eb_vma_misplaced(entry, vma, exec_flags);
  340. }
  341. static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags)
  342. {
  343. GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN));
  344. if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE))
  345. __i915_vma_unpin_fence(vma);
  346. __i915_vma_unpin(vma);
  347. }
  348. static inline void
  349. eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags)
  350. {
  351. if (!(*flags & __EXEC_OBJECT_HAS_PIN))
  352. return;
  353. __eb_unreserve_vma(vma, *flags);
  354. *flags &= ~__EXEC_OBJECT_RESERVED;
  355. }
  356. static int
  357. eb_validate_vma(struct i915_execbuffer *eb,
  358. struct drm_i915_gem_exec_object2 *entry,
  359. struct i915_vma *vma)
  360. {
  361. if (unlikely(entry->flags & eb->invalid_flags))
  362. return -EINVAL;
  363. if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
  364. return -EINVAL;
  365. /*
  366. * Offset can be used as input (EXEC_OBJECT_PINNED), reject
  367. * any non-page-aligned or non-canonical addresses.
  368. */
  369. if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
  370. entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
  371. return -EINVAL;
  372. /* pad_to_size was once a reserved field, so sanitize it */
  373. if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
  374. if (unlikely(offset_in_page(entry->pad_to_size)))
  375. return -EINVAL;
  376. } else {
  377. entry->pad_to_size = 0;
  378. }
  379. if (unlikely(vma->exec_flags)) {
  380. DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
  381. entry->handle, (int)(entry - eb->exec));
  382. return -EINVAL;
  383. }
  384. /*
  385. * From drm_mm perspective address space is continuous,
  386. * so from this point we're always using non-canonical
  387. * form internally.
  388. */
  389. entry->offset = gen8_noncanonical_addr(entry->offset);
  390. if (!eb->reloc_cache.has_fence) {
  391. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  392. } else {
  393. if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
  394. eb->reloc_cache.needs_unfenced) &&
  395. i915_gem_object_is_tiled(vma->obj))
  396. entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
  397. }
  398. if (!(entry->flags & EXEC_OBJECT_PINNED))
  399. entry->flags |= eb->context_flags;
  400. return 0;
  401. }
  402. static int
  403. eb_add_vma(struct i915_execbuffer *eb, unsigned int i, struct i915_vma *vma)
  404. {
  405. struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
  406. int err;
  407. GEM_BUG_ON(i915_vma_is_closed(vma));
  408. if (!(eb->args->flags & __EXEC_VALIDATED)) {
  409. err = eb_validate_vma(eb, entry, vma);
  410. if (unlikely(err))
  411. return err;
  412. }
  413. if (eb->lut_size > 0) {
  414. vma->exec_handle = entry->handle;
  415. hlist_add_head(&vma->exec_node,
  416. &eb->buckets[hash_32(entry->handle,
  417. eb->lut_size)]);
  418. }
  419. if (entry->relocation_count)
  420. list_add_tail(&vma->reloc_link, &eb->relocs);
  421. /*
  422. * Stash a pointer from the vma to execobj, so we can query its flags,
  423. * size, alignment etc as provided by the user. Also we stash a pointer
  424. * to the vma inside the execobj so that we can use a direct lookup
  425. * to find the right target VMA when doing relocations.
  426. */
  427. eb->vma[i] = vma;
  428. eb->flags[i] = entry->flags;
  429. vma->exec_flags = &eb->flags[i];
  430. err = 0;
  431. if (eb_pin_vma(eb, entry, vma)) {
  432. if (entry->offset != vma->node.start) {
  433. entry->offset = vma->node.start | UPDATE;
  434. eb->args->flags |= __EXEC_HAS_RELOC;
  435. }
  436. } else {
  437. eb_unreserve_vma(vma, vma->exec_flags);
  438. list_add_tail(&vma->exec_link, &eb->unbound);
  439. if (drm_mm_node_allocated(&vma->node))
  440. err = i915_vma_unbind(vma);
  441. }
  442. return err;
  443. }
  444. static inline int use_cpu_reloc(const struct reloc_cache *cache,
  445. const struct drm_i915_gem_object *obj)
  446. {
  447. if (!i915_gem_object_has_struct_page(obj))
  448. return false;
  449. if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
  450. return true;
  451. if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
  452. return false;
  453. return (cache->has_llc ||
  454. obj->cache_dirty ||
  455. obj->cache_level != I915_CACHE_NONE);
  456. }
  457. static int eb_reserve_vma(const struct i915_execbuffer *eb,
  458. struct i915_vma *vma)
  459. {
  460. struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  461. unsigned int exec_flags = *vma->exec_flags;
  462. u64 pin_flags;
  463. int err;
  464. pin_flags = PIN_USER | PIN_NONBLOCK;
  465. if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
  466. pin_flags |= PIN_GLOBAL;
  467. /*
  468. * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  469. * limit address to the first 4GBs for unflagged objects.
  470. */
  471. if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
  472. pin_flags |= PIN_ZONE_4G;
  473. if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
  474. pin_flags |= PIN_MAPPABLE;
  475. if (exec_flags & EXEC_OBJECT_PINNED) {
  476. pin_flags |= entry->offset | PIN_OFFSET_FIXED;
  477. pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
  478. } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
  479. pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  480. }
  481. err = i915_vma_pin(vma,
  482. entry->pad_to_size, entry->alignment,
  483. pin_flags);
  484. if (err)
  485. return err;
  486. if (entry->offset != vma->node.start) {
  487. entry->offset = vma->node.start | UPDATE;
  488. eb->args->flags |= __EXEC_HAS_RELOC;
  489. }
  490. if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
  491. err = i915_vma_pin_fence(vma);
  492. if (unlikely(err)) {
  493. i915_vma_unpin(vma);
  494. return err;
  495. }
  496. if (vma->fence)
  497. exec_flags |= __EXEC_OBJECT_HAS_FENCE;
  498. }
  499. *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
  500. GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
  501. return 0;
  502. }
  503. static int eb_reserve(struct i915_execbuffer *eb)
  504. {
  505. const unsigned int count = eb->buffer_count;
  506. struct list_head last;
  507. struct i915_vma *vma;
  508. unsigned int i, pass;
  509. int err;
  510. /*
  511. * Attempt to pin all of the buffers into the GTT.
  512. * This is done in 3 phases:
  513. *
  514. * 1a. Unbind all objects that do not match the GTT constraints for
  515. * the execbuffer (fenceable, mappable, alignment etc).
  516. * 1b. Increment pin count for already bound objects.
  517. * 2. Bind new objects.
  518. * 3. Decrement pin count.
  519. *
  520. * This avoid unnecessary unbinding of later objects in order to make
  521. * room for the earlier objects *unless* we need to defragment.
  522. */
  523. pass = 0;
  524. err = 0;
  525. do {
  526. list_for_each_entry(vma, &eb->unbound, exec_link) {
  527. err = eb_reserve_vma(eb, vma);
  528. if (err)
  529. break;
  530. }
  531. if (err != -ENOSPC)
  532. return err;
  533. /* Resort *all* the objects into priority order */
  534. INIT_LIST_HEAD(&eb->unbound);
  535. INIT_LIST_HEAD(&last);
  536. for (i = 0; i < count; i++) {
  537. unsigned int flags = eb->flags[i];
  538. struct i915_vma *vma = eb->vma[i];
  539. if (flags & EXEC_OBJECT_PINNED &&
  540. flags & __EXEC_OBJECT_HAS_PIN)
  541. continue;
  542. eb_unreserve_vma(vma, &eb->flags[i]);
  543. if (flags & EXEC_OBJECT_PINNED)
  544. list_add(&vma->exec_link, &eb->unbound);
  545. else if (flags & __EXEC_OBJECT_NEEDS_MAP)
  546. list_add_tail(&vma->exec_link, &eb->unbound);
  547. else
  548. list_add_tail(&vma->exec_link, &last);
  549. }
  550. list_splice_tail(&last, &eb->unbound);
  551. switch (pass++) {
  552. case 0:
  553. break;
  554. case 1:
  555. /* Too fragmented, unbind everything and retry */
  556. err = i915_gem_evict_vm(eb->vm);
  557. if (err)
  558. return err;
  559. break;
  560. default:
  561. return -ENOSPC;
  562. }
  563. } while (1);
  564. }
  565. static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
  566. {
  567. if (eb->args->flags & I915_EXEC_BATCH_FIRST)
  568. return 0;
  569. else
  570. return eb->buffer_count - 1;
  571. }
  572. static int eb_select_context(struct i915_execbuffer *eb)
  573. {
  574. struct i915_gem_context *ctx;
  575. ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
  576. if (unlikely(!ctx))
  577. return -ENOENT;
  578. eb->ctx = ctx;
  579. eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
  580. eb->context_flags = 0;
  581. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  582. eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
  583. return 0;
  584. }
  585. static int eb_lookup_vmas(struct i915_execbuffer *eb)
  586. {
  587. struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
  588. struct drm_i915_gem_object *obj;
  589. unsigned int i;
  590. int err;
  591. if (unlikely(i915_gem_context_is_closed(eb->ctx)))
  592. return -ENOENT;
  593. if (unlikely(i915_gem_context_is_banned(eb->ctx)))
  594. return -EIO;
  595. INIT_LIST_HEAD(&eb->relocs);
  596. INIT_LIST_HEAD(&eb->unbound);
  597. for (i = 0; i < eb->buffer_count; i++) {
  598. u32 handle = eb->exec[i].handle;
  599. struct i915_lut_handle *lut;
  600. struct i915_vma *vma;
  601. vma = radix_tree_lookup(handles_vma, handle);
  602. if (likely(vma))
  603. goto add_vma;
  604. obj = i915_gem_object_lookup(eb->file, handle);
  605. if (unlikely(!obj)) {
  606. err = -ENOENT;
  607. goto err_vma;
  608. }
  609. vma = i915_vma_instance(obj, eb->vm, NULL);
  610. if (unlikely(IS_ERR(vma))) {
  611. err = PTR_ERR(vma);
  612. goto err_obj;
  613. }
  614. lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
  615. if (unlikely(!lut)) {
  616. err = -ENOMEM;
  617. goto err_obj;
  618. }
  619. err = radix_tree_insert(handles_vma, handle, vma);
  620. if (unlikely(err)) {
  621. kfree(lut);
  622. goto err_obj;
  623. }
  624. /* transfer ref to ctx */
  625. vma->open_count++;
  626. list_add(&lut->obj_link, &obj->lut_list);
  627. list_add(&lut->ctx_link, &eb->ctx->handles_list);
  628. lut->ctx = eb->ctx;
  629. lut->handle = handle;
  630. add_vma:
  631. err = eb_add_vma(eb, i, vma);
  632. if (unlikely(err))
  633. goto err_vma;
  634. GEM_BUG_ON(vma != eb->vma[i]);
  635. GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
  636. }
  637. /* take note of the batch buffer before we might reorder the lists */
  638. i = eb_batch_index(eb);
  639. eb->batch = eb->vma[i];
  640. GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
  641. /*
  642. * SNA is doing fancy tricks with compressing batch buffers, which leads
  643. * to negative relocation deltas. Usually that works out ok since the
  644. * relocate address is still positive, except when the batch is placed
  645. * very low in the GTT. Ensure this doesn't happen.
  646. *
  647. * Note that actual hangs have only been observed on gen7, but for
  648. * paranoia do it everywhere.
  649. */
  650. if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
  651. eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
  652. if (eb->reloc_cache.has_fence)
  653. eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
  654. eb->args->flags |= __EXEC_VALIDATED;
  655. return eb_reserve(eb);
  656. err_obj:
  657. i915_gem_object_put(obj);
  658. err_vma:
  659. eb->vma[i] = NULL;
  660. return err;
  661. }
  662. static struct i915_vma *
  663. eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
  664. {
  665. if (eb->lut_size < 0) {
  666. if (handle >= -eb->lut_size)
  667. return NULL;
  668. return eb->vma[handle];
  669. } else {
  670. struct hlist_head *head;
  671. struct i915_vma *vma;
  672. head = &eb->buckets[hash_32(handle, eb->lut_size)];
  673. hlist_for_each_entry(vma, head, exec_node) {
  674. if (vma->exec_handle == handle)
  675. return vma;
  676. }
  677. return NULL;
  678. }
  679. }
  680. static void eb_release_vmas(const struct i915_execbuffer *eb)
  681. {
  682. const unsigned int count = eb->buffer_count;
  683. unsigned int i;
  684. for (i = 0; i < count; i++) {
  685. struct i915_vma *vma = eb->vma[i];
  686. unsigned int flags = eb->flags[i];
  687. if (!vma)
  688. break;
  689. GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
  690. vma->exec_flags = NULL;
  691. eb->vma[i] = NULL;
  692. if (flags & __EXEC_OBJECT_HAS_PIN)
  693. __eb_unreserve_vma(vma, flags);
  694. if (flags & __EXEC_OBJECT_HAS_REF)
  695. i915_vma_put(vma);
  696. }
  697. }
  698. static void eb_reset_vmas(const struct i915_execbuffer *eb)
  699. {
  700. eb_release_vmas(eb);
  701. if (eb->lut_size > 0)
  702. memset(eb->buckets, 0,
  703. sizeof(struct hlist_head) << eb->lut_size);
  704. }
  705. static void eb_destroy(const struct i915_execbuffer *eb)
  706. {
  707. GEM_BUG_ON(eb->reloc_cache.rq);
  708. if (eb->lut_size > 0)
  709. kfree(eb->buckets);
  710. }
  711. static inline u64
  712. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  713. const struct i915_vma *target)
  714. {
  715. return gen8_canonical_addr((int)reloc->delta + target->node.start);
  716. }
  717. static void reloc_cache_init(struct reloc_cache *cache,
  718. struct drm_i915_private *i915)
  719. {
  720. cache->page = -1;
  721. cache->vaddr = 0;
  722. /* Must be a variable in the struct to allow GCC to unroll. */
  723. cache->gen = INTEL_GEN(i915);
  724. cache->has_llc = HAS_LLC(i915);
  725. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  726. cache->has_fence = cache->gen < 4;
  727. cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
  728. cache->node.allocated = false;
  729. cache->rq = NULL;
  730. cache->rq_size = 0;
  731. }
  732. static inline void *unmask_page(unsigned long p)
  733. {
  734. return (void *)(uintptr_t)(p & PAGE_MASK);
  735. }
  736. static inline unsigned int unmask_flags(unsigned long p)
  737. {
  738. return p & ~PAGE_MASK;
  739. }
  740. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  741. static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
  742. {
  743. struct drm_i915_private *i915 =
  744. container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
  745. return &i915->ggtt;
  746. }
  747. static void reloc_gpu_flush(struct reloc_cache *cache)
  748. {
  749. GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
  750. cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
  751. i915_gem_object_unpin_map(cache->rq->batch->obj);
  752. i915_gem_chipset_flush(cache->rq->i915);
  753. __i915_add_request(cache->rq, true);
  754. cache->rq = NULL;
  755. }
  756. static void reloc_cache_reset(struct reloc_cache *cache)
  757. {
  758. void *vaddr;
  759. if (cache->rq)
  760. reloc_gpu_flush(cache);
  761. if (!cache->vaddr)
  762. return;
  763. vaddr = unmask_page(cache->vaddr);
  764. if (cache->vaddr & KMAP) {
  765. if (cache->vaddr & CLFLUSH_AFTER)
  766. mb();
  767. kunmap_atomic(vaddr);
  768. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  769. } else {
  770. wmb();
  771. io_mapping_unmap_atomic((void __iomem *)vaddr);
  772. if (cache->node.allocated) {
  773. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  774. ggtt->base.clear_range(&ggtt->base,
  775. cache->node.start,
  776. cache->node.size);
  777. drm_mm_remove_node(&cache->node);
  778. } else {
  779. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  780. }
  781. }
  782. cache->vaddr = 0;
  783. cache->page = -1;
  784. }
  785. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  786. struct reloc_cache *cache,
  787. unsigned long page)
  788. {
  789. void *vaddr;
  790. if (cache->vaddr) {
  791. kunmap_atomic(unmask_page(cache->vaddr));
  792. } else {
  793. unsigned int flushes;
  794. int err;
  795. err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
  796. if (err)
  797. return ERR_PTR(err);
  798. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  799. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  800. cache->vaddr = flushes | KMAP;
  801. cache->node.mm = (void *)obj;
  802. if (flushes)
  803. mb();
  804. }
  805. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
  806. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  807. cache->page = page;
  808. return vaddr;
  809. }
  810. static void *reloc_iomap(struct drm_i915_gem_object *obj,
  811. struct reloc_cache *cache,
  812. unsigned long page)
  813. {
  814. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  815. unsigned long offset;
  816. void *vaddr;
  817. if (cache->vaddr) {
  818. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  819. } else {
  820. struct i915_vma *vma;
  821. int err;
  822. if (use_cpu_reloc(cache, obj))
  823. return NULL;
  824. err = i915_gem_object_set_to_gtt_domain(obj, true);
  825. if (err)
  826. return ERR_PTR(err);
  827. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  828. PIN_MAPPABLE |
  829. PIN_NONBLOCK |
  830. PIN_NONFAULT);
  831. if (IS_ERR(vma)) {
  832. memset(&cache->node, 0, sizeof(cache->node));
  833. err = drm_mm_insert_node_in_range
  834. (&ggtt->base.mm, &cache->node,
  835. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  836. 0, ggtt->mappable_end,
  837. DRM_MM_INSERT_LOW);
  838. if (err) /* no inactive aperture space, use cpu reloc */
  839. return NULL;
  840. } else {
  841. err = i915_vma_put_fence(vma);
  842. if (err) {
  843. i915_vma_unpin(vma);
  844. return ERR_PTR(err);
  845. }
  846. cache->node.start = vma->node.start;
  847. cache->node.mm = (void *)vma;
  848. }
  849. }
  850. offset = cache->node.start;
  851. if (cache->node.allocated) {
  852. wmb();
  853. ggtt->base.insert_page(&ggtt->base,
  854. i915_gem_object_get_dma_address(obj, page),
  855. offset, I915_CACHE_NONE, 0);
  856. } else {
  857. offset += page << PAGE_SHIFT;
  858. }
  859. vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
  860. offset);
  861. cache->page = page;
  862. cache->vaddr = (unsigned long)vaddr;
  863. return vaddr;
  864. }
  865. static void *reloc_vaddr(struct drm_i915_gem_object *obj,
  866. struct reloc_cache *cache,
  867. unsigned long page)
  868. {
  869. void *vaddr;
  870. if (cache->page == page) {
  871. vaddr = unmask_page(cache->vaddr);
  872. } else {
  873. vaddr = NULL;
  874. if ((cache->vaddr & KMAP) == 0)
  875. vaddr = reloc_iomap(obj, cache, page);
  876. if (!vaddr)
  877. vaddr = reloc_kmap(obj, cache, page);
  878. }
  879. return vaddr;
  880. }
  881. static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  882. {
  883. if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
  884. if (flushes & CLFLUSH_BEFORE) {
  885. clflushopt(addr);
  886. mb();
  887. }
  888. *addr = value;
  889. /*
  890. * Writes to the same cacheline are serialised by the CPU
  891. * (including clflush). On the write path, we only require
  892. * that it hits memory in an orderly fashion and place
  893. * mb barriers at the start and end of the relocation phase
  894. * to ensure ordering of clflush wrt to the system.
  895. */
  896. if (flushes & CLFLUSH_AFTER)
  897. clflushopt(addr);
  898. } else
  899. *addr = value;
  900. }
  901. static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
  902. struct i915_vma *vma,
  903. unsigned int len)
  904. {
  905. struct reloc_cache *cache = &eb->reloc_cache;
  906. struct drm_i915_gem_object *obj;
  907. struct drm_i915_gem_request *rq;
  908. struct i915_vma *batch;
  909. u32 *cmd;
  910. int err;
  911. GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
  912. obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
  913. if (IS_ERR(obj))
  914. return PTR_ERR(obj);
  915. cmd = i915_gem_object_pin_map(obj,
  916. cache->has_llc ?
  917. I915_MAP_FORCE_WB :
  918. I915_MAP_FORCE_WC);
  919. i915_gem_object_unpin_pages(obj);
  920. if (IS_ERR(cmd))
  921. return PTR_ERR(cmd);
  922. err = i915_gem_object_set_to_wc_domain(obj, false);
  923. if (err)
  924. goto err_unmap;
  925. batch = i915_vma_instance(obj, vma->vm, NULL);
  926. if (IS_ERR(batch)) {
  927. err = PTR_ERR(batch);
  928. goto err_unmap;
  929. }
  930. err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
  931. if (err)
  932. goto err_unmap;
  933. rq = i915_gem_request_alloc(eb->engine, eb->ctx);
  934. if (IS_ERR(rq)) {
  935. err = PTR_ERR(rq);
  936. goto err_unpin;
  937. }
  938. err = i915_gem_request_await_object(rq, vma->obj, true);
  939. if (err)
  940. goto err_request;
  941. err = eb->engine->emit_bb_start(rq,
  942. batch->node.start, PAGE_SIZE,
  943. cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
  944. if (err)
  945. goto err_request;
  946. GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
  947. i915_vma_move_to_active(batch, rq, 0);
  948. reservation_object_lock(batch->resv, NULL);
  949. reservation_object_add_excl_fence(batch->resv, &rq->fence);
  950. reservation_object_unlock(batch->resv);
  951. i915_vma_unpin(batch);
  952. i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
  953. reservation_object_lock(vma->resv, NULL);
  954. reservation_object_add_excl_fence(vma->resv, &rq->fence);
  955. reservation_object_unlock(vma->resv);
  956. rq->batch = batch;
  957. cache->rq = rq;
  958. cache->rq_cmd = cmd;
  959. cache->rq_size = 0;
  960. /* Return with batch mapping (cmd) still pinned */
  961. return 0;
  962. err_request:
  963. i915_add_request(rq);
  964. err_unpin:
  965. i915_vma_unpin(batch);
  966. err_unmap:
  967. i915_gem_object_unpin_map(obj);
  968. return err;
  969. }
  970. static u32 *reloc_gpu(struct i915_execbuffer *eb,
  971. struct i915_vma *vma,
  972. unsigned int len)
  973. {
  974. struct reloc_cache *cache = &eb->reloc_cache;
  975. u32 *cmd;
  976. if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
  977. reloc_gpu_flush(cache);
  978. if (unlikely(!cache->rq)) {
  979. int err;
  980. /* If we need to copy for the cmdparser, we will stall anyway */
  981. if (eb_use_cmdparser(eb))
  982. return ERR_PTR(-EWOULDBLOCK);
  983. if (!intel_engine_can_store_dword(eb->engine))
  984. return ERR_PTR(-ENODEV);
  985. err = __reloc_gpu_alloc(eb, vma, len);
  986. if (unlikely(err))
  987. return ERR_PTR(err);
  988. }
  989. cmd = cache->rq_cmd + cache->rq_size;
  990. cache->rq_size += len;
  991. return cmd;
  992. }
  993. static u64
  994. relocate_entry(struct i915_vma *vma,
  995. const struct drm_i915_gem_relocation_entry *reloc,
  996. struct i915_execbuffer *eb,
  997. const struct i915_vma *target)
  998. {
  999. u64 offset = reloc->offset;
  1000. u64 target_offset = relocation_target(reloc, target);
  1001. bool wide = eb->reloc_cache.use_64bit_reloc;
  1002. void *vaddr;
  1003. if (!eb->reloc_cache.vaddr &&
  1004. (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
  1005. !reservation_object_test_signaled_rcu(vma->resv, true))) {
  1006. const unsigned int gen = eb->reloc_cache.gen;
  1007. unsigned int len;
  1008. u32 *batch;
  1009. u64 addr;
  1010. if (wide)
  1011. len = offset & 7 ? 8 : 5;
  1012. else if (gen >= 4)
  1013. len = 4;
  1014. else
  1015. len = 3;
  1016. batch = reloc_gpu(eb, vma, len);
  1017. if (IS_ERR(batch))
  1018. goto repeat;
  1019. addr = gen8_canonical_addr(vma->node.start + offset);
  1020. if (wide) {
  1021. if (offset & 7) {
  1022. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1023. *batch++ = lower_32_bits(addr);
  1024. *batch++ = upper_32_bits(addr);
  1025. *batch++ = lower_32_bits(target_offset);
  1026. addr = gen8_canonical_addr(addr + 4);
  1027. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1028. *batch++ = lower_32_bits(addr);
  1029. *batch++ = upper_32_bits(addr);
  1030. *batch++ = upper_32_bits(target_offset);
  1031. } else {
  1032. *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
  1033. *batch++ = lower_32_bits(addr);
  1034. *batch++ = upper_32_bits(addr);
  1035. *batch++ = lower_32_bits(target_offset);
  1036. *batch++ = upper_32_bits(target_offset);
  1037. }
  1038. } else if (gen >= 6) {
  1039. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1040. *batch++ = 0;
  1041. *batch++ = addr;
  1042. *batch++ = target_offset;
  1043. } else if (gen >= 4) {
  1044. *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
  1045. *batch++ = 0;
  1046. *batch++ = addr;
  1047. *batch++ = target_offset;
  1048. } else {
  1049. *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
  1050. *batch++ = addr;
  1051. *batch++ = target_offset;
  1052. }
  1053. goto out;
  1054. }
  1055. repeat:
  1056. vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
  1057. if (IS_ERR(vaddr))
  1058. return PTR_ERR(vaddr);
  1059. clflush_write32(vaddr + offset_in_page(offset),
  1060. lower_32_bits(target_offset),
  1061. eb->reloc_cache.vaddr);
  1062. if (wide) {
  1063. offset += sizeof(u32);
  1064. target_offset >>= 32;
  1065. wide = false;
  1066. goto repeat;
  1067. }
  1068. out:
  1069. return target->node.start | UPDATE;
  1070. }
  1071. static u64
  1072. eb_relocate_entry(struct i915_execbuffer *eb,
  1073. struct i915_vma *vma,
  1074. const struct drm_i915_gem_relocation_entry *reloc)
  1075. {
  1076. struct i915_vma *target;
  1077. int err;
  1078. /* we've already hold a reference to all valid objects */
  1079. target = eb_get_vma(eb, reloc->target_handle);
  1080. if (unlikely(!target))
  1081. return -ENOENT;
  1082. /* Validate that the target is in a valid r/w GPU domain */
  1083. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  1084. DRM_DEBUG("reloc with multiple write domains: "
  1085. "target %d offset %d "
  1086. "read %08x write %08x",
  1087. reloc->target_handle,
  1088. (int) reloc->offset,
  1089. reloc->read_domains,
  1090. reloc->write_domain);
  1091. return -EINVAL;
  1092. }
  1093. if (unlikely((reloc->write_domain | reloc->read_domains)
  1094. & ~I915_GEM_GPU_DOMAINS)) {
  1095. DRM_DEBUG("reloc with read/write non-GPU domains: "
  1096. "target %d offset %d "
  1097. "read %08x write %08x",
  1098. reloc->target_handle,
  1099. (int) reloc->offset,
  1100. reloc->read_domains,
  1101. reloc->write_domain);
  1102. return -EINVAL;
  1103. }
  1104. if (reloc->write_domain) {
  1105. *target->exec_flags |= EXEC_OBJECT_WRITE;
  1106. /*
  1107. * Sandybridge PPGTT errata: We need a global gtt mapping
  1108. * for MI and pipe_control writes because the gpu doesn't
  1109. * properly redirect them through the ppgtt for non_secure
  1110. * batchbuffers.
  1111. */
  1112. if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  1113. IS_GEN6(eb->i915)) {
  1114. err = i915_vma_bind(target, target->obj->cache_level,
  1115. PIN_GLOBAL);
  1116. if (WARN_ONCE(err,
  1117. "Unexpected failure to bind target VMA!"))
  1118. return err;
  1119. }
  1120. }
  1121. /*
  1122. * If the relocation already has the right value in it, no
  1123. * more work needs to be done.
  1124. */
  1125. if (!DBG_FORCE_RELOC &&
  1126. gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
  1127. return 0;
  1128. /* Check that the relocation address is valid... */
  1129. if (unlikely(reloc->offset >
  1130. vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
  1131. DRM_DEBUG("Relocation beyond object bounds: "
  1132. "target %d offset %d size %d.\n",
  1133. reloc->target_handle,
  1134. (int)reloc->offset,
  1135. (int)vma->size);
  1136. return -EINVAL;
  1137. }
  1138. if (unlikely(reloc->offset & 3)) {
  1139. DRM_DEBUG("Relocation not 4-byte aligned: "
  1140. "target %d offset %d.\n",
  1141. reloc->target_handle,
  1142. (int)reloc->offset);
  1143. return -EINVAL;
  1144. }
  1145. /*
  1146. * If we write into the object, we need to force the synchronisation
  1147. * barrier, either with an asynchronous clflush or if we executed the
  1148. * patching using the GPU (though that should be serialised by the
  1149. * timeline). To be completely sure, and since we are required to
  1150. * do relocations we are already stalling, disable the user's opt
  1151. * out of our synchronisation.
  1152. */
  1153. *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
  1154. /* and update the user's relocation entry */
  1155. return relocate_entry(vma, reloc, eb, target);
  1156. }
  1157. static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
  1158. {
  1159. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  1160. struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
  1161. struct drm_i915_gem_relocation_entry __user *urelocs;
  1162. const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  1163. unsigned int remain;
  1164. urelocs = u64_to_user_ptr(entry->relocs_ptr);
  1165. remain = entry->relocation_count;
  1166. if (unlikely(remain > N_RELOC(ULONG_MAX)))
  1167. return -EINVAL;
  1168. /*
  1169. * We must check that the entire relocation array is safe
  1170. * to read. However, if the array is not writable the user loses
  1171. * the updated relocation values.
  1172. */
  1173. if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
  1174. return -EFAULT;
  1175. do {
  1176. struct drm_i915_gem_relocation_entry *r = stack;
  1177. unsigned int count =
  1178. min_t(unsigned int, remain, ARRAY_SIZE(stack));
  1179. unsigned int copied;
  1180. /*
  1181. * This is the fast path and we cannot handle a pagefault
  1182. * whilst holding the struct mutex lest the user pass in the
  1183. * relocations contained within a mmaped bo. For in such a case
  1184. * we, the page fault handler would call i915_gem_fault() and
  1185. * we would try to acquire the struct mutex again. Obviously
  1186. * this is bad and so lockdep complains vehemently.
  1187. */
  1188. pagefault_disable();
  1189. copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
  1190. pagefault_enable();
  1191. if (unlikely(copied)) {
  1192. remain = -EFAULT;
  1193. goto out;
  1194. }
  1195. remain -= count;
  1196. do {
  1197. u64 offset = eb_relocate_entry(eb, vma, r);
  1198. if (likely(offset == 0)) {
  1199. } else if ((s64)offset < 0) {
  1200. remain = (int)offset;
  1201. goto out;
  1202. } else {
  1203. /*
  1204. * Note that reporting an error now
  1205. * leaves everything in an inconsistent
  1206. * state as we have *already* changed
  1207. * the relocation value inside the
  1208. * object. As we have not changed the
  1209. * reloc.presumed_offset or will not
  1210. * change the execobject.offset, on the
  1211. * call we may not rewrite the value
  1212. * inside the object, leaving it
  1213. * dangling and causing a GPU hang. Unless
  1214. * userspace dynamically rebuilds the
  1215. * relocations on each execbuf rather than
  1216. * presume a static tree.
  1217. *
  1218. * We did previously check if the relocations
  1219. * were writable (access_ok), an error now
  1220. * would be a strange race with mprotect,
  1221. * having already demonstrated that we
  1222. * can read from this userspace address.
  1223. */
  1224. offset = gen8_canonical_addr(offset & ~UPDATE);
  1225. __put_user(offset,
  1226. &urelocs[r-stack].presumed_offset);
  1227. }
  1228. } while (r++, --count);
  1229. urelocs += ARRAY_SIZE(stack);
  1230. } while (remain);
  1231. out:
  1232. reloc_cache_reset(&eb->reloc_cache);
  1233. return remain;
  1234. }
  1235. static int
  1236. eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
  1237. {
  1238. const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  1239. struct drm_i915_gem_relocation_entry *relocs =
  1240. u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1241. unsigned int i;
  1242. int err;
  1243. for (i = 0; i < entry->relocation_count; i++) {
  1244. u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
  1245. if ((s64)offset < 0) {
  1246. err = (int)offset;
  1247. goto err;
  1248. }
  1249. }
  1250. err = 0;
  1251. err:
  1252. reloc_cache_reset(&eb->reloc_cache);
  1253. return err;
  1254. }
  1255. static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
  1256. {
  1257. const char __user *addr, *end;
  1258. unsigned long size;
  1259. char __maybe_unused c;
  1260. size = entry->relocation_count;
  1261. if (size == 0)
  1262. return 0;
  1263. if (size > N_RELOC(ULONG_MAX))
  1264. return -EINVAL;
  1265. addr = u64_to_user_ptr(entry->relocs_ptr);
  1266. size *= sizeof(struct drm_i915_gem_relocation_entry);
  1267. if (!access_ok(VERIFY_READ, addr, size))
  1268. return -EFAULT;
  1269. end = addr + size;
  1270. for (; addr < end; addr += PAGE_SIZE) {
  1271. int err = __get_user(c, addr);
  1272. if (err)
  1273. return err;
  1274. }
  1275. return __get_user(c, end - 1);
  1276. }
  1277. static int eb_copy_relocations(const struct i915_execbuffer *eb)
  1278. {
  1279. const unsigned int count = eb->buffer_count;
  1280. unsigned int i;
  1281. int err;
  1282. for (i = 0; i < count; i++) {
  1283. const unsigned int nreloc = eb->exec[i].relocation_count;
  1284. struct drm_i915_gem_relocation_entry __user *urelocs;
  1285. struct drm_i915_gem_relocation_entry *relocs;
  1286. unsigned long size;
  1287. unsigned long copied;
  1288. if (nreloc == 0)
  1289. continue;
  1290. err = check_relocations(&eb->exec[i]);
  1291. if (err)
  1292. goto err;
  1293. urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
  1294. size = nreloc * sizeof(*relocs);
  1295. relocs = kvmalloc_array(size, 1, GFP_KERNEL);
  1296. if (!relocs) {
  1297. kvfree(relocs);
  1298. err = -ENOMEM;
  1299. goto err;
  1300. }
  1301. /* copy_from_user is limited to < 4GiB */
  1302. copied = 0;
  1303. do {
  1304. unsigned int len =
  1305. min_t(u64, BIT_ULL(31), size - copied);
  1306. if (__copy_from_user((char *)relocs + copied,
  1307. (char __user *)urelocs + copied,
  1308. len)) {
  1309. kvfree(relocs);
  1310. err = -EFAULT;
  1311. goto err;
  1312. }
  1313. copied += len;
  1314. } while (copied < size);
  1315. /*
  1316. * As we do not update the known relocation offsets after
  1317. * relocating (due to the complexities in lock handling),
  1318. * we need to mark them as invalid now so that we force the
  1319. * relocation processing next time. Just in case the target
  1320. * object is evicted and then rebound into its old
  1321. * presumed_offset before the next execbuffer - if that
  1322. * happened we would make the mistake of assuming that the
  1323. * relocations were valid.
  1324. */
  1325. user_access_begin();
  1326. for (copied = 0; copied < nreloc; copied++)
  1327. unsafe_put_user(-1,
  1328. &urelocs[copied].presumed_offset,
  1329. end_user);
  1330. end_user:
  1331. user_access_end();
  1332. eb->exec[i].relocs_ptr = (uintptr_t)relocs;
  1333. }
  1334. return 0;
  1335. err:
  1336. while (i--) {
  1337. struct drm_i915_gem_relocation_entry *relocs =
  1338. u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
  1339. if (eb->exec[i].relocation_count)
  1340. kvfree(relocs);
  1341. }
  1342. return err;
  1343. }
  1344. static int eb_prefault_relocations(const struct i915_execbuffer *eb)
  1345. {
  1346. const unsigned int count = eb->buffer_count;
  1347. unsigned int i;
  1348. if (unlikely(i915_modparams.prefault_disable))
  1349. return 0;
  1350. for (i = 0; i < count; i++) {
  1351. int err;
  1352. err = check_relocations(&eb->exec[i]);
  1353. if (err)
  1354. return err;
  1355. }
  1356. return 0;
  1357. }
  1358. static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
  1359. {
  1360. struct drm_device *dev = &eb->i915->drm;
  1361. bool have_copy = false;
  1362. struct i915_vma *vma;
  1363. int err = 0;
  1364. repeat:
  1365. if (signal_pending(current)) {
  1366. err = -ERESTARTSYS;
  1367. goto out;
  1368. }
  1369. /* We may process another execbuffer during the unlock... */
  1370. eb_reset_vmas(eb);
  1371. mutex_unlock(&dev->struct_mutex);
  1372. /*
  1373. * We take 3 passes through the slowpatch.
  1374. *
  1375. * 1 - we try to just prefault all the user relocation entries and
  1376. * then attempt to reuse the atomic pagefault disabled fast path again.
  1377. *
  1378. * 2 - we copy the user entries to a local buffer here outside of the
  1379. * local and allow ourselves to wait upon any rendering before
  1380. * relocations
  1381. *
  1382. * 3 - we already have a local copy of the relocation entries, but
  1383. * were interrupted (EAGAIN) whilst waiting for the objects, try again.
  1384. */
  1385. if (!err) {
  1386. err = eb_prefault_relocations(eb);
  1387. } else if (!have_copy) {
  1388. err = eb_copy_relocations(eb);
  1389. have_copy = err == 0;
  1390. } else {
  1391. cond_resched();
  1392. err = 0;
  1393. }
  1394. if (err) {
  1395. mutex_lock(&dev->struct_mutex);
  1396. goto out;
  1397. }
  1398. /* A frequent cause for EAGAIN are currently unavailable client pages */
  1399. flush_workqueue(eb->i915->mm.userptr_wq);
  1400. err = i915_mutex_lock_interruptible(dev);
  1401. if (err) {
  1402. mutex_lock(&dev->struct_mutex);
  1403. goto out;
  1404. }
  1405. /* reacquire the objects */
  1406. err = eb_lookup_vmas(eb);
  1407. if (err)
  1408. goto err;
  1409. GEM_BUG_ON(!eb->batch);
  1410. list_for_each_entry(vma, &eb->relocs, reloc_link) {
  1411. if (!have_copy) {
  1412. pagefault_disable();
  1413. err = eb_relocate_vma(eb, vma);
  1414. pagefault_enable();
  1415. if (err)
  1416. goto repeat;
  1417. } else {
  1418. err = eb_relocate_vma_slow(eb, vma);
  1419. if (err)
  1420. goto err;
  1421. }
  1422. }
  1423. /*
  1424. * Leave the user relocations as are, this is the painfully slow path,
  1425. * and we want to avoid the complication of dropping the lock whilst
  1426. * having buffers reserved in the aperture and so causing spurious
  1427. * ENOSPC for random operations.
  1428. */
  1429. err:
  1430. if (err == -EAGAIN)
  1431. goto repeat;
  1432. out:
  1433. if (have_copy) {
  1434. const unsigned int count = eb->buffer_count;
  1435. unsigned int i;
  1436. for (i = 0; i < count; i++) {
  1437. const struct drm_i915_gem_exec_object2 *entry =
  1438. &eb->exec[i];
  1439. struct drm_i915_gem_relocation_entry *relocs;
  1440. if (!entry->relocation_count)
  1441. continue;
  1442. relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1443. kvfree(relocs);
  1444. }
  1445. }
  1446. return err;
  1447. }
  1448. static int eb_relocate(struct i915_execbuffer *eb)
  1449. {
  1450. if (eb_lookup_vmas(eb))
  1451. goto slow;
  1452. /* The objects are in their final locations, apply the relocations. */
  1453. if (eb->args->flags & __EXEC_HAS_RELOC) {
  1454. struct i915_vma *vma;
  1455. list_for_each_entry(vma, &eb->relocs, reloc_link) {
  1456. if (eb_relocate_vma(eb, vma))
  1457. goto slow;
  1458. }
  1459. }
  1460. return 0;
  1461. slow:
  1462. return eb_relocate_slow(eb);
  1463. }
  1464. static void eb_export_fence(struct i915_vma *vma,
  1465. struct drm_i915_gem_request *req,
  1466. unsigned int flags)
  1467. {
  1468. struct reservation_object *resv = vma->resv;
  1469. /*
  1470. * Ignore errors from failing to allocate the new fence, we can't
  1471. * handle an error right now. Worst case should be missed
  1472. * synchronisation leading to rendering corruption.
  1473. */
  1474. reservation_object_lock(resv, NULL);
  1475. if (flags & EXEC_OBJECT_WRITE)
  1476. reservation_object_add_excl_fence(resv, &req->fence);
  1477. else if (reservation_object_reserve_shared(resv) == 0)
  1478. reservation_object_add_shared_fence(resv, &req->fence);
  1479. reservation_object_unlock(resv);
  1480. }
  1481. static int eb_move_to_gpu(struct i915_execbuffer *eb)
  1482. {
  1483. const unsigned int count = eb->buffer_count;
  1484. unsigned int i;
  1485. int err;
  1486. for (i = 0; i < count; i++) {
  1487. unsigned int flags = eb->flags[i];
  1488. struct i915_vma *vma = eb->vma[i];
  1489. struct drm_i915_gem_object *obj = vma->obj;
  1490. if (flags & EXEC_OBJECT_CAPTURE) {
  1491. struct i915_gem_capture_list *capture;
  1492. capture = kmalloc(sizeof(*capture), GFP_KERNEL);
  1493. if (unlikely(!capture))
  1494. return -ENOMEM;
  1495. capture->next = eb->request->capture_list;
  1496. capture->vma = eb->vma[i];
  1497. eb->request->capture_list = capture;
  1498. }
  1499. /*
  1500. * If the GPU is not _reading_ through the CPU cache, we need
  1501. * to make sure that any writes (both previous GPU writes from
  1502. * before a change in snooping levels and normal CPU writes)
  1503. * caught in that cache are flushed to main memory.
  1504. *
  1505. * We want to say
  1506. * obj->cache_dirty &&
  1507. * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
  1508. * but gcc's optimiser doesn't handle that as well and emits
  1509. * two jumps instead of one. Maybe one day...
  1510. */
  1511. if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
  1512. if (i915_gem_clflush_object(obj, 0))
  1513. flags &= ~EXEC_OBJECT_ASYNC;
  1514. }
  1515. if (flags & EXEC_OBJECT_ASYNC)
  1516. continue;
  1517. err = i915_gem_request_await_object
  1518. (eb->request, obj, flags & EXEC_OBJECT_WRITE);
  1519. if (err)
  1520. return err;
  1521. }
  1522. for (i = 0; i < count; i++) {
  1523. unsigned int flags = eb->flags[i];
  1524. struct i915_vma *vma = eb->vma[i];
  1525. i915_vma_move_to_active(vma, eb->request, flags);
  1526. eb_export_fence(vma, eb->request, flags);
  1527. __eb_unreserve_vma(vma, flags);
  1528. vma->exec_flags = NULL;
  1529. if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
  1530. i915_vma_put(vma);
  1531. }
  1532. eb->exec = NULL;
  1533. /* Unconditionally flush any chipset caches (for streaming writes). */
  1534. i915_gem_chipset_flush(eb->i915);
  1535. return 0;
  1536. }
  1537. static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  1538. {
  1539. if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
  1540. return false;
  1541. /* Kernel clipping was a DRI1 misfeature */
  1542. if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
  1543. if (exec->num_cliprects || exec->cliprects_ptr)
  1544. return false;
  1545. }
  1546. if (exec->DR4 == 0xffffffff) {
  1547. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  1548. exec->DR4 = 0;
  1549. }
  1550. if (exec->DR1 || exec->DR4)
  1551. return false;
  1552. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  1553. return false;
  1554. return true;
  1555. }
  1556. void i915_vma_move_to_active(struct i915_vma *vma,
  1557. struct drm_i915_gem_request *req,
  1558. unsigned int flags)
  1559. {
  1560. struct drm_i915_gem_object *obj = vma->obj;
  1561. const unsigned int idx = req->engine->id;
  1562. lockdep_assert_held(&req->i915->drm.struct_mutex);
  1563. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1564. /*
  1565. * Add a reference if we're newly entering the active list.
  1566. * The order in which we add operations to the retirement queue is
  1567. * vital here: mark_active adds to the start of the callback list,
  1568. * such that subsequent callbacks are called first. Therefore we
  1569. * add the active reference first and queue for it to be dropped
  1570. * *last*.
  1571. */
  1572. if (!i915_vma_is_active(vma))
  1573. obj->active_count++;
  1574. i915_vma_set_active(vma, idx);
  1575. i915_gem_active_set(&vma->last_read[idx], req);
  1576. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1577. obj->base.write_domain = 0;
  1578. if (flags & EXEC_OBJECT_WRITE) {
  1579. obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
  1580. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1581. i915_gem_active_set(&obj->frontbuffer_write, req);
  1582. obj->base.read_domains = 0;
  1583. }
  1584. obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
  1585. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1586. i915_gem_active_set(&vma->last_fence, req);
  1587. }
  1588. static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1589. {
  1590. u32 *cs;
  1591. int i;
  1592. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1593. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1594. return -EINVAL;
  1595. }
  1596. cs = intel_ring_begin(req, 4 * 2 + 2);
  1597. if (IS_ERR(cs))
  1598. return PTR_ERR(cs);
  1599. *cs++ = MI_LOAD_REGISTER_IMM(4);
  1600. for (i = 0; i < 4; i++) {
  1601. *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
  1602. *cs++ = 0;
  1603. }
  1604. *cs++ = MI_NOOP;
  1605. intel_ring_advance(req, cs);
  1606. return 0;
  1607. }
  1608. static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
  1609. {
  1610. struct drm_i915_gem_object *shadow_batch_obj;
  1611. struct i915_vma *vma;
  1612. int err;
  1613. shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
  1614. PAGE_ALIGN(eb->batch_len));
  1615. if (IS_ERR(shadow_batch_obj))
  1616. return ERR_CAST(shadow_batch_obj);
  1617. err = intel_engine_cmd_parser(eb->engine,
  1618. eb->batch->obj,
  1619. shadow_batch_obj,
  1620. eb->batch_start_offset,
  1621. eb->batch_len,
  1622. is_master);
  1623. if (err) {
  1624. if (err == -EACCES) /* unhandled chained batch */
  1625. vma = NULL;
  1626. else
  1627. vma = ERR_PTR(err);
  1628. goto out;
  1629. }
  1630. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1631. if (IS_ERR(vma))
  1632. goto out;
  1633. eb->vma[eb->buffer_count] = i915_vma_get(vma);
  1634. eb->flags[eb->buffer_count] =
  1635. __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
  1636. vma->exec_flags = &eb->flags[eb->buffer_count];
  1637. eb->buffer_count++;
  1638. out:
  1639. i915_gem_object_unpin_pages(shadow_batch_obj);
  1640. return vma;
  1641. }
  1642. static void
  1643. add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
  1644. {
  1645. req->file_priv = file->driver_priv;
  1646. list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
  1647. }
  1648. static int eb_submit(struct i915_execbuffer *eb)
  1649. {
  1650. int err;
  1651. err = eb_move_to_gpu(eb);
  1652. if (err)
  1653. return err;
  1654. if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1655. err = i915_reset_gen7_sol_offsets(eb->request);
  1656. if (err)
  1657. return err;
  1658. }
  1659. err = eb->engine->emit_bb_start(eb->request,
  1660. eb->batch->node.start +
  1661. eb->batch_start_offset,
  1662. eb->batch_len,
  1663. eb->batch_flags);
  1664. if (err)
  1665. return err;
  1666. return 0;
  1667. }
  1668. /**
  1669. * Find one BSD ring to dispatch the corresponding BSD command.
  1670. * The engine index is returned.
  1671. */
  1672. static unsigned int
  1673. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1674. struct drm_file *file)
  1675. {
  1676. struct drm_i915_file_private *file_priv = file->driver_priv;
  1677. /* Check whether the file_priv has already selected one ring. */
  1678. if ((int)file_priv->bsd_engine < 0)
  1679. file_priv->bsd_engine = atomic_fetch_xor(1,
  1680. &dev_priv->mm.bsd_engine_dispatch_index);
  1681. return file_priv->bsd_engine;
  1682. }
  1683. #define I915_USER_RINGS (4)
  1684. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1685. [I915_EXEC_DEFAULT] = RCS,
  1686. [I915_EXEC_RENDER] = RCS,
  1687. [I915_EXEC_BLT] = BCS,
  1688. [I915_EXEC_BSD] = VCS,
  1689. [I915_EXEC_VEBOX] = VECS
  1690. };
  1691. static struct intel_engine_cs *
  1692. eb_select_engine(struct drm_i915_private *dev_priv,
  1693. struct drm_file *file,
  1694. struct drm_i915_gem_execbuffer2 *args)
  1695. {
  1696. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1697. struct intel_engine_cs *engine;
  1698. if (user_ring_id > I915_USER_RINGS) {
  1699. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1700. return NULL;
  1701. }
  1702. if ((user_ring_id != I915_EXEC_BSD) &&
  1703. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1704. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1705. "bsd dispatch flags: %d\n", (int)(args->flags));
  1706. return NULL;
  1707. }
  1708. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1709. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1710. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1711. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1712. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1713. bsd_idx <= I915_EXEC_BSD_RING2) {
  1714. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1715. bsd_idx--;
  1716. } else {
  1717. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1718. bsd_idx);
  1719. return NULL;
  1720. }
  1721. engine = dev_priv->engine[_VCS(bsd_idx)];
  1722. } else {
  1723. engine = dev_priv->engine[user_ring_map[user_ring_id]];
  1724. }
  1725. if (!engine) {
  1726. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1727. return NULL;
  1728. }
  1729. return engine;
  1730. }
  1731. static void
  1732. __free_fence_array(struct drm_syncobj **fences, unsigned int n)
  1733. {
  1734. while (n--)
  1735. drm_syncobj_put(ptr_mask_bits(fences[n], 2));
  1736. kvfree(fences);
  1737. }
  1738. static struct drm_syncobj **
  1739. get_fence_array(struct drm_i915_gem_execbuffer2 *args,
  1740. struct drm_file *file)
  1741. {
  1742. const unsigned long nfences = args->num_cliprects;
  1743. struct drm_i915_gem_exec_fence __user *user;
  1744. struct drm_syncobj **fences;
  1745. unsigned long n;
  1746. int err;
  1747. if (!(args->flags & I915_EXEC_FENCE_ARRAY))
  1748. return NULL;
  1749. /* Check multiplication overflow for access_ok() and kvmalloc_array() */
  1750. BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
  1751. if (nfences > min_t(unsigned long,
  1752. ULONG_MAX / sizeof(*user),
  1753. SIZE_MAX / sizeof(*fences)))
  1754. return ERR_PTR(-EINVAL);
  1755. user = u64_to_user_ptr(args->cliprects_ptr);
  1756. if (!access_ok(VERIFY_READ, user, nfences * sizeof(*user)))
  1757. return ERR_PTR(-EFAULT);
  1758. fences = kvmalloc_array(nfences, sizeof(*fences),
  1759. __GFP_NOWARN | GFP_KERNEL);
  1760. if (!fences)
  1761. return ERR_PTR(-ENOMEM);
  1762. for (n = 0; n < nfences; n++) {
  1763. struct drm_i915_gem_exec_fence fence;
  1764. struct drm_syncobj *syncobj;
  1765. if (__copy_from_user(&fence, user++, sizeof(fence))) {
  1766. err = -EFAULT;
  1767. goto err;
  1768. }
  1769. if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) {
  1770. err = -EINVAL;
  1771. goto err;
  1772. }
  1773. syncobj = drm_syncobj_find(file, fence.handle);
  1774. if (!syncobj) {
  1775. DRM_DEBUG("Invalid syncobj handle provided\n");
  1776. err = -ENOENT;
  1777. goto err;
  1778. }
  1779. BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
  1780. ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
  1781. fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
  1782. }
  1783. return fences;
  1784. err:
  1785. __free_fence_array(fences, n);
  1786. return ERR_PTR(err);
  1787. }
  1788. static void
  1789. put_fence_array(struct drm_i915_gem_execbuffer2 *args,
  1790. struct drm_syncobj **fences)
  1791. {
  1792. if (fences)
  1793. __free_fence_array(fences, args->num_cliprects);
  1794. }
  1795. static int
  1796. await_fence_array(struct i915_execbuffer *eb,
  1797. struct drm_syncobj **fences)
  1798. {
  1799. const unsigned int nfences = eb->args->num_cliprects;
  1800. unsigned int n;
  1801. int err;
  1802. for (n = 0; n < nfences; n++) {
  1803. struct drm_syncobj *syncobj;
  1804. struct dma_fence *fence;
  1805. unsigned int flags;
  1806. syncobj = ptr_unpack_bits(fences[n], &flags, 2);
  1807. if (!(flags & I915_EXEC_FENCE_WAIT))
  1808. continue;
  1809. fence = drm_syncobj_fence_get(syncobj);
  1810. if (!fence)
  1811. return -EINVAL;
  1812. err = i915_gem_request_await_dma_fence(eb->request, fence);
  1813. dma_fence_put(fence);
  1814. if (err < 0)
  1815. return err;
  1816. }
  1817. return 0;
  1818. }
  1819. static void
  1820. signal_fence_array(struct i915_execbuffer *eb,
  1821. struct drm_syncobj **fences)
  1822. {
  1823. const unsigned int nfences = eb->args->num_cliprects;
  1824. struct dma_fence * const fence = &eb->request->fence;
  1825. unsigned int n;
  1826. for (n = 0; n < nfences; n++) {
  1827. struct drm_syncobj *syncobj;
  1828. unsigned int flags;
  1829. syncobj = ptr_unpack_bits(fences[n], &flags, 2);
  1830. if (!(flags & I915_EXEC_FENCE_SIGNAL))
  1831. continue;
  1832. drm_syncobj_replace_fence(syncobj, fence);
  1833. }
  1834. }
  1835. static int
  1836. i915_gem_do_execbuffer(struct drm_device *dev,
  1837. struct drm_file *file,
  1838. struct drm_i915_gem_execbuffer2 *args,
  1839. struct drm_i915_gem_exec_object2 *exec,
  1840. struct drm_syncobj **fences)
  1841. {
  1842. struct i915_execbuffer eb;
  1843. struct dma_fence *in_fence = NULL;
  1844. struct sync_file *out_fence = NULL;
  1845. int out_fence_fd = -1;
  1846. int err;
  1847. BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
  1848. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
  1849. ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  1850. eb.i915 = to_i915(dev);
  1851. eb.file = file;
  1852. eb.args = args;
  1853. if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
  1854. args->flags |= __EXEC_HAS_RELOC;
  1855. eb.exec = exec;
  1856. eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
  1857. eb.vma[0] = NULL;
  1858. eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
  1859. eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  1860. if (USES_FULL_PPGTT(eb.i915))
  1861. eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  1862. reloc_cache_init(&eb.reloc_cache, eb.i915);
  1863. eb.buffer_count = args->buffer_count;
  1864. eb.batch_start_offset = args->batch_start_offset;
  1865. eb.batch_len = args->batch_len;
  1866. eb.batch_flags = 0;
  1867. if (args->flags & I915_EXEC_SECURE) {
  1868. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1869. return -EPERM;
  1870. eb.batch_flags |= I915_DISPATCH_SECURE;
  1871. }
  1872. if (args->flags & I915_EXEC_IS_PINNED)
  1873. eb.batch_flags |= I915_DISPATCH_PINNED;
  1874. eb.engine = eb_select_engine(eb.i915, file, args);
  1875. if (!eb.engine)
  1876. return -EINVAL;
  1877. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1878. if (!HAS_RESOURCE_STREAMER(eb.i915)) {
  1879. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1880. return -EINVAL;
  1881. }
  1882. if (eb.engine->id != RCS) {
  1883. DRM_DEBUG("RS is not available on %s\n",
  1884. eb.engine->name);
  1885. return -EINVAL;
  1886. }
  1887. eb.batch_flags |= I915_DISPATCH_RS;
  1888. }
  1889. if (args->flags & I915_EXEC_FENCE_IN) {
  1890. in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
  1891. if (!in_fence)
  1892. return -EINVAL;
  1893. }
  1894. if (args->flags & I915_EXEC_FENCE_OUT) {
  1895. out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  1896. if (out_fence_fd < 0) {
  1897. err = out_fence_fd;
  1898. goto err_in_fence;
  1899. }
  1900. }
  1901. err = eb_create(&eb);
  1902. if (err)
  1903. goto err_out_fence;
  1904. GEM_BUG_ON(!eb.lut_size);
  1905. err = eb_select_context(&eb);
  1906. if (unlikely(err))
  1907. goto err_destroy;
  1908. /*
  1909. * Take a local wakeref for preparing to dispatch the execbuf as
  1910. * we expect to access the hardware fairly frequently in the
  1911. * process. Upon first dispatch, we acquire another prolonged
  1912. * wakeref that we hold until the GPU has been idle for at least
  1913. * 100ms.
  1914. */
  1915. intel_runtime_pm_get(eb.i915);
  1916. err = i915_mutex_lock_interruptible(dev);
  1917. if (err)
  1918. goto err_rpm;
  1919. err = eb_relocate(&eb);
  1920. if (err) {
  1921. /*
  1922. * If the user expects the execobject.offset and
  1923. * reloc.presumed_offset to be an exact match,
  1924. * as for using NO_RELOC, then we cannot update
  1925. * the execobject.offset until we have completed
  1926. * relocation.
  1927. */
  1928. args->flags &= ~__EXEC_HAS_RELOC;
  1929. goto err_vma;
  1930. }
  1931. if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
  1932. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1933. err = -EINVAL;
  1934. goto err_vma;
  1935. }
  1936. if (eb.batch_start_offset > eb.batch->size ||
  1937. eb.batch_len > eb.batch->size - eb.batch_start_offset) {
  1938. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1939. err = -EINVAL;
  1940. goto err_vma;
  1941. }
  1942. if (eb_use_cmdparser(&eb)) {
  1943. struct i915_vma *vma;
  1944. vma = eb_parse(&eb, drm_is_current_master(file));
  1945. if (IS_ERR(vma)) {
  1946. err = PTR_ERR(vma);
  1947. goto err_vma;
  1948. }
  1949. if (vma) {
  1950. /*
  1951. * Batch parsed and accepted:
  1952. *
  1953. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1954. * bit from MI_BATCH_BUFFER_START commands issued in
  1955. * the dispatch_execbuffer implementations. We
  1956. * specifically don't want that set on batches the
  1957. * command parser has accepted.
  1958. */
  1959. eb.batch_flags |= I915_DISPATCH_SECURE;
  1960. eb.batch_start_offset = 0;
  1961. eb.batch = vma;
  1962. }
  1963. }
  1964. if (eb.batch_len == 0)
  1965. eb.batch_len = eb.batch->size - eb.batch_start_offset;
  1966. /*
  1967. * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1968. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1969. * hsw should have this fixed, but bdw mucks it up again. */
  1970. if (eb.batch_flags & I915_DISPATCH_SECURE) {
  1971. struct i915_vma *vma;
  1972. /*
  1973. * So on first glance it looks freaky that we pin the batch here
  1974. * outside of the reservation loop. But:
  1975. * - The batch is already pinned into the relevant ppgtt, so we
  1976. * already have the backing storage fully allocated.
  1977. * - No other BO uses the global gtt (well contexts, but meh),
  1978. * so we don't really have issues with multiple objects not
  1979. * fitting due to fragmentation.
  1980. * So this is actually safe.
  1981. */
  1982. vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
  1983. if (IS_ERR(vma)) {
  1984. err = PTR_ERR(vma);
  1985. goto err_vma;
  1986. }
  1987. eb.batch = vma;
  1988. }
  1989. /* All GPU relocation batches must be submitted prior to the user rq */
  1990. GEM_BUG_ON(eb.reloc_cache.rq);
  1991. /* Allocate a request for this batch buffer nice and early. */
  1992. eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
  1993. if (IS_ERR(eb.request)) {
  1994. err = PTR_ERR(eb.request);
  1995. goto err_batch_unpin;
  1996. }
  1997. if (in_fence) {
  1998. err = i915_gem_request_await_dma_fence(eb.request, in_fence);
  1999. if (err < 0)
  2000. goto err_request;
  2001. }
  2002. if (fences) {
  2003. err = await_fence_array(&eb, fences);
  2004. if (err)
  2005. goto err_request;
  2006. }
  2007. if (out_fence_fd != -1) {
  2008. out_fence = sync_file_create(&eb.request->fence);
  2009. if (!out_fence) {
  2010. err = -ENOMEM;
  2011. goto err_request;
  2012. }
  2013. }
  2014. /*
  2015. * Whilst this request exists, batch_obj will be on the
  2016. * active_list, and so will hold the active reference. Only when this
  2017. * request is retired will the the batch_obj be moved onto the
  2018. * inactive_list and lose its active reference. Hence we do not need
  2019. * to explicitly hold another reference here.
  2020. */
  2021. eb.request->batch = eb.batch;
  2022. trace_i915_gem_request_queue(eb.request, eb.batch_flags);
  2023. err = eb_submit(&eb);
  2024. err_request:
  2025. __i915_add_request(eb.request, err == 0);
  2026. add_to_client(eb.request, file);
  2027. if (fences)
  2028. signal_fence_array(&eb, fences);
  2029. if (out_fence) {
  2030. if (err == 0) {
  2031. fd_install(out_fence_fd, out_fence->file);
  2032. args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
  2033. args->rsvd2 |= (u64)out_fence_fd << 32;
  2034. out_fence_fd = -1;
  2035. } else {
  2036. fput(out_fence->file);
  2037. }
  2038. }
  2039. err_batch_unpin:
  2040. if (eb.batch_flags & I915_DISPATCH_SECURE)
  2041. i915_vma_unpin(eb.batch);
  2042. err_vma:
  2043. if (eb.exec)
  2044. eb_release_vmas(&eb);
  2045. mutex_unlock(&dev->struct_mutex);
  2046. err_rpm:
  2047. intel_runtime_pm_put(eb.i915);
  2048. i915_gem_context_put(eb.ctx);
  2049. err_destroy:
  2050. eb_destroy(&eb);
  2051. err_out_fence:
  2052. if (out_fence_fd != -1)
  2053. put_unused_fd(out_fence_fd);
  2054. err_in_fence:
  2055. dma_fence_put(in_fence);
  2056. return err;
  2057. }
  2058. static size_t eb_element_size(void)
  2059. {
  2060. return (sizeof(struct drm_i915_gem_exec_object2) +
  2061. sizeof(struct i915_vma *) +
  2062. sizeof(unsigned int));
  2063. }
  2064. static bool check_buffer_count(size_t count)
  2065. {
  2066. const size_t sz = eb_element_size();
  2067. /*
  2068. * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
  2069. * array size (see eb_create()). Otherwise, we can accept an array as
  2070. * large as can be addressed (though use large arrays at your peril)!
  2071. */
  2072. return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
  2073. }
  2074. /*
  2075. * Legacy execbuffer just creates an exec2 list from the original exec object
  2076. * list array and passes it to the real function.
  2077. */
  2078. int
  2079. i915_gem_execbuffer(struct drm_device *dev, void *data,
  2080. struct drm_file *file)
  2081. {
  2082. struct drm_i915_gem_execbuffer *args = data;
  2083. struct drm_i915_gem_execbuffer2 exec2;
  2084. struct drm_i915_gem_exec_object *exec_list = NULL;
  2085. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  2086. const size_t count = args->buffer_count;
  2087. unsigned int i;
  2088. int err;
  2089. if (!check_buffer_count(count)) {
  2090. DRM_DEBUG("execbuf2 with %zd buffers\n", count);
  2091. return -EINVAL;
  2092. }
  2093. exec2.buffers_ptr = args->buffers_ptr;
  2094. exec2.buffer_count = args->buffer_count;
  2095. exec2.batch_start_offset = args->batch_start_offset;
  2096. exec2.batch_len = args->batch_len;
  2097. exec2.DR1 = args->DR1;
  2098. exec2.DR4 = args->DR4;
  2099. exec2.num_cliprects = args->num_cliprects;
  2100. exec2.cliprects_ptr = args->cliprects_ptr;
  2101. exec2.flags = I915_EXEC_RENDER;
  2102. i915_execbuffer2_set_context_id(exec2, 0);
  2103. if (!i915_gem_check_execbuffer(&exec2))
  2104. return -EINVAL;
  2105. /* Copy in the exec list from userland */
  2106. exec_list = kvmalloc_array(count, sizeof(*exec_list),
  2107. __GFP_NOWARN | GFP_KERNEL);
  2108. exec2_list = kvmalloc_array(count + 1, eb_element_size(),
  2109. __GFP_NOWARN | GFP_KERNEL);
  2110. if (exec_list == NULL || exec2_list == NULL) {
  2111. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  2112. args->buffer_count);
  2113. kvfree(exec_list);
  2114. kvfree(exec2_list);
  2115. return -ENOMEM;
  2116. }
  2117. err = copy_from_user(exec_list,
  2118. u64_to_user_ptr(args->buffers_ptr),
  2119. sizeof(*exec_list) * count);
  2120. if (err) {
  2121. DRM_DEBUG("copy %d exec entries failed %d\n",
  2122. args->buffer_count, err);
  2123. kvfree(exec_list);
  2124. kvfree(exec2_list);
  2125. return -EFAULT;
  2126. }
  2127. for (i = 0; i < args->buffer_count; i++) {
  2128. exec2_list[i].handle = exec_list[i].handle;
  2129. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  2130. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  2131. exec2_list[i].alignment = exec_list[i].alignment;
  2132. exec2_list[i].offset = exec_list[i].offset;
  2133. if (INTEL_GEN(to_i915(dev)) < 4)
  2134. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  2135. else
  2136. exec2_list[i].flags = 0;
  2137. }
  2138. err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
  2139. if (exec2.flags & __EXEC_HAS_RELOC) {
  2140. struct drm_i915_gem_exec_object __user *user_exec_list =
  2141. u64_to_user_ptr(args->buffers_ptr);
  2142. /* Copy the new buffer offsets back to the user's exec list. */
  2143. for (i = 0; i < args->buffer_count; i++) {
  2144. if (!(exec2_list[i].offset & UPDATE))
  2145. continue;
  2146. exec2_list[i].offset =
  2147. gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
  2148. exec2_list[i].offset &= PIN_OFFSET_MASK;
  2149. if (__copy_to_user(&user_exec_list[i].offset,
  2150. &exec2_list[i].offset,
  2151. sizeof(user_exec_list[i].offset)))
  2152. break;
  2153. }
  2154. }
  2155. kvfree(exec_list);
  2156. kvfree(exec2_list);
  2157. return err;
  2158. }
  2159. int
  2160. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  2161. struct drm_file *file)
  2162. {
  2163. struct drm_i915_gem_execbuffer2 *args = data;
  2164. struct drm_i915_gem_exec_object2 *exec2_list;
  2165. struct drm_syncobj **fences = NULL;
  2166. const size_t count = args->buffer_count;
  2167. int err;
  2168. if (!check_buffer_count(count)) {
  2169. DRM_DEBUG("execbuf2 with %zd buffers\n", count);
  2170. return -EINVAL;
  2171. }
  2172. if (!i915_gem_check_execbuffer(args))
  2173. return -EINVAL;
  2174. /* Allocate an extra slot for use by the command parser */
  2175. exec2_list = kvmalloc_array(count + 1, eb_element_size(),
  2176. __GFP_NOWARN | GFP_KERNEL);
  2177. if (exec2_list == NULL) {
  2178. DRM_DEBUG("Failed to allocate exec list for %zd buffers\n",
  2179. count);
  2180. return -ENOMEM;
  2181. }
  2182. if (copy_from_user(exec2_list,
  2183. u64_to_user_ptr(args->buffers_ptr),
  2184. sizeof(*exec2_list) * count)) {
  2185. DRM_DEBUG("copy %zd exec entries failed\n", count);
  2186. kvfree(exec2_list);
  2187. return -EFAULT;
  2188. }
  2189. if (args->flags & I915_EXEC_FENCE_ARRAY) {
  2190. fences = get_fence_array(args, file);
  2191. if (IS_ERR(fences)) {
  2192. kvfree(exec2_list);
  2193. return PTR_ERR(fences);
  2194. }
  2195. }
  2196. err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
  2197. /*
  2198. * Now that we have begun execution of the batchbuffer, we ignore
  2199. * any new error after this point. Also given that we have already
  2200. * updated the associated relocations, we try to write out the current
  2201. * object locations irrespective of any error.
  2202. */
  2203. if (args->flags & __EXEC_HAS_RELOC) {
  2204. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  2205. u64_to_user_ptr(args->buffers_ptr);
  2206. unsigned int i;
  2207. /* Copy the new buffer offsets back to the user's exec list. */
  2208. user_access_begin();
  2209. for (i = 0; i < args->buffer_count; i++) {
  2210. if (!(exec2_list[i].offset & UPDATE))
  2211. continue;
  2212. exec2_list[i].offset =
  2213. gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
  2214. unsafe_put_user(exec2_list[i].offset,
  2215. &user_exec_list[i].offset,
  2216. end_user);
  2217. }
  2218. end_user:
  2219. user_access_end();
  2220. }
  2221. args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
  2222. put_fence_array(args, fences);
  2223. kvfree(exec2_list);
  2224. return err;
  2225. }