blob: 4cf678306c9c19b05aa51324160fdf943e15a826 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Alex Deucher5df31962012-09-13 11:52:08 -040086 struct radeon_encoder *radeon_encoder =
87 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020088 /* fixme - fill in enc_priv for atom dac */
89 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100090 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Alex Deucher5df31962012-09-13 11:52:08 -040095 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
96 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
97 tv_std = tv_dac->tv_std;
98 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 }
100
Jerome Glissec93bb852009-07-13 21:04:08 +0200101 memset(&args, 0, sizeof(args));
102
103 args.ucScaler = radeon_crtc->crtc_id;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200106 switch (tv_std) {
107 case TV_STD_NTSC:
108 default:
109 args.ucTVStandard = ATOM_TV_NTSC;
110 break;
111 case TV_STD_PAL:
112 args.ucTVStandard = ATOM_TV_PAL;
113 break;
114 case TV_STD_PAL_M:
115 args.ucTVStandard = ATOM_TV_PALM;
116 break;
117 case TV_STD_PAL_60:
118 args.ucTVStandard = ATOM_TV_PAL60;
119 break;
120 case TV_STD_NTSC_J:
121 args.ucTVStandard = ATOM_TV_NTSCJ;
122 break;
123 case TV_STD_SCART_PAL:
124 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
125 break;
126 case TV_STD_SECAM:
127 args.ucTVStandard = ATOM_TV_SECAM;
128 break;
129 case TV_STD_PAL_CN:
130 args.ucTVStandard = ATOM_TV_PALCN;
131 break;
132 }
133 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000134 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200135 args.ucTVStandard = ATOM_TV_CV;
136 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
137 } else {
138 switch (radeon_crtc->rmx_type) {
139 case RMX_FULL:
140 args.ucEnable = ATOM_SCALER_EXPANSION;
141 break;
142 case RMX_CENTER:
143 args.ucEnable = ATOM_SCALER_CENTER;
144 break;
145 case RMX_ASPECT:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 default:
149 if (ASIC_IS_AVIVO(rdev))
150 args.ucEnable = ATOM_SCALER_DISABLE;
151 else
152 args.ucEnable = ATOM_SCALER_CENTER;
153 break;
154 }
155 }
156 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000157 if ((is_tv || is_cv)
158 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
159 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200160 }
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
164{
165 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
166 struct drm_device *dev = crtc->dev;
167 struct radeon_device *rdev = dev->dev_private;
168 int index =
169 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
170 ENABLE_CRTC_PS_ALLOCATION args;
171
172 memset(&args, 0, sizeof(args));
173
174 args.ucCRTC = radeon_crtc->crtc_id;
175 args.ucEnable = lock;
176
177 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
178}
179
180static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
181{
182 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
183 struct drm_device *dev = crtc->dev;
184 struct radeon_device *rdev = dev->dev_private;
185 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = state;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
212static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
213{
214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
215 struct drm_device *dev = crtc->dev;
216 struct radeon_device *rdev = dev->dev_private;
217 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
218 BLANK_CRTC_PS_ALLOCATION args;
219
220 memset(&args, 0, sizeof(args));
221
222 args.ucCRTC = radeon_crtc->crtc_id;
223 args.ucBlanking = state;
224
225 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
226}
227
Alex Deucherfef9f912012-03-20 17:18:03 -0400228static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
229{
230 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
231 struct drm_device *dev = crtc->dev;
232 struct radeon_device *rdev = dev->dev_private;
233 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
234 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
235
236 memset(&args, 0, sizeof(args));
237
238 args.ucDispPipeId = radeon_crtc->crtc_id;
239 args.ucEnable = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
242}
243
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
245{
246 struct drm_device *dev = crtc->dev;
247 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500248 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200249
250 switch (mode) {
251 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400252 radeon_crtc->enabled = true;
253 /* adjust pm to dpms changes BEFORE enabling crtcs */
254 radeon_pm_compute_clocks(rdev);
Alex Deucher37b43902010-02-09 12:04:43 -0500255 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400256 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500257 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
258 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400259 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500260 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200261 break;
262 case DRM_MODE_DPMS_STANDBY:
263 case DRM_MODE_DPMS_SUSPEND:
264 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400265 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500266 if (radeon_crtc->enabled)
267 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400268 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500269 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
270 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400271 radeon_crtc->enabled = false;
Alex Deucherd7311172010-05-03 01:13:14 -0400272 /* adjust pm to dpms changes AFTER disabling crtcs */
273 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274 break;
275 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200276}
277
278static void
279atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400280 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400282 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283 struct drm_device *dev = crtc->dev;
284 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400285 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200286 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400287 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200288
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400289 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400290 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400291 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400292 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
293 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400294 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400295 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400296 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400297 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400298 args.usH_SyncWidth =
299 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
300 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400301 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400302 args.usV_SyncWidth =
303 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400304 args.ucH_Border = radeon_crtc->h_border;
305 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400306
307 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
308 misc |= ATOM_VSYNC_POLARITY;
309 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
310 misc |= ATOM_HSYNC_POLARITY;
311 if (mode->flags & DRM_MODE_FLAG_CSYNC)
312 misc |= ATOM_COMPOSITESYNC;
313 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
314 misc |= ATOM_INTERLACE;
315 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
316 misc |= ATOM_DOUBLE_CLOCK_MODE;
317
318 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
319 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200320
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400321 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200322}
323
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400324static void atombios_crtc_set_timing(struct drm_crtc *crtc,
325 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200326{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400327 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200328 struct drm_device *dev = crtc->dev;
329 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400330 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200331 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400332 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200333
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400334 memset(&args, 0, sizeof(args));
335 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
336 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
337 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
338 args.usH_SyncWidth =
339 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
340 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
341 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
342 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
343 args.usV_SyncWidth =
344 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
345
Alex Deucher54bfe492010-09-03 15:52:53 -0400346 args.ucOverscanRight = radeon_crtc->h_border;
347 args.ucOverscanLeft = radeon_crtc->h_border;
348 args.ucOverscanBottom = radeon_crtc->v_border;
349 args.ucOverscanTop = radeon_crtc->v_border;
350
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400351 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
352 misc |= ATOM_VSYNC_POLARITY;
353 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
354 misc |= ATOM_HSYNC_POLARITY;
355 if (mode->flags & DRM_MODE_FLAG_CSYNC)
356 misc |= ATOM_COMPOSITESYNC;
357 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
358 misc |= ATOM_INTERLACE;
359 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
360 misc |= ATOM_DOUBLE_CLOCK_MODE;
361
362 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
363 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400365 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200366}
367
Alex Deucher3fa47d92012-01-20 14:56:39 -0500368static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500369{
Alex Deucherb7922102010-03-06 10:57:30 -0500370 u32 ss_cntl;
371
372 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500373 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500374 case ATOM_PPLL1:
375 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
376 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
377 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
378 break;
379 case ATOM_PPLL2:
380 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
381 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
382 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
383 break;
384 case ATOM_DCPLL:
385 case ATOM_PPLL_INVALID:
386 return;
387 }
388 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500389 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500390 case ATOM_PPLL1:
391 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
392 ss_cntl &= ~1;
393 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
394 break;
395 case ATOM_PPLL2:
396 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
397 ss_cntl &= ~1;
398 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
399 break;
400 case ATOM_DCPLL:
401 case ATOM_PPLL_INVALID:
402 return;
403 }
404 }
405}
406
407
Alex Deucher26b9fc32010-02-01 16:39:11 -0500408union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400409 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
410 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500411 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400412 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500413 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500414};
415
Alex Deucher3fa47d92012-01-20 14:56:39 -0500416static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400417 int enable,
418 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400419 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400420 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400421{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400422 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400423 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500424 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400425
Alex Deucherc4756ba2014-01-15 13:59:47 -0500426 if (enable) {
427 /* Don't mess with SS if percentage is 0 or external ss.
428 * SS is already disabled previously, and disabling it
429 * again can cause display problems if the pll is already
430 * programmed.
431 */
432 if (ss->percentage == 0)
433 return;
434 if (ss->type & ATOM_EXTERNAL_SS_MASK)
435 return;
436 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400437 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400438 if (rdev->mode_info.crtcs[i] &&
439 rdev->mode_info.crtcs[i]->enabled &&
440 i != crtc_id &&
441 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
442 /* one other crtc is using this pll don't turn
443 * off spread spectrum as it might turn off
444 * display on active crtc
445 */
446 return;
447 }
448 }
449 }
450
Alex Deucher26b9fc32010-02-01 16:39:11 -0500451 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400452
Alex Deuchera572eaa2011-01-06 21:19:16 -0500453 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500454 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400455 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500456 switch (pll_id) {
457 case ATOM_PPLL1:
458 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500459 break;
460 case ATOM_PPLL2:
461 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500462 break;
463 case ATOM_DCPLL:
464 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500465 break;
466 case ATOM_PPLL_INVALID:
467 return;
468 }
Alex Deucherf312f092012-07-17 14:02:44 -0400469 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
470 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400471 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500472 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400473 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400474 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400475 switch (pll_id) {
476 case ATOM_PPLL1:
477 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400478 break;
479 case ATOM_PPLL2:
480 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400481 break;
482 case ATOM_DCPLL:
483 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400484 break;
485 case ATOM_PPLL_INVALID:
486 return;
487 }
Alex Deucherf312f092012-07-17 14:02:44 -0400488 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
489 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400490 args.v2.ucEnable = enable;
491 } else if (ASIC_IS_DCE3(rdev)) {
492 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400493 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400494 args.v1.ucSpreadSpectrumStep = ss->step;
495 args.v1.ucSpreadSpectrumDelay = ss->delay;
496 args.v1.ucSpreadSpectrumRange = ss->range;
497 args.v1.ucPpll = pll_id;
498 args.v1.ucEnable = enable;
499 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400500 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
501 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500502 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400503 return;
504 }
505 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400506 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400507 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
508 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
509 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
510 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400511 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500512 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500513 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400514 return;
515 }
516 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400517 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400518 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
519 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
520 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400521 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500522 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400523}
524
Alex Deucher4eaeca32010-01-19 17:32:27 -0500525union adjust_pixel_clock {
526 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500527 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500528};
529
530static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400531 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200532{
Alex Deucher19eca432012-09-13 10:56:16 -0400533 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200534 struct drm_device *dev = crtc->dev;
535 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400536 struct drm_encoder *encoder = radeon_crtc->encoder;
537 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
538 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500539 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400540 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400541 u32 dp_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400542 int bpc = radeon_get_monitor_bpc(connector);
543 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500544
Alex Deucher4eaeca32010-01-19 17:32:27 -0500545 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400546 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200547
548 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400549 if ((rdev->family == CHIP_RS600) ||
550 (rdev->family == CHIP_RS690) ||
551 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400552 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
553 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000554
555 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400556 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000557 else
Alex Deucher19eca432012-09-13 10:56:16 -0400558 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400559
Alex Deucher5785e532011-04-19 15:24:59 -0400560 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400561 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400562 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500563 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400564 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400565 /* use frac fb div on RS780/RS880 */
566 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
567 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500568 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
569 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000570 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400571 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200572
Dave Airlie5480f722010-10-19 10:36:47 +1000573 if (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;
Dave Airlie5480f722010-10-19 10:36:47 +1000577 }
578
Alex Deucher5df31962012-09-13 11:52:08 -0400579 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
580 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
581 if (connector) {
582 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
583 struct radeon_connector_atom_dig *dig_connector =
584 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400585
Alex Deucher5df31962012-09-13 11:52:08 -0400586 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200587 }
588 }
589
Alex Deucher5df31962012-09-13 11:52:08 -0400590 /* use recommended ref_div for ss */
591 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
592 if (radeon_crtc->ss_enabled) {
593 if (radeon_crtc->ss.refdiv) {
594 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
595 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
596 if (ASIC_IS_AVIVO(rdev))
597 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
598 }
599 }
600 }
601
602 if (ASIC_IS_AVIVO(rdev)) {
603 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
604 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
605 adjusted_clock = mode->clock * 2;
606 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
607 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
608 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
609 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
610 } else {
611 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
612 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
613 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
614 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
615 }
616
Alex Deucher2606c882009-10-08 13:36:21 -0400617 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
618 * accordingly based on the encoder/transmitter to work around
619 * special hw requirements.
620 */
621 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500622 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500623 u8 frev, crev;
624 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400625
Alex Deucher2606c882009-10-08 13:36:21 -0400626 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400627 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
628 &crev))
629 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500630
631 memset(&args, 0, sizeof(args));
632
633 switch (frev) {
634 case 1:
635 switch (crev) {
636 case 1:
637 case 2:
638 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
639 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500640 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400641 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400642 args.v1.ucConfig |=
643 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500644
645 atom_execute_table(rdev->mode_info.atom_context,
646 index, (uint32_t *)&args);
647 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
648 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500649 case 3:
650 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
651 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
652 args.v3.sInput.ucEncodeMode = encoder_mode;
653 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400654 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000655 args.v3.sInput.ucDispPllConfig |=
656 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400657 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400658 args.v3.sInput.ucDispPllConfig |=
659 DISPPLL_CONFIG_COHERENT_MODE;
660 /* 16200 or 27000 */
661 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
662 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500663 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400664 if (encoder_mode == ATOM_ENCODER_MODE_HDMI)
665 /* deep color support */
666 args.v3.sInput.usPixelClock =
667 cpu_to_le16((mode->clock * bpc / 8) / 10);
668 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500669 args.v3.sInput.ucDispPllConfig |=
670 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500671 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500672 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400673 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500674 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400675 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
676 ENCODER_OBJECT_ID_NONE)
677 args.v3.sInput.ucExtTransmitterID =
678 radeon_encoder_get_dp_bridge_encoder_id(encoder);
679 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400680 args.v3.sInput.ucExtTransmitterID = 0;
681
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500682 atom_execute_table(rdev->mode_info.atom_context,
683 index, (uint32_t *)&args);
684 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
685 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400686 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
687 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
688 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500689 }
690 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400691 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
692 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
693 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500694 }
695 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500696 default:
697 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
698 return adjusted_clock;
699 }
700 break;
701 default:
702 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
703 return adjusted_clock;
704 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400705 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500706 return adjusted_clock;
707}
708
709union set_pixel_clock {
710 SET_PIXEL_CLOCK_PS_ALLOCATION base;
711 PIXEL_CLOCK_PARAMETERS v1;
712 PIXEL_CLOCK_PARAMETERS_V2 v2;
713 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500714 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500715 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500716};
717
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500718/* on DCE5, make sure the voltage is high enough to support the
719 * required disp clk.
720 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400721static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500722 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500723{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500724 u8 frev, crev;
725 int index;
726 union set_pixel_clock args;
727
728 memset(&args, 0, sizeof(args));
729
730 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400731 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
732 &crev))
733 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500734
735 switch (frev) {
736 case 1:
737 switch (crev) {
738 case 5:
739 /* if the default dcpll clock is specified,
740 * SetPixelClock provides the dividers
741 */
742 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500743 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500744 args.v5.ucPpll = ATOM_DCPLL;
745 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500746 case 6:
747 /* if the default dcpll clock is specified,
748 * SetPixelClock provides the dividers
749 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500750 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400751 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400752 args.v6.ucPpll = ATOM_EXT_PLL1;
753 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400754 args.v6.ucPpll = ATOM_PPLL0;
755 else
756 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500757 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500758 default:
759 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
760 return;
761 }
762 break;
763 default:
764 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
765 return;
766 }
767 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
768}
769
Alex Deucher37f90032010-06-11 17:58:38 -0400770static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000771 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400772 int pll_id,
773 u32 encoder_mode,
774 u32 encoder_id,
775 u32 clock,
776 u32 ref_div,
777 u32 fb_div,
778 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400779 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400780 int bpc,
781 bool ss_enabled,
782 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400783{
784 struct drm_device *dev = crtc->dev;
785 struct radeon_device *rdev = dev->dev_private;
786 u8 frev, crev;
787 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
788 union set_pixel_clock args;
789
790 memset(&args, 0, sizeof(args));
791
792 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
793 &crev))
794 return;
795
796 switch (frev) {
797 case 1:
798 switch (crev) {
799 case 1:
800 if (clock == ATOM_DISABLE)
801 return;
802 args.v1.usPixelClock = cpu_to_le16(clock / 10);
803 args.v1.usRefDiv = cpu_to_le16(ref_div);
804 args.v1.usFbDiv = cpu_to_le16(fb_div);
805 args.v1.ucFracFbDiv = frac_fb_div;
806 args.v1.ucPostDiv = post_div;
807 args.v1.ucPpll = pll_id;
808 args.v1.ucCRTC = crtc_id;
809 args.v1.ucRefDivSrc = 1;
810 break;
811 case 2:
812 args.v2.usPixelClock = cpu_to_le16(clock / 10);
813 args.v2.usRefDiv = cpu_to_le16(ref_div);
814 args.v2.usFbDiv = cpu_to_le16(fb_div);
815 args.v2.ucFracFbDiv = frac_fb_div;
816 args.v2.ucPostDiv = post_div;
817 args.v2.ucPpll = pll_id;
818 args.v2.ucCRTC = crtc_id;
819 args.v2.ucRefDivSrc = 1;
820 break;
821 case 3:
822 args.v3.usPixelClock = cpu_to_le16(clock / 10);
823 args.v3.usRefDiv = cpu_to_le16(ref_div);
824 args.v3.usFbDiv = cpu_to_le16(fb_div);
825 args.v3.ucFracFbDiv = frac_fb_div;
826 args.v3.ucPostDiv = post_div;
827 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400828 if (crtc_id == ATOM_CRTC2)
829 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
830 else
831 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400832 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
833 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400834 args.v3.ucTransmitterId = encoder_id;
835 args.v3.ucEncoderMode = encoder_mode;
836 break;
837 case 5:
838 args.v5.ucCRTC = crtc_id;
839 args.v5.usPixelClock = cpu_to_le16(clock / 10);
840 args.v5.ucRefDiv = ref_div;
841 args.v5.usFbDiv = cpu_to_le16(fb_div);
842 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
843 args.v5.ucPostDiv = post_div;
844 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400845 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
846 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400847 switch (bpc) {
848 case 8:
849 default:
850 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
851 break;
852 case 10:
853 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
854 break;
855 }
Alex Deucher37f90032010-06-11 17:58:38 -0400856 args.v5.ucTransmitterID = encoder_id;
857 args.v5.ucEncoderMode = encoder_mode;
858 args.v5.ucPpll = pll_id;
859 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500860 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000861 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500862 args.v6.ucRefDiv = ref_div;
863 args.v6.usFbDiv = cpu_to_le16(fb_div);
864 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
865 args.v6.ucPostDiv = post_div;
866 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400867 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
868 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucherdf271be2011-05-20 04:34:15 -0400869 switch (bpc) {
870 case 8:
871 default:
872 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
873 break;
874 case 10:
875 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
876 break;
877 case 12:
878 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
879 break;
880 case 16:
881 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
882 break;
883 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500884 args.v6.ucTransmitterID = encoder_id;
885 args.v6.ucEncoderMode = encoder_mode;
886 args.v6.ucPpll = pll_id;
887 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400888 default:
889 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
890 return;
891 }
892 break;
893 default:
894 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
895 return;
896 }
897
898 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
899}
900
Alex Deucher19eca432012-09-13 10:56:16 -0400901static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
902{
903 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
904 struct drm_device *dev = crtc->dev;
905 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400906 struct radeon_encoder *radeon_encoder =
907 to_radeon_encoder(radeon_crtc->encoder);
908 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400909
910 radeon_crtc->bpc = 8;
911 radeon_crtc->ss_enabled = false;
912
Alex Deucher19eca432012-09-13 10:56:16 -0400913 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400914 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400915 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
916 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400917 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400918 struct radeon_connector *radeon_connector =
919 to_radeon_connector(connector);
920 struct radeon_connector_atom_dig *dig_connector =
921 radeon_connector->con_priv;
922 int dp_clock;
923 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
924
925 switch (encoder_mode) {
926 case ATOM_ENCODER_MODE_DP_MST:
927 case ATOM_ENCODER_MODE_DP:
928 /* DP/eDP */
929 dp_clock = dig_connector->dp_clock / 10;
930 if (ASIC_IS_DCE4(rdev))
931 radeon_crtc->ss_enabled =
932 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
933 ASIC_INTERNAL_SS_ON_DP,
934 dp_clock);
935 else {
936 if (dp_clock == 16200) {
937 radeon_crtc->ss_enabled =
938 radeon_atombios_get_ppll_ss_info(rdev,
939 &radeon_crtc->ss,
940 ATOM_DP_SS_ID2);
941 if (!radeon_crtc->ss_enabled)
942 radeon_crtc->ss_enabled =
943 radeon_atombios_get_ppll_ss_info(rdev,
944 &radeon_crtc->ss,
945 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500946 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400947 radeon_crtc->ss_enabled =
948 radeon_atombios_get_ppll_ss_info(rdev,
949 &radeon_crtc->ss,
950 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500951 }
952 /* disable spread spectrum on DCE3 DP */
953 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -0400954 }
955 break;
956 case ATOM_ENCODER_MODE_LVDS:
957 if (ASIC_IS_DCE4(rdev))
958 radeon_crtc->ss_enabled =
959 radeon_atombios_get_asic_ss_info(rdev,
960 &radeon_crtc->ss,
961 dig->lcd_ss_id,
962 mode->clock / 10);
963 else
964 radeon_crtc->ss_enabled =
965 radeon_atombios_get_ppll_ss_info(rdev,
966 &radeon_crtc->ss,
967 dig->lcd_ss_id);
968 break;
969 case ATOM_ENCODER_MODE_DVI:
970 if (ASIC_IS_DCE4(rdev))
971 radeon_crtc->ss_enabled =
972 radeon_atombios_get_asic_ss_info(rdev,
973 &radeon_crtc->ss,
974 ASIC_INTERNAL_SS_ON_TMDS,
975 mode->clock / 10);
976 break;
977 case ATOM_ENCODER_MODE_HDMI:
978 if (ASIC_IS_DCE4(rdev))
979 radeon_crtc->ss_enabled =
980 radeon_atombios_get_asic_ss_info(rdev,
981 &radeon_crtc->ss,
982 ASIC_INTERNAL_SS_ON_HDMI,
983 mode->clock / 10);
984 break;
985 default:
986 break;
987 }
988 }
989
990 /* adjust pixel clock as needed */
991 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
992
993 return true;
994}
995
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500996static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -0500997{
998 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
999 struct drm_device *dev = crtc->dev;
1000 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001001 struct radeon_encoder *radeon_encoder =
1002 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001003 u32 pll_clock = mode->clock;
1004 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1005 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001006 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001007
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001008 switch (radeon_crtc->pll_id) {
1009 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001010 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001011 break;
1012 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001013 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001014 break;
1015 case ATOM_DCPLL:
1016 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001017 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001018 pll = &rdev->clock.dcpll;
1019 break;
1020 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001021
Alex Deucher19eca432012-09-13 10:56:16 -04001022 /* update pll params */
1023 pll->flags = radeon_crtc->pll_flags;
1024 pll->reference_div = radeon_crtc->pll_reference_div;
1025 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001026
Alex Deucher64146f82011-03-22 01:46:12 -04001027 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1028 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001029 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1030 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001031 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001032 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1033 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001034 else
Alex Deucher19eca432012-09-13 10:56:16 -04001035 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1036 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001037
Alex Deucher19eca432012-09-13 10:56:16 -04001038 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1039 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001040
Alex Deucher37f90032010-06-11 17:58:38 -04001041 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1042 encoder_mode, radeon_encoder->encoder_id, mode->clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001043 ref_div, fb_div, frac_fb_div, post_div,
1044 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001045
Alex Deucher19eca432012-09-13 10:56:16 -04001046 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001047 /* calculate ss amount and step size */
1048 if (ASIC_IS_DCE4(rdev)) {
1049 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001050 u32 amount = (((fb_div * 10) + frac_fb_div) *
1051 (u32)radeon_crtc->ss.percentage) /
1052 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001053 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1054 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001055 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001056 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001057 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001058 (125 * 25 * pll->reference_freq / 100);
1059 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001060 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001061 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001062 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001063 }
1064
Alex Deucher19eca432012-09-13 10:56:16 -04001065 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1066 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001067 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001068}
1069
Alex Deucherc9417bd2011-02-06 14:23:26 -05001070static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1071 struct drm_framebuffer *fb,
1072 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001073{
1074 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1075 struct drm_device *dev = crtc->dev;
1076 struct radeon_device *rdev = dev->dev_private;
1077 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001078 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001079 struct drm_gem_object *obj;
1080 struct radeon_bo *rbo;
1081 uint64_t fb_location;
1082 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001083 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001084 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001085 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001086 int r;
1087
1088 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001089 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001090 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001091 return 0;
1092 }
1093
Chris Ball4dd19b02010-09-26 06:47:23 -05001094 if (atomic) {
1095 radeon_fb = to_radeon_framebuffer(fb);
1096 target_fb = fb;
1097 }
1098 else {
1099 radeon_fb = to_radeon_framebuffer(crtc->fb);
1100 target_fb = crtc->fb;
1101 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001102
Chris Ball4dd19b02010-09-26 06:47:23 -05001103 /* If atomic, assume fb object is pinned & idle & fenced and
1104 * just update base pointers
1105 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001106 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001107 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001108 r = radeon_bo_reserve(rbo, false);
1109 if (unlikely(r != 0))
1110 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001111
1112 if (atomic)
1113 fb_location = radeon_bo_gpu_offset(rbo);
1114 else {
1115 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1116 if (unlikely(r != 0)) {
1117 radeon_bo_unreserve(rbo);
1118 return -EINVAL;
1119 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001120 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001121
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001122 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1123 radeon_bo_unreserve(rbo);
1124
Chris Ball4dd19b02010-09-26 06:47:23 -05001125 switch (target_fb->bits_per_pixel) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001126 case 8:
1127 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1128 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1129 break;
1130 case 15:
1131 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1132 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1133 break;
1134 case 16:
1135 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1136 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001137#ifdef __BIG_ENDIAN
1138 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1139#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001140 break;
1141 case 24:
1142 case 32:
1143 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1144 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001145#ifdef __BIG_ENDIAN
1146 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1147#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001148 break;
1149 default:
1150 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001151 target_fb->bits_per_pixel);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001152 return -EINVAL;
1153 }
1154
Alex Deucher392e3722011-11-28 14:49:27 -05001155 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001156 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001157
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001158 /* Set NUM_BANKS. */
1159 if (rdev->family >= CHIP_BONAIRE) {
1160 unsigned tileb, index, num_banks, tile_split_bytes;
1161
1162 /* Calculate the macrotile mode index. */
1163 tile_split_bytes = 64 << tile_split;
1164 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1165 tileb = min(tile_split_bytes, tileb);
1166
1167 for (index = 0; tileb > 64; index++) {
1168 tileb >>= 1;
1169 }
1170
1171 if (index >= 16) {
1172 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1173 target_fb->bits_per_pixel, tile_split);
1174 return -EINVAL;
1175 }
1176
1177 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
1178 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1179 } else {
1180 /* SI and older. */
1181 if (rdev->family >= CHIP_TAHITI)
1182 tmp = rdev->config.si.tile_config;
1183 else if (rdev->family >= CHIP_CAYMAN)
1184 tmp = rdev->config.cayman.tile_config;
1185 else
1186 tmp = rdev->config.evergreen.tile_config;
1187
1188 switch ((tmp & 0xf0) >> 4) {
1189 case 0: /* 4 banks */
1190 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1191 break;
1192 case 1: /* 8 banks */
1193 default:
1194 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1195 break;
1196 case 2: /* 16 banks */
1197 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1198 break;
1199 }
Alex Deucher392e3722011-11-28 14:49:27 -05001200 }
1201
Alex Deucher97d66322010-05-20 12:12:48 -04001202 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001203 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1204 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1205 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1206 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001207 if (rdev->family >= CHIP_BONAIRE) {
1208 /* XXX need to know more about the surface tiling mode */
1209 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1210 }
Alex Deucher392e3722011-11-28 14:49:27 -05001211 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001212 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1213
Alex Deucher8da0e502012-07-11 18:38:29 -04001214 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001215 /* Read the pipe config from the 2D TILED SCANOUT mode.
1216 * It should be the same for the other modes too, but not all
1217 * modes set the pipe config field. */
1218 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1219
1220 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001221 } else if ((rdev->family == CHIP_TAHITI) ||
1222 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001223 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001224 else if ((rdev->family == CHIP_VERDE) ||
1225 (rdev->family == CHIP_OLAND) ||
1226 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001227 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1228
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001229 switch (radeon_crtc->crtc_id) {
1230 case 0:
1231 WREG32(AVIVO_D1VGA_CONTROL, 0);
1232 break;
1233 case 1:
1234 WREG32(AVIVO_D2VGA_CONTROL, 0);
1235 break;
1236 case 2:
1237 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1238 break;
1239 case 3:
1240 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1241 break;
1242 case 4:
1243 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1244 break;
1245 case 5:
1246 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1247 break;
1248 default:
1249 break;
1250 }
1251
1252 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1253 upper_32_bits(fb_location));
1254 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1255 upper_32_bits(fb_location));
1256 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1257 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1258 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1259 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1260 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001261 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001262
1263 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1264 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1265 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1266 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001267 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1268 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001269
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001270 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001271 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1272 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1273
Alex Deucher8da0e502012-07-11 18:38:29 -04001274 if (rdev->family >= CHIP_BONAIRE)
1275 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1276 target_fb->height);
1277 else
1278 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1279 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001280 x &= ~3;
1281 y &= ~1;
1282 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1283 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001284 viewport_w = crtc->mode.hdisplay;
1285 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001286 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001287 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001288
Alex Deucherfb9674b2011-04-02 09:15:50 -04001289 /* pageflip setup */
1290 /* make sure flip is at vb rather than hb */
1291 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1292 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1293 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1294
1295 /* set pageflip to happen anywhere in vblank interval */
1296 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1297
Chris Ball4dd19b02010-09-26 06:47:23 -05001298 if (!atomic && fb && fb != crtc->fb) {
1299 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001300 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001301 r = radeon_bo_reserve(rbo, false);
1302 if (unlikely(r != 0))
1303 return r;
1304 radeon_bo_unpin(rbo);
1305 radeon_bo_unreserve(rbo);
1306 }
1307
1308 /* Bytes per pixel may have changed */
1309 radeon_bandwidth_update(rdev);
1310
1311 return 0;
1312}
1313
Chris Ball4dd19b02010-09-26 06:47:23 -05001314static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1315 struct drm_framebuffer *fb,
1316 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001317{
1318 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1319 struct drm_device *dev = crtc->dev;
1320 struct radeon_device *rdev = dev->dev_private;
1321 struct radeon_framebuffer *radeon_fb;
1322 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001323 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001324 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001325 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001326 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001327 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001328 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001329 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001330
Jerome Glisse2de3b482009-11-17 14:08:55 -08001331 /* no fb bound */
Chris Ball4dd19b02010-09-26 06:47:23 -05001332 if (!atomic && !crtc->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001333 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001334 return 0;
1335 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001336
Chris Ball4dd19b02010-09-26 06:47:23 -05001337 if (atomic) {
1338 radeon_fb = to_radeon_framebuffer(fb);
1339 target_fb = fb;
1340 }
1341 else {
1342 radeon_fb = to_radeon_framebuffer(crtc->fb);
1343 target_fb = crtc->fb;
1344 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001345
1346 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001347 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001348 r = radeon_bo_reserve(rbo, false);
1349 if (unlikely(r != 0))
1350 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001351
1352 /* If atomic, assume fb object is pinned & idle & fenced and
1353 * just update base pointers
1354 */
1355 if (atomic)
1356 fb_location = radeon_bo_gpu_offset(rbo);
1357 else {
1358 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1359 if (unlikely(r != 0)) {
1360 radeon_bo_unreserve(rbo);
1361 return -EINVAL;
1362 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001363 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001364 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1365 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001366
Chris Ball4dd19b02010-09-26 06:47:23 -05001367 switch (target_fb->bits_per_pixel) {
Dave Airlie41456df2009-09-16 10:15:21 +10001368 case 8:
1369 fb_format =
1370 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1371 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1372 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001373 case 15:
1374 fb_format =
1375 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1376 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1377 break;
1378 case 16:
1379 fb_format =
1380 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1381 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001382#ifdef __BIG_ENDIAN
1383 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1384#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001385 break;
1386 case 24:
1387 case 32:
1388 fb_format =
1389 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1390 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001391#ifdef __BIG_ENDIAN
1392 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1393#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001394 break;
1395 default:
1396 DRM_ERROR("Unsupported screen depth %d\n",
Chris Ball4dd19b02010-09-26 06:47:23 -05001397 target_fb->bits_per_pixel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001398 return -EINVAL;
1399 }
1400
Alex Deucher40c4ac12010-05-20 12:04:59 -04001401 if (rdev->family >= CHIP_R600) {
1402 if (tiling_flags & RADEON_TILING_MACRO)
1403 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1404 else if (tiling_flags & RADEON_TILING_MICRO)
1405 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1406 } else {
1407 if (tiling_flags & RADEON_TILING_MACRO)
1408 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001409
Alex Deucher40c4ac12010-05-20 12:04:59 -04001410 if (tiling_flags & RADEON_TILING_MICRO)
1411 fb_format |= AVIVO_D1GRPH_TILED;
1412 }
Dave Airliee024e112009-06-24 09:48:08 +10001413
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001414 if (radeon_crtc->crtc_id == 0)
1415 WREG32(AVIVO_D1VGA_CONTROL, 0);
1416 else
1417 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001418
1419 if (rdev->family >= CHIP_RV770) {
1420 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001421 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1422 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001423 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001424 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1425 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001426 }
1427 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001428 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1429 (u32) fb_location);
1430 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1431 radeon_crtc->crtc_offset, (u32) fb_location);
1432 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001433 if (rdev->family >= CHIP_R600)
1434 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001435
1436 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1437 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1438 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1439 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001440 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1441 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001442
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001443 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001444 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1445 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1446
1447 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001448 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001449 x &= ~3;
1450 y &= ~1;
1451 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1452 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001453 viewport_w = crtc->mode.hdisplay;
1454 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001455 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001456 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001457
Alex Deucherfb9674b2011-04-02 09:15:50 -04001458 /* pageflip setup */
1459 /* make sure flip is at vb rather than hb */
1460 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1461 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1462 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1463
1464 /* set pageflip to happen anywhere in vblank interval */
1465 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1466
Chris Ball4dd19b02010-09-26 06:47:23 -05001467 if (!atomic && fb && fb != crtc->fb) {
1468 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001469 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001470 r = radeon_bo_reserve(rbo, false);
1471 if (unlikely(r != 0))
1472 return r;
1473 radeon_bo_unpin(rbo);
1474 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001475 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001476
1477 /* Bytes per pixel may have changed */
1478 radeon_bandwidth_update(rdev);
1479
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001480 return 0;
1481}
1482
Alex Deucher54f088a2010-01-19 16:34:01 -05001483int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1484 struct drm_framebuffer *old_fb)
1485{
1486 struct drm_device *dev = crtc->dev;
1487 struct radeon_device *rdev = dev->dev_private;
1488
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001489 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001490 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001491 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001492 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001493 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001494 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1495}
1496
1497int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1498 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001499 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001500{
1501 struct drm_device *dev = crtc->dev;
1502 struct radeon_device *rdev = dev->dev_private;
1503
1504 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001505 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001506 else if (ASIC_IS_AVIVO(rdev))
1507 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1508 else
1509 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001510}
1511
Alex Deucher615e0cb2010-01-20 16:22:53 -05001512/* properly set additional regs when using atombios */
1513static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1514{
1515 struct drm_device *dev = crtc->dev;
1516 struct radeon_device *rdev = dev->dev_private;
1517 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1518 u32 disp_merge_cntl;
1519
1520 switch (radeon_crtc->crtc_id) {
1521 case 0:
1522 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1523 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1524 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1525 break;
1526 case 1:
1527 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1528 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1529 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1530 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1531 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1532 break;
1533 }
1534}
1535
Alex Deucherf3dd8502012-08-31 11:56:50 -04001536/**
1537 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1538 *
1539 * @crtc: drm crtc
1540 *
1541 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1542 */
1543static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1544{
1545 struct drm_device *dev = crtc->dev;
1546 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001547 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001548 u32 pll_in_use = 0;
1549
1550 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1551 if (crtc == test_crtc)
1552 continue;
1553
Alex Deucher57b35e22012-09-17 17:34:45 -04001554 test_radeon_crtc = to_radeon_crtc(test_crtc);
1555 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1556 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001557 }
1558 return pll_in_use;
1559}
1560
1561/**
1562 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1563 *
1564 * @crtc: drm crtc
1565 *
1566 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1567 * also in DP mode. For DP, a single PPLL can be used for all DP
1568 * crtcs/encoders.
1569 */
1570static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1571{
1572 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001573 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001574 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001575
Alex Deucher57b35e22012-09-17 17:34:45 -04001576 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1577 if (crtc == test_crtc)
1578 continue;
1579 test_radeon_crtc = to_radeon_crtc(test_crtc);
1580 if (test_radeon_crtc->encoder &&
1581 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1582 /* for DP use the same PLL for all */
1583 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1584 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001585 }
1586 }
1587 return ATOM_PPLL_INVALID;
1588}
1589
1590/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001591 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1592 *
1593 * @crtc: drm crtc
1594 * @encoder: drm encoder
1595 *
1596 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1597 * be shared (i.e., same clock).
1598 */
Alex Deucher5df31962012-09-13 11:52:08 -04001599static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001600{
Alex Deucher5df31962012-09-13 11:52:08 -04001601 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001602 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001603 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001604 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001605 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001606
Alex Deucher9642ac02012-09-13 12:43:41 -04001607 adjusted_clock = radeon_crtc->adjusted_clock;
1608
1609 if (adjusted_clock == 0)
1610 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001611
Alex Deucher57b35e22012-09-17 17:34:45 -04001612 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1613 if (crtc == test_crtc)
1614 continue;
1615 test_radeon_crtc = to_radeon_crtc(test_crtc);
1616 if (test_radeon_crtc->encoder &&
1617 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1618 /* check if we are already driving this connector with another crtc */
1619 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1620 /* if we are, return that pll */
1621 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001622 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001623 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001624 /* for non-DP check the clock */
1625 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1626 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1627 (adjusted_clock == test_adjusted_clock) &&
1628 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1629 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1630 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001631 }
1632 }
1633 return ATOM_PPLL_INVALID;
1634}
1635
1636/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001637 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1638 *
1639 * @crtc: drm crtc
1640 *
1641 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1642 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1643 * monitors a dedicated PPLL must be used. If a particular board has
1644 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1645 * as there is no need to program the PLL itself. If we are not able to
1646 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1647 * avoid messing up an existing monitor.
1648 *
1649 * Asic specific PLL information
1650 *
Alex Deucher0331f672012-09-14 11:57:21 -04001651 * DCE 8.x
1652 * KB/KV
1653 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1654 * CI
1655 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1656 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001657 * DCE 6.1
1658 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1659 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1660 *
1661 * DCE 6.0
1662 * - PPLL0 is available to all UNIPHY (DP only)
1663 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1664 *
1665 * DCE 5.0
1666 * - DCPLL is available to all UNIPHY (DP only)
1667 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1668 *
1669 * DCE 3.0/4.0/4.1
1670 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1671 *
1672 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001673static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1674{
Alex Deucher5df31962012-09-13 11:52:08 -04001675 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001676 struct drm_device *dev = crtc->dev;
1677 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001678 struct radeon_encoder *radeon_encoder =
1679 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001680 u32 pll_in_use;
1681 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001682
Alex Deucher0331f672012-09-14 11:57:21 -04001683 if (ASIC_IS_DCE8(rdev)) {
1684 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1685 if (rdev->clock.dp_extclk)
1686 /* skip PPLL programming if using ext clock */
1687 return ATOM_PPLL_INVALID;
1688 else {
1689 /* use the same PPLL for all DP monitors */
1690 pll = radeon_get_shared_dp_ppll(crtc);
1691 if (pll != ATOM_PPLL_INVALID)
1692 return pll;
1693 }
1694 } else {
1695 /* use the same PPLL for all monitors with the same clock */
1696 pll = radeon_get_shared_nondp_ppll(crtc);
1697 if (pll != ATOM_PPLL_INVALID)
1698 return pll;
1699 }
1700 /* otherwise, pick one of the plls */
1701 if ((rdev->family == CHIP_KAVERI) ||
1702 (rdev->family == CHIP_KABINI)) {
1703 /* KB/KV has PPLL1 and PPLL2 */
1704 pll_in_use = radeon_get_pll_use_mask(crtc);
1705 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1706 return ATOM_PPLL2;
1707 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1708 return ATOM_PPLL1;
1709 DRM_ERROR("unable to allocate a PPLL\n");
1710 return ATOM_PPLL_INVALID;
1711 } else {
1712 /* CI has PPLL0, PPLL1, and PPLL2 */
1713 pll_in_use = radeon_get_pll_use_mask(crtc);
1714 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1715 return ATOM_PPLL2;
1716 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1717 return ATOM_PPLL1;
1718 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1719 return ATOM_PPLL0;
1720 DRM_ERROR("unable to allocate a PPLL\n");
1721 return ATOM_PPLL_INVALID;
1722 }
1723 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001724 struct radeon_encoder_atom_dig *dig =
1725 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001726
Alex Deucher5df31962012-09-13 11:52:08 -04001727 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1728 (dig->linkb == false))
1729 /* UNIPHY A uses PPLL2 */
1730 return ATOM_PPLL2;
1731 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1732 /* UNIPHY B/C/D/E/F */
1733 if (rdev->clock.dp_extclk)
1734 /* skip PPLL programming if using ext clock */
1735 return ATOM_PPLL_INVALID;
1736 else {
1737 /* use the same PPLL for all DP monitors */
1738 pll = radeon_get_shared_dp_ppll(crtc);
1739 if (pll != ATOM_PPLL_INVALID)
1740 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001741 }
Alex Deucher5df31962012-09-13 11:52:08 -04001742 } else {
1743 /* use the same PPLL for all monitors with the same clock */
1744 pll = radeon_get_shared_nondp_ppll(crtc);
1745 if (pll != ATOM_PPLL_INVALID)
1746 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001747 }
1748 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001749 pll_in_use = radeon_get_pll_use_mask(crtc);
1750 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001751 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001752 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1753 return ATOM_PPLL1;
1754 DRM_ERROR("unable to allocate a PPLL\n");
1755 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001756 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001757 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1758 * depending on the asic:
1759 * DCE4: PPLL or ext clock
1760 * DCE5: PPLL, DCPLL, or ext clock
1761 * DCE6: PPLL, PPLL0, or ext clock
1762 *
1763 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1764 * PPLL/DCPLL programming and only program the DP DTO for the
1765 * crtc virtual pixel clock.
1766 */
1767 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1768 if (rdev->clock.dp_extclk)
1769 /* skip PPLL programming if using ext clock */
1770 return ATOM_PPLL_INVALID;
1771 else if (ASIC_IS_DCE6(rdev))
1772 /* use PPLL0 for all DP */
1773 return ATOM_PPLL0;
1774 else if (ASIC_IS_DCE5(rdev))
1775 /* use DCPLL for all DP */
1776 return ATOM_DCPLL;
1777 else {
1778 /* use the same PPLL for all DP monitors */
1779 pll = radeon_get_shared_dp_ppll(crtc);
1780 if (pll != ATOM_PPLL_INVALID)
1781 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001782 }
Alex Deucher70471862013-10-31 16:43:27 -04001783 } else if (!ASIC_IS_DCE41(rdev)) { /* Don't share PLLs on DCE4.1 chips */
Alex Deucher5df31962012-09-13 11:52:08 -04001784 /* use the same PPLL for all monitors with the same clock */
1785 pll = radeon_get_shared_nondp_ppll(crtc);
1786 if (pll != ATOM_PPLL_INVALID)
1787 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001788 }
1789 /* all other cases */
1790 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001791 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1792 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001793 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1794 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001795 DRM_ERROR("unable to allocate a PPLL\n");
1796 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001797 } else {
1798 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001799 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1800 * the matching btw pll and crtc is done through
1801 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1802 * pll (1 or 2) to select which register to write. ie if using
1803 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1804 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1805 * choose which value to write. Which is reverse order from
1806 * register logic. So only case that works is when pllid is
1807 * same as crtcid or when both pll and crtc are enabled and
1808 * both use same clock.
1809 *
1810 * So just return crtc id as if crtc and pll were hard linked
1811 * together even if they aren't
1812 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001813 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001814 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001815}
1816
Alex Deucherf3f1f032012-03-20 17:18:04 -04001817void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001818{
1819 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001820 if (ASIC_IS_DCE6(rdev))
1821 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1822 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001823 struct radeon_atom_ss ss;
1824 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1825 ASIC_INTERNAL_SS_ON_DCPLL,
1826 rdev->clock.default_dispclk);
1827 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001828 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001829 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001830 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001831 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001832 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001833 }
1834
1835}
1836
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001837int atombios_crtc_mode_set(struct drm_crtc *crtc,
1838 struct drm_display_mode *mode,
1839 struct drm_display_mode *adjusted_mode,
1840 int x, int y, struct drm_framebuffer *old_fb)
1841{
1842 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1843 struct drm_device *dev = crtc->dev;
1844 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001845 struct radeon_encoder *radeon_encoder =
1846 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04001847 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001848
Alex Deucher5df31962012-09-13 11:52:08 -04001849 if (radeon_encoder->active_device &
1850 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1851 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001852
1853 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001854
Alex Deucher54bfe492010-09-03 15:52:53 -04001855 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001856 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04001857 else if (ASIC_IS_AVIVO(rdev)) {
1858 if (is_tvcv)
1859 atombios_crtc_set_timing(crtc, adjusted_mode);
1860 else
1861 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1862 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001863 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04001864 if (radeon_crtc->crtc_id == 0)
1865 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05001866 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001867 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001868 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02001869 atombios_overscan_setup(crtc, mode, adjusted_mode);
1870 atombios_scaler_setup(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04001871 /* update the hw version fpr dpm */
1872 radeon_crtc->hw_mode = *adjusted_mode;
1873
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001874 return 0;
1875}
1876
1877static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001878 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001879 struct drm_display_mode *adjusted_mode)
1880{
Alex Deucher5df31962012-09-13 11:52:08 -04001881 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1882 struct drm_device *dev = crtc->dev;
1883 struct drm_encoder *encoder;
1884
1885 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
1886 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1887 if (encoder->crtc == crtc) {
1888 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04001889 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04001890 break;
1891 }
1892 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001893 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
1894 radeon_crtc->encoder = NULL;
1895 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04001896 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04001897 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001898 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1899 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04001900 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
1901 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04001902 /* pick pll */
1903 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1904 /* if we can't get a PPLL for a non-DP encoder, fail */
1905 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
1906 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
1907 return false;
1908
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001909 return true;
1910}
1911
1912static void atombios_crtc_prepare(struct drm_crtc *crtc)
1913{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001914 struct drm_device *dev = crtc->dev;
1915 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05001916
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04001917 /* disable crtc pair power gating before programming */
1918 if (ASIC_IS_DCE6(rdev))
1919 atombios_powergate_crtc(crtc, ATOM_DISABLE);
1920
Alex Deucher37b43902010-02-09 12:04:43 -05001921 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05001922 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001923}
1924
1925static void atombios_crtc_commit(struct drm_crtc *crtc)
1926{
1927 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05001928 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001929}
1930
Alex Deucher37f90032010-06-11 17:58:38 -04001931static void atombios_crtc_disable(struct drm_crtc *crtc)
1932{
1933 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04001934 struct drm_device *dev = crtc->dev;
1935 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001936 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04001937 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04001938
Alex Deucher37f90032010-06-11 17:58:38 -04001939 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04001940 if (crtc->fb) {
1941 int r;
1942 struct radeon_framebuffer *radeon_fb;
1943 struct radeon_bo *rbo;
1944
1945 radeon_fb = to_radeon_framebuffer(crtc->fb);
1946 rbo = gem_to_radeon_bo(radeon_fb->obj);
1947 r = radeon_bo_reserve(rbo, false);
1948 if (unlikely(r))
1949 DRM_ERROR("failed to reserve rbo before unpin\n");
1950 else {
1951 radeon_bo_unpin(rbo);
1952 radeon_bo_unreserve(rbo);
1953 }
1954 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04001955 /* disable the GRPH */
1956 if (ASIC_IS_DCE4(rdev))
1957 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
1958 else if (ASIC_IS_AVIVO(rdev))
1959 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
1960
Alex Deucher0e3d50b2013-02-05 11:47:09 -05001961 if (ASIC_IS_DCE6(rdev))
1962 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04001963
Alex Deucher4e585912012-08-21 19:06:21 -04001964 for (i = 0; i < rdev->num_crtc; i++) {
1965 if (rdev->mode_info.crtcs[i] &&
1966 rdev->mode_info.crtcs[i]->enabled &&
1967 i != radeon_crtc->crtc_id &&
1968 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
1969 /* one other crtc is using this pll don't turn
1970 * off the pll
1971 */
1972 goto done;
1973 }
1974 }
1975
Alex Deucher37f90032010-06-11 17:58:38 -04001976 switch (radeon_crtc->pll_id) {
1977 case ATOM_PPLL1:
1978 case ATOM_PPLL2:
1979 /* disable the ppll */
1980 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04001981 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04001982 break;
Alex Deucher64199872012-03-20 17:18:33 -04001983 case ATOM_PPLL0:
1984 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04001985 if ((rdev->family == CHIP_ARUBA) ||
1986 (rdev->family == CHIP_BONAIRE) ||
1987 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04001988 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1989 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
1990 break;
Alex Deucher37f90032010-06-11 17:58:38 -04001991 default:
1992 break;
1993 }
Alex Deucher4e585912012-08-21 19:06:21 -04001994done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04001995 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04001996 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04001997 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04001998 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04001999}
2000
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002001static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2002 .dpms = atombios_crtc_dpms,
2003 .mode_fixup = atombios_crtc_mode_fixup,
2004 .mode_set = atombios_crtc_mode_set,
2005 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002006 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002007 .prepare = atombios_crtc_prepare,
2008 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002009 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002010 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002011};
2012
2013void radeon_atombios_init_crtc(struct drm_device *dev,
2014 struct radeon_crtc *radeon_crtc)
2015{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002016 struct radeon_device *rdev = dev->dev_private;
2017
2018 if (ASIC_IS_DCE4(rdev)) {
2019 switch (radeon_crtc->crtc_id) {
2020 case 0:
2021 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002022 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002023 break;
2024 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002025 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002026 break;
2027 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002028 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002029 break;
2030 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002031 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002032 break;
2033 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002034 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002035 break;
2036 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002037 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002038 break;
2039 }
2040 } else {
2041 if (radeon_crtc->crtc_id == 1)
2042 radeon_crtc->crtc_offset =
2043 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2044 else
2045 radeon_crtc->crtc_offset = 0;
2046 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002047 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002048 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002049 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002050 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002051 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2052}