blob: 97ea46597bead25ac9d3bec4ea4f944c262a2c75 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
David Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
27#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028#include "radeon.h"
29
30#include "atom.h"
31#include <asm/div64.h>
32
Dave Airlie10ebc0b2012-09-17 14:40:31 +100033#include <linux/pm_runtime.h>
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drm_crtc_helper.h>
35#include <drm/drm_edid.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020036
Christian König32167012014-03-28 18:55:10 +010037#include <linux/gcd.h>
38
Jerome Glisse771fe6b2009-06-05 14:42:42 +020039static void avivo_crtc_load_lut(struct drm_crtc *crtc)
40{
41 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
42 struct drm_device *dev = crtc->dev;
43 struct radeon_device *rdev = dev->dev_private;
44 int i;
45
Dave Airlied9fdaaf2010-08-02 10:42:55 +100046 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020047 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
48
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
50 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
51 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
52
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
54 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
55 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
56
57 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
58 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
59 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
60
61 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
62 for (i = 0; i < 256; i++) {
63 WREG32(AVIVO_DC_LUT_30_COLOR,
64 (radeon_crtc->lut_r[i] << 20) |
65 (radeon_crtc->lut_g[i] << 10) |
66 (radeon_crtc->lut_b[i] << 0));
67 }
68
Mario Kleiner4366f3b2014-06-07 03:38:11 +020069 /* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
70 WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020071}
72
Alex Deucherfee298f2011-01-06 21:19:30 -050073static void dce4_crtc_load_lut(struct drm_crtc *crtc)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050074{
75 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
76 struct drm_device *dev = crtc->dev;
77 struct radeon_device *rdev = dev->dev_private;
78 int i;
79
Dave Airlied9fdaaf2010-08-02 10:42:55 +100080 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050081 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
82
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
84 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
85 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
86
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
88 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
89 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
90
Alex Deucher677d0762010-04-22 22:58:50 -040091 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
92 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050093
Alex Deucher677d0762010-04-22 22:58:50 -040094 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050095 for (i = 0; i < 256; i++) {
Alex Deucher677d0762010-04-22 22:58:50 -040096 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050097 (radeon_crtc->lut_r[i] << 20) |
98 (radeon_crtc->lut_g[i] << 10) |
99 (radeon_crtc->lut_b[i] << 0));
100 }
101}
102
Alex Deucherfee298f2011-01-06 21:19:30 -0500103static void dce5_crtc_load_lut(struct drm_crtc *crtc)
104{
105 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
106 struct drm_device *dev = crtc->dev;
107 struct radeon_device *rdev = dev->dev_private;
108 int i;
109
110 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
111
112 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
113 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
114 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
115 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
116 NI_GRPH_PRESCALE_BYPASS);
117 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
118 NI_OVL_PRESCALE_BYPASS);
119 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
120 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
121 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
122
123 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
124
125 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
126 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
127 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
128
129 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
130 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
131 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
132
133 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
134 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
135
136 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
137 for (i = 0; i < 256; i++) {
138 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
139 (radeon_crtc->lut_r[i] << 20) |
140 (radeon_crtc->lut_g[i] << 10) |
141 (radeon_crtc->lut_b[i] << 0));
142 }
143
144 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
145 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
147 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
148 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
149 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
150 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
151 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
152 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
153 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
154 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
155 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
156 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
157 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
158 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
159 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
Alex Deucher9e05fa12013-01-24 10:06:33 -0500160 if (ASIC_IS_DCE8(rdev)) {
161 /* XXX this only needs to be programmed once per crtc at startup,
162 * not sure where the best place for it is
163 */
164 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
165 CIK_CURSOR_ALPHA_BLND_ENA);
166 }
Alex Deucherfee298f2011-01-06 21:19:30 -0500167}
168
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169static void legacy_crtc_load_lut(struct drm_crtc *crtc)
170{
171 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
172 struct drm_device *dev = crtc->dev;
173 struct radeon_device *rdev = dev->dev_private;
174 int i;
175 uint32_t dac2_cntl;
176
177 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
178 if (radeon_crtc->crtc_id == 0)
179 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
180 else
181 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
182 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
183
184 WREG8(RADEON_PALETTE_INDEX, 0);
185 for (i = 0; i < 256; i++) {
186 WREG32(RADEON_PALETTE_30_DATA,
187 (radeon_crtc->lut_r[i] << 20) |
188 (radeon_crtc->lut_g[i] << 10) |
189 (radeon_crtc->lut_b[i] << 0));
190 }
191}
192
193void radeon_crtc_load_lut(struct drm_crtc *crtc)
194{
195 struct drm_device *dev = crtc->dev;
196 struct radeon_device *rdev = dev->dev_private;
197
198 if (!crtc->enabled)
199 return;
200
Alex Deucherfee298f2011-01-06 21:19:30 -0500201 if (ASIC_IS_DCE5(rdev))
202 dce5_crtc_load_lut(crtc);
203 else if (ASIC_IS_DCE4(rdev))
204 dce4_crtc_load_lut(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500205 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200206 avivo_crtc_load_lut(crtc);
207 else
208 legacy_crtc_load_lut(crtc);
209}
210
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000211/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
213 u16 blue, int regno)
214{
215 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
216
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200217 radeon_crtc->lut_r[regno] = red >> 6;
218 radeon_crtc->lut_g[regno] = green >> 6;
219 radeon_crtc->lut_b[regno] = blue >> 6;
220}
221
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000222/** Gets the color ramps on behalf of fbcon */
223void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
224 u16 *blue, int regno)
225{
226 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
227
228 *red = radeon_crtc->lut_r[regno] << 6;
229 *green = radeon_crtc->lut_g[regno] << 6;
230 *blue = radeon_crtc->lut_b[regno] << 6;
231}
232
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200233static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100234 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200235{
236 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100237 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000239 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100240 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000241 radeon_crtc->lut_r[i] = red[i] >> 6;
242 radeon_crtc->lut_g[i] = green[i] >> 6;
243 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200245 radeon_crtc_load_lut(crtc);
246}
247
248static void radeon_crtc_destroy(struct drm_crtc *crtc)
249{
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252 drm_crtc_cleanup(crtc);
Christian Königfa7f5172014-06-03 18:13:21 -0400253 destroy_workqueue(radeon_crtc->flip_queue);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200254 kfree(radeon_crtc);
255}
256
Christian Königfa7f5172014-06-03 18:13:21 -0400257/**
258 * radeon_unpin_work_func - unpin old buffer object
259 *
260 * @__work - kernel work item
261 *
262 * Unpin the old frame buffer object outside of the interrupt handler
Alex Deucher6f34be52010-11-21 10:59:01 -0500263 */
264static void radeon_unpin_work_func(struct work_struct *__work)
265{
Christian Königfa7f5172014-06-03 18:13:21 -0400266 struct radeon_flip_work *work =
267 container_of(__work, struct radeon_flip_work, unpin_work);
Alex Deucher6f34be52010-11-21 10:59:01 -0500268 int r;
269
270 /* unpin of the old buffer */
271 r = radeon_bo_reserve(work->old_rbo, false);
272 if (likely(r == 0)) {
273 r = radeon_bo_unpin(work->old_rbo);
274 if (unlikely(r != 0)) {
275 DRM_ERROR("failed to unpin buffer after flip\n");
276 }
277 radeon_bo_unreserve(work->old_rbo);
278 } else
279 DRM_ERROR("failed to reserve buffer after flip\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000280
281 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500282 kfree(work);
283}
284
Christian König1a0e7912014-05-27 16:49:21 +0200285void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
Alex Deucher6f34be52010-11-21 10:59:01 -0500286{
287 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
Alex Deucher6f34be52010-11-21 10:59:01 -0500288 unsigned long flags;
289 u32 update_pending;
290 int vpos, hpos;
291
Christian Königf5d636d2014-04-23 20:46:06 +0200292 /* can happen during initialization */
293 if (radeon_crtc == NULL)
294 return;
295
Alex Deucher6f34be52010-11-21 10:59:01 -0500296 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
Michel Dänzera2b6d3b2014-06-30 18:12:34 +0900297 if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
298 DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
299 "RADEON_FLIP_SUBMITTED(%d)\n",
300 radeon_crtc->flip_status,
301 RADEON_FLIP_SUBMITTED);
Alex Deucher6f34be52010-11-21 10:59:01 -0500302 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
303 return;
304 }
Christian Königfa7f5172014-06-03 18:13:21 -0400305
306 update_pending = radeon_page_flip_pending(rdev, crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500307
308 /* Has the pageflip already completed in crtc, or is it certain
309 * to complete in this vblank?
310 */
311 if (update_pending &&
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200312 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
Mario Kleinerd47abc52013-10-30 05:13:07 +0100313 &vpos, &hpos, NULL, NULL)) &&
Felix Kuehling81ffbbe2012-02-23 19:16:12 -0500314 ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
315 (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
316 /* crtc didn't flip in this target vblank interval,
317 * but flip is pending in crtc. Based on the current
318 * scanout position we know that the current frame is
319 * (nearly) complete and the flip will (likely)
320 * complete before the start of the next frame.
321 */
322 update_pending = 0;
323 }
Christian Königfa7f5172014-06-03 18:13:21 -0400324 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
325 if (!update_pending)
Christian König1a0e7912014-05-27 16:49:21 +0200326 radeon_crtc_handle_flip(rdev, crtc_id);
Christian König1a0e7912014-05-27 16:49:21 +0200327}
328
329/**
330 * radeon_crtc_handle_flip - page flip completed
331 *
332 * @rdev: radeon device pointer
333 * @crtc_id: crtc number this event is for
334 *
335 * Called when we are sure that a page flip for this crtc is completed.
336 */
337void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
338{
339 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
Christian Königfa7f5172014-06-03 18:13:21 -0400340 struct radeon_flip_work *work;
Christian König1a0e7912014-05-27 16:49:21 +0200341 unsigned long flags;
342
343 /* this can happen at init */
344 if (radeon_crtc == NULL)
345 return;
346
347 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
Christian Königfa7f5172014-06-03 18:13:21 -0400348 work = radeon_crtc->flip_work;
Michel Dänzera2b6d3b2014-06-30 18:12:34 +0900349 if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
350 DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
351 "RADEON_FLIP_SUBMITTED(%d)\n",
352 radeon_crtc->flip_status,
353 RADEON_FLIP_SUBMITTED);
Christian König1a0e7912014-05-27 16:49:21 +0200354 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
355 return;
Alex Deucher6f34be52010-11-21 10:59:01 -0500356 }
357
Christian Königfa7f5172014-06-03 18:13:21 -0400358 /* Pageflip completed. Clean up. */
Michel Dänzera2b6d3b2014-06-30 18:12:34 +0900359 radeon_crtc->flip_status = RADEON_FLIP_NONE;
Christian Königfa7f5172014-06-03 18:13:21 -0400360 radeon_crtc->flip_work = NULL;
Alex Deucher6f34be52010-11-21 10:59:01 -0500361
362 /* wakeup userspace */
Rob Clark26ae4662012-10-08 19:50:42 +0000363 if (work->event)
364 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
365
Alex Deucher6f34be52010-11-21 10:59:01 -0500366 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
367
Michel Dänzerca721b72014-06-17 19:12:03 +0900368 drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500369 radeon_fence_unref(&work->fence);
Michel Dänzer46889d92014-06-17 19:12:04 +0900370 radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
Christian Königfa7f5172014-06-03 18:13:21 -0400371 queue_work(radeon_crtc->flip_queue, &work->unpin_work);
Alex Deucher6f34be52010-11-21 10:59:01 -0500372}
373
Christian Königfa7f5172014-06-03 18:13:21 -0400374/**
375 * radeon_flip_work_func - page flip framebuffer
376 *
377 * @work - kernel work item
378 *
379 * Wait for the buffer object to become idle and do the actual page flip
380 */
381static void radeon_flip_work_func(struct work_struct *__work)
Alex Deucher6f34be52010-11-21 10:59:01 -0500382{
Christian Königfa7f5172014-06-03 18:13:21 -0400383 struct radeon_flip_work *work =
384 container_of(__work, struct radeon_flip_work, flip_work);
385 struct radeon_device *rdev = work->rdev;
386 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
387
388 struct drm_crtc *crtc = &radeon_crtc->base;
Alex Deucher6f34be52010-11-21 10:59:01 -0500389 unsigned long flags;
Alex Deucher6f34be52010-11-21 10:59:01 -0500390 int r;
391
Christian Königfa7f5172014-06-03 18:13:21 -0400392 down_read(&rdev->exclusive_lock);
Michel Dänzer306f98d2014-07-14 15:58:03 +0900393 if (work->fence) {
Christian Königfa7f5172014-06-03 18:13:21 -0400394 r = radeon_fence_wait(work->fence, false);
395 if (r == -EDEADLK) {
396 up_read(&rdev->exclusive_lock);
397 r = radeon_gpu_reset(rdev);
398 down_read(&rdev->exclusive_lock);
399 }
Michel Dänzer306f98d2014-07-14 15:58:03 +0900400 if (r)
401 DRM_ERROR("failed to wait on page flip fence (%d)!\n", r);
Alex Deucher6f34be52010-11-21 10:59:01 -0500402
Michel Dänzer306f98d2014-07-14 15:58:03 +0900403 /* We continue with the page flip even if we failed to wait on
404 * the fence, otherwise the DRM core and userspace will be
405 * confused about which BO the CRTC is scanning out
406 */
407
408 radeon_fence_unref(&work->fence);
Alex Deucher6f34be52010-11-21 10:59:01 -0500409 }
Alex Deucher6f34be52010-11-21 10:59:01 -0500410
Michel Dänzerc60381b2014-07-14 15:48:42 +0900411 /* do the flip (mmio) */
412 radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500413
Michel Dänzerc60381b2014-07-14 15:48:42 +0900414 /* We borrow the event spin lock for protecting flip_status */
415 spin_lock_irqsave(&crtc->dev->event_lock, flags);
416
417 /* set the proper interrupt */
418 radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
419
420 radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
421 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
422 up_read(&rdev->exclusive_lock);
Michel Dänzerc60381b2014-07-14 15:48:42 +0900423}
424
425static int radeon_crtc_page_flip(struct drm_crtc *crtc,
426 struct drm_framebuffer *fb,
427 struct drm_pending_vblank_event *event,
428 uint32_t page_flip_flags)
429{
430 struct drm_device *dev = crtc->dev;
431 struct radeon_device *rdev = dev->dev_private;
432 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
433 struct radeon_framebuffer *old_radeon_fb;
434 struct radeon_framebuffer *new_radeon_fb;
435 struct drm_gem_object *obj;
436 struct radeon_flip_work *work;
437 struct radeon_bo *new_rbo;
438 uint32_t tiling_flags, pitch_pixels;
439 uint64_t base;
440 unsigned long flags;
441 int r;
442
443 work = kzalloc(sizeof *work, GFP_KERNEL);
444 if (work == NULL)
445 return -ENOMEM;
446
447 INIT_WORK(&work->flip_work, radeon_flip_work_func);
448 INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
449
450 work->rdev = rdev;
451 work->crtc_id = radeon_crtc->crtc_id;
452 work->event = event;
453
454 /* schedule unpin of the old buffer */
455 old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
456 obj = old_radeon_fb->obj;
457
458 /* take a reference to the old object */
459 drm_gem_object_reference(obj);
460 work->old_rbo = gem_to_radeon_bo(obj);
461
462 new_radeon_fb = to_radeon_framebuffer(fb);
463 obj = new_radeon_fb->obj;
464 new_rbo = gem_to_radeon_bo(obj);
465
466 spin_lock(&new_rbo->tbo.bdev->fence_lock);
467 if (new_rbo->tbo.sync_obj)
468 work->fence = radeon_fence_ref(new_rbo->tbo.sync_obj);
469 spin_unlock(&new_rbo->tbo.bdev->fence_lock);
470
471 /* pin the new buffer */
472 DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
473 work->old_rbo, new_rbo);
474
475 r = radeon_bo_reserve(new_rbo, false);
Alex Deucher6f34be52010-11-21 10:59:01 -0500476 if (unlikely(r != 0)) {
477 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
Christian Königfa7f5172014-06-03 18:13:21 -0400478 goto cleanup;
Alex Deucher6f34be52010-11-21 10:59:01 -0500479 }
Michel Dänzer0349af72012-03-14 17:12:42 +0100480 /* Only 27 bit offset for legacy CRTC */
Michel Dänzerc60381b2014-07-14 15:48:42 +0900481 r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
Michel Dänzer0349af72012-03-14 17:12:42 +0100482 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500483 if (unlikely(r != 0)) {
Michel Dänzerc60381b2014-07-14 15:48:42 +0900484 radeon_bo_unreserve(new_rbo);
Alex Deucher6f34be52010-11-21 10:59:01 -0500485 r = -EINVAL;
486 DRM_ERROR("failed to pin new rbo buffer before flip\n");
Christian Königfa7f5172014-06-03 18:13:21 -0400487 goto cleanup;
Alex Deucher6f34be52010-11-21 10:59:01 -0500488 }
Michel Dänzerc60381b2014-07-14 15:48:42 +0900489 radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
490 radeon_bo_unreserve(new_rbo);
Alex Deucher6f34be52010-11-21 10:59:01 -0500491
492 if (!ASIC_IS_AVIVO(rdev)) {
493 /* crtc offset is from display base addr not FB location */
494 base -= radeon_crtc->legacy_display_base_addr;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200495 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
Alex Deucher6f34be52010-11-21 10:59:01 -0500496
497 if (tiling_flags & RADEON_TILING_MACRO) {
498 if (ASIC_IS_R300(rdev)) {
499 base &= ~0x7ff;
500 } else {
501 int byteshift = fb->bits_per_pixel >> 4;
502 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
503 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
504 }
505 } else {
506 int offset = crtc->y * pitch_pixels + crtc->x;
507 switch (fb->bits_per_pixel) {
508 case 8:
509 default:
510 offset *= 1;
511 break;
512 case 15:
513 case 16:
514 offset *= 2;
515 break;
516 case 24:
517 offset *= 3;
518 break;
519 case 32:
520 offset *= 4;
521 break;
522 }
523 base += offset;
524 }
525 base &= ~7;
526 }
Michel Dänzerc60381b2014-07-14 15:48:42 +0900527 work->base = base;
Alex Deucher6f34be52010-11-21 10:59:01 -0500528
Michel Dänzerca721b72014-06-17 19:12:03 +0900529 r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
530 if (r) {
531 DRM_ERROR("failed to get vblank before flip\n");
532 goto pflip_cleanup;
533 }
534
Christian Königfa7f5172014-06-03 18:13:21 -0400535 /* We borrow the event spin lock for protecting flip_work */
536 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Christian König1aab5512014-05-27 16:49:22 +0200537
Michel Dänzera2b6d3b2014-06-30 18:12:34 +0900538 if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
Christian Königfa7f5172014-06-03 18:13:21 -0400539 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
540 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Michel Dänzerc60381b2014-07-14 15:48:42 +0900541 r = -EBUSY;
542 goto pflip_cleanup;
Christian Königfa7f5172014-06-03 18:13:21 -0400543 }
Michel Dänzera2b6d3b2014-06-30 18:12:34 +0900544 radeon_crtc->flip_status = RADEON_FLIP_PENDING;
Christian Königfa7f5172014-06-03 18:13:21 -0400545 radeon_crtc->flip_work = work;
546
Michel Dänzer685d54b2014-06-10 10:21:57 +0900547 /* update crtc fb */
548 crtc->primary->fb = fb;
549
Christian Königfa7f5172014-06-03 18:13:21 -0400550 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
551
552 queue_work(radeon_crtc->flip_queue, &work->flip_work);
Christian Königfa7f5172014-06-03 18:13:21 -0400553 return 0;
Michel Dänzerc60381b2014-07-14 15:48:42 +0900554
555pflip_cleanup:
556 if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
557 DRM_ERROR("failed to reserve new rbo in error path\n");
558 goto cleanup;
559 }
560 if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
561 DRM_ERROR("failed to unpin new rbo in error path\n");
562 }
563 radeon_bo_unreserve(new_rbo);
564
565cleanup:
566 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
567 radeon_fence_unref(&work->fence);
568 kfree(work);
569
570 return r;
Alex Deucher6f34be52010-11-21 10:59:01 -0500571}
572
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000573static int
574radeon_crtc_set_config(struct drm_mode_set *set)
575{
576 struct drm_device *dev;
577 struct radeon_device *rdev;
578 struct drm_crtc *crtc;
579 bool active = false;
580 int ret;
581
582 if (!set || !set->crtc)
583 return -EINVAL;
584
585 dev = set->crtc->dev;
586
587 ret = pm_runtime_get_sync(dev->dev);
588 if (ret < 0)
589 return ret;
590
591 ret = drm_crtc_helper_set_config(set);
592
593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
594 if (crtc->enabled)
595 active = true;
596
597 pm_runtime_mark_last_busy(dev->dev);
598
599 rdev = dev->dev_private;
600 /* if we have active crtcs and we don't have a power ref,
601 take the current one */
602 if (active && !rdev->have_disp_power_ref) {
603 rdev->have_disp_power_ref = true;
604 return ret;
605 }
606 /* if we have no active crtcs, then drop the power ref
607 we got before */
608 if (!active && rdev->have_disp_power_ref) {
609 pm_runtime_put_autosuspend(dev->dev);
610 rdev->have_disp_power_ref = false;
611 }
612
613 /* drop the power reference we got coming in here */
614 pm_runtime_put_autosuspend(dev->dev);
615 return ret;
616}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200617static const struct drm_crtc_funcs radeon_crtc_funcs = {
618 .cursor_set = radeon_crtc_cursor_set,
619 .cursor_move = radeon_crtc_cursor_move,
620 .gamma_set = radeon_crtc_gamma_set,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000621 .set_config = radeon_crtc_set_config,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200622 .destroy = radeon_crtc_destroy,
Alex Deucher6f34be52010-11-21 10:59:01 -0500623 .page_flip = radeon_crtc_page_flip,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200624};
625
626static void radeon_crtc_init(struct drm_device *dev, int index)
627{
628 struct radeon_device *rdev = dev->dev_private;
629 struct radeon_crtc *radeon_crtc;
630 int i;
631
632 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
633 if (radeon_crtc == NULL)
634 return;
635
636 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
637
638 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
639 radeon_crtc->crtc_id = index;
Christian Königfa7f5172014-06-03 18:13:21 -0400640 radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
Jerome Glissec93bb852009-07-13 21:04:08 +0200641 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200642
Alex Deucher9e05fa12013-01-24 10:06:33 -0500643 if (rdev->family >= CHIP_BONAIRE) {
644 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
645 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
646 } else {
647 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
648 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
649 }
Alex Deucherbea61c52014-02-12 12:56:53 -0500650 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
651 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
Alex Deucher9e05fa12013-01-24 10:06:33 -0500652
Dave Airlie785b93e2009-08-28 15:46:53 +1000653#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200654 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
655 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
656 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000657#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200658
659 for (i = 0; i < 256; i++) {
660 radeon_crtc->lut_r[i] = i << 2;
661 radeon_crtc->lut_g[i] = i << 2;
662 radeon_crtc->lut_b[i] = i << 2;
663 }
664
665 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
666 radeon_atombios_init_crtc(dev, radeon_crtc);
667 else
668 radeon_legacy_init_crtc(dev, radeon_crtc);
669}
670
Alex Deuchere68adef2012-09-06 14:32:06 -0400671static const char *encoder_names[38] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200672 "NONE",
673 "INTERNAL_LVDS",
674 "INTERNAL_TMDS1",
675 "INTERNAL_TMDS2",
676 "INTERNAL_DAC1",
677 "INTERNAL_DAC2",
678 "INTERNAL_SDVOA",
679 "INTERNAL_SDVOB",
680 "SI170B",
681 "CH7303",
682 "CH7301",
683 "INTERNAL_DVO1",
684 "EXTERNAL_SDVOA",
685 "EXTERNAL_SDVOB",
686 "TITFP513",
687 "INTERNAL_LVTM1",
688 "VT1623",
689 "HDMI_SI1930",
690 "HDMI_INTERNAL",
691 "INTERNAL_KLDSCP_TMDS1",
692 "INTERNAL_KLDSCP_DVO1",
693 "INTERNAL_KLDSCP_DAC1",
694 "INTERNAL_KLDSCP_DAC2",
695 "SI178",
696 "MVPU_FPGA",
697 "INTERNAL_DDI",
698 "VT1625",
699 "HDMI_SI1932",
700 "DP_AN9801",
701 "DP_DP501",
702 "INTERNAL_UNIPHY",
703 "INTERNAL_KLDSCP_LVTMA",
704 "INTERNAL_UNIPHY1",
705 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500706 "NUTMEG",
707 "TRAVIS",
Alex Deuchere68adef2012-09-06 14:32:06 -0400708 "INTERNAL_VCE",
709 "INTERNAL_UNIPHY3",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200710};
711
Alex Deuchercbd46232010-06-07 02:24:54 -0400712static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500713 "HPD1",
714 "HPD2",
715 "HPD3",
716 "HPD4",
717 "HPD5",
718 "HPD6",
719};
720
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200721static void radeon_print_display_setup(struct drm_device *dev)
722{
723 struct drm_connector *connector;
724 struct radeon_connector *radeon_connector;
725 struct drm_encoder *encoder;
726 struct radeon_encoder *radeon_encoder;
727 uint32_t devices;
728 int i = 0;
729
730 DRM_INFO("Radeon Display Connectors\n");
731 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
732 radeon_connector = to_radeon_connector(connector);
733 DRM_INFO("Connector %d:\n", i);
Jani Nikula72082092014-06-03 14:56:19 +0300734 DRM_INFO(" %s\n", connector->name);
Alex Deuchereed45b32009-12-04 14:45:27 -0500735 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
736 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000737 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200738 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
739 radeon_connector->ddc_bus->rec.mask_clk_reg,
740 radeon_connector->ddc_bus->rec.mask_data_reg,
741 radeon_connector->ddc_bus->rec.a_clk_reg,
742 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500743 radeon_connector->ddc_bus->rec.en_clk_reg,
744 radeon_connector->ddc_bus->rec.en_data_reg,
745 radeon_connector->ddc_bus->rec.y_clk_reg,
746 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000747 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400748 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000749 radeon_connector->router.ddc_mux_control_pin,
750 radeon_connector->router.ddc_mux_state);
751 if (radeon_connector->router.cd_valid)
752 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
753 radeon_connector->router.cd_mux_control_pin,
754 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000755 } else {
756 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
757 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
758 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
759 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
760 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
761 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
762 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
763 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200764 DRM_INFO(" Encoders:\n");
765 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
766 radeon_encoder = to_radeon_encoder(encoder);
767 devices = radeon_encoder->devices & radeon_connector->devices;
768 if (devices) {
769 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
770 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
771 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
772 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
773 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
774 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
775 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
776 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
777 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
778 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
779 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
780 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
781 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
782 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
783 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
784 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400785 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
786 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200787 if (devices & ATOM_DEVICE_TV1_SUPPORT)
788 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
789 if (devices & ATOM_DEVICE_CV_SUPPORT)
790 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
791 }
792 }
793 i++;
794 }
795}
796
Dave Airlie4ce001a2009-08-13 16:32:14 +1000797static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200798{
799 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200800 bool ret = false;
801
802 if (rdev->bios) {
803 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400804 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
805 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200806 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500807 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200808 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500809 if (ret == false)
810 ret = radeon_get_legacy_connector_info_from_table(dev);
811 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200812 } else {
813 if (!ASIC_IS_AVIVO(rdev))
814 ret = radeon_get_legacy_connector_info_from_table(dev);
815 }
816 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000817 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200818 radeon_print_display_setup(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200819 }
820
821 return ret;
822}
823
824int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
825{
Alex Deucher3c537882010-02-05 04:21:19 -0500826 struct drm_device *dev = radeon_connector->base.dev;
827 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200828 int ret = 0;
829
Alex Deucher0ac66ef2014-07-14 17:57:19 -0400830 /* don't leak the edid if we already fetched it in detect() */
831 if (radeon_connector->edid)
832 goto got_edid;
833
Alex Deucher26b5bc92010-08-05 21:21:18 -0400834 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000835 if (radeon_connector->router.ddc_valid)
836 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400837
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100838 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
839 ENCODER_OBJECT_ID_NONE) {
Alex Deucher379dfc22014-04-07 10:33:46 -0400840 if (radeon_connector->ddc_bus->has_aux)
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100841 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
Alex Deucher379dfc22014-04-07 10:33:46 -0400842 &radeon_connector->ddc_bus->aux.ddc);
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100843 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
844 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000845 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Alex Deucherb06947b2011-09-02 14:23:09 +0000846
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000847 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
Alex Deucher379dfc22014-04-07 10:33:46 -0400848 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
849 radeon_connector->ddc_bus->has_aux)
Alex Deucherb06947b2011-09-02 14:23:09 +0000850 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
Alex Deucher379dfc22014-04-07 10:33:46 -0400851 &radeon_connector->ddc_bus->aux.ddc);
Alex Deucherb06947b2011-09-02 14:23:09 +0000852 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
853 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
854 &radeon_connector->ddc_bus->adapter);
855 } else {
856 if (radeon_connector->ddc_bus && !radeon_connector->edid)
857 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
858 &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400859 }
Alex Deucherc324acd2010-12-08 22:13:06 -0500860
861 if (!radeon_connector->edid) {
862 if (rdev->is_atom_bios) {
863 /* some laptops provide a hardcoded edid in rom for LCDs */
864 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
865 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
866 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
867 } else
868 /* some servers provide a hardcoded edid in rom for KVMs */
869 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
870 }
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400871 if (radeon_connector->edid) {
Alex Deucher0ac66ef2014-07-14 17:57:19 -0400872got_edid:
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400873 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
874 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Alex Deucher16086272014-03-31 11:19:46 -0400875 drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200876 return ret;
877 }
878 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000879 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200880}
881
Alex Deucherf523f742011-01-31 16:48:52 -0500882/* avivo */
Christian König32167012014-03-28 18:55:10 +0100883
884/**
885 * avivo_reduce_ratio - fractional number reduction
886 *
887 * @nom: nominator
888 * @den: denominator
889 * @nom_min: minimum value for nominator
890 * @den_min: minimum value for denominator
891 *
892 * Find the greatest common divisor and apply it on both nominator and
893 * denominator, but make nominator and denominator are at least as large
894 * as their minimum values.
895 */
896static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
897 unsigned nom_min, unsigned den_min)
Alex Deucherf523f742011-01-31 16:48:52 -0500898{
Christian König32167012014-03-28 18:55:10 +0100899 unsigned tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500900
Christian König32167012014-03-28 18:55:10 +0100901 /* reduce the numbers to a simpler ratio */
902 tmp = gcd(*nom, *den);
903 *nom /= tmp;
904 *den /= tmp;
Alex Deuchera4b40d5d2011-02-14 11:43:10 -0500905
Christian König32167012014-03-28 18:55:10 +0100906 /* make sure nominator is large enough */
907 if (*nom < nom_min) {
Christian König3b333c52014-04-24 18:39:59 +0200908 tmp = DIV_ROUND_UP(nom_min, *nom);
Christian König32167012014-03-28 18:55:10 +0100909 *nom *= tmp;
910 *den *= tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500911 }
912
Christian König32167012014-03-28 18:55:10 +0100913 /* make sure the denominator is large enough */
914 if (*den < den_min) {
Christian König3b333c52014-04-24 18:39:59 +0200915 tmp = DIV_ROUND_UP(den_min, *den);
Christian König32167012014-03-28 18:55:10 +0100916 *nom *= tmp;
917 *den *= tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500918 }
Alex Deucherf523f742011-01-31 16:48:52 -0500919}
920
Christian König32167012014-03-28 18:55:10 +0100921/**
Christian Königc2fb3092014-04-20 13:24:32 +0200922 * avivo_get_fb_ref_div - feedback and ref divider calculation
923 *
924 * @nom: nominator
925 * @den: denominator
926 * @post_div: post divider
927 * @fb_div_max: feedback divider maximum
928 * @ref_div_max: reference divider maximum
929 * @fb_div: resulting feedback divider
930 * @ref_div: resulting reference divider
931 *
932 * Calculate feedback and reference divider for a given post divider. Makes
933 * sure we stay within the limits.
934 */
935static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
936 unsigned fb_div_max, unsigned ref_div_max,
937 unsigned *fb_div, unsigned *ref_div)
938{
939 /* limit reference * post divider to a maximum */
Christian König4b21ce12014-05-21 15:25:41 +0200940 ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
Christian Königc2fb3092014-04-20 13:24:32 +0200941
942 /* get matching reference and feedback divider */
943 *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
944 *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
945
946 /* limit fb divider to its maximum */
947 if (*fb_div > fb_div_max) {
948 *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
949 *fb_div = fb_div_max;
950 }
951}
952
953/**
Christian König32167012014-03-28 18:55:10 +0100954 * radeon_compute_pll_avivo - compute PLL paramaters
955 *
956 * @pll: information about the PLL
957 * @dot_clock_p: resulting pixel clock
958 * fb_div_p: resulting feedback divider
959 * frac_fb_div_p: fractional part of the feedback divider
960 * ref_div_p: resulting reference divider
961 * post_div_p: resulting reference divider
962 *
963 * Try to calculate the PLL parameters to generate the given frequency:
964 * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
965 */
Alex Deucherf523f742011-01-31 16:48:52 -0500966void radeon_compute_pll_avivo(struct radeon_pll *pll,
967 u32 freq,
968 u32 *dot_clock_p,
969 u32 *fb_div_p,
970 u32 *frac_fb_div_p,
971 u32 *ref_div_p,
972 u32 *post_div_p)
973{
Christian Königc2fb3092014-04-20 13:24:32 +0200974 unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
975 freq : freq / 10;
976
Christian König32167012014-03-28 18:55:10 +0100977 unsigned fb_div_min, fb_div_max, fb_div;
978 unsigned post_div_min, post_div_max, post_div;
979 unsigned ref_div_min, ref_div_max, ref_div;
980 unsigned post_div_best, diff_best;
Christian Königf8a26452014-04-16 11:54:21 +0200981 unsigned nom, den;
Alex Deucherf523f742011-01-31 16:48:52 -0500982
Christian König32167012014-03-28 18:55:10 +0100983 /* determine allowed feedback divider range */
984 fb_div_min = pll->min_feedback_div;
985 fb_div_max = pll->max_feedback_div;
Alex Deucherf523f742011-01-31 16:48:52 -0500986
987 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Christian König32167012014-03-28 18:55:10 +0100988 fb_div_min *= 10;
989 fb_div_max *= 10;
Alex Deucherf523f742011-01-31 16:48:52 -0500990 }
991
Christian König32167012014-03-28 18:55:10 +0100992 /* determine allowed ref divider range */
993 if (pll->flags & RADEON_PLL_USE_REF_DIV)
994 ref_div_min = pll->reference_div;
995 else
996 ref_div_min = pll->min_ref_div;
Christian König24315812014-04-19 18:57:14 +0200997
998 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
999 pll->flags & RADEON_PLL_USE_REF_DIV)
1000 ref_div_max = pll->reference_div;
1001 else
1002 ref_div_max = pll->max_ref_div;
Christian König32167012014-03-28 18:55:10 +01001003
1004 /* determine allowed post divider range */
1005 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
1006 post_div_min = pll->post_div;
1007 post_div_max = pll->post_div;
1008 } else {
Christian König32167012014-03-28 18:55:10 +01001009 unsigned vco_min, vco_max;
1010
1011 if (pll->flags & RADEON_PLL_IS_LCD) {
1012 vco_min = pll->lcd_pll_out_min;
1013 vco_max = pll->lcd_pll_out_max;
1014 } else {
1015 vco_min = pll->pll_out_min;
1016 vco_max = pll->pll_out_max;
1017 }
1018
Christian Königc2fb3092014-04-20 13:24:32 +02001019 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1020 vco_min *= 10;
1021 vco_max *= 10;
1022 }
1023
Christian König32167012014-03-28 18:55:10 +01001024 post_div_min = vco_min / target_clock;
1025 if ((target_clock * post_div_min) < vco_min)
1026 ++post_div_min;
1027 if (post_div_min < pll->min_post_div)
1028 post_div_min = pll->min_post_div;
1029
1030 post_div_max = vco_max / target_clock;
1031 if ((target_clock * post_div_max) > vco_max)
1032 --post_div_max;
1033 if (post_div_max > pll->max_post_div)
1034 post_div_max = pll->max_post_div;
1035 }
1036
1037 /* represent the searched ratio as fractional number */
Christian Königc2fb3092014-04-20 13:24:32 +02001038 nom = target_clock;
Christian König32167012014-03-28 18:55:10 +01001039 den = pll->reference_freq;
1040
1041 /* reduce the numbers to a simpler ratio */
1042 avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
1043
1044 /* now search for a post divider */
1045 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
1046 post_div_best = post_div_min;
1047 else
1048 post_div_best = post_div_max;
1049 diff_best = ~0;
1050
1051 for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
Christian Königc2fb3092014-04-20 13:24:32 +02001052 unsigned diff;
1053 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
1054 ref_div_max, &fb_div, &ref_div);
1055 diff = abs(target_clock - (pll->reference_freq * fb_div) /
1056 (ref_div * post_div));
1057
Christian König32167012014-03-28 18:55:10 +01001058 if (diff < diff_best || (diff == diff_best &&
1059 !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
1060
1061 post_div_best = post_div;
1062 diff_best = diff;
1063 }
1064 }
1065 post_div = post_div_best;
1066
Christian Königc2fb3092014-04-20 13:24:32 +02001067 /* get the feedback and reference divider for the optimal value */
1068 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
1069 &fb_div, &ref_div);
Christian König32167012014-03-28 18:55:10 +01001070
1071 /* reduce the numbers to a simpler ratio once more */
1072 /* this also makes sure that the reference divider is large enough */
1073 avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
1074
Christian König3b333c52014-04-24 18:39:59 +02001075 /* avoid high jitter with small fractional dividers */
1076 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
Christian König74ad54f2014-05-13 12:50:54 +02001077 fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
Christian König3b333c52014-04-24 18:39:59 +02001078 if (fb_div < fb_div_min) {
1079 unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
1080 fb_div *= tmp;
1081 ref_div *= tmp;
1082 }
1083 }
1084
Christian König32167012014-03-28 18:55:10 +01001085 /* and finally save the result */
1086 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1087 *fb_div_p = fb_div / 10;
1088 *frac_fb_div_p = fb_div % 10;
1089 } else {
1090 *fb_div_p = fb_div;
1091 *frac_fb_div_p = 0;
1092 }
1093
1094 *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
1095 (pll->reference_freq * *frac_fb_div_p)) /
1096 (ref_div * post_div * 10);
Alex Deucherf523f742011-01-31 16:48:52 -05001097 *ref_div_p = ref_div;
1098 *post_div_p = post_div;
Christian König32167012014-03-28 18:55:10 +01001099
1100 DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
Christian Königc2fb3092014-04-20 13:24:32 +02001101 freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
Christian König32167012014-03-28 18:55:10 +01001102 ref_div, post_div);
Alex Deucherf523f742011-01-31 16:48:52 -05001103}
1104
1105/* pre-avivo */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001106static inline uint32_t radeon_div(uint64_t n, uint32_t d)
1107{
1108 uint64_t mod;
1109
1110 n += d / 2;
1111
1112 mod = do_div(n, d);
1113 return n;
1114}
1115
Alex Deucherf523f742011-01-31 16:48:52 -05001116void radeon_compute_pll_legacy(struct radeon_pll *pll,
1117 uint64_t freq,
1118 uint32_t *dot_clock_p,
1119 uint32_t *fb_div_p,
1120 uint32_t *frac_fb_div_p,
1121 uint32_t *ref_div_p,
1122 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001123{
1124 uint32_t min_ref_div = pll->min_ref_div;
1125 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -05001126 uint32_t min_post_div = pll->min_post_div;
1127 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001128 uint32_t min_fractional_feed_div = 0;
1129 uint32_t max_fractional_feed_div = 0;
1130 uint32_t best_vco = pll->best_vco;
1131 uint32_t best_post_div = 1;
1132 uint32_t best_ref_div = 1;
1133 uint32_t best_feedback_div = 1;
1134 uint32_t best_frac_feedback_div = 0;
1135 uint32_t best_freq = -1;
1136 uint32_t best_error = 0xffffffff;
1137 uint32_t best_vco_diff = 1;
1138 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001139 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001140
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001141 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 +02001142 freq = freq * 1000;
1143
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001144 if (pll->flags & RADEON_PLL_IS_LCD) {
1145 pll_out_min = pll->lcd_pll_out_min;
1146 pll_out_max = pll->lcd_pll_out_max;
1147 } else {
1148 pll_out_min = pll->pll_out_min;
1149 pll_out_max = pll->pll_out_max;
1150 }
1151
Alex Deucher619efb12011-01-31 16:48:53 -05001152 if (pll_out_min > 64800)
1153 pll_out_min = 64800;
1154
Alex Deucherfc103322010-01-19 17:16:10 -05001155 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001156 min_ref_div = max_ref_div = pll->reference_div;
1157 else {
1158 while (min_ref_div < max_ref_div-1) {
1159 uint32_t mid = (min_ref_div + max_ref_div) / 2;
1160 uint32_t pll_in = pll->reference_freq / mid;
1161 if (pll_in < pll->pll_in_min)
1162 max_ref_div = mid;
1163 else if (pll_in > pll->pll_in_max)
1164 min_ref_div = mid;
1165 else
1166 break;
1167 }
1168 }
1169
Alex Deucherfc103322010-01-19 17:16:10 -05001170 if (pll->flags & RADEON_PLL_USE_POST_DIV)
1171 min_post_div = max_post_div = pll->post_div;
1172
1173 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001174 min_fractional_feed_div = pll->min_frac_feedback_div;
1175 max_fractional_feed_div = pll->max_frac_feedback_div;
1176 }
1177
Alex Deucherbd6a60a2011-02-21 01:11:59 -05001178 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001179 uint32_t ref_div;
1180
Alex Deucherfc103322010-01-19 17:16:10 -05001181 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001182 continue;
1183
1184 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -05001185 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001186 if ((post_div == 5) ||
1187 (post_div == 7) ||
1188 (post_div == 9) ||
1189 (post_div == 10) ||
1190 (post_div == 11) ||
1191 (post_div == 13) ||
1192 (post_div == 14) ||
1193 (post_div == 15))
1194 continue;
1195 }
1196
1197 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1198 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1199 uint32_t pll_in = pll->reference_freq / ref_div;
1200 uint32_t min_feed_div = pll->min_feedback_div;
1201 uint32_t max_feed_div = pll->max_feedback_div + 1;
1202
1203 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1204 continue;
1205
1206 while (min_feed_div < max_feed_div) {
1207 uint32_t vco;
1208 uint32_t min_frac_feed_div = min_fractional_feed_div;
1209 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1210 uint32_t frac_feedback_div;
1211 uint64_t tmp;
1212
1213 feedback_div = (min_feed_div + max_feed_div) / 2;
1214
1215 tmp = (uint64_t)pll->reference_freq * feedback_div;
1216 vco = radeon_div(tmp, ref_div);
1217
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001218 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001219 min_feed_div = feedback_div + 1;
1220 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001221 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001222 max_feed_div = feedback_div;
1223 continue;
1224 }
1225
1226 while (min_frac_feed_div < max_frac_feed_div) {
1227 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1228 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1229 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1230 current_freq = radeon_div(tmp, ref_div * post_div);
1231
Alex Deucherfc103322010-01-19 17:16:10 -05001232 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +02001233 if (freq < current_freq)
1234 error = 0xffffffff;
1235 else
1236 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -04001237 } else
1238 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001239 vco_diff = abs(vco - best_vco);
1240
1241 if ((best_vco == 0 && error < best_error) ||
1242 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +02001243 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +10001244 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001245 best_post_div = post_div;
1246 best_ref_div = ref_div;
1247 best_feedback_div = feedback_div;
1248 best_frac_feedback_div = frac_feedback_div;
1249 best_freq = current_freq;
1250 best_error = error;
1251 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +10001252 } else if (current_freq == freq) {
1253 if (best_freq == -1) {
1254 best_post_div = post_div;
1255 best_ref_div = ref_div;
1256 best_feedback_div = feedback_div;
1257 best_frac_feedback_div = frac_feedback_div;
1258 best_freq = current_freq;
1259 best_error = error;
1260 best_vco_diff = vco_diff;
1261 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1262 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1263 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1264 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1265 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1266 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1267 best_post_div = post_div;
1268 best_ref_div = ref_div;
1269 best_feedback_div = feedback_div;
1270 best_frac_feedback_div = frac_feedback_div;
1271 best_freq = current_freq;
1272 best_error = error;
1273 best_vco_diff = vco_diff;
1274 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001275 }
1276 if (current_freq < freq)
1277 min_frac_feed_div = frac_feedback_div + 1;
1278 else
1279 max_frac_feed_div = frac_feedback_div;
1280 }
1281 if (current_freq < freq)
1282 min_feed_div = feedback_div + 1;
1283 else
1284 max_feed_div = feedback_div;
1285 }
1286 }
1287 }
1288
1289 *dot_clock_p = best_freq / 10000;
1290 *fb_div_p = best_feedback_div;
1291 *frac_fb_div_p = best_frac_feedback_div;
1292 *ref_div_p = best_ref_div;
1293 *post_div_p = best_post_div;
Joe Perchesbbb0aef52011-04-17 20:35:52 -07001294 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1295 (long long)freq,
1296 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
Alex Deucher51d4bf82011-01-31 16:48:51 -05001297 best_ref_div, best_post_div);
1298
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001299}
1300
1301static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1302{
1303 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001304
Dave Airlie29d08b32010-09-27 16:17:17 +10001305 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001306 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +10001307 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001308 drm_framebuffer_cleanup(fb);
1309 kfree(radeon_fb);
1310}
1311
1312static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1313 struct drm_file *file_priv,
1314 unsigned int *handle)
1315{
1316 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1317
1318 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1319}
1320
1321static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1322 .destroy = radeon_user_framebuffer_destroy,
1323 .create_handle = radeon_user_framebuffer_create_handle,
1324};
1325
Dave Airlieaaefcd42012-03-06 10:44:40 +00001326int
Dave Airlie38651672010-03-30 05:34:13 +00001327radeon_framebuffer_init(struct drm_device *dev,
1328 struct radeon_framebuffer *rfb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001329 struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlie38651672010-03-30 05:34:13 +00001330 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001331{
Dave Airlieaaefcd42012-03-06 10:44:40 +00001332 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00001333 rfb->obj = obj;
Daniel Vetterc7d73f62012-12-13 23:38:38 +01001334 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001335 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1336 if (ret) {
1337 rfb->obj = NULL;
1338 return ret;
1339 }
Dave Airlieaaefcd42012-03-06 10:44:40 +00001340 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001341}
1342
1343static struct drm_framebuffer *
1344radeon_user_framebuffer_create(struct drm_device *dev,
1345 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001346 struct drm_mode_fb_cmd2 *mode_cmd)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001347{
1348 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00001349 struct radeon_framebuffer *radeon_fb;
Dave Airlieaaefcd42012-03-06 10:44:40 +00001350 int ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001351
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001352 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001353 if (obj == NULL) {
1354 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001355 "can't create framebuffer\n", mode_cmd->handles[0]);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001356 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001357 }
Dave Airlie38651672010-03-30 05:34:13 +00001358
1359 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001360 if (radeon_fb == NULL) {
1361 drm_gem_object_unreference_unlocked(obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001362 return ERR_PTR(-ENOMEM);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001363 }
Dave Airlie38651672010-03-30 05:34:13 +00001364
Dave Airlieaaefcd42012-03-06 10:44:40 +00001365 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1366 if (ret) {
1367 kfree(radeon_fb);
1368 drm_gem_object_unreference_unlocked(obj);
xueminsub2f4b032013-01-22 22:16:53 +08001369 return ERR_PTR(ret);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001370 }
Dave Airlie38651672010-03-30 05:34:13 +00001371
1372 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001373}
1374
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001375static void radeon_output_poll_changed(struct drm_device *dev)
1376{
1377 struct radeon_device *rdev = dev->dev_private;
1378 radeon_fb_output_poll_changed(rdev);
1379}
1380
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001381static const struct drm_mode_config_funcs radeon_mode_funcs = {
1382 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001383 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001384};
1385
Dave Airlie445282d2009-09-09 17:40:54 +10001386static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1387{ { 0, "driver" },
1388 { 1, "bios" },
1389};
1390
1391static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1392{ { TV_STD_NTSC, "ntsc" },
1393 { TV_STD_PAL, "pal" },
1394 { TV_STD_PAL_M, "pal-m" },
1395 { TV_STD_PAL_60, "pal-60" },
1396 { TV_STD_NTSC_J, "ntsc-j" },
1397 { TV_STD_SCART_PAL, "scart-pal" },
1398 { TV_STD_PAL_CN, "pal-cn" },
1399 { TV_STD_SECAM, "secam" },
1400};
1401
Alex Deucher5b1714d2010-08-03 19:59:20 -04001402static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1403{ { UNDERSCAN_OFF, "off" },
1404 { UNDERSCAN_ON, "on" },
1405 { UNDERSCAN_AUTO, "auto" },
1406};
1407
Alex Deucher8666c072013-09-03 14:58:44 -04001408static struct drm_prop_enum_list radeon_audio_enum_list[] =
1409{ { RADEON_AUDIO_DISABLE, "off" },
1410 { RADEON_AUDIO_ENABLE, "on" },
1411 { RADEON_AUDIO_AUTO, "auto" },
1412};
1413
Alex Deucher6214bb72013-09-24 17:26:26 -04001414/* XXX support different dither options? spatial, temporal, both, etc. */
1415static struct drm_prop_enum_list radeon_dither_enum_list[] =
1416{ { RADEON_FMT_DITHER_DISABLE, "off" },
1417 { RADEON_FMT_DITHER_ENABLE, "on" },
1418};
1419
Alex Deucherd79766f2009-12-17 19:00:29 -05001420static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +10001421{
Sascha Hauer4a67d392012-02-06 10:58:17 +01001422 int sz;
Dave Airlie445282d2009-09-09 17:40:54 +10001423
1424 if (rdev->is_atom_bios) {
1425 rdev->mode_info.coherent_mode_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001426 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001427 if (!rdev->mode_info.coherent_mode_property)
1428 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001429 }
1430
1431 if (!ASIC_IS_AVIVO(rdev)) {
1432 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1433 rdev->mode_info.tmds_pll_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001434 drm_property_create_enum(rdev->ddev, 0,
1435 "tmds_pll",
1436 radeon_tmds_pll_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001437 }
1438
1439 rdev->mode_info.load_detect_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001440 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001441 if (!rdev->mode_info.load_detect_property)
1442 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001443
1444 drm_mode_create_scaling_mode_property(rdev->ddev);
1445
1446 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1447 rdev->mode_info.tv_std_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001448 drm_property_create_enum(rdev->ddev, 0,
1449 "tv standard",
1450 radeon_tv_std_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001451
Alex Deucher5b1714d2010-08-03 19:59:20 -04001452 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1453 rdev->mode_info.underscan_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001454 drm_property_create_enum(rdev->ddev, 0,
1455 "underscan",
1456 radeon_underscan_enum_list, sz);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001457
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001458 rdev->mode_info.underscan_hborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001459 drm_property_create_range(rdev->ddev, 0,
1460 "underscan hborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001461 if (!rdev->mode_info.underscan_hborder_property)
1462 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001463
1464 rdev->mode_info.underscan_vborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001465 drm_property_create_range(rdev->ddev, 0,
1466 "underscan vborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001467 if (!rdev->mode_info.underscan_vborder_property)
1468 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001469
Alex Deucher8666c072013-09-03 14:58:44 -04001470 sz = ARRAY_SIZE(radeon_audio_enum_list);
1471 rdev->mode_info.audio_property =
1472 drm_property_create_enum(rdev->ddev, 0,
1473 "audio",
1474 radeon_audio_enum_list, sz);
1475
Alex Deucher6214bb72013-09-24 17:26:26 -04001476 sz = ARRAY_SIZE(radeon_dither_enum_list);
1477 rdev->mode_info.dither_property =
1478 drm_property_create_enum(rdev->ddev, 0,
1479 "dither",
1480 radeon_dither_enum_list, sz);
1481
Dave Airlie445282d2009-09-09 17:40:54 +10001482 return 0;
1483}
1484
Alex Deucherf46c0122010-03-31 00:33:27 -04001485void radeon_update_display_priority(struct radeon_device *rdev)
1486{
1487 /* adjustment options for the display watermarks */
1488 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1489 /* set display priority to high for r3xx, rv515 chips
1490 * this avoids flickering due to underflow to the
1491 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001492 * Don't force high on rs4xx igp chips as it seems to
1493 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001494 */
Alex Deucher45737442010-05-20 11:26:11 -04001495 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1496 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001497 rdev->disp_priority = 2;
1498 else
1499 rdev->disp_priority = 0;
1500 } else
1501 rdev->disp_priority = radeon_disp_priority;
1502
1503}
1504
Alex Deucher07839862012-05-14 16:52:29 +02001505/*
1506 * Allocate hdmi structs and determine register offsets
1507 */
1508static void radeon_afmt_init(struct radeon_device *rdev)
1509{
1510 int i;
1511
1512 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1513 rdev->mode_info.afmt[i] = NULL;
1514
Alex Deucherb5306022013-07-31 16:51:33 -04001515 if (ASIC_IS_NODCE(rdev)) {
1516 /* nothing to do */
Alex Deucher07839862012-05-14 16:52:29 +02001517 } else if (ASIC_IS_DCE4(rdev)) {
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001518 static uint32_t eg_offsets[] = {
1519 EVERGREEN_CRTC0_REGISTER_OFFSET,
1520 EVERGREEN_CRTC1_REGISTER_OFFSET,
1521 EVERGREEN_CRTC2_REGISTER_OFFSET,
1522 EVERGREEN_CRTC3_REGISTER_OFFSET,
1523 EVERGREEN_CRTC4_REGISTER_OFFSET,
1524 EVERGREEN_CRTC5_REGISTER_OFFSET,
Alex Deucherb5306022013-07-31 16:51:33 -04001525 0x13830 - 0x7030,
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001526 };
1527 int num_afmt;
1528
Alex Deucherb5306022013-07-31 16:51:33 -04001529 /* DCE8 has 7 audio blocks tied to DIG encoders */
1530 /* DCE6 has 6 audio blocks tied to DIG encoders */
Alex Deucher07839862012-05-14 16:52:29 +02001531 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1532 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
Alex Deucherb5306022013-07-31 16:51:33 -04001533 if (ASIC_IS_DCE8(rdev))
1534 num_afmt = 7;
1535 else if (ASIC_IS_DCE6(rdev))
1536 num_afmt = 6;
1537 else if (ASIC_IS_DCE5(rdev))
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001538 num_afmt = 6;
1539 else if (ASIC_IS_DCE41(rdev))
1540 num_afmt = 2;
1541 else /* DCE4 */
1542 num_afmt = 6;
1543
1544 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1545 for (i = 0; i < num_afmt; i++) {
1546 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1547 if (rdev->mode_info.afmt[i]) {
1548 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1549 rdev->mode_info.afmt[i]->id = i;
Alex Deucher07839862012-05-14 16:52:29 +02001550 }
1551 }
1552 } else if (ASIC_IS_DCE3(rdev)) {
1553 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1554 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1555 if (rdev->mode_info.afmt[0]) {
1556 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1557 rdev->mode_info.afmt[0]->id = 0;
1558 }
1559 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1560 if (rdev->mode_info.afmt[1]) {
1561 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1562 rdev->mode_info.afmt[1]->id = 1;
1563 }
1564 } else if (ASIC_IS_DCE2(rdev)) {
1565 /* DCE2 has at least 1 routable audio block */
1566 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1567 if (rdev->mode_info.afmt[0]) {
1568 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1569 rdev->mode_info.afmt[0]->id = 0;
1570 }
1571 /* r6xx has 2 routable audio blocks */
1572 if (rdev->family >= CHIP_R600) {
1573 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1574 if (rdev->mode_info.afmt[1]) {
1575 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1576 rdev->mode_info.afmt[1]->id = 1;
1577 }
1578 }
1579 }
1580}
1581
1582static void radeon_afmt_fini(struct radeon_device *rdev)
1583{
1584 int i;
1585
1586 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1587 kfree(rdev->mode_info.afmt[i]);
1588 rdev->mode_info.afmt[i] = NULL;
1589 }
1590}
1591
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001592int radeon_modeset_init(struct radeon_device *rdev)
1593{
Alex Deucher18917b62010-02-01 16:02:25 -05001594 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001595 int ret;
1596
1597 drm_mode_config_init(rdev->ddev);
1598 rdev->mode_info.mode_config_initialized = true;
1599
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001600 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001601
Alex Deucher881dd742011-01-06 21:19:14 -05001602 if (ASIC_IS_DCE5(rdev)) {
1603 rdev->ddev->mode_config.max_width = 16384;
1604 rdev->ddev->mode_config.max_height = 16384;
1605 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001606 rdev->ddev->mode_config.max_width = 8192;
1607 rdev->ddev->mode_config.max_height = 8192;
1608 } else {
1609 rdev->ddev->mode_config.max_width = 4096;
1610 rdev->ddev->mode_config.max_height = 4096;
1611 }
1612
Dave Airlie019d96c2011-09-29 16:20:42 +01001613 rdev->ddev->mode_config.preferred_depth = 24;
1614 rdev->ddev->mode_config.prefer_shadow = 1;
1615
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001616 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1617
Dave Airlie445282d2009-09-09 17:40:54 +10001618 ret = radeon_modeset_create_props(rdev);
1619 if (ret) {
1620 return ret;
1621 }
Dave Airliedfee5612009-10-02 09:19:09 +10001622
Alex Deucherf376b942010-08-05 21:21:16 -04001623 /* init i2c buses */
1624 radeon_i2c_init(rdev);
1625
Alex Deucher3c537882010-02-05 04:21:19 -05001626 /* check combios for a valid hardcoded EDID - Sun servers */
1627 if (!rdev->is_atom_bios) {
1628 /* check for hardcoded EDID in BIOS */
1629 radeon_combios_check_hardcoded_edid(rdev);
1630 }
1631
Dave Airliedfee5612009-10-02 09:19:09 +10001632 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001633 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001634 radeon_crtc_init(rdev->ddev, i);
1635 }
1636
1637 /* okay we should have all the bios connectors */
1638 ret = radeon_setup_enc_conn(rdev->ddev);
1639 if (!ret) {
1640 return ret;
1641 }
Alex Deucherac89af12011-05-22 13:20:36 -04001642
Alex Deucher3fa47d92012-01-20 14:56:39 -05001643 /* init dig PHYs, disp eng pll */
1644 if (rdev->is_atom_bios) {
Alex Deucherac89af12011-05-22 13:20:36 -04001645 radeon_atom_encoder_init(rdev);
Alex Deucherf3f1f032012-03-20 17:18:04 -04001646 radeon_atom_disp_eng_pll_init(rdev);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001647 }
Alex Deucherac89af12011-05-22 13:20:36 -04001648
Alex Deucherd4877cf2009-12-04 16:56:37 -05001649 /* initialize hpd */
1650 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001651
Alex Deucher07839862012-05-14 16:52:29 +02001652 /* setup afmt */
1653 radeon_afmt_init(rdev);
1654
Dave Airlie38651672010-03-30 05:34:13 +00001655 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001656 drm_kms_helper_poll_init(rdev->ddev);
1657
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001658 if (rdev->pm.dpm_enabled) {
1659 /* do dpm late init */
1660 ret = radeon_pm_late_init(rdev);
1661 if (ret) {
1662 rdev->pm.dpm_enabled = false;
1663 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1664 }
1665 /* set the dpm state for PX since there won't be
1666 * a modeset to call this.
1667 */
1668 radeon_pm_compute_clocks(rdev);
1669 }
1670
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001671 return 0;
1672}
1673
1674void radeon_modeset_fini(struct radeon_device *rdev)
1675{
Dave Airlie38651672010-03-30 05:34:13 +00001676 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001677 kfree(rdev->mode_info.bios_hardcoded_edid);
1678
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001679 if (rdev->mode_info.mode_config_initialized) {
Alex Deucher07839862012-05-14 16:52:29 +02001680 radeon_afmt_fini(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001681 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001682 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001683 drm_mode_config_cleanup(rdev->ddev);
1684 rdev->mode_info.mode_config_initialized = false;
1685 }
Alex Deucherf376b942010-08-05 21:21:16 -04001686 /* free i2c buses */
1687 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001688}
1689
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001690static bool is_hdtv_mode(const struct drm_display_mode *mode)
Alex Deucher039ed2d2010-08-20 11:57:19 -04001691{
1692 /* try and guess if this is a tv or a monitor */
1693 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1694 (mode->vdisplay == 576) || /* 576p */
1695 (mode->vdisplay == 720) || /* 720p */
1696 (mode->vdisplay == 1080)) /* 1080p */
1697 return true;
1698 else
1699 return false;
1700}
1701
Jerome Glissec93bb852009-07-13 21:04:08 +02001702bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001703 const struct drm_display_mode *mode,
Jerome Glissec93bb852009-07-13 21:04:08 +02001704 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001705{
Jerome Glissec93bb852009-07-13 21:04:08 +02001706 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001707 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001708 struct drm_encoder *encoder;
1709 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1710 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001711 struct drm_connector *connector;
1712 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001713 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001714 u32 src_v = 1, dst_v = 1;
1715 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001716
Alex Deucher5b1714d2010-08-03 19:59:20 -04001717 radeon_crtc->h_border = 0;
1718 radeon_crtc->v_border = 0;
1719
Jerome Glissec93bb852009-07-13 21:04:08 +02001720 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001721 if (encoder->crtc != crtc)
1722 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001723 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001724 connector = radeon_get_connector_for_encoder(encoder);
1725 radeon_connector = to_radeon_connector(connector);
1726
Jerome Glissec93bb852009-07-13 21:04:08 +02001727 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001728 /* set scaling */
1729 if (radeon_encoder->rmx_type == RMX_OFF)
1730 radeon_crtc->rmx_type = RMX_OFF;
1731 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1732 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1733 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1734 else
1735 radeon_crtc->rmx_type = RMX_OFF;
1736 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001737 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001738 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001739 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001740 src_v = crtc->mode.vdisplay;
1741 dst_v = radeon_crtc->native_mode.vdisplay;
1742 src_h = crtc->mode.hdisplay;
1743 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001744
1745 /* fix up for overscan on hdmi */
1746 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001747 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001748 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1749 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001750 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1751 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001752 if (radeon_encoder->underscan_hborder != 0)
1753 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1754 else
1755 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1756 if (radeon_encoder->underscan_vborder != 0)
1757 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1758 else
1759 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001760 radeon_crtc->rmx_type = RMX_FULL;
1761 src_v = crtc->mode.vdisplay;
1762 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1763 src_h = crtc->mode.hdisplay;
1764 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1765 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001766 first = false;
1767 } else {
1768 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1769 /* WARNING: Right now this can't happen but
1770 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001771 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001772 * (ie all encoder can work with the same
1773 * scaling).
1774 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001775 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001776 return false;
1777 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001778 }
1779 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001780 if (radeon_crtc->rmx_type != RMX_OFF) {
1781 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001782 a.full = dfixed_const(src_v);
1783 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001784 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001785 a.full = dfixed_const(src_h);
1786 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001787 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001788 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001789 radeon_crtc->vsc.full = dfixed_const(1);
1790 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001791 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001792 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001793}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001794
1795/*
Mario Kleinerd47abc52013-10-30 05:13:07 +01001796 * Retrieve current video scanout position of crtc on a given gpu, and
1797 * an optional accurate timestamp of when query happened.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001798 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001799 * \param dev Device to query.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001800 * \param crtc Crtc to query.
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001801 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
Mario Kleiner6383cf72010-10-05 19:57:36 -04001802 * \param *vpos Location where vertical scanout position should be stored.
1803 * \param *hpos Location where horizontal scanout position should go.
Mario Kleinerd47abc52013-10-30 05:13:07 +01001804 * \param *stime Target location for timestamp taken immediately before
1805 * scanout position query. Can be NULL to skip timestamp.
1806 * \param *etime Target location for timestamp taken immediately after
1807 * scanout position query. Can be NULL to skip timestamp.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001808 *
1809 * Returns vpos as a positive number while in active scanout area.
1810 * Returns vpos as a negative number inside vblank, counting the number
1811 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1812 * until start of active scanout / end of vblank."
1813 *
1814 * \return Flags, or'ed together as follows:
1815 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001816 * DRM_SCANOUTPOS_VALID = Query successful.
Mario Kleinerf5a80202010-10-23 04:42:17 +02001817 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1818 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
Mario Kleiner6383cf72010-10-05 19:57:36 -04001819 * this flag means that returned position may be offset by a constant but
1820 * unknown small number of scanlines wrt. real scanout position.
1821 *
1822 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001823int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1824 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
Mario Kleiner6383cf72010-10-05 19:57:36 -04001825{
1826 u32 stat_crtc = 0, vbl = 0, position = 0;
1827 int vbl_start, vbl_end, vtotal, ret = 0;
1828 bool in_vbl = true;
1829
Mario Kleinerf5a80202010-10-23 04:42:17 +02001830 struct radeon_device *rdev = dev->dev_private;
1831
Mario Kleinerd47abc52013-10-30 05:13:07 +01001832 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1833
1834 /* Get optional system timestamp before query. */
1835 if (stime)
1836 *stime = ktime_get();
1837
Mario Kleiner6383cf72010-10-05 19:57:36 -04001838 if (ASIC_IS_DCE4(rdev)) {
1839 if (crtc == 0) {
1840 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1841 EVERGREEN_CRTC0_REGISTER_OFFSET);
1842 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1843 EVERGREEN_CRTC0_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001844 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001845 }
1846 if (crtc == 1) {
1847 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1848 EVERGREEN_CRTC1_REGISTER_OFFSET);
1849 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1850 EVERGREEN_CRTC1_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001851 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001852 }
1853 if (crtc == 2) {
1854 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1855 EVERGREEN_CRTC2_REGISTER_OFFSET);
1856 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1857 EVERGREEN_CRTC2_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001858 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001859 }
1860 if (crtc == 3) {
1861 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1862 EVERGREEN_CRTC3_REGISTER_OFFSET);
1863 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1864 EVERGREEN_CRTC3_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001865 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001866 }
1867 if (crtc == 4) {
1868 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1869 EVERGREEN_CRTC4_REGISTER_OFFSET);
1870 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1871 EVERGREEN_CRTC4_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001872 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001873 }
1874 if (crtc == 5) {
1875 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1876 EVERGREEN_CRTC5_REGISTER_OFFSET);
1877 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1878 EVERGREEN_CRTC5_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001879 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001880 }
1881 } else if (ASIC_IS_AVIVO(rdev)) {
1882 if (crtc == 0) {
1883 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1884 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001885 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001886 }
1887 if (crtc == 1) {
1888 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1889 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001890 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001891 }
1892 } else {
1893 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1894 if (crtc == 0) {
1895 /* Assume vbl_end == 0, get vbl_start from
1896 * upper 16 bits.
1897 */
1898 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1899 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1900 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1901 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1902 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1903 if (!(stat_crtc & 1))
1904 in_vbl = false;
1905
Mario Kleinerf5a80202010-10-23 04:42:17 +02001906 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001907 }
1908 if (crtc == 1) {
1909 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1910 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1911 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1912 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1913 if (!(stat_crtc & 1))
1914 in_vbl = false;
1915
Mario Kleinerf5a80202010-10-23 04:42:17 +02001916 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001917 }
1918 }
1919
Mario Kleinerd47abc52013-10-30 05:13:07 +01001920 /* Get optional system timestamp after query. */
1921 if (etime)
1922 *etime = ktime_get();
1923
1924 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1925
Mario Kleiner6383cf72010-10-05 19:57:36 -04001926 /* Decode into vertical and horizontal scanout position. */
1927 *vpos = position & 0x1fff;
1928 *hpos = (position >> 16) & 0x1fff;
1929
1930 /* Valid vblank area boundaries from gpu retrieved? */
1931 if (vbl > 0) {
1932 /* Yes: Decode. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001933 ret |= DRM_SCANOUTPOS_ACCURATE;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001934 vbl_start = vbl & 0x1fff;
1935 vbl_end = (vbl >> 16) & 0x1fff;
1936 }
1937 else {
1938 /* No: Fake something reasonable which gives at least ok results. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001939 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001940 vbl_end = 0;
1941 }
1942
1943 /* Test scanout position against vblank region. */
1944 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1945 in_vbl = false;
1946
1947 /* Check if inside vblank area and apply corrective offsets:
1948 * vpos will then be >=0 in video scanout area, but negative
1949 * within vblank area, counting down the number of lines until
1950 * start of scanout.
1951 */
1952
1953 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1954 if (in_vbl && (*vpos >= vbl_start)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001955 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001956 *vpos = *vpos - vtotal;
1957 }
1958
1959 /* Correct for shifted end of vbl at vbl_end. */
1960 *vpos = *vpos - vbl_end;
1961
1962 /* In vblank? */
1963 if (in_vbl)
Mario Kleinerf5a80202010-10-23 04:42:17 +02001964 ret |= DRM_SCANOUTPOS_INVBL;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001965
Ville Syrjälä8072bfa2013-10-28 21:22:52 +02001966 /* Is vpos outside nominal vblank area, but less than
1967 * 1/100 of a frame height away from start of vblank?
1968 * If so, assume this isn't a massively delayed vblank
1969 * interrupt, but a vblank interrupt that fired a few
1970 * microseconds before true start of vblank. Compensate
1971 * by adding a full frame duration to the final timestamp.
1972 * Happens, e.g., on ATI R500, R600.
1973 *
1974 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1975 */
1976 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1977 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1978 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1979
1980 if (vbl_start - *vpos < vtotal / 100) {
1981 *vpos -= vtotal;
1982
1983 /* Signal this correction as "applied". */
1984 ret |= 0x8;
1985 }
1986 }
1987
Mario Kleiner6383cf72010-10-05 19:57:36 -04001988 return ret;
1989}