i915_gem_execbuffer.c 71 KB

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