i915_gem_execbuffer.c 38 KB

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