i915_gem_execbuffer.c 30 KB

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