i915_gem_execbuffer.c 72 KB

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