i915_gem_execbuffer.c 32 KB

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