3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
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:
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
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.
30 #include "drm_crtc_helper.h"
32 #include <linux/kfifo.h>
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"
41 #define KDS_WAITALL_MAX_TRIES 15
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;
51 * Exynos specific crtc structure.
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
65 * @flip_pending: there is a flip pending that we need to process next vblank
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;
74 atomic_t flip_pending;
77 #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
80 static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
82 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
83 struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
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);
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)
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);
103 for (i = 0; i < nr; i++) {
104 buffer = exynos_drm_fb_buffer(exynos_fb, i);
106 overlay->dma_addr[i] = buffer->dma_addr;
108 DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
109 i, (unsigned long)overlay->dma_addr[i]);
112 actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
113 actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
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;
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;
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;
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);
145 static void exynos_drm_crtc_update(struct drm_crtc *crtc,
146 struct drm_framebuffer *fb)
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;
153 exynos_crtc = to_exynos_crtc(crtc);
154 overlay = &exynos_crtc->overlay;
156 memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
158 /* it means the offset of framebuffer to be displayed. */
161 pos.fb_w = fb->width;
162 pos.fb_h = fb->height;
164 /* OSD position to be displayed. */
167 pos.crtc_w = fb->width - crtc->x;
168 pos.crtc_h = fb->height - crtc->y;
170 exynos_drm_overlay_update(overlay, fb, mode, &pos);
173 static void exynos_drm_crtc_flip_complete(struct drm_device *dev,
174 struct drm_pending_vblank_event *e)
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);
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)
194 struct exynos_drm_fb *exynos_fb;
195 struct exynos_drm_gem_obj *gem_ob;
197 unsigned long shared = 0UL;
198 struct kds_resource *resource_list;
199 struct kds_resource_set *resource_set;
208 kds_resource_set_release(&desc->kds);
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;
216 if (unlikely(!exynos_fb->dma_buf)) {
218 exynos_fb->dma_buf = buf;
220 BUG_ON(exynos_fb->dma_buf != buf);
222 /* Synchronously wait for the frame to render */
223 resource_list = get_dma_buf_kds_resource(buf);
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)
231 if (IS_ERR(resource_set)) {
232 DRM_ERROR("kds_waitall failed with ret=%ld\n",
233 PTR_ERR(resource_set));
235 } else if (!resource_set) {
236 DRM_ERROR("kds_waitall timed out\n");
240 /* Clean up the remaining resource so we're not holding onto anything */
241 kds_resource_set_release(&resource_set);
244 static void exynos_drm_crtc_release_flips(struct drm_crtc *crtc)
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;
253 kds_resource_set_release(&cur_desc->kds);
255 /* If there aren't any pending frames, be merry and exit */
256 if (!kfifo_len(&exynos_crtc->flip_fifo))
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);
265 exynos_drm_fb_put(to_exynos_fb(next_desc.fb));
267 kds_resource_set_release(&next_desc.kds);
270 /* Now we'll promote the pending frame to front */
271 ret = kfifo_get(&exynos_crtc->flip_fifo, &next_desc);
274 atomic_set(&exynos_crtc->flip_pending, 0);
276 exynos_drm_crtc_wait_and_release_kds(&next_desc);
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;
283 if (exynos_crtc->event) {
284 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
285 exynos_crtc->event = NULL;
288 *cur_desc = next_desc;
292 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
294 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
296 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
297 if (mode != DRM_MODE_DPMS_ON)
298 exynos_drm_crtc_release_flips(crtc);
302 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
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;
308 exynos_drm_fn_encoder(crtc, &win,
309 exynos_drm_encoder_crtc_disable);
312 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
314 DRM_DEBUG_KMS("%s\n", __FILE__);
316 /* drm framework doesn't check NULL. */
319 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
320 struct drm_framebuffer *fb,
321 struct drm_pending_vblank_event *event);
323 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
325 int ret, mode = DRM_MODE_DPMS_ON;
327 DRM_DEBUG_KMS("%s\n", __FILE__);
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.
335 exynos_drm_crtc_dpms(crtc, mode);
337 exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
339 ret = exynos_drm_crtc_page_flip(crtc, crtc->fb, NULL);
341 DRM_ERROR("page_flip failed\n");
345 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
346 struct drm_display_mode *mode,
347 struct drm_display_mode *adjusted_mode)
349 DRM_DEBUG_KMS("%s\n", __FILE__);
351 /* drm framework doesn't check NULL */
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)
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;
365 DRM_DEBUG_KMS("%s\n", __FILE__);
371 * copy the mode data adjusted by mode_fixup() into crtc->mode
372 * so that hardware can be seet to proper mode.
374 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
376 /* We should never timeout here. */
377 ret = wait_event_timeout(dev_priv->wait_vsync_queue,
378 kfifo_is_empty(&exynos_crtc->flip_fifo),
381 DRM_ERROR("Timed out waiting for flips to complete\n");
383 exynos_drm_crtc_update(crtc, fb);
388 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
389 struct drm_framebuffer *old_fb)
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;
396 DRM_DEBUG_KMS("%s\n", __FILE__);
401 /* We should never timeout here. */
402 ret = wait_event_timeout(dev_priv->wait_vsync_queue,
403 kfifo_is_empty(&exynos_crtc->flip_fifo),
406 DRM_ERROR("Timed out waiting for flips to complete\n");
408 ret = exynos_drm_crtc_page_flip(crtc, fb, NULL);
410 DRM_ERROR("page_flip failed\n");
415 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
417 DRM_DEBUG_KMS("%s\n", __FILE__);
418 /* drm framework doesn't check NULL */
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,
432 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
433 void exynos_drm_kds_callback(void *callback_parameter, void *callback_extra_parameter)
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);
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.
449 to_exynos_fb(fb)->rendered = true;
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;
459 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
460 struct drm_framebuffer *fb,
461 struct drm_pending_vblank_event *event)
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;
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];
473 DRM_DEBUG_KMS("%s\n", __FILE__);
476 * the pipe from user always is 0 so we can set pipe number
477 * of current owner to event.
480 event->pipe = exynos_crtc->pipe;
482 ret = drm_vblank_get(dev, exynos_crtc->pipe);
484 DRM_ERROR("Unable to get vblank\n");
488 if (kfifo_is_full(&exynos_crtc->flip_fifo)) {
489 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
491 goto fail_queue_full;
494 /* Send flip event if no flips pending. */
495 BUG_ON(exynos_crtc->event);
496 if (kfifo_is_empty(&exynos_crtc->flip_fifo))
499 exynos_drm_fb_get(exynos_fb);
501 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
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);
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().
514 if (!exynos_fb->dma_buf) {
516 exynos_fb->dma_buf = buf;
518 BUG_ON(exynos_fb->dma_buf != buf);
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,
525 DRM_ERROR("kds_async_waitall failed: %d\n", ret);
530 * For normal page-flip (i.e. non-modeset) we should
531 * never be flipping a non-kds buffer.
534 DRM_ERROR("flipping a non-kds buffer\n");
535 flip_desc.kds = NULL;
536 exynos_drm_kds_callback(fb, crtc);
542 exynos_drm_crtc_flip_complete(dev, event);
544 exynos_crtc->event = event;
546 kfifo_put(&exynos_crtc->flip_fifo, &flip_desc);
549 trace_exynos_flip_request(exynos_crtc->pipe);
554 exynos_drm_fb_put(exynos_fb);
556 drm_vblank_put(dev, exynos_crtc->pipe);
560 void exynos_drm_crtc_finish_pageflip(struct drm_device *drm_dev, int crtc_idx)
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;
568 /* set wait vsync wake up queue. */
569 DRM_WAKEUP(&dev_priv->wait_vsync_queue);
571 if (!atomic_read(&exynos_crtc->flip_pending))
573 if (!kfifo_peek(&exynos_crtc->flip_fifo, &next_desc))
575 if (!to_exynos_fb(next_desc.fb)->prepared)
577 if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 1, 0))
580 trace_exynos_flip_complete(crtc_idx);
583 exynos_drm_fb_put(to_exynos_fb(cur_descp->fb));
585 kds_resource_set_release(&cur_descp->kds);
586 *cur_descp = next_desc;
587 kfifo_skip(&exynos_crtc->flip_fifo);
589 if (exynos_crtc->event) {
590 exynos_drm_crtc_flip_complete(drm_dev, exynos_crtc->event);
591 exynos_crtc->event = NULL;
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.
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;
607 drm_vblank_put(drm_dev, crtc_idx);
610 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
612 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
613 struct exynos_drm_private *private = crtc->dev->dev_private;
615 DRM_DEBUG_KMS("%s\n", __FILE__);
617 private->crtc[exynos_crtc->pipe] = NULL;
619 drm_crtc_cleanup(crtc);
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,
629 struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
630 struct drm_crtc *crtc)
632 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
634 return &exynos_crtc->overlay;
637 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
639 struct exynos_drm_crtc *exynos_crtc;
640 struct exynos_drm_private *private = dev->dev_private;
641 struct drm_crtc *crtc;
643 DRM_DEBUG_KMS("%s\n", __FILE__);
645 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
647 DRM_ERROR("failed to allocate exynos crtc\n");
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;
656 private->crtc[nr] = crtc;
658 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
659 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
664 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
666 struct exynos_drm_private *private = dev->dev_private;
668 DRM_DEBUG_KMS("%s\n", __FILE__);
670 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
671 exynos_drm_enable_vblank);
676 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
678 struct exynos_drm_private *private = dev->dev_private;
680 DRM_DEBUG_KMS("%s\n", __FILE__);
682 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
683 exynos_drm_disable_vblank);