i915_gem_execbuffer.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  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,
  433. unsigned int i, unsigned batch_idx,
  434. struct i915_vma *vma)
  435. {
  436. struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
  437. int err;
  438. GEM_BUG_ON(i915_vma_is_closed(vma));
  439. if (!(eb->args->flags & __EXEC_VALIDATED)) {
  440. err = eb_validate_vma(eb, entry, vma);
  441. if (unlikely(err))
  442. return err;
  443. }
  444. if (eb->lut_size > 0) {
  445. vma->exec_handle = entry->handle;
  446. hlist_add_head(&vma->exec_node,
  447. &eb->buckets[hash_32(entry->handle,
  448. eb->lut_size)]);
  449. }
  450. if (entry->relocation_count)
  451. list_add_tail(&vma->reloc_link, &eb->relocs);
  452. /*
  453. * Stash a pointer from the vma to execobj, so we can query its flags,
  454. * size, alignment etc as provided by the user. Also we stash a pointer
  455. * to the vma inside the execobj so that we can use a direct lookup
  456. * to find the right target VMA when doing relocations.
  457. */
  458. eb->vma[i] = vma;
  459. eb->flags[i] = entry->flags;
  460. vma->exec_flags = &eb->flags[i];
  461. /*
  462. * SNA is doing fancy tricks with compressing batch buffers, which leads
  463. * to negative relocation deltas. Usually that works out ok since the
  464. * relocate address is still positive, except when the batch is placed
  465. * very low in the GTT. Ensure this doesn't happen.
  466. *
  467. * Note that actual hangs have only been observed on gen7, but for
  468. * paranoia do it everywhere.
  469. */
  470. if (i == batch_idx) {
  471. if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
  472. eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
  473. if (eb->reloc_cache.has_fence)
  474. eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
  475. eb->batch = vma;
  476. }
  477. err = 0;
  478. if (eb_pin_vma(eb, entry, vma)) {
  479. if (entry->offset != vma->node.start) {
  480. entry->offset = vma->node.start | UPDATE;
  481. eb->args->flags |= __EXEC_HAS_RELOC;
  482. }
  483. } else {
  484. eb_unreserve_vma(vma, vma->exec_flags);
  485. list_add_tail(&vma->exec_link, &eb->unbound);
  486. if (drm_mm_node_allocated(&vma->node))
  487. err = i915_vma_unbind(vma);
  488. if (unlikely(err))
  489. vma->exec_flags = NULL;
  490. }
  491. return err;
  492. }
  493. static inline int use_cpu_reloc(const struct reloc_cache *cache,
  494. const struct drm_i915_gem_object *obj)
  495. {
  496. if (!i915_gem_object_has_struct_page(obj))
  497. return false;
  498. if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
  499. return true;
  500. if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
  501. return false;
  502. return (cache->has_llc ||
  503. obj->cache_dirty ||
  504. obj->cache_level != I915_CACHE_NONE);
  505. }
  506. static int eb_reserve_vma(const struct i915_execbuffer *eb,
  507. struct i915_vma *vma)
  508. {
  509. struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  510. unsigned int exec_flags = *vma->exec_flags;
  511. u64 pin_flags;
  512. int err;
  513. pin_flags = PIN_USER | PIN_NONBLOCK;
  514. if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
  515. pin_flags |= PIN_GLOBAL;
  516. /*
  517. * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  518. * limit address to the first 4GBs for unflagged objects.
  519. */
  520. if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
  521. pin_flags |= PIN_ZONE_4G;
  522. if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
  523. pin_flags |= PIN_MAPPABLE;
  524. if (exec_flags & EXEC_OBJECT_PINNED) {
  525. pin_flags |= entry->offset | PIN_OFFSET_FIXED;
  526. pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
  527. } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
  528. pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  529. }
  530. err = i915_vma_pin(vma,
  531. entry->pad_to_size, entry->alignment,
  532. pin_flags);
  533. if (err)
  534. return err;
  535. if (entry->offset != vma->node.start) {
  536. entry->offset = vma->node.start | UPDATE;
  537. eb->args->flags |= __EXEC_HAS_RELOC;
  538. }
  539. if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
  540. err = i915_vma_pin_fence(vma);
  541. if (unlikely(err)) {
  542. i915_vma_unpin(vma);
  543. return err;
  544. }
  545. if (vma->fence)
  546. exec_flags |= __EXEC_OBJECT_HAS_FENCE;
  547. }
  548. *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
  549. GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
  550. return 0;
  551. }
  552. static int eb_reserve(struct i915_execbuffer *eb)
  553. {
  554. const unsigned int count = eb->buffer_count;
  555. struct list_head last;
  556. struct i915_vma *vma;
  557. unsigned int i, pass;
  558. int err;
  559. /*
  560. * Attempt to pin all of the buffers into the GTT.
  561. * This is done in 3 phases:
  562. *
  563. * 1a. Unbind all objects that do not match the GTT constraints for
  564. * the execbuffer (fenceable, mappable, alignment etc).
  565. * 1b. Increment pin count for already bound objects.
  566. * 2. Bind new objects.
  567. * 3. Decrement pin count.
  568. *
  569. * This avoid unnecessary unbinding of later objects in order to make
  570. * room for the earlier objects *unless* we need to defragment.
  571. */
  572. pass = 0;
  573. err = 0;
  574. do {
  575. list_for_each_entry(vma, &eb->unbound, exec_link) {
  576. err = eb_reserve_vma(eb, vma);
  577. if (err)
  578. break;
  579. }
  580. if (err != -ENOSPC)
  581. return err;
  582. /* Resort *all* the objects into priority order */
  583. INIT_LIST_HEAD(&eb->unbound);
  584. INIT_LIST_HEAD(&last);
  585. for (i = 0; i < count; i++) {
  586. unsigned int flags = eb->flags[i];
  587. struct i915_vma *vma = eb->vma[i];
  588. if (flags & EXEC_OBJECT_PINNED &&
  589. flags & __EXEC_OBJECT_HAS_PIN)
  590. continue;
  591. eb_unreserve_vma(vma, &eb->flags[i]);
  592. if (flags & EXEC_OBJECT_PINNED)
  593. list_add(&vma->exec_link, &eb->unbound);
  594. else if (flags & __EXEC_OBJECT_NEEDS_MAP)
  595. list_add_tail(&vma->exec_link, &eb->unbound);
  596. else
  597. list_add_tail(&vma->exec_link, &last);
  598. }
  599. list_splice_tail(&last, &eb->unbound);
  600. switch (pass++) {
  601. case 0:
  602. break;
  603. case 1:
  604. /* Too fragmented, unbind everything and retry */
  605. err = i915_gem_evict_vm(eb->vm);
  606. if (err)
  607. return err;
  608. break;
  609. default:
  610. return -ENOSPC;
  611. }
  612. } while (1);
  613. }
  614. static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
  615. {
  616. if (eb->args->flags & I915_EXEC_BATCH_FIRST)
  617. return 0;
  618. else
  619. return eb->buffer_count - 1;
  620. }
  621. static int eb_select_context(struct i915_execbuffer *eb)
  622. {
  623. struct i915_gem_context *ctx;
  624. ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
  625. if (unlikely(!ctx))
  626. return -ENOENT;
  627. eb->ctx = ctx;
  628. eb->vm = ctx->ppgtt ? &ctx->ppgtt->vm : &eb->i915->ggtt.vm;
  629. eb->context_flags = 0;
  630. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  631. eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
  632. return 0;
  633. }
  634. static int eb_lookup_vmas(struct i915_execbuffer *eb)
  635. {
  636. struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
  637. struct drm_i915_gem_object *obj;
  638. unsigned int i, batch;
  639. int err;
  640. if (unlikely(i915_gem_context_is_closed(eb->ctx)))
  641. return -ENOENT;
  642. if (unlikely(i915_gem_context_is_banned(eb->ctx)))
  643. return -EIO;
  644. INIT_LIST_HEAD(&eb->relocs);
  645. INIT_LIST_HEAD(&eb->unbound);
  646. batch = eb_batch_index(eb);
  647. for (i = 0; i < eb->buffer_count; i++) {
  648. u32 handle = eb->exec[i].handle;
  649. struct i915_lut_handle *lut;
  650. struct i915_vma *vma;
  651. vma = radix_tree_lookup(handles_vma, handle);
  652. if (likely(vma))
  653. goto add_vma;
  654. obj = i915_gem_object_lookup(eb->file, handle);
  655. if (unlikely(!obj)) {
  656. err = -ENOENT;
  657. goto err_vma;
  658. }
  659. vma = i915_vma_instance(obj, eb->vm, NULL);
  660. if (unlikely(IS_ERR(vma))) {
  661. err = PTR_ERR(vma);
  662. goto err_obj;
  663. }
  664. lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
  665. if (unlikely(!lut)) {
  666. err = -ENOMEM;
  667. goto err_obj;
  668. }
  669. err = radix_tree_insert(handles_vma, handle, vma);
  670. if (unlikely(err)) {
  671. kmem_cache_free(eb->i915->luts, lut);
  672. goto err_obj;
  673. }
  674. /* transfer ref to ctx */
  675. if (!vma->open_count++)
  676. i915_vma_reopen(vma);
  677. list_add(&lut->obj_link, &obj->lut_list);
  678. list_add(&lut->ctx_link, &eb->ctx->handles_list);
  679. lut->ctx = eb->ctx;
  680. lut->handle = handle;
  681. add_vma:
  682. err = eb_add_vma(eb, i, batch, vma);
  683. if (unlikely(err))
  684. goto err_vma;
  685. GEM_BUG_ON(vma != eb->vma[i]);
  686. GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
  687. GEM_BUG_ON(drm_mm_node_allocated(&vma->node) &&
  688. eb_vma_misplaced(&eb->exec[i], vma, eb->flags[i]));
  689. }
  690. eb->args->flags |= __EXEC_VALIDATED;
  691. return eb_reserve(eb);
  692. err_obj:
  693. i915_gem_object_put(obj);
  694. err_vma:
  695. eb->vma[i] = NULL;
  696. return err;
  697. }
  698. static struct i915_vma *
  699. eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
  700. {
  701. if (eb->lut_size < 0) {
  702. if (handle >= -eb->lut_size)
  703. return NULL;
  704. return eb->vma[handle];
  705. } else {
  706. struct hlist_head *head;
  707. struct i915_vma *vma;
  708. head = &eb->buckets[hash_32(handle, eb->lut_size)];
  709. hlist_for_each_entry(vma, head, exec_node) {
  710. if (vma->exec_handle == handle)
  711. return vma;
  712. }
  713. return NULL;
  714. }
  715. }
  716. static void eb_release_vmas(const struct i915_execbuffer *eb)
  717. {
  718. const unsigned int count = eb->buffer_count;
  719. unsigned int i;
  720. for (i = 0; i < count; i++) {
  721. struct i915_vma *vma = eb->vma[i];
  722. unsigned int flags = eb->flags[i];
  723. if (!vma)
  724. break;
  725. GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
  726. vma->exec_flags = NULL;
  727. eb->vma[i] = NULL;
  728. if (flags & __EXEC_OBJECT_HAS_PIN)
  729. __eb_unreserve_vma(vma, flags);
  730. if (flags & __EXEC_OBJECT_HAS_REF)
  731. i915_vma_put(vma);
  732. }
  733. }
  734. static void eb_reset_vmas(const struct i915_execbuffer *eb)
  735. {
  736. eb_release_vmas(eb);
  737. if (eb->lut_size > 0)
  738. memset(eb->buckets, 0,
  739. sizeof(struct hlist_head) << eb->lut_size);
  740. }
  741. static void eb_destroy(const struct i915_execbuffer *eb)
  742. {
  743. GEM_BUG_ON(eb->reloc_cache.rq);
  744. if (eb->lut_size > 0)
  745. kfree(eb->buckets);
  746. }
  747. static inline u64
  748. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  749. const struct i915_vma *target)
  750. {
  751. return gen8_canonical_addr((int)reloc->delta + target->node.start);
  752. }
  753. static void reloc_cache_init(struct reloc_cache *cache,
  754. struct drm_i915_private *i915)
  755. {
  756. cache->page = -1;
  757. cache->vaddr = 0;
  758. /* Must be a variable in the struct to allow GCC to unroll. */
  759. cache->gen = INTEL_GEN(i915);
  760. cache->has_llc = HAS_LLC(i915);
  761. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  762. cache->has_fence = cache->gen < 4;
  763. cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
  764. cache->node.allocated = false;
  765. cache->rq = NULL;
  766. cache->rq_size = 0;
  767. }
  768. static inline void *unmask_page(unsigned long p)
  769. {
  770. return (void *)(uintptr_t)(p & PAGE_MASK);
  771. }
  772. static inline unsigned int unmask_flags(unsigned long p)
  773. {
  774. return p & ~PAGE_MASK;
  775. }
  776. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  777. static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
  778. {
  779. struct drm_i915_private *i915 =
  780. container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
  781. return &i915->ggtt;
  782. }
  783. static void reloc_gpu_flush(struct reloc_cache *cache)
  784. {
  785. GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
  786. cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
  787. i915_gem_object_unpin_map(cache->rq->batch->obj);
  788. i915_gem_chipset_flush(cache->rq->i915);
  789. i915_request_add(cache->rq);
  790. cache->rq = NULL;
  791. }
  792. static void reloc_cache_reset(struct reloc_cache *cache)
  793. {
  794. void *vaddr;
  795. if (cache->rq)
  796. reloc_gpu_flush(cache);
  797. if (!cache->vaddr)
  798. return;
  799. vaddr = unmask_page(cache->vaddr);
  800. if (cache->vaddr & KMAP) {
  801. if (cache->vaddr & CLFLUSH_AFTER)
  802. mb();
  803. kunmap_atomic(vaddr);
  804. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  805. } else {
  806. wmb();
  807. io_mapping_unmap_atomic((void __iomem *)vaddr);
  808. if (cache->node.allocated) {
  809. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  810. ggtt->vm.clear_range(&ggtt->vm,
  811. cache->node.start,
  812. cache->node.size);
  813. drm_mm_remove_node(&cache->node);
  814. } else {
  815. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  816. }
  817. }
  818. cache->vaddr = 0;
  819. cache->page = -1;
  820. }
  821. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  822. struct reloc_cache *cache,
  823. unsigned long page)
  824. {
  825. void *vaddr;
  826. if (cache->vaddr) {
  827. kunmap_atomic(unmask_page(cache->vaddr));
  828. } else {
  829. unsigned int flushes;
  830. int err;
  831. err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
  832. if (err)
  833. return ERR_PTR(err);
  834. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  835. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  836. cache->vaddr = flushes | KMAP;
  837. cache->node.mm = (void *)obj;
  838. if (flushes)
  839. mb();
  840. }
  841. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
  842. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  843. cache->page = page;
  844. return vaddr;
  845. }
  846. static void *reloc_iomap(struct drm_i915_gem_object *obj,
  847. struct reloc_cache *cache,
  848. unsigned long page)
  849. {
  850. struct i915_ggtt *ggtt = cache_to_ggtt(cache);
  851. unsigned long offset;
  852. void *vaddr;
  853. if (cache->vaddr) {
  854. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  855. } else {
  856. struct i915_vma *vma;
  857. int err;
  858. if (use_cpu_reloc(cache, obj))
  859. return NULL;
  860. err = i915_gem_object_set_to_gtt_domain(obj, true);
  861. if (err)
  862. return ERR_PTR(err);
  863. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  864. PIN_MAPPABLE |
  865. PIN_NONBLOCK |
  866. PIN_NONFAULT);
  867. if (IS_ERR(vma)) {
  868. memset(&cache->node, 0, sizeof(cache->node));
  869. err = drm_mm_insert_node_in_range
  870. (&ggtt->vm.mm, &cache->node,
  871. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  872. 0, ggtt->mappable_end,
  873. DRM_MM_INSERT_LOW);
  874. if (err) /* no inactive aperture space, use cpu reloc */
  875. return NULL;
  876. } else {
  877. err = i915_vma_put_fence(vma);
  878. if (err) {
  879. i915_vma_unpin(vma);
  880. return ERR_PTR(err);
  881. }
  882. cache->node.start = vma->node.start;
  883. cache->node.mm = (void *)vma;
  884. }
  885. }
  886. offset = cache->node.start;
  887. if (cache->node.allocated) {
  888. wmb();
  889. ggtt->vm.insert_page(&ggtt->vm,
  890. i915_gem_object_get_dma_address(obj, page),
  891. offset, I915_CACHE_NONE, 0);
  892. } else {
  893. offset += page << PAGE_SHIFT;
  894. }
  895. vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
  896. offset);
  897. cache->page = page;
  898. cache->vaddr = (unsigned long)vaddr;
  899. return vaddr;
  900. }
  901. static void *reloc_vaddr(struct drm_i915_gem_object *obj,
  902. struct reloc_cache *cache,
  903. unsigned long page)
  904. {
  905. void *vaddr;
  906. if (cache->page == page) {
  907. vaddr = unmask_page(cache->vaddr);
  908. } else {
  909. vaddr = NULL;
  910. if ((cache->vaddr & KMAP) == 0)
  911. vaddr = reloc_iomap(obj, cache, page);
  912. if (!vaddr)
  913. vaddr = reloc_kmap(obj, cache, page);
  914. }
  915. return vaddr;
  916. }
  917. static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  918. {
  919. if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
  920. if (flushes & CLFLUSH_BEFORE) {
  921. clflushopt(addr);
  922. mb();
  923. }
  924. *addr = value;
  925. /*
  926. * Writes to the same cacheline are serialised by the CPU
  927. * (including clflush). On the write path, we only require
  928. * that it hits memory in an orderly fashion and place
  929. * mb barriers at the start and end of the relocation phase
  930. * to ensure ordering of clflush wrt to the system.
  931. */
  932. if (flushes & CLFLUSH_AFTER)
  933. clflushopt(addr);
  934. } else
  935. *addr = value;
  936. }
  937. static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
  938. struct i915_vma *vma,
  939. unsigned int len)
  940. {
  941. struct reloc_cache *cache = &eb->reloc_cache;
  942. struct drm_i915_gem_object *obj;
  943. struct i915_request *rq;
  944. struct i915_vma *batch;
  945. u32 *cmd;
  946. int err;
  947. GEM_BUG_ON(vma->obj->write_domain & I915_GEM_DOMAIN_CPU);
  948. obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
  949. if (IS_ERR(obj))
  950. return PTR_ERR(obj);
  951. cmd = i915_gem_object_pin_map(obj,
  952. cache->has_llc ?
  953. I915_MAP_FORCE_WB :
  954. I915_MAP_FORCE_WC);
  955. i915_gem_object_unpin_pages(obj);
  956. if (IS_ERR(cmd))
  957. return PTR_ERR(cmd);
  958. err = i915_gem_object_set_to_wc_domain(obj, false);
  959. if (err)
  960. goto err_unmap;
  961. batch = i915_vma_instance(obj, vma->vm, NULL);
  962. if (IS_ERR(batch)) {
  963. err = PTR_ERR(batch);
  964. goto err_unmap;
  965. }
  966. err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
  967. if (err)
  968. goto err_unmap;
  969. rq = i915_request_alloc(eb->engine, eb->ctx);
  970. if (IS_ERR(rq)) {
  971. err = PTR_ERR(rq);
  972. goto err_unpin;
  973. }
  974. err = i915_request_await_object(rq, vma->obj, true);
  975. if (err)
  976. goto err_request;
  977. err = eb->engine->emit_bb_start(rq,
  978. batch->node.start, PAGE_SIZE,
  979. cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
  980. if (err)
  981. goto err_request;
  982. GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
  983. i915_vma_move_to_active(batch, rq, 0);
  984. reservation_object_lock(batch->resv, NULL);
  985. reservation_object_add_excl_fence(batch->resv, &rq->fence);
  986. reservation_object_unlock(batch->resv);
  987. i915_vma_unpin(batch);
  988. i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
  989. reservation_object_lock(vma->resv, NULL);
  990. reservation_object_add_excl_fence(vma->resv, &rq->fence);
  991. reservation_object_unlock(vma->resv);
  992. rq->batch = batch;
  993. cache->rq = rq;
  994. cache->rq_cmd = cmd;
  995. cache->rq_size = 0;
  996. /* Return with batch mapping (cmd) still pinned */
  997. return 0;
  998. err_request:
  999. i915_request_add(rq);
  1000. err_unpin:
  1001. i915_vma_unpin(batch);
  1002. err_unmap:
  1003. i915_gem_object_unpin_map(obj);
  1004. return err;
  1005. }
  1006. static u32 *reloc_gpu(struct i915_execbuffer *eb,
  1007. struct i915_vma *vma,
  1008. unsigned int len)
  1009. {
  1010. struct reloc_cache *cache = &eb->reloc_cache;
  1011. u32 *cmd;
  1012. if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
  1013. reloc_gpu_flush(cache);
  1014. if (unlikely(!cache->rq)) {
  1015. int err;
  1016. /* If we need to copy for the cmdparser, we will stall anyway */
  1017. if (eb_use_cmdparser(eb))
  1018. return ERR_PTR(-EWOULDBLOCK);
  1019. if (!intel_engine_can_store_dword(eb->engine))
  1020. return ERR_PTR(-ENODEV);
  1021. err = __reloc_gpu_alloc(eb, vma, len);
  1022. if (unlikely(err))
  1023. return ERR_PTR(err);
  1024. }
  1025. cmd = cache->rq_cmd + cache->rq_size;
  1026. cache->rq_size += len;
  1027. return cmd;
  1028. }
  1029. static u64
  1030. relocate_entry(struct i915_vma *vma,
  1031. const struct drm_i915_gem_relocation_entry *reloc,
  1032. struct i915_execbuffer *eb,
  1033. const struct i915_vma *target)
  1034. {
  1035. u64 offset = reloc->offset;
  1036. u64 target_offset = relocation_target(reloc, target);
  1037. bool wide = eb->reloc_cache.use_64bit_reloc;
  1038. void *vaddr;
  1039. if (!eb->reloc_cache.vaddr &&
  1040. (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
  1041. !reservation_object_test_signaled_rcu(vma->resv, true))) {
  1042. const unsigned int gen = eb->reloc_cache.gen;
  1043. unsigned int len;
  1044. u32 *batch;
  1045. u64 addr;
  1046. if (wide)
  1047. len = offset & 7 ? 8 : 5;
  1048. else if (gen >= 4)
  1049. len = 4;
  1050. else
  1051. len = 3;
  1052. batch = reloc_gpu(eb, vma, len);
  1053. if (IS_ERR(batch))
  1054. goto repeat;
  1055. addr = gen8_canonical_addr(vma->node.start + offset);
  1056. if (wide) {
  1057. if (offset & 7) {
  1058. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1059. *batch++ = lower_32_bits(addr);
  1060. *batch++ = upper_32_bits(addr);
  1061. *batch++ = lower_32_bits(target_offset);
  1062. addr = gen8_canonical_addr(addr + 4);
  1063. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1064. *batch++ = lower_32_bits(addr);
  1065. *batch++ = upper_32_bits(addr);
  1066. *batch++ = upper_32_bits(target_offset);
  1067. } else {
  1068. *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
  1069. *batch++ = lower_32_bits(addr);
  1070. *batch++ = upper_32_bits(addr);
  1071. *batch++ = lower_32_bits(target_offset);
  1072. *batch++ = upper_32_bits(target_offset);
  1073. }
  1074. } else if (gen >= 6) {
  1075. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  1076. *batch++ = 0;
  1077. *batch++ = addr;
  1078. *batch++ = target_offset;
  1079. } else if (gen >= 4) {
  1080. *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
  1081. *batch++ = 0;
  1082. *batch++ = addr;
  1083. *batch++ = target_offset;
  1084. } else {
  1085. *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
  1086. *batch++ = addr;
  1087. *batch++ = target_offset;
  1088. }
  1089. goto out;
  1090. }
  1091. repeat:
  1092. vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
  1093. if (IS_ERR(vaddr))
  1094. return PTR_ERR(vaddr);
  1095. clflush_write32(vaddr + offset_in_page(offset),
  1096. lower_32_bits(target_offset),
  1097. eb->reloc_cache.vaddr);
  1098. if (wide) {
  1099. offset += sizeof(u32);
  1100. target_offset >>= 32;
  1101. wide = false;
  1102. goto repeat;
  1103. }
  1104. out:
  1105. return target->node.start | UPDATE;
  1106. }
  1107. static u64
  1108. eb_relocate_entry(struct i915_execbuffer *eb,
  1109. struct i915_vma *vma,
  1110. const struct drm_i915_gem_relocation_entry *reloc)
  1111. {
  1112. struct i915_vma *target;
  1113. int err;
  1114. /* we've already hold a reference to all valid objects */
  1115. target = eb_get_vma(eb, reloc->target_handle);
  1116. if (unlikely(!target))
  1117. return -ENOENT;
  1118. /* Validate that the target is in a valid r/w GPU domain */
  1119. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  1120. DRM_DEBUG("reloc with multiple write domains: "
  1121. "target %d offset %d "
  1122. "read %08x write %08x",
  1123. reloc->target_handle,
  1124. (int) reloc->offset,
  1125. reloc->read_domains,
  1126. reloc->write_domain);
  1127. return -EINVAL;
  1128. }
  1129. if (unlikely((reloc->write_domain | reloc->read_domains)
  1130. & ~I915_GEM_GPU_DOMAINS)) {
  1131. DRM_DEBUG("reloc with read/write non-GPU domains: "
  1132. "target %d offset %d "
  1133. "read %08x write %08x",
  1134. reloc->target_handle,
  1135. (int) reloc->offset,
  1136. reloc->read_domains,
  1137. reloc->write_domain);
  1138. return -EINVAL;
  1139. }
  1140. if (reloc->write_domain) {
  1141. *target->exec_flags |= EXEC_OBJECT_WRITE;
  1142. /*
  1143. * Sandybridge PPGTT errata: We need a global gtt mapping
  1144. * for MI and pipe_control writes because the gpu doesn't
  1145. * properly redirect them through the ppgtt for non_secure
  1146. * batchbuffers.
  1147. */
  1148. if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  1149. IS_GEN6(eb->i915)) {
  1150. err = i915_vma_bind(target, target->obj->cache_level,
  1151. PIN_GLOBAL);
  1152. if (WARN_ONCE(err,
  1153. "Unexpected failure to bind target VMA!"))
  1154. return err;
  1155. }
  1156. }
  1157. /*
  1158. * If the relocation already has the right value in it, no
  1159. * more work needs to be done.
  1160. */
  1161. if (!DBG_FORCE_RELOC &&
  1162. gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
  1163. return 0;
  1164. /* Check that the relocation address is valid... */
  1165. if (unlikely(reloc->offset >
  1166. vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
  1167. DRM_DEBUG("Relocation beyond object bounds: "
  1168. "target %d offset %d size %d.\n",
  1169. reloc->target_handle,
  1170. (int)reloc->offset,
  1171. (int)vma->size);
  1172. return -EINVAL;
  1173. }
  1174. if (unlikely(reloc->offset & 3)) {
  1175. DRM_DEBUG("Relocation not 4-byte aligned: "
  1176. "target %d offset %d.\n",
  1177. reloc->target_handle,
  1178. (int)reloc->offset);
  1179. return -EINVAL;
  1180. }
  1181. /*
  1182. * If we write into the object, we need to force the synchronisation
  1183. * barrier, either with an asynchronous clflush or if we executed the
  1184. * patching using the GPU (though that should be serialised by the
  1185. * timeline). To be completely sure, and since we are required to
  1186. * do relocations we are already stalling, disable the user's opt
  1187. * out of our synchronisation.
  1188. */
  1189. *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
  1190. /* and update the user's relocation entry */
  1191. return relocate_entry(vma, reloc, eb, target);
  1192. }
  1193. static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
  1194. {
  1195. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  1196. struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
  1197. struct drm_i915_gem_relocation_entry __user *urelocs;
  1198. const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  1199. unsigned int remain;
  1200. urelocs = u64_to_user_ptr(entry->relocs_ptr);
  1201. remain = entry->relocation_count;
  1202. if (unlikely(remain > N_RELOC(ULONG_MAX)))
  1203. return -EINVAL;
  1204. /*
  1205. * We must check that the entire relocation array is safe
  1206. * to read. However, if the array is not writable the user loses
  1207. * the updated relocation values.
  1208. */
  1209. if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
  1210. return -EFAULT;
  1211. do {
  1212. struct drm_i915_gem_relocation_entry *r = stack;
  1213. unsigned int count =
  1214. min_t(unsigned int, remain, ARRAY_SIZE(stack));
  1215. unsigned int copied;
  1216. /*
  1217. * This is the fast path and we cannot handle a pagefault
  1218. * whilst holding the struct mutex lest the user pass in the
  1219. * relocations contained within a mmaped bo. For in such a case
  1220. * we, the page fault handler would call i915_gem_fault() and
  1221. * we would try to acquire the struct mutex again. Obviously
  1222. * this is bad and so lockdep complains vehemently.
  1223. */
  1224. pagefault_disable();
  1225. copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
  1226. pagefault_enable();
  1227. if (unlikely(copied)) {
  1228. remain = -EFAULT;
  1229. goto out;
  1230. }
  1231. remain -= count;
  1232. do {
  1233. u64 offset = eb_relocate_entry(eb, vma, r);
  1234. if (likely(offset == 0)) {
  1235. } else if ((s64)offset < 0) {
  1236. remain = (int)offset;
  1237. goto out;
  1238. } else {
  1239. /*
  1240. * Note that reporting an error now
  1241. * leaves everything in an inconsistent
  1242. * state as we have *already* changed
  1243. * the relocation value inside the
  1244. * object. As we have not changed the
  1245. * reloc.presumed_offset or will not
  1246. * change the execobject.offset, on the
  1247. * call we may not rewrite the value
  1248. * inside the object, leaving it
  1249. * dangling and causing a GPU hang. Unless
  1250. * userspace dynamically rebuilds the
  1251. * relocations on each execbuf rather than
  1252. * presume a static tree.
  1253. *
  1254. * We did previously check if the relocations
  1255. * were writable (access_ok), an error now
  1256. * would be a strange race with mprotect,
  1257. * having already demonstrated that we
  1258. * can read from this userspace address.
  1259. */
  1260. offset = gen8_canonical_addr(offset & ~UPDATE);
  1261. __put_user(offset,
  1262. &urelocs[r-stack].presumed_offset);
  1263. }
  1264. } while (r++, --count);
  1265. urelocs += ARRAY_SIZE(stack);
  1266. } while (remain);
  1267. out:
  1268. reloc_cache_reset(&eb->reloc_cache);
  1269. return remain;
  1270. }
  1271. static int
  1272. eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
  1273. {
  1274. const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
  1275. struct drm_i915_gem_relocation_entry *relocs =
  1276. u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1277. unsigned int i;
  1278. int err;
  1279. for (i = 0; i < entry->relocation_count; i++) {
  1280. u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
  1281. if ((s64)offset < 0) {
  1282. err = (int)offset;
  1283. goto err;
  1284. }
  1285. }
  1286. err = 0;
  1287. err:
  1288. reloc_cache_reset(&eb->reloc_cache);
  1289. return err;
  1290. }
  1291. static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
  1292. {
  1293. const char __user *addr, *end;
  1294. unsigned long size;
  1295. char __maybe_unused c;
  1296. size = entry->relocation_count;
  1297. if (size == 0)
  1298. return 0;
  1299. if (size > N_RELOC(ULONG_MAX))
  1300. return -EINVAL;
  1301. addr = u64_to_user_ptr(entry->relocs_ptr);
  1302. size *= sizeof(struct drm_i915_gem_relocation_entry);
  1303. if (!access_ok(VERIFY_READ, addr, size))
  1304. return -EFAULT;
  1305. end = addr + size;
  1306. for (; addr < end; addr += PAGE_SIZE) {
  1307. int err = __get_user(c, addr);
  1308. if (err)
  1309. return err;
  1310. }
  1311. return __get_user(c, end - 1);
  1312. }
  1313. static int eb_copy_relocations(const struct i915_execbuffer *eb)
  1314. {
  1315. const unsigned int count = eb->buffer_count;
  1316. unsigned int i;
  1317. int err;
  1318. for (i = 0; i < count; i++) {
  1319. const unsigned int nreloc = eb->exec[i].relocation_count;
  1320. struct drm_i915_gem_relocation_entry __user *urelocs;
  1321. struct drm_i915_gem_relocation_entry *relocs;
  1322. unsigned long size;
  1323. unsigned long copied;
  1324. if (nreloc == 0)
  1325. continue;
  1326. err = check_relocations(&eb->exec[i]);
  1327. if (err)
  1328. goto err;
  1329. urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
  1330. size = nreloc * sizeof(*relocs);
  1331. relocs = kvmalloc_array(size, 1, GFP_KERNEL);
  1332. if (!relocs) {
  1333. kvfree(relocs);
  1334. err = -ENOMEM;
  1335. goto err;
  1336. }
  1337. /* copy_from_user is limited to < 4GiB */
  1338. copied = 0;
  1339. do {
  1340. unsigned int len =
  1341. min_t(u64, BIT_ULL(31), size - copied);
  1342. if (__copy_from_user((char *)relocs + copied,
  1343. (char __user *)urelocs + copied,
  1344. len)) {
  1345. kvfree(relocs);
  1346. err = -EFAULT;
  1347. goto err;
  1348. }
  1349. copied += len;
  1350. } while (copied < size);
  1351. /*
  1352. * As we do not update the known relocation offsets after
  1353. * relocating (due to the complexities in lock handling),
  1354. * we need to mark them as invalid now so that we force the
  1355. * relocation processing next time. Just in case the target
  1356. * object is evicted and then rebound into its old
  1357. * presumed_offset before the next execbuffer - if that
  1358. * happened we would make the mistake of assuming that the
  1359. * relocations were valid.
  1360. */
  1361. user_access_begin();
  1362. for (copied = 0; copied < nreloc; copied++)
  1363. unsafe_put_user(-1,
  1364. &urelocs[copied].presumed_offset,
  1365. end_user);
  1366. end_user:
  1367. user_access_end();
  1368. eb->exec[i].relocs_ptr = (uintptr_t)relocs;
  1369. }
  1370. return 0;
  1371. err:
  1372. while (i--) {
  1373. struct drm_i915_gem_relocation_entry *relocs =
  1374. u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
  1375. if (eb->exec[i].relocation_count)
  1376. kvfree(relocs);
  1377. }
  1378. return err;
  1379. }
  1380. static int eb_prefault_relocations(const struct i915_execbuffer *eb)
  1381. {
  1382. const unsigned int count = eb->buffer_count;
  1383. unsigned int i;
  1384. if (unlikely(i915_modparams.prefault_disable))
  1385. return 0;
  1386. for (i = 0; i < count; i++) {
  1387. int err;
  1388. err = check_relocations(&eb->exec[i]);
  1389. if (err)
  1390. return err;
  1391. }
  1392. return 0;
  1393. }
  1394. static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
  1395. {
  1396. struct drm_device *dev = &eb->i915->drm;
  1397. bool have_copy = false;
  1398. struct i915_vma *vma;
  1399. int err = 0;
  1400. repeat:
  1401. if (signal_pending(current)) {
  1402. err = -ERESTARTSYS;
  1403. goto out;
  1404. }
  1405. /* We may process another execbuffer during the unlock... */
  1406. eb_reset_vmas(eb);
  1407. mutex_unlock(&dev->struct_mutex);
  1408. /*
  1409. * We take 3 passes through the slowpatch.
  1410. *
  1411. * 1 - we try to just prefault all the user relocation entries and
  1412. * then attempt to reuse the atomic pagefault disabled fast path again.
  1413. *
  1414. * 2 - we copy the user entries to a local buffer here outside of the
  1415. * local and allow ourselves to wait upon any rendering before
  1416. * relocations
  1417. *
  1418. * 3 - we already have a local copy of the relocation entries, but
  1419. * were interrupted (EAGAIN) whilst waiting for the objects, try again.
  1420. */
  1421. if (!err) {
  1422. err = eb_prefault_relocations(eb);
  1423. } else if (!have_copy) {
  1424. err = eb_copy_relocations(eb);
  1425. have_copy = err == 0;
  1426. } else {
  1427. cond_resched();
  1428. err = 0;
  1429. }
  1430. if (err) {
  1431. mutex_lock(&dev->struct_mutex);
  1432. goto out;
  1433. }
  1434. /* A frequent cause for EAGAIN are currently unavailable client pages */
  1435. flush_workqueue(eb->i915->mm.userptr_wq);
  1436. err = i915_mutex_lock_interruptible(dev);
  1437. if (err) {
  1438. mutex_lock(&dev->struct_mutex);
  1439. goto out;
  1440. }
  1441. /* reacquire the objects */
  1442. err = eb_lookup_vmas(eb);
  1443. if (err)
  1444. goto err;
  1445. GEM_BUG_ON(!eb->batch);
  1446. list_for_each_entry(vma, &eb->relocs, reloc_link) {
  1447. if (!have_copy) {
  1448. pagefault_disable();
  1449. err = eb_relocate_vma(eb, vma);
  1450. pagefault_enable();
  1451. if (err)
  1452. goto repeat;
  1453. } else {
  1454. err = eb_relocate_vma_slow(eb, vma);
  1455. if (err)
  1456. goto err;
  1457. }
  1458. }
  1459. /*
  1460. * Leave the user relocations as are, this is the painfully slow path,
  1461. * and we want to avoid the complication of dropping the lock whilst
  1462. * having buffers reserved in the aperture and so causing spurious
  1463. * ENOSPC for random operations.
  1464. */
  1465. err:
  1466. if (err == -EAGAIN)
  1467. goto repeat;
  1468. out:
  1469. if (have_copy) {
  1470. const unsigned int count = eb->buffer_count;
  1471. unsigned int i;
  1472. for (i = 0; i < count; i++) {
  1473. const struct drm_i915_gem_exec_object2 *entry =
  1474. &eb->exec[i];
  1475. struct drm_i915_gem_relocation_entry *relocs;
  1476. if (!entry->relocation_count)
  1477. continue;
  1478. relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
  1479. kvfree(relocs);
  1480. }
  1481. }
  1482. return err;
  1483. }
  1484. static int eb_relocate(struct i915_execbuffer *eb)
  1485. {
  1486. if (eb_lookup_vmas(eb))
  1487. goto slow;
  1488. /* The objects are in their final locations, apply the relocations. */
  1489. if (eb->args->flags & __EXEC_HAS_RELOC) {
  1490. struct i915_vma *vma;
  1491. list_for_each_entry(vma, &eb->relocs, reloc_link) {
  1492. if (eb_relocate_vma(eb, vma))
  1493. goto slow;
  1494. }
  1495. }
  1496. return 0;
  1497. slow:
  1498. return eb_relocate_slow(eb);
  1499. }
  1500. static void eb_export_fence(struct i915_vma *vma,
  1501. struct i915_request *rq,
  1502. unsigned int flags)
  1503. {
  1504. struct reservation_object *resv = vma->resv;
  1505. /*
  1506. * Ignore errors from failing to allocate the new fence, we can't
  1507. * handle an error right now. Worst case should be missed
  1508. * synchronisation leading to rendering corruption.
  1509. */
  1510. reservation_object_lock(resv, NULL);
  1511. if (flags & EXEC_OBJECT_WRITE)
  1512. reservation_object_add_excl_fence(resv, &rq->fence);
  1513. else if (reservation_object_reserve_shared(resv) == 0)
  1514. reservation_object_add_shared_fence(resv, &rq->fence);
  1515. reservation_object_unlock(resv);
  1516. }
  1517. static int eb_move_to_gpu(struct i915_execbuffer *eb)
  1518. {
  1519. const unsigned int count = eb->buffer_count;
  1520. unsigned int i;
  1521. int err;
  1522. for (i = 0; i < count; i++) {
  1523. unsigned int flags = eb->flags[i];
  1524. struct i915_vma *vma = eb->vma[i];
  1525. struct drm_i915_gem_object *obj = vma->obj;
  1526. if (flags & EXEC_OBJECT_CAPTURE) {
  1527. struct i915_capture_list *capture;
  1528. capture = kmalloc(sizeof(*capture), GFP_KERNEL);
  1529. if (unlikely(!capture))
  1530. return -ENOMEM;
  1531. capture->next = eb->request->capture_list;
  1532. capture->vma = eb->vma[i];
  1533. eb->request->capture_list = capture;
  1534. }
  1535. /*
  1536. * If the GPU is not _reading_ through the CPU cache, we need
  1537. * to make sure that any writes (both previous GPU writes from
  1538. * before a change in snooping levels and normal CPU writes)
  1539. * caught in that cache are flushed to main memory.
  1540. *
  1541. * We want to say
  1542. * obj->cache_dirty &&
  1543. * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
  1544. * but gcc's optimiser doesn't handle that as well and emits
  1545. * two jumps instead of one. Maybe one day...
  1546. */
  1547. if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
  1548. if (i915_gem_clflush_object(obj, 0))
  1549. flags &= ~EXEC_OBJECT_ASYNC;
  1550. }
  1551. if (flags & EXEC_OBJECT_ASYNC)
  1552. continue;
  1553. err = i915_request_await_object
  1554. (eb->request, obj, flags & EXEC_OBJECT_WRITE);
  1555. if (err)
  1556. return err;
  1557. }
  1558. for (i = 0; i < count; i++) {
  1559. unsigned int flags = eb->flags[i];
  1560. struct i915_vma *vma = eb->vma[i];
  1561. i915_vma_move_to_active(vma, eb->request, flags);
  1562. eb_export_fence(vma, eb->request, flags);
  1563. __eb_unreserve_vma(vma, flags);
  1564. vma->exec_flags = NULL;
  1565. if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
  1566. i915_vma_put(vma);
  1567. }
  1568. eb->exec = NULL;
  1569. /* Unconditionally flush any chipset caches (for streaming writes). */
  1570. i915_gem_chipset_flush(eb->i915);
  1571. return 0;
  1572. }
  1573. static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  1574. {
  1575. if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
  1576. return false;
  1577. /* Kernel clipping was a DRI1 misfeature */
  1578. if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
  1579. if (exec->num_cliprects || exec->cliprects_ptr)
  1580. return false;
  1581. }
  1582. if (exec->DR4 == 0xffffffff) {
  1583. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  1584. exec->DR4 = 0;
  1585. }
  1586. if (exec->DR1 || exec->DR4)
  1587. return false;
  1588. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  1589. return false;
  1590. return true;
  1591. }
  1592. void i915_vma_move_to_active(struct i915_vma *vma,
  1593. struct i915_request *rq,
  1594. unsigned int flags)
  1595. {
  1596. struct drm_i915_gem_object *obj = vma->obj;
  1597. const unsigned int idx = rq->engine->id;
  1598. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  1599. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1600. /*
  1601. * Add a reference if we're newly entering the active list.
  1602. * The order in which we add operations to the retirement queue is
  1603. * vital here: mark_active adds to the start of the callback list,
  1604. * such that subsequent callbacks are called first. Therefore we
  1605. * add the active reference first and queue for it to be dropped
  1606. * *last*.
  1607. */
  1608. if (!i915_vma_is_active(vma))
  1609. obj->active_count++;
  1610. i915_vma_set_active(vma, idx);
  1611. i915_gem_active_set(&vma->last_read[idx], rq);
  1612. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1613. obj->write_domain = 0;
  1614. if (flags & EXEC_OBJECT_WRITE) {
  1615. obj->write_domain = I915_GEM_DOMAIN_RENDER;
  1616. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1617. i915_gem_active_set(&obj->frontbuffer_write, rq);
  1618. obj->read_domains = 0;
  1619. }
  1620. obj->read_domains |= I915_GEM_GPU_DOMAINS;
  1621. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1622. i915_gem_active_set(&vma->last_fence, rq);
  1623. }
  1624. static int i915_reset_gen7_sol_offsets(struct i915_request *rq)
  1625. {
  1626. u32 *cs;
  1627. int i;
  1628. if (!IS_GEN7(rq->i915) || rq->engine->id != RCS) {
  1629. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1630. return -EINVAL;
  1631. }
  1632. cs = intel_ring_begin(rq, 4 * 2 + 2);
  1633. if (IS_ERR(cs))
  1634. return PTR_ERR(cs);
  1635. *cs++ = MI_LOAD_REGISTER_IMM(4);
  1636. for (i = 0; i < 4; i++) {
  1637. *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
  1638. *cs++ = 0;
  1639. }
  1640. *cs++ = MI_NOOP;
  1641. intel_ring_advance(rq, cs);
  1642. return 0;
  1643. }
  1644. static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
  1645. {
  1646. struct drm_i915_gem_object *shadow_batch_obj;
  1647. struct i915_vma *vma;
  1648. int err;
  1649. shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
  1650. PAGE_ALIGN(eb->batch_len));
  1651. if (IS_ERR(shadow_batch_obj))
  1652. return ERR_CAST(shadow_batch_obj);
  1653. err = intel_engine_cmd_parser(eb->engine,
  1654. eb->batch->obj,
  1655. shadow_batch_obj,
  1656. eb->batch_start_offset,
  1657. eb->batch_len,
  1658. is_master);
  1659. if (err) {
  1660. if (err == -EACCES) /* unhandled chained batch */
  1661. vma = NULL;
  1662. else
  1663. vma = ERR_PTR(err);
  1664. goto out;
  1665. }
  1666. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1667. if (IS_ERR(vma))
  1668. goto out;
  1669. eb->vma[eb->buffer_count] = i915_vma_get(vma);
  1670. eb->flags[eb->buffer_count] =
  1671. __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
  1672. vma->exec_flags = &eb->flags[eb->buffer_count];
  1673. eb->buffer_count++;
  1674. out:
  1675. i915_gem_object_unpin_pages(shadow_batch_obj);
  1676. return vma;
  1677. }
  1678. static void
  1679. add_to_client(struct i915_request *rq, struct drm_file *file)
  1680. {
  1681. rq->file_priv = file->driver_priv;
  1682. list_add_tail(&rq->client_link, &rq->file_priv->mm.request_list);
  1683. }
  1684. static int eb_submit(struct i915_execbuffer *eb)
  1685. {
  1686. int err;
  1687. err = eb_move_to_gpu(eb);
  1688. if (err)
  1689. return err;
  1690. if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1691. err = i915_reset_gen7_sol_offsets(eb->request);
  1692. if (err)
  1693. return err;
  1694. }
  1695. err = eb->engine->emit_bb_start(eb->request,
  1696. eb->batch->node.start +
  1697. eb->batch_start_offset,
  1698. eb->batch_len,
  1699. eb->batch_flags);
  1700. if (err)
  1701. return err;
  1702. return 0;
  1703. }
  1704. /*
  1705. * Find one BSD ring to dispatch the corresponding BSD command.
  1706. * The engine index is returned.
  1707. */
  1708. static unsigned int
  1709. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1710. struct drm_file *file)
  1711. {
  1712. struct drm_i915_file_private *file_priv = file->driver_priv;
  1713. /* Check whether the file_priv has already selected one ring. */
  1714. if ((int)file_priv->bsd_engine < 0)
  1715. file_priv->bsd_engine = atomic_fetch_xor(1,
  1716. &dev_priv->mm.bsd_engine_dispatch_index);
  1717. return file_priv->bsd_engine;
  1718. }
  1719. #define I915_USER_RINGS (4)
  1720. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1721. [I915_EXEC_DEFAULT] = RCS,
  1722. [I915_EXEC_RENDER] = RCS,
  1723. [I915_EXEC_BLT] = BCS,
  1724. [I915_EXEC_BSD] = VCS,
  1725. [I915_EXEC_VEBOX] = VECS
  1726. };
  1727. static struct intel_engine_cs *
  1728. eb_select_engine(struct drm_i915_private *dev_priv,
  1729. struct drm_file *file,
  1730. struct drm_i915_gem_execbuffer2 *args)
  1731. {
  1732. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1733. struct intel_engine_cs *engine;
  1734. if (user_ring_id > I915_USER_RINGS) {
  1735. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1736. return NULL;
  1737. }
  1738. if ((user_ring_id != I915_EXEC_BSD) &&
  1739. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1740. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1741. "bsd dispatch flags: %d\n", (int)(args->flags));
  1742. return NULL;
  1743. }
  1744. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1745. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1746. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1747. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1748. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1749. bsd_idx <= I915_EXEC_BSD_RING2) {
  1750. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1751. bsd_idx--;
  1752. } else {
  1753. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1754. bsd_idx);
  1755. return NULL;
  1756. }
  1757. engine = dev_priv->engine[_VCS(bsd_idx)];
  1758. } else {
  1759. engine = dev_priv->engine[user_ring_map[user_ring_id]];
  1760. }
  1761. if (!engine) {
  1762. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1763. return NULL;
  1764. }
  1765. return engine;
  1766. }
  1767. static void
  1768. __free_fence_array(struct drm_syncobj **fences, unsigned int n)
  1769. {
  1770. while (n--)
  1771. drm_syncobj_put(ptr_mask_bits(fences[n], 2));
  1772. kvfree(fences);
  1773. }
  1774. static struct drm_syncobj **
  1775. get_fence_array(struct drm_i915_gem_execbuffer2 *args,
  1776. struct drm_file *file)
  1777. {
  1778. const unsigned long nfences = args->num_cliprects;
  1779. struct drm_i915_gem_exec_fence __user *user;
  1780. struct drm_syncobj **fences;
  1781. unsigned long n;
  1782. int err;
  1783. if (!(args->flags & I915_EXEC_FENCE_ARRAY))
  1784. return NULL;
  1785. /* Check multiplication overflow for access_ok() and kvmalloc_array() */
  1786. BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
  1787. if (nfences > min_t(unsigned long,
  1788. ULONG_MAX / sizeof(*user),
  1789. SIZE_MAX / sizeof(*fences)))
  1790. return ERR_PTR(-EINVAL);
  1791. user = u64_to_user_ptr(args->cliprects_ptr);
  1792. if (!access_ok(VERIFY_READ, user, nfences * sizeof(*user)))
  1793. return ERR_PTR(-EFAULT);
  1794. fences = kvmalloc_array(nfences, sizeof(*fences),
  1795. __GFP_NOWARN | GFP_KERNEL);
  1796. if (!fences)
  1797. return ERR_PTR(-ENOMEM);
  1798. for (n = 0; n < nfences; n++) {
  1799. struct drm_i915_gem_exec_fence fence;
  1800. struct drm_syncobj *syncobj;
  1801. if (__copy_from_user(&fence, user++, sizeof(fence))) {
  1802. err = -EFAULT;
  1803. goto err;
  1804. }
  1805. if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) {
  1806. err = -EINVAL;
  1807. goto err;
  1808. }
  1809. syncobj = drm_syncobj_find(file, fence.handle);
  1810. if (!syncobj) {
  1811. DRM_DEBUG("Invalid syncobj handle provided\n");
  1812. err = -ENOENT;
  1813. goto err;
  1814. }
  1815. BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
  1816. ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
  1817. fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
  1818. }
  1819. return fences;
  1820. err:
  1821. __free_fence_array(fences, n);
  1822. return ERR_PTR(err);
  1823. }
  1824. static void
  1825. put_fence_array(struct drm_i915_gem_execbuffer2 *args,
  1826. struct drm_syncobj **fences)
  1827. {
  1828. if (fences)
  1829. __free_fence_array(fences, args->num_cliprects);
  1830. }
  1831. static int
  1832. await_fence_array(struct i915_execbuffer *eb,
  1833. struct drm_syncobj **fences)
  1834. {
  1835. const unsigned int nfences = eb->args->num_cliprects;
  1836. unsigned int n;
  1837. int err;
  1838. for (n = 0; n < nfences; n++) {
  1839. struct drm_syncobj *syncobj;
  1840. struct dma_fence *fence;
  1841. unsigned int flags;
  1842. syncobj = ptr_unpack_bits(fences[n], &flags, 2);
  1843. if (!(flags & I915_EXEC_FENCE_WAIT))
  1844. continue;
  1845. fence = drm_syncobj_fence_get(syncobj);
  1846. if (!fence)
  1847. return -EINVAL;
  1848. err = i915_request_await_dma_fence(eb->request, fence);
  1849. dma_fence_put(fence);
  1850. if (err < 0)
  1851. return err;
  1852. }
  1853. return 0;
  1854. }
  1855. static void
  1856. signal_fence_array(struct i915_execbuffer *eb,
  1857. struct drm_syncobj **fences)
  1858. {
  1859. const unsigned int nfences = eb->args->num_cliprects;
  1860. struct dma_fence * const fence = &eb->request->fence;
  1861. unsigned int n;
  1862. for (n = 0; n < nfences; n++) {
  1863. struct drm_syncobj *syncobj;
  1864. unsigned int flags;
  1865. syncobj = ptr_unpack_bits(fences[n], &flags, 2);
  1866. if (!(flags & I915_EXEC_FENCE_SIGNAL))
  1867. continue;
  1868. drm_syncobj_replace_fence(syncobj, fence);
  1869. }
  1870. }
  1871. static int
  1872. i915_gem_do_execbuffer(struct drm_device *dev,
  1873. struct drm_file *file,
  1874. struct drm_i915_gem_execbuffer2 *args,
  1875. struct drm_i915_gem_exec_object2 *exec,
  1876. struct drm_syncobj **fences)
  1877. {
  1878. struct i915_execbuffer eb;
  1879. struct dma_fence *in_fence = NULL;
  1880. struct sync_file *out_fence = NULL;
  1881. int out_fence_fd = -1;
  1882. int err;
  1883. BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
  1884. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
  1885. ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  1886. eb.i915 = to_i915(dev);
  1887. eb.file = file;
  1888. eb.args = args;
  1889. if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
  1890. args->flags |= __EXEC_HAS_RELOC;
  1891. eb.exec = exec;
  1892. eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
  1893. eb.vma[0] = NULL;
  1894. eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
  1895. eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  1896. if (USES_FULL_PPGTT(eb.i915))
  1897. eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  1898. reloc_cache_init(&eb.reloc_cache, eb.i915);
  1899. eb.buffer_count = args->buffer_count;
  1900. eb.batch_start_offset = args->batch_start_offset;
  1901. eb.batch_len = args->batch_len;
  1902. eb.batch_flags = 0;
  1903. if (args->flags & I915_EXEC_SECURE) {
  1904. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1905. return -EPERM;
  1906. eb.batch_flags |= I915_DISPATCH_SECURE;
  1907. }
  1908. if (args->flags & I915_EXEC_IS_PINNED)
  1909. eb.batch_flags |= I915_DISPATCH_PINNED;
  1910. eb.engine = eb_select_engine(eb.i915, file, args);
  1911. if (!eb.engine)
  1912. return -EINVAL;
  1913. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1914. if (!HAS_RESOURCE_STREAMER(eb.i915)) {
  1915. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1916. return -EINVAL;
  1917. }
  1918. if (eb.engine->id != RCS) {
  1919. DRM_DEBUG("RS is not available on %s\n",
  1920. eb.engine->name);
  1921. return -EINVAL;
  1922. }
  1923. eb.batch_flags |= I915_DISPATCH_RS;
  1924. }
  1925. if (args->flags & I915_EXEC_FENCE_IN) {
  1926. in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
  1927. if (!in_fence)
  1928. return -EINVAL;
  1929. }
  1930. if (args->flags & I915_EXEC_FENCE_OUT) {
  1931. out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  1932. if (out_fence_fd < 0) {
  1933. err = out_fence_fd;
  1934. goto err_in_fence;
  1935. }
  1936. }
  1937. err = eb_create(&eb);
  1938. if (err)
  1939. goto err_out_fence;
  1940. GEM_BUG_ON(!eb.lut_size);
  1941. err = eb_select_context(&eb);
  1942. if (unlikely(err))
  1943. goto err_destroy;
  1944. /*
  1945. * Take a local wakeref for preparing to dispatch the execbuf as
  1946. * we expect to access the hardware fairly frequently in the
  1947. * process. Upon first dispatch, we acquire another prolonged
  1948. * wakeref that we hold until the GPU has been idle for at least
  1949. * 100ms.
  1950. */
  1951. intel_runtime_pm_get(eb.i915);
  1952. err = i915_mutex_lock_interruptible(dev);
  1953. if (err)
  1954. goto err_rpm;
  1955. err = eb_relocate(&eb);
  1956. if (err) {
  1957. /*
  1958. * If the user expects the execobject.offset and
  1959. * reloc.presumed_offset to be an exact match,
  1960. * as for using NO_RELOC, then we cannot update
  1961. * the execobject.offset until we have completed
  1962. * relocation.
  1963. */
  1964. args->flags &= ~__EXEC_HAS_RELOC;
  1965. goto err_vma;
  1966. }
  1967. if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
  1968. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1969. err = -EINVAL;
  1970. goto err_vma;
  1971. }
  1972. if (eb.batch_start_offset > eb.batch->size ||
  1973. eb.batch_len > eb.batch->size - eb.batch_start_offset) {
  1974. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1975. err = -EINVAL;
  1976. goto err_vma;
  1977. }
  1978. if (eb_use_cmdparser(&eb)) {
  1979. struct i915_vma *vma;
  1980. vma = eb_parse(&eb, drm_is_current_master(file));
  1981. if (IS_ERR(vma)) {
  1982. err = PTR_ERR(vma);
  1983. goto err_vma;
  1984. }
  1985. if (vma) {
  1986. /*
  1987. * Batch parsed and accepted:
  1988. *
  1989. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1990. * bit from MI_BATCH_BUFFER_START commands issued in
  1991. * the dispatch_execbuffer implementations. We
  1992. * specifically don't want that set on batches the
  1993. * command parser has accepted.
  1994. */
  1995. eb.batch_flags |= I915_DISPATCH_SECURE;
  1996. eb.batch_start_offset = 0;
  1997. eb.batch = vma;
  1998. }
  1999. }
  2000. if (eb.batch_len == 0)
  2001. eb.batch_len = eb.batch->size - eb.batch_start_offset;
  2002. /*
  2003. * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  2004. * batch" bit. Hence we need to pin secure batches into the global gtt.
  2005. * hsw should have this fixed, but bdw mucks it up again. */
  2006. if (eb.batch_flags & I915_DISPATCH_SECURE) {
  2007. struct i915_vma *vma;
  2008. /*
  2009. * So on first glance it looks freaky that we pin the batch here
  2010. * outside of the reservation loop. But:
  2011. * - The batch is already pinned into the relevant ppgtt, so we
  2012. * already have the backing storage fully allocated.
  2013. * - No other BO uses the global gtt (well contexts, but meh),
  2014. * so we don't really have issues with multiple objects not
  2015. * fitting due to fragmentation.
  2016. * So this is actually safe.
  2017. */
  2018. vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
  2019. if (IS_ERR(vma)) {
  2020. err = PTR_ERR(vma);
  2021. goto err_vma;
  2022. }
  2023. eb.batch = vma;
  2024. }
  2025. /* All GPU relocation batches must be submitted prior to the user rq */
  2026. GEM_BUG_ON(eb.reloc_cache.rq);
  2027. /* Allocate a request for this batch buffer nice and early. */
  2028. eb.request = i915_request_alloc(eb.engine, eb.ctx);
  2029. if (IS_ERR(eb.request)) {
  2030. err = PTR_ERR(eb.request);
  2031. goto err_batch_unpin;
  2032. }
  2033. if (in_fence) {
  2034. err = i915_request_await_dma_fence(eb.request, in_fence);
  2035. if (err < 0)
  2036. goto err_request;
  2037. }
  2038. if (fences) {
  2039. err = await_fence_array(&eb, fences);
  2040. if (err)
  2041. goto err_request;
  2042. }
  2043. if (out_fence_fd != -1) {
  2044. out_fence = sync_file_create(&eb.request->fence);
  2045. if (!out_fence) {
  2046. err = -ENOMEM;
  2047. goto err_request;
  2048. }
  2049. }
  2050. /*
  2051. * Whilst this request exists, batch_obj will be on the
  2052. * active_list, and so will hold the active reference. Only when this
  2053. * request is retired will the the batch_obj be moved onto the
  2054. * inactive_list and lose its active reference. Hence we do not need
  2055. * to explicitly hold another reference here.
  2056. */
  2057. eb.request->batch = eb.batch;
  2058. trace_i915_request_queue(eb.request, eb.batch_flags);
  2059. err = eb_submit(&eb);
  2060. err_request:
  2061. i915_request_add(eb.request);
  2062. add_to_client(eb.request, file);
  2063. if (fences)
  2064. signal_fence_array(&eb, fences);
  2065. if (out_fence) {
  2066. if (err == 0) {
  2067. fd_install(out_fence_fd, out_fence->file);
  2068. args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */
  2069. args->rsvd2 |= (u64)out_fence_fd << 32;
  2070. out_fence_fd = -1;
  2071. } else {
  2072. fput(out_fence->file);
  2073. }
  2074. }
  2075. err_batch_unpin:
  2076. if (eb.batch_flags & I915_DISPATCH_SECURE)
  2077. i915_vma_unpin(eb.batch);
  2078. err_vma:
  2079. if (eb.exec)
  2080. eb_release_vmas(&eb);
  2081. mutex_unlock(&dev->struct_mutex);
  2082. err_rpm:
  2083. intel_runtime_pm_put(eb.i915);
  2084. i915_gem_context_put(eb.ctx);
  2085. err_destroy:
  2086. eb_destroy(&eb);
  2087. err_out_fence:
  2088. if (out_fence_fd != -1)
  2089. put_unused_fd(out_fence_fd);
  2090. err_in_fence:
  2091. dma_fence_put(in_fence);
  2092. return err;
  2093. }
  2094. static size_t eb_element_size(void)
  2095. {
  2096. return (sizeof(struct drm_i915_gem_exec_object2) +
  2097. sizeof(struct i915_vma *) +
  2098. sizeof(unsigned int));
  2099. }
  2100. static bool check_buffer_count(size_t count)
  2101. {
  2102. const size_t sz = eb_element_size();
  2103. /*
  2104. * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
  2105. * array size (see eb_create()). Otherwise, we can accept an array as
  2106. * large as can be addressed (though use large arrays at your peril)!
  2107. */
  2108. return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
  2109. }
  2110. /*
  2111. * Legacy execbuffer just creates an exec2 list from the original exec object
  2112. * list array and passes it to the real function.
  2113. */
  2114. int
  2115. i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data,
  2116. struct drm_file *file)
  2117. {
  2118. struct drm_i915_gem_execbuffer *args = data;
  2119. struct drm_i915_gem_execbuffer2 exec2;
  2120. struct drm_i915_gem_exec_object *exec_list = NULL;
  2121. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  2122. const size_t count = args->buffer_count;
  2123. unsigned int i;
  2124. int err;
  2125. if (!check_buffer_count(count)) {
  2126. DRM_DEBUG("execbuf2 with %zd buffers\n", count);
  2127. return -EINVAL;
  2128. }
  2129. exec2.buffers_ptr = args->buffers_ptr;
  2130. exec2.buffer_count = args->buffer_count;
  2131. exec2.batch_start_offset = args->batch_start_offset;
  2132. exec2.batch_len = args->batch_len;
  2133. exec2.DR1 = args->DR1;
  2134. exec2.DR4 = args->DR4;
  2135. exec2.num_cliprects = args->num_cliprects;
  2136. exec2.cliprects_ptr = args->cliprects_ptr;
  2137. exec2.flags = I915_EXEC_RENDER;
  2138. i915_execbuffer2_set_context_id(exec2, 0);
  2139. if (!i915_gem_check_execbuffer(&exec2))
  2140. return -EINVAL;
  2141. /* Copy in the exec list from userland */
  2142. exec_list = kvmalloc_array(count, sizeof(*exec_list),
  2143. __GFP_NOWARN | GFP_KERNEL);
  2144. exec2_list = kvmalloc_array(count + 1, eb_element_size(),
  2145. __GFP_NOWARN | GFP_KERNEL);
  2146. if (exec_list == NULL || exec2_list == NULL) {
  2147. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  2148. args->buffer_count);
  2149. kvfree(exec_list);
  2150. kvfree(exec2_list);
  2151. return -ENOMEM;
  2152. }
  2153. err = copy_from_user(exec_list,
  2154. u64_to_user_ptr(args->buffers_ptr),
  2155. sizeof(*exec_list) * count);
  2156. if (err) {
  2157. DRM_DEBUG("copy %d exec entries failed %d\n",
  2158. args->buffer_count, err);
  2159. kvfree(exec_list);
  2160. kvfree(exec2_list);
  2161. return -EFAULT;
  2162. }
  2163. for (i = 0; i < args->buffer_count; i++) {
  2164. exec2_list[i].handle = exec_list[i].handle;
  2165. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  2166. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  2167. exec2_list[i].alignment = exec_list[i].alignment;
  2168. exec2_list[i].offset = exec_list[i].offset;
  2169. if (INTEL_GEN(to_i915(dev)) < 4)
  2170. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  2171. else
  2172. exec2_list[i].flags = 0;
  2173. }
  2174. err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
  2175. if (exec2.flags & __EXEC_HAS_RELOC) {
  2176. struct drm_i915_gem_exec_object __user *user_exec_list =
  2177. u64_to_user_ptr(args->buffers_ptr);
  2178. /* Copy the new buffer offsets back to the user's exec list. */
  2179. for (i = 0; i < args->buffer_count; i++) {
  2180. if (!(exec2_list[i].offset & UPDATE))
  2181. continue;
  2182. exec2_list[i].offset =
  2183. gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
  2184. exec2_list[i].offset &= PIN_OFFSET_MASK;
  2185. if (__copy_to_user(&user_exec_list[i].offset,
  2186. &exec2_list[i].offset,
  2187. sizeof(user_exec_list[i].offset)))
  2188. break;
  2189. }
  2190. }
  2191. kvfree(exec_list);
  2192. kvfree(exec2_list);
  2193. return err;
  2194. }
  2195. int
  2196. i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
  2197. struct drm_file *file)
  2198. {
  2199. struct drm_i915_gem_execbuffer2 *args = data;
  2200. struct drm_i915_gem_exec_object2 *exec2_list;
  2201. struct drm_syncobj **fences = NULL;
  2202. const size_t count = args->buffer_count;
  2203. int err;
  2204. if (!check_buffer_count(count)) {
  2205. DRM_DEBUG("execbuf2 with %zd buffers\n", count);
  2206. return -EINVAL;
  2207. }
  2208. if (!i915_gem_check_execbuffer(args))
  2209. return -EINVAL;
  2210. /* Allocate an extra slot for use by the command parser */
  2211. exec2_list = kvmalloc_array(count + 1, eb_element_size(),
  2212. __GFP_NOWARN | GFP_KERNEL);
  2213. if (exec2_list == NULL) {
  2214. DRM_DEBUG("Failed to allocate exec list for %zd buffers\n",
  2215. count);
  2216. return -ENOMEM;
  2217. }
  2218. if (copy_from_user(exec2_list,
  2219. u64_to_user_ptr(args->buffers_ptr),
  2220. sizeof(*exec2_list) * count)) {
  2221. DRM_DEBUG("copy %zd exec entries failed\n", count);
  2222. kvfree(exec2_list);
  2223. return -EFAULT;
  2224. }
  2225. if (args->flags & I915_EXEC_FENCE_ARRAY) {
  2226. fences = get_fence_array(args, file);
  2227. if (IS_ERR(fences)) {
  2228. kvfree(exec2_list);
  2229. return PTR_ERR(fences);
  2230. }
  2231. }
  2232. err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
  2233. /*
  2234. * Now that we have begun execution of the batchbuffer, we ignore
  2235. * any new error after this point. Also given that we have already
  2236. * updated the associated relocations, we try to write out the current
  2237. * object locations irrespective of any error.
  2238. */
  2239. if (args->flags & __EXEC_HAS_RELOC) {
  2240. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  2241. u64_to_user_ptr(args->buffers_ptr);
  2242. unsigned int i;
  2243. /* Copy the new buffer offsets back to the user's exec list. */
  2244. user_access_begin();
  2245. for (i = 0; i < args->buffer_count; i++) {
  2246. if (!(exec2_list[i].offset & UPDATE))
  2247. continue;
  2248. exec2_list[i].offset =
  2249. gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
  2250. unsafe_put_user(exec2_list[i].offset,
  2251. &user_exec_list[i].offset,
  2252. end_user);
  2253. }
  2254. end_user:
  2255. user_access_end();
  2256. }
  2257. args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
  2258. put_fence_array(args, fences);
  2259. kvfree(exec2_list);
  2260. return err;
  2261. }