2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Dave Airlie
27 #include <drm/radeon_drm.h>
31 #include <asm/div64.h>
33 #include <linux/pm_runtime.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_edid.h>
37 #include <linux/gcd.h>
39 static void avivo_crtc_load_lut(struct drm_crtc *crtc)
41 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
42 struct drm_device *dev = crtc->dev;
43 struct radeon_device *rdev = dev->dev_private;
46 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
47 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
50 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
51 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
54 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
55 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
57 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
58 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
59 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
61 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
62 for (i = 0; i < 256; i++) {
63 WREG32(AVIVO_DC_LUT_30_COLOR,
64 (radeon_crtc->lut_r[i] << 20) |
65 (radeon_crtc->lut_g[i] << 10) |
66 (radeon_crtc->lut_b[i] << 0));
69 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
72 static void dce4_crtc_load_lut(struct drm_crtc *crtc)
74 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
75 struct drm_device *dev = crtc->dev;
76 struct radeon_device *rdev = dev->dev_private;
79 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
80 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
84 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
88 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
90 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
91 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
93 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
94 for (i = 0; i < 256; i++) {
95 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
96 (radeon_crtc->lut_r[i] << 20) |
97 (radeon_crtc->lut_g[i] << 10) |
98 (radeon_crtc->lut_b[i] << 0));
102 static void dce5_crtc_load_lut(struct drm_crtc *crtc)
104 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
105 struct drm_device *dev = crtc->dev;
106 struct radeon_device *rdev = dev->dev_private;
109 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
111 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
112 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
113 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
114 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
115 NI_GRPH_PRESCALE_BYPASS);
116 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
117 NI_OVL_PRESCALE_BYPASS);
118 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
119 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
120 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
122 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
124 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
125 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
126 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
128 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
129 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
130 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
132 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
133 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
135 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
136 for (i = 0; i < 256; i++) {
137 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
138 (radeon_crtc->lut_r[i] << 20) |
139 (radeon_crtc->lut_g[i] << 10) |
140 (radeon_crtc->lut_b[i] << 0));
143 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
144 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
145 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
147 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
148 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
149 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
150 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
151 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
152 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
153 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
154 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
155 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
156 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
157 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
158 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
159 if (ASIC_IS_DCE8(rdev)) {
160 /* XXX this only needs to be programmed once per crtc at startup,
161 * not sure where the best place for it is
163 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
164 CIK_CURSOR_ALPHA_BLND_ENA);
168 static void legacy_crtc_load_lut(struct drm_crtc *crtc)
170 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
171 struct drm_device *dev = crtc->dev;
172 struct radeon_device *rdev = dev->dev_private;
176 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
177 if (radeon_crtc->crtc_id == 0)
178 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
180 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
181 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
183 WREG8(RADEON_PALETTE_INDEX, 0);
184 for (i = 0; i < 256; i++) {
185 WREG32(RADEON_PALETTE_30_DATA,
186 (radeon_crtc->lut_r[i] << 20) |
187 (radeon_crtc->lut_g[i] << 10) |
188 (radeon_crtc->lut_b[i] << 0));
192 void radeon_crtc_load_lut(struct drm_crtc *crtc)
194 struct drm_device *dev = crtc->dev;
195 struct radeon_device *rdev = dev->dev_private;
200 if (ASIC_IS_DCE5(rdev))
201 dce5_crtc_load_lut(crtc);
202 else if (ASIC_IS_DCE4(rdev))
203 dce4_crtc_load_lut(crtc);
204 else if (ASIC_IS_AVIVO(rdev))
205 avivo_crtc_load_lut(crtc);
207 legacy_crtc_load_lut(crtc);
210 /** Sets the color ramps on behalf of fbcon */
211 void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
216 radeon_crtc->lut_r[regno] = red >> 6;
217 radeon_crtc->lut_g[regno] = green >> 6;
218 radeon_crtc->lut_b[regno] = blue >> 6;
221 /** Gets the color ramps on behalf of fbcon */
222 void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
223 u16 *blue, int regno)
225 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
227 *red = radeon_crtc->lut_r[regno] << 6;
228 *green = radeon_crtc->lut_g[regno] << 6;
229 *blue = radeon_crtc->lut_b[regno] << 6;
232 static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
233 u16 *blue, uint32_t start, uint32_t size)
235 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
236 int end = (start + size > 256) ? 256 : start + size, i;
238 /* userspace palettes are always correct as is */
239 for (i = start; i < end; i++) {
240 radeon_crtc->lut_r[i] = red[i] >> 6;
241 radeon_crtc->lut_g[i] = green[i] >> 6;
242 radeon_crtc->lut_b[i] = blue[i] >> 6;
244 radeon_crtc_load_lut(crtc);
247 static void radeon_crtc_destroy(struct drm_crtc *crtc)
249 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251 drm_crtc_cleanup(crtc);
252 destroy_workqueue(radeon_crtc->flip_queue);
257 * radeon_unpin_work_func - unpin old buffer object
259 * @__work - kernel work item
261 * Unpin the old frame buffer object outside of the interrupt handler
263 static void radeon_unpin_work_func(struct work_struct *__work)
265 struct radeon_flip_work *work =
266 container_of(__work, struct radeon_flip_work, unpin_work);
269 /* unpin of the old buffer */
270 r = radeon_bo_reserve(work->old_rbo, false);
271 if (likely(r == 0)) {
272 r = radeon_bo_unpin(work->old_rbo);
273 if (unlikely(r != 0)) {
274 DRM_ERROR("failed to unpin buffer after flip\n");
276 radeon_bo_unreserve(work->old_rbo);
278 DRM_ERROR("failed to reserve buffer after flip\n");
280 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
284 void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
286 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
287 struct radeon_flip_work *work;
292 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
293 work = radeon_crtc->flip_work;
295 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
299 update_pending = radeon_page_flip_pending(rdev, crtc_id);
301 /* Has the pageflip already completed in crtc, or is it certain
302 * to complete in this vblank?
304 if (update_pending &&
305 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
306 &vpos, &hpos, NULL, NULL)) &&
307 ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
308 (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
309 /* crtc didn't flip in this target vblank interval,
310 * but flip is pending in crtc. Based on the current
311 * scanout position we know that the current frame is
312 * (nearly) complete and the flip will (likely)
313 * complete before the start of the next frame.
317 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
319 radeon_crtc_handle_flip(rdev, crtc_id);
323 * radeon_crtc_handle_flip - page flip completed
325 * @rdev: radeon device pointer
326 * @crtc_id: crtc number this event is for
328 * Called when we are sure that a page flip for this crtc is completed.
330 void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
332 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
333 struct radeon_flip_work *work;
336 /* this can happen at init */
337 if (radeon_crtc == NULL)
340 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
341 work = radeon_crtc->flip_work;
343 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
347 /* Pageflip completed. Clean up. */
348 radeon_crtc->flip_work = NULL;
350 /* wakeup userspace */
352 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
354 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
356 radeon_fence_unref(&work->fence);
357 radeon_irq_kms_pflip_irq_get(rdev, work->crtc_id);
358 queue_work(radeon_crtc->flip_queue, &work->unpin_work);
362 * radeon_flip_work_func - page flip framebuffer
364 * @work - kernel work item
366 * Wait for the buffer object to become idle and do the actual page flip
368 static void radeon_flip_work_func(struct work_struct *__work)
370 struct radeon_flip_work *work =
371 container_of(__work, struct radeon_flip_work, flip_work);
372 struct radeon_device *rdev = work->rdev;
373 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
375 struct drm_crtc *crtc = &radeon_crtc->base;
376 struct drm_framebuffer *fb = work->fb;
378 uint32_t tiling_flags, pitch_pixels;
384 down_read(&rdev->exclusive_lock);
385 while (work->fence) {
386 r = radeon_fence_wait(work->fence, false);
388 up_read(&rdev->exclusive_lock);
389 r = radeon_gpu_reset(rdev);
390 down_read(&rdev->exclusive_lock);
394 DRM_ERROR("failed to wait on page flip fence (%d)!\n",
398 radeon_fence_unref(&work->fence);
401 /* pin the new buffer */
402 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
403 work->old_rbo, work->new_rbo);
405 r = radeon_bo_reserve(work->new_rbo, false);
406 if (unlikely(r != 0)) {
407 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
410 /* Only 27 bit offset for legacy CRTC */
411 r = radeon_bo_pin_restricted(work->new_rbo, RADEON_GEM_DOMAIN_VRAM,
412 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
413 if (unlikely(r != 0)) {
414 radeon_bo_unreserve(work->new_rbo);
416 DRM_ERROR("failed to pin new rbo buffer before flip\n");
419 radeon_bo_get_tiling_flags(work->new_rbo, &tiling_flags, NULL);
420 radeon_bo_unreserve(work->new_rbo);
422 if (!ASIC_IS_AVIVO(rdev)) {
423 /* crtc offset is from display base addr not FB location */
424 base -= radeon_crtc->legacy_display_base_addr;
425 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
427 if (tiling_flags & RADEON_TILING_MACRO) {
428 if (ASIC_IS_R300(rdev)) {
431 int byteshift = fb->bits_per_pixel >> 4;
432 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
433 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
436 int offset = crtc->y * pitch_pixels + crtc->x;
437 switch (fb->bits_per_pixel) {
458 /* We borrow the event spin lock for protecting flip_work */
459 spin_lock_irqsave(&crtc->dev->event_lock, flags);
462 crtc->primary->fb = fb;
464 /* set the proper interrupt */
465 radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
467 /* do the flip (mmio) */
468 radeon_page_flip(rdev, radeon_crtc->crtc_id, base);
470 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
471 up_read(&rdev->exclusive_lock);
476 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
477 radeon_fence_unref(&work->fence);
479 up_read(&rdev->exclusive_lock);
482 static int radeon_crtc_page_flip(struct drm_crtc *crtc,
483 struct drm_framebuffer *fb,
484 struct drm_pending_vblank_event *event,
485 uint32_t page_flip_flags)
487 struct drm_device *dev = crtc->dev;
488 struct radeon_device *rdev = dev->dev_private;
489 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
490 struct radeon_framebuffer *old_radeon_fb;
491 struct radeon_framebuffer *new_radeon_fb;
492 struct drm_gem_object *obj;
493 struct radeon_flip_work *work;
496 work = kzalloc(sizeof *work, GFP_KERNEL);
500 INIT_WORK(&work->flip_work, radeon_flip_work_func);
501 INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
504 work->crtc_id = radeon_crtc->crtc_id;
508 /* schedule unpin of the old buffer */
509 old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
510 obj = old_radeon_fb->obj;
512 /* take a reference to the old object */
513 drm_gem_object_reference(obj);
514 work->old_rbo = gem_to_radeon_bo(obj);
516 new_radeon_fb = to_radeon_framebuffer(fb);
517 obj = new_radeon_fb->obj;
518 work->new_rbo = gem_to_radeon_bo(obj);
520 spin_lock(&work->new_rbo->tbo.bdev->fence_lock);
521 if (work->new_rbo->tbo.sync_obj)
522 work->fence = radeon_fence_ref(work->new_rbo->tbo.sync_obj);
523 spin_unlock(&work->new_rbo->tbo.bdev->fence_lock);
525 /* We borrow the event spin lock for protecting flip_work */
526 spin_lock_irqsave(&crtc->dev->event_lock, flags);
528 if (radeon_crtc->flip_work) {
529 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
530 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
531 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
532 radeon_fence_unref(&work->fence);
536 radeon_crtc->flip_work = work;
538 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
540 queue_work(radeon_crtc->flip_queue, &work->flip_work);
546 radeon_crtc_set_config(struct drm_mode_set *set)
548 struct drm_device *dev;
549 struct radeon_device *rdev;
550 struct drm_crtc *crtc;
554 if (!set || !set->crtc)
557 dev = set->crtc->dev;
559 ret = pm_runtime_get_sync(dev->dev);
563 ret = drm_crtc_helper_set_config(set);
565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
569 pm_runtime_mark_last_busy(dev->dev);
571 rdev = dev->dev_private;
572 /* if we have active crtcs and we don't have a power ref,
573 take the current one */
574 if (active && !rdev->have_disp_power_ref) {
575 rdev->have_disp_power_ref = true;
578 /* if we have no active crtcs, then drop the power ref
580 if (!active && rdev->have_disp_power_ref) {
581 pm_runtime_put_autosuspend(dev->dev);
582 rdev->have_disp_power_ref = false;
585 /* drop the power reference we got coming in here */
586 pm_runtime_put_autosuspend(dev->dev);
589 static const struct drm_crtc_funcs radeon_crtc_funcs = {
590 .cursor_set = radeon_crtc_cursor_set,
591 .cursor_move = radeon_crtc_cursor_move,
592 .gamma_set = radeon_crtc_gamma_set,
593 .set_config = radeon_crtc_set_config,
594 .destroy = radeon_crtc_destroy,
595 .page_flip = radeon_crtc_page_flip,
598 static void radeon_crtc_init(struct drm_device *dev, int index)
600 struct radeon_device *rdev = dev->dev_private;
601 struct radeon_crtc *radeon_crtc;
604 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
605 if (radeon_crtc == NULL)
608 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
610 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
611 radeon_crtc->crtc_id = index;
612 radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
613 rdev->mode_info.crtcs[index] = radeon_crtc;
615 if (rdev->family >= CHIP_BONAIRE) {
616 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
617 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
619 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
620 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
622 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
623 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
626 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
627 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
628 radeon_crtc->mode_set.num_connectors = 0;
631 for (i = 0; i < 256; i++) {
632 radeon_crtc->lut_r[i] = i << 2;
633 radeon_crtc->lut_g[i] = i << 2;
634 radeon_crtc->lut_b[i] = i << 2;
637 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
638 radeon_atombios_init_crtc(dev, radeon_crtc);
640 radeon_legacy_init_crtc(dev, radeon_crtc);
643 static const char *encoder_names[38] = {
663 "INTERNAL_KLDSCP_TMDS1",
664 "INTERNAL_KLDSCP_DVO1",
665 "INTERNAL_KLDSCP_DAC1",
666 "INTERNAL_KLDSCP_DAC2",
675 "INTERNAL_KLDSCP_LVTMA",
684 static const char *hpd_names[6] = {
693 static void radeon_print_display_setup(struct drm_device *dev)
695 struct drm_connector *connector;
696 struct radeon_connector *radeon_connector;
697 struct drm_encoder *encoder;
698 struct radeon_encoder *radeon_encoder;
702 DRM_INFO("Radeon Display Connectors\n");
703 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
704 radeon_connector = to_radeon_connector(connector);
705 DRM_INFO("Connector %d:\n", i);
706 DRM_INFO(" %s\n", drm_get_connector_name(connector));
707 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
708 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
709 if (radeon_connector->ddc_bus) {
710 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
711 radeon_connector->ddc_bus->rec.mask_clk_reg,
712 radeon_connector->ddc_bus->rec.mask_data_reg,
713 radeon_connector->ddc_bus->rec.a_clk_reg,
714 radeon_connector->ddc_bus->rec.a_data_reg,
715 radeon_connector->ddc_bus->rec.en_clk_reg,
716 radeon_connector->ddc_bus->rec.en_data_reg,
717 radeon_connector->ddc_bus->rec.y_clk_reg,
718 radeon_connector->ddc_bus->rec.y_data_reg);
719 if (radeon_connector->router.ddc_valid)
720 DRM_INFO(" DDC Router 0x%x/0x%x\n",
721 radeon_connector->router.ddc_mux_control_pin,
722 radeon_connector->router.ddc_mux_state);
723 if (radeon_connector->router.cd_valid)
724 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
725 radeon_connector->router.cd_mux_control_pin,
726 radeon_connector->router.cd_mux_state);
728 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
729 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
730 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
731 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
732 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
733 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
734 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
736 DRM_INFO(" Encoders:\n");
737 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
738 radeon_encoder = to_radeon_encoder(encoder);
739 devices = radeon_encoder->devices & radeon_connector->devices;
741 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
742 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
743 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
744 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
745 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
746 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
747 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
748 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
749 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
750 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
751 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
752 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
753 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
754 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
755 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
756 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
757 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
758 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
759 if (devices & ATOM_DEVICE_TV1_SUPPORT)
760 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
761 if (devices & ATOM_DEVICE_CV_SUPPORT)
762 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
769 static bool radeon_setup_enc_conn(struct drm_device *dev)
771 struct radeon_device *rdev = dev->dev_private;
775 if (rdev->is_atom_bios) {
776 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
778 ret = radeon_get_atom_connector_info_from_object_table(dev);
780 ret = radeon_get_legacy_connector_info_from_bios(dev);
782 ret = radeon_get_legacy_connector_info_from_table(dev);
785 if (!ASIC_IS_AVIVO(rdev))
786 ret = radeon_get_legacy_connector_info_from_table(dev);
789 radeon_setup_encoder_clones(dev);
790 radeon_print_display_setup(dev);
796 int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
798 struct drm_device *dev = radeon_connector->base.dev;
799 struct radeon_device *rdev = dev->dev_private;
802 /* on hw with routers, select right port */
803 if (radeon_connector->router.ddc_valid)
804 radeon_router_select_ddc_port(radeon_connector);
806 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
807 ENCODER_OBJECT_ID_NONE) {
808 if (radeon_connector->ddc_bus->has_aux)
809 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
810 &radeon_connector->ddc_bus->aux.ddc);
811 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
812 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
813 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
815 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
816 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
817 radeon_connector->ddc_bus->has_aux)
818 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
819 &radeon_connector->ddc_bus->aux.ddc);
820 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
821 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
822 &radeon_connector->ddc_bus->adapter);
824 if (radeon_connector->ddc_bus && !radeon_connector->edid)
825 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
826 &radeon_connector->ddc_bus->adapter);
829 if (!radeon_connector->edid) {
830 if (rdev->is_atom_bios) {
831 /* some laptops provide a hardcoded edid in rom for LCDs */
832 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
833 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
834 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
836 /* some servers provide a hardcoded edid in rom for KVMs */
837 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
839 if (radeon_connector->edid) {
840 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
841 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
842 drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
845 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
852 * avivo_reduce_ratio - fractional number reduction
856 * @nom_min: minimum value for nominator
857 * @den_min: minimum value for denominator
859 * Find the greatest common divisor and apply it on both nominator and
860 * denominator, but make nominator and denominator are at least as large
861 * as their minimum values.
863 static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
864 unsigned nom_min, unsigned den_min)
868 /* reduce the numbers to a simpler ratio */
869 tmp = gcd(*nom, *den);
873 /* make sure nominator is large enough */
874 if (*nom < nom_min) {
875 tmp = (nom_min + *nom - 1) / *nom;
880 /* make sure the denominator is large enough */
881 if (*den < den_min) {
882 tmp = (den_min + *den - 1) / *den;
889 * avivo_get_fb_ref_div - feedback and ref divider calculation
893 * @post_div: post divider
894 * @fb_div_max: feedback divider maximum
895 * @ref_div_max: reference divider maximum
896 * @fb_div: resulting feedback divider
897 * @ref_div: resulting reference divider
899 * Calculate feedback and reference divider for a given post divider. Makes
900 * sure we stay within the limits.
902 static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
903 unsigned fb_div_max, unsigned ref_div_max,
904 unsigned *fb_div, unsigned *ref_div)
906 /* limit reference * post divider to a maximum */
907 ref_div_max = min(210 / post_div, ref_div_max);
909 /* get matching reference and feedback divider */
910 *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
911 *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
913 /* limit fb divider to its maximum */
914 if (*fb_div > fb_div_max) {
915 *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
916 *fb_div = fb_div_max;
921 * radeon_compute_pll_avivo - compute PLL paramaters
923 * @pll: information about the PLL
924 * @dot_clock_p: resulting pixel clock
925 * fb_div_p: resulting feedback divider
926 * frac_fb_div_p: fractional part of the feedback divider
927 * ref_div_p: resulting reference divider
928 * post_div_p: resulting reference divider
930 * Try to calculate the PLL parameters to generate the given frequency:
931 * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
933 void radeon_compute_pll_avivo(struct radeon_pll *pll,
941 unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
944 unsigned fb_div_min, fb_div_max, fb_div;
945 unsigned post_div_min, post_div_max, post_div;
946 unsigned ref_div_min, ref_div_max, ref_div;
947 unsigned post_div_best, diff_best;
950 /* determine allowed feedback divider range */
951 fb_div_min = pll->min_feedback_div;
952 fb_div_max = pll->max_feedback_div;
954 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
959 /* determine allowed ref divider range */
960 if (pll->flags & RADEON_PLL_USE_REF_DIV)
961 ref_div_min = pll->reference_div;
963 ref_div_min = pll->min_ref_div;
965 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
966 pll->flags & RADEON_PLL_USE_REF_DIV)
967 ref_div_max = pll->reference_div;
969 ref_div_max = pll->max_ref_div;
971 /* determine allowed post divider range */
972 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
973 post_div_min = pll->post_div;
974 post_div_max = pll->post_div;
976 unsigned vco_min, vco_max;
978 if (pll->flags & RADEON_PLL_IS_LCD) {
979 vco_min = pll->lcd_pll_out_min;
980 vco_max = pll->lcd_pll_out_max;
982 vco_min = pll->pll_out_min;
983 vco_max = pll->pll_out_max;
986 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
991 post_div_min = vco_min / target_clock;
992 if ((target_clock * post_div_min) < vco_min)
994 if (post_div_min < pll->min_post_div)
995 post_div_min = pll->min_post_div;
997 post_div_max = vco_max / target_clock;
998 if ((target_clock * post_div_max) > vco_max)
1000 if (post_div_max > pll->max_post_div)
1001 post_div_max = pll->max_post_div;
1004 /* represent the searched ratio as fractional number */
1006 den = pll->reference_freq;
1008 /* reduce the numbers to a simpler ratio */
1009 avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
1011 /* now search for a post divider */
1012 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
1013 post_div_best = post_div_min;
1015 post_div_best = post_div_max;
1018 for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
1020 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
1021 ref_div_max, &fb_div, &ref_div);
1022 diff = abs(target_clock - (pll->reference_freq * fb_div) /
1023 (ref_div * post_div));
1025 if (diff < diff_best || (diff == diff_best &&
1026 !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
1028 post_div_best = post_div;
1032 post_div = post_div_best;
1034 /* get the feedback and reference divider for the optimal value */
1035 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
1038 /* reduce the numbers to a simpler ratio once more */
1039 /* this also makes sure that the reference divider is large enough */
1040 avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
1042 /* and finally save the result */
1043 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1044 *fb_div_p = fb_div / 10;
1045 *frac_fb_div_p = fb_div % 10;
1051 *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
1052 (pll->reference_freq * *frac_fb_div_p)) /
1053 (ref_div * post_div * 10);
1054 *ref_div_p = ref_div;
1055 *post_div_p = post_div;
1057 DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1058 freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
1063 static inline uint32_t radeon_div(uint64_t n, uint32_t d)
1073 void radeon_compute_pll_legacy(struct radeon_pll *pll,
1075 uint32_t *dot_clock_p,
1077 uint32_t *frac_fb_div_p,
1078 uint32_t *ref_div_p,
1079 uint32_t *post_div_p)
1081 uint32_t min_ref_div = pll->min_ref_div;
1082 uint32_t max_ref_div = pll->max_ref_div;
1083 uint32_t min_post_div = pll->min_post_div;
1084 uint32_t max_post_div = pll->max_post_div;
1085 uint32_t min_fractional_feed_div = 0;
1086 uint32_t max_fractional_feed_div = 0;
1087 uint32_t best_vco = pll->best_vco;
1088 uint32_t best_post_div = 1;
1089 uint32_t best_ref_div = 1;
1090 uint32_t best_feedback_div = 1;
1091 uint32_t best_frac_feedback_div = 0;
1092 uint32_t best_freq = -1;
1093 uint32_t best_error = 0xffffffff;
1094 uint32_t best_vco_diff = 1;
1096 u32 pll_out_min, pll_out_max;
1098 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
1101 if (pll->flags & RADEON_PLL_IS_LCD) {
1102 pll_out_min = pll->lcd_pll_out_min;
1103 pll_out_max = pll->lcd_pll_out_max;
1105 pll_out_min = pll->pll_out_min;
1106 pll_out_max = pll->pll_out_max;
1109 if (pll_out_min > 64800)
1110 pll_out_min = 64800;
1112 if (pll->flags & RADEON_PLL_USE_REF_DIV)
1113 min_ref_div = max_ref_div = pll->reference_div;
1115 while (min_ref_div < max_ref_div-1) {
1116 uint32_t mid = (min_ref_div + max_ref_div) / 2;
1117 uint32_t pll_in = pll->reference_freq / mid;
1118 if (pll_in < pll->pll_in_min)
1120 else if (pll_in > pll->pll_in_max)
1127 if (pll->flags & RADEON_PLL_USE_POST_DIV)
1128 min_post_div = max_post_div = pll->post_div;
1130 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1131 min_fractional_feed_div = pll->min_frac_feedback_div;
1132 max_fractional_feed_div = pll->max_frac_feedback_div;
1135 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
1138 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
1141 /* legacy radeons only have a few post_divs */
1142 if (pll->flags & RADEON_PLL_LEGACY) {
1143 if ((post_div == 5) ||
1154 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1155 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1156 uint32_t pll_in = pll->reference_freq / ref_div;
1157 uint32_t min_feed_div = pll->min_feedback_div;
1158 uint32_t max_feed_div = pll->max_feedback_div + 1;
1160 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1163 while (min_feed_div < max_feed_div) {
1165 uint32_t min_frac_feed_div = min_fractional_feed_div;
1166 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1167 uint32_t frac_feedback_div;
1170 feedback_div = (min_feed_div + max_feed_div) / 2;
1172 tmp = (uint64_t)pll->reference_freq * feedback_div;
1173 vco = radeon_div(tmp, ref_div);
1175 if (vco < pll_out_min) {
1176 min_feed_div = feedback_div + 1;
1178 } else if (vco > pll_out_max) {
1179 max_feed_div = feedback_div;
1183 while (min_frac_feed_div < max_frac_feed_div) {
1184 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1185 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1186 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1187 current_freq = radeon_div(tmp, ref_div * post_div);
1189 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
1190 if (freq < current_freq)
1193 error = freq - current_freq;
1195 error = abs(current_freq - freq);
1196 vco_diff = abs(vco - best_vco);
1198 if ((best_vco == 0 && error < best_error) ||
1200 ((best_error > 100 && error < best_error - 100) ||
1201 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
1202 best_post_div = post_div;
1203 best_ref_div = ref_div;
1204 best_feedback_div = feedback_div;
1205 best_frac_feedback_div = frac_feedback_div;
1206 best_freq = current_freq;
1208 best_vco_diff = vco_diff;
1209 } else if (current_freq == freq) {
1210 if (best_freq == -1) {
1211 best_post_div = post_div;
1212 best_ref_div = ref_div;
1213 best_feedback_div = feedback_div;
1214 best_frac_feedback_div = frac_feedback_div;
1215 best_freq = current_freq;
1217 best_vco_diff = vco_diff;
1218 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1219 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1220 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1221 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1222 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1223 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1224 best_post_div = post_div;
1225 best_ref_div = ref_div;
1226 best_feedback_div = feedback_div;
1227 best_frac_feedback_div = frac_feedback_div;
1228 best_freq = current_freq;
1230 best_vco_diff = vco_diff;
1233 if (current_freq < freq)
1234 min_frac_feed_div = frac_feedback_div + 1;
1236 max_frac_feed_div = frac_feedback_div;
1238 if (current_freq < freq)
1239 min_feed_div = feedback_div + 1;
1241 max_feed_div = feedback_div;
1246 *dot_clock_p = best_freq / 10000;
1247 *fb_div_p = best_feedback_div;
1248 *frac_fb_div_p = best_frac_feedback_div;
1249 *ref_div_p = best_ref_div;
1250 *post_div_p = best_post_div;
1251 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1253 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
1254 best_ref_div, best_post_div);
1258 static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1260 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1262 if (radeon_fb->obj) {
1263 drm_gem_object_unreference_unlocked(radeon_fb->obj);
1265 drm_framebuffer_cleanup(fb);
1269 static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1270 struct drm_file *file_priv,
1271 unsigned int *handle)
1273 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1275 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1278 static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1279 .destroy = radeon_user_framebuffer_destroy,
1280 .create_handle = radeon_user_framebuffer_create_handle,
1284 radeon_framebuffer_init(struct drm_device *dev,
1285 struct radeon_framebuffer *rfb,
1286 struct drm_mode_fb_cmd2 *mode_cmd,
1287 struct drm_gem_object *obj)
1291 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
1292 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1300 static struct drm_framebuffer *
1301 radeon_user_framebuffer_create(struct drm_device *dev,
1302 struct drm_file *file_priv,
1303 struct drm_mode_fb_cmd2 *mode_cmd)
1305 struct drm_gem_object *obj;
1306 struct radeon_framebuffer *radeon_fb;
1309 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
1311 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
1312 "can't create framebuffer\n", mode_cmd->handles[0]);
1313 return ERR_PTR(-ENOENT);
1316 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
1317 if (radeon_fb == NULL) {
1318 drm_gem_object_unreference_unlocked(obj);
1319 return ERR_PTR(-ENOMEM);
1322 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1325 drm_gem_object_unreference_unlocked(obj);
1326 return ERR_PTR(ret);
1329 return &radeon_fb->base;
1332 static void radeon_output_poll_changed(struct drm_device *dev)
1334 struct radeon_device *rdev = dev->dev_private;
1335 radeon_fb_output_poll_changed(rdev);
1338 static const struct drm_mode_config_funcs radeon_mode_funcs = {
1339 .fb_create = radeon_user_framebuffer_create,
1340 .output_poll_changed = radeon_output_poll_changed
1343 static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1348 static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1349 { { TV_STD_NTSC, "ntsc" },
1350 { TV_STD_PAL, "pal" },
1351 { TV_STD_PAL_M, "pal-m" },
1352 { TV_STD_PAL_60, "pal-60" },
1353 { TV_STD_NTSC_J, "ntsc-j" },
1354 { TV_STD_SCART_PAL, "scart-pal" },
1355 { TV_STD_PAL_CN, "pal-cn" },
1356 { TV_STD_SECAM, "secam" },
1359 static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1360 { { UNDERSCAN_OFF, "off" },
1361 { UNDERSCAN_ON, "on" },
1362 { UNDERSCAN_AUTO, "auto" },
1365 static struct drm_prop_enum_list radeon_audio_enum_list[] =
1366 { { RADEON_AUDIO_DISABLE, "off" },
1367 { RADEON_AUDIO_ENABLE, "on" },
1368 { RADEON_AUDIO_AUTO, "auto" },
1371 /* XXX support different dither options? spatial, temporal, both, etc. */
1372 static struct drm_prop_enum_list radeon_dither_enum_list[] =
1373 { { RADEON_FMT_DITHER_DISABLE, "off" },
1374 { RADEON_FMT_DITHER_ENABLE, "on" },
1377 static int radeon_modeset_create_props(struct radeon_device *rdev)
1381 if (rdev->is_atom_bios) {
1382 rdev->mode_info.coherent_mode_property =
1383 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
1384 if (!rdev->mode_info.coherent_mode_property)
1388 if (!ASIC_IS_AVIVO(rdev)) {
1389 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1390 rdev->mode_info.tmds_pll_property =
1391 drm_property_create_enum(rdev->ddev, 0,
1393 radeon_tmds_pll_enum_list, sz);
1396 rdev->mode_info.load_detect_property =
1397 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
1398 if (!rdev->mode_info.load_detect_property)
1401 drm_mode_create_scaling_mode_property(rdev->ddev);
1403 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1404 rdev->mode_info.tv_std_property =
1405 drm_property_create_enum(rdev->ddev, 0,
1407 radeon_tv_std_enum_list, sz);
1409 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1410 rdev->mode_info.underscan_property =
1411 drm_property_create_enum(rdev->ddev, 0,
1413 radeon_underscan_enum_list, sz);
1415 rdev->mode_info.underscan_hborder_property =
1416 drm_property_create_range(rdev->ddev, 0,
1417 "underscan hborder", 0, 128);
1418 if (!rdev->mode_info.underscan_hborder_property)
1421 rdev->mode_info.underscan_vborder_property =
1422 drm_property_create_range(rdev->ddev, 0,
1423 "underscan vborder", 0, 128);
1424 if (!rdev->mode_info.underscan_vborder_property)
1427 sz = ARRAY_SIZE(radeon_audio_enum_list);
1428 rdev->mode_info.audio_property =
1429 drm_property_create_enum(rdev->ddev, 0,
1431 radeon_audio_enum_list, sz);
1433 sz = ARRAY_SIZE(radeon_dither_enum_list);
1434 rdev->mode_info.dither_property =
1435 drm_property_create_enum(rdev->ddev, 0,
1437 radeon_dither_enum_list, sz);
1442 void radeon_update_display_priority(struct radeon_device *rdev)
1444 /* adjustment options for the display watermarks */
1445 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1446 /* set display priority to high for r3xx, rv515 chips
1447 * this avoids flickering due to underflow to the
1448 * display controllers during heavy acceleration.
1449 * Don't force high on rs4xx igp chips as it seems to
1450 * affect the sound card. See kernel bug 15982.
1452 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1453 !(rdev->flags & RADEON_IS_IGP))
1454 rdev->disp_priority = 2;
1456 rdev->disp_priority = 0;
1458 rdev->disp_priority = radeon_disp_priority;
1463 * Allocate hdmi structs and determine register offsets
1465 static void radeon_afmt_init(struct radeon_device *rdev)
1469 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1470 rdev->mode_info.afmt[i] = NULL;
1472 if (ASIC_IS_NODCE(rdev)) {
1474 } else if (ASIC_IS_DCE4(rdev)) {
1475 static uint32_t eg_offsets[] = {
1476 EVERGREEN_CRTC0_REGISTER_OFFSET,
1477 EVERGREEN_CRTC1_REGISTER_OFFSET,
1478 EVERGREEN_CRTC2_REGISTER_OFFSET,
1479 EVERGREEN_CRTC3_REGISTER_OFFSET,
1480 EVERGREEN_CRTC4_REGISTER_OFFSET,
1481 EVERGREEN_CRTC5_REGISTER_OFFSET,
1486 /* DCE8 has 7 audio blocks tied to DIG encoders */
1487 /* DCE6 has 6 audio blocks tied to DIG encoders */
1488 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1489 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
1490 if (ASIC_IS_DCE8(rdev))
1492 else if (ASIC_IS_DCE6(rdev))
1494 else if (ASIC_IS_DCE5(rdev))
1496 else if (ASIC_IS_DCE41(rdev))
1501 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1502 for (i = 0; i < num_afmt; i++) {
1503 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1504 if (rdev->mode_info.afmt[i]) {
1505 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1506 rdev->mode_info.afmt[i]->id = i;
1509 } else if (ASIC_IS_DCE3(rdev)) {
1510 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1511 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1512 if (rdev->mode_info.afmt[0]) {
1513 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1514 rdev->mode_info.afmt[0]->id = 0;
1516 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1517 if (rdev->mode_info.afmt[1]) {
1518 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1519 rdev->mode_info.afmt[1]->id = 1;
1521 } else if (ASIC_IS_DCE2(rdev)) {
1522 /* DCE2 has at least 1 routable audio block */
1523 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1524 if (rdev->mode_info.afmt[0]) {
1525 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1526 rdev->mode_info.afmt[0]->id = 0;
1528 /* r6xx has 2 routable audio blocks */
1529 if (rdev->family >= CHIP_R600) {
1530 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1531 if (rdev->mode_info.afmt[1]) {
1532 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1533 rdev->mode_info.afmt[1]->id = 1;
1539 static void radeon_afmt_fini(struct radeon_device *rdev)
1543 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1544 kfree(rdev->mode_info.afmt[i]);
1545 rdev->mode_info.afmt[i] = NULL;
1549 int radeon_modeset_init(struct radeon_device *rdev)
1554 drm_mode_config_init(rdev->ddev);
1555 rdev->mode_info.mode_config_initialized = true;
1557 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
1559 if (ASIC_IS_DCE5(rdev)) {
1560 rdev->ddev->mode_config.max_width = 16384;
1561 rdev->ddev->mode_config.max_height = 16384;
1562 } else if (ASIC_IS_AVIVO(rdev)) {
1563 rdev->ddev->mode_config.max_width = 8192;
1564 rdev->ddev->mode_config.max_height = 8192;
1566 rdev->ddev->mode_config.max_width = 4096;
1567 rdev->ddev->mode_config.max_height = 4096;
1570 rdev->ddev->mode_config.preferred_depth = 24;
1571 rdev->ddev->mode_config.prefer_shadow = 1;
1573 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1575 ret = radeon_modeset_create_props(rdev);
1580 /* init i2c buses */
1581 radeon_i2c_init(rdev);
1583 /* check combios for a valid hardcoded EDID - Sun servers */
1584 if (!rdev->is_atom_bios) {
1585 /* check for hardcoded EDID in BIOS */
1586 radeon_combios_check_hardcoded_edid(rdev);
1589 /* allocate crtcs */
1590 for (i = 0; i < rdev->num_crtc; i++) {
1591 radeon_crtc_init(rdev->ddev, i);
1594 /* okay we should have all the bios connectors */
1595 ret = radeon_setup_enc_conn(rdev->ddev);
1600 /* init dig PHYs, disp eng pll */
1601 if (rdev->is_atom_bios) {
1602 radeon_atom_encoder_init(rdev);
1603 radeon_atom_disp_eng_pll_init(rdev);
1606 /* initialize hpd */
1607 radeon_hpd_init(rdev);
1610 radeon_afmt_init(rdev);
1612 radeon_fbdev_init(rdev);
1613 drm_kms_helper_poll_init(rdev->ddev);
1615 if (rdev->pm.dpm_enabled) {
1616 /* do dpm late init */
1617 ret = radeon_pm_late_init(rdev);
1619 rdev->pm.dpm_enabled = false;
1620 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1622 /* set the dpm state for PX since there won't be
1623 * a modeset to call this.
1625 radeon_pm_compute_clocks(rdev);
1631 void radeon_modeset_fini(struct radeon_device *rdev)
1633 radeon_fbdev_fini(rdev);
1634 kfree(rdev->mode_info.bios_hardcoded_edid);
1636 if (rdev->mode_info.mode_config_initialized) {
1637 radeon_afmt_fini(rdev);
1638 drm_kms_helper_poll_fini(rdev->ddev);
1639 radeon_hpd_fini(rdev);
1640 drm_mode_config_cleanup(rdev->ddev);
1641 rdev->mode_info.mode_config_initialized = false;
1643 /* free i2c buses */
1644 radeon_i2c_fini(rdev);
1647 static bool is_hdtv_mode(const struct drm_display_mode *mode)
1649 /* try and guess if this is a tv or a monitor */
1650 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1651 (mode->vdisplay == 576) || /* 576p */
1652 (mode->vdisplay == 720) || /* 720p */
1653 (mode->vdisplay == 1080)) /* 1080p */
1659 bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1660 const struct drm_display_mode *mode,
1661 struct drm_display_mode *adjusted_mode)
1663 struct drm_device *dev = crtc->dev;
1664 struct radeon_device *rdev = dev->dev_private;
1665 struct drm_encoder *encoder;
1666 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1667 struct radeon_encoder *radeon_encoder;
1668 struct drm_connector *connector;
1669 struct radeon_connector *radeon_connector;
1671 u32 src_v = 1, dst_v = 1;
1672 u32 src_h = 1, dst_h = 1;
1674 radeon_crtc->h_border = 0;
1675 radeon_crtc->v_border = 0;
1677 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1678 if (encoder->crtc != crtc)
1680 radeon_encoder = to_radeon_encoder(encoder);
1681 connector = radeon_get_connector_for_encoder(encoder);
1682 radeon_connector = to_radeon_connector(connector);
1686 if (radeon_encoder->rmx_type == RMX_OFF)
1687 radeon_crtc->rmx_type = RMX_OFF;
1688 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1689 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1690 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1692 radeon_crtc->rmx_type = RMX_OFF;
1693 /* copy native mode */
1694 memcpy(&radeon_crtc->native_mode,
1695 &radeon_encoder->native_mode,
1696 sizeof(struct drm_display_mode));
1697 src_v = crtc->mode.vdisplay;
1698 dst_v = radeon_crtc->native_mode.vdisplay;
1699 src_h = crtc->mode.hdisplay;
1700 dst_h = radeon_crtc->native_mode.hdisplay;
1702 /* fix up for overscan on hdmi */
1703 if (ASIC_IS_AVIVO(rdev) &&
1704 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1705 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1706 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
1707 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1708 is_hdtv_mode(mode)))) {
1709 if (radeon_encoder->underscan_hborder != 0)
1710 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1712 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1713 if (radeon_encoder->underscan_vborder != 0)
1714 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1716 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1717 radeon_crtc->rmx_type = RMX_FULL;
1718 src_v = crtc->mode.vdisplay;
1719 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1720 src_h = crtc->mode.hdisplay;
1721 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1725 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1726 /* WARNING: Right now this can't happen but
1727 * in the future we need to check that scaling
1728 * are consistent across different encoder
1729 * (ie all encoder can work with the same
1732 DRM_ERROR("Scaling not consistent across encoder.\n");
1737 if (radeon_crtc->rmx_type != RMX_OFF) {
1739 a.full = dfixed_const(src_v);
1740 b.full = dfixed_const(dst_v);
1741 radeon_crtc->vsc.full = dfixed_div(a, b);
1742 a.full = dfixed_const(src_h);
1743 b.full = dfixed_const(dst_h);
1744 radeon_crtc->hsc.full = dfixed_div(a, b);
1746 radeon_crtc->vsc.full = dfixed_const(1);
1747 radeon_crtc->hsc.full = dfixed_const(1);
1753 * Retrieve current video scanout position of crtc on a given gpu, and
1754 * an optional accurate timestamp of when query happened.
1756 * \param dev Device to query.
1757 * \param crtc Crtc to query.
1758 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1759 * \param *vpos Location where vertical scanout position should be stored.
1760 * \param *hpos Location where horizontal scanout position should go.
1761 * \param *stime Target location for timestamp taken immediately before
1762 * scanout position query. Can be NULL to skip timestamp.
1763 * \param *etime Target location for timestamp taken immediately after
1764 * scanout position query. Can be NULL to skip timestamp.
1766 * Returns vpos as a positive number while in active scanout area.
1767 * Returns vpos as a negative number inside vblank, counting the number
1768 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1769 * until start of active scanout / end of vblank."
1771 * \return Flags, or'ed together as follows:
1773 * DRM_SCANOUTPOS_VALID = Query successful.
1774 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1775 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1776 * this flag means that returned position may be offset by a constant but
1777 * unknown small number of scanlines wrt. real scanout position.
1780 int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1781 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
1783 u32 stat_crtc = 0, vbl = 0, position = 0;
1784 int vbl_start, vbl_end, vtotal, ret = 0;
1787 struct radeon_device *rdev = dev->dev_private;
1789 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1791 /* Get optional system timestamp before query. */
1793 *stime = ktime_get();
1795 if (ASIC_IS_DCE4(rdev)) {
1797 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1798 EVERGREEN_CRTC0_REGISTER_OFFSET);
1799 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1800 EVERGREEN_CRTC0_REGISTER_OFFSET);
1801 ret |= DRM_SCANOUTPOS_VALID;
1804 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1805 EVERGREEN_CRTC1_REGISTER_OFFSET);
1806 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1807 EVERGREEN_CRTC1_REGISTER_OFFSET);
1808 ret |= DRM_SCANOUTPOS_VALID;
1811 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1812 EVERGREEN_CRTC2_REGISTER_OFFSET);
1813 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1814 EVERGREEN_CRTC2_REGISTER_OFFSET);
1815 ret |= DRM_SCANOUTPOS_VALID;
1818 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1819 EVERGREEN_CRTC3_REGISTER_OFFSET);
1820 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1821 EVERGREEN_CRTC3_REGISTER_OFFSET);
1822 ret |= DRM_SCANOUTPOS_VALID;
1825 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1826 EVERGREEN_CRTC4_REGISTER_OFFSET);
1827 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1828 EVERGREEN_CRTC4_REGISTER_OFFSET);
1829 ret |= DRM_SCANOUTPOS_VALID;
1832 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1833 EVERGREEN_CRTC5_REGISTER_OFFSET);
1834 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1835 EVERGREEN_CRTC5_REGISTER_OFFSET);
1836 ret |= DRM_SCANOUTPOS_VALID;
1838 } else if (ASIC_IS_AVIVO(rdev)) {
1840 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1841 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1842 ret |= DRM_SCANOUTPOS_VALID;
1845 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1846 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1847 ret |= DRM_SCANOUTPOS_VALID;
1850 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1852 /* Assume vbl_end == 0, get vbl_start from
1855 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1856 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1857 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1858 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1859 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1860 if (!(stat_crtc & 1))
1863 ret |= DRM_SCANOUTPOS_VALID;
1866 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1867 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1868 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1869 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1870 if (!(stat_crtc & 1))
1873 ret |= DRM_SCANOUTPOS_VALID;
1877 /* Get optional system timestamp after query. */
1879 *etime = ktime_get();
1881 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1883 /* Decode into vertical and horizontal scanout position. */
1884 *vpos = position & 0x1fff;
1885 *hpos = (position >> 16) & 0x1fff;
1887 /* Valid vblank area boundaries from gpu retrieved? */
1890 ret |= DRM_SCANOUTPOS_ACCURATE;
1891 vbl_start = vbl & 0x1fff;
1892 vbl_end = (vbl >> 16) & 0x1fff;
1895 /* No: Fake something reasonable which gives at least ok results. */
1896 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1900 /* Test scanout position against vblank region. */
1901 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1904 /* Check if inside vblank area and apply corrective offsets:
1905 * vpos will then be >=0 in video scanout area, but negative
1906 * within vblank area, counting down the number of lines until
1910 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1911 if (in_vbl && (*vpos >= vbl_start)) {
1912 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1913 *vpos = *vpos - vtotal;
1916 /* Correct for shifted end of vbl at vbl_end. */
1917 *vpos = *vpos - vbl_end;
1921 ret |= DRM_SCANOUTPOS_INVBL;
1923 /* Is vpos outside nominal vblank area, but less than
1924 * 1/100 of a frame height away from start of vblank?
1925 * If so, assume this isn't a massively delayed vblank
1926 * interrupt, but a vblank interrupt that fired a few
1927 * microseconds before true start of vblank. Compensate
1928 * by adding a full frame duration to the final timestamp.
1929 * Happens, e.g., on ATI R500, R600.
1931 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1933 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1934 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1935 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1937 if (vbl_start - *vpos < vtotal / 100) {
1940 /* Signal this correction as "applied". */