i915_gem_execbuffer.c 35 KB

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