i915_vma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. static void
  30. i915_vma_retire(struct i915_gem_active *active, struct i915_request *rq)
  31. {
  32. const unsigned int idx = rq->engine->id;
  33. struct i915_vma *vma =
  34. container_of(active, struct i915_vma, last_read[idx]);
  35. struct drm_i915_gem_object *obj = vma->obj;
  36. GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
  37. i915_vma_clear_active(vma, idx);
  38. if (i915_vma_is_active(vma))
  39. return;
  40. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  41. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  42. GEM_BUG_ON(!i915_gem_object_is_active(obj));
  43. if (--obj->active_count)
  44. return;
  45. /* Prune the shared fence arrays iff completely idle (inc. external) */
  46. if (reservation_object_trylock(obj->resv)) {
  47. if (reservation_object_test_signaled_rcu(obj->resv, true))
  48. reservation_object_add_excl_fence(obj->resv, NULL);
  49. reservation_object_unlock(obj->resv);
  50. }
  51. /* Bump our place on the bound list to keep it roughly in LRU order
  52. * so that we don't steal from recently used but inactive objects
  53. * (unless we are forced to ofc!)
  54. */
  55. spin_lock(&rq->i915->mm.obj_lock);
  56. if (obj->bind_count)
  57. list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
  58. spin_unlock(&rq->i915->mm.obj_lock);
  59. obj->mm.dirty = true; /* be paranoid */
  60. if (i915_gem_object_has_active_reference(obj)) {
  61. i915_gem_object_clear_active_reference(obj);
  62. i915_gem_object_put(obj);
  63. }
  64. }
  65. static struct i915_vma *
  66. vma_create(struct drm_i915_gem_object *obj,
  67. struct i915_address_space *vm,
  68. const struct i915_ggtt_view *view)
  69. {
  70. struct i915_vma *vma;
  71. struct rb_node *rb, **p;
  72. int i;
  73. /* The aliasing_ppgtt should never be used directly! */
  74. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  75. vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
  76. if (vma == NULL)
  77. return ERR_PTR(-ENOMEM);
  78. for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
  79. init_request_active(&vma->last_read[i], i915_vma_retire);
  80. init_request_active(&vma->last_fence, NULL);
  81. vma->vm = vm;
  82. vma->obj = obj;
  83. vma->resv = obj->resv;
  84. vma->size = obj->base.size;
  85. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  86. if (view && view->type != I915_GGTT_VIEW_NORMAL) {
  87. vma->ggtt_view = *view;
  88. if (view->type == I915_GGTT_VIEW_PARTIAL) {
  89. GEM_BUG_ON(range_overflows_t(u64,
  90. view->partial.offset,
  91. view->partial.size,
  92. obj->base.size >> PAGE_SHIFT));
  93. vma->size = view->partial.size;
  94. vma->size <<= PAGE_SHIFT;
  95. GEM_BUG_ON(vma->size >= obj->base.size);
  96. } else if (view->type == I915_GGTT_VIEW_ROTATED) {
  97. vma->size = intel_rotation_info_size(&view->rotated);
  98. vma->size <<= PAGE_SHIFT;
  99. }
  100. }
  101. if (unlikely(vma->size > vm->total))
  102. goto err_vma;
  103. GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
  104. if (i915_is_ggtt(vm)) {
  105. if (unlikely(overflows_type(vma->size, u32)))
  106. goto err_vma;
  107. vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
  108. i915_gem_object_get_tiling(obj),
  109. i915_gem_object_get_stride(obj));
  110. if (unlikely(vma->fence_size < vma->size || /* overflow */
  111. vma->fence_size > vm->total))
  112. goto err_vma;
  113. GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
  114. vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
  115. i915_gem_object_get_tiling(obj),
  116. i915_gem_object_get_stride(obj));
  117. GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
  118. /*
  119. * We put the GGTT vma at the start of the vma-list, followed
  120. * by the ppGGTT vma. This allows us to break early when
  121. * iterating over only the GGTT vma for an object, see
  122. * for_each_ggtt_vma()
  123. */
  124. vma->flags |= I915_VMA_GGTT;
  125. list_add(&vma->obj_link, &obj->vma_list);
  126. } else {
  127. i915_ppgtt_get(i915_vm_to_ppgtt(vm));
  128. list_add_tail(&vma->obj_link, &obj->vma_list);
  129. }
  130. rb = NULL;
  131. p = &obj->vma_tree.rb_node;
  132. while (*p) {
  133. struct i915_vma *pos;
  134. rb = *p;
  135. pos = rb_entry(rb, struct i915_vma, obj_node);
  136. if (i915_vma_compare(pos, vm, view) < 0)
  137. p = &rb->rb_right;
  138. else
  139. p = &rb->rb_left;
  140. }
  141. rb_link_node(&vma->obj_node, rb, p);
  142. rb_insert_color(&vma->obj_node, &obj->vma_tree);
  143. list_add(&vma->vm_link, &vm->unbound_list);
  144. return vma;
  145. err_vma:
  146. kmem_cache_free(vm->i915->vmas, vma);
  147. return ERR_PTR(-E2BIG);
  148. }
  149. static struct i915_vma *
  150. vma_lookup(struct drm_i915_gem_object *obj,
  151. struct i915_address_space *vm,
  152. const struct i915_ggtt_view *view)
  153. {
  154. struct rb_node *rb;
  155. rb = obj->vma_tree.rb_node;
  156. while (rb) {
  157. struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
  158. long cmp;
  159. cmp = i915_vma_compare(vma, vm, view);
  160. if (cmp == 0)
  161. return vma;
  162. if (cmp < 0)
  163. rb = rb->rb_right;
  164. else
  165. rb = rb->rb_left;
  166. }
  167. return NULL;
  168. }
  169. /**
  170. * i915_vma_instance - return the singleton instance of the VMA
  171. * @obj: parent &struct drm_i915_gem_object to be mapped
  172. * @vm: address space in which the mapping is located
  173. * @view: additional mapping requirements
  174. *
  175. * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
  176. * the same @view characteristics. If a match is not found, one is created.
  177. * Once created, the VMA is kept until either the object is freed, or the
  178. * address space is closed.
  179. *
  180. * Must be called with struct_mutex held.
  181. *
  182. * Returns the vma, or an error pointer.
  183. */
  184. struct i915_vma *
  185. i915_vma_instance(struct drm_i915_gem_object *obj,
  186. struct i915_address_space *vm,
  187. const struct i915_ggtt_view *view)
  188. {
  189. struct i915_vma *vma;
  190. lockdep_assert_held(&obj->base.dev->struct_mutex);
  191. GEM_BUG_ON(view && !i915_is_ggtt(vm));
  192. GEM_BUG_ON(vm->closed);
  193. vma = vma_lookup(obj, vm, view);
  194. if (!vma)
  195. vma = vma_create(obj, vm, view);
  196. GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
  197. GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
  198. return vma;
  199. }
  200. /**
  201. * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
  202. * @vma: VMA to map
  203. * @cache_level: mapping cache level
  204. * @flags: flags like global or local mapping
  205. *
  206. * DMA addresses are taken from the scatter-gather table of this object (or of
  207. * this VMA in case of non-default GGTT views) and PTE entries set up.
  208. * Note that DMA addresses are also the only part of the SG table we care about.
  209. */
  210. int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
  211. u32 flags)
  212. {
  213. u32 bind_flags;
  214. u32 vma_flags;
  215. int ret;
  216. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  217. GEM_BUG_ON(vma->size > vma->node.size);
  218. if (GEM_WARN_ON(range_overflows(vma->node.start,
  219. vma->node.size,
  220. vma->vm->total)))
  221. return -ENODEV;
  222. if (GEM_WARN_ON(!flags))
  223. return -EINVAL;
  224. bind_flags = 0;
  225. if (flags & PIN_GLOBAL)
  226. bind_flags |= I915_VMA_GLOBAL_BIND;
  227. if (flags & PIN_USER)
  228. bind_flags |= I915_VMA_LOCAL_BIND;
  229. vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  230. if (flags & PIN_UPDATE)
  231. bind_flags |= vma_flags;
  232. else
  233. bind_flags &= ~vma_flags;
  234. if (bind_flags == 0)
  235. return 0;
  236. GEM_BUG_ON(!vma->pages);
  237. trace_i915_vma_bind(vma, bind_flags);
  238. ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
  239. if (ret)
  240. return ret;
  241. vma->flags |= bind_flags;
  242. return 0;
  243. }
  244. void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
  245. {
  246. void __iomem *ptr;
  247. int err;
  248. /* Access through the GTT requires the device to be awake. */
  249. assert_rpm_wakelock_held(vma->vm->i915);
  250. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  251. if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
  252. err = -ENODEV;
  253. goto err;
  254. }
  255. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  256. GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
  257. ptr = vma->iomap;
  258. if (ptr == NULL) {
  259. ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
  260. vma->node.start,
  261. vma->node.size);
  262. if (ptr == NULL) {
  263. err = -ENOMEM;
  264. goto err;
  265. }
  266. vma->iomap = ptr;
  267. }
  268. __i915_vma_pin(vma);
  269. err = i915_vma_pin_fence(vma);
  270. if (err)
  271. goto err_unpin;
  272. i915_vma_set_ggtt_write(vma);
  273. return ptr;
  274. err_unpin:
  275. __i915_vma_unpin(vma);
  276. err:
  277. return IO_ERR_PTR(err);
  278. }
  279. void i915_vma_flush_writes(struct i915_vma *vma)
  280. {
  281. if (!i915_vma_has_ggtt_write(vma))
  282. return;
  283. i915_gem_flush_ggtt_writes(vma->vm->i915);
  284. i915_vma_unset_ggtt_write(vma);
  285. }
  286. void i915_vma_unpin_iomap(struct i915_vma *vma)
  287. {
  288. lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
  289. GEM_BUG_ON(vma->iomap == NULL);
  290. i915_vma_flush_writes(vma);
  291. i915_vma_unpin_fence(vma);
  292. i915_vma_unpin(vma);
  293. }
  294. void i915_vma_unpin_and_release(struct i915_vma **p_vma)
  295. {
  296. struct i915_vma *vma;
  297. struct drm_i915_gem_object *obj;
  298. vma = fetch_and_zero(p_vma);
  299. if (!vma)
  300. return;
  301. obj = vma->obj;
  302. i915_vma_unpin(vma);
  303. i915_vma_close(vma);
  304. __i915_gem_object_release_unless_active(obj);
  305. }
  306. bool i915_vma_misplaced(const struct i915_vma *vma,
  307. u64 size, u64 alignment, u64 flags)
  308. {
  309. if (!drm_mm_node_allocated(&vma->node))
  310. return false;
  311. if (vma->node.size < size)
  312. return true;
  313. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  314. if (alignment && !IS_ALIGNED(vma->node.start, alignment))
  315. return true;
  316. if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
  317. return true;
  318. if (flags & PIN_OFFSET_BIAS &&
  319. vma->node.start < (flags & PIN_OFFSET_MASK))
  320. return true;
  321. if (flags & PIN_OFFSET_FIXED &&
  322. vma->node.start != (flags & PIN_OFFSET_MASK))
  323. return true;
  324. return false;
  325. }
  326. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  327. {
  328. bool mappable, fenceable;
  329. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  330. GEM_BUG_ON(!vma->fence_size);
  331. /*
  332. * Explicitly disable for rotated VMA since the display does not
  333. * need the fence and the VMA is not accessible to other users.
  334. */
  335. if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
  336. return;
  337. fenceable = (vma->node.size >= vma->fence_size &&
  338. IS_ALIGNED(vma->node.start, vma->fence_alignment));
  339. mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
  340. if (mappable && fenceable)
  341. vma->flags |= I915_VMA_CAN_FENCE;
  342. else
  343. vma->flags &= ~I915_VMA_CAN_FENCE;
  344. }
  345. static bool color_differs(struct drm_mm_node *node, unsigned long color)
  346. {
  347. return node->allocated && node->color != color;
  348. }
  349. bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
  350. {
  351. struct drm_mm_node *node = &vma->node;
  352. struct drm_mm_node *other;
  353. /*
  354. * On some machines we have to be careful when putting differing types
  355. * of snoopable memory together to avoid the prefetcher crossing memory
  356. * domains and dying. During vm initialisation, we decide whether or not
  357. * these constraints apply and set the drm_mm.color_adjust
  358. * appropriately.
  359. */
  360. if (vma->vm->mm.color_adjust == NULL)
  361. return true;
  362. /* Only valid to be called on an already inserted vma */
  363. GEM_BUG_ON(!drm_mm_node_allocated(node));
  364. GEM_BUG_ON(list_empty(&node->node_list));
  365. other = list_prev_entry(node, node_list);
  366. if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
  367. return false;
  368. other = list_next_entry(node, node_list);
  369. if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
  370. return false;
  371. return true;
  372. }
  373. /**
  374. * i915_vma_insert - finds a slot for the vma in its address space
  375. * @vma: the vma
  376. * @size: requested size in bytes (can be larger than the VMA)
  377. * @alignment: required alignment
  378. * @flags: mask of PIN_* flags to use
  379. *
  380. * First we try to allocate some free space that meets the requirements for
  381. * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
  382. * preferrably the oldest idle entry to make room for the new VMA.
  383. *
  384. * Returns:
  385. * 0 on success, negative error code otherwise.
  386. */
  387. static int
  388. i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  389. {
  390. struct drm_i915_private *dev_priv = vma->vm->i915;
  391. struct drm_i915_gem_object *obj = vma->obj;
  392. u64 start, end;
  393. int ret;
  394. GEM_BUG_ON(i915_vma_is_closed(vma));
  395. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  396. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  397. size = max(size, vma->size);
  398. alignment = max(alignment, vma->display_alignment);
  399. if (flags & PIN_MAPPABLE) {
  400. size = max_t(typeof(size), size, vma->fence_size);
  401. alignment = max_t(typeof(alignment),
  402. alignment, vma->fence_alignment);
  403. }
  404. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  405. GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  406. GEM_BUG_ON(!is_power_of_2(alignment));
  407. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  408. GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  409. end = vma->vm->total;
  410. if (flags & PIN_MAPPABLE)
  411. end = min_t(u64, end, dev_priv->ggtt.mappable_end);
  412. if (flags & PIN_ZONE_4G)
  413. end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
  414. GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  415. /* If binding the object/GGTT view requires more space than the entire
  416. * aperture has, reject it early before evicting everything in a vain
  417. * attempt to find space.
  418. */
  419. if (size > end) {
  420. DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
  421. size, obj->base.size,
  422. flags & PIN_MAPPABLE ? "mappable" : "total",
  423. end);
  424. return -ENOSPC;
  425. }
  426. ret = i915_gem_object_pin_pages(obj);
  427. if (ret)
  428. return ret;
  429. GEM_BUG_ON(vma->pages);
  430. ret = vma->vm->set_pages(vma);
  431. if (ret)
  432. goto err_unpin;
  433. if (flags & PIN_OFFSET_FIXED) {
  434. u64 offset = flags & PIN_OFFSET_MASK;
  435. if (!IS_ALIGNED(offset, alignment) ||
  436. range_overflows(offset, size, end)) {
  437. ret = -EINVAL;
  438. goto err_clear;
  439. }
  440. ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
  441. size, offset, obj->cache_level,
  442. flags);
  443. if (ret)
  444. goto err_clear;
  445. } else {
  446. /*
  447. * We only support huge gtt pages through the 48b PPGTT,
  448. * however we also don't want to force any alignment for
  449. * objects which need to be tightly packed into the low 32bits.
  450. *
  451. * Note that we assume that GGTT are limited to 4GiB for the
  452. * forseeable future. See also i915_ggtt_offset().
  453. */
  454. if (upper_32_bits(end - 1) &&
  455. vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
  456. /*
  457. * We can't mix 64K and 4K PTEs in the same page-table
  458. * (2M block), and so to avoid the ugliness and
  459. * complexity of coloring we opt for just aligning 64K
  460. * objects to 2M.
  461. */
  462. u64 page_alignment =
  463. rounddown_pow_of_two(vma->page_sizes.sg |
  464. I915_GTT_PAGE_SIZE_2M);
  465. /*
  466. * Check we don't expand for the limited Global GTT
  467. * (mappable aperture is even more precious!). This
  468. * also checks that we exclude the aliasing-ppgtt.
  469. */
  470. GEM_BUG_ON(i915_vma_is_ggtt(vma));
  471. alignment = max(alignment, page_alignment);
  472. if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
  473. size = round_up(size, I915_GTT_PAGE_SIZE_2M);
  474. }
  475. ret = i915_gem_gtt_insert(vma->vm, &vma->node,
  476. size, alignment, obj->cache_level,
  477. start, end, flags);
  478. if (ret)
  479. goto err_clear;
  480. GEM_BUG_ON(vma->node.start < start);
  481. GEM_BUG_ON(vma->node.start + vma->node.size > end);
  482. }
  483. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  484. GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
  485. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  486. spin_lock(&dev_priv->mm.obj_lock);
  487. list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
  488. obj->bind_count++;
  489. spin_unlock(&dev_priv->mm.obj_lock);
  490. GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
  491. return 0;
  492. err_clear:
  493. vma->vm->clear_pages(vma);
  494. err_unpin:
  495. i915_gem_object_unpin_pages(obj);
  496. return ret;
  497. }
  498. static void
  499. i915_vma_remove(struct i915_vma *vma)
  500. {
  501. struct drm_i915_private *i915 = vma->vm->i915;
  502. struct drm_i915_gem_object *obj = vma->obj;
  503. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  504. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  505. vma->vm->clear_pages(vma);
  506. drm_mm_remove_node(&vma->node);
  507. list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
  508. /* Since the unbound list is global, only move to that list if
  509. * no more VMAs exist.
  510. */
  511. spin_lock(&i915->mm.obj_lock);
  512. if (--obj->bind_count == 0)
  513. list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
  514. spin_unlock(&i915->mm.obj_lock);
  515. /* And finally now the object is completely decoupled from this vma,
  516. * we can drop its hold on the backing storage and allow it to be
  517. * reaped by the shrinker.
  518. */
  519. i915_gem_object_unpin_pages(obj);
  520. GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
  521. }
  522. int __i915_vma_do_pin(struct i915_vma *vma,
  523. u64 size, u64 alignment, u64 flags)
  524. {
  525. const unsigned int bound = vma->flags;
  526. int ret;
  527. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  528. GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
  529. GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
  530. if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
  531. ret = -EBUSY;
  532. goto err_unpin;
  533. }
  534. if ((bound & I915_VMA_BIND_MASK) == 0) {
  535. ret = i915_vma_insert(vma, size, alignment, flags);
  536. if (ret)
  537. goto err_unpin;
  538. }
  539. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  540. ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
  541. if (ret)
  542. goto err_remove;
  543. GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
  544. if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
  545. __i915_vma_set_map_and_fenceable(vma);
  546. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  547. return 0;
  548. err_remove:
  549. if ((bound & I915_VMA_BIND_MASK) == 0) {
  550. i915_vma_remove(vma);
  551. GEM_BUG_ON(vma->pages);
  552. GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
  553. }
  554. err_unpin:
  555. __i915_vma_unpin(vma);
  556. return ret;
  557. }
  558. void i915_vma_close(struct i915_vma *vma)
  559. {
  560. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  561. GEM_BUG_ON(i915_vma_is_closed(vma));
  562. vma->flags |= I915_VMA_CLOSED;
  563. /*
  564. * We defer actually closing, unbinding and destroying the VMA until
  565. * the next idle point, or if the object is freed in the meantime. By
  566. * postponing the unbind, we allow for it to be resurrected by the
  567. * client, avoiding the work required to rebind the VMA. This is
  568. * advantageous for DRI, where the client/server pass objects
  569. * between themselves, temporarily opening a local VMA to the
  570. * object, and then closing it again. The same object is then reused
  571. * on the next frame (or two, depending on the depth of the swap queue)
  572. * causing us to rebind the VMA once more. This ends up being a lot
  573. * of wasted work for the steady state.
  574. */
  575. list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
  576. }
  577. void i915_vma_reopen(struct i915_vma *vma)
  578. {
  579. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  580. if (vma->flags & I915_VMA_CLOSED) {
  581. vma->flags &= ~I915_VMA_CLOSED;
  582. list_del(&vma->closed_link);
  583. }
  584. }
  585. static void __i915_vma_destroy(struct i915_vma *vma)
  586. {
  587. int i;
  588. GEM_BUG_ON(vma->node.allocated);
  589. GEM_BUG_ON(vma->fence);
  590. for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
  591. GEM_BUG_ON(i915_gem_active_isset(&vma->last_read[i]));
  592. GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
  593. list_del(&vma->obj_link);
  594. list_del(&vma->vm_link);
  595. rb_erase(&vma->obj_node, &vma->obj->vma_tree);
  596. if (!i915_vma_is_ggtt(vma))
  597. i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
  598. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  599. }
  600. void i915_vma_destroy(struct i915_vma *vma)
  601. {
  602. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  603. GEM_BUG_ON(i915_vma_is_active(vma));
  604. GEM_BUG_ON(i915_vma_is_pinned(vma));
  605. if (i915_vma_is_closed(vma))
  606. list_del(&vma->closed_link);
  607. WARN_ON(i915_vma_unbind(vma));
  608. __i915_vma_destroy(vma);
  609. }
  610. void i915_vma_parked(struct drm_i915_private *i915)
  611. {
  612. struct i915_vma *vma, *next;
  613. list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
  614. GEM_BUG_ON(!i915_vma_is_closed(vma));
  615. i915_vma_destroy(vma);
  616. }
  617. GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
  618. }
  619. static void __i915_vma_iounmap(struct i915_vma *vma)
  620. {
  621. GEM_BUG_ON(i915_vma_is_pinned(vma));
  622. if (vma->iomap == NULL)
  623. return;
  624. io_mapping_unmap(vma->iomap);
  625. vma->iomap = NULL;
  626. }
  627. void i915_vma_revoke_mmap(struct i915_vma *vma)
  628. {
  629. struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
  630. u64 vma_offset;
  631. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  632. if (!i915_vma_has_userfault(vma))
  633. return;
  634. GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
  635. GEM_BUG_ON(!vma->obj->userfault_count);
  636. vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
  637. unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
  638. drm_vma_node_offset_addr(node) + vma_offset,
  639. vma->size,
  640. 1);
  641. i915_vma_unset_userfault(vma);
  642. if (!--vma->obj->userfault_count)
  643. list_del(&vma->obj->userfault_link);
  644. }
  645. int i915_vma_unbind(struct i915_vma *vma)
  646. {
  647. struct drm_i915_gem_object *obj = vma->obj;
  648. unsigned long active;
  649. int ret;
  650. lockdep_assert_held(&obj->base.dev->struct_mutex);
  651. /* First wait upon any activity as retiring the request may
  652. * have side-effects such as unpinning or even unbinding this vma.
  653. */
  654. might_sleep();
  655. active = i915_vma_get_active(vma);
  656. if (active) {
  657. int idx;
  658. /* When a closed VMA is retired, it is unbound - eek.
  659. * In order to prevent it from being recursively closed,
  660. * take a pin on the vma so that the second unbind is
  661. * aborted.
  662. *
  663. * Even more scary is that the retire callback may free
  664. * the object (last active vma). To prevent the explosion
  665. * we defer the actual object free to a worker that can
  666. * only proceed once it acquires the struct_mutex (which
  667. * we currently hold, therefore it cannot free this object
  668. * before we are finished).
  669. */
  670. __i915_vma_pin(vma);
  671. for_each_active(active, idx) {
  672. ret = i915_gem_active_retire(&vma->last_read[idx],
  673. &vma->vm->i915->drm.struct_mutex);
  674. if (ret)
  675. break;
  676. }
  677. if (!ret) {
  678. ret = i915_gem_active_retire(&vma->last_fence,
  679. &vma->vm->i915->drm.struct_mutex);
  680. }
  681. __i915_vma_unpin(vma);
  682. if (ret)
  683. return ret;
  684. }
  685. GEM_BUG_ON(i915_vma_is_active(vma));
  686. if (i915_vma_is_pinned(vma))
  687. return -EBUSY;
  688. if (!drm_mm_node_allocated(&vma->node))
  689. return 0;
  690. GEM_BUG_ON(obj->bind_count == 0);
  691. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  692. if (i915_vma_is_map_and_fenceable(vma)) {
  693. /*
  694. * Check that we have flushed all writes through the GGTT
  695. * before the unbind, other due to non-strict nature of those
  696. * indirect writes they may end up referencing the GGTT PTE
  697. * after the unbind.
  698. */
  699. i915_vma_flush_writes(vma);
  700. GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
  701. /* release the fence reg _after_ flushing */
  702. ret = i915_vma_put_fence(vma);
  703. if (ret)
  704. return ret;
  705. /* Force a pagefault for domain tracking on next user access */
  706. i915_vma_revoke_mmap(vma);
  707. __i915_vma_iounmap(vma);
  708. vma->flags &= ~I915_VMA_CAN_FENCE;
  709. }
  710. GEM_BUG_ON(vma->fence);
  711. GEM_BUG_ON(i915_vma_has_userfault(vma));
  712. if (likely(!vma->vm->closed)) {
  713. trace_i915_vma_unbind(vma);
  714. vma->vm->unbind_vma(vma);
  715. }
  716. vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  717. i915_vma_remove(vma);
  718. return 0;
  719. }
  720. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  721. #include "selftests/i915_vma.c"
  722. #endif