i915_gem_execbuffer.c 32 KB

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