db9167aca766d7396b4ff44ad6cf174f359569eb
[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_gem.h"
39 #include "exynos_trace.h"
40
41 #define KDS_WAITALL_MAX_TRIES 15
42
43 struct exynos_drm_flip_desc {
44         struct drm_framebuffer  *fb;
45 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
46         struct kds_resource_set *kds;
47 #endif
48 };
49
50 /*
51  * Exynos specific crtc structure.
52  *
53  * @drm_crtc: crtc object.
54  * @overlay: contain information common to display controller and hdmi and
55  *      contents of this overlay object would be copied to sub driver size.
56  * @current_fb: current fb that is being scanned out
57  * @event: vblank event that is currently queued for flip
58  * @pipe: a crtc index created at load() with a new crtc object creation
59  *      and the crtc object would be set to private->crtc array
60  *      to get a crtc object corresponding to this pipe from private->crtc
61  *      array when irq interrupt occured. the reason of using this pipe is that
62  *      drm framework doesn't support multiple irq yet.
63  *      we can refer to the crtc to current hardware interrupt occured through
64  *      this pipe value.
65  * @flip_pending: there is a flip pending that we need to process next vblank
66  */
67 struct exynos_drm_crtc {
68         struct drm_crtc                 drm_crtc;
69         struct exynos_drm_overlay       overlay;
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         unsigned int                    pipe;
74         atomic_t                        flip_pending;
75 };
76
77 #define to_exynos_crtc(x)       container_of(x, struct exynos_drm_crtc,\
78                                 drm_crtc)
79
80 static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
81 {
82         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
83         struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
84
85         exynos_drm_fn_encoder(crtc, overlay,
86                         exynos_drm_encoder_crtc_mode_set);
87         exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
88                         exynos_drm_encoder_crtc_commit);
89 }
90
91 void exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
92                                struct drm_framebuffer *fb,
93                                struct drm_display_mode *mode,
94                                struct exynos_drm_crtc_pos *pos)
95 {
96         struct exynos_drm_gem_buf *buffer;
97         unsigned int actual_w;
98         unsigned int actual_h;
99         struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
100         int nr = exynos_drm_format_num_buffers(fb->pixel_format);
101         int i;
102
103         for (i = 0; i < nr; i++) {
104                 buffer = exynos_drm_fb_buffer(exynos_fb, i);
105
106                 overlay->dma_addr[i] = buffer->dma_addr;
107
108                 DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
109                                 i, (unsigned long)overlay->dma_addr[i]);
110         }
111
112         actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
113         actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
114
115         /* set drm framebuffer data. */
116         overlay->fb_x = pos->fb_x;
117         overlay->fb_y = pos->fb_y;
118         overlay->fb_width = min(pos->fb_w, actual_w);
119         overlay->fb_height = min(pos->fb_h, actual_h);
120         overlay->fb_pitch = fb->pitches[0];
121         overlay->bpp = fb->bits_per_pixel;
122         overlay->pixel_format = fb->pixel_format;
123
124         /* set overlay range to be displayed. */
125         overlay->crtc_x = pos->crtc_x;
126         overlay->crtc_y = pos->crtc_y;
127         overlay->crtc_width = actual_w;
128         overlay->crtc_height = actual_h;
129         overlay->crtc_htotal = mode->crtc_htotal;
130         overlay->crtc_hsync_len = mode->hsync_end - mode->hsync_start;
131         overlay->crtc_vtotal = mode->crtc_vtotal;
132         overlay->crtc_vsync_len = mode->vsync_end - mode->vsync_start;
133
134         /* set drm mode data. */
135         overlay->mode_width = mode->hdisplay;
136         overlay->mode_height = mode->vdisplay;
137         overlay->refresh = mode->vrefresh;
138         overlay->scan_flag = mode->flags;
139
140         DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
141                         overlay->crtc_x, overlay->crtc_y,
142                         overlay->crtc_width, overlay->crtc_height);
143 }
144
145 static void exynos_drm_crtc_update(struct drm_crtc *crtc,
146                                    struct drm_framebuffer *fb)
147 {
148         struct exynos_drm_crtc *exynos_crtc;
149         struct exynos_drm_overlay *overlay;
150         struct exynos_drm_crtc_pos pos;
151         struct drm_display_mode *mode = &crtc->mode;
152
153         exynos_crtc = to_exynos_crtc(crtc);
154         overlay = &exynos_crtc->overlay;
155
156         memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
157
158         /* it means the offset of framebuffer to be displayed. */
159         pos.fb_x = crtc->x;
160         pos.fb_y = crtc->y;
161         pos.fb_w = fb->width;
162         pos.fb_h = fb->height;
163
164         /* OSD position to be displayed. */
165         pos.crtc_x = 0;
166         pos.crtc_y = 0;
167         pos.crtc_w = fb->width - crtc->x;
168         pos.crtc_h = fb->height - crtc->y;
169
170         exynos_drm_overlay_update(overlay, fb, mode, &pos);
171 }
172
173 static void exynos_drm_crtc_flip_complete(struct drm_device *dev,
174                                           struct drm_pending_vblank_event *e)
175 {
176         struct timeval now;
177         unsigned long flags;
178
179         do_gettimeofday(&now);
180         e->event.sequence = 0;
181         e->event.tv_sec = now.tv_sec;
182         e->event.tv_usec = now.tv_usec;
183         spin_lock_irqsave(&dev->event_lock, flags);
184         list_add_tail(&e->base.link, &e->base.file_priv->event_list);
185         spin_unlock_irqrestore(&dev->event_lock, flags);
186         wake_up_interruptible(&e->base.file_priv->event_wait);
187         trace_exynos_fake_flip_complete(e->pipe);
188 }
189
190 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
191 static void exynos_drm_crtc_wait_and_release_kds(
192                 struct exynos_drm_flip_desc *desc)
193 {
194         struct exynos_drm_fb *exynos_fb;
195         struct exynos_drm_gem_obj *gem_ob;
196         struct dma_buf *buf;
197         unsigned long shared = 0UL;
198         struct kds_resource *resource_list;
199         struct kds_resource_set *resource_set;
200         int i;
201
202         if (!desc->fb) {
203                 BUG_ON(desc->kds);
204                 return;
205         }
206
207         if (desc->kds)
208                 kds_resource_set_release(&desc->kds);
209
210         exynos_fb = to_exynos_fb(desc->fb);
211         gem_ob = (struct exynos_drm_gem_obj *)exynos_fb->exynos_gem_obj[0];
212         buf = gem_ob->base.export_dma_buf;
213         if (!buf)
214                 return;
215
216         if (unlikely(!exynos_fb->dma_buf)) {
217                 get_dma_buf(buf);
218                 exynos_fb->dma_buf = buf;
219         }
220         BUG_ON(exynos_fb->dma_buf !=  buf);
221
222         /* Synchronously wait for the frame to render */
223         resource_list = get_dma_buf_kds_resource(buf);
224
225         for (i = 0; i < KDS_WAITALL_MAX_TRIES; i++) {
226                 resource_set = kds_waitall(1, &shared, &resource_list,
227                                 msecs_to_jiffies(1000));
228                 if (PTR_ERR(resource_set) != -ERESTARTSYS)
229                         break;
230         }
231         if (IS_ERR(resource_set)) {
232                 DRM_ERROR("kds_waitall failed with ret=%ld\n",
233                                 PTR_ERR(resource_set));
234                 return;
235         } else if (!resource_set) {
236                 DRM_ERROR("kds_waitall timed out\n");
237                 return;
238         }
239
240         /* Clean up the remaining resource so we're not holding onto anything */
241         kds_resource_set_release(&resource_set);
242 }
243
244 static void exynos_drm_crtc_release_flips(struct drm_crtc *crtc)
245 {
246         struct drm_device *drm_dev = crtc->dev;
247         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
248         struct exynos_drm_flip_desc *cur_desc = &exynos_crtc->scanout_desc;
249         struct exynos_drm_flip_desc next_desc;
250         unsigned int ret;
251
252         if (cur_desc->kds)
253                 kds_resource_set_release(&cur_desc->kds);
254
255         /* If there aren't any pending frames, be merry and exit */
256         if (!kfifo_len(&exynos_crtc->flip_fifo))
257                 return;
258
259         /* Loop through the fifo and drop all frames except the last */
260         while (kfifo_len(&exynos_crtc->flip_fifo) > 1) {
261                 ret = kfifo_get(&exynos_crtc->flip_fifo, &next_desc);
262                 BUG_ON(!ret);
263
264                 if (next_desc.fb)
265                         exynos_drm_fb_put(to_exynos_fb(next_desc.fb));
266                 if (next_desc.kds)
267                         kds_resource_set_release(&next_desc.kds);
268         }
269
270         /* Now we'll promote the pending frame to front */
271         ret = kfifo_get(&exynos_crtc->flip_fifo, &next_desc);
272         BUG_ON(!ret);
273
274         atomic_set(&exynos_crtc->flip_pending, 0);
275
276         exynos_drm_crtc_wait_and_release_kds(&next_desc);
277
278         to_exynos_fb(next_desc.fb)->rendered = true;
279         exynos_drm_crtc_update(crtc, next_desc.fb);
280         exynos_drm_crtc_apply(crtc);
281         to_exynos_fb(next_desc.fb)->prepared = true;
282
283         if (exynos_crtc->event) {
284                 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
285                 exynos_crtc->event = NULL;
286         }
287
288         *cur_desc = next_desc;
289 }
290 #endif
291
292 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
293 {
294         DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
295
296 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
297         if (mode != DRM_MODE_DPMS_ON)
298                 exynos_drm_crtc_release_flips(crtc);
299 #endif
300 }
301
302 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
303 {
304         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
305         struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
306         int win = overlay->zpos;
307
308         exynos_drm_fn_encoder(crtc, &win,
309                 exynos_drm_encoder_crtc_disable);
310 }
311
312 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
313 {
314         DRM_DEBUG_KMS("%s\n", __FILE__);
315
316         /* drm framework doesn't check NULL. */
317 }
318
319 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
320                                      struct drm_framebuffer *fb,
321                                      struct drm_pending_vblank_event *event);
322
323 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
324 {
325         int ret, mode = DRM_MODE_DPMS_ON;
326
327         DRM_DEBUG_KMS("%s\n", __FILE__);
328
329         /*
330          * when set_crtc is requested from user or at booting time,
331          * crtc->commit would be called without dpms call so if dpms is
332          * no power on then crtc->dpms should be called
333          * with DRM_MODE_DPMS_ON for the hardware power to be on.
334          */
335         exynos_drm_crtc_dpms(crtc, mode);
336
337         exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
338
339         ret = exynos_drm_crtc_page_flip(crtc, crtc->fb, NULL);
340         if (ret)
341                 DRM_ERROR("page_flip failed\n");
342 }
343
344 static bool
345 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
346                             struct drm_display_mode *mode,
347                             struct drm_display_mode *adjusted_mode)
348 {
349         DRM_DEBUG_KMS("%s\n", __FILE__);
350
351         /* drm framework doesn't check NULL */
352         return true;
353 }
354
355 static int
356 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
357                           struct drm_display_mode *adjusted_mode, int x, int y,
358                           struct drm_framebuffer *old_fb)
359 {
360         struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
361         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
362         struct drm_framebuffer *fb = crtc->fb;
363         int ret;
364
365         DRM_DEBUG_KMS("%s\n", __FILE__);
366
367         if (!fb)
368                 return -EINVAL;
369
370         /*
371          * copy the mode data adjusted by mode_fixup() into crtc->mode
372          * so that hardware can be seet to proper mode.
373          */
374         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
375
376         /* We should never timeout here. */
377         ret = wait_event_timeout(dev_priv->wait_vsync_queue,
378                                  kfifo_is_empty(&exynos_crtc->flip_fifo),
379                                  DRM_HZ/20);
380         if (!ret)
381                 DRM_ERROR("Timed out waiting for flips to complete\n");
382
383         exynos_drm_crtc_update(crtc, fb);
384
385         return 0;
386 }
387
388 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
389                                           struct drm_framebuffer *old_fb)
390 {
391         struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
392         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
393         struct drm_framebuffer *fb = crtc->fb;
394         int ret;
395
396         DRM_DEBUG_KMS("%s\n", __FILE__);
397
398         if (!fb)
399                 return -EINVAL;
400
401         /* We should never timeout here. */
402         ret = wait_event_timeout(dev_priv->wait_vsync_queue,
403                                  kfifo_is_empty(&exynos_crtc->flip_fifo),
404                                  DRM_HZ/20);
405         if (!ret)
406                 DRM_ERROR("Timed out waiting for flips to complete\n");
407
408         ret = exynos_drm_crtc_page_flip(crtc, fb, NULL);
409         if (ret)
410                 DRM_ERROR("page_flip failed\n");
411
412         return ret;
413 }
414
415 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
416 {
417         DRM_DEBUG_KMS("%s\n", __FILE__);
418         /* drm framework doesn't check NULL */
419 }
420
421 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
422         .dpms           = exynos_drm_crtc_dpms,
423         .disable        = exynos_drm_crtc_disable,
424         .prepare        = exynos_drm_crtc_prepare,
425         .commit         = exynos_drm_crtc_commit,
426         .mode_fixup     = exynos_drm_crtc_mode_fixup,
427         .mode_set       = exynos_drm_crtc_mode_set,
428         .mode_set_base  = exynos_drm_crtc_mode_set_base,
429         .load_lut       = exynos_drm_crtc_load_lut,
430 };
431
432 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
433 void exynos_drm_kds_callback(void *callback_parameter, void *callback_extra_parameter)
434 {
435         struct drm_framebuffer  *fb = callback_parameter;
436         struct drm_crtc *crtc =  callback_extra_parameter;
437         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
438
439         /*
440          * Under rare circumstances (modeset followed by flip) it is possible
441          * to have two fbs ready in time for the same vblank. We don't want to
442          * drop the latter fb because it is most recent. We can't drop the
443          * former either because we've already applied it and the mixer does
444          * not allow applying more than one frame on a vblank. So instead of
445          * dropping a frame, we store the latter frame in the extra slot and
446          * apply it on the next vblank.
447          */
448
449         to_exynos_fb(fb)->rendered = true;
450
451         if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 0, 1)) {
452                 exynos_drm_crtc_update(crtc, fb);
453                 exynos_drm_crtc_apply(crtc);
454                 to_exynos_fb(fb)->prepared = true;
455         }
456 }
457 #endif
458
459 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
460                                      struct drm_framebuffer *fb,
461                                      struct drm_pending_vblank_event *event)
462 {
463         struct drm_device *dev = crtc->dev;
464         struct exynos_drm_private *dev_priv = dev->dev_private;
465         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
466         struct exynos_drm_flip_desc flip_desc;
467         bool send_event = false;
468         int ret;
469 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
470         struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
471         struct exynos_drm_gem_obj *gem_ob = (struct exynos_drm_gem_obj *)exynos_fb->exynos_gem_obj[0];
472 #endif
473         DRM_DEBUG_KMS("%s\n", __FILE__);
474
475         /*
476          * the pipe from user always is 0 so we can set pipe number
477          * of current owner to event.
478          */
479         if (event)
480                 event->pipe = exynos_crtc->pipe;
481
482         ret = drm_vblank_get(dev, exynos_crtc->pipe);
483         if (ret) {
484                 DRM_ERROR("Unable to get vblank\n");
485                 return -EINVAL;
486         }
487
488         if (kfifo_is_full(&exynos_crtc->flip_fifo)) {
489                 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
490                 ret = -EBUSY;
491                 goto fail_queue_full;
492         }
493
494         /* Send flip event if no flips pending. */
495         BUG_ON(exynos_crtc->event);
496         if (kfifo_is_empty(&exynos_crtc->flip_fifo))
497                 send_event = true;
498
499         exynos_drm_fb_get(exynos_fb);
500
501 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
502         flip_desc.fb = fb;
503         exynos_fb->rendered = false;
504         exynos_fb->prepared = false;
505         if (gem_ob->base.export_dma_buf) {
506                 struct dma_buf *buf = gem_ob->base.export_dma_buf;
507                 unsigned long shared = 0UL;
508                 struct kds_resource *res_list = get_dma_buf_kds_resource(buf);
509
510                 /*
511                  * If we don't already have a reference to the dma_buf,
512                  * grab one now. We'll release it in exynos_drm_fb_destory().
513                  */
514                 if (!exynos_fb->dma_buf) {
515                         get_dma_buf(buf);
516                         exynos_fb->dma_buf = buf;
517                 }
518                 BUG_ON(exynos_fb->dma_buf !=  buf);
519
520                 /* Waiting for the KDS resource*/
521                 ret = kds_async_waitall(&flip_desc.kds, KDS_FLAG_LOCKED_WAIT,
522                                         &dev_priv->kds_cb, fb, crtc, 1,
523                                         &shared, &res_list);
524                 if (ret) {
525                         DRM_ERROR("kds_async_waitall failed: %d\n", ret);
526                         goto fail_kds;
527                 }
528         } else {
529                 /*
530                  * For normal page-flip (i.e. non-modeset) we should
531                  * never be flipping a non-kds buffer.
532                  */
533                 if (event)
534                         DRM_ERROR("flipping a non-kds buffer\n");
535                 flip_desc.kds = NULL;
536                 exynos_drm_kds_callback(fb, crtc);
537         }
538 #endif
539
540         if (event) {
541                 if (send_event)
542                         exynos_drm_crtc_flip_complete(dev, event);
543                 else
544                         exynos_crtc->event = event;
545         }
546         kfifo_put(&exynos_crtc->flip_fifo, &flip_desc);
547         crtc->fb = fb;
548
549         trace_exynos_flip_request(exynos_crtc->pipe);
550
551         return 0;
552
553 fail_kds:
554         exynos_drm_fb_put(exynos_fb);
555 fail_queue_full:
556         drm_vblank_put(dev, exynos_crtc->pipe);
557         return ret;
558 }
559
560 void exynos_drm_crtc_finish_pageflip(struct drm_device *drm_dev, int crtc_idx)
561 {
562         struct exynos_drm_private *dev_priv = drm_dev->dev_private;
563         struct drm_crtc *crtc = dev_priv->crtc[crtc_idx];
564         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
565         struct exynos_drm_flip_desc *cur_descp = &exynos_crtc->scanout_desc;
566         struct exynos_drm_flip_desc next_desc;
567
568         /* set wait vsync wake up queue. */
569         DRM_WAKEUP(&dev_priv->wait_vsync_queue);
570
571         if (!atomic_read(&exynos_crtc->flip_pending))
572                 return;
573         if (!kfifo_peek(&exynos_crtc->flip_fifo, &next_desc))
574                 return;
575         if (!to_exynos_fb(next_desc.fb)->prepared)
576                 return;
577         if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 1, 0))
578                 return;
579
580         trace_exynos_flip_complete(crtc_idx);
581
582         if (cur_descp->fb)
583                 exynos_drm_fb_put(to_exynos_fb(cur_descp->fb));
584         if (cur_descp->kds)
585                 kds_resource_set_release(&cur_descp->kds);
586         *cur_descp = next_desc;
587         kfifo_skip(&exynos_crtc->flip_fifo);
588
589         if (exynos_crtc->event) {
590                 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
591                 exynos_crtc->event = NULL;
592         }
593
594         /*
595          * exynos_drm_kds_callback can't update the shadow registers if there
596          * is a pending flip ahead. So we do the update here.
597          */
598         if (kfifo_peek(&exynos_crtc->flip_fifo, &next_desc)) {
599                 if (unlikely(to_exynos_fb(next_desc.fb)->rendered) &&
600                     !atomic_cmpxchg(&exynos_crtc->flip_pending, 0, 1)) {
601                         exynos_drm_crtc_update(crtc, next_desc.fb);
602                         exynos_drm_crtc_apply(crtc);
603                         to_exynos_fb(next_desc.fb)->prepared = true;
604                 }
605         }
606
607         drm_vblank_put(drm_dev, crtc_idx);
608 }
609
610 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
611 {
612         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
613         struct exynos_drm_private *private = crtc->dev->dev_private;
614
615         DRM_DEBUG_KMS("%s\n", __FILE__);
616
617         private->crtc[exynos_crtc->pipe] = NULL;
618
619         drm_crtc_cleanup(crtc);
620         kfree(exynos_crtc);
621 }
622
623 static struct drm_crtc_funcs exynos_crtc_funcs = {
624         .set_config     = drm_crtc_helper_set_config,
625         .page_flip      = exynos_drm_crtc_page_flip,
626         .destroy        = exynos_drm_crtc_destroy,
627 };
628
629 struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
630                 struct drm_crtc *crtc)
631 {
632         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
633
634         return &exynos_crtc->overlay;
635 }
636
637 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
638 {
639         struct exynos_drm_crtc *exynos_crtc;
640         struct exynos_drm_private *private = dev->dev_private;
641         struct drm_crtc *crtc;
642
643         DRM_DEBUG_KMS("%s\n", __FILE__);
644
645         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
646         if (!exynos_crtc) {
647                 DRM_ERROR("failed to allocate exynos crtc\n");
648                 return -ENOMEM;
649         }
650
651         INIT_KFIFO(exynos_crtc->flip_fifo);
652         exynos_crtc->pipe = nr;
653         exynos_crtc->overlay.zpos = DEFAULT_ZPOS;
654         crtc = &exynos_crtc->drm_crtc;
655
656         private->crtc[nr] = crtc;
657
658         drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
659         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
660
661         return 0;
662 }
663
664 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
665 {
666         struct exynos_drm_private *private = dev->dev_private;
667
668         DRM_DEBUG_KMS("%s\n", __FILE__);
669
670         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
671                         exynos_drm_enable_vblank);
672
673         return 0;
674 }
675
676 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
677 {
678         struct exynos_drm_private *private = dev->dev_private;
679
680         DRM_DEBUG_KMS("%s\n", __FILE__);
681
682         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
683                         exynos_drm_disable_vblank);
684 }