blob: cf8ac5a5d8cad6cb23b0a694f8f75776232d1dd6 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Dave Airlie4ce001a2009-08-13 16:32:14 +100086
Jerome Glissec93bb852009-07-13 21:04:08 +020087 /* fixme - fill in enc_priv for atom dac */
88 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100089 bool is_tv = false, is_cv = false;
90 struct drm_encoder *encoder;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Dave Airlie4ce001a2009-08-13 16:32:14 +100095 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
96 /* find tv std */
97 if (encoder->crtc == crtc) {
98 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
99 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
100 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
101 tv_std = tv_dac->tv_std;
102 is_tv = true;
103 }
104 }
105 }
106
Jerome Glissec93bb852009-07-13 21:04:08 +0200107 memset(&args, 0, sizeof(args));
108
109 args.ucScaler = radeon_crtc->crtc_id;
110
Dave Airlie4ce001a2009-08-13 16:32:14 +1000111 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200112 switch (tv_std) {
113 case TV_STD_NTSC:
114 default:
115 args.ucTVStandard = ATOM_TV_NTSC;
116 break;
117 case TV_STD_PAL:
118 args.ucTVStandard = ATOM_TV_PAL;
119 break;
120 case TV_STD_PAL_M:
121 args.ucTVStandard = ATOM_TV_PALM;
122 break;
123 case TV_STD_PAL_60:
124 args.ucTVStandard = ATOM_TV_PAL60;
125 break;
126 case TV_STD_NTSC_J:
127 args.ucTVStandard = ATOM_TV_NTSCJ;
128 break;
129 case TV_STD_SCART_PAL:
130 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
131 break;
132 case TV_STD_SECAM:
133 args.ucTVStandard = ATOM_TV_SECAM;
134 break;
135 case TV_STD_PAL_CN:
136 args.ucTVStandard = ATOM_TV_PALCN;
137 break;
138 }
139 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000140 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200141 args.ucTVStandard = ATOM_TV_CV;
142 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
143 } else {
144 switch (radeon_crtc->rmx_type) {
145 case RMX_FULL:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 case RMX_CENTER:
149 args.ucEnable = ATOM_SCALER_CENTER;
150 break;
151 case RMX_ASPECT:
152 args.ucEnable = ATOM_SCALER_EXPANSION;
153 break;
154 default:
155 if (ASIC_IS_AVIVO(rdev))
156 args.ucEnable = ATOM_SCALER_DISABLE;
157 else
158 args.ucEnable = ATOM_SCALER_CENTER;
159 break;
160 }
161 }
162 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000163 if ((is_tv || is_cv)
164 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
165 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200166 }
167}
168
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
170{
171 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
172 struct drm_device *dev = crtc->dev;
173 struct radeon_device *rdev = dev->dev_private;
174 int index =
175 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
176 ENABLE_CRTC_PS_ALLOCATION args;
177
178 memset(&args, 0, sizeof(args));
179
180 args.ucCRTC = radeon_crtc->crtc_id;
181 args.ucEnable = lock;
182
183 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
184}
185
186static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
187{
188 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
189 struct drm_device *dev = crtc->dev;
190 struct radeon_device *rdev = dev->dev_private;
191 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
192 ENABLE_CRTC_PS_ALLOCATION args;
193
194 memset(&args, 0, sizeof(args));
195
196 args.ucCRTC = radeon_crtc->crtc_id;
197 args.ucEnable = state;
198
199 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
200}
201
202static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
203{
204 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
205 struct drm_device *dev = crtc->dev;
206 struct radeon_device *rdev = dev->dev_private;
207 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
208 ENABLE_CRTC_PS_ALLOCATION args;
209
210 memset(&args, 0, sizeof(args));
211
212 args.ucCRTC = radeon_crtc->crtc_id;
213 args.ucEnable = state;
214
215 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
216}
217
218static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
219{
220 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
221 struct drm_device *dev = crtc->dev;
222 struct radeon_device *rdev = dev->dev_private;
223 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
224 BLANK_CRTC_PS_ALLOCATION args;
225
226 memset(&args, 0, sizeof(args));
227
228 args.ucCRTC = radeon_crtc->crtc_id;
229 args.ucBlanking = state;
230
231 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
232}
233
Alex Deucherfef9f912012-03-20 17:18:03 -0400234static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
235{
236 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
237 struct drm_device *dev = crtc->dev;
238 struct radeon_device *rdev = dev->dev_private;
239 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
240 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
241
242 memset(&args, 0, sizeof(args));
243
244 args.ucDispPipeId = radeon_crtc->crtc_id;
245 args.ucEnable = state;
246
247 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
248}
249
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200250void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
251{
252 struct drm_device *dev = crtc->dev;
253 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500254 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200255
256 switch (mode) {
257 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400258 radeon_crtc->enabled = true;
259 /* adjust pm to dpms changes BEFORE enabling crtcs */
260 radeon_pm_compute_clocks(rdev);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -0400261 if (ASIC_IS_DCE6(rdev) && !radeon_crtc->in_mode_set)
Alex Deucherfef9f912012-03-20 17:18:03 -0400262 atombios_powergate_crtc(crtc, ATOM_DISABLE);
Alex Deucher37b43902010-02-09 12:04:43 -0500263 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400264 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500265 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
266 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400267 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500268 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200269 break;
270 case DRM_MODE_DPMS_STANDBY:
271 case DRM_MODE_DPMS_SUSPEND:
272 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400273 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500274 if (radeon_crtc->enabled)
275 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400276 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500277 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
278 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400279 radeon_crtc->enabled = false;
Alex Deucherc205b232012-08-24 18:21:21 -0400280 if (ASIC_IS_DCE6(rdev) && !radeon_crtc->in_mode_set)
281 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucherd7311172010-05-03 01:13:14 -0400282 /* adjust pm to dpms changes AFTER disabling crtcs */
283 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200284 break;
285 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200286}
287
288static void
289atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400290 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200291{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400292 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293 struct drm_device *dev = crtc->dev;
294 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400295 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400297 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200298
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400299 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400300 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400301 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400302 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
303 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400304 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400305 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400306 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400307 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400308 args.usH_SyncWidth =
309 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
310 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400311 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400312 args.usV_SyncWidth =
313 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400314 args.ucH_Border = radeon_crtc->h_border;
315 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316
317 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
318 misc |= ATOM_VSYNC_POLARITY;
319 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
320 misc |= ATOM_HSYNC_POLARITY;
321 if (mode->flags & DRM_MODE_FLAG_CSYNC)
322 misc |= ATOM_COMPOSITESYNC;
323 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
324 misc |= ATOM_INTERLACE;
325 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
326 misc |= ATOM_DOUBLE_CLOCK_MODE;
327
328 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
329 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200330
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400331 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332}
333
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400334static void atombios_crtc_set_timing(struct drm_crtc *crtc,
335 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200336{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400337 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338 struct drm_device *dev = crtc->dev;
339 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400340 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200341 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400342 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200343
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400344 memset(&args, 0, sizeof(args));
345 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
346 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
347 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
348 args.usH_SyncWidth =
349 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
350 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
351 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
352 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
353 args.usV_SyncWidth =
354 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
355
Alex Deucher54bfe492010-09-03 15:52:53 -0400356 args.ucOverscanRight = radeon_crtc->h_border;
357 args.ucOverscanLeft = radeon_crtc->h_border;
358 args.ucOverscanBottom = radeon_crtc->v_border;
359 args.ucOverscanTop = radeon_crtc->v_border;
360
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400361 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
362 misc |= ATOM_VSYNC_POLARITY;
363 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
364 misc |= ATOM_HSYNC_POLARITY;
365 if (mode->flags & DRM_MODE_FLAG_CSYNC)
366 misc |= ATOM_COMPOSITESYNC;
367 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
368 misc |= ATOM_INTERLACE;
369 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
370 misc |= ATOM_DOUBLE_CLOCK_MODE;
371
372 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
373 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200374
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400375 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200376}
377
Alex Deucher3fa47d92012-01-20 14:56:39 -0500378static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500379{
Alex Deucherb7922102010-03-06 10:57:30 -0500380 u32 ss_cntl;
381
382 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500383 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500384 case ATOM_PPLL1:
385 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
386 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
387 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
388 break;
389 case ATOM_PPLL2:
390 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
391 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
392 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
393 break;
394 case ATOM_DCPLL:
395 case ATOM_PPLL_INVALID:
396 return;
397 }
398 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500399 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500400 case ATOM_PPLL1:
401 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
402 ss_cntl &= ~1;
403 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
404 break;
405 case ATOM_PPLL2:
406 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
407 ss_cntl &= ~1;
408 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
409 break;
410 case ATOM_DCPLL:
411 case ATOM_PPLL_INVALID:
412 return;
413 }
414 }
415}
416
417
Alex Deucher26b9fc32010-02-01 16:39:11 -0500418union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400419 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
420 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500421 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400422 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500423 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500424};
425
Alex Deucher3fa47d92012-01-20 14:56:39 -0500426static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400427 int enable,
428 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400429 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400430 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400431{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400432 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400433 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500434 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400435
Jerome Glisse5efcc762012-08-17 14:40:04 -0400436 if (!enable) {
Alex Deucher53176702012-08-21 18:52:56 -0400437 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400438 if (rdev->mode_info.crtcs[i] &&
439 rdev->mode_info.crtcs[i]->enabled &&
440 i != crtc_id &&
441 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
442 /* one other crtc is using this pll don't turn
443 * off spread spectrum as it might turn off
444 * display on active crtc
445 */
446 return;
447 }
448 }
449 }
450
Alex Deucher26b9fc32010-02-01 16:39:11 -0500451 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400452
Alex Deuchera572eaa2011-01-06 21:19:16 -0500453 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500454 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400455 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500456 switch (pll_id) {
457 case ATOM_PPLL1:
458 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500459 break;
460 case ATOM_PPLL2:
461 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500462 break;
463 case ATOM_DCPLL:
464 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500465 break;
466 case ATOM_PPLL_INVALID:
467 return;
468 }
Alex Deucherf312f092012-07-17 14:02:44 -0400469 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
470 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400471 args.v3.ucEnable = enable;
Alex Deucher0671bdd72012-03-20 17:18:34 -0400472 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK) || ASIC_IS_DCE61(rdev))
Alex Deucher8e8e5232011-05-20 04:34:16 -0400473 args.v3.ucEnable = ATOM_DISABLE;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500474 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400475 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400476 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400477 switch (pll_id) {
478 case ATOM_PPLL1:
479 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400480 break;
481 case ATOM_PPLL2:
482 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400483 break;
484 case ATOM_DCPLL:
485 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400486 break;
487 case ATOM_PPLL_INVALID:
488 return;
489 }
Alex Deucherf312f092012-07-17 14:02:44 -0400490 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
491 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400492 args.v2.ucEnable = enable;
Alex Deucher09cc6502011-10-12 18:44:33 -0400493 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK) || ASIC_IS_DCE41(rdev))
Alex Deucher8e8e5232011-05-20 04:34:16 -0400494 args.v2.ucEnable = ATOM_DISABLE;
Alex Deucherba032a52010-10-04 17:13:01 -0400495 } else if (ASIC_IS_DCE3(rdev)) {
496 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400497 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400498 args.v1.ucSpreadSpectrumStep = ss->step;
499 args.v1.ucSpreadSpectrumDelay = ss->delay;
500 args.v1.ucSpreadSpectrumRange = ss->range;
501 args.v1.ucPpll = pll_id;
502 args.v1.ucEnable = enable;
503 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400504 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
505 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500506 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400507 return;
508 }
509 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400510 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400511 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
512 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
513 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
514 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400515 } else {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400516 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
517 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500518 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400519 return;
520 }
521 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400522 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400523 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
524 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
525 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400526 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500527 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400528}
529
Alex Deucher4eaeca32010-01-19 17:32:27 -0500530union adjust_pixel_clock {
531 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500532 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500533};
534
535static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400536 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200537{
Alex Deucher19eca432012-09-13 10:56:16 -0400538 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200539 struct drm_device *dev = crtc->dev;
540 struct radeon_device *rdev = dev->dev_private;
541 struct drm_encoder *encoder = NULL;
542 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucherdf271be2011-05-20 04:34:15 -0400543 struct drm_connector *connector = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500544 u32 adjusted_clock = mode->clock;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500545 int encoder_mode = 0;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400546 u32 dp_clock = mode->clock;
547 int bpc = 8;
Alex Deucher9aa59992012-01-20 15:03:30 -0500548 bool is_duallink = false;
Alex Deucherfc103322010-01-19 17:16:10 -0500549
Alex Deucher4eaeca32010-01-19 17:32:27 -0500550 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400551 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552
553 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400554 if ((rdev->family == CHIP_RS600) ||
555 (rdev->family == CHIP_RS690) ||
556 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400557 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
558 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000559
560 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400561 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000562 else
Alex Deucher19eca432012-09-13 10:56:16 -0400563 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400564
Alex Deucher5785e532011-04-19 15:24:59 -0400565 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400566 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400567 /* use frac fb div on APUs */
568 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400569 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000570 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400571 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200572
Dave Airlie5480f722010-10-19 10:36:47 +1000573 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400574 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000575 else
Alex Deucher19eca432012-09-13 10:56:16 -0400576 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000577 }
578
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200579 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
580 if (encoder->crtc == crtc) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500581 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherdf271be2011-05-20 04:34:15 -0400582 connector = radeon_get_connector_for_encoder(encoder);
Alex Deuchereccea792012-03-26 15:12:54 -0400583 bpc = radeon_get_monitor_bpc(connector);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500584 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucher9aa59992012-01-20 15:03:30 -0500585 is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deuchereac4dff2011-05-20 04:34:22 -0400586 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400587 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucherfbee67a2010-08-16 12:44:47 -0400588 if (connector) {
589 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
590 struct radeon_connector_atom_dig *dig_connector =
591 radeon_connector->con_priv;
592
593 dp_clock = dig_connector->dp_clock;
594 }
595 }
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500596
Alex Deucherba032a52010-10-04 17:13:01 -0400597 /* use recommended ref_div for ss */
598 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400599 if (radeon_crtc->ss_enabled) {
600 if (radeon_crtc->ss.refdiv) {
601 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
602 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500603 if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400604 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucherba032a52010-10-04 17:13:01 -0400605 }
606 }
607 }
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500608
Alex Deucher4eaeca32010-01-19 17:32:27 -0500609 if (ASIC_IS_AVIVO(rdev)) {
610 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
611 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
612 adjusted_clock = mode->clock * 2;
Alex Deucher48dfaae2010-09-29 11:37:41 -0400613 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
Alex Deucher19eca432012-09-13 10:56:16 -0400614 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
Alex Deucher5b40ddf2011-02-14 11:43:11 -0500615 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
Alex Deucher19eca432012-09-13 10:56:16 -0400616 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500617 } else {
618 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
Alex Deucher19eca432012-09-13 10:56:16 -0400619 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500620 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
Alex Deucher19eca432012-09-13 10:56:16 -0400621 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200622 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000623 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200624 }
625 }
626
Alex Deucher2606c882009-10-08 13:36:21 -0400627 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
628 * accordingly based on the encoder/transmitter to work around
629 * special hw requirements.
630 */
631 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500632 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500633 u8 frev, crev;
634 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400635
Alex Deucher2606c882009-10-08 13:36:21 -0400636 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400637 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
638 &crev))
639 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500640
641 memset(&args, 0, sizeof(args));
642
643 switch (frev) {
644 case 1:
645 switch (crev) {
646 case 1:
647 case 2:
648 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
649 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500650 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400651 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400652 args.v1.ucConfig |=
653 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500654
655 atom_execute_table(rdev->mode_info.atom_context,
656 index, (uint32_t *)&args);
657 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
658 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500659 case 3:
660 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
661 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
662 args.v3.sInput.ucEncodeMode = encoder_mode;
663 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400664 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000665 args.v3.sInput.ucDispPllConfig |=
666 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400667 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400668 args.v3.sInput.ucDispPllConfig |=
669 DISPPLL_CONFIG_COHERENT_MODE;
670 /* 16200 or 27000 */
671 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
672 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500673 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400674 if (encoder_mode == ATOM_ENCODER_MODE_HDMI)
675 /* deep color support */
676 args.v3.sInput.usPixelClock =
677 cpu_to_le16((mode->clock * bpc / 8) / 10);
678 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500679 args.v3.sInput.ucDispPllConfig |=
680 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500681 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500682 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400683 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500684 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400685 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
686 ENCODER_OBJECT_ID_NONE)
687 args.v3.sInput.ucExtTransmitterID =
688 radeon_encoder_get_dp_bridge_encoder_id(encoder);
689 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400690 args.v3.sInput.ucExtTransmitterID = 0;
691
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500692 atom_execute_table(rdev->mode_info.atom_context,
693 index, (uint32_t *)&args);
694 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
695 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400696 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
697 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
698 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500699 }
700 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400701 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
702 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
703 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500704 }
705 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500706 default:
707 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
708 return adjusted_clock;
709 }
710 break;
711 default:
712 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
713 return adjusted_clock;
714 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400715 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500716 return adjusted_clock;
717}
718
719union set_pixel_clock {
720 SET_PIXEL_CLOCK_PS_ALLOCATION base;
721 PIXEL_CLOCK_PARAMETERS v1;
722 PIXEL_CLOCK_PARAMETERS_V2 v2;
723 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500724 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500725 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500726};
727
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500728/* on DCE5, make sure the voltage is high enough to support the
729 * required disp clk.
730 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400731static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500732 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500733{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500734 u8 frev, crev;
735 int index;
736 union set_pixel_clock args;
737
738 memset(&args, 0, sizeof(args));
739
740 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400741 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
742 &crev))
743 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500744
745 switch (frev) {
746 case 1:
747 switch (crev) {
748 case 5:
749 /* if the default dcpll clock is specified,
750 * SetPixelClock provides the dividers
751 */
752 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500753 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500754 args.v5.ucPpll = ATOM_DCPLL;
755 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500756 case 6:
757 /* if the default dcpll clock is specified,
758 * SetPixelClock provides the dividers
759 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500760 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher729b95e2012-03-20 17:18:31 -0400761 if (ASIC_IS_DCE61(rdev))
762 args.v6.ucPpll = ATOM_EXT_PLL1;
763 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400764 args.v6.ucPpll = ATOM_PPLL0;
765 else
766 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500767 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500768 default:
769 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
770 return;
771 }
772 break;
773 default:
774 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
775 return;
776 }
777 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
778}
779
Alex Deucher37f90032010-06-11 17:58:38 -0400780static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000781 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400782 int pll_id,
783 u32 encoder_mode,
784 u32 encoder_id,
785 u32 clock,
786 u32 ref_div,
787 u32 fb_div,
788 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400789 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400790 int bpc,
791 bool ss_enabled,
792 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400793{
794 struct drm_device *dev = crtc->dev;
795 struct radeon_device *rdev = dev->dev_private;
796 u8 frev, crev;
797 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
798 union set_pixel_clock args;
799
800 memset(&args, 0, sizeof(args));
801
802 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
803 &crev))
804 return;
805
806 switch (frev) {
807 case 1:
808 switch (crev) {
809 case 1:
810 if (clock == ATOM_DISABLE)
811 return;
812 args.v1.usPixelClock = cpu_to_le16(clock / 10);
813 args.v1.usRefDiv = cpu_to_le16(ref_div);
814 args.v1.usFbDiv = cpu_to_le16(fb_div);
815 args.v1.ucFracFbDiv = frac_fb_div;
816 args.v1.ucPostDiv = post_div;
817 args.v1.ucPpll = pll_id;
818 args.v1.ucCRTC = crtc_id;
819 args.v1.ucRefDivSrc = 1;
820 break;
821 case 2:
822 args.v2.usPixelClock = cpu_to_le16(clock / 10);
823 args.v2.usRefDiv = cpu_to_le16(ref_div);
824 args.v2.usFbDiv = cpu_to_le16(fb_div);
825 args.v2.ucFracFbDiv = frac_fb_div;
826 args.v2.ucPostDiv = post_div;
827 args.v2.ucPpll = pll_id;
828 args.v2.ucCRTC = crtc_id;
829 args.v2.ucRefDivSrc = 1;
830 break;
831 case 3:
832 args.v3.usPixelClock = cpu_to_le16(clock / 10);
833 args.v3.usRefDiv = cpu_to_le16(ref_div);
834 args.v3.usFbDiv = cpu_to_le16(fb_div);
835 args.v3.ucFracFbDiv = frac_fb_div;
836 args.v3.ucPostDiv = post_div;
837 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400838 if (crtc_id == ATOM_CRTC2)
839 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
840 else
841 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400842 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
843 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400844 args.v3.ucTransmitterId = encoder_id;
845 args.v3.ucEncoderMode = encoder_mode;
846 break;
847 case 5:
848 args.v5.ucCRTC = crtc_id;
849 args.v5.usPixelClock = cpu_to_le16(clock / 10);
850 args.v5.ucRefDiv = ref_div;
851 args.v5.usFbDiv = cpu_to_le16(fb_div);
852 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
853 args.v5.ucPostDiv = post_div;
854 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400855 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
856 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400857 switch (bpc) {
858 case 8:
859 default:
860 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
861 break;
862 case 10:
863 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
864 break;
865 }
Alex Deucher37f90032010-06-11 17:58:38 -0400866 args.v5.ucTransmitterID = encoder_id;
867 args.v5.ucEncoderMode = encoder_mode;
868 args.v5.ucPpll = pll_id;
869 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500870 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000871 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500872 args.v6.ucRefDiv = ref_div;
873 args.v6.usFbDiv = cpu_to_le16(fb_div);
874 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
875 args.v6.ucPostDiv = post_div;
876 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400877 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
878 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400879 switch (bpc) {
880 case 8:
881 default:
882 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
883 break;
884 case 10:
885 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
886 break;
887 case 12:
888 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
889 break;
890 case 16:
891 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
892 break;
893 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500894 args.v6.ucTransmitterID = encoder_id;
895 args.v6.ucEncoderMode = encoder_mode;
896 args.v6.ucPpll = pll_id;
897 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400898 default:
899 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
900 return;
901 }
902 break;
903 default:
904 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
905 return;
906 }
907
908 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
909}
910
Alex Deucher19eca432012-09-13 10:56:16 -0400911static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
912{
913 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
914 struct drm_device *dev = crtc->dev;
915 struct radeon_device *rdev = dev->dev_private;
916 struct drm_encoder *encoder = NULL;
917 struct radeon_encoder *radeon_encoder = NULL;
918 int encoder_mode = 0;
919
920 radeon_crtc->bpc = 8;
921 radeon_crtc->ss_enabled = false;
922
923 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
924 if (encoder->crtc == crtc) {
925 radeon_encoder = to_radeon_encoder(encoder);
926 encoder_mode = atombios_get_encoder_mode(encoder);
927 break;
928 }
929 }
930
931 if (!radeon_encoder)
932 return false;
933
934 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
935 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
936 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
937 struct drm_connector *connector =
938 radeon_get_connector_for_encoder(encoder);
939 struct radeon_connector *radeon_connector =
940 to_radeon_connector(connector);
941 struct radeon_connector_atom_dig *dig_connector =
942 radeon_connector->con_priv;
943 int dp_clock;
944 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
945
946 switch (encoder_mode) {
947 case ATOM_ENCODER_MODE_DP_MST:
948 case ATOM_ENCODER_MODE_DP:
949 /* DP/eDP */
950 dp_clock = dig_connector->dp_clock / 10;
951 if (ASIC_IS_DCE4(rdev))
952 radeon_crtc->ss_enabled =
953 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
954 ASIC_INTERNAL_SS_ON_DP,
955 dp_clock);
956 else {
957 if (dp_clock == 16200) {
958 radeon_crtc->ss_enabled =
959 radeon_atombios_get_ppll_ss_info(rdev,
960 &radeon_crtc->ss,
961 ATOM_DP_SS_ID2);
962 if (!radeon_crtc->ss_enabled)
963 radeon_crtc->ss_enabled =
964 radeon_atombios_get_ppll_ss_info(rdev,
965 &radeon_crtc->ss,
966 ATOM_DP_SS_ID1);
967 } else
968 radeon_crtc->ss_enabled =
969 radeon_atombios_get_ppll_ss_info(rdev,
970 &radeon_crtc->ss,
971 ATOM_DP_SS_ID1);
972 }
973 break;
974 case ATOM_ENCODER_MODE_LVDS:
975 if (ASIC_IS_DCE4(rdev))
976 radeon_crtc->ss_enabled =
977 radeon_atombios_get_asic_ss_info(rdev,
978 &radeon_crtc->ss,
979 dig->lcd_ss_id,
980 mode->clock / 10);
981 else
982 radeon_crtc->ss_enabled =
983 radeon_atombios_get_ppll_ss_info(rdev,
984 &radeon_crtc->ss,
985 dig->lcd_ss_id);
986 break;
987 case ATOM_ENCODER_MODE_DVI:
988 if (ASIC_IS_DCE4(rdev))
989 radeon_crtc->ss_enabled =
990 radeon_atombios_get_asic_ss_info(rdev,
991 &radeon_crtc->ss,
992 ASIC_INTERNAL_SS_ON_TMDS,
993 mode->clock / 10);
994 break;
995 case ATOM_ENCODER_MODE_HDMI:
996 if (ASIC_IS_DCE4(rdev))
997 radeon_crtc->ss_enabled =
998 radeon_atombios_get_asic_ss_info(rdev,
999 &radeon_crtc->ss,
1000 ASIC_INTERNAL_SS_ON_HDMI,
1001 mode->clock / 10);
1002 break;
1003 default:
1004 break;
1005 }
1006 }
1007
1008 /* adjust pixel clock as needed */
1009 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1010
1011 return true;
1012}
1013
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001014static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001015{
1016 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1017 struct drm_device *dev = crtc->dev;
1018 struct radeon_device *rdev = dev->dev_private;
1019 struct drm_encoder *encoder = NULL;
1020 struct radeon_encoder *radeon_encoder = NULL;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001021 u32 pll_clock = mode->clock;
1022 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1023 struct radeon_pll *pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001024 int encoder_mode = 0;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001025
Alex Deucher4eaeca32010-01-19 17:32:27 -05001026 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1027 if (encoder->crtc == crtc) {
1028 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001029 encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001030 break;
1031 }
1032 }
1033
1034 if (!radeon_encoder)
1035 return;
1036
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001037 switch (radeon_crtc->pll_id) {
1038 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001039 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001040 break;
1041 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001042 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001043 break;
1044 case ATOM_DCPLL:
1045 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001046 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001047 pll = &rdev->clock.dcpll;
1048 break;
1049 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001050
Alex Deucher19eca432012-09-13 10:56:16 -04001051 /* update pll params */
1052 pll->flags = radeon_crtc->pll_flags;
1053 pll->reference_div = radeon_crtc->pll_reference_div;
1054 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001055
Alex Deucher64146f82011-03-22 01:46:12 -04001056 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1057 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001058 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1059 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001060 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001061 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1062 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001063 else
Alex Deucher19eca432012-09-13 10:56:16 -04001064 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1065 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001066
Alex Deucher19eca432012-09-13 10:56:16 -04001067 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1068 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001069
Alex Deucher37f90032010-06-11 17:58:38 -04001070 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1071 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001072 ref_div, fb_div, frac_fb_div, post_div,
1073 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001074
Alex Deucher19eca432012-09-13 10:56:16 -04001075 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001076 /* calculate ss amount and step size */
1077 if (ASIC_IS_DCE4(rdev)) {
1078 u32 step_size;
Alex Deucher19eca432012-09-13 10:56:16 -04001079 u32 amount = (((fb_div * 10) + frac_fb_div) * radeon_crtc->ss.percentage) / 10000;
1080 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1081 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001082 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001083 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
1084 step_size = (4 * amount * ref_div * (radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001085 (125 * 25 * pll->reference_freq / 100);
1086 else
Alex Deucher19eca432012-09-13 10:56:16 -04001087 step_size = (2 * amount * ref_div * (radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001088 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001089 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001090 }
1091
Alex Deucher19eca432012-09-13 10:56:16 -04001092 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1093 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001094 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001095}
1096
Alex Deucherc9417bd2011-02-06 14:23:26 -05001097static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1098 struct drm_framebuffer *fb,
1099 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001100{
1101 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1102 struct drm_device *dev = crtc->dev;
1103 struct radeon_device *rdev = dev->dev_private;
1104 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001105 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001106 struct drm_gem_object *obj;
1107 struct radeon_bo *rbo;
1108 uint64_t fb_location;
1109 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001110 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001111 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001112 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001113 int r;
1114
1115 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001116 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001117 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001118 return 0;
1119 }
1120
Chris Ball4dd19b02010-09-26 06:47:23 -05001121 if (atomic) {
1122 radeon_fb = to_radeon_framebuffer(fb);
1123 target_fb = fb;
1124 }
1125 else {
1126 radeon_fb = to_radeon_framebuffer(crtc->fb);
1127 target_fb = crtc->fb;
1128 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001129
Chris Ball4dd19b02010-09-26 06:47:23 -05001130 /* If atomic, assume fb object is pinned & idle & fenced and
1131 * just update base pointers
1132 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001133 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001134 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001135 r = radeon_bo_reserve(rbo, false);
1136 if (unlikely(r != 0))
1137 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001138
1139 if (atomic)
1140 fb_location = radeon_bo_gpu_offset(rbo);
1141 else {
1142 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1143 if (unlikely(r != 0)) {
1144 radeon_bo_unreserve(rbo);
1145 return -EINVAL;
1146 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001147 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001148
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001149 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1150 radeon_bo_unreserve(rbo);
1151
Chris Ball4dd19b02010-09-26 06:47:23 -05001152 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001153 case 8:
1154 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1155 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1156 break;
1157 case 15:
1158 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1159 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1160 break;
1161 case 16:
1162 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1163 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001164#ifdef __BIG_ENDIAN
1165 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1166#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001167 break;
1168 case 24:
1169 case 32:
1170 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1171 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001172#ifdef __BIG_ENDIAN
1173 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1174#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001175 break;
1176 default:
1177 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001178 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001179 return -EINVAL;
1180 }
1181
Alex Deucher392e3722011-11-28 14:49:27 -05001182 if (tiling_flags & RADEON_TILING_MACRO) {
Alex Deucherb7019b22012-06-14 15:58:25 -04001183 if (rdev->family >= CHIP_TAHITI)
1184 tmp = rdev->config.si.tile_config;
1185 else if (rdev->family >= CHIP_CAYMAN)
Alex Deucher392e3722011-11-28 14:49:27 -05001186 tmp = rdev->config.cayman.tile_config;
1187 else
1188 tmp = rdev->config.evergreen.tile_config;
1189
1190 switch ((tmp & 0xf0) >> 4) {
1191 case 0: /* 4 banks */
1192 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1193 break;
1194 case 1: /* 8 banks */
1195 default:
1196 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1197 break;
1198 case 2: /* 16 banks */
1199 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1200 break;
1201 }
1202
Alex Deucher97d66322010-05-20 12:12:48 -04001203 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001204
1205 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
1206 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1207 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1208 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1209 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher392e3722011-11-28 14:49:27 -05001210 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001211 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1212
Alex Deucherb7019b22012-06-14 15:58:25 -04001213 if ((rdev->family == CHIP_TAHITI) ||
1214 (rdev->family == CHIP_PITCAIRN))
1215 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
1216 else if (rdev->family == CHIP_VERDE)
1217 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1218
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001219 switch (radeon_crtc->crtc_id) {
1220 case 0:
1221 WREG32(AVIVO_D1VGA_CONTROL, 0);
1222 break;
1223 case 1:
1224 WREG32(AVIVO_D2VGA_CONTROL, 0);
1225 break;
1226 case 2:
1227 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1228 break;
1229 case 3:
1230 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1231 break;
1232 case 4:
1233 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1234 break;
1235 case 5:
1236 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1237 break;
1238 default:
1239 break;
1240 }
1241
1242 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1243 upper_32_bits(fb_location));
1244 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1245 upper_32_bits(fb_location));
1246 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1247 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1248 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1249 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1250 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001251 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001252
1253 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1254 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1255 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1256 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001257 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1258 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001259
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001260 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001261 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1262 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1263
1264 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001265 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001266 x &= ~3;
1267 y &= ~1;
1268 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1269 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001270 viewport_w = crtc->mode.hdisplay;
1271 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001272 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001273 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001274
Alex Deucherfb9674b2011-04-02 09:15:50 -04001275 /* pageflip setup */
1276 /* make sure flip is at vb rather than hb */
1277 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1278 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1279 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1280
1281 /* set pageflip to happen anywhere in vblank interval */
1282 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1283
Chris Ball4dd19b02010-09-26 06:47:23 -05001284 if (!atomic && fb && fb != crtc->fb) {
1285 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001286 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001287 r = radeon_bo_reserve(rbo, false);
1288 if (unlikely(r != 0))
1289 return r;
1290 radeon_bo_unpin(rbo);
1291 radeon_bo_unreserve(rbo);
1292 }
1293
1294 /* Bytes per pixel may have changed */
1295 radeon_bandwidth_update(rdev);
1296
1297 return 0;
1298}
1299
Chris Ball4dd19b02010-09-26 06:47:23 -05001300static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1301 struct drm_framebuffer *fb,
1302 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001303{
1304 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1305 struct drm_device *dev = crtc->dev;
1306 struct radeon_device *rdev = dev->dev_private;
1307 struct radeon_framebuffer *radeon_fb;
1308 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001309 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001310 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001311 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001312 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001313 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001314 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001315 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001316
Jerome Glisse2de3b482009-11-17 14:08:55 -08001317 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001318 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001319 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001320 return 0;
1321 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001322
Chris Ball4dd19b02010-09-26 06:47:23 -05001323 if (atomic) {
1324 radeon_fb = to_radeon_framebuffer(fb);
1325 target_fb = fb;
1326 }
1327 else {
1328 radeon_fb = to_radeon_framebuffer(crtc->fb);
1329 target_fb = crtc->fb;
1330 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001331
1332 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001333 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001334 r = radeon_bo_reserve(rbo, false);
1335 if (unlikely(r != 0))
1336 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001337
1338 /* If atomic, assume fb object is pinned & idle & fenced and
1339 * just update base pointers
1340 */
1341 if (atomic)
1342 fb_location = radeon_bo_gpu_offset(rbo);
1343 else {
1344 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1345 if (unlikely(r != 0)) {
1346 radeon_bo_unreserve(rbo);
1347 return -EINVAL;
1348 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001349 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001350 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1351 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001352
Chris Ball4dd19b02010-09-26 06:47:23 -05001353 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001354 case 8:
1355 fb_format =
1356 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1357 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1358 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001359 case 15:
1360 fb_format =
1361 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1362 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1363 break;
1364 case 16:
1365 fb_format =
1366 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1367 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001368#ifdef __BIG_ENDIAN
1369 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1370#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001371 break;
1372 case 24:
1373 case 32:
1374 fb_format =
1375 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1376 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001377#ifdef __BIG_ENDIAN
1378 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1379#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001380 break;
1381 default:
1382 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001383 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001384 return -EINVAL;
1385 }
1386
Alex Deucher40c4ac12010-05-20 12:04:59 -04001387 if (rdev->family >= CHIP_R600) {
1388 if (tiling_flags & RADEON_TILING_MACRO)
1389 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1390 else if (tiling_flags & RADEON_TILING_MICRO)
1391 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1392 } else {
1393 if (tiling_flags & RADEON_TILING_MACRO)
1394 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001395
Alex Deucher40c4ac12010-05-20 12:04:59 -04001396 if (tiling_flags & RADEON_TILING_MICRO)
1397 fb_format |= AVIVO_D1GRPH_TILED;
1398 }
Dave Airliee024e112009-06-24 09:48:08 +10001399
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001400 if (radeon_crtc->crtc_id == 0)
1401 WREG32(AVIVO_D1VGA_CONTROL, 0);
1402 else
1403 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001404
1405 if (rdev->family >= CHIP_RV770) {
1406 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001407 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1408 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001409 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001410 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1411 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001412 }
1413 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001414 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1415 (u32) fb_location);
1416 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1417 radeon_crtc->crtc_offset, (u32) fb_location);
1418 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001419 if (rdev->family >= CHIP_R600)
1420 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001421
1422 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1423 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1424 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1425 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001426 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1427 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001428
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001429 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001430 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1431 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1432
1433 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001434 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001435 x &= ~3;
1436 y &= ~1;
1437 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1438 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001439 viewport_w = crtc->mode.hdisplay;
1440 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001441 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001442 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001443
Alex Deucherfb9674b2011-04-02 09:15:50 -04001444 /* pageflip setup */
1445 /* make sure flip is at vb rather than hb */
1446 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1447 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1448 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1449
1450 /* set pageflip to happen anywhere in vblank interval */
1451 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1452
Chris Ball4dd19b02010-09-26 06:47:23 -05001453 if (!atomic && fb && fb != crtc->fb) {
1454 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001455 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001456 r = radeon_bo_reserve(rbo, false);
1457 if (unlikely(r != 0))
1458 return r;
1459 radeon_bo_unpin(rbo);
1460 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001461 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001462
1463 /* Bytes per pixel may have changed */
1464 radeon_bandwidth_update(rdev);
1465
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001466 return 0;
1467}
1468
Alex Deucher54f088a2010-01-19 16:34:01 -05001469int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1470 struct drm_framebuffer *old_fb)
1471{
1472 struct drm_device *dev = crtc->dev;
1473 struct radeon_device *rdev = dev->dev_private;
1474
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001475 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001476 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001477 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001478 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001479 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001480 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1481}
1482
1483int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1484 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001485 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001486{
1487 struct drm_device *dev = crtc->dev;
1488 struct radeon_device *rdev = dev->dev_private;
1489
1490 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001491 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001492 else if (ASIC_IS_AVIVO(rdev))
1493 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1494 else
1495 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001496}
1497
Alex Deucher615e0cb2010-01-20 16:22:53 -05001498/* properly set additional regs when using atombios */
1499static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1500{
1501 struct drm_device *dev = crtc->dev;
1502 struct radeon_device *rdev = dev->dev_private;
1503 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1504 u32 disp_merge_cntl;
1505
1506 switch (radeon_crtc->crtc_id) {
1507 case 0:
1508 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1509 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1510 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1511 break;
1512 case 1:
1513 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1514 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1515 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1516 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1517 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1518 break;
1519 }
1520}
1521
Alex Deucherf3dd8502012-08-31 11:56:50 -04001522/**
1523 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1524 *
1525 * @crtc: drm crtc
1526 *
1527 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1528 */
1529static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1530{
1531 struct drm_device *dev = crtc->dev;
1532 struct drm_crtc *test_crtc;
1533 struct radeon_crtc *radeon_test_crtc;
1534 u32 pll_in_use = 0;
1535
1536 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1537 if (crtc == test_crtc)
1538 continue;
1539
1540 radeon_test_crtc = to_radeon_crtc(test_crtc);
1541 if (radeon_test_crtc->pll_id != ATOM_PPLL_INVALID)
1542 pll_in_use |= (1 << radeon_test_crtc->pll_id);
1543 }
1544 return pll_in_use;
1545}
1546
1547/**
1548 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1549 *
1550 * @crtc: drm crtc
1551 *
1552 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1553 * also in DP mode. For DP, a single PPLL can be used for all DP
1554 * crtcs/encoders.
1555 */
1556static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1557{
1558 struct drm_device *dev = crtc->dev;
1559 struct drm_encoder *test_encoder;
1560 struct radeon_crtc *radeon_test_crtc;
1561
1562 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1563 if (test_encoder->crtc && (test_encoder->crtc != crtc)) {
1564 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_encoder))) {
1565 /* for DP use the same PLL for all */
1566 radeon_test_crtc = to_radeon_crtc(test_encoder->crtc);
1567 if (radeon_test_crtc->pll_id != ATOM_PPLL_INVALID)
1568 return radeon_test_crtc->pll_id;
1569 }
1570 }
1571 }
1572 return ATOM_PPLL_INVALID;
1573}
1574
1575/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001576 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1577 *
1578 * @crtc: drm crtc
1579 * @encoder: drm encoder
1580 *
1581 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1582 * be shared (i.e., same clock).
1583 */
1584static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc,
1585 struct drm_encoder *encoder)
1586{
1587 struct drm_device *dev = crtc->dev;
1588 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1589 struct drm_encoder *test_encoder;
1590 struct radeon_crtc *radeon_test_crtc;
1591 struct radeon_encoder *test_radeon_encoder;
1592 u32 target_clock, test_clock;
1593
1594 if (radeon_encoder->native_mode.clock)
1595 target_clock = radeon_encoder->native_mode.clock;
1596 else
1597 target_clock = crtc->mode.clock;
1598
1599 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1600 if (test_encoder->crtc && (test_encoder->crtc != crtc)) {
1601 if (!ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_encoder))) {
1602 test_radeon_encoder = to_radeon_encoder(test_encoder);
1603 radeon_test_crtc = to_radeon_crtc(test_encoder->crtc);
1604 /* for non-DP check the clock */
1605 if (test_radeon_encoder->native_mode.clock)
1606 test_clock = test_radeon_encoder->native_mode.clock;
1607 else
1608 test_clock = test_encoder->crtc->mode.clock;
1609 if ((target_clock == test_clock) &&
1610 (radeon_test_crtc->pll_id != ATOM_PPLL_INVALID))
1611 return radeon_test_crtc->pll_id;
1612 }
1613 }
1614 }
1615 return ATOM_PPLL_INVALID;
1616}
1617
1618/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001619 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1620 *
1621 * @crtc: drm crtc
1622 *
1623 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1624 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1625 * monitors a dedicated PPLL must be used. If a particular board has
1626 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1627 * as there is no need to program the PLL itself. If we are not able to
1628 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1629 * avoid messing up an existing monitor.
1630 *
1631 * Asic specific PLL information
1632 *
1633 * DCE 6.1
1634 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1635 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1636 *
1637 * DCE 6.0
1638 * - PPLL0 is available to all UNIPHY (DP only)
1639 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1640 *
1641 * DCE 5.0
1642 * - DCPLL is available to all UNIPHY (DP only)
1643 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1644 *
1645 * DCE 3.0/4.0/4.1
1646 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1647 *
1648 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001649static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1650{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001651 struct drm_device *dev = crtc->dev;
1652 struct radeon_device *rdev = dev->dev_private;
1653 struct drm_encoder *test_encoder;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001654 u32 pll_in_use;
1655 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001656
Alex Deucher24e1f792012-03-20 17:18:32 -04001657 if (ASIC_IS_DCE61(rdev)) {
1658 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1659 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
1660 struct radeon_encoder *test_radeon_encoder =
1661 to_radeon_encoder(test_encoder);
1662 struct radeon_encoder_atom_dig *dig =
1663 test_radeon_encoder->enc_priv;
1664
1665 if ((test_radeon_encoder->encoder_id ==
1666 ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
Alex Deucherf3dd8502012-08-31 11:56:50 -04001667 (dig->linkb == false))
1668 /* UNIPHY A uses PPLL2 */
Alex Deucher24e1f792012-03-20 17:18:32 -04001669 return ATOM_PPLL2;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001670 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_encoder))) {
1671 /* UNIPHY B/C/D/E/F */
1672 if (rdev->clock.dp_extclk)
1673 /* skip PPLL programming if using ext clock */
1674 return ATOM_PPLL_INVALID;
1675 else {
1676 /* use the same PPLL for all DP monitors */
1677 pll = radeon_get_shared_dp_ppll(crtc);
1678 if (pll != ATOM_PPLL_INVALID)
1679 return pll;
1680 }
Alex Deucher2f454cf2012-09-12 18:54:14 -04001681 } else {
1682 /* use the same PPLL for all monitors with the same clock */
1683 pll = radeon_get_shared_nondp_ppll(crtc, test_encoder);
1684 if (pll != ATOM_PPLL_INVALID)
1685 return pll;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001686 }
1687 break;
Alex Deucher24e1f792012-03-20 17:18:32 -04001688 }
1689 }
1690 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001691 pll_in_use = radeon_get_pll_use_mask(crtc);
1692 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001693 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001694 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1695 return ATOM_PPLL1;
1696 DRM_ERROR("unable to allocate a PPLL\n");
1697 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001698 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001699 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1700 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
Alex Deucher86a94de2011-05-20 04:34:17 -04001701 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1702 * depending on the asic:
1703 * DCE4: PPLL or ext clock
Alex Deucherf3dd8502012-08-31 11:56:50 -04001704 * DCE5: PPLL, DCPLL, or ext clock
1705 * DCE6: PPLL, PPLL0, or ext clock
Alex Deucher86a94de2011-05-20 04:34:17 -04001706 *
1707 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1708 * PPLL/DCPLL programming and only program the DP DTO for the
1709 * crtc virtual pixel clock.
1710 */
Alex Deucher996d5c52011-10-26 15:59:50 -04001711 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_encoder))) {
Alex Deucherecd67952012-08-06 17:06:03 -04001712 if (rdev->clock.dp_extclk)
Alex Deucherf3dd8502012-08-31 11:56:50 -04001713 /* skip PPLL programming if using ext clock */
Alex Deucherecd67952012-08-06 17:06:03 -04001714 return ATOM_PPLL_INVALID;
Alex Deucher26fe45a2012-07-17 14:02:43 -04001715 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3dd8502012-08-31 11:56:50 -04001716 /* use PPLL0 for all DP */
Alex Deucher26fe45a2012-07-17 14:02:43 -04001717 return ATOM_PPLL0;
Alex Deucherecd67952012-08-06 17:06:03 -04001718 else if (ASIC_IS_DCE5(rdev))
Alex Deucherf3dd8502012-08-31 11:56:50 -04001719 /* use DCPLL for all DP */
Alex Deucherecd67952012-08-06 17:06:03 -04001720 return ATOM_DCPLL;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001721 else {
1722 /* use the same PPLL for all DP monitors */
1723 pll = radeon_get_shared_dp_ppll(crtc);
1724 if (pll != ATOM_PPLL_INVALID)
1725 return pll;
1726 }
Alex Deucher2f454cf2012-09-12 18:54:14 -04001727 } else {
1728 /* use the same PPLL for all monitors with the same clock */
1729 pll = radeon_get_shared_nondp_ppll(crtc, test_encoder);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001730 if (pll != ATOM_PPLL_INVALID)
1731 return pll;
1732 }
1733 break;
1734 }
1735 }
1736 /* all other cases */
1737 pll_in_use = radeon_get_pll_use_mask(crtc);
1738 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1739 return ATOM_PPLL2;
1740 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1741 return ATOM_PPLL1;
1742 DRM_ERROR("unable to allocate a PPLL\n");
1743 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001744 } else {
1745 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
1746 if (!ASIC_IS_AVIVO(rdev)) {
1747 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1748 return radeon_crtc->crtc_id;
1749 }
1750 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1751 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
1752 /* in DP mode, the DP ref clock can come from either PPLL
1753 * depending on the asic:
1754 * DCE3: PPLL1 or PPLL2
1755 */
1756 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_encoder))) {
1757 /* use the same PPLL for all DP monitors */
1758 pll = radeon_get_shared_dp_ppll(crtc);
1759 if (pll != ATOM_PPLL_INVALID)
1760 return pll;
1761 } else {
1762 /* use the same PPLL for all monitors with the same clock */
1763 pll = radeon_get_shared_nondp_ppll(crtc, test_encoder);
1764 if (pll != ATOM_PPLL_INVALID)
1765 return pll;
1766 }
1767 break;
1768 }
1769 }
1770 /* all other cases */
1771 pll_in_use = radeon_get_pll_use_mask(crtc);
1772 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1773 return ATOM_PPLL2;
1774 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1775 return ATOM_PPLL1;
1776 DRM_ERROR("unable to allocate a PPLL\n");
1777 return ATOM_PPLL_INVALID;
1778 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001779}
1780
Alex Deucherf3f1f032012-03-20 17:18:04 -04001781void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001782{
1783 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001784 if (ASIC_IS_DCE6(rdev))
1785 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1786 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001787 struct radeon_atom_ss ss;
1788 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1789 ASIC_INTERNAL_SS_ON_DCPLL,
1790 rdev->clock.default_dispclk);
1791 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001792 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001793 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001794 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001795 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001796 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001797 }
1798
1799}
1800
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001801int atombios_crtc_mode_set(struct drm_crtc *crtc,
1802 struct drm_display_mode *mode,
1803 struct drm_display_mode *adjusted_mode,
1804 int x, int y, struct drm_framebuffer *old_fb)
1805{
1806 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1807 struct drm_device *dev = crtc->dev;
1808 struct radeon_device *rdev = dev->dev_private;
Alex Deucher54bfe492010-09-03 15:52:53 -04001809 struct drm_encoder *encoder;
1810 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001811
Alex Deucher54bfe492010-09-03 15:52:53 -04001812 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1813 /* find tv std */
1814 if (encoder->crtc == crtc) {
1815 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1816 if (radeon_encoder->active_device &
1817 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1818 is_tvcv = true;
1819 }
1820 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001821
1822 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001823
Alex Deucher54bfe492010-09-03 15:52:53 -04001824 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001825 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001826 else if (ASIC_IS_AVIVO(rdev)) {
1827 if (is_tvcv)
1828 atombios_crtc_set_timing(crtc, adjusted_mode);
1829 else
1830 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1831 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001832 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001833 if (radeon_crtc->crtc_id == 0)
1834 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001835 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001836 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001837 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001838 atombios_overscan_setup(crtc, mode, adjusted_mode);
1839 atombios_scaler_setup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001840 return 0;
1841}
1842
1843static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001844 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001845 struct drm_display_mode *adjusted_mode)
1846{
Jerome Glissec93bb852009-07-13 21:04:08 +02001847 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1848 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001849 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1850 return false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001851 return true;
1852}
1853
1854static void atombios_crtc_prepare(struct drm_crtc *crtc)
1855{
Alex Deucher267364a2010-03-08 17:10:41 -05001856 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001857 struct drm_device *dev = crtc->dev;
1858 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001859
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001860 radeon_crtc->in_mode_set = true;
Alex Deucher267364a2010-03-08 17:10:41 -05001861 /* pick pll */
1862 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1863
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001864 /* disable crtc pair power gating before programming */
1865 if (ASIC_IS_DCE6(rdev))
1866 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1867
Alex Deucher37b43902010-02-09 12:04:43 -05001868 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001869 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001870}
1871
1872static void atombios_crtc_commit(struct drm_crtc *crtc)
1873{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001874 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1875
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001876 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001877 atombios_lock_crtc(crtc, ATOM_DISABLE);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001878 radeon_crtc->in_mode_set = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001879}
1880
Alex Deucher37f90032010-06-11 17:58:38 -04001881static void atombios_crtc_disable(struct drm_crtc *crtc)
1882{
1883 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04001884 struct drm_device *dev = crtc->dev;
1885 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001886 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04001887 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001888
Alex Deucher37f90032010-06-11 17:58:38 -04001889 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1890
Alex Deucher4e585912012-08-21 19:06:21 -04001891 for (i = 0; i < rdev->num_crtc; i++) {
1892 if (rdev->mode_info.crtcs[i] &&
1893 rdev->mode_info.crtcs[i]->enabled &&
1894 i != radeon_crtc->crtc_id &&
1895 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
1896 /* one other crtc is using this pll don't turn
1897 * off the pll
1898 */
1899 goto done;
1900 }
1901 }
1902
Alex Deucher37f90032010-06-11 17:58:38 -04001903 switch (radeon_crtc->pll_id) {
1904 case ATOM_PPLL1:
1905 case ATOM_PPLL2:
1906 /* disable the ppll */
1907 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04001908 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04001909 break;
Alex Deucher64199872012-03-20 17:18:33 -04001910 case ATOM_PPLL0:
1911 /* disable the ppll */
1912 if (ASIC_IS_DCE61(rdev))
1913 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1914 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
1915 break;
Alex Deucher37f90032010-06-11 17:58:38 -04001916 default:
1917 break;
1918 }
Alex Deucher4e585912012-08-21 19:06:21 -04001919done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04001920 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher37f90032010-06-11 17:58:38 -04001921}
1922
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001923static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1924 .dpms = atombios_crtc_dpms,
1925 .mode_fixup = atombios_crtc_mode_fixup,
1926 .mode_set = atombios_crtc_mode_set,
1927 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05001928 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001929 .prepare = atombios_crtc_prepare,
1930 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10001931 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04001932 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001933};
1934
1935void radeon_atombios_init_crtc(struct drm_device *dev,
1936 struct radeon_crtc *radeon_crtc)
1937{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001938 struct radeon_device *rdev = dev->dev_private;
1939
1940 if (ASIC_IS_DCE4(rdev)) {
1941 switch (radeon_crtc->crtc_id) {
1942 case 0:
1943 default:
Alex Deucher12d77982010-02-09 17:18:48 -05001944 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001945 break;
1946 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05001947 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001948 break;
1949 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05001950 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001951 break;
1952 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05001953 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001954 break;
1955 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05001956 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001957 break;
1958 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05001959 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001960 break;
1961 }
1962 } else {
1963 if (radeon_crtc->crtc_id == 1)
1964 radeon_crtc->crtc_offset =
1965 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1966 else
1967 radeon_crtc->crtc_offset = 0;
1968 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04001969 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001970 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
1971}