i915_gem_execbuffer.c 38 KB

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