i915_gem_execbuffer.c 35 KB

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