i915_gem_execbuffer.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. /* The target buffer should have appeared before us in the
  106. * exec_object list, so it should have a GTT space bound by now.
  107. */
  108. if (unlikely(target_offset == 0)) {
  109. DRM_DEBUG("No GTT space found for object %d\n",
  110. reloc->target_handle);
  111. return ret;
  112. }
  113. /* Validate that the target is in a valid r/w GPU domain */
  114. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  115. DRM_DEBUG("reloc with multiple write domains: "
  116. "obj %p target %d offset %d "
  117. "read %08x write %08x",
  118. obj, reloc->target_handle,
  119. (int) reloc->offset,
  120. reloc->read_domains,
  121. reloc->write_domain);
  122. return ret;
  123. }
  124. if (unlikely((reloc->write_domain | reloc->read_domains)
  125. & ~I915_GEM_GPU_DOMAINS)) {
  126. DRM_DEBUG("reloc with read/write non-GPU domains: "
  127. "obj %p target %d offset %d "
  128. "read %08x write %08x",
  129. obj, reloc->target_handle,
  130. (int) reloc->offset,
  131. reloc->read_domains,
  132. reloc->write_domain);
  133. return ret;
  134. }
  135. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  136. reloc->write_domain != target_obj->pending_write_domain)) {
  137. DRM_DEBUG("Write domain conflict: "
  138. "obj %p target %d offset %d "
  139. "new %08x old %08x\n",
  140. obj, reloc->target_handle,
  141. (int) reloc->offset,
  142. reloc->write_domain,
  143. target_obj->pending_write_domain);
  144. return ret;
  145. }
  146. target_obj->pending_read_domains |= reloc->read_domains;
  147. target_obj->pending_write_domain |= reloc->write_domain;
  148. /* If the relocation already has the right value in it, no
  149. * more work needs to be done.
  150. */
  151. if (target_offset == reloc->presumed_offset)
  152. return 0;
  153. /* Check that the relocation address is valid... */
  154. if (unlikely(reloc->offset > obj->base.size - 4)) {
  155. DRM_DEBUG("Relocation beyond object bounds: "
  156. "obj %p target %d offset %d size %d.\n",
  157. obj, reloc->target_handle,
  158. (int) reloc->offset,
  159. (int) obj->base.size);
  160. return ret;
  161. }
  162. if (unlikely(reloc->offset & 3)) {
  163. DRM_DEBUG("Relocation not 4-byte aligned: "
  164. "obj %p target %d offset %d.\n",
  165. obj, reloc->target_handle,
  166. (int) reloc->offset);
  167. return ret;
  168. }
  169. /* We can't wait for rendering with pagefaults disabled */
  170. if (obj->active && in_atomic())
  171. return -EFAULT;
  172. reloc->delta += target_offset;
  173. if (use_cpu_reloc(obj)) {
  174. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  175. char *vaddr;
  176. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  177. if (ret)
  178. return ret;
  179. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  180. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  181. kunmap_atomic(vaddr);
  182. } else {
  183. struct drm_i915_private *dev_priv = dev->dev_private;
  184. uint32_t __iomem *reloc_entry;
  185. void __iomem *reloc_page;
  186. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  187. if (ret)
  188. return ret;
  189. ret = i915_gem_object_put_fence(obj);
  190. if (ret)
  191. return ret;
  192. /* Map the page containing the relocation we're going to perform. */
  193. reloc->offset += obj->gtt_offset;
  194. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  195. reloc->offset & PAGE_MASK);
  196. reloc_entry = (uint32_t __iomem *)
  197. (reloc_page + (reloc->offset & ~PAGE_MASK));
  198. iowrite32(reloc->delta, reloc_entry);
  199. io_mapping_unmap_atomic(reloc_page);
  200. }
  201. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  202. * pipe_control writes because the gpu doesn't properly redirect them
  203. * through the ppgtt for non_secure batchbuffers. */
  204. if (unlikely(IS_GEN6(dev) &&
  205. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  206. !target_i915_obj->has_global_gtt_mapping)) {
  207. i915_gem_gtt_bind_object(target_i915_obj,
  208. target_i915_obj->cache_level);
  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. ret = i915_gem_flush_ring(ring,
  597. I915_GEM_GPU_DOMAINS,
  598. ring->gpu_caches_dirty ? I915_GEM_GPU_DOMAINS : 0);
  599. if (ret)
  600. return ret;
  601. ring->gpu_caches_dirty = false;
  602. return 0;
  603. }
  604. static bool
  605. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  606. {
  607. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  608. }
  609. static int
  610. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  611. int count)
  612. {
  613. int i;
  614. for (i = 0; i < count; i++) {
  615. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  616. int length; /* limited by fault_in_pages_readable() */
  617. /* First check for malicious input causing overflow */
  618. if (exec[i].relocation_count >
  619. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  620. return -EINVAL;
  621. length = exec[i].relocation_count *
  622. sizeof(struct drm_i915_gem_relocation_entry);
  623. if (!access_ok(VERIFY_READ, ptr, length))
  624. return -EFAULT;
  625. /* we may also need to update the presumed offsets */
  626. if (!access_ok(VERIFY_WRITE, ptr, length))
  627. return -EFAULT;
  628. if (fault_in_multipages_readable(ptr, length))
  629. return -EFAULT;
  630. }
  631. return 0;
  632. }
  633. static void
  634. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  635. struct intel_ring_buffer *ring,
  636. u32 seqno)
  637. {
  638. struct drm_i915_gem_object *obj;
  639. list_for_each_entry(obj, objects, exec_list) {
  640. u32 old_read = obj->base.read_domains;
  641. u32 old_write = obj->base.write_domain;
  642. obj->base.read_domains = obj->base.pending_read_domains;
  643. obj->base.write_domain = obj->base.pending_write_domain;
  644. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  645. i915_gem_object_move_to_active(obj, ring, seqno);
  646. if (obj->base.write_domain) {
  647. obj->dirty = 1;
  648. obj->last_write_seqno = seqno;
  649. if (obj->pin_count) /* check for potential scanout */
  650. intel_mark_busy(ring->dev, obj);
  651. }
  652. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  653. }
  654. intel_mark_busy(ring->dev, NULL);
  655. }
  656. static void
  657. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  658. struct drm_file *file,
  659. struct intel_ring_buffer *ring)
  660. {
  661. /* Unconditionally force add_request to emit a full flush. */
  662. ring->gpu_caches_dirty = true;
  663. /* Add a breadcrumb for the completion of the batch buffer */
  664. (void)i915_add_request(ring, file, NULL);
  665. }
  666. static int
  667. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  668. struct intel_ring_buffer *ring)
  669. {
  670. drm_i915_private_t *dev_priv = dev->dev_private;
  671. int ret, i;
  672. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  673. return 0;
  674. ret = intel_ring_begin(ring, 4 * 3);
  675. if (ret)
  676. return ret;
  677. for (i = 0; i < 4; i++) {
  678. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  679. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  680. intel_ring_emit(ring, 0);
  681. }
  682. intel_ring_advance(ring);
  683. return 0;
  684. }
  685. static int
  686. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  687. struct drm_file *file,
  688. struct drm_i915_gem_execbuffer2 *args,
  689. struct drm_i915_gem_exec_object2 *exec)
  690. {
  691. drm_i915_private_t *dev_priv = dev->dev_private;
  692. struct list_head objects;
  693. struct eb_objects *eb;
  694. struct drm_i915_gem_object *batch_obj;
  695. struct drm_clip_rect *cliprects = NULL;
  696. struct intel_ring_buffer *ring;
  697. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  698. u32 exec_start, exec_len;
  699. u32 seqno;
  700. u32 mask;
  701. int ret, mode, i;
  702. if (!i915_gem_check_execbuffer(args)) {
  703. DRM_DEBUG("execbuf with invalid offset/length\n");
  704. return -EINVAL;
  705. }
  706. ret = validate_exec_list(exec, args->buffer_count);
  707. if (ret)
  708. return ret;
  709. switch (args->flags & I915_EXEC_RING_MASK) {
  710. case I915_EXEC_DEFAULT:
  711. case I915_EXEC_RENDER:
  712. ring = &dev_priv->ring[RCS];
  713. break;
  714. case I915_EXEC_BSD:
  715. ring = &dev_priv->ring[VCS];
  716. if (ctx_id != 0) {
  717. DRM_DEBUG("Ring %s doesn't support contexts\n",
  718. ring->name);
  719. return -EPERM;
  720. }
  721. break;
  722. case I915_EXEC_BLT:
  723. ring = &dev_priv->ring[BCS];
  724. if (ctx_id != 0) {
  725. DRM_DEBUG("Ring %s doesn't support contexts\n",
  726. ring->name);
  727. return -EPERM;
  728. }
  729. break;
  730. default:
  731. DRM_DEBUG("execbuf with unknown ring: %d\n",
  732. (int)(args->flags & I915_EXEC_RING_MASK));
  733. return -EINVAL;
  734. }
  735. if (!intel_ring_initialized(ring)) {
  736. DRM_DEBUG("execbuf with invalid ring: %d\n",
  737. (int)(args->flags & I915_EXEC_RING_MASK));
  738. return -EINVAL;
  739. }
  740. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  741. mask = I915_EXEC_CONSTANTS_MASK;
  742. switch (mode) {
  743. case I915_EXEC_CONSTANTS_REL_GENERAL:
  744. case I915_EXEC_CONSTANTS_ABSOLUTE:
  745. case I915_EXEC_CONSTANTS_REL_SURFACE:
  746. if (ring == &dev_priv->ring[RCS] &&
  747. mode != dev_priv->relative_constants_mode) {
  748. if (INTEL_INFO(dev)->gen < 4)
  749. return -EINVAL;
  750. if (INTEL_INFO(dev)->gen > 5 &&
  751. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  752. return -EINVAL;
  753. /* The HW changed the meaning on this bit on gen6 */
  754. if (INTEL_INFO(dev)->gen >= 6)
  755. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  756. }
  757. break;
  758. default:
  759. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  760. return -EINVAL;
  761. }
  762. if (args->buffer_count < 1) {
  763. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  764. return -EINVAL;
  765. }
  766. if (args->num_cliprects != 0) {
  767. if (ring != &dev_priv->ring[RCS]) {
  768. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  769. return -EINVAL;
  770. }
  771. if (INTEL_INFO(dev)->gen >= 5) {
  772. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  773. return -EINVAL;
  774. }
  775. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  776. DRM_DEBUG("execbuf with %u cliprects\n",
  777. args->num_cliprects);
  778. return -EINVAL;
  779. }
  780. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  781. GFP_KERNEL);
  782. if (cliprects == NULL) {
  783. ret = -ENOMEM;
  784. goto pre_mutex_err;
  785. }
  786. if (copy_from_user(cliprects,
  787. (struct drm_clip_rect __user *)(uintptr_t)
  788. args->cliprects_ptr,
  789. sizeof(*cliprects)*args->num_cliprects)) {
  790. ret = -EFAULT;
  791. goto pre_mutex_err;
  792. }
  793. }
  794. ret = i915_mutex_lock_interruptible(dev);
  795. if (ret)
  796. goto pre_mutex_err;
  797. if (dev_priv->mm.suspended) {
  798. mutex_unlock(&dev->struct_mutex);
  799. ret = -EBUSY;
  800. goto pre_mutex_err;
  801. }
  802. eb = eb_create(args->buffer_count);
  803. if (eb == NULL) {
  804. mutex_unlock(&dev->struct_mutex);
  805. ret = -ENOMEM;
  806. goto pre_mutex_err;
  807. }
  808. /* Look up object handles */
  809. INIT_LIST_HEAD(&objects);
  810. for (i = 0; i < args->buffer_count; i++) {
  811. struct drm_i915_gem_object *obj;
  812. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  813. exec[i].handle));
  814. if (&obj->base == NULL) {
  815. DRM_DEBUG("Invalid object handle %d at index %d\n",
  816. exec[i].handle, i);
  817. /* prevent error path from reading uninitialized data */
  818. ret = -ENOENT;
  819. goto err;
  820. }
  821. if (!list_empty(&obj->exec_list)) {
  822. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  823. obj, exec[i].handle, i);
  824. ret = -EINVAL;
  825. goto err;
  826. }
  827. list_add_tail(&obj->exec_list, &objects);
  828. obj->exec_handle = exec[i].handle;
  829. obj->exec_entry = &exec[i];
  830. eb_add_object(eb, obj);
  831. }
  832. /* take note of the batch buffer before we might reorder the lists */
  833. batch_obj = list_entry(objects.prev,
  834. struct drm_i915_gem_object,
  835. exec_list);
  836. /* Move the objects en-masse into the GTT, evicting if necessary. */
  837. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  838. if (ret)
  839. goto err;
  840. /* The objects are in their final locations, apply the relocations. */
  841. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  842. if (ret) {
  843. if (ret == -EFAULT) {
  844. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  845. &objects, eb,
  846. exec,
  847. args->buffer_count);
  848. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  849. }
  850. if (ret)
  851. goto err;
  852. }
  853. /* Set the pending read domains for the batch buffer to COMMAND */
  854. if (batch_obj->base.pending_write_domain) {
  855. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  856. ret = -EINVAL;
  857. goto err;
  858. }
  859. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  860. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  861. if (ret)
  862. goto err;
  863. seqno = i915_gem_next_request_seqno(ring);
  864. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  865. if (seqno < ring->sync_seqno[i]) {
  866. /* The GPU can not handle its semaphore value wrapping,
  867. * so every billion or so execbuffers, we need to stall
  868. * the GPU in order to reset the counters.
  869. */
  870. ret = i915_gpu_idle(dev);
  871. if (ret)
  872. goto err;
  873. i915_gem_retire_requests(dev);
  874. BUG_ON(ring->sync_seqno[i]);
  875. }
  876. }
  877. ret = i915_switch_context(ring, file, ctx_id);
  878. if (ret)
  879. goto err;
  880. if (ring == &dev_priv->ring[RCS] &&
  881. mode != dev_priv->relative_constants_mode) {
  882. ret = intel_ring_begin(ring, 4);
  883. if (ret)
  884. goto err;
  885. intel_ring_emit(ring, MI_NOOP);
  886. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  887. intel_ring_emit(ring, INSTPM);
  888. intel_ring_emit(ring, mask << 16 | mode);
  889. intel_ring_advance(ring);
  890. dev_priv->relative_constants_mode = mode;
  891. }
  892. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  893. ret = i915_reset_gen7_sol_offsets(dev, ring);
  894. if (ret)
  895. goto err;
  896. }
  897. trace_i915_gem_ring_dispatch(ring, seqno);
  898. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  899. exec_len = args->batch_len;
  900. if (cliprects) {
  901. for (i = 0; i < args->num_cliprects; i++) {
  902. ret = i915_emit_box(dev, &cliprects[i],
  903. args->DR1, args->DR4);
  904. if (ret)
  905. goto err;
  906. ret = ring->dispatch_execbuffer(ring,
  907. exec_start, exec_len);
  908. if (ret)
  909. goto err;
  910. }
  911. } else {
  912. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  913. if (ret)
  914. goto err;
  915. }
  916. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  917. i915_gem_execbuffer_retire_commands(dev, file, ring);
  918. err:
  919. eb_destroy(eb);
  920. while (!list_empty(&objects)) {
  921. struct drm_i915_gem_object *obj;
  922. obj = list_first_entry(&objects,
  923. struct drm_i915_gem_object,
  924. exec_list);
  925. list_del_init(&obj->exec_list);
  926. drm_gem_object_unreference(&obj->base);
  927. }
  928. mutex_unlock(&dev->struct_mutex);
  929. pre_mutex_err:
  930. kfree(cliprects);
  931. return ret;
  932. }
  933. /*
  934. * Legacy execbuffer just creates an exec2 list from the original exec object
  935. * list array and passes it to the real function.
  936. */
  937. int
  938. i915_gem_execbuffer(struct drm_device *dev, void *data,
  939. struct drm_file *file)
  940. {
  941. struct drm_i915_gem_execbuffer *args = data;
  942. struct drm_i915_gem_execbuffer2 exec2;
  943. struct drm_i915_gem_exec_object *exec_list = NULL;
  944. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  945. int ret, i;
  946. if (args->buffer_count < 1) {
  947. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  948. return -EINVAL;
  949. }
  950. /* Copy in the exec list from userland */
  951. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  952. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  953. if (exec_list == NULL || exec2_list == NULL) {
  954. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  955. args->buffer_count);
  956. drm_free_large(exec_list);
  957. drm_free_large(exec2_list);
  958. return -ENOMEM;
  959. }
  960. ret = copy_from_user(exec_list,
  961. (struct drm_i915_relocation_entry __user *)
  962. (uintptr_t) args->buffers_ptr,
  963. sizeof(*exec_list) * args->buffer_count);
  964. if (ret != 0) {
  965. DRM_DEBUG("copy %d exec entries failed %d\n",
  966. args->buffer_count, ret);
  967. drm_free_large(exec_list);
  968. drm_free_large(exec2_list);
  969. return -EFAULT;
  970. }
  971. for (i = 0; i < args->buffer_count; i++) {
  972. exec2_list[i].handle = exec_list[i].handle;
  973. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  974. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  975. exec2_list[i].alignment = exec_list[i].alignment;
  976. exec2_list[i].offset = exec_list[i].offset;
  977. if (INTEL_INFO(dev)->gen < 4)
  978. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  979. else
  980. exec2_list[i].flags = 0;
  981. }
  982. exec2.buffers_ptr = args->buffers_ptr;
  983. exec2.buffer_count = args->buffer_count;
  984. exec2.batch_start_offset = args->batch_start_offset;
  985. exec2.batch_len = args->batch_len;
  986. exec2.DR1 = args->DR1;
  987. exec2.DR4 = args->DR4;
  988. exec2.num_cliprects = args->num_cliprects;
  989. exec2.cliprects_ptr = args->cliprects_ptr;
  990. exec2.flags = I915_EXEC_RENDER;
  991. i915_execbuffer2_set_context_id(exec2, 0);
  992. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  993. if (!ret) {
  994. /* Copy the new buffer offsets back to the user's exec list. */
  995. for (i = 0; i < args->buffer_count; i++)
  996. exec_list[i].offset = exec2_list[i].offset;
  997. /* ... and back out to userspace */
  998. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  999. (uintptr_t) args->buffers_ptr,
  1000. exec_list,
  1001. sizeof(*exec_list) * args->buffer_count);
  1002. if (ret) {
  1003. ret = -EFAULT;
  1004. DRM_DEBUG("failed to copy %d exec entries "
  1005. "back to user (%d)\n",
  1006. args->buffer_count, ret);
  1007. }
  1008. }
  1009. drm_free_large(exec_list);
  1010. drm_free_large(exec2_list);
  1011. return ret;
  1012. }
  1013. int
  1014. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1015. struct drm_file *file)
  1016. {
  1017. struct drm_i915_gem_execbuffer2 *args = data;
  1018. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1019. int ret;
  1020. if (args->buffer_count < 1 ||
  1021. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1022. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1023. return -EINVAL;
  1024. }
  1025. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1026. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1027. if (exec2_list == NULL)
  1028. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1029. args->buffer_count);
  1030. if (exec2_list == NULL) {
  1031. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1032. args->buffer_count);
  1033. return -ENOMEM;
  1034. }
  1035. ret = copy_from_user(exec2_list,
  1036. (struct drm_i915_relocation_entry __user *)
  1037. (uintptr_t) args->buffers_ptr,
  1038. sizeof(*exec2_list) * args->buffer_count);
  1039. if (ret != 0) {
  1040. DRM_DEBUG("copy %d exec entries failed %d\n",
  1041. args->buffer_count, ret);
  1042. drm_free_large(exec2_list);
  1043. return -EFAULT;
  1044. }
  1045. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1046. if (!ret) {
  1047. /* Copy the new buffer offsets back to the user's exec list. */
  1048. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1049. (uintptr_t) args->buffers_ptr,
  1050. exec2_list,
  1051. sizeof(*exec2_list) * args->buffer_count);
  1052. if (ret) {
  1053. ret = -EFAULT;
  1054. DRM_DEBUG("failed to copy %d exec entries "
  1055. "back to user (%d)\n",
  1056. args->buffer_count, ret);
  1057. }
  1058. }
  1059. drm_free_large(exec2_list);
  1060. return ret;
  1061. }