i915_gem_execbuffer.c 35 KB

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