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];
37 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
38 unsigned int plane, struct device *dev)
40 struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
42 if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
45 return rk_fb->dma_addr[plane];
48 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
50 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
51 struct drm_device *dev = fb->dev;
52 struct drm_gem_object *obj;
55 for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
56 obj = rockchip_fb->obj[i];
58 drm_gem_object_unreference_unlocked(obj);
62 if (rockchip_fb->sgt) {
63 void *start = phys_to_virt(rockchip_fb->start);
64 void *end = phys_to_virt(rockchip_fb->size);
66 dma_unmap_sg(dev->dev, rockchip_fb->sgt->sgl,
67 rockchip_fb->sgt->nents, DMA_TO_DEVICE);
68 sg_free_table(rockchip_fb->sgt);
69 memblock_free(rockchip_fb->start, rockchip_fb->size);
70 free_reserved_area(start, end, -1, "drm_fb");
73 WARN_ON(rockchip_fb->sgt);
76 drm_framebuffer_cleanup(fb);
80 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
81 struct drm_file *file_priv,
84 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
86 return drm_gem_handle_create(file_priv,
87 rockchip_fb->obj[0], handle);
90 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
91 .destroy = rockchip_drm_fb_destroy,
92 .create_handle = rockchip_drm_fb_create_handle,
95 struct drm_framebuffer *
96 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
97 struct drm_gem_object **obj, struct resource *res,
98 unsigned int num_planes)
100 struct rockchip_drm_fb *rockchip_fb;
101 struct rockchip_gem_object *rk_obj;
105 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
107 return ERR_PTR(-ENOMEM);
109 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
111 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
112 &rockchip_drm_fb_funcs);
114 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
120 for (i = 0; i < num_planes; i++)
121 rockchip_fb->obj[i] = obj[i];
123 for (i = 0; i < num_planes; i++) {
124 rk_obj = to_rockchip_obj(obj[i]);
125 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
129 unsigned long nr_pages;
131 struct sg_table *sgt;
132 DEFINE_DMA_ATTRS(attrs);
133 phys_addr_t start = res->start;
134 phys_addr_t size = res->end - res->start;
136 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
137 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
140 goto err_deinit_drm_fb;
143 while (i < nr_pages) {
144 pages[i] = phys_to_page(start);
148 sgt = drm_prime_pages_to_sg(pages, nr_pages);
152 goto err_deinit_drm_fb;
155 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
156 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
157 dma_map_sg_attrs(dev->dev, sgt->sgl, sgt->nents,
158 DMA_TO_DEVICE, &attrs);
159 rockchip_fb->dma_addr[0] = sg_dma_address(sgt->sgl);
160 rockchip_fb->sgt = sgt;
161 rockchip_fb->start = res->start;
162 rockchip_fb->size = size;
166 dev_err(dev->dev, "Failed to find available buffer\n");
167 goto err_deinit_drm_fb;
170 return &rockchip_fb->fb;
173 drm_framebuffer_cleanup(&rockchip_fb->fb);
179 static struct drm_framebuffer *
180 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
181 struct drm_mode_fb_cmd2 *mode_cmd)
183 struct drm_framebuffer *fb;
184 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
185 struct drm_gem_object *obj;
192 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
193 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
194 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
195 ROCKCHIP_MAX_FB_BUFFER);
197 for (i = 0; i < num_planes; i++) {
198 unsigned int width = mode_cmd->width / (i ? hsub : 1);
199 unsigned int height = mode_cmd->height / (i ? vsub : 1);
200 unsigned int min_size;
202 obj = drm_gem_object_lookup(dev, file_priv,
203 mode_cmd->handles[i]);
205 dev_err(dev->dev, "Failed to lookup GEM object\n");
207 goto err_gem_object_unreference;
210 min_size = (height - 1) * mode_cmd->pitches[i] +
211 mode_cmd->offsets[i] +
212 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
214 if (obj->size < min_size) {
215 drm_gem_object_unreference_unlocked(obj);
217 goto err_gem_object_unreference;
222 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
225 goto err_gem_object_unreference;
230 err_gem_object_unreference:
231 for (i--; i >= 0; i--)
232 drm_gem_object_unreference_unlocked(objs[i]);
236 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
238 struct rockchip_drm_private *private = dev->dev_private;
239 struct drm_fb_helper *fb_helper = private->fbdev_helper;
242 drm_fb_helper_hotplug_event(fb_helper);
245 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
247 struct rockchip_drm_private *priv = crtc->dev->dev_private;
248 int pipe = drm_crtc_index(crtc);
249 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
251 if (crtc_funcs && crtc_funcs->wait_for_update)
252 crtc_funcs->wait_for_update(crtc);
256 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
257 * have hardware counters for neither vblanks nor scanlines, which results in
259 * | <-- HW vsync irq and reg take effect
261 * get_vblank and wait --> |
262 * | <-- handle_vblank, vblank->count + 1
265 * | <-- HW vsync irq and reg take effect
267 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
268 * of waiting for vblank_count to change.
271 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
273 struct drm_crtc_state *old_crtc_state;
274 struct drm_crtc *crtc;
277 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
278 /* No one cares about the old state, so abuse it for tracking
279 * and store whether we hold a vblank reference (and should do a
280 * vblank wait) in the ->enable boolean.
282 old_crtc_state->enable = false;
284 if (!crtc->state->active)
287 if (!drm_atomic_helper_framebuffer_changed(dev,
291 ret = drm_crtc_vblank_get(crtc);
295 old_crtc_state->enable = true;
298 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
299 if (!old_crtc_state->enable)
302 rockchip_crtc_wait_for_update(crtc);
303 drm_crtc_vblank_put(crtc);
308 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
310 struct drm_atomic_state *state = commit->state;
311 struct drm_device *dev = commit->dev;
314 * TODO: do fence wait here.
318 * Rockchip crtc support runtime PM, can't update display planes
319 * when crtc is disabled.
321 * drm_atomic_helper_commit comments detail that:
322 * For drivers supporting runtime PM the recommended sequence is
324 * drm_atomic_helper_commit_modeset_disables(dev, state);
326 * drm_atomic_helper_commit_modeset_enables(dev, state);
328 * drm_atomic_helper_commit_planes(dev, state, true);
330 * See the kerneldoc entries for these three functions for more details.
332 drm_atomic_helper_commit_modeset_disables(dev, state);
334 drm_atomic_helper_commit_modeset_enables(dev, state);
336 drm_atomic_helper_commit_planes(dev, state, true);
338 rockchip_atomic_wait_for_complete(dev, state);
340 drm_atomic_helper_cleanup_planes(dev, state);
342 drm_atomic_state_free(state);
345 void rockchip_drm_atomic_work(struct work_struct *work)
347 struct rockchip_atomic_commit *commit = container_of(work,
348 struct rockchip_atomic_commit, work);
350 rockchip_atomic_commit_complete(commit);
353 int rockchip_drm_atomic_commit(struct drm_device *dev,
354 struct drm_atomic_state *state,
357 struct rockchip_drm_private *private = dev->dev_private;
358 struct rockchip_atomic_commit *commit = &private->commit;
361 ret = drm_atomic_helper_prepare_planes(dev, state);
365 /* serialize outstanding asynchronous commits */
366 mutex_lock(&commit->lock);
367 flush_work(&commit->work);
369 drm_atomic_helper_swap_state(dev, state);
372 commit->state = state;
375 schedule_work(&commit->work);
377 rockchip_atomic_commit_complete(commit);
379 mutex_unlock(&commit->lock);
384 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
385 .fb_create = rockchip_user_fb_create,
386 .output_poll_changed = rockchip_drm_output_poll_changed,
387 .atomic_check = drm_atomic_helper_check,
388 .atomic_commit = rockchip_drm_atomic_commit,
391 struct drm_framebuffer *
392 rockchip_drm_framebuffer_init(struct drm_device *dev,
393 struct drm_mode_fb_cmd2 *mode_cmd,
394 struct drm_gem_object *obj)
396 struct drm_framebuffer *fb;
398 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
405 void rockchip_drm_mode_config_init(struct drm_device *dev)
407 dev->mode_config.min_width = 0;
408 dev->mode_config.min_height = 0;
411 * set max width and height as default value(4096x4096).
412 * this value would be used to check framebuffer size limitation
413 * at drm_mode_addfb().
415 dev->mode_config.max_width = 4096;
416 dev->mode_config.max_height = 4096;
418 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;