i915_gem_execbuffer.c 32 KB

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