Home | History | Annotate | Line # | Download | only in radeon
      1  1.7       mrg /*	$NetBSD: radeon_ci_dpm.c,v 1.7 2023/09/30 10:46:45 mrg Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2013 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  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.7       mrg __KERNEL_RCSID(0, "$NetBSD: radeon_ci_dpm.c,v 1.7 2023/09/30 10:46:45 mrg Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/firmware.h>
     30  1.4  riastrad #include <linux/pci.h>
     31  1.4  riastrad #include <linux/seq_file.h>
     32  1.4  riastrad 
     33  1.4  riastrad #include "atom.h"
     34  1.4  riastrad #include "ci_dpm.h"
     35  1.4  riastrad #include "cikd.h"
     36  1.4  riastrad #include "r600_dpm.h"
     37  1.1  riastrad #include "radeon.h"
     38  1.1  riastrad #include "radeon_asic.h"
     39  1.1  riastrad #include "radeon_ucode.h"
     40  1.1  riastrad 
     41  1.1  riastrad #define MC_CG_ARB_FREQ_F0           0x0a
     42  1.1  riastrad #define MC_CG_ARB_FREQ_F1           0x0b
     43  1.1  riastrad #define MC_CG_ARB_FREQ_F2           0x0c
     44  1.1  riastrad #define MC_CG_ARB_FREQ_F3           0x0d
     45  1.1  riastrad 
     46  1.1  riastrad #define SMC_RAM_END 0x40000
     47  1.1  riastrad 
     48  1.1  riastrad #define VOLTAGE_SCALE               4
     49  1.1  riastrad #define VOLTAGE_VID_OFFSET_SCALE1    625
     50  1.1  riastrad #define VOLTAGE_VID_OFFSET_SCALE2    100
     51  1.1  riastrad 
     52  1.1  riastrad static const struct ci_pt_defaults defaults_hawaii_xt =
     53  1.1  riastrad {
     54  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0xB0000,
     55  1.1  riastrad 	{ 0x2E,  0x00,  0x00,  0x88,  0x00,  0x00,  0x72,  0x60,  0x51,  0xA7,  0x79,  0x6B,  0x90,  0xBD,  0x79  },
     56  1.1  riastrad 	{ 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
     57  1.1  riastrad };
     58  1.1  riastrad 
     59  1.1  riastrad static const struct ci_pt_defaults defaults_hawaii_pro =
     60  1.1  riastrad {
     61  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0x65062,
     62  1.1  riastrad 	{ 0x2E,  0x00,  0x00,  0x88,  0x00,  0x00,  0x72,  0x60,  0x51,  0xA7,  0x79,  0x6B,  0x90,  0xBD,  0x79  },
     63  1.1  riastrad 	{ 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
     64  1.1  riastrad };
     65  1.1  riastrad 
     66  1.1  riastrad static const struct ci_pt_defaults defaults_bonaire_xt =
     67  1.1  riastrad {
     68  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
     69  1.1  riastrad 	{ 0x79,  0x253, 0x25D, 0xAE,  0x72,  0x80,  0x83,  0x86,  0x6F,  0xC8,  0xC9,  0xC9,  0x2F,  0x4D,  0x61  },
     70  1.1  riastrad 	{ 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
     71  1.1  riastrad };
     72  1.1  riastrad 
     73  1.1  riastrad static const struct ci_pt_defaults defaults_bonaire_pro __unused =
     74  1.1  riastrad {
     75  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x65062,
     76  1.1  riastrad 	{ 0x8C,  0x23F, 0x244, 0xA6,  0x83,  0x85,  0x86,  0x86,  0x83,  0xDB,  0xDB,  0xDA,  0x67,  0x60,  0x5F  },
     77  1.1  riastrad 	{ 0x187, 0x193, 0x193, 0x1C7, 0x1D1, 0x1D1, 0x210, 0x219, 0x219, 0x266, 0x26C, 0x26C, 0x2C9, 0x2CB, 0x2CB }
     78  1.1  riastrad };
     79  1.1  riastrad 
     80  1.1  riastrad static const struct ci_pt_defaults defaults_saturn_xt =
     81  1.1  riastrad {
     82  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x70000,
     83  1.1  riastrad 	{ 0x8C,  0x247, 0x249, 0xA6,  0x80,  0x81,  0x8B,  0x89,  0x86,  0xC9,  0xCA,  0xC9,  0x4D,  0x4D,  0x4D  },
     84  1.1  riastrad 	{ 0x187, 0x187, 0x187, 0x1C7, 0x1C7, 0x1C7, 0x210, 0x210, 0x210, 0x266, 0x266, 0x266, 0x2C9, 0x2C9, 0x2C9 }
     85  1.1  riastrad };
     86  1.1  riastrad 
     87  1.1  riastrad static const struct ci_pt_defaults defaults_saturn_pro __unused =
     88  1.1  riastrad {
     89  1.1  riastrad 	1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x30000,
     90  1.1  riastrad 	{ 0x96,  0x21D, 0x23B, 0xA1,  0x85,  0x87,  0x83,  0x84,  0x81,  0xE6,  0xE6,  0xE6,  0x71,  0x6A,  0x6A  },
     91  1.1  riastrad 	{ 0x193, 0x19E, 0x19E, 0x1D2, 0x1DC, 0x1DC, 0x21A, 0x223, 0x223, 0x26E, 0x27E, 0x274, 0x2CF, 0x2D2, 0x2D2 }
     92  1.1  riastrad };
     93  1.1  riastrad 
     94  1.1  riastrad static const struct ci_pt_config_reg didt_config_ci[] =
     95  1.1  riastrad {
     96  1.1  riastrad 	{ 0x10, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
     97  1.1  riastrad 	{ 0x10, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
     98  1.1  riastrad 	{ 0x10, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
     99  1.1  riastrad 	{ 0x10, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    100  1.1  riastrad 	{ 0x11, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    101  1.1  riastrad 	{ 0x11, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    102  1.1  riastrad 	{ 0x11, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    103  1.1  riastrad 	{ 0x11, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    104  1.1  riastrad 	{ 0x12, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    105  1.1  riastrad 	{ 0x12, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    106  1.1  riastrad 	{ 0x12, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    107  1.1  riastrad 	{ 0x12, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    108  1.1  riastrad 	{ 0x2, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
    109  1.1  riastrad 	{ 0x2, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
    110  1.1  riastrad 	{ 0x2, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
    111  1.1  riastrad 	{ 0x1, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    112  1.1  riastrad 	{ 0x1, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    113  1.1  riastrad 	{ 0x0, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    114  1.1  riastrad 	{ 0x30, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    115  1.1  riastrad 	{ 0x30, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    116  1.1  riastrad 	{ 0x30, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    117  1.1  riastrad 	{ 0x30, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    118  1.1  riastrad 	{ 0x31, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    119  1.1  riastrad 	{ 0x31, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    120  1.1  riastrad 	{ 0x31, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    121  1.1  riastrad 	{ 0x31, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    122  1.1  riastrad 	{ 0x32, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    123  1.1  riastrad 	{ 0x32, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    124  1.1  riastrad 	{ 0x32, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    125  1.1  riastrad 	{ 0x32, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    126  1.1  riastrad 	{ 0x22, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
    127  1.1  riastrad 	{ 0x22, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
    128  1.1  riastrad 	{ 0x22, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
    129  1.1  riastrad 	{ 0x21, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    130  1.1  riastrad 	{ 0x21, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    131  1.1  riastrad 	{ 0x20, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    132  1.1  riastrad 	{ 0x50, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    133  1.1  riastrad 	{ 0x50, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    134  1.1  riastrad 	{ 0x50, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    135  1.1  riastrad 	{ 0x50, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    136  1.1  riastrad 	{ 0x51, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    137  1.1  riastrad 	{ 0x51, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    138  1.1  riastrad 	{ 0x51, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    139  1.1  riastrad 	{ 0x51, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    140  1.1  riastrad 	{ 0x52, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    141  1.1  riastrad 	{ 0x52, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    142  1.1  riastrad 	{ 0x52, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    143  1.1  riastrad 	{ 0x52, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    144  1.1  riastrad 	{ 0x42, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
    145  1.1  riastrad 	{ 0x42, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
    146  1.1  riastrad 	{ 0x42, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
    147  1.1  riastrad 	{ 0x41, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    148  1.1  riastrad 	{ 0x41, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    149  1.1  riastrad 	{ 0x40, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    150  1.1  riastrad 	{ 0x70, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    151  1.1  riastrad 	{ 0x70, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    152  1.1  riastrad 	{ 0x70, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    153  1.1  riastrad 	{ 0x70, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    154  1.1  riastrad 	{ 0x71, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    155  1.1  riastrad 	{ 0x71, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    156  1.1  riastrad 	{ 0x71, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    157  1.1  riastrad 	{ 0x71, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    158  1.1  riastrad 	{ 0x72, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    159  1.1  riastrad 	{ 0x72, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    160  1.1  riastrad 	{ 0x72, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    161  1.1  riastrad 	{ 0x72, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    162  1.1  riastrad 	{ 0x62, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
    163  1.1  riastrad 	{ 0x62, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
    164  1.1  riastrad 	{ 0x62, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
    165  1.1  riastrad 	{ 0x61, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    166  1.1  riastrad 	{ 0x61, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
    167  1.1  riastrad 	{ 0x60, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
    168  1.1  riastrad 	{ 0xFFFFFFFF }
    169  1.1  riastrad };
    170  1.1  riastrad 
    171  1.1  riastrad extern u8 rv770_get_memory_module_index(struct radeon_device *rdev);
    172  1.1  riastrad extern int ni_copy_and_switch_arb_sets(struct radeon_device *rdev,
    173  1.1  riastrad 				       u32 arb_freq_src, u32 arb_freq_dest);
    174  1.1  riastrad extern u8 si_get_ddr3_mclk_frequency_ratio(u32 memory_clock);
    175  1.1  riastrad extern u8 si_get_mclk_frequency_ratio(u32 memory_clock, bool strobe_mode);
    176  1.1  riastrad extern void si_trim_voltage_table_to_fit_state_table(struct radeon_device *rdev,
    177  1.1  riastrad 						     u32 max_voltage_steps,
    178  1.1  riastrad 						     struct atom_voltage_table *voltage_table);
    179  1.1  riastrad extern void cik_enter_rlc_safe_mode(struct radeon_device *rdev);
    180  1.1  riastrad extern void cik_exit_rlc_safe_mode(struct radeon_device *rdev);
    181  1.1  riastrad extern int ci_mc_load_microcode(struct radeon_device *rdev);
    182  1.1  riastrad extern void cik_update_cg(struct radeon_device *rdev,
    183  1.1  riastrad 			  u32 block, bool enable);
    184  1.1  riastrad 
    185  1.1  riastrad static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
    186  1.1  riastrad 					 struct atom_voltage_table_entry *voltage_table,
    187  1.1  riastrad 					 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd);
    188  1.1  riastrad static int ci_set_power_limit(struct radeon_device *rdev, u32 n);
    189  1.1  riastrad static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
    190  1.1  riastrad 				       u32 target_tdp);
    191  1.1  riastrad static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate);
    192  1.1  riastrad 
    193  1.4  riastrad static PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg);
    194  1.1  riastrad static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
    195  1.1  riastrad 						      PPSMC_Msg msg, u32 parameter);
    196  1.1  riastrad 
    197  1.1  riastrad static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev);
    198  1.1  riastrad static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev);
    199  1.1  riastrad 
    200  1.1  riastrad static struct ci_power_info *ci_get_pi(struct radeon_device *rdev)
    201  1.1  riastrad {
    202  1.4  riastrad 	struct ci_power_info *pi = rdev->pm.dpm.priv;
    203  1.1  riastrad 
    204  1.4  riastrad 	return pi;
    205  1.1  riastrad }
    206  1.1  riastrad 
    207  1.1  riastrad static struct ci_ps *ci_get_ps(struct radeon_ps *rps)
    208  1.1  riastrad {
    209  1.1  riastrad 	struct ci_ps *ps = rps->ps_priv;
    210  1.1  riastrad 
    211  1.1  riastrad 	return ps;
    212  1.1  riastrad }
    213  1.1  riastrad 
    214  1.1  riastrad static void ci_initialize_powertune_defaults(struct radeon_device *rdev)
    215  1.1  riastrad {
    216  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    217  1.1  riastrad 
    218  1.1  riastrad 	switch (rdev->pdev->device) {
    219  1.1  riastrad 	case 0x6649:
    220  1.1  riastrad 	case 0x6650:
    221  1.1  riastrad 	case 0x6651:
    222  1.1  riastrad 	case 0x6658:
    223  1.1  riastrad 	case 0x665C:
    224  1.1  riastrad 	case 0x665D:
    225  1.1  riastrad 	default:
    226  1.1  riastrad 		pi->powertune_defaults = &defaults_bonaire_xt;
    227  1.1  riastrad 		break;
    228  1.1  riastrad 	case 0x6640:
    229  1.1  riastrad 	case 0x6641:
    230  1.1  riastrad 	case 0x6646:
    231  1.1  riastrad 	case 0x6647:
    232  1.1  riastrad 		pi->powertune_defaults = &defaults_saturn_xt;
    233  1.1  riastrad 		break;
    234  1.1  riastrad 	case 0x67B8:
    235  1.1  riastrad 	case 0x67B0:
    236  1.1  riastrad 		pi->powertune_defaults = &defaults_hawaii_xt;
    237  1.1  riastrad 		break;
    238  1.1  riastrad 	case 0x67BA:
    239  1.1  riastrad 	case 0x67B1:
    240  1.1  riastrad 		pi->powertune_defaults = &defaults_hawaii_pro;
    241  1.1  riastrad 		break;
    242  1.1  riastrad 	case 0x67A0:
    243  1.1  riastrad 	case 0x67A1:
    244  1.1  riastrad 	case 0x67A2:
    245  1.1  riastrad 	case 0x67A8:
    246  1.1  riastrad 	case 0x67A9:
    247  1.1  riastrad 	case 0x67AA:
    248  1.1  riastrad 	case 0x67B9:
    249  1.1  riastrad 	case 0x67BE:
    250  1.1  riastrad 		pi->powertune_defaults = &defaults_bonaire_xt;
    251  1.1  riastrad 		break;
    252  1.1  riastrad 	}
    253  1.1  riastrad 
    254  1.1  riastrad 	pi->dte_tj_offset = 0;
    255  1.1  riastrad 
    256  1.1  riastrad 	pi->caps_power_containment = true;
    257  1.1  riastrad 	pi->caps_cac = false;
    258  1.1  riastrad 	pi->caps_sq_ramping = false;
    259  1.1  riastrad 	pi->caps_db_ramping = false;
    260  1.1  riastrad 	pi->caps_td_ramping = false;
    261  1.1  riastrad 	pi->caps_tcp_ramping = false;
    262  1.1  riastrad 
    263  1.1  riastrad 	if (pi->caps_power_containment) {
    264  1.1  riastrad 		pi->caps_cac = true;
    265  1.1  riastrad 		if (rdev->family == CHIP_HAWAII)
    266  1.1  riastrad 			pi->enable_bapm_feature = false;
    267  1.1  riastrad 		else
    268  1.1  riastrad 			pi->enable_bapm_feature = true;
    269  1.1  riastrad 		pi->enable_tdc_limit_feature = true;
    270  1.1  riastrad 		pi->enable_pkg_pwr_tracking_feature = true;
    271  1.1  riastrad 	}
    272  1.1  riastrad }
    273  1.1  riastrad 
    274  1.1  riastrad static u8 ci_convert_to_vid(u16 vddc)
    275  1.1  riastrad {
    276  1.1  riastrad 	return (6200 - (vddc * VOLTAGE_SCALE)) / 25;
    277  1.1  riastrad }
    278  1.1  riastrad 
    279  1.1  riastrad static int ci_populate_bapm_vddc_vid_sidd(struct radeon_device *rdev)
    280  1.1  riastrad {
    281  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    282  1.1  riastrad 	u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
    283  1.1  riastrad 	u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
    284  1.1  riastrad 	u8 *hi2_vid = pi->smc_powertune_table.BapmVddCVidHiSidd2;
    285  1.1  riastrad 	u32 i;
    286  1.1  riastrad 
    287  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries == NULL)
    288  1.1  riastrad 		return -EINVAL;
    289  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.cac_leakage_table.count > 8)
    290  1.1  riastrad 		return -EINVAL;
    291  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.cac_leakage_table.count !=
    292  1.1  riastrad 	    rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count)
    293  1.1  riastrad 		return -EINVAL;
    294  1.1  riastrad 
    295  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.dyn_state.cac_leakage_table.count; i++) {
    296  1.1  riastrad 		if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
    297  1.1  riastrad 			lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc1);
    298  1.1  riastrad 			hi_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc2);
    299  1.1  riastrad 			hi2_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc3);
    300  1.1  riastrad 		} else {
    301  1.1  riastrad 			lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc);
    302  1.1  riastrad 			hi_vid[i] = ci_convert_to_vid((u16)rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].leakage);
    303  1.1  riastrad 		}
    304  1.1  riastrad 	}
    305  1.1  riastrad 	return 0;
    306  1.1  riastrad }
    307  1.1  riastrad 
    308  1.1  riastrad static int ci_populate_vddc_vid(struct radeon_device *rdev)
    309  1.1  riastrad {
    310  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    311  1.1  riastrad 	u8 *vid = pi->smc_powertune_table.VddCVid;
    312  1.1  riastrad 	u32 i;
    313  1.1  riastrad 
    314  1.1  riastrad 	if (pi->vddc_voltage_table.count > 8)
    315  1.1  riastrad 		return -EINVAL;
    316  1.1  riastrad 
    317  1.1  riastrad 	for (i = 0; i < pi->vddc_voltage_table.count; i++)
    318  1.1  riastrad 		vid[i] = ci_convert_to_vid(pi->vddc_voltage_table.entries[i].value);
    319  1.1  riastrad 
    320  1.1  riastrad 	return 0;
    321  1.1  riastrad }
    322  1.1  riastrad 
    323  1.1  riastrad static int ci_populate_svi_load_line(struct radeon_device *rdev)
    324  1.1  riastrad {
    325  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    326  1.1  riastrad 	const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
    327  1.1  riastrad 
    328  1.1  riastrad 	pi->smc_powertune_table.SviLoadLineEn = pt_defaults->svi_load_line_en;
    329  1.1  riastrad 	pi->smc_powertune_table.SviLoadLineVddC = pt_defaults->svi_load_line_vddc;
    330  1.1  riastrad 	pi->smc_powertune_table.SviLoadLineTrimVddC = 3;
    331  1.1  riastrad 	pi->smc_powertune_table.SviLoadLineOffsetVddC = 0;
    332  1.1  riastrad 
    333  1.1  riastrad 	return 0;
    334  1.1  riastrad }
    335  1.1  riastrad 
    336  1.1  riastrad static int ci_populate_tdc_limit(struct radeon_device *rdev)
    337  1.1  riastrad {
    338  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    339  1.1  riastrad 	const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
    340  1.1  riastrad 	u16 tdc_limit;
    341  1.1  riastrad 
    342  1.1  riastrad 	tdc_limit = rdev->pm.dpm.dyn_state.cac_tdp_table->tdc * 256;
    343  1.1  riastrad 	pi->smc_powertune_table.TDC_VDDC_PkgLimit = cpu_to_be16(tdc_limit);
    344  1.1  riastrad 	pi->smc_powertune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
    345  1.1  riastrad 		pt_defaults->tdc_vddc_throttle_release_limit_perc;
    346  1.1  riastrad 	pi->smc_powertune_table.TDC_MAWt = pt_defaults->tdc_mawt;
    347  1.1  riastrad 
    348  1.1  riastrad 	return 0;
    349  1.1  riastrad }
    350  1.1  riastrad 
    351  1.1  riastrad static int ci_populate_dw8(struct radeon_device *rdev)
    352  1.1  riastrad {
    353  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    354  1.1  riastrad 	const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
    355  1.1  riastrad 	int ret;
    356  1.1  riastrad 
    357  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
    358  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
    359  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, PmFuseTable) +
    360  1.1  riastrad 				     offsetof(SMU7_Discrete_PmFuses, TdcWaterfallCtl),
    361  1.1  riastrad 				     (u32 *)&pi->smc_powertune_table.TdcWaterfallCtl,
    362  1.1  riastrad 				     pi->sram_end);
    363  1.1  riastrad 	if (ret)
    364  1.1  riastrad 		return -EINVAL;
    365  1.1  riastrad 	else
    366  1.1  riastrad 		pi->smc_powertune_table.TdcWaterfallCtl = pt_defaults->tdc_waterfall_ctl;
    367  1.1  riastrad 
    368  1.1  riastrad 	return 0;
    369  1.1  riastrad }
    370  1.1  riastrad 
    371  1.1  riastrad static int ci_populate_fuzzy_fan(struct radeon_device *rdev)
    372  1.1  riastrad {
    373  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    374  1.1  riastrad 
    375  1.1  riastrad 	if ((rdev->pm.dpm.fan.fan_output_sensitivity & (1 << 15)) ||
    376  1.1  riastrad 	    (rdev->pm.dpm.fan.fan_output_sensitivity == 0))
    377  1.1  riastrad 		rdev->pm.dpm.fan.fan_output_sensitivity =
    378  1.1  riastrad 			rdev->pm.dpm.fan.default_fan_output_sensitivity;
    379  1.1  riastrad 
    380  1.1  riastrad 	pi->smc_powertune_table.FuzzyFan_PwmSetDelta =
    381  1.1  riastrad 		cpu_to_be16(rdev->pm.dpm.fan.fan_output_sensitivity);
    382  1.1  riastrad 
    383  1.1  riastrad 	return 0;
    384  1.1  riastrad }
    385  1.1  riastrad 
    386  1.1  riastrad static int ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(struct radeon_device *rdev)
    387  1.1  riastrad {
    388  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    389  1.1  riastrad 	u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
    390  1.1  riastrad 	u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
    391  1.3  riastrad 	int i, min, max;
    392  1.1  riastrad 
    393  1.3  riastrad 	min = max = hi_vid[0];
    394  1.1  riastrad 	for (i = 0; i < 8; i++) {
    395  1.1  riastrad 		if (0 != hi_vid[i]) {
    396  1.3  riastrad 			if (min > hi_vid[i])
    397  1.3  riastrad 				min = hi_vid[i];
    398  1.3  riastrad 			if (max < hi_vid[i])
    399  1.3  riastrad 				max = hi_vid[i];
    400  1.1  riastrad 		}
    401  1.1  riastrad 
    402  1.1  riastrad 		if (0 != lo_vid[i]) {
    403  1.3  riastrad 			if (min > lo_vid[i])
    404  1.3  riastrad 				min = lo_vid[i];
    405  1.3  riastrad 			if (max < lo_vid[i])
    406  1.3  riastrad 				max = lo_vid[i];
    407  1.1  riastrad 		}
    408  1.1  riastrad 	}
    409  1.1  riastrad 
    410  1.3  riastrad 	if ((min == 0) || (max == 0))
    411  1.1  riastrad 		return -EINVAL;
    412  1.3  riastrad 	pi->smc_powertune_table.GnbLPMLMaxVid = (u8)max;
    413  1.3  riastrad 	pi->smc_powertune_table.GnbLPMLMinVid = (u8)min;
    414  1.1  riastrad 
    415  1.1  riastrad 	return 0;
    416  1.1  riastrad }
    417  1.1  riastrad 
    418  1.1  riastrad static int ci_populate_bapm_vddc_base_leakage_sidd(struct radeon_device *rdev)
    419  1.1  riastrad {
    420  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    421  1.1  riastrad 	u16 hi_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd;
    422  1.1  riastrad 	u16 lo_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd;
    423  1.1  riastrad 	struct radeon_cac_tdp_table *cac_tdp_table =
    424  1.1  riastrad 		rdev->pm.dpm.dyn_state.cac_tdp_table;
    425  1.1  riastrad 
    426  1.1  riastrad 	hi_sidd = cac_tdp_table->high_cac_leakage / 100 * 256;
    427  1.1  riastrad 	lo_sidd = cac_tdp_table->low_cac_leakage / 100 * 256;
    428  1.1  riastrad 
    429  1.1  riastrad 	pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd = cpu_to_be16(hi_sidd);
    430  1.1  riastrad 	pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd = cpu_to_be16(lo_sidd);
    431  1.1  riastrad 
    432  1.1  riastrad 	return 0;
    433  1.1  riastrad }
    434  1.1  riastrad 
    435  1.1  riastrad static int ci_populate_bapm_parameters_in_dpm_table(struct radeon_device *rdev)
    436  1.1  riastrad {
    437  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    438  1.1  riastrad 	const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
    439  1.1  riastrad 	SMU7_Discrete_DpmTable  *dpm_table = &pi->smc_state_table;
    440  1.1  riastrad 	struct radeon_cac_tdp_table *cac_tdp_table =
    441  1.1  riastrad 		rdev->pm.dpm.dyn_state.cac_tdp_table;
    442  1.1  riastrad 	struct radeon_ppm_table *ppm = rdev->pm.dpm.dyn_state.ppm_table;
    443  1.1  riastrad 	int i, j, k;
    444  1.1  riastrad 	const u16 *def1;
    445  1.1  riastrad 	const u16 *def2;
    446  1.1  riastrad 
    447  1.1  riastrad 	dpm_table->DefaultTdp = cac_tdp_table->tdp * 256;
    448  1.1  riastrad 	dpm_table->TargetTdp = cac_tdp_table->configurable_tdp * 256;
    449  1.1  riastrad 
    450  1.1  riastrad 	dpm_table->DTETjOffset = (u8)pi->dte_tj_offset;
    451  1.1  riastrad 	dpm_table->GpuTjMax =
    452  1.1  riastrad 		(u8)(pi->thermal_temp_setting.temperature_high / 1000);
    453  1.1  riastrad 	dpm_table->GpuTjHyst = 8;
    454  1.1  riastrad 
    455  1.1  riastrad 	dpm_table->DTEAmbientTempBase = pt_defaults->dte_ambient_temp_base;
    456  1.1  riastrad 
    457  1.1  riastrad 	if (ppm) {
    458  1.1  riastrad 		dpm_table->PPM_PkgPwrLimit = cpu_to_be16((u16)ppm->dgpu_tdp * 256 / 1000);
    459  1.1  riastrad 		dpm_table->PPM_TemperatureLimit = cpu_to_be16((u16)ppm->tj_max * 256);
    460  1.1  riastrad 	} else {
    461  1.1  riastrad 		dpm_table->PPM_PkgPwrLimit = cpu_to_be16(0);
    462  1.1  riastrad 		dpm_table->PPM_TemperatureLimit = cpu_to_be16(0);
    463  1.1  riastrad 	}
    464  1.1  riastrad 
    465  1.1  riastrad 	dpm_table->BAPM_TEMP_GRADIENT = cpu_to_be32(pt_defaults->bapm_temp_gradient);
    466  1.1  riastrad 	def1 = pt_defaults->bapmti_r;
    467  1.1  riastrad 	def2 = pt_defaults->bapmti_rc;
    468  1.1  riastrad 
    469  1.1  riastrad 	for (i = 0; i < SMU7_DTE_ITERATIONS; i++) {
    470  1.1  riastrad 		for (j = 0; j < SMU7_DTE_SOURCES; j++) {
    471  1.1  riastrad 			for (k = 0; k < SMU7_DTE_SINKS; k++) {
    472  1.1  riastrad 				dpm_table->BAPMTI_R[i][j][k] = cpu_to_be16(*def1);
    473  1.1  riastrad 				dpm_table->BAPMTI_RC[i][j][k] = cpu_to_be16(*def2);
    474  1.1  riastrad 				def1++;
    475  1.1  riastrad 				def2++;
    476  1.1  riastrad 			}
    477  1.1  riastrad 		}
    478  1.1  riastrad 	}
    479  1.1  riastrad 
    480  1.1  riastrad 	return 0;
    481  1.1  riastrad }
    482  1.1  riastrad 
    483  1.1  riastrad static int ci_populate_pm_base(struct radeon_device *rdev)
    484  1.1  riastrad {
    485  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    486  1.1  riastrad 	u32 pm_fuse_table_offset;
    487  1.1  riastrad 	int ret;
    488  1.1  riastrad 
    489  1.1  riastrad 	if (pi->caps_power_containment) {
    490  1.1  riastrad 		ret = ci_read_smc_sram_dword(rdev,
    491  1.1  riastrad 					     SMU7_FIRMWARE_HEADER_LOCATION +
    492  1.1  riastrad 					     offsetof(SMU7_Firmware_Header, PmFuseTable),
    493  1.1  riastrad 					     &pm_fuse_table_offset, pi->sram_end);
    494  1.1  riastrad 		if (ret)
    495  1.1  riastrad 			return ret;
    496  1.1  riastrad 		ret = ci_populate_bapm_vddc_vid_sidd(rdev);
    497  1.1  riastrad 		if (ret)
    498  1.1  riastrad 			return ret;
    499  1.1  riastrad 		ret = ci_populate_vddc_vid(rdev);
    500  1.1  riastrad 		if (ret)
    501  1.1  riastrad 			return ret;
    502  1.1  riastrad 		ret = ci_populate_svi_load_line(rdev);
    503  1.1  riastrad 		if (ret)
    504  1.1  riastrad 			return ret;
    505  1.1  riastrad 		ret = ci_populate_tdc_limit(rdev);
    506  1.1  riastrad 		if (ret)
    507  1.1  riastrad 			return ret;
    508  1.1  riastrad 		ret = ci_populate_dw8(rdev);
    509  1.1  riastrad 		if (ret)
    510  1.1  riastrad 			return ret;
    511  1.1  riastrad 		ret = ci_populate_fuzzy_fan(rdev);
    512  1.1  riastrad 		if (ret)
    513  1.1  riastrad 			return ret;
    514  1.1  riastrad 		ret = ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(rdev);
    515  1.1  riastrad 		if (ret)
    516  1.1  riastrad 			return ret;
    517  1.1  riastrad 		ret = ci_populate_bapm_vddc_base_leakage_sidd(rdev);
    518  1.1  riastrad 		if (ret)
    519  1.1  riastrad 			return ret;
    520  1.1  riastrad 		ret = ci_copy_bytes_to_smc(rdev, pm_fuse_table_offset,
    521  1.1  riastrad 					   (u8 *)&pi->smc_powertune_table,
    522  1.1  riastrad 					   sizeof(SMU7_Discrete_PmFuses), pi->sram_end);
    523  1.1  riastrad 		if (ret)
    524  1.1  riastrad 			return ret;
    525  1.1  riastrad 	}
    526  1.1  riastrad 
    527  1.1  riastrad 	return 0;
    528  1.1  riastrad }
    529  1.1  riastrad 
    530  1.1  riastrad static void ci_do_enable_didt(struct radeon_device *rdev, const bool enable)
    531  1.1  riastrad {
    532  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    533  1.1  riastrad 	u32 data;
    534  1.1  riastrad 
    535  1.1  riastrad 	if (pi->caps_sq_ramping) {
    536  1.1  riastrad 		data = RREG32_DIDT(DIDT_SQ_CTRL0);
    537  1.1  riastrad 		if (enable)
    538  1.1  riastrad 			data |= DIDT_CTRL_EN;
    539  1.1  riastrad 		else
    540  1.1  riastrad 			data &= ~DIDT_CTRL_EN;
    541  1.1  riastrad 		WREG32_DIDT(DIDT_SQ_CTRL0, data);
    542  1.1  riastrad 	}
    543  1.1  riastrad 
    544  1.1  riastrad 	if (pi->caps_db_ramping) {
    545  1.1  riastrad 		data = RREG32_DIDT(DIDT_DB_CTRL0);
    546  1.1  riastrad 		if (enable)
    547  1.1  riastrad 			data |= DIDT_CTRL_EN;
    548  1.1  riastrad 		else
    549  1.1  riastrad 			data &= ~DIDT_CTRL_EN;
    550  1.1  riastrad 		WREG32_DIDT(DIDT_DB_CTRL0, data);
    551  1.1  riastrad 	}
    552  1.1  riastrad 
    553  1.1  riastrad 	if (pi->caps_td_ramping) {
    554  1.1  riastrad 		data = RREG32_DIDT(DIDT_TD_CTRL0);
    555  1.1  riastrad 		if (enable)
    556  1.1  riastrad 			data |= DIDT_CTRL_EN;
    557  1.1  riastrad 		else
    558  1.1  riastrad 			data &= ~DIDT_CTRL_EN;
    559  1.1  riastrad 		WREG32_DIDT(DIDT_TD_CTRL0, data);
    560  1.1  riastrad 	}
    561  1.1  riastrad 
    562  1.1  riastrad 	if (pi->caps_tcp_ramping) {
    563  1.1  riastrad 		data = RREG32_DIDT(DIDT_TCP_CTRL0);
    564  1.1  riastrad 		if (enable)
    565  1.1  riastrad 			data |= DIDT_CTRL_EN;
    566  1.1  riastrad 		else
    567  1.1  riastrad 			data &= ~DIDT_CTRL_EN;
    568  1.1  riastrad 		WREG32_DIDT(DIDT_TCP_CTRL0, data);
    569  1.1  riastrad 	}
    570  1.1  riastrad }
    571  1.1  riastrad 
    572  1.1  riastrad static int ci_program_pt_config_registers(struct radeon_device *rdev,
    573  1.1  riastrad 					  const struct ci_pt_config_reg *cac_config_regs)
    574  1.1  riastrad {
    575  1.1  riastrad 	const struct ci_pt_config_reg *config_regs = cac_config_regs;
    576  1.1  riastrad 	u32 data;
    577  1.1  riastrad 	u32 cache = 0;
    578  1.1  riastrad 
    579  1.1  riastrad 	if (config_regs == NULL)
    580  1.1  riastrad 		return -EINVAL;
    581  1.1  riastrad 
    582  1.1  riastrad 	while (config_regs->offset != 0xFFFFFFFF) {
    583  1.1  riastrad 		if (config_regs->type == CISLANDS_CONFIGREG_CACHE) {
    584  1.1  riastrad 			cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
    585  1.1  riastrad 		} else {
    586  1.1  riastrad 			switch (config_regs->type) {
    587  1.1  riastrad 			case CISLANDS_CONFIGREG_SMC_IND:
    588  1.1  riastrad 				data = RREG32_SMC(config_regs->offset);
    589  1.1  riastrad 				break;
    590  1.1  riastrad 			case CISLANDS_CONFIGREG_DIDT_IND:
    591  1.1  riastrad 				data = RREG32_DIDT(config_regs->offset);
    592  1.1  riastrad 				break;
    593  1.1  riastrad 			default:
    594  1.1  riastrad 				data = RREG32(config_regs->offset << 2);
    595  1.1  riastrad 				break;
    596  1.1  riastrad 			}
    597  1.1  riastrad 
    598  1.1  riastrad 			data &= ~config_regs->mask;
    599  1.1  riastrad 			data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
    600  1.1  riastrad 			data |= cache;
    601  1.1  riastrad 
    602  1.1  riastrad 			switch (config_regs->type) {
    603  1.1  riastrad 			case CISLANDS_CONFIGREG_SMC_IND:
    604  1.1  riastrad 				WREG32_SMC(config_regs->offset, data);
    605  1.1  riastrad 				break;
    606  1.1  riastrad 			case CISLANDS_CONFIGREG_DIDT_IND:
    607  1.1  riastrad 				WREG32_DIDT(config_regs->offset, data);
    608  1.1  riastrad 				break;
    609  1.1  riastrad 			default:
    610  1.1  riastrad 				WREG32(config_regs->offset << 2, data);
    611  1.1  riastrad 				break;
    612  1.1  riastrad 			}
    613  1.1  riastrad 			cache = 0;
    614  1.1  riastrad 		}
    615  1.1  riastrad 		config_regs++;
    616  1.1  riastrad 	}
    617  1.1  riastrad 	return 0;
    618  1.1  riastrad }
    619  1.1  riastrad 
    620  1.1  riastrad static int ci_enable_didt(struct radeon_device *rdev, bool enable)
    621  1.1  riastrad {
    622  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    623  1.1  riastrad 	int ret;
    624  1.1  riastrad 
    625  1.1  riastrad 	if (pi->caps_sq_ramping || pi->caps_db_ramping ||
    626  1.1  riastrad 	    pi->caps_td_ramping || pi->caps_tcp_ramping) {
    627  1.1  riastrad 		cik_enter_rlc_safe_mode(rdev);
    628  1.1  riastrad 
    629  1.1  riastrad 		if (enable) {
    630  1.1  riastrad 			ret = ci_program_pt_config_registers(rdev, didt_config_ci);
    631  1.1  riastrad 			if (ret) {
    632  1.1  riastrad 				cik_exit_rlc_safe_mode(rdev);
    633  1.1  riastrad 				return ret;
    634  1.1  riastrad 			}
    635  1.1  riastrad 		}
    636  1.1  riastrad 
    637  1.1  riastrad 		ci_do_enable_didt(rdev, enable);
    638  1.1  riastrad 
    639  1.1  riastrad 		cik_exit_rlc_safe_mode(rdev);
    640  1.1  riastrad 	}
    641  1.1  riastrad 
    642  1.1  riastrad 	return 0;
    643  1.1  riastrad }
    644  1.1  riastrad 
    645  1.1  riastrad static int ci_enable_power_containment(struct radeon_device *rdev, bool enable)
    646  1.1  riastrad {
    647  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    648  1.1  riastrad 	PPSMC_Result smc_result;
    649  1.1  riastrad 	int ret = 0;
    650  1.1  riastrad 
    651  1.1  riastrad 	if (enable) {
    652  1.1  riastrad 		pi->power_containment_features = 0;
    653  1.1  riastrad 		if (pi->caps_power_containment) {
    654  1.1  riastrad 			if (pi->enable_bapm_feature) {
    655  1.1  riastrad 				smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableDTE);
    656  1.1  riastrad 				if (smc_result != PPSMC_Result_OK)
    657  1.1  riastrad 					ret = -EINVAL;
    658  1.1  riastrad 				else
    659  1.1  riastrad 					pi->power_containment_features |= POWERCONTAINMENT_FEATURE_BAPM;
    660  1.1  riastrad 			}
    661  1.1  riastrad 
    662  1.1  riastrad 			if (pi->enable_tdc_limit_feature) {
    663  1.1  riastrad 				smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitEnable);
    664  1.1  riastrad 				if (smc_result != PPSMC_Result_OK)
    665  1.1  riastrad 					ret = -EINVAL;
    666  1.1  riastrad 				else
    667  1.1  riastrad 					pi->power_containment_features |= POWERCONTAINMENT_FEATURE_TDCLimit;
    668  1.1  riastrad 			}
    669  1.1  riastrad 
    670  1.1  riastrad 			if (pi->enable_pkg_pwr_tracking_feature) {
    671  1.1  riastrad 				smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitEnable);
    672  1.1  riastrad 				if (smc_result != PPSMC_Result_OK) {
    673  1.1  riastrad 					ret = -EINVAL;
    674  1.1  riastrad 				} else {
    675  1.1  riastrad 					struct radeon_cac_tdp_table *cac_tdp_table =
    676  1.1  riastrad 						rdev->pm.dpm.dyn_state.cac_tdp_table;
    677  1.1  riastrad 					u32 default_pwr_limit =
    678  1.1  riastrad 						(u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
    679  1.1  riastrad 
    680  1.1  riastrad 					pi->power_containment_features |= POWERCONTAINMENT_FEATURE_PkgPwrLimit;
    681  1.1  riastrad 
    682  1.1  riastrad 					ci_set_power_limit(rdev, default_pwr_limit);
    683  1.1  riastrad 				}
    684  1.1  riastrad 			}
    685  1.1  riastrad 		}
    686  1.1  riastrad 	} else {
    687  1.1  riastrad 		if (pi->caps_power_containment && pi->power_containment_features) {
    688  1.1  riastrad 			if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_TDCLimit)
    689  1.1  riastrad 				ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitDisable);
    690  1.1  riastrad 
    691  1.1  riastrad 			if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_BAPM)
    692  1.1  riastrad 				ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableDTE);
    693  1.1  riastrad 
    694  1.1  riastrad 			if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit)
    695  1.1  riastrad 				ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitDisable);
    696  1.1  riastrad 			pi->power_containment_features = 0;
    697  1.1  riastrad 		}
    698  1.1  riastrad 	}
    699  1.1  riastrad 
    700  1.1  riastrad 	return ret;
    701  1.1  riastrad }
    702  1.1  riastrad 
    703  1.1  riastrad static int ci_enable_smc_cac(struct radeon_device *rdev, bool enable)
    704  1.1  riastrad {
    705  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    706  1.1  riastrad 	PPSMC_Result smc_result;
    707  1.1  riastrad 	int ret = 0;
    708  1.1  riastrad 
    709  1.1  riastrad 	if (pi->caps_cac) {
    710  1.1  riastrad 		if (enable) {
    711  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableCac);
    712  1.1  riastrad 			if (smc_result != PPSMC_Result_OK) {
    713  1.1  riastrad 				ret = -EINVAL;
    714  1.1  riastrad 				pi->cac_enabled = false;
    715  1.1  riastrad 			} else {
    716  1.1  riastrad 				pi->cac_enabled = true;
    717  1.1  riastrad 			}
    718  1.1  riastrad 		} else if (pi->cac_enabled) {
    719  1.1  riastrad 			ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableCac);
    720  1.1  riastrad 			pi->cac_enabled = false;
    721  1.1  riastrad 		}
    722  1.1  riastrad 	}
    723  1.1  riastrad 
    724  1.1  riastrad 	return ret;
    725  1.1  riastrad }
    726  1.1  riastrad 
    727  1.1  riastrad static int ci_enable_thermal_based_sclk_dpm(struct radeon_device *rdev,
    728  1.1  riastrad 					    bool enable)
    729  1.1  riastrad {
    730  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    731  1.1  riastrad 	PPSMC_Result smc_result = PPSMC_Result_OK;
    732  1.1  riastrad 
    733  1.1  riastrad 	if (pi->thermal_sclk_dpm_enabled) {
    734  1.1  riastrad 		if (enable)
    735  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_ENABLE_THERMAL_DPM);
    736  1.1  riastrad 		else
    737  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DISABLE_THERMAL_DPM);
    738  1.1  riastrad 	}
    739  1.1  riastrad 
    740  1.1  riastrad 	if (smc_result == PPSMC_Result_OK)
    741  1.1  riastrad 		return 0;
    742  1.1  riastrad 	else
    743  1.1  riastrad 		return -EINVAL;
    744  1.1  riastrad }
    745  1.1  riastrad 
    746  1.1  riastrad static int ci_power_control_set_level(struct radeon_device *rdev)
    747  1.1  riastrad {
    748  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    749  1.1  riastrad 	struct radeon_cac_tdp_table *cac_tdp_table =
    750  1.1  riastrad 		rdev->pm.dpm.dyn_state.cac_tdp_table;
    751  1.1  riastrad 	s32 adjust_percent;
    752  1.1  riastrad 	s32 target_tdp;
    753  1.1  riastrad 	int ret = 0;
    754  1.1  riastrad 	bool adjust_polarity = false; /* ??? */
    755  1.1  riastrad 
    756  1.1  riastrad 	if (pi->caps_power_containment) {
    757  1.1  riastrad 		adjust_percent = adjust_polarity ?
    758  1.1  riastrad 			rdev->pm.dpm.tdp_adjustment : (-1 * rdev->pm.dpm.tdp_adjustment);
    759  1.1  riastrad 		target_tdp = ((100 + adjust_percent) *
    760  1.1  riastrad 			      (s32)cac_tdp_table->configurable_tdp) / 100;
    761  1.1  riastrad 
    762  1.1  riastrad 		ret = ci_set_overdrive_target_tdp(rdev, (u32)target_tdp);
    763  1.1  riastrad 	}
    764  1.1  riastrad 
    765  1.1  riastrad 	return ret;
    766  1.1  riastrad }
    767  1.1  riastrad 
    768  1.1  riastrad void ci_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
    769  1.1  riastrad {
    770  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    771  1.1  riastrad 
    772  1.1  riastrad 	if (pi->uvd_power_gated == gate)
    773  1.1  riastrad 		return;
    774  1.1  riastrad 
    775  1.1  riastrad 	pi->uvd_power_gated = gate;
    776  1.1  riastrad 
    777  1.1  riastrad 	ci_update_uvd_dpm(rdev, gate);
    778  1.1  riastrad }
    779  1.1  riastrad 
    780  1.1  riastrad bool ci_dpm_vblank_too_short(struct radeon_device *rdev)
    781  1.1  riastrad {
    782  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    783  1.1  riastrad 	u32 vblank_time = r600_dpm_get_vblank_time(rdev);
    784  1.1  riastrad 	u32 switch_limit = pi->mem_gddr5 ? 450 : 300;
    785  1.1  riastrad 
    786  1.1  riastrad 	/* disable mclk switching if the refresh is >120Hz, even if the
    787  1.1  riastrad         * blanking period would allow it
    788  1.1  riastrad         */
    789  1.1  riastrad 	if (r600_dpm_get_vrefresh(rdev) > 120)
    790  1.1  riastrad 		return true;
    791  1.1  riastrad 
    792  1.1  riastrad 	if (vblank_time < switch_limit)
    793  1.1  riastrad 		return true;
    794  1.1  riastrad 	else
    795  1.1  riastrad 		return false;
    796  1.1  riastrad 
    797  1.1  riastrad }
    798  1.1  riastrad 
    799  1.1  riastrad static void ci_apply_state_adjust_rules(struct radeon_device *rdev,
    800  1.1  riastrad 					struct radeon_ps *rps)
    801  1.1  riastrad {
    802  1.1  riastrad 	struct ci_ps *ps = ci_get_ps(rps);
    803  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    804  1.1  riastrad 	struct radeon_clock_and_voltage_limits *max_limits;
    805  1.1  riastrad 	bool disable_mclk_switching;
    806  1.1  riastrad 	u32 sclk, mclk;
    807  1.1  riastrad 	int i;
    808  1.1  riastrad 
    809  1.1  riastrad 	if (rps->vce_active) {
    810  1.1  riastrad 		rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
    811  1.1  riastrad 		rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
    812  1.1  riastrad 	} else {
    813  1.1  riastrad 		rps->evclk = 0;
    814  1.1  riastrad 		rps->ecclk = 0;
    815  1.1  riastrad 	}
    816  1.1  riastrad 
    817  1.1  riastrad 	if ((rdev->pm.dpm.new_active_crtc_count > 1) ||
    818  1.1  riastrad 	    ci_dpm_vblank_too_short(rdev))
    819  1.1  riastrad 		disable_mclk_switching = true;
    820  1.1  riastrad 	else
    821  1.1  riastrad 		disable_mclk_switching = false;
    822  1.1  riastrad 
    823  1.1  riastrad 	if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
    824  1.1  riastrad 		pi->battery_state = true;
    825  1.1  riastrad 	else
    826  1.1  riastrad 		pi->battery_state = false;
    827  1.1  riastrad 
    828  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
    829  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
    830  1.1  riastrad 	else
    831  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
    832  1.1  riastrad 
    833  1.1  riastrad 	if (rdev->pm.dpm.ac_power == false) {
    834  1.1  riastrad 		for (i = 0; i < ps->performance_level_count; i++) {
    835  1.1  riastrad 			if (ps->performance_levels[i].mclk > max_limits->mclk)
    836  1.1  riastrad 				ps->performance_levels[i].mclk = max_limits->mclk;
    837  1.1  riastrad 			if (ps->performance_levels[i].sclk > max_limits->sclk)
    838  1.1  riastrad 				ps->performance_levels[i].sclk = max_limits->sclk;
    839  1.1  riastrad 		}
    840  1.1  riastrad 	}
    841  1.1  riastrad 
    842  1.1  riastrad 	/* XXX validate the min clocks required for display */
    843  1.1  riastrad 
    844  1.1  riastrad 	if (disable_mclk_switching) {
    845  1.1  riastrad 		mclk  = ps->performance_levels[ps->performance_level_count - 1].mclk;
    846  1.1  riastrad 		sclk = ps->performance_levels[0].sclk;
    847  1.1  riastrad 	} else {
    848  1.1  riastrad 		mclk = ps->performance_levels[0].mclk;
    849  1.1  riastrad 		sclk = ps->performance_levels[0].sclk;
    850  1.1  riastrad 	}
    851  1.1  riastrad 
    852  1.1  riastrad 	if (rps->vce_active) {
    853  1.1  riastrad 		if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
    854  1.1  riastrad 			sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
    855  1.1  riastrad 		if (mclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk)
    856  1.1  riastrad 			mclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk;
    857  1.1  riastrad 	}
    858  1.1  riastrad 
    859  1.1  riastrad 	ps->performance_levels[0].sclk = sclk;
    860  1.1  riastrad 	ps->performance_levels[0].mclk = mclk;
    861  1.1  riastrad 
    862  1.1  riastrad 	if (ps->performance_levels[1].sclk < ps->performance_levels[0].sclk)
    863  1.1  riastrad 		ps->performance_levels[1].sclk = ps->performance_levels[0].sclk;
    864  1.1  riastrad 
    865  1.1  riastrad 	if (disable_mclk_switching) {
    866  1.1  riastrad 		if (ps->performance_levels[0].mclk < ps->performance_levels[1].mclk)
    867  1.1  riastrad 			ps->performance_levels[0].mclk = ps->performance_levels[1].mclk;
    868  1.1  riastrad 	} else {
    869  1.1  riastrad 		if (ps->performance_levels[1].mclk < ps->performance_levels[0].mclk)
    870  1.1  riastrad 			ps->performance_levels[1].mclk = ps->performance_levels[0].mclk;
    871  1.1  riastrad 	}
    872  1.1  riastrad }
    873  1.1  riastrad 
    874  1.1  riastrad static int ci_thermal_set_temperature_range(struct radeon_device *rdev,
    875  1.1  riastrad 					    int min_temp, int max_temp)
    876  1.1  riastrad {
    877  1.1  riastrad 	int low_temp = 0 * 1000;
    878  1.1  riastrad 	int high_temp = 255 * 1000;
    879  1.1  riastrad 	u32 tmp;
    880  1.1  riastrad 
    881  1.1  riastrad 	if (low_temp < min_temp)
    882  1.1  riastrad 		low_temp = min_temp;
    883  1.1  riastrad 	if (high_temp > max_temp)
    884  1.1  riastrad 		high_temp = max_temp;
    885  1.1  riastrad 	if (high_temp < low_temp) {
    886  1.1  riastrad 		DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
    887  1.1  riastrad 		return -EINVAL;
    888  1.1  riastrad 	}
    889  1.1  riastrad 
    890  1.1  riastrad 	tmp = RREG32_SMC(CG_THERMAL_INT);
    891  1.1  riastrad 	tmp &= ~(CI_DIG_THERM_INTH_MASK | CI_DIG_THERM_INTL_MASK);
    892  1.1  riastrad 	tmp |= CI_DIG_THERM_INTH(high_temp / 1000) |
    893  1.1  riastrad 		CI_DIG_THERM_INTL(low_temp / 1000);
    894  1.1  riastrad 	WREG32_SMC(CG_THERMAL_INT, tmp);
    895  1.1  riastrad 
    896  1.1  riastrad #if 0
    897  1.1  riastrad 	/* XXX: need to figure out how to handle this properly */
    898  1.1  riastrad 	tmp = RREG32_SMC(CG_THERMAL_CTRL);
    899  1.1  riastrad 	tmp &= DIG_THERM_DPM_MASK;
    900  1.1  riastrad 	tmp |= DIG_THERM_DPM(high_temp / 1000);
    901  1.1  riastrad 	WREG32_SMC(CG_THERMAL_CTRL, tmp);
    902  1.1  riastrad #endif
    903  1.1  riastrad 
    904  1.1  riastrad 	rdev->pm.dpm.thermal.min_temp = low_temp;
    905  1.1  riastrad 	rdev->pm.dpm.thermal.max_temp = high_temp;
    906  1.1  riastrad 
    907  1.1  riastrad 	return 0;
    908  1.1  riastrad }
    909  1.1  riastrad 
    910  1.1  riastrad static int ci_thermal_enable_alert(struct radeon_device *rdev,
    911  1.1  riastrad 				   bool enable)
    912  1.1  riastrad {
    913  1.1  riastrad 	u32 thermal_int = RREG32_SMC(CG_THERMAL_INT);
    914  1.1  riastrad 	PPSMC_Result result;
    915  1.1  riastrad 
    916  1.1  riastrad 	if (enable) {
    917  1.1  riastrad 		thermal_int &= ~(THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW);
    918  1.1  riastrad 		WREG32_SMC(CG_THERMAL_INT, thermal_int);
    919  1.1  riastrad 		rdev->irq.dpm_thermal = false;
    920  1.1  riastrad 		result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Enable);
    921  1.1  riastrad 		if (result != PPSMC_Result_OK) {
    922  1.1  riastrad 			DRM_DEBUG_KMS("Could not enable thermal interrupts.\n");
    923  1.1  riastrad 			return -EINVAL;
    924  1.1  riastrad 		}
    925  1.1  riastrad 	} else {
    926  1.1  riastrad 		thermal_int |= THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW;
    927  1.1  riastrad 		WREG32_SMC(CG_THERMAL_INT, thermal_int);
    928  1.1  riastrad 		rdev->irq.dpm_thermal = true;
    929  1.1  riastrad 		result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Disable);
    930  1.1  riastrad 		if (result != PPSMC_Result_OK) {
    931  1.1  riastrad 			DRM_DEBUG_KMS("Could not disable thermal interrupts.\n");
    932  1.1  riastrad 			return -EINVAL;
    933  1.1  riastrad 		}
    934  1.1  riastrad 	}
    935  1.1  riastrad 
    936  1.1  riastrad 	return 0;
    937  1.1  riastrad }
    938  1.1  riastrad 
    939  1.1  riastrad static void ci_fan_ctrl_set_static_mode(struct radeon_device *rdev, u32 mode)
    940  1.1  riastrad {
    941  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    942  1.1  riastrad 	u32 tmp;
    943  1.1  riastrad 
    944  1.1  riastrad 	if (pi->fan_ctrl_is_in_default_mode) {
    945  1.1  riastrad 		tmp = (RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK) >> FDO_PWM_MODE_SHIFT;
    946  1.1  riastrad 		pi->fan_ctrl_default_mode = tmp;
    947  1.1  riastrad 		tmp = (RREG32_SMC(CG_FDO_CTRL2) & TMIN_MASK) >> TMIN_SHIFT;
    948  1.1  riastrad 		pi->t_min = tmp;
    949  1.1  riastrad 		pi->fan_ctrl_is_in_default_mode = false;
    950  1.1  riastrad 	}
    951  1.1  riastrad 
    952  1.1  riastrad 	tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
    953  1.1  riastrad 	tmp |= TMIN(0);
    954  1.1  riastrad 	WREG32_SMC(CG_FDO_CTRL2, tmp);
    955  1.1  riastrad 
    956  1.1  riastrad 	tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
    957  1.1  riastrad 	tmp |= FDO_PWM_MODE(mode);
    958  1.1  riastrad 	WREG32_SMC(CG_FDO_CTRL2, tmp);
    959  1.1  riastrad }
    960  1.1  riastrad 
    961  1.1  riastrad static int ci_thermal_setup_fan_table(struct radeon_device *rdev)
    962  1.1  riastrad {
    963  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
    964  1.1  riastrad 	SMU7_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
    965  1.1  riastrad 	u32 duty100;
    966  1.1  riastrad 	u32 t_diff1, t_diff2, pwm_diff1, pwm_diff2;
    967  1.1  riastrad 	u16 fdo_min, slope1, slope2;
    968  1.1  riastrad 	u32 reference_clock, tmp;
    969  1.1  riastrad 	int ret;
    970  1.1  riastrad 	u64 tmp64;
    971  1.1  riastrad 
    972  1.1  riastrad 	if (!pi->fan_table_start) {
    973  1.1  riastrad 		rdev->pm.dpm.fan.ucode_fan_control = false;
    974  1.1  riastrad 		return 0;
    975  1.1  riastrad 	}
    976  1.1  riastrad 
    977  1.1  riastrad 	duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
    978  1.1  riastrad 
    979  1.1  riastrad 	if (duty100 == 0) {
    980  1.1  riastrad 		rdev->pm.dpm.fan.ucode_fan_control = false;
    981  1.1  riastrad 		return 0;
    982  1.1  riastrad 	}
    983  1.1  riastrad 
    984  1.1  riastrad 	tmp64 = (u64)rdev->pm.dpm.fan.pwm_min * duty100;
    985  1.1  riastrad 	do_div(tmp64, 10000);
    986  1.1  riastrad 	fdo_min = (u16)tmp64;
    987  1.1  riastrad 
    988  1.1  riastrad 	t_diff1 = rdev->pm.dpm.fan.t_med - rdev->pm.dpm.fan.t_min;
    989  1.1  riastrad 	t_diff2 = rdev->pm.dpm.fan.t_high - rdev->pm.dpm.fan.t_med;
    990  1.1  riastrad 
    991  1.1  riastrad 	pwm_diff1 = rdev->pm.dpm.fan.pwm_med - rdev->pm.dpm.fan.pwm_min;
    992  1.1  riastrad 	pwm_diff2 = rdev->pm.dpm.fan.pwm_high - rdev->pm.dpm.fan.pwm_med;
    993  1.1  riastrad 
    994  1.1  riastrad 	slope1 = (u16)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
    995  1.1  riastrad 	slope2 = (u16)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
    996  1.1  riastrad 
    997  1.1  riastrad 	fan_table.TempMin = cpu_to_be16((50 + rdev->pm.dpm.fan.t_min) / 100);
    998  1.1  riastrad 	fan_table.TempMed = cpu_to_be16((50 + rdev->pm.dpm.fan.t_med) / 100);
    999  1.1  riastrad 	fan_table.TempMax = cpu_to_be16((50 + rdev->pm.dpm.fan.t_max) / 100);
   1000  1.1  riastrad 
   1001  1.1  riastrad 	fan_table.Slope1 = cpu_to_be16(slope1);
   1002  1.1  riastrad 	fan_table.Slope2 = cpu_to_be16(slope2);
   1003  1.1  riastrad 
   1004  1.1  riastrad 	fan_table.FdoMin = cpu_to_be16(fdo_min);
   1005  1.1  riastrad 
   1006  1.1  riastrad 	fan_table.HystDown = cpu_to_be16(rdev->pm.dpm.fan.t_hyst);
   1007  1.1  riastrad 
   1008  1.1  riastrad 	fan_table.HystUp = cpu_to_be16(1);
   1009  1.1  riastrad 
   1010  1.1  riastrad 	fan_table.HystSlope = cpu_to_be16(1);
   1011  1.1  riastrad 
   1012  1.1  riastrad 	fan_table.TempRespLim = cpu_to_be16(5);
   1013  1.1  riastrad 
   1014  1.1  riastrad 	reference_clock = radeon_get_xclk(rdev);
   1015  1.1  riastrad 
   1016  1.1  riastrad 	fan_table.RefreshPeriod = cpu_to_be32((rdev->pm.dpm.fan.cycle_delay *
   1017  1.1  riastrad 					       reference_clock) / 1600);
   1018  1.1  riastrad 
   1019  1.1  riastrad 	fan_table.FdoMax = cpu_to_be16((u16)duty100);
   1020  1.1  riastrad 
   1021  1.1  riastrad 	tmp = (RREG32_SMC(CG_MULT_THERMAL_CTRL) & TEMP_SEL_MASK) >> TEMP_SEL_SHIFT;
   1022  1.1  riastrad 	fan_table.TempSrc = (uint8_t)tmp;
   1023  1.1  riastrad 
   1024  1.1  riastrad 	ret = ci_copy_bytes_to_smc(rdev,
   1025  1.1  riastrad 				   pi->fan_table_start,
   1026  1.1  riastrad 				   (u8 *)(&fan_table),
   1027  1.1  riastrad 				   sizeof(fan_table),
   1028  1.1  riastrad 				   pi->sram_end);
   1029  1.1  riastrad 
   1030  1.1  riastrad 	if (ret) {
   1031  1.1  riastrad 		DRM_ERROR("Failed to load fan table to the SMC.");
   1032  1.1  riastrad 		rdev->pm.dpm.fan.ucode_fan_control = false;
   1033  1.1  riastrad 	}
   1034  1.1  riastrad 
   1035  1.1  riastrad 	return 0;
   1036  1.1  riastrad }
   1037  1.1  riastrad 
   1038  1.1  riastrad static int ci_fan_ctrl_start_smc_fan_control(struct radeon_device *rdev)
   1039  1.1  riastrad {
   1040  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1041  1.1  riastrad 	PPSMC_Result ret;
   1042  1.1  riastrad 
   1043  1.1  riastrad 	if (pi->caps_od_fuzzy_fan_control_support) {
   1044  1.1  riastrad 		ret = ci_send_msg_to_smc_with_parameter(rdev,
   1045  1.1  riastrad 							PPSMC_StartFanControl,
   1046  1.1  riastrad 							FAN_CONTROL_FUZZY);
   1047  1.1  riastrad 		if (ret != PPSMC_Result_OK)
   1048  1.1  riastrad 			return -EINVAL;
   1049  1.1  riastrad 		ret = ci_send_msg_to_smc_with_parameter(rdev,
   1050  1.1  riastrad 							PPSMC_MSG_SetFanPwmMax,
   1051  1.1  riastrad 							rdev->pm.dpm.fan.default_max_fan_pwm);
   1052  1.1  riastrad 		if (ret != PPSMC_Result_OK)
   1053  1.1  riastrad 			return -EINVAL;
   1054  1.1  riastrad 	} else {
   1055  1.1  riastrad 		ret = ci_send_msg_to_smc_with_parameter(rdev,
   1056  1.1  riastrad 							PPSMC_StartFanControl,
   1057  1.1  riastrad 							FAN_CONTROL_TABLE);
   1058  1.1  riastrad 		if (ret != PPSMC_Result_OK)
   1059  1.1  riastrad 			return -EINVAL;
   1060  1.1  riastrad 	}
   1061  1.1  riastrad 
   1062  1.1  riastrad 	pi->fan_is_controlled_by_smc = true;
   1063  1.1  riastrad 	return 0;
   1064  1.1  riastrad }
   1065  1.1  riastrad 
   1066  1.1  riastrad static int ci_fan_ctrl_stop_smc_fan_control(struct radeon_device *rdev)
   1067  1.1  riastrad {
   1068  1.1  riastrad 	PPSMC_Result ret;
   1069  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1070  1.1  riastrad 
   1071  1.1  riastrad 	ret = ci_send_msg_to_smc(rdev, PPSMC_StopFanControl);
   1072  1.1  riastrad 	if (ret == PPSMC_Result_OK) {
   1073  1.1  riastrad 		pi->fan_is_controlled_by_smc = false;
   1074  1.1  riastrad 		return 0;
   1075  1.1  riastrad 	} else
   1076  1.1  riastrad 		return -EINVAL;
   1077  1.1  riastrad }
   1078  1.1  riastrad 
   1079  1.1  riastrad int ci_fan_ctrl_get_fan_speed_percent(struct radeon_device *rdev,
   1080  1.1  riastrad 					     u32 *speed)
   1081  1.1  riastrad {
   1082  1.1  riastrad 	u32 duty, duty100;
   1083  1.1  riastrad 	u64 tmp64;
   1084  1.1  riastrad 
   1085  1.1  riastrad 	if (rdev->pm.no_fan)
   1086  1.1  riastrad 		return -ENOENT;
   1087  1.1  riastrad 
   1088  1.1  riastrad 	duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
   1089  1.1  riastrad 	duty = (RREG32_SMC(CG_THERMAL_STATUS) & FDO_PWM_DUTY_MASK) >> FDO_PWM_DUTY_SHIFT;
   1090  1.1  riastrad 
   1091  1.1  riastrad 	if (duty100 == 0)
   1092  1.1  riastrad 		return -EINVAL;
   1093  1.1  riastrad 
   1094  1.1  riastrad 	tmp64 = (u64)duty * 100;
   1095  1.1  riastrad 	do_div(tmp64, duty100);
   1096  1.1  riastrad 	*speed = (u32)tmp64;
   1097  1.1  riastrad 
   1098  1.1  riastrad 	if (*speed > 100)
   1099  1.1  riastrad 		*speed = 100;
   1100  1.1  riastrad 
   1101  1.1  riastrad 	return 0;
   1102  1.1  riastrad }
   1103  1.1  riastrad 
   1104  1.1  riastrad int ci_fan_ctrl_set_fan_speed_percent(struct radeon_device *rdev,
   1105  1.1  riastrad 					     u32 speed)
   1106  1.1  riastrad {
   1107  1.1  riastrad 	u32 tmp;
   1108  1.1  riastrad 	u32 duty, duty100;
   1109  1.1  riastrad 	u64 tmp64;
   1110  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1111  1.1  riastrad 
   1112  1.1  riastrad 	if (rdev->pm.no_fan)
   1113  1.1  riastrad 		return -ENOENT;
   1114  1.1  riastrad 
   1115  1.1  riastrad 	if (pi->fan_is_controlled_by_smc)
   1116  1.1  riastrad 		return -EINVAL;
   1117  1.1  riastrad 
   1118  1.1  riastrad 	if (speed > 100)
   1119  1.1  riastrad 		return -EINVAL;
   1120  1.1  riastrad 
   1121  1.1  riastrad 	duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
   1122  1.1  riastrad 
   1123  1.1  riastrad 	if (duty100 == 0)
   1124  1.1  riastrad 		return -EINVAL;
   1125  1.1  riastrad 
   1126  1.1  riastrad 	tmp64 = (u64)speed * duty100;
   1127  1.1  riastrad 	do_div(tmp64, 100);
   1128  1.1  riastrad 	duty = (u32)tmp64;
   1129  1.1  riastrad 
   1130  1.1  riastrad 	tmp = RREG32_SMC(CG_FDO_CTRL0) & ~FDO_STATIC_DUTY_MASK;
   1131  1.1  riastrad 	tmp |= FDO_STATIC_DUTY(duty);
   1132  1.1  riastrad 	WREG32_SMC(CG_FDO_CTRL0, tmp);
   1133  1.1  riastrad 
   1134  1.1  riastrad 	return 0;
   1135  1.1  riastrad }
   1136  1.1  riastrad 
   1137  1.1  riastrad void ci_fan_ctrl_set_mode(struct radeon_device *rdev, u32 mode)
   1138  1.1  riastrad {
   1139  1.1  riastrad 	if (mode) {
   1140  1.1  riastrad 		/* stop auto-manage */
   1141  1.1  riastrad 		if (rdev->pm.dpm.fan.ucode_fan_control)
   1142  1.1  riastrad 			ci_fan_ctrl_stop_smc_fan_control(rdev);
   1143  1.1  riastrad 		ci_fan_ctrl_set_static_mode(rdev, mode);
   1144  1.1  riastrad 	} else {
   1145  1.1  riastrad 		/* restart auto-manage */
   1146  1.1  riastrad 		if (rdev->pm.dpm.fan.ucode_fan_control)
   1147  1.1  riastrad 			ci_thermal_start_smc_fan_control(rdev);
   1148  1.1  riastrad 		else
   1149  1.1  riastrad 			ci_fan_ctrl_set_default_mode(rdev);
   1150  1.1  riastrad 	}
   1151  1.1  riastrad }
   1152  1.1  riastrad 
   1153  1.1  riastrad u32 ci_fan_ctrl_get_mode(struct radeon_device *rdev)
   1154  1.1  riastrad {
   1155  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1156  1.1  riastrad 	u32 tmp;
   1157  1.1  riastrad 
   1158  1.1  riastrad 	if (pi->fan_is_controlled_by_smc)
   1159  1.1  riastrad 		return 0;
   1160  1.1  riastrad 
   1161  1.1  riastrad 	tmp = RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK;
   1162  1.1  riastrad 	return (tmp >> FDO_PWM_MODE_SHIFT);
   1163  1.1  riastrad }
   1164  1.1  riastrad 
   1165  1.1  riastrad #if 0
   1166  1.1  riastrad static int ci_fan_ctrl_get_fan_speed_rpm(struct radeon_device *rdev,
   1167  1.1  riastrad 					 u32 *speed)
   1168  1.1  riastrad {
   1169  1.1  riastrad 	u32 tach_period;
   1170  1.1  riastrad 	u32 xclk = radeon_get_xclk(rdev);
   1171  1.1  riastrad 
   1172  1.1  riastrad 	if (rdev->pm.no_fan)
   1173  1.1  riastrad 		return -ENOENT;
   1174  1.1  riastrad 
   1175  1.1  riastrad 	if (rdev->pm.fan_pulses_per_revolution == 0)
   1176  1.1  riastrad 		return -ENOENT;
   1177  1.1  riastrad 
   1178  1.1  riastrad 	tach_period = (RREG32_SMC(CG_TACH_STATUS) & TACH_PERIOD_MASK) >> TACH_PERIOD_SHIFT;
   1179  1.1  riastrad 	if (tach_period == 0)
   1180  1.1  riastrad 		return -ENOENT;
   1181  1.1  riastrad 
   1182  1.1  riastrad 	*speed = 60 * xclk * 10000 / tach_period;
   1183  1.1  riastrad 
   1184  1.1  riastrad 	return 0;
   1185  1.1  riastrad }
   1186  1.1  riastrad 
   1187  1.1  riastrad static int ci_fan_ctrl_set_fan_speed_rpm(struct radeon_device *rdev,
   1188  1.1  riastrad 					 u32 speed)
   1189  1.1  riastrad {
   1190  1.1  riastrad 	u32 tach_period, tmp;
   1191  1.1  riastrad 	u32 xclk = radeon_get_xclk(rdev);
   1192  1.1  riastrad 
   1193  1.1  riastrad 	if (rdev->pm.no_fan)
   1194  1.1  riastrad 		return -ENOENT;
   1195  1.1  riastrad 
   1196  1.1  riastrad 	if (rdev->pm.fan_pulses_per_revolution == 0)
   1197  1.1  riastrad 		return -ENOENT;
   1198  1.1  riastrad 
   1199  1.1  riastrad 	if ((speed < rdev->pm.fan_min_rpm) ||
   1200  1.1  riastrad 	    (speed > rdev->pm.fan_max_rpm))
   1201  1.1  riastrad 		return -EINVAL;
   1202  1.1  riastrad 
   1203  1.1  riastrad 	if (rdev->pm.dpm.fan.ucode_fan_control)
   1204  1.1  riastrad 		ci_fan_ctrl_stop_smc_fan_control(rdev);
   1205  1.1  riastrad 
   1206  1.1  riastrad 	tach_period = 60 * xclk * 10000 / (8 * speed);
   1207  1.1  riastrad 	tmp = RREG32_SMC(CG_TACH_CTRL) & ~TARGET_PERIOD_MASK;
   1208  1.1  riastrad 	tmp |= TARGET_PERIOD(tach_period);
   1209  1.1  riastrad 	WREG32_SMC(CG_TACH_CTRL, tmp);
   1210  1.1  riastrad 
   1211  1.1  riastrad 	ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC_RPM);
   1212  1.1  riastrad 
   1213  1.1  riastrad 	return 0;
   1214  1.1  riastrad }
   1215  1.1  riastrad #endif
   1216  1.1  riastrad 
   1217  1.1  riastrad static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev)
   1218  1.1  riastrad {
   1219  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1220  1.1  riastrad 	u32 tmp;
   1221  1.1  riastrad 
   1222  1.1  riastrad 	if (!pi->fan_ctrl_is_in_default_mode) {
   1223  1.1  riastrad 		tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
   1224  1.1  riastrad 		tmp |= FDO_PWM_MODE(pi->fan_ctrl_default_mode);
   1225  1.1  riastrad 		WREG32_SMC(CG_FDO_CTRL2, tmp);
   1226  1.1  riastrad 
   1227  1.1  riastrad 		tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
   1228  1.1  riastrad 		tmp |= TMIN(pi->t_min);
   1229  1.1  riastrad 		WREG32_SMC(CG_FDO_CTRL2, tmp);
   1230  1.1  riastrad 		pi->fan_ctrl_is_in_default_mode = true;
   1231  1.1  riastrad 	}
   1232  1.1  riastrad }
   1233  1.1  riastrad 
   1234  1.1  riastrad static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev)
   1235  1.1  riastrad {
   1236  1.1  riastrad 	if (rdev->pm.dpm.fan.ucode_fan_control) {
   1237  1.1  riastrad 		ci_fan_ctrl_start_smc_fan_control(rdev);
   1238  1.1  riastrad 		ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC);
   1239  1.1  riastrad 	}
   1240  1.1  riastrad }
   1241  1.1  riastrad 
   1242  1.1  riastrad static void ci_thermal_initialize(struct radeon_device *rdev)
   1243  1.1  riastrad {
   1244  1.1  riastrad 	u32 tmp;
   1245  1.1  riastrad 
   1246  1.1  riastrad 	if (rdev->pm.fan_pulses_per_revolution) {
   1247  1.1  riastrad 		tmp = RREG32_SMC(CG_TACH_CTRL) & ~EDGE_PER_REV_MASK;
   1248  1.1  riastrad 		tmp |= EDGE_PER_REV(rdev->pm.fan_pulses_per_revolution -1);
   1249  1.1  riastrad 		WREG32_SMC(CG_TACH_CTRL, tmp);
   1250  1.1  riastrad 	}
   1251  1.1  riastrad 
   1252  1.1  riastrad 	tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TACH_PWM_RESP_RATE_MASK;
   1253  1.1  riastrad 	tmp |= TACH_PWM_RESP_RATE(0x28);
   1254  1.1  riastrad 	WREG32_SMC(CG_FDO_CTRL2, tmp);
   1255  1.1  riastrad }
   1256  1.1  riastrad 
   1257  1.1  riastrad static int ci_thermal_start_thermal_controller(struct radeon_device *rdev)
   1258  1.1  riastrad {
   1259  1.1  riastrad 	int ret;
   1260  1.1  riastrad 
   1261  1.1  riastrad 	ci_thermal_initialize(rdev);
   1262  1.1  riastrad 	ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
   1263  1.1  riastrad 	if (ret)
   1264  1.1  riastrad 		return ret;
   1265  1.1  riastrad 	ret = ci_thermal_enable_alert(rdev, true);
   1266  1.1  riastrad 	if (ret)
   1267  1.1  riastrad 		return ret;
   1268  1.1  riastrad 	if (rdev->pm.dpm.fan.ucode_fan_control) {
   1269  1.1  riastrad 		ret = ci_thermal_setup_fan_table(rdev);
   1270  1.1  riastrad 		if (ret)
   1271  1.1  riastrad 			return ret;
   1272  1.1  riastrad 		ci_thermal_start_smc_fan_control(rdev);
   1273  1.1  riastrad 	}
   1274  1.1  riastrad 
   1275  1.1  riastrad 	return 0;
   1276  1.1  riastrad }
   1277  1.1  riastrad 
   1278  1.1  riastrad static void ci_thermal_stop_thermal_controller(struct radeon_device *rdev)
   1279  1.1  riastrad {
   1280  1.1  riastrad 	if (!rdev->pm.no_fan)
   1281  1.1  riastrad 		ci_fan_ctrl_set_default_mode(rdev);
   1282  1.1  riastrad }
   1283  1.1  riastrad 
   1284  1.1  riastrad #if 0
   1285  1.1  riastrad static int ci_read_smc_soft_register(struct radeon_device *rdev,
   1286  1.1  riastrad 				     u16 reg_offset, u32 *value)
   1287  1.1  riastrad {
   1288  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1289  1.1  riastrad 
   1290  1.1  riastrad 	return ci_read_smc_sram_dword(rdev,
   1291  1.1  riastrad 				      pi->soft_regs_start + reg_offset,
   1292  1.1  riastrad 				      value, pi->sram_end);
   1293  1.1  riastrad }
   1294  1.1  riastrad #endif
   1295  1.1  riastrad 
   1296  1.1  riastrad static int ci_write_smc_soft_register(struct radeon_device *rdev,
   1297  1.1  riastrad 				      u16 reg_offset, u32 value)
   1298  1.1  riastrad {
   1299  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1300  1.1  riastrad 
   1301  1.1  riastrad 	return ci_write_smc_sram_dword(rdev,
   1302  1.1  riastrad 				       pi->soft_regs_start + reg_offset,
   1303  1.1  riastrad 				       value, pi->sram_end);
   1304  1.1  riastrad }
   1305  1.1  riastrad 
   1306  1.1  riastrad static void ci_init_fps_limits(struct radeon_device *rdev)
   1307  1.1  riastrad {
   1308  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1309  1.1  riastrad 	SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
   1310  1.1  riastrad 
   1311  1.1  riastrad 	if (pi->caps_fps) {
   1312  1.1  riastrad 		u16 tmp;
   1313  1.1  riastrad 
   1314  1.1  riastrad 		tmp = 45;
   1315  1.1  riastrad 		table->FpsHighT = cpu_to_be16(tmp);
   1316  1.1  riastrad 
   1317  1.1  riastrad 		tmp = 30;
   1318  1.1  riastrad 		table->FpsLowT = cpu_to_be16(tmp);
   1319  1.1  riastrad 	}
   1320  1.1  riastrad }
   1321  1.1  riastrad 
   1322  1.1  riastrad static int ci_update_sclk_t(struct radeon_device *rdev)
   1323  1.1  riastrad {
   1324  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1325  1.1  riastrad 	int ret = 0;
   1326  1.1  riastrad 	u32 low_sclk_interrupt_t = 0;
   1327  1.1  riastrad 
   1328  1.1  riastrad 	if (pi->caps_sclk_throttle_low_notification) {
   1329  1.1  riastrad 		low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
   1330  1.1  riastrad 
   1331  1.1  riastrad 		ret = ci_copy_bytes_to_smc(rdev,
   1332  1.1  riastrad 					   pi->dpm_table_start +
   1333  1.1  riastrad 					   offsetof(SMU7_Discrete_DpmTable, LowSclkInterruptT),
   1334  1.1  riastrad 					   (u8 *)&low_sclk_interrupt_t,
   1335  1.1  riastrad 					   sizeof(u32), pi->sram_end);
   1336  1.1  riastrad 
   1337  1.1  riastrad 	}
   1338  1.1  riastrad 
   1339  1.1  riastrad 	return ret;
   1340  1.1  riastrad }
   1341  1.1  riastrad 
   1342  1.1  riastrad static void ci_get_leakage_voltages(struct radeon_device *rdev)
   1343  1.1  riastrad {
   1344  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1345  1.1  riastrad 	u16 leakage_id, virtual_voltage_id;
   1346  1.1  riastrad 	u16 vddc, vddci;
   1347  1.1  riastrad 	int i;
   1348  1.1  riastrad 
   1349  1.1  riastrad 	pi->vddc_leakage.count = 0;
   1350  1.1  riastrad 	pi->vddci_leakage.count = 0;
   1351  1.1  riastrad 
   1352  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
   1353  1.1  riastrad 		for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
   1354  1.1  riastrad 			virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
   1355  1.1  riastrad 			if (radeon_atom_get_voltage_evv(rdev, virtual_voltage_id, &vddc) != 0)
   1356  1.1  riastrad 				continue;
   1357  1.1  riastrad 			if (vddc != 0 && vddc != virtual_voltage_id) {
   1358  1.1  riastrad 				pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
   1359  1.1  riastrad 				pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
   1360  1.1  riastrad 				pi->vddc_leakage.count++;
   1361  1.1  riastrad 			}
   1362  1.1  riastrad 		}
   1363  1.1  riastrad 	} else if (radeon_atom_get_leakage_id_from_vbios(rdev, &leakage_id) == 0) {
   1364  1.1  riastrad 		for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
   1365  1.1  riastrad 			virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
   1366  1.1  riastrad 			if (radeon_atom_get_leakage_vddc_based_on_leakage_params(rdev, &vddc, &vddci,
   1367  1.1  riastrad 										 virtual_voltage_id,
   1368  1.1  riastrad 										 leakage_id) == 0) {
   1369  1.1  riastrad 				if (vddc != 0 && vddc != virtual_voltage_id) {
   1370  1.1  riastrad 					pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
   1371  1.1  riastrad 					pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
   1372  1.1  riastrad 					pi->vddc_leakage.count++;
   1373  1.1  riastrad 				}
   1374  1.1  riastrad 				if (vddci != 0 && vddci != virtual_voltage_id) {
   1375  1.1  riastrad 					pi->vddci_leakage.actual_voltage[pi->vddci_leakage.count] = vddci;
   1376  1.1  riastrad 					pi->vddci_leakage.leakage_id[pi->vddci_leakage.count] = virtual_voltage_id;
   1377  1.1  riastrad 					pi->vddci_leakage.count++;
   1378  1.1  riastrad 				}
   1379  1.1  riastrad 			}
   1380  1.1  riastrad 		}
   1381  1.1  riastrad 	}
   1382  1.1  riastrad }
   1383  1.1  riastrad 
   1384  1.1  riastrad static void ci_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
   1385  1.1  riastrad {
   1386  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1387  1.1  riastrad 	bool want_thermal_protection;
   1388  1.1  riastrad 	enum radeon_dpm_event_src dpm_event_src;
   1389  1.1  riastrad 	u32 tmp;
   1390  1.1  riastrad 
   1391  1.1  riastrad 	switch (sources) {
   1392  1.1  riastrad 	case 0:
   1393  1.1  riastrad 	default:
   1394  1.1  riastrad 		want_thermal_protection = false;
   1395  1.1  riastrad 		break;
   1396  1.1  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
   1397  1.1  riastrad 		want_thermal_protection = true;
   1398  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGITAL;
   1399  1.1  riastrad 		break;
   1400  1.1  riastrad 	case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
   1401  1.1  riastrad 		want_thermal_protection = true;
   1402  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_EXTERNAL;
   1403  1.1  riastrad 		break;
   1404  1.1  riastrad 	case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
   1405  1.1  riastrad 	      (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
   1406  1.1  riastrad 		want_thermal_protection = true;
   1407  1.1  riastrad 		dpm_event_src = RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL;
   1408  1.1  riastrad 		break;
   1409  1.1  riastrad 	}
   1410  1.1  riastrad 
   1411  1.1  riastrad 	if (want_thermal_protection) {
   1412  1.1  riastrad #if 0
   1413  1.1  riastrad 		/* XXX: need to figure out how to handle this properly */
   1414  1.1  riastrad 		tmp = RREG32_SMC(CG_THERMAL_CTRL);
   1415  1.1  riastrad 		tmp &= DPM_EVENT_SRC_MASK;
   1416  1.1  riastrad 		tmp |= DPM_EVENT_SRC(dpm_event_src);
   1417  1.1  riastrad 		WREG32_SMC(CG_THERMAL_CTRL, tmp);
   1418  1.1  riastrad #else
   1419  1.1  riastrad 		(void)dpm_event_src;
   1420  1.1  riastrad #endif
   1421  1.1  riastrad 
   1422  1.1  riastrad 		tmp = RREG32_SMC(GENERAL_PWRMGT);
   1423  1.1  riastrad 		if (pi->thermal_protection)
   1424  1.1  riastrad 			tmp &= ~THERMAL_PROTECTION_DIS;
   1425  1.1  riastrad 		else
   1426  1.1  riastrad 			tmp |= THERMAL_PROTECTION_DIS;
   1427  1.1  riastrad 		WREG32_SMC(GENERAL_PWRMGT, tmp);
   1428  1.1  riastrad 	} else {
   1429  1.1  riastrad 		tmp = RREG32_SMC(GENERAL_PWRMGT);
   1430  1.1  riastrad 		tmp |= THERMAL_PROTECTION_DIS;
   1431  1.1  riastrad 		WREG32_SMC(GENERAL_PWRMGT, tmp);
   1432  1.1  riastrad 	}
   1433  1.1  riastrad }
   1434  1.1  riastrad 
   1435  1.1  riastrad static void ci_enable_auto_throttle_source(struct radeon_device *rdev,
   1436  1.1  riastrad 					   enum radeon_dpm_auto_throttle_src source,
   1437  1.1  riastrad 					   bool enable)
   1438  1.1  riastrad {
   1439  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1440  1.1  riastrad 
   1441  1.1  riastrad 	if (enable) {
   1442  1.1  riastrad 		if (!(pi->active_auto_throttle_sources & (1 << source))) {
   1443  1.1  riastrad 			pi->active_auto_throttle_sources |= 1 << source;
   1444  1.1  riastrad 			ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1445  1.1  riastrad 		}
   1446  1.1  riastrad 	} else {
   1447  1.1  riastrad 		if (pi->active_auto_throttle_sources & (1 << source)) {
   1448  1.1  riastrad 			pi->active_auto_throttle_sources &= ~(1 << source);
   1449  1.1  riastrad 			ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
   1450  1.1  riastrad 		}
   1451  1.1  riastrad 	}
   1452  1.1  riastrad }
   1453  1.1  riastrad 
   1454  1.1  riastrad static void ci_enable_vr_hot_gpio_interrupt(struct radeon_device *rdev)
   1455  1.1  riastrad {
   1456  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT)
   1457  1.1  riastrad 		ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableVRHotGPIOInterrupt);
   1458  1.1  riastrad }
   1459  1.1  riastrad 
   1460  1.1  riastrad static int ci_unfreeze_sclk_mclk_dpm(struct radeon_device *rdev)
   1461  1.1  riastrad {
   1462  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1463  1.1  riastrad 	PPSMC_Result smc_result;
   1464  1.1  riastrad 
   1465  1.1  riastrad 	if (!pi->need_update_smu7_dpm_table)
   1466  1.1  riastrad 		return 0;
   1467  1.1  riastrad 
   1468  1.1  riastrad 	if ((!pi->sclk_dpm_key_disabled) &&
   1469  1.1  riastrad 	    (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
   1470  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
   1471  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1472  1.1  riastrad 			return -EINVAL;
   1473  1.1  riastrad 	}
   1474  1.1  riastrad 
   1475  1.1  riastrad 	if ((!pi->mclk_dpm_key_disabled) &&
   1476  1.1  riastrad 	    (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
   1477  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
   1478  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1479  1.1  riastrad 			return -EINVAL;
   1480  1.1  riastrad 	}
   1481  1.1  riastrad 
   1482  1.1  riastrad 	pi->need_update_smu7_dpm_table = 0;
   1483  1.1  riastrad 	return 0;
   1484  1.1  riastrad }
   1485  1.1  riastrad 
   1486  1.1  riastrad static int ci_enable_sclk_mclk_dpm(struct radeon_device *rdev, bool enable)
   1487  1.1  riastrad {
   1488  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1489  1.1  riastrad 	PPSMC_Result smc_result;
   1490  1.1  riastrad 
   1491  1.1  riastrad 	if (enable) {
   1492  1.1  riastrad 		if (!pi->sclk_dpm_key_disabled) {
   1493  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Enable);
   1494  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   1495  1.1  riastrad 				return -EINVAL;
   1496  1.1  riastrad 		}
   1497  1.1  riastrad 
   1498  1.1  riastrad 		if (!pi->mclk_dpm_key_disabled) {
   1499  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Enable);
   1500  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   1501  1.1  riastrad 				return -EINVAL;
   1502  1.1  riastrad 
   1503  1.1  riastrad 			WREG32_P(MC_SEQ_CNTL_3, CAC_EN, ~CAC_EN);
   1504  1.1  riastrad 
   1505  1.1  riastrad 			WREG32_SMC(LCAC_MC0_CNTL, 0x05);
   1506  1.1  riastrad 			WREG32_SMC(LCAC_MC1_CNTL, 0x05);
   1507  1.1  riastrad 			WREG32_SMC(LCAC_CPL_CNTL, 0x100005);
   1508  1.1  riastrad 
   1509  1.1  riastrad 			udelay(10);
   1510  1.1  riastrad 
   1511  1.1  riastrad 			WREG32_SMC(LCAC_MC0_CNTL, 0x400005);
   1512  1.1  riastrad 			WREG32_SMC(LCAC_MC1_CNTL, 0x400005);
   1513  1.1  riastrad 			WREG32_SMC(LCAC_CPL_CNTL, 0x500005);
   1514  1.1  riastrad 		}
   1515  1.1  riastrad 	} else {
   1516  1.1  riastrad 		if (!pi->sclk_dpm_key_disabled) {
   1517  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Disable);
   1518  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   1519  1.1  riastrad 				return -EINVAL;
   1520  1.1  riastrad 		}
   1521  1.1  riastrad 
   1522  1.1  riastrad 		if (!pi->mclk_dpm_key_disabled) {
   1523  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Disable);
   1524  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   1525  1.1  riastrad 				return -EINVAL;
   1526  1.1  riastrad 		}
   1527  1.1  riastrad 	}
   1528  1.1  riastrad 
   1529  1.1  riastrad 	return 0;
   1530  1.1  riastrad }
   1531  1.1  riastrad 
   1532  1.1  riastrad static int ci_start_dpm(struct radeon_device *rdev)
   1533  1.1  riastrad {
   1534  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1535  1.1  riastrad 	PPSMC_Result smc_result;
   1536  1.1  riastrad 	int ret;
   1537  1.1  riastrad 	u32 tmp;
   1538  1.1  riastrad 
   1539  1.1  riastrad 	tmp = RREG32_SMC(GENERAL_PWRMGT);
   1540  1.1  riastrad 	tmp |= GLOBAL_PWRMGT_EN;
   1541  1.1  riastrad 	WREG32_SMC(GENERAL_PWRMGT, tmp);
   1542  1.1  riastrad 
   1543  1.1  riastrad 	tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
   1544  1.1  riastrad 	tmp |= DYNAMIC_PM_EN;
   1545  1.1  riastrad 	WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
   1546  1.1  riastrad 
   1547  1.1  riastrad 	ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VoltageChangeTimeout), 0x1000);
   1548  1.1  riastrad 
   1549  1.1  riastrad 	WREG32_P(BIF_LNCNT_RESET, 0, ~RESET_LNCNT_EN);
   1550  1.1  riastrad 
   1551  1.1  riastrad 	smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Enable);
   1552  1.1  riastrad 	if (smc_result != PPSMC_Result_OK)
   1553  1.1  riastrad 		return -EINVAL;
   1554  1.1  riastrad 
   1555  1.1  riastrad 	ret = ci_enable_sclk_mclk_dpm(rdev, true);
   1556  1.1  riastrad 	if (ret)
   1557  1.1  riastrad 		return ret;
   1558  1.1  riastrad 
   1559  1.1  riastrad 	if (!pi->pcie_dpm_key_disabled) {
   1560  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Enable);
   1561  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1562  1.1  riastrad 			return -EINVAL;
   1563  1.1  riastrad 	}
   1564  1.1  riastrad 
   1565  1.1  riastrad 	return 0;
   1566  1.1  riastrad }
   1567  1.1  riastrad 
   1568  1.1  riastrad static int ci_freeze_sclk_mclk_dpm(struct radeon_device *rdev)
   1569  1.1  riastrad {
   1570  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1571  1.1  riastrad 	PPSMC_Result smc_result;
   1572  1.1  riastrad 
   1573  1.1  riastrad 	if (!pi->need_update_smu7_dpm_table)
   1574  1.1  riastrad 		return 0;
   1575  1.1  riastrad 
   1576  1.1  riastrad 	if ((!pi->sclk_dpm_key_disabled) &&
   1577  1.1  riastrad 	    (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
   1578  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_FreezeLevel);
   1579  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1580  1.1  riastrad 			return -EINVAL;
   1581  1.1  riastrad 	}
   1582  1.1  riastrad 
   1583  1.1  riastrad 	if ((!pi->mclk_dpm_key_disabled) &&
   1584  1.1  riastrad 	    (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
   1585  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_FreezeLevel);
   1586  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1587  1.1  riastrad 			return -EINVAL;
   1588  1.1  riastrad 	}
   1589  1.1  riastrad 
   1590  1.1  riastrad 	return 0;
   1591  1.1  riastrad }
   1592  1.1  riastrad 
   1593  1.1  riastrad static int ci_stop_dpm(struct radeon_device *rdev)
   1594  1.1  riastrad {
   1595  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1596  1.1  riastrad 	PPSMC_Result smc_result;
   1597  1.1  riastrad 	int ret;
   1598  1.1  riastrad 	u32 tmp;
   1599  1.1  riastrad 
   1600  1.1  riastrad 	tmp = RREG32_SMC(GENERAL_PWRMGT);
   1601  1.1  riastrad 	tmp &= ~GLOBAL_PWRMGT_EN;
   1602  1.1  riastrad 	WREG32_SMC(GENERAL_PWRMGT, tmp);
   1603  1.1  riastrad 
   1604  1.1  riastrad 	tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
   1605  1.1  riastrad 	tmp &= ~DYNAMIC_PM_EN;
   1606  1.1  riastrad 	WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
   1607  1.1  riastrad 
   1608  1.1  riastrad 	if (!pi->pcie_dpm_key_disabled) {
   1609  1.1  riastrad 		smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Disable);
   1610  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1611  1.1  riastrad 			return -EINVAL;
   1612  1.1  riastrad 	}
   1613  1.1  riastrad 
   1614  1.1  riastrad 	ret = ci_enable_sclk_mclk_dpm(rdev, false);
   1615  1.1  riastrad 	if (ret)
   1616  1.1  riastrad 		return ret;
   1617  1.1  riastrad 
   1618  1.1  riastrad 	smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Disable);
   1619  1.1  riastrad 	if (smc_result != PPSMC_Result_OK)
   1620  1.1  riastrad 		return -EINVAL;
   1621  1.1  riastrad 
   1622  1.1  riastrad 	return 0;
   1623  1.1  riastrad }
   1624  1.1  riastrad 
   1625  1.1  riastrad static void ci_enable_sclk_control(struct radeon_device *rdev, bool enable)
   1626  1.1  riastrad {
   1627  1.1  riastrad 	u32 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
   1628  1.1  riastrad 
   1629  1.1  riastrad 	if (enable)
   1630  1.1  riastrad 		tmp &= ~SCLK_PWRMGT_OFF;
   1631  1.1  riastrad 	else
   1632  1.1  riastrad 		tmp |= SCLK_PWRMGT_OFF;
   1633  1.1  riastrad 	WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
   1634  1.1  riastrad }
   1635  1.1  riastrad 
   1636  1.1  riastrad #if 0
   1637  1.1  riastrad static int ci_notify_hw_of_power_source(struct radeon_device *rdev,
   1638  1.1  riastrad 					bool ac_power)
   1639  1.1  riastrad {
   1640  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1641  1.1  riastrad 	struct radeon_cac_tdp_table *cac_tdp_table =
   1642  1.1  riastrad 		rdev->pm.dpm.dyn_state.cac_tdp_table;
   1643  1.1  riastrad 	u32 power_limit;
   1644  1.1  riastrad 
   1645  1.1  riastrad 	if (ac_power)
   1646  1.1  riastrad 		power_limit = (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
   1647  1.1  riastrad 	else
   1648  1.1  riastrad 		power_limit = (u32)(cac_tdp_table->battery_power_limit * 256);
   1649  1.1  riastrad 
   1650  1.4  riastrad 	ci_set_power_limit(rdev, power_limit);
   1651  1.1  riastrad 
   1652  1.1  riastrad 	if (pi->caps_automatic_dc_transition) {
   1653  1.1  riastrad 		if (ac_power)
   1654  1.1  riastrad 			ci_send_msg_to_smc(rdev, PPSMC_MSG_RunningOnAC);
   1655  1.1  riastrad 		else
   1656  1.1  riastrad 			ci_send_msg_to_smc(rdev, PPSMC_MSG_Remove_DC_Clamp);
   1657  1.1  riastrad 	}
   1658  1.1  riastrad 
   1659  1.1  riastrad 	return 0;
   1660  1.1  riastrad }
   1661  1.1  riastrad #endif
   1662  1.1  riastrad 
   1663  1.4  riastrad static PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
   1664  1.4  riastrad {
   1665  1.4  riastrad 	u32 tmp;
   1666  1.4  riastrad 	int i;
   1667  1.4  riastrad 
   1668  1.4  riastrad 	if (!ci_is_smc_running(rdev))
   1669  1.4  riastrad 		return PPSMC_Result_Failed;
   1670  1.4  riastrad 
   1671  1.4  riastrad 	WREG32(SMC_MESSAGE_0, msg);
   1672  1.4  riastrad 
   1673  1.4  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   1674  1.4  riastrad 		tmp = RREG32(SMC_RESP_0);
   1675  1.4  riastrad 		if (tmp != 0)
   1676  1.4  riastrad 			break;
   1677  1.4  riastrad 		udelay(1);
   1678  1.4  riastrad 	}
   1679  1.4  riastrad 	tmp = RREG32(SMC_RESP_0);
   1680  1.4  riastrad 
   1681  1.4  riastrad 	return (PPSMC_Result)tmp;
   1682  1.4  riastrad }
   1683  1.4  riastrad 
   1684  1.1  riastrad static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
   1685  1.1  riastrad 						      PPSMC_Msg msg, u32 parameter)
   1686  1.1  riastrad {
   1687  1.1  riastrad 	WREG32(SMC_MSG_ARG_0, parameter);
   1688  1.1  riastrad 	return ci_send_msg_to_smc(rdev, msg);
   1689  1.1  riastrad }
   1690  1.1  riastrad 
   1691  1.1  riastrad static PPSMC_Result ci_send_msg_to_smc_return_parameter(struct radeon_device *rdev,
   1692  1.1  riastrad 							PPSMC_Msg msg, u32 *parameter)
   1693  1.1  riastrad {
   1694  1.1  riastrad 	PPSMC_Result smc_result;
   1695  1.1  riastrad 
   1696  1.1  riastrad 	smc_result = ci_send_msg_to_smc(rdev, msg);
   1697  1.1  riastrad 
   1698  1.1  riastrad 	if ((smc_result == PPSMC_Result_OK) && parameter)
   1699  1.1  riastrad 		*parameter = RREG32(SMC_MSG_ARG_0);
   1700  1.1  riastrad 
   1701  1.1  riastrad 	return smc_result;
   1702  1.1  riastrad }
   1703  1.1  riastrad 
   1704  1.1  riastrad static int ci_dpm_force_state_sclk(struct radeon_device *rdev, u32 n)
   1705  1.1  riastrad {
   1706  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1707  1.1  riastrad 
   1708  1.1  riastrad 	if (!pi->sclk_dpm_key_disabled) {
   1709  1.1  riastrad 		PPSMC_Result smc_result =
   1710  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SCLKDPM_SetEnabledMask, 1 << n);
   1711  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1712  1.1  riastrad 			return -EINVAL;
   1713  1.1  riastrad 	}
   1714  1.1  riastrad 
   1715  1.1  riastrad 	return 0;
   1716  1.1  riastrad }
   1717  1.1  riastrad 
   1718  1.1  riastrad static int ci_dpm_force_state_mclk(struct radeon_device *rdev, u32 n)
   1719  1.1  riastrad {
   1720  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1721  1.1  riastrad 
   1722  1.1  riastrad 	if (!pi->mclk_dpm_key_disabled) {
   1723  1.1  riastrad 		PPSMC_Result smc_result =
   1724  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_MCLKDPM_SetEnabledMask, 1 << n);
   1725  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1726  1.1  riastrad 			return -EINVAL;
   1727  1.1  riastrad 	}
   1728  1.1  riastrad 
   1729  1.1  riastrad 	return 0;
   1730  1.1  riastrad }
   1731  1.1  riastrad 
   1732  1.1  riastrad static int ci_dpm_force_state_pcie(struct radeon_device *rdev, u32 n)
   1733  1.1  riastrad {
   1734  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1735  1.1  riastrad 
   1736  1.1  riastrad 	if (!pi->pcie_dpm_key_disabled) {
   1737  1.1  riastrad 		PPSMC_Result smc_result =
   1738  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PCIeDPM_ForceLevel, n);
   1739  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1740  1.1  riastrad 			return -EINVAL;
   1741  1.1  riastrad 	}
   1742  1.1  riastrad 
   1743  1.1  riastrad 	return 0;
   1744  1.1  riastrad }
   1745  1.1  riastrad 
   1746  1.1  riastrad static int ci_set_power_limit(struct radeon_device *rdev, u32 n)
   1747  1.1  riastrad {
   1748  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1749  1.1  riastrad 
   1750  1.1  riastrad 	if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit) {
   1751  1.1  riastrad 		PPSMC_Result smc_result =
   1752  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PkgPwrSetLimit, n);
   1753  1.1  riastrad 		if (smc_result != PPSMC_Result_OK)
   1754  1.1  riastrad 			return -EINVAL;
   1755  1.1  riastrad 	}
   1756  1.1  riastrad 
   1757  1.1  riastrad 	return 0;
   1758  1.1  riastrad }
   1759  1.1  riastrad 
   1760  1.1  riastrad static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
   1761  1.1  riastrad 				       u32 target_tdp)
   1762  1.1  riastrad {
   1763  1.1  riastrad 	PPSMC_Result smc_result =
   1764  1.1  riastrad 		ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_OverDriveSetTargetTdp, target_tdp);
   1765  1.1  riastrad 	if (smc_result != PPSMC_Result_OK)
   1766  1.1  riastrad 		return -EINVAL;
   1767  1.1  riastrad 	return 0;
   1768  1.1  riastrad }
   1769  1.1  riastrad 
   1770  1.1  riastrad #if 0
   1771  1.1  riastrad static int ci_set_boot_state(struct radeon_device *rdev)
   1772  1.1  riastrad {
   1773  1.1  riastrad 	return ci_enable_sclk_mclk_dpm(rdev, false);
   1774  1.1  riastrad }
   1775  1.1  riastrad #endif
   1776  1.1  riastrad 
   1777  1.1  riastrad static u32 ci_get_average_sclk_freq(struct radeon_device *rdev)
   1778  1.1  riastrad {
   1779  1.1  riastrad 	u32 sclk_freq;
   1780  1.1  riastrad 	PPSMC_Result smc_result =
   1781  1.1  riastrad 		ci_send_msg_to_smc_return_parameter(rdev,
   1782  1.1  riastrad 						    PPSMC_MSG_API_GetSclkFrequency,
   1783  1.1  riastrad 						    &sclk_freq);
   1784  1.1  riastrad 	if (smc_result != PPSMC_Result_OK)
   1785  1.1  riastrad 		sclk_freq = 0;
   1786  1.1  riastrad 
   1787  1.1  riastrad 	return sclk_freq;
   1788  1.1  riastrad }
   1789  1.1  riastrad 
   1790  1.1  riastrad static u32 ci_get_average_mclk_freq(struct radeon_device *rdev)
   1791  1.1  riastrad {
   1792  1.1  riastrad 	u32 mclk_freq;
   1793  1.1  riastrad 	PPSMC_Result smc_result =
   1794  1.1  riastrad 		ci_send_msg_to_smc_return_parameter(rdev,
   1795  1.1  riastrad 						    PPSMC_MSG_API_GetMclkFrequency,
   1796  1.1  riastrad 						    &mclk_freq);
   1797  1.1  riastrad 	if (smc_result != PPSMC_Result_OK)
   1798  1.1  riastrad 		mclk_freq = 0;
   1799  1.1  riastrad 
   1800  1.1  riastrad 	return mclk_freq;
   1801  1.1  riastrad }
   1802  1.1  riastrad 
   1803  1.1  riastrad static void ci_dpm_start_smc(struct radeon_device *rdev)
   1804  1.1  riastrad {
   1805  1.1  riastrad 	int i;
   1806  1.1  riastrad 
   1807  1.1  riastrad 	ci_program_jump_on_start(rdev);
   1808  1.1  riastrad 	ci_start_smc_clock(rdev);
   1809  1.1  riastrad 	ci_start_smc(rdev);
   1810  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   1811  1.1  riastrad 		if (RREG32_SMC(FIRMWARE_FLAGS) & INTERRUPTS_ENABLED)
   1812  1.1  riastrad 			break;
   1813  1.1  riastrad 	}
   1814  1.1  riastrad }
   1815  1.1  riastrad 
   1816  1.1  riastrad static void ci_dpm_stop_smc(struct radeon_device *rdev)
   1817  1.1  riastrad {
   1818  1.1  riastrad 	ci_reset_smc(rdev);
   1819  1.1  riastrad 	ci_stop_smc_clock(rdev);
   1820  1.1  riastrad }
   1821  1.1  riastrad 
   1822  1.1  riastrad static int ci_process_firmware_header(struct radeon_device *rdev)
   1823  1.1  riastrad {
   1824  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1825  1.1  riastrad 	u32 tmp;
   1826  1.1  riastrad 	int ret;
   1827  1.1  riastrad 
   1828  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
   1829  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
   1830  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, DpmTable),
   1831  1.1  riastrad 				     &tmp, pi->sram_end);
   1832  1.1  riastrad 	if (ret)
   1833  1.1  riastrad 		return ret;
   1834  1.1  riastrad 
   1835  1.1  riastrad 	pi->dpm_table_start = tmp;
   1836  1.1  riastrad 
   1837  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
   1838  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
   1839  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, SoftRegisters),
   1840  1.1  riastrad 				     &tmp, pi->sram_end);
   1841  1.1  riastrad 	if (ret)
   1842  1.1  riastrad 		return ret;
   1843  1.1  riastrad 
   1844  1.1  riastrad 	pi->soft_regs_start = tmp;
   1845  1.1  riastrad 
   1846  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
   1847  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
   1848  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, mcRegisterTable),
   1849  1.1  riastrad 				     &tmp, pi->sram_end);
   1850  1.1  riastrad 	if (ret)
   1851  1.1  riastrad 		return ret;
   1852  1.1  riastrad 
   1853  1.1  riastrad 	pi->mc_reg_table_start = tmp;
   1854  1.1  riastrad 
   1855  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
   1856  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
   1857  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, FanTable),
   1858  1.1  riastrad 				     &tmp, pi->sram_end);
   1859  1.1  riastrad 	if (ret)
   1860  1.1  riastrad 		return ret;
   1861  1.1  riastrad 
   1862  1.1  riastrad 	pi->fan_table_start = tmp;
   1863  1.1  riastrad 
   1864  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev,
   1865  1.1  riastrad 				     SMU7_FIRMWARE_HEADER_LOCATION +
   1866  1.1  riastrad 				     offsetof(SMU7_Firmware_Header, mcArbDramTimingTable),
   1867  1.1  riastrad 				     &tmp, pi->sram_end);
   1868  1.1  riastrad 	if (ret)
   1869  1.1  riastrad 		return ret;
   1870  1.1  riastrad 
   1871  1.1  riastrad 	pi->arb_table_start = tmp;
   1872  1.1  riastrad 
   1873  1.1  riastrad 	return 0;
   1874  1.1  riastrad }
   1875  1.1  riastrad 
   1876  1.1  riastrad static void ci_read_clock_registers(struct radeon_device *rdev)
   1877  1.1  riastrad {
   1878  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1879  1.1  riastrad 
   1880  1.1  riastrad 	pi->clock_registers.cg_spll_func_cntl =
   1881  1.1  riastrad 		RREG32_SMC(CG_SPLL_FUNC_CNTL);
   1882  1.1  riastrad 	pi->clock_registers.cg_spll_func_cntl_2 =
   1883  1.1  riastrad 		RREG32_SMC(CG_SPLL_FUNC_CNTL_2);
   1884  1.1  riastrad 	pi->clock_registers.cg_spll_func_cntl_3 =
   1885  1.1  riastrad 		RREG32_SMC(CG_SPLL_FUNC_CNTL_3);
   1886  1.1  riastrad 	pi->clock_registers.cg_spll_func_cntl_4 =
   1887  1.1  riastrad 		RREG32_SMC(CG_SPLL_FUNC_CNTL_4);
   1888  1.1  riastrad 	pi->clock_registers.cg_spll_spread_spectrum =
   1889  1.1  riastrad 		RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
   1890  1.1  riastrad 	pi->clock_registers.cg_spll_spread_spectrum_2 =
   1891  1.1  riastrad 		RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM_2);
   1892  1.1  riastrad 	pi->clock_registers.dll_cntl = RREG32(DLL_CNTL);
   1893  1.1  riastrad 	pi->clock_registers.mclk_pwrmgt_cntl = RREG32(MCLK_PWRMGT_CNTL);
   1894  1.1  riastrad 	pi->clock_registers.mpll_ad_func_cntl = RREG32(MPLL_AD_FUNC_CNTL);
   1895  1.1  riastrad 	pi->clock_registers.mpll_dq_func_cntl = RREG32(MPLL_DQ_FUNC_CNTL);
   1896  1.1  riastrad 	pi->clock_registers.mpll_func_cntl = RREG32(MPLL_FUNC_CNTL);
   1897  1.1  riastrad 	pi->clock_registers.mpll_func_cntl_1 = RREG32(MPLL_FUNC_CNTL_1);
   1898  1.1  riastrad 	pi->clock_registers.mpll_func_cntl_2 = RREG32(MPLL_FUNC_CNTL_2);
   1899  1.1  riastrad 	pi->clock_registers.mpll_ss1 = RREG32(MPLL_SS1);
   1900  1.1  riastrad 	pi->clock_registers.mpll_ss2 = RREG32(MPLL_SS2);
   1901  1.1  riastrad }
   1902  1.1  riastrad 
   1903  1.1  riastrad static void ci_init_sclk_t(struct radeon_device *rdev)
   1904  1.1  riastrad {
   1905  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1906  1.1  riastrad 
   1907  1.1  riastrad 	pi->low_sclk_interrupt_t = 0;
   1908  1.1  riastrad }
   1909  1.1  riastrad 
   1910  1.1  riastrad static void ci_enable_thermal_protection(struct radeon_device *rdev,
   1911  1.1  riastrad 					 bool enable)
   1912  1.1  riastrad {
   1913  1.1  riastrad 	u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
   1914  1.1  riastrad 
   1915  1.1  riastrad 	if (enable)
   1916  1.1  riastrad 		tmp &= ~THERMAL_PROTECTION_DIS;
   1917  1.1  riastrad 	else
   1918  1.1  riastrad 		tmp |= THERMAL_PROTECTION_DIS;
   1919  1.1  riastrad 	WREG32_SMC(GENERAL_PWRMGT, tmp);
   1920  1.1  riastrad }
   1921  1.1  riastrad 
   1922  1.1  riastrad static void ci_enable_acpi_power_management(struct radeon_device *rdev)
   1923  1.1  riastrad {
   1924  1.1  riastrad 	u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
   1925  1.1  riastrad 
   1926  1.1  riastrad 	tmp |= STATIC_PM_EN;
   1927  1.1  riastrad 
   1928  1.1  riastrad 	WREG32_SMC(GENERAL_PWRMGT, tmp);
   1929  1.1  riastrad }
   1930  1.1  riastrad 
   1931  1.1  riastrad #if 0
   1932  1.1  riastrad static int ci_enter_ulp_state(struct radeon_device *rdev)
   1933  1.1  riastrad {
   1934  1.1  riastrad 
   1935  1.1  riastrad 	WREG32(SMC_MESSAGE_0, PPSMC_MSG_SwitchToMinimumPower);
   1936  1.1  riastrad 
   1937  1.1  riastrad 	udelay(25000);
   1938  1.1  riastrad 
   1939  1.1  riastrad 	return 0;
   1940  1.1  riastrad }
   1941  1.1  riastrad 
   1942  1.1  riastrad static int ci_exit_ulp_state(struct radeon_device *rdev)
   1943  1.1  riastrad {
   1944  1.1  riastrad 	int i;
   1945  1.1  riastrad 
   1946  1.1  riastrad 	WREG32(SMC_MESSAGE_0, PPSMC_MSG_ResumeFromMinimumPower);
   1947  1.1  riastrad 
   1948  1.1  riastrad 	udelay(7000);
   1949  1.1  riastrad 
   1950  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   1951  1.1  riastrad 		if (RREG32(SMC_RESP_0) == 1)
   1952  1.1  riastrad 			break;
   1953  1.1  riastrad 		udelay(1000);
   1954  1.1  riastrad 	}
   1955  1.1  riastrad 
   1956  1.1  riastrad 	return 0;
   1957  1.1  riastrad }
   1958  1.1  riastrad #endif
   1959  1.1  riastrad 
   1960  1.1  riastrad static int ci_notify_smc_display_change(struct radeon_device *rdev,
   1961  1.1  riastrad 					bool has_display)
   1962  1.1  riastrad {
   1963  1.1  riastrad 	PPSMC_Msg msg = has_display ? PPSMC_MSG_HasDisplay : PPSMC_MSG_NoDisplay;
   1964  1.1  riastrad 
   1965  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, msg) == PPSMC_Result_OK) ?  0 : -EINVAL;
   1966  1.1  riastrad }
   1967  1.1  riastrad 
   1968  1.1  riastrad static int ci_enable_ds_master_switch(struct radeon_device *rdev,
   1969  1.1  riastrad 				      bool enable)
   1970  1.1  riastrad {
   1971  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   1972  1.1  riastrad 
   1973  1.1  riastrad 	if (enable) {
   1974  1.1  riastrad 		if (pi->caps_sclk_ds) {
   1975  1.1  riastrad 			if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_ON) != PPSMC_Result_OK)
   1976  1.1  riastrad 				return -EINVAL;
   1977  1.1  riastrad 		} else {
   1978  1.1  riastrad 			if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
   1979  1.1  riastrad 				return -EINVAL;
   1980  1.1  riastrad 		}
   1981  1.1  riastrad 	} else {
   1982  1.1  riastrad 		if (pi->caps_sclk_ds) {
   1983  1.1  riastrad 			if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
   1984  1.1  riastrad 				return -EINVAL;
   1985  1.1  riastrad 		}
   1986  1.1  riastrad 	}
   1987  1.1  riastrad 
   1988  1.1  riastrad 	return 0;
   1989  1.1  riastrad }
   1990  1.1  riastrad 
   1991  1.1  riastrad static void ci_program_display_gap(struct radeon_device *rdev)
   1992  1.1  riastrad {
   1993  1.1  riastrad 	u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
   1994  1.1  riastrad 	u32 pre_vbi_time_in_us;
   1995  1.1  riastrad 	u32 frame_time_in_us;
   1996  1.1  riastrad 	u32 ref_clock = rdev->clock.spll.reference_freq;
   1997  1.1  riastrad 	u32 refresh_rate = r600_dpm_get_vrefresh(rdev);
   1998  1.1  riastrad 	u32 vblank_time = r600_dpm_get_vblank_time(rdev);
   1999  1.1  riastrad 
   2000  1.1  riastrad 	tmp &= ~DISP_GAP_MASK;
   2001  1.1  riastrad 	if (rdev->pm.dpm.new_active_crtc_count > 0)
   2002  1.1  riastrad 		tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM);
   2003  1.1  riastrad 	else
   2004  1.1  riastrad 		tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE);
   2005  1.1  riastrad 	WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
   2006  1.1  riastrad 
   2007  1.1  riastrad 	if (refresh_rate == 0)
   2008  1.1  riastrad 		refresh_rate = 60;
   2009  1.1  riastrad 	if (vblank_time == 0xffffffff)
   2010  1.1  riastrad 		vblank_time = 500;
   2011  1.1  riastrad 	frame_time_in_us = 1000000 / refresh_rate;
   2012  1.1  riastrad 	pre_vbi_time_in_us =
   2013  1.1  riastrad 		frame_time_in_us - 200 - vblank_time;
   2014  1.1  riastrad 	tmp = pre_vbi_time_in_us * (ref_clock / 100);
   2015  1.1  riastrad 
   2016  1.1  riastrad 	WREG32_SMC(CG_DISPLAY_GAP_CNTL2, tmp);
   2017  1.1  riastrad 	ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, PreVBlankGap), 0x64);
   2018  1.1  riastrad 	ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VBlankTimeout), (frame_time_in_us - pre_vbi_time_in_us));
   2019  1.1  riastrad 
   2020  1.1  riastrad 
   2021  1.1  riastrad 	ci_notify_smc_display_change(rdev, (rdev->pm.dpm.new_active_crtc_count == 1));
   2022  1.1  riastrad 
   2023  1.1  riastrad }
   2024  1.1  riastrad 
   2025  1.1  riastrad static void ci_enable_spread_spectrum(struct radeon_device *rdev, bool enable)
   2026  1.1  riastrad {
   2027  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2028  1.1  riastrad 	u32 tmp;
   2029  1.1  riastrad 
   2030  1.1  riastrad 	if (enable) {
   2031  1.1  riastrad 		if (pi->caps_sclk_ss_support) {
   2032  1.1  riastrad 			tmp = RREG32_SMC(GENERAL_PWRMGT);
   2033  1.1  riastrad 			tmp |= DYN_SPREAD_SPECTRUM_EN;
   2034  1.1  riastrad 			WREG32_SMC(GENERAL_PWRMGT, tmp);
   2035  1.1  riastrad 		}
   2036  1.1  riastrad 	} else {
   2037  1.1  riastrad 		tmp = RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
   2038  1.1  riastrad 		tmp &= ~SSEN;
   2039  1.1  riastrad 		WREG32_SMC(CG_SPLL_SPREAD_SPECTRUM, tmp);
   2040  1.1  riastrad 
   2041  1.1  riastrad 		tmp = RREG32_SMC(GENERAL_PWRMGT);
   2042  1.1  riastrad 		tmp &= ~DYN_SPREAD_SPECTRUM_EN;
   2043  1.1  riastrad 		WREG32_SMC(GENERAL_PWRMGT, tmp);
   2044  1.1  riastrad 	}
   2045  1.1  riastrad }
   2046  1.1  riastrad 
   2047  1.1  riastrad static void ci_program_sstp(struct radeon_device *rdev)
   2048  1.1  riastrad {
   2049  1.1  riastrad 	WREG32_SMC(CG_SSP, (SSTU(R600_SSTU_DFLT) | SST(R600_SST_DFLT)));
   2050  1.1  riastrad }
   2051  1.1  riastrad 
   2052  1.1  riastrad static void ci_enable_display_gap(struct radeon_device *rdev)
   2053  1.1  riastrad {
   2054  1.1  riastrad 	u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
   2055  1.1  riastrad 
   2056  1.4  riastrad 	tmp &= ~(DISP_GAP_MASK | DISP_GAP_MCHG_MASK);
   2057  1.4  riastrad 	tmp |= (DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE) |
   2058  1.4  riastrad 		DISP_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK));
   2059  1.1  riastrad 
   2060  1.1  riastrad 	WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
   2061  1.1  riastrad }
   2062  1.1  riastrad 
   2063  1.1  riastrad static void ci_program_vc(struct radeon_device *rdev)
   2064  1.1  riastrad {
   2065  1.1  riastrad 	u32 tmp;
   2066  1.1  riastrad 
   2067  1.1  riastrad 	tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
   2068  1.1  riastrad 	tmp &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
   2069  1.1  riastrad 	WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
   2070  1.1  riastrad 
   2071  1.1  riastrad 	WREG32_SMC(CG_FTV_0, CISLANDS_VRC_DFLT0);
   2072  1.1  riastrad 	WREG32_SMC(CG_FTV_1, CISLANDS_VRC_DFLT1);
   2073  1.1  riastrad 	WREG32_SMC(CG_FTV_2, CISLANDS_VRC_DFLT2);
   2074  1.1  riastrad 	WREG32_SMC(CG_FTV_3, CISLANDS_VRC_DFLT3);
   2075  1.1  riastrad 	WREG32_SMC(CG_FTV_4, CISLANDS_VRC_DFLT4);
   2076  1.1  riastrad 	WREG32_SMC(CG_FTV_5, CISLANDS_VRC_DFLT5);
   2077  1.1  riastrad 	WREG32_SMC(CG_FTV_6, CISLANDS_VRC_DFLT6);
   2078  1.1  riastrad 	WREG32_SMC(CG_FTV_7, CISLANDS_VRC_DFLT7);
   2079  1.1  riastrad }
   2080  1.1  riastrad 
   2081  1.1  riastrad static void ci_clear_vc(struct radeon_device *rdev)
   2082  1.1  riastrad {
   2083  1.1  riastrad 	u32 tmp;
   2084  1.1  riastrad 
   2085  1.1  riastrad 	tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
   2086  1.1  riastrad 	tmp |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
   2087  1.1  riastrad 	WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
   2088  1.1  riastrad 
   2089  1.1  riastrad 	WREG32_SMC(CG_FTV_0, 0);
   2090  1.1  riastrad 	WREG32_SMC(CG_FTV_1, 0);
   2091  1.1  riastrad 	WREG32_SMC(CG_FTV_2, 0);
   2092  1.1  riastrad 	WREG32_SMC(CG_FTV_3, 0);
   2093  1.1  riastrad 	WREG32_SMC(CG_FTV_4, 0);
   2094  1.1  riastrad 	WREG32_SMC(CG_FTV_5, 0);
   2095  1.1  riastrad 	WREG32_SMC(CG_FTV_6, 0);
   2096  1.1  riastrad 	WREG32_SMC(CG_FTV_7, 0);
   2097  1.1  riastrad }
   2098  1.1  riastrad 
   2099  1.1  riastrad static int ci_upload_firmware(struct radeon_device *rdev)
   2100  1.1  riastrad {
   2101  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2102  1.1  riastrad 	int i, ret;
   2103  1.1  riastrad 
   2104  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   2105  1.1  riastrad 		if (RREG32_SMC(RCU_UC_EVENTS) & BOOT_SEQ_DONE)
   2106  1.1  riastrad 			break;
   2107  1.1  riastrad 	}
   2108  1.1  riastrad 	WREG32_SMC(SMC_SYSCON_MISC_CNTL, 1);
   2109  1.1  riastrad 
   2110  1.1  riastrad 	ci_stop_smc_clock(rdev);
   2111  1.1  riastrad 	ci_reset_smc(rdev);
   2112  1.1  riastrad 
   2113  1.1  riastrad 	ret = ci_load_smc_ucode(rdev, pi->sram_end);
   2114  1.1  riastrad 
   2115  1.1  riastrad 	return ret;
   2116  1.1  riastrad 
   2117  1.1  riastrad }
   2118  1.1  riastrad 
   2119  1.1  riastrad static int ci_get_svi2_voltage_table(struct radeon_device *rdev,
   2120  1.1  riastrad 				     struct radeon_clock_voltage_dependency_table *voltage_dependency_table,
   2121  1.1  riastrad 				     struct atom_voltage_table *voltage_table)
   2122  1.1  riastrad {
   2123  1.1  riastrad 	u32 i;
   2124  1.1  riastrad 
   2125  1.1  riastrad 	if (voltage_dependency_table == NULL)
   2126  1.1  riastrad 		return -EINVAL;
   2127  1.1  riastrad 
   2128  1.1  riastrad 	voltage_table->mask_low = 0;
   2129  1.1  riastrad 	voltage_table->phase_delay = 0;
   2130  1.1  riastrad 
   2131  1.1  riastrad 	voltage_table->count = voltage_dependency_table->count;
   2132  1.1  riastrad 	for (i = 0; i < voltage_table->count; i++) {
   2133  1.1  riastrad 		voltage_table->entries[i].value = voltage_dependency_table->entries[i].v;
   2134  1.1  riastrad 		voltage_table->entries[i].smio_low = 0;
   2135  1.1  riastrad 	}
   2136  1.1  riastrad 
   2137  1.1  riastrad 	return 0;
   2138  1.1  riastrad }
   2139  1.1  riastrad 
   2140  1.1  riastrad static int ci_construct_voltage_tables(struct radeon_device *rdev)
   2141  1.1  riastrad {
   2142  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2143  1.1  riastrad 	int ret;
   2144  1.1  riastrad 
   2145  1.1  riastrad 	if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
   2146  1.1  riastrad 		ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDC,
   2147  1.1  riastrad 						    VOLTAGE_OBJ_GPIO_LUT,
   2148  1.1  riastrad 						    &pi->vddc_voltage_table);
   2149  1.1  riastrad 		if (ret)
   2150  1.1  riastrad 			return ret;
   2151  1.1  riastrad 	} else if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
   2152  1.1  riastrad 		ret = ci_get_svi2_voltage_table(rdev,
   2153  1.1  riastrad 						&rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
   2154  1.1  riastrad 						&pi->vddc_voltage_table);
   2155  1.1  riastrad 		if (ret)
   2156  1.1  riastrad 			return ret;
   2157  1.1  riastrad 	}
   2158  1.1  riastrad 
   2159  1.1  riastrad 	if (pi->vddc_voltage_table.count > SMU7_MAX_LEVELS_VDDC)
   2160  1.1  riastrad 		si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDC,
   2161  1.1  riastrad 							 &pi->vddc_voltage_table);
   2162  1.1  riastrad 
   2163  1.1  riastrad 	if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
   2164  1.1  riastrad 		ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDCI,
   2165  1.1  riastrad 						    VOLTAGE_OBJ_GPIO_LUT,
   2166  1.1  riastrad 						    &pi->vddci_voltage_table);
   2167  1.1  riastrad 		if (ret)
   2168  1.1  riastrad 			return ret;
   2169  1.1  riastrad 	} else if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
   2170  1.1  riastrad 		ret = ci_get_svi2_voltage_table(rdev,
   2171  1.1  riastrad 						&rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
   2172  1.1  riastrad 						&pi->vddci_voltage_table);
   2173  1.1  riastrad 		if (ret)
   2174  1.1  riastrad 			return ret;
   2175  1.1  riastrad 	}
   2176  1.1  riastrad 
   2177  1.1  riastrad 	if (pi->vddci_voltage_table.count > SMU7_MAX_LEVELS_VDDCI)
   2178  1.1  riastrad 		si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDCI,
   2179  1.1  riastrad 							 &pi->vddci_voltage_table);
   2180  1.1  riastrad 
   2181  1.1  riastrad 	if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
   2182  1.1  riastrad 		ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_MVDDC,
   2183  1.1  riastrad 						    VOLTAGE_OBJ_GPIO_LUT,
   2184  1.1  riastrad 						    &pi->mvdd_voltage_table);
   2185  1.1  riastrad 		if (ret)
   2186  1.1  riastrad 			return ret;
   2187  1.1  riastrad 	} else if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
   2188  1.1  riastrad 		ret = ci_get_svi2_voltage_table(rdev,
   2189  1.1  riastrad 						&rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
   2190  1.1  riastrad 						&pi->mvdd_voltage_table);
   2191  1.1  riastrad 		if (ret)
   2192  1.1  riastrad 			return ret;
   2193  1.1  riastrad 	}
   2194  1.1  riastrad 
   2195  1.1  riastrad 	if (pi->mvdd_voltage_table.count > SMU7_MAX_LEVELS_MVDD)
   2196  1.1  riastrad 		si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_MVDD,
   2197  1.1  riastrad 							 &pi->mvdd_voltage_table);
   2198  1.1  riastrad 
   2199  1.1  riastrad 	return 0;
   2200  1.1  riastrad }
   2201  1.1  riastrad 
   2202  1.1  riastrad static void ci_populate_smc_voltage_table(struct radeon_device *rdev,
   2203  1.1  riastrad 					  struct atom_voltage_table_entry *voltage_table,
   2204  1.1  riastrad 					  SMU7_Discrete_VoltageLevel *smc_voltage_table)
   2205  1.1  riastrad {
   2206  1.1  riastrad 	int ret;
   2207  1.1  riastrad 
   2208  1.1  riastrad 	ret = ci_get_std_voltage_value_sidd(rdev, voltage_table,
   2209  1.1  riastrad 					    &smc_voltage_table->StdVoltageHiSidd,
   2210  1.1  riastrad 					    &smc_voltage_table->StdVoltageLoSidd);
   2211  1.1  riastrad 
   2212  1.1  riastrad 	if (ret) {
   2213  1.1  riastrad 		smc_voltage_table->StdVoltageHiSidd = voltage_table->value * VOLTAGE_SCALE;
   2214  1.1  riastrad 		smc_voltage_table->StdVoltageLoSidd = voltage_table->value * VOLTAGE_SCALE;
   2215  1.1  riastrad 	}
   2216  1.1  riastrad 
   2217  1.1  riastrad 	smc_voltage_table->Voltage = cpu_to_be16(voltage_table->value * VOLTAGE_SCALE);
   2218  1.1  riastrad 	smc_voltage_table->StdVoltageHiSidd =
   2219  1.1  riastrad 		cpu_to_be16(smc_voltage_table->StdVoltageHiSidd);
   2220  1.1  riastrad 	smc_voltage_table->StdVoltageLoSidd =
   2221  1.1  riastrad 		cpu_to_be16(smc_voltage_table->StdVoltageLoSidd);
   2222  1.1  riastrad }
   2223  1.1  riastrad 
   2224  1.1  riastrad static int ci_populate_smc_vddc_table(struct radeon_device *rdev,
   2225  1.1  riastrad 				      SMU7_Discrete_DpmTable *table)
   2226  1.1  riastrad {
   2227  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2228  1.1  riastrad 	unsigned int count;
   2229  1.1  riastrad 
   2230  1.1  riastrad 	table->VddcLevelCount = pi->vddc_voltage_table.count;
   2231  1.1  riastrad 	for (count = 0; count < table->VddcLevelCount; count++) {
   2232  1.1  riastrad 		ci_populate_smc_voltage_table(rdev,
   2233  1.1  riastrad 					      &pi->vddc_voltage_table.entries[count],
   2234  1.1  riastrad 					      &table->VddcLevel[count]);
   2235  1.1  riastrad 
   2236  1.1  riastrad 		if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
   2237  1.1  riastrad 			table->VddcLevel[count].Smio |=
   2238  1.1  riastrad 				pi->vddc_voltage_table.entries[count].smio_low;
   2239  1.1  riastrad 		else
   2240  1.1  riastrad 			table->VddcLevel[count].Smio = 0;
   2241  1.1  riastrad 	}
   2242  1.1  riastrad 	table->VddcLevelCount = cpu_to_be32(table->VddcLevelCount);
   2243  1.1  riastrad 
   2244  1.1  riastrad 	return 0;
   2245  1.1  riastrad }
   2246  1.1  riastrad 
   2247  1.1  riastrad static int ci_populate_smc_vddci_table(struct radeon_device *rdev,
   2248  1.1  riastrad 				       SMU7_Discrete_DpmTable *table)
   2249  1.1  riastrad {
   2250  1.1  riastrad 	unsigned int count;
   2251  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2252  1.1  riastrad 
   2253  1.1  riastrad 	table->VddciLevelCount = pi->vddci_voltage_table.count;
   2254  1.1  riastrad 	for (count = 0; count < table->VddciLevelCount; count++) {
   2255  1.1  riastrad 		ci_populate_smc_voltage_table(rdev,
   2256  1.1  riastrad 					      &pi->vddci_voltage_table.entries[count],
   2257  1.1  riastrad 					      &table->VddciLevel[count]);
   2258  1.1  riastrad 
   2259  1.1  riastrad 		if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
   2260  1.1  riastrad 			table->VddciLevel[count].Smio |=
   2261  1.1  riastrad 				pi->vddci_voltage_table.entries[count].smio_low;
   2262  1.1  riastrad 		else
   2263  1.1  riastrad 			table->VddciLevel[count].Smio = 0;
   2264  1.1  riastrad 	}
   2265  1.1  riastrad 	table->VddciLevelCount = cpu_to_be32(table->VddciLevelCount);
   2266  1.1  riastrad 
   2267  1.1  riastrad 	return 0;
   2268  1.1  riastrad }
   2269  1.1  riastrad 
   2270  1.1  riastrad static int ci_populate_smc_mvdd_table(struct radeon_device *rdev,
   2271  1.1  riastrad 				      SMU7_Discrete_DpmTable *table)
   2272  1.1  riastrad {
   2273  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2274  1.1  riastrad 	unsigned int count;
   2275  1.1  riastrad 
   2276  1.1  riastrad 	table->MvddLevelCount = pi->mvdd_voltage_table.count;
   2277  1.1  riastrad 	for (count = 0; count < table->MvddLevelCount; count++) {
   2278  1.1  riastrad 		ci_populate_smc_voltage_table(rdev,
   2279  1.1  riastrad 					      &pi->mvdd_voltage_table.entries[count],
   2280  1.1  riastrad 					      &table->MvddLevel[count]);
   2281  1.1  riastrad 
   2282  1.1  riastrad 		if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
   2283  1.1  riastrad 			table->MvddLevel[count].Smio |=
   2284  1.1  riastrad 				pi->mvdd_voltage_table.entries[count].smio_low;
   2285  1.1  riastrad 		else
   2286  1.1  riastrad 			table->MvddLevel[count].Smio = 0;
   2287  1.1  riastrad 	}
   2288  1.1  riastrad 	table->MvddLevelCount = cpu_to_be32(table->MvddLevelCount);
   2289  1.1  riastrad 
   2290  1.1  riastrad 	return 0;
   2291  1.1  riastrad }
   2292  1.1  riastrad 
   2293  1.1  riastrad static int ci_populate_smc_voltage_tables(struct radeon_device *rdev,
   2294  1.1  riastrad 					  SMU7_Discrete_DpmTable *table)
   2295  1.1  riastrad {
   2296  1.1  riastrad 	int ret;
   2297  1.1  riastrad 
   2298  1.1  riastrad 	ret = ci_populate_smc_vddc_table(rdev, table);
   2299  1.1  riastrad 	if (ret)
   2300  1.1  riastrad 		return ret;
   2301  1.1  riastrad 
   2302  1.1  riastrad 	ret = ci_populate_smc_vddci_table(rdev, table);
   2303  1.1  riastrad 	if (ret)
   2304  1.1  riastrad 		return ret;
   2305  1.1  riastrad 
   2306  1.1  riastrad 	ret = ci_populate_smc_mvdd_table(rdev, table);
   2307  1.1  riastrad 	if (ret)
   2308  1.1  riastrad 		return ret;
   2309  1.1  riastrad 
   2310  1.1  riastrad 	return 0;
   2311  1.1  riastrad }
   2312  1.1  riastrad 
   2313  1.1  riastrad static int ci_populate_mvdd_value(struct radeon_device *rdev, u32 mclk,
   2314  1.1  riastrad 				  SMU7_Discrete_VoltageLevel *voltage)
   2315  1.1  riastrad {
   2316  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2317  1.1  riastrad 	u32 i = 0;
   2318  1.1  riastrad 
   2319  1.1  riastrad 	if (pi->mvdd_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
   2320  1.1  riastrad 		for (i = 0; i < rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count; i++) {
   2321  1.1  riastrad 			if (mclk <= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries[i].clk) {
   2322  1.1  riastrad 				voltage->Voltage = pi->mvdd_voltage_table.entries[i].value;
   2323  1.1  riastrad 				break;
   2324  1.1  riastrad 			}
   2325  1.1  riastrad 		}
   2326  1.1  riastrad 
   2327  1.1  riastrad 		if (i >= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count)
   2328  1.1  riastrad 			return -EINVAL;
   2329  1.1  riastrad 	}
   2330  1.1  riastrad 
   2331  1.1  riastrad 	return -EINVAL;
   2332  1.1  riastrad }
   2333  1.1  riastrad 
   2334  1.1  riastrad static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
   2335  1.1  riastrad 					 struct atom_voltage_table_entry *voltage_table,
   2336  1.1  riastrad 					 u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd)
   2337  1.1  riastrad {
   2338  1.1  riastrad 	u16 v_index, idx;
   2339  1.1  riastrad 	bool voltage_found = false;
   2340  1.1  riastrad 	*std_voltage_hi_sidd = voltage_table->value * VOLTAGE_SCALE;
   2341  1.1  riastrad 	*std_voltage_lo_sidd = voltage_table->value * VOLTAGE_SCALE;
   2342  1.1  riastrad 
   2343  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries == NULL)
   2344  1.1  riastrad 		return -EINVAL;
   2345  1.1  riastrad 
   2346  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries) {
   2347  1.1  riastrad 		for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
   2348  1.1  riastrad 			if (voltage_table->value ==
   2349  1.1  riastrad 			    rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
   2350  1.1  riastrad 				voltage_found = true;
   2351  1.1  riastrad 				if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
   2352  1.1  riastrad 					idx = v_index;
   2353  1.1  riastrad 				else
   2354  1.1  riastrad 					idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
   2355  1.1  riastrad 				*std_voltage_lo_sidd =
   2356  1.1  riastrad 					rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
   2357  1.1  riastrad 				*std_voltage_hi_sidd =
   2358  1.1  riastrad 					rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
   2359  1.1  riastrad 				break;
   2360  1.1  riastrad 			}
   2361  1.1  riastrad 		}
   2362  1.1  riastrad 
   2363  1.1  riastrad 		if (!voltage_found) {
   2364  1.1  riastrad 			for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
   2365  1.1  riastrad 				if (voltage_table->value <=
   2366  1.1  riastrad 				    rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
   2367  1.1  riastrad 					voltage_found = true;
   2368  1.1  riastrad 					if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
   2369  1.1  riastrad 						idx = v_index;
   2370  1.1  riastrad 					else
   2371  1.1  riastrad 						idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
   2372  1.1  riastrad 					*std_voltage_lo_sidd =
   2373  1.1  riastrad 						rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
   2374  1.1  riastrad 					*std_voltage_hi_sidd =
   2375  1.1  riastrad 						rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
   2376  1.1  riastrad 					break;
   2377  1.1  riastrad 				}
   2378  1.1  riastrad 			}
   2379  1.1  riastrad 		}
   2380  1.1  riastrad 	}
   2381  1.1  riastrad 
   2382  1.1  riastrad 	return 0;
   2383  1.1  riastrad }
   2384  1.1  riastrad 
   2385  1.1  riastrad static void ci_populate_phase_value_based_on_sclk(struct radeon_device *rdev,
   2386  1.1  riastrad 						  const struct radeon_phase_shedding_limits_table *limits,
   2387  1.1  riastrad 						  u32 sclk,
   2388  1.1  riastrad 						  u32 *phase_shedding)
   2389  1.1  riastrad {
   2390  1.1  riastrad 	unsigned int i;
   2391  1.1  riastrad 
   2392  1.1  riastrad 	*phase_shedding = 1;
   2393  1.1  riastrad 
   2394  1.1  riastrad 	for (i = 0; i < limits->count; i++) {
   2395  1.1  riastrad 		if (sclk < limits->entries[i].sclk) {
   2396  1.1  riastrad 			*phase_shedding = i;
   2397  1.1  riastrad 			break;
   2398  1.1  riastrad 		}
   2399  1.1  riastrad 	}
   2400  1.1  riastrad }
   2401  1.1  riastrad 
   2402  1.1  riastrad static void ci_populate_phase_value_based_on_mclk(struct radeon_device *rdev,
   2403  1.1  riastrad 						  const struct radeon_phase_shedding_limits_table *limits,
   2404  1.1  riastrad 						  u32 mclk,
   2405  1.1  riastrad 						  u32 *phase_shedding)
   2406  1.1  riastrad {
   2407  1.1  riastrad 	unsigned int i;
   2408  1.1  riastrad 
   2409  1.1  riastrad 	*phase_shedding = 1;
   2410  1.1  riastrad 
   2411  1.1  riastrad 	for (i = 0; i < limits->count; i++) {
   2412  1.1  riastrad 		if (mclk < limits->entries[i].mclk) {
   2413  1.1  riastrad 			*phase_shedding = i;
   2414  1.1  riastrad 			break;
   2415  1.1  riastrad 		}
   2416  1.1  riastrad 	}
   2417  1.1  riastrad }
   2418  1.1  riastrad 
   2419  1.1  riastrad static int ci_init_arb_table_index(struct radeon_device *rdev)
   2420  1.1  riastrad {
   2421  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2422  1.1  riastrad 	u32 tmp;
   2423  1.1  riastrad 	int ret;
   2424  1.1  riastrad 
   2425  1.1  riastrad 	ret = ci_read_smc_sram_dword(rdev, pi->arb_table_start,
   2426  1.1  riastrad 				     &tmp, pi->sram_end);
   2427  1.1  riastrad 	if (ret)
   2428  1.1  riastrad 		return ret;
   2429  1.1  riastrad 
   2430  1.1  riastrad 	tmp &= 0x00FFFFFF;
   2431  1.1  riastrad 	tmp |= MC_CG_ARB_FREQ_F1 << 24;
   2432  1.1  riastrad 
   2433  1.1  riastrad 	return ci_write_smc_sram_dword(rdev, pi->arb_table_start,
   2434  1.1  riastrad 				       tmp, pi->sram_end);
   2435  1.1  riastrad }
   2436  1.1  riastrad 
   2437  1.1  riastrad static int ci_get_dependency_volt_by_clk(struct radeon_device *rdev,
   2438  1.1  riastrad 					 struct radeon_clock_voltage_dependency_table *allowed_clock_voltage_table,
   2439  1.1  riastrad 					 u32 clock, u32 *voltage)
   2440  1.1  riastrad {
   2441  1.1  riastrad 	u32 i = 0;
   2442  1.1  riastrad 
   2443  1.1  riastrad 	if (allowed_clock_voltage_table->count == 0)
   2444  1.1  riastrad 		return -EINVAL;
   2445  1.1  riastrad 
   2446  1.1  riastrad 	for (i = 0; i < allowed_clock_voltage_table->count; i++) {
   2447  1.1  riastrad 		if (allowed_clock_voltage_table->entries[i].clk >= clock) {
   2448  1.1  riastrad 			*voltage = allowed_clock_voltage_table->entries[i].v;
   2449  1.1  riastrad 			return 0;
   2450  1.1  riastrad 		}
   2451  1.1  riastrad 	}
   2452  1.1  riastrad 
   2453  1.1  riastrad 	*voltage = allowed_clock_voltage_table->entries[i-1].v;
   2454  1.1  riastrad 
   2455  1.1  riastrad 	return 0;
   2456  1.1  riastrad }
   2457  1.1  riastrad 
   2458  1.1  riastrad static u8 ci_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
   2459  1.1  riastrad 					     u32 sclk, u32 min_sclk_in_sr)
   2460  1.1  riastrad {
   2461  1.1  riastrad 	u32 i;
   2462  1.1  riastrad 	u32 tmp;
   2463  1.3  riastrad 	u32 min = (min_sclk_in_sr > CISLAND_MINIMUM_ENGINE_CLOCK) ?
   2464  1.1  riastrad 		min_sclk_in_sr : CISLAND_MINIMUM_ENGINE_CLOCK;
   2465  1.1  riastrad 
   2466  1.3  riastrad 	if (sclk < min)
   2467  1.1  riastrad 		return 0;
   2468  1.1  riastrad 
   2469  1.1  riastrad 	for (i = CISLAND_MAX_DEEPSLEEP_DIVIDER_ID;  ; i--) {
   2470  1.1  riastrad 		tmp = sclk / (1 << i);
   2471  1.3  riastrad 		if (tmp >= min || i == 0)
   2472  1.1  riastrad 			break;
   2473  1.1  riastrad 	}
   2474  1.1  riastrad 
   2475  1.1  riastrad 	return (u8)i;
   2476  1.1  riastrad }
   2477  1.1  riastrad 
   2478  1.1  riastrad static int ci_initial_switch_from_arb_f0_to_f1(struct radeon_device *rdev)
   2479  1.1  riastrad {
   2480  1.1  riastrad 	return ni_copy_and_switch_arb_sets(rdev, MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
   2481  1.1  riastrad }
   2482  1.1  riastrad 
   2483  1.1  riastrad static int ci_reset_to_default(struct radeon_device *rdev)
   2484  1.1  riastrad {
   2485  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, PPSMC_MSG_ResetToDefaults) == PPSMC_Result_OK) ?
   2486  1.1  riastrad 		0 : -EINVAL;
   2487  1.1  riastrad }
   2488  1.1  riastrad 
   2489  1.1  riastrad static int ci_force_switch_to_arb_f0(struct radeon_device *rdev)
   2490  1.1  riastrad {
   2491  1.1  riastrad 	u32 tmp;
   2492  1.1  riastrad 
   2493  1.1  riastrad 	tmp = (RREG32_SMC(SMC_SCRATCH9) & 0x0000ff00) >> 8;
   2494  1.1  riastrad 
   2495  1.1  riastrad 	if (tmp == MC_CG_ARB_FREQ_F0)
   2496  1.1  riastrad 		return 0;
   2497  1.1  riastrad 
   2498  1.1  riastrad 	return ni_copy_and_switch_arb_sets(rdev, tmp, MC_CG_ARB_FREQ_F0);
   2499  1.1  riastrad }
   2500  1.1  riastrad 
   2501  1.1  riastrad static void ci_register_patching_mc_arb(struct radeon_device *rdev,
   2502  1.1  riastrad 					const u32 engine_clock,
   2503  1.1  riastrad 					const u32 memory_clock,
   2504  1.1  riastrad 					u32 *dram_timimg2)
   2505  1.1  riastrad {
   2506  1.1  riastrad 	bool patch;
   2507  1.1  riastrad 	u32 tmp, tmp2;
   2508  1.1  riastrad 
   2509  1.1  riastrad 	tmp = RREG32(MC_SEQ_MISC0);
   2510  1.1  riastrad 	patch = ((tmp & 0x0000f00) == 0x300) ? true : false;
   2511  1.1  riastrad 
   2512  1.1  riastrad 	if (patch &&
   2513  1.1  riastrad 	    ((rdev->pdev->device == 0x67B0) ||
   2514  1.1  riastrad 	     (rdev->pdev->device == 0x67B1))) {
   2515  1.1  riastrad 		if ((memory_clock > 100000) && (memory_clock <= 125000)) {
   2516  1.1  riastrad 			tmp2 = (((0x31 * engine_clock) / 125000) - 1) & 0xff;
   2517  1.1  riastrad 			*dram_timimg2 &= ~0x00ff0000;
   2518  1.1  riastrad 			*dram_timimg2 |= tmp2 << 16;
   2519  1.1  riastrad 		} else if ((memory_clock > 125000) && (memory_clock <= 137500)) {
   2520  1.1  riastrad 			tmp2 = (((0x36 * engine_clock) / 137500) - 1) & 0xff;
   2521  1.1  riastrad 			*dram_timimg2 &= ~0x00ff0000;
   2522  1.1  riastrad 			*dram_timimg2 |= tmp2 << 16;
   2523  1.1  riastrad 		}
   2524  1.1  riastrad 	}
   2525  1.1  riastrad }
   2526  1.1  riastrad 
   2527  1.1  riastrad 
   2528  1.1  riastrad static int ci_populate_memory_timing_parameters(struct radeon_device *rdev,
   2529  1.1  riastrad 						u32 sclk,
   2530  1.1  riastrad 						u32 mclk,
   2531  1.1  riastrad 						SMU7_Discrete_MCArbDramTimingTableEntry *arb_regs)
   2532  1.1  riastrad {
   2533  1.1  riastrad 	u32 dram_timing;
   2534  1.1  riastrad 	u32 dram_timing2;
   2535  1.1  riastrad 	u32 burst_time;
   2536  1.1  riastrad 
   2537  1.1  riastrad 	radeon_atom_set_engine_dram_timings(rdev, sclk, mclk);
   2538  1.1  riastrad 
   2539  1.1  riastrad 	dram_timing  = RREG32(MC_ARB_DRAM_TIMING);
   2540  1.1  riastrad 	dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
   2541  1.1  riastrad 	burst_time = RREG32(MC_ARB_BURST_TIME) & STATE0_MASK;
   2542  1.1  riastrad 
   2543  1.1  riastrad 	ci_register_patching_mc_arb(rdev, sclk, mclk, &dram_timing2);
   2544  1.1  riastrad 
   2545  1.1  riastrad 	arb_regs->McArbDramTiming  = cpu_to_be32(dram_timing);
   2546  1.1  riastrad 	arb_regs->McArbDramTiming2 = cpu_to_be32(dram_timing2);
   2547  1.1  riastrad 	arb_regs->McArbBurstTime = (u8)burst_time;
   2548  1.1  riastrad 
   2549  1.1  riastrad 	return 0;
   2550  1.1  riastrad }
   2551  1.1  riastrad 
   2552  1.1  riastrad static int ci_do_program_memory_timing_parameters(struct radeon_device *rdev)
   2553  1.1  riastrad {
   2554  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2555  1.1  riastrad 	SMU7_Discrete_MCArbDramTimingTable arb_regs;
   2556  1.1  riastrad 	u32 i, j;
   2557  1.1  riastrad 	int ret =  0;
   2558  1.1  riastrad 
   2559  1.1  riastrad 	memset(&arb_regs, 0, sizeof(SMU7_Discrete_MCArbDramTimingTable));
   2560  1.1  riastrad 
   2561  1.1  riastrad 	for (i = 0; i < pi->dpm_table.sclk_table.count; i++) {
   2562  1.1  riastrad 		for (j = 0; j < pi->dpm_table.mclk_table.count; j++) {
   2563  1.1  riastrad 			ret = ci_populate_memory_timing_parameters(rdev,
   2564  1.1  riastrad 								   pi->dpm_table.sclk_table.dpm_levels[i].value,
   2565  1.1  riastrad 								   pi->dpm_table.mclk_table.dpm_levels[j].value,
   2566  1.1  riastrad 								   &arb_regs.entries[i][j]);
   2567  1.1  riastrad 			if (ret)
   2568  1.1  riastrad 				break;
   2569  1.1  riastrad 		}
   2570  1.1  riastrad 	}
   2571  1.1  riastrad 
   2572  1.1  riastrad 	if (ret == 0)
   2573  1.1  riastrad 		ret = ci_copy_bytes_to_smc(rdev,
   2574  1.1  riastrad 					   pi->arb_table_start,
   2575  1.1  riastrad 					   (u8 *)&arb_regs,
   2576  1.1  riastrad 					   sizeof(SMU7_Discrete_MCArbDramTimingTable),
   2577  1.1  riastrad 					   pi->sram_end);
   2578  1.1  riastrad 
   2579  1.1  riastrad 	return ret;
   2580  1.1  riastrad }
   2581  1.1  riastrad 
   2582  1.1  riastrad static int ci_program_memory_timing_parameters(struct radeon_device *rdev)
   2583  1.1  riastrad {
   2584  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2585  1.1  riastrad 
   2586  1.1  riastrad 	if (pi->need_update_smu7_dpm_table == 0)
   2587  1.1  riastrad 		return 0;
   2588  1.1  riastrad 
   2589  1.1  riastrad 	return ci_do_program_memory_timing_parameters(rdev);
   2590  1.1  riastrad }
   2591  1.1  riastrad 
   2592  1.1  riastrad static void ci_populate_smc_initial_state(struct radeon_device *rdev,
   2593  1.1  riastrad 					  struct radeon_ps *radeon_boot_state)
   2594  1.1  riastrad {
   2595  1.1  riastrad 	struct ci_ps *boot_state = ci_get_ps(radeon_boot_state);
   2596  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2597  1.1  riastrad 	u32 level = 0;
   2598  1.1  riastrad 
   2599  1.1  riastrad 	for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; level++) {
   2600  1.1  riastrad 		if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[level].clk >=
   2601  1.1  riastrad 		    boot_state->performance_levels[0].sclk) {
   2602  1.1  riastrad 			pi->smc_state_table.GraphicsBootLevel = level;
   2603  1.1  riastrad 			break;
   2604  1.1  riastrad 		}
   2605  1.1  riastrad 	}
   2606  1.1  riastrad 
   2607  1.1  riastrad 	for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.count; level++) {
   2608  1.1  riastrad 		if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries[level].clk >=
   2609  1.1  riastrad 		    boot_state->performance_levels[0].mclk) {
   2610  1.1  riastrad 			pi->smc_state_table.MemoryBootLevel = level;
   2611  1.1  riastrad 			break;
   2612  1.1  riastrad 		}
   2613  1.1  riastrad 	}
   2614  1.1  riastrad }
   2615  1.1  riastrad 
   2616  1.1  riastrad static u32 ci_get_dpm_level_enable_mask_value(struct ci_single_dpm_table *dpm_table)
   2617  1.1  riastrad {
   2618  1.1  riastrad 	u32 i;
   2619  1.1  riastrad 	u32 mask_value = 0;
   2620  1.1  riastrad 
   2621  1.1  riastrad 	for (i = dpm_table->count; i > 0; i--) {
   2622  1.1  riastrad 		mask_value = mask_value << 1;
   2623  1.1  riastrad 		if (dpm_table->dpm_levels[i-1].enabled)
   2624  1.1  riastrad 			mask_value |= 0x1;
   2625  1.1  riastrad 		else
   2626  1.1  riastrad 			mask_value &= 0xFFFFFFFE;
   2627  1.1  riastrad 	}
   2628  1.1  riastrad 
   2629  1.1  riastrad 	return mask_value;
   2630  1.1  riastrad }
   2631  1.1  riastrad 
   2632  1.1  riastrad static void ci_populate_smc_link_level(struct radeon_device *rdev,
   2633  1.1  riastrad 				       SMU7_Discrete_DpmTable *table)
   2634  1.1  riastrad {
   2635  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2636  1.1  riastrad 	struct ci_dpm_table *dpm_table = &pi->dpm_table;
   2637  1.1  riastrad 	u32 i;
   2638  1.1  riastrad 
   2639  1.1  riastrad 	for (i = 0; i < dpm_table->pcie_speed_table.count; i++) {
   2640  1.1  riastrad 		table->LinkLevel[i].PcieGenSpeed =
   2641  1.1  riastrad 			(u8)dpm_table->pcie_speed_table.dpm_levels[i].value;
   2642  1.1  riastrad 		table->LinkLevel[i].PcieLaneCount =
   2643  1.1  riastrad 			r600_encode_pci_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
   2644  1.1  riastrad 		table->LinkLevel[i].EnabledForActivity = 1;
   2645  1.1  riastrad 		table->LinkLevel[i].DownT = cpu_to_be32(5);
   2646  1.1  riastrad 		table->LinkLevel[i].UpT = cpu_to_be32(30);
   2647  1.1  riastrad 	}
   2648  1.1  riastrad 
   2649  1.1  riastrad 	pi->smc_state_table.LinkLevelCount = (u8)dpm_table->pcie_speed_table.count;
   2650  1.1  riastrad 	pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
   2651  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
   2652  1.1  riastrad }
   2653  1.1  riastrad 
   2654  1.1  riastrad static int ci_populate_smc_uvd_level(struct radeon_device *rdev,
   2655  1.1  riastrad 				     SMU7_Discrete_DpmTable *table)
   2656  1.1  riastrad {
   2657  1.1  riastrad 	u32 count;
   2658  1.1  riastrad 	struct atom_clock_dividers dividers;
   2659  1.1  riastrad 	int ret = -EINVAL;
   2660  1.1  riastrad 
   2661  1.1  riastrad 	table->UvdLevelCount =
   2662  1.1  riastrad 		rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count;
   2663  1.1  riastrad 
   2664  1.1  riastrad 	for (count = 0; count < table->UvdLevelCount; count++) {
   2665  1.1  riastrad 		table->UvdLevel[count].VclkFrequency =
   2666  1.1  riastrad 			rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].vclk;
   2667  1.1  riastrad 		table->UvdLevel[count].DclkFrequency =
   2668  1.1  riastrad 			rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].dclk;
   2669  1.1  riastrad 		table->UvdLevel[count].MinVddc =
   2670  1.1  riastrad 			rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
   2671  1.1  riastrad 		table->UvdLevel[count].MinVddcPhases = 1;
   2672  1.1  riastrad 
   2673  1.1  riastrad 		ret = radeon_atom_get_clock_dividers(rdev,
   2674  1.1  riastrad 						     COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
   2675  1.1  riastrad 						     table->UvdLevel[count].VclkFrequency, false, &dividers);
   2676  1.1  riastrad 		if (ret)
   2677  1.1  riastrad 			return ret;
   2678  1.1  riastrad 
   2679  1.1  riastrad 		table->UvdLevel[count].VclkDivider = (u8)dividers.post_divider;
   2680  1.1  riastrad 
   2681  1.1  riastrad 		ret = radeon_atom_get_clock_dividers(rdev,
   2682  1.1  riastrad 						     COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
   2683  1.1  riastrad 						     table->UvdLevel[count].DclkFrequency, false, &dividers);
   2684  1.1  riastrad 		if (ret)
   2685  1.1  riastrad 			return ret;
   2686  1.1  riastrad 
   2687  1.1  riastrad 		table->UvdLevel[count].DclkDivider = (u8)dividers.post_divider;
   2688  1.1  riastrad 
   2689  1.1  riastrad 		table->UvdLevel[count].VclkFrequency = cpu_to_be32(table->UvdLevel[count].VclkFrequency);
   2690  1.1  riastrad 		table->UvdLevel[count].DclkFrequency = cpu_to_be32(table->UvdLevel[count].DclkFrequency);
   2691  1.1  riastrad 		table->UvdLevel[count].MinVddc = cpu_to_be16(table->UvdLevel[count].MinVddc);
   2692  1.1  riastrad 	}
   2693  1.1  riastrad 
   2694  1.1  riastrad 	return ret;
   2695  1.1  riastrad }
   2696  1.1  riastrad 
   2697  1.1  riastrad static int ci_populate_smc_vce_level(struct radeon_device *rdev,
   2698  1.1  riastrad 				     SMU7_Discrete_DpmTable *table)
   2699  1.1  riastrad {
   2700  1.1  riastrad 	u32 count;
   2701  1.1  riastrad 	struct atom_clock_dividers dividers;
   2702  1.1  riastrad 	int ret = -EINVAL;
   2703  1.1  riastrad 
   2704  1.1  riastrad 	table->VceLevelCount =
   2705  1.1  riastrad 		rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count;
   2706  1.1  riastrad 
   2707  1.1  riastrad 	for (count = 0; count < table->VceLevelCount; count++) {
   2708  1.1  riastrad 		table->VceLevel[count].Frequency =
   2709  1.1  riastrad 			rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].evclk;
   2710  1.1  riastrad 		table->VceLevel[count].MinVoltage =
   2711  1.1  riastrad 			(u16)rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
   2712  1.1  riastrad 		table->VceLevel[count].MinPhases = 1;
   2713  1.1  riastrad 
   2714  1.1  riastrad 		ret = radeon_atom_get_clock_dividers(rdev,
   2715  1.1  riastrad 						     COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
   2716  1.1  riastrad 						     table->VceLevel[count].Frequency, false, &dividers);
   2717  1.1  riastrad 		if (ret)
   2718  1.1  riastrad 			return ret;
   2719  1.1  riastrad 
   2720  1.1  riastrad 		table->VceLevel[count].Divider = (u8)dividers.post_divider;
   2721  1.1  riastrad 
   2722  1.1  riastrad 		table->VceLevel[count].Frequency = cpu_to_be32(table->VceLevel[count].Frequency);
   2723  1.1  riastrad 		table->VceLevel[count].MinVoltage = cpu_to_be16(table->VceLevel[count].MinVoltage);
   2724  1.1  riastrad 	}
   2725  1.1  riastrad 
   2726  1.1  riastrad 	return ret;
   2727  1.1  riastrad 
   2728  1.1  riastrad }
   2729  1.1  riastrad 
   2730  1.1  riastrad static int ci_populate_smc_acp_level(struct radeon_device *rdev,
   2731  1.1  riastrad 				     SMU7_Discrete_DpmTable *table)
   2732  1.1  riastrad {
   2733  1.1  riastrad 	u32 count;
   2734  1.1  riastrad 	struct atom_clock_dividers dividers;
   2735  1.1  riastrad 	int ret = -EINVAL;
   2736  1.1  riastrad 
   2737  1.1  riastrad 	table->AcpLevelCount = (u8)
   2738  1.1  riastrad 		(rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count);
   2739  1.1  riastrad 
   2740  1.1  riastrad 	for (count = 0; count < table->AcpLevelCount; count++) {
   2741  1.1  riastrad 		table->AcpLevel[count].Frequency =
   2742  1.1  riastrad 			rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].clk;
   2743  1.1  riastrad 		table->AcpLevel[count].MinVoltage =
   2744  1.1  riastrad 			rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].v;
   2745  1.1  riastrad 		table->AcpLevel[count].MinPhases = 1;
   2746  1.1  riastrad 
   2747  1.1  riastrad 		ret = radeon_atom_get_clock_dividers(rdev,
   2748  1.1  riastrad 						     COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
   2749  1.1  riastrad 						     table->AcpLevel[count].Frequency, false, &dividers);
   2750  1.1  riastrad 		if (ret)
   2751  1.1  riastrad 			return ret;
   2752  1.1  riastrad 
   2753  1.1  riastrad 		table->AcpLevel[count].Divider = (u8)dividers.post_divider;
   2754  1.1  riastrad 
   2755  1.1  riastrad 		table->AcpLevel[count].Frequency = cpu_to_be32(table->AcpLevel[count].Frequency);
   2756  1.1  riastrad 		table->AcpLevel[count].MinVoltage = cpu_to_be16(table->AcpLevel[count].MinVoltage);
   2757  1.1  riastrad 	}
   2758  1.1  riastrad 
   2759  1.1  riastrad 	return ret;
   2760  1.1  riastrad }
   2761  1.1  riastrad 
   2762  1.1  riastrad static int ci_populate_smc_samu_level(struct radeon_device *rdev,
   2763  1.1  riastrad 				      SMU7_Discrete_DpmTable *table)
   2764  1.1  riastrad {
   2765  1.1  riastrad 	u32 count;
   2766  1.1  riastrad 	struct atom_clock_dividers dividers;
   2767  1.1  riastrad 	int ret = -EINVAL;
   2768  1.1  riastrad 
   2769  1.1  riastrad 	table->SamuLevelCount =
   2770  1.1  riastrad 		rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count;
   2771  1.1  riastrad 
   2772  1.1  riastrad 	for (count = 0; count < table->SamuLevelCount; count++) {
   2773  1.1  riastrad 		table->SamuLevel[count].Frequency =
   2774  1.1  riastrad 			rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].clk;
   2775  1.1  riastrad 		table->SamuLevel[count].MinVoltage =
   2776  1.1  riastrad 			rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
   2777  1.1  riastrad 		table->SamuLevel[count].MinPhases = 1;
   2778  1.1  riastrad 
   2779  1.1  riastrad 		ret = radeon_atom_get_clock_dividers(rdev,
   2780  1.1  riastrad 						     COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
   2781  1.1  riastrad 						     table->SamuLevel[count].Frequency, false, &dividers);
   2782  1.1  riastrad 		if (ret)
   2783  1.1  riastrad 			return ret;
   2784  1.1  riastrad 
   2785  1.1  riastrad 		table->SamuLevel[count].Divider = (u8)dividers.post_divider;
   2786  1.1  riastrad 
   2787  1.1  riastrad 		table->SamuLevel[count].Frequency = cpu_to_be32(table->SamuLevel[count].Frequency);
   2788  1.1  riastrad 		table->SamuLevel[count].MinVoltage = cpu_to_be16(table->SamuLevel[count].MinVoltage);
   2789  1.1  riastrad 	}
   2790  1.1  riastrad 
   2791  1.1  riastrad 	return ret;
   2792  1.1  riastrad }
   2793  1.1  riastrad 
   2794  1.1  riastrad static int ci_calculate_mclk_params(struct radeon_device *rdev,
   2795  1.1  riastrad 				    u32 memory_clock,
   2796  1.1  riastrad 				    SMU7_Discrete_MemoryLevel *mclk,
   2797  1.1  riastrad 				    bool strobe_mode,
   2798  1.1  riastrad 				    bool dll_state_on)
   2799  1.1  riastrad {
   2800  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2801  1.1  riastrad 	u32  dll_cntl = pi->clock_registers.dll_cntl;
   2802  1.1  riastrad 	u32  mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
   2803  1.1  riastrad 	u32  mpll_ad_func_cntl = pi->clock_registers.mpll_ad_func_cntl;
   2804  1.1  riastrad 	u32  mpll_dq_func_cntl = pi->clock_registers.mpll_dq_func_cntl;
   2805  1.1  riastrad 	u32  mpll_func_cntl = pi->clock_registers.mpll_func_cntl;
   2806  1.1  riastrad 	u32  mpll_func_cntl_1 = pi->clock_registers.mpll_func_cntl_1;
   2807  1.1  riastrad 	u32  mpll_func_cntl_2 = pi->clock_registers.mpll_func_cntl_2;
   2808  1.1  riastrad 	u32  mpll_ss1 = pi->clock_registers.mpll_ss1;
   2809  1.1  riastrad 	u32  mpll_ss2 = pi->clock_registers.mpll_ss2;
   2810  1.1  riastrad 	struct atom_mpll_param mpll_param;
   2811  1.1  riastrad 	int ret;
   2812  1.1  riastrad 
   2813  1.1  riastrad 	ret = radeon_atom_get_memory_pll_dividers(rdev, memory_clock, strobe_mode, &mpll_param);
   2814  1.1  riastrad 	if (ret)
   2815  1.1  riastrad 		return ret;
   2816  1.1  riastrad 
   2817  1.1  riastrad 	mpll_func_cntl &= ~BWCTRL_MASK;
   2818  1.1  riastrad 	mpll_func_cntl |= BWCTRL(mpll_param.bwcntl);
   2819  1.1  riastrad 
   2820  1.1  riastrad 	mpll_func_cntl_1 &= ~(CLKF_MASK | CLKFRAC_MASK | VCO_MODE_MASK);
   2821  1.1  riastrad 	mpll_func_cntl_1 |= CLKF(mpll_param.clkf) |
   2822  1.1  riastrad 		CLKFRAC(mpll_param.clkfrac) | VCO_MODE(mpll_param.vco_mode);
   2823  1.1  riastrad 
   2824  1.1  riastrad 	mpll_ad_func_cntl &= ~YCLK_POST_DIV_MASK;
   2825  1.1  riastrad 	mpll_ad_func_cntl |= YCLK_POST_DIV(mpll_param.post_div);
   2826  1.1  riastrad 
   2827  1.1  riastrad 	if (pi->mem_gddr5) {
   2828  1.1  riastrad 		mpll_dq_func_cntl &= ~(YCLK_SEL_MASK | YCLK_POST_DIV_MASK);
   2829  1.1  riastrad 		mpll_dq_func_cntl |= YCLK_SEL(mpll_param.yclk_sel) |
   2830  1.1  riastrad 			YCLK_POST_DIV(mpll_param.post_div);
   2831  1.1  riastrad 	}
   2832  1.1  riastrad 
   2833  1.1  riastrad 	if (pi->caps_mclk_ss_support) {
   2834  1.1  riastrad 		struct radeon_atom_ss ss;
   2835  1.1  riastrad 		u32 freq_nom;
   2836  1.1  riastrad 		u32 tmp;
   2837  1.1  riastrad 		u32 reference_clock = rdev->clock.mpll.reference_freq;
   2838  1.1  riastrad 
   2839  1.1  riastrad 		if (mpll_param.qdr == 1)
   2840  1.1  riastrad 			freq_nom = memory_clock * 4 * (1 << mpll_param.post_div);
   2841  1.1  riastrad 		else
   2842  1.1  riastrad 			freq_nom = memory_clock * 2 * (1 << mpll_param.post_div);
   2843  1.1  riastrad 
   2844  1.1  riastrad 		tmp = (freq_nom / reference_clock);
   2845  1.1  riastrad 		tmp = tmp * tmp;
   2846  1.1  riastrad 		if (radeon_atombios_get_asic_ss_info(rdev, &ss,
   2847  1.1  riastrad 						     ASIC_INTERNAL_MEMORY_SS, freq_nom)) {
   2848  1.1  riastrad 			u32 clks = reference_clock * 5 / ss.rate;
   2849  1.1  riastrad 			u32 clkv = (u32)((((131 * ss.percentage * ss.rate) / 100) * tmp) / freq_nom);
   2850  1.1  riastrad 
   2851  1.1  riastrad 			mpll_ss1 &= ~CLKV_MASK;
   2852  1.1  riastrad 			mpll_ss1 |= CLKV(clkv);
   2853  1.1  riastrad 
   2854  1.1  riastrad 			mpll_ss2 &= ~CLKS_MASK;
   2855  1.1  riastrad 			mpll_ss2 |= CLKS(clks);
   2856  1.1  riastrad 		}
   2857  1.1  riastrad 	}
   2858  1.1  riastrad 
   2859  1.1  riastrad 	mclk_pwrmgt_cntl &= ~DLL_SPEED_MASK;
   2860  1.1  riastrad 	mclk_pwrmgt_cntl |= DLL_SPEED(mpll_param.dll_speed);
   2861  1.1  riastrad 
   2862  1.1  riastrad 	if (dll_state_on)
   2863  1.1  riastrad 		mclk_pwrmgt_cntl |= MRDCK0_PDNB | MRDCK1_PDNB;
   2864  1.1  riastrad 	else
   2865  1.1  riastrad 		mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
   2866  1.1  riastrad 
   2867  1.1  riastrad 	mclk->MclkFrequency = memory_clock;
   2868  1.1  riastrad 	mclk->MpllFuncCntl = mpll_func_cntl;
   2869  1.1  riastrad 	mclk->MpllFuncCntl_1 = mpll_func_cntl_1;
   2870  1.1  riastrad 	mclk->MpllFuncCntl_2 = mpll_func_cntl_2;
   2871  1.1  riastrad 	mclk->MpllAdFuncCntl = mpll_ad_func_cntl;
   2872  1.1  riastrad 	mclk->MpllDqFuncCntl = mpll_dq_func_cntl;
   2873  1.1  riastrad 	mclk->MclkPwrmgtCntl = mclk_pwrmgt_cntl;
   2874  1.1  riastrad 	mclk->DllCntl = dll_cntl;
   2875  1.1  riastrad 	mclk->MpllSs1 = mpll_ss1;
   2876  1.1  riastrad 	mclk->MpllSs2 = mpll_ss2;
   2877  1.1  riastrad 
   2878  1.1  riastrad 	return 0;
   2879  1.1  riastrad }
   2880  1.1  riastrad 
   2881  1.1  riastrad static int ci_populate_single_memory_level(struct radeon_device *rdev,
   2882  1.1  riastrad 					   u32 memory_clock,
   2883  1.1  riastrad 					   SMU7_Discrete_MemoryLevel *memory_level)
   2884  1.1  riastrad {
   2885  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2886  1.1  riastrad 	int ret;
   2887  1.1  riastrad 	bool dll_state_on;
   2888  1.1  riastrad 
   2889  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries) {
   2890  1.1  riastrad 		ret = ci_get_dependency_volt_by_clk(rdev,
   2891  1.1  riastrad 						    &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
   2892  1.1  riastrad 						    memory_clock, &memory_level->MinVddc);
   2893  1.1  riastrad 		if (ret)
   2894  1.1  riastrad 			return ret;
   2895  1.1  riastrad 	}
   2896  1.1  riastrad 
   2897  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk.entries) {
   2898  1.1  riastrad 		ret = ci_get_dependency_volt_by_clk(rdev,
   2899  1.1  riastrad 						    &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
   2900  1.1  riastrad 						    memory_clock, &memory_level->MinVddci);
   2901  1.1  riastrad 		if (ret)
   2902  1.1  riastrad 			return ret;
   2903  1.1  riastrad 	}
   2904  1.1  riastrad 
   2905  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries) {
   2906  1.1  riastrad 		ret = ci_get_dependency_volt_by_clk(rdev,
   2907  1.1  riastrad 						    &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
   2908  1.1  riastrad 						    memory_clock, &memory_level->MinMvdd);
   2909  1.1  riastrad 		if (ret)
   2910  1.1  riastrad 			return ret;
   2911  1.1  riastrad 	}
   2912  1.1  riastrad 
   2913  1.1  riastrad 	memory_level->MinVddcPhases = 1;
   2914  1.1  riastrad 
   2915  1.1  riastrad 	if (pi->vddc_phase_shed_control)
   2916  1.1  riastrad 		ci_populate_phase_value_based_on_mclk(rdev,
   2917  1.1  riastrad 						      &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
   2918  1.1  riastrad 						      memory_clock,
   2919  1.1  riastrad 						      &memory_level->MinVddcPhases);
   2920  1.1  riastrad 
   2921  1.1  riastrad 	memory_level->EnabledForThrottle = 1;
   2922  1.1  riastrad 	memory_level->UpH = 0;
   2923  1.1  riastrad 	memory_level->DownH = 100;
   2924  1.1  riastrad 	memory_level->VoltageDownH = 0;
   2925  1.1  riastrad 	memory_level->ActivityLevel = (u16)pi->mclk_activity_target;
   2926  1.1  riastrad 
   2927  1.1  riastrad 	memory_level->StutterEnable = false;
   2928  1.1  riastrad 	memory_level->StrobeEnable = false;
   2929  1.1  riastrad 	memory_level->EdcReadEnable = false;
   2930  1.1  riastrad 	memory_level->EdcWriteEnable = false;
   2931  1.1  riastrad 	memory_level->RttEnable = false;
   2932  1.1  riastrad 
   2933  1.1  riastrad 	memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
   2934  1.1  riastrad 
   2935  1.1  riastrad 	if (pi->mclk_stutter_mode_threshold &&
   2936  1.1  riastrad 	    (memory_clock <= pi->mclk_stutter_mode_threshold) &&
   2937  1.1  riastrad 	    (pi->uvd_enabled == false) &&
   2938  1.1  riastrad 	    (RREG32(DPG_PIPE_STUTTER_CONTROL) & STUTTER_ENABLE) &&
   2939  1.1  riastrad 	    (rdev->pm.dpm.new_active_crtc_count <= 2))
   2940  1.1  riastrad 		memory_level->StutterEnable = true;
   2941  1.1  riastrad 
   2942  1.1  riastrad 	if (pi->mclk_strobe_mode_threshold &&
   2943  1.1  riastrad 	    (memory_clock <= pi->mclk_strobe_mode_threshold))
   2944  1.1  riastrad 		memory_level->StrobeEnable = 1;
   2945  1.1  riastrad 
   2946  1.1  riastrad 	if (pi->mem_gddr5) {
   2947  1.1  riastrad 		memory_level->StrobeRatio =
   2948  1.1  riastrad 			si_get_mclk_frequency_ratio(memory_clock, memory_level->StrobeEnable);
   2949  1.1  riastrad 		if (pi->mclk_edc_enable_threshold &&
   2950  1.1  riastrad 		    (memory_clock > pi->mclk_edc_enable_threshold))
   2951  1.1  riastrad 			memory_level->EdcReadEnable = true;
   2952  1.1  riastrad 
   2953  1.1  riastrad 		if (pi->mclk_edc_wr_enable_threshold &&
   2954  1.1  riastrad 		    (memory_clock > pi->mclk_edc_wr_enable_threshold))
   2955  1.1  riastrad 			memory_level->EdcWriteEnable = true;
   2956  1.1  riastrad 
   2957  1.1  riastrad 		if (memory_level->StrobeEnable) {
   2958  1.1  riastrad 			if (si_get_mclk_frequency_ratio(memory_clock, true) >=
   2959  1.1  riastrad 			    ((RREG32(MC_SEQ_MISC7) >> 16) & 0xf))
   2960  1.1  riastrad 				dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
   2961  1.1  riastrad 			else
   2962  1.1  riastrad 				dll_state_on = ((RREG32(MC_SEQ_MISC6) >> 1) & 0x1) ? true : false;
   2963  1.1  riastrad 		} else {
   2964  1.1  riastrad 			dll_state_on = pi->dll_default_on;
   2965  1.1  riastrad 		}
   2966  1.1  riastrad 	} else {
   2967  1.1  riastrad 		memory_level->StrobeRatio = si_get_ddr3_mclk_frequency_ratio(memory_clock);
   2968  1.1  riastrad 		dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
   2969  1.1  riastrad 	}
   2970  1.1  riastrad 
   2971  1.1  riastrad 	ret = ci_calculate_mclk_params(rdev, memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
   2972  1.1  riastrad 	if (ret)
   2973  1.1  riastrad 		return ret;
   2974  1.1  riastrad 
   2975  1.1  riastrad 	memory_level->MinVddc = cpu_to_be32(memory_level->MinVddc * VOLTAGE_SCALE);
   2976  1.1  riastrad 	memory_level->MinVddcPhases = cpu_to_be32(memory_level->MinVddcPhases);
   2977  1.4  riastrad 	memory_level->MinVddci = cpu_to_be32(memory_level->MinVddci * VOLTAGE_SCALE);
   2978  1.4  riastrad 	memory_level->MinMvdd = cpu_to_be32(memory_level->MinMvdd * VOLTAGE_SCALE);
   2979  1.1  riastrad 
   2980  1.1  riastrad 	memory_level->MclkFrequency = cpu_to_be32(memory_level->MclkFrequency);
   2981  1.1  riastrad 	memory_level->ActivityLevel = cpu_to_be16(memory_level->ActivityLevel);
   2982  1.1  riastrad 	memory_level->MpllFuncCntl = cpu_to_be32(memory_level->MpllFuncCntl);
   2983  1.1  riastrad 	memory_level->MpllFuncCntl_1 = cpu_to_be32(memory_level->MpllFuncCntl_1);
   2984  1.1  riastrad 	memory_level->MpllFuncCntl_2 = cpu_to_be32(memory_level->MpllFuncCntl_2);
   2985  1.1  riastrad 	memory_level->MpllAdFuncCntl = cpu_to_be32(memory_level->MpllAdFuncCntl);
   2986  1.1  riastrad 	memory_level->MpllDqFuncCntl = cpu_to_be32(memory_level->MpllDqFuncCntl);
   2987  1.1  riastrad 	memory_level->MclkPwrmgtCntl = cpu_to_be32(memory_level->MclkPwrmgtCntl);
   2988  1.1  riastrad 	memory_level->DllCntl = cpu_to_be32(memory_level->DllCntl);
   2989  1.1  riastrad 	memory_level->MpllSs1 = cpu_to_be32(memory_level->MpllSs1);
   2990  1.1  riastrad 	memory_level->MpllSs2 = cpu_to_be32(memory_level->MpllSs2);
   2991  1.1  riastrad 
   2992  1.1  riastrad 	return 0;
   2993  1.1  riastrad }
   2994  1.1  riastrad 
   2995  1.1  riastrad static int ci_populate_smc_acpi_level(struct radeon_device *rdev,
   2996  1.1  riastrad 				      SMU7_Discrete_DpmTable *table)
   2997  1.1  riastrad {
   2998  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   2999  1.1  riastrad 	struct atom_clock_dividers dividers;
   3000  1.1  riastrad 	SMU7_Discrete_VoltageLevel voltage_level;
   3001  1.1  riastrad 	u32 spll_func_cntl = pi->clock_registers.cg_spll_func_cntl;
   3002  1.1  riastrad 	u32 spll_func_cntl_2 = pi->clock_registers.cg_spll_func_cntl_2;
   3003  1.1  riastrad 	u32 dll_cntl = pi->clock_registers.dll_cntl;
   3004  1.1  riastrad 	u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
   3005  1.1  riastrad 	int ret;
   3006  1.1  riastrad 
   3007  1.1  riastrad 	table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
   3008  1.1  riastrad 
   3009  1.1  riastrad 	if (pi->acpi_vddc)
   3010  1.1  riastrad 		table->ACPILevel.MinVddc = cpu_to_be32(pi->acpi_vddc * VOLTAGE_SCALE);
   3011  1.1  riastrad 	else
   3012  1.1  riastrad 		table->ACPILevel.MinVddc = cpu_to_be32(pi->min_vddc_in_pp_table * VOLTAGE_SCALE);
   3013  1.1  riastrad 
   3014  1.1  riastrad 	table->ACPILevel.MinVddcPhases = pi->vddc_phase_shed_control ? 0 : 1;
   3015  1.1  riastrad 
   3016  1.1  riastrad 	table->ACPILevel.SclkFrequency = rdev->clock.spll.reference_freq;
   3017  1.1  riastrad 
   3018  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev,
   3019  1.1  riastrad 					     COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
   3020  1.1  riastrad 					     table->ACPILevel.SclkFrequency, false, &dividers);
   3021  1.1  riastrad 	if (ret)
   3022  1.1  riastrad 		return ret;
   3023  1.1  riastrad 
   3024  1.1  riastrad 	table->ACPILevel.SclkDid = (u8)dividers.post_divider;
   3025  1.1  riastrad 	table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
   3026  1.1  riastrad 	table->ACPILevel.DeepSleepDivId = 0;
   3027  1.1  riastrad 
   3028  1.1  riastrad 	spll_func_cntl &= ~SPLL_PWRON;
   3029  1.1  riastrad 	spll_func_cntl |= SPLL_RESET;
   3030  1.1  riastrad 
   3031  1.1  riastrad 	spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
   3032  1.1  riastrad 	spll_func_cntl_2 |= SCLK_MUX_SEL(4);
   3033  1.1  riastrad 
   3034  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
   3035  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
   3036  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl3 = pi->clock_registers.cg_spll_func_cntl_3;
   3037  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl4 = pi->clock_registers.cg_spll_func_cntl_4;
   3038  1.1  riastrad 	table->ACPILevel.SpllSpreadSpectrum = pi->clock_registers.cg_spll_spread_spectrum;
   3039  1.1  riastrad 	table->ACPILevel.SpllSpreadSpectrum2 = pi->clock_registers.cg_spll_spread_spectrum_2;
   3040  1.1  riastrad 	table->ACPILevel.CcPwrDynRm = 0;
   3041  1.1  riastrad 	table->ACPILevel.CcPwrDynRm1 = 0;
   3042  1.1  riastrad 
   3043  1.1  riastrad 	table->ACPILevel.Flags = cpu_to_be32(table->ACPILevel.Flags);
   3044  1.1  riastrad 	table->ACPILevel.MinVddcPhases = cpu_to_be32(table->ACPILevel.MinVddcPhases);
   3045  1.1  riastrad 	table->ACPILevel.SclkFrequency = cpu_to_be32(table->ACPILevel.SclkFrequency);
   3046  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl);
   3047  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl2 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl2);
   3048  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl3 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl3);
   3049  1.1  riastrad 	table->ACPILevel.CgSpllFuncCntl4 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl4);
   3050  1.1  riastrad 	table->ACPILevel.SpllSpreadSpectrum = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum);
   3051  1.1  riastrad 	table->ACPILevel.SpllSpreadSpectrum2 = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum2);
   3052  1.1  riastrad 	table->ACPILevel.CcPwrDynRm = cpu_to_be32(table->ACPILevel.CcPwrDynRm);
   3053  1.1  riastrad 	table->ACPILevel.CcPwrDynRm1 = cpu_to_be32(table->ACPILevel.CcPwrDynRm1);
   3054  1.1  riastrad 
   3055  1.1  riastrad 	table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
   3056  1.1  riastrad 	table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
   3057  1.1  riastrad 
   3058  1.1  riastrad 	if (pi->vddci_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
   3059  1.1  riastrad 		if (pi->acpi_vddci)
   3060  1.1  riastrad 			table->MemoryACPILevel.MinVddci =
   3061  1.1  riastrad 				cpu_to_be32(pi->acpi_vddci * VOLTAGE_SCALE);
   3062  1.1  riastrad 		else
   3063  1.1  riastrad 			table->MemoryACPILevel.MinVddci =
   3064  1.1  riastrad 				cpu_to_be32(pi->min_vddci_in_pp_table * VOLTAGE_SCALE);
   3065  1.1  riastrad 	}
   3066  1.1  riastrad 
   3067  1.1  riastrad 	if (ci_populate_mvdd_value(rdev, 0, &voltage_level))
   3068  1.1  riastrad 		table->MemoryACPILevel.MinMvdd = 0;
   3069  1.1  riastrad 	else
   3070  1.1  riastrad 		table->MemoryACPILevel.MinMvdd =
   3071  1.1  riastrad 			cpu_to_be32(voltage_level.Voltage * VOLTAGE_SCALE);
   3072  1.1  riastrad 
   3073  1.1  riastrad 	mclk_pwrmgt_cntl |= MRDCK0_RESET | MRDCK1_RESET;
   3074  1.1  riastrad 	mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
   3075  1.1  riastrad 
   3076  1.1  riastrad 	dll_cntl &= ~(MRDCK0_BYPASS | MRDCK1_BYPASS);
   3077  1.1  riastrad 
   3078  1.1  riastrad 	table->MemoryACPILevel.DllCntl = cpu_to_be32(dll_cntl);
   3079  1.1  riastrad 	table->MemoryACPILevel.MclkPwrmgtCntl = cpu_to_be32(mclk_pwrmgt_cntl);
   3080  1.1  riastrad 	table->MemoryACPILevel.MpllAdFuncCntl =
   3081  1.1  riastrad 		cpu_to_be32(pi->clock_registers.mpll_ad_func_cntl);
   3082  1.1  riastrad 	table->MemoryACPILevel.MpllDqFuncCntl =
   3083  1.1  riastrad 		cpu_to_be32(pi->clock_registers.mpll_dq_func_cntl);
   3084  1.1  riastrad 	table->MemoryACPILevel.MpllFuncCntl =
   3085  1.1  riastrad 		cpu_to_be32(pi->clock_registers.mpll_func_cntl);
   3086  1.1  riastrad 	table->MemoryACPILevel.MpllFuncCntl_1 =
   3087  1.1  riastrad 		cpu_to_be32(pi->clock_registers.mpll_func_cntl_1);
   3088  1.1  riastrad 	table->MemoryACPILevel.MpllFuncCntl_2 =
   3089  1.1  riastrad 		cpu_to_be32(pi->clock_registers.mpll_func_cntl_2);
   3090  1.1  riastrad 	table->MemoryACPILevel.MpllSs1 = cpu_to_be32(pi->clock_registers.mpll_ss1);
   3091  1.1  riastrad 	table->MemoryACPILevel.MpllSs2 = cpu_to_be32(pi->clock_registers.mpll_ss2);
   3092  1.1  riastrad 
   3093  1.1  riastrad 	table->MemoryACPILevel.EnabledForThrottle = 0;
   3094  1.1  riastrad 	table->MemoryACPILevel.EnabledForActivity = 0;
   3095  1.1  riastrad 	table->MemoryACPILevel.UpH = 0;
   3096  1.1  riastrad 	table->MemoryACPILevel.DownH = 100;
   3097  1.1  riastrad 	table->MemoryACPILevel.VoltageDownH = 0;
   3098  1.1  riastrad 	table->MemoryACPILevel.ActivityLevel =
   3099  1.1  riastrad 		cpu_to_be16((u16)pi->mclk_activity_target);
   3100  1.1  riastrad 
   3101  1.1  riastrad 	table->MemoryACPILevel.StutterEnable = false;
   3102  1.1  riastrad 	table->MemoryACPILevel.StrobeEnable = false;
   3103  1.1  riastrad 	table->MemoryACPILevel.EdcReadEnable = false;
   3104  1.1  riastrad 	table->MemoryACPILevel.EdcWriteEnable = false;
   3105  1.1  riastrad 	table->MemoryACPILevel.RttEnable = false;
   3106  1.1  riastrad 
   3107  1.1  riastrad 	return 0;
   3108  1.1  riastrad }
   3109  1.1  riastrad 
   3110  1.1  riastrad 
   3111  1.1  riastrad static int ci_enable_ulv(struct radeon_device *rdev, bool enable)
   3112  1.1  riastrad {
   3113  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3114  1.1  riastrad 	struct ci_ulv_parm *ulv = &pi->ulv;
   3115  1.1  riastrad 
   3116  1.1  riastrad 	if (ulv->supported) {
   3117  1.1  riastrad 		if (enable)
   3118  1.1  riastrad 			return (ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableULV) == PPSMC_Result_OK) ?
   3119  1.1  riastrad 				0 : -EINVAL;
   3120  1.1  riastrad 		else
   3121  1.1  riastrad 			return (ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableULV) == PPSMC_Result_OK) ?
   3122  1.1  riastrad 				0 : -EINVAL;
   3123  1.1  riastrad 	}
   3124  1.1  riastrad 
   3125  1.1  riastrad 	return 0;
   3126  1.1  riastrad }
   3127  1.1  riastrad 
   3128  1.1  riastrad static int ci_populate_ulv_level(struct radeon_device *rdev,
   3129  1.1  riastrad 				 SMU7_Discrete_Ulv *state)
   3130  1.1  riastrad {
   3131  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3132  1.1  riastrad 	u16 ulv_voltage = rdev->pm.dpm.backbias_response_time;
   3133  1.1  riastrad 
   3134  1.1  riastrad 	state->CcPwrDynRm = 0;
   3135  1.1  riastrad 	state->CcPwrDynRm1 = 0;
   3136  1.1  riastrad 
   3137  1.1  riastrad 	if (ulv_voltage == 0) {
   3138  1.1  riastrad 		pi->ulv.supported = false;
   3139  1.1  riastrad 		return 0;
   3140  1.1  riastrad 	}
   3141  1.1  riastrad 
   3142  1.1  riastrad 	if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
   3143  1.1  riastrad 		if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
   3144  1.1  riastrad 			state->VddcOffset = 0;
   3145  1.1  riastrad 		else
   3146  1.1  riastrad 			state->VddcOffset =
   3147  1.1  riastrad 				rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage;
   3148  1.1  riastrad 	} else {
   3149  1.1  riastrad 		if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
   3150  1.1  riastrad 			state->VddcOffsetVid = 0;
   3151  1.1  riastrad 		else
   3152  1.1  riastrad 			state->VddcOffsetVid = (u8)
   3153  1.1  riastrad 				((rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage) *
   3154  1.1  riastrad 				 VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
   3155  1.1  riastrad 	}
   3156  1.1  riastrad 	state->VddcPhase = pi->vddc_phase_shed_control ? 0 : 1;
   3157  1.1  riastrad 
   3158  1.1  riastrad 	state->CcPwrDynRm = cpu_to_be32(state->CcPwrDynRm);
   3159  1.1  riastrad 	state->CcPwrDynRm1 = cpu_to_be32(state->CcPwrDynRm1);
   3160  1.1  riastrad 	state->VddcOffset = cpu_to_be16(state->VddcOffset);
   3161  1.1  riastrad 
   3162  1.1  riastrad 	return 0;
   3163  1.1  riastrad }
   3164  1.1  riastrad 
   3165  1.1  riastrad static int ci_calculate_sclk_params(struct radeon_device *rdev,
   3166  1.1  riastrad 				    u32 engine_clock,
   3167  1.1  riastrad 				    SMU7_Discrete_GraphicsLevel *sclk)
   3168  1.1  riastrad {
   3169  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3170  1.1  riastrad 	struct atom_clock_dividers dividers;
   3171  1.1  riastrad 	u32 spll_func_cntl_3 = pi->clock_registers.cg_spll_func_cntl_3;
   3172  1.1  riastrad 	u32 spll_func_cntl_4 = pi->clock_registers.cg_spll_func_cntl_4;
   3173  1.1  riastrad 	u32 cg_spll_spread_spectrum = pi->clock_registers.cg_spll_spread_spectrum;
   3174  1.1  riastrad 	u32 cg_spll_spread_spectrum_2 = pi->clock_registers.cg_spll_spread_spectrum_2;
   3175  1.1  riastrad 	u32 reference_clock = rdev->clock.spll.reference_freq;
   3176  1.1  riastrad 	u32 reference_divider;
   3177  1.1  riastrad 	u32 fbdiv;
   3178  1.1  riastrad 	int ret;
   3179  1.1  riastrad 
   3180  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev,
   3181  1.1  riastrad 					     COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
   3182  1.1  riastrad 					     engine_clock, false, &dividers);
   3183  1.1  riastrad 	if (ret)
   3184  1.1  riastrad 		return ret;
   3185  1.1  riastrad 
   3186  1.1  riastrad 	reference_divider = 1 + dividers.ref_div;
   3187  1.1  riastrad 	fbdiv = dividers.fb_div & 0x3FFFFFF;
   3188  1.1  riastrad 
   3189  1.1  riastrad 	spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
   3190  1.1  riastrad 	spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
   3191  1.4  riastrad 	spll_func_cntl_3 |= SPLL_DITHEN;
   3192  1.1  riastrad 
   3193  1.1  riastrad 	if (pi->caps_sclk_ss_support) {
   3194  1.1  riastrad 		struct radeon_atom_ss ss;
   3195  1.1  riastrad 		u32 vco_freq = engine_clock * dividers.post_div;
   3196  1.1  riastrad 
   3197  1.1  riastrad 		if (radeon_atombios_get_asic_ss_info(rdev, &ss,
   3198  1.1  riastrad 						     ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
   3199  1.1  riastrad 			u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
   3200  1.1  riastrad 			u32 clk_v = 4 * ss.percentage * fbdiv / (clk_s * 10000);
   3201  1.1  riastrad 
   3202  1.1  riastrad 			cg_spll_spread_spectrum &= ~CLK_S_MASK;
   3203  1.1  riastrad 			cg_spll_spread_spectrum |= CLK_S(clk_s);
   3204  1.1  riastrad 			cg_spll_spread_spectrum |= SSEN;
   3205  1.1  riastrad 
   3206  1.1  riastrad 			cg_spll_spread_spectrum_2 &= ~CLK_V_MASK;
   3207  1.1  riastrad 			cg_spll_spread_spectrum_2 |= CLK_V(clk_v);
   3208  1.1  riastrad 		}
   3209  1.1  riastrad 	}
   3210  1.1  riastrad 
   3211  1.1  riastrad 	sclk->SclkFrequency = engine_clock;
   3212  1.1  riastrad 	sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
   3213  1.1  riastrad 	sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
   3214  1.1  riastrad 	sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
   3215  1.1  riastrad 	sclk->SpllSpreadSpectrum2  = cg_spll_spread_spectrum_2;
   3216  1.1  riastrad 	sclk->SclkDid = (u8)dividers.post_divider;
   3217  1.1  riastrad 
   3218  1.1  riastrad 	return 0;
   3219  1.1  riastrad }
   3220  1.1  riastrad 
   3221  1.1  riastrad static int ci_populate_single_graphic_level(struct radeon_device *rdev,
   3222  1.1  riastrad 					    u32 engine_clock,
   3223  1.1  riastrad 					    u16 sclk_activity_level_t,
   3224  1.1  riastrad 					    SMU7_Discrete_GraphicsLevel *graphic_level)
   3225  1.1  riastrad {
   3226  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3227  1.1  riastrad 	int ret;
   3228  1.1  riastrad 
   3229  1.1  riastrad 	ret = ci_calculate_sclk_params(rdev, engine_clock, graphic_level);
   3230  1.1  riastrad 	if (ret)
   3231  1.1  riastrad 		return ret;
   3232  1.1  riastrad 
   3233  1.1  riastrad 	ret = ci_get_dependency_volt_by_clk(rdev,
   3234  1.1  riastrad 					    &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk,
   3235  1.1  riastrad 					    engine_clock, &graphic_level->MinVddc);
   3236  1.1  riastrad 	if (ret)
   3237  1.1  riastrad 		return ret;
   3238  1.1  riastrad 
   3239  1.1  riastrad 	graphic_level->SclkFrequency = engine_clock;
   3240  1.1  riastrad 
   3241  1.1  riastrad 	graphic_level->Flags =  0;
   3242  1.1  riastrad 	graphic_level->MinVddcPhases = 1;
   3243  1.1  riastrad 
   3244  1.1  riastrad 	if (pi->vddc_phase_shed_control)
   3245  1.1  riastrad 		ci_populate_phase_value_based_on_sclk(rdev,
   3246  1.1  riastrad 						      &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
   3247  1.1  riastrad 						      engine_clock,
   3248  1.1  riastrad 						      &graphic_level->MinVddcPhases);
   3249  1.1  riastrad 
   3250  1.1  riastrad 	graphic_level->ActivityLevel = sclk_activity_level_t;
   3251  1.1  riastrad 
   3252  1.1  riastrad 	graphic_level->CcPwrDynRm = 0;
   3253  1.1  riastrad 	graphic_level->CcPwrDynRm1 = 0;
   3254  1.1  riastrad 	graphic_level->EnabledForThrottle = 1;
   3255  1.1  riastrad 	graphic_level->UpH = 0;
   3256  1.1  riastrad 	graphic_level->DownH = 0;
   3257  1.1  riastrad 	graphic_level->VoltageDownH = 0;
   3258  1.1  riastrad 	graphic_level->PowerThrottle = 0;
   3259  1.1  riastrad 
   3260  1.1  riastrad 	if (pi->caps_sclk_ds)
   3261  1.1  riastrad 		graphic_level->DeepSleepDivId = ci_get_sleep_divider_id_from_clock(rdev,
   3262  1.1  riastrad 										   engine_clock,
   3263  1.1  riastrad 										   CISLAND_MINIMUM_ENGINE_CLOCK);
   3264  1.1  riastrad 
   3265  1.1  riastrad 	graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
   3266  1.1  riastrad 
   3267  1.1  riastrad 	graphic_level->Flags = cpu_to_be32(graphic_level->Flags);
   3268  1.4  riastrad 	graphic_level->MinVddc = cpu_to_be32(graphic_level->MinVddc * VOLTAGE_SCALE);
   3269  1.1  riastrad 	graphic_level->MinVddcPhases = cpu_to_be32(graphic_level->MinVddcPhases);
   3270  1.1  riastrad 	graphic_level->SclkFrequency = cpu_to_be32(graphic_level->SclkFrequency);
   3271  1.1  riastrad 	graphic_level->ActivityLevel = cpu_to_be16(graphic_level->ActivityLevel);
   3272  1.1  riastrad 	graphic_level->CgSpllFuncCntl3 = cpu_to_be32(graphic_level->CgSpllFuncCntl3);
   3273  1.1  riastrad 	graphic_level->CgSpllFuncCntl4 = cpu_to_be32(graphic_level->CgSpllFuncCntl4);
   3274  1.1  riastrad 	graphic_level->SpllSpreadSpectrum = cpu_to_be32(graphic_level->SpllSpreadSpectrum);
   3275  1.1  riastrad 	graphic_level->SpllSpreadSpectrum2 = cpu_to_be32(graphic_level->SpllSpreadSpectrum2);
   3276  1.1  riastrad 	graphic_level->CcPwrDynRm = cpu_to_be32(graphic_level->CcPwrDynRm);
   3277  1.1  riastrad 	graphic_level->CcPwrDynRm1 = cpu_to_be32(graphic_level->CcPwrDynRm1);
   3278  1.1  riastrad 
   3279  1.1  riastrad 	return 0;
   3280  1.1  riastrad }
   3281  1.1  riastrad 
   3282  1.1  riastrad static int ci_populate_all_graphic_levels(struct radeon_device *rdev)
   3283  1.1  riastrad {
   3284  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3285  1.1  riastrad 	struct ci_dpm_table *dpm_table = &pi->dpm_table;
   3286  1.1  riastrad 	u32 level_array_address = pi->dpm_table_start +
   3287  1.1  riastrad 		offsetof(SMU7_Discrete_DpmTable, GraphicsLevel);
   3288  1.1  riastrad 	u32 level_array_size = sizeof(SMU7_Discrete_GraphicsLevel) *
   3289  1.1  riastrad 		SMU7_MAX_LEVELS_GRAPHICS;
   3290  1.1  riastrad 	SMU7_Discrete_GraphicsLevel *levels = pi->smc_state_table.GraphicsLevel;
   3291  1.1  riastrad 	u32 i, ret;
   3292  1.1  riastrad 
   3293  1.1  riastrad 	memset(levels, 0, level_array_size);
   3294  1.1  riastrad 
   3295  1.1  riastrad 	for (i = 0; i < dpm_table->sclk_table.count; i++) {
   3296  1.1  riastrad 		ret = ci_populate_single_graphic_level(rdev,
   3297  1.1  riastrad 						       dpm_table->sclk_table.dpm_levels[i].value,
   3298  1.1  riastrad 						       (u16)pi->activity_target[i],
   3299  1.1  riastrad 						       &pi->smc_state_table.GraphicsLevel[i]);
   3300  1.1  riastrad 		if (ret)
   3301  1.1  riastrad 			return ret;
   3302  1.1  riastrad 		if (i > 1)
   3303  1.1  riastrad 			pi->smc_state_table.GraphicsLevel[i].DeepSleepDivId = 0;
   3304  1.1  riastrad 		if (i == (dpm_table->sclk_table.count - 1))
   3305  1.1  riastrad 			pi->smc_state_table.GraphicsLevel[i].DisplayWatermark =
   3306  1.1  riastrad 				PPSMC_DISPLAY_WATERMARK_HIGH;
   3307  1.1  riastrad 	}
   3308  1.1  riastrad 	pi->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
   3309  1.1  riastrad 
   3310  1.1  riastrad 	pi->smc_state_table.GraphicsDpmLevelCount = (u8)dpm_table->sclk_table.count;
   3311  1.1  riastrad 	pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
   3312  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
   3313  1.1  riastrad 
   3314  1.1  riastrad 	ret = ci_copy_bytes_to_smc(rdev, level_array_address,
   3315  1.1  riastrad 				   (u8 *)levels, level_array_size,
   3316  1.1  riastrad 				   pi->sram_end);
   3317  1.1  riastrad 	if (ret)
   3318  1.1  riastrad 		return ret;
   3319  1.1  riastrad 
   3320  1.1  riastrad 	return 0;
   3321  1.1  riastrad }
   3322  1.1  riastrad 
   3323  1.1  riastrad static int ci_populate_ulv_state(struct radeon_device *rdev,
   3324  1.1  riastrad 				 SMU7_Discrete_Ulv *ulv_level)
   3325  1.1  riastrad {
   3326  1.1  riastrad 	return ci_populate_ulv_level(rdev, ulv_level);
   3327  1.1  riastrad }
   3328  1.1  riastrad 
   3329  1.1  riastrad static int ci_populate_all_memory_levels(struct radeon_device *rdev)
   3330  1.1  riastrad {
   3331  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3332  1.1  riastrad 	struct ci_dpm_table *dpm_table = &pi->dpm_table;
   3333  1.1  riastrad 	u32 level_array_address = pi->dpm_table_start +
   3334  1.1  riastrad 		offsetof(SMU7_Discrete_DpmTable, MemoryLevel);
   3335  1.1  riastrad 	u32 level_array_size = sizeof(SMU7_Discrete_MemoryLevel) *
   3336  1.1  riastrad 		SMU7_MAX_LEVELS_MEMORY;
   3337  1.1  riastrad 	SMU7_Discrete_MemoryLevel *levels = pi->smc_state_table.MemoryLevel;
   3338  1.1  riastrad 	u32 i, ret;
   3339  1.1  riastrad 
   3340  1.1  riastrad 	memset(levels, 0, level_array_size);
   3341  1.1  riastrad 
   3342  1.1  riastrad 	for (i = 0; i < dpm_table->mclk_table.count; i++) {
   3343  1.1  riastrad 		if (dpm_table->mclk_table.dpm_levels[i].value == 0)
   3344  1.1  riastrad 			return -EINVAL;
   3345  1.1  riastrad 		ret = ci_populate_single_memory_level(rdev,
   3346  1.1  riastrad 						      dpm_table->mclk_table.dpm_levels[i].value,
   3347  1.1  riastrad 						      &pi->smc_state_table.MemoryLevel[i]);
   3348  1.1  riastrad 		if (ret)
   3349  1.1  riastrad 			return ret;
   3350  1.1  riastrad 	}
   3351  1.1  riastrad 
   3352  1.1  riastrad 	pi->smc_state_table.MemoryLevel[0].EnabledForActivity = 1;
   3353  1.1  riastrad 
   3354  1.1  riastrad 	if ((dpm_table->mclk_table.count >= 2) &&
   3355  1.1  riastrad 	    ((rdev->pdev->device == 0x67B0) || (rdev->pdev->device == 0x67B1))) {
   3356  1.1  riastrad 		pi->smc_state_table.MemoryLevel[1].MinVddc =
   3357  1.1  riastrad 			pi->smc_state_table.MemoryLevel[0].MinVddc;
   3358  1.1  riastrad 		pi->smc_state_table.MemoryLevel[1].MinVddcPhases =
   3359  1.1  riastrad 			pi->smc_state_table.MemoryLevel[0].MinVddcPhases;
   3360  1.1  riastrad 	}
   3361  1.1  riastrad 
   3362  1.1  riastrad 	pi->smc_state_table.MemoryLevel[0].ActivityLevel = cpu_to_be16(0x1F);
   3363  1.1  riastrad 
   3364  1.1  riastrad 	pi->smc_state_table.MemoryDpmLevelCount = (u8)dpm_table->mclk_table.count;
   3365  1.1  riastrad 	pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
   3366  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
   3367  1.1  riastrad 
   3368  1.1  riastrad 	pi->smc_state_table.MemoryLevel[dpm_table->mclk_table.count - 1].DisplayWatermark =
   3369  1.1  riastrad 		PPSMC_DISPLAY_WATERMARK_HIGH;
   3370  1.1  riastrad 
   3371  1.1  riastrad 	ret = ci_copy_bytes_to_smc(rdev, level_array_address,
   3372  1.1  riastrad 				   (u8 *)levels, level_array_size,
   3373  1.1  riastrad 				   pi->sram_end);
   3374  1.1  riastrad 	if (ret)
   3375  1.1  riastrad 		return ret;
   3376  1.1  riastrad 
   3377  1.1  riastrad 	return 0;
   3378  1.1  riastrad }
   3379  1.1  riastrad 
   3380  1.1  riastrad static void ci_reset_single_dpm_table(struct radeon_device *rdev,
   3381  1.1  riastrad 				      struct ci_single_dpm_table* dpm_table,
   3382  1.1  riastrad 				      u32 count)
   3383  1.1  riastrad {
   3384  1.1  riastrad 	u32 i;
   3385  1.1  riastrad 
   3386  1.1  riastrad 	dpm_table->count = count;
   3387  1.1  riastrad 	for (i = 0; i < MAX_REGULAR_DPM_NUMBER; i++)
   3388  1.1  riastrad 		dpm_table->dpm_levels[i].enabled = false;
   3389  1.1  riastrad }
   3390  1.1  riastrad 
   3391  1.1  riastrad static void ci_setup_pcie_table_entry(struct ci_single_dpm_table* dpm_table,
   3392  1.1  riastrad 				      u32 index, u32 pcie_gen, u32 pcie_lanes)
   3393  1.1  riastrad {
   3394  1.1  riastrad 	dpm_table->dpm_levels[index].value = pcie_gen;
   3395  1.1  riastrad 	dpm_table->dpm_levels[index].param1 = pcie_lanes;
   3396  1.1  riastrad 	dpm_table->dpm_levels[index].enabled = true;
   3397  1.1  riastrad }
   3398  1.1  riastrad 
   3399  1.1  riastrad static int ci_setup_default_pcie_tables(struct radeon_device *rdev)
   3400  1.1  riastrad {
   3401  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3402  1.1  riastrad 
   3403  1.1  riastrad 	if (!pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels)
   3404  1.1  riastrad 		return -EINVAL;
   3405  1.1  riastrad 
   3406  1.1  riastrad 	if (pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels) {
   3407  1.1  riastrad 		pi->pcie_gen_powersaving = pi->pcie_gen_performance;
   3408  1.1  riastrad 		pi->pcie_lane_powersaving = pi->pcie_lane_performance;
   3409  1.1  riastrad 	} else if (!pi->use_pcie_performance_levels && pi->use_pcie_powersaving_levels) {
   3410  1.1  riastrad 		pi->pcie_gen_performance = pi->pcie_gen_powersaving;
   3411  1.1  riastrad 		pi->pcie_lane_performance = pi->pcie_lane_powersaving;
   3412  1.1  riastrad 	}
   3413  1.1  riastrad 
   3414  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3415  1.1  riastrad 				  &pi->dpm_table.pcie_speed_table,
   3416  1.1  riastrad 				  SMU7_MAX_LEVELS_LINK);
   3417  1.1  riastrad 
   3418  1.1  riastrad 	if (rdev->family == CHIP_BONAIRE)
   3419  1.1  riastrad 		ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
   3420  1.1  riastrad 					  pi->pcie_gen_powersaving.min,
   3421  1.1  riastrad 					  pi->pcie_lane_powersaving.max);
   3422  1.1  riastrad 	else
   3423  1.1  riastrad 		ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
   3424  1.1  riastrad 					  pi->pcie_gen_powersaving.min,
   3425  1.1  riastrad 					  pi->pcie_lane_powersaving.min);
   3426  1.1  riastrad 	ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 1,
   3427  1.1  riastrad 				  pi->pcie_gen_performance.min,
   3428  1.1  riastrad 				  pi->pcie_lane_performance.min);
   3429  1.1  riastrad 	ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 2,
   3430  1.1  riastrad 				  pi->pcie_gen_powersaving.min,
   3431  1.1  riastrad 				  pi->pcie_lane_powersaving.max);
   3432  1.1  riastrad 	ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 3,
   3433  1.1  riastrad 				  pi->pcie_gen_performance.min,
   3434  1.1  riastrad 				  pi->pcie_lane_performance.max);
   3435  1.1  riastrad 	ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 4,
   3436  1.1  riastrad 				  pi->pcie_gen_powersaving.max,
   3437  1.1  riastrad 				  pi->pcie_lane_powersaving.max);
   3438  1.1  riastrad 	ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 5,
   3439  1.1  riastrad 				  pi->pcie_gen_performance.max,
   3440  1.1  riastrad 				  pi->pcie_lane_performance.max);
   3441  1.1  riastrad 
   3442  1.1  riastrad 	pi->dpm_table.pcie_speed_table.count = 6;
   3443  1.1  riastrad 
   3444  1.1  riastrad 	return 0;
   3445  1.1  riastrad }
   3446  1.1  riastrad 
   3447  1.1  riastrad static int ci_setup_default_dpm_tables(struct radeon_device *rdev)
   3448  1.1  riastrad {
   3449  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3450  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
   3451  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
   3452  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *allowed_mclk_table =
   3453  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
   3454  1.1  riastrad 	struct radeon_cac_leakage_table *std_voltage_table =
   3455  1.1  riastrad 		&rdev->pm.dpm.dyn_state.cac_leakage_table;
   3456  1.1  riastrad 	u32 i;
   3457  1.1  riastrad 
   3458  1.1  riastrad 	if (allowed_sclk_vddc_table == NULL)
   3459  1.1  riastrad 		return -EINVAL;
   3460  1.1  riastrad 	if (allowed_sclk_vddc_table->count < 1)
   3461  1.1  riastrad 		return -EINVAL;
   3462  1.1  riastrad 	if (allowed_mclk_table == NULL)
   3463  1.1  riastrad 		return -EINVAL;
   3464  1.1  riastrad 	if (allowed_mclk_table->count < 1)
   3465  1.1  riastrad 		return -EINVAL;
   3466  1.1  riastrad 
   3467  1.1  riastrad 	memset(&pi->dpm_table, 0, sizeof(struct ci_dpm_table));
   3468  1.1  riastrad 
   3469  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3470  1.1  riastrad 				  &pi->dpm_table.sclk_table,
   3471  1.1  riastrad 				  SMU7_MAX_LEVELS_GRAPHICS);
   3472  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3473  1.1  riastrad 				  &pi->dpm_table.mclk_table,
   3474  1.1  riastrad 				  SMU7_MAX_LEVELS_MEMORY);
   3475  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3476  1.1  riastrad 				  &pi->dpm_table.vddc_table,
   3477  1.1  riastrad 				  SMU7_MAX_LEVELS_VDDC);
   3478  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3479  1.1  riastrad 				  &pi->dpm_table.vddci_table,
   3480  1.1  riastrad 				  SMU7_MAX_LEVELS_VDDCI);
   3481  1.1  riastrad 	ci_reset_single_dpm_table(rdev,
   3482  1.1  riastrad 				  &pi->dpm_table.mvdd_table,
   3483  1.1  riastrad 				  SMU7_MAX_LEVELS_MVDD);
   3484  1.1  riastrad 
   3485  1.1  riastrad 	pi->dpm_table.sclk_table.count = 0;
   3486  1.1  riastrad 	for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
   3487  1.1  riastrad 		if ((i == 0) ||
   3488  1.1  riastrad 		    (pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count-1].value !=
   3489  1.1  riastrad 		     allowed_sclk_vddc_table->entries[i].clk)) {
   3490  1.1  riastrad 			pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].value =
   3491  1.1  riastrad 				allowed_sclk_vddc_table->entries[i].clk;
   3492  1.1  riastrad 			pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].enabled =
   3493  1.1  riastrad 				(i == 0) ? true : false;
   3494  1.1  riastrad 			pi->dpm_table.sclk_table.count++;
   3495  1.1  riastrad 		}
   3496  1.1  riastrad 	}
   3497  1.1  riastrad 
   3498  1.1  riastrad 	pi->dpm_table.mclk_table.count = 0;
   3499  1.1  riastrad 	for (i = 0; i < allowed_mclk_table->count; i++) {
   3500  1.1  riastrad 		if ((i == 0) ||
   3501  1.1  riastrad 		    (pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count-1].value !=
   3502  1.1  riastrad 		     allowed_mclk_table->entries[i].clk)) {
   3503  1.1  riastrad 			pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].value =
   3504  1.1  riastrad 				allowed_mclk_table->entries[i].clk;
   3505  1.1  riastrad 			pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].enabled =
   3506  1.1  riastrad 				(i == 0) ? true : false;
   3507  1.1  riastrad 			pi->dpm_table.mclk_table.count++;
   3508  1.1  riastrad 		}
   3509  1.1  riastrad 	}
   3510  1.1  riastrad 
   3511  1.1  riastrad 	for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
   3512  1.1  riastrad 		pi->dpm_table.vddc_table.dpm_levels[i].value =
   3513  1.1  riastrad 			allowed_sclk_vddc_table->entries[i].v;
   3514  1.1  riastrad 		pi->dpm_table.vddc_table.dpm_levels[i].param1 =
   3515  1.1  riastrad 			std_voltage_table->entries[i].leakage;
   3516  1.1  riastrad 		pi->dpm_table.vddc_table.dpm_levels[i].enabled = true;
   3517  1.1  riastrad 	}
   3518  1.1  riastrad 	pi->dpm_table.vddc_table.count = allowed_sclk_vddc_table->count;
   3519  1.1  riastrad 
   3520  1.1  riastrad 	allowed_mclk_table = &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
   3521  1.1  riastrad 	if (allowed_mclk_table) {
   3522  1.1  riastrad 		for (i = 0; i < allowed_mclk_table->count; i++) {
   3523  1.1  riastrad 			pi->dpm_table.vddci_table.dpm_levels[i].value =
   3524  1.1  riastrad 				allowed_mclk_table->entries[i].v;
   3525  1.1  riastrad 			pi->dpm_table.vddci_table.dpm_levels[i].enabled = true;
   3526  1.1  riastrad 		}
   3527  1.1  riastrad 		pi->dpm_table.vddci_table.count = allowed_mclk_table->count;
   3528  1.1  riastrad 	}
   3529  1.1  riastrad 
   3530  1.1  riastrad 	allowed_mclk_table = &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk;
   3531  1.1  riastrad 	if (allowed_mclk_table) {
   3532  1.1  riastrad 		for (i = 0; i < allowed_mclk_table->count; i++) {
   3533  1.1  riastrad 			pi->dpm_table.mvdd_table.dpm_levels[i].value =
   3534  1.1  riastrad 				allowed_mclk_table->entries[i].v;
   3535  1.1  riastrad 			pi->dpm_table.mvdd_table.dpm_levels[i].enabled = true;
   3536  1.1  riastrad 		}
   3537  1.1  riastrad 		pi->dpm_table.mvdd_table.count = allowed_mclk_table->count;
   3538  1.1  riastrad 	}
   3539  1.1  riastrad 
   3540  1.1  riastrad 	ci_setup_default_pcie_tables(rdev);
   3541  1.1  riastrad 
   3542  1.1  riastrad 	return 0;
   3543  1.1  riastrad }
   3544  1.1  riastrad 
   3545  1.1  riastrad static int ci_find_boot_level(struct ci_single_dpm_table *table,
   3546  1.1  riastrad 			      u32 value, u32 *boot_level)
   3547  1.1  riastrad {
   3548  1.1  riastrad 	u32 i;
   3549  1.1  riastrad 	int ret = -EINVAL;
   3550  1.1  riastrad 
   3551  1.1  riastrad 	for(i = 0; i < table->count; i++) {
   3552  1.1  riastrad 		if (value == table->dpm_levels[i].value) {
   3553  1.1  riastrad 			*boot_level = i;
   3554  1.1  riastrad 			ret = 0;
   3555  1.1  riastrad 		}
   3556  1.1  riastrad 	}
   3557  1.1  riastrad 
   3558  1.1  riastrad 	return ret;
   3559  1.1  riastrad }
   3560  1.1  riastrad 
   3561  1.1  riastrad static int ci_init_smc_table(struct radeon_device *rdev)
   3562  1.1  riastrad {
   3563  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3564  1.1  riastrad 	struct ci_ulv_parm *ulv = &pi->ulv;
   3565  1.1  riastrad 	struct radeon_ps *radeon_boot_state = rdev->pm.dpm.boot_ps;
   3566  1.1  riastrad 	SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
   3567  1.1  riastrad 	int ret;
   3568  1.1  riastrad 
   3569  1.1  riastrad 	ret = ci_setup_default_dpm_tables(rdev);
   3570  1.1  riastrad 	if (ret)
   3571  1.1  riastrad 		return ret;
   3572  1.1  riastrad 
   3573  1.1  riastrad 	if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE)
   3574  1.1  riastrad 		ci_populate_smc_voltage_tables(rdev, table);
   3575  1.1  riastrad 
   3576  1.1  riastrad 	ci_init_fps_limits(rdev);
   3577  1.1  riastrad 
   3578  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC)
   3579  1.1  riastrad 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
   3580  1.1  riastrad 
   3581  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
   3582  1.1  riastrad 		table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
   3583  1.1  riastrad 
   3584  1.1  riastrad 	if (pi->mem_gddr5)
   3585  1.1  riastrad 		table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
   3586  1.1  riastrad 
   3587  1.1  riastrad 	if (ulv->supported) {
   3588  1.1  riastrad 		ret = ci_populate_ulv_state(rdev, &pi->smc_state_table.Ulv);
   3589  1.1  riastrad 		if (ret)
   3590  1.1  riastrad 			return ret;
   3591  1.1  riastrad 		WREG32_SMC(CG_ULV_PARAMETER, ulv->cg_ulv_parameter);
   3592  1.1  riastrad 	}
   3593  1.1  riastrad 
   3594  1.1  riastrad 	ret = ci_populate_all_graphic_levels(rdev);
   3595  1.1  riastrad 	if (ret)
   3596  1.1  riastrad 		return ret;
   3597  1.1  riastrad 
   3598  1.1  riastrad 	ret = ci_populate_all_memory_levels(rdev);
   3599  1.1  riastrad 	if (ret)
   3600  1.1  riastrad 		return ret;
   3601  1.1  riastrad 
   3602  1.1  riastrad 	ci_populate_smc_link_level(rdev, table);
   3603  1.1  riastrad 
   3604  1.1  riastrad 	ret = ci_populate_smc_acpi_level(rdev, table);
   3605  1.1  riastrad 	if (ret)
   3606  1.1  riastrad 		return ret;
   3607  1.1  riastrad 
   3608  1.1  riastrad 	ret = ci_populate_smc_vce_level(rdev, table);
   3609  1.1  riastrad 	if (ret)
   3610  1.1  riastrad 		return ret;
   3611  1.1  riastrad 
   3612  1.1  riastrad 	ret = ci_populate_smc_acp_level(rdev, table);
   3613  1.1  riastrad 	if (ret)
   3614  1.1  riastrad 		return ret;
   3615  1.1  riastrad 
   3616  1.1  riastrad 	ret = ci_populate_smc_samu_level(rdev, table);
   3617  1.1  riastrad 	if (ret)
   3618  1.1  riastrad 		return ret;
   3619  1.1  riastrad 
   3620  1.1  riastrad 	ret = ci_do_program_memory_timing_parameters(rdev);
   3621  1.1  riastrad 	if (ret)
   3622  1.1  riastrad 		return ret;
   3623  1.1  riastrad 
   3624  1.1  riastrad 	ret = ci_populate_smc_uvd_level(rdev, table);
   3625  1.1  riastrad 	if (ret)
   3626  1.1  riastrad 		return ret;
   3627  1.1  riastrad 
   3628  1.1  riastrad 	table->UvdBootLevel  = 0;
   3629  1.1  riastrad 	table->VceBootLevel  = 0;
   3630  1.1  riastrad 	table->AcpBootLevel  = 0;
   3631  1.1  riastrad 	table->SamuBootLevel  = 0;
   3632  1.1  riastrad 	table->GraphicsBootLevel  = 0;
   3633  1.1  riastrad 	table->MemoryBootLevel  = 0;
   3634  1.1  riastrad 
   3635  1.1  riastrad 	ret = ci_find_boot_level(&pi->dpm_table.sclk_table,
   3636  1.1  riastrad 				 pi->vbios_boot_state.sclk_bootup_value,
   3637  1.1  riastrad 				 (u32 *)&pi->smc_state_table.GraphicsBootLevel);
   3638  1.1  riastrad 
   3639  1.1  riastrad 	ret = ci_find_boot_level(&pi->dpm_table.mclk_table,
   3640  1.1  riastrad 				 pi->vbios_boot_state.mclk_bootup_value,
   3641  1.1  riastrad 				 (u32 *)&pi->smc_state_table.MemoryBootLevel);
   3642  1.1  riastrad 
   3643  1.1  riastrad 	table->BootVddc = pi->vbios_boot_state.vddc_bootup_value;
   3644  1.1  riastrad 	table->BootVddci = pi->vbios_boot_state.vddci_bootup_value;
   3645  1.1  riastrad 	table->BootMVdd = pi->vbios_boot_state.mvdd_bootup_value;
   3646  1.1  riastrad 
   3647  1.1  riastrad 	ci_populate_smc_initial_state(rdev, radeon_boot_state);
   3648  1.1  riastrad 
   3649  1.1  riastrad 	ret = ci_populate_bapm_parameters_in_dpm_table(rdev);
   3650  1.1  riastrad 	if (ret)
   3651  1.1  riastrad 		return ret;
   3652  1.1  riastrad 
   3653  1.1  riastrad 	table->UVDInterval = 1;
   3654  1.1  riastrad 	table->VCEInterval = 1;
   3655  1.1  riastrad 	table->ACPInterval = 1;
   3656  1.1  riastrad 	table->SAMUInterval = 1;
   3657  1.1  riastrad 	table->GraphicsVoltageChangeEnable = 1;
   3658  1.1  riastrad 	table->GraphicsThermThrottleEnable = 1;
   3659  1.1  riastrad 	table->GraphicsInterval = 1;
   3660  1.1  riastrad 	table->VoltageInterval = 1;
   3661  1.1  riastrad 	table->ThermalInterval = 1;
   3662  1.1  riastrad 	table->TemperatureLimitHigh = (u16)((pi->thermal_temp_setting.temperature_high *
   3663  1.1  riastrad 					     CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
   3664  1.1  riastrad 	table->TemperatureLimitLow = (u16)((pi->thermal_temp_setting.temperature_low *
   3665  1.1  riastrad 					    CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
   3666  1.1  riastrad 	table->MemoryVoltageChangeEnable = 1;
   3667  1.1  riastrad 	table->MemoryInterval = 1;
   3668  1.1  riastrad 	table->VoltageResponseTime = 0;
   3669  1.1  riastrad 	table->VddcVddciDelta = 4000;
   3670  1.1  riastrad 	table->PhaseResponseTime = 0;
   3671  1.1  riastrad 	table->MemoryThermThrottleEnable = 1;
   3672  1.1  riastrad 	table->PCIeBootLinkLevel = pi->dpm_table.pcie_speed_table.count - 1;
   3673  1.1  riastrad 	table->PCIeGenInterval = 1;
   3674  1.1  riastrad 	if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2)
   3675  1.1  riastrad 		table->SVI2Enable  = 1;
   3676  1.1  riastrad 	else
   3677  1.1  riastrad 		table->SVI2Enable  = 0;
   3678  1.1  riastrad 
   3679  1.1  riastrad 	table->ThermGpio = 17;
   3680  1.1  riastrad 	table->SclkStepSize = 0x4000;
   3681  1.1  riastrad 
   3682  1.1  riastrad 	table->SystemFlags = cpu_to_be32(table->SystemFlags);
   3683  1.1  riastrad 	table->SmioMaskVddcVid = cpu_to_be32(table->SmioMaskVddcVid);
   3684  1.1  riastrad 	table->SmioMaskVddcPhase = cpu_to_be32(table->SmioMaskVddcPhase);
   3685  1.1  riastrad 	table->SmioMaskVddciVid = cpu_to_be32(table->SmioMaskVddciVid);
   3686  1.1  riastrad 	table->SmioMaskMvddVid = cpu_to_be32(table->SmioMaskMvddVid);
   3687  1.1  riastrad 	table->SclkStepSize = cpu_to_be32(table->SclkStepSize);
   3688  1.1  riastrad 	table->TemperatureLimitHigh = cpu_to_be16(table->TemperatureLimitHigh);
   3689  1.1  riastrad 	table->TemperatureLimitLow = cpu_to_be16(table->TemperatureLimitLow);
   3690  1.1  riastrad 	table->VddcVddciDelta = cpu_to_be16(table->VddcVddciDelta);
   3691  1.1  riastrad 	table->VoltageResponseTime = cpu_to_be16(table->VoltageResponseTime);
   3692  1.1  riastrad 	table->PhaseResponseTime = cpu_to_be16(table->PhaseResponseTime);
   3693  1.1  riastrad 	table->BootVddc = cpu_to_be16(table->BootVddc * VOLTAGE_SCALE);
   3694  1.1  riastrad 	table->BootVddci = cpu_to_be16(table->BootVddci * VOLTAGE_SCALE);
   3695  1.1  riastrad 	table->BootMVdd = cpu_to_be16(table->BootMVdd * VOLTAGE_SCALE);
   3696  1.1  riastrad 
   3697  1.1  riastrad 	ret = ci_copy_bytes_to_smc(rdev,
   3698  1.1  riastrad 				   pi->dpm_table_start +
   3699  1.1  riastrad 				   offsetof(SMU7_Discrete_DpmTable, SystemFlags),
   3700  1.1  riastrad 				   (u8 *)&table->SystemFlags,
   3701  1.1  riastrad 				   sizeof(SMU7_Discrete_DpmTable) - 3 * sizeof(SMU7_PIDController),
   3702  1.1  riastrad 				   pi->sram_end);
   3703  1.1  riastrad 	if (ret)
   3704  1.1  riastrad 		return ret;
   3705  1.1  riastrad 
   3706  1.1  riastrad 	return 0;
   3707  1.1  riastrad }
   3708  1.1  riastrad 
   3709  1.1  riastrad static void ci_trim_single_dpm_states(struct radeon_device *rdev,
   3710  1.1  riastrad 				      struct ci_single_dpm_table *dpm_table,
   3711  1.1  riastrad 				      u32 low_limit, u32 high_limit)
   3712  1.1  riastrad {
   3713  1.1  riastrad 	u32 i;
   3714  1.1  riastrad 
   3715  1.1  riastrad 	for (i = 0; i < dpm_table->count; i++) {
   3716  1.1  riastrad 		if ((dpm_table->dpm_levels[i].value < low_limit) ||
   3717  1.1  riastrad 		    (dpm_table->dpm_levels[i].value > high_limit))
   3718  1.1  riastrad 			dpm_table->dpm_levels[i].enabled = false;
   3719  1.1  riastrad 		else
   3720  1.1  riastrad 			dpm_table->dpm_levels[i].enabled = true;
   3721  1.1  riastrad 	}
   3722  1.1  riastrad }
   3723  1.1  riastrad 
   3724  1.1  riastrad static void ci_trim_pcie_dpm_states(struct radeon_device *rdev,
   3725  1.1  riastrad 				    u32 speed_low, u32 lanes_low,
   3726  1.1  riastrad 				    u32 speed_high, u32 lanes_high)
   3727  1.1  riastrad {
   3728  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3729  1.1  riastrad 	struct ci_single_dpm_table *pcie_table = &pi->dpm_table.pcie_speed_table;
   3730  1.1  riastrad 	u32 i, j;
   3731  1.1  riastrad 
   3732  1.1  riastrad 	for (i = 0; i < pcie_table->count; i++) {
   3733  1.1  riastrad 		if ((pcie_table->dpm_levels[i].value < speed_low) ||
   3734  1.1  riastrad 		    (pcie_table->dpm_levels[i].param1 < lanes_low) ||
   3735  1.1  riastrad 		    (pcie_table->dpm_levels[i].value > speed_high) ||
   3736  1.1  riastrad 		    (pcie_table->dpm_levels[i].param1 > lanes_high))
   3737  1.1  riastrad 			pcie_table->dpm_levels[i].enabled = false;
   3738  1.1  riastrad 		else
   3739  1.1  riastrad 			pcie_table->dpm_levels[i].enabled = true;
   3740  1.1  riastrad 	}
   3741  1.1  riastrad 
   3742  1.1  riastrad 	for (i = 0; i < pcie_table->count; i++) {
   3743  1.1  riastrad 		if (pcie_table->dpm_levels[i].enabled) {
   3744  1.1  riastrad 			for (j = i + 1; j < pcie_table->count; j++) {
   3745  1.1  riastrad 				if (pcie_table->dpm_levels[j].enabled) {
   3746  1.1  riastrad 					if ((pcie_table->dpm_levels[i].value == pcie_table->dpm_levels[j].value) &&
   3747  1.1  riastrad 					    (pcie_table->dpm_levels[i].param1 == pcie_table->dpm_levels[j].param1))
   3748  1.1  riastrad 						pcie_table->dpm_levels[j].enabled = false;
   3749  1.1  riastrad 				}
   3750  1.1  riastrad 			}
   3751  1.1  riastrad 		}
   3752  1.1  riastrad 	}
   3753  1.1  riastrad }
   3754  1.1  riastrad 
   3755  1.1  riastrad static int ci_trim_dpm_states(struct radeon_device *rdev,
   3756  1.1  riastrad 			      struct radeon_ps *radeon_state)
   3757  1.1  riastrad {
   3758  1.1  riastrad 	struct ci_ps *state = ci_get_ps(radeon_state);
   3759  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3760  1.1  riastrad 	u32 high_limit_count;
   3761  1.1  riastrad 
   3762  1.1  riastrad 	if (state->performance_level_count < 1)
   3763  1.1  riastrad 		return -EINVAL;
   3764  1.1  riastrad 
   3765  1.1  riastrad 	if (state->performance_level_count == 1)
   3766  1.1  riastrad 		high_limit_count = 0;
   3767  1.1  riastrad 	else
   3768  1.1  riastrad 		high_limit_count = 1;
   3769  1.1  riastrad 
   3770  1.1  riastrad 	ci_trim_single_dpm_states(rdev,
   3771  1.1  riastrad 				  &pi->dpm_table.sclk_table,
   3772  1.1  riastrad 				  state->performance_levels[0].sclk,
   3773  1.1  riastrad 				  state->performance_levels[high_limit_count].sclk);
   3774  1.1  riastrad 
   3775  1.1  riastrad 	ci_trim_single_dpm_states(rdev,
   3776  1.1  riastrad 				  &pi->dpm_table.mclk_table,
   3777  1.1  riastrad 				  state->performance_levels[0].mclk,
   3778  1.1  riastrad 				  state->performance_levels[high_limit_count].mclk);
   3779  1.1  riastrad 
   3780  1.1  riastrad 	ci_trim_pcie_dpm_states(rdev,
   3781  1.1  riastrad 				state->performance_levels[0].pcie_gen,
   3782  1.1  riastrad 				state->performance_levels[0].pcie_lane,
   3783  1.1  riastrad 				state->performance_levels[high_limit_count].pcie_gen,
   3784  1.1  riastrad 				state->performance_levels[high_limit_count].pcie_lane);
   3785  1.1  riastrad 
   3786  1.1  riastrad 	return 0;
   3787  1.1  riastrad }
   3788  1.1  riastrad 
   3789  1.1  riastrad static int ci_apply_disp_minimum_voltage_request(struct radeon_device *rdev)
   3790  1.1  riastrad {
   3791  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *disp_voltage_table =
   3792  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk;
   3793  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *vddc_table =
   3794  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
   3795  1.1  riastrad 	u32 requested_voltage = 0;
   3796  1.1  riastrad 	u32 i;
   3797  1.1  riastrad 
   3798  1.1  riastrad 	if (disp_voltage_table == NULL)
   3799  1.1  riastrad 		return -EINVAL;
   3800  1.1  riastrad 	if (!disp_voltage_table->count)
   3801  1.1  riastrad 		return -EINVAL;
   3802  1.1  riastrad 
   3803  1.1  riastrad 	for (i = 0; i < disp_voltage_table->count; i++) {
   3804  1.1  riastrad 		if (rdev->clock.current_dispclk == disp_voltage_table->entries[i].clk)
   3805  1.1  riastrad 			requested_voltage = disp_voltage_table->entries[i].v;
   3806  1.1  riastrad 	}
   3807  1.1  riastrad 
   3808  1.1  riastrad 	for (i = 0; i < vddc_table->count; i++) {
   3809  1.1  riastrad 		if (requested_voltage <= vddc_table->entries[i].v) {
   3810  1.1  riastrad 			requested_voltage = vddc_table->entries[i].v;
   3811  1.1  riastrad 			return (ci_send_msg_to_smc_with_parameter(rdev,
   3812  1.1  riastrad 								  PPSMC_MSG_VddC_Request,
   3813  1.1  riastrad 								  requested_voltage * VOLTAGE_SCALE) == PPSMC_Result_OK) ?
   3814  1.1  riastrad 				0 : -EINVAL;
   3815  1.1  riastrad 		}
   3816  1.1  riastrad 	}
   3817  1.1  riastrad 
   3818  1.1  riastrad 	return -EINVAL;
   3819  1.1  riastrad }
   3820  1.1  riastrad 
   3821  1.1  riastrad static int ci_upload_dpm_level_enable_mask(struct radeon_device *rdev)
   3822  1.1  riastrad {
   3823  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3824  1.1  riastrad 	PPSMC_Result result;
   3825  1.1  riastrad 
   3826  1.1  riastrad 	ci_apply_disp_minimum_voltage_request(rdev);
   3827  1.1  riastrad 
   3828  1.1  riastrad 	if (!pi->sclk_dpm_key_disabled) {
   3829  1.1  riastrad 		if (pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
   3830  1.1  riastrad 			result = ci_send_msg_to_smc_with_parameter(rdev,
   3831  1.1  riastrad 								   PPSMC_MSG_SCLKDPM_SetEnabledMask,
   3832  1.1  riastrad 								   pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
   3833  1.1  riastrad 			if (result != PPSMC_Result_OK)
   3834  1.1  riastrad 				return -EINVAL;
   3835  1.1  riastrad 		}
   3836  1.1  riastrad 	}
   3837  1.1  riastrad 
   3838  1.1  riastrad 	if (!pi->mclk_dpm_key_disabled) {
   3839  1.1  riastrad 		if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
   3840  1.1  riastrad 			result = ci_send_msg_to_smc_with_parameter(rdev,
   3841  1.1  riastrad 								   PPSMC_MSG_MCLKDPM_SetEnabledMask,
   3842  1.1  riastrad 								   pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
   3843  1.1  riastrad 			if (result != PPSMC_Result_OK)
   3844  1.1  riastrad 				return -EINVAL;
   3845  1.1  riastrad 		}
   3846  1.1  riastrad 	}
   3847  1.1  riastrad #if 0
   3848  1.1  riastrad 	if (!pi->pcie_dpm_key_disabled) {
   3849  1.1  riastrad 		if (pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
   3850  1.1  riastrad 			result = ci_send_msg_to_smc_with_parameter(rdev,
   3851  1.1  riastrad 								   PPSMC_MSG_PCIeDPM_SetEnabledMask,
   3852  1.1  riastrad 								   pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
   3853  1.1  riastrad 			if (result != PPSMC_Result_OK)
   3854  1.1  riastrad 				return -EINVAL;
   3855  1.1  riastrad 		}
   3856  1.1  riastrad 	}
   3857  1.1  riastrad #endif
   3858  1.1  riastrad 	return 0;
   3859  1.1  riastrad }
   3860  1.1  riastrad 
   3861  1.1  riastrad static void ci_find_dpm_states_clocks_in_dpm_table(struct radeon_device *rdev,
   3862  1.1  riastrad 						   struct radeon_ps *radeon_state)
   3863  1.1  riastrad {
   3864  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3865  1.1  riastrad 	struct ci_ps *state = ci_get_ps(radeon_state);
   3866  1.1  riastrad 	struct ci_single_dpm_table *sclk_table = &pi->dpm_table.sclk_table;
   3867  1.1  riastrad 	u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
   3868  1.1  riastrad 	struct ci_single_dpm_table *mclk_table = &pi->dpm_table.mclk_table;
   3869  1.1  riastrad 	u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
   3870  1.1  riastrad 	u32 i;
   3871  1.1  riastrad 
   3872  1.1  riastrad 	pi->need_update_smu7_dpm_table = 0;
   3873  1.1  riastrad 
   3874  1.1  riastrad 	for (i = 0; i < sclk_table->count; i++) {
   3875  1.1  riastrad 		if (sclk == sclk_table->dpm_levels[i].value)
   3876  1.1  riastrad 			break;
   3877  1.1  riastrad 	}
   3878  1.1  riastrad 
   3879  1.1  riastrad 	if (i >= sclk_table->count) {
   3880  1.1  riastrad 		pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
   3881  1.1  riastrad 	} else {
   3882  1.4  riastrad 		/* XXX The current code always reprogrammed the sclk levels,
   3883  1.4  riastrad 		 * but we don't currently handle disp sclk requirements
   3884  1.4  riastrad 		 * so just skip it.
   3885  1.4  riastrad 		 */
   3886  1.1  riastrad 		if (CISLAND_MINIMUM_ENGINE_CLOCK != CISLAND_MINIMUM_ENGINE_CLOCK)
   3887  1.1  riastrad 			pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
   3888  1.1  riastrad 	}
   3889  1.1  riastrad 
   3890  1.1  riastrad 	for (i = 0; i < mclk_table->count; i++) {
   3891  1.1  riastrad 		if (mclk == mclk_table->dpm_levels[i].value)
   3892  1.1  riastrad 			break;
   3893  1.1  riastrad 	}
   3894  1.1  riastrad 
   3895  1.1  riastrad 	if (i >= mclk_table->count)
   3896  1.1  riastrad 		pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
   3897  1.1  riastrad 
   3898  1.1  riastrad 	if (rdev->pm.dpm.current_active_crtc_count !=
   3899  1.1  riastrad 	    rdev->pm.dpm.new_active_crtc_count)
   3900  1.1  riastrad 		pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
   3901  1.1  riastrad }
   3902  1.1  riastrad 
   3903  1.1  riastrad static int ci_populate_and_upload_sclk_mclk_dpm_levels(struct radeon_device *rdev,
   3904  1.1  riastrad 						       struct radeon_ps *radeon_state)
   3905  1.1  riastrad {
   3906  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3907  1.1  riastrad 	struct ci_ps *state = ci_get_ps(radeon_state);
   3908  1.1  riastrad 	u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
   3909  1.1  riastrad 	u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
   3910  1.1  riastrad 	struct ci_dpm_table *dpm_table = &pi->dpm_table;
   3911  1.1  riastrad 	int ret;
   3912  1.1  riastrad 
   3913  1.1  riastrad 	if (!pi->need_update_smu7_dpm_table)
   3914  1.1  riastrad 		return 0;
   3915  1.1  riastrad 
   3916  1.1  riastrad 	if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK)
   3917  1.1  riastrad 		dpm_table->sclk_table.dpm_levels[dpm_table->sclk_table.count-1].value = sclk;
   3918  1.1  riastrad 
   3919  1.1  riastrad 	if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)
   3920  1.1  riastrad 		dpm_table->mclk_table.dpm_levels[dpm_table->mclk_table.count-1].value = mclk;
   3921  1.1  riastrad 
   3922  1.1  riastrad 	if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK)) {
   3923  1.1  riastrad 		ret = ci_populate_all_graphic_levels(rdev);
   3924  1.1  riastrad 		if (ret)
   3925  1.1  riastrad 			return ret;
   3926  1.1  riastrad 	}
   3927  1.1  riastrad 
   3928  1.1  riastrad 	if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_MCLK | DPMTABLE_UPDATE_MCLK)) {
   3929  1.1  riastrad 		ret = ci_populate_all_memory_levels(rdev);
   3930  1.1  riastrad 		if (ret)
   3931  1.1  riastrad 			return ret;
   3932  1.1  riastrad 	}
   3933  1.1  riastrad 
   3934  1.1  riastrad 	return 0;
   3935  1.1  riastrad }
   3936  1.1  riastrad 
   3937  1.1  riastrad static int ci_enable_uvd_dpm(struct radeon_device *rdev, bool enable)
   3938  1.1  riastrad {
   3939  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3940  1.1  riastrad 	const struct radeon_clock_and_voltage_limits *max_limits;
   3941  1.1  riastrad 	int i;
   3942  1.1  riastrad 
   3943  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
   3944  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   3945  1.1  riastrad 	else
   3946  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
   3947  1.1  riastrad 
   3948  1.1  riastrad 	if (enable) {
   3949  1.1  riastrad 		pi->dpm_level_enable_mask.uvd_dpm_enable_mask = 0;
   3950  1.1  riastrad 
   3951  1.1  riastrad 		for (i = rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
   3952  1.1  riastrad 			if (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
   3953  1.1  riastrad 				pi->dpm_level_enable_mask.uvd_dpm_enable_mask |= 1 << i;
   3954  1.1  riastrad 
   3955  1.1  riastrad 				if (!pi->caps_uvd_dpm)
   3956  1.1  riastrad 					break;
   3957  1.1  riastrad 			}
   3958  1.1  riastrad 		}
   3959  1.1  riastrad 
   3960  1.1  riastrad 		ci_send_msg_to_smc_with_parameter(rdev,
   3961  1.1  riastrad 						  PPSMC_MSG_UVDDPM_SetEnabledMask,
   3962  1.1  riastrad 						  pi->dpm_level_enable_mask.uvd_dpm_enable_mask);
   3963  1.1  riastrad 
   3964  1.1  riastrad 		if (pi->last_mclk_dpm_enable_mask & 0x1) {
   3965  1.1  riastrad 			pi->uvd_enabled = true;
   3966  1.1  riastrad 			pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
   3967  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev,
   3968  1.1  riastrad 							  PPSMC_MSG_MCLKDPM_SetEnabledMask,
   3969  1.1  riastrad 							  pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
   3970  1.1  riastrad 		}
   3971  1.1  riastrad 	} else {
   3972  1.1  riastrad 		if (pi->last_mclk_dpm_enable_mask & 0x1) {
   3973  1.1  riastrad 			pi->uvd_enabled = false;
   3974  1.1  riastrad 			pi->dpm_level_enable_mask.mclk_dpm_enable_mask |= 1;
   3975  1.1  riastrad 			ci_send_msg_to_smc_with_parameter(rdev,
   3976  1.1  riastrad 							  PPSMC_MSG_MCLKDPM_SetEnabledMask,
   3977  1.1  riastrad 							  pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
   3978  1.1  riastrad 		}
   3979  1.1  riastrad 	}
   3980  1.1  riastrad 
   3981  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, enable ?
   3982  1.1  riastrad 				   PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable) == PPSMC_Result_OK) ?
   3983  1.1  riastrad 		0 : -EINVAL;
   3984  1.1  riastrad }
   3985  1.1  riastrad 
   3986  1.1  riastrad static int ci_enable_vce_dpm(struct radeon_device *rdev, bool enable)
   3987  1.1  riastrad {
   3988  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   3989  1.1  riastrad 	const struct radeon_clock_and_voltage_limits *max_limits;
   3990  1.1  riastrad 	int i;
   3991  1.1  riastrad 
   3992  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
   3993  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   3994  1.1  riastrad 	else
   3995  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
   3996  1.1  riastrad 
   3997  1.1  riastrad 	if (enable) {
   3998  1.1  riastrad 		pi->dpm_level_enable_mask.vce_dpm_enable_mask = 0;
   3999  1.1  riastrad 		for (i = rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
   4000  1.1  riastrad 			if (rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
   4001  1.1  riastrad 				pi->dpm_level_enable_mask.vce_dpm_enable_mask |= 1 << i;
   4002  1.1  riastrad 
   4003  1.1  riastrad 				if (!pi->caps_vce_dpm)
   4004  1.1  riastrad 					break;
   4005  1.1  riastrad 			}
   4006  1.1  riastrad 		}
   4007  1.1  riastrad 
   4008  1.1  riastrad 		ci_send_msg_to_smc_with_parameter(rdev,
   4009  1.1  riastrad 						  PPSMC_MSG_VCEDPM_SetEnabledMask,
   4010  1.1  riastrad 						  pi->dpm_level_enable_mask.vce_dpm_enable_mask);
   4011  1.1  riastrad 	}
   4012  1.1  riastrad 
   4013  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, enable ?
   4014  1.1  riastrad 				   PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable) == PPSMC_Result_OK) ?
   4015  1.1  riastrad 		0 : -EINVAL;
   4016  1.1  riastrad }
   4017  1.1  riastrad 
   4018  1.1  riastrad #if 0
   4019  1.1  riastrad static int ci_enable_samu_dpm(struct radeon_device *rdev, bool enable)
   4020  1.1  riastrad {
   4021  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4022  1.1  riastrad 	const struct radeon_clock_and_voltage_limits *max_limits;
   4023  1.1  riastrad 	int i;
   4024  1.1  riastrad 
   4025  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
   4026  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   4027  1.1  riastrad 	else
   4028  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
   4029  1.1  riastrad 
   4030  1.1  riastrad 	if (enable) {
   4031  1.1  riastrad 		pi->dpm_level_enable_mask.samu_dpm_enable_mask = 0;
   4032  1.1  riastrad 		for (i = rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
   4033  1.1  riastrad 			if (rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
   4034  1.1  riastrad 				pi->dpm_level_enable_mask.samu_dpm_enable_mask |= 1 << i;
   4035  1.1  riastrad 
   4036  1.1  riastrad 				if (!pi->caps_samu_dpm)
   4037  1.1  riastrad 					break;
   4038  1.1  riastrad 			}
   4039  1.1  riastrad 		}
   4040  1.1  riastrad 
   4041  1.1  riastrad 		ci_send_msg_to_smc_with_parameter(rdev,
   4042  1.1  riastrad 						  PPSMC_MSG_SAMUDPM_SetEnabledMask,
   4043  1.1  riastrad 						  pi->dpm_level_enable_mask.samu_dpm_enable_mask);
   4044  1.1  riastrad 	}
   4045  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, enable ?
   4046  1.1  riastrad 				   PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable) == PPSMC_Result_OK) ?
   4047  1.1  riastrad 		0 : -EINVAL;
   4048  1.1  riastrad }
   4049  1.1  riastrad 
   4050  1.1  riastrad static int ci_enable_acp_dpm(struct radeon_device *rdev, bool enable)
   4051  1.1  riastrad {
   4052  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4053  1.1  riastrad 	const struct radeon_clock_and_voltage_limits *max_limits;
   4054  1.1  riastrad 	int i;
   4055  1.1  riastrad 
   4056  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
   4057  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   4058  1.1  riastrad 	else
   4059  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
   4060  1.1  riastrad 
   4061  1.1  riastrad 	if (enable) {
   4062  1.1  riastrad 		pi->dpm_level_enable_mask.acp_dpm_enable_mask = 0;
   4063  1.1  riastrad 		for (i = rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
   4064  1.1  riastrad 			if (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
   4065  1.1  riastrad 				pi->dpm_level_enable_mask.acp_dpm_enable_mask |= 1 << i;
   4066  1.1  riastrad 
   4067  1.1  riastrad 				if (!pi->caps_acp_dpm)
   4068  1.1  riastrad 					break;
   4069  1.1  riastrad 			}
   4070  1.1  riastrad 		}
   4071  1.1  riastrad 
   4072  1.1  riastrad 		ci_send_msg_to_smc_with_parameter(rdev,
   4073  1.1  riastrad 						  PPSMC_MSG_ACPDPM_SetEnabledMask,
   4074  1.1  riastrad 						  pi->dpm_level_enable_mask.acp_dpm_enable_mask);
   4075  1.1  riastrad 	}
   4076  1.1  riastrad 
   4077  1.1  riastrad 	return (ci_send_msg_to_smc(rdev, enable ?
   4078  1.1  riastrad 				   PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable) == PPSMC_Result_OK) ?
   4079  1.1  riastrad 		0 : -EINVAL;
   4080  1.1  riastrad }
   4081  1.1  riastrad #endif
   4082  1.1  riastrad 
   4083  1.1  riastrad static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate)
   4084  1.1  riastrad {
   4085  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4086  1.1  riastrad 	u32 tmp;
   4087  1.1  riastrad 
   4088  1.1  riastrad 	if (!gate) {
   4089  1.1  riastrad 		if (pi->caps_uvd_dpm ||
   4090  1.1  riastrad 		    (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count <= 0))
   4091  1.1  riastrad 			pi->smc_state_table.UvdBootLevel = 0;
   4092  1.1  riastrad 		else
   4093  1.1  riastrad 			pi->smc_state_table.UvdBootLevel =
   4094  1.1  riastrad 				rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1;
   4095  1.1  riastrad 
   4096  1.1  riastrad 		tmp = RREG32_SMC(DPM_TABLE_475);
   4097  1.1  riastrad 		tmp &= ~UvdBootLevel_MASK;
   4098  1.1  riastrad 		tmp |= UvdBootLevel(pi->smc_state_table.UvdBootLevel);
   4099  1.1  riastrad 		WREG32_SMC(DPM_TABLE_475, tmp);
   4100  1.1  riastrad 	}
   4101  1.1  riastrad 
   4102  1.1  riastrad 	return ci_enable_uvd_dpm(rdev, !gate);
   4103  1.1  riastrad }
   4104  1.1  riastrad 
   4105  1.1  riastrad static u8 ci_get_vce_boot_level(struct radeon_device *rdev)
   4106  1.1  riastrad {
   4107  1.1  riastrad 	u8 i;
   4108  1.1  riastrad 	u32 min_evclk = 30000; /* ??? */
   4109  1.1  riastrad 	struct radeon_vce_clock_voltage_dependency_table *table =
   4110  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
   4111  1.1  riastrad 
   4112  1.1  riastrad 	for (i = 0; i < table->count; i++) {
   4113  1.1  riastrad 		if (table->entries[i].evclk >= min_evclk)
   4114  1.1  riastrad 			return i;
   4115  1.1  riastrad 	}
   4116  1.1  riastrad 
   4117  1.1  riastrad 	return table->count - 1;
   4118  1.1  riastrad }
   4119  1.1  riastrad 
   4120  1.1  riastrad static int ci_update_vce_dpm(struct radeon_device *rdev,
   4121  1.1  riastrad 			     struct radeon_ps *radeon_new_state,
   4122  1.1  riastrad 			     struct radeon_ps *radeon_current_state)
   4123  1.1  riastrad {
   4124  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4125  1.1  riastrad 	int ret = 0;
   4126  1.1  riastrad 	u32 tmp;
   4127  1.1  riastrad 
   4128  1.1  riastrad 	if (radeon_current_state->evclk != radeon_new_state->evclk) {
   4129  1.1  riastrad 		if (radeon_new_state->evclk) {
   4130  1.1  riastrad 			/* turn the clocks on when encoding */
   4131  1.1  riastrad 			cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, false);
   4132  1.1  riastrad 
   4133  1.1  riastrad 			pi->smc_state_table.VceBootLevel = ci_get_vce_boot_level(rdev);
   4134  1.1  riastrad 			tmp = RREG32_SMC(DPM_TABLE_475);
   4135  1.1  riastrad 			tmp &= ~VceBootLevel_MASK;
   4136  1.1  riastrad 			tmp |= VceBootLevel(pi->smc_state_table.VceBootLevel);
   4137  1.1  riastrad 			WREG32_SMC(DPM_TABLE_475, tmp);
   4138  1.1  riastrad 
   4139  1.1  riastrad 			ret = ci_enable_vce_dpm(rdev, true);
   4140  1.1  riastrad 		} else {
   4141  1.1  riastrad 			/* turn the clocks off when not encoding */
   4142  1.1  riastrad 			cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, true);
   4143  1.1  riastrad 
   4144  1.1  riastrad 			ret = ci_enable_vce_dpm(rdev, false);
   4145  1.1  riastrad 		}
   4146  1.1  riastrad 	}
   4147  1.1  riastrad 	return ret;
   4148  1.1  riastrad }
   4149  1.1  riastrad 
   4150  1.1  riastrad #if 0
   4151  1.1  riastrad static int ci_update_samu_dpm(struct radeon_device *rdev, bool gate)
   4152  1.1  riastrad {
   4153  1.1  riastrad 	return ci_enable_samu_dpm(rdev, gate);
   4154  1.1  riastrad }
   4155  1.1  riastrad 
   4156  1.1  riastrad static int ci_update_acp_dpm(struct radeon_device *rdev, bool gate)
   4157  1.1  riastrad {
   4158  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4159  1.1  riastrad 	u32 tmp;
   4160  1.1  riastrad 
   4161  1.1  riastrad 	if (!gate) {
   4162  1.1  riastrad 		pi->smc_state_table.AcpBootLevel = 0;
   4163  1.1  riastrad 
   4164  1.1  riastrad 		tmp = RREG32_SMC(DPM_TABLE_475);
   4165  1.1  riastrad 		tmp &= ~AcpBootLevel_MASK;
   4166  1.1  riastrad 		tmp |= AcpBootLevel(pi->smc_state_table.AcpBootLevel);
   4167  1.1  riastrad 		WREG32_SMC(DPM_TABLE_475, tmp);
   4168  1.1  riastrad 	}
   4169  1.1  riastrad 
   4170  1.1  riastrad 	return ci_enable_acp_dpm(rdev, !gate);
   4171  1.1  riastrad }
   4172  1.1  riastrad #endif
   4173  1.1  riastrad 
   4174  1.1  riastrad static int ci_generate_dpm_level_enable_mask(struct radeon_device *rdev,
   4175  1.1  riastrad 					     struct radeon_ps *radeon_state)
   4176  1.1  riastrad {
   4177  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4178  1.1  riastrad 	int ret;
   4179  1.1  riastrad 
   4180  1.1  riastrad 	ret = ci_trim_dpm_states(rdev, radeon_state);
   4181  1.1  riastrad 	if (ret)
   4182  1.1  riastrad 		return ret;
   4183  1.1  riastrad 
   4184  1.1  riastrad 	pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
   4185  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&pi->dpm_table.sclk_table);
   4186  1.1  riastrad 	pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
   4187  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&pi->dpm_table.mclk_table);
   4188  1.1  riastrad 	pi->last_mclk_dpm_enable_mask =
   4189  1.1  riastrad 		pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
   4190  1.1  riastrad 	if (pi->uvd_enabled) {
   4191  1.1  riastrad 		if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask & 1)
   4192  1.1  riastrad 			pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
   4193  1.1  riastrad 	}
   4194  1.1  riastrad 	pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
   4195  1.1  riastrad 		ci_get_dpm_level_enable_mask_value(&pi->dpm_table.pcie_speed_table);
   4196  1.1  riastrad 
   4197  1.1  riastrad 	return 0;
   4198  1.1  riastrad }
   4199  1.1  riastrad 
   4200  1.1  riastrad static u32 ci_get_lowest_enabled_level(struct radeon_device *rdev,
   4201  1.1  riastrad 				       u32 level_mask)
   4202  1.1  riastrad {
   4203  1.1  riastrad 	u32 level = 0;
   4204  1.1  riastrad 
   4205  1.1  riastrad 	while ((level_mask & (1 << level)) == 0)
   4206  1.1  riastrad 		level++;
   4207  1.1  riastrad 
   4208  1.1  riastrad 	return level;
   4209  1.1  riastrad }
   4210  1.1  riastrad 
   4211  1.1  riastrad 
   4212  1.1  riastrad int ci_dpm_force_performance_level(struct radeon_device *rdev,
   4213  1.1  riastrad 				   enum radeon_dpm_forced_level level)
   4214  1.1  riastrad {
   4215  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4216  1.1  riastrad 	u32 tmp, levels, i;
   4217  1.1  riastrad 	int ret;
   4218  1.1  riastrad 
   4219  1.1  riastrad 	if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
   4220  1.1  riastrad 		if ((!pi->pcie_dpm_key_disabled) &&
   4221  1.1  riastrad 		    pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
   4222  1.1  riastrad 			levels = 0;
   4223  1.1  riastrad 			tmp = pi->dpm_level_enable_mask.pcie_dpm_enable_mask;
   4224  1.1  riastrad 			while (tmp >>= 1)
   4225  1.1  riastrad 				levels++;
   4226  1.1  riastrad 			if (levels) {
   4227  1.1  riastrad 				ret = ci_dpm_force_state_pcie(rdev, level);
   4228  1.1  riastrad 				if (ret)
   4229  1.1  riastrad 					return ret;
   4230  1.1  riastrad 				for (i = 0; i < rdev->usec_timeout; i++) {
   4231  1.1  riastrad 					tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
   4232  1.1  riastrad 					       CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
   4233  1.1  riastrad 					if (tmp == levels)
   4234  1.1  riastrad 						break;
   4235  1.1  riastrad 					udelay(1);
   4236  1.1  riastrad 				}
   4237  1.1  riastrad 			}
   4238  1.1  riastrad 		}
   4239  1.1  riastrad 		if ((!pi->sclk_dpm_key_disabled) &&
   4240  1.1  riastrad 		    pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
   4241  1.1  riastrad 			levels = 0;
   4242  1.1  riastrad 			tmp = pi->dpm_level_enable_mask.sclk_dpm_enable_mask;
   4243  1.1  riastrad 			while (tmp >>= 1)
   4244  1.1  riastrad 				levels++;
   4245  1.1  riastrad 			if (levels) {
   4246  1.1  riastrad 				ret = ci_dpm_force_state_sclk(rdev, levels);
   4247  1.1  riastrad 				if (ret)
   4248  1.1  riastrad 					return ret;
   4249  1.1  riastrad 				for (i = 0; i < rdev->usec_timeout; i++) {
   4250  1.1  riastrad 					tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
   4251  1.1  riastrad 					       CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
   4252  1.1  riastrad 					if (tmp == levels)
   4253  1.1  riastrad 						break;
   4254  1.1  riastrad 					udelay(1);
   4255  1.1  riastrad 				}
   4256  1.1  riastrad 			}
   4257  1.1  riastrad 		}
   4258  1.1  riastrad 		if ((!pi->mclk_dpm_key_disabled) &&
   4259  1.1  riastrad 		    pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
   4260  1.1  riastrad 			levels = 0;
   4261  1.1  riastrad 			tmp = pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
   4262  1.1  riastrad 			while (tmp >>= 1)
   4263  1.1  riastrad 				levels++;
   4264  1.1  riastrad 			if (levels) {
   4265  1.1  riastrad 				ret = ci_dpm_force_state_mclk(rdev, levels);
   4266  1.1  riastrad 				if (ret)
   4267  1.1  riastrad 					return ret;
   4268  1.1  riastrad 				for (i = 0; i < rdev->usec_timeout; i++) {
   4269  1.1  riastrad 					tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
   4270  1.1  riastrad 					       CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
   4271  1.1  riastrad 					if (tmp == levels)
   4272  1.1  riastrad 						break;
   4273  1.1  riastrad 					udelay(1);
   4274  1.1  riastrad 				}
   4275  1.1  riastrad 			}
   4276  1.1  riastrad 		}
   4277  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
   4278  1.1  riastrad 		if ((!pi->sclk_dpm_key_disabled) &&
   4279  1.1  riastrad 		    pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
   4280  1.1  riastrad 			levels = ci_get_lowest_enabled_level(rdev,
   4281  1.1  riastrad 							     pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
   4282  1.1  riastrad 			ret = ci_dpm_force_state_sclk(rdev, levels);
   4283  1.1  riastrad 			if (ret)
   4284  1.1  riastrad 				return ret;
   4285  1.1  riastrad 			for (i = 0; i < rdev->usec_timeout; i++) {
   4286  1.1  riastrad 				tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
   4287  1.1  riastrad 				       CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
   4288  1.1  riastrad 				if (tmp == levels)
   4289  1.1  riastrad 					break;
   4290  1.1  riastrad 				udelay(1);
   4291  1.1  riastrad 			}
   4292  1.1  riastrad 		}
   4293  1.1  riastrad 		if ((!pi->mclk_dpm_key_disabled) &&
   4294  1.1  riastrad 		    pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
   4295  1.1  riastrad 			levels = ci_get_lowest_enabled_level(rdev,
   4296  1.1  riastrad 							     pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
   4297  1.1  riastrad 			ret = ci_dpm_force_state_mclk(rdev, levels);
   4298  1.1  riastrad 			if (ret)
   4299  1.1  riastrad 				return ret;
   4300  1.1  riastrad 			for (i = 0; i < rdev->usec_timeout; i++) {
   4301  1.1  riastrad 				tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
   4302  1.1  riastrad 				       CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
   4303  1.1  riastrad 				if (tmp == levels)
   4304  1.1  riastrad 					break;
   4305  1.1  riastrad 				udelay(1);
   4306  1.1  riastrad 			}
   4307  1.1  riastrad 		}
   4308  1.1  riastrad 		if ((!pi->pcie_dpm_key_disabled) &&
   4309  1.1  riastrad 		    pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
   4310  1.1  riastrad 			levels = ci_get_lowest_enabled_level(rdev,
   4311  1.1  riastrad 							     pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
   4312  1.1  riastrad 			ret = ci_dpm_force_state_pcie(rdev, levels);
   4313  1.1  riastrad 			if (ret)
   4314  1.1  riastrad 				return ret;
   4315  1.1  riastrad 			for (i = 0; i < rdev->usec_timeout; i++) {
   4316  1.1  riastrad 				tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
   4317  1.1  riastrad 				       CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
   4318  1.1  riastrad 				if (tmp == levels)
   4319  1.1  riastrad 					break;
   4320  1.1  riastrad 				udelay(1);
   4321  1.1  riastrad 			}
   4322  1.1  riastrad 		}
   4323  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
   4324  1.1  riastrad 		if (!pi->pcie_dpm_key_disabled) {
   4325  1.1  riastrad 			PPSMC_Result smc_result;
   4326  1.1  riastrad 
   4327  1.1  riastrad 			smc_result = ci_send_msg_to_smc(rdev,
   4328  1.1  riastrad 							PPSMC_MSG_PCIeDPM_UnForceLevel);
   4329  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   4330  1.1  riastrad 				return -EINVAL;
   4331  1.1  riastrad 		}
   4332  1.1  riastrad 		ret = ci_upload_dpm_level_enable_mask(rdev);
   4333  1.1  riastrad 		if (ret)
   4334  1.1  riastrad 			return ret;
   4335  1.1  riastrad 	}
   4336  1.1  riastrad 
   4337  1.1  riastrad 	rdev->pm.dpm.forced_level = level;
   4338  1.1  riastrad 
   4339  1.1  riastrad 	return 0;
   4340  1.1  riastrad }
   4341  1.1  riastrad 
   4342  1.1  riastrad static int ci_set_mc_special_registers(struct radeon_device *rdev,
   4343  1.1  riastrad 				       struct ci_mc_reg_table *table)
   4344  1.1  riastrad {
   4345  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4346  1.1  riastrad 	u8 i, j, k;
   4347  1.1  riastrad 	u32 temp_reg;
   4348  1.1  riastrad 
   4349  1.1  riastrad 	for (i = 0, j = table->last; i < table->last; i++) {
   4350  1.1  riastrad 		if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4351  1.1  riastrad 			return -EINVAL;
   4352  1.1  riastrad 		switch(table->mc_reg_address[i].s1 << 2) {
   4353  1.1  riastrad 		case MC_SEQ_MISC1:
   4354  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_EMRS);
   4355  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_EMRS >> 2;
   4356  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
   4357  1.1  riastrad 			for (k = 0; k < table->num_entries; k++) {
   4358  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   4359  1.1  riastrad 					((temp_reg & 0xffff0000)) | ((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
   4360  1.1  riastrad 			}
   4361  1.1  riastrad 			j++;
   4362  1.1  riastrad 			if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4363  1.1  riastrad 				return -EINVAL;
   4364  1.1  riastrad 
   4365  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_MRS);
   4366  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS >> 2;
   4367  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS_LP >> 2;
   4368  1.1  riastrad 			for (k = 0; k < table->num_entries; k++) {
   4369  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   4370  1.1  riastrad 					(temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
   4371  1.1  riastrad 				if (!pi->mem_gddr5)
   4372  1.1  riastrad 					table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
   4373  1.1  riastrad 			}
   4374  1.1  riastrad 			j++;
   4375  1.1  riastrad 			if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4376  1.1  riastrad 				return -EINVAL;
   4377  1.1  riastrad 
   4378  1.1  riastrad 			if (!pi->mem_gddr5) {
   4379  1.1  riastrad 				table->mc_reg_address[j].s1 = MC_PMG_AUTO_CMD >> 2;
   4380  1.1  riastrad 				table->mc_reg_address[j].s0 = MC_PMG_AUTO_CMD >> 2;
   4381  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4382  1.1  riastrad 					table->mc_reg_table_entry[k].mc_data[j] =
   4383  1.1  riastrad 						(table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16;
   4384  1.1  riastrad 				}
   4385  1.1  riastrad 				j++;
   4386  1.1  riastrad 				if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4387  1.1  riastrad 					return -EINVAL;
   4388  1.1  riastrad 			}
   4389  1.1  riastrad 			break;
   4390  1.1  riastrad 		case MC_SEQ_RESERVE_M:
   4391  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_MRS1);
   4392  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS1 >> 2;
   4393  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
   4394  1.1  riastrad 			for (k = 0; k < table->num_entries; k++) {
   4395  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   4396  1.1  riastrad 					(temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
   4397  1.1  riastrad 			}
   4398  1.1  riastrad 			j++;
   4399  1.1  riastrad 			if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4400  1.1  riastrad 				return -EINVAL;
   4401  1.1  riastrad 			break;
   4402  1.1  riastrad 		default:
   4403  1.1  riastrad 			break;
   4404  1.1  riastrad 		}
   4405  1.1  riastrad 
   4406  1.1  riastrad 	}
   4407  1.1  riastrad 
   4408  1.1  riastrad 	table->last = j;
   4409  1.1  riastrad 
   4410  1.1  riastrad 	return 0;
   4411  1.1  riastrad }
   4412  1.1  riastrad 
   4413  1.1  riastrad static bool ci_check_s0_mc_reg_index(u16 in_reg, u16 *out_reg)
   4414  1.1  riastrad {
   4415  1.1  riastrad 	bool result = true;
   4416  1.1  riastrad 
   4417  1.1  riastrad 	switch(in_reg) {
   4418  1.1  riastrad 	case MC_SEQ_RAS_TIMING >> 2:
   4419  1.1  riastrad 		*out_reg = MC_SEQ_RAS_TIMING_LP >> 2;
   4420  1.1  riastrad 		break;
   4421  1.1  riastrad 	case MC_SEQ_DLL_STBY >> 2:
   4422  1.1  riastrad 		*out_reg = MC_SEQ_DLL_STBY_LP >> 2;
   4423  1.1  riastrad 		break;
   4424  1.1  riastrad 	case MC_SEQ_G5PDX_CMD0 >> 2:
   4425  1.1  riastrad 		*out_reg = MC_SEQ_G5PDX_CMD0_LP >> 2;
   4426  1.1  riastrad 		break;
   4427  1.1  riastrad 	case MC_SEQ_G5PDX_CMD1 >> 2:
   4428  1.1  riastrad 		*out_reg = MC_SEQ_G5PDX_CMD1_LP >> 2;
   4429  1.1  riastrad 		break;
   4430  1.1  riastrad 	case MC_SEQ_G5PDX_CTRL >> 2:
   4431  1.1  riastrad 		*out_reg = MC_SEQ_G5PDX_CTRL_LP >> 2;
   4432  1.1  riastrad 		break;
   4433  1.1  riastrad 	case MC_SEQ_CAS_TIMING >> 2:
   4434  1.1  riastrad 		*out_reg = MC_SEQ_CAS_TIMING_LP >> 2;
   4435  1.4  riastrad 		break;
   4436  1.1  riastrad 	case MC_SEQ_MISC_TIMING >> 2:
   4437  1.1  riastrad 		*out_reg = MC_SEQ_MISC_TIMING_LP >> 2;
   4438  1.1  riastrad 		break;
   4439  1.1  riastrad 	case MC_SEQ_MISC_TIMING2 >> 2:
   4440  1.1  riastrad 		*out_reg = MC_SEQ_MISC_TIMING2_LP >> 2;
   4441  1.1  riastrad 		break;
   4442  1.1  riastrad 	case MC_SEQ_PMG_DVS_CMD >> 2:
   4443  1.1  riastrad 		*out_reg = MC_SEQ_PMG_DVS_CMD_LP >> 2;
   4444  1.1  riastrad 		break;
   4445  1.1  riastrad 	case MC_SEQ_PMG_DVS_CTL >> 2:
   4446  1.1  riastrad 		*out_reg = MC_SEQ_PMG_DVS_CTL_LP >> 2;
   4447  1.1  riastrad 		break;
   4448  1.1  riastrad 	case MC_SEQ_RD_CTL_D0 >> 2:
   4449  1.1  riastrad 		*out_reg = MC_SEQ_RD_CTL_D0_LP >> 2;
   4450  1.1  riastrad 		break;
   4451  1.1  riastrad 	case MC_SEQ_RD_CTL_D1 >> 2:
   4452  1.1  riastrad 		*out_reg = MC_SEQ_RD_CTL_D1_LP >> 2;
   4453  1.1  riastrad 		break;
   4454  1.1  riastrad 	case MC_SEQ_WR_CTL_D0 >> 2:
   4455  1.1  riastrad 		*out_reg = MC_SEQ_WR_CTL_D0_LP >> 2;
   4456  1.1  riastrad 		break;
   4457  1.1  riastrad 	case MC_SEQ_WR_CTL_D1 >> 2:
   4458  1.1  riastrad 		*out_reg = MC_SEQ_WR_CTL_D1_LP >> 2;
   4459  1.1  riastrad 		break;
   4460  1.1  riastrad 	case MC_PMG_CMD_EMRS >> 2:
   4461  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
   4462  1.1  riastrad 		break;
   4463  1.1  riastrad 	case MC_PMG_CMD_MRS >> 2:
   4464  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS_LP >> 2;
   4465  1.1  riastrad 		break;
   4466  1.1  riastrad 	case MC_PMG_CMD_MRS1 >> 2:
   4467  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
   4468  1.1  riastrad 		break;
   4469  1.1  riastrad 	case MC_SEQ_PMG_TIMING >> 2:
   4470  1.1  riastrad 		*out_reg = MC_SEQ_PMG_TIMING_LP >> 2;
   4471  1.1  riastrad 		break;
   4472  1.1  riastrad 	case MC_PMG_CMD_MRS2 >> 2:
   4473  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS2_LP >> 2;
   4474  1.1  riastrad 		break;
   4475  1.1  riastrad 	case MC_SEQ_WR_CTL_2 >> 2:
   4476  1.1  riastrad 		*out_reg = MC_SEQ_WR_CTL_2_LP >> 2;
   4477  1.1  riastrad 		break;
   4478  1.1  riastrad 	default:
   4479  1.1  riastrad 		result = false;
   4480  1.1  riastrad 		break;
   4481  1.1  riastrad 	}
   4482  1.1  riastrad 
   4483  1.1  riastrad 	return result;
   4484  1.1  riastrad }
   4485  1.1  riastrad 
   4486  1.1  riastrad static void ci_set_valid_flag(struct ci_mc_reg_table *table)
   4487  1.1  riastrad {
   4488  1.1  riastrad 	u8 i, j;
   4489  1.1  riastrad 
   4490  1.1  riastrad 	for (i = 0; i < table->last; i++) {
   4491  1.1  riastrad 		for (j = 1; j < table->num_entries; j++) {
   4492  1.1  riastrad 			if (table->mc_reg_table_entry[j-1].mc_data[i] !=
   4493  1.1  riastrad 			    table->mc_reg_table_entry[j].mc_data[i]) {
   4494  1.1  riastrad 				table->valid_flag |= 1 << i;
   4495  1.1  riastrad 				break;
   4496  1.1  riastrad 			}
   4497  1.1  riastrad 		}
   4498  1.1  riastrad 	}
   4499  1.1  riastrad }
   4500  1.1  riastrad 
   4501  1.1  riastrad static void ci_set_s0_mc_reg_index(struct ci_mc_reg_table *table)
   4502  1.1  riastrad {
   4503  1.1  riastrad 	u32 i;
   4504  1.1  riastrad 	u16 address;
   4505  1.1  riastrad 
   4506  1.1  riastrad 	for (i = 0; i < table->last; i++) {
   4507  1.1  riastrad 		table->mc_reg_address[i].s0 =
   4508  1.1  riastrad 			ci_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address) ?
   4509  1.1  riastrad 			address : table->mc_reg_address[i].s1;
   4510  1.1  riastrad 	}
   4511  1.1  riastrad }
   4512  1.1  riastrad 
   4513  1.1  riastrad static int ci_copy_vbios_mc_reg_table(const struct atom_mc_reg_table *table,
   4514  1.1  riastrad 				      struct ci_mc_reg_table *ci_table)
   4515  1.1  riastrad {
   4516  1.1  riastrad 	u8 i, j;
   4517  1.1  riastrad 
   4518  1.1  riastrad 	if (table->last > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4519  1.1  riastrad 		return -EINVAL;
   4520  1.1  riastrad 	if (table->num_entries > MAX_AC_TIMING_ENTRIES)
   4521  1.1  riastrad 		return -EINVAL;
   4522  1.1  riastrad 
   4523  1.1  riastrad 	for (i = 0; i < table->last; i++)
   4524  1.1  riastrad 		ci_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
   4525  1.1  riastrad 
   4526  1.1  riastrad 	ci_table->last = table->last;
   4527  1.1  riastrad 
   4528  1.1  riastrad 	for (i = 0; i < table->num_entries; i++) {
   4529  1.1  riastrad 		ci_table->mc_reg_table_entry[i].mclk_max =
   4530  1.1  riastrad 			table->mc_reg_table_entry[i].mclk_max;
   4531  1.1  riastrad 		for (j = 0; j < table->last; j++)
   4532  1.1  riastrad 			ci_table->mc_reg_table_entry[i].mc_data[j] =
   4533  1.1  riastrad 				table->mc_reg_table_entry[i].mc_data[j];
   4534  1.1  riastrad 	}
   4535  1.1  riastrad 	ci_table->num_entries = table->num_entries;
   4536  1.1  riastrad 
   4537  1.1  riastrad 	return 0;
   4538  1.1  riastrad }
   4539  1.1  riastrad 
   4540  1.1  riastrad static int ci_register_patching_mc_seq(struct radeon_device *rdev,
   4541  1.1  riastrad 				       struct ci_mc_reg_table *table)
   4542  1.1  riastrad {
   4543  1.1  riastrad 	u8 i, k;
   4544  1.1  riastrad 	u32 tmp;
   4545  1.1  riastrad 	bool patch;
   4546  1.1  riastrad 
   4547  1.1  riastrad 	tmp = RREG32(MC_SEQ_MISC0);
   4548  1.1  riastrad 	patch = ((tmp & 0x0000f00) == 0x300) ? true : false;
   4549  1.1  riastrad 
   4550  1.1  riastrad 	if (patch &&
   4551  1.1  riastrad 	    ((rdev->pdev->device == 0x67B0) ||
   4552  1.1  riastrad 	     (rdev->pdev->device == 0x67B1))) {
   4553  1.1  riastrad 		for (i = 0; i < table->last; i++) {
   4554  1.1  riastrad 			if (table->last >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4555  1.1  riastrad 				return -EINVAL;
   4556  1.1  riastrad 			switch(table->mc_reg_address[i].s1 >> 2) {
   4557  1.1  riastrad 			case MC_SEQ_MISC1:
   4558  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4559  1.1  riastrad 					if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
   4560  1.1  riastrad 					    (table->mc_reg_table_entry[k].mclk_max == 137500))
   4561  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4562  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFF8) |
   4563  1.1  riastrad 							0x00000007;
   4564  1.1  riastrad 				}
   4565  1.1  riastrad 				break;
   4566  1.1  riastrad 			case MC_SEQ_WR_CTL_D0:
   4567  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4568  1.1  riastrad 					if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
   4569  1.1  riastrad 					    (table->mc_reg_table_entry[k].mclk_max == 137500))
   4570  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4571  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFFF0F00) |
   4572  1.1  riastrad 							0x0000D0DD;
   4573  1.1  riastrad 				}
   4574  1.1  riastrad 				break;
   4575  1.1  riastrad 			case MC_SEQ_WR_CTL_D1:
   4576  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4577  1.1  riastrad 					if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
   4578  1.1  riastrad 					    (table->mc_reg_table_entry[k].mclk_max == 137500))
   4579  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4580  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFFF0F00) |
   4581  1.1  riastrad 							0x0000D0DD;
   4582  1.1  riastrad 				}
   4583  1.1  riastrad 				break;
   4584  1.1  riastrad 			case MC_SEQ_WR_CTL_2:
   4585  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4586  1.1  riastrad 					if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
   4587  1.1  riastrad 					    (table->mc_reg_table_entry[k].mclk_max == 137500))
   4588  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] = 0;
   4589  1.1  riastrad 				}
   4590  1.1  riastrad 				break;
   4591  1.1  riastrad 			case MC_SEQ_CAS_TIMING:
   4592  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4593  1.1  riastrad 					if (table->mc_reg_table_entry[k].mclk_max == 125000)
   4594  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4595  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFE0FE0F) |
   4596  1.1  riastrad 							0x000C0140;
   4597  1.1  riastrad 					else if (table->mc_reg_table_entry[k].mclk_max == 137500)
   4598  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4599  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFE0FE0F) |
   4600  1.1  riastrad 							0x000C0150;
   4601  1.1  riastrad 				}
   4602  1.1  riastrad 				break;
   4603  1.1  riastrad 			case MC_SEQ_MISC_TIMING:
   4604  1.1  riastrad 				for (k = 0; k < table->num_entries; k++) {
   4605  1.1  riastrad 					if (table->mc_reg_table_entry[k].mclk_max == 125000)
   4606  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4607  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFE0) |
   4608  1.1  riastrad 							0x00000030;
   4609  1.1  riastrad 					else if (table->mc_reg_table_entry[k].mclk_max == 137500)
   4610  1.1  riastrad 						table->mc_reg_table_entry[k].mc_data[i] =
   4611  1.1  riastrad 							(table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFE0) |
   4612  1.1  riastrad 							0x00000035;
   4613  1.1  riastrad 				}
   4614  1.1  riastrad 				break;
   4615  1.1  riastrad 			default:
   4616  1.1  riastrad 				break;
   4617  1.1  riastrad 			}
   4618  1.1  riastrad 		}
   4619  1.1  riastrad 
   4620  1.1  riastrad 		WREG32(MC_SEQ_IO_DEBUG_INDEX, 3);
   4621  1.1  riastrad 		tmp = RREG32(MC_SEQ_IO_DEBUG_DATA);
   4622  1.1  riastrad 		tmp = (tmp & 0xFFF8FFFF) | (1 << 16);
   4623  1.1  riastrad 		WREG32(MC_SEQ_IO_DEBUG_INDEX, 3);
   4624  1.1  riastrad 		WREG32(MC_SEQ_IO_DEBUG_DATA, tmp);
   4625  1.1  riastrad 	}
   4626  1.1  riastrad 
   4627  1.1  riastrad 	return 0;
   4628  1.1  riastrad }
   4629  1.1  riastrad 
   4630  1.1  riastrad static int ci_initialize_mc_reg_table(struct radeon_device *rdev)
   4631  1.1  riastrad {
   4632  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4633  1.1  riastrad 	struct atom_mc_reg_table *table;
   4634  1.1  riastrad 	struct ci_mc_reg_table *ci_table = &pi->mc_reg_table;
   4635  1.1  riastrad 	u8 module_index = rv770_get_memory_module_index(rdev);
   4636  1.1  riastrad 	int ret;
   4637  1.1  riastrad 
   4638  1.1  riastrad 	table = kzalloc(sizeof(struct atom_mc_reg_table), GFP_KERNEL);
   4639  1.1  riastrad 	if (!table)
   4640  1.1  riastrad 		return -ENOMEM;
   4641  1.1  riastrad 
   4642  1.1  riastrad 	WREG32(MC_SEQ_RAS_TIMING_LP, RREG32(MC_SEQ_RAS_TIMING));
   4643  1.1  riastrad 	WREG32(MC_SEQ_CAS_TIMING_LP, RREG32(MC_SEQ_CAS_TIMING));
   4644  1.1  riastrad 	WREG32(MC_SEQ_DLL_STBY_LP, RREG32(MC_SEQ_DLL_STBY));
   4645  1.1  riastrad 	WREG32(MC_SEQ_G5PDX_CMD0_LP, RREG32(MC_SEQ_G5PDX_CMD0));
   4646  1.1  riastrad 	WREG32(MC_SEQ_G5PDX_CMD1_LP, RREG32(MC_SEQ_G5PDX_CMD1));
   4647  1.1  riastrad 	WREG32(MC_SEQ_G5PDX_CTRL_LP, RREG32(MC_SEQ_G5PDX_CTRL));
   4648  1.1  riastrad 	WREG32(MC_SEQ_PMG_DVS_CMD_LP, RREG32(MC_SEQ_PMG_DVS_CMD));
   4649  1.1  riastrad 	WREG32(MC_SEQ_PMG_DVS_CTL_LP, RREG32(MC_SEQ_PMG_DVS_CTL));
   4650  1.1  riastrad 	WREG32(MC_SEQ_MISC_TIMING_LP, RREG32(MC_SEQ_MISC_TIMING));
   4651  1.1  riastrad 	WREG32(MC_SEQ_MISC_TIMING2_LP, RREG32(MC_SEQ_MISC_TIMING2));
   4652  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_EMRS_LP, RREG32(MC_PMG_CMD_EMRS));
   4653  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS_LP, RREG32(MC_PMG_CMD_MRS));
   4654  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS1_LP, RREG32(MC_PMG_CMD_MRS1));
   4655  1.1  riastrad 	WREG32(MC_SEQ_WR_CTL_D0_LP, RREG32(MC_SEQ_WR_CTL_D0));
   4656  1.1  riastrad 	WREG32(MC_SEQ_WR_CTL_D1_LP, RREG32(MC_SEQ_WR_CTL_D1));
   4657  1.1  riastrad 	WREG32(MC_SEQ_RD_CTL_D0_LP, RREG32(MC_SEQ_RD_CTL_D0));
   4658  1.1  riastrad 	WREG32(MC_SEQ_RD_CTL_D1_LP, RREG32(MC_SEQ_RD_CTL_D1));
   4659  1.1  riastrad 	WREG32(MC_SEQ_PMG_TIMING_LP, RREG32(MC_SEQ_PMG_TIMING));
   4660  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS2_LP, RREG32(MC_PMG_CMD_MRS2));
   4661  1.1  riastrad 	WREG32(MC_SEQ_WR_CTL_2_LP, RREG32(MC_SEQ_WR_CTL_2));
   4662  1.1  riastrad 
   4663  1.1  riastrad 	ret = radeon_atom_init_mc_reg_table(rdev, module_index, table);
   4664  1.1  riastrad 	if (ret)
   4665  1.1  riastrad 		goto init_mc_done;
   4666  1.1  riastrad 
   4667  1.4  riastrad 	ret = ci_copy_vbios_mc_reg_table(table, ci_table);
   4668  1.1  riastrad 	if (ret)
   4669  1.1  riastrad 		goto init_mc_done;
   4670  1.1  riastrad 
   4671  1.1  riastrad 	ci_set_s0_mc_reg_index(ci_table);
   4672  1.1  riastrad 
   4673  1.1  riastrad 	ret = ci_register_patching_mc_seq(rdev, ci_table);
   4674  1.1  riastrad 	if (ret)
   4675  1.1  riastrad 		goto init_mc_done;
   4676  1.1  riastrad 
   4677  1.1  riastrad 	ret = ci_set_mc_special_registers(rdev, ci_table);
   4678  1.1  riastrad 	if (ret)
   4679  1.1  riastrad 		goto init_mc_done;
   4680  1.1  riastrad 
   4681  1.1  riastrad 	ci_set_valid_flag(ci_table);
   4682  1.1  riastrad 
   4683  1.1  riastrad init_mc_done:
   4684  1.1  riastrad 	kfree(table);
   4685  1.1  riastrad 
   4686  1.1  riastrad 	return ret;
   4687  1.1  riastrad }
   4688  1.1  riastrad 
   4689  1.1  riastrad static int ci_populate_mc_reg_addresses(struct radeon_device *rdev,
   4690  1.1  riastrad 					SMU7_Discrete_MCRegisters *mc_reg_table)
   4691  1.1  riastrad {
   4692  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4693  1.1  riastrad 	u32 i, j;
   4694  1.1  riastrad 
   4695  1.1  riastrad 	for (i = 0, j = 0; j < pi->mc_reg_table.last; j++) {
   4696  1.1  riastrad 		if (pi->mc_reg_table.valid_flag & (1 << j)) {
   4697  1.1  riastrad 			if (i >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
   4698  1.1  riastrad 				return -EINVAL;
   4699  1.1  riastrad 			mc_reg_table->address[i].s0 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s0);
   4700  1.1  riastrad 			mc_reg_table->address[i].s1 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s1);
   4701  1.1  riastrad 			i++;
   4702  1.1  riastrad 		}
   4703  1.1  riastrad 	}
   4704  1.1  riastrad 
   4705  1.1  riastrad 	mc_reg_table->last = (u8)i;
   4706  1.1  riastrad 
   4707  1.1  riastrad 	return 0;
   4708  1.1  riastrad }
   4709  1.1  riastrad 
   4710  1.1  riastrad static void ci_convert_mc_registers(const struct ci_mc_reg_entry *entry,
   4711  1.1  riastrad 				    SMU7_Discrete_MCRegisterSet *data,
   4712  1.1  riastrad 				    u32 num_entries, u32 valid_flag)
   4713  1.1  riastrad {
   4714  1.1  riastrad 	u32 i, j;
   4715  1.1  riastrad 
   4716  1.1  riastrad 	for (i = 0, j = 0; j < num_entries; j++) {
   4717  1.1  riastrad 		if (valid_flag & (1 << j)) {
   4718  1.1  riastrad 			data->value[i] = cpu_to_be32(entry->mc_data[j]);
   4719  1.1  riastrad 			i++;
   4720  1.1  riastrad 		}
   4721  1.1  riastrad 	}
   4722  1.1  riastrad }
   4723  1.1  riastrad 
   4724  1.1  riastrad static void ci_convert_mc_reg_table_entry_to_smc(struct radeon_device *rdev,
   4725  1.1  riastrad 						 const u32 memory_clock,
   4726  1.1  riastrad 						 SMU7_Discrete_MCRegisterSet *mc_reg_table_data)
   4727  1.1  riastrad {
   4728  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4729  1.1  riastrad 	u32 i = 0;
   4730  1.1  riastrad 
   4731  1.1  riastrad 	for(i = 0; i < pi->mc_reg_table.num_entries; i++) {
   4732  1.1  riastrad 		if (memory_clock <= pi->mc_reg_table.mc_reg_table_entry[i].mclk_max)
   4733  1.1  riastrad 			break;
   4734  1.1  riastrad 	}
   4735  1.1  riastrad 
   4736  1.1  riastrad 	if ((i == pi->mc_reg_table.num_entries) && (i > 0))
   4737  1.1  riastrad 		--i;
   4738  1.1  riastrad 
   4739  1.1  riastrad 	ci_convert_mc_registers(&pi->mc_reg_table.mc_reg_table_entry[i],
   4740  1.1  riastrad 				mc_reg_table_data, pi->mc_reg_table.last,
   4741  1.1  riastrad 				pi->mc_reg_table.valid_flag);
   4742  1.1  riastrad }
   4743  1.1  riastrad 
   4744  1.1  riastrad static void ci_convert_mc_reg_table_to_smc(struct radeon_device *rdev,
   4745  1.1  riastrad 					   SMU7_Discrete_MCRegisters *mc_reg_table)
   4746  1.1  riastrad {
   4747  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4748  1.1  riastrad 	u32 i;
   4749  1.1  riastrad 
   4750  1.1  riastrad 	for (i = 0; i < pi->dpm_table.mclk_table.count; i++)
   4751  1.1  riastrad 		ci_convert_mc_reg_table_entry_to_smc(rdev,
   4752  1.1  riastrad 						     pi->dpm_table.mclk_table.dpm_levels[i].value,
   4753  1.1  riastrad 						     &mc_reg_table->data[i]);
   4754  1.1  riastrad }
   4755  1.1  riastrad 
   4756  1.1  riastrad static int ci_populate_initial_mc_reg_table(struct radeon_device *rdev)
   4757  1.1  riastrad {
   4758  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4759  1.1  riastrad 	int ret;
   4760  1.1  riastrad 
   4761  1.1  riastrad 	memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
   4762  1.1  riastrad 
   4763  1.1  riastrad 	ret = ci_populate_mc_reg_addresses(rdev, &pi->smc_mc_reg_table);
   4764  1.1  riastrad 	if (ret)
   4765  1.1  riastrad 		return ret;
   4766  1.1  riastrad 	ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
   4767  1.1  riastrad 
   4768  1.1  riastrad 	return ci_copy_bytes_to_smc(rdev,
   4769  1.1  riastrad 				    pi->mc_reg_table_start,
   4770  1.1  riastrad 				    (u8 *)&pi->smc_mc_reg_table,
   4771  1.1  riastrad 				    sizeof(SMU7_Discrete_MCRegisters),
   4772  1.1  riastrad 				    pi->sram_end);
   4773  1.1  riastrad }
   4774  1.1  riastrad 
   4775  1.1  riastrad static int ci_update_and_upload_mc_reg_table(struct radeon_device *rdev)
   4776  1.1  riastrad {
   4777  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4778  1.1  riastrad 
   4779  1.1  riastrad 	if (!(pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK))
   4780  1.1  riastrad 		return 0;
   4781  1.1  riastrad 
   4782  1.1  riastrad 	memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
   4783  1.1  riastrad 
   4784  1.1  riastrad 	ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
   4785  1.1  riastrad 
   4786  1.1  riastrad 	return ci_copy_bytes_to_smc(rdev,
   4787  1.1  riastrad 				    pi->mc_reg_table_start +
   4788  1.1  riastrad 				    offsetof(SMU7_Discrete_MCRegisters, data[0]),
   4789  1.1  riastrad 				    (u8 *)&pi->smc_mc_reg_table.data[0],
   4790  1.1  riastrad 				    sizeof(SMU7_Discrete_MCRegisterSet) *
   4791  1.1  riastrad 				    pi->dpm_table.mclk_table.count,
   4792  1.1  riastrad 				    pi->sram_end);
   4793  1.1  riastrad }
   4794  1.1  riastrad 
   4795  1.1  riastrad static void ci_enable_voltage_control(struct radeon_device *rdev)
   4796  1.1  riastrad {
   4797  1.1  riastrad 	u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
   4798  1.1  riastrad 
   4799  1.1  riastrad 	tmp |= VOLT_PWRMGT_EN;
   4800  1.1  riastrad 	WREG32_SMC(GENERAL_PWRMGT, tmp);
   4801  1.1  riastrad }
   4802  1.1  riastrad 
   4803  1.1  riastrad static enum radeon_pcie_gen ci_get_maximum_link_speed(struct radeon_device *rdev,
   4804  1.1  riastrad 						      struct radeon_ps *radeon_state)
   4805  1.1  riastrad {
   4806  1.1  riastrad 	struct ci_ps *state = ci_get_ps(radeon_state);
   4807  1.1  riastrad 	int i;
   4808  1.1  riastrad 	u16 pcie_speed, max_speed = 0;
   4809  1.1  riastrad 
   4810  1.1  riastrad 	for (i = 0; i < state->performance_level_count; i++) {
   4811  1.1  riastrad 		pcie_speed = state->performance_levels[i].pcie_gen;
   4812  1.1  riastrad 		if (max_speed < pcie_speed)
   4813  1.1  riastrad 			max_speed = pcie_speed;
   4814  1.1  riastrad 	}
   4815  1.1  riastrad 
   4816  1.1  riastrad 	return max_speed;
   4817  1.1  riastrad }
   4818  1.1  riastrad 
   4819  1.1  riastrad static u16 ci_get_current_pcie_speed(struct radeon_device *rdev)
   4820  1.1  riastrad {
   4821  1.1  riastrad 	u32 speed_cntl = 0;
   4822  1.1  riastrad 
   4823  1.1  riastrad 	speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL) & LC_CURRENT_DATA_RATE_MASK;
   4824  1.1  riastrad 	speed_cntl >>= LC_CURRENT_DATA_RATE_SHIFT;
   4825  1.1  riastrad 
   4826  1.1  riastrad 	return (u16)speed_cntl;
   4827  1.1  riastrad }
   4828  1.1  riastrad 
   4829  1.1  riastrad static int ci_get_current_pcie_lane_number(struct radeon_device *rdev)
   4830  1.1  riastrad {
   4831  1.1  riastrad 	u32 link_width = 0;
   4832  1.1  riastrad 
   4833  1.1  riastrad 	link_width = RREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL) & LC_LINK_WIDTH_RD_MASK;
   4834  1.1  riastrad 	link_width >>= LC_LINK_WIDTH_RD_SHIFT;
   4835  1.1  riastrad 
   4836  1.1  riastrad 	switch (link_width) {
   4837  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X1:
   4838  1.1  riastrad 		return 1;
   4839  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X2:
   4840  1.1  riastrad 		return 2;
   4841  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X4:
   4842  1.1  riastrad 		return 4;
   4843  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X8:
   4844  1.1  riastrad 		return 8;
   4845  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X12:
   4846  1.1  riastrad 		/* not actually supported */
   4847  1.1  riastrad 		return 12;
   4848  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X0:
   4849  1.1  riastrad 	case RADEON_PCIE_LC_LINK_WIDTH_X16:
   4850  1.1  riastrad 	default:
   4851  1.1  riastrad 		return 16;
   4852  1.1  riastrad 	}
   4853  1.1  riastrad }
   4854  1.1  riastrad 
   4855  1.1  riastrad static void ci_request_link_speed_change_before_state_change(struct radeon_device *rdev,
   4856  1.1  riastrad 							     struct radeon_ps *radeon_new_state,
   4857  1.1  riastrad 							     struct radeon_ps *radeon_current_state)
   4858  1.1  riastrad {
   4859  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4860  1.1  riastrad 	enum radeon_pcie_gen target_link_speed =
   4861  1.1  riastrad 		ci_get_maximum_link_speed(rdev, radeon_new_state);
   4862  1.1  riastrad 	enum radeon_pcie_gen current_link_speed;
   4863  1.1  riastrad 
   4864  1.1  riastrad 	if (pi->force_pcie_gen == RADEON_PCIE_GEN_INVALID)
   4865  1.1  riastrad 		current_link_speed = ci_get_maximum_link_speed(rdev, radeon_current_state);
   4866  1.1  riastrad 	else
   4867  1.1  riastrad 		current_link_speed = pi->force_pcie_gen;
   4868  1.1  riastrad 
   4869  1.1  riastrad 	pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
   4870  1.1  riastrad 	pi->pspp_notify_required = false;
   4871  1.1  riastrad 	if (target_link_speed > current_link_speed) {
   4872  1.1  riastrad 		switch (target_link_speed) {
   4873  1.1  riastrad #ifdef CONFIG_ACPI
   4874  1.1  riastrad 		case RADEON_PCIE_GEN3:
   4875  1.1  riastrad 			if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN3, false) == 0)
   4876  1.1  riastrad 				break;
   4877  1.1  riastrad 			pi->force_pcie_gen = RADEON_PCIE_GEN2;
   4878  1.1  riastrad 			if (current_link_speed == RADEON_PCIE_GEN2)
   4879  1.1  riastrad 				break;
   4880  1.4  riastrad 			/* fall through */
   4881  1.1  riastrad 		case RADEON_PCIE_GEN2:
   4882  1.1  riastrad 			if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN2, false) == 0)
   4883  1.1  riastrad 				break;
   4884  1.1  riastrad #endif
   4885  1.4  riastrad 			/* fall through */
   4886  1.1  riastrad 		default:
   4887  1.1  riastrad 			pi->force_pcie_gen = ci_get_current_pcie_speed(rdev);
   4888  1.1  riastrad 			break;
   4889  1.1  riastrad 		}
   4890  1.1  riastrad 	} else {
   4891  1.1  riastrad 		if (target_link_speed < current_link_speed)
   4892  1.1  riastrad 			pi->pspp_notify_required = true;
   4893  1.1  riastrad 	}
   4894  1.1  riastrad }
   4895  1.1  riastrad 
   4896  1.1  riastrad static void ci_notify_link_speed_change_after_state_change(struct radeon_device *rdev,
   4897  1.1  riastrad 							   struct radeon_ps *radeon_new_state,
   4898  1.1  riastrad 							   struct radeon_ps *radeon_current_state)
   4899  1.1  riastrad {
   4900  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4901  1.1  riastrad 	enum radeon_pcie_gen target_link_speed =
   4902  1.1  riastrad 		ci_get_maximum_link_speed(rdev, radeon_new_state);
   4903  1.1  riastrad 	u8 request;
   4904  1.1  riastrad 
   4905  1.1  riastrad 	if (pi->pspp_notify_required) {
   4906  1.1  riastrad 		if (target_link_speed == RADEON_PCIE_GEN3)
   4907  1.1  riastrad 			request = PCIE_PERF_REQ_PECI_GEN3;
   4908  1.1  riastrad 		else if (target_link_speed == RADEON_PCIE_GEN2)
   4909  1.1  riastrad 			request = PCIE_PERF_REQ_PECI_GEN2;
   4910  1.1  riastrad 		else
   4911  1.1  riastrad 			request = PCIE_PERF_REQ_PECI_GEN1;
   4912  1.1  riastrad 
   4913  1.1  riastrad 		if ((request == PCIE_PERF_REQ_PECI_GEN1) &&
   4914  1.1  riastrad 		    (ci_get_current_pcie_speed(rdev) > 0))
   4915  1.1  riastrad 			return;
   4916  1.1  riastrad 
   4917  1.1  riastrad #ifdef CONFIG_ACPI
   4918  1.1  riastrad 		radeon_acpi_pcie_performance_request(rdev, request, false);
   4919  1.1  riastrad #endif
   4920  1.1  riastrad 	}
   4921  1.1  riastrad }
   4922  1.1  riastrad 
   4923  1.1  riastrad static int ci_set_private_data_variables_based_on_pptable(struct radeon_device *rdev)
   4924  1.1  riastrad {
   4925  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4926  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
   4927  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
   4928  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *allowed_mclk_vddc_table =
   4929  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
   4930  1.1  riastrad 	struct radeon_clock_voltage_dependency_table *allowed_mclk_vddci_table =
   4931  1.1  riastrad 		&rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
   4932  1.1  riastrad 
   4933  1.1  riastrad 	if (allowed_sclk_vddc_table == NULL)
   4934  1.1  riastrad 		return -EINVAL;
   4935  1.1  riastrad 	if (allowed_sclk_vddc_table->count < 1)
   4936  1.1  riastrad 		return -EINVAL;
   4937  1.1  riastrad 	if (allowed_mclk_vddc_table == NULL)
   4938  1.1  riastrad 		return -EINVAL;
   4939  1.1  riastrad 	if (allowed_mclk_vddc_table->count < 1)
   4940  1.1  riastrad 		return -EINVAL;
   4941  1.1  riastrad 	if (allowed_mclk_vddci_table == NULL)
   4942  1.1  riastrad 		return -EINVAL;
   4943  1.1  riastrad 	if (allowed_mclk_vddci_table->count < 1)
   4944  1.1  riastrad 		return -EINVAL;
   4945  1.1  riastrad 
   4946  1.1  riastrad 	pi->min_vddc_in_pp_table = allowed_sclk_vddc_table->entries[0].v;
   4947  1.1  riastrad 	pi->max_vddc_in_pp_table =
   4948  1.1  riastrad 		allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
   4949  1.1  riastrad 
   4950  1.1  riastrad 	pi->min_vddci_in_pp_table = allowed_mclk_vddci_table->entries[0].v;
   4951  1.1  riastrad 	pi->max_vddci_in_pp_table =
   4952  1.1  riastrad 		allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
   4953  1.1  riastrad 
   4954  1.1  riastrad 	rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk =
   4955  1.1  riastrad 		allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
   4956  1.1  riastrad 	rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk =
   4957  1.1  riastrad 		allowed_mclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
   4958  1.1  riastrad 	rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddc =
   4959  1.1  riastrad 		allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
   4960  1.4  riastrad 	rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddci =
   4961  1.1  riastrad 		allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
   4962  1.1  riastrad 
   4963  1.1  riastrad 	return 0;
   4964  1.1  riastrad }
   4965  1.1  riastrad 
   4966  1.1  riastrad static void ci_patch_with_vddc_leakage(struct radeon_device *rdev, u16 *vddc)
   4967  1.1  riastrad {
   4968  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4969  1.1  riastrad 	struct ci_leakage_voltage *leakage_table = &pi->vddc_leakage;
   4970  1.1  riastrad 	u32 leakage_index;
   4971  1.1  riastrad 
   4972  1.1  riastrad 	for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
   4973  1.1  riastrad 		if (leakage_table->leakage_id[leakage_index] == *vddc) {
   4974  1.1  riastrad 			*vddc = leakage_table->actual_voltage[leakage_index];
   4975  1.1  riastrad 			break;
   4976  1.1  riastrad 		}
   4977  1.1  riastrad 	}
   4978  1.1  riastrad }
   4979  1.1  riastrad 
   4980  1.1  riastrad static void ci_patch_with_vddci_leakage(struct radeon_device *rdev, u16 *vddci)
   4981  1.1  riastrad {
   4982  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   4983  1.1  riastrad 	struct ci_leakage_voltage *leakage_table = &pi->vddci_leakage;
   4984  1.1  riastrad 	u32 leakage_index;
   4985  1.1  riastrad 
   4986  1.1  riastrad 	for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
   4987  1.1  riastrad 		if (leakage_table->leakage_id[leakage_index] == *vddci) {
   4988  1.1  riastrad 			*vddci = leakage_table->actual_voltage[leakage_index];
   4989  1.1  riastrad 			break;
   4990  1.1  riastrad 		}
   4991  1.1  riastrad 	}
   4992  1.1  riastrad }
   4993  1.1  riastrad 
   4994  1.1  riastrad static void ci_patch_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
   4995  1.1  riastrad 								      struct radeon_clock_voltage_dependency_table *table)
   4996  1.1  riastrad {
   4997  1.1  riastrad 	u32 i;
   4998  1.1  riastrad 
   4999  1.1  riastrad 	if (table) {
   5000  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5001  1.1  riastrad 			ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
   5002  1.1  riastrad 	}
   5003  1.1  riastrad }
   5004  1.1  riastrad 
   5005  1.1  riastrad static void ci_patch_clock_voltage_dependency_table_with_vddci_leakage(struct radeon_device *rdev,
   5006  1.1  riastrad 								       struct radeon_clock_voltage_dependency_table *table)
   5007  1.1  riastrad {
   5008  1.1  riastrad 	u32 i;
   5009  1.1  riastrad 
   5010  1.1  riastrad 	if (table) {
   5011  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5012  1.1  riastrad 			ci_patch_with_vddci_leakage(rdev, &table->entries[i].v);
   5013  1.1  riastrad 	}
   5014  1.1  riastrad }
   5015  1.1  riastrad 
   5016  1.1  riastrad static void ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
   5017  1.1  riastrad 									  struct radeon_vce_clock_voltage_dependency_table *table)
   5018  1.1  riastrad {
   5019  1.1  riastrad 	u32 i;
   5020  1.1  riastrad 
   5021  1.1  riastrad 	if (table) {
   5022  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5023  1.1  riastrad 			ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
   5024  1.1  riastrad 	}
   5025  1.1  riastrad }
   5026  1.1  riastrad 
   5027  1.1  riastrad static void ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
   5028  1.1  riastrad 									  struct radeon_uvd_clock_voltage_dependency_table *table)
   5029  1.1  riastrad {
   5030  1.1  riastrad 	u32 i;
   5031  1.1  riastrad 
   5032  1.1  riastrad 	if (table) {
   5033  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5034  1.1  riastrad 			ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
   5035  1.1  riastrad 	}
   5036  1.1  riastrad }
   5037  1.1  riastrad 
   5038  1.1  riastrad static void ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(struct radeon_device *rdev,
   5039  1.1  riastrad 								   struct radeon_phase_shedding_limits_table *table)
   5040  1.1  riastrad {
   5041  1.1  riastrad 	u32 i;
   5042  1.1  riastrad 
   5043  1.1  riastrad 	if (table) {
   5044  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5045  1.1  riastrad 			ci_patch_with_vddc_leakage(rdev, &table->entries[i].voltage);
   5046  1.1  riastrad 	}
   5047  1.1  riastrad }
   5048  1.1  riastrad 
   5049  1.1  riastrad static void ci_patch_clock_voltage_limits_with_vddc_leakage(struct radeon_device *rdev,
   5050  1.1  riastrad 							    struct radeon_clock_and_voltage_limits *table)
   5051  1.1  riastrad {
   5052  1.1  riastrad 	if (table) {
   5053  1.1  riastrad 		ci_patch_with_vddc_leakage(rdev, (u16 *)&table->vddc);
   5054  1.1  riastrad 		ci_patch_with_vddci_leakage(rdev, (u16 *)&table->vddci);
   5055  1.1  riastrad 	}
   5056  1.1  riastrad }
   5057  1.1  riastrad 
   5058  1.1  riastrad static void ci_patch_cac_leakage_table_with_vddc_leakage(struct radeon_device *rdev,
   5059  1.1  riastrad 							 struct radeon_cac_leakage_table *table)
   5060  1.1  riastrad {
   5061  1.1  riastrad 	u32 i;
   5062  1.1  riastrad 
   5063  1.1  riastrad 	if (table) {
   5064  1.1  riastrad 		for (i = 0; i < table->count; i++)
   5065  1.1  riastrad 			ci_patch_with_vddc_leakage(rdev, &table->entries[i].vddc);
   5066  1.1  riastrad 	}
   5067  1.1  riastrad }
   5068  1.1  riastrad 
   5069  1.1  riastrad static void ci_patch_dependency_tables_with_leakage(struct radeon_device *rdev)
   5070  1.1  riastrad {
   5071  1.1  riastrad 
   5072  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5073  1.1  riastrad 								  &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk);
   5074  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5075  1.1  riastrad 								  &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk);
   5076  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5077  1.1  riastrad 								  &rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk);
   5078  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddci_leakage(rdev,
   5079  1.1  riastrad 								   &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk);
   5080  1.1  riastrad 	ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5081  1.1  riastrad 								      &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table);
   5082  1.1  riastrad 	ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5083  1.1  riastrad 								      &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table);
   5084  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5085  1.1  riastrad 								  &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table);
   5086  1.1  riastrad 	ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
   5087  1.1  riastrad 								  &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table);
   5088  1.1  riastrad 	ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(rdev,
   5089  1.1  riastrad 							       &rdev->pm.dpm.dyn_state.phase_shedding_limits_table);
   5090  1.1  riastrad 	ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
   5091  1.1  riastrad 							&rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
   5092  1.1  riastrad 	ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
   5093  1.1  riastrad 							&rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc);
   5094  1.1  riastrad 	ci_patch_cac_leakage_table_with_vddc_leakage(rdev,
   5095  1.1  riastrad 						     &rdev->pm.dpm.dyn_state.cac_leakage_table);
   5096  1.1  riastrad 
   5097  1.1  riastrad }
   5098  1.1  riastrad 
   5099  1.1  riastrad static void ci_get_memory_type(struct radeon_device *rdev)
   5100  1.1  riastrad {
   5101  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5102  1.1  riastrad 	u32 tmp;
   5103  1.1  riastrad 
   5104  1.1  riastrad 	tmp = RREG32(MC_SEQ_MISC0);
   5105  1.1  riastrad 
   5106  1.1  riastrad 	if (((tmp & MC_SEQ_MISC0_GDDR5_MASK) >> MC_SEQ_MISC0_GDDR5_SHIFT) ==
   5107  1.1  riastrad 	    MC_SEQ_MISC0_GDDR5_VALUE)
   5108  1.1  riastrad 		pi->mem_gddr5 = true;
   5109  1.1  riastrad 	else
   5110  1.1  riastrad 		pi->mem_gddr5 = false;
   5111  1.1  riastrad 
   5112  1.1  riastrad }
   5113  1.1  riastrad 
   5114  1.1  riastrad static void ci_update_current_ps(struct radeon_device *rdev,
   5115  1.1  riastrad 				 struct radeon_ps *rps)
   5116  1.1  riastrad {
   5117  1.1  riastrad 	struct ci_ps *new_ps = ci_get_ps(rps);
   5118  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5119  1.1  riastrad 
   5120  1.1  riastrad 	pi->current_rps = *rps;
   5121  1.1  riastrad 	pi->current_ps = *new_ps;
   5122  1.1  riastrad 	pi->current_rps.ps_priv = &pi->current_ps;
   5123  1.1  riastrad }
   5124  1.1  riastrad 
   5125  1.1  riastrad static void ci_update_requested_ps(struct radeon_device *rdev,
   5126  1.1  riastrad 				   struct radeon_ps *rps)
   5127  1.1  riastrad {
   5128  1.1  riastrad 	struct ci_ps *new_ps = ci_get_ps(rps);
   5129  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5130  1.1  riastrad 
   5131  1.1  riastrad 	pi->requested_rps = *rps;
   5132  1.1  riastrad 	pi->requested_ps = *new_ps;
   5133  1.1  riastrad 	pi->requested_rps.ps_priv = &pi->requested_ps;
   5134  1.1  riastrad }
   5135  1.1  riastrad 
   5136  1.1  riastrad int ci_dpm_pre_set_power_state(struct radeon_device *rdev)
   5137  1.1  riastrad {
   5138  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5139  1.1  riastrad 	struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
   5140  1.1  riastrad 	struct radeon_ps *new_ps = &requested_ps;
   5141  1.1  riastrad 
   5142  1.1  riastrad 	ci_update_requested_ps(rdev, new_ps);
   5143  1.1  riastrad 
   5144  1.1  riastrad 	ci_apply_state_adjust_rules(rdev, &pi->requested_rps);
   5145  1.1  riastrad 
   5146  1.1  riastrad 	return 0;
   5147  1.1  riastrad }
   5148  1.1  riastrad 
   5149  1.1  riastrad void ci_dpm_post_set_power_state(struct radeon_device *rdev)
   5150  1.1  riastrad {
   5151  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5152  1.1  riastrad 	struct radeon_ps *new_ps = &pi->requested_rps;
   5153  1.1  riastrad 
   5154  1.1  riastrad 	ci_update_current_ps(rdev, new_ps);
   5155  1.1  riastrad }
   5156  1.1  riastrad 
   5157  1.1  riastrad 
   5158  1.1  riastrad void ci_dpm_setup_asic(struct radeon_device *rdev)
   5159  1.1  riastrad {
   5160  1.1  riastrad 	int r;
   5161  1.1  riastrad 
   5162  1.1  riastrad 	r = ci_mc_load_microcode(rdev);
   5163  1.1  riastrad 	if (r)
   5164  1.1  riastrad 		DRM_ERROR("Failed to load MC firmware!\n");
   5165  1.1  riastrad 	ci_read_clock_registers(rdev);
   5166  1.1  riastrad 	ci_get_memory_type(rdev);
   5167  1.1  riastrad 	ci_enable_acpi_power_management(rdev);
   5168  1.1  riastrad 	ci_init_sclk_t(rdev);
   5169  1.1  riastrad }
   5170  1.1  riastrad 
   5171  1.1  riastrad int ci_dpm_enable(struct radeon_device *rdev)
   5172  1.1  riastrad {
   5173  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5174  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   5175  1.1  riastrad 	int ret;
   5176  1.1  riastrad 
   5177  1.1  riastrad 	if (ci_is_smc_running(rdev))
   5178  1.1  riastrad 		return -EINVAL;
   5179  1.1  riastrad 	if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
   5180  1.1  riastrad 		ci_enable_voltage_control(rdev);
   5181  1.1  riastrad 		ret = ci_construct_voltage_tables(rdev);
   5182  1.1  riastrad 		if (ret) {
   5183  1.1  riastrad 			DRM_ERROR("ci_construct_voltage_tables failed\n");
   5184  1.1  riastrad 			return ret;
   5185  1.1  riastrad 		}
   5186  1.1  riastrad 	}
   5187  1.1  riastrad 	if (pi->caps_dynamic_ac_timing) {
   5188  1.1  riastrad 		ret = ci_initialize_mc_reg_table(rdev);
   5189  1.1  riastrad 		if (ret)
   5190  1.1  riastrad 			pi->caps_dynamic_ac_timing = false;
   5191  1.1  riastrad 	}
   5192  1.1  riastrad 	if (pi->dynamic_ss)
   5193  1.1  riastrad 		ci_enable_spread_spectrum(rdev, true);
   5194  1.1  riastrad 	if (pi->thermal_protection)
   5195  1.1  riastrad 		ci_enable_thermal_protection(rdev, true);
   5196  1.1  riastrad 	ci_program_sstp(rdev);
   5197  1.1  riastrad 	ci_enable_display_gap(rdev);
   5198  1.1  riastrad 	ci_program_vc(rdev);
   5199  1.1  riastrad 	ret = ci_upload_firmware(rdev);
   5200  1.1  riastrad 	if (ret) {
   5201  1.1  riastrad 		DRM_ERROR("ci_upload_firmware failed\n");
   5202  1.1  riastrad 		return ret;
   5203  1.1  riastrad 	}
   5204  1.1  riastrad 	ret = ci_process_firmware_header(rdev);
   5205  1.1  riastrad 	if (ret) {
   5206  1.1  riastrad 		DRM_ERROR("ci_process_firmware_header failed\n");
   5207  1.1  riastrad 		return ret;
   5208  1.1  riastrad 	}
   5209  1.1  riastrad 	ret = ci_initial_switch_from_arb_f0_to_f1(rdev);
   5210  1.1  riastrad 	if (ret) {
   5211  1.1  riastrad 		DRM_ERROR("ci_initial_switch_from_arb_f0_to_f1 failed\n");
   5212  1.1  riastrad 		return ret;
   5213  1.1  riastrad 	}
   5214  1.1  riastrad 	ret = ci_init_smc_table(rdev);
   5215  1.1  riastrad 	if (ret) {
   5216  1.1  riastrad 		DRM_ERROR("ci_init_smc_table failed\n");
   5217  1.1  riastrad 		return ret;
   5218  1.1  riastrad 	}
   5219  1.1  riastrad 	ret = ci_init_arb_table_index(rdev);
   5220  1.1  riastrad 	if (ret) {
   5221  1.1  riastrad 		DRM_ERROR("ci_init_arb_table_index failed\n");
   5222  1.1  riastrad 		return ret;
   5223  1.1  riastrad 	}
   5224  1.1  riastrad 	if (pi->caps_dynamic_ac_timing) {
   5225  1.1  riastrad 		ret = ci_populate_initial_mc_reg_table(rdev);
   5226  1.1  riastrad 		if (ret) {
   5227  1.1  riastrad 			DRM_ERROR("ci_populate_initial_mc_reg_table failed\n");
   5228  1.1  riastrad 			return ret;
   5229  1.1  riastrad 		}
   5230  1.1  riastrad 	}
   5231  1.1  riastrad 	ret = ci_populate_pm_base(rdev);
   5232  1.1  riastrad 	if (ret) {
   5233  1.1  riastrad 		DRM_ERROR("ci_populate_pm_base failed\n");
   5234  1.1  riastrad 		return ret;
   5235  1.1  riastrad 	}
   5236  1.1  riastrad 	ci_dpm_start_smc(rdev);
   5237  1.1  riastrad 	ci_enable_vr_hot_gpio_interrupt(rdev);
   5238  1.1  riastrad 	ret = ci_notify_smc_display_change(rdev, false);
   5239  1.1  riastrad 	if (ret) {
   5240  1.1  riastrad 		DRM_ERROR("ci_notify_smc_display_change failed\n");
   5241  1.1  riastrad 		return ret;
   5242  1.1  riastrad 	}
   5243  1.1  riastrad 	ci_enable_sclk_control(rdev, true);
   5244  1.1  riastrad 	ret = ci_enable_ulv(rdev, true);
   5245  1.1  riastrad 	if (ret) {
   5246  1.1  riastrad 		DRM_ERROR("ci_enable_ulv failed\n");
   5247  1.1  riastrad 		return ret;
   5248  1.1  riastrad 	}
   5249  1.1  riastrad 	ret = ci_enable_ds_master_switch(rdev, true);
   5250  1.1  riastrad 	if (ret) {
   5251  1.1  riastrad 		DRM_ERROR("ci_enable_ds_master_switch failed\n");
   5252  1.1  riastrad 		return ret;
   5253  1.1  riastrad 	}
   5254  1.1  riastrad 	ret = ci_start_dpm(rdev);
   5255  1.1  riastrad 	if (ret) {
   5256  1.1  riastrad 		DRM_ERROR("ci_start_dpm failed\n");
   5257  1.1  riastrad 		return ret;
   5258  1.1  riastrad 	}
   5259  1.1  riastrad 	ret = ci_enable_didt(rdev, true);
   5260  1.1  riastrad 	if (ret) {
   5261  1.1  riastrad 		DRM_ERROR("ci_enable_didt failed\n");
   5262  1.1  riastrad 		return ret;
   5263  1.1  riastrad 	}
   5264  1.1  riastrad 	ret = ci_enable_smc_cac(rdev, true);
   5265  1.1  riastrad 	if (ret) {
   5266  1.1  riastrad 		DRM_ERROR("ci_enable_smc_cac failed\n");
   5267  1.1  riastrad 		return ret;
   5268  1.1  riastrad 	}
   5269  1.1  riastrad 	ret = ci_enable_power_containment(rdev, true);
   5270  1.1  riastrad 	if (ret) {
   5271  1.1  riastrad 		DRM_ERROR("ci_enable_power_containment failed\n");
   5272  1.1  riastrad 		return ret;
   5273  1.1  riastrad 	}
   5274  1.1  riastrad 
   5275  1.1  riastrad 	ret = ci_power_control_set_level(rdev);
   5276  1.1  riastrad 	if (ret) {
   5277  1.1  riastrad 		DRM_ERROR("ci_power_control_set_level failed\n");
   5278  1.1  riastrad 		return ret;
   5279  1.1  riastrad 	}
   5280  1.1  riastrad 
   5281  1.1  riastrad 	ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
   5282  1.1  riastrad 
   5283  1.1  riastrad 	ret = ci_enable_thermal_based_sclk_dpm(rdev, true);
   5284  1.1  riastrad 	if (ret) {
   5285  1.1  riastrad 		DRM_ERROR("ci_enable_thermal_based_sclk_dpm failed\n");
   5286  1.1  riastrad 		return ret;
   5287  1.1  riastrad 	}
   5288  1.1  riastrad 
   5289  1.1  riastrad 	ci_thermal_start_thermal_controller(rdev);
   5290  1.1  riastrad 
   5291  1.1  riastrad 	ci_update_current_ps(rdev, boot_ps);
   5292  1.1  riastrad 
   5293  1.1  riastrad 	return 0;
   5294  1.1  riastrad }
   5295  1.1  riastrad 
   5296  1.1  riastrad static int ci_set_temperature_range(struct radeon_device *rdev)
   5297  1.1  riastrad {
   5298  1.1  riastrad 	int ret;
   5299  1.1  riastrad 
   5300  1.1  riastrad 	ret = ci_thermal_enable_alert(rdev, false);
   5301  1.1  riastrad 	if (ret)
   5302  1.1  riastrad 		return ret;
   5303  1.1  riastrad 	ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
   5304  1.1  riastrad 	if (ret)
   5305  1.1  riastrad 		return ret;
   5306  1.1  riastrad 	ret = ci_thermal_enable_alert(rdev, true);
   5307  1.1  riastrad 	if (ret)
   5308  1.1  riastrad 		return ret;
   5309  1.1  riastrad 
   5310  1.1  riastrad 	return ret;
   5311  1.1  riastrad }
   5312  1.1  riastrad 
   5313  1.1  riastrad int ci_dpm_late_enable(struct radeon_device *rdev)
   5314  1.1  riastrad {
   5315  1.1  riastrad 	int ret;
   5316  1.1  riastrad 
   5317  1.1  riastrad 	ret = ci_set_temperature_range(rdev);
   5318  1.1  riastrad 	if (ret)
   5319  1.1  riastrad 		return ret;
   5320  1.1  riastrad 
   5321  1.1  riastrad 	ci_dpm_powergate_uvd(rdev, true);
   5322  1.1  riastrad 
   5323  1.1  riastrad 	return 0;
   5324  1.1  riastrad }
   5325  1.1  riastrad 
   5326  1.1  riastrad void ci_dpm_disable(struct radeon_device *rdev)
   5327  1.1  riastrad {
   5328  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5329  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   5330  1.1  riastrad 
   5331  1.1  riastrad 	ci_dpm_powergate_uvd(rdev, false);
   5332  1.1  riastrad 
   5333  1.1  riastrad 	if (!ci_is_smc_running(rdev))
   5334  1.1  riastrad 		return;
   5335  1.1  riastrad 
   5336  1.1  riastrad 	ci_thermal_stop_thermal_controller(rdev);
   5337  1.1  riastrad 
   5338  1.1  riastrad 	if (pi->thermal_protection)
   5339  1.1  riastrad 		ci_enable_thermal_protection(rdev, false);
   5340  1.1  riastrad 	ci_enable_power_containment(rdev, false);
   5341  1.1  riastrad 	ci_enable_smc_cac(rdev, false);
   5342  1.1  riastrad 	ci_enable_didt(rdev, false);
   5343  1.1  riastrad 	ci_enable_spread_spectrum(rdev, false);
   5344  1.1  riastrad 	ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, false);
   5345  1.1  riastrad 	ci_stop_dpm(rdev);
   5346  1.1  riastrad 	ci_enable_ds_master_switch(rdev, false);
   5347  1.1  riastrad 	ci_enable_ulv(rdev, false);
   5348  1.1  riastrad 	ci_clear_vc(rdev);
   5349  1.1  riastrad 	ci_reset_to_default(rdev);
   5350  1.1  riastrad 	ci_dpm_stop_smc(rdev);
   5351  1.1  riastrad 	ci_force_switch_to_arb_f0(rdev);
   5352  1.1  riastrad 	ci_enable_thermal_based_sclk_dpm(rdev, false);
   5353  1.1  riastrad 
   5354  1.1  riastrad 	ci_update_current_ps(rdev, boot_ps);
   5355  1.1  riastrad }
   5356  1.1  riastrad 
   5357  1.1  riastrad int ci_dpm_set_power_state(struct radeon_device *rdev)
   5358  1.1  riastrad {
   5359  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5360  1.1  riastrad 	struct radeon_ps *new_ps = &pi->requested_rps;
   5361  1.1  riastrad 	struct radeon_ps *old_ps = &pi->current_rps;
   5362  1.1  riastrad 	int ret;
   5363  1.1  riastrad 
   5364  1.1  riastrad 	ci_find_dpm_states_clocks_in_dpm_table(rdev, new_ps);
   5365  1.1  riastrad 	if (pi->pcie_performance_request)
   5366  1.1  riastrad 		ci_request_link_speed_change_before_state_change(rdev, new_ps, old_ps);
   5367  1.1  riastrad 	ret = ci_freeze_sclk_mclk_dpm(rdev);
   5368  1.1  riastrad 	if (ret) {
   5369  1.1  riastrad 		DRM_ERROR("ci_freeze_sclk_mclk_dpm failed\n");
   5370  1.1  riastrad 		return ret;
   5371  1.1  riastrad 	}
   5372  1.1  riastrad 	ret = ci_populate_and_upload_sclk_mclk_dpm_levels(rdev, new_ps);
   5373  1.1  riastrad 	if (ret) {
   5374  1.1  riastrad 		DRM_ERROR("ci_populate_and_upload_sclk_mclk_dpm_levels failed\n");
   5375  1.1  riastrad 		return ret;
   5376  1.1  riastrad 	}
   5377  1.1  riastrad 	ret = ci_generate_dpm_level_enable_mask(rdev, new_ps);
   5378  1.1  riastrad 	if (ret) {
   5379  1.1  riastrad 		DRM_ERROR("ci_generate_dpm_level_enable_mask failed\n");
   5380  1.1  riastrad 		return ret;
   5381  1.1  riastrad 	}
   5382  1.1  riastrad 
   5383  1.1  riastrad 	ret = ci_update_vce_dpm(rdev, new_ps, old_ps);
   5384  1.1  riastrad 	if (ret) {
   5385  1.1  riastrad 		DRM_ERROR("ci_update_vce_dpm failed\n");
   5386  1.1  riastrad 		return ret;
   5387  1.1  riastrad 	}
   5388  1.1  riastrad 
   5389  1.1  riastrad 	ret = ci_update_sclk_t(rdev);
   5390  1.1  riastrad 	if (ret) {
   5391  1.1  riastrad 		DRM_ERROR("ci_update_sclk_t failed\n");
   5392  1.1  riastrad 		return ret;
   5393  1.1  riastrad 	}
   5394  1.1  riastrad 	if (pi->caps_dynamic_ac_timing) {
   5395  1.1  riastrad 		ret = ci_update_and_upload_mc_reg_table(rdev);
   5396  1.1  riastrad 		if (ret) {
   5397  1.1  riastrad 			DRM_ERROR("ci_update_and_upload_mc_reg_table failed\n");
   5398  1.1  riastrad 			return ret;
   5399  1.1  riastrad 		}
   5400  1.1  riastrad 	}
   5401  1.1  riastrad 	ret = ci_program_memory_timing_parameters(rdev);
   5402  1.1  riastrad 	if (ret) {
   5403  1.1  riastrad 		DRM_ERROR("ci_program_memory_timing_parameters failed\n");
   5404  1.1  riastrad 		return ret;
   5405  1.1  riastrad 	}
   5406  1.1  riastrad 	ret = ci_unfreeze_sclk_mclk_dpm(rdev);
   5407  1.1  riastrad 	if (ret) {
   5408  1.1  riastrad 		DRM_ERROR("ci_unfreeze_sclk_mclk_dpm failed\n");
   5409  1.1  riastrad 		return ret;
   5410  1.1  riastrad 	}
   5411  1.1  riastrad 	ret = ci_upload_dpm_level_enable_mask(rdev);
   5412  1.1  riastrad 	if (ret) {
   5413  1.1  riastrad 		DRM_ERROR("ci_upload_dpm_level_enable_mask failed\n");
   5414  1.1  riastrad 		return ret;
   5415  1.1  riastrad 	}
   5416  1.1  riastrad 	if (pi->pcie_performance_request)
   5417  1.1  riastrad 		ci_notify_link_speed_change_after_state_change(rdev, new_ps, old_ps);
   5418  1.1  riastrad 
   5419  1.1  riastrad 	return 0;
   5420  1.1  riastrad }
   5421  1.1  riastrad 
   5422  1.1  riastrad #if 0
   5423  1.1  riastrad void ci_dpm_reset_asic(struct radeon_device *rdev)
   5424  1.1  riastrad {
   5425  1.1  riastrad 	ci_set_boot_state(rdev);
   5426  1.1  riastrad }
   5427  1.1  riastrad #endif
   5428  1.1  riastrad 
   5429  1.1  riastrad void ci_dpm_display_configuration_changed(struct radeon_device *rdev)
   5430  1.1  riastrad {
   5431  1.1  riastrad 	ci_program_display_gap(rdev);
   5432  1.1  riastrad }
   5433  1.1  riastrad 
   5434  1.1  riastrad union power_info {
   5435  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO info;
   5436  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
   5437  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
   5438  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
   5439  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
   5440  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
   5441  1.1  riastrad };
   5442  1.1  riastrad 
   5443  1.1  riastrad union pplib_clock_info {
   5444  1.1  riastrad 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
   5445  1.1  riastrad 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
   5446  1.1  riastrad 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
   5447  1.1  riastrad 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
   5448  1.1  riastrad 	struct _ATOM_PPLIB_SI_CLOCK_INFO si;
   5449  1.1  riastrad 	struct _ATOM_PPLIB_CI_CLOCK_INFO ci;
   5450  1.1  riastrad };
   5451  1.1  riastrad 
   5452  1.1  riastrad union pplib_power_state {
   5453  1.1  riastrad 	struct _ATOM_PPLIB_STATE v1;
   5454  1.1  riastrad 	struct _ATOM_PPLIB_STATE_V2 v2;
   5455  1.1  riastrad };
   5456  1.1  riastrad 
   5457  1.1  riastrad static void ci_parse_pplib_non_clock_info(struct radeon_device *rdev,
   5458  1.1  riastrad 					  struct radeon_ps *rps,
   5459  1.1  riastrad 					  struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
   5460  1.1  riastrad 					  u8 table_rev)
   5461  1.1  riastrad {
   5462  1.1  riastrad 	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
   5463  1.1  riastrad 	rps->class = le16_to_cpu(non_clock_info->usClassification);
   5464  1.1  riastrad 	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
   5465  1.1  riastrad 
   5466  1.1  riastrad 	if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
   5467  1.1  riastrad 		rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
   5468  1.1  riastrad 		rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
   5469  1.1  riastrad 	} else {
   5470  1.1  riastrad 		rps->vclk = 0;
   5471  1.1  riastrad 		rps->dclk = 0;
   5472  1.1  riastrad 	}
   5473  1.1  riastrad 
   5474  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
   5475  1.1  riastrad 		rdev->pm.dpm.boot_ps = rps;
   5476  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
   5477  1.1  riastrad 		rdev->pm.dpm.uvd_ps = rps;
   5478  1.1  riastrad }
   5479  1.1  riastrad 
   5480  1.1  riastrad static void ci_parse_pplib_clock_info(struct radeon_device *rdev,
   5481  1.1  riastrad 				      struct radeon_ps *rps, int index,
   5482  1.1  riastrad 				      union pplib_clock_info *clock_info)
   5483  1.1  riastrad {
   5484  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5485  1.1  riastrad 	struct ci_ps *ps = ci_get_ps(rps);
   5486  1.1  riastrad 	struct ci_pl *pl = &ps->performance_levels[index];
   5487  1.1  riastrad 
   5488  1.1  riastrad 	ps->performance_level_count = index + 1;
   5489  1.1  riastrad 
   5490  1.1  riastrad 	pl->sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
   5491  1.1  riastrad 	pl->sclk |= clock_info->ci.ucEngineClockHigh << 16;
   5492  1.1  riastrad 	pl->mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
   5493  1.1  riastrad 	pl->mclk |= clock_info->ci.ucMemoryClockHigh << 16;
   5494  1.1  riastrad 
   5495  1.1  riastrad 	pl->pcie_gen = r600_get_pcie_gen_support(rdev,
   5496  1.1  riastrad 						 pi->sys_pcie_mask,
   5497  1.1  riastrad 						 pi->vbios_boot_state.pcie_gen_bootup_value,
   5498  1.1  riastrad 						 clock_info->ci.ucPCIEGen);
   5499  1.1  riastrad 	pl->pcie_lane = r600_get_pcie_lane_support(rdev,
   5500  1.1  riastrad 						   pi->vbios_boot_state.pcie_lane_bootup_value,
   5501  1.1  riastrad 						   le16_to_cpu(clock_info->ci.usPCIELane));
   5502  1.1  riastrad 
   5503  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) {
   5504  1.1  riastrad 		pi->acpi_pcie_gen = pl->pcie_gen;
   5505  1.1  riastrad 	}
   5506  1.1  riastrad 
   5507  1.1  riastrad 	if (rps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) {
   5508  1.1  riastrad 		pi->ulv.supported = true;
   5509  1.1  riastrad 		pi->ulv.pl = *pl;
   5510  1.1  riastrad 		pi->ulv.cg_ulv_parameter = CISLANDS_CGULVPARAMETER_DFLT;
   5511  1.1  riastrad 	}
   5512  1.1  riastrad 
   5513  1.1  riastrad 	/* patch up boot state */
   5514  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
   5515  1.1  riastrad 		pl->mclk = pi->vbios_boot_state.mclk_bootup_value;
   5516  1.1  riastrad 		pl->sclk = pi->vbios_boot_state.sclk_bootup_value;
   5517  1.1  riastrad 		pl->pcie_gen = pi->vbios_boot_state.pcie_gen_bootup_value;
   5518  1.1  riastrad 		pl->pcie_lane = pi->vbios_boot_state.pcie_lane_bootup_value;
   5519  1.1  riastrad 	}
   5520  1.1  riastrad 
   5521  1.1  riastrad 	switch (rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) {
   5522  1.1  riastrad 	case ATOM_PPLIB_CLASSIFICATION_UI_BATTERY:
   5523  1.1  riastrad 		pi->use_pcie_powersaving_levels = true;
   5524  1.1  riastrad 		if (pi->pcie_gen_powersaving.max < pl->pcie_gen)
   5525  1.1  riastrad 			pi->pcie_gen_powersaving.max = pl->pcie_gen;
   5526  1.1  riastrad 		if (pi->pcie_gen_powersaving.min > pl->pcie_gen)
   5527  1.1  riastrad 			pi->pcie_gen_powersaving.min = pl->pcie_gen;
   5528  1.1  riastrad 		if (pi->pcie_lane_powersaving.max < pl->pcie_lane)
   5529  1.1  riastrad 			pi->pcie_lane_powersaving.max = pl->pcie_lane;
   5530  1.1  riastrad 		if (pi->pcie_lane_powersaving.min > pl->pcie_lane)
   5531  1.1  riastrad 			pi->pcie_lane_powersaving.min = pl->pcie_lane;
   5532  1.1  riastrad 		break;
   5533  1.1  riastrad 	case ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE:
   5534  1.1  riastrad 		pi->use_pcie_performance_levels = true;
   5535  1.1  riastrad 		if (pi->pcie_gen_performance.max < pl->pcie_gen)
   5536  1.1  riastrad 			pi->pcie_gen_performance.max = pl->pcie_gen;
   5537  1.1  riastrad 		if (pi->pcie_gen_performance.min > pl->pcie_gen)
   5538  1.1  riastrad 			pi->pcie_gen_performance.min = pl->pcie_gen;
   5539  1.1  riastrad 		if (pi->pcie_lane_performance.max < pl->pcie_lane)
   5540  1.1  riastrad 			pi->pcie_lane_performance.max = pl->pcie_lane;
   5541  1.1  riastrad 		if (pi->pcie_lane_performance.min > pl->pcie_lane)
   5542  1.1  riastrad 			pi->pcie_lane_performance.min = pl->pcie_lane;
   5543  1.1  riastrad 		break;
   5544  1.1  riastrad 	default:
   5545  1.1  riastrad 		break;
   5546  1.1  riastrad 	}
   5547  1.1  riastrad }
   5548  1.1  riastrad 
   5549  1.1  riastrad static int ci_parse_power_table(struct radeon_device *rdev)
   5550  1.1  riastrad {
   5551  1.1  riastrad 	struct radeon_mode_info *mode_info = &rdev->mode_info;
   5552  1.1  riastrad 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
   5553  1.1  riastrad 	union pplib_power_state *power_state;
   5554  1.1  riastrad 	int i, j, k, non_clock_array_index, clock_array_index;
   5555  1.1  riastrad 	union pplib_clock_info *clock_info;
   5556  1.1  riastrad 	struct _StateArray *state_array;
   5557  1.1  riastrad 	struct _ClockInfoArray *clock_info_array;
   5558  1.1  riastrad 	struct _NonClockInfoArray *non_clock_info_array;
   5559  1.1  riastrad 	union power_info *power_info;
   5560  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
   5561  1.4  riastrad 	u16 data_offset;
   5562  1.1  riastrad 	u8 frev, crev;
   5563  1.1  riastrad 	u8 *power_state_offset;
   5564  1.1  riastrad 	struct ci_ps *ps;
   5565  1.1  riastrad 
   5566  1.1  riastrad 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
   5567  1.1  riastrad 				   &frev, &crev, &data_offset))
   5568  1.1  riastrad 		return -EINVAL;
   5569  1.1  riastrad 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
   5570  1.1  riastrad 
   5571  1.1  riastrad 	state_array = (struct _StateArray *)
   5572  1.1  riastrad 		(mode_info->atom_context->bios + data_offset +
   5573  1.1  riastrad 		 le16_to_cpu(power_info->pplib.usStateArrayOffset));
   5574  1.1  riastrad 	clock_info_array = (struct _ClockInfoArray *)
   5575  1.1  riastrad 		(mode_info->atom_context->bios + data_offset +
   5576  1.1  riastrad 		 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
   5577  1.1  riastrad 	non_clock_info_array = (struct _NonClockInfoArray *)
   5578  1.1  riastrad 		(mode_info->atom_context->bios + data_offset +
   5579  1.1  riastrad 		 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
   5580  1.1  riastrad 
   5581  1.4  riastrad 	rdev->pm.dpm.ps = kcalloc(state_array->ucNumEntries,
   5582  1.4  riastrad 				  sizeof(struct radeon_ps),
   5583  1.4  riastrad 				  GFP_KERNEL);
   5584  1.1  riastrad 	if (!rdev->pm.dpm.ps)
   5585  1.1  riastrad 		return -ENOMEM;
   5586  1.1  riastrad 	power_state_offset = (u8 *)state_array->states;
   5587  1.1  riastrad 	for (i = 0; i < state_array->ucNumEntries; i++) {
   5588  1.1  riastrad 		u8 *idx;
   5589  1.1  riastrad 		power_state = (union pplib_power_state *)power_state_offset;
   5590  1.1  riastrad 		non_clock_array_index = power_state->v2.nonClockInfoIndex;
   5591  1.1  riastrad 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
   5592  1.1  riastrad 			&non_clock_info_array->nonClockInfo[non_clock_array_index];
   5593  1.1  riastrad 		if (!rdev->pm.power_state[i].clock_info)
   5594  1.1  riastrad 			return -EINVAL;
   5595  1.1  riastrad 		ps = kzalloc(sizeof(struct ci_ps), GFP_KERNEL);
   5596  1.1  riastrad 		if (ps == NULL) {
   5597  1.1  riastrad 			kfree(rdev->pm.dpm.ps);
   5598  1.1  riastrad 			return -ENOMEM;
   5599  1.1  riastrad 		}
   5600  1.1  riastrad 		rdev->pm.dpm.ps[i].ps_priv = ps;
   5601  1.1  riastrad 		ci_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
   5602  1.1  riastrad 					      non_clock_info,
   5603  1.1  riastrad 					      non_clock_info_array->ucEntrySize);
   5604  1.1  riastrad 		k = 0;
   5605  1.1  riastrad 		idx = (u8 *)&power_state->v2.clockInfoIndex[0];
   5606  1.1  riastrad 		for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
   5607  1.1  riastrad 			clock_array_index = idx[j];
   5608  1.1  riastrad 			if (clock_array_index >= clock_info_array->ucNumEntries)
   5609  1.1  riastrad 				continue;
   5610  1.1  riastrad 			if (k >= CISLANDS_MAX_HARDWARE_POWERLEVELS)
   5611  1.1  riastrad 				break;
   5612  1.1  riastrad 			clock_info = (union pplib_clock_info *)
   5613  1.1  riastrad 				((u8 *)&clock_info_array->clockInfo[0] +
   5614  1.1  riastrad 				 (clock_array_index * clock_info_array->ucEntrySize));
   5615  1.1  riastrad 			ci_parse_pplib_clock_info(rdev,
   5616  1.1  riastrad 						  &rdev->pm.dpm.ps[i], k,
   5617  1.1  riastrad 						  clock_info);
   5618  1.1  riastrad 			k++;
   5619  1.1  riastrad 		}
   5620  1.1  riastrad 		power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
   5621  1.1  riastrad 	}
   5622  1.1  riastrad 	rdev->pm.dpm.num_ps = state_array->ucNumEntries;
   5623  1.1  riastrad 
   5624  1.1  riastrad 	/* fill in the vce power states */
   5625  1.1  riastrad 	for (i = 0; i < RADEON_MAX_VCE_LEVELS; i++) {
   5626  1.1  riastrad 		u32 sclk, mclk;
   5627  1.1  riastrad 		clock_array_index = rdev->pm.dpm.vce_states[i].clk_idx;
   5628  1.1  riastrad 		clock_info = (union pplib_clock_info *)
   5629  1.1  riastrad 			&clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
   5630  1.1  riastrad 		sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
   5631  1.1  riastrad 		sclk |= clock_info->ci.ucEngineClockHigh << 16;
   5632  1.1  riastrad 		mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
   5633  1.1  riastrad 		mclk |= clock_info->ci.ucMemoryClockHigh << 16;
   5634  1.1  riastrad 		rdev->pm.dpm.vce_states[i].sclk = sclk;
   5635  1.1  riastrad 		rdev->pm.dpm.vce_states[i].mclk = mclk;
   5636  1.1  riastrad 	}
   5637  1.1  riastrad 
   5638  1.1  riastrad 	return 0;
   5639  1.1  riastrad }
   5640  1.1  riastrad 
   5641  1.1  riastrad static int ci_get_vbios_boot_values(struct radeon_device *rdev,
   5642  1.1  riastrad 				    struct ci_vbios_boot_state *boot_state)
   5643  1.1  riastrad {
   5644  1.1  riastrad 	struct radeon_mode_info *mode_info = &rdev->mode_info;
   5645  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
   5646  1.1  riastrad 	ATOM_FIRMWARE_INFO_V2_2 *firmware_info;
   5647  1.1  riastrad 	u8 frev, crev;
   5648  1.1  riastrad 	u16 data_offset;
   5649  1.1  riastrad 
   5650  1.1  riastrad 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
   5651  1.1  riastrad 				   &frev, &crev, &data_offset)) {
   5652  1.1  riastrad 		firmware_info =
   5653  1.1  riastrad 			(ATOM_FIRMWARE_INFO_V2_2 *)(mode_info->atom_context->bios +
   5654  1.1  riastrad 						    data_offset);
   5655  1.1  riastrad 		boot_state->mvdd_bootup_value = le16_to_cpu(firmware_info->usBootUpMVDDCVoltage);
   5656  1.1  riastrad 		boot_state->vddc_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCVoltage);
   5657  1.1  riastrad 		boot_state->vddci_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCIVoltage);
   5658  1.1  riastrad 		boot_state->pcie_gen_bootup_value = ci_get_current_pcie_speed(rdev);
   5659  1.1  riastrad 		boot_state->pcie_lane_bootup_value = ci_get_current_pcie_lane_number(rdev);
   5660  1.1  riastrad 		boot_state->sclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultEngineClock);
   5661  1.1  riastrad 		boot_state->mclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultMemoryClock);
   5662  1.1  riastrad 
   5663  1.1  riastrad 		return 0;
   5664  1.1  riastrad 	}
   5665  1.1  riastrad 	return -EINVAL;
   5666  1.1  riastrad }
   5667  1.1  riastrad 
   5668  1.1  riastrad void ci_dpm_fini(struct radeon_device *rdev)
   5669  1.1  riastrad {
   5670  1.1  riastrad 	int i;
   5671  1.1  riastrad 
   5672  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
   5673  1.1  riastrad 		kfree(rdev->pm.dpm.ps[i].ps_priv);
   5674  1.1  riastrad 	}
   5675  1.1  riastrad 	kfree(rdev->pm.dpm.ps);
   5676  1.1  riastrad 	kfree(rdev->pm.dpm.priv);
   5677  1.1  riastrad 	kfree(rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries);
   5678  1.1  riastrad 	r600_free_extended_power_table(rdev);
   5679  1.1  riastrad }
   5680  1.1  riastrad 
   5681  1.1  riastrad int ci_dpm_init(struct radeon_device *rdev)
   5682  1.1  riastrad {
   5683  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
   5684  1.1  riastrad 	SMU7_Discrete_DpmTable  *dpm_table;
   5685  1.1  riastrad 	struct radeon_gpio_rec gpio;
   5686  1.1  riastrad 	u16 data_offset, size;
   5687  1.1  riastrad 	u8 frev, crev;
   5688  1.1  riastrad 	struct ci_power_info *pi;
   5689  1.4  riastrad 	enum pci_bus_speed speed_cap = PCI_SPEED_UNKNOWN;
   5690  1.4  riastrad 	struct pci_dev *root = rdev->pdev->bus->self;
   5691  1.1  riastrad 	int ret;
   5692  1.1  riastrad 
   5693  1.1  riastrad 	pi = kzalloc(sizeof(struct ci_power_info), GFP_KERNEL);
   5694  1.1  riastrad 	if (pi == NULL)
   5695  1.1  riastrad 		return -ENOMEM;
   5696  1.1  riastrad 	rdev->pm.dpm.priv = pi;
   5697  1.1  riastrad 
   5698  1.4  riastrad 	if (!pci_is_root_bus(rdev->pdev->bus))
   5699  1.4  riastrad 		speed_cap = pcie_get_speed_cap(root);
   5700  1.4  riastrad 	if (speed_cap == PCI_SPEED_UNKNOWN) {
   5701  1.1  riastrad 		pi->sys_pcie_mask = 0;
   5702  1.4  riastrad 	} else {
   5703  1.4  riastrad 		if (speed_cap == PCIE_SPEED_8_0GT)
   5704  1.4  riastrad 			pi->sys_pcie_mask = RADEON_PCIE_SPEED_25 |
   5705  1.4  riastrad 				RADEON_PCIE_SPEED_50 |
   5706  1.4  riastrad 				RADEON_PCIE_SPEED_80;
   5707  1.4  riastrad 		else if (speed_cap == PCIE_SPEED_5_0GT)
   5708  1.4  riastrad 			pi->sys_pcie_mask = RADEON_PCIE_SPEED_25 |
   5709  1.4  riastrad 				RADEON_PCIE_SPEED_50;
   5710  1.4  riastrad 		else
   5711  1.4  riastrad 			pi->sys_pcie_mask = RADEON_PCIE_SPEED_25;
   5712  1.4  riastrad 	}
   5713  1.1  riastrad 	pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
   5714  1.1  riastrad 
   5715  1.1  riastrad 	pi->pcie_gen_performance.max = RADEON_PCIE_GEN1;
   5716  1.1  riastrad 	pi->pcie_gen_performance.min = RADEON_PCIE_GEN3;
   5717  1.1  riastrad 	pi->pcie_gen_powersaving.max = RADEON_PCIE_GEN1;
   5718  1.1  riastrad 	pi->pcie_gen_powersaving.min = RADEON_PCIE_GEN3;
   5719  1.1  riastrad 
   5720  1.1  riastrad 	pi->pcie_lane_performance.max = 0;
   5721  1.1  riastrad 	pi->pcie_lane_performance.min = 16;
   5722  1.1  riastrad 	pi->pcie_lane_powersaving.max = 0;
   5723  1.1  riastrad 	pi->pcie_lane_powersaving.min = 16;
   5724  1.1  riastrad 
   5725  1.1  riastrad 	ret = ci_get_vbios_boot_values(rdev, &pi->vbios_boot_state);
   5726  1.1  riastrad 	if (ret) {
   5727  1.1  riastrad 		ci_dpm_fini(rdev);
   5728  1.1  riastrad 		return ret;
   5729  1.1  riastrad 	}
   5730  1.1  riastrad 
   5731  1.1  riastrad 	ret = r600_get_platform_caps(rdev);
   5732  1.1  riastrad 	if (ret) {
   5733  1.1  riastrad 		ci_dpm_fini(rdev);
   5734  1.1  riastrad 		return ret;
   5735  1.1  riastrad 	}
   5736  1.1  riastrad 
   5737  1.1  riastrad 	ret = r600_parse_extended_power_table(rdev);
   5738  1.1  riastrad 	if (ret) {
   5739  1.1  riastrad 		ci_dpm_fini(rdev);
   5740  1.1  riastrad 		return ret;
   5741  1.1  riastrad 	}
   5742  1.1  riastrad 
   5743  1.1  riastrad 	ret = ci_parse_power_table(rdev);
   5744  1.1  riastrad 	if (ret) {
   5745  1.1  riastrad 		ci_dpm_fini(rdev);
   5746  1.1  riastrad 		return ret;
   5747  1.1  riastrad 	}
   5748  1.1  riastrad 
   5749  1.4  riastrad 	pi->dll_default_on = false;
   5750  1.4  riastrad 	pi->sram_end = SMC_RAM_END;
   5751  1.1  riastrad 
   5752  1.1  riastrad 	pi->activity_target[0] = CISLAND_TARGETACTIVITY_DFLT;
   5753  1.1  riastrad 	pi->activity_target[1] = CISLAND_TARGETACTIVITY_DFLT;
   5754  1.1  riastrad 	pi->activity_target[2] = CISLAND_TARGETACTIVITY_DFLT;
   5755  1.1  riastrad 	pi->activity_target[3] = CISLAND_TARGETACTIVITY_DFLT;
   5756  1.1  riastrad 	pi->activity_target[4] = CISLAND_TARGETACTIVITY_DFLT;
   5757  1.1  riastrad 	pi->activity_target[5] = CISLAND_TARGETACTIVITY_DFLT;
   5758  1.1  riastrad 	pi->activity_target[6] = CISLAND_TARGETACTIVITY_DFLT;
   5759  1.1  riastrad 	pi->activity_target[7] = CISLAND_TARGETACTIVITY_DFLT;
   5760  1.1  riastrad 
   5761  1.1  riastrad 	pi->mclk_activity_target = CISLAND_MCLK_TARGETACTIVITY_DFLT;
   5762  1.1  riastrad 
   5763  1.1  riastrad 	pi->sclk_dpm_key_disabled = 0;
   5764  1.1  riastrad 	pi->mclk_dpm_key_disabled = 0;
   5765  1.1  riastrad 	pi->pcie_dpm_key_disabled = 0;
   5766  1.1  riastrad 	pi->thermal_sclk_dpm_enabled = 0;
   5767  1.1  riastrad 
   5768  1.1  riastrad 	/* mclk dpm is unstable on some R7 260X cards with the old mc ucode */
   5769  1.1  riastrad 	if ((rdev->pdev->device == 0x6658) &&
   5770  1.1  riastrad 	    (rdev->mc_fw->size == (BONAIRE_MC_UCODE_SIZE * 4))) {
   5771  1.1  riastrad 		pi->mclk_dpm_key_disabled = 1;
   5772  1.1  riastrad 	}
   5773  1.1  riastrad 
   5774  1.1  riastrad 	pi->caps_sclk_ds = true;
   5775  1.1  riastrad 
   5776  1.1  riastrad 	pi->mclk_strobe_mode_threshold = 40000;
   5777  1.1  riastrad 	pi->mclk_stutter_mode_threshold = 40000;
   5778  1.1  riastrad 	pi->mclk_edc_enable_threshold = 40000;
   5779  1.1  riastrad 	pi->mclk_edc_wr_enable_threshold = 40000;
   5780  1.1  riastrad 
   5781  1.1  riastrad 	ci_initialize_powertune_defaults(rdev);
   5782  1.1  riastrad 
   5783  1.1  riastrad 	pi->caps_fps = false;
   5784  1.1  riastrad 
   5785  1.1  riastrad 	pi->caps_sclk_throttle_low_notification = false;
   5786  1.1  riastrad 
   5787  1.1  riastrad 	pi->caps_uvd_dpm = true;
   5788  1.1  riastrad 	pi->caps_vce_dpm = true;
   5789  1.1  riastrad 
   5790  1.4  riastrad 	ci_get_leakage_voltages(rdev);
   5791  1.4  riastrad 	ci_patch_dependency_tables_with_leakage(rdev);
   5792  1.4  riastrad 	ci_set_private_data_variables_based_on_pptable(rdev);
   5793  1.1  riastrad 
   5794  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries =
   5795  1.4  riastrad 		kcalloc(4,
   5796  1.4  riastrad 			sizeof(struct radeon_clock_voltage_dependency_entry),
   5797  1.4  riastrad 			GFP_KERNEL);
   5798  1.1  riastrad 	if (!rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries) {
   5799  1.1  riastrad 		ci_dpm_fini(rdev);
   5800  1.1  riastrad 		return -ENOMEM;
   5801  1.1  riastrad 	}
   5802  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.count = 4;
   5803  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].clk = 0;
   5804  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].v = 0;
   5805  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].clk = 36000;
   5806  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].v = 720;
   5807  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].clk = 54000;
   5808  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].v = 810;
   5809  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].clk = 72000;
   5810  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].v = 900;
   5811  1.1  riastrad 
   5812  1.1  riastrad 	rdev->pm.dpm.dyn_state.mclk_sclk_ratio = 4;
   5813  1.1  riastrad 	rdev->pm.dpm.dyn_state.sclk_mclk_delta = 15000;
   5814  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_vddci_delta = 200;
   5815  1.1  riastrad 
   5816  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_sclk_values.count = 0;
   5817  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_sclk_values.values = NULL;
   5818  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_mclk_values.count = 0;
   5819  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_mclk_values.values = NULL;
   5820  1.1  riastrad 
   5821  1.1  riastrad 	if (rdev->family == CHIP_HAWAII) {
   5822  1.1  riastrad 		pi->thermal_temp_setting.temperature_low = 94500;
   5823  1.1  riastrad 		pi->thermal_temp_setting.temperature_high = 95000;
   5824  1.1  riastrad 		pi->thermal_temp_setting.temperature_shutdown = 104000;
   5825  1.1  riastrad 	} else {
   5826  1.1  riastrad 		pi->thermal_temp_setting.temperature_low = 99500;
   5827  1.1  riastrad 		pi->thermal_temp_setting.temperature_high = 100000;
   5828  1.1  riastrad 		pi->thermal_temp_setting.temperature_shutdown = 104000;
   5829  1.1  riastrad 	}
   5830  1.1  riastrad 
   5831  1.1  riastrad 	pi->uvd_enabled = false;
   5832  1.1  riastrad 
   5833  1.1  riastrad 	dpm_table = &pi->smc_state_table;
   5834  1.1  riastrad 
   5835  1.1  riastrad 	gpio = radeon_atombios_lookup_gpio(rdev, VDDC_VRHOT_GPIO_PINID);
   5836  1.1  riastrad 	if (gpio.valid) {
   5837  1.1  riastrad 		dpm_table->VRHotGpio = gpio.shift;
   5838  1.1  riastrad 		rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
   5839  1.1  riastrad 	} else {
   5840  1.1  riastrad 		dpm_table->VRHotGpio = CISLANDS_UNUSED_GPIO_PIN;
   5841  1.1  riastrad 		rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
   5842  1.1  riastrad 	}
   5843  1.1  riastrad 
   5844  1.1  riastrad 	gpio = radeon_atombios_lookup_gpio(rdev, PP_AC_DC_SWITCH_GPIO_PINID);
   5845  1.1  riastrad 	if (gpio.valid) {
   5846  1.1  riastrad 		dpm_table->AcDcGpio = gpio.shift;
   5847  1.1  riastrad 		rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_HARDWAREDC;
   5848  1.1  riastrad 	} else {
   5849  1.1  riastrad 		dpm_table->AcDcGpio = CISLANDS_UNUSED_GPIO_PIN;
   5850  1.1  riastrad 		rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_HARDWAREDC;
   5851  1.1  riastrad 	}
   5852  1.1  riastrad 
   5853  1.1  riastrad 	gpio = radeon_atombios_lookup_gpio(rdev, VDDC_PCC_GPIO_PINID);
   5854  1.1  riastrad 	if (gpio.valid) {
   5855  1.1  riastrad 		u32 tmp = RREG32_SMC(CNB_PWRMGT_CNTL);
   5856  1.1  riastrad 
   5857  1.1  riastrad 		switch (gpio.shift) {
   5858  1.1  riastrad 		case 0:
   5859  1.1  riastrad 			tmp &= ~GNB_SLOW_MODE_MASK;
   5860  1.1  riastrad 			tmp |= GNB_SLOW_MODE(1);
   5861  1.1  riastrad 			break;
   5862  1.1  riastrad 		case 1:
   5863  1.1  riastrad 			tmp &= ~GNB_SLOW_MODE_MASK;
   5864  1.1  riastrad 			tmp |= GNB_SLOW_MODE(2);
   5865  1.1  riastrad 			break;
   5866  1.1  riastrad 		case 2:
   5867  1.1  riastrad 			tmp |= GNB_SLOW;
   5868  1.1  riastrad 			break;
   5869  1.1  riastrad 		case 3:
   5870  1.1  riastrad 			tmp |= FORCE_NB_PS1;
   5871  1.1  riastrad 			break;
   5872  1.1  riastrad 		case 4:
   5873  1.1  riastrad 			tmp |= DPM_ENABLED;
   5874  1.1  riastrad 			break;
   5875  1.1  riastrad 		default:
   5876  1.1  riastrad 			DRM_DEBUG("Invalid PCC GPIO: %u!\n", gpio.shift);
   5877  1.1  riastrad 			break;
   5878  1.1  riastrad 		}
   5879  1.1  riastrad 		WREG32_SMC(CNB_PWRMGT_CNTL, tmp);
   5880  1.1  riastrad 	}
   5881  1.1  riastrad 
   5882  1.1  riastrad 	pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_NONE;
   5883  1.1  riastrad 	pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_NONE;
   5884  1.1  riastrad 	pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_NONE;
   5885  1.1  riastrad 	if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_GPIO_LUT))
   5886  1.1  riastrad 		pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
   5887  1.1  riastrad 	else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
   5888  1.1  riastrad 		pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
   5889  1.1  riastrad 
   5890  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL) {
   5891  1.1  riastrad 		if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
   5892  1.1  riastrad 			pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
   5893  1.1  riastrad 		else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
   5894  1.1  riastrad 			pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
   5895  1.1  riastrad 		else
   5896  1.1  riastrad 			rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL;
   5897  1.4  riastrad 	}
   5898  1.1  riastrad 
   5899  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_MVDDCONTROL) {
   5900  1.1  riastrad 		if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
   5901  1.1  riastrad 			pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
   5902  1.1  riastrad 		else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_SVID2))
   5903  1.1  riastrad 			pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
   5904  1.1  riastrad 		else
   5905  1.1  riastrad 			rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_MVDDCONTROL;
   5906  1.1  riastrad 	}
   5907  1.1  riastrad 
   5908  1.1  riastrad 	pi->vddc_phase_shed_control = true;
   5909  1.1  riastrad 
   5910  1.1  riastrad #if defined(CONFIG_ACPI)
   5911  1.1  riastrad 	pi->pcie_performance_request =
   5912  1.1  riastrad 		radeon_acpi_is_pcie_performance_request_supported(rdev);
   5913  1.1  riastrad #else
   5914  1.1  riastrad 	pi->pcie_performance_request = false;
   5915  1.1  riastrad #endif
   5916  1.1  riastrad 
   5917  1.1  riastrad 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
   5918  1.4  riastrad 				   &frev, &crev, &data_offset)) {
   5919  1.1  riastrad 		pi->caps_sclk_ss_support = true;
   5920  1.1  riastrad 		pi->caps_mclk_ss_support = true;
   5921  1.1  riastrad 		pi->dynamic_ss = true;
   5922  1.1  riastrad 	} else {
   5923  1.1  riastrad 		pi->caps_sclk_ss_support = false;
   5924  1.1  riastrad 		pi->caps_mclk_ss_support = false;
   5925  1.1  riastrad 		pi->dynamic_ss = true;
   5926  1.1  riastrad 	}
   5927  1.1  riastrad 
   5928  1.1  riastrad 	if (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE)
   5929  1.1  riastrad 		pi->thermal_protection = true;
   5930  1.1  riastrad 	else
   5931  1.1  riastrad 		pi->thermal_protection = false;
   5932  1.1  riastrad 
   5933  1.1  riastrad 	pi->caps_dynamic_ac_timing = true;
   5934  1.1  riastrad 
   5935  1.1  riastrad 	pi->uvd_power_gated = false;
   5936  1.1  riastrad 
   5937  1.1  riastrad 	/* make sure dc limits are valid */
   5938  1.1  riastrad 	if ((rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.sclk == 0) ||
   5939  1.1  riastrad 	    (rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.mclk == 0))
   5940  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc =
   5941  1.1  riastrad 			rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   5942  1.1  riastrad 
   5943  1.1  riastrad 	pi->fan_ctrl_is_in_default_mode = true;
   5944  1.1  riastrad 
   5945  1.1  riastrad 	return 0;
   5946  1.1  riastrad }
   5947  1.1  riastrad 
   5948  1.1  riastrad #ifdef CONFIG_DEBUG_FS
   5949  1.1  riastrad void ci_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
   5950  1.1  riastrad 						    struct seq_file *m)
   5951  1.1  riastrad {
   5952  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5953  1.1  riastrad 	struct radeon_ps *rps = &pi->current_rps;
   5954  1.1  riastrad 	u32 sclk = ci_get_average_sclk_freq(rdev);
   5955  1.1  riastrad 	u32 mclk = ci_get_average_mclk_freq(rdev);
   5956  1.1  riastrad 
   5957  1.1  riastrad 	seq_printf(m, "uvd    %sabled\n", pi->uvd_enabled ? "en" : "dis");
   5958  1.1  riastrad 	seq_printf(m, "vce    %sabled\n", rps->vce_active ? "en" : "dis");
   5959  1.1  riastrad 	seq_printf(m, "power level avg    sclk: %u mclk: %u\n",
   5960  1.1  riastrad 		   sclk, mclk);
   5961  1.1  riastrad }
   5962  1.1  riastrad #endif
   5963  1.1  riastrad 
   5964  1.1  riastrad void ci_dpm_print_power_state(struct radeon_device *rdev,
   5965  1.1  riastrad 			      struct radeon_ps *rps)
   5966  1.1  riastrad {
   5967  1.1  riastrad 	struct ci_ps *ps = ci_get_ps(rps);
   5968  1.1  riastrad 	struct ci_pl *pl;
   5969  1.1  riastrad 	int i;
   5970  1.1  riastrad 
   5971  1.1  riastrad 	r600_dpm_print_class_info(rps->class, rps->class2);
   5972  1.1  riastrad 	r600_dpm_print_cap_info(rps->caps);
   5973  1.1  riastrad 	printk("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   5974  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++) {
   5975  1.1  riastrad 		pl = &ps->performance_levels[i];
   5976  1.1  riastrad 		printk("\t\tpower level %d    sclk: %u mclk: %u pcie gen: %u pcie lanes: %u\n",
   5977  1.1  riastrad 		       i, pl->sclk, pl->mclk, pl->pcie_gen + 1, pl->pcie_lane);
   5978  1.1  riastrad 	}
   5979  1.1  riastrad 	r600_dpm_print_ps_status(rdev, rps);
   5980  1.1  riastrad }
   5981  1.1  riastrad 
   5982  1.1  riastrad u32 ci_dpm_get_current_sclk(struct radeon_device *rdev)
   5983  1.1  riastrad {
   5984  1.1  riastrad 	u32 sclk = ci_get_average_sclk_freq(rdev);
   5985  1.1  riastrad 
   5986  1.1  riastrad 	return sclk;
   5987  1.1  riastrad }
   5988  1.1  riastrad 
   5989  1.1  riastrad u32 ci_dpm_get_current_mclk(struct radeon_device *rdev)
   5990  1.1  riastrad {
   5991  1.1  riastrad 	u32 mclk = ci_get_average_mclk_freq(rdev);
   5992  1.1  riastrad 
   5993  1.1  riastrad 	return mclk;
   5994  1.1  riastrad }
   5995  1.1  riastrad 
   5996  1.1  riastrad u32 ci_dpm_get_sclk(struct radeon_device *rdev, bool low)
   5997  1.1  riastrad {
   5998  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   5999  1.1  riastrad 	struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
   6000  1.1  riastrad 
   6001  1.1  riastrad 	if (low)
   6002  1.1  riastrad 		return requested_state->performance_levels[0].sclk;
   6003  1.1  riastrad 	else
   6004  1.1  riastrad 		return requested_state->performance_levels[requested_state->performance_level_count - 1].sclk;
   6005  1.1  riastrad }
   6006  1.1  riastrad 
   6007  1.1  riastrad u32 ci_dpm_get_mclk(struct radeon_device *rdev, bool low)
   6008  1.1  riastrad {
   6009  1.1  riastrad 	struct ci_power_info *pi = ci_get_pi(rdev);
   6010  1.1  riastrad 	struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
   6011  1.1  riastrad 
   6012  1.1  riastrad 	if (low)
   6013  1.1  riastrad 		return requested_state->performance_levels[0].mclk;
   6014  1.1  riastrad 	else
   6015  1.1  riastrad 		return requested_state->performance_levels[requested_state->performance_level_count - 1].mclk;
   6016  1.1  riastrad }
   6017