i915_gem_execbuffer.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. static int
  202. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  203. struct drm_file *file_priv,
  204. struct drm_i915_gem_exec_object2 *entry,
  205. struct drm_i915_gem_relocation_entry *reloc)
  206. {
  207. struct drm_device *dev = obj->base.dev;
  208. struct drm_gem_object *target_obj;
  209. uint32_t target_offset;
  210. int ret = -EINVAL;
  211. target_obj = drm_gem_object_lookup(dev, file_priv,
  212. reloc->target_handle);
  213. if (target_obj == NULL)
  214. return -ENOENT;
  215. target_offset = to_intel_bo(target_obj)->gtt_offset;
  216. #if WATCH_RELOC
  217. DRM_INFO("%s: obj %p offset %08x target %d "
  218. "read %08x write %08x gtt %08x "
  219. "presumed %08x delta %08x\n",
  220. __func__,
  221. obj,
  222. (int) reloc->offset,
  223. (int) reloc->target_handle,
  224. (int) reloc->read_domains,
  225. (int) reloc->write_domain,
  226. (int) target_offset,
  227. (int) reloc->presumed_offset,
  228. reloc->delta);
  229. #endif
  230. /* The target buffer should have appeared before us in the
  231. * exec_object list, so it should have a GTT space bound by now.
  232. */
  233. if (target_offset == 0) {
  234. DRM_ERROR("No GTT space found for object %d\n",
  235. reloc->target_handle);
  236. goto err;
  237. }
  238. /* Validate that the target is in a valid r/w GPU domain */
  239. if (reloc->write_domain & (reloc->write_domain - 1)) {
  240. DRM_ERROR("reloc with multiple write domains: "
  241. "obj %p target %d offset %d "
  242. "read %08x write %08x",
  243. obj, reloc->target_handle,
  244. (int) reloc->offset,
  245. reloc->read_domains,
  246. reloc->write_domain);
  247. goto err;
  248. }
  249. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  250. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  251. DRM_ERROR("reloc with read/write CPU domains: "
  252. "obj %p target %d offset %d "
  253. "read %08x write %08x",
  254. obj, reloc->target_handle,
  255. (int) reloc->offset,
  256. reloc->read_domains,
  257. reloc->write_domain);
  258. goto err;
  259. }
  260. if (reloc->write_domain && target_obj->pending_write_domain &&
  261. reloc->write_domain != target_obj->pending_write_domain) {
  262. DRM_ERROR("Write domain conflict: "
  263. "obj %p target %d offset %d "
  264. "new %08x old %08x\n",
  265. obj, reloc->target_handle,
  266. (int) reloc->offset,
  267. reloc->write_domain,
  268. target_obj->pending_write_domain);
  269. goto err;
  270. }
  271. target_obj->pending_read_domains |= reloc->read_domains;
  272. target_obj->pending_write_domain |= reloc->write_domain;
  273. /* If the relocation already has the right value in it, no
  274. * more work needs to be done.
  275. */
  276. if (target_offset == reloc->presumed_offset)
  277. goto out;
  278. /* Check that the relocation address is valid... */
  279. if (reloc->offset > obj->base.size - 4) {
  280. DRM_ERROR("Relocation beyond object bounds: "
  281. "obj %p target %d offset %d size %d.\n",
  282. obj, reloc->target_handle,
  283. (int) reloc->offset,
  284. (int) obj->base.size);
  285. goto err;
  286. }
  287. if (reloc->offset & 3) {
  288. DRM_ERROR("Relocation not 4-byte aligned: "
  289. "obj %p target %d offset %d.\n",
  290. obj, reloc->target_handle,
  291. (int) reloc->offset);
  292. goto err;
  293. }
  294. /* and points to somewhere within the target object. */
  295. if (reloc->delta >= target_obj->size) {
  296. DRM_ERROR("Relocation beyond target object bounds: "
  297. "obj %p target %d delta %d size %d.\n",
  298. obj, reloc->target_handle,
  299. (int) reloc->delta,
  300. (int) target_obj->size);
  301. goto err;
  302. }
  303. reloc->delta += target_offset;
  304. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  305. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  306. char *vaddr;
  307. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  308. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  309. kunmap_atomic(vaddr);
  310. } else {
  311. struct drm_i915_private *dev_priv = dev->dev_private;
  312. uint32_t __iomem *reloc_entry;
  313. void __iomem *reloc_page;
  314. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  315. if (ret)
  316. goto err;
  317. /* Map the page containing the relocation we're going to perform. */
  318. reloc->offset += obj->gtt_offset;
  319. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  320. reloc->offset & PAGE_MASK);
  321. reloc_entry = (uint32_t __iomem *)
  322. (reloc_page + (reloc->offset & ~PAGE_MASK));
  323. iowrite32(reloc->delta, reloc_entry);
  324. io_mapping_unmap_atomic(reloc_page);
  325. }
  326. /* and update the user's relocation entry */
  327. reloc->presumed_offset = target_offset;
  328. out:
  329. ret = 0;
  330. err:
  331. drm_gem_object_unreference(target_obj);
  332. return ret;
  333. }
  334. static int
  335. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  336. struct drm_file *file_priv,
  337. struct drm_i915_gem_exec_object2 *entry)
  338. {
  339. struct drm_i915_gem_relocation_entry __user *user_relocs;
  340. int i, ret;
  341. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  342. for (i = 0; i < entry->relocation_count; i++) {
  343. struct drm_i915_gem_relocation_entry reloc;
  344. if (__copy_from_user_inatomic(&reloc,
  345. user_relocs+i,
  346. sizeof(reloc)))
  347. return -EFAULT;
  348. ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
  349. if (ret)
  350. return ret;
  351. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  352. &reloc.presumed_offset,
  353. sizeof(reloc.presumed_offset)))
  354. return -EFAULT;
  355. }
  356. return 0;
  357. }
  358. static int
  359. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  360. struct drm_file *file_priv,
  361. struct drm_i915_gem_exec_object2 *entry,
  362. struct drm_i915_gem_relocation_entry *relocs)
  363. {
  364. int i, ret;
  365. for (i = 0; i < entry->relocation_count; i++) {
  366. ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
  367. if (ret)
  368. return ret;
  369. }
  370. return 0;
  371. }
  372. static int
  373. i915_gem_execbuffer_relocate(struct drm_device *dev,
  374. struct drm_file *file,
  375. struct list_head *objects,
  376. struct drm_i915_gem_exec_object2 *exec)
  377. {
  378. struct drm_i915_gem_object *obj;
  379. int ret;
  380. list_for_each_entry(obj, objects, exec_list) {
  381. obj->base.pending_read_domains = 0;
  382. obj->base.pending_write_domain = 0;
  383. ret = i915_gem_execbuffer_relocate_object(obj, file, exec++);
  384. if (ret)
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static int
  390. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  391. struct drm_file *file,
  392. struct list_head *objects,
  393. struct drm_i915_gem_exec_object2 *exec)
  394. {
  395. struct drm_i915_gem_object *obj;
  396. struct drm_i915_gem_exec_object2 *entry;
  397. int ret, retry;
  398. /* Attempt to pin all of the buffers into the GTT.
  399. * This is done in 3 phases:
  400. *
  401. * 1a. Unbind all objects that do not match the GTT constraints for
  402. * the execbuffer (fenceable, mappable, alignment etc).
  403. * 1b. Increment pin count for already bound objects.
  404. * 2. Bind new objects.
  405. * 3. Decrement pin count.
  406. *
  407. * This avoid unnecessary unbinding of later objects in order to makr
  408. * room for the earlier objects *unless* we need to defragment.
  409. */
  410. retry = 0;
  411. do {
  412. ret = 0;
  413. /* Unbind any ill-fitting objects or pin. */
  414. entry = exec;
  415. list_for_each_entry(obj, objects, exec_list) {
  416. bool need_fence, need_mappable;
  417. if (!obj->gtt_space) {
  418. entry++;
  419. continue;
  420. }
  421. need_fence =
  422. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  423. obj->tiling_mode != I915_TILING_NONE;
  424. need_mappable =
  425. entry->relocation_count ? true : need_fence;
  426. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  427. (need_mappable && !obj->map_and_fenceable))
  428. ret = i915_gem_object_unbind(obj);
  429. else
  430. ret = i915_gem_object_pin(obj,
  431. entry->alignment,
  432. need_mappable);
  433. if (ret)
  434. goto err;
  435. entry++;
  436. }
  437. /* Bind fresh objects */
  438. entry = exec;
  439. list_for_each_entry(obj, objects, exec_list) {
  440. bool need_fence;
  441. need_fence =
  442. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  443. obj->tiling_mode != I915_TILING_NONE;
  444. if (!obj->gtt_space) {
  445. bool need_mappable =
  446. entry->relocation_count ? true : need_fence;
  447. ret = i915_gem_object_pin(obj,
  448. entry->alignment,
  449. need_mappable);
  450. if (ret)
  451. break;
  452. }
  453. if (need_fence) {
  454. ret = i915_gem_object_get_fence(obj, ring, 1);
  455. if (ret)
  456. break;
  457. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  458. obj->tiling_mode == I915_TILING_NONE) {
  459. /* XXX pipelined! */
  460. ret = i915_gem_object_put_fence(obj);
  461. if (ret)
  462. break;
  463. }
  464. obj->pending_fenced_gpu_access = need_fence;
  465. entry->offset = obj->gtt_offset;
  466. entry++;
  467. }
  468. /* Decrement pin count for bound objects */
  469. list_for_each_entry(obj, objects, exec_list) {
  470. if (obj->gtt_space)
  471. i915_gem_object_unpin(obj);
  472. }
  473. if (ret != -ENOSPC || retry > 1)
  474. return ret;
  475. /* First attempt, just clear anything that is purgeable.
  476. * Second attempt, clear the entire GTT.
  477. */
  478. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  479. if (ret)
  480. return ret;
  481. retry++;
  482. } while (1);
  483. err:
  484. obj = list_entry(obj->exec_list.prev,
  485. struct drm_i915_gem_object,
  486. exec_list);
  487. while (objects != &obj->exec_list) {
  488. if (obj->gtt_space)
  489. i915_gem_object_unpin(obj);
  490. obj = list_entry(obj->exec_list.prev,
  491. struct drm_i915_gem_object,
  492. exec_list);
  493. }
  494. return ret;
  495. }
  496. static int
  497. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  498. struct drm_file *file,
  499. struct intel_ring_buffer *ring,
  500. struct list_head *objects,
  501. struct drm_i915_gem_exec_object2 *exec,
  502. int count)
  503. {
  504. struct drm_i915_gem_relocation_entry *reloc;
  505. struct drm_i915_gem_object *obj;
  506. int i, total, ret;
  507. mutex_unlock(&dev->struct_mutex);
  508. total = 0;
  509. for (i = 0; i < count; i++)
  510. total += exec[i].relocation_count;
  511. reloc = drm_malloc_ab(total, sizeof(*reloc));
  512. if (reloc == NULL) {
  513. mutex_lock(&dev->struct_mutex);
  514. return -ENOMEM;
  515. }
  516. total = 0;
  517. for (i = 0; i < count; i++) {
  518. struct drm_i915_gem_relocation_entry __user *user_relocs;
  519. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  520. if (copy_from_user(reloc+total, user_relocs,
  521. exec[i].relocation_count * sizeof(*reloc))) {
  522. ret = -EFAULT;
  523. mutex_lock(&dev->struct_mutex);
  524. goto err;
  525. }
  526. total += exec[i].relocation_count;
  527. }
  528. ret = i915_mutex_lock_interruptible(dev);
  529. if (ret) {
  530. mutex_lock(&dev->struct_mutex);
  531. goto err;
  532. }
  533. ret = i915_gem_execbuffer_reserve(ring, file, objects, exec);
  534. if (ret)
  535. goto err;
  536. total = 0;
  537. list_for_each_entry(obj, objects, exec_list) {
  538. obj->base.pending_read_domains = 0;
  539. obj->base.pending_write_domain = 0;
  540. ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
  541. exec,
  542. reloc + total);
  543. if (ret)
  544. goto err;
  545. total += exec->relocation_count;
  546. exec++;
  547. }
  548. /* Leave the user relocations as are, this is the painfully slow path,
  549. * and we want to avoid the complication of dropping the lock whilst
  550. * having buffers reserved in the aperture and so causing spurious
  551. * ENOSPC for random operations.
  552. */
  553. err:
  554. drm_free_large(reloc);
  555. return ret;
  556. }
  557. static void
  558. i915_gem_execbuffer_flush(struct drm_device *dev,
  559. uint32_t invalidate_domains,
  560. uint32_t flush_domains,
  561. uint32_t flush_rings)
  562. {
  563. drm_i915_private_t *dev_priv = dev->dev_private;
  564. if (flush_domains & I915_GEM_DOMAIN_CPU)
  565. intel_gtt_chipset_flush();
  566. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  567. if (flush_rings & RING_RENDER)
  568. i915_gem_flush_ring(dev, &dev_priv->render_ring,
  569. invalidate_domains, flush_domains);
  570. if (flush_rings & RING_BSD)
  571. i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
  572. invalidate_domains, flush_domains);
  573. if (flush_rings & RING_BLT)
  574. i915_gem_flush_ring(dev, &dev_priv->blt_ring,
  575. invalidate_domains, flush_domains);
  576. }
  577. }
  578. static int
  579. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  580. struct list_head *objects)
  581. {
  582. struct drm_i915_gem_object *obj;
  583. struct change_domains cd;
  584. int ret;
  585. cd.invalidate_domains = 0;
  586. cd.flush_domains = 0;
  587. cd.flush_rings = 0;
  588. list_for_each_entry(obj, objects, exec_list)
  589. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  590. if (cd.invalidate_domains | cd.flush_domains) {
  591. #if WATCH_EXEC
  592. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  593. __func__,
  594. cd.invalidate_domains,
  595. cd.flush_domains);
  596. #endif
  597. i915_gem_execbuffer_flush(ring->dev,
  598. cd.invalidate_domains,
  599. cd.flush_domains,
  600. cd.flush_rings);
  601. }
  602. list_for_each_entry(obj, objects, exec_list) {
  603. /* XXX replace with semaphores */
  604. if (obj->ring && ring != obj->ring) {
  605. ret = i915_gem_object_wait_rendering(obj, true);
  606. if (ret)
  607. return ret;
  608. }
  609. }
  610. return 0;
  611. }
  612. static bool
  613. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  614. {
  615. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  616. }
  617. static int
  618. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  619. int count)
  620. {
  621. int i;
  622. for (i = 0; i < count; i++) {
  623. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  624. int length; /* limited by fault_in_pages_readable() */
  625. /* First check for malicious input causing overflow */
  626. if (exec[i].relocation_count >
  627. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  628. return -EINVAL;
  629. length = exec[i].relocation_count *
  630. sizeof(struct drm_i915_gem_relocation_entry);
  631. if (!access_ok(VERIFY_READ, ptr, length))
  632. return -EFAULT;
  633. /* we may also need to update the presumed offsets */
  634. if (!access_ok(VERIFY_WRITE, ptr, length))
  635. return -EFAULT;
  636. if (fault_in_pages_readable(ptr, length))
  637. return -EFAULT;
  638. }
  639. return 0;
  640. }
  641. static int
  642. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
  643. struct list_head *objects)
  644. {
  645. struct drm_i915_gem_object *obj;
  646. int flips;
  647. /* Check for any pending flips. As we only maintain a flip queue depth
  648. * of 1, we can simply insert a WAIT for the next display flip prior
  649. * to executing the batch and avoid stalling the CPU.
  650. */
  651. flips = 0;
  652. list_for_each_entry(obj, objects, exec_list) {
  653. if (obj->base.write_domain)
  654. flips |= atomic_read(&obj->pending_flip);
  655. }
  656. if (flips) {
  657. int plane, flip_mask, ret;
  658. for (plane = 0; flips >> plane; plane++) {
  659. if (((flips >> plane) & 1) == 0)
  660. continue;
  661. if (plane)
  662. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  663. else
  664. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  665. ret = intel_ring_begin(ring, 2);
  666. if (ret)
  667. return ret;
  668. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  669. intel_ring_emit(ring, MI_NOOP);
  670. intel_ring_advance(ring);
  671. }
  672. }
  673. return 0;
  674. }
  675. static void
  676. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  677. struct intel_ring_buffer *ring)
  678. {
  679. struct drm_i915_gem_object *obj;
  680. list_for_each_entry(obj, objects, exec_list) {
  681. obj->base.read_domains = obj->base.pending_read_domains;
  682. obj->base.write_domain = obj->base.pending_write_domain;
  683. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  684. i915_gem_object_move_to_active(obj, ring);
  685. if (obj->base.write_domain) {
  686. obj->dirty = 1;
  687. obj->pending_gpu_write = true;
  688. list_move_tail(&obj->gpu_write_list,
  689. &ring->gpu_write_list);
  690. intel_mark_busy(ring->dev, obj);
  691. }
  692. trace_i915_gem_object_change_domain(obj,
  693. obj->base.read_domains,
  694. obj->base.write_domain);
  695. }
  696. }
  697. static void
  698. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  699. struct drm_file *file,
  700. struct intel_ring_buffer *ring)
  701. {
  702. struct drm_i915_gem_request *request;
  703. u32 flush_domains;
  704. /*
  705. * Ensure that the commands in the batch buffer are
  706. * finished before the interrupt fires.
  707. *
  708. * The sampler always gets flushed on i965 (sigh).
  709. */
  710. flush_domains = 0;
  711. if (INTEL_INFO(dev)->gen >= 4)
  712. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  713. ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
  714. /* Add a breadcrumb for the completion of the batch buffer */
  715. request = kzalloc(sizeof(*request), GFP_KERNEL);
  716. if (request == NULL || i915_add_request(dev, file, request, ring)) {
  717. i915_gem_next_request_seqno(dev, ring);
  718. kfree(request);
  719. }
  720. }
  721. static int
  722. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  723. struct drm_file *file,
  724. struct drm_i915_gem_execbuffer2 *args,
  725. struct drm_i915_gem_exec_object2 *exec)
  726. {
  727. drm_i915_private_t *dev_priv = dev->dev_private;
  728. struct list_head objects;
  729. struct drm_i915_gem_object *batch_obj;
  730. struct drm_clip_rect *cliprects = NULL;
  731. struct intel_ring_buffer *ring;
  732. u32 exec_start, exec_len;
  733. int ret, i;
  734. if (!i915_gem_check_execbuffer(args)) {
  735. DRM_ERROR("execbuf with invalid offset/length\n");
  736. return -EINVAL;
  737. }
  738. ret = validate_exec_list(exec, args->buffer_count);
  739. if (ret)
  740. return ret;
  741. #if WATCH_EXEC
  742. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  743. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  744. #endif
  745. switch (args->flags & I915_EXEC_RING_MASK) {
  746. case I915_EXEC_DEFAULT:
  747. case I915_EXEC_RENDER:
  748. ring = &dev_priv->render_ring;
  749. break;
  750. case I915_EXEC_BSD:
  751. if (!HAS_BSD(dev)) {
  752. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  753. return -EINVAL;
  754. }
  755. ring = &dev_priv->bsd_ring;
  756. break;
  757. case I915_EXEC_BLT:
  758. if (!HAS_BLT(dev)) {
  759. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  760. return -EINVAL;
  761. }
  762. ring = &dev_priv->blt_ring;
  763. break;
  764. default:
  765. DRM_ERROR("execbuf with unknown ring: %d\n",
  766. (int)(args->flags & I915_EXEC_RING_MASK));
  767. return -EINVAL;
  768. }
  769. if (args->buffer_count < 1) {
  770. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  771. return -EINVAL;
  772. }
  773. if (args->num_cliprects != 0) {
  774. if (ring != &dev_priv->render_ring) {
  775. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  776. return -EINVAL;
  777. }
  778. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  779. GFP_KERNEL);
  780. if (cliprects == NULL) {
  781. ret = -ENOMEM;
  782. goto pre_mutex_err;
  783. }
  784. if (copy_from_user(cliprects,
  785. (struct drm_clip_rect __user *)(uintptr_t)
  786. args->cliprects_ptr,
  787. sizeof(*cliprects)*args->num_cliprects)) {
  788. ret = -EFAULT;
  789. goto pre_mutex_err;
  790. }
  791. }
  792. ret = i915_mutex_lock_interruptible(dev);
  793. if (ret)
  794. goto pre_mutex_err;
  795. if (dev_priv->mm.suspended) {
  796. mutex_unlock(&dev->struct_mutex);
  797. ret = -EBUSY;
  798. goto pre_mutex_err;
  799. }
  800. /* Look up object handles */
  801. INIT_LIST_HEAD(&objects);
  802. for (i = 0; i < args->buffer_count; i++) {
  803. struct drm_i915_gem_object *obj;
  804. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  805. exec[i].handle));
  806. if (obj == NULL) {
  807. DRM_ERROR("Invalid object handle %d at index %d\n",
  808. exec[i].handle, i);
  809. /* prevent error path from reading uninitialized data */
  810. ret = -ENOENT;
  811. goto err;
  812. }
  813. if (!list_empty(&obj->exec_list)) {
  814. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  815. obj, exec[i].handle, i);
  816. ret = -EINVAL;
  817. goto err;
  818. }
  819. list_add_tail(&obj->exec_list, &objects);
  820. }
  821. /* Move the objects en-masse into the GTT, evicting if necessary. */
  822. ret = i915_gem_execbuffer_reserve(ring, file, &objects, exec);
  823. if (ret)
  824. goto err;
  825. /* The objects are in their final locations, apply the relocations. */
  826. ret = i915_gem_execbuffer_relocate(dev, file, &objects, exec);
  827. if (ret) {
  828. if (ret == -EFAULT) {
  829. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  830. &objects, exec,
  831. args->buffer_count);
  832. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  833. }
  834. if (ret)
  835. goto err;
  836. }
  837. /* Set the pending read domains for the batch buffer to COMMAND */
  838. batch_obj = list_entry(objects.prev,
  839. struct drm_i915_gem_object,
  840. exec_list);
  841. if (batch_obj->base.pending_write_domain) {
  842. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  843. ret = -EINVAL;
  844. goto err;
  845. }
  846. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  847. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  848. if (ret)
  849. goto err;
  850. ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
  851. if (ret)
  852. goto err;
  853. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  854. exec_len = args->batch_len;
  855. if (cliprects) {
  856. for (i = 0; i < args->num_cliprects; i++) {
  857. ret = i915_emit_box(dev, &cliprects[i],
  858. args->DR1, args->DR4);
  859. if (ret)
  860. goto err;
  861. ret = ring->dispatch_execbuffer(ring,
  862. exec_start, exec_len);
  863. if (ret)
  864. goto err;
  865. }
  866. } else {
  867. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  868. if (ret)
  869. goto err;
  870. }
  871. i915_gem_execbuffer_move_to_active(&objects, ring);
  872. i915_gem_execbuffer_retire_commands(dev, file, ring);
  873. err:
  874. while (!list_empty(&objects)) {
  875. struct drm_i915_gem_object *obj;
  876. obj = list_first_entry(&objects,
  877. struct drm_i915_gem_object,
  878. exec_list);
  879. list_del_init(&obj->exec_list);
  880. drm_gem_object_unreference(&obj->base);
  881. }
  882. mutex_unlock(&dev->struct_mutex);
  883. pre_mutex_err:
  884. kfree(cliprects);
  885. return ret;
  886. }
  887. /*
  888. * Legacy execbuffer just creates an exec2 list from the original exec object
  889. * list array and passes it to the real function.
  890. */
  891. int
  892. i915_gem_execbuffer(struct drm_device *dev, void *data,
  893. struct drm_file *file)
  894. {
  895. struct drm_i915_gem_execbuffer *args = data;
  896. struct drm_i915_gem_execbuffer2 exec2;
  897. struct drm_i915_gem_exec_object *exec_list = NULL;
  898. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  899. int ret, i;
  900. #if WATCH_EXEC
  901. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  902. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  903. #endif
  904. if (args->buffer_count < 1) {
  905. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  906. return -EINVAL;
  907. }
  908. /* Copy in the exec list from userland */
  909. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  910. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  911. if (exec_list == NULL || exec2_list == NULL) {
  912. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  913. args->buffer_count);
  914. drm_free_large(exec_list);
  915. drm_free_large(exec2_list);
  916. return -ENOMEM;
  917. }
  918. ret = copy_from_user(exec_list,
  919. (struct drm_i915_relocation_entry __user *)
  920. (uintptr_t) args->buffers_ptr,
  921. sizeof(*exec_list) * args->buffer_count);
  922. if (ret != 0) {
  923. DRM_ERROR("copy %d exec entries failed %d\n",
  924. args->buffer_count, ret);
  925. drm_free_large(exec_list);
  926. drm_free_large(exec2_list);
  927. return -EFAULT;
  928. }
  929. for (i = 0; i < args->buffer_count; i++) {
  930. exec2_list[i].handle = exec_list[i].handle;
  931. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  932. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  933. exec2_list[i].alignment = exec_list[i].alignment;
  934. exec2_list[i].offset = exec_list[i].offset;
  935. if (INTEL_INFO(dev)->gen < 4)
  936. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  937. else
  938. exec2_list[i].flags = 0;
  939. }
  940. exec2.buffers_ptr = args->buffers_ptr;
  941. exec2.buffer_count = args->buffer_count;
  942. exec2.batch_start_offset = args->batch_start_offset;
  943. exec2.batch_len = args->batch_len;
  944. exec2.DR1 = args->DR1;
  945. exec2.DR4 = args->DR4;
  946. exec2.num_cliprects = args->num_cliprects;
  947. exec2.cliprects_ptr = args->cliprects_ptr;
  948. exec2.flags = I915_EXEC_RENDER;
  949. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  950. if (!ret) {
  951. /* Copy the new buffer offsets back to the user's exec list. */
  952. for (i = 0; i < args->buffer_count; i++)
  953. exec_list[i].offset = exec2_list[i].offset;
  954. /* ... and back out to userspace */
  955. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  956. (uintptr_t) args->buffers_ptr,
  957. exec_list,
  958. sizeof(*exec_list) * args->buffer_count);
  959. if (ret) {
  960. ret = -EFAULT;
  961. DRM_ERROR("failed to copy %d exec entries "
  962. "back to user (%d)\n",
  963. args->buffer_count, ret);
  964. }
  965. }
  966. drm_free_large(exec_list);
  967. drm_free_large(exec2_list);
  968. return ret;
  969. }
  970. int
  971. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  972. struct drm_file *file)
  973. {
  974. struct drm_i915_gem_execbuffer2 *args = data;
  975. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  976. int ret;
  977. #if WATCH_EXEC
  978. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  979. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  980. #endif
  981. if (args->buffer_count < 1) {
  982. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  983. return -EINVAL;
  984. }
  985. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  986. if (exec2_list == NULL) {
  987. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  988. args->buffer_count);
  989. return -ENOMEM;
  990. }
  991. ret = copy_from_user(exec2_list,
  992. (struct drm_i915_relocation_entry __user *)
  993. (uintptr_t) args->buffers_ptr,
  994. sizeof(*exec2_list) * args->buffer_count);
  995. if (ret != 0) {
  996. DRM_ERROR("copy %d exec entries failed %d\n",
  997. args->buffer_count, ret);
  998. drm_free_large(exec2_list);
  999. return -EFAULT;
  1000. }
  1001. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1002. if (!ret) {
  1003. /* Copy the new buffer offsets back to the user's exec list. */
  1004. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1005. (uintptr_t) args->buffers_ptr,
  1006. exec2_list,
  1007. sizeof(*exec2_list) * args->buffer_count);
  1008. if (ret) {
  1009. ret = -EFAULT;
  1010. DRM_ERROR("failed to copy %d exec entries "
  1011. "back to user (%d)\n",
  1012. args->buffer_count, ret);
  1013. }
  1014. }
  1015. drm_free_large(exec2_list);
  1016. return ret;
  1017. }