i915_vma.c 22 KB

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