xref: /dragonfly/sys/dev/drm/radeon/radeon_display.c (revision 782e40d3)
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);
2525a3b77d5SFrançois Tigeot 	drm_free(radeon_crtc, M_DRM);
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);
2755a3b77d5SFrançois Tigeot 	drm_free(work, M_DRM);
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 &&
309*782e40d3SFrançois Tigeot 	    (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
310*782e40d3SFranç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 
3645a3b77d5SFrançois Tigeot 	work = kmalloc(sizeof *work, M_DRM, M_WAITOK | M_ZERO);
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;
371926deccbSFrançois Tigeot 	old_radeon_fb = to_radeon_framebuffer(crtc->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 */
462926deccbSFrançois Tigeot 	crtc->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);
4925a3b77d5SFrançois Tigeot 	drm_free(work, M_DRM);
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 
512926deccbSFrançois Tigeot 	radeon_crtc = kmalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)),
5135a3b77d5SFrançois Tigeot 			      M_DRM, M_WAITOK | M_ZERO);
514926deccbSFrançois Tigeot 	if (radeon_crtc == NULL)
515926deccbSFrançois Tigeot 		return;
516926deccbSFrançois Tigeot 
517926deccbSFrançois Tigeot 	drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
518926deccbSFrançois Tigeot 
519926deccbSFrançois Tigeot 	drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
520926deccbSFrançois Tigeot 	radeon_crtc->crtc_id = index;
521926deccbSFrançois Tigeot 	rdev->mode_info.crtcs[index] = radeon_crtc;
522926deccbSFrançois Tigeot 
52357e252bfSMichael Neumann 	if (rdev->family >= CHIP_BONAIRE) {
52457e252bfSMichael Neumann 		radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
52557e252bfSMichael Neumann 		radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
52657e252bfSMichael Neumann 	} else {
52757e252bfSMichael Neumann 		radeon_crtc->max_cursor_width = CURSOR_WIDTH;
52857e252bfSMichael Neumann 		radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
52957e252bfSMichael Neumann 	}
53057e252bfSMichael Neumann 
531926deccbSFrançois Tigeot #if 0
532926deccbSFrançois Tigeot 	radeon_crtc->mode_set.crtc = &radeon_crtc->base;
533926deccbSFrançois Tigeot 	radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
534926deccbSFrançois Tigeot 	radeon_crtc->mode_set.num_connectors = 0;
535926deccbSFrançois Tigeot #endif
536926deccbSFrançois Tigeot 
537926deccbSFrançois Tigeot 	for (i = 0; i < 256; i++) {
538926deccbSFrançois Tigeot 		radeon_crtc->lut_r[i] = i << 2;
539926deccbSFrançois Tigeot 		radeon_crtc->lut_g[i] = i << 2;
540926deccbSFrançois Tigeot 		radeon_crtc->lut_b[i] = i << 2;
541926deccbSFrançois Tigeot 	}
542926deccbSFrançois Tigeot 
543926deccbSFrançois Tigeot 	if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
544926deccbSFrançois Tigeot 		radeon_atombios_init_crtc(dev, radeon_crtc);
545926deccbSFrançois Tigeot 	else
546926deccbSFrançois Tigeot 		radeon_legacy_init_crtc(dev, radeon_crtc);
547926deccbSFrançois Tigeot }
548926deccbSFrançois Tigeot 
54957e252bfSMichael Neumann static const char *encoder_names[38] = {
550926deccbSFrançois Tigeot 	"NONE",
551926deccbSFrançois Tigeot 	"INTERNAL_LVDS",
552926deccbSFrançois Tigeot 	"INTERNAL_TMDS1",
553926deccbSFrançois Tigeot 	"INTERNAL_TMDS2",
554926deccbSFrançois Tigeot 	"INTERNAL_DAC1",
555926deccbSFrançois Tigeot 	"INTERNAL_DAC2",
556926deccbSFrançois Tigeot 	"INTERNAL_SDVOA",
557926deccbSFrançois Tigeot 	"INTERNAL_SDVOB",
558926deccbSFrançois Tigeot 	"SI170B",
559926deccbSFrançois Tigeot 	"CH7303",
560926deccbSFrançois Tigeot 	"CH7301",
561926deccbSFrançois Tigeot 	"INTERNAL_DVO1",
562926deccbSFrançois Tigeot 	"EXTERNAL_SDVOA",
563926deccbSFrançois Tigeot 	"EXTERNAL_SDVOB",
564926deccbSFrançois Tigeot 	"TITFP513",
565926deccbSFrançois Tigeot 	"INTERNAL_LVTM1",
566926deccbSFrançois Tigeot 	"VT1623",
567926deccbSFrançois Tigeot 	"HDMI_SI1930",
568926deccbSFrançois Tigeot 	"HDMI_INTERNAL",
569926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_TMDS1",
570926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DVO1",
571926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DAC1",
572926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_DAC2",
573926deccbSFrançois Tigeot 	"SI178",
574926deccbSFrançois Tigeot 	"MVPU_FPGA",
575926deccbSFrançois Tigeot 	"INTERNAL_DDI",
576926deccbSFrançois Tigeot 	"VT1625",
577926deccbSFrançois Tigeot 	"HDMI_SI1932",
578926deccbSFrançois Tigeot 	"DP_AN9801",
579926deccbSFrançois Tigeot 	"DP_DP501",
580926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY",
581926deccbSFrançois Tigeot 	"INTERNAL_KLDSCP_LVTMA",
582926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY1",
583926deccbSFrançois Tigeot 	"INTERNAL_UNIPHY2",
584926deccbSFrançois Tigeot 	"NUTMEG",
585926deccbSFrançois Tigeot 	"TRAVIS",
58657e252bfSMichael Neumann 	"INTERNAL_VCE",
58757e252bfSMichael Neumann 	"INTERNAL_UNIPHY3",
588926deccbSFrançois Tigeot };
589926deccbSFrançois Tigeot 
590926deccbSFrançois Tigeot static const char *hpd_names[6] = {
591926deccbSFrançois Tigeot 	"HPD1",
592926deccbSFrançois Tigeot 	"HPD2",
593926deccbSFrançois Tigeot 	"HPD3",
594926deccbSFrançois Tigeot 	"HPD4",
595926deccbSFrançois Tigeot 	"HPD5",
596926deccbSFrançois Tigeot 	"HPD6",
597926deccbSFrançois Tigeot };
598926deccbSFrançois Tigeot 
599926deccbSFrançois Tigeot static void radeon_print_display_setup(struct drm_device *dev)
600926deccbSFrançois Tigeot {
601926deccbSFrançois Tigeot 	struct drm_connector *connector;
602926deccbSFrançois Tigeot 	struct radeon_connector *radeon_connector;
603926deccbSFrançois Tigeot 	struct drm_encoder *encoder;
604926deccbSFrançois Tigeot 	struct radeon_encoder *radeon_encoder;
605926deccbSFrançois Tigeot 	uint32_t devices;
606926deccbSFrançois Tigeot 	int i = 0;
607926deccbSFrançois Tigeot 
608926deccbSFrançois Tigeot 	DRM_INFO("Radeon Display Connectors\n");
609926deccbSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
610926deccbSFrançois Tigeot 		radeon_connector = to_radeon_connector(connector);
611926deccbSFrançois Tigeot 		DRM_INFO("Connector %d:\n", i);
612926deccbSFrançois Tigeot 		DRM_INFO("  %s\n", drm_get_connector_name(connector));
613926deccbSFrançois Tigeot 		if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
614926deccbSFrançois Tigeot 			DRM_INFO("  %s\n", hpd_names[radeon_connector->hpd.hpd]);
615926deccbSFrançois Tigeot 		if (radeon_connector->ddc_bus) {
616926deccbSFrançois Tigeot 			DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
617926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.mask_clk_reg,
618926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.mask_data_reg,
619926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.a_clk_reg,
620926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.a_data_reg,
621926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.en_clk_reg,
622926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.en_data_reg,
623926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.y_clk_reg,
624926deccbSFrançois Tigeot 				 radeon_connector->ddc_bus->rec.y_data_reg);
625926deccbSFrançois Tigeot 			if (radeon_connector->router.ddc_valid)
626926deccbSFrançois Tigeot 				DRM_INFO("  DDC Router 0x%x/0x%x\n",
627926deccbSFrançois Tigeot 					 radeon_connector->router.ddc_mux_control_pin,
628926deccbSFrançois Tigeot 					 radeon_connector->router.ddc_mux_state);
629926deccbSFrançois Tigeot 			if (radeon_connector->router.cd_valid)
630926deccbSFrançois Tigeot 				DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
631926deccbSFrançois Tigeot 					 radeon_connector->router.cd_mux_control_pin,
632926deccbSFrançois Tigeot 					 radeon_connector->router.cd_mux_state);
633926deccbSFrançois Tigeot 		} else {
634926deccbSFrançois Tigeot 			if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
635926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
636926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
637926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
638926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
639926deccbSFrançois Tigeot 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
640926deccbSFrançois Tigeot 				DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
641926deccbSFrançois Tigeot 		}
642926deccbSFrançois Tigeot 		DRM_INFO("  Encoders:\n");
643926deccbSFrançois Tigeot 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
644926deccbSFrançois Tigeot 			radeon_encoder = to_radeon_encoder(encoder);
645926deccbSFrançois Tigeot 			devices = radeon_encoder->devices & radeon_connector->devices;
646926deccbSFrançois Tigeot 			if (devices) {
647926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CRT1_SUPPORT)
648926deccbSFrançois Tigeot 					DRM_INFO("    CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
649926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CRT2_SUPPORT)
650926deccbSFrançois Tigeot 					DRM_INFO("    CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
651926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_LCD1_SUPPORT)
652926deccbSFrançois Tigeot 					DRM_INFO("    LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
653926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP1_SUPPORT)
654926deccbSFrançois Tigeot 					DRM_INFO("    DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
655926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP2_SUPPORT)
656926deccbSFrançois Tigeot 					DRM_INFO("    DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
657926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP3_SUPPORT)
658926deccbSFrançois Tigeot 					DRM_INFO("    DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
659926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP4_SUPPORT)
660926deccbSFrançois Tigeot 					DRM_INFO("    DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
661926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP5_SUPPORT)
662926deccbSFrançois Tigeot 					DRM_INFO("    DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
663926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_DFP6_SUPPORT)
664926deccbSFrançois Tigeot 					DRM_INFO("    DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
665926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_TV1_SUPPORT)
666926deccbSFrançois Tigeot 					DRM_INFO("    TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
667926deccbSFrançois Tigeot 				if (devices & ATOM_DEVICE_CV_SUPPORT)
668926deccbSFrançois Tigeot 					DRM_INFO("    CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
669926deccbSFrançois Tigeot 			}
670926deccbSFrançois Tigeot 		}
671926deccbSFrançois Tigeot 		i++;
672926deccbSFrançois Tigeot 	}
673926deccbSFrançois Tigeot }
674926deccbSFrançois Tigeot 
675926deccbSFrançois Tigeot static bool radeon_setup_enc_conn(struct drm_device *dev)
676926deccbSFrançois Tigeot {
677926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
678926deccbSFrançois Tigeot 	bool ret = false;
679926deccbSFrançois Tigeot 
680926deccbSFrançois Tigeot 	if (rdev->bios) {
681926deccbSFrançois Tigeot 		if (rdev->is_atom_bios) {
682926deccbSFrançois Tigeot 			ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
683926deccbSFrançois Tigeot 			if (ret == false)
684926deccbSFrançois Tigeot 				ret = radeon_get_atom_connector_info_from_object_table(dev);
685926deccbSFrançois Tigeot 		} else {
686926deccbSFrançois Tigeot 			ret = radeon_get_legacy_connector_info_from_bios(dev);
687926deccbSFrançois Tigeot 			if (ret == false)
688926deccbSFrançois Tigeot 				ret = radeon_get_legacy_connector_info_from_table(dev);
689926deccbSFrançois Tigeot 		}
690926deccbSFrançois Tigeot 	} else {
691926deccbSFrançois Tigeot 		if (!ASIC_IS_AVIVO(rdev))
692926deccbSFrançois Tigeot 			ret = radeon_get_legacy_connector_info_from_table(dev);
693926deccbSFrançois Tigeot 	}
694926deccbSFrançois Tigeot 	if (ret) {
695926deccbSFrançois Tigeot 		radeon_setup_encoder_clones(dev);
696926deccbSFrançois Tigeot 		radeon_print_display_setup(dev);
697926deccbSFrançois Tigeot 	}
698926deccbSFrançois Tigeot 
699926deccbSFrançois Tigeot 	return ret;
700926deccbSFrançois Tigeot }
701926deccbSFrançois Tigeot 
702926deccbSFrançois Tigeot int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
703926deccbSFrançois Tigeot {
704926deccbSFrançois Tigeot 	struct drm_device *dev = radeon_connector->base.dev;
705926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
706926deccbSFrançois Tigeot 	int ret = 0;
707926deccbSFrançois Tigeot 
708926deccbSFrançois Tigeot 	/* on hw with routers, select right port */
709926deccbSFrançois Tigeot 	if (radeon_connector->router.ddc_valid)
710926deccbSFrançois Tigeot 		radeon_router_select_ddc_port(radeon_connector);
711926deccbSFrançois Tigeot 
712926deccbSFrançois Tigeot 	if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
713926deccbSFrançois Tigeot 	    ENCODER_OBJECT_ID_NONE) {
714926deccbSFrançois Tigeot 		struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
715926deccbSFrançois Tigeot 
716926deccbSFrançois Tigeot 		if (dig->dp_i2c_bus)
717926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
718926deccbSFrançois Tigeot 							      dig->dp_i2c_bus->adapter);
719926deccbSFrançois Tigeot 	} else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
720926deccbSFrançois Tigeot 		   (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
721926deccbSFrançois Tigeot 		struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
722926deccbSFrançois Tigeot 
723926deccbSFrançois Tigeot 		if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
724926deccbSFrançois Tigeot 		     dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
725926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
726926deccbSFrançois Tigeot 							      dig->dp_i2c_bus->adapter);
727926deccbSFrançois Tigeot 		else if (radeon_connector->ddc_bus && !radeon_connector->edid)
728926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
729926deccbSFrançois Tigeot 							      radeon_connector->ddc_bus->adapter);
730926deccbSFrançois Tigeot 	} else {
731926deccbSFrançois Tigeot 		if (radeon_connector->ddc_bus && !radeon_connector->edid)
732926deccbSFrançois Tigeot 			radeon_connector->edid = drm_get_edid(&radeon_connector->base,
733926deccbSFrançois Tigeot 							      radeon_connector->ddc_bus->adapter);
734926deccbSFrançois Tigeot 	}
735926deccbSFrançois Tigeot 
736926deccbSFrançois Tigeot 	if (!radeon_connector->edid) {
737926deccbSFrançois Tigeot 		if (rdev->is_atom_bios) {
738926deccbSFrançois Tigeot 			/* some laptops provide a hardcoded edid in rom for LCDs */
739926deccbSFrançois Tigeot 			if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
740926deccbSFrançois Tigeot 			     (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
741926deccbSFrançois Tigeot 				radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
742926deccbSFrançois Tigeot 		} else
743926deccbSFrançois Tigeot 			/* some servers provide a hardcoded edid in rom for KVMs */
744926deccbSFrançois Tigeot 			radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
745926deccbSFrançois Tigeot 	}
746926deccbSFrançois Tigeot 	if (radeon_connector->edid) {
747926deccbSFrançois Tigeot 		drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
748926deccbSFrançois Tigeot 		ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
749926deccbSFrançois Tigeot 		return ret;
750926deccbSFrançois Tigeot 	}
751926deccbSFrançois Tigeot 	drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
752926deccbSFrançois Tigeot 	return 0;
753926deccbSFrançois Tigeot }
754926deccbSFrançois Tigeot 
755926deccbSFrançois Tigeot /* avivo */
756926deccbSFrançois Tigeot static void avivo_get_fb_div(struct radeon_pll *pll,
757926deccbSFrançois Tigeot 			     u32 target_clock,
758926deccbSFrançois Tigeot 			     u32 post_div,
759926deccbSFrançois Tigeot 			     u32 ref_div,
760926deccbSFrançois Tigeot 			     u32 *fb_div,
761926deccbSFrançois Tigeot 			     u32 *frac_fb_div)
762926deccbSFrançois Tigeot {
763926deccbSFrançois Tigeot 	u32 tmp = post_div * ref_div;
764926deccbSFrançois Tigeot 
765926deccbSFrançois Tigeot 	tmp *= target_clock;
766926deccbSFrançois Tigeot 	*fb_div = tmp / pll->reference_freq;
767926deccbSFrançois Tigeot 	*frac_fb_div = tmp % pll->reference_freq;
768926deccbSFrançois Tigeot 
769926deccbSFrançois Tigeot         if (*fb_div > pll->max_feedback_div)
770926deccbSFrançois Tigeot 		*fb_div = pll->max_feedback_div;
771926deccbSFrançois Tigeot         else if (*fb_div < pll->min_feedback_div)
772926deccbSFrançois Tigeot                 *fb_div = pll->min_feedback_div;
773926deccbSFrançois Tigeot }
774926deccbSFrançois Tigeot 
775926deccbSFrançois Tigeot static u32 avivo_get_post_div(struct radeon_pll *pll,
776926deccbSFrançois Tigeot 			      u32 target_clock)
777926deccbSFrançois Tigeot {
778926deccbSFrançois Tigeot 	u32 vco, post_div, tmp;
779926deccbSFrançois Tigeot 
780926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_POST_DIV)
781926deccbSFrançois Tigeot 		return pll->post_div;
782926deccbSFrançois Tigeot 
783926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
784926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_IS_LCD)
785926deccbSFrançois Tigeot 			vco = pll->lcd_pll_out_min;
786926deccbSFrançois Tigeot 		else
787926deccbSFrançois Tigeot 			vco = pll->pll_out_min;
788926deccbSFrançois Tigeot 	} else {
789926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_IS_LCD)
790926deccbSFrançois Tigeot 			vco = pll->lcd_pll_out_max;
791926deccbSFrançois Tigeot 		else
792926deccbSFrançois Tigeot 			vco = pll->pll_out_max;
793926deccbSFrançois Tigeot 	}
794926deccbSFrançois Tigeot 
795926deccbSFrançois Tigeot 	post_div = vco / target_clock;
796926deccbSFrançois Tigeot 	tmp = vco % target_clock;
797926deccbSFrançois Tigeot 
798926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
799926deccbSFrançois Tigeot 		if (tmp)
800926deccbSFrançois Tigeot 			post_div++;
801926deccbSFrançois Tigeot 	} else {
802926deccbSFrançois Tigeot 		if (!tmp)
803926deccbSFrançois Tigeot 			post_div--;
804926deccbSFrançois Tigeot 	}
805926deccbSFrançois Tigeot 
806926deccbSFrançois Tigeot 	if (post_div > pll->max_post_div)
807926deccbSFrançois Tigeot 		post_div = pll->max_post_div;
808926deccbSFrançois Tigeot 	else if (post_div < pll->min_post_div)
809926deccbSFrançois Tigeot 		post_div = pll->min_post_div;
810926deccbSFrançois Tigeot 
811926deccbSFrançois Tigeot 	return post_div;
812926deccbSFrançois Tigeot }
813926deccbSFrançois Tigeot 
814926deccbSFrançois Tigeot #define MAX_TOLERANCE 10
815926deccbSFrançois Tigeot 
816926deccbSFrançois Tigeot void radeon_compute_pll_avivo(struct radeon_pll *pll,
817926deccbSFrançois Tigeot 			      u32 freq,
818926deccbSFrançois Tigeot 			      u32 *dot_clock_p,
819926deccbSFrançois Tigeot 			      u32 *fb_div_p,
820926deccbSFrançois Tigeot 			      u32 *frac_fb_div_p,
821926deccbSFrançois Tigeot 			      u32 *ref_div_p,
822926deccbSFrançois Tigeot 			      u32 *post_div_p)
823926deccbSFrançois Tigeot {
824926deccbSFrançois Tigeot 	u32 target_clock = freq / 10;
825926deccbSFrançois Tigeot 	u32 post_div = avivo_get_post_div(pll, target_clock);
826926deccbSFrançois Tigeot 	u32 ref_div = pll->min_ref_div;
827926deccbSFrançois Tigeot 	u32 fb_div = 0, frac_fb_div = 0, tmp;
828926deccbSFrançois Tigeot 
829926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
830926deccbSFrançois Tigeot 		ref_div = pll->reference_div;
831926deccbSFrançois Tigeot 
832926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
833926deccbSFrançois Tigeot 		avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
834926deccbSFrançois Tigeot 		frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
835926deccbSFrançois Tigeot 		if (frac_fb_div >= 5) {
836926deccbSFrançois Tigeot 			frac_fb_div -= 5;
837926deccbSFrançois Tigeot 			frac_fb_div = frac_fb_div / 10;
838926deccbSFrançois Tigeot 			frac_fb_div++;
839926deccbSFrançois Tigeot 		}
840926deccbSFrançois Tigeot 		if (frac_fb_div >= 10) {
841926deccbSFrançois Tigeot 			fb_div++;
842926deccbSFrançois Tigeot 			frac_fb_div = 0;
843926deccbSFrançois Tigeot 		}
844926deccbSFrançois Tigeot 	} else {
845926deccbSFrançois Tigeot 		while (ref_div <= pll->max_ref_div) {
846926deccbSFrançois Tigeot 			avivo_get_fb_div(pll, target_clock, post_div, ref_div,
847926deccbSFrançois Tigeot 					 &fb_div, &frac_fb_div);
848926deccbSFrançois Tigeot 			if (frac_fb_div >= (pll->reference_freq / 2))
849926deccbSFrançois Tigeot 				fb_div++;
850926deccbSFrançois Tigeot 			frac_fb_div = 0;
851926deccbSFrançois Tigeot 			tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
852926deccbSFrançois Tigeot 			tmp = (tmp * 10000) / target_clock;
853926deccbSFrançois Tigeot 
854926deccbSFrançois Tigeot 			if (tmp > (10000 + MAX_TOLERANCE))
855926deccbSFrançois Tigeot 				ref_div++;
856926deccbSFrançois Tigeot 			else if (tmp >= (10000 - MAX_TOLERANCE))
857926deccbSFrançois Tigeot 				break;
858926deccbSFrançois Tigeot 			else
859926deccbSFrançois Tigeot 				ref_div++;
860926deccbSFrançois Tigeot 		}
861926deccbSFrançois Tigeot 	}
862926deccbSFrançois Tigeot 
863926deccbSFrançois Tigeot 	*dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
864926deccbSFrançois Tigeot 		(ref_div * post_div * 10);
865926deccbSFrançois Tigeot 	*fb_div_p = fb_div;
866926deccbSFrançois Tigeot 	*frac_fb_div_p = frac_fb_div;
867926deccbSFrançois Tigeot 	*ref_div_p = ref_div;
868926deccbSFrançois Tigeot 	*post_div_p = post_div;
869926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
870926deccbSFrançois Tigeot 		      *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
871926deccbSFrançois Tigeot }
872926deccbSFrançois Tigeot 
873926deccbSFrançois Tigeot /* pre-avivo */
874926deccbSFrançois Tigeot static inline uint32_t radeon_div(uint64_t n, uint32_t d)
875926deccbSFrançois Tigeot {
876926deccbSFrançois Tigeot 	uint64_t mod;
877926deccbSFrançois Tigeot 
878926deccbSFrançois Tigeot 	n += d / 2;
879926deccbSFrançois Tigeot 
880926deccbSFrançois Tigeot 	mod = do_div(n, d);
881926deccbSFrançois Tigeot 	return n;
882926deccbSFrançois Tigeot }
883926deccbSFrançois Tigeot 
884926deccbSFrançois Tigeot void radeon_compute_pll_legacy(struct radeon_pll *pll,
885926deccbSFrançois Tigeot 			       uint64_t freq,
886926deccbSFrançois Tigeot 			       uint32_t *dot_clock_p,
887926deccbSFrançois Tigeot 			       uint32_t *fb_div_p,
888926deccbSFrançois Tigeot 			       uint32_t *frac_fb_div_p,
889926deccbSFrançois Tigeot 			       uint32_t *ref_div_p,
890926deccbSFrançois Tigeot 			       uint32_t *post_div_p)
891926deccbSFrançois Tigeot {
892926deccbSFrançois Tigeot 	uint32_t min_ref_div = pll->min_ref_div;
893926deccbSFrançois Tigeot 	uint32_t max_ref_div = pll->max_ref_div;
894926deccbSFrançois Tigeot 	uint32_t min_post_div = pll->min_post_div;
895926deccbSFrançois Tigeot 	uint32_t max_post_div = pll->max_post_div;
896926deccbSFrançois Tigeot 	uint32_t min_fractional_feed_div = 0;
897926deccbSFrançois Tigeot 	uint32_t max_fractional_feed_div = 0;
898926deccbSFrançois Tigeot 	uint32_t best_vco = pll->best_vco;
899926deccbSFrançois Tigeot 	uint32_t best_post_div = 1;
900926deccbSFrançois Tigeot 	uint32_t best_ref_div = 1;
901926deccbSFrançois Tigeot 	uint32_t best_feedback_div = 1;
902926deccbSFrançois Tigeot 	uint32_t best_frac_feedback_div = 0;
903926deccbSFrançois Tigeot 	uint32_t best_freq = -1;
904926deccbSFrançois Tigeot 	uint32_t best_error = 0xffffffff;
905926deccbSFrançois Tigeot 	uint32_t best_vco_diff = 1;
906926deccbSFrançois Tigeot 	uint32_t post_div;
907926deccbSFrançois Tigeot 	u32 pll_out_min, pll_out_max;
908926deccbSFrançois Tigeot 
909926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("PLL freq %ju %u %u\n", (uintmax_t)freq, pll->min_ref_div, pll->max_ref_div);
910926deccbSFrançois Tigeot 	freq = freq * 1000;
911926deccbSFrançois Tigeot 
912926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_IS_LCD) {
913926deccbSFrançois Tigeot 		pll_out_min = pll->lcd_pll_out_min;
914926deccbSFrançois Tigeot 		pll_out_max = pll->lcd_pll_out_max;
915926deccbSFrançois Tigeot 	} else {
916926deccbSFrançois Tigeot 		pll_out_min = pll->pll_out_min;
917926deccbSFrançois Tigeot 		pll_out_max = pll->pll_out_max;
918926deccbSFrançois Tigeot 	}
919926deccbSFrançois Tigeot 
920926deccbSFrançois Tigeot 	if (pll_out_min > 64800)
921926deccbSFrançois Tigeot 		pll_out_min = 64800;
922926deccbSFrançois Tigeot 
923926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_REF_DIV)
924926deccbSFrançois Tigeot 		min_ref_div = max_ref_div = pll->reference_div;
925926deccbSFrançois Tigeot 	else {
926926deccbSFrançois Tigeot 		while (min_ref_div < max_ref_div-1) {
927926deccbSFrançois Tigeot 			uint32_t mid = (min_ref_div + max_ref_div) / 2;
928926deccbSFrançois Tigeot 			uint32_t pll_in = pll->reference_freq / mid;
929926deccbSFrançois Tigeot 			if (pll_in < pll->pll_in_min)
930926deccbSFrançois Tigeot 				max_ref_div = mid;
931926deccbSFrançois Tigeot 			else if (pll_in > pll->pll_in_max)
932926deccbSFrançois Tigeot 				min_ref_div = mid;
933926deccbSFrançois Tigeot 			else
934926deccbSFrançois Tigeot 				break;
935926deccbSFrançois Tigeot 		}
936926deccbSFrançois Tigeot 	}
937926deccbSFrançois Tigeot 
938926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_POST_DIV)
939926deccbSFrançois Tigeot 		min_post_div = max_post_div = pll->post_div;
940926deccbSFrançois Tigeot 
941926deccbSFrançois Tigeot 	if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
942926deccbSFrançois Tigeot 		min_fractional_feed_div = pll->min_frac_feedback_div;
943926deccbSFrançois Tigeot 		max_fractional_feed_div = pll->max_frac_feedback_div;
944926deccbSFrançois Tigeot 	}
945926deccbSFrançois Tigeot 
946926deccbSFrançois Tigeot 	for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
947926deccbSFrançois Tigeot 		uint32_t ref_div;
948926deccbSFrançois Tigeot 
949926deccbSFrançois Tigeot 		if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
950926deccbSFrançois Tigeot 			continue;
951926deccbSFrançois Tigeot 
952926deccbSFrançois Tigeot 		/* legacy radeons only have a few post_divs */
953926deccbSFrançois Tigeot 		if (pll->flags & RADEON_PLL_LEGACY) {
954926deccbSFrançois Tigeot 			if ((post_div == 5) ||
955926deccbSFrançois Tigeot 			    (post_div == 7) ||
956926deccbSFrançois Tigeot 			    (post_div == 9) ||
957926deccbSFrançois Tigeot 			    (post_div == 10) ||
958926deccbSFrançois Tigeot 			    (post_div == 11) ||
959926deccbSFrançois Tigeot 			    (post_div == 13) ||
960926deccbSFrançois Tigeot 			    (post_div == 14) ||
961926deccbSFrançois Tigeot 			    (post_div == 15))
962926deccbSFrançois Tigeot 				continue;
963926deccbSFrançois Tigeot 		}
964926deccbSFrançois Tigeot 
965926deccbSFrançois Tigeot 		for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
966926deccbSFrançois Tigeot 			uint32_t feedback_div, current_freq = 0, error, vco_diff;
967926deccbSFrançois Tigeot 			uint32_t pll_in = pll->reference_freq / ref_div;
968926deccbSFrançois Tigeot 			uint32_t min_feed_div = pll->min_feedback_div;
969926deccbSFrançois Tigeot 			uint32_t max_feed_div = pll->max_feedback_div + 1;
970926deccbSFrançois Tigeot 
971926deccbSFrançois Tigeot 			if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
972926deccbSFrançois Tigeot 				continue;
973926deccbSFrançois Tigeot 
974926deccbSFrançois Tigeot 			while (min_feed_div < max_feed_div) {
975926deccbSFrançois Tigeot 				uint32_t vco;
976926deccbSFrançois Tigeot 				uint32_t min_frac_feed_div = min_fractional_feed_div;
977926deccbSFrançois Tigeot 				uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
978926deccbSFrançois Tigeot 				uint32_t frac_feedback_div;
979926deccbSFrançois Tigeot 				uint64_t tmp;
980926deccbSFrançois Tigeot 
981926deccbSFrançois Tigeot 				feedback_div = (min_feed_div + max_feed_div) / 2;
982926deccbSFrançois Tigeot 
983926deccbSFrançois Tigeot 				tmp = (uint64_t)pll->reference_freq * feedback_div;
984926deccbSFrançois Tigeot 				vco = radeon_div(tmp, ref_div);
985926deccbSFrançois Tigeot 
986926deccbSFrançois Tigeot 				if (vco < pll_out_min) {
987926deccbSFrançois Tigeot 					min_feed_div = feedback_div + 1;
988926deccbSFrançois Tigeot 					continue;
989926deccbSFrançois Tigeot 				} else if (vco > pll_out_max) {
990926deccbSFrançois Tigeot 					max_feed_div = feedback_div;
991926deccbSFrançois Tigeot 					continue;
992926deccbSFrançois Tigeot 				}
993926deccbSFrançois Tigeot 
994926deccbSFrançois Tigeot 				while (min_frac_feed_div < max_frac_feed_div) {
995926deccbSFrançois Tigeot 					frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
996926deccbSFrançois Tigeot 					tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
997926deccbSFrançois Tigeot 					tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
998926deccbSFrançois Tigeot 					current_freq = radeon_div(tmp, ref_div * post_div);
999926deccbSFrançois Tigeot 
1000926deccbSFrançois Tigeot 					if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
1001926deccbSFrançois Tigeot 						if (freq < current_freq)
1002926deccbSFrançois Tigeot 							error = 0xffffffff;
1003926deccbSFrançois Tigeot 						else
1004926deccbSFrançois Tigeot 							error = freq - current_freq;
1005926deccbSFrançois Tigeot 					} else
1006926deccbSFrançois Tigeot 						error = abs(current_freq - freq);
1007926deccbSFrançois Tigeot 					vco_diff = abs(vco - best_vco);
1008926deccbSFrançois Tigeot 
1009926deccbSFrançois Tigeot 					if ((best_vco == 0 && error < best_error) ||
1010926deccbSFrançois Tigeot 					    (best_vco != 0 &&
1011926deccbSFrançois Tigeot 					     ((best_error > 100 && error < best_error - 100) ||
1012926deccbSFrançois Tigeot 					      (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
1013926deccbSFrançois Tigeot 						best_post_div = post_div;
1014926deccbSFrançois Tigeot 						best_ref_div = ref_div;
1015926deccbSFrançois Tigeot 						best_feedback_div = feedback_div;
1016926deccbSFrançois Tigeot 						best_frac_feedback_div = frac_feedback_div;
1017926deccbSFrançois Tigeot 						best_freq = current_freq;
1018926deccbSFrançois Tigeot 						best_error = error;
1019926deccbSFrançois Tigeot 						best_vco_diff = vco_diff;
1020926deccbSFrançois Tigeot 					} else if (current_freq == freq) {
1021926deccbSFrançois Tigeot 						if (best_freq == -1) {
1022926deccbSFrançois Tigeot 							best_post_div = post_div;
1023926deccbSFrançois Tigeot 							best_ref_div = ref_div;
1024926deccbSFrançois Tigeot 							best_feedback_div = feedback_div;
1025926deccbSFrançois Tigeot 							best_frac_feedback_div = frac_feedback_div;
1026926deccbSFrançois Tigeot 							best_freq = current_freq;
1027926deccbSFrançois Tigeot 							best_error = error;
1028926deccbSFrançois Tigeot 							best_vco_diff = vco_diff;
1029926deccbSFrançois Tigeot 						} else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
1030926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
1031926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
1032926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
1033926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
1034926deccbSFrançois Tigeot 							   ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
1035926deccbSFrançois Tigeot 							best_post_div = post_div;
1036926deccbSFrançois Tigeot 							best_ref_div = ref_div;
1037926deccbSFrançois Tigeot 							best_feedback_div = feedback_div;
1038926deccbSFrançois Tigeot 							best_frac_feedback_div = frac_feedback_div;
1039926deccbSFrançois Tigeot 							best_freq = current_freq;
1040926deccbSFrançois Tigeot 							best_error = error;
1041926deccbSFrançois Tigeot 							best_vco_diff = vco_diff;
1042926deccbSFrançois Tigeot 						}
1043926deccbSFrançois Tigeot 					}
1044926deccbSFrançois Tigeot 					if (current_freq < freq)
1045926deccbSFrançois Tigeot 						min_frac_feed_div = frac_feedback_div + 1;
1046926deccbSFrançois Tigeot 					else
1047926deccbSFrançois Tigeot 						max_frac_feed_div = frac_feedback_div;
1048926deccbSFrançois Tigeot 				}
1049926deccbSFrançois Tigeot 				if (current_freq < freq)
1050926deccbSFrançois Tigeot 					min_feed_div = feedback_div + 1;
1051926deccbSFrançois Tigeot 				else
1052926deccbSFrançois Tigeot 					max_feed_div = feedback_div;
1053926deccbSFrançois Tigeot 			}
1054926deccbSFrançois Tigeot 		}
1055926deccbSFrançois Tigeot 	}
1056926deccbSFrançois Tigeot 
1057926deccbSFrançois Tigeot 	*dot_clock_p = best_freq / 10000;
1058926deccbSFrançois Tigeot 	*fb_div_p = best_feedback_div;
1059926deccbSFrançois Tigeot 	*frac_fb_div_p = best_frac_feedback_div;
1060926deccbSFrançois Tigeot 	*ref_div_p = best_ref_div;
1061926deccbSFrançois Tigeot 	*post_div_p = best_post_div;
1062926deccbSFrançois Tigeot 	DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1063926deccbSFrançois Tigeot 		      (long long)freq,
1064926deccbSFrançois Tigeot 		      best_freq / 1000, best_feedback_div, best_frac_feedback_div,
1065926deccbSFrançois Tigeot 		      best_ref_div, best_post_div);
1066926deccbSFrançois Tigeot 
1067926deccbSFrançois Tigeot }
1068926deccbSFrançois Tigeot 
1069926deccbSFrançois Tigeot static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
1070926deccbSFrançois Tigeot {
1071926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1072926deccbSFrançois Tigeot 
1073926deccbSFrançois Tigeot 	if (radeon_fb->obj) {
1074926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(radeon_fb->obj);
1075926deccbSFrançois Tigeot 	}
1076926deccbSFrançois Tigeot 	drm_framebuffer_cleanup(fb);
10775a3b77d5SFrançois Tigeot 	drm_free(radeon_fb, M_DRM);
1078926deccbSFrançois Tigeot }
1079926deccbSFrançois Tigeot 
1080926deccbSFrançois Tigeot static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1081926deccbSFrançois Tigeot 						  struct drm_file *file_priv,
1082926deccbSFrançois Tigeot 						  unsigned int *handle)
1083926deccbSFrançois Tigeot {
1084926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
1085926deccbSFrançois Tigeot 
1086926deccbSFrançois Tigeot 	return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
1087926deccbSFrançois Tigeot }
1088926deccbSFrançois Tigeot 
1089926deccbSFrançois Tigeot static const struct drm_framebuffer_funcs radeon_fb_funcs = {
1090926deccbSFrançois Tigeot 	.destroy = radeon_user_framebuffer_destroy,
1091926deccbSFrançois Tigeot 	.create_handle = radeon_user_framebuffer_create_handle,
1092926deccbSFrançois Tigeot };
1093926deccbSFrançois Tigeot 
1094926deccbSFrançois Tigeot int
1095926deccbSFrançois Tigeot radeon_framebuffer_init(struct drm_device *dev,
1096926deccbSFrançois Tigeot 			struct radeon_framebuffer *rfb,
1097926deccbSFrançois Tigeot 			struct drm_mode_fb_cmd2 *mode_cmd,
1098926deccbSFrançois Tigeot 			struct drm_gem_object *obj)
1099926deccbSFrançois Tigeot {
1100926deccbSFrançois Tigeot 	int ret;
1101926deccbSFrançois Tigeot 	rfb->obj = obj;
1102b403bed8SMichael Neumann 	drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
1103926deccbSFrançois Tigeot 	ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
1104926deccbSFrançois Tigeot 	if (ret) {
1105926deccbSFrançois Tigeot 		rfb->obj = NULL;
1106926deccbSFrançois Tigeot 		return ret;
1107926deccbSFrançois Tigeot 	}
1108926deccbSFrançois Tigeot 	return 0;
1109926deccbSFrançois Tigeot }
1110926deccbSFrançois Tigeot 
111160fc7eecSFrançois Tigeot static struct drm_framebuffer *
1112926deccbSFrançois Tigeot radeon_user_framebuffer_create(struct drm_device *dev,
1113926deccbSFrançois Tigeot 			       struct drm_file *file_priv,
111460fc7eecSFrançois Tigeot 			       struct drm_mode_fb_cmd2 *mode_cmd)
1115926deccbSFrançois Tigeot {
1116926deccbSFrançois Tigeot 	struct drm_gem_object *obj;
1117926deccbSFrançois Tigeot 	struct radeon_framebuffer *radeon_fb;
1118926deccbSFrançois Tigeot 	int ret;
1119926deccbSFrançois Tigeot 
1120926deccbSFrançois Tigeot 	obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
1121926deccbSFrançois Tigeot 	if (obj ==  NULL) {
11226df74fa7SFrançois Tigeot 		dev_err(dev->dev, "No GEM object associated to handle 0x%08X, "
1123926deccbSFrançois Tigeot 			"can't create framebuffer\n", mode_cmd->handles[0]);
112460fc7eecSFrançois Tigeot 		return ERR_PTR(-ENOENT);
1125926deccbSFrançois Tigeot 	}
1126926deccbSFrançois Tigeot 
11275a3b77d5SFrançois Tigeot 	radeon_fb = kmalloc(sizeof(*radeon_fb), M_DRM,
1128926deccbSFrançois Tigeot 			    M_WAITOK | M_ZERO);
1129926deccbSFrançois Tigeot 	if (radeon_fb == NULL) {
1130926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(obj);
113160fc7eecSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
1132926deccbSFrançois Tigeot 	}
1133926deccbSFrançois Tigeot 
1134926deccbSFrançois Tigeot 	ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
1135926deccbSFrançois Tigeot 	if (ret) {
1136158486a6SFrançois Tigeot 		kfree(radeon_fb);
1137926deccbSFrançois Tigeot 		drm_gem_object_unreference_unlocked(obj);
113860fc7eecSFrançois Tigeot 		return ERR_PTR(ret);
1139926deccbSFrançois Tigeot 	}
1140926deccbSFrançois Tigeot 
114160fc7eecSFrançois Tigeot 	return &radeon_fb->base;
1142926deccbSFrançois Tigeot }
1143926deccbSFrançois Tigeot 
1144926deccbSFrançois Tigeot static void radeon_output_poll_changed(struct drm_device *dev)
1145926deccbSFrançois Tigeot {
1146926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1147926deccbSFrançois Tigeot 	radeon_fb_output_poll_changed(rdev);
1148926deccbSFrançois Tigeot }
1149926deccbSFrançois Tigeot 
1150926deccbSFrançois Tigeot static const struct drm_mode_config_funcs radeon_mode_funcs = {
1151926deccbSFrançois Tigeot 	.fb_create = radeon_user_framebuffer_create,
1152926deccbSFrançois Tigeot 	.output_poll_changed = radeon_output_poll_changed
1153926deccbSFrançois Tigeot };
1154926deccbSFrançois Tigeot 
1155926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
1156926deccbSFrançois Tigeot {	{ 0, "driver" },
1157926deccbSFrançois Tigeot 	{ 1, "bios" },
1158926deccbSFrançois Tigeot };
1159926deccbSFrançois Tigeot 
1160926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
1161926deccbSFrançois Tigeot {	{ TV_STD_NTSC, "ntsc" },
1162926deccbSFrançois Tigeot 	{ TV_STD_PAL, "pal" },
1163926deccbSFrançois Tigeot 	{ TV_STD_PAL_M, "pal-m" },
1164926deccbSFrançois Tigeot 	{ TV_STD_PAL_60, "pal-60" },
1165926deccbSFrançois Tigeot 	{ TV_STD_NTSC_J, "ntsc-j" },
1166926deccbSFrançois Tigeot 	{ TV_STD_SCART_PAL, "scart-pal" },
1167926deccbSFrançois Tigeot 	{ TV_STD_PAL_CN, "pal-cn" },
1168926deccbSFrançois Tigeot 	{ TV_STD_SECAM, "secam" },
1169926deccbSFrançois Tigeot };
1170926deccbSFrançois Tigeot 
1171926deccbSFrançois Tigeot static struct drm_prop_enum_list radeon_underscan_enum_list[] =
1172926deccbSFrançois Tigeot {	{ UNDERSCAN_OFF, "off" },
1173926deccbSFrançois Tigeot 	{ UNDERSCAN_ON, "on" },
1174926deccbSFrançois Tigeot 	{ UNDERSCAN_AUTO, "auto" },
1175926deccbSFrançois Tigeot };
1176926deccbSFrançois Tigeot 
1177926deccbSFrançois Tigeot static int radeon_modeset_create_props(struct radeon_device *rdev)
1178926deccbSFrançois Tigeot {
1179926deccbSFrançois Tigeot 	int sz;
1180926deccbSFrançois Tigeot 
1181926deccbSFrançois Tigeot 	if (rdev->is_atom_bios) {
1182926deccbSFrançois Tigeot 		rdev->mode_info.coherent_mode_property =
1183926deccbSFrançois Tigeot 			drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
1184926deccbSFrançois Tigeot 		if (!rdev->mode_info.coherent_mode_property)
1185926deccbSFrançois Tigeot 			return -ENOMEM;
1186926deccbSFrançois Tigeot 	}
1187926deccbSFrançois Tigeot 
1188926deccbSFrançois Tigeot 	if (!ASIC_IS_AVIVO(rdev)) {
1189926deccbSFrançois Tigeot 		sz = DRM_ARRAY_SIZE(radeon_tmds_pll_enum_list);
1190926deccbSFrançois Tigeot 		rdev->mode_info.tmds_pll_property =
1191926deccbSFrançois Tigeot 			drm_property_create_enum(rdev->ddev, 0,
1192926deccbSFrançois Tigeot 					    "tmds_pll",
1193926deccbSFrançois Tigeot 					    radeon_tmds_pll_enum_list, sz);
1194926deccbSFrançois Tigeot 	}
1195926deccbSFrançois Tigeot 
1196926deccbSFrançois Tigeot 	rdev->mode_info.load_detect_property =
1197926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
1198926deccbSFrançois Tigeot 	if (!rdev->mode_info.load_detect_property)
1199926deccbSFrançois Tigeot 		return -ENOMEM;
1200926deccbSFrançois Tigeot 
1201926deccbSFrançois Tigeot 	drm_mode_create_scaling_mode_property(rdev->ddev);
1202926deccbSFrançois Tigeot 
1203926deccbSFrançois Tigeot 	sz = DRM_ARRAY_SIZE(radeon_tv_std_enum_list);
1204926deccbSFrançois Tigeot 	rdev->mode_info.tv_std_property =
1205926deccbSFrançois Tigeot 		drm_property_create_enum(rdev->ddev, 0,
1206926deccbSFrançois Tigeot 				    "tv standard",
1207926deccbSFrançois Tigeot 				    radeon_tv_std_enum_list, sz);
1208926deccbSFrançois Tigeot 
1209926deccbSFrançois Tigeot 	sz = DRM_ARRAY_SIZE(radeon_underscan_enum_list);
1210926deccbSFrançois Tigeot 	rdev->mode_info.underscan_property =
1211926deccbSFrançois Tigeot 		drm_property_create_enum(rdev->ddev, 0,
1212926deccbSFrançois Tigeot 				    "underscan",
1213926deccbSFrançois Tigeot 				    radeon_underscan_enum_list, sz);
1214926deccbSFrançois Tigeot 
1215926deccbSFrançois Tigeot 	rdev->mode_info.underscan_hborder_property =
1216926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0,
1217926deccbSFrançois Tigeot 					"underscan hborder", 0, 128);
1218926deccbSFrançois Tigeot 	if (!rdev->mode_info.underscan_hborder_property)
1219926deccbSFrançois Tigeot 		return -ENOMEM;
1220926deccbSFrançois Tigeot 
1221926deccbSFrançois Tigeot 	rdev->mode_info.underscan_vborder_property =
1222926deccbSFrançois Tigeot 		drm_property_create_range(rdev->ddev, 0,
1223926deccbSFrançois Tigeot 					"underscan vborder", 0, 128);
1224926deccbSFrançois Tigeot 	if (!rdev->mode_info.underscan_vborder_property)
1225926deccbSFrançois Tigeot 		return -ENOMEM;
1226926deccbSFrançois Tigeot 
1227926deccbSFrançois Tigeot 	return 0;
1228926deccbSFrançois Tigeot }
1229926deccbSFrançois Tigeot 
1230926deccbSFrançois Tigeot void radeon_update_display_priority(struct radeon_device *rdev)
1231926deccbSFrançois Tigeot {
1232926deccbSFrançois Tigeot 	/* adjustment options for the display watermarks */
1233926deccbSFrançois Tigeot 	if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1234926deccbSFrançois Tigeot 		/* set display priority to high for r3xx, rv515 chips
1235926deccbSFrançois Tigeot 		 * this avoids flickering due to underflow to the
1236926deccbSFrançois Tigeot 		 * display controllers during heavy acceleration.
1237926deccbSFrançois Tigeot 		 * Don't force high on rs4xx igp chips as it seems to
1238926deccbSFrançois Tigeot 		 * affect the sound card.  See kernel bug 15982.
1239926deccbSFrançois Tigeot 		 */
1240926deccbSFrançois Tigeot 		if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1241926deccbSFrançois Tigeot 		    !(rdev->flags & RADEON_IS_IGP))
1242926deccbSFrançois Tigeot 			rdev->disp_priority = 2;
1243926deccbSFrançois Tigeot 		else
1244926deccbSFrançois Tigeot 			rdev->disp_priority = 0;
1245926deccbSFrançois Tigeot 	} else
1246926deccbSFrançois Tigeot 		rdev->disp_priority = radeon_disp_priority;
1247926deccbSFrançois Tigeot 
1248926deccbSFrançois Tigeot }
1249926deccbSFrançois Tigeot 
1250926deccbSFrançois Tigeot /*
1251926deccbSFrançois Tigeot  * Allocate hdmi structs and determine register offsets
1252926deccbSFrançois Tigeot  */
1253926deccbSFrançois Tigeot static void radeon_afmt_init(struct radeon_device *rdev)
1254926deccbSFrançois Tigeot {
1255926deccbSFrançois Tigeot 	int i;
1256926deccbSFrançois Tigeot 
1257926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
1258926deccbSFrançois Tigeot 		rdev->mode_info.afmt[i] = NULL;
1259926deccbSFrançois Tigeot 
1260926deccbSFrançois Tigeot 	if (ASIC_IS_DCE6(rdev)) {
1261926deccbSFrançois Tigeot 		/* todo */
1262926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE4(rdev)) {
1263926deccbSFrançois Tigeot 		/* DCE4/5 has 6 audio blocks tied to DIG encoders */
1264926deccbSFrançois Tigeot 		/* DCE4.1 has 2 audio blocks tied to DIG encoders */
1265926deccbSFrançois Tigeot 		rdev->mode_info.afmt[0] = kmalloc(sizeof(struct radeon_afmt),
12665a3b77d5SFrançois Tigeot 						  M_DRM,
1267926deccbSFrançois Tigeot 						  M_WAITOK | M_ZERO);
1268926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1269926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
1270926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1271926deccbSFrançois Tigeot 		}
1272926deccbSFrançois Tigeot 		rdev->mode_info.afmt[1] = kmalloc(sizeof(struct radeon_afmt),
12735a3b77d5SFrançois Tigeot 						  M_DRM,
1274926deccbSFrançois Tigeot 						  M_WAITOK | M_ZERO);
1275926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[1]) {
1276926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
1277926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->id = 1;
1278926deccbSFrançois Tigeot 		}
1279926deccbSFrançois Tigeot 		if (!ASIC_IS_DCE41(rdev)) {
1280926deccbSFrançois Tigeot 			rdev->mode_info.afmt[2] = kmalloc(sizeof(struct radeon_afmt),
12815a3b77d5SFrançois Tigeot 							  M_DRM,
1282926deccbSFrançois Tigeot 							  M_WAITOK | M_ZERO);
1283926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[2]) {
1284926deccbSFrançois Tigeot 				rdev->mode_info.afmt[2]->offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
1285926deccbSFrançois Tigeot 				rdev->mode_info.afmt[2]->id = 2;
1286926deccbSFrançois Tigeot 			}
1287926deccbSFrançois Tigeot 			rdev->mode_info.afmt[3] = kmalloc(sizeof(struct radeon_afmt),
12885a3b77d5SFrançois Tigeot 							  M_DRM,
1289926deccbSFrançois Tigeot 							  M_WAITOK | M_ZERO);
1290926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[3]) {
1291926deccbSFrançois Tigeot 				rdev->mode_info.afmt[3]->offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
1292926deccbSFrançois Tigeot 				rdev->mode_info.afmt[3]->id = 3;
1293926deccbSFrançois Tigeot 			}
1294926deccbSFrançois Tigeot 			rdev->mode_info.afmt[4] = kmalloc(sizeof(struct radeon_afmt),
12955a3b77d5SFrançois Tigeot 							  M_DRM,
1296926deccbSFrançois Tigeot 							  M_WAITOK | M_ZERO);
1297926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[4]) {
1298926deccbSFrançois Tigeot 				rdev->mode_info.afmt[4]->offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
1299926deccbSFrançois Tigeot 				rdev->mode_info.afmt[4]->id = 4;
1300926deccbSFrançois Tigeot 			}
1301926deccbSFrançois Tigeot 			rdev->mode_info.afmt[5] = kmalloc(sizeof(struct radeon_afmt),
13025a3b77d5SFrançois Tigeot 							  M_DRM,
1303926deccbSFrançois Tigeot 							  M_WAITOK | M_ZERO);
1304926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[5]) {
1305926deccbSFrançois Tigeot 				rdev->mode_info.afmt[5]->offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
1306926deccbSFrançois Tigeot 				rdev->mode_info.afmt[5]->id = 5;
1307926deccbSFrançois Tigeot 			}
1308926deccbSFrançois Tigeot 		}
1309926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE3(rdev)) {
1310926deccbSFrançois Tigeot 		/* DCE3.x has 2 audio blocks tied to DIG encoders */
1311926deccbSFrançois Tigeot 		rdev->mode_info.afmt[0] = kmalloc(sizeof(struct radeon_afmt),
13125a3b77d5SFrançois Tigeot 						  M_DRM,
1313926deccbSFrançois Tigeot 						  M_WAITOK | M_ZERO);
1314926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1315926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
1316926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1317926deccbSFrançois Tigeot 		}
1318926deccbSFrançois Tigeot 		rdev->mode_info.afmt[1] = kmalloc(sizeof(struct radeon_afmt),
13195a3b77d5SFrançois Tigeot 						  M_DRM,
1320926deccbSFrançois Tigeot 						  M_WAITOK | M_ZERO);
1321926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[1]) {
1322926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
1323926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1]->id = 1;
1324926deccbSFrançois Tigeot 		}
1325926deccbSFrançois Tigeot 	} else if (ASIC_IS_DCE2(rdev)) {
1326926deccbSFrançois Tigeot 		/* DCE2 has at least 1 routable audio block */
1327926deccbSFrançois Tigeot 		rdev->mode_info.afmt[0] = kmalloc(sizeof(struct radeon_afmt),
13285a3b77d5SFrançois Tigeot 						  M_DRM,
1329926deccbSFrançois Tigeot 						  M_WAITOK | M_ZERO);
1330926deccbSFrançois Tigeot 		if (rdev->mode_info.afmt[0]) {
1331926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
1332926deccbSFrançois Tigeot 			rdev->mode_info.afmt[0]->id = 0;
1333926deccbSFrançois Tigeot 		}
1334926deccbSFrançois Tigeot 		/* r6xx has 2 routable audio blocks */
1335926deccbSFrançois Tigeot 		if (rdev->family >= CHIP_R600) {
1336926deccbSFrançois Tigeot 			rdev->mode_info.afmt[1] = kmalloc(sizeof(struct radeon_afmt),
13375a3b77d5SFrançois Tigeot 							  M_DRM,
1338926deccbSFrançois Tigeot 							  M_WAITOK | M_ZERO);
1339926deccbSFrançois Tigeot 			if (rdev->mode_info.afmt[1]) {
1340926deccbSFrançois Tigeot 				rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
1341926deccbSFrançois Tigeot 				rdev->mode_info.afmt[1]->id = 1;
1342926deccbSFrançois Tigeot 			}
1343926deccbSFrançois Tigeot 		}
1344926deccbSFrançois Tigeot 	}
1345926deccbSFrançois Tigeot }
1346926deccbSFrançois Tigeot 
1347926deccbSFrançois Tigeot static void radeon_afmt_fini(struct radeon_device *rdev)
1348926deccbSFrançois Tigeot {
1349926deccbSFrançois Tigeot 	int i;
1350926deccbSFrançois Tigeot 
1351926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
13525a3b77d5SFrançois Tigeot 		drm_free(rdev->mode_info.afmt[i], M_DRM);
1353926deccbSFrançois Tigeot 		rdev->mode_info.afmt[i] = NULL;
1354926deccbSFrançois Tigeot 	}
1355926deccbSFrançois Tigeot }
1356926deccbSFrançois Tigeot 
1357926deccbSFrançois Tigeot int radeon_modeset_init(struct radeon_device *rdev)
1358926deccbSFrançois Tigeot {
1359926deccbSFrançois Tigeot 	int i;
1360926deccbSFrançois Tigeot 	int ret;
1361926deccbSFrançois Tigeot 
1362926deccbSFrançois Tigeot 	drm_mode_config_init(rdev->ddev);
1363926deccbSFrançois Tigeot 	rdev->mode_info.mode_config_initialized = true;
1364926deccbSFrançois Tigeot 
1365926deccbSFrançois Tigeot 	rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
1366926deccbSFrançois Tigeot 
1367926deccbSFrançois Tigeot 	if (ASIC_IS_DCE5(rdev)) {
1368926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 16384;
1369926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 16384;
1370926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
1371926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 8192;
1372926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 8192;
1373926deccbSFrançois Tigeot 	} else {
1374926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_width = 4096;
1375926deccbSFrançois Tigeot 		rdev->ddev->mode_config.max_height = 4096;
1376926deccbSFrançois Tigeot 	}
1377926deccbSFrançois Tigeot 
1378926deccbSFrançois Tigeot 	rdev->ddev->mode_config.preferred_depth = 24;
1379926deccbSFrançois Tigeot 	rdev->ddev->mode_config.prefer_shadow = 1;
1380926deccbSFrançois Tigeot 
1381926deccbSFrançois Tigeot 	rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1382926deccbSFrançois Tigeot 
1383926deccbSFrançois Tigeot 	ret = radeon_modeset_create_props(rdev);
1384926deccbSFrançois Tigeot 	if (ret) {
1385926deccbSFrançois Tigeot 		return ret;
1386926deccbSFrançois Tigeot 	}
1387926deccbSFrançois Tigeot 
1388926deccbSFrançois Tigeot 	/* init i2c buses */
1389926deccbSFrançois Tigeot 	radeon_i2c_init(rdev);
1390926deccbSFrançois Tigeot 
1391926deccbSFrançois Tigeot 	/* check combios for a valid hardcoded EDID - Sun servers */
1392926deccbSFrançois Tigeot 	if (!rdev->is_atom_bios) {
1393926deccbSFrançois Tigeot 		/* check for hardcoded EDID in BIOS */
1394926deccbSFrançois Tigeot 		radeon_combios_check_hardcoded_edid(rdev);
1395926deccbSFrançois Tigeot 	}
1396926deccbSFrançois Tigeot 
1397926deccbSFrançois Tigeot 	/* allocate crtcs */
1398926deccbSFrançois Tigeot 	for (i = 0; i < rdev->num_crtc; i++) {
1399926deccbSFrançois Tigeot 		radeon_crtc_init(rdev->ddev, i);
1400926deccbSFrançois Tigeot 	}
1401926deccbSFrançois Tigeot 
1402926deccbSFrançois Tigeot 	/* okay we should have all the bios connectors */
1403926deccbSFrançois Tigeot 	ret = radeon_setup_enc_conn(rdev->ddev);
1404926deccbSFrançois Tigeot 	if (!ret) {
1405926deccbSFrançois Tigeot 		return ret;
1406926deccbSFrançois Tigeot 	}
1407926deccbSFrançois Tigeot 
1408926deccbSFrançois Tigeot 	/* init dig PHYs, disp eng pll */
1409926deccbSFrançois Tigeot 	if (rdev->is_atom_bios) {
1410926deccbSFrançois Tigeot 		radeon_atom_encoder_init(rdev);
1411926deccbSFrançois Tigeot 		radeon_atom_disp_eng_pll_init(rdev);
1412926deccbSFrançois Tigeot 	}
1413926deccbSFrançois Tigeot 
1414926deccbSFrançois Tigeot 	/* initialize hpd */
1415926deccbSFrançois Tigeot 	radeon_hpd_init(rdev);
1416926deccbSFrançois Tigeot 
1417926deccbSFrançois Tigeot 	/* setup afmt */
1418926deccbSFrançois Tigeot 	radeon_afmt_init(rdev);
1419926deccbSFrançois Tigeot 
1420926deccbSFrançois Tigeot 	/* Initialize power management */
1421926deccbSFrançois Tigeot 	radeon_pm_init(rdev);
1422926deccbSFrançois Tigeot 
1423926deccbSFrançois Tigeot 	radeon_fbdev_init(rdev);
1424926deccbSFrançois Tigeot 	drm_kms_helper_poll_init(rdev->ddev);
1425926deccbSFrançois Tigeot 
1426926deccbSFrançois Tigeot 	return 0;
1427926deccbSFrançois Tigeot }
1428926deccbSFrançois Tigeot 
1429926deccbSFrançois Tigeot void radeon_modeset_fini(struct radeon_device *rdev)
1430926deccbSFrançois Tigeot {
1431926deccbSFrançois Tigeot 	radeon_fbdev_fini(rdev);
14325a3b77d5SFrançois Tigeot 	drm_free(rdev->mode_info.bios_hardcoded_edid, M_DRM);
1433926deccbSFrançois Tigeot 	radeon_pm_fini(rdev);
1434926deccbSFrançois Tigeot 
1435926deccbSFrançois Tigeot 	if (rdev->mode_info.mode_config_initialized) {
1436926deccbSFrançois Tigeot 		radeon_afmt_fini(rdev);
1437926deccbSFrançois Tigeot 		drm_kms_helper_poll_fini(rdev->ddev);
1438926deccbSFrançois Tigeot 		radeon_hpd_fini(rdev);
1439926deccbSFrançois Tigeot 		DRM_UNLOCK(rdev->ddev); /* Work around lock recursion. dumbbell@ */
1440926deccbSFrançois Tigeot 		drm_mode_config_cleanup(rdev->ddev);
1441926deccbSFrançois Tigeot 		DRM_LOCK(rdev->ddev);
1442926deccbSFrançois Tigeot 		rdev->mode_info.mode_config_initialized = false;
1443926deccbSFrançois Tigeot 	}
1444926deccbSFrançois Tigeot 	/* free i2c buses */
1445926deccbSFrançois Tigeot 	radeon_i2c_fini(rdev);
1446926deccbSFrançois Tigeot }
1447926deccbSFrançois Tigeot 
1448926deccbSFrançois Tigeot static bool is_hdtv_mode(const struct drm_display_mode *mode)
1449926deccbSFrançois Tigeot {
1450926deccbSFrançois Tigeot 	/* try and guess if this is a tv or a monitor */
1451926deccbSFrançois Tigeot 	if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1452926deccbSFrançois Tigeot 	    (mode->vdisplay == 576) || /* 576p */
1453926deccbSFrançois Tigeot 	    (mode->vdisplay == 720) || /* 720p */
1454926deccbSFrançois Tigeot 	    (mode->vdisplay == 1080)) /* 1080p */
1455926deccbSFrançois Tigeot 		return true;
1456926deccbSFrançois Tigeot 	else
1457926deccbSFrançois Tigeot 		return false;
1458926deccbSFrançois Tigeot }
1459926deccbSFrançois Tigeot 
1460926deccbSFrançois Tigeot bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1461926deccbSFrançois Tigeot 				const struct drm_display_mode *mode,
1462926deccbSFrançois Tigeot 				struct drm_display_mode *adjusted_mode)
1463926deccbSFrançois Tigeot {
1464926deccbSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
1465926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1466926deccbSFrançois Tigeot 	struct drm_encoder *encoder;
1467926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1468926deccbSFrançois Tigeot 	struct radeon_encoder *radeon_encoder;
1469926deccbSFrançois Tigeot 	struct drm_connector *connector;
1470926deccbSFrançois Tigeot 	struct radeon_connector *radeon_connector;
1471926deccbSFrançois Tigeot 	bool first = true;
1472926deccbSFrançois Tigeot 	u32 src_v = 1, dst_v = 1;
1473926deccbSFrançois Tigeot 	u32 src_h = 1, dst_h = 1;
1474926deccbSFrançois Tigeot 
1475926deccbSFrançois Tigeot 	radeon_crtc->h_border = 0;
1476926deccbSFrançois Tigeot 	radeon_crtc->v_border = 0;
1477926deccbSFrançois Tigeot 
1478926deccbSFrançois Tigeot 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1479926deccbSFrançois Tigeot 		if (encoder->crtc != crtc)
1480926deccbSFrançois Tigeot 			continue;
1481926deccbSFrançois Tigeot 		radeon_encoder = to_radeon_encoder(encoder);
1482926deccbSFrançois Tigeot 		connector = radeon_get_connector_for_encoder(encoder);
1483926deccbSFrançois Tigeot 		radeon_connector = to_radeon_connector(connector);
1484926deccbSFrançois Tigeot 
1485926deccbSFrançois Tigeot 		if (first) {
1486926deccbSFrançois Tigeot 			/* set scaling */
1487926deccbSFrançois Tigeot 			if (radeon_encoder->rmx_type == RMX_OFF)
1488926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_OFF;
1489926deccbSFrançois Tigeot 			else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1490926deccbSFrançois Tigeot 				 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1491926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1492926deccbSFrançois Tigeot 			else
1493926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_OFF;
1494926deccbSFrançois Tigeot 			/* copy native mode */
1495926deccbSFrançois Tigeot 			memcpy(&radeon_crtc->native_mode,
1496926deccbSFrançois Tigeot 			       &radeon_encoder->native_mode,
1497926deccbSFrançois Tigeot 				sizeof(struct drm_display_mode));
1498926deccbSFrançois Tigeot 			src_v = crtc->mode.vdisplay;
1499926deccbSFrançois Tigeot 			dst_v = radeon_crtc->native_mode.vdisplay;
1500926deccbSFrançois Tigeot 			src_h = crtc->mode.hdisplay;
1501926deccbSFrançois Tigeot 			dst_h = radeon_crtc->native_mode.hdisplay;
1502926deccbSFrançois Tigeot 
1503926deccbSFrançois Tigeot 			/* fix up for overscan on hdmi */
1504926deccbSFrançois Tigeot 			if (ASIC_IS_AVIVO(rdev) &&
1505926deccbSFrançois Tigeot 			    (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1506926deccbSFrançois Tigeot 			    ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1507926deccbSFrançois Tigeot 			     ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
1508926deccbSFrançois Tigeot 			      drm_detect_hdmi_monitor(radeon_connector->edid) &&
1509926deccbSFrançois Tigeot 			      is_hdtv_mode(mode)))) {
1510926deccbSFrançois Tigeot 				if (radeon_encoder->underscan_hborder != 0)
1511926deccbSFrançois Tigeot 					radeon_crtc->h_border = radeon_encoder->underscan_hborder;
1512926deccbSFrançois Tigeot 				else
1513926deccbSFrançois Tigeot 					radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1514926deccbSFrançois Tigeot 				if (radeon_encoder->underscan_vborder != 0)
1515926deccbSFrançois Tigeot 					radeon_crtc->v_border = radeon_encoder->underscan_vborder;
1516926deccbSFrançois Tigeot 				else
1517926deccbSFrançois Tigeot 					radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1518926deccbSFrançois Tigeot 				radeon_crtc->rmx_type = RMX_FULL;
1519926deccbSFrançois Tigeot 				src_v = crtc->mode.vdisplay;
1520926deccbSFrançois Tigeot 				dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1521926deccbSFrançois Tigeot 				src_h = crtc->mode.hdisplay;
1522926deccbSFrançois Tigeot 				dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1523926deccbSFrançois Tigeot 			}
1524926deccbSFrançois Tigeot 			first = false;
1525926deccbSFrançois Tigeot 		} else {
1526926deccbSFrançois Tigeot 			if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1527926deccbSFrançois Tigeot 				/* WARNING: Right now this can't happen but
1528926deccbSFrançois Tigeot 				 * in the future we need to check that scaling
1529926deccbSFrançois Tigeot 				 * are consistent across different encoder
1530926deccbSFrançois Tigeot 				 * (ie all encoder can work with the same
1531926deccbSFrançois Tigeot 				 *  scaling).
1532926deccbSFrançois Tigeot 				 */
1533926deccbSFrançois Tigeot 				DRM_ERROR("Scaling not consistent across encoder.\n");
1534926deccbSFrançois Tigeot 				return false;
1535926deccbSFrançois Tigeot 			}
1536926deccbSFrançois Tigeot 		}
1537926deccbSFrançois Tigeot 	}
1538926deccbSFrançois Tigeot 	if (radeon_crtc->rmx_type != RMX_OFF) {
1539926deccbSFrançois Tigeot 		fixed20_12 a, b;
1540926deccbSFrançois Tigeot 		a.full = dfixed_const(src_v);
1541926deccbSFrançois Tigeot 		b.full = dfixed_const(dst_v);
1542926deccbSFrançois Tigeot 		radeon_crtc->vsc.full = dfixed_div(a, b);
1543926deccbSFrançois Tigeot 		a.full = dfixed_const(src_h);
1544926deccbSFrançois Tigeot 		b.full = dfixed_const(dst_h);
1545926deccbSFrançois Tigeot 		radeon_crtc->hsc.full = dfixed_div(a, b);
1546926deccbSFrançois Tigeot 	} else {
1547926deccbSFrançois Tigeot 		radeon_crtc->vsc.full = dfixed_const(1);
1548926deccbSFrançois Tigeot 		radeon_crtc->hsc.full = dfixed_const(1);
1549926deccbSFrançois Tigeot 	}
1550926deccbSFrançois Tigeot 	return true;
1551926deccbSFrançois Tigeot }
1552926deccbSFrançois Tigeot 
1553926deccbSFrançois Tigeot /*
1554*782e40d3SFrançois Tigeot  * Retrieve current video scanout position of crtc on a given gpu, and
1555*782e40d3SFrançois Tigeot  * an optional accurate timestamp of when query happened.
1556926deccbSFrançois Tigeot  *
1557926deccbSFrançois Tigeot  * \param dev Device to query.
1558926deccbSFrançois Tigeot  * \param crtc Crtc to query.
1559*782e40d3SFrançois Tigeot  * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1560926deccbSFrançois Tigeot  * \param *vpos Location where vertical scanout position should be stored.
1561926deccbSFrançois Tigeot  * \param *hpos Location where horizontal scanout position should go.
1562*782e40d3SFrançois Tigeot  * \param *stime Target location for timestamp taken immediately before
1563*782e40d3SFrançois Tigeot  *               scanout position query. Can be NULL to skip timestamp.
1564*782e40d3SFrançois Tigeot  * \param *etime Target location for timestamp taken immediately after
1565*782e40d3SFrançois Tigeot  *               scanout position query. Can be NULL to skip timestamp.
1566926deccbSFrançois Tigeot  *
1567926deccbSFrançois Tigeot  * Returns vpos as a positive number while in active scanout area.
1568926deccbSFrançois Tigeot  * Returns vpos as a negative number inside vblank, counting the number
1569926deccbSFrançois Tigeot  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1570926deccbSFrançois Tigeot  * until start of active scanout / end of vblank."
1571926deccbSFrançois Tigeot  *
1572926deccbSFrançois Tigeot  * \return Flags, or'ed together as follows:
1573926deccbSFrançois Tigeot  *
1574926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_VALID = Query successful.
1575926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_INVBL = Inside vblank.
1576926deccbSFrançois Tigeot  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1577926deccbSFrançois Tigeot  * this flag means that returned position may be offset by a constant but
1578926deccbSFrançois Tigeot  * unknown small number of scanlines wrt. real scanout position.
1579926deccbSFrançois Tigeot  *
1580926deccbSFrançois Tigeot  */
1581*782e40d3SFrançois Tigeot int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
1582*782e40d3SFrançois Tigeot 			       int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
1583926deccbSFrançois Tigeot {
1584926deccbSFrançois Tigeot 	u32 stat_crtc = 0, vbl = 0, position = 0;
1585926deccbSFrançois Tigeot 	int vbl_start, vbl_end, vtotal, ret = 0;
1586926deccbSFrançois Tigeot 	bool in_vbl = true;
1587926deccbSFrançois Tigeot 
1588926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1589926deccbSFrançois Tigeot 
1590*782e40d3SFrançois Tigeot 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1591*782e40d3SFrançois Tigeot 
1592*782e40d3SFrançois Tigeot 	/* Get optional system timestamp before query. */
1593*782e40d3SFrançois Tigeot 	if (stime)
1594*782e40d3SFrançois Tigeot 		*stime = ktime_get();
1595*782e40d3SFrançois Tigeot 
1596926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
1597926deccbSFrançois Tigeot 		if (crtc == 0) {
1598926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1599926deccbSFrançois Tigeot 				     EVERGREEN_CRTC0_REGISTER_OFFSET);
1600926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1601926deccbSFrançois Tigeot 					  EVERGREEN_CRTC0_REGISTER_OFFSET);
1602926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1603926deccbSFrançois Tigeot 		}
1604926deccbSFrançois Tigeot 		if (crtc == 1) {
1605926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1606926deccbSFrançois Tigeot 				     EVERGREEN_CRTC1_REGISTER_OFFSET);
1607926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1608926deccbSFrançois Tigeot 					  EVERGREEN_CRTC1_REGISTER_OFFSET);
1609926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1610926deccbSFrançois Tigeot 		}
1611926deccbSFrançois Tigeot 		if (crtc == 2) {
1612926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1613926deccbSFrançois Tigeot 				     EVERGREEN_CRTC2_REGISTER_OFFSET);
1614926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1615926deccbSFrançois Tigeot 					  EVERGREEN_CRTC2_REGISTER_OFFSET);
1616926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1617926deccbSFrançois Tigeot 		}
1618926deccbSFrançois Tigeot 		if (crtc == 3) {
1619926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1620926deccbSFrançois Tigeot 				     EVERGREEN_CRTC3_REGISTER_OFFSET);
1621926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1622926deccbSFrançois Tigeot 					  EVERGREEN_CRTC3_REGISTER_OFFSET);
1623926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1624926deccbSFrançois Tigeot 		}
1625926deccbSFrançois Tigeot 		if (crtc == 4) {
1626926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1627926deccbSFrançois Tigeot 				     EVERGREEN_CRTC4_REGISTER_OFFSET);
1628926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1629926deccbSFrançois Tigeot 					  EVERGREEN_CRTC4_REGISTER_OFFSET);
1630926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1631926deccbSFrançois Tigeot 		}
1632926deccbSFrançois Tigeot 		if (crtc == 5) {
1633926deccbSFrançois Tigeot 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1634926deccbSFrançois Tigeot 				     EVERGREEN_CRTC5_REGISTER_OFFSET);
1635926deccbSFrançois Tigeot 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1636926deccbSFrançois Tigeot 					  EVERGREEN_CRTC5_REGISTER_OFFSET);
1637926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1638926deccbSFrançois Tigeot 		}
1639926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
1640926deccbSFrançois Tigeot 		if (crtc == 0) {
1641926deccbSFrançois Tigeot 			vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1642926deccbSFrançois Tigeot 			position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1643926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1644926deccbSFrançois Tigeot 		}
1645926deccbSFrançois Tigeot 		if (crtc == 1) {
1646926deccbSFrançois Tigeot 			vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1647926deccbSFrançois Tigeot 			position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1648926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1649926deccbSFrançois Tigeot 		}
1650926deccbSFrançois Tigeot 	} else {
1651926deccbSFrançois Tigeot 		/* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1652926deccbSFrançois Tigeot 		if (crtc == 0) {
1653926deccbSFrançois Tigeot 			/* Assume vbl_end == 0, get vbl_start from
1654926deccbSFrançois Tigeot 			 * upper 16 bits.
1655926deccbSFrançois Tigeot 			 */
1656926deccbSFrançois Tigeot 			vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1657926deccbSFrançois Tigeot 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1658926deccbSFrançois Tigeot 			/* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1659926deccbSFrançois Tigeot 			position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1660926deccbSFrançois Tigeot 			stat_crtc = RREG32(RADEON_CRTC_STATUS);
1661926deccbSFrançois Tigeot 			if (!(stat_crtc & 1))
1662926deccbSFrançois Tigeot 				in_vbl = false;
1663926deccbSFrançois Tigeot 
1664926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1665926deccbSFrançois Tigeot 		}
1666926deccbSFrançois Tigeot 		if (crtc == 1) {
1667926deccbSFrançois Tigeot 			vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1668926deccbSFrançois Tigeot 				RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1669926deccbSFrançois Tigeot 			position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1670926deccbSFrançois Tigeot 			stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1671926deccbSFrançois Tigeot 			if (!(stat_crtc & 1))
1672926deccbSFrançois Tigeot 				in_vbl = false;
1673926deccbSFrançois Tigeot 
1674926deccbSFrançois Tigeot 			ret |= DRM_SCANOUTPOS_VALID;
1675926deccbSFrançois Tigeot 		}
1676926deccbSFrançois Tigeot 	}
1677926deccbSFrançois Tigeot 
1678*782e40d3SFrançois Tigeot 	/* Get optional system timestamp after query. */
1679*782e40d3SFrançois Tigeot 	if (etime)
1680*782e40d3SFrançois Tigeot 		*etime = ktime_get();
1681*782e40d3SFrançois Tigeot 
1682*782e40d3SFrançois Tigeot 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1683*782e40d3SFrançois Tigeot 
1684926deccbSFrançois Tigeot 	/* Decode into vertical and horizontal scanout position. */
1685926deccbSFrançois Tigeot 	*vpos = position & 0x1fff;
1686926deccbSFrançois Tigeot 	*hpos = (position >> 16) & 0x1fff;
1687926deccbSFrançois Tigeot 
1688926deccbSFrançois Tigeot 	/* Valid vblank area boundaries from gpu retrieved? */
1689926deccbSFrançois Tigeot 	if (vbl > 0) {
1690926deccbSFrançois Tigeot 		/* Yes: Decode. */
1691926deccbSFrançois Tigeot 		ret |= DRM_SCANOUTPOS_ACCURATE;
1692926deccbSFrançois Tigeot 		vbl_start = vbl & 0x1fff;
1693926deccbSFrançois Tigeot 		vbl_end = (vbl >> 16) & 0x1fff;
1694926deccbSFrançois Tigeot 	}
1695926deccbSFrançois Tigeot 	else {
1696926deccbSFrançois Tigeot 		/* No: Fake something reasonable which gives at least ok results. */
1697926deccbSFrançois Tigeot 		vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1698926deccbSFrançois Tigeot 		vbl_end = 0;
1699926deccbSFrançois Tigeot 	}
1700926deccbSFrançois Tigeot 
1701926deccbSFrançois Tigeot 	/* Test scanout position against vblank region. */
1702926deccbSFrançois Tigeot 	if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1703926deccbSFrançois Tigeot 		in_vbl = false;
1704926deccbSFrançois Tigeot 
1705926deccbSFrançois Tigeot 	/* Check if inside vblank area and apply corrective offsets:
1706926deccbSFrançois Tigeot 	 * vpos will then be >=0 in video scanout area, but negative
1707926deccbSFrançois Tigeot 	 * within vblank area, counting down the number of lines until
1708926deccbSFrançois Tigeot 	 * start of scanout.
1709926deccbSFrançois Tigeot 	 */
1710926deccbSFrançois Tigeot 
1711926deccbSFrançois Tigeot 	/* Inside "upper part" of vblank area? Apply corrective offset if so: */
1712926deccbSFrançois Tigeot 	if (in_vbl && (*vpos >= vbl_start)) {
1713926deccbSFrançois Tigeot 		vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1714926deccbSFrançois Tigeot 		*vpos = *vpos - vtotal;
1715926deccbSFrançois Tigeot 	}
1716926deccbSFrançois Tigeot 
1717926deccbSFrançois Tigeot 	/* Correct for shifted end of vbl at vbl_end. */
1718926deccbSFrançois Tigeot 	*vpos = *vpos - vbl_end;
1719926deccbSFrançois Tigeot 
1720926deccbSFrançois Tigeot 	/* In vblank? */
1721926deccbSFrançois Tigeot 	if (in_vbl)
1722926deccbSFrançois Tigeot 		ret |= DRM_SCANOUTPOS_INVBL;
1723926deccbSFrançois Tigeot 
1724*782e40d3SFrançois Tigeot 	/* Is vpos outside nominal vblank area, but less than
1725*782e40d3SFrançois Tigeot 	 * 1/100 of a frame height away from start of vblank?
1726*782e40d3SFrançois Tigeot 	 * If so, assume this isn't a massively delayed vblank
1727*782e40d3SFrançois Tigeot 	 * interrupt, but a vblank interrupt that fired a few
1728*782e40d3SFrançois Tigeot 	 * microseconds before true start of vblank. Compensate
1729*782e40d3SFrançois Tigeot 	 * by adding a full frame duration to the final timestamp.
1730*782e40d3SFrançois Tigeot 	 * Happens, e.g., on ATI R500, R600.
1731*782e40d3SFrançois Tigeot 	 *
1732*782e40d3SFrançois Tigeot 	 * We only do this if DRM_CALLED_FROM_VBLIRQ.
1733*782e40d3SFrançois Tigeot 	 */
1734*782e40d3SFrançois Tigeot 	if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
1735*782e40d3SFrançois Tigeot 		vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1736*782e40d3SFrançois Tigeot 		vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1737*782e40d3SFrançois Tigeot 
1738*782e40d3SFrançois Tigeot 		if (vbl_start - *vpos < vtotal / 100) {
1739*782e40d3SFrançois Tigeot 			*vpos -= vtotal;
1740*782e40d3SFrançois Tigeot 
1741*782e40d3SFrançois Tigeot 			/* Signal this correction as "applied". */
1742*782e40d3SFrançois Tigeot 			ret |= 0x8;
1743*782e40d3SFrançois Tigeot 		}
1744*782e40d3SFrançois Tigeot 	}
1745*782e40d3SFrançois Tigeot 
1746926deccbSFrançois Tigeot 	return ret;
1747926deccbSFrançois Tigeot }
1748