i915_gem_execbuffer.c 72 KB

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