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 <linux/dma_remapping.h>
  34. #include <linux/uaccess.h>
  35. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  36. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  37. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  38. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  39. #define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
  40. #define BATCH_OFFSET_BIAS (256*1024)
  41. struct i915_execbuffer_params {
  42. struct drm_device *dev;
  43. struct drm_file *file;
  44. u32 dispatch_flags;
  45. u32 args_batch_start_offset;
  46. u32 batch_obj_vm_offset;
  47. struct intel_engine_cs *engine;
  48. struct drm_i915_gem_object *batch_obj;
  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 int
  94. eb_lookup_vmas(struct eb_vmas *eb,
  95. struct drm_i915_gem_exec_object2 *exec,
  96. const struct drm_i915_gem_execbuffer2 *args,
  97. struct i915_address_space *vm,
  98. struct drm_file *file)
  99. {
  100. struct drm_i915_gem_object *obj;
  101. struct list_head objects;
  102. int i, ret;
  103. INIT_LIST_HEAD(&objects);
  104. spin_lock(&file->table_lock);
  105. /* Grab a reference to the object and release the lock so we can lookup
  106. * or create the VMA without using GFP_ATOMIC */
  107. for (i = 0; i < args->buffer_count; i++) {
  108. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  109. if (obj == NULL) {
  110. spin_unlock(&file->table_lock);
  111. DRM_DEBUG("Invalid object handle %d at index %d\n",
  112. exec[i].handle, i);
  113. ret = -ENOENT;
  114. goto err;
  115. }
  116. if (!list_empty(&obj->obj_exec_link)) {
  117. spin_unlock(&file->table_lock);
  118. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  119. obj, exec[i].handle, i);
  120. ret = -EINVAL;
  121. goto err;
  122. }
  123. i915_gem_object_get(obj);
  124. list_add_tail(&obj->obj_exec_link, &objects);
  125. }
  126. spin_unlock(&file->table_lock);
  127. i = 0;
  128. while (!list_empty(&objects)) {
  129. struct i915_vma *vma;
  130. obj = list_first_entry(&objects,
  131. struct drm_i915_gem_object,
  132. obj_exec_link);
  133. /*
  134. * NOTE: We can leak any vmas created here when something fails
  135. * later on. But that's no issue since vma_unbind can deal with
  136. * vmas which are not actually bound. And since only
  137. * lookup_or_create exists as an interface to get at the vma
  138. * from the (obj, vm) we don't run the risk of creating
  139. * duplicated vmas for the same vm.
  140. */
  141. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  142. if (IS_ERR(vma)) {
  143. DRM_DEBUG("Failed to lookup VMA\n");
  144. ret = PTR_ERR(vma);
  145. goto err;
  146. }
  147. /* Transfer ownership from the objects list to the vmas list. */
  148. list_add_tail(&vma->exec_list, &eb->vmas);
  149. list_del_init(&obj->obj_exec_link);
  150. vma->exec_entry = &exec[i];
  151. if (eb->and < 0) {
  152. eb->lut[i] = vma;
  153. } else {
  154. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  155. vma->exec_handle = handle;
  156. hlist_add_head(&vma->exec_node,
  157. &eb->buckets[handle & eb->and]);
  158. }
  159. ++i;
  160. }
  161. return 0;
  162. err:
  163. while (!list_empty(&objects)) {
  164. obj = list_first_entry(&objects,
  165. struct drm_i915_gem_object,
  166. obj_exec_link);
  167. list_del_init(&obj->obj_exec_link);
  168. i915_gem_object_put(obj);
  169. }
  170. /*
  171. * Objects already transfered to the vmas list will be unreferenced by
  172. * eb_destroy.
  173. */
  174. return ret;
  175. }
  176. static inline struct i915_vma *
  177. eb_get_batch_vma(struct eb_vmas *eb)
  178. {
  179. /* The batch is always the LAST item in the VMA list */
  180. struct i915_vma *vma = list_last_entry(&eb->vmas, typeof(*vma), exec_list);
  181. return vma;
  182. }
  183. static struct drm_i915_gem_object *
  184. eb_get_batch(struct eb_vmas *eb)
  185. {
  186. struct i915_vma *vma = eb_get_batch_vma(eb);
  187. /*
  188. * SNA is doing fancy tricks with compressing batch buffers, which leads
  189. * to negative relocation deltas. Usually that works out ok since the
  190. * relocate address is still positive, except when the batch is placed
  191. * very low in the GTT. Ensure this doesn't happen.
  192. *
  193. * Note that actual hangs have only been observed on gen7, but for
  194. * paranoia do it everywhere.
  195. */
  196. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  197. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  198. return vma->obj;
  199. }
  200. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  201. {
  202. if (eb->and < 0) {
  203. if (handle >= -eb->and)
  204. return NULL;
  205. return eb->lut[handle];
  206. } else {
  207. struct hlist_head *head;
  208. struct i915_vma *vma;
  209. head = &eb->buckets[handle & eb->and];
  210. hlist_for_each_entry(vma, head, exec_node) {
  211. if (vma->exec_handle == handle)
  212. return vma;
  213. }
  214. return NULL;
  215. }
  216. }
  217. static void
  218. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  219. {
  220. struct drm_i915_gem_exec_object2 *entry;
  221. struct drm_i915_gem_object *obj = vma->obj;
  222. if (!drm_mm_node_allocated(&vma->node))
  223. return;
  224. entry = vma->exec_entry;
  225. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  226. i915_gem_object_unpin_fence(obj);
  227. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  228. vma->pin_count--;
  229. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  230. }
  231. static void eb_destroy(struct eb_vmas *eb)
  232. {
  233. while (!list_empty(&eb->vmas)) {
  234. struct i915_vma *vma;
  235. vma = list_first_entry(&eb->vmas,
  236. struct i915_vma,
  237. exec_list);
  238. list_del_init(&vma->exec_list);
  239. i915_gem_execbuffer_unreserve_vma(vma);
  240. i915_gem_object_put(vma->obj);
  241. }
  242. kfree(eb);
  243. }
  244. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  245. {
  246. return (HAS_LLC(obj->base.dev) ||
  247. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  248. obj->cache_level != I915_CACHE_NONE);
  249. }
  250. /* Used to convert any address to canonical form.
  251. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  252. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  253. * addresses to be in a canonical form:
  254. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  255. * canonical form [63:48] == [47]."
  256. */
  257. #define GEN8_HIGH_ADDRESS_BIT 47
  258. static inline uint64_t gen8_canonical_addr(uint64_t address)
  259. {
  260. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  261. }
  262. static inline uint64_t gen8_noncanonical_addr(uint64_t address)
  263. {
  264. return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
  265. }
  266. static inline uint64_t
  267. relocation_target(struct drm_i915_gem_relocation_entry *reloc,
  268. uint64_t target_offset)
  269. {
  270. return gen8_canonical_addr((int)reloc->delta + target_offset);
  271. }
  272. static int
  273. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  274. struct drm_i915_gem_relocation_entry *reloc,
  275. uint64_t target_offset)
  276. {
  277. struct drm_device *dev = obj->base.dev;
  278. uint32_t page_offset = offset_in_page(reloc->offset);
  279. uint64_t delta = relocation_target(reloc, target_offset);
  280. char *vaddr;
  281. int ret;
  282. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  283. if (ret)
  284. return ret;
  285. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  286. reloc->offset >> PAGE_SHIFT));
  287. *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
  288. if (INTEL_INFO(dev)->gen >= 8) {
  289. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  290. if (page_offset == 0) {
  291. kunmap_atomic(vaddr);
  292. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  293. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  294. }
  295. *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
  296. }
  297. kunmap_atomic(vaddr);
  298. return 0;
  299. }
  300. static int
  301. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  302. struct drm_i915_gem_relocation_entry *reloc,
  303. uint64_t target_offset)
  304. {
  305. struct drm_device *dev = obj->base.dev;
  306. struct drm_i915_private *dev_priv = to_i915(dev);
  307. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  308. uint64_t delta = relocation_target(reloc, target_offset);
  309. uint64_t offset;
  310. void __iomem *reloc_page;
  311. int ret;
  312. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  313. if (ret)
  314. return ret;
  315. ret = i915_gem_object_put_fence(obj);
  316. if (ret)
  317. return ret;
  318. /* Map the page containing the relocation we're going to perform. */
  319. offset = i915_gem_obj_ggtt_offset(obj);
  320. offset += reloc->offset;
  321. reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
  322. offset & PAGE_MASK);
  323. iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
  324. if (INTEL_INFO(dev)->gen >= 8) {
  325. offset += sizeof(uint32_t);
  326. if (offset_in_page(offset) == 0) {
  327. io_mapping_unmap_atomic(reloc_page);
  328. reloc_page =
  329. io_mapping_map_atomic_wc(ggtt->mappable,
  330. offset);
  331. }
  332. iowrite32(upper_32_bits(delta),
  333. reloc_page + offset_in_page(offset));
  334. }
  335. io_mapping_unmap_atomic(reloc_page);
  336. return 0;
  337. }
  338. static void
  339. clflush_write32(void *addr, uint32_t value)
  340. {
  341. /* This is not a fast path, so KISS. */
  342. drm_clflush_virt_range(addr, sizeof(uint32_t));
  343. *(uint32_t *)addr = value;
  344. drm_clflush_virt_range(addr, sizeof(uint32_t));
  345. }
  346. static int
  347. relocate_entry_clflush(struct drm_i915_gem_object *obj,
  348. struct drm_i915_gem_relocation_entry *reloc,
  349. uint64_t target_offset)
  350. {
  351. struct drm_device *dev = obj->base.dev;
  352. uint32_t page_offset = offset_in_page(reloc->offset);
  353. uint64_t delta = relocation_target(reloc, target_offset);
  354. char *vaddr;
  355. int ret;
  356. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  357. if (ret)
  358. return ret;
  359. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  360. reloc->offset >> PAGE_SHIFT));
  361. clflush_write32(vaddr + page_offset, lower_32_bits(delta));
  362. if (INTEL_INFO(dev)->gen >= 8) {
  363. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  364. if (page_offset == 0) {
  365. kunmap_atomic(vaddr);
  366. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  367. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  368. }
  369. clflush_write32(vaddr + page_offset, upper_32_bits(delta));
  370. }
  371. kunmap_atomic(vaddr);
  372. return 0;
  373. }
  374. static bool object_is_idle(struct drm_i915_gem_object *obj)
  375. {
  376. unsigned long active = obj->active;
  377. int idx;
  378. for_each_active(active, idx) {
  379. if (!i915_gem_active_is_idle(&obj->last_read[idx],
  380. &obj->base.dev->struct_mutex))
  381. return false;
  382. }
  383. return true;
  384. }
  385. static int
  386. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  387. struct eb_vmas *eb,
  388. struct drm_i915_gem_relocation_entry *reloc)
  389. {
  390. struct drm_device *dev = obj->base.dev;
  391. struct drm_gem_object *target_obj;
  392. struct drm_i915_gem_object *target_i915_obj;
  393. struct i915_vma *target_vma;
  394. uint64_t target_offset;
  395. int ret;
  396. /* we've already hold a reference to all valid objects */
  397. target_vma = eb_get_vma(eb, reloc->target_handle);
  398. if (unlikely(target_vma == NULL))
  399. return -ENOENT;
  400. target_i915_obj = target_vma->obj;
  401. target_obj = &target_vma->obj->base;
  402. target_offset = gen8_canonical_addr(target_vma->node.start);
  403. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  404. * pipe_control writes because the gpu doesn't properly redirect them
  405. * through the ppgtt for non_secure batchbuffers. */
  406. if (unlikely(IS_GEN6(dev) &&
  407. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
  408. ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
  409. PIN_GLOBAL);
  410. if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
  411. return ret;
  412. }
  413. /* Validate that the target is in a valid r/w GPU domain */
  414. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  415. DRM_DEBUG("reloc with multiple write domains: "
  416. "obj %p target %d offset %d "
  417. "read %08x write %08x",
  418. obj, reloc->target_handle,
  419. (int) reloc->offset,
  420. reloc->read_domains,
  421. reloc->write_domain);
  422. return -EINVAL;
  423. }
  424. if (unlikely((reloc->write_domain | reloc->read_domains)
  425. & ~I915_GEM_GPU_DOMAINS)) {
  426. DRM_DEBUG("reloc with read/write non-GPU domains: "
  427. "obj %p target %d offset %d "
  428. "read %08x write %08x",
  429. obj, reloc->target_handle,
  430. (int) reloc->offset,
  431. reloc->read_domains,
  432. reloc->write_domain);
  433. return -EINVAL;
  434. }
  435. target_obj->pending_read_domains |= reloc->read_domains;
  436. target_obj->pending_write_domain |= reloc->write_domain;
  437. /* If the relocation already has the right value in it, no
  438. * more work needs to be done.
  439. */
  440. if (target_offset == reloc->presumed_offset)
  441. return 0;
  442. /* Check that the relocation address is valid... */
  443. if (unlikely(reloc->offset >
  444. obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  445. DRM_DEBUG("Relocation beyond object bounds: "
  446. "obj %p target %d offset %d size %d.\n",
  447. obj, reloc->target_handle,
  448. (int) reloc->offset,
  449. (int) obj->base.size);
  450. return -EINVAL;
  451. }
  452. if (unlikely(reloc->offset & 3)) {
  453. DRM_DEBUG("Relocation not 4-byte aligned: "
  454. "obj %p target %d offset %d.\n",
  455. obj, reloc->target_handle,
  456. (int) reloc->offset);
  457. return -EINVAL;
  458. }
  459. /* We can't wait for rendering with pagefaults disabled */
  460. if (pagefault_disabled() && !object_is_idle(obj))
  461. return -EFAULT;
  462. if (use_cpu_reloc(obj))
  463. ret = relocate_entry_cpu(obj, reloc, target_offset);
  464. else if (obj->map_and_fenceable)
  465. ret = relocate_entry_gtt(obj, reloc, target_offset);
  466. else if (static_cpu_has(X86_FEATURE_CLFLUSH))
  467. ret = relocate_entry_clflush(obj, reloc, target_offset);
  468. else {
  469. WARN_ONCE(1, "Impossible case in relocation handling\n");
  470. ret = -ENODEV;
  471. }
  472. if (ret)
  473. return ret;
  474. /* and update the user's relocation entry */
  475. reloc->presumed_offset = target_offset;
  476. return 0;
  477. }
  478. static int
  479. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  480. struct eb_vmas *eb)
  481. {
  482. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  483. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  484. struct drm_i915_gem_relocation_entry __user *user_relocs;
  485. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  486. int remain, ret;
  487. user_relocs = u64_to_user_ptr(entry->relocs_ptr);
  488. remain = entry->relocation_count;
  489. while (remain) {
  490. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  491. int count = remain;
  492. if (count > ARRAY_SIZE(stack_reloc))
  493. count = ARRAY_SIZE(stack_reloc);
  494. remain -= count;
  495. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  496. return -EFAULT;
  497. do {
  498. u64 offset = r->presumed_offset;
  499. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
  500. if (ret)
  501. return ret;
  502. if (r->presumed_offset != offset &&
  503. __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
  504. return -EFAULT;
  505. }
  506. user_relocs++;
  507. r++;
  508. } while (--count);
  509. }
  510. return 0;
  511. #undef N_RELOC
  512. }
  513. static int
  514. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  515. struct eb_vmas *eb,
  516. struct drm_i915_gem_relocation_entry *relocs)
  517. {
  518. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  519. int i, ret;
  520. for (i = 0; i < entry->relocation_count; i++) {
  521. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
  522. if (ret)
  523. return ret;
  524. }
  525. return 0;
  526. }
  527. static int
  528. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  529. {
  530. struct i915_vma *vma;
  531. int ret = 0;
  532. /* This is the fast path and we cannot handle a pagefault whilst
  533. * holding the struct mutex lest the user pass in the relocations
  534. * contained within a mmaped bo. For in such a case we, the page
  535. * fault handler would call i915_gem_fault() and we would try to
  536. * acquire the struct mutex again. Obviously this is bad and so
  537. * lockdep complains vehemently.
  538. */
  539. pagefault_disable();
  540. list_for_each_entry(vma, &eb->vmas, exec_list) {
  541. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  542. if (ret)
  543. break;
  544. }
  545. pagefault_enable();
  546. return ret;
  547. }
  548. static bool only_mappable_for_reloc(unsigned int flags)
  549. {
  550. return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
  551. __EXEC_OBJECT_NEEDS_MAP;
  552. }
  553. static int
  554. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  555. struct intel_engine_cs *engine,
  556. bool *need_reloc)
  557. {
  558. struct drm_i915_gem_object *obj = vma->obj;
  559. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  560. uint64_t flags;
  561. int ret;
  562. flags = PIN_USER;
  563. if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  564. flags |= PIN_GLOBAL;
  565. if (!drm_mm_node_allocated(&vma->node)) {
  566. /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  567. * limit address to the first 4GBs for unflagged objects.
  568. */
  569. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
  570. flags |= PIN_ZONE_4G;
  571. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  572. flags |= PIN_GLOBAL | PIN_MAPPABLE;
  573. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  574. flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  575. if (entry->flags & EXEC_OBJECT_PINNED)
  576. flags |= entry->offset | PIN_OFFSET_FIXED;
  577. if ((flags & PIN_MAPPABLE) == 0)
  578. flags |= PIN_HIGH;
  579. }
  580. ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
  581. if ((ret == -ENOSPC || ret == -E2BIG) &&
  582. only_mappable_for_reloc(entry->flags))
  583. ret = i915_gem_object_pin(obj, vma->vm,
  584. entry->alignment,
  585. flags & ~PIN_MAPPABLE);
  586. if (ret)
  587. return ret;
  588. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  589. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  590. ret = i915_gem_object_get_fence(obj);
  591. if (ret)
  592. return ret;
  593. if (i915_gem_object_pin_fence(obj))
  594. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  595. }
  596. if (entry->offset != vma->node.start) {
  597. entry->offset = vma->node.start;
  598. *need_reloc = true;
  599. }
  600. if (entry->flags & EXEC_OBJECT_WRITE) {
  601. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  602. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  603. }
  604. return 0;
  605. }
  606. static bool
  607. need_reloc_mappable(struct i915_vma *vma)
  608. {
  609. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  610. if (entry->relocation_count == 0)
  611. return false;
  612. if (!vma->is_ggtt)
  613. return false;
  614. /* See also use_cpu_reloc() */
  615. if (HAS_LLC(vma->obj->base.dev))
  616. return false;
  617. if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  618. return false;
  619. return true;
  620. }
  621. static bool
  622. eb_vma_misplaced(struct i915_vma *vma)
  623. {
  624. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  625. struct drm_i915_gem_object *obj = vma->obj;
  626. WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
  627. if (entry->alignment &&
  628. vma->node.start & (entry->alignment - 1))
  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 int
  836. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  837. struct list_head *vmas)
  838. {
  839. const unsigned other_rings = ~intel_engine_flag(req->engine);
  840. struct i915_vma *vma;
  841. uint32_t flush_domains = 0;
  842. bool flush_chipset = false;
  843. int ret;
  844. list_for_each_entry(vma, vmas, exec_list) {
  845. struct drm_i915_gem_object *obj = vma->obj;
  846. if (obj->active & other_rings) {
  847. ret = i915_gem_object_sync(obj, req);
  848. if (ret)
  849. return ret;
  850. }
  851. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  852. flush_chipset |= i915_gem_clflush_object(obj, false);
  853. flush_domains |= obj->base.write_domain;
  854. }
  855. if (flush_chipset)
  856. i915_gem_chipset_flush(req->engine->i915);
  857. if (flush_domains & I915_GEM_DOMAIN_GTT)
  858. wmb();
  859. /* Unconditionally invalidate GPU caches and TLBs. */
  860. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  861. }
  862. static bool
  863. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  864. {
  865. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  866. return false;
  867. /* Kernel clipping was a DRI1 misfeature */
  868. if (exec->num_cliprects || exec->cliprects_ptr)
  869. return false;
  870. if (exec->DR4 == 0xffffffff) {
  871. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  872. exec->DR4 = 0;
  873. }
  874. if (exec->DR1 || exec->DR4)
  875. return false;
  876. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  877. return false;
  878. return true;
  879. }
  880. static int
  881. validate_exec_list(struct drm_device *dev,
  882. struct drm_i915_gem_exec_object2 *exec,
  883. int count)
  884. {
  885. unsigned relocs_total = 0;
  886. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  887. unsigned invalid_flags;
  888. int i;
  889. /* INTERNAL flags must not overlap with external ones */
  890. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  891. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  892. if (USES_FULL_PPGTT(dev))
  893. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  894. for (i = 0; i < count; i++) {
  895. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  896. int length; /* limited by fault_in_pages_readable() */
  897. if (exec[i].flags & invalid_flags)
  898. return -EINVAL;
  899. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  900. * any non-page-aligned or non-canonical addresses.
  901. */
  902. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  903. if (exec[i].offset !=
  904. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  905. return -EINVAL;
  906. /* From drm_mm perspective address space is continuous,
  907. * so from this point we're always using non-canonical
  908. * form internally.
  909. */
  910. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  911. }
  912. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  913. return -EINVAL;
  914. /* First check for malicious input causing overflow in
  915. * the worst case where we need to allocate the entire
  916. * relocation tree as a single array.
  917. */
  918. if (exec[i].relocation_count > relocs_max - relocs_total)
  919. return -EINVAL;
  920. relocs_total += exec[i].relocation_count;
  921. length = exec[i].relocation_count *
  922. sizeof(struct drm_i915_gem_relocation_entry);
  923. /*
  924. * We must check that the entire relocation array is safe
  925. * to read, but since we may need to update the presumed
  926. * offsets during execution, check for full write access.
  927. */
  928. if (!access_ok(VERIFY_WRITE, ptr, length))
  929. return -EFAULT;
  930. if (likely(!i915.prefault_disable)) {
  931. if (fault_in_multipages_readable(ptr, length))
  932. return -EFAULT;
  933. }
  934. }
  935. return 0;
  936. }
  937. static struct i915_gem_context *
  938. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  939. struct intel_engine_cs *engine, const u32 ctx_id)
  940. {
  941. struct i915_gem_context *ctx = NULL;
  942. struct i915_ctx_hang_stats *hs;
  943. if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  944. return ERR_PTR(-EINVAL);
  945. ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
  946. if (IS_ERR(ctx))
  947. return ctx;
  948. hs = &ctx->hang_stats;
  949. if (hs->banned) {
  950. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  951. return ERR_PTR(-EIO);
  952. }
  953. return ctx;
  954. }
  955. void i915_vma_move_to_active(struct i915_vma *vma,
  956. struct drm_i915_gem_request *req,
  957. unsigned int flags)
  958. {
  959. struct drm_i915_gem_object *obj = vma->obj;
  960. const unsigned int idx = req->engine->id;
  961. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  962. obj->dirty = 1; /* be paranoid */
  963. /* Add a reference if we're newly entering the active list.
  964. * The order in which we add operations to the retirement queue is
  965. * vital here: mark_active adds to the start of the callback list,
  966. * such that subsequent callbacks are called first. Therefore we
  967. * add the active reference first and queue for it to be dropped
  968. * *last*.
  969. */
  970. if (obj->active == 0)
  971. i915_gem_object_get(obj);
  972. obj->active |= 1 << idx;
  973. i915_gem_active_set(&obj->last_read[idx], req);
  974. if (flags & EXEC_OBJECT_WRITE) {
  975. i915_gem_active_set(&obj->last_write, req);
  976. intel_fb_obj_invalidate(obj, ORIGIN_CS);
  977. /* update for the implicit flush after a batch */
  978. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  979. }
  980. if (flags & EXEC_OBJECT_NEEDS_FENCE) {
  981. i915_gem_active_set(&obj->last_fence, req);
  982. if (flags & __EXEC_OBJECT_HAS_FENCE) {
  983. struct drm_i915_private *dev_priv = req->i915;
  984. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  985. &dev_priv->mm.fence_list);
  986. }
  987. }
  988. i915_vma_set_active(vma, idx);
  989. i915_gem_active_set(&vma->last_read[idx], req);
  990. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  991. }
  992. static void
  993. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  994. struct drm_i915_gem_request *req)
  995. {
  996. struct i915_vma *vma;
  997. list_for_each_entry(vma, vmas, exec_list) {
  998. struct drm_i915_gem_object *obj = vma->obj;
  999. u32 old_read = obj->base.read_domains;
  1000. u32 old_write = obj->base.write_domain;
  1001. obj->base.write_domain = obj->base.pending_write_domain;
  1002. if (obj->base.write_domain)
  1003. vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
  1004. else
  1005. obj->base.pending_read_domains |= obj->base.read_domains;
  1006. obj->base.read_domains = obj->base.pending_read_domains;
  1007. i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
  1008. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  1009. }
  1010. }
  1011. static void
  1012. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  1013. {
  1014. /* Add a breadcrumb for the completion of the batch buffer */
  1015. __i915_add_request(params->request, params->batch_obj, true);
  1016. }
  1017. static int
  1018. i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1019. {
  1020. struct intel_ring *ring = req->ring;
  1021. int ret, i;
  1022. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1023. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1024. return -EINVAL;
  1025. }
  1026. ret = intel_ring_begin(req, 4 * 3);
  1027. if (ret)
  1028. return ret;
  1029. for (i = 0; i < 4; i++) {
  1030. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1031. intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
  1032. intel_ring_emit(ring, 0);
  1033. }
  1034. intel_ring_advance(ring);
  1035. return 0;
  1036. }
  1037. static struct drm_i915_gem_object*
  1038. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  1039. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1040. struct eb_vmas *eb,
  1041. struct drm_i915_gem_object *batch_obj,
  1042. u32 batch_start_offset,
  1043. u32 batch_len,
  1044. bool is_master)
  1045. {
  1046. struct drm_i915_gem_object *shadow_batch_obj;
  1047. struct i915_vma *vma;
  1048. int ret;
  1049. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  1050. PAGE_ALIGN(batch_len));
  1051. if (IS_ERR(shadow_batch_obj))
  1052. return shadow_batch_obj;
  1053. ret = intel_engine_cmd_parser(engine,
  1054. batch_obj,
  1055. shadow_batch_obj,
  1056. batch_start_offset,
  1057. batch_len,
  1058. is_master);
  1059. if (ret)
  1060. goto err;
  1061. ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  1062. if (ret)
  1063. goto err;
  1064. i915_gem_object_unpin_pages(shadow_batch_obj);
  1065. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1066. vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  1067. vma->exec_entry = shadow_exec_entry;
  1068. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1069. i915_gem_object_get(shadow_batch_obj);
  1070. list_add_tail(&vma->exec_list, &eb->vmas);
  1071. shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1072. return shadow_batch_obj;
  1073. err:
  1074. i915_gem_object_unpin_pages(shadow_batch_obj);
  1075. if (ret == -EACCES) /* unhandled chained batch */
  1076. return batch_obj;
  1077. else
  1078. return ERR_PTR(ret);
  1079. }
  1080. static int
  1081. execbuf_submit(struct i915_execbuffer_params *params,
  1082. struct drm_i915_gem_execbuffer2 *args,
  1083. struct list_head *vmas)
  1084. {
  1085. struct drm_i915_private *dev_priv = params->request->i915;
  1086. u64 exec_start, exec_len;
  1087. int instp_mode;
  1088. u32 instp_mask;
  1089. int ret;
  1090. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1091. if (ret)
  1092. return ret;
  1093. ret = i915_switch_context(params->request);
  1094. if (ret)
  1095. return ret;
  1096. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1097. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1098. switch (instp_mode) {
  1099. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1100. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1101. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1102. if (instp_mode != 0 && params->engine->id != RCS) {
  1103. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1104. return -EINVAL;
  1105. }
  1106. if (instp_mode != dev_priv->relative_constants_mode) {
  1107. if (INTEL_INFO(dev_priv)->gen < 4) {
  1108. DRM_DEBUG("no rel constants on pre-gen4\n");
  1109. return -EINVAL;
  1110. }
  1111. if (INTEL_INFO(dev_priv)->gen > 5 &&
  1112. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1113. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1114. return -EINVAL;
  1115. }
  1116. /* The HW changed the meaning on this bit on gen6 */
  1117. if (INTEL_INFO(dev_priv)->gen >= 6)
  1118. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1119. }
  1120. break;
  1121. default:
  1122. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1123. return -EINVAL;
  1124. }
  1125. if (params->engine->id == RCS &&
  1126. instp_mode != dev_priv->relative_constants_mode) {
  1127. struct intel_ring *ring = params->request->ring;
  1128. ret = intel_ring_begin(params->request, 4);
  1129. if (ret)
  1130. return ret;
  1131. intel_ring_emit(ring, MI_NOOP);
  1132. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1133. intel_ring_emit_reg(ring, INSTPM);
  1134. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1135. intel_ring_advance(ring);
  1136. dev_priv->relative_constants_mode = instp_mode;
  1137. }
  1138. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1139. ret = i915_reset_gen7_sol_offsets(params->request);
  1140. if (ret)
  1141. return ret;
  1142. }
  1143. exec_len = args->batch_len;
  1144. exec_start = params->batch_obj_vm_offset +
  1145. params->args_batch_start_offset;
  1146. if (exec_len == 0)
  1147. exec_len = params->batch_obj->base.size;
  1148. ret = params->engine->emit_bb_start(params->request,
  1149. exec_start, exec_len,
  1150. params->dispatch_flags);
  1151. if (ret)
  1152. return ret;
  1153. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1154. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1155. return 0;
  1156. }
  1157. /**
  1158. * Find one BSD ring to dispatch the corresponding BSD command.
  1159. * The engine index is returned.
  1160. */
  1161. static unsigned int
  1162. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1163. struct drm_file *file)
  1164. {
  1165. struct drm_i915_file_private *file_priv = file->driver_priv;
  1166. /* Check whether the file_priv has already selected one ring. */
  1167. if ((int)file_priv->bsd_engine < 0) {
  1168. /* If not, use the ping-pong mechanism to select one. */
  1169. mutex_lock(&dev_priv->drm.struct_mutex);
  1170. file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
  1171. dev_priv->mm.bsd_engine_dispatch_index ^= 1;
  1172. mutex_unlock(&dev_priv->drm.struct_mutex);
  1173. }
  1174. return file_priv->bsd_engine;
  1175. }
  1176. #define I915_USER_RINGS (4)
  1177. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1178. [I915_EXEC_DEFAULT] = RCS,
  1179. [I915_EXEC_RENDER] = RCS,
  1180. [I915_EXEC_BLT] = BCS,
  1181. [I915_EXEC_BSD] = VCS,
  1182. [I915_EXEC_VEBOX] = VECS
  1183. };
  1184. static struct intel_engine_cs *
  1185. eb_select_engine(struct drm_i915_private *dev_priv,
  1186. struct drm_file *file,
  1187. struct drm_i915_gem_execbuffer2 *args)
  1188. {
  1189. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1190. struct intel_engine_cs *engine;
  1191. if (user_ring_id > I915_USER_RINGS) {
  1192. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1193. return NULL;
  1194. }
  1195. if ((user_ring_id != I915_EXEC_BSD) &&
  1196. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1197. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1198. "bsd dispatch flags: %d\n", (int)(args->flags));
  1199. return NULL;
  1200. }
  1201. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1202. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1203. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1204. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1205. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1206. bsd_idx <= I915_EXEC_BSD_RING2) {
  1207. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1208. bsd_idx--;
  1209. } else {
  1210. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1211. bsd_idx);
  1212. return NULL;
  1213. }
  1214. engine = &dev_priv->engine[_VCS(bsd_idx)];
  1215. } else {
  1216. engine = &dev_priv->engine[user_ring_map[user_ring_id]];
  1217. }
  1218. if (!intel_engine_initialized(engine)) {
  1219. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1220. return NULL;
  1221. }
  1222. return engine;
  1223. }
  1224. static int
  1225. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1226. struct drm_file *file,
  1227. struct drm_i915_gem_execbuffer2 *args,
  1228. struct drm_i915_gem_exec_object2 *exec)
  1229. {
  1230. struct drm_i915_private *dev_priv = to_i915(dev);
  1231. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1232. struct eb_vmas *eb;
  1233. struct drm_i915_gem_object *batch_obj;
  1234. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1235. struct intel_engine_cs *engine;
  1236. struct i915_gem_context *ctx;
  1237. struct i915_address_space *vm;
  1238. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1239. struct i915_execbuffer_params *params = &params_master;
  1240. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1241. u32 dispatch_flags;
  1242. int ret;
  1243. bool need_relocs;
  1244. if (!i915_gem_check_execbuffer(args))
  1245. return -EINVAL;
  1246. ret = validate_exec_list(dev, exec, args->buffer_count);
  1247. if (ret)
  1248. return ret;
  1249. dispatch_flags = 0;
  1250. if (args->flags & I915_EXEC_SECURE) {
  1251. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1252. return -EPERM;
  1253. dispatch_flags |= I915_DISPATCH_SECURE;
  1254. }
  1255. if (args->flags & I915_EXEC_IS_PINNED)
  1256. dispatch_flags |= I915_DISPATCH_PINNED;
  1257. engine = eb_select_engine(dev_priv, file, args);
  1258. if (!engine)
  1259. return -EINVAL;
  1260. if (args->buffer_count < 1) {
  1261. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1262. return -EINVAL;
  1263. }
  1264. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1265. if (!HAS_RESOURCE_STREAMER(dev)) {
  1266. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1267. return -EINVAL;
  1268. }
  1269. if (engine->id != RCS) {
  1270. DRM_DEBUG("RS is not available on %s\n",
  1271. engine->name);
  1272. return -EINVAL;
  1273. }
  1274. dispatch_flags |= I915_DISPATCH_RS;
  1275. }
  1276. /* Take a local wakeref for preparing to dispatch the execbuf as
  1277. * we expect to access the hardware fairly frequently in the
  1278. * process. Upon first dispatch, we acquire another prolonged
  1279. * wakeref that we hold until the GPU has been idle for at least
  1280. * 100ms.
  1281. */
  1282. intel_runtime_pm_get(dev_priv);
  1283. ret = i915_mutex_lock_interruptible(dev);
  1284. if (ret)
  1285. goto pre_mutex_err;
  1286. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1287. if (IS_ERR(ctx)) {
  1288. mutex_unlock(&dev->struct_mutex);
  1289. ret = PTR_ERR(ctx);
  1290. goto pre_mutex_err;
  1291. }
  1292. i915_gem_context_get(ctx);
  1293. if (ctx->ppgtt)
  1294. vm = &ctx->ppgtt->base;
  1295. else
  1296. vm = &ggtt->base;
  1297. memset(&params_master, 0x00, sizeof(params_master));
  1298. eb = eb_create(args);
  1299. if (eb == NULL) {
  1300. i915_gem_context_put(ctx);
  1301. mutex_unlock(&dev->struct_mutex);
  1302. ret = -ENOMEM;
  1303. goto pre_mutex_err;
  1304. }
  1305. /* Look up object handles */
  1306. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1307. if (ret)
  1308. goto err;
  1309. /* take note of the batch buffer before we might reorder the lists */
  1310. batch_obj = eb_get_batch(eb);
  1311. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1312. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1313. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1314. &need_relocs);
  1315. if (ret)
  1316. goto err;
  1317. /* The objects are in their final locations, apply the relocations. */
  1318. if (need_relocs)
  1319. ret = i915_gem_execbuffer_relocate(eb);
  1320. if (ret) {
  1321. if (ret == -EFAULT) {
  1322. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1323. engine,
  1324. eb, exec, ctx);
  1325. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1326. }
  1327. if (ret)
  1328. goto err;
  1329. }
  1330. /* Set the pending read domains for the batch buffer to COMMAND */
  1331. if (batch_obj->base.pending_write_domain) {
  1332. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1333. ret = -EINVAL;
  1334. goto err;
  1335. }
  1336. params->args_batch_start_offset = args->batch_start_offset;
  1337. if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
  1338. struct drm_i915_gem_object *parsed_batch_obj;
  1339. parsed_batch_obj = i915_gem_execbuffer_parse(engine,
  1340. &shadow_exec_entry,
  1341. eb,
  1342. batch_obj,
  1343. args->batch_start_offset,
  1344. args->batch_len,
  1345. drm_is_current_master(file));
  1346. if (IS_ERR(parsed_batch_obj)) {
  1347. ret = PTR_ERR(parsed_batch_obj);
  1348. goto err;
  1349. }
  1350. /*
  1351. * parsed_batch_obj == batch_obj means batch not fully parsed:
  1352. * Accept, but don't promote to secure.
  1353. */
  1354. if (parsed_batch_obj != batch_obj) {
  1355. /*
  1356. * Batch parsed and accepted:
  1357. *
  1358. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1359. * bit from MI_BATCH_BUFFER_START commands issued in
  1360. * the dispatch_execbuffer implementations. We
  1361. * specifically don't want that set on batches the
  1362. * command parser has accepted.
  1363. */
  1364. dispatch_flags |= I915_DISPATCH_SECURE;
  1365. params->args_batch_start_offset = 0;
  1366. batch_obj = parsed_batch_obj;
  1367. }
  1368. }
  1369. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1370. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1371. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1372. * hsw should have this fixed, but bdw mucks it up again. */
  1373. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1374. /*
  1375. * So on first glance it looks freaky that we pin the batch here
  1376. * outside of the reservation loop. But:
  1377. * - The batch is already pinned into the relevant ppgtt, so we
  1378. * already have the backing storage fully allocated.
  1379. * - No other BO uses the global gtt (well contexts, but meh),
  1380. * so we don't really have issues with multiple objects not
  1381. * fitting due to fragmentation.
  1382. * So this is actually safe.
  1383. */
  1384. ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1385. if (ret)
  1386. goto err;
  1387. params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
  1388. } else
  1389. params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
  1390. /* Allocate a request for this batch buffer nice and early. */
  1391. params->request = i915_gem_request_alloc(engine, ctx);
  1392. if (IS_ERR(params->request)) {
  1393. ret = PTR_ERR(params->request);
  1394. goto err_batch_unpin;
  1395. }
  1396. ret = i915_gem_request_add_to_client(params->request, file);
  1397. if (ret)
  1398. goto err_request;
  1399. /*
  1400. * Save assorted stuff away to pass through to *_submission().
  1401. * NB: This data should be 'persistent' and not local as it will
  1402. * kept around beyond the duration of the IOCTL once the GPU
  1403. * scheduler arrives.
  1404. */
  1405. params->dev = dev;
  1406. params->file = file;
  1407. params->engine = engine;
  1408. params->dispatch_flags = dispatch_flags;
  1409. params->batch_obj = batch_obj;
  1410. params->ctx = ctx;
  1411. ret = execbuf_submit(params, args, &eb->vmas);
  1412. err_request:
  1413. i915_gem_execbuffer_retire_commands(params);
  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_gem_object_ggtt_unpin(batch_obj);
  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. }