i915_gem_execbuffer.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * Copyright © 2008,2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/dma_remapping.h>
  34. struct eb_objects {
  35. int and;
  36. struct hlist_head buckets[0];
  37. };
  38. static struct eb_objects *
  39. eb_create(int size)
  40. {
  41. struct eb_objects *eb;
  42. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  43. BUILD_BUG_ON(!is_power_of_2(PAGE_SIZE / sizeof(struct hlist_head)));
  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. /* Validate that the target is in a valid r/w GPU domain */
  116. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  117. DRM_DEBUG("reloc with multiple write domains: "
  118. "obj %p target %d offset %d "
  119. "read %08x write %08x",
  120. obj, reloc->target_handle,
  121. (int) reloc->offset,
  122. reloc->read_domains,
  123. reloc->write_domain);
  124. return ret;
  125. }
  126. if (unlikely((reloc->write_domain | reloc->read_domains)
  127. & ~I915_GEM_GPU_DOMAINS)) {
  128. DRM_DEBUG("reloc with read/write non-GPU domains: "
  129. "obj %p target %d offset %d "
  130. "read %08x write %08x",
  131. obj, reloc->target_handle,
  132. (int) reloc->offset,
  133. reloc->read_domains,
  134. reloc->write_domain);
  135. return ret;
  136. }
  137. target_obj->pending_read_domains |= reloc->read_domains;
  138. target_obj->pending_write_domain |= reloc->write_domain;
  139. /* If the relocation already has the right value in it, no
  140. * more work needs to be done.
  141. */
  142. if (target_offset == reloc->presumed_offset)
  143. return 0;
  144. /* Check that the relocation address is valid... */
  145. if (unlikely(reloc->offset > obj->base.size - 4)) {
  146. DRM_DEBUG("Relocation beyond object bounds: "
  147. "obj %p target %d offset %d size %d.\n",
  148. obj, reloc->target_handle,
  149. (int) reloc->offset,
  150. (int) obj->base.size);
  151. return ret;
  152. }
  153. if (unlikely(reloc->offset & 3)) {
  154. DRM_DEBUG("Relocation not 4-byte aligned: "
  155. "obj %p target %d offset %d.\n",
  156. obj, reloc->target_handle,
  157. (int) reloc->offset);
  158. return ret;
  159. }
  160. /* We can't wait for rendering with pagefaults disabled */
  161. if (obj->active && in_atomic())
  162. return -EFAULT;
  163. reloc->delta += target_offset;
  164. if (use_cpu_reloc(obj)) {
  165. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  166. char *vaddr;
  167. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  168. if (ret)
  169. return ret;
  170. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  171. reloc->offset >> PAGE_SHIFT));
  172. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  173. kunmap_atomic(vaddr);
  174. } else {
  175. struct drm_i915_private *dev_priv = dev->dev_private;
  176. uint32_t __iomem *reloc_entry;
  177. void __iomem *reloc_page;
  178. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  179. if (ret)
  180. return ret;
  181. ret = i915_gem_object_put_fence(obj);
  182. if (ret)
  183. return ret;
  184. /* Map the page containing the relocation we're going to perform. */
  185. reloc->offset += obj->gtt_offset;
  186. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  187. reloc->offset & PAGE_MASK);
  188. reloc_entry = (uint32_t __iomem *)
  189. (reloc_page + (reloc->offset & ~PAGE_MASK));
  190. iowrite32(reloc->delta, reloc_entry);
  191. io_mapping_unmap_atomic(reloc_page);
  192. }
  193. /* and update the user's relocation entry */
  194. reloc->presumed_offset = target_offset;
  195. return 0;
  196. }
  197. static int
  198. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  199. struct eb_objects *eb)
  200. {
  201. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  202. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  203. struct drm_i915_gem_relocation_entry __user *user_relocs;
  204. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  205. int remain, ret;
  206. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  207. remain = entry->relocation_count;
  208. while (remain) {
  209. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  210. int count = remain;
  211. if (count > ARRAY_SIZE(stack_reloc))
  212. count = ARRAY_SIZE(stack_reloc);
  213. remain -= count;
  214. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  215. return -EFAULT;
  216. do {
  217. u64 offset = r->presumed_offset;
  218. ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
  219. if (ret)
  220. return ret;
  221. if (r->presumed_offset != offset &&
  222. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  223. &r->presumed_offset,
  224. sizeof(r->presumed_offset))) {
  225. return -EFAULT;
  226. }
  227. user_relocs++;
  228. r++;
  229. } while (--count);
  230. }
  231. return 0;
  232. #undef N_RELOC
  233. }
  234. static int
  235. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  236. struct eb_objects *eb,
  237. struct drm_i915_gem_relocation_entry *relocs)
  238. {
  239. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  240. int i, ret;
  241. for (i = 0; i < entry->relocation_count; i++) {
  242. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  243. if (ret)
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. static int
  249. i915_gem_execbuffer_relocate(struct drm_device *dev,
  250. struct eb_objects *eb,
  251. struct list_head *objects)
  252. {
  253. struct drm_i915_gem_object *obj;
  254. int ret = 0;
  255. /* This is the fast path and we cannot handle a pagefault whilst
  256. * holding the struct mutex lest the user pass in the relocations
  257. * contained within a mmaped bo. For in such a case we, the page
  258. * fault handler would call i915_gem_fault() and we would try to
  259. * acquire the struct mutex again. Obviously this is bad and so
  260. * lockdep complains vehemently.
  261. */
  262. pagefault_disable();
  263. list_for_each_entry(obj, objects, exec_list) {
  264. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  265. if (ret)
  266. break;
  267. }
  268. pagefault_enable();
  269. return ret;
  270. }
  271. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  272. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  273. static int
  274. need_reloc_mappable(struct drm_i915_gem_object *obj)
  275. {
  276. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  277. return entry->relocation_count && !use_cpu_reloc(obj);
  278. }
  279. static int
  280. i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
  281. struct intel_ring_buffer *ring)
  282. {
  283. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  284. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  285. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  286. bool need_fence, need_mappable;
  287. int ret;
  288. need_fence =
  289. has_fenced_gpu_access &&
  290. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  291. obj->tiling_mode != I915_TILING_NONE;
  292. need_mappable = need_fence || need_reloc_mappable(obj);
  293. ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
  294. if (ret)
  295. return ret;
  296. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  297. if (has_fenced_gpu_access) {
  298. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  299. ret = i915_gem_object_get_fence(obj);
  300. if (ret)
  301. return ret;
  302. if (i915_gem_object_pin_fence(obj))
  303. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  304. obj->pending_fenced_gpu_access = true;
  305. }
  306. }
  307. /* Ensure ppgtt mapping exists if needed */
  308. if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  309. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  310. obj, obj->cache_level);
  311. obj->has_aliasing_ppgtt_mapping = 1;
  312. }
  313. entry->offset = obj->gtt_offset;
  314. return 0;
  315. }
  316. static void
  317. i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
  318. {
  319. struct drm_i915_gem_exec_object2 *entry;
  320. if (!obj->gtt_space)
  321. return;
  322. entry = obj->exec_entry;
  323. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  324. i915_gem_object_unpin_fence(obj);
  325. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  326. i915_gem_object_unpin(obj);
  327. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  328. }
  329. static int
  330. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  331. struct drm_file *file,
  332. struct list_head *objects)
  333. {
  334. struct drm_i915_gem_object *obj;
  335. struct list_head ordered_objects;
  336. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  337. int retry;
  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 make
  370. * room for the earlier objects *unless* we need to defragment.
  371. */
  372. retry = 0;
  373. do {
  374. int 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 = i915_gem_execbuffer_reserve_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 = i915_gem_execbuffer_reserve_object(obj, ring);
  399. if (ret)
  400. goto err;
  401. }
  402. err: /* Decrement pin count for bound objects */
  403. list_for_each_entry(obj, objects, exec_list)
  404. i915_gem_execbuffer_unreserve_object(obj);
  405. if (ret != -ENOSPC || retry++)
  406. return ret;
  407. ret = i915_gem_evict_everything(ring->dev);
  408. if (ret)
  409. return ret;
  410. } while (1);
  411. }
  412. static int
  413. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  414. struct drm_file *file,
  415. struct intel_ring_buffer *ring,
  416. struct list_head *objects,
  417. struct eb_objects *eb,
  418. struct drm_i915_gem_exec_object2 *exec,
  419. int count)
  420. {
  421. struct drm_i915_gem_relocation_entry *reloc;
  422. struct drm_i915_gem_object *obj;
  423. int *reloc_offset;
  424. int i, total, ret;
  425. /* We may process another execbuffer during the unlock... */
  426. while (!list_empty(objects)) {
  427. obj = list_first_entry(objects,
  428. struct drm_i915_gem_object,
  429. exec_list);
  430. list_del_init(&obj->exec_list);
  431. drm_gem_object_unreference(&obj->base);
  432. }
  433. mutex_unlock(&dev->struct_mutex);
  434. total = 0;
  435. for (i = 0; i < count; i++)
  436. total += exec[i].relocation_count;
  437. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  438. reloc = drm_malloc_ab(total, sizeof(*reloc));
  439. if (reloc == NULL || reloc_offset == NULL) {
  440. drm_free_large(reloc);
  441. drm_free_large(reloc_offset);
  442. mutex_lock(&dev->struct_mutex);
  443. return -ENOMEM;
  444. }
  445. total = 0;
  446. for (i = 0; i < count; i++) {
  447. struct drm_i915_gem_relocation_entry __user *user_relocs;
  448. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  449. if (copy_from_user(reloc+total, user_relocs,
  450. exec[i].relocation_count * sizeof(*reloc))) {
  451. ret = -EFAULT;
  452. mutex_lock(&dev->struct_mutex);
  453. goto err;
  454. }
  455. reloc_offset[i] = total;
  456. total += exec[i].relocation_count;
  457. }
  458. ret = i915_mutex_lock_interruptible(dev);
  459. if (ret) {
  460. mutex_lock(&dev->struct_mutex);
  461. goto err;
  462. }
  463. /* reacquire the objects */
  464. eb_reset(eb);
  465. for (i = 0; i < count; i++) {
  466. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  467. exec[i].handle));
  468. if (&obj->base == NULL) {
  469. DRM_DEBUG("Invalid object handle %d at index %d\n",
  470. exec[i].handle, i);
  471. ret = -ENOENT;
  472. goto err;
  473. }
  474. list_add_tail(&obj->exec_list, objects);
  475. obj->exec_handle = exec[i].handle;
  476. obj->exec_entry = &exec[i];
  477. eb_add_object(eb, obj);
  478. }
  479. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  480. if (ret)
  481. goto err;
  482. list_for_each_entry(obj, objects, exec_list) {
  483. int offset = obj->exec_entry - exec;
  484. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  485. reloc + reloc_offset[offset]);
  486. if (ret)
  487. goto err;
  488. }
  489. /* Leave the user relocations as are, this is the painfully slow path,
  490. * and we want to avoid the complication of dropping the lock whilst
  491. * having buffers reserved in the aperture and so causing spurious
  492. * ENOSPC for random operations.
  493. */
  494. err:
  495. drm_free_large(reloc);
  496. drm_free_large(reloc_offset);
  497. return ret;
  498. }
  499. static int
  500. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  501. struct list_head *objects)
  502. {
  503. struct drm_i915_gem_object *obj;
  504. uint32_t flush_domains = 0;
  505. int ret;
  506. list_for_each_entry(obj, objects, exec_list) {
  507. ret = i915_gem_object_sync(obj, ring);
  508. if (ret)
  509. return ret;
  510. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  511. i915_gem_clflush_object(obj);
  512. flush_domains |= obj->base.write_domain;
  513. }
  514. if (flush_domains & I915_GEM_DOMAIN_CPU)
  515. i915_gem_chipset_flush(ring->dev);
  516. if (flush_domains & I915_GEM_DOMAIN_GTT)
  517. wmb();
  518. /* Unconditionally invalidate gpu caches and ensure that we do flush
  519. * any residual writes from the previous batch.
  520. */
  521. return intel_ring_invalidate_all_caches(ring);
  522. }
  523. static bool
  524. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  525. {
  526. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  527. }
  528. static int
  529. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  530. int count)
  531. {
  532. int i;
  533. for (i = 0; i < count; i++) {
  534. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  535. int length; /* limited by fault_in_pages_readable() */
  536. /* First check for malicious input causing overflow */
  537. if (exec[i].relocation_count >
  538. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  539. return -EINVAL;
  540. length = exec[i].relocation_count *
  541. sizeof(struct drm_i915_gem_relocation_entry);
  542. if (!access_ok(VERIFY_READ, ptr, length))
  543. return -EFAULT;
  544. /* we may also need to update the presumed offsets */
  545. if (!access_ok(VERIFY_WRITE, ptr, length))
  546. return -EFAULT;
  547. if (fault_in_multipages_readable(ptr, length))
  548. return -EFAULT;
  549. }
  550. return 0;
  551. }
  552. static void
  553. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  554. struct intel_ring_buffer *ring)
  555. {
  556. struct drm_i915_gem_object *obj;
  557. list_for_each_entry(obj, objects, exec_list) {
  558. u32 old_read = obj->base.read_domains;
  559. u32 old_write = obj->base.write_domain;
  560. obj->base.read_domains = obj->base.pending_read_domains;
  561. obj->base.write_domain = obj->base.pending_write_domain;
  562. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  563. i915_gem_object_move_to_active(obj, ring);
  564. if (obj->base.write_domain) {
  565. obj->dirty = 1;
  566. obj->last_write_seqno = intel_ring_get_seqno(ring);
  567. if (obj->pin_count) /* check for potential scanout */
  568. intel_mark_fb_busy(obj);
  569. }
  570. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  571. }
  572. }
  573. static void
  574. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  575. struct drm_file *file,
  576. struct intel_ring_buffer *ring)
  577. {
  578. /* Unconditionally force add_request to emit a full flush. */
  579. ring->gpu_caches_dirty = true;
  580. /* Add a breadcrumb for the completion of the batch buffer */
  581. (void)i915_add_request(ring, file, NULL);
  582. }
  583. static int
  584. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  585. struct intel_ring_buffer *ring)
  586. {
  587. drm_i915_private_t *dev_priv = dev->dev_private;
  588. int ret, i;
  589. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  590. return 0;
  591. ret = intel_ring_begin(ring, 4 * 3);
  592. if (ret)
  593. return ret;
  594. for (i = 0; i < 4; i++) {
  595. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  596. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  597. intel_ring_emit(ring, 0);
  598. }
  599. intel_ring_advance(ring);
  600. return 0;
  601. }
  602. static int
  603. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  604. struct drm_file *file,
  605. struct drm_i915_gem_execbuffer2 *args,
  606. struct drm_i915_gem_exec_object2 *exec)
  607. {
  608. drm_i915_private_t *dev_priv = dev->dev_private;
  609. struct list_head objects;
  610. struct eb_objects *eb;
  611. struct drm_i915_gem_object *batch_obj;
  612. struct drm_clip_rect *cliprects = NULL;
  613. struct intel_ring_buffer *ring;
  614. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  615. u32 exec_start, exec_len;
  616. u32 mask;
  617. u32 flags;
  618. int ret, mode, i;
  619. if (!i915_gem_check_execbuffer(args)) {
  620. DRM_DEBUG("execbuf with invalid offset/length\n");
  621. return -EINVAL;
  622. }
  623. ret = validate_exec_list(exec, args->buffer_count);
  624. if (ret)
  625. return ret;
  626. flags = 0;
  627. if (args->flags & I915_EXEC_SECURE) {
  628. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  629. return -EPERM;
  630. flags |= I915_DISPATCH_SECURE;
  631. }
  632. switch (args->flags & I915_EXEC_RING_MASK) {
  633. case I915_EXEC_DEFAULT:
  634. case I915_EXEC_RENDER:
  635. ring = &dev_priv->ring[RCS];
  636. break;
  637. case I915_EXEC_BSD:
  638. ring = &dev_priv->ring[VCS];
  639. if (ctx_id != 0) {
  640. DRM_DEBUG("Ring %s doesn't support contexts\n",
  641. ring->name);
  642. return -EPERM;
  643. }
  644. break;
  645. case I915_EXEC_BLT:
  646. ring = &dev_priv->ring[BCS];
  647. if (ctx_id != 0) {
  648. DRM_DEBUG("Ring %s doesn't support contexts\n",
  649. ring->name);
  650. return -EPERM;
  651. }
  652. break;
  653. default:
  654. DRM_DEBUG("execbuf with unknown ring: %d\n",
  655. (int)(args->flags & I915_EXEC_RING_MASK));
  656. return -EINVAL;
  657. }
  658. if (!intel_ring_initialized(ring)) {
  659. DRM_DEBUG("execbuf with invalid ring: %d\n",
  660. (int)(args->flags & I915_EXEC_RING_MASK));
  661. return -EINVAL;
  662. }
  663. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  664. mask = I915_EXEC_CONSTANTS_MASK;
  665. switch (mode) {
  666. case I915_EXEC_CONSTANTS_REL_GENERAL:
  667. case I915_EXEC_CONSTANTS_ABSOLUTE:
  668. case I915_EXEC_CONSTANTS_REL_SURFACE:
  669. if (ring == &dev_priv->ring[RCS] &&
  670. mode != dev_priv->relative_constants_mode) {
  671. if (INTEL_INFO(dev)->gen < 4)
  672. return -EINVAL;
  673. if (INTEL_INFO(dev)->gen > 5 &&
  674. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  675. return -EINVAL;
  676. /* The HW changed the meaning on this bit on gen6 */
  677. if (INTEL_INFO(dev)->gen >= 6)
  678. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  679. }
  680. break;
  681. default:
  682. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  683. return -EINVAL;
  684. }
  685. if (args->buffer_count < 1) {
  686. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  687. return -EINVAL;
  688. }
  689. if (args->num_cliprects != 0) {
  690. if (ring != &dev_priv->ring[RCS]) {
  691. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  692. return -EINVAL;
  693. }
  694. if (INTEL_INFO(dev)->gen >= 5) {
  695. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  696. return -EINVAL;
  697. }
  698. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  699. DRM_DEBUG("execbuf with %u cliprects\n",
  700. args->num_cliprects);
  701. return -EINVAL;
  702. }
  703. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  704. GFP_KERNEL);
  705. if (cliprects == NULL) {
  706. ret = -ENOMEM;
  707. goto pre_mutex_err;
  708. }
  709. if (copy_from_user(cliprects,
  710. (struct drm_clip_rect __user *)(uintptr_t)
  711. args->cliprects_ptr,
  712. sizeof(*cliprects)*args->num_cliprects)) {
  713. ret = -EFAULT;
  714. goto pre_mutex_err;
  715. }
  716. }
  717. ret = i915_mutex_lock_interruptible(dev);
  718. if (ret)
  719. goto pre_mutex_err;
  720. if (dev_priv->mm.suspended) {
  721. mutex_unlock(&dev->struct_mutex);
  722. ret = -EBUSY;
  723. goto pre_mutex_err;
  724. }
  725. eb = eb_create(args->buffer_count);
  726. if (eb == NULL) {
  727. mutex_unlock(&dev->struct_mutex);
  728. ret = -ENOMEM;
  729. goto pre_mutex_err;
  730. }
  731. /* Look up object handles */
  732. INIT_LIST_HEAD(&objects);
  733. for (i = 0; i < args->buffer_count; i++) {
  734. struct drm_i915_gem_object *obj;
  735. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  736. exec[i].handle));
  737. if (&obj->base == NULL) {
  738. DRM_DEBUG("Invalid object handle %d at index %d\n",
  739. exec[i].handle, i);
  740. /* prevent error path from reading uninitialized data */
  741. ret = -ENOENT;
  742. goto err;
  743. }
  744. if (!list_empty(&obj->exec_list)) {
  745. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  746. obj, exec[i].handle, i);
  747. ret = -EINVAL;
  748. goto err;
  749. }
  750. list_add_tail(&obj->exec_list, &objects);
  751. obj->exec_handle = exec[i].handle;
  752. obj->exec_entry = &exec[i];
  753. eb_add_object(eb, obj);
  754. }
  755. /* take note of the batch buffer before we might reorder the lists */
  756. batch_obj = list_entry(objects.prev,
  757. struct drm_i915_gem_object,
  758. exec_list);
  759. /* Move the objects en-masse into the GTT, evicting if necessary. */
  760. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  761. if (ret)
  762. goto err;
  763. /* The objects are in their final locations, apply the relocations. */
  764. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  765. if (ret) {
  766. if (ret == -EFAULT) {
  767. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  768. &objects, eb,
  769. exec,
  770. args->buffer_count);
  771. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  772. }
  773. if (ret)
  774. goto err;
  775. }
  776. /* Set the pending read domains for the batch buffer to COMMAND */
  777. if (batch_obj->base.pending_write_domain) {
  778. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  779. ret = -EINVAL;
  780. goto err;
  781. }
  782. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  783. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  784. * batch" bit. Hence we need to pin secure batches into the global gtt.
  785. * hsw should have this fixed, but let's be paranoid and do it
  786. * unconditionally for now. */
  787. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  788. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  789. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  790. if (ret)
  791. goto err;
  792. ret = i915_switch_context(ring, file, ctx_id);
  793. if (ret)
  794. goto err;
  795. if (ring == &dev_priv->ring[RCS] &&
  796. mode != dev_priv->relative_constants_mode) {
  797. ret = intel_ring_begin(ring, 4);
  798. if (ret)
  799. goto err;
  800. intel_ring_emit(ring, MI_NOOP);
  801. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  802. intel_ring_emit(ring, INSTPM);
  803. intel_ring_emit(ring, mask << 16 | mode);
  804. intel_ring_advance(ring);
  805. dev_priv->relative_constants_mode = mode;
  806. }
  807. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  808. ret = i915_reset_gen7_sol_offsets(dev, ring);
  809. if (ret)
  810. goto err;
  811. }
  812. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  813. exec_len = args->batch_len;
  814. if (cliprects) {
  815. for (i = 0; i < args->num_cliprects; i++) {
  816. ret = i915_emit_box(dev, &cliprects[i],
  817. args->DR1, args->DR4);
  818. if (ret)
  819. goto err;
  820. ret = ring->dispatch_execbuffer(ring,
  821. exec_start, exec_len,
  822. flags);
  823. if (ret)
  824. goto err;
  825. }
  826. } else {
  827. ret = ring->dispatch_execbuffer(ring,
  828. exec_start, exec_len,
  829. flags);
  830. if (ret)
  831. goto err;
  832. }
  833. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  834. i915_gem_execbuffer_move_to_active(&objects, ring);
  835. i915_gem_execbuffer_retire_commands(dev, file, ring);
  836. err:
  837. eb_destroy(eb);
  838. while (!list_empty(&objects)) {
  839. struct drm_i915_gem_object *obj;
  840. obj = list_first_entry(&objects,
  841. struct drm_i915_gem_object,
  842. exec_list);
  843. list_del_init(&obj->exec_list);
  844. drm_gem_object_unreference(&obj->base);
  845. }
  846. mutex_unlock(&dev->struct_mutex);
  847. pre_mutex_err:
  848. kfree(cliprects);
  849. return ret;
  850. }
  851. /*
  852. * Legacy execbuffer just creates an exec2 list from the original exec object
  853. * list array and passes it to the real function.
  854. */
  855. int
  856. i915_gem_execbuffer(struct drm_device *dev, void *data,
  857. struct drm_file *file)
  858. {
  859. struct drm_i915_gem_execbuffer *args = data;
  860. struct drm_i915_gem_execbuffer2 exec2;
  861. struct drm_i915_gem_exec_object *exec_list = NULL;
  862. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  863. int ret, i;
  864. if (args->buffer_count < 1) {
  865. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  866. return -EINVAL;
  867. }
  868. /* Copy in the exec list from userland */
  869. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  870. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  871. if (exec_list == NULL || exec2_list == NULL) {
  872. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  873. args->buffer_count);
  874. drm_free_large(exec_list);
  875. drm_free_large(exec2_list);
  876. return -ENOMEM;
  877. }
  878. ret = copy_from_user(exec_list,
  879. (void __user *)(uintptr_t)args->buffers_ptr,
  880. sizeof(*exec_list) * args->buffer_count);
  881. if (ret != 0) {
  882. DRM_DEBUG("copy %d exec entries failed %d\n",
  883. args->buffer_count, ret);
  884. drm_free_large(exec_list);
  885. drm_free_large(exec2_list);
  886. return -EFAULT;
  887. }
  888. for (i = 0; i < args->buffer_count; i++) {
  889. exec2_list[i].handle = exec_list[i].handle;
  890. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  891. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  892. exec2_list[i].alignment = exec_list[i].alignment;
  893. exec2_list[i].offset = exec_list[i].offset;
  894. if (INTEL_INFO(dev)->gen < 4)
  895. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  896. else
  897. exec2_list[i].flags = 0;
  898. }
  899. exec2.buffers_ptr = args->buffers_ptr;
  900. exec2.buffer_count = args->buffer_count;
  901. exec2.batch_start_offset = args->batch_start_offset;
  902. exec2.batch_len = args->batch_len;
  903. exec2.DR1 = args->DR1;
  904. exec2.DR4 = args->DR4;
  905. exec2.num_cliprects = args->num_cliprects;
  906. exec2.cliprects_ptr = args->cliprects_ptr;
  907. exec2.flags = I915_EXEC_RENDER;
  908. i915_execbuffer2_set_context_id(exec2, 0);
  909. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  910. if (!ret) {
  911. /* Copy the new buffer offsets back to the user's exec list. */
  912. for (i = 0; i < args->buffer_count; i++)
  913. exec_list[i].offset = exec2_list[i].offset;
  914. /* ... and back out to userspace */
  915. ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
  916. exec_list,
  917. sizeof(*exec_list) * args->buffer_count);
  918. if (ret) {
  919. ret = -EFAULT;
  920. DRM_DEBUG("failed to copy %d exec entries "
  921. "back to user (%d)\n",
  922. args->buffer_count, ret);
  923. }
  924. }
  925. drm_free_large(exec_list);
  926. drm_free_large(exec2_list);
  927. return ret;
  928. }
  929. int
  930. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  931. struct drm_file *file)
  932. {
  933. struct drm_i915_gem_execbuffer2 *args = data;
  934. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  935. int ret;
  936. if (args->buffer_count < 1 ||
  937. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  938. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  939. return -EINVAL;
  940. }
  941. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  942. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  943. if (exec2_list == NULL)
  944. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  945. args->buffer_count);
  946. if (exec2_list == NULL) {
  947. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  948. args->buffer_count);
  949. return -ENOMEM;
  950. }
  951. ret = copy_from_user(exec2_list,
  952. (struct drm_i915_relocation_entry __user *)
  953. (uintptr_t) args->buffers_ptr,
  954. sizeof(*exec2_list) * args->buffer_count);
  955. if (ret != 0) {
  956. DRM_DEBUG("copy %d exec entries failed %d\n",
  957. args->buffer_count, ret);
  958. drm_free_large(exec2_list);
  959. return -EFAULT;
  960. }
  961. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  962. if (!ret) {
  963. /* Copy the new buffer offsets back to the user's exec list. */
  964. ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
  965. exec2_list,
  966. sizeof(*exec2_list) * args->buffer_count);
  967. if (ret) {
  968. ret = -EFAULT;
  969. DRM_DEBUG("failed to copy %d exec entries "
  970. "back to user (%d)\n",
  971. args->buffer_count, ret);
  972. }
  973. }
  974. drm_free_large(exec2_list);
  975. return ret;
  976. }