i915_gem_execbuffer.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  625. if (copy_from_user(reloc+total, user_relocs,
  626. exec[i].relocation_count * sizeof(*reloc))) {
  627. ret = -EFAULT;
  628. mutex_lock(&dev->struct_mutex);
  629. goto err;
  630. }
  631. reloc_offset[i] = total;
  632. total += exec[i].relocation_count;
  633. }
  634. ret = i915_mutex_lock_interruptible(dev);
  635. if (ret) {
  636. mutex_lock(&dev->struct_mutex);
  637. goto err;
  638. }
  639. /* reacquire the objects */
  640. eb_reset(eb);
  641. for (i = 0; i < count; i++) {
  642. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  643. exec[i].handle));
  644. if (&obj->base == NULL) {
  645. DRM_DEBUG("Invalid object handle %d at index %d\n",
  646. exec[i].handle, i);
  647. ret = -ENOENT;
  648. goto err;
  649. }
  650. list_add_tail(&obj->exec_list, objects);
  651. obj->exec_handle = exec[i].handle;
  652. obj->exec_entry = &exec[i];
  653. eb_add_object(eb, obj);
  654. }
  655. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  656. if (ret)
  657. goto err;
  658. list_for_each_entry(obj, objects, exec_list) {
  659. int offset = obj->exec_entry - exec;
  660. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  661. reloc + reloc_offset[offset]);
  662. if (ret)
  663. goto err;
  664. }
  665. /* Leave the user relocations as are, this is the painfully slow path,
  666. * and we want to avoid the complication of dropping the lock whilst
  667. * having buffers reserved in the aperture and so causing spurious
  668. * ENOSPC for random operations.
  669. */
  670. err:
  671. drm_free_large(reloc);
  672. drm_free_large(reloc_offset);
  673. return ret;
  674. }
  675. static int
  676. i915_gem_execbuffer_flush(struct drm_device *dev,
  677. uint32_t invalidate_domains,
  678. uint32_t flush_domains,
  679. uint32_t flush_rings)
  680. {
  681. drm_i915_private_t *dev_priv = dev->dev_private;
  682. int i, ret;
  683. if (flush_domains & I915_GEM_DOMAIN_CPU)
  684. intel_gtt_chipset_flush();
  685. if (flush_domains & I915_GEM_DOMAIN_GTT)
  686. wmb();
  687. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  688. for (i = 0; i < I915_NUM_RINGS; i++)
  689. if (flush_rings & (1 << i)) {
  690. ret = i915_gem_flush_ring(&dev_priv->ring[i],
  691. invalidate_domains,
  692. flush_domains);
  693. if (ret)
  694. return ret;
  695. }
  696. }
  697. return 0;
  698. }
  699. static bool
  700. intel_enable_semaphores(struct drm_device *dev)
  701. {
  702. if (INTEL_INFO(dev)->gen < 6)
  703. return 0;
  704. if (i915_semaphores >= 0)
  705. return i915_semaphores;
  706. /* Disable semaphores on SNB */
  707. if (INTEL_INFO(dev)->gen == 6)
  708. return 0;
  709. return 1;
  710. }
  711. static int
  712. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  713. struct intel_ring_buffer *to)
  714. {
  715. struct intel_ring_buffer *from = obj->ring;
  716. u32 seqno;
  717. int ret, idx;
  718. if (from == NULL || to == from)
  719. return 0;
  720. /* XXX gpu semaphores are implicated in various hard hangs on SNB */
  721. if (!intel_enable_semaphores(obj->base.dev))
  722. return i915_gem_object_wait_rendering(obj);
  723. idx = intel_ring_sync_index(from, to);
  724. seqno = obj->last_rendering_seqno;
  725. if (seqno <= from->sync_seqno[idx])
  726. return 0;
  727. if (seqno == from->outstanding_lazy_request) {
  728. struct drm_i915_gem_request *request;
  729. request = kzalloc(sizeof(*request), GFP_KERNEL);
  730. if (request == NULL)
  731. return -ENOMEM;
  732. ret = i915_add_request(from, NULL, request);
  733. if (ret) {
  734. kfree(request);
  735. return ret;
  736. }
  737. seqno = request->seqno;
  738. }
  739. from->sync_seqno[idx] = seqno;
  740. return to->sync_to(to, from, seqno - 1);
  741. }
  742. static int
  743. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  744. {
  745. u32 plane, flip_mask;
  746. int ret;
  747. /* Check for any pending flips. As we only maintain a flip queue depth
  748. * of 1, we can simply insert a WAIT for the next display flip prior
  749. * to executing the batch and avoid stalling the CPU.
  750. */
  751. for (plane = 0; flips >> plane; plane++) {
  752. if (((flips >> plane) & 1) == 0)
  753. continue;
  754. if (plane)
  755. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  756. else
  757. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  758. ret = intel_ring_begin(ring, 2);
  759. if (ret)
  760. return ret;
  761. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  762. intel_ring_emit(ring, MI_NOOP);
  763. intel_ring_advance(ring);
  764. }
  765. return 0;
  766. }
  767. static int
  768. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  769. struct list_head *objects)
  770. {
  771. struct drm_i915_gem_object *obj;
  772. struct change_domains cd;
  773. int ret;
  774. memset(&cd, 0, sizeof(cd));
  775. list_for_each_entry(obj, objects, exec_list)
  776. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  777. if (cd.invalidate_domains | cd.flush_domains) {
  778. ret = i915_gem_execbuffer_flush(ring->dev,
  779. cd.invalidate_domains,
  780. cd.flush_domains,
  781. cd.flush_rings);
  782. if (ret)
  783. return ret;
  784. }
  785. if (cd.flips) {
  786. ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
  787. if (ret)
  788. return ret;
  789. }
  790. list_for_each_entry(obj, objects, exec_list) {
  791. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  792. if (ret)
  793. return ret;
  794. }
  795. return 0;
  796. }
  797. static bool
  798. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  799. {
  800. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  801. }
  802. static int
  803. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  804. int count)
  805. {
  806. int i;
  807. for (i = 0; i < count; i++) {
  808. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  809. int length; /* limited by fault_in_pages_readable() */
  810. /* First check for malicious input causing overflow */
  811. if (exec[i].relocation_count >
  812. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  813. return -EINVAL;
  814. length = exec[i].relocation_count *
  815. sizeof(struct drm_i915_gem_relocation_entry);
  816. if (!access_ok(VERIFY_READ, ptr, length))
  817. return -EFAULT;
  818. /* we may also need to update the presumed offsets */
  819. if (!access_ok(VERIFY_WRITE, ptr, length))
  820. return -EFAULT;
  821. if (fault_in_pages_readable(ptr, length))
  822. return -EFAULT;
  823. }
  824. return 0;
  825. }
  826. static void
  827. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  828. struct intel_ring_buffer *ring,
  829. u32 seqno)
  830. {
  831. struct drm_i915_gem_object *obj;
  832. list_for_each_entry(obj, objects, exec_list) {
  833. u32 old_read = obj->base.read_domains;
  834. u32 old_write = obj->base.write_domain;
  835. obj->base.read_domains = obj->base.pending_read_domains;
  836. obj->base.write_domain = obj->base.pending_write_domain;
  837. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  838. i915_gem_object_move_to_active(obj, ring, seqno);
  839. if (obj->base.write_domain) {
  840. obj->dirty = 1;
  841. obj->pending_gpu_write = true;
  842. list_move_tail(&obj->gpu_write_list,
  843. &ring->gpu_write_list);
  844. intel_mark_busy(ring->dev, obj);
  845. }
  846. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  847. }
  848. }
  849. static void
  850. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  851. struct drm_file *file,
  852. struct intel_ring_buffer *ring)
  853. {
  854. struct drm_i915_gem_request *request;
  855. u32 invalidate;
  856. /*
  857. * Ensure that the commands in the batch buffer are
  858. * finished before the interrupt fires.
  859. *
  860. * The sampler always gets flushed on i965 (sigh).
  861. */
  862. invalidate = I915_GEM_DOMAIN_COMMAND;
  863. if (INTEL_INFO(dev)->gen >= 4)
  864. invalidate |= I915_GEM_DOMAIN_SAMPLER;
  865. if (ring->flush(ring, invalidate, 0)) {
  866. i915_gem_next_request_seqno(ring);
  867. return;
  868. }
  869. /* Add a breadcrumb for the completion of the batch buffer */
  870. request = kzalloc(sizeof(*request), GFP_KERNEL);
  871. if (request == NULL || i915_add_request(ring, file, request)) {
  872. i915_gem_next_request_seqno(ring);
  873. kfree(request);
  874. }
  875. }
  876. static int
  877. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  878. struct intel_ring_buffer *ring)
  879. {
  880. drm_i915_private_t *dev_priv = dev->dev_private;
  881. int ret, i;
  882. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  883. return 0;
  884. ret = intel_ring_begin(ring, 4 * 3);
  885. if (ret)
  886. return ret;
  887. for (i = 0; i < 4; i++) {
  888. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  889. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  890. intel_ring_emit(ring, 0);
  891. }
  892. intel_ring_advance(ring);
  893. return 0;
  894. }
  895. static int
  896. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  897. struct drm_file *file,
  898. struct drm_i915_gem_execbuffer2 *args,
  899. struct drm_i915_gem_exec_object2 *exec)
  900. {
  901. drm_i915_private_t *dev_priv = dev->dev_private;
  902. struct list_head objects;
  903. struct eb_objects *eb;
  904. struct drm_i915_gem_object *batch_obj;
  905. struct drm_clip_rect *cliprects = NULL;
  906. struct intel_ring_buffer *ring;
  907. u32 exec_start, exec_len;
  908. u32 seqno;
  909. u32 mask;
  910. int ret, mode, i;
  911. if (!i915_gem_check_execbuffer(args)) {
  912. DRM_DEBUG("execbuf with invalid offset/length\n");
  913. return -EINVAL;
  914. }
  915. ret = validate_exec_list(exec, args->buffer_count);
  916. if (ret)
  917. return ret;
  918. switch (args->flags & I915_EXEC_RING_MASK) {
  919. case I915_EXEC_DEFAULT:
  920. case I915_EXEC_RENDER:
  921. ring = &dev_priv->ring[RCS];
  922. break;
  923. case I915_EXEC_BSD:
  924. if (!HAS_BSD(dev)) {
  925. DRM_DEBUG("execbuf with invalid ring (BSD)\n");
  926. return -EINVAL;
  927. }
  928. ring = &dev_priv->ring[VCS];
  929. break;
  930. case I915_EXEC_BLT:
  931. if (!HAS_BLT(dev)) {
  932. DRM_DEBUG("execbuf with invalid ring (BLT)\n");
  933. return -EINVAL;
  934. }
  935. ring = &dev_priv->ring[BCS];
  936. break;
  937. default:
  938. DRM_DEBUG("execbuf with unknown ring: %d\n",
  939. (int)(args->flags & I915_EXEC_RING_MASK));
  940. return -EINVAL;
  941. }
  942. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  943. mask = I915_EXEC_CONSTANTS_MASK;
  944. switch (mode) {
  945. case I915_EXEC_CONSTANTS_REL_GENERAL:
  946. case I915_EXEC_CONSTANTS_ABSOLUTE:
  947. case I915_EXEC_CONSTANTS_REL_SURFACE:
  948. if (ring == &dev_priv->ring[RCS] &&
  949. mode != dev_priv->relative_constants_mode) {
  950. if (INTEL_INFO(dev)->gen < 4)
  951. return -EINVAL;
  952. if (INTEL_INFO(dev)->gen > 5 &&
  953. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  954. return -EINVAL;
  955. /* The HW changed the meaning on this bit on gen6 */
  956. if (INTEL_INFO(dev)->gen >= 6)
  957. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  958. }
  959. break;
  960. default:
  961. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  962. return -EINVAL;
  963. }
  964. if (args->buffer_count < 1) {
  965. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  966. return -EINVAL;
  967. }
  968. if (args->num_cliprects != 0) {
  969. if (ring != &dev_priv->ring[RCS]) {
  970. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  971. return -EINVAL;
  972. }
  973. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  974. GFP_KERNEL);
  975. if (cliprects == NULL) {
  976. ret = -ENOMEM;
  977. goto pre_mutex_err;
  978. }
  979. if (copy_from_user(cliprects,
  980. (struct drm_clip_rect __user *)(uintptr_t)
  981. args->cliprects_ptr,
  982. sizeof(*cliprects)*args->num_cliprects)) {
  983. ret = -EFAULT;
  984. goto pre_mutex_err;
  985. }
  986. }
  987. ret = i915_mutex_lock_interruptible(dev);
  988. if (ret)
  989. goto pre_mutex_err;
  990. if (dev_priv->mm.suspended) {
  991. mutex_unlock(&dev->struct_mutex);
  992. ret = -EBUSY;
  993. goto pre_mutex_err;
  994. }
  995. eb = eb_create(args->buffer_count);
  996. if (eb == NULL) {
  997. mutex_unlock(&dev->struct_mutex);
  998. ret = -ENOMEM;
  999. goto pre_mutex_err;
  1000. }
  1001. /* Look up object handles */
  1002. INIT_LIST_HEAD(&objects);
  1003. for (i = 0; i < args->buffer_count; i++) {
  1004. struct drm_i915_gem_object *obj;
  1005. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  1006. exec[i].handle));
  1007. if (&obj->base == NULL) {
  1008. DRM_DEBUG("Invalid object handle %d at index %d\n",
  1009. exec[i].handle, i);
  1010. /* prevent error path from reading uninitialized data */
  1011. ret = -ENOENT;
  1012. goto err;
  1013. }
  1014. if (!list_empty(&obj->exec_list)) {
  1015. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  1016. obj, exec[i].handle, i);
  1017. ret = -EINVAL;
  1018. goto err;
  1019. }
  1020. list_add_tail(&obj->exec_list, &objects);
  1021. obj->exec_handle = exec[i].handle;
  1022. obj->exec_entry = &exec[i];
  1023. eb_add_object(eb, obj);
  1024. }
  1025. /* take note of the batch buffer before we might reorder the lists */
  1026. batch_obj = list_entry(objects.prev,
  1027. struct drm_i915_gem_object,
  1028. exec_list);
  1029. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1030. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  1031. if (ret)
  1032. goto err;
  1033. /* The objects are in their final locations, apply the relocations. */
  1034. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  1035. if (ret) {
  1036. if (ret == -EFAULT) {
  1037. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  1038. &objects, eb,
  1039. exec,
  1040. args->buffer_count);
  1041. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1042. }
  1043. if (ret)
  1044. goto err;
  1045. }
  1046. /* Set the pending read domains for the batch buffer to COMMAND */
  1047. if (batch_obj->base.pending_write_domain) {
  1048. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1049. ret = -EINVAL;
  1050. goto err;
  1051. }
  1052. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1053. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  1054. if (ret)
  1055. goto err;
  1056. seqno = i915_gem_next_request_seqno(ring);
  1057. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  1058. if (seqno < ring->sync_seqno[i]) {
  1059. /* The GPU can not handle its semaphore value wrapping,
  1060. * so every billion or so execbuffers, we need to stall
  1061. * the GPU in order to reset the counters.
  1062. */
  1063. ret = i915_gpu_idle(dev, true);
  1064. if (ret)
  1065. goto err;
  1066. BUG_ON(ring->sync_seqno[i]);
  1067. }
  1068. }
  1069. if (ring == &dev_priv->ring[RCS] &&
  1070. mode != dev_priv->relative_constants_mode) {
  1071. ret = intel_ring_begin(ring, 4);
  1072. if (ret)
  1073. goto err;
  1074. intel_ring_emit(ring, MI_NOOP);
  1075. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1076. intel_ring_emit(ring, INSTPM);
  1077. intel_ring_emit(ring, mask << 16 | mode);
  1078. intel_ring_advance(ring);
  1079. dev_priv->relative_constants_mode = mode;
  1080. }
  1081. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1082. ret = i915_reset_gen7_sol_offsets(dev, ring);
  1083. if (ret)
  1084. goto err;
  1085. }
  1086. trace_i915_gem_ring_dispatch(ring, seqno);
  1087. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1088. exec_len = args->batch_len;
  1089. if (cliprects) {
  1090. for (i = 0; i < args->num_cliprects; i++) {
  1091. ret = i915_emit_box(dev, &cliprects[i],
  1092. args->DR1, args->DR4);
  1093. if (ret)
  1094. goto err;
  1095. ret = ring->dispatch_execbuffer(ring,
  1096. exec_start, exec_len);
  1097. if (ret)
  1098. goto err;
  1099. }
  1100. } else {
  1101. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1102. if (ret)
  1103. goto err;
  1104. }
  1105. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1106. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1107. err:
  1108. eb_destroy(eb);
  1109. while (!list_empty(&objects)) {
  1110. struct drm_i915_gem_object *obj;
  1111. obj = list_first_entry(&objects,
  1112. struct drm_i915_gem_object,
  1113. exec_list);
  1114. list_del_init(&obj->exec_list);
  1115. drm_gem_object_unreference(&obj->base);
  1116. }
  1117. mutex_unlock(&dev->struct_mutex);
  1118. pre_mutex_err:
  1119. kfree(cliprects);
  1120. return ret;
  1121. }
  1122. /*
  1123. * Legacy execbuffer just creates an exec2 list from the original exec object
  1124. * list array and passes it to the real function.
  1125. */
  1126. int
  1127. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1128. struct drm_file *file)
  1129. {
  1130. struct drm_i915_gem_execbuffer *args = data;
  1131. struct drm_i915_gem_execbuffer2 exec2;
  1132. struct drm_i915_gem_exec_object *exec_list = NULL;
  1133. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1134. int ret, i;
  1135. if (args->buffer_count < 1) {
  1136. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1137. return -EINVAL;
  1138. }
  1139. /* Copy in the exec list from userland */
  1140. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1141. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1142. if (exec_list == NULL || exec2_list == NULL) {
  1143. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1144. args->buffer_count);
  1145. drm_free_large(exec_list);
  1146. drm_free_large(exec2_list);
  1147. return -ENOMEM;
  1148. }
  1149. ret = copy_from_user(exec_list,
  1150. (struct drm_i915_relocation_entry __user *)
  1151. (uintptr_t) args->buffers_ptr,
  1152. sizeof(*exec_list) * args->buffer_count);
  1153. if (ret != 0) {
  1154. DRM_DEBUG("copy %d exec entries failed %d\n",
  1155. args->buffer_count, ret);
  1156. drm_free_large(exec_list);
  1157. drm_free_large(exec2_list);
  1158. return -EFAULT;
  1159. }
  1160. for (i = 0; i < args->buffer_count; i++) {
  1161. exec2_list[i].handle = exec_list[i].handle;
  1162. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1163. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1164. exec2_list[i].alignment = exec_list[i].alignment;
  1165. exec2_list[i].offset = exec_list[i].offset;
  1166. if (INTEL_INFO(dev)->gen < 4)
  1167. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1168. else
  1169. exec2_list[i].flags = 0;
  1170. }
  1171. exec2.buffers_ptr = args->buffers_ptr;
  1172. exec2.buffer_count = args->buffer_count;
  1173. exec2.batch_start_offset = args->batch_start_offset;
  1174. exec2.batch_len = args->batch_len;
  1175. exec2.DR1 = args->DR1;
  1176. exec2.DR4 = args->DR4;
  1177. exec2.num_cliprects = args->num_cliprects;
  1178. exec2.cliprects_ptr = args->cliprects_ptr;
  1179. exec2.flags = I915_EXEC_RENDER;
  1180. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1181. if (!ret) {
  1182. /* Copy the new buffer offsets back to the user's exec list. */
  1183. for (i = 0; i < args->buffer_count; i++)
  1184. exec_list[i].offset = exec2_list[i].offset;
  1185. /* ... and back out to userspace */
  1186. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1187. (uintptr_t) args->buffers_ptr,
  1188. exec_list,
  1189. sizeof(*exec_list) * args->buffer_count);
  1190. if (ret) {
  1191. ret = -EFAULT;
  1192. DRM_DEBUG("failed to copy %d exec entries "
  1193. "back to user (%d)\n",
  1194. args->buffer_count, ret);
  1195. }
  1196. }
  1197. drm_free_large(exec_list);
  1198. drm_free_large(exec2_list);
  1199. return ret;
  1200. }
  1201. int
  1202. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1203. struct drm_file *file)
  1204. {
  1205. struct drm_i915_gem_execbuffer2 *args = data;
  1206. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1207. int ret;
  1208. if (args->buffer_count < 1 ||
  1209. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1210. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1211. return -EINVAL;
  1212. }
  1213. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1214. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1215. if (exec2_list == NULL)
  1216. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1217. args->buffer_count);
  1218. if (exec2_list == NULL) {
  1219. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1220. args->buffer_count);
  1221. return -ENOMEM;
  1222. }
  1223. ret = copy_from_user(exec2_list,
  1224. (struct drm_i915_relocation_entry __user *)
  1225. (uintptr_t) args->buffers_ptr,
  1226. sizeof(*exec2_list) * args->buffer_count);
  1227. if (ret != 0) {
  1228. DRM_DEBUG("copy %d exec entries failed %d\n",
  1229. args->buffer_count, ret);
  1230. drm_free_large(exec2_list);
  1231. return -EFAULT;
  1232. }
  1233. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1234. if (!ret) {
  1235. /* Copy the new buffer offsets back to the user's exec list. */
  1236. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1237. (uintptr_t) args->buffers_ptr,
  1238. exec2_list,
  1239. sizeof(*exec2_list) * args->buffer_count);
  1240. if (ret) {
  1241. ret = -EFAULT;
  1242. DRM_DEBUG("failed to copy %d exec entries "
  1243. "back to user (%d)\n",
  1244. args->buffer_count, ret);
  1245. }
  1246. }
  1247. drm_free_large(exec2_list);
  1248. return ret;
  1249. }