i915_gem_execbuffer.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. }
  357. list_splice(&ordered_objects, objects);
  358. /* Attempt to pin all of the buffers into the GTT.
  359. * This is done in 3 phases:
  360. *
  361. * 1a. Unbind all objects that do not match the GTT constraints for
  362. * the execbuffer (fenceable, mappable, alignment etc).
  363. * 1b. Increment pin count for already bound objects.
  364. * 2. Bind new objects.
  365. * 3. Decrement pin count.
  366. *
  367. * This avoid unnecessary unbinding of later objects in order to makr
  368. * room for the earlier objects *unless* we need to defragment.
  369. */
  370. retry = 0;
  371. do {
  372. ret = 0;
  373. /* Unbind any ill-fitting objects or pin. */
  374. list_for_each_entry(obj, objects, exec_list) {
  375. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  376. bool need_fence, need_mappable;
  377. if (!obj->gtt_space)
  378. continue;
  379. need_fence =
  380. has_fenced_gpu_access &&
  381. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  382. obj->tiling_mode != I915_TILING_NONE;
  383. need_mappable = need_fence || need_reloc_mappable(obj);
  384. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  385. (need_mappable && !obj->map_and_fenceable))
  386. ret = i915_gem_object_unbind(obj);
  387. else
  388. ret = pin_and_fence_object(obj, ring);
  389. if (ret)
  390. goto err;
  391. }
  392. /* Bind fresh objects */
  393. list_for_each_entry(obj, objects, exec_list) {
  394. if (obj->gtt_space)
  395. continue;
  396. ret = pin_and_fence_object(obj, ring);
  397. if (ret) {
  398. int ret_ignore;
  399. /* This can potentially raise a harmless
  400. * -EINVAL if we failed to bind in the above
  401. * call. It cannot raise -EINTR since we know
  402. * that the bo is freshly bound and so will
  403. * not need to be flushed or waited upon.
  404. */
  405. ret_ignore = i915_gem_object_unbind(obj);
  406. (void)ret_ignore;
  407. WARN_ON(obj->gtt_space);
  408. break;
  409. }
  410. }
  411. /* Decrement pin count for bound objects */
  412. list_for_each_entry(obj, objects, exec_list) {
  413. struct drm_i915_gem_exec_object2 *entry;
  414. if (!obj->gtt_space)
  415. continue;
  416. entry = obj->exec_entry;
  417. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  418. i915_gem_object_unpin_fence(obj);
  419. entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
  420. }
  421. i915_gem_object_unpin(obj);
  422. /* ... and ensure ppgtt mapping exist if needed. */
  423. if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  424. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  425. obj, obj->cache_level);
  426. obj->has_aliasing_ppgtt_mapping = 1;
  427. }
  428. }
  429. if (ret != -ENOSPC || retry > 1)
  430. return ret;
  431. /* First attempt, just clear anything that is purgeable.
  432. * Second attempt, clear the entire GTT.
  433. */
  434. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  435. if (ret)
  436. return ret;
  437. retry++;
  438. } while (1);
  439. err:
  440. list_for_each_entry_continue_reverse(obj, objects, exec_list) {
  441. struct drm_i915_gem_exec_object2 *entry;
  442. if (!obj->gtt_space)
  443. continue;
  444. entry = obj->exec_entry;
  445. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  446. i915_gem_object_unpin_fence(obj);
  447. entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
  448. }
  449. i915_gem_object_unpin(obj);
  450. }
  451. return ret;
  452. }
  453. static int
  454. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  455. struct drm_file *file,
  456. struct intel_ring_buffer *ring,
  457. struct list_head *objects,
  458. struct eb_objects *eb,
  459. struct drm_i915_gem_exec_object2 *exec,
  460. int count)
  461. {
  462. struct drm_i915_gem_relocation_entry *reloc;
  463. struct drm_i915_gem_object *obj;
  464. int *reloc_offset;
  465. int i, total, ret;
  466. /* We may process another execbuffer during the unlock... */
  467. while (!list_empty(objects)) {
  468. obj = list_first_entry(objects,
  469. struct drm_i915_gem_object,
  470. exec_list);
  471. list_del_init(&obj->exec_list);
  472. drm_gem_object_unreference(&obj->base);
  473. }
  474. mutex_unlock(&dev->struct_mutex);
  475. total = 0;
  476. for (i = 0; i < count; i++)
  477. total += exec[i].relocation_count;
  478. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  479. reloc = drm_malloc_ab(total, sizeof(*reloc));
  480. if (reloc == NULL || reloc_offset == NULL) {
  481. drm_free_large(reloc);
  482. drm_free_large(reloc_offset);
  483. mutex_lock(&dev->struct_mutex);
  484. return -ENOMEM;
  485. }
  486. total = 0;
  487. for (i = 0; i < count; i++) {
  488. struct drm_i915_gem_relocation_entry __user *user_relocs;
  489. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  490. if (copy_from_user(reloc+total, user_relocs,
  491. exec[i].relocation_count * sizeof(*reloc))) {
  492. ret = -EFAULT;
  493. mutex_lock(&dev->struct_mutex);
  494. goto err;
  495. }
  496. reloc_offset[i] = total;
  497. total += exec[i].relocation_count;
  498. }
  499. ret = i915_mutex_lock_interruptible(dev);
  500. if (ret) {
  501. mutex_lock(&dev->struct_mutex);
  502. goto err;
  503. }
  504. /* reacquire the objects */
  505. eb_reset(eb);
  506. for (i = 0; i < count; i++) {
  507. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  508. exec[i].handle));
  509. if (&obj->base == NULL) {
  510. DRM_DEBUG("Invalid object handle %d at index %d\n",
  511. exec[i].handle, i);
  512. ret = -ENOENT;
  513. goto err;
  514. }
  515. list_add_tail(&obj->exec_list, objects);
  516. obj->exec_handle = exec[i].handle;
  517. obj->exec_entry = &exec[i];
  518. eb_add_object(eb, obj);
  519. }
  520. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  521. if (ret)
  522. goto err;
  523. list_for_each_entry(obj, objects, exec_list) {
  524. int offset = obj->exec_entry - exec;
  525. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  526. reloc + reloc_offset[offset]);
  527. if (ret)
  528. goto err;
  529. }
  530. /* Leave the user relocations as are, this is the painfully slow path,
  531. * and we want to avoid the complication of dropping the lock whilst
  532. * having buffers reserved in the aperture and so causing spurious
  533. * ENOSPC for random operations.
  534. */
  535. err:
  536. drm_free_large(reloc);
  537. drm_free_large(reloc_offset);
  538. return ret;
  539. }
  540. static int
  541. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  542. {
  543. u32 plane, flip_mask;
  544. int ret;
  545. /* Check for any pending flips. As we only maintain a flip queue depth
  546. * of 1, we can simply insert a WAIT for the next display flip prior
  547. * to executing the batch and avoid stalling the CPU.
  548. */
  549. for (plane = 0; flips >> plane; plane++) {
  550. if (((flips >> plane) & 1) == 0)
  551. continue;
  552. if (plane)
  553. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  554. else
  555. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  556. ret = intel_ring_begin(ring, 2);
  557. if (ret)
  558. return ret;
  559. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  560. intel_ring_emit(ring, MI_NOOP);
  561. intel_ring_advance(ring);
  562. }
  563. return 0;
  564. }
  565. static int
  566. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  567. struct list_head *objects)
  568. {
  569. struct drm_i915_gem_object *obj;
  570. uint32_t flush_domains = 0;
  571. uint32_t flips = 0;
  572. int ret;
  573. list_for_each_entry(obj, objects, exec_list) {
  574. ret = i915_gem_object_sync(obj, ring);
  575. if (ret)
  576. return ret;
  577. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  578. i915_gem_clflush_object(obj);
  579. if (obj->base.pending_write_domain)
  580. flips |= atomic_read(&obj->pending_flip);
  581. flush_domains |= obj->base.write_domain;
  582. }
  583. if (flips) {
  584. ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
  585. if (ret)
  586. return ret;
  587. }
  588. if (flush_domains & I915_GEM_DOMAIN_CPU)
  589. intel_gtt_chipset_flush();
  590. if (flush_domains & I915_GEM_DOMAIN_GTT)
  591. wmb();
  592. /* Unconditionally invalidate gpu caches and ensure that we do flush
  593. * any residual writes from the previous batch.
  594. */
  595. ret = i915_gem_flush_ring(ring,
  596. I915_GEM_GPU_DOMAINS,
  597. ring->gpu_caches_dirty ? I915_GEM_GPU_DOMAINS : 0);
  598. if (ret)
  599. return ret;
  600. ring->gpu_caches_dirty = false;
  601. return 0;
  602. }
  603. static bool
  604. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  605. {
  606. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  607. }
  608. static int
  609. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  610. int count)
  611. {
  612. int i;
  613. for (i = 0; i < count; i++) {
  614. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  615. int length; /* limited by fault_in_pages_readable() */
  616. /* First check for malicious input causing overflow */
  617. if (exec[i].relocation_count >
  618. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  619. return -EINVAL;
  620. length = exec[i].relocation_count *
  621. sizeof(struct drm_i915_gem_relocation_entry);
  622. if (!access_ok(VERIFY_READ, ptr, length))
  623. return -EFAULT;
  624. /* we may also need to update the presumed offsets */
  625. if (!access_ok(VERIFY_WRITE, ptr, length))
  626. return -EFAULT;
  627. if (fault_in_multipages_readable(ptr, length))
  628. return -EFAULT;
  629. }
  630. return 0;
  631. }
  632. static void
  633. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  634. struct intel_ring_buffer *ring,
  635. u32 seqno)
  636. {
  637. struct drm_i915_gem_object *obj;
  638. list_for_each_entry(obj, objects, exec_list) {
  639. u32 old_read = obj->base.read_domains;
  640. u32 old_write = obj->base.write_domain;
  641. obj->base.read_domains = obj->base.pending_read_domains;
  642. obj->base.write_domain = obj->base.pending_write_domain;
  643. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  644. i915_gem_object_move_to_active(obj, ring, seqno);
  645. if (obj->base.write_domain) {
  646. obj->dirty = 1;
  647. obj->last_write_seqno = seqno;
  648. if (obj->pin_count) /* check for potential scanout */
  649. intel_mark_busy(ring->dev, obj);
  650. }
  651. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  652. }
  653. intel_mark_busy(ring->dev, NULL);
  654. }
  655. static void
  656. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  657. struct drm_file *file,
  658. struct intel_ring_buffer *ring)
  659. {
  660. /* Unconditionally force add_request to emit a full flush. */
  661. ring->gpu_caches_dirty = true;
  662. /* Add a breadcrumb for the completion of the batch buffer */
  663. (void)i915_add_request(ring, file, NULL);
  664. }
  665. static int
  666. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  667. struct intel_ring_buffer *ring)
  668. {
  669. drm_i915_private_t *dev_priv = dev->dev_private;
  670. int ret, i;
  671. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  672. return 0;
  673. ret = intel_ring_begin(ring, 4 * 3);
  674. if (ret)
  675. return ret;
  676. for (i = 0; i < 4; i++) {
  677. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  678. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  679. intel_ring_emit(ring, 0);
  680. }
  681. intel_ring_advance(ring);
  682. return 0;
  683. }
  684. static int
  685. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  686. struct drm_file *file,
  687. struct drm_i915_gem_execbuffer2 *args,
  688. struct drm_i915_gem_exec_object2 *exec)
  689. {
  690. drm_i915_private_t *dev_priv = dev->dev_private;
  691. struct list_head objects;
  692. struct eb_objects *eb;
  693. struct drm_i915_gem_object *batch_obj;
  694. struct drm_clip_rect *cliprects = NULL;
  695. struct intel_ring_buffer *ring;
  696. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  697. u32 exec_start, exec_len;
  698. u32 seqno;
  699. u32 mask;
  700. int ret, mode, i;
  701. if (!i915_gem_check_execbuffer(args)) {
  702. DRM_DEBUG("execbuf with invalid offset/length\n");
  703. return -EINVAL;
  704. }
  705. ret = validate_exec_list(exec, args->buffer_count);
  706. if (ret)
  707. return ret;
  708. switch (args->flags & I915_EXEC_RING_MASK) {
  709. case I915_EXEC_DEFAULT:
  710. case I915_EXEC_RENDER:
  711. ring = &dev_priv->ring[RCS];
  712. break;
  713. case I915_EXEC_BSD:
  714. ring = &dev_priv->ring[VCS];
  715. if (ctx_id != 0) {
  716. DRM_DEBUG("Ring %s doesn't support contexts\n",
  717. ring->name);
  718. return -EPERM;
  719. }
  720. break;
  721. case I915_EXEC_BLT:
  722. ring = &dev_priv->ring[BCS];
  723. if (ctx_id != 0) {
  724. DRM_DEBUG("Ring %s doesn't support contexts\n",
  725. ring->name);
  726. return -EPERM;
  727. }
  728. break;
  729. default:
  730. DRM_DEBUG("execbuf with unknown ring: %d\n",
  731. (int)(args->flags & I915_EXEC_RING_MASK));
  732. return -EINVAL;
  733. }
  734. if (!intel_ring_initialized(ring)) {
  735. DRM_DEBUG("execbuf with invalid ring: %d\n",
  736. (int)(args->flags & I915_EXEC_RING_MASK));
  737. return -EINVAL;
  738. }
  739. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  740. mask = I915_EXEC_CONSTANTS_MASK;
  741. switch (mode) {
  742. case I915_EXEC_CONSTANTS_REL_GENERAL:
  743. case I915_EXEC_CONSTANTS_ABSOLUTE:
  744. case I915_EXEC_CONSTANTS_REL_SURFACE:
  745. if (ring == &dev_priv->ring[RCS] &&
  746. mode != dev_priv->relative_constants_mode) {
  747. if (INTEL_INFO(dev)->gen < 4)
  748. return -EINVAL;
  749. if (INTEL_INFO(dev)->gen > 5 &&
  750. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  751. return -EINVAL;
  752. /* The HW changed the meaning on this bit on gen6 */
  753. if (INTEL_INFO(dev)->gen >= 6)
  754. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  755. }
  756. break;
  757. default:
  758. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  759. return -EINVAL;
  760. }
  761. if (args->buffer_count < 1) {
  762. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  763. return -EINVAL;
  764. }
  765. if (args->num_cliprects != 0) {
  766. if (ring != &dev_priv->ring[RCS]) {
  767. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  768. return -EINVAL;
  769. }
  770. if (INTEL_INFO(dev)->gen >= 5) {
  771. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  772. return -EINVAL;
  773. }
  774. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  775. DRM_DEBUG("execbuf with %u cliprects\n",
  776. args->num_cliprects);
  777. return -EINVAL;
  778. }
  779. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  780. GFP_KERNEL);
  781. if (cliprects == NULL) {
  782. ret = -ENOMEM;
  783. goto pre_mutex_err;
  784. }
  785. if (copy_from_user(cliprects,
  786. (struct drm_clip_rect __user *)(uintptr_t)
  787. args->cliprects_ptr,
  788. sizeof(*cliprects)*args->num_cliprects)) {
  789. ret = -EFAULT;
  790. goto pre_mutex_err;
  791. }
  792. }
  793. ret = i915_mutex_lock_interruptible(dev);
  794. if (ret)
  795. goto pre_mutex_err;
  796. if (dev_priv->mm.suspended) {
  797. mutex_unlock(&dev->struct_mutex);
  798. ret = -EBUSY;
  799. goto pre_mutex_err;
  800. }
  801. eb = eb_create(args->buffer_count);
  802. if (eb == NULL) {
  803. mutex_unlock(&dev->struct_mutex);
  804. ret = -ENOMEM;
  805. goto pre_mutex_err;
  806. }
  807. /* Look up object handles */
  808. INIT_LIST_HEAD(&objects);
  809. for (i = 0; i < args->buffer_count; i++) {
  810. struct drm_i915_gem_object *obj;
  811. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  812. exec[i].handle));
  813. if (&obj->base == NULL) {
  814. DRM_DEBUG("Invalid object handle %d at index %d\n",
  815. exec[i].handle, i);
  816. /* prevent error path from reading uninitialized data */
  817. ret = -ENOENT;
  818. goto err;
  819. }
  820. if (!list_empty(&obj->exec_list)) {
  821. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  822. obj, exec[i].handle, i);
  823. ret = -EINVAL;
  824. goto err;
  825. }
  826. list_add_tail(&obj->exec_list, &objects);
  827. obj->exec_handle = exec[i].handle;
  828. obj->exec_entry = &exec[i];
  829. eb_add_object(eb, obj);
  830. }
  831. /* take note of the batch buffer before we might reorder the lists */
  832. batch_obj = list_entry(objects.prev,
  833. struct drm_i915_gem_object,
  834. exec_list);
  835. /* Move the objects en-masse into the GTT, evicting if necessary. */
  836. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  837. if (ret)
  838. goto err;
  839. /* The objects are in their final locations, apply the relocations. */
  840. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  841. if (ret) {
  842. if (ret == -EFAULT) {
  843. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  844. &objects, eb,
  845. exec,
  846. args->buffer_count);
  847. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  848. }
  849. if (ret)
  850. goto err;
  851. }
  852. /* Set the pending read domains for the batch buffer to COMMAND */
  853. if (batch_obj->base.pending_write_domain) {
  854. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  855. ret = -EINVAL;
  856. goto err;
  857. }
  858. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  859. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  860. if (ret)
  861. goto err;
  862. seqno = i915_gem_next_request_seqno(ring);
  863. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  864. if (seqno < ring->sync_seqno[i]) {
  865. /* The GPU can not handle its semaphore value wrapping,
  866. * so every billion or so execbuffers, we need to stall
  867. * the GPU in order to reset the counters.
  868. */
  869. ret = i915_gpu_idle(dev);
  870. if (ret)
  871. goto err;
  872. i915_gem_retire_requests(dev);
  873. BUG_ON(ring->sync_seqno[i]);
  874. }
  875. }
  876. ret = i915_switch_context(ring, file, ctx_id);
  877. if (ret)
  878. goto err;
  879. if (ring == &dev_priv->ring[RCS] &&
  880. mode != dev_priv->relative_constants_mode) {
  881. ret = intel_ring_begin(ring, 4);
  882. if (ret)
  883. goto err;
  884. intel_ring_emit(ring, MI_NOOP);
  885. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  886. intel_ring_emit(ring, INSTPM);
  887. intel_ring_emit(ring, mask << 16 | mode);
  888. intel_ring_advance(ring);
  889. dev_priv->relative_constants_mode = mode;
  890. }
  891. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  892. ret = i915_reset_gen7_sol_offsets(dev, ring);
  893. if (ret)
  894. goto err;
  895. }
  896. trace_i915_gem_ring_dispatch(ring, seqno);
  897. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  898. exec_len = args->batch_len;
  899. if (cliprects) {
  900. for (i = 0; i < args->num_cliprects; i++) {
  901. ret = i915_emit_box(dev, &cliprects[i],
  902. args->DR1, args->DR4);
  903. if (ret)
  904. goto err;
  905. ret = ring->dispatch_execbuffer(ring,
  906. exec_start, exec_len);
  907. if (ret)
  908. goto err;
  909. }
  910. } else {
  911. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  912. if (ret)
  913. goto err;
  914. }
  915. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  916. i915_gem_execbuffer_retire_commands(dev, file, ring);
  917. err:
  918. eb_destroy(eb);
  919. while (!list_empty(&objects)) {
  920. struct drm_i915_gem_object *obj;
  921. obj = list_first_entry(&objects,
  922. struct drm_i915_gem_object,
  923. exec_list);
  924. list_del_init(&obj->exec_list);
  925. drm_gem_object_unreference(&obj->base);
  926. }
  927. mutex_unlock(&dev->struct_mutex);
  928. pre_mutex_err:
  929. kfree(cliprects);
  930. return ret;
  931. }
  932. /*
  933. * Legacy execbuffer just creates an exec2 list from the original exec object
  934. * list array and passes it to the real function.
  935. */
  936. int
  937. i915_gem_execbuffer(struct drm_device *dev, void *data,
  938. struct drm_file *file)
  939. {
  940. struct drm_i915_gem_execbuffer *args = data;
  941. struct drm_i915_gem_execbuffer2 exec2;
  942. struct drm_i915_gem_exec_object *exec_list = NULL;
  943. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  944. int ret, i;
  945. if (args->buffer_count < 1) {
  946. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  947. return -EINVAL;
  948. }
  949. /* Copy in the exec list from userland */
  950. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  951. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  952. if (exec_list == NULL || exec2_list == NULL) {
  953. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  954. args->buffer_count);
  955. drm_free_large(exec_list);
  956. drm_free_large(exec2_list);
  957. return -ENOMEM;
  958. }
  959. ret = copy_from_user(exec_list,
  960. (struct drm_i915_relocation_entry __user *)
  961. (uintptr_t) args->buffers_ptr,
  962. sizeof(*exec_list) * args->buffer_count);
  963. if (ret != 0) {
  964. DRM_DEBUG("copy %d exec entries failed %d\n",
  965. args->buffer_count, ret);
  966. drm_free_large(exec_list);
  967. drm_free_large(exec2_list);
  968. return -EFAULT;
  969. }
  970. for (i = 0; i < args->buffer_count; i++) {
  971. exec2_list[i].handle = exec_list[i].handle;
  972. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  973. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  974. exec2_list[i].alignment = exec_list[i].alignment;
  975. exec2_list[i].offset = exec_list[i].offset;
  976. if (INTEL_INFO(dev)->gen < 4)
  977. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  978. else
  979. exec2_list[i].flags = 0;
  980. }
  981. exec2.buffers_ptr = args->buffers_ptr;
  982. exec2.buffer_count = args->buffer_count;
  983. exec2.batch_start_offset = args->batch_start_offset;
  984. exec2.batch_len = args->batch_len;
  985. exec2.DR1 = args->DR1;
  986. exec2.DR4 = args->DR4;
  987. exec2.num_cliprects = args->num_cliprects;
  988. exec2.cliprects_ptr = args->cliprects_ptr;
  989. exec2.flags = I915_EXEC_RENDER;
  990. i915_execbuffer2_set_context_id(exec2, 0);
  991. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  992. if (!ret) {
  993. /* Copy the new buffer offsets back to the user's exec list. */
  994. for (i = 0; i < args->buffer_count; i++)
  995. exec_list[i].offset = exec2_list[i].offset;
  996. /* ... and back out to userspace */
  997. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  998. (uintptr_t) args->buffers_ptr,
  999. exec_list,
  1000. sizeof(*exec_list) * args->buffer_count);
  1001. if (ret) {
  1002. ret = -EFAULT;
  1003. DRM_DEBUG("failed to copy %d exec entries "
  1004. "back to user (%d)\n",
  1005. args->buffer_count, ret);
  1006. }
  1007. }
  1008. drm_free_large(exec_list);
  1009. drm_free_large(exec2_list);
  1010. return ret;
  1011. }
  1012. int
  1013. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1014. struct drm_file *file)
  1015. {
  1016. struct drm_i915_gem_execbuffer2 *args = data;
  1017. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1018. int ret;
  1019. if (args->buffer_count < 1 ||
  1020. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1021. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1022. return -EINVAL;
  1023. }
  1024. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1025. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1026. if (exec2_list == NULL)
  1027. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1028. args->buffer_count);
  1029. if (exec2_list == NULL) {
  1030. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1031. args->buffer_count);
  1032. return -ENOMEM;
  1033. }
  1034. ret = copy_from_user(exec2_list,
  1035. (struct drm_i915_relocation_entry __user *)
  1036. (uintptr_t) args->buffers_ptr,
  1037. sizeof(*exec2_list) * args->buffer_count);
  1038. if (ret != 0) {
  1039. DRM_DEBUG("copy %d exec entries failed %d\n",
  1040. args->buffer_count, ret);
  1041. drm_free_large(exec2_list);
  1042. return -EFAULT;
  1043. }
  1044. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1045. if (!ret) {
  1046. /* Copy the new buffer offsets back to the user's exec list. */
  1047. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1048. (uintptr_t) args->buffers_ptr,
  1049. exec2_list,
  1050. sizeof(*exec2_list) * args->buffer_count);
  1051. if (ret) {
  1052. ret = -EFAULT;
  1053. DRM_DEBUG("failed to copy %d exec entries "
  1054. "back to user (%d)\n",
  1055. args->buffer_count, ret);
  1056. }
  1057. }
  1058. drm_free_large(exec2_list);
  1059. return ret;
  1060. }