]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_fbdev.c
ARM: sa11x0/assabet: ensure CS2 is configured appropriately
[karo-tx-linux.git] / drivers / gpu / drm / exynos / exynos_drm_fbdev.c
1 /* 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  * 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.
13  */
14
15 #include <drm/drmP.h>
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>
20
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_fbdev.h"
24 #include "exynos_drm_gem.h"
25 #include "exynos_drm_iommu.h"
26
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
33 struct exynos_drm_fbdev {
34         struct drm_fb_helper            drm_fb_helper;
35         struct exynos_drm_gem_obj       *exynos_gem_obj;
36 };
37
38 static 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
48         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
55         ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
56                 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
65 static struct fb_ops exynos_drm_fb_ops = {
66         .owner          = THIS_MODULE,
67         .fb_mmap        = exynos_drm_fb_mmap,
68         .fb_fillrect    = cfb_fillrect,
69         .fb_copyarea    = cfb_copyarea,
70         .fb_imageblit   = cfb_imageblit,
71         .fb_check_var   = drm_fb_helper_check_var,
72         .fb_set_par     = drm_fb_helper_set_par,
73         .fb_blank       = drm_fb_helper_blank,
74         .fb_pan_display = drm_fb_helper_pan_display,
75         .fb_setcmap     = drm_fb_helper_setcmap,
76 };
77
78 static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
79                                      struct drm_framebuffer *fb)
80 {
81         struct fb_info *fbi = helper->fbdev;
82         struct drm_device *dev = helper->dev;
83         struct exynos_drm_gem_buf *buffer;
84         unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
85         unsigned long offset;
86
87         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
88         drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
89
90         /* RGB formats use only one buffer */
91         buffer = exynos_drm_fb_buffer(fb, 0);
92         if (!buffer) {
93                 DRM_LOG_KMS("buffer is null.\n");
94                 return -EFAULT;
95         }
96
97         /* map pages with kernel virtual space. */
98         if (!buffer->kvaddr) {
99                 if (is_drm_iommu_supported(dev)) {
100                         unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
101
102                         buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
103                                         pgprot_writecombine(PAGE_KERNEL));
104                 } else {
105                         phys_addr_t dma_addr = buffer->dma_addr;
106                         if (dma_addr)
107                                 buffer->kvaddr = phys_to_virt(dma_addr);
108                         else
109                                 buffer->kvaddr = (void __iomem *)NULL;
110                 }
111                 if (!buffer->kvaddr) {
112                         DRM_ERROR("failed to map pages to kernel space.\n");
113                         return -EIO;
114                 }
115         }
116
117         /* buffer count to framebuffer always is 1 at booting time. */
118         exynos_drm_fb_set_buf_cnt(fb, 1);
119
120         offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
121         offset += fbi->var.yoffset * fb->pitches[0];
122
123         dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
124         fbi->screen_base = buffer->kvaddr + offset;
125         if (is_drm_iommu_supported(dev))
126                 fbi->fix.smem_start = (unsigned long)
127                         (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
128         else
129                 fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
130
131         fbi->screen_size = size;
132         fbi->fix.smem_len = size;
133
134         return 0;
135 }
136
137 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
138                                     struct drm_fb_helper_surface_size *sizes)
139 {
140         struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
141         struct exynos_drm_gem_obj *exynos_gem_obj;
142         struct drm_device *dev = helper->dev;
143         struct fb_info *fbi;
144         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
145         struct platform_device *pdev = dev->platformdev;
146         unsigned long size;
147         int ret;
148
149         DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
150                         sizes->surface_width, sizes->surface_height,
151                         sizes->surface_bpp);
152
153         mode_cmd.width = sizes->surface_width;
154         mode_cmd.height = sizes->surface_height;
155         mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
156         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
157                                                           sizes->surface_depth);
158
159         mutex_lock(&dev->struct_mutex);
160
161         fbi = framebuffer_alloc(0, &pdev->dev);
162         if (!fbi) {
163                 DRM_ERROR("failed to allocate fb info.\n");
164                 ret = -ENOMEM;
165                 goto out;
166         }
167
168         size = mode_cmd.pitches[0] * mode_cmd.height;
169
170         exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
171         /*
172          * If physically contiguous memory allocation fails and if IOMMU is
173          * supported then try to get buffer from non physically contiguous
174          * memory area.
175          */
176         if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
177                 dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
178                 exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
179                                                         size);
180         }
181
182         if (IS_ERR(exynos_gem_obj)) {
183                 ret = PTR_ERR(exynos_gem_obj);
184                 goto err_release_framebuffer;
185         }
186
187         exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
188
189         helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
190                         &exynos_gem_obj->base);
191         if (IS_ERR(helper->fb)) {
192                 DRM_ERROR("failed to create drm framebuffer.\n");
193                 ret = PTR_ERR(helper->fb);
194                 goto err_destroy_gem;
195         }
196
197         helper->fbdev = fbi;
198
199         fbi->par = helper;
200         fbi->flags = FBINFO_FLAG_DEFAULT;
201         fbi->fbops = &exynos_drm_fb_ops;
202
203         ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
204         if (ret) {
205                 DRM_ERROR("failed to allocate cmap.\n");
206                 goto err_destroy_framebuffer;
207         }
208
209         ret = exynos_drm_fbdev_update(helper, helper->fb);
210         if (ret < 0)
211                 goto err_dealloc_cmap;
212
213         mutex_unlock(&dev->struct_mutex);
214         return ret;
215
216 err_dealloc_cmap:
217         fb_dealloc_cmap(&fbi->cmap);
218 err_destroy_framebuffer:
219         drm_framebuffer_cleanup(helper->fb);
220 err_destroy_gem:
221         exynos_drm_gem_destroy(exynos_gem_obj);
222 err_release_framebuffer:
223         framebuffer_release(fbi);
224
225 /*
226  * if failed, all resources allocated above would be released by
227  * drm_mode_config_cleanup() when drm_load() had been called prior
228  * to any specific driver such as fimd or hdmi driver.
229  */
230 out:
231         mutex_unlock(&dev->struct_mutex);
232         return ret;
233 }
234
235 static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
236         .fb_probe =     exynos_drm_fbdev_create,
237 };
238
239 int exynos_drm_fbdev_init(struct drm_device *dev)
240 {
241         struct exynos_drm_fbdev *fbdev;
242         struct exynos_drm_private *private = dev->dev_private;
243         struct drm_fb_helper *helper;
244         unsigned int num_crtc;
245         int ret;
246
247         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
248                 return 0;
249
250         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
251         if (!fbdev)
252                 return -ENOMEM;
253
254         private->fb_helper = helper = &fbdev->drm_fb_helper;
255         helper->funcs = &exynos_drm_fb_helper_funcs;
256
257         num_crtc = dev->mode_config.num_crtc;
258
259         ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
260         if (ret < 0) {
261                 DRM_ERROR("failed to initialize drm fb helper.\n");
262                 goto err_init;
263         }
264
265         ret = drm_fb_helper_single_add_all_connectors(helper);
266         if (ret < 0) {
267                 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
268                 goto err_setup;
269
270         }
271
272         /* disable all the possible outputs/crtcs before entering KMS mode */
273         drm_helper_disable_unused_functions(dev);
274
275         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
276         if (ret < 0) {
277                 DRM_ERROR("failed to set up hw configuration.\n");
278                 goto err_setup;
279         }
280
281         return 0;
282
283 err_setup:
284         drm_fb_helper_fini(helper);
285
286 err_init:
287         private->fb_helper = NULL;
288         kfree(fbdev);
289
290         return ret;
291 }
292
293 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
294                                       struct drm_fb_helper *fb_helper)
295 {
296         struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
297         struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
298         struct drm_framebuffer *fb;
299
300         if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
301                 vunmap(exynos_gem_obj->buffer->kvaddr);
302
303         /* release drm framebuffer and real buffer */
304         if (fb_helper->fb && fb_helper->fb->funcs) {
305                 fb = fb_helper->fb;
306                 if (fb) {
307                         drm_framebuffer_unregister_private(fb);
308                         drm_framebuffer_remove(fb);
309                 }
310         }
311
312         /* release linux framebuffer */
313         if (fb_helper->fbdev) {
314                 struct fb_info *info;
315                 int ret;
316
317                 info = fb_helper->fbdev;
318                 ret = unregister_framebuffer(info);
319                 if (ret < 0)
320                         DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
321
322                 if (info->cmap.len)
323                         fb_dealloc_cmap(&info->cmap);
324
325                 framebuffer_release(info);
326         }
327
328         drm_fb_helper_fini(fb_helper);
329 }
330
331 void exynos_drm_fbdev_fini(struct drm_device *dev)
332 {
333         struct exynos_drm_private *private = dev->dev_private;
334         struct exynos_drm_fbdev *fbdev;
335
336         if (!private || !private->fb_helper)
337                 return;
338
339         fbdev = to_exynos_fbdev(private->fb_helper);
340
341         if (fbdev->exynos_gem_obj)
342                 exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
343
344         exynos_drm_fbdev_destroy(dev, private->fb_helper);
345         kfree(fbdev);
346         private->fb_helper = NULL;
347 }
348
349 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
350 {
351         struct exynos_drm_private *private = dev->dev_private;
352
353         if (!private || !private->fb_helper)
354                 return;
355
356         drm_modeset_lock_all(dev);
357         drm_fb_helper_restore_fbdev_mode(private->fb_helper);
358         drm_modeset_unlock_all(dev);
359 }