i915_gem_execbuffer.c 31 KB

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