drm/rockchip: vop: add rk3366 vop lit support
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_fb.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 <linux/kernel.h>
16 #include <drm/drm.h>
17 #include <drm/drmP.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <linux/memblock.h>
22 #include <linux/iommu.h>
23
24 #include "rockchip_drm_drv.h"
25 #include "rockchip_drm_gem.h"
26 #include "rockchip_drm_backlight.h"
27
28 #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
29
30 struct rockchip_drm_fb {
31         struct drm_framebuffer fb;
32         dma_addr_t dma_addr[ROCKCHIP_MAX_FB_BUFFER];
33         struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
34         struct rockchip_logo *logo;
35 };
36
37 bool rockchip_fb_is_logo(struct drm_framebuffer *fb)
38 {
39         struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
40
41         return rk_fb && rk_fb->logo;
42 }
43
44 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
45                                     unsigned int plane)
46 {
47         struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
48
49         if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
50                 return 0;
51
52         return rk_fb->dma_addr[plane];
53 }
54
55 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
56 {
57         struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
58         struct drm_gem_object *obj;
59         int i;
60
61         for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
62                 obj = rockchip_fb->obj[i];
63                 if (obj)
64                         drm_gem_object_unreference_unlocked(obj);
65         }
66
67 #ifndef MODULE
68         if (rockchip_fb->logo)
69                 rockchip_free_loader_memory(fb->dev);
70 #else
71         WARN_ON(rockchip_fb->logo);
72 #endif
73
74         drm_framebuffer_cleanup(fb);
75         kfree(rockchip_fb);
76 }
77
78 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
79                                          struct drm_file *file_priv,
80                                          unsigned int *handle)
81 {
82         struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
83
84         return drm_gem_handle_create(file_priv,
85                                      rockchip_fb->obj[0], handle);
86 }
87
88 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
89         .destroy        = rockchip_drm_fb_destroy,
90         .create_handle  = rockchip_drm_fb_create_handle,
91 };
92
93 struct drm_framebuffer *
94 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
95                   struct drm_gem_object **obj, struct rockchip_logo *logo,
96                   unsigned int num_planes)
97 {
98         struct rockchip_drm_fb *rockchip_fb;
99         struct rockchip_gem_object *rk_obj;
100         int ret = 0;
101         int i;
102
103         rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
104         if (!rockchip_fb)
105                 return ERR_PTR(-ENOMEM);
106
107         drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
108
109         ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
110                                    &rockchip_drm_fb_funcs);
111         if (ret) {
112                 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
113                         ret);
114                 goto err_free_fb;
115         }
116
117         if (obj) {
118                 for (i = 0; i < num_planes; i++)
119                         rockchip_fb->obj[i] = obj[i];
120
121                 for (i = 0; i < num_planes; i++) {
122                         rk_obj = to_rockchip_obj(obj[i]);
123                         rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
124                 }
125 #ifndef MODULE
126         } else if (logo) {
127                 rockchip_fb->dma_addr[0] = logo->dma_addr;
128                 rockchip_fb->logo = logo;
129                 logo->count++;
130 #endif
131         } else {
132                 ret = -EINVAL;
133                 dev_err(dev->dev, "Failed to find available buffer\n");
134                 goto err_deinit_drm_fb;
135         }
136
137         return &rockchip_fb->fb;
138
139 err_deinit_drm_fb:
140         drm_framebuffer_cleanup(&rockchip_fb->fb);
141 err_free_fb:
142         kfree(rockchip_fb);
143         return ERR_PTR(ret);
144 }
145
146 static struct drm_framebuffer *
147 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
148                         struct drm_mode_fb_cmd2 *mode_cmd)
149 {
150         struct drm_framebuffer *fb;
151         struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
152         struct drm_gem_object *obj;
153         unsigned int hsub;
154         unsigned int vsub;
155         int num_planes;
156         int ret;
157         int i;
158
159         hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
160         vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
161         num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
162                          ROCKCHIP_MAX_FB_BUFFER);
163
164         for (i = 0; i < num_planes; i++) {
165                 unsigned int width = mode_cmd->width / (i ? hsub : 1);
166                 unsigned int height = mode_cmd->height / (i ? vsub : 1);
167                 unsigned int min_size;
168                 unsigned int bpp =
169                         drm_format_plane_bpp(mode_cmd->pixel_format, i);
170
171                 obj = drm_gem_object_lookup(dev, file_priv,
172                                             mode_cmd->handles[i]);
173                 if (!obj) {
174                         dev_err(dev->dev, "Failed to lookup GEM object\n");
175                         ret = -ENXIO;
176                         goto err_gem_object_unreference;
177                 }
178
179                 min_size = (height - 1) * mode_cmd->pitches[i] +
180                         mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
181                 if (obj->size < min_size) {
182                         drm_gem_object_unreference_unlocked(obj);
183                         ret = -EINVAL;
184                         goto err_gem_object_unreference;
185                 }
186                 objs[i] = obj;
187         }
188
189         fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
190         if (IS_ERR(fb)) {
191                 ret = PTR_ERR(fb);
192                 goto err_gem_object_unreference;
193         }
194
195         return fb;
196
197 err_gem_object_unreference:
198         for (i--; i >= 0; i--)
199                 drm_gem_object_unreference_unlocked(objs[i]);
200         return ERR_PTR(ret);
201 }
202
203 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
204 {
205         struct rockchip_drm_private *private = dev->dev_private;
206         struct drm_fb_helper *fb_helper = private->fbdev_helper;
207
208         if (fb_helper)
209                 drm_fb_helper_hotplug_event(fb_helper);
210 }
211
212 static void
213 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
214 {
215         struct drm_atomic_state *state = commit->state;
216         struct drm_device *dev = commit->dev;
217
218         /*
219          * TODO: do fence wait here.
220          */
221
222         /*
223          * Rockchip crtc support runtime PM, can't update display planes
224          * when crtc is disabled.
225          *
226          * drm_atomic_helper_commit comments detail that:
227          *     For drivers supporting runtime PM the recommended sequence is
228          *
229          *     drm_atomic_helper_commit_modeset_disables(dev, state);
230          *
231          *     drm_atomic_helper_commit_modeset_enables(dev, state);
232          *
233          *     drm_atomic_helper_commit_planes(dev, state, true);
234          *
235          * See the kerneldoc entries for these three functions for more details.
236          */
237         drm_atomic_helper_commit_modeset_disables(dev, state);
238
239         drm_atomic_helper_commit_modeset_enables(dev, state);
240
241         rockchip_drm_backlight_update(dev);
242
243         drm_atomic_helper_commit_planes(dev, state, true);
244
245         drm_atomic_helper_wait_for_vblanks(dev, state);
246
247         drm_atomic_helper_cleanup_planes(dev, state);
248
249         drm_atomic_state_free(state);
250 }
251
252 void rockchip_drm_atomic_work(struct work_struct *work)
253 {
254         struct rockchip_atomic_commit *commit = container_of(work,
255                                         struct rockchip_atomic_commit, work);
256
257         rockchip_atomic_commit_complete(commit);
258 }
259
260 int rockchip_drm_atomic_commit(struct drm_device *dev,
261                                struct drm_atomic_state *state,
262                                bool async)
263 {
264         struct rockchip_drm_private *private = dev->dev_private;
265         struct rockchip_atomic_commit *commit = &private->commit;
266         int ret;
267
268         ret = drm_atomic_helper_prepare_planes(dev, state);
269         if (ret)
270                 return ret;
271
272         /* serialize outstanding asynchronous commits */
273         mutex_lock(&commit->lock);
274         flush_work(&commit->work);
275
276         drm_atomic_helper_swap_state(dev, state);
277
278         commit->dev = dev;
279         commit->state = state;
280
281         if (async)
282                 schedule_work(&commit->work);
283         else
284                 rockchip_atomic_commit_complete(commit);
285
286         mutex_unlock(&commit->lock);
287
288         return 0;
289 }
290
291 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
292         .fb_create = rockchip_user_fb_create,
293         .output_poll_changed = rockchip_drm_output_poll_changed,
294         .atomic_check = drm_atomic_helper_check,
295         .atomic_commit = rockchip_drm_atomic_commit,
296 };
297
298 struct drm_framebuffer *
299 rockchip_drm_framebuffer_init(struct drm_device *dev,
300                               struct drm_mode_fb_cmd2 *mode_cmd,
301                               struct drm_gem_object *obj)
302 {
303         struct drm_framebuffer *fb;
304
305         fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
306         if (IS_ERR(fb))
307                 return NULL;
308
309         return fb;
310 }
311
312 void rockchip_drm_mode_config_init(struct drm_device *dev)
313 {
314         dev->mode_config.min_width = 0;
315         dev->mode_config.min_height = 0;
316
317         /*
318          * set max width and height as default value(4096x4096).
319          * this value would be used to check framebuffer size limitation
320          * at drm_mode_addfb().
321          */
322         dev->mode_config.max_width = 8192;
323         dev->mode_config.max_height = 8192;
324
325         dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
326 }