Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_rv6xx_dpm.c,v 1.2 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.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_rv6xx_dpm.c,v 1.2 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 "rv6xxd.h"
     33  1.1  riastrad #include "r600_dpm.h"
     34  1.1  riastrad #include "rv6xx_dpm.h"
     35  1.1  riastrad #include "atom.h"
     36  1.1  riastrad #include <linux/seq_file.h>
     37  1.1  riastrad 
     38  1.1  riastrad static u32 rv6xx_scale_count_given_unit(struct radeon_device *rdev,
     39  1.1  riastrad 					u32 unscaled_count, u32 unit);
     40  1.1  riastrad 
     41  1.1  riastrad static struct rv6xx_ps *rv6xx_get_ps(struct radeon_ps *rps)
     42  1.1  riastrad {
     43  1.1  riastrad 	struct rv6xx_ps *ps = rps->ps_priv;
     44  1.1  riastrad 
     45  1.1  riastrad 	return ps;
     46  1.1  riastrad }
     47  1.1  riastrad 
     48  1.1  riastrad static struct rv6xx_power_info *rv6xx_get_pi(struct radeon_device *rdev)
     49  1.1  riastrad {
     50  1.1  riastrad 	struct rv6xx_power_info *pi = rdev->pm.dpm.priv;
     51  1.1  riastrad 
     52  1.1  riastrad 	return pi;
     53  1.1  riastrad }
     54  1.1  riastrad 
     55  1.1  riastrad static void rv6xx_force_pcie_gen1(struct radeon_device *rdev)
     56  1.1  riastrad {
     57  1.1  riastrad 	u32 tmp;
     58  1.1  riastrad 	int i;
     59  1.1  riastrad 
     60  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
     61  1.1  riastrad 	tmp &= LC_GEN2_EN;
     62  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
     63  1.1  riastrad 
     64  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
     65  1.1  riastrad 	tmp |= LC_INITIATE_LINK_SPEED_CHANGE;
     66  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
     67  1.1  riastrad 
     68  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
     69  1.1  riastrad 		if (!(RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL) & LC_CURRENT_DATA_RATE))
     70  1.1  riastrad 			break;
     71  1.1  riastrad 		udelay(1);
     72  1.1  riastrad 	}
     73  1.1  riastrad 
     74  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
     75  1.1  riastrad 	tmp &= ~LC_INITIATE_LINK_SPEED_CHANGE;
     76  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
     77  1.1  riastrad }
     78  1.1  riastrad 
     79  1.1  riastrad static void rv6xx_enable_pcie_gen2_support(struct radeon_device *rdev)
     80  1.1  riastrad {
     81  1.1  riastrad 	u32 tmp;
     82  1.1  riastrad 
     83  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
     84  1.1  riastrad 
     85  1.1  riastrad 	if ((tmp & LC_OTHER_SIDE_EVER_SENT_GEN2) &&
     86  1.1  riastrad 	    (tmp & LC_OTHER_SIDE_SUPPORTS_GEN2)) {
     87  1.1  riastrad 		tmp |= LC_GEN2_EN;
     88  1.1  riastrad 		WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
     89  1.1  riastrad 	}
     90  1.1  riastrad }
     91  1.1  riastrad 
     92  1.1  riastrad static void rv6xx_enable_bif_dynamic_pcie_gen2(struct radeon_device *rdev,
     93  1.1  riastrad 					       bool enable)
     94  1.1  riastrad {
     95  1.1  riastrad 	u32 tmp;
     96  1.1  riastrad 
     97  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL) & ~LC_HW_VOLTAGE_IF_CONTROL_MASK;
     98  1.1  riastrad 	if (enable)
     99  1.1  riastrad 		tmp |= LC_HW_VOLTAGE_IF_CONTROL(1);
    100  1.1  riastrad 	else
    101  1.1  riastrad 		tmp |= LC_HW_VOLTAGE_IF_CONTROL(0);
    102  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
    103  1.1  riastrad }
    104  1.1  riastrad 
    105  1.1  riastrad static void rv6xx_enable_l0s(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) & ~LC_L0S_INACTIVITY_MASK;
    110  1.1  riastrad 	tmp |= LC_L0S_INACTIVITY(3);
    111  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad static void rv6xx_enable_l1(struct radeon_device *rdev)
    115  1.1  riastrad {
    116  1.1  riastrad 	u32 tmp;
    117  1.1  riastrad 
    118  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL);
    119  1.1  riastrad 	tmp &= ~LC_L1_INACTIVITY_MASK;
    120  1.1  riastrad 	tmp |= LC_L1_INACTIVITY(4);
    121  1.1  riastrad 	tmp &= ~LC_PMI_TO_L1_DIS;
    122  1.1  riastrad 	tmp &= ~LC_ASPM_TO_L1_DIS;
    123  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    124  1.1  riastrad }
    125  1.1  riastrad 
    126  1.1  riastrad static void rv6xx_enable_pll_sleep_in_l1(struct radeon_device *rdev)
    127  1.1  riastrad {
    128  1.1  riastrad 	u32 tmp;
    129  1.1  riastrad 
    130  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_CNTL) & ~LC_L1_INACTIVITY_MASK;
    131  1.1  riastrad 	tmp |= LC_L1_INACTIVITY(8);
    132  1.1  riastrad 	WREG32_PCIE_PORT(PCIE_LC_CNTL, tmp);
    133  1.1  riastrad 
    134  1.1  riastrad 	/* NOTE, this is a PCIE indirect reg, not PCIE PORT */
    135  1.1  riastrad 	tmp = RREG32_PCIE(PCIE_P_CNTL);
    136  1.1  riastrad 	tmp |= P_PLL_PWRDN_IN_L1L23;
    137  1.1  riastrad 	tmp &= ~P_PLL_BUF_PDNB;
    138  1.1  riastrad 	tmp &= ~P_PLL_PDNB;
    139  1.1  riastrad 	tmp |= P_ALLOW_PRX_FRONTEND_SHUTOFF;
    140  1.1  riastrad 	WREG32_PCIE(PCIE_P_CNTL, tmp);
    141  1.1  riastrad }
    142  1.1  riastrad 
    143  1.1  riastrad static int rv6xx_convert_clock_to_stepping(struct radeon_device *rdev,
    144  1.1  riastrad 					   u32 clock, struct rv6xx_sclk_stepping *step)
    145  1.1  riastrad {
    146  1.1  riastrad 	int ret;
    147  1.1  riastrad 	struct atom_clock_dividers dividers;
    148  1.1  riastrad 
    149  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
    150  1.1  riastrad 					     clock, false, &dividers);
    151  1.1  riastrad 	if (ret)
    152  1.1  riastrad 		return ret;
    153  1.1  riastrad 
    154  1.1  riastrad 	if (dividers.enable_post_div)
    155  1.1  riastrad 		step->post_divider = 2 + (dividers.post_div & 0xF) + (dividers.post_div >> 4);
    156  1.1  riastrad 	else
    157  1.1  riastrad 		step->post_divider = 1;
    158  1.1  riastrad 
    159  1.1  riastrad 	step->vco_frequency = clock * step->post_divider;
    160  1.1  riastrad 
    161  1.1  riastrad 	return 0;
    162  1.1  riastrad }
    163  1.1  riastrad 
    164  1.1  riastrad static void rv6xx_output_stepping(struct radeon_device *rdev,
    165  1.1  riastrad 				  u32 step_index, struct rv6xx_sclk_stepping *step)
    166  1.1  riastrad {
    167  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    168  1.1  riastrad 	u32 ref_clk = rdev->clock.spll.reference_freq;
    169  1.1  riastrad 	u32 fb_divider;
    170  1.1  riastrad 	u32 spll_step_count = rv6xx_scale_count_given_unit(rdev,
    171  1.1  riastrad 							   R600_SPLLSTEPTIME_DFLT *
    172  1.1  riastrad 							   pi->spll_ref_div,
    173  1.1  riastrad 							   R600_SPLLSTEPUNIT_DFLT);
    174  1.1  riastrad 
    175  1.1  riastrad 	r600_engine_clock_entry_enable(rdev, step_index, true);
    176  1.1  riastrad 	r600_engine_clock_entry_enable_pulse_skipping(rdev, step_index, false);
    177  1.1  riastrad 
    178  1.1  riastrad 	if (step->post_divider == 1)
    179  1.1  riastrad 		r600_engine_clock_entry_enable_post_divider(rdev, step_index, false);
    180  1.1  riastrad 	else {
    181  1.1  riastrad 		u32 lo_len = (step->post_divider - 2) / 2;
    182  1.1  riastrad 		u32 hi_len = step->post_divider - 2 - lo_len;
    183  1.1  riastrad 
    184  1.1  riastrad 		r600_engine_clock_entry_enable_post_divider(rdev, step_index, true);
    185  1.1  riastrad 		r600_engine_clock_entry_set_post_divider(rdev, step_index, (hi_len << 4) | lo_len);
    186  1.1  riastrad 	}
    187  1.1  riastrad 
    188  1.1  riastrad 	fb_divider = ((step->vco_frequency * pi->spll_ref_div) / ref_clk) >>
    189  1.1  riastrad 		pi->fb_div_scale;
    190  1.1  riastrad 
    191  1.1  riastrad 	r600_engine_clock_entry_set_reference_divider(rdev, step_index,
    192  1.1  riastrad 						      pi->spll_ref_div - 1);
    193  1.1  riastrad 	r600_engine_clock_entry_set_feedback_divider(rdev, step_index, fb_divider);
    194  1.1  riastrad 	r600_engine_clock_entry_set_step_time(rdev, step_index, spll_step_count);
    195  1.1  riastrad 
    196  1.1  riastrad }
    197  1.1  riastrad 
    198  1.1  riastrad static struct rv6xx_sclk_stepping rv6xx_next_vco_step(struct radeon_device *rdev,
    199  1.1  riastrad 						      struct rv6xx_sclk_stepping *cur,
    200  1.1  riastrad 						      bool increasing_vco, u32 step_size)
    201  1.1  riastrad {
    202  1.1  riastrad 	struct rv6xx_sclk_stepping next;
    203  1.1  riastrad 
    204  1.1  riastrad 	next.post_divider = cur->post_divider;
    205  1.1  riastrad 
    206  1.1  riastrad 	if (increasing_vco)
    207  1.1  riastrad 		next.vco_frequency = (cur->vco_frequency * (100 + step_size)) / 100;
    208  1.1  riastrad 	else
    209  1.1  riastrad 		next.vco_frequency = (cur->vco_frequency * 100 + 99 + step_size) / (100 + step_size);
    210  1.1  riastrad 
    211  1.1  riastrad 	return next;
    212  1.1  riastrad }
    213  1.1  riastrad 
    214  1.1  riastrad static bool rv6xx_can_step_post_div(struct radeon_device *rdev,
    215  1.1  riastrad 				    struct rv6xx_sclk_stepping *cur,
    216  1.2  riastrad 				    struct rv6xx_sclk_stepping *target)
    217  1.1  riastrad {
    218  1.1  riastrad 	return (cur->post_divider > target->post_divider) &&
    219  1.1  riastrad 		((cur->vco_frequency * target->post_divider) <=
    220  1.1  riastrad 		 (target->vco_frequency * (cur->post_divider - 1)));
    221  1.1  riastrad }
    222  1.1  riastrad 
    223  1.1  riastrad static struct rv6xx_sclk_stepping rv6xx_next_post_div_step(struct radeon_device *rdev,
    224  1.1  riastrad 							   struct rv6xx_sclk_stepping *cur,
    225  1.1  riastrad 							   struct rv6xx_sclk_stepping *target)
    226  1.1  riastrad {
    227  1.1  riastrad 	struct rv6xx_sclk_stepping next = *cur;
    228  1.1  riastrad 
    229  1.1  riastrad 	while (rv6xx_can_step_post_div(rdev, &next, target))
    230  1.1  riastrad 		next.post_divider--;
    231  1.1  riastrad 
    232  1.1  riastrad 	return next;
    233  1.1  riastrad }
    234  1.1  riastrad 
    235  1.1  riastrad static bool rv6xx_reached_stepping_target(struct radeon_device *rdev,
    236  1.1  riastrad 					  struct rv6xx_sclk_stepping *cur,
    237  1.1  riastrad 					  struct rv6xx_sclk_stepping *target,
    238  1.1  riastrad 					  bool increasing_vco)
    239  1.1  riastrad {
    240  1.1  riastrad 	return (increasing_vco && (cur->vco_frequency >= target->vco_frequency)) ||
    241  1.1  riastrad 		(!increasing_vco && (cur->vco_frequency <= target->vco_frequency));
    242  1.1  riastrad }
    243  1.1  riastrad 
    244  1.1  riastrad static void rv6xx_generate_steps(struct radeon_device *rdev,
    245  1.1  riastrad 				 u32 low, u32 high,
    246  1.2  riastrad 				 u32 start_index, u8 *end_index)
    247  1.1  riastrad {
    248  1.1  riastrad 	struct rv6xx_sclk_stepping cur;
    249  1.1  riastrad 	struct rv6xx_sclk_stepping target;
    250  1.1  riastrad 	bool increasing_vco;
    251  1.1  riastrad 	u32 step_index = start_index;
    252  1.1  riastrad 
    253  1.1  riastrad 	rv6xx_convert_clock_to_stepping(rdev, low, &cur);
    254  1.1  riastrad 	rv6xx_convert_clock_to_stepping(rdev, high, &target);
    255  1.1  riastrad 
    256  1.1  riastrad 	rv6xx_output_stepping(rdev, step_index++, &cur);
    257  1.1  riastrad 
    258  1.1  riastrad 	increasing_vco = (target.vco_frequency >= cur.vco_frequency);
    259  1.1  riastrad 
    260  1.1  riastrad 	if (target.post_divider > cur.post_divider)
    261  1.1  riastrad 		cur.post_divider = target.post_divider;
    262  1.1  riastrad 
    263  1.1  riastrad 	while (1) {
    264  1.1  riastrad 		struct rv6xx_sclk_stepping next;
    265  1.1  riastrad 
    266  1.1  riastrad 		if (rv6xx_can_step_post_div(rdev, &cur, &target))
    267  1.1  riastrad 			next = rv6xx_next_post_div_step(rdev, &cur, &target);
    268  1.1  riastrad 		else
    269  1.1  riastrad 			next = rv6xx_next_vco_step(rdev, &cur, increasing_vco, R600_VCOSTEPPCT_DFLT);
    270  1.1  riastrad 
    271  1.1  riastrad 		if (rv6xx_reached_stepping_target(rdev, &next, &target, increasing_vco)) {
    272  1.1  riastrad 			struct rv6xx_sclk_stepping tiny =
    273  1.1  riastrad 				rv6xx_next_vco_step(rdev, &target, !increasing_vco, R600_ENDINGVCOSTEPPCT_DFLT);
    274  1.1  riastrad 			tiny.post_divider = next.post_divider;
    275  1.1  riastrad 
    276  1.1  riastrad 			if (!rv6xx_reached_stepping_target(rdev, &tiny, &cur, !increasing_vco))
    277  1.1  riastrad 				rv6xx_output_stepping(rdev, step_index++, &tiny);
    278  1.1  riastrad 
    279  1.1  riastrad 			if ((next.post_divider != target.post_divider) &&
    280  1.1  riastrad 			    (next.vco_frequency != target.vco_frequency)) {
    281  1.1  riastrad 				struct rv6xx_sclk_stepping final_vco;
    282  1.1  riastrad 
    283  1.1  riastrad 				final_vco.vco_frequency = target.vco_frequency;
    284  1.1  riastrad 				final_vco.post_divider = next.post_divider;
    285  1.1  riastrad 
    286  1.1  riastrad 				rv6xx_output_stepping(rdev, step_index++, &final_vco);
    287  1.1  riastrad 			}
    288  1.1  riastrad 
    289  1.1  riastrad 			rv6xx_output_stepping(rdev, step_index++, &target);
    290  1.1  riastrad 			break;
    291  1.1  riastrad 		} else
    292  1.1  riastrad 			rv6xx_output_stepping(rdev, step_index++, &next);
    293  1.1  riastrad 
    294  1.1  riastrad 		cur = next;
    295  1.1  riastrad 	}
    296  1.1  riastrad 
    297  1.1  riastrad 	*end_index = (u8)step_index - 1;
    298  1.1  riastrad 
    299  1.1  riastrad }
    300  1.1  riastrad 
    301  1.1  riastrad static void rv6xx_generate_single_step(struct radeon_device *rdev,
    302  1.1  riastrad 				       u32 clock, u32 index)
    303  1.1  riastrad {
    304  1.1  riastrad 	struct rv6xx_sclk_stepping step;
    305  1.1  riastrad 
    306  1.1  riastrad 	rv6xx_convert_clock_to_stepping(rdev, clock, &step);
    307  1.1  riastrad 	rv6xx_output_stepping(rdev, index, &step);
    308  1.1  riastrad }
    309  1.1  riastrad 
    310  1.1  riastrad static void rv6xx_invalidate_intermediate_steps_range(struct radeon_device *rdev,
    311  1.1  riastrad 						      u32 start_index, u32 end_index)
    312  1.1  riastrad {
    313  1.1  riastrad 	u32 step_index;
    314  1.1  riastrad 
    315  1.1  riastrad 	for (step_index = start_index + 1; step_index < end_index; step_index++)
    316  1.1  riastrad 		r600_engine_clock_entry_enable(rdev, step_index, false);
    317  1.1  riastrad }
    318  1.1  riastrad 
    319  1.1  riastrad static void rv6xx_set_engine_spread_spectrum_clk_s(struct radeon_device *rdev,
    320  1.1  riastrad 						   u32 index, u32 clk_s)
    321  1.1  riastrad {
    322  1.1  riastrad 	WREG32_P(CG_SPLL_SPREAD_SPECTRUM_LOW + (index * 4),
    323  1.1  riastrad 		 CLKS(clk_s), ~CLKS_MASK);
    324  1.1  riastrad }
    325  1.1  riastrad 
    326  1.1  riastrad static void rv6xx_set_engine_spread_spectrum_clk_v(struct radeon_device *rdev,
    327  1.1  riastrad 						   u32 index, u32 clk_v)
    328  1.1  riastrad {
    329  1.1  riastrad 	WREG32_P(CG_SPLL_SPREAD_SPECTRUM_LOW + (index * 4),
    330  1.1  riastrad 		 CLKV(clk_v), ~CLKV_MASK);
    331  1.1  riastrad }
    332  1.1  riastrad 
    333  1.1  riastrad static void rv6xx_enable_engine_spread_spectrum(struct radeon_device *rdev,
    334  1.1  riastrad 						u32 index, bool enable)
    335  1.1  riastrad {
    336  1.1  riastrad 	if (enable)
    337  1.1  riastrad 		WREG32_P(CG_SPLL_SPREAD_SPECTRUM_LOW + (index * 4),
    338  1.1  riastrad 			 SSEN, ~SSEN);
    339  1.1  riastrad 	else
    340  1.1  riastrad 		WREG32_P(CG_SPLL_SPREAD_SPECTRUM_LOW + (index * 4),
    341  1.1  riastrad 			 0, ~SSEN);
    342  1.1  riastrad }
    343  1.1  riastrad 
    344  1.1  riastrad static void rv6xx_set_memory_spread_spectrum_clk_s(struct radeon_device *rdev,
    345  1.1  riastrad 						   u32 clk_s)
    346  1.1  riastrad {
    347  1.1  riastrad 	WREG32_P(CG_MPLL_SPREAD_SPECTRUM, CLKS(clk_s), ~CLKS_MASK);
    348  1.1  riastrad }
    349  1.1  riastrad 
    350  1.1  riastrad static void rv6xx_set_memory_spread_spectrum_clk_v(struct radeon_device *rdev,
    351  1.1  riastrad 						   u32 clk_v)
    352  1.1  riastrad {
    353  1.1  riastrad 	WREG32_P(CG_MPLL_SPREAD_SPECTRUM, CLKV(clk_v), ~CLKV_MASK);
    354  1.1  riastrad }
    355  1.1  riastrad 
    356  1.1  riastrad static void rv6xx_enable_memory_spread_spectrum(struct radeon_device *rdev,
    357  1.1  riastrad 						bool enable)
    358  1.1  riastrad {
    359  1.1  riastrad 	if (enable)
    360  1.1  riastrad 		WREG32_P(CG_MPLL_SPREAD_SPECTRUM, SSEN, ~SSEN);
    361  1.1  riastrad 	else
    362  1.1  riastrad 		WREG32_P(CG_MPLL_SPREAD_SPECTRUM, 0, ~SSEN);
    363  1.1  riastrad }
    364  1.1  riastrad 
    365  1.1  riastrad static void rv6xx_enable_dynamic_spread_spectrum(struct radeon_device *rdev,
    366  1.1  riastrad 						 bool enable)
    367  1.1  riastrad {
    368  1.1  riastrad 	if (enable)
    369  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, ~DYN_SPREAD_SPECTRUM_EN);
    370  1.1  riastrad 	else
    371  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~DYN_SPREAD_SPECTRUM_EN);
    372  1.1  riastrad }
    373  1.1  riastrad 
    374  1.1  riastrad static void rv6xx_memory_clock_entry_enable_post_divider(struct radeon_device *rdev,
    375  1.1  riastrad 							 u32 index, bool enable)
    376  1.1  riastrad {
    377  1.1  riastrad 	if (enable)
    378  1.1  riastrad 		WREG32_P(MPLL_FREQ_LEVEL_0 + (index * 4),
    379  1.1  riastrad 			 LEVEL0_MPLL_DIV_EN, ~LEVEL0_MPLL_DIV_EN);
    380  1.1  riastrad 	else
    381  1.1  riastrad 		WREG32_P(MPLL_FREQ_LEVEL_0 + (index * 4), 0, ~LEVEL0_MPLL_DIV_EN);
    382  1.1  riastrad }
    383  1.1  riastrad 
    384  1.1  riastrad static void rv6xx_memory_clock_entry_set_post_divider(struct radeon_device *rdev,
    385  1.1  riastrad 						      u32 index, u32 divider)
    386  1.1  riastrad {
    387  1.1  riastrad 	WREG32_P(MPLL_FREQ_LEVEL_0 + (index * 4),
    388  1.1  riastrad 		 LEVEL0_MPLL_POST_DIV(divider), ~LEVEL0_MPLL_POST_DIV_MASK);
    389  1.1  riastrad }
    390  1.1  riastrad 
    391  1.1  riastrad static void rv6xx_memory_clock_entry_set_feedback_divider(struct radeon_device *rdev,
    392  1.1  riastrad 							  u32 index, u32 divider)
    393  1.1  riastrad {
    394  1.1  riastrad 	WREG32_P(MPLL_FREQ_LEVEL_0 + (index * 4), LEVEL0_MPLL_FB_DIV(divider),
    395  1.1  riastrad 		 ~LEVEL0_MPLL_FB_DIV_MASK);
    396  1.1  riastrad }
    397  1.1  riastrad 
    398  1.1  riastrad static void rv6xx_memory_clock_entry_set_reference_divider(struct radeon_device *rdev,
    399  1.1  riastrad 							   u32 index, u32 divider)
    400  1.1  riastrad {
    401  1.1  riastrad 	WREG32_P(MPLL_FREQ_LEVEL_0 + (index * 4),
    402  1.1  riastrad 		 LEVEL0_MPLL_REF_DIV(divider), ~LEVEL0_MPLL_REF_DIV_MASK);
    403  1.1  riastrad }
    404  1.1  riastrad 
    405  1.1  riastrad static void rv6xx_vid_response_set_brt(struct radeon_device *rdev, u32 rt)
    406  1.1  riastrad {
    407  1.1  riastrad 	WREG32_P(VID_RT, BRT(rt), ~BRT_MASK);
    408  1.1  riastrad }
    409  1.1  riastrad 
    410  1.1  riastrad static void rv6xx_enable_engine_feedback_and_reference_sync(struct radeon_device *rdev)
    411  1.1  riastrad {
    412  1.1  riastrad 	WREG32_P(SPLL_CNTL_MODE, SPLL_DIV_SYNC, ~SPLL_DIV_SYNC);
    413  1.1  riastrad }
    414  1.1  riastrad 
    415  1.1  riastrad static u32 rv6xx_clocks_per_unit(u32 unit)
    416  1.1  riastrad {
    417  1.1  riastrad 	u32 tmp = 1 << (2 * unit);
    418  1.1  riastrad 
    419  1.1  riastrad 	return tmp;
    420  1.1  riastrad }
    421  1.1  riastrad 
    422  1.1  riastrad static u32 rv6xx_scale_count_given_unit(struct radeon_device *rdev,
    423  1.1  riastrad 					u32 unscaled_count, u32 unit)
    424  1.1  riastrad {
    425  1.1  riastrad 	u32 count_per_unit = rv6xx_clocks_per_unit(unit);
    426  1.1  riastrad 
    427  1.1  riastrad 	return (unscaled_count + count_per_unit - 1) / count_per_unit;
    428  1.1  riastrad }
    429  1.1  riastrad 
    430  1.1  riastrad static u32 rv6xx_compute_count_for_delay(struct radeon_device *rdev,
    431  1.1  riastrad 					 u32 delay_us, u32 unit)
    432  1.1  riastrad {
    433  1.1  riastrad 	u32 ref_clk = rdev->clock.spll.reference_freq;
    434  1.1  riastrad 
    435  1.1  riastrad 	return rv6xx_scale_count_given_unit(rdev, delay_us * (ref_clk / 100), unit);
    436  1.1  riastrad }
    437  1.1  riastrad 
    438  1.1  riastrad static void rv6xx_calculate_engine_speed_stepping_parameters(struct radeon_device *rdev,
    439  1.1  riastrad 							     struct rv6xx_ps *state)
    440  1.1  riastrad {
    441  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    442  1.1  riastrad 
    443  1.1  riastrad 	pi->hw.sclks[R600_POWER_LEVEL_LOW] =
    444  1.1  riastrad 		state->low.sclk;
    445  1.1  riastrad 	pi->hw.sclks[R600_POWER_LEVEL_MEDIUM] =
    446  1.1  riastrad 		state->medium.sclk;
    447  1.1  riastrad 	pi->hw.sclks[R600_POWER_LEVEL_HIGH] =
    448  1.1  riastrad 		state->high.sclk;
    449  1.1  riastrad 
    450  1.1  riastrad 	pi->hw.low_sclk_index = R600_POWER_LEVEL_LOW;
    451  1.1  riastrad 	pi->hw.medium_sclk_index = R600_POWER_LEVEL_MEDIUM;
    452  1.1  riastrad 	pi->hw.high_sclk_index = R600_POWER_LEVEL_HIGH;
    453  1.1  riastrad }
    454  1.1  riastrad 
    455  1.1  riastrad static void rv6xx_calculate_memory_clock_stepping_parameters(struct radeon_device *rdev,
    456  1.1  riastrad 							     struct rv6xx_ps *state)
    457  1.1  riastrad {
    458  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    459  1.1  riastrad 
    460  1.1  riastrad 	pi->hw.mclks[R600_POWER_LEVEL_CTXSW] =
    461  1.1  riastrad 		state->high.mclk;
    462  1.1  riastrad 	pi->hw.mclks[R600_POWER_LEVEL_HIGH] =
    463  1.1  riastrad 		state->high.mclk;
    464  1.1  riastrad 	pi->hw.mclks[R600_POWER_LEVEL_MEDIUM] =
    465  1.1  riastrad 		state->medium.mclk;
    466  1.1  riastrad 	pi->hw.mclks[R600_POWER_LEVEL_LOW] =
    467  1.1  riastrad 		state->low.mclk;
    468  1.1  riastrad 
    469  1.1  riastrad 	pi->hw.high_mclk_index = R600_POWER_LEVEL_HIGH;
    470  1.1  riastrad 
    471  1.1  riastrad 	if (state->high.mclk == state->medium.mclk)
    472  1.1  riastrad 		pi->hw.medium_mclk_index =
    473  1.1  riastrad 			pi->hw.high_mclk_index;
    474  1.1  riastrad 	else
    475  1.1  riastrad 		pi->hw.medium_mclk_index = R600_POWER_LEVEL_MEDIUM;
    476  1.1  riastrad 
    477  1.1  riastrad 
    478  1.1  riastrad 	if (state->medium.mclk == state->low.mclk)
    479  1.1  riastrad 		pi->hw.low_mclk_index =
    480  1.1  riastrad 			pi->hw.medium_mclk_index;
    481  1.1  riastrad 	else
    482  1.1  riastrad 		pi->hw.low_mclk_index = R600_POWER_LEVEL_LOW;
    483  1.1  riastrad }
    484  1.1  riastrad 
    485  1.1  riastrad static void rv6xx_calculate_voltage_stepping_parameters(struct radeon_device *rdev,
    486  1.1  riastrad 							struct rv6xx_ps *state)
    487  1.1  riastrad {
    488  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    489  1.1  riastrad 
    490  1.1  riastrad 	pi->hw.vddc[R600_POWER_LEVEL_CTXSW] = state->high.vddc;
    491  1.1  riastrad 	pi->hw.vddc[R600_POWER_LEVEL_HIGH] = state->high.vddc;
    492  1.1  riastrad 	pi->hw.vddc[R600_POWER_LEVEL_MEDIUM] = state->medium.vddc;
    493  1.1  riastrad 	pi->hw.vddc[R600_POWER_LEVEL_LOW] = state->low.vddc;
    494  1.1  riastrad 
    495  1.1  riastrad 	pi->hw.backbias[R600_POWER_LEVEL_CTXSW] =
    496  1.1  riastrad 		(state->high.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ? true : false;
    497  1.1  riastrad 	pi->hw.backbias[R600_POWER_LEVEL_HIGH] =
    498  1.1  riastrad 		(state->high.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ? true : false;
    499  1.1  riastrad 	pi->hw.backbias[R600_POWER_LEVEL_MEDIUM] =
    500  1.1  riastrad 		(state->medium.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ? true : false;
    501  1.1  riastrad 	pi->hw.backbias[R600_POWER_LEVEL_LOW] =
    502  1.1  riastrad 		(state->low.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ? true : false;
    503  1.1  riastrad 
    504  1.1  riastrad 	pi->hw.pcie_gen2[R600_POWER_LEVEL_HIGH] =
    505  1.1  riastrad 		(state->high.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? true : false;
    506  1.1  riastrad 	pi->hw.pcie_gen2[R600_POWER_LEVEL_MEDIUM] =
    507  1.1  riastrad 		(state->medium.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? true : false;
    508  1.1  riastrad 	pi->hw.pcie_gen2[R600_POWER_LEVEL_LOW] =
    509  1.1  riastrad 		(state->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? true : false;
    510  1.1  riastrad 
    511  1.1  riastrad 	pi->hw.high_vddc_index = R600_POWER_LEVEL_HIGH;
    512  1.1  riastrad 
    513  1.1  riastrad 	if ((state->high.vddc == state->medium.vddc) &&
    514  1.1  riastrad 	    ((state->high.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ==
    515  1.1  riastrad 	     (state->medium.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE)))
    516  1.1  riastrad 		pi->hw.medium_vddc_index =
    517  1.1  riastrad 			pi->hw.high_vddc_index;
    518  1.1  riastrad 	else
    519  1.1  riastrad 		pi->hw.medium_vddc_index = R600_POWER_LEVEL_MEDIUM;
    520  1.1  riastrad 
    521  1.1  riastrad 	if ((state->medium.vddc == state->low.vddc) &&
    522  1.1  riastrad 	    ((state->medium.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) ==
    523  1.1  riastrad 	     (state->low.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE)))
    524  1.1  riastrad 		pi->hw.low_vddc_index =
    525  1.1  riastrad 			pi->hw.medium_vddc_index;
    526  1.1  riastrad 	else
    527  1.1  riastrad 		pi->hw.medium_vddc_index = R600_POWER_LEVEL_LOW;
    528  1.1  riastrad }
    529  1.1  riastrad 
    530  1.1  riastrad static inline u32 rv6xx_calculate_vco_frequency(u32 ref_clock,
    531  1.1  riastrad 						struct atom_clock_dividers *dividers,
    532  1.1  riastrad 						u32 fb_divider_scale)
    533  1.1  riastrad {
    534  1.1  riastrad 	return ref_clock * ((dividers->fb_div & ~1) << fb_divider_scale) /
    535  1.1  riastrad 		(dividers->ref_div + 1);
    536  1.1  riastrad }
    537  1.1  riastrad 
    538  1.1  riastrad static inline u32 rv6xx_calculate_spread_spectrum_clk_v(u32 vco_freq, u32 ref_freq,
    539  1.1  riastrad 							u32 ss_rate, u32 ss_percent,
    540  1.1  riastrad 							u32 fb_divider_scale)
    541  1.1  riastrad {
    542  1.1  riastrad 	u32 fb_divider = vco_freq / ref_freq;
    543  1.1  riastrad 
    544  1.1  riastrad 	return (ss_percent * ss_rate * 4 * (fb_divider * fb_divider) /
    545  1.1  riastrad 		(5375 * ((vco_freq * 10) / (4096 >> fb_divider_scale))));
    546  1.1  riastrad }
    547  1.1  riastrad 
    548  1.1  riastrad static inline u32 rv6xx_calculate_spread_spectrum_clk_s(u32 ss_rate, u32 ref_freq)
    549  1.1  riastrad {
    550  1.1  riastrad 	return (((ref_freq * 10) / (ss_rate * 2)) - 1) / 4;
    551  1.1  riastrad }
    552  1.1  riastrad 
    553  1.1  riastrad static void rv6xx_program_engine_spread_spectrum(struct radeon_device *rdev,
    554  1.1  riastrad 						 u32 clock, enum r600_power_level level)
    555  1.1  riastrad {
    556  1.1  riastrad 	u32 ref_clk = rdev->clock.spll.reference_freq;
    557  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    558  1.1  riastrad 	struct atom_clock_dividers dividers;
    559  1.1  riastrad 	struct radeon_atom_ss ss;
    560  1.1  riastrad 	u32 vco_freq, clk_v, clk_s;
    561  1.1  riastrad 
    562  1.1  riastrad 	rv6xx_enable_engine_spread_spectrum(rdev, level, false);
    563  1.1  riastrad 
    564  1.1  riastrad 	if (clock && pi->sclk_ss) {
    565  1.1  riastrad 		if (radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM, clock, false, &dividers) == 0) {
    566  1.1  riastrad 			vco_freq = rv6xx_calculate_vco_frequency(ref_clk, &dividers,
    567  1.1  riastrad 								 pi->fb_div_scale);
    568  1.1  riastrad 
    569  1.1  riastrad 			if (radeon_atombios_get_asic_ss_info(rdev, &ss,
    570  1.1  riastrad 							     ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
    571  1.1  riastrad 				clk_v = rv6xx_calculate_spread_spectrum_clk_v(vco_freq,
    572  1.1  riastrad 									      (ref_clk / (dividers.ref_div + 1)),
    573  1.1  riastrad 									      ss.rate,
    574  1.1  riastrad 									      ss.percentage,
    575  1.1  riastrad 									      pi->fb_div_scale);
    576  1.1  riastrad 
    577  1.1  riastrad 				clk_s = rv6xx_calculate_spread_spectrum_clk_s(ss.rate,
    578  1.1  riastrad 									      (ref_clk / (dividers.ref_div + 1)));
    579  1.1  riastrad 
    580  1.1  riastrad 				rv6xx_set_engine_spread_spectrum_clk_v(rdev, level, clk_v);
    581  1.1  riastrad 				rv6xx_set_engine_spread_spectrum_clk_s(rdev, level, clk_s);
    582  1.1  riastrad 				rv6xx_enable_engine_spread_spectrum(rdev, level, true);
    583  1.1  riastrad 			}
    584  1.1  riastrad 		}
    585  1.1  riastrad 	}
    586  1.1  riastrad }
    587  1.1  riastrad 
    588  1.1  riastrad static void rv6xx_program_sclk_spread_spectrum_parameters_except_lowest_entry(struct radeon_device *rdev)
    589  1.1  riastrad {
    590  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    591  1.1  riastrad 
    592  1.1  riastrad 	rv6xx_program_engine_spread_spectrum(rdev,
    593  1.1  riastrad 					     pi->hw.sclks[R600_POWER_LEVEL_HIGH],
    594  1.1  riastrad 					     R600_POWER_LEVEL_HIGH);
    595  1.1  riastrad 
    596  1.1  riastrad 	rv6xx_program_engine_spread_spectrum(rdev,
    597  1.1  riastrad 					     pi->hw.sclks[R600_POWER_LEVEL_MEDIUM],
    598  1.1  riastrad 					     R600_POWER_LEVEL_MEDIUM);
    599  1.1  riastrad 
    600  1.1  riastrad }
    601  1.1  riastrad 
    602  1.1  riastrad static int rv6xx_program_mclk_stepping_entry(struct radeon_device *rdev,
    603  1.1  riastrad 					     u32 entry, u32 clock)
    604  1.1  riastrad {
    605  1.1  riastrad 	struct atom_clock_dividers dividers;
    606  1.1  riastrad 
    607  1.1  riastrad 	if (radeon_atom_get_clock_dividers(rdev, COMPUTE_MEMORY_PLL_PARAM, clock, false, &dividers))
    608  1.1  riastrad 	    return -EINVAL;
    609  1.1  riastrad 
    610  1.1  riastrad 
    611  1.1  riastrad 	rv6xx_memory_clock_entry_set_reference_divider(rdev, entry, dividers.ref_div);
    612  1.1  riastrad 	rv6xx_memory_clock_entry_set_feedback_divider(rdev, entry, dividers.fb_div);
    613  1.1  riastrad 	rv6xx_memory_clock_entry_set_post_divider(rdev, entry, dividers.post_div);
    614  1.1  riastrad 
    615  1.1  riastrad 	if (dividers.enable_post_div)
    616  1.1  riastrad 		rv6xx_memory_clock_entry_enable_post_divider(rdev, entry, true);
    617  1.1  riastrad 	else
    618  1.1  riastrad 		rv6xx_memory_clock_entry_enable_post_divider(rdev, entry, false);
    619  1.1  riastrad 
    620  1.1  riastrad 	return 0;
    621  1.1  riastrad }
    622  1.1  riastrad 
    623  1.1  riastrad static void rv6xx_program_mclk_stepping_parameters_except_lowest_entry(struct radeon_device *rdev)
    624  1.1  riastrad {
    625  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    626  1.1  riastrad 	int i;
    627  1.1  riastrad 
    628  1.1  riastrad 	for (i = 1; i < R600_PM_NUMBER_OF_MCLKS; i++) {
    629  1.1  riastrad 		if (pi->hw.mclks[i])
    630  1.1  riastrad 			rv6xx_program_mclk_stepping_entry(rdev, i,
    631  1.1  riastrad 							  pi->hw.mclks[i]);
    632  1.1  riastrad 	}
    633  1.1  riastrad }
    634  1.1  riastrad 
    635  1.1  riastrad static void rv6xx_find_memory_clock_with_highest_vco(struct radeon_device *rdev,
    636  1.1  riastrad 						     u32 requested_memory_clock,
    637  1.1  riastrad 						     u32 ref_clk,
    638  1.1  riastrad 						     struct atom_clock_dividers *dividers,
    639  1.1  riastrad 						     u32 *vco_freq)
    640  1.1  riastrad {
    641  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    642  1.1  riastrad 	struct atom_clock_dividers req_dividers;
    643  1.1  riastrad 	u32 vco_freq_temp;
    644  1.1  riastrad 
    645  1.1  riastrad 	if (radeon_atom_get_clock_dividers(rdev, COMPUTE_MEMORY_PLL_PARAM,
    646  1.1  riastrad 					   requested_memory_clock, false, &req_dividers) == 0) {
    647  1.1  riastrad 		vco_freq_temp = rv6xx_calculate_vco_frequency(ref_clk, &req_dividers,
    648  1.1  riastrad 							      pi->fb_div_scale);
    649  1.1  riastrad 
    650  1.1  riastrad 		if (vco_freq_temp > *vco_freq) {
    651  1.1  riastrad 			*dividers = req_dividers;
    652  1.1  riastrad 			*vco_freq = vco_freq_temp;
    653  1.1  riastrad 		}
    654  1.1  riastrad 	}
    655  1.1  riastrad }
    656  1.1  riastrad 
    657  1.1  riastrad static void rv6xx_program_mclk_spread_spectrum_parameters(struct radeon_device *rdev)
    658  1.1  riastrad {
    659  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    660  1.1  riastrad 	u32 ref_clk = rdev->clock.mpll.reference_freq;
    661  1.1  riastrad 	struct atom_clock_dividers dividers;
    662  1.1  riastrad 	struct radeon_atom_ss ss;
    663  1.1  riastrad 	u32 vco_freq = 0, clk_v, clk_s;
    664  1.1  riastrad 
    665  1.1  riastrad 	rv6xx_enable_memory_spread_spectrum(rdev, false);
    666  1.1  riastrad 
    667  1.1  riastrad 	if (pi->mclk_ss) {
    668  1.1  riastrad 		rv6xx_find_memory_clock_with_highest_vco(rdev,
    669  1.1  riastrad 							 pi->hw.mclks[pi->hw.high_mclk_index],
    670  1.1  riastrad 							 ref_clk,
    671  1.1  riastrad 							 &dividers,
    672  1.1  riastrad 							 &vco_freq);
    673  1.1  riastrad 
    674  1.1  riastrad 		rv6xx_find_memory_clock_with_highest_vco(rdev,
    675  1.1  riastrad 							 pi->hw.mclks[pi->hw.medium_mclk_index],
    676  1.1  riastrad 							 ref_clk,
    677  1.1  riastrad 							 &dividers,
    678  1.1  riastrad 							 &vco_freq);
    679  1.1  riastrad 
    680  1.1  riastrad 		rv6xx_find_memory_clock_with_highest_vco(rdev,
    681  1.1  riastrad 							 pi->hw.mclks[pi->hw.low_mclk_index],
    682  1.1  riastrad 							 ref_clk,
    683  1.1  riastrad 							 &dividers,
    684  1.1  riastrad 							 &vco_freq);
    685  1.1  riastrad 
    686  1.1  riastrad 		if (vco_freq) {
    687  1.1  riastrad 			if (radeon_atombios_get_asic_ss_info(rdev, &ss,
    688  1.1  riastrad 							     ASIC_INTERNAL_MEMORY_SS, vco_freq)) {
    689  1.1  riastrad 				clk_v = rv6xx_calculate_spread_spectrum_clk_v(vco_freq,
    690  1.1  riastrad 									     (ref_clk / (dividers.ref_div + 1)),
    691  1.1  riastrad 									     ss.rate,
    692  1.1  riastrad 									     ss.percentage,
    693  1.1  riastrad 									     pi->fb_div_scale);
    694  1.1  riastrad 
    695  1.1  riastrad 				clk_s = rv6xx_calculate_spread_spectrum_clk_s(ss.rate,
    696  1.1  riastrad 									     (ref_clk / (dividers.ref_div + 1)));
    697  1.1  riastrad 
    698  1.1  riastrad 				rv6xx_set_memory_spread_spectrum_clk_v(rdev, clk_v);
    699  1.1  riastrad 				rv6xx_set_memory_spread_spectrum_clk_s(rdev, clk_s);
    700  1.1  riastrad 				rv6xx_enable_memory_spread_spectrum(rdev, true);
    701  1.1  riastrad 			}
    702  1.1  riastrad 		}
    703  1.1  riastrad 	}
    704  1.1  riastrad }
    705  1.1  riastrad 
    706  1.1  riastrad static int rv6xx_program_voltage_stepping_entry(struct radeon_device *rdev,
    707  1.1  riastrad 						u32 entry, u16 voltage)
    708  1.1  riastrad {
    709  1.1  riastrad 	u32 mask, set_pins;
    710  1.1  riastrad 	int ret;
    711  1.1  riastrad 
    712  1.1  riastrad 	ret = radeon_atom_get_voltage_gpio_settings(rdev, voltage,
    713  1.1  riastrad 						    SET_VOLTAGE_TYPE_ASIC_VDDC,
    714  1.1  riastrad 						    &set_pins, &mask);
    715  1.1  riastrad 	if (ret)
    716  1.1  riastrad 		return ret;
    717  1.1  riastrad 
    718  1.1  riastrad 	r600_voltage_control_program_voltages(rdev, entry, set_pins);
    719  1.1  riastrad 
    720  1.1  riastrad 	return 0;
    721  1.1  riastrad }
    722  1.1  riastrad 
    723  1.1  riastrad static void rv6xx_program_voltage_stepping_parameters_except_lowest_entry(struct radeon_device *rdev)
    724  1.1  riastrad {
    725  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    726  1.1  riastrad 	int i;
    727  1.1  riastrad 
    728  1.1  riastrad 	for (i = 1; i < R600_PM_NUMBER_OF_VOLTAGE_LEVELS; i++)
    729  1.1  riastrad 		rv6xx_program_voltage_stepping_entry(rdev, i,
    730  1.1  riastrad 						     pi->hw.vddc[i]);
    731  1.1  riastrad 
    732  1.1  riastrad }
    733  1.1  riastrad 
    734  1.1  riastrad static void rv6xx_program_backbias_stepping_parameters_except_lowest_entry(struct radeon_device *rdev)
    735  1.1  riastrad {
    736  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    737  1.1  riastrad 
    738  1.1  riastrad 	if (pi->hw.backbias[1])
    739  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, MEDIUM_BACKBIAS_VALUE, ~MEDIUM_BACKBIAS_VALUE);
    740  1.1  riastrad 	else
    741  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, 0, ~MEDIUM_BACKBIAS_VALUE);
    742  1.1  riastrad 
    743  1.1  riastrad 	if (pi->hw.backbias[2])
    744  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, HIGH_BACKBIAS_VALUE, ~HIGH_BACKBIAS_VALUE);
    745  1.1  riastrad 	else
    746  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, 0, ~HIGH_BACKBIAS_VALUE);
    747  1.1  riastrad }
    748  1.1  riastrad 
    749  1.1  riastrad static void rv6xx_program_sclk_spread_spectrum_parameters_lowest_entry(struct radeon_device *rdev)
    750  1.1  riastrad {
    751  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    752  1.1  riastrad 
    753  1.1  riastrad 	rv6xx_program_engine_spread_spectrum(rdev,
    754  1.1  riastrad 					     pi->hw.sclks[R600_POWER_LEVEL_LOW],
    755  1.1  riastrad 					     R600_POWER_LEVEL_LOW);
    756  1.1  riastrad }
    757  1.1  riastrad 
    758  1.1  riastrad static void rv6xx_program_mclk_stepping_parameters_lowest_entry(struct radeon_device *rdev)
    759  1.1  riastrad {
    760  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    761  1.1  riastrad 
    762  1.1  riastrad 	if (pi->hw.mclks[0])
    763  1.1  riastrad 		rv6xx_program_mclk_stepping_entry(rdev, 0,
    764  1.1  riastrad 						  pi->hw.mclks[0]);
    765  1.1  riastrad }
    766  1.1  riastrad 
    767  1.1  riastrad static void rv6xx_program_voltage_stepping_parameters_lowest_entry(struct radeon_device *rdev)
    768  1.1  riastrad {
    769  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    770  1.1  riastrad 
    771  1.1  riastrad 	rv6xx_program_voltage_stepping_entry(rdev, 0,
    772  1.1  riastrad 					     pi->hw.vddc[0]);
    773  1.1  riastrad 
    774  1.1  riastrad }
    775  1.1  riastrad 
    776  1.1  riastrad static void rv6xx_program_backbias_stepping_parameters_lowest_entry(struct radeon_device *rdev)
    777  1.1  riastrad {
    778  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    779  1.1  riastrad 
    780  1.1  riastrad 	if (pi->hw.backbias[0])
    781  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, LOW_BACKBIAS_VALUE, ~LOW_BACKBIAS_VALUE);
    782  1.1  riastrad 	else
    783  1.1  riastrad 		WREG32_P(VID_UPPER_GPIO_CNTL, 0, ~LOW_BACKBIAS_VALUE);
    784  1.1  riastrad }
    785  1.1  riastrad 
    786  1.1  riastrad static u32 calculate_memory_refresh_rate(struct radeon_device *rdev,
    787  1.1  riastrad 					 u32 engine_clock)
    788  1.1  riastrad {
    789  1.1  riastrad 	u32 dram_rows, dram_refresh_rate;
    790  1.1  riastrad 	u32 tmp;
    791  1.1  riastrad 
    792  1.1  riastrad 	tmp = (RREG32(RAMCFG) & NOOFROWS_MASK) >> NOOFROWS_SHIFT;
    793  1.1  riastrad 	dram_rows = 1 << (tmp + 10);
    794  1.1  riastrad 	dram_refresh_rate = 1 << ((RREG32(MC_SEQ_RESERVE_M) & 0x3) + 3);
    795  1.1  riastrad 
    796  1.1  riastrad 	return ((engine_clock * 10) * dram_refresh_rate / dram_rows - 32) / 64;
    797  1.1  riastrad }
    798  1.1  riastrad 
    799  1.1  riastrad static void rv6xx_program_memory_timing_parameters(struct radeon_device *rdev)
    800  1.1  riastrad {
    801  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    802  1.1  riastrad 	u32 sqm_ratio;
    803  1.1  riastrad 	u32 arb_refresh_rate;
    804  1.1  riastrad 	u32 high_clock;
    805  1.1  riastrad 
    806  1.1  riastrad 	if (pi->hw.sclks[R600_POWER_LEVEL_HIGH] <
    807  1.1  riastrad 	    (pi->hw.sclks[R600_POWER_LEVEL_LOW] * 0xFF / 0x40))
    808  1.1  riastrad 		high_clock = pi->hw.sclks[R600_POWER_LEVEL_HIGH];
    809  1.1  riastrad 	else
    810  1.1  riastrad 		high_clock =
    811  1.1  riastrad 			pi->hw.sclks[R600_POWER_LEVEL_LOW] * 0xFF / 0x40;
    812  1.1  riastrad 
    813  1.1  riastrad 	radeon_atom_set_engine_dram_timings(rdev, high_clock, 0);
    814  1.1  riastrad 
    815  1.1  riastrad 	sqm_ratio = (STATE0(64 * high_clock / pi->hw.sclks[R600_POWER_LEVEL_LOW]) |
    816  1.1  riastrad 		     STATE1(64 * high_clock / pi->hw.sclks[R600_POWER_LEVEL_MEDIUM]) |
    817  1.1  riastrad 		     STATE2(64 * high_clock / pi->hw.sclks[R600_POWER_LEVEL_HIGH]) |
    818  1.1  riastrad 		     STATE3(64 * high_clock / pi->hw.sclks[R600_POWER_LEVEL_HIGH]));
    819  1.1  riastrad 	WREG32(SQM_RATIO, sqm_ratio);
    820  1.1  riastrad 
    821  1.1  riastrad 	arb_refresh_rate =
    822  1.1  riastrad 		(POWERMODE0(calculate_memory_refresh_rate(rdev,
    823  1.1  riastrad 							  pi->hw.sclks[R600_POWER_LEVEL_LOW])) |
    824  1.1  riastrad 		 POWERMODE1(calculate_memory_refresh_rate(rdev,
    825  1.1  riastrad 							  pi->hw.sclks[R600_POWER_LEVEL_MEDIUM])) |
    826  1.1  riastrad 		 POWERMODE2(calculate_memory_refresh_rate(rdev,
    827  1.1  riastrad 							  pi->hw.sclks[R600_POWER_LEVEL_HIGH])) |
    828  1.1  riastrad 		 POWERMODE3(calculate_memory_refresh_rate(rdev,
    829  1.1  riastrad 							  pi->hw.sclks[R600_POWER_LEVEL_HIGH])));
    830  1.1  riastrad 	WREG32(ARB_RFSH_RATE, arb_refresh_rate);
    831  1.1  riastrad }
    832  1.1  riastrad 
    833  1.1  riastrad static void rv6xx_program_mpll_timing_parameters(struct radeon_device *rdev)
    834  1.1  riastrad {
    835  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    836  1.1  riastrad 
    837  1.1  riastrad 	r600_set_mpll_lock_time(rdev, R600_MPLLLOCKTIME_DFLT *
    838  1.1  riastrad 				pi->mpll_ref_div);
    839  1.1  riastrad 	r600_set_mpll_reset_time(rdev, R600_MPLLRESETTIME_DFLT);
    840  1.1  riastrad }
    841  1.1  riastrad 
    842  1.1  riastrad static void rv6xx_program_bsp(struct radeon_device *rdev)
    843  1.1  riastrad {
    844  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    845  1.1  riastrad 	u32 ref_clk = rdev->clock.spll.reference_freq;
    846  1.1  riastrad 
    847  1.1  riastrad 	r600_calculate_u_and_p(R600_ASI_DFLT,
    848  1.1  riastrad 			       ref_clk, 16,
    849  1.1  riastrad 			       &pi->bsp,
    850  1.1  riastrad 			       &pi->bsu);
    851  1.1  riastrad 
    852  1.1  riastrad 	r600_set_bsp(rdev, pi->bsu, pi->bsp);
    853  1.1  riastrad }
    854  1.1  riastrad 
    855  1.1  riastrad static void rv6xx_program_at(struct radeon_device *rdev)
    856  1.1  riastrad {
    857  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    858  1.1  riastrad 
    859  1.1  riastrad 	r600_set_at(rdev,
    860  1.1  riastrad 		    (pi->hw.rp[0] * pi->bsp) / 200,
    861  1.1  riastrad 		    (pi->hw.rp[1] * pi->bsp) / 200,
    862  1.1  riastrad 		    (pi->hw.lp[2] * pi->bsp) / 200,
    863  1.1  riastrad 		    (pi->hw.lp[1] * pi->bsp) / 200);
    864  1.1  riastrad }
    865  1.1  riastrad 
    866  1.1  riastrad static void rv6xx_program_git(struct radeon_device *rdev)
    867  1.1  riastrad {
    868  1.1  riastrad 	r600_set_git(rdev, R600_GICST_DFLT);
    869  1.1  riastrad }
    870  1.1  riastrad 
    871  1.1  riastrad static void rv6xx_program_tp(struct radeon_device *rdev)
    872  1.1  riastrad {
    873  1.1  riastrad 	int i;
    874  1.1  riastrad 
    875  1.1  riastrad 	for (i = 0; i < R600_PM_NUMBER_OF_TC; i++)
    876  1.1  riastrad 		r600_set_tc(rdev, i, r600_utc[i], r600_dtc[i]);
    877  1.1  riastrad 
    878  1.1  riastrad 	r600_select_td(rdev, R600_TD_DFLT);
    879  1.1  riastrad }
    880  1.1  riastrad 
    881  1.1  riastrad static void rv6xx_program_vc(struct radeon_device *rdev)
    882  1.1  riastrad {
    883  1.1  riastrad 	r600_set_vrc(rdev, R600_VRC_DFLT);
    884  1.1  riastrad }
    885  1.1  riastrad 
    886  1.1  riastrad static void rv6xx_clear_vc(struct radeon_device *rdev)
    887  1.1  riastrad {
    888  1.1  riastrad 	r600_set_vrc(rdev, 0);
    889  1.1  riastrad }
    890  1.1  riastrad 
    891  1.1  riastrad static void rv6xx_program_tpp(struct radeon_device *rdev)
    892  1.1  riastrad {
    893  1.1  riastrad 	r600_set_tpu(rdev, R600_TPU_DFLT);
    894  1.1  riastrad 	r600_set_tpc(rdev, R600_TPC_DFLT);
    895  1.1  riastrad }
    896  1.1  riastrad 
    897  1.1  riastrad static void rv6xx_program_sstp(struct radeon_device *rdev)
    898  1.1  riastrad {
    899  1.1  riastrad 	r600_set_sstu(rdev, R600_SSTU_DFLT);
    900  1.1  riastrad 	r600_set_sst(rdev, R600_SST_DFLT);
    901  1.1  riastrad }
    902  1.1  riastrad 
    903  1.1  riastrad static void rv6xx_program_fcp(struct radeon_device *rdev)
    904  1.1  riastrad {
    905  1.1  riastrad 	r600_set_fctu(rdev, R600_FCTU_DFLT);
    906  1.1  riastrad 	r600_set_fct(rdev, R600_FCT_DFLT);
    907  1.1  riastrad }
    908  1.1  riastrad 
    909  1.1  riastrad static void rv6xx_program_vddc3d_parameters(struct radeon_device *rdev)
    910  1.1  riastrad {
    911  1.1  riastrad 	r600_set_vddc3d_oorsu(rdev, R600_VDDC3DOORSU_DFLT);
    912  1.1  riastrad 	r600_set_vddc3d_oorphc(rdev, R600_VDDC3DOORPHC_DFLT);
    913  1.1  riastrad 	r600_set_vddc3d_oorsdc(rdev, R600_VDDC3DOORSDC_DFLT);
    914  1.1  riastrad 	r600_set_ctxcgtt3d_rphc(rdev, R600_CTXCGTT3DRPHC_DFLT);
    915  1.1  riastrad 	r600_set_ctxcgtt3d_rsdc(rdev, R600_CTXCGTT3DRSDC_DFLT);
    916  1.1  riastrad }
    917  1.1  riastrad 
    918  1.1  riastrad static void rv6xx_program_voltage_timing_parameters(struct radeon_device *rdev)
    919  1.1  riastrad {
    920  1.1  riastrad 	u32 rt;
    921  1.1  riastrad 
    922  1.1  riastrad 	r600_vid_rt_set_vru(rdev, R600_VRU_DFLT);
    923  1.1  riastrad 
    924  1.1  riastrad 	r600_vid_rt_set_vrt(rdev,
    925  1.1  riastrad 			    rv6xx_compute_count_for_delay(rdev,
    926  1.1  riastrad 							  rdev->pm.dpm.voltage_response_time,
    927  1.1  riastrad 							  R600_VRU_DFLT));
    928  1.1  riastrad 
    929  1.1  riastrad 	rt = rv6xx_compute_count_for_delay(rdev,
    930  1.1  riastrad 					   rdev->pm.dpm.backbias_response_time,
    931  1.1  riastrad 					   R600_VRU_DFLT);
    932  1.1  riastrad 
    933  1.1  riastrad 	rv6xx_vid_response_set_brt(rdev, (rt + 0x1F) >> 5);
    934  1.1  riastrad }
    935  1.1  riastrad 
    936  1.1  riastrad static void rv6xx_program_engine_speed_parameters(struct radeon_device *rdev)
    937  1.1  riastrad {
    938  1.1  riastrad 	r600_vid_rt_set_ssu(rdev, R600_SPLLSTEPUNIT_DFLT);
    939  1.1  riastrad 	rv6xx_enable_engine_feedback_and_reference_sync(rdev);
    940  1.1  riastrad }
    941  1.1  riastrad 
    942  1.1  riastrad static u64 rv6xx_get_master_voltage_mask(struct radeon_device *rdev)
    943  1.1  riastrad {
    944  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
    945  1.1  riastrad 	u64 master_mask = 0;
    946  1.1  riastrad 	int i;
    947  1.1  riastrad 
    948  1.1  riastrad 	for (i = 0; i < R600_PM_NUMBER_OF_VOLTAGE_LEVELS; i++) {
    949  1.1  riastrad 		u32 tmp_mask, tmp_set_pins;
    950  1.1  riastrad 		int ret;
    951  1.1  riastrad 
    952  1.1  riastrad 		ret = radeon_atom_get_voltage_gpio_settings(rdev,
    953  1.1  riastrad 							    pi->hw.vddc[i],
    954  1.1  riastrad 							    SET_VOLTAGE_TYPE_ASIC_VDDC,
    955  1.1  riastrad 							    &tmp_set_pins, &tmp_mask);
    956  1.1  riastrad 
    957  1.1  riastrad 		if (ret == 0)
    958  1.1  riastrad 			master_mask |= tmp_mask;
    959  1.1  riastrad 	}
    960  1.1  riastrad 
    961  1.1  riastrad 	return master_mask;
    962  1.1  riastrad }
    963  1.1  riastrad 
    964  1.1  riastrad static void rv6xx_program_voltage_gpio_pins(struct radeon_device *rdev)
    965  1.1  riastrad {
    966  1.1  riastrad 	r600_voltage_control_enable_pins(rdev,
    967  1.1  riastrad 					 rv6xx_get_master_voltage_mask(rdev));
    968  1.1  riastrad }
    969  1.1  riastrad 
    970  1.1  riastrad static void rv6xx_enable_static_voltage_control(struct radeon_device *rdev,
    971  1.1  riastrad 						struct radeon_ps *new_ps,
    972  1.1  riastrad 						bool enable)
    973  1.1  riastrad {
    974  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
    975  1.1  riastrad 
    976  1.1  riastrad 	if (enable)
    977  1.1  riastrad 		radeon_atom_set_voltage(rdev,
    978  1.1  riastrad 					new_state->low.vddc,
    979  1.1  riastrad 					SET_VOLTAGE_TYPE_ASIC_VDDC);
    980  1.1  riastrad 	else
    981  1.1  riastrad 		r600_voltage_control_deactivate_static_control(rdev,
    982  1.1  riastrad 							       rv6xx_get_master_voltage_mask(rdev));
    983  1.1  riastrad }
    984  1.1  riastrad 
    985  1.1  riastrad static void rv6xx_enable_display_gap(struct radeon_device *rdev, bool enable)
    986  1.1  riastrad {
    987  1.1  riastrad 	if (enable) {
    988  1.1  riastrad 		u32 tmp = (DISP1_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM) |
    989  1.1  riastrad 			   DISP2_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM) |
    990  1.1  riastrad 			   DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE) |
    991  1.1  riastrad 			   DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE) |
    992  1.1  riastrad 			   VBI_TIMER_COUNT(0x3FFF) |
    993  1.1  riastrad 			   VBI_TIMER_UNIT(7));
    994  1.1  riastrad 		WREG32(CG_DISPLAY_GAP_CNTL, tmp);
    995  1.1  riastrad 
    996  1.1  riastrad 		WREG32_P(MCLK_PWRMGT_CNTL, USE_DISPLAY_GAP, ~USE_DISPLAY_GAP);
    997  1.1  riastrad 	} else
    998  1.1  riastrad 		WREG32_P(MCLK_PWRMGT_CNTL, 0, ~USE_DISPLAY_GAP);
    999  1.1  riastrad }
   1000  1.1  riastrad 
   1001  1.1  riastrad static void rv6xx_program_power_level_enter_state(struct radeon_device *rdev)
   1002  1.1  riastrad {
   1003  1.1  riastrad 	r600_power_level_set_enter_index(rdev, R600_POWER_LEVEL_MEDIUM);
   1004  1.1  riastrad }
   1005  1.1  riastrad 
   1006  1.1  riastrad static void rv6xx_calculate_t(u32 l_f, u32 h_f, int h,
   1007  1.1  riastrad 			      int d_l, int d_r, u8 *l, u8 *r)
   1008  1.1  riastrad {
   1009  1.1  riastrad 	int a_n, a_d, h_r, l_r;
   1010  1.1  riastrad 
   1011  1.1  riastrad 	h_r = d_l;
   1012  1.1  riastrad 	l_r = 100 - d_r;
   1013  1.1  riastrad 
   1014  1.1  riastrad 	a_n = (int)h_f * d_l + (int)l_f * (h - d_r);
   1015  1.1  riastrad 	a_d = (int)l_f * l_r + (int)h_f * h_r;
   1016  1.1  riastrad 
   1017  1.1  riastrad 	if (a_d != 0) {
   1018  1.1  riastrad 		*l = d_l - h_r * a_n / a_d;
   1019  1.1  riastrad 		*r = d_r + l_r * a_n / a_d;
   1020  1.1  riastrad 	}
   1021  1.1  riastrad }
   1022  1.1  riastrad 
   1023  1.1  riastrad static void rv6xx_calculate_ap(struct radeon_device *rdev,
   1024  1.1  riastrad 			       struct rv6xx_ps *state)
   1025  1.1  riastrad {
   1026  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1027  1.1  riastrad 
   1028  1.1  riastrad 	pi->hw.lp[0] = 0;
   1029  1.1  riastrad 	pi->hw.rp[R600_PM_NUMBER_OF_ACTIVITY_LEVELS - 1]
   1030  1.1  riastrad 		= 100;
   1031  1.1  riastrad 
   1032  1.1  riastrad 	rv6xx_calculate_t(state->low.sclk,
   1033  1.1  riastrad 			  state->medium.sclk,
   1034  1.1  riastrad 			  R600_AH_DFLT,
   1035  1.1  riastrad 			  R600_LMP_DFLT,
   1036  1.1  riastrad 			  R600_RLP_DFLT,
   1037  1.1  riastrad 			  &pi->hw.lp[1],
   1038  1.1  riastrad 			  &pi->hw.rp[0]);
   1039  1.1  riastrad 
   1040  1.1  riastrad 	rv6xx_calculate_t(state->medium.sclk,
   1041  1.1  riastrad 			  state->high.sclk,
   1042  1.1  riastrad 			  R600_AH_DFLT,
   1043  1.1  riastrad 			  R600_LHP_DFLT,
   1044  1.1  riastrad 			  R600_RMP_DFLT,
   1045  1.1  riastrad 			  &pi->hw.lp[2],
   1046  1.1  riastrad 			  &pi->hw.rp[1]);
   1047  1.1  riastrad 
   1048  1.1  riastrad }
   1049  1.1  riastrad 
   1050  1.1  riastrad static void rv6xx_calculate_stepping_parameters(struct radeon_device *rdev,
   1051  1.1  riastrad 						struct radeon_ps *new_ps)
   1052  1.1  riastrad {
   1053  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1054  1.1  riastrad 
   1055  1.1  riastrad 	rv6xx_calculate_engine_speed_stepping_parameters(rdev, new_state);
   1056  1.1  riastrad 	rv6xx_calculate_memory_clock_stepping_parameters(rdev, new_state);
   1057  1.1  riastrad 	rv6xx_calculate_voltage_stepping_parameters(rdev, new_state);
   1058  1.1  riastrad 	rv6xx_calculate_ap(rdev, new_state);
   1059  1.1  riastrad }
   1060  1.1  riastrad 
   1061  1.1  riastrad static void rv6xx_program_stepping_parameters_except_lowest_entry(struct radeon_device *rdev)
   1062  1.1  riastrad {
   1063  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1064  1.1  riastrad 
   1065  1.1  riastrad 	rv6xx_program_mclk_stepping_parameters_except_lowest_entry(rdev);
   1066  1.1  riastrad 	if (pi->voltage_control)
   1067  1.1  riastrad 		rv6xx_program_voltage_stepping_parameters_except_lowest_entry(rdev);
   1068  1.1  riastrad 	rv6xx_program_backbias_stepping_parameters_except_lowest_entry(rdev);
   1069  1.1  riastrad 	rv6xx_program_sclk_spread_spectrum_parameters_except_lowest_entry(rdev);
   1070  1.1  riastrad 	rv6xx_program_mclk_spread_spectrum_parameters(rdev);
   1071  1.1  riastrad 	rv6xx_program_memory_timing_parameters(rdev);
   1072  1.1  riastrad }
   1073  1.1  riastrad 
   1074  1.1  riastrad static void rv6xx_program_stepping_parameters_lowest_entry(struct radeon_device *rdev)
   1075  1.1  riastrad {
   1076  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1077  1.1  riastrad 
   1078  1.1  riastrad 	rv6xx_program_mclk_stepping_parameters_lowest_entry(rdev);
   1079  1.1  riastrad 	if (pi->voltage_control)
   1080  1.1  riastrad 		rv6xx_program_voltage_stepping_parameters_lowest_entry(rdev);
   1081  1.1  riastrad 	rv6xx_program_backbias_stepping_parameters_lowest_entry(rdev);
   1082  1.1  riastrad 	rv6xx_program_sclk_spread_spectrum_parameters_lowest_entry(rdev);
   1083  1.1  riastrad }
   1084  1.1  riastrad 
   1085  1.1  riastrad static void rv6xx_program_power_level_low(struct radeon_device *rdev)
   1086  1.1  riastrad {
   1087  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1088  1.1  riastrad 
   1089  1.1  riastrad 	r600_power_level_set_voltage_index(rdev, R600_POWER_LEVEL_LOW,
   1090  1.1  riastrad 					   pi->hw.low_vddc_index);
   1091  1.1  riastrad 	r600_power_level_set_mem_clock_index(rdev, R600_POWER_LEVEL_LOW,
   1092  1.1  riastrad 					     pi->hw.low_mclk_index);
   1093  1.1  riastrad 	r600_power_level_set_eng_clock_index(rdev, R600_POWER_LEVEL_LOW,
   1094  1.1  riastrad 					     pi->hw.low_sclk_index);
   1095  1.1  riastrad 	r600_power_level_set_watermark_id(rdev, R600_POWER_LEVEL_LOW,
   1096  1.1  riastrad 					  R600_DISPLAY_WATERMARK_LOW);
   1097  1.1  riastrad 	r600_power_level_set_pcie_gen2(rdev, R600_POWER_LEVEL_LOW,
   1098  1.1  riastrad 				       pi->hw.pcie_gen2[R600_POWER_LEVEL_LOW]);
   1099  1.1  riastrad }
   1100  1.1  riastrad 
   1101  1.1  riastrad static void rv6xx_program_power_level_low_to_lowest_state(struct radeon_device *rdev)
   1102  1.1  riastrad {
   1103  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1104  1.1  riastrad 
   1105  1.1  riastrad 	r600_power_level_set_voltage_index(rdev, R600_POWER_LEVEL_LOW, 0);
   1106  1.1  riastrad 	r600_power_level_set_mem_clock_index(rdev, R600_POWER_LEVEL_LOW, 0);
   1107  1.1  riastrad 	r600_power_level_set_eng_clock_index(rdev, R600_POWER_LEVEL_LOW, 0);
   1108  1.1  riastrad 
   1109  1.1  riastrad 	r600_power_level_set_watermark_id(rdev, R600_POWER_LEVEL_LOW,
   1110  1.1  riastrad 					  R600_DISPLAY_WATERMARK_LOW);
   1111  1.1  riastrad 
   1112  1.1  riastrad 	r600_power_level_set_pcie_gen2(rdev, R600_POWER_LEVEL_LOW,
   1113  1.1  riastrad 				       pi->hw.pcie_gen2[R600_POWER_LEVEL_LOW]);
   1114  1.1  riastrad 
   1115  1.1  riastrad }
   1116  1.1  riastrad 
   1117  1.1  riastrad static void rv6xx_program_power_level_medium(struct radeon_device *rdev)
   1118  1.1  riastrad {
   1119  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1120  1.1  riastrad 
   1121  1.1  riastrad 	r600_power_level_set_voltage_index(rdev, R600_POWER_LEVEL_MEDIUM,
   1122  1.1  riastrad 					  pi->hw.medium_vddc_index);
   1123  1.1  riastrad 	r600_power_level_set_mem_clock_index(rdev, R600_POWER_LEVEL_MEDIUM,
   1124  1.1  riastrad 					    pi->hw.medium_mclk_index);
   1125  1.1  riastrad 	r600_power_level_set_eng_clock_index(rdev, R600_POWER_LEVEL_MEDIUM,
   1126  1.1  riastrad 					    pi->hw.medium_sclk_index);
   1127  1.1  riastrad 	r600_power_level_set_watermark_id(rdev, R600_POWER_LEVEL_MEDIUM,
   1128  1.1  riastrad 					 R600_DISPLAY_WATERMARK_LOW);
   1129  1.1  riastrad 	r600_power_level_set_pcie_gen2(rdev, R600_POWER_LEVEL_MEDIUM,
   1130  1.1  riastrad 				      pi->hw.pcie_gen2[R600_POWER_LEVEL_MEDIUM]);
   1131  1.1  riastrad }
   1132  1.1  riastrad 
   1133  1.1  riastrad static void rv6xx_program_power_level_medium_for_transition(struct radeon_device *rdev)
   1134  1.1  riastrad {
   1135  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1136  1.1  riastrad 
   1137  1.1  riastrad 	rv6xx_program_mclk_stepping_entry(rdev,
   1138  1.1  riastrad 					  R600_POWER_LEVEL_CTXSW,
   1139  1.1  riastrad 					  pi->hw.mclks[pi->hw.low_mclk_index]);
   1140  1.1  riastrad 
   1141  1.1  riastrad 	r600_power_level_set_voltage_index(rdev, R600_POWER_LEVEL_MEDIUM, 1);
   1142  1.1  riastrad 
   1143  1.1  riastrad 	r600_power_level_set_mem_clock_index(rdev, R600_POWER_LEVEL_MEDIUM,
   1144  1.1  riastrad 					     R600_POWER_LEVEL_CTXSW);
   1145  1.1  riastrad 	r600_power_level_set_eng_clock_index(rdev, R600_POWER_LEVEL_MEDIUM,
   1146  1.1  riastrad 					     pi->hw.medium_sclk_index);
   1147  1.1  riastrad 
   1148  1.1  riastrad 	r600_power_level_set_watermark_id(rdev, R600_POWER_LEVEL_MEDIUM,
   1149  1.1  riastrad 					  R600_DISPLAY_WATERMARK_LOW);
   1150  1.1  riastrad 
   1151  1.1  riastrad 	rv6xx_enable_engine_spread_spectrum(rdev, R600_POWER_LEVEL_MEDIUM, false);
   1152  1.1  riastrad 
   1153  1.1  riastrad 	r600_power_level_set_pcie_gen2(rdev, R600_POWER_LEVEL_MEDIUM,
   1154  1.1  riastrad 				       pi->hw.pcie_gen2[R600_POWER_LEVEL_LOW]);
   1155  1.1  riastrad }
   1156  1.1  riastrad 
   1157  1.1  riastrad static void rv6xx_program_power_level_high(struct radeon_device *rdev)
   1158  1.1  riastrad {
   1159  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1160  1.1  riastrad 
   1161  1.1  riastrad 	r600_power_level_set_voltage_index(rdev, R600_POWER_LEVEL_HIGH,
   1162  1.1  riastrad 					   pi->hw.high_vddc_index);
   1163  1.1  riastrad 	r600_power_level_set_mem_clock_index(rdev, R600_POWER_LEVEL_HIGH,
   1164  1.1  riastrad 					     pi->hw.high_mclk_index);
   1165  1.1  riastrad 	r600_power_level_set_eng_clock_index(rdev, R600_POWER_LEVEL_HIGH,
   1166  1.1  riastrad 					     pi->hw.high_sclk_index);
   1167  1.1  riastrad 
   1168  1.1  riastrad 	r600_power_level_set_watermark_id(rdev, R600_POWER_LEVEL_HIGH,
   1169  1.1  riastrad 					  R600_DISPLAY_WATERMARK_HIGH);
   1170  1.1  riastrad 
   1171  1.1  riastrad 	r600_power_level_set_pcie_gen2(rdev, R600_POWER_LEVEL_HIGH,
   1172  1.1  riastrad 				       pi->hw.pcie_gen2[R600_POWER_LEVEL_HIGH]);
   1173  1.1  riastrad }
   1174  1.1  riastrad 
   1175  1.1  riastrad static void rv6xx_enable_backbias(struct radeon_device *rdev, bool enable)
   1176  1.1  riastrad {
   1177  1.1  riastrad 	if (enable)
   1178  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, BACKBIAS_PAD_EN | BACKBIAS_DPM_CNTL,
   1179  1.1  riastrad 			 ~(BACKBIAS_PAD_EN | BACKBIAS_DPM_CNTL));
   1180  1.1  riastrad 	else
   1181  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0,
   1182  1.1  riastrad 			 ~(BACKBIAS_VALUE | BACKBIAS_PAD_EN | BACKBIAS_DPM_CNTL));
   1183  1.1  riastrad }
   1184  1.1  riastrad 
   1185  1.1  riastrad static void rv6xx_program_display_gap(struct radeon_device *rdev)
   1186  1.1  riastrad {
   1187  1.1  riastrad 	u32 tmp = RREG32(CG_DISPLAY_GAP_CNTL);
   1188  1.1  riastrad 
   1189  1.1  riastrad 	tmp &= ~(DISP1_GAP_MCHG_MASK | DISP2_GAP_MCHG_MASK);
   1190  1.1  riastrad 	if (rdev->pm.dpm.new_active_crtcs & 1) {
   1191  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK);
   1192  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1193  1.1  riastrad 	} else if (rdev->pm.dpm.new_active_crtcs & 2) {
   1194  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1195  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK);
   1196  1.1  riastrad 	} else {
   1197  1.1  riastrad 		tmp |= DISP1_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1198  1.1  riastrad 		tmp |= DISP2_GAP_MCHG(R600_PM_DISPLAY_GAP_IGNORE);
   1199  1.1  riastrad 	}
   1200  1.1  riastrad 	WREG32(CG_DISPLAY_GAP_CNTL, tmp);
   1201  1.1  riastrad }
   1202  1.1  riastrad 
   1203  1.1  riastrad static void rv6xx_set_sw_voltage_to_safe(struct radeon_device *rdev,
   1204  1.1  riastrad 					 struct radeon_ps *new_ps,
   1205  1.1  riastrad 					 struct radeon_ps *old_ps)
   1206  1.1  riastrad {
   1207  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1208  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1209  1.1  riastrad 	u16 safe_voltage;
   1210  1.1  riastrad 
   1211  1.1  riastrad 	safe_voltage = (new_state->low.vddc >= old_state->low.vddc) ?
   1212  1.1  riastrad 		new_state->low.vddc : old_state->low.vddc;
   1213  1.1  riastrad 
   1214  1.1  riastrad 	rv6xx_program_voltage_stepping_entry(rdev, R600_POWER_LEVEL_CTXSW,
   1215  1.1  riastrad 					     safe_voltage);
   1216  1.1  riastrad 
   1217  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, SW_GPIO_INDEX(R600_POWER_LEVEL_CTXSW),
   1218  1.1  riastrad 		 ~SW_GPIO_INDEX_MASK);
   1219  1.1  riastrad }
   1220  1.1  riastrad 
   1221  1.1  riastrad static void rv6xx_set_sw_voltage_to_low(struct radeon_device *rdev,
   1222  1.1  riastrad 					struct radeon_ps *old_ps)
   1223  1.1  riastrad {
   1224  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1225  1.1  riastrad 
   1226  1.1  riastrad 	rv6xx_program_voltage_stepping_entry(rdev, R600_POWER_LEVEL_CTXSW,
   1227  1.1  riastrad 					     old_state->low.vddc);
   1228  1.1  riastrad 
   1229  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, SW_GPIO_INDEX(R600_POWER_LEVEL_CTXSW),
   1230  1.1  riastrad 		~SW_GPIO_INDEX_MASK);
   1231  1.1  riastrad }
   1232  1.1  riastrad 
   1233  1.1  riastrad static void rv6xx_set_safe_backbias(struct radeon_device *rdev,
   1234  1.1  riastrad 				    struct radeon_ps *new_ps,
   1235  1.1  riastrad 				    struct radeon_ps *old_ps)
   1236  1.1  riastrad {
   1237  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1238  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1239  1.1  riastrad 
   1240  1.1  riastrad 	if ((new_state->low.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE) &&
   1241  1.1  riastrad 	    (old_state->low.flags & ATOM_PPLIB_R600_FLAGS_BACKBIASENABLE))
   1242  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, BACKBIAS_VALUE, ~BACKBIAS_VALUE);
   1243  1.1  riastrad 	else
   1244  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~BACKBIAS_VALUE);
   1245  1.1  riastrad }
   1246  1.1  riastrad 
   1247  1.1  riastrad static void rv6xx_set_safe_pcie_gen2(struct radeon_device *rdev,
   1248  1.1  riastrad 				     struct radeon_ps *new_ps,
   1249  1.1  riastrad 				     struct radeon_ps *old_ps)
   1250  1.1  riastrad {
   1251  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1252  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1253  1.1  riastrad 
   1254  1.1  riastrad 	if ((new_state->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) !=
   1255  1.1  riastrad 	    (old_state->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2))
   1256  1.1  riastrad 		rv6xx_force_pcie_gen1(rdev);
   1257  1.1  riastrad }
   1258  1.1  riastrad 
   1259  1.1  riastrad static void rv6xx_enable_dynamic_voltage_control(struct radeon_device *rdev,
   1260  1.1  riastrad 						 bool enable)
   1261  1.1  riastrad {
   1262  1.1  riastrad 	if (enable)
   1263  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, VOLT_PWRMGT_EN, ~VOLT_PWRMGT_EN);
   1264  1.1  riastrad 	else
   1265  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~VOLT_PWRMGT_EN);
   1266  1.1  riastrad }
   1267  1.1  riastrad 
   1268  1.1  riastrad static void rv6xx_enable_dynamic_backbias_control(struct radeon_device *rdev,
   1269  1.1  riastrad 						  bool enable)
   1270  1.1  riastrad {
   1271  1.1  riastrad 	if (enable)
   1272  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, BACKBIAS_DPM_CNTL, ~BACKBIAS_DPM_CNTL);
   1273  1.1  riastrad 	else
   1274  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~BACKBIAS_DPM_CNTL);
   1275  1.1  riastrad }
   1276  1.1  riastrad 
   1277  1.1  riastrad static int rv6xx_step_sw_voltage(struct radeon_device *rdev,
   1278  1.1  riastrad 				 u16 initial_voltage,
   1279  1.1  riastrad 				 u16 target_voltage)
   1280  1.1  riastrad {
   1281  1.1  riastrad 	u16 current_voltage;
   1282  1.1  riastrad 	u16 true_target_voltage;
   1283  1.1  riastrad 	u16 voltage_step;
   1284  1.1  riastrad 	int signed_voltage_step;
   1285  1.1  riastrad 
   1286  1.1  riastrad 	if ((radeon_atom_get_voltage_step(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC,
   1287  1.1  riastrad 					  &voltage_step)) ||
   1288  1.1  riastrad 	    (radeon_atom_round_to_true_voltage(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC,
   1289  1.1  riastrad 					       initial_voltage, &current_voltage)) ||
   1290  1.1  riastrad 	    (radeon_atom_round_to_true_voltage(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC,
   1291  1.1  riastrad 					       target_voltage, &true_target_voltage)))
   1292  1.1  riastrad 		return -EINVAL;
   1293  1.1  riastrad 
   1294  1.1  riastrad 	if (true_target_voltage < current_voltage)
   1295  1.1  riastrad 		signed_voltage_step = -(int)voltage_step;
   1296  1.1  riastrad 	else
   1297  1.1  riastrad 		signed_voltage_step = voltage_step;
   1298  1.1  riastrad 
   1299  1.1  riastrad 	while (current_voltage != true_target_voltage) {
   1300  1.1  riastrad 		current_voltage += signed_voltage_step;
   1301  1.1  riastrad 		rv6xx_program_voltage_stepping_entry(rdev, R600_POWER_LEVEL_CTXSW,
   1302  1.1  riastrad 						     current_voltage);
   1303  1.1  riastrad 		msleep((rdev->pm.dpm.voltage_response_time + 999) / 1000);
   1304  1.1  riastrad 	}
   1305  1.1  riastrad 
   1306  1.1  riastrad 	return 0;
   1307  1.1  riastrad }
   1308  1.1  riastrad 
   1309  1.1  riastrad static int rv6xx_step_voltage_if_increasing(struct radeon_device *rdev,
   1310  1.1  riastrad 					    struct radeon_ps *new_ps,
   1311  1.1  riastrad 					    struct radeon_ps *old_ps)
   1312  1.1  riastrad {
   1313  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1314  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1315  1.1  riastrad 
   1316  1.1  riastrad 	if (new_state->low.vddc > old_state->low.vddc)
   1317  1.1  riastrad 		return rv6xx_step_sw_voltage(rdev,
   1318  1.1  riastrad 					     old_state->low.vddc,
   1319  1.1  riastrad 					     new_state->low.vddc);
   1320  1.1  riastrad 
   1321  1.1  riastrad 	return 0;
   1322  1.1  riastrad }
   1323  1.1  riastrad 
   1324  1.1  riastrad static int rv6xx_step_voltage_if_decreasing(struct radeon_device *rdev,
   1325  1.1  riastrad 					    struct radeon_ps *new_ps,
   1326  1.1  riastrad 					    struct radeon_ps *old_ps)
   1327  1.1  riastrad {
   1328  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1329  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1330  1.1  riastrad 
   1331  1.1  riastrad 	if (new_state->low.vddc < old_state->low.vddc)
   1332  1.1  riastrad 		return rv6xx_step_sw_voltage(rdev,
   1333  1.1  riastrad 					     old_state->low.vddc,
   1334  1.1  riastrad 					     new_state->low.vddc);
   1335  1.1  riastrad 	else
   1336  1.1  riastrad 		return 0;
   1337  1.1  riastrad }
   1338  1.1  riastrad 
   1339  1.1  riastrad static void rv6xx_enable_high(struct radeon_device *rdev)
   1340  1.1  riastrad {
   1341  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1342  1.1  riastrad 
   1343  1.1  riastrad 	if ((pi->restricted_levels < 1) ||
   1344  1.1  riastrad 	    (pi->restricted_levels == 3))
   1345  1.1  riastrad 		r600_power_level_enable(rdev, R600_POWER_LEVEL_HIGH, true);
   1346  1.1  riastrad }
   1347  1.1  riastrad 
   1348  1.1  riastrad static void rv6xx_enable_medium(struct radeon_device *rdev)
   1349  1.1  riastrad {
   1350  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1351  1.1  riastrad 
   1352  1.1  riastrad 	if (pi->restricted_levels < 2)
   1353  1.1  riastrad 		r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, true);
   1354  1.1  riastrad }
   1355  1.1  riastrad 
   1356  1.1  riastrad static void rv6xx_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
   1357  1.1  riastrad {
   1358  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1359  1.1  riastrad 	bool want_thermal_protection;
   1360  1.1  riastrad 	enum radeon_dpm_event_src dpm_event_src;
   1361  1.1  riastrad 
   1362  1.1  riastrad 	switch (sources) {
   1363  1.2  riastrad 	case 0:
   1364  1.2  riastrad 	default:
   1365  1.1  riastrad 		want_thermal_protection = false;
   1366  1.1  riastrad 		break;
   1367  1.2  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
   1368  1.1  riastrad 		want_thermal_protection = true;
   1369  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGITAL;
   1370  1.1  riastrad 		break;
   1371  1.1  riastrad 
   1372  1.2  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
   1373  1.1  riastrad 		want_thermal_protection = true;
   1374  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_EXTERNAL;
   1375  1.1  riastrad 		break;
   1376  1.1  riastrad 
   1377  1.2  riastrad 	case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
   1378  1.1  riastrad 	      (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
   1379  1.2  riastrad 			want_thermal_protection = true;
   1380  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL;
   1381  1.1  riastrad 		break;
   1382  1.1  riastrad 	}
   1383  1.1  riastrad 
   1384  1.1  riastrad 	if (want_thermal_protection) {
   1385  1.1  riastrad 		WREG32_P(CG_THERMAL_CTRL, DPM_EVENT_SRC(dpm_event_src), ~DPM_EVENT_SRC_MASK);
   1386  1.1  riastrad 		if (pi->thermal_protection)
   1387  1.1  riastrad 			WREG32_P(GENERAL_PWRMGT, 0, ~THERMAL_PROTECTION_DIS);
   1388  1.1  riastrad 	} else {
   1389  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, ~THERMAL_PROTECTION_DIS);
   1390  1.1  riastrad 	}
   1391  1.1  riastrad }
   1392  1.1  riastrad 
   1393  1.1  riastrad static void rv6xx_enable_auto_throttle_source(struct radeon_device *rdev,
   1394  1.1  riastrad 					      enum radeon_dpm_auto_throttle_src source,
   1395  1.1  riastrad 					      bool enable)
   1396  1.1  riastrad {
   1397  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1398  1.1  riastrad 
   1399  1.1  riastrad 	if (enable) {
   1400  1.1  riastrad 		if (!(pi->active_auto_throttle_sources & (1 << source))) {
   1401  1.1  riastrad 			pi->active_auto_throttle_sources |= 1 << source;
   1402  1.1  riastrad 			rv6xx_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1403  1.1  riastrad 		}
   1404  1.1  riastrad 	} else {
   1405  1.1  riastrad 		if (pi->active_auto_throttle_sources & (1 << source)) {
   1406  1.1  riastrad 			pi->active_auto_throttle_sources &= ~(1 << source);
   1407  1.1  riastrad 			rv6xx_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1408  1.1  riastrad 		}
   1409  1.1  riastrad 	}
   1410  1.1  riastrad }
   1411  1.1  riastrad 
   1412  1.1  riastrad 
   1413  1.1  riastrad static void rv6xx_enable_thermal_protection(struct radeon_device *rdev,
   1414  1.1  riastrad 					    bool enable)
   1415  1.1  riastrad {
   1416  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1417  1.1  riastrad 
   1418  1.1  riastrad 	if (pi->active_auto_throttle_sources)
   1419  1.1  riastrad 		r600_enable_thermal_protection(rdev, enable);
   1420  1.1  riastrad }
   1421  1.1  riastrad 
   1422  1.1  riastrad static void rv6xx_generate_transition_stepping(struct radeon_device *rdev,
   1423  1.1  riastrad 					       struct radeon_ps *new_ps,
   1424  1.1  riastrad 					       struct radeon_ps *old_ps)
   1425  1.1  riastrad {
   1426  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1427  1.1  riastrad 	struct rv6xx_ps *old_state = rv6xx_get_ps(old_ps);
   1428  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1429  1.1  riastrad 
   1430  1.1  riastrad 	rv6xx_generate_steps(rdev,
   1431  1.1  riastrad 			     old_state->low.sclk,
   1432  1.1  riastrad 			     new_state->low.sclk,
   1433  1.1  riastrad 			     0, &pi->hw.medium_sclk_index);
   1434  1.1  riastrad }
   1435  1.1  riastrad 
   1436  1.1  riastrad static void rv6xx_generate_low_step(struct radeon_device *rdev,
   1437  1.1  riastrad 				    struct radeon_ps *new_ps)
   1438  1.1  riastrad {
   1439  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1440  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1441  1.1  riastrad 
   1442  1.1  riastrad 	pi->hw.low_sclk_index = 0;
   1443  1.1  riastrad 	rv6xx_generate_single_step(rdev,
   1444  1.1  riastrad 				   new_state->low.sclk,
   1445  1.1  riastrad 				   0);
   1446  1.1  riastrad }
   1447  1.1  riastrad 
   1448  1.1  riastrad static void rv6xx_invalidate_intermediate_steps(struct radeon_device *rdev)
   1449  1.1  riastrad {
   1450  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1451  1.1  riastrad 
   1452  1.1  riastrad 	rv6xx_invalidate_intermediate_steps_range(rdev, 0,
   1453  1.1  riastrad 						  pi->hw.medium_sclk_index);
   1454  1.1  riastrad }
   1455  1.1  riastrad 
   1456  1.1  riastrad static void rv6xx_generate_stepping_table(struct radeon_device *rdev,
   1457  1.1  riastrad 					  struct radeon_ps *new_ps)
   1458  1.1  riastrad {
   1459  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1460  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1461  1.1  riastrad 
   1462  1.1  riastrad 	pi->hw.low_sclk_index = 0;
   1463  1.1  riastrad 
   1464  1.1  riastrad 	rv6xx_generate_steps(rdev,
   1465  1.1  riastrad 			     new_state->low.sclk,
   1466  1.1  riastrad 			     new_state->medium.sclk,
   1467  1.1  riastrad 			     0,
   1468  1.1  riastrad 			     &pi->hw.medium_sclk_index);
   1469  1.1  riastrad 	rv6xx_generate_steps(rdev,
   1470  1.1  riastrad 			     new_state->medium.sclk,
   1471  1.1  riastrad 			     new_state->high.sclk,
   1472  1.1  riastrad 			     pi->hw.medium_sclk_index,
   1473  1.1  riastrad 			     &pi->hw.high_sclk_index);
   1474  1.1  riastrad }
   1475  1.1  riastrad 
   1476  1.1  riastrad static void rv6xx_enable_spread_spectrum(struct radeon_device *rdev,
   1477  1.1  riastrad 					 bool enable)
   1478  1.1  riastrad {
   1479  1.1  riastrad 	if (enable)
   1480  1.1  riastrad 		rv6xx_enable_dynamic_spread_spectrum(rdev, true);
   1481  1.1  riastrad 	else {
   1482  1.1  riastrad 		rv6xx_enable_engine_spread_spectrum(rdev, R600_POWER_LEVEL_LOW, false);
   1483  1.1  riastrad 		rv6xx_enable_engine_spread_spectrum(rdev, R600_POWER_LEVEL_MEDIUM, false);
   1484  1.1  riastrad 		rv6xx_enable_engine_spread_spectrum(rdev, R600_POWER_LEVEL_HIGH, false);
   1485  1.1  riastrad 		rv6xx_enable_dynamic_spread_spectrum(rdev, false);
   1486  1.1  riastrad 		rv6xx_enable_memory_spread_spectrum(rdev, false);
   1487  1.1  riastrad 	}
   1488  1.1  riastrad }
   1489  1.1  riastrad 
   1490  1.1  riastrad static void rv6xx_reset_lvtm_data_sync(struct radeon_device *rdev)
   1491  1.1  riastrad {
   1492  1.1  riastrad 	if (ASIC_IS_DCE3(rdev))
   1493  1.1  riastrad 		WREG32_P(DCE3_LVTMA_DATA_SYNCHRONIZATION, LVTMA_PFREQCHG, ~LVTMA_PFREQCHG);
   1494  1.1  riastrad 	else
   1495  1.1  riastrad 		WREG32_P(LVTMA_DATA_SYNCHRONIZATION, LVTMA_PFREQCHG, ~LVTMA_PFREQCHG);
   1496  1.1  riastrad }
   1497  1.1  riastrad 
   1498  1.1  riastrad static void rv6xx_enable_dynamic_pcie_gen2(struct radeon_device *rdev,
   1499  1.1  riastrad 					   struct radeon_ps *new_ps,
   1500  1.1  riastrad 					   bool enable)
   1501  1.1  riastrad {
   1502  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1503  1.1  riastrad 
   1504  1.1  riastrad 	if (enable) {
   1505  1.1  riastrad 		rv6xx_enable_bif_dynamic_pcie_gen2(rdev, true);
   1506  1.1  riastrad 		rv6xx_enable_pcie_gen2_support(rdev);
   1507  1.1  riastrad 		r600_enable_dynamic_pcie_gen2(rdev, true);
   1508  1.1  riastrad 	} else {
   1509  1.1  riastrad 		if (!(new_state->low.flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2))
   1510  1.1  riastrad 			rv6xx_force_pcie_gen1(rdev);
   1511  1.1  riastrad 		rv6xx_enable_bif_dynamic_pcie_gen2(rdev, false);
   1512  1.1  riastrad 		r600_enable_dynamic_pcie_gen2(rdev, false);
   1513  1.1  riastrad 	}
   1514  1.1  riastrad }
   1515  1.1  riastrad 
   1516  1.1  riastrad static void rv6xx_set_uvd_clock_before_set_eng_clock(struct radeon_device *rdev,
   1517  1.1  riastrad 						     struct radeon_ps *new_ps,
   1518  1.1  riastrad 						     struct radeon_ps *old_ps)
   1519  1.1  riastrad {
   1520  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1521  1.1  riastrad 	struct rv6xx_ps *current_state = rv6xx_get_ps(old_ps);
   1522  1.1  riastrad 
   1523  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   1524  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   1525  1.1  riastrad 		return;
   1526  1.1  riastrad 
   1527  1.1  riastrad 	if (new_state->high.sclk >= current_state->high.sclk)
   1528  1.1  riastrad 		return;
   1529  1.1  riastrad 
   1530  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   1531  1.1  riastrad }
   1532  1.1  riastrad 
   1533  1.1  riastrad static void rv6xx_set_uvd_clock_after_set_eng_clock(struct radeon_device *rdev,
   1534  1.1  riastrad 						    struct radeon_ps *new_ps,
   1535  1.1  riastrad 						    struct radeon_ps *old_ps)
   1536  1.1  riastrad {
   1537  1.1  riastrad 	struct rv6xx_ps *new_state = rv6xx_get_ps(new_ps);
   1538  1.1  riastrad 	struct rv6xx_ps *current_state = rv6xx_get_ps(old_ps);
   1539  1.1  riastrad 
   1540  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   1541  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   1542  1.1  riastrad 		return;
   1543  1.1  riastrad 
   1544  1.1  riastrad 	if (new_state->high.sclk < current_state->high.sclk)
   1545  1.1  riastrad 		return;
   1546  1.1  riastrad 
   1547  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   1548  1.1  riastrad }
   1549  1.1  riastrad 
   1550  1.1  riastrad int rv6xx_dpm_enable(struct radeon_device *rdev)
   1551  1.1  riastrad {
   1552  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1553  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   1554  1.1  riastrad 
   1555  1.1  riastrad 	if (r600_dynamicpm_enabled(rdev))
   1556  1.1  riastrad 		return -EINVAL;
   1557  1.1  riastrad 
   1558  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1559  1.1  riastrad 		rv6xx_enable_backbias(rdev, true);
   1560  1.1  riastrad 
   1561  1.1  riastrad 	if (pi->dynamic_ss)
   1562  1.1  riastrad 		rv6xx_enable_spread_spectrum(rdev, true);
   1563  1.1  riastrad 
   1564  1.1  riastrad 	rv6xx_program_mpll_timing_parameters(rdev);
   1565  1.1  riastrad 	rv6xx_program_bsp(rdev);
   1566  1.1  riastrad 	rv6xx_program_git(rdev);
   1567  1.1  riastrad 	rv6xx_program_tp(rdev);
   1568  1.1  riastrad 	rv6xx_program_tpp(rdev);
   1569  1.1  riastrad 	rv6xx_program_sstp(rdev);
   1570  1.1  riastrad 	rv6xx_program_fcp(rdev);
   1571  1.1  riastrad 	rv6xx_program_vddc3d_parameters(rdev);
   1572  1.1  riastrad 	rv6xx_program_voltage_timing_parameters(rdev);
   1573  1.1  riastrad 	rv6xx_program_engine_speed_parameters(rdev);
   1574  1.1  riastrad 
   1575  1.1  riastrad 	rv6xx_enable_display_gap(rdev, true);
   1576  1.1  riastrad 	if (pi->display_gap == false)
   1577  1.1  riastrad 		rv6xx_enable_display_gap(rdev, false);
   1578  1.1  riastrad 
   1579  1.1  riastrad 	rv6xx_program_power_level_enter_state(rdev);
   1580  1.1  riastrad 
   1581  1.1  riastrad 	rv6xx_calculate_stepping_parameters(rdev, boot_ps);
   1582  1.1  riastrad 
   1583  1.1  riastrad 	if (pi->voltage_control)
   1584  1.1  riastrad 		rv6xx_program_voltage_gpio_pins(rdev);
   1585  1.1  riastrad 
   1586  1.1  riastrad 	rv6xx_generate_stepping_table(rdev, boot_ps);
   1587  1.1  riastrad 
   1588  1.1  riastrad 	rv6xx_program_stepping_parameters_except_lowest_entry(rdev);
   1589  1.1  riastrad 	rv6xx_program_stepping_parameters_lowest_entry(rdev);
   1590  1.1  riastrad 
   1591  1.1  riastrad 	rv6xx_program_power_level_low(rdev);
   1592  1.1  riastrad 	rv6xx_program_power_level_medium(rdev);
   1593  1.1  riastrad 	rv6xx_program_power_level_high(rdev);
   1594  1.1  riastrad 	rv6xx_program_vc(rdev);
   1595  1.1  riastrad 	rv6xx_program_at(rdev);
   1596  1.1  riastrad 
   1597  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, true);
   1598  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, true);
   1599  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_HIGH, true);
   1600  1.1  riastrad 
   1601  1.1  riastrad 	rv6xx_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
   1602  1.1  riastrad 
   1603  1.1  riastrad 	r600_start_dpm(rdev);
   1604  1.1  riastrad 
   1605  1.1  riastrad 	if (pi->voltage_control)
   1606  1.1  riastrad 		rv6xx_enable_static_voltage_control(rdev, boot_ps, false);
   1607  1.1  riastrad 
   1608  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   1609  1.1  riastrad 		rv6xx_enable_dynamic_pcie_gen2(rdev, boot_ps, true);
   1610  1.1  riastrad 
   1611  1.1  riastrad 	if (pi->gfx_clock_gating)
   1612  1.1  riastrad 		r600_gfx_clockgating_enable(rdev, true);
   1613  1.1  riastrad 
   1614  1.1  riastrad 	return 0;
   1615  1.1  riastrad }
   1616  1.1  riastrad 
   1617  1.1  riastrad void rv6xx_dpm_disable(struct radeon_device *rdev)
   1618  1.1  riastrad {
   1619  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1620  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   1621  1.1  riastrad 
   1622  1.1  riastrad 	if (!r600_dynamicpm_enabled(rdev))
   1623  1.1  riastrad 		return;
   1624  1.1  riastrad 
   1625  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, true);
   1626  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, true);
   1627  1.1  riastrad 	rv6xx_enable_display_gap(rdev, false);
   1628  1.1  riastrad 	rv6xx_clear_vc(rdev);
   1629  1.1  riastrad 	r600_set_at(rdev, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
   1630  1.1  riastrad 
   1631  1.1  riastrad 	if (pi->thermal_protection)
   1632  1.1  riastrad 		r600_enable_thermal_protection(rdev, false);
   1633  1.1  riastrad 
   1634  1.1  riastrad 	r600_wait_for_power_level(rdev, R600_POWER_LEVEL_LOW);
   1635  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_HIGH, false);
   1636  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, false);
   1637  1.1  riastrad 
   1638  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1639  1.1  riastrad 		rv6xx_enable_backbias(rdev, false);
   1640  1.1  riastrad 
   1641  1.1  riastrad 	rv6xx_enable_spread_spectrum(rdev, false);
   1642  1.1  riastrad 
   1643  1.1  riastrad 	if (pi->voltage_control)
   1644  1.1  riastrad 		rv6xx_enable_static_voltage_control(rdev, boot_ps, true);
   1645  1.1  riastrad 
   1646  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   1647  1.1  riastrad 		rv6xx_enable_dynamic_pcie_gen2(rdev, boot_ps, false);
   1648  1.1  riastrad 
   1649  1.1  riastrad 	if (rdev->irq.installed &&
   1650  1.1  riastrad 	    r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
   1651  1.1  riastrad 		rdev->irq.dpm_thermal = false;
   1652  1.1  riastrad 		radeon_irq_set(rdev);
   1653  1.1  riastrad 	}
   1654  1.1  riastrad 
   1655  1.1  riastrad 	if (pi->gfx_clock_gating)
   1656  1.1  riastrad 		r600_gfx_clockgating_enable(rdev, false);
   1657  1.1  riastrad 
   1658  1.1  riastrad 	r600_stop_dpm(rdev);
   1659  1.1  riastrad }
   1660  1.1  riastrad 
   1661  1.1  riastrad int rv6xx_dpm_set_power_state(struct radeon_device *rdev)
   1662  1.1  riastrad {
   1663  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   1664  1.1  riastrad 	struct radeon_ps *new_ps = rdev->pm.dpm.requested_ps;
   1665  1.1  riastrad 	struct radeon_ps *old_ps = rdev->pm.dpm.current_ps;
   1666  1.1  riastrad 	int ret;
   1667  1.1  riastrad 
   1668  1.1  riastrad 	pi->restricted_levels = 0;
   1669  1.1  riastrad 
   1670  1.1  riastrad 	rv6xx_set_uvd_clock_before_set_eng_clock(rdev, new_ps, old_ps);
   1671  1.1  riastrad 
   1672  1.1  riastrad 	rv6xx_clear_vc(rdev);
   1673  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, true);
   1674  1.1  riastrad 	r600_set_at(rdev, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
   1675  1.1  riastrad 
   1676  1.1  riastrad 	if (pi->thermal_protection)
   1677  1.1  riastrad 		r600_enable_thermal_protection(rdev, false);
   1678  1.1  riastrad 
   1679  1.1  riastrad 	r600_wait_for_power_level(rdev, R600_POWER_LEVEL_LOW);
   1680  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_HIGH, false);
   1681  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, false);
   1682  1.1  riastrad 
   1683  1.1  riastrad 	rv6xx_generate_transition_stepping(rdev, new_ps, old_ps);
   1684  1.1  riastrad 	rv6xx_program_power_level_medium_for_transition(rdev);
   1685  1.1  riastrad 
   1686  1.1  riastrad 	if (pi->voltage_control) {
   1687  1.1  riastrad 		rv6xx_set_sw_voltage_to_safe(rdev, new_ps, old_ps);
   1688  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
   1689  1.1  riastrad 			rv6xx_set_sw_voltage_to_low(rdev, old_ps);
   1690  1.1  riastrad 	}
   1691  1.1  riastrad 
   1692  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1693  1.1  riastrad 		rv6xx_set_safe_backbias(rdev, new_ps, old_ps);
   1694  1.1  riastrad 
   1695  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   1696  1.1  riastrad 		rv6xx_set_safe_pcie_gen2(rdev, new_ps, old_ps);
   1697  1.1  riastrad 
   1698  1.1  riastrad 	if (pi->voltage_control)
   1699  1.1  riastrad 		rv6xx_enable_dynamic_voltage_control(rdev, false);
   1700  1.1  riastrad 
   1701  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1702  1.1  riastrad 		rv6xx_enable_dynamic_backbias_control(rdev, false);
   1703  1.1  riastrad 
   1704  1.1  riastrad 	if (pi->voltage_control) {
   1705  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
   1706  1.1  riastrad 			rv6xx_step_voltage_if_increasing(rdev, new_ps, old_ps);
   1707  1.1  riastrad 		msleep((rdev->pm.dpm.voltage_response_time + 999) / 1000);
   1708  1.1  riastrad 	}
   1709  1.1  riastrad 
   1710  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, true);
   1711  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, false);
   1712  1.1  riastrad 	r600_wait_for_power_level_unequal(rdev, R600_POWER_LEVEL_LOW);
   1713  1.1  riastrad 
   1714  1.1  riastrad 	rv6xx_generate_low_step(rdev, new_ps);
   1715  1.1  riastrad 	rv6xx_invalidate_intermediate_steps(rdev);
   1716  1.1  riastrad 	rv6xx_calculate_stepping_parameters(rdev, new_ps);
   1717  1.1  riastrad 	rv6xx_program_stepping_parameters_lowest_entry(rdev);
   1718  1.1  riastrad 	rv6xx_program_power_level_low_to_lowest_state(rdev);
   1719  1.1  riastrad 
   1720  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, true);
   1721  1.1  riastrad 	r600_wait_for_power_level(rdev, R600_POWER_LEVEL_LOW);
   1722  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, false);
   1723  1.1  riastrad 
   1724  1.1  riastrad 	if (pi->voltage_control) {
   1725  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC) {
   1726  1.1  riastrad 			ret = rv6xx_step_voltage_if_decreasing(rdev, new_ps, old_ps);
   1727  1.1  riastrad 			if (ret)
   1728  1.1  riastrad 				return ret;
   1729  1.1  riastrad 		}
   1730  1.1  riastrad 		rv6xx_enable_dynamic_voltage_control(rdev, true);
   1731  1.1  riastrad 	}
   1732  1.1  riastrad 
   1733  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS)
   1734  1.1  riastrad 		rv6xx_enable_dynamic_backbias_control(rdev, true);
   1735  1.1  riastrad 
   1736  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   1737  1.1  riastrad 		rv6xx_enable_dynamic_pcie_gen2(rdev, new_ps, true);
   1738  1.1  riastrad 
   1739  1.1  riastrad 	rv6xx_reset_lvtm_data_sync(rdev);
   1740  1.1  riastrad 
   1741  1.1  riastrad 	rv6xx_generate_stepping_table(rdev, new_ps);
   1742  1.1  riastrad 	rv6xx_program_stepping_parameters_except_lowest_entry(rdev);
   1743  1.1  riastrad 	rv6xx_program_power_level_low(rdev);
   1744  1.1  riastrad 	rv6xx_program_power_level_medium(rdev);
   1745  1.1  riastrad 	rv6xx_program_power_level_high(rdev);
   1746  1.1  riastrad 	rv6xx_enable_medium(rdev);
   1747  1.1  riastrad 	rv6xx_enable_high(rdev);
   1748  1.1  riastrad 
   1749  1.1  riastrad 	if (pi->thermal_protection)
   1750  1.1  riastrad 		rv6xx_enable_thermal_protection(rdev, true);
   1751  1.1  riastrad 	rv6xx_program_vc(rdev);
   1752  1.1  riastrad 	rv6xx_program_at(rdev);
   1753  1.1  riastrad 
   1754  1.1  riastrad 	rv6xx_set_uvd_clock_after_set_eng_clock(rdev, new_ps, old_ps);
   1755  1.1  riastrad 
   1756  1.1  riastrad 	return 0;
   1757  1.1  riastrad }
   1758  1.1  riastrad 
   1759  1.1  riastrad void rv6xx_setup_asic(struct radeon_device *rdev)
   1760  1.1  riastrad {
   1761  1.1  riastrad 	r600_enable_acpi_pm(rdev);
   1762  1.1  riastrad 
   1763  1.1  riastrad 	if (radeon_aspm != 0) {
   1764  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_ASPM_L0s)
   1765  1.1  riastrad 			rv6xx_enable_l0s(rdev);
   1766  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_ASPM_L1)
   1767  1.1  riastrad 			rv6xx_enable_l1(rdev);
   1768  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_TURNOFFPLL_ASPML1)
   1769  1.1  riastrad 			rv6xx_enable_pll_sleep_in_l1(rdev);
   1770  1.1  riastrad 	}
   1771  1.1  riastrad }
   1772  1.1  riastrad 
   1773  1.1  riastrad void rv6xx_dpm_display_configuration_changed(struct radeon_device *rdev)
   1774  1.1  riastrad {
   1775  1.1  riastrad 	rv6xx_program_display_gap(rdev);
   1776  1.1  riastrad }
   1777  1.1  riastrad 
   1778  1.1  riastrad union power_info {
   1779  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO info;
   1780  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
   1781  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
   1782  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
   1783  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
   1784  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
   1785  1.1  riastrad };
   1786  1.1  riastrad 
   1787  1.1  riastrad union pplib_clock_info {
   1788  1.1  riastrad 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
   1789  1.1  riastrad 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
   1790  1.1  riastrad 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
   1791  1.1  riastrad 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
   1792  1.1  riastrad };
   1793  1.1  riastrad 
   1794  1.1  riastrad union pplib_power_state {
   1795  1.1  riastrad 	struct _ATOM_PPLIB_STATE v1;
   1796  1.1  riastrad 	struct _ATOM_PPLIB_STATE_V2 v2;
   1797  1.1  riastrad };
   1798  1.1  riastrad 
   1799  1.1  riastrad static void rv6xx_parse_pplib_non_clock_info(struct radeon_device *rdev,
   1800  1.1  riastrad 					     struct radeon_ps *rps,
   1801  1.1  riastrad 					     struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info)
   1802  1.1  riastrad {
   1803  1.1  riastrad 	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
   1804  1.1  riastrad 	rps->class = le16_to_cpu(non_clock_info->usClassification);
   1805  1.1  riastrad 	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
   1806  1.1  riastrad 
   1807  1.1  riastrad 	if (r600_is_uvd_state(rps->class, rps->class2)) {
   1808  1.1  riastrad 		rps->vclk = RV6XX_DEFAULT_VCLK_FREQ;
   1809  1.1  riastrad 		rps->dclk = RV6XX_DEFAULT_DCLK_FREQ;
   1810  1.1  riastrad 	} else {
   1811  1.1  riastrad 		rps->vclk = 0;
   1812  1.1  riastrad 		rps->dclk = 0;
   1813  1.1  riastrad 	}
   1814  1.1  riastrad 
   1815  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
   1816  1.1  riastrad 		rdev->pm.dpm.boot_ps = rps;
   1817  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
   1818  1.1  riastrad 		rdev->pm.dpm.uvd_ps = rps;
   1819  1.1  riastrad }
   1820  1.1  riastrad 
   1821  1.1  riastrad static void rv6xx_parse_pplib_clock_info(struct radeon_device *rdev,
   1822  1.1  riastrad 					 struct radeon_ps *rps, int index,
   1823  1.1  riastrad 					 union pplib_clock_info *clock_info)
   1824  1.1  riastrad {
   1825  1.1  riastrad 	struct rv6xx_ps *ps = rv6xx_get_ps(rps);
   1826  1.1  riastrad 	u32 sclk, mclk;
   1827  1.1  riastrad 	u16 vddc;
   1828  1.1  riastrad 	struct rv6xx_pl *pl;
   1829  1.1  riastrad 
   1830  1.1  riastrad 	switch (index) {
   1831  1.1  riastrad 	case 0:
   1832  1.1  riastrad 		pl = &ps->low;
   1833  1.1  riastrad 		break;
   1834  1.1  riastrad 	case 1:
   1835  1.1  riastrad 		pl = &ps->medium;
   1836  1.1  riastrad 		break;
   1837  1.1  riastrad 	case 2:
   1838  1.1  riastrad 	default:
   1839  1.1  riastrad 		pl = &ps->high;
   1840  1.1  riastrad 		break;
   1841  1.1  riastrad 	}
   1842  1.1  riastrad 
   1843  1.1  riastrad 	sclk = le16_to_cpu(clock_info->r600.usEngineClockLow);
   1844  1.1  riastrad 	sclk |= clock_info->r600.ucEngineClockHigh << 16;
   1845  1.1  riastrad 	mclk = le16_to_cpu(clock_info->r600.usMemoryClockLow);
   1846  1.1  riastrad 	mclk |= clock_info->r600.ucMemoryClockHigh << 16;
   1847  1.1  riastrad 
   1848  1.1  riastrad 	pl->mclk = mclk;
   1849  1.1  riastrad 	pl->sclk = sclk;
   1850  1.1  riastrad 	pl->vddc = le16_to_cpu(clock_info->r600.usVDDC);
   1851  1.1  riastrad 	pl->flags = le32_to_cpu(clock_info->r600.ulFlags);
   1852  1.1  riastrad 
   1853  1.1  riastrad 	/* patch up vddc if necessary */
   1854  1.1  riastrad 	if (pl->vddc == 0xff01) {
   1855  1.1  riastrad 		if (radeon_atom_get_max_vddc(rdev, 0, 0, &vddc) == 0)
   1856  1.1  riastrad 			pl->vddc = vddc;
   1857  1.1  riastrad 	}
   1858  1.1  riastrad 
   1859  1.1  riastrad 	/* fix up pcie gen2 */
   1860  1.1  riastrad 	if (pl->flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) {
   1861  1.1  riastrad 		if ((rdev->family == CHIP_RV610) || (rdev->family == CHIP_RV630)) {
   1862  1.1  riastrad 			if (pl->vddc < 1100)
   1863  1.1  riastrad 				pl->flags &= ~ATOM_PPLIB_R600_FLAGS_PCIEGEN2;
   1864  1.1  riastrad 		}
   1865  1.1  riastrad 	}
   1866  1.1  riastrad 
   1867  1.1  riastrad 	/* patch up boot state */
   1868  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
   1869  1.1  riastrad 		u16 vddc, vddci, mvdd;
   1870  1.1  riastrad 		radeon_atombios_get_default_voltages(rdev, &vddc, &vddci, &mvdd);
   1871  1.1  riastrad 		pl->mclk = rdev->clock.default_mclk;
   1872  1.1  riastrad 		pl->sclk = rdev->clock.default_sclk;
   1873  1.1  riastrad 		pl->vddc = vddc;
   1874  1.1  riastrad 	}
   1875  1.1  riastrad }
   1876  1.1  riastrad 
   1877  1.1  riastrad static int rv6xx_parse_power_table(struct radeon_device *rdev)
   1878  1.1  riastrad {
   1879  1.1  riastrad 	struct radeon_mode_info *mode_info = &rdev->mode_info;
   1880  1.1  riastrad 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
   1881  1.1  riastrad 	union pplib_power_state *power_state;
   1882  1.1  riastrad 	int i, j;
   1883  1.1  riastrad 	union pplib_clock_info *clock_info;
   1884  1.1  riastrad 	union power_info *power_info;
   1885  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
   1886  1.2  riastrad 	u16 data_offset;
   1887  1.1  riastrad 	u8 frev, crev;
   1888  1.1  riastrad 	struct rv6xx_ps *ps;
   1889  1.1  riastrad 
   1890  1.1  riastrad 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
   1891  1.1  riastrad 				   &frev, &crev, &data_offset))
   1892  1.1  riastrad 		return -EINVAL;
   1893  1.1  riastrad 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
   1894  1.1  riastrad 
   1895  1.2  riastrad 	rdev->pm.dpm.ps = kcalloc(power_info->pplib.ucNumStates,
   1896  1.2  riastrad 				  sizeof(struct radeon_ps),
   1897  1.2  riastrad 				  GFP_KERNEL);
   1898  1.1  riastrad 	if (!rdev->pm.dpm.ps)
   1899  1.1  riastrad 		return -ENOMEM;
   1900  1.1  riastrad 
   1901  1.1  riastrad 	for (i = 0; i < power_info->pplib.ucNumStates; i++) {
   1902  1.1  riastrad 		power_state = (union pplib_power_state *)
   1903  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   1904  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usStateArrayOffset) +
   1905  1.1  riastrad 			 i * power_info->pplib.ucStateEntrySize);
   1906  1.1  riastrad 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
   1907  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   1908  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset) +
   1909  1.1  riastrad 			 (power_state->v1.ucNonClockStateIndex *
   1910  1.1  riastrad 			  power_info->pplib.ucNonClockSize));
   1911  1.1  riastrad 		if (power_info->pplib.ucStateEntrySize - 1) {
   1912  1.1  riastrad 			u8 *idx;
   1913  1.1  riastrad 			ps = kzalloc(sizeof(struct rv6xx_ps), GFP_KERNEL);
   1914  1.1  riastrad 			if (ps == NULL) {
   1915  1.1  riastrad 				kfree(rdev->pm.dpm.ps);
   1916  1.1  riastrad 				return -ENOMEM;
   1917  1.1  riastrad 			}
   1918  1.1  riastrad 			rdev->pm.dpm.ps[i].ps_priv = ps;
   1919  1.1  riastrad 			rv6xx_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
   1920  1.1  riastrad 							 non_clock_info);
   1921  1.1  riastrad 			idx = (u8 *)&power_state->v1.ucClockStateIndices[0];
   1922  1.1  riastrad 			for (j = 0; j < (power_info->pplib.ucStateEntrySize - 1); j++) {
   1923  1.1  riastrad 				clock_info = (union pplib_clock_info *)
   1924  1.1  riastrad 					(mode_info->atom_context->bios + data_offset +
   1925  1.1  riastrad 					 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset) +
   1926  1.1  riastrad 					 (idx[j] * power_info->pplib.ucClockInfoSize));
   1927  1.1  riastrad 				rv6xx_parse_pplib_clock_info(rdev,
   1928  1.1  riastrad 							     &rdev->pm.dpm.ps[i], j,
   1929  1.1  riastrad 							     clock_info);
   1930  1.1  riastrad 			}
   1931  1.1  riastrad 		}
   1932  1.1  riastrad 	}
   1933  1.1  riastrad 	rdev->pm.dpm.num_ps = power_info->pplib.ucNumStates;
   1934  1.1  riastrad 	return 0;
   1935  1.1  riastrad }
   1936  1.1  riastrad 
   1937  1.1  riastrad int rv6xx_dpm_init(struct radeon_device *rdev)
   1938  1.1  riastrad {
   1939  1.1  riastrad 	struct radeon_atom_ss ss;
   1940  1.1  riastrad 	struct atom_clock_dividers dividers;
   1941  1.1  riastrad 	struct rv6xx_power_info *pi;
   1942  1.1  riastrad 	int ret;
   1943  1.1  riastrad 
   1944  1.1  riastrad 	pi = kzalloc(sizeof(struct rv6xx_power_info), GFP_KERNEL);
   1945  1.1  riastrad 	if (pi == NULL)
   1946  1.1  riastrad 		return -ENOMEM;
   1947  1.1  riastrad 	rdev->pm.dpm.priv = pi;
   1948  1.1  riastrad 
   1949  1.1  riastrad 	ret = r600_get_platform_caps(rdev);
   1950  1.1  riastrad 	if (ret)
   1951  1.1  riastrad 		return ret;
   1952  1.1  riastrad 
   1953  1.1  riastrad 	ret = rv6xx_parse_power_table(rdev);
   1954  1.1  riastrad 	if (ret)
   1955  1.1  riastrad 		return ret;
   1956  1.1  riastrad 
   1957  1.1  riastrad 	if (rdev->pm.dpm.voltage_response_time == 0)
   1958  1.1  riastrad 		rdev->pm.dpm.voltage_response_time = R600_VOLTAGERESPONSETIME_DFLT;
   1959  1.1  riastrad 	if (rdev->pm.dpm.backbias_response_time == 0)
   1960  1.1  riastrad 		rdev->pm.dpm.backbias_response_time = R600_BACKBIASRESPONSETIME_DFLT;
   1961  1.1  riastrad 
   1962  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
   1963  1.1  riastrad 					     0, false, &dividers);
   1964  1.1  riastrad 	if (ret)
   1965  1.1  riastrad 		pi->spll_ref_div = dividers.ref_div + 1;
   1966  1.1  riastrad 	else
   1967  1.1  riastrad 		pi->spll_ref_div = R600_REFERENCEDIVIDER_DFLT;
   1968  1.1  riastrad 
   1969  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_MEMORY_PLL_PARAM,
   1970  1.1  riastrad 					     0, false, &dividers);
   1971  1.1  riastrad 	if (ret)
   1972  1.1  riastrad 		pi->mpll_ref_div = dividers.ref_div + 1;
   1973  1.1  riastrad 	else
   1974  1.1  riastrad 		pi->mpll_ref_div = R600_REFERENCEDIVIDER_DFLT;
   1975  1.1  riastrad 
   1976  1.1  riastrad 	if (rdev->family >= CHIP_RV670)
   1977  1.1  riastrad 		pi->fb_div_scale = 1;
   1978  1.1  riastrad 	else
   1979  1.1  riastrad 		pi->fb_div_scale = 0;
   1980  1.1  riastrad 
   1981  1.1  riastrad 	pi->voltage_control =
   1982  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, 0);
   1983  1.1  riastrad 
   1984  1.1  riastrad 	pi->gfx_clock_gating = true;
   1985  1.1  riastrad 
   1986  1.1  riastrad 	pi->sclk_ss = radeon_atombios_get_asic_ss_info(rdev, &ss,
   1987  1.1  riastrad 						       ASIC_INTERNAL_ENGINE_SS, 0);
   1988  1.1  riastrad 	pi->mclk_ss = radeon_atombios_get_asic_ss_info(rdev, &ss,
   1989  1.1  riastrad 						       ASIC_INTERNAL_MEMORY_SS, 0);
   1990  1.1  riastrad 
   1991  1.1  riastrad 	/* Disable sclk ss, causes hangs on a lot of systems */
   1992  1.1  riastrad 	pi->sclk_ss = false;
   1993  1.1  riastrad 
   1994  1.1  riastrad 	if (pi->sclk_ss || pi->mclk_ss)
   1995  1.1  riastrad 		pi->dynamic_ss = true;
   1996  1.1  riastrad 	else
   1997  1.1  riastrad 		pi->dynamic_ss = false;
   1998  1.1  riastrad 
   1999  1.1  riastrad 	pi->dynamic_pcie_gen2 = true;
   2000  1.1  riastrad 
   2001  1.1  riastrad 	if (pi->gfx_clock_gating &&
   2002  1.1  riastrad 	    (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE))
   2003  1.1  riastrad 		pi->thermal_protection = true;
   2004  1.1  riastrad 	else
   2005  1.1  riastrad 		pi->thermal_protection = false;
   2006  1.1  riastrad 
   2007  1.1  riastrad 	pi->display_gap = true;
   2008  1.1  riastrad 
   2009  1.1  riastrad 	return 0;
   2010  1.1  riastrad }
   2011  1.1  riastrad 
   2012  1.1  riastrad void rv6xx_dpm_print_power_state(struct radeon_device *rdev,
   2013  1.1  riastrad 				 struct radeon_ps *rps)
   2014  1.1  riastrad {
   2015  1.1  riastrad 	struct rv6xx_ps *ps = rv6xx_get_ps(rps);
   2016  1.1  riastrad 	struct rv6xx_pl *pl;
   2017  1.1  riastrad 
   2018  1.1  riastrad 	r600_dpm_print_class_info(rps->class, rps->class2);
   2019  1.1  riastrad 	r600_dpm_print_cap_info(rps->caps);
   2020  1.1  riastrad 	printk("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   2021  1.1  riastrad 	pl = &ps->low;
   2022  1.1  riastrad 	printk("\t\tpower level 0    sclk: %u mclk: %u vddc: %u\n",
   2023  1.1  riastrad 	       pl->sclk, pl->mclk, pl->vddc);
   2024  1.1  riastrad 	pl = &ps->medium;
   2025  1.1  riastrad 	printk("\t\tpower level 1    sclk: %u mclk: %u vddc: %u\n",
   2026  1.1  riastrad 	       pl->sclk, pl->mclk, pl->vddc);
   2027  1.1  riastrad 	pl = &ps->high;
   2028  1.1  riastrad 	printk("\t\tpower level 2    sclk: %u mclk: %u vddc: %u\n",
   2029  1.1  riastrad 	       pl->sclk, pl->mclk, pl->vddc);
   2030  1.1  riastrad 	r600_dpm_print_ps_status(rdev, rps);
   2031  1.1  riastrad }
   2032  1.1  riastrad 
   2033  1.1  riastrad #ifdef CONFIG_DEBUG_FS
   2034  1.1  riastrad void rv6xx_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
   2035  1.1  riastrad 						       struct seq_file *m)
   2036  1.1  riastrad {
   2037  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2038  1.1  riastrad 	struct rv6xx_ps *ps = rv6xx_get_ps(rps);
   2039  1.1  riastrad 	struct rv6xx_pl *pl;
   2040  1.1  riastrad 	u32 current_index =
   2041  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2042  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2043  1.1  riastrad 
   2044  1.1  riastrad 	if (current_index > 2) {
   2045  1.1  riastrad 		seq_printf(m, "invalid dpm profile %d\n", current_index);
   2046  1.1  riastrad 	} else {
   2047  1.1  riastrad 		if (current_index == 0)
   2048  1.1  riastrad 			pl = &ps->low;
   2049  1.1  riastrad 		else if (current_index == 1)
   2050  1.1  riastrad 			pl = &ps->medium;
   2051  1.1  riastrad 		else /* current_index == 2 */
   2052  1.1  riastrad 			pl = &ps->high;
   2053  1.1  riastrad 		seq_printf(m, "uvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   2054  1.1  riastrad 		seq_printf(m, "power level %d    sclk: %u mclk: %u vddc: %u\n",
   2055  1.1  riastrad 			   current_index, pl->sclk, pl->mclk, pl->vddc);
   2056  1.1  riastrad 	}
   2057  1.1  riastrad }
   2058  1.1  riastrad #endif
   2059  1.1  riastrad 
   2060  1.1  riastrad /* get the current sclk in 10 khz units */
   2061  1.1  riastrad u32 rv6xx_dpm_get_current_sclk(struct radeon_device *rdev)
   2062  1.1  riastrad {
   2063  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2064  1.1  riastrad 	struct rv6xx_ps *ps = rv6xx_get_ps(rps);
   2065  1.1  riastrad 	struct rv6xx_pl *pl;
   2066  1.1  riastrad 	u32 current_index =
   2067  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2068  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2069  1.1  riastrad 
   2070  1.1  riastrad 	if (current_index > 2) {
   2071  1.1  riastrad 		return 0;
   2072  1.1  riastrad 	} else {
   2073  1.1  riastrad 		if (current_index == 0)
   2074  1.1  riastrad 			pl = &ps->low;
   2075  1.1  riastrad 		else if (current_index == 1)
   2076  1.1  riastrad 			pl = &ps->medium;
   2077  1.1  riastrad 		else /* current_index == 2 */
   2078  1.1  riastrad 			pl = &ps->high;
   2079  1.1  riastrad 		return pl->sclk;
   2080  1.1  riastrad 	}
   2081  1.1  riastrad }
   2082  1.1  riastrad 
   2083  1.1  riastrad /* get the current mclk in 10 khz units */
   2084  1.1  riastrad u32 rv6xx_dpm_get_current_mclk(struct radeon_device *rdev)
   2085  1.1  riastrad {
   2086  1.1  riastrad 	struct radeon_ps *rps = rdev->pm.dpm.current_ps;
   2087  1.1  riastrad 	struct rv6xx_ps *ps = rv6xx_get_ps(rps);
   2088  1.1  riastrad 	struct rv6xx_pl *pl;
   2089  1.1  riastrad 	u32 current_index =
   2090  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_PROFILE_INDEX_MASK) >>
   2091  1.1  riastrad 		CURRENT_PROFILE_INDEX_SHIFT;
   2092  1.1  riastrad 
   2093  1.1  riastrad 	if (current_index > 2) {
   2094  1.1  riastrad 		return 0;
   2095  1.1  riastrad 	} else {
   2096  1.1  riastrad 		if (current_index == 0)
   2097  1.1  riastrad 			pl = &ps->low;
   2098  1.1  riastrad 		else if (current_index == 1)
   2099  1.1  riastrad 			pl = &ps->medium;
   2100  1.1  riastrad 		else /* current_index == 2 */
   2101  1.1  riastrad 			pl = &ps->high;
   2102  1.1  riastrad 		return pl->mclk;
   2103  1.1  riastrad 	}
   2104  1.1  riastrad }
   2105  1.1  riastrad 
   2106  1.1  riastrad void rv6xx_dpm_fini(struct radeon_device *rdev)
   2107  1.1  riastrad {
   2108  1.1  riastrad 	int i;
   2109  1.1  riastrad 
   2110  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
   2111  1.1  riastrad 		kfree(rdev->pm.dpm.ps[i].ps_priv);
   2112  1.1  riastrad 	}
   2113  1.1  riastrad 	kfree(rdev->pm.dpm.ps);
   2114  1.1  riastrad 	kfree(rdev->pm.dpm.priv);
   2115  1.1  riastrad }
   2116  1.1  riastrad 
   2117  1.1  riastrad u32 rv6xx_dpm_get_sclk(struct radeon_device *rdev, bool low)
   2118  1.1  riastrad {
   2119  1.1  riastrad 	struct rv6xx_ps *requested_state = rv6xx_get_ps(rdev->pm.dpm.requested_ps);
   2120  1.1  riastrad 
   2121  1.1  riastrad 	if (low)
   2122  1.1  riastrad 		return requested_state->low.sclk;
   2123  1.1  riastrad 	else
   2124  1.1  riastrad 		return requested_state->high.sclk;
   2125  1.1  riastrad }
   2126  1.1  riastrad 
   2127  1.1  riastrad u32 rv6xx_dpm_get_mclk(struct radeon_device *rdev, bool low)
   2128  1.1  riastrad {
   2129  1.1  riastrad 	struct rv6xx_ps *requested_state = rv6xx_get_ps(rdev->pm.dpm.requested_ps);
   2130  1.1  riastrad 
   2131  1.1  riastrad 	if (low)
   2132  1.1  riastrad 		return requested_state->low.mclk;
   2133  1.1  riastrad 	else
   2134  1.1  riastrad 		return requested_state->high.mclk;
   2135  1.1  riastrad }
   2136  1.1  riastrad 
   2137  1.1  riastrad int rv6xx_dpm_force_performance_level(struct radeon_device *rdev,
   2138  1.1  riastrad 				      enum radeon_dpm_forced_level level)
   2139  1.1  riastrad {
   2140  1.1  riastrad 	struct rv6xx_power_info *pi = rv6xx_get_pi(rdev);
   2141  1.1  riastrad 
   2142  1.1  riastrad 	if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
   2143  1.1  riastrad 		pi->restricted_levels = 3;
   2144  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
   2145  1.1  riastrad 		pi->restricted_levels = 2;
   2146  1.1  riastrad 	} else {
   2147  1.1  riastrad 		pi->restricted_levels = 0;
   2148  1.1  riastrad 	}
   2149  1.1  riastrad 
   2150  1.1  riastrad 	rv6xx_clear_vc(rdev);
   2151  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, true);
   2152  1.1  riastrad 	r600_set_at(rdev, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
   2153  1.1  riastrad 	r600_wait_for_power_level(rdev, R600_POWER_LEVEL_LOW);
   2154  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_HIGH, false);
   2155  1.1  riastrad 	r600_power_level_enable(rdev, R600_POWER_LEVEL_MEDIUM, false);
   2156  1.1  riastrad 	rv6xx_enable_medium(rdev);
   2157  1.1  riastrad 	rv6xx_enable_high(rdev);
   2158  1.1  riastrad 	if (pi->restricted_levels == 3)
   2159  1.1  riastrad 		r600_power_level_enable(rdev, R600_POWER_LEVEL_LOW, false);
   2160  1.1  riastrad 	rv6xx_program_vc(rdev);
   2161  1.1  riastrad 	rv6xx_program_at(rdev);
   2162  1.1  riastrad 
   2163  1.1  riastrad 	rdev->pm.dpm.forced_level = level;
   2164  1.1  riastrad 
   2165  1.1  riastrad 	return 0;
   2166  1.1  riastrad }
   2167