i915_gem_execbuffer.c 32 KB

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