1 /* 2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd 3 * Author:Mark Yao <mark.yao@rock-chips.com> 4 * 5 * This software is licensed under the terms of the GNU General Public 6 * License version 2, as published by the Free Software Foundation, and 7 * may be copied, distributed, and modified under those terms. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/kernel.h> 16 #include <drm/drm.h> 17 #include <drm/drmP.h> 18 #include <drm/drm_atomic.h> 19 #include <drm/drm_fb_helper.h> 20 #include <drm/drm_crtc_helper.h> 21 22 #include "rockchip_drm_drv.h" 23 #include "rockchip_drm_fb.h" 24 #include "rockchip_drm_gem.h" 25 #include "rockchip_drm_psr.h" 26 27 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb) 28 29 struct rockchip_drm_fb { 30 struct drm_framebuffer fb; 31 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER]; 32 }; 33 34 struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb, 35 unsigned int plane) 36 { 37 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb); 38 39 if (plane >= ROCKCHIP_MAX_FB_BUFFER) 40 return NULL; 41 42 return rk_fb->obj[plane]; 43 } 44 45 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb) 46 { 47 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb); 48 int i; 49 50 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) 51 drm_gem_object_put_unlocked(rockchip_fb->obj[i]); 52 53 drm_framebuffer_cleanup(fb); 54 kfree(rockchip_fb); 55 } 56 57 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb, 58 struct drm_file *file_priv, 59 unsigned int *handle) 60 { 61 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb); 62 63 return drm_gem_handle_create(file_priv, 64 rockchip_fb->obj[0], handle); 65 } 66 67 static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb, 68 struct drm_file *file, 69 unsigned int flags, unsigned int color, 70 struct drm_clip_rect *clips, 71 unsigned int num_clips) 72 { 73 rockchip_drm_psr_flush_all(fb->dev); 74 return 0; 75 } 76 77 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = { 78 .destroy = rockchip_drm_fb_destroy, 79 .create_handle = rockchip_drm_fb_create_handle, 80 .dirty = rockchip_drm_fb_dirty, 81 }; 82 83 static struct rockchip_drm_fb * 84 rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd, 85 struct drm_gem_object **obj, unsigned int num_planes) 86 { 87 struct rockchip_drm_fb *rockchip_fb; 88 int ret; 89 int i; 90 91 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL); 92 if (!rockchip_fb) 93 return ERR_PTR(-ENOMEM); 94 95 drm_helper_mode_fill_fb_struct(dev, &rockchip_fb->fb, mode_cmd); 96 97 for (i = 0; i < num_planes; i++) 98 rockchip_fb->obj[i] = obj[i]; 99 100 ret = drm_framebuffer_init(dev, &rockchip_fb->fb, 101 &rockchip_drm_fb_funcs); 102 if (ret) { 103 DRM_DEV_ERROR(dev->dev, 104 "Failed to initialize framebuffer: %d\n", 105 ret); 106 kfree(rockchip_fb); 107 return ERR_PTR(ret); 108 } 109 110 return rockchip_fb; 111 } 112 113 static struct drm_framebuffer * 114 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, 115 const struct drm_mode_fb_cmd2 *mode_cmd) 116 { 117 struct rockchip_drm_fb *rockchip_fb; 118 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER]; 119 struct drm_gem_object *obj; 120 unsigned int hsub; 121 unsigned int vsub; 122 int num_planes; 123 int ret; 124 int i; 125 126 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); 127 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); 128 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format), 129 ROCKCHIP_MAX_FB_BUFFER); 130 131 for (i = 0; i < num_planes; i++) { 132 unsigned int width = mode_cmd->width / (i ? hsub : 1); 133 unsigned int height = mode_cmd->height / (i ? vsub : 1); 134 unsigned int min_size; 135 136 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]); 137 if (!obj) { 138 DRM_DEV_ERROR(dev->dev, 139 "Failed to lookup GEM object\n"); 140 ret = -ENXIO; 141 goto err_gem_object_unreference; 142 } 143 144 min_size = (height - 1) * mode_cmd->pitches[i] + 145 mode_cmd->offsets[i] + 146 width * drm_format_plane_cpp(mode_cmd->pixel_format, i); 147 148 if (obj->size < min_size) { 149 drm_gem_object_put_unlocked(obj); 150 ret = -EINVAL; 151 goto err_gem_object_unreference; 152 } 153 objs[i] = obj; 154 } 155 156 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i); 157 if (IS_ERR(rockchip_fb)) { 158 ret = PTR_ERR(rockchip_fb); 159 goto err_gem_object_unreference; 160 } 161 162 return &rockchip_fb->fb; 163 164 err_gem_object_unreference: 165 for (i--; i >= 0; i--) 166 drm_gem_object_put_unlocked(objs[i]); 167 return ERR_PTR(ret); 168 } 169 170 static void 171 rockchip_drm_psr_inhibit_get_state(struct drm_atomic_state *state) 172 { 173 struct drm_crtc *crtc; 174 struct drm_crtc_state *crtc_state; 175 struct drm_encoder *encoder; 176 u32 encoder_mask = 0; 177 int i; 178 179 for_each_old_crtc_in_state(state, crtc, crtc_state, i) { 180 encoder_mask |= crtc_state->encoder_mask; 181 encoder_mask |= crtc->state->encoder_mask; 182 } 183 184 drm_for_each_encoder_mask(encoder, state->dev, encoder_mask) 185 rockchip_drm_psr_inhibit_get(encoder); 186 } 187 188 static void 189 rockchip_drm_psr_inhibit_put_state(struct drm_atomic_state *state) 190 { 191 struct drm_crtc *crtc; 192 struct drm_crtc_state *crtc_state; 193 struct drm_encoder *encoder; 194 u32 encoder_mask = 0; 195 int i; 196 197 for_each_old_crtc_in_state(state, crtc, crtc_state, i) { 198 encoder_mask |= crtc_state->encoder_mask; 199 encoder_mask |= crtc->state->encoder_mask; 200 } 201 202 drm_for_each_encoder_mask(encoder, state->dev, encoder_mask) 203 rockchip_drm_psr_inhibit_put(encoder); 204 } 205 206 static void 207 rockchip_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state) 208 { 209 struct drm_device *dev = old_state->dev; 210 211 rockchip_drm_psr_inhibit_get_state(old_state); 212 213 drm_atomic_helper_commit_modeset_disables(dev, old_state); 214 215 drm_atomic_helper_commit_modeset_enables(dev, old_state); 216 217 drm_atomic_helper_commit_planes(dev, old_state, 218 DRM_PLANE_COMMIT_ACTIVE_ONLY); 219 220 rockchip_drm_psr_inhibit_put_state(old_state); 221 222 drm_atomic_helper_commit_hw_done(old_state); 223 224 drm_atomic_helper_wait_for_vblanks(dev, old_state); 225 226 drm_atomic_helper_cleanup_planes(dev, old_state); 227 } 228 229 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = { 230 .atomic_commit_tail = rockchip_atomic_helper_commit_tail_rpm, 231 }; 232 233 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = { 234 .fb_create = rockchip_user_fb_create, 235 .output_poll_changed = drm_fb_helper_output_poll_changed, 236 .atomic_check = drm_atomic_helper_check, 237 .atomic_commit = drm_atomic_helper_commit, 238 }; 239 240 struct drm_framebuffer * 241 rockchip_drm_framebuffer_init(struct drm_device *dev, 242 const struct drm_mode_fb_cmd2 *mode_cmd, 243 struct drm_gem_object *obj) 244 { 245 struct rockchip_drm_fb *rockchip_fb; 246 247 rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1); 248 if (IS_ERR(rockchip_fb)) 249 return ERR_CAST(rockchip_fb); 250 251 return &rockchip_fb->fb; 252 } 253 254 void rockchip_drm_mode_config_init(struct drm_device *dev) 255 { 256 dev->mode_config.min_width = 0; 257 dev->mode_config.min_height = 0; 258 259 /* 260 * set max width and height as default value(4096x4096). 261 * this value would be used to check framebuffer size limitation 262 * at drm_mode_addfb(). 263 */ 264 dev->mode_config.max_width = 4096; 265 dev->mode_config.max_height = 4096; 266 267 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs; 268 dev->mode_config.helper_private = &rockchip_mode_config_helpers; 269 } 270