blob: d771b467cf0c03c70446f58bc68d3029dd5f6305 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_fbdev.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Authors:
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
8 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * 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.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc.h>
17#include <drm/drm_fb_helper.h>
18#include <drm/drm_crtc_helper.h>
Vikas Sajjana1bfacf2013-08-06 17:22:04 +053019#include <drm/exynos_drm.h>
Inki Dae1c248b72011-10-04 19:19:01 +090020
21#include "exynos_drm_drv.h"
22#include "exynos_drm_fb.h"
Mark Browne30655d2013-08-13 00:46:40 +010023#include "exynos_drm_fbdev.h"
Inki Dae2c871122011-11-12 15:23:32 +090024#include "exynos_drm_gem.h"
Inki Daec704f1b2012-12-21 17:59:20 +090025#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090026
27#define MAX_CONNECTOR 4
28#define PREFERRED_BPP 32
29
30#define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
31 drm_fb_helper)
32
33struct exynos_drm_fbdev {
Joonyoung Shime1533c02011-12-13 14:46:57 +090034 struct drm_fb_helper drm_fb_helper;
35 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +090036};
37
Prathyush Kdd265852012-11-19 13:55:28 +053038static int exynos_drm_fb_mmap(struct fb_info *info,
39 struct vm_area_struct *vma)
40{
41 struct drm_fb_helper *helper = info->par;
42 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
43 struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
44 struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
45 unsigned long vm_size;
46 int ret;
47
Prathyush Kdd265852012-11-19 13:55:28 +053048 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
49
50 vm_size = vma->vm_end - vma->vm_start;
51
52 if (vm_size > buffer->size)
53 return -EINVAL;
54
Inki Dae4744ad22012-12-07 17:51:27 +090055 ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
Prathyush Kdd265852012-11-19 13:55:28 +053056 buffer->dma_addr, buffer->size, &buffer->dma_attrs);
57 if (ret < 0) {
58 DRM_ERROR("failed to mmap.\n");
59 return ret;
60 }
61
62 return 0;
63}
64
Inki Dae1c248b72011-10-04 19:19:01 +090065static struct fb_ops exynos_drm_fb_ops = {
66 .owner = THIS_MODULE,
Prathyush Kdd265852012-11-19 13:55:28 +053067 .fb_mmap = exynos_drm_fb_mmap,
Inki Dae1c248b72011-10-04 19:19:01 +090068 .fb_fillrect = cfb_fillrect,
69 .fb_copyarea = cfb_copyarea,
70 .fb_imageblit = cfb_imageblit,
71 .fb_check_var = drm_fb_helper_check_var,
Sascha Hauer83b316f2012-02-01 11:38:37 +010072 .fb_set_par = drm_fb_helper_set_par,
Inki Dae1c248b72011-10-04 19:19:01 +090073 .fb_blank = drm_fb_helper_blank,
74 .fb_pan_display = drm_fb_helper_pan_display,
75 .fb_setcmap = drm_fb_helper_setcmap,
76};
77
Inki Dae19c8b832011-10-14 13:29:46 +090078static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +090079 struct drm_framebuffer *fb)
Inki Dae1c248b72011-10-04 19:19:01 +090080{
81 struct fb_info *fbi = helper->fbdev;
82 struct drm_device *dev = helper->dev;
Inki Dae2c871122011-11-12 15:23:32 +090083 struct exynos_drm_gem_buf *buffer;
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +090084 unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
Inki Dae19c8b832011-10-14 13:29:46 +090085 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +090086
Ville Syrjälä01f2c772011-12-20 00:06:49 +020087 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +090088 drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
Inki Dae1c248b72011-10-04 19:19:01 +090089
Seung-Woo Kim229d3532011-12-15 14:36:22 +090090 /* RGB formats use only one buffer */
91 buffer = exynos_drm_fb_buffer(fb, 0);
Inki Dae2c871122011-11-12 15:23:32 +090092 if (!buffer) {
Lespiau, Damien133dcde2014-03-24 15:53:10 +000093 DRM_DEBUG_KMS("buffer is null.\n");
Inki Dae19c8b832011-10-14 13:29:46 +090094 return -EFAULT;
95 }
Inki Dae1c248b72011-10-04 19:19:01 +090096
Inki Dae4744ad22012-12-07 17:51:27 +090097 /* map pages with kernel virtual space. */
98 if (!buffer->kvaddr) {
Inki Daec704f1b2012-12-21 17:59:20 +090099 if (is_drm_iommu_supported(dev)) {
100 unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
101
Sachin Kamatfafb3832013-09-05 17:01:36 +0530102 buffer->kvaddr = (void __iomem *) vmap(buffer->pages,
103 nr_pages, VM_MAP,
Inki Dae4744ad22012-12-07 17:51:27 +0900104 pgprot_writecombine(PAGE_KERNEL));
Inki Daec704f1b2012-12-21 17:59:20 +0900105 } else {
106 phys_addr_t dma_addr = buffer->dma_addr;
107 if (dma_addr)
Sachin Kamatfafb3832013-09-05 17:01:36 +0530108 buffer->kvaddr = (void __iomem *)phys_to_virt(dma_addr);
Inki Daec704f1b2012-12-21 17:59:20 +0900109 else
110 buffer->kvaddr = (void __iomem *)NULL;
111 }
Inki Dae4744ad22012-12-07 17:51:27 +0900112 if (!buffer->kvaddr) {
113 DRM_ERROR("failed to map pages to kernel space.\n");
114 return -EIO;
115 }
116 }
117
Inki Dae01ed8122012-08-20 20:05:56 +0900118 /* buffer count to framebuffer always is 1 at booting time. */
119 exynos_drm_fb_set_buf_cnt(fb, 1);
120
Inki Dae19c8b832011-10-14 13:29:46 +0900121 offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200122 offset += fbi->var.yoffset * fb->pitches[0];
Inki Dae1c248b72011-10-04 19:19:01 +0900123
Inki Dae2c871122011-11-12 15:23:32 +0900124 fbi->screen_base = buffer->kvaddr + offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900125 fbi->screen_size = size;
Inki Dae19c8b832011-10-14 13:29:46 +0900126
127 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900128}
129
130static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
131 struct drm_fb_helper_surface_size *sizes)
132{
133 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
Joonyoung Shime1533c02011-12-13 14:46:57 +0900134 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900135 struct drm_device *dev = helper->dev;
136 struct fb_info *fbi;
Joonyoung Shima794d572011-12-08 15:05:19 +0900137 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Inki Dae1c248b72011-10-04 19:19:01 +0900138 struct platform_device *pdev = dev->platformdev;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900139 unsigned long size;
Inki Dae1c248b72011-10-04 19:19:01 +0900140 int ret;
141
Inki Dae1c248b72011-10-04 19:19:01 +0900142 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
143 sizes->surface_width, sizes->surface_height,
144 sizes->surface_bpp);
145
146 mode_cmd.width = sizes->surface_width;
147 mode_cmd.height = sizes->surface_height;
Joonyoung Shima794d572011-12-08 15:05:19 +0900148 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
149 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
150 sizes->surface_depth);
Inki Dae1c248b72011-10-04 19:19:01 +0900151
152 mutex_lock(&dev->struct_mutex);
153
154 fbi = framebuffer_alloc(0, &pdev->dev);
155 if (!fbi) {
156 DRM_ERROR("failed to allocate fb info.\n");
157 ret = -ENOMEM;
158 goto out;
159 }
160
Joonyoung Shime1533c02011-12-13 14:46:57 +0900161 size = mode_cmd.pitches[0] * mode_cmd.height;
Inki Dae2b358922012-03-16 18:47:05 +0900162
Vikas Sajjana1bfacf2013-08-06 17:22:04 +0530163 exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
164 /*
165 * If physically contiguous memory allocation fails and if IOMMU is
166 * supported then try to get buffer from non physically contiguous
167 * memory area.
168 */
169 if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
170 dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
171 exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
172 size);
173 }
174
Joonyoung Shime1533c02011-12-13 14:46:57 +0900175 if (IS_ERR(exynos_gem_obj)) {
176 ret = PTR_ERR(exynos_gem_obj);
Inki Dae662aa6d2012-12-07 18:06:43 +0900177 goto err_release_framebuffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900178 }
179
Joonyoung Shime1533c02011-12-13 14:46:57 +0900180 exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
181
182 helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
183 &exynos_gem_obj->base);
Sachin Kamat41eab402013-04-29 12:27:05 +0530184 if (IS_ERR(helper->fb)) {
Joonyoung Shime1533c02011-12-13 14:46:57 +0900185 DRM_ERROR("failed to create drm framebuffer.\n");
186 ret = PTR_ERR(helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900187 goto err_destroy_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900188 }
189
Inki Dae1c248b72011-10-04 19:19:01 +0900190 helper->fbdev = fbi;
191
192 fbi->par = helper;
193 fbi->flags = FBINFO_FLAG_DEFAULT;
194 fbi->fbops = &exynos_drm_fb_ops;
195
196 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
197 if (ret) {
198 DRM_ERROR("failed to allocate cmap.\n");
Inki Dae662aa6d2012-12-07 18:06:43 +0900199 goto err_destroy_framebuffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900200 }
201
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +0900202 ret = exynos_drm_fbdev_update(helper, helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900203 if (ret < 0)
204 goto err_dealloc_cmap;
205
206 mutex_unlock(&dev->struct_mutex);
207 return ret;
208
209err_dealloc_cmap:
210 fb_dealloc_cmap(&fbi->cmap);
211err_destroy_framebuffer:
212 drm_framebuffer_cleanup(helper->fb);
213err_destroy_gem:
214 exynos_drm_gem_destroy(exynos_gem_obj);
215err_release_framebuffer:
216 framebuffer_release(fbi);
Inki Dae1c248b72011-10-04 19:19:01 +0900217
218/*
219 * if failed, all resources allocated above would be released by
220 * drm_mode_config_cleanup() when drm_load() had been called prior
221 * to any specific driver such as fimd or hdmi driver.
222 */
223out:
224 mutex_unlock(&dev->struct_mutex);
225 return ret;
226}
227
Inki Dae1c248b72011-10-04 19:19:01 +0900228static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
Daniel Vettercd5428a2013-01-21 23:42:49 +0100229 .fb_probe = exynos_drm_fbdev_create,
Inki Dae1c248b72011-10-04 19:19:01 +0900230};
231
Jingoo Han9230ffc2014-04-17 19:07:22 +0900232static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
Andrzej Hajda3eb578e2014-03-28 12:52:38 +0100233{
234 struct drm_connector *connector;
235 bool ret = false;
236
237 mutex_lock(&dev->mode_config.mutex);
238 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
239 if (connector->status != connector_status_connected)
240 continue;
241
242 ret = true;
243 break;
244 }
245 mutex_unlock(&dev->mode_config.mutex);
246
247 return ret;
248}
249
Inki Dae1c248b72011-10-04 19:19:01 +0900250int exynos_drm_fbdev_init(struct drm_device *dev)
251{
252 struct exynos_drm_fbdev *fbdev;
253 struct exynos_drm_private *private = dev->dev_private;
254 struct drm_fb_helper *helper;
255 unsigned int num_crtc;
256 int ret;
257
Inki Dae1c248b72011-10-04 19:19:01 +0900258 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
259 return 0;
260
Andrzej Hajda3eb578e2014-03-28 12:52:38 +0100261 if (!exynos_drm_fbdev_is_anything_connected(dev))
262 return 0;
263
Inki Dae1c248b72011-10-04 19:19:01 +0900264 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900265 if (!fbdev)
Inki Dae1c248b72011-10-04 19:19:01 +0900266 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +0900267
268 private->fb_helper = helper = &fbdev->drm_fb_helper;
269 helper->funcs = &exynos_drm_fb_helper_funcs;
270
271 num_crtc = dev->mode_config.num_crtc;
272
273 ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
274 if (ret < 0) {
275 DRM_ERROR("failed to initialize drm fb helper.\n");
276 goto err_init;
277 }
278
279 ret = drm_fb_helper_single_add_all_connectors(helper);
280 if (ret < 0) {
281 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
282 goto err_setup;
283
284 }
285
Daniel Vetter76a39db2013-01-20 23:12:54 +0100286 /* disable all the possible outputs/crtcs before entering KMS mode */
287 drm_helper_disable_unused_functions(dev);
288
Inki Dae1c248b72011-10-04 19:19:01 +0900289 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
290 if (ret < 0) {
291 DRM_ERROR("failed to set up hw configuration.\n");
292 goto err_setup;
293 }
294
295 return 0;
296
297err_setup:
298 drm_fb_helper_fini(helper);
299
300err_init:
301 private->fb_helper = NULL;
302 kfree(fbdev);
303
304 return ret;
305}
306
307static void exynos_drm_fbdev_destroy(struct drm_device *dev,
308 struct drm_fb_helper *fb_helper)
309{
Inki Dae4744ad22012-12-07 17:51:27 +0900310 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
311 struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900312 struct drm_framebuffer *fb;
313
Inki Daec704f1b2012-12-21 17:59:20 +0900314 if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
Inki Dae4744ad22012-12-07 17:51:27 +0900315 vunmap(exynos_gem_obj->buffer->kvaddr);
316
Inki Dae1c248b72011-10-04 19:19:01 +0900317 /* release drm framebuffer and real buffer */
318 if (fb_helper->fb && fb_helper->fb->funcs) {
319 fb = fb_helper->fb;
Daniel Vetter36206362012-12-10 20:42:17 +0100320 if (fb) {
321 drm_framebuffer_unregister_private(fb);
Rob Clarkf7eff602012-09-05 21:48:38 +0000322 drm_framebuffer_remove(fb);
Daniel Vetter36206362012-12-10 20:42:17 +0100323 }
Inki Dae1c248b72011-10-04 19:19:01 +0900324 }
325
326 /* release linux framebuffer */
327 if (fb_helper->fbdev) {
328 struct fb_info *info;
329 int ret;
330
331 info = fb_helper->fbdev;
332 ret = unregister_framebuffer(info);
333 if (ret < 0)
334 DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
335
336 if (info->cmap.len)
337 fb_dealloc_cmap(&info->cmap);
338
339 framebuffer_release(info);
340 }
341
342 drm_fb_helper_fini(fb_helper);
343}
344
345void exynos_drm_fbdev_fini(struct drm_device *dev)
346{
347 struct exynos_drm_private *private = dev->dev_private;
348 struct exynos_drm_fbdev *fbdev;
349
350 if (!private || !private->fb_helper)
351 return;
352
353 fbdev = to_exynos_fbdev(private->fb_helper);
354
Joonyoung Shime1533c02011-12-13 14:46:57 +0900355 if (fbdev->exynos_gem_obj)
356 exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
357
Inki Dae1c248b72011-10-04 19:19:01 +0900358 exynos_drm_fbdev_destroy(dev, private->fb_helper);
359 kfree(fbdev);
360 private->fb_helper = NULL;
361}
362
363void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
364{
365 struct exynos_drm_private *private = dev->dev_private;
366
367 if (!private || !private->fb_helper)
368 return;
369
Rob Clark5ea1f752014-05-30 12:29:48 -0400370 drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
Inki Dae1c248b72011-10-04 19:19:01 +0900371}