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>
23 #include "rockchip_drm_drv.h"
24 #include "rockchip_drm_gem.h"
26 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
28 struct rockchip_drm_fb {
29 struct drm_framebuffer fb;
30 dma_addr_t dma_addr[ROCKCHIP_MAX_FB_BUFFER];
31 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
32 struct rockchip_logo *logo;
35 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
38 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
40 if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
43 return rk_fb->dma_addr[plane];
46 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
48 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
49 struct drm_gem_object *obj;
52 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
53 obj = rockchip_fb->obj[i];
55 drm_gem_object_unreference_unlocked(obj);
59 if (rockchip_fb->logo) {
60 struct rockchip_logo *logo = rockchip_fb->logo;
63 void *start = phys_to_virt(logo->start);
64 void *end = phys_to_virt(logo->size);
66 dma_unmap_sg(fb->dev->dev, logo->sgt->sgl,
67 logo->sgt->nents, DMA_TO_DEVICE);
68 sg_free_table(logo->sgt);
69 memblock_free(logo->start, logo->size);
70 free_reserved_area(start, end, -1, "drm_logo");
74 WARN_ON(rockchip_fb->sgt);
77 drm_framebuffer_cleanup(fb);
81 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
82 struct drm_file *file_priv,
85 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
87 return drm_gem_handle_create(file_priv,
88 rockchip_fb->obj[0], handle);
91 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
92 .destroy = rockchip_drm_fb_destroy,
93 .create_handle = rockchip_drm_fb_create_handle,
96 struct drm_framebuffer *
97 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
98 struct drm_gem_object **obj, struct rockchip_logo *logo,
99 unsigned int num_planes)
101 struct rockchip_drm_fb *rockchip_fb;
102 struct rockchip_gem_object *rk_obj;
106 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
108 return ERR_PTR(-ENOMEM);
110 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
112 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
113 &rockchip_drm_fb_funcs);
115 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
121 for (i = 0; i < num_planes; i++)
122 rockchip_fb->obj[i] = obj[i];
124 for (i = 0; i < num_planes; i++) {
125 rk_obj = to_rockchip_obj(obj[i]);
126 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
130 rockchip_fb->dma_addr[0] = logo->dma_addr;
131 rockchip_fb->logo = logo;
136 dev_err(dev->dev, "Failed to find available buffer\n");
137 goto err_deinit_drm_fb;
140 return &rockchip_fb->fb;
143 drm_framebuffer_cleanup(&rockchip_fb->fb);
149 static struct drm_framebuffer *
150 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
151 struct drm_mode_fb_cmd2 *mode_cmd)
153 struct drm_framebuffer *fb;
154 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
155 struct drm_gem_object *obj;
162 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
163 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
164 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
165 ROCKCHIP_MAX_FB_BUFFER);
167 for (i = 0; i < num_planes; i++) {
168 unsigned int width = mode_cmd->width / (i ? hsub : 1);
169 unsigned int height = mode_cmd->height / (i ? vsub : 1);
170 unsigned int min_size;
172 obj = drm_gem_object_lookup(dev, file_priv,
173 mode_cmd->handles[i]);
175 dev_err(dev->dev, "Failed to lookup GEM object\n");
177 goto err_gem_object_unreference;
180 min_size = (height - 1) * mode_cmd->pitches[i] +
181 mode_cmd->offsets[i] +
182 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
184 if (obj->size < min_size) {
185 drm_gem_object_unreference_unlocked(obj);
187 goto err_gem_object_unreference;
192 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
195 goto err_gem_object_unreference;
200 err_gem_object_unreference:
201 for (i--; i >= 0; i--)
202 drm_gem_object_unreference_unlocked(objs[i]);
206 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
208 struct rockchip_drm_private *private = dev->dev_private;
209 struct drm_fb_helper *fb_helper = private->fbdev_helper;
212 drm_fb_helper_hotplug_event(fb_helper);
215 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
217 struct rockchip_drm_private *priv = crtc->dev->dev_private;
218 int pipe = drm_crtc_index(crtc);
219 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
221 if (crtc_funcs && crtc_funcs->wait_for_update)
222 crtc_funcs->wait_for_update(crtc);
226 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
227 * have hardware counters for neither vblanks nor scanlines, which results in
229 * | <-- HW vsync irq and reg take effect
231 * get_vblank and wait --> |
232 * | <-- handle_vblank, vblank->count + 1
235 * | <-- HW vsync irq and reg take effect
237 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
238 * of waiting for vblank_count to change.
241 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
243 struct drm_crtc_state *old_crtc_state;
244 struct drm_crtc *crtc;
247 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
248 /* No one cares about the old state, so abuse it for tracking
249 * and store whether we hold a vblank reference (and should do a
250 * vblank wait) in the ->enable boolean.
252 old_crtc_state->enable = false;
254 if (!crtc->state->active)
257 if (!drm_atomic_helper_framebuffer_changed(dev,
261 ret = drm_crtc_vblank_get(crtc);
265 old_crtc_state->enable = true;
268 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
269 if (!old_crtc_state->enable)
272 rockchip_crtc_wait_for_update(crtc);
273 drm_crtc_vblank_put(crtc);
278 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
280 struct drm_atomic_state *state = commit->state;
281 struct drm_device *dev = commit->dev;
284 * TODO: do fence wait here.
288 * Rockchip crtc support runtime PM, can't update display planes
289 * when crtc is disabled.
291 * drm_atomic_helper_commit comments detail that:
292 * For drivers supporting runtime PM the recommended sequence is
294 * drm_atomic_helper_commit_modeset_disables(dev, state);
296 * drm_atomic_helper_commit_modeset_enables(dev, state);
298 * drm_atomic_helper_commit_planes(dev, state, true);
300 * See the kerneldoc entries for these three functions for more details.
302 drm_atomic_helper_commit_modeset_disables(dev, state);
304 drm_atomic_helper_commit_modeset_enables(dev, state);
306 drm_atomic_helper_commit_planes(dev, state, true);
308 rockchip_atomic_wait_for_complete(dev, state);
310 drm_atomic_helper_cleanup_planes(dev, state);
312 drm_atomic_state_free(state);
315 void rockchip_drm_atomic_work(struct work_struct *work)
317 struct rockchip_atomic_commit *commit = container_of(work,
318 struct rockchip_atomic_commit, work);
320 rockchip_atomic_commit_complete(commit);
323 int rockchip_drm_atomic_commit(struct drm_device *dev,
324 struct drm_atomic_state *state,
327 struct rockchip_drm_private *private = dev->dev_private;
328 struct rockchip_atomic_commit *commit = &private->commit;
331 ret = drm_atomic_helper_prepare_planes(dev, state);
335 /* serialize outstanding asynchronous commits */
336 mutex_lock(&commit->lock);
337 flush_work(&commit->work);
339 drm_atomic_helper_swap_state(dev, state);
342 commit->state = state;
345 schedule_work(&commit->work);
347 rockchip_atomic_commit_complete(commit);
349 mutex_unlock(&commit->lock);
354 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
355 .fb_create = rockchip_user_fb_create,
356 .output_poll_changed = rockchip_drm_output_poll_changed,
357 .atomic_check = drm_atomic_helper_check,
358 .atomic_commit = rockchip_drm_atomic_commit,
361 struct drm_framebuffer *
362 rockchip_drm_framebuffer_init(struct drm_device *dev,
363 struct drm_mode_fb_cmd2 *mode_cmd,
364 struct drm_gem_object *obj)
366 struct drm_framebuffer *fb;
368 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
375 void rockchip_drm_mode_config_init(struct drm_device *dev)
377 dev->mode_config.min_width = 0;
378 dev->mode_config.min_height = 0;
381 * set max width and height as default value(4096x4096).
382 * this value would be used to check framebuffer size limitation
383 * at drm_mode_addfb().
385 dev->mode_config.max_width = 4096;
386 dev->mode_config.max_height = 4096;
388 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;