i915_gem_execbuffer.c 39 KB

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