blob: 0d19f4f94d5a065b2687399b559aaf43f4a5c587 [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 Deucher7d5a33b2014-02-03 15:53:25 -0500562 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400563 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500564
Alex Deucher4eaeca32010-01-19 17:32:27 -0500565 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400566 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200567
568 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400569 if ((rdev->family == CHIP_RS600) ||
570 (rdev->family == CHIP_RS690) ||
571 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400572 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
573 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000574
575 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400576 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000577 else
Alex Deucher19eca432012-09-13 10:56:16 -0400578 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400579
Alex Deucher5785e532011-04-19 15:24:59 -0400580 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400581 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400582 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500583 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400584 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400585 /* use frac fb div on RS780/RS880 */
586 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
587 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500588 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
589 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000590 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400591 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200592
Dave Airlie5480f722010-10-19 10:36:47 +1000593 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400594 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000595 else
Alex Deucher19eca432012-09-13 10:56:16 -0400596 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000597 }
598
Alex Deucher5df31962012-09-13 11:52:08 -0400599 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
600 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
601 if (connector) {
602 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
603 struct radeon_connector_atom_dig *dig_connector =
604 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400605
Alex Deucher5df31962012-09-13 11:52:08 -0400606 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200607 }
608 }
609
Alex Deucher5df31962012-09-13 11:52:08 -0400610 /* use recommended ref_div for ss */
611 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
612 if (radeon_crtc->ss_enabled) {
613 if (radeon_crtc->ss.refdiv) {
614 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
615 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
616 if (ASIC_IS_AVIVO(rdev))
617 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
618 }
619 }
620 }
621
622 if (ASIC_IS_AVIVO(rdev)) {
623 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
624 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
625 adjusted_clock = mode->clock * 2;
626 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
627 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
628 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
629 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
630 } else {
631 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
632 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
633 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
634 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
635 }
636
Alex Deucher2606c882009-10-08 13:36:21 -0400637 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
638 * accordingly based on the encoder/transmitter to work around
639 * special hw requirements.
640 */
641 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500642 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500643 u8 frev, crev;
644 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400645
Alex Deucher2606c882009-10-08 13:36:21 -0400646 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400647 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
648 &crev))
649 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500650
651 memset(&args, 0, sizeof(args));
652
653 switch (frev) {
654 case 1:
655 switch (crev) {
656 case 1:
657 case 2:
658 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
659 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500660 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400661 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400662 args.v1.ucConfig |=
663 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500664
665 atom_execute_table(rdev->mode_info.atom_context,
666 index, (uint32_t *)&args);
667 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
668 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500669 case 3:
670 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
671 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
672 args.v3.sInput.ucEncodeMode = encoder_mode;
673 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400674 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000675 args.v3.sInput.ucDispPllConfig |=
676 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400677 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400678 args.v3.sInput.ucDispPllConfig |=
679 DISPPLL_CONFIG_COHERENT_MODE;
680 /* 16200 or 27000 */
681 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
682 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500683 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400684 if (encoder_mode == ATOM_ENCODER_MODE_HDMI)
685 /* deep color support */
686 args.v3.sInput.usPixelClock =
687 cpu_to_le16((mode->clock * bpc / 8) / 10);
688 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500689 args.v3.sInput.ucDispPllConfig |=
690 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500691 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500692 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400693 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500694 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400695 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
696 ENCODER_OBJECT_ID_NONE)
697 args.v3.sInput.ucExtTransmitterID =
698 radeon_encoder_get_dp_bridge_encoder_id(encoder);
699 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400700 args.v3.sInput.ucExtTransmitterID = 0;
701
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500702 atom_execute_table(rdev->mode_info.atom_context,
703 index, (uint32_t *)&args);
704 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
705 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400706 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
707 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
708 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500709 }
710 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400711 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
712 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
713 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500714 }
715 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500716 default:
717 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
718 return adjusted_clock;
719 }
720 break;
721 default:
722 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
723 return adjusted_clock;
724 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400725 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500726 return adjusted_clock;
727}
728
729union set_pixel_clock {
730 SET_PIXEL_CLOCK_PS_ALLOCATION base;
731 PIXEL_CLOCK_PARAMETERS v1;
732 PIXEL_CLOCK_PARAMETERS_V2 v2;
733 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500734 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500735 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500736};
737
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500738/* on DCE5, make sure the voltage is high enough to support the
739 * required disp clk.
740 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400741static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500742 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500743{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500744 u8 frev, crev;
745 int index;
746 union set_pixel_clock args;
747
748 memset(&args, 0, sizeof(args));
749
750 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400751 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
752 &crev))
753 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500754
755 switch (frev) {
756 case 1:
757 switch (crev) {
758 case 5:
759 /* if the default dcpll clock is specified,
760 * SetPixelClock provides the dividers
761 */
762 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500763 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500764 args.v5.ucPpll = ATOM_DCPLL;
765 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500766 case 6:
767 /* if the default dcpll clock is specified,
768 * SetPixelClock provides the dividers
769 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500770 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400771 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400772 args.v6.ucPpll = ATOM_EXT_PLL1;
773 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400774 args.v6.ucPpll = ATOM_PPLL0;
775 else
776 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500777 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500778 default:
779 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
780 return;
781 }
782 break;
783 default:
784 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
785 return;
786 }
787 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
788}
789
Alex Deucher37f90032010-06-11 17:58:38 -0400790static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000791 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400792 int pll_id,
793 u32 encoder_mode,
794 u32 encoder_id,
795 u32 clock,
796 u32 ref_div,
797 u32 fb_div,
798 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400799 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400800 int bpc,
801 bool ss_enabled,
802 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400803{
804 struct drm_device *dev = crtc->dev;
805 struct radeon_device *rdev = dev->dev_private;
806 u8 frev, crev;
807 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
808 union set_pixel_clock args;
809
810 memset(&args, 0, sizeof(args));
811
812 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
813 &crev))
814 return;
815
816 switch (frev) {
817 case 1:
818 switch (crev) {
819 case 1:
820 if (clock == ATOM_DISABLE)
821 return;
822 args.v1.usPixelClock = cpu_to_le16(clock / 10);
823 args.v1.usRefDiv = cpu_to_le16(ref_div);
824 args.v1.usFbDiv = cpu_to_le16(fb_div);
825 args.v1.ucFracFbDiv = frac_fb_div;
826 args.v1.ucPostDiv = post_div;
827 args.v1.ucPpll = pll_id;
828 args.v1.ucCRTC = crtc_id;
829 args.v1.ucRefDivSrc = 1;
830 break;
831 case 2:
832 args.v2.usPixelClock = cpu_to_le16(clock / 10);
833 args.v2.usRefDiv = cpu_to_le16(ref_div);
834 args.v2.usFbDiv = cpu_to_le16(fb_div);
835 args.v2.ucFracFbDiv = frac_fb_div;
836 args.v2.ucPostDiv = post_div;
837 args.v2.ucPpll = pll_id;
838 args.v2.ucCRTC = crtc_id;
839 args.v2.ucRefDivSrc = 1;
840 break;
841 case 3:
842 args.v3.usPixelClock = cpu_to_le16(clock / 10);
843 args.v3.usRefDiv = cpu_to_le16(ref_div);
844 args.v3.usFbDiv = cpu_to_le16(fb_div);
845 args.v3.ucFracFbDiv = frac_fb_div;
846 args.v3.ucPostDiv = post_div;
847 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400848 if (crtc_id == ATOM_CRTC2)
849 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
850 else
851 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400852 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
853 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400854 args.v3.ucTransmitterId = encoder_id;
855 args.v3.ucEncoderMode = encoder_mode;
856 break;
857 case 5:
858 args.v5.ucCRTC = crtc_id;
859 args.v5.usPixelClock = cpu_to_le16(clock / 10);
860 args.v5.ucRefDiv = ref_div;
861 args.v5.usFbDiv = cpu_to_le16(fb_div);
862 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
863 args.v5.ucPostDiv = post_div;
864 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400865 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
866 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400867 switch (bpc) {
868 case 8:
869 default:
870 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
871 break;
872 case 10:
873 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
874 break;
875 }
Alex Deucher37f90032010-06-11 17:58:38 -0400876 args.v5.ucTransmitterID = encoder_id;
877 args.v5.ucEncoderMode = encoder_mode;
878 args.v5.ucPpll = pll_id;
879 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500880 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000881 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500882 args.v6.ucRefDiv = ref_div;
883 args.v6.usFbDiv = cpu_to_le16(fb_div);
884 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
885 args.v6.ucPostDiv = post_div;
886 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400887 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
888 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400889 switch (bpc) {
890 case 8:
891 default:
892 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
893 break;
894 case 10:
895 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
896 break;
897 case 12:
898 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
899 break;
900 case 16:
901 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
902 break;
903 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500904 args.v6.ucTransmitterID = encoder_id;
905 args.v6.ucEncoderMode = encoder_mode;
906 args.v6.ucPpll = pll_id;
907 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400908 default:
909 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
910 return;
911 }
912 break;
913 default:
914 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
915 return;
916 }
917
918 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
919}
920
Alex Deucher19eca432012-09-13 10:56:16 -0400921static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
922{
923 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
924 struct drm_device *dev = crtc->dev;
925 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400926 struct radeon_encoder *radeon_encoder =
927 to_radeon_encoder(radeon_crtc->encoder);
928 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400929
930 radeon_crtc->bpc = 8;
931 radeon_crtc->ss_enabled = false;
932
Alex Deucher19eca432012-09-13 10:56:16 -0400933 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400934 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400935 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
936 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400937 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400938 struct radeon_connector *radeon_connector =
939 to_radeon_connector(connector);
940 struct radeon_connector_atom_dig *dig_connector =
941 radeon_connector->con_priv;
942 int dp_clock;
943 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
944
945 switch (encoder_mode) {
946 case ATOM_ENCODER_MODE_DP_MST:
947 case ATOM_ENCODER_MODE_DP:
948 /* DP/eDP */
949 dp_clock = dig_connector->dp_clock / 10;
950 if (ASIC_IS_DCE4(rdev))
951 radeon_crtc->ss_enabled =
952 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
953 ASIC_INTERNAL_SS_ON_DP,
954 dp_clock);
955 else {
956 if (dp_clock == 16200) {
957 radeon_crtc->ss_enabled =
958 radeon_atombios_get_ppll_ss_info(rdev,
959 &radeon_crtc->ss,
960 ATOM_DP_SS_ID2);
961 if (!radeon_crtc->ss_enabled)
962 radeon_crtc->ss_enabled =
963 radeon_atombios_get_ppll_ss_info(rdev,
964 &radeon_crtc->ss,
965 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500966 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400967 radeon_crtc->ss_enabled =
968 radeon_atombios_get_ppll_ss_info(rdev,
969 &radeon_crtc->ss,
970 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500971 }
972 /* disable spread spectrum on DCE3 DP */
973 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -0400974 }
975 break;
976 case ATOM_ENCODER_MODE_LVDS:
977 if (ASIC_IS_DCE4(rdev))
978 radeon_crtc->ss_enabled =
979 radeon_atombios_get_asic_ss_info(rdev,
980 &radeon_crtc->ss,
981 dig->lcd_ss_id,
982 mode->clock / 10);
983 else
984 radeon_crtc->ss_enabled =
985 radeon_atombios_get_ppll_ss_info(rdev,
986 &radeon_crtc->ss,
987 dig->lcd_ss_id);
988 break;
989 case ATOM_ENCODER_MODE_DVI:
990 if (ASIC_IS_DCE4(rdev))
991 radeon_crtc->ss_enabled =
992 radeon_atombios_get_asic_ss_info(rdev,
993 &radeon_crtc->ss,
994 ASIC_INTERNAL_SS_ON_TMDS,
995 mode->clock / 10);
996 break;
997 case ATOM_ENCODER_MODE_HDMI:
998 if (ASIC_IS_DCE4(rdev))
999 radeon_crtc->ss_enabled =
1000 radeon_atombios_get_asic_ss_info(rdev,
1001 &radeon_crtc->ss,
1002 ASIC_INTERNAL_SS_ON_HDMI,
1003 mode->clock / 10);
1004 break;
1005 default:
1006 break;
1007 }
1008 }
1009
1010 /* adjust pixel clock as needed */
1011 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1012
1013 return true;
1014}
1015
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001016static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001017{
1018 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1019 struct drm_device *dev = crtc->dev;
1020 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001021 struct radeon_encoder *radeon_encoder =
1022 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001023 u32 pll_clock = mode->clock;
1024 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1025 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001026 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001027
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001028 switch (radeon_crtc->pll_id) {
1029 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001030 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001031 break;
1032 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001033 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001034 break;
1035 case ATOM_DCPLL:
1036 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001037 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001038 pll = &rdev->clock.dcpll;
1039 break;
1040 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001041
Alex Deucher19eca432012-09-13 10:56:16 -04001042 /* update pll params */
1043 pll->flags = radeon_crtc->pll_flags;
1044 pll->reference_div = radeon_crtc->pll_reference_div;
1045 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001046
Alex Deucher64146f82011-03-22 01:46:12 -04001047 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1048 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001049 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1050 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001051 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001052 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1053 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001054 else
Alex Deucher19eca432012-09-13 10:56:16 -04001055 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1056 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001057
Alex Deucher19eca432012-09-13 10:56:16 -04001058 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1059 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001060
Alex Deucher37f90032010-06-11 17:58:38 -04001061 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1062 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001063 ref_div, fb_div, frac_fb_div, post_div,
1064 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001065
Alex Deucher19eca432012-09-13 10:56:16 -04001066 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001067 /* calculate ss amount and step size */
1068 if (ASIC_IS_DCE4(rdev)) {
1069 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001070 u32 amount = (((fb_div * 10) + frac_fb_div) *
1071 (u32)radeon_crtc->ss.percentage) /
1072 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001073 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1074 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001075 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001076 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001077 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001078 (125 * 25 * pll->reference_freq / 100);
1079 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001080 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001081 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001082 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001083 }
1084
Alex Deucher19eca432012-09-13 10:56:16 -04001085 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1086 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001087 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001088}
1089
Alex Deucherc9417bd2011-02-06 14:23:26 -05001090static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1091 struct drm_framebuffer *fb,
1092 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001093{
1094 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1095 struct drm_device *dev = crtc->dev;
1096 struct radeon_device *rdev = dev->dev_private;
1097 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001098 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001099 struct drm_gem_object *obj;
1100 struct radeon_bo *rbo;
1101 uint64_t fb_location;
1102 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001103 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001104 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001105 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001106 int r;
1107
1108 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001109 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001110 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001111 return 0;
1112 }
1113
Chris Ball4dd19b02010-09-26 06:47:23 -05001114 if (atomic) {
1115 radeon_fb = to_radeon_framebuffer(fb);
1116 target_fb = fb;
1117 }
1118 else {
1119 radeon_fb = to_radeon_framebuffer(crtc->fb);
1120 target_fb = crtc->fb;
1121 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001122
Chris Ball4dd19b02010-09-26 06:47:23 -05001123 /* If atomic, assume fb object is pinned & idle & fenced and
1124 * just update base pointers
1125 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001126 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001127 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001128 r = radeon_bo_reserve(rbo, false);
1129 if (unlikely(r != 0))
1130 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001131
1132 if (atomic)
1133 fb_location = radeon_bo_gpu_offset(rbo);
1134 else {
1135 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1136 if (unlikely(r != 0)) {
1137 radeon_bo_unreserve(rbo);
1138 return -EINVAL;
1139 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001140 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001141
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001142 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1143 radeon_bo_unreserve(rbo);
1144
Chris Ball4dd19b02010-09-26 06:47:23 -05001145 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001146 case 8:
1147 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1148 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1149 break;
1150 case 15:
1151 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1152 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1153 break;
1154 case 16:
1155 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1156 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001157#ifdef __BIG_ENDIAN
1158 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1159#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001160 break;
1161 case 24:
1162 case 32:
1163 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1164 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001165#ifdef __BIG_ENDIAN
1166 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1167#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001168 break;
1169 default:
1170 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001171 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001172 return -EINVAL;
1173 }
1174
Alex Deucher392e3722011-11-28 14:49:27 -05001175 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001176 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001177
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001178 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001179 if (rdev->family >= CHIP_TAHITI) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001180 unsigned tileb, index, num_banks, tile_split_bytes;
1181
1182 /* Calculate the macrotile mode index. */
1183 tile_split_bytes = 64 << tile_split;
1184 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1185 tileb = min(tile_split_bytes, tileb);
1186
1187 for (index = 0; tileb > 64; index++) {
1188 tileb >>= 1;
1189 }
1190
1191 if (index >= 16) {
1192 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1193 target_fb->bits_per_pixel, tile_split);
1194 return -EINVAL;
1195 }
1196
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001197 if (rdev->family >= CHIP_BONAIRE)
1198 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
1199 else
1200 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001201 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1202 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001203 /* NI and older. */
1204 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001205 tmp = rdev->config.cayman.tile_config;
1206 else
1207 tmp = rdev->config.evergreen.tile_config;
1208
1209 switch ((tmp & 0xf0) >> 4) {
1210 case 0: /* 4 banks */
1211 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1212 break;
1213 case 1: /* 8 banks */
1214 default:
1215 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1216 break;
1217 case 2: /* 16 banks */
1218 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1219 break;
1220 }
Alex Deucher392e3722011-11-28 14:49:27 -05001221 }
1222
Alex Deucher97d66322010-05-20 12:12:48 -04001223 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001224 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1225 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1226 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1227 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001228 if (rdev->family >= CHIP_BONAIRE) {
1229 /* XXX need to know more about the surface tiling mode */
1230 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1231 }
Alex Deucher392e3722011-11-28 14:49:27 -05001232 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001233 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1234
Alex Deucher8da0e502012-07-11 18:38:29 -04001235 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001236 /* Read the pipe config from the 2D TILED SCANOUT mode.
1237 * It should be the same for the other modes too, but not all
1238 * modes set the pipe config field. */
1239 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1240
1241 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001242 } else if ((rdev->family == CHIP_TAHITI) ||
1243 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001244 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001245 else if ((rdev->family == CHIP_VERDE) ||
1246 (rdev->family == CHIP_OLAND) ||
1247 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001248 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1249
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001250 switch (radeon_crtc->crtc_id) {
1251 case 0:
1252 WREG32(AVIVO_D1VGA_CONTROL, 0);
1253 break;
1254 case 1:
1255 WREG32(AVIVO_D2VGA_CONTROL, 0);
1256 break;
1257 case 2:
1258 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1259 break;
1260 case 3:
1261 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1262 break;
1263 case 4:
1264 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1265 break;
1266 case 5:
1267 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1268 break;
1269 default:
1270 break;
1271 }
1272
1273 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1274 upper_32_bits(fb_location));
1275 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1276 upper_32_bits(fb_location));
1277 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1278 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1279 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1280 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1281 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001282 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001283
1284 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1285 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1286 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1287 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001288 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1289 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001290
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001291 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001292 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1293 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1294
Alex Deucher8da0e502012-07-11 18:38:29 -04001295 if (rdev->family >= CHIP_BONAIRE)
1296 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1297 target_fb->height);
1298 else
1299 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1300 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001301 x &= ~3;
1302 y &= ~1;
1303 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1304 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001305 viewport_w = crtc->mode.hdisplay;
1306 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001307 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001308 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001309
Alex Deucherfb9674b2011-04-02 09:15:50 -04001310 /* pageflip setup */
1311 /* make sure flip is at vb rather than hb */
1312 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1313 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1314 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1315
1316 /* set pageflip to happen anywhere in vblank interval */
1317 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1318
Chris Ball4dd19b02010-09-26 06:47:23 -05001319 if (!atomic && fb && fb != crtc->fb) {
1320 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001321 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001322 r = radeon_bo_reserve(rbo, false);
1323 if (unlikely(r != 0))
1324 return r;
1325 radeon_bo_unpin(rbo);
1326 radeon_bo_unreserve(rbo);
1327 }
1328
1329 /* Bytes per pixel may have changed */
1330 radeon_bandwidth_update(rdev);
1331
1332 return 0;
1333}
1334
Chris Ball4dd19b02010-09-26 06:47:23 -05001335static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1336 struct drm_framebuffer *fb,
1337 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001338{
1339 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1340 struct drm_device *dev = crtc->dev;
1341 struct radeon_device *rdev = dev->dev_private;
1342 struct radeon_framebuffer *radeon_fb;
1343 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001344 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001345 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001346 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001347 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001348 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001349 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001350 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001351
Jerome Glisse2de3b482009-11-17 14:08:55 -08001352 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001353 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001354 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001355 return 0;
1356 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001357
Chris Ball4dd19b02010-09-26 06:47:23 -05001358 if (atomic) {
1359 radeon_fb = to_radeon_framebuffer(fb);
1360 target_fb = fb;
1361 }
1362 else {
1363 radeon_fb = to_radeon_framebuffer(crtc->fb);
1364 target_fb = crtc->fb;
1365 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001366
1367 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001368 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001369 r = radeon_bo_reserve(rbo, false);
1370 if (unlikely(r != 0))
1371 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001372
1373 /* If atomic, assume fb object is pinned & idle & fenced and
1374 * just update base pointers
1375 */
1376 if (atomic)
1377 fb_location = radeon_bo_gpu_offset(rbo);
1378 else {
1379 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1380 if (unlikely(r != 0)) {
1381 radeon_bo_unreserve(rbo);
1382 return -EINVAL;
1383 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001384 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001385 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1386 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001387
Chris Ball4dd19b02010-09-26 06:47:23 -05001388 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001389 case 8:
1390 fb_format =
1391 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1392 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1393 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001394 case 15:
1395 fb_format =
1396 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1397 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1398 break;
1399 case 16:
1400 fb_format =
1401 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1402 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001403#ifdef __BIG_ENDIAN
1404 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1405#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001406 break;
1407 case 24:
1408 case 32:
1409 fb_format =
1410 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1411 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001412#ifdef __BIG_ENDIAN
1413 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1414#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001415 break;
1416 default:
1417 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001418 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001419 return -EINVAL;
1420 }
1421
Alex Deucher40c4ac12010-05-20 12:04:59 -04001422 if (rdev->family >= CHIP_R600) {
1423 if (tiling_flags & RADEON_TILING_MACRO)
1424 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1425 else if (tiling_flags & RADEON_TILING_MICRO)
1426 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1427 } else {
1428 if (tiling_flags & RADEON_TILING_MACRO)
1429 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001430
Alex Deucher40c4ac12010-05-20 12:04:59 -04001431 if (tiling_flags & RADEON_TILING_MICRO)
1432 fb_format |= AVIVO_D1GRPH_TILED;
1433 }
Dave Airliee024e112009-06-24 09:48:08 +10001434
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001435 if (radeon_crtc->crtc_id == 0)
1436 WREG32(AVIVO_D1VGA_CONTROL, 0);
1437 else
1438 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001439
1440 if (rdev->family >= CHIP_RV770) {
1441 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001442 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1443 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001444 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001445 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1446 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001447 }
1448 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001449 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1450 (u32) fb_location);
1451 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1452 radeon_crtc->crtc_offset, (u32) fb_location);
1453 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001454 if (rdev->family >= CHIP_R600)
1455 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001456
1457 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1458 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1459 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1460 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001461 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1462 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001463
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001464 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001465 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1466 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1467
1468 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001469 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001470 x &= ~3;
1471 y &= ~1;
1472 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1473 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001474 viewport_w = crtc->mode.hdisplay;
1475 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001476 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001477 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001478
Alex Deucherfb9674b2011-04-02 09:15:50 -04001479 /* pageflip setup */
1480 /* make sure flip is at vb rather than hb */
1481 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1482 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1483 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1484
1485 /* set pageflip to happen anywhere in vblank interval */
1486 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1487
Chris Ball4dd19b02010-09-26 06:47:23 -05001488 if (!atomic && fb && fb != crtc->fb) {
1489 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001490 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001491 r = radeon_bo_reserve(rbo, false);
1492 if (unlikely(r != 0))
1493 return r;
1494 radeon_bo_unpin(rbo);
1495 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001496 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001497
1498 /* Bytes per pixel may have changed */
1499 radeon_bandwidth_update(rdev);
1500
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001501 return 0;
1502}
1503
Alex Deucher54f088a2010-01-19 16:34:01 -05001504int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1505 struct drm_framebuffer *old_fb)
1506{
1507 struct drm_device *dev = crtc->dev;
1508 struct radeon_device *rdev = dev->dev_private;
1509
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001510 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001511 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001512 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001513 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001514 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001515 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1516}
1517
1518int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1519 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001520 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001521{
1522 struct drm_device *dev = crtc->dev;
1523 struct radeon_device *rdev = dev->dev_private;
1524
1525 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001526 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001527 else if (ASIC_IS_AVIVO(rdev))
1528 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1529 else
1530 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001531}
1532
Alex Deucher615e0cb2010-01-20 16:22:53 -05001533/* properly set additional regs when using atombios */
1534static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1535{
1536 struct drm_device *dev = crtc->dev;
1537 struct radeon_device *rdev = dev->dev_private;
1538 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1539 u32 disp_merge_cntl;
1540
1541 switch (radeon_crtc->crtc_id) {
1542 case 0:
1543 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1544 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1545 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1546 break;
1547 case 1:
1548 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1549 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1550 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1551 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1552 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1553 break;
1554 }
1555}
1556
Alex Deucherf3dd8502012-08-31 11:56:50 -04001557/**
1558 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1559 *
1560 * @crtc: drm crtc
1561 *
1562 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1563 */
1564static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1565{
1566 struct drm_device *dev = crtc->dev;
1567 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001568 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001569 u32 pll_in_use = 0;
1570
1571 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1572 if (crtc == test_crtc)
1573 continue;
1574
Alex Deucher57b35e22012-09-17 17:34:45 -04001575 test_radeon_crtc = to_radeon_crtc(test_crtc);
1576 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1577 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001578 }
1579 return pll_in_use;
1580}
1581
1582/**
1583 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1584 *
1585 * @crtc: drm crtc
1586 *
1587 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1588 * also in DP mode. For DP, a single PPLL can be used for all DP
1589 * crtcs/encoders.
1590 */
1591static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1592{
1593 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001594 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001595 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001596
Alex Deucher57b35e22012-09-17 17:34:45 -04001597 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1598 if (crtc == test_crtc)
1599 continue;
1600 test_radeon_crtc = to_radeon_crtc(test_crtc);
1601 if (test_radeon_crtc->encoder &&
1602 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1603 /* for DP use the same PLL for all */
1604 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1605 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001606 }
1607 }
1608 return ATOM_PPLL_INVALID;
1609}
1610
1611/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001612 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1613 *
1614 * @crtc: drm crtc
1615 * @encoder: drm encoder
1616 *
1617 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1618 * be shared (i.e., same clock).
1619 */
Alex Deucher5df31962012-09-13 11:52:08 -04001620static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001621{
Alex Deucher5df31962012-09-13 11:52:08 -04001622 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001623 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001624 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001625 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001626 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001627
Alex Deucher9642ac02012-09-13 12:43:41 -04001628 adjusted_clock = radeon_crtc->adjusted_clock;
1629
1630 if (adjusted_clock == 0)
1631 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001632
Alex Deucher57b35e22012-09-17 17:34:45 -04001633 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1634 if (crtc == test_crtc)
1635 continue;
1636 test_radeon_crtc = to_radeon_crtc(test_crtc);
1637 if (test_radeon_crtc->encoder &&
1638 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1639 /* check if we are already driving this connector with another crtc */
1640 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1641 /* if we are, return that pll */
1642 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001643 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001644 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001645 /* for non-DP check the clock */
1646 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1647 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1648 (adjusted_clock == test_adjusted_clock) &&
1649 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1650 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1651 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001652 }
1653 }
1654 return ATOM_PPLL_INVALID;
1655}
1656
1657/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001658 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1659 *
1660 * @crtc: drm crtc
1661 *
1662 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1663 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1664 * monitors a dedicated PPLL must be used. If a particular board has
1665 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1666 * as there is no need to program the PLL itself. If we are not able to
1667 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1668 * avoid messing up an existing monitor.
1669 *
1670 * Asic specific PLL information
1671 *
Alex Deucher0331f672012-09-14 11:57:21 -04001672 * DCE 8.x
1673 * KB/KV
1674 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1675 * CI
1676 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1677 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001678 * DCE 6.1
1679 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1680 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1681 *
1682 * DCE 6.0
1683 * - PPLL0 is available to all UNIPHY (DP only)
1684 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1685 *
1686 * DCE 5.0
1687 * - DCPLL is available to all UNIPHY (DP only)
1688 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1689 *
1690 * DCE 3.0/4.0/4.1
1691 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1692 *
1693 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001694static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1695{
Alex Deucher5df31962012-09-13 11:52:08 -04001696 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001697 struct drm_device *dev = crtc->dev;
1698 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001699 struct radeon_encoder *radeon_encoder =
1700 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001701 u32 pll_in_use;
1702 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001703
Alex Deucher0331f672012-09-14 11:57:21 -04001704 if (ASIC_IS_DCE8(rdev)) {
1705 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1706 if (rdev->clock.dp_extclk)
1707 /* skip PPLL programming if using ext clock */
1708 return ATOM_PPLL_INVALID;
1709 else {
1710 /* use the same PPLL for all DP monitors */
1711 pll = radeon_get_shared_dp_ppll(crtc);
1712 if (pll != ATOM_PPLL_INVALID)
1713 return pll;
1714 }
1715 } else {
1716 /* use the same PPLL for all monitors with the same clock */
1717 pll = radeon_get_shared_nondp_ppll(crtc);
1718 if (pll != ATOM_PPLL_INVALID)
1719 return pll;
1720 }
1721 /* otherwise, pick one of the plls */
1722 if ((rdev->family == CHIP_KAVERI) ||
1723 (rdev->family == CHIP_KABINI)) {
1724 /* KB/KV has PPLL1 and PPLL2 */
1725 pll_in_use = radeon_get_pll_use_mask(crtc);
1726 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1727 return ATOM_PPLL2;
1728 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1729 return ATOM_PPLL1;
1730 DRM_ERROR("unable to allocate a PPLL\n");
1731 return ATOM_PPLL_INVALID;
1732 } else {
1733 /* CI has PPLL0, PPLL1, and PPLL2 */
1734 pll_in_use = radeon_get_pll_use_mask(crtc);
1735 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1736 return ATOM_PPLL2;
1737 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1738 return ATOM_PPLL1;
1739 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1740 return ATOM_PPLL0;
1741 DRM_ERROR("unable to allocate a PPLL\n");
1742 return ATOM_PPLL_INVALID;
1743 }
1744 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001745 struct radeon_encoder_atom_dig *dig =
1746 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001747
Alex Deucher5df31962012-09-13 11:52:08 -04001748 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1749 (dig->linkb == false))
1750 /* UNIPHY A uses PPLL2 */
1751 return ATOM_PPLL2;
1752 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1753 /* UNIPHY B/C/D/E/F */
1754 if (rdev->clock.dp_extclk)
1755 /* skip PPLL programming if using ext clock */
1756 return ATOM_PPLL_INVALID;
1757 else {
1758 /* use the same PPLL for all DP monitors */
1759 pll = radeon_get_shared_dp_ppll(crtc);
1760 if (pll != ATOM_PPLL_INVALID)
1761 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001762 }
Alex Deucher5df31962012-09-13 11:52:08 -04001763 } else {
1764 /* use the same PPLL for all monitors with the same clock */
1765 pll = radeon_get_shared_nondp_ppll(crtc);
1766 if (pll != ATOM_PPLL_INVALID)
1767 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001768 }
1769 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001770 pll_in_use = radeon_get_pll_use_mask(crtc);
1771 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001772 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001773 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1774 return ATOM_PPLL1;
1775 DRM_ERROR("unable to allocate a PPLL\n");
1776 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001777 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001778 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1779 * depending on the asic:
1780 * DCE4: PPLL or ext clock
1781 * DCE5: PPLL, DCPLL, or ext clock
1782 * DCE6: PPLL, PPLL0, or ext clock
1783 *
1784 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1785 * PPLL/DCPLL programming and only program the DP DTO for the
1786 * crtc virtual pixel clock.
1787 */
1788 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1789 if (rdev->clock.dp_extclk)
1790 /* skip PPLL programming if using ext clock */
1791 return ATOM_PPLL_INVALID;
1792 else if (ASIC_IS_DCE6(rdev))
1793 /* use PPLL0 for all DP */
1794 return ATOM_PPLL0;
1795 else if (ASIC_IS_DCE5(rdev))
1796 /* use DCPLL for all DP */
1797 return ATOM_DCPLL;
1798 else {
1799 /* use the same PPLL for all DP monitors */
1800 pll = radeon_get_shared_dp_ppll(crtc);
1801 if (pll != ATOM_PPLL_INVALID)
1802 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001803 }
Alex Deucher70471862013-10-31 16:43:27 -04001804 } else if (!ASIC_IS_DCE41(rdev)) { /* Don't share PLLs on DCE4.1 chips */
Alex Deucher5df31962012-09-13 11:52:08 -04001805 /* use the same PPLL for all monitors with the same clock */
1806 pll = radeon_get_shared_nondp_ppll(crtc);
1807 if (pll != ATOM_PPLL_INVALID)
1808 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001809 }
1810 /* all other cases */
1811 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001812 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1813 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001814 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1815 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001816 DRM_ERROR("unable to allocate a PPLL\n");
1817 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001818 } else {
1819 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001820 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1821 * the matching btw pll and crtc is done through
1822 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1823 * pll (1 or 2) to select which register to write. ie if using
1824 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1825 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1826 * choose which value to write. Which is reverse order from
1827 * register logic. So only case that works is when pllid is
1828 * same as crtcid or when both pll and crtc are enabled and
1829 * both use same clock.
1830 *
1831 * So just return crtc id as if crtc and pll were hard linked
1832 * together even if they aren't
1833 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001834 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001835 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001836}
1837
Alex Deucherf3f1f032012-03-20 17:18:04 -04001838void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001839{
1840 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001841 if (ASIC_IS_DCE6(rdev))
1842 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1843 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001844 struct radeon_atom_ss ss;
1845 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1846 ASIC_INTERNAL_SS_ON_DCPLL,
1847 rdev->clock.default_dispclk);
1848 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001849 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001850 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001851 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001852 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001853 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001854 }
1855
1856}
1857
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001858int atombios_crtc_mode_set(struct drm_crtc *crtc,
1859 struct drm_display_mode *mode,
1860 struct drm_display_mode *adjusted_mode,
1861 int x, int y, struct drm_framebuffer *old_fb)
1862{
1863 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1864 struct drm_device *dev = crtc->dev;
1865 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001866 struct radeon_encoder *radeon_encoder =
1867 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04001868 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001869
Alex Deucher5df31962012-09-13 11:52:08 -04001870 if (radeon_encoder->active_device &
1871 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1872 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001873
1874 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001875
Alex Deucher54bfe492010-09-03 15:52:53 -04001876 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001877 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001878 else if (ASIC_IS_AVIVO(rdev)) {
1879 if (is_tvcv)
1880 atombios_crtc_set_timing(crtc, adjusted_mode);
1881 else
1882 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1883 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001884 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001885 if (radeon_crtc->crtc_id == 0)
1886 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001887 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001888 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001889 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001890 atombios_overscan_setup(crtc, mode, adjusted_mode);
1891 atombios_scaler_setup(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04001892 /* update the hw version fpr dpm */
1893 radeon_crtc->hw_mode = *adjusted_mode;
1894
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001895 return 0;
1896}
1897
1898static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001899 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001900 struct drm_display_mode *adjusted_mode)
1901{
Alex Deucher5df31962012-09-13 11:52:08 -04001902 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1903 struct drm_device *dev = crtc->dev;
1904 struct drm_encoder *encoder;
1905
1906 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
1907 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1908 if (encoder->crtc == crtc) {
1909 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04001910 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04001911 break;
1912 }
1913 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001914 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
1915 radeon_crtc->encoder = NULL;
1916 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04001917 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04001918 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001919 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1920 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001921 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1922 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04001923 /* pick pll */
1924 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1925 /* if we can't get a PPLL for a non-DP encoder, fail */
1926 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
1927 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
1928 return false;
1929
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001930 return true;
1931}
1932
1933static void atombios_crtc_prepare(struct drm_crtc *crtc)
1934{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001935 struct drm_device *dev = crtc->dev;
1936 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001937
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001938 /* disable crtc pair power gating before programming */
1939 if (ASIC_IS_DCE6(rdev))
1940 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1941
Alex Deucher37b43902010-02-09 12:04:43 -05001942 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001943 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001944}
1945
1946static void atombios_crtc_commit(struct drm_crtc *crtc)
1947{
1948 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001949 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001950}
1951
Alex Deucher37f90032010-06-11 17:58:38 -04001952static void atombios_crtc_disable(struct drm_crtc *crtc)
1953{
1954 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04001955 struct drm_device *dev = crtc->dev;
1956 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001957 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04001958 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001959
Alex Deucher37f90032010-06-11 17:58:38 -04001960 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04001961 if (crtc->fb) {
1962 int r;
1963 struct radeon_framebuffer *radeon_fb;
1964 struct radeon_bo *rbo;
1965
1966 radeon_fb = to_radeon_framebuffer(crtc->fb);
1967 rbo = gem_to_radeon_bo(radeon_fb->obj);
1968 r = radeon_bo_reserve(rbo, false);
1969 if (unlikely(r))
1970 DRM_ERROR("failed to reserve rbo before unpin\n");
1971 else {
1972 radeon_bo_unpin(rbo);
1973 radeon_bo_unreserve(rbo);
1974 }
1975 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04001976 /* disable the GRPH */
1977 if (ASIC_IS_DCE4(rdev))
1978 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
1979 else if (ASIC_IS_AVIVO(rdev))
1980 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
1981
Alex Deucher0e3d50b2013-02-05 11:47:09 -05001982 if (ASIC_IS_DCE6(rdev))
1983 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04001984
Alex Deucher4e585912012-08-21 19:06:21 -04001985 for (i = 0; i < rdev->num_crtc; i++) {
1986 if (rdev->mode_info.crtcs[i] &&
1987 rdev->mode_info.crtcs[i]->enabled &&
1988 i != radeon_crtc->crtc_id &&
1989 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
1990 /* one other crtc is using this pll don't turn
1991 * off the pll
1992 */
1993 goto done;
1994 }
1995 }
1996
Alex Deucher37f90032010-06-11 17:58:38 -04001997 switch (radeon_crtc->pll_id) {
1998 case ATOM_PPLL1:
1999 case ATOM_PPLL2:
2000 /* disable the ppll */
2001 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002002 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002003 break;
Alex Deucher64199872012-03-20 17:18:33 -04002004 case ATOM_PPLL0:
2005 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002006 if ((rdev->family == CHIP_ARUBA) ||
2007 (rdev->family == CHIP_BONAIRE) ||
2008 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002009 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2010 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2011 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002012 default:
2013 break;
2014 }
Alex Deucher4e585912012-08-21 19:06:21 -04002015done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002016 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002017 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002018 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002019 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002020}
2021
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002022static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2023 .dpms = atombios_crtc_dpms,
2024 .mode_fixup = atombios_crtc_mode_fixup,
2025 .mode_set = atombios_crtc_mode_set,
2026 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002027 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002028 .prepare = atombios_crtc_prepare,
2029 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002030 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002031 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002032};
2033
2034void radeon_atombios_init_crtc(struct drm_device *dev,
2035 struct radeon_crtc *radeon_crtc)
2036{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002037 struct radeon_device *rdev = dev->dev_private;
2038
2039 if (ASIC_IS_DCE4(rdev)) {
2040 switch (radeon_crtc->crtc_id) {
2041 case 0:
2042 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002043 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002044 break;
2045 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002046 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002047 break;
2048 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002049 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002050 break;
2051 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002052 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002053 break;
2054 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002055 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002056 break;
2057 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002058 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002059 break;
2060 }
2061 } else {
2062 if (radeon_crtc->crtc_id == 1)
2063 radeon_crtc->crtc_offset =
2064 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2065 else
2066 radeon_crtc->crtc_offset = 0;
2067 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002068 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002069 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002070 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002071 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002072 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2073}