i915_gem_execbuffer.c 31 KB

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