blob: fbd8b930f2bec75276a5baf781653e658a023eaa [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
27#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028#include "radeon.h"
29
30#include "atom.h"
31#include <asm/div64.h>
32
Dave Airlie10ebc0b2012-09-17 14:40:31 +100033#include <linux/pm_runtime.h>
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drm_crtc_helper.h>
35#include <drm/drm_edid.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020036
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037static void avivo_crtc_load_lut(struct drm_crtc *crtc)
38{
39 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
40 struct drm_device *dev = crtc->dev;
41 struct radeon_device *rdev = dev->dev_private;
42 int i;
43
Dave Airlied9fdaaf2010-08-02 10:42:55 +100044 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020045 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
46
47 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
48 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
50
51 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
52 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
54
55 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
56 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
57 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
58
59 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
60 for (i = 0; i < 256; i++) {
61 WREG32(AVIVO_DC_LUT_30_COLOR,
62 (radeon_crtc->lut_r[i] << 20) |
63 (radeon_crtc->lut_g[i] << 10) |
64 (radeon_crtc->lut_b[i] << 0));
65 }
66
67 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
68}
69
Alex Deucherfee298f2011-01-06 21:19:30 -050070static void dce4_crtc_load_lut(struct drm_crtc *crtc)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050071{
72 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
73 struct drm_device *dev = crtc->dev;
74 struct radeon_device *rdev = dev->dev_private;
75 int i;
76
Dave Airlied9fdaaf2010-08-02 10:42:55 +100077 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050078 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
79
80 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
81 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
83
84 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
85 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
87
Alex Deucher677d0762010-04-22 22:58:50 -040088 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
89 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050090
Alex Deucher677d0762010-04-22 22:58:50 -040091 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050092 for (i = 0; i < 256; i++) {
Alex Deucher677d0762010-04-22 22:58:50 -040093 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050094 (radeon_crtc->lut_r[i] << 20) |
95 (radeon_crtc->lut_g[i] << 10) |
96 (radeon_crtc->lut_b[i] << 0));
97 }
98}
99
Alex Deucherfee298f2011-01-06 21:19:30 -0500100static void dce5_crtc_load_lut(struct drm_crtc *crtc)
101{
102 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
103 struct drm_device *dev = crtc->dev;
104 struct radeon_device *rdev = dev->dev_private;
105 int i;
106
107 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
108
109 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
110 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
111 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
112 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
113 NI_GRPH_PRESCALE_BYPASS);
114 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
115 NI_OVL_PRESCALE_BYPASS);
116 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
117 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
118 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
119
120 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
121
122 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
123 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
124 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
125
126 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
127 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
128 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
129
130 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
131 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
132
133 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
134 for (i = 0; i < 256; i++) {
135 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
136 (radeon_crtc->lut_r[i] << 20) |
137 (radeon_crtc->lut_g[i] << 10) |
138 (radeon_crtc->lut_b[i] << 0));
139 }
140
141 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
142 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
143 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
144 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
145 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
146 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
147 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
148 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
149 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
150 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
151 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
152 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
153 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
154 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
155 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
156 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
Alex Deucher9e05fa12013-01-24 10:06:33 -0500157 if (ASIC_IS_DCE8(rdev)) {
158 /* XXX this only needs to be programmed once per crtc at startup,
159 * not sure where the best place for it is
160 */
161 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
162 CIK_CURSOR_ALPHA_BLND_ENA);
163 }
Alex Deucherfee298f2011-01-06 21:19:30 -0500164}
165
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166static void legacy_crtc_load_lut(struct drm_crtc *crtc)
167{
168 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
169 struct drm_device *dev = crtc->dev;
170 struct radeon_device *rdev = dev->dev_private;
171 int i;
172 uint32_t dac2_cntl;
173
174 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
175 if (radeon_crtc->crtc_id == 0)
176 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
177 else
178 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
179 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
180
181 WREG8(RADEON_PALETTE_INDEX, 0);
182 for (i = 0; i < 256; i++) {
183 WREG32(RADEON_PALETTE_30_DATA,
184 (radeon_crtc->lut_r[i] << 20) |
185 (radeon_crtc->lut_g[i] << 10) |
186 (radeon_crtc->lut_b[i] << 0));
187 }
188}
189
190void radeon_crtc_load_lut(struct drm_crtc *crtc)
191{
192 struct drm_device *dev = crtc->dev;
193 struct radeon_device *rdev = dev->dev_private;
194
195 if (!crtc->enabled)
196 return;
197
Alex Deucherfee298f2011-01-06 21:19:30 -0500198 if (ASIC_IS_DCE5(rdev))
199 dce5_crtc_load_lut(crtc);
200 else if (ASIC_IS_DCE4(rdev))
201 dce4_crtc_load_lut(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500202 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200203 avivo_crtc_load_lut(crtc);
204 else
205 legacy_crtc_load_lut(crtc);
206}
207
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000208/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
210 u16 blue, int regno)
211{
212 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
213
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200214 radeon_crtc->lut_r[regno] = red >> 6;
215 radeon_crtc->lut_g[regno] = green >> 6;
216 radeon_crtc->lut_b[regno] = blue >> 6;
217}
218
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000219/** Gets the color ramps on behalf of fbcon */
220void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
221 u16 *blue, int regno)
222{
223 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
224
225 *red = radeon_crtc->lut_r[regno] << 6;
226 *green = radeon_crtc->lut_g[regno] << 6;
227 *blue = radeon_crtc->lut_b[regno] << 6;
228}
229
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100231 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200232{
233 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100234 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200235
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000236 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100237 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000238 radeon_crtc->lut_r[i] = red[i] >> 6;
239 radeon_crtc->lut_g[i] = green[i] >> 6;
240 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200241 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200242 radeon_crtc_load_lut(crtc);
243}
244
245static void radeon_crtc_destroy(struct drm_crtc *crtc)
246{
247 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
248
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200249 drm_crtc_cleanup(crtc);
250 kfree(radeon_crtc);
251}
252
Alex Deucher6f34be52010-11-21 10:59:01 -0500253/*
254 * Handle unpin events outside the interrupt handler proper.
255 */
256static void radeon_unpin_work_func(struct work_struct *__work)
257{
258 struct radeon_unpin_work *work =
259 container_of(__work, struct radeon_unpin_work, work);
260 int r;
261
262 /* unpin of the old buffer */
263 r = radeon_bo_reserve(work->old_rbo, false);
264 if (likely(r == 0)) {
265 r = radeon_bo_unpin(work->old_rbo);
266 if (unlikely(r != 0)) {
267 DRM_ERROR("failed to unpin buffer after flip\n");
268 }
269 radeon_bo_unreserve(work->old_rbo);
270 } else
271 DRM_ERROR("failed to reserve buffer after flip\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000272
273 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500274 kfree(work);
275}
276
277void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
278{
279 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
280 struct radeon_unpin_work *work;
Alex Deucher6f34be52010-11-21 10:59:01 -0500281 unsigned long flags;
282 u32 update_pending;
283 int vpos, hpos;
284
285 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
286 work = radeon_crtc->unpin_work;
287 if (work == NULL ||
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000288 (work->fence && !radeon_fence_signaled(work->fence))) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500289 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
290 return;
291 }
292 /* New pageflip, or just completion of a previous one? */
293 if (!radeon_crtc->deferred_flip_completion) {
294 /* do the flip (mmio) */
295 update_pending = radeon_page_flip(rdev, crtc_id, work->new_crtc_base);
296 } else {
297 /* This is just a completion of a flip queued in crtc
298 * at last invocation. Make sure we go directly to
299 * completion routine.
300 */
301 update_pending = 0;
302 radeon_crtc->deferred_flip_completion = 0;
303 }
304
305 /* Has the pageflip already completed in crtc, or is it certain
306 * to complete in this vblank?
307 */
308 if (update_pending &&
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200309 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
Mario Kleinerd47abc52013-10-30 05:13:07 +0100310 &vpos, &hpos, NULL, NULL)) &&
Felix Kuehling81ffbbe2012-02-23 19:16:12 -0500311 ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
312 (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
313 /* crtc didn't flip in this target vblank interval,
314 * but flip is pending in crtc. Based on the current
315 * scanout position we know that the current frame is
316 * (nearly) complete and the flip will (likely)
317 * complete before the start of the next frame.
318 */
319 update_pending = 0;
320 }
321 if (update_pending) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500322 /* crtc didn't flip in this target vblank interval,
323 * but flip is pending in crtc. It will complete it
324 * in next vblank interval, so complete the flip at
325 * next vblank irq.
326 */
327 radeon_crtc->deferred_flip_completion = 1;
328 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
329 return;
330 }
331
332 /* Pageflip (will be) certainly completed in this vblank. Clean up. */
333 radeon_crtc->unpin_work = NULL;
334
335 /* wakeup userspace */
Rob Clark26ae4662012-10-08 19:50:42 +0000336 if (work->event)
337 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
338
Alex Deucher6f34be52010-11-21 10:59:01 -0500339 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
340
341 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
342 radeon_fence_unref(&work->fence);
343 radeon_post_page_flip(work->rdev, work->crtc_id);
344 schedule_work(&work->work);
345}
346
347static int radeon_crtc_page_flip(struct drm_crtc *crtc,
348 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700349 struct drm_pending_vblank_event *event,
350 uint32_t page_flip_flags)
Alex Deucher6f34be52010-11-21 10:59:01 -0500351{
352 struct drm_device *dev = crtc->dev;
353 struct radeon_device *rdev = dev->dev_private;
354 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
355 struct radeon_framebuffer *old_radeon_fb;
356 struct radeon_framebuffer *new_radeon_fb;
357 struct drm_gem_object *obj;
358 struct radeon_bo *rbo;
Alex Deucher6f34be52010-11-21 10:59:01 -0500359 struct radeon_unpin_work *work;
360 unsigned long flags;
361 u32 tiling_flags, pitch_pixels;
362 u64 base;
363 int r;
364
365 work = kzalloc(sizeof *work, GFP_KERNEL);
366 if (work == NULL)
367 return -ENOMEM;
368
Alex Deucher6f34be52010-11-21 10:59:01 -0500369 work->event = event;
370 work->rdev = rdev;
371 work->crtc_id = radeon_crtc->crtc_id;
Alex Deucher6f34be52010-11-21 10:59:01 -0500372 old_radeon_fb = to_radeon_framebuffer(crtc->fb);
373 new_radeon_fb = to_radeon_framebuffer(fb);
374 /* schedule unpin of the old buffer */
375 obj = old_radeon_fb->obj;
Dave Airlie498c5552011-05-29 17:48:32 +1000376 /* take a reference to the old object */
377 drm_gem_object_reference(obj);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100378 rbo = gem_to_radeon_bo(obj);
Alex Deucher6f34be52010-11-21 10:59:01 -0500379 work->old_rbo = rbo;
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000380 obj = new_radeon_fb->obj;
381 rbo = gem_to_radeon_bo(obj);
Daniel Vetter9af20792012-12-11 23:42:24 +0100382
383 spin_lock(&rbo->tbo.bdev->fence_lock);
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000384 if (rbo->tbo.sync_obj)
385 work->fence = radeon_fence_ref(rbo->tbo.sync_obj);
Daniel Vetter9af20792012-12-11 23:42:24 +0100386 spin_unlock(&rbo->tbo.bdev->fence_lock);
387
Alex Deucher6f34be52010-11-21 10:59:01 -0500388 INIT_WORK(&work->work, radeon_unpin_work_func);
389
390 /* We borrow the event spin lock for protecting unpin_work */
391 spin_lock_irqsave(&dev->event_lock, flags);
392 if (radeon_crtc->unpin_work) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500393 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000394 r = -EBUSY;
395 goto unlock_free;
Alex Deucher6f34be52010-11-21 10:59:01 -0500396 }
397 radeon_crtc->unpin_work = work;
398 radeon_crtc->deferred_flip_completion = 0;
399 spin_unlock_irqrestore(&dev->event_lock, flags);
400
401 /* pin the new buffer */
Alex Deucher6f34be52010-11-21 10:59:01 -0500402 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
403 work->old_rbo, rbo);
404
405 r = radeon_bo_reserve(rbo, false);
406 if (unlikely(r != 0)) {
407 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
408 goto pflip_cleanup;
409 }
Michel Dänzer0349af72012-03-14 17:12:42 +0100410 /* Only 27 bit offset for legacy CRTC */
411 r = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
412 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500413 if (unlikely(r != 0)) {
414 radeon_bo_unreserve(rbo);
415 r = -EINVAL;
416 DRM_ERROR("failed to pin new rbo buffer before flip\n");
417 goto pflip_cleanup;
418 }
419 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
420 radeon_bo_unreserve(rbo);
421
422 if (!ASIC_IS_AVIVO(rdev)) {
423 /* crtc offset is from display base addr not FB location */
424 base -= radeon_crtc->legacy_display_base_addr;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200425 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
Alex Deucher6f34be52010-11-21 10:59:01 -0500426
427 if (tiling_flags & RADEON_TILING_MACRO) {
428 if (ASIC_IS_R300(rdev)) {
429 base &= ~0x7ff;
430 } else {
431 int byteshift = fb->bits_per_pixel >> 4;
432 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
433 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
434 }
435 } else {
436 int offset = crtc->y * pitch_pixels + crtc->x;
437 switch (fb->bits_per_pixel) {
438 case 8:
439 default:
440 offset *= 1;
441 break;
442 case 15:
443 case 16:
444 offset *= 2;
445 break;
446 case 24:
447 offset *= 3;
448 break;
449 case 32:
450 offset *= 4;
451 break;
452 }
453 base += offset;
454 }
455 base &= ~7;
456 }
457
458 spin_lock_irqsave(&dev->event_lock, flags);
459 work->new_crtc_base = base;
460 spin_unlock_irqrestore(&dev->event_lock, flags);
461
462 /* update crtc fb */
463 crtc->fb = fb;
464
465 r = drm_vblank_get(dev, radeon_crtc->crtc_id);
466 if (r) {
467 DRM_ERROR("failed to get vblank before flip\n");
468 goto pflip_cleanup1;
469 }
470
Alex Deucher6f34be52010-11-21 10:59:01 -0500471 /* set the proper interrupt */
472 radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500473
474 return 0;
475
Alex Deucher6f34be52010-11-21 10:59:01 -0500476pflip_cleanup1:
Michel Dänzerd0254d52011-07-13 15:18:10 +0000477 if (unlikely(radeon_bo_reserve(rbo, false) != 0)) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500478 DRM_ERROR("failed to reserve new rbo in error path\n");
479 goto pflip_cleanup;
480 }
Michel Dänzerd0254d52011-07-13 15:18:10 +0000481 if (unlikely(radeon_bo_unpin(rbo) != 0)) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500482 DRM_ERROR("failed to unpin new rbo in error path\n");
Alex Deucher6f34be52010-11-21 10:59:01 -0500483 }
484 radeon_bo_unreserve(rbo);
485
486pflip_cleanup:
487 spin_lock_irqsave(&dev->event_lock, flags);
488 radeon_crtc->unpin_work = NULL;
Dave Airlie498c5552011-05-29 17:48:32 +1000489unlock_free:
Alex Deucher6f34be52010-11-21 10:59:01 -0500490 spin_unlock_irqrestore(&dev->event_lock, flags);
Michel Dänzerdb318d72011-09-13 11:29:12 +0200491 drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000492 radeon_fence_unref(&work->fence);
Alex Deucher6f34be52010-11-21 10:59:01 -0500493 kfree(work);
494
495 return r;
496}
497
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000498static int
499radeon_crtc_set_config(struct drm_mode_set *set)
500{
501 struct drm_device *dev;
502 struct radeon_device *rdev;
503 struct drm_crtc *crtc;
504 bool active = false;
505 int ret;
506
507 if (!set || !set->crtc)
508 return -EINVAL;
509
510 dev = set->crtc->dev;
511
512 ret = pm_runtime_get_sync(dev->dev);
513 if (ret < 0)
514 return ret;
515
516 ret = drm_crtc_helper_set_config(set);
517
518 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
519 if (crtc->enabled)
520 active = true;
521
522 pm_runtime_mark_last_busy(dev->dev);
523
524 rdev = dev->dev_private;
525 /* if we have active crtcs and we don't have a power ref,
526 take the current one */
527 if (active && !rdev->have_disp_power_ref) {
528 rdev->have_disp_power_ref = true;
529 return ret;
530 }
531 /* if we have no active crtcs, then drop the power ref
532 we got before */
533 if (!active && rdev->have_disp_power_ref) {
534 pm_runtime_put_autosuspend(dev->dev);
535 rdev->have_disp_power_ref = false;
536 }
537
538 /* drop the power reference we got coming in here */
539 pm_runtime_put_autosuspend(dev->dev);
540 return ret;
541}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200542static const struct drm_crtc_funcs radeon_crtc_funcs = {
543 .cursor_set = radeon_crtc_cursor_set,
544 .cursor_move = radeon_crtc_cursor_move,
545 .gamma_set = radeon_crtc_gamma_set,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000546 .set_config = radeon_crtc_set_config,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200547 .destroy = radeon_crtc_destroy,
Alex Deucher6f34be52010-11-21 10:59:01 -0500548 .page_flip = radeon_crtc_page_flip,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549};
550
551static void radeon_crtc_init(struct drm_device *dev, int index)
552{
553 struct radeon_device *rdev = dev->dev_private;
554 struct radeon_crtc *radeon_crtc;
555 int i;
556
557 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
558 if (radeon_crtc == NULL)
559 return;
560
561 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
562
563 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
564 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200565 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200566
Alex Deucher9e05fa12013-01-24 10:06:33 -0500567 if (rdev->family >= CHIP_BONAIRE) {
568 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
569 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
570 } else {
571 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
572 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
573 }
Alex Deucherbea61c52014-02-12 12:56:53 -0500574 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
575 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
Alex Deucher9e05fa12013-01-24 10:06:33 -0500576
Dave Airlie785b93e2009-08-28 15:46:53 +1000577#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200578 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
579 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
580 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000581#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200582
583 for (i = 0; i < 256; i++) {
584 radeon_crtc->lut_r[i] = i << 2;
585 radeon_crtc->lut_g[i] = i << 2;
586 radeon_crtc->lut_b[i] = i << 2;
587 }
588
589 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
590 radeon_atombios_init_crtc(dev, radeon_crtc);
591 else
592 radeon_legacy_init_crtc(dev, radeon_crtc);
593}
594
Alex Deuchere68adef2012-09-06 14:32:06 -0400595static const char *encoder_names[38] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200596 "NONE",
597 "INTERNAL_LVDS",
598 "INTERNAL_TMDS1",
599 "INTERNAL_TMDS2",
600 "INTERNAL_DAC1",
601 "INTERNAL_DAC2",
602 "INTERNAL_SDVOA",
603 "INTERNAL_SDVOB",
604 "SI170B",
605 "CH7303",
606 "CH7301",
607 "INTERNAL_DVO1",
608 "EXTERNAL_SDVOA",
609 "EXTERNAL_SDVOB",
610 "TITFP513",
611 "INTERNAL_LVTM1",
612 "VT1623",
613 "HDMI_SI1930",
614 "HDMI_INTERNAL",
615 "INTERNAL_KLDSCP_TMDS1",
616 "INTERNAL_KLDSCP_DVO1",
617 "INTERNAL_KLDSCP_DAC1",
618 "INTERNAL_KLDSCP_DAC2",
619 "SI178",
620 "MVPU_FPGA",
621 "INTERNAL_DDI",
622 "VT1625",
623 "HDMI_SI1932",
624 "DP_AN9801",
625 "DP_DP501",
626 "INTERNAL_UNIPHY",
627 "INTERNAL_KLDSCP_LVTMA",
628 "INTERNAL_UNIPHY1",
629 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500630 "NUTMEG",
631 "TRAVIS",
Alex Deuchere68adef2012-09-06 14:32:06 -0400632 "INTERNAL_VCE",
633 "INTERNAL_UNIPHY3",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200634};
635
Alex Deuchercbd46232010-06-07 02:24:54 -0400636static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500637 "HPD1",
638 "HPD2",
639 "HPD3",
640 "HPD4",
641 "HPD5",
642 "HPD6",
643};
644
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200645static void radeon_print_display_setup(struct drm_device *dev)
646{
647 struct drm_connector *connector;
648 struct radeon_connector *radeon_connector;
649 struct drm_encoder *encoder;
650 struct radeon_encoder *radeon_encoder;
651 uint32_t devices;
652 int i = 0;
653
654 DRM_INFO("Radeon Display Connectors\n");
655 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
656 radeon_connector = to_radeon_connector(connector);
657 DRM_INFO("Connector %d:\n", i);
Ilija Hadzicc1d2dbd2012-05-04 11:25:12 -0400658 DRM_INFO(" %s\n", drm_get_connector_name(connector));
Alex Deuchereed45b32009-12-04 14:45:27 -0500659 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
660 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000661 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200662 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
663 radeon_connector->ddc_bus->rec.mask_clk_reg,
664 radeon_connector->ddc_bus->rec.mask_data_reg,
665 radeon_connector->ddc_bus->rec.a_clk_reg,
666 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500667 radeon_connector->ddc_bus->rec.en_clk_reg,
668 radeon_connector->ddc_bus->rec.en_data_reg,
669 radeon_connector->ddc_bus->rec.y_clk_reg,
670 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000671 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400672 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000673 radeon_connector->router.ddc_mux_control_pin,
674 radeon_connector->router.ddc_mux_state);
675 if (radeon_connector->router.cd_valid)
676 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
677 radeon_connector->router.cd_mux_control_pin,
678 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000679 } else {
680 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
681 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
682 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
683 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
684 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
685 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
686 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
687 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200688 DRM_INFO(" Encoders:\n");
689 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
690 radeon_encoder = to_radeon_encoder(encoder);
691 devices = radeon_encoder->devices & radeon_connector->devices;
692 if (devices) {
693 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
694 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
695 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
696 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
697 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
698 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
699 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
700 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
701 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
702 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
703 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
704 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
705 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
706 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
707 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
708 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400709 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
710 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200711 if (devices & ATOM_DEVICE_TV1_SUPPORT)
712 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
713 if (devices & ATOM_DEVICE_CV_SUPPORT)
714 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
715 }
716 }
717 i++;
718 }
719}
720
Dave Airlie4ce001a2009-08-13 16:32:14 +1000721static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200722{
723 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200724 bool ret = false;
725
726 if (rdev->bios) {
727 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400728 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
729 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200730 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500731 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200732 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500733 if (ret == false)
734 ret = radeon_get_legacy_connector_info_from_table(dev);
735 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200736 } else {
737 if (!ASIC_IS_AVIVO(rdev))
738 ret = radeon_get_legacy_connector_info_from_table(dev);
739 }
740 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000741 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200742 radeon_print_display_setup(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200743 }
744
745 return ret;
746}
747
748int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
749{
Alex Deucher3c537882010-02-05 04:21:19 -0500750 struct drm_device *dev = radeon_connector->base.dev;
751 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200752 int ret = 0;
753
Alex Deucher26b5bc92010-08-05 21:21:18 -0400754 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000755 if (radeon_connector->router.ddc_valid)
756 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400757
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100758 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
759 ENCODER_OBJECT_ID_NONE) {
760 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
761
762 if (dig->dp_i2c_bus)
763 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
764 &dig->dp_i2c_bus->adapter);
765 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
766 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000767 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Alex Deucherb06947b2011-09-02 14:23:09 +0000768
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000769 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
770 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucherb06947b2011-09-02 14:23:09 +0000771 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
772 &dig->dp_i2c_bus->adapter);
773 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
774 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
775 &radeon_connector->ddc_bus->adapter);
776 } else {
777 if (radeon_connector->ddc_bus && !radeon_connector->edid)
778 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
779 &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400780 }
Alex Deucherc324acd2010-12-08 22:13:06 -0500781
782 if (!radeon_connector->edid) {
783 if (rdev->is_atom_bios) {
784 /* some laptops provide a hardcoded edid in rom for LCDs */
785 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
786 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
787 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
788 } else
789 /* some servers provide a hardcoded edid in rom for KVMs */
790 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
791 }
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400792 if (radeon_connector->edid) {
793 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
794 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200795 return ret;
796 }
797 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000798 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200799}
800
Alex Deucherf523f742011-01-31 16:48:52 -0500801/* avivo */
802static void avivo_get_fb_div(struct radeon_pll *pll,
803 u32 target_clock,
804 u32 post_div,
805 u32 ref_div,
806 u32 *fb_div,
807 u32 *frac_fb_div)
808{
809 u32 tmp = post_div * ref_div;
810
811 tmp *= target_clock;
812 *fb_div = tmp / pll->reference_freq;
813 *frac_fb_div = tmp % pll->reference_freq;
Alex Deuchera4b40d52011-02-14 11:43:10 -0500814
815 if (*fb_div > pll->max_feedback_div)
816 *fb_div = pll->max_feedback_div;
817 else if (*fb_div < pll->min_feedback_div)
818 *fb_div = pll->min_feedback_div;
Alex Deucherf523f742011-01-31 16:48:52 -0500819}
820
821static u32 avivo_get_post_div(struct radeon_pll *pll,
822 u32 target_clock)
823{
824 u32 vco, post_div, tmp;
825
826 if (pll->flags & RADEON_PLL_USE_POST_DIV)
827 return pll->post_div;
828
829 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
830 if (pll->flags & RADEON_PLL_IS_LCD)
831 vco = pll->lcd_pll_out_min;
832 else
833 vco = pll->pll_out_min;
834 } else {
835 if (pll->flags & RADEON_PLL_IS_LCD)
836 vco = pll->lcd_pll_out_max;
837 else
838 vco = pll->pll_out_max;
839 }
840
841 post_div = vco / target_clock;
842 tmp = vco % target_clock;
843
844 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
845 if (tmp)
846 post_div++;
847 } else {
848 if (!tmp)
849 post_div--;
850 }
851
Alex Deuchera4b40d52011-02-14 11:43:10 -0500852 if (post_div > pll->max_post_div)
853 post_div = pll->max_post_div;
854 else if (post_div < pll->min_post_div)
855 post_div = pll->min_post_div;
856
Alex Deucherf523f742011-01-31 16:48:52 -0500857 return post_div;
858}
859
860#define MAX_TOLERANCE 10
861
862void radeon_compute_pll_avivo(struct radeon_pll *pll,
863 u32 freq,
864 u32 *dot_clock_p,
865 u32 *fb_div_p,
866 u32 *frac_fb_div_p,
867 u32 *ref_div_p,
868 u32 *post_div_p)
869{
870 u32 target_clock = freq / 10;
871 u32 post_div = avivo_get_post_div(pll, target_clock);
872 u32 ref_div = pll->min_ref_div;
873 u32 fb_div = 0, frac_fb_div = 0, tmp;
874
875 if (pll->flags & RADEON_PLL_USE_REF_DIV)
876 ref_div = pll->reference_div;
877
878 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
879 avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
880 frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
881 if (frac_fb_div >= 5) {
882 frac_fb_div -= 5;
883 frac_fb_div = frac_fb_div / 10;
884 frac_fb_div++;
885 }
886 if (frac_fb_div >= 10) {
887 fb_div++;
888 frac_fb_div = 0;
889 }
890 } else {
891 while (ref_div <= pll->max_ref_div) {
892 avivo_get_fb_div(pll, target_clock, post_div, ref_div,
893 &fb_div, &frac_fb_div);
894 if (frac_fb_div >= (pll->reference_freq / 2))
895 fb_div++;
896 frac_fb_div = 0;
897 tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
898 tmp = (tmp * 10000) / target_clock;
899
900 if (tmp > (10000 + MAX_TOLERANCE))
901 ref_div++;
902 else if (tmp >= (10000 - MAX_TOLERANCE))
903 break;
904 else
905 ref_div++;
906 }
907 }
908
909 *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
910 (ref_div * post_div * 10);
911 *fb_div_p = fb_div;
912 *frac_fb_div_p = frac_fb_div;
913 *ref_div_p = ref_div;
914 *post_div_p = post_div;
915 DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
916 *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
917}
918
919/* pre-avivo */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200920static inline uint32_t radeon_div(uint64_t n, uint32_t d)
921{
922 uint64_t mod;
923
924 n += d / 2;
925
926 mod = do_div(n, d);
927 return n;
928}
929
Alex Deucherf523f742011-01-31 16:48:52 -0500930void radeon_compute_pll_legacy(struct radeon_pll *pll,
931 uint64_t freq,
932 uint32_t *dot_clock_p,
933 uint32_t *fb_div_p,
934 uint32_t *frac_fb_div_p,
935 uint32_t *ref_div_p,
936 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200937{
938 uint32_t min_ref_div = pll->min_ref_div;
939 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500940 uint32_t min_post_div = pll->min_post_div;
941 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200942 uint32_t min_fractional_feed_div = 0;
943 uint32_t max_fractional_feed_div = 0;
944 uint32_t best_vco = pll->best_vco;
945 uint32_t best_post_div = 1;
946 uint32_t best_ref_div = 1;
947 uint32_t best_feedback_div = 1;
948 uint32_t best_frac_feedback_div = 0;
949 uint32_t best_freq = -1;
950 uint32_t best_error = 0xffffffff;
951 uint32_t best_vco_diff = 1;
952 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500953 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200954
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000955 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200956 freq = freq * 1000;
957
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500958 if (pll->flags & RADEON_PLL_IS_LCD) {
959 pll_out_min = pll->lcd_pll_out_min;
960 pll_out_max = pll->lcd_pll_out_max;
961 } else {
962 pll_out_min = pll->pll_out_min;
963 pll_out_max = pll->pll_out_max;
964 }
965
Alex Deucher619efb12011-01-31 16:48:53 -0500966 if (pll_out_min > 64800)
967 pll_out_min = 64800;
968
Alex Deucherfc103322010-01-19 17:16:10 -0500969 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200970 min_ref_div = max_ref_div = pll->reference_div;
971 else {
972 while (min_ref_div < max_ref_div-1) {
973 uint32_t mid = (min_ref_div + max_ref_div) / 2;
974 uint32_t pll_in = pll->reference_freq / mid;
975 if (pll_in < pll->pll_in_min)
976 max_ref_div = mid;
977 else if (pll_in > pll->pll_in_max)
978 min_ref_div = mid;
979 else
980 break;
981 }
982 }
983
Alex Deucherfc103322010-01-19 17:16:10 -0500984 if (pll->flags & RADEON_PLL_USE_POST_DIV)
985 min_post_div = max_post_div = pll->post_div;
986
987 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200988 min_fractional_feed_div = pll->min_frac_feedback_div;
989 max_fractional_feed_div = pll->max_frac_feedback_div;
990 }
991
Alex Deucherbd6a60a2011-02-21 01:11:59 -0500992 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200993 uint32_t ref_div;
994
Alex Deucherfc103322010-01-19 17:16:10 -0500995 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200996 continue;
997
998 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500999 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001000 if ((post_div == 5) ||
1001 (post_div == 7) ||
1002 (post_div == 9) ||
1003 (post_div == 10) ||
1004 (post_div == 11) ||
1005 (post_div == 13) ||
1006 (post_div == 14) ||
1007 (post_div == 15))
1008 continue;
1009 }
1010
1011 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1012 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1013 uint32_t pll_in = pll->reference_freq / ref_div;
1014 uint32_t min_feed_div = pll->min_feedback_div;
1015 uint32_t max_feed_div = pll->max_feedback_div + 1;
1016
1017 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1018 continue;
1019
1020 while (min_feed_div < max_feed_div) {
1021 uint32_t vco;
1022 uint32_t min_frac_feed_div = min_fractional_feed_div;
1023 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1024 uint32_t frac_feedback_div;
1025 uint64_t tmp;
1026
1027 feedback_div = (min_feed_div + max_feed_div) / 2;
1028
1029 tmp = (uint64_t)pll->reference_freq * feedback_div;
1030 vco = radeon_div(tmp, ref_div);
1031
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001032 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001033 min_feed_div = feedback_div + 1;
1034 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001035 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001036 max_feed_div = feedback_div;
1037 continue;
1038 }
1039
1040 while (min_frac_feed_div < max_frac_feed_div) {
1041 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1042 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1043 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1044 current_freq = radeon_div(tmp, ref_div * post_div);
1045
Alex Deucherfc103322010-01-19 17:16:10 -05001046 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +02001047 if (freq < current_freq)
1048 error = 0xffffffff;
1049 else
1050 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -04001051 } else
1052 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001053 vco_diff = abs(vco - best_vco);
1054
1055 if ((best_vco == 0 && error < best_error) ||
1056 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +02001057 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +10001058 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001059 best_post_div = post_div;
1060 best_ref_div = ref_div;
1061 best_feedback_div = feedback_div;
1062 best_frac_feedback_div = frac_feedback_div;
1063 best_freq = current_freq;
1064 best_error = error;
1065 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +10001066 } else if (current_freq == freq) {
1067 if (best_freq == -1) {
1068 best_post_div = post_div;
1069 best_ref_div = ref_div;
1070 best_feedback_div = feedback_div;
1071 best_frac_feedback_div = frac_feedback_div;
1072 best_freq = current_freq;
1073 best_error = error;
1074 best_vco_diff = vco_diff;
1075 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1076 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1077 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1078 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1079 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1080 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1081 best_post_div = post_div;
1082 best_ref_div = ref_div;
1083 best_feedback_div = feedback_div;
1084 best_frac_feedback_div = frac_feedback_div;
1085 best_freq = current_freq;
1086 best_error = error;
1087 best_vco_diff = vco_diff;
1088 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001089 }
1090 if (current_freq < freq)
1091 min_frac_feed_div = frac_feedback_div + 1;
1092 else
1093 max_frac_feed_div = frac_feedback_div;
1094 }
1095 if (current_freq < freq)
1096 min_feed_div = feedback_div + 1;
1097 else
1098 max_feed_div = feedback_div;
1099 }
1100 }
1101 }
1102
1103 *dot_clock_p = best_freq / 10000;
1104 *fb_div_p = best_feedback_div;
1105 *frac_fb_div_p = best_frac_feedback_div;
1106 *ref_div_p = best_ref_div;
1107 *post_div_p = best_post_div;
Joe Perchesbbb0aef2011-04-17 20:35:52 -07001108 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1109 (long long)freq,
1110 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
Alex Deucher51d4bf82011-01-31 16:48:51 -05001111 best_ref_div, best_post_div);
1112
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001113}
1114
1115static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1116{
1117 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001118
Dave Airlie29d08b32010-09-27 16:17:17 +10001119 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001120 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +10001121 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001122 drm_framebuffer_cleanup(fb);
1123 kfree(radeon_fb);
1124}
1125
1126static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1127 struct drm_file *file_priv,
1128 unsigned int *handle)
1129{
1130 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1131
1132 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1133}
1134
1135static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1136 .destroy = radeon_user_framebuffer_destroy,
1137 .create_handle = radeon_user_framebuffer_create_handle,
1138};
1139
Dave Airlieaaefcd42012-03-06 10:44:40 +00001140int
Dave Airlie38651672010-03-30 05:34:13 +00001141radeon_framebuffer_init(struct drm_device *dev,
1142 struct radeon_framebuffer *rfb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001143 struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlie38651672010-03-30 05:34:13 +00001144 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001145{
Dave Airlieaaefcd42012-03-06 10:44:40 +00001146 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00001147 rfb->obj = obj;
Daniel Vetterc7d73f62012-12-13 23:38:38 +01001148 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001149 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1150 if (ret) {
1151 rfb->obj = NULL;
1152 return ret;
1153 }
Dave Airlieaaefcd42012-03-06 10:44:40 +00001154 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001155}
1156
1157static struct drm_framebuffer *
1158radeon_user_framebuffer_create(struct drm_device *dev,
1159 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001160 struct drm_mode_fb_cmd2 *mode_cmd)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001161{
1162 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00001163 struct radeon_framebuffer *radeon_fb;
Dave Airlieaaefcd42012-03-06 10:44:40 +00001164 int ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001165
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001166 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001167 if (obj == NULL) {
1168 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001169 "can't create framebuffer\n", mode_cmd->handles[0]);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001170 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001171 }
Dave Airlie38651672010-03-30 05:34:13 +00001172
1173 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001174 if (radeon_fb == NULL) {
1175 drm_gem_object_unreference_unlocked(obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001176 return ERR_PTR(-ENOMEM);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001177 }
Dave Airlie38651672010-03-30 05:34:13 +00001178
Dave Airlieaaefcd42012-03-06 10:44:40 +00001179 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1180 if (ret) {
1181 kfree(radeon_fb);
1182 drm_gem_object_unreference_unlocked(obj);
xueminsub2f4b032013-01-22 22:16:53 +08001183 return ERR_PTR(ret);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001184 }
Dave Airlie38651672010-03-30 05:34:13 +00001185
1186 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001187}
1188
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001189static void radeon_output_poll_changed(struct drm_device *dev)
1190{
1191 struct radeon_device *rdev = dev->dev_private;
1192 radeon_fb_output_poll_changed(rdev);
1193}
1194
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001195static const struct drm_mode_config_funcs radeon_mode_funcs = {
1196 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001197 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001198};
1199
Dave Airlie445282d2009-09-09 17:40:54 +10001200static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1201{ { 0, "driver" },
1202 { 1, "bios" },
1203};
1204
1205static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1206{ { TV_STD_NTSC, "ntsc" },
1207 { TV_STD_PAL, "pal" },
1208 { TV_STD_PAL_M, "pal-m" },
1209 { TV_STD_PAL_60, "pal-60" },
1210 { TV_STD_NTSC_J, "ntsc-j" },
1211 { TV_STD_SCART_PAL, "scart-pal" },
1212 { TV_STD_PAL_CN, "pal-cn" },
1213 { TV_STD_SECAM, "secam" },
1214};
1215
Alex Deucher5b1714d2010-08-03 19:59:20 -04001216static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1217{ { UNDERSCAN_OFF, "off" },
1218 { UNDERSCAN_ON, "on" },
1219 { UNDERSCAN_AUTO, "auto" },
1220};
1221
Alex Deucher8666c072013-09-03 14:58:44 -04001222static struct drm_prop_enum_list radeon_audio_enum_list[] =
1223{ { RADEON_AUDIO_DISABLE, "off" },
1224 { RADEON_AUDIO_ENABLE, "on" },
1225 { RADEON_AUDIO_AUTO, "auto" },
1226};
1227
Alex Deucher6214bb72013-09-24 17:26:26 -04001228/* XXX support different dither options? spatial, temporal, both, etc. */
1229static struct drm_prop_enum_list radeon_dither_enum_list[] =
1230{ { RADEON_FMT_DITHER_DISABLE, "off" },
1231 { RADEON_FMT_DITHER_ENABLE, "on" },
1232};
1233
Alex Deucherd79766f2009-12-17 19:00:29 -05001234static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +10001235{
Sascha Hauer4a67d392012-02-06 10:58:17 +01001236 int sz;
Dave Airlie445282d2009-09-09 17:40:54 +10001237
1238 if (rdev->is_atom_bios) {
1239 rdev->mode_info.coherent_mode_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001240 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001241 if (!rdev->mode_info.coherent_mode_property)
1242 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001243 }
1244
1245 if (!ASIC_IS_AVIVO(rdev)) {
1246 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1247 rdev->mode_info.tmds_pll_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001248 drm_property_create_enum(rdev->ddev, 0,
1249 "tmds_pll",
1250 radeon_tmds_pll_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001251 }
1252
1253 rdev->mode_info.load_detect_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001254 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001255 if (!rdev->mode_info.load_detect_property)
1256 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001257
1258 drm_mode_create_scaling_mode_property(rdev->ddev);
1259
1260 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1261 rdev->mode_info.tv_std_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001262 drm_property_create_enum(rdev->ddev, 0,
1263 "tv standard",
1264 radeon_tv_std_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001265
Alex Deucher5b1714d2010-08-03 19:59:20 -04001266 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1267 rdev->mode_info.underscan_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001268 drm_property_create_enum(rdev->ddev, 0,
1269 "underscan",
1270 radeon_underscan_enum_list, sz);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001271
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001272 rdev->mode_info.underscan_hborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001273 drm_property_create_range(rdev->ddev, 0,
1274 "underscan hborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001275 if (!rdev->mode_info.underscan_hborder_property)
1276 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001277
1278 rdev->mode_info.underscan_vborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001279 drm_property_create_range(rdev->ddev, 0,
1280 "underscan vborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001281 if (!rdev->mode_info.underscan_vborder_property)
1282 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001283
Alex Deucher8666c072013-09-03 14:58:44 -04001284 sz = ARRAY_SIZE(radeon_audio_enum_list);
1285 rdev->mode_info.audio_property =
1286 drm_property_create_enum(rdev->ddev, 0,
1287 "audio",
1288 radeon_audio_enum_list, sz);
1289
Alex Deucher6214bb72013-09-24 17:26:26 -04001290 sz = ARRAY_SIZE(radeon_dither_enum_list);
1291 rdev->mode_info.dither_property =
1292 drm_property_create_enum(rdev->ddev, 0,
1293 "dither",
1294 radeon_dither_enum_list, sz);
1295
Dave Airlie445282d2009-09-09 17:40:54 +10001296 return 0;
1297}
1298
Alex Deucherf46c0122010-03-31 00:33:27 -04001299void radeon_update_display_priority(struct radeon_device *rdev)
1300{
1301 /* adjustment options for the display watermarks */
1302 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1303 /* set display priority to high for r3xx, rv515 chips
1304 * this avoids flickering due to underflow to the
1305 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001306 * Don't force high on rs4xx igp chips as it seems to
1307 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001308 */
Alex Deucher45737442010-05-20 11:26:11 -04001309 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1310 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001311 rdev->disp_priority = 2;
1312 else
1313 rdev->disp_priority = 0;
1314 } else
1315 rdev->disp_priority = radeon_disp_priority;
1316
1317}
1318
Alex Deucher07839862012-05-14 16:52:29 +02001319/*
1320 * Allocate hdmi structs and determine register offsets
1321 */
1322static void radeon_afmt_init(struct radeon_device *rdev)
1323{
1324 int i;
1325
1326 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1327 rdev->mode_info.afmt[i] = NULL;
1328
Alex Deucherb5306022013-07-31 16:51:33 -04001329 if (ASIC_IS_NODCE(rdev)) {
1330 /* nothing to do */
Alex Deucher07839862012-05-14 16:52:29 +02001331 } else if (ASIC_IS_DCE4(rdev)) {
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001332 static uint32_t eg_offsets[] = {
1333 EVERGREEN_CRTC0_REGISTER_OFFSET,
1334 EVERGREEN_CRTC1_REGISTER_OFFSET,
1335 EVERGREEN_CRTC2_REGISTER_OFFSET,
1336 EVERGREEN_CRTC3_REGISTER_OFFSET,
1337 EVERGREEN_CRTC4_REGISTER_OFFSET,
1338 EVERGREEN_CRTC5_REGISTER_OFFSET,
Alex Deucherb5306022013-07-31 16:51:33 -04001339 0x13830 - 0x7030,
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001340 };
1341 int num_afmt;
1342
Alex Deucherb5306022013-07-31 16:51:33 -04001343 /* DCE8 has 7 audio blocks tied to DIG encoders */
1344 /* DCE6 has 6 audio blocks tied to DIG encoders */
Alex Deucher07839862012-05-14 16:52:29 +02001345 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1346 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
Alex Deucherb5306022013-07-31 16:51:33 -04001347 if (ASIC_IS_DCE8(rdev))
1348 num_afmt = 7;
1349 else if (ASIC_IS_DCE6(rdev))
1350 num_afmt = 6;
1351 else if (ASIC_IS_DCE5(rdev))
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001352 num_afmt = 6;
1353 else if (ASIC_IS_DCE41(rdev))
1354 num_afmt = 2;
1355 else /* DCE4 */
1356 num_afmt = 6;
1357
1358 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1359 for (i = 0; i < num_afmt; i++) {
1360 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1361 if (rdev->mode_info.afmt[i]) {
1362 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1363 rdev->mode_info.afmt[i]->id = i;
Alex Deucher07839862012-05-14 16:52:29 +02001364 }
1365 }
1366 } else if (ASIC_IS_DCE3(rdev)) {
1367 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1368 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1369 if (rdev->mode_info.afmt[0]) {
1370 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1371 rdev->mode_info.afmt[0]->id = 0;
1372 }
1373 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1374 if (rdev->mode_info.afmt[1]) {
1375 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1376 rdev->mode_info.afmt[1]->id = 1;
1377 }
1378 } else if (ASIC_IS_DCE2(rdev)) {
1379 /* DCE2 has at least 1 routable audio block */
1380 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1381 if (rdev->mode_info.afmt[0]) {
1382 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1383 rdev->mode_info.afmt[0]->id = 0;
1384 }
1385 /* r6xx has 2 routable audio blocks */
1386 if (rdev->family >= CHIP_R600) {
1387 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1388 if (rdev->mode_info.afmt[1]) {
1389 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1390 rdev->mode_info.afmt[1]->id = 1;
1391 }
1392 }
1393 }
1394}
1395
1396static void radeon_afmt_fini(struct radeon_device *rdev)
1397{
1398 int i;
1399
1400 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1401 kfree(rdev->mode_info.afmt[i]);
1402 rdev->mode_info.afmt[i] = NULL;
1403 }
1404}
1405
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001406int radeon_modeset_init(struct radeon_device *rdev)
1407{
Alex Deucher18917b62010-02-01 16:02:25 -05001408 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001409 int ret;
1410
1411 drm_mode_config_init(rdev->ddev);
1412 rdev->mode_info.mode_config_initialized = true;
1413
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001414 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001415
Alex Deucher881dd742011-01-06 21:19:14 -05001416 if (ASIC_IS_DCE5(rdev)) {
1417 rdev->ddev->mode_config.max_width = 16384;
1418 rdev->ddev->mode_config.max_height = 16384;
1419 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001420 rdev->ddev->mode_config.max_width = 8192;
1421 rdev->ddev->mode_config.max_height = 8192;
1422 } else {
1423 rdev->ddev->mode_config.max_width = 4096;
1424 rdev->ddev->mode_config.max_height = 4096;
1425 }
1426
Dave Airlie019d96c2011-09-29 16:20:42 +01001427 rdev->ddev->mode_config.preferred_depth = 24;
1428 rdev->ddev->mode_config.prefer_shadow = 1;
1429
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001430 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1431
Dave Airlie445282d2009-09-09 17:40:54 +10001432 ret = radeon_modeset_create_props(rdev);
1433 if (ret) {
1434 return ret;
1435 }
Dave Airliedfee5612009-10-02 09:19:09 +10001436
Alex Deucherf376b942010-08-05 21:21:16 -04001437 /* init i2c buses */
1438 radeon_i2c_init(rdev);
1439
Alex Deucher3c537882010-02-05 04:21:19 -05001440 /* check combios for a valid hardcoded EDID - Sun servers */
1441 if (!rdev->is_atom_bios) {
1442 /* check for hardcoded EDID in BIOS */
1443 radeon_combios_check_hardcoded_edid(rdev);
1444 }
1445
Dave Airliedfee5612009-10-02 09:19:09 +10001446 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001447 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001448 radeon_crtc_init(rdev->ddev, i);
1449 }
1450
1451 /* okay we should have all the bios connectors */
1452 ret = radeon_setup_enc_conn(rdev->ddev);
1453 if (!ret) {
1454 return ret;
1455 }
Alex Deucherac89af12011-05-22 13:20:36 -04001456
Alex Deucher3fa47d92012-01-20 14:56:39 -05001457 /* init dig PHYs, disp eng pll */
1458 if (rdev->is_atom_bios) {
Alex Deucherac89af12011-05-22 13:20:36 -04001459 radeon_atom_encoder_init(rdev);
Alex Deucherf3f1f032012-03-20 17:18:04 -04001460 radeon_atom_disp_eng_pll_init(rdev);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001461 }
Alex Deucherac89af12011-05-22 13:20:36 -04001462
Alex Deucherd4877cf2009-12-04 16:56:37 -05001463 /* initialize hpd */
1464 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001465
Alex Deucher07839862012-05-14 16:52:29 +02001466 /* setup afmt */
1467 radeon_afmt_init(rdev);
1468
Dave Airlie38651672010-03-30 05:34:13 +00001469 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001470 drm_kms_helper_poll_init(rdev->ddev);
1471
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001472 if (rdev->pm.dpm_enabled) {
1473 /* do dpm late init */
1474 ret = radeon_pm_late_init(rdev);
1475 if (ret) {
1476 rdev->pm.dpm_enabled = false;
1477 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1478 }
1479 /* set the dpm state for PX since there won't be
1480 * a modeset to call this.
1481 */
1482 radeon_pm_compute_clocks(rdev);
1483 }
1484
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001485 return 0;
1486}
1487
1488void radeon_modeset_fini(struct radeon_device *rdev)
1489{
Dave Airlie38651672010-03-30 05:34:13 +00001490 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001491 kfree(rdev->mode_info.bios_hardcoded_edid);
1492
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001493 if (rdev->mode_info.mode_config_initialized) {
Alex Deucher07839862012-05-14 16:52:29 +02001494 radeon_afmt_fini(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001495 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001496 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001497 drm_mode_config_cleanup(rdev->ddev);
1498 rdev->mode_info.mode_config_initialized = false;
1499 }
Alex Deucherf376b942010-08-05 21:21:16 -04001500 /* free i2c buses */
1501 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001502}
1503
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001504static bool is_hdtv_mode(const struct drm_display_mode *mode)
Alex Deucher039ed2d2010-08-20 11:57:19 -04001505{
1506 /* try and guess if this is a tv or a monitor */
1507 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1508 (mode->vdisplay == 576) || /* 576p */
1509 (mode->vdisplay == 720) || /* 720p */
1510 (mode->vdisplay == 1080)) /* 1080p */
1511 return true;
1512 else
1513 return false;
1514}
1515
Jerome Glissec93bb852009-07-13 21:04:08 +02001516bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001517 const struct drm_display_mode *mode,
Jerome Glissec93bb852009-07-13 21:04:08 +02001518 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001519{
Jerome Glissec93bb852009-07-13 21:04:08 +02001520 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001521 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001522 struct drm_encoder *encoder;
1523 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1524 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001525 struct drm_connector *connector;
1526 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001527 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001528 u32 src_v = 1, dst_v = 1;
1529 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001530
Alex Deucher5b1714d2010-08-03 19:59:20 -04001531 radeon_crtc->h_border = 0;
1532 radeon_crtc->v_border = 0;
1533
Jerome Glissec93bb852009-07-13 21:04:08 +02001534 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001535 if (encoder->crtc != crtc)
1536 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001537 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001538 connector = radeon_get_connector_for_encoder(encoder);
1539 radeon_connector = to_radeon_connector(connector);
1540
Jerome Glissec93bb852009-07-13 21:04:08 +02001541 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001542 /* set scaling */
1543 if (radeon_encoder->rmx_type == RMX_OFF)
1544 radeon_crtc->rmx_type = RMX_OFF;
1545 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1546 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1547 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1548 else
1549 radeon_crtc->rmx_type = RMX_OFF;
1550 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001551 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001552 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001553 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001554 src_v = crtc->mode.vdisplay;
1555 dst_v = radeon_crtc->native_mode.vdisplay;
1556 src_h = crtc->mode.hdisplay;
1557 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001558
1559 /* fix up for overscan on hdmi */
1560 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001561 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001562 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1563 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001564 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1565 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001566 if (radeon_encoder->underscan_hborder != 0)
1567 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1568 else
1569 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1570 if (radeon_encoder->underscan_vborder != 0)
1571 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1572 else
1573 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001574 radeon_crtc->rmx_type = RMX_FULL;
1575 src_v = crtc->mode.vdisplay;
1576 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1577 src_h = crtc->mode.hdisplay;
1578 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1579 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001580 first = false;
1581 } else {
1582 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1583 /* WARNING: Right now this can't happen but
1584 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001585 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001586 * (ie all encoder can work with the same
1587 * scaling).
1588 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001589 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001590 return false;
1591 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001592 }
1593 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001594 if (radeon_crtc->rmx_type != RMX_OFF) {
1595 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001596 a.full = dfixed_const(src_v);
1597 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001598 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001599 a.full = dfixed_const(src_h);
1600 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001601 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001602 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001603 radeon_crtc->vsc.full = dfixed_const(1);
1604 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001605 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001606 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001607}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001608
1609/*
Mario Kleinerd47abc52013-10-30 05:13:07 +01001610 * Retrieve current video scanout position of crtc on a given gpu, and
1611 * an optional accurate timestamp of when query happened.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001612 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001613 * \param dev Device to query.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001614 * \param crtc Crtc to query.
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001615 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
Mario Kleiner6383cf72010-10-05 19:57:36 -04001616 * \param *vpos Location where vertical scanout position should be stored.
1617 * \param *hpos Location where horizontal scanout position should go.
Mario Kleinerd47abc52013-10-30 05:13:07 +01001618 * \param *stime Target location for timestamp taken immediately before
1619 * scanout position query. Can be NULL to skip timestamp.
1620 * \param *etime Target location for timestamp taken immediately after
1621 * scanout position query. Can be NULL to skip timestamp.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001622 *
1623 * Returns vpos as a positive number while in active scanout area.
1624 * Returns vpos as a negative number inside vblank, counting the number
1625 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1626 * until start of active scanout / end of vblank."
1627 *
1628 * \return Flags, or'ed together as follows:
1629 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001630 * DRM_SCANOUTPOS_VALID = Query successful.
Mario Kleinerf5a80202010-10-23 04:42:17 +02001631 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1632 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
Mario Kleiner6383cf72010-10-05 19:57:36 -04001633 * this flag means that returned position may be offset by a constant but
1634 * unknown small number of scanlines wrt. real scanout position.
1635 *
1636 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001637int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1638 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
Mario Kleiner6383cf72010-10-05 19:57:36 -04001639{
1640 u32 stat_crtc = 0, vbl = 0, position = 0;
1641 int vbl_start, vbl_end, vtotal, ret = 0;
1642 bool in_vbl = true;
1643
Mario Kleinerf5a80202010-10-23 04:42:17 +02001644 struct radeon_device *rdev = dev->dev_private;
1645
Mario Kleinerd47abc52013-10-30 05:13:07 +01001646 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1647
1648 /* Get optional system timestamp before query. */
1649 if (stime)
1650 *stime = ktime_get();
1651
Mario Kleiner6383cf72010-10-05 19:57:36 -04001652 if (ASIC_IS_DCE4(rdev)) {
1653 if (crtc == 0) {
1654 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1655 EVERGREEN_CRTC0_REGISTER_OFFSET);
1656 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1657 EVERGREEN_CRTC0_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001658 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001659 }
1660 if (crtc == 1) {
1661 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1662 EVERGREEN_CRTC1_REGISTER_OFFSET);
1663 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1664 EVERGREEN_CRTC1_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001665 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001666 }
1667 if (crtc == 2) {
1668 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1669 EVERGREEN_CRTC2_REGISTER_OFFSET);
1670 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1671 EVERGREEN_CRTC2_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001672 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001673 }
1674 if (crtc == 3) {
1675 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1676 EVERGREEN_CRTC3_REGISTER_OFFSET);
1677 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1678 EVERGREEN_CRTC3_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001679 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001680 }
1681 if (crtc == 4) {
1682 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1683 EVERGREEN_CRTC4_REGISTER_OFFSET);
1684 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1685 EVERGREEN_CRTC4_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001686 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001687 }
1688 if (crtc == 5) {
1689 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1690 EVERGREEN_CRTC5_REGISTER_OFFSET);
1691 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1692 EVERGREEN_CRTC5_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001693 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001694 }
1695 } else if (ASIC_IS_AVIVO(rdev)) {
1696 if (crtc == 0) {
1697 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1698 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001699 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001700 }
1701 if (crtc == 1) {
1702 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1703 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001704 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001705 }
1706 } else {
1707 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1708 if (crtc == 0) {
1709 /* Assume vbl_end == 0, get vbl_start from
1710 * upper 16 bits.
1711 */
1712 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1713 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1714 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1715 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1716 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1717 if (!(stat_crtc & 1))
1718 in_vbl = false;
1719
Mario Kleinerf5a80202010-10-23 04:42:17 +02001720 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001721 }
1722 if (crtc == 1) {
1723 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1724 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1725 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1726 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1727 if (!(stat_crtc & 1))
1728 in_vbl = false;
1729
Mario Kleinerf5a80202010-10-23 04:42:17 +02001730 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001731 }
1732 }
1733
Mario Kleinerd47abc52013-10-30 05:13:07 +01001734 /* Get optional system timestamp after query. */
1735 if (etime)
1736 *etime = ktime_get();
1737
1738 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1739
Mario Kleiner6383cf72010-10-05 19:57:36 -04001740 /* Decode into vertical and horizontal scanout position. */
1741 *vpos = position & 0x1fff;
1742 *hpos = (position >> 16) & 0x1fff;
1743
1744 /* Valid vblank area boundaries from gpu retrieved? */
1745 if (vbl > 0) {
1746 /* Yes: Decode. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001747 ret |= DRM_SCANOUTPOS_ACCURATE;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001748 vbl_start = vbl & 0x1fff;
1749 vbl_end = (vbl >> 16) & 0x1fff;
1750 }
1751 else {
1752 /* No: Fake something reasonable which gives at least ok results. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001753 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001754 vbl_end = 0;
1755 }
1756
1757 /* Test scanout position against vblank region. */
1758 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1759 in_vbl = false;
1760
1761 /* Check if inside vblank area and apply corrective offsets:
1762 * vpos will then be >=0 in video scanout area, but negative
1763 * within vblank area, counting down the number of lines until
1764 * start of scanout.
1765 */
1766
1767 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1768 if (in_vbl && (*vpos >= vbl_start)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001769 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001770 *vpos = *vpos - vtotal;
1771 }
1772
1773 /* Correct for shifted end of vbl at vbl_end. */
1774 *vpos = *vpos - vbl_end;
1775
1776 /* In vblank? */
1777 if (in_vbl)
Mario Kleinerf5a80202010-10-23 04:42:17 +02001778 ret |= DRM_SCANOUTPOS_INVBL;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001779
Ville Syrjälä8072bfa2013-10-28 21:22:52 +02001780 /* Is vpos outside nominal vblank area, but less than
1781 * 1/100 of a frame height away from start of vblank?
1782 * If so, assume this isn't a massively delayed vblank
1783 * interrupt, but a vblank interrupt that fired a few
1784 * microseconds before true start of vblank. Compensate
1785 * by adding a full frame duration to the final timestamp.
1786 * Happens, e.g., on ATI R500, R600.
1787 *
1788 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1789 */
1790 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1791 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1792 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1793
1794 if (vbl_start - *vpos < vtotal / 100) {
1795 *vpos -= vtotal;
1796
1797 /* Signal this correction as "applied". */
1798 ret |= 0x8;
1799 }
1800 }
1801
Mario Kleiner6383cf72010-10-05 19:57:36 -04001802 return ret;
1803}