i915_gem_execbuffer.c 35 KB

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