blob: 967d193d36d0f3912b39b326657421a5435ad4fb [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Alex Deucher5df31962012-09-13 11:52:08 -040086 struct radeon_encoder *radeon_encoder =
87 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020088 /* fixme - fill in enc_priv for atom dac */
89 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100090 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Alex Deucher5df31962012-09-13 11:52:08 -040095 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
96 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
97 tv_std = tv_dac->tv_std;
98 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 }
100
Jerome Glissec93bb852009-07-13 21:04:08 +0200101 memset(&args, 0, sizeof(args));
102
103 args.ucScaler = radeon_crtc->crtc_id;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200106 switch (tv_std) {
107 case TV_STD_NTSC:
108 default:
109 args.ucTVStandard = ATOM_TV_NTSC;
110 break;
111 case TV_STD_PAL:
112 args.ucTVStandard = ATOM_TV_PAL;
113 break;
114 case TV_STD_PAL_M:
115 args.ucTVStandard = ATOM_TV_PALM;
116 break;
117 case TV_STD_PAL_60:
118 args.ucTVStandard = ATOM_TV_PAL60;
119 break;
120 case TV_STD_NTSC_J:
121 args.ucTVStandard = ATOM_TV_NTSCJ;
122 break;
123 case TV_STD_SCART_PAL:
124 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
125 break;
126 case TV_STD_SECAM:
127 args.ucTVStandard = ATOM_TV_SECAM;
128 break;
129 case TV_STD_PAL_CN:
130 args.ucTVStandard = ATOM_TV_PALCN;
131 break;
132 }
133 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000134 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200135 args.ucTVStandard = ATOM_TV_CV;
136 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
137 } else {
138 switch (radeon_crtc->rmx_type) {
139 case RMX_FULL:
140 args.ucEnable = ATOM_SCALER_EXPANSION;
141 break;
142 case RMX_CENTER:
143 args.ucEnable = ATOM_SCALER_CENTER;
144 break;
145 case RMX_ASPECT:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 default:
149 if (ASIC_IS_AVIVO(rdev))
150 args.ucEnable = ATOM_SCALER_DISABLE;
151 else
152 args.ucEnable = ATOM_SCALER_CENTER;
153 break;
154 }
155 }
156 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000157 if ((is_tv || is_cv)
158 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
159 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200160 }
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
164{
165 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
166 struct drm_device *dev = crtc->dev;
167 struct radeon_device *rdev = dev->dev_private;
168 int index =
169 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
170 ENABLE_CRTC_PS_ALLOCATION args;
171
172 memset(&args, 0, sizeof(args));
173
174 args.ucCRTC = radeon_crtc->crtc_id;
175 args.ucEnable = lock;
176
177 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
178}
179
180static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
181{
182 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
183 struct drm_device *dev = crtc->dev;
184 struct radeon_device *rdev = dev->dev_private;
185 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = state;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
Alex Deucher78fe9e52014-01-28 23:49:37 -0500212static const u32 vga_control_regs[6] =
213{
214 AVIVO_D1VGA_CONTROL,
215 AVIVO_D2VGA_CONTROL,
216 EVERGREEN_D3VGA_CONTROL,
217 EVERGREEN_D4VGA_CONTROL,
218 EVERGREEN_D5VGA_CONTROL,
219 EVERGREEN_D6VGA_CONTROL,
220};
221
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
223{
224 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
225 struct drm_device *dev = crtc->dev;
226 struct radeon_device *rdev = dev->dev_private;
227 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
228 BLANK_CRTC_PS_ALLOCATION args;
Alex Deucher78fe9e52014-01-28 23:49:37 -0500229 u32 vga_control = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230
231 memset(&args, 0, sizeof(args));
232
Alex Deucher78fe9e52014-01-28 23:49:37 -0500233 if (ASIC_IS_DCE8(rdev)) {
234 vga_control = RREG32(vga_control_regs[radeon_crtc->crtc_id]);
235 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control | 1);
236 }
237
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238 args.ucCRTC = radeon_crtc->crtc_id;
239 args.ucBlanking = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucher78fe9e52014-01-28 23:49:37 -0500242
243 if (ASIC_IS_DCE8(rdev)) {
244 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control);
245 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200246}
247
Alex Deucherfef9f912012-03-20 17:18:03 -0400248static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
249{
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251 struct drm_device *dev = crtc->dev;
252 struct radeon_device *rdev = dev->dev_private;
253 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
254 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
255
256 memset(&args, 0, sizeof(args));
257
258 args.ucDispPipeId = radeon_crtc->crtc_id;
259 args.ucEnable = state;
260
261 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
262}
263
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
265{
266 struct drm_device *dev = crtc->dev;
267 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500268 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200269
270 switch (mode) {
271 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400272 radeon_crtc->enabled = true;
273 /* adjust pm to dpms changes BEFORE enabling crtcs */
274 radeon_pm_compute_clocks(rdev);
Alex Deucher37b43902010-02-09 12:04:43 -0500275 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400276 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500277 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
278 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400279 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
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:
Alex Deucher45f9a392010-03-24 13:55:51 -0400285 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500286 if (radeon_crtc->enabled)
287 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400288 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500289 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
290 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400291 radeon_crtc->enabled = false;
Alex Deucherd7311172010-05-03 01:13:14 -0400292 /* adjust pm to dpms changes AFTER disabling crtcs */
293 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294 break;
295 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296}
297
298static void
299atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400300 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400302 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200303 struct drm_device *dev = crtc->dev;
304 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400305 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200306 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400307 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200308
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400309 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400310 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400311 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400312 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
313 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400314 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400315 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400317 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400318 args.usH_SyncWidth =
319 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
320 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400321 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400322 args.usV_SyncWidth =
323 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400324 args.ucH_Border = radeon_crtc->h_border;
325 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400326
327 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
328 misc |= ATOM_VSYNC_POLARITY;
329 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
330 misc |= ATOM_HSYNC_POLARITY;
331 if (mode->flags & DRM_MODE_FLAG_CSYNC)
332 misc |= ATOM_COMPOSITESYNC;
333 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
334 misc |= ATOM_INTERLACE;
335 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
336 misc |= ATOM_DOUBLE_CLOCK_MODE;
337
338 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
339 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200340
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400341 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342}
343
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400344static void atombios_crtc_set_timing(struct drm_crtc *crtc,
345 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400347 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200348 struct drm_device *dev = crtc->dev;
349 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400350 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400352 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200353
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400354 memset(&args, 0, sizeof(args));
355 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
356 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
357 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
358 args.usH_SyncWidth =
359 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
360 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
361 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
362 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
363 args.usV_SyncWidth =
364 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
365
Alex Deucher54bfe492010-09-03 15:52:53 -0400366 args.ucOverscanRight = radeon_crtc->h_border;
367 args.ucOverscanLeft = radeon_crtc->h_border;
368 args.ucOverscanBottom = radeon_crtc->v_border;
369 args.ucOverscanTop = radeon_crtc->v_border;
370
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400371 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
372 misc |= ATOM_VSYNC_POLARITY;
373 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
374 misc |= ATOM_HSYNC_POLARITY;
375 if (mode->flags & DRM_MODE_FLAG_CSYNC)
376 misc |= ATOM_COMPOSITESYNC;
377 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
378 misc |= ATOM_INTERLACE;
379 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
380 misc |= ATOM_DOUBLE_CLOCK_MODE;
381
382 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
383 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400385 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386}
387
Alex Deucher3fa47d92012-01-20 14:56:39 -0500388static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500389{
Alex Deucherb7922102010-03-06 10:57:30 -0500390 u32 ss_cntl;
391
392 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500393 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500394 case ATOM_PPLL1:
395 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
396 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
397 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
398 break;
399 case ATOM_PPLL2:
400 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
401 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
402 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
403 break;
404 case ATOM_DCPLL:
405 case ATOM_PPLL_INVALID:
406 return;
407 }
408 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500409 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500410 case ATOM_PPLL1:
411 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
412 ss_cntl &= ~1;
413 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
414 break;
415 case ATOM_PPLL2:
416 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
417 ss_cntl &= ~1;
418 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
419 break;
420 case ATOM_DCPLL:
421 case ATOM_PPLL_INVALID:
422 return;
423 }
424 }
425}
426
427
Alex Deucher26b9fc32010-02-01 16:39:11 -0500428union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400429 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
430 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500431 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400432 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500433 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500434};
435
Alex Deucher3fa47d92012-01-20 14:56:39 -0500436static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400437 int enable,
438 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400439 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400440 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400441{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400442 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400443 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500444 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400445
Alex Deucherc4756ba2014-01-15 13:59:47 -0500446 if (enable) {
447 /* Don't mess with SS if percentage is 0 or external ss.
448 * SS is already disabled previously, and disabling it
449 * again can cause display problems if the pll is already
450 * programmed.
451 */
452 if (ss->percentage == 0)
453 return;
454 if (ss->type & ATOM_EXTERNAL_SS_MASK)
455 return;
456 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400457 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400458 if (rdev->mode_info.crtcs[i] &&
459 rdev->mode_info.crtcs[i]->enabled &&
460 i != crtc_id &&
461 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
462 /* one other crtc is using this pll don't turn
463 * off spread spectrum as it might turn off
464 * display on active crtc
465 */
466 return;
467 }
468 }
469 }
470
Alex Deucher26b9fc32010-02-01 16:39:11 -0500471 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400472
Alex Deuchera572eaa2011-01-06 21:19:16 -0500473 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500474 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400475 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500476 switch (pll_id) {
477 case ATOM_PPLL1:
478 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500479 break;
480 case ATOM_PPLL2:
481 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500482 break;
483 case ATOM_DCPLL:
484 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500485 break;
486 case ATOM_PPLL_INVALID:
487 return;
488 }
Alex Deucherf312f092012-07-17 14:02:44 -0400489 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
490 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400491 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500492 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400493 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400494 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400495 switch (pll_id) {
496 case ATOM_PPLL1:
497 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400498 break;
499 case ATOM_PPLL2:
500 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400501 break;
502 case ATOM_DCPLL:
503 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400504 break;
505 case ATOM_PPLL_INVALID:
506 return;
507 }
Alex Deucherf312f092012-07-17 14:02:44 -0400508 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
509 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400510 args.v2.ucEnable = enable;
511 } else if (ASIC_IS_DCE3(rdev)) {
512 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400513 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400514 args.v1.ucSpreadSpectrumStep = ss->step;
515 args.v1.ucSpreadSpectrumDelay = ss->delay;
516 args.v1.ucSpreadSpectrumRange = ss->range;
517 args.v1.ucPpll = pll_id;
518 args.v1.ucEnable = enable;
519 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400520 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
521 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500522 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400523 return;
524 }
525 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400526 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400527 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
528 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
529 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
530 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400531 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500532 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500533 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400534 return;
535 }
536 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400537 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400538 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
539 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
540 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400541 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500542 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400543}
544
Alex Deucher4eaeca32010-01-19 17:32:27 -0500545union adjust_pixel_clock {
546 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500547 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500548};
549
550static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400551 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552{
Alex Deucher19eca432012-09-13 10:56:16 -0400553 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200554 struct drm_device *dev = crtc->dev;
555 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400556 struct drm_encoder *encoder = radeon_crtc->encoder;
557 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
558 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500559 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400560 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400561 u32 dp_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400562 u32 clock = mode->clock;
Alex Deucher7d5a33b2014-02-03 15:53:25 -0500563 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400564 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500565
Alex Deucher4eaeca32010-01-19 17:32:27 -0500566 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400567 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200568
569 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400570 if ((rdev->family == CHIP_RS600) ||
571 (rdev->family == CHIP_RS690) ||
572 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400573 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
574 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000575
576 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400577 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000578 else
Alex Deucher19eca432012-09-13 10:56:16 -0400579 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400580
Alex Deucher5785e532011-04-19 15:24:59 -0400581 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400582 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400583 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500584 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400585 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400586 /* use frac fb div on RS780/RS880 */
587 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
588 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500589 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
590 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000591 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400592 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200593
Dave Airlie5480f722010-10-19 10:36:47 +1000594 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400595 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000596 else
Alex Deucher19eca432012-09-13 10:56:16 -0400597 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000598 }
599
Alex Deucher5df31962012-09-13 11:52:08 -0400600 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
601 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
602 if (connector) {
603 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
604 struct radeon_connector_atom_dig *dig_connector =
605 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400606
Alex Deucher5df31962012-09-13 11:52:08 -0400607 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200608 }
609 }
610
Alex Deucher5df31962012-09-13 11:52:08 -0400611 /* use recommended ref_div for ss */
612 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
613 if (radeon_crtc->ss_enabled) {
614 if (radeon_crtc->ss.refdiv) {
615 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
616 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
617 if (ASIC_IS_AVIVO(rdev))
618 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
619 }
620 }
621 }
622
623 if (ASIC_IS_AVIVO(rdev)) {
624 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
625 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
626 adjusted_clock = mode->clock * 2;
627 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
628 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
629 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
630 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
631 } else {
632 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
633 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
634 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
635 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
636 }
637
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400638 /* adjust pll for deep color modes */
639 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
640 switch (bpc) {
641 case 8:
642 default:
643 break;
644 case 10:
645 clock = (clock * 5) / 4;
646 break;
647 case 12:
648 clock = (clock * 3) / 2;
649 break;
650 case 16:
651 clock = clock * 2;
652 break;
653 }
654 }
655
Alex Deucher2606c882009-10-08 13:36:21 -0400656 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
657 * accordingly based on the encoder/transmitter to work around
658 * special hw requirements.
659 */
660 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500661 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500662 u8 frev, crev;
663 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400664
Alex Deucher2606c882009-10-08 13:36:21 -0400665 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400666 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
667 &crev))
668 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500669
670 memset(&args, 0, sizeof(args));
671
672 switch (frev) {
673 case 1:
674 switch (crev) {
675 case 1:
676 case 2:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400677 args.v1.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500678 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500679 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400680 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400681 args.v1.ucConfig |=
682 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500683
684 atom_execute_table(rdev->mode_info.atom_context,
685 index, (uint32_t *)&args);
686 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
687 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500688 case 3:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400689 args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500690 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
691 args.v3.sInput.ucEncodeMode = encoder_mode;
692 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400693 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000694 args.v3.sInput.ucDispPllConfig |=
695 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400696 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400697 args.v3.sInput.ucDispPllConfig |=
698 DISPPLL_CONFIG_COHERENT_MODE;
699 /* 16200 or 27000 */
700 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
701 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500702 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400703 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500704 args.v3.sInput.ucDispPllConfig |=
705 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500706 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500707 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400708 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500709 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400710 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
711 ENCODER_OBJECT_ID_NONE)
712 args.v3.sInput.ucExtTransmitterID =
713 radeon_encoder_get_dp_bridge_encoder_id(encoder);
714 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400715 args.v3.sInput.ucExtTransmitterID = 0;
716
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500717 atom_execute_table(rdev->mode_info.atom_context,
718 index, (uint32_t *)&args);
719 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
720 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400721 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
722 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
723 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500724 }
725 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400726 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
727 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
728 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500729 }
730 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500731 default:
732 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
733 return adjusted_clock;
734 }
735 break;
736 default:
737 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
738 return adjusted_clock;
739 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400740 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500741 return adjusted_clock;
742}
743
744union set_pixel_clock {
745 SET_PIXEL_CLOCK_PS_ALLOCATION base;
746 PIXEL_CLOCK_PARAMETERS v1;
747 PIXEL_CLOCK_PARAMETERS_V2 v2;
748 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500749 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500750 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500751};
752
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500753/* on DCE5, make sure the voltage is high enough to support the
754 * required disp clk.
755 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400756static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500757 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500758{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500759 u8 frev, crev;
760 int index;
761 union set_pixel_clock args;
762
763 memset(&args, 0, sizeof(args));
764
765 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400766 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
767 &crev))
768 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500769
770 switch (frev) {
771 case 1:
772 switch (crev) {
773 case 5:
774 /* if the default dcpll clock is specified,
775 * SetPixelClock provides the dividers
776 */
777 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500778 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500779 args.v5.ucPpll = ATOM_DCPLL;
780 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500781 case 6:
782 /* if the default dcpll clock is specified,
783 * SetPixelClock provides the dividers
784 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500785 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400786 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400787 args.v6.ucPpll = ATOM_EXT_PLL1;
788 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400789 args.v6.ucPpll = ATOM_PPLL0;
790 else
791 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500792 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500793 default:
794 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
795 return;
796 }
797 break;
798 default:
799 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
800 return;
801 }
802 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
803}
804
Alex Deucher37f90032010-06-11 17:58:38 -0400805static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000806 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400807 int pll_id,
808 u32 encoder_mode,
809 u32 encoder_id,
810 u32 clock,
811 u32 ref_div,
812 u32 fb_div,
813 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400814 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400815 int bpc,
816 bool ss_enabled,
817 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400818{
819 struct drm_device *dev = crtc->dev;
820 struct radeon_device *rdev = dev->dev_private;
821 u8 frev, crev;
822 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
823 union set_pixel_clock args;
824
825 memset(&args, 0, sizeof(args));
826
827 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
828 &crev))
829 return;
830
831 switch (frev) {
832 case 1:
833 switch (crev) {
834 case 1:
835 if (clock == ATOM_DISABLE)
836 return;
837 args.v1.usPixelClock = cpu_to_le16(clock / 10);
838 args.v1.usRefDiv = cpu_to_le16(ref_div);
839 args.v1.usFbDiv = cpu_to_le16(fb_div);
840 args.v1.ucFracFbDiv = frac_fb_div;
841 args.v1.ucPostDiv = post_div;
842 args.v1.ucPpll = pll_id;
843 args.v1.ucCRTC = crtc_id;
844 args.v1.ucRefDivSrc = 1;
845 break;
846 case 2:
847 args.v2.usPixelClock = cpu_to_le16(clock / 10);
848 args.v2.usRefDiv = cpu_to_le16(ref_div);
849 args.v2.usFbDiv = cpu_to_le16(fb_div);
850 args.v2.ucFracFbDiv = frac_fb_div;
851 args.v2.ucPostDiv = post_div;
852 args.v2.ucPpll = pll_id;
853 args.v2.ucCRTC = crtc_id;
854 args.v2.ucRefDivSrc = 1;
855 break;
856 case 3:
857 args.v3.usPixelClock = cpu_to_le16(clock / 10);
858 args.v3.usRefDiv = cpu_to_le16(ref_div);
859 args.v3.usFbDiv = cpu_to_le16(fb_div);
860 args.v3.ucFracFbDiv = frac_fb_div;
861 args.v3.ucPostDiv = post_div;
862 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400863 if (crtc_id == ATOM_CRTC2)
864 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
865 else
866 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400867 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
868 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400869 args.v3.ucTransmitterId = encoder_id;
870 args.v3.ucEncoderMode = encoder_mode;
871 break;
872 case 5:
873 args.v5.ucCRTC = crtc_id;
874 args.v5.usPixelClock = cpu_to_le16(clock / 10);
875 args.v5.ucRefDiv = ref_div;
876 args.v5.usFbDiv = cpu_to_le16(fb_div);
877 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
878 args.v5.ucPostDiv = post_div;
879 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400880 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
881 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400882 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
883 switch (bpc) {
884 case 8:
885 default:
886 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
887 break;
888 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400889 /* yes this is correct, the atom define is wrong */
890 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
891 break;
892 case 12:
893 /* yes this is correct, the atom define is wrong */
Alex Deucher7d5ab302014-04-21 21:45:09 -0400894 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
895 break;
896 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400897 }
Alex Deucher37f90032010-06-11 17:58:38 -0400898 args.v5.ucTransmitterID = encoder_id;
899 args.v5.ucEncoderMode = encoder_mode;
900 args.v5.ucPpll = pll_id;
901 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500902 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000903 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500904 args.v6.ucRefDiv = ref_div;
905 args.v6.usFbDiv = cpu_to_le16(fb_div);
906 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
907 args.v6.ucPostDiv = post_div;
908 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400909 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
910 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400911 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
912 switch (bpc) {
913 case 8:
914 default:
915 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
916 break;
917 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400918 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400919 break;
920 case 12:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400921 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400922 break;
923 case 16:
924 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
925 break;
926 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400927 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500928 args.v6.ucTransmitterID = encoder_id;
929 args.v6.ucEncoderMode = encoder_mode;
930 args.v6.ucPpll = pll_id;
931 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400932 default:
933 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
934 return;
935 }
936 break;
937 default:
938 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
939 return;
940 }
941
942 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
943}
944
Alex Deucher19eca432012-09-13 10:56:16 -0400945static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
946{
947 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
948 struct drm_device *dev = crtc->dev;
949 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400950 struct radeon_encoder *radeon_encoder =
951 to_radeon_encoder(radeon_crtc->encoder);
952 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400953
954 radeon_crtc->bpc = 8;
955 radeon_crtc->ss_enabled = false;
956
Alex Deucher19eca432012-09-13 10:56:16 -0400957 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400958 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400959 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
960 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400961 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400962 struct radeon_connector *radeon_connector =
963 to_radeon_connector(connector);
964 struct radeon_connector_atom_dig *dig_connector =
965 radeon_connector->con_priv;
966 int dp_clock;
967 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
968
969 switch (encoder_mode) {
970 case ATOM_ENCODER_MODE_DP_MST:
971 case ATOM_ENCODER_MODE_DP:
972 /* DP/eDP */
973 dp_clock = dig_connector->dp_clock / 10;
974 if (ASIC_IS_DCE4(rdev))
975 radeon_crtc->ss_enabled =
976 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
977 ASIC_INTERNAL_SS_ON_DP,
978 dp_clock);
979 else {
980 if (dp_clock == 16200) {
981 radeon_crtc->ss_enabled =
982 radeon_atombios_get_ppll_ss_info(rdev,
983 &radeon_crtc->ss,
984 ATOM_DP_SS_ID2);
985 if (!radeon_crtc->ss_enabled)
986 radeon_crtc->ss_enabled =
987 radeon_atombios_get_ppll_ss_info(rdev,
988 &radeon_crtc->ss,
989 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500990 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400991 radeon_crtc->ss_enabled =
992 radeon_atombios_get_ppll_ss_info(rdev,
993 &radeon_crtc->ss,
994 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500995 }
996 /* disable spread spectrum on DCE3 DP */
997 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -0400998 }
999 break;
1000 case ATOM_ENCODER_MODE_LVDS:
1001 if (ASIC_IS_DCE4(rdev))
1002 radeon_crtc->ss_enabled =
1003 radeon_atombios_get_asic_ss_info(rdev,
1004 &radeon_crtc->ss,
1005 dig->lcd_ss_id,
1006 mode->clock / 10);
1007 else
1008 radeon_crtc->ss_enabled =
1009 radeon_atombios_get_ppll_ss_info(rdev,
1010 &radeon_crtc->ss,
1011 dig->lcd_ss_id);
1012 break;
1013 case ATOM_ENCODER_MODE_DVI:
1014 if (ASIC_IS_DCE4(rdev))
1015 radeon_crtc->ss_enabled =
1016 radeon_atombios_get_asic_ss_info(rdev,
1017 &radeon_crtc->ss,
1018 ASIC_INTERNAL_SS_ON_TMDS,
1019 mode->clock / 10);
1020 break;
1021 case ATOM_ENCODER_MODE_HDMI:
1022 if (ASIC_IS_DCE4(rdev))
1023 radeon_crtc->ss_enabled =
1024 radeon_atombios_get_asic_ss_info(rdev,
1025 &radeon_crtc->ss,
1026 ASIC_INTERNAL_SS_ON_HDMI,
1027 mode->clock / 10);
1028 break;
1029 default:
1030 break;
1031 }
1032 }
1033
1034 /* adjust pixel clock as needed */
1035 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1036
1037 return true;
1038}
1039
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001040static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001041{
1042 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1043 struct drm_device *dev = crtc->dev;
1044 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001045 struct radeon_encoder *radeon_encoder =
1046 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001047 u32 pll_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001048 u32 clock = mode->clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001049 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1050 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001051 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001052
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001053 /* pass the actual clock to atombios_crtc_program_pll for DCE5,6 for HDMI */
1054 if (ASIC_IS_DCE5(rdev) && !ASIC_IS_DCE8(rdev) &&
1055 (encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
1056 (radeon_crtc->bpc > 8))
1057 clock = radeon_crtc->adjusted_clock;
1058
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001059 switch (radeon_crtc->pll_id) {
1060 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001061 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001062 break;
1063 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001064 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001065 break;
1066 case ATOM_DCPLL:
1067 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001068 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001069 pll = &rdev->clock.dcpll;
1070 break;
1071 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001072
Alex Deucher19eca432012-09-13 10:56:16 -04001073 /* update pll params */
1074 pll->flags = radeon_crtc->pll_flags;
1075 pll->reference_div = radeon_crtc->pll_reference_div;
1076 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001077
Alex Deucher64146f82011-03-22 01:46:12 -04001078 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1079 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001080 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1081 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001082 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001083 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1084 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001085 else
Alex Deucher19eca432012-09-13 10:56:16 -04001086 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1087 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001088
Alex Deucher19eca432012-09-13 10:56:16 -04001089 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1090 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001091
Alex Deucher37f90032010-06-11 17:58:38 -04001092 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001093 encoder_mode, radeon_encoder->encoder_id, clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001094 ref_div, fb_div, frac_fb_div, post_div,
1095 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001096
Alex Deucher19eca432012-09-13 10:56:16 -04001097 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001098 /* calculate ss amount and step size */
1099 if (ASIC_IS_DCE4(rdev)) {
1100 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001101 u32 amount = (((fb_div * 10) + frac_fb_div) *
1102 (u32)radeon_crtc->ss.percentage) /
1103 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001104 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1105 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001106 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001107 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001108 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001109 (125 * 25 * pll->reference_freq / 100);
1110 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001111 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001112 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001113 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001114 }
1115
Alex Deucher19eca432012-09-13 10:56:16 -04001116 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1117 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001118 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001119}
1120
Alex Deucherc9417bd2011-02-06 14:23:26 -05001121static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1122 struct drm_framebuffer *fb,
1123 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001124{
1125 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1126 struct drm_device *dev = crtc->dev;
1127 struct radeon_device *rdev = dev->dev_private;
1128 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001129 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001130 struct drm_gem_object *obj;
1131 struct radeon_bo *rbo;
1132 uint64_t fb_location;
1133 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001134 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001135 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001136 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001137 int r;
1138
1139 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001140 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001141 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001142 return 0;
1143 }
1144
Chris Ball4dd19b02010-09-26 06:47:23 -05001145 if (atomic) {
1146 radeon_fb = to_radeon_framebuffer(fb);
1147 target_fb = fb;
1148 }
1149 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001150 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1151 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001152 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001153
Chris Ball4dd19b02010-09-26 06:47:23 -05001154 /* If atomic, assume fb object is pinned & idle & fenced and
1155 * just update base pointers
1156 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001157 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001158 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001159 r = radeon_bo_reserve(rbo, false);
1160 if (unlikely(r != 0))
1161 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001162
1163 if (atomic)
1164 fb_location = radeon_bo_gpu_offset(rbo);
1165 else {
1166 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1167 if (unlikely(r != 0)) {
1168 radeon_bo_unreserve(rbo);
1169 return -EINVAL;
1170 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001171 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001172
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001173 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1174 radeon_bo_unreserve(rbo);
1175
Chris Ball4dd19b02010-09-26 06:47:23 -05001176 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001177 case 8:
1178 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1179 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1180 break;
1181 case 15:
1182 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1183 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1184 break;
1185 case 16:
1186 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1187 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001188#ifdef __BIG_ENDIAN
1189 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1190#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001191 break;
1192 case 24:
1193 case 32:
1194 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1195 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001196#ifdef __BIG_ENDIAN
1197 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1198#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001199 break;
1200 default:
1201 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001202 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001203 return -EINVAL;
1204 }
1205
Alex Deucher392e3722011-11-28 14:49:27 -05001206 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001207 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001208
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001209 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001210 if (rdev->family >= CHIP_TAHITI) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001211 unsigned tileb, index, num_banks, tile_split_bytes;
1212
1213 /* Calculate the macrotile mode index. */
1214 tile_split_bytes = 64 << tile_split;
1215 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1216 tileb = min(tile_split_bytes, tileb);
1217
1218 for (index = 0; tileb > 64; index++) {
1219 tileb >>= 1;
1220 }
1221
1222 if (index >= 16) {
1223 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1224 target_fb->bits_per_pixel, tile_split);
1225 return -EINVAL;
1226 }
1227
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001228 if (rdev->family >= CHIP_BONAIRE)
1229 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
1230 else
1231 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001232 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1233 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001234 /* NI and older. */
1235 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001236 tmp = rdev->config.cayman.tile_config;
1237 else
1238 tmp = rdev->config.evergreen.tile_config;
1239
1240 switch ((tmp & 0xf0) >> 4) {
1241 case 0: /* 4 banks */
1242 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1243 break;
1244 case 1: /* 8 banks */
1245 default:
1246 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1247 break;
1248 case 2: /* 16 banks */
1249 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1250 break;
1251 }
Alex Deucher392e3722011-11-28 14:49:27 -05001252 }
1253
Alex Deucher97d66322010-05-20 12:12:48 -04001254 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001255 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1256 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1257 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1258 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001259 if (rdev->family >= CHIP_BONAIRE) {
1260 /* XXX need to know more about the surface tiling mode */
1261 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1262 }
Alex Deucher392e3722011-11-28 14:49:27 -05001263 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001264 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1265
Alex Deucher8da0e502012-07-11 18:38:29 -04001266 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001267 /* Read the pipe config from the 2D TILED SCANOUT mode.
1268 * It should be the same for the other modes too, but not all
1269 * modes set the pipe config field. */
1270 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1271
1272 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001273 } else if ((rdev->family == CHIP_TAHITI) ||
1274 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001275 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001276 else if ((rdev->family == CHIP_VERDE) ||
1277 (rdev->family == CHIP_OLAND) ||
1278 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001279 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1280
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001281 switch (radeon_crtc->crtc_id) {
1282 case 0:
1283 WREG32(AVIVO_D1VGA_CONTROL, 0);
1284 break;
1285 case 1:
1286 WREG32(AVIVO_D2VGA_CONTROL, 0);
1287 break;
1288 case 2:
1289 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1290 break;
1291 case 3:
1292 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1293 break;
1294 case 4:
1295 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1296 break;
1297 case 5:
1298 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1299 break;
1300 default:
1301 break;
1302 }
1303
1304 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1305 upper_32_bits(fb_location));
1306 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1307 upper_32_bits(fb_location));
1308 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1309 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1310 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1311 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1312 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001313 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001314
1315 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1316 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1317 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1318 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001319 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1320 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001321
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001322 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001323 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1324 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1325
Alex Deucher8da0e502012-07-11 18:38:29 -04001326 if (rdev->family >= CHIP_BONAIRE)
1327 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1328 target_fb->height);
1329 else
1330 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1331 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001332 x &= ~3;
1333 y &= ~1;
1334 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1335 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001336 viewport_w = crtc->mode.hdisplay;
1337 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001338 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001339 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001340
Alex Deucherfb9674b2011-04-02 09:15:50 -04001341 /* pageflip setup */
1342 /* make sure flip is at vb rather than hb */
1343 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1344 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1345 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1346
1347 /* set pageflip to happen anywhere in vblank interval */
1348 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1349
Matt Roperf4510a22014-04-01 15:22:40 -07001350 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001351 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001352 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001353 r = radeon_bo_reserve(rbo, false);
1354 if (unlikely(r != 0))
1355 return r;
1356 radeon_bo_unpin(rbo);
1357 radeon_bo_unreserve(rbo);
1358 }
1359
1360 /* Bytes per pixel may have changed */
1361 radeon_bandwidth_update(rdev);
1362
1363 return 0;
1364}
1365
Chris Ball4dd19b02010-09-26 06:47:23 -05001366static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1367 struct drm_framebuffer *fb,
1368 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001369{
1370 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1371 struct drm_device *dev = crtc->dev;
1372 struct radeon_device *rdev = dev->dev_private;
1373 struct radeon_framebuffer *radeon_fb;
1374 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001375 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001376 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001377 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001378 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001379 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001380 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001381 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001382
Jerome Glisse2de3b482009-11-17 14:08:55 -08001383 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001384 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001385 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001386 return 0;
1387 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001388
Chris Ball4dd19b02010-09-26 06:47:23 -05001389 if (atomic) {
1390 radeon_fb = to_radeon_framebuffer(fb);
1391 target_fb = fb;
1392 }
1393 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001394 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1395 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001396 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001397
1398 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001399 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001400 r = radeon_bo_reserve(rbo, false);
1401 if (unlikely(r != 0))
1402 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001403
1404 /* If atomic, assume fb object is pinned & idle & fenced and
1405 * just update base pointers
1406 */
1407 if (atomic)
1408 fb_location = radeon_bo_gpu_offset(rbo);
1409 else {
1410 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1411 if (unlikely(r != 0)) {
1412 radeon_bo_unreserve(rbo);
1413 return -EINVAL;
1414 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001415 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001416 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1417 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001418
Chris Ball4dd19b02010-09-26 06:47:23 -05001419 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001420 case 8:
1421 fb_format =
1422 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1423 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1424 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001425 case 15:
1426 fb_format =
1427 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1428 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1429 break;
1430 case 16:
1431 fb_format =
1432 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1433 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001434#ifdef __BIG_ENDIAN
1435 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1436#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001437 break;
1438 case 24:
1439 case 32:
1440 fb_format =
1441 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1442 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001443#ifdef __BIG_ENDIAN
1444 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1445#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001446 break;
1447 default:
1448 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001449 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001450 return -EINVAL;
1451 }
1452
Alex Deucher40c4ac12010-05-20 12:04:59 -04001453 if (rdev->family >= CHIP_R600) {
1454 if (tiling_flags & RADEON_TILING_MACRO)
1455 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1456 else if (tiling_flags & RADEON_TILING_MICRO)
1457 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1458 } else {
1459 if (tiling_flags & RADEON_TILING_MACRO)
1460 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001461
Alex Deucher40c4ac12010-05-20 12:04:59 -04001462 if (tiling_flags & RADEON_TILING_MICRO)
1463 fb_format |= AVIVO_D1GRPH_TILED;
1464 }
Dave Airliee024e112009-06-24 09:48:08 +10001465
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001466 if (radeon_crtc->crtc_id == 0)
1467 WREG32(AVIVO_D1VGA_CONTROL, 0);
1468 else
1469 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001470
1471 if (rdev->family >= CHIP_RV770) {
1472 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001473 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1474 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001475 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001476 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1477 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001478 }
1479 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001480 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1481 (u32) fb_location);
1482 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1483 radeon_crtc->crtc_offset, (u32) fb_location);
1484 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001485 if (rdev->family >= CHIP_R600)
1486 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001487
1488 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1489 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1490 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1491 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001492 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1493 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001494
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001495 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001496 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1497 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1498
1499 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001500 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001501 x &= ~3;
1502 y &= ~1;
1503 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1504 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001505 viewport_w = crtc->mode.hdisplay;
1506 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001507 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001508 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001509
Alex Deucherfb9674b2011-04-02 09:15:50 -04001510 /* pageflip setup */
1511 /* make sure flip is at vb rather than hb */
1512 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1513 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1514 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1515
1516 /* set pageflip to happen anywhere in vblank interval */
1517 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1518
Matt Roperf4510a22014-04-01 15:22:40 -07001519 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001520 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001521 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001522 r = radeon_bo_reserve(rbo, false);
1523 if (unlikely(r != 0))
1524 return r;
1525 radeon_bo_unpin(rbo);
1526 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001527 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001528
1529 /* Bytes per pixel may have changed */
1530 radeon_bandwidth_update(rdev);
1531
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001532 return 0;
1533}
1534
Alex Deucher54f088a2010-01-19 16:34:01 -05001535int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1536 struct drm_framebuffer *old_fb)
1537{
1538 struct drm_device *dev = crtc->dev;
1539 struct radeon_device *rdev = dev->dev_private;
1540
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001541 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001542 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001543 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001544 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001545 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001546 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1547}
1548
1549int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1550 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001551 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001552{
1553 struct drm_device *dev = crtc->dev;
1554 struct radeon_device *rdev = dev->dev_private;
1555
1556 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001557 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001558 else if (ASIC_IS_AVIVO(rdev))
1559 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1560 else
1561 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001562}
1563
Alex Deucher615e0cb2010-01-20 16:22:53 -05001564/* properly set additional regs when using atombios */
1565static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1566{
1567 struct drm_device *dev = crtc->dev;
1568 struct radeon_device *rdev = dev->dev_private;
1569 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1570 u32 disp_merge_cntl;
1571
1572 switch (radeon_crtc->crtc_id) {
1573 case 0:
1574 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1575 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1576 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1577 break;
1578 case 1:
1579 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1580 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1581 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1582 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1583 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1584 break;
1585 }
1586}
1587
Alex Deucherf3dd8502012-08-31 11:56:50 -04001588/**
1589 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1590 *
1591 * @crtc: drm crtc
1592 *
1593 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1594 */
1595static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1596{
1597 struct drm_device *dev = crtc->dev;
1598 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001599 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001600 u32 pll_in_use = 0;
1601
1602 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1603 if (crtc == test_crtc)
1604 continue;
1605
Alex Deucher57b35e22012-09-17 17:34:45 -04001606 test_radeon_crtc = to_radeon_crtc(test_crtc);
1607 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1608 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001609 }
1610 return pll_in_use;
1611}
1612
1613/**
1614 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1615 *
1616 * @crtc: drm crtc
1617 *
1618 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1619 * also in DP mode. For DP, a single PPLL can be used for all DP
1620 * crtcs/encoders.
1621 */
1622static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1623{
1624 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001625 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001626 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001627
Alex Deucher57b35e22012-09-17 17:34:45 -04001628 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1629 if (crtc == test_crtc)
1630 continue;
1631 test_radeon_crtc = to_radeon_crtc(test_crtc);
1632 if (test_radeon_crtc->encoder &&
1633 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1634 /* for DP use the same PLL for all */
1635 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1636 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001637 }
1638 }
1639 return ATOM_PPLL_INVALID;
1640}
1641
1642/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001643 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1644 *
1645 * @crtc: drm crtc
1646 * @encoder: drm encoder
1647 *
1648 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1649 * be shared (i.e., same clock).
1650 */
Alex Deucher5df31962012-09-13 11:52:08 -04001651static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001652{
Alex Deucher5df31962012-09-13 11:52:08 -04001653 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001654 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001655 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001656 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001657 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001658
Alex Deucher9642ac02012-09-13 12:43:41 -04001659 adjusted_clock = radeon_crtc->adjusted_clock;
1660
1661 if (adjusted_clock == 0)
1662 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001663
Alex Deucher57b35e22012-09-17 17:34:45 -04001664 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1665 if (crtc == test_crtc)
1666 continue;
1667 test_radeon_crtc = to_radeon_crtc(test_crtc);
1668 if (test_radeon_crtc->encoder &&
1669 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1670 /* check if we are already driving this connector with another crtc */
1671 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1672 /* if we are, return that pll */
1673 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001674 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001675 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001676 /* for non-DP check the clock */
1677 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1678 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1679 (adjusted_clock == test_adjusted_clock) &&
1680 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1681 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1682 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001683 }
1684 }
1685 return ATOM_PPLL_INVALID;
1686}
1687
1688/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001689 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1690 *
1691 * @crtc: drm crtc
1692 *
1693 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1694 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1695 * monitors a dedicated PPLL must be used. If a particular board has
1696 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1697 * as there is no need to program the PLL itself. If we are not able to
1698 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1699 * avoid messing up an existing monitor.
1700 *
1701 * Asic specific PLL information
1702 *
Alex Deucher0331f672012-09-14 11:57:21 -04001703 * DCE 8.x
1704 * KB/KV
1705 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1706 * CI
1707 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1708 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001709 * DCE 6.1
1710 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1711 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1712 *
1713 * DCE 6.0
1714 * - PPLL0 is available to all UNIPHY (DP only)
1715 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1716 *
1717 * DCE 5.0
1718 * - DCPLL is available to all UNIPHY (DP only)
1719 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1720 *
1721 * DCE 3.0/4.0/4.1
1722 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1723 *
1724 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001725static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1726{
Alex Deucher5df31962012-09-13 11:52:08 -04001727 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001728 struct drm_device *dev = crtc->dev;
1729 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001730 struct radeon_encoder *radeon_encoder =
1731 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001732 u32 pll_in_use;
1733 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001734
Alex Deucher0331f672012-09-14 11:57:21 -04001735 if (ASIC_IS_DCE8(rdev)) {
1736 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1737 if (rdev->clock.dp_extclk)
1738 /* skip PPLL programming if using ext clock */
1739 return ATOM_PPLL_INVALID;
1740 else {
1741 /* use the same PPLL for all DP monitors */
1742 pll = radeon_get_shared_dp_ppll(crtc);
1743 if (pll != ATOM_PPLL_INVALID)
1744 return pll;
1745 }
1746 } else {
1747 /* use the same PPLL for all monitors with the same clock */
1748 pll = radeon_get_shared_nondp_ppll(crtc);
1749 if (pll != ATOM_PPLL_INVALID)
1750 return pll;
1751 }
1752 /* otherwise, pick one of the plls */
1753 if ((rdev->family == CHIP_KAVERI) ||
1754 (rdev->family == CHIP_KABINI)) {
1755 /* KB/KV has PPLL1 and PPLL2 */
1756 pll_in_use = radeon_get_pll_use_mask(crtc);
1757 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1758 return ATOM_PPLL2;
1759 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1760 return ATOM_PPLL1;
1761 DRM_ERROR("unable to allocate a PPLL\n");
1762 return ATOM_PPLL_INVALID;
1763 } else {
1764 /* CI has PPLL0, PPLL1, and PPLL2 */
1765 pll_in_use = radeon_get_pll_use_mask(crtc);
1766 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1767 return ATOM_PPLL2;
1768 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1769 return ATOM_PPLL1;
1770 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1771 return ATOM_PPLL0;
1772 DRM_ERROR("unable to allocate a PPLL\n");
1773 return ATOM_PPLL_INVALID;
1774 }
1775 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001776 struct radeon_encoder_atom_dig *dig =
1777 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001778
Alex Deucher5df31962012-09-13 11:52:08 -04001779 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1780 (dig->linkb == false))
1781 /* UNIPHY A uses PPLL2 */
1782 return ATOM_PPLL2;
1783 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1784 /* UNIPHY B/C/D/E/F */
1785 if (rdev->clock.dp_extclk)
1786 /* skip PPLL programming if using ext clock */
1787 return ATOM_PPLL_INVALID;
1788 else {
1789 /* use the same PPLL for all DP monitors */
1790 pll = radeon_get_shared_dp_ppll(crtc);
1791 if (pll != ATOM_PPLL_INVALID)
1792 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001793 }
Alex Deucher5df31962012-09-13 11:52:08 -04001794 } else {
1795 /* use the same PPLL for all monitors with the same clock */
1796 pll = radeon_get_shared_nondp_ppll(crtc);
1797 if (pll != ATOM_PPLL_INVALID)
1798 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001799 }
1800 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001801 pll_in_use = radeon_get_pll_use_mask(crtc);
1802 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001803 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001804 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1805 return ATOM_PPLL1;
1806 DRM_ERROR("unable to allocate a PPLL\n");
1807 return ATOM_PPLL_INVALID;
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001808 } else if (ASIC_IS_DCE41(rdev)) {
1809 /* Don't share PLLs on DCE4.1 chips */
1810 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1811 if (rdev->clock.dp_extclk)
1812 /* skip PPLL programming if using ext clock */
1813 return ATOM_PPLL_INVALID;
1814 }
1815 pll_in_use = radeon_get_pll_use_mask(crtc);
1816 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1817 return ATOM_PPLL1;
1818 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1819 return ATOM_PPLL2;
1820 DRM_ERROR("unable to allocate a PPLL\n");
1821 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001822 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001823 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1824 * depending on the asic:
1825 * DCE4: PPLL or ext clock
1826 * DCE5: PPLL, DCPLL, or ext clock
1827 * DCE6: PPLL, PPLL0, or ext clock
1828 *
1829 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1830 * PPLL/DCPLL programming and only program the DP DTO for the
1831 * crtc virtual pixel clock.
1832 */
1833 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1834 if (rdev->clock.dp_extclk)
1835 /* skip PPLL programming if using ext clock */
1836 return ATOM_PPLL_INVALID;
1837 else if (ASIC_IS_DCE6(rdev))
1838 /* use PPLL0 for all DP */
1839 return ATOM_PPLL0;
1840 else if (ASIC_IS_DCE5(rdev))
1841 /* use DCPLL for all DP */
1842 return ATOM_DCPLL;
1843 else {
1844 /* use the same PPLL for all DP monitors */
1845 pll = radeon_get_shared_dp_ppll(crtc);
1846 if (pll != ATOM_PPLL_INVALID)
1847 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001848 }
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001849 } else {
Alex Deucher5df31962012-09-13 11:52:08 -04001850 /* use the same PPLL for all monitors with the same clock */
1851 pll = radeon_get_shared_nondp_ppll(crtc);
1852 if (pll != ATOM_PPLL_INVALID)
1853 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001854 }
1855 /* all other cases */
1856 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001857 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1858 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001859 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1860 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001861 DRM_ERROR("unable to allocate a PPLL\n");
1862 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001863 } else {
1864 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001865 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1866 * the matching btw pll and crtc is done through
1867 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1868 * pll (1 or 2) to select which register to write. ie if using
1869 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1870 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1871 * choose which value to write. Which is reverse order from
1872 * register logic. So only case that works is when pllid is
1873 * same as crtcid or when both pll and crtc are enabled and
1874 * both use same clock.
1875 *
1876 * So just return crtc id as if crtc and pll were hard linked
1877 * together even if they aren't
1878 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001879 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001880 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001881}
1882
Alex Deucherf3f1f032012-03-20 17:18:04 -04001883void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001884{
1885 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001886 if (ASIC_IS_DCE6(rdev))
1887 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1888 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001889 struct radeon_atom_ss ss;
1890 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1891 ASIC_INTERNAL_SS_ON_DCPLL,
1892 rdev->clock.default_dispclk);
1893 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001894 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001895 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001896 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001897 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001898 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001899 }
1900
1901}
1902
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001903int atombios_crtc_mode_set(struct drm_crtc *crtc,
1904 struct drm_display_mode *mode,
1905 struct drm_display_mode *adjusted_mode,
1906 int x, int y, struct drm_framebuffer *old_fb)
1907{
1908 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1909 struct drm_device *dev = crtc->dev;
1910 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001911 struct radeon_encoder *radeon_encoder =
1912 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04001913 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001914
Alex Deucher5df31962012-09-13 11:52:08 -04001915 if (radeon_encoder->active_device &
1916 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1917 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001918
1919 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001920
Alex Deucher54bfe492010-09-03 15:52:53 -04001921 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001922 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001923 else if (ASIC_IS_AVIVO(rdev)) {
1924 if (is_tvcv)
1925 atombios_crtc_set_timing(crtc, adjusted_mode);
1926 else
1927 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1928 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001929 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001930 if (radeon_crtc->crtc_id == 0)
1931 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001932 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001933 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001934 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001935 atombios_overscan_setup(crtc, mode, adjusted_mode);
1936 atombios_scaler_setup(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04001937 /* update the hw version fpr dpm */
1938 radeon_crtc->hw_mode = *adjusted_mode;
1939
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001940 return 0;
1941}
1942
1943static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001944 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001945 struct drm_display_mode *adjusted_mode)
1946{
Alex Deucher5df31962012-09-13 11:52:08 -04001947 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1948 struct drm_device *dev = crtc->dev;
1949 struct drm_encoder *encoder;
1950
1951 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
1952 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1953 if (encoder->crtc == crtc) {
1954 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04001955 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04001956 break;
1957 }
1958 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001959 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
1960 radeon_crtc->encoder = NULL;
1961 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04001962 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04001963 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001964 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1965 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001966 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1967 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04001968 /* pick pll */
1969 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1970 /* if we can't get a PPLL for a non-DP encoder, fail */
1971 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
1972 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
1973 return false;
1974
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001975 return true;
1976}
1977
1978static void atombios_crtc_prepare(struct drm_crtc *crtc)
1979{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001980 struct drm_device *dev = crtc->dev;
1981 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001982
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001983 /* disable crtc pair power gating before programming */
1984 if (ASIC_IS_DCE6(rdev))
1985 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1986
Alex Deucher37b43902010-02-09 12:04:43 -05001987 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001988 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001989}
1990
1991static void atombios_crtc_commit(struct drm_crtc *crtc)
1992{
1993 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001994 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001995}
1996
Alex Deucher37f90032010-06-11 17:58:38 -04001997static void atombios_crtc_disable(struct drm_crtc *crtc)
1998{
1999 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04002000 struct drm_device *dev = crtc->dev;
2001 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002002 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04002003 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002004
Alex Deucher37f90032010-06-11 17:58:38 -04002005 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07002006 if (crtc->primary->fb) {
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002007 int r;
2008 struct radeon_framebuffer *radeon_fb;
2009 struct radeon_bo *rbo;
2010
Matt Roperf4510a22014-04-01 15:22:40 -07002011 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002012 rbo = gem_to_radeon_bo(radeon_fb->obj);
2013 r = radeon_bo_reserve(rbo, false);
2014 if (unlikely(r))
2015 DRM_ERROR("failed to reserve rbo before unpin\n");
2016 else {
2017 radeon_bo_unpin(rbo);
2018 radeon_bo_unreserve(rbo);
2019 }
2020 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04002021 /* disable the GRPH */
2022 if (ASIC_IS_DCE4(rdev))
2023 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2024 else if (ASIC_IS_AVIVO(rdev))
2025 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2026
Alex Deucher0e3d50b2013-02-05 11:47:09 -05002027 if (ASIC_IS_DCE6(rdev))
2028 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04002029
Alex Deucher4e585912012-08-21 19:06:21 -04002030 for (i = 0; i < rdev->num_crtc; i++) {
2031 if (rdev->mode_info.crtcs[i] &&
2032 rdev->mode_info.crtcs[i]->enabled &&
2033 i != radeon_crtc->crtc_id &&
2034 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
2035 /* one other crtc is using this pll don't turn
2036 * off the pll
2037 */
2038 goto done;
2039 }
2040 }
2041
Alex Deucher37f90032010-06-11 17:58:38 -04002042 switch (radeon_crtc->pll_id) {
2043 case ATOM_PPLL1:
2044 case ATOM_PPLL2:
2045 /* disable the ppll */
2046 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002047 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002048 break;
Alex Deucher64199872012-03-20 17:18:33 -04002049 case ATOM_PPLL0:
2050 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002051 if ((rdev->family == CHIP_ARUBA) ||
2052 (rdev->family == CHIP_BONAIRE) ||
2053 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002054 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2055 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2056 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002057 default:
2058 break;
2059 }
Alex Deucher4e585912012-08-21 19:06:21 -04002060done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002061 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002062 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002063 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002064 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002065}
2066
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002067static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2068 .dpms = atombios_crtc_dpms,
2069 .mode_fixup = atombios_crtc_mode_fixup,
2070 .mode_set = atombios_crtc_mode_set,
2071 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002072 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002073 .prepare = atombios_crtc_prepare,
2074 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002075 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002076 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002077};
2078
2079void radeon_atombios_init_crtc(struct drm_device *dev,
2080 struct radeon_crtc *radeon_crtc)
2081{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002082 struct radeon_device *rdev = dev->dev_private;
2083
2084 if (ASIC_IS_DCE4(rdev)) {
2085 switch (radeon_crtc->crtc_id) {
2086 case 0:
2087 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002088 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002089 break;
2090 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002091 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002092 break;
2093 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002094 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002095 break;
2096 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002097 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002098 break;
2099 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002100 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002101 break;
2102 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002103 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002104 break;
2105 }
2106 } else {
2107 if (radeon_crtc->crtc_id == 1)
2108 radeon_crtc->crtc_offset =
2109 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2110 else
2111 radeon_crtc->crtc_offset = 0;
2112 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002113 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002114 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002115 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002116 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002117 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2118}