i915_gem_execbuffer.c 71 KB

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