i915_gem_execbuffer.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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 drm_file *file,
  404. struct list_head *objects,
  405. bool *need_relocs)
  406. {
  407. struct drm_i915_gem_object *obj;
  408. struct list_head ordered_objects;
  409. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  410. int retry;
  411. INIT_LIST_HEAD(&ordered_objects);
  412. while (!list_empty(objects)) {
  413. struct drm_i915_gem_exec_object2 *entry;
  414. bool need_fence, need_mappable;
  415. obj = list_first_entry(objects,
  416. struct drm_i915_gem_object,
  417. exec_list);
  418. entry = obj->exec_entry;
  419. need_fence =
  420. has_fenced_gpu_access &&
  421. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  422. obj->tiling_mode != I915_TILING_NONE;
  423. need_mappable = need_fence || need_reloc_mappable(obj);
  424. if (need_mappable)
  425. list_move(&obj->exec_list, &ordered_objects);
  426. else
  427. list_move_tail(&obj->exec_list, &ordered_objects);
  428. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  429. obj->base.pending_write_domain = 0;
  430. obj->pending_fenced_gpu_access = false;
  431. }
  432. list_splice(&ordered_objects, objects);
  433. /* Attempt to pin all of the buffers into the GTT.
  434. * This is done in 3 phases:
  435. *
  436. * 1a. Unbind all objects that do not match the GTT constraints for
  437. * the execbuffer (fenceable, mappable, alignment etc).
  438. * 1b. Increment pin count for already bound objects.
  439. * 2. Bind new objects.
  440. * 3. Decrement pin count.
  441. *
  442. * This avoid unnecessary unbinding of later objects in order to make
  443. * room for the earlier objects *unless* we need to defragment.
  444. */
  445. retry = 0;
  446. do {
  447. int ret = 0;
  448. /* Unbind any ill-fitting objects or pin. */
  449. list_for_each_entry(obj, objects, exec_list) {
  450. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  451. bool need_fence, need_mappable;
  452. if (!obj->gtt_space)
  453. continue;
  454. need_fence =
  455. has_fenced_gpu_access &&
  456. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  457. obj->tiling_mode != I915_TILING_NONE;
  458. need_mappable = need_fence || need_reloc_mappable(obj);
  459. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  460. (need_mappable && !obj->map_and_fenceable))
  461. ret = i915_gem_object_unbind(obj);
  462. else
  463. ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs);
  464. if (ret)
  465. goto err;
  466. }
  467. /* Bind fresh objects */
  468. list_for_each_entry(obj, objects, exec_list) {
  469. if (obj->gtt_space)
  470. continue;
  471. ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs);
  472. if (ret)
  473. goto err;
  474. }
  475. err: /* Decrement pin count for bound objects */
  476. list_for_each_entry(obj, objects, exec_list)
  477. i915_gem_execbuffer_unreserve_object(obj);
  478. if (ret != -ENOSPC || retry++)
  479. return ret;
  480. ret = i915_gem_evict_everything(ring->dev);
  481. if (ret)
  482. return ret;
  483. } while (1);
  484. }
  485. static int
  486. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  487. struct drm_i915_gem_execbuffer2 *args,
  488. struct drm_file *file,
  489. struct intel_ring_buffer *ring,
  490. struct eb_objects *eb,
  491. struct drm_i915_gem_exec_object2 *exec)
  492. {
  493. struct drm_i915_gem_relocation_entry *reloc;
  494. struct drm_i915_gem_object *obj;
  495. bool need_relocs;
  496. int *reloc_offset;
  497. int i, total, ret;
  498. int count = args->buffer_count;
  499. /* We may process another execbuffer during the unlock... */
  500. while (!list_empty(&eb->objects)) {
  501. obj = list_first_entry(&eb->objects,
  502. struct drm_i915_gem_object,
  503. exec_list);
  504. list_del_init(&obj->exec_list);
  505. drm_gem_object_unreference(&obj->base);
  506. }
  507. mutex_unlock(&dev->struct_mutex);
  508. total = 0;
  509. for (i = 0; i < count; i++)
  510. total += exec[i].relocation_count;
  511. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  512. reloc = drm_malloc_ab(total, sizeof(*reloc));
  513. if (reloc == NULL || reloc_offset == NULL) {
  514. drm_free_large(reloc);
  515. drm_free_large(reloc_offset);
  516. mutex_lock(&dev->struct_mutex);
  517. return -ENOMEM;
  518. }
  519. total = 0;
  520. for (i = 0; i < count; i++) {
  521. struct drm_i915_gem_relocation_entry __user *user_relocs;
  522. u64 invalid_offset = (u64)-1;
  523. int j;
  524. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  525. if (copy_from_user(reloc+total, user_relocs,
  526. exec[i].relocation_count * sizeof(*reloc))) {
  527. ret = -EFAULT;
  528. mutex_lock(&dev->struct_mutex);
  529. goto err;
  530. }
  531. /* As we do not update the known relocation offsets after
  532. * relocating (due to the complexities in lock handling),
  533. * we need to mark them as invalid now so that we force the
  534. * relocation processing next time. Just in case the target
  535. * object is evicted and then rebound into its old
  536. * presumed_offset before the next execbuffer - if that
  537. * happened we would make the mistake of assuming that the
  538. * relocations were valid.
  539. */
  540. for (j = 0; j < exec[i].relocation_count; j++) {
  541. if (copy_to_user(&user_relocs[j].presumed_offset,
  542. &invalid_offset,
  543. sizeof(invalid_offset))) {
  544. ret = -EFAULT;
  545. mutex_lock(&dev->struct_mutex);
  546. goto err;
  547. }
  548. }
  549. reloc_offset[i] = total;
  550. total += exec[i].relocation_count;
  551. }
  552. ret = i915_mutex_lock_interruptible(dev);
  553. if (ret) {
  554. mutex_lock(&dev->struct_mutex);
  555. goto err;
  556. }
  557. /* reacquire the objects */
  558. eb_reset(eb);
  559. ret = eb_lookup_objects(eb, exec, args, file);
  560. if (ret)
  561. goto err;
  562. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  563. ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects, &need_relocs);
  564. if (ret)
  565. goto err;
  566. list_for_each_entry(obj, &eb->objects, exec_list) {
  567. int offset = obj->exec_entry - exec;
  568. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  569. reloc + reloc_offset[offset]);
  570. if (ret)
  571. goto err;
  572. }
  573. /* Leave the user relocations as are, this is the painfully slow path,
  574. * and we want to avoid the complication of dropping the lock whilst
  575. * having buffers reserved in the aperture and so causing spurious
  576. * ENOSPC for random operations.
  577. */
  578. err:
  579. drm_free_large(reloc);
  580. drm_free_large(reloc_offset);
  581. return ret;
  582. }
  583. static int
  584. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  585. struct list_head *objects)
  586. {
  587. struct drm_i915_gem_object *obj;
  588. uint32_t flush_domains = 0;
  589. int ret;
  590. list_for_each_entry(obj, objects, exec_list) {
  591. ret = i915_gem_object_sync(obj, ring);
  592. if (ret)
  593. return ret;
  594. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  595. i915_gem_clflush_object(obj);
  596. flush_domains |= obj->base.write_domain;
  597. }
  598. if (flush_domains & I915_GEM_DOMAIN_CPU)
  599. i915_gem_chipset_flush(ring->dev);
  600. if (flush_domains & I915_GEM_DOMAIN_GTT)
  601. wmb();
  602. /* Unconditionally invalidate gpu caches and ensure that we do flush
  603. * any residual writes from the previous batch.
  604. */
  605. return intel_ring_invalidate_all_caches(ring);
  606. }
  607. static bool
  608. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  609. {
  610. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  611. return false;
  612. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  613. }
  614. static int
  615. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  616. int count)
  617. {
  618. int i;
  619. for (i = 0; i < count; i++) {
  620. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  621. int length; /* limited by fault_in_pages_readable() */
  622. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  623. return -EINVAL;
  624. /* First check for malicious input causing overflow */
  625. if (exec[i].relocation_count >
  626. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  627. return -EINVAL;
  628. length = exec[i].relocation_count *
  629. sizeof(struct drm_i915_gem_relocation_entry);
  630. /* we may also need to update the presumed offsets */
  631. if (!access_ok(VERIFY_WRITE, ptr, length))
  632. return -EFAULT;
  633. if (fault_in_multipages_readable(ptr, length))
  634. return -EFAULT;
  635. }
  636. return 0;
  637. }
  638. static void
  639. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  640. struct intel_ring_buffer *ring)
  641. {
  642. struct drm_i915_gem_object *obj;
  643. list_for_each_entry(obj, objects, exec_list) {
  644. u32 old_read = obj->base.read_domains;
  645. u32 old_write = obj->base.write_domain;
  646. obj->base.write_domain = obj->base.pending_write_domain;
  647. if (obj->base.write_domain == 0)
  648. obj->base.pending_read_domains |= obj->base.read_domains;
  649. obj->base.read_domains = obj->base.pending_read_domains;
  650. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  651. i915_gem_object_move_to_active(obj, ring);
  652. if (obj->base.write_domain) {
  653. obj->dirty = 1;
  654. obj->last_write_seqno = intel_ring_get_seqno(ring);
  655. if (obj->pin_count) /* check for potential scanout */
  656. intel_mark_fb_busy(obj);
  657. }
  658. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  659. }
  660. }
  661. static void
  662. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  663. struct drm_file *file,
  664. struct intel_ring_buffer *ring)
  665. {
  666. /* Unconditionally force add_request to emit a full flush. */
  667. ring->gpu_caches_dirty = true;
  668. /* Add a breadcrumb for the completion of the batch buffer */
  669. (void)i915_add_request(ring, file, NULL);
  670. }
  671. static int
  672. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  673. struct intel_ring_buffer *ring)
  674. {
  675. drm_i915_private_t *dev_priv = dev->dev_private;
  676. int ret, i;
  677. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  678. return 0;
  679. ret = intel_ring_begin(ring, 4 * 3);
  680. if (ret)
  681. return ret;
  682. for (i = 0; i < 4; i++) {
  683. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  684. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  685. intel_ring_emit(ring, 0);
  686. }
  687. intel_ring_advance(ring);
  688. return 0;
  689. }
  690. static int
  691. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  692. struct drm_file *file,
  693. struct drm_i915_gem_execbuffer2 *args,
  694. struct drm_i915_gem_exec_object2 *exec)
  695. {
  696. drm_i915_private_t *dev_priv = dev->dev_private;
  697. struct eb_objects *eb;
  698. struct drm_i915_gem_object *batch_obj;
  699. struct drm_clip_rect *cliprects = NULL;
  700. struct intel_ring_buffer *ring;
  701. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  702. u32 exec_start, exec_len;
  703. u32 mask, flags;
  704. int ret, mode, i;
  705. bool need_relocs;
  706. if (!i915_gem_check_execbuffer(args))
  707. return -EINVAL;
  708. ret = validate_exec_list(exec, args->buffer_count);
  709. if (ret)
  710. return ret;
  711. flags = 0;
  712. if (args->flags & I915_EXEC_SECURE) {
  713. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  714. return -EPERM;
  715. flags |= I915_DISPATCH_SECURE;
  716. }
  717. if (args->flags & I915_EXEC_IS_PINNED)
  718. flags |= I915_DISPATCH_PINNED;
  719. switch (args->flags & I915_EXEC_RING_MASK) {
  720. case I915_EXEC_DEFAULT:
  721. case I915_EXEC_RENDER:
  722. ring = &dev_priv->ring[RCS];
  723. break;
  724. case I915_EXEC_BSD:
  725. ring = &dev_priv->ring[VCS];
  726. if (ctx_id != 0) {
  727. DRM_DEBUG("Ring %s doesn't support contexts\n",
  728. ring->name);
  729. return -EPERM;
  730. }
  731. break;
  732. case I915_EXEC_BLT:
  733. ring = &dev_priv->ring[BCS];
  734. if (ctx_id != 0) {
  735. DRM_DEBUG("Ring %s doesn't support contexts\n",
  736. ring->name);
  737. return -EPERM;
  738. }
  739. break;
  740. default:
  741. DRM_DEBUG("execbuf with unknown ring: %d\n",
  742. (int)(args->flags & I915_EXEC_RING_MASK));
  743. return -EINVAL;
  744. }
  745. if (!intel_ring_initialized(ring)) {
  746. DRM_DEBUG("execbuf with invalid ring: %d\n",
  747. (int)(args->flags & I915_EXEC_RING_MASK));
  748. return -EINVAL;
  749. }
  750. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  751. mask = I915_EXEC_CONSTANTS_MASK;
  752. switch (mode) {
  753. case I915_EXEC_CONSTANTS_REL_GENERAL:
  754. case I915_EXEC_CONSTANTS_ABSOLUTE:
  755. case I915_EXEC_CONSTANTS_REL_SURFACE:
  756. if (ring == &dev_priv->ring[RCS] &&
  757. mode != dev_priv->relative_constants_mode) {
  758. if (INTEL_INFO(dev)->gen < 4)
  759. return -EINVAL;
  760. if (INTEL_INFO(dev)->gen > 5 &&
  761. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  762. return -EINVAL;
  763. /* The HW changed the meaning on this bit on gen6 */
  764. if (INTEL_INFO(dev)->gen >= 6)
  765. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  766. }
  767. break;
  768. default:
  769. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  770. return -EINVAL;
  771. }
  772. if (args->buffer_count < 1) {
  773. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  774. return -EINVAL;
  775. }
  776. if (args->num_cliprects != 0) {
  777. if (ring != &dev_priv->ring[RCS]) {
  778. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  779. return -EINVAL;
  780. }
  781. if (INTEL_INFO(dev)->gen >= 5) {
  782. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  783. return -EINVAL;
  784. }
  785. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  786. DRM_DEBUG("execbuf with %u cliprects\n",
  787. args->num_cliprects);
  788. return -EINVAL;
  789. }
  790. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  791. GFP_KERNEL);
  792. if (cliprects == NULL) {
  793. ret = -ENOMEM;
  794. goto pre_mutex_err;
  795. }
  796. if (copy_from_user(cliprects,
  797. to_user_ptr(args->cliprects_ptr),
  798. sizeof(*cliprects)*args->num_cliprects)) {
  799. ret = -EFAULT;
  800. goto pre_mutex_err;
  801. }
  802. }
  803. ret = i915_mutex_lock_interruptible(dev);
  804. if (ret)
  805. goto pre_mutex_err;
  806. if (dev_priv->mm.suspended) {
  807. mutex_unlock(&dev->struct_mutex);
  808. ret = -EBUSY;
  809. goto pre_mutex_err;
  810. }
  811. eb = eb_create(args);
  812. if (eb == NULL) {
  813. mutex_unlock(&dev->struct_mutex);
  814. ret = -ENOMEM;
  815. goto pre_mutex_err;
  816. }
  817. /* Look up object handles */
  818. ret = eb_lookup_objects(eb, exec, args, file);
  819. if (ret)
  820. goto err;
  821. /* take note of the batch buffer before we might reorder the lists */
  822. batch_obj = list_entry(eb->objects.prev,
  823. struct drm_i915_gem_object,
  824. exec_list);
  825. /* Move the objects en-masse into the GTT, evicting if necessary. */
  826. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  827. ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects, &need_relocs);
  828. if (ret)
  829. goto err;
  830. /* The objects are in their final locations, apply the relocations. */
  831. if (need_relocs)
  832. ret = i915_gem_execbuffer_relocate(dev, eb);
  833. if (ret) {
  834. if (ret == -EFAULT) {
  835. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  836. eb, exec);
  837. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  838. }
  839. if (ret)
  840. goto err;
  841. }
  842. /* Set the pending read domains for the batch buffer to COMMAND */
  843. if (batch_obj->base.pending_write_domain) {
  844. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  845. ret = -EINVAL;
  846. goto err;
  847. }
  848. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  849. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  850. * batch" bit. Hence we need to pin secure batches into the global gtt.
  851. * hsw should have this fixed, but let's be paranoid and do it
  852. * unconditionally for now. */
  853. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  854. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  855. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects);
  856. if (ret)
  857. goto err;
  858. ret = i915_switch_context(ring, file, ctx_id);
  859. if (ret)
  860. goto err;
  861. if (ring == &dev_priv->ring[RCS] &&
  862. mode != dev_priv->relative_constants_mode) {
  863. ret = intel_ring_begin(ring, 4);
  864. if (ret)
  865. goto err;
  866. intel_ring_emit(ring, MI_NOOP);
  867. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  868. intel_ring_emit(ring, INSTPM);
  869. intel_ring_emit(ring, mask << 16 | mode);
  870. intel_ring_advance(ring);
  871. dev_priv->relative_constants_mode = mode;
  872. }
  873. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  874. ret = i915_reset_gen7_sol_offsets(dev, ring);
  875. if (ret)
  876. goto err;
  877. }
  878. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  879. exec_len = args->batch_len;
  880. if (cliprects) {
  881. for (i = 0; i < args->num_cliprects; i++) {
  882. ret = i915_emit_box(dev, &cliprects[i],
  883. args->DR1, args->DR4);
  884. if (ret)
  885. goto err;
  886. ret = ring->dispatch_execbuffer(ring,
  887. exec_start, exec_len,
  888. flags);
  889. if (ret)
  890. goto err;
  891. }
  892. } else {
  893. ret = ring->dispatch_execbuffer(ring,
  894. exec_start, exec_len,
  895. flags);
  896. if (ret)
  897. goto err;
  898. }
  899. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  900. i915_gem_execbuffer_move_to_active(&eb->objects, ring);
  901. i915_gem_execbuffer_retire_commands(dev, file, ring);
  902. err:
  903. eb_destroy(eb);
  904. mutex_unlock(&dev->struct_mutex);
  905. pre_mutex_err:
  906. kfree(cliprects);
  907. return ret;
  908. }
  909. /*
  910. * Legacy execbuffer just creates an exec2 list from the original exec object
  911. * list array and passes it to the real function.
  912. */
  913. int
  914. i915_gem_execbuffer(struct drm_device *dev, void *data,
  915. struct drm_file *file)
  916. {
  917. struct drm_i915_gem_execbuffer *args = data;
  918. struct drm_i915_gem_execbuffer2 exec2;
  919. struct drm_i915_gem_exec_object *exec_list = NULL;
  920. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  921. int ret, i;
  922. if (args->buffer_count < 1) {
  923. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  924. return -EINVAL;
  925. }
  926. /* Copy in the exec list from userland */
  927. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  928. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  929. if (exec_list == NULL || exec2_list == NULL) {
  930. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  931. args->buffer_count);
  932. drm_free_large(exec_list);
  933. drm_free_large(exec2_list);
  934. return -ENOMEM;
  935. }
  936. ret = copy_from_user(exec_list,
  937. to_user_ptr(args->buffers_ptr),
  938. sizeof(*exec_list) * args->buffer_count);
  939. if (ret != 0) {
  940. DRM_DEBUG("copy %d exec entries failed %d\n",
  941. args->buffer_count, ret);
  942. drm_free_large(exec_list);
  943. drm_free_large(exec2_list);
  944. return -EFAULT;
  945. }
  946. for (i = 0; i < args->buffer_count; i++) {
  947. exec2_list[i].handle = exec_list[i].handle;
  948. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  949. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  950. exec2_list[i].alignment = exec_list[i].alignment;
  951. exec2_list[i].offset = exec_list[i].offset;
  952. if (INTEL_INFO(dev)->gen < 4)
  953. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  954. else
  955. exec2_list[i].flags = 0;
  956. }
  957. exec2.buffers_ptr = args->buffers_ptr;
  958. exec2.buffer_count = args->buffer_count;
  959. exec2.batch_start_offset = args->batch_start_offset;
  960. exec2.batch_len = args->batch_len;
  961. exec2.DR1 = args->DR1;
  962. exec2.DR4 = args->DR4;
  963. exec2.num_cliprects = args->num_cliprects;
  964. exec2.cliprects_ptr = args->cliprects_ptr;
  965. exec2.flags = I915_EXEC_RENDER;
  966. i915_execbuffer2_set_context_id(exec2, 0);
  967. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  968. if (!ret) {
  969. /* Copy the new buffer offsets back to the user's exec list. */
  970. for (i = 0; i < args->buffer_count; i++)
  971. exec_list[i].offset = exec2_list[i].offset;
  972. /* ... and back out to userspace */
  973. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  974. exec_list,
  975. sizeof(*exec_list) * args->buffer_count);
  976. if (ret) {
  977. ret = -EFAULT;
  978. DRM_DEBUG("failed to copy %d exec entries "
  979. "back to user (%d)\n",
  980. args->buffer_count, ret);
  981. }
  982. }
  983. drm_free_large(exec_list);
  984. drm_free_large(exec2_list);
  985. return ret;
  986. }
  987. int
  988. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  989. struct drm_file *file)
  990. {
  991. struct drm_i915_gem_execbuffer2 *args = data;
  992. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  993. int ret;
  994. if (args->buffer_count < 1 ||
  995. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  996. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  997. return -EINVAL;
  998. }
  999. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1000. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1001. if (exec2_list == NULL)
  1002. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1003. args->buffer_count);
  1004. if (exec2_list == NULL) {
  1005. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1006. args->buffer_count);
  1007. return -ENOMEM;
  1008. }
  1009. ret = copy_from_user(exec2_list,
  1010. to_user_ptr(args->buffers_ptr),
  1011. sizeof(*exec2_list) * args->buffer_count);
  1012. if (ret != 0) {
  1013. DRM_DEBUG("copy %d exec entries failed %d\n",
  1014. args->buffer_count, ret);
  1015. drm_free_large(exec2_list);
  1016. return -EFAULT;
  1017. }
  1018. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1019. if (!ret) {
  1020. /* Copy the new buffer offsets back to the user's exec list. */
  1021. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1022. exec2_list,
  1023. sizeof(*exec2_list) * args->buffer_count);
  1024. if (ret) {
  1025. ret = -EFAULT;
  1026. DRM_DEBUG("failed to copy %d exec entries "
  1027. "back to user (%d)\n",
  1028. args->buffer_count, ret);
  1029. }
  1030. }
  1031. drm_free_large(exec2_list);
  1032. return ret;
  1033. }