3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/exynos_drm.h>
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_fbdev.h"
24 #include "exynos_drm_iommu.h"
26 #define MAX_CONNECTOR 4
27 #define PREFERRED_BPP 32
29 #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
32 struct exynos_drm_fbdev {
33 struct drm_fb_helper drm_fb_helper;
34 struct exynos_drm_gem_obj *obj;
37 static int exynos_drm_fb_mmap(struct fb_info *info,
38 struct vm_area_struct *vma)
40 struct drm_fb_helper *helper = info->par;
41 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
42 struct exynos_drm_gem_obj *obj = exynos_fbd->obj;
43 unsigned long vm_size;
46 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
48 vm_size = vma->vm_end - vma->vm_start;
50 if (vm_size > obj->size)
53 ret = dma_mmap_attrs(helper->dev->dev, vma, obj->pages, obj->dma_addr,
54 obj->size, &obj->dma_attrs);
56 DRM_ERROR("failed to mmap.\n");
63 static struct fb_ops exynos_drm_fb_ops = {
65 .fb_mmap = exynos_drm_fb_mmap,
66 .fb_fillrect = drm_fb_helper_cfb_fillrect,
67 .fb_copyarea = drm_fb_helper_cfb_copyarea,
68 .fb_imageblit = drm_fb_helper_cfb_imageblit,
69 .fb_check_var = drm_fb_helper_check_var,
70 .fb_set_par = drm_fb_helper_set_par,
71 .fb_blank = drm_fb_helper_blank,
72 .fb_pan_display = drm_fb_helper_pan_display,
73 .fb_setcmap = drm_fb_helper_setcmap,
76 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
77 struct drm_fb_helper_surface_size *sizes,
78 struct exynos_drm_gem_obj *obj)
81 struct drm_framebuffer *fb = helper->fb;
82 unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
83 unsigned int nr_pages;
86 fbi = drm_fb_helper_alloc_fbi(helper);
88 DRM_ERROR("failed to allocate fb info.\n");
93 fbi->flags = FBINFO_FLAG_DEFAULT;
94 fbi->fbops = &exynos_drm_fb_ops;
96 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
97 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
99 nr_pages = obj->size >> PAGE_SHIFT;
101 obj->kvaddr = (void __iomem *) vmap(obj->pages, nr_pages, VM_MAP,
102 pgprot_writecombine(PAGE_KERNEL));
104 DRM_ERROR("failed to map pages to kernel space.\n");
105 drm_fb_helper_release_fbi(helper);
109 offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
110 offset += fbi->var.yoffset * fb->pitches[0];
112 fbi->screen_base = obj->kvaddr + offset;
113 fbi->screen_size = size;
114 fbi->fix.smem_len = size;
119 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
120 struct drm_fb_helper_surface_size *sizes)
122 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
123 struct exynos_drm_gem_obj *obj;
124 struct drm_device *dev = helper->dev;
125 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
126 struct platform_device *pdev = dev->platformdev;
130 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
131 sizes->surface_width, sizes->surface_height,
134 mode_cmd.width = sizes->surface_width;
135 mode_cmd.height = sizes->surface_height;
136 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
137 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
138 sizes->surface_depth);
140 mutex_lock(&dev->struct_mutex);
142 size = mode_cmd.pitches[0] * mode_cmd.height;
144 obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
146 * If physically contiguous memory allocation fails and if IOMMU is
147 * supported then try to get buffer from non physically contiguous
150 if (IS_ERR(obj) && is_drm_iommu_supported(dev)) {
151 dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
152 obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG, size);
160 exynos_fbdev->obj = obj;
162 helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd, &obj, 1);
163 if (IS_ERR(helper->fb)) {
164 DRM_ERROR("failed to create drm framebuffer.\n");
165 ret = PTR_ERR(helper->fb);
166 goto err_destroy_gem;
169 ret = exynos_drm_fbdev_update(helper, sizes, obj);
171 goto err_destroy_framebuffer;
173 mutex_unlock(&dev->struct_mutex);
176 err_destroy_framebuffer:
177 drm_framebuffer_cleanup(helper->fb);
179 exynos_drm_gem_destroy(obj);
182 * if failed, all resources allocated above would be released by
183 * drm_mode_config_cleanup() when drm_load() had been called prior
184 * to any specific driver such as fimd or hdmi driver.
187 mutex_unlock(&dev->struct_mutex);
191 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
192 .fb_probe = exynos_drm_fbdev_create,
195 static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
197 struct drm_connector *connector;
200 mutex_lock(&dev->mode_config.mutex);
201 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
202 if (connector->status != connector_status_connected)
208 mutex_unlock(&dev->mode_config.mutex);
213 int exynos_drm_fbdev_init(struct drm_device *dev)
215 struct exynos_drm_fbdev *fbdev;
216 struct exynos_drm_private *private = dev->dev_private;
217 struct drm_fb_helper *helper;
218 unsigned int num_crtc;
221 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
224 if (!exynos_drm_fbdev_is_anything_connected(dev))
227 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
231 private->fb_helper = helper = &fbdev->drm_fb_helper;
233 drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
235 num_crtc = dev->mode_config.num_crtc;
237 ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
239 DRM_ERROR("failed to initialize drm fb helper.\n");
243 ret = drm_fb_helper_single_add_all_connectors(helper);
245 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
250 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
252 DRM_ERROR("failed to set up hw configuration.\n");
259 drm_fb_helper_fini(helper);
262 private->fb_helper = NULL;
268 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
269 struct drm_fb_helper *fb_helper)
271 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
272 struct exynos_drm_gem_obj *obj = exynos_fbd->obj;
273 struct drm_framebuffer *fb;
278 /* release drm framebuffer and real buffer */
279 if (fb_helper->fb && fb_helper->fb->funcs) {
282 drm_framebuffer_unregister_private(fb);
283 drm_framebuffer_remove(fb);
287 drm_fb_helper_unregister_fbi(fb_helper);
288 drm_fb_helper_release_fbi(fb_helper);
290 drm_fb_helper_fini(fb_helper);
293 void exynos_drm_fbdev_fini(struct drm_device *dev)
295 struct exynos_drm_private *private = dev->dev_private;
296 struct exynos_drm_fbdev *fbdev;
298 if (!private || !private->fb_helper)
301 fbdev = to_exynos_fbdev(private->fb_helper);
303 exynos_drm_fbdev_destroy(dev, private->fb_helper);
305 private->fb_helper = NULL;
308 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
310 struct exynos_drm_private *private = dev->dev_private;
312 if (!private || !private->fb_helper)
315 drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);