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);
212 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
214 struct drm_atomic_state *state = commit->state;
215 struct drm_device *dev = commit->dev;
218 * TODO: do fence wait here.
222 * Rockchip crtc support runtime PM, can't update display planes
223 * when crtc is disabled.
225 * drm_atomic_helper_commit comments detail that:
226 * For drivers supporting runtime PM the recommended sequence is
228 * drm_atomic_helper_commit_modeset_disables(dev, state);
230 * drm_atomic_helper_commit_modeset_enables(dev, state);
232 * drm_atomic_helper_commit_planes(dev, state, true);
234 * See the kerneldoc entries for these three functions for more details.
236 drm_atomic_helper_commit_modeset_disables(dev, state);
238 drm_atomic_helper_commit_modeset_enables(dev, state);
240 drm_atomic_helper_commit_planes(dev, state, true);
242 drm_atomic_helper_wait_for_vblanks(dev, state);
244 drm_atomic_helper_cleanup_planes(dev, state);
246 drm_atomic_state_free(state);
249 void rockchip_drm_atomic_work(struct work_struct *work)
251 struct rockchip_atomic_commit *commit = container_of(work,
252 struct rockchip_atomic_commit, work);
254 rockchip_atomic_commit_complete(commit);
257 int rockchip_drm_atomic_commit(struct drm_device *dev,
258 struct drm_atomic_state *state,
261 struct rockchip_drm_private *private = dev->dev_private;
262 struct rockchip_atomic_commit *commit = &private->commit;
265 ret = drm_atomic_helper_prepare_planes(dev, state);
269 /* serialize outstanding asynchronous commits */
270 mutex_lock(&commit->lock);
271 flush_work(&commit->work);
273 drm_atomic_helper_swap_state(dev, state);
276 commit->state = state;
279 schedule_work(&commit->work);
281 rockchip_atomic_commit_complete(commit);
283 mutex_unlock(&commit->lock);
288 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
289 .fb_create = rockchip_user_fb_create,
290 .output_poll_changed = rockchip_drm_output_poll_changed,
291 .atomic_check = drm_atomic_helper_check,
292 .atomic_commit = rockchip_drm_atomic_commit,
295 struct drm_framebuffer *
296 rockchip_drm_framebuffer_init(struct drm_device *dev,
297 struct drm_mode_fb_cmd2 *mode_cmd,
298 struct drm_gem_object *obj)
300 struct drm_framebuffer *fb;
302 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
309 void rockchip_drm_mode_config_init(struct drm_device *dev)
311 dev->mode_config.min_width = 0;
312 dev->mode_config.min_height = 0;
315 * set max width and height as default value(4096x4096).
316 * this value would be used to check framebuffer size limitation
317 * at drm_mode_addfb().
319 dev->mode_config.max_width = 8192;
320 dev->mode_config.max_height = 8192;
322 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;