blob: 7b17e639ab32c121419064441fd1aa1c60451471 [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 */
26#include "drmP.h"
27#include "radeon_drm.h"
28#include "radeon.h"
29
30#include "atom.h"
31#include <asm/div64.h>
32
33#include "drm_crtc_helper.h"
34#include "drm_edid.h"
35
36static int radeon_ddc_dump(struct drm_connector *connector);
37
38static void avivo_crtc_load_lut(struct drm_crtc *crtc)
39{
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 struct drm_device *dev = crtc->dev;
42 struct radeon_device *rdev = dev->dev_private;
43 int i;
44
Dave Airlied9fdaaf2010-08-02 10:42:55 +100045 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020046 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
47
48 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
50 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
51
52 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
54 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
55
56 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
57 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
58 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
59
60 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
61 for (i = 0; i < 256; i++) {
62 WREG32(AVIVO_DC_LUT_30_COLOR,
63 (radeon_crtc->lut_r[i] << 20) |
64 (radeon_crtc->lut_g[i] << 10) |
65 (radeon_crtc->lut_b[i] << 0));
66 }
67
68 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
69}
70
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050071static void evergreen_crtc_load_lut(struct drm_crtc *crtc)
72{
73 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
74 struct drm_device *dev = crtc->dev;
75 struct radeon_device *rdev = dev->dev_private;
76 int i;
77
Dave Airlied9fdaaf2010-08-02 10:42:55 +100078 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050079 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
80
81 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
84
85 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
88
Alex Deucher677d0762010-04-22 22:58:50 -040089 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
90 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050091
Alex Deucher677d0762010-04-22 22:58:50 -040092 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050093 for (i = 0; i < 256; i++) {
Alex Deucher677d0762010-04-22 22:58:50 -040094 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050095 (radeon_crtc->lut_r[i] << 20) |
96 (radeon_crtc->lut_g[i] << 10) |
97 (radeon_crtc->lut_b[i] << 0));
98 }
99}
100
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101static void legacy_crtc_load_lut(struct drm_crtc *crtc)
102{
103 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
104 struct drm_device *dev = crtc->dev;
105 struct radeon_device *rdev = dev->dev_private;
106 int i;
107 uint32_t dac2_cntl;
108
109 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
110 if (radeon_crtc->crtc_id == 0)
111 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
112 else
113 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
114 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
115
116 WREG8(RADEON_PALETTE_INDEX, 0);
117 for (i = 0; i < 256; i++) {
118 WREG32(RADEON_PALETTE_30_DATA,
119 (radeon_crtc->lut_r[i] << 20) |
120 (radeon_crtc->lut_g[i] << 10) |
121 (radeon_crtc->lut_b[i] << 0));
122 }
123}
124
125void radeon_crtc_load_lut(struct drm_crtc *crtc)
126{
127 struct drm_device *dev = crtc->dev;
128 struct radeon_device *rdev = dev->dev_private;
129
130 if (!crtc->enabled)
131 return;
132
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500133 if (ASIC_IS_DCE4(rdev))
134 evergreen_crtc_load_lut(crtc);
135 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200136 avivo_crtc_load_lut(crtc);
137 else
138 legacy_crtc_load_lut(crtc);
139}
140
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000141/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
143 u16 blue, int regno)
144{
145 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
146
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200147 radeon_crtc->lut_r[regno] = red >> 6;
148 radeon_crtc->lut_g[regno] = green >> 6;
149 radeon_crtc->lut_b[regno] = blue >> 6;
150}
151
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000152/** Gets the color ramps on behalf of fbcon */
153void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
154 u16 *blue, int regno)
155{
156 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
157
158 *red = radeon_crtc->lut_r[regno] << 6;
159 *green = radeon_crtc->lut_g[regno] << 6;
160 *blue = radeon_crtc->lut_b[regno] << 6;
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100164 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200165{
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100167 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000169 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100170 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000171 radeon_crtc->lut_r[i] = red[i] >> 6;
172 radeon_crtc->lut_g[i] = green[i] >> 6;
173 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200174 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175 radeon_crtc_load_lut(crtc);
176}
177
178static void radeon_crtc_destroy(struct drm_crtc *crtc)
179{
180 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
181
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200182 drm_crtc_cleanup(crtc);
183 kfree(radeon_crtc);
184}
185
Alex Deucher6f34be52010-11-21 10:59:01 -0500186/*
187 * Handle unpin events outside the interrupt handler proper.
188 */
189static void radeon_unpin_work_func(struct work_struct *__work)
190{
191 struct radeon_unpin_work *work =
192 container_of(__work, struct radeon_unpin_work, work);
193 int r;
194
195 /* unpin of the old buffer */
196 r = radeon_bo_reserve(work->old_rbo, false);
197 if (likely(r == 0)) {
198 r = radeon_bo_unpin(work->old_rbo);
199 if (unlikely(r != 0)) {
200 DRM_ERROR("failed to unpin buffer after flip\n");
201 }
202 radeon_bo_unreserve(work->old_rbo);
203 } else
204 DRM_ERROR("failed to reserve buffer after flip\n");
205 kfree(work);
206}
207
208void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
209{
210 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
211 struct radeon_unpin_work *work;
212 struct drm_pending_vblank_event *e;
213 struct timeval now;
214 unsigned long flags;
215 u32 update_pending;
216 int vpos, hpos;
217
218 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
219 work = radeon_crtc->unpin_work;
220 if (work == NULL ||
221 !radeon_fence_signaled(work->fence)) {
222 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
223 return;
224 }
225 /* New pageflip, or just completion of a previous one? */
226 if (!radeon_crtc->deferred_flip_completion) {
227 /* do the flip (mmio) */
228 update_pending = radeon_page_flip(rdev, crtc_id, work->new_crtc_base);
229 } else {
230 /* This is just a completion of a flip queued in crtc
231 * at last invocation. Make sure we go directly to
232 * completion routine.
233 */
234 update_pending = 0;
235 radeon_crtc->deferred_flip_completion = 0;
236 }
237
238 /* Has the pageflip already completed in crtc, or is it certain
239 * to complete in this vblank?
240 */
241 if (update_pending &&
242 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id,
243 &vpos, &hpos)) &&
244 (vpos >=0) &&
245 (vpos < (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100)) {
246 /* crtc didn't flip in this target vblank interval,
247 * but flip is pending in crtc. It will complete it
248 * in next vblank interval, so complete the flip at
249 * next vblank irq.
250 */
251 radeon_crtc->deferred_flip_completion = 1;
252 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
253 return;
254 }
255
256 /* Pageflip (will be) certainly completed in this vblank. Clean up. */
257 radeon_crtc->unpin_work = NULL;
258
259 /* wakeup userspace */
260 if (work->event) {
261 e = work->event;
Mario Kleinerb6724402010-11-21 10:59:03 -0500262 e->event.sequence = drm_vblank_count_and_time(rdev->ddev, crtc_id, &now);
Alex Deucher6f34be52010-11-21 10:59:01 -0500263 e->event.tv_sec = now.tv_sec;
264 e->event.tv_usec = now.tv_usec;
265 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
266 wake_up_interruptible(&e->base.file_priv->event_wait);
267 }
268 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
269
270 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
271 radeon_fence_unref(&work->fence);
272 radeon_post_page_flip(work->rdev, work->crtc_id);
273 schedule_work(&work->work);
274}
275
276static int radeon_crtc_page_flip(struct drm_crtc *crtc,
277 struct drm_framebuffer *fb,
278 struct drm_pending_vblank_event *event)
279{
280 struct drm_device *dev = crtc->dev;
281 struct radeon_device *rdev = dev->dev_private;
282 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
283 struct radeon_framebuffer *old_radeon_fb;
284 struct radeon_framebuffer *new_radeon_fb;
285 struct drm_gem_object *obj;
286 struct radeon_bo *rbo;
287 struct radeon_fence *fence;
288 struct radeon_unpin_work *work;
289 unsigned long flags;
290 u32 tiling_flags, pitch_pixels;
291 u64 base;
292 int r;
293
294 work = kzalloc(sizeof *work, GFP_KERNEL);
295 if (work == NULL)
296 return -ENOMEM;
297
298 r = radeon_fence_create(rdev, &fence);
299 if (unlikely(r != 0)) {
300 kfree(work);
301 DRM_ERROR("flip queue: failed to create fence.\n");
302 return -ENOMEM;
303 }
304 work->event = event;
305 work->rdev = rdev;
306 work->crtc_id = radeon_crtc->crtc_id;
307 work->fence = radeon_fence_ref(fence);
308 old_radeon_fb = to_radeon_framebuffer(crtc->fb);
309 new_radeon_fb = to_radeon_framebuffer(fb);
310 /* schedule unpin of the old buffer */
311 obj = old_radeon_fb->obj;
312 rbo = obj->driver_private;
313 work->old_rbo = rbo;
314 INIT_WORK(&work->work, radeon_unpin_work_func);
315
316 /* We borrow the event spin lock for protecting unpin_work */
317 spin_lock_irqsave(&dev->event_lock, flags);
318 if (radeon_crtc->unpin_work) {
319 spin_unlock_irqrestore(&dev->event_lock, flags);
320 kfree(work);
321 radeon_fence_unref(&fence);
322
323 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
324 return -EBUSY;
325 }
326 radeon_crtc->unpin_work = work;
327 radeon_crtc->deferred_flip_completion = 0;
328 spin_unlock_irqrestore(&dev->event_lock, flags);
329
330 /* pin the new buffer */
331 obj = new_radeon_fb->obj;
332 rbo = obj->driver_private;
333
334 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
335 work->old_rbo, rbo);
336
337 r = radeon_bo_reserve(rbo, false);
338 if (unlikely(r != 0)) {
339 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
340 goto pflip_cleanup;
341 }
342 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &base);
343 if (unlikely(r != 0)) {
344 radeon_bo_unreserve(rbo);
345 r = -EINVAL;
346 DRM_ERROR("failed to pin new rbo buffer before flip\n");
347 goto pflip_cleanup;
348 }
349 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
350 radeon_bo_unreserve(rbo);
351
352 if (!ASIC_IS_AVIVO(rdev)) {
353 /* crtc offset is from display base addr not FB location */
354 base -= radeon_crtc->legacy_display_base_addr;
355 pitch_pixels = fb->pitch / (fb->bits_per_pixel / 8);
356
357 if (tiling_flags & RADEON_TILING_MACRO) {
358 if (ASIC_IS_R300(rdev)) {
359 base &= ~0x7ff;
360 } else {
361 int byteshift = fb->bits_per_pixel >> 4;
362 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
363 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
364 }
365 } else {
366 int offset = crtc->y * pitch_pixels + crtc->x;
367 switch (fb->bits_per_pixel) {
368 case 8:
369 default:
370 offset *= 1;
371 break;
372 case 15:
373 case 16:
374 offset *= 2;
375 break;
376 case 24:
377 offset *= 3;
378 break;
379 case 32:
380 offset *= 4;
381 break;
382 }
383 base += offset;
384 }
385 base &= ~7;
386 }
387
388 spin_lock_irqsave(&dev->event_lock, flags);
389 work->new_crtc_base = base;
390 spin_unlock_irqrestore(&dev->event_lock, flags);
391
392 /* update crtc fb */
393 crtc->fb = fb;
394
395 r = drm_vblank_get(dev, radeon_crtc->crtc_id);
396 if (r) {
397 DRM_ERROR("failed to get vblank before flip\n");
398 goto pflip_cleanup1;
399 }
400
401 /* 32 ought to cover us */
402 r = radeon_ring_lock(rdev, 32);
403 if (r) {
404 DRM_ERROR("failed to lock the ring before flip\n");
405 goto pflip_cleanup2;
406 }
407
408 /* emit the fence */
409 radeon_fence_emit(rdev, fence);
410 /* set the proper interrupt */
411 radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
412 /* fire the ring */
413 radeon_ring_unlock_commit(rdev);
414
415 return 0;
416
417pflip_cleanup2:
418 drm_vblank_put(dev, radeon_crtc->crtc_id);
419
420pflip_cleanup1:
421 r = radeon_bo_reserve(rbo, false);
422 if (unlikely(r != 0)) {
423 DRM_ERROR("failed to reserve new rbo in error path\n");
424 goto pflip_cleanup;
425 }
426 r = radeon_bo_unpin(rbo);
427 if (unlikely(r != 0)) {
428 radeon_bo_unreserve(rbo);
429 r = -EINVAL;
430 DRM_ERROR("failed to unpin new rbo in error path\n");
431 goto pflip_cleanup;
432 }
433 radeon_bo_unreserve(rbo);
434
435pflip_cleanup:
436 spin_lock_irqsave(&dev->event_lock, flags);
437 radeon_crtc->unpin_work = NULL;
438 spin_unlock_irqrestore(&dev->event_lock, flags);
439 radeon_fence_unref(&fence);
440 kfree(work);
441
442 return r;
443}
444
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200445static const struct drm_crtc_funcs radeon_crtc_funcs = {
446 .cursor_set = radeon_crtc_cursor_set,
447 .cursor_move = radeon_crtc_cursor_move,
448 .gamma_set = radeon_crtc_gamma_set,
449 .set_config = drm_crtc_helper_set_config,
450 .destroy = radeon_crtc_destroy,
Alex Deucher6f34be52010-11-21 10:59:01 -0500451 .page_flip = radeon_crtc_page_flip,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200452};
453
454static void radeon_crtc_init(struct drm_device *dev, int index)
455{
456 struct radeon_device *rdev = dev->dev_private;
457 struct radeon_crtc *radeon_crtc;
458 int i;
459
460 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
461 if (radeon_crtc == NULL)
462 return;
463
464 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
465
466 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
467 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200468 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200469
Dave Airlie785b93e2009-08-28 15:46:53 +1000470#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200471 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
472 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
473 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000474#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200475
476 for (i = 0; i < 256; i++) {
477 radeon_crtc->lut_r[i] = i << 2;
478 radeon_crtc->lut_g[i] = i << 2;
479 radeon_crtc->lut_b[i] = i << 2;
480 }
481
482 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
483 radeon_atombios_init_crtc(dev, radeon_crtc);
484 else
485 radeon_legacy_init_crtc(dev, radeon_crtc);
486}
487
Alex Deucherbf982eb2010-11-22 17:56:24 -0500488static const char *encoder_names[36] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200489 "NONE",
490 "INTERNAL_LVDS",
491 "INTERNAL_TMDS1",
492 "INTERNAL_TMDS2",
493 "INTERNAL_DAC1",
494 "INTERNAL_DAC2",
495 "INTERNAL_SDVOA",
496 "INTERNAL_SDVOB",
497 "SI170B",
498 "CH7303",
499 "CH7301",
500 "INTERNAL_DVO1",
501 "EXTERNAL_SDVOA",
502 "EXTERNAL_SDVOB",
503 "TITFP513",
504 "INTERNAL_LVTM1",
505 "VT1623",
506 "HDMI_SI1930",
507 "HDMI_INTERNAL",
508 "INTERNAL_KLDSCP_TMDS1",
509 "INTERNAL_KLDSCP_DVO1",
510 "INTERNAL_KLDSCP_DAC1",
511 "INTERNAL_KLDSCP_DAC2",
512 "SI178",
513 "MVPU_FPGA",
514 "INTERNAL_DDI",
515 "VT1625",
516 "HDMI_SI1932",
517 "DP_AN9801",
518 "DP_DP501",
519 "INTERNAL_UNIPHY",
520 "INTERNAL_KLDSCP_LVTMA",
521 "INTERNAL_UNIPHY1",
522 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500523 "NUTMEG",
524 "TRAVIS",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200525};
526
Alex Deucher196c58d2010-01-07 14:22:32 -0500527static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200528 "Unknown",
529 "VGA",
530 "DVI-I",
531 "DVI-D",
532 "DVI-A",
533 "Composite",
534 "S-video",
535 "LVDS",
536 "Component",
537 "DIN",
538 "DisplayPort",
539 "HDMI-A",
540 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500541 "TV",
542 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543};
544
Alex Deuchercbd46232010-06-07 02:24:54 -0400545static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500546 "HPD1",
547 "HPD2",
548 "HPD3",
549 "HPD4",
550 "HPD5",
551 "HPD6",
552};
553
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200554static void radeon_print_display_setup(struct drm_device *dev)
555{
556 struct drm_connector *connector;
557 struct radeon_connector *radeon_connector;
558 struct drm_encoder *encoder;
559 struct radeon_encoder *radeon_encoder;
560 uint32_t devices;
561 int i = 0;
562
563 DRM_INFO("Radeon Display Connectors\n");
564 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
565 radeon_connector = to_radeon_connector(connector);
566 DRM_INFO("Connector %d:\n", i);
567 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500568 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
569 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000570 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200571 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
572 radeon_connector->ddc_bus->rec.mask_clk_reg,
573 radeon_connector->ddc_bus->rec.mask_data_reg,
574 radeon_connector->ddc_bus->rec.a_clk_reg,
575 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500576 radeon_connector->ddc_bus->rec.en_clk_reg,
577 radeon_connector->ddc_bus->rec.en_data_reg,
578 radeon_connector->ddc_bus->rec.y_clk_reg,
579 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000580 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400581 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000582 radeon_connector->router.ddc_mux_control_pin,
583 radeon_connector->router.ddc_mux_state);
584 if (radeon_connector->router.cd_valid)
585 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
586 radeon_connector->router.cd_mux_control_pin,
587 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000588 } else {
589 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
590 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
591 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
592 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
593 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
594 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
595 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
596 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200597 DRM_INFO(" Encoders:\n");
598 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
599 radeon_encoder = to_radeon_encoder(encoder);
600 devices = radeon_encoder->devices & radeon_connector->devices;
601 if (devices) {
602 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
603 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
604 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
605 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
606 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
607 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
608 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
609 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
610 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
611 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
612 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
613 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
614 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
615 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
616 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
617 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400618 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
619 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200620 if (devices & ATOM_DEVICE_TV1_SUPPORT)
621 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
622 if (devices & ATOM_DEVICE_CV_SUPPORT)
623 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
624 }
625 }
626 i++;
627 }
628}
629
Dave Airlie4ce001a2009-08-13 16:32:14 +1000630static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200631{
632 struct radeon_device *rdev = dev->dev_private;
633 struct drm_connector *drm_connector;
634 bool ret = false;
635
636 if (rdev->bios) {
637 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400638 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
639 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200640 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500641 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200642 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500643 if (ret == false)
644 ret = radeon_get_legacy_connector_info_from_table(dev);
645 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200646 } else {
647 if (!ASIC_IS_AVIVO(rdev))
648 ret = radeon_get_legacy_connector_info_from_table(dev);
649 }
650 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000651 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200652 radeon_print_display_setup(dev);
653 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
654 radeon_ddc_dump(drm_connector);
655 }
656
657 return ret;
658}
659
660int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
661{
Alex Deucher3c537882010-02-05 04:21:19 -0500662 struct drm_device *dev = radeon_connector->base.dev;
663 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200664 int ret = 0;
665
Alex Deucher26b5bc92010-08-05 21:21:18 -0400666 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000667 if (radeon_connector->router.ddc_valid)
668 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400669
Alex Deucher196c58d2010-01-07 14:22:32 -0500670 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
671 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000672 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Dave Airlie7a15cbd2010-01-14 11:42:17 +1000673 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
674 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucher9fa05c92009-11-27 13:01:46 -0500675 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000676 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200677 if (!radeon_connector->ddc_bus)
678 return -1;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000679 if (!radeon_connector->edid) {
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400680 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400681 }
Alex Deucher3c537882010-02-05 04:21:19 -0500682 /* some servers provide a hardcoded edid in rom for KVMs */
683 if (!radeon_connector->edid)
684 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400685 if (radeon_connector->edid) {
686 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
687 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200688 return ret;
689 }
690 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000691 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200692}
693
694static int radeon_ddc_dump(struct drm_connector *connector)
695{
696 struct edid *edid;
697 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
698 int ret = 0;
699
Alex Deucher26b5bc92010-08-05 21:21:18 -0400700 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000701 if (radeon_connector->router.ddc_valid)
702 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400703
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200704 if (!radeon_connector->ddc_bus)
705 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200706 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200707 if (edid) {
708 kfree(edid);
709 }
710 return ret;
711}
712
713static inline uint32_t radeon_div(uint64_t n, uint32_t d)
714{
715 uint64_t mod;
716
717 n += d / 2;
718
719 mod = do_div(n, d);
720 return n;
721}
722
Alex Deucher48dfaae2010-09-29 11:37:41 -0400723void radeon_compute_pll(struct radeon_pll *pll,
724 uint64_t freq,
725 uint32_t *dot_clock_p,
726 uint32_t *fb_div_p,
727 uint32_t *frac_fb_div_p,
728 uint32_t *ref_div_p,
729 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200730{
731 uint32_t min_ref_div = pll->min_ref_div;
732 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500733 uint32_t min_post_div = pll->min_post_div;
734 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200735 uint32_t min_fractional_feed_div = 0;
736 uint32_t max_fractional_feed_div = 0;
737 uint32_t best_vco = pll->best_vco;
738 uint32_t best_post_div = 1;
739 uint32_t best_ref_div = 1;
740 uint32_t best_feedback_div = 1;
741 uint32_t best_frac_feedback_div = 0;
742 uint32_t best_freq = -1;
743 uint32_t best_error = 0xffffffff;
744 uint32_t best_vco_diff = 1;
745 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500746 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200747
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000748 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 +0200749 freq = freq * 1000;
750
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500751 if (pll->flags & RADEON_PLL_IS_LCD) {
752 pll_out_min = pll->lcd_pll_out_min;
753 pll_out_max = pll->lcd_pll_out_max;
754 } else {
755 pll_out_min = pll->pll_out_min;
756 pll_out_max = pll->pll_out_max;
757 }
758
Alex Deucherfc103322010-01-19 17:16:10 -0500759 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200760 min_ref_div = max_ref_div = pll->reference_div;
761 else {
762 while (min_ref_div < max_ref_div-1) {
763 uint32_t mid = (min_ref_div + max_ref_div) / 2;
764 uint32_t pll_in = pll->reference_freq / mid;
765 if (pll_in < pll->pll_in_min)
766 max_ref_div = mid;
767 else if (pll_in > pll->pll_in_max)
768 min_ref_div = mid;
769 else
770 break;
771 }
772 }
773
Alex Deucherfc103322010-01-19 17:16:10 -0500774 if (pll->flags & RADEON_PLL_USE_POST_DIV)
775 min_post_div = max_post_div = pll->post_div;
776
777 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200778 min_fractional_feed_div = pll->min_frac_feedback_div;
779 max_fractional_feed_div = pll->max_frac_feedback_div;
780 }
781
Alex Deucherbcac54d2010-09-29 11:37:39 -0400782 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200783 uint32_t ref_div;
784
Alex Deucherfc103322010-01-19 17:16:10 -0500785 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200786 continue;
787
788 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500789 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200790 if ((post_div == 5) ||
791 (post_div == 7) ||
792 (post_div == 9) ||
793 (post_div == 10) ||
794 (post_div == 11) ||
795 (post_div == 13) ||
796 (post_div == 14) ||
797 (post_div == 15))
798 continue;
799 }
800
801 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
802 uint32_t feedback_div, current_freq = 0, error, vco_diff;
803 uint32_t pll_in = pll->reference_freq / ref_div;
804 uint32_t min_feed_div = pll->min_feedback_div;
805 uint32_t max_feed_div = pll->max_feedback_div + 1;
806
807 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
808 continue;
809
810 while (min_feed_div < max_feed_div) {
811 uint32_t vco;
812 uint32_t min_frac_feed_div = min_fractional_feed_div;
813 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
814 uint32_t frac_feedback_div;
815 uint64_t tmp;
816
817 feedback_div = (min_feed_div + max_feed_div) / 2;
818
819 tmp = (uint64_t)pll->reference_freq * feedback_div;
820 vco = radeon_div(tmp, ref_div);
821
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500822 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200823 min_feed_div = feedback_div + 1;
824 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500825 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200826 max_feed_div = feedback_div;
827 continue;
828 }
829
830 while (min_frac_feed_div < max_frac_feed_div) {
831 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
832 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
833 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
834 current_freq = radeon_div(tmp, ref_div * post_div);
835
Alex Deucherfc103322010-01-19 17:16:10 -0500836 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +0200837 if (freq < current_freq)
838 error = 0xffffffff;
839 else
840 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -0400841 } else
842 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200843 vco_diff = abs(vco - best_vco);
844
845 if ((best_vco == 0 && error < best_error) ||
846 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +0200847 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +1000848 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200849 best_post_div = post_div;
850 best_ref_div = ref_div;
851 best_feedback_div = feedback_div;
852 best_frac_feedback_div = frac_feedback_div;
853 best_freq = current_freq;
854 best_error = error;
855 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +1000856 } else if (current_freq == freq) {
857 if (best_freq == -1) {
858 best_post_div = post_div;
859 best_ref_div = ref_div;
860 best_feedback_div = feedback_div;
861 best_frac_feedback_div = frac_feedback_div;
862 best_freq = current_freq;
863 best_error = error;
864 best_vco_diff = vco_diff;
865 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
866 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
867 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
868 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
869 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
870 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
871 best_post_div = post_div;
872 best_ref_div = ref_div;
873 best_feedback_div = feedback_div;
874 best_frac_feedback_div = frac_feedback_div;
875 best_freq = current_freq;
876 best_error = error;
877 best_vco_diff = vco_diff;
878 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200879 }
880 if (current_freq < freq)
881 min_frac_feed_div = frac_feedback_div + 1;
882 else
883 max_frac_feed_div = frac_feedback_div;
884 }
885 if (current_freq < freq)
886 min_feed_div = feedback_div + 1;
887 else
888 max_feed_div = feedback_div;
889 }
890 }
891 }
892
893 *dot_clock_p = best_freq / 10000;
894 *fb_div_p = best_feedback_div;
895 *frac_fb_div_p = best_frac_feedback_div;
896 *ref_div_p = best_ref_div;
897 *post_div_p = best_post_div;
898}
899
900static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
901{
902 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200903
Dave Airlie29d08b32010-09-27 16:17:17 +1000904 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000905 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +1000906 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200907 drm_framebuffer_cleanup(fb);
908 kfree(radeon_fb);
909}
910
911static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
912 struct drm_file *file_priv,
913 unsigned int *handle)
914{
915 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
916
917 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
918}
919
920static const struct drm_framebuffer_funcs radeon_fb_funcs = {
921 .destroy = radeon_user_framebuffer_destroy,
922 .create_handle = radeon_user_framebuffer_create_handle,
923};
924
Dave Airlie38651672010-03-30 05:34:13 +0000925void
926radeon_framebuffer_init(struct drm_device *dev,
927 struct radeon_framebuffer *rfb,
928 struct drm_mode_fb_cmd *mode_cmd,
929 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200930{
Dave Airlie38651672010-03-30 05:34:13 +0000931 rfb->obj = obj;
932 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
933 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200934}
935
936static struct drm_framebuffer *
937radeon_user_framebuffer_create(struct drm_device *dev,
938 struct drm_file *file_priv,
939 struct drm_mode_fb_cmd *mode_cmd)
940{
941 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +0000942 struct radeon_framebuffer *radeon_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200943
944 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100945 if (obj == NULL) {
946 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
947 "can't create framebuffer\n", mode_cmd->handle);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100948 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100949 }
Dave Airlie38651672010-03-30 05:34:13 +0000950
951 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100952 if (radeon_fb == NULL)
953 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +0000954
955 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
956
957 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200958}
959
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000960static void radeon_output_poll_changed(struct drm_device *dev)
961{
962 struct radeon_device *rdev = dev->dev_private;
963 radeon_fb_output_poll_changed(rdev);
964}
965
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200966static const struct drm_mode_config_funcs radeon_mode_funcs = {
967 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000968 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200969};
970
Dave Airlie445282d2009-09-09 17:40:54 +1000971struct drm_prop_enum_list {
972 int type;
973 char *name;
974};
975
976static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
977{ { 0, "driver" },
978 { 1, "bios" },
979};
980
981static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
982{ { TV_STD_NTSC, "ntsc" },
983 { TV_STD_PAL, "pal" },
984 { TV_STD_PAL_M, "pal-m" },
985 { TV_STD_PAL_60, "pal-60" },
986 { TV_STD_NTSC_J, "ntsc-j" },
987 { TV_STD_SCART_PAL, "scart-pal" },
988 { TV_STD_PAL_CN, "pal-cn" },
989 { TV_STD_SECAM, "secam" },
990};
991
Alex Deucher5b1714d2010-08-03 19:59:20 -0400992static struct drm_prop_enum_list radeon_underscan_enum_list[] =
993{ { UNDERSCAN_OFF, "off" },
994 { UNDERSCAN_ON, "on" },
995 { UNDERSCAN_AUTO, "auto" },
996};
997
Alex Deucherd79766f2009-12-17 19:00:29 -0500998static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +1000999{
1000 int i, sz;
1001
1002 if (rdev->is_atom_bios) {
1003 rdev->mode_info.coherent_mode_property =
1004 drm_property_create(rdev->ddev,
1005 DRM_MODE_PROP_RANGE,
1006 "coherent", 2);
1007 if (!rdev->mode_info.coherent_mode_property)
1008 return -ENOMEM;
1009
1010 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -05001011 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +10001012 }
1013
1014 if (!ASIC_IS_AVIVO(rdev)) {
1015 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1016 rdev->mode_info.tmds_pll_property =
1017 drm_property_create(rdev->ddev,
1018 DRM_MODE_PROP_ENUM,
1019 "tmds_pll", sz);
1020 for (i = 0; i < sz; i++) {
1021 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
1022 i,
1023 radeon_tmds_pll_enum_list[i].type,
1024 radeon_tmds_pll_enum_list[i].name);
1025 }
1026 }
1027
1028 rdev->mode_info.load_detect_property =
1029 drm_property_create(rdev->ddev,
1030 DRM_MODE_PROP_RANGE,
1031 "load detection", 2);
1032 if (!rdev->mode_info.load_detect_property)
1033 return -ENOMEM;
1034 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -05001035 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +10001036
1037 drm_mode_create_scaling_mode_property(rdev->ddev);
1038
1039 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1040 rdev->mode_info.tv_std_property =
1041 drm_property_create(rdev->ddev,
1042 DRM_MODE_PROP_ENUM,
1043 "tv standard", sz);
1044 for (i = 0; i < sz; i++) {
1045 drm_property_add_enum(rdev->mode_info.tv_std_property,
1046 i,
1047 radeon_tv_std_enum_list[i].type,
1048 radeon_tv_std_enum_list[i].name);
1049 }
1050
Alex Deucher5b1714d2010-08-03 19:59:20 -04001051 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1052 rdev->mode_info.underscan_property =
1053 drm_property_create(rdev->ddev,
1054 DRM_MODE_PROP_ENUM,
1055 "underscan", sz);
1056 for (i = 0; i < sz; i++) {
1057 drm_property_add_enum(rdev->mode_info.underscan_property,
1058 i,
1059 radeon_underscan_enum_list[i].type,
1060 radeon_underscan_enum_list[i].name);
1061 }
1062
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001063 rdev->mode_info.underscan_hborder_property =
1064 drm_property_create(rdev->ddev,
1065 DRM_MODE_PROP_RANGE,
1066 "underscan hborder", 2);
1067 if (!rdev->mode_info.underscan_hborder_property)
1068 return -ENOMEM;
1069 rdev->mode_info.underscan_hborder_property->values[0] = 0;
1070 rdev->mode_info.underscan_hborder_property->values[1] = 128;
1071
1072 rdev->mode_info.underscan_vborder_property =
1073 drm_property_create(rdev->ddev,
1074 DRM_MODE_PROP_RANGE,
1075 "underscan vborder", 2);
1076 if (!rdev->mode_info.underscan_vborder_property)
1077 return -ENOMEM;
1078 rdev->mode_info.underscan_vborder_property->values[0] = 0;
1079 rdev->mode_info.underscan_vborder_property->values[1] = 128;
1080
Dave Airlie445282d2009-09-09 17:40:54 +10001081 return 0;
1082}
1083
Alex Deucherf46c0122010-03-31 00:33:27 -04001084void radeon_update_display_priority(struct radeon_device *rdev)
1085{
1086 /* adjustment options for the display watermarks */
1087 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1088 /* set display priority to high for r3xx, rv515 chips
1089 * this avoids flickering due to underflow to the
1090 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001091 * Don't force high on rs4xx igp chips as it seems to
1092 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001093 */
Alex Deucher45737442010-05-20 11:26:11 -04001094 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1095 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001096 rdev->disp_priority = 2;
1097 else
1098 rdev->disp_priority = 0;
1099 } else
1100 rdev->disp_priority = radeon_disp_priority;
1101
1102}
1103
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001104int radeon_modeset_init(struct radeon_device *rdev)
1105{
Alex Deucher18917b62010-02-01 16:02:25 -05001106 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001107 int ret;
1108
1109 drm_mode_config_init(rdev->ddev);
1110 rdev->mode_info.mode_config_initialized = true;
1111
1112 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
1113
1114 if (ASIC_IS_AVIVO(rdev)) {
1115 rdev->ddev->mode_config.max_width = 8192;
1116 rdev->ddev->mode_config.max_height = 8192;
1117 } else {
1118 rdev->ddev->mode_config.max_width = 4096;
1119 rdev->ddev->mode_config.max_height = 4096;
1120 }
1121
1122 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1123
Dave Airlie445282d2009-09-09 17:40:54 +10001124 ret = radeon_modeset_create_props(rdev);
1125 if (ret) {
1126 return ret;
1127 }
Dave Airliedfee5612009-10-02 09:19:09 +10001128
Alex Deucherf376b942010-08-05 21:21:16 -04001129 /* init i2c buses */
1130 radeon_i2c_init(rdev);
1131
Alex Deucher3c537882010-02-05 04:21:19 -05001132 /* check combios for a valid hardcoded EDID - Sun servers */
1133 if (!rdev->is_atom_bios) {
1134 /* check for hardcoded EDID in BIOS */
1135 radeon_combios_check_hardcoded_edid(rdev);
1136 }
1137
Dave Airliedfee5612009-10-02 09:19:09 +10001138 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001139 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001140 radeon_crtc_init(rdev->ddev, i);
1141 }
1142
1143 /* okay we should have all the bios connectors */
1144 ret = radeon_setup_enc_conn(rdev->ddev);
1145 if (!ret) {
1146 return ret;
1147 }
Alex Deucherd4877cf2009-12-04 16:56:37 -05001148 /* initialize hpd */
1149 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001150
Alex Deucherce8f5372010-05-07 15:10:16 -04001151 /* Initialize power management */
1152 radeon_pm_init(rdev);
1153
Dave Airlie38651672010-03-30 05:34:13 +00001154 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001155 drm_kms_helper_poll_init(rdev->ddev);
1156
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001157 return 0;
1158}
1159
1160void radeon_modeset_fini(struct radeon_device *rdev)
1161{
Dave Airlie38651672010-03-30 05:34:13 +00001162 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001163 kfree(rdev->mode_info.bios_hardcoded_edid);
Alex Deucherce8f5372010-05-07 15:10:16 -04001164 radeon_pm_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001165
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001166 if (rdev->mode_info.mode_config_initialized) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001167 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001168 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001169 drm_mode_config_cleanup(rdev->ddev);
1170 rdev->mode_info.mode_config_initialized = false;
1171 }
Alex Deucherf376b942010-08-05 21:21:16 -04001172 /* free i2c buses */
1173 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001174}
1175
Alex Deucher039ed2d2010-08-20 11:57:19 -04001176static bool is_hdtv_mode(struct drm_display_mode *mode)
1177{
1178 /* try and guess if this is a tv or a monitor */
1179 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1180 (mode->vdisplay == 576) || /* 576p */
1181 (mode->vdisplay == 720) || /* 720p */
1182 (mode->vdisplay == 1080)) /* 1080p */
1183 return true;
1184 else
1185 return false;
1186}
1187
Jerome Glissec93bb852009-07-13 21:04:08 +02001188bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1189 struct drm_display_mode *mode,
1190 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001191{
Jerome Glissec93bb852009-07-13 21:04:08 +02001192 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001193 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001194 struct drm_encoder *encoder;
1195 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1196 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001197 struct drm_connector *connector;
1198 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001199 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001200 u32 src_v = 1, dst_v = 1;
1201 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001202
Alex Deucher5b1714d2010-08-03 19:59:20 -04001203 radeon_crtc->h_border = 0;
1204 radeon_crtc->v_border = 0;
1205
Jerome Glissec93bb852009-07-13 21:04:08 +02001206 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001207 if (encoder->crtc != crtc)
1208 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001209 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001210 connector = radeon_get_connector_for_encoder(encoder);
1211 radeon_connector = to_radeon_connector(connector);
1212
Jerome Glissec93bb852009-07-13 21:04:08 +02001213 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001214 /* set scaling */
1215 if (radeon_encoder->rmx_type == RMX_OFF)
1216 radeon_crtc->rmx_type = RMX_OFF;
1217 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1218 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1219 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1220 else
1221 radeon_crtc->rmx_type = RMX_OFF;
1222 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001223 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001224 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001225 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001226 src_v = crtc->mode.vdisplay;
1227 dst_v = radeon_crtc->native_mode.vdisplay;
1228 src_h = crtc->mode.hdisplay;
1229 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001230
1231 /* fix up for overscan on hdmi */
1232 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001233 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001234 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1235 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001236 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1237 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001238 if (radeon_encoder->underscan_hborder != 0)
1239 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1240 else
1241 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1242 if (radeon_encoder->underscan_vborder != 0)
1243 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1244 else
1245 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001246 radeon_crtc->rmx_type = RMX_FULL;
1247 src_v = crtc->mode.vdisplay;
1248 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1249 src_h = crtc->mode.hdisplay;
1250 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1251 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001252 first = false;
1253 } else {
1254 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1255 /* WARNING: Right now this can't happen but
1256 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001257 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001258 * (ie all encoder can work with the same
1259 * scaling).
1260 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001261 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001262 return false;
1263 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001264 }
1265 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001266 if (radeon_crtc->rmx_type != RMX_OFF) {
1267 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001268 a.full = dfixed_const(src_v);
1269 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001270 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001271 a.full = dfixed_const(src_h);
1272 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001273 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001274 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001275 radeon_crtc->vsc.full = dfixed_const(1);
1276 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001277 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001278 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001279}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001280
1281/*
1282 * Retrieve current video scanout position of crtc on a given gpu.
1283 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001284 * \param dev Device to query.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001285 * \param crtc Crtc to query.
1286 * \param *vpos Location where vertical scanout position should be stored.
1287 * \param *hpos Location where horizontal scanout position should go.
1288 *
1289 * Returns vpos as a positive number while in active scanout area.
1290 * Returns vpos as a negative number inside vblank, counting the number
1291 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1292 * until start of active scanout / end of vblank."
1293 *
1294 * \return Flags, or'ed together as follows:
1295 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001296 * DRM_SCANOUTPOS_VALID = Query successfull.
1297 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1298 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
Mario Kleiner6383cf72010-10-05 19:57:36 -04001299 * this flag means that returned position may be offset by a constant but
1300 * unknown small number of scanlines wrt. real scanout position.
1301 *
1302 */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001303int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
Mario Kleiner6383cf72010-10-05 19:57:36 -04001304{
1305 u32 stat_crtc = 0, vbl = 0, position = 0;
1306 int vbl_start, vbl_end, vtotal, ret = 0;
1307 bool in_vbl = true;
1308
Mario Kleinerf5a80202010-10-23 04:42:17 +02001309 struct radeon_device *rdev = dev->dev_private;
1310
Mario Kleiner6383cf72010-10-05 19:57:36 -04001311 if (ASIC_IS_DCE4(rdev)) {
1312 if (crtc == 0) {
1313 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1314 EVERGREEN_CRTC0_REGISTER_OFFSET);
1315 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1316 EVERGREEN_CRTC0_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001317 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001318 }
1319 if (crtc == 1) {
1320 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1321 EVERGREEN_CRTC1_REGISTER_OFFSET);
1322 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1323 EVERGREEN_CRTC1_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001324 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001325 }
1326 if (crtc == 2) {
1327 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1328 EVERGREEN_CRTC2_REGISTER_OFFSET);
1329 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1330 EVERGREEN_CRTC2_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001331 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001332 }
1333 if (crtc == 3) {
1334 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1335 EVERGREEN_CRTC3_REGISTER_OFFSET);
1336 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1337 EVERGREEN_CRTC3_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001338 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001339 }
1340 if (crtc == 4) {
1341 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1342 EVERGREEN_CRTC4_REGISTER_OFFSET);
1343 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1344 EVERGREEN_CRTC4_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001345 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001346 }
1347 if (crtc == 5) {
1348 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1349 EVERGREEN_CRTC5_REGISTER_OFFSET);
1350 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1351 EVERGREEN_CRTC5_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001352 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001353 }
1354 } else if (ASIC_IS_AVIVO(rdev)) {
1355 if (crtc == 0) {
1356 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1357 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001358 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001359 }
1360 if (crtc == 1) {
1361 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1362 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001363 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001364 }
1365 } else {
1366 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1367 if (crtc == 0) {
1368 /* Assume vbl_end == 0, get vbl_start from
1369 * upper 16 bits.
1370 */
1371 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1372 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1373 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1374 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1375 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1376 if (!(stat_crtc & 1))
1377 in_vbl = false;
1378
Mario Kleinerf5a80202010-10-23 04:42:17 +02001379 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001380 }
1381 if (crtc == 1) {
1382 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1383 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1384 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1385 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1386 if (!(stat_crtc & 1))
1387 in_vbl = false;
1388
Mario Kleinerf5a80202010-10-23 04:42:17 +02001389 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001390 }
1391 }
1392
1393 /* Decode into vertical and horizontal scanout position. */
1394 *vpos = position & 0x1fff;
1395 *hpos = (position >> 16) & 0x1fff;
1396
1397 /* Valid vblank area boundaries from gpu retrieved? */
1398 if (vbl > 0) {
1399 /* Yes: Decode. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001400 ret |= DRM_SCANOUTPOS_ACCURATE;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001401 vbl_start = vbl & 0x1fff;
1402 vbl_end = (vbl >> 16) & 0x1fff;
1403 }
1404 else {
1405 /* No: Fake something reasonable which gives at least ok results. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001406 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001407 vbl_end = 0;
1408 }
1409
1410 /* Test scanout position against vblank region. */
1411 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1412 in_vbl = false;
1413
1414 /* Check if inside vblank area and apply corrective offsets:
1415 * vpos will then be >=0 in video scanout area, but negative
1416 * within vblank area, counting down the number of lines until
1417 * start of scanout.
1418 */
1419
1420 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1421 if (in_vbl && (*vpos >= vbl_start)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001422 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001423 *vpos = *vpos - vtotal;
1424 }
1425
1426 /* Correct for shifted end of vbl at vbl_end. */
1427 *vpos = *vpos - vbl_end;
1428
1429 /* In vblank? */
1430 if (in_vbl)
Mario Kleinerf5a80202010-10-23 04:42:17 +02001431 ret |= DRM_SCANOUTPOS_INVBL;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001432
1433 return ret;
1434}