blob: 1dcf39084555b7edb15ab6a816ead9a0fe60374c [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>
Daniel Vetterb516a9e2015-12-04 09:45:43 +010028#include <drm/drm_fb_helper.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020029#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100030#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020031#include "radeon.h"
32#include "atom.h"
33#include "atom-bits.h"
34
Jerome Glissec93bb852009-07-13 21:04:08 +020035static void atombios_overscan_setup(struct drm_crtc *crtc,
36 struct drm_display_mode *mode,
37 struct drm_display_mode *adjusted_mode)
38{
39 struct drm_device *dev = crtc->dev;
40 struct radeon_device *rdev = dev->dev_private;
41 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
42 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
43 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
44 int a1, a2;
45
46 memset(&args, 0, sizeof(args));
47
Jerome Glissec93bb852009-07-13 21:04:08 +020048 args.ucCRTC = radeon_crtc->crtc_id;
49
50 switch (radeon_crtc->rmx_type) {
51 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050052 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
54 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
55 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020056 break;
57 case RMX_ASPECT:
58 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
59 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
60
61 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050062 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
63 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020064 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040065 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
66 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020067 }
Jerome Glissec93bb852009-07-13 21:04:08 +020068 break;
69 case RMX_FULL:
70 default:
Cédric Cano45894332011-02-11 19:45:37 -050071 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
73 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
74 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020075 break;
76 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040077 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020078}
79
80static void atombios_scaler_setup(struct drm_crtc *crtc)
81{
82 struct drm_device *dev = crtc->dev;
83 struct radeon_device *rdev = dev->dev_private;
84 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
85 ENABLE_SCALER_PS_ALLOCATION args;
86 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Alex Deucher5df31962012-09-13 11:52:08 -040087 struct radeon_encoder *radeon_encoder =
88 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020089 /* fixme - fill in enc_priv for atom dac */
90 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100091 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020092
93 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
94 return;
95
Alex Deucher5df31962012-09-13 11:52:08 -040096 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
97 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
98 tv_std = tv_dac->tv_std;
99 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000100 }
101
Jerome Glissec93bb852009-07-13 21:04:08 +0200102 memset(&args, 0, sizeof(args));
103
104 args.ucScaler = radeon_crtc->crtc_id;
105
Dave Airlie4ce001a2009-08-13 16:32:14 +1000106 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200107 switch (tv_std) {
108 case TV_STD_NTSC:
109 default:
110 args.ucTVStandard = ATOM_TV_NTSC;
111 break;
112 case TV_STD_PAL:
113 args.ucTVStandard = ATOM_TV_PAL;
114 break;
115 case TV_STD_PAL_M:
116 args.ucTVStandard = ATOM_TV_PALM;
117 break;
118 case TV_STD_PAL_60:
119 args.ucTVStandard = ATOM_TV_PAL60;
120 break;
121 case TV_STD_NTSC_J:
122 args.ucTVStandard = ATOM_TV_NTSCJ;
123 break;
124 case TV_STD_SCART_PAL:
125 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
126 break;
127 case TV_STD_SECAM:
128 args.ucTVStandard = ATOM_TV_SECAM;
129 break;
130 case TV_STD_PAL_CN:
131 args.ucTVStandard = ATOM_TV_PALCN;
132 break;
133 }
134 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000135 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200136 args.ucTVStandard = ATOM_TV_CV;
137 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
138 } else {
139 switch (radeon_crtc->rmx_type) {
140 case RMX_FULL:
141 args.ucEnable = ATOM_SCALER_EXPANSION;
142 break;
143 case RMX_CENTER:
144 args.ucEnable = ATOM_SCALER_CENTER;
145 break;
146 case RMX_ASPECT:
147 args.ucEnable = ATOM_SCALER_EXPANSION;
148 break;
149 default:
150 if (ASIC_IS_AVIVO(rdev))
151 args.ucEnable = ATOM_SCALER_DISABLE;
152 else
153 args.ucEnable = ATOM_SCALER_CENTER;
154 break;
155 }
156 }
157 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000158 if ((is_tv || is_cv)
159 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
160 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200161 }
162}
163
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200164static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
165{
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
167 struct drm_device *dev = crtc->dev;
168 struct radeon_device *rdev = dev->dev_private;
169 int index =
170 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
171 ENABLE_CRTC_PS_ALLOCATION args;
172
173 memset(&args, 0, sizeof(args));
174
175 args.ucCRTC = radeon_crtc->crtc_id;
176 args.ucEnable = lock;
177
178 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
179}
180
181static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
182{
183 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
184 struct drm_device *dev = crtc->dev;
185 struct radeon_device *rdev = dev->dev_private;
186 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
187 ENABLE_CRTC_PS_ALLOCATION args;
188
189 memset(&args, 0, sizeof(args));
190
191 args.ucCRTC = radeon_crtc->crtc_id;
192 args.ucEnable = state;
193
194 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
195}
196
197static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
198{
199 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
200 struct drm_device *dev = crtc->dev;
201 struct radeon_device *rdev = dev->dev_private;
202 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
203 ENABLE_CRTC_PS_ALLOCATION args;
204
205 memset(&args, 0, sizeof(args));
206
207 args.ucCRTC = radeon_crtc->crtc_id;
208 args.ucEnable = state;
209
210 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
211}
212
Alex Deucher78fe9e52014-01-28 23:49:37 -0500213static const u32 vga_control_regs[6] =
214{
215 AVIVO_D1VGA_CONTROL,
216 AVIVO_D2VGA_CONTROL,
217 EVERGREEN_D3VGA_CONTROL,
218 EVERGREEN_D4VGA_CONTROL,
219 EVERGREEN_D5VGA_CONTROL,
220 EVERGREEN_D6VGA_CONTROL,
221};
222
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200223static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
224{
225 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
226 struct drm_device *dev = crtc->dev;
227 struct radeon_device *rdev = dev->dev_private;
228 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
229 BLANK_CRTC_PS_ALLOCATION args;
Alex Deucher78fe9e52014-01-28 23:49:37 -0500230 u32 vga_control = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200231
232 memset(&args, 0, sizeof(args));
233
Alex Deucher78fe9e52014-01-28 23:49:37 -0500234 if (ASIC_IS_DCE8(rdev)) {
235 vga_control = RREG32(vga_control_regs[radeon_crtc->crtc_id]);
236 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control | 1);
237 }
238
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239 args.ucCRTC = radeon_crtc->crtc_id;
240 args.ucBlanking = state;
241
242 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucher78fe9e52014-01-28 23:49:37 -0500243
244 if (ASIC_IS_DCE8(rdev)) {
245 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control);
246 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200247}
248
Alex Deucherfef9f912012-03-20 17:18:03 -0400249static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
250{
251 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
252 struct drm_device *dev = crtc->dev;
253 struct radeon_device *rdev = dev->dev_private;
254 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
255 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
256
257 memset(&args, 0, sizeof(args));
258
259 args.ucDispPipeId = radeon_crtc->crtc_id;
260 args.ucEnable = state;
261
262 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
263}
264
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
266{
267 struct drm_device *dev = crtc->dev;
268 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500269 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270
271 switch (mode) {
272 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400273 radeon_crtc->enabled = true;
Alex Deucher37b43902010-02-09 12:04:43 -0500274 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400275 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500276 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
277 atombios_blank_crtc(crtc, ATOM_DISABLE);
Michel Dänzer5e916a32016-04-01 17:28:44 +0900278 if (dev->num_crtcs > radeon_crtc->crtc_id)
Gustavo Padovan5c9ac112016-06-07 11:08:00 -0300279 drm_crtc_vblank_on(crtc);
Alex Deucher500b7582009-12-02 11:46:52 -0500280 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281 break;
282 case DRM_MODE_DPMS_STANDBY:
283 case DRM_MODE_DPMS_SUSPEND:
284 case DRM_MODE_DPMS_OFF:
Michel Dänzer5e916a32016-04-01 17:28:44 +0900285 if (dev->num_crtcs > radeon_crtc->crtc_id)
Gustavo Padovan5c9ac112016-06-07 11:08:00 -0300286 drm_crtc_vblank_off(crtc);
Alex Deuchera93f3442010-12-20 11:22:29 -0500287 if (radeon_crtc->enabled)
288 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400289 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500290 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
291 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400292 radeon_crtc->enabled = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293 break;
294 }
Alex Deucher3640da22014-05-30 12:40:15 -0400295 /* adjust pm to dpms */
296 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200297}
298
299static void
300atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400301 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200302{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400303 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200304 struct drm_device *dev = crtc->dev;
305 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400306 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200307 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400308 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200309
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400310 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400311 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400312 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400313 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
314 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400315 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400316 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400317 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400318 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400319 args.usH_SyncWidth =
320 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
321 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400322 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400323 args.usV_SyncWidth =
324 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400325 args.ucH_Border = radeon_crtc->h_border;
326 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400327
328 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
329 misc |= ATOM_VSYNC_POLARITY;
330 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
331 misc |= ATOM_HSYNC_POLARITY;
332 if (mode->flags & DRM_MODE_FLAG_CSYNC)
333 misc |= ATOM_COMPOSITESYNC;
334 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
335 misc |= ATOM_INTERLACE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500336 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400337 misc |= ATOM_DOUBLE_CLOCK_MODE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500338 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
339 misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400340
341 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
342 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200343
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400344 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200345}
346
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400347static void atombios_crtc_set_timing(struct drm_crtc *crtc,
348 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200349{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400350 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351 struct drm_device *dev = crtc->dev;
352 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400353 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200354 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400355 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200356
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400357 memset(&args, 0, sizeof(args));
358 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
359 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
360 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
361 args.usH_SyncWidth =
362 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
363 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
364 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
365 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
366 args.usV_SyncWidth =
367 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
368
Alex Deucher54bfe492010-09-03 15:52:53 -0400369 args.ucOverscanRight = radeon_crtc->h_border;
370 args.ucOverscanLeft = radeon_crtc->h_border;
371 args.ucOverscanBottom = radeon_crtc->v_border;
372 args.ucOverscanTop = radeon_crtc->v_border;
373
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400374 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
375 misc |= ATOM_VSYNC_POLARITY;
376 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
377 misc |= ATOM_HSYNC_POLARITY;
378 if (mode->flags & DRM_MODE_FLAG_CSYNC)
379 misc |= ATOM_COMPOSITESYNC;
380 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
381 misc |= ATOM_INTERLACE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500382 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400383 misc |= ATOM_DOUBLE_CLOCK_MODE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500384 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
385 misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400386
387 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
388 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400390 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200391}
392
Alex Deucher3fa47d92012-01-20 14:56:39 -0500393static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500394{
Alex Deucherb7922102010-03-06 10:57:30 -0500395 u32 ss_cntl;
396
397 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500398 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500399 case ATOM_PPLL1:
400 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
401 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
402 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
403 break;
404 case ATOM_PPLL2:
405 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
406 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
407 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
408 break;
409 case ATOM_DCPLL:
410 case ATOM_PPLL_INVALID:
411 return;
412 }
413 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500414 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500415 case ATOM_PPLL1:
416 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
417 ss_cntl &= ~1;
418 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
419 break;
420 case ATOM_PPLL2:
421 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
422 ss_cntl &= ~1;
423 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
424 break;
425 case ATOM_DCPLL:
426 case ATOM_PPLL_INVALID:
427 return;
428 }
429 }
430}
431
432
Alex Deucher26b9fc32010-02-01 16:39:11 -0500433union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400434 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
435 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500436 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400437 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500438 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500439};
440
Alex Deucher3fa47d92012-01-20 14:56:39 -0500441static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400442 int enable,
443 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400444 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400445 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400446{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400447 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400448 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500449 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400450
Alex Deucherc4756ba2014-01-15 13:59:47 -0500451 if (enable) {
452 /* Don't mess with SS if percentage is 0 or external ss.
453 * SS is already disabled previously, and disabling it
454 * again can cause display problems if the pll is already
455 * programmed.
456 */
457 if (ss->percentage == 0)
458 return;
459 if (ss->type & ATOM_EXTERNAL_SS_MASK)
460 return;
461 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400462 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400463 if (rdev->mode_info.crtcs[i] &&
464 rdev->mode_info.crtcs[i]->enabled &&
465 i != crtc_id &&
466 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
467 /* one other crtc is using this pll don't turn
468 * off spread spectrum as it might turn off
469 * display on active crtc
470 */
471 return;
472 }
473 }
474 }
475
Alex Deucher26b9fc32010-02-01 16:39:11 -0500476 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400477
Alex Deuchera572eaa2011-01-06 21:19:16 -0500478 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500479 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400480 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500481 switch (pll_id) {
482 case ATOM_PPLL1:
483 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500484 break;
485 case ATOM_PPLL2:
486 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500487 break;
488 case ATOM_DCPLL:
489 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500490 break;
491 case ATOM_PPLL_INVALID:
492 return;
493 }
Alex Deucherf312f092012-07-17 14:02:44 -0400494 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
495 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400496 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500497 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400498 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400499 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400500 switch (pll_id) {
501 case ATOM_PPLL1:
502 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400503 break;
504 case ATOM_PPLL2:
505 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400506 break;
507 case ATOM_DCPLL:
508 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400509 break;
510 case ATOM_PPLL_INVALID:
511 return;
512 }
Alex Deucherf312f092012-07-17 14:02:44 -0400513 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
514 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400515 args.v2.ucEnable = enable;
516 } else if (ASIC_IS_DCE3(rdev)) {
517 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400518 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400519 args.v1.ucSpreadSpectrumStep = ss->step;
520 args.v1.ucSpreadSpectrumDelay = ss->delay;
521 args.v1.ucSpreadSpectrumRange = ss->range;
522 args.v1.ucPpll = pll_id;
523 args.v1.ucEnable = enable;
524 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400525 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
526 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500527 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400528 return;
529 }
530 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400531 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400532 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
533 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
534 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
535 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400536 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500537 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500538 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400539 return;
540 }
541 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400542 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400543 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
544 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
545 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400546 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500547 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400548}
549
Alex Deucher4eaeca32010-01-19 17:32:27 -0500550union adjust_pixel_clock {
551 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500552 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500553};
554
555static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400556 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200557{
Alex Deucher19eca432012-09-13 10:56:16 -0400558 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200559 struct drm_device *dev = crtc->dev;
560 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400561 struct drm_encoder *encoder = radeon_crtc->encoder;
562 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
563 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500564 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400565 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400566 u32 dp_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400567 u32 clock = mode->clock;
Alex Deucher7d5a33b2014-02-03 15:53:25 -0500568 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400569 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500570
Alex Deucher4eaeca32010-01-19 17:32:27 -0500571 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400572 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200573
574 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400575 if ((rdev->family == CHIP_RS600) ||
576 (rdev->family == CHIP_RS690) ||
577 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400578 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
579 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000580
581 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400582 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000583 else
Alex Deucher19eca432012-09-13 10:56:16 -0400584 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400585
Alex Deucher5785e532011-04-19 15:24:59 -0400586 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400587 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400588 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500589 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400590 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400591 /* use frac fb div on RS780/RS880 */
Christian König9ef85372016-06-13 16:09:53 +0200592 if (((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
593 && !radeon_crtc->ss_enabled)
Alex Deucher41167822013-04-01 16:06:25 -0400594 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500595 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
596 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000597 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400598 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200599
Dave Airlie5480f722010-10-19 10:36:47 +1000600 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400601 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000602 else
Alex Deucher19eca432012-09-13 10:56:16 -0400603 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000604 }
605
Alex Deucher5df31962012-09-13 11:52:08 -0400606 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
607 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
608 if (connector) {
609 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
610 struct radeon_connector_atom_dig *dig_connector =
611 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400612
Alex Deucher5df31962012-09-13 11:52:08 -0400613 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200614 }
615 }
616
Dave Airlie9843ead2015-02-24 09:24:04 +1000617 if (radeon_encoder->is_mst_encoder) {
618 struct radeon_encoder_mst *mst_enc = radeon_encoder->enc_priv;
619 struct radeon_connector_atom_dig *dig_connector = mst_enc->connector->con_priv;
620
621 dp_clock = dig_connector->dp_clock;
622 }
623
Alex Deucher5df31962012-09-13 11:52:08 -0400624 /* use recommended ref_div for ss */
625 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
626 if (radeon_crtc->ss_enabled) {
627 if (radeon_crtc->ss.refdiv) {
628 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
629 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
Christian Königae5b80d2016-08-18 11:51:14 +0200630 if (ASIC_IS_AVIVO(rdev) &&
631 rdev->family != CHIP_RS780 &&
632 rdev->family != CHIP_RS880)
Alex Deucher5df31962012-09-13 11:52:08 -0400633 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
634 }
635 }
636 }
637
638 if (ASIC_IS_AVIVO(rdev)) {
639 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
640 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
641 adjusted_clock = mode->clock * 2;
642 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
643 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
644 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
645 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
646 } else {
647 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
648 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
649 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
650 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
651 }
652
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400653 /* adjust pll for deep color modes */
654 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
655 switch (bpc) {
656 case 8:
657 default:
658 break;
659 case 10:
660 clock = (clock * 5) / 4;
661 break;
662 case 12:
663 clock = (clock * 3) / 2;
664 break;
665 case 16:
666 clock = clock * 2;
667 break;
668 }
669 }
670
Alex Deucher2606c882009-10-08 13:36:21 -0400671 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
672 * accordingly based on the encoder/transmitter to work around
673 * special hw requirements.
674 */
675 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500676 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500677 u8 frev, crev;
678 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400679
Alex Deucher2606c882009-10-08 13:36:21 -0400680 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400681 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
682 &crev))
683 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500684
685 memset(&args, 0, sizeof(args));
686
687 switch (frev) {
688 case 1:
689 switch (crev) {
690 case 1:
691 case 2:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400692 args.v1.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500693 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500694 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400695 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400696 args.v1.ucConfig |=
697 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500698
699 atom_execute_table(rdev->mode_info.atom_context,
700 index, (uint32_t *)&args);
701 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
702 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500703 case 3:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400704 args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500705 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
706 args.v3.sInput.ucEncodeMode = encoder_mode;
707 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400708 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000709 args.v3.sInput.ucDispPllConfig |=
710 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400711 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400712 args.v3.sInput.ucDispPllConfig |=
713 DISPPLL_CONFIG_COHERENT_MODE;
714 /* 16200 or 27000 */
715 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
716 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500717 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400718 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500719 args.v3.sInput.ucDispPllConfig |=
720 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500721 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500722 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400723 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500724 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400725 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
726 ENCODER_OBJECT_ID_NONE)
727 args.v3.sInput.ucExtTransmitterID =
728 radeon_encoder_get_dp_bridge_encoder_id(encoder);
729 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400730 args.v3.sInput.ucExtTransmitterID = 0;
731
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500732 atom_execute_table(rdev->mode_info.atom_context,
733 index, (uint32_t *)&args);
734 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
735 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400736 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
737 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
738 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500739 }
740 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400741 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
742 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
743 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500744 }
745 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500746 default:
747 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
748 return adjusted_clock;
749 }
750 break;
751 default:
752 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
753 return adjusted_clock;
754 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400755 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500756 return adjusted_clock;
757}
758
759union set_pixel_clock {
760 SET_PIXEL_CLOCK_PS_ALLOCATION base;
761 PIXEL_CLOCK_PARAMETERS v1;
762 PIXEL_CLOCK_PARAMETERS_V2 v2;
763 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500764 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500765 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500766};
767
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500768/* on DCE5, make sure the voltage is high enough to support the
769 * required disp clk.
770 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400771static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500772 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500773{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500774 u8 frev, crev;
775 int index;
776 union set_pixel_clock args;
777
778 memset(&args, 0, sizeof(args));
779
780 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400781 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
782 &crev))
783 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500784
785 switch (frev) {
786 case 1:
787 switch (crev) {
788 case 5:
789 /* if the default dcpll clock is specified,
790 * SetPixelClock provides the dividers
791 */
792 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500793 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500794 args.v5.ucPpll = ATOM_DCPLL;
795 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500796 case 6:
797 /* if the default dcpll clock is specified,
798 * SetPixelClock provides the dividers
799 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500800 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400801 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400802 args.v6.ucPpll = ATOM_EXT_PLL1;
803 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400804 args.v6.ucPpll = ATOM_PPLL0;
805 else
806 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500807 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500808 default:
809 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
810 return;
811 }
812 break;
813 default:
814 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
815 return;
816 }
817 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
818}
819
Alex Deucher37f90032010-06-11 17:58:38 -0400820static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000821 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400822 int pll_id,
823 u32 encoder_mode,
824 u32 encoder_id,
825 u32 clock,
826 u32 ref_div,
827 u32 fb_div,
828 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400829 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400830 int bpc,
831 bool ss_enabled,
832 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400833{
834 struct drm_device *dev = crtc->dev;
835 struct radeon_device *rdev = dev->dev_private;
836 u8 frev, crev;
837 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
838 union set_pixel_clock args;
839
840 memset(&args, 0, sizeof(args));
841
842 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
843 &crev))
844 return;
845
846 switch (frev) {
847 case 1:
848 switch (crev) {
849 case 1:
850 if (clock == ATOM_DISABLE)
851 return;
852 args.v1.usPixelClock = cpu_to_le16(clock / 10);
853 args.v1.usRefDiv = cpu_to_le16(ref_div);
854 args.v1.usFbDiv = cpu_to_le16(fb_div);
855 args.v1.ucFracFbDiv = frac_fb_div;
856 args.v1.ucPostDiv = post_div;
857 args.v1.ucPpll = pll_id;
858 args.v1.ucCRTC = crtc_id;
859 args.v1.ucRefDivSrc = 1;
860 break;
861 case 2:
862 args.v2.usPixelClock = cpu_to_le16(clock / 10);
863 args.v2.usRefDiv = cpu_to_le16(ref_div);
864 args.v2.usFbDiv = cpu_to_le16(fb_div);
865 args.v2.ucFracFbDiv = frac_fb_div;
866 args.v2.ucPostDiv = post_div;
867 args.v2.ucPpll = pll_id;
868 args.v2.ucCRTC = crtc_id;
869 args.v2.ucRefDivSrc = 1;
870 break;
871 case 3:
872 args.v3.usPixelClock = cpu_to_le16(clock / 10);
873 args.v3.usRefDiv = cpu_to_le16(ref_div);
874 args.v3.usFbDiv = cpu_to_le16(fb_div);
875 args.v3.ucFracFbDiv = frac_fb_div;
876 args.v3.ucPostDiv = post_div;
877 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400878 if (crtc_id == ATOM_CRTC2)
879 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
880 else
881 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400882 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
883 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400884 args.v3.ucTransmitterId = encoder_id;
885 args.v3.ucEncoderMode = encoder_mode;
886 break;
887 case 5:
888 args.v5.ucCRTC = crtc_id;
889 args.v5.usPixelClock = cpu_to_le16(clock / 10);
890 args.v5.ucRefDiv = ref_div;
891 args.v5.usFbDiv = cpu_to_le16(fb_div);
892 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
893 args.v5.ucPostDiv = post_div;
894 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400895 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
896 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400897 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
898 switch (bpc) {
899 case 8:
900 default:
901 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
902 break;
903 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400904 /* yes this is correct, the atom define is wrong */
905 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
906 break;
907 case 12:
908 /* yes this is correct, the atom define is wrong */
Alex Deucher7d5ab302014-04-21 21:45:09 -0400909 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
910 break;
911 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400912 }
Alex Deucher37f90032010-06-11 17:58:38 -0400913 args.v5.ucTransmitterID = encoder_id;
914 args.v5.ucEncoderMode = encoder_mode;
915 args.v5.ucPpll = pll_id;
916 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500917 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000918 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500919 args.v6.ucRefDiv = ref_div;
920 args.v6.usFbDiv = cpu_to_le16(fb_div);
921 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
922 args.v6.ucPostDiv = post_div;
923 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400924 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
925 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400926 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
927 switch (bpc) {
928 case 8:
929 default:
930 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
931 break;
932 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400933 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400934 break;
935 case 12:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400936 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400937 break;
938 case 16:
939 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
940 break;
941 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400942 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500943 args.v6.ucTransmitterID = encoder_id;
944 args.v6.ucEncoderMode = encoder_mode;
945 args.v6.ucPpll = pll_id;
946 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400947 default:
948 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
949 return;
950 }
951 break;
952 default:
953 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
954 return;
955 }
956
957 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
958}
959
Alex Deucher19eca432012-09-13 10:56:16 -0400960static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
961{
962 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
963 struct drm_device *dev = crtc->dev;
964 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400965 struct radeon_encoder *radeon_encoder =
966 to_radeon_encoder(radeon_crtc->encoder);
967 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400968
969 radeon_crtc->bpc = 8;
970 radeon_crtc->ss_enabled = false;
971
Dave Airlie9843ead2015-02-24 09:24:04 +1000972 if (radeon_encoder->is_mst_encoder) {
973 radeon_dp_mst_prepare_pll(crtc, mode);
974 } else if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400975 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400976 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
977 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400978 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400979 struct radeon_connector *radeon_connector =
980 to_radeon_connector(connector);
981 struct radeon_connector_atom_dig *dig_connector =
982 radeon_connector->con_priv;
983 int dp_clock;
Mario Kleinerea292862014-06-05 09:58:24 -0400984
985 /* Assign mode clock for hdmi deep color max clock limit check */
986 radeon_connector->pixelclock_for_modeset = mode->clock;
Alex Deucher19eca432012-09-13 10:56:16 -0400987 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
988
989 switch (encoder_mode) {
990 case ATOM_ENCODER_MODE_DP_MST:
991 case ATOM_ENCODER_MODE_DP:
992 /* DP/eDP */
993 dp_clock = dig_connector->dp_clock / 10;
994 if (ASIC_IS_DCE4(rdev))
995 radeon_crtc->ss_enabled =
996 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
997 ASIC_INTERNAL_SS_ON_DP,
998 dp_clock);
999 else {
1000 if (dp_clock == 16200) {
1001 radeon_crtc->ss_enabled =
1002 radeon_atombios_get_ppll_ss_info(rdev,
1003 &radeon_crtc->ss,
1004 ATOM_DP_SS_ID2);
1005 if (!radeon_crtc->ss_enabled)
1006 radeon_crtc->ss_enabled =
1007 radeon_atombios_get_ppll_ss_info(rdev,
1008 &radeon_crtc->ss,
1009 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -05001010 } else {
Alex Deucher19eca432012-09-13 10:56:16 -04001011 radeon_crtc->ss_enabled =
1012 radeon_atombios_get_ppll_ss_info(rdev,
1013 &radeon_crtc->ss,
1014 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -05001015 }
1016 /* disable spread spectrum on DCE3 DP */
1017 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -04001018 }
1019 break;
1020 case ATOM_ENCODER_MODE_LVDS:
1021 if (ASIC_IS_DCE4(rdev))
1022 radeon_crtc->ss_enabled =
1023 radeon_atombios_get_asic_ss_info(rdev,
1024 &radeon_crtc->ss,
1025 dig->lcd_ss_id,
1026 mode->clock / 10);
1027 else
1028 radeon_crtc->ss_enabled =
1029 radeon_atombios_get_ppll_ss_info(rdev,
1030 &radeon_crtc->ss,
1031 dig->lcd_ss_id);
1032 break;
1033 case ATOM_ENCODER_MODE_DVI:
1034 if (ASIC_IS_DCE4(rdev))
1035 radeon_crtc->ss_enabled =
1036 radeon_atombios_get_asic_ss_info(rdev,
1037 &radeon_crtc->ss,
1038 ASIC_INTERNAL_SS_ON_TMDS,
1039 mode->clock / 10);
1040 break;
1041 case ATOM_ENCODER_MODE_HDMI:
1042 if (ASIC_IS_DCE4(rdev))
1043 radeon_crtc->ss_enabled =
1044 radeon_atombios_get_asic_ss_info(rdev,
1045 &radeon_crtc->ss,
1046 ASIC_INTERNAL_SS_ON_HDMI,
1047 mode->clock / 10);
1048 break;
1049 default:
1050 break;
1051 }
1052 }
1053
1054 /* adjust pixel clock as needed */
1055 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1056
1057 return true;
1058}
1059
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001060static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001061{
1062 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1063 struct drm_device *dev = crtc->dev;
1064 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001065 struct radeon_encoder *radeon_encoder =
1066 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001067 u32 pll_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001068 u32 clock = mode->clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001069 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1070 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001071 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001072
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001073 /* pass the actual clock to atombios_crtc_program_pll for DCE5,6 for HDMI */
Mario Kleiner5c868222014-06-15 20:36:29 +02001074 if (ASIC_IS_DCE5(rdev) &&
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001075 (encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
1076 (radeon_crtc->bpc > 8))
1077 clock = radeon_crtc->adjusted_clock;
1078
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001079 switch (radeon_crtc->pll_id) {
1080 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001081 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001082 break;
1083 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001084 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001085 break;
1086 case ATOM_DCPLL:
1087 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001088 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001089 pll = &rdev->clock.dcpll;
1090 break;
1091 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001092
Alex Deucher19eca432012-09-13 10:56:16 -04001093 /* update pll params */
1094 pll->flags = radeon_crtc->pll_flags;
1095 pll->reference_div = radeon_crtc->pll_reference_div;
1096 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001097
Alex Deucher64146f82011-03-22 01:46:12 -04001098 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1099 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001100 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1101 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001102 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001103 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1104 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001105 else
Alex Deucher19eca432012-09-13 10:56:16 -04001106 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1107 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001108
Alex Deucher19eca432012-09-13 10:56:16 -04001109 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1110 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001111
Alex Deucher37f90032010-06-11 17:58:38 -04001112 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001113 encoder_mode, radeon_encoder->encoder_id, clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001114 ref_div, fb_div, frac_fb_div, post_div,
1115 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001116
Alex Deucher19eca432012-09-13 10:56:16 -04001117 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001118 /* calculate ss amount and step size */
1119 if (ASIC_IS_DCE4(rdev)) {
1120 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001121 u32 amount = (((fb_div * 10) + frac_fb_div) *
1122 (u32)radeon_crtc->ss.percentage) /
1123 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001124 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1125 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001126 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001127 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001128 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001129 (125 * 25 * pll->reference_freq / 100);
1130 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001131 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001132 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001133 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001134 }
1135
Alex Deucher19eca432012-09-13 10:56:16 -04001136 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1137 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001138 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001139}
1140
Alex Deucherc9417bd2011-02-06 14:23:26 -05001141static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1142 struct drm_framebuffer *fb,
1143 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001144{
1145 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1146 struct drm_device *dev = crtc->dev;
1147 struct radeon_device *rdev = dev->dev_private;
1148 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001149 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001150 struct drm_gem_object *obj;
1151 struct radeon_bo *rbo;
1152 uint64_t fb_location;
1153 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001154 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001155 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001156 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001157 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001158 bool bypass_lut = false;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001159
1160 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001161 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001162 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001163 return 0;
1164 }
1165
Chris Ball4dd19b02010-09-26 06:47:23 -05001166 if (atomic) {
1167 radeon_fb = to_radeon_framebuffer(fb);
1168 target_fb = fb;
1169 }
1170 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001171 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1172 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001173 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001174
Chris Ball4dd19b02010-09-26 06:47:23 -05001175 /* If atomic, assume fb object is pinned & idle & fenced and
1176 * just update base pointers
1177 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001178 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001179 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001180 r = radeon_bo_reserve(rbo, false);
1181 if (unlikely(r != 0))
1182 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001183
1184 if (atomic)
1185 fb_location = radeon_bo_gpu_offset(rbo);
1186 else {
1187 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1188 if (unlikely(r != 0)) {
1189 radeon_bo_unreserve(rbo);
1190 return -EINVAL;
1191 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001192 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001193
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001194 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1195 radeon_bo_unreserve(rbo);
1196
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001197 switch (target_fb->pixel_format) {
1198 case DRM_FORMAT_C8:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001199 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1200 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1201 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001202 case DRM_FORMAT_XRGB4444:
1203 case DRM_FORMAT_ARGB4444:
1204 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1205 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB4444));
1206#ifdef __BIG_ENDIAN
1207 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1208#endif
1209 break;
1210 case DRM_FORMAT_XRGB1555:
1211 case DRM_FORMAT_ARGB1555:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001212 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1213 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001214#ifdef __BIG_ENDIAN
1215 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1216#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001217 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001218 case DRM_FORMAT_BGRX5551:
1219 case DRM_FORMAT_BGRA5551:
1220 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1221 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA5551));
1222#ifdef __BIG_ENDIAN
1223 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1224#endif
1225 break;
1226 case DRM_FORMAT_RGB565:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001227 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1228 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001229#ifdef __BIG_ENDIAN
1230 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1231#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001232 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001233 case DRM_FORMAT_XRGB8888:
1234 case DRM_FORMAT_ARGB8888:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001235 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1236 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001237#ifdef __BIG_ENDIAN
1238 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1239#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001240 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001241 case DRM_FORMAT_XRGB2101010:
1242 case DRM_FORMAT_ARGB2101010:
1243 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1244 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB2101010));
1245#ifdef __BIG_ENDIAN
1246 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1247#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001248 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1249 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001250 break;
1251 case DRM_FORMAT_BGRX1010102:
1252 case DRM_FORMAT_BGRA1010102:
1253 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1254 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA1010102));
1255#ifdef __BIG_ENDIAN
1256 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1257#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001258 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1259 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001260 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001261 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001262 DRM_ERROR("Unsupported screen format %s\n",
1263 drm_get_format_name(target_fb->pixel_format));
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001264 return -EINVAL;
1265 }
1266
Alex Deucher392e3722011-11-28 14:49:27 -05001267 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001268 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001269
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001270 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001271 if (rdev->family >= CHIP_TAHITI) {
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001272 unsigned index, num_banks;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001273
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001274 if (rdev->family >= CHIP_BONAIRE) {
1275 unsigned tileb, tile_split_bytes;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001276
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001277 /* Calculate the macrotile mode index. */
1278 tile_split_bytes = 64 << tile_split;
1279 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1280 tileb = min(tile_split_bytes, tileb);
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001281
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001282 for (index = 0; tileb > 64; index++)
1283 tileb >>= 1;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001284
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001285 if (index >= 16) {
1286 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1287 target_fb->bits_per_pixel, tile_split);
1288 return -EINVAL;
1289 }
1290
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001291 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001292 } else {
1293 switch (target_fb->bits_per_pixel) {
1294 case 8:
1295 index = 10;
1296 break;
1297 case 16:
1298 index = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
1299 break;
1300 default:
1301 case 32:
1302 index = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
1303 break;
1304 }
1305
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001306 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001307 }
1308
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001309 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1310 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001311 /* NI and older. */
1312 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001313 tmp = rdev->config.cayman.tile_config;
1314 else
1315 tmp = rdev->config.evergreen.tile_config;
1316
1317 switch ((tmp & 0xf0) >> 4) {
1318 case 0: /* 4 banks */
1319 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1320 break;
1321 case 1: /* 8 banks */
1322 default:
1323 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1324 break;
1325 case 2: /* 16 banks */
1326 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1327 break;
1328 }
Alex Deucher392e3722011-11-28 14:49:27 -05001329 }
1330
Alex Deucher97d66322010-05-20 12:12:48 -04001331 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001332 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1333 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1334 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1335 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001336 if (rdev->family >= CHIP_BONAIRE) {
1337 /* XXX need to know more about the surface tiling mode */
1338 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1339 }
Alex Deucher392e3722011-11-28 14:49:27 -05001340 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001341 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1342
Alex Deucher8da0e502012-07-11 18:38:29 -04001343 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001344 /* Read the pipe config from the 2D TILED SCANOUT mode.
1345 * It should be the same for the other modes too, but not all
1346 * modes set the pipe config field. */
1347 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1348
1349 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001350 } else if ((rdev->family == CHIP_TAHITI) ||
1351 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001352 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001353 else if ((rdev->family == CHIP_VERDE) ||
1354 (rdev->family == CHIP_OLAND) ||
1355 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001356 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1357
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001358 switch (radeon_crtc->crtc_id) {
1359 case 0:
1360 WREG32(AVIVO_D1VGA_CONTROL, 0);
1361 break;
1362 case 1:
1363 WREG32(AVIVO_D2VGA_CONTROL, 0);
1364 break;
1365 case 2:
1366 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1367 break;
1368 case 3:
1369 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1370 break;
1371 case 4:
1372 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1373 break;
1374 case 5:
1375 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1376 break;
1377 default:
1378 break;
1379 }
1380
Michel Dänzerc63dd752016-04-01 18:51:34 +09001381 /* Make sure surface address is updated at vertical blank rather than
1382 * horizontal blank
1383 */
1384 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, 0);
1385
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001386 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1387 upper_32_bits(fb_location));
1388 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1389 upper_32_bits(fb_location));
1390 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1391 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1392 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1393 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1394 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001395 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001396
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001397 /*
1398 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
1399 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
1400 * retain the full precision throughout the pipeline.
1401 */
1402 WREG32_P(EVERGREEN_GRPH_LUT_10BIT_BYPASS_CONTROL + radeon_crtc->crtc_offset,
1403 (bypass_lut ? EVERGREEN_LUT_10BIT_BYPASS_EN : 0),
1404 ~EVERGREEN_LUT_10BIT_BYPASS_EN);
1405
1406 if (bypass_lut)
1407 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1408
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001409 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1410 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1411 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1412 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001413 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1414 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001415
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001416 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001417 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1418 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1419
Alex Deucher8da0e502012-07-11 18:38:29 -04001420 if (rdev->family >= CHIP_BONAIRE)
1421 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1422 target_fb->height);
1423 else
1424 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1425 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001426 x &= ~3;
1427 y &= ~1;
1428 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1429 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001430 viewport_w = crtc->mode.hdisplay;
1431 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucher77ae5f42015-03-03 17:00:43 -05001432 if ((rdev->family >= CHIP_BONAIRE) &&
1433 (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE))
1434 viewport_h *= 2;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001435 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001436 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001437
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001438 /* set pageflip to happen only at start of vblank interval (front porch) */
1439 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001440
Matt Roperf4510a22014-04-01 15:22:40 -07001441 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001442 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001443 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001444 r = radeon_bo_reserve(rbo, false);
1445 if (unlikely(r != 0))
1446 return r;
1447 radeon_bo_unpin(rbo);
1448 radeon_bo_unreserve(rbo);
1449 }
1450
1451 /* Bytes per pixel may have changed */
1452 radeon_bandwidth_update(rdev);
1453
1454 return 0;
1455}
1456
Chris Ball4dd19b02010-09-26 06:47:23 -05001457static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1458 struct drm_framebuffer *fb,
1459 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001460{
1461 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1462 struct drm_device *dev = crtc->dev;
1463 struct radeon_device *rdev = dev->dev_private;
1464 struct radeon_framebuffer *radeon_fb;
1465 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001466 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001467 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001468 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001469 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001470 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Michel Dänzerc63dd752016-04-01 18:51:34 +09001471 u32 viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001472 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001473 bool bypass_lut = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001474
Jerome Glisse2de3b482009-11-17 14:08:55 -08001475 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001476 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001477 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001478 return 0;
1479 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001480
Chris Ball4dd19b02010-09-26 06:47:23 -05001481 if (atomic) {
1482 radeon_fb = to_radeon_framebuffer(fb);
1483 target_fb = fb;
1484 }
1485 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001486 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1487 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001488 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001489
1490 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001491 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001492 r = radeon_bo_reserve(rbo, false);
1493 if (unlikely(r != 0))
1494 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001495
1496 /* If atomic, assume fb object is pinned & idle & fenced and
1497 * just update base pointers
1498 */
1499 if (atomic)
1500 fb_location = radeon_bo_gpu_offset(rbo);
1501 else {
1502 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1503 if (unlikely(r != 0)) {
1504 radeon_bo_unreserve(rbo);
1505 return -EINVAL;
1506 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001507 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001508 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1509 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001510
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001511 switch (target_fb->pixel_format) {
1512 case DRM_FORMAT_C8:
Dave Airlie41456df2009-09-16 10:15:21 +10001513 fb_format =
1514 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1515 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1516 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001517 case DRM_FORMAT_XRGB4444:
1518 case DRM_FORMAT_ARGB4444:
1519 fb_format =
1520 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1521 AVIVO_D1GRPH_CONTROL_16BPP_ARGB4444;
1522#ifdef __BIG_ENDIAN
1523 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1524#endif
1525 break;
1526 case DRM_FORMAT_XRGB1555:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001527 fb_format =
1528 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1529 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001530#ifdef __BIG_ENDIAN
1531 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1532#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001533 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001534 case DRM_FORMAT_RGB565:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001535 fb_format =
1536 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1537 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001538#ifdef __BIG_ENDIAN
1539 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1540#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001541 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001542 case DRM_FORMAT_XRGB8888:
1543 case DRM_FORMAT_ARGB8888:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001544 fb_format =
1545 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1546 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001547#ifdef __BIG_ENDIAN
1548 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1549#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001550 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001551 case DRM_FORMAT_XRGB2101010:
1552 case DRM_FORMAT_ARGB2101010:
1553 fb_format =
1554 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1555 AVIVO_D1GRPH_CONTROL_32BPP_ARGB2101010;
1556#ifdef __BIG_ENDIAN
1557 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1558#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001559 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1560 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001561 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001562 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001563 DRM_ERROR("Unsupported screen format %s\n",
1564 drm_get_format_name(target_fb->pixel_format));
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001565 return -EINVAL;
1566 }
1567
Alex Deucher40c4ac12010-05-20 12:04:59 -04001568 if (rdev->family >= CHIP_R600) {
1569 if (tiling_flags & RADEON_TILING_MACRO)
1570 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1571 else if (tiling_flags & RADEON_TILING_MICRO)
1572 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1573 } else {
1574 if (tiling_flags & RADEON_TILING_MACRO)
1575 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001576
Alex Deucher40c4ac12010-05-20 12:04:59 -04001577 if (tiling_flags & RADEON_TILING_MICRO)
1578 fb_format |= AVIVO_D1GRPH_TILED;
1579 }
Dave Airliee024e112009-06-24 09:48:08 +10001580
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001581 if (radeon_crtc->crtc_id == 0)
1582 WREG32(AVIVO_D1VGA_CONTROL, 0);
1583 else
1584 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001585
Michel Dänzerc63dd752016-04-01 18:51:34 +09001586 /* Make sure surface address is update at vertical blank rather than
1587 * horizontal blank
1588 */
1589 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, 0);
1590
Alex Deucherc290dad2009-10-22 16:12:34 -04001591 if (rdev->family >= CHIP_RV770) {
1592 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001593 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1594 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001595 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001596 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1597 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001598 }
1599 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001600 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1601 (u32) fb_location);
1602 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1603 radeon_crtc->crtc_offset, (u32) fb_location);
1604 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001605 if (rdev->family >= CHIP_R600)
1606 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001607
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001608 /* LUT only has 256 slots for 8 bpc fb. Bypass for > 8 bpc scanout for precision */
1609 WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset,
1610 (bypass_lut ? AVIVO_LUT_10BIT_BYPASS_EN : 0), ~AVIVO_LUT_10BIT_BYPASS_EN);
1611
1612 if (bypass_lut)
1613 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1614
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001615 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1616 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1617 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1618 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001619 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1620 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001621
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001622 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001623 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1624 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1625
1626 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001627 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001628 x &= ~3;
1629 y &= ~1;
1630 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1631 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001632 viewport_w = crtc->mode.hdisplay;
1633 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001634 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001635 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001636
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001637 /* set pageflip to happen only at start of vblank interval (front porch) */
1638 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001639
Matt Roperf4510a22014-04-01 15:22:40 -07001640 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001641 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001642 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001643 r = radeon_bo_reserve(rbo, false);
1644 if (unlikely(r != 0))
1645 return r;
1646 radeon_bo_unpin(rbo);
1647 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001648 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001649
1650 /* Bytes per pixel may have changed */
1651 radeon_bandwidth_update(rdev);
1652
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001653 return 0;
1654}
1655
Alex Deucher54f088a2010-01-19 16:34:01 -05001656int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1657 struct drm_framebuffer *old_fb)
1658{
1659 struct drm_device *dev = crtc->dev;
1660 struct radeon_device *rdev = dev->dev_private;
1661
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001662 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001663 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001664 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001665 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001666 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001667 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1668}
1669
1670int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
Jérome Glisse3cf8bb12016-03-16 12:56:45 +01001671 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001672 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001673{
Jérome Glisse3cf8bb12016-03-16 12:56:45 +01001674 struct drm_device *dev = crtc->dev;
1675 struct radeon_device *rdev = dev->dev_private;
Chris Ball4dd19b02010-09-26 06:47:23 -05001676
1677 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001678 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001679 else if (ASIC_IS_AVIVO(rdev))
1680 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1681 else
1682 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001683}
1684
Alex Deucher615e0cb2010-01-20 16:22:53 -05001685/* properly set additional regs when using atombios */
1686static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1687{
1688 struct drm_device *dev = crtc->dev;
1689 struct radeon_device *rdev = dev->dev_private;
1690 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1691 u32 disp_merge_cntl;
1692
1693 switch (radeon_crtc->crtc_id) {
1694 case 0:
1695 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1696 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1697 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1698 break;
1699 case 1:
1700 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1701 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1702 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1703 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1704 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1705 break;
1706 }
1707}
1708
Alex Deucherf3dd8502012-08-31 11:56:50 -04001709/**
1710 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1711 *
1712 * @crtc: drm crtc
1713 *
1714 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1715 */
1716static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1717{
1718 struct drm_device *dev = crtc->dev;
1719 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001720 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001721 u32 pll_in_use = 0;
1722
1723 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1724 if (crtc == test_crtc)
1725 continue;
1726
Alex Deucher57b35e22012-09-17 17:34:45 -04001727 test_radeon_crtc = to_radeon_crtc(test_crtc);
1728 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1729 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001730 }
1731 return pll_in_use;
1732}
1733
1734/**
1735 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1736 *
1737 * @crtc: drm crtc
1738 *
1739 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1740 * also in DP mode. For DP, a single PPLL can be used for all DP
1741 * crtcs/encoders.
1742 */
1743static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1744{
1745 struct drm_device *dev = crtc->dev;
Lucas Stache3c00d82016-05-05 10:16:44 -04001746 struct radeon_device *rdev = dev->dev_private;
Alex Deucher57b35e22012-09-17 17:34:45 -04001747 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001748 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001749
Alex Deucher57b35e22012-09-17 17:34:45 -04001750 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1751 if (crtc == test_crtc)
1752 continue;
1753 test_radeon_crtc = to_radeon_crtc(test_crtc);
1754 if (test_radeon_crtc->encoder &&
1755 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
Lucas Stache3c00d82016-05-05 10:16:44 -04001756 /* PPLL2 is exclusive to UNIPHYA on DCE61 */
1757 if (ASIC_IS_DCE61(rdev) && !ASIC_IS_DCE8(rdev) &&
1758 test_radeon_crtc->pll_id == ATOM_PPLL2)
1759 continue;
Alex Deucher57b35e22012-09-17 17:34:45 -04001760 /* for DP use the same PLL for all */
1761 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1762 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001763 }
1764 }
1765 return ATOM_PPLL_INVALID;
1766}
1767
1768/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001769 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1770 *
1771 * @crtc: drm crtc
1772 * @encoder: drm encoder
1773 *
1774 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1775 * be shared (i.e., same clock).
1776 */
Alex Deucher5df31962012-09-13 11:52:08 -04001777static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001778{
Alex Deucher5df31962012-09-13 11:52:08 -04001779 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001780 struct drm_device *dev = crtc->dev;
Lucas Stache3c00d82016-05-05 10:16:44 -04001781 struct radeon_device *rdev = dev->dev_private;
Alex Deucher9642ac02012-09-13 12:43:41 -04001782 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001783 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001784 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001785
Alex Deucher9642ac02012-09-13 12:43:41 -04001786 adjusted_clock = radeon_crtc->adjusted_clock;
1787
1788 if (adjusted_clock == 0)
1789 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001790
Alex Deucher57b35e22012-09-17 17:34:45 -04001791 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1792 if (crtc == test_crtc)
1793 continue;
1794 test_radeon_crtc = to_radeon_crtc(test_crtc);
1795 if (test_radeon_crtc->encoder &&
1796 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
Lucas Stache3c00d82016-05-05 10:16:44 -04001797 /* PPLL2 is exclusive to UNIPHYA on DCE61 */
1798 if (ASIC_IS_DCE61(rdev) && !ASIC_IS_DCE8(rdev) &&
1799 test_radeon_crtc->pll_id == ATOM_PPLL2)
1800 continue;
Alex Deucher57b35e22012-09-17 17:34:45 -04001801 /* check if we are already driving this connector with another crtc */
1802 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1803 /* if we are, return that pll */
1804 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001805 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001806 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001807 /* for non-DP check the clock */
1808 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1809 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1810 (adjusted_clock == test_adjusted_clock) &&
1811 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
Alex Deucher6fb3c022015-06-10 01:29:14 -04001812 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
Alex Deucher57b35e22012-09-17 17:34:45 -04001813 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001814 }
1815 }
1816 return ATOM_PPLL_INVALID;
1817}
1818
1819/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001820 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1821 *
1822 * @crtc: drm crtc
1823 *
1824 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1825 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1826 * monitors a dedicated PPLL must be used. If a particular board has
1827 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1828 * as there is no need to program the PLL itself. If we are not able to
1829 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1830 * avoid messing up an existing monitor.
1831 *
1832 * Asic specific PLL information
1833 *
Alex Deucher0331f672012-09-14 11:57:21 -04001834 * DCE 8.x
1835 * KB/KV
1836 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1837 * CI
1838 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1839 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001840 * DCE 6.1
1841 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1842 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1843 *
1844 * DCE 6.0
1845 * - PPLL0 is available to all UNIPHY (DP only)
1846 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1847 *
1848 * DCE 5.0
1849 * - DCPLL is available to all UNIPHY (DP only)
1850 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1851 *
1852 * DCE 3.0/4.0/4.1
1853 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1854 *
1855 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001856static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1857{
Alex Deucher5df31962012-09-13 11:52:08 -04001858 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001859 struct drm_device *dev = crtc->dev;
1860 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001861 struct radeon_encoder *radeon_encoder =
1862 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001863 u32 pll_in_use;
1864 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001865
Alex Deucher0331f672012-09-14 11:57:21 -04001866 if (ASIC_IS_DCE8(rdev)) {
1867 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1868 if (rdev->clock.dp_extclk)
1869 /* skip PPLL programming if using ext clock */
1870 return ATOM_PPLL_INVALID;
1871 else {
1872 /* use the same PPLL for all DP monitors */
1873 pll = radeon_get_shared_dp_ppll(crtc);
1874 if (pll != ATOM_PPLL_INVALID)
1875 return pll;
1876 }
1877 } else {
1878 /* use the same PPLL for all monitors with the same clock */
1879 pll = radeon_get_shared_nondp_ppll(crtc);
1880 if (pll != ATOM_PPLL_INVALID)
1881 return pll;
1882 }
1883 /* otherwise, pick one of the plls */
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001884 if ((rdev->family == CHIP_KABINI) ||
Samuel Lib214f2a2014-04-30 18:40:53 -04001885 (rdev->family == CHIP_MULLINS)) {
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001886 /* KB/ML has PPLL1 and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001887 pll_in_use = radeon_get_pll_use_mask(crtc);
1888 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1889 return ATOM_PPLL2;
1890 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1891 return ATOM_PPLL1;
1892 DRM_ERROR("unable to allocate a PPLL\n");
1893 return ATOM_PPLL_INVALID;
1894 } else {
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001895 /* CI/KV has PPLL0, PPLL1, and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001896 pll_in_use = radeon_get_pll_use_mask(crtc);
1897 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1898 return ATOM_PPLL2;
1899 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1900 return ATOM_PPLL1;
1901 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1902 return ATOM_PPLL0;
1903 DRM_ERROR("unable to allocate a PPLL\n");
1904 return ATOM_PPLL_INVALID;
1905 }
1906 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001907 struct radeon_encoder_atom_dig *dig =
1908 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001909
Alex Deucher5df31962012-09-13 11:52:08 -04001910 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1911 (dig->linkb == false))
1912 /* UNIPHY A uses PPLL2 */
1913 return ATOM_PPLL2;
1914 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1915 /* UNIPHY B/C/D/E/F */
1916 if (rdev->clock.dp_extclk)
1917 /* skip PPLL programming if using ext clock */
1918 return ATOM_PPLL_INVALID;
1919 else {
1920 /* use the same PPLL for all DP monitors */
1921 pll = radeon_get_shared_dp_ppll(crtc);
1922 if (pll != ATOM_PPLL_INVALID)
1923 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001924 }
Alex Deucher5df31962012-09-13 11:52:08 -04001925 } else {
1926 /* use the same PPLL for all monitors with the same clock */
1927 pll = radeon_get_shared_nondp_ppll(crtc);
1928 if (pll != ATOM_PPLL_INVALID)
1929 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001930 }
1931 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001932 pll_in_use = radeon_get_pll_use_mask(crtc);
1933 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001934 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001935 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1936 return ATOM_PPLL1;
1937 DRM_ERROR("unable to allocate a PPLL\n");
1938 return ATOM_PPLL_INVALID;
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001939 } else if (ASIC_IS_DCE41(rdev)) {
1940 /* Don't share PLLs on DCE4.1 chips */
1941 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1942 if (rdev->clock.dp_extclk)
1943 /* skip PPLL programming if using ext clock */
1944 return ATOM_PPLL_INVALID;
1945 }
1946 pll_in_use = radeon_get_pll_use_mask(crtc);
1947 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1948 return ATOM_PPLL1;
1949 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1950 return ATOM_PPLL2;
1951 DRM_ERROR("unable to allocate a PPLL\n");
1952 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001953 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001954 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1955 * depending on the asic:
1956 * DCE4: PPLL or ext clock
1957 * DCE5: PPLL, DCPLL, or ext clock
1958 * DCE6: PPLL, PPLL0, or ext clock
1959 *
1960 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1961 * PPLL/DCPLL programming and only program the DP DTO for the
1962 * crtc virtual pixel clock.
1963 */
1964 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1965 if (rdev->clock.dp_extclk)
1966 /* skip PPLL programming if using ext clock */
1967 return ATOM_PPLL_INVALID;
1968 else if (ASIC_IS_DCE6(rdev))
1969 /* use PPLL0 for all DP */
1970 return ATOM_PPLL0;
1971 else if (ASIC_IS_DCE5(rdev))
1972 /* use DCPLL for all DP */
1973 return ATOM_DCPLL;
1974 else {
1975 /* use the same PPLL for all DP monitors */
1976 pll = radeon_get_shared_dp_ppll(crtc);
1977 if (pll != ATOM_PPLL_INVALID)
1978 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001979 }
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001980 } else {
Alex Deucher5df31962012-09-13 11:52:08 -04001981 /* use the same PPLL for all monitors with the same clock */
1982 pll = radeon_get_shared_nondp_ppll(crtc);
1983 if (pll != ATOM_PPLL_INVALID)
1984 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001985 }
1986 /* all other cases */
1987 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001988 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1989 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001990 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1991 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001992 DRM_ERROR("unable to allocate a PPLL\n");
1993 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001994 } else {
1995 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001996 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1997 * the matching btw pll and crtc is done through
1998 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1999 * pll (1 or 2) to select which register to write. ie if using
2000 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
2001 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
2002 * choose which value to write. Which is reverse order from
2003 * register logic. So only case that works is when pllid is
2004 * same as crtcid or when both pll and crtc are enabled and
2005 * both use same clock.
2006 *
2007 * So just return crtc id as if crtc and pll were hard linked
2008 * together even if they aren't
2009 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05002010 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04002011 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002012}
2013
Alex Deucherf3f1f032012-03-20 17:18:04 -04002014void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05002015{
2016 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04002017 if (ASIC_IS_DCE6(rdev))
2018 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
2019 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05002020 struct radeon_atom_ss ss;
2021 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
2022 ASIC_INTERNAL_SS_ON_DCPLL,
2023 rdev->clock.default_dispclk);
2024 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04002025 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002026 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04002027 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002028 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04002029 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002030 }
2031
2032}
2033
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002034int atombios_crtc_mode_set(struct drm_crtc *crtc,
2035 struct drm_display_mode *mode,
2036 struct drm_display_mode *adjusted_mode,
2037 int x, int y, struct drm_framebuffer *old_fb)
2038{
2039 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2040 struct drm_device *dev = crtc->dev;
2041 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04002042 struct radeon_encoder *radeon_encoder =
2043 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04002044 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002045
Alex Deucher5df31962012-09-13 11:52:08 -04002046 if (radeon_encoder->active_device &
2047 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
2048 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002049
Christian Königcde10122014-05-02 14:27:42 +02002050 if (!radeon_crtc->adjusted_clock)
2051 return -EINVAL;
2052
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002053 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002054
Alex Deucher54bfe492010-09-03 15:52:53 -04002055 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002056 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04002057 else if (ASIC_IS_AVIVO(rdev)) {
2058 if (is_tvcv)
2059 atombios_crtc_set_timing(crtc, adjusted_mode);
2060 else
2061 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
2062 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002063 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04002064 if (radeon_crtc->crtc_id == 0)
2065 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05002066 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002067 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002068 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02002069 atombios_overscan_setup(crtc, mode, adjusted_mode);
2070 atombios_scaler_setup(crtc);
Michel Dänzer6d3759f2014-11-21 11:48:57 +09002071 radeon_cursor_reset(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04002072 /* update the hw version fpr dpm */
2073 radeon_crtc->hw_mode = *adjusted_mode;
2074
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002075 return 0;
2076}
2077
2078static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02002079 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002080 struct drm_display_mode *adjusted_mode)
2081{
Alex Deucher5df31962012-09-13 11:52:08 -04002082 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2083 struct drm_device *dev = crtc->dev;
2084 struct drm_encoder *encoder;
2085
2086 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
2087 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2088 if (encoder->crtc == crtc) {
2089 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04002090 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04002091 break;
2092 }
2093 }
Alex Deucher57b35e22012-09-17 17:34:45 -04002094 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
2095 radeon_crtc->encoder = NULL;
2096 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04002097 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04002098 }
Alex Deucher643b1f52015-02-23 10:59:36 -05002099 if (radeon_crtc->encoder) {
2100 struct radeon_encoder *radeon_encoder =
2101 to_radeon_encoder(radeon_crtc->encoder);
2102
2103 radeon_crtc->output_csc = radeon_encoder->output_csc;
2104 }
Jerome Glissec93bb852009-07-13 21:04:08 +02002105 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2106 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04002107 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
2108 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04002109 /* pick pll */
2110 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
2111 /* if we can't get a PPLL for a non-DP encoder, fail */
2112 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
2113 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
2114 return false;
2115
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002116 return true;
2117}
2118
2119static void atombios_crtc_prepare(struct drm_crtc *crtc)
2120{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002121 struct drm_device *dev = crtc->dev;
2122 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05002123
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002124 /* disable crtc pair power gating before programming */
2125 if (ASIC_IS_DCE6(rdev))
2126 atombios_powergate_crtc(crtc, ATOM_DISABLE);
2127
Alex Deucher37b43902010-02-09 12:04:43 -05002128 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05002129 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002130}
2131
2132static void atombios_crtc_commit(struct drm_crtc *crtc)
2133{
2134 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05002135 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002136}
2137
Alex Deucher37f90032010-06-11 17:58:38 -04002138static void atombios_crtc_disable(struct drm_crtc *crtc)
2139{
2140 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04002141 struct drm_device *dev = crtc->dev;
2142 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002143 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04002144 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002145
Alex Deucher37f90032010-06-11 17:58:38 -04002146 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07002147 if (crtc->primary->fb) {
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002148 int r;
2149 struct radeon_framebuffer *radeon_fb;
2150 struct radeon_bo *rbo;
2151
Matt Roperf4510a22014-04-01 15:22:40 -07002152 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002153 rbo = gem_to_radeon_bo(radeon_fb->obj);
2154 r = radeon_bo_reserve(rbo, false);
2155 if (unlikely(r))
2156 DRM_ERROR("failed to reserve rbo before unpin\n");
2157 else {
2158 radeon_bo_unpin(rbo);
2159 radeon_bo_unreserve(rbo);
2160 }
2161 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04002162 /* disable the GRPH */
2163 if (ASIC_IS_DCE4(rdev))
2164 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2165 else if (ASIC_IS_AVIVO(rdev))
2166 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2167
Alex Deucher0e3d50b2013-02-05 11:47:09 -05002168 if (ASIC_IS_DCE6(rdev))
2169 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04002170
Alex Deucher4e585912012-08-21 19:06:21 -04002171 for (i = 0; i < rdev->num_crtc; i++) {
2172 if (rdev->mode_info.crtcs[i] &&
2173 rdev->mode_info.crtcs[i]->enabled &&
2174 i != radeon_crtc->crtc_id &&
2175 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
2176 /* one other crtc is using this pll don't turn
2177 * off the pll
2178 */
2179 goto done;
2180 }
2181 }
2182
Alex Deucher37f90032010-06-11 17:58:38 -04002183 switch (radeon_crtc->pll_id) {
2184 case ATOM_PPLL1:
2185 case ATOM_PPLL2:
2186 /* disable the ppll */
2187 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002188 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002189 break;
Alex Deucher64199872012-03-20 17:18:33 -04002190 case ATOM_PPLL0:
2191 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002192 if ((rdev->family == CHIP_ARUBA) ||
Alex Deucherfbedf1c2014-12-05 13:46:07 -05002193 (rdev->family == CHIP_KAVERI) ||
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002194 (rdev->family == CHIP_BONAIRE) ||
2195 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002196 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2197 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2198 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002199 default:
2200 break;
2201 }
Alex Deucher4e585912012-08-21 19:06:21 -04002202done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002203 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002204 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002205 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002206 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002207}
2208
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002209static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2210 .dpms = atombios_crtc_dpms,
2211 .mode_fixup = atombios_crtc_mode_fixup,
2212 .mode_set = atombios_crtc_mode_set,
2213 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002214 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002215 .prepare = atombios_crtc_prepare,
2216 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002217 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002218 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002219};
2220
2221void radeon_atombios_init_crtc(struct drm_device *dev,
2222 struct radeon_crtc *radeon_crtc)
2223{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002224 struct radeon_device *rdev = dev->dev_private;
2225
2226 if (ASIC_IS_DCE4(rdev)) {
2227 switch (radeon_crtc->crtc_id) {
2228 case 0:
2229 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002230 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002231 break;
2232 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002233 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002234 break;
2235 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002236 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002237 break;
2238 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002239 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002240 break;
2241 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002242 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002243 break;
2244 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002245 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002246 break;
2247 }
2248 } else {
2249 if (radeon_crtc->crtc_id == 1)
2250 radeon_crtc->crtc_offset =
2251 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2252 else
2253 radeon_crtc->crtc_offset = 0;
2254 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002255 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002256 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002257 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002258 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002259 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2260}