BACKPORT: drm/exynos: use private plane for crtc
[cascardo/linux.git] / drivers / gpu / drm / exynos / exynos_drm_crtc.c
1 /* exynos_drm_crtc.c
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Authors:
5  *      Inki Dae <inki.dae@samsung.com>
6  *      Joonyoung Shim <jy0922.shim@samsung.com>
7  *      Seung-Woo Kim <sw0312.kim@samsung.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "drmP.h"
30 #include "drm_crtc_helper.h"
31
32 #include <linux/kfifo.h>
33
34 #include "exynos_drm_crtc.h"
35 #include "exynos_drm_drv.h"
36 #include "exynos_drm_fb.h"
37 #include "exynos_drm_encoder.h"
38 #include "exynos_drm_display.h"
39 #include "exynos_drm_gem.h"
40 #include "exynos_trace.h"
41 #include "exynos_drm_plane.h"
42
43 #define KDS_WAITALL_MAX_TRIES 15
44
45 struct exynos_drm_flip_desc {
46         struct drm_framebuffer  *fb;
47 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
48         struct kds_resource_set *kds;
49 #endif
50 };
51
52 /*
53  * Exynos specific crtc structure.
54  *
55  * @drm_crtc: crtc object.
56  * @current_fb: current fb that is being scanned out
57  * @event: vblank event that is currently queued for flip
58  * @plane: pointer of private plane object for this crtc
59  * @pipe: a crtc index created at load() with a new crtc object creation
60  *      and the crtc object would be set to private->crtc array
61  *      to get a crtc object corresponding to this pipe from private->crtc
62  *      array when irq interrupt occured. the reason of using this pipe is that
63  *      drm framework doesn't support multiple irq yet.
64  *      we can refer to the crtc to current hardware interrupt occured through
65  *      this pipe value.
66  * @flip_pending: there is a flip pending that we need to process next vblank
67  */
68 struct exynos_drm_crtc {
69         struct drm_crtc                 drm_crtc;
70         struct drm_pending_vblank_event *event;
71         DECLARE_KFIFO(flip_fifo, struct exynos_drm_flip_desc, 2);
72         struct exynos_drm_flip_desc     scanout_desc;
73         struct exynos_drm_display       *display;
74         struct drm_plane                *plane;
75         unsigned int                    pipe;
76         atomic_t                        flip_pending;
77 };
78
79 #define to_exynos_crtc(x)       container_of(x, struct exynos_drm_crtc,\
80                                 drm_crtc)
81
82 void exynos_drm_crtc_apply(struct drm_crtc *crtc,
83                 struct exynos_drm_overlay *overlay)
84 {
85         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
86         struct exynos_drm_display *display = exynos_crtc->display;
87
88         if (display->controller_ops && display->controller_ops->mode_set)
89                 display->controller_ops->mode_set(display->controller_ctx,
90                                 overlay);
91
92         display->pipe = exynos_crtc->pipe;
93
94         if (display->controller_ops && display->controller_ops->win_commit)
95                 display->controller_ops->win_commit(display->controller_ctx,
96                                 overlay->zpos);
97 }
98
99 void exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
100                                struct drm_framebuffer *fb,
101                                struct drm_display_mode *mode,
102                                struct exynos_drm_crtc_pos *pos)
103 {
104         struct exynos_drm_gem_buf *buffer;
105         unsigned int actual_w;
106         unsigned int actual_h;
107         struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
108         int nr = exynos_drm_format_num_buffers(fb->pixel_format);
109         int i;
110
111         for (i = 0; i < nr; i++) {
112                 buffer = exynos_drm_fb_buffer(exynos_fb, i);
113
114                 overlay->dma_addr[i] = buffer->dma_addr;
115
116                 DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
117                                 i, (unsigned long)overlay->dma_addr[i]);
118         }
119
120         actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
121         actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
122
123         /* set drm framebuffer data. */
124         overlay->fb_x = pos->fb_x;
125         overlay->fb_y = pos->fb_y;
126         overlay->fb_width = min(pos->fb_w, actual_w);
127         overlay->fb_height = min(pos->fb_h, actual_h);
128         overlay->fb_pitch = fb->pitches[0];
129         overlay->bpp = fb->bits_per_pixel;
130         overlay->pixel_format = fb->pixel_format;
131
132         /* set overlay range to be displayed. */
133         overlay->crtc_x = pos->crtc_x;
134         overlay->crtc_y = pos->crtc_y;
135         overlay->crtc_width = actual_w;
136         overlay->crtc_height = actual_h;
137         overlay->crtc_htotal = mode->crtc_htotal;
138         overlay->crtc_hsync_len = mode->hsync_end - mode->hsync_start;
139         overlay->crtc_vtotal = mode->crtc_vtotal;
140         overlay->crtc_vsync_len = mode->vsync_end - mode->vsync_start;
141
142         /* set drm mode data. */
143         overlay->mode_width = mode->hdisplay;
144         overlay->mode_height = mode->vdisplay;
145         overlay->refresh = mode->vrefresh;
146         overlay->scan_flag = mode->flags;
147
148         DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
149                         overlay->crtc_x, overlay->crtc_y,
150                         overlay->crtc_width, overlay->crtc_height);
151 }
152
153 void exynos_drm_overlay_disable(struct drm_crtc *crtc, int zpos)
154 {
155         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
156         struct exynos_drm_display *display = exynos_crtc->display;
157
158         if (display->controller_ops && display->controller_ops->win_disable)
159                 display->controller_ops->win_disable(display->controller_ctx,
160                                 zpos);
161 }
162
163 static void exynos_drm_crtc_update(struct drm_crtc *crtc,
164                                    struct drm_framebuffer *fb)
165 {
166         struct exynos_drm_crtc *exynos_crtc;
167         struct exynos_drm_overlay *overlay;
168         struct exynos_drm_crtc_pos pos;
169         struct drm_display_mode *mode = &crtc->mode;
170
171         exynos_crtc = to_exynos_crtc(crtc);
172         overlay = get_exynos_drm_overlay(exynos_crtc->plane);
173
174         memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
175
176         /* it means the offset of framebuffer to be displayed. */
177         pos.fb_x = crtc->x;
178         pos.fb_y = crtc->y;
179         pos.fb_w = fb->width;
180         pos.fb_h = fb->height;
181
182         /* OSD position to be displayed. */
183         pos.crtc_x = 0;
184         pos.crtc_y = 0;
185         pos.crtc_w = fb->width - crtc->x;
186         pos.crtc_h = fb->height - crtc->y;
187
188         exynos_drm_overlay_update(overlay, fb, mode, &pos);
189 }
190
191 static void exynos_drm_crtc_flip_complete(struct drm_device *dev,
192                                           struct drm_pending_vblank_event *e)
193 {
194         struct timeval now;
195         unsigned long flags;
196
197         do_gettimeofday(&now);
198         e->event.sequence = 0;
199         e->event.tv_sec = now.tv_sec;
200         e->event.tv_usec = now.tv_usec;
201         spin_lock_irqsave(&dev->event_lock, flags);
202         list_add_tail(&e->base.link, &e->base.file_priv->event_list);
203         spin_unlock_irqrestore(&dev->event_lock, flags);
204         wake_up_interruptible(&e->base.file_priv->event_wait);
205         trace_exynos_fake_flip_complete(e->pipe);
206 }
207
208 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
209 static void exynos_drm_crtc_wait_and_release_kds(
210                 struct exynos_drm_flip_desc *desc)
211 {
212         struct exynos_drm_fb *exynos_fb;
213         struct exynos_drm_gem_obj *gem_ob;
214         struct dma_buf *buf;
215         unsigned long shared = 0UL;
216         struct kds_resource *resource_list;
217         struct kds_resource_set *resource_set;
218         int i;
219
220         if (!desc->fb) {
221                 BUG_ON(desc->kds);
222                 return;
223         }
224
225         if (desc->kds)
226                 kds_resource_set_release(&desc->kds);
227
228         exynos_fb = to_exynos_fb(desc->fb);
229         gem_ob = (struct exynos_drm_gem_obj *)exynos_fb->exynos_gem_obj[0];
230         buf = gem_ob->base.export_dma_buf;
231         if (!buf)
232                 return;
233
234         if (unlikely(!exynos_fb->dma_buf)) {
235                 get_dma_buf(buf);
236                 exynos_fb->dma_buf = buf;
237         }
238         BUG_ON(exynos_fb->dma_buf !=  buf);
239
240         /* Synchronously wait for the frame to render */
241         resource_list = get_dma_buf_kds_resource(buf);
242
243         for (i = 0; i < KDS_WAITALL_MAX_TRIES; i++) {
244                 resource_set = kds_waitall(1, &shared, &resource_list,
245                                 msecs_to_jiffies(1000));
246                 if (PTR_ERR(resource_set) != -ERESTARTSYS)
247                         break;
248         }
249         if (IS_ERR(resource_set)) {
250                 DRM_ERROR("kds_waitall failed with ret=%ld\n",
251                                 PTR_ERR(resource_set));
252                 return;
253         } else if (!resource_set) {
254                 DRM_ERROR("kds_waitall timed out\n");
255                 return;
256         }
257
258         /* Clean up the remaining resource so we're not holding onto anything */
259         kds_resource_set_release(&resource_set);
260 }
261
262 static void exynos_drm_crtc_release_flips(struct drm_crtc *crtc)
263 {
264         struct drm_device *drm_dev = crtc->dev;
265         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
266         struct exynos_drm_flip_desc *cur_desc = &exynos_crtc->scanout_desc;
267         struct exynos_drm_flip_desc next_desc;
268         unsigned int ret;
269
270         DRM_DEBUG_KMS("[CRTC:%d]\n", DRM_BASE_ID(crtc));
271
272         if (cur_desc->kds)
273                 kds_resource_set_release(&cur_desc->kds);
274
275         /* If there aren't any pending frames, be merry and exit */
276         if (!kfifo_len(&exynos_crtc->flip_fifo))
277                 return;
278
279         /* Loop through the fifo and drop all frames except the last */
280         while (kfifo_len(&exynos_crtc->flip_fifo) > 1) {
281                 ret = kfifo_get(&exynos_crtc->flip_fifo, &next_desc);
282                 BUG_ON(!ret);
283
284                 if (next_desc.fb)
285                         exynos_drm_fb_put(to_exynos_fb(next_desc.fb));
286                 if (next_desc.kds)
287                         kds_resource_set_release(&next_desc.kds);
288         }
289
290         /* Now we'll promote the pending frame to front */
291         ret = kfifo_get(&exynos_crtc->flip_fifo, &next_desc);
292         BUG_ON(!ret);
293
294         atomic_set(&exynos_crtc->flip_pending, 0);
295
296         exynos_drm_crtc_wait_and_release_kds(&next_desc);
297
298         to_exynos_fb(next_desc.fb)->rendered = true;
299         exynos_drm_crtc_update(crtc, next_desc.fb);
300         exynos_drm_crtc_apply(crtc, get_exynos_drm_overlay(exynos_crtc->plane));
301         to_exynos_fb(next_desc.fb)->prepared = true;
302
303         if (exynos_crtc->event) {
304                 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
305                 exynos_crtc->event = NULL;
306         }
307
308         *cur_desc = next_desc;
309 }
310 #endif
311
312 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
313 {
314         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
315         struct exynos_drm_display *display = exynos_crtc->display;
316
317         DRM_DEBUG_KMS("[CRTC:%d] [DPMS:%s]\n", DRM_BASE_ID(crtc),
318                         drm_get_dpms_name(mode));
319
320         if (display->controller_ops && display->controller_ops->dpms)
321                 display->controller_ops->dpms(display->controller_ctx, mode);
322
323 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
324         if (mode != DRM_MODE_DPMS_ON)
325                 exynos_drm_crtc_release_flips(crtc);
326 #endif
327 }
328
329 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
330 {
331         DRM_DEBUG_KMS("[CRTC:%d]\n", DRM_BASE_ID(crtc));
332
333         /* drm framework doesn't check NULL. */
334 }
335
336 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
337                                      struct drm_framebuffer *fb,
338                                      struct drm_pending_vblank_event *event);
339
340 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
341 {
342         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
343         struct exynos_drm_display *display = exynos_crtc->display;
344         int ret;
345
346         DRM_DEBUG_KMS("[CRTC:%d]\n", DRM_BASE_ID(crtc));
347
348         /*
349          * when set_crtc is requested from user or at booting time,
350          * crtc->commit would be called without dpms call so crtc->dpms
351          * should be called with DRM_MODE_DPMS_ON for the controller power
352          * to be on.
353          */
354         exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
355
356         ret = exynos_drm_crtc_page_flip(crtc, crtc->fb, NULL);
357         if (ret)
358                 DRM_ERROR("page_flip failed\n");
359
360         if (display->controller_ops && display->controller_ops->commit)
361                 display->controller_ops->commit(display->controller_ctx);
362 }
363
364 static bool
365 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
366                             struct drm_display_mode *mode,
367                             struct drm_display_mode *adjusted_mode)
368 {
369         DRM_DEBUG_KMS("[CRTC:%d] [MODE:%d:%s]\n", DRM_BASE_ID(crtc),
370                         DRM_BASE_ID(mode), mode->name);
371
372         /* drm framework doesn't check NULL */
373         return true;
374 }
375
376 static int
377 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
378                           struct drm_display_mode *adjusted_mode, int x, int y,
379                           struct drm_framebuffer *old_fb)
380 {
381         struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
382         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
383         struct exynos_drm_display *display = exynos_crtc->display;
384         struct drm_framebuffer *fb = crtc->fb;
385         int ret;
386
387         DRM_DEBUG_KMS("[CRTC:%d] [MODE:%d:%s] @ (%d, %d) [OLD_FB:%d]\n",
388                         DRM_BASE_ID(crtc), DRM_BASE_ID(mode), mode->name, x, y,
389                         DRM_BASE_ID(old_fb));
390
391         if (!fb)
392                 return -EINVAL;
393
394         /*
395          * copy the mode data adjusted by mode_fixup() into crtc->mode
396          * so that hardware can be seet to proper mode.
397          */
398         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
399
400         /* We should never timeout here. */
401         ret = wait_event_timeout(dev_priv->wait_vsync_queue,
402                                  kfifo_is_empty(&exynos_crtc->flip_fifo),
403                                  DRM_HZ/20);
404         if (!ret)
405                 DRM_ERROR("Timed out waiting for flips to complete\n");
406
407         exynos_drm_crtc_update(crtc, fb);
408
409         if (display->controller_ops && display->controller_ops->mode_set)
410                 display->controller_ops->mode_set(display->controller_ctx,
411                                 get_exynos_drm_overlay(exynos_crtc->plane));
412
413         return 0;
414 }
415
416 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
417                                           struct drm_framebuffer *old_fb)
418 {
419         struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
420         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
421         struct drm_framebuffer *fb = crtc->fb;
422         int ret;
423
424         DRM_DEBUG_KMS("[CRTC:%d] @ (%d, %d) [OLD_FB:%d]\n",
425                         DRM_BASE_ID(crtc), x, y, DRM_BASE_ID(old_fb));
426
427         if (!fb)
428                 return -EINVAL;
429
430         /* We should never timeout here. */
431         ret = wait_event_timeout(dev_priv->wait_vsync_queue,
432                                  kfifo_is_empty(&exynos_crtc->flip_fifo),
433                                  DRM_HZ/20);
434         if (!ret)
435                 DRM_ERROR("Timed out waiting for flips to complete\n");
436
437         ret = exynos_drm_crtc_page_flip(crtc, fb, NULL);
438         if (ret)
439                 DRM_ERROR("page_flip failed\n");
440
441         return ret;
442 }
443
444 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
445 {
446         DRM_DEBUG_KMS("[CRTC:%d]\n", DRM_BASE_ID(crtc));
447         /* drm framework doesn't check NULL */
448 }
449
450 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
451         .dpms           = exynos_drm_crtc_dpms,
452         .prepare        = exynos_drm_crtc_prepare,
453         .commit         = exynos_drm_crtc_commit,
454         .mode_fixup     = exynos_drm_crtc_mode_fixup,
455         .mode_set       = exynos_drm_crtc_mode_set,
456         .mode_set_base  = exynos_drm_crtc_mode_set_base,
457         .load_lut       = exynos_drm_crtc_load_lut,
458 };
459
460 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
461 void exynos_drm_kds_callback(void *callback_parameter, void *callback_extra_parameter)
462 {
463         struct drm_framebuffer  *fb = callback_parameter;
464         struct drm_crtc *crtc =  callback_extra_parameter;
465         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
466
467         /*
468          * Under rare circumstances (modeset followed by flip) it is possible
469          * to have two fbs ready in time for the same vblank. We don't want to
470          * drop the latter fb because it is most recent. We can't drop the
471          * former either because we've already applied it and the mixer does
472          * not allow applying more than one frame on a vblank. So instead of
473          * dropping a frame, we store the latter frame in the extra slot and
474          * apply it on the next vblank.
475          */
476
477         to_exynos_fb(fb)->rendered = true;
478
479         if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 0, 1)) {
480                 struct exynos_drm_overlay *overlay;
481
482                 overlay = get_exynos_drm_overlay(exynos_crtc->plane);
483                 exynos_drm_crtc_update(crtc, fb);
484                 exynos_drm_crtc_apply(crtc, overlay);
485                 to_exynos_fb(fb)->prepared = true;
486         }
487 }
488 #endif
489
490 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
491                                      struct drm_framebuffer *fb,
492                                      struct drm_pending_vblank_event *event)
493 {
494         struct drm_device *dev = crtc->dev;
495         struct exynos_drm_private *dev_priv = dev->dev_private;
496         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
497         struct exynos_drm_flip_desc flip_desc;
498         bool send_event = false;
499         int ret;
500 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
501         struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
502         struct exynos_drm_gem_obj *gem_ob = (struct exynos_drm_gem_obj *)exynos_fb->exynos_gem_obj[0];
503 #endif
504         DRM_DEBUG_KMS("[CRTC:%d] [FB:%d]\n", DRM_BASE_ID(crtc),
505                         DRM_BASE_ID(fb));
506
507         /*
508          * the pipe from user always is 0 so we can set pipe number
509          * of current owner to event.
510          */
511         if (event)
512                 event->pipe = exynos_crtc->pipe;
513
514         ret = drm_vblank_get(dev, exynos_crtc->pipe);
515         if (ret) {
516                 DRM_ERROR("Unable to get vblank\n");
517                 return -EINVAL;
518         }
519
520         if (kfifo_is_full(&exynos_crtc->flip_fifo)) {
521                 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
522                 ret = -EBUSY;
523                 goto fail_queue_full;
524         }
525
526         /* Send flip event if no flips pending. */
527         BUG_ON(exynos_crtc->event);
528         if (kfifo_is_empty(&exynos_crtc->flip_fifo))
529                 send_event = true;
530
531         exynos_drm_fb_get(exynos_fb);
532
533 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
534         flip_desc.fb = fb;
535         exynos_fb->rendered = false;
536         exynos_fb->prepared = false;
537         if (gem_ob->base.export_dma_buf) {
538                 struct dma_buf *buf = gem_ob->base.export_dma_buf;
539                 unsigned long shared = 0UL;
540                 struct kds_resource *res_list = get_dma_buf_kds_resource(buf);
541
542                 /*
543                  * If we don't already have a reference to the dma_buf,
544                  * grab one now. We'll release it in exynos_drm_fb_destory().
545                  */
546                 if (!exynos_fb->dma_buf) {
547                         get_dma_buf(buf);
548                         exynos_fb->dma_buf = buf;
549                 }
550                 BUG_ON(exynos_fb->dma_buf !=  buf);
551
552                 /* Waiting for the KDS resource*/
553                 ret = kds_async_waitall(&flip_desc.kds, KDS_FLAG_LOCKED_WAIT,
554                                         &dev_priv->kds_cb, fb, crtc, 1,
555                                         &shared, &res_list);
556                 if (ret) {
557                         DRM_ERROR("kds_async_waitall failed: %d\n", ret);
558                         goto fail_kds;
559                 }
560         } else {
561                 /*
562                  * For normal page-flip (i.e. non-modeset) we should
563                  * never be flipping a non-kds buffer.
564                  */
565                 if (event)
566                         DRM_ERROR("flipping a non-kds buffer\n");
567                 flip_desc.kds = NULL;
568                 exynos_drm_kds_callback(fb, crtc);
569         }
570 #endif
571
572         if (event) {
573                 if (send_event)
574                         exynos_drm_crtc_flip_complete(dev, event);
575                 else
576                         exynos_crtc->event = event;
577         }
578         kfifo_put(&exynos_crtc->flip_fifo, &flip_desc);
579         crtc->fb = fb;
580
581         trace_exynos_flip_request(exynos_crtc->pipe);
582
583         return 0;
584
585 fail_kds:
586         exynos_drm_fb_put(exynos_fb);
587 fail_queue_full:
588         drm_vblank_put(dev, exynos_crtc->pipe);
589         return ret;
590 }
591
592 void exynos_drm_crtc_finish_pageflip(struct drm_device *drm_dev, int crtc_idx)
593 {
594         struct exynos_drm_private *dev_priv = drm_dev->dev_private;
595         struct drm_crtc *crtc = dev_priv->crtc[crtc_idx];
596         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
597         struct exynos_drm_flip_desc *cur_descp = &exynos_crtc->scanout_desc;
598         struct exynos_drm_flip_desc next_desc;
599
600         /* set wait vsync wake up queue. */
601         DRM_WAKEUP(&dev_priv->wait_vsync_queue);
602
603         if (!atomic_read(&exynos_crtc->flip_pending))
604                 return;
605         if (!kfifo_peek(&exynos_crtc->flip_fifo, &next_desc))
606                 return;
607         if (!to_exynos_fb(next_desc.fb)->prepared)
608                 return;
609         if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 1, 0))
610                 return;
611
612         trace_exynos_flip_complete(crtc_idx);
613
614         if (cur_descp->fb)
615                 exynos_drm_fb_put(to_exynos_fb(cur_descp->fb));
616         if (cur_descp->kds)
617                 kds_resource_set_release(&cur_descp->kds);
618         *cur_descp = next_desc;
619         kfifo_skip(&exynos_crtc->flip_fifo);
620
621         if (exynos_crtc->event) {
622                 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
623                 exynos_crtc->event = NULL;
624         }
625
626         /*
627          * exynos_drm_kds_callback can't update the shadow registers if there
628          * is a pending flip ahead. So we do the update here.
629          */
630         if (kfifo_peek(&exynos_crtc->flip_fifo, &next_desc)) {
631                 if (unlikely(to_exynos_fb(next_desc.fb)->rendered) &&
632                     !atomic_cmpxchg(&exynos_crtc->flip_pending, 0, 1)) {
633                         struct exynos_drm_overlay *overlay;
634
635                         overlay = get_exynos_drm_overlay(exynos_crtc->plane);
636                         exynos_drm_crtc_update(crtc, next_desc.fb);
637                         exynos_drm_crtc_apply(crtc, overlay);
638                         to_exynos_fb(next_desc.fb)->prepared = true;
639                 }
640         }
641
642         drm_vblank_put(drm_dev, crtc_idx);
643 }
644
645 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
646 {
647         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
648         struct exynos_drm_private *private = crtc->dev->dev_private;
649
650         DRM_DEBUG_KMS("[CRTC:%d]\n", DRM_BASE_ID(crtc));
651
652         private->crtc[exynos_crtc->pipe] = NULL;
653
654         drm_crtc_cleanup(crtc);
655         kfree(exynos_crtc);
656 }
657
658 static struct drm_crtc_funcs exynos_crtc_funcs = {
659         .set_config     = drm_crtc_helper_set_config,
660         .page_flip      = exynos_drm_crtc_page_flip,
661         .destroy        = exynos_drm_crtc_destroy,
662 };
663
664 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr,
665                 struct exynos_drm_display *display)
666 {
667         struct exynos_drm_crtc *exynos_crtc;
668         struct exynos_drm_private *private = dev->dev_private;
669         struct drm_crtc *crtc;
670
671         DRM_DEBUG_KMS("[DEV:%s] pipe: %d\n", dev->devname, nr);
672
673         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
674         if (!exynos_crtc) {
675                 DRM_ERROR("failed to allocate exynos crtc\n");
676                 return -ENOMEM;
677         }
678
679         INIT_KFIFO(exynos_crtc->flip_fifo);
680         exynos_crtc->pipe = nr;
681         exynos_crtc->display = display;
682
683         exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
684         if (!exynos_crtc->plane) {
685                 kfree(exynos_crtc);
686                 return -ENOMEM;
687         }
688
689         crtc = &exynos_crtc->drm_crtc;
690
691         private->crtc[nr] = crtc;
692
693         drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
694         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
695
696         DRM_DEBUG_KMS("Created [CRTC:%d]\n", DRM_BASE_ID(crtc));
697
698         return 0;
699 }
700
701 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
702 {
703         struct exynos_drm_private *private = dev->dev_private;
704         struct exynos_drm_crtc *exynos_crtc =
705                 to_exynos_crtc(private->crtc[crtc]);
706         struct exynos_drm_display *display = exynos_crtc->display;
707         int ret = 0;
708
709         DRM_DEBUG_KMS("[DEV:%s] crtc: %d\n", dev->devname, crtc);
710
711         if (display->pipe == -1)
712                 display->pipe = crtc;
713
714         if (display->controller_ops && display->controller_ops->enable_vblank)
715                 ret = display->controller_ops->enable_vblank(
716                                 display->controller_ctx, crtc);
717         return ret;
718 }
719
720 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
721 {
722         struct exynos_drm_private *private = dev->dev_private;
723         struct exynos_drm_crtc *exynos_crtc =
724                 to_exynos_crtc(private->crtc[crtc]);
725         struct exynos_drm_display *display = exynos_crtc->display;
726
727         DRM_DEBUG_KMS("[DEV:%s] pipe: %d\n", dev->devname, crtc);
728
729         /*
730          * TODO(seanpaul): This seems like a hack. I don't think it's actually
731          * needed for 2 reasons:
732          *   (1) disable_vblank implies vblank has been enabled. If
733          *       enable_vblank hasn't already been called, that's a bug.
734          *   (2) Even if (1) isn't true, this function should just disable an
735          *       interrupt, and shouldn't affect pipe.
736          */
737         if (display->pipe == -1)
738                 display->pipe = crtc;
739
740         if (display->controller_ops && display->controller_ops->disable_vblank)
741                 display->controller_ops->disable_vblank(
742                                 display->controller_ctx);
743 }