FROMLIST: drm/rockchip: fix fbdev crash when not use DRM_FBDEV_EMULATION
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_fbdev.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_crtc_helper.h>
19
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_gem.h"
22 #include "rockchip_drm_fb.h"
23
24 #define PREFERRED_BPP           32
25
26 static int rockchip_fbdev_mmap(struct fb_info *info,
27                                struct vm_area_struct *vma)
28 {
29         struct drm_fb_helper *helper = info->par;
30         struct rockchip_drm_private *private = helper->dev->dev_private;
31
32         return rockchip_gem_mmap_buf(private->fbdev_bo, vma);
33 }
34
35 static struct fb_ops rockchip_drm_fbdev_ops = {
36         .owner          = THIS_MODULE,
37         .fb_mmap        = rockchip_fbdev_mmap,
38         .fb_fillrect    = drm_fb_helper_cfb_fillrect,
39         .fb_copyarea    = drm_fb_helper_cfb_copyarea,
40         .fb_imageblit   = drm_fb_helper_cfb_imageblit,
41         .fb_check_var   = drm_fb_helper_check_var,
42         .fb_set_par     = drm_fb_helper_set_par,
43         .fb_blank       = drm_fb_helper_blank,
44         .fb_pan_display = drm_fb_helper_pan_display,
45         .fb_setcmap     = drm_fb_helper_setcmap,
46 };
47
48 static int rockchip_drm_fbdev_create(struct drm_fb_helper *helper,
49                                      struct drm_fb_helper_surface_size *sizes)
50 {
51         struct rockchip_drm_private *private = helper->dev->dev_private;
52         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
53         struct drm_device *dev = helper->dev;
54         struct rockchip_gem_object *rk_obj;
55         struct drm_framebuffer *fb;
56         unsigned int bytes_per_pixel;
57         unsigned long offset;
58         struct fb_info *fbi;
59         size_t size;
60         int ret;
61
62         bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
63
64         mode_cmd.width = sizes->surface_width;
65         mode_cmd.height = sizes->surface_height;
66         mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
67         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
68                 sizes->surface_depth);
69
70         size = mode_cmd.pitches[0] * mode_cmd.height;
71
72         rk_obj = rockchip_gem_create_object(dev, size, true);
73         if (IS_ERR(rk_obj))
74                 return -ENOMEM;
75
76         private->fbdev_bo = &rk_obj->base;
77
78         fbi = drm_fb_helper_alloc_fbi(helper);
79         if (IS_ERR(fbi)) {
80                 dev_err(dev->dev, "Failed to create framebuffer info.\n");
81                 ret = PTR_ERR(fbi);
82                 goto err_rockchip_gem_free_object;
83         }
84
85         helper->fb = rockchip_drm_framebuffer_init(dev, &mode_cmd,
86                                                    private->fbdev_bo);
87         if (IS_ERR(helper->fb)) {
88                 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
89                 ret = PTR_ERR(helper->fb);
90                 goto err_release_fbi;
91         }
92
93         fbi->par = helper;
94         fbi->flags = FBINFO_FLAG_DEFAULT;
95         fbi->fbops = &rockchip_drm_fbdev_ops;
96
97         fb = helper->fb;
98         drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
99         drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
100
101         offset = fbi->var.xoffset * bytes_per_pixel;
102         offset += fbi->var.yoffset * fb->pitches[0];
103
104         dev->mode_config.fb_base = 0;
105         fbi->screen_base = rk_obj->kvaddr + offset;
106         fbi->screen_size = rk_obj->base.size;
107         fbi->fix.smem_len = rk_obj->base.size;
108
109         DRM_DEBUG_KMS("FB [%dx%d]-%d kvaddr=%p offset=%ld size=%zu\n",
110                       fb->width, fb->height, fb->depth, rk_obj->kvaddr,
111                       offset, size);
112
113         fbi->skip_vt_switch = true;
114
115         return 0;
116
117 err_release_fbi:
118         drm_fb_helper_release_fbi(helper);
119 err_rockchip_gem_free_object:
120         rockchip_gem_free_object(&rk_obj->base);
121         return ret;
122 }
123
124 static const struct drm_fb_helper_funcs rockchip_drm_fb_helper_funcs = {
125         .fb_probe = rockchip_drm_fbdev_create,
126 };
127
128 int rockchip_drm_fbdev_init(struct drm_device *dev)
129 {
130         struct rockchip_drm_private *private = dev->dev_private;
131         struct drm_fb_helper *helper;
132         unsigned int num_crtc;
133         int ret;
134
135         if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
136                 return -EINVAL;
137
138         num_crtc = dev->mode_config.num_crtc;
139
140         helper = devm_kzalloc(dev->dev, sizeof(*helper), GFP_KERNEL);
141         if (!helper)
142                 return -ENOMEM;
143
144         private->fbdev_helper = helper;
145
146         drm_fb_helper_prepare(dev, helper, &rockchip_drm_fb_helper_funcs);
147
148         ret = drm_fb_helper_init(dev, helper, num_crtc, ROCKCHIP_MAX_CONNECTOR);
149         if (ret < 0) {
150                 dev_err(dev->dev, "Failed to initialize drm fb helper - %d.\n",
151                         ret);
152                 return ret;
153         }
154
155         ret = drm_fb_helper_single_add_all_connectors(helper);
156         if (ret < 0) {
157                 dev_err(dev->dev, "Failed to add connectors - %d.\n", ret);
158                 goto err_drm_fb_helper_fini;
159         }
160
161         ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
162         if (ret < 0) {
163                 dev_err(dev->dev, "Failed to set initial hw config - %d.\n",
164                         ret);
165                 goto err_drm_fb_helper_fini;
166         }
167
168         return 0;
169
170 err_drm_fb_helper_fini:
171         drm_fb_helper_fini(helper);
172         return ret;
173 }
174
175 void rockchip_drm_fbdev_fini(struct drm_device *dev)
176 {
177         struct rockchip_drm_private *private = dev->dev_private;
178         struct drm_fb_helper *helper = private->fbdev_helper;
179
180         if (!helper)
181                 return;
182
183         drm_fb_helper_unregister_fbi(helper);
184         drm_fb_helper_release_fbi(helper);
185
186         if (helper->fb)
187                 drm_framebuffer_unreference(helper->fb);
188
189         drm_fb_helper_fini(helper);
190 }