blob: 6b3de5ca5bc7207b2aa43f5ecd52fc869ffee188 [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
69 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
70}
71
Alex Deucherfee298f2011-01-06 21:19:30 -050072static void dce4_crtc_load_lut(struct drm_crtc *crtc)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050073{
74 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
75 struct drm_device *dev = crtc->dev;
76 struct radeon_device *rdev = dev->dev_private;
77 int i;
78
Dave Airlied9fdaaf2010-08-02 10:42:55 +100079 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050080 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
81
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
84 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
85
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
88 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
89
Alex Deucher677d0762010-04-22 22:58:50 -040090 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
91 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050092
Alex Deucher677d0762010-04-22 22:58:50 -040093 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050094 for (i = 0; i < 256; i++) {
Alex Deucher677d0762010-04-22 22:58:50 -040095 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050096 (radeon_crtc->lut_r[i] << 20) |
97 (radeon_crtc->lut_g[i] << 10) |
98 (radeon_crtc->lut_b[i] << 0));
99 }
100}
101
Alex Deucherfee298f2011-01-06 21:19:30 -0500102static void dce5_crtc_load_lut(struct drm_crtc *crtc)
103{
104 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
105 struct drm_device *dev = crtc->dev;
106 struct radeon_device *rdev = dev->dev_private;
107 int i;
108
109 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
110
111 WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
112 (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
113 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
114 WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
115 NI_GRPH_PRESCALE_BYPASS);
116 WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
117 NI_OVL_PRESCALE_BYPASS);
118 WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
119 (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
120 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
121
122 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
123
124 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
125 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
126 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
127
128 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
129 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
130 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
131
132 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
133 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
134
135 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
136 for (i = 0; i < 256; i++) {
137 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
138 (radeon_crtc->lut_r[i] << 20) |
139 (radeon_crtc->lut_g[i] << 10) |
140 (radeon_crtc->lut_b[i] << 0));
141 }
142
143 WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
144 (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
145 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
147 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
148 WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
149 (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
150 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
151 WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
152 (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
153 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
154 WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
155 (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
156 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
157 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
158 WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
Alex Deucher9e05fa12013-01-24 10:06:33 -0500159 if (ASIC_IS_DCE8(rdev)) {
160 /* XXX this only needs to be programmed once per crtc at startup,
161 * not sure where the best place for it is
162 */
163 WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
164 CIK_CURSOR_ALPHA_BLND_ENA);
165 }
Alex Deucherfee298f2011-01-06 21:19:30 -0500166}
167
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168static void legacy_crtc_load_lut(struct drm_crtc *crtc)
169{
170 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
171 struct drm_device *dev = crtc->dev;
172 struct radeon_device *rdev = dev->dev_private;
173 int i;
174 uint32_t dac2_cntl;
175
176 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
177 if (radeon_crtc->crtc_id == 0)
178 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
179 else
180 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
181 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
182
183 WREG8(RADEON_PALETTE_INDEX, 0);
184 for (i = 0; i < 256; i++) {
185 WREG32(RADEON_PALETTE_30_DATA,
186 (radeon_crtc->lut_r[i] << 20) |
187 (radeon_crtc->lut_g[i] << 10) |
188 (radeon_crtc->lut_b[i] << 0));
189 }
190}
191
192void radeon_crtc_load_lut(struct drm_crtc *crtc)
193{
194 struct drm_device *dev = crtc->dev;
195 struct radeon_device *rdev = dev->dev_private;
196
197 if (!crtc->enabled)
198 return;
199
Alex Deucherfee298f2011-01-06 21:19:30 -0500200 if (ASIC_IS_DCE5(rdev))
201 dce5_crtc_load_lut(crtc);
202 else if (ASIC_IS_DCE4(rdev))
203 dce4_crtc_load_lut(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500204 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200205 avivo_crtc_load_lut(crtc);
206 else
207 legacy_crtc_load_lut(crtc);
208}
209
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000210/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200211void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
212 u16 blue, int regno)
213{
214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
215
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200216 radeon_crtc->lut_r[regno] = red >> 6;
217 radeon_crtc->lut_g[regno] = green >> 6;
218 radeon_crtc->lut_b[regno] = blue >> 6;
219}
220
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000221/** Gets the color ramps on behalf of fbcon */
222void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
223 u16 *blue, int regno)
224{
225 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
226
227 *red = radeon_crtc->lut_r[regno] << 6;
228 *green = radeon_crtc->lut_g[regno] << 6;
229 *blue = radeon_crtc->lut_b[regno] << 6;
230}
231
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200232static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100233 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200234{
235 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100236 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200237
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000238 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100239 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000240 radeon_crtc->lut_r[i] = red[i] >> 6;
241 radeon_crtc->lut_g[i] = green[i] >> 6;
242 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200243 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244 radeon_crtc_load_lut(crtc);
245}
246
247static void radeon_crtc_destroy(struct drm_crtc *crtc)
248{
249 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
250
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200251 drm_crtc_cleanup(crtc);
Christian König1aab5512014-05-27 16:49:22 +0200252 destroy_workqueue(radeon_crtc->flip_queue);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200253 kfree(radeon_crtc);
254}
255
Christian König1aab5512014-05-27 16:49:22 +0200256/**
257 * radeon_unpin_work_func - unpin old buffer object
258 *
259 * @__work - kernel work item
260 *
261 * Unpin the old frame buffer object outside of the interrupt handler
Alex Deucher6f34be52010-11-21 10:59:01 -0500262 */
263static void radeon_unpin_work_func(struct work_struct *__work)
264{
Christian König1aab5512014-05-27 16:49:22 +0200265 struct radeon_flip_work *work =
266 container_of(__work, struct radeon_flip_work, unpin_work);
Alex Deucher6f34be52010-11-21 10:59:01 -0500267 int r;
268
269 /* unpin of the old buffer */
270 r = radeon_bo_reserve(work->old_rbo, false);
271 if (likely(r == 0)) {
272 r = radeon_bo_unpin(work->old_rbo);
273 if (unlikely(r != 0)) {
274 DRM_ERROR("failed to unpin buffer after flip\n");
275 }
276 radeon_bo_unreserve(work->old_rbo);
277 } else
278 DRM_ERROR("failed to reserve buffer after flip\n");
Dave Airlie498c5552011-05-29 17:48:32 +1000279
280 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500281 kfree(work);
282}
283
Christian König1a0e7912014-05-27 16:49:21 +0200284void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
Alex Deucher6f34be52010-11-21 10:59:01 -0500285{
286 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
Christian König1aab5512014-05-27 16:49:22 +0200287 struct radeon_flip_work *work;
Alex Deucher6f34be52010-11-21 10:59:01 -0500288 unsigned long flags;
289 u32 update_pending;
290 int vpos, hpos;
291
292 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
Christian König1aab5512014-05-27 16:49:22 +0200293 work = radeon_crtc->flip_work;
294 if (work == NULL) {
Alex Deucher6f34be52010-11-21 10:59:01 -0500295 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
296 return;
297 }
Christian König1aab5512014-05-27 16:49:22 +0200298
299 update_pending = radeon_page_flip_pending(rdev, crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500300
301 /* Has the pageflip already completed in crtc, or is it certain
302 * to complete in this vblank?
303 */
304 if (update_pending &&
Ville Syrjäläabca9e452013-10-28 20:50:48 +0200305 (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
Mario Kleinerd47abc52013-10-30 05:13:07 +0100306 &vpos, &hpos, NULL, NULL)) &&
Felix Kuehling81ffbbe2012-02-23 19:16:12 -0500307 ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
308 (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
309 /* crtc didn't flip in this target vblank interval,
310 * but flip is pending in crtc. Based on the current
311 * scanout position we know that the current frame is
312 * (nearly) complete and the flip will (likely)
313 * complete before the start of the next frame.
314 */
315 update_pending = 0;
316 }
Christian König1aab5512014-05-27 16:49:22 +0200317 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
318 if (!update_pending)
Christian König1a0e7912014-05-27 16:49:21 +0200319 radeon_crtc_handle_flip(rdev, crtc_id);
Christian König1a0e7912014-05-27 16:49:21 +0200320}
321
322/**
323 * radeon_crtc_handle_flip - page flip completed
324 *
325 * @rdev: radeon device pointer
326 * @crtc_id: crtc number this event is for
327 *
328 * Called when we are sure that a page flip for this crtc is completed.
329 */
330void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
331{
332 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
Christian König1aab5512014-05-27 16:49:22 +0200333 struct radeon_flip_work *work;
Christian König1a0e7912014-05-27 16:49:21 +0200334 unsigned long flags;
335
336 /* this can happen at init */
337 if (radeon_crtc == NULL)
338 return;
339
340 spin_lock_irqsave(&rdev->ddev->event_lock, flags);
Christian König1aab5512014-05-27 16:49:22 +0200341 work = radeon_crtc->flip_work;
Christian König1a0e7912014-05-27 16:49:21 +0200342 if (work == NULL) {
343 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
344 return;
Alex Deucher6f34be52010-11-21 10:59:01 -0500345 }
346
Christian König1aab5512014-05-27 16:49:22 +0200347 /* Pageflip completed. Clean up. */
348 radeon_crtc->flip_work = NULL;
Alex Deucher6f34be52010-11-21 10:59:01 -0500349
350 /* wakeup userspace */
Rob Clark26ae4662012-10-08 19:50:42 +0000351 if (work->event)
352 drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
353
Alex Deucher6f34be52010-11-21 10:59:01 -0500354 spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
355
Alex Deucher6f34be52010-11-21 10:59:01 -0500356 radeon_fence_unref(&work->fence);
Christian Könige928c612014-05-27 16:49:18 +0200357 radeon_irq_kms_pflip_irq_get(rdev, work->crtc_id);
Christian König1aab5512014-05-27 16:49:22 +0200358 queue_work(radeon_crtc->flip_queue, &work->unpin_work);
Alex Deucher6f34be52010-11-21 10:59:01 -0500359}
360
Christian König1aab5512014-05-27 16:49:22 +0200361/**
362 * radeon_flip_work_func - page flip framebuffer
363 *
364 * @work - kernel work item
365 *
366 * Wait for the buffer object to become idle and do the actual page flip
367 */
368static void radeon_flip_work_func(struct work_struct *__work)
Alex Deucher6f34be52010-11-21 10:59:01 -0500369{
Christian König1aab5512014-05-27 16:49:22 +0200370 struct radeon_flip_work *work =
371 container_of(__work, struct radeon_flip_work, flip_work);
372 struct radeon_device *rdev = work->rdev;
373 struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
374
375 struct drm_crtc *crtc = &radeon_crtc->base;
376 struct drm_framebuffer *fb = work->fb;
377
378 uint32_t tiling_flags, pitch_pixels;
379 uint64_t base;
380
Alex Deucher6f34be52010-11-21 10:59:01 -0500381 unsigned long flags;
Alex Deucher6f34be52010-11-21 10:59:01 -0500382 int r;
383
Christian König1aab5512014-05-27 16:49:22 +0200384 down_read(&rdev->exclusive_lock);
385 while (work->fence) {
386 r = radeon_fence_wait(work->fence, false);
387 if (r == -EDEADLK) {
388 up_read(&rdev->exclusive_lock);
389 r = radeon_gpu_reset(rdev);
390 down_read(&rdev->exclusive_lock);
391 }
Alex Deucher6f34be52010-11-21 10:59:01 -0500392
Christian König1aab5512014-05-27 16:49:22 +0200393 if (r) {
394 DRM_ERROR("failed to wait on page flip fence (%d)!\n",
395 r);
396 goto cleanup;
397 } else
398 radeon_fence_unref(&work->fence);
Alex Deucher6f34be52010-11-21 10:59:01 -0500399 }
Alex Deucher6f34be52010-11-21 10:59:01 -0500400
401 /* pin the new buffer */
Alex Deucher6f34be52010-11-21 10:59:01 -0500402 DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
Christian König1aab5512014-05-27 16:49:22 +0200403 work->old_rbo, work->new_rbo);
Alex Deucher6f34be52010-11-21 10:59:01 -0500404
Christian König1aab5512014-05-27 16:49:22 +0200405 r = radeon_bo_reserve(work->new_rbo, false);
Alex Deucher6f34be52010-11-21 10:59:01 -0500406 if (unlikely(r != 0)) {
407 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
Christian König1aab5512014-05-27 16:49:22 +0200408 goto cleanup;
Alex Deucher6f34be52010-11-21 10:59:01 -0500409 }
Michel Dänzer0349af72012-03-14 17:12:42 +0100410 /* Only 27 bit offset for legacy CRTC */
Christian König1aab5512014-05-27 16:49:22 +0200411 r = radeon_bo_pin_restricted(work->new_rbo, RADEON_GEM_DOMAIN_VRAM,
Michel Dänzer0349af72012-03-14 17:12:42 +0100412 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500413 if (unlikely(r != 0)) {
Christian König1aab5512014-05-27 16:49:22 +0200414 radeon_bo_unreserve(work->new_rbo);
Alex Deucher6f34be52010-11-21 10:59:01 -0500415 r = -EINVAL;
416 DRM_ERROR("failed to pin new rbo buffer before flip\n");
Christian König1aab5512014-05-27 16:49:22 +0200417 goto cleanup;
Alex Deucher6f34be52010-11-21 10:59:01 -0500418 }
Christian König1aab5512014-05-27 16:49:22 +0200419 radeon_bo_get_tiling_flags(work->new_rbo, &tiling_flags, NULL);
420 radeon_bo_unreserve(work->new_rbo);
Alex Deucher6f34be52010-11-21 10:59:01 -0500421
422 if (!ASIC_IS_AVIVO(rdev)) {
423 /* crtc offset is from display base addr not FB location */
424 base -= radeon_crtc->legacy_display_base_addr;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200425 pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
Alex Deucher6f34be52010-11-21 10:59:01 -0500426
427 if (tiling_flags & RADEON_TILING_MACRO) {
428 if (ASIC_IS_R300(rdev)) {
429 base &= ~0x7ff;
430 } else {
431 int byteshift = fb->bits_per_pixel >> 4;
432 int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
433 base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
434 }
435 } else {
436 int offset = crtc->y * pitch_pixels + crtc->x;
437 switch (fb->bits_per_pixel) {
438 case 8:
439 default:
440 offset *= 1;
441 break;
442 case 15:
443 case 16:
444 offset *= 2;
445 break;
446 case 24:
447 offset *= 3;
448 break;
449 case 32:
450 offset *= 4;
451 break;
452 }
453 base += offset;
454 }
455 base &= ~7;
456 }
457
Christian König1aab5512014-05-27 16:49:22 +0200458 /* We borrow the event spin lock for protecting flip_work */
459 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Alex Deucher6f34be52010-11-21 10:59:01 -0500460
Alex Deucher6f34be52010-11-21 10:59:01 -0500461 /* set the proper interrupt */
Christian Könige928c612014-05-27 16:49:18 +0200462 radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
Alex Deucher6f34be52010-11-21 10:59:01 -0500463
Christian König1aab5512014-05-27 16:49:22 +0200464 /* do the flip (mmio) */
465 radeon_page_flip(rdev, radeon_crtc->crtc_id, base);
Alex Deucher6f34be52010-11-21 10:59:01 -0500466
Christian König1aab5512014-05-27 16:49:22 +0200467 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
468 up_read(&rdev->exclusive_lock);
469
470 return;
471
472cleanup:
473 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
Michel Dänzerfcc485d2011-07-13 15:18:09 +0000474 radeon_fence_unref(&work->fence);
Alex Deucher6f34be52010-11-21 10:59:01 -0500475 kfree(work);
Christian König1aab5512014-05-27 16:49:22 +0200476 up_read(&rdev->exclusive_lock);
477}
Alex Deucher6f34be52010-11-21 10:59:01 -0500478
Christian König1aab5512014-05-27 16:49:22 +0200479static int radeon_crtc_page_flip(struct drm_crtc *crtc,
480 struct drm_framebuffer *fb,
481 struct drm_pending_vblank_event *event,
482 uint32_t page_flip_flags)
483{
484 struct drm_device *dev = crtc->dev;
485 struct radeon_device *rdev = dev->dev_private;
486 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
487 struct radeon_framebuffer *old_radeon_fb;
488 struct radeon_framebuffer *new_radeon_fb;
489 struct drm_gem_object *obj;
490 struct radeon_flip_work *work;
491 unsigned long flags;
492
493 work = kzalloc(sizeof *work, GFP_KERNEL);
494 if (work == NULL)
495 return -ENOMEM;
496
497 INIT_WORK(&work->flip_work, radeon_flip_work_func);
498 INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
499
500 work->rdev = rdev;
501 work->crtc_id = radeon_crtc->crtc_id;
502 work->fb = fb;
503 work->event = event;
504
505 /* schedule unpin of the old buffer */
506 old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
507 obj = old_radeon_fb->obj;
508
509 /* take a reference to the old object */
510 drm_gem_object_reference(obj);
511 work->old_rbo = gem_to_radeon_bo(obj);
512
513 new_radeon_fb = to_radeon_framebuffer(fb);
514 obj = new_radeon_fb->obj;
515 work->new_rbo = gem_to_radeon_bo(obj);
516
517 spin_lock(&work->new_rbo->tbo.bdev->fence_lock);
518 if (work->new_rbo->tbo.sync_obj)
519 work->fence = radeon_fence_ref(work->new_rbo->tbo.sync_obj);
520 spin_unlock(&work->new_rbo->tbo.bdev->fence_lock);
521
522 /* update crtc fb */
523 crtc->primary->fb = fb;
524
525 /* We borrow the event spin lock for protecting flip_work */
526 spin_lock_irqsave(&crtc->dev->event_lock, flags);
527
528 if (radeon_crtc->flip_work) {
529 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
530 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
531 drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
532 radeon_fence_unref(&work->fence);
533 kfree(work);
534 return -EBUSY;
535 }
536 radeon_crtc->flip_work = work;
537
538 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
539
540 queue_work(radeon_crtc->flip_queue, &work->flip_work);
541
542 return 0;
Alex Deucher6f34be52010-11-21 10:59:01 -0500543}
544
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000545static int
546radeon_crtc_set_config(struct drm_mode_set *set)
547{
548 struct drm_device *dev;
549 struct radeon_device *rdev;
550 struct drm_crtc *crtc;
551 bool active = false;
552 int ret;
553
554 if (!set || !set->crtc)
555 return -EINVAL;
556
557 dev = set->crtc->dev;
558
559 ret = pm_runtime_get_sync(dev->dev);
560 if (ret < 0)
561 return ret;
562
563 ret = drm_crtc_helper_set_config(set);
564
565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
566 if (crtc->enabled)
567 active = true;
568
569 pm_runtime_mark_last_busy(dev->dev);
570
571 rdev = dev->dev_private;
572 /* if we have active crtcs and we don't have a power ref,
573 take the current one */
574 if (active && !rdev->have_disp_power_ref) {
575 rdev->have_disp_power_ref = true;
576 return ret;
577 }
578 /* if we have no active crtcs, then drop the power ref
579 we got before */
580 if (!active && rdev->have_disp_power_ref) {
581 pm_runtime_put_autosuspend(dev->dev);
582 rdev->have_disp_power_ref = false;
583 }
584
585 /* drop the power reference we got coming in here */
586 pm_runtime_put_autosuspend(dev->dev);
587 return ret;
588}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200589static const struct drm_crtc_funcs radeon_crtc_funcs = {
590 .cursor_set = radeon_crtc_cursor_set,
591 .cursor_move = radeon_crtc_cursor_move,
592 .gamma_set = radeon_crtc_gamma_set,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000593 .set_config = radeon_crtc_set_config,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200594 .destroy = radeon_crtc_destroy,
Alex Deucher6f34be52010-11-21 10:59:01 -0500595 .page_flip = radeon_crtc_page_flip,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200596};
597
598static void radeon_crtc_init(struct drm_device *dev, int index)
599{
600 struct radeon_device *rdev = dev->dev_private;
601 struct radeon_crtc *radeon_crtc;
602 int i;
603
604 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
605 if (radeon_crtc == NULL)
606 return;
607
608 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
609
610 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
611 radeon_crtc->crtc_id = index;
Christian König1aab5512014-05-27 16:49:22 +0200612 radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
Jerome Glissec93bb852009-07-13 21:04:08 +0200613 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200614
Alex Deucher9e05fa12013-01-24 10:06:33 -0500615 if (rdev->family >= CHIP_BONAIRE) {
616 radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
617 radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
618 } else {
619 radeon_crtc->max_cursor_width = CURSOR_WIDTH;
620 radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
621 }
Alex Deucherbea61c52014-02-12 12:56:53 -0500622 dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
623 dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
Alex Deucher9e05fa12013-01-24 10:06:33 -0500624
Dave Airlie785b93e2009-08-28 15:46:53 +1000625#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200626 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
627 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
628 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000629#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200630
631 for (i = 0; i < 256; i++) {
632 radeon_crtc->lut_r[i] = i << 2;
633 radeon_crtc->lut_g[i] = i << 2;
634 radeon_crtc->lut_b[i] = i << 2;
635 }
636
637 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
638 radeon_atombios_init_crtc(dev, radeon_crtc);
639 else
640 radeon_legacy_init_crtc(dev, radeon_crtc);
641}
642
Alex Deuchere68adef2012-09-06 14:32:06 -0400643static const char *encoder_names[38] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200644 "NONE",
645 "INTERNAL_LVDS",
646 "INTERNAL_TMDS1",
647 "INTERNAL_TMDS2",
648 "INTERNAL_DAC1",
649 "INTERNAL_DAC2",
650 "INTERNAL_SDVOA",
651 "INTERNAL_SDVOB",
652 "SI170B",
653 "CH7303",
654 "CH7301",
655 "INTERNAL_DVO1",
656 "EXTERNAL_SDVOA",
657 "EXTERNAL_SDVOB",
658 "TITFP513",
659 "INTERNAL_LVTM1",
660 "VT1623",
661 "HDMI_SI1930",
662 "HDMI_INTERNAL",
663 "INTERNAL_KLDSCP_TMDS1",
664 "INTERNAL_KLDSCP_DVO1",
665 "INTERNAL_KLDSCP_DAC1",
666 "INTERNAL_KLDSCP_DAC2",
667 "SI178",
668 "MVPU_FPGA",
669 "INTERNAL_DDI",
670 "VT1625",
671 "HDMI_SI1932",
672 "DP_AN9801",
673 "DP_DP501",
674 "INTERNAL_UNIPHY",
675 "INTERNAL_KLDSCP_LVTMA",
676 "INTERNAL_UNIPHY1",
677 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500678 "NUTMEG",
679 "TRAVIS",
Alex Deuchere68adef2012-09-06 14:32:06 -0400680 "INTERNAL_VCE",
681 "INTERNAL_UNIPHY3",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200682};
683
Alex Deuchercbd46232010-06-07 02:24:54 -0400684static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500685 "HPD1",
686 "HPD2",
687 "HPD3",
688 "HPD4",
689 "HPD5",
690 "HPD6",
691};
692
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200693static void radeon_print_display_setup(struct drm_device *dev)
694{
695 struct drm_connector *connector;
696 struct radeon_connector *radeon_connector;
697 struct drm_encoder *encoder;
698 struct radeon_encoder *radeon_encoder;
699 uint32_t devices;
700 int i = 0;
701
702 DRM_INFO("Radeon Display Connectors\n");
703 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
704 radeon_connector = to_radeon_connector(connector);
705 DRM_INFO("Connector %d:\n", i);
Ilija Hadzicc1d2dbd2012-05-04 11:25:12 -0400706 DRM_INFO(" %s\n", drm_get_connector_name(connector));
Alex Deuchereed45b32009-12-04 14:45:27 -0500707 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
708 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000709 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200710 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
711 radeon_connector->ddc_bus->rec.mask_clk_reg,
712 radeon_connector->ddc_bus->rec.mask_data_reg,
713 radeon_connector->ddc_bus->rec.a_clk_reg,
714 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500715 radeon_connector->ddc_bus->rec.en_clk_reg,
716 radeon_connector->ddc_bus->rec.en_data_reg,
717 radeon_connector->ddc_bus->rec.y_clk_reg,
718 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000719 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400720 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000721 radeon_connector->router.ddc_mux_control_pin,
722 radeon_connector->router.ddc_mux_state);
723 if (radeon_connector->router.cd_valid)
724 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
725 radeon_connector->router.cd_mux_control_pin,
726 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000727 } else {
728 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
729 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
730 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
731 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
732 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
733 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
734 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
735 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200736 DRM_INFO(" Encoders:\n");
737 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
738 radeon_encoder = to_radeon_encoder(encoder);
739 devices = radeon_encoder->devices & radeon_connector->devices;
740 if (devices) {
741 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
742 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
743 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
744 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
745 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
746 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
747 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
748 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
749 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
750 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
751 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
752 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
753 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
754 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
755 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
756 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400757 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
758 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200759 if (devices & ATOM_DEVICE_TV1_SUPPORT)
760 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
761 if (devices & ATOM_DEVICE_CV_SUPPORT)
762 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
763 }
764 }
765 i++;
766 }
767}
768
Dave Airlie4ce001a2009-08-13 16:32:14 +1000769static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200770{
771 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200772 bool ret = false;
773
774 if (rdev->bios) {
775 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400776 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
777 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200778 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500779 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200780 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500781 if (ret == false)
782 ret = radeon_get_legacy_connector_info_from_table(dev);
783 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200784 } else {
785 if (!ASIC_IS_AVIVO(rdev))
786 ret = radeon_get_legacy_connector_info_from_table(dev);
787 }
788 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000789 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200790 radeon_print_display_setup(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200791 }
792
793 return ret;
794}
795
796int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
797{
Alex Deucher3c537882010-02-05 04:21:19 -0500798 struct drm_device *dev = radeon_connector->base.dev;
799 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200800 int ret = 0;
801
Alex Deucher26b5bc92010-08-05 21:21:18 -0400802 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000803 if (radeon_connector->router.ddc_valid)
804 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400805
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100806 if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
807 ENCODER_OBJECT_ID_NONE) {
Alex Deucher379dfc22014-04-07 10:33:46 -0400808 if (radeon_connector->ddc_bus->has_aux)
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100809 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
Alex Deucher379dfc22014-04-07 10:33:46 -0400810 &radeon_connector->ddc_bus->aux.ddc);
Niels Ole Salscheider0a9069d2013-01-03 19:09:28 +0100811 } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
812 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000813 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Alex Deucherb06947b2011-09-02 14:23:09 +0000814
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000815 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
Alex Deucher379dfc22014-04-07 10:33:46 -0400816 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
817 radeon_connector->ddc_bus->has_aux)
Alex Deucherb06947b2011-09-02 14:23:09 +0000818 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
Alex Deucher379dfc22014-04-07 10:33:46 -0400819 &radeon_connector->ddc_bus->aux.ddc);
Alex Deucherb06947b2011-09-02 14:23:09 +0000820 else if (radeon_connector->ddc_bus && !radeon_connector->edid)
821 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
822 &radeon_connector->ddc_bus->adapter);
823 } else {
824 if (radeon_connector->ddc_bus && !radeon_connector->edid)
825 radeon_connector->edid = drm_get_edid(&radeon_connector->base,
826 &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400827 }
Alex Deucherc324acd2010-12-08 22:13:06 -0500828
829 if (!radeon_connector->edid) {
830 if (rdev->is_atom_bios) {
831 /* some laptops provide a hardcoded edid in rom for LCDs */
832 if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
833 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
834 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
835 } else
836 /* some servers provide a hardcoded edid in rom for KVMs */
837 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
838 }
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400839 if (radeon_connector->edid) {
840 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
841 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Alex Deucher16086272014-03-31 11:19:46 -0400842 drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200843 return ret;
844 }
845 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000846 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200847}
848
Alex Deucherf523f742011-01-31 16:48:52 -0500849/* avivo */
Christian König32167012014-03-28 18:55:10 +0100850
851/**
852 * avivo_reduce_ratio - fractional number reduction
853 *
854 * @nom: nominator
855 * @den: denominator
856 * @nom_min: minimum value for nominator
857 * @den_min: minimum value for denominator
858 *
859 * Find the greatest common divisor and apply it on both nominator and
860 * denominator, but make nominator and denominator are at least as large
861 * as their minimum values.
862 */
863static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
864 unsigned nom_min, unsigned den_min)
Alex Deucherf523f742011-01-31 16:48:52 -0500865{
Christian König32167012014-03-28 18:55:10 +0100866 unsigned tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500867
Christian König32167012014-03-28 18:55:10 +0100868 /* reduce the numbers to a simpler ratio */
869 tmp = gcd(*nom, *den);
870 *nom /= tmp;
871 *den /= tmp;
Alex Deuchera4b40d5d2011-02-14 11:43:10 -0500872
Christian König32167012014-03-28 18:55:10 +0100873 /* make sure nominator is large enough */
874 if (*nom < nom_min) {
875 tmp = (nom_min + *nom - 1) / *nom;
876 *nom *= tmp;
877 *den *= tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500878 }
879
Christian König32167012014-03-28 18:55:10 +0100880 /* make sure the denominator is large enough */
881 if (*den < den_min) {
882 tmp = (den_min + *den - 1) / *den;
883 *nom *= tmp;
884 *den *= tmp;
Alex Deucherf523f742011-01-31 16:48:52 -0500885 }
Alex Deucherf523f742011-01-31 16:48:52 -0500886}
887
Christian König32167012014-03-28 18:55:10 +0100888/**
Christian Königc2fb3092014-04-20 13:24:32 +0200889 * avivo_get_fb_ref_div - feedback and ref divider calculation
890 *
891 * @nom: nominator
892 * @den: denominator
893 * @post_div: post divider
894 * @fb_div_max: feedback divider maximum
895 * @ref_div_max: reference divider maximum
896 * @fb_div: resulting feedback divider
897 * @ref_div: resulting reference divider
898 *
899 * Calculate feedback and reference divider for a given post divider. Makes
900 * sure we stay within the limits.
901 */
902static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
903 unsigned fb_div_max, unsigned ref_div_max,
904 unsigned *fb_div, unsigned *ref_div)
905{
906 /* limit reference * post divider to a maximum */
907 ref_div_max = min(210 / post_div, ref_div_max);
908
909 /* get matching reference and feedback divider */
910 *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
911 *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
912
913 /* limit fb divider to its maximum */
914 if (*fb_div > fb_div_max) {
915 *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
916 *fb_div = fb_div_max;
917 }
918}
919
920/**
Christian König32167012014-03-28 18:55:10 +0100921 * radeon_compute_pll_avivo - compute PLL paramaters
922 *
923 * @pll: information about the PLL
924 * @dot_clock_p: resulting pixel clock
925 * fb_div_p: resulting feedback divider
926 * frac_fb_div_p: fractional part of the feedback divider
927 * ref_div_p: resulting reference divider
928 * post_div_p: resulting reference divider
929 *
930 * Try to calculate the PLL parameters to generate the given frequency:
931 * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
932 */
Alex Deucherf523f742011-01-31 16:48:52 -0500933void radeon_compute_pll_avivo(struct radeon_pll *pll,
934 u32 freq,
935 u32 *dot_clock_p,
936 u32 *fb_div_p,
937 u32 *frac_fb_div_p,
938 u32 *ref_div_p,
939 u32 *post_div_p)
940{
Christian Königc2fb3092014-04-20 13:24:32 +0200941 unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
942 freq : freq / 10;
943
Christian König32167012014-03-28 18:55:10 +0100944 unsigned fb_div_min, fb_div_max, fb_div;
945 unsigned post_div_min, post_div_max, post_div;
946 unsigned ref_div_min, ref_div_max, ref_div;
947 unsigned post_div_best, diff_best;
Christian Königf8a2645e2014-04-16 11:54:21 +0200948 unsigned nom, den;
Alex Deucherf523f742011-01-31 16:48:52 -0500949
Christian König32167012014-03-28 18:55:10 +0100950 /* determine allowed feedback divider range */
951 fb_div_min = pll->min_feedback_div;
952 fb_div_max = pll->max_feedback_div;
Alex Deucherf523f742011-01-31 16:48:52 -0500953
954 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Christian König32167012014-03-28 18:55:10 +0100955 fb_div_min *= 10;
956 fb_div_max *= 10;
Alex Deucherf523f742011-01-31 16:48:52 -0500957 }
958
Christian König32167012014-03-28 18:55:10 +0100959 /* determine allowed ref divider range */
960 if (pll->flags & RADEON_PLL_USE_REF_DIV)
961 ref_div_min = pll->reference_div;
962 else
963 ref_div_min = pll->min_ref_div;
Christian König24315812014-04-19 18:57:14 +0200964
965 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
966 pll->flags & RADEON_PLL_USE_REF_DIV)
967 ref_div_max = pll->reference_div;
968 else
969 ref_div_max = pll->max_ref_div;
Christian König32167012014-03-28 18:55:10 +0100970
971 /* determine allowed post divider range */
972 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
973 post_div_min = pll->post_div;
974 post_div_max = pll->post_div;
975 } else {
Christian König32167012014-03-28 18:55:10 +0100976 unsigned vco_min, vco_max;
977
978 if (pll->flags & RADEON_PLL_IS_LCD) {
979 vco_min = pll->lcd_pll_out_min;
980 vco_max = pll->lcd_pll_out_max;
981 } else {
982 vco_min = pll->pll_out_min;
983 vco_max = pll->pll_out_max;
984 }
985
Christian Königc2fb3092014-04-20 13:24:32 +0200986 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
987 vco_min *= 10;
988 vco_max *= 10;
989 }
990
Christian König32167012014-03-28 18:55:10 +0100991 post_div_min = vco_min / target_clock;
992 if ((target_clock * post_div_min) < vco_min)
993 ++post_div_min;
994 if (post_div_min < pll->min_post_div)
995 post_div_min = pll->min_post_div;
996
997 post_div_max = vco_max / target_clock;
998 if ((target_clock * post_div_max) > vco_max)
999 --post_div_max;
1000 if (post_div_max > pll->max_post_div)
1001 post_div_max = pll->max_post_div;
1002 }
1003
1004 /* represent the searched ratio as fractional number */
Christian Königc2fb3092014-04-20 13:24:32 +02001005 nom = target_clock;
Christian König32167012014-03-28 18:55:10 +01001006 den = pll->reference_freq;
1007
1008 /* reduce the numbers to a simpler ratio */
1009 avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
1010
1011 /* now search for a post divider */
1012 if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
1013 post_div_best = post_div_min;
1014 else
1015 post_div_best = post_div_max;
1016 diff_best = ~0;
1017
1018 for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
Christian Königc2fb3092014-04-20 13:24:32 +02001019 unsigned diff;
1020 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
1021 ref_div_max, &fb_div, &ref_div);
1022 diff = abs(target_clock - (pll->reference_freq * fb_div) /
1023 (ref_div * post_div));
1024
Christian König32167012014-03-28 18:55:10 +01001025 if (diff < diff_best || (diff == diff_best &&
1026 !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
1027
1028 post_div_best = post_div;
1029 diff_best = diff;
1030 }
1031 }
1032 post_div = post_div_best;
1033
Christian Königc2fb3092014-04-20 13:24:32 +02001034 /* get the feedback and reference divider for the optimal value */
1035 avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
1036 &fb_div, &ref_div);
Christian König32167012014-03-28 18:55:10 +01001037
1038 /* reduce the numbers to a simpler ratio once more */
1039 /* this also makes sure that the reference divider is large enough */
1040 avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
1041
1042 /* and finally save the result */
1043 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
1044 *fb_div_p = fb_div / 10;
1045 *frac_fb_div_p = fb_div % 10;
1046 } else {
1047 *fb_div_p = fb_div;
1048 *frac_fb_div_p = 0;
1049 }
1050
1051 *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
1052 (pll->reference_freq * *frac_fb_div_p)) /
1053 (ref_div * post_div * 10);
Alex Deucherf523f742011-01-31 16:48:52 -05001054 *ref_div_p = ref_div;
1055 *post_div_p = post_div;
Christian König32167012014-03-28 18:55:10 +01001056
1057 DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
Christian Königc2fb3092014-04-20 13:24:32 +02001058 freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
Christian König32167012014-03-28 18:55:10 +01001059 ref_div, post_div);
Alex Deucherf523f742011-01-31 16:48:52 -05001060}
1061
1062/* pre-avivo */
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001063static inline uint32_t radeon_div(uint64_t n, uint32_t d)
1064{
1065 uint64_t mod;
1066
1067 n += d / 2;
1068
1069 mod = do_div(n, d);
1070 return n;
1071}
1072
Alex Deucherf523f742011-01-31 16:48:52 -05001073void radeon_compute_pll_legacy(struct radeon_pll *pll,
1074 uint64_t freq,
1075 uint32_t *dot_clock_p,
1076 uint32_t *fb_div_p,
1077 uint32_t *frac_fb_div_p,
1078 uint32_t *ref_div_p,
1079 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001080{
1081 uint32_t min_ref_div = pll->min_ref_div;
1082 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -05001083 uint32_t min_post_div = pll->min_post_div;
1084 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001085 uint32_t min_fractional_feed_div = 0;
1086 uint32_t max_fractional_feed_div = 0;
1087 uint32_t best_vco = pll->best_vco;
1088 uint32_t best_post_div = 1;
1089 uint32_t best_ref_div = 1;
1090 uint32_t best_feedback_div = 1;
1091 uint32_t best_frac_feedback_div = 0;
1092 uint32_t best_freq = -1;
1093 uint32_t best_error = 0xffffffff;
1094 uint32_t best_vco_diff = 1;
1095 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001096 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001097
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001098 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 +02001099 freq = freq * 1000;
1100
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001101 if (pll->flags & RADEON_PLL_IS_LCD) {
1102 pll_out_min = pll->lcd_pll_out_min;
1103 pll_out_max = pll->lcd_pll_out_max;
1104 } else {
1105 pll_out_min = pll->pll_out_min;
1106 pll_out_max = pll->pll_out_max;
1107 }
1108
Alex Deucher619efb12011-01-31 16:48:53 -05001109 if (pll_out_min > 64800)
1110 pll_out_min = 64800;
1111
Alex Deucherfc103322010-01-19 17:16:10 -05001112 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001113 min_ref_div = max_ref_div = pll->reference_div;
1114 else {
1115 while (min_ref_div < max_ref_div-1) {
1116 uint32_t mid = (min_ref_div + max_ref_div) / 2;
1117 uint32_t pll_in = pll->reference_freq / mid;
1118 if (pll_in < pll->pll_in_min)
1119 max_ref_div = mid;
1120 else if (pll_in > pll->pll_in_max)
1121 min_ref_div = mid;
1122 else
1123 break;
1124 }
1125 }
1126
Alex Deucherfc103322010-01-19 17:16:10 -05001127 if (pll->flags & RADEON_PLL_USE_POST_DIV)
1128 min_post_div = max_post_div = pll->post_div;
1129
1130 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001131 min_fractional_feed_div = pll->min_frac_feedback_div;
1132 max_fractional_feed_div = pll->max_frac_feedback_div;
1133 }
1134
Alex Deucherbd6a60a2011-02-21 01:11:59 -05001135 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001136 uint32_t ref_div;
1137
Alex Deucherfc103322010-01-19 17:16:10 -05001138 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001139 continue;
1140
1141 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -05001142 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001143 if ((post_div == 5) ||
1144 (post_div == 7) ||
1145 (post_div == 9) ||
1146 (post_div == 10) ||
1147 (post_div == 11) ||
1148 (post_div == 13) ||
1149 (post_div == 14) ||
1150 (post_div == 15))
1151 continue;
1152 }
1153
1154 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
1155 uint32_t feedback_div, current_freq = 0, error, vco_diff;
1156 uint32_t pll_in = pll->reference_freq / ref_div;
1157 uint32_t min_feed_div = pll->min_feedback_div;
1158 uint32_t max_feed_div = pll->max_feedback_div + 1;
1159
1160 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
1161 continue;
1162
1163 while (min_feed_div < max_feed_div) {
1164 uint32_t vco;
1165 uint32_t min_frac_feed_div = min_fractional_feed_div;
1166 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
1167 uint32_t frac_feedback_div;
1168 uint64_t tmp;
1169
1170 feedback_div = (min_feed_div + max_feed_div) / 2;
1171
1172 tmp = (uint64_t)pll->reference_freq * feedback_div;
1173 vco = radeon_div(tmp, ref_div);
1174
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001175 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001176 min_feed_div = feedback_div + 1;
1177 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -05001178 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001179 max_feed_div = feedback_div;
1180 continue;
1181 }
1182
1183 while (min_frac_feed_div < max_frac_feed_div) {
1184 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
1185 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
1186 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
1187 current_freq = radeon_div(tmp, ref_div * post_div);
1188
Alex Deucherfc103322010-01-19 17:16:10 -05001189 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +02001190 if (freq < current_freq)
1191 error = 0xffffffff;
1192 else
1193 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -04001194 } else
1195 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001196 vco_diff = abs(vco - best_vco);
1197
1198 if ((best_vco == 0 && error < best_error) ||
1199 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +02001200 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +10001201 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001202 best_post_div = post_div;
1203 best_ref_div = ref_div;
1204 best_feedback_div = feedback_div;
1205 best_frac_feedback_div = frac_feedback_div;
1206 best_freq = current_freq;
1207 best_error = error;
1208 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +10001209 } else if (current_freq == freq) {
1210 if (best_freq == -1) {
1211 best_post_div = post_div;
1212 best_ref_div = ref_div;
1213 best_feedback_div = feedback_div;
1214 best_frac_feedback_div = frac_feedback_div;
1215 best_freq = current_freq;
1216 best_error = error;
1217 best_vco_diff = vco_diff;
1218 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1219 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1220 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1221 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1222 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1223 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1224 best_post_div = post_div;
1225 best_ref_div = ref_div;
1226 best_feedback_div = feedback_div;
1227 best_frac_feedback_div = frac_feedback_div;
1228 best_freq = current_freq;
1229 best_error = error;
1230 best_vco_diff = vco_diff;
1231 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001232 }
1233 if (current_freq < freq)
1234 min_frac_feed_div = frac_feedback_div + 1;
1235 else
1236 max_frac_feed_div = frac_feedback_div;
1237 }
1238 if (current_freq < freq)
1239 min_feed_div = feedback_div + 1;
1240 else
1241 max_feed_div = feedback_div;
1242 }
1243 }
1244 }
1245
1246 *dot_clock_p = best_freq / 10000;
1247 *fb_div_p = best_feedback_div;
1248 *frac_fb_div_p = best_frac_feedback_div;
1249 *ref_div_p = best_ref_div;
1250 *post_div_p = best_post_div;
Joe Perchesbbb0aef52011-04-17 20:35:52 -07001251 DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1252 (long long)freq,
1253 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
Alex Deucher51d4bf82011-01-31 16:48:51 -05001254 best_ref_div, best_post_div);
1255
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001256}
1257
1258static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1259{
1260 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001261
Dave Airlie29d08b32010-09-27 16:17:17 +10001262 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001263 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +10001264 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001265 drm_framebuffer_cleanup(fb);
1266 kfree(radeon_fb);
1267}
1268
1269static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1270 struct drm_file *file_priv,
1271 unsigned int *handle)
1272{
1273 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1274
1275 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1276}
1277
1278static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1279 .destroy = radeon_user_framebuffer_destroy,
1280 .create_handle = radeon_user_framebuffer_create_handle,
1281};
1282
Dave Airlieaaefcd42012-03-06 10:44:40 +00001283int
Dave Airlie38651672010-03-30 05:34:13 +00001284radeon_framebuffer_init(struct drm_device *dev,
1285 struct radeon_framebuffer *rfb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001286 struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlie38651672010-03-30 05:34:13 +00001287 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001288{
Dave Airlieaaefcd42012-03-06 10:44:40 +00001289 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00001290 rfb->obj = obj;
Daniel Vetterc7d73f62012-12-13 23:38:38 +01001291 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001292 ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1293 if (ret) {
1294 rfb->obj = NULL;
1295 return ret;
1296 }
Dave Airlieaaefcd42012-03-06 10:44:40 +00001297 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001298}
1299
1300static struct drm_framebuffer *
1301radeon_user_framebuffer_create(struct drm_device *dev,
1302 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001303 struct drm_mode_fb_cmd2 *mode_cmd)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001304{
1305 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +00001306 struct radeon_framebuffer *radeon_fb;
Dave Airlieaaefcd42012-03-06 10:44:40 +00001307 int ret;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001308
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001309 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001310 if (obj == NULL) {
1311 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001312 "can't create framebuffer\n", mode_cmd->handles[0]);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001313 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +01001314 }
Dave Airlie38651672010-03-30 05:34:13 +00001315
1316 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001317 if (radeon_fb == NULL) {
1318 drm_gem_object_unreference_unlocked(obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001319 return ERR_PTR(-ENOMEM);
liu chuanshengf2d68cf2013-01-31 22:13:00 +08001320 }
Dave Airlie38651672010-03-30 05:34:13 +00001321
Dave Airlieaaefcd42012-03-06 10:44:40 +00001322 ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1323 if (ret) {
1324 kfree(radeon_fb);
1325 drm_gem_object_unreference_unlocked(obj);
xueminsub2f4b032013-01-22 22:16:53 +08001326 return ERR_PTR(ret);
Dave Airlieaaefcd42012-03-06 10:44:40 +00001327 }
Dave Airlie38651672010-03-30 05:34:13 +00001328
1329 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001330}
1331
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001332static void radeon_output_poll_changed(struct drm_device *dev)
1333{
1334 struct radeon_device *rdev = dev->dev_private;
1335 radeon_fb_output_poll_changed(rdev);
1336}
1337
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001338static const struct drm_mode_config_funcs radeon_mode_funcs = {
1339 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001340 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001341};
1342
Dave Airlie445282d2009-09-09 17:40:54 +10001343static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1344{ { 0, "driver" },
1345 { 1, "bios" },
1346};
1347
1348static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1349{ { TV_STD_NTSC, "ntsc" },
1350 { TV_STD_PAL, "pal" },
1351 { TV_STD_PAL_M, "pal-m" },
1352 { TV_STD_PAL_60, "pal-60" },
1353 { TV_STD_NTSC_J, "ntsc-j" },
1354 { TV_STD_SCART_PAL, "scart-pal" },
1355 { TV_STD_PAL_CN, "pal-cn" },
1356 { TV_STD_SECAM, "secam" },
1357};
1358
Alex Deucher5b1714d2010-08-03 19:59:20 -04001359static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1360{ { UNDERSCAN_OFF, "off" },
1361 { UNDERSCAN_ON, "on" },
1362 { UNDERSCAN_AUTO, "auto" },
1363};
1364
Alex Deucher8666c072013-09-03 14:58:44 -04001365static struct drm_prop_enum_list radeon_audio_enum_list[] =
1366{ { RADEON_AUDIO_DISABLE, "off" },
1367 { RADEON_AUDIO_ENABLE, "on" },
1368 { RADEON_AUDIO_AUTO, "auto" },
1369};
1370
Alex Deucher6214bb72013-09-24 17:26:26 -04001371/* XXX support different dither options? spatial, temporal, both, etc. */
1372static struct drm_prop_enum_list radeon_dither_enum_list[] =
1373{ { RADEON_FMT_DITHER_DISABLE, "off" },
1374 { RADEON_FMT_DITHER_ENABLE, "on" },
1375};
1376
Alex Deucherd79766f2009-12-17 19:00:29 -05001377static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +10001378{
Sascha Hauer4a67d392012-02-06 10:58:17 +01001379 int sz;
Dave Airlie445282d2009-09-09 17:40:54 +10001380
1381 if (rdev->is_atom_bios) {
1382 rdev->mode_info.coherent_mode_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001383 drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001384 if (!rdev->mode_info.coherent_mode_property)
1385 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001386 }
1387
1388 if (!ASIC_IS_AVIVO(rdev)) {
1389 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1390 rdev->mode_info.tmds_pll_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001391 drm_property_create_enum(rdev->ddev, 0,
1392 "tmds_pll",
1393 radeon_tmds_pll_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001394 }
1395
1396 rdev->mode_info.load_detect_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001397 drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
Dave Airlie445282d2009-09-09 17:40:54 +10001398 if (!rdev->mode_info.load_detect_property)
1399 return -ENOMEM;
Dave Airlie445282d2009-09-09 17:40:54 +10001400
1401 drm_mode_create_scaling_mode_property(rdev->ddev);
1402
1403 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1404 rdev->mode_info.tv_std_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001405 drm_property_create_enum(rdev->ddev, 0,
1406 "tv standard",
1407 radeon_tv_std_enum_list, sz);
Dave Airlie445282d2009-09-09 17:40:54 +10001408
Alex Deucher5b1714d2010-08-03 19:59:20 -04001409 sz = ARRAY_SIZE(radeon_underscan_enum_list);
1410 rdev->mode_info.underscan_property =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001411 drm_property_create_enum(rdev->ddev, 0,
1412 "underscan",
1413 radeon_underscan_enum_list, sz);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001414
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001415 rdev->mode_info.underscan_hborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001416 drm_property_create_range(rdev->ddev, 0,
1417 "underscan hborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001418 if (!rdev->mode_info.underscan_hborder_property)
1419 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001420
1421 rdev->mode_info.underscan_vborder_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001422 drm_property_create_range(rdev->ddev, 0,
1423 "underscan vborder", 0, 128);
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001424 if (!rdev->mode_info.underscan_vborder_property)
1425 return -ENOMEM;
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001426
Alex Deucher8666c072013-09-03 14:58:44 -04001427 sz = ARRAY_SIZE(radeon_audio_enum_list);
1428 rdev->mode_info.audio_property =
1429 drm_property_create_enum(rdev->ddev, 0,
1430 "audio",
1431 radeon_audio_enum_list, sz);
1432
Alex Deucher6214bb72013-09-24 17:26:26 -04001433 sz = ARRAY_SIZE(radeon_dither_enum_list);
1434 rdev->mode_info.dither_property =
1435 drm_property_create_enum(rdev->ddev, 0,
1436 "dither",
1437 radeon_dither_enum_list, sz);
1438
Dave Airlie445282d2009-09-09 17:40:54 +10001439 return 0;
1440}
1441
Alex Deucherf46c0122010-03-31 00:33:27 -04001442void radeon_update_display_priority(struct radeon_device *rdev)
1443{
1444 /* adjustment options for the display watermarks */
1445 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1446 /* set display priority to high for r3xx, rv515 chips
1447 * this avoids flickering due to underflow to the
1448 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001449 * Don't force high on rs4xx igp chips as it seems to
1450 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001451 */
Alex Deucher45737442010-05-20 11:26:11 -04001452 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1453 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001454 rdev->disp_priority = 2;
1455 else
1456 rdev->disp_priority = 0;
1457 } else
1458 rdev->disp_priority = radeon_disp_priority;
1459
1460}
1461
Alex Deucher07839862012-05-14 16:52:29 +02001462/*
1463 * Allocate hdmi structs and determine register offsets
1464 */
1465static void radeon_afmt_init(struct radeon_device *rdev)
1466{
1467 int i;
1468
1469 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1470 rdev->mode_info.afmt[i] = NULL;
1471
Alex Deucherb5306022013-07-31 16:51:33 -04001472 if (ASIC_IS_NODCE(rdev)) {
1473 /* nothing to do */
Alex Deucher07839862012-05-14 16:52:29 +02001474 } else if (ASIC_IS_DCE4(rdev)) {
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001475 static uint32_t eg_offsets[] = {
1476 EVERGREEN_CRTC0_REGISTER_OFFSET,
1477 EVERGREEN_CRTC1_REGISTER_OFFSET,
1478 EVERGREEN_CRTC2_REGISTER_OFFSET,
1479 EVERGREEN_CRTC3_REGISTER_OFFSET,
1480 EVERGREEN_CRTC4_REGISTER_OFFSET,
1481 EVERGREEN_CRTC5_REGISTER_OFFSET,
Alex Deucherb5306022013-07-31 16:51:33 -04001482 0x13830 - 0x7030,
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001483 };
1484 int num_afmt;
1485
Alex Deucherb5306022013-07-31 16:51:33 -04001486 /* DCE8 has 7 audio blocks tied to DIG encoders */
1487 /* DCE6 has 6 audio blocks tied to DIG encoders */
Alex Deucher07839862012-05-14 16:52:29 +02001488 /* DCE4/5 has 6 audio blocks tied to DIG encoders */
1489 /* DCE4.1 has 2 audio blocks tied to DIG encoders */
Alex Deucherb5306022013-07-31 16:51:33 -04001490 if (ASIC_IS_DCE8(rdev))
1491 num_afmt = 7;
1492 else if (ASIC_IS_DCE6(rdev))
1493 num_afmt = 6;
1494 else if (ASIC_IS_DCE5(rdev))
Rafał Miłeckia4d39e62013-08-01 17:29:16 +02001495 num_afmt = 6;
1496 else if (ASIC_IS_DCE41(rdev))
1497 num_afmt = 2;
1498 else /* DCE4 */
1499 num_afmt = 6;
1500
1501 BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
1502 for (i = 0; i < num_afmt; i++) {
1503 rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1504 if (rdev->mode_info.afmt[i]) {
1505 rdev->mode_info.afmt[i]->offset = eg_offsets[i];
1506 rdev->mode_info.afmt[i]->id = i;
Alex Deucher07839862012-05-14 16:52:29 +02001507 }
1508 }
1509 } else if (ASIC_IS_DCE3(rdev)) {
1510 /* DCE3.x has 2 audio blocks tied to DIG encoders */
1511 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1512 if (rdev->mode_info.afmt[0]) {
1513 rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1514 rdev->mode_info.afmt[0]->id = 0;
1515 }
1516 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1517 if (rdev->mode_info.afmt[1]) {
1518 rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1519 rdev->mode_info.afmt[1]->id = 1;
1520 }
1521 } else if (ASIC_IS_DCE2(rdev)) {
1522 /* DCE2 has at least 1 routable audio block */
1523 rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1524 if (rdev->mode_info.afmt[0]) {
1525 rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1526 rdev->mode_info.afmt[0]->id = 0;
1527 }
1528 /* r6xx has 2 routable audio blocks */
1529 if (rdev->family >= CHIP_R600) {
1530 rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1531 if (rdev->mode_info.afmt[1]) {
1532 rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1533 rdev->mode_info.afmt[1]->id = 1;
1534 }
1535 }
1536 }
1537}
1538
1539static void radeon_afmt_fini(struct radeon_device *rdev)
1540{
1541 int i;
1542
1543 for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1544 kfree(rdev->mode_info.afmt[i]);
1545 rdev->mode_info.afmt[i] = NULL;
1546 }
1547}
1548
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001549int radeon_modeset_init(struct radeon_device *rdev)
1550{
Alex Deucher18917b62010-02-01 16:02:25 -05001551 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001552 int ret;
1553
1554 drm_mode_config_init(rdev->ddev);
1555 rdev->mode_info.mode_config_initialized = true;
1556
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001557 rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001558
Alex Deucher881dd742011-01-06 21:19:14 -05001559 if (ASIC_IS_DCE5(rdev)) {
1560 rdev->ddev->mode_config.max_width = 16384;
1561 rdev->ddev->mode_config.max_height = 16384;
1562 } else if (ASIC_IS_AVIVO(rdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001563 rdev->ddev->mode_config.max_width = 8192;
1564 rdev->ddev->mode_config.max_height = 8192;
1565 } else {
1566 rdev->ddev->mode_config.max_width = 4096;
1567 rdev->ddev->mode_config.max_height = 4096;
1568 }
1569
Dave Airlie019d96c2011-09-29 16:20:42 +01001570 rdev->ddev->mode_config.preferred_depth = 24;
1571 rdev->ddev->mode_config.prefer_shadow = 1;
1572
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001573 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1574
Dave Airlie445282d2009-09-09 17:40:54 +10001575 ret = radeon_modeset_create_props(rdev);
1576 if (ret) {
1577 return ret;
1578 }
Dave Airliedfee5612009-10-02 09:19:09 +10001579
Alex Deucherf376b942010-08-05 21:21:16 -04001580 /* init i2c buses */
1581 radeon_i2c_init(rdev);
1582
Alex Deucher3c537882010-02-05 04:21:19 -05001583 /* check combios for a valid hardcoded EDID - Sun servers */
1584 if (!rdev->is_atom_bios) {
1585 /* check for hardcoded EDID in BIOS */
1586 radeon_combios_check_hardcoded_edid(rdev);
1587 }
1588
Dave Airliedfee5612009-10-02 09:19:09 +10001589 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001590 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001591 radeon_crtc_init(rdev->ddev, i);
1592 }
1593
1594 /* okay we should have all the bios connectors */
1595 ret = radeon_setup_enc_conn(rdev->ddev);
1596 if (!ret) {
1597 return ret;
1598 }
Alex Deucherac89af12011-05-22 13:20:36 -04001599
Alex Deucher3fa47d92012-01-20 14:56:39 -05001600 /* init dig PHYs, disp eng pll */
1601 if (rdev->is_atom_bios) {
Alex Deucherac89af12011-05-22 13:20:36 -04001602 radeon_atom_encoder_init(rdev);
Alex Deucherf3f1f032012-03-20 17:18:04 -04001603 radeon_atom_disp_eng_pll_init(rdev);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001604 }
Alex Deucherac89af12011-05-22 13:20:36 -04001605
Alex Deucherd4877cf2009-12-04 16:56:37 -05001606 /* initialize hpd */
1607 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001608
Alex Deucher07839862012-05-14 16:52:29 +02001609 /* setup afmt */
1610 radeon_afmt_init(rdev);
1611
Dave Airlie38651672010-03-30 05:34:13 +00001612 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001613 drm_kms_helper_poll_init(rdev->ddev);
1614
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001615 if (rdev->pm.dpm_enabled) {
1616 /* do dpm late init */
1617 ret = radeon_pm_late_init(rdev);
1618 if (ret) {
1619 rdev->pm.dpm_enabled = false;
1620 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1621 }
1622 /* set the dpm state for PX since there won't be
1623 * a modeset to call this.
1624 */
1625 radeon_pm_compute_clocks(rdev);
1626 }
1627
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001628 return 0;
1629}
1630
1631void radeon_modeset_fini(struct radeon_device *rdev)
1632{
Dave Airlie38651672010-03-30 05:34:13 +00001633 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001634 kfree(rdev->mode_info.bios_hardcoded_edid);
1635
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001636 if (rdev->mode_info.mode_config_initialized) {
Alex Deucher07839862012-05-14 16:52:29 +02001637 radeon_afmt_fini(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001638 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001639 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001640 drm_mode_config_cleanup(rdev->ddev);
1641 rdev->mode_info.mode_config_initialized = false;
1642 }
Alex Deucherf376b942010-08-05 21:21:16 -04001643 /* free i2c buses */
1644 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001645}
1646
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001647static bool is_hdtv_mode(const struct drm_display_mode *mode)
Alex Deucher039ed2d2010-08-20 11:57:19 -04001648{
1649 /* try and guess if this is a tv or a monitor */
1650 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1651 (mode->vdisplay == 576) || /* 576p */
1652 (mode->vdisplay == 720) || /* 720p */
1653 (mode->vdisplay == 1080)) /* 1080p */
1654 return true;
1655 else
1656 return false;
1657}
1658
Jerome Glissec93bb852009-07-13 21:04:08 +02001659bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001660 const struct drm_display_mode *mode,
Jerome Glissec93bb852009-07-13 21:04:08 +02001661 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001662{
Jerome Glissec93bb852009-07-13 21:04:08 +02001663 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001664 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001665 struct drm_encoder *encoder;
1666 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1667 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001668 struct drm_connector *connector;
1669 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001670 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001671 u32 src_v = 1, dst_v = 1;
1672 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001673
Alex Deucher5b1714d2010-08-03 19:59:20 -04001674 radeon_crtc->h_border = 0;
1675 radeon_crtc->v_border = 0;
1676
Jerome Glissec93bb852009-07-13 21:04:08 +02001677 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001678 if (encoder->crtc != crtc)
1679 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001680 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001681 connector = radeon_get_connector_for_encoder(encoder);
1682 radeon_connector = to_radeon_connector(connector);
1683
Jerome Glissec93bb852009-07-13 21:04:08 +02001684 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001685 /* set scaling */
1686 if (radeon_encoder->rmx_type == RMX_OFF)
1687 radeon_crtc->rmx_type = RMX_OFF;
1688 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1689 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1690 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1691 else
1692 radeon_crtc->rmx_type = RMX_OFF;
1693 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001694 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001695 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001696 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001697 src_v = crtc->mode.vdisplay;
1698 dst_v = radeon_crtc->native_mode.vdisplay;
1699 src_h = crtc->mode.hdisplay;
1700 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001701
1702 /* fix up for overscan on hdmi */
1703 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001704 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001705 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1706 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001707 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1708 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +02001709 if (radeon_encoder->underscan_hborder != 0)
1710 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1711 else
1712 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1713 if (radeon_encoder->underscan_vborder != 0)
1714 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1715 else
1716 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001717 radeon_crtc->rmx_type = RMX_FULL;
1718 src_v = crtc->mode.vdisplay;
1719 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1720 src_h = crtc->mode.hdisplay;
1721 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1722 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001723 first = false;
1724 } else {
1725 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1726 /* WARNING: Right now this can't happen but
1727 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001728 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001729 * (ie all encoder can work with the same
1730 * scaling).
1731 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001732 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001733 return false;
1734 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001735 }
1736 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001737 if (radeon_crtc->rmx_type != RMX_OFF) {
1738 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001739 a.full = dfixed_const(src_v);
1740 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001741 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001742 a.full = dfixed_const(src_h);
1743 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001744 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001745 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001746 radeon_crtc->vsc.full = dfixed_const(1);
1747 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001748 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001749 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001750}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001751
1752/*
Mario Kleinerd47abc52013-10-30 05:13:07 +01001753 * Retrieve current video scanout position of crtc on a given gpu, and
1754 * an optional accurate timestamp of when query happened.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001755 *
Mario Kleinerf5a80202010-10-23 04:42:17 +02001756 * \param dev Device to query.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001757 * \param crtc Crtc to query.
Ville Syrjäläabca9e452013-10-28 20:50:48 +02001758 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
Mario Kleiner6383cf72010-10-05 19:57:36 -04001759 * \param *vpos Location where vertical scanout position should be stored.
1760 * \param *hpos Location where horizontal scanout position should go.
Mario Kleinerd47abc52013-10-30 05:13:07 +01001761 * \param *stime Target location for timestamp taken immediately before
1762 * scanout position query. Can be NULL to skip timestamp.
1763 * \param *etime Target location for timestamp taken immediately after
1764 * scanout position query. Can be NULL to skip timestamp.
Mario Kleiner6383cf72010-10-05 19:57:36 -04001765 *
1766 * Returns vpos as a positive number while in active scanout area.
1767 * Returns vpos as a negative number inside vblank, counting the number
1768 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1769 * until start of active scanout / end of vblank."
1770 *
1771 * \return Flags, or'ed together as follows:
1772 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001773 * DRM_SCANOUTPOS_VALID = Query successful.
Mario Kleinerf5a80202010-10-23 04:42:17 +02001774 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1775 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
Mario Kleiner6383cf72010-10-05 19:57:36 -04001776 * this flag means that returned position may be offset by a constant but
1777 * unknown small number of scanlines wrt. real scanout position.
1778 *
1779 */
Ville Syrjäläabca9e452013-10-28 20:50:48 +02001780int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1781 int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
Mario Kleiner6383cf72010-10-05 19:57:36 -04001782{
1783 u32 stat_crtc = 0, vbl = 0, position = 0;
1784 int vbl_start, vbl_end, vtotal, ret = 0;
1785 bool in_vbl = true;
1786
Mario Kleinerf5a80202010-10-23 04:42:17 +02001787 struct radeon_device *rdev = dev->dev_private;
1788
Mario Kleinerd47abc52013-10-30 05:13:07 +01001789 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1790
1791 /* Get optional system timestamp before query. */
1792 if (stime)
1793 *stime = ktime_get();
1794
Mario Kleiner6383cf72010-10-05 19:57:36 -04001795 if (ASIC_IS_DCE4(rdev)) {
1796 if (crtc == 0) {
1797 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1798 EVERGREEN_CRTC0_REGISTER_OFFSET);
1799 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1800 EVERGREEN_CRTC0_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001801 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001802 }
1803 if (crtc == 1) {
1804 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1805 EVERGREEN_CRTC1_REGISTER_OFFSET);
1806 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1807 EVERGREEN_CRTC1_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001808 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001809 }
1810 if (crtc == 2) {
1811 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1812 EVERGREEN_CRTC2_REGISTER_OFFSET);
1813 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1814 EVERGREEN_CRTC2_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001815 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001816 }
1817 if (crtc == 3) {
1818 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1819 EVERGREEN_CRTC3_REGISTER_OFFSET);
1820 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1821 EVERGREEN_CRTC3_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001822 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001823 }
1824 if (crtc == 4) {
1825 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1826 EVERGREEN_CRTC4_REGISTER_OFFSET);
1827 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1828 EVERGREEN_CRTC4_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001829 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001830 }
1831 if (crtc == 5) {
1832 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1833 EVERGREEN_CRTC5_REGISTER_OFFSET);
1834 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1835 EVERGREEN_CRTC5_REGISTER_OFFSET);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001836 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001837 }
1838 } else if (ASIC_IS_AVIVO(rdev)) {
1839 if (crtc == 0) {
1840 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1841 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001842 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001843 }
1844 if (crtc == 1) {
1845 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1846 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001847 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001848 }
1849 } else {
1850 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1851 if (crtc == 0) {
1852 /* Assume vbl_end == 0, get vbl_start from
1853 * upper 16 bits.
1854 */
1855 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1856 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1857 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1858 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1859 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1860 if (!(stat_crtc & 1))
1861 in_vbl = false;
1862
Mario Kleinerf5a80202010-10-23 04:42:17 +02001863 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001864 }
1865 if (crtc == 1) {
1866 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1867 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1868 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1869 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1870 if (!(stat_crtc & 1))
1871 in_vbl = false;
1872
Mario Kleinerf5a80202010-10-23 04:42:17 +02001873 ret |= DRM_SCANOUTPOS_VALID;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001874 }
1875 }
1876
Mario Kleinerd47abc52013-10-30 05:13:07 +01001877 /* Get optional system timestamp after query. */
1878 if (etime)
1879 *etime = ktime_get();
1880
1881 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1882
Mario Kleiner6383cf72010-10-05 19:57:36 -04001883 /* Decode into vertical and horizontal scanout position. */
1884 *vpos = position & 0x1fff;
1885 *hpos = (position >> 16) & 0x1fff;
1886
1887 /* Valid vblank area boundaries from gpu retrieved? */
1888 if (vbl > 0) {
1889 /* Yes: Decode. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001890 ret |= DRM_SCANOUTPOS_ACCURATE;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001891 vbl_start = vbl & 0x1fff;
1892 vbl_end = (vbl >> 16) & 0x1fff;
1893 }
1894 else {
1895 /* No: Fake something reasonable which gives at least ok results. */
Mario Kleinerf5a80202010-10-23 04:42:17 +02001896 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001897 vbl_end = 0;
1898 }
1899
1900 /* Test scanout position against vblank region. */
1901 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1902 in_vbl = false;
1903
1904 /* Check if inside vblank area and apply corrective offsets:
1905 * vpos will then be >=0 in video scanout area, but negative
1906 * within vblank area, counting down the number of lines until
1907 * start of scanout.
1908 */
1909
1910 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1911 if (in_vbl && (*vpos >= vbl_start)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001912 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001913 *vpos = *vpos - vtotal;
1914 }
1915
1916 /* Correct for shifted end of vbl at vbl_end. */
1917 *vpos = *vpos - vbl_end;
1918
1919 /* In vblank? */
1920 if (in_vbl)
Mario Kleinerf5a80202010-10-23 04:42:17 +02001921 ret |= DRM_SCANOUTPOS_INVBL;
Mario Kleiner6383cf72010-10-05 19:57:36 -04001922
Ville Syrjälä8072bfa2013-10-28 21:22:52 +02001923 /* Is vpos outside nominal vblank area, but less than
1924 * 1/100 of a frame height away from start of vblank?
1925 * If so, assume this isn't a massively delayed vblank
1926 * interrupt, but a vblank interrupt that fired a few
1927 * microseconds before true start of vblank. Compensate
1928 * by adding a full frame duration to the final timestamp.
1929 * Happens, e.g., on ATI R500, R600.
1930 *
1931 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1932 */
1933 if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1934 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1935 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1936
1937 if (vbl_start - *vpos < vtotal / 100) {
1938 *vpos -= vtotal;
1939
1940 /* Signal this correction as "applied". */
1941 ret |= 0x8;
1942 }
1943 }
1944
Mario Kleiner6383cf72010-10-05 19:57:36 -04001945 return ret;
1946}