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"
26 #include "rockchip_drm_backlight.h"
28 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
30 struct rockchip_drm_fb {
31 struct drm_framebuffer fb;
32 dma_addr_t dma_addr[ROCKCHIP_MAX_FB_BUFFER];
33 struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
34 struct rockchip_logo *logo;
37 bool rockchip_fb_is_logo(struct drm_framebuffer *fb)
39 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
41 return rk_fb && rk_fb->logo;
44 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
47 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
49 if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
52 return rk_fb->dma_addr[plane];
55 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
57 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
58 struct drm_gem_object *obj;
61 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
62 obj = rockchip_fb->obj[i];
64 drm_gem_object_unreference_unlocked(obj);
68 if (rockchip_fb->logo)
69 rockchip_free_loader_memory(fb->dev);
71 WARN_ON(rockchip_fb->logo);
74 drm_framebuffer_cleanup(fb);
78 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
79 struct drm_file *file_priv,
82 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
84 return drm_gem_handle_create(file_priv,
85 rockchip_fb->obj[0], handle);
88 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
89 .destroy = rockchip_drm_fb_destroy,
90 .create_handle = rockchip_drm_fb_create_handle,
93 struct drm_framebuffer *
94 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
95 struct drm_gem_object **obj, struct rockchip_logo *logo,
96 unsigned int num_planes)
98 struct rockchip_drm_fb *rockchip_fb;
99 struct rockchip_gem_object *rk_obj;
103 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
105 return ERR_PTR(-ENOMEM);
107 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
109 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
110 &rockchip_drm_fb_funcs);
112 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
118 for (i = 0; i < num_planes; i++)
119 rockchip_fb->obj[i] = obj[i];
121 for (i = 0; i < num_planes; i++) {
122 rk_obj = to_rockchip_obj(obj[i]);
123 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
127 rockchip_fb->dma_addr[0] = logo->dma_addr;
128 rockchip_fb->logo = logo;
133 dev_err(dev->dev, "Failed to find available buffer\n");
134 goto err_deinit_drm_fb;
137 return &rockchip_fb->fb;
140 drm_framebuffer_cleanup(&rockchip_fb->fb);
146 static struct drm_framebuffer *
147 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
148 struct drm_mode_fb_cmd2 *mode_cmd)
150 struct drm_framebuffer *fb;
151 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
152 struct drm_gem_object *obj;
159 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
160 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
161 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
162 ROCKCHIP_MAX_FB_BUFFER);
164 for (i = 0; i < num_planes; i++) {
165 unsigned int width = mode_cmd->width / (i ? hsub : 1);
166 unsigned int height = mode_cmd->height / (i ? vsub : 1);
167 unsigned int min_size;
169 drm_format_plane_bpp(mode_cmd->pixel_format, i);
171 obj = drm_gem_object_lookup(dev, file_priv,
172 mode_cmd->handles[i]);
174 dev_err(dev->dev, "Failed to lookup GEM object\n");
176 goto err_gem_object_unreference;
179 min_size = (height - 1) * mode_cmd->pitches[i] +
180 mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
181 if (obj->size < min_size) {
182 drm_gem_object_unreference_unlocked(obj);
184 goto err_gem_object_unreference;
189 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
192 goto err_gem_object_unreference;
197 err_gem_object_unreference:
198 for (i--; i >= 0; i--)
199 drm_gem_object_unreference_unlocked(objs[i]);
203 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
205 struct rockchip_drm_private *private = dev->dev_private;
206 struct drm_fb_helper *fb_helper = private->fbdev_helper;
209 drm_fb_helper_hotplug_event(fb_helper);
213 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
215 struct drm_atomic_state *state = commit->state;
216 struct drm_device *dev = commit->dev;
219 * TODO: do fence wait here.
223 * Rockchip crtc support runtime PM, can't update display planes
224 * when crtc is disabled.
226 * drm_atomic_helper_commit comments detail that:
227 * For drivers supporting runtime PM the recommended sequence is
229 * drm_atomic_helper_commit_modeset_disables(dev, state);
231 * drm_atomic_helper_commit_modeset_enables(dev, state);
233 * drm_atomic_helper_commit_planes(dev, state, true);
235 * See the kerneldoc entries for these three functions for more details.
237 drm_atomic_helper_commit_modeset_disables(dev, state);
239 drm_atomic_helper_commit_modeset_enables(dev, state);
241 rockchip_drm_backlight_update(dev);
243 drm_atomic_helper_commit_planes(dev, state, true);
245 drm_atomic_helper_wait_for_vblanks(dev, state);
247 drm_atomic_helper_cleanup_planes(dev, state);
249 drm_atomic_state_free(state);
252 void rockchip_drm_atomic_work(struct work_struct *work)
254 struct rockchip_atomic_commit *commit = container_of(work,
255 struct rockchip_atomic_commit, work);
257 rockchip_atomic_commit_complete(commit);
260 int rockchip_drm_atomic_commit(struct drm_device *dev,
261 struct drm_atomic_state *state,
264 struct rockchip_drm_private *private = dev->dev_private;
265 struct rockchip_atomic_commit *commit = &private->commit;
268 ret = drm_atomic_helper_prepare_planes(dev, state);
272 /* serialize outstanding asynchronous commits */
273 mutex_lock(&commit->lock);
274 flush_work(&commit->work);
276 drm_atomic_helper_swap_state(dev, state);
279 commit->state = state;
282 schedule_work(&commit->work);
284 rockchip_atomic_commit_complete(commit);
286 mutex_unlock(&commit->lock);
291 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
292 .fb_create = rockchip_user_fb_create,
293 .output_poll_changed = rockchip_drm_output_poll_changed,
294 .atomic_check = drm_atomic_helper_check,
295 .atomic_commit = rockchip_drm_atomic_commit,
298 struct drm_framebuffer *
299 rockchip_drm_framebuffer_init(struct drm_device *dev,
300 struct drm_mode_fb_cmd2 *mode_cmd,
301 struct drm_gem_object *obj)
303 struct drm_framebuffer *fb;
305 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
312 void rockchip_drm_mode_config_init(struct drm_device *dev)
314 dev->mode_config.min_width = 0;
315 dev->mode_config.min_height = 0;
318 * set max width and height as default value(4096x4096).
319 * this value would be used to check framebuffer size limitation
320 * at drm_mode_addfb().
322 dev->mode_config.max_width = 8192;
323 dev->mode_config.max_height = 8192;
325 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;