i915_gem_execbuffer.c 38 KB

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