2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
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.
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.
15 #include <linux/kernel.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <linux/memblock.h>
22 #include <linux/iommu.h>
24 #include "rockchip_drm_drv.h"
25 #include "rockchip_drm_gem.h"
27 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
29 struct rockchip_drm_fb {
30 struct drm_framebuffer fb;
31 dma_addr_t dma_addr[ROCKCHIP_MAX_FB_BUFFER];
32 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
33 struct rockchip_logo *logo;
36 bool rockchip_fb_is_logo(struct drm_framebuffer *fb)
38 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
40 return rk_fb && rk_fb->logo;
43 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
46 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
48 if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
51 return rk_fb->dma_addr[plane];
54 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
56 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
57 struct drm_gem_object *obj;
60 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
61 obj = rockchip_fb->obj[i];
63 drm_gem_object_unreference_unlocked(obj);
67 if (rockchip_fb->logo)
68 rockchip_free_loader_memory(fb->dev);
70 WARN_ON(rockchip_fb->logo);
73 drm_framebuffer_cleanup(fb);
77 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
78 struct drm_file *file_priv,
81 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
83 return drm_gem_handle_create(file_priv,
84 rockchip_fb->obj[0], handle);
87 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
88 .destroy = rockchip_drm_fb_destroy,
89 .create_handle = rockchip_drm_fb_create_handle,
92 struct drm_framebuffer *
93 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
94 struct drm_gem_object **obj, struct rockchip_logo *logo,
95 unsigned int num_planes)
97 struct rockchip_drm_fb *rockchip_fb;
98 struct rockchip_gem_object *rk_obj;
102 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
104 return ERR_PTR(-ENOMEM);
106 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
108 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
109 &rockchip_drm_fb_funcs);
111 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
117 for (i = 0; i < num_planes; i++)
118 rockchip_fb->obj[i] = obj[i];
120 for (i = 0; i < num_planes; i++) {
121 rk_obj = to_rockchip_obj(obj[i]);
122 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
126 rockchip_fb->dma_addr[0] = logo->dma_addr;
127 rockchip_fb->logo = logo;
132 dev_err(dev->dev, "Failed to find available buffer\n");
133 goto err_deinit_drm_fb;
136 return &rockchip_fb->fb;
139 drm_framebuffer_cleanup(&rockchip_fb->fb);
145 static struct drm_framebuffer *
146 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
147 struct drm_mode_fb_cmd2 *mode_cmd)
149 struct drm_framebuffer *fb;
150 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
151 struct drm_gem_object *obj;
158 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
159 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
160 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
161 ROCKCHIP_MAX_FB_BUFFER);
163 for (i = 0; i < num_planes; i++) {
164 unsigned int width = mode_cmd->width / (i ? hsub : 1);
165 unsigned int height = mode_cmd->height / (i ? vsub : 1);
166 unsigned int min_size;
168 drm_format_plane_bpp(mode_cmd->pixel_format, i);
170 obj = drm_gem_object_lookup(dev, file_priv,
171 mode_cmd->handles[i]);
173 dev_err(dev->dev, "Failed to lookup GEM object\n");
175 goto err_gem_object_unreference;
178 min_size = (height - 1) * mode_cmd->pitches[i] +
179 mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
180 if (obj->size < min_size) {
181 drm_gem_object_unreference_unlocked(obj);
183 goto err_gem_object_unreference;
188 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
191 goto err_gem_object_unreference;
196 err_gem_object_unreference:
197 for (i--; i >= 0; i--)
198 drm_gem_object_unreference_unlocked(objs[i]);
202 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
204 struct rockchip_drm_private *private = dev->dev_private;
205 struct drm_fb_helper *fb_helper = private->fbdev_helper;
208 drm_fb_helper_hotplug_event(fb_helper);
211 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
213 struct rockchip_drm_private *priv = crtc->dev->dev_private;
214 int pipe = drm_crtc_index(crtc);
215 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
217 if (crtc_funcs && crtc_funcs->wait_for_update)
218 crtc_funcs->wait_for_update(crtc);
222 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
223 * have hardware counters for neither vblanks nor scanlines, which results in
225 * | <-- HW vsync irq and reg take effect
227 * get_vblank and wait --> |
228 * | <-- handle_vblank, vblank->count + 1
231 * | <-- HW vsync irq and reg take effect
233 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
234 * of waiting for vblank_count to change.
237 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
239 struct drm_crtc_state *old_crtc_state;
240 struct drm_crtc *crtc;
243 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
244 /* No one cares about the old state, so abuse it for tracking
245 * and store whether we hold a vblank reference (and should do a
246 * vblank wait) in the ->enable boolean.
248 old_crtc_state->enable = false;
250 if (!crtc->state->active)
253 if (!drm_atomic_helper_framebuffer_changed(dev,
257 ret = drm_crtc_vblank_get(crtc);
261 old_crtc_state->enable = true;
264 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
265 if (!old_crtc_state->enable)
268 rockchip_crtc_wait_for_update(crtc);
269 drm_crtc_vblank_put(crtc);
274 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
276 struct drm_atomic_state *state = commit->state;
277 struct drm_device *dev = commit->dev;
280 * TODO: do fence wait here.
284 * Rockchip crtc support runtime PM, can't update display planes
285 * when crtc is disabled.
287 * drm_atomic_helper_commit comments detail that:
288 * For drivers supporting runtime PM the recommended sequence is
290 * drm_atomic_helper_commit_modeset_disables(dev, state);
292 * drm_atomic_helper_commit_modeset_enables(dev, state);
294 * drm_atomic_helper_commit_planes(dev, state, true);
296 * See the kerneldoc entries for these three functions for more details.
298 drm_atomic_helper_commit_modeset_disables(dev, state);
300 drm_atomic_helper_commit_modeset_enables(dev, state);
302 drm_atomic_helper_commit_planes(dev, state, true);
304 rockchip_atomic_wait_for_complete(dev, state);
306 drm_atomic_helper_cleanup_planes(dev, state);
308 drm_atomic_state_free(state);
311 void rockchip_drm_atomic_work(struct work_struct *work)
313 struct rockchip_atomic_commit *commit = container_of(work,
314 struct rockchip_atomic_commit, work);
316 rockchip_atomic_commit_complete(commit);
319 int rockchip_drm_atomic_commit(struct drm_device *dev,
320 struct drm_atomic_state *state,
323 struct rockchip_drm_private *private = dev->dev_private;
324 struct rockchip_atomic_commit *commit = &private->commit;
327 ret = drm_atomic_helper_prepare_planes(dev, state);
331 /* serialize outstanding asynchronous commits */
332 mutex_lock(&commit->lock);
333 flush_work(&commit->work);
335 drm_atomic_helper_swap_state(dev, state);
338 commit->state = state;
341 schedule_work(&commit->work);
343 rockchip_atomic_commit_complete(commit);
345 mutex_unlock(&commit->lock);
350 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
351 .fb_create = rockchip_user_fb_create,
352 .output_poll_changed = rockchip_drm_output_poll_changed,
353 .atomic_check = drm_atomic_helper_check,
354 .atomic_commit = rockchip_drm_atomic_commit,
357 struct drm_framebuffer *
358 rockchip_drm_framebuffer_init(struct drm_device *dev,
359 struct drm_mode_fb_cmd2 *mode_cmd,
360 struct drm_gem_object *obj)
362 struct drm_framebuffer *fb;
364 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
371 void rockchip_drm_mode_config_init(struct drm_device *dev)
373 dev->mode_config.min_width = 0;
374 dev->mode_config.min_height = 0;
377 * set max width and height as default value(4096x4096).
378 * this value would be used to check framebuffer size limitation
379 * at drm_mode_addfb().
381 dev->mode_config.max_width = 8192;
382 dev->mode_config.max_height = 8192;
384 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;