i915_gem_execbuffer.c 30 KB

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