drm/radeon: Move pinning the BO back to radeon_crtc_page_flip()
[deliverable/linux.git] / drivers / gpu / drm / radeon / radeon_display.c
CommitLineData
771fe6b9
JG
1/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
760285e7
DH
26#include <drm/drmP.h>
27#include <drm/radeon_drm.h>
771fe6b9
JG
28#include "radeon.h"
29
30#include "atom.h"
31#include <asm/div64.h>
32
10ebc0bc 33#include <linux/pm_runtime.h>
760285e7
DH
34#include <drm/drm_crtc_helper.h>
35#include <drm/drm_edid.h>
771fe6b9 36
32167016
CK
37#include <linux/gcd.h>
38
771fe6b9
JG
39static void avivo_crtc_load_lut(struct drm_crtc *crtc)
40{
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;
44 int i;
45
d9fdaafb 46 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
771fe6b9
JG
47 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
48
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);
52
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);
56
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);
60
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));
67 }
68
4366f3b5
MK
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);
771fe6b9
JG
71}
72
fee298fd 73static void dce4_crtc_load_lut(struct drm_crtc *crtc)
bcc1c2a1
AD
74{
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;
78 int i;
79
d9fdaafb 80 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
bcc1c2a1
AD
81 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
82
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);
86
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);
90
677d0768
AD
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);
bcc1c2a1 93
677d0768 94 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
bcc1c2a1 95 for (i = 0; i < 256; i++) {
677d0768 96 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
bcc1c2a1
AD
97 (radeon_crtc->lut_r[i] << 20) |
98 (radeon_crtc->lut_g[i] << 10) |
99 (radeon_crtc->lut_b[i] << 0));
100 }
101}
102
fee298fd
AD
103static void dce5_crtc_load_lut(struct drm_crtc *crtc)
104{
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;
108 int i;
109
110 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
111
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)));
122
123 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
124
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);
128
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);
132
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);
135
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));
142 }
143
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);
9e05fa1d
AD
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
163 */
164 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
165 CIK_CURSOR_ALPHA_BLND_ENA);
166 }
fee298fd
AD
167}
168
771fe6b9
JG
169static void legacy_crtc_load_lut(struct drm_crtc *crtc)
170{
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;
174 int i;
175 uint32_t dac2_cntl;
176
177 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
178 if (radeon_crtc->crtc_id == 0)
179 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
180 else
181 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
182 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
183
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));
190 }
191}
192
193void radeon_crtc_load_lut(struct drm_crtc *crtc)
194{
195 struct drm_device *dev = crtc->dev;
196 struct radeon_device *rdev = dev->dev_private;
197
198 if (!crtc->enabled)
199 return;
200
fee298fd
AD
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);
bcc1c2a1 205 else if (ASIC_IS_AVIVO(rdev))
771fe6b9
JG
206 avivo_crtc_load_lut(crtc);
207 else
208 legacy_crtc_load_lut(crtc);
209}
210
b8c00ac5 211/** Sets the color ramps on behalf of fbcon */
771fe6b9
JG
212void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
213 u16 blue, int regno)
214{
215 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
216
771fe6b9
JG
217 radeon_crtc->lut_r[regno] = red >> 6;
218 radeon_crtc->lut_g[regno] = green >> 6;
219 radeon_crtc->lut_b[regno] = blue >> 6;
220}
221
b8c00ac5
DA
222/** Gets the color ramps on behalf of fbcon */
223void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
224 u16 *blue, int regno)
225{
226 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
227
228 *red = radeon_crtc->lut_r[regno] << 6;
229 *green = radeon_crtc->lut_g[regno] << 6;
230 *blue = radeon_crtc->lut_b[regno] << 6;
231}
232
771fe6b9 233static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 234 u16 *blue, uint32_t start, uint32_t size)
771fe6b9
JG
235{
236 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
7203425a 237 int end = (start + size > 256) ? 256 : start + size, i;
771fe6b9 238
b8c00ac5 239 /* userspace palettes are always correct as is */
7203425a 240 for (i = start; i < end; i++) {
b8c00ac5
DA
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;
771fe6b9 244 }
771fe6b9
JG
245 radeon_crtc_load_lut(crtc);
246}
247
248static void radeon_crtc_destroy(struct drm_crtc *crtc)
249{
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251
771fe6b9 252 drm_crtc_cleanup(crtc);
fa7f517c 253 destroy_workqueue(radeon_crtc->flip_queue);
771fe6b9
JG
254 kfree(radeon_crtc);
255}
256
fa7f517c
CK
257/**
258 * radeon_unpin_work_func - unpin old buffer object
259 *
260 * @__work - kernel work item
261 *
262 * Unpin the old frame buffer object outside of the interrupt handler
6f34be50
AD
263 */
264static void radeon_unpin_work_func(struct work_struct *__work)
265{
fa7f517c
CK
266 struct radeon_flip_work *work =
267 container_of(__work, struct radeon_flip_work, unpin_work);
6f34be50
AD
268 int r;
269
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");
276 }
277 radeon_bo_unreserve(work->old_rbo);
278 } else
279 DRM_ERROR("failed to reserve buffer after flip\n");
498c555f
DA
280
281 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
6f34be50
AD
282 kfree(work);
283}
284
1a0e7918 285void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
6f34be50
AD
286{
287 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
6f34be50
AD
288 unsigned long flags;
289 u32 update_pending;
290 int vpos, hpos;
291
f5d636d2
CK
292 /* can happen during initialization */
293 if (radeon_crtc == NULL)
294 return;
6f34be50
AD
295
296 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
a2b6d3b3
MD
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);
6f34be50
AD
302 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
303 return;
304 }
fa7f517c
CK
305
306 update_pending = radeon_page_flip_pending(rdev, crtc_id);
6f34be50
AD
307
308 /* Has the pageflip already completed in crtc, or is it certain
309 * to complete in this vblank?
310 */
311 if (update_pending &&
abca9e45 312 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
d47abc58 313 &vpos, &hpos, NULL, NULL)) &&
81ffbbed
FK
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.
321 */
322 update_pending = 0;
323 }
fa7f517c
CK
324 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
325 if (!update_pending)
1a0e7918 326 radeon_crtc_handle_flip(rdev, crtc_id);
1a0e7918
CK
327}
328
329/**
330 * radeon_crtc_handle_flip - page flip completed
331 *
332 * @rdev: radeon device pointer
333 * @crtc_id: crtc number this event is for
334 *
335 * Called when we are sure that a page flip for this crtc is completed.
336 */
337void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
338{
339 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
fa7f517c 340 struct radeon_flip_work *work;
1a0e7918
CK
341 unsigned long flags;
342
343 /* this can happen at init */
344 if (radeon_crtc == NULL)
345 return;
346
347 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
fa7f517c 348 work = radeon_crtc->flip_work;
a2b6d3b3
MD
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);
1a0e7918
CK
354 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
355 return;
6f34be50
AD
356 }
357
fa7f517c 358 /* Pageflip completed. Clean up. */
a2b6d3b3 359 radeon_crtc->flip_status = RADEON_FLIP_NONE;
fa7f517c 360 radeon_crtc->flip_work = NULL;
6f34be50
AD
361
362 /* wakeup userspace */
26ae4667
RC
363 if (work->event)
364 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
365
6f34be50
AD
366 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
367
ca721b79 368 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
6f34be50 369 radeon_fence_unref(&work->fence);
46889d95 370 radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
fa7f517c 371 queue_work(radeon_crtc->flip_queue, &work->unpin_work);
6f34be50
AD
372}
373
fa7f517c
CK
374/**
375 * radeon_flip_work_func - page flip framebuffer
376 *
377 * @work - kernel work item
378 *
379 * Wait for the buffer object to become idle and do the actual page flip
380 */
381static void radeon_flip_work_func(struct work_struct *__work)
6f34be50 382{
fa7f517c
CK
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];
6f34be50 387
fa7f517c 388 struct drm_crtc *crtc = &radeon_crtc->base;
fa7f517c
CK
389 unsigned long flags;
390 int r;
9af20792 391
fa7f517c
CK
392 down_read(&rdev->exclusive_lock);
393 while (work->fence) {
394 r = radeon_fence_wait(work->fence, false);
395 if (r == -EDEADLK) {
396 up_read(&rdev->exclusive_lock);
397 r = radeon_gpu_reset(rdev);
398 down_read(&rdev->exclusive_lock);
399 }
6f34be50 400
fa7f517c
CK
401 if (r) {
402 DRM_ERROR("failed to wait on page flip fence (%d)!\n",
403 r);
404 goto cleanup;
405 } else
406 radeon_fence_unref(&work->fence);
6f34be50 407 }
6f34be50 408
c60381bd
MD
409 /* do the flip (mmio) */
410 radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base);
411
412 /* We borrow the event spin lock for protecting flip_status */
413 spin_lock_irqsave(&crtc->dev->event_lock, flags);
414
415 /* set the proper interrupt */
416 radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
417
418 radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
419 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
420 up_read(&rdev->exclusive_lock);
421
422 return;
423
424cleanup:
425 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
426 radeon_fence_unref(&work->fence);
427 kfree(work);
428 up_read(&rdev->exclusive_lock);
429}
430
431static int radeon_crtc_page_flip(struct drm_crtc *crtc,
432 struct drm_framebuffer *fb,
433 struct drm_pending_vblank_event *event,
434 uint32_t page_flip_flags)
435{
436 struct drm_device *dev = crtc->dev;
437 struct radeon_device *rdev = dev->dev_private;
438 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
439 struct radeon_framebuffer *old_radeon_fb;
440 struct radeon_framebuffer *new_radeon_fb;
441 struct drm_gem_object *obj;
442 struct radeon_flip_work *work;
443 struct radeon_bo *new_rbo;
444 uint32_t tiling_flags, pitch_pixels;
445 uint64_t base;
446 unsigned long flags;
447 int r;
448
449 work = kzalloc(sizeof *work, GFP_KERNEL);
450 if (work == NULL)
451 return -ENOMEM;
452
453 INIT_WORK(&work->flip_work, radeon_flip_work_func);
454 INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
455
456 work->rdev = rdev;
457 work->crtc_id = radeon_crtc->crtc_id;
458 work->event = event;
459
460 /* schedule unpin of the old buffer */
461 old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
462 obj = old_radeon_fb->obj;
463
464 /* take a reference to the old object */
465 drm_gem_object_reference(obj);
466 work->old_rbo = gem_to_radeon_bo(obj);
467
468 new_radeon_fb = to_radeon_framebuffer(fb);
469 obj = new_radeon_fb->obj;
470 new_rbo = gem_to_radeon_bo(obj);
471
472 spin_lock(&new_rbo->tbo.bdev->fence_lock);
473 if (new_rbo->tbo.sync_obj)
474 work->fence = radeon_fence_ref(new_rbo->tbo.sync_obj);
475 spin_unlock(&new_rbo->tbo.bdev->fence_lock);
476
6f34be50 477 /* pin the new buffer */
c60381bd
MD
478 DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
479 work->old_rbo, new_rbo);
6f34be50 480
c60381bd 481 r = radeon_bo_reserve(new_rbo, false);
6f34be50
AD
482 if (unlikely(r != 0)) {
483 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
fa7f517c 484 goto cleanup;
6f34be50 485 }
0349af70 486 /* Only 27 bit offset for legacy CRTC */
c60381bd 487 r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
0349af70 488 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
6f34be50 489 if (unlikely(r != 0)) {
c60381bd 490 radeon_bo_unreserve(new_rbo);
6f34be50
AD
491 r = -EINVAL;
492 DRM_ERROR("failed to pin new rbo buffer before flip\n");
fa7f517c 493 goto cleanup;
6f34be50 494 }
c60381bd
MD
495 radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
496 radeon_bo_unreserve(new_rbo);
6f34be50
AD
497
498 if (!ASIC_IS_AVIVO(rdev)) {
499 /* crtc offset is from display base addr not FB location */
500 base -= radeon_crtc->legacy_display_base_addr;
01f2c773 501 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
6f34be50
AD
502
503 if (tiling_flags & RADEON_TILING_MACRO) {
504 if (ASIC_IS_R300(rdev)) {
505 base &= ~0x7ff;
506 } else {
507 int byteshift = fb->bits_per_pixel >> 4;
508 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
509 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
510 }
511 } else {
512 int offset = crtc->y * pitch_pixels + crtc->x;
513 switch (fb->bits_per_pixel) {
514 case 8:
515 default:
516 offset *= 1;
517 break;
518 case 15:
519 case 16:
520 offset *= 2;
521 break;
522 case 24:
523 offset *= 3;
524 break;
525 case 32:
526 offset *= 4;
527 break;
528 }
529 base += offset;
530 }
531 base &= ~7;
532 }
c60381bd 533 work->base = base;
6f34be50 534
ca721b79
MD
535 r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
536 if (r) {
537 DRM_ERROR("failed to get vblank before flip\n");
538 goto pflip_cleanup;
539 }
540
fa7f517c
CK
541 /* We borrow the event spin lock for protecting flip_work */
542 spin_lock_irqsave(&crtc->dev->event_lock, flags);
b15eb4ea 543
c60381bd
MD
544 if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
545 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
546 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
547 r = -EBUSY;
548 goto pflip_cleanup;
549 }
550 radeon_crtc->flip_status = RADEON_FLIP_PENDING;
551 radeon_crtc->flip_work = work;
6f34be50 552
c60381bd
MD
553 /* update crtc fb */
554 crtc->primary->fb = fb;
fa7f517c
CK
555
556 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
fa7f517c 557
c60381bd
MD
558 queue_work(radeon_crtc->flip_queue, &work->flip_work);
559 return 0;
1aab5514 560
ca721b79 561pflip_cleanup:
c60381bd 562 if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
ca721b79
MD
563 DRM_ERROR("failed to reserve new rbo in error path\n");
564 goto cleanup;
565 }
c60381bd 566 if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
ca721b79
MD
567 DRM_ERROR("failed to unpin new rbo in error path\n");
568 }
c60381bd 569 radeon_bo_unreserve(new_rbo);
ca721b79 570
fa7f517c
CK
571cleanup:
572 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
fcc485d6 573 radeon_fence_unref(&work->fence);
6f34be50 574 kfree(work);
fa7f517c 575
c60381bd 576 return r;
6f34be50
AD
577}
578
10ebc0bc
DA
579static int
580radeon_crtc_set_config(struct drm_mode_set *set)
581{
582 struct drm_device *dev;
583 struct radeon_device *rdev;
584 struct drm_crtc *crtc;
585 bool active = false;
586 int ret;
587
588 if (!set || !set->crtc)
589 return -EINVAL;
590
591 dev = set->crtc->dev;
592
593 ret = pm_runtime_get_sync(dev->dev);
594 if (ret < 0)
595 return ret;
596
597 ret = drm_crtc_helper_set_config(set);
598
599 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
600 if (crtc->enabled)
601 active = true;
602
603 pm_runtime_mark_last_busy(dev->dev);
604
605 rdev = dev->dev_private;
606 /* if we have active crtcs and we don't have a power ref,
607 take the current one */
608 if (active && !rdev->have_disp_power_ref) {
609 rdev->have_disp_power_ref = true;
610 return ret;
611 }
612 /* if we have no active crtcs, then drop the power ref
613 we got before */
614 if (!active && rdev->have_disp_power_ref) {
615 pm_runtime_put_autosuspend(dev->dev);
616 rdev->have_disp_power_ref = false;
617 }
618
619 /* drop the power reference we got coming in here */
620 pm_runtime_put_autosuspend(dev->dev);
621 return ret;
622}
771fe6b9
JG
623static const struct drm_crtc_funcs radeon_crtc_funcs = {
624 .cursor_set = radeon_crtc_cursor_set,
625 .cursor_move = radeon_crtc_cursor_move,
626 .gamma_set = radeon_crtc_gamma_set,
10ebc0bc 627 .set_config = radeon_crtc_set_config,
771fe6b9 628 .destroy = radeon_crtc_destroy,
6f34be50 629 .page_flip = radeon_crtc_page_flip,
771fe6b9
JG
630};
631
632static void radeon_crtc_init(struct drm_device *dev, int index)
633{
634 struct radeon_device *rdev = dev->dev_private;
635 struct radeon_crtc *radeon_crtc;
636 int i;
637
638 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
639 if (radeon_crtc == NULL)
640 return;
641
642 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
643
644 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
645 radeon_crtc->crtc_id = index;
fa7f517c 646 radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
c93bb85b 647 rdev->mode_info.crtcs[index] = radeon_crtc;
771fe6b9 648
9e05fa1d
AD
649 if (rdev->family >= CHIP_BONAIRE) {
650 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
651 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
652 } else {
653 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
654 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
655 }
bea61c59
AD
656 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
657 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
9e05fa1d 658
785b93ef 659#if 0
771fe6b9
JG
660 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
661 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
662 radeon_crtc->mode_set.num_connectors = 0;
785b93ef 663#endif
771fe6b9
JG
664
665 for (i = 0; i < 256; i++) {
666 radeon_crtc->lut_r[i] = i << 2;
667 radeon_crtc->lut_g[i] = i << 2;
668 radeon_crtc->lut_b[i] = i << 2;
669 }
670
671 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
672 radeon_atombios_init_crtc(dev, radeon_crtc);
673 else
674 radeon_legacy_init_crtc(dev, radeon_crtc);
675}
676
e68adef8 677static const char *encoder_names[38] = {
771fe6b9
JG
678 "NONE",
679 "INTERNAL_LVDS",
680 "INTERNAL_TMDS1",
681 "INTERNAL_TMDS2",
682 "INTERNAL_DAC1",
683 "INTERNAL_DAC2",
684 "INTERNAL_SDVOA",
685 "INTERNAL_SDVOB",
686 "SI170B",
687 "CH7303",
688 "CH7301",
689 "INTERNAL_DVO1",
690 "EXTERNAL_SDVOA",
691 "EXTERNAL_SDVOB",
692 "TITFP513",
693 "INTERNAL_LVTM1",
694 "VT1623",
695 "HDMI_SI1930",
696 "HDMI_INTERNAL",
697 "INTERNAL_KLDSCP_TMDS1",
698 "INTERNAL_KLDSCP_DVO1",
699 "INTERNAL_KLDSCP_DAC1",
700 "INTERNAL_KLDSCP_DAC2",
701 "SI178",
702 "MVPU_FPGA",
703 "INTERNAL_DDI",
704 "VT1625",
705 "HDMI_SI1932",
706 "DP_AN9801",
707 "DP_DP501",
708 "INTERNAL_UNIPHY",
709 "INTERNAL_KLDSCP_LVTMA",
710 "INTERNAL_UNIPHY1",
711 "INTERNAL_UNIPHY2",
bf982ebf
AD
712 "NUTMEG",
713 "TRAVIS",
e68adef8
AD
714 "INTERNAL_VCE",
715 "INTERNAL_UNIPHY3",
771fe6b9
JG
716};
717
cbd4623d 718static const char *hpd_names[6] = {
eed45b30
AD
719 "HPD1",
720 "HPD2",
721 "HPD3",
722 "HPD4",
723 "HPD5",
724 "HPD6",
725};
726
771fe6b9
JG
727static void radeon_print_display_setup(struct drm_device *dev)
728{
729 struct drm_connector *connector;
730 struct radeon_connector *radeon_connector;
731 struct drm_encoder *encoder;
732 struct radeon_encoder *radeon_encoder;
733 uint32_t devices;
734 int i = 0;
735
736 DRM_INFO("Radeon Display Connectors\n");
737 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
738 radeon_connector = to_radeon_connector(connector);
739 DRM_INFO("Connector %d:\n", i);
72082093 740 DRM_INFO(" %s\n", connector->name);
eed45b30
AD
741 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
742 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
4b9d2a21 743 if (radeon_connector->ddc_bus) {
771fe6b9
JG
744 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
745 radeon_connector->ddc_bus->rec.mask_clk_reg,
746 radeon_connector->ddc_bus->rec.mask_data_reg,
747 radeon_connector->ddc_bus->rec.a_clk_reg,
748 radeon_connector->ddc_bus->rec.a_data_reg,
9b9fe724
AD
749 radeon_connector->ddc_bus->rec.en_clk_reg,
750 radeon_connector->ddc_bus->rec.en_data_reg,
751 radeon_connector->ddc_bus->rec.y_clk_reg,
752 radeon_connector->ddc_bus->rec.y_data_reg);
fb939dfc 753 if (radeon_connector->router.ddc_valid)
26b5bc98 754 DRM_INFO(" DDC Router 0x%x/0x%x\n",
fb939dfc
AD
755 radeon_connector->router.ddc_mux_control_pin,
756 radeon_connector->router.ddc_mux_state);
757 if (radeon_connector->router.cd_valid)
758 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
759 radeon_connector->router.cd_mux_control_pin,
760 radeon_connector->router.cd_mux_state);
4b9d2a21
DA
761 } else {
762 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
763 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
764 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
765 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
766 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
767 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
768 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
769 }
771fe6b9
JG
770 DRM_INFO(" Encoders:\n");
771 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
772 radeon_encoder = to_radeon_encoder(encoder);
773 devices = radeon_encoder->devices & radeon_connector->devices;
774 if (devices) {
775 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
776 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
777 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
778 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
779 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
780 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
781 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
782 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
783 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
784 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
785 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
786 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
787 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
788 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
789 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
790 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
73758a5d
AD
791 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
792 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
771fe6b9
JG
793 if (devices & ATOM_DEVICE_TV1_SUPPORT)
794 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
795 if (devices & ATOM_DEVICE_CV_SUPPORT)
796 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
797 }
798 }
799 i++;
800 }
801}
802
4ce001ab 803static bool radeon_setup_enc_conn(struct drm_device *dev)
771fe6b9
JG
804{
805 struct radeon_device *rdev = dev->dev_private;
771fe6b9
JG
806 bool ret = false;
807
808 if (rdev->bios) {
809 if (rdev->is_atom_bios) {
a084e6ee
AD
810 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
811 if (ret == false)
771fe6b9 812 ret = radeon_get_atom_connector_info_from_object_table(dev);
b9597a1c 813 } else {
771fe6b9 814 ret = radeon_get_legacy_connector_info_from_bios(dev);
b9597a1c
AD
815 if (ret == false)
816 ret = radeon_get_legacy_connector_info_from_table(dev);
817 }
771fe6b9
JG
818 } else {
819 if (!ASIC_IS_AVIVO(rdev))
820 ret = radeon_get_legacy_connector_info_from_table(dev);
821 }
822 if (ret) {
1f3b6a45 823 radeon_setup_encoder_clones(dev);
771fe6b9 824 radeon_print_display_setup(dev);
771fe6b9
JG
825 }
826
827 return ret;
828}
829
830int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
831{
3c537889
AD
832 struct drm_device *dev = radeon_connector->base.dev;
833 struct radeon_device *rdev = dev->dev_private;
771fe6b9
JG
834 int ret = 0;
835
0ac66eff
AD
836 /* don't leak the edid if we already fetched it in detect() */
837 if (radeon_connector->edid)
838 goto got_edid;
839
26b5bc98 840 /* on hw with routers, select right port */
fb939dfc
AD
841 if (radeon_connector->router.ddc_valid)
842 radeon_router_select_ddc_port(radeon_connector);
26b5bc98 843
0a9069d3
NOS
844 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
845 ENCODER_OBJECT_ID_NONE) {
379dfc25 846 if (radeon_connector->ddc_bus->has_aux)
0a9069d3 847 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
379dfc25 848 &radeon_connector->ddc_bus->aux.ddc);
0a9069d3
NOS
849 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
850 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
746c1aa4 851 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
b06947b5 852
7a15cbd4 853 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
379dfc25
AD
854 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
855 radeon_connector->ddc_bus->has_aux)
b06947b5 856 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
379dfc25 857 &radeon_connector->ddc_bus->aux.ddc);
b06947b5
AD
858 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
859 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
860 &radeon_connector->ddc_bus->adapter);
861 } else {
862 if (radeon_connector->ddc_bus && !radeon_connector->edid)
863 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
864 &radeon_connector->ddc_bus->adapter);
0294cf4f 865 }
c324acd5
AD
866
867 if (!radeon_connector->edid) {
868 if (rdev->is_atom_bios) {
869 /* some laptops provide a hardcoded edid in rom for LCDs */
870 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
871 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
872 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
873 } else
874 /* some servers provide a hardcoded edid in rom for KVMs */
875 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
876 }
0294cf4f 877 if (radeon_connector->edid) {
0ac66eff 878got_edid:
0294cf4f
AD
879 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
880 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
16086279 881 drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
771fe6b9
JG
882 return ret;
883 }
884 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
42dea5dd 885 return 0;
771fe6b9
JG
886}
887
f523f74e 888/* avivo */
f523f74e 889
32167016
CK
890/**
891 * avivo_reduce_ratio - fractional number reduction
892 *
893 * @nom: nominator
894 * @den: denominator
895 * @nom_min: minimum value for nominator
896 * @den_min: minimum value for denominator
897 *
898 * Find the greatest common divisor and apply it on both nominator and
899 * denominator, but make nominator and denominator are at least as large
900 * as their minimum values.
901 */
902static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
903 unsigned nom_min, unsigned den_min)
f523f74e 904{
32167016
CK
905 unsigned tmp;
906
907 /* reduce the numbers to a simpler ratio */
908 tmp = gcd(*nom, *den);
909 *nom /= tmp;
910 *den /= tmp;
911
912 /* make sure nominator is large enough */
913 if (*nom < nom_min) {
3b333c55 914 tmp = DIV_ROUND_UP(nom_min, *nom);
32167016
CK
915 *nom *= tmp;
916 *den *= tmp;
f523f74e
AD
917 }
918
32167016
CK
919 /* make sure the denominator is large enough */
920 if (*den < den_min) {
3b333c55 921 tmp = DIV_ROUND_UP(den_min, *den);
32167016
CK
922 *nom *= tmp;
923 *den *= tmp;
f523f74e 924 }
f523f74e
AD
925}
926
c2fb3094
CK
927/**
928 * avivo_get_fb_ref_div - feedback and ref divider calculation
929 *
930 * @nom: nominator
931 * @den: denominator
932 * @post_div: post divider
933 * @fb_div_max: feedback divider maximum
934 * @ref_div_max: reference divider maximum
935 * @fb_div: resulting feedback divider
936 * @ref_div: resulting reference divider
937 *
938 * Calculate feedback and reference divider for a given post divider. Makes
939 * sure we stay within the limits.
940 */
941static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
942 unsigned fb_div_max, unsigned ref_div_max,
943 unsigned *fb_div, unsigned *ref_div)
944{
945 /* limit reference * post divider to a maximum */
4b21ce1b 946 ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
c2fb3094
CK
947
948 /* get matching reference and feedback divider */
949 *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
950 *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
951
952 /* limit fb divider to its maximum */
953 if (*fb_div > fb_div_max) {
954 *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
955 *fb_div = fb_div_max;
956 }
957}
958
32167016
CK
959/**
960 * radeon_compute_pll_avivo - compute PLL paramaters
961 *
962 * @pll: information about the PLL
963 * @dot_clock_p: resulting pixel clock
964 * fb_div_p: resulting feedback divider
965 * frac_fb_div_p: fractional part of the feedback divider
966 * ref_div_p: resulting reference divider
967 * post_div_p: resulting reference divider
968 *
969 * Try to calculate the PLL parameters to generate the given frequency:
970 * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
971 */
f523f74e
AD
972void radeon_compute_pll_avivo(struct radeon_pll *pll,
973 u32 freq,
974 u32 *dot_clock_p,
975 u32 *fb_div_p,
976 u32 *frac_fb_div_p,
977 u32 *ref_div_p,
978 u32 *post_div_p)
979{
c2fb3094
CK
980 unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
981 freq : freq / 10;
982
32167016
CK
983 unsigned fb_div_min, fb_div_max, fb_div;
984 unsigned post_div_min, post_div_max, post_div;
985 unsigned ref_div_min, ref_div_max, ref_div;
986 unsigned post_div_best, diff_best;
f8a2645e 987 unsigned nom, den;
f523f74e 988
32167016
CK
989 /* determine allowed feedback divider range */
990 fb_div_min = pll->min_feedback_div;
991 fb_div_max = pll->max_feedback_div;
f523f74e
AD
992
993 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
32167016
CK
994 fb_div_min *= 10;
995 fb_div_max *= 10;
996 }
997
998 /* determine allowed ref divider range */
999 if (pll->flags & RADEON_PLL_USE_REF_DIV)
1000 ref_div_min = pll->reference_div;
1001 else
1002 ref_div_min = pll->min_ref_div;
24315814
CK
1003
1004 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
1005 pll->flags & RADEON_PLL_USE_REF_DIV)
1006 ref_div_max = pll->reference_div;
1007 else
1008 ref_div_max = pll->max_ref_div;
32167016
CK
1009
1010 /* determine allowed post divider range */
1011 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
1012 post_div_min = pll->post_div;
1013 post_div_max = pll->post_div;
1014 } else {
32167016
CK
1015 unsigned vco_min, vco_max;
1016
1017 if (pll->flags & RADEON_PLL_IS_LCD) {
1018 vco_min = pll->lcd_pll_out_min;
1019 vco_max = pll->lcd_pll_out_max;
1020 } else {
1021 vco_min = pll->pll_out_min;
1022 vco_max = pll->pll_out_max;
f523f74e 1023 }
32167016 1024
c2fb3094
CK
1025 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1026 vco_min *= 10;
1027 vco_max *= 10;
1028 }
1029
32167016
CK
1030 post_div_min = vco_min / target_clock;
1031 if ((target_clock * post_div_min) < vco_min)
1032 ++post_div_min;
1033 if (post_div_min < pll->min_post_div)
1034 post_div_min = pll->min_post_div;
1035
1036 post_div_max = vco_max / target_clock;
1037 if ((target_clock * post_div_max) > vco_max)
1038 --post_div_max;
1039 if (post_div_max > pll->max_post_div)
1040 post_div_max = pll->max_post_div;
1041 }
1042
1043 /* represent the searched ratio as fractional number */
c2fb3094 1044 nom = target_clock;
32167016
CK
1045 den = pll->reference_freq;
1046
1047 /* reduce the numbers to a simpler ratio */
1048 avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
1049
1050 /* now search for a post divider */
1051 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
1052 post_div_best = post_div_min;
1053 else
1054 post_div_best = post_div_max;
1055 diff_best = ~0;
1056
1057 for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
c2fb3094
CK
1058 unsigned diff;
1059 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
1060 ref_div_max, &fb_div, &ref_div);
1061 diff = abs(target_clock - (pll->reference_freq * fb_div) /
1062 (ref_div * post_div));
1063
32167016
CK
1064 if (diff < diff_best || (diff == diff_best &&
1065 !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
1066
1067 post_div_best = post_div;
1068 diff_best = diff;
f523f74e 1069 }
32167016
CK
1070 }
1071 post_div = post_div_best;
1072
c2fb3094
CK
1073 /* get the feedback and reference divider for the optimal value */
1074 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
1075 &fb_div, &ref_div);
32167016
CK
1076
1077 /* reduce the numbers to a simpler ratio once more */
1078 /* this also makes sure that the reference divider is large enough */
1079 avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
1080
3b333c55
CK
1081 /* avoid high jitter with small fractional dividers */
1082 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
74ad54f2 1083 fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
3b333c55
CK
1084 if (fb_div < fb_div_min) {
1085 unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
1086 fb_div *= tmp;
1087 ref_div *= tmp;
1088 }
1089 }
1090
32167016
CK
1091 /* and finally save the result */
1092 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1093 *fb_div_p = fb_div / 10;
1094 *frac_fb_div_p = fb_div % 10;
f523f74e 1095 } else {
32167016
CK
1096 *fb_div_p = fb_div;
1097 *frac_fb_div_p = 0;
f523f74e
AD
1098 }
1099
32167016
CK
1100 *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
1101 (pll->reference_freq * *frac_fb_div_p)) /
1102 (ref_div * post_div * 10);
f523f74e
AD
1103 *ref_div_p = ref_div;
1104 *post_div_p = post_div;
32167016
CK
1105
1106 DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
c2fb3094 1107 freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
32167016 1108 ref_div, post_div);
f523f74e
AD
1109}
1110
1111/* pre-avivo */
771fe6b9
JG
1112static inline uint32_t radeon_div(uint64_t n, uint32_t d)
1113{
1114 uint64_t mod;
1115
1116 n += d / 2;
1117
1118 mod = do_div(n, d);
1119 return n;
1120}
1121
f523f74e
AD
1122void radeon_compute_pll_legacy(struct radeon_pll *pll,
1123 uint64_t freq,
1124 uint32_t *dot_clock_p,
1125 uint32_t *fb_div_p,
1126 uint32_t *frac_fb_div_p,
1127 uint32_t *ref_div_p,
1128 uint32_t *post_div_p)
771fe6b9
JG
1129{
1130 uint32_t min_ref_div = pll->min_ref_div;
1131 uint32_t max_ref_div = pll->max_ref_div;
fc10332b
AD
1132 uint32_t min_post_div = pll->min_post_div;
1133 uint32_t max_post_div = pll->max_post_div;
771fe6b9
JG
1134 uint32_t min_fractional_feed_div = 0;
1135 uint32_t max_fractional_feed_div = 0;
1136 uint32_t best_vco = pll->best_vco;
1137 uint32_t best_post_div = 1;
1138 uint32_t best_ref_div = 1;
1139 uint32_t best_feedback_div = 1;
1140 uint32_t best_frac_feedback_div = 0;
1141 uint32_t best_freq = -1;
1142 uint32_t best_error = 0xffffffff;
1143 uint32_t best_vco_diff = 1;
1144 uint32_t post_div;
86cb2bbf 1145 u32 pll_out_min, pll_out_max;
771fe6b9 1146
d9fdaafb 1147 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
771fe6b9
JG
1148 freq = freq * 1000;
1149
86cb2bbf
AD
1150 if (pll->flags & RADEON_PLL_IS_LCD) {
1151 pll_out_min = pll->lcd_pll_out_min;
1152 pll_out_max = pll->lcd_pll_out_max;
1153 } else {
1154 pll_out_min = pll->pll_out_min;
1155 pll_out_max = pll->pll_out_max;
1156 }
1157
619efb10
AD
1158 if (pll_out_min > 64800)
1159 pll_out_min = 64800;
1160
fc10332b 1161 if (pll->flags & RADEON_PLL_USE_REF_DIV)
771fe6b9
JG
1162 min_ref_div = max_ref_div = pll->reference_div;
1163 else {
1164 while (min_ref_div < max_ref_div-1) {
1165 uint32_t mid = (min_ref_div + max_ref_div) / 2;
1166 uint32_t pll_in = pll->reference_freq / mid;
1167 if (pll_in < pll->pll_in_min)
1168 max_ref_div = mid;
1169 else if (pll_in > pll->pll_in_max)
1170 min_ref_div = mid;
1171 else
1172 break;
1173 }
1174 }
1175
fc10332b
AD
1176 if (pll->flags & RADEON_PLL_USE_POST_DIV)
1177 min_post_div = max_post_div = pll->post_div;
1178
1179 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
771fe6b9
JG
1180 min_fractional_feed_div = pll->min_frac_feedback_div;
1181 max_fractional_feed_div = pll->max_frac_feedback_div;
1182 }
1183
bd6a60af 1184 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
771fe6b9
JG
1185 uint32_t ref_div;
1186
fc10332b 1187 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
771fe6b9
JG
1188 continue;
1189
1190 /* legacy radeons only have a few post_divs */
fc10332b 1191 if (pll->flags & RADEON_PLL_LEGACY) {
771fe6b9
JG
1192 if ((post_div == 5) ||
1193 (post_div == 7) ||
1194 (post_div == 9) ||
1195 (post_div == 10) ||
1196 (post_div == 11) ||
1197 (post_div == 13) ||
1198 (post_div == 14) ||
1199 (post_div == 15))
1200 continue;
1201 }
1202
1203 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1204 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1205 uint32_t pll_in = pll->reference_freq / ref_div;
1206 uint32_t min_feed_div = pll->min_feedback_div;
1207 uint32_t max_feed_div = pll->max_feedback_div + 1;
1208
1209 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1210 continue;
1211
1212 while (min_feed_div < max_feed_div) {
1213 uint32_t vco;
1214 uint32_t min_frac_feed_div = min_fractional_feed_div;
1215 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1216 uint32_t frac_feedback_div;
1217 uint64_t tmp;
1218
1219 feedback_div = (min_feed_div + max_feed_div) / 2;
1220
1221 tmp = (uint64_t)pll->reference_freq * feedback_div;
1222 vco = radeon_div(tmp, ref_div);
1223
86cb2bbf 1224 if (vco < pll_out_min) {
771fe6b9
JG
1225 min_feed_div = feedback_div + 1;
1226 continue;
86cb2bbf 1227 } else if (vco > pll_out_max) {
771fe6b9
JG
1228 max_feed_div = feedback_div;
1229 continue;
1230 }
1231
1232 while (min_frac_feed_div < max_frac_feed_div) {
1233 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1234 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1235 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1236 current_freq = radeon_div(tmp, ref_div * post_div);
1237
fc10332b 1238 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
167ffc44
DC
1239 if (freq < current_freq)
1240 error = 0xffffffff;
1241 else
1242 error = freq - current_freq;
d0e275a9
AD
1243 } else
1244 error = abs(current_freq - freq);
771fe6b9
JG
1245 vco_diff = abs(vco - best_vco);
1246
1247 if ((best_vco == 0 && error < best_error) ||
1248 (best_vco != 0 &&
167ffc44 1249 ((best_error > 100 && error < best_error - 100) ||
5480f727 1250 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
771fe6b9
JG
1251 best_post_div = post_div;
1252 best_ref_div = ref_div;
1253 best_feedback_div = feedback_div;
1254 best_frac_feedback_div = frac_feedback_div;
1255 best_freq = current_freq;
1256 best_error = error;
1257 best_vco_diff = vco_diff;
5480f727
DA
1258 } else if (current_freq == freq) {
1259 if (best_freq == -1) {
1260 best_post_div = post_div;
1261 best_ref_div = ref_div;
1262 best_feedback_div = feedback_div;
1263 best_frac_feedback_div = frac_feedback_div;
1264 best_freq = current_freq;
1265 best_error = error;
1266 best_vco_diff = vco_diff;
1267 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1268 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1269 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1270 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1271 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1272 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1273 best_post_div = post_div;
1274 best_ref_div = ref_div;
1275 best_feedback_div = feedback_div;
1276 best_frac_feedback_div = frac_feedback_div;
1277 best_freq = current_freq;
1278 best_error = error;
1279 best_vco_diff = vco_diff;
1280 }
771fe6b9
JG
1281 }
1282 if (current_freq < freq)
1283 min_frac_feed_div = frac_feedback_div + 1;
1284 else
1285 max_frac_feed_div = frac_feedback_div;
1286 }
1287 if (current_freq < freq)
1288 min_feed_div = feedback_div + 1;
1289 else
1290 max_feed_div = feedback_div;
1291 }
1292 }
1293 }
1294
1295 *dot_clock_p = best_freq / 10000;
1296 *fb_div_p = best_feedback_div;
1297 *frac_fb_div_p = best_frac_feedback_div;
1298 *ref_div_p = best_ref_div;
1299 *post_div_p = best_post_div;
bbb0aef5
JP
1300 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1301 (long long)freq,
1302 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
51d4bf84
AD
1303 best_ref_div, best_post_div);
1304
771fe6b9
JG
1305}
1306
1307static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1308{
1309 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
771fe6b9 1310
29d08b3e 1311 if (radeon_fb->obj) {
bc9025bd 1312 drm_gem_object_unreference_unlocked(radeon_fb->obj);
29d08b3e 1313 }
771fe6b9
JG
1314 drm_framebuffer_cleanup(fb);
1315 kfree(radeon_fb);
1316}
1317
1318static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1319 struct drm_file *file_priv,
1320 unsigned int *handle)
1321{
1322 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1323
1324 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1325}
1326
1327static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1328 .destroy = radeon_user_framebuffer_destroy,
1329 .create_handle = radeon_user_framebuffer_create_handle,
1330};
1331
aaefcd42 1332int
38651674
DA
1333radeon_framebuffer_init(struct drm_device *dev,
1334 struct radeon_framebuffer *rfb,
308e5bcb 1335 struct drm_mode_fb_cmd2 *mode_cmd,
38651674 1336 struct drm_gem_object *obj)
771fe6b9 1337{
aaefcd42 1338 int ret;
38651674 1339 rfb->obj = obj;
c7d73f6a 1340 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
aaefcd42
DA
1341 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1342 if (ret) {
1343 rfb->obj = NULL;
1344 return ret;
1345 }
aaefcd42 1346 return 0;
771fe6b9
JG
1347}
1348
1349static struct drm_framebuffer *
1350radeon_user_framebuffer_create(struct drm_device *dev,
1351 struct drm_file *file_priv,
308e5bcb 1352 struct drm_mode_fb_cmd2 *mode_cmd)
771fe6b9
JG
1353{
1354 struct drm_gem_object *obj;
38651674 1355 struct radeon_framebuffer *radeon_fb;
aaefcd42 1356 int ret;
771fe6b9 1357
308e5bcb 1358 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
7e71c9e2
JG
1359 if (obj == NULL) {
1360 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
308e5bcb 1361 "can't create framebuffer\n", mode_cmd->handles[0]);
cce13ff7 1362 return ERR_PTR(-ENOENT);
7e71c9e2 1363 }
38651674
DA
1364
1365 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
f2d68cf4 1366 if (radeon_fb == NULL) {
1367 drm_gem_object_unreference_unlocked(obj);
cce13ff7 1368 return ERR_PTR(-ENOMEM);
f2d68cf4 1369 }
38651674 1370
aaefcd42
DA
1371 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1372 if (ret) {
1373 kfree(radeon_fb);
1374 drm_gem_object_unreference_unlocked(obj);
b2f4b03f 1375 return ERR_PTR(ret);
aaefcd42 1376 }
38651674
DA
1377
1378 return &radeon_fb->base;
771fe6b9
JG
1379}
1380
eb1f8e4f
DA
1381static void radeon_output_poll_changed(struct drm_device *dev)
1382{
1383 struct radeon_device *rdev = dev->dev_private;
1384 radeon_fb_output_poll_changed(rdev);
1385}
1386
771fe6b9
JG
1387static const struct drm_mode_config_funcs radeon_mode_funcs = {
1388 .fb_create = radeon_user_framebuffer_create,
eb1f8e4f 1389 .output_poll_changed = radeon_output_poll_changed
771fe6b9
JG
1390};
1391
445282db
DA
1392static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1393{ { 0, "driver" },
1394 { 1, "bios" },
1395};
1396
1397static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1398{ { TV_STD_NTSC, "ntsc" },
1399 { TV_STD_PAL, "pal" },
1400 { TV_STD_PAL_M, "pal-m" },
1401 { TV_STD_PAL_60, "pal-60" },
1402 { TV_STD_NTSC_J, "ntsc-j" },
1403 { TV_STD_SCART_PAL, "scart-pal" },
1404 { TV_STD_PAL_CN, "pal-cn" },
1405 { TV_STD_SECAM, "secam" },
1406};
1407
5b1714d3
AD
1408static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1409{ { UNDERSCAN_OFF, "off" },
1410 { UNDERSCAN_ON, "on" },
1411 { UNDERSCAN_AUTO, "auto" },
1412};
1413
8666c076
AD
1414static struct drm_prop_enum_list radeon_audio_enum_list[] =
1415{ { RADEON_AUDIO_DISABLE, "off" },
1416 { RADEON_AUDIO_ENABLE, "on" },
1417 { RADEON_AUDIO_AUTO, "auto" },
1418};
1419
6214bb74
AD
1420/* XXX support different dither options? spatial, temporal, both, etc. */
1421static struct drm_prop_enum_list radeon_dither_enum_list[] =
1422{ { RADEON_FMT_DITHER_DISABLE, "off" },
1423 { RADEON_FMT_DITHER_ENABLE, "on" },
1424};
1425
d79766fa 1426static int radeon_modeset_create_props(struct radeon_device *rdev)
445282db 1427{
4a67d391 1428 int sz;
445282db
DA
1429
1430 if (rdev->is_atom_bios) {
1431 rdev->mode_info.coherent_mode_property =
d9bc3c02 1432 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
445282db
DA
1433 if (!rdev->mode_info.coherent_mode_property)
1434 return -ENOMEM;
445282db
DA
1435 }
1436
1437 if (!ASIC_IS_AVIVO(rdev)) {
1438 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1439 rdev->mode_info.tmds_pll_property =
4a67d391
SH
1440 drm_property_create_enum(rdev->ddev, 0,
1441 "tmds_pll",
1442 radeon_tmds_pll_enum_list, sz);
445282db
DA
1443 }
1444
1445 rdev->mode_info.load_detect_property =
d9bc3c02 1446 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
445282db
DA
1447 if (!rdev->mode_info.load_detect_property)
1448 return -ENOMEM;
445282db
DA
1449
1450 drm_mode_create_scaling_mode_property(rdev->ddev);
1451
1452 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1453 rdev->mode_info.tv_std_property =
4a67d391
SH
1454 drm_property_create_enum(rdev->ddev, 0,
1455 "tv standard",
1456 radeon_tv_std_enum_list, sz);
445282db 1457
5b1714d3
AD
1458 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1459 rdev->mode_info.underscan_property =
4a67d391
SH
1460 drm_property_create_enum(rdev->ddev, 0,
1461 "underscan",
1462 radeon_underscan_enum_list, sz);
5b1714d3 1463
5bccf5e3 1464 rdev->mode_info.underscan_hborder_property =
d9bc3c02
SH
1465 drm_property_create_range(rdev->ddev, 0,
1466 "underscan hborder", 0, 128);
5bccf5e3
MG
1467 if (!rdev->mode_info.underscan_hborder_property)
1468 return -ENOMEM;
5bccf5e3
MG
1469
1470 rdev->mode_info.underscan_vborder_property =
d9bc3c02
SH
1471 drm_property_create_range(rdev->ddev, 0,
1472 "underscan vborder", 0, 128);
5bccf5e3
MG
1473 if (!rdev->mode_info.underscan_vborder_property)
1474 return -ENOMEM;
5bccf5e3 1475
8666c076
AD
1476 sz = ARRAY_SIZE(radeon_audio_enum_list);
1477 rdev->mode_info.audio_property =
1478 drm_property_create_enum(rdev->ddev, 0,
1479 "audio",
1480 radeon_audio_enum_list, sz);
1481
6214bb74
AD
1482 sz = ARRAY_SIZE(radeon_dither_enum_list);
1483 rdev->mode_info.dither_property =
1484 drm_property_create_enum(rdev->ddev, 0,
1485 "dither",
1486 radeon_dither_enum_list, sz);
1487
445282db
DA
1488 return 0;
1489}
1490
f46c0120
AD
1491void radeon_update_display_priority(struct radeon_device *rdev)
1492{
1493 /* adjustment options for the display watermarks */
1494 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1495 /* set display priority to high for r3xx, rv515 chips
1496 * this avoids flickering due to underflow to the
1497 * display controllers during heavy acceleration.
45737447
AD
1498 * Don't force high on rs4xx igp chips as it seems to
1499 * affect the sound card. See kernel bug 15982.
f46c0120 1500 */
45737447
AD
1501 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1502 !(rdev->flags & RADEON_IS_IGP))
f46c0120
AD
1503 rdev->disp_priority = 2;
1504 else
1505 rdev->disp_priority = 0;
1506 } else
1507 rdev->disp_priority = radeon_disp_priority;
1508
1509}
1510
0783986a
AD
1511/*
1512 * Allocate hdmi structs and determine register offsets
1513 */
1514static void radeon_afmt_init(struct radeon_device *rdev)
1515{
1516 int i;
1517
1518 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1519 rdev->mode_info.afmt[i] = NULL;
1520
b530602f
AD
1521 if (ASIC_IS_NODCE(rdev)) {
1522 /* nothing to do */
0783986a 1523 } else if (ASIC_IS_DCE4(rdev)) {
a4d39e68
RM
1524 static uint32_t eg_offsets[] = {
1525 EVERGREEN_CRTC0_REGISTER_OFFSET,
1526 EVERGREEN_CRTC1_REGISTER_OFFSET,
1527 EVERGREEN_CRTC2_REGISTER_OFFSET,
1528 EVERGREEN_CRTC3_REGISTER_OFFSET,
1529 EVERGREEN_CRTC4_REGISTER_OFFSET,
1530 EVERGREEN_CRTC5_REGISTER_OFFSET,
b530602f 1531 0x13830 - 0x7030,
a4d39e68
RM
1532 };
1533 int num_afmt;
1534
b530602f
AD
1535 /* DCE8 has 7 audio blocks tied to DIG encoders */
1536 /* DCE6 has 6 audio blocks tied to DIG encoders */
0783986a
AD
1537 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1538 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
b530602f
AD
1539 if (ASIC_IS_DCE8(rdev))
1540 num_afmt = 7;
1541 else if (ASIC_IS_DCE6(rdev))
1542 num_afmt = 6;
1543 else if (ASIC_IS_DCE5(rdev))
a4d39e68
RM
1544 num_afmt = 6;
1545 else if (ASIC_IS_DCE41(rdev))
1546 num_afmt = 2;
1547 else /* DCE4 */
1548 num_afmt = 6;
1549
1550 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1551 for (i = 0; i < num_afmt; i++) {
1552 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1553 if (rdev->mode_info.afmt[i]) {
1554 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1555 rdev->mode_info.afmt[i]->id = i;
0783986a
AD
1556 }
1557 }
1558 } else if (ASIC_IS_DCE3(rdev)) {
1559 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1560 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1561 if (rdev->mode_info.afmt[0]) {
1562 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1563 rdev->mode_info.afmt[0]->id = 0;
1564 }
1565 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1566 if (rdev->mode_info.afmt[1]) {
1567 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1568 rdev->mode_info.afmt[1]->id = 1;
1569 }
1570 } else if (ASIC_IS_DCE2(rdev)) {
1571 /* DCE2 has at least 1 routable audio block */
1572 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1573 if (rdev->mode_info.afmt[0]) {
1574 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1575 rdev->mode_info.afmt[0]->id = 0;
1576 }
1577 /* r6xx has 2 routable audio blocks */
1578 if (rdev->family >= CHIP_R600) {
1579 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1580 if (rdev->mode_info.afmt[1]) {
1581 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1582 rdev->mode_info.afmt[1]->id = 1;
1583 }
1584 }
1585 }
1586}
1587
1588static void radeon_afmt_fini(struct radeon_device *rdev)
1589{
1590 int i;
1591
1592 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1593 kfree(rdev->mode_info.afmt[i]);
1594 rdev->mode_info.afmt[i] = NULL;
1595 }
1596}
1597
771fe6b9
JG
1598int radeon_modeset_init(struct radeon_device *rdev)
1599{
18917b60 1600 int i;
771fe6b9
JG
1601 int ret;
1602
1603 drm_mode_config_init(rdev->ddev);
1604 rdev->mode_info.mode_config_initialized = true;
1605
e6ecefaa 1606 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
771fe6b9 1607
881dd74e
AD
1608 if (ASIC_IS_DCE5(rdev)) {
1609 rdev->ddev->mode_config.max_width = 16384;
1610 rdev->ddev->mode_config.max_height = 16384;
1611 } else if (ASIC_IS_AVIVO(rdev)) {
771fe6b9
JG
1612 rdev->ddev->mode_config.max_width = 8192;
1613 rdev->ddev->mode_config.max_height = 8192;
1614 } else {
1615 rdev->ddev->mode_config.max_width = 4096;
1616 rdev->ddev->mode_config.max_height = 4096;
1617 }
1618
019d96cb
DA
1619 rdev->ddev->mode_config.preferred_depth = 24;
1620 rdev->ddev->mode_config.prefer_shadow = 1;
1621
771fe6b9
JG
1622 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1623
445282db
DA
1624 ret = radeon_modeset_create_props(rdev);
1625 if (ret) {
1626 return ret;
1627 }
dfee5614 1628
f376b94f
AD
1629 /* init i2c buses */
1630 radeon_i2c_init(rdev);
1631
3c537889
AD
1632 /* check combios for a valid hardcoded EDID - Sun servers */
1633 if (!rdev->is_atom_bios) {
1634 /* check for hardcoded EDID in BIOS */
1635 radeon_combios_check_hardcoded_edid(rdev);
1636 }
1637
dfee5614 1638 /* allocate crtcs */
18917b60 1639 for (i = 0; i < rdev->num_crtc; i++) {
771fe6b9
JG
1640 radeon_crtc_init(rdev->ddev, i);
1641 }
1642
1643 /* okay we should have all the bios connectors */
1644 ret = radeon_setup_enc_conn(rdev->ddev);
1645 if (!ret) {
1646 return ret;
1647 }
ac89af1e 1648
3fa47d9e
AD
1649 /* init dig PHYs, disp eng pll */
1650 if (rdev->is_atom_bios) {
ac89af1e 1651 radeon_atom_encoder_init(rdev);
f3f1f03e 1652 radeon_atom_disp_eng_pll_init(rdev);
3fa47d9e 1653 }
ac89af1e 1654
d4877cf2
AD
1655 /* initialize hpd */
1656 radeon_hpd_init(rdev);
38651674 1657
0783986a
AD
1658 /* setup afmt */
1659 radeon_afmt_init(rdev);
1660
38651674 1661 radeon_fbdev_init(rdev);
eb1f8e4f
DA
1662 drm_kms_helper_poll_init(rdev->ddev);
1663
6c7bccea
AD
1664 if (rdev->pm.dpm_enabled) {
1665 /* do dpm late init */
1666 ret = radeon_pm_late_init(rdev);
1667 if (ret) {
1668 rdev->pm.dpm_enabled = false;
1669 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1670 }
1671 /* set the dpm state for PX since there won't be
1672 * a modeset to call this.
1673 */
1674 radeon_pm_compute_clocks(rdev);
1675 }
1676
771fe6b9
JG
1677 return 0;
1678}
1679
1680void radeon_modeset_fini(struct radeon_device *rdev)
1681{
38651674 1682 radeon_fbdev_fini(rdev);
3c537889
AD
1683 kfree(rdev->mode_info.bios_hardcoded_edid);
1684
771fe6b9 1685 if (rdev->mode_info.mode_config_initialized) {
0783986a 1686 radeon_afmt_fini(rdev);
eb1f8e4f 1687 drm_kms_helper_poll_fini(rdev->ddev);
d4877cf2 1688 radeon_hpd_fini(rdev);
771fe6b9
JG
1689 drm_mode_config_cleanup(rdev->ddev);
1690 rdev->mode_info.mode_config_initialized = false;
1691 }
f376b94f
AD
1692 /* free i2c buses */
1693 radeon_i2c_fini(rdev);
771fe6b9
JG
1694}
1695
e811f5ae 1696static bool is_hdtv_mode(const struct drm_display_mode *mode)
039ed2d9
AD
1697{
1698 /* try and guess if this is a tv or a monitor */
1699 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1700 (mode->vdisplay == 576) || /* 576p */
1701 (mode->vdisplay == 720) || /* 720p */
1702 (mode->vdisplay == 1080)) /* 1080p */
1703 return true;
1704 else
1705 return false;
1706}
1707
c93bb85b 1708bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
e811f5ae 1709 const struct drm_display_mode *mode,
c93bb85b 1710 struct drm_display_mode *adjusted_mode)
771fe6b9 1711{
c93bb85b 1712 struct drm_device *dev = crtc->dev;
5b1714d3 1713 struct radeon_device *rdev = dev->dev_private;
c93bb85b
JG
1714 struct drm_encoder *encoder;
1715 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1716 struct radeon_encoder *radeon_encoder;
5b1714d3
AD
1717 struct drm_connector *connector;
1718 struct radeon_connector *radeon_connector;
c93bb85b 1719 bool first = true;
d65d65b1
AD
1720 u32 src_v = 1, dst_v = 1;
1721 u32 src_h = 1, dst_h = 1;
771fe6b9 1722
5b1714d3
AD
1723 radeon_crtc->h_border = 0;
1724 radeon_crtc->v_border = 0;
1725
c93bb85b 1726 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
c93bb85b
JG
1727 if (encoder->crtc != crtc)
1728 continue;
d65d65b1 1729 radeon_encoder = to_radeon_encoder(encoder);
5b1714d3
AD
1730 connector = radeon_get_connector_for_encoder(encoder);
1731 radeon_connector = to_radeon_connector(connector);
1732
c93bb85b 1733 if (first) {
80297e87
AD
1734 /* set scaling */
1735 if (radeon_encoder->rmx_type == RMX_OFF)
1736 radeon_crtc->rmx_type = RMX_OFF;
1737 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1738 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1739 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1740 else
1741 radeon_crtc->rmx_type = RMX_OFF;
1742 /* copy native mode */
c93bb85b 1743 memcpy(&radeon_crtc->native_mode,
80297e87 1744 &radeon_encoder->native_mode,
de2103e4 1745 sizeof(struct drm_display_mode));
ff32a59d
AD
1746 src_v = crtc->mode.vdisplay;
1747 dst_v = radeon_crtc->native_mode.vdisplay;
1748 src_h = crtc->mode.hdisplay;
1749 dst_h = radeon_crtc->native_mode.hdisplay;
5b1714d3
AD
1750
1751 /* fix up for overscan on hdmi */
1752 if (ASIC_IS_AVIVO(rdev) &&
e6db0da0 1753 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
5b1714d3
AD
1754 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1755 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
039ed2d9
AD
1756 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1757 is_hdtv_mode(mode)))) {
5bccf5e3
MG
1758 if (radeon_encoder->underscan_hborder != 0)
1759 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1760 else
1761 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1762 if (radeon_encoder->underscan_vborder != 0)
1763 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1764 else
1765 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
5b1714d3
AD
1766 radeon_crtc->rmx_type = RMX_FULL;
1767 src_v = crtc->mode.vdisplay;
1768 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1769 src_h = crtc->mode.hdisplay;
1770 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1771 }
c93bb85b
JG
1772 first = false;
1773 } else {
1774 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1775 /* WARNING: Right now this can't happen but
1776 * in the future we need to check that scaling
d65d65b1 1777 * are consistent across different encoder
c93bb85b
JG
1778 * (ie all encoder can work with the same
1779 * scaling).
1780 */
d65d65b1 1781 DRM_ERROR("Scaling not consistent across encoder.\n");
c93bb85b
JG
1782 return false;
1783 }
771fe6b9
JG
1784 }
1785 }
c93bb85b
JG
1786 if (radeon_crtc->rmx_type != RMX_OFF) {
1787 fixed20_12 a, b;
d65d65b1
AD
1788 a.full = dfixed_const(src_v);
1789 b.full = dfixed_const(dst_v);
68adac5e 1790 radeon_crtc->vsc.full = dfixed_div(a, b);
d65d65b1
AD
1791 a.full = dfixed_const(src_h);
1792 b.full = dfixed_const(dst_h);
68adac5e 1793 radeon_crtc->hsc.full = dfixed_div(a, b);
771fe6b9 1794 } else {
68adac5e
BS
1795 radeon_crtc->vsc.full = dfixed_const(1);
1796 radeon_crtc->hsc.full = dfixed_const(1);
771fe6b9 1797 }
c93bb85b 1798 return true;
771fe6b9 1799}
6383cf7d
MK
1800
1801/*
d47abc58
MK
1802 * Retrieve current video scanout position of crtc on a given gpu, and
1803 * an optional accurate timestamp of when query happened.
6383cf7d 1804 *
f5a80209 1805 * \param dev Device to query.
6383cf7d 1806 * \param crtc Crtc to query.
abca9e45 1807 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
6383cf7d
MK
1808 * \param *vpos Location where vertical scanout position should be stored.
1809 * \param *hpos Location where horizontal scanout position should go.
d47abc58
MK
1810 * \param *stime Target location for timestamp taken immediately before
1811 * scanout position query. Can be NULL to skip timestamp.
1812 * \param *etime Target location for timestamp taken immediately after
1813 * scanout position query. Can be NULL to skip timestamp.
6383cf7d
MK
1814 *
1815 * Returns vpos as a positive number while in active scanout area.
1816 * Returns vpos as a negative number inside vblank, counting the number
1817 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1818 * until start of active scanout / end of vblank."
1819 *
1820 * \return Flags, or'ed together as follows:
1821 *
25985edc 1822 * DRM_SCANOUTPOS_VALID = Query successful.
f5a80209
MK
1823 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1824 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
6383cf7d
MK
1825 * this flag means that returned position may be offset by a constant but
1826 * unknown small number of scanlines wrt. real scanout position.
1827 *
1828 */
abca9e45
VS
1829int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1830 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
6383cf7d
MK
1831{
1832 u32 stat_crtc = 0, vbl = 0, position = 0;
1833 int vbl_start, vbl_end, vtotal, ret = 0;
1834 bool in_vbl = true;
1835
f5a80209
MK
1836 struct radeon_device *rdev = dev->dev_private;
1837
d47abc58
MK
1838 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1839
1840 /* Get optional system timestamp before query. */
1841 if (stime)
1842 *stime = ktime_get();
1843
6383cf7d
MK
1844 if (ASIC_IS_DCE4(rdev)) {
1845 if (crtc == 0) {
1846 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1847 EVERGREEN_CRTC0_REGISTER_OFFSET);
1848 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1849 EVERGREEN_CRTC0_REGISTER_OFFSET);
f5a80209 1850 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1851 }
1852 if (crtc == 1) {
1853 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1854 EVERGREEN_CRTC1_REGISTER_OFFSET);
1855 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1856 EVERGREEN_CRTC1_REGISTER_OFFSET);
f5a80209 1857 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1858 }
1859 if (crtc == 2) {
1860 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1861 EVERGREEN_CRTC2_REGISTER_OFFSET);
1862 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1863 EVERGREEN_CRTC2_REGISTER_OFFSET);
f5a80209 1864 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1865 }
1866 if (crtc == 3) {
1867 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1868 EVERGREEN_CRTC3_REGISTER_OFFSET);
1869 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1870 EVERGREEN_CRTC3_REGISTER_OFFSET);
f5a80209 1871 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1872 }
1873 if (crtc == 4) {
1874 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1875 EVERGREEN_CRTC4_REGISTER_OFFSET);
1876 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1877 EVERGREEN_CRTC4_REGISTER_OFFSET);
f5a80209 1878 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1879 }
1880 if (crtc == 5) {
1881 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1882 EVERGREEN_CRTC5_REGISTER_OFFSET);
1883 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1884 EVERGREEN_CRTC5_REGISTER_OFFSET);
f5a80209 1885 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1886 }
1887 } else if (ASIC_IS_AVIVO(rdev)) {
1888 if (crtc == 0) {
1889 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1890 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
f5a80209 1891 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1892 }
1893 if (crtc == 1) {
1894 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1895 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
f5a80209 1896 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1897 }
1898 } else {
1899 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1900 if (crtc == 0) {
1901 /* Assume vbl_end == 0, get vbl_start from
1902 * upper 16 bits.
1903 */
1904 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1905 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1906 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1907 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1908 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1909 if (!(stat_crtc & 1))
1910 in_vbl = false;
1911
f5a80209 1912 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1913 }
1914 if (crtc == 1) {
1915 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1916 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1917 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1918 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1919 if (!(stat_crtc & 1))
1920 in_vbl = false;
1921
f5a80209 1922 ret |= DRM_SCANOUTPOS_VALID;
6383cf7d
MK
1923 }
1924 }
1925
d47abc58
MK
1926 /* Get optional system timestamp after query. */
1927 if (etime)
1928 *etime = ktime_get();
1929
1930 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1931
6383cf7d
MK
1932 /* Decode into vertical and horizontal scanout position. */
1933 *vpos = position & 0x1fff;
1934 *hpos = (position >> 16) & 0x1fff;
1935
1936 /* Valid vblank area boundaries from gpu retrieved? */
1937 if (vbl > 0) {
1938 /* Yes: Decode. */
f5a80209 1939 ret |= DRM_SCANOUTPOS_ACCURATE;
6383cf7d
MK
1940 vbl_start = vbl & 0x1fff;
1941 vbl_end = (vbl >> 16) & 0x1fff;
1942 }
1943 else {
1944 /* No: Fake something reasonable which gives at least ok results. */
f5a80209 1945 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
6383cf7d
MK
1946 vbl_end = 0;
1947 }
1948
1949 /* Test scanout position against vblank region. */
1950 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1951 in_vbl = false;
1952
1953 /* Check if inside vblank area and apply corrective offsets:
1954 * vpos will then be >=0 in video scanout area, but negative
1955 * within vblank area, counting down the number of lines until
1956 * start of scanout.
1957 */
1958
1959 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1960 if (in_vbl && (*vpos >= vbl_start)) {
f5a80209 1961 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
6383cf7d
MK
1962 *vpos = *vpos - vtotal;
1963 }
1964
1965 /* Correct for shifted end of vbl at vbl_end. */
1966 *vpos = *vpos - vbl_end;
1967
1968 /* In vblank? */
1969 if (in_vbl)
f5a80209 1970 ret |= DRM_SCANOUTPOS_INVBL;
6383cf7d 1971
8072bfa6
VS
1972 /* Is vpos outside nominal vblank area, but less than
1973 * 1/100 of a frame height away from start of vblank?
1974 * If so, assume this isn't a massively delayed vblank
1975 * interrupt, but a vblank interrupt that fired a few
1976 * microseconds before true start of vblank. Compensate
1977 * by adding a full frame duration to the final timestamp.
1978 * Happens, e.g., on ATI R500, R600.
1979 *
1980 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1981 */
1982 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1983 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1984 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1985
1986 if (vbl_start - *vpos < vtotal / 100) {
1987 *vpos -= vtotal;
1988
1989 /* Signal this correction as "applied". */
1990 ret |= 0x8;
1991 }
1992 }
1993
6383cf7d
MK
1994 return ret;
1995}
This page took 0.381149 seconds and 5 git commands to generate.