2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
27 #include <drm/drm_crtc_helper.h>
28 #include <drm/radeon_drm.h>
29 #include <drm/drm_fixed.h>
32 #include "atom-bits.h"
34 static void atombios_overscan_setup(struct drm_crtc *crtc,
35 struct drm_display_mode *mode,
36 struct drm_display_mode *adjusted_mode)
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);
45 memset(&args, 0, sizeof(args));
47 args.ucCRTC = radeon_crtc->crtc_id;
49 switch (radeon_crtc->rmx_type) {
51 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);
57 a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
58 a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
61 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);
64 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);
70 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);
76 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
79 static void atombios_scaler_setup(struct drm_crtc *crtc)
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);
87 /* fixme - fill in enc_priv for atom dac */
88 enum radeon_tv_std tv_std = TV_STD_NTSC;
89 bool is_tv = false, is_cv = false;
90 struct drm_encoder *encoder;
92 if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
95 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
97 if (encoder->crtc == crtc) {
98 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
99 if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
100 struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
101 tv_std = tv_dac->tv_std;
107 memset(&args, 0, sizeof(args));
109 args.ucScaler = radeon_crtc->crtc_id;
115 args.ucTVStandard = ATOM_TV_NTSC;
118 args.ucTVStandard = ATOM_TV_PAL;
121 args.ucTVStandard = ATOM_TV_PALM;
124 args.ucTVStandard = ATOM_TV_PAL60;
127 args.ucTVStandard = ATOM_TV_NTSCJ;
129 case TV_STD_SCART_PAL:
130 args.ucTVStandard = ATOM_TV_PAL; /* ??? */
133 args.ucTVStandard = ATOM_TV_SECAM;
136 args.ucTVStandard = ATOM_TV_PALCN;
139 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
141 args.ucTVStandard = ATOM_TV_CV;
142 args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
144 switch (radeon_crtc->rmx_type) {
146 args.ucEnable = ATOM_SCALER_EXPANSION;
149 args.ucEnable = ATOM_SCALER_CENTER;
152 args.ucEnable = ATOM_SCALER_EXPANSION;
155 if (ASIC_IS_AVIVO(rdev))
156 args.ucEnable = ATOM_SCALER_DISABLE;
158 args.ucEnable = ATOM_SCALER_CENTER;
162 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
164 && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
165 atom_rv515_force_tv_scaler(rdev, radeon_crtc);
169 static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
171 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
172 struct drm_device *dev = crtc->dev;
173 struct radeon_device *rdev = dev->dev_private;
175 GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
176 ENABLE_CRTC_PS_ALLOCATION args;
178 memset(&args, 0, sizeof(args));
180 args.ucCRTC = radeon_crtc->crtc_id;
181 args.ucEnable = lock;
183 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
186 static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
188 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
189 struct drm_device *dev = crtc->dev;
190 struct radeon_device *rdev = dev->dev_private;
191 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
192 ENABLE_CRTC_PS_ALLOCATION args;
194 memset(&args, 0, sizeof(args));
196 args.ucCRTC = radeon_crtc->crtc_id;
197 args.ucEnable = state;
199 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
202 static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
204 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
205 struct drm_device *dev = crtc->dev;
206 struct radeon_device *rdev = dev->dev_private;
207 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
208 ENABLE_CRTC_PS_ALLOCATION args;
210 memset(&args, 0, sizeof(args));
212 args.ucCRTC = radeon_crtc->crtc_id;
213 args.ucEnable = state;
215 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
218 static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
220 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
221 struct drm_device *dev = crtc->dev;
222 struct radeon_device *rdev = dev->dev_private;
223 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
224 BLANK_CRTC_PS_ALLOCATION args;
226 memset(&args, 0, sizeof(args));
228 args.ucCRTC = radeon_crtc->crtc_id;
229 args.ucBlanking = state;
231 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
234 void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
236 struct drm_device *dev = crtc->dev;
237 struct radeon_device *rdev = dev->dev_private;
238 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
241 case DRM_MODE_DPMS_ON:
242 radeon_crtc->enabled = true;
243 /* adjust pm to dpms changes BEFORE enabling crtcs */
244 radeon_pm_compute_clocks(rdev);
245 atombios_enable_crtc(crtc, ATOM_ENABLE);
246 if (ASIC_IS_DCE3(rdev))
247 atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
248 atombios_blank_crtc(crtc, ATOM_DISABLE);
249 drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
250 radeon_crtc_load_lut(crtc);
252 case DRM_MODE_DPMS_STANDBY:
253 case DRM_MODE_DPMS_SUSPEND:
254 case DRM_MODE_DPMS_OFF:
255 drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
256 if (radeon_crtc->enabled)
257 atombios_blank_crtc(crtc, ATOM_ENABLE);
258 if (ASIC_IS_DCE3(rdev))
259 atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
260 atombios_enable_crtc(crtc, ATOM_DISABLE);
261 radeon_crtc->enabled = false;
262 /* adjust pm to dpms changes AFTER disabling crtcs */
263 radeon_pm_compute_clocks(rdev);
269 atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
270 struct drm_display_mode *mode)
272 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
273 struct drm_device *dev = crtc->dev;
274 struct radeon_device *rdev = dev->dev_private;
275 SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
276 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
279 memset(&args, 0, sizeof(args));
280 args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
281 args.usH_Blanking_Time =
282 cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
283 args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
284 args.usV_Blanking_Time =
285 cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
286 args.usH_SyncOffset =
287 cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
289 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
290 args.usV_SyncOffset =
291 cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
293 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
294 args.ucH_Border = radeon_crtc->h_border;
295 args.ucV_Border = radeon_crtc->v_border;
297 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
298 misc |= ATOM_VSYNC_POLARITY;
299 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
300 misc |= ATOM_HSYNC_POLARITY;
301 if (mode->flags & DRM_MODE_FLAG_CSYNC)
302 misc |= ATOM_COMPOSITESYNC;
303 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
304 misc |= ATOM_INTERLACE;
305 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
306 misc |= ATOM_DOUBLE_CLOCK_MODE;
308 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
309 args.ucCRTC = radeon_crtc->crtc_id;
311 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
314 static void atombios_crtc_set_timing(struct drm_crtc *crtc,
315 struct drm_display_mode *mode)
317 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
318 struct drm_device *dev = crtc->dev;
319 struct radeon_device *rdev = dev->dev_private;
320 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
321 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
324 memset(&args, 0, sizeof(args));
325 args.usH_Total = cpu_to_le16(mode->crtc_htotal);
326 args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
327 args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
329 cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
330 args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
331 args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
332 args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
334 cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
336 args.ucOverscanRight = radeon_crtc->h_border;
337 args.ucOverscanLeft = radeon_crtc->h_border;
338 args.ucOverscanBottom = radeon_crtc->v_border;
339 args.ucOverscanTop = radeon_crtc->v_border;
341 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
342 misc |= ATOM_VSYNC_POLARITY;
343 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
344 misc |= ATOM_HSYNC_POLARITY;
345 if (mode->flags & DRM_MODE_FLAG_CSYNC)
346 misc |= ATOM_COMPOSITESYNC;
347 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
348 misc |= ATOM_INTERLACE;
349 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
350 misc |= ATOM_DOUBLE_CLOCK_MODE;
352 args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
353 args.ucCRTC = radeon_crtc->crtc_id;
355 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
358 static void atombios_disable_ss(struct drm_crtc *crtc)
360 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
361 struct drm_device *dev = crtc->dev;
362 struct radeon_device *rdev = dev->dev_private;
365 if (ASIC_IS_DCE4(rdev)) {
366 switch (radeon_crtc->pll_id) {
368 ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
369 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
370 WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
373 ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
374 ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
375 WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
378 case ATOM_PPLL_INVALID:
381 } else if (ASIC_IS_AVIVO(rdev)) {
382 switch (radeon_crtc->pll_id) {
384 ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
386 WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
389 ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
391 WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
394 case ATOM_PPLL_INVALID:
401 union atom_enable_ss {
402 ENABLE_LVDS_SS_PARAMETERS lvds_ss;
403 ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
404 ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
405 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
406 ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
409 static void atombios_crtc_program_ss(struct drm_crtc *crtc,
412 struct radeon_atom_ss *ss)
414 struct drm_device *dev = crtc->dev;
415 struct radeon_device *rdev = dev->dev_private;
416 int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
417 union atom_enable_ss args;
419 memset(&args, 0, sizeof(args));
421 if (ASIC_IS_DCE5(rdev)) {
422 args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
423 args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
426 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
427 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
428 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
431 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
432 args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
433 args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
436 args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
437 args.v3.usSpreadSpectrumAmount = cpu_to_le16(0);
438 args.v3.usSpreadSpectrumStep = cpu_to_le16(0);
440 case ATOM_PPLL_INVALID:
443 args.v3.ucEnable = enable;
444 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK))
445 args.v3.ucEnable = ATOM_DISABLE;
446 } else if (ASIC_IS_DCE4(rdev)) {
447 args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
448 args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
451 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
452 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
453 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
456 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
457 args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
458 args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
461 args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
462 args.v2.usSpreadSpectrumAmount = cpu_to_le16(0);
463 args.v2.usSpreadSpectrumStep = cpu_to_le16(0);
465 case ATOM_PPLL_INVALID:
468 args.v2.ucEnable = enable;
469 if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK) || ASIC_IS_DCE41(rdev))
470 args.v2.ucEnable = ATOM_DISABLE;
471 } else if (ASIC_IS_DCE3(rdev)) {
472 args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
473 args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
474 args.v1.ucSpreadSpectrumStep = ss->step;
475 args.v1.ucSpreadSpectrumDelay = ss->delay;
476 args.v1.ucSpreadSpectrumRange = ss->range;
477 args.v1.ucPpll = pll_id;
478 args.v1.ucEnable = enable;
479 } else if (ASIC_IS_AVIVO(rdev)) {
480 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
481 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
482 atombios_disable_ss(crtc);
485 args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
486 args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
487 args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
488 args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
489 args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
490 args.lvds_ss_2.ucEnable = enable;
492 if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
493 (ss->type & ATOM_EXTERNAL_SS_MASK)) {
494 atombios_disable_ss(crtc);
497 args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
498 args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
499 args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
500 args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
501 args.lvds_ss.ucEnable = enable;
503 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
506 union adjust_pixel_clock {
507 ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
508 ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
511 static u32 atombios_adjust_pll(struct drm_crtc *crtc,
512 struct drm_display_mode *mode,
513 struct radeon_pll *pll,
515 struct radeon_atom_ss *ss)
517 struct drm_device *dev = crtc->dev;
518 struct radeon_device *rdev = dev->dev_private;
519 struct drm_encoder *encoder = NULL;
520 struct radeon_encoder *radeon_encoder = NULL;
521 struct drm_connector *connector = NULL;
522 u32 adjusted_clock = mode->clock;
523 int encoder_mode = 0;
524 u32 dp_clock = mode->clock;
527 /* reset the pll flags */
530 if (ASIC_IS_AVIVO(rdev)) {
531 if ((rdev->family == CHIP_RS600) ||
532 (rdev->family == CHIP_RS690) ||
533 (rdev->family == CHIP_RS740))
534 pll->flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
535 RADEON_PLL_PREFER_CLOSEST_LOWER);
537 if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
538 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
540 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
542 if (rdev->family < CHIP_RV770)
543 pll->flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
545 pll->flags |= RADEON_PLL_LEGACY;
547 if (mode->clock > 200000) /* range limits??? */
548 pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
550 pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
553 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
554 if (encoder->crtc == crtc) {
555 radeon_encoder = to_radeon_encoder(encoder);
556 connector = radeon_get_connector_for_encoder(encoder);
558 bpc = connector->display_info.bpc;
559 encoder_mode = atombios_get_encoder_mode(encoder);
560 if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
561 radeon_encoder_is_dp_bridge(encoder)) {
563 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
564 struct radeon_connector_atom_dig *dig_connector =
565 radeon_connector->con_priv;
567 dp_clock = dig_connector->dp_clock;
571 /* use recommended ref_div for ss */
572 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
575 pll->flags |= RADEON_PLL_USE_REF_DIV;
576 pll->reference_div = ss->refdiv;
577 if (ASIC_IS_AVIVO(rdev))
578 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
583 if (ASIC_IS_AVIVO(rdev)) {
584 /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
585 if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
586 adjusted_clock = mode->clock * 2;
587 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
588 pll->flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
589 if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
590 pll->flags |= RADEON_PLL_IS_LCD;
592 if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
593 pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
594 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
595 pll->flags |= RADEON_PLL_USE_REF_DIV;
601 /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
602 * accordingly based on the encoder/transmitter to work around
603 * special hw requirements.
605 if (ASIC_IS_DCE3(rdev)) {
606 union adjust_pixel_clock args;
610 index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
611 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
613 return adjusted_clock;
615 memset(&args, 0, sizeof(args));
622 args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
623 args.v1.ucTransmitterID = radeon_encoder->encoder_id;
624 args.v1.ucEncodeMode = encoder_mode;
625 if (ss_enabled && ss->percentage)
627 ADJUST_DISPLAY_CONFIG_SS_ENABLE;
629 atom_execute_table(rdev->mode_info.atom_context,
630 index, (uint32_t *)&args);
631 adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
634 args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
635 args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
636 args.v3.sInput.ucEncodeMode = encoder_mode;
637 args.v3.sInput.ucDispPllConfig = 0;
638 if (ss_enabled && ss->percentage)
639 args.v3.sInput.ucDispPllConfig |=
640 DISPPLL_CONFIG_SS_ENABLE;
641 if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT) ||
642 radeon_encoder_is_dp_bridge(encoder)) {
643 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
644 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
645 args.v3.sInput.ucDispPllConfig |=
646 DISPPLL_CONFIG_COHERENT_MODE;
648 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
650 if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
651 /* deep color support */
652 args.v3.sInput.usPixelClock =
653 cpu_to_le16((mode->clock * bpc / 8) / 10);
655 if (dig->coherent_mode)
656 args.v3.sInput.ucDispPllConfig |=
657 DISPPLL_CONFIG_COHERENT_MODE;
658 if (mode->clock > 165000)
659 args.v3.sInput.ucDispPllConfig |=
660 DISPPLL_CONFIG_DUAL_LINK;
662 } else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
663 if (encoder_mode == ATOM_ENCODER_MODE_DP) {
664 args.v3.sInput.ucDispPllConfig |=
665 DISPPLL_CONFIG_COHERENT_MODE;
667 args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
668 } else if (encoder_mode != ATOM_ENCODER_MODE_LVDS) {
669 if (mode->clock > 165000)
670 args.v3.sInput.ucDispPllConfig |=
671 DISPPLL_CONFIG_DUAL_LINK;
674 if (radeon_encoder_is_dp_bridge(encoder)) {
675 struct drm_encoder *ext_encoder = radeon_atom_get_external_encoder(encoder);
676 struct radeon_encoder *ext_radeon_encoder = to_radeon_encoder(ext_encoder);
677 args.v3.sInput.ucExtTransmitterID = ext_radeon_encoder->encoder_id;
679 args.v3.sInput.ucExtTransmitterID = 0;
681 atom_execute_table(rdev->mode_info.atom_context,
682 index, (uint32_t *)&args);
683 adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
684 if (args.v3.sOutput.ucRefDiv) {
685 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
686 pll->flags |= RADEON_PLL_USE_REF_DIV;
687 pll->reference_div = args.v3.sOutput.ucRefDiv;
689 if (args.v3.sOutput.ucPostDiv) {
690 pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
691 pll->flags |= RADEON_PLL_USE_POST_DIV;
692 pll->post_div = args.v3.sOutput.ucPostDiv;
696 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
697 return adjusted_clock;
701 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
702 return adjusted_clock;
705 return adjusted_clock;
708 union set_pixel_clock {
709 SET_PIXEL_CLOCK_PS_ALLOCATION base;
710 PIXEL_CLOCK_PARAMETERS v1;
711 PIXEL_CLOCK_PARAMETERS_V2 v2;
712 PIXEL_CLOCK_PARAMETERS_V3 v3;
713 PIXEL_CLOCK_PARAMETERS_V5 v5;
714 PIXEL_CLOCK_PARAMETERS_V6 v6;
717 /* on DCE5, make sure the voltage is high enough to support the
720 static void atombios_crtc_set_dcpll(struct drm_crtc *crtc,
723 struct drm_device *dev = crtc->dev;
724 struct radeon_device *rdev = dev->dev_private;
727 union set_pixel_clock args;
729 memset(&args, 0, sizeof(args));
731 index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
732 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
740 /* if the default dcpll clock is specified,
741 * SetPixelClock provides the dividers
743 args.v5.ucCRTC = ATOM_CRTC_INVALID;
744 args.v5.usPixelClock = cpu_to_le16(dispclk);
745 args.v5.ucPpll = ATOM_DCPLL;
748 /* if the default dcpll clock is specified,
749 * SetPixelClock provides the dividers
751 args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
752 args.v6.ucPpll = ATOM_DCPLL;
755 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
760 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
763 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
766 static void atombios_crtc_program_pll(struct drm_crtc *crtc,
778 struct radeon_atom_ss *ss)
780 struct drm_device *dev = crtc->dev;
781 struct radeon_device *rdev = dev->dev_private;
783 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
784 union set_pixel_clock args;
786 memset(&args, 0, sizeof(args));
788 if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
796 if (clock == ATOM_DISABLE)
798 args.v1.usPixelClock = cpu_to_le16(clock / 10);
799 args.v1.usRefDiv = cpu_to_le16(ref_div);
800 args.v1.usFbDiv = cpu_to_le16(fb_div);
801 args.v1.ucFracFbDiv = frac_fb_div;
802 args.v1.ucPostDiv = post_div;
803 args.v1.ucPpll = pll_id;
804 args.v1.ucCRTC = crtc_id;
805 args.v1.ucRefDivSrc = 1;
808 args.v2.usPixelClock = cpu_to_le16(clock / 10);
809 args.v2.usRefDiv = cpu_to_le16(ref_div);
810 args.v2.usFbDiv = cpu_to_le16(fb_div);
811 args.v2.ucFracFbDiv = frac_fb_div;
812 args.v2.ucPostDiv = post_div;
813 args.v2.ucPpll = pll_id;
814 args.v2.ucCRTC = crtc_id;
815 args.v2.ucRefDivSrc = 1;
818 args.v3.usPixelClock = cpu_to_le16(clock / 10);
819 args.v3.usRefDiv = cpu_to_le16(ref_div);
820 args.v3.usFbDiv = cpu_to_le16(fb_div);
821 args.v3.ucFracFbDiv = frac_fb_div;
822 args.v3.ucPostDiv = post_div;
823 args.v3.ucPpll = pll_id;
824 args.v3.ucMiscInfo = (pll_id << 2);
825 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
826 args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
827 args.v3.ucTransmitterId = encoder_id;
828 args.v3.ucEncoderMode = encoder_mode;
831 args.v5.ucCRTC = crtc_id;
832 args.v5.usPixelClock = cpu_to_le16(clock / 10);
833 args.v5.ucRefDiv = ref_div;
834 args.v5.usFbDiv = cpu_to_le16(fb_div);
835 args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
836 args.v5.ucPostDiv = post_div;
837 args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
838 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
839 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
843 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
846 args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
849 args.v5.ucTransmitterID = encoder_id;
850 args.v5.ucEncoderMode = encoder_mode;
851 args.v5.ucPpll = pll_id;
854 args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
855 args.v6.ucRefDiv = ref_div;
856 args.v6.usFbDiv = cpu_to_le16(fb_div);
857 args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
858 args.v6.ucPostDiv = post_div;
859 args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
860 if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
861 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
865 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
868 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
871 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
874 args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
877 args.v6.ucTransmitterID = encoder_id;
878 args.v6.ucEncoderMode = encoder_mode;
879 args.v6.ucPpll = pll_id;
882 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
887 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
891 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
894 static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
896 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
897 struct drm_device *dev = crtc->dev;
898 struct radeon_device *rdev = dev->dev_private;
899 struct drm_encoder *encoder = NULL;
900 struct radeon_encoder *radeon_encoder = NULL;
901 u32 pll_clock = mode->clock;
902 u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
903 struct radeon_pll *pll;
905 int encoder_mode = 0;
906 struct radeon_atom_ss ss;
907 bool ss_enabled = false;
910 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
911 if (encoder->crtc == crtc) {
912 radeon_encoder = to_radeon_encoder(encoder);
913 encoder_mode = atombios_get_encoder_mode(encoder);
921 switch (radeon_crtc->pll_id) {
923 pll = &rdev->clock.p1pll;
926 pll = &rdev->clock.p2pll;
929 case ATOM_PPLL_INVALID:
931 pll = &rdev->clock.dcpll;
935 if (radeon_encoder->active_device &
936 (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) {
937 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
938 struct drm_connector *connector =
939 radeon_get_connector_for_encoder(encoder);
940 struct radeon_connector *radeon_connector =
941 to_radeon_connector(connector);
942 struct radeon_connector_atom_dig *dig_connector =
943 radeon_connector->con_priv;
945 bpc = connector->display_info.bpc;
947 switch (encoder_mode) {
948 case ATOM_ENCODER_MODE_DP:
950 dp_clock = dig_connector->dp_clock / 10;
951 if (ASIC_IS_DCE4(rdev))
953 radeon_atombios_get_asic_ss_info(rdev, &ss,
954 ASIC_INTERNAL_SS_ON_DP,
957 if (dp_clock == 16200) {
959 radeon_atombios_get_ppll_ss_info(rdev, &ss,
963 radeon_atombios_get_ppll_ss_info(rdev, &ss,
967 radeon_atombios_get_ppll_ss_info(rdev, &ss,
971 case ATOM_ENCODER_MODE_LVDS:
972 if (ASIC_IS_DCE4(rdev))
973 ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
977 ss_enabled = radeon_atombios_get_ppll_ss_info(rdev, &ss,
980 case ATOM_ENCODER_MODE_DVI:
981 if (ASIC_IS_DCE4(rdev))
983 radeon_atombios_get_asic_ss_info(rdev, &ss,
984 ASIC_INTERNAL_SS_ON_TMDS,
987 case ATOM_ENCODER_MODE_HDMI:
988 if (ASIC_IS_DCE4(rdev))
990 radeon_atombios_get_asic_ss_info(rdev, &ss,
991 ASIC_INTERNAL_SS_ON_HDMI,
999 /* adjust pixel clock as needed */
1000 adjusted_clock = atombios_adjust_pll(crtc, mode, pll, ss_enabled, &ss);
1002 if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
1003 /* TV seems to prefer the legacy algo on some boards */
1004 radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1005 &ref_div, &post_div);
1006 else if (ASIC_IS_AVIVO(rdev))
1007 radeon_compute_pll_avivo(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1008 &ref_div, &post_div);
1010 radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
1011 &ref_div, &post_div);
1013 atombios_crtc_program_ss(crtc, ATOM_DISABLE, radeon_crtc->pll_id, &ss);
1015 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1016 encoder_mode, radeon_encoder->encoder_id, mode->clock,
1017 ref_div, fb_div, frac_fb_div, post_div, bpc, ss_enabled, &ss);
1020 /* calculate ss amount and step size */
1021 if (ASIC_IS_DCE4(rdev)) {
1023 u32 amount = (((fb_div * 10) + frac_fb_div) * ss.percentage) / 10000;
1024 ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
1025 ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
1026 ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
1027 if (ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
1028 step_size = (4 * amount * ref_div * (ss.rate * 2048)) /
1029 (125 * 25 * pll->reference_freq / 100);
1031 step_size = (2 * amount * ref_div * (ss.rate * 2048)) /
1032 (125 * 25 * pll->reference_freq / 100);
1033 ss.step = step_size;
1036 atombios_crtc_program_ss(crtc, ATOM_ENABLE, radeon_crtc->pll_id, &ss);
1040 static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
1041 struct drm_framebuffer *fb,
1042 int x, int y, int atomic)
1044 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1045 struct drm_device *dev = crtc->dev;
1046 struct radeon_device *rdev = dev->dev_private;
1047 struct radeon_framebuffer *radeon_fb;
1048 struct drm_framebuffer *target_fb;
1049 struct drm_gem_object *obj;
1050 struct radeon_bo *rbo;
1051 uint64_t fb_location;
1052 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
1053 u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
1054 u32 tmp, viewport_w, viewport_h;
1058 if (!atomic && !crtc->fb) {
1059 DRM_DEBUG_KMS("No FB bound\n");
1064 radeon_fb = to_radeon_framebuffer(fb);
1068 radeon_fb = to_radeon_framebuffer(crtc->fb);
1069 target_fb = crtc->fb;
1072 /* If atomic, assume fb object is pinned & idle & fenced and
1073 * just update base pointers
1075 obj = radeon_fb->obj;
1076 rbo = gem_to_radeon_bo(obj);
1077 r = radeon_bo_reserve(rbo, false);
1078 if (unlikely(r != 0))
1082 fb_location = radeon_bo_gpu_offset(rbo);
1084 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1085 if (unlikely(r != 0)) {
1086 radeon_bo_unreserve(rbo);
1091 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1092 radeon_bo_unreserve(rbo);
1094 switch (target_fb->bits_per_pixel) {
1096 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
1097 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
1100 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1101 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
1104 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
1105 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
1107 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
1112 fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
1113 EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
1115 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
1119 DRM_ERROR("Unsupported screen depth %d\n",
1120 target_fb->bits_per_pixel);
1124 if (tiling_flags & RADEON_TILING_MACRO)
1125 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
1126 else if (tiling_flags & RADEON_TILING_MICRO)
1127 fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
1129 switch (radeon_crtc->crtc_id) {
1131 WREG32(AVIVO_D1VGA_CONTROL, 0);
1134 WREG32(AVIVO_D2VGA_CONTROL, 0);
1137 WREG32(EVERGREEN_D3VGA_CONTROL, 0);
1140 WREG32(EVERGREEN_D4VGA_CONTROL, 0);
1143 WREG32(EVERGREEN_D5VGA_CONTROL, 0);
1146 WREG32(EVERGREEN_D6VGA_CONTROL, 0);
1152 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1153 upper_32_bits(fb_location));
1154 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
1155 upper_32_bits(fb_location));
1156 WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1157 (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1158 WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1159 (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
1160 WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
1161 WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
1163 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1164 WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1165 WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
1166 WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
1167 WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1168 WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
1170 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
1171 WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1172 WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1174 WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1175 crtc->mode.vdisplay);
1178 WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
1180 viewport_w = crtc->mode.hdisplay;
1181 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
1182 WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1183 (viewport_w << 16) | viewport_h);
1185 /* pageflip setup */
1186 /* make sure flip is at vb rather than hb */
1187 tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1188 tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1189 WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1191 /* set pageflip to happen anywhere in vblank interval */
1192 WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1194 if (!atomic && fb && fb != crtc->fb) {
1195 radeon_fb = to_radeon_framebuffer(fb);
1196 rbo = gem_to_radeon_bo(radeon_fb->obj);
1197 r = radeon_bo_reserve(rbo, false);
1198 if (unlikely(r != 0))
1200 radeon_bo_unpin(rbo);
1201 radeon_bo_unreserve(rbo);
1204 /* Bytes per pixel may have changed */
1205 radeon_bandwidth_update(rdev);
1210 static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
1211 struct drm_framebuffer *fb,
1212 int x, int y, int atomic)
1214 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1215 struct drm_device *dev = crtc->dev;
1216 struct radeon_device *rdev = dev->dev_private;
1217 struct radeon_framebuffer *radeon_fb;
1218 struct drm_gem_object *obj;
1219 struct radeon_bo *rbo;
1220 struct drm_framebuffer *target_fb;
1221 uint64_t fb_location;
1222 uint32_t fb_format, fb_pitch_pixels, tiling_flags;
1223 u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
1224 u32 tmp, viewport_w, viewport_h;
1228 if (!atomic && !crtc->fb) {
1229 DRM_DEBUG_KMS("No FB bound\n");
1234 radeon_fb = to_radeon_framebuffer(fb);
1238 radeon_fb = to_radeon_framebuffer(crtc->fb);
1239 target_fb = crtc->fb;
1242 obj = radeon_fb->obj;
1243 rbo = gem_to_radeon_bo(obj);
1244 r = radeon_bo_reserve(rbo, false);
1245 if (unlikely(r != 0))
1248 /* If atomic, assume fb object is pinned & idle & fenced and
1249 * just update base pointers
1252 fb_location = radeon_bo_gpu_offset(rbo);
1254 r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
1255 if (unlikely(r != 0)) {
1256 radeon_bo_unreserve(rbo);
1260 radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
1261 radeon_bo_unreserve(rbo);
1263 switch (target_fb->bits_per_pixel) {
1266 AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
1267 AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
1271 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1272 AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
1276 AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
1277 AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
1279 fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
1285 AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
1286 AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
1288 fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
1292 DRM_ERROR("Unsupported screen depth %d\n",
1293 target_fb->bits_per_pixel);
1297 if (rdev->family >= CHIP_R600) {
1298 if (tiling_flags & RADEON_TILING_MACRO)
1299 fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
1300 else if (tiling_flags & RADEON_TILING_MICRO)
1301 fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
1303 if (tiling_flags & RADEON_TILING_MACRO)
1304 fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
1306 if (tiling_flags & RADEON_TILING_MICRO)
1307 fb_format |= AVIVO_D1GRPH_TILED;
1310 if (radeon_crtc->crtc_id == 0)
1311 WREG32(AVIVO_D1VGA_CONTROL, 0);
1313 WREG32(AVIVO_D2VGA_CONTROL, 0);
1315 if (rdev->family >= CHIP_RV770) {
1316 if (radeon_crtc->crtc_id) {
1317 WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1318 WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1320 WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1321 WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
1324 WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
1326 WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
1327 radeon_crtc->crtc_offset, (u32) fb_location);
1328 WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
1329 if (rdev->family >= CHIP_R600)
1330 WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
1332 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
1333 WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
1334 WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
1335 WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
1336 WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
1337 WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
1339 fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
1340 WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
1341 WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
1343 WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
1344 crtc->mode.vdisplay);
1347 WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
1349 viewport_w = crtc->mode.hdisplay;
1350 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
1351 WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
1352 (viewport_w << 16) | viewport_h);
1354 /* pageflip setup */
1355 /* make sure flip is at vb rather than hb */
1356 tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
1357 tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
1358 WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
1360 /* set pageflip to happen anywhere in vblank interval */
1361 WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
1363 if (!atomic && fb && fb != crtc->fb) {
1364 radeon_fb = to_radeon_framebuffer(fb);
1365 rbo = gem_to_radeon_bo(radeon_fb->obj);
1366 r = radeon_bo_reserve(rbo, false);
1367 if (unlikely(r != 0))
1369 radeon_bo_unpin(rbo);
1370 radeon_bo_unreserve(rbo);
1373 /* Bytes per pixel may have changed */
1374 radeon_bandwidth_update(rdev);
1379 int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
1380 struct drm_framebuffer *old_fb)
1382 struct drm_device *dev = crtc->dev;
1383 struct radeon_device *rdev = dev->dev_private;
1385 if (ASIC_IS_DCE4(rdev))
1386 return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
1387 else if (ASIC_IS_AVIVO(rdev))
1388 return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
1390 return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
1393 int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
1394 struct drm_framebuffer *fb,
1395 int x, int y, enum mode_set_atomic state)
1397 struct drm_device *dev = crtc->dev;
1398 struct radeon_device *rdev = dev->dev_private;
1400 if (ASIC_IS_DCE4(rdev))
1401 return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
1402 else if (ASIC_IS_AVIVO(rdev))
1403 return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
1405 return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
1408 /* properly set additional regs when using atombios */
1409 static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
1411 struct drm_device *dev = crtc->dev;
1412 struct radeon_device *rdev = dev->dev_private;
1413 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1414 u32 disp_merge_cntl;
1416 switch (radeon_crtc->crtc_id) {
1418 disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
1419 disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
1420 WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
1423 disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
1424 disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
1425 WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
1426 WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
1427 WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
1432 static int radeon_atom_pick_pll(struct drm_crtc *crtc)
1434 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1435 struct drm_device *dev = crtc->dev;
1436 struct radeon_device *rdev = dev->dev_private;
1437 struct drm_encoder *test_encoder;
1438 struct drm_crtc *test_crtc;
1439 uint32_t pll_in_use = 0;
1441 if (ASIC_IS_DCE4(rdev)) {
1442 list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
1443 if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
1444 /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
1445 * depending on the asic:
1446 * DCE4: PPLL or ext clock
1447 * DCE5: DCPLL or ext clock
1449 * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
1450 * PPLL/DCPLL programming and only program the DP DTO for the
1451 * crtc virtual pixel clock.
1453 if (atombios_get_encoder_mode(test_encoder) == ATOM_ENCODER_MODE_DP) {
1454 if (ASIC_IS_DCE5(rdev) || rdev->clock.dp_extclk)
1455 return ATOM_PPLL_INVALID;
1460 /* otherwise, pick one of the plls */
1461 list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
1462 struct radeon_crtc *radeon_test_crtc;
1464 if (crtc == test_crtc)
1467 radeon_test_crtc = to_radeon_crtc(test_crtc);
1468 if ((radeon_test_crtc->pll_id >= ATOM_PPLL1) &&
1469 (radeon_test_crtc->pll_id <= ATOM_PPLL2))
1470 pll_in_use |= (1 << radeon_test_crtc->pll_id);
1472 if (!(pll_in_use & 1))
1476 return radeon_crtc->crtc_id;
1480 int atombios_crtc_mode_set(struct drm_crtc *crtc,
1481 struct drm_display_mode *mode,
1482 struct drm_display_mode *adjusted_mode,
1483 int x, int y, struct drm_framebuffer *old_fb)
1485 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1486 struct drm_device *dev = crtc->dev;
1487 struct radeon_device *rdev = dev->dev_private;
1488 struct drm_encoder *encoder;
1489 bool is_tvcv = false;
1491 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1493 if (encoder->crtc == crtc) {
1494 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1495 if (radeon_encoder->active_device &
1496 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
1501 /* always set DCPLL */
1502 if (ASIC_IS_DCE4(rdev)) {
1503 struct radeon_atom_ss ss;
1504 bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
1505 ASIC_INTERNAL_SS_ON_DCPLL,
1506 rdev->clock.default_dispclk);
1508 atombios_crtc_program_ss(crtc, ATOM_DISABLE, ATOM_DCPLL, &ss);
1509 /* XXX: DCE5, make sure voltage, dispclk is high enough */
1510 atombios_crtc_set_dcpll(crtc, rdev->clock.default_dispclk);
1512 atombios_crtc_program_ss(crtc, ATOM_ENABLE, ATOM_DCPLL, &ss);
1514 atombios_crtc_set_pll(crtc, adjusted_mode);
1516 if (ASIC_IS_DCE4(rdev))
1517 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1518 else if (ASIC_IS_AVIVO(rdev)) {
1520 atombios_crtc_set_timing(crtc, adjusted_mode);
1522 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1524 atombios_crtc_set_timing(crtc, adjusted_mode);
1525 if (radeon_crtc->crtc_id == 0)
1526 atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
1527 radeon_legacy_atom_fixup(crtc);
1529 atombios_crtc_set_base(crtc, x, y, old_fb);
1530 atombios_overscan_setup(crtc, mode, adjusted_mode);
1531 atombios_scaler_setup(crtc);
1535 static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
1536 struct drm_display_mode *mode,
1537 struct drm_display_mode *adjusted_mode)
1539 struct drm_device *dev = crtc->dev;
1540 struct radeon_device *rdev = dev->dev_private;
1542 /* adjust pm to upcoming mode change */
1543 radeon_pm_compute_clocks(rdev);
1545 if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
1550 static void atombios_crtc_prepare(struct drm_crtc *crtc)
1552 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1555 radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
1557 atombios_lock_crtc(crtc, ATOM_ENABLE);
1558 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1561 static void atombios_crtc_commit(struct drm_crtc *crtc)
1563 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
1564 atombios_lock_crtc(crtc, ATOM_DISABLE);
1567 static void atombios_crtc_disable(struct drm_crtc *crtc)
1569 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1570 struct radeon_atom_ss ss;
1572 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
1574 switch (radeon_crtc->pll_id) {
1577 /* disable the ppll */
1578 atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
1579 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
1584 radeon_crtc->pll_id = -1;
1587 static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
1588 .dpms = atombios_crtc_dpms,
1589 .mode_fixup = atombios_crtc_mode_fixup,
1590 .mode_set = atombios_crtc_mode_set,
1591 .mode_set_base = atombios_crtc_set_base,
1592 .mode_set_base_atomic = atombios_crtc_set_base_atomic,
1593 .prepare = atombios_crtc_prepare,
1594 .commit = atombios_crtc_commit,
1595 .load_lut = radeon_crtc_load_lut,
1596 .disable = atombios_crtc_disable,
1599 void radeon_atombios_init_crtc(struct drm_device *dev,
1600 struct radeon_crtc *radeon_crtc)
1602 struct radeon_device *rdev = dev->dev_private;
1604 if (ASIC_IS_DCE4(rdev)) {
1605 switch (radeon_crtc->crtc_id) {
1608 radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
1611 radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
1614 radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
1617 radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
1620 radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
1623 radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
1627 if (radeon_crtc->crtc_id == 1)
1628 radeon_crtc->crtc_offset =
1629 AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
1631 radeon_crtc->crtc_offset = 0;
1633 radeon_crtc->pll_id = -1;
1634 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);