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 /* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
70 WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
73 static void dce4_crtc_load_lut(struct drm_crtc *crtc)
75 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
76 struct drm_device *dev = crtc->dev;
77 struct radeon_device *rdev = dev->dev_private;
80 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
81 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
84 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
85 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
88 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
89 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
91 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
92 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
94 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
95 for (i = 0; i < 256; i++) {
96 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
97 (radeon_crtc->lut_r[i] << 20) |
98 (radeon_crtc->lut_g[i] << 10) |
99 (radeon_crtc->lut_b[i] << 0));
103 static void dce5_crtc_load_lut(struct drm_crtc *crtc)
105 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
106 struct drm_device *dev = crtc->dev;
107 struct radeon_device *rdev = dev->dev_private;
110 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
112 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
113 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
114 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
115 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
116 NI_GRPH_PRESCALE_BYPASS);
117 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
118 NI_OVL_PRESCALE_BYPASS);
119 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
120 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
121 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
123 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
125 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
126 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
127 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
129 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
130 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
131 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
133 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
134 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
136 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
137 for (i = 0; i < 256; i++) {
138 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
139 (radeon_crtc->lut_r[i] << 20) |
140 (radeon_crtc->lut_g[i] << 10) |
141 (radeon_crtc->lut_b[i] << 0));
144 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
145 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
147 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
148 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
149 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
150 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
151 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
152 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
153 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
154 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
155 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
156 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
157 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
158 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
159 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
160 if (ASIC_IS_DCE8(rdev)) {
161 /* XXX this only needs to be programmed once per crtc at startup,
162 * not sure where the best place for it is
164 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
165 CIK_CURSOR_ALPHA_BLND_ENA);
169 static void legacy_crtc_load_lut(struct drm_crtc *crtc)
171 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
172 struct drm_device *dev = crtc->dev;
173 struct radeon_device *rdev = dev->dev_private;
177 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
178 if (radeon_crtc->crtc_id == 0)
179 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
181 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
182 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
184 WREG8(RADEON_PALETTE_INDEX, 0);
185 for (i = 0; i < 256; i++) {
186 WREG32(RADEON_PALETTE_30_DATA,
187 (radeon_crtc->lut_r[i] << 20) |
188 (radeon_crtc->lut_g[i] << 10) |
189 (radeon_crtc->lut_b[i] << 0));
193 void radeon_crtc_load_lut(struct drm_crtc *crtc)
195 struct drm_device *dev = crtc->dev;
196 struct radeon_device *rdev = dev->dev_private;
201 if (ASIC_IS_DCE5(rdev))
202 dce5_crtc_load_lut(crtc);
203 else if (ASIC_IS_DCE4(rdev))
204 dce4_crtc_load_lut(crtc);
205 else if (ASIC_IS_AVIVO(rdev))
206 avivo_crtc_load_lut(crtc);
208 legacy_crtc_load_lut(crtc);
211 /** Sets the color ramps on behalf of fbcon */
212 void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
215 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
217 radeon_crtc->lut_r[regno] = red >> 6;
218 radeon_crtc->lut_g[regno] = green >> 6;
219 radeon_crtc->lut_b[regno] = blue >> 6;
222 /** Gets the color ramps on behalf of fbcon */
223 void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
224 u16 *blue, int regno)
226 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
228 *red = radeon_crtc->lut_r[regno] << 6;
229 *green = radeon_crtc->lut_g[regno] << 6;
230 *blue = radeon_crtc->lut_b[regno] << 6;
233 static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
234 u16 *blue, uint32_t start, uint32_t size)
236 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
237 int end = (start + size > 256) ? 256 : start + size, i;
239 /* userspace palettes are always correct as is */
240 for (i = start; i < end; i++) {
241 radeon_crtc->lut_r[i] = red[i] >> 6;
242 radeon_crtc->lut_g[i] = green[i] >> 6;
243 radeon_crtc->lut_b[i] = blue[i] >> 6;
245 radeon_crtc_load_lut(crtc);
248 static void radeon_crtc_destroy(struct drm_crtc *crtc)
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
252 drm_crtc_cleanup(crtc);
253 destroy_workqueue(radeon_crtc->flip_queue);
258 * radeon_unpin_work_func - unpin old buffer object
260 * @__work - kernel work item
262 * Unpin the old frame buffer object outside of the interrupt handler
264 static void radeon_unpin_work_func(struct work_struct *__work)
266 struct radeon_flip_work *work =
267 container_of(__work, struct radeon_flip_work, unpin_work);
270 /* unpin of the old buffer */
271 r = radeon_bo_reserve(work->old_rbo, false);
272 if (likely(r == 0)) {
273 r = radeon_bo_unpin(work->old_rbo);
274 if (unlikely(r != 0)) {
275 DRM_ERROR("failed to unpin buffer after flip\n");
277 radeon_bo_unreserve(work->old_rbo);
279 DRM_ERROR("failed to reserve buffer after flip\n");
281 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
285 void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
287 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
292 /* can happen during initialization */
293 if (radeon_crtc == NULL)
296 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
297 if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
298 DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
299 "RADEON_FLIP_SUBMITTED(%d)\n",
300 radeon_crtc->flip_status,
301 RADEON_FLIP_SUBMITTED);
302 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
306 update_pending = radeon_page_flip_pending(rdev, crtc_id);
308 /* Has the pageflip already completed in crtc, or is it certain
309 * to complete in this vblank?
311 if (update_pending &&
312 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
313 &vpos, &hpos, NULL, NULL)) &&
314 ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
315 (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
316 /* crtc didn't flip in this target vblank interval,
317 * but flip is pending in crtc. Based on the current
318 * scanout position we know that the current frame is
319 * (nearly) complete and the flip will (likely)
320 * complete before the start of the next frame.
324 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
326 radeon_crtc_handle_flip(rdev, crtc_id);
330 * radeon_crtc_handle_flip - page flip completed
332 * @rdev: radeon device pointer
333 * @crtc_id: crtc number this event is for
335 * Called when we are sure that a page flip for this crtc is completed.
337 void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
339 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
340 struct radeon_flip_work *work;
343 /* this can happen at init */
344 if (radeon_crtc == NULL)
347 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
348 work = radeon_crtc->flip_work;
349 if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
350 DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
351 "RADEON_FLIP_SUBMITTED(%d)\n",
352 radeon_crtc->flip_status,
353 RADEON_FLIP_SUBMITTED);
354 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
358 /* Pageflip completed. Clean up. */
359 radeon_crtc->flip_status = RADEON_FLIP_NONE;
360 radeon_crtc->flip_work = NULL;
362 /* wakeup userspace */
364 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
366 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
368 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
369 radeon_fence_unref(&work->fence);
370 radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
371 queue_work(radeon_crtc->flip_queue, &work->unpin_work);
375 * radeon_flip_work_func - page flip framebuffer
377 * @work - kernel work item
379 * Wait for the buffer object to become idle and do the actual page flip
381 static void radeon_flip_work_func(struct work_struct *__work)
383 struct radeon_flip_work *work =
384 container_of(__work, struct radeon_flip_work, flip_work);
385 struct radeon_device *rdev = work->rdev;
386 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
388 struct drm_crtc *crtc = &radeon_crtc->base;
389 struct drm_framebuffer *fb = work->fb;
391 uint32_t tiling_flags, pitch_pixels;
397 down_read(&rdev->exclusive_lock);
398 while (work->fence) {
399 r = radeon_fence_wait(work->fence, false);
401 up_read(&rdev->exclusive_lock);
402 r = radeon_gpu_reset(rdev);
403 down_read(&rdev->exclusive_lock);
407 DRM_ERROR("failed to wait on page flip fence (%d)!\n",
411 radeon_fence_unref(&work->fence);
414 /* pin the new buffer */
415 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
416 work->old_rbo, work->new_rbo);
418 r = radeon_bo_reserve(work->new_rbo, false);
419 if (unlikely(r != 0)) {
420 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
423 /* Only 27 bit offset for legacy CRTC */
424 r = radeon_bo_pin_restricted(work->new_rbo, RADEON_GEM_DOMAIN_VRAM,
425 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
426 if (unlikely(r != 0)) {
427 radeon_bo_unreserve(work->new_rbo);
429 DRM_ERROR("failed to pin new rbo buffer before flip\n");
432 radeon_bo_get_tiling_flags(work->new_rbo, &tiling_flags, NULL);
433 radeon_bo_unreserve(work->new_rbo);
435 if (!ASIC_IS_AVIVO(rdev)) {
436 /* crtc offset is from display base addr not FB location */
437 base -= radeon_crtc->legacy_display_base_addr;
438 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
440 if (tiling_flags & RADEON_TILING_MACRO) {
441 if (ASIC_IS_R300(rdev)) {
444 int byteshift = fb->bits_per_pixel >> 4;
445 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
446 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
449 int offset = crtc->y * pitch_pixels + crtc->x;
450 switch (fb->bits_per_pixel) {
471 r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
473 DRM_ERROR("failed to get vblank before flip\n");
477 /* We borrow the event spin lock for protecting flip_work */
478 spin_lock_irqsave(&crtc->dev->event_lock, flags);
480 /* set the proper interrupt */
481 radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
483 /* do the flip (mmio) */
484 radeon_page_flip(rdev, radeon_crtc->crtc_id, base);
486 radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
487 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
488 up_read(&rdev->exclusive_lock);
493 if (unlikely(radeon_bo_reserve(work->new_rbo, false) != 0)) {
494 DRM_ERROR("failed to reserve new rbo in error path\n");
497 if (unlikely(radeon_bo_unpin(work->new_rbo) != 0)) {
498 DRM_ERROR("failed to unpin new rbo in error path\n");
500 radeon_bo_unreserve(work->new_rbo);
503 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
504 radeon_fence_unref(&work->fence);
506 up_read(&rdev->exclusive_lock);
509 static int radeon_crtc_page_flip(struct drm_crtc *crtc,
510 struct drm_framebuffer *fb,
511 struct drm_pending_vblank_event *event,
512 uint32_t page_flip_flags)
514 struct drm_device *dev = crtc->dev;
515 struct radeon_device *rdev = dev->dev_private;
516 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
517 struct radeon_framebuffer *old_radeon_fb;
518 struct radeon_framebuffer *new_radeon_fb;
519 struct drm_gem_object *obj;
520 struct radeon_flip_work *work;
523 work = kzalloc(sizeof *work, GFP_KERNEL);
527 INIT_WORK(&work->flip_work, radeon_flip_work_func);
528 INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
531 work->crtc_id = radeon_crtc->crtc_id;
535 /* schedule unpin of the old buffer */
536 old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
537 obj = old_radeon_fb->obj;
539 /* take a reference to the old object */
540 drm_gem_object_reference(obj);
541 work->old_rbo = gem_to_radeon_bo(obj);
543 new_radeon_fb = to_radeon_framebuffer(fb);
544 obj = new_radeon_fb->obj;
545 work->new_rbo = gem_to_radeon_bo(obj);
547 spin_lock(&work->new_rbo->tbo.bdev->fence_lock);
548 if (work->new_rbo->tbo.sync_obj)
549 work->fence = radeon_fence_ref(work->new_rbo->tbo.sync_obj);
550 spin_unlock(&work->new_rbo->tbo.bdev->fence_lock);
552 /* We borrow the event spin lock for protecting flip_work */
553 spin_lock_irqsave(&crtc->dev->event_lock, flags);
555 if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
556 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
557 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
558 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
559 radeon_fence_unref(&work->fence);
563 radeon_crtc->flip_status = RADEON_FLIP_PENDING;
564 radeon_crtc->flip_work = work;
567 crtc->primary->fb = fb;
569 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
571 queue_work(radeon_crtc->flip_queue, &work->flip_work);
577 radeon_crtc_set_config(struct drm_mode_set *set)
579 struct drm_device *dev;
580 struct radeon_device *rdev;
581 struct drm_crtc *crtc;
585 if (!set || !set->crtc)
588 dev = set->crtc->dev;
590 ret = pm_runtime_get_sync(dev->dev);
594 ret = drm_crtc_helper_set_config(set);
596 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
600 pm_runtime_mark_last_busy(dev->dev);
602 rdev = dev->dev_private;
603 /* if we have active crtcs and we don't have a power ref,
604 take the current one */
605 if (active && !rdev->have_disp_power_ref) {
606 rdev->have_disp_power_ref = true;
609 /* if we have no active crtcs, then drop the power ref
611 if (!active && rdev->have_disp_power_ref) {
612 pm_runtime_put_autosuspend(dev->dev);
613 rdev->have_disp_power_ref = false;
616 /* drop the power reference we got coming in here */
617 pm_runtime_put_autosuspend(dev->dev);
620 static const struct drm_crtc_funcs radeon_crtc_funcs = {
621 .cursor_set = radeon_crtc_cursor_set,
622 .cursor_move = radeon_crtc_cursor_move,
623 .gamma_set = radeon_crtc_gamma_set,
624 .set_config = radeon_crtc_set_config,
625 .destroy = radeon_crtc_destroy,
626 .page_flip = radeon_crtc_page_flip,
629 static void radeon_crtc_init(struct drm_device *dev, int index)
631 struct radeon_device *rdev = dev->dev_private;
632 struct radeon_crtc *radeon_crtc;
635 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
636 if (radeon_crtc == NULL)
639 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
641 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
642 radeon_crtc->crtc_id = index;
643 radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
644 rdev->mode_info.crtcs[index] = radeon_crtc;
646 if (rdev->family >= CHIP_BONAIRE) {
647 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
648 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
650 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
651 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
653 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
654 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
657 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
658 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
659 radeon_crtc->mode_set.num_connectors = 0;
662 for (i = 0; i < 256; i++) {
663 radeon_crtc->lut_r[i] = i << 2;
664 radeon_crtc->lut_g[i] = i << 2;
665 radeon_crtc->lut_b[i] = i << 2;
668 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
669 radeon_atombios_init_crtc(dev, radeon_crtc);
671 radeon_legacy_init_crtc(dev, radeon_crtc);
674 static const char *encoder_names[38] = {
694 "INTERNAL_KLDSCP_TMDS1",
695 "INTERNAL_KLDSCP_DVO1",
696 "INTERNAL_KLDSCP_DAC1",
697 "INTERNAL_KLDSCP_DAC2",
706 "INTERNAL_KLDSCP_LVTMA",
715 static const char *hpd_names[6] = {
724 static void radeon_print_display_setup(struct drm_device *dev)
726 struct drm_connector *connector;
727 struct radeon_connector *radeon_connector;
728 struct drm_encoder *encoder;
729 struct radeon_encoder *radeon_encoder;
733 DRM_INFO("Radeon Display Connectors\n");
734 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
735 radeon_connector = to_radeon_connector(connector);
736 DRM_INFO("Connector %d:\n", i);
737 DRM_INFO(" %s\n", connector->name);
738 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
739 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
740 if (radeon_connector->ddc_bus) {
741 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
742 radeon_connector->ddc_bus->rec.mask_clk_reg,
743 radeon_connector->ddc_bus->rec.mask_data_reg,
744 radeon_connector->ddc_bus->rec.a_clk_reg,
745 radeon_connector->ddc_bus->rec.a_data_reg,
746 radeon_connector->ddc_bus->rec.en_clk_reg,
747 radeon_connector->ddc_bus->rec.en_data_reg,
748 radeon_connector->ddc_bus->rec.y_clk_reg,
749 radeon_connector->ddc_bus->rec.y_data_reg);
750 if (radeon_connector->router.ddc_valid)
751 DRM_INFO(" DDC Router 0x%x/0x%x\n",
752 radeon_connector->router.ddc_mux_control_pin,
753 radeon_connector->router.ddc_mux_state);
754 if (radeon_connector->router.cd_valid)
755 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
756 radeon_connector->router.cd_mux_control_pin,
757 radeon_connector->router.cd_mux_state);
759 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
760 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
761 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
762 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
763 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
764 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
765 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
767 DRM_INFO(" Encoders:\n");
768 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
769 radeon_encoder = to_radeon_encoder(encoder);
770 devices = radeon_encoder->devices & radeon_connector->devices;
772 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
773 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
774 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
775 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
776 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
777 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
778 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
779 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
780 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
781 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
782 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
783 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
784 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
785 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
786 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
787 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
788 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
789 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
790 if (devices & ATOM_DEVICE_TV1_SUPPORT)
791 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
792 if (devices & ATOM_DEVICE_CV_SUPPORT)
793 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
800 static bool radeon_setup_enc_conn(struct drm_device *dev)
802 struct radeon_device *rdev = dev->dev_private;
806 if (rdev->is_atom_bios) {
807 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
809 ret = radeon_get_atom_connector_info_from_object_table(dev);
811 ret = radeon_get_legacy_connector_info_from_bios(dev);
813 ret = radeon_get_legacy_connector_info_from_table(dev);
816 if (!ASIC_IS_AVIVO(rdev))
817 ret = radeon_get_legacy_connector_info_from_table(dev);
820 radeon_setup_encoder_clones(dev);
821 radeon_print_display_setup(dev);
827 int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
829 struct drm_device *dev = radeon_connector->base.dev;
830 struct radeon_device *rdev = dev->dev_private;
833 /* on hw with routers, select right port */
834 if (radeon_connector->router.ddc_valid)
835 radeon_router_select_ddc_port(radeon_connector);
837 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
838 ENCODER_OBJECT_ID_NONE) {
839 if (radeon_connector->ddc_bus->has_aux)
840 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
841 &radeon_connector->ddc_bus->aux.ddc);
842 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
843 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
844 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
846 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
847 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
848 radeon_connector->ddc_bus->has_aux)
849 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
850 &radeon_connector->ddc_bus->aux.ddc);
851 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
852 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
853 &radeon_connector->ddc_bus->adapter);
855 if (radeon_connector->ddc_bus && !radeon_connector->edid)
856 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
857 &radeon_connector->ddc_bus->adapter);
860 if (!radeon_connector->edid) {
861 if (rdev->is_atom_bios) {
862 /* some laptops provide a hardcoded edid in rom for LCDs */
863 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
864 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
865 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
867 /* some servers provide a hardcoded edid in rom for KVMs */
868 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
870 if (radeon_connector->edid) {
871 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
872 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
873 drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
876 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
883 * avivo_reduce_ratio - fractional number reduction
887 * @nom_min: minimum value for nominator
888 * @den_min: minimum value for denominator
890 * Find the greatest common divisor and apply it on both nominator and
891 * denominator, but make nominator and denominator are at least as large
892 * as their minimum values.
894 static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
895 unsigned nom_min, unsigned den_min)
899 /* reduce the numbers to a simpler ratio */
900 tmp = gcd(*nom, *den);
904 /* make sure nominator is large enough */
905 if (*nom < nom_min) {
906 tmp = DIV_ROUND_UP(nom_min, *nom);
911 /* make sure the denominator is large enough */
912 if (*den < den_min) {
913 tmp = DIV_ROUND_UP(den_min, *den);
920 * avivo_get_fb_ref_div - feedback and ref divider calculation
924 * @post_div: post divider
925 * @fb_div_max: feedback divider maximum
926 * @ref_div_max: reference divider maximum
927 * @fb_div: resulting feedback divider
928 * @ref_div: resulting reference divider
930 * Calculate feedback and reference divider for a given post divider. Makes
931 * sure we stay within the limits.
933 static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
934 unsigned fb_div_max, unsigned ref_div_max,
935 unsigned *fb_div, unsigned *ref_div)
937 /* limit reference * post divider to a maximum */
938 ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
940 /* get matching reference and feedback divider */
941 *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
942 *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
944 /* limit fb divider to its maximum */
945 if (*fb_div > fb_div_max) {
946 *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
947 *fb_div = fb_div_max;
952 * radeon_compute_pll_avivo - compute PLL paramaters
954 * @pll: information about the PLL
955 * @dot_clock_p: resulting pixel clock
956 * fb_div_p: resulting feedback divider
957 * frac_fb_div_p: fractional part of the feedback divider
958 * ref_div_p: resulting reference divider
959 * post_div_p: resulting reference divider
961 * Try to calculate the PLL parameters to generate the given frequency:
962 * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
964 void radeon_compute_pll_avivo(struct radeon_pll *pll,
972 unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
975 unsigned fb_div_min, fb_div_max, fb_div;
976 unsigned post_div_min, post_div_max, post_div;
977 unsigned ref_div_min, ref_div_max, ref_div;
978 unsigned post_div_best, diff_best;
981 /* determine allowed feedback divider range */
982 fb_div_min = pll->min_feedback_div;
983 fb_div_max = pll->max_feedback_div;
985 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
990 /* determine allowed ref divider range */
991 if (pll->flags & RADEON_PLL_USE_REF_DIV)
992 ref_div_min = pll->reference_div;
994 ref_div_min = pll->min_ref_div;
996 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
997 pll->flags & RADEON_PLL_USE_REF_DIV)
998 ref_div_max = pll->reference_div;
1000 ref_div_max = pll->max_ref_div;
1002 /* determine allowed post divider range */
1003 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
1004 post_div_min = pll->post_div;
1005 post_div_max = pll->post_div;
1007 unsigned vco_min, vco_max;
1009 if (pll->flags & RADEON_PLL_IS_LCD) {
1010 vco_min = pll->lcd_pll_out_min;
1011 vco_max = pll->lcd_pll_out_max;
1013 vco_min = pll->pll_out_min;
1014 vco_max = pll->pll_out_max;
1017 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1022 post_div_min = vco_min / target_clock;
1023 if ((target_clock * post_div_min) < vco_min)
1025 if (post_div_min < pll->min_post_div)
1026 post_div_min = pll->min_post_div;
1028 post_div_max = vco_max / target_clock;
1029 if ((target_clock * post_div_max) > vco_max)
1031 if (post_div_max > pll->max_post_div)
1032 post_div_max = pll->max_post_div;
1035 /* represent the searched ratio as fractional number */
1037 den = pll->reference_freq;
1039 /* reduce the numbers to a simpler ratio */
1040 avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
1042 /* now search for a post divider */
1043 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
1044 post_div_best = post_div_min;
1046 post_div_best = post_div_max;
1049 for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
1051 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
1052 ref_div_max, &fb_div, &ref_div);
1053 diff = abs(target_clock - (pll->reference_freq * fb_div) /
1054 (ref_div * post_div));
1056 if (diff < diff_best || (diff == diff_best &&
1057 !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
1059 post_div_best = post_div;
1063 post_div = post_div_best;
1065 /* get the feedback and reference divider for the optimal value */
1066 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
1069 /* reduce the numbers to a simpler ratio once more */
1070 /* this also makes sure that the reference divider is large enough */
1071 avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
1073 /* avoid high jitter with small fractional dividers */
1074 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
1075 fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
1076 if (fb_div < fb_div_min) {
1077 unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
1083 /* and finally save the result */
1084 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1085 *fb_div_p = fb_div / 10;
1086 *frac_fb_div_p = fb_div % 10;
1092 *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
1093 (pll->reference_freq * *frac_fb_div_p)) /
1094 (ref_div * post_div * 10);
1095 *ref_div_p = ref_div;
1096 *post_div_p = post_div;
1098 DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1099 freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
1104 static inline uint32_t radeon_div(uint64_t n, uint32_t d)
1114 void radeon_compute_pll_legacy(struct radeon_pll *pll,
1116 uint32_t *dot_clock_p,
1118 uint32_t *frac_fb_div_p,
1119 uint32_t *ref_div_p,
1120 uint32_t *post_div_p)
1122 uint32_t min_ref_div = pll->min_ref_div;
1123 uint32_t max_ref_div = pll->max_ref_div;
1124 uint32_t min_post_div = pll->min_post_div;
1125 uint32_t max_post_div = pll->max_post_div;
1126 uint32_t min_fractional_feed_div = 0;
1127 uint32_t max_fractional_feed_div = 0;
1128 uint32_t best_vco = pll->best_vco;
1129 uint32_t best_post_div = 1;
1130 uint32_t best_ref_div = 1;
1131 uint32_t best_feedback_div = 1;
1132 uint32_t best_frac_feedback_div = 0;
1133 uint32_t best_freq = -1;
1134 uint32_t best_error = 0xffffffff;
1135 uint32_t best_vco_diff = 1;
1137 u32 pll_out_min, pll_out_max;
1139 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
1142 if (pll->flags & RADEON_PLL_IS_LCD) {
1143 pll_out_min = pll->lcd_pll_out_min;
1144 pll_out_max = pll->lcd_pll_out_max;
1146 pll_out_min = pll->pll_out_min;
1147 pll_out_max = pll->pll_out_max;
1150 if (pll_out_min > 64800)
1151 pll_out_min = 64800;
1153 if (pll->flags & RADEON_PLL_USE_REF_DIV)
1154 min_ref_div = max_ref_div = pll->reference_div;
1156 while (min_ref_div < max_ref_div-1) {
1157 uint32_t mid = (min_ref_div + max_ref_div) / 2;
1158 uint32_t pll_in = pll->reference_freq / mid;
1159 if (pll_in < pll->pll_in_min)
1161 else if (pll_in > pll->pll_in_max)
1168 if (pll->flags & RADEON_PLL_USE_POST_DIV)
1169 min_post_div = max_post_div = pll->post_div;
1171 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1172 min_fractional_feed_div = pll->min_frac_feedback_div;
1173 max_fractional_feed_div = pll->max_frac_feedback_div;
1176 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
1179 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
1182 /* legacy radeons only have a few post_divs */
1183 if (pll->flags & RADEON_PLL_LEGACY) {
1184 if ((post_div == 5) ||
1195 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1196 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1197 uint32_t pll_in = pll->reference_freq / ref_div;
1198 uint32_t min_feed_div = pll->min_feedback_div;
1199 uint32_t max_feed_div = pll->max_feedback_div + 1;
1201 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1204 while (min_feed_div < max_feed_div) {
1206 uint32_t min_frac_feed_div = min_fractional_feed_div;
1207 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1208 uint32_t frac_feedback_div;
1211 feedback_div = (min_feed_div + max_feed_div) / 2;
1213 tmp = (uint64_t)pll->reference_freq * feedback_div;
1214 vco = radeon_div(tmp, ref_div);
1216 if (vco < pll_out_min) {
1217 min_feed_div = feedback_div + 1;
1219 } else if (vco > pll_out_max) {
1220 max_feed_div = feedback_div;
1224 while (min_frac_feed_div < max_frac_feed_div) {
1225 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1226 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1227 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1228 current_freq = radeon_div(tmp, ref_div * post_div);
1230 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
1231 if (freq < current_freq)
1234 error = freq - current_freq;
1236 error = abs(current_freq - freq);
1237 vco_diff = abs(vco - best_vco);
1239 if ((best_vco == 0 && error < best_error) ||
1241 ((best_error > 100 && error < best_error - 100) ||
1242 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
1243 best_post_div = post_div;
1244 best_ref_div = ref_div;
1245 best_feedback_div = feedback_div;
1246 best_frac_feedback_div = frac_feedback_div;
1247 best_freq = current_freq;
1249 best_vco_diff = vco_diff;
1250 } else if (current_freq == freq) {
1251 if (best_freq == -1) {
1252 best_post_div = post_div;
1253 best_ref_div = ref_div;
1254 best_feedback_div = feedback_div;
1255 best_frac_feedback_div = frac_feedback_div;
1256 best_freq = current_freq;
1258 best_vco_diff = vco_diff;
1259 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1260 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1261 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1262 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1263 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1264 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1265 best_post_div = post_div;
1266 best_ref_div = ref_div;
1267 best_feedback_div = feedback_div;
1268 best_frac_feedback_div = frac_feedback_div;
1269 best_freq = current_freq;
1271 best_vco_diff = vco_diff;
1274 if (current_freq < freq)
1275 min_frac_feed_div = frac_feedback_div + 1;
1277 max_frac_feed_div = frac_feedback_div;
1279 if (current_freq < freq)
1280 min_feed_div = feedback_div + 1;
1282 max_feed_div = feedback_div;
1287 *dot_clock_p = best_freq / 10000;
1288 *fb_div_p = best_feedback_div;
1289 *frac_fb_div_p = best_frac_feedback_div;
1290 *ref_div_p = best_ref_div;
1291 *post_div_p = best_post_div;
1292 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1294 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
1295 best_ref_div, best_post_div);
1299 static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1301 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1303 if (radeon_fb->obj) {
1304 drm_gem_object_unreference_unlocked(radeon_fb->obj);
1306 drm_framebuffer_cleanup(fb);
1310 static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1311 struct drm_file *file_priv,
1312 unsigned int *handle)
1314 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1316 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1319 static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1320 .destroy = radeon_user_framebuffer_destroy,
1321 .create_handle = radeon_user_framebuffer_create_handle,
1325 radeon_framebuffer_init(struct drm_device *dev,
1326 struct radeon_framebuffer *rfb,
1327 struct drm_mode_fb_cmd2 *mode_cmd,
1328 struct drm_gem_object *obj)
1332 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
1333 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1341 static struct drm_framebuffer *
1342 radeon_user_framebuffer_create(struct drm_device *dev,
1343 struct drm_file *file_priv,
1344 struct drm_mode_fb_cmd2 *mode_cmd)
1346 struct drm_gem_object *obj;
1347 struct radeon_framebuffer *radeon_fb;
1350 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
1352 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
1353 "can't create framebuffer\n", mode_cmd->handles[0]);
1354 return ERR_PTR(-ENOENT);
1357 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
1358 if (radeon_fb == NULL) {
1359 drm_gem_object_unreference_unlocked(obj);
1360 return ERR_PTR(-ENOMEM);
1363 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1366 drm_gem_object_unreference_unlocked(obj);
1367 return ERR_PTR(ret);
1370 return &radeon_fb->base;
1373 static void radeon_output_poll_changed(struct drm_device *dev)
1375 struct radeon_device *rdev = dev->dev_private;
1376 radeon_fb_output_poll_changed(rdev);
1379 static const struct drm_mode_config_funcs radeon_mode_funcs = {
1380 .fb_create = radeon_user_framebuffer_create,
1381 .output_poll_changed = radeon_output_poll_changed
1384 static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1389 static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1390 { { TV_STD_NTSC, "ntsc" },
1391 { TV_STD_PAL, "pal" },
1392 { TV_STD_PAL_M, "pal-m" },
1393 { TV_STD_PAL_60, "pal-60" },
1394 { TV_STD_NTSC_J, "ntsc-j" },
1395 { TV_STD_SCART_PAL, "scart-pal" },
1396 { TV_STD_PAL_CN, "pal-cn" },
1397 { TV_STD_SECAM, "secam" },
1400 static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1401 { { UNDERSCAN_OFF, "off" },
1402 { UNDERSCAN_ON, "on" },
1403 { UNDERSCAN_AUTO, "auto" },
1406 static struct drm_prop_enum_list radeon_audio_enum_list[] =
1407 { { RADEON_AUDIO_DISABLE, "off" },
1408 { RADEON_AUDIO_ENABLE, "on" },
1409 { RADEON_AUDIO_AUTO, "auto" },
1412 /* XXX support different dither options? spatial, temporal, both, etc. */
1413 static struct drm_prop_enum_list radeon_dither_enum_list[] =
1414 { { RADEON_FMT_DITHER_DISABLE, "off" },
1415 { RADEON_FMT_DITHER_ENABLE, "on" },
1418 static int radeon_modeset_create_props(struct radeon_device *rdev)
1422 if (rdev->is_atom_bios) {
1423 rdev->mode_info.coherent_mode_property =
1424 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
1425 if (!rdev->mode_info.coherent_mode_property)
1429 if (!ASIC_IS_AVIVO(rdev)) {
1430 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1431 rdev->mode_info.tmds_pll_property =
1432 drm_property_create_enum(rdev->ddev, 0,
1434 radeon_tmds_pll_enum_list, sz);
1437 rdev->mode_info.load_detect_property =
1438 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
1439 if (!rdev->mode_info.load_detect_property)
1442 drm_mode_create_scaling_mode_property(rdev->ddev);
1444 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1445 rdev->mode_info.tv_std_property =
1446 drm_property_create_enum(rdev->ddev, 0,
1448 radeon_tv_std_enum_list, sz);
1450 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1451 rdev->mode_info.underscan_property =
1452 drm_property_create_enum(rdev->ddev, 0,
1454 radeon_underscan_enum_list, sz);
1456 rdev->mode_info.underscan_hborder_property =
1457 drm_property_create_range(rdev->ddev, 0,
1458 "underscan hborder", 0, 128);
1459 if (!rdev->mode_info.underscan_hborder_property)
1462 rdev->mode_info.underscan_vborder_property =
1463 drm_property_create_range(rdev->ddev, 0,
1464 "underscan vborder", 0, 128);
1465 if (!rdev->mode_info.underscan_vborder_property)
1468 sz = ARRAY_SIZE(radeon_audio_enum_list);
1469 rdev->mode_info.audio_property =
1470 drm_property_create_enum(rdev->ddev, 0,
1472 radeon_audio_enum_list, sz);
1474 sz = ARRAY_SIZE(radeon_dither_enum_list);
1475 rdev->mode_info.dither_property =
1476 drm_property_create_enum(rdev->ddev, 0,
1478 radeon_dither_enum_list, sz);
1483 void radeon_update_display_priority(struct radeon_device *rdev)
1485 /* adjustment options for the display watermarks */
1486 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1487 /* set display priority to high for r3xx, rv515 chips
1488 * this avoids flickering due to underflow to the
1489 * display controllers during heavy acceleration.
1490 * Don't force high on rs4xx igp chips as it seems to
1491 * affect the sound card. See kernel bug 15982.
1493 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1494 !(rdev->flags & RADEON_IS_IGP))
1495 rdev->disp_priority = 2;
1497 rdev->disp_priority = 0;
1499 rdev->disp_priority = radeon_disp_priority;
1504 * Allocate hdmi structs and determine register offsets
1506 static void radeon_afmt_init(struct radeon_device *rdev)
1510 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1511 rdev->mode_info.afmt[i] = NULL;
1513 if (ASIC_IS_NODCE(rdev)) {
1515 } else if (ASIC_IS_DCE4(rdev)) {
1516 static uint32_t eg_offsets[] = {
1517 EVERGREEN_CRTC0_REGISTER_OFFSET,
1518 EVERGREEN_CRTC1_REGISTER_OFFSET,
1519 EVERGREEN_CRTC2_REGISTER_OFFSET,
1520 EVERGREEN_CRTC3_REGISTER_OFFSET,
1521 EVERGREEN_CRTC4_REGISTER_OFFSET,
1522 EVERGREEN_CRTC5_REGISTER_OFFSET,
1527 /* DCE8 has 7 audio blocks tied to DIG encoders */
1528 /* DCE6 has 6 audio blocks tied to DIG encoders */
1529 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1530 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
1531 if (ASIC_IS_DCE8(rdev))
1533 else if (ASIC_IS_DCE6(rdev))
1535 else if (ASIC_IS_DCE5(rdev))
1537 else if (ASIC_IS_DCE41(rdev))
1542 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1543 for (i = 0; i < num_afmt; i++) {
1544 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1545 if (rdev->mode_info.afmt[i]) {
1546 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1547 rdev->mode_info.afmt[i]->id = i;
1550 } else if (ASIC_IS_DCE3(rdev)) {
1551 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1552 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1553 if (rdev->mode_info.afmt[0]) {
1554 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1555 rdev->mode_info.afmt[0]->id = 0;
1557 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1558 if (rdev->mode_info.afmt[1]) {
1559 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1560 rdev->mode_info.afmt[1]->id = 1;
1562 } else if (ASIC_IS_DCE2(rdev)) {
1563 /* DCE2 has at least 1 routable audio block */
1564 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1565 if (rdev->mode_info.afmt[0]) {
1566 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1567 rdev->mode_info.afmt[0]->id = 0;
1569 /* r6xx has 2 routable audio blocks */
1570 if (rdev->family >= CHIP_R600) {
1571 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1572 if (rdev->mode_info.afmt[1]) {
1573 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1574 rdev->mode_info.afmt[1]->id = 1;
1580 static void radeon_afmt_fini(struct radeon_device *rdev)
1584 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1585 kfree(rdev->mode_info.afmt[i]);
1586 rdev->mode_info.afmt[i] = NULL;
1590 int radeon_modeset_init(struct radeon_device *rdev)
1595 drm_mode_config_init(rdev->ddev);
1596 rdev->mode_info.mode_config_initialized = true;
1598 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
1600 if (ASIC_IS_DCE5(rdev)) {
1601 rdev->ddev->mode_config.max_width = 16384;
1602 rdev->ddev->mode_config.max_height = 16384;
1603 } else if (ASIC_IS_AVIVO(rdev)) {
1604 rdev->ddev->mode_config.max_width = 8192;
1605 rdev->ddev->mode_config.max_height = 8192;
1607 rdev->ddev->mode_config.max_width = 4096;
1608 rdev->ddev->mode_config.max_height = 4096;
1611 rdev->ddev->mode_config.preferred_depth = 24;
1612 rdev->ddev->mode_config.prefer_shadow = 1;
1614 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1616 ret = radeon_modeset_create_props(rdev);
1621 /* init i2c buses */
1622 radeon_i2c_init(rdev);
1624 /* check combios for a valid hardcoded EDID - Sun servers */
1625 if (!rdev->is_atom_bios) {
1626 /* check for hardcoded EDID in BIOS */
1627 radeon_combios_check_hardcoded_edid(rdev);
1630 /* allocate crtcs */
1631 for (i = 0; i < rdev->num_crtc; i++) {
1632 radeon_crtc_init(rdev->ddev, i);
1635 /* okay we should have all the bios connectors */
1636 ret = radeon_setup_enc_conn(rdev->ddev);
1641 /* init dig PHYs, disp eng pll */
1642 if (rdev->is_atom_bios) {
1643 radeon_atom_encoder_init(rdev);
1644 radeon_atom_disp_eng_pll_init(rdev);
1647 /* initialize hpd */
1648 radeon_hpd_init(rdev);
1651 radeon_afmt_init(rdev);
1653 radeon_fbdev_init(rdev);
1654 drm_kms_helper_poll_init(rdev->ddev);
1656 if (rdev->pm.dpm_enabled) {
1657 /* do dpm late init */
1658 ret = radeon_pm_late_init(rdev);
1660 rdev->pm.dpm_enabled = false;
1661 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1663 /* set the dpm state for PX since there won't be
1664 * a modeset to call this.
1666 radeon_pm_compute_clocks(rdev);
1672 void radeon_modeset_fini(struct radeon_device *rdev)
1674 radeon_fbdev_fini(rdev);
1675 kfree(rdev->mode_info.bios_hardcoded_edid);
1677 if (rdev->mode_info.mode_config_initialized) {
1678 radeon_afmt_fini(rdev);
1679 drm_kms_helper_poll_fini(rdev->ddev);
1680 radeon_hpd_fini(rdev);
1681 drm_mode_config_cleanup(rdev->ddev);
1682 rdev->mode_info.mode_config_initialized = false;
1684 /* free i2c buses */
1685 radeon_i2c_fini(rdev);
1688 static bool is_hdtv_mode(const struct drm_display_mode *mode)
1690 /* try and guess if this is a tv or a monitor */
1691 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1692 (mode->vdisplay == 576) || /* 576p */
1693 (mode->vdisplay == 720) || /* 720p */
1694 (mode->vdisplay == 1080)) /* 1080p */
1700 bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1701 const struct drm_display_mode *mode,
1702 struct drm_display_mode *adjusted_mode)
1704 struct drm_device *dev = crtc->dev;
1705 struct radeon_device *rdev = dev->dev_private;
1706 struct drm_encoder *encoder;
1707 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1708 struct radeon_encoder *radeon_encoder;
1709 struct drm_connector *connector;
1710 struct radeon_connector *radeon_connector;
1712 u32 src_v = 1, dst_v = 1;
1713 u32 src_h = 1, dst_h = 1;
1715 radeon_crtc->h_border = 0;
1716 radeon_crtc->v_border = 0;
1718 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1719 if (encoder->crtc != crtc)
1721 radeon_encoder = to_radeon_encoder(encoder);
1722 connector = radeon_get_connector_for_encoder(encoder);
1723 radeon_connector = to_radeon_connector(connector);
1727 if (radeon_encoder->rmx_type == RMX_OFF)
1728 radeon_crtc->rmx_type = RMX_OFF;
1729 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1730 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1731 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1733 radeon_crtc->rmx_type = RMX_OFF;
1734 /* copy native mode */
1735 memcpy(&radeon_crtc->native_mode,
1736 &radeon_encoder->native_mode,
1737 sizeof(struct drm_display_mode));
1738 src_v = crtc->mode.vdisplay;
1739 dst_v = radeon_crtc->native_mode.vdisplay;
1740 src_h = crtc->mode.hdisplay;
1741 dst_h = radeon_crtc->native_mode.hdisplay;
1743 /* fix up for overscan on hdmi */
1744 if (ASIC_IS_AVIVO(rdev) &&
1745 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1746 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1747 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
1748 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1749 is_hdtv_mode(mode)))) {
1750 if (radeon_encoder->underscan_hborder != 0)
1751 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1753 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1754 if (radeon_encoder->underscan_vborder != 0)
1755 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1757 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1758 radeon_crtc->rmx_type = RMX_FULL;
1759 src_v = crtc->mode.vdisplay;
1760 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1761 src_h = crtc->mode.hdisplay;
1762 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1766 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1767 /* WARNING: Right now this can't happen but
1768 * in the future we need to check that scaling
1769 * are consistent across different encoder
1770 * (ie all encoder can work with the same
1773 DRM_ERROR("Scaling not consistent across encoder.\n");
1778 if (radeon_crtc->rmx_type != RMX_OFF) {
1780 a.full = dfixed_const(src_v);
1781 b.full = dfixed_const(dst_v);
1782 radeon_crtc->vsc.full = dfixed_div(a, b);
1783 a.full = dfixed_const(src_h);
1784 b.full = dfixed_const(dst_h);
1785 radeon_crtc->hsc.full = dfixed_div(a, b);
1787 radeon_crtc->vsc.full = dfixed_const(1);
1788 radeon_crtc->hsc.full = dfixed_const(1);
1794 * Retrieve current video scanout position of crtc on a given gpu, and
1795 * an optional accurate timestamp of when query happened.
1797 * \param dev Device to query.
1798 * \param crtc Crtc to query.
1799 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1800 * \param *vpos Location where vertical scanout position should be stored.
1801 * \param *hpos Location where horizontal scanout position should go.
1802 * \param *stime Target location for timestamp taken immediately before
1803 * scanout position query. Can be NULL to skip timestamp.
1804 * \param *etime Target location for timestamp taken immediately after
1805 * scanout position query. Can be NULL to skip timestamp.
1807 * Returns vpos as a positive number while in active scanout area.
1808 * Returns vpos as a negative number inside vblank, counting the number
1809 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1810 * until start of active scanout / end of vblank."
1812 * \return Flags, or'ed together as follows:
1814 * DRM_SCANOUTPOS_VALID = Query successful.
1815 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1816 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1817 * this flag means that returned position may be offset by a constant but
1818 * unknown small number of scanlines wrt. real scanout position.
1821 int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1822 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
1824 u32 stat_crtc = 0, vbl = 0, position = 0;
1825 int vbl_start, vbl_end, vtotal, ret = 0;
1828 struct radeon_device *rdev = dev->dev_private;
1830 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1832 /* Get optional system timestamp before query. */
1834 *stime = ktime_get();
1836 if (ASIC_IS_DCE4(rdev)) {
1838 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1839 EVERGREEN_CRTC0_REGISTER_OFFSET);
1840 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1841 EVERGREEN_CRTC0_REGISTER_OFFSET);
1842 ret |= DRM_SCANOUTPOS_VALID;
1845 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1846 EVERGREEN_CRTC1_REGISTER_OFFSET);
1847 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1848 EVERGREEN_CRTC1_REGISTER_OFFSET);
1849 ret |= DRM_SCANOUTPOS_VALID;
1852 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1853 EVERGREEN_CRTC2_REGISTER_OFFSET);
1854 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1855 EVERGREEN_CRTC2_REGISTER_OFFSET);
1856 ret |= DRM_SCANOUTPOS_VALID;
1859 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1860 EVERGREEN_CRTC3_REGISTER_OFFSET);
1861 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1862 EVERGREEN_CRTC3_REGISTER_OFFSET);
1863 ret |= DRM_SCANOUTPOS_VALID;
1866 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1867 EVERGREEN_CRTC4_REGISTER_OFFSET);
1868 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1869 EVERGREEN_CRTC4_REGISTER_OFFSET);
1870 ret |= DRM_SCANOUTPOS_VALID;
1873 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1874 EVERGREEN_CRTC5_REGISTER_OFFSET);
1875 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1876 EVERGREEN_CRTC5_REGISTER_OFFSET);
1877 ret |= DRM_SCANOUTPOS_VALID;
1879 } else if (ASIC_IS_AVIVO(rdev)) {
1881 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1882 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1883 ret |= DRM_SCANOUTPOS_VALID;
1886 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1887 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1888 ret |= DRM_SCANOUTPOS_VALID;
1891 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1893 /* Assume vbl_end == 0, get vbl_start from
1896 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1897 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1898 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1899 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1900 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1901 if (!(stat_crtc & 1))
1904 ret |= DRM_SCANOUTPOS_VALID;
1907 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1908 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1909 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1910 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1911 if (!(stat_crtc & 1))
1914 ret |= DRM_SCANOUTPOS_VALID;
1918 /* Get optional system timestamp after query. */
1920 *etime = ktime_get();
1922 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1924 /* Decode into vertical and horizontal scanout position. */
1925 *vpos = position & 0x1fff;
1926 *hpos = (position >> 16) & 0x1fff;
1928 /* Valid vblank area boundaries from gpu retrieved? */
1931 ret |= DRM_SCANOUTPOS_ACCURATE;
1932 vbl_start = vbl & 0x1fff;
1933 vbl_end = (vbl >> 16) & 0x1fff;
1936 /* No: Fake something reasonable which gives at least ok results. */
1937 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1941 /* Test scanout position against vblank region. */
1942 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1945 /* Check if inside vblank area and apply corrective offsets:
1946 * vpos will then be >=0 in video scanout area, but negative
1947 * within vblank area, counting down the number of lines until
1951 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1952 if (in_vbl && (*vpos >= vbl_start)) {
1953 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1954 *vpos = *vpos - vtotal;
1957 /* Correct for shifted end of vbl at vbl_end. */
1958 *vpos = *vpos - vbl_end;
1962 ret |= DRM_SCANOUTPOS_INVBL;
1964 /* Is vpos outside nominal vblank area, but less than
1965 * 1/100 of a frame height away from start of vblank?
1966 * If so, assume this isn't a massively delayed vblank
1967 * interrupt, but a vblank interrupt that fired a few
1968 * microseconds before true start of vblank. Compensate
1969 * by adding a full frame duration to the final timestamp.
1970 * Happens, e.g., on ATI R500, R600.
1972 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1974 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1975 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1976 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1978 if (vbl_start - *vpos < vtotal / 100) {
1981 /* Signal this correction as "applied". */