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