i915_gem_execbuffer.c 70 KB

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