Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 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> |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 18 | #include <drm/drm_atomic.h> |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 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_gem.h" |
| 24 | |
| 25 | #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb) |
| 26 | |
| 27 | struct rockchip_drm_fb { |
| 28 | struct drm_framebuffer fb; |
| 29 | struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER]; |
| 30 | }; |
| 31 | |
| 32 | struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb, |
| 33 | unsigned int plane) |
| 34 | { |
| 35 | struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb); |
| 36 | |
| 37 | if (plane >= ROCKCHIP_MAX_FB_BUFFER) |
| 38 | return NULL; |
| 39 | |
| 40 | return rk_fb->obj[plane]; |
| 41 | } |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 42 | |
| 43 | static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb) |
| 44 | { |
| 45 | struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb); |
| 46 | struct drm_gem_object *obj; |
| 47 | int i; |
| 48 | |
| 49 | for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) { |
| 50 | obj = rockchip_fb->obj[i]; |
| 51 | if (obj) |
| 52 | drm_gem_object_unreference_unlocked(obj); |
| 53 | } |
| 54 | |
| 55 | drm_framebuffer_cleanup(fb); |
| 56 | kfree(rockchip_fb); |
| 57 | } |
| 58 | |
| 59 | static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb, |
| 60 | struct drm_file *file_priv, |
| 61 | unsigned int *handle) |
| 62 | { |
| 63 | struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb); |
| 64 | |
| 65 | return drm_gem_handle_create(file_priv, |
| 66 | rockchip_fb->obj[0], handle); |
| 67 | } |
| 68 | |
Ville Syrjälä | 28c508e | 2015-12-15 12:21:12 +0100 | [diff] [blame] | 69 | static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = { |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 70 | .destroy = rockchip_drm_fb_destroy, |
| 71 | .create_handle = rockchip_drm_fb_create_handle, |
| 72 | }; |
| 73 | |
| 74 | static struct rockchip_drm_fb * |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 75 | rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd, |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 76 | struct drm_gem_object **obj, unsigned int num_planes) |
| 77 | { |
| 78 | struct rockchip_drm_fb *rockchip_fb; |
| 79 | int ret; |
| 80 | int i; |
| 81 | |
| 82 | rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL); |
| 83 | if (!rockchip_fb) |
| 84 | return ERR_PTR(-ENOMEM); |
| 85 | |
| 86 | drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd); |
| 87 | |
| 88 | for (i = 0; i < num_planes; i++) |
| 89 | rockchip_fb->obj[i] = obj[i]; |
| 90 | |
| 91 | ret = drm_framebuffer_init(dev, &rockchip_fb->fb, |
| 92 | &rockchip_drm_fb_funcs); |
| 93 | if (ret) { |
| 94 | dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", |
| 95 | ret); |
| 96 | kfree(rockchip_fb); |
| 97 | return ERR_PTR(ret); |
| 98 | } |
| 99 | |
| 100 | return rockchip_fb; |
| 101 | } |
| 102 | |
| 103 | static struct drm_framebuffer * |
| 104 | rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 105 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 106 | { |
| 107 | struct rockchip_drm_fb *rockchip_fb; |
| 108 | struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER]; |
| 109 | struct drm_gem_object *obj; |
| 110 | unsigned int hsub; |
| 111 | unsigned int vsub; |
| 112 | int num_planes; |
| 113 | int ret; |
| 114 | int i; |
| 115 | |
| 116 | hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); |
| 117 | vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); |
| 118 | num_planes = min(drm_format_num_planes(mode_cmd->pixel_format), |
| 119 | ROCKCHIP_MAX_FB_BUFFER); |
| 120 | |
| 121 | for (i = 0; i < num_planes; i++) { |
| 122 | unsigned int width = mode_cmd->width / (i ? hsub : 1); |
| 123 | unsigned int height = mode_cmd->height / (i ? vsub : 1); |
| 124 | unsigned int min_size; |
| 125 | |
| 126 | obj = drm_gem_object_lookup(dev, file_priv, |
| 127 | mode_cmd->handles[i]); |
| 128 | if (!obj) { |
| 129 | dev_err(dev->dev, "Failed to lookup GEM object\n"); |
| 130 | ret = -ENXIO; |
| 131 | goto err_gem_object_unreference; |
| 132 | } |
| 133 | |
| 134 | min_size = (height - 1) * mode_cmd->pitches[i] + |
| 135 | mode_cmd->offsets[i] + |
| 136 | width * drm_format_plane_cpp(mode_cmd->pixel_format, i); |
| 137 | |
| 138 | if (obj->size < min_size) { |
| 139 | drm_gem_object_unreference_unlocked(obj); |
| 140 | ret = -EINVAL; |
| 141 | goto err_gem_object_unreference; |
| 142 | } |
| 143 | objs[i] = obj; |
| 144 | } |
| 145 | |
| 146 | rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i); |
| 147 | if (IS_ERR(rockchip_fb)) { |
| 148 | ret = PTR_ERR(rockchip_fb); |
| 149 | goto err_gem_object_unreference; |
| 150 | } |
| 151 | |
| 152 | return &rockchip_fb->fb; |
| 153 | |
| 154 | err_gem_object_unreference: |
| 155 | for (i--; i >= 0; i--) |
| 156 | drm_gem_object_unreference_unlocked(objs[i]); |
| 157 | return ERR_PTR(ret); |
| 158 | } |
| 159 | |
| 160 | static void rockchip_drm_output_poll_changed(struct drm_device *dev) |
| 161 | { |
| 162 | struct rockchip_drm_private *private = dev->dev_private; |
| 163 | struct drm_fb_helper *fb_helper = &private->fbdev_helper; |
| 164 | |
Heiko Stübner | 765c35b | 2015-06-02 16:41:45 +0200 | [diff] [blame] | 165 | if (fb_helper) |
| 166 | drm_fb_helper_hotplug_event(fb_helper); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 169 | static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc) |
| 170 | { |
| 171 | struct rockchip_drm_private *priv = crtc->dev->dev_private; |
| 172 | int pipe = drm_crtc_index(crtc); |
| 173 | const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe]; |
| 174 | |
| 175 | if (crtc_funcs && crtc_funcs->wait_for_update) |
| 176 | crtc_funcs->wait_for_update(crtc); |
| 177 | } |
| 178 | |
| 179 | static void |
John Keeping | f2227f4 | 2016-01-19 10:46:59 +0000 | [diff] [blame^] | 180 | rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state) |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 181 | { |
| 182 | struct drm_crtc_state *old_crtc_state; |
| 183 | struct drm_crtc *crtc; |
| 184 | int i, ret; |
| 185 | |
| 186 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
| 187 | /* No one cares about the old state, so abuse it for tracking |
| 188 | * and store whether we hold a vblank reference (and should do a |
| 189 | * vblank wait) in the ->enable boolean. |
| 190 | */ |
| 191 | old_crtc_state->enable = false; |
| 192 | |
| 193 | if (!crtc->state->active) |
| 194 | continue; |
| 195 | |
John Keeping | f2227f4 | 2016-01-19 10:46:59 +0000 | [diff] [blame^] | 196 | if (!drm_atomic_helper_framebuffer_changed(dev, |
| 197 | old_state, crtc)) |
| 198 | continue; |
| 199 | |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 200 | ret = drm_crtc_vblank_get(crtc); |
| 201 | if (ret != 0) |
| 202 | continue; |
| 203 | |
| 204 | old_crtc_state->enable = true; |
| 205 | } |
| 206 | |
| 207 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
| 208 | if (!old_crtc_state->enable) |
| 209 | continue; |
| 210 | |
| 211 | rockchip_crtc_wait_for_update(crtc); |
| 212 | drm_crtc_vblank_put(crtc); |
| 213 | } |
| 214 | } |
| 215 | |
Mark Yao | f32fad5 | 2015-12-16 18:09:38 +0800 | [diff] [blame] | 216 | static void |
| 217 | rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit) |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 218 | { |
Mark Yao | f32fad5 | 2015-12-16 18:09:38 +0800 | [diff] [blame] | 219 | struct drm_atomic_state *state = commit->state; |
| 220 | struct drm_device *dev = commit->dev; |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * TODO: do fence wait here. |
| 224 | */ |
| 225 | |
| 226 | /* |
| 227 | * Rockchip crtc support runtime PM, can't update display planes |
| 228 | * when crtc is disabled. |
| 229 | * |
| 230 | * drm_atomic_helper_commit comments detail that: |
| 231 | * For drivers supporting runtime PM the recommended sequence is |
| 232 | * |
| 233 | * drm_atomic_helper_commit_modeset_disables(dev, state); |
| 234 | * |
| 235 | * drm_atomic_helper_commit_modeset_enables(dev, state); |
| 236 | * |
| 237 | * drm_atomic_helper_commit_planes(dev, state, true); |
| 238 | * |
| 239 | * See the kerneldoc entries for these three functions for more details. |
| 240 | */ |
| 241 | drm_atomic_helper_commit_modeset_disables(dev, state); |
| 242 | |
| 243 | drm_atomic_helper_commit_modeset_enables(dev, state); |
| 244 | |
| 245 | drm_atomic_helper_commit_planes(dev, state, true); |
| 246 | |
John Keeping | f2227f4 | 2016-01-19 10:46:59 +0000 | [diff] [blame^] | 247 | rockchip_atomic_wait_for_complete(dev, state); |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 248 | |
| 249 | drm_atomic_helper_cleanup_planes(dev, state); |
| 250 | |
| 251 | drm_atomic_state_free(state); |
Mark Yao | f32fad5 | 2015-12-16 18:09:38 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void rockchip_drm_atomic_work(struct work_struct *work) |
| 255 | { |
| 256 | struct rockchip_atomic_commit *commit = container_of(work, |
| 257 | struct rockchip_atomic_commit, work); |
| 258 | |
| 259 | rockchip_atomic_commit_complete(commit); |
| 260 | } |
| 261 | |
| 262 | int rockchip_drm_atomic_commit(struct drm_device *dev, |
| 263 | struct drm_atomic_state *state, |
| 264 | bool async) |
| 265 | { |
| 266 | struct rockchip_drm_private *private = dev->dev_private; |
| 267 | struct rockchip_atomic_commit *commit = &private->commit; |
| 268 | int ret; |
| 269 | |
| 270 | ret = drm_atomic_helper_prepare_planes(dev, state); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | |
| 274 | /* serialize outstanding asynchronous commits */ |
| 275 | mutex_lock(&commit->lock); |
| 276 | flush_work(&commit->work); |
| 277 | |
| 278 | drm_atomic_helper_swap_state(dev, state); |
| 279 | |
| 280 | commit->dev = dev; |
| 281 | commit->state = state; |
| 282 | |
| 283 | if (async) |
| 284 | schedule_work(&commit->work); |
| 285 | else |
| 286 | rockchip_atomic_commit_complete(commit); |
| 287 | |
| 288 | mutex_unlock(&commit->lock); |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 293 | static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = { |
| 294 | .fb_create = rockchip_user_fb_create, |
| 295 | .output_poll_changed = rockchip_drm_output_poll_changed, |
Mark Yao | 63ebb9f | 2015-11-30 18:22:42 +0800 | [diff] [blame] | 296 | .atomic_check = drm_atomic_helper_check, |
| 297 | .atomic_commit = rockchip_drm_atomic_commit, |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | struct drm_framebuffer * |
| 301 | rockchip_drm_framebuffer_init(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 302 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 303 | struct drm_gem_object *obj) |
| 304 | { |
| 305 | struct rockchip_drm_fb *rockchip_fb; |
| 306 | |
| 307 | rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1); |
| 308 | if (IS_ERR(rockchip_fb)) |
| 309 | return NULL; |
| 310 | |
| 311 | return &rockchip_fb->fb; |
| 312 | } |
| 313 | |
| 314 | void rockchip_drm_mode_config_init(struct drm_device *dev) |
| 315 | { |
| 316 | dev->mode_config.min_width = 0; |
| 317 | dev->mode_config.min_height = 0; |
| 318 | |
| 319 | /* |
| 320 | * set max width and height as default value(4096x4096). |
| 321 | * this value would be used to check framebuffer size limitation |
| 322 | * at drm_mode_addfb(). |
| 323 | */ |
| 324 | dev->mode_config.max_width = 4096; |
| 325 | dev->mode_config.max_height = 4096; |
| 326 | |
| 327 | dev->mode_config.funcs = &rockchip_drm_mode_config_funcs; |
| 328 | } |