Merge tag 'topic/drm-misc-2015-08-13' of git://anongit.freedesktop.org/drm-intel...
[cascardo/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    = drm_fb_helper_cfb_fillrect,
69         .fb_copyarea    = drm_fb_helper_cfb_copyarea,
70         .fb_imageblit   = drm_fb_helper_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_fb_helper_surface_size *sizes,
80                                      struct drm_framebuffer *fb)
81 {
82         struct fb_info *fbi = helper->fbdev;
83         struct exynos_drm_gem_buf *buffer;
84         unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
85         unsigned int nr_pages;
86         unsigned long offset;
87
88         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
89         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
90
91         /* RGB formats use only one buffer */
92         buffer = exynos_drm_fb_buffer(fb, 0);
93         if (!buffer) {
94                 DRM_DEBUG_KMS("buffer is null.\n");
95                 return -EFAULT;
96         }
97
98         nr_pages = buffer->size >> PAGE_SHIFT;
99
100         buffer->kvaddr = (void __iomem *) vmap(buffer->pages,
101                         nr_pages, VM_MAP,
102                         pgprot_writecombine(PAGE_KERNEL));
103         if (!buffer->kvaddr) {
104                 DRM_ERROR("failed to map pages to kernel space.\n");
105                 return -EIO;
106         }
107
108         /* buffer count to framebuffer always is 1 at booting time. */
109         exynos_drm_fb_set_buf_cnt(fb, 1);
110
111         offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
112         offset += fbi->var.yoffset * fb->pitches[0];
113
114         fbi->screen_base = buffer->kvaddr + offset;
115         fbi->screen_size = size;
116         fbi->fix.smem_len = size;
117
118         return 0;
119 }
120
121 static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
122                                     struct drm_fb_helper_surface_size *sizes)
123 {
124         struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
125         struct exynos_drm_gem_obj *exynos_gem_obj;
126         struct drm_device *dev = helper->dev;
127         struct fb_info *fbi;
128         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
129         struct platform_device *pdev = dev->platformdev;
130         unsigned long size;
131         int ret;
132
133         DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
134                         sizes->surface_width, sizes->surface_height,
135                         sizes->surface_bpp);
136
137         mode_cmd.width = sizes->surface_width;
138         mode_cmd.height = sizes->surface_height;
139         mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
140         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
141                                                           sizes->surface_depth);
142
143         mutex_lock(&dev->struct_mutex);
144
145         fbi = drm_fb_helper_alloc_fbi(helper);
146         if (IS_ERR(fbi)) {
147                 DRM_ERROR("failed to allocate fb info.\n");
148                 ret = PTR_ERR(fbi);
149                 goto out;
150         }
151
152         size = mode_cmd.pitches[0] * mode_cmd.height;
153
154         exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
155         /*
156          * If physically contiguous memory allocation fails and if IOMMU is
157          * supported then try to get buffer from non physically contiguous
158          * memory area.
159          */
160         if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
161                 dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
162                 exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
163                                                         size);
164         }
165
166         if (IS_ERR(exynos_gem_obj)) {
167                 ret = PTR_ERR(exynos_gem_obj);
168                 goto err_release_fbi;
169         }
170
171         exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
172
173         helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
174                         &exynos_gem_obj->base);
175         if (IS_ERR(helper->fb)) {
176                 DRM_ERROR("failed to create drm framebuffer.\n");
177                 ret = PTR_ERR(helper->fb);
178                 goto err_destroy_gem;
179         }
180
181         fbi->par = helper;
182         fbi->flags = FBINFO_FLAG_DEFAULT;
183         fbi->fbops = &exynos_drm_fb_ops;
184
185         ret = exynos_drm_fbdev_update(helper, sizes, helper->fb);
186         if (ret < 0)
187                 goto err_destroy_framebuffer;
188
189         mutex_unlock(&dev->struct_mutex);
190         return ret;
191
192 err_destroy_framebuffer:
193         drm_framebuffer_cleanup(helper->fb);
194 err_destroy_gem:
195         exynos_drm_gem_destroy(exynos_gem_obj);
196 err_release_fbi:
197         drm_fb_helper_release_fbi(helper);
198
199 /*
200  * if failed, all resources allocated above would be released by
201  * drm_mode_config_cleanup() when drm_load() had been called prior
202  * to any specific driver such as fimd or hdmi driver.
203  */
204 out:
205         mutex_unlock(&dev->struct_mutex);
206         return ret;
207 }
208
209 static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
210         .fb_probe =     exynos_drm_fbdev_create,
211 };
212
213 static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
214 {
215         struct drm_connector *connector;
216         bool ret = false;
217
218         mutex_lock(&dev->mode_config.mutex);
219         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
220                 if (connector->status != connector_status_connected)
221                         continue;
222
223                 ret = true;
224                 break;
225         }
226         mutex_unlock(&dev->mode_config.mutex);
227
228         return ret;
229 }
230
231 int exynos_drm_fbdev_init(struct drm_device *dev)
232 {
233         struct exynos_drm_fbdev *fbdev;
234         struct exynos_drm_private *private = dev->dev_private;
235         struct drm_fb_helper *helper;
236         unsigned int num_crtc;
237         int ret;
238
239         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
240                 return 0;
241
242         if (!exynos_drm_fbdev_is_anything_connected(dev))
243                 return 0;
244
245         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
246         if (!fbdev)
247                 return -ENOMEM;
248
249         private->fb_helper = helper = &fbdev->drm_fb_helper;
250
251         drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
252
253         num_crtc = dev->mode_config.num_crtc;
254
255         ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
256         if (ret < 0) {
257                 DRM_ERROR("failed to initialize drm fb helper.\n");
258                 goto err_init;
259         }
260
261         ret = drm_fb_helper_single_add_all_connectors(helper);
262         if (ret < 0) {
263                 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
264                 goto err_setup;
265
266         }
267
268         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
269         if (ret < 0) {
270                 DRM_ERROR("failed to set up hw configuration.\n");
271                 goto err_setup;
272         }
273
274         return 0;
275
276 err_setup:
277         drm_fb_helper_fini(helper);
278
279 err_init:
280         private->fb_helper = NULL;
281         kfree(fbdev);
282
283         return ret;
284 }
285
286 static void exynos_drm_fbdev_destroy(struct drm_device *dev,
287                                       struct drm_fb_helper *fb_helper)
288 {
289         struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
290         struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
291         struct drm_framebuffer *fb;
292
293         if (exynos_gem_obj->buffer->kvaddr)
294                 vunmap(exynos_gem_obj->buffer->kvaddr);
295
296         /* release drm framebuffer and real buffer */
297         if (fb_helper->fb && fb_helper->fb->funcs) {
298                 fb = fb_helper->fb;
299                 if (fb) {
300                         drm_framebuffer_unregister_private(fb);
301                         drm_framebuffer_remove(fb);
302                 }
303         }
304
305         drm_fb_helper_unregister_fbi(fb_helper);
306         drm_fb_helper_release_fbi(fb_helper);
307
308         drm_fb_helper_fini(fb_helper);
309 }
310
311 void exynos_drm_fbdev_fini(struct drm_device *dev)
312 {
313         struct exynos_drm_private *private = dev->dev_private;
314         struct exynos_drm_fbdev *fbdev;
315
316         if (!private || !private->fb_helper)
317                 return;
318
319         fbdev = to_exynos_fbdev(private->fb_helper);
320
321         exynos_drm_fbdev_destroy(dev, private->fb_helper);
322         kfree(fbdev);
323         private->fb_helper = NULL;
324 }
325
326 void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
327 {
328         struct exynos_drm_private *private = dev->dev_private;
329
330         if (!private || !private->fb_helper)
331                 return;
332
333         drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
334 }