Home | History | Annotate | Line # | Download | only in radeon
      1  1.3  riastrad /*	$NetBSD: radeon_rv770_dpm.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2011 Advanced Micro Devices, Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Alex Deucher
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.1  riastrad #include <sys/cdefs.h>
     28  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_rv770_dpm.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     29  1.1  riastrad 
     30  1.1  riastrad #include "radeon.h"
     31  1.1  riastrad #include "radeon_asic.h"
     32  1.1  riastrad #include "rv770d.h"
     33  1.1  riastrad #include "r600_dpm.h"
     34  1.1  riastrad #include "rv770_dpm.h"
     35  1.1  riastrad #include "cypress_dpm.h"
     36  1.1  riastrad #include "atom.h"
     37  1.1  riastrad #include <linux/seq_file.h>
     38  1.1  riastrad 
     39  1.1  riastrad #define MC_CG_ARB_FREQ_F0           0x0a
     40  1.1  riastrad #define MC_CG_ARB_FREQ_F1           0x0b
     41  1.1  riastrad #define MC_CG_ARB_FREQ_F2           0x0c
     42  1.1  riastrad #define MC_CG_ARB_FREQ_F3           0x0d
     43  1.1  riastrad 
     44  1.1  riastrad #define MC_CG_SEQ_DRAMCONF_S0       0x05
     45  1.1  riastrad #define MC_CG_SEQ_DRAMCONF_S1       0x06
     46  1.1  riastrad 
     47  1.1  riastrad #define PCIE_BUS_CLK                10000
     48  1.1  riastrad #define TCLK                        (PCIE_BUS_CLK / 10)
     49  1.1  riastrad 
     50  1.1  riastrad #define SMC_RAM_END 0xC000
     51  1.1  riastrad 
     52  1.1  riastrad struct rv7xx_ps *rv770_get_ps(struct radeon_ps *rps)
     53  1.1  riastrad {
     54  1.1  riastrad 	struct rv7xx_ps *ps = rps->ps_priv;
     55  1.1  riastrad 
     56  1.1  riastrad 	return ps;
     57  1.1  riastrad }
     58  1.1  riastrad 
     59  1.1  riastrad struct rv7xx_power_info *rv770_get_pi(struct radeon_device *rdev)
     60  1.1  riastrad {
     61  1.1  riastrad 	struct rv7xx_power_info *pi = rdev->pm.dpm.priv;
     62  1.1  riastrad 
     63  1.1  riastrad 	return pi;
     64  1.1  riastrad }
     65  1.1  riastrad 
     66  1.1  riastrad struct evergreen_power_info *evergreen_get_pi(struct radeon_device *rdev)
     67  1.1  riastrad {
     68  1.1  riastrad 	struct evergreen_power_info *pi = rdev->pm.dpm.priv;
     69  1.1  riastrad 
     70  1.1  riastrad 	return pi;
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad static void rv770_enable_bif_dynamic_pcie_gen2(struct radeon_device *rdev,
     74  1.1  riastrad 					       bool enable)
     75  1.1  riastrad {
     76  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
     77  1.1  riastrad 	u32 tmp;
     78  1.1  riastrad 
     79  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
     80  1.1  riastrad 	if (enable) {
     81  1.1  riastrad 		tmp &= ~LC_HW_VOLTAGE_IF_CONTROL_MASK;
     82  1.1  riastrad 		tmp |= LC_HW_VOLTAGE_IF_CONTROL(1);
     83  1.1  riastrad 		tmp |= LC_GEN2_EN_STRAP;
     84  1.1  riastrad 	} else {
     85  1.1  riastrad 		if (!pi->boot_in_gen2) {
     86  1.1  riastrad 			tmp &= ~LC_HW_VOLTAGE_IF_CONTROL_MASK;
     87  1.1  riastrad 			tmp &= ~LC_GEN2_EN_STRAP;
     88  1.1  riastrad 		}
     89  1.1  riastrad 	}
     90  1.1  riastrad 	if ((tmp & LC_OTHER_SIDE_EVER_SENT_GEN2) ||
     91  1.1  riastrad 	    (tmp & LC_OTHER_SIDE_SUPPORTS_GEN2))
     92  1.1  riastrad 		WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
     93  1.1  riastrad 
     94  1.1  riastrad }
     95  1.1  riastrad 
     96  1.1  riastrad static void rv770_enable_l0s(struct radeon_device *rdev)
     97  1.1  riastrad {
     98  1.1  riastrad 	u32 tmp;
     99  1.1  riastrad 
    100  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL) & ~LC_L0S_INACTIVITY_MASK;
    101  1.1  riastrad 	tmp |= LC_L0S_INACTIVITY(3);
    102  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    103  1.1  riastrad }
    104  1.1  riastrad 
    105  1.1  riastrad static void rv770_enable_l1(struct radeon_device *rdev)
    106  1.1  riastrad {
    107  1.1  riastrad 	u32 tmp;
    108  1.1  riastrad 
    109  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL);
    110  1.1  riastrad 	tmp &= ~LC_L1_INACTIVITY_MASK;
    111  1.1  riastrad 	tmp |= LC_L1_INACTIVITY(4);
    112  1.1  riastrad 	tmp &= ~LC_PMI_TO_L1_DIS;
    113  1.1  riastrad 	tmp &= ~LC_ASPM_TO_L1_DIS;
    114  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    115  1.1  riastrad }
    116  1.1  riastrad 
    117  1.1  riastrad static void rv770_enable_pll_sleep_in_l1(struct radeon_device *rdev)
    118  1.1  riastrad {
    119  1.1  riastrad 	u32 tmp;
    120  1.1  riastrad 
    121  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL) & ~LC_L1_INACTIVITY_MASK;
    122  1.1  riastrad 	tmp |= LC_L1_INACTIVITY(8);
    123  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    124  1.1  riastrad 
    125  1.1  riastrad 	/* NOTE, this is a PCIE indirect reg, not PCIE PORT */
    126  1.1  riastrad 	tmp = RREG32_PCIE(PCIE_P_CNTL);
    127  1.1  riastrad 	tmp |= P_PLL_PWRDN_IN_L1L23;
    128  1.1  riastrad 	tmp &= ~P_PLL_BUF_PDNB;
    129  1.1  riastrad 	tmp &= ~P_PLL_PDNB;
    130  1.1  riastrad 	tmp |= P_ALLOW_PRX_FRONTEND_SHUTOFF;
    131  1.1  riastrad 	WREG32_PCIE(PCIE_P_CNTL, tmp);
    132  1.1  riastrad }
    133  1.1  riastrad 
    134  1.1  riastrad static void rv770_gfx_clock_gating_enable(struct radeon_device *rdev,
    135  1.1  riastrad 					  bool enable)
    136  1.1  riastrad {
    137  1.1  riastrad 	if (enable)
    138  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, DYN_GFX_CLK_OFF_EN, ~DYN_GFX_CLK_OFF_EN);
    139  1.1  riastrad 	else {
    140  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~DYN_GFX_CLK_OFF_EN);
    141  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, GFX_CLK_FORCE_ON, ~GFX_CLK_FORCE_ON);
    142  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~GFX_CLK_FORCE_ON);
    143  1.1  riastrad 		RREG32(GB_TILING_CONFIG);
    144  1.1  riastrad 	}
    145  1.1  riastrad }
    146  1.1  riastrad 
    147  1.1  riastrad static void rv770_mg_clock_gating_enable(struct radeon_device *rdev,
    148  1.1  riastrad 					 bool enable)
    149  1.1  riastrad {
    150  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    151  1.1  riastrad 
    152  1.1  riastrad 	if (enable) {
    153  1.1  riastrad 		u32 mgcg_cgtt_local0;
    154  1.1  riastrad 
    155  1.1  riastrad 		if (rdev->family == CHIP_RV770)
    156  1.1  riastrad 			mgcg_cgtt_local0 = RV770_MGCGTTLOCAL0_DFLT;
    157  1.1  riastrad 		else
    158  1.1  riastrad 			mgcg_cgtt_local0 = RV7XX_MGCGTTLOCAL0_DFLT;
    159  1.1  riastrad 
    160  1.1  riastrad 		WREG32(CG_CGTT_LOCAL_0, mgcg_cgtt_local0);
    161  1.1  riastrad 		WREG32(CG_CGTT_LOCAL_1, (RV770_MGCGTTLOCAL1_DFLT & 0xFFFFCFFF));
    162  1.1  riastrad 
    163  1.1  riastrad 		if (pi->mgcgtssm)
    164  1.1  riastrad 			WREG32(CGTS_SM_CTRL_REG, RV770_MGCGCGTSSMCTRL_DFLT);
    165  1.1  riastrad 	} else {
    166  1.1  riastrad 		WREG32(CG_CGTT_LOCAL_0, 0xFFFFFFFF);
    167  1.1  riastrad 		WREG32(CG_CGTT_LOCAL_1, 0xFFFFCFFF);
    168  1.1  riastrad 	}
    169  1.1  riastrad }
    170  1.1  riastrad 
    171  1.1  riastrad void rv770_restore_cgcg(struct radeon_device *rdev)
    172  1.1  riastrad {
    173  1.1  riastrad 	bool dpm_en = false, cg_en = false;
    174  1.1  riastrad 
    175  1.1  riastrad 	if (RREG32(GENERAL_PWRMGT) & GLOBAL_PWRMGT_EN)
    176  1.1  riastrad 		dpm_en = true;
    177  1.1  riastrad 	if (RREG32(SCLK_PWRMGT_CNTL) & DYN_GFX_CLK_OFF_EN)
    178  1.1  riastrad 		cg_en = true;
    179  1.1  riastrad 
    180  1.1  riastrad 	if (dpm_en && !cg_en)
    181  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, DYN_GFX_CLK_OFF_EN, ~DYN_GFX_CLK_OFF_EN);
    182  1.1  riastrad }
    183  1.1  riastrad 
    184  1.1  riastrad static void rv770_start_dpm(struct radeon_device *rdev)
    185  1.1  riastrad {
    186  1.1  riastrad 	WREG32_P(SCLK_PWRMGT_CNTL, 0, ~SCLK_PWRMGT_OFF);
    187  1.1  riastrad 
    188  1.1  riastrad 	WREG32_P(MCLK_PWRMGT_CNTL, 0, ~MPLL_PWRMGT_OFF);
    189  1.1  riastrad 
    190  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, GLOBAL_PWRMGT_EN, ~GLOBAL_PWRMGT_EN);
    191  1.1  riastrad }
    192  1.1  riastrad 
    193  1.1  riastrad void rv770_stop_dpm(struct radeon_device *rdev)
    194  1.1  riastrad {
    195  1.1  riastrad 	PPSMC_Result result;
    196  1.1  riastrad 
    197  1.1  riastrad 	result = rv770_send_msg_to_smc(rdev, PPSMC_MSG_TwoLevelsDisabled);
    198  1.1  riastrad 
    199  1.1  riastrad 	if (result != PPSMC_Result_OK)
    200  1.1  riastrad 		DRM_DEBUG("Could not force DPM to low.\n");
    201  1.1  riastrad 
    202  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, 0, ~GLOBAL_PWRMGT_EN);
    203  1.1  riastrad 
    204  1.1  riastrad 	WREG32_P(SCLK_PWRMGT_CNTL, SCLK_PWRMGT_OFF, ~SCLK_PWRMGT_OFF);
    205  1.1  riastrad 
    206  1.1  riastrad 	WREG32_P(MCLK_PWRMGT_CNTL, MPLL_PWRMGT_OFF, ~MPLL_PWRMGT_OFF);
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.1  riastrad bool rv770_dpm_enabled(struct radeon_device *rdev)
    210  1.1  riastrad {
    211  1.1  riastrad 	if (RREG32(GENERAL_PWRMGT) & GLOBAL_PWRMGT_EN)
    212  1.1  riastrad 		return true;
    213  1.1  riastrad 	else
    214  1.1  riastrad 		return false;
    215  1.1  riastrad }
    216  1.1  riastrad 
    217  1.1  riastrad void rv770_enable_thermal_protection(struct radeon_device *rdev,
    218  1.1  riastrad 				     bool enable)
    219  1.1  riastrad {
    220  1.1  riastrad 	if (enable)
    221  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~THERMAL_PROTECTION_DIS);
    222  1.1  riastrad 	else
    223  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, ~THERMAL_PROTECTION_DIS);
    224  1.1  riastrad }
    225  1.1  riastrad 
    226  1.1  riastrad void rv770_enable_acpi_pm(struct radeon_device *rdev)
    227  1.1  riastrad {
    228  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, STATIC_PM_EN, ~STATIC_PM_EN);
    229  1.1  riastrad }
    230  1.1  riastrad 
    231  1.1  riastrad u8 rv770_get_seq_value(struct radeon_device *rdev,
    232  1.1  riastrad 		       struct rv7xx_pl *pl)
    233  1.1  riastrad {
    234  1.1  riastrad 	return (pl->flags & ATOM_PPLIB_R600_FLAGS_LOWPOWER) ?
    235  1.1  riastrad 		MC_CG_SEQ_DRAMCONF_S0 : MC_CG_SEQ_DRAMCONF_S1;
    236  1.1  riastrad }
    237  1.1  riastrad 
    238  1.1  riastrad #if 0
    239  1.1  riastrad int rv770_read_smc_soft_register(struct radeon_device *rdev,
    240  1.1  riastrad 				 u16 reg_offset, u32 *value)
    241  1.1  riastrad {
    242  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    243  1.1  riastrad 
    244  1.1  riastrad 	return rv770_read_smc_sram_dword(rdev,
    245  1.1  riastrad 					 pi->soft_regs_start + reg_offset,
    246  1.1  riastrad 					 value, pi->sram_end);
    247  1.1  riastrad }
    248  1.1  riastrad #endif
    249  1.1  riastrad 
    250  1.1  riastrad int rv770_write_smc_soft_register(struct radeon_device *rdev,
    251  1.1  riastrad 				  u16 reg_offset, u32 value)
    252  1.1  riastrad {
    253  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    254  1.1  riastrad 
    255  1.1  riastrad 	return rv770_write_smc_sram_dword(rdev,
    256  1.1  riastrad 					  pi->soft_regs_start + reg_offset,
    257  1.1  riastrad 					  value, pi->sram_end);
    258  1.1  riastrad }
    259  1.1  riastrad 
    260  1.1  riastrad int rv770_populate_smc_t(struct radeon_device *rdev,
    261  1.1  riastrad 			 struct radeon_ps *radeon_state,
    262  1.1  riastrad 			 RV770_SMC_SWSTATE *smc_state)
    263  1.1  riastrad {
    264  1.1  riastrad 	struct rv7xx_ps *state = rv770_get_ps(radeon_state);
    265  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    266  1.1  riastrad 	int i;
    267  1.1  riastrad 	int a_n;
    268  1.1  riastrad 	int a_d;
    269  1.1  riastrad 	u8 l[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE];
    270  1.1  riastrad 	u8 r[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE];
    271  1.1  riastrad 	u32 a_t;
    272  1.1  riastrad 
    273  1.1  riastrad 	l[0] = 0;
    274  1.1  riastrad 	r[2] = 100;
    275  1.1  riastrad 
    276  1.1  riastrad 	a_n = (int)state->medium.sclk * pi->lmp +
    277  1.1  riastrad 		(int)state->low.sclk * (R600_AH_DFLT - pi->rlp);
    278  1.1  riastrad 	a_d = (int)state->low.sclk * (100 - (int)pi->rlp) +
    279  1.1  riastrad 		(int)state->medium.sclk * pi->lmp;
    280  1.1  riastrad 
    281  1.1  riastrad 	l[1] = (u8)(pi->lmp - (int)pi->lmp * a_n / a_d);
    282  1.1  riastrad 	r[0] = (u8)(pi->rlp + (100 - (int)pi->rlp) * a_n / a_d);
    283  1.1  riastrad 
    284  1.1  riastrad 	a_n = (int)state->high.sclk * pi->lhp + (int)state->medium.sclk *
    285  1.1  riastrad 		(R600_AH_DFLT - pi->rmp);
    286  1.1  riastrad 	a_d = (int)state->medium.sclk * (100 - (int)pi->rmp) +
    287  1.1  riastrad 		(int)state->high.sclk * pi->lhp;
    288  1.1  riastrad 
    289  1.1  riastrad 	l[2] = (u8)(pi->lhp - (int)pi->lhp * a_n / a_d);
    290  1.1  riastrad 	r[1] = (u8)(pi->rmp + (100 - (int)pi->rmp) * a_n / a_d);
    291  1.1  riastrad 
    292  1.1  riastrad 	for (i = 0; i < (RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1); i++) {
    293  1.1  riastrad 		a_t = CG_R(r[i] * pi->bsp / 200) | CG_L(l[i] * pi->bsp / 200);
    294  1.1  riastrad 		smc_state->levels[i].aT = cpu_to_be32(a_t);
    295  1.1  riastrad 	}
    296  1.1  riastrad 
    297  1.1  riastrad 	a_t = CG_R(r[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1] * pi->pbsp / 200) |
    298  1.1  riastrad 		CG_L(l[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1] * pi->pbsp / 200);
    299  1.1  riastrad 
    300  1.1  riastrad 	smc_state->levels[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1].aT =
    301  1.1  riastrad 		cpu_to_be32(a_t);
    302  1.1  riastrad 
    303  1.1  riastrad 	return 0;
    304  1.1  riastrad }
    305  1.1  riastrad 
    306  1.1  riastrad int rv770_populate_smc_sp(struct radeon_device *rdev,
    307  1.1  riastrad 			  struct radeon_ps *radeon_state,
    308  1.1  riastrad 			  RV770_SMC_SWSTATE *smc_state)
    309  1.1  riastrad {
    310  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    311  1.1  riastrad 	int i;
    312  1.1  riastrad 
    313  1.1  riastrad 	for (i = 0; i < (RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1); i++)
    314  1.1  riastrad 		smc_state->levels[i].bSP = cpu_to_be32(pi->dsp);
    315  1.1  riastrad 
    316  1.1  riastrad 	smc_state->levels[RV770_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1].bSP =
    317  1.1  riastrad 		cpu_to_be32(pi->psp);
    318  1.1  riastrad 
    319  1.1  riastrad 	return 0;
    320  1.1  riastrad }
    321  1.1  riastrad 
    322  1.1  riastrad static void rv770_calculate_fractional_mpll_feedback_divider(u32 memory_clock,
    323  1.1  riastrad 							     u32 reference_clock,
    324  1.1  riastrad 							     bool gddr5,
    325  1.1  riastrad 							     struct atom_clock_dividers *dividers,
    326  1.1  riastrad 							     u32 *clkf,
    327  1.1  riastrad 							     u32 *clkfrac)
    328  1.1  riastrad {
    329  1.1  riastrad 	u32 post_divider, reference_divider, feedback_divider8;
    330  1.1  riastrad 	u32 fyclk;
    331  1.1  riastrad 
    332  1.1  riastrad 	if (gddr5)
    333  1.1  riastrad 		fyclk = (memory_clock * 8) / 2;
    334  1.1  riastrad 	else
    335  1.1  riastrad 		fyclk = (memory_clock * 4) / 2;
    336  1.1  riastrad 
    337  1.1  riastrad 	post_divider = dividers->post_div;
    338  1.1  riastrad 	reference_divider = dividers->ref_div;
    339  1.1  riastrad 
    340  1.1  riastrad 	feedback_divider8 =
    341  1.1  riastrad 		(8 * fyclk * reference_divider * post_divider) / reference_clock;
    342  1.1  riastrad 
    343  1.1  riastrad 	*clkf = feedback_divider8 / 8;
    344  1.1  riastrad 	*clkfrac = feedback_divider8 % 8;
    345  1.1  riastrad }
    346  1.1  riastrad 
    347  1.1  riastrad static int rv770_encode_yclk_post_div(u32 postdiv, u32 *encoded_postdiv)
    348  1.1  riastrad {
    349  1.1  riastrad 	int ret = 0;
    350  1.1  riastrad 
    351  1.1  riastrad 	switch (postdiv) {
    352  1.3  riastrad 	case 1:
    353  1.1  riastrad 		*encoded_postdiv = 0;
    354  1.1  riastrad 		break;
    355  1.3  riastrad 	case 2:
    356  1.1  riastrad 		*encoded_postdiv = 1;
    357  1.1  riastrad 		break;
    358  1.3  riastrad 	case 4:
    359  1.1  riastrad 		*encoded_postdiv = 2;
    360  1.1  riastrad 		break;
    361  1.3  riastrad 	case 8:
    362  1.1  riastrad 		*encoded_postdiv = 3;
    363  1.1  riastrad 		break;
    364  1.3  riastrad 	case 16:
    365  1.1  riastrad 		*encoded_postdiv = 4;
    366  1.1  riastrad 		break;
    367  1.3  riastrad 	default:
    368  1.1  riastrad 		ret = -EINVAL;
    369  1.1  riastrad 		break;
    370  1.1  riastrad 	}
    371  1.1  riastrad 
    372  1.3  riastrad 	return ret;
    373  1.1  riastrad }
    374  1.1  riastrad 
    375  1.1  riastrad u32 rv770_map_clkf_to_ibias(struct radeon_device *rdev, u32 clkf)
    376  1.1  riastrad {
    377  1.1  riastrad 	if (clkf <= 0x10)
    378  1.1  riastrad 		return 0x4B;
    379  1.1  riastrad 	if (clkf <= 0x19)
    380  1.1  riastrad 		return 0x5B;
    381  1.1  riastrad 	if (clkf <= 0x21)
    382  1.1  riastrad 		return 0x2B;
    383  1.1  riastrad 	if (clkf <= 0x27)
    384  1.1  riastrad 		return 0x6C;
    385  1.1  riastrad 	if (clkf <= 0x31)
    386  1.1  riastrad 		return 0x9D;
    387  1.1  riastrad 	return 0xC6;
    388  1.1  riastrad }
    389  1.1  riastrad 
    390  1.1  riastrad static int rv770_populate_mclk_value(struct radeon_device *rdev,
    391  1.1  riastrad 				     u32 engine_clock, u32 memory_clock,
    392  1.1  riastrad 				     RV7XX_SMC_MCLK_VALUE *mclk)
    393  1.1  riastrad {
    394  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    395  1.1  riastrad 	u8 encoded_reference_dividers[] = { 0, 16, 17, 20, 21 };
    396  1.1  riastrad 	u32 mpll_ad_func_cntl =
    397  1.1  riastrad 		pi->clk_regs.rv770.mpll_ad_func_cntl;
    398  1.1  riastrad 	u32 mpll_ad_func_cntl_2 =
    399  1.1  riastrad 		pi->clk_regs.rv770.mpll_ad_func_cntl_2;
    400  1.1  riastrad 	u32 mpll_dq_func_cntl =
    401  1.1  riastrad 		pi->clk_regs.rv770.mpll_dq_func_cntl;
    402  1.1  riastrad 	u32 mpll_dq_func_cntl_2 =
    403  1.1  riastrad 		pi->clk_regs.rv770.mpll_dq_func_cntl_2;
    404  1.1  riastrad 	u32 mclk_pwrmgt_cntl =
    405  1.1  riastrad 		pi->clk_regs.rv770.mclk_pwrmgt_cntl;
    406  1.1  riastrad 	u32 dll_cntl = pi->clk_regs.rv770.dll_cntl;
    407  1.1  riastrad 	struct atom_clock_dividers dividers;
    408  1.1  riastrad 	u32 reference_clock = rdev->clock.mpll.reference_freq;
    409  1.1  riastrad 	u32 clkf, clkfrac;
    410  1.1  riastrad 	u32 postdiv_yclk;
    411  1.1  riastrad 	u32 ibias;
    412  1.1  riastrad 	int ret;
    413  1.1  riastrad 
    414  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_MEMORY_PLL_PARAM,
    415  1.1  riastrad 					     memory_clock, false, &dividers);
    416  1.1  riastrad 	if (ret)
    417  1.1  riastrad 		return ret;
    418  1.1  riastrad 
    419  1.1  riastrad 	if ((dividers.ref_div < 1) || (dividers.ref_div > 5))
    420  1.1  riastrad 		return -EINVAL;
    421  1.1  riastrad 
    422  1.1  riastrad 	rv770_calculate_fractional_mpll_feedback_divider(memory_clock, reference_clock,
    423  1.1  riastrad 							 pi->mem_gddr5,
    424  1.1  riastrad 							 &dividers, &clkf, &clkfrac);
    425  1.1  riastrad 
    426  1.1  riastrad 	ret = rv770_encode_yclk_post_div(dividers.post_div, &postdiv_yclk);
    427  1.1  riastrad 	if (ret)
    428  1.1  riastrad 		return ret;
    429  1.1  riastrad 
    430  1.1  riastrad 	ibias = rv770_map_clkf_to_ibias(rdev, clkf);
    431  1.1  riastrad 
    432  1.1  riastrad 	mpll_ad_func_cntl &= ~(CLKR_MASK |
    433  1.1  riastrad 			       YCLK_POST_DIV_MASK |
    434  1.1  riastrad 			       CLKF_MASK |
    435  1.1  riastrad 			       CLKFRAC_MASK |
    436  1.1  riastrad 			       IBIAS_MASK);
    437  1.1  riastrad 	mpll_ad_func_cntl |= CLKR(encoded_reference_dividers[dividers.ref_div - 1]);
    438  1.1  riastrad 	mpll_ad_func_cntl |= YCLK_POST_DIV(postdiv_yclk);
    439  1.1  riastrad 	mpll_ad_func_cntl |= CLKF(clkf);
    440  1.1  riastrad 	mpll_ad_func_cntl |= CLKFRAC(clkfrac);
    441  1.1  riastrad 	mpll_ad_func_cntl |= IBIAS(ibias);
    442  1.1  riastrad 
    443  1.1  riastrad 	if (dividers.vco_mode)
    444  1.1  riastrad 		mpll_ad_func_cntl_2 |= VCO_MODE;
    445  1.1  riastrad 	else
    446  1.1  riastrad 		mpll_ad_func_cntl_2 &= ~VCO_MODE;
    447  1.1  riastrad 
    448  1.1  riastrad 	if (pi->mem_gddr5) {
    449  1.1  riastrad 		rv770_calculate_fractional_mpll_feedback_divider(memory_clock,
    450  1.1  riastrad 								 reference_clock,
    451  1.1  riastrad 								 pi->mem_gddr5,
    452  1.1  riastrad 								 &dividers, &clkf, &clkfrac);
    453  1.1  riastrad 
    454  1.1  riastrad 		ibias = rv770_map_clkf_to_ibias(rdev, clkf);
    455  1.1  riastrad 
    456  1.1  riastrad 		ret = rv770_encode_yclk_post_div(dividers.post_div, &postdiv_yclk);
    457  1.1  riastrad 		if (ret)
    458  1.1  riastrad 			return ret;
    459  1.1  riastrad 
    460  1.1  riastrad 		mpll_dq_func_cntl &= ~(CLKR_MASK |
    461  1.1  riastrad 				       YCLK_POST_DIV_MASK |
    462  1.1  riastrad 				       CLKF_MASK |
    463  1.1  riastrad 				       CLKFRAC_MASK |
    464  1.1  riastrad 				       IBIAS_MASK);
    465  1.1  riastrad 		mpll_dq_func_cntl |= CLKR(encoded_reference_dividers[dividers.ref_div - 1]);
    466  1.1  riastrad 		mpll_dq_func_cntl |= YCLK_POST_DIV(postdiv_yclk);
    467  1.1  riastrad 		mpll_dq_func_cntl |= CLKF(clkf);
    468  1.1  riastrad 		mpll_dq_func_cntl |= CLKFRAC(clkfrac);
    469  1.1  riastrad 		mpll_dq_func_cntl |= IBIAS(ibias);
    470  1.1  riastrad 
    471  1.1  riastrad 		if (dividers.vco_mode)
    472  1.1  riastrad 			mpll_dq_func_cntl_2 |= VCO_MODE;
    473  1.1  riastrad 		else
    474  1.1  riastrad 			mpll_dq_func_cntl_2 &= ~VCO_MODE;
    475  1.1  riastrad 	}
    476  1.1  riastrad 
    477  1.1  riastrad 	mclk->mclk770.mclk_value = cpu_to_be32(memory_clock);
    478  1.1  riastrad 	mclk->mclk770.vMPLL_AD_FUNC_CNTL = cpu_to_be32(mpll_ad_func_cntl);
    479  1.1  riastrad 	mclk->mclk770.vMPLL_AD_FUNC_CNTL_2 = cpu_to_be32(mpll_ad_func_cntl_2);
    480  1.1  riastrad 	mclk->mclk770.vMPLL_DQ_FUNC_CNTL = cpu_to_be32(mpll_dq_func_cntl);
    481  1.1  riastrad 	mclk->mclk770.vMPLL_DQ_FUNC_CNTL_2 = cpu_to_be32(mpll_dq_func_cntl_2);
    482  1.1  riastrad 	mclk->mclk770.vMCLK_PWRMGT_CNTL = cpu_to_be32(mclk_pwrmgt_cntl);
    483  1.1  riastrad 	mclk->mclk770.vDLL_CNTL = cpu_to_be32(dll_cntl);
    484  1.1  riastrad 
    485  1.1  riastrad 	return 0;
    486  1.1  riastrad }
    487  1.1  riastrad 
    488  1.1  riastrad static int rv770_populate_sclk_value(struct radeon_device *rdev,
    489  1.1  riastrad 				     u32 engine_clock,
    490  1.1  riastrad 				     RV770_SMC_SCLK_VALUE *sclk)
    491  1.1  riastrad {
    492  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    493  1.1  riastrad 	struct atom_clock_dividers dividers;
    494  1.1  riastrad 	u32 spll_func_cntl =
    495  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl;
    496  1.1  riastrad 	u32 spll_func_cntl_2 =
    497  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl_2;
    498  1.1  riastrad 	u32 spll_func_cntl_3 =
    499  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl_3;
    500  1.1  riastrad 	u32 cg_spll_spread_spectrum =
    501  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_spread_spectrum;
    502  1.1  riastrad 	u32 cg_spll_spread_spectrum_2 =
    503  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_spread_spectrum_2;
    504  1.1  riastrad 	u64 tmp;
    505  1.1  riastrad 	u32 reference_clock = rdev->clock.spll.reference_freq;
    506  1.1  riastrad 	u32 reference_divider, post_divider;
    507  1.1  riastrad 	u32 fbdiv;
    508  1.1  riastrad 	int ret;
    509  1.1  riastrad 
    510  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
    511  1.1  riastrad 					     engine_clock, false, &dividers);
    512  1.1  riastrad 	if (ret)
    513  1.1  riastrad 		return ret;
    514  1.1  riastrad 
    515  1.1  riastrad 	reference_divider = 1 + dividers.ref_div;
    516  1.1  riastrad 
    517  1.1  riastrad 	if (dividers.enable_post_div)
    518  1.1  riastrad 		post_divider = (0x0f & (dividers.post_div >> 4)) + (0x0f & dividers.post_div) + 2;
    519  1.1  riastrad 	else
    520  1.1  riastrad 		post_divider = 1;
    521  1.1  riastrad 
    522  1.1  riastrad 	tmp = (u64) engine_clock * reference_divider * post_divider * 16384;
    523  1.1  riastrad 	do_div(tmp, reference_clock);
    524  1.1  riastrad 	fbdiv = (u32) tmp;
    525  1.1  riastrad 
    526  1.1  riastrad 	if (dividers.enable_post_div)
    527  1.1  riastrad 		spll_func_cntl |= SPLL_DIVEN;
    528  1.1  riastrad 	else
    529  1.1  riastrad 		spll_func_cntl &= ~SPLL_DIVEN;
    530  1.1  riastrad 	spll_func_cntl &= ~(SPLL_HILEN_MASK | SPLL_LOLEN_MASK | SPLL_REF_DIV_MASK);
    531  1.1  riastrad 	spll_func_cntl |= SPLL_REF_DIV(dividers.ref_div);
    532  1.1  riastrad 	spll_func_cntl |= SPLL_HILEN((dividers.post_div >> 4) & 0xf);
    533  1.1  riastrad 	spll_func_cntl |= SPLL_LOLEN(dividers.post_div & 0xf);
    534  1.1  riastrad 
    535  1.1  riastrad 	spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
    536  1.1  riastrad 	spll_func_cntl_2 |= SCLK_MUX_SEL(2);
    537  1.1  riastrad 
    538  1.1  riastrad 	spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
    539  1.1  riastrad 	spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
    540  1.1  riastrad 	spll_func_cntl_3 |= SPLL_DITHEN;
    541  1.1  riastrad 
    542  1.1  riastrad 	if (pi->sclk_ss) {
    543  1.1  riastrad 		struct radeon_atom_ss ss;
    544  1.1  riastrad 		u32 vco_freq = engine_clock * post_divider;
    545  1.1  riastrad 
    546  1.1  riastrad 		if (radeon_atombios_get_asic_ss_info(rdev, &ss,
    547  1.1  riastrad 						     ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
    548  1.1  riastrad 			u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
    549  1.1  riastrad 			u32 clk_v = ss.percentage * fbdiv / (clk_s * 10000);
    550  1.1  riastrad 
    551  1.1  riastrad 			cg_spll_spread_spectrum &= ~CLKS_MASK;
    552  1.1  riastrad 			cg_spll_spread_spectrum |= CLKS(clk_s);
    553  1.1  riastrad 			cg_spll_spread_spectrum |= SSEN;
    554  1.1  riastrad 
    555  1.1  riastrad 			cg_spll_spread_spectrum_2 &= ~CLKV_MASK;
    556  1.1  riastrad 			cg_spll_spread_spectrum_2 |= CLKV(clk_v);
    557  1.1  riastrad 		}
    558  1.1  riastrad 	}
    559  1.1  riastrad 
    560  1.1  riastrad 	sclk->sclk_value = cpu_to_be32(engine_clock);
    561  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL = cpu_to_be32(spll_func_cntl);
    562  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL_2 = cpu_to_be32(spll_func_cntl_2);
    563  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL_3 = cpu_to_be32(spll_func_cntl_3);
    564  1.1  riastrad 	sclk->vCG_SPLL_SPREAD_SPECTRUM = cpu_to_be32(cg_spll_spread_spectrum);
    565  1.1  riastrad 	sclk->vCG_SPLL_SPREAD_SPECTRUM_2 = cpu_to_be32(cg_spll_spread_spectrum_2);
    566  1.1  riastrad 
    567  1.1  riastrad 	return 0;
    568  1.1  riastrad }
    569  1.1  riastrad 
    570  1.1  riastrad int rv770_populate_vddc_value(struct radeon_device *rdev, u16 vddc,
    571  1.1  riastrad 			      RV770_SMC_VOLTAGE_VALUE *voltage)
    572  1.1  riastrad {
    573  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    574  1.1  riastrad 	int i;
    575  1.1  riastrad 
    576  1.1  riastrad 	if (!pi->voltage_control) {
    577  1.1  riastrad 		voltage->index = 0;
    578  1.1  riastrad 		voltage->value = 0;
    579  1.1  riastrad 		return 0;
    580  1.1  riastrad 	}
    581  1.1  riastrad 
    582  1.1  riastrad 	for (i = 0; i < pi->valid_vddc_entries; i++) {
    583  1.1  riastrad 		if (vddc <= pi->vddc_table[i].vddc) {
    584  1.1  riastrad 			voltage->index = pi->vddc_table[i].vddc_index;
    585  1.1  riastrad 			voltage->value = cpu_to_be16(vddc);
    586  1.1  riastrad 			break;
    587  1.1  riastrad 		}
    588  1.1  riastrad 	}
    589  1.1  riastrad 
    590  1.1  riastrad 	if (i == pi->valid_vddc_entries)
    591  1.1  riastrad 		return -EINVAL;
    592  1.1  riastrad 
    593  1.1  riastrad 	return 0;
    594  1.1  riastrad }
    595  1.1  riastrad 
    596  1.1  riastrad int rv770_populate_mvdd_value(struct radeon_device *rdev, u32 mclk,
    597  1.1  riastrad 			      RV770_SMC_VOLTAGE_VALUE *voltage)
    598  1.1  riastrad {
    599  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    600  1.1  riastrad 
    601  1.1  riastrad 	if (!pi->mvdd_control) {
    602  1.1  riastrad 		voltage->index = MVDD_HIGH_INDEX;
    603  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
    604  1.1  riastrad 		return 0;
    605  1.1  riastrad 	}
    606  1.1  riastrad 
    607  1.1  riastrad 	if (mclk <= pi->mvdd_split_frequency) {
    608  1.1  riastrad 		voltage->index = MVDD_LOW_INDEX;
    609  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_LOW_VALUE);
    610  1.1  riastrad 	} else {
    611  1.1  riastrad 		voltage->index = MVDD_HIGH_INDEX;
    612  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
    613  1.1  riastrad 	}
    614  1.1  riastrad 
    615  1.1  riastrad 	return 0;
    616  1.1  riastrad }
    617  1.1  riastrad 
    618  1.1  riastrad static int rv770_convert_power_level_to_smc(struct radeon_device *rdev,
    619  1.1  riastrad 					    struct rv7xx_pl *pl,
    620  1.1  riastrad 					    RV770_SMC_HW_PERFORMANCE_LEVEL *level,
    621  1.1  riastrad 					    u8 watermark_level)
    622  1.1  riastrad {
    623  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    624  1.1  riastrad 	int ret;
    625  1.1  riastrad 
    626  1.1  riastrad 	level->gen2PCIE = pi->pcie_gen2 ?
    627  1.1  riastrad 		((pl->flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? 1 : 0) : 0;
    628  1.1  riastrad 	level->gen2XSP  = (pl->flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? 1 : 0;
    629  1.1  riastrad 	level->backbias = (pl->flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ? 1 : 0;
    630  1.1  riastrad 	level->displayWatermark = watermark_level;
    631  1.1  riastrad 
    632  1.1  riastrad 	if (rdev->family == CHIP_RV740)
    633  1.1  riastrad 		ret = rv740_populate_sclk_value(rdev, pl->sclk,
    634  1.1  riastrad 						&level->sclk);
    635  1.1  riastrad 	else if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
    636  1.1  riastrad 		ret = rv730_populate_sclk_value(rdev, pl->sclk,
    637  1.1  riastrad 						&level->sclk);
    638  1.1  riastrad 	else
    639  1.1  riastrad 		ret = rv770_populate_sclk_value(rdev, pl->sclk,
    640  1.1  riastrad 						&level->sclk);
    641  1.1  riastrad 	if (ret)
    642  1.1  riastrad 		return ret;
    643  1.1  riastrad 
    644  1.1  riastrad 	if (rdev->family == CHIP_RV740) {
    645  1.1  riastrad 		if (pi->mem_gddr5) {
    646  1.1  riastrad 			if (pl->mclk <= pi->mclk_strobe_mode_threshold)
    647  1.1  riastrad 				level->strobeMode =
    648  1.1  riastrad 					rv740_get_mclk_frequency_ratio(pl->mclk) | 0x10;
    649  1.1  riastrad 			else
    650  1.1  riastrad 				level->strobeMode = 0;
    651  1.1  riastrad 
    652  1.1  riastrad 			if (pl->mclk > pi->mclk_edc_enable_threshold)
    653  1.1  riastrad 				level->mcFlags = SMC_MC_EDC_RD_FLAG | SMC_MC_EDC_WR_FLAG;
    654  1.1  riastrad 			else
    655  1.1  riastrad 				level->mcFlags =  0;
    656  1.1  riastrad 		}
    657  1.1  riastrad 		ret = rv740_populate_mclk_value(rdev, pl->sclk,
    658  1.1  riastrad 						pl->mclk, &level->mclk);
    659  1.1  riastrad 	} else if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
    660  1.1  riastrad 		ret = rv730_populate_mclk_value(rdev, pl->sclk,
    661  1.1  riastrad 						pl->mclk, &level->mclk);
    662  1.1  riastrad 	else
    663  1.1  riastrad 		ret = rv770_populate_mclk_value(rdev, pl->sclk,
    664  1.1  riastrad 						pl->mclk, &level->mclk);
    665  1.1  riastrad 	if (ret)
    666  1.1  riastrad 		return ret;
    667  1.1  riastrad 
    668  1.1  riastrad 	ret = rv770_populate_vddc_value(rdev, pl->vddc,
    669  1.1  riastrad 					&level->vddc);
    670  1.1  riastrad 	if (ret)
    671  1.1  riastrad 		return ret;
    672  1.1  riastrad 
    673  1.1  riastrad 	ret = rv770_populate_mvdd_value(rdev, pl->mclk, &level->mvdd);
    674  1.1  riastrad 
    675  1.1  riastrad 	return ret;
    676  1.1  riastrad }
    677  1.1  riastrad 
    678  1.1  riastrad static int rv770_convert_power_state_to_smc(struct radeon_device *rdev,
    679  1.1  riastrad 					    struct radeon_ps *radeon_state,
    680  1.1  riastrad 					    RV770_SMC_SWSTATE *smc_state)
    681  1.1  riastrad {
    682  1.1  riastrad 	struct rv7xx_ps *state = rv770_get_ps(radeon_state);
    683  1.1  riastrad 	int ret;
    684  1.1  riastrad 
    685  1.1  riastrad 	if (!(radeon_state->caps & ATOM_PPLIB_DISALLOW_ON_DC))
    686  1.1  riastrad 		smc_state->flags |= PPSMC_SWSTATE_FLAG_DC;
    687  1.1  riastrad 
    688  1.1  riastrad 	ret = rv770_convert_power_level_to_smc(rdev,
    689  1.1  riastrad 					       &state->low,
    690  1.1  riastrad 					       &smc_state->levels[0],
    691  1.1  riastrad 					       PPSMC_DISPLAY_WATERMARK_LOW);
    692  1.1  riastrad 	if (ret)
    693  1.1  riastrad 		return ret;
    694  1.1  riastrad 
    695  1.1  riastrad 	ret = rv770_convert_power_level_to_smc(rdev,
    696  1.1  riastrad 					       &state->medium,
    697  1.1  riastrad 					       &smc_state->levels[1],
    698  1.1  riastrad 					       PPSMC_DISPLAY_WATERMARK_LOW);
    699  1.1  riastrad 	if (ret)
    700  1.1  riastrad 		return ret;
    701  1.1  riastrad 
    702  1.1  riastrad 	ret = rv770_convert_power_level_to_smc(rdev,
    703  1.1  riastrad 					       &state->high,
    704  1.1  riastrad 					       &smc_state->levels[2],
    705  1.1  riastrad 					       PPSMC_DISPLAY_WATERMARK_HIGH);
    706  1.1  riastrad 	if (ret)
    707  1.1  riastrad 		return ret;
    708  1.1  riastrad 
    709  1.1  riastrad 	smc_state->levels[0].arbValue = MC_CG_ARB_FREQ_F1;
    710  1.1  riastrad 	smc_state->levels[1].arbValue = MC_CG_ARB_FREQ_F2;
    711  1.1  riastrad 	smc_state->levels[2].arbValue = MC_CG_ARB_FREQ_F3;
    712  1.1  riastrad 
    713  1.1  riastrad 	smc_state->levels[0].seqValue = rv770_get_seq_value(rdev,
    714  1.1  riastrad 							    &state->low);
    715  1.1  riastrad 	smc_state->levels[1].seqValue = rv770_get_seq_value(rdev,
    716  1.1  riastrad 							    &state->medium);
    717  1.1  riastrad 	smc_state->levels[2].seqValue = rv770_get_seq_value(rdev,
    718  1.1  riastrad 							    &state->high);
    719  1.1  riastrad 
    720  1.1  riastrad 	rv770_populate_smc_sp(rdev, radeon_state, smc_state);
    721  1.1  riastrad 
    722  1.1  riastrad 	return rv770_populate_smc_t(rdev, radeon_state, smc_state);
    723  1.1  riastrad 
    724  1.1  riastrad }
    725  1.1  riastrad 
    726  1.1  riastrad u32 rv770_calculate_memory_refresh_rate(struct radeon_device *rdev,
    727  1.1  riastrad 					u32 engine_clock)
    728  1.1  riastrad {
    729  1.1  riastrad 	u32 dram_rows;
    730  1.1  riastrad 	u32 dram_refresh_rate;
    731  1.1  riastrad 	u32 mc_arb_rfsh_rate;
    732  1.1  riastrad 	u32 tmp;
    733  1.1  riastrad 
    734  1.1  riastrad 	tmp = (RREG32(MC_ARB_RAMCFG) & NOOFROWS_MASK) >> NOOFROWS_SHIFT;
    735  1.1  riastrad 	dram_rows = 1 << (tmp + 10);
    736  1.1  riastrad 	tmp = RREG32(MC_SEQ_MISC0) & 3;
    737  1.1  riastrad 	dram_refresh_rate = 1 << (tmp + 3);
    738  1.1  riastrad 	mc_arb_rfsh_rate = ((engine_clock * 10) * dram_refresh_rate / dram_rows - 32) / 64;
    739  1.1  riastrad 
    740  1.1  riastrad 	return mc_arb_rfsh_rate;
    741  1.1  riastrad }
    742  1.1  riastrad 
    743  1.1  riastrad static void rv770_program_memory_timing_parameters(struct radeon_device *rdev,
    744  1.1  riastrad 						   struct radeon_ps *radeon_state)
    745  1.1  riastrad {
    746  1.1  riastrad 	struct rv7xx_ps *state = rv770_get_ps(radeon_state);
    747  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    748  1.1  riastrad 	u32 sqm_ratio;
    749  1.1  riastrad 	u32 arb_refresh_rate;
    750  1.1  riastrad 	u32 high_clock;
    751  1.1  riastrad 
    752  1.1  riastrad 	if (state->high.sclk < (state->low.sclk * 0xFF / 0x40))
    753  1.1  riastrad 		high_clock = state->high.sclk;
    754  1.1  riastrad 	else
    755  1.1  riastrad 		high_clock = (state->low.sclk * 0xFF / 0x40);
    756  1.1  riastrad 
    757  1.1  riastrad 	radeon_atom_set_engine_dram_timings(rdev, high_clock,
    758  1.1  riastrad 					    state->high.mclk);
    759  1.1  riastrad 
    760  1.1  riastrad 	sqm_ratio =
    761  1.1  riastrad 		STATE0(64 * high_clock / pi->boot_sclk) |
    762  1.1  riastrad 		STATE1(64 * high_clock / state->low.sclk) |
    763  1.1  riastrad 		STATE2(64 * high_clock / state->medium.sclk) |
    764  1.1  riastrad 		STATE3(64 * high_clock / state->high.sclk);
    765  1.1  riastrad 	WREG32(MC_ARB_SQM_RATIO, sqm_ratio);
    766  1.1  riastrad 
    767  1.1  riastrad 	arb_refresh_rate =
    768  1.1  riastrad 		POWERMODE0(rv770_calculate_memory_refresh_rate(rdev, pi->boot_sclk)) |
    769  1.1  riastrad 		POWERMODE1(rv770_calculate_memory_refresh_rate(rdev, state->low.sclk)) |
    770  1.1  riastrad 		POWERMODE2(rv770_calculate_memory_refresh_rate(rdev, state->medium.sclk)) |
    771  1.1  riastrad 		POWERMODE3(rv770_calculate_memory_refresh_rate(rdev, state->high.sclk));
    772  1.1  riastrad 	WREG32(MC_ARB_RFSH_RATE, arb_refresh_rate);
    773  1.1  riastrad }
    774  1.1  riastrad 
    775  1.1  riastrad void rv770_enable_backbias(struct radeon_device *rdev,
    776  1.1  riastrad 			   bool enable)
    777  1.1  riastrad {
    778  1.1  riastrad 	if (enable)
    779  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, BACKBIAS_PAD_EN, ~BACKBIAS_PAD_EN);
    780  1.1  riastrad 	else
    781  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~(BACKBIAS_VALUE | BACKBIAS_PAD_EN));
    782  1.1  riastrad }
    783  1.1  riastrad 
    784  1.1  riastrad static void rv770_enable_spread_spectrum(struct radeon_device *rdev,
    785  1.1  riastrad 					 bool enable)
    786  1.1  riastrad {
    787  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    788  1.1  riastrad 
    789  1.1  riastrad 	if (enable) {
    790  1.1  riastrad 		if (pi->sclk_ss)
    791  1.1  riastrad 			WREG32_P(GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, ~DYN_SPREAD_SPECTRUM_EN);
    792  1.1  riastrad 
    793  1.1  riastrad 		if (pi->mclk_ss) {
    794  1.1  riastrad 			if (rdev->family == CHIP_RV740)
    795  1.1  riastrad 				rv740_enable_mclk_spread_spectrum(rdev, true);
    796  1.1  riastrad 		}
    797  1.1  riastrad 	} else {
    798  1.1  riastrad 		WREG32_P(CG_SPLL_SPREAD_SPECTRUM, 0, ~SSEN);
    799  1.1  riastrad 
    800  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~DYN_SPREAD_SPECTRUM_EN);
    801  1.1  riastrad 
    802  1.1  riastrad 		WREG32_P(CG_MPLL_SPREAD_SPECTRUM, 0, ~SSEN);
    803  1.1  riastrad 
    804  1.1  riastrad 		if (rdev->family == CHIP_RV740)
    805  1.1  riastrad 			rv740_enable_mclk_spread_spectrum(rdev, false);
    806  1.1  riastrad 	}
    807  1.1  riastrad }
    808  1.1  riastrad 
    809  1.1  riastrad static void rv770_program_mpll_timing_parameters(struct radeon_device *rdev)
    810  1.1  riastrad {
    811  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    812  1.1  riastrad 
    813  1.1  riastrad 	if ((rdev->family == CHIP_RV770) && !pi->mem_gddr5) {
    814  1.1  riastrad 		WREG32(MPLL_TIME,
    815  1.1  riastrad 		       (MPLL_LOCK_TIME(R600_MPLLLOCKTIME_DFLT * pi->ref_div) |
    816  1.1  riastrad 			MPLL_RESET_TIME(R600_MPLLRESETTIME_DFLT)));
    817  1.1  riastrad 	}
    818  1.1  riastrad }
    819  1.1  riastrad 
    820  1.1  riastrad void rv770_setup_bsp(struct radeon_device *rdev)
    821  1.1  riastrad {
    822  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    823  1.1  riastrad 	u32 xclk = radeon_get_xclk(rdev);
    824  1.1  riastrad 
    825  1.1  riastrad 	r600_calculate_u_and_p(pi->asi,
    826  1.1  riastrad 			       xclk,
    827  1.1  riastrad 			       16,
    828  1.1  riastrad 			       &pi->bsp,
    829  1.1  riastrad 			       &pi->bsu);
    830  1.1  riastrad 
    831  1.1  riastrad 	r600_calculate_u_and_p(pi->pasi,
    832  1.1  riastrad 			       xclk,
    833  1.1  riastrad 			       16,
    834  1.1  riastrad 			       &pi->pbsp,
    835  1.1  riastrad 			       &pi->pbsu);
    836  1.1  riastrad 
    837  1.1  riastrad 	pi->dsp = BSP(pi->bsp) | BSU(pi->bsu);
    838  1.1  riastrad 	pi->psp = BSP(pi->pbsp) | BSU(pi->pbsu);
    839  1.1  riastrad 
    840  1.1  riastrad 	WREG32(CG_BSP, pi->dsp);
    841  1.1  riastrad 
    842  1.1  riastrad }
    843  1.1  riastrad 
    844  1.1  riastrad void rv770_program_git(struct radeon_device *rdev)
    845  1.1  riastrad {
    846  1.1  riastrad 	WREG32_P(CG_GIT, CG_GICST(R600_GICST_DFLT), ~CG_GICST_MASK);
    847  1.1  riastrad }
    848  1.1  riastrad 
    849  1.1  riastrad void rv770_program_tp(struct radeon_device *rdev)
    850  1.1  riastrad {
    851  1.1  riastrad 	int i;
    852  1.1  riastrad 	enum r600_td td = R600_TD_DFLT;
    853  1.1  riastrad 
    854  1.1  riastrad 	for (i = 0; i < R600_PM_NUMBER_OF_TC; i++)
    855  1.1  riastrad 		WREG32(CG_FFCT_0 + (i * 4), (UTC_0(r600_utc[i]) | DTC_0(r600_dtc[i])));
    856  1.1  riastrad 
    857  1.1  riastrad 	if (td == R600_TD_AUTO)
    858  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~FIR_FORCE_TREND_SEL);
    859  1.1  riastrad 	else
    860  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, FIR_FORCE_TREND_SEL, ~FIR_FORCE_TREND_SEL);
    861  1.1  riastrad 	if (td == R600_TD_UP)
    862  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~FIR_TREND_MODE);
    863  1.1  riastrad 	if (td == R600_TD_DOWN)
    864  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, FIR_TREND_MODE, ~FIR_TREND_MODE);
    865  1.1  riastrad }
    866  1.1  riastrad 
    867  1.1  riastrad void rv770_program_tpp(struct radeon_device *rdev)
    868  1.1  riastrad {
    869  1.1  riastrad 	WREG32(CG_TPC, R600_TPC_DFLT);
    870  1.1  riastrad }
    871  1.1  riastrad 
    872  1.1  riastrad void rv770_program_sstp(struct radeon_device *rdev)
    873  1.1  riastrad {
    874  1.1  riastrad 	WREG32(CG_SSP, (SSTU(R600_SSTU_DFLT) | SST(R600_SST_DFLT)));
    875  1.1  riastrad }
    876  1.1  riastrad 
    877  1.1  riastrad void rv770_program_engine_speed_parameters(struct radeon_device *rdev)
    878  1.1  riastrad {
    879  1.1  riastrad 	WREG32_P(SPLL_CNTL_MODE, SPLL_DIV_SYNC, ~SPLL_DIV_SYNC);
    880  1.1  riastrad }
    881  1.1  riastrad 
    882  1.1  riastrad static void rv770_enable_display_gap(struct radeon_device *rdev)
    883  1.1  riastrad {
    884  1.1  riastrad 	u32 tmp = RREG32(CG_DISPLAY_GAP_CNTL);
    885  1.1  riastrad 
    886  1.1  riastrad 	tmp &= ~(DISP1_GAP_MCHG_MASK | DISP2_GAP_MCHG_MASK);
    887  1.1  riastrad 	tmp |= (DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE) |
    888  1.1  riastrad 		DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE));
    889  1.1  riastrad 	WREG32(CG_DISPLAY_GAP_CNTL, tmp);
    890  1.1  riastrad }
    891  1.1  riastrad 
    892  1.1  riastrad void rv770_program_vc(struct radeon_device *rdev)
    893  1.1  riastrad {
    894  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    895  1.1  riastrad 
    896  1.1  riastrad 	WREG32(CG_FTV, pi->vrc);
    897  1.1  riastrad }
    898  1.1  riastrad 
    899  1.1  riastrad void rv770_clear_vc(struct radeon_device *rdev)
    900  1.1  riastrad {
    901  1.1  riastrad 	WREG32(CG_FTV, 0);
    902  1.1  riastrad }
    903  1.1  riastrad 
    904  1.1  riastrad int rv770_upload_firmware(struct radeon_device *rdev)
    905  1.1  riastrad {
    906  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    907  1.1  riastrad 	int ret;
    908  1.1  riastrad 
    909  1.1  riastrad 	rv770_reset_smc(rdev);
    910  1.1  riastrad 	rv770_stop_smc_clock(rdev);
    911  1.1  riastrad 
    912  1.1  riastrad 	ret = rv770_load_smc_ucode(rdev, pi->sram_end);
    913  1.1  riastrad 	if (ret)
    914  1.1  riastrad 		return ret;
    915  1.1  riastrad 
    916  1.1  riastrad 	return 0;
    917  1.1  riastrad }
    918  1.1  riastrad 
    919  1.1  riastrad static int rv770_populate_smc_acpi_state(struct radeon_device *rdev,
    920  1.1  riastrad 					 RV770_SMC_STATETABLE *table)
    921  1.1  riastrad {
    922  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    923  1.1  riastrad 
    924  1.1  riastrad 	u32 mpll_ad_func_cntl =
    925  1.1  riastrad 		pi->clk_regs.rv770.mpll_ad_func_cntl;
    926  1.1  riastrad 	u32 mpll_ad_func_cntl_2 =
    927  1.1  riastrad 		pi->clk_regs.rv770.mpll_ad_func_cntl_2;
    928  1.1  riastrad 	u32 mpll_dq_func_cntl =
    929  1.1  riastrad 		pi->clk_regs.rv770.mpll_dq_func_cntl;
    930  1.1  riastrad 	u32 mpll_dq_func_cntl_2 =
    931  1.1  riastrad 		pi->clk_regs.rv770.mpll_dq_func_cntl_2;
    932  1.1  riastrad 	u32 spll_func_cntl =
    933  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl;
    934  1.1  riastrad 	u32 spll_func_cntl_2 =
    935  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl_2;
    936  1.1  riastrad 	u32 spll_func_cntl_3 =
    937  1.1  riastrad 		pi->clk_regs.rv770.cg_spll_func_cntl_3;
    938  1.1  riastrad 	u32 mclk_pwrmgt_cntl;
    939  1.1  riastrad 	u32 dll_cntl;
    940  1.1  riastrad 
    941  1.1  riastrad 	table->ACPIState = table->initialState;
    942  1.1  riastrad 
    943  1.1  riastrad 	table->ACPIState.flags &= ~PPSMC_SWSTATE_FLAG_DC;
    944  1.1  riastrad 
    945  1.1  riastrad 	if (pi->acpi_vddc) {
    946  1.1  riastrad 		rv770_populate_vddc_value(rdev, pi->acpi_vddc,
    947  1.1  riastrad 					  &table->ACPIState.levels[0].vddc);
    948  1.1  riastrad 		if (pi->pcie_gen2) {
    949  1.1  riastrad 			if (pi->acpi_pcie_gen2)
    950  1.1  riastrad 				table->ACPIState.levels[0].gen2PCIE = 1;
    951  1.1  riastrad 			else
    952  1.1  riastrad 				table->ACPIState.levels[0].gen2PCIE = 0;
    953  1.1  riastrad 		} else
    954  1.1  riastrad 			table->ACPIState.levels[0].gen2PCIE = 0;
    955  1.1  riastrad 		if (pi->acpi_pcie_gen2)
    956  1.1  riastrad 			table->ACPIState.levels[0].gen2XSP = 1;
    957  1.1  riastrad 		else
    958  1.1  riastrad 			table->ACPIState.levels[0].gen2XSP = 0;
    959  1.1  riastrad 	} else {
    960  1.1  riastrad 		rv770_populate_vddc_value(rdev, pi->min_vddc_in_table,
    961  1.1  riastrad 					  &table->ACPIState.levels[0].vddc);
    962  1.1  riastrad 		table->ACPIState.levels[0].gen2PCIE = 0;
    963  1.1  riastrad 	}
    964  1.1  riastrad 
    965  1.1  riastrad 
    966  1.1  riastrad 	mpll_ad_func_cntl_2 |= BIAS_GEN_PDNB | RESET_EN;
    967  1.1  riastrad 
    968  1.1  riastrad 	mpll_dq_func_cntl_2 |= BIAS_GEN_PDNB | RESET_EN;
    969  1.1  riastrad 
    970  1.1  riastrad 	mclk_pwrmgt_cntl = (MRDCKA0_RESET |
    971  1.1  riastrad 			    MRDCKA1_RESET |
    972  1.1  riastrad 			    MRDCKB0_RESET |
    973  1.1  riastrad 			    MRDCKB1_RESET |
    974  1.1  riastrad 			    MRDCKC0_RESET |
    975  1.1  riastrad 			    MRDCKC1_RESET |
    976  1.1  riastrad 			    MRDCKD0_RESET |
    977  1.1  riastrad 			    MRDCKD1_RESET);
    978  1.1  riastrad 
    979  1.1  riastrad 	dll_cntl = 0xff000000;
    980  1.1  riastrad 
    981  1.1  riastrad 	spll_func_cntl |= SPLL_RESET | SPLL_SLEEP | SPLL_BYPASS_EN;
    982  1.1  riastrad 
    983  1.1  riastrad 	spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
    984  1.1  riastrad 	spll_func_cntl_2 |= SCLK_MUX_SEL(4);
    985  1.1  riastrad 
    986  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vMPLL_AD_FUNC_CNTL = cpu_to_be32(mpll_ad_func_cntl);
    987  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vMPLL_AD_FUNC_CNTL_2 = cpu_to_be32(mpll_ad_func_cntl_2);
    988  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vMPLL_DQ_FUNC_CNTL = cpu_to_be32(mpll_dq_func_cntl);
    989  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vMPLL_DQ_FUNC_CNTL_2 = cpu_to_be32(mpll_dq_func_cntl_2);
    990  1.1  riastrad 
    991  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vMCLK_PWRMGT_CNTL = cpu_to_be32(mclk_pwrmgt_cntl);
    992  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.vDLL_CNTL = cpu_to_be32(dll_cntl);
    993  1.1  riastrad 
    994  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk770.mclk_value = 0;
    995  1.1  riastrad 
    996  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL = cpu_to_be32(spll_func_cntl);
    997  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_2 = cpu_to_be32(spll_func_cntl_2);
    998  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_3 = cpu_to_be32(spll_func_cntl_3);
    999  1.1  riastrad 
   1000  1.1  riastrad 	table->ACPIState.levels[0].sclk.sclk_value = 0;
   1001  1.1  riastrad 
   1002  1.1  riastrad 	rv770_populate_mvdd_value(rdev, 0, &table->ACPIState.levels[0].mvdd);
   1003  1.1  riastrad 
   1004  1.1  riastrad 	table->ACPIState.levels[1] = table->ACPIState.levels[0];
   1005  1.1  riastrad 	table->ACPIState.levels[2] = table->ACPIState.levels[0];
   1006  1.1  riastrad 
   1007  1.1  riastrad 	return 0;
   1008  1.1  riastrad }
   1009  1.1  riastrad 
   1010  1.1  riastrad int rv770_populate_initial_mvdd_value(struct radeon_device *rdev,
   1011  1.1  riastrad 				      RV770_SMC_VOLTAGE_VALUE *voltage)
   1012  1.1  riastrad {
   1013  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1014  1.1  riastrad 
   1015  1.1  riastrad 	if ((pi->s0_vid_lower_smio_cntl & pi->mvdd_mask_low) ==
   1016  1.1  riastrad 	     (pi->mvdd_low_smio[MVDD_LOW_INDEX] & pi->mvdd_mask_low) ) {
   1017  1.1  riastrad 		voltage->index = MVDD_LOW_INDEX;
   1018  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_LOW_VALUE);
   1019  1.1  riastrad 	} else {
   1020  1.1  riastrad 		voltage->index = MVDD_HIGH_INDEX;
   1021  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
   1022  1.1  riastrad 	}
   1023  1.1  riastrad 
   1024  1.1  riastrad 	return 0;
   1025  1.1  riastrad }
   1026  1.1  riastrad 
   1027  1.1  riastrad static int rv770_populate_smc_initial_state(struct radeon_device *rdev,
   1028  1.1  riastrad 					    struct radeon_ps *radeon_state,
   1029  1.1  riastrad 					    RV770_SMC_STATETABLE *table)
   1030  1.1  riastrad {
   1031  1.1  riastrad 	struct rv7xx_ps *initial_state = rv770_get_ps(radeon_state);
   1032  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1033  1.1  riastrad 	u32 a_t;
   1034  1.1  riastrad 
   1035  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_AD_FUNC_CNTL =
   1036  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_ad_func_cntl);
   1037  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_AD_FUNC_CNTL_2 =
   1038  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_ad_func_cntl_2);
   1039  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_DQ_FUNC_CNTL =
   1040  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_dq_func_cntl);
   1041  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_DQ_FUNC_CNTL_2 =
   1042  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_dq_func_cntl_2);
   1043  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMCLK_PWRMGT_CNTL =
   1044  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mclk_pwrmgt_cntl);
   1045  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vDLL_CNTL =
   1046  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.dll_cntl);
   1047  1.1  riastrad 
   1048  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_SS =
   1049  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_ss1);
   1050  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.vMPLL_SS2 =
   1051  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.mpll_ss2);
   1052  1.1  riastrad 
   1053  1.1  riastrad 	table->initialState.levels[0].mclk.mclk770.mclk_value =
   1054  1.1  riastrad 		cpu_to_be32(initial_state->low.mclk);
   1055  1.1  riastrad 
   1056  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL =
   1057  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.cg_spll_func_cntl);
   1058  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_2 =
   1059  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.cg_spll_func_cntl_2);
   1060  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_3 =
   1061  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.cg_spll_func_cntl_3);
   1062  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_SPREAD_SPECTRUM =
   1063  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.cg_spll_spread_spectrum);
   1064  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_SPREAD_SPECTRUM_2 =
   1065  1.1  riastrad 		cpu_to_be32(pi->clk_regs.rv770.cg_spll_spread_spectrum_2);
   1066  1.1  riastrad 
   1067  1.1  riastrad 	table->initialState.levels[0].sclk.sclk_value =
   1068  1.1  riastrad 		cpu_to_be32(initial_state->low.sclk);
   1069  1.1  riastrad 
   1070  1.1  riastrad 	table->initialState.levels[0].arbValue = MC_CG_ARB_FREQ_F0;
   1071  1.1  riastrad 
   1072  1.1  riastrad 	table->initialState.levels[0].seqValue =
   1073  1.1  riastrad 		rv770_get_seq_value(rdev, &initial_state->low);
   1074  1.1  riastrad 
   1075  1.1  riastrad 	rv770_populate_vddc_value(rdev,
   1076  1.1  riastrad 				  initial_state->low.vddc,
   1077  1.1  riastrad 				  &table->initialState.levels[0].vddc);
   1078  1.1  riastrad 	rv770_populate_initial_mvdd_value(rdev,
   1079  1.1  riastrad 					  &table->initialState.levels[0].mvdd);
   1080  1.1  riastrad 
   1081  1.1  riastrad 	a_t = CG_R(0xffff) | CG_L(0);
   1082  1.1  riastrad 	table->initialState.levels[0].aT = cpu_to_be32(a_t);
   1083  1.1  riastrad 
   1084  1.1  riastrad 	table->initialState.levels[0].bSP = cpu_to_be32(pi->dsp);
   1085  1.1  riastrad 
   1086  1.1  riastrad 	if (pi->boot_in_gen2)
   1087  1.1  riastrad 		table->initialState.levels[0].gen2PCIE = 1;
   1088  1.1  riastrad 	else
   1089  1.1  riastrad 		table->initialState.levels[0].gen2PCIE = 0;
   1090  1.1  riastrad 	if (initial_state->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2)
   1091  1.1  riastrad 		table->initialState.levels[0].gen2XSP = 1;
   1092  1.1  riastrad 	else
   1093  1.1  riastrad 		table->initialState.levels[0].gen2XSP = 0;
   1094  1.1  riastrad 
   1095  1.1  riastrad 	if (rdev->family == CHIP_RV740) {
   1096  1.1  riastrad 		if (pi->mem_gddr5) {
   1097  1.1  riastrad 			if (initial_state->low.mclk <= pi->mclk_strobe_mode_threshold)
   1098  1.1  riastrad 				table->initialState.levels[0].strobeMode =
   1099  1.1  riastrad 					rv740_get_mclk_frequency_ratio(initial_state->low.mclk) | 0x10;
   1100  1.1  riastrad 			else
   1101  1.1  riastrad 				table->initialState.levels[0].strobeMode = 0;
   1102  1.1  riastrad 
   1103  1.1  riastrad 			if (initial_state->low.mclk >= pi->mclk_edc_enable_threshold)
   1104  1.1  riastrad 				table->initialState.levels[0].mcFlags = SMC_MC_EDC_RD_FLAG | SMC_MC_EDC_WR_FLAG;
   1105  1.1  riastrad 			else
   1106  1.1  riastrad 				table->initialState.levels[0].mcFlags =  0;
   1107  1.1  riastrad 		}
   1108  1.1  riastrad 	}
   1109  1.1  riastrad 
   1110  1.1  riastrad 	table->initialState.levels[1] = table->initialState.levels[0];
   1111  1.1  riastrad 	table->initialState.levels[2] = table->initialState.levels[0];
   1112  1.1  riastrad 
   1113  1.1  riastrad 	table->initialState.flags |= PPSMC_SWSTATE_FLAG_DC;
   1114  1.1  riastrad 
   1115  1.1  riastrad 	return 0;
   1116  1.1  riastrad }
   1117  1.1  riastrad 
   1118  1.1  riastrad static int rv770_populate_smc_vddc_table(struct radeon_device *rdev,
   1119  1.1  riastrad 					 RV770_SMC_STATETABLE *table)
   1120  1.1  riastrad {
   1121  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1122  1.1  riastrad 	int i;
   1123  1.1  riastrad 
   1124  1.1  riastrad 	for (i = 0; i < pi->valid_vddc_entries; i++) {
   1125  1.1  riastrad 		table->highSMIO[pi->vddc_table[i].vddc_index] =
   1126  1.1  riastrad 			pi->vddc_table[i].high_smio;
   1127  1.1  riastrad 		table->lowSMIO[pi->vddc_table[i].vddc_index] =
   1128  1.1  riastrad 			cpu_to_be32(pi->vddc_table[i].low_smio);
   1129  1.1  riastrad 	}
   1130  1.1  riastrad 
   1131  1.1  riastrad 	table->voltageMaskTable.highMask[RV770_SMC_VOLTAGEMASK_VDDC] = 0;
   1132  1.1  riastrad 	table->voltageMaskTable.lowMask[RV770_SMC_VOLTAGEMASK_VDDC] =
   1133  1.1  riastrad 		cpu_to_be32(pi->vddc_mask_low);
   1134  1.1  riastrad 
   1135  1.1  riastrad 	for (i = 0;
   1136  1.1  riastrad 	     ((i < pi->valid_vddc_entries) &&
   1137  1.1  riastrad 	      (pi->max_vddc_in_table >
   1138  1.1  riastrad 	       pi->vddc_table[i].vddc));
   1139  1.1  riastrad 	     i++);
   1140  1.1  riastrad 
   1141  1.1  riastrad 	table->maxVDDCIndexInPPTable =
   1142  1.1  riastrad 		pi->vddc_table[i].vddc_index;
   1143  1.1  riastrad 
   1144  1.1  riastrad 	return 0;
   1145  1.1  riastrad }
   1146  1.1  riastrad 
   1147  1.1  riastrad static int rv770_populate_smc_mvdd_table(struct radeon_device *rdev,
   1148  1.1  riastrad 					 RV770_SMC_STATETABLE *table)
   1149  1.1  riastrad {
   1150  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1151  1.1  riastrad 
   1152  1.1  riastrad 	if (pi->mvdd_control) {
   1153  1.1  riastrad 		table->lowSMIO[MVDD_HIGH_INDEX] |=
   1154  1.1  riastrad 			cpu_to_be32(pi->mvdd_low_smio[MVDD_HIGH_INDEX]);
   1155  1.1  riastrad 		table->lowSMIO[MVDD_LOW_INDEX] |=
   1156  1.1  riastrad 			cpu_to_be32(pi->mvdd_low_smio[MVDD_LOW_INDEX]);
   1157  1.1  riastrad 
   1158  1.1  riastrad 		table->voltageMaskTable.highMask[RV770_SMC_VOLTAGEMASK_MVDD] = 0;
   1159  1.1  riastrad 		table->voltageMaskTable.lowMask[RV770_SMC_VOLTAGEMASK_MVDD] =
   1160  1.1  riastrad 			cpu_to_be32(pi->mvdd_mask_low);
   1161  1.1  riastrad 	}
   1162  1.1  riastrad 
   1163  1.1  riastrad 	return 0;
   1164  1.1  riastrad }
   1165  1.1  riastrad 
   1166  1.1  riastrad static int rv770_init_smc_table(struct radeon_device *rdev,
   1167  1.1  riastrad 				struct radeon_ps *radeon_boot_state)
   1168  1.1  riastrad {
   1169  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1170  1.1  riastrad 	struct rv7xx_ps *boot_state = rv770_get_ps(radeon_boot_state);
   1171  1.1  riastrad 	RV770_SMC_STATETABLE *table = &pi->smc_statetable;
   1172  1.1  riastrad 	int ret;
   1173  1.1  riastrad 
   1174  1.1  riastrad 	memset(table, 0, sizeof(RV770_SMC_STATETABLE));
   1175  1.1  riastrad 
   1176  1.1  riastrad 	pi->boot_sclk = boot_state->low.sclk;
   1177  1.1  riastrad 
   1178  1.1  riastrad 	rv770_populate_smc_vddc_table(rdev, table);
   1179  1.1  riastrad 	rv770_populate_smc_mvdd_table(rdev, table);
   1180  1.1  riastrad 
   1181  1.1  riastrad 	switch (rdev->pm.int_thermal_type) {
   1182  1.3  riastrad 	case THERMAL_TYPE_RV770:
   1183  1.3  riastrad 	case THERMAL_TYPE_ADT7473_WITH_INTERNAL:
   1184  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_INTERNAL;
   1185  1.1  riastrad 		break;
   1186  1.3  riastrad 	case THERMAL_TYPE_NONE:
   1187  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_NONE;
   1188  1.1  riastrad 		break;
   1189  1.3  riastrad 	case THERMAL_TYPE_EXTERNAL_GPIO:
   1190  1.3  riastrad 	default:
   1191  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_EXTERNAL;
   1192  1.1  riastrad 		break;
   1193  1.1  riastrad 	}
   1194  1.1  riastrad 
   1195  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC) {
   1196  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
   1197  1.1  riastrad 
   1198  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_DONT_WAIT_FOR_VBLANK_ON_ALERT)
   1199  1.1  riastrad 			table->extraFlags |= PPSMC_EXTRAFLAGS_AC2DC_DONT_WAIT_FOR_VBLANK;
   1200  1.1  riastrad 
   1201  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_GOTO_BOOT_ON_ALERT)
   1202  1.1  riastrad 			table->extraFlags |= PPSMC_EXTRAFLAGS_AC2DC_ACTION_GOTOINITIALSTATE;
   1203  1.1  riastrad 	}
   1204  1.1  riastrad 
   1205  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
   1206  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
   1207  1.1  riastrad 
   1208  1.1  riastrad 	if (pi->mem_gddr5)
   1209  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
   1210  1.1  riastrad 
   1211  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1212  1.1  riastrad 		ret = rv730_populate_smc_initial_state(rdev, radeon_boot_state, table);
   1213  1.1  riastrad 	else
   1214  1.1  riastrad 		ret = rv770_populate_smc_initial_state(rdev, radeon_boot_state, table);
   1215  1.1  riastrad 	if (ret)
   1216  1.1  riastrad 		return ret;
   1217  1.1  riastrad 
   1218  1.1  riastrad 	if (rdev->family == CHIP_RV740)
   1219  1.1  riastrad 		ret = rv740_populate_smc_acpi_state(rdev, table);
   1220  1.1  riastrad 	else if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1221  1.1  riastrad 		ret = rv730_populate_smc_acpi_state(rdev, table);
   1222  1.1  riastrad 	else
   1223  1.1  riastrad 		ret = rv770_populate_smc_acpi_state(rdev, table);
   1224  1.1  riastrad 	if (ret)
   1225  1.1  riastrad 		return ret;
   1226  1.1  riastrad 
   1227  1.1  riastrad 	table->driverState = table->initialState;
   1228  1.1  riastrad 
   1229  1.1  riastrad 	return rv770_copy_bytes_to_smc(rdev,
   1230  1.1  riastrad 				       pi->state_table_start,
   1231  1.1  riastrad 				       (const u8 *)table,
   1232  1.1  riastrad 				       sizeof(RV770_SMC_STATETABLE),
   1233  1.1  riastrad 				       pi->sram_end);
   1234  1.1  riastrad }
   1235  1.1  riastrad 
   1236  1.1  riastrad static int rv770_construct_vddc_table(struct radeon_device *rdev)
   1237  1.1  riastrad {
   1238  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1239  1.2  riastrad 	u16 min, max, step;
   1240  1.1  riastrad 	u32 steps = 0;
   1241  1.1  riastrad 	u8 vddc_index = 0;
   1242  1.1  riastrad 	u32 i;
   1243  1.1  riastrad 
   1244  1.2  riastrad 	radeon_atom_get_min_voltage(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, &min);
   1245  1.2  riastrad 	radeon_atom_get_max_voltage(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, &max);
   1246  1.1  riastrad 	radeon_atom_get_voltage_step(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, &step);
   1247  1.1  riastrad 
   1248  1.2  riastrad 	steps = (max - min) / step + 1;
   1249  1.1  riastrad 
   1250  1.1  riastrad 	if (steps > MAX_NO_VREG_STEPS)
   1251  1.1  riastrad 		return -EINVAL;
   1252  1.1  riastrad 
   1253  1.1  riastrad 	for (i = 0; i < steps; i++) {
   1254  1.1  riastrad 		u32 gpio_pins, gpio_mask;
   1255  1.1  riastrad 
   1256  1.2  riastrad 		pi->vddc_table[i].vddc = (u16)(min + i * step);
   1257  1.1  riastrad 		radeon_atom_get_voltage_gpio_settings(rdev,
   1258  1.1  riastrad 						      pi->vddc_table[i].vddc,
   1259  1.1  riastrad 						      SET_VOLTAGE_TYPE_ASIC_VDDC,
   1260  1.1  riastrad 						      &gpio_pins, &gpio_mask);
   1261  1.1  riastrad 		pi->vddc_table[i].low_smio = gpio_pins & gpio_mask;
   1262  1.1  riastrad 		pi->vddc_table[i].high_smio = 0;
   1263  1.1  riastrad 		pi->vddc_mask_low = gpio_mask;
   1264  1.1  riastrad 		if (i > 0) {
   1265  1.1  riastrad 			if ((pi->vddc_table[i].low_smio !=
   1266  1.1  riastrad 			     pi->vddc_table[i - 1].low_smio ) ||
   1267  1.1  riastrad 			     (pi->vddc_table[i].high_smio !=
   1268  1.1  riastrad 			      pi->vddc_table[i - 1].high_smio))
   1269  1.1  riastrad 				vddc_index++;
   1270  1.1  riastrad 		}
   1271  1.1  riastrad 		pi->vddc_table[i].vddc_index = vddc_index;
   1272  1.1  riastrad 	}
   1273  1.1  riastrad 
   1274  1.1  riastrad 	pi->valid_vddc_entries = (u8)steps;
   1275  1.1  riastrad 
   1276  1.1  riastrad 	return 0;
   1277  1.1  riastrad }
   1278  1.1  riastrad 
   1279  1.1  riastrad static u32 rv770_get_mclk_split_point(struct atom_memory_info *memory_info)
   1280  1.1  riastrad {
   1281  1.1  riastrad 	if (memory_info->mem_type == MEM_TYPE_GDDR3)
   1282  1.1  riastrad 		return 30000;
   1283  1.1  riastrad 
   1284  1.1  riastrad 	return 0;
   1285  1.1  riastrad }
   1286  1.1  riastrad 
   1287  1.1  riastrad static int rv770_get_mvdd_pin_configuration(struct radeon_device *rdev)
   1288  1.1  riastrad {
   1289  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1290  1.1  riastrad 	u32 gpio_pins, gpio_mask;
   1291  1.1  riastrad 
   1292  1.1  riastrad 	radeon_atom_get_voltage_gpio_settings(rdev,
   1293  1.1  riastrad 					      MVDD_HIGH_VALUE, SET_VOLTAGE_TYPE_ASIC_MVDDC,
   1294  1.1  riastrad 					      &gpio_pins, &gpio_mask);
   1295  1.1  riastrad 	pi->mvdd_mask_low = gpio_mask;
   1296  1.1  riastrad 	pi->mvdd_low_smio[MVDD_HIGH_INDEX] =
   1297  1.1  riastrad 		gpio_pins & gpio_mask;
   1298  1.1  riastrad 
   1299  1.1  riastrad 	radeon_atom_get_voltage_gpio_settings(rdev,
   1300  1.1  riastrad 					      MVDD_LOW_VALUE, SET_VOLTAGE_TYPE_ASIC_MVDDC,
   1301  1.1  riastrad 					      &gpio_pins, &gpio_mask);
   1302  1.1  riastrad 	pi->mvdd_low_smio[MVDD_LOW_INDEX] =
   1303  1.1  riastrad 		gpio_pins & gpio_mask;
   1304  1.1  riastrad 
   1305  1.1  riastrad 	return 0;
   1306  1.1  riastrad }
   1307  1.1  riastrad 
   1308  1.1  riastrad u8 rv770_get_memory_module_index(struct radeon_device *rdev)
   1309  1.1  riastrad {
   1310  1.1  riastrad 	return (u8) ((RREG32(BIOS_SCRATCH_4) >> 16) & 0xff);
   1311  1.1  riastrad }
   1312  1.1  riastrad 
   1313  1.1  riastrad static int rv770_get_mvdd_configuration(struct radeon_device *rdev)
   1314  1.1  riastrad {
   1315  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1316  1.1  riastrad 	u8 memory_module_index;
   1317  1.1  riastrad 	struct atom_memory_info memory_info;
   1318  1.1  riastrad 
   1319  1.1  riastrad 	memory_module_index = rv770_get_memory_module_index(rdev);
   1320  1.1  riastrad 
   1321  1.1  riastrad 	if (radeon_atom_get_memory_info(rdev, memory_module_index, &memory_info)) {
   1322  1.1  riastrad 		pi->mvdd_control = false;
   1323  1.1  riastrad 		return 0;
   1324  1.1  riastrad 	}
   1325  1.1  riastrad 
   1326  1.1  riastrad 	pi->mvdd_split_frequency =
   1327  1.1  riastrad 		rv770_get_mclk_split_point(&memory_info);
   1328  1.1  riastrad 
   1329  1.1  riastrad 	if (pi->mvdd_split_frequency == 0) {
   1330  1.1  riastrad 		pi->mvdd_control = false;
   1331  1.1  riastrad 		return 0;
   1332  1.1  riastrad 	}
   1333  1.1  riastrad 
   1334  1.1  riastrad 	return rv770_get_mvdd_pin_configuration(rdev);
   1335  1.1  riastrad }
   1336  1.1  riastrad 
   1337  1.1  riastrad void rv770_enable_voltage_control(struct radeon_device *rdev,
   1338  1.1  riastrad 				  bool enable)
   1339  1.1  riastrad {
   1340  1.1  riastrad 	if (enable)
   1341  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, VOLT_PWRMGT_EN, ~VOLT_PWRMGT_EN);
   1342  1.1  riastrad 	else
   1343  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~VOLT_PWRMGT_EN);
   1344  1.1  riastrad }
   1345  1.1  riastrad 
   1346  1.1  riastrad static void rv770_program_display_gap(struct radeon_device *rdev)
   1347  1.1  riastrad {
   1348  1.1  riastrad 	u32 tmp = RREG32(CG_DISPLAY_GAP_CNTL);
   1349  1.1  riastrad 
   1350  1.1  riastrad 	tmp &= ~(DISP1_GAP_MCHG_MASK | DISP2_GAP_MCHG_MASK);
   1351  1.1  riastrad 	if (rdev->pm.dpm.new_active_crtcs & 1) {
   1352  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK);
   1353  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1354  1.1  riastrad 	} else if (rdev->pm.dpm.new_active_crtcs & 2) {
   1355  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1356  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK);
   1357  1.1  riastrad 	} else {
   1358  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1359  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1360  1.1  riastrad 	}
   1361  1.1  riastrad 	WREG32(CG_DISPLAY_GAP_CNTL, tmp);
   1362  1.1  riastrad }
   1363  1.1  riastrad 
   1364  1.1  riastrad static void rv770_enable_dynamic_pcie_gen2(struct radeon_device *rdev,
   1365  1.1  riastrad 					   bool enable)
   1366  1.1  riastrad {
   1367  1.1  riastrad 	rv770_enable_bif_dynamic_pcie_gen2(rdev, enable);
   1368  1.1  riastrad 
   1369  1.1  riastrad 	if (enable)
   1370  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, ENABLE_GEN2PCIE, ~ENABLE_GEN2PCIE);
   1371  1.1  riastrad 	else
   1372  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~ENABLE_GEN2PCIE);
   1373  1.1  riastrad }
   1374  1.1  riastrad 
   1375  1.1  riastrad static void r7xx_program_memory_timing_parameters(struct radeon_device *rdev,
   1376  1.1  riastrad 						  struct radeon_ps *radeon_new_state)
   1377  1.1  riastrad {
   1378  1.1  riastrad 	if ((rdev->family == CHIP_RV730) ||
   1379  1.1  riastrad 	    (rdev->family == CHIP_RV710) ||
   1380  1.1  riastrad 	    (rdev->family == CHIP_RV740))
   1381  1.1  riastrad 		rv730_program_memory_timing_parameters(rdev, radeon_new_state);
   1382  1.1  riastrad 	else
   1383  1.1  riastrad 		rv770_program_memory_timing_parameters(rdev, radeon_new_state);
   1384  1.1  riastrad }
   1385  1.1  riastrad 
   1386  1.1  riastrad static int rv770_upload_sw_state(struct radeon_device *rdev,
   1387  1.1  riastrad 				 struct radeon_ps *radeon_new_state)
   1388  1.1  riastrad {
   1389  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1390  1.1  riastrad 	u16 address = pi->state_table_start +
   1391  1.1  riastrad 		offsetof(RV770_SMC_STATETABLE, driverState);
   1392  1.1  riastrad 	RV770_SMC_SWSTATE state = { 0 };
   1393  1.1  riastrad 	int ret;
   1394  1.1  riastrad 
   1395  1.1  riastrad 	ret = rv770_convert_power_state_to_smc(rdev, radeon_new_state, &state);
   1396  1.1  riastrad 	if (ret)
   1397  1.1  riastrad 		return ret;
   1398  1.1  riastrad 
   1399  1.1  riastrad 	return rv770_copy_bytes_to_smc(rdev, address, (const u8 *)&state,
   1400  1.1  riastrad 				       sizeof(RV770_SMC_SWSTATE),
   1401  1.1  riastrad 				       pi->sram_end);
   1402  1.1  riastrad }
   1403  1.1  riastrad 
   1404  1.1  riastrad int rv770_halt_smc(struct radeon_device *rdev)
   1405  1.1  riastrad {
   1406  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_Halt) != PPSMC_Result_OK)
   1407  1.1  riastrad 		return -EINVAL;
   1408  1.1  riastrad 
   1409  1.1  riastrad 	if (rv770_wait_for_smc_inactive(rdev) != PPSMC_Result_OK)
   1410  1.1  riastrad 		return -EINVAL;
   1411  1.1  riastrad 
   1412  1.1  riastrad 	return 0;
   1413  1.1  riastrad }
   1414  1.1  riastrad 
   1415  1.1  riastrad int rv770_resume_smc(struct radeon_device *rdev)
   1416  1.1  riastrad {
   1417  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_Resume) != PPSMC_Result_OK)
   1418  1.1  riastrad 		return -EINVAL;
   1419  1.1  riastrad 	return 0;
   1420  1.1  riastrad }
   1421  1.1  riastrad 
   1422  1.1  riastrad int rv770_set_sw_state(struct radeon_device *rdev)
   1423  1.1  riastrad {
   1424  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_SwitchToSwState) != PPSMC_Result_OK)
   1425  1.1  riastrad 		DRM_DEBUG("rv770_set_sw_state failed\n");
   1426  1.1  riastrad 	return 0;
   1427  1.1  riastrad }
   1428  1.1  riastrad 
   1429  1.1  riastrad int rv770_set_boot_state(struct radeon_device *rdev)
   1430  1.1  riastrad {
   1431  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_SwitchToInitialState) != PPSMC_Result_OK)
   1432  1.1  riastrad 		return -EINVAL;
   1433  1.1  riastrad 	return 0;
   1434  1.1  riastrad }
   1435  1.1  riastrad 
   1436  1.1  riastrad void rv770_set_uvd_clock_before_set_eng_clock(struct radeon_device *rdev,
   1437  1.1  riastrad 					      struct radeon_ps *new_ps,
   1438  1.1  riastrad 					      struct radeon_ps *old_ps)
   1439  1.1  riastrad {
   1440  1.1  riastrad 	struct rv7xx_ps *new_state = rv770_get_ps(new_ps);
   1441  1.1  riastrad 	struct rv7xx_ps *current_state = rv770_get_ps(old_ps);
   1442  1.1  riastrad 
   1443  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   1444  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   1445  1.1  riastrad 		return;
   1446  1.1  riastrad 
   1447  1.1  riastrad 	if (new_state->high.sclk >= current_state->high.sclk)
   1448  1.1  riastrad 		return;
   1449  1.1  riastrad 
   1450  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   1451  1.1  riastrad }
   1452  1.1  riastrad 
   1453  1.1  riastrad void rv770_set_uvd_clock_after_set_eng_clock(struct radeon_device *rdev,
   1454  1.1  riastrad 					     struct radeon_ps *new_ps,
   1455  1.1  riastrad 					     struct radeon_ps *old_ps)
   1456  1.1  riastrad {
   1457  1.1  riastrad 	struct rv7xx_ps *new_state = rv770_get_ps(new_ps);
   1458  1.1  riastrad 	struct rv7xx_ps *current_state = rv770_get_ps(old_ps);
   1459  1.1  riastrad 
   1460  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   1461  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   1462  1.1  riastrad 		return;
   1463  1.1  riastrad 
   1464  1.1  riastrad 	if (new_state->high.sclk < current_state->high.sclk)
   1465  1.1  riastrad 		return;
   1466  1.1  riastrad 
   1467  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   1468  1.1  riastrad }
   1469  1.1  riastrad 
   1470  1.1  riastrad int rv770_restrict_performance_levels_before_switch(struct radeon_device *rdev)
   1471  1.1  riastrad {
   1472  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, (PPSMC_Msg)(PPSMC_MSG_NoForcedLevel)) != PPSMC_Result_OK)
   1473  1.1  riastrad 		return -EINVAL;
   1474  1.1  riastrad 
   1475  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, (PPSMC_Msg)(PPSMC_MSG_TwoLevelsDisabled)) != PPSMC_Result_OK)
   1476  1.1  riastrad 		return -EINVAL;
   1477  1.1  riastrad 
   1478  1.1  riastrad 	return 0;
   1479  1.1  riastrad }
   1480  1.1  riastrad 
   1481  1.1  riastrad int rv770_dpm_force_performance_level(struct radeon_device *rdev,
   1482  1.1  riastrad 				      enum radeon_dpm_forced_level level)
   1483  1.1  riastrad {
   1484  1.1  riastrad 	PPSMC_Msg msg;
   1485  1.1  riastrad 
   1486  1.1  riastrad 	if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
   1487  1.1  riastrad 		if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_ZeroLevelsDisabled) != PPSMC_Result_OK)
   1488  1.1  riastrad 			return -EINVAL;
   1489  1.1  riastrad 		msg = PPSMC_MSG_ForceHigh;
   1490  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
   1491  1.1  riastrad 		if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_NoForcedLevel) != PPSMC_Result_OK)
   1492  1.1  riastrad 			return -EINVAL;
   1493  1.1  riastrad 		msg = (PPSMC_Msg)(PPSMC_MSG_TwoLevelsDisabled);
   1494  1.1  riastrad 	} else {
   1495  1.1  riastrad 		if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_NoForcedLevel) != PPSMC_Result_OK)
   1496  1.1  riastrad 			return -EINVAL;
   1497  1.1  riastrad 		msg = (PPSMC_Msg)(PPSMC_MSG_ZeroLevelsDisabled);
   1498  1.1  riastrad 	}
   1499  1.1  riastrad 
   1500  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, msg) != PPSMC_Result_OK)
   1501  1.1  riastrad 		return -EINVAL;
   1502  1.1  riastrad 
   1503  1.1  riastrad 	rdev->pm.dpm.forced_level = level;
   1504  1.1  riastrad 
   1505  1.1  riastrad 	return 0;
   1506  1.1  riastrad }
   1507  1.1  riastrad 
   1508  1.1  riastrad void r7xx_start_smc(struct radeon_device *rdev)
   1509  1.1  riastrad {
   1510  1.1  riastrad 	rv770_start_smc(rdev);
   1511  1.1  riastrad 	rv770_start_smc_clock(rdev);
   1512  1.1  riastrad }
   1513  1.1  riastrad 
   1514  1.1  riastrad 
   1515  1.1  riastrad void r7xx_stop_smc(struct radeon_device *rdev)
   1516  1.1  riastrad {
   1517  1.1  riastrad 	rv770_reset_smc(rdev);
   1518  1.1  riastrad 	rv770_stop_smc_clock(rdev);
   1519  1.1  riastrad }
   1520  1.1  riastrad 
   1521  1.1  riastrad static void rv770_read_clock_registers(struct radeon_device *rdev)
   1522  1.1  riastrad {
   1523  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1524  1.1  riastrad 
   1525  1.1  riastrad 	pi->clk_regs.rv770.cg_spll_func_cntl =
   1526  1.1  riastrad 		RREG32(CG_SPLL_FUNC_CNTL);
   1527  1.1  riastrad 	pi->clk_regs.rv770.cg_spll_func_cntl_2 =
   1528  1.1  riastrad 		RREG32(CG_SPLL_FUNC_CNTL_2);
   1529  1.1  riastrad 	pi->clk_regs.rv770.cg_spll_func_cntl_3 =
   1530  1.1  riastrad 		RREG32(CG_SPLL_FUNC_CNTL_3);
   1531  1.1  riastrad 	pi->clk_regs.rv770.cg_spll_spread_spectrum =
   1532  1.1  riastrad 		RREG32(CG_SPLL_SPREAD_SPECTRUM);
   1533  1.1  riastrad 	pi->clk_regs.rv770.cg_spll_spread_spectrum_2 =
   1534  1.1  riastrad 		RREG32(CG_SPLL_SPREAD_SPECTRUM_2);
   1535  1.1  riastrad 	pi->clk_regs.rv770.mpll_ad_func_cntl =
   1536  1.1  riastrad 		RREG32(MPLL_AD_FUNC_CNTL);
   1537  1.1  riastrad 	pi->clk_regs.rv770.mpll_ad_func_cntl_2 =
   1538  1.1  riastrad 		RREG32(MPLL_AD_FUNC_CNTL_2);
   1539  1.1  riastrad 	pi->clk_regs.rv770.mpll_dq_func_cntl =
   1540  1.1  riastrad 		RREG32(MPLL_DQ_FUNC_CNTL);
   1541  1.1  riastrad 	pi->clk_regs.rv770.mpll_dq_func_cntl_2 =
   1542  1.1  riastrad 		RREG32(MPLL_DQ_FUNC_CNTL_2);
   1543  1.1  riastrad 	pi->clk_regs.rv770.mclk_pwrmgt_cntl =
   1544  1.1  riastrad 		RREG32(MCLK_PWRMGT_CNTL);
   1545  1.1  riastrad 	pi->clk_regs.rv770.dll_cntl = RREG32(DLL_CNTL);
   1546  1.1  riastrad }
   1547  1.1  riastrad 
   1548  1.1  riastrad static void r7xx_read_clock_registers(struct radeon_device *rdev)
   1549  1.1  riastrad {
   1550  1.1  riastrad 	if (rdev->family == CHIP_RV740)
   1551  1.1  riastrad 		rv740_read_clock_registers(rdev);
   1552  1.1  riastrad 	else if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1553  1.1  riastrad 		rv730_read_clock_registers(rdev);
   1554  1.1  riastrad 	else
   1555  1.1  riastrad 		rv770_read_clock_registers(rdev);
   1556  1.1  riastrad }
   1557  1.1  riastrad 
   1558  1.1  riastrad void rv770_read_voltage_smio_registers(struct radeon_device *rdev)
   1559  1.1  riastrad {
   1560  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1561  1.1  riastrad 
   1562  1.1  riastrad 	pi->s0_vid_lower_smio_cntl =
   1563  1.1  riastrad 		RREG32(S0_VID_LOWER_SMIO_CNTL);
   1564  1.1  riastrad }
   1565  1.1  riastrad 
   1566  1.1  riastrad void rv770_reset_smio_status(struct radeon_device *rdev)
   1567  1.1  riastrad {
   1568  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1569  1.1  riastrad 	u32 sw_smio_index, vid_smio_cntl;
   1570  1.1  riastrad 
   1571  1.1  riastrad 	sw_smio_index =
   1572  1.1  riastrad 		(RREG32(GENERAL_PWRMGT) & SW_SMIO_INDEX_MASK) >> SW_SMIO_INDEX_SHIFT;
   1573  1.1  riastrad 	switch (sw_smio_index) {
   1574  1.3  riastrad 	case 3:
   1575  1.1  riastrad 		vid_smio_cntl = RREG32(S3_VID_LOWER_SMIO_CNTL);
   1576  1.1  riastrad 		break;
   1577  1.3  riastrad 	case 2:
   1578  1.1  riastrad 		vid_smio_cntl = RREG32(S2_VID_LOWER_SMIO_CNTL);
   1579  1.1  riastrad 		break;
   1580  1.3  riastrad 	case 1:
   1581  1.1  riastrad 		vid_smio_cntl = RREG32(S1_VID_LOWER_SMIO_CNTL);
   1582  1.1  riastrad 		break;
   1583  1.3  riastrad 	case 0:
   1584  1.1  riastrad 		return;
   1585  1.3  riastrad 	default:
   1586  1.1  riastrad 		vid_smio_cntl = pi->s0_vid_lower_smio_cntl;
   1587  1.1  riastrad 		break;
   1588  1.1  riastrad 	}
   1589  1.1  riastrad 
   1590  1.1  riastrad 	WREG32(S0_VID_LOWER_SMIO_CNTL, vid_smio_cntl);
   1591  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, SW_SMIO_INDEX(0), ~SW_SMIO_INDEX_MASK);
   1592  1.1  riastrad }
   1593  1.1  riastrad 
   1594  1.1  riastrad void rv770_get_memory_type(struct radeon_device *rdev)
   1595  1.1  riastrad {
   1596  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1597  1.1  riastrad 	u32 tmp;
   1598  1.1  riastrad 
   1599  1.1  riastrad 	tmp = RREG32(MC_SEQ_MISC0);
   1600  1.1  riastrad 
   1601  1.1  riastrad 	if (((tmp & MC_SEQ_MISC0_GDDR5_MASK) >> MC_SEQ_MISC0_GDDR5_SHIFT) ==
   1602  1.1  riastrad 	    MC_SEQ_MISC0_GDDR5_VALUE)
   1603  1.1  riastrad 		pi->mem_gddr5 = true;
   1604  1.1  riastrad 	else
   1605  1.1  riastrad 		pi->mem_gddr5 = false;
   1606  1.1  riastrad 
   1607  1.1  riastrad }
   1608  1.1  riastrad 
   1609  1.1  riastrad void rv770_get_pcie_gen2_status(struct radeon_device *rdev)
   1610  1.1  riastrad {
   1611  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1612  1.1  riastrad 	u32 tmp;
   1613  1.1  riastrad 
   1614  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
   1615  1.1  riastrad 
   1616  1.1  riastrad 	if ((tmp & LC_OTHER_SIDE_EVER_SENT_GEN2) &&
   1617  1.1  riastrad 	    (tmp & LC_OTHER_SIDE_SUPPORTS_GEN2))
   1618  1.1  riastrad 		pi->pcie_gen2 = true;
   1619  1.1  riastrad 	else
   1620  1.1  riastrad 		pi->pcie_gen2 = false;
   1621  1.1  riastrad 
   1622  1.1  riastrad 	if (pi->pcie_gen2) {
   1623  1.1  riastrad 		if (tmp & LC_CURRENT_DATA_RATE)
   1624  1.1  riastrad 			pi->boot_in_gen2 = true;
   1625  1.1  riastrad 		else
   1626  1.1  riastrad 			pi->boot_in_gen2 = false;
   1627  1.1  riastrad 	} else
   1628  1.1  riastrad 		pi->boot_in_gen2 = false;
   1629  1.1  riastrad }
   1630  1.1  riastrad 
   1631  1.1  riastrad #if 0
   1632  1.1  riastrad static int rv770_enter_ulp_state(struct radeon_device *rdev)
   1633  1.1  riastrad {
   1634  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1635  1.1  riastrad 
   1636  1.1  riastrad 	if (pi->gfx_clock_gating) {
   1637  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~DYN_GFX_CLK_OFF_EN);
   1638  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, GFX_CLK_FORCE_ON, ~GFX_CLK_FORCE_ON);
   1639  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~GFX_CLK_FORCE_ON);
   1640  1.1  riastrad 		RREG32(GB_TILING_CONFIG);
   1641  1.1  riastrad 	}
   1642  1.1  riastrad 
   1643  1.1  riastrad 	WREG32_P(SMC_MSG, HOST_SMC_MSG(PPSMC_MSG_SwitchToMinimumPower),
   1644  1.1  riastrad 		 ~HOST_SMC_MSG_MASK);
   1645  1.1  riastrad 
   1646  1.1  riastrad 	udelay(7000);
   1647  1.1  riastrad 
   1648  1.1  riastrad 	return 0;
   1649  1.1  riastrad }
   1650  1.1  riastrad 
   1651  1.1  riastrad static int rv770_exit_ulp_state(struct radeon_device *rdev)
   1652  1.1  riastrad {
   1653  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1654  1.1  riastrad 	int i;
   1655  1.1  riastrad 
   1656  1.1  riastrad 	WREG32_P(SMC_MSG, HOST_SMC_MSG(PPSMC_MSG_ResumeFromMinimumPower),
   1657  1.1  riastrad 		 ~HOST_SMC_MSG_MASK);
   1658  1.1  riastrad 
   1659  1.1  riastrad 	udelay(7000);
   1660  1.1  riastrad 
   1661  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   1662  1.1  riastrad 		if (((RREG32(SMC_MSG) & HOST_SMC_RESP_MASK) >> HOST_SMC_RESP_SHIFT) == 1)
   1663  1.1  riastrad 			break;
   1664  1.1  riastrad 		udelay(1000);
   1665  1.1  riastrad 	}
   1666  1.1  riastrad 
   1667  1.1  riastrad 	if (pi->gfx_clock_gating)
   1668  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, DYN_GFX_CLK_OFF_EN, ~DYN_GFX_CLK_OFF_EN);
   1669  1.1  riastrad 
   1670  1.1  riastrad 	return 0;
   1671  1.1  riastrad }
   1672  1.1  riastrad #endif
   1673  1.1  riastrad 
   1674  1.1  riastrad static void rv770_get_mclk_odt_threshold(struct radeon_device *rdev)
   1675  1.1  riastrad {
   1676  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1677  1.1  riastrad 	u8 memory_module_index;
   1678  1.1  riastrad 	struct atom_memory_info memory_info;
   1679  1.1  riastrad 
   1680  1.1  riastrad 	pi->mclk_odt_threshold = 0;
   1681  1.1  riastrad 
   1682  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710)) {
   1683  1.1  riastrad 		memory_module_index = rv770_get_memory_module_index(rdev);
   1684  1.1  riastrad 
   1685  1.1  riastrad 		if (radeon_atom_get_memory_info(rdev, memory_module_index, &memory_info))
   1686  1.1  riastrad 			return;
   1687  1.1  riastrad 
   1688  1.1  riastrad 		if (memory_info.mem_type == MEM_TYPE_DDR2 ||
   1689  1.1  riastrad 		    memory_info.mem_type == MEM_TYPE_DDR3)
   1690  1.1  riastrad 			pi->mclk_odt_threshold = 30000;
   1691  1.1  riastrad 	}
   1692  1.1  riastrad }
   1693  1.1  riastrad 
   1694  1.1  riastrad void rv770_get_max_vddc(struct radeon_device *rdev)
   1695  1.1  riastrad {
   1696  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1697  1.1  riastrad 	u16 vddc;
   1698  1.1  riastrad 
   1699  1.1  riastrad 	if (radeon_atom_get_max_vddc(rdev, 0, 0, &vddc))
   1700  1.1  riastrad 		pi->max_vddc = 0;
   1701  1.1  riastrad 	else
   1702  1.1  riastrad 		pi->max_vddc = vddc;
   1703  1.1  riastrad }
   1704  1.1  riastrad 
   1705  1.1  riastrad void rv770_program_response_times(struct radeon_device *rdev)
   1706  1.1  riastrad {
   1707  1.1  riastrad 	u32 voltage_response_time, backbias_response_time;
   1708  1.1  riastrad 	u32 acpi_delay_time, vbi_time_out;
   1709  1.1  riastrad 	u32 vddc_dly, bb_dly, acpi_dly, vbi_dly;
   1710  1.1  riastrad 	u32 reference_clock;
   1711  1.1  riastrad 
   1712  1.1  riastrad 	voltage_response_time = (u32)rdev->pm.dpm.voltage_response_time;
   1713  1.1  riastrad 	backbias_response_time = (u32)rdev->pm.dpm.backbias_response_time;
   1714  1.1  riastrad 
   1715  1.1  riastrad 	if (voltage_response_time == 0)
   1716  1.1  riastrad 		voltage_response_time = 1000;
   1717  1.1  riastrad 
   1718  1.1  riastrad 	if (backbias_response_time == 0)
   1719  1.1  riastrad 		backbias_response_time = 1000;
   1720  1.1  riastrad 
   1721  1.1  riastrad 	acpi_delay_time = 15000;
   1722  1.1  riastrad 	vbi_time_out = 100000;
   1723  1.1  riastrad 
   1724  1.1  riastrad 	reference_clock = radeon_get_xclk(rdev);
   1725  1.1  riastrad 
   1726  1.1  riastrad 	vddc_dly = (voltage_response_time  * reference_clock) / 1600;
   1727  1.1  riastrad 	bb_dly = (backbias_response_time * reference_clock) / 1600;
   1728  1.1  riastrad 	acpi_dly = (acpi_delay_time * reference_clock) / 1600;
   1729  1.1  riastrad 	vbi_dly = (vbi_time_out * reference_clock) / 1600;
   1730  1.1  riastrad 
   1731  1.1  riastrad 	rv770_write_smc_soft_register(rdev,
   1732  1.1  riastrad 				      RV770_SMC_SOFT_REGISTER_delay_vreg, vddc_dly);
   1733  1.1  riastrad 	rv770_write_smc_soft_register(rdev,
   1734  1.1  riastrad 				      RV770_SMC_SOFT_REGISTER_delay_bbias, bb_dly);
   1735  1.1  riastrad 	rv770_write_smc_soft_register(rdev,
   1736  1.1  riastrad 				      RV770_SMC_SOFT_REGISTER_delay_acpi, acpi_dly);
   1737  1.1  riastrad 	rv770_write_smc_soft_register(rdev,
   1738  1.1  riastrad 				      RV770_SMC_SOFT_REGISTER_mclk_chg_timeout, vbi_dly);
   1739  1.1  riastrad #if 0
   1740  1.1  riastrad 	/* XXX look up hw revision */
   1741  1.1  riastrad 	if (WEKIVA_A21)
   1742  1.1  riastrad 		rv770_write_smc_soft_register(rdev,
   1743  1.1  riastrad 					      RV770_SMC_SOFT_REGISTER_baby_step_timer,
   1744  1.1  riastrad 					      0x10);
   1745  1.1  riastrad #endif
   1746  1.1  riastrad }
   1747  1.1  riastrad 
   1748  1.1  riastrad static void rv770_program_dcodt_before_state_switch(struct radeon_device *rdev,
   1749  1.1  riastrad 						    struct radeon_ps *radeon_new_state,
   1750  1.1  riastrad 						    struct radeon_ps *radeon_current_state)
   1751  1.1  riastrad {
   1752  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1753  1.1  riastrad 	struct rv7xx_ps *new_state = rv770_get_ps(radeon_new_state);
   1754  1.1  riastrad 	struct rv7xx_ps *current_state = rv770_get_ps(radeon_current_state);
   1755  1.1  riastrad 	bool current_use_dc = false;
   1756  1.1  riastrad 	bool new_use_dc = false;
   1757  1.1  riastrad 
   1758  1.1  riastrad 	if (pi->mclk_odt_threshold == 0)
   1759  1.1  riastrad 		return;
   1760  1.1  riastrad 
   1761  1.1  riastrad 	if (current_state->high.mclk <= pi->mclk_odt_threshold)
   1762  1.1  riastrad 		current_use_dc = true;
   1763  1.1  riastrad 
   1764  1.1  riastrad 	if (new_state->high.mclk <= pi->mclk_odt_threshold)
   1765  1.1  riastrad 		new_use_dc = true;
   1766  1.1  riastrad 
   1767  1.1  riastrad 	if (current_use_dc == new_use_dc)
   1768  1.1  riastrad 		return;
   1769  1.1  riastrad 
   1770  1.1  riastrad 	if (!current_use_dc && new_use_dc)
   1771  1.1  riastrad 		return;
   1772  1.1  riastrad 
   1773  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1774  1.1  riastrad 		rv730_program_dcodt(rdev, new_use_dc);
   1775  1.1  riastrad }
   1776  1.1  riastrad 
   1777  1.1  riastrad static void rv770_program_dcodt_after_state_switch(struct radeon_device *rdev,
   1778  1.1  riastrad 						   struct radeon_ps *radeon_new_state,
   1779  1.1  riastrad 						   struct radeon_ps *radeon_current_state)
   1780  1.1  riastrad {
   1781  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1782  1.1  riastrad 	struct rv7xx_ps *new_state = rv770_get_ps(radeon_new_state);
   1783  1.1  riastrad 	struct rv7xx_ps *current_state = rv770_get_ps(radeon_current_state);
   1784  1.1  riastrad 	bool current_use_dc = false;
   1785  1.1  riastrad 	bool new_use_dc = false;
   1786  1.1  riastrad 
   1787  1.1  riastrad 	if (pi->mclk_odt_threshold == 0)
   1788  1.1  riastrad 		return;
   1789  1.1  riastrad 
   1790  1.1  riastrad 	if (current_state->high.mclk <= pi->mclk_odt_threshold)
   1791  1.1  riastrad 		current_use_dc = true;
   1792  1.1  riastrad 
   1793  1.1  riastrad 	if (new_state->high.mclk <= pi->mclk_odt_threshold)
   1794  1.1  riastrad 		new_use_dc = true;
   1795  1.1  riastrad 
   1796  1.1  riastrad 	if (current_use_dc == new_use_dc)
   1797  1.1  riastrad 		return;
   1798  1.1  riastrad 
   1799  1.1  riastrad 	if (current_use_dc && !new_use_dc)
   1800  1.1  riastrad 		return;
   1801  1.1  riastrad 
   1802  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1803  1.1  riastrad 		rv730_program_dcodt(rdev, new_use_dc);
   1804  1.1  riastrad }
   1805  1.1  riastrad 
   1806  1.1  riastrad static void rv770_retrieve_odt_values(struct radeon_device *rdev)
   1807  1.1  riastrad {
   1808  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1809  1.1  riastrad 
   1810  1.1  riastrad 	if (pi->mclk_odt_threshold == 0)
   1811  1.1  riastrad 		return;
   1812  1.1  riastrad 
   1813  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1814  1.1  riastrad 		rv730_get_odt_values(rdev);
   1815  1.1  riastrad }
   1816  1.1  riastrad 
   1817  1.1  riastrad static void rv770_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
   1818  1.1  riastrad {
   1819  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1820  1.1  riastrad 	bool want_thermal_protection;
   1821  1.1  riastrad 	enum radeon_dpm_event_src dpm_event_src;
   1822  1.1  riastrad 
   1823  1.1  riastrad 	switch (sources) {
   1824  1.3  riastrad 	case 0:
   1825  1.3  riastrad 	default:
   1826  1.1  riastrad 		want_thermal_protection = false;
   1827  1.1  riastrad 		break;
   1828  1.3  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
   1829  1.1  riastrad 		want_thermal_protection = true;
   1830  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGITAL;
   1831  1.1  riastrad 		break;
   1832  1.1  riastrad 
   1833  1.3  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
   1834  1.1  riastrad 		want_thermal_protection = true;
   1835  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_EXTERNAL;
   1836  1.1  riastrad 		break;
   1837  1.1  riastrad 
   1838  1.3  riastrad 	case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
   1839  1.1  riastrad 	      (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
   1840  1.1  riastrad 		want_thermal_protection = true;
   1841  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL;
   1842  1.1  riastrad 		break;
   1843  1.1  riastrad 	}
   1844  1.1  riastrad 
   1845  1.1  riastrad 	if (want_thermal_protection) {
   1846  1.1  riastrad 		WREG32_P(CG_THERMAL_CTRL, DPM_EVENT_SRC(dpm_event_src), ~DPM_EVENT_SRC_MASK);
   1847  1.1  riastrad 		if (pi->thermal_protection)
   1848  1.1  riastrad 			WREG32_P(GENERAL_PWRMGT, 0, ~THERMAL_PROTECTION_DIS);
   1849  1.1  riastrad 	} else {
   1850  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, ~THERMAL_PROTECTION_DIS);
   1851  1.1  riastrad 	}
   1852  1.1  riastrad }
   1853  1.1  riastrad 
   1854  1.1  riastrad void rv770_enable_auto_throttle_source(struct radeon_device *rdev,
   1855  1.1  riastrad 				       enum radeon_dpm_auto_throttle_src source,
   1856  1.1  riastrad 				       bool enable)
   1857  1.1  riastrad {
   1858  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1859  1.1  riastrad 
   1860  1.1  riastrad 	if (enable) {
   1861  1.1  riastrad 		if (!(pi->active_auto_throttle_sources & (1 << source))) {
   1862  1.1  riastrad 			pi->active_auto_throttle_sources |= 1 << source;
   1863  1.1  riastrad 			rv770_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1864  1.1  riastrad 		}
   1865  1.1  riastrad 	} else {
   1866  1.1  riastrad 		if (pi->active_auto_throttle_sources & (1 << source)) {
   1867  1.1  riastrad 			pi->active_auto_throttle_sources &= ~(1 << source);
   1868  1.1  riastrad 			rv770_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1869  1.1  riastrad 		}
   1870  1.1  riastrad 	}
   1871  1.1  riastrad }
   1872  1.1  riastrad 
   1873  1.1  riastrad static int rv770_set_thermal_temperature_range(struct radeon_device *rdev,
   1874  1.1  riastrad 					       int min_temp, int max_temp)
   1875  1.1  riastrad {
   1876  1.1  riastrad 	int low_temp = 0 * 1000;
   1877  1.1  riastrad 	int high_temp = 255 * 1000;
   1878  1.1  riastrad 
   1879  1.1  riastrad 	if (low_temp < min_temp)
   1880  1.1  riastrad 		low_temp = min_temp;
   1881  1.1  riastrad 	if (high_temp > max_temp)
   1882  1.1  riastrad 		high_temp = max_temp;
   1883  1.1  riastrad 	if (high_temp < low_temp) {
   1884  1.1  riastrad 		DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
   1885  1.1  riastrad 		return -EINVAL;
   1886  1.1  riastrad 	}
   1887  1.1  riastrad 
   1888  1.1  riastrad 	WREG32_P(CG_THERMAL_INT, DIG_THERM_INTH(high_temp / 1000), ~DIG_THERM_INTH_MASK);
   1889  1.1  riastrad 	WREG32_P(CG_THERMAL_INT, DIG_THERM_INTL(low_temp / 1000), ~DIG_THERM_INTL_MASK);
   1890  1.1  riastrad 	WREG32_P(CG_THERMAL_CTRL, DIG_THERM_DPM(high_temp / 1000), ~DIG_THERM_DPM_MASK);
   1891  1.1  riastrad 
   1892  1.1  riastrad 	rdev->pm.dpm.thermal.min_temp = low_temp;
   1893  1.1  riastrad 	rdev->pm.dpm.thermal.max_temp = high_temp;
   1894  1.1  riastrad 
   1895  1.1  riastrad 	return 0;
   1896  1.1  riastrad }
   1897  1.1  riastrad 
   1898  1.1  riastrad int rv770_dpm_enable(struct radeon_device *rdev)
   1899  1.1  riastrad {
   1900  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1901  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   1902  1.1  riastrad 	int ret;
   1903  1.1  riastrad 
   1904  1.1  riastrad 	if (pi->gfx_clock_gating)
   1905  1.1  riastrad 		rv770_restore_cgcg(rdev);
   1906  1.1  riastrad 
   1907  1.1  riastrad 	if (rv770_dpm_enabled(rdev))
   1908  1.1  riastrad 		return -EINVAL;
   1909  1.1  riastrad 
   1910  1.1  riastrad 	if (pi->voltage_control) {
   1911  1.1  riastrad 		rv770_enable_voltage_control(rdev, true);
   1912  1.1  riastrad 		ret = rv770_construct_vddc_table(rdev);
   1913  1.1  riastrad 		if (ret) {
   1914  1.1  riastrad 			DRM_ERROR("rv770_construct_vddc_table failed\n");
   1915  1.1  riastrad 			return ret;
   1916  1.1  riastrad 		}
   1917  1.1  riastrad 	}
   1918  1.1  riastrad 
   1919  1.1  riastrad 	if (pi->dcodt)
   1920  1.1  riastrad 		rv770_retrieve_odt_values(rdev);
   1921  1.1  riastrad 
   1922  1.1  riastrad 	if (pi->mvdd_control) {
   1923  1.1  riastrad 		ret = rv770_get_mvdd_configuration(rdev);
   1924  1.1  riastrad 		if (ret) {
   1925  1.1  riastrad 			DRM_ERROR("rv770_get_mvdd_configuration failed\n");
   1926  1.1  riastrad 			return ret;
   1927  1.1  riastrad 		}
   1928  1.1  riastrad 	}
   1929  1.1  riastrad 
   1930  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1931  1.1  riastrad 		rv770_enable_backbias(rdev, true);
   1932  1.1  riastrad 
   1933  1.1  riastrad 	rv770_enable_spread_spectrum(rdev, true);
   1934  1.1  riastrad 
   1935  1.1  riastrad 	if (pi->thermal_protection)
   1936  1.1  riastrad 		rv770_enable_thermal_protection(rdev, true);
   1937  1.1  riastrad 
   1938  1.1  riastrad 	rv770_program_mpll_timing_parameters(rdev);
   1939  1.1  riastrad 	rv770_setup_bsp(rdev);
   1940  1.1  riastrad 	rv770_program_git(rdev);
   1941  1.1  riastrad 	rv770_program_tp(rdev);
   1942  1.1  riastrad 	rv770_program_tpp(rdev);
   1943  1.1  riastrad 	rv770_program_sstp(rdev);
   1944  1.1  riastrad 	rv770_program_engine_speed_parameters(rdev);
   1945  1.1  riastrad 	rv770_enable_display_gap(rdev);
   1946  1.1  riastrad 	rv770_program_vc(rdev);
   1947  1.1  riastrad 
   1948  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   1949  1.1  riastrad 		rv770_enable_dynamic_pcie_gen2(rdev, true);
   1950  1.1  riastrad 
   1951  1.1  riastrad 	ret = rv770_upload_firmware(rdev);
   1952  1.1  riastrad 	if (ret) {
   1953  1.1  riastrad 		DRM_ERROR("rv770_upload_firmware failed\n");
   1954  1.1  riastrad 		return ret;
   1955  1.1  riastrad 	}
   1956  1.1  riastrad 	ret = rv770_init_smc_table(rdev, boot_ps);
   1957  1.1  riastrad 	if (ret) {
   1958  1.1  riastrad 		DRM_ERROR("rv770_init_smc_table failed\n");
   1959  1.1  riastrad 		return ret;
   1960  1.1  riastrad 	}
   1961  1.1  riastrad 
   1962  1.1  riastrad 	rv770_program_response_times(rdev);
   1963  1.1  riastrad 	r7xx_start_smc(rdev);
   1964  1.1  riastrad 
   1965  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   1966  1.1  riastrad 		rv730_start_dpm(rdev);
   1967  1.1  riastrad 	else
   1968  1.1  riastrad 		rv770_start_dpm(rdev);
   1969  1.1  riastrad 
   1970  1.1  riastrad 	if (pi->gfx_clock_gating)
   1971  1.1  riastrad 		rv770_gfx_clock_gating_enable(rdev, true);
   1972  1.1  riastrad 
   1973  1.1  riastrad 	if (pi->mg_clock_gating)
   1974  1.1  riastrad 		rv770_mg_clock_gating_enable(rdev, true);
   1975  1.1  riastrad 
   1976  1.1  riastrad 	rv770_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
   1977  1.1  riastrad 
   1978  1.1  riastrad 	return 0;
   1979  1.1  riastrad }
   1980  1.1  riastrad 
   1981  1.1  riastrad int rv770_dpm_late_enable(struct radeon_device *rdev)
   1982  1.1  riastrad {
   1983  1.1  riastrad 	int ret;
   1984  1.1  riastrad 
   1985  1.1  riastrad 	if (rdev->irq.installed &&
   1986  1.1  riastrad 	    r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
   1987  1.1  riastrad 		PPSMC_Result result;
   1988  1.1  riastrad 
   1989  1.1  riastrad 		ret = rv770_set_thermal_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
   1990  1.1  riastrad 		if (ret)
   1991  1.1  riastrad 			return ret;
   1992  1.1  riastrad 		rdev->irq.dpm_thermal = true;
   1993  1.1  riastrad 		radeon_irq_set(rdev);
   1994  1.1  riastrad 		result = rv770_send_msg_to_smc(rdev, PPSMC_MSG_EnableThermalInterrupt);
   1995  1.1  riastrad 
   1996  1.1  riastrad 		if (result != PPSMC_Result_OK)
   1997  1.1  riastrad 			DRM_DEBUG_KMS("Could not enable thermal interrupts.\n");
   1998  1.1  riastrad 	}
   1999  1.1  riastrad 
   2000  1.1  riastrad 	return 0;
   2001  1.1  riastrad }
   2002  1.1  riastrad 
   2003  1.1  riastrad void rv770_dpm_disable(struct radeon_device *rdev)
   2004  1.1  riastrad {
   2005  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2006  1.1  riastrad 
   2007  1.1  riastrad 	if (!rv770_dpm_enabled(rdev))
   2008  1.1  riastrad 		return;
   2009  1.1  riastrad 
   2010  1.1  riastrad 	rv770_clear_vc(rdev);
   2011  1.1  riastrad 
   2012  1.1  riastrad 	if (pi->thermal_protection)
   2013  1.1  riastrad 		rv770_enable_thermal_protection(rdev, false);
   2014  1.1  riastrad 
   2015  1.1  riastrad 	rv770_enable_spread_spectrum(rdev, false);
   2016  1.1  riastrad 
   2017  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   2018  1.1  riastrad 		rv770_enable_dynamic_pcie_gen2(rdev, false);
   2019  1.1  riastrad 
   2020  1.1  riastrad 	if (rdev->irq.installed &&
   2021  1.1  riastrad 	    r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
   2022  1.1  riastrad 		rdev->irq.dpm_thermal = false;
   2023  1.1  riastrad 		radeon_irq_set(rdev);
   2024  1.1  riastrad 	}
   2025  1.1  riastrad 
   2026  1.1  riastrad 	if (pi->gfx_clock_gating)
   2027  1.1  riastrad 		rv770_gfx_clock_gating_enable(rdev, false);
   2028  1.1  riastrad 
   2029  1.1  riastrad 	if (pi->mg_clock_gating)
   2030  1.1  riastrad 		rv770_mg_clock_gating_enable(rdev, false);
   2031  1.1  riastrad 
   2032  1.1  riastrad 	if ((rdev->family == CHIP_RV730) || (rdev->family == CHIP_RV710))
   2033  1.1  riastrad 		rv730_stop_dpm(rdev);
   2034  1.1  riastrad 	else
   2035  1.1  riastrad 		rv770_stop_dpm(rdev);
   2036  1.1  riastrad 
   2037  1.1  riastrad 	r7xx_stop_smc(rdev);
   2038  1.1  riastrad 	rv770_reset_smio_status(rdev);
   2039  1.1  riastrad }
   2040  1.1  riastrad 
   2041  1.1  riastrad int rv770_dpm_set_power_state(struct radeon_device *rdev)
   2042  1.1  riastrad {
   2043  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2044  1.1  riastrad 	struct radeon_ps *new_ps = rdev->pm.dpm.requested_ps;
   2045  1.1  riastrad 	struct radeon_ps *old_ps = rdev->pm.dpm.current_ps;
   2046  1.1  riastrad 	int ret;
   2047  1.1  riastrad 
   2048  1.1  riastrad 	ret = rv770_restrict_performance_levels_before_switch(rdev);
   2049  1.1  riastrad 	if (ret) {
   2050  1.1  riastrad 		DRM_ERROR("rv770_restrict_performance_levels_before_switch failed\n");
   2051  1.1  riastrad 		return ret;
   2052  1.1  riastrad 	}
   2053  1.1  riastrad 	rv770_set_uvd_clock_before_set_eng_clock(rdev, new_ps, old_ps);
   2054  1.1  riastrad 	ret = rv770_halt_smc(rdev);
   2055  1.1  riastrad 	if (ret) {
   2056  1.1  riastrad 		DRM_ERROR("rv770_halt_smc failed\n");
   2057  1.1  riastrad 		return ret;
   2058  1.1  riastrad 	}
   2059  1.1  riastrad 	ret = rv770_upload_sw_state(rdev, new_ps);
   2060  1.1  riastrad 	if (ret) {
   2061  1.1  riastrad 		DRM_ERROR("rv770_upload_sw_state failed\n");
   2062  1.1  riastrad 		return ret;
   2063  1.1  riastrad 	}
   2064  1.1  riastrad 	r7xx_program_memory_timing_parameters(rdev, new_ps);
   2065  1.1  riastrad 	if (pi->dcodt)
   2066  1.1  riastrad 		rv770_program_dcodt_before_state_switch(rdev, new_ps, old_ps);
   2067  1.1  riastrad 	ret = rv770_resume_smc(rdev);
   2068  1.1  riastrad 	if (ret) {
   2069  1.1  riastrad 		DRM_ERROR("rv770_resume_smc failed\n");
   2070  1.1  riastrad 		return ret;
   2071  1.1  riastrad 	}
   2072  1.1  riastrad 	ret = rv770_set_sw_state(rdev);
   2073  1.1  riastrad 	if (ret) {
   2074  1.1  riastrad 		DRM_ERROR("rv770_set_sw_state failed\n");
   2075  1.1  riastrad 		return ret;
   2076  1.1  riastrad 	}
   2077  1.1  riastrad 	if (pi->dcodt)
   2078  1.1  riastrad 		rv770_program_dcodt_after_state_switch(rdev, new_ps, old_ps);
   2079  1.1  riastrad 	rv770_set_uvd_clock_after_set_eng_clock(rdev, new_ps, old_ps);
   2080  1.1  riastrad 
   2081  1.1  riastrad 	return 0;
   2082  1.1  riastrad }
   2083  1.1  riastrad 
   2084  1.1  riastrad #if 0
   2085  1.1  riastrad void rv770_dpm_reset_asic(struct radeon_device *rdev)
   2086  1.1  riastrad {
   2087  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2088  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   2089  1.1  riastrad 
   2090  1.1  riastrad 	rv770_restrict_performance_levels_before_switch(rdev);
   2091  1.1  riastrad 	if (pi->dcodt)
   2092  1.1  riastrad 		rv770_program_dcodt_before_state_switch(rdev, boot_ps, boot_ps);
   2093  1.1  riastrad 	rv770_set_boot_state(rdev);
   2094  1.1  riastrad 	if (pi->dcodt)
   2095  1.1  riastrad 		rv770_program_dcodt_after_state_switch(rdev, boot_ps, boot_ps);
   2096  1.1  riastrad }
   2097  1.1  riastrad #endif
   2098  1.1  riastrad 
   2099  1.1  riastrad void rv770_dpm_setup_asic(struct radeon_device *rdev)
   2100  1.1  riastrad {
   2101  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2102  1.1  riastrad 
   2103  1.1  riastrad 	r7xx_read_clock_registers(rdev);
   2104  1.1  riastrad 	rv770_read_voltage_smio_registers(rdev);
   2105  1.1  riastrad 	rv770_get_memory_type(rdev);
   2106  1.1  riastrad 	if (pi->dcodt)
   2107  1.1  riastrad 		rv770_get_mclk_odt_threshold(rdev);
   2108  1.1  riastrad 	rv770_get_pcie_gen2_status(rdev);
   2109  1.1  riastrad 
   2110  1.1  riastrad 	rv770_enable_acpi_pm(rdev);
   2111  1.1  riastrad 
   2112  1.1  riastrad 	if (radeon_aspm != 0) {
   2113  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_ASPM_L0s)
   2114  1.1  riastrad 			rv770_enable_l0s(rdev);
   2115  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_ASPM_L1)
   2116  1.1  riastrad 			rv770_enable_l1(rdev);
   2117  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_TURNOFFPLL_ASPML1)
   2118  1.1  riastrad 			rv770_enable_pll_sleep_in_l1(rdev);
   2119  1.1  riastrad 	}
   2120  1.1  riastrad }
   2121  1.1  riastrad 
   2122  1.1  riastrad void rv770_dpm_display_configuration_changed(struct radeon_device *rdev)
   2123  1.1  riastrad {
   2124  1.1  riastrad 	rv770_program_display_gap(rdev);
   2125  1.1  riastrad }
   2126  1.1  riastrad 
   2127  1.1  riastrad union power_info {
   2128  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO info;
   2129  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
   2130  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
   2131  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
   2132  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
   2133  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
   2134  1.1  riastrad };
   2135  1.1  riastrad 
   2136  1.1  riastrad union pplib_clock_info {
   2137  1.1  riastrad 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
   2138  1.1  riastrad 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
   2139  1.1  riastrad 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
   2140  1.1  riastrad 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
   2141  1.1  riastrad };
   2142  1.1  riastrad 
   2143  1.1  riastrad union pplib_power_state {
   2144  1.1  riastrad 	struct _ATOM_PPLIB_STATE v1;
   2145  1.1  riastrad 	struct _ATOM_PPLIB_STATE_V2 v2;
   2146  1.1  riastrad };
   2147  1.1  riastrad 
   2148  1.1  riastrad static void rv7xx_parse_pplib_non_clock_info(struct radeon_device *rdev,
   2149  1.1  riastrad 					     struct radeon_ps *rps,
   2150  1.1  riastrad 					     struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
   2151  1.1  riastrad 					     u8 table_rev)
   2152  1.1  riastrad {
   2153  1.1  riastrad 	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
   2154  1.1  riastrad 	rps->class = le16_to_cpu(non_clock_info->usClassification);
   2155  1.1  riastrad 	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
   2156  1.1  riastrad 
   2157  1.1  riastrad 	if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
   2158  1.1  riastrad 		rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
   2159  1.1  riastrad 		rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
   2160  1.1  riastrad 	} else {
   2161  1.1  riastrad 		rps->vclk = 0;
   2162  1.1  riastrad 		rps->dclk = 0;
   2163  1.1  riastrad 	}
   2164  1.1  riastrad 
   2165  1.1  riastrad 	if (r600_is_uvd_state(rps->class, rps->class2)) {
   2166  1.1  riastrad 		if ((rps->vclk == 0) || (rps->dclk == 0)) {
   2167  1.1  riastrad 			rps->vclk = RV770_DEFAULT_VCLK_FREQ;
   2168  1.1  riastrad 			rps->dclk = RV770_DEFAULT_DCLK_FREQ;
   2169  1.1  riastrad 		}
   2170  1.1  riastrad 	}
   2171  1.1  riastrad 
   2172  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
   2173  1.1  riastrad 		rdev->pm.dpm.boot_ps = rps;
   2174  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
   2175  1.1  riastrad 		rdev->pm.dpm.uvd_ps = rps;
   2176  1.1  riastrad }
   2177  1.1  riastrad 
   2178  1.1  riastrad static void rv7xx_parse_pplib_clock_info(struct radeon_device *rdev,
   2179  1.1  riastrad 					 struct radeon_ps *rps, int index,
   2180  1.1  riastrad 					 union pplib_clock_info *clock_info)
   2181  1.1  riastrad {
   2182  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2183  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   2184  1.1  riastrad 	struct rv7xx_ps *ps = rv770_get_ps(rps);
   2185  1.1  riastrad 	u32 sclk, mclk;
   2186  1.1  riastrad 	struct rv7xx_pl *pl;
   2187  1.1  riastrad 
   2188  1.1  riastrad 	switch (index) {
   2189  1.1  riastrad 	case 0:
   2190  1.1  riastrad 		pl = &ps->low;
   2191  1.1  riastrad 		break;
   2192  1.1  riastrad 	case 1:
   2193  1.1  riastrad 		pl = &ps->medium;
   2194  1.1  riastrad 		break;
   2195  1.1  riastrad 	case 2:
   2196  1.1  riastrad 	default:
   2197  1.1  riastrad 		pl = &ps->high;
   2198  1.1  riastrad 		break;
   2199  1.1  riastrad 	}
   2200  1.1  riastrad 
   2201  1.1  riastrad 	if (rdev->family >= CHIP_CEDAR) {
   2202  1.1  riastrad 		sclk = le16_to_cpu(clock_info->evergreen.usEngineClockLow);
   2203  1.1  riastrad 		sclk |= clock_info->evergreen.ucEngineClockHigh << 16;
   2204  1.1  riastrad 		mclk = le16_to_cpu(clock_info->evergreen.usMemoryClockLow);
   2205  1.1  riastrad 		mclk |= clock_info->evergreen.ucMemoryClockHigh << 16;
   2206  1.1  riastrad 
   2207  1.1  riastrad 		pl->vddc = le16_to_cpu(clock_info->evergreen.usVDDC);
   2208  1.1  riastrad 		pl->vddci = le16_to_cpu(clock_info->evergreen.usVDDCI);
   2209  1.1  riastrad 		pl->flags = le32_to_cpu(clock_info->evergreen.ulFlags);
   2210  1.1  riastrad 	} else {
   2211  1.1  riastrad 		sclk = le16_to_cpu(clock_info->r600.usEngineClockLow);
   2212  1.1  riastrad 		sclk |= clock_info->r600.ucEngineClockHigh << 16;
   2213  1.1  riastrad 		mclk = le16_to_cpu(clock_info->r600.usMemoryClockLow);
   2214  1.1  riastrad 		mclk |= clock_info->r600.ucMemoryClockHigh << 16;
   2215  1.1  riastrad 
   2216  1.1  riastrad 		pl->vddc = le16_to_cpu(clock_info->r600.usVDDC);
   2217  1.1  riastrad 		pl->flags = le32_to_cpu(clock_info->r600.ulFlags);
   2218  1.1  riastrad 	}
   2219  1.1  riastrad 
   2220  1.1  riastrad 	pl->mclk = mclk;
   2221  1.1  riastrad 	pl->sclk = sclk;
   2222  1.1  riastrad 
   2223  1.1  riastrad 	/* patch up vddc if necessary */
   2224  1.1  riastrad 	if (pl->vddc == 0xff01) {
   2225  1.1  riastrad 		if (pi->max_vddc)
   2226  1.1  riastrad 			pl->vddc = pi->max_vddc;
   2227  1.1  riastrad 	}
   2228  1.1  riastrad 
   2229  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) {
   2230  1.1  riastrad 		pi->acpi_vddc = pl->vddc;
   2231  1.1  riastrad 		if (rdev->family >= CHIP_CEDAR)
   2232  1.1  riastrad 			eg_pi->acpi_vddci = pl->vddci;
   2233  1.1  riastrad 		if (ps->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2)
   2234  1.1  riastrad 			pi->acpi_pcie_gen2 = true;
   2235  1.1  riastrad 		else
   2236  1.1  riastrad 			pi->acpi_pcie_gen2 = false;
   2237  1.1  riastrad 	}
   2238  1.1  riastrad 
   2239  1.1  riastrad 	if (rps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) {
   2240  1.1  riastrad 		if (rdev->family >= CHIP_BARTS) {
   2241  1.1  riastrad 			eg_pi->ulv.supported = true;
   2242  1.1  riastrad 			eg_pi->ulv.pl = pl;
   2243  1.1  riastrad 		}
   2244  1.1  riastrad 	}
   2245  1.1  riastrad 
   2246  1.1  riastrad 	if (pi->min_vddc_in_table > pl->vddc)
   2247  1.1  riastrad 		pi->min_vddc_in_table = pl->vddc;
   2248  1.1  riastrad 
   2249  1.1  riastrad 	if (pi->max_vddc_in_table < pl->vddc)
   2250  1.1  riastrad 		pi->max_vddc_in_table = pl->vddc;
   2251  1.1  riastrad 
   2252  1.1  riastrad 	/* patch up boot state */
   2253  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
   2254  1.1  riastrad 		u16 vddc, vddci, mvdd;
   2255  1.1  riastrad 		radeon_atombios_get_default_voltages(rdev, &vddc, &vddci, &mvdd);
   2256  1.1  riastrad 		pl->mclk = rdev->clock.default_mclk;
   2257  1.1  riastrad 		pl->sclk = rdev->clock.default_sclk;
   2258  1.1  riastrad 		pl->vddc = vddc;
   2259  1.1  riastrad 		pl->vddci = vddci;
   2260  1.1  riastrad 	}
   2261  1.1  riastrad 
   2262  1.1  riastrad 	if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
   2263  1.1  riastrad 	    ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
   2264  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk = pl->sclk;
   2265  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk = pl->mclk;
   2266  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddc = pl->vddc;
   2267  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddci = pl->vddci;
   2268  1.1  riastrad 	}
   2269  1.1  riastrad }
   2270  1.1  riastrad 
   2271  1.1  riastrad int rv7xx_parse_power_table(struct radeon_device *rdev)
   2272  1.1  riastrad {
   2273  1.1  riastrad 	struct radeon_mode_info *mode_info = &rdev->mode_info;
   2274  1.1  riastrad 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
   2275  1.1  riastrad 	union pplib_power_state *power_state;
   2276  1.1  riastrad 	int i, j;
   2277  1.1  riastrad 	union pplib_clock_info *clock_info;
   2278  1.1  riastrad 	union power_info *power_info;
   2279  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
   2280  1.3  riastrad 	u16 data_offset;
   2281  1.1  riastrad 	u8 frev, crev;
   2282  1.1  riastrad 	struct rv7xx_ps *ps;
   2283  1.1  riastrad 
   2284  1.1  riastrad 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
   2285  1.1  riastrad 				   &frev, &crev, &data_offset))
   2286  1.1  riastrad 		return -EINVAL;
   2287  1.1  riastrad 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
   2288  1.1  riastrad 
   2289  1.3  riastrad 	rdev->pm.dpm.ps = kcalloc(power_info->pplib.ucNumStates,
   2290  1.3  riastrad 				  sizeof(struct radeon_ps),
   2291  1.3  riastrad 				  GFP_KERNEL);
   2292  1.1  riastrad 	if (!rdev->pm.dpm.ps)
   2293  1.1  riastrad 		return -ENOMEM;
   2294  1.1  riastrad 
   2295  1.1  riastrad 	for (i = 0; i < power_info->pplib.ucNumStates; i++) {
   2296  1.1  riastrad 		power_state = (union pplib_power_state *)
   2297  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   2298  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usStateArrayOffset) +
   2299  1.1  riastrad 			 i * power_info->pplib.ucStateEntrySize);
   2300  1.1  riastrad 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
   2301  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   2302  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset) +
   2303  1.1  riastrad 			 (power_state->v1.ucNonClockStateIndex *
   2304  1.1  riastrad 			  power_info->pplib.ucNonClockSize));
   2305  1.1  riastrad 		if (power_info->pplib.ucStateEntrySize - 1) {
   2306  1.1  riastrad 			u8 *idx;
   2307  1.1  riastrad 			ps = kzalloc(sizeof(struct rv7xx_ps), GFP_KERNEL);
   2308  1.1  riastrad 			if (ps == NULL) {
   2309  1.1  riastrad 				kfree(rdev->pm.dpm.ps);
   2310  1.1  riastrad 				return -ENOMEM;
   2311  1.1  riastrad 			}
   2312  1.1  riastrad 			rdev->pm.dpm.ps[i].ps_priv = ps;
   2313  1.1  riastrad 			rv7xx_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
   2314  1.1  riastrad 							 non_clock_info,
   2315  1.1  riastrad 							 power_info->pplib.ucNonClockSize);
   2316  1.1  riastrad 			idx = (u8 *)&power_state->v1.ucClockStateIndices[0];
   2317  1.1  riastrad 			for (j = 0; j < (power_info->pplib.ucStateEntrySize - 1); j++) {
   2318  1.1  riastrad 				clock_info = (union pplib_clock_info *)
   2319  1.1  riastrad 					(mode_info->atom_context->bios + data_offset +
   2320  1.1  riastrad 					 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset) +
   2321  1.1  riastrad 					 (idx[j] * power_info->pplib.ucClockInfoSize));
   2322  1.1  riastrad 				rv7xx_parse_pplib_clock_info(rdev,
   2323  1.1  riastrad 							     &rdev->pm.dpm.ps[i], j,
   2324  1.1  riastrad 							     clock_info);
   2325  1.1  riastrad 			}
   2326  1.1  riastrad 		}
   2327  1.1  riastrad 	}
   2328  1.1  riastrad 	rdev->pm.dpm.num_ps = power_info->pplib.ucNumStates;
   2329  1.1  riastrad 	return 0;
   2330  1.1  riastrad }
   2331  1.1  riastrad 
   2332  1.1  riastrad void rv770_get_engine_memory_ss(struct radeon_device *rdev)
   2333  1.1  riastrad {
   2334  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2335  1.1  riastrad 	struct radeon_atom_ss ss;
   2336  1.1  riastrad 
   2337  1.1  riastrad 	pi->sclk_ss = radeon_atombios_get_asic_ss_info(rdev, &ss,
   2338  1.1  riastrad 						       ASIC_INTERNAL_ENGINE_SS, 0);
   2339  1.1  riastrad 	pi->mclk_ss = radeon_atombios_get_asic_ss_info(rdev, &ss,
   2340  1.1  riastrad 						       ASIC_INTERNAL_MEMORY_SS, 0);
   2341  1.1  riastrad 
   2342  1.1  riastrad 	if (pi->sclk_ss || pi->mclk_ss)
   2343  1.1  riastrad 		pi->dynamic_ss = true;
   2344  1.1  riastrad 	else
   2345  1.1  riastrad 		pi->dynamic_ss = false;
   2346  1.1  riastrad }
   2347  1.1  riastrad 
   2348  1.1  riastrad int rv770_dpm_init(struct radeon_device *rdev)
   2349  1.1  riastrad {
   2350  1.1  riastrad 	struct rv7xx_power_info *pi;
   2351  1.1  riastrad 	struct atom_clock_dividers dividers;
   2352  1.1  riastrad 	int ret;
   2353  1.1  riastrad 
   2354  1.1  riastrad 	pi = kzalloc(sizeof(struct rv7xx_power_info), GFP_KERNEL);
   2355  1.1  riastrad 	if (pi == NULL)
   2356  1.1  riastrad 		return -ENOMEM;
   2357  1.1  riastrad 	rdev->pm.dpm.priv = pi;
   2358  1.1  riastrad 
   2359  1.1  riastrad 	rv770_get_max_vddc(rdev);
   2360  1.1  riastrad 
   2361  1.1  riastrad 	pi->acpi_vddc = 0;
   2362  1.1  riastrad 	pi->min_vddc_in_table = 0;
   2363  1.1  riastrad 	pi->max_vddc_in_table = 0;
   2364  1.1  riastrad 
   2365  1.1  riastrad 	ret = r600_get_platform_caps(rdev);
   2366  1.1  riastrad 	if (ret)
   2367  1.1  riastrad 		return ret;
   2368  1.1  riastrad 
   2369  1.1  riastrad 	ret = rv7xx_parse_power_table(rdev);
   2370  1.1  riastrad 	if (ret)
   2371  1.1  riastrad 		return ret;
   2372  1.1  riastrad 
   2373  1.1  riastrad 	if (rdev->pm.dpm.voltage_response_time == 0)
   2374  1.1  riastrad 		rdev->pm.dpm.voltage_response_time = R600_VOLTAGERESPONSETIME_DFLT;
   2375  1.1  riastrad 	if (rdev->pm.dpm.backbias_response_time == 0)
   2376  1.1  riastrad 		rdev->pm.dpm.backbias_response_time = R600_BACKBIASRESPONSETIME_DFLT;
   2377  1.1  riastrad 
   2378  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
   2379  1.1  riastrad 					     0, false, &dividers);
   2380  1.1  riastrad 	if (ret)
   2381  1.1  riastrad 		pi->ref_div = dividers.ref_div + 1;
   2382  1.1  riastrad 	else
   2383  1.1  riastrad 		pi->ref_div = R600_REFERENCEDIVIDER_DFLT;
   2384  1.1  riastrad 
   2385  1.1  riastrad 	pi->mclk_strobe_mode_threshold = 30000;
   2386  1.1  riastrad 	pi->mclk_edc_enable_threshold = 30000;
   2387  1.1  riastrad 
   2388  1.1  riastrad 	pi->rlp = RV770_RLP_DFLT;
   2389  1.1  riastrad 	pi->rmp = RV770_RMP_DFLT;
   2390  1.1  riastrad 	pi->lhp = RV770_LHP_DFLT;
   2391  1.1  riastrad 	pi->lmp = RV770_LMP_DFLT;
   2392  1.1  riastrad 
   2393  1.1  riastrad 	pi->voltage_control =
   2394  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, 0);
   2395  1.1  riastrad 
   2396  1.1  riastrad 	pi->mvdd_control =
   2397  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_MVDDC, 0);
   2398  1.1  riastrad 
   2399  1.1  riastrad 	rv770_get_engine_memory_ss(rdev);
   2400  1.1  riastrad 
   2401  1.1  riastrad 	pi->asi = RV770_ASI_DFLT;
   2402  1.1  riastrad 	pi->pasi = RV770_HASI_DFLT;
   2403  1.1  riastrad 	pi->vrc = RV770_VRC_DFLT;
   2404  1.1  riastrad 
   2405  1.1  riastrad 	pi->power_gating = false;
   2406  1.1  riastrad 
   2407  1.1  riastrad 	pi->gfx_clock_gating = true;
   2408  1.1  riastrad 
   2409  1.1  riastrad 	pi->mg_clock_gating = true;
   2410  1.1  riastrad 	pi->mgcgtssm = true;
   2411  1.1  riastrad 
   2412  1.1  riastrad 	pi->dynamic_pcie_gen2 = true;
   2413  1.1  riastrad 
   2414  1.1  riastrad 	if (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE)
   2415  1.1  riastrad 		pi->thermal_protection = true;
   2416  1.1  riastrad 	else
   2417  1.1  riastrad 		pi->thermal_protection = false;
   2418  1.1  riastrad 
   2419  1.1  riastrad 	pi->display_gap = true;
   2420  1.1  riastrad 
   2421  1.1  riastrad 	if (rdev->flags & RADEON_IS_MOBILITY)
   2422  1.1  riastrad 		pi->dcodt = true;
   2423  1.1  riastrad 	else
   2424  1.1  riastrad 		pi->dcodt = false;
   2425  1.1  riastrad 
   2426  1.1  riastrad 	pi->ulps = true;
   2427  1.1  riastrad 
   2428  1.1  riastrad 	pi->mclk_stutter_mode_threshold = 0;
   2429  1.1  riastrad 
   2430  1.1  riastrad 	pi->sram_end = SMC_RAM_END;
   2431  1.1  riastrad 	pi->state_table_start = RV770_SMC_TABLE_ADDRESS;
   2432  1.1  riastrad 	pi->soft_regs_start = RV770_SMC_SOFT_REGISTERS_START;
   2433  1.1  riastrad 
   2434  1.1  riastrad 	return 0;
   2435  1.1  riastrad }
   2436  1.1  riastrad 
   2437  1.1  riastrad void rv770_dpm_print_power_state(struct radeon_device *rdev,
   2438  1.1  riastrad 				 struct radeon_ps *rps)
   2439  1.1  riastrad {
   2440  1.1  riastrad 	struct rv7xx_ps *ps = rv770_get_ps(rps);
   2441  1.1  riastrad 	struct rv7xx_pl *pl;
   2442  1.1  riastrad 
   2443  1.1  riastrad 	r600_dpm_print_class_info(rps->class, rps->class2);
   2444  1.1  riastrad 	r600_dpm_print_cap_info(rps->caps);
   2445  1.1  riastrad 	printk("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   2446  1.1  riastrad 	if (rdev->family >= CHIP_CEDAR) {
   2447  1.1  riastrad 		pl = &ps->low;
   2448  1.1  riastrad 		printk("\t\tpower level 0    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   2449  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   2450  1.1  riastrad 		pl = &ps->medium;
   2451  1.1  riastrad 		printk("\t\tpower level 1    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   2452  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   2453  1.1  riastrad 		pl = &ps->high;
   2454  1.1  riastrad 		printk("\t\tpower level 2    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   2455  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   2456  1.1  riastrad 	} else {
   2457  1.1  riastrad 		pl = &ps->low;
   2458  1.1  riastrad 		printk("\t\tpower level 0    sclk: %u mclk: %u vddc: %u\n",
   2459  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc);
   2460  1.1  riastrad 		pl = &ps->medium;
   2461  1.1  riastrad 		printk("\t\tpower level 1    sclk: %u mclk: %u vddc: %u\n",
   2462  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc);
   2463  1.1  riastrad 		pl = &ps->high;
   2464  1.1  riastrad 		printk("\t\tpower level 2    sclk: %u mclk: %u vddc: %u\n",
   2465  1.1  riastrad 		       pl->sclk, pl->mclk, pl->vddc);
   2466  1.1  riastrad 	}
   2467  1.1  riastrad 	r600_dpm_print_ps_status(rdev, rps);
   2468  1.1  riastrad }
   2469  1.1  riastrad 
   2470  1.1  riastrad #ifdef CONFIG_DEBUG_FS
   2471  1.1  riastrad void rv770_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
   2472  1.1  riastrad 						       struct seq_file *m)
   2473  1.1  riastrad {
   2474  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2475  1.1  riastrad 	struct rv7xx_ps *ps = rv770_get_ps(rps);
   2476  1.1  riastrad 	struct rv7xx_pl *pl;
   2477  1.1  riastrad 	u32 current_index =
   2478  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2479  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2480  1.1  riastrad 
   2481  1.1  riastrad 	if (current_index > 2) {
   2482  1.1  riastrad 		seq_printf(m, "invalid dpm profile %d\n", current_index);
   2483  1.1  riastrad 	} else {
   2484  1.1  riastrad 		if (current_index == 0)
   2485  1.1  riastrad 			pl = &ps->low;
   2486  1.1  riastrad 		else if (current_index == 1)
   2487  1.1  riastrad 			pl = &ps->medium;
   2488  1.1  riastrad 		else /* current_index == 2 */
   2489  1.1  riastrad 			pl = &ps->high;
   2490  1.1  riastrad 		seq_printf(m, "uvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   2491  1.1  riastrad 		if (rdev->family >= CHIP_CEDAR) {
   2492  1.1  riastrad 			seq_printf(m, "power level %d    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   2493  1.1  riastrad 				   current_index, pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   2494  1.1  riastrad 		} else {
   2495  1.1  riastrad 			seq_printf(m, "power level %d    sclk: %u mclk: %u vddc: %u\n",
   2496  1.1  riastrad 				   current_index, pl->sclk, pl->mclk, pl->vddc);
   2497  1.1  riastrad 		}
   2498  1.1  riastrad 	}
   2499  1.1  riastrad }
   2500  1.1  riastrad #endif
   2501  1.1  riastrad 
   2502  1.1  riastrad u32 rv770_dpm_get_current_sclk(struct radeon_device *rdev)
   2503  1.1  riastrad {
   2504  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2505  1.1  riastrad 	struct rv7xx_ps *ps = rv770_get_ps(rps);
   2506  1.1  riastrad 	struct rv7xx_pl *pl;
   2507  1.1  riastrad 	u32 current_index =
   2508  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2509  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2510  1.1  riastrad 
   2511  1.1  riastrad 	if (current_index > 2) {
   2512  1.1  riastrad 		return 0;
   2513  1.1  riastrad 	} else {
   2514  1.1  riastrad 		if (current_index == 0)
   2515  1.1  riastrad 			pl = &ps->low;
   2516  1.1  riastrad 		else if (current_index == 1)
   2517  1.1  riastrad 			pl = &ps->medium;
   2518  1.1  riastrad 		else /* current_index == 2 */
   2519  1.1  riastrad 			pl = &ps->high;
   2520  1.1  riastrad 		return  pl->sclk;
   2521  1.1  riastrad 	}
   2522  1.1  riastrad }
   2523  1.1  riastrad 
   2524  1.1  riastrad u32 rv770_dpm_get_current_mclk(struct radeon_device *rdev)
   2525  1.1  riastrad {
   2526  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2527  1.1  riastrad 	struct rv7xx_ps *ps = rv770_get_ps(rps);
   2528  1.1  riastrad 	struct rv7xx_pl *pl;
   2529  1.1  riastrad 	u32 current_index =
   2530  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2531  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2532  1.1  riastrad 
   2533  1.1  riastrad 	if (current_index > 2) {
   2534  1.1  riastrad 		return 0;
   2535  1.1  riastrad 	} else {
   2536  1.1  riastrad 		if (current_index == 0)
   2537  1.1  riastrad 			pl = &ps->low;
   2538  1.1  riastrad 		else if (current_index == 1)
   2539  1.1  riastrad 			pl = &ps->medium;
   2540  1.1  riastrad 		else /* current_index == 2 */
   2541  1.1  riastrad 			pl = &ps->high;
   2542  1.1  riastrad 		return  pl->mclk;
   2543  1.1  riastrad 	}
   2544  1.1  riastrad }
   2545  1.1  riastrad 
   2546  1.1  riastrad void rv770_dpm_fini(struct radeon_device *rdev)
   2547  1.1  riastrad {
   2548  1.1  riastrad 	int i;
   2549  1.1  riastrad 
   2550  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
   2551  1.1  riastrad 		kfree(rdev->pm.dpm.ps[i].ps_priv);
   2552  1.1  riastrad 	}
   2553  1.1  riastrad 	kfree(rdev->pm.dpm.ps);
   2554  1.1  riastrad 	kfree(rdev->pm.dpm.priv);
   2555  1.1  riastrad }
   2556  1.1  riastrad 
   2557  1.1  riastrad u32 rv770_dpm_get_sclk(struct radeon_device *rdev, bool low)
   2558  1.1  riastrad {
   2559  1.1  riastrad 	struct rv7xx_ps *requested_state = rv770_get_ps(rdev->pm.dpm.requested_ps);
   2560  1.1  riastrad 
   2561  1.1  riastrad 	if (low)
   2562  1.1  riastrad 		return requested_state->low.sclk;
   2563  1.1  riastrad 	else
   2564  1.1  riastrad 		return requested_state->high.sclk;
   2565  1.1  riastrad }
   2566  1.1  riastrad 
   2567  1.1  riastrad u32 rv770_dpm_get_mclk(struct radeon_device *rdev, bool low)
   2568  1.1  riastrad {
   2569  1.1  riastrad 	struct rv7xx_ps *requested_state = rv770_get_ps(rdev->pm.dpm.requested_ps);
   2570  1.1  riastrad 
   2571  1.1  riastrad 	if (low)
   2572  1.1  riastrad 		return requested_state->low.mclk;
   2573  1.1  riastrad 	else
   2574  1.1  riastrad 		return requested_state->high.mclk;
   2575  1.1  riastrad }
   2576  1.1  riastrad 
   2577  1.1  riastrad bool rv770_dpm_vblank_too_short(struct radeon_device *rdev)
   2578  1.1  riastrad {
   2579  1.1  riastrad 	u32 vblank_time = r600_dpm_get_vblank_time(rdev);
   2580  1.1  riastrad 	u32 switch_limit = 200; /* 300 */
   2581  1.1  riastrad 
   2582  1.1  riastrad 	/* RV770 */
   2583  1.1  riastrad 	/* mclk switching doesn't seem to work reliably on desktop RV770s */
   2584  1.1  riastrad 	if ((rdev->family == CHIP_RV770) &&
   2585  1.1  riastrad 	    !(rdev->flags & RADEON_IS_MOBILITY))
   2586  1.1  riastrad 		switch_limit = 0xffffffff; /* disable mclk switching */
   2587  1.1  riastrad 
   2588  1.1  riastrad 	if (vblank_time < switch_limit)
   2589  1.1  riastrad 		return true;
   2590  1.1  riastrad 	else
   2591  1.1  riastrad 		return false;
   2592  1.1  riastrad 
   2593  1.1  riastrad }
   2594