i915_gem_execbuffer.c 50 KB

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