1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include "gem/i915_gem_domain.h"
7 #include "gem/i915_gem_internal.h"
8 #include "gem/i915_gem_lmem.h"
9 #include "gt/gen8_ppgtt.h"
14 #include "intel_display_types.h"
15 #include "intel_dpt.h"
19 struct i915_address_space vm;
21 struct drm_i915_gem_object *obj;
26 #define i915_is_dpt(vm) ((vm)->is_dpt)
28 static inline struct i915_dpt *
29 i915_vm_to_dpt(struct i915_address_space *vm)
31 BUILD_BUG_ON(offsetof(struct i915_dpt, vm));
32 drm_WARN_ON(&vm->i915->drm, !i915_is_dpt(vm));
33 return container_of(vm, struct i915_dpt, vm);
36 #define dpt_total_entries(dpt) ((dpt)->vm.total >> PAGE_SHIFT)
38 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
43 static void dpt_insert_page(struct i915_address_space *vm,
46 unsigned int pat_index,
49 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
50 gen8_pte_t __iomem *base = dpt->iomem;
52 gen8_set_pte(base + offset / I915_GTT_PAGE_SIZE,
53 vm->pte_encode(addr, pat_index, flags));
56 static void dpt_insert_entries(struct i915_address_space *vm,
57 struct i915_vma_resource *vma_res,
58 unsigned int pat_index,
61 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
62 gen8_pte_t __iomem *base = dpt->iomem;
63 const gen8_pte_t pte_encode = vm->pte_encode(0, pat_index, flags);
64 struct sgt_iter sgt_iter;
69 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
70 * not to allow the user to override access to a read only page.
73 i = vma_res->start / I915_GTT_PAGE_SIZE;
74 for_each_sgt_daddr(addr, sgt_iter, vma_res->bi.pages)
75 gen8_set_pte(&base[i++], pte_encode | addr);
78 static void dpt_clear_range(struct i915_address_space *vm,
79 u64 start, u64 length)
83 static void dpt_bind_vma(struct i915_address_space *vm,
84 struct i915_vm_pt_stash *stash,
85 struct i915_vma_resource *vma_res,
86 unsigned int pat_index,
91 if (vma_res->bound_flags)
94 /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
96 if (vm->has_read_only && vma_res->bi.readonly)
97 pte_flags |= PTE_READ_ONLY;
101 vm->insert_entries(vm, vma_res, pat_index, pte_flags);
103 vma_res->page_sizes_gtt = I915_GTT_PAGE_SIZE;
106 * Without aliasing PPGTT there's no difference between
107 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
108 * upgrade to both bound if we bind either to avoid double-binding.
110 vma_res->bound_flags = I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
113 static void dpt_unbind_vma(struct i915_address_space *vm,
114 struct i915_vma_resource *vma_res)
116 vm->clear_range(vm, vma_res->start, vma_res->vma_size);
119 static void dpt_cleanup(struct i915_address_space *vm)
121 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
123 i915_gem_object_put(dpt->obj);
126 struct i915_vma *intel_dpt_pin(struct i915_address_space *vm)
128 struct drm_i915_private *i915 = vm->i915;
129 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
130 intel_wakeref_t wakeref;
131 struct i915_vma *vma;
133 struct i915_gem_ww_ctx ww;
137 if (i915_gem_object_is_stolen(dpt->obj))
138 pin_flags |= PIN_MAPPABLE;
140 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
141 atomic_inc(&i915->gpu_error.pending_fb_pin);
143 for_i915_gem_ww(&ww, err, true) {
144 err = i915_gem_object_lock(dpt->obj, &ww);
148 vma = i915_gem_object_ggtt_pin_ww(dpt->obj, &ww, NULL, 0, 4096,
155 iomem = i915_vma_pin_iomap(vma);
159 err = PTR_ERR(iomem);
169 dpt->obj->mm.dirty = true;
171 atomic_dec(&i915->gpu_error.pending_fb_pin);
172 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
174 return err ? ERR_PTR(err) : vma;
177 void intel_dpt_unpin(struct i915_address_space *vm)
179 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
181 i915_vma_unpin_iomap(dpt->vma);
182 i915_vma_put(dpt->vma);
186 * intel_dpt_resume - restore the memory mapping for all DPT FBs during system resume
187 * @i915: device instance
189 * Restore the memory mapping during system resume for all framebuffers which
190 * are mapped to HW via a GGTT->DPT page table. The content of these page
191 * tables are not stored in the hibernation image during S4 and S3RST->S4
192 * transitions, so here we reprogram the PTE entries in those tables.
194 * This function must be called after the mappings in GGTT have been restored calling
195 * i915_ggtt_resume().
197 void intel_dpt_resume(struct drm_i915_private *i915)
199 struct drm_framebuffer *drm_fb;
201 if (!HAS_DISPLAY(i915))
204 mutex_lock(&i915->drm.mode_config.fb_lock);
205 drm_for_each_fb(drm_fb, &i915->drm) {
206 struct intel_framebuffer *fb = to_intel_framebuffer(drm_fb);
209 i915_ggtt_resume_vm(fb->dpt_vm);
211 mutex_unlock(&i915->drm.mode_config.fb_lock);
215 * intel_dpt_suspend - suspend the memory mapping for all DPT FBs during system suspend
216 * @i915: device instance
218 * Suspend the memory mapping during system suspend for all framebuffers which
219 * are mapped to HW via a GGTT->DPT page table.
221 * This function must be called before the mappings in GGTT are suspended calling
222 * i915_ggtt_suspend().
224 void intel_dpt_suspend(struct drm_i915_private *i915)
226 struct drm_framebuffer *drm_fb;
228 if (!HAS_DISPLAY(i915))
231 mutex_lock(&i915->drm.mode_config.fb_lock);
233 drm_for_each_fb(drm_fb, &i915->drm) {
234 struct intel_framebuffer *fb = to_intel_framebuffer(drm_fb);
237 i915_ggtt_suspend_vm(fb->dpt_vm);
240 mutex_unlock(&i915->drm.mode_config.fb_lock);
243 struct i915_address_space *
244 intel_dpt_create(struct intel_framebuffer *fb)
246 struct drm_gem_object *obj = &intel_fb_obj(&fb->base)->base;
247 struct drm_i915_private *i915 = to_i915(obj->dev);
248 struct drm_i915_gem_object *dpt_obj;
249 struct i915_address_space *vm;
250 struct i915_dpt *dpt;
254 if (intel_fb_needs_pot_stride_remap(fb))
255 size = intel_remapped_info_size(&fb->remapped_view.gtt.remapped);
257 size = DIV_ROUND_UP_ULL(obj->size, I915_GTT_PAGE_SIZE);
259 size = round_up(size * sizeof(gen8_pte_t), I915_GTT_PAGE_SIZE);
261 dpt_obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_CONTIGUOUS);
262 if (IS_ERR(dpt_obj) && i915_ggtt_has_aperture(to_gt(i915)->ggtt))
263 dpt_obj = i915_gem_object_create_stolen(i915, size);
264 if (IS_ERR(dpt_obj) && !HAS_LMEM(i915)) {
265 drm_dbg_kms(&i915->drm, "Allocating dpt from smem\n");
266 dpt_obj = i915_gem_object_create_shmem(i915, size);
269 return ERR_CAST(dpt_obj);
271 ret = i915_gem_object_lock_interruptible(dpt_obj, NULL);
273 ret = i915_gem_object_set_cache_level(dpt_obj, I915_CACHE_NONE);
274 i915_gem_object_unlock(dpt_obj);
277 i915_gem_object_put(dpt_obj);
281 dpt = kzalloc(sizeof(*dpt), GFP_KERNEL);
283 i915_gem_object_put(dpt_obj);
284 return ERR_PTR(-ENOMEM);
289 vm->gt = to_gt(i915);
291 vm->dma = i915->drm.dev;
292 vm->total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
295 i915_address_space_init(vm, VM_CLASS_DPT);
297 vm->insert_page = dpt_insert_page;
298 vm->clear_range = dpt_clear_range;
299 vm->insert_entries = dpt_insert_entries;
300 vm->cleanup = dpt_cleanup;
302 vm->vma_ops.bind_vma = dpt_bind_vma;
303 vm->vma_ops.unbind_vma = dpt_unbind_vma;
305 vm->pte_encode = vm->gt->ggtt->vm.pte_encode;
308 dpt->obj->is_dpt = true;
313 void intel_dpt_destroy(struct i915_address_space *vm)
315 struct i915_dpt *dpt = i915_vm_to_dpt(vm);
317 dpt->obj->is_dpt = false;
318 i915_vm_put(&dpt->vm);
321 void intel_dpt_configure(struct intel_crtc *crtc)
323 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
325 if (DISPLAY_VER(i915) == 14) {
326 enum pipe pipe = crtc->pipe;
327 enum plane_id plane_id;
329 for_each_plane_id_on_crtc(crtc, plane_id) {
330 if (plane_id == PLANE_CURSOR)
333 intel_de_rmw(i915, PLANE_CHICKEN(pipe, plane_id),
334 PLANE_CHICKEN_DISABLE_DPT,
335 i915->params.enable_dpt ? 0 : PLANE_CHICKEN_DISABLE_DPT);
337 } else if (DISPLAY_VER(i915) == 13) {
338 intel_de_rmw(i915, CHICKEN_MISC_2,
339 CHICKEN_MISC_DISABLE_DPT,
340 i915->params.enable_dpt ? 0 : CHICKEN_MISC_DISABLE_DPT);