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 "exynos_drm_crtc.h"
33 #include "exynos_drm_drv.h"
34 #include "exynos_drm_fb.h"
35 #include "exynos_drm_encoder.h"
36 #include "exynos_drm_gem.h"
37 #include "exynos_trace.h"
39 static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
41 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
42 struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
44 exynos_drm_fn_encoder(crtc, overlay,
45 exynos_drm_encoder_crtc_mode_set);
46 exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
47 exynos_drm_encoder_crtc_commit);
50 void exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
51 struct drm_framebuffer *fb,
52 struct drm_display_mode *mode,
53 struct exynos_drm_crtc_pos *pos)
55 struct exynos_drm_gem_buf *buffer;
56 unsigned int actual_w;
57 unsigned int actual_h;
58 int nr = exynos_drm_format_num_buffers(fb->pixel_format);
61 for (i = 0; i < nr; i++) {
62 buffer = exynos_drm_fb_buffer(fb, i);
64 overlay->dma_addr[i] = buffer->dma_addr;
66 DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
67 i, (unsigned long)overlay->dma_addr[i]);
70 actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
71 actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
73 /* set drm framebuffer data. */
74 overlay->fb_x = pos->fb_x;
75 overlay->fb_y = pos->fb_y;
76 overlay->fb_width = min(pos->fb_w, actual_w);
77 overlay->fb_height = min(pos->fb_h, actual_h);
78 overlay->fb_pitch = fb->pitches[0];
79 overlay->bpp = fb->bits_per_pixel;
80 overlay->pixel_format = fb->pixel_format;
82 /* set overlay range to be displayed. */
83 overlay->crtc_x = pos->crtc_x;
84 overlay->crtc_y = pos->crtc_y;
85 overlay->crtc_width = actual_w;
86 overlay->crtc_height = actual_h;
87 overlay->crtc_htotal = mode->crtc_htotal;
88 overlay->crtc_hsync_len = mode->hsync_end - mode->hsync_start;
89 overlay->crtc_vtotal = mode->crtc_vtotal;
90 overlay->crtc_vsync_len = mode->vsync_end - mode->vsync_start;
92 /* set drm mode data. */
93 overlay->mode_width = mode->hdisplay;
94 overlay->mode_height = mode->vdisplay;
95 overlay->refresh = mode->vrefresh;
96 overlay->scan_flag = mode->flags;
98 DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
99 overlay->crtc_x, overlay->crtc_y,
100 overlay->crtc_width, overlay->crtc_height);
103 static void exynos_drm_crtc_update(struct drm_crtc *crtc,
104 struct drm_framebuffer *fb)
106 struct exynos_drm_crtc *exynos_crtc;
107 struct exynos_drm_overlay *overlay;
108 struct exynos_drm_crtc_pos pos;
109 struct drm_display_mode *mode = &crtc->mode;
111 exynos_crtc = to_exynos_crtc(crtc);
112 overlay = &exynos_crtc->overlay;
114 memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
116 /* it means the offset of framebuffer to be displayed. */
119 pos.fb_w = fb->width;
120 pos.fb_h = fb->height;
122 /* OSD position to be displayed. */
125 pos.crtc_w = fb->width - crtc->x;
126 pos.crtc_h = fb->height - crtc->y;
128 exynos_drm_overlay_update(overlay, fb, mode, &pos);
131 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
133 struct drm_device *dev = crtc->dev;
134 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
136 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
138 if (exynos_crtc->dpms == mode) {
139 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
143 mutex_lock(&dev->struct_mutex);
146 case DRM_MODE_DPMS_ON:
147 exynos_drm_fn_encoder(crtc, &mode,
148 exynos_drm_encoder_crtc_dpms);
149 exynos_crtc->dpms = mode;
151 case DRM_MODE_DPMS_STANDBY:
152 case DRM_MODE_DPMS_SUSPEND:
153 case DRM_MODE_DPMS_OFF:
154 exynos_drm_fn_encoder(crtc, &mode,
155 exynos_drm_encoder_crtc_dpms);
156 exynos_crtc->dpms = mode;
159 DRM_ERROR("unspecified mode %d\n", mode);
163 mutex_unlock(&dev->struct_mutex);
166 static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
168 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
169 struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
170 int win = overlay->zpos;
172 exynos_drm_fn_encoder(crtc, &win,
173 exynos_drm_encoder_crtc_disable);
176 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
178 DRM_DEBUG_KMS("%s\n", __FILE__);
180 /* drm framework doesn't check NULL. */
183 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
184 struct drm_framebuffer *fb,
185 struct drm_pending_vblank_event *event);
187 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
189 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
192 DRM_DEBUG_KMS("%s\n", __FILE__);
195 * when set_crtc is requested from user or at booting time,
196 * crtc->commit would be called without dpms call so if dpms is
197 * no power on then crtc->dpms should be called
198 * with DRM_MODE_DPMS_ON for the hardware power to be on.
200 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) {
201 int mode = DRM_MODE_DPMS_ON;
204 * TODO(seanpaul): This has the nasty habit of calling the
205 * underlying dpms/power callbacks twice on boot. This code
206 * needs to be cleaned up so this doesn't happen.
210 * enable hardware(power on) to all encoders hdmi connected
213 exynos_drm_crtc_dpms(crtc, mode);
215 * enable dma to all encoders connected to current crtc and
218 exynos_drm_fn_encoder(crtc, &mode,
219 exynos_drm_encoder_dpms_from_crtc);
222 ret = exynos_drm_crtc_page_flip(crtc, crtc->fb, NULL);
224 DRM_ERROR("page_flip failed\n");
228 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
229 struct drm_display_mode *mode,
230 struct drm_display_mode *adjusted_mode)
232 DRM_DEBUG_KMS("%s\n", __FILE__);
234 /* drm framework doesn't check NULL */
239 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
240 struct drm_display_mode *adjusted_mode, int x, int y,
241 struct drm_framebuffer *old_fb)
243 struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
244 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
245 struct drm_framebuffer *fb = crtc->fb;
248 DRM_DEBUG_KMS("%s\n", __FILE__);
254 * copy the mode data adjusted by mode_fixup() into crtc->mode
255 * so that hardware can be seet to proper mode.
257 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
259 /* We should never timeout here. */
260 ret = wait_event_timeout(dev_priv->wait_vsync_queue,
261 !exynos_crtc->flip_in_flight,
264 DRM_ERROR("Timed out waiting for flips to complete\n");
266 exynos_drm_crtc_update(crtc, fb);
271 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
272 struct drm_framebuffer *old_fb)
274 struct exynos_drm_private *dev_priv = crtc->dev->dev_private;
275 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
276 struct drm_framebuffer *fb = crtc->fb;
279 DRM_DEBUG_KMS("%s\n", __FILE__);
284 /* We should never timeout here. */
285 ret = wait_event_timeout(dev_priv->wait_vsync_queue,
286 !exynos_crtc->flip_in_flight,
289 DRM_ERROR("Timed out waiting for flips to complete\n");
292 ret = exynos_drm_crtc_page_flip(crtc, fb, NULL);
294 DRM_ERROR("page_flip failed\n");
299 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
301 DRM_DEBUG_KMS("%s\n", __FILE__);
302 /* drm framework doesn't check NULL */
305 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
306 .dpms = exynos_drm_crtc_dpms,
307 .disable = exynos_drm_crtc_disable,
308 .prepare = exynos_drm_crtc_prepare,
309 .commit = exynos_drm_crtc_commit,
310 .mode_fixup = exynos_drm_crtc_mode_fixup,
311 .mode_set = exynos_drm_crtc_mode_set,
312 .mode_set_base = exynos_drm_crtc_mode_set_base,
313 .load_lut = exynos_drm_crtc_load_lut,
316 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
317 void exynos_drm_kds_callback(void *callback_parameter, void *callback_extra_parameter)
319 struct drm_framebuffer *fb = callback_parameter;
320 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
321 struct drm_crtc *crtc = exynos_fb->crtc;
322 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
323 struct drm_device *dev = crtc->dev;
324 struct kds_resource_set **pkds = callback_extra_parameter;
325 struct kds_resource_set *prev_kds;
326 struct drm_framebuffer *prev_fb;
329 exynos_drm_crtc_update(crtc, fb);
330 exynos_drm_crtc_apply(crtc);
332 spin_lock_irqsave(&dev->event_lock, flags);
333 prev_kds = exynos_crtc->pending_kds;
334 prev_fb = exynos_crtc->pending_fb;
335 exynos_crtc->pending_kds = *pkds;
336 exynos_crtc->pending_fb = fb;
339 exynos_crtc->flip_in_flight--;
340 spin_unlock_irqrestore(&dev->event_lock, flags);
343 DRM_ERROR("previous work detected\n");
344 exynos_drm_fb_put(to_exynos_fb(prev_fb));
346 kds_resource_set_release(&prev_kds);
348 BUG_ON(atomic_read(&exynos_crtc->flip_pending));
350 atomic_set(&exynos_crtc->flip_pending, 1);
355 static void exynos_drm_crtc_flip_complete(struct drm_pending_vblank_event *e)
359 do_gettimeofday(&now);
360 e->event.sequence = 0;
361 e->event.tv_sec = now.tv_sec;
362 e->event.tv_usec = now.tv_usec;
363 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
364 wake_up_interruptible(&e->base.file_priv->event_wait);
365 trace_exynos_fake_flip_complete(e->pipe);
368 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
369 struct drm_framebuffer *fb,
370 struct drm_pending_vblank_event *event)
372 struct drm_device *dev = crtc->dev;
373 struct exynos_drm_private *dev_priv = dev->dev_private;
374 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
377 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
378 struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
379 struct exynos_drm_gem_obj *gem_ob = (struct exynos_drm_gem_obj *)exynos_fb->exynos_gem_obj[0];
380 struct kds_resource_set **pkds;
381 struct drm_pending_vblank_event *event_to_send;
383 DRM_DEBUG_KMS("%s\n", __FILE__);
386 * the pipe from user always is 0 so we can set pipe number
387 * of current owner to event.
390 event->pipe = exynos_crtc->pipe;
392 ret = drm_vblank_get(dev, exynos_crtc->pipe);
394 DRM_ERROR("Unable to get vblank\n");
398 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
399 spin_lock_irqsave(&dev->event_lock, flags);
400 if (exynos_crtc->flip_in_flight > 1) {
401 spin_unlock_irqrestore(&dev->event_lock, flags);
402 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
404 goto fail_max_in_flight;
406 /* Signal previous flip event. Or if none in flight signal current. */
407 if (exynos_crtc->flip_in_flight) {
408 event_to_send = exynos_crtc->event;
409 exynos_crtc->event = event;
411 event_to_send = event;
412 exynos_crtc->event = NULL;
414 pkds = &exynos_crtc->future_kds;
416 pkds = &exynos_crtc->future_kds_extra;
417 *pkds = ERR_PTR(-EINVAL); /* Make it non-NULL */
418 exynos_crtc->flip_in_flight++;
419 spin_unlock_irqrestore(&dev->event_lock, flags);
422 mutex_lock(&dev->struct_mutex);
426 mutex_unlock(&dev->struct_mutex);
428 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
429 exynos_fb->crtc = crtc;
430 if (gem_ob->base.export_dma_buf) {
431 struct dma_buf *buf = gem_ob->base.export_dma_buf;
432 unsigned long shared = 0UL;
433 struct kds_resource *res_list = get_dma_buf_kds_resource(buf);
436 * If we don't already have a reference to the dma_buf,
437 * grab one now. We'll release it in exynos_drm_fb_destory().
439 if (!exynos_fb->dma_buf) {
441 exynos_fb->dma_buf = buf;
443 BUG_ON(exynos_fb->dma_buf != buf);
445 /* Waiting for the KDS resource*/
446 ret = kds_async_waitall(pkds, KDS_FLAG_LOCKED_WAIT,
447 &dev_priv->kds_cb, fb, pkds, 1,
450 DRM_ERROR("kds_async_waitall failed: %d\n", ret);
455 * For normal page-flip (i.e. non-modeset) we should
456 * never be flipping a non-kds buffer.
459 DRM_ERROR("flipping a non-kds buffer\n");
461 exynos_drm_kds_callback(fb, pkds);
465 spin_lock_irqsave(&dev->event_lock, flags);
466 exynos_drm_crtc_flip_complete(event_to_send);
467 spin_unlock_irqrestore(&dev->event_lock, flags);
471 exynos_drm_fb_get(exynos_fb);
473 trace_exynos_flip_request(exynos_crtc->pipe);
479 spin_lock_irqsave(&dev->event_lock, flags);
480 exynos_crtc->flip_in_flight--;
481 spin_unlock_irqrestore(&dev->event_lock, flags);
483 drm_vblank_put(dev, exynos_crtc->pipe);
487 void exynos_drm_crtc_finish_pageflip(struct drm_device *drm_dev, int crtc_idx)
489 struct exynos_drm_private *dev_priv = drm_dev->dev_private;
490 struct drm_crtc *crtc = dev_priv->crtc[crtc_idx];
491 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
492 struct kds_resource_set *kds;
493 struct drm_framebuffer *fb;
496 /* set wait vsync wake up queue. */
497 DRM_WAKEUP(&dev_priv->wait_vsync_queue);
499 if (!atomic_cmpxchg(&exynos_crtc->flip_pending, 1, 0))
502 trace_exynos_flip_complete(crtc_idx);
504 spin_lock_irqsave(&drm_dev->event_lock, flags);
505 if (exynos_crtc->event) {
506 exynos_drm_crtc_flip_complete(exynos_crtc->event);
507 exynos_crtc->event = NULL;
509 kds = exynos_crtc->current_kds;
510 exynos_crtc->current_kds = exynos_crtc->pending_kds;
511 exynos_crtc->pending_kds = NULL;
512 fb = exynos_crtc->current_fb;
513 exynos_crtc->current_fb = exynos_crtc->pending_fb;
514 exynos_crtc->pending_fb = NULL;
515 exynos_crtc->flip_in_flight--;
516 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
519 exynos_drm_fb_put(to_exynos_fb(fb));
521 kds_resource_set_release(&kds);
523 drm_vblank_put(drm_dev, crtc_idx);
526 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
528 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
529 struct exynos_drm_private *private = crtc->dev->dev_private;
531 DRM_DEBUG_KMS("%s\n", __FILE__);
533 private->crtc[exynos_crtc->pipe] = NULL;
535 drm_crtc_cleanup(crtc);
539 static struct drm_crtc_funcs exynos_crtc_funcs = {
540 .set_config = drm_crtc_helper_set_config,
541 .page_flip = exynos_drm_crtc_page_flip,
542 .destroy = exynos_drm_crtc_destroy,
545 struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
546 struct drm_crtc *crtc)
548 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
550 return &exynos_crtc->overlay;
553 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
555 struct exynos_drm_crtc *exynos_crtc;
556 struct exynos_drm_private *private = dev->dev_private;
557 struct drm_crtc *crtc;
559 DRM_DEBUG_KMS("%s\n", __FILE__);
561 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
563 DRM_ERROR("failed to allocate exynos crtc\n");
567 exynos_crtc->pipe = nr;
568 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
569 exynos_crtc->overlay.zpos = DEFAULT_ZPOS;
570 crtc = &exynos_crtc->drm_crtc;
572 private->crtc[nr] = crtc;
574 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
575 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
580 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
582 struct exynos_drm_private *private = dev->dev_private;
583 struct exynos_drm_crtc *exynos_crtc =
584 to_exynos_crtc(private->crtc[crtc]);
586 DRM_DEBUG_KMS("%s\n", __FILE__);
588 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
591 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
592 exynos_drm_enable_vblank);
597 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
599 struct exynos_drm_private *private = dev->dev_private;
600 struct exynos_drm_crtc *exynos_crtc =
601 to_exynos_crtc(private->crtc[crtc]);
603 DRM_DEBUG_KMS("%s\n", __FILE__);
605 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
608 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
609 exynos_drm_disable_vblank);