blob: 36156ea92a93c6bbc0c77071a12ad3cf57306f2d [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Alex Deucher5df31962012-09-13 11:52:08 -040086 struct radeon_encoder *radeon_encoder =
87 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020088 /* fixme - fill in enc_priv for atom dac */
89 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100090 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Alex Deucher5df31962012-09-13 11:52:08 -040095 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
96 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
97 tv_std = tv_dac->tv_std;
98 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 }
100
Jerome Glissec93bb852009-07-13 21:04:08 +0200101 memset(&args, 0, sizeof(args));
102
103 args.ucScaler = radeon_crtc->crtc_id;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200106 switch (tv_std) {
107 case TV_STD_NTSC:
108 default:
109 args.ucTVStandard = ATOM_TV_NTSC;
110 break;
111 case TV_STD_PAL:
112 args.ucTVStandard = ATOM_TV_PAL;
113 break;
114 case TV_STD_PAL_M:
115 args.ucTVStandard = ATOM_TV_PALM;
116 break;
117 case TV_STD_PAL_60:
118 args.ucTVStandard = ATOM_TV_PAL60;
119 break;
120 case TV_STD_NTSC_J:
121 args.ucTVStandard = ATOM_TV_NTSCJ;
122 break;
123 case TV_STD_SCART_PAL:
124 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
125 break;
126 case TV_STD_SECAM:
127 args.ucTVStandard = ATOM_TV_SECAM;
128 break;
129 case TV_STD_PAL_CN:
130 args.ucTVStandard = ATOM_TV_PALCN;
131 break;
132 }
133 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000134 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200135 args.ucTVStandard = ATOM_TV_CV;
136 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
137 } else {
138 switch (radeon_crtc->rmx_type) {
139 case RMX_FULL:
140 args.ucEnable = ATOM_SCALER_EXPANSION;
141 break;
142 case RMX_CENTER:
143 args.ucEnable = ATOM_SCALER_CENTER;
144 break;
145 case RMX_ASPECT:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 default:
149 if (ASIC_IS_AVIVO(rdev))
150 args.ucEnable = ATOM_SCALER_DISABLE;
151 else
152 args.ucEnable = ATOM_SCALER_CENTER;
153 break;
154 }
155 }
156 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000157 if ((is_tv || is_cv)
158 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
159 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200160 }
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
164{
165 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
166 struct drm_device *dev = crtc->dev;
167 struct radeon_device *rdev = dev->dev_private;
168 int index =
169 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
170 ENABLE_CRTC_PS_ALLOCATION args;
171
172 memset(&args, 0, sizeof(args));
173
174 args.ucCRTC = radeon_crtc->crtc_id;
175 args.ucEnable = lock;
176
177 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
178}
179
180static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
181{
182 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
183 struct drm_device *dev = crtc->dev;
184 struct radeon_device *rdev = dev->dev_private;
185 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = state;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
Alex Deucher78fe9e52014-01-28 23:49:37 -0500212static const u32 vga_control_regs[6] =
213{
214 AVIVO_D1VGA_CONTROL,
215 AVIVO_D2VGA_CONTROL,
216 EVERGREEN_D3VGA_CONTROL,
217 EVERGREEN_D4VGA_CONTROL,
218 EVERGREEN_D5VGA_CONTROL,
219 EVERGREEN_D6VGA_CONTROL,
220};
221
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
223{
224 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
225 struct drm_device *dev = crtc->dev;
226 struct radeon_device *rdev = dev->dev_private;
227 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
228 BLANK_CRTC_PS_ALLOCATION args;
Alex Deucher78fe9e52014-01-28 23:49:37 -0500229 u32 vga_control = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230
231 memset(&args, 0, sizeof(args));
232
Alex Deucher78fe9e52014-01-28 23:49:37 -0500233 if (ASIC_IS_DCE8(rdev)) {
234 vga_control = RREG32(vga_control_regs[radeon_crtc->crtc_id]);
235 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control | 1);
236 }
237
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238 args.ucCRTC = radeon_crtc->crtc_id;
239 args.ucBlanking = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucher78fe9e52014-01-28 23:49:37 -0500242
243 if (ASIC_IS_DCE8(rdev)) {
244 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control);
245 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200246}
247
Alex Deucherfef9f912012-03-20 17:18:03 -0400248static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
249{
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251 struct drm_device *dev = crtc->dev;
252 struct radeon_device *rdev = dev->dev_private;
253 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
254 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
255
256 memset(&args, 0, sizeof(args));
257
258 args.ucDispPipeId = radeon_crtc->crtc_id;
259 args.ucEnable = state;
260
261 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
262}
263
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
265{
266 struct drm_device *dev = crtc->dev;
267 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500268 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200269
270 switch (mode) {
271 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400272 radeon_crtc->enabled = true;
Alex Deucher37b43902010-02-09 12:04:43 -0500273 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400274 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500275 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
276 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400277 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500278 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279 break;
280 case DRM_MODE_DPMS_STANDBY:
281 case DRM_MODE_DPMS_SUSPEND:
282 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400283 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500284 if (radeon_crtc->enabled)
285 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400286 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500287 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
288 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400289 radeon_crtc->enabled = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290 break;
291 }
Alex Deucher3640da22014-05-30 12:40:15 -0400292 /* adjust pm to dpms */
293 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294}
295
296static void
297atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400298 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200299{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400300 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301 struct drm_device *dev = crtc->dev;
302 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400303 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200304 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400305 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200306
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400307 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400308 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400309 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400310 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
311 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400312 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400313 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400314 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400315 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316 args.usH_SyncWidth =
317 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
318 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400319 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400320 args.usV_SyncWidth =
321 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400322 args.ucH_Border = radeon_crtc->h_border;
323 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400324
325 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
326 misc |= ATOM_VSYNC_POLARITY;
327 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
328 misc |= ATOM_HSYNC_POLARITY;
329 if (mode->flags & DRM_MODE_FLAG_CSYNC)
330 misc |= ATOM_COMPOSITESYNC;
331 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
332 misc |= ATOM_INTERLACE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500333 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400334 misc |= ATOM_DOUBLE_CLOCK_MODE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500335 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
336 misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400337
338 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
339 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200340
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400341 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342}
343
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400344static void atombios_crtc_set_timing(struct drm_crtc *crtc,
345 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400347 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200348 struct drm_device *dev = crtc->dev;
349 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400350 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400352 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200353
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400354 memset(&args, 0, sizeof(args));
355 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
356 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
357 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
358 args.usH_SyncWidth =
359 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
360 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
361 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
362 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
363 args.usV_SyncWidth =
364 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
365
Alex Deucher54bfe492010-09-03 15:52:53 -0400366 args.ucOverscanRight = radeon_crtc->h_border;
367 args.ucOverscanLeft = radeon_crtc->h_border;
368 args.ucOverscanBottom = radeon_crtc->v_border;
369 args.ucOverscanTop = radeon_crtc->v_border;
370
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400371 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
372 misc |= ATOM_VSYNC_POLARITY;
373 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
374 misc |= ATOM_HSYNC_POLARITY;
375 if (mode->flags & DRM_MODE_FLAG_CSYNC)
376 misc |= ATOM_COMPOSITESYNC;
377 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
378 misc |= ATOM_INTERLACE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500379 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400380 misc |= ATOM_DOUBLE_CLOCK_MODE;
Alex Deucherfd99a092015-02-24 11:29:21 -0500381 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
382 misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400383
384 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
385 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400387 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388}
389
Alex Deucher3fa47d92012-01-20 14:56:39 -0500390static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500391{
Alex Deucherb7922102010-03-06 10:57:30 -0500392 u32 ss_cntl;
393
394 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500395 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500396 case ATOM_PPLL1:
397 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
398 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
399 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
400 break;
401 case ATOM_PPLL2:
402 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
403 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
404 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
405 break;
406 case ATOM_DCPLL:
407 case ATOM_PPLL_INVALID:
408 return;
409 }
410 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500411 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500412 case ATOM_PPLL1:
413 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
414 ss_cntl &= ~1;
415 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
416 break;
417 case ATOM_PPLL2:
418 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
419 ss_cntl &= ~1;
420 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
421 break;
422 case ATOM_DCPLL:
423 case ATOM_PPLL_INVALID:
424 return;
425 }
426 }
427}
428
429
Alex Deucher26b9fc32010-02-01 16:39:11 -0500430union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400431 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
432 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500433 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400434 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500435 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500436};
437
Alex Deucher3fa47d92012-01-20 14:56:39 -0500438static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400439 int enable,
440 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400441 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400442 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400443{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400444 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400445 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500446 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400447
Alex Deucherc4756ba2014-01-15 13:59:47 -0500448 if (enable) {
449 /* Don't mess with SS if percentage is 0 or external ss.
450 * SS is already disabled previously, and disabling it
451 * again can cause display problems if the pll is already
452 * programmed.
453 */
454 if (ss->percentage == 0)
455 return;
456 if (ss->type & ATOM_EXTERNAL_SS_MASK)
457 return;
458 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400459 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400460 if (rdev->mode_info.crtcs[i] &&
461 rdev->mode_info.crtcs[i]->enabled &&
462 i != crtc_id &&
463 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
464 /* one other crtc is using this pll don't turn
465 * off spread spectrum as it might turn off
466 * display on active crtc
467 */
468 return;
469 }
470 }
471 }
472
Alex Deucher26b9fc32010-02-01 16:39:11 -0500473 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400474
Alex Deuchera572eaa2011-01-06 21:19:16 -0500475 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500476 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400477 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500478 switch (pll_id) {
479 case ATOM_PPLL1:
480 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500481 break;
482 case ATOM_PPLL2:
483 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500484 break;
485 case ATOM_DCPLL:
486 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500487 break;
488 case ATOM_PPLL_INVALID:
489 return;
490 }
Alex Deucherf312f092012-07-17 14:02:44 -0400491 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
492 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400493 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500494 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400495 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400496 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400497 switch (pll_id) {
498 case ATOM_PPLL1:
499 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400500 break;
501 case ATOM_PPLL2:
502 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400503 break;
504 case ATOM_DCPLL:
505 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400506 break;
507 case ATOM_PPLL_INVALID:
508 return;
509 }
Alex Deucherf312f092012-07-17 14:02:44 -0400510 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
511 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400512 args.v2.ucEnable = enable;
513 } else if (ASIC_IS_DCE3(rdev)) {
514 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400515 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400516 args.v1.ucSpreadSpectrumStep = ss->step;
517 args.v1.ucSpreadSpectrumDelay = ss->delay;
518 args.v1.ucSpreadSpectrumRange = ss->range;
519 args.v1.ucPpll = pll_id;
520 args.v1.ucEnable = enable;
521 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400522 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
523 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500524 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400525 return;
526 }
527 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400528 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400529 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
530 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
531 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
532 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400533 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500534 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500535 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400536 return;
537 }
538 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400539 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400540 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
541 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
542 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400543 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500544 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400545}
546
Alex Deucher4eaeca32010-01-19 17:32:27 -0500547union adjust_pixel_clock {
548 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500549 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500550};
551
552static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400553 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200554{
Alex Deucher19eca432012-09-13 10:56:16 -0400555 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200556 struct drm_device *dev = crtc->dev;
557 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400558 struct drm_encoder *encoder = radeon_crtc->encoder;
559 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
560 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500561 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400562 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400563 u32 dp_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400564 u32 clock = mode->clock;
Alex Deucher7d5a33b2014-02-03 15:53:25 -0500565 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400566 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500567
Alex Deucher4eaeca32010-01-19 17:32:27 -0500568 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400569 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200570
571 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400572 if ((rdev->family == CHIP_RS600) ||
573 (rdev->family == CHIP_RS690) ||
574 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400575 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
576 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000577
578 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400579 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000580 else
Alex Deucher19eca432012-09-13 10:56:16 -0400581 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400582
Alex Deucher5785e532011-04-19 15:24:59 -0400583 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400584 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400585 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500586 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400587 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400588 /* use frac fb div on RS780/RS880 */
589 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
590 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500591 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
592 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000593 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400594 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200595
Dave Airlie5480f722010-10-19 10:36:47 +1000596 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400597 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000598 else
Alex Deucher19eca432012-09-13 10:56:16 -0400599 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000600 }
601
Alex Deucher5df31962012-09-13 11:52:08 -0400602 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
603 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
604 if (connector) {
605 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
606 struct radeon_connector_atom_dig *dig_connector =
607 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400608
Alex Deucher5df31962012-09-13 11:52:08 -0400609 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200610 }
611 }
612
Alex Deucher5df31962012-09-13 11:52:08 -0400613 /* use recommended ref_div for ss */
614 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
615 if (radeon_crtc->ss_enabled) {
616 if (radeon_crtc->ss.refdiv) {
617 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
618 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
619 if (ASIC_IS_AVIVO(rdev))
620 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
621 }
622 }
623 }
624
625 if (ASIC_IS_AVIVO(rdev)) {
626 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
627 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
628 adjusted_clock = mode->clock * 2;
629 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
630 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
631 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
632 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
633 } else {
634 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
635 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
636 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
637 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
638 }
639
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400640 /* adjust pll for deep color modes */
641 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
642 switch (bpc) {
643 case 8:
644 default:
645 break;
646 case 10:
647 clock = (clock * 5) / 4;
648 break;
649 case 12:
650 clock = (clock * 3) / 2;
651 break;
652 case 16:
653 clock = clock * 2;
654 break;
655 }
656 }
657
Alex Deucher2606c882009-10-08 13:36:21 -0400658 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
659 * accordingly based on the encoder/transmitter to work around
660 * special hw requirements.
661 */
662 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500663 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500664 u8 frev, crev;
665 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400666
Alex Deucher2606c882009-10-08 13:36:21 -0400667 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400668 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
669 &crev))
670 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500671
672 memset(&args, 0, sizeof(args));
673
674 switch (frev) {
675 case 1:
676 switch (crev) {
677 case 1:
678 case 2:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400679 args.v1.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500680 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500681 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400682 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400683 args.v1.ucConfig |=
684 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500685
686 atom_execute_table(rdev->mode_info.atom_context,
687 index, (uint32_t *)&args);
688 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
689 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500690 case 3:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400691 args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500692 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
693 args.v3.sInput.ucEncodeMode = encoder_mode;
694 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400695 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000696 args.v3.sInput.ucDispPllConfig |=
697 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400698 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400699 args.v3.sInput.ucDispPllConfig |=
700 DISPPLL_CONFIG_COHERENT_MODE;
701 /* 16200 or 27000 */
702 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
703 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500704 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400705 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500706 args.v3.sInput.ucDispPllConfig |=
707 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500708 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500709 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400710 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500711 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400712 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
713 ENCODER_OBJECT_ID_NONE)
714 args.v3.sInput.ucExtTransmitterID =
715 radeon_encoder_get_dp_bridge_encoder_id(encoder);
716 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400717 args.v3.sInput.ucExtTransmitterID = 0;
718
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500719 atom_execute_table(rdev->mode_info.atom_context,
720 index, (uint32_t *)&args);
721 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
722 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400723 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
724 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
725 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500726 }
727 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400728 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
729 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
730 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500731 }
732 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500733 default:
734 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
735 return adjusted_clock;
736 }
737 break;
738 default:
739 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
740 return adjusted_clock;
741 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400742 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500743 return adjusted_clock;
744}
745
746union set_pixel_clock {
747 SET_PIXEL_CLOCK_PS_ALLOCATION base;
748 PIXEL_CLOCK_PARAMETERS v1;
749 PIXEL_CLOCK_PARAMETERS_V2 v2;
750 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500751 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500752 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500753};
754
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500755/* on DCE5, make sure the voltage is high enough to support the
756 * required disp clk.
757 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400758static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500759 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500760{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500761 u8 frev, crev;
762 int index;
763 union set_pixel_clock args;
764
765 memset(&args, 0, sizeof(args));
766
767 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400768 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
769 &crev))
770 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500771
772 switch (frev) {
773 case 1:
774 switch (crev) {
775 case 5:
776 /* if the default dcpll clock is specified,
777 * SetPixelClock provides the dividers
778 */
779 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500780 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500781 args.v5.ucPpll = ATOM_DCPLL;
782 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500783 case 6:
784 /* if the default dcpll clock is specified,
785 * SetPixelClock provides the dividers
786 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500787 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400788 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400789 args.v6.ucPpll = ATOM_EXT_PLL1;
790 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400791 args.v6.ucPpll = ATOM_PPLL0;
792 else
793 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500794 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500795 default:
796 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
797 return;
798 }
799 break;
800 default:
801 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
802 return;
803 }
804 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
805}
806
Alex Deucher37f90032010-06-11 17:58:38 -0400807static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000808 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400809 int pll_id,
810 u32 encoder_mode,
811 u32 encoder_id,
812 u32 clock,
813 u32 ref_div,
814 u32 fb_div,
815 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400816 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400817 int bpc,
818 bool ss_enabled,
819 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400820{
821 struct drm_device *dev = crtc->dev;
822 struct radeon_device *rdev = dev->dev_private;
823 u8 frev, crev;
824 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
825 union set_pixel_clock args;
826
827 memset(&args, 0, sizeof(args));
828
829 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
830 &crev))
831 return;
832
833 switch (frev) {
834 case 1:
835 switch (crev) {
836 case 1:
837 if (clock == ATOM_DISABLE)
838 return;
839 args.v1.usPixelClock = cpu_to_le16(clock / 10);
840 args.v1.usRefDiv = cpu_to_le16(ref_div);
841 args.v1.usFbDiv = cpu_to_le16(fb_div);
842 args.v1.ucFracFbDiv = frac_fb_div;
843 args.v1.ucPostDiv = post_div;
844 args.v1.ucPpll = pll_id;
845 args.v1.ucCRTC = crtc_id;
846 args.v1.ucRefDivSrc = 1;
847 break;
848 case 2:
849 args.v2.usPixelClock = cpu_to_le16(clock / 10);
850 args.v2.usRefDiv = cpu_to_le16(ref_div);
851 args.v2.usFbDiv = cpu_to_le16(fb_div);
852 args.v2.ucFracFbDiv = frac_fb_div;
853 args.v2.ucPostDiv = post_div;
854 args.v2.ucPpll = pll_id;
855 args.v2.ucCRTC = crtc_id;
856 args.v2.ucRefDivSrc = 1;
857 break;
858 case 3:
859 args.v3.usPixelClock = cpu_to_le16(clock / 10);
860 args.v3.usRefDiv = cpu_to_le16(ref_div);
861 args.v3.usFbDiv = cpu_to_le16(fb_div);
862 args.v3.ucFracFbDiv = frac_fb_div;
863 args.v3.ucPostDiv = post_div;
864 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400865 if (crtc_id == ATOM_CRTC2)
866 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
867 else
868 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400869 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
870 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400871 args.v3.ucTransmitterId = encoder_id;
872 args.v3.ucEncoderMode = encoder_mode;
873 break;
874 case 5:
875 args.v5.ucCRTC = crtc_id;
876 args.v5.usPixelClock = cpu_to_le16(clock / 10);
877 args.v5.ucRefDiv = ref_div;
878 args.v5.usFbDiv = cpu_to_le16(fb_div);
879 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
880 args.v5.ucPostDiv = post_div;
881 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400882 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
883 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400884 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
885 switch (bpc) {
886 case 8:
887 default:
888 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
889 break;
890 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400891 /* yes this is correct, the atom define is wrong */
892 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
893 break;
894 case 12:
895 /* yes this is correct, the atom define is wrong */
Alex Deucher7d5ab302014-04-21 21:45:09 -0400896 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
897 break;
898 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400899 }
Alex Deucher37f90032010-06-11 17:58:38 -0400900 args.v5.ucTransmitterID = encoder_id;
901 args.v5.ucEncoderMode = encoder_mode;
902 args.v5.ucPpll = pll_id;
903 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500904 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000905 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500906 args.v6.ucRefDiv = ref_div;
907 args.v6.usFbDiv = cpu_to_le16(fb_div);
908 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
909 args.v6.ucPostDiv = post_div;
910 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400911 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
912 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400913 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
914 switch (bpc) {
915 case 8:
916 default:
917 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
918 break;
919 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400920 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400921 break;
922 case 12:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400923 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400924 break;
925 case 16:
926 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
927 break;
928 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400929 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500930 args.v6.ucTransmitterID = encoder_id;
931 args.v6.ucEncoderMode = encoder_mode;
932 args.v6.ucPpll = pll_id;
933 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400934 default:
935 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
936 return;
937 }
938 break;
939 default:
940 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
941 return;
942 }
943
944 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
945}
946
Alex Deucher19eca432012-09-13 10:56:16 -0400947static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
948{
949 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
950 struct drm_device *dev = crtc->dev;
951 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400952 struct radeon_encoder *radeon_encoder =
953 to_radeon_encoder(radeon_crtc->encoder);
954 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400955
956 radeon_crtc->bpc = 8;
957 radeon_crtc->ss_enabled = false;
958
Alex Deucher19eca432012-09-13 10:56:16 -0400959 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400960 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400961 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
962 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400963 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400964 struct radeon_connector *radeon_connector =
965 to_radeon_connector(connector);
966 struct radeon_connector_atom_dig *dig_connector =
967 radeon_connector->con_priv;
968 int dp_clock;
Mario Kleinerea292862014-06-05 09:58:24 -0400969
970 /* Assign mode clock for hdmi deep color max clock limit check */
971 radeon_connector->pixelclock_for_modeset = mode->clock;
Alex Deucher19eca432012-09-13 10:56:16 -0400972 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
973
974 switch (encoder_mode) {
975 case ATOM_ENCODER_MODE_DP_MST:
976 case ATOM_ENCODER_MODE_DP:
977 /* DP/eDP */
978 dp_clock = dig_connector->dp_clock / 10;
979 if (ASIC_IS_DCE4(rdev))
980 radeon_crtc->ss_enabled =
981 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
982 ASIC_INTERNAL_SS_ON_DP,
983 dp_clock);
984 else {
985 if (dp_clock == 16200) {
986 radeon_crtc->ss_enabled =
987 radeon_atombios_get_ppll_ss_info(rdev,
988 &radeon_crtc->ss,
989 ATOM_DP_SS_ID2);
990 if (!radeon_crtc->ss_enabled)
991 radeon_crtc->ss_enabled =
992 radeon_atombios_get_ppll_ss_info(rdev,
993 &radeon_crtc->ss,
994 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500995 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400996 radeon_crtc->ss_enabled =
997 radeon_atombios_get_ppll_ss_info(rdev,
998 &radeon_crtc->ss,
999 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -05001000 }
1001 /* disable spread spectrum on DCE3 DP */
1002 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -04001003 }
1004 break;
1005 case ATOM_ENCODER_MODE_LVDS:
1006 if (ASIC_IS_DCE4(rdev))
1007 radeon_crtc->ss_enabled =
1008 radeon_atombios_get_asic_ss_info(rdev,
1009 &radeon_crtc->ss,
1010 dig->lcd_ss_id,
1011 mode->clock / 10);
1012 else
1013 radeon_crtc->ss_enabled =
1014 radeon_atombios_get_ppll_ss_info(rdev,
1015 &radeon_crtc->ss,
1016 dig->lcd_ss_id);
1017 break;
1018 case ATOM_ENCODER_MODE_DVI:
1019 if (ASIC_IS_DCE4(rdev))
1020 radeon_crtc->ss_enabled =
1021 radeon_atombios_get_asic_ss_info(rdev,
1022 &radeon_crtc->ss,
1023 ASIC_INTERNAL_SS_ON_TMDS,
1024 mode->clock / 10);
1025 break;
1026 case ATOM_ENCODER_MODE_HDMI:
1027 if (ASIC_IS_DCE4(rdev))
1028 radeon_crtc->ss_enabled =
1029 radeon_atombios_get_asic_ss_info(rdev,
1030 &radeon_crtc->ss,
1031 ASIC_INTERNAL_SS_ON_HDMI,
1032 mode->clock / 10);
1033 break;
1034 default:
1035 break;
1036 }
1037 }
1038
1039 /* adjust pixel clock as needed */
1040 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1041
1042 return true;
1043}
1044
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001045static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001046{
1047 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1048 struct drm_device *dev = crtc->dev;
1049 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001050 struct radeon_encoder *radeon_encoder =
1051 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001052 u32 pll_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001053 u32 clock = mode->clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001054 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1055 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001056 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001057
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001058 /* pass the actual clock to atombios_crtc_program_pll for DCE5,6 for HDMI */
Mario Kleiner5c868222014-06-15 20:36:29 +02001059 if (ASIC_IS_DCE5(rdev) &&
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001060 (encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
1061 (radeon_crtc->bpc > 8))
1062 clock = radeon_crtc->adjusted_clock;
1063
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001064 switch (radeon_crtc->pll_id) {
1065 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001066 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001067 break;
1068 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001069 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001070 break;
1071 case ATOM_DCPLL:
1072 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001073 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001074 pll = &rdev->clock.dcpll;
1075 break;
1076 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001077
Alex Deucher19eca432012-09-13 10:56:16 -04001078 /* update pll params */
1079 pll->flags = radeon_crtc->pll_flags;
1080 pll->reference_div = radeon_crtc->pll_reference_div;
1081 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001082
Alex Deucher64146f82011-03-22 01:46:12 -04001083 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1084 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001085 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1086 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001087 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001088 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1089 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001090 else
Alex Deucher19eca432012-09-13 10:56:16 -04001091 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1092 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001093
Alex Deucher19eca432012-09-13 10:56:16 -04001094 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1095 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001096
Alex Deucher37f90032010-06-11 17:58:38 -04001097 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001098 encoder_mode, radeon_encoder->encoder_id, clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001099 ref_div, fb_div, frac_fb_div, post_div,
1100 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001101
Alex Deucher19eca432012-09-13 10:56:16 -04001102 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001103 /* calculate ss amount and step size */
1104 if (ASIC_IS_DCE4(rdev)) {
1105 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001106 u32 amount = (((fb_div * 10) + frac_fb_div) *
1107 (u32)radeon_crtc->ss.percentage) /
1108 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001109 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1110 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001111 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001112 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001113 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001114 (125 * 25 * pll->reference_freq / 100);
1115 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001116 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001117 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001118 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001119 }
1120
Alex Deucher19eca432012-09-13 10:56:16 -04001121 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1122 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001123 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001124}
1125
Alex Deucherc9417bd2011-02-06 14:23:26 -05001126static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1127 struct drm_framebuffer *fb,
1128 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001129{
1130 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1131 struct drm_device *dev = crtc->dev;
1132 struct radeon_device *rdev = dev->dev_private;
1133 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001134 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001135 struct drm_gem_object *obj;
1136 struct radeon_bo *rbo;
1137 uint64_t fb_location;
1138 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001139 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001140 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001141 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001142 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001143 bool bypass_lut = false;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001144
1145 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001146 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001147 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001148 return 0;
1149 }
1150
Chris Ball4dd19b02010-09-26 06:47:23 -05001151 if (atomic) {
1152 radeon_fb = to_radeon_framebuffer(fb);
1153 target_fb = fb;
1154 }
1155 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001156 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1157 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001158 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001159
Chris Ball4dd19b02010-09-26 06:47:23 -05001160 /* If atomic, assume fb object is pinned & idle & fenced and
1161 * just update base pointers
1162 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001163 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001164 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001165 r = radeon_bo_reserve(rbo, false);
1166 if (unlikely(r != 0))
1167 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001168
1169 if (atomic)
1170 fb_location = radeon_bo_gpu_offset(rbo);
1171 else {
1172 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1173 if (unlikely(r != 0)) {
1174 radeon_bo_unreserve(rbo);
1175 return -EINVAL;
1176 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001177 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001178
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001179 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1180 radeon_bo_unreserve(rbo);
1181
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001182 switch (target_fb->pixel_format) {
1183 case DRM_FORMAT_C8:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001184 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1185 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1186 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001187 case DRM_FORMAT_XRGB4444:
1188 case DRM_FORMAT_ARGB4444:
1189 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1190 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB4444));
1191#ifdef __BIG_ENDIAN
1192 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1193#endif
1194 break;
1195 case DRM_FORMAT_XRGB1555:
1196 case DRM_FORMAT_ARGB1555:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001197 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1198 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001199#ifdef __BIG_ENDIAN
1200 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1201#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001202 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001203 case DRM_FORMAT_BGRX5551:
1204 case DRM_FORMAT_BGRA5551:
1205 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1206 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA5551));
1207#ifdef __BIG_ENDIAN
1208 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1209#endif
1210 break;
1211 case DRM_FORMAT_RGB565:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001212 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1213 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001214#ifdef __BIG_ENDIAN
1215 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1216#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001217 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001218 case DRM_FORMAT_XRGB8888:
1219 case DRM_FORMAT_ARGB8888:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001220 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1221 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001222#ifdef __BIG_ENDIAN
1223 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1224#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001225 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001226 case DRM_FORMAT_XRGB2101010:
1227 case DRM_FORMAT_ARGB2101010:
1228 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1229 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB2101010));
1230#ifdef __BIG_ENDIAN
1231 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1232#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001233 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1234 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001235 break;
1236 case DRM_FORMAT_BGRX1010102:
1237 case DRM_FORMAT_BGRA1010102:
1238 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1239 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA1010102));
1240#ifdef __BIG_ENDIAN
1241 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1242#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001243 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1244 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001245 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001246 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001247 DRM_ERROR("Unsupported screen format %s\n",
1248 drm_get_format_name(target_fb->pixel_format));
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001249 return -EINVAL;
1250 }
1251
Alex Deucher392e3722011-11-28 14:49:27 -05001252 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001253 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001254
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001255 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001256 if (rdev->family >= CHIP_TAHITI) {
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001257 unsigned index, num_banks;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001258
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001259 if (rdev->family >= CHIP_BONAIRE) {
1260 unsigned tileb, tile_split_bytes;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001261
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001262 /* Calculate the macrotile mode index. */
1263 tile_split_bytes = 64 << tile_split;
1264 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1265 tileb = min(tile_split_bytes, tileb);
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001266
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001267 for (index = 0; tileb > 64; index++)
1268 tileb >>= 1;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001269
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001270 if (index >= 16) {
1271 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1272 target_fb->bits_per_pixel, tile_split);
1273 return -EINVAL;
1274 }
1275
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001276 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001277 } else {
1278 switch (target_fb->bits_per_pixel) {
1279 case 8:
1280 index = 10;
1281 break;
1282 case 16:
1283 index = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
1284 break;
1285 default:
1286 case 32:
1287 index = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
1288 break;
1289 }
1290
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001291 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001292 }
1293
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001294 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1295 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001296 /* NI and older. */
1297 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001298 tmp = rdev->config.cayman.tile_config;
1299 else
1300 tmp = rdev->config.evergreen.tile_config;
1301
1302 switch ((tmp & 0xf0) >> 4) {
1303 case 0: /* 4 banks */
1304 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1305 break;
1306 case 1: /* 8 banks */
1307 default:
1308 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1309 break;
1310 case 2: /* 16 banks */
1311 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1312 break;
1313 }
Alex Deucher392e3722011-11-28 14:49:27 -05001314 }
1315
Alex Deucher97d66322010-05-20 12:12:48 -04001316 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001317 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1318 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1319 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1320 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001321 if (rdev->family >= CHIP_BONAIRE) {
1322 /* XXX need to know more about the surface tiling mode */
1323 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1324 }
Alex Deucher392e3722011-11-28 14:49:27 -05001325 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001326 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1327
Alex Deucher8da0e502012-07-11 18:38:29 -04001328 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001329 /* Read the pipe config from the 2D TILED SCANOUT mode.
1330 * It should be the same for the other modes too, but not all
1331 * modes set the pipe config field. */
1332 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1333
1334 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001335 } else if ((rdev->family == CHIP_TAHITI) ||
1336 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001337 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001338 else if ((rdev->family == CHIP_VERDE) ||
1339 (rdev->family == CHIP_OLAND) ||
1340 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001341 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1342
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001343 switch (radeon_crtc->crtc_id) {
1344 case 0:
1345 WREG32(AVIVO_D1VGA_CONTROL, 0);
1346 break;
1347 case 1:
1348 WREG32(AVIVO_D2VGA_CONTROL, 0);
1349 break;
1350 case 2:
1351 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1352 break;
1353 case 3:
1354 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1355 break;
1356 case 4:
1357 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1358 break;
1359 case 5:
1360 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1361 break;
1362 default:
1363 break;
1364 }
1365
1366 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1367 upper_32_bits(fb_location));
1368 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1369 upper_32_bits(fb_location));
1370 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1371 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1372 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1373 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1374 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001375 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001376
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001377 /*
1378 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
1379 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
1380 * retain the full precision throughout the pipeline.
1381 */
1382 WREG32_P(EVERGREEN_GRPH_LUT_10BIT_BYPASS_CONTROL + radeon_crtc->crtc_offset,
1383 (bypass_lut ? EVERGREEN_LUT_10BIT_BYPASS_EN : 0),
1384 ~EVERGREEN_LUT_10BIT_BYPASS_EN);
1385
1386 if (bypass_lut)
1387 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1388
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001389 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1390 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1391 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1392 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001393 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1394 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001395
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001396 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001397 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1398 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1399
Alex Deucher8da0e502012-07-11 18:38:29 -04001400 if (rdev->family >= CHIP_BONAIRE)
1401 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1402 target_fb->height);
1403 else
1404 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1405 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001406 x &= ~3;
1407 y &= ~1;
1408 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1409 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001410 viewport_w = crtc->mode.hdisplay;
1411 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucher77ae5f42015-03-03 17:00:43 -05001412 if ((rdev->family >= CHIP_BONAIRE) &&
1413 (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE))
1414 viewport_h *= 2;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001415 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001416 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001417
Alex Deucherfb9674b2011-04-02 09:15:50 -04001418 /* pageflip setup */
1419 /* make sure flip is at vb rather than hb */
1420 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1421 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1422 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1423
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001424 /* set pageflip to happen only at start of vblank interval (front porch) */
1425 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001426
Matt Roperf4510a22014-04-01 15:22:40 -07001427 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001428 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001429 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001430 r = radeon_bo_reserve(rbo, false);
1431 if (unlikely(r != 0))
1432 return r;
1433 radeon_bo_unpin(rbo);
1434 radeon_bo_unreserve(rbo);
1435 }
1436
1437 /* Bytes per pixel may have changed */
1438 radeon_bandwidth_update(rdev);
1439
1440 return 0;
1441}
1442
Chris Ball4dd19b02010-09-26 06:47:23 -05001443static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1444 struct drm_framebuffer *fb,
1445 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001446{
1447 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1448 struct drm_device *dev = crtc->dev;
1449 struct radeon_device *rdev = dev->dev_private;
1450 struct radeon_framebuffer *radeon_fb;
1451 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001452 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001453 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001454 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001455 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001456 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001457 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001458 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001459 bool bypass_lut = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001460
Jerome Glisse2de3b482009-11-17 14:08:55 -08001461 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001462 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001463 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001464 return 0;
1465 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001466
Chris Ball4dd19b02010-09-26 06:47:23 -05001467 if (atomic) {
1468 radeon_fb = to_radeon_framebuffer(fb);
1469 target_fb = fb;
1470 }
1471 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001472 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1473 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001474 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001475
1476 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001477 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001478 r = radeon_bo_reserve(rbo, false);
1479 if (unlikely(r != 0))
1480 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001481
1482 /* If atomic, assume fb object is pinned & idle & fenced and
1483 * just update base pointers
1484 */
1485 if (atomic)
1486 fb_location = radeon_bo_gpu_offset(rbo);
1487 else {
1488 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1489 if (unlikely(r != 0)) {
1490 radeon_bo_unreserve(rbo);
1491 return -EINVAL;
1492 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001493 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001494 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1495 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001496
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001497 switch (target_fb->pixel_format) {
1498 case DRM_FORMAT_C8:
Dave Airlie41456df2009-09-16 10:15:21 +10001499 fb_format =
1500 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1501 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1502 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001503 case DRM_FORMAT_XRGB4444:
1504 case DRM_FORMAT_ARGB4444:
1505 fb_format =
1506 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1507 AVIVO_D1GRPH_CONTROL_16BPP_ARGB4444;
1508#ifdef __BIG_ENDIAN
1509 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1510#endif
1511 break;
1512 case DRM_FORMAT_XRGB1555:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001513 fb_format =
1514 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1515 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001516#ifdef __BIG_ENDIAN
1517 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1518#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001519 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001520 case DRM_FORMAT_RGB565:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001521 fb_format =
1522 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1523 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001524#ifdef __BIG_ENDIAN
1525 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1526#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001527 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001528 case DRM_FORMAT_XRGB8888:
1529 case DRM_FORMAT_ARGB8888:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001530 fb_format =
1531 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1532 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001533#ifdef __BIG_ENDIAN
1534 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1535#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001536 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001537 case DRM_FORMAT_XRGB2101010:
1538 case DRM_FORMAT_ARGB2101010:
1539 fb_format =
1540 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1541 AVIVO_D1GRPH_CONTROL_32BPP_ARGB2101010;
1542#ifdef __BIG_ENDIAN
1543 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1544#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001545 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1546 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001547 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001548 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001549 DRM_ERROR("Unsupported screen format %s\n",
1550 drm_get_format_name(target_fb->pixel_format));
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001551 return -EINVAL;
1552 }
1553
Alex Deucher40c4ac12010-05-20 12:04:59 -04001554 if (rdev->family >= CHIP_R600) {
1555 if (tiling_flags & RADEON_TILING_MACRO)
1556 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1557 else if (tiling_flags & RADEON_TILING_MICRO)
1558 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1559 } else {
1560 if (tiling_flags & RADEON_TILING_MACRO)
1561 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001562
Alex Deucher40c4ac12010-05-20 12:04:59 -04001563 if (tiling_flags & RADEON_TILING_MICRO)
1564 fb_format |= AVIVO_D1GRPH_TILED;
1565 }
Dave Airliee024e112009-06-24 09:48:08 +10001566
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001567 if (radeon_crtc->crtc_id == 0)
1568 WREG32(AVIVO_D1VGA_CONTROL, 0);
1569 else
1570 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001571
1572 if (rdev->family >= CHIP_RV770) {
1573 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001574 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1575 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001576 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001577 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1578 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001579 }
1580 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001581 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1582 (u32) fb_location);
1583 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1584 radeon_crtc->crtc_offset, (u32) fb_location);
1585 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001586 if (rdev->family >= CHIP_R600)
1587 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001588
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001589 /* LUT only has 256 slots for 8 bpc fb. Bypass for > 8 bpc scanout for precision */
1590 WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset,
1591 (bypass_lut ? AVIVO_LUT_10BIT_BYPASS_EN : 0), ~AVIVO_LUT_10BIT_BYPASS_EN);
1592
1593 if (bypass_lut)
1594 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1595
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001596 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1597 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1598 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1599 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001600 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1601 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001602
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001603 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001604 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1605 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1606
1607 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001608 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001609 x &= ~3;
1610 y &= ~1;
1611 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1612 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001613 viewport_w = crtc->mode.hdisplay;
1614 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001615 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001616 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001617
Alex Deucherfb9674b2011-04-02 09:15:50 -04001618 /* pageflip setup */
1619 /* make sure flip is at vb rather than hb */
1620 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1621 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1622 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1623
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001624 /* set pageflip to happen only at start of vblank interval (front porch) */
1625 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001626
Matt Roperf4510a22014-04-01 15:22:40 -07001627 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001628 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001629 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001630 r = radeon_bo_reserve(rbo, false);
1631 if (unlikely(r != 0))
1632 return r;
1633 radeon_bo_unpin(rbo);
1634 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001635 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001636
1637 /* Bytes per pixel may have changed */
1638 radeon_bandwidth_update(rdev);
1639
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001640 return 0;
1641}
1642
Alex Deucher54f088a2010-01-19 16:34:01 -05001643int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1644 struct drm_framebuffer *old_fb)
1645{
1646 struct drm_device *dev = crtc->dev;
1647 struct radeon_device *rdev = dev->dev_private;
1648
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001649 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001650 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001651 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001652 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001653 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001654 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1655}
1656
1657int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1658 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001659 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001660{
1661 struct drm_device *dev = crtc->dev;
1662 struct radeon_device *rdev = dev->dev_private;
1663
1664 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001665 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001666 else if (ASIC_IS_AVIVO(rdev))
1667 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1668 else
1669 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001670}
1671
Alex Deucher615e0cb2010-01-20 16:22:53 -05001672/* properly set additional regs when using atombios */
1673static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1674{
1675 struct drm_device *dev = crtc->dev;
1676 struct radeon_device *rdev = dev->dev_private;
1677 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1678 u32 disp_merge_cntl;
1679
1680 switch (radeon_crtc->crtc_id) {
1681 case 0:
1682 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1683 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1684 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1685 break;
1686 case 1:
1687 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1688 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1689 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1690 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1691 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1692 break;
1693 }
1694}
1695
Alex Deucherf3dd8502012-08-31 11:56:50 -04001696/**
1697 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1698 *
1699 * @crtc: drm crtc
1700 *
1701 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1702 */
1703static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1704{
1705 struct drm_device *dev = crtc->dev;
1706 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001707 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001708 u32 pll_in_use = 0;
1709
1710 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1711 if (crtc == test_crtc)
1712 continue;
1713
Alex Deucher57b35e22012-09-17 17:34:45 -04001714 test_radeon_crtc = to_radeon_crtc(test_crtc);
1715 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1716 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001717 }
1718 return pll_in_use;
1719}
1720
1721/**
1722 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1723 *
1724 * @crtc: drm crtc
1725 *
1726 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1727 * also in DP mode. For DP, a single PPLL can be used for all DP
1728 * crtcs/encoders.
1729 */
1730static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1731{
1732 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001733 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001734 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001735
Alex Deucher57b35e22012-09-17 17:34:45 -04001736 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1737 if (crtc == test_crtc)
1738 continue;
1739 test_radeon_crtc = to_radeon_crtc(test_crtc);
1740 if (test_radeon_crtc->encoder &&
1741 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1742 /* for DP use the same PLL for all */
1743 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1744 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001745 }
1746 }
1747 return ATOM_PPLL_INVALID;
1748}
1749
1750/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001751 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1752 *
1753 * @crtc: drm crtc
1754 * @encoder: drm encoder
1755 *
1756 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1757 * be shared (i.e., same clock).
1758 */
Alex Deucher5df31962012-09-13 11:52:08 -04001759static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001760{
Alex Deucher5df31962012-09-13 11:52:08 -04001761 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001762 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001763 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001764 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001765 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001766
Alex Deucher9642ac02012-09-13 12:43:41 -04001767 adjusted_clock = radeon_crtc->adjusted_clock;
1768
1769 if (adjusted_clock == 0)
1770 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001771
Alex Deucher57b35e22012-09-17 17:34:45 -04001772 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1773 if (crtc == test_crtc)
1774 continue;
1775 test_radeon_crtc = to_radeon_crtc(test_crtc);
1776 if (test_radeon_crtc->encoder &&
1777 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1778 /* check if we are already driving this connector with another crtc */
1779 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1780 /* if we are, return that pll */
1781 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001782 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001783 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001784 /* for non-DP check the clock */
1785 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1786 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1787 (adjusted_clock == test_adjusted_clock) &&
1788 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1789 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1790 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001791 }
1792 }
1793 return ATOM_PPLL_INVALID;
1794}
1795
1796/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001797 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1798 *
1799 * @crtc: drm crtc
1800 *
1801 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1802 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1803 * monitors a dedicated PPLL must be used. If a particular board has
1804 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1805 * as there is no need to program the PLL itself. If we are not able to
1806 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1807 * avoid messing up an existing monitor.
1808 *
1809 * Asic specific PLL information
1810 *
Alex Deucher0331f672012-09-14 11:57:21 -04001811 * DCE 8.x
1812 * KB/KV
1813 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1814 * CI
1815 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1816 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001817 * DCE 6.1
1818 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1819 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1820 *
1821 * DCE 6.0
1822 * - PPLL0 is available to all UNIPHY (DP only)
1823 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1824 *
1825 * DCE 5.0
1826 * - DCPLL is available to all UNIPHY (DP only)
1827 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1828 *
1829 * DCE 3.0/4.0/4.1
1830 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1831 *
1832 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001833static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1834{
Alex Deucher5df31962012-09-13 11:52:08 -04001835 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001836 struct drm_device *dev = crtc->dev;
1837 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001838 struct radeon_encoder *radeon_encoder =
1839 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001840 u32 pll_in_use;
1841 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001842
Alex Deucher0331f672012-09-14 11:57:21 -04001843 if (ASIC_IS_DCE8(rdev)) {
1844 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1845 if (rdev->clock.dp_extclk)
1846 /* skip PPLL programming if using ext clock */
1847 return ATOM_PPLL_INVALID;
1848 else {
1849 /* use the same PPLL for all DP monitors */
1850 pll = radeon_get_shared_dp_ppll(crtc);
1851 if (pll != ATOM_PPLL_INVALID)
1852 return pll;
1853 }
1854 } else {
1855 /* use the same PPLL for all monitors with the same clock */
1856 pll = radeon_get_shared_nondp_ppll(crtc);
1857 if (pll != ATOM_PPLL_INVALID)
1858 return pll;
1859 }
1860 /* otherwise, pick one of the plls */
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001861 if ((rdev->family == CHIP_KABINI) ||
Samuel Lib214f2a2014-04-30 18:40:53 -04001862 (rdev->family == CHIP_MULLINS)) {
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001863 /* KB/ML has PPLL1 and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001864 pll_in_use = radeon_get_pll_use_mask(crtc);
1865 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1866 return ATOM_PPLL2;
1867 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1868 return ATOM_PPLL1;
1869 DRM_ERROR("unable to allocate a PPLL\n");
1870 return ATOM_PPLL_INVALID;
1871 } else {
Alex Deucherfbedf1c2014-12-05 13:46:07 -05001872 /* CI/KV has PPLL0, PPLL1, and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001873 pll_in_use = radeon_get_pll_use_mask(crtc);
1874 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1875 return ATOM_PPLL2;
1876 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1877 return ATOM_PPLL1;
1878 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1879 return ATOM_PPLL0;
1880 DRM_ERROR("unable to allocate a PPLL\n");
1881 return ATOM_PPLL_INVALID;
1882 }
1883 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001884 struct radeon_encoder_atom_dig *dig =
1885 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001886
Alex Deucher5df31962012-09-13 11:52:08 -04001887 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1888 (dig->linkb == false))
1889 /* UNIPHY A uses PPLL2 */
1890 return ATOM_PPLL2;
1891 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1892 /* UNIPHY B/C/D/E/F */
1893 if (rdev->clock.dp_extclk)
1894 /* skip PPLL programming if using ext clock */
1895 return ATOM_PPLL_INVALID;
1896 else {
1897 /* use the same PPLL for all DP monitors */
1898 pll = radeon_get_shared_dp_ppll(crtc);
1899 if (pll != ATOM_PPLL_INVALID)
1900 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001901 }
Alex Deucher5df31962012-09-13 11:52:08 -04001902 } else {
1903 /* use the same PPLL for all monitors with the same clock */
1904 pll = radeon_get_shared_nondp_ppll(crtc);
1905 if (pll != ATOM_PPLL_INVALID)
1906 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001907 }
1908 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001909 pll_in_use = radeon_get_pll_use_mask(crtc);
1910 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001911 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001912 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1913 return ATOM_PPLL1;
1914 DRM_ERROR("unable to allocate a PPLL\n");
1915 return ATOM_PPLL_INVALID;
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001916 } else if (ASIC_IS_DCE41(rdev)) {
1917 /* Don't share PLLs on DCE4.1 chips */
1918 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1919 if (rdev->clock.dp_extclk)
1920 /* skip PPLL programming if using ext clock */
1921 return ATOM_PPLL_INVALID;
1922 }
1923 pll_in_use = radeon_get_pll_use_mask(crtc);
1924 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1925 return ATOM_PPLL1;
1926 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1927 return ATOM_PPLL2;
1928 DRM_ERROR("unable to allocate a PPLL\n");
1929 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001930 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001931 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1932 * depending on the asic:
1933 * DCE4: PPLL or ext clock
1934 * DCE5: PPLL, DCPLL, or ext clock
1935 * DCE6: PPLL, PPLL0, or ext clock
1936 *
1937 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1938 * PPLL/DCPLL programming and only program the DP DTO for the
1939 * crtc virtual pixel clock.
1940 */
1941 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1942 if (rdev->clock.dp_extclk)
1943 /* skip PPLL programming if using ext clock */
1944 return ATOM_PPLL_INVALID;
1945 else if (ASIC_IS_DCE6(rdev))
1946 /* use PPLL0 for all DP */
1947 return ATOM_PPLL0;
1948 else if (ASIC_IS_DCE5(rdev))
1949 /* use DCPLL for all DP */
1950 return ATOM_DCPLL;
1951 else {
1952 /* use the same PPLL for all DP monitors */
1953 pll = radeon_get_shared_dp_ppll(crtc);
1954 if (pll != ATOM_PPLL_INVALID)
1955 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001956 }
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001957 } else {
Alex Deucher5df31962012-09-13 11:52:08 -04001958 /* use the same PPLL for all monitors with the same clock */
1959 pll = radeon_get_shared_nondp_ppll(crtc);
1960 if (pll != ATOM_PPLL_INVALID)
1961 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001962 }
1963 /* all other cases */
1964 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001965 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1966 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001967 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1968 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001969 DRM_ERROR("unable to allocate a PPLL\n");
1970 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001971 } else {
1972 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001973 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1974 * the matching btw pll and crtc is done through
1975 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1976 * pll (1 or 2) to select which register to write. ie if using
1977 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1978 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1979 * choose which value to write. Which is reverse order from
1980 * register logic. So only case that works is when pllid is
1981 * same as crtcid or when both pll and crtc are enabled and
1982 * both use same clock.
1983 *
1984 * So just return crtc id as if crtc and pll were hard linked
1985 * together even if they aren't
1986 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001987 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001988 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001989}
1990
Alex Deucherf3f1f032012-03-20 17:18:04 -04001991void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001992{
1993 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001994 if (ASIC_IS_DCE6(rdev))
1995 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1996 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001997 struct radeon_atom_ss ss;
1998 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1999 ASIC_INTERNAL_SS_ON_DCPLL,
2000 rdev->clock.default_dispclk);
2001 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04002002 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002003 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04002004 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002005 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04002006 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002007 }
2008
2009}
2010
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002011int atombios_crtc_mode_set(struct drm_crtc *crtc,
2012 struct drm_display_mode *mode,
2013 struct drm_display_mode *adjusted_mode,
2014 int x, int y, struct drm_framebuffer *old_fb)
2015{
2016 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2017 struct drm_device *dev = crtc->dev;
2018 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04002019 struct radeon_encoder *radeon_encoder =
2020 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04002021 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002022
Alex Deucher5df31962012-09-13 11:52:08 -04002023 if (radeon_encoder->active_device &
2024 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
2025 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002026
Christian Königcde10122014-05-02 14:27:42 +02002027 if (!radeon_crtc->adjusted_clock)
2028 return -EINVAL;
2029
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002030 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002031
Alex Deucher54bfe492010-09-03 15:52:53 -04002032 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002033 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04002034 else if (ASIC_IS_AVIVO(rdev)) {
2035 if (is_tvcv)
2036 atombios_crtc_set_timing(crtc, adjusted_mode);
2037 else
2038 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
2039 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002040 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04002041 if (radeon_crtc->crtc_id == 0)
2042 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05002043 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002044 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002045 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02002046 atombios_overscan_setup(crtc, mode, adjusted_mode);
2047 atombios_scaler_setup(crtc);
Michel Dänzer6d3759f2014-11-21 11:48:57 +09002048 radeon_cursor_reset(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04002049 /* update the hw version fpr dpm */
2050 radeon_crtc->hw_mode = *adjusted_mode;
2051
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002052 return 0;
2053}
2054
2055static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02002056 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002057 struct drm_display_mode *adjusted_mode)
2058{
Alex Deucher5df31962012-09-13 11:52:08 -04002059 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2060 struct drm_device *dev = crtc->dev;
2061 struct drm_encoder *encoder;
2062
2063 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
2064 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2065 if (encoder->crtc == crtc) {
2066 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04002067 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04002068 break;
2069 }
2070 }
Alex Deucher57b35e22012-09-17 17:34:45 -04002071 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
2072 radeon_crtc->encoder = NULL;
2073 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04002074 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04002075 }
Alex Deucher643b1f52015-02-23 10:59:36 -05002076 if (radeon_crtc->encoder) {
2077 struct radeon_encoder *radeon_encoder =
2078 to_radeon_encoder(radeon_crtc->encoder);
2079
2080 radeon_crtc->output_csc = radeon_encoder->output_csc;
2081 }
Jerome Glissec93bb852009-07-13 21:04:08 +02002082 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2083 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04002084 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
2085 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04002086 /* pick pll */
2087 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
2088 /* if we can't get a PPLL for a non-DP encoder, fail */
2089 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
2090 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
2091 return false;
2092
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002093 return true;
2094}
2095
2096static void atombios_crtc_prepare(struct drm_crtc *crtc)
2097{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002098 struct drm_device *dev = crtc->dev;
2099 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05002100
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002101 /* disable crtc pair power gating before programming */
2102 if (ASIC_IS_DCE6(rdev))
2103 atombios_powergate_crtc(crtc, ATOM_DISABLE);
2104
Alex Deucher37b43902010-02-09 12:04:43 -05002105 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05002106 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002107}
2108
2109static void atombios_crtc_commit(struct drm_crtc *crtc)
2110{
2111 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05002112 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002113}
2114
Alex Deucher37f90032010-06-11 17:58:38 -04002115static void atombios_crtc_disable(struct drm_crtc *crtc)
2116{
2117 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04002118 struct drm_device *dev = crtc->dev;
2119 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002120 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04002121 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002122
Alex Deucher37f90032010-06-11 17:58:38 -04002123 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07002124 if (crtc->primary->fb) {
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002125 int r;
2126 struct radeon_framebuffer *radeon_fb;
2127 struct radeon_bo *rbo;
2128
Matt Roperf4510a22014-04-01 15:22:40 -07002129 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002130 rbo = gem_to_radeon_bo(radeon_fb->obj);
2131 r = radeon_bo_reserve(rbo, false);
2132 if (unlikely(r))
2133 DRM_ERROR("failed to reserve rbo before unpin\n");
2134 else {
2135 radeon_bo_unpin(rbo);
2136 radeon_bo_unreserve(rbo);
2137 }
2138 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04002139 /* disable the GRPH */
2140 if (ASIC_IS_DCE4(rdev))
2141 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2142 else if (ASIC_IS_AVIVO(rdev))
2143 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2144
Alex Deucher0e3d50b2013-02-05 11:47:09 -05002145 if (ASIC_IS_DCE6(rdev))
2146 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04002147
Alex Deucher4e585912012-08-21 19:06:21 -04002148 for (i = 0; i < rdev->num_crtc; i++) {
2149 if (rdev->mode_info.crtcs[i] &&
2150 rdev->mode_info.crtcs[i]->enabled &&
2151 i != radeon_crtc->crtc_id &&
2152 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
2153 /* one other crtc is using this pll don't turn
2154 * off the pll
2155 */
2156 goto done;
2157 }
2158 }
2159
Alex Deucher37f90032010-06-11 17:58:38 -04002160 switch (radeon_crtc->pll_id) {
2161 case ATOM_PPLL1:
2162 case ATOM_PPLL2:
2163 /* disable the ppll */
2164 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002165 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002166 break;
Alex Deucher64199872012-03-20 17:18:33 -04002167 case ATOM_PPLL0:
2168 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002169 if ((rdev->family == CHIP_ARUBA) ||
Alex Deucherfbedf1c2014-12-05 13:46:07 -05002170 (rdev->family == CHIP_KAVERI) ||
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002171 (rdev->family == CHIP_BONAIRE) ||
2172 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002173 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2174 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2175 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002176 default:
2177 break;
2178 }
Alex Deucher4e585912012-08-21 19:06:21 -04002179done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002180 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002181 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002182 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002183 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002184}
2185
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002186static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2187 .dpms = atombios_crtc_dpms,
2188 .mode_fixup = atombios_crtc_mode_fixup,
2189 .mode_set = atombios_crtc_mode_set,
2190 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002191 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002192 .prepare = atombios_crtc_prepare,
2193 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002194 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002195 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002196};
2197
2198void radeon_atombios_init_crtc(struct drm_device *dev,
2199 struct radeon_crtc *radeon_crtc)
2200{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002201 struct radeon_device *rdev = dev->dev_private;
2202
2203 if (ASIC_IS_DCE4(rdev)) {
2204 switch (radeon_crtc->crtc_id) {
2205 case 0:
2206 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002207 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002208 break;
2209 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002210 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002211 break;
2212 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002213 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002214 break;
2215 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002216 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002217 break;
2218 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002219 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002220 break;
2221 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002222 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002223 break;
2224 }
2225 } else {
2226 if (radeon_crtc->crtc_id == 1)
2227 radeon_crtc->crtc_offset =
2228 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2229 else
2230 radeon_crtc->crtc_offset = 0;
2231 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002232 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002233 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002234 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002235 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002236 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2237}