Merge tag 'lsk-v4.4-17.05-android' of git://git.linaro.org/kernel/linux-linaro-stable.git
[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 rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
212 {
213         struct rockchip_drm_private *priv = crtc->dev->dev_private;
214         int pipe = drm_crtc_index(crtc);
215         const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
216
217         if (crtc_funcs && crtc_funcs->wait_for_update)
218                 crtc_funcs->wait_for_update(crtc);
219 }
220
221 /*
222  * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
223  * have hardware counters for neither vblanks nor scanlines, which results in
224  * a race where:
225  *                              | <-- HW vsync irq and reg take effect
226  *             plane_commit --> |
227  *      get_vblank and wait --> |
228  *                              | <-- handle_vblank, vblank->count + 1
229  *               cleanup_fb --> |
230  *              iommu crash --> |
231  *                              | <-- HW vsync irq and reg take effect
232  *
233  * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
234  * of waiting for vblank_count to change.
235  */
236 static void
237 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
238 {
239         struct drm_crtc_state *old_crtc_state;
240         struct drm_crtc *crtc;
241         int i, ret;
242
243         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
244                 /* No one cares about the old state, so abuse it for tracking
245                  * and store whether we hold a vblank reference (and should do a
246                  * vblank wait) in the ->enable boolean.
247                  */
248                 old_crtc_state->enable = false;
249
250                 if (!crtc->state->active)
251                         continue;
252
253                 if (!drm_atomic_helper_framebuffer_changed(dev,
254                                 old_state, crtc))
255                         continue;
256
257                 ret = drm_crtc_vblank_get(crtc);
258                 if (ret != 0)
259                         continue;
260
261                 old_crtc_state->enable = true;
262         }
263
264         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
265                 if (!old_crtc_state->enable)
266                         continue;
267
268                 rockchip_crtc_wait_for_update(crtc);
269                 drm_crtc_vblank_put(crtc);
270         }
271 }
272
273 static void
274 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
275 {
276         struct drm_atomic_state *state = commit->state;
277         struct drm_device *dev = commit->dev;
278
279         /*
280          * TODO: do fence wait here.
281          */
282
283         /*
284          * Rockchip crtc support runtime PM, can't update display planes
285          * when crtc is disabled.
286          *
287          * drm_atomic_helper_commit comments detail that:
288          *     For drivers supporting runtime PM the recommended sequence is
289          *
290          *     drm_atomic_helper_commit_modeset_disables(dev, state);
291          *
292          *     drm_atomic_helper_commit_modeset_enables(dev, state);
293          *
294          *     drm_atomic_helper_commit_planes(dev, state, true);
295          *
296          * See the kerneldoc entries for these three functions for more details.
297          */
298         drm_atomic_helper_commit_modeset_disables(dev, state);
299
300         drm_atomic_helper_commit_modeset_enables(dev, state);
301
302         drm_atomic_helper_commit_planes(dev, state, true);
303
304         rockchip_atomic_wait_for_complete(dev, state);
305
306         drm_atomic_helper_cleanup_planes(dev, state);
307
308         drm_atomic_state_free(state);
309 }
310
311 void rockchip_drm_atomic_work(struct work_struct *work)
312 {
313         struct rockchip_atomic_commit *commit = container_of(work,
314                                         struct rockchip_atomic_commit, work);
315
316         rockchip_atomic_commit_complete(commit);
317 }
318
319 int rockchip_drm_atomic_commit(struct drm_device *dev,
320                                struct drm_atomic_state *state,
321                                bool async)
322 {
323         struct rockchip_drm_private *private = dev->dev_private;
324         struct rockchip_atomic_commit *commit = &private->commit;
325         int ret;
326
327         ret = drm_atomic_helper_prepare_planes(dev, state);
328         if (ret)
329                 return ret;
330
331         /* serialize outstanding asynchronous commits */
332         mutex_lock(&commit->lock);
333         flush_work(&commit->work);
334
335         drm_atomic_helper_swap_state(dev, state);
336
337         commit->dev = dev;
338         commit->state = state;
339
340         if (async)
341                 schedule_work(&commit->work);
342         else
343                 rockchip_atomic_commit_complete(commit);
344
345         mutex_unlock(&commit->lock);
346
347         return 0;
348 }
349
350 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
351         .fb_create = rockchip_user_fb_create,
352         .output_poll_changed = rockchip_drm_output_poll_changed,
353         .atomic_check = drm_atomic_helper_check,
354         .atomic_commit = rockchip_drm_atomic_commit,
355 };
356
357 struct drm_framebuffer *
358 rockchip_drm_framebuffer_init(struct drm_device *dev,
359                               struct drm_mode_fb_cmd2 *mode_cmd,
360                               struct drm_gem_object *obj)
361 {
362         struct drm_framebuffer *fb;
363
364         fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
365         if (IS_ERR(fb))
366                 return NULL;
367
368         return fb;
369 }
370
371 void rockchip_drm_mode_config_init(struct drm_device *dev)
372 {
373         dev->mode_config.min_width = 0;
374         dev->mode_config.min_height = 0;
375
376         /*
377          * set max width and height as default value(4096x4096).
378          * this value would be used to check framebuffer size limitation
379          * at drm_mode_addfb().
380          */
381         dev->mode_config.max_width = 8192;
382         dev->mode_config.max_height = 8192;
383
384         dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
385 }