i915_gem_execbuffer.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 i915_address_space *vm;
  470. struct list_head ordered_vmas;
  471. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  472. int retry;
  473. if (list_empty(vmas))
  474. return 0;
  475. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  476. INIT_LIST_HEAD(&ordered_vmas);
  477. while (!list_empty(vmas)) {
  478. struct drm_i915_gem_exec_object2 *entry;
  479. bool need_fence, need_mappable;
  480. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  481. obj = vma->obj;
  482. entry = vma->exec_entry;
  483. need_fence =
  484. has_fenced_gpu_access &&
  485. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  486. obj->tiling_mode != I915_TILING_NONE;
  487. need_mappable = need_fence || need_reloc_mappable(vma);
  488. if (need_mappable)
  489. list_move(&vma->exec_list, &ordered_vmas);
  490. else
  491. list_move_tail(&vma->exec_list, &ordered_vmas);
  492. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  493. obj->base.pending_write_domain = 0;
  494. obj->pending_fenced_gpu_access = false;
  495. }
  496. list_splice(&ordered_vmas, vmas);
  497. /* Attempt to pin all of the buffers into the GTT.
  498. * This is done in 3 phases:
  499. *
  500. * 1a. Unbind all objects that do not match the GTT constraints for
  501. * the execbuffer (fenceable, mappable, alignment etc).
  502. * 1b. Increment pin count for already bound objects.
  503. * 2. Bind new objects.
  504. * 3. Decrement pin count.
  505. *
  506. * This avoid unnecessary unbinding of later objects in order to make
  507. * room for the earlier objects *unless* we need to defragment.
  508. */
  509. retry = 0;
  510. do {
  511. int ret = 0;
  512. /* Unbind any ill-fitting objects or pin. */
  513. list_for_each_entry(vma, vmas, exec_list) {
  514. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  515. bool need_fence, need_mappable;
  516. obj = vma->obj;
  517. if (!drm_mm_node_allocated(&vma->node))
  518. continue;
  519. need_fence =
  520. has_fenced_gpu_access &&
  521. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  522. obj->tiling_mode != I915_TILING_NONE;
  523. need_mappable = need_fence || need_reloc_mappable(vma);
  524. WARN_ON((need_mappable || need_fence) &&
  525. !i915_is_ggtt(vma->vm));
  526. if ((entry->alignment &&
  527. vma->node.start & (entry->alignment - 1)) ||
  528. (need_mappable && !obj->map_and_fenceable))
  529. ret = i915_vma_unbind(vma);
  530. else
  531. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  532. if (ret)
  533. goto err;
  534. }
  535. /* Bind fresh objects */
  536. list_for_each_entry(vma, vmas, exec_list) {
  537. if (drm_mm_node_allocated(&vma->node))
  538. continue;
  539. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  540. if (ret)
  541. goto err;
  542. }
  543. err: /* Decrement pin count for bound objects */
  544. list_for_each_entry(vma, vmas, exec_list)
  545. i915_gem_execbuffer_unreserve_vma(vma);
  546. if (ret != -ENOSPC || retry++)
  547. return ret;
  548. ret = i915_gem_evict_vm(vm, true);
  549. if (ret)
  550. return ret;
  551. } while (1);
  552. }
  553. static int
  554. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  555. struct drm_i915_gem_execbuffer2 *args,
  556. struct drm_file *file,
  557. struct intel_ring_buffer *ring,
  558. struct eb_vmas *eb,
  559. struct drm_i915_gem_exec_object2 *exec)
  560. {
  561. struct drm_i915_gem_relocation_entry *reloc;
  562. struct i915_address_space *vm;
  563. struct i915_vma *vma;
  564. bool need_relocs;
  565. int *reloc_offset;
  566. int i, total, ret;
  567. int count = args->buffer_count;
  568. if (WARN_ON(list_empty(&eb->vmas)))
  569. return 0;
  570. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  571. /* We may process another execbuffer during the unlock... */
  572. while (!list_empty(&eb->vmas)) {
  573. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  574. list_del_init(&vma->exec_list);
  575. drm_gem_object_unreference(&vma->obj->base);
  576. }
  577. mutex_unlock(&dev->struct_mutex);
  578. total = 0;
  579. for (i = 0; i < count; i++)
  580. total += exec[i].relocation_count;
  581. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  582. reloc = drm_malloc_ab(total, sizeof(*reloc));
  583. if (reloc == NULL || reloc_offset == NULL) {
  584. drm_free_large(reloc);
  585. drm_free_large(reloc_offset);
  586. mutex_lock(&dev->struct_mutex);
  587. return -ENOMEM;
  588. }
  589. total = 0;
  590. for (i = 0; i < count; i++) {
  591. struct drm_i915_gem_relocation_entry __user *user_relocs;
  592. u64 invalid_offset = (u64)-1;
  593. int j;
  594. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  595. if (copy_from_user(reloc+total, user_relocs,
  596. exec[i].relocation_count * sizeof(*reloc))) {
  597. ret = -EFAULT;
  598. mutex_lock(&dev->struct_mutex);
  599. goto err;
  600. }
  601. /* As we do not update the known relocation offsets after
  602. * relocating (due to the complexities in lock handling),
  603. * we need to mark them as invalid now so that we force the
  604. * relocation processing next time. Just in case the target
  605. * object is evicted and then rebound into its old
  606. * presumed_offset before the next execbuffer - if that
  607. * happened we would make the mistake of assuming that the
  608. * relocations were valid.
  609. */
  610. for (j = 0; j < exec[i].relocation_count; j++) {
  611. if (copy_to_user(&user_relocs[j].presumed_offset,
  612. &invalid_offset,
  613. sizeof(invalid_offset))) {
  614. ret = -EFAULT;
  615. mutex_lock(&dev->struct_mutex);
  616. goto err;
  617. }
  618. }
  619. reloc_offset[i] = total;
  620. total += exec[i].relocation_count;
  621. }
  622. ret = i915_mutex_lock_interruptible(dev);
  623. if (ret) {
  624. mutex_lock(&dev->struct_mutex);
  625. goto err;
  626. }
  627. /* reacquire the objects */
  628. eb_reset(eb);
  629. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  630. if (ret)
  631. goto err;
  632. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  633. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  634. if (ret)
  635. goto err;
  636. list_for_each_entry(vma, &eb->vmas, exec_list) {
  637. int offset = vma->exec_entry - exec;
  638. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  639. reloc + reloc_offset[offset]);
  640. if (ret)
  641. goto err;
  642. }
  643. /* Leave the user relocations as are, this is the painfully slow path,
  644. * and we want to avoid the complication of dropping the lock whilst
  645. * having buffers reserved in the aperture and so causing spurious
  646. * ENOSPC for random operations.
  647. */
  648. err:
  649. drm_free_large(reloc);
  650. drm_free_large(reloc_offset);
  651. return ret;
  652. }
  653. static int
  654. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  655. struct list_head *vmas)
  656. {
  657. struct i915_vma *vma;
  658. uint32_t flush_domains = 0;
  659. bool flush_chipset = false;
  660. int ret;
  661. list_for_each_entry(vma, vmas, exec_list) {
  662. struct drm_i915_gem_object *obj = vma->obj;
  663. ret = i915_gem_object_sync(obj, ring);
  664. if (ret)
  665. return ret;
  666. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  667. flush_chipset |= i915_gem_clflush_object(obj, false);
  668. flush_domains |= obj->base.write_domain;
  669. }
  670. if (flush_chipset)
  671. i915_gem_chipset_flush(ring->dev);
  672. if (flush_domains & I915_GEM_DOMAIN_GTT)
  673. wmb();
  674. /* Unconditionally invalidate gpu caches and ensure that we do flush
  675. * any residual writes from the previous batch.
  676. */
  677. return intel_ring_invalidate_all_caches(ring);
  678. }
  679. static bool
  680. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  681. {
  682. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  683. return false;
  684. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  685. }
  686. static int
  687. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  688. int count)
  689. {
  690. int i;
  691. int relocs_total = 0;
  692. int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  693. for (i = 0; i < count; i++) {
  694. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  695. int length; /* limited by fault_in_pages_readable() */
  696. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  697. return -EINVAL;
  698. /* First check for malicious input causing overflow in
  699. * the worst case where we need to allocate the entire
  700. * relocation tree as a single array.
  701. */
  702. if (exec[i].relocation_count > relocs_max - relocs_total)
  703. return -EINVAL;
  704. relocs_total += exec[i].relocation_count;
  705. length = exec[i].relocation_count *
  706. sizeof(struct drm_i915_gem_relocation_entry);
  707. /*
  708. * We must check that the entire relocation array is safe
  709. * to read, but since we may need to update the presumed
  710. * offsets during execution, check for full write access.
  711. */
  712. if (!access_ok(VERIFY_WRITE, ptr, length))
  713. return -EFAULT;
  714. if (likely(!i915_prefault_disable)) {
  715. if (fault_in_multipages_readable(ptr, length))
  716. return -EFAULT;
  717. }
  718. }
  719. return 0;
  720. }
  721. static void
  722. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  723. struct intel_ring_buffer *ring)
  724. {
  725. struct i915_vma *vma;
  726. list_for_each_entry(vma, vmas, exec_list) {
  727. struct drm_i915_gem_object *obj = vma->obj;
  728. u32 old_read = obj->base.read_domains;
  729. u32 old_write = obj->base.write_domain;
  730. obj->base.write_domain = obj->base.pending_write_domain;
  731. if (obj->base.write_domain == 0)
  732. obj->base.pending_read_domains |= obj->base.read_domains;
  733. obj->base.read_domains = obj->base.pending_read_domains;
  734. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  735. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  736. i915_gem_object_move_to_active(obj, ring);
  737. if (obj->base.write_domain) {
  738. obj->dirty = 1;
  739. obj->last_write_seqno = intel_ring_get_seqno(ring);
  740. if (obj->pin_count) /* check for potential scanout */
  741. intel_mark_fb_busy(obj, ring);
  742. }
  743. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  744. }
  745. }
  746. static void
  747. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  748. struct drm_file *file,
  749. struct intel_ring_buffer *ring,
  750. struct drm_i915_gem_object *obj)
  751. {
  752. /* Unconditionally force add_request to emit a full flush. */
  753. ring->gpu_caches_dirty = true;
  754. /* Add a breadcrumb for the completion of the batch buffer */
  755. (void)__i915_add_request(ring, file, obj, NULL);
  756. }
  757. static int
  758. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  759. struct intel_ring_buffer *ring)
  760. {
  761. drm_i915_private_t *dev_priv = dev->dev_private;
  762. int ret, i;
  763. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  764. return 0;
  765. ret = intel_ring_begin(ring, 4 * 3);
  766. if (ret)
  767. return ret;
  768. for (i = 0; i < 4; i++) {
  769. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  770. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  771. intel_ring_emit(ring, 0);
  772. }
  773. intel_ring_advance(ring);
  774. return 0;
  775. }
  776. static int
  777. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  778. struct drm_file *file,
  779. struct drm_i915_gem_execbuffer2 *args,
  780. struct drm_i915_gem_exec_object2 *exec,
  781. struct i915_address_space *vm)
  782. {
  783. drm_i915_private_t *dev_priv = dev->dev_private;
  784. struct eb_vmas *eb;
  785. struct drm_i915_gem_object *batch_obj;
  786. struct drm_clip_rect *cliprects = NULL;
  787. struct intel_ring_buffer *ring;
  788. struct i915_ctx_hang_stats *hs;
  789. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  790. u32 exec_start, exec_len;
  791. u32 mask, flags;
  792. int ret, mode, i;
  793. bool need_relocs;
  794. if (!i915_gem_check_execbuffer(args))
  795. return -EINVAL;
  796. ret = validate_exec_list(exec, args->buffer_count);
  797. if (ret)
  798. return ret;
  799. flags = 0;
  800. if (args->flags & I915_EXEC_SECURE) {
  801. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  802. return -EPERM;
  803. flags |= I915_DISPATCH_SECURE;
  804. }
  805. if (args->flags & I915_EXEC_IS_PINNED)
  806. flags |= I915_DISPATCH_PINNED;
  807. switch (args->flags & I915_EXEC_RING_MASK) {
  808. case I915_EXEC_DEFAULT:
  809. case I915_EXEC_RENDER:
  810. ring = &dev_priv->ring[RCS];
  811. break;
  812. case I915_EXEC_BSD:
  813. ring = &dev_priv->ring[VCS];
  814. if (ctx_id != DEFAULT_CONTEXT_ID) {
  815. DRM_DEBUG("Ring %s doesn't support contexts\n",
  816. ring->name);
  817. return -EPERM;
  818. }
  819. break;
  820. case I915_EXEC_BLT:
  821. ring = &dev_priv->ring[BCS];
  822. if (ctx_id != DEFAULT_CONTEXT_ID) {
  823. DRM_DEBUG("Ring %s doesn't support contexts\n",
  824. ring->name);
  825. return -EPERM;
  826. }
  827. break;
  828. case I915_EXEC_VEBOX:
  829. ring = &dev_priv->ring[VECS];
  830. if (ctx_id != DEFAULT_CONTEXT_ID) {
  831. DRM_DEBUG("Ring %s doesn't support contexts\n",
  832. ring->name);
  833. return -EPERM;
  834. }
  835. break;
  836. default:
  837. DRM_DEBUG("execbuf with unknown ring: %d\n",
  838. (int)(args->flags & I915_EXEC_RING_MASK));
  839. return -EINVAL;
  840. }
  841. if (!intel_ring_initialized(ring)) {
  842. DRM_DEBUG("execbuf with invalid ring: %d\n",
  843. (int)(args->flags & I915_EXEC_RING_MASK));
  844. return -EINVAL;
  845. }
  846. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  847. mask = I915_EXEC_CONSTANTS_MASK;
  848. switch (mode) {
  849. case I915_EXEC_CONSTANTS_REL_GENERAL:
  850. case I915_EXEC_CONSTANTS_ABSOLUTE:
  851. case I915_EXEC_CONSTANTS_REL_SURFACE:
  852. if (ring == &dev_priv->ring[RCS] &&
  853. mode != dev_priv->relative_constants_mode) {
  854. if (INTEL_INFO(dev)->gen < 4)
  855. return -EINVAL;
  856. if (INTEL_INFO(dev)->gen > 5 &&
  857. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  858. return -EINVAL;
  859. /* The HW changed the meaning on this bit on gen6 */
  860. if (INTEL_INFO(dev)->gen >= 6)
  861. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  862. }
  863. break;
  864. default:
  865. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  866. return -EINVAL;
  867. }
  868. if (args->buffer_count < 1) {
  869. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  870. return -EINVAL;
  871. }
  872. if (args->num_cliprects != 0) {
  873. if (ring != &dev_priv->ring[RCS]) {
  874. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  875. return -EINVAL;
  876. }
  877. if (INTEL_INFO(dev)->gen >= 5) {
  878. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  879. return -EINVAL;
  880. }
  881. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  882. DRM_DEBUG("execbuf with %u cliprects\n",
  883. args->num_cliprects);
  884. return -EINVAL;
  885. }
  886. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  887. GFP_KERNEL);
  888. if (cliprects == NULL) {
  889. ret = -ENOMEM;
  890. goto pre_mutex_err;
  891. }
  892. if (copy_from_user(cliprects,
  893. to_user_ptr(args->cliprects_ptr),
  894. sizeof(*cliprects)*args->num_cliprects)) {
  895. ret = -EFAULT;
  896. goto pre_mutex_err;
  897. }
  898. }
  899. ret = i915_mutex_lock_interruptible(dev);
  900. if (ret)
  901. goto pre_mutex_err;
  902. if (dev_priv->ums.mm_suspended) {
  903. mutex_unlock(&dev->struct_mutex);
  904. ret = -EBUSY;
  905. goto pre_mutex_err;
  906. }
  907. eb = eb_create(args, vm);
  908. if (eb == NULL) {
  909. mutex_unlock(&dev->struct_mutex);
  910. ret = -ENOMEM;
  911. goto pre_mutex_err;
  912. }
  913. /* Look up object handles */
  914. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  915. if (ret)
  916. goto err;
  917. /* take note of the batch buffer before we might reorder the lists */
  918. batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
  919. /* Move the objects en-masse into the GTT, evicting if necessary. */
  920. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  921. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  922. if (ret)
  923. goto err;
  924. /* The objects are in their final locations, apply the relocations. */
  925. if (need_relocs)
  926. ret = i915_gem_execbuffer_relocate(eb, vm);
  927. if (ret) {
  928. if (ret == -EFAULT) {
  929. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  930. eb, exec);
  931. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  932. }
  933. if (ret)
  934. goto err;
  935. }
  936. /* Set the pending read domains for the batch buffer to COMMAND */
  937. if (batch_obj->base.pending_write_domain) {
  938. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  939. ret = -EINVAL;
  940. goto err;
  941. }
  942. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  943. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  944. * batch" bit. Hence we need to pin secure batches into the global gtt.
  945. * hsw should have this fixed, but let's be paranoid and do it
  946. * unconditionally for now. */
  947. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  948. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  949. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
  950. if (ret)
  951. goto err;
  952. hs = i915_gem_context_get_hang_stats(dev, file, ctx_id);
  953. if (IS_ERR(hs)) {
  954. ret = PTR_ERR(hs);
  955. goto err;
  956. }
  957. if (hs->banned) {
  958. ret = -EIO;
  959. goto err;
  960. }
  961. ret = i915_switch_context(ring, file, ctx_id);
  962. if (ret)
  963. goto err;
  964. if (ring == &dev_priv->ring[RCS] &&
  965. mode != dev_priv->relative_constants_mode) {
  966. ret = intel_ring_begin(ring, 4);
  967. if (ret)
  968. goto err;
  969. intel_ring_emit(ring, MI_NOOP);
  970. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  971. intel_ring_emit(ring, INSTPM);
  972. intel_ring_emit(ring, mask << 16 | mode);
  973. intel_ring_advance(ring);
  974. dev_priv->relative_constants_mode = mode;
  975. }
  976. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  977. ret = i915_reset_gen7_sol_offsets(dev, ring);
  978. if (ret)
  979. goto err;
  980. }
  981. exec_start = i915_gem_obj_offset(batch_obj, vm) +
  982. args->batch_start_offset;
  983. exec_len = args->batch_len;
  984. if (cliprects) {
  985. for (i = 0; i < args->num_cliprects; i++) {
  986. ret = i915_emit_box(dev, &cliprects[i],
  987. args->DR1, args->DR4);
  988. if (ret)
  989. goto err;
  990. ret = ring->dispatch_execbuffer(ring,
  991. exec_start, exec_len,
  992. flags);
  993. if (ret)
  994. goto err;
  995. }
  996. } else {
  997. ret = ring->dispatch_execbuffer(ring,
  998. exec_start, exec_len,
  999. flags);
  1000. if (ret)
  1001. goto err;
  1002. }
  1003. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  1004. i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
  1005. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  1006. err:
  1007. eb_destroy(eb);
  1008. mutex_unlock(&dev->struct_mutex);
  1009. pre_mutex_err:
  1010. kfree(cliprects);
  1011. return ret;
  1012. }
  1013. /*
  1014. * Legacy execbuffer just creates an exec2 list from the original exec object
  1015. * list array and passes it to the real function.
  1016. */
  1017. int
  1018. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1019. struct drm_file *file)
  1020. {
  1021. struct drm_i915_private *dev_priv = dev->dev_private;
  1022. struct drm_i915_gem_execbuffer *args = data;
  1023. struct drm_i915_gem_execbuffer2 exec2;
  1024. struct drm_i915_gem_exec_object *exec_list = NULL;
  1025. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1026. int ret, i;
  1027. if (args->buffer_count < 1) {
  1028. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1029. return -EINVAL;
  1030. }
  1031. /* Copy in the exec list from userland */
  1032. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1033. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1034. if (exec_list == NULL || exec2_list == NULL) {
  1035. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1036. args->buffer_count);
  1037. drm_free_large(exec_list);
  1038. drm_free_large(exec2_list);
  1039. return -ENOMEM;
  1040. }
  1041. ret = copy_from_user(exec_list,
  1042. to_user_ptr(args->buffers_ptr),
  1043. sizeof(*exec_list) * args->buffer_count);
  1044. if (ret != 0) {
  1045. DRM_DEBUG("copy %d exec entries failed %d\n",
  1046. args->buffer_count, ret);
  1047. drm_free_large(exec_list);
  1048. drm_free_large(exec2_list);
  1049. return -EFAULT;
  1050. }
  1051. for (i = 0; i < args->buffer_count; i++) {
  1052. exec2_list[i].handle = exec_list[i].handle;
  1053. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1054. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1055. exec2_list[i].alignment = exec_list[i].alignment;
  1056. exec2_list[i].offset = exec_list[i].offset;
  1057. if (INTEL_INFO(dev)->gen < 4)
  1058. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1059. else
  1060. exec2_list[i].flags = 0;
  1061. }
  1062. exec2.buffers_ptr = args->buffers_ptr;
  1063. exec2.buffer_count = args->buffer_count;
  1064. exec2.batch_start_offset = args->batch_start_offset;
  1065. exec2.batch_len = args->batch_len;
  1066. exec2.DR1 = args->DR1;
  1067. exec2.DR4 = args->DR4;
  1068. exec2.num_cliprects = args->num_cliprects;
  1069. exec2.cliprects_ptr = args->cliprects_ptr;
  1070. exec2.flags = I915_EXEC_RENDER;
  1071. i915_execbuffer2_set_context_id(exec2, 0);
  1072. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
  1073. &dev_priv->gtt.base);
  1074. if (!ret) {
  1075. /* Copy the new buffer offsets back to the user's exec list. */
  1076. for (i = 0; i < args->buffer_count; i++)
  1077. exec_list[i].offset = exec2_list[i].offset;
  1078. /* ... and back out to userspace */
  1079. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1080. exec_list,
  1081. sizeof(*exec_list) * args->buffer_count);
  1082. if (ret) {
  1083. ret = -EFAULT;
  1084. DRM_DEBUG("failed to copy %d exec entries "
  1085. "back to user (%d)\n",
  1086. args->buffer_count, ret);
  1087. }
  1088. }
  1089. drm_free_large(exec_list);
  1090. drm_free_large(exec2_list);
  1091. return ret;
  1092. }
  1093. int
  1094. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1095. struct drm_file *file)
  1096. {
  1097. struct drm_i915_private *dev_priv = dev->dev_private;
  1098. struct drm_i915_gem_execbuffer2 *args = data;
  1099. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1100. int ret;
  1101. if (args->buffer_count < 1 ||
  1102. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1103. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1104. return -EINVAL;
  1105. }
  1106. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1107. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1108. if (exec2_list == NULL)
  1109. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1110. args->buffer_count);
  1111. if (exec2_list == NULL) {
  1112. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1113. args->buffer_count);
  1114. return -ENOMEM;
  1115. }
  1116. ret = copy_from_user(exec2_list,
  1117. to_user_ptr(args->buffers_ptr),
  1118. sizeof(*exec2_list) * args->buffer_count);
  1119. if (ret != 0) {
  1120. DRM_DEBUG("copy %d exec entries failed %d\n",
  1121. args->buffer_count, ret);
  1122. drm_free_large(exec2_list);
  1123. return -EFAULT;
  1124. }
  1125. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
  1126. &dev_priv->gtt.base);
  1127. if (!ret) {
  1128. /* Copy the new buffer offsets back to the user's exec list. */
  1129. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1130. exec2_list,
  1131. sizeof(*exec2_list) * args->buffer_count);
  1132. if (ret) {
  1133. ret = -EFAULT;
  1134. DRM_DEBUG("failed to copy %d exec entries "
  1135. "back to user (%d)\n",
  1136. args->buffer_count, ret);
  1137. }
  1138. }
  1139. drm_free_large(exec2_list);
  1140. return ret;
  1141. }