i915_vma.c 22 KB

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