i915_vma.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * Copyright © 2016 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. */
  24. #include "i915_vma.h"
  25. #include "i915_drv.h"
  26. #include "intel_ringbuffer.h"
  27. #include "intel_frontbuffer.h"
  28. #include <drm/drm_gem.h>
  29. #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
  30. #include <linux/stackdepot.h>
  31. static void vma_print_allocator(struct i915_vma *vma, const char *reason)
  32. {
  33. unsigned long entries[12];
  34. struct stack_trace trace = {
  35. .entries = entries,
  36. .max_entries = ARRAY_SIZE(entries),
  37. };
  38. char buf[512];
  39. if (!vma->node.stack) {
  40. DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
  41. vma->node.start, vma->node.size, reason);
  42. return;
  43. }
  44. depot_fetch_stack(vma->node.stack, &trace);
  45. snprint_stack_trace(buf, sizeof(buf), &trace, 0);
  46. DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
  47. vma->node.start, vma->node.size, reason, buf);
  48. }
  49. #else
  50. static void vma_print_allocator(struct i915_vma *vma, const char *reason)
  51. {
  52. }
  53. #endif
  54. struct i915_vma_active {
  55. struct i915_gem_active base;
  56. struct i915_vma *vma;
  57. struct rb_node node;
  58. u64 timeline;
  59. };
  60. static void
  61. __i915_vma_retire(struct i915_vma *vma, struct i915_request *rq)
  62. {
  63. struct drm_i915_gem_object *obj = vma->obj;
  64. GEM_BUG_ON(!i915_vma_is_active(vma));
  65. if (--vma->active_count)
  66. return;
  67. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  68. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  69. GEM_BUG_ON(!i915_gem_object_is_active(obj));
  70. if (--obj->active_count)
  71. return;
  72. /* Prune the shared fence arrays iff completely idle (inc. external) */
  73. if (reservation_object_trylock(obj->resv)) {
  74. if (reservation_object_test_signaled_rcu(obj->resv, true))
  75. reservation_object_add_excl_fence(obj->resv, NULL);
  76. reservation_object_unlock(obj->resv);
  77. }
  78. /* Bump our place on the bound list to keep it roughly in LRU order
  79. * so that we don't steal from recently used but inactive objects
  80. * (unless we are forced to ofc!)
  81. */
  82. spin_lock(&rq->i915->mm.obj_lock);
  83. if (obj->bind_count)
  84. list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
  85. spin_unlock(&rq->i915->mm.obj_lock);
  86. obj->mm.dirty = true; /* be paranoid */
  87. if (i915_gem_object_has_active_reference(obj)) {
  88. i915_gem_object_clear_active_reference(obj);
  89. i915_gem_object_put(obj);
  90. }
  91. }
  92. static void
  93. i915_vma_retire(struct i915_gem_active *base, struct i915_request *rq)
  94. {
  95. struct i915_vma_active *active =
  96. container_of(base, typeof(*active), base);
  97. __i915_vma_retire(active->vma, rq);
  98. }
  99. static void
  100. i915_vma_last_retire(struct i915_gem_active *base, struct i915_request *rq)
  101. {
  102. __i915_vma_retire(container_of(base, struct i915_vma, last_active), rq);
  103. }
  104. static struct i915_vma *
  105. vma_create(struct drm_i915_gem_object *obj,
  106. struct i915_address_space *vm,
  107. const struct i915_ggtt_view *view)
  108. {
  109. struct i915_vma *vma;
  110. struct rb_node *rb, **p;
  111. /* The aliasing_ppgtt should never be used directly! */
  112. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
  113. vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
  114. if (vma == NULL)
  115. return ERR_PTR(-ENOMEM);
  116. vma->active = RB_ROOT;
  117. init_request_active(&vma->last_active, i915_vma_last_retire);
  118. init_request_active(&vma->last_fence, NULL);
  119. vma->vm = vm;
  120. vma->ops = &vm->vma_ops;
  121. vma->obj = obj;
  122. vma->resv = obj->resv;
  123. vma->size = obj->base.size;
  124. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  125. if (view && view->type != I915_GGTT_VIEW_NORMAL) {
  126. vma->ggtt_view = *view;
  127. if (view->type == I915_GGTT_VIEW_PARTIAL) {
  128. GEM_BUG_ON(range_overflows_t(u64,
  129. view->partial.offset,
  130. view->partial.size,
  131. obj->base.size >> PAGE_SHIFT));
  132. vma->size = view->partial.size;
  133. vma->size <<= PAGE_SHIFT;
  134. GEM_BUG_ON(vma->size > obj->base.size);
  135. } else if (view->type == I915_GGTT_VIEW_ROTATED) {
  136. vma->size = intel_rotation_info_size(&view->rotated);
  137. vma->size <<= PAGE_SHIFT;
  138. }
  139. }
  140. if (unlikely(vma->size > vm->total))
  141. goto err_vma;
  142. GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
  143. if (i915_is_ggtt(vm)) {
  144. if (unlikely(overflows_type(vma->size, u32)))
  145. goto err_vma;
  146. vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
  147. i915_gem_object_get_tiling(obj),
  148. i915_gem_object_get_stride(obj));
  149. if (unlikely(vma->fence_size < vma->size || /* overflow */
  150. vma->fence_size > vm->total))
  151. goto err_vma;
  152. GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
  153. vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
  154. i915_gem_object_get_tiling(obj),
  155. i915_gem_object_get_stride(obj));
  156. GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
  157. /*
  158. * We put the GGTT vma at the start of the vma-list, followed
  159. * by the ppGGTT vma. This allows us to break early when
  160. * iterating over only the GGTT vma for an object, see
  161. * for_each_ggtt_vma()
  162. */
  163. vma->flags |= I915_VMA_GGTT;
  164. list_add(&vma->obj_link, &obj->vma_list);
  165. } else {
  166. list_add_tail(&vma->obj_link, &obj->vma_list);
  167. }
  168. rb = NULL;
  169. p = &obj->vma_tree.rb_node;
  170. while (*p) {
  171. struct i915_vma *pos;
  172. rb = *p;
  173. pos = rb_entry(rb, struct i915_vma, obj_node);
  174. if (i915_vma_compare(pos, vm, view) < 0)
  175. p = &rb->rb_right;
  176. else
  177. p = &rb->rb_left;
  178. }
  179. rb_link_node(&vma->obj_node, rb, p);
  180. rb_insert_color(&vma->obj_node, &obj->vma_tree);
  181. list_add(&vma->vm_link, &vm->unbound_list);
  182. return vma;
  183. err_vma:
  184. kmem_cache_free(vm->i915->vmas, vma);
  185. return ERR_PTR(-E2BIG);
  186. }
  187. static struct i915_vma *
  188. vma_lookup(struct drm_i915_gem_object *obj,
  189. struct i915_address_space *vm,
  190. const struct i915_ggtt_view *view)
  191. {
  192. struct rb_node *rb;
  193. rb = obj->vma_tree.rb_node;
  194. while (rb) {
  195. struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
  196. long cmp;
  197. cmp = i915_vma_compare(vma, vm, view);
  198. if (cmp == 0)
  199. return vma;
  200. if (cmp < 0)
  201. rb = rb->rb_right;
  202. else
  203. rb = rb->rb_left;
  204. }
  205. return NULL;
  206. }
  207. /**
  208. * i915_vma_instance - return the singleton instance of the VMA
  209. * @obj: parent &struct drm_i915_gem_object to be mapped
  210. * @vm: address space in which the mapping is located
  211. * @view: additional mapping requirements
  212. *
  213. * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
  214. * the same @view characteristics. If a match is not found, one is created.
  215. * Once created, the VMA is kept until either the object is freed, or the
  216. * address space is closed.
  217. *
  218. * Must be called with struct_mutex held.
  219. *
  220. * Returns the vma, or an error pointer.
  221. */
  222. struct i915_vma *
  223. i915_vma_instance(struct drm_i915_gem_object *obj,
  224. struct i915_address_space *vm,
  225. const struct i915_ggtt_view *view)
  226. {
  227. struct i915_vma *vma;
  228. lockdep_assert_held(&obj->base.dev->struct_mutex);
  229. GEM_BUG_ON(view && !i915_is_ggtt(vm));
  230. GEM_BUG_ON(vm->closed);
  231. vma = vma_lookup(obj, vm, view);
  232. if (!vma)
  233. vma = vma_create(obj, vm, view);
  234. GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
  235. GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
  236. return vma;
  237. }
  238. /**
  239. * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
  240. * @vma: VMA to map
  241. * @cache_level: mapping cache level
  242. * @flags: flags like global or local mapping
  243. *
  244. * DMA addresses are taken from the scatter-gather table of this object (or of
  245. * this VMA in case of non-default GGTT views) and PTE entries set up.
  246. * Note that DMA addresses are also the only part of the SG table we care about.
  247. */
  248. int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
  249. u32 flags)
  250. {
  251. u32 bind_flags;
  252. u32 vma_flags;
  253. int ret;
  254. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  255. GEM_BUG_ON(vma->size > vma->node.size);
  256. if (GEM_WARN_ON(range_overflows(vma->node.start,
  257. vma->node.size,
  258. vma->vm->total)))
  259. return -ENODEV;
  260. if (GEM_WARN_ON(!flags))
  261. return -EINVAL;
  262. bind_flags = 0;
  263. if (flags & PIN_GLOBAL)
  264. bind_flags |= I915_VMA_GLOBAL_BIND;
  265. if (flags & PIN_USER)
  266. bind_flags |= I915_VMA_LOCAL_BIND;
  267. vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  268. if (flags & PIN_UPDATE)
  269. bind_flags |= vma_flags;
  270. else
  271. bind_flags &= ~vma_flags;
  272. if (bind_flags == 0)
  273. return 0;
  274. GEM_BUG_ON(!vma->pages);
  275. trace_i915_vma_bind(vma, bind_flags);
  276. ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
  277. if (ret)
  278. return ret;
  279. vma->flags |= bind_flags;
  280. return 0;
  281. }
  282. void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
  283. {
  284. void __iomem *ptr;
  285. int err;
  286. /* Access through the GTT requires the device to be awake. */
  287. assert_rpm_wakelock_held(vma->vm->i915);
  288. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  289. if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
  290. err = -ENODEV;
  291. goto err;
  292. }
  293. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  294. GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
  295. ptr = vma->iomap;
  296. if (ptr == NULL) {
  297. ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
  298. vma->node.start,
  299. vma->node.size);
  300. if (ptr == NULL) {
  301. err = -ENOMEM;
  302. goto err;
  303. }
  304. vma->iomap = ptr;
  305. }
  306. __i915_vma_pin(vma);
  307. err = i915_vma_pin_fence(vma);
  308. if (err)
  309. goto err_unpin;
  310. i915_vma_set_ggtt_write(vma);
  311. return ptr;
  312. err_unpin:
  313. __i915_vma_unpin(vma);
  314. err:
  315. return IO_ERR_PTR(err);
  316. }
  317. void i915_vma_flush_writes(struct i915_vma *vma)
  318. {
  319. if (!i915_vma_has_ggtt_write(vma))
  320. return;
  321. i915_gem_flush_ggtt_writes(vma->vm->i915);
  322. i915_vma_unset_ggtt_write(vma);
  323. }
  324. void i915_vma_unpin_iomap(struct i915_vma *vma)
  325. {
  326. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  327. GEM_BUG_ON(vma->iomap == NULL);
  328. i915_vma_flush_writes(vma);
  329. i915_vma_unpin_fence(vma);
  330. i915_vma_unpin(vma);
  331. }
  332. void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
  333. {
  334. struct i915_vma *vma;
  335. struct drm_i915_gem_object *obj;
  336. vma = fetch_and_zero(p_vma);
  337. if (!vma)
  338. return;
  339. obj = vma->obj;
  340. GEM_BUG_ON(!obj);
  341. i915_vma_unpin(vma);
  342. i915_vma_close(vma);
  343. if (flags & I915_VMA_RELEASE_MAP)
  344. i915_gem_object_unpin_map(obj);
  345. __i915_gem_object_release_unless_active(obj);
  346. }
  347. bool i915_vma_misplaced(const struct i915_vma *vma,
  348. u64 size, u64 alignment, u64 flags)
  349. {
  350. if (!drm_mm_node_allocated(&vma->node))
  351. return false;
  352. if (vma->node.size < size)
  353. return true;
  354. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  355. if (alignment && !IS_ALIGNED(vma->node.start, alignment))
  356. return true;
  357. if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
  358. return true;
  359. if (flags & PIN_OFFSET_BIAS &&
  360. vma->node.start < (flags & PIN_OFFSET_MASK))
  361. return true;
  362. if (flags & PIN_OFFSET_FIXED &&
  363. vma->node.start != (flags & PIN_OFFSET_MASK))
  364. return true;
  365. return false;
  366. }
  367. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  368. {
  369. bool mappable, fenceable;
  370. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  371. GEM_BUG_ON(!vma->fence_size);
  372. /*
  373. * Explicitly disable for rotated VMA since the display does not
  374. * need the fence and the VMA is not accessible to other users.
  375. */
  376. if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
  377. return;
  378. fenceable = (vma->node.size >= vma->fence_size &&
  379. IS_ALIGNED(vma->node.start, vma->fence_alignment));
  380. mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
  381. if (mappable && fenceable)
  382. vma->flags |= I915_VMA_CAN_FENCE;
  383. else
  384. vma->flags &= ~I915_VMA_CAN_FENCE;
  385. }
  386. static bool color_differs(struct drm_mm_node *node, unsigned long color)
  387. {
  388. return node->allocated && node->color != color;
  389. }
  390. bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
  391. {
  392. struct drm_mm_node *node = &vma->node;
  393. struct drm_mm_node *other;
  394. /*
  395. * On some machines we have to be careful when putting differing types
  396. * of snoopable memory together to avoid the prefetcher crossing memory
  397. * domains and dying. During vm initialisation, we decide whether or not
  398. * these constraints apply and set the drm_mm.color_adjust
  399. * appropriately.
  400. */
  401. if (vma->vm->mm.color_adjust == NULL)
  402. return true;
  403. /* Only valid to be called on an already inserted vma */
  404. GEM_BUG_ON(!drm_mm_node_allocated(node));
  405. GEM_BUG_ON(list_empty(&node->node_list));
  406. other = list_prev_entry(node, node_list);
  407. if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
  408. return false;
  409. other = list_next_entry(node, node_list);
  410. if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
  411. return false;
  412. return true;
  413. }
  414. static void assert_bind_count(const struct drm_i915_gem_object *obj)
  415. {
  416. /*
  417. * Combine the assertion that the object is bound and that we have
  418. * pinned its pages. But we should never have bound the object
  419. * more than we have pinned its pages. (For complete accuracy, we
  420. * assume that no else is pinning the pages, but as a rough assertion
  421. * that we will not run into problems later, this will do!)
  422. */
  423. GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
  424. }
  425. /**
  426. * i915_vma_insert - finds a slot for the vma in its address space
  427. * @vma: the vma
  428. * @size: requested size in bytes (can be larger than the VMA)
  429. * @alignment: required alignment
  430. * @flags: mask of PIN_* flags to use
  431. *
  432. * First we try to allocate some free space that meets the requirements for
  433. * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
  434. * preferrably the oldest idle entry to make room for the new VMA.
  435. *
  436. * Returns:
  437. * 0 on success, negative error code otherwise.
  438. */
  439. static int
  440. i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  441. {
  442. struct drm_i915_private *dev_priv = vma->vm->i915;
  443. unsigned int cache_level;
  444. u64 start, end;
  445. int ret;
  446. GEM_BUG_ON(i915_vma_is_closed(vma));
  447. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  448. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  449. size = max(size, vma->size);
  450. alignment = max(alignment, vma->display_alignment);
  451. if (flags & PIN_MAPPABLE) {
  452. size = max_t(typeof(size), size, vma->fence_size);
  453. alignment = max_t(typeof(alignment),
  454. alignment, vma->fence_alignment);
  455. }
  456. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  457. GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  458. GEM_BUG_ON(!is_power_of_2(alignment));
  459. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  460. GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  461. end = vma->vm->total;
  462. if (flags & PIN_MAPPABLE)
  463. end = min_t(u64, end, dev_priv->ggtt.mappable_end);
  464. if (flags & PIN_ZONE_4G)
  465. end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
  466. GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  467. /* If binding the object/GGTT view requires more space than the entire
  468. * aperture has, reject it early before evicting everything in a vain
  469. * attempt to find space.
  470. */
  471. if (size > end) {
  472. DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
  473. size, flags & PIN_MAPPABLE ? "mappable" : "total",
  474. end);
  475. return -ENOSPC;
  476. }
  477. if (vma->obj) {
  478. ret = i915_gem_object_pin_pages(vma->obj);
  479. if (ret)
  480. return ret;
  481. cache_level = vma->obj->cache_level;
  482. } else {
  483. cache_level = 0;
  484. }
  485. GEM_BUG_ON(vma->pages);
  486. ret = vma->ops->set_pages(vma);
  487. if (ret)
  488. goto err_unpin;
  489. if (flags & PIN_OFFSET_FIXED) {
  490. u64 offset = flags & PIN_OFFSET_MASK;
  491. if (!IS_ALIGNED(offset, alignment) ||
  492. range_overflows(offset, size, end)) {
  493. ret = -EINVAL;
  494. goto err_clear;
  495. }
  496. ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
  497. size, offset, cache_level,
  498. flags);
  499. if (ret)
  500. goto err_clear;
  501. } else {
  502. /*
  503. * We only support huge gtt pages through the 48b PPGTT,
  504. * however we also don't want to force any alignment for
  505. * objects which need to be tightly packed into the low 32bits.
  506. *
  507. * Note that we assume that GGTT are limited to 4GiB for the
  508. * forseeable future. See also i915_ggtt_offset().
  509. */
  510. if (upper_32_bits(end - 1) &&
  511. vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
  512. /*
  513. * We can't mix 64K and 4K PTEs in the same page-table
  514. * (2M block), and so to avoid the ugliness and
  515. * complexity of coloring we opt for just aligning 64K
  516. * objects to 2M.
  517. */
  518. u64 page_alignment =
  519. rounddown_pow_of_two(vma->page_sizes.sg |
  520. I915_GTT_PAGE_SIZE_2M);
  521. /*
  522. * Check we don't expand for the limited Global GTT
  523. * (mappable aperture is even more precious!). This
  524. * also checks that we exclude the aliasing-ppgtt.
  525. */
  526. GEM_BUG_ON(i915_vma_is_ggtt(vma));
  527. alignment = max(alignment, page_alignment);
  528. if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
  529. size = round_up(size, I915_GTT_PAGE_SIZE_2M);
  530. }
  531. ret = i915_gem_gtt_insert(vma->vm, &vma->node,
  532. size, alignment, cache_level,
  533. start, end, flags);
  534. if (ret)
  535. goto err_clear;
  536. GEM_BUG_ON(vma->node.start < start);
  537. GEM_BUG_ON(vma->node.start + vma->node.size > end);
  538. }
  539. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  540. GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
  541. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  542. if (vma->obj) {
  543. struct drm_i915_gem_object *obj = vma->obj;
  544. spin_lock(&dev_priv->mm.obj_lock);
  545. list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
  546. obj->bind_count++;
  547. spin_unlock(&dev_priv->mm.obj_lock);
  548. assert_bind_count(obj);
  549. }
  550. return 0;
  551. err_clear:
  552. vma->ops->clear_pages(vma);
  553. err_unpin:
  554. if (vma->obj)
  555. i915_gem_object_unpin_pages(vma->obj);
  556. return ret;
  557. }
  558. static void
  559. i915_vma_remove(struct i915_vma *vma)
  560. {
  561. struct drm_i915_private *i915 = vma->vm->i915;
  562. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  563. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  564. vma->ops->clear_pages(vma);
  565. drm_mm_remove_node(&vma->node);
  566. list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
  567. /*
  568. * Since the unbound list is global, only move to that list if
  569. * no more VMAs exist.
  570. */
  571. if (vma->obj) {
  572. struct drm_i915_gem_object *obj = vma->obj;
  573. spin_lock(&i915->mm.obj_lock);
  574. if (--obj->bind_count == 0)
  575. list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
  576. spin_unlock(&i915->mm.obj_lock);
  577. /*
  578. * And finally now the object is completely decoupled from this
  579. * vma, we can drop its hold on the backing storage and allow
  580. * it to be reaped by the shrinker.
  581. */
  582. i915_gem_object_unpin_pages(obj);
  583. assert_bind_count(obj);
  584. }
  585. }
  586. int __i915_vma_do_pin(struct i915_vma *vma,
  587. u64 size, u64 alignment, u64 flags)
  588. {
  589. const unsigned int bound = vma->flags;
  590. int ret;
  591. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  592. GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
  593. GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
  594. if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
  595. ret = -EBUSY;
  596. goto err_unpin;
  597. }
  598. if ((bound & I915_VMA_BIND_MASK) == 0) {
  599. ret = i915_vma_insert(vma, size, alignment, flags);
  600. if (ret)
  601. goto err_unpin;
  602. }
  603. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  604. ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
  605. if (ret)
  606. goto err_remove;
  607. GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
  608. if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
  609. __i915_vma_set_map_and_fenceable(vma);
  610. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  611. return 0;
  612. err_remove:
  613. if ((bound & I915_VMA_BIND_MASK) == 0) {
  614. i915_vma_remove(vma);
  615. GEM_BUG_ON(vma->pages);
  616. GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
  617. }
  618. err_unpin:
  619. __i915_vma_unpin(vma);
  620. return ret;
  621. }
  622. void i915_vma_close(struct i915_vma *vma)
  623. {
  624. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  625. GEM_BUG_ON(i915_vma_is_closed(vma));
  626. vma->flags |= I915_VMA_CLOSED;
  627. /*
  628. * We defer actually closing, unbinding and destroying the VMA until
  629. * the next idle point, or if the object is freed in the meantime. By
  630. * postponing the unbind, we allow for it to be resurrected by the
  631. * client, avoiding the work required to rebind the VMA. This is
  632. * advantageous for DRI, where the client/server pass objects
  633. * between themselves, temporarily opening a local VMA to the
  634. * object, and then closing it again. The same object is then reused
  635. * on the next frame (or two, depending on the depth of the swap queue)
  636. * causing us to rebind the VMA once more. This ends up being a lot
  637. * of wasted work for the steady state.
  638. */
  639. list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
  640. }
  641. void i915_vma_reopen(struct i915_vma *vma)
  642. {
  643. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  644. if (vma->flags & I915_VMA_CLOSED) {
  645. vma->flags &= ~I915_VMA_CLOSED;
  646. list_del(&vma->closed_link);
  647. }
  648. }
  649. static void __i915_vma_destroy(struct i915_vma *vma)
  650. {
  651. struct drm_i915_private *i915 = vma->vm->i915;
  652. struct i915_vma_active *iter, *n;
  653. GEM_BUG_ON(vma->node.allocated);
  654. GEM_BUG_ON(vma->fence);
  655. GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
  656. list_del(&vma->obj_link);
  657. list_del(&vma->vm_link);
  658. if (vma->obj)
  659. rb_erase(&vma->obj_node, &vma->obj->vma_tree);
  660. rbtree_postorder_for_each_entry_safe(iter, n, &vma->active, node) {
  661. GEM_BUG_ON(i915_gem_active_isset(&iter->base));
  662. kfree(iter);
  663. }
  664. kmem_cache_free(i915->vmas, vma);
  665. }
  666. void i915_vma_destroy(struct i915_vma *vma)
  667. {
  668. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  669. GEM_BUG_ON(i915_vma_is_active(vma));
  670. GEM_BUG_ON(i915_vma_is_pinned(vma));
  671. if (i915_vma_is_closed(vma))
  672. list_del(&vma->closed_link);
  673. WARN_ON(i915_vma_unbind(vma));
  674. __i915_vma_destroy(vma);
  675. }
  676. void i915_vma_parked(struct drm_i915_private *i915)
  677. {
  678. struct i915_vma *vma, *next;
  679. list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
  680. GEM_BUG_ON(!i915_vma_is_closed(vma));
  681. i915_vma_destroy(vma);
  682. }
  683. GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
  684. }
  685. static void __i915_vma_iounmap(struct i915_vma *vma)
  686. {
  687. GEM_BUG_ON(i915_vma_is_pinned(vma));
  688. if (vma->iomap == NULL)
  689. return;
  690. io_mapping_unmap(vma->iomap);
  691. vma->iomap = NULL;
  692. }
  693. void i915_vma_revoke_mmap(struct i915_vma *vma)
  694. {
  695. struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
  696. u64 vma_offset;
  697. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  698. if (!i915_vma_has_userfault(vma))
  699. return;
  700. GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
  701. GEM_BUG_ON(!vma->obj->userfault_count);
  702. vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
  703. unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
  704. drm_vma_node_offset_addr(node) + vma_offset,
  705. vma->size,
  706. 1);
  707. i915_vma_unset_userfault(vma);
  708. if (!--vma->obj->userfault_count)
  709. list_del(&vma->obj->userfault_link);
  710. }
  711. static void export_fence(struct i915_vma *vma,
  712. struct i915_request *rq,
  713. unsigned int flags)
  714. {
  715. struct reservation_object *resv = vma->resv;
  716. /*
  717. * Ignore errors from failing to allocate the new fence, we can't
  718. * handle an error right now. Worst case should be missed
  719. * synchronisation leading to rendering corruption.
  720. */
  721. reservation_object_lock(resv, NULL);
  722. if (flags & EXEC_OBJECT_WRITE)
  723. reservation_object_add_excl_fence(resv, &rq->fence);
  724. else if (reservation_object_reserve_shared(resv) == 0)
  725. reservation_object_add_shared_fence(resv, &rq->fence);
  726. reservation_object_unlock(resv);
  727. }
  728. static struct i915_gem_active *active_instance(struct i915_vma *vma, u64 idx)
  729. {
  730. struct i915_vma_active *active;
  731. struct rb_node **p, *parent;
  732. struct i915_request *old;
  733. /*
  734. * We track the most recently used timeline to skip a rbtree search
  735. * for the common case, under typical loads we never need the rbtree
  736. * at all. We can reuse the last_active slot if it is empty, that is
  737. * after the previous activity has been retired, or if the active
  738. * matches the current timeline.
  739. *
  740. * Note that we allow the timeline to be active simultaneously in
  741. * the rbtree and the last_active cache. We do this to avoid having
  742. * to search and replace the rbtree element for a new timeline, with
  743. * the cost being that we must be aware that the vma may be retired
  744. * twice for the same timeline (as the older rbtree element will be
  745. * retired before the new request added to last_active).
  746. */
  747. old = i915_gem_active_raw(&vma->last_active,
  748. &vma->vm->i915->drm.struct_mutex);
  749. if (!old || old->fence.context == idx)
  750. goto out;
  751. /* Move the currently active fence into the rbtree */
  752. idx = old->fence.context;
  753. parent = NULL;
  754. p = &vma->active.rb_node;
  755. while (*p) {
  756. parent = *p;
  757. active = rb_entry(parent, struct i915_vma_active, node);
  758. if (active->timeline == idx)
  759. goto replace;
  760. if (active->timeline < idx)
  761. p = &parent->rb_right;
  762. else
  763. p = &parent->rb_left;
  764. }
  765. active = kmalloc(sizeof(*active), GFP_KERNEL);
  766. /* kmalloc may retire the vma->last_active request (thanks shrinker)! */
  767. if (unlikely(!i915_gem_active_raw(&vma->last_active,
  768. &vma->vm->i915->drm.struct_mutex))) {
  769. kfree(active);
  770. goto out;
  771. }
  772. if (unlikely(!active))
  773. return ERR_PTR(-ENOMEM);
  774. init_request_active(&active->base, i915_vma_retire);
  775. active->vma = vma;
  776. active->timeline = idx;
  777. rb_link_node(&active->node, parent, p);
  778. rb_insert_color(&active->node, &vma->active);
  779. replace:
  780. /*
  781. * Overwrite the previous active slot in the rbtree with last_active,
  782. * leaving last_active zeroed. If the previous slot is still active,
  783. * we must be careful as we now only expect to receive one retire
  784. * callback not two, and so much undo the active counting for the
  785. * overwritten slot.
  786. */
  787. if (i915_gem_active_isset(&active->base)) {
  788. /* Retire ourselves from the old rq->active_list */
  789. __list_del_entry(&active->base.link);
  790. vma->active_count--;
  791. GEM_BUG_ON(!vma->active_count);
  792. }
  793. GEM_BUG_ON(list_empty(&vma->last_active.link));
  794. list_replace_init(&vma->last_active.link, &active->base.link);
  795. active->base.request = fetch_and_zero(&vma->last_active.request);
  796. out:
  797. return &vma->last_active;
  798. }
  799. int i915_vma_move_to_active(struct i915_vma *vma,
  800. struct i915_request *rq,
  801. unsigned int flags)
  802. {
  803. struct drm_i915_gem_object *obj = vma->obj;
  804. struct i915_gem_active *active;
  805. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  806. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  807. active = active_instance(vma, rq->fence.context);
  808. if (IS_ERR(active))
  809. return PTR_ERR(active);
  810. /*
  811. * Add a reference if we're newly entering the active list.
  812. * The order in which we add operations to the retirement queue is
  813. * vital here: mark_active adds to the start of the callback list,
  814. * such that subsequent callbacks are called first. Therefore we
  815. * add the active reference first and queue for it to be dropped
  816. * *last*.
  817. */
  818. if (!i915_gem_active_isset(active) && !vma->active_count++) {
  819. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  820. obj->active_count++;
  821. }
  822. i915_gem_active_set(active, rq);
  823. GEM_BUG_ON(!i915_vma_is_active(vma));
  824. GEM_BUG_ON(!obj->active_count);
  825. obj->write_domain = 0;
  826. if (flags & EXEC_OBJECT_WRITE) {
  827. obj->write_domain = I915_GEM_DOMAIN_RENDER;
  828. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  829. i915_gem_active_set(&obj->frontbuffer_write, rq);
  830. obj->read_domains = 0;
  831. }
  832. obj->read_domains |= I915_GEM_GPU_DOMAINS;
  833. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  834. i915_gem_active_set(&vma->last_fence, rq);
  835. export_fence(vma, rq, flags);
  836. return 0;
  837. }
  838. int i915_vma_unbind(struct i915_vma *vma)
  839. {
  840. int ret;
  841. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  842. /*
  843. * First wait upon any activity as retiring the request may
  844. * have side-effects such as unpinning or even unbinding this vma.
  845. */
  846. might_sleep();
  847. if (i915_vma_is_active(vma)) {
  848. struct i915_vma_active *active, *n;
  849. /*
  850. * When a closed VMA is retired, it is unbound - eek.
  851. * In order to prevent it from being recursively closed,
  852. * take a pin on the vma so that the second unbind is
  853. * aborted.
  854. *
  855. * Even more scary is that the retire callback may free
  856. * the object (last active vma). To prevent the explosion
  857. * we defer the actual object free to a worker that can
  858. * only proceed once it acquires the struct_mutex (which
  859. * we currently hold, therefore it cannot free this object
  860. * before we are finished).
  861. */
  862. __i915_vma_pin(vma);
  863. ret = i915_gem_active_retire(&vma->last_active,
  864. &vma->vm->i915->drm.struct_mutex);
  865. if (ret)
  866. goto unpin;
  867. rbtree_postorder_for_each_entry_safe(active, n,
  868. &vma->active, node) {
  869. ret = i915_gem_active_retire(&active->base,
  870. &vma->vm->i915->drm.struct_mutex);
  871. if (ret)
  872. goto unpin;
  873. }
  874. ret = i915_gem_active_retire(&vma->last_fence,
  875. &vma->vm->i915->drm.struct_mutex);
  876. unpin:
  877. __i915_vma_unpin(vma);
  878. if (ret)
  879. return ret;
  880. }
  881. GEM_BUG_ON(i915_vma_is_active(vma));
  882. if (i915_vma_is_pinned(vma)) {
  883. vma_print_allocator(vma, "is pinned");
  884. return -EBUSY;
  885. }
  886. if (!drm_mm_node_allocated(&vma->node))
  887. return 0;
  888. if (i915_vma_is_map_and_fenceable(vma)) {
  889. /*
  890. * Check that we have flushed all writes through the GGTT
  891. * before the unbind, other due to non-strict nature of those
  892. * indirect writes they may end up referencing the GGTT PTE
  893. * after the unbind.
  894. */
  895. i915_vma_flush_writes(vma);
  896. GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
  897. /* release the fence reg _after_ flushing */
  898. ret = i915_vma_put_fence(vma);
  899. if (ret)
  900. return ret;
  901. /* Force a pagefault for domain tracking on next user access */
  902. i915_vma_revoke_mmap(vma);
  903. __i915_vma_iounmap(vma);
  904. vma->flags &= ~I915_VMA_CAN_FENCE;
  905. }
  906. GEM_BUG_ON(vma->fence);
  907. GEM_BUG_ON(i915_vma_has_userfault(vma));
  908. if (likely(!vma->vm->closed)) {
  909. trace_i915_vma_unbind(vma);
  910. vma->ops->unbind_vma(vma);
  911. }
  912. vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  913. i915_vma_remove(vma);
  914. return 0;
  915. }
  916. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  917. #include "selftests/i915_vma.c"
  918. #endif