1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include <drm/drm_crtc.h> 8 #include <drm/drm_damage_helper.h> 9 #include <drm/drm_file.h> 10 #include <drm/drm_fourcc.h> 11 #include <drm/drm_framebuffer.h> 12 #include <drm/drm_gem_framebuffer_helper.h> 13 #include <drm/drm_probe_helper.h> 14 15 #include "msm_drv.h" 16 #include "msm_kms.h" 17 #include "msm_gem.h" 18 19 struct msm_framebuffer { 20 struct drm_framebuffer base; 21 const struct msm_format *format; 22 23 /* Count of # of attached planes which need dirtyfb: */ 24 refcount_t dirtyfb; 25 26 /* Framebuffer per-plane address, if pinned, else zero: */ 27 uint64_t iova[DRM_FORMAT_MAX_PLANES]; 28 atomic_t prepare_count; 29 }; 30 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base) 31 32 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev, 33 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos); 34 35 static int msm_framebuffer_dirtyfb(struct drm_framebuffer *fb, 36 struct drm_file *file_priv, unsigned int flags, 37 unsigned int color, struct drm_clip_rect *clips, 38 unsigned int num_clips) 39 { 40 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); 41 42 /* If this fb is not used on any display requiring pixel data to be 43 * flushed, then skip dirtyfb 44 */ 45 if (refcount_read(&msm_fb->dirtyfb) == 1) 46 return 0; 47 48 return drm_atomic_helper_dirtyfb(fb, file_priv, flags, color, 49 clips, num_clips); 50 } 51 52 static const struct drm_framebuffer_funcs msm_framebuffer_funcs = { 53 .create_handle = drm_gem_fb_create_handle, 54 .destroy = drm_gem_fb_destroy, 55 .dirty = msm_framebuffer_dirtyfb, 56 }; 57 58 #ifdef CONFIG_DEBUG_FS 59 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m) 60 { 61 struct msm_gem_stats stats = {}; 62 int i, n = fb->format->num_planes; 63 64 seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n", 65 fb->width, fb->height, (char *)&fb->format->format, 66 drm_framebuffer_read_refcount(fb), fb->base.id); 67 68 for (i = 0; i < n; i++) { 69 seq_printf(m, " %d: offset=%d pitch=%d, obj: ", 70 i, fb->offsets[i], fb->pitches[i]); 71 msm_gem_describe(fb->obj[i], m, &stats); 72 } 73 } 74 #endif 75 76 /* prepare/pin all the fb's bo's for scanout. 77 */ 78 int msm_framebuffer_prepare(struct drm_framebuffer *fb, 79 struct msm_gem_address_space *aspace, 80 bool needs_dirtyfb) 81 { 82 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); 83 int ret, i, n = fb->format->num_planes; 84 85 if (needs_dirtyfb) 86 refcount_inc(&msm_fb->dirtyfb); 87 88 atomic_inc(&msm_fb->prepare_count); 89 90 for (i = 0; i < n; i++) { 91 ret = msm_gem_get_and_pin_iova(fb->obj[i], aspace, &msm_fb->iova[i]); 92 drm_dbg_state(fb->dev, "FB[%u]: iova[%d]: %08llx (%d)\n", 93 fb->base.id, i, msm_fb->iova[i], ret); 94 if (ret) 95 return ret; 96 } 97 98 return 0; 99 } 100 101 void msm_framebuffer_cleanup(struct drm_framebuffer *fb, 102 struct msm_gem_address_space *aspace, 103 bool needed_dirtyfb) 104 { 105 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); 106 int i, n = fb->format->num_planes; 107 108 if (needed_dirtyfb) 109 refcount_dec(&msm_fb->dirtyfb); 110 111 for (i = 0; i < n; i++) 112 msm_gem_unpin_iova(fb->obj[i], aspace); 113 114 if (!atomic_dec_return(&msm_fb->prepare_count)) 115 memset(msm_fb->iova, 0, sizeof(msm_fb->iova)); 116 } 117 118 uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb, 119 struct msm_gem_address_space *aspace, int plane) 120 { 121 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); 122 return msm_fb->iova[plane] + fb->offsets[plane]; 123 } 124 125 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane) 126 { 127 return drm_gem_fb_get_obj(fb, plane); 128 } 129 130 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb) 131 { 132 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); 133 return msm_fb->format; 134 } 135 136 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev, 137 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd) 138 { 139 const struct drm_format_info *info = drm_get_format_info(dev, 140 mode_cmd); 141 struct drm_gem_object *bos[4] = {0}; 142 struct drm_framebuffer *fb; 143 int ret, i, n = info->num_planes; 144 145 for (i = 0; i < n; i++) { 146 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); 147 if (!bos[i]) { 148 ret = -ENXIO; 149 goto out_unref; 150 } 151 } 152 153 fb = msm_framebuffer_init(dev, mode_cmd, bos); 154 if (IS_ERR(fb)) { 155 ret = PTR_ERR(fb); 156 goto out_unref; 157 } 158 159 return fb; 160 161 out_unref: 162 for (i = 0; i < n; i++) 163 drm_gem_object_put(bos[i]); 164 return ERR_PTR(ret); 165 } 166 167 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev, 168 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos) 169 { 170 const struct drm_format_info *info = drm_get_format_info(dev, 171 mode_cmd); 172 struct msm_drm_private *priv = dev->dev_private; 173 struct msm_kms *kms = priv->kms; 174 struct msm_framebuffer *msm_fb = NULL; 175 struct drm_framebuffer *fb; 176 const struct msm_format *format; 177 int ret, i, n; 178 179 drm_dbg_state(dev, "create framebuffer: mode_cmd=%p (%dx%d@%p4cc)\n", 180 mode_cmd, mode_cmd->width, mode_cmd->height, 181 &mode_cmd->pixel_format); 182 183 n = info->num_planes; 184 format = mdp_get_format(kms, mode_cmd->pixel_format, 185 mode_cmd->modifier[0]); 186 if (!format) { 187 DRM_DEV_ERROR(dev->dev, "unsupported pixel format: %p4cc\n", 188 &mode_cmd->pixel_format); 189 ret = -EINVAL; 190 goto fail; 191 } 192 193 msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL); 194 if (!msm_fb) { 195 ret = -ENOMEM; 196 goto fail; 197 } 198 199 fb = &msm_fb->base; 200 201 msm_fb->format = format; 202 203 if (n > ARRAY_SIZE(fb->obj)) { 204 ret = -EINVAL; 205 goto fail; 206 } 207 208 for (i = 0; i < n; i++) { 209 unsigned int width = mode_cmd->width / (i ? info->hsub : 1); 210 unsigned int height = mode_cmd->height / (i ? info->vsub : 1); 211 unsigned int min_size; 212 213 min_size = (height - 1) * mode_cmd->pitches[i] 214 + width * info->cpp[i] 215 + mode_cmd->offsets[i]; 216 217 if (bos[i]->size < min_size) { 218 ret = -EINVAL; 219 goto fail; 220 } 221 222 msm_fb->base.obj[i] = bos[i]; 223 } 224 225 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); 226 227 ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs); 228 if (ret) { 229 DRM_DEV_ERROR(dev->dev, "framebuffer init failed: %d\n", ret); 230 goto fail; 231 } 232 233 refcount_set(&msm_fb->dirtyfb, 1); 234 235 drm_dbg_state(dev, "create: FB ID: %d (%p)\n", fb->base.id, fb); 236 237 return fb; 238 239 fail: 240 kfree(msm_fb); 241 242 return ERR_PTR(ret); 243 } 244 245 struct drm_framebuffer * 246 msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format) 247 { 248 struct drm_mode_fb_cmd2 mode_cmd = { 249 .pixel_format = format, 250 .width = w, 251 .height = h, 252 .pitches = { p }, 253 }; 254 struct drm_gem_object *bo; 255 struct drm_framebuffer *fb; 256 int size; 257 258 /* allocate backing bo */ 259 size = mode_cmd.pitches[0] * mode_cmd.height; 260 DBG("allocating %d bytes for fb %d", size, dev->primary->index); 261 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN); 262 if (IS_ERR(bo)) { 263 dev_warn(dev->dev, "could not allocate stolen bo\n"); 264 /* try regular bo: */ 265 bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC); 266 } 267 if (IS_ERR(bo)) { 268 DRM_DEV_ERROR(dev->dev, "failed to allocate buffer object\n"); 269 return ERR_CAST(bo); 270 } 271 272 msm_gem_object_set_name(bo, "stolenfb"); 273 274 fb = msm_framebuffer_init(dev, &mode_cmd, &bo); 275 if (IS_ERR(fb)) { 276 DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n"); 277 /* note: if fb creation failed, we can't rely on fb destroy 278 * to unref the bo: 279 */ 280 drm_gem_object_put(bo); 281 return ERR_CAST(fb); 282 } 283 284 return fb; 285 } 286