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