blob: e911898348f87d673b904ae09707faa0ea0ce849 [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;
Alex Deucher37b43902010-02-09 12:04:43 -0500273 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400274 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500275 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
276 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400277 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500278 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279 break;
280 case DRM_MODE_DPMS_STANDBY:
281 case DRM_MODE_DPMS_SUSPEND:
282 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400283 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500284 if (radeon_crtc->enabled)
285 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400286 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500287 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
288 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400289 radeon_crtc->enabled = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290 break;
291 }
Alex Deucher3640da22014-05-30 12:40:15 -0400292 /* adjust pm to dpms */
293 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294}
295
296static void
297atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400298 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200299{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400300 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301 struct drm_device *dev = crtc->dev;
302 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400303 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200304 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400305 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200306
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400307 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400308 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400309 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400310 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
311 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400312 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400313 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400314 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400315 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316 args.usH_SyncWidth =
317 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
318 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400319 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400320 args.usV_SyncWidth =
321 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400322 args.ucH_Border = radeon_crtc->h_border;
323 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400324
325 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
326 misc |= ATOM_VSYNC_POLARITY;
327 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
328 misc |= ATOM_HSYNC_POLARITY;
329 if (mode->flags & DRM_MODE_FLAG_CSYNC)
330 misc |= ATOM_COMPOSITESYNC;
331 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
332 misc |= ATOM_INTERLACE;
333 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
334 misc |= ATOM_DOUBLE_CLOCK_MODE;
335
336 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
337 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400339 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200340}
341
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400342static void atombios_crtc_set_timing(struct drm_crtc *crtc,
343 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200344{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400345 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346 struct drm_device *dev = crtc->dev;
347 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400348 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200349 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400350 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400352 memset(&args, 0, sizeof(args));
353 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
354 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
355 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
356 args.usH_SyncWidth =
357 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
358 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
359 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
360 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
361 args.usV_SyncWidth =
362 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
363
Alex Deucher54bfe492010-09-03 15:52:53 -0400364 args.ucOverscanRight = radeon_crtc->h_border;
365 args.ucOverscanLeft = radeon_crtc->h_border;
366 args.ucOverscanBottom = radeon_crtc->v_border;
367 args.ucOverscanTop = radeon_crtc->v_border;
368
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400369 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
370 misc |= ATOM_VSYNC_POLARITY;
371 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
372 misc |= ATOM_HSYNC_POLARITY;
373 if (mode->flags & DRM_MODE_FLAG_CSYNC)
374 misc |= ATOM_COMPOSITESYNC;
375 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
376 misc |= ATOM_INTERLACE;
377 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
378 misc |= ATOM_DOUBLE_CLOCK_MODE;
379
380 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
381 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400383 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384}
385
Alex Deucher3fa47d92012-01-20 14:56:39 -0500386static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500387{
Alex Deucherb7922102010-03-06 10:57:30 -0500388 u32 ss_cntl;
389
390 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500391 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500392 case ATOM_PPLL1:
393 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
394 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
395 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
396 break;
397 case ATOM_PPLL2:
398 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
399 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
400 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
401 break;
402 case ATOM_DCPLL:
403 case ATOM_PPLL_INVALID:
404 return;
405 }
406 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500407 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500408 case ATOM_PPLL1:
409 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
410 ss_cntl &= ~1;
411 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
412 break;
413 case ATOM_PPLL2:
414 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
415 ss_cntl &= ~1;
416 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
417 break;
418 case ATOM_DCPLL:
419 case ATOM_PPLL_INVALID:
420 return;
421 }
422 }
423}
424
425
Alex Deucher26b9fc32010-02-01 16:39:11 -0500426union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400427 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
428 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500429 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400430 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500431 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500432};
433
Alex Deucher3fa47d92012-01-20 14:56:39 -0500434static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400435 int enable,
436 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400437 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400438 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400439{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400440 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400441 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500442 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400443
Alex Deucherc4756ba2014-01-15 13:59:47 -0500444 if (enable) {
445 /* Don't mess with SS if percentage is 0 or external ss.
446 * SS is already disabled previously, and disabling it
447 * again can cause display problems if the pll is already
448 * programmed.
449 */
450 if (ss->percentage == 0)
451 return;
452 if (ss->type & ATOM_EXTERNAL_SS_MASK)
453 return;
454 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400455 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400456 if (rdev->mode_info.crtcs[i] &&
457 rdev->mode_info.crtcs[i]->enabled &&
458 i != crtc_id &&
459 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
460 /* one other crtc is using this pll don't turn
461 * off spread spectrum as it might turn off
462 * display on active crtc
463 */
464 return;
465 }
466 }
467 }
468
Alex Deucher26b9fc32010-02-01 16:39:11 -0500469 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400470
Alex Deuchera572eaa2011-01-06 21:19:16 -0500471 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500472 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400473 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500474 switch (pll_id) {
475 case ATOM_PPLL1:
476 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500477 break;
478 case ATOM_PPLL2:
479 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500480 break;
481 case ATOM_DCPLL:
482 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500483 break;
484 case ATOM_PPLL_INVALID:
485 return;
486 }
Alex Deucherf312f092012-07-17 14:02:44 -0400487 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
488 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400489 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500490 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400491 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400492 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400493 switch (pll_id) {
494 case ATOM_PPLL1:
495 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400496 break;
497 case ATOM_PPLL2:
498 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400499 break;
500 case ATOM_DCPLL:
501 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400502 break;
503 case ATOM_PPLL_INVALID:
504 return;
505 }
Alex Deucherf312f092012-07-17 14:02:44 -0400506 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
507 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400508 args.v2.ucEnable = enable;
509 } else if (ASIC_IS_DCE3(rdev)) {
510 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400511 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400512 args.v1.ucSpreadSpectrumStep = ss->step;
513 args.v1.ucSpreadSpectrumDelay = ss->delay;
514 args.v1.ucSpreadSpectrumRange = ss->range;
515 args.v1.ucPpll = pll_id;
516 args.v1.ucEnable = enable;
517 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400518 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
519 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500520 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400521 return;
522 }
523 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400524 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400525 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
526 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
527 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
528 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400529 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500530 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500531 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400532 return;
533 }
534 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400535 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400536 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
537 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
538 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400539 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500540 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400541}
542
Alex Deucher4eaeca32010-01-19 17:32:27 -0500543union adjust_pixel_clock {
544 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500545 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500546};
547
548static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400549 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200550{
Alex Deucher19eca432012-09-13 10:56:16 -0400551 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 struct drm_device *dev = crtc->dev;
553 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400554 struct drm_encoder *encoder = radeon_crtc->encoder;
555 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
556 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500557 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400558 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400559 u32 dp_clock = mode->clock;
Alex Deucher7d5a33b2014-02-03 15:53:25 -0500560 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400561 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500562
Alex Deucher4eaeca32010-01-19 17:32:27 -0500563 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400564 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200565
566 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400567 if ((rdev->family == CHIP_RS600) ||
568 (rdev->family == CHIP_RS690) ||
569 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400570 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
571 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000572
573 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400574 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000575 else
Alex Deucher19eca432012-09-13 10:56:16 -0400576 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400577
Alex Deucher5785e532011-04-19 15:24:59 -0400578 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400579 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400580 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500581 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400582 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400583 /* use frac fb div on RS780/RS880 */
584 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
585 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500586 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
587 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000588 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400589 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200590
Dave Airlie5480f722010-10-19 10:36:47 +1000591 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400592 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000593 else
Alex Deucher19eca432012-09-13 10:56:16 -0400594 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000595 }
596
Alex Deucher5df31962012-09-13 11:52:08 -0400597 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
598 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
599 if (connector) {
600 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
601 struct radeon_connector_atom_dig *dig_connector =
602 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400603
Alex Deucher5df31962012-09-13 11:52:08 -0400604 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200605 }
606 }
607
Alex Deucher5df31962012-09-13 11:52:08 -0400608 /* use recommended ref_div for ss */
609 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
610 if (radeon_crtc->ss_enabled) {
611 if (radeon_crtc->ss.refdiv) {
612 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
613 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
614 if (ASIC_IS_AVIVO(rdev))
615 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
616 }
617 }
618 }
619
620 if (ASIC_IS_AVIVO(rdev)) {
621 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
622 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
623 adjusted_clock = mode->clock * 2;
624 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
625 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
626 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
627 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
628 } else {
629 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
630 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
631 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
632 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
633 }
634
Alex Deucher2606c882009-10-08 13:36:21 -0400635 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
636 * accordingly based on the encoder/transmitter to work around
637 * special hw requirements.
638 */
639 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500640 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500641 u8 frev, crev;
642 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400643
Alex Deucher2606c882009-10-08 13:36:21 -0400644 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400645 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
646 &crev))
647 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500648
649 memset(&args, 0, sizeof(args));
650
651 switch (frev) {
652 case 1:
653 switch (crev) {
654 case 1:
655 case 2:
656 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
657 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500658 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400659 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400660 args.v1.ucConfig |=
661 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500662
663 atom_execute_table(rdev->mode_info.atom_context,
664 index, (uint32_t *)&args);
665 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
666 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500667 case 3:
668 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
669 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
670 args.v3.sInput.ucEncodeMode = encoder_mode;
671 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400672 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000673 args.v3.sInput.ucDispPllConfig |=
674 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400675 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400676 args.v3.sInput.ucDispPllConfig |=
677 DISPPLL_CONFIG_COHERENT_MODE;
678 /* 16200 or 27000 */
679 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
680 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500681 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400682 if (encoder_mode == ATOM_ENCODER_MODE_HDMI)
683 /* deep color support */
684 args.v3.sInput.usPixelClock =
685 cpu_to_le16((mode->clock * bpc / 8) / 10);
686 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500687 args.v3.sInput.ucDispPllConfig |=
688 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500689 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500690 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400691 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500692 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400693 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
694 ENCODER_OBJECT_ID_NONE)
695 args.v3.sInput.ucExtTransmitterID =
696 radeon_encoder_get_dp_bridge_encoder_id(encoder);
697 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400698 args.v3.sInput.ucExtTransmitterID = 0;
699
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500700 atom_execute_table(rdev->mode_info.atom_context,
701 index, (uint32_t *)&args);
702 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
703 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400704 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
705 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
706 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500707 }
708 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400709 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
710 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
711 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500712 }
713 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500714 default:
715 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
716 return adjusted_clock;
717 }
718 break;
719 default:
720 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
721 return adjusted_clock;
722 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400723 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500724 return adjusted_clock;
725}
726
727union set_pixel_clock {
728 SET_PIXEL_CLOCK_PS_ALLOCATION base;
729 PIXEL_CLOCK_PARAMETERS v1;
730 PIXEL_CLOCK_PARAMETERS_V2 v2;
731 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500732 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500733 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500734};
735
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500736/* on DCE5, make sure the voltage is high enough to support the
737 * required disp clk.
738 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400739static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500740 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500741{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500742 u8 frev, crev;
743 int index;
744 union set_pixel_clock args;
745
746 memset(&args, 0, sizeof(args));
747
748 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400749 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
750 &crev))
751 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500752
753 switch (frev) {
754 case 1:
755 switch (crev) {
756 case 5:
757 /* if the default dcpll clock is specified,
758 * SetPixelClock provides the dividers
759 */
760 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500761 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500762 args.v5.ucPpll = ATOM_DCPLL;
763 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500764 case 6:
765 /* if the default dcpll clock is specified,
766 * SetPixelClock provides the dividers
767 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500768 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400769 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400770 args.v6.ucPpll = ATOM_EXT_PLL1;
771 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400772 args.v6.ucPpll = ATOM_PPLL0;
773 else
774 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500775 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500776 default:
777 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
778 return;
779 }
780 break;
781 default:
782 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
783 return;
784 }
785 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
786}
787
Alex Deucher37f90032010-06-11 17:58:38 -0400788static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000789 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400790 int pll_id,
791 u32 encoder_mode,
792 u32 encoder_id,
793 u32 clock,
794 u32 ref_div,
795 u32 fb_div,
796 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400797 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400798 int bpc,
799 bool ss_enabled,
800 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400801{
802 struct drm_device *dev = crtc->dev;
803 struct radeon_device *rdev = dev->dev_private;
804 u8 frev, crev;
805 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
806 union set_pixel_clock args;
807
808 memset(&args, 0, sizeof(args));
809
810 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
811 &crev))
812 return;
813
814 switch (frev) {
815 case 1:
816 switch (crev) {
817 case 1:
818 if (clock == ATOM_DISABLE)
819 return;
820 args.v1.usPixelClock = cpu_to_le16(clock / 10);
821 args.v1.usRefDiv = cpu_to_le16(ref_div);
822 args.v1.usFbDiv = cpu_to_le16(fb_div);
823 args.v1.ucFracFbDiv = frac_fb_div;
824 args.v1.ucPostDiv = post_div;
825 args.v1.ucPpll = pll_id;
826 args.v1.ucCRTC = crtc_id;
827 args.v1.ucRefDivSrc = 1;
828 break;
829 case 2:
830 args.v2.usPixelClock = cpu_to_le16(clock / 10);
831 args.v2.usRefDiv = cpu_to_le16(ref_div);
832 args.v2.usFbDiv = cpu_to_le16(fb_div);
833 args.v2.ucFracFbDiv = frac_fb_div;
834 args.v2.ucPostDiv = post_div;
835 args.v2.ucPpll = pll_id;
836 args.v2.ucCRTC = crtc_id;
837 args.v2.ucRefDivSrc = 1;
838 break;
839 case 3:
840 args.v3.usPixelClock = cpu_to_le16(clock / 10);
841 args.v3.usRefDiv = cpu_to_le16(ref_div);
842 args.v3.usFbDiv = cpu_to_le16(fb_div);
843 args.v3.ucFracFbDiv = frac_fb_div;
844 args.v3.ucPostDiv = post_div;
845 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400846 if (crtc_id == ATOM_CRTC2)
847 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
848 else
849 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400850 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
851 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400852 args.v3.ucTransmitterId = encoder_id;
853 args.v3.ucEncoderMode = encoder_mode;
854 break;
855 case 5:
856 args.v5.ucCRTC = crtc_id;
857 args.v5.usPixelClock = cpu_to_le16(clock / 10);
858 args.v5.ucRefDiv = ref_div;
859 args.v5.usFbDiv = cpu_to_le16(fb_div);
860 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
861 args.v5.ucPostDiv = post_div;
862 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400863 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
864 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400865 switch (bpc) {
866 case 8:
867 default:
868 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
869 break;
870 case 10:
871 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
872 break;
873 }
Alex Deucher37f90032010-06-11 17:58:38 -0400874 args.v5.ucTransmitterID = encoder_id;
875 args.v5.ucEncoderMode = encoder_mode;
876 args.v5.ucPpll = pll_id;
877 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500878 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000879 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500880 args.v6.ucRefDiv = ref_div;
881 args.v6.usFbDiv = cpu_to_le16(fb_div);
882 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
883 args.v6.ucPostDiv = post_div;
884 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400885 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
886 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400887 switch (bpc) {
888 case 8:
889 default:
890 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
891 break;
892 case 10:
893 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
894 break;
895 case 12:
896 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
897 break;
898 case 16:
899 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
900 break;
901 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500902 args.v6.ucTransmitterID = encoder_id;
903 args.v6.ucEncoderMode = encoder_mode;
904 args.v6.ucPpll = pll_id;
905 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400906 default:
907 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
908 return;
909 }
910 break;
911 default:
912 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
913 return;
914 }
915
916 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
917}
918
Alex Deucher19eca432012-09-13 10:56:16 -0400919static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
920{
921 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
922 struct drm_device *dev = crtc->dev;
923 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400924 struct radeon_encoder *radeon_encoder =
925 to_radeon_encoder(radeon_crtc->encoder);
926 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400927
928 radeon_crtc->bpc = 8;
929 radeon_crtc->ss_enabled = false;
930
Alex Deucher19eca432012-09-13 10:56:16 -0400931 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400932 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400933 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
934 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400935 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400936 struct radeon_connector *radeon_connector =
937 to_radeon_connector(connector);
938 struct radeon_connector_atom_dig *dig_connector =
939 radeon_connector->con_priv;
940 int dp_clock;
941 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
942
943 switch (encoder_mode) {
944 case ATOM_ENCODER_MODE_DP_MST:
945 case ATOM_ENCODER_MODE_DP:
946 /* DP/eDP */
947 dp_clock = dig_connector->dp_clock / 10;
948 if (ASIC_IS_DCE4(rdev))
949 radeon_crtc->ss_enabled =
950 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
951 ASIC_INTERNAL_SS_ON_DP,
952 dp_clock);
953 else {
954 if (dp_clock == 16200) {
955 radeon_crtc->ss_enabled =
956 radeon_atombios_get_ppll_ss_info(rdev,
957 &radeon_crtc->ss,
958 ATOM_DP_SS_ID2);
959 if (!radeon_crtc->ss_enabled)
960 radeon_crtc->ss_enabled =
961 radeon_atombios_get_ppll_ss_info(rdev,
962 &radeon_crtc->ss,
963 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500964 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400965 radeon_crtc->ss_enabled =
966 radeon_atombios_get_ppll_ss_info(rdev,
967 &radeon_crtc->ss,
968 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500969 }
970 /* disable spread spectrum on DCE3 DP */
971 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -0400972 }
973 break;
974 case ATOM_ENCODER_MODE_LVDS:
975 if (ASIC_IS_DCE4(rdev))
976 radeon_crtc->ss_enabled =
977 radeon_atombios_get_asic_ss_info(rdev,
978 &radeon_crtc->ss,
979 dig->lcd_ss_id,
980 mode->clock / 10);
981 else
982 radeon_crtc->ss_enabled =
983 radeon_atombios_get_ppll_ss_info(rdev,
984 &radeon_crtc->ss,
985 dig->lcd_ss_id);
986 break;
987 case ATOM_ENCODER_MODE_DVI:
988 if (ASIC_IS_DCE4(rdev))
989 radeon_crtc->ss_enabled =
990 radeon_atombios_get_asic_ss_info(rdev,
991 &radeon_crtc->ss,
992 ASIC_INTERNAL_SS_ON_TMDS,
993 mode->clock / 10);
994 break;
995 case ATOM_ENCODER_MODE_HDMI:
996 if (ASIC_IS_DCE4(rdev))
997 radeon_crtc->ss_enabled =
998 radeon_atombios_get_asic_ss_info(rdev,
999 &radeon_crtc->ss,
1000 ASIC_INTERNAL_SS_ON_HDMI,
1001 mode->clock / 10);
1002 break;
1003 default:
1004 break;
1005 }
1006 }
1007
1008 /* adjust pixel clock as needed */
1009 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1010
1011 return true;
1012}
1013
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001014static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001015{
1016 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1017 struct drm_device *dev = crtc->dev;
1018 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001019 struct radeon_encoder *radeon_encoder =
1020 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001021 u32 pll_clock = mode->clock;
1022 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1023 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001024 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001025
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001026 switch (radeon_crtc->pll_id) {
1027 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001028 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001029 break;
1030 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001031 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001032 break;
1033 case ATOM_DCPLL:
1034 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001035 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001036 pll = &rdev->clock.dcpll;
1037 break;
1038 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001039
Alex Deucher19eca432012-09-13 10:56:16 -04001040 /* update pll params */
1041 pll->flags = radeon_crtc->pll_flags;
1042 pll->reference_div = radeon_crtc->pll_reference_div;
1043 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001044
Alex Deucher64146f82011-03-22 01:46:12 -04001045 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1046 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001047 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1048 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001049 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001050 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1051 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001052 else
Alex Deucher19eca432012-09-13 10:56:16 -04001053 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1054 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001055
Alex Deucher19eca432012-09-13 10:56:16 -04001056 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1057 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001058
Alex Deucher37f90032010-06-11 17:58:38 -04001059 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1060 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001061 ref_div, fb_div, frac_fb_div, post_div,
1062 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001063
Alex Deucher19eca432012-09-13 10:56:16 -04001064 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001065 /* calculate ss amount and step size */
1066 if (ASIC_IS_DCE4(rdev)) {
1067 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001068 u32 amount = (((fb_div * 10) + frac_fb_div) *
1069 (u32)radeon_crtc->ss.percentage) /
1070 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001071 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1072 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001073 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001074 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001075 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001076 (125 * 25 * pll->reference_freq / 100);
1077 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001078 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001079 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001080 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001081 }
1082
Alex Deucher19eca432012-09-13 10:56:16 -04001083 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1084 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001085 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001086}
1087
Alex Deucherc9417bd2011-02-06 14:23:26 -05001088static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1089 struct drm_framebuffer *fb,
1090 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001091{
1092 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1093 struct drm_device *dev = crtc->dev;
1094 struct radeon_device *rdev = dev->dev_private;
1095 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001096 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001097 struct drm_gem_object *obj;
1098 struct radeon_bo *rbo;
1099 uint64_t fb_location;
1100 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001101 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001102 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001103 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001104 int r;
1105
1106 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001107 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001108 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001109 return 0;
1110 }
1111
Chris Ball4dd19b02010-09-26 06:47:23 -05001112 if (atomic) {
1113 radeon_fb = to_radeon_framebuffer(fb);
1114 target_fb = fb;
1115 }
1116 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001117 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1118 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001119 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001120
Chris Ball4dd19b02010-09-26 06:47:23 -05001121 /* If atomic, assume fb object is pinned & idle & fenced and
1122 * just update base pointers
1123 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001124 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001125 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001126 r = radeon_bo_reserve(rbo, false);
1127 if (unlikely(r != 0))
1128 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001129
1130 if (atomic)
1131 fb_location = radeon_bo_gpu_offset(rbo);
1132 else {
1133 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1134 if (unlikely(r != 0)) {
1135 radeon_bo_unreserve(rbo);
1136 return -EINVAL;
1137 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001138 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001139
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001140 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1141 radeon_bo_unreserve(rbo);
1142
Chris Ball4dd19b02010-09-26 06:47:23 -05001143 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001144 case 8:
1145 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1146 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1147 break;
1148 case 15:
1149 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1150 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1151 break;
1152 case 16:
1153 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1154 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001155#ifdef __BIG_ENDIAN
1156 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1157#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001158 break;
1159 case 24:
1160 case 32:
1161 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1162 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001163#ifdef __BIG_ENDIAN
1164 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1165#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001166 break;
1167 default:
1168 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001169 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001170 return -EINVAL;
1171 }
1172
Alex Deucher392e3722011-11-28 14:49:27 -05001173 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001174 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001175
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001176 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001177 if (rdev->family >= CHIP_TAHITI) {
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001178 unsigned index, num_banks;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001179
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001180 if (rdev->family >= CHIP_BONAIRE) {
1181 unsigned tileb, tile_split_bytes;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001182
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001183 /* Calculate the macrotile mode index. */
1184 tile_split_bytes = 64 << tile_split;
1185 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1186 tileb = min(tile_split_bytes, tileb);
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001187
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001188 for (index = 0; tileb > 64; index++)
1189 tileb >>= 1;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001190
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001191 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 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001198 } else {
1199 switch (target_fb->bits_per_pixel) {
1200 case 8:
1201 index = 10;
1202 break;
1203 case 16:
1204 index = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
1205 break;
1206 default:
1207 case 32:
1208 index = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
1209 break;
1210 }
1211
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001212 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001213 }
1214
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001215 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1216 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001217 /* NI and older. */
1218 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001219 tmp = rdev->config.cayman.tile_config;
1220 else
1221 tmp = rdev->config.evergreen.tile_config;
1222
1223 switch ((tmp & 0xf0) >> 4) {
1224 case 0: /* 4 banks */
1225 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1226 break;
1227 case 1: /* 8 banks */
1228 default:
1229 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1230 break;
1231 case 2: /* 16 banks */
1232 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1233 break;
1234 }
Alex Deucher392e3722011-11-28 14:49:27 -05001235 }
1236
Alex Deucher97d66322010-05-20 12:12:48 -04001237 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001238 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1239 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1240 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1241 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001242 if (rdev->family >= CHIP_BONAIRE) {
1243 /* XXX need to know more about the surface tiling mode */
1244 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1245 }
Alex Deucher392e3722011-11-28 14:49:27 -05001246 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001247 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1248
Alex Deucher8da0e502012-07-11 18:38:29 -04001249 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001250 /* Read the pipe config from the 2D TILED SCANOUT mode.
1251 * It should be the same for the other modes too, but not all
1252 * modes set the pipe config field. */
1253 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1254
1255 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001256 } else if ((rdev->family == CHIP_TAHITI) ||
1257 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001258 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001259 else if ((rdev->family == CHIP_VERDE) ||
1260 (rdev->family == CHIP_OLAND) ||
1261 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001262 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1263
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001264 switch (radeon_crtc->crtc_id) {
1265 case 0:
1266 WREG32(AVIVO_D1VGA_CONTROL, 0);
1267 break;
1268 case 1:
1269 WREG32(AVIVO_D2VGA_CONTROL, 0);
1270 break;
1271 case 2:
1272 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1273 break;
1274 case 3:
1275 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1276 break;
1277 case 4:
1278 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1279 break;
1280 case 5:
1281 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1282 break;
1283 default:
1284 break;
1285 }
1286
1287 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1288 upper_32_bits(fb_location));
1289 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1290 upper_32_bits(fb_location));
1291 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1292 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1293 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1294 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1295 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001296 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001297
1298 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1299 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1300 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1301 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001302 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1303 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001304
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001305 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001306 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1307 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1308
Alex Deucher8da0e502012-07-11 18:38:29 -04001309 if (rdev->family >= CHIP_BONAIRE)
1310 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1311 target_fb->height);
1312 else
1313 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1314 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001315 x &= ~3;
1316 y &= ~1;
1317 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1318 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001319 viewport_w = crtc->mode.hdisplay;
1320 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001321 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001322 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001323
Alex Deucherfb9674b2011-04-02 09:15:50 -04001324 /* pageflip setup */
1325 /* make sure flip is at vb rather than hb */
1326 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1327 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1328 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1329
1330 /* set pageflip to happen anywhere in vblank interval */
1331 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1332
Matt Roperf4510a22014-04-01 15:22:40 -07001333 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001334 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001335 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001336 r = radeon_bo_reserve(rbo, false);
1337 if (unlikely(r != 0))
1338 return r;
1339 radeon_bo_unpin(rbo);
1340 radeon_bo_unreserve(rbo);
1341 }
1342
1343 /* Bytes per pixel may have changed */
1344 radeon_bandwidth_update(rdev);
1345
1346 return 0;
1347}
1348
Chris Ball4dd19b02010-09-26 06:47:23 -05001349static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1350 struct drm_framebuffer *fb,
1351 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001352{
1353 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1354 struct drm_device *dev = crtc->dev;
1355 struct radeon_device *rdev = dev->dev_private;
1356 struct radeon_framebuffer *radeon_fb;
1357 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001358 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001359 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001360 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001361 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001362 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001363 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001364 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001365
Jerome Glisse2de3b482009-11-17 14:08:55 -08001366 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001367 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001368 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001369 return 0;
1370 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001371
Chris Ball4dd19b02010-09-26 06:47:23 -05001372 if (atomic) {
1373 radeon_fb = to_radeon_framebuffer(fb);
1374 target_fb = fb;
1375 }
1376 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001377 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1378 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001379 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001380
1381 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001382 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001383 r = radeon_bo_reserve(rbo, false);
1384 if (unlikely(r != 0))
1385 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001386
1387 /* If atomic, assume fb object is pinned & idle & fenced and
1388 * just update base pointers
1389 */
1390 if (atomic)
1391 fb_location = radeon_bo_gpu_offset(rbo);
1392 else {
1393 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1394 if (unlikely(r != 0)) {
1395 radeon_bo_unreserve(rbo);
1396 return -EINVAL;
1397 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001398 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001399 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1400 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001401
Chris Ball4dd19b02010-09-26 06:47:23 -05001402 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001403 case 8:
1404 fb_format =
1405 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1406 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1407 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001408 case 15:
1409 fb_format =
1410 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1411 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1412 break;
1413 case 16:
1414 fb_format =
1415 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1416 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001417#ifdef __BIG_ENDIAN
1418 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1419#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001420 break;
1421 case 24:
1422 case 32:
1423 fb_format =
1424 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1425 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001426#ifdef __BIG_ENDIAN
1427 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1428#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001429 break;
1430 default:
1431 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001432 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001433 return -EINVAL;
1434 }
1435
Alex Deucher40c4ac12010-05-20 12:04:59 -04001436 if (rdev->family >= CHIP_R600) {
1437 if (tiling_flags & RADEON_TILING_MACRO)
1438 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1439 else if (tiling_flags & RADEON_TILING_MICRO)
1440 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1441 } else {
1442 if (tiling_flags & RADEON_TILING_MACRO)
1443 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001444
Alex Deucher40c4ac12010-05-20 12:04:59 -04001445 if (tiling_flags & RADEON_TILING_MICRO)
1446 fb_format |= AVIVO_D1GRPH_TILED;
1447 }
Dave Airliee024e112009-06-24 09:48:08 +10001448
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001449 if (radeon_crtc->crtc_id == 0)
1450 WREG32(AVIVO_D1VGA_CONTROL, 0);
1451 else
1452 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001453
1454 if (rdev->family >= CHIP_RV770) {
1455 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001456 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1457 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001458 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001459 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1460 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001461 }
1462 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001463 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1464 (u32) fb_location);
1465 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1466 radeon_crtc->crtc_offset, (u32) fb_location);
1467 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001468 if (rdev->family >= CHIP_R600)
1469 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001470
1471 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1472 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1473 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1474 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001475 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1476 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001477
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001478 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001479 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1480 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1481
1482 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001483 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001484 x &= ~3;
1485 y &= ~1;
1486 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1487 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001488 viewport_w = crtc->mode.hdisplay;
1489 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001490 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001491 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001492
Alex Deucherfb9674b2011-04-02 09:15:50 -04001493 /* pageflip setup */
1494 /* make sure flip is at vb rather than hb */
1495 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1496 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1497 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1498
1499 /* set pageflip to happen anywhere in vblank interval */
1500 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1501
Matt Roperf4510a22014-04-01 15:22:40 -07001502 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001503 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001504 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001505 r = radeon_bo_reserve(rbo, false);
1506 if (unlikely(r != 0))
1507 return r;
1508 radeon_bo_unpin(rbo);
1509 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001510 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001511
1512 /* Bytes per pixel may have changed */
1513 radeon_bandwidth_update(rdev);
1514
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001515 return 0;
1516}
1517
Alex Deucher54f088a2010-01-19 16:34:01 -05001518int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1519 struct drm_framebuffer *old_fb)
1520{
1521 struct drm_device *dev = crtc->dev;
1522 struct radeon_device *rdev = dev->dev_private;
1523
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001524 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001525 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001526 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001527 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001528 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001529 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1530}
1531
1532int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1533 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001534 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001535{
1536 struct drm_device *dev = crtc->dev;
1537 struct radeon_device *rdev = dev->dev_private;
1538
1539 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001540 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001541 else if (ASIC_IS_AVIVO(rdev))
1542 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1543 else
1544 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001545}
1546
Alex Deucher615e0cb2010-01-20 16:22:53 -05001547/* properly set additional regs when using atombios */
1548static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1549{
1550 struct drm_device *dev = crtc->dev;
1551 struct radeon_device *rdev = dev->dev_private;
1552 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1553 u32 disp_merge_cntl;
1554
1555 switch (radeon_crtc->crtc_id) {
1556 case 0:
1557 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1558 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1559 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1560 break;
1561 case 1:
1562 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1563 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1564 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1565 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1566 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1567 break;
1568 }
1569}
1570
Alex Deucherf3dd8502012-08-31 11:56:50 -04001571/**
1572 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1573 *
1574 * @crtc: drm crtc
1575 *
1576 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1577 */
1578static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1579{
1580 struct drm_device *dev = crtc->dev;
1581 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001582 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001583 u32 pll_in_use = 0;
1584
1585 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1586 if (crtc == test_crtc)
1587 continue;
1588
Alex Deucher57b35e22012-09-17 17:34:45 -04001589 test_radeon_crtc = to_radeon_crtc(test_crtc);
1590 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1591 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001592 }
1593 return pll_in_use;
1594}
1595
1596/**
1597 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1598 *
1599 * @crtc: drm crtc
1600 *
1601 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1602 * also in DP mode. For DP, a single PPLL can be used for all DP
1603 * crtcs/encoders.
1604 */
1605static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1606{
1607 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001608 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001609 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001610
Alex Deucher57b35e22012-09-17 17:34:45 -04001611 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1612 if (crtc == test_crtc)
1613 continue;
1614 test_radeon_crtc = to_radeon_crtc(test_crtc);
1615 if (test_radeon_crtc->encoder &&
1616 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1617 /* for DP use the same PLL for all */
1618 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1619 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001620 }
1621 }
1622 return ATOM_PPLL_INVALID;
1623}
1624
1625/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001626 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1627 *
1628 * @crtc: drm crtc
1629 * @encoder: drm encoder
1630 *
1631 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1632 * be shared (i.e., same clock).
1633 */
Alex Deucher5df31962012-09-13 11:52:08 -04001634static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001635{
Alex Deucher5df31962012-09-13 11:52:08 -04001636 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001637 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001638 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001639 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001640 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001641
Alex Deucher9642ac02012-09-13 12:43:41 -04001642 adjusted_clock = radeon_crtc->adjusted_clock;
1643
1644 if (adjusted_clock == 0)
1645 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001646
Alex Deucher57b35e22012-09-17 17:34:45 -04001647 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1648 if (crtc == test_crtc)
1649 continue;
1650 test_radeon_crtc = to_radeon_crtc(test_crtc);
1651 if (test_radeon_crtc->encoder &&
1652 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1653 /* check if we are already driving this connector with another crtc */
1654 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1655 /* if we are, return that pll */
1656 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001657 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001658 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001659 /* for non-DP check the clock */
1660 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1661 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1662 (adjusted_clock == test_adjusted_clock) &&
1663 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1664 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1665 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001666 }
1667 }
1668 return ATOM_PPLL_INVALID;
1669}
1670
1671/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001672 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1673 *
1674 * @crtc: drm crtc
1675 *
1676 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1677 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1678 * monitors a dedicated PPLL must be used. If a particular board has
1679 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1680 * as there is no need to program the PLL itself. If we are not able to
1681 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1682 * avoid messing up an existing monitor.
1683 *
1684 * Asic specific PLL information
1685 *
Alex Deucher0331f672012-09-14 11:57:21 -04001686 * DCE 8.x
1687 * KB/KV
1688 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1689 * CI
1690 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1691 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001692 * DCE 6.1
1693 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1694 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1695 *
1696 * DCE 6.0
1697 * - PPLL0 is available to all UNIPHY (DP only)
1698 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1699 *
1700 * DCE 5.0
1701 * - DCPLL is available to all UNIPHY (DP only)
1702 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1703 *
1704 * DCE 3.0/4.0/4.1
1705 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1706 *
1707 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001708static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1709{
Alex Deucher5df31962012-09-13 11:52:08 -04001710 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001711 struct drm_device *dev = crtc->dev;
1712 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001713 struct radeon_encoder *radeon_encoder =
1714 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001715 u32 pll_in_use;
1716 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001717
Alex Deucher0331f672012-09-14 11:57:21 -04001718 if (ASIC_IS_DCE8(rdev)) {
1719 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1720 if (rdev->clock.dp_extclk)
1721 /* skip PPLL programming if using ext clock */
1722 return ATOM_PPLL_INVALID;
1723 else {
1724 /* use the same PPLL for all DP monitors */
1725 pll = radeon_get_shared_dp_ppll(crtc);
1726 if (pll != ATOM_PPLL_INVALID)
1727 return pll;
1728 }
1729 } else {
1730 /* use the same PPLL for all monitors with the same clock */
1731 pll = radeon_get_shared_nondp_ppll(crtc);
1732 if (pll != ATOM_PPLL_INVALID)
1733 return pll;
1734 }
1735 /* otherwise, pick one of the plls */
1736 if ((rdev->family == CHIP_KAVERI) ||
Samuel Lib214f2a2014-04-30 18:40:53 -04001737 (rdev->family == CHIP_KABINI) ||
1738 (rdev->family == CHIP_MULLINS)) {
1739 /* KB/KV/ML has PPLL1 and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001740 pll_in_use = radeon_get_pll_use_mask(crtc);
1741 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1742 return ATOM_PPLL2;
1743 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1744 return ATOM_PPLL1;
1745 DRM_ERROR("unable to allocate a PPLL\n");
1746 return ATOM_PPLL_INVALID;
1747 } else {
1748 /* CI has PPLL0, PPLL1, and PPLL2 */
1749 pll_in_use = radeon_get_pll_use_mask(crtc);
1750 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1751 return ATOM_PPLL2;
1752 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1753 return ATOM_PPLL1;
1754 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1755 return ATOM_PPLL0;
1756 DRM_ERROR("unable to allocate a PPLL\n");
1757 return ATOM_PPLL_INVALID;
1758 }
1759 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001760 struct radeon_encoder_atom_dig *dig =
1761 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001762
Alex Deucher5df31962012-09-13 11:52:08 -04001763 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1764 (dig->linkb == false))
1765 /* UNIPHY A uses PPLL2 */
1766 return ATOM_PPLL2;
1767 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1768 /* UNIPHY B/C/D/E/F */
1769 if (rdev->clock.dp_extclk)
1770 /* skip PPLL programming if using ext clock */
1771 return ATOM_PPLL_INVALID;
1772 else {
1773 /* use the same PPLL for all DP monitors */
1774 pll = radeon_get_shared_dp_ppll(crtc);
1775 if (pll != ATOM_PPLL_INVALID)
1776 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001777 }
Alex Deucher5df31962012-09-13 11:52:08 -04001778 } else {
1779 /* use the same PPLL for all monitors with the same clock */
1780 pll = radeon_get_shared_nondp_ppll(crtc);
1781 if (pll != ATOM_PPLL_INVALID)
1782 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001783 }
1784 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001785 pll_in_use = radeon_get_pll_use_mask(crtc);
1786 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001787 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001788 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1789 return ATOM_PPLL1;
1790 DRM_ERROR("unable to allocate a PPLL\n");
1791 return ATOM_PPLL_INVALID;
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001792 } else if (ASIC_IS_DCE41(rdev)) {
1793 /* Don't share PLLs on DCE4.1 chips */
1794 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1795 if (rdev->clock.dp_extclk)
1796 /* skip PPLL programming if using ext clock */
1797 return ATOM_PPLL_INVALID;
1798 }
1799 pll_in_use = radeon_get_pll_use_mask(crtc);
1800 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1801 return ATOM_PPLL1;
1802 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1803 return ATOM_PPLL2;
1804 DRM_ERROR("unable to allocate a PPLL\n");
1805 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001806 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001807 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1808 * depending on the asic:
1809 * DCE4: PPLL or ext clock
1810 * DCE5: PPLL, DCPLL, or ext clock
1811 * DCE6: PPLL, PPLL0, or ext clock
1812 *
1813 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1814 * PPLL/DCPLL programming and only program the DP DTO for the
1815 * crtc virtual pixel clock.
1816 */
1817 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1818 if (rdev->clock.dp_extclk)
1819 /* skip PPLL programming if using ext clock */
1820 return ATOM_PPLL_INVALID;
1821 else if (ASIC_IS_DCE6(rdev))
1822 /* use PPLL0 for all DP */
1823 return ATOM_PPLL0;
1824 else if (ASIC_IS_DCE5(rdev))
1825 /* use DCPLL for all DP */
1826 return ATOM_DCPLL;
1827 else {
1828 /* use the same PPLL for all DP monitors */
1829 pll = radeon_get_shared_dp_ppll(crtc);
1830 if (pll != ATOM_PPLL_INVALID)
1831 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001832 }
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001833 } else {
Alex Deucher5df31962012-09-13 11:52:08 -04001834 /* use the same PPLL for all monitors with the same clock */
1835 pll = radeon_get_shared_nondp_ppll(crtc);
1836 if (pll != ATOM_PPLL_INVALID)
1837 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001838 }
1839 /* all other cases */
1840 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001841 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1842 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001843 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1844 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001845 DRM_ERROR("unable to allocate a PPLL\n");
1846 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001847 } else {
1848 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001849 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1850 * the matching btw pll and crtc is done through
1851 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1852 * pll (1 or 2) to select which register to write. ie if using
1853 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1854 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1855 * choose which value to write. Which is reverse order from
1856 * register logic. So only case that works is when pllid is
1857 * same as crtcid or when both pll and crtc are enabled and
1858 * both use same clock.
1859 *
1860 * So just return crtc id as if crtc and pll were hard linked
1861 * together even if they aren't
1862 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001863 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001864 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001865}
1866
Alex Deucherf3f1f032012-03-20 17:18:04 -04001867void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001868{
1869 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001870 if (ASIC_IS_DCE6(rdev))
1871 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1872 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001873 struct radeon_atom_ss ss;
1874 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1875 ASIC_INTERNAL_SS_ON_DCPLL,
1876 rdev->clock.default_dispclk);
1877 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001878 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001879 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001880 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001881 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001882 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001883 }
1884
1885}
1886
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001887int atombios_crtc_mode_set(struct drm_crtc *crtc,
1888 struct drm_display_mode *mode,
1889 struct drm_display_mode *adjusted_mode,
1890 int x, int y, struct drm_framebuffer *old_fb)
1891{
1892 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1893 struct drm_device *dev = crtc->dev;
1894 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001895 struct radeon_encoder *radeon_encoder =
1896 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04001897 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001898
Alex Deucher5df31962012-09-13 11:52:08 -04001899 if (radeon_encoder->active_device &
1900 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1901 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001902
Christian Königcde10122014-05-02 14:27:42 +02001903 if (!radeon_crtc->adjusted_clock)
1904 return -EINVAL;
1905
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001906 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001907
Alex Deucher54bfe492010-09-03 15:52:53 -04001908 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001909 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001910 else if (ASIC_IS_AVIVO(rdev)) {
1911 if (is_tvcv)
1912 atombios_crtc_set_timing(crtc, adjusted_mode);
1913 else
1914 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1915 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001916 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001917 if (radeon_crtc->crtc_id == 0)
1918 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001919 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001920 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001921 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001922 atombios_overscan_setup(crtc, mode, adjusted_mode);
1923 atombios_scaler_setup(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04001924 /* update the hw version fpr dpm */
1925 radeon_crtc->hw_mode = *adjusted_mode;
1926
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001927 return 0;
1928}
1929
1930static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001931 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001932 struct drm_display_mode *adjusted_mode)
1933{
Alex Deucher5df31962012-09-13 11:52:08 -04001934 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1935 struct drm_device *dev = crtc->dev;
1936 struct drm_encoder *encoder;
1937
1938 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
1939 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1940 if (encoder->crtc == crtc) {
1941 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04001942 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04001943 break;
1944 }
1945 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001946 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
1947 radeon_crtc->encoder = NULL;
1948 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04001949 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04001950 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001951 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1952 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001953 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1954 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04001955 /* pick pll */
1956 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1957 /* if we can't get a PPLL for a non-DP encoder, fail */
1958 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
1959 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
1960 return false;
1961
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001962 return true;
1963}
1964
1965static void atombios_crtc_prepare(struct drm_crtc *crtc)
1966{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001967 struct drm_device *dev = crtc->dev;
1968 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001969
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001970 /* disable crtc pair power gating before programming */
1971 if (ASIC_IS_DCE6(rdev))
1972 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1973
Alex Deucher37b43902010-02-09 12:04:43 -05001974 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001975 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001976}
1977
1978static void atombios_crtc_commit(struct drm_crtc *crtc)
1979{
1980 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001981 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001982}
1983
Alex Deucher37f90032010-06-11 17:58:38 -04001984static void atombios_crtc_disable(struct drm_crtc *crtc)
1985{
1986 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04001987 struct drm_device *dev = crtc->dev;
1988 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001989 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04001990 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001991
Alex Deucher37f90032010-06-11 17:58:38 -04001992 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07001993 if (crtc->primary->fb) {
Ilija Hadzic75b871e2013-11-02 23:00:19 -04001994 int r;
1995 struct radeon_framebuffer *radeon_fb;
1996 struct radeon_bo *rbo;
1997
Matt Roperf4510a22014-04-01 15:22:40 -07001998 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04001999 rbo = gem_to_radeon_bo(radeon_fb->obj);
2000 r = radeon_bo_reserve(rbo, false);
2001 if (unlikely(r))
2002 DRM_ERROR("failed to reserve rbo before unpin\n");
2003 else {
2004 radeon_bo_unpin(rbo);
2005 radeon_bo_unreserve(rbo);
2006 }
2007 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04002008 /* disable the GRPH */
2009 if (ASIC_IS_DCE4(rdev))
2010 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2011 else if (ASIC_IS_AVIVO(rdev))
2012 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2013
Alex Deucher0e3d50b2013-02-05 11:47:09 -05002014 if (ASIC_IS_DCE6(rdev))
2015 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04002016
Alex Deucher4e585912012-08-21 19:06:21 -04002017 for (i = 0; i < rdev->num_crtc; i++) {
2018 if (rdev->mode_info.crtcs[i] &&
2019 rdev->mode_info.crtcs[i]->enabled &&
2020 i != radeon_crtc->crtc_id &&
2021 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
2022 /* one other crtc is using this pll don't turn
2023 * off the pll
2024 */
2025 goto done;
2026 }
2027 }
2028
Alex Deucher37f90032010-06-11 17:58:38 -04002029 switch (radeon_crtc->pll_id) {
2030 case ATOM_PPLL1:
2031 case ATOM_PPLL2:
2032 /* disable the ppll */
2033 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002034 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002035 break;
Alex Deucher64199872012-03-20 17:18:33 -04002036 case ATOM_PPLL0:
2037 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002038 if ((rdev->family == CHIP_ARUBA) ||
2039 (rdev->family == CHIP_BONAIRE) ||
2040 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002041 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2042 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2043 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002044 default:
2045 break;
2046 }
Alex Deucher4e585912012-08-21 19:06:21 -04002047done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002048 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002049 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002050 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002051 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002052}
2053
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002054static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2055 .dpms = atombios_crtc_dpms,
2056 .mode_fixup = atombios_crtc_mode_fixup,
2057 .mode_set = atombios_crtc_mode_set,
2058 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002059 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002060 .prepare = atombios_crtc_prepare,
2061 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002062 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002063 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002064};
2065
2066void radeon_atombios_init_crtc(struct drm_device *dev,
2067 struct radeon_crtc *radeon_crtc)
2068{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002069 struct radeon_device *rdev = dev->dev_private;
2070
2071 if (ASIC_IS_DCE4(rdev)) {
2072 switch (radeon_crtc->crtc_id) {
2073 case 0:
2074 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002075 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002076 break;
2077 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002078 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002079 break;
2080 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002081 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002082 break;
2083 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002084 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002085 break;
2086 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002087 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002088 break;
2089 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002090 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002091 break;
2092 }
2093 } else {
2094 if (radeon_crtc->crtc_id == 1)
2095 radeon_crtc->crtc_offset =
2096 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2097 else
2098 radeon_crtc->crtc_offset = 0;
2099 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002100 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002101 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002102 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002103 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002104 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2105}