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