1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
7 * DOC: display pinning helpers
10 #include "gem/i915_gem_domain.h"
11 #include "gem/i915_gem_object.h"
14 #include "intel_atomic_plane.h"
15 #include "intel_display_types.h"
16 #include "intel_dpt.h"
18 #include "intel_fb_pin.h"
20 static struct i915_vma *
21 intel_fb_pin_to_dpt(const struct drm_framebuffer *fb,
22 const struct i915_gtt_view *view,
23 unsigned int alignment,
24 unsigned long *out_flags,
25 struct i915_address_space *vm)
27 struct drm_device *dev = fb->dev;
28 struct drm_i915_private *dev_priv = to_i915(dev);
29 struct drm_gem_object *_obj = intel_fb_bo(fb);
30 struct drm_i915_gem_object *obj = to_intel_bo(_obj);
31 struct i915_gem_ww_ctx ww;
36 * We are not syncing against the binding (and potential migrations)
37 * below, so this vm must never be async.
39 if (drm_WARN_ON(&dev_priv->drm, vm->bind_async_flags))
40 return ERR_PTR(-EINVAL);
42 if (WARN_ON(!i915_gem_object_is_framebuffer(obj)))
43 return ERR_PTR(-EINVAL);
45 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
47 for_i915_gem_ww(&ww, ret, true) {
48 ret = i915_gem_object_lock(obj, &ww);
52 if (HAS_LMEM(dev_priv)) {
53 unsigned int flags = obj->flags;
56 * For this type of buffer we need to able to read from the CPU
57 * the clear color value found in the buffer, hence we need to
58 * ensure it is always in the mappable part of lmem, if this is
61 if (intel_fb_rc_ccs_cc_plane(fb) >= 0)
62 flags &= ~I915_BO_ALLOC_GPU_ONLY;
63 ret = __i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0,
69 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
73 vma = i915_vma_instance(obj, vm, view);
79 if (i915_vma_misplaced(vma, 0, alignment, 0)) {
80 ret = i915_vma_unbind(vma);
85 ret = i915_vma_pin_ww(vma, &ww, 0, alignment, PIN_GLOBAL);
94 vma->display_alignment = max(vma->display_alignment, alignment);
96 i915_gem_object_flush_if_display(obj);
100 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
106 intel_fb_pin_to_ggtt(const struct drm_framebuffer *fb,
107 const struct i915_gtt_view *view,
108 unsigned int alignment,
109 unsigned int phys_alignment,
111 unsigned long *out_flags)
113 struct drm_device *dev = fb->dev;
114 struct drm_i915_private *dev_priv = to_i915(dev);
115 struct drm_gem_object *_obj = intel_fb_bo(fb);
116 struct drm_i915_gem_object *obj = to_intel_bo(_obj);
117 intel_wakeref_t wakeref;
118 struct i915_gem_ww_ctx ww;
119 struct i915_vma *vma;
123 if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
124 return ERR_PTR(-EINVAL);
126 if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
127 return ERR_PTR(-EINVAL);
129 /* Note that the w/a also requires 64 PTE of padding following the
130 * bo. We currently fill all unused PTE with the shadow page and so
131 * we should always have valid PTE following the scanout preventing
134 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
135 alignment = 256 * 1024;
138 * Global gtt pte registers are special registers which actually forward
139 * writes to a chunk of system memory. Which means that there is no risk
140 * that the register values disappear as soon as we call
141 * intel_runtime_pm_put(), so it is correct to wrap only the
142 * pin/unpin/fence and not more.
144 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
146 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
149 * Valleyview is definitely limited to scanning out the first
150 * 512MiB. Lets presume this behaviour was inherited from the
151 * g4x display engine and that all earlier gen are similarly
152 * limited. Testing suggests that it is a little more
153 * complicated than this. For example, Cherryview appears quite
154 * happy to scanout from anywhere within its global aperture.
157 if (HAS_GMCH(dev_priv))
158 pinctl |= PIN_MAPPABLE;
160 i915_gem_ww_ctx_init(&ww, true);
162 ret = i915_gem_object_lock(obj, &ww);
163 if (!ret && phys_alignment)
164 ret = i915_gem_object_attach_phys(obj, phys_alignment);
165 else if (!ret && HAS_LMEM(dev_priv))
166 ret = i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0);
168 ret = i915_gem_object_pin_pages(obj);
172 vma = i915_gem_object_pin_to_display_plane(obj, &ww, alignment,
179 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
181 * Install a fence for tiled scan-out. Pre-i965 always needs a
182 * fence, whereas 965+ only requires a fence if using
183 * framebuffer compression. For simplicity, we always, when
184 * possible, install a fence as the cost is not that onerous.
186 * If we fail to fence the tiled scanout, then either the
187 * modeset will reject the change (which is highly unlikely as
188 * the affected systems, all but one, do not have unmappable
189 * space) or we will not be able to enable full powersaving
190 * techniques (also likely not to apply due to various limits
191 * FBC and the like impose on the size of the buffer, which
192 * presumably we violated anyway with this unmappable buffer).
193 * Anyway, it is presumably better to stumble onwards with
194 * something and try to run the system in a "less than optimal"
195 * mode that matches the user configuration.
197 ret = i915_vma_pin_fence(vma);
198 if (ret != 0 && DISPLAY_VER(dev_priv) < 4) {
205 *out_flags |= PLANE_HAS_FENCE;
211 i915_gem_object_unpin_pages(obj);
213 if (ret == -EDEADLK) {
214 ret = i915_gem_ww_ctx_backoff(&ww);
218 i915_gem_ww_ctx_fini(&ww);
222 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
223 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
227 void intel_fb_unpin_vma(struct i915_vma *vma, unsigned long flags)
229 if (flags & PLANE_HAS_FENCE)
230 i915_vma_unpin_fence(vma);
236 intel_plane_fb_min_alignment(const struct intel_plane_state *plane_state)
238 const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
240 return fb->min_alignment;
244 intel_plane_fb_min_phys_alignment(const struct intel_plane_state *plane_state)
246 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
247 const struct drm_framebuffer *fb = plane_state->hw.fb;
249 if (!intel_plane_needs_physical(plane))
252 return plane->min_alignment(plane, fb, 0);
255 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
257 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
258 const struct intel_framebuffer *fb =
259 to_intel_framebuffer(plane_state->hw.fb);
260 struct i915_vma *vma;
262 if (!intel_fb_uses_dpt(&fb->base)) {
263 vma = intel_fb_pin_to_ggtt(&fb->base, &plane_state->view.gtt,
264 intel_plane_fb_min_alignment(plane_state),
265 intel_plane_fb_min_phys_alignment(plane_state),
266 intel_plane_uses_fence(plane_state),
267 &plane_state->flags);
271 plane_state->ggtt_vma = vma;
274 * Pre-populate the dma address before we enter the vblank
275 * evade critical section as i915_gem_object_get_dma_address()
276 * will trigger might_sleep() even if it won't actually sleep,
277 * which is the case when the fb has already been pinned.
279 if (intel_plane_needs_physical(plane)) {
280 struct drm_i915_gem_object *obj = to_intel_bo(intel_fb_bo(&fb->base));
282 plane_state->phys_dma_addr = i915_gem_object_get_dma_address(obj, 0);
285 unsigned int alignment = intel_plane_fb_min_alignment(plane_state);
287 vma = intel_dpt_pin_to_ggtt(fb->dpt_vm, alignment / 512);
291 plane_state->ggtt_vma = vma;
293 vma = intel_fb_pin_to_dpt(&fb->base, &plane_state->view.gtt,
294 alignment, &plane_state->flags,
297 intel_dpt_unpin_from_ggtt(fb->dpt_vm);
298 plane_state->ggtt_vma = NULL;
302 plane_state->dpt_vma = vma;
304 WARN_ON(plane_state->ggtt_vma == plane_state->dpt_vma);
310 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
312 const struct intel_framebuffer *fb =
313 to_intel_framebuffer(old_plane_state->hw.fb);
314 struct i915_vma *vma;
316 if (!intel_fb_uses_dpt(&fb->base)) {
317 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
319 intel_fb_unpin_vma(vma, old_plane_state->flags);
321 vma = fetch_and_zero(&old_plane_state->dpt_vma);
323 intel_fb_unpin_vma(vma, old_plane_state->flags);
325 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
327 intel_dpt_unpin_from_ggtt(fb->dpt_vm);