i915_gem_execbuffer.c 70 KB

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