i915_gem_execbuffer.c 53 KB

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