i915_gem_execbuffer.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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_relocation_entry *reloc)
  254. {
  255. struct drm_device *dev = obj->base.dev;
  256. struct drm_gem_object *target_obj;
  257. uint32_t target_offset;
  258. int ret = -EINVAL;
  259. /* we've already hold a reference to all valid objects */
  260. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  261. if (unlikely(target_obj == NULL))
  262. return -ENOENT;
  263. target_offset = to_intel_bo(target_obj)->gtt_offset;
  264. #if WATCH_RELOC
  265. DRM_INFO("%s: obj %p offset %08x target %d "
  266. "read %08x write %08x gtt %08x "
  267. "presumed %08x delta %08x\n",
  268. __func__,
  269. obj,
  270. (int) reloc->offset,
  271. (int) reloc->target_handle,
  272. (int) reloc->read_domains,
  273. (int) reloc->write_domain,
  274. (int) target_offset,
  275. (int) reloc->presumed_offset,
  276. reloc->delta);
  277. #endif
  278. /* The target buffer should have appeared before us in the
  279. * exec_object list, so it should have a GTT space bound by now.
  280. */
  281. if (unlikely(target_offset == 0)) {
  282. DRM_ERROR("No GTT space found for object %d\n",
  283. reloc->target_handle);
  284. return ret;
  285. }
  286. /* Validate that the target is in a valid r/w GPU domain */
  287. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  288. DRM_ERROR("reloc with multiple write domains: "
  289. "obj %p target %d offset %d "
  290. "read %08x write %08x",
  291. obj, reloc->target_handle,
  292. (int) reloc->offset,
  293. reloc->read_domains,
  294. reloc->write_domain);
  295. return ret;
  296. }
  297. if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
  298. DRM_ERROR("reloc with read/write CPU domains: "
  299. "obj %p target %d offset %d "
  300. "read %08x write %08x",
  301. obj, reloc->target_handle,
  302. (int) reloc->offset,
  303. reloc->read_domains,
  304. reloc->write_domain);
  305. return ret;
  306. }
  307. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  308. reloc->write_domain != target_obj->pending_write_domain)) {
  309. DRM_ERROR("Write domain conflict: "
  310. "obj %p target %d offset %d "
  311. "new %08x old %08x\n",
  312. obj, reloc->target_handle,
  313. (int) reloc->offset,
  314. reloc->write_domain,
  315. target_obj->pending_write_domain);
  316. return ret;
  317. }
  318. target_obj->pending_read_domains |= reloc->read_domains;
  319. target_obj->pending_write_domain |= reloc->write_domain;
  320. /* If the relocation already has the right value in it, no
  321. * more work needs to be done.
  322. */
  323. if (target_offset == reloc->presumed_offset)
  324. return 0;
  325. /* Check that the relocation address is valid... */
  326. if (unlikely(reloc->offset > obj->base.size - 4)) {
  327. DRM_ERROR("Relocation beyond object bounds: "
  328. "obj %p target %d offset %d size %d.\n",
  329. obj, reloc->target_handle,
  330. (int) reloc->offset,
  331. (int) obj->base.size);
  332. return ret;
  333. }
  334. if (unlikely(reloc->offset & 3)) {
  335. DRM_ERROR("Relocation not 4-byte aligned: "
  336. "obj %p target %d offset %d.\n",
  337. obj, reloc->target_handle,
  338. (int) reloc->offset);
  339. return ret;
  340. }
  341. /* and points to somewhere within the target object. */
  342. if (unlikely(reloc->delta >= target_obj->size)) {
  343. DRM_ERROR("Relocation beyond target object bounds: "
  344. "obj %p target %d delta %d size %d.\n",
  345. obj, reloc->target_handle,
  346. (int) reloc->delta,
  347. (int) target_obj->size);
  348. return ret;
  349. }
  350. reloc->delta += target_offset;
  351. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  352. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  353. char *vaddr;
  354. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  355. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  356. kunmap_atomic(vaddr);
  357. } else {
  358. struct drm_i915_private *dev_priv = dev->dev_private;
  359. uint32_t __iomem *reloc_entry;
  360. void __iomem *reloc_page;
  361. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  362. if (ret)
  363. return ret;
  364. /* Map the page containing the relocation we're going to perform. */
  365. reloc->offset += obj->gtt_offset;
  366. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  367. reloc->offset & PAGE_MASK);
  368. reloc_entry = (uint32_t __iomem *)
  369. (reloc_page + (reloc->offset & ~PAGE_MASK));
  370. iowrite32(reloc->delta, reloc_entry);
  371. io_mapping_unmap_atomic(reloc_page);
  372. }
  373. /* and update the user's relocation entry */
  374. reloc->presumed_offset = target_offset;
  375. return 0;
  376. }
  377. static int
  378. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  379. struct eb_objects *eb)
  380. {
  381. struct drm_i915_gem_relocation_entry __user *user_relocs;
  382. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  383. int i, ret;
  384. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  385. for (i = 0; i < entry->relocation_count; i++) {
  386. struct drm_i915_gem_relocation_entry reloc;
  387. if (__copy_from_user_inatomic(&reloc,
  388. user_relocs+i,
  389. sizeof(reloc)))
  390. return -EFAULT;
  391. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
  392. if (ret)
  393. return ret;
  394. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  395. &reloc.presumed_offset,
  396. sizeof(reloc.presumed_offset)))
  397. return -EFAULT;
  398. }
  399. return 0;
  400. }
  401. static int
  402. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  403. struct eb_objects *eb,
  404. struct drm_i915_gem_relocation_entry *relocs)
  405. {
  406. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  407. int i, ret;
  408. for (i = 0; i < entry->relocation_count; i++) {
  409. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  410. if (ret)
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static int
  416. i915_gem_execbuffer_relocate(struct drm_device *dev,
  417. struct eb_objects *eb,
  418. struct list_head *objects)
  419. {
  420. struct drm_i915_gem_object *obj;
  421. int ret;
  422. list_for_each_entry(obj, objects, exec_list) {
  423. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  424. if (ret)
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. static int
  430. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  431. struct drm_file *file,
  432. struct list_head *objects)
  433. {
  434. struct drm_i915_gem_object *obj;
  435. int ret, retry;
  436. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  437. struct list_head ordered_objects;
  438. INIT_LIST_HEAD(&ordered_objects);
  439. while (!list_empty(objects)) {
  440. struct drm_i915_gem_exec_object2 *entry;
  441. bool need_fence, need_mappable;
  442. obj = list_first_entry(objects,
  443. struct drm_i915_gem_object,
  444. exec_list);
  445. entry = obj->exec_entry;
  446. need_fence =
  447. has_fenced_gpu_access &&
  448. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  449. obj->tiling_mode != I915_TILING_NONE;
  450. need_mappable =
  451. entry->relocation_count ? true : need_fence;
  452. if (need_mappable)
  453. list_move(&obj->exec_list, &ordered_objects);
  454. else
  455. list_move_tail(&obj->exec_list, &ordered_objects);
  456. obj->base.pending_read_domains = 0;
  457. obj->base.pending_write_domain = 0;
  458. }
  459. list_splice(&ordered_objects, objects);
  460. /* Attempt to pin all of the buffers into the GTT.
  461. * This is done in 3 phases:
  462. *
  463. * 1a. Unbind all objects that do not match the GTT constraints for
  464. * the execbuffer (fenceable, mappable, alignment etc).
  465. * 1b. Increment pin count for already bound objects.
  466. * 2. Bind new objects.
  467. * 3. Decrement pin count.
  468. *
  469. * This avoid unnecessary unbinding of later objects in order to makr
  470. * room for the earlier objects *unless* we need to defragment.
  471. */
  472. retry = 0;
  473. do {
  474. ret = 0;
  475. /* Unbind any ill-fitting objects or pin. */
  476. list_for_each_entry(obj, objects, exec_list) {
  477. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  478. bool need_fence, need_mappable;
  479. if (!obj->gtt_space)
  480. continue;
  481. need_fence =
  482. has_fenced_gpu_access &&
  483. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  484. obj->tiling_mode != I915_TILING_NONE;
  485. need_mappable =
  486. entry->relocation_count ? true : need_fence;
  487. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  488. (need_mappable && !obj->map_and_fenceable))
  489. ret = i915_gem_object_unbind(obj);
  490. else
  491. ret = i915_gem_object_pin(obj,
  492. entry->alignment,
  493. need_mappable);
  494. if (ret)
  495. goto err;
  496. entry++;
  497. }
  498. /* Bind fresh objects */
  499. list_for_each_entry(obj, objects, exec_list) {
  500. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  501. bool need_fence;
  502. need_fence =
  503. has_fenced_gpu_access &&
  504. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  505. obj->tiling_mode != I915_TILING_NONE;
  506. if (!obj->gtt_space) {
  507. bool need_mappable =
  508. entry->relocation_count ? true : need_fence;
  509. ret = i915_gem_object_pin(obj,
  510. entry->alignment,
  511. need_mappable);
  512. if (ret)
  513. break;
  514. }
  515. if (has_fenced_gpu_access) {
  516. if (need_fence) {
  517. ret = i915_gem_object_get_fence(obj, ring, 1);
  518. if (ret)
  519. break;
  520. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  521. obj->tiling_mode == I915_TILING_NONE) {
  522. /* XXX pipelined! */
  523. ret = i915_gem_object_put_fence(obj);
  524. if (ret)
  525. break;
  526. }
  527. obj->pending_fenced_gpu_access = need_fence;
  528. }
  529. entry->offset = obj->gtt_offset;
  530. }
  531. /* Decrement pin count for bound objects */
  532. list_for_each_entry(obj, objects, exec_list) {
  533. if (obj->gtt_space)
  534. i915_gem_object_unpin(obj);
  535. }
  536. if (ret != -ENOSPC || retry > 1)
  537. return ret;
  538. /* First attempt, just clear anything that is purgeable.
  539. * Second attempt, clear the entire GTT.
  540. */
  541. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  542. if (ret)
  543. return ret;
  544. retry++;
  545. } while (1);
  546. err:
  547. obj = list_entry(obj->exec_list.prev,
  548. struct drm_i915_gem_object,
  549. exec_list);
  550. while (objects != &obj->exec_list) {
  551. if (obj->gtt_space)
  552. i915_gem_object_unpin(obj);
  553. obj = list_entry(obj->exec_list.prev,
  554. struct drm_i915_gem_object,
  555. exec_list);
  556. }
  557. return ret;
  558. }
  559. static int
  560. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  561. struct drm_file *file,
  562. struct intel_ring_buffer *ring,
  563. struct list_head *objects,
  564. struct eb_objects *eb,
  565. struct drm_i915_gem_exec_object2 *exec,
  566. int count)
  567. {
  568. struct drm_i915_gem_relocation_entry *reloc;
  569. struct drm_i915_gem_object *obj;
  570. int *reloc_offset;
  571. int i, total, ret;
  572. /* We may process another execbuffer during the unlock... */
  573. while (!list_empty(objects)) {
  574. obj = list_first_entry(objects,
  575. struct drm_i915_gem_object,
  576. exec_list);
  577. list_del_init(&obj->exec_list);
  578. drm_gem_object_unreference(&obj->base);
  579. }
  580. mutex_unlock(&dev->struct_mutex);
  581. total = 0;
  582. for (i = 0; i < count; i++)
  583. total += exec[i].relocation_count;
  584. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  585. reloc = drm_malloc_ab(total, sizeof(*reloc));
  586. if (reloc == NULL || reloc_offset == NULL) {
  587. drm_free_large(reloc);
  588. drm_free_large(reloc_offset);
  589. mutex_lock(&dev->struct_mutex);
  590. return -ENOMEM;
  591. }
  592. total = 0;
  593. for (i = 0; i < count; i++) {
  594. struct drm_i915_gem_relocation_entry __user *user_relocs;
  595. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  596. if (copy_from_user(reloc+total, user_relocs,
  597. exec[i].relocation_count * sizeof(*reloc))) {
  598. ret = -EFAULT;
  599. mutex_lock(&dev->struct_mutex);
  600. goto err;
  601. }
  602. reloc_offset[i] = total;
  603. total += exec[i].relocation_count;
  604. }
  605. ret = i915_mutex_lock_interruptible(dev);
  606. if (ret) {
  607. mutex_lock(&dev->struct_mutex);
  608. goto err;
  609. }
  610. /* reacquire the objects */
  611. eb_reset(eb);
  612. for (i = 0; i < count; i++) {
  613. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  614. exec[i].handle));
  615. if (obj == NULL) {
  616. DRM_ERROR("Invalid object handle %d at index %d\n",
  617. exec[i].handle, i);
  618. ret = -ENOENT;
  619. goto err;
  620. }
  621. list_add_tail(&obj->exec_list, objects);
  622. obj->exec_handle = exec[i].handle;
  623. obj->exec_entry = &exec[i];
  624. eb_add_object(eb, obj);
  625. }
  626. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  627. if (ret)
  628. goto err;
  629. list_for_each_entry(obj, objects, exec_list) {
  630. int offset = obj->exec_entry - exec;
  631. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  632. reloc + reloc_offset[offset]);
  633. if (ret)
  634. goto err;
  635. }
  636. /* Leave the user relocations as are, this is the painfully slow path,
  637. * and we want to avoid the complication of dropping the lock whilst
  638. * having buffers reserved in the aperture and so causing spurious
  639. * ENOSPC for random operations.
  640. */
  641. err:
  642. drm_free_large(reloc);
  643. drm_free_large(reloc_offset);
  644. return ret;
  645. }
  646. static int
  647. i915_gem_execbuffer_flush(struct drm_device *dev,
  648. uint32_t invalidate_domains,
  649. uint32_t flush_domains,
  650. uint32_t flush_rings)
  651. {
  652. drm_i915_private_t *dev_priv = dev->dev_private;
  653. int i, ret;
  654. if (flush_domains & I915_GEM_DOMAIN_CPU)
  655. intel_gtt_chipset_flush();
  656. if (flush_domains & I915_GEM_DOMAIN_GTT)
  657. wmb();
  658. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  659. for (i = 0; i < I915_NUM_RINGS; i++)
  660. if (flush_rings & (1 << i)) {
  661. ret = i915_gem_flush_ring(dev,
  662. &dev_priv->ring[i],
  663. invalidate_domains,
  664. flush_domains);
  665. if (ret)
  666. return ret;
  667. }
  668. }
  669. return 0;
  670. }
  671. static int
  672. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  673. struct intel_ring_buffer *to)
  674. {
  675. struct intel_ring_buffer *from = obj->ring;
  676. u32 seqno;
  677. int ret, idx;
  678. if (from == NULL || to == from)
  679. return 0;
  680. /* XXX gpu semaphores are currently causing hard hangs on SNB mobile */
  681. if (INTEL_INFO(obj->base.dev)->gen < 6 || IS_MOBILE(obj->base.dev))
  682. return i915_gem_object_wait_rendering(obj, true);
  683. idx = intel_ring_sync_index(from, to);
  684. seqno = obj->last_rendering_seqno;
  685. if (seqno <= from->sync_seqno[idx])
  686. return 0;
  687. if (seqno == from->outstanding_lazy_request) {
  688. struct drm_i915_gem_request *request;
  689. request = kzalloc(sizeof(*request), GFP_KERNEL);
  690. if (request == NULL)
  691. return -ENOMEM;
  692. ret = i915_add_request(obj->base.dev, NULL, request, from);
  693. if (ret) {
  694. kfree(request);
  695. return ret;
  696. }
  697. seqno = request->seqno;
  698. }
  699. from->sync_seqno[idx] = seqno;
  700. return intel_ring_sync(to, from, seqno - 1);
  701. }
  702. static int
  703. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  704. struct list_head *objects)
  705. {
  706. struct drm_i915_gem_object *obj;
  707. struct change_domains cd;
  708. int ret;
  709. cd.invalidate_domains = 0;
  710. cd.flush_domains = 0;
  711. cd.flush_rings = 0;
  712. list_for_each_entry(obj, objects, exec_list)
  713. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  714. if (cd.invalidate_domains | cd.flush_domains) {
  715. #if WATCH_EXEC
  716. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  717. __func__,
  718. cd.invalidate_domains,
  719. cd.flush_domains);
  720. #endif
  721. ret = i915_gem_execbuffer_flush(ring->dev,
  722. cd.invalidate_domains,
  723. cd.flush_domains,
  724. cd.flush_rings);
  725. if (ret)
  726. return ret;
  727. }
  728. list_for_each_entry(obj, objects, exec_list) {
  729. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  730. if (ret)
  731. return ret;
  732. }
  733. return 0;
  734. }
  735. static bool
  736. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  737. {
  738. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  739. }
  740. static int
  741. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  742. int count)
  743. {
  744. int i;
  745. for (i = 0; i < count; i++) {
  746. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  747. int length; /* limited by fault_in_pages_readable() */
  748. /* First check for malicious input causing overflow */
  749. if (exec[i].relocation_count >
  750. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  751. return -EINVAL;
  752. length = exec[i].relocation_count *
  753. sizeof(struct drm_i915_gem_relocation_entry);
  754. if (!access_ok(VERIFY_READ, ptr, length))
  755. return -EFAULT;
  756. /* we may also need to update the presumed offsets */
  757. if (!access_ok(VERIFY_WRITE, ptr, length))
  758. return -EFAULT;
  759. if (fault_in_pages_readable(ptr, length))
  760. return -EFAULT;
  761. }
  762. return 0;
  763. }
  764. static int
  765. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
  766. struct list_head *objects)
  767. {
  768. struct drm_i915_gem_object *obj;
  769. int flips;
  770. /* Check for any pending flips. As we only maintain a flip queue depth
  771. * of 1, we can simply insert a WAIT for the next display flip prior
  772. * to executing the batch and avoid stalling the CPU.
  773. */
  774. flips = 0;
  775. list_for_each_entry(obj, objects, exec_list) {
  776. if (obj->base.write_domain)
  777. flips |= atomic_read(&obj->pending_flip);
  778. }
  779. if (flips) {
  780. int plane, flip_mask, ret;
  781. for (plane = 0; flips >> plane; plane++) {
  782. if (((flips >> plane) & 1) == 0)
  783. continue;
  784. if (plane)
  785. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  786. else
  787. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  788. ret = intel_ring_begin(ring, 2);
  789. if (ret)
  790. return ret;
  791. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  792. intel_ring_emit(ring, MI_NOOP);
  793. intel_ring_advance(ring);
  794. }
  795. }
  796. return 0;
  797. }
  798. static void
  799. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  800. struct intel_ring_buffer *ring,
  801. u32 seqno)
  802. {
  803. struct drm_i915_gem_object *obj;
  804. list_for_each_entry(obj, objects, exec_list) {
  805. obj->base.read_domains = obj->base.pending_read_domains;
  806. obj->base.write_domain = obj->base.pending_write_domain;
  807. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  808. i915_gem_object_move_to_active(obj, ring, seqno);
  809. if (obj->base.write_domain) {
  810. obj->dirty = 1;
  811. obj->pending_gpu_write = true;
  812. list_move_tail(&obj->gpu_write_list,
  813. &ring->gpu_write_list);
  814. intel_mark_busy(ring->dev, obj);
  815. }
  816. trace_i915_gem_object_change_domain(obj,
  817. obj->base.read_domains,
  818. obj->base.write_domain);
  819. }
  820. }
  821. static void
  822. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  823. struct drm_file *file,
  824. struct intel_ring_buffer *ring)
  825. {
  826. struct drm_i915_gem_request *request;
  827. u32 invalidate;
  828. /*
  829. * Ensure that the commands in the batch buffer are
  830. * finished before the interrupt fires.
  831. *
  832. * The sampler always gets flushed on i965 (sigh).
  833. */
  834. invalidate = I915_GEM_DOMAIN_COMMAND;
  835. if (INTEL_INFO(dev)->gen >= 4)
  836. invalidate |= I915_GEM_DOMAIN_SAMPLER;
  837. if (ring->flush(ring, invalidate, 0)) {
  838. i915_gem_next_request_seqno(dev, ring);
  839. return;
  840. }
  841. /* Add a breadcrumb for the completion of the batch buffer */
  842. request = kzalloc(sizeof(*request), GFP_KERNEL);
  843. if (request == NULL || i915_add_request(dev, file, request, ring)) {
  844. i915_gem_next_request_seqno(dev, ring);
  845. kfree(request);
  846. }
  847. }
  848. static int
  849. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  850. struct drm_file *file,
  851. struct drm_i915_gem_execbuffer2 *args,
  852. struct drm_i915_gem_exec_object2 *exec)
  853. {
  854. drm_i915_private_t *dev_priv = dev->dev_private;
  855. struct list_head objects;
  856. struct eb_objects *eb;
  857. struct drm_i915_gem_object *batch_obj;
  858. struct drm_clip_rect *cliprects = NULL;
  859. struct intel_ring_buffer *ring;
  860. u32 exec_start, exec_len;
  861. u32 seqno;
  862. int ret, mode, i;
  863. if (!i915_gem_check_execbuffer(args)) {
  864. DRM_ERROR("execbuf with invalid offset/length\n");
  865. return -EINVAL;
  866. }
  867. ret = validate_exec_list(exec, args->buffer_count);
  868. if (ret)
  869. return ret;
  870. #if WATCH_EXEC
  871. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  872. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  873. #endif
  874. switch (args->flags & I915_EXEC_RING_MASK) {
  875. case I915_EXEC_DEFAULT:
  876. case I915_EXEC_RENDER:
  877. ring = &dev_priv->ring[RCS];
  878. break;
  879. case I915_EXEC_BSD:
  880. if (!HAS_BSD(dev)) {
  881. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  882. return -EINVAL;
  883. }
  884. ring = &dev_priv->ring[VCS];
  885. break;
  886. case I915_EXEC_BLT:
  887. if (!HAS_BLT(dev)) {
  888. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  889. return -EINVAL;
  890. }
  891. ring = &dev_priv->ring[BCS];
  892. break;
  893. default:
  894. DRM_ERROR("execbuf with unknown ring: %d\n",
  895. (int)(args->flags & I915_EXEC_RING_MASK));
  896. return -EINVAL;
  897. }
  898. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  899. switch (mode) {
  900. case I915_EXEC_CONSTANTS_REL_GENERAL:
  901. case I915_EXEC_CONSTANTS_ABSOLUTE:
  902. case I915_EXEC_CONSTANTS_REL_SURFACE:
  903. if (ring == &dev_priv->ring[RCS] &&
  904. mode != dev_priv->relative_constants_mode) {
  905. if (INTEL_INFO(dev)->gen < 4)
  906. return -EINVAL;
  907. if (INTEL_INFO(dev)->gen > 5 &&
  908. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  909. return -EINVAL;
  910. ret = intel_ring_begin(ring, 4);
  911. if (ret)
  912. return ret;
  913. intel_ring_emit(ring, MI_NOOP);
  914. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  915. intel_ring_emit(ring, INSTPM);
  916. intel_ring_emit(ring,
  917. I915_EXEC_CONSTANTS_MASK << 16 | mode);
  918. intel_ring_advance(ring);
  919. dev_priv->relative_constants_mode = mode;
  920. }
  921. break;
  922. default:
  923. DRM_ERROR("execbuf with unknown constants: %d\n", mode);
  924. return -EINVAL;
  925. }
  926. if (args->buffer_count < 1) {
  927. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  928. return -EINVAL;
  929. }
  930. if (args->num_cliprects != 0) {
  931. if (ring != &dev_priv->ring[RCS]) {
  932. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  933. return -EINVAL;
  934. }
  935. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  936. GFP_KERNEL);
  937. if (cliprects == NULL) {
  938. ret = -ENOMEM;
  939. goto pre_mutex_err;
  940. }
  941. if (copy_from_user(cliprects,
  942. (struct drm_clip_rect __user *)(uintptr_t)
  943. args->cliprects_ptr,
  944. sizeof(*cliprects)*args->num_cliprects)) {
  945. ret = -EFAULT;
  946. goto pre_mutex_err;
  947. }
  948. }
  949. ret = i915_mutex_lock_interruptible(dev);
  950. if (ret)
  951. goto pre_mutex_err;
  952. if (dev_priv->mm.suspended) {
  953. mutex_unlock(&dev->struct_mutex);
  954. ret = -EBUSY;
  955. goto pre_mutex_err;
  956. }
  957. eb = eb_create(args->buffer_count);
  958. if (eb == NULL) {
  959. mutex_unlock(&dev->struct_mutex);
  960. ret = -ENOMEM;
  961. goto pre_mutex_err;
  962. }
  963. /* Look up object handles */
  964. INIT_LIST_HEAD(&objects);
  965. for (i = 0; i < args->buffer_count; i++) {
  966. struct drm_i915_gem_object *obj;
  967. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  968. exec[i].handle));
  969. if (obj == NULL) {
  970. DRM_ERROR("Invalid object handle %d at index %d\n",
  971. exec[i].handle, i);
  972. /* prevent error path from reading uninitialized data */
  973. ret = -ENOENT;
  974. goto err;
  975. }
  976. if (!list_empty(&obj->exec_list)) {
  977. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  978. obj, exec[i].handle, i);
  979. ret = -EINVAL;
  980. goto err;
  981. }
  982. list_add_tail(&obj->exec_list, &objects);
  983. obj->exec_handle = exec[i].handle;
  984. obj->exec_entry = &exec[i];
  985. eb_add_object(eb, obj);
  986. }
  987. /* take note of the batch buffer before we might reorder the lists */
  988. batch_obj = list_entry(objects.prev,
  989. struct drm_i915_gem_object,
  990. exec_list);
  991. /* Move the objects en-masse into the GTT, evicting if necessary. */
  992. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  993. if (ret)
  994. goto err;
  995. /* The objects are in their final locations, apply the relocations. */
  996. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  997. if (ret) {
  998. if (ret == -EFAULT) {
  999. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  1000. &objects, eb,
  1001. exec,
  1002. args->buffer_count);
  1003. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1004. }
  1005. if (ret)
  1006. goto err;
  1007. }
  1008. /* Set the pending read domains for the batch buffer to COMMAND */
  1009. if (batch_obj->base.pending_write_domain) {
  1010. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  1011. ret = -EINVAL;
  1012. goto err;
  1013. }
  1014. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1015. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  1016. if (ret)
  1017. goto err;
  1018. ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
  1019. if (ret)
  1020. goto err;
  1021. seqno = i915_gem_next_request_seqno(dev, ring);
  1022. for (i = 0; i < I915_NUM_RINGS-1; i++) {
  1023. if (seqno < ring->sync_seqno[i]) {
  1024. /* The GPU can not handle its semaphore value wrapping,
  1025. * so every billion or so execbuffers, we need to stall
  1026. * the GPU in order to reset the counters.
  1027. */
  1028. ret = i915_gpu_idle(dev);
  1029. if (ret)
  1030. goto err;
  1031. BUG_ON(ring->sync_seqno[i]);
  1032. }
  1033. }
  1034. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1035. exec_len = args->batch_len;
  1036. if (cliprects) {
  1037. for (i = 0; i < args->num_cliprects; i++) {
  1038. ret = i915_emit_box(dev, &cliprects[i],
  1039. args->DR1, args->DR4);
  1040. if (ret)
  1041. goto err;
  1042. ret = ring->dispatch_execbuffer(ring,
  1043. exec_start, exec_len);
  1044. if (ret)
  1045. goto err;
  1046. }
  1047. } else {
  1048. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1049. if (ret)
  1050. goto err;
  1051. }
  1052. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1053. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1054. err:
  1055. eb_destroy(eb);
  1056. while (!list_empty(&objects)) {
  1057. struct drm_i915_gem_object *obj;
  1058. obj = list_first_entry(&objects,
  1059. struct drm_i915_gem_object,
  1060. exec_list);
  1061. list_del_init(&obj->exec_list);
  1062. drm_gem_object_unreference(&obj->base);
  1063. }
  1064. mutex_unlock(&dev->struct_mutex);
  1065. pre_mutex_err:
  1066. kfree(cliprects);
  1067. return ret;
  1068. }
  1069. /*
  1070. * Legacy execbuffer just creates an exec2 list from the original exec object
  1071. * list array and passes it to the real function.
  1072. */
  1073. int
  1074. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1075. struct drm_file *file)
  1076. {
  1077. struct drm_i915_gem_execbuffer *args = data;
  1078. struct drm_i915_gem_execbuffer2 exec2;
  1079. struct drm_i915_gem_exec_object *exec_list = NULL;
  1080. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1081. int ret, i;
  1082. #if WATCH_EXEC
  1083. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1084. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1085. #endif
  1086. if (args->buffer_count < 1) {
  1087. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1088. return -EINVAL;
  1089. }
  1090. /* Copy in the exec list from userland */
  1091. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1092. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1093. if (exec_list == NULL || exec2_list == NULL) {
  1094. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1095. args->buffer_count);
  1096. drm_free_large(exec_list);
  1097. drm_free_large(exec2_list);
  1098. return -ENOMEM;
  1099. }
  1100. ret = copy_from_user(exec_list,
  1101. (struct drm_i915_relocation_entry __user *)
  1102. (uintptr_t) args->buffers_ptr,
  1103. sizeof(*exec_list) * args->buffer_count);
  1104. if (ret != 0) {
  1105. DRM_ERROR("copy %d exec entries failed %d\n",
  1106. args->buffer_count, ret);
  1107. drm_free_large(exec_list);
  1108. drm_free_large(exec2_list);
  1109. return -EFAULT;
  1110. }
  1111. for (i = 0; i < args->buffer_count; i++) {
  1112. exec2_list[i].handle = exec_list[i].handle;
  1113. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1114. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1115. exec2_list[i].alignment = exec_list[i].alignment;
  1116. exec2_list[i].offset = exec_list[i].offset;
  1117. if (INTEL_INFO(dev)->gen < 4)
  1118. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1119. else
  1120. exec2_list[i].flags = 0;
  1121. }
  1122. exec2.buffers_ptr = args->buffers_ptr;
  1123. exec2.buffer_count = args->buffer_count;
  1124. exec2.batch_start_offset = args->batch_start_offset;
  1125. exec2.batch_len = args->batch_len;
  1126. exec2.DR1 = args->DR1;
  1127. exec2.DR4 = args->DR4;
  1128. exec2.num_cliprects = args->num_cliprects;
  1129. exec2.cliprects_ptr = args->cliprects_ptr;
  1130. exec2.flags = I915_EXEC_RENDER;
  1131. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1132. if (!ret) {
  1133. /* Copy the new buffer offsets back to the user's exec list. */
  1134. for (i = 0; i < args->buffer_count; i++)
  1135. exec_list[i].offset = exec2_list[i].offset;
  1136. /* ... and back out to userspace */
  1137. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1138. (uintptr_t) args->buffers_ptr,
  1139. exec_list,
  1140. sizeof(*exec_list) * args->buffer_count);
  1141. if (ret) {
  1142. ret = -EFAULT;
  1143. DRM_ERROR("failed to copy %d exec entries "
  1144. "back to user (%d)\n",
  1145. args->buffer_count, ret);
  1146. }
  1147. }
  1148. drm_free_large(exec_list);
  1149. drm_free_large(exec2_list);
  1150. return ret;
  1151. }
  1152. int
  1153. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1154. struct drm_file *file)
  1155. {
  1156. struct drm_i915_gem_execbuffer2 *args = data;
  1157. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1158. int ret;
  1159. #if WATCH_EXEC
  1160. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1161. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1162. #endif
  1163. if (args->buffer_count < 1) {
  1164. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  1165. return -EINVAL;
  1166. }
  1167. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1168. if (exec2_list == NULL) {
  1169. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1170. args->buffer_count);
  1171. return -ENOMEM;
  1172. }
  1173. ret = copy_from_user(exec2_list,
  1174. (struct drm_i915_relocation_entry __user *)
  1175. (uintptr_t) args->buffers_ptr,
  1176. sizeof(*exec2_list) * args->buffer_count);
  1177. if (ret != 0) {
  1178. DRM_ERROR("copy %d exec entries failed %d\n",
  1179. args->buffer_count, ret);
  1180. drm_free_large(exec2_list);
  1181. return -EFAULT;
  1182. }
  1183. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1184. if (!ret) {
  1185. /* Copy the new buffer offsets back to the user's exec list. */
  1186. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1187. (uintptr_t) args->buffers_ptr,
  1188. exec2_list,
  1189. sizeof(*exec2_list) * args->buffer_count);
  1190. if (ret) {
  1191. ret = -EFAULT;
  1192. DRM_ERROR("failed to copy %d exec entries "
  1193. "back to user (%d)\n",
  1194. args->buffer_count, ret);
  1195. }
  1196. }
  1197. drm_free_large(exec2_list);
  1198. return ret;
  1199. }