i915_gem_execbuffer.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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/uaccess.h>
  31. #include <drm/drmP.h>
  32. #include <drm/i915_drm.h>
  33. #include "i915_drv.h"
  34. #include "i915_trace.h"
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
  38. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  39. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  40. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  41. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  42. #define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
  43. #define BATCH_OFFSET_BIAS (256*1024)
  44. struct i915_execbuffer_params {
  45. struct drm_device *dev;
  46. struct drm_file *file;
  47. struct i915_vma *batch;
  48. u32 dispatch_flags;
  49. u32 args_batch_start_offset;
  50. struct intel_engine_cs *engine;
  51. struct i915_gem_context *ctx;
  52. struct drm_i915_gem_request *request;
  53. };
  54. struct eb_vmas {
  55. struct drm_i915_private *i915;
  56. struct list_head vmas;
  57. int and;
  58. union {
  59. struct i915_vma *lut[0];
  60. struct hlist_head buckets[0];
  61. };
  62. };
  63. static struct eb_vmas *
  64. eb_create(struct drm_i915_private *i915,
  65. struct drm_i915_gem_execbuffer2 *args)
  66. {
  67. struct eb_vmas *eb = NULL;
  68. if (args->flags & I915_EXEC_HANDLE_LUT) {
  69. unsigned size = args->buffer_count;
  70. size *= sizeof(struct i915_vma *);
  71. size += sizeof(struct eb_vmas);
  72. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  73. }
  74. if (eb == NULL) {
  75. unsigned size = args->buffer_count;
  76. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  77. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  78. while (count > 2*size)
  79. count >>= 1;
  80. eb = kzalloc(count*sizeof(struct hlist_head) +
  81. sizeof(struct eb_vmas),
  82. GFP_TEMPORARY);
  83. if (eb == NULL)
  84. return eb;
  85. eb->and = count - 1;
  86. } else
  87. eb->and = -args->buffer_count;
  88. eb->i915 = i915;
  89. INIT_LIST_HEAD(&eb->vmas);
  90. return eb;
  91. }
  92. static void
  93. eb_reset(struct eb_vmas *eb)
  94. {
  95. if (eb->and >= 0)
  96. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  97. }
  98. static struct i915_vma *
  99. eb_get_batch(struct eb_vmas *eb)
  100. {
  101. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  102. /*
  103. * SNA is doing fancy tricks with compressing batch buffers, which leads
  104. * to negative relocation deltas. Usually that works out ok since the
  105. * relocate address is still positive, except when the batch is placed
  106. * very low in the GTT. Ensure this doesn't happen.
  107. *
  108. * Note that actual hangs have only been observed on gen7, but for
  109. * paranoia do it everywhere.
  110. */
  111. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  112. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  113. return vma;
  114. }
  115. static int
  116. eb_lookup_vmas(struct eb_vmas *eb,
  117. struct drm_i915_gem_exec_object2 *exec,
  118. const struct drm_i915_gem_execbuffer2 *args,
  119. struct i915_address_space *vm,
  120. struct drm_file *file)
  121. {
  122. struct drm_i915_gem_object *obj;
  123. struct list_head objects;
  124. int i, ret;
  125. INIT_LIST_HEAD(&objects);
  126. spin_lock(&file->table_lock);
  127. /* Grab a reference to the object and release the lock so we can lookup
  128. * or create the VMA without using GFP_ATOMIC */
  129. for (i = 0; i < args->buffer_count; i++) {
  130. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  131. if (obj == NULL) {
  132. spin_unlock(&file->table_lock);
  133. DRM_DEBUG("Invalid object handle %d at index %d\n",
  134. exec[i].handle, i);
  135. ret = -ENOENT;
  136. goto err;
  137. }
  138. if (!list_empty(&obj->obj_exec_link)) {
  139. spin_unlock(&file->table_lock);
  140. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  141. obj, exec[i].handle, i);
  142. ret = -EINVAL;
  143. goto err;
  144. }
  145. i915_gem_object_get(obj);
  146. list_add_tail(&obj->obj_exec_link, &objects);
  147. }
  148. spin_unlock(&file->table_lock);
  149. i = 0;
  150. while (!list_empty(&objects)) {
  151. struct i915_vma *vma;
  152. obj = list_first_entry(&objects,
  153. struct drm_i915_gem_object,
  154. obj_exec_link);
  155. /*
  156. * NOTE: We can leak any vmas created here when something fails
  157. * later on. But that's no issue since vma_unbind can deal with
  158. * vmas which are not actually bound. And since only
  159. * lookup_or_create exists as an interface to get at the vma
  160. * from the (obj, vm) we don't run the risk of creating
  161. * duplicated vmas for the same vm.
  162. */
  163. vma = i915_gem_obj_lookup_or_create_vma(obj, vm, NULL);
  164. if (unlikely(IS_ERR(vma))) {
  165. DRM_DEBUG("Failed to lookup VMA\n");
  166. ret = PTR_ERR(vma);
  167. goto err;
  168. }
  169. /* Transfer ownership from the objects list to the vmas list. */
  170. list_add_tail(&vma->exec_list, &eb->vmas);
  171. list_del_init(&obj->obj_exec_link);
  172. vma->exec_entry = &exec[i];
  173. if (eb->and < 0) {
  174. eb->lut[i] = vma;
  175. } else {
  176. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  177. vma->exec_handle = handle;
  178. hlist_add_head(&vma->exec_node,
  179. &eb->buckets[handle & eb->and]);
  180. }
  181. ++i;
  182. }
  183. return 0;
  184. err:
  185. while (!list_empty(&objects)) {
  186. obj = list_first_entry(&objects,
  187. struct drm_i915_gem_object,
  188. obj_exec_link);
  189. list_del_init(&obj->obj_exec_link);
  190. i915_gem_object_put(obj);
  191. }
  192. /*
  193. * Objects already transfered to the vmas list will be unreferenced by
  194. * eb_destroy.
  195. */
  196. return ret;
  197. }
  198. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  199. {
  200. if (eb->and < 0) {
  201. if (handle >= -eb->and)
  202. return NULL;
  203. return eb->lut[handle];
  204. } else {
  205. struct hlist_head *head;
  206. struct i915_vma *vma;
  207. head = &eb->buckets[handle & eb->and];
  208. hlist_for_each_entry(vma, head, exec_node) {
  209. if (vma->exec_handle == handle)
  210. return vma;
  211. }
  212. return NULL;
  213. }
  214. }
  215. static void
  216. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  217. {
  218. struct drm_i915_gem_exec_object2 *entry;
  219. if (!drm_mm_node_allocated(&vma->node))
  220. return;
  221. entry = vma->exec_entry;
  222. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  223. i915_vma_unpin_fence(vma);
  224. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  225. __i915_vma_unpin(vma);
  226. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  227. }
  228. static void eb_destroy(struct eb_vmas *eb)
  229. {
  230. while (!list_empty(&eb->vmas)) {
  231. struct i915_vma *vma;
  232. vma = list_first_entry(&eb->vmas,
  233. struct i915_vma,
  234. exec_list);
  235. list_del_init(&vma->exec_list);
  236. i915_gem_execbuffer_unreserve_vma(vma);
  237. i915_vma_put(vma);
  238. }
  239. kfree(eb);
  240. }
  241. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  242. {
  243. if (!i915_gem_object_has_struct_page(obj))
  244. return false;
  245. if (DBG_USE_CPU_RELOC)
  246. return DBG_USE_CPU_RELOC > 0;
  247. return (HAS_LLC(to_i915(obj->base.dev)) ||
  248. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  249. obj->cache_level != I915_CACHE_NONE);
  250. }
  251. /* Used to convert any address to canonical form.
  252. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  253. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  254. * addresses to be in a canonical form:
  255. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  256. * canonical form [63:48] == [47]."
  257. */
  258. #define GEN8_HIGH_ADDRESS_BIT 47
  259. static inline uint64_t gen8_canonical_addr(uint64_t address)
  260. {
  261. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  262. }
  263. static inline uint64_t gen8_noncanonical_addr(uint64_t address)
  264. {
  265. return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
  266. }
  267. static inline uint64_t
  268. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  269. uint64_t target_offset)
  270. {
  271. return gen8_canonical_addr((int)reloc->delta + target_offset);
  272. }
  273. struct reloc_cache {
  274. struct drm_i915_private *i915;
  275. struct drm_mm_node node;
  276. unsigned long vaddr;
  277. unsigned int page;
  278. bool use_64bit_reloc;
  279. };
  280. static void reloc_cache_init(struct reloc_cache *cache,
  281. struct drm_i915_private *i915)
  282. {
  283. cache->page = -1;
  284. cache->vaddr = 0;
  285. cache->i915 = i915;
  286. /* Must be a variable in the struct to allow GCC to unroll. */
  287. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  288. cache->node.allocated = false;
  289. }
  290. static inline void *unmask_page(unsigned long p)
  291. {
  292. return (void *)(uintptr_t)(p & PAGE_MASK);
  293. }
  294. static inline unsigned int unmask_flags(unsigned long p)
  295. {
  296. return p & ~PAGE_MASK;
  297. }
  298. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  299. static void reloc_cache_fini(struct reloc_cache *cache)
  300. {
  301. void *vaddr;
  302. if (!cache->vaddr)
  303. return;
  304. vaddr = unmask_page(cache->vaddr);
  305. if (cache->vaddr & KMAP) {
  306. if (cache->vaddr & CLFLUSH_AFTER)
  307. mb();
  308. kunmap_atomic(vaddr);
  309. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  310. } else {
  311. wmb();
  312. io_mapping_unmap_atomic((void __iomem *)vaddr);
  313. if (cache->node.allocated) {
  314. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  315. ggtt->base.clear_range(&ggtt->base,
  316. cache->node.start,
  317. cache->node.size);
  318. drm_mm_remove_node(&cache->node);
  319. } else {
  320. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  321. }
  322. }
  323. }
  324. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  325. struct reloc_cache *cache,
  326. int page)
  327. {
  328. void *vaddr;
  329. if (cache->vaddr) {
  330. kunmap_atomic(unmask_page(cache->vaddr));
  331. } else {
  332. unsigned int flushes;
  333. int ret;
  334. ret = i915_gem_obj_prepare_shmem_write(obj, &flushes);
  335. if (ret)
  336. return ERR_PTR(ret);
  337. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  338. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  339. cache->vaddr = flushes | KMAP;
  340. cache->node.mm = (void *)obj;
  341. if (flushes)
  342. mb();
  343. }
  344. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
  345. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  346. cache->page = page;
  347. return vaddr;
  348. }
  349. static void *reloc_iomap(struct drm_i915_gem_object *obj,
  350. struct reloc_cache *cache,
  351. int page)
  352. {
  353. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  354. unsigned long offset;
  355. void *vaddr;
  356. if (cache->vaddr) {
  357. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  358. } else {
  359. struct i915_vma *vma;
  360. int ret;
  361. if (use_cpu_reloc(obj))
  362. return NULL;
  363. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  364. if (ret)
  365. return ERR_PTR(ret);
  366. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  367. PIN_MAPPABLE | PIN_NONBLOCK);
  368. if (IS_ERR(vma)) {
  369. memset(&cache->node, 0, sizeof(cache->node));
  370. ret = drm_mm_insert_node_in_range_generic
  371. (&ggtt->base.mm, &cache->node,
  372. 4096, 0, 0,
  373. 0, ggtt->mappable_end,
  374. DRM_MM_SEARCH_DEFAULT,
  375. DRM_MM_CREATE_DEFAULT);
  376. if (ret) /* no inactive aperture space, use cpu reloc */
  377. return NULL;
  378. } else {
  379. ret = i915_vma_put_fence(vma);
  380. if (ret) {
  381. i915_vma_unpin(vma);
  382. return ERR_PTR(ret);
  383. }
  384. cache->node.start = vma->node.start;
  385. cache->node.mm = (void *)vma;
  386. }
  387. }
  388. offset = cache->node.start;
  389. if (cache->node.allocated) {
  390. wmb();
  391. ggtt->base.insert_page(&ggtt->base,
  392. i915_gem_object_get_dma_address(obj, page),
  393. offset, I915_CACHE_NONE, 0);
  394. } else {
  395. offset += page << PAGE_SHIFT;
  396. }
  397. vaddr = (void __force *) io_mapping_map_atomic_wc(&cache->i915->ggtt.mappable, offset);
  398. cache->page = page;
  399. cache->vaddr = (unsigned long)vaddr;
  400. return vaddr;
  401. }
  402. static void *reloc_vaddr(struct drm_i915_gem_object *obj,
  403. struct reloc_cache *cache,
  404. int page)
  405. {
  406. void *vaddr;
  407. if (cache->page == page) {
  408. vaddr = unmask_page(cache->vaddr);
  409. } else {
  410. vaddr = NULL;
  411. if ((cache->vaddr & KMAP) == 0)
  412. vaddr = reloc_iomap(obj, cache, page);
  413. if (!vaddr)
  414. vaddr = reloc_kmap(obj, cache, page);
  415. }
  416. return vaddr;
  417. }
  418. static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  419. {
  420. if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
  421. if (flushes & CLFLUSH_BEFORE) {
  422. clflushopt(addr);
  423. mb();
  424. }
  425. *addr = value;
  426. /* Writes to the same cacheline are serialised by the CPU
  427. * (including clflush). On the write path, we only require
  428. * that it hits memory in an orderly fashion and place
  429. * mb barriers at the start and end of the relocation phase
  430. * to ensure ordering of clflush wrt to the system.
  431. */
  432. if (flushes & CLFLUSH_AFTER)
  433. clflushopt(addr);
  434. } else
  435. *addr = value;
  436. }
  437. static int
  438. relocate_entry(struct drm_i915_gem_object *obj,
  439. const struct drm_i915_gem_relocation_entry *reloc,
  440. struct reloc_cache *cache,
  441. u64 target_offset)
  442. {
  443. u64 offset = reloc->offset;
  444. bool wide = cache->use_64bit_reloc;
  445. void *vaddr;
  446. target_offset = relocation_target(reloc, target_offset);
  447. repeat:
  448. vaddr = reloc_vaddr(obj, cache, offset >> PAGE_SHIFT);
  449. if (IS_ERR(vaddr))
  450. return PTR_ERR(vaddr);
  451. clflush_write32(vaddr + offset_in_page(offset),
  452. lower_32_bits(target_offset),
  453. cache->vaddr);
  454. if (wide) {
  455. offset += sizeof(u32);
  456. target_offset >>= 32;
  457. wide = false;
  458. goto repeat;
  459. }
  460. return 0;
  461. }
  462. static int
  463. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  464. struct eb_vmas *eb,
  465. struct drm_i915_gem_relocation_entry *reloc,
  466. struct reloc_cache *cache)
  467. {
  468. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  469. struct drm_gem_object *target_obj;
  470. struct drm_i915_gem_object *target_i915_obj;
  471. struct i915_vma *target_vma;
  472. uint64_t target_offset;
  473. int ret;
  474. /* we've already hold a reference to all valid objects */
  475. target_vma = eb_get_vma(eb, reloc->target_handle);
  476. if (unlikely(target_vma == NULL))
  477. return -ENOENT;
  478. target_i915_obj = target_vma->obj;
  479. target_obj = &target_vma->obj->base;
  480. target_offset = gen8_canonical_addr(target_vma->node.start);
  481. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  482. * pipe_control writes because the gpu doesn't properly redirect them
  483. * through the ppgtt for non_secure batchbuffers. */
  484. if (unlikely(IS_GEN6(dev_priv) &&
  485. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
  486. ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
  487. PIN_GLOBAL);
  488. if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
  489. return ret;
  490. }
  491. /* Validate that the target is in a valid r/w GPU domain */
  492. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  493. DRM_DEBUG("reloc with multiple write domains: "
  494. "obj %p target %d offset %d "
  495. "read %08x write %08x",
  496. obj, reloc->target_handle,
  497. (int) reloc->offset,
  498. reloc->read_domains,
  499. reloc->write_domain);
  500. return -EINVAL;
  501. }
  502. if (unlikely((reloc->write_domain | reloc->read_domains)
  503. & ~I915_GEM_GPU_DOMAINS)) {
  504. DRM_DEBUG("reloc with read/write non-GPU domains: "
  505. "obj %p target %d offset %d "
  506. "read %08x write %08x",
  507. obj, reloc->target_handle,
  508. (int) reloc->offset,
  509. reloc->read_domains,
  510. reloc->write_domain);
  511. return -EINVAL;
  512. }
  513. target_obj->pending_read_domains |= reloc->read_domains;
  514. target_obj->pending_write_domain |= reloc->write_domain;
  515. /* If the relocation already has the right value in it, no
  516. * more work needs to be done.
  517. */
  518. if (target_offset == reloc->presumed_offset)
  519. return 0;
  520. /* Check that the relocation address is valid... */
  521. if (unlikely(reloc->offset >
  522. obj->base.size - (cache->use_64bit_reloc ? 8 : 4))) {
  523. DRM_DEBUG("Relocation beyond object bounds: "
  524. "obj %p target %d offset %d size %d.\n",
  525. obj, reloc->target_handle,
  526. (int) reloc->offset,
  527. (int) obj->base.size);
  528. return -EINVAL;
  529. }
  530. if (unlikely(reloc->offset & 3)) {
  531. DRM_DEBUG("Relocation not 4-byte aligned: "
  532. "obj %p target %d offset %d.\n",
  533. obj, reloc->target_handle,
  534. (int) reloc->offset);
  535. return -EINVAL;
  536. }
  537. ret = relocate_entry(obj, reloc, cache, target_offset);
  538. if (ret)
  539. return ret;
  540. /* and update the user's relocation entry */
  541. reloc->presumed_offset = target_offset;
  542. return 0;
  543. }
  544. static int
  545. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  546. struct eb_vmas *eb)
  547. {
  548. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  549. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  550. struct drm_i915_gem_relocation_entry __user *user_relocs;
  551. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  552. struct reloc_cache cache;
  553. int remain, ret = 0;
  554. user_relocs = u64_to_user_ptr(entry->relocs_ptr);
  555. reloc_cache_init(&cache, eb->i915);
  556. remain = entry->relocation_count;
  557. while (remain) {
  558. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  559. unsigned long unwritten;
  560. unsigned int count;
  561. count = min_t(unsigned int, remain, ARRAY_SIZE(stack_reloc));
  562. remain -= count;
  563. /* This is the fast path and we cannot handle a pagefault
  564. * whilst holding the struct mutex lest the user pass in the
  565. * relocations contained within a mmaped bo. For in such a case
  566. * we, the page fault handler would call i915_gem_fault() and
  567. * we would try to acquire the struct mutex again. Obviously
  568. * this is bad and so lockdep complains vehemently.
  569. */
  570. pagefault_disable();
  571. unwritten = __copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]));
  572. pagefault_enable();
  573. if (unlikely(unwritten)) {
  574. ret = -EFAULT;
  575. goto out;
  576. }
  577. do {
  578. u64 offset = r->presumed_offset;
  579. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r, &cache);
  580. if (ret)
  581. goto out;
  582. if (r->presumed_offset != offset) {
  583. pagefault_disable();
  584. unwritten = __put_user(r->presumed_offset,
  585. &user_relocs->presumed_offset);
  586. pagefault_enable();
  587. if (unlikely(unwritten)) {
  588. /* Note that reporting an error now
  589. * leaves everything in an inconsistent
  590. * state as we have *already* changed
  591. * the relocation value inside the
  592. * object. As we have not changed the
  593. * reloc.presumed_offset or will not
  594. * change the execobject.offset, on the
  595. * call we may not rewrite the value
  596. * inside the object, leaving it
  597. * dangling and causing a GPU hang.
  598. */
  599. ret = -EFAULT;
  600. goto out;
  601. }
  602. }
  603. user_relocs++;
  604. r++;
  605. } while (--count);
  606. }
  607. out:
  608. reloc_cache_fini(&cache);
  609. return ret;
  610. #undef N_RELOC
  611. }
  612. static int
  613. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  614. struct eb_vmas *eb,
  615. struct drm_i915_gem_relocation_entry *relocs)
  616. {
  617. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  618. struct reloc_cache cache;
  619. int i, ret = 0;
  620. reloc_cache_init(&cache, eb->i915);
  621. for (i = 0; i < entry->relocation_count; i++) {
  622. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i], &cache);
  623. if (ret)
  624. break;
  625. }
  626. reloc_cache_fini(&cache);
  627. return ret;
  628. }
  629. static int
  630. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  631. {
  632. struct i915_vma *vma;
  633. int ret = 0;
  634. list_for_each_entry(vma, &eb->vmas, exec_list) {
  635. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  636. if (ret)
  637. break;
  638. }
  639. return ret;
  640. }
  641. static bool only_mappable_for_reloc(unsigned int flags)
  642. {
  643. return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
  644. __EXEC_OBJECT_NEEDS_MAP;
  645. }
  646. static int
  647. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  648. struct intel_engine_cs *engine,
  649. bool *need_reloc)
  650. {
  651. struct drm_i915_gem_object *obj = vma->obj;
  652. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  653. uint64_t flags;
  654. int ret;
  655. flags = PIN_USER;
  656. if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  657. flags |= PIN_GLOBAL;
  658. if (!drm_mm_node_allocated(&vma->node)) {
  659. /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  660. * limit address to the first 4GBs for unflagged objects.
  661. */
  662. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
  663. flags |= PIN_ZONE_4G;
  664. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  665. flags |= PIN_GLOBAL | PIN_MAPPABLE;
  666. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  667. flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  668. if (entry->flags & EXEC_OBJECT_PINNED)
  669. flags |= entry->offset | PIN_OFFSET_FIXED;
  670. if ((flags & PIN_MAPPABLE) == 0)
  671. flags |= PIN_HIGH;
  672. }
  673. ret = i915_vma_pin(vma,
  674. entry->pad_to_size,
  675. entry->alignment,
  676. flags);
  677. if ((ret == -ENOSPC || ret == -E2BIG) &&
  678. only_mappable_for_reloc(entry->flags))
  679. ret = i915_vma_pin(vma,
  680. entry->pad_to_size,
  681. entry->alignment,
  682. flags & ~PIN_MAPPABLE);
  683. if (ret)
  684. return ret;
  685. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  686. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  687. ret = i915_vma_get_fence(vma);
  688. if (ret)
  689. return ret;
  690. if (i915_vma_pin_fence(vma))
  691. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  692. }
  693. if (entry->offset != vma->node.start) {
  694. entry->offset = vma->node.start;
  695. *need_reloc = true;
  696. }
  697. if (entry->flags & EXEC_OBJECT_WRITE) {
  698. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  699. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  700. }
  701. return 0;
  702. }
  703. static bool
  704. need_reloc_mappable(struct i915_vma *vma)
  705. {
  706. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  707. if (entry->relocation_count == 0)
  708. return false;
  709. if (!i915_vma_is_ggtt(vma))
  710. return false;
  711. /* See also use_cpu_reloc() */
  712. if (HAS_LLC(to_i915(vma->obj->base.dev)))
  713. return false;
  714. if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  715. return false;
  716. return true;
  717. }
  718. static bool
  719. eb_vma_misplaced(struct i915_vma *vma)
  720. {
  721. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  722. WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  723. !i915_vma_is_ggtt(vma));
  724. if (entry->alignment &&
  725. vma->node.start & (entry->alignment - 1))
  726. return true;
  727. if (vma->node.size < entry->pad_to_size)
  728. return true;
  729. if (entry->flags & EXEC_OBJECT_PINNED &&
  730. vma->node.start != entry->offset)
  731. return true;
  732. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  733. vma->node.start < BATCH_OFFSET_BIAS)
  734. return true;
  735. /* avoid costly ping-pong once a batch bo ended up non-mappable */
  736. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  737. !i915_vma_is_map_and_fenceable(vma))
  738. return !only_mappable_for_reloc(entry->flags);
  739. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  740. (vma->node.start + vma->node.size - 1) >> 32)
  741. return true;
  742. return false;
  743. }
  744. static int
  745. i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
  746. struct list_head *vmas,
  747. struct i915_gem_context *ctx,
  748. bool *need_relocs)
  749. {
  750. struct drm_i915_gem_object *obj;
  751. struct i915_vma *vma;
  752. struct i915_address_space *vm;
  753. struct list_head ordered_vmas;
  754. struct list_head pinned_vmas;
  755. bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
  756. int retry;
  757. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  758. INIT_LIST_HEAD(&ordered_vmas);
  759. INIT_LIST_HEAD(&pinned_vmas);
  760. while (!list_empty(vmas)) {
  761. struct drm_i915_gem_exec_object2 *entry;
  762. bool need_fence, need_mappable;
  763. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  764. obj = vma->obj;
  765. entry = vma->exec_entry;
  766. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  767. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  768. if (!has_fenced_gpu_access)
  769. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  770. need_fence =
  771. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  772. i915_gem_object_is_tiled(obj);
  773. need_mappable = need_fence || need_reloc_mappable(vma);
  774. if (entry->flags & EXEC_OBJECT_PINNED)
  775. list_move_tail(&vma->exec_list, &pinned_vmas);
  776. else if (need_mappable) {
  777. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  778. list_move(&vma->exec_list, &ordered_vmas);
  779. } else
  780. list_move_tail(&vma->exec_list, &ordered_vmas);
  781. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  782. obj->base.pending_write_domain = 0;
  783. }
  784. list_splice(&ordered_vmas, vmas);
  785. list_splice(&pinned_vmas, vmas);
  786. /* Attempt to pin all of the buffers into the GTT.
  787. * This is done in 3 phases:
  788. *
  789. * 1a. Unbind all objects that do not match the GTT constraints for
  790. * the execbuffer (fenceable, mappable, alignment etc).
  791. * 1b. Increment pin count for already bound objects.
  792. * 2. Bind new objects.
  793. * 3. Decrement pin count.
  794. *
  795. * This avoid unnecessary unbinding of later objects in order to make
  796. * room for the earlier objects *unless* we need to defragment.
  797. */
  798. retry = 0;
  799. do {
  800. int ret = 0;
  801. /* Unbind any ill-fitting objects or pin. */
  802. list_for_each_entry(vma, vmas, exec_list) {
  803. if (!drm_mm_node_allocated(&vma->node))
  804. continue;
  805. if (eb_vma_misplaced(vma))
  806. ret = i915_vma_unbind(vma);
  807. else
  808. ret = i915_gem_execbuffer_reserve_vma(vma,
  809. engine,
  810. need_relocs);
  811. if (ret)
  812. goto err;
  813. }
  814. /* Bind fresh objects */
  815. list_for_each_entry(vma, vmas, exec_list) {
  816. if (drm_mm_node_allocated(&vma->node))
  817. continue;
  818. ret = i915_gem_execbuffer_reserve_vma(vma, engine,
  819. need_relocs);
  820. if (ret)
  821. goto err;
  822. }
  823. err:
  824. if (ret != -ENOSPC || retry++)
  825. return ret;
  826. /* Decrement pin count for bound objects */
  827. list_for_each_entry(vma, vmas, exec_list)
  828. i915_gem_execbuffer_unreserve_vma(vma);
  829. ret = i915_gem_evict_vm(vm, true);
  830. if (ret)
  831. return ret;
  832. } while (1);
  833. }
  834. static int
  835. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  836. struct drm_i915_gem_execbuffer2 *args,
  837. struct drm_file *file,
  838. struct intel_engine_cs *engine,
  839. struct eb_vmas *eb,
  840. struct drm_i915_gem_exec_object2 *exec,
  841. struct i915_gem_context *ctx)
  842. {
  843. struct drm_i915_gem_relocation_entry *reloc;
  844. struct i915_address_space *vm;
  845. struct i915_vma *vma;
  846. bool need_relocs;
  847. int *reloc_offset;
  848. int i, total, ret;
  849. unsigned count = args->buffer_count;
  850. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  851. /* We may process another execbuffer during the unlock... */
  852. while (!list_empty(&eb->vmas)) {
  853. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  854. list_del_init(&vma->exec_list);
  855. i915_gem_execbuffer_unreserve_vma(vma);
  856. i915_vma_put(vma);
  857. }
  858. mutex_unlock(&dev->struct_mutex);
  859. total = 0;
  860. for (i = 0; i < count; i++)
  861. total += exec[i].relocation_count;
  862. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  863. reloc = drm_malloc_ab(total, sizeof(*reloc));
  864. if (reloc == NULL || reloc_offset == NULL) {
  865. drm_free_large(reloc);
  866. drm_free_large(reloc_offset);
  867. mutex_lock(&dev->struct_mutex);
  868. return -ENOMEM;
  869. }
  870. total = 0;
  871. for (i = 0; i < count; i++) {
  872. struct drm_i915_gem_relocation_entry __user *user_relocs;
  873. u64 invalid_offset = (u64)-1;
  874. int j;
  875. user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
  876. if (copy_from_user(reloc+total, user_relocs,
  877. exec[i].relocation_count * sizeof(*reloc))) {
  878. ret = -EFAULT;
  879. mutex_lock(&dev->struct_mutex);
  880. goto err;
  881. }
  882. /* As we do not update the known relocation offsets after
  883. * relocating (due to the complexities in lock handling),
  884. * we need to mark them as invalid now so that we force the
  885. * relocation processing next time. Just in case the target
  886. * object is evicted and then rebound into its old
  887. * presumed_offset before the next execbuffer - if that
  888. * happened we would make the mistake of assuming that the
  889. * relocations were valid.
  890. */
  891. for (j = 0; j < exec[i].relocation_count; j++) {
  892. if (__copy_to_user(&user_relocs[j].presumed_offset,
  893. &invalid_offset,
  894. sizeof(invalid_offset))) {
  895. ret = -EFAULT;
  896. mutex_lock(&dev->struct_mutex);
  897. goto err;
  898. }
  899. }
  900. reloc_offset[i] = total;
  901. total += exec[i].relocation_count;
  902. }
  903. ret = i915_mutex_lock_interruptible(dev);
  904. if (ret) {
  905. mutex_lock(&dev->struct_mutex);
  906. goto err;
  907. }
  908. /* reacquire the objects */
  909. eb_reset(eb);
  910. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  911. if (ret)
  912. goto err;
  913. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  914. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  915. &need_relocs);
  916. if (ret)
  917. goto err;
  918. list_for_each_entry(vma, &eb->vmas, exec_list) {
  919. int offset = vma->exec_entry - exec;
  920. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  921. reloc + reloc_offset[offset]);
  922. if (ret)
  923. goto err;
  924. }
  925. /* Leave the user relocations as are, this is the painfully slow path,
  926. * and we want to avoid the complication of dropping the lock whilst
  927. * having buffers reserved in the aperture and so causing spurious
  928. * ENOSPC for random operations.
  929. */
  930. err:
  931. drm_free_large(reloc);
  932. drm_free_large(reloc_offset);
  933. return ret;
  934. }
  935. static int
  936. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  937. struct list_head *vmas)
  938. {
  939. struct i915_vma *vma;
  940. int ret;
  941. list_for_each_entry(vma, vmas, exec_list) {
  942. struct drm_i915_gem_object *obj = vma->obj;
  943. ret = i915_gem_request_await_object
  944. (req, obj, obj->base.pending_write_domain);
  945. if (ret)
  946. return ret;
  947. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  948. i915_gem_clflush_object(obj, false);
  949. }
  950. /* Unconditionally flush any chipset caches (for streaming writes). */
  951. i915_gem_chipset_flush(req->engine->i915);
  952. /* Unconditionally invalidate GPU caches and TLBs. */
  953. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  954. }
  955. static bool
  956. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  957. {
  958. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  959. return false;
  960. /* Kernel clipping was a DRI1 misfeature */
  961. if (exec->num_cliprects || exec->cliprects_ptr)
  962. return false;
  963. if (exec->DR4 == 0xffffffff) {
  964. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  965. exec->DR4 = 0;
  966. }
  967. if (exec->DR1 || exec->DR4)
  968. return false;
  969. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  970. return false;
  971. return true;
  972. }
  973. static int
  974. validate_exec_list(struct drm_device *dev,
  975. struct drm_i915_gem_exec_object2 *exec,
  976. int count)
  977. {
  978. unsigned relocs_total = 0;
  979. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  980. unsigned invalid_flags;
  981. int i;
  982. /* INTERNAL flags must not overlap with external ones */
  983. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  984. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  985. if (USES_FULL_PPGTT(dev))
  986. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  987. for (i = 0; i < count; i++) {
  988. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  989. int length; /* limited by fault_in_pages_readable() */
  990. if (exec[i].flags & invalid_flags)
  991. return -EINVAL;
  992. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  993. * any non-page-aligned or non-canonical addresses.
  994. */
  995. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  996. if (exec[i].offset !=
  997. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  998. return -EINVAL;
  999. }
  1000. /* From drm_mm perspective address space is continuous,
  1001. * so from this point we're always using non-canonical
  1002. * form internally.
  1003. */
  1004. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  1005. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  1006. return -EINVAL;
  1007. /* pad_to_size was once a reserved field, so sanitize it */
  1008. if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
  1009. if (offset_in_page(exec[i].pad_to_size))
  1010. return -EINVAL;
  1011. } else {
  1012. exec[i].pad_to_size = 0;
  1013. }
  1014. /* First check for malicious input causing overflow in
  1015. * the worst case where we need to allocate the entire
  1016. * relocation tree as a single array.
  1017. */
  1018. if (exec[i].relocation_count > relocs_max - relocs_total)
  1019. return -EINVAL;
  1020. relocs_total += exec[i].relocation_count;
  1021. length = exec[i].relocation_count *
  1022. sizeof(struct drm_i915_gem_relocation_entry);
  1023. /*
  1024. * We must check that the entire relocation array is safe
  1025. * to read, but since we may need to update the presumed
  1026. * offsets during execution, check for full write access.
  1027. */
  1028. if (!access_ok(VERIFY_WRITE, ptr, length))
  1029. return -EFAULT;
  1030. if (likely(!i915.prefault_disable)) {
  1031. if (fault_in_pages_readable(ptr, length))
  1032. return -EFAULT;
  1033. }
  1034. }
  1035. return 0;
  1036. }
  1037. static struct i915_gem_context *
  1038. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  1039. struct intel_engine_cs *engine, const u32 ctx_id)
  1040. {
  1041. struct i915_gem_context *ctx;
  1042. struct i915_ctx_hang_stats *hs;
  1043. ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
  1044. if (IS_ERR(ctx))
  1045. return ctx;
  1046. hs = &ctx->hang_stats;
  1047. if (hs->banned) {
  1048. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  1049. return ERR_PTR(-EIO);
  1050. }
  1051. return ctx;
  1052. }
  1053. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  1054. {
  1055. return !(obj->cache_level == I915_CACHE_NONE ||
  1056. obj->cache_level == I915_CACHE_WT);
  1057. }
  1058. void i915_vma_move_to_active(struct i915_vma *vma,
  1059. struct drm_i915_gem_request *req,
  1060. unsigned int flags)
  1061. {
  1062. struct drm_i915_gem_object *obj = vma->obj;
  1063. const unsigned int idx = req->engine->id;
  1064. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1065. /* Add a reference if we're newly entering the active list.
  1066. * The order in which we add operations to the retirement queue is
  1067. * vital here: mark_active adds to the start of the callback list,
  1068. * such that subsequent callbacks are called first. Therefore we
  1069. * add the active reference first and queue for it to be dropped
  1070. * *last*.
  1071. */
  1072. if (!i915_vma_is_active(vma))
  1073. obj->active_count++;
  1074. i915_vma_set_active(vma, idx);
  1075. i915_gem_active_set(&vma->last_read[idx], req);
  1076. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1077. if (flags & EXEC_OBJECT_WRITE) {
  1078. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1079. i915_gem_active_set(&obj->frontbuffer_write, req);
  1080. /* update for the implicit flush after a batch */
  1081. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  1082. if (!obj->cache_dirty && gpu_write_needs_clflush(obj))
  1083. obj->cache_dirty = true;
  1084. }
  1085. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1086. i915_gem_active_set(&vma->last_fence, req);
  1087. }
  1088. static void eb_export_fence(struct drm_i915_gem_object *obj,
  1089. struct drm_i915_gem_request *req,
  1090. unsigned int flags)
  1091. {
  1092. struct reservation_object *resv = obj->resv;
  1093. /* Ignore errors from failing to allocate the new fence, we can't
  1094. * handle an error right now. Worst case should be missed
  1095. * synchronisation leading to rendering corruption.
  1096. */
  1097. ww_mutex_lock(&resv->lock, NULL);
  1098. if (flags & EXEC_OBJECT_WRITE)
  1099. reservation_object_add_excl_fence(resv, &req->fence);
  1100. else if (reservation_object_reserve_shared(resv) == 0)
  1101. reservation_object_add_shared_fence(resv, &req->fence);
  1102. ww_mutex_unlock(&resv->lock);
  1103. }
  1104. static void
  1105. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  1106. struct drm_i915_gem_request *req)
  1107. {
  1108. struct i915_vma *vma;
  1109. list_for_each_entry(vma, vmas, exec_list) {
  1110. struct drm_i915_gem_object *obj = vma->obj;
  1111. u32 old_read = obj->base.read_domains;
  1112. u32 old_write = obj->base.write_domain;
  1113. obj->base.write_domain = obj->base.pending_write_domain;
  1114. if (obj->base.write_domain)
  1115. vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
  1116. else
  1117. obj->base.pending_read_domains |= obj->base.read_domains;
  1118. obj->base.read_domains = obj->base.pending_read_domains;
  1119. i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
  1120. eb_export_fence(obj, req, vma->exec_entry->flags);
  1121. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  1122. }
  1123. }
  1124. static int
  1125. i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1126. {
  1127. struct intel_ring *ring = req->ring;
  1128. int ret, i;
  1129. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1130. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1131. return -EINVAL;
  1132. }
  1133. ret = intel_ring_begin(req, 4 * 3);
  1134. if (ret)
  1135. return ret;
  1136. for (i = 0; i < 4; i++) {
  1137. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1138. intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
  1139. intel_ring_emit(ring, 0);
  1140. }
  1141. intel_ring_advance(ring);
  1142. return 0;
  1143. }
  1144. static struct i915_vma *
  1145. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  1146. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1147. struct drm_i915_gem_object *batch_obj,
  1148. struct eb_vmas *eb,
  1149. u32 batch_start_offset,
  1150. u32 batch_len,
  1151. bool is_master)
  1152. {
  1153. struct drm_i915_gem_object *shadow_batch_obj;
  1154. struct i915_vma *vma;
  1155. int ret;
  1156. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  1157. PAGE_ALIGN(batch_len));
  1158. if (IS_ERR(shadow_batch_obj))
  1159. return ERR_CAST(shadow_batch_obj);
  1160. ret = intel_engine_cmd_parser(engine,
  1161. batch_obj,
  1162. shadow_batch_obj,
  1163. batch_start_offset,
  1164. batch_len,
  1165. is_master);
  1166. if (ret) {
  1167. if (ret == -EACCES) /* unhandled chained batch */
  1168. vma = NULL;
  1169. else
  1170. vma = ERR_PTR(ret);
  1171. goto out;
  1172. }
  1173. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1174. if (IS_ERR(vma))
  1175. goto out;
  1176. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1177. vma->exec_entry = shadow_exec_entry;
  1178. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1179. i915_gem_object_get(shadow_batch_obj);
  1180. list_add_tail(&vma->exec_list, &eb->vmas);
  1181. out:
  1182. i915_gem_object_unpin_pages(shadow_batch_obj);
  1183. return vma;
  1184. }
  1185. static int
  1186. execbuf_submit(struct i915_execbuffer_params *params,
  1187. struct drm_i915_gem_execbuffer2 *args,
  1188. struct list_head *vmas)
  1189. {
  1190. struct drm_i915_private *dev_priv = params->request->i915;
  1191. u64 exec_start, exec_len;
  1192. int instp_mode;
  1193. u32 instp_mask;
  1194. int ret;
  1195. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1196. if (ret)
  1197. return ret;
  1198. ret = i915_switch_context(params->request);
  1199. if (ret)
  1200. return ret;
  1201. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1202. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1203. switch (instp_mode) {
  1204. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1205. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1206. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1207. if (instp_mode != 0 && params->engine->id != RCS) {
  1208. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1209. return -EINVAL;
  1210. }
  1211. if (instp_mode != dev_priv->relative_constants_mode) {
  1212. if (INTEL_INFO(dev_priv)->gen < 4) {
  1213. DRM_DEBUG("no rel constants on pre-gen4\n");
  1214. return -EINVAL;
  1215. }
  1216. if (INTEL_INFO(dev_priv)->gen > 5 &&
  1217. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1218. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1219. return -EINVAL;
  1220. }
  1221. /* The HW changed the meaning on this bit on gen6 */
  1222. if (INTEL_INFO(dev_priv)->gen >= 6)
  1223. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1224. }
  1225. break;
  1226. default:
  1227. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1228. return -EINVAL;
  1229. }
  1230. if (params->engine->id == RCS &&
  1231. instp_mode != dev_priv->relative_constants_mode) {
  1232. struct intel_ring *ring = params->request->ring;
  1233. ret = intel_ring_begin(params->request, 4);
  1234. if (ret)
  1235. return ret;
  1236. intel_ring_emit(ring, MI_NOOP);
  1237. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1238. intel_ring_emit_reg(ring, INSTPM);
  1239. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1240. intel_ring_advance(ring);
  1241. dev_priv->relative_constants_mode = instp_mode;
  1242. }
  1243. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1244. ret = i915_reset_gen7_sol_offsets(params->request);
  1245. if (ret)
  1246. return ret;
  1247. }
  1248. exec_len = args->batch_len;
  1249. exec_start = params->batch->node.start +
  1250. params->args_batch_start_offset;
  1251. if (exec_len == 0)
  1252. exec_len = params->batch->size - params->args_batch_start_offset;
  1253. ret = params->engine->emit_bb_start(params->request,
  1254. exec_start, exec_len,
  1255. params->dispatch_flags);
  1256. if (ret)
  1257. return ret;
  1258. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1259. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1260. return 0;
  1261. }
  1262. /**
  1263. * Find one BSD ring to dispatch the corresponding BSD command.
  1264. * The engine index is returned.
  1265. */
  1266. static unsigned int
  1267. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1268. struct drm_file *file)
  1269. {
  1270. struct drm_i915_file_private *file_priv = file->driver_priv;
  1271. /* Check whether the file_priv has already selected one ring. */
  1272. if ((int)file_priv->bsd_engine < 0)
  1273. file_priv->bsd_engine = atomic_fetch_xor(1,
  1274. &dev_priv->mm.bsd_engine_dispatch_index);
  1275. return file_priv->bsd_engine;
  1276. }
  1277. #define I915_USER_RINGS (4)
  1278. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1279. [I915_EXEC_DEFAULT] = RCS,
  1280. [I915_EXEC_RENDER] = RCS,
  1281. [I915_EXEC_BLT] = BCS,
  1282. [I915_EXEC_BSD] = VCS,
  1283. [I915_EXEC_VEBOX] = VECS
  1284. };
  1285. static struct intel_engine_cs *
  1286. eb_select_engine(struct drm_i915_private *dev_priv,
  1287. struct drm_file *file,
  1288. struct drm_i915_gem_execbuffer2 *args)
  1289. {
  1290. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1291. struct intel_engine_cs *engine;
  1292. if (user_ring_id > I915_USER_RINGS) {
  1293. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1294. return NULL;
  1295. }
  1296. if ((user_ring_id != I915_EXEC_BSD) &&
  1297. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1298. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1299. "bsd dispatch flags: %d\n", (int)(args->flags));
  1300. return NULL;
  1301. }
  1302. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1303. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1304. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1305. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1306. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1307. bsd_idx <= I915_EXEC_BSD_RING2) {
  1308. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1309. bsd_idx--;
  1310. } else {
  1311. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1312. bsd_idx);
  1313. return NULL;
  1314. }
  1315. engine = dev_priv->engine[_VCS(bsd_idx)];
  1316. } else {
  1317. engine = dev_priv->engine[user_ring_map[user_ring_id]];
  1318. }
  1319. if (!engine) {
  1320. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1321. return NULL;
  1322. }
  1323. return engine;
  1324. }
  1325. static int
  1326. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1327. struct drm_file *file,
  1328. struct drm_i915_gem_execbuffer2 *args,
  1329. struct drm_i915_gem_exec_object2 *exec)
  1330. {
  1331. struct drm_i915_private *dev_priv = to_i915(dev);
  1332. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1333. struct eb_vmas *eb;
  1334. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1335. struct intel_engine_cs *engine;
  1336. struct i915_gem_context *ctx;
  1337. struct i915_address_space *vm;
  1338. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1339. struct i915_execbuffer_params *params = &params_master;
  1340. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1341. u32 dispatch_flags;
  1342. int ret;
  1343. bool need_relocs;
  1344. if (!i915_gem_check_execbuffer(args))
  1345. return -EINVAL;
  1346. ret = validate_exec_list(dev, exec, args->buffer_count);
  1347. if (ret)
  1348. return ret;
  1349. dispatch_flags = 0;
  1350. if (args->flags & I915_EXEC_SECURE) {
  1351. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1352. return -EPERM;
  1353. dispatch_flags |= I915_DISPATCH_SECURE;
  1354. }
  1355. if (args->flags & I915_EXEC_IS_PINNED)
  1356. dispatch_flags |= I915_DISPATCH_PINNED;
  1357. engine = eb_select_engine(dev_priv, file, args);
  1358. if (!engine)
  1359. return -EINVAL;
  1360. if (args->buffer_count < 1) {
  1361. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1362. return -EINVAL;
  1363. }
  1364. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1365. if (!HAS_RESOURCE_STREAMER(dev_priv)) {
  1366. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1367. return -EINVAL;
  1368. }
  1369. if (engine->id != RCS) {
  1370. DRM_DEBUG("RS is not available on %s\n",
  1371. engine->name);
  1372. return -EINVAL;
  1373. }
  1374. dispatch_flags |= I915_DISPATCH_RS;
  1375. }
  1376. /* Take a local wakeref for preparing to dispatch the execbuf as
  1377. * we expect to access the hardware fairly frequently in the
  1378. * process. Upon first dispatch, we acquire another prolonged
  1379. * wakeref that we hold until the GPU has been idle for at least
  1380. * 100ms.
  1381. */
  1382. intel_runtime_pm_get(dev_priv);
  1383. ret = i915_mutex_lock_interruptible(dev);
  1384. if (ret)
  1385. goto pre_mutex_err;
  1386. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1387. if (IS_ERR(ctx)) {
  1388. mutex_unlock(&dev->struct_mutex);
  1389. ret = PTR_ERR(ctx);
  1390. goto pre_mutex_err;
  1391. }
  1392. i915_gem_context_get(ctx);
  1393. if (ctx->ppgtt)
  1394. vm = &ctx->ppgtt->base;
  1395. else
  1396. vm = &ggtt->base;
  1397. memset(&params_master, 0x00, sizeof(params_master));
  1398. eb = eb_create(dev_priv, args);
  1399. if (eb == NULL) {
  1400. i915_gem_context_put(ctx);
  1401. mutex_unlock(&dev->struct_mutex);
  1402. ret = -ENOMEM;
  1403. goto pre_mutex_err;
  1404. }
  1405. /* Look up object handles */
  1406. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1407. if (ret)
  1408. goto err;
  1409. /* take note of the batch buffer before we might reorder the lists */
  1410. params->batch = eb_get_batch(eb);
  1411. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1412. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1413. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1414. &need_relocs);
  1415. if (ret)
  1416. goto err;
  1417. /* The objects are in their final locations, apply the relocations. */
  1418. if (need_relocs)
  1419. ret = i915_gem_execbuffer_relocate(eb);
  1420. if (ret) {
  1421. if (ret == -EFAULT) {
  1422. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1423. engine,
  1424. eb, exec, ctx);
  1425. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1426. }
  1427. if (ret)
  1428. goto err;
  1429. }
  1430. /* Set the pending read domains for the batch buffer to COMMAND */
  1431. if (params->batch->obj->base.pending_write_domain) {
  1432. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1433. ret = -EINVAL;
  1434. goto err;
  1435. }
  1436. if (args->batch_start_offset > params->batch->size ||
  1437. args->batch_len > params->batch->size - args->batch_start_offset) {
  1438. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1439. ret = -EINVAL;
  1440. goto err;
  1441. }
  1442. params->args_batch_start_offset = args->batch_start_offset;
  1443. if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
  1444. struct i915_vma *vma;
  1445. vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
  1446. params->batch->obj,
  1447. eb,
  1448. args->batch_start_offset,
  1449. args->batch_len,
  1450. drm_is_current_master(file));
  1451. if (IS_ERR(vma)) {
  1452. ret = PTR_ERR(vma);
  1453. goto err;
  1454. }
  1455. if (vma) {
  1456. /*
  1457. * Batch parsed and accepted:
  1458. *
  1459. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1460. * bit from MI_BATCH_BUFFER_START commands issued in
  1461. * the dispatch_execbuffer implementations. We
  1462. * specifically don't want that set on batches the
  1463. * command parser has accepted.
  1464. */
  1465. dispatch_flags |= I915_DISPATCH_SECURE;
  1466. params->args_batch_start_offset = 0;
  1467. params->batch = vma;
  1468. }
  1469. }
  1470. params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1471. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1472. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1473. * hsw should have this fixed, but bdw mucks it up again. */
  1474. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1475. struct drm_i915_gem_object *obj = params->batch->obj;
  1476. struct i915_vma *vma;
  1477. /*
  1478. * So on first glance it looks freaky that we pin the batch here
  1479. * outside of the reservation loop. But:
  1480. * - The batch is already pinned into the relevant ppgtt, so we
  1481. * already have the backing storage fully allocated.
  1482. * - No other BO uses the global gtt (well contexts, but meh),
  1483. * so we don't really have issues with multiple objects not
  1484. * fitting due to fragmentation.
  1485. * So this is actually safe.
  1486. */
  1487. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  1488. if (IS_ERR(vma)) {
  1489. ret = PTR_ERR(vma);
  1490. goto err;
  1491. }
  1492. params->batch = vma;
  1493. }
  1494. /* Allocate a request for this batch buffer nice and early. */
  1495. params->request = i915_gem_request_alloc(engine, ctx);
  1496. if (IS_ERR(params->request)) {
  1497. ret = PTR_ERR(params->request);
  1498. goto err_batch_unpin;
  1499. }
  1500. /* Whilst this request exists, batch_obj will be on the
  1501. * active_list, and so will hold the active reference. Only when this
  1502. * request is retired will the the batch_obj be moved onto the
  1503. * inactive_list and lose its active reference. Hence we do not need
  1504. * to explicitly hold another reference here.
  1505. */
  1506. params->request->batch = params->batch;
  1507. ret = i915_gem_request_add_to_client(params->request, file);
  1508. if (ret)
  1509. goto err_request;
  1510. /*
  1511. * Save assorted stuff away to pass through to *_submission().
  1512. * NB: This data should be 'persistent' and not local as it will
  1513. * kept around beyond the duration of the IOCTL once the GPU
  1514. * scheduler arrives.
  1515. */
  1516. params->dev = dev;
  1517. params->file = file;
  1518. params->engine = engine;
  1519. params->dispatch_flags = dispatch_flags;
  1520. params->ctx = ctx;
  1521. ret = execbuf_submit(params, args, &eb->vmas);
  1522. err_request:
  1523. __i915_add_request(params->request, ret == 0);
  1524. err_batch_unpin:
  1525. /*
  1526. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1527. * batch vma for correctness. For less ugly and less fragility this
  1528. * needs to be adjusted to also track the ggtt batch vma properly as
  1529. * active.
  1530. */
  1531. if (dispatch_flags & I915_DISPATCH_SECURE)
  1532. i915_vma_unpin(params->batch);
  1533. err:
  1534. /* the request owns the ref now */
  1535. i915_gem_context_put(ctx);
  1536. eb_destroy(eb);
  1537. mutex_unlock(&dev->struct_mutex);
  1538. pre_mutex_err:
  1539. /* intel_gpu_busy should also get a ref, so it will free when the device
  1540. * is really idle. */
  1541. intel_runtime_pm_put(dev_priv);
  1542. return ret;
  1543. }
  1544. /*
  1545. * Legacy execbuffer just creates an exec2 list from the original exec object
  1546. * list array and passes it to the real function.
  1547. */
  1548. int
  1549. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1550. struct drm_file *file)
  1551. {
  1552. struct drm_i915_gem_execbuffer *args = data;
  1553. struct drm_i915_gem_execbuffer2 exec2;
  1554. struct drm_i915_gem_exec_object *exec_list = NULL;
  1555. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1556. int ret, i;
  1557. if (args->buffer_count < 1) {
  1558. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1559. return -EINVAL;
  1560. }
  1561. /* Copy in the exec list from userland */
  1562. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1563. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1564. if (exec_list == NULL || exec2_list == NULL) {
  1565. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1566. args->buffer_count);
  1567. drm_free_large(exec_list);
  1568. drm_free_large(exec2_list);
  1569. return -ENOMEM;
  1570. }
  1571. ret = copy_from_user(exec_list,
  1572. u64_to_user_ptr(args->buffers_ptr),
  1573. sizeof(*exec_list) * args->buffer_count);
  1574. if (ret != 0) {
  1575. DRM_DEBUG("copy %d exec entries failed %d\n",
  1576. args->buffer_count, ret);
  1577. drm_free_large(exec_list);
  1578. drm_free_large(exec2_list);
  1579. return -EFAULT;
  1580. }
  1581. for (i = 0; i < args->buffer_count; i++) {
  1582. exec2_list[i].handle = exec_list[i].handle;
  1583. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1584. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1585. exec2_list[i].alignment = exec_list[i].alignment;
  1586. exec2_list[i].offset = exec_list[i].offset;
  1587. if (INTEL_GEN(to_i915(dev)) < 4)
  1588. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1589. else
  1590. exec2_list[i].flags = 0;
  1591. }
  1592. exec2.buffers_ptr = args->buffers_ptr;
  1593. exec2.buffer_count = args->buffer_count;
  1594. exec2.batch_start_offset = args->batch_start_offset;
  1595. exec2.batch_len = args->batch_len;
  1596. exec2.DR1 = args->DR1;
  1597. exec2.DR4 = args->DR4;
  1598. exec2.num_cliprects = args->num_cliprects;
  1599. exec2.cliprects_ptr = args->cliprects_ptr;
  1600. exec2.flags = I915_EXEC_RENDER;
  1601. i915_execbuffer2_set_context_id(exec2, 0);
  1602. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1603. if (!ret) {
  1604. struct drm_i915_gem_exec_object __user *user_exec_list =
  1605. u64_to_user_ptr(args->buffers_ptr);
  1606. /* Copy the new buffer offsets back to the user's exec list. */
  1607. for (i = 0; i < args->buffer_count; i++) {
  1608. exec2_list[i].offset =
  1609. gen8_canonical_addr(exec2_list[i].offset);
  1610. ret = __copy_to_user(&user_exec_list[i].offset,
  1611. &exec2_list[i].offset,
  1612. sizeof(user_exec_list[i].offset));
  1613. if (ret) {
  1614. ret = -EFAULT;
  1615. DRM_DEBUG("failed to copy %d exec entries "
  1616. "back to user (%d)\n",
  1617. args->buffer_count, ret);
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. drm_free_large(exec_list);
  1623. drm_free_large(exec2_list);
  1624. return ret;
  1625. }
  1626. int
  1627. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1628. struct drm_file *file)
  1629. {
  1630. struct drm_i915_gem_execbuffer2 *args = data;
  1631. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1632. int ret;
  1633. if (args->buffer_count < 1 ||
  1634. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1635. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1636. return -EINVAL;
  1637. }
  1638. if (args->rsvd2 != 0) {
  1639. DRM_DEBUG("dirty rvsd2 field\n");
  1640. return -EINVAL;
  1641. }
  1642. exec2_list = drm_malloc_gfp(args->buffer_count,
  1643. sizeof(*exec2_list),
  1644. GFP_TEMPORARY);
  1645. if (exec2_list == NULL) {
  1646. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1647. args->buffer_count);
  1648. return -ENOMEM;
  1649. }
  1650. ret = copy_from_user(exec2_list,
  1651. u64_to_user_ptr(args->buffers_ptr),
  1652. sizeof(*exec2_list) * args->buffer_count);
  1653. if (ret != 0) {
  1654. DRM_DEBUG("copy %d exec entries failed %d\n",
  1655. args->buffer_count, ret);
  1656. drm_free_large(exec2_list);
  1657. return -EFAULT;
  1658. }
  1659. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1660. if (!ret) {
  1661. /* Copy the new buffer offsets back to the user's exec list. */
  1662. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1663. u64_to_user_ptr(args->buffers_ptr);
  1664. int i;
  1665. for (i = 0; i < args->buffer_count; i++) {
  1666. exec2_list[i].offset =
  1667. gen8_canonical_addr(exec2_list[i].offset);
  1668. ret = __copy_to_user(&user_exec_list[i].offset,
  1669. &exec2_list[i].offset,
  1670. sizeof(user_exec_list[i].offset));
  1671. if (ret) {
  1672. ret = -EFAULT;
  1673. DRM_DEBUG("failed to copy %d exec entries "
  1674. "back to user\n",
  1675. args->buffer_count);
  1676. break;
  1677. }
  1678. }
  1679. }
  1680. drm_free_large(exec2_list);
  1681. return ret;
  1682. }