i915_gem_execbuffer.c 49 KB

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