i915_gem_execbuffer.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. /* blow away mappings if mapped through GTT */
  185. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
  186. i915_gem_release_mmap(obj);
  187. if (obj->base.pending_write_domain)
  188. cd->flips |= atomic_read(&obj->pending_flip);
  189. /* The actual obj->write_domain will be updated with
  190. * pending_write_domain after we emit the accumulated flush for all
  191. * of our domain changes in execbuffers (which clears objects'
  192. * write_domains). So if we have a current write domain that we
  193. * aren't changing, set pending_write_domain to that.
  194. */
  195. if (flush_domains == 0 && obj->base.pending_write_domain == 0)
  196. obj->base.pending_write_domain = obj->base.write_domain;
  197. cd->invalidate_domains |= invalidate_domains;
  198. cd->flush_domains |= flush_domains;
  199. if (flush_domains & I915_GEM_GPU_DOMAINS)
  200. cd->flush_rings |= obj->ring->id;
  201. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  202. cd->flush_rings |= ring->id;
  203. }
  204. struct eb_objects {
  205. int and;
  206. struct hlist_head buckets[0];
  207. };
  208. static struct eb_objects *
  209. eb_create(int size)
  210. {
  211. struct eb_objects *eb;
  212. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  213. while (count > size)
  214. count >>= 1;
  215. eb = kzalloc(count*sizeof(struct hlist_head) +
  216. sizeof(struct eb_objects),
  217. GFP_KERNEL);
  218. if (eb == NULL)
  219. return eb;
  220. eb->and = count - 1;
  221. return eb;
  222. }
  223. static void
  224. eb_reset(struct eb_objects *eb)
  225. {
  226. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  227. }
  228. static void
  229. eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
  230. {
  231. hlist_add_head(&obj->exec_node,
  232. &eb->buckets[obj->exec_handle & eb->and]);
  233. }
  234. static struct drm_i915_gem_object *
  235. eb_get_object(struct eb_objects *eb, unsigned long handle)
  236. {
  237. struct hlist_head *head;
  238. struct hlist_node *node;
  239. struct drm_i915_gem_object *obj;
  240. head = &eb->buckets[handle & eb->and];
  241. hlist_for_each(node, head) {
  242. obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
  243. if (obj->exec_handle == handle)
  244. return obj;
  245. }
  246. return NULL;
  247. }
  248. static void
  249. eb_destroy(struct eb_objects *eb)
  250. {
  251. kfree(eb);
  252. }
  253. static int
  254. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  255. struct eb_objects *eb,
  256. struct drm_i915_gem_relocation_entry *reloc)
  257. {
  258. struct drm_device *dev = obj->base.dev;
  259. struct drm_gem_object *target_obj;
  260. uint32_t target_offset;
  261. int ret = -EINVAL;
  262. /* we've already hold a reference to all valid objects */
  263. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  264. if (unlikely(target_obj == NULL))
  265. return -ENOENT;
  266. target_offset = to_intel_bo(target_obj)->gtt_offset;
  267. /* The target buffer should have appeared before us in the
  268. * exec_object list, so it should have a GTT space bound by now.
  269. */
  270. if (unlikely(target_offset == 0)) {
  271. DRM_ERROR("No GTT space found for object %d\n",
  272. reloc->target_handle);
  273. return ret;
  274. }
  275. /* Validate that the target is in a valid r/w GPU domain */
  276. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  277. DRM_ERROR("reloc with multiple write domains: "
  278. "obj %p target %d offset %d "
  279. "read %08x write %08x",
  280. obj, reloc->target_handle,
  281. (int) reloc->offset,
  282. reloc->read_domains,
  283. reloc->write_domain);
  284. return ret;
  285. }
  286. if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
  287. DRM_ERROR("reloc with read/write CPU domains: "
  288. "obj %p target %d offset %d "
  289. "read %08x write %08x",
  290. obj, reloc->target_handle,
  291. (int) reloc->offset,
  292. reloc->read_domains,
  293. reloc->write_domain);
  294. return ret;
  295. }
  296. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  297. reloc->write_domain != target_obj->pending_write_domain)) {
  298. DRM_ERROR("Write domain conflict: "
  299. "obj %p target %d offset %d "
  300. "new %08x old %08x\n",
  301. obj, reloc->target_handle,
  302. (int) reloc->offset,
  303. reloc->write_domain,
  304. target_obj->pending_write_domain);
  305. return ret;
  306. }
  307. target_obj->pending_read_domains |= reloc->read_domains;
  308. target_obj->pending_write_domain |= reloc->write_domain;
  309. /* If the relocation already has the right value in it, no
  310. * more work needs to be done.
  311. */
  312. if (target_offset == reloc->presumed_offset)
  313. return 0;
  314. /* Check that the relocation address is valid... */
  315. if (unlikely(reloc->offset > obj->base.size - 4)) {
  316. DRM_ERROR("Relocation beyond object bounds: "
  317. "obj %p target %d offset %d size %d.\n",
  318. obj, reloc->target_handle,
  319. (int) reloc->offset,
  320. (int) obj->base.size);
  321. return ret;
  322. }
  323. if (unlikely(reloc->offset & 3)) {
  324. DRM_ERROR("Relocation not 4-byte aligned: "
  325. "obj %p target %d offset %d.\n",
  326. obj, reloc->target_handle,
  327. (int) reloc->offset);
  328. return ret;
  329. }
  330. reloc->delta += target_offset;
  331. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  332. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  333. char *vaddr;
  334. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  335. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  336. kunmap_atomic(vaddr);
  337. } else {
  338. struct drm_i915_private *dev_priv = dev->dev_private;
  339. uint32_t __iomem *reloc_entry;
  340. void __iomem *reloc_page;
  341. /* We can't wait for rendering with pagefaults disabled */
  342. if (obj->active && in_atomic())
  343. return -EFAULT;
  344. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  345. if (ret)
  346. return ret;
  347. /* Map the page containing the relocation we're going to perform. */
  348. reloc->offset += obj->gtt_offset;
  349. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  350. reloc->offset & PAGE_MASK);
  351. reloc_entry = (uint32_t __iomem *)
  352. (reloc_page + (reloc->offset & ~PAGE_MASK));
  353. iowrite32(reloc->delta, reloc_entry);
  354. io_mapping_unmap_atomic(reloc_page);
  355. }
  356. /* and update the user's relocation entry */
  357. reloc->presumed_offset = target_offset;
  358. return 0;
  359. }
  360. static int
  361. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  362. struct eb_objects *eb)
  363. {
  364. struct drm_i915_gem_relocation_entry __user *user_relocs;
  365. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  366. int i, ret;
  367. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  368. for (i = 0; i < entry->relocation_count; i++) {
  369. struct drm_i915_gem_relocation_entry reloc;
  370. if (__copy_from_user_inatomic(&reloc,
  371. user_relocs+i,
  372. sizeof(reloc)))
  373. return -EFAULT;
  374. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
  375. if (ret)
  376. return ret;
  377. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  378. &reloc.presumed_offset,
  379. sizeof(reloc.presumed_offset)))
  380. return -EFAULT;
  381. }
  382. return 0;
  383. }
  384. static int
  385. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  386. struct eb_objects *eb,
  387. struct drm_i915_gem_relocation_entry *relocs)
  388. {
  389. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  390. int i, ret;
  391. for (i = 0; i < entry->relocation_count; i++) {
  392. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  393. if (ret)
  394. return ret;
  395. }
  396. return 0;
  397. }
  398. static int
  399. i915_gem_execbuffer_relocate(struct drm_device *dev,
  400. struct eb_objects *eb,
  401. struct list_head *objects)
  402. {
  403. struct drm_i915_gem_object *obj;
  404. int ret = 0;
  405. /* This is the fast path and we cannot handle a pagefault whilst
  406. * holding the struct mutex lest the user pass in the relocations
  407. * contained within a mmaped bo. For in such a case we, the page
  408. * fault handler would call i915_gem_fault() and we would try to
  409. * acquire the struct mutex again. Obviously this is bad and so
  410. * lockdep complains vehemently.
  411. */
  412. pagefault_disable();
  413. list_for_each_entry(obj, objects, exec_list) {
  414. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  415. if (ret)
  416. break;
  417. }
  418. pagefault_enable();
  419. return ret;
  420. }
  421. static int
  422. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  423. struct drm_file *file,
  424. struct list_head *objects)
  425. {
  426. struct drm_i915_gem_object *obj;
  427. int ret, retry;
  428. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  429. struct list_head ordered_objects;
  430. INIT_LIST_HEAD(&ordered_objects);
  431. while (!list_empty(objects)) {
  432. struct drm_i915_gem_exec_object2 *entry;
  433. bool need_fence, need_mappable;
  434. obj = list_first_entry(objects,
  435. struct drm_i915_gem_object,
  436. exec_list);
  437. entry = obj->exec_entry;
  438. need_fence =
  439. has_fenced_gpu_access &&
  440. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  441. obj->tiling_mode != I915_TILING_NONE;
  442. need_mappable =
  443. entry->relocation_count ? true : need_fence;
  444. if (need_mappable)
  445. list_move(&obj->exec_list, &ordered_objects);
  446. else
  447. list_move_tail(&obj->exec_list, &ordered_objects);
  448. obj->base.pending_read_domains = 0;
  449. obj->base.pending_write_domain = 0;
  450. }
  451. list_splice(&ordered_objects, objects);
  452. /* Attempt to pin all of the buffers into the GTT.
  453. * This is done in 3 phases:
  454. *
  455. * 1a. Unbind all objects that do not match the GTT constraints for
  456. * the execbuffer (fenceable, mappable, alignment etc).
  457. * 1b. Increment pin count for already bound objects.
  458. * 2. Bind new objects.
  459. * 3. Decrement pin count.
  460. *
  461. * This avoid unnecessary unbinding of later objects in order to makr
  462. * room for the earlier objects *unless* we need to defragment.
  463. */
  464. retry = 0;
  465. do {
  466. ret = 0;
  467. /* Unbind any ill-fitting objects or pin. */
  468. list_for_each_entry(obj, objects, exec_list) {
  469. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  470. bool need_fence, need_mappable;
  471. if (!obj->gtt_space)
  472. continue;
  473. need_fence =
  474. has_fenced_gpu_access &&
  475. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  476. obj->tiling_mode != I915_TILING_NONE;
  477. need_mappable =
  478. entry->relocation_count ? true : need_fence;
  479. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  480. (need_mappable && !obj->map_and_fenceable))
  481. ret = i915_gem_object_unbind(obj);
  482. else
  483. ret = i915_gem_object_pin(obj,
  484. entry->alignment,
  485. need_mappable);
  486. if (ret)
  487. goto err;
  488. entry++;
  489. }
  490. /* Bind fresh objects */
  491. list_for_each_entry(obj, objects, exec_list) {
  492. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  493. bool need_fence;
  494. need_fence =
  495. has_fenced_gpu_access &&
  496. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  497. obj->tiling_mode != I915_TILING_NONE;
  498. if (!obj->gtt_space) {
  499. bool need_mappable =
  500. entry->relocation_count ? true : need_fence;
  501. ret = i915_gem_object_pin(obj,
  502. entry->alignment,
  503. need_mappable);
  504. if (ret)
  505. break;
  506. }
  507. if (has_fenced_gpu_access) {
  508. if (need_fence) {
  509. ret = i915_gem_object_get_fence(obj, ring);
  510. if (ret)
  511. break;
  512. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  513. obj->tiling_mode == I915_TILING_NONE) {
  514. /* XXX pipelined! */
  515. ret = i915_gem_object_put_fence(obj);
  516. if (ret)
  517. break;
  518. }
  519. obj->pending_fenced_gpu_access = need_fence;
  520. }
  521. entry->offset = obj->gtt_offset;
  522. }
  523. /* Decrement pin count for bound objects */
  524. list_for_each_entry(obj, objects, exec_list) {
  525. if (obj->gtt_space)
  526. i915_gem_object_unpin(obj);
  527. }
  528. if (ret != -ENOSPC || retry > 1)
  529. return ret;
  530. /* First attempt, just clear anything that is purgeable.
  531. * Second attempt, clear the entire GTT.
  532. */
  533. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  534. if (ret)
  535. return ret;
  536. retry++;
  537. } while (1);
  538. err:
  539. obj = list_entry(obj->exec_list.prev,
  540. struct drm_i915_gem_object,
  541. exec_list);
  542. while (objects != &obj->exec_list) {
  543. if (obj->gtt_space)
  544. i915_gem_object_unpin(obj);
  545. obj = list_entry(obj->exec_list.prev,
  546. struct drm_i915_gem_object,
  547. exec_list);
  548. }
  549. return ret;
  550. }
  551. static int
  552. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  553. struct drm_file *file,
  554. struct intel_ring_buffer *ring,
  555. struct list_head *objects,
  556. struct eb_objects *eb,
  557. struct drm_i915_gem_exec_object2 *exec,
  558. int count)
  559. {
  560. struct drm_i915_gem_relocation_entry *reloc;
  561. struct drm_i915_gem_object *obj;
  562. int *reloc_offset;
  563. int i, total, ret;
  564. /* We may process another execbuffer during the unlock... */
  565. while (!list_empty(objects)) {
  566. obj = list_first_entry(objects,
  567. struct drm_i915_gem_object,
  568. exec_list);
  569. list_del_init(&obj->exec_list);
  570. drm_gem_object_unreference(&obj->base);
  571. }
  572. mutex_unlock(&dev->struct_mutex);
  573. total = 0;
  574. for (i = 0; i < count; i++)
  575. total += exec[i].relocation_count;
  576. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  577. reloc = drm_malloc_ab(total, sizeof(*reloc));
  578. if (reloc == NULL || reloc_offset == NULL) {
  579. drm_free_large(reloc);
  580. drm_free_large(reloc_offset);
  581. mutex_lock(&dev->struct_mutex);
  582. return -ENOMEM;
  583. }
  584. total = 0;
  585. for (i = 0; i < count; i++) {
  586. struct drm_i915_gem_relocation_entry __user *user_relocs;
  587. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  588. if (copy_from_user(reloc+total, user_relocs,
  589. exec[i].relocation_count * sizeof(*reloc))) {
  590. ret = -EFAULT;
  591. mutex_lock(&dev->struct_mutex);
  592. goto err;
  593. }
  594. reloc_offset[i] = total;
  595. total += exec[i].relocation_count;
  596. }
  597. ret = i915_mutex_lock_interruptible(dev);
  598. if (ret) {
  599. mutex_lock(&dev->struct_mutex);
  600. goto err;
  601. }
  602. /* reacquire the objects */
  603. eb_reset(eb);
  604. for (i = 0; i < count; i++) {
  605. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  606. exec[i].handle));
  607. if (&obj->base == NULL) {
  608. DRM_ERROR("Invalid object handle %d at index %d\n",
  609. exec[i].handle, i);
  610. ret = -ENOENT;
  611. goto err;
  612. }
  613. list_add_tail(&obj->exec_list, objects);
  614. obj->exec_handle = exec[i].handle;
  615. obj->exec_entry = &exec[i];
  616. eb_add_object(eb, obj);
  617. }
  618. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  619. if (ret)
  620. goto err;
  621. list_for_each_entry(obj, objects, exec_list) {
  622. int offset = obj->exec_entry - exec;
  623. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  624. reloc + reloc_offset[offset]);
  625. if (ret)
  626. goto err;
  627. }
  628. /* Leave the user relocations as are, this is the painfully slow path,
  629. * and we want to avoid the complication of dropping the lock whilst
  630. * having buffers reserved in the aperture and so causing spurious
  631. * ENOSPC for random operations.
  632. */
  633. err:
  634. drm_free_large(reloc);
  635. drm_free_large(reloc_offset);
  636. return ret;
  637. }
  638. static int
  639. i915_gem_execbuffer_flush(struct drm_device *dev,
  640. uint32_t invalidate_domains,
  641. uint32_t flush_domains,
  642. uint32_t flush_rings)
  643. {
  644. drm_i915_private_t *dev_priv = dev->dev_private;
  645. int i, ret;
  646. if (flush_domains & I915_GEM_DOMAIN_CPU)
  647. intel_gtt_chipset_flush();
  648. if (flush_domains & I915_GEM_DOMAIN_GTT)
  649. wmb();
  650. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  651. for (i = 0; i < I915_NUM_RINGS; i++)
  652. if (flush_rings & (1 << i)) {
  653. ret = i915_gem_flush_ring(&dev_priv->ring[i],
  654. invalidate_domains,
  655. flush_domains);
  656. if (ret)
  657. return ret;
  658. }
  659. }
  660. return 0;
  661. }
  662. static int
  663. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  664. struct intel_ring_buffer *to)
  665. {
  666. struct intel_ring_buffer *from = obj->ring;
  667. u32 seqno;
  668. int ret, idx;
  669. if (from == NULL || to == from)
  670. return 0;
  671. /* XXX gpu semaphores are implicated in various hard hangs on SNB */
  672. if (INTEL_INFO(obj->base.dev)->gen < 6 || !i915_semaphores)
  673. return i915_gem_object_wait_rendering(obj);
  674. idx = intel_ring_sync_index(from, to);
  675. seqno = obj->last_rendering_seqno;
  676. if (seqno <= from->sync_seqno[idx])
  677. return 0;
  678. if (seqno == from->outstanding_lazy_request) {
  679. struct drm_i915_gem_request *request;
  680. request = kzalloc(sizeof(*request), GFP_KERNEL);
  681. if (request == NULL)
  682. return -ENOMEM;
  683. ret = i915_add_request(from, NULL, request);
  684. if (ret) {
  685. kfree(request);
  686. return ret;
  687. }
  688. seqno = request->seqno;
  689. }
  690. from->sync_seqno[idx] = seqno;
  691. return intel_ring_sync(to, from, seqno - 1);
  692. }
  693. static int
  694. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  695. {
  696. u32 plane, flip_mask;
  697. int ret;
  698. /* Check for any pending flips. As we only maintain a flip queue depth
  699. * of 1, we can simply insert a WAIT for the next display flip prior
  700. * to executing the batch and avoid stalling the CPU.
  701. */
  702. for (plane = 0; flips >> plane; plane++) {
  703. if (((flips >> plane) & 1) == 0)
  704. continue;
  705. if (plane)
  706. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  707. else
  708. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  709. ret = intel_ring_begin(ring, 2);
  710. if (ret)
  711. return ret;
  712. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  713. intel_ring_emit(ring, MI_NOOP);
  714. intel_ring_advance(ring);
  715. }
  716. return 0;
  717. }
  718. static int
  719. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  720. struct list_head *objects)
  721. {
  722. struct drm_i915_gem_object *obj;
  723. struct change_domains cd;
  724. int ret;
  725. memset(&cd, 0, sizeof(cd));
  726. list_for_each_entry(obj, objects, exec_list)
  727. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  728. if (cd.invalidate_domains | cd.flush_domains) {
  729. ret = i915_gem_execbuffer_flush(ring->dev,
  730. cd.invalidate_domains,
  731. cd.flush_domains,
  732. cd.flush_rings);
  733. if (ret)
  734. return ret;
  735. }
  736. if (cd.flips) {
  737. ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
  738. if (ret)
  739. return ret;
  740. }
  741. list_for_each_entry(obj, objects, exec_list) {
  742. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  743. if (ret)
  744. return ret;
  745. }
  746. return 0;
  747. }
  748. static bool
  749. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  750. {
  751. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  752. }
  753. static int
  754. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  755. int count)
  756. {
  757. int i;
  758. for (i = 0; i < count; i++) {
  759. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  760. int length; /* limited by fault_in_pages_readable() */
  761. /* First check for malicious input causing overflow */
  762. if (exec[i].relocation_count >
  763. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  764. return -EINVAL;
  765. length = exec[i].relocation_count *
  766. sizeof(struct drm_i915_gem_relocation_entry);
  767. if (!access_ok(VERIFY_READ, ptr, length))
  768. return -EFAULT;
  769. /* we may also need to update the presumed offsets */
  770. if (!access_ok(VERIFY_WRITE, ptr, length))
  771. return -EFAULT;
  772. if (fault_in_pages_readable(ptr, length))
  773. return -EFAULT;
  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->base == 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. seqno = i915_gem_next_request_seqno(ring);
  994. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  995. if (seqno < ring->sync_seqno[i]) {
  996. /* The GPU can not handle its semaphore value wrapping,
  997. * so every billion or so execbuffers, we need to stall
  998. * the GPU in order to reset the counters.
  999. */
  1000. ret = i915_gpu_idle(dev);
  1001. if (ret)
  1002. goto err;
  1003. BUG_ON(ring->sync_seqno[i]);
  1004. }
  1005. }
  1006. trace_i915_gem_ring_dispatch(ring, seqno);
  1007. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1008. exec_len = args->batch_len;
  1009. if (cliprects) {
  1010. for (i = 0; i < args->num_cliprects; i++) {
  1011. ret = i915_emit_box(dev, &cliprects[i],
  1012. args->DR1, args->DR4);
  1013. if (ret)
  1014. goto err;
  1015. ret = ring->dispatch_execbuffer(ring,
  1016. exec_start, exec_len);
  1017. if (ret)
  1018. goto err;
  1019. }
  1020. } else {
  1021. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1022. if (ret)
  1023. goto err;
  1024. }
  1025. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1026. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1027. err:
  1028. eb_destroy(eb);
  1029. while (!list_empty(&objects)) {
  1030. struct drm_i915_gem_object *obj;
  1031. obj = list_first_entry(&objects,
  1032. struct drm_i915_gem_object,
  1033. exec_list);
  1034. list_del_init(&obj->exec_list);
  1035. drm_gem_object_unreference(&obj->base);
  1036. }
  1037. mutex_unlock(&dev->struct_mutex);
  1038. pre_mutex_err:
  1039. kfree(cliprects);
  1040. return ret;
  1041. }
  1042. /*
  1043. * Legacy execbuffer just creates an exec2 list from the original exec object
  1044. * list array and passes it to the real function.
  1045. */
  1046. int
  1047. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1048. struct drm_file *file)
  1049. {
  1050. struct drm_i915_gem_execbuffer *args = data;
  1051. struct drm_i915_gem_execbuffer2 exec2;
  1052. struct drm_i915_gem_exec_object *exec_list = NULL;
  1053. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1054. int ret, i;
  1055. if (args->buffer_count < 1) {
  1056. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1057. return -EINVAL;
  1058. }
  1059. /* Copy in the exec list from userland */
  1060. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1061. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1062. if (exec_list == NULL || exec2_list == NULL) {
  1063. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1064. args->buffer_count);
  1065. drm_free_large(exec_list);
  1066. drm_free_large(exec2_list);
  1067. return -ENOMEM;
  1068. }
  1069. ret = copy_from_user(exec_list,
  1070. (struct drm_i915_relocation_entry __user *)
  1071. (uintptr_t) args->buffers_ptr,
  1072. sizeof(*exec_list) * args->buffer_count);
  1073. if (ret != 0) {
  1074. DRM_ERROR("copy %d exec entries failed %d\n",
  1075. args->buffer_count, ret);
  1076. drm_free_large(exec_list);
  1077. drm_free_large(exec2_list);
  1078. return -EFAULT;
  1079. }
  1080. for (i = 0; i < args->buffer_count; i++) {
  1081. exec2_list[i].handle = exec_list[i].handle;
  1082. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1083. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1084. exec2_list[i].alignment = exec_list[i].alignment;
  1085. exec2_list[i].offset = exec_list[i].offset;
  1086. if (INTEL_INFO(dev)->gen < 4)
  1087. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1088. else
  1089. exec2_list[i].flags = 0;
  1090. }
  1091. exec2.buffers_ptr = args->buffers_ptr;
  1092. exec2.buffer_count = args->buffer_count;
  1093. exec2.batch_start_offset = args->batch_start_offset;
  1094. exec2.batch_len = args->batch_len;
  1095. exec2.DR1 = args->DR1;
  1096. exec2.DR4 = args->DR4;
  1097. exec2.num_cliprects = args->num_cliprects;
  1098. exec2.cliprects_ptr = args->cliprects_ptr;
  1099. exec2.flags = I915_EXEC_RENDER;
  1100. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1101. if (!ret) {
  1102. /* Copy the new buffer offsets back to the user's exec list. */
  1103. for (i = 0; i < args->buffer_count; i++)
  1104. exec_list[i].offset = exec2_list[i].offset;
  1105. /* ... and back out to userspace */
  1106. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1107. (uintptr_t) args->buffers_ptr,
  1108. exec_list,
  1109. sizeof(*exec_list) * args->buffer_count);
  1110. if (ret) {
  1111. ret = -EFAULT;
  1112. DRM_ERROR("failed to copy %d exec entries "
  1113. "back to user (%d)\n",
  1114. args->buffer_count, ret);
  1115. }
  1116. }
  1117. drm_free_large(exec_list);
  1118. drm_free_large(exec2_list);
  1119. return ret;
  1120. }
  1121. int
  1122. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1123. struct drm_file *file)
  1124. {
  1125. struct drm_i915_gem_execbuffer2 *args = data;
  1126. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1127. int ret;
  1128. if (args->buffer_count < 1) {
  1129. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  1130. return -EINVAL;
  1131. }
  1132. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1133. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1134. if (exec2_list == NULL)
  1135. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1136. args->buffer_count);
  1137. if (exec2_list == NULL) {
  1138. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1139. args->buffer_count);
  1140. return -ENOMEM;
  1141. }
  1142. ret = copy_from_user(exec2_list,
  1143. (struct drm_i915_relocation_entry __user *)
  1144. (uintptr_t) args->buffers_ptr,
  1145. sizeof(*exec2_list) * args->buffer_count);
  1146. if (ret != 0) {
  1147. DRM_ERROR("copy %d exec entries failed %d\n",
  1148. args->buffer_count, ret);
  1149. drm_free_large(exec2_list);
  1150. return -EFAULT;
  1151. }
  1152. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1153. if (!ret) {
  1154. /* Copy the new buffer offsets back to the user's exec list. */
  1155. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1156. (uintptr_t) args->buffers_ptr,
  1157. exec2_list,
  1158. sizeof(*exec2_list) * args->buffer_count);
  1159. if (ret) {
  1160. ret = -EFAULT;
  1161. DRM_ERROR("failed to copy %d exec entries "
  1162. "back to user (%d)\n",
  1163. args->buffer_count, ret);
  1164. }
  1165. }
  1166. drm_free_large(exec2_list);
  1167. return ret;
  1168. }