1fb4d8502Sjsg /*
2fb4d8502Sjsg  * Copyright 2007-8 Advanced Micro Devices, Inc.
3fb4d8502Sjsg  * Copyright 2008 Red Hat Inc.
4fb4d8502Sjsg  *
5fb4d8502Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
6fb4d8502Sjsg  * copy of this software and associated documentation files (the "Software"),
7fb4d8502Sjsg  * to deal in the Software without restriction, including without limitation
8fb4d8502Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9fb4d8502Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
10fb4d8502Sjsg  * Software is furnished to do so, subject to the following conditions:
11fb4d8502Sjsg  *
12fb4d8502Sjsg  * The above copyright notice and this permission notice shall be included in
13fb4d8502Sjsg  * all copies or substantial portions of the Software.
14fb4d8502Sjsg  *
15fb4d8502Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16fb4d8502Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17fb4d8502Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18fb4d8502Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19fb4d8502Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20fb4d8502Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21fb4d8502Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
22fb4d8502Sjsg  *
23fb4d8502Sjsg  * Authors: Dave Airlie
24fb4d8502Sjsg  *          Alex Deucher
25fb4d8502Sjsg  */
26c349dbc7Sjsg 
27fb4d8502Sjsg #include <drm/amdgpu_drm.h>
28fb4d8502Sjsg #include <drm/drm_fixed.h>
29fb4d8502Sjsg #include "amdgpu.h"
30fb4d8502Sjsg #include "atom.h"
31fb4d8502Sjsg #include "atom-bits.h"
32fb4d8502Sjsg #include "atombios_encoders.h"
33fb4d8502Sjsg #include "atombios_crtc.h"
34fb4d8502Sjsg #include "amdgpu_atombios.h"
35fb4d8502Sjsg #include "amdgpu_pll.h"
36fb4d8502Sjsg #include "amdgpu_connectors.h"
37fb4d8502Sjsg 
amdgpu_atombios_crtc_overscan_setup(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)38fb4d8502Sjsg void amdgpu_atombios_crtc_overscan_setup(struct drm_crtc *crtc,
39fb4d8502Sjsg 				  struct drm_display_mode *mode,
40fb4d8502Sjsg 				  struct drm_display_mode *adjusted_mode)
41fb4d8502Sjsg {
42fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
43ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
44fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
45fb4d8502Sjsg 	SET_CRTC_OVERSCAN_PS_ALLOCATION args;
46fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
47fb4d8502Sjsg 	int a1, a2;
48fb4d8502Sjsg 
49fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
50fb4d8502Sjsg 
51fb4d8502Sjsg 	args.ucCRTC = amdgpu_crtc->crtc_id;
52fb4d8502Sjsg 
53fb4d8502Sjsg 	switch (amdgpu_crtc->rmx_type) {
54fb4d8502Sjsg 	case RMX_CENTER:
55fb4d8502Sjsg 		args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
56fb4d8502Sjsg 		args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
57fb4d8502Sjsg 		args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
58fb4d8502Sjsg 		args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
59fb4d8502Sjsg 		break;
60fb4d8502Sjsg 	case RMX_ASPECT:
61fb4d8502Sjsg 		a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
62fb4d8502Sjsg 		a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
63fb4d8502Sjsg 
64fb4d8502Sjsg 		if (a1 > a2) {
65fb4d8502Sjsg 			args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
66fb4d8502Sjsg 			args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
67fb4d8502Sjsg 		} else if (a2 > a1) {
68fb4d8502Sjsg 			args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
69fb4d8502Sjsg 			args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
70fb4d8502Sjsg 		}
71fb4d8502Sjsg 		break;
72fb4d8502Sjsg 	case RMX_FULL:
73fb4d8502Sjsg 	default:
74fb4d8502Sjsg 		args.usOverscanRight = cpu_to_le16(amdgpu_crtc->h_border);
75fb4d8502Sjsg 		args.usOverscanLeft = cpu_to_le16(amdgpu_crtc->h_border);
76fb4d8502Sjsg 		args.usOverscanBottom = cpu_to_le16(amdgpu_crtc->v_border);
77fb4d8502Sjsg 		args.usOverscanTop = cpu_to_le16(amdgpu_crtc->v_border);
78fb4d8502Sjsg 		break;
79fb4d8502Sjsg 	}
80fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
81fb4d8502Sjsg }
82fb4d8502Sjsg 
amdgpu_atombios_crtc_scaler_setup(struct drm_crtc * crtc)83fb4d8502Sjsg void amdgpu_atombios_crtc_scaler_setup(struct drm_crtc *crtc)
84fb4d8502Sjsg {
85fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
86ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
87fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
88fb4d8502Sjsg 	ENABLE_SCALER_PS_ALLOCATION args;
89fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
90fb4d8502Sjsg 
91fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
92fb4d8502Sjsg 
93fb4d8502Sjsg 	args.ucScaler = amdgpu_crtc->crtc_id;
94fb4d8502Sjsg 
95fb4d8502Sjsg 	switch (amdgpu_crtc->rmx_type) {
96fb4d8502Sjsg 	case RMX_FULL:
97fb4d8502Sjsg 		args.ucEnable = ATOM_SCALER_EXPANSION;
98fb4d8502Sjsg 		break;
99fb4d8502Sjsg 	case RMX_CENTER:
100fb4d8502Sjsg 		args.ucEnable = ATOM_SCALER_CENTER;
101fb4d8502Sjsg 		break;
102fb4d8502Sjsg 	case RMX_ASPECT:
103fb4d8502Sjsg 		args.ucEnable = ATOM_SCALER_EXPANSION;
104fb4d8502Sjsg 		break;
105fb4d8502Sjsg 	default:
106fb4d8502Sjsg 		args.ucEnable = ATOM_SCALER_DISABLE;
107fb4d8502Sjsg 		break;
108fb4d8502Sjsg 	}
109fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
110fb4d8502Sjsg }
111fb4d8502Sjsg 
amdgpu_atombios_crtc_lock(struct drm_crtc * crtc,int lock)112fb4d8502Sjsg void amdgpu_atombios_crtc_lock(struct drm_crtc *crtc, int lock)
113fb4d8502Sjsg {
114fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
115fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
116ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
117fb4d8502Sjsg 	int index =
118fb4d8502Sjsg 	    GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
119fb4d8502Sjsg 	ENABLE_CRTC_PS_ALLOCATION args;
120fb4d8502Sjsg 
121fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
122fb4d8502Sjsg 
123fb4d8502Sjsg 	args.ucCRTC = amdgpu_crtc->crtc_id;
124fb4d8502Sjsg 	args.ucEnable = lock;
125fb4d8502Sjsg 
126fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
127fb4d8502Sjsg }
128fb4d8502Sjsg 
amdgpu_atombios_crtc_enable(struct drm_crtc * crtc,int state)129fb4d8502Sjsg void amdgpu_atombios_crtc_enable(struct drm_crtc *crtc, int state)
130fb4d8502Sjsg {
131fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
132fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
133ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
134fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
135fb4d8502Sjsg 	ENABLE_CRTC_PS_ALLOCATION args;
136fb4d8502Sjsg 
137fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
138fb4d8502Sjsg 
139fb4d8502Sjsg 	args.ucCRTC = amdgpu_crtc->crtc_id;
140fb4d8502Sjsg 	args.ucEnable = state;
141fb4d8502Sjsg 
142fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
143fb4d8502Sjsg }
144fb4d8502Sjsg 
amdgpu_atombios_crtc_blank(struct drm_crtc * crtc,int state)145fb4d8502Sjsg void amdgpu_atombios_crtc_blank(struct drm_crtc *crtc, int state)
146fb4d8502Sjsg {
147fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
148fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
149ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
150fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
151fb4d8502Sjsg 	BLANK_CRTC_PS_ALLOCATION args;
152fb4d8502Sjsg 
153fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
154fb4d8502Sjsg 
155fb4d8502Sjsg 	args.ucCRTC = amdgpu_crtc->crtc_id;
156fb4d8502Sjsg 	args.ucBlanking = state;
157fb4d8502Sjsg 
158fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
159fb4d8502Sjsg }
160fb4d8502Sjsg 
amdgpu_atombios_crtc_powergate(struct drm_crtc * crtc,int state)161fb4d8502Sjsg void amdgpu_atombios_crtc_powergate(struct drm_crtc *crtc, int state)
162fb4d8502Sjsg {
163fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
164fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
165ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
166fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
167fb4d8502Sjsg 	ENABLE_DISP_POWER_GATING_PS_ALLOCATION args;
168fb4d8502Sjsg 
169fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
170fb4d8502Sjsg 
171fb4d8502Sjsg 	args.ucDispPipeId = amdgpu_crtc->crtc_id;
172fb4d8502Sjsg 	args.ucEnable = state;
173fb4d8502Sjsg 
174fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
175fb4d8502Sjsg }
176fb4d8502Sjsg 
amdgpu_atombios_crtc_powergate_init(struct amdgpu_device * adev)177fb4d8502Sjsg void amdgpu_atombios_crtc_powergate_init(struct amdgpu_device *adev)
178fb4d8502Sjsg {
179fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
180fb4d8502Sjsg 	ENABLE_DISP_POWER_GATING_PS_ALLOCATION args;
181fb4d8502Sjsg 
182fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
183fb4d8502Sjsg 
184fb4d8502Sjsg 	args.ucEnable = ATOM_INIT;
185fb4d8502Sjsg 
186fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
187fb4d8502Sjsg }
188fb4d8502Sjsg 
amdgpu_atombios_crtc_set_dtd_timing(struct drm_crtc * crtc,struct drm_display_mode * mode)189fb4d8502Sjsg void amdgpu_atombios_crtc_set_dtd_timing(struct drm_crtc *crtc,
190fb4d8502Sjsg 				  struct drm_display_mode *mode)
191fb4d8502Sjsg {
192fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
193fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
194ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
195fb4d8502Sjsg 	SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
196fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
197fb4d8502Sjsg 	u16 misc = 0;
198fb4d8502Sjsg 
199fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
200fb4d8502Sjsg 	args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (amdgpu_crtc->h_border * 2));
201fb4d8502Sjsg 	args.usH_Blanking_Time =
202fb4d8502Sjsg 		cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (amdgpu_crtc->h_border * 2));
203fb4d8502Sjsg 	args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (amdgpu_crtc->v_border * 2));
204fb4d8502Sjsg 	args.usV_Blanking_Time =
205fb4d8502Sjsg 		cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (amdgpu_crtc->v_border * 2));
206fb4d8502Sjsg 	args.usH_SyncOffset =
207fb4d8502Sjsg 		cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + amdgpu_crtc->h_border);
208fb4d8502Sjsg 	args.usH_SyncWidth =
209fb4d8502Sjsg 		cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
210fb4d8502Sjsg 	args.usV_SyncOffset =
211fb4d8502Sjsg 		cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + amdgpu_crtc->v_border);
212fb4d8502Sjsg 	args.usV_SyncWidth =
213fb4d8502Sjsg 		cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
214fb4d8502Sjsg 	args.ucH_Border = amdgpu_crtc->h_border;
215fb4d8502Sjsg 	args.ucV_Border = amdgpu_crtc->v_border;
216fb4d8502Sjsg 
217fb4d8502Sjsg 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
218fb4d8502Sjsg 		misc |= ATOM_VSYNC_POLARITY;
219fb4d8502Sjsg 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
220fb4d8502Sjsg 		misc |= ATOM_HSYNC_POLARITY;
221fb4d8502Sjsg 	if (mode->flags & DRM_MODE_FLAG_CSYNC)
222fb4d8502Sjsg 		misc |= ATOM_COMPOSITESYNC;
223fb4d8502Sjsg 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
224fb4d8502Sjsg 		misc |= ATOM_INTERLACE;
225fb4d8502Sjsg 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
226fb4d8502Sjsg 		misc |= ATOM_DOUBLE_CLOCK_MODE;
227fb4d8502Sjsg 
228fb4d8502Sjsg 	args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
229fb4d8502Sjsg 	args.ucCRTC = amdgpu_crtc->crtc_id;
230fb4d8502Sjsg 
231fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
232fb4d8502Sjsg }
233fb4d8502Sjsg 
234fb4d8502Sjsg union atom_enable_ss {
235fb4d8502Sjsg 	ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
236fb4d8502Sjsg 	ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
237fb4d8502Sjsg 	ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
238fb4d8502Sjsg };
239fb4d8502Sjsg 
amdgpu_atombios_crtc_program_ss(struct amdgpu_device * adev,int enable,int pll_id,int crtc_id,struct amdgpu_atom_ss * ss)240fb4d8502Sjsg static void amdgpu_atombios_crtc_program_ss(struct amdgpu_device *adev,
241fb4d8502Sjsg 				     int enable,
242fb4d8502Sjsg 				     int pll_id,
243fb4d8502Sjsg 				     int crtc_id,
244fb4d8502Sjsg 				     struct amdgpu_atom_ss *ss)
245fb4d8502Sjsg {
246fb4d8502Sjsg 	unsigned i;
247fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
248fb4d8502Sjsg 	union atom_enable_ss args;
249fb4d8502Sjsg 
250fb4d8502Sjsg 	if (enable) {
251fb4d8502Sjsg 		/* Don't mess with SS if percentage is 0 or external ss.
252fb4d8502Sjsg 		 * SS is already disabled previously, and disabling it
253fb4d8502Sjsg 		 * again can cause display problems if the pll is already
254fb4d8502Sjsg 		 * programmed.
255fb4d8502Sjsg 		 */
256fb4d8502Sjsg 		if (ss->percentage == 0)
257fb4d8502Sjsg 			return;
258fb4d8502Sjsg 		if (ss->type & ATOM_EXTERNAL_SS_MASK)
259fb4d8502Sjsg 			return;
260fb4d8502Sjsg 	} else {
261fb4d8502Sjsg 		for (i = 0; i < adev->mode_info.num_crtc; i++) {
262fb4d8502Sjsg 			if (adev->mode_info.crtcs[i] &&
263fb4d8502Sjsg 			    adev->mode_info.crtcs[i]->enabled &&
264fb4d8502Sjsg 			    i != crtc_id &&
265fb4d8502Sjsg 			    pll_id == adev->mode_info.crtcs[i]->pll_id) {
266fb4d8502Sjsg 				/* one other crtc is using this pll don't turn
267fb4d8502Sjsg 				 * off spread spectrum as it might turn off
268fb4d8502Sjsg 				 * display on active crtc
269fb4d8502Sjsg 				 */
270fb4d8502Sjsg 				return;
271fb4d8502Sjsg 			}
272fb4d8502Sjsg 		}
273fb4d8502Sjsg 	}
274fb4d8502Sjsg 
275fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
276fb4d8502Sjsg 
277fb4d8502Sjsg 	args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
278fb4d8502Sjsg 	args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
279fb4d8502Sjsg 	switch (pll_id) {
280fb4d8502Sjsg 	case ATOM_PPLL1:
281fb4d8502Sjsg 		args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
282fb4d8502Sjsg 		break;
283fb4d8502Sjsg 	case ATOM_PPLL2:
284fb4d8502Sjsg 		args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
285fb4d8502Sjsg 		break;
286fb4d8502Sjsg 	case ATOM_DCPLL:
287fb4d8502Sjsg 		args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
288fb4d8502Sjsg 		break;
289fb4d8502Sjsg 	case ATOM_PPLL_INVALID:
290fb4d8502Sjsg 		return;
291fb4d8502Sjsg 	}
292fb4d8502Sjsg 	args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
293fb4d8502Sjsg 	args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
294fb4d8502Sjsg 	args.v3.ucEnable = enable;
295fb4d8502Sjsg 
296fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
297fb4d8502Sjsg }
298fb4d8502Sjsg 
299fb4d8502Sjsg union adjust_pixel_clock {
300fb4d8502Sjsg 	ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
301fb4d8502Sjsg 	ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
302fb4d8502Sjsg };
303fb4d8502Sjsg 
amdgpu_atombios_crtc_adjust_pll(struct drm_crtc * crtc,struct drm_display_mode * mode)304fb4d8502Sjsg static u32 amdgpu_atombios_crtc_adjust_pll(struct drm_crtc *crtc,
305fb4d8502Sjsg 				    struct drm_display_mode *mode)
306fb4d8502Sjsg {
307fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
308fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
309ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
310fb4d8502Sjsg 	struct drm_encoder *encoder = amdgpu_crtc->encoder;
311fb4d8502Sjsg 	struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
312fb4d8502Sjsg 	struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
313fb4d8502Sjsg 	u32 adjusted_clock = mode->clock;
314fb4d8502Sjsg 	int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
315fb4d8502Sjsg 	u32 dp_clock = mode->clock;
316fb4d8502Sjsg 	u32 clock = mode->clock;
317fb4d8502Sjsg 	int bpc = amdgpu_crtc->bpc;
318fb4d8502Sjsg 	bool is_duallink = amdgpu_dig_monitor_is_duallink(encoder, mode->clock);
319fb4d8502Sjsg 	union adjust_pixel_clock args;
320fb4d8502Sjsg 	u8 frev, crev;
321fb4d8502Sjsg 	int index;
322fb4d8502Sjsg 
323fb4d8502Sjsg 	amdgpu_crtc->pll_flags = AMDGPU_PLL_USE_FRAC_FB_DIV;
324fb4d8502Sjsg 
325fb4d8502Sjsg 	if ((amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
326fb4d8502Sjsg 	    (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
327fb4d8502Sjsg 		if (connector) {
328fb4d8502Sjsg 			struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
329fb4d8502Sjsg 			struct amdgpu_connector_atom_dig *dig_connector =
330fb4d8502Sjsg 				amdgpu_connector->con_priv;
331fb4d8502Sjsg 
332fb4d8502Sjsg 			dp_clock = dig_connector->dp_clock;
333fb4d8502Sjsg 		}
334fb4d8502Sjsg 	}
335fb4d8502Sjsg 
336fb4d8502Sjsg 	/* use recommended ref_div for ss */
337fb4d8502Sjsg 	if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
338fb4d8502Sjsg 		if (amdgpu_crtc->ss_enabled) {
339fb4d8502Sjsg 			if (amdgpu_crtc->ss.refdiv) {
340fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_REF_DIV;
341fb4d8502Sjsg 				amdgpu_crtc->pll_reference_div = amdgpu_crtc->ss.refdiv;
342fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
343fb4d8502Sjsg 			}
344fb4d8502Sjsg 		}
345fb4d8502Sjsg 	}
346fb4d8502Sjsg 
347fb4d8502Sjsg 	/* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
348fb4d8502Sjsg 	if (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
349fb4d8502Sjsg 		adjusted_clock = mode->clock * 2;
350fb4d8502Sjsg 	if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
351fb4d8502Sjsg 		amdgpu_crtc->pll_flags |= AMDGPU_PLL_PREFER_CLOSEST_LOWER;
352fb4d8502Sjsg 	if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
353fb4d8502Sjsg 		amdgpu_crtc->pll_flags |= AMDGPU_PLL_IS_LCD;
354fb4d8502Sjsg 
355fb4d8502Sjsg 
356fb4d8502Sjsg 	/* adjust pll for deep color modes */
357fb4d8502Sjsg 	if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
358fb4d8502Sjsg 		switch (bpc) {
359fb4d8502Sjsg 		case 8:
360fb4d8502Sjsg 		default:
361fb4d8502Sjsg 			break;
362fb4d8502Sjsg 		case 10:
363fb4d8502Sjsg 			clock = (clock * 5) / 4;
364fb4d8502Sjsg 			break;
365fb4d8502Sjsg 		case 12:
366fb4d8502Sjsg 			clock = (clock * 3) / 2;
367fb4d8502Sjsg 			break;
368fb4d8502Sjsg 		case 16:
369fb4d8502Sjsg 			clock = clock * 2;
370fb4d8502Sjsg 			break;
371fb4d8502Sjsg 		}
372fb4d8502Sjsg 	}
373fb4d8502Sjsg 
374fb4d8502Sjsg 	/* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
375fb4d8502Sjsg 	 * accordingly based on the encoder/transmitter to work around
376fb4d8502Sjsg 	 * special hw requirements.
377fb4d8502Sjsg 	 */
378fb4d8502Sjsg 	index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
379fb4d8502Sjsg 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
380fb4d8502Sjsg 				   &crev))
381fb4d8502Sjsg 		return adjusted_clock;
382fb4d8502Sjsg 
383fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
384fb4d8502Sjsg 
385fb4d8502Sjsg 	switch (frev) {
386fb4d8502Sjsg 	case 1:
387fb4d8502Sjsg 		switch (crev) {
388fb4d8502Sjsg 		case 1:
389fb4d8502Sjsg 		case 2:
390fb4d8502Sjsg 			args.v1.usPixelClock = cpu_to_le16(clock / 10);
391fb4d8502Sjsg 			args.v1.ucTransmitterID = amdgpu_encoder->encoder_id;
392fb4d8502Sjsg 			args.v1.ucEncodeMode = encoder_mode;
393fb4d8502Sjsg 			if (amdgpu_crtc->ss_enabled && amdgpu_crtc->ss.percentage)
394fb4d8502Sjsg 				args.v1.ucConfig |=
395fb4d8502Sjsg 					ADJUST_DISPLAY_CONFIG_SS_ENABLE;
396fb4d8502Sjsg 
397fb4d8502Sjsg 			amdgpu_atom_execute_table(adev->mode_info.atom_context,
398fb4d8502Sjsg 					   index, (uint32_t *)&args);
399fb4d8502Sjsg 			adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
400fb4d8502Sjsg 			break;
401fb4d8502Sjsg 		case 3:
402fb4d8502Sjsg 			args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
403fb4d8502Sjsg 			args.v3.sInput.ucTransmitterID = amdgpu_encoder->encoder_id;
404fb4d8502Sjsg 			args.v3.sInput.ucEncodeMode = encoder_mode;
405fb4d8502Sjsg 			args.v3.sInput.ucDispPllConfig = 0;
406fb4d8502Sjsg 			if (amdgpu_crtc->ss_enabled && amdgpu_crtc->ss.percentage)
407fb4d8502Sjsg 				args.v3.sInput.ucDispPllConfig |=
408fb4d8502Sjsg 					DISPPLL_CONFIG_SS_ENABLE;
409fb4d8502Sjsg 			if (ENCODER_MODE_IS_DP(encoder_mode)) {
410fb4d8502Sjsg 				args.v3.sInput.ucDispPllConfig |=
411fb4d8502Sjsg 					DISPPLL_CONFIG_COHERENT_MODE;
412fb4d8502Sjsg 				/* 16200 or 27000 */
413fb4d8502Sjsg 				args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
414fb4d8502Sjsg 			} else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
415fb4d8502Sjsg 				struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
416fb4d8502Sjsg 				if (dig->coherent_mode)
417fb4d8502Sjsg 					args.v3.sInput.ucDispPllConfig |=
418fb4d8502Sjsg 						DISPPLL_CONFIG_COHERENT_MODE;
419fb4d8502Sjsg 				if (is_duallink)
420fb4d8502Sjsg 					args.v3.sInput.ucDispPllConfig |=
421fb4d8502Sjsg 						DISPPLL_CONFIG_DUAL_LINK;
422fb4d8502Sjsg 			}
423fb4d8502Sjsg 			if (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
424fb4d8502Sjsg 			    ENCODER_OBJECT_ID_NONE)
425fb4d8502Sjsg 				args.v3.sInput.ucExtTransmitterID =
426fb4d8502Sjsg 					amdgpu_encoder_get_dp_bridge_encoder_id(encoder);
427fb4d8502Sjsg 			else
428fb4d8502Sjsg 				args.v3.sInput.ucExtTransmitterID = 0;
429fb4d8502Sjsg 
430fb4d8502Sjsg 			amdgpu_atom_execute_table(adev->mode_info.atom_context,
431fb4d8502Sjsg 					   index, (uint32_t *)&args);
432fb4d8502Sjsg 			adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
433fb4d8502Sjsg 			if (args.v3.sOutput.ucRefDiv) {
434fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
435fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_REF_DIV;
436fb4d8502Sjsg 				amdgpu_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
437fb4d8502Sjsg 			}
438fb4d8502Sjsg 			if (args.v3.sOutput.ucPostDiv) {
439fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
440fb4d8502Sjsg 				amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_POST_DIV;
441fb4d8502Sjsg 				amdgpu_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
442fb4d8502Sjsg 			}
443fb4d8502Sjsg 			break;
444fb4d8502Sjsg 		default:
445fb4d8502Sjsg 			DRM_ERROR("Unknown table version %d %d\n", frev, crev);
446fb4d8502Sjsg 			return adjusted_clock;
447fb4d8502Sjsg 		}
448fb4d8502Sjsg 		break;
449fb4d8502Sjsg 	default:
450fb4d8502Sjsg 		DRM_ERROR("Unknown table version %d %d\n", frev, crev);
451fb4d8502Sjsg 		return adjusted_clock;
452fb4d8502Sjsg 	}
453fb4d8502Sjsg 
454fb4d8502Sjsg 	return adjusted_clock;
455fb4d8502Sjsg }
456fb4d8502Sjsg 
457fb4d8502Sjsg union set_pixel_clock {
458fb4d8502Sjsg 	SET_PIXEL_CLOCK_PS_ALLOCATION base;
459fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS v1;
460fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS_V2 v2;
461fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS_V3 v3;
462fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS_V5 v5;
463fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS_V6 v6;
464fb4d8502Sjsg 	PIXEL_CLOCK_PARAMETERS_V7 v7;
465fb4d8502Sjsg };
466fb4d8502Sjsg 
467fb4d8502Sjsg /* on DCE5, make sure the voltage is high enough to support the
468fb4d8502Sjsg  * required disp clk.
469fb4d8502Sjsg  */
amdgpu_atombios_crtc_set_disp_eng_pll(struct amdgpu_device * adev,u32 dispclk)470fb4d8502Sjsg void amdgpu_atombios_crtc_set_disp_eng_pll(struct amdgpu_device *adev,
471fb4d8502Sjsg 					   u32 dispclk)
472fb4d8502Sjsg {
473fb4d8502Sjsg 	u8 frev, crev;
474fb4d8502Sjsg 	int index;
475fb4d8502Sjsg 	union set_pixel_clock args;
476fb4d8502Sjsg 
477fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
478fb4d8502Sjsg 
479fb4d8502Sjsg 	index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
480fb4d8502Sjsg 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
481fb4d8502Sjsg 				   &crev))
482fb4d8502Sjsg 		return;
483fb4d8502Sjsg 
484fb4d8502Sjsg 	switch (frev) {
485fb4d8502Sjsg 	case 1:
486fb4d8502Sjsg 		switch (crev) {
487fb4d8502Sjsg 		case 5:
488fb4d8502Sjsg 			/* if the default dcpll clock is specified,
489fb4d8502Sjsg 			 * SetPixelClock provides the dividers
490fb4d8502Sjsg 			 */
491fb4d8502Sjsg 			args.v5.ucCRTC = ATOM_CRTC_INVALID;
492fb4d8502Sjsg 			args.v5.usPixelClock = cpu_to_le16(dispclk);
493fb4d8502Sjsg 			args.v5.ucPpll = ATOM_DCPLL;
494fb4d8502Sjsg 			break;
495fb4d8502Sjsg 		case 6:
496fb4d8502Sjsg 			/* if the default dcpll clock is specified,
497fb4d8502Sjsg 			 * SetPixelClock provides the dividers
498fb4d8502Sjsg 			 */
499fb4d8502Sjsg 			args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
500fb4d8502Sjsg 			if (adev->asic_type == CHIP_TAHITI ||
501fb4d8502Sjsg 			    adev->asic_type == CHIP_PITCAIRN ||
502fb4d8502Sjsg 			    adev->asic_type == CHIP_VERDE ||
503fb4d8502Sjsg 			    adev->asic_type == CHIP_OLAND)
504fb4d8502Sjsg 				args.v6.ucPpll = ATOM_PPLL0;
505fb4d8502Sjsg 			else
506fb4d8502Sjsg 				args.v6.ucPpll = ATOM_EXT_PLL1;
507fb4d8502Sjsg 			break;
508fb4d8502Sjsg 		default:
509fb4d8502Sjsg 			DRM_ERROR("Unknown table version %d %d\n", frev, crev);
510fb4d8502Sjsg 			return;
511fb4d8502Sjsg 		}
512fb4d8502Sjsg 		break;
513fb4d8502Sjsg 	default:
514fb4d8502Sjsg 		DRM_ERROR("Unknown table version %d %d\n", frev, crev);
515fb4d8502Sjsg 		return;
516fb4d8502Sjsg 	}
517fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
518fb4d8502Sjsg }
519fb4d8502Sjsg 
520fb4d8502Sjsg union set_dce_clock {
521fb4d8502Sjsg 	SET_DCE_CLOCK_PS_ALLOCATION_V1_1 v1_1;
522fb4d8502Sjsg 	SET_DCE_CLOCK_PS_ALLOCATION_V2_1 v2_1;
523fb4d8502Sjsg };
524fb4d8502Sjsg 
amdgpu_atombios_crtc_set_dce_clock(struct amdgpu_device * adev,u32 freq,u8 clk_type,u8 clk_src)525fb4d8502Sjsg u32 amdgpu_atombios_crtc_set_dce_clock(struct amdgpu_device *adev,
526fb4d8502Sjsg 				       u32 freq, u8 clk_type, u8 clk_src)
527fb4d8502Sjsg {
528fb4d8502Sjsg 	u8 frev, crev;
529fb4d8502Sjsg 	int index;
530fb4d8502Sjsg 	union set_dce_clock args;
531fb4d8502Sjsg 	u32 ret_freq = 0;
532fb4d8502Sjsg 
533fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
534fb4d8502Sjsg 
535fb4d8502Sjsg 	index = GetIndexIntoMasterTable(COMMAND, SetDCEClock);
536fb4d8502Sjsg 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
537fb4d8502Sjsg 				   &crev))
538fb4d8502Sjsg 		return 0;
539fb4d8502Sjsg 
540fb4d8502Sjsg 	switch (frev) {
541fb4d8502Sjsg 	case 2:
542fb4d8502Sjsg 		switch (crev) {
543fb4d8502Sjsg 		case 1:
544fb4d8502Sjsg 			args.v2_1.asParam.ulDCEClkFreq = cpu_to_le32(freq); /* 10kHz units */
545fb4d8502Sjsg 			args.v2_1.asParam.ucDCEClkType = clk_type;
546fb4d8502Sjsg 			args.v2_1.asParam.ucDCEClkSrc = clk_src;
547fb4d8502Sjsg 			amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
548fb4d8502Sjsg 			ret_freq = le32_to_cpu(args.v2_1.asParam.ulDCEClkFreq) * 10;
549fb4d8502Sjsg 			break;
550fb4d8502Sjsg 		default:
551fb4d8502Sjsg 			DRM_ERROR("Unknown table version %d %d\n", frev, crev);
552fb4d8502Sjsg 			return 0;
553fb4d8502Sjsg 		}
554fb4d8502Sjsg 		break;
555fb4d8502Sjsg 	default:
556fb4d8502Sjsg 		DRM_ERROR("Unknown table version %d %d\n", frev, crev);
557fb4d8502Sjsg 		return 0;
558fb4d8502Sjsg 	}
559fb4d8502Sjsg 
560fb4d8502Sjsg 	return ret_freq;
561fb4d8502Sjsg }
562fb4d8502Sjsg 
is_pixel_clock_source_from_pll(u32 encoder_mode,int pll_id)563fb4d8502Sjsg static bool is_pixel_clock_source_from_pll(u32 encoder_mode, int pll_id)
564fb4d8502Sjsg {
565fb4d8502Sjsg 	if (ENCODER_MODE_IS_DP(encoder_mode)) {
566fb4d8502Sjsg 		if (pll_id < ATOM_EXT_PLL1)
567fb4d8502Sjsg 			return true;
568fb4d8502Sjsg 		else
569fb4d8502Sjsg 			return false;
570fb4d8502Sjsg 	} else {
571fb4d8502Sjsg 		return true;
572fb4d8502Sjsg 	}
573fb4d8502Sjsg }
574fb4d8502Sjsg 
amdgpu_atombios_crtc_program_pll(struct drm_crtc * crtc,u32 crtc_id,int pll_id,u32 encoder_mode,u32 encoder_id,u32 clock,u32 ref_div,u32 fb_div,u32 frac_fb_div,u32 post_div,int bpc,bool ss_enabled,struct amdgpu_atom_ss * ss)575fb4d8502Sjsg void amdgpu_atombios_crtc_program_pll(struct drm_crtc *crtc,
576fb4d8502Sjsg 				      u32 crtc_id,
577fb4d8502Sjsg 				      int pll_id,
578fb4d8502Sjsg 				      u32 encoder_mode,
579fb4d8502Sjsg 				      u32 encoder_id,
580fb4d8502Sjsg 				      u32 clock,
581fb4d8502Sjsg 				      u32 ref_div,
582fb4d8502Sjsg 				      u32 fb_div,
583fb4d8502Sjsg 				      u32 frac_fb_div,
584fb4d8502Sjsg 				      u32 post_div,
585fb4d8502Sjsg 				      int bpc,
586fb4d8502Sjsg 				      bool ss_enabled,
587fb4d8502Sjsg 				      struct amdgpu_atom_ss *ss)
588fb4d8502Sjsg {
589fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
590ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
591fb4d8502Sjsg 	u8 frev, crev;
592fb4d8502Sjsg 	int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
593fb4d8502Sjsg 	union set_pixel_clock args;
594fb4d8502Sjsg 
595fb4d8502Sjsg 	memset(&args, 0, sizeof(args));
596fb4d8502Sjsg 
597fb4d8502Sjsg 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
598fb4d8502Sjsg 				   &crev))
599fb4d8502Sjsg 		return;
600fb4d8502Sjsg 
601fb4d8502Sjsg 	switch (frev) {
602fb4d8502Sjsg 	case 1:
603fb4d8502Sjsg 		switch (crev) {
604fb4d8502Sjsg 		case 1:
605fb4d8502Sjsg 			if (clock == ATOM_DISABLE)
606fb4d8502Sjsg 				return;
607fb4d8502Sjsg 			args.v1.usPixelClock = cpu_to_le16(clock / 10);
608fb4d8502Sjsg 			args.v1.usRefDiv = cpu_to_le16(ref_div);
609fb4d8502Sjsg 			args.v1.usFbDiv = cpu_to_le16(fb_div);
610fb4d8502Sjsg 			args.v1.ucFracFbDiv = frac_fb_div;
611fb4d8502Sjsg 			args.v1.ucPostDiv = post_div;
612fb4d8502Sjsg 			args.v1.ucPpll = pll_id;
613fb4d8502Sjsg 			args.v1.ucCRTC = crtc_id;
614fb4d8502Sjsg 			args.v1.ucRefDivSrc = 1;
615fb4d8502Sjsg 			break;
616fb4d8502Sjsg 		case 2:
617fb4d8502Sjsg 			args.v2.usPixelClock = cpu_to_le16(clock / 10);
618fb4d8502Sjsg 			args.v2.usRefDiv = cpu_to_le16(ref_div);
619fb4d8502Sjsg 			args.v2.usFbDiv = cpu_to_le16(fb_div);
620fb4d8502Sjsg 			args.v2.ucFracFbDiv = frac_fb_div;
621fb4d8502Sjsg 			args.v2.ucPostDiv = post_div;
622fb4d8502Sjsg 			args.v2.ucPpll = pll_id;
623fb4d8502Sjsg 			args.v2.ucCRTC = crtc_id;
624fb4d8502Sjsg 			args.v2.ucRefDivSrc = 1;
625fb4d8502Sjsg 			break;
626fb4d8502Sjsg 		case 3:
627fb4d8502Sjsg 			args.v3.usPixelClock = cpu_to_le16(clock / 10);
628fb4d8502Sjsg 			args.v3.usRefDiv = cpu_to_le16(ref_div);
629fb4d8502Sjsg 			args.v3.usFbDiv = cpu_to_le16(fb_div);
630fb4d8502Sjsg 			args.v3.ucFracFbDiv = frac_fb_div;
631fb4d8502Sjsg 			args.v3.ucPostDiv = post_div;
632fb4d8502Sjsg 			args.v3.ucPpll = pll_id;
633fb4d8502Sjsg 			if (crtc_id == ATOM_CRTC2)
634fb4d8502Sjsg 				args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
635fb4d8502Sjsg 			else
636fb4d8502Sjsg 				args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
637fb4d8502Sjsg 			if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
638fb4d8502Sjsg 				args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
639fb4d8502Sjsg 			args.v3.ucTransmitterId = encoder_id;
640fb4d8502Sjsg 			args.v3.ucEncoderMode = encoder_mode;
641fb4d8502Sjsg 			break;
642fb4d8502Sjsg 		case 5:
643fb4d8502Sjsg 			args.v5.ucCRTC = crtc_id;
644fb4d8502Sjsg 			args.v5.usPixelClock = cpu_to_le16(clock / 10);
645fb4d8502Sjsg 			args.v5.ucRefDiv = ref_div;
646fb4d8502Sjsg 			args.v5.usFbDiv = cpu_to_le16(fb_div);
647fb4d8502Sjsg 			args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
648fb4d8502Sjsg 			args.v5.ucPostDiv = post_div;
649fb4d8502Sjsg 			args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
650fb4d8502Sjsg 			if ((ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK)) &&
651fb4d8502Sjsg 			    (pll_id < ATOM_EXT_PLL1))
652fb4d8502Sjsg 				args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
653fb4d8502Sjsg 			if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
654fb4d8502Sjsg 				switch (bpc) {
655fb4d8502Sjsg 				case 8:
656fb4d8502Sjsg 				default:
657fb4d8502Sjsg 					args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
658fb4d8502Sjsg 					break;
659fb4d8502Sjsg 				case 10:
660fb4d8502Sjsg 					/* yes this is correct, the atom define is wrong */
661fb4d8502Sjsg 					args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
662fb4d8502Sjsg 					break;
663fb4d8502Sjsg 				case 12:
664fb4d8502Sjsg 					/* yes this is correct, the atom define is wrong */
665fb4d8502Sjsg 					args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
666fb4d8502Sjsg 					break;
667fb4d8502Sjsg 				}
668fb4d8502Sjsg 			}
669fb4d8502Sjsg 			args.v5.ucTransmitterID = encoder_id;
670fb4d8502Sjsg 			args.v5.ucEncoderMode = encoder_mode;
671fb4d8502Sjsg 			args.v5.ucPpll = pll_id;
672fb4d8502Sjsg 			break;
673fb4d8502Sjsg 		case 6:
674fb4d8502Sjsg 			args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
675fb4d8502Sjsg 			args.v6.ucRefDiv = ref_div;
676fb4d8502Sjsg 			args.v6.usFbDiv = cpu_to_le16(fb_div);
677fb4d8502Sjsg 			args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
678fb4d8502Sjsg 			args.v6.ucPostDiv = post_div;
679fb4d8502Sjsg 			args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
680fb4d8502Sjsg 			if ((ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK)) &&
681fb4d8502Sjsg 			    (pll_id < ATOM_EXT_PLL1) &&
682fb4d8502Sjsg 			    !is_pixel_clock_source_from_pll(encoder_mode, pll_id))
683fb4d8502Sjsg 				args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
684fb4d8502Sjsg 			if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
685fb4d8502Sjsg 				switch (bpc) {
686fb4d8502Sjsg 				case 8:
687fb4d8502Sjsg 				default:
688fb4d8502Sjsg 					args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
689fb4d8502Sjsg 					break;
690fb4d8502Sjsg 				case 10:
691fb4d8502Sjsg 					args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
692fb4d8502Sjsg 					break;
693fb4d8502Sjsg 				case 12:
694fb4d8502Sjsg 					args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
695fb4d8502Sjsg 					break;
696fb4d8502Sjsg 				case 16:
697fb4d8502Sjsg 					args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
698fb4d8502Sjsg 					break;
699fb4d8502Sjsg 				}
700fb4d8502Sjsg 			}
701fb4d8502Sjsg 			args.v6.ucTransmitterID = encoder_id;
702fb4d8502Sjsg 			args.v6.ucEncoderMode = encoder_mode;
703fb4d8502Sjsg 			args.v6.ucPpll = pll_id;
704fb4d8502Sjsg 			break;
705fb4d8502Sjsg 		case 7:
706fb4d8502Sjsg 			args.v7.ulPixelClock = cpu_to_le32(clock * 10); /* 100 hz units */
707fb4d8502Sjsg 			args.v7.ucMiscInfo = 0;
708fb4d8502Sjsg 			if ((encoder_mode == ATOM_ENCODER_MODE_DVI) &&
709fb4d8502Sjsg 			    (clock > 165000))
710fb4d8502Sjsg 				args.v7.ucMiscInfo |= PIXEL_CLOCK_V7_MISC_DVI_DUALLINK_EN;
711fb4d8502Sjsg 			args.v7.ucCRTC = crtc_id;
712fb4d8502Sjsg 			if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
713fb4d8502Sjsg 				switch (bpc) {
714fb4d8502Sjsg 				case 8:
715fb4d8502Sjsg 				default:
716fb4d8502Sjsg 					args.v7.ucDeepColorRatio = PIXEL_CLOCK_V7_DEEPCOLOR_RATIO_DIS;
717fb4d8502Sjsg 					break;
718fb4d8502Sjsg 				case 10:
719fb4d8502Sjsg 					args.v7.ucDeepColorRatio = PIXEL_CLOCK_V7_DEEPCOLOR_RATIO_5_4;
720fb4d8502Sjsg 					break;
721fb4d8502Sjsg 				case 12:
722fb4d8502Sjsg 					args.v7.ucDeepColorRatio = PIXEL_CLOCK_V7_DEEPCOLOR_RATIO_3_2;
723fb4d8502Sjsg 					break;
724fb4d8502Sjsg 				case 16:
725fb4d8502Sjsg 					args.v7.ucDeepColorRatio = PIXEL_CLOCK_V7_DEEPCOLOR_RATIO_2_1;
726fb4d8502Sjsg 					break;
727fb4d8502Sjsg 				}
728fb4d8502Sjsg 			}
729fb4d8502Sjsg 			args.v7.ucTransmitterID = encoder_id;
730fb4d8502Sjsg 			args.v7.ucEncoderMode = encoder_mode;
731fb4d8502Sjsg 			args.v7.ucPpll = pll_id;
732fb4d8502Sjsg 			break;
733fb4d8502Sjsg 		default:
734fb4d8502Sjsg 			DRM_ERROR("Unknown table version %d %d\n", frev, crev);
735fb4d8502Sjsg 			return;
736fb4d8502Sjsg 		}
737fb4d8502Sjsg 		break;
738fb4d8502Sjsg 	default:
739fb4d8502Sjsg 		DRM_ERROR("Unknown table version %d %d\n", frev, crev);
740fb4d8502Sjsg 		return;
741fb4d8502Sjsg 	}
742fb4d8502Sjsg 
743fb4d8502Sjsg 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
744fb4d8502Sjsg }
745fb4d8502Sjsg 
amdgpu_atombios_crtc_prepare_pll(struct drm_crtc * crtc,struct drm_display_mode * mode)746fb4d8502Sjsg int amdgpu_atombios_crtc_prepare_pll(struct drm_crtc *crtc,
747fb4d8502Sjsg 			      struct drm_display_mode *mode)
748fb4d8502Sjsg {
749fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
750fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
751ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
752fb4d8502Sjsg 	struct amdgpu_encoder *amdgpu_encoder =
753fb4d8502Sjsg 		to_amdgpu_encoder(amdgpu_crtc->encoder);
754fb4d8502Sjsg 	int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
755fb4d8502Sjsg 
756fb4d8502Sjsg 	amdgpu_crtc->bpc = 8;
757fb4d8502Sjsg 	amdgpu_crtc->ss_enabled = false;
758fb4d8502Sjsg 
759fb4d8502Sjsg 	if ((amdgpu_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
760fb4d8502Sjsg 	    (amdgpu_encoder_get_dp_bridge_encoder_id(amdgpu_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
761fb4d8502Sjsg 		struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
762fb4d8502Sjsg 		struct drm_connector *connector =
763fb4d8502Sjsg 			amdgpu_get_connector_for_encoder(amdgpu_crtc->encoder);
764fb4d8502Sjsg 		struct amdgpu_connector *amdgpu_connector =
765fb4d8502Sjsg 			to_amdgpu_connector(connector);
766fb4d8502Sjsg 		struct amdgpu_connector_atom_dig *dig_connector =
767fb4d8502Sjsg 			amdgpu_connector->con_priv;
768fb4d8502Sjsg 		int dp_clock;
769fb4d8502Sjsg 
770fb4d8502Sjsg 		/* Assign mode clock for hdmi deep color max clock limit check */
771fb4d8502Sjsg 		amdgpu_connector->pixelclock_for_modeset = mode->clock;
772fb4d8502Sjsg 		amdgpu_crtc->bpc = amdgpu_connector_get_monitor_bpc(connector);
773fb4d8502Sjsg 
774fb4d8502Sjsg 		switch (encoder_mode) {
775fb4d8502Sjsg 		case ATOM_ENCODER_MODE_DP_MST:
776fb4d8502Sjsg 		case ATOM_ENCODER_MODE_DP:
777fb4d8502Sjsg 			/* DP/eDP */
778fb4d8502Sjsg 			dp_clock = dig_connector->dp_clock / 10;
779fb4d8502Sjsg 			amdgpu_crtc->ss_enabled =
780fb4d8502Sjsg 				amdgpu_atombios_get_asic_ss_info(adev, &amdgpu_crtc->ss,
781fb4d8502Sjsg 								 ASIC_INTERNAL_SS_ON_DP,
782fb4d8502Sjsg 								 dp_clock);
783fb4d8502Sjsg 			break;
784fb4d8502Sjsg 		case ATOM_ENCODER_MODE_LVDS:
785fb4d8502Sjsg 			amdgpu_crtc->ss_enabled =
786fb4d8502Sjsg 				amdgpu_atombios_get_asic_ss_info(adev,
787fb4d8502Sjsg 								 &amdgpu_crtc->ss,
788fb4d8502Sjsg 								 dig->lcd_ss_id,
789fb4d8502Sjsg 								 mode->clock / 10);
790fb4d8502Sjsg 			break;
791fb4d8502Sjsg 		case ATOM_ENCODER_MODE_DVI:
792fb4d8502Sjsg 			amdgpu_crtc->ss_enabled =
793fb4d8502Sjsg 				amdgpu_atombios_get_asic_ss_info(adev,
794fb4d8502Sjsg 								 &amdgpu_crtc->ss,
795fb4d8502Sjsg 								 ASIC_INTERNAL_SS_ON_TMDS,
796fb4d8502Sjsg 								 mode->clock / 10);
797fb4d8502Sjsg 			break;
798fb4d8502Sjsg 		case ATOM_ENCODER_MODE_HDMI:
799fb4d8502Sjsg 			amdgpu_crtc->ss_enabled =
800fb4d8502Sjsg 				amdgpu_atombios_get_asic_ss_info(adev,
801fb4d8502Sjsg 								 &amdgpu_crtc->ss,
802fb4d8502Sjsg 								 ASIC_INTERNAL_SS_ON_HDMI,
803fb4d8502Sjsg 								 mode->clock / 10);
804fb4d8502Sjsg 			break;
805fb4d8502Sjsg 		default:
806fb4d8502Sjsg 			break;
807fb4d8502Sjsg 		}
808fb4d8502Sjsg 	}
809fb4d8502Sjsg 
810fb4d8502Sjsg 	/* adjust pixel clock as needed */
811fb4d8502Sjsg 	amdgpu_crtc->adjusted_clock = amdgpu_atombios_crtc_adjust_pll(crtc, mode);
812fb4d8502Sjsg 
813fb4d8502Sjsg 	return 0;
814fb4d8502Sjsg }
815fb4d8502Sjsg 
amdgpu_atombios_crtc_set_pll(struct drm_crtc * crtc,struct drm_display_mode * mode)816fb4d8502Sjsg void amdgpu_atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
817fb4d8502Sjsg {
818fb4d8502Sjsg 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
819fb4d8502Sjsg 	struct drm_device *dev = crtc->dev;
820ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
821fb4d8502Sjsg 	struct amdgpu_encoder *amdgpu_encoder =
822fb4d8502Sjsg 		to_amdgpu_encoder(amdgpu_crtc->encoder);
823fb4d8502Sjsg 	u32 pll_clock = mode->clock;
824fb4d8502Sjsg 	u32 clock = mode->clock;
825fb4d8502Sjsg 	u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
826fb4d8502Sjsg 	struct amdgpu_pll *pll;
827fb4d8502Sjsg 	int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
828fb4d8502Sjsg 
829fb4d8502Sjsg 	/* pass the actual clock to amdgpu_atombios_crtc_program_pll for HDMI */
830fb4d8502Sjsg 	if ((encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
831fb4d8502Sjsg 	    (amdgpu_crtc->bpc > 8))
832fb4d8502Sjsg 		clock = amdgpu_crtc->adjusted_clock;
833fb4d8502Sjsg 
834fb4d8502Sjsg 	switch (amdgpu_crtc->pll_id) {
835fb4d8502Sjsg 	case ATOM_PPLL1:
836fb4d8502Sjsg 		pll = &adev->clock.ppll[0];
837fb4d8502Sjsg 		break;
838fb4d8502Sjsg 	case ATOM_PPLL2:
839fb4d8502Sjsg 		pll = &adev->clock.ppll[1];
840fb4d8502Sjsg 		break;
841fb4d8502Sjsg 	case ATOM_PPLL0:
842fb4d8502Sjsg 	case ATOM_PPLL_INVALID:
843fb4d8502Sjsg 	default:
844fb4d8502Sjsg 		pll = &adev->clock.ppll[2];
845fb4d8502Sjsg 		break;
846fb4d8502Sjsg 	}
847fb4d8502Sjsg 
848fb4d8502Sjsg 	/* update pll params */
849fb4d8502Sjsg 	pll->flags = amdgpu_crtc->pll_flags;
850fb4d8502Sjsg 	pll->reference_div = amdgpu_crtc->pll_reference_div;
851fb4d8502Sjsg 	pll->post_div = amdgpu_crtc->pll_post_div;
852fb4d8502Sjsg 
853*5ca02815Sjsg 	amdgpu_pll_compute(adev, pll, amdgpu_crtc->adjusted_clock, &pll_clock,
854fb4d8502Sjsg 			    &fb_div, &frac_fb_div, &ref_div, &post_div);
855fb4d8502Sjsg 
856fb4d8502Sjsg 	amdgpu_atombios_crtc_program_ss(adev, ATOM_DISABLE, amdgpu_crtc->pll_id,
857fb4d8502Sjsg 				 amdgpu_crtc->crtc_id, &amdgpu_crtc->ss);
858fb4d8502Sjsg 
859fb4d8502Sjsg 	amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
860fb4d8502Sjsg 				  encoder_mode, amdgpu_encoder->encoder_id, clock,
861fb4d8502Sjsg 				  ref_div, fb_div, frac_fb_div, post_div,
862fb4d8502Sjsg 				  amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
863fb4d8502Sjsg 
864fb4d8502Sjsg 	if (amdgpu_crtc->ss_enabled) {
865fb4d8502Sjsg 		/* calculate ss amount and step size */
866fb4d8502Sjsg 		u32 step_size;
867fb4d8502Sjsg 		u32 amount = (((fb_div * 10) + frac_fb_div) *
868fb4d8502Sjsg 			      (u32)amdgpu_crtc->ss.percentage) /
869fb4d8502Sjsg 			(100 * (u32)amdgpu_crtc->ss.percentage_divider);
870fb4d8502Sjsg 		amdgpu_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
871fb4d8502Sjsg 		amdgpu_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
872fb4d8502Sjsg 			ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
873fb4d8502Sjsg 		if (amdgpu_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
874fb4d8502Sjsg 			step_size = (4 * amount * ref_div * ((u32)amdgpu_crtc->ss.rate * 2048)) /
875fb4d8502Sjsg 				(125 * 25 * pll->reference_freq / 100);
876fb4d8502Sjsg 		else
877fb4d8502Sjsg 			step_size = (2 * amount * ref_div * ((u32)amdgpu_crtc->ss.rate * 2048)) /
878fb4d8502Sjsg 				(125 * 25 * pll->reference_freq / 100);
879fb4d8502Sjsg 		amdgpu_crtc->ss.step = step_size;
880fb4d8502Sjsg 
881fb4d8502Sjsg 		amdgpu_atombios_crtc_program_ss(adev, ATOM_ENABLE, amdgpu_crtc->pll_id,
882fb4d8502Sjsg 					 amdgpu_crtc->crtc_id, &amdgpu_crtc->ss);
883fb4d8502Sjsg 	}
884fb4d8502Sjsg }
885fb4d8502Sjsg 
886