1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
7 * DOC: display pinning helpers
10 #include "intel_display_types.h"
11 #include "intel_fb_pin.h"
14 #include "intel_dpt.h"
16 #include "gem/i915_gem_object.h"
18 static struct i915_vma *
19 intel_pin_fb_obj_dpt(struct drm_framebuffer *fb,
20 const struct i915_ggtt_view *view,
22 unsigned long *out_flags,
23 struct i915_address_space *vm)
25 struct drm_device *dev = fb->dev;
26 struct drm_i915_private *dev_priv = to_i915(dev);
27 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
32 if (WARN_ON(!i915_gem_object_is_framebuffer(obj)))
33 return ERR_PTR(-EINVAL);
35 alignment = 4096 * 512;
37 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
39 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
45 vma = i915_vma_instance(obj, vm, view);
49 if (i915_vma_misplaced(vma, 0, alignment, 0)) {
50 ret = i915_vma_unbind(vma);
57 ret = i915_vma_pin(vma, 0, alignment, PIN_GLOBAL);
63 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
65 i915_gem_object_flush_if_display(obj);
69 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
75 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
77 const struct i915_ggtt_view *view,
79 unsigned long *out_flags)
81 struct drm_device *dev = fb->dev;
82 struct drm_i915_private *dev_priv = to_i915(dev);
83 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
84 intel_wakeref_t wakeref;
85 struct i915_gem_ww_ctx ww;
91 if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
92 return ERR_PTR(-EINVAL);
95 alignment = intel_cursor_alignment(dev_priv);
97 alignment = intel_surf_alignment(fb, 0);
98 if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
99 return ERR_PTR(-EINVAL);
101 /* Note that the w/a also requires 64 PTE of padding following the
102 * bo. We currently fill all unused PTE with the shadow page and so
103 * we should always have valid PTE following the scanout preventing
106 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
107 alignment = 256 * 1024;
110 * Global gtt pte registers are special registers which actually forward
111 * writes to a chunk of system memory. Which means that there is no risk
112 * that the register values disappear as soon as we call
113 * intel_runtime_pm_put(), so it is correct to wrap only the
114 * pin/unpin/fence and not more.
116 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
118 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
121 * Valleyview is definitely limited to scanning out the first
122 * 512MiB. Lets presume this behaviour was inherited from the
123 * g4x display engine and that all earlier gen are similarly
124 * limited. Testing suggests that it is a little more
125 * complicated than this. For example, Cherryview appears quite
126 * happy to scanout from anywhere within its global aperture.
129 if (HAS_GMCH(dev_priv))
130 pinctl |= PIN_MAPPABLE;
132 i915_gem_ww_ctx_init(&ww, true);
134 ret = i915_gem_object_lock(obj, &ww);
135 if (!ret && phys_cursor)
136 ret = i915_gem_object_attach_phys(obj, alignment);
137 else if (!ret && HAS_LMEM(dev_priv))
138 ret = i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM);
139 /* TODO: Do we need to sync when migration becomes async? */
141 ret = i915_gem_object_pin_pages(obj);
146 vma = i915_gem_object_pin_to_display_plane(obj, &ww, alignment,
154 if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
156 * Install a fence for tiled scan-out. Pre-i965 always needs a
157 * fence, whereas 965+ only requires a fence if using
158 * framebuffer compression. For simplicity, we always, when
159 * possible, install a fence as the cost is not that onerous.
161 * If we fail to fence the tiled scanout, then either the
162 * modeset will reject the change (which is highly unlikely as
163 * the affected systems, all but one, do not have unmappable
164 * space) or we will not be able to enable full powersaving
165 * techniques (also likely not to apply due to various limits
166 * FBC and the like impose on the size of the buffer, which
167 * presumably we violated anyway with this unmappable buffer).
168 * Anyway, it is presumably better to stumble onwards with
169 * something and try to run the system in a "less than optimal"
170 * mode that matches the user configuration.
172 ret = i915_vma_pin_fence(vma);
173 if (ret != 0 && DISPLAY_VER(dev_priv) < 4) {
180 *out_flags |= PLANE_HAS_FENCE;
186 i915_gem_object_unpin_pages(obj);
188 if (ret == -EDEADLK) {
189 ret = i915_gem_ww_ctx_backoff(&ww);
193 i915_gem_ww_ctx_fini(&ww);
197 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
198 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
202 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
204 if (flags & PLANE_HAS_FENCE)
205 i915_vma_unpin_fence(vma);
210 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
212 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
213 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
214 struct drm_framebuffer *fb = plane_state->hw.fb;
215 struct i915_vma *vma;
217 plane->id == PLANE_CURSOR &&
218 INTEL_INFO(dev_priv)->display.cursor_needs_physical;
220 if (!intel_fb_uses_dpt(fb)) {
221 vma = intel_pin_and_fence_fb_obj(fb, phys_cursor,
222 &plane_state->view.gtt,
223 intel_plane_uses_fence(plane_state),
224 &plane_state->flags);
228 plane_state->ggtt_vma = vma;
230 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
232 vma = intel_dpt_pin(intel_fb->dpt_vm);
236 plane_state->ggtt_vma = vma;
238 vma = intel_pin_fb_obj_dpt(fb, &plane_state->view.gtt, false,
239 &plane_state->flags, intel_fb->dpt_vm);
241 intel_dpt_unpin(intel_fb->dpt_vm);
242 plane_state->ggtt_vma = NULL;
246 plane_state->dpt_vma = vma;
248 WARN_ON(plane_state->ggtt_vma == plane_state->dpt_vma);
254 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
256 struct drm_framebuffer *fb = old_plane_state->hw.fb;
257 struct i915_vma *vma;
259 if (!intel_fb_uses_dpt(fb)) {
260 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
262 intel_unpin_fb_vma(vma, old_plane_state->flags);
264 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
266 vma = fetch_and_zero(&old_plane_state->dpt_vma);
268 intel_unpin_fb_vma(vma, old_plane_state->flags);
270 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
272 intel_dpt_unpin(intel_fb->dpt_vm);