1 // SPDX-License-Identifier: MIT 2 /* Copyright © 2024 Intel Corporation */ 3 4 #include <linux/fb.h> 5 6 #include <drm/drm_panic.h> 7 #include <drm/drm_print.h> 8 #include <drm/intel/display_parent_interface.h> 9 10 #include "display/intel_fb.h" 11 #include "gem/i915_gem_lmem.h" 12 #include "gem/i915_gem_mman.h" 13 #include "gem/i915_gem_object.h" 14 #include "gem/i915_gem_object_frontbuffer.h" 15 #include "pxp/intel_pxp.h" 16 17 #include "i915_bo.h" 18 #include "i915_debugfs.h" 19 #include "i915_drv.h" 20 21 static bool i915_bo_is_tiled(struct drm_gem_object *obj) 22 { 23 return i915_gem_object_is_tiled(to_intel_bo(obj)); 24 } 25 26 static bool i915_bo_is_userptr(struct drm_gem_object *obj) 27 { 28 return i915_gem_object_is_userptr(to_intel_bo(obj)); 29 } 30 31 static bool i915_bo_is_shmem(struct drm_gem_object *obj) 32 { 33 return i915_gem_object_is_shmem(to_intel_bo(obj)); 34 } 35 36 static bool i915_bo_is_protected(struct drm_gem_object *obj) 37 { 38 return i915_gem_object_is_protected(to_intel_bo(obj)); 39 } 40 41 static int i915_bo_key_check(struct drm_gem_object *obj) 42 { 43 return intel_pxp_key_check(obj, false); 44 } 45 46 static int i915_bo_fb_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 47 { 48 return i915_gem_fb_mmap(to_intel_bo(obj), vma); 49 } 50 51 static int i915_bo_read_from_page(struct drm_gem_object *obj, u64 offset, void *dst, int size) 52 { 53 return i915_gem_object_read_from_page(to_intel_bo(obj), offset, dst, size); 54 } 55 56 static void i915_bo_describe(struct seq_file *m, struct drm_gem_object *obj) 57 { 58 i915_debugfs_describe_obj(m, to_intel_bo(obj)); 59 } 60 61 static int i915_bo_framebuffer_init(struct drm_gem_object *_obj, 62 struct drm_mode_fb_cmd2 *mode_cmd) 63 { 64 struct drm_i915_gem_object *obj = to_intel_bo(_obj); 65 struct drm_i915_private *i915 = to_i915(obj->base.dev); 66 unsigned int tiling, stride; 67 68 i915_gem_object_lock(obj, NULL); 69 tiling = i915_gem_object_get_tiling(obj); 70 stride = i915_gem_object_get_stride(obj); 71 i915_gem_object_unlock(obj); 72 73 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) { 74 /* 75 * If there's a fence, enforce that 76 * the fb modifier and tiling mode match. 77 */ 78 if (tiling != I915_TILING_NONE && 79 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) { 80 drm_dbg_kms(&i915->drm, 81 "tiling_mode doesn't match fb modifier\n"); 82 return -EINVAL; 83 } 84 } else { 85 if (tiling == I915_TILING_X) { 86 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED; 87 } else if (tiling == I915_TILING_Y) { 88 drm_dbg_kms(&i915->drm, 89 "No Y tiling for legacy addfb\n"); 90 return -EINVAL; 91 } 92 } 93 94 /* 95 * gen2/3 display engine uses the fence if present, 96 * so the tiling mode must match the fb modifier exactly. 97 */ 98 if (GRAPHICS_VER(i915) < 4 && 99 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) { 100 drm_dbg_kms(&i915->drm, 101 "tiling_mode must match fb modifier exactly on gen2/3\n"); 102 return -EINVAL; 103 } 104 105 /* 106 * If there's a fence, enforce that 107 * the fb pitch and fence stride match. 108 */ 109 if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) { 110 drm_dbg_kms(&i915->drm, 111 "pitch (%d) must match tiling stride (%d)\n", 112 mode_cmd->pitches[0], stride); 113 return -EINVAL; 114 } 115 116 return 0; 117 } 118 119 static void i915_bo_framebuffer_fini(struct drm_gem_object *obj) 120 { 121 /* Nothing to do for i915 */ 122 } 123 124 static struct drm_gem_object * 125 i915_bo_framebuffer_lookup(struct drm_device *drm, 126 struct drm_file *filp, 127 const struct drm_mode_fb_cmd2 *mode_cmd) 128 { 129 struct drm_i915_private *i915 = to_i915(drm); 130 struct drm_i915_gem_object *obj; 131 132 obj = i915_gem_object_lookup(filp, mode_cmd->handles[0]); 133 if (!obj) 134 return ERR_PTR(-ENOENT); 135 136 /* object is backed with LMEM for discrete */ 137 if (HAS_LMEM(i915) && !i915_gem_object_can_migrate(obj, INTEL_REGION_LMEM_0)) { 138 /* object is "remote", not in local memory */ 139 i915_gem_object_put(obj); 140 drm_dbg_kms(&i915->drm, "framebuffer must reside in local memory\n"); 141 return ERR_PTR(-EREMOTE); 142 } 143 144 return intel_bo_to_drm_bo(obj); 145 } 146 147 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) 148 static u32 i915_bo_fbdev_pitch_align(u32 stride) 149 { 150 return ALIGN(stride, 64); 151 } 152 153 bool i915_bo_fbdev_prefer_stolen(struct drm_i915_private *i915, unsigned int size) 154 { 155 /* Skip stolen on MTL as Wa_22018444074 mitigation. */ 156 if (IS_METEORLAKE(i915)) 157 return false; 158 159 /* 160 * If the FB is too big, just don't use it since fbdev is not very 161 * important and we should probably use that space with FBC or other 162 * features. 163 */ 164 return i915->dsm.usable_size >= size * 2; 165 } 166 167 static struct drm_gem_object *i915_bo_fbdev_create(struct drm_device *drm, int size) 168 { 169 struct drm_i915_private *i915 = to_i915(drm); 170 struct drm_i915_gem_object *obj; 171 172 obj = ERR_PTR(-ENODEV); 173 if (HAS_LMEM(i915)) { 174 obj = i915_gem_object_create_lmem(i915, size, 175 I915_BO_ALLOC_CONTIGUOUS | 176 I915_BO_ALLOC_USER); 177 } else { 178 if (i915_bo_fbdev_prefer_stolen(i915, size)) 179 obj = i915_gem_object_create_stolen(i915, size); 180 else 181 drm_info(drm, "Allocating fbdev: Stolen memory not preferred.\n"); 182 183 if (IS_ERR(obj)) 184 obj = i915_gem_object_create_shmem(i915, size); 185 } 186 187 if (IS_ERR(obj)) { 188 drm_err(drm, "failed to allocate framebuffer (%pe)\n", obj); 189 return ERR_PTR(-ENOMEM); 190 } 191 192 return &obj->base; 193 } 194 195 static void i915_bo_fbdev_destroy(struct drm_gem_object *obj) 196 { 197 drm_gem_object_put(obj); 198 } 199 200 static int i915_bo_fbdev_fill_info(struct drm_gem_object *_obj, struct fb_info *info, 201 struct i915_vma *vma) 202 { 203 struct drm_i915_private *i915 = to_i915(_obj->dev); 204 struct drm_i915_gem_object *obj = to_intel_bo(_obj); 205 struct i915_gem_ww_ctx ww; 206 void __iomem *vaddr; 207 int ret; 208 209 if (i915_gem_object_is_lmem(obj)) { 210 struct intel_memory_region *mem = obj->mm.region; 211 212 /* Use fbdev's framebuffer from lmem for discrete */ 213 info->fix.smem_start = 214 (unsigned long)(mem->io.start + 215 i915_gem_object_get_dma_address(obj, 0) - 216 mem->region.start); 217 info->fix.smem_len = obj->base.size; 218 } else { 219 struct i915_ggtt *ggtt = to_gt(i915)->ggtt; 220 221 /* Our framebuffer is the entirety of fbdev's system memory */ 222 info->fix.smem_start = 223 (unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma)); 224 info->fix.smem_len = vma->size; 225 } 226 227 for_i915_gem_ww(&ww, ret, false) { 228 ret = i915_gem_object_lock(vma->obj, &ww); 229 230 if (ret) 231 continue; 232 233 vaddr = i915_vma_pin_iomap(vma); 234 if (IS_ERR(vaddr)) { 235 drm_err(&i915->drm, 236 "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr); 237 ret = PTR_ERR(vaddr); 238 continue; 239 } 240 } 241 242 if (ret) 243 return ret; 244 245 info->screen_base = vaddr; 246 info->screen_size = intel_bo_to_drm_bo(obj)->size; 247 248 return 0; 249 } 250 #endif 251 252 const struct intel_display_bo_interface i915_display_bo_interface = { 253 .is_tiled = i915_bo_is_tiled, 254 .is_userptr = i915_bo_is_userptr, 255 .is_shmem = i915_bo_is_shmem, 256 .is_protected = i915_bo_is_protected, 257 .key_check = i915_bo_key_check, 258 .fb_mmap = i915_bo_fb_mmap, 259 .read_from_page = i915_bo_read_from_page, 260 .describe = i915_bo_describe, 261 .framebuffer_init = i915_bo_framebuffer_init, 262 .framebuffer_fini = i915_bo_framebuffer_fini, 263 .framebuffer_lookup = i915_bo_framebuffer_lookup, 264 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) 265 .fbdev_create = i915_bo_fbdev_create, 266 .fbdev_destroy = i915_bo_fbdev_destroy, 267 .fbdev_fill_info = i915_bo_fbdev_fill_info, 268 .fbdev_pitch_align = i915_bo_fbdev_pitch_align, 269 #endif 270 }; 271