i915_gem_execbuffer.c 32 KB

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