i915_gem_execbuffer.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  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_vma_instance(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. vma->exec_entry = NULL;
  238. i915_vma_put(vma);
  239. }
  240. kfree(eb);
  241. }
  242. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  243. {
  244. if (!i915_gem_object_has_struct_page(obj))
  245. return false;
  246. if (DBG_USE_CPU_RELOC)
  247. return DBG_USE_CPU_RELOC > 0;
  248. return (HAS_LLC(to_i915(obj->base.dev)) ||
  249. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  250. obj->cache_level != I915_CACHE_NONE);
  251. }
  252. /* Used to convert any address to canonical form.
  253. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  254. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  255. * addresses to be in a canonical form:
  256. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  257. * canonical form [63:48] == [47]."
  258. */
  259. #define GEN8_HIGH_ADDRESS_BIT 47
  260. static inline uint64_t gen8_canonical_addr(uint64_t address)
  261. {
  262. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  263. }
  264. static inline uint64_t gen8_noncanonical_addr(uint64_t address)
  265. {
  266. return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
  267. }
  268. static inline uint64_t
  269. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  270. uint64_t target_offset)
  271. {
  272. return gen8_canonical_addr((int)reloc->delta + target_offset);
  273. }
  274. struct reloc_cache {
  275. struct drm_i915_private *i915;
  276. struct drm_mm_node node;
  277. unsigned long vaddr;
  278. unsigned int page;
  279. bool use_64bit_reloc;
  280. };
  281. static void reloc_cache_init(struct reloc_cache *cache,
  282. struct drm_i915_private *i915)
  283. {
  284. cache->page = -1;
  285. cache->vaddr = 0;
  286. cache->i915 = i915;
  287. /* Must be a variable in the struct to allow GCC to unroll. */
  288. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  289. cache->node.allocated = false;
  290. }
  291. static inline void *unmask_page(unsigned long p)
  292. {
  293. return (void *)(uintptr_t)(p & PAGE_MASK);
  294. }
  295. static inline unsigned int unmask_flags(unsigned long p)
  296. {
  297. return p & ~PAGE_MASK;
  298. }
  299. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  300. static void reloc_cache_fini(struct reloc_cache *cache)
  301. {
  302. void *vaddr;
  303. if (!cache->vaddr)
  304. return;
  305. vaddr = unmask_page(cache->vaddr);
  306. if (cache->vaddr & KMAP) {
  307. if (cache->vaddr & CLFLUSH_AFTER)
  308. mb();
  309. kunmap_atomic(vaddr);
  310. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  311. } else {
  312. wmb();
  313. io_mapping_unmap_atomic((void __iomem *)vaddr);
  314. if (cache->node.allocated) {
  315. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  316. ggtt->base.clear_range(&ggtt->base,
  317. cache->node.start,
  318. cache->node.size);
  319. drm_mm_remove_node(&cache->node);
  320. } else {
  321. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  322. }
  323. }
  324. }
  325. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  326. struct reloc_cache *cache,
  327. int page)
  328. {
  329. void *vaddr;
  330. if (cache->vaddr) {
  331. kunmap_atomic(unmask_page(cache->vaddr));
  332. } else {
  333. unsigned int flushes;
  334. int ret;
  335. ret = i915_gem_obj_prepare_shmem_write(obj, &flushes);
  336. if (ret)
  337. return ERR_PTR(ret);
  338. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  339. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  340. cache->vaddr = flushes | KMAP;
  341. cache->node.mm = (void *)obj;
  342. if (flushes)
  343. mb();
  344. }
  345. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
  346. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  347. cache->page = page;
  348. return vaddr;
  349. }
  350. static void *reloc_iomap(struct drm_i915_gem_object *obj,
  351. struct reloc_cache *cache,
  352. int page)
  353. {
  354. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  355. unsigned long offset;
  356. void *vaddr;
  357. if (cache->vaddr) {
  358. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  359. } else {
  360. struct i915_vma *vma;
  361. int ret;
  362. if (use_cpu_reloc(obj))
  363. return NULL;
  364. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  365. if (ret)
  366. return ERR_PTR(ret);
  367. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  368. PIN_MAPPABLE | PIN_NONBLOCK);
  369. if (IS_ERR(vma)) {
  370. memset(&cache->node, 0, sizeof(cache->node));
  371. ret = drm_mm_insert_node_in_range
  372. (&ggtt->base.mm, &cache->node,
  373. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  374. 0, ggtt->mappable_end,
  375. DRM_MM_INSERT_LOW);
  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 && !IS_ALIGNED(vma->node.start, entry->alignment))
  725. return true;
  726. if (vma->node.size < entry->pad_to_size)
  727. return true;
  728. if (entry->flags & EXEC_OBJECT_PINNED &&
  729. vma->node.start != entry->offset)
  730. return true;
  731. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  732. vma->node.start < BATCH_OFFSET_BIAS)
  733. return true;
  734. /* avoid costly ping-pong once a batch bo ended up non-mappable */
  735. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  736. !i915_vma_is_map_and_fenceable(vma))
  737. return !only_mappable_for_reloc(entry->flags);
  738. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  739. (vma->node.start + vma->node.size - 1) >> 32)
  740. return true;
  741. return false;
  742. }
  743. static int
  744. i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
  745. struct list_head *vmas,
  746. struct i915_gem_context *ctx,
  747. bool *need_relocs)
  748. {
  749. struct drm_i915_gem_object *obj;
  750. struct i915_vma *vma;
  751. struct i915_address_space *vm;
  752. struct list_head ordered_vmas;
  753. struct list_head pinned_vmas;
  754. bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
  755. int retry;
  756. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  757. INIT_LIST_HEAD(&ordered_vmas);
  758. INIT_LIST_HEAD(&pinned_vmas);
  759. while (!list_empty(vmas)) {
  760. struct drm_i915_gem_exec_object2 *entry;
  761. bool need_fence, need_mappable;
  762. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  763. obj = vma->obj;
  764. entry = vma->exec_entry;
  765. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  766. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  767. if (!has_fenced_gpu_access)
  768. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  769. need_fence =
  770. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  771. i915_gem_object_is_tiled(obj);
  772. need_mappable = need_fence || need_reloc_mappable(vma);
  773. if (entry->flags & EXEC_OBJECT_PINNED)
  774. list_move_tail(&vma->exec_list, &pinned_vmas);
  775. else if (need_mappable) {
  776. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  777. list_move(&vma->exec_list, &ordered_vmas);
  778. } else
  779. list_move_tail(&vma->exec_list, &ordered_vmas);
  780. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  781. obj->base.pending_write_domain = 0;
  782. }
  783. list_splice(&ordered_vmas, vmas);
  784. list_splice(&pinned_vmas, vmas);
  785. /* Attempt to pin all of the buffers into the GTT.
  786. * This is done in 3 phases:
  787. *
  788. * 1a. Unbind all objects that do not match the GTT constraints for
  789. * the execbuffer (fenceable, mappable, alignment etc).
  790. * 1b. Increment pin count for already bound objects.
  791. * 2. Bind new objects.
  792. * 3. Decrement pin count.
  793. *
  794. * This avoid unnecessary unbinding of later objects in order to make
  795. * room for the earlier objects *unless* we need to defragment.
  796. */
  797. retry = 0;
  798. do {
  799. int ret = 0;
  800. /* Unbind any ill-fitting objects or pin. */
  801. list_for_each_entry(vma, vmas, exec_list) {
  802. if (!drm_mm_node_allocated(&vma->node))
  803. continue;
  804. if (eb_vma_misplaced(vma))
  805. ret = i915_vma_unbind(vma);
  806. else
  807. ret = i915_gem_execbuffer_reserve_vma(vma,
  808. engine,
  809. need_relocs);
  810. if (ret)
  811. goto err;
  812. }
  813. /* Bind fresh objects */
  814. list_for_each_entry(vma, vmas, exec_list) {
  815. if (drm_mm_node_allocated(&vma->node))
  816. continue;
  817. ret = i915_gem_execbuffer_reserve_vma(vma, engine,
  818. need_relocs);
  819. if (ret)
  820. goto err;
  821. }
  822. err:
  823. if (ret != -ENOSPC || retry++)
  824. return ret;
  825. /* Decrement pin count for bound objects */
  826. list_for_each_entry(vma, vmas, exec_list)
  827. i915_gem_execbuffer_unreserve_vma(vma);
  828. ret = i915_gem_evict_vm(vm, true);
  829. if (ret)
  830. return ret;
  831. } while (1);
  832. }
  833. static int
  834. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  835. struct drm_i915_gem_execbuffer2 *args,
  836. struct drm_file *file,
  837. struct intel_engine_cs *engine,
  838. struct eb_vmas *eb,
  839. struct drm_i915_gem_exec_object2 *exec,
  840. struct i915_gem_context *ctx)
  841. {
  842. struct drm_i915_gem_relocation_entry *reloc;
  843. struct i915_address_space *vm;
  844. struct i915_vma *vma;
  845. bool need_relocs;
  846. int *reloc_offset;
  847. int i, total, ret;
  848. unsigned count = args->buffer_count;
  849. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  850. /* We may process another execbuffer during the unlock... */
  851. while (!list_empty(&eb->vmas)) {
  852. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  853. list_del_init(&vma->exec_list);
  854. i915_gem_execbuffer_unreserve_vma(vma);
  855. i915_vma_put(vma);
  856. }
  857. mutex_unlock(&dev->struct_mutex);
  858. total = 0;
  859. for (i = 0; i < count; i++)
  860. total += exec[i].relocation_count;
  861. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  862. reloc = drm_malloc_ab(total, sizeof(*reloc));
  863. if (reloc == NULL || reloc_offset == NULL) {
  864. drm_free_large(reloc);
  865. drm_free_large(reloc_offset);
  866. mutex_lock(&dev->struct_mutex);
  867. return -ENOMEM;
  868. }
  869. total = 0;
  870. for (i = 0; i < count; i++) {
  871. struct drm_i915_gem_relocation_entry __user *user_relocs;
  872. u64 invalid_offset = (u64)-1;
  873. int j;
  874. user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
  875. if (copy_from_user(reloc+total, user_relocs,
  876. exec[i].relocation_count * sizeof(*reloc))) {
  877. ret = -EFAULT;
  878. mutex_lock(&dev->struct_mutex);
  879. goto err;
  880. }
  881. /* As we do not update the known relocation offsets after
  882. * relocating (due to the complexities in lock handling),
  883. * we need to mark them as invalid now so that we force the
  884. * relocation processing next time. Just in case the target
  885. * object is evicted and then rebound into its old
  886. * presumed_offset before the next execbuffer - if that
  887. * happened we would make the mistake of assuming that the
  888. * relocations were valid.
  889. */
  890. for (j = 0; j < exec[i].relocation_count; j++) {
  891. if (__copy_to_user(&user_relocs[j].presumed_offset,
  892. &invalid_offset,
  893. sizeof(invalid_offset))) {
  894. ret = -EFAULT;
  895. mutex_lock(&dev->struct_mutex);
  896. goto err;
  897. }
  898. }
  899. reloc_offset[i] = total;
  900. total += exec[i].relocation_count;
  901. }
  902. ret = i915_mutex_lock_interruptible(dev);
  903. if (ret) {
  904. mutex_lock(&dev->struct_mutex);
  905. goto err;
  906. }
  907. /* reacquire the objects */
  908. eb_reset(eb);
  909. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  910. if (ret)
  911. goto err;
  912. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  913. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  914. &need_relocs);
  915. if (ret)
  916. goto err;
  917. list_for_each_entry(vma, &eb->vmas, exec_list) {
  918. int offset = vma->exec_entry - exec;
  919. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  920. reloc + reloc_offset[offset]);
  921. if (ret)
  922. goto err;
  923. }
  924. /* Leave the user relocations as are, this is the painfully slow path,
  925. * and we want to avoid the complication of dropping the lock whilst
  926. * having buffers reserved in the aperture and so causing spurious
  927. * ENOSPC for random operations.
  928. */
  929. err:
  930. drm_free_large(reloc);
  931. drm_free_large(reloc_offset);
  932. return ret;
  933. }
  934. static int
  935. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  936. struct list_head *vmas)
  937. {
  938. struct i915_vma *vma;
  939. int ret;
  940. list_for_each_entry(vma, vmas, exec_list) {
  941. struct drm_i915_gem_object *obj = vma->obj;
  942. ret = i915_gem_request_await_object
  943. (req, obj, obj->base.pending_write_domain);
  944. if (ret)
  945. return ret;
  946. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  947. i915_gem_clflush_object(obj, false);
  948. }
  949. /* Unconditionally flush any chipset caches (for streaming writes). */
  950. i915_gem_chipset_flush(req->engine->i915);
  951. /* Unconditionally invalidate GPU caches and TLBs. */
  952. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  953. }
  954. static bool
  955. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  956. {
  957. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  958. return false;
  959. /* Kernel clipping was a DRI1 misfeature */
  960. if (exec->num_cliprects || exec->cliprects_ptr)
  961. return false;
  962. if (exec->DR4 == 0xffffffff) {
  963. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  964. exec->DR4 = 0;
  965. }
  966. if (exec->DR1 || exec->DR4)
  967. return false;
  968. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  969. return false;
  970. return true;
  971. }
  972. static int
  973. validate_exec_list(struct drm_device *dev,
  974. struct drm_i915_gem_exec_object2 *exec,
  975. int count)
  976. {
  977. unsigned relocs_total = 0;
  978. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  979. unsigned invalid_flags;
  980. int i;
  981. /* INTERNAL flags must not overlap with external ones */
  982. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  983. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  984. if (USES_FULL_PPGTT(dev))
  985. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  986. for (i = 0; i < count; i++) {
  987. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  988. int length; /* limited by fault_in_pages_readable() */
  989. if (exec[i].flags & invalid_flags)
  990. return -EINVAL;
  991. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  992. * any non-page-aligned or non-canonical addresses.
  993. */
  994. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  995. if (exec[i].offset !=
  996. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  997. return -EINVAL;
  998. }
  999. /* From drm_mm perspective address space is continuous,
  1000. * so from this point we're always using non-canonical
  1001. * form internally.
  1002. */
  1003. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  1004. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  1005. return -EINVAL;
  1006. /* pad_to_size was once a reserved field, so sanitize it */
  1007. if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
  1008. if (offset_in_page(exec[i].pad_to_size))
  1009. return -EINVAL;
  1010. } else {
  1011. exec[i].pad_to_size = 0;
  1012. }
  1013. /* First check for malicious input causing overflow in
  1014. * the worst case where we need to allocate the entire
  1015. * relocation tree as a single array.
  1016. */
  1017. if (exec[i].relocation_count > relocs_max - relocs_total)
  1018. return -EINVAL;
  1019. relocs_total += exec[i].relocation_count;
  1020. length = exec[i].relocation_count *
  1021. sizeof(struct drm_i915_gem_relocation_entry);
  1022. /*
  1023. * We must check that the entire relocation array is safe
  1024. * to read, but since we may need to update the presumed
  1025. * offsets during execution, check for full write access.
  1026. */
  1027. if (!access_ok(VERIFY_WRITE, ptr, length))
  1028. return -EFAULT;
  1029. if (likely(!i915.prefault_disable)) {
  1030. if (fault_in_pages_readable(ptr, length))
  1031. return -EFAULT;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. static struct i915_gem_context *
  1037. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  1038. struct intel_engine_cs *engine, const u32 ctx_id)
  1039. {
  1040. struct i915_gem_context *ctx;
  1041. ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
  1042. if (IS_ERR(ctx))
  1043. return ctx;
  1044. if (i915_gem_context_is_banned(ctx)) {
  1045. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  1046. return ERR_PTR(-EIO);
  1047. }
  1048. return ctx;
  1049. }
  1050. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  1051. {
  1052. return !(obj->cache_level == I915_CACHE_NONE ||
  1053. obj->cache_level == I915_CACHE_WT);
  1054. }
  1055. void i915_vma_move_to_active(struct i915_vma *vma,
  1056. struct drm_i915_gem_request *req,
  1057. unsigned int flags)
  1058. {
  1059. struct drm_i915_gem_object *obj = vma->obj;
  1060. const unsigned int idx = req->engine->id;
  1061. lockdep_assert_held(&req->i915->drm.struct_mutex);
  1062. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1063. /* Add a reference if we're newly entering the active list.
  1064. * The order in which we add operations to the retirement queue is
  1065. * vital here: mark_active adds to the start of the callback list,
  1066. * such that subsequent callbacks are called first. Therefore we
  1067. * add the active reference first and queue for it to be dropped
  1068. * *last*.
  1069. */
  1070. if (!i915_vma_is_active(vma))
  1071. obj->active_count++;
  1072. i915_vma_set_active(vma, idx);
  1073. i915_gem_active_set(&vma->last_read[idx], req);
  1074. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1075. if (flags & EXEC_OBJECT_WRITE) {
  1076. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1077. i915_gem_active_set(&obj->frontbuffer_write, req);
  1078. /* update for the implicit flush after a batch */
  1079. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  1080. if (!obj->cache_dirty && gpu_write_needs_clflush(obj))
  1081. obj->cache_dirty = true;
  1082. }
  1083. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1084. i915_gem_active_set(&vma->last_fence, req);
  1085. }
  1086. static void eb_export_fence(struct drm_i915_gem_object *obj,
  1087. struct drm_i915_gem_request *req,
  1088. unsigned int flags)
  1089. {
  1090. struct reservation_object *resv = obj->resv;
  1091. /* Ignore errors from failing to allocate the new fence, we can't
  1092. * handle an error right now. Worst case should be missed
  1093. * synchronisation leading to rendering corruption.
  1094. */
  1095. ww_mutex_lock(&resv->lock, NULL);
  1096. if (flags & EXEC_OBJECT_WRITE)
  1097. reservation_object_add_excl_fence(resv, &req->fence);
  1098. else if (reservation_object_reserve_shared(resv) == 0)
  1099. reservation_object_add_shared_fence(resv, &req->fence);
  1100. ww_mutex_unlock(&resv->lock);
  1101. }
  1102. static void
  1103. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  1104. struct drm_i915_gem_request *req)
  1105. {
  1106. struct i915_vma *vma;
  1107. list_for_each_entry(vma, vmas, exec_list) {
  1108. struct drm_i915_gem_object *obj = vma->obj;
  1109. u32 old_read = obj->base.read_domains;
  1110. u32 old_write = obj->base.write_domain;
  1111. obj->base.write_domain = obj->base.pending_write_domain;
  1112. if (obj->base.write_domain)
  1113. vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
  1114. else
  1115. obj->base.pending_read_domains |= obj->base.read_domains;
  1116. obj->base.read_domains = obj->base.pending_read_domains;
  1117. i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
  1118. eb_export_fence(obj, req, vma->exec_entry->flags);
  1119. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  1120. }
  1121. }
  1122. static int
  1123. i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1124. {
  1125. struct intel_ring *ring = req->ring;
  1126. int ret, i;
  1127. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1128. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1129. return -EINVAL;
  1130. }
  1131. ret = intel_ring_begin(req, 4 * 3);
  1132. if (ret)
  1133. return ret;
  1134. for (i = 0; i < 4; i++) {
  1135. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1136. intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
  1137. intel_ring_emit(ring, 0);
  1138. }
  1139. intel_ring_advance(ring);
  1140. return 0;
  1141. }
  1142. static struct i915_vma *
  1143. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  1144. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1145. struct drm_i915_gem_object *batch_obj,
  1146. struct eb_vmas *eb,
  1147. u32 batch_start_offset,
  1148. u32 batch_len,
  1149. bool is_master)
  1150. {
  1151. struct drm_i915_gem_object *shadow_batch_obj;
  1152. struct i915_vma *vma;
  1153. int ret;
  1154. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  1155. PAGE_ALIGN(batch_len));
  1156. if (IS_ERR(shadow_batch_obj))
  1157. return ERR_CAST(shadow_batch_obj);
  1158. ret = intel_engine_cmd_parser(engine,
  1159. batch_obj,
  1160. shadow_batch_obj,
  1161. batch_start_offset,
  1162. batch_len,
  1163. is_master);
  1164. if (ret) {
  1165. if (ret == -EACCES) /* unhandled chained batch */
  1166. vma = NULL;
  1167. else
  1168. vma = ERR_PTR(ret);
  1169. goto out;
  1170. }
  1171. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1172. if (IS_ERR(vma))
  1173. goto out;
  1174. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1175. vma->exec_entry = shadow_exec_entry;
  1176. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1177. i915_gem_object_get(shadow_batch_obj);
  1178. list_add_tail(&vma->exec_list, &eb->vmas);
  1179. out:
  1180. i915_gem_object_unpin_pages(shadow_batch_obj);
  1181. return vma;
  1182. }
  1183. static int
  1184. execbuf_submit(struct i915_execbuffer_params *params,
  1185. struct drm_i915_gem_execbuffer2 *args,
  1186. struct list_head *vmas)
  1187. {
  1188. u64 exec_start, exec_len;
  1189. int ret;
  1190. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1191. if (ret)
  1192. return ret;
  1193. ret = i915_switch_context(params->request);
  1194. if (ret)
  1195. return ret;
  1196. if (args->flags & I915_EXEC_CONSTANTS_MASK) {
  1197. DRM_DEBUG("I915_EXEC_CONSTANTS_* unsupported\n");
  1198. return -EINVAL;
  1199. }
  1200. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1201. ret = i915_reset_gen7_sol_offsets(params->request);
  1202. if (ret)
  1203. return ret;
  1204. }
  1205. exec_len = args->batch_len;
  1206. exec_start = params->batch->node.start +
  1207. params->args_batch_start_offset;
  1208. if (exec_len == 0)
  1209. exec_len = params->batch->size - params->args_batch_start_offset;
  1210. ret = params->engine->emit_bb_start(params->request,
  1211. exec_start, exec_len,
  1212. params->dispatch_flags);
  1213. if (ret)
  1214. return ret;
  1215. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1216. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1217. return 0;
  1218. }
  1219. /**
  1220. * Find one BSD ring to dispatch the corresponding BSD command.
  1221. * The engine index is returned.
  1222. */
  1223. static unsigned int
  1224. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1225. struct drm_file *file)
  1226. {
  1227. struct drm_i915_file_private *file_priv = file->driver_priv;
  1228. /* Check whether the file_priv has already selected one ring. */
  1229. if ((int)file_priv->bsd_engine < 0)
  1230. file_priv->bsd_engine = atomic_fetch_xor(1,
  1231. &dev_priv->mm.bsd_engine_dispatch_index);
  1232. return file_priv->bsd_engine;
  1233. }
  1234. #define I915_USER_RINGS (4)
  1235. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1236. [I915_EXEC_DEFAULT] = RCS,
  1237. [I915_EXEC_RENDER] = RCS,
  1238. [I915_EXEC_BLT] = BCS,
  1239. [I915_EXEC_BSD] = VCS,
  1240. [I915_EXEC_VEBOX] = VECS
  1241. };
  1242. static struct intel_engine_cs *
  1243. eb_select_engine(struct drm_i915_private *dev_priv,
  1244. struct drm_file *file,
  1245. struct drm_i915_gem_execbuffer2 *args)
  1246. {
  1247. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1248. struct intel_engine_cs *engine;
  1249. if (user_ring_id > I915_USER_RINGS) {
  1250. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1251. return NULL;
  1252. }
  1253. if ((user_ring_id != I915_EXEC_BSD) &&
  1254. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1255. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1256. "bsd dispatch flags: %d\n", (int)(args->flags));
  1257. return NULL;
  1258. }
  1259. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1260. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1261. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1262. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1263. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1264. bsd_idx <= I915_EXEC_BSD_RING2) {
  1265. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1266. bsd_idx--;
  1267. } else {
  1268. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1269. bsd_idx);
  1270. return NULL;
  1271. }
  1272. engine = dev_priv->engine[_VCS(bsd_idx)];
  1273. } else {
  1274. engine = dev_priv->engine[user_ring_map[user_ring_id]];
  1275. }
  1276. if (!engine) {
  1277. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1278. return NULL;
  1279. }
  1280. return engine;
  1281. }
  1282. static int
  1283. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1284. struct drm_file *file,
  1285. struct drm_i915_gem_execbuffer2 *args,
  1286. struct drm_i915_gem_exec_object2 *exec)
  1287. {
  1288. struct drm_i915_private *dev_priv = to_i915(dev);
  1289. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1290. struct eb_vmas *eb;
  1291. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1292. struct intel_engine_cs *engine;
  1293. struct i915_gem_context *ctx;
  1294. struct i915_address_space *vm;
  1295. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1296. struct i915_execbuffer_params *params = &params_master;
  1297. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1298. u32 dispatch_flags;
  1299. int ret;
  1300. bool need_relocs;
  1301. if (!i915_gem_check_execbuffer(args))
  1302. return -EINVAL;
  1303. ret = validate_exec_list(dev, exec, args->buffer_count);
  1304. if (ret)
  1305. return ret;
  1306. dispatch_flags = 0;
  1307. if (args->flags & I915_EXEC_SECURE) {
  1308. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1309. return -EPERM;
  1310. dispatch_flags |= I915_DISPATCH_SECURE;
  1311. }
  1312. if (args->flags & I915_EXEC_IS_PINNED)
  1313. dispatch_flags |= I915_DISPATCH_PINNED;
  1314. engine = eb_select_engine(dev_priv, file, args);
  1315. if (!engine)
  1316. return -EINVAL;
  1317. if (args->buffer_count < 1) {
  1318. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1319. return -EINVAL;
  1320. }
  1321. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1322. if (!HAS_RESOURCE_STREAMER(dev_priv)) {
  1323. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1324. return -EINVAL;
  1325. }
  1326. if (engine->id != RCS) {
  1327. DRM_DEBUG("RS is not available on %s\n",
  1328. engine->name);
  1329. return -EINVAL;
  1330. }
  1331. dispatch_flags |= I915_DISPATCH_RS;
  1332. }
  1333. /* Take a local wakeref for preparing to dispatch the execbuf as
  1334. * we expect to access the hardware fairly frequently in the
  1335. * process. Upon first dispatch, we acquire another prolonged
  1336. * wakeref that we hold until the GPU has been idle for at least
  1337. * 100ms.
  1338. */
  1339. intel_runtime_pm_get(dev_priv);
  1340. ret = i915_mutex_lock_interruptible(dev);
  1341. if (ret)
  1342. goto pre_mutex_err;
  1343. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1344. if (IS_ERR(ctx)) {
  1345. mutex_unlock(&dev->struct_mutex);
  1346. ret = PTR_ERR(ctx);
  1347. goto pre_mutex_err;
  1348. }
  1349. i915_gem_context_get(ctx);
  1350. if (ctx->ppgtt)
  1351. vm = &ctx->ppgtt->base;
  1352. else
  1353. vm = &ggtt->base;
  1354. memset(&params_master, 0x00, sizeof(params_master));
  1355. eb = eb_create(dev_priv, args);
  1356. if (eb == NULL) {
  1357. i915_gem_context_put(ctx);
  1358. mutex_unlock(&dev->struct_mutex);
  1359. ret = -ENOMEM;
  1360. goto pre_mutex_err;
  1361. }
  1362. /* Look up object handles */
  1363. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1364. if (ret)
  1365. goto err;
  1366. /* take note of the batch buffer before we might reorder the lists */
  1367. params->batch = eb_get_batch(eb);
  1368. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1369. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1370. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1371. &need_relocs);
  1372. if (ret)
  1373. goto err;
  1374. /* The objects are in their final locations, apply the relocations. */
  1375. if (need_relocs)
  1376. ret = i915_gem_execbuffer_relocate(eb);
  1377. if (ret) {
  1378. if (ret == -EFAULT) {
  1379. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1380. engine,
  1381. eb, exec, ctx);
  1382. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1383. }
  1384. if (ret)
  1385. goto err;
  1386. }
  1387. /* Set the pending read domains for the batch buffer to COMMAND */
  1388. if (params->batch->obj->base.pending_write_domain) {
  1389. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1390. ret = -EINVAL;
  1391. goto err;
  1392. }
  1393. if (args->batch_start_offset > params->batch->size ||
  1394. args->batch_len > params->batch->size - args->batch_start_offset) {
  1395. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1396. ret = -EINVAL;
  1397. goto err;
  1398. }
  1399. params->args_batch_start_offset = args->batch_start_offset;
  1400. if (engine->needs_cmd_parser && args->batch_len) {
  1401. struct i915_vma *vma;
  1402. vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
  1403. params->batch->obj,
  1404. eb,
  1405. args->batch_start_offset,
  1406. args->batch_len,
  1407. drm_is_current_master(file));
  1408. if (IS_ERR(vma)) {
  1409. ret = PTR_ERR(vma);
  1410. goto err;
  1411. }
  1412. if (vma) {
  1413. /*
  1414. * Batch parsed and accepted:
  1415. *
  1416. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1417. * bit from MI_BATCH_BUFFER_START commands issued in
  1418. * the dispatch_execbuffer implementations. We
  1419. * specifically don't want that set on batches the
  1420. * command parser has accepted.
  1421. */
  1422. dispatch_flags |= I915_DISPATCH_SECURE;
  1423. params->args_batch_start_offset = 0;
  1424. params->batch = vma;
  1425. }
  1426. }
  1427. params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1428. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1429. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1430. * hsw should have this fixed, but bdw mucks it up again. */
  1431. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1432. struct drm_i915_gem_object *obj = params->batch->obj;
  1433. struct i915_vma *vma;
  1434. /*
  1435. * So on first glance it looks freaky that we pin the batch here
  1436. * outside of the reservation loop. But:
  1437. * - The batch is already pinned into the relevant ppgtt, so we
  1438. * already have the backing storage fully allocated.
  1439. * - No other BO uses the global gtt (well contexts, but meh),
  1440. * so we don't really have issues with multiple objects not
  1441. * fitting due to fragmentation.
  1442. * So this is actually safe.
  1443. */
  1444. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  1445. if (IS_ERR(vma)) {
  1446. ret = PTR_ERR(vma);
  1447. goto err;
  1448. }
  1449. params->batch = vma;
  1450. }
  1451. /* Allocate a request for this batch buffer nice and early. */
  1452. params->request = i915_gem_request_alloc(engine, ctx);
  1453. if (IS_ERR(params->request)) {
  1454. ret = PTR_ERR(params->request);
  1455. goto err_batch_unpin;
  1456. }
  1457. /* Whilst this request exists, batch_obj will be on the
  1458. * active_list, and so will hold the active reference. Only when this
  1459. * request is retired will the the batch_obj be moved onto the
  1460. * inactive_list and lose its active reference. Hence we do not need
  1461. * to explicitly hold another reference here.
  1462. */
  1463. params->request->batch = params->batch;
  1464. ret = i915_gem_request_add_to_client(params->request, file);
  1465. if (ret)
  1466. goto err_request;
  1467. /*
  1468. * Save assorted stuff away to pass through to *_submission().
  1469. * NB: This data should be 'persistent' and not local as it will
  1470. * kept around beyond the duration of the IOCTL once the GPU
  1471. * scheduler arrives.
  1472. */
  1473. params->dev = dev;
  1474. params->file = file;
  1475. params->engine = engine;
  1476. params->dispatch_flags = dispatch_flags;
  1477. params->ctx = ctx;
  1478. ret = execbuf_submit(params, args, &eb->vmas);
  1479. err_request:
  1480. __i915_add_request(params->request, ret == 0);
  1481. err_batch_unpin:
  1482. /*
  1483. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1484. * batch vma for correctness. For less ugly and less fragility this
  1485. * needs to be adjusted to also track the ggtt batch vma properly as
  1486. * active.
  1487. */
  1488. if (dispatch_flags & I915_DISPATCH_SECURE)
  1489. i915_vma_unpin(params->batch);
  1490. err:
  1491. /* the request owns the ref now */
  1492. i915_gem_context_put(ctx);
  1493. eb_destroy(eb);
  1494. mutex_unlock(&dev->struct_mutex);
  1495. pre_mutex_err:
  1496. /* intel_gpu_busy should also get a ref, so it will free when the device
  1497. * is really idle. */
  1498. intel_runtime_pm_put(dev_priv);
  1499. return ret;
  1500. }
  1501. /*
  1502. * Legacy execbuffer just creates an exec2 list from the original exec object
  1503. * list array and passes it to the real function.
  1504. */
  1505. int
  1506. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1507. struct drm_file *file)
  1508. {
  1509. struct drm_i915_gem_execbuffer *args = data;
  1510. struct drm_i915_gem_execbuffer2 exec2;
  1511. struct drm_i915_gem_exec_object *exec_list = NULL;
  1512. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1513. int ret, i;
  1514. if (args->buffer_count < 1) {
  1515. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1516. return -EINVAL;
  1517. }
  1518. /* Copy in the exec list from userland */
  1519. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1520. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1521. if (exec_list == NULL || exec2_list == NULL) {
  1522. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1523. args->buffer_count);
  1524. drm_free_large(exec_list);
  1525. drm_free_large(exec2_list);
  1526. return -ENOMEM;
  1527. }
  1528. ret = copy_from_user(exec_list,
  1529. u64_to_user_ptr(args->buffers_ptr),
  1530. sizeof(*exec_list) * args->buffer_count);
  1531. if (ret != 0) {
  1532. DRM_DEBUG("copy %d exec entries failed %d\n",
  1533. args->buffer_count, ret);
  1534. drm_free_large(exec_list);
  1535. drm_free_large(exec2_list);
  1536. return -EFAULT;
  1537. }
  1538. for (i = 0; i < args->buffer_count; i++) {
  1539. exec2_list[i].handle = exec_list[i].handle;
  1540. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1541. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1542. exec2_list[i].alignment = exec_list[i].alignment;
  1543. exec2_list[i].offset = exec_list[i].offset;
  1544. if (INTEL_GEN(to_i915(dev)) < 4)
  1545. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1546. else
  1547. exec2_list[i].flags = 0;
  1548. }
  1549. exec2.buffers_ptr = args->buffers_ptr;
  1550. exec2.buffer_count = args->buffer_count;
  1551. exec2.batch_start_offset = args->batch_start_offset;
  1552. exec2.batch_len = args->batch_len;
  1553. exec2.DR1 = args->DR1;
  1554. exec2.DR4 = args->DR4;
  1555. exec2.num_cliprects = args->num_cliprects;
  1556. exec2.cliprects_ptr = args->cliprects_ptr;
  1557. exec2.flags = I915_EXEC_RENDER;
  1558. i915_execbuffer2_set_context_id(exec2, 0);
  1559. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1560. if (!ret) {
  1561. struct drm_i915_gem_exec_object __user *user_exec_list =
  1562. u64_to_user_ptr(args->buffers_ptr);
  1563. /* Copy the new buffer offsets back to the user's exec list. */
  1564. for (i = 0; i < args->buffer_count; i++) {
  1565. exec2_list[i].offset =
  1566. gen8_canonical_addr(exec2_list[i].offset);
  1567. ret = __copy_to_user(&user_exec_list[i].offset,
  1568. &exec2_list[i].offset,
  1569. sizeof(user_exec_list[i].offset));
  1570. if (ret) {
  1571. ret = -EFAULT;
  1572. DRM_DEBUG("failed to copy %d exec entries "
  1573. "back to user (%d)\n",
  1574. args->buffer_count, ret);
  1575. break;
  1576. }
  1577. }
  1578. }
  1579. drm_free_large(exec_list);
  1580. drm_free_large(exec2_list);
  1581. return ret;
  1582. }
  1583. int
  1584. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1585. struct drm_file *file)
  1586. {
  1587. struct drm_i915_gem_execbuffer2 *args = data;
  1588. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1589. int ret;
  1590. if (args->buffer_count < 1 ||
  1591. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1592. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1593. return -EINVAL;
  1594. }
  1595. if (args->rsvd2 != 0) {
  1596. DRM_DEBUG("dirty rvsd2 field\n");
  1597. return -EINVAL;
  1598. }
  1599. exec2_list = drm_malloc_gfp(args->buffer_count,
  1600. sizeof(*exec2_list),
  1601. GFP_TEMPORARY);
  1602. if (exec2_list == NULL) {
  1603. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1604. args->buffer_count);
  1605. return -ENOMEM;
  1606. }
  1607. ret = copy_from_user(exec2_list,
  1608. u64_to_user_ptr(args->buffers_ptr),
  1609. sizeof(*exec2_list) * args->buffer_count);
  1610. if (ret != 0) {
  1611. DRM_DEBUG("copy %d exec entries failed %d\n",
  1612. args->buffer_count, ret);
  1613. drm_free_large(exec2_list);
  1614. return -EFAULT;
  1615. }
  1616. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1617. if (!ret) {
  1618. /* Copy the new buffer offsets back to the user's exec list. */
  1619. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1620. u64_to_user_ptr(args->buffers_ptr);
  1621. int i;
  1622. for (i = 0; i < args->buffer_count; i++) {
  1623. exec2_list[i].offset =
  1624. gen8_canonical_addr(exec2_list[i].offset);
  1625. ret = __copy_to_user(&user_exec_list[i].offset,
  1626. &exec2_list[i].offset,
  1627. sizeof(user_exec_list[i].offset));
  1628. if (ret) {
  1629. ret = -EFAULT;
  1630. DRM_DEBUG("failed to copy %d exec entries "
  1631. "back to user\n",
  1632. args->buffer_count);
  1633. break;
  1634. }
  1635. }
  1636. }
  1637. drm_free_large(exec2_list);
  1638. return ret;
  1639. }