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 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
39 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
41 if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
44 return rk_fb->dma_addr[plane];
47 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
49 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
50 struct drm_gem_object *obj;
53 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
54 obj = rockchip_fb->obj[i];
56 drm_gem_object_unreference_unlocked(obj);
60 if (rockchip_fb->logo)
61 rockchip_free_loader_memory(fb->dev);
63 WARN_ON(rockchip_fb->logo);
66 drm_framebuffer_cleanup(fb);
70 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
71 struct drm_file *file_priv,
74 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
76 return drm_gem_handle_create(file_priv,
77 rockchip_fb->obj[0], handle);
80 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
81 .destroy = rockchip_drm_fb_destroy,
82 .create_handle = rockchip_drm_fb_create_handle,
85 struct drm_framebuffer *
86 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
87 struct drm_gem_object **obj, struct rockchip_logo *logo,
88 unsigned int num_planes)
90 struct rockchip_drm_fb *rockchip_fb;
91 struct rockchip_gem_object *rk_obj;
95 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
97 return ERR_PTR(-ENOMEM);
99 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
101 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
102 &rockchip_drm_fb_funcs);
104 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
110 for (i = 0; i < num_planes; i++)
111 rockchip_fb->obj[i] = obj[i];
113 for (i = 0; i < num_planes; i++) {
114 rk_obj = to_rockchip_obj(obj[i]);
115 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
119 rockchip_fb->dma_addr[0] = logo->dma_addr;
120 rockchip_fb->logo = logo;
125 dev_err(dev->dev, "Failed to find available buffer\n");
126 goto err_deinit_drm_fb;
129 return &rockchip_fb->fb;
132 drm_framebuffer_cleanup(&rockchip_fb->fb);
138 static struct drm_framebuffer *
139 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
140 struct drm_mode_fb_cmd2 *mode_cmd)
142 struct drm_framebuffer *fb;
143 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
144 struct drm_gem_object *obj;
151 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
152 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
153 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
154 ROCKCHIP_MAX_FB_BUFFER);
156 for (i = 0; i < num_planes; i++) {
157 unsigned int width = mode_cmd->width / (i ? hsub : 1);
158 unsigned int height = mode_cmd->height / (i ? vsub : 1);
159 unsigned int min_size;
161 drm_format_plane_bpp(mode_cmd->pixel_format, i);
163 obj = drm_gem_object_lookup(dev, file_priv,
164 mode_cmd->handles[i]);
166 dev_err(dev->dev, "Failed to lookup GEM object\n");
168 goto err_gem_object_unreference;
171 min_size = (height - 1) * mode_cmd->pitches[i] +
172 mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
173 if (obj->size < min_size) {
174 drm_gem_object_unreference_unlocked(obj);
176 goto err_gem_object_unreference;
181 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
184 goto err_gem_object_unreference;
189 err_gem_object_unreference:
190 for (i--; i >= 0; i--)
191 drm_gem_object_unreference_unlocked(objs[i]);
195 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
197 struct rockchip_drm_private *private = dev->dev_private;
198 struct drm_fb_helper *fb_helper = private->fbdev_helper;
201 drm_fb_helper_hotplug_event(fb_helper);
204 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
206 struct rockchip_drm_private *priv = crtc->dev->dev_private;
207 int pipe = drm_crtc_index(crtc);
208 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
210 if (crtc_funcs && crtc_funcs->wait_for_update)
211 crtc_funcs->wait_for_update(crtc);
215 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
216 * have hardware counters for neither vblanks nor scanlines, which results in
218 * | <-- HW vsync irq and reg take effect
220 * get_vblank and wait --> |
221 * | <-- handle_vblank, vblank->count + 1
224 * | <-- HW vsync irq and reg take effect
226 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
227 * of waiting for vblank_count to change.
230 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
232 struct drm_crtc_state *old_crtc_state;
233 struct drm_crtc *crtc;
236 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
237 /* No one cares about the old state, so abuse it for tracking
238 * and store whether we hold a vblank reference (and should do a
239 * vblank wait) in the ->enable boolean.
241 old_crtc_state->enable = false;
243 if (!crtc->state->active)
246 if (!drm_atomic_helper_framebuffer_changed(dev,
250 ret = drm_crtc_vblank_get(crtc);
254 old_crtc_state->enable = true;
257 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
258 if (!old_crtc_state->enable)
261 rockchip_crtc_wait_for_update(crtc);
262 drm_crtc_vblank_put(crtc);
267 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
269 struct drm_atomic_state *state = commit->state;
270 struct drm_device *dev = commit->dev;
273 * TODO: do fence wait here.
277 * Rockchip crtc support runtime PM, can't update display planes
278 * when crtc is disabled.
280 * drm_atomic_helper_commit comments detail that:
281 * For drivers supporting runtime PM the recommended sequence is
283 * drm_atomic_helper_commit_modeset_disables(dev, state);
285 * drm_atomic_helper_commit_modeset_enables(dev, state);
287 * drm_atomic_helper_commit_planes(dev, state, true);
289 * See the kerneldoc entries for these three functions for more details.
291 drm_atomic_helper_commit_modeset_disables(dev, state);
293 drm_atomic_helper_commit_modeset_enables(dev, state);
295 drm_atomic_helper_commit_planes(dev, state, true);
297 rockchip_atomic_wait_for_complete(dev, state);
299 drm_atomic_helper_cleanup_planes(dev, state);
301 drm_atomic_state_free(state);
304 void rockchip_drm_atomic_work(struct work_struct *work)
306 struct rockchip_atomic_commit *commit = container_of(work,
307 struct rockchip_atomic_commit, work);
309 rockchip_atomic_commit_complete(commit);
312 int rockchip_drm_atomic_commit(struct drm_device *dev,
313 struct drm_atomic_state *state,
316 struct rockchip_drm_private *private = dev->dev_private;
317 struct rockchip_atomic_commit *commit = &private->commit;
320 ret = drm_atomic_helper_prepare_planes(dev, state);
324 /* serialize outstanding asynchronous commits */
325 mutex_lock(&commit->lock);
326 flush_work(&commit->work);
328 drm_atomic_helper_swap_state(dev, state);
331 commit->state = state;
334 schedule_work(&commit->work);
336 rockchip_atomic_commit_complete(commit);
338 mutex_unlock(&commit->lock);
343 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
344 .fb_create = rockchip_user_fb_create,
345 .output_poll_changed = rockchip_drm_output_poll_changed,
346 .atomic_check = drm_atomic_helper_check,
347 .atomic_commit = rockchip_drm_atomic_commit,
350 struct drm_framebuffer *
351 rockchip_drm_framebuffer_init(struct drm_device *dev,
352 struct drm_mode_fb_cmd2 *mode_cmd,
353 struct drm_gem_object *obj)
355 struct drm_framebuffer *fb;
357 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
364 void rockchip_drm_mode_config_init(struct drm_device *dev)
366 dev->mode_config.min_width = 0;
367 dev->mode_config.min_height = 0;
370 * set max width and height as default value(4096x4096).
371 * this value would be used to check framebuffer size limitation
372 * at drm_mode_addfb().
374 dev->mode_config.max_width = 8192;
375 dev->mode_config.max_height = 8192;
377 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;