blob: eae064a3ee3072029a46d7e9ae2fdd4bc5ae1eee [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 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/drm_crtc.h>
31#include <drm/drm_fb_helper.h>
32#include <drm/drm_crtc_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090033
34#include "exynos_drm_drv.h"
35#include "exynos_drm_fb.h"
Inki Dae2c871122011-11-12 15:23:32 +090036#include "exynos_drm_gem.h"
Inki Daec704f1b2012-12-21 17:59:20 +090037#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090038
39#define MAX_CONNECTOR 4
40#define PREFERRED_BPP 32
41
42#define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
43 drm_fb_helper)
44
45struct exynos_drm_fbdev {
Joonyoung Shime1533c02011-12-13 14:46:57 +090046 struct drm_fb_helper drm_fb_helper;
47 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +090048};
49
Prathyush Kdd265852012-11-19 13:55:28 +053050static int exynos_drm_fb_mmap(struct fb_info *info,
51 struct vm_area_struct *vma)
52{
53 struct drm_fb_helper *helper = info->par;
54 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
55 struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
56 struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
57 unsigned long vm_size;
58 int ret;
59
60 DRM_DEBUG_KMS("%s\n", __func__);
61
62 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
63
64 vm_size = vma->vm_end - vma->vm_start;
65
66 if (vm_size > buffer->size)
67 return -EINVAL;
68
Inki Dae4744ad22012-12-07 17:51:27 +090069 ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
Prathyush Kdd265852012-11-19 13:55:28 +053070 buffer->dma_addr, buffer->size, &buffer->dma_attrs);
71 if (ret < 0) {
72 DRM_ERROR("failed to mmap.\n");
73 return ret;
74 }
75
76 return 0;
77}
78
Inki Dae1c248b72011-10-04 19:19:01 +090079static struct fb_ops exynos_drm_fb_ops = {
80 .owner = THIS_MODULE,
Prathyush Kdd265852012-11-19 13:55:28 +053081 .fb_mmap = exynos_drm_fb_mmap,
Inki Dae1c248b72011-10-04 19:19:01 +090082 .fb_fillrect = cfb_fillrect,
83 .fb_copyarea = cfb_copyarea,
84 .fb_imageblit = cfb_imageblit,
85 .fb_check_var = drm_fb_helper_check_var,
Sascha Hauer83b316f2012-02-01 11:38:37 +010086 .fb_set_par = drm_fb_helper_set_par,
Inki Dae1c248b72011-10-04 19:19:01 +090087 .fb_blank = drm_fb_helper_blank,
88 .fb_pan_display = drm_fb_helper_pan_display,
89 .fb_setcmap = drm_fb_helper_setcmap,
90};
91
Inki Dae19c8b832011-10-14 13:29:46 +090092static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +090093 struct drm_framebuffer *fb)
Inki Dae1c248b72011-10-04 19:19:01 +090094{
95 struct fb_info *fbi = helper->fbdev;
96 struct drm_device *dev = helper->dev;
Inki Dae2c871122011-11-12 15:23:32 +090097 struct exynos_drm_gem_buf *buffer;
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +090098 unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
Inki Dae19c8b832011-10-14 13:29:46 +090099 unsigned long offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900100
101 DRM_DEBUG_KMS("%s\n", __FILE__);
102
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200103 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +0900104 drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
Inki Dae1c248b72011-10-04 19:19:01 +0900105
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900106 /* RGB formats use only one buffer */
107 buffer = exynos_drm_fb_buffer(fb, 0);
Inki Dae2c871122011-11-12 15:23:32 +0900108 if (!buffer) {
109 DRM_LOG_KMS("buffer is null.\n");
Inki Dae19c8b832011-10-14 13:29:46 +0900110 return -EFAULT;
111 }
Inki Dae1c248b72011-10-04 19:19:01 +0900112
Inki Dae4744ad22012-12-07 17:51:27 +0900113 /* map pages with kernel virtual space. */
114 if (!buffer->kvaddr) {
Inki Daec704f1b2012-12-21 17:59:20 +0900115 if (is_drm_iommu_supported(dev)) {
116 unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
117
118 buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
Inki Dae4744ad22012-12-07 17:51:27 +0900119 pgprot_writecombine(PAGE_KERNEL));
Inki Daec704f1b2012-12-21 17:59:20 +0900120 } else {
121 phys_addr_t dma_addr = buffer->dma_addr;
122 if (dma_addr)
123 buffer->kvaddr = phys_to_virt(dma_addr);
124 else
125 buffer->kvaddr = (void __iomem *)NULL;
126 }
Inki Dae4744ad22012-12-07 17:51:27 +0900127 if (!buffer->kvaddr) {
128 DRM_ERROR("failed to map pages to kernel space.\n");
129 return -EIO;
130 }
131 }
132
Inki Dae01ed8122012-08-20 20:05:56 +0900133 /* buffer count to framebuffer always is 1 at booting time. */
134 exynos_drm_fb_set_buf_cnt(fb, 1);
135
Inki Dae19c8b832011-10-14 13:29:46 +0900136 offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200137 offset += fbi->var.yoffset * fb->pitches[0];
Inki Dae1c248b72011-10-04 19:19:01 +0900138
Inki Dae2c871122011-11-12 15:23:32 +0900139 dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
140 fbi->screen_base = buffer->kvaddr + offset;
Inki Daec704f1b2012-12-21 17:59:20 +0900141 if (is_drm_iommu_supported(dev))
142 fbi->fix.smem_start = (unsigned long)
Prathyush K640631d2012-11-22 12:18:35 +0530143 (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
Inki Daec704f1b2012-12-21 17:59:20 +0900144 else
145 fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
146
Inki Dae1c248b72011-10-04 19:19:01 +0900147 fbi->screen_size = size;
Inki Dae1c248b72011-10-04 19:19:01 +0900148 fbi->fix.smem_len = size;
Inki Dae19c8b832011-10-14 13:29:46 +0900149
150 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900151}
152
153static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
154 struct drm_fb_helper_surface_size *sizes)
155{
156 struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
Joonyoung Shime1533c02011-12-13 14:46:57 +0900157 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900158 struct drm_device *dev = helper->dev;
159 struct fb_info *fbi;
Joonyoung Shima794d572011-12-08 15:05:19 +0900160 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Inki Dae1c248b72011-10-04 19:19:01 +0900161 struct platform_device *pdev = dev->platformdev;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900162 unsigned long size;
Inki Dae1c248b72011-10-04 19:19:01 +0900163 int ret;
164
165 DRM_DEBUG_KMS("%s\n", __FILE__);
166
167 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
168 sizes->surface_width, sizes->surface_height,
169 sizes->surface_bpp);
170
171 mode_cmd.width = sizes->surface_width;
172 mode_cmd.height = sizes->surface_height;
Joonyoung Shima794d572011-12-08 15:05:19 +0900173 mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
174 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
175 sizes->surface_depth);
Inki Dae1c248b72011-10-04 19:19:01 +0900176
177 mutex_lock(&dev->struct_mutex);
178
179 fbi = framebuffer_alloc(0, &pdev->dev);
180 if (!fbi) {
181 DRM_ERROR("failed to allocate fb info.\n");
182 ret = -ENOMEM;
183 goto out;
184 }
185
Joonyoung Shime1533c02011-12-13 14:46:57 +0900186 size = mode_cmd.pitches[0] * mode_cmd.height;
Inki Dae2b358922012-03-16 18:47:05 +0900187
188 /* 0 means to allocate physically continuous memory */
189 exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
Joonyoung Shime1533c02011-12-13 14:46:57 +0900190 if (IS_ERR(exynos_gem_obj)) {
191 ret = PTR_ERR(exynos_gem_obj);
Inki Dae662aa6d2012-12-07 18:06:43 +0900192 goto err_release_framebuffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900193 }
194
Joonyoung Shime1533c02011-12-13 14:46:57 +0900195 exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
196
197 helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
198 &exynos_gem_obj->base);
199 if (IS_ERR_OR_NULL(helper->fb)) {
200 DRM_ERROR("failed to create drm framebuffer.\n");
201 ret = PTR_ERR(helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900202 goto err_destroy_gem;
Joonyoung Shime1533c02011-12-13 14:46:57 +0900203 }
204
Inki Dae1c248b72011-10-04 19:19:01 +0900205 helper->fbdev = fbi;
206
207 fbi->par = helper;
208 fbi->flags = FBINFO_FLAG_DEFAULT;
209 fbi->fbops = &exynos_drm_fb_ops;
210
211 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
212 if (ret) {
213 DRM_ERROR("failed to allocate cmap.\n");
Inki Dae662aa6d2012-12-07 18:06:43 +0900214 goto err_destroy_framebuffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900215 }
216
Seung-Woo Kimaa6b2b62011-11-04 13:44:38 +0900217 ret = exynos_drm_fbdev_update(helper, helper->fb);
Inki Dae662aa6d2012-12-07 18:06:43 +0900218 if (ret < 0)
219 goto err_dealloc_cmap;
220
221 mutex_unlock(&dev->struct_mutex);
222 return ret;
223
224err_dealloc_cmap:
225 fb_dealloc_cmap(&fbi->cmap);
226err_destroy_framebuffer:
227 drm_framebuffer_cleanup(helper->fb);
228err_destroy_gem:
229 exynos_drm_gem_destroy(exynos_gem_obj);
230err_release_framebuffer:
231 framebuffer_release(fbi);
Inki Dae1c248b72011-10-04 19:19:01 +0900232
233/*
234 * if failed, all resources allocated above would be released by
235 * drm_mode_config_cleanup() when drm_load() had been called prior
236 * to any specific driver such as fimd or hdmi driver.
237 */
238out:
239 mutex_unlock(&dev->struct_mutex);
240 return ret;
241}
242
Inki Dae1c248b72011-10-04 19:19:01 +0900243static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
244 struct drm_fb_helper_surface_size *sizes)
245{
246 int ret = 0;
247
248 DRM_DEBUG_KMS("%s\n", __FILE__);
249
Inki Daebc41eae2012-02-15 11:25:21 +0900250 /*
251 * with !helper->fb, it means that this funcion is called first time
252 * and after that, the helper->fb would be used as clone mode.
253 */
Inki Dae1c248b72011-10-04 19:19:01 +0900254 if (!helper->fb) {
255 ret = exynos_drm_fbdev_create(helper, sizes);
256 if (ret < 0) {
257 DRM_ERROR("failed to create fbdev.\n");
258 return ret;
259 }
260
261 /*
262 * fb_helper expects a value more than 1 if succeed
263 * because register_framebuffer() should be called.
264 */
265 ret = 1;
Inki Dae1c248b72011-10-04 19:19:01 +0900266 }
267
268 return ret;
269}
270
271static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
272 .fb_probe = exynos_drm_fbdev_probe,
273};
274
275int exynos_drm_fbdev_init(struct drm_device *dev)
276{
277 struct exynos_drm_fbdev *fbdev;
278 struct exynos_drm_private *private = dev->dev_private;
279 struct drm_fb_helper *helper;
280 unsigned int num_crtc;
281 int ret;
282
283 DRM_DEBUG_KMS("%s\n", __FILE__);
284
285 if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
286 return 0;
287
288 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
289 if (!fbdev) {
290 DRM_ERROR("failed to allocate drm fbdev.\n");
291 return -ENOMEM;
292 }
293
294 private->fb_helper = helper = &fbdev->drm_fb_helper;
295 helper->funcs = &exynos_drm_fb_helper_funcs;
296
297 num_crtc = dev->mode_config.num_crtc;
298
299 ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
300 if (ret < 0) {
301 DRM_ERROR("failed to initialize drm fb helper.\n");
302 goto err_init;
303 }
304
305 ret = drm_fb_helper_single_add_all_connectors(helper);
306 if (ret < 0) {
307 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
308 goto err_setup;
309
310 }
311
312 ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
313 if (ret < 0) {
314 DRM_ERROR("failed to set up hw configuration.\n");
315 goto err_setup;
316 }
317
318 return 0;
319
320err_setup:
321 drm_fb_helper_fini(helper);
322
323err_init:
324 private->fb_helper = NULL;
325 kfree(fbdev);
326
327 return ret;
328}
329
330static void exynos_drm_fbdev_destroy(struct drm_device *dev,
331 struct drm_fb_helper *fb_helper)
332{
Inki Dae4744ad22012-12-07 17:51:27 +0900333 struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
334 struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900335 struct drm_framebuffer *fb;
336
Inki Daec704f1b2012-12-21 17:59:20 +0900337 if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
Inki Dae4744ad22012-12-07 17:51:27 +0900338 vunmap(exynos_gem_obj->buffer->kvaddr);
339
Inki Dae1c248b72011-10-04 19:19:01 +0900340 /* release drm framebuffer and real buffer */
341 if (fb_helper->fb && fb_helper->fb->funcs) {
342 fb = fb_helper->fb;
Rob Clarkf7eff602012-09-05 21:48:38 +0000343 if (fb)
344 drm_framebuffer_remove(fb);
Inki Dae1c248b72011-10-04 19:19:01 +0900345 }
346
347 /* release linux framebuffer */
348 if (fb_helper->fbdev) {
349 struct fb_info *info;
350 int ret;
351
352 info = fb_helper->fbdev;
353 ret = unregister_framebuffer(info);
354 if (ret < 0)
355 DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
356
357 if (info->cmap.len)
358 fb_dealloc_cmap(&info->cmap);
359
360 framebuffer_release(info);
361 }
362
363 drm_fb_helper_fini(fb_helper);
364}
365
366void exynos_drm_fbdev_fini(struct drm_device *dev)
367{
368 struct exynos_drm_private *private = dev->dev_private;
369 struct exynos_drm_fbdev *fbdev;
370
371 if (!private || !private->fb_helper)
372 return;
373
374 fbdev = to_exynos_fbdev(private->fb_helper);
375
Joonyoung Shime1533c02011-12-13 14:46:57 +0900376 if (fbdev->exynos_gem_obj)
377 exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
378
Inki Dae1c248b72011-10-04 19:19:01 +0900379 exynos_drm_fbdev_destroy(dev, private->fb_helper);
380 kfree(fbdev);
381 private->fb_helper = NULL;
382}
383
384void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
385{
386 struct exynos_drm_private *private = dev->dev_private;
387
388 if (!private || !private->fb_helper)
389 return;
390
391 drm_fb_helper_restore_fbdev_mode(private->fb_helper);
392}