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_display_types.h"
15 #include "intel_dpt.h"
17 #include "intel_fb_pin.h"
19 static struct i915_vma *
20 intel_pin_fb_obj_dpt(struct drm_framebuffer *fb,
21 const struct i915_gtt_view *view,
23 unsigned long *out_flags,
24 struct i915_address_space *vm)
26 struct drm_device *dev = fb->dev;
27 struct drm_i915_private *dev_priv = to_i915(dev);
28 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
29 struct i915_gem_ww_ctx ww;
35 * We are not syncing against the binding (and potential migrations)
36 * below, so this vm must never be async.
38 if (drm_WARN_ON(&dev_priv->drm, vm->bind_async_flags))
39 return ERR_PTR(-EINVAL);
41 if (WARN_ON(!i915_gem_object_is_framebuffer(obj)))
42 return ERR_PTR(-EINVAL);
44 alignment = 4096 * 512;
46 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
48 for_i915_gem_ww(&ww, ret, true) {
49 ret = i915_gem_object_lock(obj, &ww);
53 if (HAS_LMEM(dev_priv)) {
54 unsigned int flags = obj->flags;
57 * For this type of buffer we need to able to read from the CPU
58 * the clear color value found in the buffer, hence we need to
59 * ensure it is always in the mappable part of lmem, if this is
62 if (intel_fb_rc_ccs_cc_plane(fb) >= 0)
63 flags &= ~I915_BO_ALLOC_GPU_ONLY;
64 ret = __i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0,
70 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
74 vma = i915_vma_instance(obj, vm, view);
80 if (i915_vma_misplaced(vma, 0, alignment, 0)) {
81 ret = i915_vma_unbind(vma);
86 ret = i915_vma_pin_ww(vma, &ww, 0, alignment, PIN_GLOBAL);
95 vma->display_alignment = max(vma->display_alignment, alignment);
97 i915_gem_object_flush_if_display(obj);
101 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
107 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
109 const struct i915_gtt_view *view,
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_i915_gem_object *obj = intel_fb_obj(fb);
116 intel_wakeref_t wakeref;
117 struct i915_gem_ww_ctx ww;
118 struct i915_vma *vma;
123 if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
124 return ERR_PTR(-EINVAL);
127 alignment = intel_cursor_alignment(dev_priv);
129 alignment = intel_surf_alignment(fb, 0);
130 if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
131 return ERR_PTR(-EINVAL);
133 /* Note that the w/a also requires 64 PTE of padding following the
134 * bo. We currently fill all unused PTE with the shadow page and so
135 * we should always have valid PTE following the scanout preventing
138 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
139 alignment = 256 * 1024;
142 * Global gtt pte registers are special registers which actually forward
143 * writes to a chunk of system memory. Which means that there is no risk
144 * that the register values disappear as soon as we call
145 * intel_runtime_pm_put(), so it is correct to wrap only the
146 * pin/unpin/fence and not more.
148 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
150 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
153 * Valleyview is definitely limited to scanning out the first
154 * 512MiB. Lets presume this behaviour was inherited from the
155 * g4x display engine and that all earlier gen are similarly
156 * limited. Testing suggests that it is a little more
157 * complicated than this. For example, Cherryview appears quite
158 * happy to scanout from anywhere within its global aperture.
161 if (HAS_GMCH(dev_priv))
162 pinctl |= PIN_MAPPABLE;
164 i915_gem_ww_ctx_init(&ww, true);
166 ret = i915_gem_object_lock(obj, &ww);
167 if (!ret && phys_cursor)
168 ret = i915_gem_object_attach_phys(obj, alignment);
169 else if (!ret && HAS_LMEM(dev_priv))
170 ret = i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0);
172 ret = i915_gem_object_pin_pages(obj);
176 vma = i915_gem_object_pin_to_display_plane(obj, &ww, alignment,
183 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
185 * Install a fence for tiled scan-out. Pre-i965 always needs a
186 * fence, whereas 965+ only requires a fence if using
187 * framebuffer compression. For simplicity, we always, when
188 * possible, install a fence as the cost is not that onerous.
190 * If we fail to fence the tiled scanout, then either the
191 * modeset will reject the change (which is highly unlikely as
192 * the affected systems, all but one, do not have unmappable
193 * space) or we will not be able to enable full powersaving
194 * techniques (also likely not to apply due to various limits
195 * FBC and the like impose on the size of the buffer, which
196 * presumably we violated anyway with this unmappable buffer).
197 * Anyway, it is presumably better to stumble onwards with
198 * something and try to run the system in a "less than optimal"
199 * mode that matches the user configuration.
201 ret = i915_vma_pin_fence(vma);
202 if (ret != 0 && DISPLAY_VER(dev_priv) < 4) {
209 *out_flags |= PLANE_HAS_FENCE;
215 i915_gem_object_unpin_pages(obj);
217 if (ret == -EDEADLK) {
218 ret = i915_gem_ww_ctx_backoff(&ww);
222 i915_gem_ww_ctx_fini(&ww);
226 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
227 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
231 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
233 if (flags & PLANE_HAS_FENCE)
234 i915_vma_unpin_fence(vma);
239 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
241 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
242 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
243 struct drm_framebuffer *fb = plane_state->hw.fb;
244 struct i915_vma *vma;
246 plane->id == PLANE_CURSOR &&
247 DISPLAY_INFO(dev_priv)->cursor_needs_physical;
249 if (!intel_fb_uses_dpt(fb)) {
250 vma = intel_pin_and_fence_fb_obj(fb, phys_cursor,
251 &plane_state->view.gtt,
252 intel_plane_uses_fence(plane_state),
253 &plane_state->flags);
257 plane_state->ggtt_vma = vma;
259 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
261 vma = intel_dpt_pin(intel_fb->dpt_vm);
265 plane_state->ggtt_vma = vma;
267 vma = intel_pin_fb_obj_dpt(fb, &plane_state->view.gtt, false,
268 &plane_state->flags, intel_fb->dpt_vm);
270 intel_dpt_unpin(intel_fb->dpt_vm);
271 plane_state->ggtt_vma = NULL;
275 plane_state->dpt_vma = vma;
277 WARN_ON(plane_state->ggtt_vma == plane_state->dpt_vma);
283 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
285 struct drm_framebuffer *fb = old_plane_state->hw.fb;
286 struct i915_vma *vma;
288 if (!intel_fb_uses_dpt(fb)) {
289 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
291 intel_unpin_fb_vma(vma, old_plane_state->flags);
293 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
295 vma = fetch_and_zero(&old_plane_state->dpt_vma);
297 intel_unpin_fb_vma(vma, old_plane_state->flags);
299 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
301 intel_dpt_unpin(intel_fb->dpt_vm);