i915_gem_execbuffer.c 32 KB

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