blob: 07ac48162a13244f45dcc82105dc8c0eb5c7b638 [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 Deucherfee298f2011-01-06 21:19:30 -050071static void dce4_crtc_load_lut(struct drm_crtc *crtc)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050072{
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
Alex Deucherfee298f2011-01-06 21:19:30 -0500101static void dce5_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
108 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
109
110 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
111 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
112 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
113 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
114 NI_GRPH_PRESCALE_BYPASS);
115 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
116 NI_OVL_PRESCALE_BYPASS);
117 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
118 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
119 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
120
121 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
122
123 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
124 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
125 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
126
127 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
128 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
129 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
130
131 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
132 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
133
134 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
135 for (i = 0; i < 256; i++) {
136 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
137 (radeon_crtc->lut_r[i] << 20) |
138 (radeon_crtc->lut_g[i] << 10) |
139 (radeon_crtc->lut_b[i] << 0));
140 }
141
142 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
143 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
144 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
145 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
147 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
148 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
149 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
150 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
151 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
152 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
153 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
154 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
155 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
156 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
157 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
158
159}
160
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161static void legacy_crtc_load_lut(struct drm_crtc *crtc)
162{
163 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
164 struct drm_device *dev = crtc->dev;
165 struct radeon_device *rdev = dev->dev_private;
166 int i;
167 uint32_t dac2_cntl;
168
169 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
170 if (radeon_crtc->crtc_id == 0)
171 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
172 else
173 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
174 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
175
176 WREG8(RADEON_PALETTE_INDEX, 0);
177 for (i = 0; i < 256; i++) {
178 WREG32(RADEON_PALETTE_30_DATA,
179 (radeon_crtc->lut_r[i] << 20) |
180 (radeon_crtc->lut_g[i] << 10) |
181 (radeon_crtc->lut_b[i] << 0));
182 }
183}
184
185void radeon_crtc_load_lut(struct drm_crtc *crtc)
186{
187 struct drm_device *dev = crtc->dev;
188 struct radeon_device *rdev = dev->dev_private;
189
190 if (!crtc->enabled)
191 return;
192
Alex Deucherfee298f2011-01-06 21:19:30 -0500193 if (ASIC_IS_DCE5(rdev))
194 dce5_crtc_load_lut(crtc);
195 else if (ASIC_IS_DCE4(rdev))
196 dce4_crtc_load_lut(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500197 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200198 avivo_crtc_load_lut(crtc);
199 else
200 legacy_crtc_load_lut(crtc);
201}
202
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000203/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
205 u16 blue, int regno)
206{
207 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
208
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209 radeon_crtc->lut_r[regno] = red >> 6;
210 radeon_crtc->lut_g[regno] = green >> 6;
211 radeon_crtc->lut_b[regno] = blue >> 6;
212}
213
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000214/** Gets the color ramps on behalf of fbcon */
215void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
216 u16 *blue, int regno)
217{
218 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
219
220 *red = radeon_crtc->lut_r[regno] << 6;
221 *green = radeon_crtc->lut_g[regno] << 6;
222 *blue = radeon_crtc->lut_b[regno] << 6;
223}
224
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200225static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100226 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200227{
228 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100229 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000231 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100232 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000233 radeon_crtc->lut_r[i] = red[i] >> 6;
234 radeon_crtc->lut_g[i] = green[i] >> 6;
235 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200236 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200237 radeon_crtc_load_lut(crtc);
238}
239
240static void radeon_crtc_destroy(struct drm_crtc *crtc)
241{
242 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
243
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244 drm_crtc_cleanup(crtc);
245 kfree(radeon_crtc);
246}
247
Alex Deucher6f34be52010-11-21 10:59:01 -0500248/*
249 * Handle unpin events outside the interrupt handler proper.
250 */
251static void radeon_unpin_work_func(struct work_struct *__work)
252{
253 struct radeon_unpin_work *work =
254 container_of(__work, struct radeon_unpin_work, work);
255 int r;
256
257 /* unpin of the old buffer */
258 r = radeon_bo_reserve(work->old_rbo, false);
259 if (likely(r == 0)) {
260 r = radeon_bo_unpin(work->old_rbo);
261 if (unlikely(r != 0)) {
262 DRM_ERROR("failed to unpin buffer after flip\n");
263 }
264 radeon_bo_unreserve(work->old_rbo);
265 } else
266 DRM_ERROR("failed to reserve buffer after flip\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000267
268 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500269 kfree(work);
270}
271
272void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
273{
274 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
275 struct radeon_unpin_work *work;
276 struct drm_pending_vblank_event *e;
277 struct timeval now;
278 unsigned long flags;
279 u32 update_pending;
280 int vpos, hpos;
281
282 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
283 work = radeon_crtc->unpin_work;
284 if (work == NULL ||
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000285 (work->fence && !radeon_fence_signaled(work->fence))) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500286 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
287 return;
288 }
289 /* New pageflip, or just completion of a previous one? */
290 if (!radeon_crtc->deferred_flip_completion) {
291 /* do the flip (mmio) */
292 update_pending = radeon_page_flip(rdev, crtc_id, work->new_crtc_base);
293 } else {
294 /* This is just a completion of a flip queued in crtc
295 * at last invocation. Make sure we go directly to
296 * completion routine.
297 */
298 update_pending = 0;
299 radeon_crtc->deferred_flip_completion = 0;
300 }
301
302 /* Has the pageflip already completed in crtc, or is it certain
303 * to complete in this vblank?
304 */
305 if (update_pending &&
306 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id,
307 &vpos, &hpos)) &&
308 (vpos >=0) &&
309 (vpos < (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100)) {
310 /* crtc didn't flip in this target vblank interval,
311 * but flip is pending in crtc. It will complete it
312 * in next vblank interval, so complete the flip at
313 * next vblank irq.
314 */
315 radeon_crtc->deferred_flip_completion = 1;
316 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
317 return;
318 }
319
320 /* Pageflip (will be) certainly completed in this vblank. Clean up. */
321 radeon_crtc->unpin_work = NULL;
322
323 /* wakeup userspace */
324 if (work->event) {
325 e = work->event;
Mario Kleinerb6724402010-11-21 10:59:03 -0500326 e->event.sequence = drm_vblank_count_and_time(rdev->ddev, crtc_id, &now);
Alex Deucher6f34be52010-11-21 10:59:01 -0500327 e->event.tv_sec = now.tv_sec;
328 e->event.tv_usec = now.tv_usec;
329 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
330 wake_up_interruptible(&e->base.file_priv->event_wait);
331 }
332 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
333
334 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
335 radeon_fence_unref(&work->fence);
336 radeon_post_page_flip(work->rdev, work->crtc_id);
337 schedule_work(&work->work);
338}
339
340static int radeon_crtc_page_flip(struct drm_crtc *crtc,
341 struct drm_framebuffer *fb,
342 struct drm_pending_vblank_event *event)
343{
344 struct drm_device *dev = crtc->dev;
345 struct radeon_device *rdev = dev->dev_private;
346 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
347 struct radeon_framebuffer *old_radeon_fb;
348 struct radeon_framebuffer *new_radeon_fb;
349 struct drm_gem_object *obj;
350 struct radeon_bo *rbo;
Alex Deucher6f34be52010-11-21 10:59:01 -0500351 struct radeon_unpin_work *work;
352 unsigned long flags;
353 u32 tiling_flags, pitch_pixels;
354 u64 base;
355 int r;
356
357 work = kzalloc(sizeof *work, GFP_KERNEL);
358 if (work == NULL)
359 return -ENOMEM;
360
Alex Deucher6f34be52010-11-21 10:59:01 -0500361 work->event = event;
362 work->rdev = rdev;
363 work->crtc_id = radeon_crtc->crtc_id;
Alex Deucher6f34be52010-11-21 10:59:01 -0500364 old_radeon_fb = to_radeon_framebuffer(crtc->fb);
365 new_radeon_fb = to_radeon_framebuffer(fb);
366 /* schedule unpin of the old buffer */
367 obj = old_radeon_fb->obj;
Dave Airlie498c5552011-05-29 17:48:32 +1000368 /* take a reference to the old object */
369 drm_gem_object_reference(obj);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +0100370 rbo = gem_to_radeon_bo(obj);
Alex Deucher6f34be52010-11-21 10:59:01 -0500371 work->old_rbo = rbo;
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000372 obj = new_radeon_fb->obj;
373 rbo = gem_to_radeon_bo(obj);
374 if (rbo->tbo.sync_obj)
375 work->fence = radeon_fence_ref(rbo->tbo.sync_obj);
Alex Deucher6f34be52010-11-21 10:59:01 -0500376 INIT_WORK(&work->work, radeon_unpin_work_func);
377
378 /* We borrow the event spin lock for protecting unpin_work */
379 spin_lock_irqsave(&dev->event_lock, flags);
380 if (radeon_crtc->unpin_work) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500381 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000382 r = -EBUSY;
383 goto unlock_free;
Alex Deucher6f34be52010-11-21 10:59:01 -0500384 }
385 radeon_crtc->unpin_work = work;
386 radeon_crtc->deferred_flip_completion = 0;
387 spin_unlock_irqrestore(&dev->event_lock, flags);
388
389 /* pin the new buffer */
Alex Deucher6f34be52010-11-21 10:59:01 -0500390 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
391 work->old_rbo, rbo);
392
393 r = radeon_bo_reserve(rbo, false);
394 if (unlikely(r != 0)) {
395 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
396 goto pflip_cleanup;
397 }
398 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &base);
399 if (unlikely(r != 0)) {
400 radeon_bo_unreserve(rbo);
401 r = -EINVAL;
402 DRM_ERROR("failed to pin new rbo buffer before flip\n");
403 goto pflip_cleanup;
404 }
405 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
406 radeon_bo_unreserve(rbo);
407
408 if (!ASIC_IS_AVIVO(rdev)) {
409 /* crtc offset is from display base addr not FB location */
410 base -= radeon_crtc->legacy_display_base_addr;
411 pitch_pixels = fb->pitch / (fb->bits_per_pixel / 8);
412
413 if (tiling_flags & RADEON_TILING_MACRO) {
414 if (ASIC_IS_R300(rdev)) {
415 base &= ~0x7ff;
416 } else {
417 int byteshift = fb->bits_per_pixel >> 4;
418 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
419 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
420 }
421 } else {
422 int offset = crtc->y * pitch_pixels + crtc->x;
423 switch (fb->bits_per_pixel) {
424 case 8:
425 default:
426 offset *= 1;
427 break;
428 case 15:
429 case 16:
430 offset *= 2;
431 break;
432 case 24:
433 offset *= 3;
434 break;
435 case 32:
436 offset *= 4;
437 break;
438 }
439 base += offset;
440 }
441 base &= ~7;
442 }
443
444 spin_lock_irqsave(&dev->event_lock, flags);
445 work->new_crtc_base = base;
446 spin_unlock_irqrestore(&dev->event_lock, flags);
447
448 /* update crtc fb */
449 crtc->fb = fb;
450
451 r = drm_vblank_get(dev, radeon_crtc->crtc_id);
452 if (r) {
453 DRM_ERROR("failed to get vblank before flip\n");
454 goto pflip_cleanup1;
455 }
456
Alex Deucher6f34be52010-11-21 10:59:01 -0500457 /* set the proper interrupt */
458 radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500459
460 return 0;
461
Alex Deucher6f34be52010-11-21 10:59:01 -0500462pflip_cleanup1:
Michel Dänzerd0254d52011-07-13 15:18:10 +0000463 if (unlikely(radeon_bo_reserve(rbo, false) != 0)) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500464 DRM_ERROR("failed to reserve new rbo in error path\n");
465 goto pflip_cleanup;
466 }
Michel Dänzerd0254d52011-07-13 15:18:10 +0000467 if (unlikely(radeon_bo_unpin(rbo) != 0)) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500468 DRM_ERROR("failed to unpin new rbo in error path\n");
Alex Deucher6f34be52010-11-21 10:59:01 -0500469 }
470 radeon_bo_unreserve(rbo);
471
472pflip_cleanup:
473 spin_lock_irqsave(&dev->event_lock, flags);
474 radeon_crtc->unpin_work = NULL;
Dave Airlie498c5552011-05-29 17:48:32 +1000475unlock_free:
Alex Deucher6f34be52010-11-21 10:59:01 -0500476 spin_unlock_irqrestore(&dev->event_lock, flags);
Michel Dänzerdb318d72011-09-13 11:29:12 +0200477 drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000478 radeon_fence_unref(&work->fence);
Alex Deucher6f34be52010-11-21 10:59:01 -0500479 kfree(work);
480
481 return r;
482}
483
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200484static const struct drm_crtc_funcs radeon_crtc_funcs = {
485 .cursor_set = radeon_crtc_cursor_set,
486 .cursor_move = radeon_crtc_cursor_move,
487 .gamma_set = radeon_crtc_gamma_set,
488 .set_config = drm_crtc_helper_set_config,
489 .destroy = radeon_crtc_destroy,
Alex Deucher6f34be52010-11-21 10:59:01 -0500490 .page_flip = radeon_crtc_page_flip,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200491};
492
493static void radeon_crtc_init(struct drm_device *dev, int index)
494{
495 struct radeon_device *rdev = dev->dev_private;
496 struct radeon_crtc *radeon_crtc;
497 int i;
498
499 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
500 if (radeon_crtc == NULL)
501 return;
502
503 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
504
505 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
506 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200507 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200508
Dave Airlie785b93e2009-08-28 15:46:53 +1000509#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200510 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
511 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
512 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000513#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200514
515 for (i = 0; i < 256; i++) {
516 radeon_crtc->lut_r[i] = i << 2;
517 radeon_crtc->lut_g[i] = i << 2;
518 radeon_crtc->lut_b[i] = i << 2;
519 }
520
521 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
522 radeon_atombios_init_crtc(dev, radeon_crtc);
523 else
524 radeon_legacy_init_crtc(dev, radeon_crtc);
525}
526
Alex Deucherbf982eb2010-11-22 17:56:24 -0500527static const char *encoder_names[36] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200528 "NONE",
529 "INTERNAL_LVDS",
530 "INTERNAL_TMDS1",
531 "INTERNAL_TMDS2",
532 "INTERNAL_DAC1",
533 "INTERNAL_DAC2",
534 "INTERNAL_SDVOA",
535 "INTERNAL_SDVOB",
536 "SI170B",
537 "CH7303",
538 "CH7301",
539 "INTERNAL_DVO1",
540 "EXTERNAL_SDVOA",
541 "EXTERNAL_SDVOB",
542 "TITFP513",
543 "INTERNAL_LVTM1",
544 "VT1623",
545 "HDMI_SI1930",
546 "HDMI_INTERNAL",
547 "INTERNAL_KLDSCP_TMDS1",
548 "INTERNAL_KLDSCP_DVO1",
549 "INTERNAL_KLDSCP_DAC1",
550 "INTERNAL_KLDSCP_DAC2",
551 "SI178",
552 "MVPU_FPGA",
553 "INTERNAL_DDI",
554 "VT1625",
555 "HDMI_SI1932",
556 "DP_AN9801",
557 "DP_DP501",
558 "INTERNAL_UNIPHY",
559 "INTERNAL_KLDSCP_LVTMA",
560 "INTERNAL_UNIPHY1",
561 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500562 "NUTMEG",
563 "TRAVIS",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200564};
565
Alex Deucher196c58d2010-01-07 14:22:32 -0500566static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200567 "Unknown",
568 "VGA",
569 "DVI-I",
570 "DVI-D",
571 "DVI-A",
572 "Composite",
573 "S-video",
574 "LVDS",
575 "Component",
576 "DIN",
577 "DisplayPort",
578 "HDMI-A",
579 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500580 "TV",
581 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200582};
583
Alex Deuchercbd46232010-06-07 02:24:54 -0400584static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500585 "HPD1",
586 "HPD2",
587 "HPD3",
588 "HPD4",
589 "HPD5",
590 "HPD6",
591};
592
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200593static void radeon_print_display_setup(struct drm_device *dev)
594{
595 struct drm_connector *connector;
596 struct radeon_connector *radeon_connector;
597 struct drm_encoder *encoder;
598 struct radeon_encoder *radeon_encoder;
599 uint32_t devices;
600 int i = 0;
601
602 DRM_INFO("Radeon Display Connectors\n");
603 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
604 radeon_connector = to_radeon_connector(connector);
605 DRM_INFO("Connector %d:\n", i);
606 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500607 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
608 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000609 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200610 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
611 radeon_connector->ddc_bus->rec.mask_clk_reg,
612 radeon_connector->ddc_bus->rec.mask_data_reg,
613 radeon_connector->ddc_bus->rec.a_clk_reg,
614 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500615 radeon_connector->ddc_bus->rec.en_clk_reg,
616 radeon_connector->ddc_bus->rec.en_data_reg,
617 radeon_connector->ddc_bus->rec.y_clk_reg,
618 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000619 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400620 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000621 radeon_connector->router.ddc_mux_control_pin,
622 radeon_connector->router.ddc_mux_state);
623 if (radeon_connector->router.cd_valid)
624 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
625 radeon_connector->router.cd_mux_control_pin,
626 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000627 } else {
628 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
629 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
630 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
631 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
632 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
633 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
634 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
635 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200636 DRM_INFO(" Encoders:\n");
637 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
638 radeon_encoder = to_radeon_encoder(encoder);
639 devices = radeon_encoder->devices & radeon_connector->devices;
640 if (devices) {
641 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
642 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
643 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
644 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
645 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
646 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
647 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
648 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
649 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
650 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
651 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
652 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
653 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
654 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
655 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
656 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400657 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
658 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200659 if (devices & ATOM_DEVICE_TV1_SUPPORT)
660 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
661 if (devices & ATOM_DEVICE_CV_SUPPORT)
662 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
663 }
664 }
665 i++;
666 }
667}
668
Dave Airlie4ce001a2009-08-13 16:32:14 +1000669static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200670{
671 struct radeon_device *rdev = dev->dev_private;
672 struct drm_connector *drm_connector;
673 bool ret = false;
674
675 if (rdev->bios) {
676 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400677 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
678 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200679 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500680 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200681 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500682 if (ret == false)
683 ret = radeon_get_legacy_connector_info_from_table(dev);
684 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200685 } else {
686 if (!ASIC_IS_AVIVO(rdev))
687 ret = radeon_get_legacy_connector_info_from_table(dev);
688 }
689 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000690 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200691 radeon_print_display_setup(dev);
692 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
693 radeon_ddc_dump(drm_connector);
694 }
695
696 return ret;
697}
698
699int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
700{
Alex Deucher3c537882010-02-05 04:21:19 -0500701 struct drm_device *dev = radeon_connector->base.dev;
702 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200703 int ret = 0;
704
Alex Deucher26b5bc92010-08-05 21:21:18 -0400705 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000706 if (radeon_connector->router.ddc_valid)
707 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400708
Alex Deucher196c58d2010-01-07 14:22:32 -0500709 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
Alex Deucherb06947b2011-09-02 14:23:09 +0000710 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) ||
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400711 (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
712 ENCODER_OBJECT_ID_NONE)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000713 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Alex Deucherb06947b2011-09-02 14:23:09 +0000714
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000715 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
716 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucherb06947b2011-09-02 14:23:09 +0000717 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
718 &dig->dp_i2c_bus->adapter);
719 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
720 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
721 &radeon_connector->ddc_bus->adapter);
722 } else {
723 if (radeon_connector->ddc_bus && !radeon_connector->edid)
724 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
725 &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400726 }
Alex Deucherc324acd2010-12-08 22:13:06 -0500727
728 if (!radeon_connector->edid) {
729 if (rdev->is_atom_bios) {
730 /* some laptops provide a hardcoded edid in rom for LCDs */
731 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
732 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
733 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
734 } else
735 /* some servers provide a hardcoded edid in rom for KVMs */
736 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
737 }
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400738 if (radeon_connector->edid) {
739 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
740 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200741 return ret;
742 }
743 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000744 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200745}
746
747static int radeon_ddc_dump(struct drm_connector *connector)
748{
749 struct edid *edid;
750 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
751 int ret = 0;
752
Alex Deucher26b5bc92010-08-05 21:21:18 -0400753 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000754 if (radeon_connector->router.ddc_valid)
755 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400756
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200757 if (!radeon_connector->ddc_bus)
758 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200759 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Thomas Reime384fab2011-07-29 14:28:58 +0000760 /* Log EDID retrieval status here. In particular with regard to
761 * connectors with requires_extended_probe flag set, that will prevent
762 * function radeon_dvi_detect() to fetch EDID on this connector,
763 * as long as there is no valid EDID header found */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200764 if (edid) {
Thomas Reime384fab2011-07-29 14:28:58 +0000765 DRM_INFO("Radeon display connector %s: Found valid EDID",
766 drm_get_connector_name(connector));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200767 kfree(edid);
Thomas Reime384fab2011-07-29 14:28:58 +0000768 } else {
769 DRM_INFO("Radeon display connector %s: No monitor connected or invalid EDID",
770 drm_get_connector_name(connector));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200771 }
772 return ret;
773}
774
Alex Deucherf523f742011-01-31 16:48:52 -0500775/* avivo */
776static void avivo_get_fb_div(struct radeon_pll *pll,
777 u32 target_clock,
778 u32 post_div,
779 u32 ref_div,
780 u32 *fb_div,
781 u32 *frac_fb_div)
782{
783 u32 tmp = post_div * ref_div;
784
785 tmp *= target_clock;
786 *fb_div = tmp / pll->reference_freq;
787 *frac_fb_div = tmp % pll->reference_freq;
Alex Deuchera4b40d5d2011-02-14 11:43:10 -0500788
789 if (*fb_div > pll->max_feedback_div)
790 *fb_div = pll->max_feedback_div;
791 else if (*fb_div < pll->min_feedback_div)
792 *fb_div = pll->min_feedback_div;
Alex Deucherf523f742011-01-31 16:48:52 -0500793}
794
795static u32 avivo_get_post_div(struct radeon_pll *pll,
796 u32 target_clock)
797{
798 u32 vco, post_div, tmp;
799
800 if (pll->flags & RADEON_PLL_USE_POST_DIV)
801 return pll->post_div;
802
803 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
804 if (pll->flags & RADEON_PLL_IS_LCD)
805 vco = pll->lcd_pll_out_min;
806 else
807 vco = pll->pll_out_min;
808 } else {
809 if (pll->flags & RADEON_PLL_IS_LCD)
810 vco = pll->lcd_pll_out_max;
811 else
812 vco = pll->pll_out_max;
813 }
814
815 post_div = vco / target_clock;
816 tmp = vco % target_clock;
817
818 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
819 if (tmp)
820 post_div++;
821 } else {
822 if (!tmp)
823 post_div--;
824 }
825
Alex Deuchera4b40d5d2011-02-14 11:43:10 -0500826 if (post_div > pll->max_post_div)
827 post_div = pll->max_post_div;
828 else if (post_div < pll->min_post_div)
829 post_div = pll->min_post_div;
830
Alex Deucherf523f742011-01-31 16:48:52 -0500831 return post_div;
832}
833
834#define MAX_TOLERANCE 10
835
836void radeon_compute_pll_avivo(struct radeon_pll *pll,
837 u32 freq,
838 u32 *dot_clock_p,
839 u32 *fb_div_p,
840 u32 *frac_fb_div_p,
841 u32 *ref_div_p,
842 u32 *post_div_p)
843{
844 u32 target_clock = freq / 10;
845 u32 post_div = avivo_get_post_div(pll, target_clock);
846 u32 ref_div = pll->min_ref_div;
847 u32 fb_div = 0, frac_fb_div = 0, tmp;
848
849 if (pll->flags & RADEON_PLL_USE_REF_DIV)
850 ref_div = pll->reference_div;
851
852 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
853 avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
854 frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
855 if (frac_fb_div >= 5) {
856 frac_fb_div -= 5;
857 frac_fb_div = frac_fb_div / 10;
858 frac_fb_div++;
859 }
860 if (frac_fb_div >= 10) {
861 fb_div++;
862 frac_fb_div = 0;
863 }
864 } else {
865 while (ref_div <= pll->max_ref_div) {
866 avivo_get_fb_div(pll, target_clock, post_div, ref_div,
867 &fb_div, &frac_fb_div);
868 if (frac_fb_div >= (pll->reference_freq / 2))
869 fb_div++;
870 frac_fb_div = 0;
871 tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
872 tmp = (tmp * 10000) / target_clock;
873
874 if (tmp > (10000 + MAX_TOLERANCE))
875 ref_div++;
876 else if (tmp >= (10000 - MAX_TOLERANCE))
877 break;
878 else
879 ref_div++;
880 }
881 }
882
883 *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
884 (ref_div * post_div * 10);
885 *fb_div_p = fb_div;
886 *frac_fb_div_p = frac_fb_div;
887 *ref_div_p = ref_div;
888 *post_div_p = post_div;
889 DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
890 *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
891}
892
893/* pre-avivo */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200894static inline uint32_t radeon_div(uint64_t n, uint32_t d)
895{
896 uint64_t mod;
897
898 n += d / 2;
899
900 mod = do_div(n, d);
901 return n;
902}
903
Alex Deucherf523f742011-01-31 16:48:52 -0500904void radeon_compute_pll_legacy(struct radeon_pll *pll,
905 uint64_t freq,
906 uint32_t *dot_clock_p,
907 uint32_t *fb_div_p,
908 uint32_t *frac_fb_div_p,
909 uint32_t *ref_div_p,
910 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200911{
912 uint32_t min_ref_div = pll->min_ref_div;
913 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500914 uint32_t min_post_div = pll->min_post_div;
915 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200916 uint32_t min_fractional_feed_div = 0;
917 uint32_t max_fractional_feed_div = 0;
918 uint32_t best_vco = pll->best_vco;
919 uint32_t best_post_div = 1;
920 uint32_t best_ref_div = 1;
921 uint32_t best_feedback_div = 1;
922 uint32_t best_frac_feedback_div = 0;
923 uint32_t best_freq = -1;
924 uint32_t best_error = 0xffffffff;
925 uint32_t best_vco_diff = 1;
926 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500927 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200928
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000929 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 +0200930 freq = freq * 1000;
931
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500932 if (pll->flags & RADEON_PLL_IS_LCD) {
933 pll_out_min = pll->lcd_pll_out_min;
934 pll_out_max = pll->lcd_pll_out_max;
935 } else {
936 pll_out_min = pll->pll_out_min;
937 pll_out_max = pll->pll_out_max;
938 }
939
Alex Deucher619efb12011-01-31 16:48:53 -0500940 if (pll_out_min > 64800)
941 pll_out_min = 64800;
942
Alex Deucherfc103322010-01-19 17:16:10 -0500943 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200944 min_ref_div = max_ref_div = pll->reference_div;
945 else {
946 while (min_ref_div < max_ref_div-1) {
947 uint32_t mid = (min_ref_div + max_ref_div) / 2;
948 uint32_t pll_in = pll->reference_freq / mid;
949 if (pll_in < pll->pll_in_min)
950 max_ref_div = mid;
951 else if (pll_in > pll->pll_in_max)
952 min_ref_div = mid;
953 else
954 break;
955 }
956 }
957
Alex Deucherfc103322010-01-19 17:16:10 -0500958 if (pll->flags & RADEON_PLL_USE_POST_DIV)
959 min_post_div = max_post_div = pll->post_div;
960
961 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200962 min_fractional_feed_div = pll->min_frac_feedback_div;
963 max_fractional_feed_div = pll->max_frac_feedback_div;
964 }
965
Alex Deucherbd6a60a2011-02-21 01:11:59 -0500966 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200967 uint32_t ref_div;
968
Alex Deucherfc103322010-01-19 17:16:10 -0500969 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200970 continue;
971
972 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500973 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200974 if ((post_div == 5) ||
975 (post_div == 7) ||
976 (post_div == 9) ||
977 (post_div == 10) ||
978 (post_div == 11) ||
979 (post_div == 13) ||
980 (post_div == 14) ||
981 (post_div == 15))
982 continue;
983 }
984
985 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
986 uint32_t feedback_div, current_freq = 0, error, vco_diff;
987 uint32_t pll_in = pll->reference_freq / ref_div;
988 uint32_t min_feed_div = pll->min_feedback_div;
989 uint32_t max_feed_div = pll->max_feedback_div + 1;
990
991 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
992 continue;
993
994 while (min_feed_div < max_feed_div) {
995 uint32_t vco;
996 uint32_t min_frac_feed_div = min_fractional_feed_div;
997 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
998 uint32_t frac_feedback_div;
999 uint64_t tmp;
1000
1001 feedback_div = (min_feed_div + max_feed_div) / 2;
1002
1003 tmp = (uint64_t)pll->reference_freq * feedback_div;
1004 vco = radeon_div(tmp, ref_div);
1005
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001006 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001007 min_feed_div = feedback_div + 1;
1008 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001009 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001010 max_feed_div = feedback_div;
1011 continue;
1012 }
1013
1014 while (min_frac_feed_div < max_frac_feed_div) {
1015 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1016 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1017 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1018 current_freq = radeon_div(tmp, ref_div * post_div);
1019
Alex Deucherfc103322010-01-19 17:16:10 -05001020 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +02001021 if (freq < current_freq)
1022 error = 0xffffffff;
1023 else
1024 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -04001025 } else
1026 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001027 vco_diff = abs(vco - best_vco);
1028
1029 if ((best_vco == 0 && error < best_error) ||
1030 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +02001031 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +10001032 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001033 best_post_div = post_div;
1034 best_ref_div = ref_div;
1035 best_feedback_div = feedback_div;
1036 best_frac_feedback_div = frac_feedback_div;
1037 best_freq = current_freq;
1038 best_error = error;
1039 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +10001040 } else if (current_freq == freq) {
1041 if (best_freq == -1) {
1042 best_post_div = post_div;
1043 best_ref_div = ref_div;
1044 best_feedback_div = feedback_div;
1045 best_frac_feedback_div = frac_feedback_div;
1046 best_freq = current_freq;
1047 best_error = error;
1048 best_vco_diff = vco_diff;
1049 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1050 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1051 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1052 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1053 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1054 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1055 best_post_div = post_div;
1056 best_ref_div = ref_div;
1057 best_feedback_div = feedback_div;
1058 best_frac_feedback_div = frac_feedback_div;
1059 best_freq = current_freq;
1060 best_error = error;
1061 best_vco_diff = vco_diff;
1062 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001063 }
1064 if (current_freq < freq)
1065 min_frac_feed_div = frac_feedback_div + 1;
1066 else
1067 max_frac_feed_div = frac_feedback_div;
1068 }
1069 if (current_freq < freq)
1070 min_feed_div = feedback_div + 1;
1071 else
1072 max_feed_div = feedback_div;
1073 }
1074 }
1075 }
1076
1077 *dot_clock_p = best_freq / 10000;
1078 *fb_div_p = best_feedback_div;
1079 *frac_fb_div_p = best_frac_feedback_div;
1080 *ref_div_p = best_ref_div;
1081 *post_div_p = best_post_div;
Joe Perchesbbb0aef52011-04-17 20:35:52 -07001082 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1083 (long long)freq,
1084 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
Alex Deucher51d4bf82011-01-31 16:48:51 -05001085 best_ref_div, best_post_div);
1086
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001087}
1088
1089static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1090{
1091 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001092
Dave Airlie29d08b32010-09-27 16:17:17 +10001093 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001094 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +10001095 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001096 drm_framebuffer_cleanup(fb);
1097 kfree(radeon_fb);
1098}
1099
1100static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1101 struct drm_file *file_priv,
1102 unsigned int *handle)
1103{
1104 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1105
1106 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1107}
1108
1109static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1110 .destroy = radeon_user_framebuffer_destroy,
1111 .create_handle = radeon_user_framebuffer_create_handle,
1112};
1113
Dave Airlie38651672010-03-30 05:34:13 +00001114void
1115radeon_framebuffer_init(struct drm_device *dev,
1116 struct radeon_framebuffer *rfb,
1117 struct drm_mode_fb_cmd *mode_cmd,
1118 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001119{
Dave Airlie38651672010-03-30 05:34:13 +00001120 rfb->obj = obj;
1121 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1122 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001123}
1124
1125static struct drm_framebuffer *
1126radeon_user_framebuffer_create(struct drm_device *dev,
1127 struct drm_file *file_priv,
1128 struct drm_mode_fb_cmd *mode_cmd)
1129{
1130 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00001131 struct radeon_framebuffer *radeon_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001132
1133 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001134 if (obj == NULL) {
1135 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
1136 "can't create framebuffer\n", mode_cmd->handle);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001137 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001138 }
Dave Airlie38651672010-03-30 05:34:13 +00001139
1140 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001141 if (radeon_fb == NULL)
1142 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +00001143
1144 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1145
1146 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001147}
1148
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001149static void radeon_output_poll_changed(struct drm_device *dev)
1150{
1151 struct radeon_device *rdev = dev->dev_private;
1152 radeon_fb_output_poll_changed(rdev);
1153}
1154
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001155static const struct drm_mode_config_funcs radeon_mode_funcs = {
1156 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001157 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001158};
1159
Dave Airlie445282d2009-09-09 17:40:54 +10001160struct drm_prop_enum_list {
1161 int type;
1162 char *name;
1163};
1164
1165static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1166{ { 0, "driver" },
1167 { 1, "bios" },
1168};
1169
1170static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1171{ { TV_STD_NTSC, "ntsc" },
1172 { TV_STD_PAL, "pal" },
1173 { TV_STD_PAL_M, "pal-m" },
1174 { TV_STD_PAL_60, "pal-60" },
1175 { TV_STD_NTSC_J, "ntsc-j" },
1176 { TV_STD_SCART_PAL, "scart-pal" },
1177 { TV_STD_PAL_CN, "pal-cn" },
1178 { TV_STD_SECAM, "secam" },
1179};
1180
Alex Deucher5b1714d2010-08-03 19:59:20 -04001181static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1182{ { UNDERSCAN_OFF, "off" },
1183 { UNDERSCAN_ON, "on" },
1184 { UNDERSCAN_AUTO, "auto" },
1185};
1186
Alex Deucherd79766f2009-12-17 19:00:29 -05001187static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +10001188{
1189 int i, sz;
1190
1191 if (rdev->is_atom_bios) {
1192 rdev->mode_info.coherent_mode_property =
1193 drm_property_create(rdev->ddev,
1194 DRM_MODE_PROP_RANGE,
1195 "coherent", 2);
1196 if (!rdev->mode_info.coherent_mode_property)
1197 return -ENOMEM;
1198
1199 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -05001200 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +10001201 }
1202
1203 if (!ASIC_IS_AVIVO(rdev)) {
1204 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1205 rdev->mode_info.tmds_pll_property =
1206 drm_property_create(rdev->ddev,
1207 DRM_MODE_PROP_ENUM,
1208 "tmds_pll", sz);
1209 for (i = 0; i < sz; i++) {
1210 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
1211 i,
1212 radeon_tmds_pll_enum_list[i].type,
1213 radeon_tmds_pll_enum_list[i].name);
1214 }
1215 }
1216
1217 rdev->mode_info.load_detect_property =
1218 drm_property_create(rdev->ddev,
1219 DRM_MODE_PROP_RANGE,
1220 "load detection", 2);
1221 if (!rdev->mode_info.load_detect_property)
1222 return -ENOMEM;
1223 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -05001224 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +10001225
1226 drm_mode_create_scaling_mode_property(rdev->ddev);
1227
1228 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1229 rdev->mode_info.tv_std_property =
1230 drm_property_create(rdev->ddev,
1231 DRM_MODE_PROP_ENUM,
1232 "tv standard", sz);
1233 for (i = 0; i < sz; i++) {
1234 drm_property_add_enum(rdev->mode_info.tv_std_property,
1235 i,
1236 radeon_tv_std_enum_list[i].type,
1237 radeon_tv_std_enum_list[i].name);
1238 }
1239
Alex Deucher5b1714d2010-08-03 19:59:20 -04001240 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1241 rdev->mode_info.underscan_property =
1242 drm_property_create(rdev->ddev,
1243 DRM_MODE_PROP_ENUM,
1244 "underscan", sz);
1245 for (i = 0; i < sz; i++) {
1246 drm_property_add_enum(rdev->mode_info.underscan_property,
1247 i,
1248 radeon_underscan_enum_list[i].type,
1249 radeon_underscan_enum_list[i].name);
1250 }
1251
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001252 rdev->mode_info.underscan_hborder_property =
1253 drm_property_create(rdev->ddev,
1254 DRM_MODE_PROP_RANGE,
1255 "underscan hborder", 2);
1256 if (!rdev->mode_info.underscan_hborder_property)
1257 return -ENOMEM;
1258 rdev->mode_info.underscan_hborder_property->values[0] = 0;
1259 rdev->mode_info.underscan_hborder_property->values[1] = 128;
1260
1261 rdev->mode_info.underscan_vborder_property =
1262 drm_property_create(rdev->ddev,
1263 DRM_MODE_PROP_RANGE,
1264 "underscan vborder", 2);
1265 if (!rdev->mode_info.underscan_vborder_property)
1266 return -ENOMEM;
1267 rdev->mode_info.underscan_vborder_property->values[0] = 0;
1268 rdev->mode_info.underscan_vborder_property->values[1] = 128;
1269
Dave Airlie445282d2009-09-09 17:40:54 +10001270 return 0;
1271}
1272
Alex Deucherf46c0122010-03-31 00:33:27 -04001273void radeon_update_display_priority(struct radeon_device *rdev)
1274{
1275 /* adjustment options for the display watermarks */
1276 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1277 /* set display priority to high for r3xx, rv515 chips
1278 * this avoids flickering due to underflow to the
1279 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001280 * Don't force high on rs4xx igp chips as it seems to
1281 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001282 */
Alex Deucher45737442010-05-20 11:26:11 -04001283 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1284 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001285 rdev->disp_priority = 2;
1286 else
1287 rdev->disp_priority = 0;
1288 } else
1289 rdev->disp_priority = radeon_disp_priority;
1290
1291}
1292
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001293int radeon_modeset_init(struct radeon_device *rdev)
1294{
Alex Deucher18917b62010-02-01 16:02:25 -05001295 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001296 int ret;
1297
1298 drm_mode_config_init(rdev->ddev);
1299 rdev->mode_info.mode_config_initialized = true;
1300
1301 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
1302
Alex Deucher881dd742011-01-06 21:19:14 -05001303 if (ASIC_IS_DCE5(rdev)) {
1304 rdev->ddev->mode_config.max_width = 16384;
1305 rdev->ddev->mode_config.max_height = 16384;
1306 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001307 rdev->ddev->mode_config.max_width = 8192;
1308 rdev->ddev->mode_config.max_height = 8192;
1309 } else {
1310 rdev->ddev->mode_config.max_width = 4096;
1311 rdev->ddev->mode_config.max_height = 4096;
1312 }
1313
1314 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1315
Dave Airlie445282d2009-09-09 17:40:54 +10001316 ret = radeon_modeset_create_props(rdev);
1317 if (ret) {
1318 return ret;
1319 }
Dave Airliedfee5612009-10-02 09:19:09 +10001320
Alex Deucherf376b942010-08-05 21:21:16 -04001321 /* init i2c buses */
1322 radeon_i2c_init(rdev);
1323
Alex Deucher3c537882010-02-05 04:21:19 -05001324 /* check combios for a valid hardcoded EDID - Sun servers */
1325 if (!rdev->is_atom_bios) {
1326 /* check for hardcoded EDID in BIOS */
1327 radeon_combios_check_hardcoded_edid(rdev);
1328 }
1329
Dave Airliedfee5612009-10-02 09:19:09 +10001330 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001331 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001332 radeon_crtc_init(rdev->ddev, i);
1333 }
1334
1335 /* okay we should have all the bios connectors */
1336 ret = radeon_setup_enc_conn(rdev->ddev);
1337 if (!ret) {
1338 return ret;
1339 }
Alex Deucherac89af12011-05-22 13:20:36 -04001340
1341 /* init dig PHYs */
1342 if (rdev->is_atom_bios)
1343 radeon_atom_encoder_init(rdev);
1344
Alex Deucherd4877cf2009-12-04 16:56:37 -05001345 /* initialize hpd */
1346 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001347
Alex Deucherce8f5372010-05-07 15:10:16 -04001348 /* Initialize power management */
1349 radeon_pm_init(rdev);
1350
Dave Airlie38651672010-03-30 05:34:13 +00001351 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001352 drm_kms_helper_poll_init(rdev->ddev);
1353
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001354 return 0;
1355}
1356
1357void radeon_modeset_fini(struct radeon_device *rdev)
1358{
Dave Airlie38651672010-03-30 05:34:13 +00001359 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001360 kfree(rdev->mode_info.bios_hardcoded_edid);
Alex Deucherce8f5372010-05-07 15:10:16 -04001361 radeon_pm_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001362
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001363 if (rdev->mode_info.mode_config_initialized) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001364 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001365 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001366 drm_mode_config_cleanup(rdev->ddev);
1367 rdev->mode_info.mode_config_initialized = false;
1368 }
Alex Deucherf376b942010-08-05 21:21:16 -04001369 /* free i2c buses */
1370 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001371}
1372
Alex Deucher039ed2d2010-08-20 11:57:19 -04001373static bool is_hdtv_mode(struct drm_display_mode *mode)
1374{
1375 /* try and guess if this is a tv or a monitor */
1376 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1377 (mode->vdisplay == 576) || /* 576p */
1378 (mode->vdisplay == 720) || /* 720p */
1379 (mode->vdisplay == 1080)) /* 1080p */
1380 return true;
1381 else
1382 return false;
1383}
1384
Jerome Glissec93bb852009-07-13 21:04:08 +02001385bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1386 struct drm_display_mode *mode,
1387 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001388{
Jerome Glissec93bb852009-07-13 21:04:08 +02001389 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001390 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001391 struct drm_encoder *encoder;
1392 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1393 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001394 struct drm_connector *connector;
1395 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001396 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001397 u32 src_v = 1, dst_v = 1;
1398 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001399
Alex Deucher5b1714d2010-08-03 19:59:20 -04001400 radeon_crtc->h_border = 0;
1401 radeon_crtc->v_border = 0;
1402
Jerome Glissec93bb852009-07-13 21:04:08 +02001403 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001404 if (encoder->crtc != crtc)
1405 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001406 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001407 connector = radeon_get_connector_for_encoder(encoder);
1408 radeon_connector = to_radeon_connector(connector);
1409
Jerome Glissec93bb852009-07-13 21:04:08 +02001410 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001411 /* set scaling */
1412 if (radeon_encoder->rmx_type == RMX_OFF)
1413 radeon_crtc->rmx_type = RMX_OFF;
1414 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1415 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1416 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1417 else
1418 radeon_crtc->rmx_type = RMX_OFF;
1419 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001420 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001421 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001422 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001423 src_v = crtc->mode.vdisplay;
1424 dst_v = radeon_crtc->native_mode.vdisplay;
1425 src_h = crtc->mode.hdisplay;
1426 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001427
1428 /* fix up for overscan on hdmi */
1429 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001430 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001431 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1432 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001433 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1434 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001435 if (radeon_encoder->underscan_hborder != 0)
1436 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1437 else
1438 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1439 if (radeon_encoder->underscan_vborder != 0)
1440 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1441 else
1442 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001443 radeon_crtc->rmx_type = RMX_FULL;
1444 src_v = crtc->mode.vdisplay;
1445 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1446 src_h = crtc->mode.hdisplay;
1447 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1448 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001449 first = false;
1450 } else {
1451 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1452 /* WARNING: Right now this can't happen but
1453 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001454 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001455 * (ie all encoder can work with the same
1456 * scaling).
1457 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001458 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001459 return false;
1460 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001461 }
1462 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001463 if (radeon_crtc->rmx_type != RMX_OFF) {
1464 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001465 a.full = dfixed_const(src_v);
1466 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001467 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001468 a.full = dfixed_const(src_h);
1469 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001470 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001471 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001472 radeon_crtc->vsc.full = dfixed_const(1);
1473 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001474 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001475 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001476}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001477
1478/*
1479 * Retrieve current video scanout position of crtc on a given gpu.
1480 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001481 * \param dev Device to query.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001482 * \param crtc Crtc to query.
1483 * \param *vpos Location where vertical scanout position should be stored.
1484 * \param *hpos Location where horizontal scanout position should go.
1485 *
1486 * Returns vpos as a positive number while in active scanout area.
1487 * Returns vpos as a negative number inside vblank, counting the number
1488 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1489 * until start of active scanout / end of vblank."
1490 *
1491 * \return Flags, or'ed together as follows:
1492 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001493 * DRM_SCANOUTPOS_VALID = Query successful.
Mario Kleinerf5a80202010-10-23 04:42:17 +02001494 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1495 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
Mario Kleiner6383cf72010-10-05 19:57:36 -04001496 * this flag means that returned position may be offset by a constant but
1497 * unknown small number of scanlines wrt. real scanout position.
1498 *
1499 */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001500int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
Mario Kleiner6383cf72010-10-05 19:57:36 -04001501{
1502 u32 stat_crtc = 0, vbl = 0, position = 0;
1503 int vbl_start, vbl_end, vtotal, ret = 0;
1504 bool in_vbl = true;
1505
Mario Kleinerf5a80202010-10-23 04:42:17 +02001506 struct radeon_device *rdev = dev->dev_private;
1507
Mario Kleiner6383cf72010-10-05 19:57:36 -04001508 if (ASIC_IS_DCE4(rdev)) {
1509 if (crtc == 0) {
1510 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1511 EVERGREEN_CRTC0_REGISTER_OFFSET);
1512 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1513 EVERGREEN_CRTC0_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001514 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001515 }
1516 if (crtc == 1) {
1517 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1518 EVERGREEN_CRTC1_REGISTER_OFFSET);
1519 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1520 EVERGREEN_CRTC1_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001521 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001522 }
1523 if (crtc == 2) {
1524 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1525 EVERGREEN_CRTC2_REGISTER_OFFSET);
1526 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1527 EVERGREEN_CRTC2_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001528 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001529 }
1530 if (crtc == 3) {
1531 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1532 EVERGREEN_CRTC3_REGISTER_OFFSET);
1533 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1534 EVERGREEN_CRTC3_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001535 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001536 }
1537 if (crtc == 4) {
1538 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1539 EVERGREEN_CRTC4_REGISTER_OFFSET);
1540 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1541 EVERGREEN_CRTC4_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001542 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001543 }
1544 if (crtc == 5) {
1545 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1546 EVERGREEN_CRTC5_REGISTER_OFFSET);
1547 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1548 EVERGREEN_CRTC5_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001549 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001550 }
1551 } else if (ASIC_IS_AVIVO(rdev)) {
1552 if (crtc == 0) {
1553 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1554 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001555 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001556 }
1557 if (crtc == 1) {
1558 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1559 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001560 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001561 }
1562 } else {
1563 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1564 if (crtc == 0) {
1565 /* Assume vbl_end == 0, get vbl_start from
1566 * upper 16 bits.
1567 */
1568 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1569 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1570 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1571 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1572 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1573 if (!(stat_crtc & 1))
1574 in_vbl = false;
1575
Mario Kleinerf5a80202010-10-23 04:42:17 +02001576 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001577 }
1578 if (crtc == 1) {
1579 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1580 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1581 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1582 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1583 if (!(stat_crtc & 1))
1584 in_vbl = false;
1585
Mario Kleinerf5a80202010-10-23 04:42:17 +02001586 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001587 }
1588 }
1589
1590 /* Decode into vertical and horizontal scanout position. */
1591 *vpos = position & 0x1fff;
1592 *hpos = (position >> 16) & 0x1fff;
1593
1594 /* Valid vblank area boundaries from gpu retrieved? */
1595 if (vbl > 0) {
1596 /* Yes: Decode. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001597 ret |= DRM_SCANOUTPOS_ACCURATE;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001598 vbl_start = vbl & 0x1fff;
1599 vbl_end = (vbl >> 16) & 0x1fff;
1600 }
1601 else {
1602 /* No: Fake something reasonable which gives at least ok results. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001603 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001604 vbl_end = 0;
1605 }
1606
1607 /* Test scanout position against vblank region. */
1608 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1609 in_vbl = false;
1610
1611 /* Check if inside vblank area and apply corrective offsets:
1612 * vpos will then be >=0 in video scanout area, but negative
1613 * within vblank area, counting down the number of lines until
1614 * start of scanout.
1615 */
1616
1617 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1618 if (in_vbl && (*vpos >= vbl_start)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001619 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001620 *vpos = *vpos - vtotal;
1621 }
1622
1623 /* Correct for shifted end of vbl at vbl_end. */
1624 *vpos = *vpos - vbl_end;
1625
1626 /* In vblank? */
1627 if (in_vbl)
Mario Kleinerf5a80202010-10-23 04:42:17 +02001628 ret |= DRM_SCANOUTPOS_INVBL;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001629
1630 return ret;
1631}