i915_gem_execbuffer.c 32 KB

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