i915_gem_execbuffer.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. {
  600. struct drm_i915_gem_object *obj;
  601. list_for_each_entry(obj, objects, exec_list) {
  602. u32 old_read = obj->base.read_domains;
  603. u32 old_write = obj->base.write_domain;
  604. obj->base.read_domains = obj->base.pending_read_domains;
  605. obj->base.write_domain = obj->base.pending_write_domain;
  606. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  607. i915_gem_object_move_to_active(obj, ring);
  608. if (obj->base.write_domain) {
  609. obj->dirty = 1;
  610. obj->last_write_seqno = intel_ring_get_seqno(ring);
  611. if (obj->pin_count) /* check for potential scanout */
  612. intel_mark_fb_busy(obj);
  613. }
  614. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  615. }
  616. }
  617. static void
  618. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  619. struct drm_file *file,
  620. struct intel_ring_buffer *ring)
  621. {
  622. /* Unconditionally force add_request to emit a full flush. */
  623. ring->gpu_caches_dirty = true;
  624. /* Add a breadcrumb for the completion of the batch buffer */
  625. (void)i915_add_request(ring, file, NULL);
  626. }
  627. static int
  628. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  629. struct intel_ring_buffer *ring)
  630. {
  631. drm_i915_private_t *dev_priv = dev->dev_private;
  632. int ret, i;
  633. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  634. return 0;
  635. ret = intel_ring_begin(ring, 4 * 3);
  636. if (ret)
  637. return ret;
  638. for (i = 0; i < 4; i++) {
  639. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  640. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  641. intel_ring_emit(ring, 0);
  642. }
  643. intel_ring_advance(ring);
  644. return 0;
  645. }
  646. static int
  647. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  648. struct drm_file *file,
  649. struct drm_i915_gem_execbuffer2 *args,
  650. struct drm_i915_gem_exec_object2 *exec)
  651. {
  652. drm_i915_private_t *dev_priv = dev->dev_private;
  653. struct list_head objects;
  654. struct eb_objects *eb;
  655. struct drm_i915_gem_object *batch_obj;
  656. struct drm_clip_rect *cliprects = NULL;
  657. struct intel_ring_buffer *ring;
  658. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  659. u32 exec_start, exec_len;
  660. u32 mask;
  661. u32 flags;
  662. int ret, mode, i;
  663. if (!i915_gem_check_execbuffer(args)) {
  664. DRM_DEBUG("execbuf with invalid offset/length\n");
  665. return -EINVAL;
  666. }
  667. ret = validate_exec_list(exec, args->buffer_count);
  668. if (ret)
  669. return ret;
  670. flags = 0;
  671. if (args->flags & I915_EXEC_SECURE) {
  672. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  673. return -EPERM;
  674. flags |= I915_DISPATCH_SECURE;
  675. }
  676. switch (args->flags & I915_EXEC_RING_MASK) {
  677. case I915_EXEC_DEFAULT:
  678. case I915_EXEC_RENDER:
  679. ring = &dev_priv->ring[RCS];
  680. break;
  681. case I915_EXEC_BSD:
  682. ring = &dev_priv->ring[VCS];
  683. if (ctx_id != 0) {
  684. DRM_DEBUG("Ring %s doesn't support contexts\n",
  685. ring->name);
  686. return -EPERM;
  687. }
  688. break;
  689. case I915_EXEC_BLT:
  690. ring = &dev_priv->ring[BCS];
  691. if (ctx_id != 0) {
  692. DRM_DEBUG("Ring %s doesn't support contexts\n",
  693. ring->name);
  694. return -EPERM;
  695. }
  696. break;
  697. default:
  698. DRM_DEBUG("execbuf with unknown ring: %d\n",
  699. (int)(args->flags & I915_EXEC_RING_MASK));
  700. return -EINVAL;
  701. }
  702. if (!intel_ring_initialized(ring)) {
  703. DRM_DEBUG("execbuf with invalid ring: %d\n",
  704. (int)(args->flags & I915_EXEC_RING_MASK));
  705. return -EINVAL;
  706. }
  707. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  708. mask = I915_EXEC_CONSTANTS_MASK;
  709. switch (mode) {
  710. case I915_EXEC_CONSTANTS_REL_GENERAL:
  711. case I915_EXEC_CONSTANTS_ABSOLUTE:
  712. case I915_EXEC_CONSTANTS_REL_SURFACE:
  713. if (ring == &dev_priv->ring[RCS] &&
  714. mode != dev_priv->relative_constants_mode) {
  715. if (INTEL_INFO(dev)->gen < 4)
  716. return -EINVAL;
  717. if (INTEL_INFO(dev)->gen > 5 &&
  718. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  719. return -EINVAL;
  720. /* The HW changed the meaning on this bit on gen6 */
  721. if (INTEL_INFO(dev)->gen >= 6)
  722. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  723. }
  724. break;
  725. default:
  726. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  727. return -EINVAL;
  728. }
  729. if (args->buffer_count < 1) {
  730. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  731. return -EINVAL;
  732. }
  733. if (args->num_cliprects != 0) {
  734. if (ring != &dev_priv->ring[RCS]) {
  735. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  736. return -EINVAL;
  737. }
  738. if (INTEL_INFO(dev)->gen >= 5) {
  739. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  740. return -EINVAL;
  741. }
  742. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  743. DRM_DEBUG("execbuf with %u cliprects\n",
  744. args->num_cliprects);
  745. return -EINVAL;
  746. }
  747. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  748. GFP_KERNEL);
  749. if (cliprects == NULL) {
  750. ret = -ENOMEM;
  751. goto pre_mutex_err;
  752. }
  753. if (copy_from_user(cliprects,
  754. (struct drm_clip_rect __user *)(uintptr_t)
  755. args->cliprects_ptr,
  756. sizeof(*cliprects)*args->num_cliprects)) {
  757. ret = -EFAULT;
  758. goto pre_mutex_err;
  759. }
  760. }
  761. ret = i915_mutex_lock_interruptible(dev);
  762. if (ret)
  763. goto pre_mutex_err;
  764. if (dev_priv->mm.suspended) {
  765. mutex_unlock(&dev->struct_mutex);
  766. ret = -EBUSY;
  767. goto pre_mutex_err;
  768. }
  769. eb = eb_create(args->buffer_count);
  770. if (eb == NULL) {
  771. mutex_unlock(&dev->struct_mutex);
  772. ret = -ENOMEM;
  773. goto pre_mutex_err;
  774. }
  775. /* Look up object handles */
  776. INIT_LIST_HEAD(&objects);
  777. for (i = 0; i < args->buffer_count; i++) {
  778. struct drm_i915_gem_object *obj;
  779. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  780. exec[i].handle));
  781. if (&obj->base == NULL) {
  782. DRM_DEBUG("Invalid object handle %d at index %d\n",
  783. exec[i].handle, i);
  784. /* prevent error path from reading uninitialized data */
  785. ret = -ENOENT;
  786. goto err;
  787. }
  788. if (!list_empty(&obj->exec_list)) {
  789. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  790. obj, exec[i].handle, i);
  791. ret = -EINVAL;
  792. goto err;
  793. }
  794. list_add_tail(&obj->exec_list, &objects);
  795. obj->exec_handle = exec[i].handle;
  796. obj->exec_entry = &exec[i];
  797. eb_add_object(eb, obj);
  798. }
  799. /* take note of the batch buffer before we might reorder the lists */
  800. batch_obj = list_entry(objects.prev,
  801. struct drm_i915_gem_object,
  802. exec_list);
  803. /* Move the objects en-masse into the GTT, evicting if necessary. */
  804. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  805. if (ret)
  806. goto err;
  807. /* The objects are in their final locations, apply the relocations. */
  808. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  809. if (ret) {
  810. if (ret == -EFAULT) {
  811. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  812. &objects, eb,
  813. exec,
  814. args->buffer_count);
  815. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  816. }
  817. if (ret)
  818. goto err;
  819. }
  820. /* Set the pending read domains for the batch buffer to COMMAND */
  821. if (batch_obj->base.pending_write_domain) {
  822. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  823. ret = -EINVAL;
  824. goto err;
  825. }
  826. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  827. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  828. * batch" bit. Hence we need to pin secure batches into the global gtt.
  829. * hsw should have this fixed, but let's be paranoid and do it
  830. * unconditionally for now. */
  831. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  832. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  833. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  834. if (ret)
  835. goto err;
  836. ret = i915_switch_context(ring, file, ctx_id);
  837. if (ret)
  838. goto err;
  839. if (ring == &dev_priv->ring[RCS] &&
  840. mode != dev_priv->relative_constants_mode) {
  841. ret = intel_ring_begin(ring, 4);
  842. if (ret)
  843. goto err;
  844. intel_ring_emit(ring, MI_NOOP);
  845. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  846. intel_ring_emit(ring, INSTPM);
  847. intel_ring_emit(ring, mask << 16 | mode);
  848. intel_ring_advance(ring);
  849. dev_priv->relative_constants_mode = mode;
  850. }
  851. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  852. ret = i915_reset_gen7_sol_offsets(dev, ring);
  853. if (ret)
  854. goto err;
  855. }
  856. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  857. exec_len = args->batch_len;
  858. if (cliprects) {
  859. for (i = 0; i < args->num_cliprects; i++) {
  860. ret = i915_emit_box(dev, &cliprects[i],
  861. args->DR1, args->DR4);
  862. if (ret)
  863. goto err;
  864. ret = ring->dispatch_execbuffer(ring,
  865. exec_start, exec_len,
  866. flags);
  867. if (ret)
  868. goto err;
  869. }
  870. } else {
  871. ret = ring->dispatch_execbuffer(ring,
  872. exec_start, exec_len,
  873. flags);
  874. if (ret)
  875. goto err;
  876. }
  877. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  878. i915_gem_execbuffer_move_to_active(&objects, ring);
  879. i915_gem_execbuffer_retire_commands(dev, file, ring);
  880. err:
  881. eb_destroy(eb);
  882. while (!list_empty(&objects)) {
  883. struct drm_i915_gem_object *obj;
  884. obj = list_first_entry(&objects,
  885. struct drm_i915_gem_object,
  886. exec_list);
  887. list_del_init(&obj->exec_list);
  888. drm_gem_object_unreference(&obj->base);
  889. }
  890. mutex_unlock(&dev->struct_mutex);
  891. pre_mutex_err:
  892. kfree(cliprects);
  893. return ret;
  894. }
  895. /*
  896. * Legacy execbuffer just creates an exec2 list from the original exec object
  897. * list array and passes it to the real function.
  898. */
  899. int
  900. i915_gem_execbuffer(struct drm_device *dev, void *data,
  901. struct drm_file *file)
  902. {
  903. struct drm_i915_gem_execbuffer *args = data;
  904. struct drm_i915_gem_execbuffer2 exec2;
  905. struct drm_i915_gem_exec_object *exec_list = NULL;
  906. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  907. int ret, i;
  908. if (args->buffer_count < 1) {
  909. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  910. return -EINVAL;
  911. }
  912. /* Copy in the exec list from userland */
  913. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  914. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  915. if (exec_list == NULL || exec2_list == NULL) {
  916. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  917. args->buffer_count);
  918. drm_free_large(exec_list);
  919. drm_free_large(exec2_list);
  920. return -ENOMEM;
  921. }
  922. ret = copy_from_user(exec_list,
  923. (void __user *)(uintptr_t)args->buffers_ptr,
  924. sizeof(*exec_list) * args->buffer_count);
  925. if (ret != 0) {
  926. DRM_DEBUG("copy %d exec entries failed %d\n",
  927. args->buffer_count, ret);
  928. drm_free_large(exec_list);
  929. drm_free_large(exec2_list);
  930. return -EFAULT;
  931. }
  932. for (i = 0; i < args->buffer_count; i++) {
  933. exec2_list[i].handle = exec_list[i].handle;
  934. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  935. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  936. exec2_list[i].alignment = exec_list[i].alignment;
  937. exec2_list[i].offset = exec_list[i].offset;
  938. if (INTEL_INFO(dev)->gen < 4)
  939. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  940. else
  941. exec2_list[i].flags = 0;
  942. }
  943. exec2.buffers_ptr = args->buffers_ptr;
  944. exec2.buffer_count = args->buffer_count;
  945. exec2.batch_start_offset = args->batch_start_offset;
  946. exec2.batch_len = args->batch_len;
  947. exec2.DR1 = args->DR1;
  948. exec2.DR4 = args->DR4;
  949. exec2.num_cliprects = args->num_cliprects;
  950. exec2.cliprects_ptr = args->cliprects_ptr;
  951. exec2.flags = I915_EXEC_RENDER;
  952. i915_execbuffer2_set_context_id(exec2, 0);
  953. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  954. if (!ret) {
  955. /* Copy the new buffer offsets back to the user's exec list. */
  956. for (i = 0; i < args->buffer_count; i++)
  957. exec_list[i].offset = exec2_list[i].offset;
  958. /* ... and back out to userspace */
  959. ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
  960. exec_list,
  961. sizeof(*exec_list) * args->buffer_count);
  962. if (ret) {
  963. ret = -EFAULT;
  964. DRM_DEBUG("failed to copy %d exec entries "
  965. "back to user (%d)\n",
  966. args->buffer_count, ret);
  967. }
  968. }
  969. drm_free_large(exec_list);
  970. drm_free_large(exec2_list);
  971. return ret;
  972. }
  973. int
  974. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  975. struct drm_file *file)
  976. {
  977. struct drm_i915_gem_execbuffer2 *args = data;
  978. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  979. int ret;
  980. if (args->buffer_count < 1 ||
  981. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  982. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  983. return -EINVAL;
  984. }
  985. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  986. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  987. if (exec2_list == NULL)
  988. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  989. args->buffer_count);
  990. if (exec2_list == NULL) {
  991. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  992. args->buffer_count);
  993. return -ENOMEM;
  994. }
  995. ret = copy_from_user(exec2_list,
  996. (struct drm_i915_relocation_entry __user *)
  997. (uintptr_t) args->buffers_ptr,
  998. sizeof(*exec2_list) * args->buffer_count);
  999. if (ret != 0) {
  1000. DRM_DEBUG("copy %d exec entries failed %d\n",
  1001. args->buffer_count, ret);
  1002. drm_free_large(exec2_list);
  1003. return -EFAULT;
  1004. }
  1005. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1006. if (!ret) {
  1007. /* Copy the new buffer offsets back to the user's exec list. */
  1008. ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
  1009. exec2_list,
  1010. sizeof(*exec2_list) * args->buffer_count);
  1011. if (ret) {
  1012. ret = -EFAULT;
  1013. DRM_DEBUG("failed to copy %d exec entries "
  1014. "back to user (%d)\n",
  1015. args->buffer_count, ret);
  1016. }
  1017. }
  1018. drm_free_large(exec2_list);
  1019. return ret;
  1020. }