Merge tag 'lsk-v3.10-15.10-android'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_fbdev.c
1 /*
2  * Copyright (C) ROCKCHIP, Inc.
3  * Author:yzq<yzq@rock-chips.com>
4  *
5  * based on exynos_drm_fbdev.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 #include <drm/drmP.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_crtc_helper.h>
20
21 #include "rockchip_drm_drv.h"
22 #include "rockchip_drm_fb.h"
23 #include "rockchip_drm_gem.h"
24 #include "rockchip_drm_iommu.h"
25
26 #define MAX_CONNECTOR           4
27 #define PREFERRED_BPP           32
28
29 #define to_rockchip_fbdev(x)    container_of(x, struct rockchip_drm_fbdev,\
30                                 drm_fb_helper)
31
32 struct rockchip_drm_fbdev {
33         struct drm_fb_helper            drm_fb_helper;
34         struct rockchip_drm_gem_obj     *rockchip_gem_obj;
35 };
36
37 static int rockchip_drm_fb_mmap(struct fb_info *info,
38                         struct vm_area_struct *vma)
39 {
40         struct drm_fb_helper *helper = info->par;
41         struct rockchip_drm_fbdev *rockchip_fbd = to_rockchip_fbdev(helper);
42         struct rockchip_drm_gem_obj *rockchip_gem_obj = rockchip_fbd->rockchip_gem_obj;
43         struct rockchip_drm_gem_buf *buffer = rockchip_gem_obj->buffer;
44         unsigned long vm_size;
45         int ret;
46
47         DRM_DEBUG_KMS("%s\n", __func__);
48
49         vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
50
51         vm_size = vma->vm_end - vma->vm_start;
52
53         if (vm_size > buffer->size)
54                 return -EINVAL;
55
56         ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
57                 buffer->dma_addr, buffer->size, &buffer->dma_attrs);
58         if (ret < 0) {
59                 DRM_ERROR("failed to mmap.\n");
60                 return ret;
61         }
62
63         return 0;
64 }
65
66 static struct fb_ops rockchip_drm_fb_ops = {
67         .owner          = THIS_MODULE,
68         .fb_mmap        = rockchip_drm_fb_mmap,
69         .fb_fillrect    = cfb_fillrect,
70         .fb_copyarea    = cfb_copyarea,
71         .fb_imageblit   = cfb_imageblit,
72         .fb_check_var   = drm_fb_helper_check_var,
73         .fb_set_par     = drm_fb_helper_set_par,
74         .fb_blank       = drm_fb_helper_blank,
75         .fb_pan_display = drm_fb_helper_pan_display,
76         .fb_setcmap     = drm_fb_helper_setcmap,
77 };
78
79 static int rockchip_drm_fbdev_update(struct drm_fb_helper *helper,
80                                      struct drm_framebuffer *fb)
81 {
82         struct fb_info *fbi = helper->fbdev;
83         struct drm_device *dev = helper->dev;
84         struct rockchip_drm_gem_buf *buffer;
85         unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
86         unsigned long offset;
87
88         DRM_DEBUG_KMS("%s\n", __FILE__);
89
90         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
91         drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
92
93         /* RGB formats use only one buffer */
94         buffer = rockchip_drm_fb_buffer(fb, 0);
95         if (!buffer) {
96                 DRM_LOG_KMS("buffer is null.\n");
97                 return -EFAULT;
98         }
99
100         /* map pages with kernel virtual space. */
101         if (!buffer->kvaddr) {
102                 if (is_drm_iommu_supported(dev)) {
103                         unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
104
105                         buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
106                                         pgprot_writecombine(PAGE_KERNEL));
107                 } else {
108                         phys_addr_t dma_addr = buffer->dma_addr;
109                         if (dma_addr)
110                                 buffer->kvaddr = phys_to_virt(dma_addr);
111                         else
112                                 buffer->kvaddr = (void __iomem *)NULL;
113                 }
114                 if (!buffer->kvaddr) {
115                         DRM_ERROR("failed to map pages to kernel space.\n");
116                         return -EIO;
117                 }
118         }
119
120         /* buffer count to framebuffer always is 1 at booting time. */
121         rockchip_drm_fb_set_buf_cnt(fb, 1);
122
123         offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
124         offset += fbi->var.yoffset * fb->pitches[0];
125
126         dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
127         fbi->screen_base = buffer->kvaddr + offset;
128         if (is_drm_iommu_supported(dev))
129                 fbi->fix.smem_start = (unsigned long)
130                         (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
131         else
132                 fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
133
134         fbi->screen_size = size;
135         fbi->fix.smem_len = size;
136
137         return 0;
138 }
139
140 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
141                                     struct drm_fb_helper_surface_size *sizes)
142 {
143         struct rockchip_drm_fbdev *rockchip_fbdev = to_rockchip_fbdev(helper);
144         struct rockchip_drm_gem_obj *rockchip_gem_obj;
145         struct drm_device *dev = helper->dev;
146         struct fb_info *fbi;
147         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
148         struct platform_device *pdev = dev->platformdev;
149         unsigned long size;
150         int ret;
151
152         DRM_DEBUG_KMS("%s\n", __FILE__);
153
154         DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
155                         sizes->surface_width, sizes->surface_height,
156                         sizes->surface_bpp);
157
158         mode_cmd.width = sizes->surface_width;
159         mode_cmd.height = sizes->surface_height;
160         mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
161         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
162                                                           sizes->surface_depth);
163
164         mutex_lock(&dev->struct_mutex);
165
166         fbi = framebuffer_alloc(0, &pdev->dev);
167         if (!fbi) {
168                 DRM_ERROR("failed to allocate fb info.\n");
169                 ret = -ENOMEM;
170                 goto out;
171         }
172
173         size = mode_cmd.pitches[0] * mode_cmd.height;
174
175         /* 0 means to allocate physically continuous memory */
176         rockchip_gem_obj = rockchip_drm_gem_create(dev, 0, size);
177         if (IS_ERR(rockchip_gem_obj)) {
178                 ret = PTR_ERR(rockchip_gem_obj);
179                 goto err_release_framebuffer;
180         }
181
182         rockchip_fbdev->rockchip_gem_obj = rockchip_gem_obj;
183
184         helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
185                         &rockchip_gem_obj->base);
186         if (IS_ERR(helper->fb)) {
187                 DRM_ERROR("failed to create drm framebuffer.\n");
188                 ret = PTR_ERR(helper->fb);
189                 goto err_destroy_gem;
190         }
191
192         helper->fbdev = fbi;
193
194         fbi->par = helper;
195         fbi->flags = FBINFO_FLAG_DEFAULT;
196         fbi->fbops = &rockchip_drm_fb_ops;
197
198         ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
199         if (ret) {
200                 DRM_ERROR("failed to allocate cmap.\n");
201                 goto err_destroy_framebuffer;
202         }
203
204         ret = rockchip_drm_fbdev_update(helper, helper->fb);
205         if (ret < 0)
206                 goto err_dealloc_cmap;
207
208         mutex_unlock(&dev->struct_mutex);
209         return ret;
210
211 err_dealloc_cmap:
212         fb_dealloc_cmap(&fbi->cmap);
213 err_destroy_framebuffer:
214         drm_framebuffer_cleanup(helper->fb);
215 err_destroy_gem:
216         rockchip_drm_gem_destroy(rockchip_gem_obj);
217 err_release_framebuffer:
218         framebuffer_release(fbi);
219
220 /*
221  * if failed, all resources allocated above would be released by
222  * drm_mode_config_cleanup() when drm_load() had been called prior
223  * to any specific driver such as fimd or hdmi driver.
224  */
225 out:
226         mutex_unlock(&dev->struct_mutex);
227         return ret;
228 }
229
230 static struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
231         .fb_probe =     rockchip_drm_fbdev_create,
232 };
233
234 int rockchip_drm_fbdev_init(struct drm_device *dev)
235 {
236         struct rockchip_drm_fbdev *fbdev;
237         struct rockchip_drm_private *private = dev->dev_private;
238         struct drm_fb_helper *helper;
239         unsigned int num_crtc;
240         int ret;
241
242         DRM_DEBUG_KMS("%s\n", __FILE__);
243
244         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
245                 return 0;
246
247         fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
248         if (!fbdev) {
249                 DRM_ERROR("failed to allocate drm fbdev.\n");
250                 return -ENOMEM;
251         }
252
253         private->fb_helper = helper = &fbdev->drm_fb_helper;
254         helper->funcs = &rockchip_drm_fb_helper_funcs;
255
256         num_crtc = dev->mode_config.num_crtc;
257
258         ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
259         if (ret < 0) {
260                 DRM_ERROR("failed to initialize drm fb helper.\n");
261                 goto err_init;
262         }
263
264         ret = drm_fb_helper_single_add_all_connectors(helper);
265         if (ret < 0) {
266                 DRM_ERROR("failed to register drm_fb_helper_connector.\n");
267                 goto err_setup;
268
269         }
270
271         /* disable all the possible outputs/crtcs before entering KMS mode */
272         drm_helper_disable_unused_functions(dev);
273
274         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
275         if (ret < 0) {
276                 DRM_ERROR("failed to set up hw configuration.\n");
277                 goto err_setup;
278         }
279
280         return 0;
281
282 err_setup:
283         drm_fb_helper_fini(helper);
284
285 err_init:
286         private->fb_helper = NULL;
287         kfree(fbdev);
288
289         return ret;
290 }
291
292 static void rockchip_drm_fbdev_destroy(struct drm_device *dev,
293                                       struct drm_fb_helper *fb_helper)
294 {
295         struct rockchip_drm_fbdev *rockchip_fbd = to_rockchip_fbdev(fb_helper);
296         struct rockchip_drm_gem_obj *rockchip_gem_obj = rockchip_fbd->rockchip_gem_obj;
297         struct drm_framebuffer *fb;
298
299         if (is_drm_iommu_supported(dev) && rockchip_gem_obj->buffer->kvaddr)
300                 vunmap(rockchip_gem_obj->buffer->kvaddr);
301
302         /* release drm framebuffer and real buffer */
303         if (fb_helper->fb && fb_helper->fb->funcs) {
304                 fb = fb_helper->fb;
305                 if (fb) {
306                         drm_framebuffer_unregister_private(fb);
307                         drm_framebuffer_remove(fb);
308                 }
309         }
310
311         /* release linux framebuffer */
312         if (fb_helper->fbdev) {
313                 struct fb_info *info;
314                 int ret;
315
316                 info = fb_helper->fbdev;
317                 ret = unregister_framebuffer(info);
318                 if (ret < 0)
319                         DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
320
321                 if (info->cmap.len)
322                         fb_dealloc_cmap(&info->cmap);
323
324                 framebuffer_release(info);
325         }
326
327         drm_fb_helper_fini(fb_helper);
328 }
329
330 void rockchip_drm_fbdev_fini(struct drm_device *dev)
331 {
332         struct rockchip_drm_private *private = dev->dev_private;
333         struct rockchip_drm_fbdev *fbdev;
334
335         if (!private || !private->fb_helper)
336                 return;
337
338         fbdev = to_rockchip_fbdev(private->fb_helper);
339
340         if (fbdev->rockchip_gem_obj)
341                 rockchip_drm_gem_destroy(fbdev->rockchip_gem_obj);
342
343         rockchip_drm_fbdev_destroy(dev, private->fb_helper);
344         kfree(fbdev);
345         private->fb_helper = NULL;
346 }
347
348 void rockchip_drm_fbdev_restore_mode(struct drm_device *dev)
349 {
350         struct rockchip_drm_private *private = dev->dev_private;
351
352         if (!private || !private->fb_helper)
353                 return;
354
355         drm_modeset_lock_all(dev);
356         drm_fb_helper_restore_fbdev_mode(private->fb_helper);
357         drm_modeset_unlock_all(dev);
358 }