xref: /dragonfly/sys/dev/drm/radeon/radeon_display.c (revision ba55f2f5)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2007-8 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot  * Copyright 2008 Red Hat Inc.
4926deccbSFrançois Tigeot  *
5926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
7926deccbSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
8926deccbSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9926deccbSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
10926deccbSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
11926deccbSFrançois Tigeot  *
12926deccbSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
13926deccbSFrançois Tigeot  * all copies or substantial portions of the Software.
14926deccbSFrançois Tigeot  *
15926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19926deccbSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20926deccbSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21926deccbSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
22926deccbSFrançois Tigeot  *
23926deccbSFrançois Tigeot  * Authors: Dave Airlie
24926deccbSFrançois Tigeot  *          Alex Deucher
25926deccbSFrançois Tigeot  *
26926deccbSFrançois Tigeot  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_display.c 254885 2013-08-25 19:37:15Z dumbbell $
27926deccbSFrançois Tigeot  */
28926deccbSFrançois Tigeot 
29926deccbSFrançois Tigeot #include <drm/drmP.h>
30926deccbSFrançois Tigeot #include <uapi_drm/radeon_drm.h>
31926deccbSFrançois Tigeot #include "radeon.h"
32926deccbSFrançois Tigeot 
33926deccbSFrançois Tigeot #include "atom.h"
34926deccbSFrançois Tigeot 
35926deccbSFrançois Tigeot #include <drm/drm_crtc_helper.h>
36926deccbSFrançois Tigeot #include <drm/drm_edid.h>
3760fc7eecSFrançois Tigeot #include <linux/err.h>
38926deccbSFrançois Tigeot 
39926deccbSFrançois Tigeot static void avivo_crtc_load_lut(struct drm_crtc *crtc)
40926deccbSFrançois Tigeot {
41926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
42926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
43926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
44926deccbSFrançois Tigeot 	int i;
45926deccbSFrançois Tigeot 
46926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
47926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
48926deccbSFrançois Tigeot 
49926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
50926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
51926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
52926deccbSFrançois Tigeot 
53926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
54926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
55926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
56926deccbSFrançois Tigeot 
57926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
58926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUT_RW_MODE, 0);
59926deccbSFrançois Tigeot 	WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
60926deccbSFrançois Tigeot 
61926deccbSFrançois Tigeot 	WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
62926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
63926deccbSFrançois Tigeot 		WREG32(AVIVO_DC_LUT_30_COLOR,
64926deccbSFrançois Tigeot 			     (radeon_crtc->lut_r[i] << 20) |
65926deccbSFrançois Tigeot 			     (radeon_crtc->lut_g[i] << 10) |
66926deccbSFrançois Tigeot 			     (radeon_crtc->lut_b[i] << 0));
67926deccbSFrançois Tigeot 	}
68926deccbSFrançois Tigeot 
69926deccbSFrançois Tigeot 	WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
70926deccbSFrançois Tigeot }
71926deccbSFrançois Tigeot 
72926deccbSFrançois Tigeot static void dce4_crtc_load_lut(struct drm_crtc *crtc)
73926deccbSFrançois Tigeot {
74926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
75926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
76926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
77926deccbSFrançois Tigeot 	int i;
78926deccbSFrançois Tigeot 
79926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
80926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
81926deccbSFrançois Tigeot 
82926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
83926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
84926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
85926deccbSFrançois Tigeot 
86926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
87926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
88926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
89926deccbSFrançois Tigeot 
90926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
91926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
92926deccbSFrançois Tigeot 
93926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
94926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
95926deccbSFrançois Tigeot 		WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
96926deccbSFrançois Tigeot 		       (radeon_crtc->lut_r[i] << 20) |
97926deccbSFrançois Tigeot 		       (radeon_crtc->lut_g[i] << 10) |
98926deccbSFrançois Tigeot 		       (radeon_crtc->lut_b[i] << 0));
99926deccbSFrançois Tigeot 	}
100926deccbSFrançois Tigeot }
101926deccbSFrançois Tigeot 
102926deccbSFrançois Tigeot static void dce5_crtc_load_lut(struct drm_crtc *crtc)
103926deccbSFrançois Tigeot {
104926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
105926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
106926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
107926deccbSFrançois Tigeot 	int i;
108926deccbSFrançois Tigeot 
109926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
110926deccbSFrançois Tigeot 
111926deccbSFrançois Tigeot 	WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
112926deccbSFrançois Tigeot 	       (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
113926deccbSFrançois Tigeot 		NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
114926deccbSFrançois Tigeot 	WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
115926deccbSFrançois Tigeot 	       NI_GRPH_PRESCALE_BYPASS);
116926deccbSFrançois Tigeot 	WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
117926deccbSFrançois Tigeot 	       NI_OVL_PRESCALE_BYPASS);
118926deccbSFrançois Tigeot 	WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
119926deccbSFrançois Tigeot 	       (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
120926deccbSFrançois Tigeot 		NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
121926deccbSFrançois Tigeot 
122926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
123926deccbSFrançois Tigeot 
124926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
125926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
126926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
127926deccbSFrançois Tigeot 
128926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
129926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
130926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
131926deccbSFrançois Tigeot 
132926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
133926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
134926deccbSFrançois Tigeot 
135926deccbSFrançois Tigeot 	WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
136926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
137926deccbSFrançois Tigeot 		WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
138926deccbSFrançois Tigeot 		       (radeon_crtc->lut_r[i] << 20) |
139926deccbSFrançois Tigeot 		       (radeon_crtc->lut_g[i] << 10) |
140926deccbSFrançois Tigeot 		       (radeon_crtc->lut_b[i] << 0));
141926deccbSFrançois Tigeot 	}
142926deccbSFrançois Tigeot 
143926deccbSFrançois Tigeot 	WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
144926deccbSFrançois Tigeot 	       (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
145926deccbSFrançois Tigeot 		NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
146926deccbSFrançois Tigeot 		NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
147926deccbSFrançois Tigeot 		NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
148926deccbSFrançois Tigeot 	WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
149926deccbSFrançois Tigeot 	       (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
150926deccbSFrançois Tigeot 		NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
151926deccbSFrançois Tigeot 	WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
152926deccbSFrançois Tigeot 	       (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
153926deccbSFrançois Tigeot 		NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
154926deccbSFrançois Tigeot 	WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
155926deccbSFrançois Tigeot 	       (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
156926deccbSFrançois Tigeot 		NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
157926deccbSFrançois Tigeot 	/* XXX match this to the depth of the crtc fmt block, move to modeset? */
158926deccbSFrançois Tigeot 	WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
15957e252bfSMichael Neumann 	if (ASIC_IS_DCE8(rdev)) {
16057e252bfSMichael Neumann 		/* XXX this only needs to be programmed once per crtc at startup,
16157e252bfSMichael Neumann 		 * not sure where the best place for it is
16257e252bfSMichael Neumann 		 */
16357e252bfSMichael Neumann 		WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
16457e252bfSMichael Neumann 		       CIK_CURSOR_ALPHA_BLND_ENA);
16557e252bfSMichael Neumann 	}
166926deccbSFrançois Tigeot }
167926deccbSFrançois Tigeot 
168926deccbSFrançois Tigeot static void legacy_crtc_load_lut(struct drm_crtc *crtc)
169926deccbSFrançois Tigeot {
170926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
171926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
172926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
173926deccbSFrançois Tigeot 	int i;
174926deccbSFrançois Tigeot 	uint32_t dac2_cntl;
175926deccbSFrançois Tigeot 
176926deccbSFrançois Tigeot 	dac2_cntl = RREG32(RADEON_DAC_CNTL2);
177926deccbSFrançois Tigeot 	if (radeon_crtc->crtc_id == 0)
178926deccbSFrançois Tigeot 		dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
179926deccbSFrançois Tigeot 	else
180926deccbSFrançois Tigeot 		dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
181926deccbSFrançois Tigeot 	WREG32(RADEON_DAC_CNTL2, dac2_cntl);
182926deccbSFrançois Tigeot 
183926deccbSFrançois Tigeot 	WREG8(RADEON_PALETTE_INDEX, 0);
184926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
185926deccbSFrançois Tigeot 		WREG32(RADEON_PALETTE_30_DATA,
186926deccbSFrançois Tigeot 			     (radeon_crtc->lut_r[i] << 20) |
187926deccbSFrançois Tigeot 			     (radeon_crtc->lut_g[i] << 10) |
188926deccbSFrançois Tigeot 			     (radeon_crtc->lut_b[i] << 0));
189926deccbSFrançois Tigeot 	}
190926deccbSFrançois Tigeot }
191926deccbSFrançois Tigeot 
192926deccbSFrançois Tigeot void radeon_crtc_load_lut(struct drm_crtc *crtc)
193926deccbSFrançois Tigeot {
194926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
195926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
196926deccbSFrançois Tigeot 
197926deccbSFrançois Tigeot 	if (!crtc->enabled)
198926deccbSFrançois Tigeot 		return;
199926deccbSFrançois Tigeot 
200926deccbSFrançois Tigeot 	if (ASIC_IS_DCE5(rdev))
201926deccbSFrançois Tigeot 		dce5_crtc_load_lut(crtc);
202926deccbSFrançois Tigeot 	else if (ASIC_IS_DCE4(rdev))
203926deccbSFrançois Tigeot 		dce4_crtc_load_lut(crtc);
204926deccbSFrançois Tigeot 	else if (ASIC_IS_AVIVO(rdev))
205926deccbSFrançois Tigeot 		avivo_crtc_load_lut(crtc);
206926deccbSFrançois Tigeot 	else
207926deccbSFrançois Tigeot 		legacy_crtc_load_lut(crtc);
208926deccbSFrançois Tigeot }
209926deccbSFrançois Tigeot 
210926deccbSFrançois Tigeot /** Sets the color ramps on behalf of fbcon */
211926deccbSFrançois Tigeot void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
212926deccbSFrançois Tigeot 			      u16 blue, int regno)
213926deccbSFrançois Tigeot {
214926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
215926deccbSFrançois Tigeot 
216926deccbSFrançois Tigeot 	radeon_crtc->lut_r[regno] = red >> 6;
217926deccbSFrançois Tigeot 	radeon_crtc->lut_g[regno] = green >> 6;
218926deccbSFrançois Tigeot 	radeon_crtc->lut_b[regno] = blue >> 6;
219926deccbSFrançois Tigeot }
220926deccbSFrançois Tigeot 
221926deccbSFrançois Tigeot /** Gets the color ramps on behalf of fbcon */
222926deccbSFrançois Tigeot void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
223926deccbSFrançois Tigeot 			      u16 *blue, int regno)
224926deccbSFrançois Tigeot {
225926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
226926deccbSFrançois Tigeot 
227926deccbSFrançois Tigeot 	*red = radeon_crtc->lut_r[regno] << 6;
228926deccbSFrançois Tigeot 	*green = radeon_crtc->lut_g[regno] << 6;
229926deccbSFrançois Tigeot 	*blue = radeon_crtc->lut_b[regno] << 6;
230926deccbSFrançois Tigeot }
231926deccbSFrançois Tigeot 
232926deccbSFrançois Tigeot static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
233926deccbSFrançois Tigeot 				  u16 *blue, uint32_t start, uint32_t size)
234926deccbSFrançois Tigeot {
235926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
236926deccbSFrançois Tigeot 	int end = (start + size > 256) ? 256 : start + size, i;
237926deccbSFrançois Tigeot 
238926deccbSFrançois Tigeot 	/* userspace palettes are always correct as is */
239926deccbSFrançois Tigeot 	for (i = start; i < end; i++) {
240926deccbSFrançois Tigeot 		radeon_crtc->lut_r[i] = red[i] >> 6;
241926deccbSFrançois Tigeot 		radeon_crtc->lut_g[i] = green[i] >> 6;
242926deccbSFrançois Tigeot 		radeon_crtc->lut_b[i] = blue[i] >> 6;
243926deccbSFrançois Tigeot 	}
244926deccbSFrançois Tigeot 	radeon_crtc_load_lut(crtc);
245926deccbSFrançois Tigeot }
246926deccbSFrançois Tigeot 
247926deccbSFrançois Tigeot static void radeon_crtc_destroy(struct drm_crtc *crtc)
248926deccbSFrançois Tigeot {
249926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
250926deccbSFrançois Tigeot 
251926deccbSFrançois Tigeot 	drm_crtc_cleanup(crtc);
252c4ef309bSzrj 	kfree(radeon_crtc);
253926deccbSFrançois Tigeot }
254926deccbSFrançois Tigeot 
255926deccbSFrançois Tigeot /*
256926deccbSFrançois Tigeot  * Handle unpin events outside the interrupt handler proper.
257926deccbSFrançois Tigeot  */
258926deccbSFrançois Tigeot static void radeon_unpin_work_func(void *arg, int pending)
259926deccbSFrançois Tigeot {
260926deccbSFrançois Tigeot 	struct radeon_unpin_work *work = arg;
261926deccbSFrançois Tigeot 	int r;
262926deccbSFrançois Tigeot 
263926deccbSFrançois Tigeot 	/* unpin of the old buffer */
264926deccbSFrançois Tigeot 	r = radeon_bo_reserve(work->old_rbo, false);
265926deccbSFrançois Tigeot 	if (likely(r == 0)) {
266926deccbSFrançois Tigeot 		r = radeon_bo_unpin(work->old_rbo);
267926deccbSFrançois Tigeot 		if (unlikely(r != 0)) {
268926deccbSFrançois Tigeot 			DRM_ERROR("failed to unpin buffer after flip\n");
269926deccbSFrançois Tigeot 		}
270926deccbSFrançois Tigeot 		radeon_bo_unreserve(work->old_rbo);
271926deccbSFrançois Tigeot 	} else
272926deccbSFrançois Tigeot 		DRM_ERROR("failed to reserve buffer after flip\n");
273926deccbSFrançois Tigeot 
274926deccbSFrançois Tigeot 	drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
275c4ef309bSzrj 	kfree(work);
276926deccbSFrançois Tigeot }
277926deccbSFrançois Tigeot 
278926deccbSFrançois Tigeot void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
279926deccbSFrançois Tigeot {
280926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
281926deccbSFrançois Tigeot 	struct radeon_unpin_work *work;
282926deccbSFrançois Tigeot 	u32 update_pending;
283926deccbSFrançois Tigeot 	int vpos, hpos;
284926deccbSFrançois Tigeot 
285926deccbSFrançois Tigeot 	lockmgr(&rdev->ddev->event_lock, LK_EXCLUSIVE);
286926deccbSFrançois Tigeot 	work = radeon_crtc->unpin_work;
287926deccbSFrançois Tigeot 	if (work == NULL ||
288926deccbSFrançois Tigeot 	    (work->fence && !radeon_fence_signaled(work->fence))) {
289926deccbSFrançois Tigeot 		lockmgr(&rdev->ddev->event_lock, LK_RELEASE);
290926deccbSFrançois Tigeot 		return;
291926deccbSFrançois Tigeot 	}
292926deccbSFrançois Tigeot 	/* New pageflip, or just completion of a previous one? */
293926deccbSFrançois Tigeot 	if (!radeon_crtc->deferred_flip_completion) {
294926deccbSFrançois Tigeot 		/* do the flip (mmio) */
295926deccbSFrançois Tigeot 		update_pending = radeon_page_flip(rdev, crtc_id, work->new_crtc_base);
296926deccbSFrançois Tigeot 	} else {
297926deccbSFrançois Tigeot 		/* This is just a completion of a flip queued in crtc
298926deccbSFrançois Tigeot 		 * at last invocation. Make sure we go directly to
299926deccbSFrançois Tigeot 		 * completion routine.
300926deccbSFrançois Tigeot 		 */
301926deccbSFrançois Tigeot 		update_pending = 0;
302926deccbSFrançois Tigeot 		radeon_crtc->deferred_flip_completion = 0;
303926deccbSFrançois Tigeot 	}
304926deccbSFrançois Tigeot 
305926deccbSFrançois Tigeot 	/* Has the pageflip already completed in crtc, or is it certain
306926deccbSFrançois Tigeot 	 * to complete in this vblank?
307926deccbSFrançois Tigeot 	 */
308926deccbSFrançois Tigeot 	if (update_pending &&
309782e40d3SFrançois Tigeot 	    (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
310782e40d3SFrançois Tigeot 							       &vpos, &hpos, NULL, NULL)) &&
311926deccbSFrançois Tigeot 	    ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
312926deccbSFrançois Tigeot 	     (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
313926deccbSFrançois Tigeot 		/* crtc didn't flip in this target vblank interval,
314926deccbSFrançois Tigeot 		 * but flip is pending in crtc. Based on the current
315926deccbSFrançois Tigeot 		 * scanout position we know that the current frame is
316926deccbSFrançois Tigeot 		 * (nearly) complete and the flip will (likely)
317926deccbSFrançois Tigeot 		 * complete before the start of the next frame.
318926deccbSFrançois Tigeot 		 */
319926deccbSFrançois Tigeot 		update_pending = 0;
320926deccbSFrançois Tigeot 	}
321926deccbSFrançois Tigeot 	if (update_pending) {
322926deccbSFrançois Tigeot 		/* crtc didn't flip in this target vblank interval,
323926deccbSFrançois Tigeot 		 * but flip is pending in crtc. It will complete it
324926deccbSFrançois Tigeot 		 * in next vblank interval, so complete the flip at
325926deccbSFrançois Tigeot 		 * next vblank irq.
326926deccbSFrançois Tigeot 		 */
327926deccbSFrançois Tigeot 		radeon_crtc->deferred_flip_completion = 1;
328926deccbSFrançois Tigeot 		lockmgr(&rdev->ddev->event_lock, LK_RELEASE);
329926deccbSFrançois Tigeot 		return;
330926deccbSFrançois Tigeot 	}
331926deccbSFrançois Tigeot 
332926deccbSFrançois Tigeot 	/* Pageflip (will be) certainly completed in this vblank. Clean up. */
333926deccbSFrançois Tigeot 	radeon_crtc->unpin_work = NULL;
334926deccbSFrançois Tigeot 
335926deccbSFrançois Tigeot 	/* wakeup userspace */
336f43cf1b1SMichael Neumann 	if (work->event)
337f43cf1b1SMichael Neumann 		drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
338f43cf1b1SMichael Neumann 
339926deccbSFrançois Tigeot 	lockmgr(&rdev->ddev->event_lock, LK_RELEASE);
340926deccbSFrançois Tigeot 
341926deccbSFrançois Tigeot 	drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
342926deccbSFrançois Tigeot 	radeon_fence_unref(&work->fence);
343926deccbSFrançois Tigeot 	radeon_post_page_flip(work->rdev, work->crtc_id);
344926deccbSFrançois Tigeot 	taskqueue_enqueue(rdev->tq, &work->work);
345926deccbSFrançois Tigeot }
346926deccbSFrançois Tigeot 
347926deccbSFrançois Tigeot static int radeon_crtc_page_flip(struct drm_crtc *crtc,
348926deccbSFrançois Tigeot 				 struct drm_framebuffer *fb,
3499edbd4a0SFrançois Tigeot 				 struct drm_pending_vblank_event *event,
3509edbd4a0SFrançois Tigeot 				 uint32_t page_flip_flags)
351926deccbSFrançois Tigeot {
352926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
353926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
354926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
355926deccbSFrançois Tigeot 	struct radeon_framebuffer *old_radeon_fb;
356926deccbSFrançois Tigeot 	struct radeon_framebuffer *new_radeon_fb;
357926deccbSFrançois Tigeot 	struct drm_gem_object *obj;
358926deccbSFrançois Tigeot 	struct radeon_bo *rbo;
359926deccbSFrançois Tigeot 	struct radeon_unpin_work *work;
360926deccbSFrançois Tigeot 	u32 tiling_flags, pitch_pixels;
361926deccbSFrançois Tigeot 	u64 base;
362926deccbSFrançois Tigeot 	int r;
363926deccbSFrançois Tigeot 
364c4ef309bSzrj 	work = kzalloc(sizeof *work, GFP_KERNEL);
365926deccbSFrançois Tigeot 	if (work == NULL)
366926deccbSFrançois Tigeot 		return -ENOMEM;
367926deccbSFrançois Tigeot 
368926deccbSFrançois Tigeot 	work->event = event;
369926deccbSFrançois Tigeot 	work->rdev = rdev;
370926deccbSFrançois Tigeot 	work->crtc_id = radeon_crtc->crtc_id;
371*ba55f2f5SFrançois Tigeot 	old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
372926deccbSFrançois Tigeot 	new_radeon_fb = to_radeon_framebuffer(fb);
373926deccbSFrançois Tigeot 	/* schedule unpin of the old buffer */
374926deccbSFrançois Tigeot 	obj = old_radeon_fb->obj;
375926deccbSFrançois Tigeot 	/* take a reference to the old object */
376926deccbSFrançois Tigeot 	drm_gem_object_reference(obj);
377926deccbSFrançois Tigeot 	rbo = gem_to_radeon_bo(obj);
378926deccbSFrançois Tigeot 	work->old_rbo = rbo;
379926deccbSFrançois Tigeot 	obj = new_radeon_fb->obj;
380926deccbSFrançois Tigeot 	rbo = gem_to_radeon_bo(obj);
381926deccbSFrançois Tigeot 
382926deccbSFrançois Tigeot 	lockmgr(&rbo->tbo.bdev->fence_lock, LK_EXCLUSIVE);
383926deccbSFrançois Tigeot 	if (rbo->tbo.sync_obj)
384926deccbSFrançois Tigeot 		work->fence = radeon_fence_ref(rbo->tbo.sync_obj);
385926deccbSFrançois Tigeot 	lockmgr(&rbo->tbo.bdev->fence_lock, LK_RELEASE);
386926deccbSFrançois Tigeot 
387926deccbSFrançois Tigeot 	TASK_INIT(&work->work, 0, radeon_unpin_work_func, work);
388926deccbSFrançois Tigeot 
389926deccbSFrançois Tigeot 	/* We borrow the event spin lock for protecting unpin_work */
390926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
391926deccbSFrançois Tigeot 	if (radeon_crtc->unpin_work) {
392926deccbSFrançois Tigeot 		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
393926deccbSFrançois Tigeot 		r = -EBUSY;
394926deccbSFrançois Tigeot 		goto unlock_free;
395926deccbSFrançois Tigeot 	}
396926deccbSFrançois Tigeot 	radeon_crtc->unpin_work = work;
397926deccbSFrançois Tigeot 	radeon_crtc->deferred_flip_completion = 0;
398926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_RELEASE);
399926deccbSFrançois Tigeot 
400926deccbSFrançois Tigeot 	/* pin the new buffer */
401926deccbSFrançois Tigeot 	DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
402926deccbSFrançois Tigeot 			 work->old_rbo, rbo);
403926deccbSFrançois Tigeot 
404926deccbSFrançois Tigeot 	r = radeon_bo_reserve(rbo, false);
405926deccbSFrançois Tigeot 	if (unlikely(r != 0)) {
406926deccbSFrançois Tigeot 		DRM_ERROR("failed to reserve new rbo buffer before flip\n");
407926deccbSFrançois Tigeot 		goto pflip_cleanup;
408926deccbSFrançois Tigeot 	}
409926deccbSFrançois Tigeot 	/* Only 27 bit offset for legacy CRTC */
410926deccbSFrançois Tigeot 	r = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
411926deccbSFrançois Tigeot 				     ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
412926deccbSFrançois Tigeot 	if (unlikely(r != 0)) {
413926deccbSFrançois Tigeot 		radeon_bo_unreserve(rbo);
414926deccbSFrançois Tigeot 		r = -EINVAL;
415926deccbSFrançois Tigeot 		DRM_ERROR("failed to pin new rbo buffer before flip\n");
416926deccbSFrançois Tigeot 		goto pflip_cleanup;
417926deccbSFrançois Tigeot 	}
418926deccbSFrançois Tigeot 	radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
419926deccbSFrançois Tigeot 	radeon_bo_unreserve(rbo);
420926deccbSFrançois Tigeot 
421926deccbSFrançois Tigeot 	if (!ASIC_IS_AVIVO(rdev)) {
422926deccbSFrançois Tigeot 		/* crtc offset is from display base addr not FB location */
423926deccbSFrançois Tigeot 		base -= radeon_crtc->legacy_display_base_addr;
424926deccbSFrançois Tigeot 		pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
425926deccbSFrançois Tigeot 
426926deccbSFrançois Tigeot 		if (tiling_flags & RADEON_TILING_MACRO) {
427926deccbSFrançois Tigeot 			if (ASIC_IS_R300(rdev)) {
428926deccbSFrançois Tigeot 				base &= ~0x7ff;
429926deccbSFrançois Tigeot 			} else {
430926deccbSFrançois Tigeot 				int byteshift = fb->bits_per_pixel >> 4;
431926deccbSFrançois Tigeot 				int tile_addr = (((crtc->y >> 3) * pitch_pixels +  crtc->x) >> (8 - byteshift)) << 11;
432926deccbSFrançois Tigeot 				base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
433926deccbSFrançois Tigeot 			}
434926deccbSFrançois Tigeot 		} else {
435926deccbSFrançois Tigeot 			int offset = crtc->y * pitch_pixels + crtc->x;
436926deccbSFrançois Tigeot 			switch (fb->bits_per_pixel) {
437926deccbSFrançois Tigeot 			case 8:
438926deccbSFrançois Tigeot 			default:
439926deccbSFrançois Tigeot 				offset *= 1;
440926deccbSFrançois Tigeot 				break;
441926deccbSFrançois Tigeot 			case 15:
442926deccbSFrançois Tigeot 			case 16:
443926deccbSFrançois Tigeot 				offset *= 2;
444926deccbSFrançois Tigeot 				break;
445926deccbSFrançois Tigeot 			case 24:
446926deccbSFrançois Tigeot 				offset *= 3;
447926deccbSFrançois Tigeot 				break;
448926deccbSFrançois Tigeot 			case 32:
449926deccbSFrançois Tigeot 				offset *= 4;
450926deccbSFrançois Tigeot 				break;
451926deccbSFrançois Tigeot 			}
452926deccbSFrançois Tigeot 			base += offset;
453926deccbSFrançois Tigeot 		}
454926deccbSFrançois Tigeot 		base &= ~7;
455926deccbSFrançois Tigeot 	}
456926deccbSFrançois Tigeot 
457926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
458926deccbSFrançois Tigeot 	work->new_crtc_base = base;
459926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_RELEASE);
460926deccbSFrançois Tigeot 
461926deccbSFrançois Tigeot 	/* update crtc fb */
462*ba55f2f5SFrançois Tigeot 	crtc->primary->fb = fb;
463926deccbSFrançois Tigeot 
464926deccbSFrançois Tigeot 	r = drm_vblank_get(dev, radeon_crtc->crtc_id);
465926deccbSFrançois Tigeot 	if (r) {
466926deccbSFrançois Tigeot 		DRM_ERROR("failed to get vblank before flip\n");
467926deccbSFrançois Tigeot 		goto pflip_cleanup1;
468926deccbSFrançois Tigeot 	}
469926deccbSFrançois Tigeot 
470926deccbSFrançois Tigeot 	/* set the proper interrupt */
471926deccbSFrançois Tigeot 	radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
472926deccbSFrançois Tigeot 
473926deccbSFrançois Tigeot 	return 0;
474926deccbSFrançois Tigeot 
475926deccbSFrançois Tigeot pflip_cleanup1:
476926deccbSFrançois Tigeot 	if (unlikely(radeon_bo_reserve(rbo, false) != 0)) {
477926deccbSFrançois Tigeot 		DRM_ERROR("failed to reserve new rbo in error path\n");
478926deccbSFrançois Tigeot 		goto pflip_cleanup;
479926deccbSFrançois Tigeot 	}
480926deccbSFrançois Tigeot 	if (unlikely(radeon_bo_unpin(rbo) != 0)) {
481926deccbSFrançois Tigeot 		DRM_ERROR("failed to unpin new rbo in error path\n");
482926deccbSFrançois Tigeot 	}
483926deccbSFrançois Tigeot 	radeon_bo_unreserve(rbo);
484926deccbSFrançois Tigeot 
485926deccbSFrançois Tigeot pflip_cleanup:
486926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
487926deccbSFrançois Tigeot 	radeon_crtc->unpin_work = NULL;
488926deccbSFrançois Tigeot unlock_free:
489926deccbSFrançois Tigeot 	lockmgr(&dev->event_lock, LK_RELEASE);
490926deccbSFrançois Tigeot 	drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
491926deccbSFrançois Tigeot 	radeon_fence_unref(&work->fence);
492c4ef309bSzrj 	kfree(work);
493926deccbSFrançois Tigeot 
494926deccbSFrançois Tigeot 	return r;
495926deccbSFrançois Tigeot }
496926deccbSFrançois Tigeot 
497926deccbSFrançois Tigeot static const struct drm_crtc_funcs radeon_crtc_funcs = {
498926deccbSFrançois Tigeot 	.cursor_set = radeon_crtc_cursor_set,
499926deccbSFrançois Tigeot 	.cursor_move = radeon_crtc_cursor_move,
500926deccbSFrançois Tigeot 	.gamma_set = radeon_crtc_gamma_set,
501926deccbSFrançois Tigeot 	.set_config = drm_crtc_helper_set_config,
502926deccbSFrançois Tigeot 	.destroy = radeon_crtc_destroy,
503926deccbSFrançois Tigeot 	.page_flip = radeon_crtc_page_flip,
504926deccbSFrançois Tigeot };
505926deccbSFrançois Tigeot 
506926deccbSFrançois Tigeot static void radeon_crtc_init(struct drm_device *dev, int index)
507926deccbSFrançois Tigeot {
508926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
509926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc;
510926deccbSFrançois Tigeot 	int i;
511926deccbSFrançois Tigeot 
512c4ef309bSzrj 	radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
513926deccbSFrançois Tigeot 	if (radeon_crtc == NULL)
514926deccbSFrançois Tigeot 		return;
515926deccbSFrançois Tigeot 
516926deccbSFrançois Tigeot 	drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
517926deccbSFrançois Tigeot 
518926deccbSFrançois Tigeot 	drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
519926deccbSFrançois Tigeot 	radeon_crtc->crtc_id = index;
520926deccbSFrançois Tigeot 	rdev->mode_info.crtcs[index] = radeon_crtc;
521926deccbSFrançois Tigeot 
52257e252bfSMichael Neumann 	if (rdev->family >= CHIP_BONAIRE) {
52357e252bfSMichael Neumann 		radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
52457e252bfSMichael Neumann 		radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
52557e252bfSMichael Neumann 	} else {
52657e252bfSMichael Neumann 		radeon_crtc->max_cursor_width = CURSOR_WIDTH;
52757e252bfSMichael Neumann 		radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
52857e252bfSMichael Neumann 	}
52957e252bfSMichael Neumann 
530926deccbSFrançois Tigeot #if 0
531926deccbSFrançois Tigeot 	radeon_crtc->mode_set.crtc = &radeon_crtc->base;
532926deccbSFrançois Tigeot 	radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
533926deccbSFrançois Tigeot 	radeon_crtc->mode_set.num_connectors = 0;
534926deccbSFrançois Tigeot #endif
535926deccbSFrançois Tigeot 
536926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
537926deccbSFrançois Tigeot 		radeon_crtc->lut_r[i] = i << 2;
538926deccbSFrançois Tigeot 		radeon_crtc->lut_g[i] = i << 2;
539926deccbSFrançois Tigeot 		radeon_crtc->lut_b[i] = i << 2;
540926deccbSFrançois Tigeot 	}
541926deccbSFrançois Tigeot 
542926deccbSFrançois Tigeot 	if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
543926deccbSFrançois Tigeot 		radeon_atombios_init_crtc(dev, radeon_crtc);
544926deccbSFrançois Tigeot 	else
545926deccbSFrançois Tigeot 		radeon_legacy_init_crtc(dev, radeon_crtc);
546926deccbSFrançois Tigeot }
547926deccbSFrançois Tigeot 
54857e252bfSMichael Neumann static const char *encoder_names[38] = {
549926deccbSFrançois Tigeot 	"NONE",
550926deccbSFrançois Tigeot 	"INTERNAL_LVDS",
551926deccbSFrançois Tigeot 	"INTERNAL_TMDS1",
552926deccbSFrançois Tigeot 	"INTERNAL_TMDS2",
553926deccbSFrançois Tigeot 	"INTERNAL_DAC1",
554926deccbSFrançois Tigeot 	"INTERNAL_DAC2",
555926deccbSFrançois Tigeot 	"INTERNAL_SDVOA",
556926deccbSFrançois Tigeot 	"INTERNAL_SDVOB",
557926deccbSFrançois Tigeot 	"SI170B",
558926deccbSFrançois Tigeot 	"CH7303",
559926deccbSFrançois Tigeot 	"CH7301",
560926deccbSFrançois Tigeot 	"INTERNAL_DVO1",
561926deccbSFrançois Tigeot 	"EXTERNAL_SDVOA",
562926deccbSFrançois Tigeot 	"EXTERNAL_SDVOB",
563926deccbSFrançois Tigeot 	"TITFP513",
564926deccbSFrançois Tigeot 	"INTERNAL_LVTM1",
565926deccbSFrançois Tigeot 	"VT1623",
566926deccbSFrançois Tigeot 	"HDMI_SI1930",
567926deccbSFrançois Tigeot 	"HDMI_INTERNAL",
568926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_TMDS1",
569926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DVO1",
570926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DAC1",
571926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DAC2",
572926deccbSFrançois Tigeot 	"SI178",
573926deccbSFrançois Tigeot 	"MVPU_FPGA",
574926deccbSFrançois Tigeot 	"INTERNAL_DDI",
575926deccbSFrançois Tigeot 	"VT1625",
576926deccbSFrançois Tigeot 	"HDMI_SI1932",
577926deccbSFrançois Tigeot 	"DP_AN9801",
578926deccbSFrançois Tigeot 	"DP_DP501",
579926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY",
580926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_LVTMA",
581926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY1",
582926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY2",
583926deccbSFrançois Tigeot 	"NUTMEG",
584926deccbSFrançois Tigeot 	"TRAVIS",
58557e252bfSMichael Neumann 	"INTERNAL_VCE",
58657e252bfSMichael Neumann 	"INTERNAL_UNIPHY3",
587926deccbSFrançois Tigeot };
588926deccbSFrançois Tigeot 
589926deccbSFrançois Tigeot static const char *hpd_names[6] = {
590926deccbSFrançois Tigeot 	"HPD1",
591926deccbSFrançois Tigeot 	"HPD2",
592926deccbSFrançois Tigeot 	"HPD3",
593926deccbSFrançois Tigeot 	"HPD4",
594926deccbSFrançois Tigeot 	"HPD5",
595926deccbSFrançois Tigeot 	"HPD6",
596926deccbSFrançois Tigeot };
597926deccbSFrançois Tigeot 
598926deccbSFrançois Tigeot static void radeon_print_display_setup(struct drm_device *dev)
599926deccbSFrançois Tigeot {
600926deccbSFrançois Tigeot 	struct drm_connector *connector;
601926deccbSFrançois Tigeot 	struct radeon_connector *radeon_connector;
602926deccbSFrançois Tigeot 	struct drm_encoder *encoder;
603926deccbSFrançois Tigeot 	struct radeon_encoder *radeon_encoder;
604926deccbSFrançois Tigeot 	uint32_t devices;
605926deccbSFrançois Tigeot 	int i = 0;
606926deccbSFrançois Tigeot 
607926deccbSFrançois Tigeot 	DRM_INFO("Radeon Display Connectors\n");
608926deccbSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
609926deccbSFrançois Tigeot 		radeon_connector = to_radeon_connector(connector);
610926deccbSFrançois Tigeot 		DRM_INFO("Connector %d:\n", i);
611*ba55f2f5SFrançois Tigeot 		DRM_INFO("  %s\n", connector->name);
612926deccbSFrançois Tigeot 		if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
613926deccbSFrançois Tigeot 			DRM_INFO("  %s\n", hpd_names[radeon_connector->hpd.hpd]);
614926deccbSFrançois Tigeot 		if (radeon_connector->ddc_bus) {
615926deccbSFrançois Tigeot 			DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
616926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.mask_clk_reg,
617926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.mask_data_reg,
618926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.a_clk_reg,
619926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.a_data_reg,
620926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.en_clk_reg,
621926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.en_data_reg,
622926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.y_clk_reg,
623926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.y_data_reg);
624926deccbSFrançois Tigeot 			if (radeon_connector->router.ddc_valid)
625926deccbSFrançois Tigeot 				DRM_INFO("  DDC Router 0x%x/0x%x\n",
626926deccbSFrançois Tigeot 					 radeon_connector->router.ddc_mux_control_pin,
627926deccbSFrançois Tigeot 					 radeon_connector->router.ddc_mux_state);
628926deccbSFrançois Tigeot 			if (radeon_connector->router.cd_valid)
629926deccbSFrançois Tigeot 				DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
630926deccbSFrançois Tigeot 					 radeon_connector->router.cd_mux_control_pin,
631926deccbSFrançois Tigeot 					 radeon_connector->router.cd_mux_state);
632926deccbSFrançois Tigeot 		} else {
633926deccbSFrançois Tigeot 			if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
634926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
635926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
636926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
637926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
638926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
639926deccbSFrançois Tigeot 				DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
640926deccbSFrançois Tigeot 		}
641926deccbSFrançois Tigeot 		DRM_INFO("  Encoders:\n");
642926deccbSFrançois Tigeot 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
643926deccbSFrançois Tigeot 			radeon_encoder = to_radeon_encoder(encoder);
644926deccbSFrançois Tigeot 			devices = radeon_encoder->devices & radeon_connector->devices;
645926deccbSFrançois Tigeot 			if (devices) {
646926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CRT1_SUPPORT)
647926deccbSFrançois Tigeot 					DRM_INFO("    CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
648926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CRT2_SUPPORT)
649926deccbSFrançois Tigeot 					DRM_INFO("    CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
650926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_LCD1_SUPPORT)
651926deccbSFrançois Tigeot 					DRM_INFO("    LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
652926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP1_SUPPORT)
653926deccbSFrançois Tigeot 					DRM_INFO("    DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
654926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP2_SUPPORT)
655926deccbSFrançois Tigeot 					DRM_INFO("    DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
656926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP3_SUPPORT)
657926deccbSFrançois Tigeot 					DRM_INFO("    DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
658926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP4_SUPPORT)
659926deccbSFrançois Tigeot 					DRM_INFO("    DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
660926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP5_SUPPORT)
661926deccbSFrançois Tigeot 					DRM_INFO("    DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
662926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP6_SUPPORT)
663926deccbSFrançois Tigeot 					DRM_INFO("    DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
664926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_TV1_SUPPORT)
665926deccbSFrançois Tigeot 					DRM_INFO("    TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
666926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CV_SUPPORT)
667926deccbSFrançois Tigeot 					DRM_INFO("    CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
668926deccbSFrançois Tigeot 			}
669926deccbSFrançois Tigeot 		}
670926deccbSFrançois Tigeot 		i++;
671926deccbSFrançois Tigeot 	}
672926deccbSFrançois Tigeot }
673926deccbSFrançois Tigeot 
674926deccbSFrançois Tigeot static bool radeon_setup_enc_conn(struct drm_device *dev)
675926deccbSFrançois Tigeot {
676926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
677926deccbSFrançois Tigeot 	bool ret = false;
678926deccbSFrançois Tigeot 
679926deccbSFrançois Tigeot 	if (rdev->bios) {
680926deccbSFrançois Tigeot 		if (rdev->is_atom_bios) {
681926deccbSFrançois Tigeot 			ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
682926deccbSFrançois Tigeot 			if (ret == false)
683926deccbSFrançois Tigeot 				ret = radeon_get_atom_connector_info_from_object_table(dev);
684926deccbSFrançois Tigeot 		} else {
685926deccbSFrançois Tigeot 			ret = radeon_get_legacy_connector_info_from_bios(dev);
686926deccbSFrançois Tigeot 			if (ret == false)
687926deccbSFrançois Tigeot 				ret = radeon_get_legacy_connector_info_from_table(dev);
688926deccbSFrançois Tigeot 		}
689926deccbSFrançois Tigeot 	} else {
690926deccbSFrançois Tigeot 		if (!ASIC_IS_AVIVO(rdev))
691926deccbSFrançois Tigeot 			ret = radeon_get_legacy_connector_info_from_table(dev);
692926deccbSFrançois Tigeot 	}
693926deccbSFrançois Tigeot 	if (ret) {
694926deccbSFrançois Tigeot 		radeon_setup_encoder_clones(dev);
695926deccbSFrançois Tigeot 		radeon_print_display_setup(dev);
696926deccbSFrançois Tigeot 	}
697926deccbSFrançois Tigeot 
698926deccbSFrançois Tigeot 	return ret;
699926deccbSFrançois Tigeot }
700926deccbSFrançois Tigeot 
701926deccbSFrançois Tigeot int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
702926deccbSFrançois Tigeot {
703926deccbSFrançois Tigeot 	struct drm_device *dev = radeon_connector->base.dev;
704926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
705926deccbSFrançois Tigeot 	int ret = 0;
706926deccbSFrançois Tigeot 
707926deccbSFrançois Tigeot 	/* on hw with routers, select right port */
708926deccbSFrançois Tigeot 	if (radeon_connector->router.ddc_valid)
709926deccbSFrançois Tigeot 		radeon_router_select_ddc_port(radeon_connector);
710926deccbSFrançois Tigeot 
711926deccbSFrançois Tigeot 	if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
712926deccbSFrançois Tigeot 	    ENCODER_OBJECT_ID_NONE) {
713926deccbSFrançois Tigeot 		struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
714926deccbSFrançois Tigeot 
715926deccbSFrançois Tigeot 		if (dig->dp_i2c_bus)
716926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
717926deccbSFrançois Tigeot 							      dig->dp_i2c_bus->adapter);
718926deccbSFrançois Tigeot 	} else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
719926deccbSFrançois Tigeot 		   (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
720926deccbSFrançois Tigeot 		struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
721926deccbSFrançois Tigeot 
722926deccbSFrançois Tigeot 		if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
723926deccbSFrançois Tigeot 		     dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
724926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
725926deccbSFrançois Tigeot 							      dig->dp_i2c_bus->adapter);
726926deccbSFrançois Tigeot 		else if (radeon_connector->ddc_bus && !radeon_connector->edid)
727926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
728926deccbSFrançois Tigeot 							      radeon_connector->ddc_bus->adapter);
729926deccbSFrançois Tigeot 	} else {
730926deccbSFrançois Tigeot 		if (radeon_connector->ddc_bus && !radeon_connector->edid)
731926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
732926deccbSFrançois Tigeot 							      radeon_connector->ddc_bus->adapter);
733926deccbSFrançois Tigeot 	}
734926deccbSFrançois Tigeot 
735926deccbSFrançois Tigeot 	if (!radeon_connector->edid) {
736926deccbSFrançois Tigeot 		if (rdev->is_atom_bios) {
737926deccbSFrançois Tigeot 			/* some laptops provide a hardcoded edid in rom for LCDs */
738926deccbSFrançois Tigeot 			if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
739926deccbSFrançois Tigeot 			     (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
740926deccbSFrançois Tigeot 				radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
741926deccbSFrançois Tigeot 		} else
742926deccbSFrançois Tigeot 			/* some servers provide a hardcoded edid in rom for KVMs */
743926deccbSFrançois Tigeot 			radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
744926deccbSFrançois Tigeot 	}
745926deccbSFrançois Tigeot 	if (radeon_connector->edid) {
746926deccbSFrançois Tigeot 		drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
747926deccbSFrançois Tigeot 		ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
748926deccbSFrançois Tigeot 		return ret;
749926deccbSFrançois Tigeot 	}
750926deccbSFrançois Tigeot 	drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
751926deccbSFrançois Tigeot 	return 0;
752926deccbSFrançois Tigeot }
753926deccbSFrançois Tigeot 
754926deccbSFrançois Tigeot /* avivo */
755926deccbSFrançois Tigeot static void avivo_get_fb_div(struct radeon_pll *pll,
756926deccbSFrançois Tigeot 			     u32 target_clock,
757926deccbSFrançois Tigeot 			     u32 post_div,
758926deccbSFrançois Tigeot 			     u32 ref_div,
759926deccbSFrançois Tigeot 			     u32 *fb_div,
760926deccbSFrançois Tigeot 			     u32 *frac_fb_div)
761926deccbSFrançois Tigeot {
762926deccbSFrançois Tigeot 	u32 tmp = post_div * ref_div;
763926deccbSFrançois Tigeot 
764926deccbSFrançois Tigeot 	tmp *= target_clock;
765926deccbSFrançois Tigeot 	*fb_div = tmp / pll->reference_freq;
766926deccbSFrançois Tigeot 	*frac_fb_div = tmp % pll->reference_freq;
767926deccbSFrançois Tigeot 
768926deccbSFrançois Tigeot         if (*fb_div > pll->max_feedback_div)
769926deccbSFrançois Tigeot 		*fb_div = pll->max_feedback_div;
770926deccbSFrançois Tigeot         else if (*fb_div < pll->min_feedback_div)
771926deccbSFrançois Tigeot                 *fb_div = pll->min_feedback_div;
772926deccbSFrançois Tigeot }
773926deccbSFrançois Tigeot 
774926deccbSFrançois Tigeot static u32 avivo_get_post_div(struct radeon_pll *pll,
775926deccbSFrançois Tigeot 			      u32 target_clock)
776926deccbSFrançois Tigeot {
777926deccbSFrançois Tigeot 	u32 vco, post_div, tmp;
778926deccbSFrançois Tigeot 
779926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_POST_DIV)
780926deccbSFrançois Tigeot 		return pll->post_div;
781926deccbSFrançois Tigeot 
782926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
783926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_IS_LCD)
784926deccbSFrançois Tigeot 			vco = pll->lcd_pll_out_min;
785926deccbSFrançois Tigeot 		else
786926deccbSFrançois Tigeot 			vco = pll->pll_out_min;
787926deccbSFrançois Tigeot 	} else {
788926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_IS_LCD)
789926deccbSFrançois Tigeot 			vco = pll->lcd_pll_out_max;
790926deccbSFrançois Tigeot 		else
791926deccbSFrançois Tigeot 			vco = pll->pll_out_max;
792926deccbSFrançois Tigeot 	}
793926deccbSFrançois Tigeot 
794926deccbSFrançois Tigeot 	post_div = vco / target_clock;
795926deccbSFrançois Tigeot 	tmp = vco % target_clock;
796926deccbSFrançois Tigeot 
797926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
798926deccbSFrançois Tigeot 		if (tmp)
799926deccbSFrançois Tigeot 			post_div++;
800926deccbSFrançois Tigeot 	} else {
801926deccbSFrançois Tigeot 		if (!tmp)
802926deccbSFrançois Tigeot 			post_div--;
803926deccbSFrançois Tigeot 	}
804926deccbSFrançois Tigeot 
805926deccbSFrançois Tigeot 	if (post_div > pll->max_post_div)
806926deccbSFrançois Tigeot 		post_div = pll->max_post_div;
807926deccbSFrançois Tigeot 	else if (post_div < pll->min_post_div)
808926deccbSFrançois Tigeot 		post_div = pll->min_post_div;
809926deccbSFrançois Tigeot 
810926deccbSFrançois Tigeot 	return post_div;
811926deccbSFrançois Tigeot }
812926deccbSFrançois Tigeot 
813926deccbSFrançois Tigeot #define MAX_TOLERANCE 10
814926deccbSFrançois Tigeot 
815926deccbSFrançois Tigeot void radeon_compute_pll_avivo(struct radeon_pll *pll,
816926deccbSFrançois Tigeot 			      u32 freq,
817926deccbSFrançois Tigeot 			      u32 *dot_clock_p,
818926deccbSFrançois Tigeot 			      u32 *fb_div_p,
819926deccbSFrançois Tigeot 			      u32 *frac_fb_div_p,
820926deccbSFrançois Tigeot 			      u32 *ref_div_p,
821926deccbSFrançois Tigeot 			      u32 *post_div_p)
822926deccbSFrançois Tigeot {
823926deccbSFrançois Tigeot 	u32 target_clock = freq / 10;
824926deccbSFrançois Tigeot 	u32 post_div = avivo_get_post_div(pll, target_clock);
825926deccbSFrançois Tigeot 	u32 ref_div = pll->min_ref_div;
826926deccbSFrançois Tigeot 	u32 fb_div = 0, frac_fb_div = 0, tmp;
827926deccbSFrançois Tigeot 
828926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
829926deccbSFrançois Tigeot 		ref_div = pll->reference_div;
830926deccbSFrançois Tigeot 
831926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
832926deccbSFrançois Tigeot 		avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
833926deccbSFrançois Tigeot 		frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
834926deccbSFrançois Tigeot 		if (frac_fb_div >= 5) {
835926deccbSFrançois Tigeot 			frac_fb_div -= 5;
836926deccbSFrançois Tigeot 			frac_fb_div = frac_fb_div / 10;
837926deccbSFrançois Tigeot 			frac_fb_div++;
838926deccbSFrançois Tigeot 		}
839926deccbSFrançois Tigeot 		if (frac_fb_div >= 10) {
840926deccbSFrançois Tigeot 			fb_div++;
841926deccbSFrançois Tigeot 			frac_fb_div = 0;
842926deccbSFrançois Tigeot 		}
843926deccbSFrançois Tigeot 	} else {
844926deccbSFrançois Tigeot 		while (ref_div <= pll->max_ref_div) {
845926deccbSFrançois Tigeot 			avivo_get_fb_div(pll, target_clock, post_div, ref_div,
846926deccbSFrançois Tigeot 					 &fb_div, &frac_fb_div);
847926deccbSFrançois Tigeot 			if (frac_fb_div >= (pll->reference_freq / 2))
848926deccbSFrançois Tigeot 				fb_div++;
849926deccbSFrançois Tigeot 			frac_fb_div = 0;
850926deccbSFrançois Tigeot 			tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
851926deccbSFrançois Tigeot 			tmp = (tmp * 10000) / target_clock;
852926deccbSFrançois Tigeot 
853926deccbSFrançois Tigeot 			if (tmp > (10000 + MAX_TOLERANCE))
854926deccbSFrançois Tigeot 				ref_div++;
855926deccbSFrançois Tigeot 			else if (tmp >= (10000 - MAX_TOLERANCE))
856926deccbSFrançois Tigeot 				break;
857926deccbSFrançois Tigeot 			else
858926deccbSFrançois Tigeot 				ref_div++;
859926deccbSFrançois Tigeot 		}
860926deccbSFrançois Tigeot 	}
861926deccbSFrançois Tigeot 
862926deccbSFrançois Tigeot 	*dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
863926deccbSFrançois Tigeot 		(ref_div * post_div * 10);
864926deccbSFrançois Tigeot 	*fb_div_p = fb_div;
865926deccbSFrançois Tigeot 	*frac_fb_div_p = frac_fb_div;
866926deccbSFrançois Tigeot 	*ref_div_p = ref_div;
867926deccbSFrançois Tigeot 	*post_div_p = post_div;
868926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
869926deccbSFrançois Tigeot 		      *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
870926deccbSFrançois Tigeot }
871926deccbSFrançois Tigeot 
872926deccbSFrançois Tigeot /* pre-avivo */
873926deccbSFrançois Tigeot static inline uint32_t radeon_div(uint64_t n, uint32_t d)
874926deccbSFrançois Tigeot {
875926deccbSFrançois Tigeot 	uint64_t mod;
876926deccbSFrançois Tigeot 
877926deccbSFrançois Tigeot 	n += d / 2;
878926deccbSFrançois Tigeot 
879926deccbSFrançois Tigeot 	mod = do_div(n, d);
880926deccbSFrançois Tigeot 	return n;
881926deccbSFrançois Tigeot }
882926deccbSFrançois Tigeot 
883926deccbSFrançois Tigeot void radeon_compute_pll_legacy(struct radeon_pll *pll,
884926deccbSFrançois Tigeot 			       uint64_t freq,
885926deccbSFrançois Tigeot 			       uint32_t *dot_clock_p,
886926deccbSFrançois Tigeot 			       uint32_t *fb_div_p,
887926deccbSFrançois Tigeot 			       uint32_t *frac_fb_div_p,
888926deccbSFrançois Tigeot 			       uint32_t *ref_div_p,
889926deccbSFrançois Tigeot 			       uint32_t *post_div_p)
890926deccbSFrançois Tigeot {
891926deccbSFrançois Tigeot 	uint32_t min_ref_div = pll->min_ref_div;
892926deccbSFrançois Tigeot 	uint32_t max_ref_div = pll->max_ref_div;
893926deccbSFrançois Tigeot 	uint32_t min_post_div = pll->min_post_div;
894926deccbSFrançois Tigeot 	uint32_t max_post_div = pll->max_post_div;
895926deccbSFrançois Tigeot 	uint32_t min_fractional_feed_div = 0;
896926deccbSFrançois Tigeot 	uint32_t max_fractional_feed_div = 0;
897926deccbSFrançois Tigeot 	uint32_t best_vco = pll->best_vco;
898926deccbSFrançois Tigeot 	uint32_t best_post_div = 1;
899926deccbSFrançois Tigeot 	uint32_t best_ref_div = 1;
900926deccbSFrançois Tigeot 	uint32_t best_feedback_div = 1;
901926deccbSFrançois Tigeot 	uint32_t best_frac_feedback_div = 0;
902926deccbSFrançois Tigeot 	uint32_t best_freq = -1;
903926deccbSFrançois Tigeot 	uint32_t best_error = 0xffffffff;
904926deccbSFrançois Tigeot 	uint32_t best_vco_diff = 1;
905926deccbSFrançois Tigeot 	uint32_t post_div;
906926deccbSFrançois Tigeot 	u32 pll_out_min, pll_out_max;
907926deccbSFrançois Tigeot 
908926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("PLL freq %ju %u %u\n", (uintmax_t)freq, pll->min_ref_div, pll->max_ref_div);
909926deccbSFrançois Tigeot 	freq = freq * 1000;
910926deccbSFrançois Tigeot 
911926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_IS_LCD) {
912926deccbSFrançois Tigeot 		pll_out_min = pll->lcd_pll_out_min;
913926deccbSFrançois Tigeot 		pll_out_max = pll->lcd_pll_out_max;
914926deccbSFrançois Tigeot 	} else {
915926deccbSFrançois Tigeot 		pll_out_min = pll->pll_out_min;
916926deccbSFrançois Tigeot 		pll_out_max = pll->pll_out_max;
917926deccbSFrançois Tigeot 	}
918926deccbSFrançois Tigeot 
919926deccbSFrançois Tigeot 	if (pll_out_min > 64800)
920926deccbSFrançois Tigeot 		pll_out_min = 64800;
921926deccbSFrançois Tigeot 
922926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
923926deccbSFrançois Tigeot 		min_ref_div = max_ref_div = pll->reference_div;
924926deccbSFrançois Tigeot 	else {
925926deccbSFrançois Tigeot 		while (min_ref_div < max_ref_div-1) {
926926deccbSFrançois Tigeot 			uint32_t mid = (min_ref_div + max_ref_div) / 2;
927926deccbSFrançois Tigeot 			uint32_t pll_in = pll->reference_freq / mid;
928926deccbSFrançois Tigeot 			if (pll_in < pll->pll_in_min)
929926deccbSFrançois Tigeot 				max_ref_div = mid;
930926deccbSFrançois Tigeot 			else if (pll_in > pll->pll_in_max)
931926deccbSFrançois Tigeot 				min_ref_div = mid;
932926deccbSFrançois Tigeot 			else
933926deccbSFrançois Tigeot 				break;
934926deccbSFrançois Tigeot 		}
935926deccbSFrançois Tigeot 	}
936926deccbSFrançois Tigeot 
937926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_POST_DIV)
938926deccbSFrançois Tigeot 		min_post_div = max_post_div = pll->post_div;
939926deccbSFrançois Tigeot 
940926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
941926deccbSFrançois Tigeot 		min_fractional_feed_div = pll->min_frac_feedback_div;
942926deccbSFrançois Tigeot 		max_fractional_feed_div = pll->max_frac_feedback_div;
943926deccbSFrançois Tigeot 	}
944926deccbSFrançois Tigeot 
945926deccbSFrançois Tigeot 	for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
946926deccbSFrançois Tigeot 		uint32_t ref_div;
947926deccbSFrançois Tigeot 
948926deccbSFrançois Tigeot 		if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
949926deccbSFrançois Tigeot 			continue;
950926deccbSFrançois Tigeot 
951926deccbSFrançois Tigeot 		/* legacy radeons only have a few post_divs */
952926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_LEGACY) {
953926deccbSFrançois Tigeot 			if ((post_div == 5) ||
954926deccbSFrançois Tigeot 			    (post_div == 7) ||
955926deccbSFrançois Tigeot 			    (post_div == 9) ||
956926deccbSFrançois Tigeot 			    (post_div == 10) ||
957926deccbSFrançois Tigeot 			    (post_div == 11) ||
958926deccbSFrançois Tigeot 			    (post_div == 13) ||
959926deccbSFrançois Tigeot 			    (post_div == 14) ||
960926deccbSFrançois Tigeot 			    (post_div == 15))
961926deccbSFrançois Tigeot 				continue;
962926deccbSFrançois Tigeot 		}
963926deccbSFrançois Tigeot 
964926deccbSFrançois Tigeot 		for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
965926deccbSFrançois Tigeot 			uint32_t feedback_div, current_freq = 0, error, vco_diff;
966926deccbSFrançois Tigeot 			uint32_t pll_in = pll->reference_freq / ref_div;
967926deccbSFrançois Tigeot 			uint32_t min_feed_div = pll->min_feedback_div;
968926deccbSFrançois Tigeot 			uint32_t max_feed_div = pll->max_feedback_div + 1;
969926deccbSFrançois Tigeot 
970926deccbSFrançois Tigeot 			if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
971926deccbSFrançois Tigeot 				continue;
972926deccbSFrançois Tigeot 
973926deccbSFrançois Tigeot 			while (min_feed_div < max_feed_div) {
974926deccbSFrançois Tigeot 				uint32_t vco;
975926deccbSFrançois Tigeot 				uint32_t min_frac_feed_div = min_fractional_feed_div;
976926deccbSFrançois Tigeot 				uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
977926deccbSFrançois Tigeot 				uint32_t frac_feedback_div;
978926deccbSFrançois Tigeot 				uint64_t tmp;
979926deccbSFrançois Tigeot 
980926deccbSFrançois Tigeot 				feedback_div = (min_feed_div + max_feed_div) / 2;
981926deccbSFrançois Tigeot 
982926deccbSFrançois Tigeot 				tmp = (uint64_t)pll->reference_freq * feedback_div;
983926deccbSFrançois Tigeot 				vco = radeon_div(tmp, ref_div);
984926deccbSFrançois Tigeot 
985926deccbSFrançois Tigeot 				if (vco < pll_out_min) {
986926deccbSFrançois Tigeot 					min_feed_div = feedback_div + 1;
987926deccbSFrançois Tigeot 					continue;
988926deccbSFrançois Tigeot 				} else if (vco > pll_out_max) {
989926deccbSFrançois Tigeot 					max_feed_div = feedback_div;
990926deccbSFrançois Tigeot 					continue;
991926deccbSFrançois Tigeot 				}
992926deccbSFrançois Tigeot 
993926deccbSFrançois Tigeot 				while (min_frac_feed_div < max_frac_feed_div) {
994926deccbSFrançois Tigeot 					frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
995926deccbSFrançois Tigeot 					tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
996926deccbSFrançois Tigeot 					tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
997926deccbSFrançois Tigeot 					current_freq = radeon_div(tmp, ref_div * post_div);
998926deccbSFrançois Tigeot 
999926deccbSFrançois Tigeot 					if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
1000926deccbSFrançois Tigeot 						if (freq < current_freq)
1001926deccbSFrançois Tigeot 							error = 0xffffffff;
1002926deccbSFrançois Tigeot 						else
1003926deccbSFrançois Tigeot 							error = freq - current_freq;
1004926deccbSFrançois Tigeot 					} else
1005926deccbSFrançois Tigeot 						error = abs(current_freq - freq);
1006926deccbSFrançois Tigeot 					vco_diff = abs(vco - best_vco);
1007926deccbSFrançois Tigeot 
1008926deccbSFrançois Tigeot 					if ((best_vco == 0 && error < best_error) ||
1009926deccbSFrançois Tigeot 					    (best_vco != 0 &&
1010926deccbSFrançois Tigeot 					     ((best_error > 100 && error < best_error - 100) ||
1011926deccbSFrançois Tigeot 					      (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
1012926deccbSFrançois Tigeot 						best_post_div = post_div;
1013926deccbSFrançois Tigeot 						best_ref_div = ref_div;
1014926deccbSFrançois Tigeot 						best_feedback_div = feedback_div;
1015926deccbSFrançois Tigeot 						best_frac_feedback_div = frac_feedback_div;
1016926deccbSFrançois Tigeot 						best_freq = current_freq;
1017926deccbSFrançois Tigeot 						best_error = error;
1018926deccbSFrançois Tigeot 						best_vco_diff = vco_diff;
1019926deccbSFrançois Tigeot 					} else if (current_freq == freq) {
1020926deccbSFrançois Tigeot 						if (best_freq == -1) {
1021926deccbSFrançois Tigeot 							best_post_div = post_div;
1022926deccbSFrançois Tigeot 							best_ref_div = ref_div;
1023926deccbSFrançois Tigeot 							best_feedback_div = feedback_div;
1024926deccbSFrançois Tigeot 							best_frac_feedback_div = frac_feedback_div;
1025926deccbSFrançois Tigeot 							best_freq = current_freq;
1026926deccbSFrançois Tigeot 							best_error = error;
1027926deccbSFrançois Tigeot 							best_vco_diff = vco_diff;
1028926deccbSFrançois Tigeot 						} else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1029926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1030926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1031926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1032926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1033926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1034926deccbSFrançois Tigeot 							best_post_div = post_div;
1035926deccbSFrançois Tigeot 							best_ref_div = ref_div;
1036926deccbSFrançois Tigeot 							best_feedback_div = feedback_div;
1037926deccbSFrançois Tigeot 							best_frac_feedback_div = frac_feedback_div;
1038926deccbSFrançois Tigeot 							best_freq = current_freq;
1039926deccbSFrançois Tigeot 							best_error = error;
1040926deccbSFrançois Tigeot 							best_vco_diff = vco_diff;
1041926deccbSFrançois Tigeot 						}
1042926deccbSFrançois Tigeot 					}
1043926deccbSFrançois Tigeot 					if (current_freq < freq)
1044926deccbSFrançois Tigeot 						min_frac_feed_div = frac_feedback_div + 1;
1045926deccbSFrançois Tigeot 					else
1046926deccbSFrançois Tigeot 						max_frac_feed_div = frac_feedback_div;
1047926deccbSFrançois Tigeot 				}
1048926deccbSFrançois Tigeot 				if (current_freq < freq)
1049926deccbSFrançois Tigeot 					min_feed_div = feedback_div + 1;
1050926deccbSFrançois Tigeot 				else
1051926deccbSFrançois Tigeot 					max_feed_div = feedback_div;
1052926deccbSFrançois Tigeot 			}
1053926deccbSFrançois Tigeot 		}
1054926deccbSFrançois Tigeot 	}
1055926deccbSFrançois Tigeot 
1056926deccbSFrançois Tigeot 	*dot_clock_p = best_freq / 10000;
1057926deccbSFrançois Tigeot 	*fb_div_p = best_feedback_div;
1058926deccbSFrançois Tigeot 	*frac_fb_div_p = best_frac_feedback_div;
1059926deccbSFrançois Tigeot 	*ref_div_p = best_ref_div;
1060926deccbSFrançois Tigeot 	*post_div_p = best_post_div;
1061926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1062926deccbSFrançois Tigeot 		      (long long)freq,
1063926deccbSFrançois Tigeot 		      best_freq / 1000, best_feedback_div, best_frac_feedback_div,
1064926deccbSFrançois Tigeot 		      best_ref_div, best_post_div);
1065926deccbSFrançois Tigeot 
1066926deccbSFrançois Tigeot }
1067926deccbSFrançois Tigeot 
1068926deccbSFrançois Tigeot static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1069926deccbSFrançois Tigeot {
1070926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1071926deccbSFrançois Tigeot 
1072926deccbSFrançois Tigeot 	if (radeon_fb->obj) {
1073926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(radeon_fb->obj);
1074926deccbSFrançois Tigeot 	}
1075926deccbSFrançois Tigeot 	drm_framebuffer_cleanup(fb);
1076c4ef309bSzrj 	kfree(radeon_fb);
1077926deccbSFrançois Tigeot }
1078926deccbSFrançois Tigeot 
1079926deccbSFrançois Tigeot static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1080926deccbSFrançois Tigeot 						  struct drm_file *file_priv,
1081926deccbSFrançois Tigeot 						  unsigned int *handle)
1082926deccbSFrançois Tigeot {
1083926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1084926deccbSFrançois Tigeot 
1085926deccbSFrançois Tigeot 	return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1086926deccbSFrançois Tigeot }
1087926deccbSFrançois Tigeot 
1088926deccbSFrançois Tigeot static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1089926deccbSFrançois Tigeot 	.destroy = radeon_user_framebuffer_destroy,
1090926deccbSFrançois Tigeot 	.create_handle = radeon_user_framebuffer_create_handle,
1091926deccbSFrançois Tigeot };
1092926deccbSFrançois Tigeot 
1093926deccbSFrançois Tigeot int
1094926deccbSFrançois Tigeot radeon_framebuffer_init(struct drm_device *dev,
1095926deccbSFrançois Tigeot 			struct radeon_framebuffer *rfb,
1096926deccbSFrançois Tigeot 			struct drm_mode_fb_cmd2 *mode_cmd,
1097926deccbSFrançois Tigeot 			struct drm_gem_object *obj)
1098926deccbSFrançois Tigeot {
1099926deccbSFrançois Tigeot 	int ret;
1100926deccbSFrançois Tigeot 	rfb->obj = obj;
1101b403bed8SMichael Neumann 	drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
1102926deccbSFrançois Tigeot 	ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1103926deccbSFrançois Tigeot 	if (ret) {
1104926deccbSFrançois Tigeot 		rfb->obj = NULL;
1105926deccbSFrançois Tigeot 		return ret;
1106926deccbSFrançois Tigeot 	}
1107926deccbSFrançois Tigeot 	return 0;
1108926deccbSFrançois Tigeot }
1109926deccbSFrançois Tigeot 
111060fc7eecSFrançois Tigeot static struct drm_framebuffer *
1111926deccbSFrançois Tigeot radeon_user_framebuffer_create(struct drm_device *dev,
1112926deccbSFrançois Tigeot 			       struct drm_file *file_priv,
111360fc7eecSFrançois Tigeot 			       struct drm_mode_fb_cmd2 *mode_cmd)
1114926deccbSFrançois Tigeot {
1115926deccbSFrançois Tigeot 	struct drm_gem_object *obj;
1116926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb;
1117926deccbSFrançois Tigeot 	int ret;
1118926deccbSFrançois Tigeot 
1119926deccbSFrançois Tigeot 	obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
1120926deccbSFrançois Tigeot 	if (obj ==  NULL) {
11216df74fa7SFrançois Tigeot 		dev_err(dev->dev, "No GEM object associated to handle 0x%08X, "
1122926deccbSFrançois Tigeot 			"can't create framebuffer\n", mode_cmd->handles[0]);
112360fc7eecSFrançois Tigeot 		return ERR_PTR(-ENOENT);
1124926deccbSFrançois Tigeot 	}
1125926deccbSFrançois Tigeot 
1126c4ef309bSzrj 	radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
1127926deccbSFrançois Tigeot 	if (radeon_fb == NULL) {
1128926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(obj);
112960fc7eecSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
1130926deccbSFrançois Tigeot 	}
1131926deccbSFrançois Tigeot 
1132926deccbSFrançois Tigeot 	ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1133926deccbSFrançois Tigeot 	if (ret) {
1134158486a6SFrançois Tigeot 		kfree(radeon_fb);
1135926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(obj);
113660fc7eecSFrançois Tigeot 		return ERR_PTR(ret);
1137926deccbSFrançois Tigeot 	}
1138926deccbSFrançois Tigeot 
113960fc7eecSFrançois Tigeot 	return &radeon_fb->base;
1140926deccbSFrançois Tigeot }
1141926deccbSFrançois Tigeot 
1142926deccbSFrançois Tigeot static void radeon_output_poll_changed(struct drm_device *dev)
1143926deccbSFrançois Tigeot {
1144926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1145926deccbSFrançois Tigeot 	radeon_fb_output_poll_changed(rdev);
1146926deccbSFrançois Tigeot }
1147926deccbSFrançois Tigeot 
1148926deccbSFrançois Tigeot static const struct drm_mode_config_funcs radeon_mode_funcs = {
1149926deccbSFrançois Tigeot 	.fb_create = radeon_user_framebuffer_create,
1150926deccbSFrançois Tigeot 	.output_poll_changed = radeon_output_poll_changed
1151926deccbSFrançois Tigeot };
1152926deccbSFrançois Tigeot 
1153926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1154926deccbSFrançois Tigeot {	{ 0, "driver" },
1155926deccbSFrançois Tigeot 	{ 1, "bios" },
1156926deccbSFrançois Tigeot };
1157926deccbSFrançois Tigeot 
1158926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1159926deccbSFrançois Tigeot {	{ TV_STD_NTSC, "ntsc" },
1160926deccbSFrançois Tigeot 	{ TV_STD_PAL, "pal" },
1161926deccbSFrançois Tigeot 	{ TV_STD_PAL_M, "pal-m" },
1162926deccbSFrançois Tigeot 	{ TV_STD_PAL_60, "pal-60" },
1163926deccbSFrançois Tigeot 	{ TV_STD_NTSC_J, "ntsc-j" },
1164926deccbSFrançois Tigeot 	{ TV_STD_SCART_PAL, "scart-pal" },
1165926deccbSFrançois Tigeot 	{ TV_STD_PAL_CN, "pal-cn" },
1166926deccbSFrançois Tigeot 	{ TV_STD_SECAM, "secam" },
1167926deccbSFrançois Tigeot };
1168926deccbSFrançois Tigeot 
1169926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1170926deccbSFrançois Tigeot {	{ UNDERSCAN_OFF, "off" },
1171926deccbSFrançois Tigeot 	{ UNDERSCAN_ON, "on" },
1172926deccbSFrançois Tigeot 	{ UNDERSCAN_AUTO, "auto" },
1173926deccbSFrançois Tigeot };
1174926deccbSFrançois Tigeot 
1175926deccbSFrançois Tigeot static int radeon_modeset_create_props(struct radeon_device *rdev)
1176926deccbSFrançois Tigeot {
1177926deccbSFrançois Tigeot 	int sz;
1178926deccbSFrançois Tigeot 
1179926deccbSFrançois Tigeot 	if (rdev->is_atom_bios) {
1180926deccbSFrançois Tigeot 		rdev->mode_info.coherent_mode_property =
1181926deccbSFrançois Tigeot 			drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
1182926deccbSFrançois Tigeot 		if (!rdev->mode_info.coherent_mode_property)
1183926deccbSFrançois Tigeot 			return -ENOMEM;
1184926deccbSFrançois Tigeot 	}
1185926deccbSFrançois Tigeot 
1186926deccbSFrançois Tigeot 	if (!ASIC_IS_AVIVO(rdev)) {
1187c4ef309bSzrj 		sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
1188926deccbSFrançois Tigeot 		rdev->mode_info.tmds_pll_property =
1189926deccbSFrançois Tigeot 			drm_property_create_enum(rdev->ddev, 0,
1190926deccbSFrançois Tigeot 					    "tmds_pll",
1191926deccbSFrançois Tigeot 					    radeon_tmds_pll_enum_list, sz);
1192926deccbSFrançois Tigeot 	}
1193926deccbSFrançois Tigeot 
1194926deccbSFrançois Tigeot 	rdev->mode_info.load_detect_property =
1195926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
1196926deccbSFrançois Tigeot 	if (!rdev->mode_info.load_detect_property)
1197926deccbSFrançois Tigeot 		return -ENOMEM;
1198926deccbSFrançois Tigeot 
1199926deccbSFrançois Tigeot 	drm_mode_create_scaling_mode_property(rdev->ddev);
1200926deccbSFrançois Tigeot 
1201c4ef309bSzrj 	sz = ARRAY_SIZE(radeon_tv_std_enum_list);
1202926deccbSFrançois Tigeot 	rdev->mode_info.tv_std_property =
1203926deccbSFrançois Tigeot 		drm_property_create_enum(rdev->ddev, 0,
1204926deccbSFrançois Tigeot 				    "tv standard",
1205926deccbSFrançois Tigeot 				    radeon_tv_std_enum_list, sz);
1206926deccbSFrançois Tigeot 
1207c4ef309bSzrj 	sz = ARRAY_SIZE(radeon_underscan_enum_list);
1208926deccbSFrançois Tigeot 	rdev->mode_info.underscan_property =
1209926deccbSFrançois Tigeot 		drm_property_create_enum(rdev->ddev, 0,
1210926deccbSFrançois Tigeot 				    "underscan",
1211926deccbSFrançois Tigeot 				    radeon_underscan_enum_list, sz);
1212926deccbSFrançois Tigeot 
1213926deccbSFrançois Tigeot 	rdev->mode_info.underscan_hborder_property =
1214926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0,
1215926deccbSFrançois Tigeot 					"underscan hborder", 0, 128);
1216926deccbSFrançois Tigeot 	if (!rdev->mode_info.underscan_hborder_property)
1217926deccbSFrançois Tigeot 		return -ENOMEM;
1218926deccbSFrançois Tigeot 
1219926deccbSFrançois Tigeot 	rdev->mode_info.underscan_vborder_property =
1220926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0,
1221926deccbSFrançois Tigeot 					"underscan vborder", 0, 128);
1222926deccbSFrançois Tigeot 	if (!rdev->mode_info.underscan_vborder_property)
1223926deccbSFrançois Tigeot 		return -ENOMEM;
1224926deccbSFrançois Tigeot 
1225926deccbSFrançois Tigeot 	return 0;
1226926deccbSFrançois Tigeot }
1227926deccbSFrançois Tigeot 
1228926deccbSFrançois Tigeot void radeon_update_display_priority(struct radeon_device *rdev)
1229926deccbSFrançois Tigeot {
1230926deccbSFrançois Tigeot 	/* adjustment options for the display watermarks */
1231926deccbSFrançois Tigeot 	if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1232926deccbSFrançois Tigeot 		/* set display priority to high for r3xx, rv515 chips
1233926deccbSFrançois Tigeot 		 * this avoids flickering due to underflow to the
1234926deccbSFrançois Tigeot 		 * display controllers during heavy acceleration.
1235926deccbSFrançois Tigeot 		 * Don't force high on rs4xx igp chips as it seems to
1236926deccbSFrançois Tigeot 		 * affect the sound card.  See kernel bug 15982.
1237926deccbSFrançois Tigeot 		 */
1238926deccbSFrançois Tigeot 		if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1239926deccbSFrançois Tigeot 		    !(rdev->flags & RADEON_IS_IGP))
1240926deccbSFrançois Tigeot 			rdev->disp_priority = 2;
1241926deccbSFrançois Tigeot 		else
1242926deccbSFrançois Tigeot 			rdev->disp_priority = 0;
1243926deccbSFrançois Tigeot 	} else
1244926deccbSFrançois Tigeot 		rdev->disp_priority = radeon_disp_priority;
1245926deccbSFrançois Tigeot 
1246926deccbSFrançois Tigeot }
1247926deccbSFrançois Tigeot 
1248926deccbSFrançois Tigeot /*
1249926deccbSFrançois Tigeot  * Allocate hdmi structs and determine register offsets
1250926deccbSFrançois Tigeot  */
1251926deccbSFrançois Tigeot static void radeon_afmt_init(struct radeon_device *rdev)
1252926deccbSFrançois Tigeot {
1253926deccbSFrançois Tigeot 	int i;
1254926deccbSFrançois Tigeot 
1255926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1256926deccbSFrançois Tigeot 		rdev->mode_info.afmt[i] = NULL;
1257926deccbSFrançois Tigeot 
1258926deccbSFrançois Tigeot 	if (ASIC_IS_DCE6(rdev)) {
1259926deccbSFrançois Tigeot 		/* todo */
1260926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE4(rdev)) {
1261926deccbSFrançois Tigeot 		/* DCE4/5 has 6 audio blocks tied to DIG encoders */
1262926deccbSFrançois Tigeot 		/* DCE4.1 has 2 audio blocks tied to DIG encoders */
1263c4ef309bSzrj 		rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1264926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1265926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
1266926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1267926deccbSFrançois Tigeot 		}
1268c4ef309bSzrj 		rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1269926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[1]) {
1270926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
1271926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->id = 1;
1272926deccbSFrançois Tigeot 		}
1273926deccbSFrançois Tigeot 		if (!ASIC_IS_DCE41(rdev)) {
1274c4ef309bSzrj 			rdev->mode_info.afmt[2] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1275926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[2]) {
1276926deccbSFrançois Tigeot 				rdev->mode_info.afmt[2]->offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
1277926deccbSFrançois Tigeot 				rdev->mode_info.afmt[2]->id = 2;
1278926deccbSFrançois Tigeot 			}
1279c4ef309bSzrj 			rdev->mode_info.afmt[3] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1280926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[3]) {
1281926deccbSFrançois Tigeot 				rdev->mode_info.afmt[3]->offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
1282926deccbSFrançois Tigeot 				rdev->mode_info.afmt[3]->id = 3;
1283926deccbSFrançois Tigeot 			}
1284c4ef309bSzrj 			rdev->mode_info.afmt[4] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1285926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[4]) {
1286926deccbSFrançois Tigeot 				rdev->mode_info.afmt[4]->offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
1287926deccbSFrançois Tigeot 				rdev->mode_info.afmt[4]->id = 4;
1288926deccbSFrançois Tigeot 			}
1289c4ef309bSzrj 			rdev->mode_info.afmt[5] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1290926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[5]) {
1291926deccbSFrançois Tigeot 				rdev->mode_info.afmt[5]->offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
1292926deccbSFrançois Tigeot 				rdev->mode_info.afmt[5]->id = 5;
1293926deccbSFrançois Tigeot 			}
1294926deccbSFrançois Tigeot 		}
1295926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE3(rdev)) {
1296926deccbSFrançois Tigeot 		/* DCE3.x has 2 audio blocks tied to DIG encoders */
1297c4ef309bSzrj 		rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1298926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1299926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1300926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1301926deccbSFrançois Tigeot 		}
1302c4ef309bSzrj 		rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1303926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[1]) {
1304926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1305926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->id = 1;
1306926deccbSFrançois Tigeot 		}
1307926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE2(rdev)) {
1308926deccbSFrançois Tigeot 		/* DCE2 has at least 1 routable audio block */
1309c4ef309bSzrj 		rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1310926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1311926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1312926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1313926deccbSFrançois Tigeot 		}
1314926deccbSFrançois Tigeot 		/* r6xx has 2 routable audio blocks */
1315926deccbSFrançois Tigeot 		if (rdev->family >= CHIP_R600) {
1316c4ef309bSzrj 			rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
1317926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[1]) {
1318926deccbSFrançois Tigeot 				rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1319926deccbSFrançois Tigeot 				rdev->mode_info.afmt[1]->id = 1;
1320926deccbSFrançois Tigeot 			}
1321926deccbSFrançois Tigeot 		}
1322926deccbSFrançois Tigeot 	}
1323926deccbSFrançois Tigeot }
1324926deccbSFrançois Tigeot 
1325926deccbSFrançois Tigeot static void radeon_afmt_fini(struct radeon_device *rdev)
1326926deccbSFrançois Tigeot {
1327926deccbSFrançois Tigeot 	int i;
1328926deccbSFrançois Tigeot 
1329926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
1330c4ef309bSzrj 		kfree(rdev->mode_info.afmt[i]);
1331926deccbSFrançois Tigeot 		rdev->mode_info.afmt[i] = NULL;
1332926deccbSFrançois Tigeot 	}
1333926deccbSFrançois Tigeot }
1334926deccbSFrançois Tigeot 
1335926deccbSFrançois Tigeot int radeon_modeset_init(struct radeon_device *rdev)
1336926deccbSFrançois Tigeot {
1337926deccbSFrançois Tigeot 	int i;
1338926deccbSFrançois Tigeot 	int ret;
1339926deccbSFrançois Tigeot 
1340926deccbSFrançois Tigeot 	drm_mode_config_init(rdev->ddev);
1341926deccbSFrançois Tigeot 	rdev->mode_info.mode_config_initialized = true;
1342926deccbSFrançois Tigeot 
1343926deccbSFrançois Tigeot 	rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
1344926deccbSFrançois Tigeot 
1345926deccbSFrançois Tigeot 	if (ASIC_IS_DCE5(rdev)) {
1346926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 16384;
1347926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 16384;
1348926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
1349926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 8192;
1350926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 8192;
1351926deccbSFrançois Tigeot 	} else {
1352926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 4096;
1353926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 4096;
1354926deccbSFrançois Tigeot 	}
1355926deccbSFrançois Tigeot 
1356926deccbSFrançois Tigeot 	rdev->ddev->mode_config.preferred_depth = 24;
1357926deccbSFrançois Tigeot 	rdev->ddev->mode_config.prefer_shadow = 1;
1358926deccbSFrançois Tigeot 
1359926deccbSFrançois Tigeot 	rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1360926deccbSFrançois Tigeot 
1361926deccbSFrançois Tigeot 	ret = radeon_modeset_create_props(rdev);
1362926deccbSFrançois Tigeot 	if (ret) {
1363926deccbSFrançois Tigeot 		return ret;
1364926deccbSFrançois Tigeot 	}
1365926deccbSFrançois Tigeot 
1366926deccbSFrançois Tigeot 	/* init i2c buses */
1367926deccbSFrançois Tigeot 	radeon_i2c_init(rdev);
1368926deccbSFrançois Tigeot 
1369926deccbSFrançois Tigeot 	/* check combios for a valid hardcoded EDID - Sun servers */
1370926deccbSFrançois Tigeot 	if (!rdev->is_atom_bios) {
1371926deccbSFrançois Tigeot 		/* check for hardcoded EDID in BIOS */
1372926deccbSFrançois Tigeot 		radeon_combios_check_hardcoded_edid(rdev);
1373926deccbSFrançois Tigeot 	}
1374926deccbSFrançois Tigeot 
1375926deccbSFrançois Tigeot 	/* allocate crtcs */
1376926deccbSFrançois Tigeot 	for (i = 0; i < rdev->num_crtc; i++) {
1377926deccbSFrançois Tigeot 		radeon_crtc_init(rdev->ddev, i);
1378926deccbSFrançois Tigeot 	}
1379926deccbSFrançois Tigeot 
1380926deccbSFrançois Tigeot 	/* okay we should have all the bios connectors */
1381926deccbSFrançois Tigeot 	ret = radeon_setup_enc_conn(rdev->ddev);
1382926deccbSFrançois Tigeot 	if (!ret) {
1383926deccbSFrançois Tigeot 		return ret;
1384926deccbSFrançois Tigeot 	}
1385926deccbSFrançois Tigeot 
1386926deccbSFrançois Tigeot 	/* init dig PHYs, disp eng pll */
1387926deccbSFrançois Tigeot 	if (rdev->is_atom_bios) {
1388926deccbSFrançois Tigeot 		radeon_atom_encoder_init(rdev);
1389926deccbSFrançois Tigeot 		radeon_atom_disp_eng_pll_init(rdev);
1390926deccbSFrançois Tigeot 	}
1391926deccbSFrançois Tigeot 
1392926deccbSFrançois Tigeot 	/* initialize hpd */
1393926deccbSFrançois Tigeot 	radeon_hpd_init(rdev);
1394926deccbSFrançois Tigeot 
1395926deccbSFrançois Tigeot 	/* setup afmt */
1396926deccbSFrançois Tigeot 	radeon_afmt_init(rdev);
1397926deccbSFrançois Tigeot 
1398926deccbSFrançois Tigeot 	/* Initialize power management */
1399926deccbSFrançois Tigeot 	radeon_pm_init(rdev);
1400926deccbSFrançois Tigeot 
1401926deccbSFrançois Tigeot 	radeon_fbdev_init(rdev);
1402926deccbSFrançois Tigeot 	drm_kms_helper_poll_init(rdev->ddev);
1403926deccbSFrançois Tigeot 
1404926deccbSFrançois Tigeot 	return 0;
1405926deccbSFrançois Tigeot }
1406926deccbSFrançois Tigeot 
1407926deccbSFrançois Tigeot void radeon_modeset_fini(struct radeon_device *rdev)
1408926deccbSFrançois Tigeot {
1409926deccbSFrançois Tigeot 	radeon_fbdev_fini(rdev);
1410c4ef309bSzrj 	kfree(rdev->mode_info.bios_hardcoded_edid);
1411926deccbSFrançois Tigeot 	radeon_pm_fini(rdev);
1412926deccbSFrançois Tigeot 
1413926deccbSFrançois Tigeot 	if (rdev->mode_info.mode_config_initialized) {
1414926deccbSFrançois Tigeot 		radeon_afmt_fini(rdev);
1415926deccbSFrançois Tigeot 		drm_kms_helper_poll_fini(rdev->ddev);
1416926deccbSFrançois Tigeot 		radeon_hpd_fini(rdev);
1417926deccbSFrançois Tigeot 		DRM_UNLOCK(rdev->ddev); /* Work around lock recursion. dumbbell@ */
1418926deccbSFrançois Tigeot 		drm_mode_config_cleanup(rdev->ddev);
1419926deccbSFrançois Tigeot 		DRM_LOCK(rdev->ddev);
1420926deccbSFrançois Tigeot 		rdev->mode_info.mode_config_initialized = false;
1421926deccbSFrançois Tigeot 	}
1422926deccbSFrançois Tigeot 	/* free i2c buses */
1423926deccbSFrançois Tigeot 	radeon_i2c_fini(rdev);
1424926deccbSFrançois Tigeot }
1425926deccbSFrançois Tigeot 
1426926deccbSFrançois Tigeot static bool is_hdtv_mode(const struct drm_display_mode *mode)
1427926deccbSFrançois Tigeot {
1428926deccbSFrançois Tigeot 	/* try and guess if this is a tv or a monitor */
1429926deccbSFrançois Tigeot 	if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1430926deccbSFrançois Tigeot 	    (mode->vdisplay == 576) || /* 576p */
1431926deccbSFrançois Tigeot 	    (mode->vdisplay == 720) || /* 720p */
1432926deccbSFrançois Tigeot 	    (mode->vdisplay == 1080)) /* 1080p */
1433926deccbSFrançois Tigeot 		return true;
1434926deccbSFrançois Tigeot 	else
1435926deccbSFrançois Tigeot 		return false;
1436926deccbSFrançois Tigeot }
1437926deccbSFrançois Tigeot 
1438926deccbSFrançois Tigeot bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1439926deccbSFrançois Tigeot 				const struct drm_display_mode *mode,
1440926deccbSFrançois Tigeot 				struct drm_display_mode *adjusted_mode)
1441926deccbSFrançois Tigeot {
1442926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
1443926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1444926deccbSFrançois Tigeot 	struct drm_encoder *encoder;
1445926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1446926deccbSFrançois Tigeot 	struct radeon_encoder *radeon_encoder;
1447926deccbSFrançois Tigeot 	struct drm_connector *connector;
1448926deccbSFrançois Tigeot 	struct radeon_connector *radeon_connector;
1449926deccbSFrançois Tigeot 	bool first = true;
1450926deccbSFrançois Tigeot 	u32 src_v = 1, dst_v = 1;
1451926deccbSFrançois Tigeot 	u32 src_h = 1, dst_h = 1;
1452926deccbSFrançois Tigeot 
1453926deccbSFrançois Tigeot 	radeon_crtc->h_border = 0;
1454926deccbSFrançois Tigeot 	radeon_crtc->v_border = 0;
1455926deccbSFrançois Tigeot 
1456926deccbSFrançois Tigeot 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1457926deccbSFrançois Tigeot 		if (encoder->crtc != crtc)
1458926deccbSFrançois Tigeot 			continue;
1459926deccbSFrançois Tigeot 		radeon_encoder = to_radeon_encoder(encoder);
1460926deccbSFrançois Tigeot 		connector = radeon_get_connector_for_encoder(encoder);
1461926deccbSFrançois Tigeot 		radeon_connector = to_radeon_connector(connector);
1462926deccbSFrançois Tigeot 
1463926deccbSFrançois Tigeot 		if (first) {
1464926deccbSFrançois Tigeot 			/* set scaling */
1465926deccbSFrançois Tigeot 			if (radeon_encoder->rmx_type == RMX_OFF)
1466926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_OFF;
1467926deccbSFrançois Tigeot 			else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1468926deccbSFrançois Tigeot 				 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1469926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1470926deccbSFrançois Tigeot 			else
1471926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_OFF;
1472926deccbSFrançois Tigeot 			/* copy native mode */
1473926deccbSFrançois Tigeot 			memcpy(&radeon_crtc->native_mode,
1474926deccbSFrançois Tigeot 			       &radeon_encoder->native_mode,
1475926deccbSFrançois Tigeot 				sizeof(struct drm_display_mode));
1476926deccbSFrançois Tigeot 			src_v = crtc->mode.vdisplay;
1477926deccbSFrançois Tigeot 			dst_v = radeon_crtc->native_mode.vdisplay;
1478926deccbSFrançois Tigeot 			src_h = crtc->mode.hdisplay;
1479926deccbSFrançois Tigeot 			dst_h = radeon_crtc->native_mode.hdisplay;
1480926deccbSFrançois Tigeot 
1481926deccbSFrançois Tigeot 			/* fix up for overscan on hdmi */
1482926deccbSFrançois Tigeot 			if (ASIC_IS_AVIVO(rdev) &&
1483926deccbSFrançois Tigeot 			    (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1484926deccbSFrançois Tigeot 			    ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1485926deccbSFrançois Tigeot 			     ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
1486926deccbSFrançois Tigeot 			      drm_detect_hdmi_monitor(radeon_connector->edid) &&
1487926deccbSFrançois Tigeot 			      is_hdtv_mode(mode)))) {
1488926deccbSFrançois Tigeot 				if (radeon_encoder->underscan_hborder != 0)
1489926deccbSFrançois Tigeot 					radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1490926deccbSFrançois Tigeot 				else
1491926deccbSFrançois Tigeot 					radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1492926deccbSFrançois Tigeot 				if (radeon_encoder->underscan_vborder != 0)
1493926deccbSFrançois Tigeot 					radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1494926deccbSFrançois Tigeot 				else
1495926deccbSFrançois Tigeot 					radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1496926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_FULL;
1497926deccbSFrançois Tigeot 				src_v = crtc->mode.vdisplay;
1498926deccbSFrançois Tigeot 				dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1499926deccbSFrançois Tigeot 				src_h = crtc->mode.hdisplay;
1500926deccbSFrançois Tigeot 				dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1501926deccbSFrançois Tigeot 			}
1502926deccbSFrançois Tigeot 			first = false;
1503926deccbSFrançois Tigeot 		} else {
1504926deccbSFrançois Tigeot 			if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1505926deccbSFrançois Tigeot 				/* WARNING: Right now this can't happen but
1506926deccbSFrançois Tigeot 				 * in the future we need to check that scaling
1507926deccbSFrançois Tigeot 				 * are consistent across different encoder
1508926deccbSFrançois Tigeot 				 * (ie all encoder can work with the same
1509926deccbSFrançois Tigeot 				 *  scaling).
1510926deccbSFrançois Tigeot 				 */
1511926deccbSFrançois Tigeot 				DRM_ERROR("Scaling not consistent across encoder.\n");
1512926deccbSFrançois Tigeot 				return false;
1513926deccbSFrançois Tigeot 			}
1514926deccbSFrançois Tigeot 		}
1515926deccbSFrançois Tigeot 	}
1516926deccbSFrançois Tigeot 	if (radeon_crtc->rmx_type != RMX_OFF) {
1517926deccbSFrançois Tigeot 		fixed20_12 a, b;
1518926deccbSFrançois Tigeot 		a.full = dfixed_const(src_v);
1519926deccbSFrançois Tigeot 		b.full = dfixed_const(dst_v);
1520926deccbSFrançois Tigeot 		radeon_crtc->vsc.full = dfixed_div(a, b);
1521926deccbSFrançois Tigeot 		a.full = dfixed_const(src_h);
1522926deccbSFrançois Tigeot 		b.full = dfixed_const(dst_h);
1523926deccbSFrançois Tigeot 		radeon_crtc->hsc.full = dfixed_div(a, b);
1524926deccbSFrançois Tigeot 	} else {
1525926deccbSFrançois Tigeot 		radeon_crtc->vsc.full = dfixed_const(1);
1526926deccbSFrançois Tigeot 		radeon_crtc->hsc.full = dfixed_const(1);
1527926deccbSFrançois Tigeot 	}
1528926deccbSFrançois Tigeot 	return true;
1529926deccbSFrançois Tigeot }
1530926deccbSFrançois Tigeot 
1531926deccbSFrançois Tigeot /*
1532782e40d3SFrançois Tigeot  * Retrieve current video scanout position of crtc on a given gpu, and
1533782e40d3SFrançois Tigeot  * an optional accurate timestamp of when query happened.
1534926deccbSFrançois Tigeot  *
1535926deccbSFrançois Tigeot  * \param dev Device to query.
1536926deccbSFrançois Tigeot  * \param crtc Crtc to query.
1537782e40d3SFrançois Tigeot  * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1538926deccbSFrançois Tigeot  * \param *vpos Location where vertical scanout position should be stored.
1539926deccbSFrançois Tigeot  * \param *hpos Location where horizontal scanout position should go.
1540782e40d3SFrançois Tigeot  * \param *stime Target location for timestamp taken immediately before
1541782e40d3SFrançois Tigeot  *               scanout position query. Can be NULL to skip timestamp.
1542782e40d3SFrançois Tigeot  * \param *etime Target location for timestamp taken immediately after
1543782e40d3SFrançois Tigeot  *               scanout position query. Can be NULL to skip timestamp.
1544926deccbSFrançois Tigeot  *
1545926deccbSFrançois Tigeot  * Returns vpos as a positive number while in active scanout area.
1546926deccbSFrançois Tigeot  * Returns vpos as a negative number inside vblank, counting the number
1547926deccbSFrançois Tigeot  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1548926deccbSFrançois Tigeot  * until start of active scanout / end of vblank."
1549926deccbSFrançois Tigeot  *
1550926deccbSFrançois Tigeot  * \return Flags, or'ed together as follows:
1551926deccbSFrançois Tigeot  *
1552926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_VALID = Query successful.
1553926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_INVBL = Inside vblank.
1554926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1555926deccbSFrançois Tigeot  * this flag means that returned position may be offset by a constant but
1556926deccbSFrançois Tigeot  * unknown small number of scanlines wrt. real scanout position.
1557926deccbSFrançois Tigeot  *
1558926deccbSFrançois Tigeot  */
1559782e40d3SFrançois Tigeot int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1560782e40d3SFrançois Tigeot 			       int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
1561926deccbSFrançois Tigeot {
1562926deccbSFrançois Tigeot 	u32 stat_crtc = 0, vbl = 0, position = 0;
1563926deccbSFrançois Tigeot 	int vbl_start, vbl_end, vtotal, ret = 0;
1564926deccbSFrançois Tigeot 	bool in_vbl = true;
1565926deccbSFrançois Tigeot 
1566926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1567926deccbSFrançois Tigeot 
1568782e40d3SFrançois Tigeot 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1569782e40d3SFrançois Tigeot 
1570782e40d3SFrançois Tigeot 	/* Get optional system timestamp before query. */
1571782e40d3SFrançois Tigeot 	if (stime)
1572782e40d3SFrançois Tigeot 		*stime = ktime_get();
1573782e40d3SFrançois Tigeot 
1574926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
1575926deccbSFrançois Tigeot 		if (crtc == 0) {
1576926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1577926deccbSFrançois Tigeot 				     EVERGREEN_CRTC0_REGISTER_OFFSET);
1578926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1579926deccbSFrançois Tigeot 					  EVERGREEN_CRTC0_REGISTER_OFFSET);
1580926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1581926deccbSFrançois Tigeot 		}
1582926deccbSFrançois Tigeot 		if (crtc == 1) {
1583926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1584926deccbSFrançois Tigeot 				     EVERGREEN_CRTC1_REGISTER_OFFSET);
1585926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1586926deccbSFrançois Tigeot 					  EVERGREEN_CRTC1_REGISTER_OFFSET);
1587926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1588926deccbSFrançois Tigeot 		}
1589926deccbSFrançois Tigeot 		if (crtc == 2) {
1590926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1591926deccbSFrançois Tigeot 				     EVERGREEN_CRTC2_REGISTER_OFFSET);
1592926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1593926deccbSFrançois Tigeot 					  EVERGREEN_CRTC2_REGISTER_OFFSET);
1594926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1595926deccbSFrançois Tigeot 		}
1596926deccbSFrançois Tigeot 		if (crtc == 3) {
1597926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1598926deccbSFrançois Tigeot 				     EVERGREEN_CRTC3_REGISTER_OFFSET);
1599926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1600926deccbSFrançois Tigeot 					  EVERGREEN_CRTC3_REGISTER_OFFSET);
1601926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1602926deccbSFrançois Tigeot 		}
1603926deccbSFrançois Tigeot 		if (crtc == 4) {
1604926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1605926deccbSFrançois Tigeot 				     EVERGREEN_CRTC4_REGISTER_OFFSET);
1606926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1607926deccbSFrançois Tigeot 					  EVERGREEN_CRTC4_REGISTER_OFFSET);
1608926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1609926deccbSFrançois Tigeot 		}
1610926deccbSFrançois Tigeot 		if (crtc == 5) {
1611926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1612926deccbSFrançois Tigeot 				     EVERGREEN_CRTC5_REGISTER_OFFSET);
1613926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1614926deccbSFrançois Tigeot 					  EVERGREEN_CRTC5_REGISTER_OFFSET);
1615926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1616926deccbSFrançois Tigeot 		}
1617926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
1618926deccbSFrançois Tigeot 		if (crtc == 0) {
1619926deccbSFrançois Tigeot 			vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1620926deccbSFrançois Tigeot 			position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1621926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1622926deccbSFrançois Tigeot 		}
1623926deccbSFrançois Tigeot 		if (crtc == 1) {
1624926deccbSFrançois Tigeot 			vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1625926deccbSFrançois Tigeot 			position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1626926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1627926deccbSFrançois Tigeot 		}
1628926deccbSFrançois Tigeot 	} else {
1629926deccbSFrançois Tigeot 		/* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1630926deccbSFrançois Tigeot 		if (crtc == 0) {
1631926deccbSFrançois Tigeot 			/* Assume vbl_end == 0, get vbl_start from
1632926deccbSFrançois Tigeot 			 * upper 16 bits.
1633926deccbSFrançois Tigeot 			 */
1634926deccbSFrançois Tigeot 			vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1635926deccbSFrançois Tigeot 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1636926deccbSFrançois Tigeot 			/* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1637926deccbSFrançois Tigeot 			position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1638926deccbSFrançois Tigeot 			stat_crtc = RREG32(RADEON_CRTC_STATUS);
1639926deccbSFrançois Tigeot 			if (!(stat_crtc & 1))
1640926deccbSFrançois Tigeot 				in_vbl = false;
1641926deccbSFrançois Tigeot 
1642926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1643926deccbSFrançois Tigeot 		}
1644926deccbSFrançois Tigeot 		if (crtc == 1) {
1645926deccbSFrançois Tigeot 			vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1646926deccbSFrançois Tigeot 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1647926deccbSFrançois Tigeot 			position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1648926deccbSFrançois Tigeot 			stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1649926deccbSFrançois Tigeot 			if (!(stat_crtc & 1))
1650926deccbSFrançois Tigeot 				in_vbl = false;
1651926deccbSFrançois Tigeot 
1652926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1653926deccbSFrançois Tigeot 		}
1654926deccbSFrançois Tigeot 	}
1655926deccbSFrançois Tigeot 
1656782e40d3SFrançois Tigeot 	/* Get optional system timestamp after query. */
1657782e40d3SFrançois Tigeot 	if (etime)
1658782e40d3SFrançois Tigeot 		*etime = ktime_get();
1659782e40d3SFrançois Tigeot 
1660782e40d3SFrançois Tigeot 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1661782e40d3SFrançois Tigeot 
1662926deccbSFrançois Tigeot 	/* Decode into vertical and horizontal scanout position. */
1663926deccbSFrançois Tigeot 	*vpos = position & 0x1fff;
1664926deccbSFrançois Tigeot 	*hpos = (position >> 16) & 0x1fff;
1665926deccbSFrançois Tigeot 
1666926deccbSFrançois Tigeot 	/* Valid vblank area boundaries from gpu retrieved? */
1667926deccbSFrançois Tigeot 	if (vbl > 0) {
1668926deccbSFrançois Tigeot 		/* Yes: Decode. */
1669926deccbSFrançois Tigeot 		ret |= DRM_SCANOUTPOS_ACCURATE;
1670926deccbSFrançois Tigeot 		vbl_start = vbl & 0x1fff;
1671926deccbSFrançois Tigeot 		vbl_end = (vbl >> 16) & 0x1fff;
1672926deccbSFrançois Tigeot 	}
1673926deccbSFrançois Tigeot 	else {
1674926deccbSFrançois Tigeot 		/* No: Fake something reasonable which gives at least ok results. */
1675926deccbSFrançois Tigeot 		vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1676926deccbSFrançois Tigeot 		vbl_end = 0;
1677926deccbSFrançois Tigeot 	}
1678926deccbSFrançois Tigeot 
1679926deccbSFrançois Tigeot 	/* Test scanout position against vblank region. */
1680926deccbSFrançois Tigeot 	if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1681926deccbSFrançois Tigeot 		in_vbl = false;
1682926deccbSFrançois Tigeot 
1683926deccbSFrançois Tigeot 	/* Check if inside vblank area and apply corrective offsets:
1684926deccbSFrançois Tigeot 	 * vpos will then be >=0 in video scanout area, but negative
1685926deccbSFrançois Tigeot 	 * within vblank area, counting down the number of lines until
1686926deccbSFrançois Tigeot 	 * start of scanout.
1687926deccbSFrançois Tigeot 	 */
1688926deccbSFrançois Tigeot 
1689926deccbSFrançois Tigeot 	/* Inside "upper part" of vblank area? Apply corrective offset if so: */
1690926deccbSFrançois Tigeot 	if (in_vbl && (*vpos >= vbl_start)) {
1691926deccbSFrançois Tigeot 		vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1692926deccbSFrançois Tigeot 		*vpos = *vpos - vtotal;
1693926deccbSFrançois Tigeot 	}
1694926deccbSFrançois Tigeot 
1695926deccbSFrançois Tigeot 	/* Correct for shifted end of vbl at vbl_end. */
1696926deccbSFrançois Tigeot 	*vpos = *vpos - vbl_end;
1697926deccbSFrançois Tigeot 
1698926deccbSFrançois Tigeot 	/* In vblank? */
1699926deccbSFrançois Tigeot 	if (in_vbl)
1700926deccbSFrançois Tigeot 		ret |= DRM_SCANOUTPOS_INVBL;
1701926deccbSFrançois Tigeot 
1702782e40d3SFrançois Tigeot 	/* Is vpos outside nominal vblank area, but less than
1703782e40d3SFrançois Tigeot 	 * 1/100 of a frame height away from start of vblank?
1704782e40d3SFrançois Tigeot 	 * If so, assume this isn't a massively delayed vblank
1705782e40d3SFrançois Tigeot 	 * interrupt, but a vblank interrupt that fired a few
1706782e40d3SFrançois Tigeot 	 * microseconds before true start of vblank. Compensate
1707782e40d3SFrançois Tigeot 	 * by adding a full frame duration to the final timestamp.
1708782e40d3SFrançois Tigeot 	 * Happens, e.g., on ATI R500, R600.
1709782e40d3SFrançois Tigeot 	 *
1710782e40d3SFrançois Tigeot 	 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1711782e40d3SFrançois Tigeot 	 */
1712782e40d3SFrançois Tigeot 	if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1713782e40d3SFrançois Tigeot 		vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1714782e40d3SFrançois Tigeot 		vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1715782e40d3SFrançois Tigeot 
1716782e40d3SFrançois Tigeot 		if (vbl_start - *vpos < vtotal / 100) {
1717782e40d3SFrançois Tigeot 			*vpos -= vtotal;
1718782e40d3SFrançois Tigeot 
1719782e40d3SFrançois Tigeot 			/* Signal this correction as "applied". */
1720782e40d3SFrançois Tigeot 			ret |= 0x8;
1721782e40d3SFrançois Tigeot 		}
1722782e40d3SFrançois Tigeot 	}
1723782e40d3SFrançois Tigeot 
1724926deccbSFrançois Tigeot 	return ret;
1725926deccbSFrançois Tigeot }
1726