i915_gem_execbuffer.c 32 KB

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