i915_gem_execbuffer.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Copyright © 2008,2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "i915_drm.h"
  31. #include "i915_drv.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/dma_remapping.h>
  35. struct change_domains {
  36. uint32_t invalidate_domains;
  37. uint32_t flush_domains;
  38. uint32_t flush_rings;
  39. uint32_t flips;
  40. };
  41. /*
  42. * Set the next domain for the specified object. This
  43. * may not actually perform the necessary flushing/invaliding though,
  44. * as that may want to be batched with other set_domain operations
  45. *
  46. * This is (we hope) the only really tricky part of gem. The goal
  47. * is fairly simple -- track which caches hold bits of the object
  48. * and make sure they remain coherent. A few concrete examples may
  49. * help to explain how it works. For shorthand, we use the notation
  50. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  51. * a pair of read and write domain masks.
  52. *
  53. * Case 1: the batch buffer
  54. *
  55. * 1. Allocated
  56. * 2. Written by CPU
  57. * 3. Mapped to GTT
  58. * 4. Read by GPU
  59. * 5. Unmapped from GTT
  60. * 6. Freed
  61. *
  62. * Let's take these a step at a time
  63. *
  64. * 1. Allocated
  65. * Pages allocated from the kernel may still have
  66. * cache contents, so we set them to (CPU, CPU) always.
  67. * 2. Written by CPU (using pwrite)
  68. * The pwrite function calls set_domain (CPU, CPU) and
  69. * this function does nothing (as nothing changes)
  70. * 3. Mapped by GTT
  71. * This function asserts that the object is not
  72. * currently in any GPU-based read or write domains
  73. * 4. Read by GPU
  74. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  75. * As write_domain is zero, this function adds in the
  76. * current read domains (CPU+COMMAND, 0).
  77. * flush_domains is set to CPU.
  78. * invalidate_domains is set to COMMAND
  79. * clflush is run to get data out of the CPU caches
  80. * then i915_dev_set_domain calls i915_gem_flush to
  81. * emit an MI_FLUSH and drm_agp_chipset_flush
  82. * 5. Unmapped from GTT
  83. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  84. * flush_domains and invalidate_domains end up both zero
  85. * so no flushing/invalidating happens
  86. * 6. Freed
  87. * yay, done
  88. *
  89. * Case 2: The shared render buffer
  90. *
  91. * 1. Allocated
  92. * 2. Mapped to GTT
  93. * 3. Read/written by GPU
  94. * 4. set_domain to (CPU,CPU)
  95. * 5. Read/written by CPU
  96. * 6. Read/written by GPU
  97. *
  98. * 1. Allocated
  99. * Same as last example, (CPU, CPU)
  100. * 2. Mapped to GTT
  101. * Nothing changes (assertions find that it is not in the GPU)
  102. * 3. Read/written by GPU
  103. * execbuffer calls set_domain (RENDER, RENDER)
  104. * flush_domains gets CPU
  105. * invalidate_domains gets GPU
  106. * clflush (obj)
  107. * MI_FLUSH and drm_agp_chipset_flush
  108. * 4. set_domain (CPU, CPU)
  109. * flush_domains gets GPU
  110. * invalidate_domains gets CPU
  111. * wait_rendering (obj) to make sure all drawing is complete.
  112. * This will include an MI_FLUSH to get the data from GPU
  113. * to memory
  114. * clflush (obj) to invalidate the CPU cache
  115. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  116. * 5. Read/written by CPU
  117. * cache lines are loaded and dirtied
  118. * 6. Read written by GPU
  119. * Same as last GPU access
  120. *
  121. * Case 3: The constant buffer
  122. *
  123. * 1. Allocated
  124. * 2. Written by CPU
  125. * 3. Read by GPU
  126. * 4. Updated (written) by CPU again
  127. * 5. Read by GPU
  128. *
  129. * 1. Allocated
  130. * (CPU, CPU)
  131. * 2. Written by CPU
  132. * (CPU, CPU)
  133. * 3. Read by GPU
  134. * (CPU+RENDER, 0)
  135. * flush_domains = CPU
  136. * invalidate_domains = RENDER
  137. * clflush (obj)
  138. * MI_FLUSH
  139. * drm_agp_chipset_flush
  140. * 4. Updated (written) by CPU again
  141. * (CPU, CPU)
  142. * flush_domains = 0 (no previous write domain)
  143. * invalidate_domains = 0 (no new read domains)
  144. * 5. Read by GPU
  145. * (CPU+RENDER, 0)
  146. * flush_domains = CPU
  147. * invalidate_domains = RENDER
  148. * clflush (obj)
  149. * MI_FLUSH
  150. * drm_agp_chipset_flush
  151. */
  152. static void
  153. i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
  154. struct intel_ring_buffer *ring,
  155. struct change_domains *cd)
  156. {
  157. uint32_t invalidate_domains = 0, flush_domains = 0;
  158. /*
  159. * If the object isn't moving to a new write domain,
  160. * let the object stay in multiple read domains
  161. */
  162. if (obj->base.pending_write_domain == 0)
  163. obj->base.pending_read_domains |= obj->base.read_domains;
  164. /*
  165. * Flush the current write domain if
  166. * the new read domains don't match. Invalidate
  167. * any read domains which differ from the old
  168. * write domain
  169. */
  170. if (obj->base.write_domain &&
  171. (((obj->base.write_domain != obj->base.pending_read_domains ||
  172. obj->ring != ring)) ||
  173. (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
  174. flush_domains |= obj->base.write_domain;
  175. invalidate_domains |=
  176. obj->base.pending_read_domains & ~obj->base.write_domain;
  177. }
  178. /*
  179. * Invalidate any read caches which may have
  180. * stale data. That is, any new read domains.
  181. */
  182. invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
  183. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
  184. i915_gem_clflush_object(obj);
  185. if (obj->base.pending_write_domain)
  186. cd->flips |= atomic_read(&obj->pending_flip);
  187. /* The actual obj->write_domain will be updated with
  188. * pending_write_domain after we emit the accumulated flush for all
  189. * of our domain changes in execbuffers (which clears objects'
  190. * write_domains). So if we have a current write domain that we
  191. * aren't changing, set pending_write_domain to that.
  192. */
  193. if (flush_domains == 0 && obj->base.pending_write_domain == 0)
  194. obj->base.pending_write_domain = obj->base.write_domain;
  195. cd->invalidate_domains |= invalidate_domains;
  196. cd->flush_domains |= flush_domains;
  197. if (flush_domains & I915_GEM_GPU_DOMAINS)
  198. cd->flush_rings |= obj->ring->id;
  199. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  200. cd->flush_rings |= ring->id;
  201. }
  202. struct eb_objects {
  203. int and;
  204. struct hlist_head buckets[0];
  205. };
  206. static struct eb_objects *
  207. eb_create(int size)
  208. {
  209. struct eb_objects *eb;
  210. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  211. while (count > size)
  212. count >>= 1;
  213. eb = kzalloc(count*sizeof(struct hlist_head) +
  214. sizeof(struct eb_objects),
  215. GFP_KERNEL);
  216. if (eb == NULL)
  217. return eb;
  218. eb->and = count - 1;
  219. return eb;
  220. }
  221. static void
  222. eb_reset(struct eb_objects *eb)
  223. {
  224. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  225. }
  226. static void
  227. eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
  228. {
  229. hlist_add_head(&obj->exec_node,
  230. &eb->buckets[obj->exec_handle & eb->and]);
  231. }
  232. static struct drm_i915_gem_object *
  233. eb_get_object(struct eb_objects *eb, unsigned long handle)
  234. {
  235. struct hlist_head *head;
  236. struct hlist_node *node;
  237. struct drm_i915_gem_object *obj;
  238. head = &eb->buckets[handle & eb->and];
  239. hlist_for_each(node, head) {
  240. obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
  241. if (obj->exec_handle == handle)
  242. return obj;
  243. }
  244. return NULL;
  245. }
  246. static void
  247. eb_destroy(struct eb_objects *eb)
  248. {
  249. kfree(eb);
  250. }
  251. static int
  252. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  253. struct eb_objects *eb,
  254. struct drm_i915_gem_relocation_entry *reloc)
  255. {
  256. struct drm_device *dev = obj->base.dev;
  257. struct drm_gem_object *target_obj;
  258. uint32_t target_offset;
  259. int ret = -EINVAL;
  260. /* we've already hold a reference to all valid objects */
  261. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  262. if (unlikely(target_obj == NULL))
  263. return -ENOENT;
  264. target_offset = to_intel_bo(target_obj)->gtt_offset;
  265. /* The target buffer should have appeared before us in the
  266. * exec_object list, so it should have a GTT space bound by now.
  267. */
  268. if (unlikely(target_offset == 0)) {
  269. DRM_ERROR("No GTT space found for object %d\n",
  270. reloc->target_handle);
  271. return ret;
  272. }
  273. /* Validate that the target is in a valid r/w GPU domain */
  274. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  275. DRM_ERROR("reloc with multiple write domains: "
  276. "obj %p target %d offset %d "
  277. "read %08x write %08x",
  278. obj, reloc->target_handle,
  279. (int) reloc->offset,
  280. reloc->read_domains,
  281. reloc->write_domain);
  282. return ret;
  283. }
  284. if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
  285. DRM_ERROR("reloc with read/write CPU domains: "
  286. "obj %p target %d offset %d "
  287. "read %08x write %08x",
  288. obj, reloc->target_handle,
  289. (int) reloc->offset,
  290. reloc->read_domains,
  291. reloc->write_domain);
  292. return ret;
  293. }
  294. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  295. reloc->write_domain != target_obj->pending_write_domain)) {
  296. DRM_ERROR("Write domain conflict: "
  297. "obj %p target %d offset %d "
  298. "new %08x old %08x\n",
  299. obj, reloc->target_handle,
  300. (int) reloc->offset,
  301. reloc->write_domain,
  302. target_obj->pending_write_domain);
  303. return ret;
  304. }
  305. target_obj->pending_read_domains |= reloc->read_domains;
  306. target_obj->pending_write_domain |= reloc->write_domain;
  307. /* If the relocation already has the right value in it, no
  308. * more work needs to be done.
  309. */
  310. if (target_offset == reloc->presumed_offset)
  311. return 0;
  312. /* Check that the relocation address is valid... */
  313. if (unlikely(reloc->offset > obj->base.size - 4)) {
  314. DRM_ERROR("Relocation beyond object bounds: "
  315. "obj %p target %d offset %d size %d.\n",
  316. obj, reloc->target_handle,
  317. (int) reloc->offset,
  318. (int) obj->base.size);
  319. return ret;
  320. }
  321. if (unlikely(reloc->offset & 3)) {
  322. DRM_ERROR("Relocation not 4-byte aligned: "
  323. "obj %p target %d offset %d.\n",
  324. obj, reloc->target_handle,
  325. (int) reloc->offset);
  326. return ret;
  327. }
  328. reloc->delta += target_offset;
  329. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  330. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  331. char *vaddr;
  332. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  333. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  334. kunmap_atomic(vaddr);
  335. } else {
  336. struct drm_i915_private *dev_priv = dev->dev_private;
  337. uint32_t __iomem *reloc_entry;
  338. void __iomem *reloc_page;
  339. /* We can't wait for rendering with pagefaults disabled */
  340. if (obj->active && in_atomic())
  341. return -EFAULT;
  342. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  343. if (ret)
  344. return ret;
  345. /* Map the page containing the relocation we're going to perform. */
  346. reloc->offset += obj->gtt_offset;
  347. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  348. reloc->offset & PAGE_MASK);
  349. reloc_entry = (uint32_t __iomem *)
  350. (reloc_page + (reloc->offset & ~PAGE_MASK));
  351. iowrite32(reloc->delta, reloc_entry);
  352. io_mapping_unmap_atomic(reloc_page);
  353. }
  354. /* and update the user's relocation entry */
  355. reloc->presumed_offset = target_offset;
  356. return 0;
  357. }
  358. static int
  359. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  360. struct eb_objects *eb)
  361. {
  362. struct drm_i915_gem_relocation_entry __user *user_relocs;
  363. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  364. int i, ret;
  365. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  366. for (i = 0; i < entry->relocation_count; i++) {
  367. struct drm_i915_gem_relocation_entry reloc;
  368. if (__copy_from_user_inatomic(&reloc,
  369. user_relocs+i,
  370. sizeof(reloc)))
  371. return -EFAULT;
  372. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
  373. if (ret)
  374. return ret;
  375. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  376. &reloc.presumed_offset,
  377. sizeof(reloc.presumed_offset)))
  378. return -EFAULT;
  379. }
  380. return 0;
  381. }
  382. static int
  383. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  384. struct eb_objects *eb,
  385. struct drm_i915_gem_relocation_entry *relocs)
  386. {
  387. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  388. int i, ret;
  389. for (i = 0; i < entry->relocation_count; i++) {
  390. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  391. if (ret)
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static int
  397. i915_gem_execbuffer_relocate(struct drm_device *dev,
  398. struct eb_objects *eb,
  399. struct list_head *objects)
  400. {
  401. struct drm_i915_gem_object *obj;
  402. int ret = 0;
  403. /* This is the fast path and we cannot handle a pagefault whilst
  404. * holding the struct mutex lest the user pass in the relocations
  405. * contained within a mmaped bo. For in such a case we, the page
  406. * fault handler would call i915_gem_fault() and we would try to
  407. * acquire the struct mutex again. Obviously this is bad and so
  408. * lockdep complains vehemently.
  409. */
  410. pagefault_disable();
  411. list_for_each_entry(obj, objects, exec_list) {
  412. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  413. if (ret)
  414. break;
  415. }
  416. pagefault_enable();
  417. return ret;
  418. }
  419. static int
  420. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  421. struct drm_file *file,
  422. struct list_head *objects)
  423. {
  424. struct drm_i915_gem_object *obj;
  425. int ret, retry;
  426. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  427. struct list_head ordered_objects;
  428. INIT_LIST_HEAD(&ordered_objects);
  429. while (!list_empty(objects)) {
  430. struct drm_i915_gem_exec_object2 *entry;
  431. bool need_fence, need_mappable;
  432. obj = list_first_entry(objects,
  433. struct drm_i915_gem_object,
  434. exec_list);
  435. entry = obj->exec_entry;
  436. need_fence =
  437. has_fenced_gpu_access &&
  438. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  439. obj->tiling_mode != I915_TILING_NONE;
  440. need_mappable =
  441. entry->relocation_count ? true : need_fence;
  442. if (need_mappable)
  443. list_move(&obj->exec_list, &ordered_objects);
  444. else
  445. list_move_tail(&obj->exec_list, &ordered_objects);
  446. obj->base.pending_read_domains = 0;
  447. obj->base.pending_write_domain = 0;
  448. }
  449. list_splice(&ordered_objects, objects);
  450. /* Attempt to pin all of the buffers into the GTT.
  451. * This is done in 3 phases:
  452. *
  453. * 1a. Unbind all objects that do not match the GTT constraints for
  454. * the execbuffer (fenceable, mappable, alignment etc).
  455. * 1b. Increment pin count for already bound objects.
  456. * 2. Bind new objects.
  457. * 3. Decrement pin count.
  458. *
  459. * This avoid unnecessary unbinding of later objects in order to makr
  460. * room for the earlier objects *unless* we need to defragment.
  461. */
  462. retry = 0;
  463. do {
  464. ret = 0;
  465. /* Unbind any ill-fitting objects or pin. */
  466. list_for_each_entry(obj, objects, exec_list) {
  467. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  468. bool need_fence, need_mappable;
  469. if (!obj->gtt_space)
  470. continue;
  471. need_fence =
  472. has_fenced_gpu_access &&
  473. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  474. obj->tiling_mode != I915_TILING_NONE;
  475. need_mappable =
  476. entry->relocation_count ? true : need_fence;
  477. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  478. (need_mappable && !obj->map_and_fenceable))
  479. ret = i915_gem_object_unbind(obj);
  480. else
  481. ret = i915_gem_object_pin(obj,
  482. entry->alignment,
  483. need_mappable);
  484. if (ret)
  485. goto err;
  486. entry++;
  487. }
  488. /* Bind fresh objects */
  489. list_for_each_entry(obj, objects, exec_list) {
  490. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  491. bool need_fence;
  492. need_fence =
  493. has_fenced_gpu_access &&
  494. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  495. obj->tiling_mode != I915_TILING_NONE;
  496. if (!obj->gtt_space) {
  497. bool need_mappable =
  498. entry->relocation_count ? true : need_fence;
  499. ret = i915_gem_object_pin(obj,
  500. entry->alignment,
  501. need_mappable);
  502. if (ret)
  503. break;
  504. }
  505. if (has_fenced_gpu_access) {
  506. if (need_fence) {
  507. ret = i915_gem_object_get_fence(obj, ring);
  508. if (ret)
  509. break;
  510. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  511. obj->tiling_mode == I915_TILING_NONE) {
  512. /* XXX pipelined! */
  513. ret = i915_gem_object_put_fence(obj);
  514. if (ret)
  515. break;
  516. }
  517. obj->pending_fenced_gpu_access = need_fence;
  518. }
  519. entry->offset = obj->gtt_offset;
  520. }
  521. /* Decrement pin count for bound objects */
  522. list_for_each_entry(obj, objects, exec_list) {
  523. if (obj->gtt_space)
  524. i915_gem_object_unpin(obj);
  525. }
  526. if (ret != -ENOSPC || retry > 1)
  527. return ret;
  528. /* First attempt, just clear anything that is purgeable.
  529. * Second attempt, clear the entire GTT.
  530. */
  531. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  532. if (ret)
  533. return ret;
  534. retry++;
  535. } while (1);
  536. err:
  537. obj = list_entry(obj->exec_list.prev,
  538. struct drm_i915_gem_object,
  539. exec_list);
  540. while (objects != &obj->exec_list) {
  541. if (obj->gtt_space)
  542. i915_gem_object_unpin(obj);
  543. obj = list_entry(obj->exec_list.prev,
  544. struct drm_i915_gem_object,
  545. exec_list);
  546. }
  547. return ret;
  548. }
  549. static int
  550. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  551. struct drm_file *file,
  552. struct intel_ring_buffer *ring,
  553. struct list_head *objects,
  554. struct eb_objects *eb,
  555. struct drm_i915_gem_exec_object2 *exec,
  556. int count)
  557. {
  558. struct drm_i915_gem_relocation_entry *reloc;
  559. struct drm_i915_gem_object *obj;
  560. int *reloc_offset;
  561. int i, total, ret;
  562. /* We may process another execbuffer during the unlock... */
  563. while (!list_empty(objects)) {
  564. obj = list_first_entry(objects,
  565. struct drm_i915_gem_object,
  566. exec_list);
  567. list_del_init(&obj->exec_list);
  568. drm_gem_object_unreference(&obj->base);
  569. }
  570. mutex_unlock(&dev->struct_mutex);
  571. total = 0;
  572. for (i = 0; i < count; i++)
  573. total += exec[i].relocation_count;
  574. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  575. reloc = drm_malloc_ab(total, sizeof(*reloc));
  576. if (reloc == NULL || reloc_offset == NULL) {
  577. drm_free_large(reloc);
  578. drm_free_large(reloc_offset);
  579. mutex_lock(&dev->struct_mutex);
  580. return -ENOMEM;
  581. }
  582. total = 0;
  583. for (i = 0; i < count; i++) {
  584. struct drm_i915_gem_relocation_entry __user *user_relocs;
  585. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  586. if (copy_from_user(reloc+total, user_relocs,
  587. exec[i].relocation_count * sizeof(*reloc))) {
  588. ret = -EFAULT;
  589. mutex_lock(&dev->struct_mutex);
  590. goto err;
  591. }
  592. reloc_offset[i] = total;
  593. total += exec[i].relocation_count;
  594. }
  595. ret = i915_mutex_lock_interruptible(dev);
  596. if (ret) {
  597. mutex_lock(&dev->struct_mutex);
  598. goto err;
  599. }
  600. /* reacquire the objects */
  601. eb_reset(eb);
  602. for (i = 0; i < count; i++) {
  603. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  604. exec[i].handle));
  605. if (&obj->base == NULL) {
  606. DRM_ERROR("Invalid object handle %d at index %d\n",
  607. exec[i].handle, i);
  608. ret = -ENOENT;
  609. goto err;
  610. }
  611. list_add_tail(&obj->exec_list, objects);
  612. obj->exec_handle = exec[i].handle;
  613. obj->exec_entry = &exec[i];
  614. eb_add_object(eb, obj);
  615. }
  616. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  617. if (ret)
  618. goto err;
  619. list_for_each_entry(obj, objects, exec_list) {
  620. int offset = obj->exec_entry - exec;
  621. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  622. reloc + reloc_offset[offset]);
  623. if (ret)
  624. goto err;
  625. }
  626. /* Leave the user relocations as are, this is the painfully slow path,
  627. * and we want to avoid the complication of dropping the lock whilst
  628. * having buffers reserved in the aperture and so causing spurious
  629. * ENOSPC for random operations.
  630. */
  631. err:
  632. drm_free_large(reloc);
  633. drm_free_large(reloc_offset);
  634. return ret;
  635. }
  636. static int
  637. i915_gem_execbuffer_flush(struct drm_device *dev,
  638. uint32_t invalidate_domains,
  639. uint32_t flush_domains,
  640. uint32_t flush_rings)
  641. {
  642. drm_i915_private_t *dev_priv = dev->dev_private;
  643. int i, ret;
  644. if (flush_domains & I915_GEM_DOMAIN_CPU)
  645. intel_gtt_chipset_flush();
  646. if (flush_domains & I915_GEM_DOMAIN_GTT)
  647. wmb();
  648. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  649. for (i = 0; i < I915_NUM_RINGS; i++)
  650. if (flush_rings & (1 << i)) {
  651. ret = i915_gem_flush_ring(&dev_priv->ring[i],
  652. invalidate_domains,
  653. flush_domains);
  654. if (ret)
  655. return ret;
  656. }
  657. }
  658. return 0;
  659. }
  660. static bool
  661. intel_enable_semaphores(struct drm_device *dev)
  662. {
  663. if (INTEL_INFO(dev)->gen < 6)
  664. return 0;
  665. if (i915_semaphores >= 0)
  666. return i915_semaphores;
  667. /* Enable semaphores on SNB when IO remapping is off */
  668. if (INTEL_INFO(dev)->gen == 6)
  669. return !intel_iommu_enabled;
  670. return 1;
  671. }
  672. static int
  673. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  674. struct intel_ring_buffer *to)
  675. {
  676. struct intel_ring_buffer *from = obj->ring;
  677. u32 seqno;
  678. int ret, idx;
  679. if (from == NULL || to == from)
  680. return 0;
  681. /* XXX gpu semaphores are implicated in various hard hangs on SNB */
  682. if (!intel_enable_semaphores(obj->base.dev))
  683. return i915_gem_object_wait_rendering(obj);
  684. idx = intel_ring_sync_index(from, to);
  685. seqno = obj->last_rendering_seqno;
  686. if (seqno <= from->sync_seqno[idx])
  687. return 0;
  688. if (seqno == from->outstanding_lazy_request) {
  689. struct drm_i915_gem_request *request;
  690. request = kzalloc(sizeof(*request), GFP_KERNEL);
  691. if (request == NULL)
  692. return -ENOMEM;
  693. ret = i915_add_request(from, NULL, request);
  694. if (ret) {
  695. kfree(request);
  696. return ret;
  697. }
  698. seqno = request->seqno;
  699. }
  700. from->sync_seqno[idx] = seqno;
  701. return to->sync_to(to, from, seqno - 1);
  702. }
  703. static int
  704. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  705. {
  706. u32 plane, flip_mask;
  707. int ret;
  708. /* Check for any pending flips. As we only maintain a flip queue depth
  709. * of 1, we can simply insert a WAIT for the next display flip prior
  710. * to executing the batch and avoid stalling the CPU.
  711. */
  712. for (plane = 0; flips >> plane; plane++) {
  713. if (((flips >> plane) & 1) == 0)
  714. continue;
  715. if (plane)
  716. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  717. else
  718. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  719. ret = intel_ring_begin(ring, 2);
  720. if (ret)
  721. return ret;
  722. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  723. intel_ring_emit(ring, MI_NOOP);
  724. intel_ring_advance(ring);
  725. }
  726. return 0;
  727. }
  728. static int
  729. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  730. struct list_head *objects)
  731. {
  732. struct drm_i915_gem_object *obj;
  733. struct change_domains cd;
  734. int ret;
  735. memset(&cd, 0, sizeof(cd));
  736. list_for_each_entry(obj, objects, exec_list)
  737. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  738. if (cd.invalidate_domains | cd.flush_domains) {
  739. ret = i915_gem_execbuffer_flush(ring->dev,
  740. cd.invalidate_domains,
  741. cd.flush_domains,
  742. cd.flush_rings);
  743. if (ret)
  744. return ret;
  745. }
  746. if (cd.flips) {
  747. ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
  748. if (ret)
  749. return ret;
  750. }
  751. list_for_each_entry(obj, objects, exec_list) {
  752. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  753. if (ret)
  754. return ret;
  755. }
  756. return 0;
  757. }
  758. static bool
  759. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  760. {
  761. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  762. }
  763. static int
  764. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  765. int count)
  766. {
  767. int i;
  768. for (i = 0; i < count; i++) {
  769. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  770. int length; /* limited by fault_in_pages_readable() */
  771. /* First check for malicious input causing overflow */
  772. if (exec[i].relocation_count >
  773. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  774. return -EINVAL;
  775. length = exec[i].relocation_count *
  776. sizeof(struct drm_i915_gem_relocation_entry);
  777. if (!access_ok(VERIFY_READ, ptr, length))
  778. return -EFAULT;
  779. /* we may also need to update the presumed offsets */
  780. if (!access_ok(VERIFY_WRITE, ptr, length))
  781. return -EFAULT;
  782. if (fault_in_pages_readable(ptr, length))
  783. return -EFAULT;
  784. }
  785. return 0;
  786. }
  787. static void
  788. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  789. struct intel_ring_buffer *ring,
  790. u32 seqno)
  791. {
  792. struct drm_i915_gem_object *obj;
  793. list_for_each_entry(obj, objects, exec_list) {
  794. u32 old_read = obj->base.read_domains;
  795. u32 old_write = obj->base.write_domain;
  796. obj->base.read_domains = obj->base.pending_read_domains;
  797. obj->base.write_domain = obj->base.pending_write_domain;
  798. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  799. i915_gem_object_move_to_active(obj, ring, seqno);
  800. if (obj->base.write_domain) {
  801. obj->dirty = 1;
  802. obj->pending_gpu_write = true;
  803. list_move_tail(&obj->gpu_write_list,
  804. &ring->gpu_write_list);
  805. intel_mark_busy(ring->dev, obj);
  806. }
  807. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  808. }
  809. }
  810. static void
  811. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  812. struct drm_file *file,
  813. struct intel_ring_buffer *ring)
  814. {
  815. struct drm_i915_gem_request *request;
  816. u32 invalidate;
  817. /*
  818. * Ensure that the commands in the batch buffer are
  819. * finished before the interrupt fires.
  820. *
  821. * The sampler always gets flushed on i965 (sigh).
  822. */
  823. invalidate = I915_GEM_DOMAIN_COMMAND;
  824. if (INTEL_INFO(dev)->gen >= 4)
  825. invalidate |= I915_GEM_DOMAIN_SAMPLER;
  826. if (ring->flush(ring, invalidate, 0)) {
  827. i915_gem_next_request_seqno(ring);
  828. return;
  829. }
  830. /* Add a breadcrumb for the completion of the batch buffer */
  831. request = kzalloc(sizeof(*request), GFP_KERNEL);
  832. if (request == NULL || i915_add_request(ring, file, request)) {
  833. i915_gem_next_request_seqno(ring);
  834. kfree(request);
  835. }
  836. }
  837. static int
  838. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  839. struct drm_file *file,
  840. struct drm_i915_gem_execbuffer2 *args,
  841. struct drm_i915_gem_exec_object2 *exec)
  842. {
  843. drm_i915_private_t *dev_priv = dev->dev_private;
  844. struct list_head objects;
  845. struct eb_objects *eb;
  846. struct drm_i915_gem_object *batch_obj;
  847. struct drm_clip_rect *cliprects = NULL;
  848. struct intel_ring_buffer *ring;
  849. u32 exec_start, exec_len;
  850. u32 seqno;
  851. int ret, mode, i;
  852. if (!i915_gem_check_execbuffer(args)) {
  853. DRM_ERROR("execbuf with invalid offset/length\n");
  854. return -EINVAL;
  855. }
  856. ret = validate_exec_list(exec, args->buffer_count);
  857. if (ret)
  858. return ret;
  859. switch (args->flags & I915_EXEC_RING_MASK) {
  860. case I915_EXEC_DEFAULT:
  861. case I915_EXEC_RENDER:
  862. ring = &dev_priv->ring[RCS];
  863. break;
  864. case I915_EXEC_BSD:
  865. if (!HAS_BSD(dev)) {
  866. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  867. return -EINVAL;
  868. }
  869. ring = &dev_priv->ring[VCS];
  870. break;
  871. case I915_EXEC_BLT:
  872. if (!HAS_BLT(dev)) {
  873. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  874. return -EINVAL;
  875. }
  876. ring = &dev_priv->ring[BCS];
  877. break;
  878. default:
  879. DRM_ERROR("execbuf with unknown ring: %d\n",
  880. (int)(args->flags & I915_EXEC_RING_MASK));
  881. return -EINVAL;
  882. }
  883. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  884. switch (mode) {
  885. case I915_EXEC_CONSTANTS_REL_GENERAL:
  886. case I915_EXEC_CONSTANTS_ABSOLUTE:
  887. case I915_EXEC_CONSTANTS_REL_SURFACE:
  888. if (ring == &dev_priv->ring[RCS] &&
  889. mode != dev_priv->relative_constants_mode) {
  890. if (INTEL_INFO(dev)->gen < 4)
  891. return -EINVAL;
  892. if (INTEL_INFO(dev)->gen > 5 &&
  893. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  894. return -EINVAL;
  895. ret = intel_ring_begin(ring, 4);
  896. if (ret)
  897. return ret;
  898. intel_ring_emit(ring, MI_NOOP);
  899. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  900. intel_ring_emit(ring, INSTPM);
  901. intel_ring_emit(ring,
  902. I915_EXEC_CONSTANTS_MASK << 16 | mode);
  903. intel_ring_advance(ring);
  904. dev_priv->relative_constants_mode = mode;
  905. }
  906. break;
  907. default:
  908. DRM_ERROR("execbuf with unknown constants: %d\n", mode);
  909. return -EINVAL;
  910. }
  911. if (args->buffer_count < 1) {
  912. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  913. return -EINVAL;
  914. }
  915. if (args->num_cliprects != 0) {
  916. if (ring != &dev_priv->ring[RCS]) {
  917. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  918. return -EINVAL;
  919. }
  920. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  921. GFP_KERNEL);
  922. if (cliprects == NULL) {
  923. ret = -ENOMEM;
  924. goto pre_mutex_err;
  925. }
  926. if (copy_from_user(cliprects,
  927. (struct drm_clip_rect __user *)(uintptr_t)
  928. args->cliprects_ptr,
  929. sizeof(*cliprects)*args->num_cliprects)) {
  930. ret = -EFAULT;
  931. goto pre_mutex_err;
  932. }
  933. }
  934. ret = i915_mutex_lock_interruptible(dev);
  935. if (ret)
  936. goto pre_mutex_err;
  937. if (dev_priv->mm.suspended) {
  938. mutex_unlock(&dev->struct_mutex);
  939. ret = -EBUSY;
  940. goto pre_mutex_err;
  941. }
  942. eb = eb_create(args->buffer_count);
  943. if (eb == NULL) {
  944. mutex_unlock(&dev->struct_mutex);
  945. ret = -ENOMEM;
  946. goto pre_mutex_err;
  947. }
  948. /* Look up object handles */
  949. INIT_LIST_HEAD(&objects);
  950. for (i = 0; i < args->buffer_count; i++) {
  951. struct drm_i915_gem_object *obj;
  952. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  953. exec[i].handle));
  954. if (&obj->base == NULL) {
  955. DRM_ERROR("Invalid object handle %d at index %d\n",
  956. exec[i].handle, i);
  957. /* prevent error path from reading uninitialized data */
  958. ret = -ENOENT;
  959. goto err;
  960. }
  961. if (!list_empty(&obj->exec_list)) {
  962. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  963. obj, exec[i].handle, i);
  964. ret = -EINVAL;
  965. goto err;
  966. }
  967. list_add_tail(&obj->exec_list, &objects);
  968. obj->exec_handle = exec[i].handle;
  969. obj->exec_entry = &exec[i];
  970. eb_add_object(eb, obj);
  971. }
  972. /* take note of the batch buffer before we might reorder the lists */
  973. batch_obj = list_entry(objects.prev,
  974. struct drm_i915_gem_object,
  975. exec_list);
  976. /* Move the objects en-masse into the GTT, evicting if necessary. */
  977. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  978. if (ret)
  979. goto err;
  980. /* The objects are in their final locations, apply the relocations. */
  981. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  982. if (ret) {
  983. if (ret == -EFAULT) {
  984. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  985. &objects, eb,
  986. exec,
  987. args->buffer_count);
  988. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  989. }
  990. if (ret)
  991. goto err;
  992. }
  993. /* Set the pending read domains for the batch buffer to COMMAND */
  994. if (batch_obj->base.pending_write_domain) {
  995. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  996. ret = -EINVAL;
  997. goto err;
  998. }
  999. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1000. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  1001. if (ret)
  1002. goto err;
  1003. seqno = i915_gem_next_request_seqno(ring);
  1004. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  1005. if (seqno < ring->sync_seqno[i]) {
  1006. /* The GPU can not handle its semaphore value wrapping,
  1007. * so every billion or so execbuffers, we need to stall
  1008. * the GPU in order to reset the counters.
  1009. */
  1010. ret = i915_gpu_idle(dev);
  1011. if (ret)
  1012. goto err;
  1013. BUG_ON(ring->sync_seqno[i]);
  1014. }
  1015. }
  1016. trace_i915_gem_ring_dispatch(ring, seqno);
  1017. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1018. exec_len = args->batch_len;
  1019. if (cliprects) {
  1020. for (i = 0; i < args->num_cliprects; i++) {
  1021. ret = i915_emit_box(dev, &cliprects[i],
  1022. args->DR1, args->DR4);
  1023. if (ret)
  1024. goto err;
  1025. ret = ring->dispatch_execbuffer(ring,
  1026. exec_start, exec_len);
  1027. if (ret)
  1028. goto err;
  1029. }
  1030. } else {
  1031. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1032. if (ret)
  1033. goto err;
  1034. }
  1035. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1036. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1037. err:
  1038. eb_destroy(eb);
  1039. while (!list_empty(&objects)) {
  1040. struct drm_i915_gem_object *obj;
  1041. obj = list_first_entry(&objects,
  1042. struct drm_i915_gem_object,
  1043. exec_list);
  1044. list_del_init(&obj->exec_list);
  1045. drm_gem_object_unreference(&obj->base);
  1046. }
  1047. mutex_unlock(&dev->struct_mutex);
  1048. pre_mutex_err:
  1049. kfree(cliprects);
  1050. return ret;
  1051. }
  1052. /*
  1053. * Legacy execbuffer just creates an exec2 list from the original exec object
  1054. * list array and passes it to the real function.
  1055. */
  1056. int
  1057. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1058. struct drm_file *file)
  1059. {
  1060. struct drm_i915_gem_execbuffer *args = data;
  1061. struct drm_i915_gem_execbuffer2 exec2;
  1062. struct drm_i915_gem_exec_object *exec_list = NULL;
  1063. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1064. int ret, i;
  1065. if (args->buffer_count < 1) {
  1066. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1067. return -EINVAL;
  1068. }
  1069. /* Copy in the exec list from userland */
  1070. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1071. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1072. if (exec_list == NULL || exec2_list == NULL) {
  1073. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1074. args->buffer_count);
  1075. drm_free_large(exec_list);
  1076. drm_free_large(exec2_list);
  1077. return -ENOMEM;
  1078. }
  1079. ret = copy_from_user(exec_list,
  1080. (struct drm_i915_relocation_entry __user *)
  1081. (uintptr_t) args->buffers_ptr,
  1082. sizeof(*exec_list) * args->buffer_count);
  1083. if (ret != 0) {
  1084. DRM_ERROR("copy %d exec entries failed %d\n",
  1085. args->buffer_count, ret);
  1086. drm_free_large(exec_list);
  1087. drm_free_large(exec2_list);
  1088. return -EFAULT;
  1089. }
  1090. for (i = 0; i < args->buffer_count; i++) {
  1091. exec2_list[i].handle = exec_list[i].handle;
  1092. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1093. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1094. exec2_list[i].alignment = exec_list[i].alignment;
  1095. exec2_list[i].offset = exec_list[i].offset;
  1096. if (INTEL_INFO(dev)->gen < 4)
  1097. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1098. else
  1099. exec2_list[i].flags = 0;
  1100. }
  1101. exec2.buffers_ptr = args->buffers_ptr;
  1102. exec2.buffer_count = args->buffer_count;
  1103. exec2.batch_start_offset = args->batch_start_offset;
  1104. exec2.batch_len = args->batch_len;
  1105. exec2.DR1 = args->DR1;
  1106. exec2.DR4 = args->DR4;
  1107. exec2.num_cliprects = args->num_cliprects;
  1108. exec2.cliprects_ptr = args->cliprects_ptr;
  1109. exec2.flags = I915_EXEC_RENDER;
  1110. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1111. if (!ret) {
  1112. /* Copy the new buffer offsets back to the user's exec list. */
  1113. for (i = 0; i < args->buffer_count; i++)
  1114. exec_list[i].offset = exec2_list[i].offset;
  1115. /* ... and back out to userspace */
  1116. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1117. (uintptr_t) args->buffers_ptr,
  1118. exec_list,
  1119. sizeof(*exec_list) * args->buffer_count);
  1120. if (ret) {
  1121. ret = -EFAULT;
  1122. DRM_ERROR("failed to copy %d exec entries "
  1123. "back to user (%d)\n",
  1124. args->buffer_count, ret);
  1125. }
  1126. }
  1127. drm_free_large(exec_list);
  1128. drm_free_large(exec2_list);
  1129. return ret;
  1130. }
  1131. int
  1132. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1133. struct drm_file *file)
  1134. {
  1135. struct drm_i915_gem_execbuffer2 *args = data;
  1136. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1137. int ret;
  1138. if (args->buffer_count < 1) {
  1139. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  1140. return -EINVAL;
  1141. }
  1142. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1143. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1144. if (exec2_list == NULL)
  1145. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1146. args->buffer_count);
  1147. if (exec2_list == NULL) {
  1148. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1149. args->buffer_count);
  1150. return -ENOMEM;
  1151. }
  1152. ret = copy_from_user(exec2_list,
  1153. (struct drm_i915_relocation_entry __user *)
  1154. (uintptr_t) args->buffers_ptr,
  1155. sizeof(*exec2_list) * args->buffer_count);
  1156. if (ret != 0) {
  1157. DRM_ERROR("copy %d exec entries failed %d\n",
  1158. args->buffer_count, ret);
  1159. drm_free_large(exec2_list);
  1160. return -EFAULT;
  1161. }
  1162. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1163. if (!ret) {
  1164. /* Copy the new buffer offsets back to the user's exec list. */
  1165. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1166. (uintptr_t) args->buffers_ptr,
  1167. exec2_list,
  1168. sizeof(*exec2_list) * args->buffer_count);
  1169. if (ret) {
  1170. ret = -EFAULT;
  1171. DRM_ERROR("failed to copy %d exec entries "
  1172. "back to user (%d)\n",
  1173. args->buffer_count, ret);
  1174. }
  1175. }
  1176. drm_free_large(exec2_list);
  1177. return ret;
  1178. }