i915_gem_execbuffer.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. struct eb_vmas {
  35. struct list_head vmas;
  36. int and;
  37. union {
  38. struct i915_vma *lut[0];
  39. struct hlist_head buckets[0];
  40. };
  41. };
  42. static struct eb_vmas *
  43. eb_create(struct drm_i915_gem_execbuffer2 *args, struct i915_address_space *vm)
  44. {
  45. struct eb_vmas *eb = NULL;
  46. if (args->flags & I915_EXEC_HANDLE_LUT) {
  47. int size = args->buffer_count;
  48. size *= sizeof(struct i915_vma *);
  49. size += sizeof(struct eb_vmas);
  50. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  51. }
  52. if (eb == NULL) {
  53. int size = args->buffer_count;
  54. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  55. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  56. while (count > 2*size)
  57. count >>= 1;
  58. eb = kzalloc(count*sizeof(struct hlist_head) +
  59. sizeof(struct eb_vmas),
  60. GFP_TEMPORARY);
  61. if (eb == NULL)
  62. return eb;
  63. eb->and = count - 1;
  64. } else
  65. eb->and = -args->buffer_count;
  66. INIT_LIST_HEAD(&eb->vmas);
  67. return eb;
  68. }
  69. static void
  70. eb_reset(struct eb_vmas *eb)
  71. {
  72. if (eb->and >= 0)
  73. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  74. }
  75. static int
  76. eb_lookup_vmas(struct eb_vmas *eb,
  77. struct drm_i915_gem_exec_object2 *exec,
  78. const struct drm_i915_gem_execbuffer2 *args,
  79. struct i915_address_space *vm,
  80. struct drm_file *file)
  81. {
  82. struct drm_i915_gem_object *obj;
  83. struct list_head objects;
  84. int i, ret = 0;
  85. INIT_LIST_HEAD(&objects);
  86. spin_lock(&file->table_lock);
  87. /* Grab a reference to the object and release the lock so we can lookup
  88. * or create the VMA without using GFP_ATOMIC */
  89. for (i = 0; i < args->buffer_count; i++) {
  90. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  91. if (obj == NULL) {
  92. spin_unlock(&file->table_lock);
  93. DRM_DEBUG("Invalid object handle %d at index %d\n",
  94. exec[i].handle, i);
  95. ret = -ENOENT;
  96. goto out;
  97. }
  98. if (!list_empty(&obj->obj_exec_link)) {
  99. spin_unlock(&file->table_lock);
  100. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  101. obj, exec[i].handle, i);
  102. ret = -EINVAL;
  103. goto out;
  104. }
  105. drm_gem_object_reference(&obj->base);
  106. list_add_tail(&obj->obj_exec_link, &objects);
  107. }
  108. spin_unlock(&file->table_lock);
  109. i = 0;
  110. list_for_each_entry(obj, &objects, obj_exec_link) {
  111. struct i915_vma *vma;
  112. /*
  113. * NOTE: We can leak any vmas created here when something fails
  114. * later on. But that's no issue since vma_unbind can deal with
  115. * vmas which are not actually bound. And since only
  116. * lookup_or_create exists as an interface to get at the vma
  117. * from the (obj, vm) we don't run the risk of creating
  118. * duplicated vmas for the same vm.
  119. */
  120. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  121. if (IS_ERR(vma)) {
  122. DRM_DEBUG("Failed to lookup VMA\n");
  123. ret = PTR_ERR(vma);
  124. goto out;
  125. }
  126. list_add_tail(&vma->exec_list, &eb->vmas);
  127. vma->exec_entry = &exec[i];
  128. if (eb->and < 0) {
  129. eb->lut[i] = vma;
  130. } else {
  131. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  132. vma->exec_handle = handle;
  133. hlist_add_head(&vma->exec_node,
  134. &eb->buckets[handle & eb->and]);
  135. }
  136. ++i;
  137. }
  138. out:
  139. while (!list_empty(&objects)) {
  140. obj = list_first_entry(&objects,
  141. struct drm_i915_gem_object,
  142. obj_exec_link);
  143. list_del_init(&obj->obj_exec_link);
  144. if (ret)
  145. drm_gem_object_unreference(&obj->base);
  146. }
  147. return ret;
  148. }
  149. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  150. {
  151. if (eb->and < 0) {
  152. if (handle >= -eb->and)
  153. return NULL;
  154. return eb->lut[handle];
  155. } else {
  156. struct hlist_head *head;
  157. struct hlist_node *node;
  158. head = &eb->buckets[handle & eb->and];
  159. hlist_for_each(node, head) {
  160. struct i915_vma *vma;
  161. vma = hlist_entry(node, struct i915_vma, exec_node);
  162. if (vma->exec_handle == handle)
  163. return vma;
  164. }
  165. return NULL;
  166. }
  167. }
  168. static void eb_destroy(struct eb_vmas *eb) {
  169. while (!list_empty(&eb->vmas)) {
  170. struct i915_vma *vma;
  171. vma = list_first_entry(&eb->vmas,
  172. struct i915_vma,
  173. exec_list);
  174. list_del_init(&vma->exec_list);
  175. drm_gem_object_unreference(&vma->obj->base);
  176. }
  177. kfree(eb);
  178. }
  179. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  180. {
  181. return (HAS_LLC(obj->base.dev) ||
  182. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  183. !obj->map_and_fenceable ||
  184. obj->cache_level != I915_CACHE_NONE);
  185. }
  186. static int
  187. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  188. struct drm_i915_gem_relocation_entry *reloc)
  189. {
  190. uint32_t page_offset = offset_in_page(reloc->offset);
  191. char *vaddr;
  192. int ret = -EINVAL;
  193. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  194. if (ret)
  195. return ret;
  196. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  197. reloc->offset >> PAGE_SHIFT));
  198. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  199. kunmap_atomic(vaddr);
  200. return 0;
  201. }
  202. static int
  203. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  204. struct drm_i915_gem_relocation_entry *reloc)
  205. {
  206. struct drm_device *dev = obj->base.dev;
  207. struct drm_i915_private *dev_priv = dev->dev_private;
  208. uint32_t __iomem *reloc_entry;
  209. void __iomem *reloc_page;
  210. int ret = -EINVAL;
  211. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  212. if (ret)
  213. return ret;
  214. ret = i915_gem_object_put_fence(obj);
  215. if (ret)
  216. return ret;
  217. /* Map the page containing the relocation we're going to perform. */
  218. reloc->offset += i915_gem_obj_ggtt_offset(obj);
  219. reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  220. reloc->offset & PAGE_MASK);
  221. reloc_entry = (uint32_t __iomem *)
  222. (reloc_page + offset_in_page(reloc->offset));
  223. iowrite32(reloc->delta, reloc_entry);
  224. io_mapping_unmap_atomic(reloc_page);
  225. return 0;
  226. }
  227. static int
  228. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  229. struct eb_vmas *eb,
  230. struct drm_i915_gem_relocation_entry *reloc,
  231. struct i915_address_space *vm)
  232. {
  233. struct drm_device *dev = obj->base.dev;
  234. struct drm_gem_object *target_obj;
  235. struct drm_i915_gem_object *target_i915_obj;
  236. struct i915_vma *target_vma;
  237. uint32_t target_offset;
  238. int ret = -EINVAL;
  239. /* we've already hold a reference to all valid objects */
  240. target_vma = eb_get_vma(eb, reloc->target_handle);
  241. if (unlikely(target_vma == NULL))
  242. return -ENOENT;
  243. target_i915_obj = target_vma->obj;
  244. target_obj = &target_vma->obj->base;
  245. target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
  246. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  247. * pipe_control writes because the gpu doesn't properly redirect them
  248. * through the ppgtt for non_secure batchbuffers. */
  249. if (unlikely(IS_GEN6(dev) &&
  250. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  251. !target_i915_obj->has_global_gtt_mapping)) {
  252. i915_gem_gtt_bind_object(target_i915_obj,
  253. target_i915_obj->cache_level);
  254. }
  255. /* Validate that the target is in a valid r/w GPU domain */
  256. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  257. DRM_DEBUG("reloc with multiple write domains: "
  258. "obj %p target %d offset %d "
  259. "read %08x write %08x",
  260. obj, reloc->target_handle,
  261. (int) reloc->offset,
  262. reloc->read_domains,
  263. reloc->write_domain);
  264. return ret;
  265. }
  266. if (unlikely((reloc->write_domain | reloc->read_domains)
  267. & ~I915_GEM_GPU_DOMAINS)) {
  268. DRM_DEBUG("reloc with read/write non-GPU domains: "
  269. "obj %p target %d offset %d "
  270. "read %08x write %08x",
  271. obj, reloc->target_handle,
  272. (int) reloc->offset,
  273. reloc->read_domains,
  274. reloc->write_domain);
  275. return ret;
  276. }
  277. target_obj->pending_read_domains |= reloc->read_domains;
  278. target_obj->pending_write_domain |= reloc->write_domain;
  279. /* If the relocation already has the right value in it, no
  280. * more work needs to be done.
  281. */
  282. if (target_offset == reloc->presumed_offset)
  283. return 0;
  284. /* Check that the relocation address is valid... */
  285. if (unlikely(reloc->offset > obj->base.size - 4)) {
  286. DRM_DEBUG("Relocation beyond object bounds: "
  287. "obj %p target %d offset %d size %d.\n",
  288. obj, reloc->target_handle,
  289. (int) reloc->offset,
  290. (int) obj->base.size);
  291. return ret;
  292. }
  293. if (unlikely(reloc->offset & 3)) {
  294. DRM_DEBUG("Relocation not 4-byte aligned: "
  295. "obj %p target %d offset %d.\n",
  296. obj, reloc->target_handle,
  297. (int) reloc->offset);
  298. return ret;
  299. }
  300. /* We can't wait for rendering with pagefaults disabled */
  301. if (obj->active && in_atomic())
  302. return -EFAULT;
  303. reloc->delta += target_offset;
  304. if (use_cpu_reloc(obj))
  305. ret = relocate_entry_cpu(obj, reloc);
  306. else
  307. ret = relocate_entry_gtt(obj, reloc);
  308. if (ret)
  309. return ret;
  310. /* and update the user's relocation entry */
  311. reloc->presumed_offset = target_offset;
  312. return 0;
  313. }
  314. static int
  315. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  316. struct eb_vmas *eb)
  317. {
  318. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  319. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  320. struct drm_i915_gem_relocation_entry __user *user_relocs;
  321. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  322. int remain, ret;
  323. user_relocs = to_user_ptr(entry->relocs_ptr);
  324. remain = entry->relocation_count;
  325. while (remain) {
  326. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  327. int count = remain;
  328. if (count > ARRAY_SIZE(stack_reloc))
  329. count = ARRAY_SIZE(stack_reloc);
  330. remain -= count;
  331. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  332. return -EFAULT;
  333. do {
  334. u64 offset = r->presumed_offset;
  335. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
  336. vma->vm);
  337. if (ret)
  338. return ret;
  339. if (r->presumed_offset != offset &&
  340. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  341. &r->presumed_offset,
  342. sizeof(r->presumed_offset))) {
  343. return -EFAULT;
  344. }
  345. user_relocs++;
  346. r++;
  347. } while (--count);
  348. }
  349. return 0;
  350. #undef N_RELOC
  351. }
  352. static int
  353. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  354. struct eb_vmas *eb,
  355. struct drm_i915_gem_relocation_entry *relocs)
  356. {
  357. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  358. int i, ret;
  359. for (i = 0; i < entry->relocation_count; i++) {
  360. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
  361. vma->vm);
  362. if (ret)
  363. return ret;
  364. }
  365. return 0;
  366. }
  367. static int
  368. i915_gem_execbuffer_relocate(struct eb_vmas *eb,
  369. struct i915_address_space *vm)
  370. {
  371. struct i915_vma *vma;
  372. int ret = 0;
  373. /* This is the fast path and we cannot handle a pagefault whilst
  374. * holding the struct mutex lest the user pass in the relocations
  375. * contained within a mmaped bo. For in such a case we, the page
  376. * fault handler would call i915_gem_fault() and we would try to
  377. * acquire the struct mutex again. Obviously this is bad and so
  378. * lockdep complains vehemently.
  379. */
  380. pagefault_disable();
  381. list_for_each_entry(vma, &eb->vmas, exec_list) {
  382. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  383. if (ret)
  384. break;
  385. }
  386. pagefault_enable();
  387. return ret;
  388. }
  389. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  390. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  391. static int
  392. need_reloc_mappable(struct i915_vma *vma)
  393. {
  394. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  395. return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
  396. i915_is_ggtt(vma->vm);
  397. }
  398. static int
  399. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  400. struct intel_ring_buffer *ring,
  401. bool *need_reloc)
  402. {
  403. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  404. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  405. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  406. bool need_fence, need_mappable;
  407. struct drm_i915_gem_object *obj = vma->obj;
  408. int ret;
  409. need_fence =
  410. has_fenced_gpu_access &&
  411. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  412. obj->tiling_mode != I915_TILING_NONE;
  413. need_mappable = need_fence || need_reloc_mappable(vma);
  414. ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
  415. false);
  416. if (ret)
  417. return ret;
  418. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  419. if (has_fenced_gpu_access) {
  420. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  421. ret = i915_gem_object_get_fence(obj);
  422. if (ret)
  423. return ret;
  424. if (i915_gem_object_pin_fence(obj))
  425. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  426. obj->pending_fenced_gpu_access = true;
  427. }
  428. }
  429. /* Ensure ppgtt mapping exists if needed */
  430. if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  431. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  432. obj, obj->cache_level);
  433. obj->has_aliasing_ppgtt_mapping = 1;
  434. }
  435. if (entry->offset != vma->node.start) {
  436. entry->offset = vma->node.start;
  437. *need_reloc = true;
  438. }
  439. if (entry->flags & EXEC_OBJECT_WRITE) {
  440. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  441. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  442. }
  443. if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
  444. !obj->has_global_gtt_mapping)
  445. i915_gem_gtt_bind_object(obj, obj->cache_level);
  446. return 0;
  447. }
  448. static void
  449. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  450. {
  451. struct drm_i915_gem_exec_object2 *entry;
  452. struct drm_i915_gem_object *obj = vma->obj;
  453. if (!drm_mm_node_allocated(&vma->node))
  454. return;
  455. entry = vma->exec_entry;
  456. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  457. i915_gem_object_unpin_fence(obj);
  458. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  459. i915_gem_object_unpin(obj);
  460. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  461. }
  462. static int
  463. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  464. struct list_head *vmas,
  465. bool *need_relocs)
  466. {
  467. struct drm_i915_gem_object *obj;
  468. struct i915_vma *vma;
  469. struct list_head ordered_vmas;
  470. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  471. int retry;
  472. INIT_LIST_HEAD(&ordered_vmas);
  473. while (!list_empty(vmas)) {
  474. struct drm_i915_gem_exec_object2 *entry;
  475. bool need_fence, need_mappable;
  476. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  477. obj = vma->obj;
  478. entry = vma->exec_entry;
  479. need_fence =
  480. has_fenced_gpu_access &&
  481. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  482. obj->tiling_mode != I915_TILING_NONE;
  483. need_mappable = need_fence || need_reloc_mappable(vma);
  484. if (need_mappable)
  485. list_move(&vma->exec_list, &ordered_vmas);
  486. else
  487. list_move_tail(&vma->exec_list, &ordered_vmas);
  488. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  489. obj->base.pending_write_domain = 0;
  490. obj->pending_fenced_gpu_access = false;
  491. }
  492. list_splice(&ordered_vmas, vmas);
  493. /* Attempt to pin all of the buffers into the GTT.
  494. * This is done in 3 phases:
  495. *
  496. * 1a. Unbind all objects that do not match the GTT constraints for
  497. * the execbuffer (fenceable, mappable, alignment etc).
  498. * 1b. Increment pin count for already bound objects.
  499. * 2. Bind new objects.
  500. * 3. Decrement pin count.
  501. *
  502. * This avoid unnecessary unbinding of later objects in order to make
  503. * room for the earlier objects *unless* we need to defragment.
  504. */
  505. retry = 0;
  506. do {
  507. int ret = 0;
  508. /* Unbind any ill-fitting objects or pin. */
  509. list_for_each_entry(vma, vmas, exec_list) {
  510. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  511. bool need_fence, need_mappable;
  512. obj = vma->obj;
  513. if (!drm_mm_node_allocated(&vma->node))
  514. continue;
  515. need_fence =
  516. has_fenced_gpu_access &&
  517. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  518. obj->tiling_mode != I915_TILING_NONE;
  519. need_mappable = need_fence || need_reloc_mappable(vma);
  520. WARN_ON((need_mappable || need_fence) &&
  521. !i915_is_ggtt(vma->vm));
  522. if ((entry->alignment &&
  523. vma->node.start & (entry->alignment - 1)) ||
  524. (need_mappable && !obj->map_and_fenceable))
  525. ret = i915_vma_unbind(vma);
  526. else
  527. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  528. if (ret)
  529. goto err;
  530. }
  531. /* Bind fresh objects */
  532. list_for_each_entry(vma, vmas, exec_list) {
  533. if (drm_mm_node_allocated(&vma->node))
  534. continue;
  535. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  536. if (ret)
  537. goto err;
  538. }
  539. err: /* Decrement pin count for bound objects */
  540. list_for_each_entry(vma, vmas, exec_list)
  541. i915_gem_execbuffer_unreserve_vma(vma);
  542. if (ret != -ENOSPC || retry++)
  543. return ret;
  544. ret = i915_gem_evict_everything(ring->dev);
  545. if (ret)
  546. return ret;
  547. } while (1);
  548. }
  549. static int
  550. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  551. struct drm_i915_gem_execbuffer2 *args,
  552. struct drm_file *file,
  553. struct intel_ring_buffer *ring,
  554. struct eb_vmas *eb,
  555. struct drm_i915_gem_exec_object2 *exec)
  556. {
  557. struct drm_i915_gem_relocation_entry *reloc;
  558. struct i915_address_space *vm;
  559. struct i915_vma *vma;
  560. bool need_relocs;
  561. int *reloc_offset;
  562. int i, total, ret;
  563. int count = args->buffer_count;
  564. if (WARN_ON(list_empty(&eb->vmas)))
  565. return 0;
  566. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  567. /* We may process another execbuffer during the unlock... */
  568. while (!list_empty(&eb->vmas)) {
  569. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  570. list_del_init(&vma->exec_list);
  571. drm_gem_object_unreference(&vma->obj->base);
  572. }
  573. mutex_unlock(&dev->struct_mutex);
  574. total = 0;
  575. for (i = 0; i < count; i++)
  576. total += exec[i].relocation_count;
  577. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  578. reloc = drm_malloc_ab(total, sizeof(*reloc));
  579. if (reloc == NULL || reloc_offset == NULL) {
  580. drm_free_large(reloc);
  581. drm_free_large(reloc_offset);
  582. mutex_lock(&dev->struct_mutex);
  583. return -ENOMEM;
  584. }
  585. total = 0;
  586. for (i = 0; i < count; i++) {
  587. struct drm_i915_gem_relocation_entry __user *user_relocs;
  588. u64 invalid_offset = (u64)-1;
  589. int j;
  590. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  591. if (copy_from_user(reloc+total, user_relocs,
  592. exec[i].relocation_count * sizeof(*reloc))) {
  593. ret = -EFAULT;
  594. mutex_lock(&dev->struct_mutex);
  595. goto err;
  596. }
  597. /* As we do not update the known relocation offsets after
  598. * relocating (due to the complexities in lock handling),
  599. * we need to mark them as invalid now so that we force the
  600. * relocation processing next time. Just in case the target
  601. * object is evicted and then rebound into its old
  602. * presumed_offset before the next execbuffer - if that
  603. * happened we would make the mistake of assuming that the
  604. * relocations were valid.
  605. */
  606. for (j = 0; j < exec[i].relocation_count; j++) {
  607. if (copy_to_user(&user_relocs[j].presumed_offset,
  608. &invalid_offset,
  609. sizeof(invalid_offset))) {
  610. ret = -EFAULT;
  611. mutex_lock(&dev->struct_mutex);
  612. goto err;
  613. }
  614. }
  615. reloc_offset[i] = total;
  616. total += exec[i].relocation_count;
  617. }
  618. ret = i915_mutex_lock_interruptible(dev);
  619. if (ret) {
  620. mutex_lock(&dev->struct_mutex);
  621. goto err;
  622. }
  623. /* reacquire the objects */
  624. eb_reset(eb);
  625. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  626. if (ret)
  627. goto err;
  628. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  629. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  630. if (ret)
  631. goto err;
  632. list_for_each_entry(vma, &eb->vmas, exec_list) {
  633. int offset = vma->exec_entry - exec;
  634. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  635. reloc + reloc_offset[offset]);
  636. if (ret)
  637. goto err;
  638. }
  639. /* Leave the user relocations as are, this is the painfully slow path,
  640. * and we want to avoid the complication of dropping the lock whilst
  641. * having buffers reserved in the aperture and so causing spurious
  642. * ENOSPC for random operations.
  643. */
  644. err:
  645. drm_free_large(reloc);
  646. drm_free_large(reloc_offset);
  647. return ret;
  648. }
  649. static int
  650. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  651. struct list_head *vmas)
  652. {
  653. struct i915_vma *vma;
  654. uint32_t flush_domains = 0;
  655. bool flush_chipset = false;
  656. int ret;
  657. list_for_each_entry(vma, vmas, exec_list) {
  658. struct drm_i915_gem_object *obj = vma->obj;
  659. ret = i915_gem_object_sync(obj, ring);
  660. if (ret)
  661. return ret;
  662. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  663. flush_chipset |= i915_gem_clflush_object(obj, false);
  664. flush_domains |= obj->base.write_domain;
  665. }
  666. if (flush_chipset)
  667. i915_gem_chipset_flush(ring->dev);
  668. if (flush_domains & I915_GEM_DOMAIN_GTT)
  669. wmb();
  670. /* Unconditionally invalidate gpu caches and ensure that we do flush
  671. * any residual writes from the previous batch.
  672. */
  673. return intel_ring_invalidate_all_caches(ring);
  674. }
  675. static bool
  676. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  677. {
  678. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  679. return false;
  680. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  681. }
  682. static int
  683. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  684. int count)
  685. {
  686. int i;
  687. int relocs_total = 0;
  688. int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  689. for (i = 0; i < count; i++) {
  690. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  691. int length; /* limited by fault_in_pages_readable() */
  692. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  693. return -EINVAL;
  694. /* First check for malicious input causing overflow in
  695. * the worst case where we need to allocate the entire
  696. * relocation tree as a single array.
  697. */
  698. if (exec[i].relocation_count > relocs_max - relocs_total)
  699. return -EINVAL;
  700. relocs_total += exec[i].relocation_count;
  701. length = exec[i].relocation_count *
  702. sizeof(struct drm_i915_gem_relocation_entry);
  703. /*
  704. * We must check that the entire relocation array is safe
  705. * to read, but since we may need to update the presumed
  706. * offsets during execution, check for full write access.
  707. */
  708. if (!access_ok(VERIFY_WRITE, ptr, length))
  709. return -EFAULT;
  710. if (likely(!i915_prefault_disable)) {
  711. if (fault_in_multipages_readable(ptr, length))
  712. return -EFAULT;
  713. }
  714. }
  715. return 0;
  716. }
  717. static void
  718. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  719. struct intel_ring_buffer *ring)
  720. {
  721. struct i915_vma *vma;
  722. list_for_each_entry(vma, vmas, exec_list) {
  723. struct drm_i915_gem_object *obj = vma->obj;
  724. u32 old_read = obj->base.read_domains;
  725. u32 old_write = obj->base.write_domain;
  726. obj->base.write_domain = obj->base.pending_write_domain;
  727. if (obj->base.write_domain == 0)
  728. obj->base.pending_read_domains |= obj->base.read_domains;
  729. obj->base.read_domains = obj->base.pending_read_domains;
  730. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  731. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  732. i915_gem_object_move_to_active(obj, ring);
  733. if (obj->base.write_domain) {
  734. obj->dirty = 1;
  735. obj->last_write_seqno = intel_ring_get_seqno(ring);
  736. if (obj->pin_count) /* check for potential scanout */
  737. intel_mark_fb_busy(obj, ring);
  738. }
  739. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  740. }
  741. }
  742. static void
  743. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  744. struct drm_file *file,
  745. struct intel_ring_buffer *ring,
  746. struct drm_i915_gem_object *obj)
  747. {
  748. /* Unconditionally force add_request to emit a full flush. */
  749. ring->gpu_caches_dirty = true;
  750. /* Add a breadcrumb for the completion of the batch buffer */
  751. (void)__i915_add_request(ring, file, obj, NULL);
  752. }
  753. static int
  754. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  755. struct intel_ring_buffer *ring)
  756. {
  757. drm_i915_private_t *dev_priv = dev->dev_private;
  758. int ret, i;
  759. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  760. return 0;
  761. ret = intel_ring_begin(ring, 4 * 3);
  762. if (ret)
  763. return ret;
  764. for (i = 0; i < 4; i++) {
  765. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  766. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  767. intel_ring_emit(ring, 0);
  768. }
  769. intel_ring_advance(ring);
  770. return 0;
  771. }
  772. static int
  773. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  774. struct drm_file *file,
  775. struct drm_i915_gem_execbuffer2 *args,
  776. struct drm_i915_gem_exec_object2 *exec,
  777. struct i915_address_space *vm)
  778. {
  779. drm_i915_private_t *dev_priv = dev->dev_private;
  780. struct eb_vmas *eb;
  781. struct drm_i915_gem_object *batch_obj;
  782. struct drm_clip_rect *cliprects = NULL;
  783. struct intel_ring_buffer *ring;
  784. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  785. u32 exec_start, exec_len;
  786. u32 mask, flags;
  787. int ret, mode, i;
  788. bool need_relocs;
  789. if (!i915_gem_check_execbuffer(args))
  790. return -EINVAL;
  791. ret = validate_exec_list(exec, args->buffer_count);
  792. if (ret)
  793. return ret;
  794. flags = 0;
  795. if (args->flags & I915_EXEC_SECURE) {
  796. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  797. return -EPERM;
  798. flags |= I915_DISPATCH_SECURE;
  799. }
  800. if (args->flags & I915_EXEC_IS_PINNED)
  801. flags |= I915_DISPATCH_PINNED;
  802. switch (args->flags & I915_EXEC_RING_MASK) {
  803. case I915_EXEC_DEFAULT:
  804. case I915_EXEC_RENDER:
  805. ring = &dev_priv->ring[RCS];
  806. break;
  807. case I915_EXEC_BSD:
  808. ring = &dev_priv->ring[VCS];
  809. if (ctx_id != DEFAULT_CONTEXT_ID) {
  810. DRM_DEBUG("Ring %s doesn't support contexts\n",
  811. ring->name);
  812. return -EPERM;
  813. }
  814. break;
  815. case I915_EXEC_BLT:
  816. ring = &dev_priv->ring[BCS];
  817. if (ctx_id != DEFAULT_CONTEXT_ID) {
  818. DRM_DEBUG("Ring %s doesn't support contexts\n",
  819. ring->name);
  820. return -EPERM;
  821. }
  822. break;
  823. case I915_EXEC_VEBOX:
  824. ring = &dev_priv->ring[VECS];
  825. if (ctx_id != DEFAULT_CONTEXT_ID) {
  826. DRM_DEBUG("Ring %s doesn't support contexts\n",
  827. ring->name);
  828. return -EPERM;
  829. }
  830. break;
  831. default:
  832. DRM_DEBUG("execbuf with unknown ring: %d\n",
  833. (int)(args->flags & I915_EXEC_RING_MASK));
  834. return -EINVAL;
  835. }
  836. if (!intel_ring_initialized(ring)) {
  837. DRM_DEBUG("execbuf with invalid ring: %d\n",
  838. (int)(args->flags & I915_EXEC_RING_MASK));
  839. return -EINVAL;
  840. }
  841. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  842. mask = I915_EXEC_CONSTANTS_MASK;
  843. switch (mode) {
  844. case I915_EXEC_CONSTANTS_REL_GENERAL:
  845. case I915_EXEC_CONSTANTS_ABSOLUTE:
  846. case I915_EXEC_CONSTANTS_REL_SURFACE:
  847. if (ring == &dev_priv->ring[RCS] &&
  848. mode != dev_priv->relative_constants_mode) {
  849. if (INTEL_INFO(dev)->gen < 4)
  850. return -EINVAL;
  851. if (INTEL_INFO(dev)->gen > 5 &&
  852. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  853. return -EINVAL;
  854. /* The HW changed the meaning on this bit on gen6 */
  855. if (INTEL_INFO(dev)->gen >= 6)
  856. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  857. }
  858. break;
  859. default:
  860. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  861. return -EINVAL;
  862. }
  863. if (args->buffer_count < 1) {
  864. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  865. return -EINVAL;
  866. }
  867. if (args->num_cliprects != 0) {
  868. if (ring != &dev_priv->ring[RCS]) {
  869. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  870. return -EINVAL;
  871. }
  872. if (INTEL_INFO(dev)->gen >= 5) {
  873. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  874. return -EINVAL;
  875. }
  876. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  877. DRM_DEBUG("execbuf with %u cliprects\n",
  878. args->num_cliprects);
  879. return -EINVAL;
  880. }
  881. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  882. GFP_KERNEL);
  883. if (cliprects == NULL) {
  884. ret = -ENOMEM;
  885. goto pre_mutex_err;
  886. }
  887. if (copy_from_user(cliprects,
  888. to_user_ptr(args->cliprects_ptr),
  889. sizeof(*cliprects)*args->num_cliprects)) {
  890. ret = -EFAULT;
  891. goto pre_mutex_err;
  892. }
  893. }
  894. ret = i915_mutex_lock_interruptible(dev);
  895. if (ret)
  896. goto pre_mutex_err;
  897. if (dev_priv->ums.mm_suspended) {
  898. mutex_unlock(&dev->struct_mutex);
  899. ret = -EBUSY;
  900. goto pre_mutex_err;
  901. }
  902. eb = eb_create(args, vm);
  903. if (eb == NULL) {
  904. mutex_unlock(&dev->struct_mutex);
  905. ret = -ENOMEM;
  906. goto pre_mutex_err;
  907. }
  908. /* Look up object handles */
  909. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  910. if (ret)
  911. goto err;
  912. /* take note of the batch buffer before we might reorder the lists */
  913. batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
  914. /* Move the objects en-masse into the GTT, evicting if necessary. */
  915. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  916. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  917. if (ret)
  918. goto err;
  919. /* The objects are in their final locations, apply the relocations. */
  920. if (need_relocs)
  921. ret = i915_gem_execbuffer_relocate(eb, vm);
  922. if (ret) {
  923. if (ret == -EFAULT) {
  924. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  925. eb, exec);
  926. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  927. }
  928. if (ret)
  929. goto err;
  930. }
  931. /* Set the pending read domains for the batch buffer to COMMAND */
  932. if (batch_obj->base.pending_write_domain) {
  933. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  934. ret = -EINVAL;
  935. goto err;
  936. }
  937. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  938. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  939. * batch" bit. Hence we need to pin secure batches into the global gtt.
  940. * hsw should have this fixed, but let's be paranoid and do it
  941. * unconditionally for now. */
  942. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  943. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  944. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
  945. if (ret)
  946. goto err;
  947. ret = i915_switch_context(ring, file, ctx_id);
  948. if (ret)
  949. goto err;
  950. if (ring == &dev_priv->ring[RCS] &&
  951. mode != dev_priv->relative_constants_mode) {
  952. ret = intel_ring_begin(ring, 4);
  953. if (ret)
  954. goto err;
  955. intel_ring_emit(ring, MI_NOOP);
  956. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  957. intel_ring_emit(ring, INSTPM);
  958. intel_ring_emit(ring, mask << 16 | mode);
  959. intel_ring_advance(ring);
  960. dev_priv->relative_constants_mode = mode;
  961. }
  962. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  963. ret = i915_reset_gen7_sol_offsets(dev, ring);
  964. if (ret)
  965. goto err;
  966. }
  967. exec_start = i915_gem_obj_offset(batch_obj, vm) +
  968. args->batch_start_offset;
  969. exec_len = args->batch_len;
  970. if (cliprects) {
  971. for (i = 0; i < args->num_cliprects; i++) {
  972. ret = i915_emit_box(dev, &cliprects[i],
  973. args->DR1, args->DR4);
  974. if (ret)
  975. goto err;
  976. ret = ring->dispatch_execbuffer(ring,
  977. exec_start, exec_len,
  978. flags);
  979. if (ret)
  980. goto err;
  981. }
  982. } else {
  983. ret = ring->dispatch_execbuffer(ring,
  984. exec_start, exec_len,
  985. flags);
  986. if (ret)
  987. goto err;
  988. }
  989. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  990. i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
  991. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  992. err:
  993. eb_destroy(eb);
  994. mutex_unlock(&dev->struct_mutex);
  995. pre_mutex_err:
  996. kfree(cliprects);
  997. return ret;
  998. }
  999. /*
  1000. * Legacy execbuffer just creates an exec2 list from the original exec object
  1001. * list array and passes it to the real function.
  1002. */
  1003. int
  1004. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1005. struct drm_file *file)
  1006. {
  1007. struct drm_i915_private *dev_priv = dev->dev_private;
  1008. struct drm_i915_gem_execbuffer *args = data;
  1009. struct drm_i915_gem_execbuffer2 exec2;
  1010. struct drm_i915_gem_exec_object *exec_list = NULL;
  1011. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1012. int ret, i;
  1013. if (args->buffer_count < 1) {
  1014. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1015. return -EINVAL;
  1016. }
  1017. /* Copy in the exec list from userland */
  1018. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1019. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1020. if (exec_list == NULL || exec2_list == NULL) {
  1021. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1022. args->buffer_count);
  1023. drm_free_large(exec_list);
  1024. drm_free_large(exec2_list);
  1025. return -ENOMEM;
  1026. }
  1027. ret = copy_from_user(exec_list,
  1028. to_user_ptr(args->buffers_ptr),
  1029. sizeof(*exec_list) * args->buffer_count);
  1030. if (ret != 0) {
  1031. DRM_DEBUG("copy %d exec entries failed %d\n",
  1032. args->buffer_count, ret);
  1033. drm_free_large(exec_list);
  1034. drm_free_large(exec2_list);
  1035. return -EFAULT;
  1036. }
  1037. for (i = 0; i < args->buffer_count; i++) {
  1038. exec2_list[i].handle = exec_list[i].handle;
  1039. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1040. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1041. exec2_list[i].alignment = exec_list[i].alignment;
  1042. exec2_list[i].offset = exec_list[i].offset;
  1043. if (INTEL_INFO(dev)->gen < 4)
  1044. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1045. else
  1046. exec2_list[i].flags = 0;
  1047. }
  1048. exec2.buffers_ptr = args->buffers_ptr;
  1049. exec2.buffer_count = args->buffer_count;
  1050. exec2.batch_start_offset = args->batch_start_offset;
  1051. exec2.batch_len = args->batch_len;
  1052. exec2.DR1 = args->DR1;
  1053. exec2.DR4 = args->DR4;
  1054. exec2.num_cliprects = args->num_cliprects;
  1055. exec2.cliprects_ptr = args->cliprects_ptr;
  1056. exec2.flags = I915_EXEC_RENDER;
  1057. i915_execbuffer2_set_context_id(exec2, 0);
  1058. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
  1059. &dev_priv->gtt.base);
  1060. if (!ret) {
  1061. /* Copy the new buffer offsets back to the user's exec list. */
  1062. for (i = 0; i < args->buffer_count; i++)
  1063. exec_list[i].offset = exec2_list[i].offset;
  1064. /* ... and back out to userspace */
  1065. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1066. exec_list,
  1067. sizeof(*exec_list) * args->buffer_count);
  1068. if (ret) {
  1069. ret = -EFAULT;
  1070. DRM_DEBUG("failed to copy %d exec entries "
  1071. "back to user (%d)\n",
  1072. args->buffer_count, ret);
  1073. }
  1074. }
  1075. drm_free_large(exec_list);
  1076. drm_free_large(exec2_list);
  1077. return ret;
  1078. }
  1079. int
  1080. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1081. struct drm_file *file)
  1082. {
  1083. struct drm_i915_private *dev_priv = dev->dev_private;
  1084. struct drm_i915_gem_execbuffer2 *args = data;
  1085. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1086. int ret;
  1087. if (args->buffer_count < 1 ||
  1088. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1089. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1090. return -EINVAL;
  1091. }
  1092. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1093. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1094. if (exec2_list == NULL)
  1095. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1096. args->buffer_count);
  1097. if (exec2_list == NULL) {
  1098. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1099. args->buffer_count);
  1100. return -ENOMEM;
  1101. }
  1102. ret = copy_from_user(exec2_list,
  1103. to_user_ptr(args->buffers_ptr),
  1104. sizeof(*exec2_list) * args->buffer_count);
  1105. if (ret != 0) {
  1106. DRM_DEBUG("copy %d exec entries failed %d\n",
  1107. args->buffer_count, ret);
  1108. drm_free_large(exec2_list);
  1109. return -EFAULT;
  1110. }
  1111. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
  1112. &dev_priv->gtt.base);
  1113. if (!ret) {
  1114. /* Copy the new buffer offsets back to the user's exec list. */
  1115. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1116. exec2_list,
  1117. sizeof(*exec2_list) * args->buffer_count);
  1118. if (ret) {
  1119. ret = -EFAULT;
  1120. DRM_DEBUG("failed to copy %d exec entries "
  1121. "back to user (%d)\n",
  1122. args->buffer_count, ret);
  1123. }
  1124. }
  1125. drm_free_large(exec2_list);
  1126. return ret;
  1127. }