i915_gem_execbuffer.c 69 KB

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