i915_gem_execbuffer.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 drm_device *dev,
  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_reg(obj, true);
  455. if (ret)
  456. break;
  457. }
  458. obj->pending_fenced_gpu_access = need_fence;
  459. entry->offset = obj->gtt_offset;
  460. entry++;
  461. }
  462. /* Decrement pin count for bound objects */
  463. list_for_each_entry(obj, objects, exec_list) {
  464. if (obj->gtt_space)
  465. i915_gem_object_unpin(obj);
  466. }
  467. if (ret != -ENOSPC || retry > 1)
  468. return ret;
  469. /* First attempt, just clear anything that is purgeable.
  470. * Second attempt, clear the entire GTT.
  471. */
  472. ret = i915_gem_evict_everything(dev, retry == 0);
  473. if (ret)
  474. return ret;
  475. retry++;
  476. } while (1);
  477. err:
  478. obj = list_entry(obj->exec_list.prev,
  479. struct drm_i915_gem_object,
  480. exec_list);
  481. while (objects != &obj->exec_list) {
  482. if (obj->gtt_space)
  483. i915_gem_object_unpin(obj);
  484. obj = list_entry(obj->exec_list.prev,
  485. struct drm_i915_gem_object,
  486. exec_list);
  487. }
  488. return ret;
  489. }
  490. static int
  491. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  492. struct drm_file *file,
  493. struct list_head *objects,
  494. struct drm_i915_gem_exec_object2 *exec,
  495. int count)
  496. {
  497. struct drm_i915_gem_relocation_entry *reloc;
  498. struct drm_i915_gem_object *obj;
  499. int i, total, ret;
  500. mutex_unlock(&dev->struct_mutex);
  501. total = 0;
  502. for (i = 0; i < count; i++)
  503. total += exec[i].relocation_count;
  504. reloc = drm_malloc_ab(total, sizeof(*reloc));
  505. if (reloc == NULL) {
  506. mutex_lock(&dev->struct_mutex);
  507. return -ENOMEM;
  508. }
  509. total = 0;
  510. for (i = 0; i < count; i++) {
  511. struct drm_i915_gem_relocation_entry __user *user_relocs;
  512. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  513. if (copy_from_user(reloc+total, user_relocs,
  514. exec[i].relocation_count * sizeof(*reloc))) {
  515. ret = -EFAULT;
  516. mutex_lock(&dev->struct_mutex);
  517. goto err;
  518. }
  519. total += exec[i].relocation_count;
  520. }
  521. ret = i915_mutex_lock_interruptible(dev);
  522. if (ret) {
  523. mutex_lock(&dev->struct_mutex);
  524. goto err;
  525. }
  526. ret = i915_gem_execbuffer_reserve(dev, file, objects, exec);
  527. if (ret)
  528. goto err;
  529. total = 0;
  530. list_for_each_entry(obj, objects, exec_list) {
  531. obj->base.pending_read_domains = 0;
  532. obj->base.pending_write_domain = 0;
  533. ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
  534. exec,
  535. reloc + total);
  536. if (ret)
  537. goto err;
  538. total += exec->relocation_count;
  539. exec++;
  540. }
  541. /* Leave the user relocations as are, this is the painfully slow path,
  542. * and we want to avoid the complication of dropping the lock whilst
  543. * having buffers reserved in the aperture and so causing spurious
  544. * ENOSPC for random operations.
  545. */
  546. err:
  547. drm_free_large(reloc);
  548. return ret;
  549. }
  550. static void
  551. i915_gem_execbuffer_flush(struct drm_device *dev,
  552. uint32_t invalidate_domains,
  553. uint32_t flush_domains,
  554. uint32_t flush_rings)
  555. {
  556. drm_i915_private_t *dev_priv = dev->dev_private;
  557. if (flush_domains & I915_GEM_DOMAIN_CPU)
  558. intel_gtt_chipset_flush();
  559. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  560. if (flush_rings & RING_RENDER)
  561. i915_gem_flush_ring(dev, &dev_priv->render_ring,
  562. invalidate_domains, flush_domains);
  563. if (flush_rings & RING_BSD)
  564. i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
  565. invalidate_domains, flush_domains);
  566. if (flush_rings & RING_BLT)
  567. i915_gem_flush_ring(dev, &dev_priv->blt_ring,
  568. invalidate_domains, flush_domains);
  569. }
  570. }
  571. static int
  572. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  573. struct list_head *objects)
  574. {
  575. struct drm_i915_gem_object *obj;
  576. struct change_domains cd;
  577. int ret;
  578. cd.invalidate_domains = 0;
  579. cd.flush_domains = 0;
  580. cd.flush_rings = 0;
  581. list_for_each_entry(obj, objects, exec_list)
  582. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  583. if (cd.invalidate_domains | cd.flush_domains) {
  584. #if WATCH_EXEC
  585. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  586. __func__,
  587. cd.invalidate_domains,
  588. cd.flush_domains);
  589. #endif
  590. i915_gem_execbuffer_flush(ring->dev,
  591. cd.invalidate_domains,
  592. cd.flush_domains,
  593. cd.flush_rings);
  594. }
  595. list_for_each_entry(obj, objects, exec_list) {
  596. /* XXX replace with semaphores */
  597. if (obj->ring && ring != obj->ring) {
  598. ret = i915_gem_object_wait_rendering(obj, true);
  599. if (ret)
  600. return ret;
  601. }
  602. }
  603. return 0;
  604. }
  605. static bool
  606. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  607. {
  608. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  609. }
  610. static int
  611. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  612. int count)
  613. {
  614. int i;
  615. for (i = 0; i < count; i++) {
  616. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  617. int length; /* limited by fault_in_pages_readable() */
  618. /* First check for malicious input causing overflow */
  619. if (exec[i].relocation_count >
  620. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  621. return -EINVAL;
  622. length = exec[i].relocation_count *
  623. sizeof(struct drm_i915_gem_relocation_entry);
  624. if (!access_ok(VERIFY_READ, ptr, length))
  625. return -EFAULT;
  626. /* we may also need to update the presumed offsets */
  627. if (!access_ok(VERIFY_WRITE, ptr, length))
  628. return -EFAULT;
  629. if (fault_in_pages_readable(ptr, length))
  630. return -EFAULT;
  631. }
  632. return 0;
  633. }
  634. static int
  635. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
  636. struct list_head *objects)
  637. {
  638. struct drm_i915_gem_object *obj;
  639. int flips;
  640. /* Check for any pending flips. As we only maintain a flip queue depth
  641. * of 1, we can simply insert a WAIT for the next display flip prior
  642. * to executing the batch and avoid stalling the CPU.
  643. */
  644. flips = 0;
  645. list_for_each_entry(obj, objects, exec_list) {
  646. if (obj->base.write_domain)
  647. flips |= atomic_read(&obj->pending_flip);
  648. }
  649. if (flips) {
  650. int plane, flip_mask, ret;
  651. for (plane = 0; flips >> plane; plane++) {
  652. if (((flips >> plane) & 1) == 0)
  653. continue;
  654. if (plane)
  655. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  656. else
  657. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  658. ret = intel_ring_begin(ring, 2);
  659. if (ret)
  660. return ret;
  661. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  662. intel_ring_emit(ring, MI_NOOP);
  663. intel_ring_advance(ring);
  664. }
  665. }
  666. return 0;
  667. }
  668. static void
  669. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  670. struct intel_ring_buffer *ring)
  671. {
  672. struct drm_i915_gem_object *obj;
  673. list_for_each_entry(obj, objects, exec_list) {
  674. obj->base.read_domains = obj->base.pending_read_domains;
  675. obj->base.write_domain = obj->base.pending_write_domain;
  676. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  677. i915_gem_object_move_to_active(obj, ring);
  678. if (obj->base.write_domain) {
  679. obj->dirty = 1;
  680. obj->pending_gpu_write = true;
  681. list_move_tail(&obj->gpu_write_list,
  682. &ring->gpu_write_list);
  683. intel_mark_busy(ring->dev, obj);
  684. }
  685. trace_i915_gem_object_change_domain(obj,
  686. obj->base.read_domains,
  687. obj->base.write_domain);
  688. }
  689. }
  690. static void
  691. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  692. struct drm_file *file,
  693. struct intel_ring_buffer *ring)
  694. {
  695. struct drm_i915_gem_request *request;
  696. u32 flush_domains;
  697. /*
  698. * Ensure that the commands in the batch buffer are
  699. * finished before the interrupt fires.
  700. *
  701. * The sampler always gets flushed on i965 (sigh).
  702. */
  703. flush_domains = 0;
  704. if (INTEL_INFO(dev)->gen >= 4)
  705. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  706. ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
  707. /* Add a breadcrumb for the completion of the batch buffer */
  708. request = kzalloc(sizeof(*request), GFP_KERNEL);
  709. if (request == NULL || i915_add_request(dev, file, request, ring)) {
  710. i915_gem_next_request_seqno(dev, ring);
  711. kfree(request);
  712. }
  713. }
  714. static int
  715. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  716. struct drm_file *file,
  717. struct drm_i915_gem_execbuffer2 *args,
  718. struct drm_i915_gem_exec_object2 *exec)
  719. {
  720. drm_i915_private_t *dev_priv = dev->dev_private;
  721. struct list_head objects;
  722. struct drm_i915_gem_object *batch_obj;
  723. struct drm_clip_rect *cliprects = NULL;
  724. struct intel_ring_buffer *ring;
  725. u32 exec_start, exec_len;
  726. int ret, i;
  727. if (!i915_gem_check_execbuffer(args)) {
  728. DRM_ERROR("execbuf with invalid offset/length\n");
  729. return -EINVAL;
  730. }
  731. ret = validate_exec_list(exec, args->buffer_count);
  732. if (ret)
  733. return ret;
  734. #if WATCH_EXEC
  735. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  736. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  737. #endif
  738. switch (args->flags & I915_EXEC_RING_MASK) {
  739. case I915_EXEC_DEFAULT:
  740. case I915_EXEC_RENDER:
  741. ring = &dev_priv->render_ring;
  742. break;
  743. case I915_EXEC_BSD:
  744. if (!HAS_BSD(dev)) {
  745. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  746. return -EINVAL;
  747. }
  748. ring = &dev_priv->bsd_ring;
  749. break;
  750. case I915_EXEC_BLT:
  751. if (!HAS_BLT(dev)) {
  752. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  753. return -EINVAL;
  754. }
  755. ring = &dev_priv->blt_ring;
  756. break;
  757. default:
  758. DRM_ERROR("execbuf with unknown ring: %d\n",
  759. (int)(args->flags & I915_EXEC_RING_MASK));
  760. return -EINVAL;
  761. }
  762. if (args->buffer_count < 1) {
  763. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  764. return -EINVAL;
  765. }
  766. if (args->num_cliprects != 0) {
  767. if (ring != &dev_priv->render_ring) {
  768. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  769. return -EINVAL;
  770. }
  771. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  772. GFP_KERNEL);
  773. if (cliprects == NULL) {
  774. ret = -ENOMEM;
  775. goto pre_mutex_err;
  776. }
  777. if (copy_from_user(cliprects,
  778. (struct drm_clip_rect __user *)(uintptr_t)
  779. args->cliprects_ptr,
  780. sizeof(*cliprects)*args->num_cliprects)) {
  781. ret = -EFAULT;
  782. goto pre_mutex_err;
  783. }
  784. }
  785. ret = i915_mutex_lock_interruptible(dev);
  786. if (ret)
  787. goto pre_mutex_err;
  788. if (dev_priv->mm.suspended) {
  789. mutex_unlock(&dev->struct_mutex);
  790. ret = -EBUSY;
  791. goto pre_mutex_err;
  792. }
  793. /* Look up object handles */
  794. INIT_LIST_HEAD(&objects);
  795. for (i = 0; i < args->buffer_count; i++) {
  796. struct drm_i915_gem_object *obj;
  797. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  798. exec[i].handle));
  799. if (obj == NULL) {
  800. DRM_ERROR("Invalid object handle %d at index %d\n",
  801. exec[i].handle, i);
  802. /* prevent error path from reading uninitialized data */
  803. ret = -ENOENT;
  804. goto err;
  805. }
  806. if (!list_empty(&obj->exec_list)) {
  807. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  808. obj, exec[i].handle, i);
  809. ret = -EINVAL;
  810. goto err;
  811. }
  812. list_add_tail(&obj->exec_list, &objects);
  813. }
  814. /* Move the objects en-masse into the GTT, evicting if necessary. */
  815. ret = i915_gem_execbuffer_reserve(dev, file, &objects, exec);
  816. if (ret)
  817. goto err;
  818. /* The objects are in their final locations, apply the relocations. */
  819. ret = i915_gem_execbuffer_relocate(dev, file, &objects, exec);
  820. if (ret) {
  821. if (ret == -EFAULT) {
  822. ret = i915_gem_execbuffer_relocate_slow(dev, file,
  823. &objects, exec,
  824. args->buffer_count);
  825. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  826. }
  827. if (ret)
  828. goto err;
  829. }
  830. /* Set the pending read domains for the batch buffer to COMMAND */
  831. batch_obj = list_entry(objects.prev,
  832. struct drm_i915_gem_object,
  833. exec_list);
  834. if (batch_obj->base.pending_write_domain) {
  835. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  836. ret = -EINVAL;
  837. goto err;
  838. }
  839. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  840. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  841. if (ret)
  842. goto err;
  843. ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
  844. if (ret)
  845. goto err;
  846. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  847. exec_len = args->batch_len;
  848. if (cliprects) {
  849. for (i = 0; i < args->num_cliprects; i++) {
  850. ret = i915_emit_box(dev, &cliprects[i],
  851. args->DR1, args->DR4);
  852. if (ret)
  853. goto err;
  854. ret = ring->dispatch_execbuffer(ring,
  855. exec_start, exec_len);
  856. if (ret)
  857. goto err;
  858. }
  859. } else {
  860. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  861. if (ret)
  862. goto err;
  863. }
  864. i915_gem_execbuffer_move_to_active(&objects, ring);
  865. i915_gem_execbuffer_retire_commands(dev, file, ring);
  866. err:
  867. while (!list_empty(&objects)) {
  868. struct drm_i915_gem_object *obj;
  869. obj = list_first_entry(&objects,
  870. struct drm_i915_gem_object,
  871. exec_list);
  872. list_del_init(&obj->exec_list);
  873. drm_gem_object_unreference(&obj->base);
  874. }
  875. mutex_unlock(&dev->struct_mutex);
  876. pre_mutex_err:
  877. kfree(cliprects);
  878. return ret;
  879. }
  880. /*
  881. * Legacy execbuffer just creates an exec2 list from the original exec object
  882. * list array and passes it to the real function.
  883. */
  884. int
  885. i915_gem_execbuffer(struct drm_device *dev, void *data,
  886. struct drm_file *file)
  887. {
  888. struct drm_i915_gem_execbuffer *args = data;
  889. struct drm_i915_gem_execbuffer2 exec2;
  890. struct drm_i915_gem_exec_object *exec_list = NULL;
  891. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  892. int ret, i;
  893. #if WATCH_EXEC
  894. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  895. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  896. #endif
  897. if (args->buffer_count < 1) {
  898. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  899. return -EINVAL;
  900. }
  901. /* Copy in the exec list from userland */
  902. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  903. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  904. if (exec_list == NULL || exec2_list == NULL) {
  905. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  906. args->buffer_count);
  907. drm_free_large(exec_list);
  908. drm_free_large(exec2_list);
  909. return -ENOMEM;
  910. }
  911. ret = copy_from_user(exec_list,
  912. (struct drm_i915_relocation_entry __user *)
  913. (uintptr_t) args->buffers_ptr,
  914. sizeof(*exec_list) * args->buffer_count);
  915. if (ret != 0) {
  916. DRM_ERROR("copy %d exec entries failed %d\n",
  917. args->buffer_count, ret);
  918. drm_free_large(exec_list);
  919. drm_free_large(exec2_list);
  920. return -EFAULT;
  921. }
  922. for (i = 0; i < args->buffer_count; i++) {
  923. exec2_list[i].handle = exec_list[i].handle;
  924. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  925. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  926. exec2_list[i].alignment = exec_list[i].alignment;
  927. exec2_list[i].offset = exec_list[i].offset;
  928. if (INTEL_INFO(dev)->gen < 4)
  929. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  930. else
  931. exec2_list[i].flags = 0;
  932. }
  933. exec2.buffers_ptr = args->buffers_ptr;
  934. exec2.buffer_count = args->buffer_count;
  935. exec2.batch_start_offset = args->batch_start_offset;
  936. exec2.batch_len = args->batch_len;
  937. exec2.DR1 = args->DR1;
  938. exec2.DR4 = args->DR4;
  939. exec2.num_cliprects = args->num_cliprects;
  940. exec2.cliprects_ptr = args->cliprects_ptr;
  941. exec2.flags = I915_EXEC_RENDER;
  942. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  943. if (!ret) {
  944. /* Copy the new buffer offsets back to the user's exec list. */
  945. for (i = 0; i < args->buffer_count; i++)
  946. exec_list[i].offset = exec2_list[i].offset;
  947. /* ... and back out to userspace */
  948. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  949. (uintptr_t) args->buffers_ptr,
  950. exec_list,
  951. sizeof(*exec_list) * args->buffer_count);
  952. if (ret) {
  953. ret = -EFAULT;
  954. DRM_ERROR("failed to copy %d exec entries "
  955. "back to user (%d)\n",
  956. args->buffer_count, ret);
  957. }
  958. }
  959. drm_free_large(exec_list);
  960. drm_free_large(exec2_list);
  961. return ret;
  962. }
  963. int
  964. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  965. struct drm_file *file)
  966. {
  967. struct drm_i915_gem_execbuffer2 *args = data;
  968. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  969. int ret;
  970. #if WATCH_EXEC
  971. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  972. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  973. #endif
  974. if (args->buffer_count < 1) {
  975. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  976. return -EINVAL;
  977. }
  978. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  979. if (exec2_list == NULL) {
  980. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  981. args->buffer_count);
  982. return -ENOMEM;
  983. }
  984. ret = copy_from_user(exec2_list,
  985. (struct drm_i915_relocation_entry __user *)
  986. (uintptr_t) args->buffers_ptr,
  987. sizeof(*exec2_list) * args->buffer_count);
  988. if (ret != 0) {
  989. DRM_ERROR("copy %d exec entries failed %d\n",
  990. args->buffer_count, ret);
  991. drm_free_large(exec2_list);
  992. return -EFAULT;
  993. }
  994. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  995. if (!ret) {
  996. /* Copy the new buffer offsets back to the user's exec list. */
  997. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  998. (uintptr_t) args->buffers_ptr,
  999. exec2_list,
  1000. sizeof(*exec2_list) * args->buffer_count);
  1001. if (ret) {
  1002. ret = -EFAULT;
  1003. DRM_ERROR("failed to copy %d exec entries "
  1004. "back to user (%d)\n",
  1005. args->buffer_count, ret);
  1006. }
  1007. }
  1008. drm_free_large(exec2_list);
  1009. return ret;
  1010. }