i915_gem_execbuffer.c 52 KB

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