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);
61 if (rockchip_fb->sgt) {
62 void *start = phys_to_virt(rockchip_fb->start);
63 void *end = phys_to_virt(rockchip_fb->size);
65 dma_unmap_sg(dev->dev, rockchip_fb->sgt->sgl,
66 rockchip_fb->sgt->nents, DMA_TO_DEVICE);
67 sg_free_table(rockchip_fb->sgt);
68 memblock_free(rockchip_fb->start, rockchip_fb->size);
69 free_reserved_area(start, end, -1, "drm_fb");
72 drm_framebuffer_cleanup(fb);
76 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
77 struct drm_file *file_priv,
80 struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
82 return drm_gem_handle_create(file_priv,
83 rockchip_fb->obj[0], handle);
86 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
87 .destroy = rockchip_drm_fb_destroy,
88 .create_handle = rockchip_drm_fb_create_handle,
91 struct drm_framebuffer *
92 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_gem_object **obj, struct resource *res,
94 unsigned int num_planes)
96 struct rockchip_drm_fb *rockchip_fb;
97 struct rockchip_gem_object *rk_obj;
101 rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
103 return ERR_PTR(-ENOMEM);
105 drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
107 ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
108 &rockchip_drm_fb_funcs);
110 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
116 for (i = 0; i < num_planes; i++)
117 rockchip_fb->obj[i] = obj[i];
119 for (i = 0; i < num_planes; i++) {
120 rk_obj = to_rockchip_obj(obj[i]);
121 rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
124 unsigned long nr_pages;
126 struct sg_table *sgt;
127 DEFINE_DMA_ATTRS(attrs);
128 phys_addr_t start = res->start;
129 phys_addr_t size = res->end - res->start;
131 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
132 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
135 goto err_deinit_drm_fb;
138 while (i < nr_pages) {
139 pages[i] = phys_to_page(start);
143 sgt = drm_prime_pages_to_sg(pages, nr_pages);
147 goto err_deinit_drm_fb;
150 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
151 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
152 dma_map_sg_attrs(dev->dev, sgt->sgl, sgt->nents,
153 DMA_TO_DEVICE, &attrs);
154 rockchip_fb->dma_addr[0] = sg_dma_address(sgt->sgl);
155 rockchip_fb->sgt = sgt;
156 rockchip_fb->start = res->start;
157 rockchip_fb->size = size;
160 dev_err(dev->dev, "Failed to find available buffer\n");
161 goto err_deinit_drm_fb;
164 return &rockchip_fb->fb;
167 drm_framebuffer_cleanup(&rockchip_fb->fb);
173 static struct drm_framebuffer *
174 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
175 struct drm_mode_fb_cmd2 *mode_cmd)
177 struct drm_framebuffer *fb;
178 struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
179 struct drm_gem_object *obj;
186 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
187 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
188 num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
189 ROCKCHIP_MAX_FB_BUFFER);
191 for (i = 0; i < num_planes; i++) {
192 unsigned int width = mode_cmd->width / (i ? hsub : 1);
193 unsigned int height = mode_cmd->height / (i ? vsub : 1);
194 unsigned int min_size;
196 obj = drm_gem_object_lookup(dev, file_priv,
197 mode_cmd->handles[i]);
199 dev_err(dev->dev, "Failed to lookup GEM object\n");
201 goto err_gem_object_unreference;
204 min_size = (height - 1) * mode_cmd->pitches[i] +
205 mode_cmd->offsets[i] +
206 width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
208 if (obj->size < min_size) {
209 drm_gem_object_unreference_unlocked(obj);
211 goto err_gem_object_unreference;
216 fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
219 goto err_gem_object_unreference;
224 err_gem_object_unreference:
225 for (i--; i >= 0; i--)
226 drm_gem_object_unreference_unlocked(objs[i]);
230 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
232 struct rockchip_drm_private *private = dev->dev_private;
233 struct drm_fb_helper *fb_helper = &private->fbdev_helper;
236 drm_fb_helper_hotplug_event(fb_helper);
239 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
241 struct rockchip_drm_private *priv = crtc->dev->dev_private;
242 int pipe = drm_crtc_index(crtc);
243 const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
245 if (crtc_funcs && crtc_funcs->wait_for_update)
246 crtc_funcs->wait_for_update(crtc);
250 * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
251 * have hardware counters for neither vblanks nor scanlines, which results in
253 * | <-- HW vsync irq and reg take effect
255 * get_vblank and wait --> |
256 * | <-- handle_vblank, vblank->count + 1
259 * | <-- HW vsync irq and reg take effect
261 * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
262 * of waiting for vblank_count to change.
265 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
267 struct drm_crtc_state *old_crtc_state;
268 struct drm_crtc *crtc;
271 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
272 /* No one cares about the old state, so abuse it for tracking
273 * and store whether we hold a vblank reference (and should do a
274 * vblank wait) in the ->enable boolean.
276 old_crtc_state->enable = false;
278 if (!crtc->state->active)
281 if (!drm_atomic_helper_framebuffer_changed(dev,
285 ret = drm_crtc_vblank_get(crtc);
289 old_crtc_state->enable = true;
292 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
293 if (!old_crtc_state->enable)
296 rockchip_crtc_wait_for_update(crtc);
297 drm_crtc_vblank_put(crtc);
302 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
304 struct drm_atomic_state *state = commit->state;
305 struct drm_device *dev = commit->dev;
308 * TODO: do fence wait here.
312 * Rockchip crtc support runtime PM, can't update display planes
313 * when crtc is disabled.
315 * drm_atomic_helper_commit comments detail that:
316 * For drivers supporting runtime PM the recommended sequence is
318 * drm_atomic_helper_commit_modeset_disables(dev, state);
320 * drm_atomic_helper_commit_modeset_enables(dev, state);
322 * drm_atomic_helper_commit_planes(dev, state, true);
324 * See the kerneldoc entries for these three functions for more details.
326 drm_atomic_helper_commit_modeset_disables(dev, state);
328 drm_atomic_helper_commit_modeset_enables(dev, state);
330 drm_atomic_helper_commit_planes(dev, state, true);
332 rockchip_atomic_wait_for_complete(dev, state);
334 drm_atomic_helper_cleanup_planes(dev, state);
336 drm_atomic_state_free(state);
339 void rockchip_drm_atomic_work(struct work_struct *work)
341 struct rockchip_atomic_commit *commit = container_of(work,
342 struct rockchip_atomic_commit, work);
344 rockchip_atomic_commit_complete(commit);
347 int rockchip_drm_atomic_commit(struct drm_device *dev,
348 struct drm_atomic_state *state,
351 struct rockchip_drm_private *private = dev->dev_private;
352 struct rockchip_atomic_commit *commit = &private->commit;
355 ret = drm_atomic_helper_prepare_planes(dev, state);
359 /* serialize outstanding asynchronous commits */
360 mutex_lock(&commit->lock);
361 flush_work(&commit->work);
363 drm_atomic_helper_swap_state(dev, state);
366 commit->state = state;
369 schedule_work(&commit->work);
371 rockchip_atomic_commit_complete(commit);
373 mutex_unlock(&commit->lock);
378 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
379 .fb_create = rockchip_user_fb_create,
380 .output_poll_changed = rockchip_drm_output_poll_changed,
381 .atomic_check = drm_atomic_helper_check,
382 .atomic_commit = rockchip_drm_atomic_commit,
385 struct drm_framebuffer *
386 rockchip_drm_framebuffer_init(struct drm_device *dev,
387 struct drm_mode_fb_cmd2 *mode_cmd,
388 struct drm_gem_object *obj)
390 struct drm_framebuffer *fb;
392 fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
399 void rockchip_drm_mode_config_init(struct drm_device *dev)
401 dev->mode_config.min_width = 0;
402 dev->mode_config.min_height = 0;
405 * set max width and height as default value(4096x4096).
406 * this value would be used to check framebuffer size limitation
407 * at drm_mode_addfb().
409 dev->mode_config.max_width = 4096;
410 dev->mode_config.max_height = 4096;
412 dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;