blob: d59ec491dbb9cba64d76369e62ef61b836c094d8 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/drm_crtc_helper.h>
28#include <drm/radeon_drm.h>
Ben Skeggs68adac52010-04-28 11:46:42 +100029#include <drm/drm_fixed.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
31#include "atom.h"
32#include "atom-bits.h"
33
Jerome Glissec93bb852009-07-13 21:04:08 +020034static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
37{
38 struct drm_device *dev = crtc->dev;
39 struct radeon_device *rdev = dev->dev_private;
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 SET_CRTC_OVERSCAN_PS_ALLOCATION args;
42 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
43 int a1, a2;
44
45 memset(&args, 0, sizeof(args));
46
Jerome Glissec93bb852009-07-13 21:04:08 +020047 args.ucCRTC = radeon_crtc->crtc_id;
48
49 switch (radeon_crtc->rmx_type) {
50 case RMX_CENTER:
Cédric Cano45894332011-02-11 19:45:37 -050051 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
52 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
53 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
54 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020055 break;
56 case RMX_ASPECT:
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
59
60 if (a1 > a2) {
Cédric Cano45894332011-02-11 19:45:37 -050061 args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
62 args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020063 } else if (a2 > a1) {
Alex Deucher942b0e92011-03-14 23:18:00 -040064 args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
65 args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
Jerome Glissec93bb852009-07-13 21:04:08 +020066 }
Jerome Glissec93bb852009-07-13 21:04:08 +020067 break;
68 case RMX_FULL:
69 default:
Cédric Cano45894332011-02-11 19:45:37 -050070 args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
71 args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
72 args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
73 args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
Jerome Glissec93bb852009-07-13 21:04:08 +020074 break;
75 }
Alex Deucher5b1714d2010-08-03 19:59:20 -040076 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glissec93bb852009-07-13 21:04:08 +020077}
78
79static void atombios_scaler_setup(struct drm_crtc *crtc)
80{
81 struct drm_device *dev = crtc->dev;
82 struct radeon_device *rdev = dev->dev_private;
83 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
84 ENABLE_SCALER_PS_ALLOCATION args;
85 int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
Alex Deucher5df31962012-09-13 11:52:08 -040086 struct radeon_encoder *radeon_encoder =
87 to_radeon_encoder(radeon_crtc->encoder);
Jerome Glissec93bb852009-07-13 21:04:08 +020088 /* fixme - fill in enc_priv for atom dac */
89 enum radeon_tv_std tv_std = TV_STD_NTSC;
Dave Airlie4ce001a2009-08-13 16:32:14 +100090 bool is_tv = false, is_cv = false;
Jerome Glissec93bb852009-07-13 21:04:08 +020091
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
93 return;
94
Alex Deucher5df31962012-09-13 11:52:08 -040095 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
96 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
97 tv_std = tv_dac->tv_std;
98 is_tv = true;
Dave Airlie4ce001a2009-08-13 16:32:14 +100099 }
100
Jerome Glissec93bb852009-07-13 21:04:08 +0200101 memset(&args, 0, sizeof(args));
102
103 args.ucScaler = radeon_crtc->crtc_id;
104
Dave Airlie4ce001a2009-08-13 16:32:14 +1000105 if (is_tv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200106 switch (tv_std) {
107 case TV_STD_NTSC:
108 default:
109 args.ucTVStandard = ATOM_TV_NTSC;
110 break;
111 case TV_STD_PAL:
112 args.ucTVStandard = ATOM_TV_PAL;
113 break;
114 case TV_STD_PAL_M:
115 args.ucTVStandard = ATOM_TV_PALM;
116 break;
117 case TV_STD_PAL_60:
118 args.ucTVStandard = ATOM_TV_PAL60;
119 break;
120 case TV_STD_NTSC_J:
121 args.ucTVStandard = ATOM_TV_NTSCJ;
122 break;
123 case TV_STD_SCART_PAL:
124 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
125 break;
126 case TV_STD_SECAM:
127 args.ucTVStandard = ATOM_TV_SECAM;
128 break;
129 case TV_STD_PAL_CN:
130 args.ucTVStandard = ATOM_TV_PALCN;
131 break;
132 }
133 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000134 } else if (is_cv) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200135 args.ucTVStandard = ATOM_TV_CV;
136 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
137 } else {
138 switch (radeon_crtc->rmx_type) {
139 case RMX_FULL:
140 args.ucEnable = ATOM_SCALER_EXPANSION;
141 break;
142 case RMX_CENTER:
143 args.ucEnable = ATOM_SCALER_CENTER;
144 break;
145 case RMX_ASPECT:
146 args.ucEnable = ATOM_SCALER_EXPANSION;
147 break;
148 default:
149 if (ASIC_IS_AVIVO(rdev))
150 args.ucEnable = ATOM_SCALER_DISABLE;
151 else
152 args.ucEnable = ATOM_SCALER_CENTER;
153 break;
154 }
155 }
156 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000157 if ((is_tv || is_cv)
158 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
159 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
Jerome Glissec93bb852009-07-13 21:04:08 +0200160 }
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
164{
165 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
166 struct drm_device *dev = crtc->dev;
167 struct radeon_device *rdev = dev->dev_private;
168 int index =
169 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
170 ENABLE_CRTC_PS_ALLOCATION args;
171
172 memset(&args, 0, sizeof(args));
173
174 args.ucCRTC = radeon_crtc->crtc_id;
175 args.ucEnable = lock;
176
177 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
178}
179
180static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
181{
182 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
183 struct drm_device *dev = crtc->dev;
184 struct radeon_device *rdev = dev->dev_private;
185 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
186 ENABLE_CRTC_PS_ALLOCATION args;
187
188 memset(&args, 0, sizeof(args));
189
190 args.ucCRTC = radeon_crtc->crtc_id;
191 args.ucEnable = state;
192
193 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
194}
195
196static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
197{
198 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
199 struct drm_device *dev = crtc->dev;
200 struct radeon_device *rdev = dev->dev_private;
201 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
202 ENABLE_CRTC_PS_ALLOCATION args;
203
204 memset(&args, 0, sizeof(args));
205
206 args.ucCRTC = radeon_crtc->crtc_id;
207 args.ucEnable = state;
208
209 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
210}
211
Alex Deucher78fe9e52014-01-28 23:49:37 -0500212static const u32 vga_control_regs[6] =
213{
214 AVIVO_D1VGA_CONTROL,
215 AVIVO_D2VGA_CONTROL,
216 EVERGREEN_D3VGA_CONTROL,
217 EVERGREEN_D4VGA_CONTROL,
218 EVERGREEN_D5VGA_CONTROL,
219 EVERGREEN_D6VGA_CONTROL,
220};
221
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
223{
224 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
225 struct drm_device *dev = crtc->dev;
226 struct radeon_device *rdev = dev->dev_private;
227 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
228 BLANK_CRTC_PS_ALLOCATION args;
Alex Deucher78fe9e52014-01-28 23:49:37 -0500229 u32 vga_control = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230
231 memset(&args, 0, sizeof(args));
232
Alex Deucher78fe9e52014-01-28 23:49:37 -0500233 if (ASIC_IS_DCE8(rdev)) {
234 vga_control = RREG32(vga_control_regs[radeon_crtc->crtc_id]);
235 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control | 1);
236 }
237
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238 args.ucCRTC = radeon_crtc->crtc_id;
239 args.ucBlanking = state;
240
241 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucher78fe9e52014-01-28 23:49:37 -0500242
243 if (ASIC_IS_DCE8(rdev)) {
244 WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control);
245 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200246}
247
Alex Deucherfef9f912012-03-20 17:18:03 -0400248static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
249{
250 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
251 struct drm_device *dev = crtc->dev;
252 struct radeon_device *rdev = dev->dev_private;
253 int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
254 ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
255
256 memset(&args, 0, sizeof(args));
257
258 args.ucDispPipeId = radeon_crtc->crtc_id;
259 args.ucEnable = state;
260
261 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
262}
263
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
265{
266 struct drm_device *dev = crtc->dev;
267 struct radeon_device *rdev = dev->dev_private;
Alex Deucher500b7582009-12-02 11:46:52 -0500268 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200269
270 switch (mode) {
271 case DRM_MODE_DPMS_ON:
Alex Deucherd7311172010-05-03 01:13:14 -0400272 radeon_crtc->enabled = true;
Alex Deucher37b43902010-02-09 12:04:43 -0500273 atombios_enable_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400274 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500275 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
276 atombios_blank_crtc(crtc, ATOM_DISABLE);
Alex Deucher45f9a392010-03-24 13:55:51 -0400277 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
Alex Deucher500b7582009-12-02 11:46:52 -0500278 radeon_crtc_load_lut(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279 break;
280 case DRM_MODE_DPMS_STANDBY:
281 case DRM_MODE_DPMS_SUSPEND:
282 case DRM_MODE_DPMS_OFF:
Alex Deucher45f9a392010-03-24 13:55:51 -0400283 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
Alex Deuchera93f3442010-12-20 11:22:29 -0500284 if (radeon_crtc->enabled)
285 atombios_blank_crtc(crtc, ATOM_ENABLE);
Alex Deucher79f17c62012-03-20 17:18:02 -0400286 if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
Alex Deucher37b43902010-02-09 12:04:43 -0500287 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
288 atombios_enable_crtc(crtc, ATOM_DISABLE);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400289 radeon_crtc->enabled = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290 break;
291 }
Alex Deucher3640da22014-05-30 12:40:15 -0400292 /* adjust pm to dpms */
293 radeon_pm_compute_clocks(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294}
295
296static void
297atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400298 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200299{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400300 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301 struct drm_device *dev = crtc->dev;
302 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400303 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200304 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400305 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200306
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400307 memset(&args, 0, sizeof(args));
Alex Deucher5b1714d2010-08-03 19:59:20 -0400308 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400309 args.usH_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400310 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
311 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400312 args.usV_Blanking_Time =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400313 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400314 args.usH_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400315 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400316 args.usH_SyncWidth =
317 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
318 args.usV_SyncOffset =
Alex Deucher5b1714d2010-08-03 19:59:20 -0400319 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400320 args.usV_SyncWidth =
321 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400322 args.ucH_Border = radeon_crtc->h_border;
323 args.ucV_Border = radeon_crtc->v_border;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400324
325 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
326 misc |= ATOM_VSYNC_POLARITY;
327 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
328 misc |= ATOM_HSYNC_POLARITY;
329 if (mode->flags & DRM_MODE_FLAG_CSYNC)
330 misc |= ATOM_COMPOSITESYNC;
331 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
332 misc |= ATOM_INTERLACE;
333 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
334 misc |= ATOM_DOUBLE_CLOCK_MODE;
335
336 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
337 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400339 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200340}
341
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400342static void atombios_crtc_set_timing(struct drm_crtc *crtc,
343 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200344{
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400345 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346 struct drm_device *dev = crtc->dev;
347 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400348 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200349 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400350 u16 misc = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400352 memset(&args, 0, sizeof(args));
353 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
354 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
355 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
356 args.usH_SyncWidth =
357 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
358 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
359 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
360 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
361 args.usV_SyncWidth =
362 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
363
Alex Deucher54bfe492010-09-03 15:52:53 -0400364 args.ucOverscanRight = radeon_crtc->h_border;
365 args.ucOverscanLeft = radeon_crtc->h_border;
366 args.ucOverscanBottom = radeon_crtc->v_border;
367 args.ucOverscanTop = radeon_crtc->v_border;
368
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400369 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
370 misc |= ATOM_VSYNC_POLARITY;
371 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
372 misc |= ATOM_HSYNC_POLARITY;
373 if (mode->flags & DRM_MODE_FLAG_CSYNC)
374 misc |= ATOM_COMPOSITESYNC;
375 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
376 misc |= ATOM_INTERLACE;
377 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
378 misc |= ATOM_DOUBLE_CLOCK_MODE;
379
380 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
381 args.ucCRTC = radeon_crtc->crtc_id;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382
Alex Deucher5a9bcac2009-10-08 15:09:31 -0400383 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384}
385
Alex Deucher3fa47d92012-01-20 14:56:39 -0500386static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
Alex Deucherb7922102010-03-06 10:57:30 -0500387{
Alex Deucherb7922102010-03-06 10:57:30 -0500388 u32 ss_cntl;
389
390 if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500391 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500392 case ATOM_PPLL1:
393 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
394 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
395 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
396 break;
397 case ATOM_PPLL2:
398 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
399 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
400 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
401 break;
402 case ATOM_DCPLL:
403 case ATOM_PPLL_INVALID:
404 return;
405 }
406 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500407 switch (pll_id) {
Alex Deucherb7922102010-03-06 10:57:30 -0500408 case ATOM_PPLL1:
409 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
410 ss_cntl &= ~1;
411 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
412 break;
413 case ATOM_PPLL2:
414 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
415 ss_cntl &= ~1;
416 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
417 break;
418 case ATOM_DCPLL:
419 case ATOM_PPLL_INVALID:
420 return;
421 }
422 }
423}
424
425
Alex Deucher26b9fc32010-02-01 16:39:11 -0500426union atom_enable_ss {
Alex Deucherba032a52010-10-04 17:13:01 -0400427 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
428 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500429 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
Alex Deucherba032a52010-10-04 17:13:01 -0400430 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500431 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
Alex Deucher26b9fc32010-02-01 16:39:11 -0500432};
433
Alex Deucher3fa47d92012-01-20 14:56:39 -0500434static void atombios_crtc_program_ss(struct radeon_device *rdev,
Alex Deucherba032a52010-10-04 17:13:01 -0400435 int enable,
436 int pll_id,
Jerome Glisse5efcc762012-08-17 14:40:04 -0400437 int crtc_id,
Alex Deucherba032a52010-10-04 17:13:01 -0400438 struct radeon_atom_ss *ss)
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400439{
Jerome Glisse5efcc762012-08-17 14:40:04 -0400440 unsigned i;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400441 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
Alex Deucher26b9fc32010-02-01 16:39:11 -0500442 union atom_enable_ss args;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400443
Alex Deucherc4756ba2014-01-15 13:59:47 -0500444 if (enable) {
445 /* Don't mess with SS if percentage is 0 or external ss.
446 * SS is already disabled previously, and disabling it
447 * again can cause display problems if the pll is already
448 * programmed.
449 */
450 if (ss->percentage == 0)
451 return;
452 if (ss->type & ATOM_EXTERNAL_SS_MASK)
453 return;
454 } else {
Alex Deucher53176702012-08-21 18:52:56 -0400455 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse5efcc762012-08-17 14:40:04 -0400456 if (rdev->mode_info.crtcs[i] &&
457 rdev->mode_info.crtcs[i]->enabled &&
458 i != crtc_id &&
459 pll_id == rdev->mode_info.crtcs[i]->pll_id) {
460 /* one other crtc is using this pll don't turn
461 * off spread spectrum as it might turn off
462 * display on active crtc
463 */
464 return;
465 }
466 }
467 }
468
Alex Deucher26b9fc32010-02-01 16:39:11 -0500469 memset(&args, 0, sizeof(args));
Alex Deucherba032a52010-10-04 17:13:01 -0400470
Alex Deuchera572eaa2011-01-06 21:19:16 -0500471 if (ASIC_IS_DCE5(rdev)) {
Cédric Cano45894332011-02-11 19:45:37 -0500472 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400473 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500474 switch (pll_id) {
475 case ATOM_PPLL1:
476 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500477 break;
478 case ATOM_PPLL2:
479 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500480 break;
481 case ATOM_DCPLL:
482 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500483 break;
484 case ATOM_PPLL_INVALID:
485 return;
486 }
Alex Deucherf312f092012-07-17 14:02:44 -0400487 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
488 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherd0ae3e82011-05-23 14:06:20 -0400489 args.v3.ucEnable = enable;
Alex Deuchera572eaa2011-01-06 21:19:16 -0500490 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucherba032a52010-10-04 17:13:01 -0400491 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400492 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400493 switch (pll_id) {
494 case ATOM_PPLL1:
495 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400496 break;
497 case ATOM_PPLL2:
498 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400499 break;
500 case ATOM_DCPLL:
501 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
Alex Deucherba032a52010-10-04 17:13:01 -0400502 break;
503 case ATOM_PPLL_INVALID:
504 return;
505 }
Alex Deucherf312f092012-07-17 14:02:44 -0400506 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
507 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
Alex Deucherba032a52010-10-04 17:13:01 -0400508 args.v2.ucEnable = enable;
509 } else if (ASIC_IS_DCE3(rdev)) {
510 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400511 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400512 args.v1.ucSpreadSpectrumStep = ss->step;
513 args.v1.ucSpreadSpectrumDelay = ss->delay;
514 args.v1.ucSpreadSpectrumRange = ss->range;
515 args.v1.ucPpll = pll_id;
516 args.v1.ucEnable = enable;
517 } else if (ASIC_IS_AVIVO(rdev)) {
Alex Deucher8e8e5232011-05-20 04:34:16 -0400518 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
519 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500520 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400521 return;
522 }
523 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400524 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400525 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
526 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
527 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
528 args.lvds_ss_2.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400529 } else {
Alex Deucherc4756ba2014-01-15 13:59:47 -0500530 if (enable == ATOM_DISABLE) {
Alex Deucher3fa47d92012-01-20 14:56:39 -0500531 atombios_disable_ss(rdev, pll_id);
Alex Deucherba032a52010-10-04 17:13:01 -0400532 return;
533 }
534 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
Alex Deucher8e8e5232011-05-20 04:34:16 -0400535 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
Alex Deucherba032a52010-10-04 17:13:01 -0400536 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
537 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
538 args.lvds_ss.ucEnable = enable;
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400539 }
Alex Deucher26b9fc32010-02-01 16:39:11 -0500540 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
Alex Deucherebbe1cb2009-10-16 11:15:25 -0400541}
542
Alex Deucher4eaeca32010-01-19 17:32:27 -0500543union adjust_pixel_clock {
544 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500545 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500546};
547
548static u32 atombios_adjust_pll(struct drm_crtc *crtc,
Alex Deucher19eca432012-09-13 10:56:16 -0400549 struct drm_display_mode *mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200550{
Alex Deucher19eca432012-09-13 10:56:16 -0400551 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 struct drm_device *dev = crtc->dev;
553 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400554 struct drm_encoder *encoder = radeon_crtc->encoder;
555 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
556 struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500557 u32 adjusted_clock = mode->clock;
Alex Deucher5df31962012-09-13 11:52:08 -0400558 int encoder_mode = atombios_get_encoder_mode(encoder);
Alex Deucherfbee67a2010-08-16 12:44:47 -0400559 u32 dp_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400560 u32 clock = mode->clock;
Alex Deucher7d5a33b2014-02-03 15:53:25 -0500561 int bpc = radeon_crtc->bpc;
Alex Deucher5df31962012-09-13 11:52:08 -0400562 bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
Alex Deucherfc103322010-01-19 17:16:10 -0500563
Alex Deucher4eaeca32010-01-19 17:32:27 -0500564 /* reset the pll flags */
Alex Deucher19eca432012-09-13 10:56:16 -0400565 radeon_crtc->pll_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200566
567 if (ASIC_IS_AVIVO(rdev)) {
Alex Deuchereb1300b2009-07-13 11:09:56 -0400568 if ((rdev->family == CHIP_RS600) ||
569 (rdev->family == CHIP_RS690) ||
570 (rdev->family == CHIP_RS740))
Alex Deucher19eca432012-09-13 10:56:16 -0400571 radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
572 RADEON_PLL_PREFER_CLOSEST_LOWER);
Dave Airlie5480f722010-10-19 10:36:47 +1000573
574 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400575 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000576 else
Alex Deucher19eca432012-09-13 10:56:16 -0400577 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Alex Deucher9bb09fa2011-04-07 10:31:25 -0400578
Alex Deucher5785e532011-04-19 15:24:59 -0400579 if (rdev->family < CHIP_RV770)
Alex Deucher19eca432012-09-13 10:56:16 -0400580 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
Alex Deucher37d41742012-04-19 10:48:38 -0400581 /* use frac fb div on APUs */
Alex Deucherc7d2f222012-12-18 22:11:51 -0500582 if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -0400583 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deucher41167822013-04-01 16:06:25 -0400584 /* use frac fb div on RS780/RS880 */
585 if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
586 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Alex Deuchera02dc742012-11-13 18:03:41 -0500587 if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
588 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000589 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400590 radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200591
Dave Airlie5480f722010-10-19 10:36:47 +1000592 if (mode->clock > 200000) /* range limits??? */
Alex Deucher19eca432012-09-13 10:56:16 -0400593 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000594 else
Alex Deucher19eca432012-09-13 10:56:16 -0400595 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
Dave Airlie5480f722010-10-19 10:36:47 +1000596 }
597
Alex Deucher5df31962012-09-13 11:52:08 -0400598 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
599 (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
600 if (connector) {
601 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
602 struct radeon_connector_atom_dig *dig_connector =
603 radeon_connector->con_priv;
Alex Deucherfbee67a2010-08-16 12:44:47 -0400604
Alex Deucher5df31962012-09-13 11:52:08 -0400605 dp_clock = dig_connector->dp_clock;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200606 }
607 }
608
Alex Deucher5df31962012-09-13 11:52:08 -0400609 /* use recommended ref_div for ss */
610 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
611 if (radeon_crtc->ss_enabled) {
612 if (radeon_crtc->ss.refdiv) {
613 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
614 radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
615 if (ASIC_IS_AVIVO(rdev))
616 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
617 }
618 }
619 }
620
621 if (ASIC_IS_AVIVO(rdev)) {
622 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
623 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
624 adjusted_clock = mode->clock * 2;
625 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
626 radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
627 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
628 radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
629 } else {
630 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
631 radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
632 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
633 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
634 }
635
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400636 /* adjust pll for deep color modes */
637 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
638 switch (bpc) {
639 case 8:
640 default:
641 break;
642 case 10:
643 clock = (clock * 5) / 4;
644 break;
645 case 12:
646 clock = (clock * 3) / 2;
647 break;
648 case 16:
649 clock = clock * 2;
650 break;
651 }
652 }
653
Alex Deucher2606c882009-10-08 13:36:21 -0400654 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
655 * accordingly based on the encoder/transmitter to work around
656 * special hw requirements.
657 */
658 if (ASIC_IS_DCE3(rdev)) {
Alex Deucher4eaeca32010-01-19 17:32:27 -0500659 union adjust_pixel_clock args;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500660 u8 frev, crev;
661 int index;
Alex Deucher2606c882009-10-08 13:36:21 -0400662
Alex Deucher2606c882009-10-08 13:36:21 -0400663 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400664 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
665 &crev))
666 return adjusted_clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500667
668 memset(&args, 0, sizeof(args));
669
670 switch (frev) {
671 case 1:
672 switch (crev) {
673 case 1:
674 case 2:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400675 args.v1.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucher4eaeca32010-01-19 17:32:27 -0500676 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500677 args.v1.ucEncodeMode = encoder_mode;
Alex Deucher19eca432012-09-13 10:56:16 -0400678 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherfbee67a2010-08-16 12:44:47 -0400679 args.v1.ucConfig |=
680 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500681
682 atom_execute_table(rdev->mode_info.atom_context,
683 index, (uint32_t *)&args);
684 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
685 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500686 case 3:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400687 args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500688 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
689 args.v3.sInput.ucEncodeMode = encoder_mode;
690 args.v3.sInput.ucDispPllConfig = 0;
Alex Deucher19eca432012-09-13 10:56:16 -0400691 if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
Alex Deucherb526ce22011-01-20 23:35:58 +0000692 args.v3.sInput.ucDispPllConfig |=
693 DISPPLL_CONFIG_SS_ENABLE;
Alex Deucher996d5c52011-10-26 15:59:50 -0400694 if (ENCODER_MODE_IS_DP(encoder_mode)) {
Alex Deucherb4f15f82011-10-25 11:34:51 -0400695 args.v3.sInput.ucDispPllConfig |=
696 DISPPLL_CONFIG_COHERENT_MODE;
697 /* 16200 or 27000 */
698 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
699 } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500700 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb4f15f82011-10-25 11:34:51 -0400701 if (dig->coherent_mode)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500702 args.v3.sInput.ucDispPllConfig |=
703 DISPPLL_CONFIG_COHERENT_MODE;
Alex Deucher9aa59992012-01-20 15:03:30 -0500704 if (is_duallink)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500705 args.v3.sInput.ucDispPllConfig |=
Alex Deucherb4f15f82011-10-25 11:34:51 -0400706 DISPPLL_CONFIG_DUAL_LINK;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500707 }
Alex Deucher1d33e1f2011-10-31 08:58:47 -0400708 if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
709 ENCODER_OBJECT_ID_NONE)
710 args.v3.sInput.ucExtTransmitterID =
711 radeon_encoder_get_dp_bridge_encoder_id(encoder);
712 else
Alex Deuchercc9f67a2011-06-16 10:06:16 -0400713 args.v3.sInput.ucExtTransmitterID = 0;
714
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500715 atom_execute_table(rdev->mode_info.atom_context,
716 index, (uint32_t *)&args);
717 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
718 if (args.v3.sOutput.ucRefDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400719 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
720 radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
721 radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500722 }
723 if (args.v3.sOutput.ucPostDiv) {
Alex Deucher19eca432012-09-13 10:56:16 -0400724 radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
725 radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
726 radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500727 }
728 break;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500729 default:
730 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
731 return adjusted_clock;
732 }
733 break;
734 default:
735 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
736 return adjusted_clock;
737 }
Alex Deucherd56ef9c2009-10-27 12:11:09 -0400738 }
Alex Deucher4eaeca32010-01-19 17:32:27 -0500739 return adjusted_clock;
740}
741
742union set_pixel_clock {
743 SET_PIXEL_CLOCK_PS_ALLOCATION base;
744 PIXEL_CLOCK_PARAMETERS v1;
745 PIXEL_CLOCK_PARAMETERS_V2 v2;
746 PIXEL_CLOCK_PARAMETERS_V3 v3;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500747 PIXEL_CLOCK_PARAMETERS_V5 v5;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500748 PIXEL_CLOCK_PARAMETERS_V6 v6;
Alex Deucher4eaeca32010-01-19 17:32:27 -0500749};
750
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500751/* on DCE5, make sure the voltage is high enough to support the
752 * required disp clk.
753 */
Alex Deucherf3f1f032012-03-20 17:18:04 -0400754static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500755 u32 dispclk)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500756{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500757 u8 frev, crev;
758 int index;
759 union set_pixel_clock args;
760
761 memset(&args, 0, sizeof(args));
762
763 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
Alex Deuchera084e6e2010-03-18 01:04:01 -0400764 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
765 &crev))
766 return;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500767
768 switch (frev) {
769 case 1:
770 switch (crev) {
771 case 5:
772 /* if the default dcpll clock is specified,
773 * SetPixelClock provides the dividers
774 */
775 args.v5.ucCRTC = ATOM_CRTC_INVALID;
Cédric Cano45894332011-02-11 19:45:37 -0500776 args.v5.usPixelClock = cpu_to_le16(dispclk);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500777 args.v5.ucPpll = ATOM_DCPLL;
778 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500779 case 6:
780 /* if the default dcpll clock is specified,
781 * SetPixelClock provides the dividers
782 */
Alex Deucher265aa6c2011-02-14 16:16:22 -0500783 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
Alex Deucher8542c122012-07-13 11:04:37 -0400784 if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
Alex Deucher729b95e2012-03-20 17:18:31 -0400785 args.v6.ucPpll = ATOM_EXT_PLL1;
786 else if (ASIC_IS_DCE6(rdev))
Alex Deucherf3f1f032012-03-20 17:18:04 -0400787 args.v6.ucPpll = ATOM_PPLL0;
788 else
789 args.v6.ucPpll = ATOM_DCPLL;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500790 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500791 default:
792 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
793 return;
794 }
795 break;
796 default:
797 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
798 return;
799 }
800 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
801}
802
Alex Deucher37f90032010-06-11 17:58:38 -0400803static void atombios_crtc_program_pll(struct drm_crtc *crtc,
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000804 u32 crtc_id,
Alex Deucher37f90032010-06-11 17:58:38 -0400805 int pll_id,
806 u32 encoder_mode,
807 u32 encoder_id,
808 u32 clock,
809 u32 ref_div,
810 u32 fb_div,
811 u32 frac_fb_div,
Alex Deucherdf271be2011-05-20 04:34:15 -0400812 u32 post_div,
Alex Deucher8e8e5232011-05-20 04:34:16 -0400813 int bpc,
814 bool ss_enabled,
815 struct radeon_atom_ss *ss)
Alex Deucher37f90032010-06-11 17:58:38 -0400816{
817 struct drm_device *dev = crtc->dev;
818 struct radeon_device *rdev = dev->dev_private;
819 u8 frev, crev;
820 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
821 union set_pixel_clock args;
822
823 memset(&args, 0, sizeof(args));
824
825 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
826 &crev))
827 return;
828
829 switch (frev) {
830 case 1:
831 switch (crev) {
832 case 1:
833 if (clock == ATOM_DISABLE)
834 return;
835 args.v1.usPixelClock = cpu_to_le16(clock / 10);
836 args.v1.usRefDiv = cpu_to_le16(ref_div);
837 args.v1.usFbDiv = cpu_to_le16(fb_div);
838 args.v1.ucFracFbDiv = frac_fb_div;
839 args.v1.ucPostDiv = post_div;
840 args.v1.ucPpll = pll_id;
841 args.v1.ucCRTC = crtc_id;
842 args.v1.ucRefDivSrc = 1;
843 break;
844 case 2:
845 args.v2.usPixelClock = cpu_to_le16(clock / 10);
846 args.v2.usRefDiv = cpu_to_le16(ref_div);
847 args.v2.usFbDiv = cpu_to_le16(fb_div);
848 args.v2.ucFracFbDiv = frac_fb_div;
849 args.v2.ucPostDiv = post_div;
850 args.v2.ucPpll = pll_id;
851 args.v2.ucCRTC = crtc_id;
852 args.v2.ucRefDivSrc = 1;
853 break;
854 case 3:
855 args.v3.usPixelClock = cpu_to_le16(clock / 10);
856 args.v3.usRefDiv = cpu_to_le16(ref_div);
857 args.v3.usFbDiv = cpu_to_le16(fb_div);
858 args.v3.ucFracFbDiv = frac_fb_div;
859 args.v3.ucPostDiv = post_div;
860 args.v3.ucPpll = pll_id;
Alex Deuchere7295862012-09-12 17:58:07 -0400861 if (crtc_id == ATOM_CRTC2)
862 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
863 else
864 args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
Alex Deucher6f15c502011-05-20 12:36:12 -0400865 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
866 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
Alex Deucher37f90032010-06-11 17:58:38 -0400867 args.v3.ucTransmitterId = encoder_id;
868 args.v3.ucEncoderMode = encoder_mode;
869 break;
870 case 5:
871 args.v5.ucCRTC = crtc_id;
872 args.v5.usPixelClock = cpu_to_le16(clock / 10);
873 args.v5.ucRefDiv = ref_div;
874 args.v5.usFbDiv = cpu_to_le16(fb_div);
875 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
876 args.v5.ucPostDiv = post_div;
877 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400878 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
879 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400880 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
881 switch (bpc) {
882 case 8:
883 default:
884 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
885 break;
886 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400887 /* yes this is correct, the atom define is wrong */
888 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
889 break;
890 case 12:
891 /* yes this is correct, the atom define is wrong */
Alex Deucher7d5ab302014-04-21 21:45:09 -0400892 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
893 break;
894 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400895 }
Alex Deucher37f90032010-06-11 17:58:38 -0400896 args.v5.ucTransmitterID = encoder_id;
897 args.v5.ucEncoderMode = encoder_mode;
898 args.v5.ucPpll = pll_id;
899 break;
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500900 case 6:
Benjamin Herrenschmidtf1bece72011-07-13 16:28:15 +1000901 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500902 args.v6.ucRefDiv = ref_div;
903 args.v6.usFbDiv = cpu_to_le16(fb_div);
904 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
905 args.v6.ucPostDiv = post_div;
906 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
Alex Deucher8e8e5232011-05-20 04:34:16 -0400907 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
908 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400909 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
910 switch (bpc) {
911 case 8:
912 default:
913 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
914 break;
915 case 10:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400916 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400917 break;
918 case 12:
Alex Deucherf71d9eb2014-04-21 22:09:19 -0400919 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
Alex Deucher7d5ab302014-04-21 21:45:09 -0400920 break;
921 case 16:
922 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
923 break;
924 }
Alex Deucherdf271be2011-05-20 04:34:15 -0400925 }
Alex Deucherf82b3dd2011-01-06 21:19:15 -0500926 args.v6.ucTransmitterID = encoder_id;
927 args.v6.ucEncoderMode = encoder_mode;
928 args.v6.ucPpll = pll_id;
929 break;
Alex Deucher37f90032010-06-11 17:58:38 -0400930 default:
931 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
932 return;
933 }
934 break;
935 default:
936 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
937 return;
938 }
939
940 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
941}
942
Alex Deucher19eca432012-09-13 10:56:16 -0400943static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
944{
945 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
946 struct drm_device *dev = crtc->dev;
947 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -0400948 struct radeon_encoder *radeon_encoder =
949 to_radeon_encoder(radeon_crtc->encoder);
950 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400951
952 radeon_crtc->bpc = 8;
953 radeon_crtc->ss_enabled = false;
954
Alex Deucher19eca432012-09-13 10:56:16 -0400955 if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
Alex Deucher5df31962012-09-13 11:52:08 -0400956 (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
Alex Deucher19eca432012-09-13 10:56:16 -0400957 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
958 struct drm_connector *connector =
Alex Deucher5df31962012-09-13 11:52:08 -0400959 radeon_get_connector_for_encoder(radeon_crtc->encoder);
Alex Deucher19eca432012-09-13 10:56:16 -0400960 struct radeon_connector *radeon_connector =
961 to_radeon_connector(connector);
962 struct radeon_connector_atom_dig *dig_connector =
963 radeon_connector->con_priv;
964 int dp_clock;
Mario Kleinerea292862014-06-05 09:58:24 -0400965
966 /* Assign mode clock for hdmi deep color max clock limit check */
967 radeon_connector->pixelclock_for_modeset = mode->clock;
Alex Deucher19eca432012-09-13 10:56:16 -0400968 radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
969
970 switch (encoder_mode) {
971 case ATOM_ENCODER_MODE_DP_MST:
972 case ATOM_ENCODER_MODE_DP:
973 /* DP/eDP */
974 dp_clock = dig_connector->dp_clock / 10;
975 if (ASIC_IS_DCE4(rdev))
976 radeon_crtc->ss_enabled =
977 radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
978 ASIC_INTERNAL_SS_ON_DP,
979 dp_clock);
980 else {
981 if (dp_clock == 16200) {
982 radeon_crtc->ss_enabled =
983 radeon_atombios_get_ppll_ss_info(rdev,
984 &radeon_crtc->ss,
985 ATOM_DP_SS_ID2);
986 if (!radeon_crtc->ss_enabled)
987 radeon_crtc->ss_enabled =
988 radeon_atombios_get_ppll_ss_info(rdev,
989 &radeon_crtc->ss,
990 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500991 } else {
Alex Deucher19eca432012-09-13 10:56:16 -0400992 radeon_crtc->ss_enabled =
993 radeon_atombios_get_ppll_ss_info(rdev,
994 &radeon_crtc->ss,
995 ATOM_DP_SS_ID1);
Alex Deucherd8e24522014-01-13 16:47:05 -0500996 }
997 /* disable spread spectrum on DCE3 DP */
998 radeon_crtc->ss_enabled = false;
Alex Deucher19eca432012-09-13 10:56:16 -0400999 }
1000 break;
1001 case ATOM_ENCODER_MODE_LVDS:
1002 if (ASIC_IS_DCE4(rdev))
1003 radeon_crtc->ss_enabled =
1004 radeon_atombios_get_asic_ss_info(rdev,
1005 &radeon_crtc->ss,
1006 dig->lcd_ss_id,
1007 mode->clock / 10);
1008 else
1009 radeon_crtc->ss_enabled =
1010 radeon_atombios_get_ppll_ss_info(rdev,
1011 &radeon_crtc->ss,
1012 dig->lcd_ss_id);
1013 break;
1014 case ATOM_ENCODER_MODE_DVI:
1015 if (ASIC_IS_DCE4(rdev))
1016 radeon_crtc->ss_enabled =
1017 radeon_atombios_get_asic_ss_info(rdev,
1018 &radeon_crtc->ss,
1019 ASIC_INTERNAL_SS_ON_TMDS,
1020 mode->clock / 10);
1021 break;
1022 case ATOM_ENCODER_MODE_HDMI:
1023 if (ASIC_IS_DCE4(rdev))
1024 radeon_crtc->ss_enabled =
1025 radeon_atombios_get_asic_ss_info(rdev,
1026 &radeon_crtc->ss,
1027 ASIC_INTERNAL_SS_ON_HDMI,
1028 mode->clock / 10);
1029 break;
1030 default:
1031 break;
1032 }
1033 }
1034
1035 /* adjust pixel clock as needed */
1036 radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
1037
1038 return true;
1039}
1040
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001041static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
Alex Deucher4eaeca32010-01-19 17:32:27 -05001042{
1043 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1044 struct drm_device *dev = crtc->dev;
1045 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001046 struct radeon_encoder *radeon_encoder =
1047 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001048 u32 pll_clock = mode->clock;
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001049 u32 clock = mode->clock;
Alex Deucher4eaeca32010-01-19 17:32:27 -05001050 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
1051 struct radeon_pll *pll;
Alex Deucher5df31962012-09-13 11:52:08 -04001052 int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
Alex Deucher4eaeca32010-01-19 17:32:27 -05001053
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001054 /* pass the actual clock to atombios_crtc_program_pll for DCE5,6 for HDMI */
Mario Kleiner5c868222014-06-15 20:36:29 +02001055 if (ASIC_IS_DCE5(rdev) &&
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001056 (encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
1057 (radeon_crtc->bpc > 8))
1058 clock = radeon_crtc->adjusted_clock;
1059
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001060 switch (radeon_crtc->pll_id) {
1061 case ATOM_PPLL1:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001062 pll = &rdev->clock.p1pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001063 break;
1064 case ATOM_PPLL2:
Alex Deucher4eaeca32010-01-19 17:32:27 -05001065 pll = &rdev->clock.p2pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001066 break;
1067 case ATOM_DCPLL:
1068 case ATOM_PPLL_INVALID:
Stefan Richter921d98b2010-05-26 10:27:44 +10001069 default:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001070 pll = &rdev->clock.dcpll;
1071 break;
1072 }
Alex Deucher4eaeca32010-01-19 17:32:27 -05001073
Alex Deucher19eca432012-09-13 10:56:16 -04001074 /* update pll params */
1075 pll->flags = radeon_crtc->pll_flags;
1076 pll->reference_div = radeon_crtc->pll_reference_div;
1077 pll->post_div = radeon_crtc->pll_post_div;
Alex Deucher2606c882009-10-08 13:36:21 -04001078
Alex Deucher64146f82011-03-22 01:46:12 -04001079 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1080 /* TV seems to prefer the legacy algo on some boards */
Alex Deucher19eca432012-09-13 10:56:16 -04001081 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1082 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher64146f82011-03-22 01:46:12 -04001083 else if (ASIC_IS_AVIVO(rdev))
Alex Deucher19eca432012-09-13 10:56:16 -04001084 radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
1085 &fb_div, &frac_fb_div, &ref_div, &post_div);
Alex Deucher619efb12011-01-31 16:48:53 -05001086 else
Alex Deucher19eca432012-09-13 10:56:16 -04001087 radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
1088 &fb_div, &frac_fb_div, &ref_div, &post_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001089
Alex Deucher19eca432012-09-13 10:56:16 -04001090 atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
1091 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001092
Alex Deucher37f90032010-06-11 17:58:38 -04001093 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucherf71d9eb2014-04-21 22:09:19 -04001094 encoder_mode, radeon_encoder->encoder_id, clock,
Alex Deucher19eca432012-09-13 10:56:16 -04001095 ref_div, fb_div, frac_fb_div, post_div,
1096 radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001097
Alex Deucher19eca432012-09-13 10:56:16 -04001098 if (radeon_crtc->ss_enabled) {
Alex Deucherba032a52010-10-04 17:13:01 -04001099 /* calculate ss amount and step size */
1100 if (ASIC_IS_DCE4(rdev)) {
1101 u32 step_size;
Alex Deucher18f8f522014-01-15 13:41:31 -05001102 u32 amount = (((fb_div * 10) + frac_fb_div) *
1103 (u32)radeon_crtc->ss.percentage) /
1104 (100 * (u32)radeon_crtc->ss.percentage_divider);
Alex Deucher19eca432012-09-13 10:56:16 -04001105 radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1106 radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
Alex Deucherba032a52010-10-04 17:13:01 -04001107 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
Alex Deucher19eca432012-09-13 10:56:16 -04001108 if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
Alex Deucher18f8f522014-01-15 13:41:31 -05001109 step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001110 (125 * 25 * pll->reference_freq / 100);
1111 else
Alex Deucher18f8f522014-01-15 13:41:31 -05001112 step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
Alex Deucherba032a52010-10-04 17:13:01 -04001113 (125 * 25 * pll->reference_freq / 100);
Alex Deucher19eca432012-09-13 10:56:16 -04001114 radeon_crtc->ss.step = step_size;
Alex Deucherba032a52010-10-04 17:13:01 -04001115 }
1116
Alex Deucher19eca432012-09-13 10:56:16 -04001117 atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
1118 radeon_crtc->crtc_id, &radeon_crtc->ss);
Alex Deucherba032a52010-10-04 17:13:01 -04001119 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001120}
1121
Alex Deucherc9417bd2011-02-06 14:23:26 -05001122static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1123 struct drm_framebuffer *fb,
1124 int x, int y, int atomic)
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001125{
1126 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1127 struct drm_device *dev = crtc->dev;
1128 struct radeon_device *rdev = dev->dev_private;
1129 struct radeon_framebuffer *radeon_fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001130 struct drm_framebuffer *target_fb;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001131 struct drm_gem_object *obj;
1132 struct radeon_bo *rbo;
1133 uint64_t fb_location;
1134 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Jerome Glisse285484e2011-12-16 17:03:42 -05001135 unsigned bankw, bankh, mtaspect, tile_split;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001136 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
Alex Deucheradcfde52011-05-27 10:05:03 -04001137 u32 tmp, viewport_w, viewport_h;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001138 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001139 bool bypass_lut = false;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001140
1141 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001142 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001143 DRM_DEBUG_KMS("No FB bound\n");
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001144 return 0;
1145 }
1146
Chris Ball4dd19b02010-09-26 06:47:23 -05001147 if (atomic) {
1148 radeon_fb = to_radeon_framebuffer(fb);
1149 target_fb = fb;
1150 }
1151 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001152 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1153 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001154 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001155
Chris Ball4dd19b02010-09-26 06:47:23 -05001156 /* If atomic, assume fb object is pinned & idle & fenced and
1157 * just update base pointers
1158 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001159 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001160 rbo = gem_to_radeon_bo(obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001161 r = radeon_bo_reserve(rbo, false);
1162 if (unlikely(r != 0))
1163 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001164
1165 if (atomic)
1166 fb_location = radeon_bo_gpu_offset(rbo);
1167 else {
1168 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1169 if (unlikely(r != 0)) {
1170 radeon_bo_unreserve(rbo);
1171 return -EINVAL;
1172 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001173 }
Chris Ball4dd19b02010-09-26 06:47:23 -05001174
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001175 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1176 radeon_bo_unreserve(rbo);
1177
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001178 switch (target_fb->pixel_format) {
1179 case DRM_FORMAT_C8:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001180 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1181 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1182 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001183 case DRM_FORMAT_XRGB4444:
1184 case DRM_FORMAT_ARGB4444:
1185 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1186 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB4444));
1187#ifdef __BIG_ENDIAN
1188 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1189#endif
1190 break;
1191 case DRM_FORMAT_XRGB1555:
1192 case DRM_FORMAT_ARGB1555:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001193 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1194 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001195#ifdef __BIG_ENDIAN
1196 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1197#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001198 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001199 case DRM_FORMAT_BGRX5551:
1200 case DRM_FORMAT_BGRA5551:
1201 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1202 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA5551));
1203#ifdef __BIG_ENDIAN
1204 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1205#endif
1206 break;
1207 case DRM_FORMAT_RGB565:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001208 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1209 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001210#ifdef __BIG_ENDIAN
1211 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1212#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001213 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001214 case DRM_FORMAT_XRGB8888:
1215 case DRM_FORMAT_ARGB8888:
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001216 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1217 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
Alex Deucherfa6bee42011-01-25 11:55:50 -05001218#ifdef __BIG_ENDIAN
1219 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1220#endif
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001221 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001222 case DRM_FORMAT_XRGB2101010:
1223 case DRM_FORMAT_ARGB2101010:
1224 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1225 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB2101010));
1226#ifdef __BIG_ENDIAN
1227 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1228#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001229 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1230 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001231 break;
1232 case DRM_FORMAT_BGRX1010102:
1233 case DRM_FORMAT_BGRA1010102:
1234 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1235 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA1010102));
1236#ifdef __BIG_ENDIAN
1237 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1238#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001239 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1240 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001241 break;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001242 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001243 DRM_ERROR("Unsupported screen format %s\n",
1244 drm_get_format_name(target_fb->pixel_format));
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001245 return -EINVAL;
1246 }
1247
Alex Deucher392e3722011-11-28 14:49:27 -05001248 if (tiling_flags & RADEON_TILING_MACRO) {
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001249 evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
Alex Deucher392e3722011-11-28 14:49:27 -05001250
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001251 /* Set NUM_BANKS. */
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001252 if (rdev->family >= CHIP_TAHITI) {
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001253 unsigned index, num_banks;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001254
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001255 if (rdev->family >= CHIP_BONAIRE) {
1256 unsigned tileb, tile_split_bytes;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001257
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001258 /* Calculate the macrotile mode index. */
1259 tile_split_bytes = 64 << tile_split;
1260 tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
1261 tileb = min(tile_split_bytes, tileb);
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001262
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001263 for (index = 0; tileb > 64; index++)
1264 tileb >>= 1;
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001265
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001266 if (index >= 16) {
1267 DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
1268 target_fb->bits_per_pixel, tile_split);
1269 return -EINVAL;
1270 }
1271
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001272 num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001273 } else {
1274 switch (target_fb->bits_per_pixel) {
1275 case 8:
1276 index = 10;
1277 break;
1278 case 16:
1279 index = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
1280 break;
1281 default:
1282 case 32:
1283 index = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
1284 break;
1285 }
1286
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001287 num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
Michel Dänzere9d14ae2014-04-22 16:53:52 +09001288 }
1289
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001290 fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
1291 } else {
Alex Deucher6d8ea7d2014-02-17 14:16:31 -05001292 /* NI and older. */
1293 if (rdev->family >= CHIP_CAYMAN)
Marek Olšáke3ea94a2013-12-23 17:11:36 +01001294 tmp = rdev->config.cayman.tile_config;
1295 else
1296 tmp = rdev->config.evergreen.tile_config;
1297
1298 switch ((tmp & 0xf0) >> 4) {
1299 case 0: /* 4 banks */
1300 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
1301 break;
1302 case 1: /* 8 banks */
1303 default:
1304 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
1305 break;
1306 case 2: /* 16 banks */
1307 fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
1308 break;
1309 }
Alex Deucher392e3722011-11-28 14:49:27 -05001310 }
1311
Alex Deucher97d66322010-05-20 12:12:48 -04001312 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001313 fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
1314 fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
1315 fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
1316 fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
Alex Deucher8da0e502012-07-11 18:38:29 -04001317 if (rdev->family >= CHIP_BONAIRE) {
1318 /* XXX need to know more about the surface tiling mode */
1319 fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
1320 }
Alex Deucher392e3722011-11-28 14:49:27 -05001321 } else if (tiling_flags & RADEON_TILING_MICRO)
Alex Deucher97d66322010-05-20 12:12:48 -04001322 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1323
Alex Deucher8da0e502012-07-11 18:38:29 -04001324 if (rdev->family >= CHIP_BONAIRE) {
Marek Olšák35a90522013-12-23 17:11:35 +01001325 /* Read the pipe config from the 2D TILED SCANOUT mode.
1326 * It should be the same for the other modes too, but not all
1327 * modes set the pipe config field. */
1328 u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
1329
1330 fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
Alex Deucher8da0e502012-07-11 18:38:29 -04001331 } else if ((rdev->family == CHIP_TAHITI) ||
1332 (rdev->family == CHIP_PITCAIRN))
Alex Deucherb7019b22012-06-14 15:58:25 -04001333 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
Alex Deucher227ae102013-12-11 11:43:58 -05001334 else if ((rdev->family == CHIP_VERDE) ||
1335 (rdev->family == CHIP_OLAND) ||
1336 (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
Alex Deucherb7019b22012-06-14 15:58:25 -04001337 fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
1338
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001339 switch (radeon_crtc->crtc_id) {
1340 case 0:
1341 WREG32(AVIVO_D1VGA_CONTROL, 0);
1342 break;
1343 case 1:
1344 WREG32(AVIVO_D2VGA_CONTROL, 0);
1345 break;
1346 case 2:
1347 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1348 break;
1349 case 3:
1350 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1351 break;
1352 case 4:
1353 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1354 break;
1355 case 5:
1356 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1357 break;
1358 default:
1359 break;
1360 }
1361
1362 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1363 upper_32_bits(fb_location));
1364 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1365 upper_32_bits(fb_location));
1366 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1367 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1368 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1369 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1370 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001371 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001372
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001373 /*
1374 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
1375 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
1376 * retain the full precision throughout the pipeline.
1377 */
1378 WREG32_P(EVERGREEN_GRPH_LUT_10BIT_BYPASS_CONTROL + radeon_crtc->crtc_offset,
1379 (bypass_lut ? EVERGREEN_LUT_10BIT_BYPASS_EN : 0),
1380 ~EVERGREEN_LUT_10BIT_BYPASS_EN);
1381
1382 if (bypass_lut)
1383 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1384
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001385 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1386 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1387 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1388 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001389 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1390 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001391
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001392 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001393 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1394 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1395
Alex Deucher8da0e502012-07-11 18:38:29 -04001396 if (rdev->family >= CHIP_BONAIRE)
1397 WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1398 target_fb->height);
1399 else
1400 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1401 target_fb->height);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001402 x &= ~3;
1403 y &= ~1;
1404 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1405 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001406 viewport_w = crtc->mode.hdisplay;
1407 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001408 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001409 (viewport_w << 16) | viewport_h);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001410
Alex Deucherfb9674b2011-04-02 09:15:50 -04001411 /* pageflip setup */
1412 /* make sure flip is at vb rather than hb */
1413 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1414 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1415 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1416
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001417 /* set pageflip to happen only at start of vblank interval (front porch) */
1418 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001419
Matt Roperf4510a22014-04-01 15:22:40 -07001420 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001421 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001422 rbo = gem_to_radeon_bo(radeon_fb->obj);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001423 r = radeon_bo_reserve(rbo, false);
1424 if (unlikely(r != 0))
1425 return r;
1426 radeon_bo_unpin(rbo);
1427 radeon_bo_unreserve(rbo);
1428 }
1429
1430 /* Bytes per pixel may have changed */
1431 radeon_bandwidth_update(rdev);
1432
1433 return 0;
1434}
1435
Chris Ball4dd19b02010-09-26 06:47:23 -05001436static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1437 struct drm_framebuffer *fb,
1438 int x, int y, int atomic)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001439{
1440 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1441 struct drm_device *dev = crtc->dev;
1442 struct radeon_device *rdev = dev->dev_private;
1443 struct radeon_framebuffer *radeon_fb;
1444 struct drm_gem_object *obj;
Jerome Glisse4c788672009-11-20 14:29:23 +01001445 struct radeon_bo *rbo;
Chris Ball4dd19b02010-09-26 06:47:23 -05001446 struct drm_framebuffer *target_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001447 uint64_t fb_location;
Dave Airliee024e112009-06-24 09:48:08 +10001448 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001449 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
Alex Deucheradcfde52011-05-27 10:05:03 -04001450 u32 tmp, viewport_w, viewport_h;
Jerome Glisse4c788672009-11-20 14:29:23 +01001451 int r;
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001452 bool bypass_lut = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001453
Jerome Glisse2de3b482009-11-17 14:08:55 -08001454 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -07001455 if (!atomic && !crtc->primary->fb) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001456 DRM_DEBUG_KMS("No FB bound\n");
Jerome Glisse2de3b482009-11-17 14:08:55 -08001457 return 0;
1458 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001459
Chris Ball4dd19b02010-09-26 06:47:23 -05001460 if (atomic) {
1461 radeon_fb = to_radeon_framebuffer(fb);
1462 target_fb = fb;
1463 }
1464 else {
Matt Roperf4510a22014-04-01 15:22:40 -07001465 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
1466 target_fb = crtc->primary->fb;
Chris Ball4dd19b02010-09-26 06:47:23 -05001467 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001468
1469 obj = radeon_fb->obj;
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001470 rbo = gem_to_radeon_bo(obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001471 r = radeon_bo_reserve(rbo, false);
1472 if (unlikely(r != 0))
1473 return r;
Chris Ball4dd19b02010-09-26 06:47:23 -05001474
1475 /* If atomic, assume fb object is pinned & idle & fenced and
1476 * just update base pointers
1477 */
1478 if (atomic)
1479 fb_location = radeon_bo_gpu_offset(rbo);
1480 else {
1481 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1482 if (unlikely(r != 0)) {
1483 radeon_bo_unreserve(rbo);
1484 return -EINVAL;
1485 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001486 }
Jerome Glisse4c788672009-11-20 14:29:23 +01001487 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1488 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001489
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001490 switch (target_fb->pixel_format) {
1491 case DRM_FORMAT_C8:
Dave Airlie41456df2009-09-16 10:15:21 +10001492 fb_format =
1493 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1494 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1495 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001496 case DRM_FORMAT_XRGB4444:
1497 case DRM_FORMAT_ARGB4444:
1498 fb_format =
1499 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1500 AVIVO_D1GRPH_CONTROL_16BPP_ARGB4444;
1501#ifdef __BIG_ENDIAN
1502 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1503#endif
1504 break;
1505 case DRM_FORMAT_XRGB1555:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001506 fb_format =
1507 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1508 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001509#ifdef __BIG_ENDIAN
1510 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1511#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001512 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001513 case DRM_FORMAT_RGB565:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001514 fb_format =
1515 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1516 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001517#ifdef __BIG_ENDIAN
1518 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1519#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001520 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001521 case DRM_FORMAT_XRGB8888:
1522 case DRM_FORMAT_ARGB8888:
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001523 fb_format =
1524 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1525 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
Alex Deucherfa6bee42011-01-25 11:55:50 -05001526#ifdef __BIG_ENDIAN
1527 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1528#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001529 break;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001530 case DRM_FORMAT_XRGB2101010:
1531 case DRM_FORMAT_ARGB2101010:
1532 fb_format =
1533 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1534 AVIVO_D1GRPH_CONTROL_32BPP_ARGB2101010;
1535#ifdef __BIG_ENDIAN
1536 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1537#endif
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001538 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1539 bypass_lut = true;
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001540 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001541 default:
Fredrik Höglund8bae4272013-09-21 17:15:36 +02001542 DRM_ERROR("Unsupported screen format %s\n",
1543 drm_get_format_name(target_fb->pixel_format));
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001544 return -EINVAL;
1545 }
1546
Alex Deucher40c4ac12010-05-20 12:04:59 -04001547 if (rdev->family >= CHIP_R600) {
1548 if (tiling_flags & RADEON_TILING_MACRO)
1549 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1550 else if (tiling_flags & RADEON_TILING_MICRO)
1551 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1552 } else {
1553 if (tiling_flags & RADEON_TILING_MACRO)
1554 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
Dave Airliecf2f05d2009-12-08 15:45:13 +10001555
Alex Deucher40c4ac12010-05-20 12:04:59 -04001556 if (tiling_flags & RADEON_TILING_MICRO)
1557 fb_format |= AVIVO_D1GRPH_TILED;
1558 }
Dave Airliee024e112009-06-24 09:48:08 +10001559
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001560 if (radeon_crtc->crtc_id == 0)
1561 WREG32(AVIVO_D1VGA_CONTROL, 0);
1562 else
1563 WREG32(AVIVO_D2VGA_CONTROL, 0);
Alex Deucherc290dad2009-10-22 16:12:34 -04001564
1565 if (rdev->family >= CHIP_RV770) {
1566 if (radeon_crtc->crtc_id) {
Alex Deucher95347872010-09-01 17:20:42 -04001567 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1568 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001569 } else {
Alex Deucher95347872010-09-01 17:20:42 -04001570 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1571 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
Alex Deucherc290dad2009-10-22 16:12:34 -04001572 }
1573 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001574 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1575 (u32) fb_location);
1576 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1577 radeon_crtc->crtc_offset, (u32) fb_location);
1578 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
Alex Deucherfa6bee42011-01-25 11:55:50 -05001579 if (rdev->family >= CHIP_R600)
1580 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001581
Mario Kleiner4366f3b2014-06-07 03:38:11 +02001582 /* LUT only has 256 slots for 8 bpc fb. Bypass for > 8 bpc scanout for precision */
1583 WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset,
1584 (bypass_lut ? AVIVO_LUT_10BIT_BYPASS_EN : 0), ~AVIVO_LUT_10BIT_BYPASS_EN);
1585
1586 if (bypass_lut)
1587 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
1588
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001589 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1590 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1591 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1592 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
Chris Ball4dd19b02010-09-26 06:47:23 -05001593 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1594 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001595
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001596 fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001597 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1598 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1599
1600 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
Michel Dänzer1b619252012-02-01 12:09:55 +01001601 target_fb->height);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001602 x &= ~3;
1603 y &= ~1;
1604 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1605 (x << 16) | y);
Alex Deucheradcfde52011-05-27 10:05:03 -04001606 viewport_w = crtc->mode.hdisplay;
1607 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001608 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
Alex Deucheradcfde52011-05-27 10:05:03 -04001609 (viewport_w << 16) | viewport_h);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001610
Alex Deucherfb9674b2011-04-02 09:15:50 -04001611 /* pageflip setup */
1612 /* make sure flip is at vb rather than hb */
1613 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1614 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1615 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1616
Mario Kleinerf53f81b2014-07-03 03:45:02 +02001617 /* set pageflip to happen only at start of vblank interval (front porch) */
1618 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
Alex Deucherfb9674b2011-04-02 09:15:50 -04001619
Matt Roperf4510a22014-04-01 15:22:40 -07001620 if (!atomic && fb && fb != crtc->primary->fb) {
Chris Ball4dd19b02010-09-26 06:47:23 -05001621 radeon_fb = to_radeon_framebuffer(fb);
Daniel Vetter7e4d15d2011-02-18 17:59:17 +01001622 rbo = gem_to_radeon_bo(radeon_fb->obj);
Jerome Glisse4c788672009-11-20 14:29:23 +01001623 r = radeon_bo_reserve(rbo, false);
1624 if (unlikely(r != 0))
1625 return r;
1626 radeon_bo_unpin(rbo);
1627 radeon_bo_unreserve(rbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001628 }
Michel Dänzerf30f37d2009-10-08 10:44:09 +02001629
1630 /* Bytes per pixel may have changed */
1631 radeon_bandwidth_update(rdev);
1632
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001633 return 0;
1634}
1635
Alex Deucher54f088a2010-01-19 16:34:01 -05001636int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1637 struct drm_framebuffer *old_fb)
1638{
1639 struct drm_device *dev = crtc->dev;
1640 struct radeon_device *rdev = dev->dev_private;
1641
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001642 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001643 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001644 else if (ASIC_IS_AVIVO(rdev))
Chris Ball4dd19b02010-09-26 06:47:23 -05001645 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
Alex Deucher54f088a2010-01-19 16:34:01 -05001646 else
Chris Ball4dd19b02010-09-26 06:47:23 -05001647 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1648}
1649
1650int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1651 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -05001652 int x, int y, enum mode_set_atomic state)
Chris Ball4dd19b02010-09-26 06:47:23 -05001653{
1654 struct drm_device *dev = crtc->dev;
1655 struct radeon_device *rdev = dev->dev_private;
1656
1657 if (ASIC_IS_DCE4(rdev))
Alex Deucherc9417bd2011-02-06 14:23:26 -05001658 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
Chris Ball4dd19b02010-09-26 06:47:23 -05001659 else if (ASIC_IS_AVIVO(rdev))
1660 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1661 else
1662 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
Alex Deucher54f088a2010-01-19 16:34:01 -05001663}
1664
Alex Deucher615e0cb2010-01-20 16:22:53 -05001665/* properly set additional regs when using atombios */
1666static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1667{
1668 struct drm_device *dev = crtc->dev;
1669 struct radeon_device *rdev = dev->dev_private;
1670 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1671 u32 disp_merge_cntl;
1672
1673 switch (radeon_crtc->crtc_id) {
1674 case 0:
1675 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1676 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1677 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1678 break;
1679 case 1:
1680 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1681 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1682 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1683 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1684 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1685 break;
1686 }
1687}
1688
Alex Deucherf3dd8502012-08-31 11:56:50 -04001689/**
1690 * radeon_get_pll_use_mask - look up a mask of which pplls are in use
1691 *
1692 * @crtc: drm crtc
1693 *
1694 * Returns the mask of which PPLLs (Pixel PLLs) are in use.
1695 */
1696static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
1697{
1698 struct drm_device *dev = crtc->dev;
1699 struct drm_crtc *test_crtc;
Alex Deucher57b35e22012-09-17 17:34:45 -04001700 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001701 u32 pll_in_use = 0;
1702
1703 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1704 if (crtc == test_crtc)
1705 continue;
1706
Alex Deucher57b35e22012-09-17 17:34:45 -04001707 test_radeon_crtc = to_radeon_crtc(test_crtc);
1708 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1709 pll_in_use |= (1 << test_radeon_crtc->pll_id);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001710 }
1711 return pll_in_use;
1712}
1713
1714/**
1715 * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
1716 *
1717 * @crtc: drm crtc
1718 *
1719 * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
1720 * also in DP mode. For DP, a single PPLL can be used for all DP
1721 * crtcs/encoders.
1722 */
1723static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
1724{
1725 struct drm_device *dev = crtc->dev;
Alex Deucher57b35e22012-09-17 17:34:45 -04001726 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001727 struct radeon_crtc *test_radeon_crtc;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001728
Alex Deucher57b35e22012-09-17 17:34:45 -04001729 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1730 if (crtc == test_crtc)
1731 continue;
1732 test_radeon_crtc = to_radeon_crtc(test_crtc);
1733 if (test_radeon_crtc->encoder &&
1734 ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1735 /* for DP use the same PLL for all */
1736 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
1737 return test_radeon_crtc->pll_id;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001738 }
1739 }
1740 return ATOM_PPLL_INVALID;
1741}
1742
1743/**
Alex Deucher2f454cf2012-09-12 18:54:14 -04001744 * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
1745 *
1746 * @crtc: drm crtc
1747 * @encoder: drm encoder
1748 *
1749 * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
1750 * be shared (i.e., same clock).
1751 */
Alex Deucher5df31962012-09-13 11:52:08 -04001752static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
Alex Deucher2f454cf2012-09-12 18:54:14 -04001753{
Alex Deucher5df31962012-09-13 11:52:08 -04001754 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher2f454cf2012-09-12 18:54:14 -04001755 struct drm_device *dev = crtc->dev;
Alex Deucher9642ac02012-09-13 12:43:41 -04001756 struct drm_crtc *test_crtc;
Alex Deucher5df31962012-09-13 11:52:08 -04001757 struct radeon_crtc *test_radeon_crtc;
Alex Deucher9642ac02012-09-13 12:43:41 -04001758 u32 adjusted_clock, test_adjusted_clock;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001759
Alex Deucher9642ac02012-09-13 12:43:41 -04001760 adjusted_clock = radeon_crtc->adjusted_clock;
1761
1762 if (adjusted_clock == 0)
1763 return ATOM_PPLL_INVALID;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001764
Alex Deucher57b35e22012-09-17 17:34:45 -04001765 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1766 if (crtc == test_crtc)
1767 continue;
1768 test_radeon_crtc = to_radeon_crtc(test_crtc);
1769 if (test_radeon_crtc->encoder &&
1770 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
1771 /* check if we are already driving this connector with another crtc */
1772 if (test_radeon_crtc->connector == radeon_crtc->connector) {
1773 /* if we are, return that pll */
1774 if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
Alex Deucher5df31962012-09-13 11:52:08 -04001775 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001776 }
Alex Deucher57b35e22012-09-17 17:34:45 -04001777 /* for non-DP check the clock */
1778 test_adjusted_clock = test_radeon_crtc->adjusted_clock;
1779 if ((crtc->mode.clock == test_crtc->mode.clock) &&
1780 (adjusted_clock == test_adjusted_clock) &&
1781 (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
1782 (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
1783 return test_radeon_crtc->pll_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001784 }
1785 }
1786 return ATOM_PPLL_INVALID;
1787}
1788
1789/**
Alex Deucherf3dd8502012-08-31 11:56:50 -04001790 * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
1791 *
1792 * @crtc: drm crtc
1793 *
1794 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
1795 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
1796 * monitors a dedicated PPLL must be used. If a particular board has
1797 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
1798 * as there is no need to program the PLL itself. If we are not able to
1799 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
1800 * avoid messing up an existing monitor.
1801 *
1802 * Asic specific PLL information
1803 *
Alex Deucher0331f672012-09-14 11:57:21 -04001804 * DCE 8.x
1805 * KB/KV
1806 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
1807 * CI
1808 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1809 *
Alex Deucherf3dd8502012-08-31 11:56:50 -04001810 * DCE 6.1
1811 * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
1812 * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
1813 *
1814 * DCE 6.0
1815 * - PPLL0 is available to all UNIPHY (DP only)
1816 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1817 *
1818 * DCE 5.0
1819 * - DCPLL is available to all UNIPHY (DP only)
1820 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1821 *
1822 * DCE 3.0/4.0/4.1
1823 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
1824 *
1825 */
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001826static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1827{
Alex Deucher5df31962012-09-13 11:52:08 -04001828 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001829 struct drm_device *dev = crtc->dev;
1830 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04001831 struct radeon_encoder *radeon_encoder =
1832 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucherf3dd8502012-08-31 11:56:50 -04001833 u32 pll_in_use;
1834 int pll;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001835
Alex Deucher0331f672012-09-14 11:57:21 -04001836 if (ASIC_IS_DCE8(rdev)) {
1837 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1838 if (rdev->clock.dp_extclk)
1839 /* skip PPLL programming if using ext clock */
1840 return ATOM_PPLL_INVALID;
1841 else {
1842 /* use the same PPLL for all DP monitors */
1843 pll = radeon_get_shared_dp_ppll(crtc);
1844 if (pll != ATOM_PPLL_INVALID)
1845 return pll;
1846 }
1847 } else {
1848 /* use the same PPLL for all monitors with the same clock */
1849 pll = radeon_get_shared_nondp_ppll(crtc);
1850 if (pll != ATOM_PPLL_INVALID)
1851 return pll;
1852 }
1853 /* otherwise, pick one of the plls */
1854 if ((rdev->family == CHIP_KAVERI) ||
Samuel Lib214f2a2014-04-30 18:40:53 -04001855 (rdev->family == CHIP_KABINI) ||
1856 (rdev->family == CHIP_MULLINS)) {
1857 /* KB/KV/ML has PPLL1 and PPLL2 */
Alex Deucher0331f672012-09-14 11:57:21 -04001858 pll_in_use = radeon_get_pll_use_mask(crtc);
1859 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1860 return ATOM_PPLL2;
1861 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1862 return ATOM_PPLL1;
1863 DRM_ERROR("unable to allocate a PPLL\n");
1864 return ATOM_PPLL_INVALID;
1865 } else {
1866 /* CI has PPLL0, PPLL1, and PPLL2 */
1867 pll_in_use = radeon_get_pll_use_mask(crtc);
1868 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1869 return ATOM_PPLL2;
1870 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1871 return ATOM_PPLL1;
1872 if (!(pll_in_use & (1 << ATOM_PPLL0)))
1873 return ATOM_PPLL0;
1874 DRM_ERROR("unable to allocate a PPLL\n");
1875 return ATOM_PPLL_INVALID;
1876 }
1877 } else if (ASIC_IS_DCE61(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001878 struct radeon_encoder_atom_dig *dig =
1879 radeon_encoder->enc_priv;
Alex Deucher24e1f792012-03-20 17:18:32 -04001880
Alex Deucher5df31962012-09-13 11:52:08 -04001881 if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
1882 (dig->linkb == false))
1883 /* UNIPHY A uses PPLL2 */
1884 return ATOM_PPLL2;
1885 else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1886 /* UNIPHY B/C/D/E/F */
1887 if (rdev->clock.dp_extclk)
1888 /* skip PPLL programming if using ext clock */
1889 return ATOM_PPLL_INVALID;
1890 else {
1891 /* use the same PPLL for all DP monitors */
1892 pll = radeon_get_shared_dp_ppll(crtc);
1893 if (pll != ATOM_PPLL_INVALID)
1894 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001895 }
Alex Deucher5df31962012-09-13 11:52:08 -04001896 } else {
1897 /* use the same PPLL for all monitors with the same clock */
1898 pll = radeon_get_shared_nondp_ppll(crtc);
1899 if (pll != ATOM_PPLL_INVALID)
1900 return pll;
Alex Deucher24e1f792012-03-20 17:18:32 -04001901 }
1902 /* UNIPHY B/C/D/E/F */
Alex Deucherf3dd8502012-08-31 11:56:50 -04001903 pll_in_use = radeon_get_pll_use_mask(crtc);
1904 if (!(pll_in_use & (1 << ATOM_PPLL0)))
Alex Deucher24e1f792012-03-20 17:18:32 -04001905 return ATOM_PPLL0;
Alex Deucherf3dd8502012-08-31 11:56:50 -04001906 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1907 return ATOM_PPLL1;
1908 DRM_ERROR("unable to allocate a PPLL\n");
1909 return ATOM_PPLL_INVALID;
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001910 } else if (ASIC_IS_DCE41(rdev)) {
1911 /* Don't share PLLs on DCE4.1 chips */
1912 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1913 if (rdev->clock.dp_extclk)
1914 /* skip PPLL programming if using ext clock */
1915 return ATOM_PPLL_INVALID;
1916 }
1917 pll_in_use = radeon_get_pll_use_mask(crtc);
1918 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1919 return ATOM_PPLL1;
1920 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1921 return ATOM_PPLL2;
1922 DRM_ERROR("unable to allocate a PPLL\n");
1923 return ATOM_PPLL_INVALID;
Alex Deucher24e1f792012-03-20 17:18:32 -04001924 } else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher5df31962012-09-13 11:52:08 -04001925 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1926 * depending on the asic:
1927 * DCE4: PPLL or ext clock
1928 * DCE5: PPLL, DCPLL, or ext clock
1929 * DCE6: PPLL, PPLL0, or ext clock
1930 *
1931 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1932 * PPLL/DCPLL programming and only program the DP DTO for the
1933 * crtc virtual pixel clock.
1934 */
1935 if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
1936 if (rdev->clock.dp_extclk)
1937 /* skip PPLL programming if using ext clock */
1938 return ATOM_PPLL_INVALID;
1939 else if (ASIC_IS_DCE6(rdev))
1940 /* use PPLL0 for all DP */
1941 return ATOM_PPLL0;
1942 else if (ASIC_IS_DCE5(rdev))
1943 /* use DCPLL for all DP */
1944 return ATOM_DCPLL;
1945 else {
1946 /* use the same PPLL for all DP monitors */
1947 pll = radeon_get_shared_dp_ppll(crtc);
1948 if (pll != ATOM_PPLL_INVALID)
1949 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001950 }
Alex Deucher9ef4e1d2014-02-25 10:21:43 -05001951 } else {
Alex Deucher5df31962012-09-13 11:52:08 -04001952 /* use the same PPLL for all monitors with the same clock */
1953 pll = radeon_get_shared_nondp_ppll(crtc);
1954 if (pll != ATOM_PPLL_INVALID)
1955 return pll;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001956 }
1957 /* all other cases */
1958 pll_in_use = radeon_get_pll_use_mask(crtc);
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001959 if (!(pll_in_use & (1 << ATOM_PPLL1)))
1960 return ATOM_PPLL1;
Alex Deucher29dbe3b2012-10-05 10:22:02 -04001961 if (!(pll_in_use & (1 << ATOM_PPLL2)))
1962 return ATOM_PPLL2;
Alex Deucher9dbbcfc2012-09-12 17:39:57 -04001963 DRM_ERROR("unable to allocate a PPLL\n");
1964 return ATOM_PPLL_INVALID;
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001965 } else {
1966 /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
Jerome Glissefc58acd2012-11-27 16:12:29 -05001967 /* some atombios (observed in some DCE2/DCE3) code have a bug,
1968 * the matching btw pll and crtc is done through
1969 * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
1970 * pll (1 or 2) to select which register to write. ie if using
1971 * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
1972 * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
1973 * choose which value to write. Which is reverse order from
1974 * register logic. So only case that works is when pllid is
1975 * same as crtcid or when both pll and crtc are enabled and
1976 * both use same clock.
1977 *
1978 * So just return crtc id as if crtc and pll were hard linked
1979 * together even if they aren't
1980 */
Alex Deucher1e4db5f2012-11-05 10:16:12 -05001981 return radeon_crtc->crtc_id;
Alex Deucher2f454cf2012-09-12 18:54:14 -04001982 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05001983}
1984
Alex Deucherf3f1f032012-03-20 17:18:04 -04001985void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
Alex Deucher3fa47d92012-01-20 14:56:39 -05001986{
1987 /* always set DCPLL */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001988 if (ASIC_IS_DCE6(rdev))
1989 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
1990 else if (ASIC_IS_DCE4(rdev)) {
Alex Deucher3fa47d92012-01-20 14:56:39 -05001991 struct radeon_atom_ss ss;
1992 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1993 ASIC_INTERNAL_SS_ON_DCPLL,
1994 rdev->clock.default_dispclk);
1995 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04001996 atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001997 /* XXX: DCE5, make sure voltage, dispclk is high enough */
Alex Deucherf3f1f032012-03-20 17:18:04 -04001998 atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
Alex Deucher3fa47d92012-01-20 14:56:39 -05001999 if (ss_enabled)
Jerome Glisse5efcc762012-08-17 14:40:04 -04002000 atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
Alex Deucher3fa47d92012-01-20 14:56:39 -05002001 }
2002
2003}
2004
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002005int atombios_crtc_mode_set(struct drm_crtc *crtc,
2006 struct drm_display_mode *mode,
2007 struct drm_display_mode *adjusted_mode,
2008 int x, int y, struct drm_framebuffer *old_fb)
2009{
2010 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2011 struct drm_device *dev = crtc->dev;
2012 struct radeon_device *rdev = dev->dev_private;
Alex Deucher5df31962012-09-13 11:52:08 -04002013 struct radeon_encoder *radeon_encoder =
2014 to_radeon_encoder(radeon_crtc->encoder);
Alex Deucher54bfe492010-09-03 15:52:53 -04002015 bool is_tvcv = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002016
Alex Deucher5df31962012-09-13 11:52:08 -04002017 if (radeon_encoder->active_device &
2018 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
2019 is_tvcv = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002020
Christian Königcde10122014-05-02 14:27:42 +02002021 if (!radeon_crtc->adjusted_clock)
2022 return -EINVAL;
2023
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002024 atombios_crtc_set_pll(crtc, adjusted_mode);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002025
Alex Deucher54bfe492010-09-03 15:52:53 -04002026 if (ASIC_IS_DCE4(rdev))
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002027 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher54bfe492010-09-03 15:52:53 -04002028 else if (ASIC_IS_AVIVO(rdev)) {
2029 if (is_tvcv)
2030 atombios_crtc_set_timing(crtc, adjusted_mode);
2031 else
2032 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
2033 } else {
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002034 atombios_crtc_set_timing(crtc, adjusted_mode);
Alex Deucher5a9bcac2009-10-08 15:09:31 -04002035 if (radeon_crtc->crtc_id == 0)
2036 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
Alex Deucher615e0cb2010-01-20 16:22:53 -05002037 radeon_legacy_atom_fixup(crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002038 }
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002039 atombios_crtc_set_base(crtc, x, y, old_fb);
Jerome Glissec93bb852009-07-13 21:04:08 +02002040 atombios_overscan_setup(crtc, mode, adjusted_mode);
2041 atombios_scaler_setup(crtc);
Michel Dänzer6d3759f2014-11-21 11:48:57 +09002042 radeon_cursor_reset(crtc);
Alex Deucher66edc1c2013-07-08 11:26:42 -04002043 /* update the hw version fpr dpm */
2044 radeon_crtc->hw_mode = *adjusted_mode;
2045
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002046 return 0;
2047}
2048
2049static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02002050 const struct drm_display_mode *mode,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002051 struct drm_display_mode *adjusted_mode)
2052{
Alex Deucher5df31962012-09-13 11:52:08 -04002053 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
2054 struct drm_device *dev = crtc->dev;
2055 struct drm_encoder *encoder;
2056
2057 /* assign the encoder to the radeon crtc to avoid repeated lookups later */
2058 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2059 if (encoder->crtc == crtc) {
2060 radeon_crtc->encoder = encoder;
Alex Deucher57b35e22012-09-17 17:34:45 -04002061 radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
Alex Deucher5df31962012-09-13 11:52:08 -04002062 break;
2063 }
2064 }
Alex Deucher57b35e22012-09-17 17:34:45 -04002065 if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
2066 radeon_crtc->encoder = NULL;
2067 radeon_crtc->connector = NULL;
Alex Deucher5df31962012-09-13 11:52:08 -04002068 return false;
Alex Deucher57b35e22012-09-17 17:34:45 -04002069 }
Jerome Glissec93bb852009-07-13 21:04:08 +02002070 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2071 return false;
Alex Deucher19eca432012-09-13 10:56:16 -04002072 if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
2073 return false;
Alex Deucherc0fd0832012-09-14 12:30:51 -04002074 /* pick pll */
2075 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
2076 /* if we can't get a PPLL for a non-DP encoder, fail */
2077 if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
2078 !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
2079 return false;
2080
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002081 return true;
2082}
2083
2084static void atombios_crtc_prepare(struct drm_crtc *crtc)
2085{
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002086 struct drm_device *dev = crtc->dev;
2087 struct radeon_device *rdev = dev->dev_private;
Alex Deucher267364a2010-03-08 17:10:41 -05002088
Alex Deucher6c0ae2a2012-07-26 13:38:52 -04002089 /* disable crtc pair power gating before programming */
2090 if (ASIC_IS_DCE6(rdev))
2091 atombios_powergate_crtc(crtc, ATOM_DISABLE);
2092
Alex Deucher37b43902010-02-09 12:04:43 -05002093 atombios_lock_crtc(crtc, ATOM_ENABLE);
Alex Deuchera348c842010-01-21 16:50:30 -05002094 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002095}
2096
2097static void atombios_crtc_commit(struct drm_crtc *crtc)
2098{
2099 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Alex Deucher37b43902010-02-09 12:04:43 -05002100 atombios_lock_crtc(crtc, ATOM_DISABLE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002101}
2102
Alex Deucher37f90032010-06-11 17:58:38 -04002103static void atombios_crtc_disable(struct drm_crtc *crtc)
2104{
2105 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Alex Deucher64199872012-03-20 17:18:33 -04002106 struct drm_device *dev = crtc->dev;
2107 struct radeon_device *rdev = dev->dev_private;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002108 struct radeon_atom_ss ss;
Alex Deucher4e585912012-08-21 19:06:21 -04002109 int i;
Alex Deucher8e8e5232011-05-20 04:34:16 -04002110
Alex Deucher37f90032010-06-11 17:58:38 -04002111 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -07002112 if (crtc->primary->fb) {
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002113 int r;
2114 struct radeon_framebuffer *radeon_fb;
2115 struct radeon_bo *rbo;
2116
Matt Roperf4510a22014-04-01 15:22:40 -07002117 radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
Ilija Hadzic75b871e2013-11-02 23:00:19 -04002118 rbo = gem_to_radeon_bo(radeon_fb->obj);
2119 r = radeon_bo_reserve(rbo, false);
2120 if (unlikely(r))
2121 DRM_ERROR("failed to reserve rbo before unpin\n");
2122 else {
2123 radeon_bo_unpin(rbo);
2124 radeon_bo_unreserve(rbo);
2125 }
2126 }
Alex Deucherac4d04d2013-08-21 14:44:15 -04002127 /* disable the GRPH */
2128 if (ASIC_IS_DCE4(rdev))
2129 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2130 else if (ASIC_IS_AVIVO(rdev))
2131 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
2132
Alex Deucher0e3d50b2013-02-05 11:47:09 -05002133 if (ASIC_IS_DCE6(rdev))
2134 atombios_powergate_crtc(crtc, ATOM_ENABLE);
Alex Deucher37f90032010-06-11 17:58:38 -04002135
Alex Deucher4e585912012-08-21 19:06:21 -04002136 for (i = 0; i < rdev->num_crtc; i++) {
2137 if (rdev->mode_info.crtcs[i] &&
2138 rdev->mode_info.crtcs[i]->enabled &&
2139 i != radeon_crtc->crtc_id &&
2140 radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
2141 /* one other crtc is using this pll don't turn
2142 * off the pll
2143 */
2144 goto done;
2145 }
2146 }
2147
Alex Deucher37f90032010-06-11 17:58:38 -04002148 switch (radeon_crtc->pll_id) {
2149 case ATOM_PPLL1:
2150 case ATOM_PPLL2:
2151 /* disable the ppll */
2152 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
Alex Deucher8e8e5232011-05-20 04:34:16 -04002153 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
Alex Deucher37f90032010-06-11 17:58:38 -04002154 break;
Alex Deucher64199872012-03-20 17:18:33 -04002155 case ATOM_PPLL0:
2156 /* disable the ppll */
Alex Deucher7eeeabf2013-08-19 10:22:26 -04002157 if ((rdev->family == CHIP_ARUBA) ||
2158 (rdev->family == CHIP_BONAIRE) ||
2159 (rdev->family == CHIP_HAWAII))
Alex Deucher64199872012-03-20 17:18:33 -04002160 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
2161 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2162 break;
Alex Deucher37f90032010-06-11 17:58:38 -04002163 default:
2164 break;
2165 }
Alex Deucher4e585912012-08-21 19:06:21 -04002166done:
Alex Deucherf3dd8502012-08-31 11:56:50 -04002167 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002168 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002169 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002170 radeon_crtc->connector = NULL;
Alex Deucher37f90032010-06-11 17:58:38 -04002171}
2172
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002173static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
2174 .dpms = atombios_crtc_dpms,
2175 .mode_fixup = atombios_crtc_mode_fixup,
2176 .mode_set = atombios_crtc_mode_set,
2177 .mode_set_base = atombios_crtc_set_base,
Chris Ball4dd19b02010-09-26 06:47:23 -05002178 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002179 .prepare = atombios_crtc_prepare,
2180 .commit = atombios_crtc_commit,
Dave Airlie068143d2009-10-05 09:58:02 +10002181 .load_lut = radeon_crtc_load_lut,
Alex Deucher37f90032010-06-11 17:58:38 -04002182 .disable = atombios_crtc_disable,
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002183};
2184
2185void radeon_atombios_init_crtc(struct drm_device *dev,
2186 struct radeon_crtc *radeon_crtc)
2187{
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002188 struct radeon_device *rdev = dev->dev_private;
2189
2190 if (ASIC_IS_DCE4(rdev)) {
2191 switch (radeon_crtc->crtc_id) {
2192 case 0:
2193 default:
Alex Deucher12d77982010-02-09 17:18:48 -05002194 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002195 break;
2196 case 1:
Alex Deucher12d77982010-02-09 17:18:48 -05002197 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002198 break;
2199 case 2:
Alex Deucher12d77982010-02-09 17:18:48 -05002200 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002201 break;
2202 case 3:
Alex Deucher12d77982010-02-09 17:18:48 -05002203 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002204 break;
2205 case 4:
Alex Deucher12d77982010-02-09 17:18:48 -05002206 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002207 break;
2208 case 5:
Alex Deucher12d77982010-02-09 17:18:48 -05002209 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -05002210 break;
2211 }
2212 } else {
2213 if (radeon_crtc->crtc_id == 1)
2214 radeon_crtc->crtc_offset =
2215 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
2216 else
2217 radeon_crtc->crtc_offset = 0;
2218 }
Alex Deucherf3dd8502012-08-31 11:56:50 -04002219 radeon_crtc->pll_id = ATOM_PPLL_INVALID;
Alex Deucher9642ac02012-09-13 12:43:41 -04002220 radeon_crtc->adjusted_clock = 0;
Alex Deucher5df31962012-09-13 11:52:08 -04002221 radeon_crtc->encoder = NULL;
Alex Deucher57b35e22012-09-17 17:34:45 -04002222 radeon_crtc->connector = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02002223 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
2224}