2 * Copyright (C) ROCKCHIP, Inc.
3 * Author:yzq<yzq@rock-chips.com>
5 * based on exynos_drm_fbdev.c
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_crtc_helper.h>
21 #include "rockchip_drm_drv.h"
22 #include "rockchip_drm_fb.h"
23 #include "rockchip_drm_gem.h"
24 #include "rockchip_drm_iommu.h"
26 #define MAX_CONNECTOR 4
27 #define PREFERRED_BPP 32
29 #define to_rockchip_fbdev(x) container_of(x, struct rockchip_drm_fbdev,\
32 struct rockchip_drm_fbdev {
33 struct drm_fb_helper drm_fb_helper;
34 struct rockchip_drm_gem_obj *rockchip_gem_obj;
37 static int rockchip_drm_fb_mmap(struct fb_info *info,
38 struct vm_area_struct *vma)
40 struct drm_fb_helper *helper = info->par;
41 struct rockchip_drm_fbdev *rockchip_fbd = to_rockchip_fbdev(helper);
42 struct rockchip_drm_gem_obj *rockchip_gem_obj = rockchip_fbd->rockchip_gem_obj;
43 struct rockchip_drm_gem_buf *buffer = rockchip_gem_obj->buffer;
44 unsigned long vm_size;
47 DRM_DEBUG_KMS("%s\n", __func__);
49 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
51 vm_size = vma->vm_end - vma->vm_start;
53 if (vm_size > buffer->size)
56 ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
57 buffer->dma_addr, buffer->size, &buffer->dma_attrs);
59 DRM_ERROR("failed to mmap.\n");
66 static struct fb_ops rockchip_drm_fb_ops = {
68 .fb_mmap = rockchip_drm_fb_mmap,
69 .fb_fillrect = cfb_fillrect,
70 .fb_copyarea = cfb_copyarea,
71 .fb_imageblit = cfb_imageblit,
72 .fb_check_var = drm_fb_helper_check_var,
73 .fb_set_par = drm_fb_helper_set_par,
74 .fb_blank = drm_fb_helper_blank,
75 .fb_pan_display = drm_fb_helper_pan_display,
76 .fb_setcmap = drm_fb_helper_setcmap,
79 static int rockchip_drm_fbdev_update(struct drm_fb_helper *helper,
80 struct drm_framebuffer *fb)
82 struct fb_info *fbi = helper->fbdev;
83 struct drm_device *dev = helper->dev;
84 struct rockchip_drm_gem_buf *buffer;
85 unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
88 DRM_DEBUG_KMS("%s\n", __FILE__);
90 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
91 drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
93 /* RGB formats use only one buffer */
94 buffer = rockchip_drm_fb_buffer(fb, 0);
96 DRM_LOG_KMS("buffer is null.\n");
100 /* map pages with kernel virtual space. */
101 if (!buffer->kvaddr) {
102 if (is_drm_iommu_supported(dev)) {
103 unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
105 buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
106 pgprot_writecombine(PAGE_KERNEL));
108 phys_addr_t dma_addr = buffer->dma_addr;
110 buffer->kvaddr = phys_to_virt(dma_addr);
112 buffer->kvaddr = (void __iomem *)NULL;
114 if (!buffer->kvaddr) {
115 DRM_ERROR("failed to map pages to kernel space.\n");
120 /* buffer count to framebuffer always is 1 at booting time. */
121 rockchip_drm_fb_set_buf_cnt(fb, 1);
123 offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
124 offset += fbi->var.yoffset * fb->pitches[0];
126 dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
127 fbi->screen_base = buffer->kvaddr + offset;
128 if (is_drm_iommu_supported(dev))
129 fbi->fix.smem_start = (unsigned long)
130 (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
132 fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
134 fbi->screen_size = size;
135 fbi->fix.smem_len = size;
140 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
141 struct drm_fb_helper_surface_size *sizes)
143 struct rockchip_drm_fbdev *rockchip_fbdev = to_rockchip_fbdev(helper);
144 struct rockchip_drm_gem_obj *rockchip_gem_obj;
145 struct drm_device *dev = helper->dev;
147 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
148 struct platform_device *pdev = dev->platformdev;
152 DRM_DEBUG_KMS("%s\n", __FILE__);
154 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
155 sizes->surface_width, sizes->surface_height,
158 mode_cmd.width = sizes->surface_width;
159 mode_cmd.height = sizes->surface_height;
160 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
161 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
162 sizes->surface_depth);
164 mutex_lock(&dev->struct_mutex);
166 fbi = framebuffer_alloc(0, &pdev->dev);
168 DRM_ERROR("failed to allocate fb info.\n");
173 size = mode_cmd.pitches[0] * mode_cmd.height;
175 /* 0 means to allocate physically continuous memory */
176 rockchip_gem_obj = rockchip_drm_gem_create(dev, 0, size);
177 if (IS_ERR(rockchip_gem_obj)) {
178 ret = PTR_ERR(rockchip_gem_obj);
179 goto err_release_framebuffer;
182 rockchip_fbdev->rockchip_gem_obj = rockchip_gem_obj;
184 helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
185 &rockchip_gem_obj->base);
186 if (IS_ERR(helper->fb)) {
187 DRM_ERROR("failed to create drm framebuffer.\n");
188 ret = PTR_ERR(helper->fb);
189 goto err_destroy_gem;
195 fbi->flags = FBINFO_FLAG_DEFAULT;
196 fbi->fbops = &rockchip_drm_fb_ops;
198 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
200 DRM_ERROR("failed to allocate cmap.\n");
201 goto err_destroy_framebuffer;
204 ret = rockchip_drm_fbdev_update(helper, helper->fb);
206 goto err_dealloc_cmap;
208 mutex_unlock(&dev->struct_mutex);
212 fb_dealloc_cmap(&fbi->cmap);
213 err_destroy_framebuffer:
214 drm_framebuffer_cleanup(helper->fb);
216 rockchip_drm_gem_destroy(rockchip_gem_obj);
217 err_release_framebuffer:
218 framebuffer_release(fbi);
221 * if failed, all resources allocated above would be released by
222 * drm_mode_config_cleanup() when drm_load() had been called prior
223 * to any specific driver such as fimd or hdmi driver.
226 mutex_unlock(&dev->struct_mutex);
230 static struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
231 .fb_probe = rockchip_drm_fbdev_create,
234 int rockchip_drm_fbdev_init(struct drm_device *dev)
236 struct rockchip_drm_fbdev *fbdev;
237 struct rockchip_drm_private *private = dev->dev_private;
238 struct drm_fb_helper *helper;
239 unsigned int num_crtc;
242 DRM_DEBUG_KMS("%s\n", __FILE__);
244 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
247 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
249 DRM_ERROR("failed to allocate drm fbdev.\n");
253 private->fb_helper = helper = &fbdev->drm_fb_helper;
254 helper->funcs = &rockchip_drm_fb_helper_funcs;
256 num_crtc = dev->mode_config.num_crtc;
258 ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
260 DRM_ERROR("failed to initialize drm fb helper.\n");
264 ret = drm_fb_helper_single_add_all_connectors(helper);
266 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
271 /* disable all the possible outputs/crtcs before entering KMS mode */
272 drm_helper_disable_unused_functions(dev);
274 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
276 DRM_ERROR("failed to set up hw configuration.\n");
283 drm_fb_helper_fini(helper);
286 private->fb_helper = NULL;
292 static void rockchip_drm_fbdev_destroy(struct drm_device *dev,
293 struct drm_fb_helper *fb_helper)
295 struct rockchip_drm_fbdev *rockchip_fbd = to_rockchip_fbdev(fb_helper);
296 struct rockchip_drm_gem_obj *rockchip_gem_obj = rockchip_fbd->rockchip_gem_obj;
297 struct drm_framebuffer *fb;
299 if (is_drm_iommu_supported(dev) && rockchip_gem_obj->buffer->kvaddr)
300 vunmap(rockchip_gem_obj->buffer->kvaddr);
302 /* release drm framebuffer and real buffer */
303 if (fb_helper->fb && fb_helper->fb->funcs) {
306 drm_framebuffer_unregister_private(fb);
307 drm_framebuffer_remove(fb);
311 /* release linux framebuffer */
312 if (fb_helper->fbdev) {
313 struct fb_info *info;
316 info = fb_helper->fbdev;
317 ret = unregister_framebuffer(info);
319 DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
322 fb_dealloc_cmap(&info->cmap);
324 framebuffer_release(info);
327 drm_fb_helper_fini(fb_helper);
330 void rockchip_drm_fbdev_fini(struct drm_device *dev)
332 struct rockchip_drm_private *private = dev->dev_private;
333 struct rockchip_drm_fbdev *fbdev;
335 if (!private || !private->fb_helper)
338 fbdev = to_rockchip_fbdev(private->fb_helper);
340 if (fbdev->rockchip_gem_obj)
341 rockchip_drm_gem_destroy(fbdev->rockchip_gem_obj);
343 rockchip_drm_fbdev_destroy(dev, private->fb_helper);
345 private->fb_helper = NULL;
348 void rockchip_drm_fbdev_restore_mode(struct drm_device *dev)
350 struct rockchip_drm_private *private = dev->dev_private;
352 if (!private || !private->fb_helper)
355 drm_modeset_lock_all(dev);
356 drm_fb_helper_restore_fbdev_mode(private->fb_helper);
357 drm_modeset_unlock_all(dev);