i915_gem_execbuffer.c 39 KB

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