blob: 9175615bbd8a158d8d38a071f0fc87e30c78f688 [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);
Alex Deucher5df31962012-09-13 11:52:08 -040086 struct radeon_encoder *radeon_encoder =
87 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020088 /* fixme - fill in enc_priv for atom dac */
89 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100090 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Alex Deucher5df31962012-09-13 11:52:08 -040095 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
96 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
97 tv_std = tv_dac->tv_std;
98 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 }
100
Jerome Glissec93bb852009-07-13 21:04:08 +0200101 memset(&args, 0, sizeof(args));
102
103 args.ucScaler = radeon_crtc->crtc_id;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200106 switch (tv_std) {
107 case TV_STD_NTSC:
108 default:
109 args.ucTVStandard = ATOM_TV_NTSC;
110 break;
111 case TV_STD_PAL:
112 args.ucTVStandard = ATOM_TV_PAL;
113 break;
114 case TV_STD_PAL_M:
115 args.ucTVStandard = ATOM_TV_PALM;
116 break;
117 case TV_STD_PAL_60:
118 args.ucTVStandard = ATOM_TV_PAL60;
119 break;
120 case TV_STD_NTSC_J:
121 args.ucTVStandard = ATOM_TV_NTSCJ;
122 break;
123 case TV_STD_SCART_PAL:
124 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
125 break;
126 case TV_STD_SECAM:
127 args.ucTVStandard = ATOM_TV_SECAM;
128 break;
129 case TV_STD_PAL_CN:
130 args.ucTVStandard = ATOM_TV_PALCN;
131 break;
132 }
133 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000134 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200135 args.ucTVStandard = ATOM_TV_CV;
136 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
137 } else {
138 switch (radeon_crtc->rmx_type) {
139 case RMX_FULL:
140 args.ucEnable = ATOM_SCALER_EXPANSION;
141 break;
142 case RMX_CENTER:
143 args.ucEnable = ATOM_SCALER_CENTER;
144 break;
145 case RMX_ASPECT:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 default:
149 if (ASIC_IS_AVIVO(rdev))
150 args.ucEnable = ATOM_SCALER_DISABLE;
151 else
152 args.ucEnable = ATOM_SCALER_CENTER;
153 break;
154 }
155 }
156 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000157 if ((is_tv || is_cv)
158 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
159 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200160 }
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
164{
165 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
166 struct drm_device *dev = crtc->dev;
167 struct radeon_device *rdev = dev->dev_private;
168 int index =
169 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
170 ENABLE_CRTC_PS_ALLOCATION args;
171
172 memset(&args, 0, sizeof(args));
173
174 args.ucCRTC = radeon_crtc->crtc_id;
175 args.ucEnable = lock;
176
177 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
178}
179
180static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
181{
182 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
183 struct drm_device *dev = crtc->dev;
184 struct radeon_device *rdev = dev->dev_private;
185 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = state;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
212static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
213{
214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
215 struct drm_device *dev = crtc->dev;
216 struct radeon_device *rdev = dev->dev_private;
217 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
218 BLANK_CRTC_PS_ALLOCATION args;
219
220 memset(&args, 0, sizeof(args));
221
222 args.ucCRTC = radeon_crtc->crtc_id;
223 args.ucBlanking = state;
224
225 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
226}
227
Alex Deucherfef9f912012-03-20 17:18:03 -0400228static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
229{
230 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
231 struct drm_device *dev = crtc->dev;
232 struct radeon_device *rdev = dev->dev_private;
233 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
234 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
235
236 memset(&args, 0, sizeof(args));
237
238 args.ucDispPipeId = radeon_crtc->crtc_id;
239 args.ucEnable = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
242}
243
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
245{
246 struct drm_device *dev = crtc->dev;
247 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500248 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200249
250 switch (mode) {
251 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400252 radeon_crtc->enabled = true;
253 /* adjust pm to dpms changes BEFORE enabling crtcs */
254 radeon_pm_compute_clocks(rdev);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -0400255 if (ASIC_IS_DCE6(rdev) && !radeon_crtc->in_mode_set)
Alex Deucherfef9f912012-03-20 17:18:03 -0400256 atombios_powergate_crtc(crtc, ATOM_DISABLE);
Alex Deucher37b43902010-02-09 12:04:43 -0500257 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400258 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500259 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
260 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400261 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500262 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263 break;
264 case DRM_MODE_DPMS_STANDBY:
265 case DRM_MODE_DPMS_SUSPEND:
266 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400267 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500268 if (radeon_crtc->enabled)
269 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400270 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500271 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
272 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400273 radeon_crtc->enabled = false;
Alex Deucherc205b232012-08-24 18:21:21 -0400274 if (ASIC_IS_DCE6(rdev) && !radeon_crtc->in_mode_set)
275 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucherd7311172010-05-03 01:13:14 -0400276 /* adjust pm to dpms changes AFTER disabling crtcs */
277 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 break;
279 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200280}
281
282static void
283atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400284 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200285{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400286 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200287 struct drm_device *dev = crtc->dev;
288 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400289 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400291 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200292
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400293 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400294 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400295 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400296 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
297 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400298 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400299 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400300 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400301 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400302 args.usH_SyncWidth =
303 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
304 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400305 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400306 args.usV_SyncWidth =
307 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400308 args.ucH_Border = radeon_crtc->h_border;
309 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400310
311 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
312 misc |= ATOM_VSYNC_POLARITY;
313 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
314 misc |= ATOM_HSYNC_POLARITY;
315 if (mode->flags & DRM_MODE_FLAG_CSYNC)
316 misc |= ATOM_COMPOSITESYNC;
317 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
318 misc |= ATOM_INTERLACE;
319 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
320 misc |= ATOM_DOUBLE_CLOCK_MODE;
321
322 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
323 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200324
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400325 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200326}
327
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400328static void atombios_crtc_set_timing(struct drm_crtc *crtc,
329 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200330{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400331 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 struct drm_device *dev = crtc->dev;
333 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400334 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200335 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400336 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200337
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400338 memset(&args, 0, sizeof(args));
339 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
340 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
341 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
342 args.usH_SyncWidth =
343 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
344 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
345 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
346 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
347 args.usV_SyncWidth =
348 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
349
Alex Deucher54bfe492010-09-03 15:52:53 -0400350 args.ucOverscanRight = radeon_crtc->h_border;
351 args.ucOverscanLeft = radeon_crtc->h_border;
352 args.ucOverscanBottom = radeon_crtc->v_border;
353 args.ucOverscanTop = radeon_crtc->v_border;
354
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400355 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
356 misc |= ATOM_VSYNC_POLARITY;
357 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
358 misc |= ATOM_HSYNC_POLARITY;
359 if (mode->flags & DRM_MODE_FLAG_CSYNC)
360 misc |= ATOM_COMPOSITESYNC;
361 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
362 misc |= ATOM_INTERLACE;
363 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
364 misc |= ATOM_DOUBLE_CLOCK_MODE;
365
366 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
367 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200368
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400369 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200370}
371
Alex Deucher3fa47d92012-01-20 14:56:39 -0500372static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500373{
Alex Deucherb7922102010-03-06 10:57:30 -0500374 u32 ss_cntl;
375
376 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500377 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500378 case ATOM_PPLL1:
379 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
380 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
381 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
382 break;
383 case ATOM_PPLL2:
384 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
385 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
386 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
387 break;
388 case ATOM_DCPLL:
389 case ATOM_PPLL_INVALID:
390 return;
391 }
392 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500393 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500394 case ATOM_PPLL1:
395 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
396 ss_cntl &= ~1;
397 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
398 break;
399 case ATOM_PPLL2:
400 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
401 ss_cntl &= ~1;
402 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
403 break;
404 case ATOM_DCPLL:
405 case ATOM_PPLL_INVALID:
406 return;
407 }
408 }
409}
410
411
Alex Deucher26b9fc32010-02-01 16:39:11 -0500412union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400413 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
414 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500415 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400416 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500417 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500418};
419
Alex Deucher3fa47d92012-01-20 14:56:39 -0500420static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400421 int enable,
422 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400423 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400424 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400425{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400426 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400427 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500428 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400429
Jerome Glisse5efcc762012-08-17 14:40:04 -0400430 if (!enable) {
Alex Deucher53176702012-08-21 18:52:56 -0400431 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400432 if (rdev->mode_info.crtcs[i] &&
433 rdev->mode_info.crtcs[i]->enabled &&
434 i != crtc_id &&
435 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
436 /* one other crtc is using this pll don't turn
437 * off spread spectrum as it might turn off
438 * display on active crtc
439 */
440 return;
441 }
442 }
443 }
444
Alex Deucher26b9fc32010-02-01 16:39:11 -0500445 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400446
Alex Deuchera572eaa2011-01-06 21:19:16 -0500447 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500448 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400449 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500450 switch (pll_id) {
451 case ATOM_PPLL1:
452 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500453 break;
454 case ATOM_PPLL2:
455 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500456 break;
457 case ATOM_DCPLL:
458 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500459 break;
460 case ATOM_PPLL_INVALID:
461 return;
462 }
Alex Deucherf312f092012-07-17 14:02:44 -0400463 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
464 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400465 args.v3.ucEnable = enable;
Alex Deucher0671bdd72012-03-20 17:18:34 -0400466 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK) || ASIC_IS_DCE61(rdev))
Alex Deucher8e8e5232011-05-20 04:34:16 -0400467 args.v3.ucEnable = ATOM_DISABLE;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500468 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400469 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400470 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400471 switch (pll_id) {
472 case ATOM_PPLL1:
473 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400474 break;
475 case ATOM_PPLL2:
476 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400477 break;
478 case ATOM_DCPLL:
479 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400480 break;
481 case ATOM_PPLL_INVALID:
482 return;
483 }
Alex Deucherf312f092012-07-17 14:02:44 -0400484 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
485 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400486 args.v2.ucEnable = enable;
Alex Deucher09cc6502011-10-12 18:44:33 -0400487 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK) || ASIC_IS_DCE41(rdev))
Alex Deucher8e8e5232011-05-20 04:34:16 -0400488 args.v2.ucEnable = ATOM_DISABLE;
Alex Deucherba032a52010-10-04 17:13:01 -0400489 } else if (ASIC_IS_DCE3(rdev)) {
490 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400491 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400492 args.v1.ucSpreadSpectrumStep = ss->step;
493 args.v1.ucSpreadSpectrumDelay = ss->delay;
494 args.v1.ucSpreadSpectrumRange = ss->range;
495 args.v1.ucPpll = pll_id;
496 args.v1.ucEnable = enable;
497 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400498 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
499 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500500 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400501 return;
502 }
503 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400504 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400505 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
506 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
507 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
508 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400509 } else {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400510 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
511 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500512 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400513 return;
514 }
515 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400516 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400517 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
518 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
519 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400520 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500521 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400522}
523
Alex Deucher4eaeca32010-01-19 17:32:27 -0500524union adjust_pixel_clock {
525 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500526 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500527};
528
529static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400530 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200531{
Alex Deucher19eca432012-09-13 10:56:16 -0400532 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200533 struct drm_device *dev = crtc->dev;
534 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400535 struct drm_encoder *encoder = radeon_crtc->encoder;
536 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
537 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500538 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400539 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400540 u32 dp_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400541 int bpc = radeon_get_monitor_bpc(connector);
542 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500543
Alex Deucher4eaeca32010-01-19 17:32:27 -0500544 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400545 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200546
547 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400548 if ((rdev->family == CHIP_RS600) ||
549 (rdev->family == CHIP_RS690) ||
550 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400551 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
552 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000553
554 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400555 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000556 else
Alex Deucher19eca432012-09-13 10:56:16 -0400557 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400558
Alex Deucher5785e532011-04-19 15:24:59 -0400559 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400560 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400561 /* use frac fb div on APUs */
562 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400563 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500564 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
565 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000566 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400567 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200568
Dave Airlie5480f722010-10-19 10:36:47 +1000569 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400570 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000571 else
Alex Deucher19eca432012-09-13 10:56:16 -0400572 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000573 }
574
Alex Deucher5df31962012-09-13 11:52:08 -0400575 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
576 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
577 if (connector) {
578 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
579 struct radeon_connector_atom_dig *dig_connector =
580 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400581
Alex Deucher5df31962012-09-13 11:52:08 -0400582 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200583 }
584 }
585
Alex Deucher5df31962012-09-13 11:52:08 -0400586 /* use recommended ref_div for ss */
587 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
588 if (radeon_crtc->ss_enabled) {
589 if (radeon_crtc->ss.refdiv) {
590 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
591 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
592 if (ASIC_IS_AVIVO(rdev))
593 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
594 }
595 }
596 }
597
598 if (ASIC_IS_AVIVO(rdev)) {
599 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
600 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
601 adjusted_clock = mode->clock * 2;
602 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
603 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
604 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
605 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
606 } else {
607 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
608 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
609 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
610 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
611 }
612
Alex Deucher2606c882009-10-08 13:36:21 -0400613 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
614 * accordingly based on the encoder/transmitter to work around
615 * special hw requirements.
616 */
617 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500618 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500619 u8 frev, crev;
620 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400621
Alex Deucher2606c882009-10-08 13:36:21 -0400622 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400623 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
624 &crev))
625 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500626
627 memset(&args, 0, sizeof(args));
628
629 switch (frev) {
630 case 1:
631 switch (crev) {
632 case 1:
633 case 2:
634 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
635 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500636 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400637 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400638 args.v1.ucConfig |=
639 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500640
641 atom_execute_table(rdev->mode_info.atom_context,
642 index, (uint32_t *)&args);
643 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
644 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500645 case 3:
646 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
647 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
648 args.v3.sInput.ucEncodeMode = encoder_mode;
649 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400650 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000651 args.v3.sInput.ucDispPllConfig |=
652 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400653 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400654 args.v3.sInput.ucDispPllConfig |=
655 DISPPLL_CONFIG_COHERENT_MODE;
656 /* 16200 or 27000 */
657 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
658 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500659 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400660 if (encoder_mode == ATOM_ENCODER_MODE_HDMI)
661 /* deep color support */
662 args.v3.sInput.usPixelClock =
663 cpu_to_le16((mode->clock * bpc / 8) / 10);
664 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500665 args.v3.sInput.ucDispPllConfig |=
666 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500667 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500668 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400669 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500670 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400671 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
672 ENCODER_OBJECT_ID_NONE)
673 args.v3.sInput.ucExtTransmitterID =
674 radeon_encoder_get_dp_bridge_encoder_id(encoder);
675 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400676 args.v3.sInput.ucExtTransmitterID = 0;
677
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500678 atom_execute_table(rdev->mode_info.atom_context,
679 index, (uint32_t *)&args);
680 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
681 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400682 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
683 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
684 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500685 }
686 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400687 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
688 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
689 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500690 }
691 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500692 default:
693 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
694 return adjusted_clock;
695 }
696 break;
697 default:
698 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
699 return adjusted_clock;
700 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400701 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500702 return adjusted_clock;
703}
704
705union set_pixel_clock {
706 SET_PIXEL_CLOCK_PS_ALLOCATION base;
707 PIXEL_CLOCK_PARAMETERS v1;
708 PIXEL_CLOCK_PARAMETERS_V2 v2;
709 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500710 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500711 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500712};
713
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500714/* on DCE5, make sure the voltage is high enough to support the
715 * required disp clk.
716 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400717static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500718 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500719{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500720 u8 frev, crev;
721 int index;
722 union set_pixel_clock args;
723
724 memset(&args, 0, sizeof(args));
725
726 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400727 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
728 &crev))
729 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500730
731 switch (frev) {
732 case 1:
733 switch (crev) {
734 case 5:
735 /* if the default dcpll clock is specified,
736 * SetPixelClock provides the dividers
737 */
738 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500739 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500740 args.v5.ucPpll = ATOM_DCPLL;
741 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500742 case 6:
743 /* if the default dcpll clock is specified,
744 * SetPixelClock provides the dividers
745 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500746 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher729b95e2012-03-20 17:18:31 -0400747 if (ASIC_IS_DCE61(rdev))
748 args.v6.ucPpll = ATOM_EXT_PLL1;
749 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400750 args.v6.ucPpll = ATOM_PPLL0;
751 else
752 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500753 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500754 default:
755 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
756 return;
757 }
758 break;
759 default:
760 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
761 return;
762 }
763 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
764}
765
Alex Deucher37f90032010-06-11 17:58:38 -0400766static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000767 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400768 int pll_id,
769 u32 encoder_mode,
770 u32 encoder_id,
771 u32 clock,
772 u32 ref_div,
773 u32 fb_div,
774 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400775 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400776 int bpc,
777 bool ss_enabled,
778 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400779{
780 struct drm_device *dev = crtc->dev;
781 struct radeon_device *rdev = dev->dev_private;
782 u8 frev, crev;
783 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
784 union set_pixel_clock args;
785
786 memset(&args, 0, sizeof(args));
787
788 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
789 &crev))
790 return;
791
792 switch (frev) {
793 case 1:
794 switch (crev) {
795 case 1:
796 if (clock == ATOM_DISABLE)
797 return;
798 args.v1.usPixelClock = cpu_to_le16(clock / 10);
799 args.v1.usRefDiv = cpu_to_le16(ref_div);
800 args.v1.usFbDiv = cpu_to_le16(fb_div);
801 args.v1.ucFracFbDiv = frac_fb_div;
802 args.v1.ucPostDiv = post_div;
803 args.v1.ucPpll = pll_id;
804 args.v1.ucCRTC = crtc_id;
805 args.v1.ucRefDivSrc = 1;
806 break;
807 case 2:
808 args.v2.usPixelClock = cpu_to_le16(clock / 10);
809 args.v2.usRefDiv = cpu_to_le16(ref_div);
810 args.v2.usFbDiv = cpu_to_le16(fb_div);
811 args.v2.ucFracFbDiv = frac_fb_div;
812 args.v2.ucPostDiv = post_div;
813 args.v2.ucPpll = pll_id;
814 args.v2.ucCRTC = crtc_id;
815 args.v2.ucRefDivSrc = 1;
816 break;
817 case 3:
818 args.v3.usPixelClock = cpu_to_le16(clock / 10);
819 args.v3.usRefDiv = cpu_to_le16(ref_div);
820 args.v3.usFbDiv = cpu_to_le16(fb_div);
821 args.v3.ucFracFbDiv = frac_fb_div;
822 args.v3.ucPostDiv = post_div;
823 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400824 if (crtc_id == ATOM_CRTC2)
825 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
826 else
827 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400828 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
829 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400830 args.v3.ucTransmitterId = encoder_id;
831 args.v3.ucEncoderMode = encoder_mode;
832 break;
833 case 5:
834 args.v5.ucCRTC = crtc_id;
835 args.v5.usPixelClock = cpu_to_le16(clock / 10);
836 args.v5.ucRefDiv = ref_div;
837 args.v5.usFbDiv = cpu_to_le16(fb_div);
838 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
839 args.v5.ucPostDiv = post_div;
840 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400841 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
842 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400843 switch (bpc) {
844 case 8:
845 default:
846 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
847 break;
848 case 10:
849 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
850 break;
851 }
Alex Deucher37f90032010-06-11 17:58:38 -0400852 args.v5.ucTransmitterID = encoder_id;
853 args.v5.ucEncoderMode = encoder_mode;
854 args.v5.ucPpll = pll_id;
855 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500856 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000857 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500858 args.v6.ucRefDiv = ref_div;
859 args.v6.usFbDiv = cpu_to_le16(fb_div);
860 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
861 args.v6.ucPostDiv = post_div;
862 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400863 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
864 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400865 switch (bpc) {
866 case 8:
867 default:
868 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
869 break;
870 case 10:
871 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
872 break;
873 case 12:
874 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
875 break;
876 case 16:
877 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
878 break;
879 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500880 args.v6.ucTransmitterID = encoder_id;
881 args.v6.ucEncoderMode = encoder_mode;
882 args.v6.ucPpll = pll_id;
883 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400884 default:
885 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
886 return;
887 }
888 break;
889 default:
890 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
891 return;
892 }
893
894 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
895}
896
Alex Deucher19eca432012-09-13 10:56:16 -0400897static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
898{
899 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
900 struct drm_device *dev = crtc->dev;
901 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400902 struct radeon_encoder *radeon_encoder =
903 to_radeon_encoder(radeon_crtc->encoder);
904 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400905
906 radeon_crtc->bpc = 8;
907 radeon_crtc->ss_enabled = false;
908
Alex Deucher19eca432012-09-13 10:56:16 -0400909 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400910 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400911 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
912 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400913 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400914 struct radeon_connector *radeon_connector =
915 to_radeon_connector(connector);
916 struct radeon_connector_atom_dig *dig_connector =
917 radeon_connector->con_priv;
918 int dp_clock;
919 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
920
921 switch (encoder_mode) {
922 case ATOM_ENCODER_MODE_DP_MST:
923 case ATOM_ENCODER_MODE_DP:
924 /* DP/eDP */
925 dp_clock = dig_connector->dp_clock / 10;
926 if (ASIC_IS_DCE4(rdev))
927 radeon_crtc->ss_enabled =
928 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
929 ASIC_INTERNAL_SS_ON_DP,
930 dp_clock);
931 else {
932 if (dp_clock == 16200) {
933 radeon_crtc->ss_enabled =
934 radeon_atombios_get_ppll_ss_info(rdev,
935 &radeon_crtc->ss,
936 ATOM_DP_SS_ID2);
937 if (!radeon_crtc->ss_enabled)
938 radeon_crtc->ss_enabled =
939 radeon_atombios_get_ppll_ss_info(rdev,
940 &radeon_crtc->ss,
941 ATOM_DP_SS_ID1);
942 } else
943 radeon_crtc->ss_enabled =
944 radeon_atombios_get_ppll_ss_info(rdev,
945 &radeon_crtc->ss,
946 ATOM_DP_SS_ID1);
947 }
948 break;
949 case ATOM_ENCODER_MODE_LVDS:
950 if (ASIC_IS_DCE4(rdev))
951 radeon_crtc->ss_enabled =
952 radeon_atombios_get_asic_ss_info(rdev,
953 &radeon_crtc->ss,
954 dig->lcd_ss_id,
955 mode->clock / 10);
956 else
957 radeon_crtc->ss_enabled =
958 radeon_atombios_get_ppll_ss_info(rdev,
959 &radeon_crtc->ss,
960 dig->lcd_ss_id);
961 break;
962 case ATOM_ENCODER_MODE_DVI:
963 if (ASIC_IS_DCE4(rdev))
964 radeon_crtc->ss_enabled =
965 radeon_atombios_get_asic_ss_info(rdev,
966 &radeon_crtc->ss,
967 ASIC_INTERNAL_SS_ON_TMDS,
968 mode->clock / 10);
969 break;
970 case ATOM_ENCODER_MODE_HDMI:
971 if (ASIC_IS_DCE4(rdev))
972 radeon_crtc->ss_enabled =
973 radeon_atombios_get_asic_ss_info(rdev,
974 &radeon_crtc->ss,
975 ASIC_INTERNAL_SS_ON_HDMI,
976 mode->clock / 10);
977 break;
978 default:
979 break;
980 }
981 }
982
983 /* adjust pixel clock as needed */
984 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
985
986 return true;
987}
988
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500989static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -0500990{
991 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
992 struct drm_device *dev = crtc->dev;
993 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400994 struct radeon_encoder *radeon_encoder =
995 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500996 u32 pll_clock = mode->clock;
997 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
998 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -0400999 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001000
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001001 switch (radeon_crtc->pll_id) {
1002 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001003 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001004 break;
1005 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001006 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001007 break;
1008 case ATOM_DCPLL:
1009 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001010 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001011 pll = &rdev->clock.dcpll;
1012 break;
1013 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001014
Alex Deucher19eca432012-09-13 10:56:16 -04001015 /* update pll params */
1016 pll->flags = radeon_crtc->pll_flags;
1017 pll->reference_div = radeon_crtc->pll_reference_div;
1018 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001019
Alex Deucher64146f82011-03-22 01:46:12 -04001020 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1021 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001022 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1023 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001024 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001025 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1026 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001027 else
Alex Deucher19eca432012-09-13 10:56:16 -04001028 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1029 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001030
Alex Deucher19eca432012-09-13 10:56:16 -04001031 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1032 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001033
Alex Deucher37f90032010-06-11 17:58:38 -04001034 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1035 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001036 ref_div, fb_div, frac_fb_div, post_div,
1037 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001038
Alex Deucher19eca432012-09-13 10:56:16 -04001039 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001040 /* calculate ss amount and step size */
1041 if (ASIC_IS_DCE4(rdev)) {
1042 u32 step_size;
Alex Deucher19eca432012-09-13 10:56:16 -04001043 u32 amount = (((fb_div * 10) + frac_fb_div) * radeon_crtc->ss.percentage) / 10000;
1044 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1045 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001046 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001047 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
1048 step_size = (4 * amount * ref_div * (radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001049 (125 * 25 * pll->reference_freq / 100);
1050 else
Alex Deucher19eca432012-09-13 10:56:16 -04001051 step_size = (2 * amount * ref_div * (radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001052 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001053 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001054 }
1055
Alex Deucher19eca432012-09-13 10:56:16 -04001056 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1057 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001058 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001059}
1060
Alex Deucherc9417bd2011-02-06 14:23:26 -05001061static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1062 struct drm_framebuffer *fb,
1063 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001064{
1065 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1066 struct drm_device *dev = crtc->dev;
1067 struct radeon_device *rdev = dev->dev_private;
1068 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001069 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001070 struct drm_gem_object *obj;
1071 struct radeon_bo *rbo;
1072 uint64_t fb_location;
1073 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001074 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001075 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001076 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001077 int r;
1078
1079 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001080 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001081 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001082 return 0;
1083 }
1084
Chris Ball4dd19b02010-09-26 06:47:23 -05001085 if (atomic) {
1086 radeon_fb = to_radeon_framebuffer(fb);
1087 target_fb = fb;
1088 }
1089 else {
1090 radeon_fb = to_radeon_framebuffer(crtc->fb);
1091 target_fb = crtc->fb;
1092 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001093
Chris Ball4dd19b02010-09-26 06:47:23 -05001094 /* If atomic, assume fb object is pinned & idle & fenced and
1095 * just update base pointers
1096 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001097 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001098 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001099 r = radeon_bo_reserve(rbo, false);
1100 if (unlikely(r != 0))
1101 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001102
1103 if (atomic)
1104 fb_location = radeon_bo_gpu_offset(rbo);
1105 else {
1106 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1107 if (unlikely(r != 0)) {
1108 radeon_bo_unreserve(rbo);
1109 return -EINVAL;
1110 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001111 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001112
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001113 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1114 radeon_bo_unreserve(rbo);
1115
Chris Ball4dd19b02010-09-26 06:47:23 -05001116 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001117 case 8:
1118 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1119 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1120 break;
1121 case 15:
1122 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1123 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1124 break;
1125 case 16:
1126 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1127 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001128#ifdef __BIG_ENDIAN
1129 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1130#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001131 break;
1132 case 24:
1133 case 32:
1134 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1135 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001136#ifdef __BIG_ENDIAN
1137 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1138#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001139 break;
1140 default:
1141 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001142 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001143 return -EINVAL;
1144 }
1145
Alex Deucher392e3722011-11-28 14:49:27 -05001146 if (tiling_flags & RADEON_TILING_MACRO) {
Alex Deucherb7019b22012-06-14 15:58:25 -04001147 if (rdev->family >= CHIP_TAHITI)
1148 tmp = rdev->config.si.tile_config;
1149 else if (rdev->family >= CHIP_CAYMAN)
Alex Deucher392e3722011-11-28 14:49:27 -05001150 tmp = rdev->config.cayman.tile_config;
1151 else
1152 tmp = rdev->config.evergreen.tile_config;
1153
1154 switch ((tmp & 0xf0) >> 4) {
1155 case 0: /* 4 banks */
1156 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1157 break;
1158 case 1: /* 8 banks */
1159 default:
1160 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1161 break;
1162 case 2: /* 16 banks */
1163 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1164 break;
1165 }
1166
Alex Deucher97d66322010-05-20 12:12:48 -04001167 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001168
1169 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
1170 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1171 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1172 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1173 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher392e3722011-11-28 14:49:27 -05001174 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001175 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1176
Alex Deucherb7019b22012-06-14 15:58:25 -04001177 if ((rdev->family == CHIP_TAHITI) ||
1178 (rdev->family == CHIP_PITCAIRN))
1179 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
1180 else if (rdev->family == CHIP_VERDE)
1181 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1182
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001183 switch (radeon_crtc->crtc_id) {
1184 case 0:
1185 WREG32(AVIVO_D1VGA_CONTROL, 0);
1186 break;
1187 case 1:
1188 WREG32(AVIVO_D2VGA_CONTROL, 0);
1189 break;
1190 case 2:
1191 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1192 break;
1193 case 3:
1194 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1195 break;
1196 case 4:
1197 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1198 break;
1199 case 5:
1200 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1201 break;
1202 default:
1203 break;
1204 }
1205
1206 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1207 upper_32_bits(fb_location));
1208 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1209 upper_32_bits(fb_location));
1210 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1211 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1212 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1213 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1214 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001215 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001216
1217 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1218 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1219 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1220 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001221 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1222 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001223
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001224 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001225 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1226 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1227
1228 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001229 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001230 x &= ~3;
1231 y &= ~1;
1232 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1233 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001234 viewport_w = crtc->mode.hdisplay;
1235 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001236 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001237 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001238
Alex Deucherfb9674b2011-04-02 09:15:50 -04001239 /* pageflip setup */
1240 /* make sure flip is at vb rather than hb */
1241 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1242 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1243 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1244
1245 /* set pageflip to happen anywhere in vblank interval */
1246 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1247
Chris Ball4dd19b02010-09-26 06:47:23 -05001248 if (!atomic && fb && fb != crtc->fb) {
1249 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001250 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001251 r = radeon_bo_reserve(rbo, false);
1252 if (unlikely(r != 0))
1253 return r;
1254 radeon_bo_unpin(rbo);
1255 radeon_bo_unreserve(rbo);
1256 }
1257
1258 /* Bytes per pixel may have changed */
1259 radeon_bandwidth_update(rdev);
1260
1261 return 0;
1262}
1263
Chris Ball4dd19b02010-09-26 06:47:23 -05001264static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1265 struct drm_framebuffer *fb,
1266 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001267{
1268 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1269 struct drm_device *dev = crtc->dev;
1270 struct radeon_device *rdev = dev->dev_private;
1271 struct radeon_framebuffer *radeon_fb;
1272 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001273 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001274 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001275 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001276 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001277 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001278 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001279 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001280
Jerome Glisse2de3b482009-11-17 14:08:55 -08001281 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001282 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001283 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001284 return 0;
1285 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001286
Chris Ball4dd19b02010-09-26 06:47:23 -05001287 if (atomic) {
1288 radeon_fb = to_radeon_framebuffer(fb);
1289 target_fb = fb;
1290 }
1291 else {
1292 radeon_fb = to_radeon_framebuffer(crtc->fb);
1293 target_fb = crtc->fb;
1294 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001295
1296 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001297 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001298 r = radeon_bo_reserve(rbo, false);
1299 if (unlikely(r != 0))
1300 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001301
1302 /* If atomic, assume fb object is pinned & idle & fenced and
1303 * just update base pointers
1304 */
1305 if (atomic)
1306 fb_location = radeon_bo_gpu_offset(rbo);
1307 else {
1308 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1309 if (unlikely(r != 0)) {
1310 radeon_bo_unreserve(rbo);
1311 return -EINVAL;
1312 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001313 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001314 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1315 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001316
Chris Ball4dd19b02010-09-26 06:47:23 -05001317 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001318 case 8:
1319 fb_format =
1320 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1321 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1322 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001323 case 15:
1324 fb_format =
1325 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1326 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1327 break;
1328 case 16:
1329 fb_format =
1330 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1331 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001332#ifdef __BIG_ENDIAN
1333 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1334#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001335 break;
1336 case 24:
1337 case 32:
1338 fb_format =
1339 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1340 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001341#ifdef __BIG_ENDIAN
1342 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1343#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001344 break;
1345 default:
1346 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001347 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001348 return -EINVAL;
1349 }
1350
Alex Deucher40c4ac12010-05-20 12:04:59 -04001351 if (rdev->family >= CHIP_R600) {
1352 if (tiling_flags & RADEON_TILING_MACRO)
1353 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1354 else if (tiling_flags & RADEON_TILING_MICRO)
1355 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1356 } else {
1357 if (tiling_flags & RADEON_TILING_MACRO)
1358 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001359
Alex Deucher40c4ac12010-05-20 12:04:59 -04001360 if (tiling_flags & RADEON_TILING_MICRO)
1361 fb_format |= AVIVO_D1GRPH_TILED;
1362 }
Dave Airliee024e112009-06-24 09:48:08 +10001363
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001364 if (radeon_crtc->crtc_id == 0)
1365 WREG32(AVIVO_D1VGA_CONTROL, 0);
1366 else
1367 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001368
1369 if (rdev->family >= CHIP_RV770) {
1370 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001371 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1372 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001373 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001374 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1375 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001376 }
1377 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001378 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1379 (u32) fb_location);
1380 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1381 radeon_crtc->crtc_offset, (u32) fb_location);
1382 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001383 if (rdev->family >= CHIP_R600)
1384 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001385
1386 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1387 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1388 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1389 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001390 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1391 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001392
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001393 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001394 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1395 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1396
1397 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001398 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001399 x &= ~3;
1400 y &= ~1;
1401 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1402 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001403 viewport_w = crtc->mode.hdisplay;
1404 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001405 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001406 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001407
Alex Deucherfb9674b2011-04-02 09:15:50 -04001408 /* pageflip setup */
1409 /* make sure flip is at vb rather than hb */
1410 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1411 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1412 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1413
1414 /* set pageflip to happen anywhere in vblank interval */
1415 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1416
Chris Ball4dd19b02010-09-26 06:47:23 -05001417 if (!atomic && fb && fb != crtc->fb) {
1418 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001419 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001420 r = radeon_bo_reserve(rbo, false);
1421 if (unlikely(r != 0))
1422 return r;
1423 radeon_bo_unpin(rbo);
1424 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001425 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001426
1427 /* Bytes per pixel may have changed */
1428 radeon_bandwidth_update(rdev);
1429
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001430 return 0;
1431}
1432
Alex Deucher54f088a2010-01-19 16:34:01 -05001433int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1434 struct drm_framebuffer *old_fb)
1435{
1436 struct drm_device *dev = crtc->dev;
1437 struct radeon_device *rdev = dev->dev_private;
1438
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001439 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001440 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001441 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001442 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001443 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001444 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1445}
1446
1447int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1448 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001449 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001450{
1451 struct drm_device *dev = crtc->dev;
1452 struct radeon_device *rdev = dev->dev_private;
1453
1454 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001455 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001456 else if (ASIC_IS_AVIVO(rdev))
1457 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1458 else
1459 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001460}
1461
Alex Deucher615e0cb2010-01-20 16:22:53 -05001462/* properly set additional regs when using atombios */
1463static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1464{
1465 struct drm_device *dev = crtc->dev;
1466 struct radeon_device *rdev = dev->dev_private;
1467 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1468 u32 disp_merge_cntl;
1469
1470 switch (radeon_crtc->crtc_id) {
1471 case 0:
1472 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1473 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1474 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1475 break;
1476 case 1:
1477 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1478 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1479 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1480 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1481 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1482 break;
1483 }
1484}
1485
Alex Deucherf3dd8502012-08-31 11:56:50 -04001486/**
1487 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1488 *
1489 * @crtc: drm crtc
1490 *
1491 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1492 */
1493static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1494{
1495 struct drm_device *dev = crtc->dev;
1496 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001497 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001498 u32 pll_in_use = 0;
1499
1500 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1501 if (crtc == test_crtc)
1502 continue;
1503
Alex Deucher57b35e22012-09-17 17:34:45 -04001504 test_radeon_crtc = to_radeon_crtc(test_crtc);
1505 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1506 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001507 }
1508 return pll_in_use;
1509}
1510
1511/**
1512 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1513 *
1514 * @crtc: drm crtc
1515 *
1516 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1517 * also in DP mode. For DP, a single PPLL can be used for all DP
1518 * crtcs/encoders.
1519 */
1520static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1521{
1522 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001523 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001524 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001525
Alex Deucher57b35e22012-09-17 17:34:45 -04001526 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1527 if (crtc == test_crtc)
1528 continue;
1529 test_radeon_crtc = to_radeon_crtc(test_crtc);
1530 if (test_radeon_crtc->encoder &&
1531 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1532 /* for DP use the same PLL for all */
1533 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1534 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001535 }
1536 }
1537 return ATOM_PPLL_INVALID;
1538}
1539
1540/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001541 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1542 *
1543 * @crtc: drm crtc
1544 * @encoder: drm encoder
1545 *
1546 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1547 * be shared (i.e., same clock).
1548 */
Alex Deucher5df31962012-09-13 11:52:08 -04001549static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001550{
Alex Deucher5df31962012-09-13 11:52:08 -04001551 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001552 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001553 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001554 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001555 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001556
Alex Deucher9642ac02012-09-13 12:43:41 -04001557 adjusted_clock = radeon_crtc->adjusted_clock;
1558
1559 if (adjusted_clock == 0)
1560 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001561
Alex Deucher57b35e22012-09-17 17:34:45 -04001562 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1563 if (crtc == test_crtc)
1564 continue;
1565 test_radeon_crtc = to_radeon_crtc(test_crtc);
1566 if (test_radeon_crtc->encoder &&
1567 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1568 /* check if we are already driving this connector with another crtc */
1569 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1570 /* if we are, return that pll */
1571 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001572 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001573 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001574 /* for non-DP check the clock */
1575 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1576 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1577 (adjusted_clock == test_adjusted_clock) &&
1578 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1579 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1580 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001581 }
1582 }
1583 return ATOM_PPLL_INVALID;
1584}
1585
1586/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001587 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1588 *
1589 * @crtc: drm crtc
1590 *
1591 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1592 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1593 * monitors a dedicated PPLL must be used. If a particular board has
1594 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1595 * as there is no need to program the PLL itself. If we are not able to
1596 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1597 * avoid messing up an existing monitor.
1598 *
1599 * Asic specific PLL information
1600 *
1601 * DCE 6.1
1602 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1603 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1604 *
1605 * DCE 6.0
1606 * - PPLL0 is available to all UNIPHY (DP only)
1607 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1608 *
1609 * DCE 5.0
1610 * - DCPLL is available to all UNIPHY (DP only)
1611 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1612 *
1613 * DCE 3.0/4.0/4.1
1614 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1615 *
1616 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001617static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1618{
Alex Deucher5df31962012-09-13 11:52:08 -04001619 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001620 struct drm_device *dev = crtc->dev;
1621 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001622 struct radeon_encoder *radeon_encoder =
1623 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001624 u32 pll_in_use;
1625 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001626
Alex Deucher24e1f792012-03-20 17:18:32 -04001627 if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001628 struct radeon_encoder_atom_dig *dig =
1629 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001630
Alex Deucher5df31962012-09-13 11:52:08 -04001631 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1632 (dig->linkb == false))
1633 /* UNIPHY A uses PPLL2 */
1634 return ATOM_PPLL2;
1635 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1636 /* UNIPHY B/C/D/E/F */
1637 if (rdev->clock.dp_extclk)
1638 /* skip PPLL programming if using ext clock */
1639 return ATOM_PPLL_INVALID;
1640 else {
1641 /* use the same PPLL for all DP monitors */
1642 pll = radeon_get_shared_dp_ppll(crtc);
1643 if (pll != ATOM_PPLL_INVALID)
1644 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001645 }
Alex Deucher5df31962012-09-13 11:52:08 -04001646 } else {
1647 /* use the same PPLL for all monitors with the same clock */
1648 pll = radeon_get_shared_nondp_ppll(crtc);
1649 if (pll != ATOM_PPLL_INVALID)
1650 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001651 }
1652 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001653 pll_in_use = radeon_get_pll_use_mask(crtc);
1654 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001655 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001656 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1657 return ATOM_PPLL1;
1658 DRM_ERROR("unable to allocate a PPLL\n");
1659 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001660 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001661 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1662 * depending on the asic:
1663 * DCE4: PPLL or ext clock
1664 * DCE5: PPLL, DCPLL, or ext clock
1665 * DCE6: PPLL, PPLL0, or ext clock
1666 *
1667 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1668 * PPLL/DCPLL programming and only program the DP DTO for the
1669 * crtc virtual pixel clock.
1670 */
1671 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1672 if (rdev->clock.dp_extclk)
1673 /* skip PPLL programming if using ext clock */
1674 return ATOM_PPLL_INVALID;
1675 else if (ASIC_IS_DCE6(rdev))
1676 /* use PPLL0 for all DP */
1677 return ATOM_PPLL0;
1678 else if (ASIC_IS_DCE5(rdev))
1679 /* use DCPLL for all DP */
1680 return ATOM_DCPLL;
1681 else {
1682 /* use the same PPLL for all DP monitors */
1683 pll = radeon_get_shared_dp_ppll(crtc);
1684 if (pll != ATOM_PPLL_INVALID)
1685 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001686 }
Alex Deucher5df31962012-09-13 11:52:08 -04001687 } else {
1688 /* use the same PPLL for all monitors with the same clock */
1689 pll = radeon_get_shared_nondp_ppll(crtc);
1690 if (pll != ATOM_PPLL_INVALID)
1691 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001692 }
1693 /* all other cases */
1694 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001695 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1696 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001697 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1698 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001699 DRM_ERROR("unable to allocate a PPLL\n");
1700 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001701 } else {
1702 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001703 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1704 * the matching btw pll and crtc is done through
1705 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1706 * pll (1 or 2) to select which register to write. ie if using
1707 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1708 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1709 * choose which value to write. Which is reverse order from
1710 * register logic. So only case that works is when pllid is
1711 * same as crtcid or when both pll and crtc are enabled and
1712 * both use same clock.
1713 *
1714 * So just return crtc id as if crtc and pll were hard linked
1715 * together even if they aren't
1716 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001717 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001718 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001719}
1720
Alex Deucherf3f1f032012-03-20 17:18:04 -04001721void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001722{
1723 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001724 if (ASIC_IS_DCE6(rdev))
1725 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1726 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001727 struct radeon_atom_ss ss;
1728 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1729 ASIC_INTERNAL_SS_ON_DCPLL,
1730 rdev->clock.default_dispclk);
1731 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001732 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001733 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001734 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001735 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001736 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001737 }
1738
1739}
1740
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001741int atombios_crtc_mode_set(struct drm_crtc *crtc,
1742 struct drm_display_mode *mode,
1743 struct drm_display_mode *adjusted_mode,
1744 int x, int y, struct drm_framebuffer *old_fb)
1745{
1746 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1747 struct drm_device *dev = crtc->dev;
1748 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001749 struct radeon_encoder *radeon_encoder =
1750 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04001751 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001752
Alex Deucher5df31962012-09-13 11:52:08 -04001753 if (radeon_encoder->active_device &
1754 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1755 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001756
1757 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001758
Alex Deucher54bfe492010-09-03 15:52:53 -04001759 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001760 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001761 else if (ASIC_IS_AVIVO(rdev)) {
1762 if (is_tvcv)
1763 atombios_crtc_set_timing(crtc, adjusted_mode);
1764 else
1765 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1766 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001767 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001768 if (radeon_crtc->crtc_id == 0)
1769 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001770 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001771 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001772 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001773 atombios_overscan_setup(crtc, mode, adjusted_mode);
1774 atombios_scaler_setup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001775 return 0;
1776}
1777
1778static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001779 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001780 struct drm_display_mode *adjusted_mode)
1781{
Alex Deucher5df31962012-09-13 11:52:08 -04001782 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1783 struct drm_device *dev = crtc->dev;
1784 struct drm_encoder *encoder;
1785
1786 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
1787 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1788 if (encoder->crtc == crtc) {
1789 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04001790 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04001791 break;
1792 }
1793 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001794 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
1795 radeon_crtc->encoder = NULL;
1796 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04001797 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04001798 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001799 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1800 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001801 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1802 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04001803 /* pick pll */
1804 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1805 /* if we can't get a PPLL for a non-DP encoder, fail */
1806 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
1807 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
1808 return false;
1809
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001810 return true;
1811}
1812
1813static void atombios_crtc_prepare(struct drm_crtc *crtc)
1814{
Alex Deucher267364a2010-03-08 17:10:41 -05001815 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001816 struct drm_device *dev = crtc->dev;
1817 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001818
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001819 radeon_crtc->in_mode_set = true;
Alex Deucher267364a2010-03-08 17:10:41 -05001820
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001821 /* disable crtc pair power gating before programming */
1822 if (ASIC_IS_DCE6(rdev))
1823 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1824
Alex Deucher37b43902010-02-09 12:04:43 -05001825 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001826 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001827}
1828
1829static void atombios_crtc_commit(struct drm_crtc *crtc)
1830{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001831 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1832
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001833 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001834 atombios_lock_crtc(crtc, ATOM_DISABLE);
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001835 radeon_crtc->in_mode_set = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001836}
1837
Alex Deucher37f90032010-06-11 17:58:38 -04001838static void atombios_crtc_disable(struct drm_crtc *crtc)
1839{
1840 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04001841 struct drm_device *dev = crtc->dev;
1842 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001843 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04001844 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001845
Alex Deucher37f90032010-06-11 17:58:38 -04001846 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1847
Alex Deucher4e585912012-08-21 19:06:21 -04001848 for (i = 0; i < rdev->num_crtc; i++) {
1849 if (rdev->mode_info.crtcs[i] &&
1850 rdev->mode_info.crtcs[i]->enabled &&
1851 i != radeon_crtc->crtc_id &&
1852 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
1853 /* one other crtc is using this pll don't turn
1854 * off the pll
1855 */
1856 goto done;
1857 }
1858 }
1859
Alex Deucher37f90032010-06-11 17:58:38 -04001860 switch (radeon_crtc->pll_id) {
1861 case ATOM_PPLL1:
1862 case ATOM_PPLL2:
1863 /* disable the ppll */
1864 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04001865 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04001866 break;
Alex Deucher64199872012-03-20 17:18:33 -04001867 case ATOM_PPLL0:
1868 /* disable the ppll */
1869 if (ASIC_IS_DCE61(rdev))
1870 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1871 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
1872 break;
Alex Deucher37f90032010-06-11 17:58:38 -04001873 default:
1874 break;
1875 }
Alex Deucher4e585912012-08-21 19:06:21 -04001876done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04001877 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04001878 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04001879 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04001880 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04001881}
1882
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001883static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1884 .dpms = atombios_crtc_dpms,
1885 .mode_fixup = atombios_crtc_mode_fixup,
1886 .mode_set = atombios_crtc_mode_set,
1887 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05001888 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001889 .prepare = atombios_crtc_prepare,
1890 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10001891 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04001892 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001893};
1894
1895void radeon_atombios_init_crtc(struct drm_device *dev,
1896 struct radeon_crtc *radeon_crtc)
1897{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001898 struct radeon_device *rdev = dev->dev_private;
1899
1900 if (ASIC_IS_DCE4(rdev)) {
1901 switch (radeon_crtc->crtc_id) {
1902 case 0:
1903 default:
Alex Deucher12d77982010-02-09 17:18:48 -05001904 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001905 break;
1906 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05001907 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001908 break;
1909 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05001910 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001911 break;
1912 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05001913 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001914 break;
1915 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05001916 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001917 break;
1918 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05001919 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001920 break;
1921 }
1922 } else {
1923 if (radeon_crtc->crtc_id == 1)
1924 radeon_crtc->crtc_offset =
1925 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1926 else
1927 radeon_crtc->crtc_offset = 0;
1928 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04001929 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04001930 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04001931 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04001932 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001933 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
1934}