i915_gem_execbuffer.c 32 KB

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