i915_vma.c 22 KB

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