c261a166ca22277ae828ce517cac229139b75e02
[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 dma_addr_t rockchip_fb_get_dma_addr(struct drm_framebuffer *fb,
37                                     unsigned int plane)
38 {
39         struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
40
41         if (WARN_ON(plane >= ROCKCHIP_MAX_FB_BUFFER))
42                 return 0;
43
44         return rk_fb->dma_addr[plane];
45 }
46
47 static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
48 {
49         struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
50         struct drm_gem_object *obj;
51         int i;
52
53         for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++) {
54                 obj = rockchip_fb->obj[i];
55                 if (obj)
56                         drm_gem_object_unreference_unlocked(obj);
57         }
58
59 #ifndef MODULE
60         if (rockchip_fb->logo)
61                 rockchip_free_loader_memory(fb->dev);
62 #else
63         WARN_ON(rockchip_fb->logo);
64 #endif
65
66         drm_framebuffer_cleanup(fb);
67         kfree(rockchip_fb);
68 }
69
70 static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
71                                          struct drm_file *file_priv,
72                                          unsigned int *handle)
73 {
74         struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
75
76         return drm_gem_handle_create(file_priv,
77                                      rockchip_fb->obj[0], handle);
78 }
79
80 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
81         .destroy        = rockchip_drm_fb_destroy,
82         .create_handle  = rockchip_drm_fb_create_handle,
83 };
84
85 struct drm_framebuffer *
86 rockchip_fb_alloc(struct drm_device *dev, struct drm_mode_fb_cmd2 *mode_cmd,
87                   struct drm_gem_object **obj, struct rockchip_logo *logo,
88                   unsigned int num_planes)
89 {
90         struct rockchip_drm_fb *rockchip_fb;
91         struct rockchip_gem_object *rk_obj;
92         int ret = 0;
93         int i;
94
95         rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
96         if (!rockchip_fb)
97                 return ERR_PTR(-ENOMEM);
98
99         drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
100
101         ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
102                                    &rockchip_drm_fb_funcs);
103         if (ret) {
104                 dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
105                         ret);
106                 goto err_free_fb;
107         }
108
109         if (obj) {
110                 for (i = 0; i < num_planes; i++)
111                         rockchip_fb->obj[i] = obj[i];
112
113                 for (i = 0; i < num_planes; i++) {
114                         rk_obj = to_rockchip_obj(obj[i]);
115                         rockchip_fb->dma_addr[i] = rk_obj->dma_addr;
116                 }
117 #ifndef MODULE
118         } else if (logo) {
119                 rockchip_fb->dma_addr[0] = logo->dma_addr;
120                 rockchip_fb->logo = logo;
121                 logo->count++;
122 #endif
123         } else {
124                 ret = -EINVAL;
125                 dev_err(dev->dev, "Failed to find available buffer\n");
126                 goto err_deinit_drm_fb;
127         }
128
129         return &rockchip_fb->fb;
130
131 err_deinit_drm_fb:
132         drm_framebuffer_cleanup(&rockchip_fb->fb);
133 err_free_fb:
134         kfree(rockchip_fb);
135         return ERR_PTR(ret);
136 }
137
138 static struct drm_framebuffer *
139 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
140                         struct drm_mode_fb_cmd2 *mode_cmd)
141 {
142         struct drm_framebuffer *fb;
143         struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
144         struct drm_gem_object *obj;
145         unsigned int hsub;
146         unsigned int vsub;
147         int num_planes;
148         int ret;
149         int i;
150
151         hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
152         vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
153         num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
154                          ROCKCHIP_MAX_FB_BUFFER);
155
156         for (i = 0; i < num_planes; i++) {
157                 unsigned int width = mode_cmd->width / (i ? hsub : 1);
158                 unsigned int height = mode_cmd->height / (i ? vsub : 1);
159                 unsigned int min_size;
160                 unsigned int bpp =
161                         drm_format_plane_bpp(mode_cmd->pixel_format, i);
162
163                 obj = drm_gem_object_lookup(dev, file_priv,
164                                             mode_cmd->handles[i]);
165                 if (!obj) {
166                         dev_err(dev->dev, "Failed to lookup GEM object\n");
167                         ret = -ENXIO;
168                         goto err_gem_object_unreference;
169                 }
170
171                 min_size = (height - 1) * mode_cmd->pitches[i] +
172                         mode_cmd->offsets[i] + roundup(width * bpp, 8) / 8;
173                 if (obj->size < min_size) {
174                         drm_gem_object_unreference_unlocked(obj);
175                         ret = -EINVAL;
176                         goto err_gem_object_unreference;
177                 }
178                 objs[i] = obj;
179         }
180
181         fb = rockchip_fb_alloc(dev, mode_cmd, objs, NULL, i);
182         if (IS_ERR(fb)) {
183                 ret = PTR_ERR(fb);
184                 goto err_gem_object_unreference;
185         }
186
187         return fb;
188
189 err_gem_object_unreference:
190         for (i--; i >= 0; i--)
191                 drm_gem_object_unreference_unlocked(objs[i]);
192         return ERR_PTR(ret);
193 }
194
195 static void rockchip_drm_output_poll_changed(struct drm_device *dev)
196 {
197         struct rockchip_drm_private *private = dev->dev_private;
198         struct drm_fb_helper *fb_helper = private->fbdev_helper;
199
200         if (fb_helper)
201                 drm_fb_helper_hotplug_event(fb_helper);
202 }
203
204 static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
205 {
206         struct rockchip_drm_private *priv = crtc->dev->dev_private;
207         int pipe = drm_crtc_index(crtc);
208         const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
209
210         if (crtc_funcs && crtc_funcs->wait_for_update)
211                 crtc_funcs->wait_for_update(crtc);
212 }
213
214 /*
215  * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
216  * have hardware counters for neither vblanks nor scanlines, which results in
217  * a race where:
218  *                              | <-- HW vsync irq and reg take effect
219  *             plane_commit --> |
220  *      get_vblank and wait --> |
221  *                              | <-- handle_vblank, vblank->count + 1
222  *               cleanup_fb --> |
223  *              iommu crash --> |
224  *                              | <-- HW vsync irq and reg take effect
225  *
226  * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
227  * of waiting for vblank_count to change.
228  */
229 static void
230 rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
231 {
232         struct drm_crtc_state *old_crtc_state;
233         struct drm_crtc *crtc;
234         int i, ret;
235
236         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
237                 /* No one cares about the old state, so abuse it for tracking
238                  * and store whether we hold a vblank reference (and should do a
239                  * vblank wait) in the ->enable boolean.
240                  */
241                 old_crtc_state->enable = false;
242
243                 if (!crtc->state->active)
244                         continue;
245
246                 if (!drm_atomic_helper_framebuffer_changed(dev,
247                                 old_state, crtc))
248                         continue;
249
250                 ret = drm_crtc_vblank_get(crtc);
251                 if (ret != 0)
252                         continue;
253
254                 old_crtc_state->enable = true;
255         }
256
257         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
258                 if (!old_crtc_state->enable)
259                         continue;
260
261                 rockchip_crtc_wait_for_update(crtc);
262                 drm_crtc_vblank_put(crtc);
263         }
264 }
265
266 static void
267 rockchip_atomic_commit_complete(struct rockchip_atomic_commit *commit)
268 {
269         struct drm_atomic_state *state = commit->state;
270         struct drm_device *dev = commit->dev;
271
272         /*
273          * TODO: do fence wait here.
274          */
275
276         /*
277          * Rockchip crtc support runtime PM, can't update display planes
278          * when crtc is disabled.
279          *
280          * drm_atomic_helper_commit comments detail that:
281          *     For drivers supporting runtime PM the recommended sequence is
282          *
283          *     drm_atomic_helper_commit_modeset_disables(dev, state);
284          *
285          *     drm_atomic_helper_commit_modeset_enables(dev, state);
286          *
287          *     drm_atomic_helper_commit_planes(dev, state, true);
288          *
289          * See the kerneldoc entries for these three functions for more details.
290          */
291         drm_atomic_helper_commit_modeset_disables(dev, state);
292
293         drm_atomic_helper_commit_modeset_enables(dev, state);
294
295         drm_atomic_helper_commit_planes(dev, state, true);
296
297         rockchip_atomic_wait_for_complete(dev, state);
298
299         drm_atomic_helper_cleanup_planes(dev, state);
300
301         drm_atomic_state_free(state);
302 }
303
304 void rockchip_drm_atomic_work(struct work_struct *work)
305 {
306         struct rockchip_atomic_commit *commit = container_of(work,
307                                         struct rockchip_atomic_commit, work);
308
309         rockchip_atomic_commit_complete(commit);
310 }
311
312 int rockchip_drm_atomic_commit(struct drm_device *dev,
313                                struct drm_atomic_state *state,
314                                bool async)
315 {
316         struct rockchip_drm_private *private = dev->dev_private;
317         struct rockchip_atomic_commit *commit = &private->commit;
318         int ret;
319
320         ret = drm_atomic_helper_prepare_planes(dev, state);
321         if (ret)
322                 return ret;
323
324         /* serialize outstanding asynchronous commits */
325         mutex_lock(&commit->lock);
326         flush_work(&commit->work);
327
328         drm_atomic_helper_swap_state(dev, state);
329
330         commit->dev = dev;
331         commit->state = state;
332
333         if (async)
334                 schedule_work(&commit->work);
335         else
336                 rockchip_atomic_commit_complete(commit);
337
338         mutex_unlock(&commit->lock);
339
340         return 0;
341 }
342
343 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
344         .fb_create = rockchip_user_fb_create,
345         .output_poll_changed = rockchip_drm_output_poll_changed,
346         .atomic_check = drm_atomic_helper_check,
347         .atomic_commit = rockchip_drm_atomic_commit,
348 };
349
350 struct drm_framebuffer *
351 rockchip_drm_framebuffer_init(struct drm_device *dev,
352                               struct drm_mode_fb_cmd2 *mode_cmd,
353                               struct drm_gem_object *obj)
354 {
355         struct drm_framebuffer *fb;
356
357         fb = rockchip_fb_alloc(dev, mode_cmd, &obj, NULL, 1);
358         if (IS_ERR(fb))
359                 return NULL;
360
361         return fb;
362 }
363
364 void rockchip_drm_mode_config_init(struct drm_device *dev)
365 {
366         dev->mode_config.min_width = 0;
367         dev->mode_config.min_height = 0;
368
369         /*
370          * set max width and height as default value(4096x4096).
371          * this value would be used to check framebuffer size limitation
372          * at drm_mode_addfb().
373          */
374         dev->mode_config.max_width = 8192;
375         dev->mode_config.max_height = 8192;
376
377         dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
378 }