i915_gem_execbuffer.c 32 KB

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