i915_gem_execbuffer.c 32 KB

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