Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_ni_dpm.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2012 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.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_ni_dpm.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $");
     28  1.1  riastrad 
     29  1.2  riastrad #include <linux/math64.h>
     30  1.2  riastrad #include <linux/pci.h>
     31  1.2  riastrad #include <linux/seq_file.h>
     32  1.2  riastrad 
     33  1.2  riastrad #include "atom.h"
     34  1.2  riastrad #include "ni_dpm.h"
     35  1.2  riastrad #include "nid.h"
     36  1.2  riastrad #include "r600_dpm.h"
     37  1.1  riastrad #include "radeon.h"
     38  1.1  riastrad #include "radeon_asic.h"
     39  1.1  riastrad 
     40  1.1  riastrad #define MC_CG_ARB_FREQ_F0           0x0a
     41  1.1  riastrad #define MC_CG_ARB_FREQ_F1           0x0b
     42  1.1  riastrad #define MC_CG_ARB_FREQ_F2           0x0c
     43  1.1  riastrad #define MC_CG_ARB_FREQ_F3           0x0d
     44  1.1  riastrad 
     45  1.1  riastrad #define SMC_RAM_END 0xC000
     46  1.1  riastrad 
     47  1.1  riastrad static const struct ni_cac_weights cac_weights_cayman_xt =
     48  1.1  riastrad {
     49  1.1  riastrad 	0x15,
     50  1.1  riastrad 	0x2,
     51  1.1  riastrad 	0x19,
     52  1.1  riastrad 	0x2,
     53  1.1  riastrad 	0x8,
     54  1.1  riastrad 	0x14,
     55  1.1  riastrad 	0x2,
     56  1.1  riastrad 	0x16,
     57  1.1  riastrad 	0xE,
     58  1.1  riastrad 	0x17,
     59  1.1  riastrad 	0x13,
     60  1.1  riastrad 	0x2B,
     61  1.1  riastrad 	0x10,
     62  1.1  riastrad 	0x7,
     63  1.1  riastrad 	0x5,
     64  1.1  riastrad 	0x5,
     65  1.1  riastrad 	0x5,
     66  1.1  riastrad 	0x2,
     67  1.1  riastrad 	0x3,
     68  1.1  riastrad 	0x9,
     69  1.1  riastrad 	0x10,
     70  1.1  riastrad 	0x10,
     71  1.1  riastrad 	0x2B,
     72  1.1  riastrad 	0xA,
     73  1.1  riastrad 	0x9,
     74  1.1  riastrad 	0x4,
     75  1.1  riastrad 	0xD,
     76  1.1  riastrad 	0xD,
     77  1.1  riastrad 	0x3E,
     78  1.1  riastrad 	0x18,
     79  1.1  riastrad 	0x14,
     80  1.1  riastrad 	0,
     81  1.1  riastrad 	0x3,
     82  1.1  riastrad 	0x3,
     83  1.1  riastrad 	0x5,
     84  1.1  riastrad 	0,
     85  1.1  riastrad 	0x2,
     86  1.1  riastrad 	0,
     87  1.1  riastrad 	0,
     88  1.1  riastrad 	0,
     89  1.1  riastrad 	0,
     90  1.1  riastrad 	0,
     91  1.1  riastrad 	0,
     92  1.1  riastrad 	0,
     93  1.1  riastrad 	0,
     94  1.1  riastrad 	0,
     95  1.1  riastrad 	0x1CC,
     96  1.1  riastrad 	0,
     97  1.1  riastrad 	0x164,
     98  1.1  riastrad 	1,
     99  1.1  riastrad 	1,
    100  1.1  riastrad 	1,
    101  1.1  riastrad 	1,
    102  1.1  riastrad 	12,
    103  1.1  riastrad 	12,
    104  1.1  riastrad 	12,
    105  1.1  riastrad 	0x12,
    106  1.1  riastrad 	0x1F,
    107  1.1  riastrad 	132,
    108  1.1  riastrad 	5,
    109  1.1  riastrad 	7,
    110  1.1  riastrad 	0,
    111  1.1  riastrad 	{ 0, 0, 0, 0, 0, 0, 0, 0 },
    112  1.1  riastrad 	{ 0, 0, 0, 0 },
    113  1.1  riastrad 	true
    114  1.1  riastrad };
    115  1.1  riastrad 
    116  1.1  riastrad static const struct ni_cac_weights cac_weights_cayman_pro =
    117  1.1  riastrad {
    118  1.1  riastrad 	0x16,
    119  1.1  riastrad 	0x4,
    120  1.1  riastrad 	0x10,
    121  1.1  riastrad 	0x2,
    122  1.1  riastrad 	0xA,
    123  1.1  riastrad 	0x16,
    124  1.1  riastrad 	0x2,
    125  1.1  riastrad 	0x18,
    126  1.1  riastrad 	0x10,
    127  1.1  riastrad 	0x1A,
    128  1.1  riastrad 	0x16,
    129  1.1  riastrad 	0x2D,
    130  1.1  riastrad 	0x12,
    131  1.1  riastrad 	0xA,
    132  1.1  riastrad 	0x6,
    133  1.1  riastrad 	0x6,
    134  1.1  riastrad 	0x6,
    135  1.1  riastrad 	0x2,
    136  1.1  riastrad 	0x4,
    137  1.1  riastrad 	0xB,
    138  1.1  riastrad 	0x11,
    139  1.1  riastrad 	0x11,
    140  1.1  riastrad 	0x2D,
    141  1.1  riastrad 	0xC,
    142  1.1  riastrad 	0xC,
    143  1.1  riastrad 	0x7,
    144  1.1  riastrad 	0x10,
    145  1.1  riastrad 	0x10,
    146  1.1  riastrad 	0x3F,
    147  1.1  riastrad 	0x1A,
    148  1.1  riastrad 	0x16,
    149  1.1  riastrad 	0,
    150  1.1  riastrad 	0x7,
    151  1.1  riastrad 	0x4,
    152  1.1  riastrad 	0x6,
    153  1.1  riastrad 	1,
    154  1.1  riastrad 	0x2,
    155  1.1  riastrad 	0x1,
    156  1.1  riastrad 	0,
    157  1.1  riastrad 	0,
    158  1.1  riastrad 	0,
    159  1.1  riastrad 	0,
    160  1.1  riastrad 	0,
    161  1.1  riastrad 	0,
    162  1.1  riastrad 	0x30,
    163  1.1  riastrad 	0,
    164  1.1  riastrad 	0x1CF,
    165  1.1  riastrad 	0,
    166  1.1  riastrad 	0x166,
    167  1.1  riastrad 	1,
    168  1.1  riastrad 	1,
    169  1.1  riastrad 	1,
    170  1.1  riastrad 	1,
    171  1.1  riastrad 	12,
    172  1.1  riastrad 	12,
    173  1.1  riastrad 	12,
    174  1.1  riastrad 	0x15,
    175  1.1  riastrad 	0x1F,
    176  1.1  riastrad 	132,
    177  1.1  riastrad 	6,
    178  1.1  riastrad 	6,
    179  1.1  riastrad 	0,
    180  1.1  riastrad 	{ 0, 0, 0, 0, 0, 0, 0, 0 },
    181  1.1  riastrad 	{ 0, 0, 0, 0 },
    182  1.1  riastrad 	true
    183  1.1  riastrad };
    184  1.1  riastrad 
    185  1.1  riastrad static const struct ni_cac_weights cac_weights_cayman_le =
    186  1.1  riastrad {
    187  1.1  riastrad 	0x7,
    188  1.1  riastrad 	0xE,
    189  1.1  riastrad 	0x1,
    190  1.1  riastrad 	0xA,
    191  1.1  riastrad 	0x1,
    192  1.1  riastrad 	0x3F,
    193  1.1  riastrad 	0x2,
    194  1.1  riastrad 	0x18,
    195  1.1  riastrad 	0x10,
    196  1.1  riastrad 	0x1A,
    197  1.1  riastrad 	0x1,
    198  1.1  riastrad 	0x3F,
    199  1.1  riastrad 	0x1,
    200  1.1  riastrad 	0xE,
    201  1.1  riastrad 	0x6,
    202  1.1  riastrad 	0x6,
    203  1.1  riastrad 	0x6,
    204  1.1  riastrad 	0x2,
    205  1.1  riastrad 	0x4,
    206  1.1  riastrad 	0x9,
    207  1.1  riastrad 	0x1A,
    208  1.1  riastrad 	0x1A,
    209  1.1  riastrad 	0x2C,
    210  1.1  riastrad 	0xA,
    211  1.1  riastrad 	0x11,
    212  1.1  riastrad 	0x8,
    213  1.1  riastrad 	0x19,
    214  1.1  riastrad 	0x19,
    215  1.1  riastrad 	0x1,
    216  1.1  riastrad 	0x1,
    217  1.1  riastrad 	0x1A,
    218  1.1  riastrad 	0,
    219  1.1  riastrad 	0x8,
    220  1.1  riastrad 	0x5,
    221  1.1  riastrad 	0x8,
    222  1.1  riastrad 	0x1,
    223  1.1  riastrad 	0x3,
    224  1.1  riastrad 	0x1,
    225  1.1  riastrad 	0,
    226  1.1  riastrad 	0,
    227  1.1  riastrad 	0,
    228  1.1  riastrad 	0,
    229  1.1  riastrad 	0,
    230  1.1  riastrad 	0,
    231  1.1  riastrad 	0x38,
    232  1.1  riastrad 	0x38,
    233  1.1  riastrad 	0x239,
    234  1.1  riastrad 	0x3,
    235  1.1  riastrad 	0x18A,
    236  1.1  riastrad 	1,
    237  1.1  riastrad 	1,
    238  1.1  riastrad 	1,
    239  1.1  riastrad 	1,
    240  1.1  riastrad 	12,
    241  1.1  riastrad 	12,
    242  1.1  riastrad 	12,
    243  1.1  riastrad 	0x15,
    244  1.1  riastrad 	0x22,
    245  1.1  riastrad 	132,
    246  1.1  riastrad 	6,
    247  1.1  riastrad 	6,
    248  1.1  riastrad 	0,
    249  1.1  riastrad 	{ 0, 0, 0, 0, 0, 0, 0, 0 },
    250  1.1  riastrad 	{ 0, 0, 0, 0 },
    251  1.1  riastrad 	true
    252  1.1  riastrad };
    253  1.1  riastrad 
    254  1.1  riastrad #define NISLANDS_MGCG_SEQUENCE  300
    255  1.1  riastrad 
    256  1.1  riastrad static const u32 cayman_cgcg_cgls_default[] =
    257  1.1  riastrad {
    258  1.1  riastrad 	0x000008f8, 0x00000010, 0xffffffff,
    259  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    260  1.1  riastrad 	0x000008f8, 0x00000011, 0xffffffff,
    261  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    262  1.1  riastrad 	0x000008f8, 0x00000012, 0xffffffff,
    263  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    264  1.1  riastrad 	0x000008f8, 0x00000013, 0xffffffff,
    265  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    266  1.1  riastrad 	0x000008f8, 0x00000014, 0xffffffff,
    267  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    268  1.1  riastrad 	0x000008f8, 0x00000015, 0xffffffff,
    269  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    270  1.1  riastrad 	0x000008f8, 0x00000016, 0xffffffff,
    271  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    272  1.1  riastrad 	0x000008f8, 0x00000017, 0xffffffff,
    273  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    274  1.1  riastrad 	0x000008f8, 0x00000018, 0xffffffff,
    275  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    276  1.1  riastrad 	0x000008f8, 0x00000019, 0xffffffff,
    277  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    278  1.1  riastrad 	0x000008f8, 0x0000001a, 0xffffffff,
    279  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    280  1.1  riastrad 	0x000008f8, 0x0000001b, 0xffffffff,
    281  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    282  1.1  riastrad 	0x000008f8, 0x00000020, 0xffffffff,
    283  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    284  1.1  riastrad 	0x000008f8, 0x00000021, 0xffffffff,
    285  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    286  1.1  riastrad 	0x000008f8, 0x00000022, 0xffffffff,
    287  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    288  1.1  riastrad 	0x000008f8, 0x00000023, 0xffffffff,
    289  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    290  1.1  riastrad 	0x000008f8, 0x00000024, 0xffffffff,
    291  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    292  1.1  riastrad 	0x000008f8, 0x00000025, 0xffffffff,
    293  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    294  1.1  riastrad 	0x000008f8, 0x00000026, 0xffffffff,
    295  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    296  1.1  riastrad 	0x000008f8, 0x00000027, 0xffffffff,
    297  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    298  1.1  riastrad 	0x000008f8, 0x00000028, 0xffffffff,
    299  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    300  1.1  riastrad 	0x000008f8, 0x00000029, 0xffffffff,
    301  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    302  1.1  riastrad 	0x000008f8, 0x0000002a, 0xffffffff,
    303  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    304  1.1  riastrad 	0x000008f8, 0x0000002b, 0xffffffff,
    305  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff
    306  1.1  riastrad };
    307  1.1  riastrad #define CAYMAN_CGCG_CGLS_DEFAULT_LENGTH sizeof(cayman_cgcg_cgls_default) / (3 * sizeof(u32))
    308  1.1  riastrad 
    309  1.1  riastrad static const u32 cayman_cgcg_cgls_disable[] =
    310  1.1  riastrad {
    311  1.1  riastrad 	0x000008f8, 0x00000010, 0xffffffff,
    312  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    313  1.1  riastrad 	0x000008f8, 0x00000011, 0xffffffff,
    314  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    315  1.1  riastrad 	0x000008f8, 0x00000012, 0xffffffff,
    316  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    317  1.1  riastrad 	0x000008f8, 0x00000013, 0xffffffff,
    318  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    319  1.1  riastrad 	0x000008f8, 0x00000014, 0xffffffff,
    320  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    321  1.1  riastrad 	0x000008f8, 0x00000015, 0xffffffff,
    322  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    323  1.1  riastrad 	0x000008f8, 0x00000016, 0xffffffff,
    324  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    325  1.1  riastrad 	0x000008f8, 0x00000017, 0xffffffff,
    326  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    327  1.1  riastrad 	0x000008f8, 0x00000018, 0xffffffff,
    328  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    329  1.1  riastrad 	0x000008f8, 0x00000019, 0xffffffff,
    330  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    331  1.1  riastrad 	0x000008f8, 0x0000001a, 0xffffffff,
    332  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    333  1.1  riastrad 	0x000008f8, 0x0000001b, 0xffffffff,
    334  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    335  1.1  riastrad 	0x000008f8, 0x00000020, 0xffffffff,
    336  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    337  1.1  riastrad 	0x000008f8, 0x00000021, 0xffffffff,
    338  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    339  1.1  riastrad 	0x000008f8, 0x00000022, 0xffffffff,
    340  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    341  1.1  riastrad 	0x000008f8, 0x00000023, 0xffffffff,
    342  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    343  1.1  riastrad 	0x000008f8, 0x00000024, 0xffffffff,
    344  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    345  1.1  riastrad 	0x000008f8, 0x00000025, 0xffffffff,
    346  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    347  1.1  riastrad 	0x000008f8, 0x00000026, 0xffffffff,
    348  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    349  1.1  riastrad 	0x000008f8, 0x00000027, 0xffffffff,
    350  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    351  1.1  riastrad 	0x000008f8, 0x00000028, 0xffffffff,
    352  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    353  1.1  riastrad 	0x000008f8, 0x00000029, 0xffffffff,
    354  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    355  1.1  riastrad 	0x000008f8, 0x0000002a, 0xffffffff,
    356  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    357  1.1  riastrad 	0x000008f8, 0x0000002b, 0xffffffff,
    358  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    359  1.1  riastrad 	0x00000644, 0x000f7902, 0x001f4180,
    360  1.1  riastrad 	0x00000644, 0x000f3802, 0x001f4180
    361  1.1  riastrad };
    362  1.1  riastrad #define CAYMAN_CGCG_CGLS_DISABLE_LENGTH sizeof(cayman_cgcg_cgls_disable) / (3 * sizeof(u32))
    363  1.1  riastrad 
    364  1.1  riastrad static const u32 cayman_cgcg_cgls_enable[] =
    365  1.1  riastrad {
    366  1.1  riastrad 	0x00000644, 0x000f7882, 0x001f4080,
    367  1.1  riastrad 	0x000008f8, 0x00000010, 0xffffffff,
    368  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    369  1.1  riastrad 	0x000008f8, 0x00000011, 0xffffffff,
    370  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    371  1.1  riastrad 	0x000008f8, 0x00000012, 0xffffffff,
    372  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    373  1.1  riastrad 	0x000008f8, 0x00000013, 0xffffffff,
    374  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    375  1.1  riastrad 	0x000008f8, 0x00000014, 0xffffffff,
    376  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    377  1.1  riastrad 	0x000008f8, 0x00000015, 0xffffffff,
    378  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    379  1.1  riastrad 	0x000008f8, 0x00000016, 0xffffffff,
    380  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    381  1.1  riastrad 	0x000008f8, 0x00000017, 0xffffffff,
    382  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    383  1.1  riastrad 	0x000008f8, 0x00000018, 0xffffffff,
    384  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    385  1.1  riastrad 	0x000008f8, 0x00000019, 0xffffffff,
    386  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    387  1.1  riastrad 	0x000008f8, 0x0000001a, 0xffffffff,
    388  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    389  1.1  riastrad 	0x000008f8, 0x0000001b, 0xffffffff,
    390  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    391  1.1  riastrad 	0x000008f8, 0x00000020, 0xffffffff,
    392  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    393  1.1  riastrad 	0x000008f8, 0x00000021, 0xffffffff,
    394  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    395  1.1  riastrad 	0x000008f8, 0x00000022, 0xffffffff,
    396  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    397  1.1  riastrad 	0x000008f8, 0x00000023, 0xffffffff,
    398  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    399  1.1  riastrad 	0x000008f8, 0x00000024, 0xffffffff,
    400  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    401  1.1  riastrad 	0x000008f8, 0x00000025, 0xffffffff,
    402  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    403  1.1  riastrad 	0x000008f8, 0x00000026, 0xffffffff,
    404  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    405  1.1  riastrad 	0x000008f8, 0x00000027, 0xffffffff,
    406  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    407  1.1  riastrad 	0x000008f8, 0x00000028, 0xffffffff,
    408  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    409  1.1  riastrad 	0x000008f8, 0x00000029, 0xffffffff,
    410  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    411  1.1  riastrad 	0x000008f8, 0x0000002a, 0xffffffff,
    412  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    413  1.1  riastrad 	0x000008f8, 0x0000002b, 0xffffffff,
    414  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff
    415  1.1  riastrad };
    416  1.1  riastrad #define CAYMAN_CGCG_CGLS_ENABLE_LENGTH  sizeof(cayman_cgcg_cgls_enable) / (3 * sizeof(u32))
    417  1.1  riastrad 
    418  1.1  riastrad static const u32 cayman_mgcg_default[] =
    419  1.1  riastrad {
    420  1.1  riastrad 	0x0000802c, 0xc0000000, 0xffffffff,
    421  1.1  riastrad 	0x00003fc4, 0xc0000000, 0xffffffff,
    422  1.1  riastrad 	0x00005448, 0x00000100, 0xffffffff,
    423  1.1  riastrad 	0x000055e4, 0x00000100, 0xffffffff,
    424  1.1  riastrad 	0x0000160c, 0x00000100, 0xffffffff,
    425  1.1  riastrad 	0x00008984, 0x06000100, 0xffffffff,
    426  1.1  riastrad 	0x0000c164, 0x00000100, 0xffffffff,
    427  1.1  riastrad 	0x00008a18, 0x00000100, 0xffffffff,
    428  1.1  riastrad 	0x0000897c, 0x06000100, 0xffffffff,
    429  1.1  riastrad 	0x00008b28, 0x00000100, 0xffffffff,
    430  1.1  riastrad 	0x00009144, 0x00800200, 0xffffffff,
    431  1.1  riastrad 	0x00009a60, 0x00000100, 0xffffffff,
    432  1.1  riastrad 	0x00009868, 0x00000100, 0xffffffff,
    433  1.1  riastrad 	0x00008d58, 0x00000100, 0xffffffff,
    434  1.1  riastrad 	0x00009510, 0x00000100, 0xffffffff,
    435  1.1  riastrad 	0x0000949c, 0x00000100, 0xffffffff,
    436  1.1  riastrad 	0x00009654, 0x00000100, 0xffffffff,
    437  1.1  riastrad 	0x00009030, 0x00000100, 0xffffffff,
    438  1.1  riastrad 	0x00009034, 0x00000100, 0xffffffff,
    439  1.1  riastrad 	0x00009038, 0x00000100, 0xffffffff,
    440  1.1  riastrad 	0x0000903c, 0x00000100, 0xffffffff,
    441  1.1  riastrad 	0x00009040, 0x00000100, 0xffffffff,
    442  1.1  riastrad 	0x0000a200, 0x00000100, 0xffffffff,
    443  1.1  riastrad 	0x0000a204, 0x00000100, 0xffffffff,
    444  1.1  riastrad 	0x0000a208, 0x00000100, 0xffffffff,
    445  1.1  riastrad 	0x0000a20c, 0x00000100, 0xffffffff,
    446  1.1  riastrad 	0x00009744, 0x00000100, 0xffffffff,
    447  1.1  riastrad 	0x00003f80, 0x00000100, 0xffffffff,
    448  1.1  riastrad 	0x0000a210, 0x00000100, 0xffffffff,
    449  1.1  riastrad 	0x0000a214, 0x00000100, 0xffffffff,
    450  1.1  riastrad 	0x000004d8, 0x00000100, 0xffffffff,
    451  1.1  riastrad 	0x00009664, 0x00000100, 0xffffffff,
    452  1.1  riastrad 	0x00009698, 0x00000100, 0xffffffff,
    453  1.1  riastrad 	0x000004d4, 0x00000200, 0xffffffff,
    454  1.1  riastrad 	0x000004d0, 0x00000000, 0xffffffff,
    455  1.1  riastrad 	0x000030cc, 0x00000104, 0xffffffff,
    456  1.1  riastrad 	0x0000d0c0, 0x00000100, 0xffffffff,
    457  1.1  riastrad 	0x0000d8c0, 0x00000100, 0xffffffff,
    458  1.1  riastrad 	0x0000802c, 0x40000000, 0xffffffff,
    459  1.1  riastrad 	0x00003fc4, 0x40000000, 0xffffffff,
    460  1.1  riastrad 	0x0000915c, 0x00010000, 0xffffffff,
    461  1.1  riastrad 	0x00009160, 0x00030002, 0xffffffff,
    462  1.1  riastrad 	0x00009164, 0x00050004, 0xffffffff,
    463  1.1  riastrad 	0x00009168, 0x00070006, 0xffffffff,
    464  1.1  riastrad 	0x00009178, 0x00070000, 0xffffffff,
    465  1.1  riastrad 	0x0000917c, 0x00030002, 0xffffffff,
    466  1.1  riastrad 	0x00009180, 0x00050004, 0xffffffff,
    467  1.1  riastrad 	0x0000918c, 0x00010006, 0xffffffff,
    468  1.1  riastrad 	0x00009190, 0x00090008, 0xffffffff,
    469  1.1  riastrad 	0x00009194, 0x00070000, 0xffffffff,
    470  1.1  riastrad 	0x00009198, 0x00030002, 0xffffffff,
    471  1.1  riastrad 	0x0000919c, 0x00050004, 0xffffffff,
    472  1.1  riastrad 	0x000091a8, 0x00010006, 0xffffffff,
    473  1.1  riastrad 	0x000091ac, 0x00090008, 0xffffffff,
    474  1.1  riastrad 	0x000091b0, 0x00070000, 0xffffffff,
    475  1.1  riastrad 	0x000091b4, 0x00030002, 0xffffffff,
    476  1.1  riastrad 	0x000091b8, 0x00050004, 0xffffffff,
    477  1.1  riastrad 	0x000091c4, 0x00010006, 0xffffffff,
    478  1.1  riastrad 	0x000091c8, 0x00090008, 0xffffffff,
    479  1.1  riastrad 	0x000091cc, 0x00070000, 0xffffffff,
    480  1.1  riastrad 	0x000091d0, 0x00030002, 0xffffffff,
    481  1.1  riastrad 	0x000091d4, 0x00050004, 0xffffffff,
    482  1.1  riastrad 	0x000091e0, 0x00010006, 0xffffffff,
    483  1.1  riastrad 	0x000091e4, 0x00090008, 0xffffffff,
    484  1.1  riastrad 	0x000091e8, 0x00000000, 0xffffffff,
    485  1.1  riastrad 	0x000091ec, 0x00070000, 0xffffffff,
    486  1.1  riastrad 	0x000091f0, 0x00030002, 0xffffffff,
    487  1.1  riastrad 	0x000091f4, 0x00050004, 0xffffffff,
    488  1.1  riastrad 	0x00009200, 0x00010006, 0xffffffff,
    489  1.1  riastrad 	0x00009204, 0x00090008, 0xffffffff,
    490  1.1  riastrad 	0x00009208, 0x00070000, 0xffffffff,
    491  1.1  riastrad 	0x0000920c, 0x00030002, 0xffffffff,
    492  1.1  riastrad 	0x00009210, 0x00050004, 0xffffffff,
    493  1.1  riastrad 	0x0000921c, 0x00010006, 0xffffffff,
    494  1.1  riastrad 	0x00009220, 0x00090008, 0xffffffff,
    495  1.1  riastrad 	0x00009224, 0x00070000, 0xffffffff,
    496  1.1  riastrad 	0x00009228, 0x00030002, 0xffffffff,
    497  1.1  riastrad 	0x0000922c, 0x00050004, 0xffffffff,
    498  1.1  riastrad 	0x00009238, 0x00010006, 0xffffffff,
    499  1.1  riastrad 	0x0000923c, 0x00090008, 0xffffffff,
    500  1.1  riastrad 	0x00009240, 0x00070000, 0xffffffff,
    501  1.1  riastrad 	0x00009244, 0x00030002, 0xffffffff,
    502  1.1  riastrad 	0x00009248, 0x00050004, 0xffffffff,
    503  1.1  riastrad 	0x00009254, 0x00010006, 0xffffffff,
    504  1.1  riastrad 	0x00009258, 0x00090008, 0xffffffff,
    505  1.1  riastrad 	0x0000925c, 0x00070000, 0xffffffff,
    506  1.1  riastrad 	0x00009260, 0x00030002, 0xffffffff,
    507  1.1  riastrad 	0x00009264, 0x00050004, 0xffffffff,
    508  1.1  riastrad 	0x00009270, 0x00010006, 0xffffffff,
    509  1.1  riastrad 	0x00009274, 0x00090008, 0xffffffff,
    510  1.1  riastrad 	0x00009278, 0x00070000, 0xffffffff,
    511  1.1  riastrad 	0x0000927c, 0x00030002, 0xffffffff,
    512  1.1  riastrad 	0x00009280, 0x00050004, 0xffffffff,
    513  1.1  riastrad 	0x0000928c, 0x00010006, 0xffffffff,
    514  1.1  riastrad 	0x00009290, 0x00090008, 0xffffffff,
    515  1.1  riastrad 	0x000092a8, 0x00070000, 0xffffffff,
    516  1.1  riastrad 	0x000092ac, 0x00030002, 0xffffffff,
    517  1.1  riastrad 	0x000092b0, 0x00050004, 0xffffffff,
    518  1.1  riastrad 	0x000092bc, 0x00010006, 0xffffffff,
    519  1.1  riastrad 	0x000092c0, 0x00090008, 0xffffffff,
    520  1.1  riastrad 	0x000092c4, 0x00070000, 0xffffffff,
    521  1.1  riastrad 	0x000092c8, 0x00030002, 0xffffffff,
    522  1.1  riastrad 	0x000092cc, 0x00050004, 0xffffffff,
    523  1.1  riastrad 	0x000092d8, 0x00010006, 0xffffffff,
    524  1.1  riastrad 	0x000092dc, 0x00090008, 0xffffffff,
    525  1.1  riastrad 	0x00009294, 0x00000000, 0xffffffff,
    526  1.1  riastrad 	0x0000802c, 0x40010000, 0xffffffff,
    527  1.1  riastrad 	0x00003fc4, 0x40010000, 0xffffffff,
    528  1.1  riastrad 	0x0000915c, 0x00010000, 0xffffffff,
    529  1.1  riastrad 	0x00009160, 0x00030002, 0xffffffff,
    530  1.1  riastrad 	0x00009164, 0x00050004, 0xffffffff,
    531  1.1  riastrad 	0x00009168, 0x00070006, 0xffffffff,
    532  1.1  riastrad 	0x00009178, 0x00070000, 0xffffffff,
    533  1.1  riastrad 	0x0000917c, 0x00030002, 0xffffffff,
    534  1.1  riastrad 	0x00009180, 0x00050004, 0xffffffff,
    535  1.1  riastrad 	0x0000918c, 0x00010006, 0xffffffff,
    536  1.1  riastrad 	0x00009190, 0x00090008, 0xffffffff,
    537  1.1  riastrad 	0x00009194, 0x00070000, 0xffffffff,
    538  1.1  riastrad 	0x00009198, 0x00030002, 0xffffffff,
    539  1.1  riastrad 	0x0000919c, 0x00050004, 0xffffffff,
    540  1.1  riastrad 	0x000091a8, 0x00010006, 0xffffffff,
    541  1.1  riastrad 	0x000091ac, 0x00090008, 0xffffffff,
    542  1.1  riastrad 	0x000091b0, 0x00070000, 0xffffffff,
    543  1.1  riastrad 	0x000091b4, 0x00030002, 0xffffffff,
    544  1.1  riastrad 	0x000091b8, 0x00050004, 0xffffffff,
    545  1.1  riastrad 	0x000091c4, 0x00010006, 0xffffffff,
    546  1.1  riastrad 	0x000091c8, 0x00090008, 0xffffffff,
    547  1.1  riastrad 	0x000091cc, 0x00070000, 0xffffffff,
    548  1.1  riastrad 	0x000091d0, 0x00030002, 0xffffffff,
    549  1.1  riastrad 	0x000091d4, 0x00050004, 0xffffffff,
    550  1.1  riastrad 	0x000091e0, 0x00010006, 0xffffffff,
    551  1.1  riastrad 	0x000091e4, 0x00090008, 0xffffffff,
    552  1.1  riastrad 	0x000091e8, 0x00000000, 0xffffffff,
    553  1.1  riastrad 	0x000091ec, 0x00070000, 0xffffffff,
    554  1.1  riastrad 	0x000091f0, 0x00030002, 0xffffffff,
    555  1.1  riastrad 	0x000091f4, 0x00050004, 0xffffffff,
    556  1.1  riastrad 	0x00009200, 0x00010006, 0xffffffff,
    557  1.1  riastrad 	0x00009204, 0x00090008, 0xffffffff,
    558  1.1  riastrad 	0x00009208, 0x00070000, 0xffffffff,
    559  1.1  riastrad 	0x0000920c, 0x00030002, 0xffffffff,
    560  1.1  riastrad 	0x00009210, 0x00050004, 0xffffffff,
    561  1.1  riastrad 	0x0000921c, 0x00010006, 0xffffffff,
    562  1.1  riastrad 	0x00009220, 0x00090008, 0xffffffff,
    563  1.1  riastrad 	0x00009224, 0x00070000, 0xffffffff,
    564  1.1  riastrad 	0x00009228, 0x00030002, 0xffffffff,
    565  1.1  riastrad 	0x0000922c, 0x00050004, 0xffffffff,
    566  1.1  riastrad 	0x00009238, 0x00010006, 0xffffffff,
    567  1.1  riastrad 	0x0000923c, 0x00090008, 0xffffffff,
    568  1.1  riastrad 	0x00009240, 0x00070000, 0xffffffff,
    569  1.1  riastrad 	0x00009244, 0x00030002, 0xffffffff,
    570  1.1  riastrad 	0x00009248, 0x00050004, 0xffffffff,
    571  1.1  riastrad 	0x00009254, 0x00010006, 0xffffffff,
    572  1.1  riastrad 	0x00009258, 0x00090008, 0xffffffff,
    573  1.1  riastrad 	0x0000925c, 0x00070000, 0xffffffff,
    574  1.1  riastrad 	0x00009260, 0x00030002, 0xffffffff,
    575  1.1  riastrad 	0x00009264, 0x00050004, 0xffffffff,
    576  1.1  riastrad 	0x00009270, 0x00010006, 0xffffffff,
    577  1.1  riastrad 	0x00009274, 0x00090008, 0xffffffff,
    578  1.1  riastrad 	0x00009278, 0x00070000, 0xffffffff,
    579  1.1  riastrad 	0x0000927c, 0x00030002, 0xffffffff,
    580  1.1  riastrad 	0x00009280, 0x00050004, 0xffffffff,
    581  1.1  riastrad 	0x0000928c, 0x00010006, 0xffffffff,
    582  1.1  riastrad 	0x00009290, 0x00090008, 0xffffffff,
    583  1.1  riastrad 	0x000092a8, 0x00070000, 0xffffffff,
    584  1.1  riastrad 	0x000092ac, 0x00030002, 0xffffffff,
    585  1.1  riastrad 	0x000092b0, 0x00050004, 0xffffffff,
    586  1.1  riastrad 	0x000092bc, 0x00010006, 0xffffffff,
    587  1.1  riastrad 	0x000092c0, 0x00090008, 0xffffffff,
    588  1.1  riastrad 	0x000092c4, 0x00070000, 0xffffffff,
    589  1.1  riastrad 	0x000092c8, 0x00030002, 0xffffffff,
    590  1.1  riastrad 	0x000092cc, 0x00050004, 0xffffffff,
    591  1.1  riastrad 	0x000092d8, 0x00010006, 0xffffffff,
    592  1.1  riastrad 	0x000092dc, 0x00090008, 0xffffffff,
    593  1.1  riastrad 	0x00009294, 0x00000000, 0xffffffff,
    594  1.1  riastrad 	0x0000802c, 0xc0000000, 0xffffffff,
    595  1.1  riastrad 	0x00003fc4, 0xc0000000, 0xffffffff,
    596  1.1  riastrad 	0x000008f8, 0x00000010, 0xffffffff,
    597  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    598  1.1  riastrad 	0x000008f8, 0x00000011, 0xffffffff,
    599  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    600  1.1  riastrad 	0x000008f8, 0x00000012, 0xffffffff,
    601  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    602  1.1  riastrad 	0x000008f8, 0x00000013, 0xffffffff,
    603  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    604  1.1  riastrad 	0x000008f8, 0x00000014, 0xffffffff,
    605  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    606  1.1  riastrad 	0x000008f8, 0x00000015, 0xffffffff,
    607  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    608  1.1  riastrad 	0x000008f8, 0x00000016, 0xffffffff,
    609  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    610  1.1  riastrad 	0x000008f8, 0x00000017, 0xffffffff,
    611  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    612  1.1  riastrad 	0x000008f8, 0x00000018, 0xffffffff,
    613  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    614  1.1  riastrad 	0x000008f8, 0x00000019, 0xffffffff,
    615  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    616  1.1  riastrad 	0x000008f8, 0x0000001a, 0xffffffff,
    617  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    618  1.1  riastrad 	0x000008f8, 0x0000001b, 0xffffffff,
    619  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff
    620  1.1  riastrad };
    621  1.1  riastrad #define CAYMAN_MGCG_DEFAULT_LENGTH sizeof(cayman_mgcg_default) / (3 * sizeof(u32))
    622  1.1  riastrad 
    623  1.1  riastrad static const u32 cayman_mgcg_disable[] =
    624  1.1  riastrad {
    625  1.1  riastrad 	0x0000802c, 0xc0000000, 0xffffffff,
    626  1.1  riastrad 	0x000008f8, 0x00000000, 0xffffffff,
    627  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    628  1.1  riastrad 	0x000008f8, 0x00000001, 0xffffffff,
    629  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    630  1.1  riastrad 	0x000008f8, 0x00000002, 0xffffffff,
    631  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    632  1.1  riastrad 	0x000008f8, 0x00000003, 0xffffffff,
    633  1.1  riastrad 	0x000008fc, 0xffffffff, 0xffffffff,
    634  1.1  riastrad 	0x00009150, 0x00600000, 0xffffffff
    635  1.1  riastrad };
    636  1.1  riastrad #define CAYMAN_MGCG_DISABLE_LENGTH   sizeof(cayman_mgcg_disable) / (3 * sizeof(u32))
    637  1.1  riastrad 
    638  1.1  riastrad static const u32 cayman_mgcg_enable[] =
    639  1.1  riastrad {
    640  1.1  riastrad 	0x0000802c, 0xc0000000, 0xffffffff,
    641  1.1  riastrad 	0x000008f8, 0x00000000, 0xffffffff,
    642  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    643  1.1  riastrad 	0x000008f8, 0x00000001, 0xffffffff,
    644  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    645  1.1  riastrad 	0x000008f8, 0x00000002, 0xffffffff,
    646  1.1  riastrad 	0x000008fc, 0x00600000, 0xffffffff,
    647  1.1  riastrad 	0x000008f8, 0x00000003, 0xffffffff,
    648  1.1  riastrad 	0x000008fc, 0x00000000, 0xffffffff,
    649  1.1  riastrad 	0x00009150, 0x96944200, 0xffffffff
    650  1.1  riastrad };
    651  1.1  riastrad 
    652  1.1  riastrad #define CAYMAN_MGCG_ENABLE_LENGTH   sizeof(cayman_mgcg_enable) / (3 * sizeof(u32))
    653  1.1  riastrad 
    654  1.1  riastrad #define NISLANDS_SYSLS_SEQUENCE  100
    655  1.1  riastrad 
    656  1.1  riastrad static const u32 cayman_sysls_default[] =
    657  1.1  riastrad {
    658  1.1  riastrad 	/* Register,   Value,     Mask bits */
    659  1.1  riastrad 	0x000055e8, 0x00000000, 0xffffffff,
    660  1.1  riastrad 	0x0000d0bc, 0x00000000, 0xffffffff,
    661  1.1  riastrad 	0x0000d8bc, 0x00000000, 0xffffffff,
    662  1.1  riastrad 	0x000015c0, 0x000c1401, 0xffffffff,
    663  1.1  riastrad 	0x0000264c, 0x000c0400, 0xffffffff,
    664  1.1  riastrad 	0x00002648, 0x000c0400, 0xffffffff,
    665  1.1  riastrad 	0x00002650, 0x000c0400, 0xffffffff,
    666  1.1  riastrad 	0x000020b8, 0x000c0400, 0xffffffff,
    667  1.1  riastrad 	0x000020bc, 0x000c0400, 0xffffffff,
    668  1.1  riastrad 	0x000020c0, 0x000c0c80, 0xffffffff,
    669  1.1  riastrad 	0x0000f4a0, 0x000000c0, 0xffffffff,
    670  1.1  riastrad 	0x0000f4a4, 0x00680fff, 0xffffffff,
    671  1.1  riastrad 	0x00002f50, 0x00000404, 0xffffffff,
    672  1.1  riastrad 	0x000004c8, 0x00000001, 0xffffffff,
    673  1.1  riastrad 	0x000064ec, 0x00000000, 0xffffffff,
    674  1.1  riastrad 	0x00000c7c, 0x00000000, 0xffffffff,
    675  1.1  riastrad 	0x00008dfc, 0x00000000, 0xffffffff
    676  1.1  riastrad };
    677  1.1  riastrad #define CAYMAN_SYSLS_DEFAULT_LENGTH sizeof(cayman_sysls_default) / (3 * sizeof(u32))
    678  1.1  riastrad 
    679  1.1  riastrad static const u32 cayman_sysls_disable[] =
    680  1.1  riastrad {
    681  1.1  riastrad 	/* Register,   Value,     Mask bits */
    682  1.1  riastrad 	0x0000d0c0, 0x00000000, 0xffffffff,
    683  1.1  riastrad 	0x0000d8c0, 0x00000000, 0xffffffff,
    684  1.1  riastrad 	0x000055e8, 0x00000000, 0xffffffff,
    685  1.1  riastrad 	0x0000d0bc, 0x00000000, 0xffffffff,
    686  1.1  riastrad 	0x0000d8bc, 0x00000000, 0xffffffff,
    687  1.1  riastrad 	0x000015c0, 0x00041401, 0xffffffff,
    688  1.1  riastrad 	0x0000264c, 0x00040400, 0xffffffff,
    689  1.1  riastrad 	0x00002648, 0x00040400, 0xffffffff,
    690  1.1  riastrad 	0x00002650, 0x00040400, 0xffffffff,
    691  1.1  riastrad 	0x000020b8, 0x00040400, 0xffffffff,
    692  1.1  riastrad 	0x000020bc, 0x00040400, 0xffffffff,
    693  1.1  riastrad 	0x000020c0, 0x00040c80, 0xffffffff,
    694  1.1  riastrad 	0x0000f4a0, 0x000000c0, 0xffffffff,
    695  1.1  riastrad 	0x0000f4a4, 0x00680000, 0xffffffff,
    696  1.1  riastrad 	0x00002f50, 0x00000404, 0xffffffff,
    697  1.1  riastrad 	0x000004c8, 0x00000001, 0xffffffff,
    698  1.1  riastrad 	0x000064ec, 0x00007ffd, 0xffffffff,
    699  1.1  riastrad 	0x00000c7c, 0x0000ff00, 0xffffffff,
    700  1.1  riastrad 	0x00008dfc, 0x0000007f, 0xffffffff
    701  1.1  riastrad };
    702  1.1  riastrad #define CAYMAN_SYSLS_DISABLE_LENGTH sizeof(cayman_sysls_disable) / (3 * sizeof(u32))
    703  1.1  riastrad 
    704  1.1  riastrad static const u32 cayman_sysls_enable[] =
    705  1.1  riastrad {
    706  1.1  riastrad 	/* Register,   Value,     Mask bits */
    707  1.1  riastrad 	0x000055e8, 0x00000001, 0xffffffff,
    708  1.1  riastrad 	0x0000d0bc, 0x00000100, 0xffffffff,
    709  1.1  riastrad 	0x0000d8bc, 0x00000100, 0xffffffff,
    710  1.1  riastrad 	0x000015c0, 0x000c1401, 0xffffffff,
    711  1.1  riastrad 	0x0000264c, 0x000c0400, 0xffffffff,
    712  1.1  riastrad 	0x00002648, 0x000c0400, 0xffffffff,
    713  1.1  riastrad 	0x00002650, 0x000c0400, 0xffffffff,
    714  1.1  riastrad 	0x000020b8, 0x000c0400, 0xffffffff,
    715  1.1  riastrad 	0x000020bc, 0x000c0400, 0xffffffff,
    716  1.1  riastrad 	0x000020c0, 0x000c0c80, 0xffffffff,
    717  1.1  riastrad 	0x0000f4a0, 0x000000c0, 0xffffffff,
    718  1.1  riastrad 	0x0000f4a4, 0x00680fff, 0xffffffff,
    719  1.1  riastrad 	0x00002f50, 0x00000903, 0xffffffff,
    720  1.1  riastrad 	0x000004c8, 0x00000000, 0xffffffff,
    721  1.1  riastrad 	0x000064ec, 0x00000000, 0xffffffff,
    722  1.1  riastrad 	0x00000c7c, 0x00000000, 0xffffffff,
    723  1.1  riastrad 	0x00008dfc, 0x00000000, 0xffffffff
    724  1.1  riastrad };
    725  1.1  riastrad #define CAYMAN_SYSLS_ENABLE_LENGTH sizeof(cayman_sysls_enable) / (3 * sizeof(u32))
    726  1.1  riastrad 
    727  1.1  riastrad struct rv7xx_power_info *rv770_get_pi(struct radeon_device *rdev);
    728  1.1  riastrad struct evergreen_power_info *evergreen_get_pi(struct radeon_device *rdev);
    729  1.1  riastrad 
    730  1.1  riastrad extern int ni_mc_load_microcode(struct radeon_device *rdev);
    731  1.1  riastrad 
    732  1.1  riastrad struct ni_power_info *ni_get_pi(struct radeon_device *rdev)
    733  1.1  riastrad {
    734  1.2  riastrad 	struct ni_power_info *pi = rdev->pm.dpm.priv;
    735  1.1  riastrad 
    736  1.2  riastrad 	return pi;
    737  1.1  riastrad }
    738  1.1  riastrad 
    739  1.1  riastrad struct ni_ps *ni_get_ps(struct radeon_ps *rps)
    740  1.1  riastrad {
    741  1.1  riastrad 	struct ni_ps *ps = rps->ps_priv;
    742  1.1  riastrad 
    743  1.1  riastrad 	return ps;
    744  1.1  riastrad }
    745  1.1  riastrad 
    746  1.1  riastrad static void ni_calculate_leakage_for_v_and_t_formula(const struct ni_leakage_coeffients *coeff,
    747  1.1  riastrad 						     u16 v, s32 t,
    748  1.1  riastrad 						     u32 ileakage,
    749  1.1  riastrad 						     u32 *leakage)
    750  1.1  riastrad {
    751  1.1  riastrad 	s64 kt, kv, leakage_w, i_leakage, vddc, temperature;
    752  1.1  riastrad 
    753  1.1  riastrad 	i_leakage = div64_s64(drm_int2fixp(ileakage), 1000);
    754  1.1  riastrad 	vddc = div64_s64(drm_int2fixp(v), 1000);
    755  1.1  riastrad 	temperature = div64_s64(drm_int2fixp(t), 1000);
    756  1.1  riastrad 
    757  1.1  riastrad 	kt = drm_fixp_mul(div64_s64(drm_int2fixp(coeff->at), 1000),
    758  1.1  riastrad 			  drm_fixp_exp(drm_fixp_mul(div64_s64(drm_int2fixp(coeff->bt), 1000), temperature)));
    759  1.1  riastrad 	kv = drm_fixp_mul(div64_s64(drm_int2fixp(coeff->av), 1000),
    760  1.1  riastrad 			  drm_fixp_exp(drm_fixp_mul(div64_s64(drm_int2fixp(coeff->bv), 1000), vddc)));
    761  1.1  riastrad 
    762  1.1  riastrad 	leakage_w = drm_fixp_mul(drm_fixp_mul(drm_fixp_mul(i_leakage, kt), kv), vddc);
    763  1.1  riastrad 
    764  1.1  riastrad 	*leakage = drm_fixp2int(leakage_w * 1000);
    765  1.1  riastrad }
    766  1.1  riastrad 
    767  1.1  riastrad static void ni_calculate_leakage_for_v_and_t(struct radeon_device *rdev,
    768  1.1  riastrad 					     const struct ni_leakage_coeffients *coeff,
    769  1.1  riastrad 					     u16 v,
    770  1.1  riastrad 					     s32 t,
    771  1.1  riastrad 					     u32 i_leakage,
    772  1.1  riastrad 					     u32 *leakage)
    773  1.1  riastrad {
    774  1.1  riastrad 	ni_calculate_leakage_for_v_and_t_formula(coeff, v, t, i_leakage, leakage);
    775  1.1  riastrad }
    776  1.1  riastrad 
    777  1.1  riastrad bool ni_dpm_vblank_too_short(struct radeon_device *rdev)
    778  1.1  riastrad {
    779  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    780  1.1  riastrad 	u32 vblank_time = r600_dpm_get_vblank_time(rdev);
    781  1.1  riastrad 	/* we never hit the non-gddr5 limit so disable it */
    782  1.1  riastrad 	u32 switch_limit = pi->mem_gddr5 ? 450 : 0;
    783  1.1  riastrad 
    784  1.1  riastrad 	if (vblank_time < switch_limit)
    785  1.1  riastrad 		return true;
    786  1.1  riastrad 	else
    787  1.1  riastrad 		return false;
    788  1.1  riastrad 
    789  1.1  riastrad }
    790  1.1  riastrad 
    791  1.1  riastrad static void ni_apply_state_adjust_rules(struct radeon_device *rdev,
    792  1.1  riastrad 					struct radeon_ps *rps)
    793  1.1  riastrad {
    794  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
    795  1.1  riastrad 	struct radeon_clock_and_voltage_limits *max_limits;
    796  1.1  riastrad 	bool disable_mclk_switching;
    797  1.1  riastrad 	u32 mclk;
    798  1.1  riastrad 	u16 vddci;
    799  1.1  riastrad 	int i;
    800  1.1  riastrad 
    801  1.1  riastrad 	if ((rdev->pm.dpm.new_active_crtc_count > 1) ||
    802  1.1  riastrad 	    ni_dpm_vblank_too_short(rdev))
    803  1.1  riastrad 		disable_mclk_switching = true;
    804  1.1  riastrad 	else
    805  1.1  riastrad 		disable_mclk_switching = false;
    806  1.1  riastrad 
    807  1.1  riastrad 	if (rdev->pm.dpm.ac_power)
    808  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
    809  1.1  riastrad 	else
    810  1.1  riastrad 		max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
    811  1.1  riastrad 
    812  1.1  riastrad 	if (rdev->pm.dpm.ac_power == false) {
    813  1.1  riastrad 		for (i = 0; i < ps->performance_level_count; i++) {
    814  1.1  riastrad 			if (ps->performance_levels[i].mclk > max_limits->mclk)
    815  1.1  riastrad 				ps->performance_levels[i].mclk = max_limits->mclk;
    816  1.1  riastrad 			if (ps->performance_levels[i].sclk > max_limits->sclk)
    817  1.1  riastrad 				ps->performance_levels[i].sclk = max_limits->sclk;
    818  1.1  riastrad 			if (ps->performance_levels[i].vddc > max_limits->vddc)
    819  1.1  riastrad 				ps->performance_levels[i].vddc = max_limits->vddc;
    820  1.1  riastrad 			if (ps->performance_levels[i].vddci > max_limits->vddci)
    821  1.1  riastrad 				ps->performance_levels[i].vddci = max_limits->vddci;
    822  1.1  riastrad 		}
    823  1.1  riastrad 	}
    824  1.1  riastrad 
    825  1.1  riastrad 	/* XXX validate the min clocks required for display */
    826  1.1  riastrad 
    827  1.1  riastrad 	/* adjust low state */
    828  1.1  riastrad 	if (disable_mclk_switching) {
    829  1.1  riastrad 		ps->performance_levels[0].mclk =
    830  1.1  riastrad 			ps->performance_levels[ps->performance_level_count - 1].mclk;
    831  1.1  riastrad 		ps->performance_levels[0].vddci =
    832  1.1  riastrad 			ps->performance_levels[ps->performance_level_count - 1].vddci;
    833  1.1  riastrad 	}
    834  1.1  riastrad 
    835  1.1  riastrad 	btc_skip_blacklist_clocks(rdev, max_limits->sclk, max_limits->mclk,
    836  1.1  riastrad 				  &ps->performance_levels[0].sclk,
    837  1.1  riastrad 				  &ps->performance_levels[0].mclk);
    838  1.1  riastrad 
    839  1.1  riastrad 	for (i = 1; i < ps->performance_level_count; i++) {
    840  1.1  riastrad 		if (ps->performance_levels[i].sclk < ps->performance_levels[i - 1].sclk)
    841  1.1  riastrad 			ps->performance_levels[i].sclk = ps->performance_levels[i - 1].sclk;
    842  1.1  riastrad 		if (ps->performance_levels[i].vddc < ps->performance_levels[i - 1].vddc)
    843  1.1  riastrad 			ps->performance_levels[i].vddc = ps->performance_levels[i - 1].vddc;
    844  1.1  riastrad 	}
    845  1.1  riastrad 
    846  1.1  riastrad 	/* adjust remaining states */
    847  1.1  riastrad 	if (disable_mclk_switching) {
    848  1.1  riastrad 		mclk = ps->performance_levels[0].mclk;
    849  1.1  riastrad 		vddci = ps->performance_levels[0].vddci;
    850  1.1  riastrad 		for (i = 1; i < ps->performance_level_count; i++) {
    851  1.1  riastrad 			if (mclk < ps->performance_levels[i].mclk)
    852  1.1  riastrad 				mclk = ps->performance_levels[i].mclk;
    853  1.1  riastrad 			if (vddci < ps->performance_levels[i].vddci)
    854  1.1  riastrad 				vddci = ps->performance_levels[i].vddci;
    855  1.1  riastrad 		}
    856  1.1  riastrad 		for (i = 0; i < ps->performance_level_count; i++) {
    857  1.1  riastrad 			ps->performance_levels[i].mclk = mclk;
    858  1.1  riastrad 			ps->performance_levels[i].vddci = vddci;
    859  1.1  riastrad 		}
    860  1.1  riastrad 	} else {
    861  1.1  riastrad 		for (i = 1; i < ps->performance_level_count; i++) {
    862  1.1  riastrad 			if (ps->performance_levels[i].mclk < ps->performance_levels[i - 1].mclk)
    863  1.1  riastrad 				ps->performance_levels[i].mclk = ps->performance_levels[i - 1].mclk;
    864  1.1  riastrad 			if (ps->performance_levels[i].vddci < ps->performance_levels[i - 1].vddci)
    865  1.1  riastrad 				ps->performance_levels[i].vddci = ps->performance_levels[i - 1].vddci;
    866  1.1  riastrad 		}
    867  1.1  riastrad 	}
    868  1.1  riastrad 
    869  1.1  riastrad 	for (i = 1; i < ps->performance_level_count; i++)
    870  1.1  riastrad 		btc_skip_blacklist_clocks(rdev, max_limits->sclk, max_limits->mclk,
    871  1.1  riastrad 					  &ps->performance_levels[i].sclk,
    872  1.1  riastrad 					  &ps->performance_levels[i].mclk);
    873  1.1  riastrad 
    874  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++)
    875  1.1  riastrad 		btc_adjust_clock_combinations(rdev, max_limits,
    876  1.1  riastrad 					      &ps->performance_levels[i]);
    877  1.1  riastrad 
    878  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++) {
    879  1.1  riastrad 		btc_apply_voltage_dependency_rules(&rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk,
    880  1.1  riastrad 						   ps->performance_levels[i].sclk,
    881  1.1  riastrad 						   max_limits->vddc,  &ps->performance_levels[i].vddc);
    882  1.1  riastrad 		btc_apply_voltage_dependency_rules(&rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
    883  1.1  riastrad 						   ps->performance_levels[i].mclk,
    884  1.1  riastrad 						   max_limits->vddci, &ps->performance_levels[i].vddci);
    885  1.1  riastrad 		btc_apply_voltage_dependency_rules(&rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
    886  1.1  riastrad 						   ps->performance_levels[i].mclk,
    887  1.1  riastrad 						   max_limits->vddc,  &ps->performance_levels[i].vddc);
    888  1.1  riastrad 		btc_apply_voltage_dependency_rules(&rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk,
    889  1.1  riastrad 						   rdev->clock.current_dispclk,
    890  1.1  riastrad 						   max_limits->vddc,  &ps->performance_levels[i].vddc);
    891  1.1  riastrad 	}
    892  1.1  riastrad 
    893  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++) {
    894  1.1  riastrad 		btc_apply_voltage_delta_rules(rdev,
    895  1.1  riastrad 					      max_limits->vddc, max_limits->vddci,
    896  1.1  riastrad 					      &ps->performance_levels[i].vddc,
    897  1.1  riastrad 					      &ps->performance_levels[i].vddci);
    898  1.1  riastrad 	}
    899  1.1  riastrad 
    900  1.1  riastrad 	ps->dc_compatible = true;
    901  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++) {
    902  1.1  riastrad 		if (ps->performance_levels[i].vddc > rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.vddc)
    903  1.1  riastrad 			ps->dc_compatible = false;
    904  1.1  riastrad 
    905  1.1  riastrad 		if (ps->performance_levels[i].vddc < rdev->pm.dpm.dyn_state.min_vddc_for_pcie_gen2)
    906  1.1  riastrad 			ps->performance_levels[i].flags &= ~ATOM_PPLIB_R600_FLAGS_PCIEGEN2;
    907  1.1  riastrad 	}
    908  1.1  riastrad }
    909  1.1  riastrad 
    910  1.1  riastrad static void ni_cg_clockgating_default(struct radeon_device *rdev)
    911  1.1  riastrad {
    912  1.1  riastrad 	u32 count;
    913  1.1  riastrad 	const u32 *ps = NULL;
    914  1.1  riastrad 
    915  1.1  riastrad 	ps = (const u32 *)&cayman_cgcg_cgls_default;
    916  1.1  riastrad 	count = CAYMAN_CGCG_CGLS_DEFAULT_LENGTH;
    917  1.1  riastrad 
    918  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    919  1.1  riastrad }
    920  1.1  riastrad 
    921  1.1  riastrad static void ni_gfx_clockgating_enable(struct radeon_device *rdev,
    922  1.1  riastrad 				      bool enable)
    923  1.1  riastrad {
    924  1.1  riastrad 	u32 count;
    925  1.1  riastrad 	const u32 *ps = NULL;
    926  1.1  riastrad 
    927  1.1  riastrad 	if (enable) {
    928  1.1  riastrad 		ps = (const u32 *)&cayman_cgcg_cgls_enable;
    929  1.1  riastrad 		count = CAYMAN_CGCG_CGLS_ENABLE_LENGTH;
    930  1.1  riastrad 	} else {
    931  1.1  riastrad 		ps = (const u32 *)&cayman_cgcg_cgls_disable;
    932  1.1  riastrad 		count = CAYMAN_CGCG_CGLS_DISABLE_LENGTH;
    933  1.1  riastrad 	}
    934  1.1  riastrad 
    935  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    936  1.1  riastrad }
    937  1.1  riastrad 
    938  1.1  riastrad static void ni_mg_clockgating_default(struct radeon_device *rdev)
    939  1.1  riastrad {
    940  1.1  riastrad 	u32 count;
    941  1.1  riastrad 	const u32 *ps = NULL;
    942  1.1  riastrad 
    943  1.1  riastrad 	ps = (const u32 *)&cayman_mgcg_default;
    944  1.1  riastrad 	count = CAYMAN_MGCG_DEFAULT_LENGTH;
    945  1.1  riastrad 
    946  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    947  1.1  riastrad }
    948  1.1  riastrad 
    949  1.1  riastrad static void ni_mg_clockgating_enable(struct radeon_device *rdev,
    950  1.1  riastrad 				     bool enable)
    951  1.1  riastrad {
    952  1.1  riastrad 	u32 count;
    953  1.1  riastrad 	const u32 *ps = NULL;
    954  1.1  riastrad 
    955  1.1  riastrad 	if (enable) {
    956  1.1  riastrad 		ps = (const u32 *)&cayman_mgcg_enable;
    957  1.1  riastrad 		count = CAYMAN_MGCG_ENABLE_LENGTH;
    958  1.1  riastrad 	} else {
    959  1.1  riastrad 		ps = (const u32 *)&cayman_mgcg_disable;
    960  1.1  riastrad 		count = CAYMAN_MGCG_DISABLE_LENGTH;
    961  1.1  riastrad 	}
    962  1.1  riastrad 
    963  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    964  1.1  riastrad }
    965  1.1  riastrad 
    966  1.1  riastrad static void ni_ls_clockgating_default(struct radeon_device *rdev)
    967  1.1  riastrad {
    968  1.1  riastrad 	u32 count;
    969  1.1  riastrad 	const u32 *ps = NULL;
    970  1.1  riastrad 
    971  1.1  riastrad 	ps = (const u32 *)&cayman_sysls_default;
    972  1.1  riastrad 	count = CAYMAN_SYSLS_DEFAULT_LENGTH;
    973  1.1  riastrad 
    974  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    975  1.1  riastrad }
    976  1.1  riastrad 
    977  1.1  riastrad static void ni_ls_clockgating_enable(struct radeon_device *rdev,
    978  1.1  riastrad 				     bool enable)
    979  1.1  riastrad {
    980  1.1  riastrad 	u32 count;
    981  1.1  riastrad 	const u32 *ps = NULL;
    982  1.1  riastrad 
    983  1.1  riastrad 	if (enable) {
    984  1.1  riastrad 		ps = (const u32 *)&cayman_sysls_enable;
    985  1.1  riastrad 		count = CAYMAN_SYSLS_ENABLE_LENGTH;
    986  1.1  riastrad 	} else {
    987  1.1  riastrad 		ps = (const u32 *)&cayman_sysls_disable;
    988  1.1  riastrad 		count = CAYMAN_SYSLS_DISABLE_LENGTH;
    989  1.1  riastrad 	}
    990  1.1  riastrad 
    991  1.1  riastrad 	btc_program_mgcg_hw_sequence(rdev, ps, count);
    992  1.1  riastrad 
    993  1.1  riastrad }
    994  1.1  riastrad 
    995  1.1  riastrad static int ni_patch_single_dependency_table_based_on_leakage(struct radeon_device *rdev,
    996  1.1  riastrad 							     struct radeon_clock_voltage_dependency_table *table)
    997  1.1  riastrad {
    998  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
    999  1.1  riastrad 	u32 i;
   1000  1.1  riastrad 
   1001  1.1  riastrad 	if (table) {
   1002  1.1  riastrad 		for (i = 0; i < table->count; i++) {
   1003  1.1  riastrad 			if (0xff01 == table->entries[i].v) {
   1004  1.1  riastrad 				if (pi->max_vddc == 0)
   1005  1.1  riastrad 					return -EINVAL;
   1006  1.1  riastrad 				table->entries[i].v = pi->max_vddc;
   1007  1.1  riastrad 			}
   1008  1.1  riastrad 		}
   1009  1.1  riastrad 	}
   1010  1.1  riastrad 	return 0;
   1011  1.1  riastrad }
   1012  1.1  riastrad 
   1013  1.1  riastrad static int ni_patch_dependency_tables_based_on_leakage(struct radeon_device *rdev)
   1014  1.1  riastrad {
   1015  1.1  riastrad 	int ret = 0;
   1016  1.1  riastrad 
   1017  1.1  riastrad 	ret = ni_patch_single_dependency_table_based_on_leakage(rdev,
   1018  1.1  riastrad 								&rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk);
   1019  1.1  riastrad 
   1020  1.1  riastrad 	ret = ni_patch_single_dependency_table_based_on_leakage(rdev,
   1021  1.1  riastrad 								&rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk);
   1022  1.1  riastrad 	return ret;
   1023  1.1  riastrad }
   1024  1.1  riastrad 
   1025  1.1  riastrad static void ni_stop_dpm(struct radeon_device *rdev)
   1026  1.1  riastrad {
   1027  1.1  riastrad 	WREG32_P(GENERAL_PWRMGT, 0, ~GLOBAL_PWRMGT_EN);
   1028  1.1  riastrad }
   1029  1.1  riastrad 
   1030  1.1  riastrad #if 0
   1031  1.1  riastrad static int ni_notify_hw_of_power_source(struct radeon_device *rdev,
   1032  1.1  riastrad 					bool ac_power)
   1033  1.1  riastrad {
   1034  1.1  riastrad 	if (ac_power)
   1035  1.1  riastrad 		return (rv770_send_msg_to_smc(rdev, PPSMC_MSG_RunningOnAC) == PPSMC_Result_OK) ?
   1036  1.1  riastrad 			0 : -EINVAL;
   1037  1.1  riastrad 
   1038  1.1  riastrad 	return 0;
   1039  1.1  riastrad }
   1040  1.1  riastrad #endif
   1041  1.1  riastrad 
   1042  1.1  riastrad static PPSMC_Result ni_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
   1043  1.1  riastrad 						      PPSMC_Msg msg, u32 parameter)
   1044  1.1  riastrad {
   1045  1.1  riastrad 	WREG32(SMC_SCRATCH0, parameter);
   1046  1.1  riastrad 	return rv770_send_msg_to_smc(rdev, msg);
   1047  1.1  riastrad }
   1048  1.1  riastrad 
   1049  1.1  riastrad static int ni_restrict_performance_levels_before_switch(struct radeon_device *rdev)
   1050  1.1  riastrad {
   1051  1.1  riastrad 	if (rv770_send_msg_to_smc(rdev, PPSMC_MSG_NoForcedLevel) != PPSMC_Result_OK)
   1052  1.1  riastrad 		return -EINVAL;
   1053  1.1  riastrad 
   1054  1.1  riastrad 	return (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetEnabledLevels, 1) == PPSMC_Result_OK) ?
   1055  1.1  riastrad 		0 : -EINVAL;
   1056  1.1  riastrad }
   1057  1.1  riastrad 
   1058  1.1  riastrad int ni_dpm_force_performance_level(struct radeon_device *rdev,
   1059  1.1  riastrad 				   enum radeon_dpm_forced_level level)
   1060  1.1  riastrad {
   1061  1.1  riastrad 	if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
   1062  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetEnabledLevels, 0) != PPSMC_Result_OK)
   1063  1.1  riastrad 			return -EINVAL;
   1064  1.1  riastrad 
   1065  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetForcedLevels, 1) != PPSMC_Result_OK)
   1066  1.1  riastrad 			return -EINVAL;
   1067  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
   1068  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetForcedLevels, 0) != PPSMC_Result_OK)
   1069  1.1  riastrad 			return -EINVAL;
   1070  1.1  riastrad 
   1071  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetEnabledLevels, 1) != PPSMC_Result_OK)
   1072  1.1  riastrad 			return -EINVAL;
   1073  1.1  riastrad 	} else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
   1074  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetForcedLevels, 0) != PPSMC_Result_OK)
   1075  1.1  riastrad 			return -EINVAL;
   1076  1.1  riastrad 
   1077  1.1  riastrad 		if (ni_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SetEnabledLevels, 0) != PPSMC_Result_OK)
   1078  1.1  riastrad 			return -EINVAL;
   1079  1.1  riastrad 	}
   1080  1.1  riastrad 
   1081  1.1  riastrad 	rdev->pm.dpm.forced_level = level;
   1082  1.1  riastrad 
   1083  1.1  riastrad 	return 0;
   1084  1.1  riastrad }
   1085  1.1  riastrad 
   1086  1.1  riastrad static void ni_stop_smc(struct radeon_device *rdev)
   1087  1.1  riastrad {
   1088  1.1  riastrad 	u32 tmp;
   1089  1.1  riastrad 	int i;
   1090  1.1  riastrad 
   1091  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
   1092  1.1  riastrad 		tmp = RREG32(LB_SYNC_RESET_SEL) & LB_SYNC_RESET_SEL_MASK;
   1093  1.1  riastrad 		if (tmp != 1)
   1094  1.1  riastrad 			break;
   1095  1.1  riastrad 		udelay(1);
   1096  1.1  riastrad 	}
   1097  1.1  riastrad 
   1098  1.1  riastrad 	udelay(100);
   1099  1.1  riastrad 
   1100  1.1  riastrad 	r7xx_stop_smc(rdev);
   1101  1.1  riastrad }
   1102  1.1  riastrad 
   1103  1.1  riastrad static int ni_process_firmware_header(struct radeon_device *rdev)
   1104  1.1  riastrad {
   1105  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1106  1.2  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1107  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1108  1.1  riastrad 	u32 tmp;
   1109  1.1  riastrad 	int ret;
   1110  1.1  riastrad 
   1111  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1112  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1113  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_stateTable,
   1114  1.1  riastrad 					&tmp, pi->sram_end);
   1115  1.1  riastrad 
   1116  1.1  riastrad 	if (ret)
   1117  1.1  riastrad 		return ret;
   1118  1.1  riastrad 
   1119  1.1  riastrad 	pi->state_table_start = (u16)tmp;
   1120  1.1  riastrad 
   1121  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1122  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1123  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_softRegisters,
   1124  1.1  riastrad 					&tmp, pi->sram_end);
   1125  1.1  riastrad 
   1126  1.1  riastrad 	if (ret)
   1127  1.1  riastrad 		return ret;
   1128  1.1  riastrad 
   1129  1.1  riastrad 	pi->soft_regs_start = (u16)tmp;
   1130  1.1  riastrad 
   1131  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1132  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1133  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_mcRegisterTable,
   1134  1.1  riastrad 					&tmp, pi->sram_end);
   1135  1.1  riastrad 
   1136  1.1  riastrad 	if (ret)
   1137  1.1  riastrad 		return ret;
   1138  1.1  riastrad 
   1139  1.1  riastrad 	eg_pi->mc_reg_table_start = (u16)tmp;
   1140  1.1  riastrad 
   1141  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1142  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1143  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_fanTable,
   1144  1.1  riastrad 					&tmp, pi->sram_end);
   1145  1.1  riastrad 
   1146  1.1  riastrad 	if (ret)
   1147  1.1  riastrad 		return ret;
   1148  1.1  riastrad 
   1149  1.1  riastrad 	ni_pi->fan_table_start = (u16)tmp;
   1150  1.1  riastrad 
   1151  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1152  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1153  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_mcArbDramAutoRefreshTable,
   1154  1.1  riastrad 					&tmp, pi->sram_end);
   1155  1.1  riastrad 
   1156  1.1  riastrad 	if (ret)
   1157  1.1  riastrad 		return ret;
   1158  1.1  riastrad 
   1159  1.1  riastrad 	ni_pi->arb_table_start = (u16)tmp;
   1160  1.1  riastrad 
   1161  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1162  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1163  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_cacTable,
   1164  1.1  riastrad 					&tmp, pi->sram_end);
   1165  1.1  riastrad 
   1166  1.1  riastrad 	if (ret)
   1167  1.1  riastrad 		return ret;
   1168  1.1  riastrad 
   1169  1.1  riastrad 	ni_pi->cac_table_start = (u16)tmp;
   1170  1.1  riastrad 
   1171  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev,
   1172  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_LOCATION +
   1173  1.1  riastrad 					NISLANDS_SMC_FIRMWARE_HEADER_spllTable,
   1174  1.1  riastrad 					&tmp, pi->sram_end);
   1175  1.1  riastrad 
   1176  1.1  riastrad 	if (ret)
   1177  1.1  riastrad 		return ret;
   1178  1.1  riastrad 
   1179  1.1  riastrad 	ni_pi->spll_table_start = (u16)tmp;
   1180  1.1  riastrad 
   1181  1.1  riastrad 
   1182  1.1  riastrad 	return ret;
   1183  1.1  riastrad }
   1184  1.1  riastrad 
   1185  1.1  riastrad static void ni_read_clock_registers(struct radeon_device *rdev)
   1186  1.1  riastrad {
   1187  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1188  1.1  riastrad 
   1189  1.1  riastrad 	ni_pi->clock_registers.cg_spll_func_cntl = RREG32(CG_SPLL_FUNC_CNTL);
   1190  1.1  riastrad 	ni_pi->clock_registers.cg_spll_func_cntl_2 = RREG32(CG_SPLL_FUNC_CNTL_2);
   1191  1.1  riastrad 	ni_pi->clock_registers.cg_spll_func_cntl_3 = RREG32(CG_SPLL_FUNC_CNTL_3);
   1192  1.1  riastrad 	ni_pi->clock_registers.cg_spll_func_cntl_4 = RREG32(CG_SPLL_FUNC_CNTL_4);
   1193  1.1  riastrad 	ni_pi->clock_registers.cg_spll_spread_spectrum = RREG32(CG_SPLL_SPREAD_SPECTRUM);
   1194  1.1  riastrad 	ni_pi->clock_registers.cg_spll_spread_spectrum_2 = RREG32(CG_SPLL_SPREAD_SPECTRUM_2);
   1195  1.1  riastrad 	ni_pi->clock_registers.mpll_ad_func_cntl = RREG32(MPLL_AD_FUNC_CNTL);
   1196  1.1  riastrad 	ni_pi->clock_registers.mpll_ad_func_cntl_2 = RREG32(MPLL_AD_FUNC_CNTL_2);
   1197  1.1  riastrad 	ni_pi->clock_registers.mpll_dq_func_cntl = RREG32(MPLL_DQ_FUNC_CNTL);
   1198  1.1  riastrad 	ni_pi->clock_registers.mpll_dq_func_cntl_2 = RREG32(MPLL_DQ_FUNC_CNTL_2);
   1199  1.1  riastrad 	ni_pi->clock_registers.mclk_pwrmgt_cntl = RREG32(MCLK_PWRMGT_CNTL);
   1200  1.1  riastrad 	ni_pi->clock_registers.dll_cntl = RREG32(DLL_CNTL);
   1201  1.1  riastrad 	ni_pi->clock_registers.mpll_ss1 = RREG32(MPLL_SS1);
   1202  1.1  riastrad 	ni_pi->clock_registers.mpll_ss2 = RREG32(MPLL_SS2);
   1203  1.1  riastrad }
   1204  1.1  riastrad 
   1205  1.1  riastrad #if 0
   1206  1.1  riastrad static int ni_enter_ulp_state(struct radeon_device *rdev)
   1207  1.1  riastrad {
   1208  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1209  1.1  riastrad 
   1210  1.1  riastrad 	if (pi->gfx_clock_gating) {
   1211  1.2  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~DYN_GFX_CLK_OFF_EN);
   1212  1.1  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, GFX_CLK_FORCE_ON, ~GFX_CLK_FORCE_ON);
   1213  1.2  riastrad 		WREG32_P(SCLK_PWRMGT_CNTL, 0, ~GFX_CLK_FORCE_ON);
   1214  1.1  riastrad 		RREG32(GB_ADDR_CONFIG);
   1215  1.2  riastrad 	}
   1216  1.1  riastrad 
   1217  1.1  riastrad 	WREG32_P(SMC_MSG, HOST_SMC_MSG(PPSMC_MSG_SwitchToMinimumPower),
   1218  1.2  riastrad 		 ~HOST_SMC_MSG_MASK);
   1219  1.1  riastrad 
   1220  1.1  riastrad 	udelay(25000);
   1221  1.1  riastrad 
   1222  1.1  riastrad 	return 0;
   1223  1.1  riastrad }
   1224  1.1  riastrad #endif
   1225  1.1  riastrad 
   1226  1.1  riastrad static void ni_program_response_times(struct radeon_device *rdev)
   1227  1.1  riastrad {
   1228  1.1  riastrad 	u32 voltage_response_time, backbias_response_time, acpi_delay_time, vbi_time_out;
   1229  1.1  riastrad 	u32 vddc_dly, bb_dly, acpi_dly, vbi_dly, mclk_switch_limit;
   1230  1.1  riastrad 	u32 reference_clock;
   1231  1.1  riastrad 
   1232  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_mvdd_chg_time, 1);
   1233  1.1  riastrad 
   1234  1.1  riastrad 	voltage_response_time = (u32)rdev->pm.dpm.voltage_response_time;
   1235  1.1  riastrad 	backbias_response_time = (u32)rdev->pm.dpm.backbias_response_time;
   1236  1.1  riastrad 
   1237  1.1  riastrad 	if (voltage_response_time == 0)
   1238  1.1  riastrad 		voltage_response_time = 1000;
   1239  1.1  riastrad 
   1240  1.1  riastrad 	if (backbias_response_time == 0)
   1241  1.1  riastrad 		backbias_response_time = 1000;
   1242  1.1  riastrad 
   1243  1.1  riastrad 	acpi_delay_time = 15000;
   1244  1.1  riastrad 	vbi_time_out = 100000;
   1245  1.1  riastrad 
   1246  1.1  riastrad 	reference_clock = radeon_get_xclk(rdev);
   1247  1.1  riastrad 
   1248  1.1  riastrad 	vddc_dly = (voltage_response_time  * reference_clock) / 1600;
   1249  1.1  riastrad 	bb_dly   = (backbias_response_time * reference_clock) / 1600;
   1250  1.1  riastrad 	acpi_dly = (acpi_delay_time * reference_clock) / 1600;
   1251  1.1  riastrad 	vbi_dly  = (vbi_time_out * reference_clock) / 1600;
   1252  1.1  riastrad 
   1253  1.1  riastrad 	mclk_switch_limit = (460 * reference_clock) / 100;
   1254  1.1  riastrad 
   1255  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_delay_vreg,  vddc_dly);
   1256  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_delay_bbias, bb_dly);
   1257  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_delay_acpi,  acpi_dly);
   1258  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_mclk_chg_timeout, vbi_dly);
   1259  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_mc_block_delay, 0xAA);
   1260  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_mclk_switch_lim, mclk_switch_limit);
   1261  1.1  riastrad }
   1262  1.1  riastrad 
   1263  1.1  riastrad static void ni_populate_smc_voltage_table(struct radeon_device *rdev,
   1264  1.1  riastrad 					  struct atom_voltage_table *voltage_table,
   1265  1.1  riastrad 					  NISLANDS_SMC_STATETABLE *table)
   1266  1.1  riastrad {
   1267  1.1  riastrad 	unsigned int i;
   1268  1.1  riastrad 
   1269  1.1  riastrad 	for (i = 0; i < voltage_table->count; i++) {
   1270  1.1  riastrad 		table->highSMIO[i] = 0;
   1271  1.1  riastrad 		table->lowSMIO[i] |= cpu_to_be32(voltage_table->entries[i].smio_low);
   1272  1.1  riastrad 	}
   1273  1.1  riastrad }
   1274  1.1  riastrad 
   1275  1.1  riastrad static void ni_populate_smc_voltage_tables(struct radeon_device *rdev,
   1276  1.1  riastrad 					   NISLANDS_SMC_STATETABLE *table)
   1277  1.1  riastrad {
   1278  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1279  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1280  1.1  riastrad 	unsigned char i;
   1281  1.1  riastrad 
   1282  1.1  riastrad 	if (eg_pi->vddc_voltage_table.count) {
   1283  1.1  riastrad 		ni_populate_smc_voltage_table(rdev, &eg_pi->vddc_voltage_table, table);
   1284  1.1  riastrad 		table->voltageMaskTable.highMask[NISLANDS_SMC_VOLTAGEMASK_VDDC] = 0;
   1285  1.1  riastrad 		table->voltageMaskTable.lowMask[NISLANDS_SMC_VOLTAGEMASK_VDDC] =
   1286  1.1  riastrad 			cpu_to_be32(eg_pi->vddc_voltage_table.mask_low);
   1287  1.1  riastrad 
   1288  1.1  riastrad 		for (i = 0; i < eg_pi->vddc_voltage_table.count; i++) {
   1289  1.1  riastrad 			if (pi->max_vddc_in_table <= eg_pi->vddc_voltage_table.entries[i].value) {
   1290  1.1  riastrad 				table->maxVDDCIndexInPPTable = i;
   1291  1.1  riastrad 				break;
   1292  1.1  riastrad 			}
   1293  1.1  riastrad 		}
   1294  1.1  riastrad 	}
   1295  1.1  riastrad 
   1296  1.1  riastrad 	if (eg_pi->vddci_voltage_table.count) {
   1297  1.1  riastrad 		ni_populate_smc_voltage_table(rdev, &eg_pi->vddci_voltage_table, table);
   1298  1.1  riastrad 
   1299  1.1  riastrad 		table->voltageMaskTable.highMask[NISLANDS_SMC_VOLTAGEMASK_VDDCI] = 0;
   1300  1.1  riastrad 		table->voltageMaskTable.lowMask[NISLANDS_SMC_VOLTAGEMASK_VDDCI] =
   1301  1.1  riastrad 			cpu_to_be32(eg_pi->vddci_voltage_table.mask_low);
   1302  1.1  riastrad 	}
   1303  1.1  riastrad }
   1304  1.1  riastrad 
   1305  1.1  riastrad static int ni_populate_voltage_value(struct radeon_device *rdev,
   1306  1.1  riastrad 				     struct atom_voltage_table *table,
   1307  1.1  riastrad 				     u16 value,
   1308  1.1  riastrad 				     NISLANDS_SMC_VOLTAGE_VALUE *voltage)
   1309  1.1  riastrad {
   1310  1.1  riastrad 	unsigned int i;
   1311  1.1  riastrad 
   1312  1.1  riastrad 	for (i = 0; i < table->count; i++) {
   1313  1.1  riastrad 		if (value <= table->entries[i].value) {
   1314  1.1  riastrad 			voltage->index = (u8)i;
   1315  1.1  riastrad 			voltage->value = cpu_to_be16(table->entries[i].value);
   1316  1.1  riastrad 			break;
   1317  1.1  riastrad 		}
   1318  1.1  riastrad 	}
   1319  1.1  riastrad 
   1320  1.1  riastrad 	if (i >= table->count)
   1321  1.1  riastrad 		return -EINVAL;
   1322  1.1  riastrad 
   1323  1.1  riastrad 	return 0;
   1324  1.1  riastrad }
   1325  1.1  riastrad 
   1326  1.1  riastrad static void ni_populate_mvdd_value(struct radeon_device *rdev,
   1327  1.1  riastrad 				   u32 mclk,
   1328  1.1  riastrad 				   NISLANDS_SMC_VOLTAGE_VALUE *voltage)
   1329  1.1  riastrad {
   1330  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1331  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1332  1.1  riastrad 
   1333  1.1  riastrad 	if (!pi->mvdd_control) {
   1334  1.1  riastrad 		voltage->index = eg_pi->mvdd_high_index;
   1335  1.2  riastrad 		voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
   1336  1.1  riastrad 		return;
   1337  1.1  riastrad 	}
   1338  1.1  riastrad 
   1339  1.1  riastrad 	if (mclk <= pi->mvdd_split_frequency) {
   1340  1.1  riastrad 		voltage->index = eg_pi->mvdd_low_index;
   1341  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_LOW_VALUE);
   1342  1.1  riastrad 	} else {
   1343  1.1  riastrad 		voltage->index = eg_pi->mvdd_high_index;
   1344  1.1  riastrad 		voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
   1345  1.1  riastrad 	}
   1346  1.1  riastrad }
   1347  1.1  riastrad 
   1348  1.1  riastrad static int ni_get_std_voltage_value(struct radeon_device *rdev,
   1349  1.1  riastrad 				    NISLANDS_SMC_VOLTAGE_VALUE *voltage,
   1350  1.1  riastrad 				    u16 *std_voltage)
   1351  1.1  riastrad {
   1352  1.1  riastrad 	if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries &&
   1353  1.1  riastrad 	    ((u32)voltage->index < rdev->pm.dpm.dyn_state.cac_leakage_table.count))
   1354  1.1  riastrad 		*std_voltage = rdev->pm.dpm.dyn_state.cac_leakage_table.entries[voltage->index].vddc;
   1355  1.1  riastrad 	else
   1356  1.1  riastrad 		*std_voltage = be16_to_cpu(voltage->value);
   1357  1.1  riastrad 
   1358  1.1  riastrad 	return 0;
   1359  1.1  riastrad }
   1360  1.1  riastrad 
   1361  1.1  riastrad static void ni_populate_std_voltage_value(struct radeon_device *rdev,
   1362  1.1  riastrad 					  u16 value, u8 index,
   1363  1.1  riastrad 					  NISLANDS_SMC_VOLTAGE_VALUE *voltage)
   1364  1.1  riastrad {
   1365  1.1  riastrad 	voltage->index = index;
   1366  1.1  riastrad 	voltage->value = cpu_to_be16(value);
   1367  1.1  riastrad }
   1368  1.1  riastrad 
   1369  1.1  riastrad static u32 ni_get_smc_power_scaling_factor(struct radeon_device *rdev)
   1370  1.1  riastrad {
   1371  1.1  riastrad 	u32 xclk_period;
   1372  1.1  riastrad 	u32 xclk = radeon_get_xclk(rdev);
   1373  1.1  riastrad 	u32 tmp = RREG32(CG_CAC_CTRL) & TID_CNT_MASK;
   1374  1.1  riastrad 
   1375  1.1  riastrad 	xclk_period = (1000000000UL / xclk);
   1376  1.1  riastrad 	xclk_period /= 10000UL;
   1377  1.1  riastrad 
   1378  1.1  riastrad 	return tmp * xclk_period;
   1379  1.1  riastrad }
   1380  1.1  riastrad 
   1381  1.1  riastrad static u32 ni_scale_power_for_smc(u32 power_in_watts, u32 scaling_factor)
   1382  1.1  riastrad {
   1383  1.1  riastrad 	return (power_in_watts * scaling_factor) << 2;
   1384  1.1  riastrad }
   1385  1.1  riastrad 
   1386  1.1  riastrad static u32 ni_calculate_power_boost_limit(struct radeon_device *rdev,
   1387  1.1  riastrad 					  struct radeon_ps *radeon_state,
   1388  1.1  riastrad 					  u32 near_tdp_limit)
   1389  1.1  riastrad {
   1390  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   1391  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1392  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1393  1.1  riastrad 	u32 power_boost_limit = 0;
   1394  1.1  riastrad 	int ret;
   1395  1.1  riastrad 
   1396  1.1  riastrad 	if (ni_pi->enable_power_containment &&
   1397  1.1  riastrad 	    ni_pi->use_power_boost_limit) {
   1398  1.1  riastrad 		NISLANDS_SMC_VOLTAGE_VALUE vddc;
   1399  1.1  riastrad 		u16 std_vddc_med;
   1400  1.1  riastrad 		u16 std_vddc_high;
   1401  1.1  riastrad 		u64 tmp, n, d;
   1402  1.1  riastrad 
   1403  1.1  riastrad 		if (state->performance_level_count < 3)
   1404  1.1  riastrad 			return 0;
   1405  1.1  riastrad 
   1406  1.1  riastrad 		ret = ni_populate_voltage_value(rdev, &eg_pi->vddc_voltage_table,
   1407  1.1  riastrad 						state->performance_levels[state->performance_level_count - 2].vddc,
   1408  1.1  riastrad 						&vddc);
   1409  1.1  riastrad 		if (ret)
   1410  1.1  riastrad 			return 0;
   1411  1.1  riastrad 
   1412  1.1  riastrad 		ret = ni_get_std_voltage_value(rdev, &vddc, &std_vddc_med);
   1413  1.1  riastrad 		if (ret)
   1414  1.1  riastrad 			return 0;
   1415  1.1  riastrad 
   1416  1.1  riastrad 		ret = ni_populate_voltage_value(rdev, &eg_pi->vddc_voltage_table,
   1417  1.1  riastrad 						state->performance_levels[state->performance_level_count - 1].vddc,
   1418  1.1  riastrad 						&vddc);
   1419  1.1  riastrad 		if (ret)
   1420  1.1  riastrad 			return 0;
   1421  1.1  riastrad 
   1422  1.1  riastrad 		ret = ni_get_std_voltage_value(rdev, &vddc, &std_vddc_high);
   1423  1.1  riastrad 		if (ret)
   1424  1.1  riastrad 			return 0;
   1425  1.1  riastrad 
   1426  1.1  riastrad 		n = ((u64)near_tdp_limit * ((u64)std_vddc_med * (u64)std_vddc_med) * 90);
   1427  1.1  riastrad 		d = ((u64)std_vddc_high * (u64)std_vddc_high * 100);
   1428  1.1  riastrad 		tmp = div64_u64(n, d);
   1429  1.1  riastrad 
   1430  1.1  riastrad 		if (tmp >> 32)
   1431  1.1  riastrad 			return 0;
   1432  1.1  riastrad 		power_boost_limit = (u32)tmp;
   1433  1.1  riastrad 	}
   1434  1.1  riastrad 
   1435  1.1  riastrad 	return power_boost_limit;
   1436  1.1  riastrad }
   1437  1.1  riastrad 
   1438  1.1  riastrad static int ni_calculate_adjusted_tdp_limits(struct radeon_device *rdev,
   1439  1.1  riastrad 					    bool adjust_polarity,
   1440  1.1  riastrad 					    u32 tdp_adjustment,
   1441  1.1  riastrad 					    u32 *tdp_limit,
   1442  1.1  riastrad 					    u32 *near_tdp_limit)
   1443  1.1  riastrad {
   1444  1.1  riastrad 	if (tdp_adjustment > (u32)rdev->pm.dpm.tdp_od_limit)
   1445  1.1  riastrad 		return -EINVAL;
   1446  1.1  riastrad 
   1447  1.1  riastrad 	if (adjust_polarity) {
   1448  1.1  riastrad 		*tdp_limit = ((100 + tdp_adjustment) * rdev->pm.dpm.tdp_limit) / 100;
   1449  1.1  riastrad 		*near_tdp_limit = rdev->pm.dpm.near_tdp_limit + (*tdp_limit - rdev->pm.dpm.tdp_limit);
   1450  1.1  riastrad 	} else {
   1451  1.1  riastrad 		*tdp_limit = ((100 - tdp_adjustment) * rdev->pm.dpm.tdp_limit) / 100;
   1452  1.1  riastrad 		*near_tdp_limit = rdev->pm.dpm.near_tdp_limit - (rdev->pm.dpm.tdp_limit - *tdp_limit);
   1453  1.1  riastrad 	}
   1454  1.1  riastrad 
   1455  1.1  riastrad 	return 0;
   1456  1.1  riastrad }
   1457  1.1  riastrad 
   1458  1.1  riastrad static int ni_populate_smc_tdp_limits(struct radeon_device *rdev,
   1459  1.1  riastrad 				      struct radeon_ps *radeon_state)
   1460  1.1  riastrad {
   1461  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1462  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1463  1.1  riastrad 
   1464  1.1  riastrad 	if (ni_pi->enable_power_containment) {
   1465  1.1  riastrad 		NISLANDS_SMC_STATETABLE *smc_table = &ni_pi->smc_statetable;
   1466  1.1  riastrad 		u32 scaling_factor = ni_get_smc_power_scaling_factor(rdev);
   1467  1.1  riastrad 		u32 tdp_limit;
   1468  1.1  riastrad 		u32 near_tdp_limit;
   1469  1.1  riastrad 		u32 power_boost_limit;
   1470  1.1  riastrad 		int ret;
   1471  1.1  riastrad 
   1472  1.1  riastrad 		if (scaling_factor == 0)
   1473  1.1  riastrad 			return -EINVAL;
   1474  1.1  riastrad 
   1475  1.1  riastrad 		memset(smc_table, 0, sizeof(NISLANDS_SMC_STATETABLE));
   1476  1.1  riastrad 
   1477  1.1  riastrad 		ret = ni_calculate_adjusted_tdp_limits(rdev,
   1478  1.1  riastrad 						       false, /* ??? */
   1479  1.1  riastrad 						       rdev->pm.dpm.tdp_adjustment,
   1480  1.1  riastrad 						       &tdp_limit,
   1481  1.1  riastrad 						       &near_tdp_limit);
   1482  1.1  riastrad 		if (ret)
   1483  1.1  riastrad 			return ret;
   1484  1.1  riastrad 
   1485  1.1  riastrad 		power_boost_limit = ni_calculate_power_boost_limit(rdev, radeon_state,
   1486  1.1  riastrad 								   near_tdp_limit);
   1487  1.1  riastrad 
   1488  1.1  riastrad 		smc_table->dpm2Params.TDPLimit =
   1489  1.1  riastrad 			cpu_to_be32(ni_scale_power_for_smc(tdp_limit, scaling_factor));
   1490  1.1  riastrad 		smc_table->dpm2Params.NearTDPLimit =
   1491  1.1  riastrad 			cpu_to_be32(ni_scale_power_for_smc(near_tdp_limit, scaling_factor));
   1492  1.1  riastrad 		smc_table->dpm2Params.SafePowerLimit =
   1493  1.1  riastrad 			cpu_to_be32(ni_scale_power_for_smc((near_tdp_limit * NISLANDS_DPM2_TDP_SAFE_LIMIT_PERCENT) / 100,
   1494  1.1  riastrad 							   scaling_factor));
   1495  1.1  riastrad 		smc_table->dpm2Params.PowerBoostLimit =
   1496  1.1  riastrad 			cpu_to_be32(ni_scale_power_for_smc(power_boost_limit, scaling_factor));
   1497  1.1  riastrad 
   1498  1.1  riastrad 		ret = rv770_copy_bytes_to_smc(rdev,
   1499  1.1  riastrad 					      (u16)(pi->state_table_start + offsetof(NISLANDS_SMC_STATETABLE, dpm2Params) +
   1500  1.1  riastrad 						    offsetof(PP_NIslands_DPM2Parameters, TDPLimit)),
   1501  1.1  riastrad 					      (u8 *)(&smc_table->dpm2Params.TDPLimit),
   1502  1.1  riastrad 					      sizeof(u32) * 4, pi->sram_end);
   1503  1.1  riastrad 		if (ret)
   1504  1.1  riastrad 			return ret;
   1505  1.1  riastrad 	}
   1506  1.1  riastrad 
   1507  1.1  riastrad 	return 0;
   1508  1.1  riastrad }
   1509  1.1  riastrad 
   1510  1.1  riastrad int ni_copy_and_switch_arb_sets(struct radeon_device *rdev,
   1511  1.1  riastrad 				u32 arb_freq_src, u32 arb_freq_dest)
   1512  1.1  riastrad {
   1513  1.1  riastrad 	u32 mc_arb_dram_timing;
   1514  1.1  riastrad 	u32 mc_arb_dram_timing2;
   1515  1.1  riastrad 	u32 burst_time;
   1516  1.1  riastrad 	u32 mc_cg_config;
   1517  1.1  riastrad 
   1518  1.1  riastrad 	switch (arb_freq_src) {
   1519  1.2  riastrad 	case MC_CG_ARB_FREQ_F0:
   1520  1.1  riastrad 		mc_arb_dram_timing  = RREG32(MC_ARB_DRAM_TIMING);
   1521  1.1  riastrad 		mc_arb_dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
   1522  1.1  riastrad 		burst_time = (RREG32(MC_ARB_BURST_TIME) & STATE0_MASK) >> STATE0_SHIFT;
   1523  1.1  riastrad 		break;
   1524  1.2  riastrad 	case MC_CG_ARB_FREQ_F1:
   1525  1.1  riastrad 		mc_arb_dram_timing  = RREG32(MC_ARB_DRAM_TIMING_1);
   1526  1.1  riastrad 		mc_arb_dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2_1);
   1527  1.1  riastrad 		burst_time = (RREG32(MC_ARB_BURST_TIME) & STATE1_MASK) >> STATE1_SHIFT;
   1528  1.1  riastrad 		break;
   1529  1.2  riastrad 	case MC_CG_ARB_FREQ_F2:
   1530  1.1  riastrad 		mc_arb_dram_timing  = RREG32(MC_ARB_DRAM_TIMING_2);
   1531  1.1  riastrad 		mc_arb_dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2_2);
   1532  1.1  riastrad 		burst_time = (RREG32(MC_ARB_BURST_TIME) & STATE2_MASK) >> STATE2_SHIFT;
   1533  1.1  riastrad 		break;
   1534  1.2  riastrad 	case MC_CG_ARB_FREQ_F3:
   1535  1.1  riastrad 		mc_arb_dram_timing  = RREG32(MC_ARB_DRAM_TIMING_3);
   1536  1.1  riastrad 		mc_arb_dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2_3);
   1537  1.1  riastrad 		burst_time = (RREG32(MC_ARB_BURST_TIME) & STATE3_MASK) >> STATE3_SHIFT;
   1538  1.1  riastrad 		break;
   1539  1.2  riastrad 	default:
   1540  1.1  riastrad 		return -EINVAL;
   1541  1.1  riastrad 	}
   1542  1.1  riastrad 
   1543  1.1  riastrad 	switch (arb_freq_dest) {
   1544  1.2  riastrad 	case MC_CG_ARB_FREQ_F0:
   1545  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING, mc_arb_dram_timing);
   1546  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING2, mc_arb_dram_timing2);
   1547  1.1  riastrad 		WREG32_P(MC_ARB_BURST_TIME, STATE0(burst_time), ~STATE0_MASK);
   1548  1.1  riastrad 		break;
   1549  1.2  riastrad 	case MC_CG_ARB_FREQ_F1:
   1550  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING_1, mc_arb_dram_timing);
   1551  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING2_1, mc_arb_dram_timing2);
   1552  1.1  riastrad 		WREG32_P(MC_ARB_BURST_TIME, STATE1(burst_time), ~STATE1_MASK);
   1553  1.1  riastrad 		break;
   1554  1.2  riastrad 	case MC_CG_ARB_FREQ_F2:
   1555  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING_2, mc_arb_dram_timing);
   1556  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING2_2, mc_arb_dram_timing2);
   1557  1.1  riastrad 		WREG32_P(MC_ARB_BURST_TIME, STATE2(burst_time), ~STATE2_MASK);
   1558  1.1  riastrad 		break;
   1559  1.2  riastrad 	case MC_CG_ARB_FREQ_F3:
   1560  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING_3, mc_arb_dram_timing);
   1561  1.1  riastrad 		WREG32(MC_ARB_DRAM_TIMING2_3, mc_arb_dram_timing2);
   1562  1.1  riastrad 		WREG32_P(MC_ARB_BURST_TIME, STATE3(burst_time), ~STATE3_MASK);
   1563  1.1  riastrad 		break;
   1564  1.1  riastrad 	default:
   1565  1.1  riastrad 		return -EINVAL;
   1566  1.1  riastrad 	}
   1567  1.1  riastrad 
   1568  1.1  riastrad 	mc_cg_config = RREG32(MC_CG_CONFIG) | 0x0000000F;
   1569  1.1  riastrad 	WREG32(MC_CG_CONFIG, mc_cg_config);
   1570  1.1  riastrad 	WREG32_P(MC_ARB_CG, CG_ARB_REQ(arb_freq_dest), ~CG_ARB_REQ_MASK);
   1571  1.1  riastrad 
   1572  1.1  riastrad 	return 0;
   1573  1.1  riastrad }
   1574  1.1  riastrad 
   1575  1.1  riastrad static int ni_init_arb_table_index(struct radeon_device *rdev)
   1576  1.1  riastrad {
   1577  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1578  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1579  1.1  riastrad 	u32 tmp;
   1580  1.1  riastrad 	int ret;
   1581  1.1  riastrad 
   1582  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev, ni_pi->arb_table_start,
   1583  1.1  riastrad 					&tmp, pi->sram_end);
   1584  1.1  riastrad 	if (ret)
   1585  1.1  riastrad 		return ret;
   1586  1.1  riastrad 
   1587  1.1  riastrad 	tmp &= 0x00FFFFFF;
   1588  1.1  riastrad 	tmp |= ((u32)MC_CG_ARB_FREQ_F1) << 24;
   1589  1.1  riastrad 
   1590  1.1  riastrad 	return rv770_write_smc_sram_dword(rdev, ni_pi->arb_table_start,
   1591  1.1  riastrad 					  tmp, pi->sram_end);
   1592  1.1  riastrad }
   1593  1.1  riastrad 
   1594  1.1  riastrad static int ni_initial_switch_from_arb_f0_to_f1(struct radeon_device *rdev)
   1595  1.1  riastrad {
   1596  1.1  riastrad 	return ni_copy_and_switch_arb_sets(rdev, MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
   1597  1.1  riastrad }
   1598  1.1  riastrad 
   1599  1.1  riastrad static int ni_force_switch_to_arb_f0(struct radeon_device *rdev)
   1600  1.1  riastrad {
   1601  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1602  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1603  1.1  riastrad 	u32 tmp;
   1604  1.1  riastrad 	int ret;
   1605  1.1  riastrad 
   1606  1.1  riastrad 	ret = rv770_read_smc_sram_dword(rdev, ni_pi->arb_table_start,
   1607  1.1  riastrad 					&tmp, pi->sram_end);
   1608  1.1  riastrad 	if (ret)
   1609  1.1  riastrad 		return ret;
   1610  1.1  riastrad 
   1611  1.1  riastrad 	tmp = (tmp >> 24) & 0xff;
   1612  1.1  riastrad 
   1613  1.1  riastrad 	if (tmp == MC_CG_ARB_FREQ_F0)
   1614  1.1  riastrad 		return 0;
   1615  1.1  riastrad 
   1616  1.1  riastrad 	return ni_copy_and_switch_arb_sets(rdev, tmp, MC_CG_ARB_FREQ_F0);
   1617  1.1  riastrad }
   1618  1.1  riastrad 
   1619  1.1  riastrad static int ni_populate_memory_timing_parameters(struct radeon_device *rdev,
   1620  1.1  riastrad 						struct rv7xx_pl *pl,
   1621  1.1  riastrad 						SMC_NIslands_MCArbDramTimingRegisterSet *arb_regs)
   1622  1.1  riastrad {
   1623  1.1  riastrad 	u32 dram_timing;
   1624  1.1  riastrad 	u32 dram_timing2;
   1625  1.1  riastrad 
   1626  1.1  riastrad 	arb_regs->mc_arb_rfsh_rate =
   1627  1.1  riastrad 		(u8)rv770_calculate_memory_refresh_rate(rdev, pl->sclk);
   1628  1.1  riastrad 
   1629  1.1  riastrad 
   1630  1.2  riastrad 	radeon_atom_set_engine_dram_timings(rdev, pl->sclk, pl->mclk);
   1631  1.1  riastrad 
   1632  1.1  riastrad 	dram_timing = RREG32(MC_ARB_DRAM_TIMING);
   1633  1.1  riastrad 	dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
   1634  1.1  riastrad 
   1635  1.1  riastrad 	arb_regs->mc_arb_dram_timing  = cpu_to_be32(dram_timing);
   1636  1.1  riastrad 	arb_regs->mc_arb_dram_timing2 = cpu_to_be32(dram_timing2);
   1637  1.1  riastrad 
   1638  1.1  riastrad 	return 0;
   1639  1.1  riastrad }
   1640  1.1  riastrad 
   1641  1.1  riastrad static int ni_do_program_memory_timing_parameters(struct radeon_device *rdev,
   1642  1.1  riastrad 						  struct radeon_ps *radeon_state,
   1643  1.1  riastrad 						  unsigned int first_arb_set)
   1644  1.1  riastrad {
   1645  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1646  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1647  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   1648  1.1  riastrad 	SMC_NIslands_MCArbDramTimingRegisterSet arb_regs = { 0 };
   1649  1.1  riastrad 	int i, ret = 0;
   1650  1.1  riastrad 
   1651  1.1  riastrad 	for (i = 0; i < state->performance_level_count; i++) {
   1652  1.1  riastrad 		ret = ni_populate_memory_timing_parameters(rdev, &state->performance_levels[i], &arb_regs);
   1653  1.1  riastrad 		if (ret)
   1654  1.1  riastrad 			break;
   1655  1.1  riastrad 
   1656  1.1  riastrad 		ret = rv770_copy_bytes_to_smc(rdev,
   1657  1.1  riastrad 					      (u16)(ni_pi->arb_table_start +
   1658  1.1  riastrad 						    offsetof(SMC_NIslands_MCArbDramTimingRegisters, data) +
   1659  1.1  riastrad 						    sizeof(SMC_NIslands_MCArbDramTimingRegisterSet) * (first_arb_set + i)),
   1660  1.1  riastrad 					      (u8 *)&arb_regs,
   1661  1.1  riastrad 					      (u16)sizeof(SMC_NIslands_MCArbDramTimingRegisterSet),
   1662  1.1  riastrad 					      pi->sram_end);
   1663  1.1  riastrad 		if (ret)
   1664  1.1  riastrad 			break;
   1665  1.1  riastrad 	}
   1666  1.1  riastrad 	return ret;
   1667  1.1  riastrad }
   1668  1.1  riastrad 
   1669  1.1  riastrad static int ni_program_memory_timing_parameters(struct radeon_device *rdev,
   1670  1.1  riastrad 					       struct radeon_ps *radeon_new_state)
   1671  1.1  riastrad {
   1672  1.1  riastrad 	return ni_do_program_memory_timing_parameters(rdev, radeon_new_state,
   1673  1.1  riastrad 						      NISLANDS_DRIVER_STATE_ARB_INDEX);
   1674  1.1  riastrad }
   1675  1.1  riastrad 
   1676  1.1  riastrad static void ni_populate_initial_mvdd_value(struct radeon_device *rdev,
   1677  1.1  riastrad 					   struct NISLANDS_SMC_VOLTAGE_VALUE *voltage)
   1678  1.1  riastrad {
   1679  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1680  1.1  riastrad 
   1681  1.1  riastrad 	voltage->index = eg_pi->mvdd_high_index;
   1682  1.1  riastrad 	voltage->value = cpu_to_be16(MVDD_HIGH_VALUE);
   1683  1.1  riastrad }
   1684  1.1  riastrad 
   1685  1.1  riastrad static int ni_populate_smc_initial_state(struct radeon_device *rdev,
   1686  1.1  riastrad 					 struct radeon_ps *radeon_initial_state,
   1687  1.1  riastrad 					 NISLANDS_SMC_STATETABLE *table)
   1688  1.1  riastrad {
   1689  1.1  riastrad 	struct ni_ps *initial_state = ni_get_ps(radeon_initial_state);
   1690  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1691  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1692  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1693  1.1  riastrad 	u32 reg;
   1694  1.1  riastrad 	int ret;
   1695  1.1  riastrad 
   1696  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_AD_FUNC_CNTL =
   1697  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_ad_func_cntl);
   1698  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_AD_FUNC_CNTL_2 =
   1699  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_ad_func_cntl_2);
   1700  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_DQ_FUNC_CNTL =
   1701  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_dq_func_cntl);
   1702  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_DQ_FUNC_CNTL_2 =
   1703  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_dq_func_cntl_2);
   1704  1.1  riastrad 	table->initialState.levels[0].mclk.vMCLK_PWRMGT_CNTL =
   1705  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mclk_pwrmgt_cntl);
   1706  1.1  riastrad 	table->initialState.levels[0].mclk.vDLL_CNTL =
   1707  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.dll_cntl);
   1708  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_SS =
   1709  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_ss1);
   1710  1.1  riastrad 	table->initialState.levels[0].mclk.vMPLL_SS2 =
   1711  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.mpll_ss2);
   1712  1.1  riastrad 	table->initialState.levels[0].mclk.mclk_value =
   1713  1.1  riastrad 		cpu_to_be32(initial_state->performance_levels[0].mclk);
   1714  1.1  riastrad 
   1715  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL =
   1716  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_func_cntl);
   1717  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_2 =
   1718  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_func_cntl_2);
   1719  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_3 =
   1720  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_func_cntl_3);
   1721  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_4 =
   1722  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_func_cntl_4);
   1723  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_SPREAD_SPECTRUM =
   1724  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_spread_spectrum);
   1725  1.1  riastrad 	table->initialState.levels[0].sclk.vCG_SPLL_SPREAD_SPECTRUM_2 =
   1726  1.1  riastrad 		cpu_to_be32(ni_pi->clock_registers.cg_spll_spread_spectrum_2);
   1727  1.1  riastrad 	table->initialState.levels[0].sclk.sclk_value =
   1728  1.1  riastrad 		cpu_to_be32(initial_state->performance_levels[0].sclk);
   1729  1.1  riastrad 	table->initialState.levels[0].arbRefreshState =
   1730  1.1  riastrad 		NISLANDS_INITIAL_STATE_ARB_INDEX;
   1731  1.1  riastrad 
   1732  1.1  riastrad 	table->initialState.levels[0].ACIndex = 0;
   1733  1.1  riastrad 
   1734  1.1  riastrad 	ret = ni_populate_voltage_value(rdev, &eg_pi->vddc_voltage_table,
   1735  1.1  riastrad 					initial_state->performance_levels[0].vddc,
   1736  1.1  riastrad 					&table->initialState.levels[0].vddc);
   1737  1.1  riastrad 	if (!ret) {
   1738  1.1  riastrad 		u16 std_vddc;
   1739  1.1  riastrad 
   1740  1.1  riastrad 		ret = ni_get_std_voltage_value(rdev,
   1741  1.1  riastrad 					       &table->initialState.levels[0].vddc,
   1742  1.1  riastrad 					       &std_vddc);
   1743  1.1  riastrad 		if (!ret)
   1744  1.1  riastrad 			ni_populate_std_voltage_value(rdev, std_vddc,
   1745  1.1  riastrad 						      table->initialState.levels[0].vddc.index,
   1746  1.1  riastrad 						      &table->initialState.levels[0].std_vddc);
   1747  1.1  riastrad 	}
   1748  1.1  riastrad 
   1749  1.1  riastrad 	if (eg_pi->vddci_control)
   1750  1.1  riastrad 		ni_populate_voltage_value(rdev,
   1751  1.1  riastrad 					  &eg_pi->vddci_voltage_table,
   1752  1.1  riastrad 					  initial_state->performance_levels[0].vddci,
   1753  1.1  riastrad 					  &table->initialState.levels[0].vddci);
   1754  1.1  riastrad 
   1755  1.1  riastrad 	ni_populate_initial_mvdd_value(rdev, &table->initialState.levels[0].mvdd);
   1756  1.1  riastrad 
   1757  1.1  riastrad 	reg = CG_R(0xffff) | CG_L(0);
   1758  1.1  riastrad 	table->initialState.levels[0].aT = cpu_to_be32(reg);
   1759  1.1  riastrad 
   1760  1.1  riastrad 	table->initialState.levels[0].bSP = cpu_to_be32(pi->dsp);
   1761  1.1  riastrad 
   1762  1.1  riastrad 	if (pi->boot_in_gen2)
   1763  1.1  riastrad 		table->initialState.levels[0].gen2PCIE = 1;
   1764  1.1  riastrad 	else
   1765  1.1  riastrad 		table->initialState.levels[0].gen2PCIE = 0;
   1766  1.1  riastrad 
   1767  1.1  riastrad 	if (pi->mem_gddr5) {
   1768  1.1  riastrad 		table->initialState.levels[0].strobeMode =
   1769  1.1  riastrad 			cypress_get_strobe_mode_settings(rdev,
   1770  1.1  riastrad 							 initial_state->performance_levels[0].mclk);
   1771  1.1  riastrad 
   1772  1.1  riastrad 		if (initial_state->performance_levels[0].mclk > pi->mclk_edc_enable_threshold)
   1773  1.1  riastrad 			table->initialState.levels[0].mcFlags = NISLANDS_SMC_MC_EDC_RD_FLAG | NISLANDS_SMC_MC_EDC_WR_FLAG;
   1774  1.1  riastrad 		else
   1775  1.1  riastrad 			table->initialState.levels[0].mcFlags =  0;
   1776  1.1  riastrad 	}
   1777  1.1  riastrad 
   1778  1.1  riastrad 	table->initialState.levelCount = 1;
   1779  1.1  riastrad 
   1780  1.1  riastrad 	table->initialState.flags |= PPSMC_SWSTATE_FLAG_DC;
   1781  1.1  riastrad 
   1782  1.1  riastrad 	table->initialState.levels[0].dpm2.MaxPS = 0;
   1783  1.1  riastrad 	table->initialState.levels[0].dpm2.NearTDPDec = 0;
   1784  1.1  riastrad 	table->initialState.levels[0].dpm2.AboveSafeInc = 0;
   1785  1.1  riastrad 	table->initialState.levels[0].dpm2.BelowSafeInc = 0;
   1786  1.1  riastrad 
   1787  1.1  riastrad 	reg = MIN_POWER_MASK | MAX_POWER_MASK;
   1788  1.1  riastrad 	table->initialState.levels[0].SQPowerThrottle = cpu_to_be32(reg);
   1789  1.1  riastrad 
   1790  1.1  riastrad 	reg = MAX_POWER_DELTA_MASK | STI_SIZE_MASK | LTI_RATIO_MASK;
   1791  1.1  riastrad 	table->initialState.levels[0].SQPowerThrottle_2 = cpu_to_be32(reg);
   1792  1.1  riastrad 
   1793  1.1  riastrad 	return 0;
   1794  1.1  riastrad }
   1795  1.1  riastrad 
   1796  1.1  riastrad static int ni_populate_smc_acpi_state(struct radeon_device *rdev,
   1797  1.1  riastrad 				      NISLANDS_SMC_STATETABLE *table)
   1798  1.1  riastrad {
   1799  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1800  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   1801  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1802  1.1  riastrad 	u32 mpll_ad_func_cntl   = ni_pi->clock_registers.mpll_ad_func_cntl;
   1803  1.1  riastrad 	u32 mpll_ad_func_cntl_2 = ni_pi->clock_registers.mpll_ad_func_cntl_2;
   1804  1.1  riastrad 	u32 mpll_dq_func_cntl   = ni_pi->clock_registers.mpll_dq_func_cntl;
   1805  1.1  riastrad 	u32 mpll_dq_func_cntl_2 = ni_pi->clock_registers.mpll_dq_func_cntl_2;
   1806  1.1  riastrad 	u32 spll_func_cntl      = ni_pi->clock_registers.cg_spll_func_cntl;
   1807  1.1  riastrad 	u32 spll_func_cntl_2    = ni_pi->clock_registers.cg_spll_func_cntl_2;
   1808  1.1  riastrad 	u32 spll_func_cntl_3    = ni_pi->clock_registers.cg_spll_func_cntl_3;
   1809  1.1  riastrad 	u32 spll_func_cntl_4    = ni_pi->clock_registers.cg_spll_func_cntl_4;
   1810  1.1  riastrad 	u32 mclk_pwrmgt_cntl    = ni_pi->clock_registers.mclk_pwrmgt_cntl;
   1811  1.1  riastrad 	u32 dll_cntl            = ni_pi->clock_registers.dll_cntl;
   1812  1.1  riastrad 	u32 reg;
   1813  1.1  riastrad 	int ret;
   1814  1.1  riastrad 
   1815  1.1  riastrad 	table->ACPIState = table->initialState;
   1816  1.1  riastrad 
   1817  1.1  riastrad 	table->ACPIState.flags &= ~PPSMC_SWSTATE_FLAG_DC;
   1818  1.1  riastrad 
   1819  1.1  riastrad 	if (pi->acpi_vddc) {
   1820  1.1  riastrad 		ret = ni_populate_voltage_value(rdev,
   1821  1.1  riastrad 						&eg_pi->vddc_voltage_table,
   1822  1.1  riastrad 						pi->acpi_vddc, &table->ACPIState.levels[0].vddc);
   1823  1.1  riastrad 		if (!ret) {
   1824  1.1  riastrad 			u16 std_vddc;
   1825  1.1  riastrad 
   1826  1.1  riastrad 			ret = ni_get_std_voltage_value(rdev,
   1827  1.1  riastrad 						       &table->ACPIState.levels[0].vddc, &std_vddc);
   1828  1.1  riastrad 			if (!ret)
   1829  1.1  riastrad 				ni_populate_std_voltage_value(rdev, std_vddc,
   1830  1.1  riastrad 							      table->ACPIState.levels[0].vddc.index,
   1831  1.1  riastrad 							      &table->ACPIState.levels[0].std_vddc);
   1832  1.1  riastrad 		}
   1833  1.1  riastrad 
   1834  1.1  riastrad 		if (pi->pcie_gen2) {
   1835  1.1  riastrad 			if (pi->acpi_pcie_gen2)
   1836  1.1  riastrad 				table->ACPIState.levels[0].gen2PCIE = 1;
   1837  1.1  riastrad 			else
   1838  1.1  riastrad 				table->ACPIState.levels[0].gen2PCIE = 0;
   1839  1.1  riastrad 		} else {
   1840  1.1  riastrad 			table->ACPIState.levels[0].gen2PCIE = 0;
   1841  1.1  riastrad 		}
   1842  1.1  riastrad 	} else {
   1843  1.1  riastrad 		ret = ni_populate_voltage_value(rdev,
   1844  1.1  riastrad 						&eg_pi->vddc_voltage_table,
   1845  1.1  riastrad 						pi->min_vddc_in_table,
   1846  1.1  riastrad 						&table->ACPIState.levels[0].vddc);
   1847  1.1  riastrad 		if (!ret) {
   1848  1.1  riastrad 			u16 std_vddc;
   1849  1.1  riastrad 
   1850  1.1  riastrad 			ret = ni_get_std_voltage_value(rdev,
   1851  1.1  riastrad 						       &table->ACPIState.levels[0].vddc,
   1852  1.1  riastrad 						       &std_vddc);
   1853  1.1  riastrad 			if (!ret)
   1854  1.1  riastrad 				ni_populate_std_voltage_value(rdev, std_vddc,
   1855  1.1  riastrad 							      table->ACPIState.levels[0].vddc.index,
   1856  1.1  riastrad 							      &table->ACPIState.levels[0].std_vddc);
   1857  1.1  riastrad 		}
   1858  1.1  riastrad 		table->ACPIState.levels[0].gen2PCIE = 0;
   1859  1.1  riastrad 	}
   1860  1.1  riastrad 
   1861  1.1  riastrad 	if (eg_pi->acpi_vddci) {
   1862  1.1  riastrad 		if (eg_pi->vddci_control)
   1863  1.1  riastrad 			ni_populate_voltage_value(rdev,
   1864  1.1  riastrad 						  &eg_pi->vddci_voltage_table,
   1865  1.1  riastrad 						  eg_pi->acpi_vddci,
   1866  1.1  riastrad 						  &table->ACPIState.levels[0].vddci);
   1867  1.1  riastrad 	}
   1868  1.1  riastrad 
   1869  1.1  riastrad 
   1870  1.1  riastrad 	mpll_ad_func_cntl &= ~PDNB;
   1871  1.1  riastrad 
   1872  1.1  riastrad 	mpll_ad_func_cntl_2 |= BIAS_GEN_PDNB | RESET_EN;
   1873  1.1  riastrad 
   1874  1.2  riastrad 	if (pi->mem_gddr5)
   1875  1.2  riastrad 		mpll_dq_func_cntl &= ~PDNB;
   1876  1.2  riastrad 	mpll_dq_func_cntl_2 |= BIAS_GEN_PDNB | RESET_EN | BYPASS;
   1877  1.1  riastrad 
   1878  1.1  riastrad 
   1879  1.1  riastrad 	mclk_pwrmgt_cntl |= (MRDCKA0_RESET |
   1880  1.1  riastrad 			     MRDCKA1_RESET |
   1881  1.1  riastrad 			     MRDCKB0_RESET |
   1882  1.1  riastrad 			     MRDCKB1_RESET |
   1883  1.1  riastrad 			     MRDCKC0_RESET |
   1884  1.1  riastrad 			     MRDCKC1_RESET |
   1885  1.1  riastrad 			     MRDCKD0_RESET |
   1886  1.1  riastrad 			     MRDCKD1_RESET);
   1887  1.1  riastrad 
   1888  1.1  riastrad 	mclk_pwrmgt_cntl &= ~(MRDCKA0_PDNB |
   1889  1.1  riastrad 			      MRDCKA1_PDNB |
   1890  1.1  riastrad 			      MRDCKB0_PDNB |
   1891  1.1  riastrad 			      MRDCKB1_PDNB |
   1892  1.1  riastrad 			      MRDCKC0_PDNB |
   1893  1.1  riastrad 			      MRDCKC1_PDNB |
   1894  1.1  riastrad 			      MRDCKD0_PDNB |
   1895  1.1  riastrad 			      MRDCKD1_PDNB);
   1896  1.1  riastrad 
   1897  1.1  riastrad 	dll_cntl |= (MRDCKA0_BYPASS |
   1898  1.2  riastrad 		     MRDCKA1_BYPASS |
   1899  1.2  riastrad 		     MRDCKB0_BYPASS |
   1900  1.2  riastrad 		     MRDCKB1_BYPASS |
   1901  1.2  riastrad 		     MRDCKC0_BYPASS |
   1902  1.2  riastrad 		     MRDCKC1_BYPASS |
   1903  1.2  riastrad 		     MRDCKD0_BYPASS |
   1904  1.2  riastrad 		     MRDCKD1_BYPASS);
   1905  1.1  riastrad 
   1906  1.2  riastrad 	spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
   1907  1.1  riastrad 	spll_func_cntl_2 |= SCLK_MUX_SEL(4);
   1908  1.1  riastrad 
   1909  1.1  riastrad 	table->ACPIState.levels[0].mclk.vMPLL_AD_FUNC_CNTL = cpu_to_be32(mpll_ad_func_cntl);
   1910  1.1  riastrad 	table->ACPIState.levels[0].mclk.vMPLL_AD_FUNC_CNTL_2 = cpu_to_be32(mpll_ad_func_cntl_2);
   1911  1.1  riastrad 	table->ACPIState.levels[0].mclk.vMPLL_DQ_FUNC_CNTL = cpu_to_be32(mpll_dq_func_cntl);
   1912  1.1  riastrad 	table->ACPIState.levels[0].mclk.vMPLL_DQ_FUNC_CNTL_2 = cpu_to_be32(mpll_dq_func_cntl_2);
   1913  1.1  riastrad 	table->ACPIState.levels[0].mclk.vMCLK_PWRMGT_CNTL = cpu_to_be32(mclk_pwrmgt_cntl);
   1914  1.1  riastrad 	table->ACPIState.levels[0].mclk.vDLL_CNTL = cpu_to_be32(dll_cntl);
   1915  1.1  riastrad 
   1916  1.1  riastrad 	table->ACPIState.levels[0].mclk.mclk_value = 0;
   1917  1.1  riastrad 
   1918  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL = cpu_to_be32(spll_func_cntl);
   1919  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_2 = cpu_to_be32(spll_func_cntl_2);
   1920  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_3 = cpu_to_be32(spll_func_cntl_3);
   1921  1.1  riastrad 	table->ACPIState.levels[0].sclk.vCG_SPLL_FUNC_CNTL_4 = cpu_to_be32(spll_func_cntl_4);
   1922  1.1  riastrad 
   1923  1.1  riastrad 	table->ACPIState.levels[0].sclk.sclk_value = 0;
   1924  1.1  riastrad 
   1925  1.1  riastrad 	ni_populate_mvdd_value(rdev, 0, &table->ACPIState.levels[0].mvdd);
   1926  1.1  riastrad 
   1927  1.1  riastrad 	if (eg_pi->dynamic_ac_timing)
   1928  1.1  riastrad 		table->ACPIState.levels[0].ACIndex = 1;
   1929  1.1  riastrad 
   1930  1.1  riastrad 	table->ACPIState.levels[0].dpm2.MaxPS = 0;
   1931  1.1  riastrad 	table->ACPIState.levels[0].dpm2.NearTDPDec = 0;
   1932  1.1  riastrad 	table->ACPIState.levels[0].dpm2.AboveSafeInc = 0;
   1933  1.1  riastrad 	table->ACPIState.levels[0].dpm2.BelowSafeInc = 0;
   1934  1.1  riastrad 
   1935  1.1  riastrad 	reg = MIN_POWER_MASK | MAX_POWER_MASK;
   1936  1.1  riastrad 	table->ACPIState.levels[0].SQPowerThrottle = cpu_to_be32(reg);
   1937  1.1  riastrad 
   1938  1.1  riastrad 	reg = MAX_POWER_DELTA_MASK | STI_SIZE_MASK | LTI_RATIO_MASK;
   1939  1.1  riastrad 	table->ACPIState.levels[0].SQPowerThrottle_2 = cpu_to_be32(reg);
   1940  1.1  riastrad 
   1941  1.1  riastrad 	return 0;
   1942  1.1  riastrad }
   1943  1.1  riastrad 
   1944  1.1  riastrad static int ni_init_smc_table(struct radeon_device *rdev)
   1945  1.1  riastrad {
   1946  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   1947  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   1948  1.1  riastrad 	int ret;
   1949  1.1  riastrad 	struct radeon_ps *radeon_boot_state = rdev->pm.dpm.boot_ps;
   1950  1.1  riastrad 	NISLANDS_SMC_STATETABLE *table = &ni_pi->smc_statetable;
   1951  1.1  riastrad 
   1952  1.1  riastrad 	memset(table, 0, sizeof(NISLANDS_SMC_STATETABLE));
   1953  1.1  riastrad 
   1954  1.1  riastrad 	ni_populate_smc_voltage_tables(rdev, table);
   1955  1.1  riastrad 
   1956  1.1  riastrad 	switch (rdev->pm.int_thermal_type) {
   1957  1.1  riastrad 	case THERMAL_TYPE_NI:
   1958  1.1  riastrad 	case THERMAL_TYPE_EMC2103_WITH_INTERNAL:
   1959  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_INTERNAL;
   1960  1.1  riastrad 		break;
   1961  1.1  riastrad 	case THERMAL_TYPE_NONE:
   1962  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_NONE;
   1963  1.1  riastrad 		break;
   1964  1.1  riastrad 	default:
   1965  1.1  riastrad 		table->thermalProtectType = PPSMC_THERMAL_PROTECT_TYPE_EXTERNAL;
   1966  1.1  riastrad 		break;
   1967  1.1  riastrad 	}
   1968  1.1  riastrad 
   1969  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC)
   1970  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
   1971  1.1  riastrad 
   1972  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT)
   1973  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_REGULATOR_HOT;
   1974  1.1  riastrad 
   1975  1.1  riastrad 	if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
   1976  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
   1977  1.1  riastrad 
   1978  1.1  riastrad 	if (pi->mem_gddr5)
   1979  1.1  riastrad 		table->systemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
   1980  1.1  riastrad 
   1981  1.1  riastrad 	ret = ni_populate_smc_initial_state(rdev, radeon_boot_state, table);
   1982  1.1  riastrad 	if (ret)
   1983  1.1  riastrad 		return ret;
   1984  1.1  riastrad 
   1985  1.1  riastrad 	ret = ni_populate_smc_acpi_state(rdev, table);
   1986  1.1  riastrad 	if (ret)
   1987  1.1  riastrad 		return ret;
   1988  1.1  riastrad 
   1989  1.1  riastrad 	table->driverState = table->initialState;
   1990  1.1  riastrad 
   1991  1.1  riastrad 	table->ULVState = table->initialState;
   1992  1.1  riastrad 
   1993  1.1  riastrad 	ret = ni_do_program_memory_timing_parameters(rdev, radeon_boot_state,
   1994  1.1  riastrad 						     NISLANDS_INITIAL_STATE_ARB_INDEX);
   1995  1.1  riastrad 	if (ret)
   1996  1.1  riastrad 		return ret;
   1997  1.1  riastrad 
   1998  1.1  riastrad 	return rv770_copy_bytes_to_smc(rdev, pi->state_table_start, (u8 *)table,
   1999  1.1  riastrad 				       sizeof(NISLANDS_SMC_STATETABLE), pi->sram_end);
   2000  1.1  riastrad }
   2001  1.1  riastrad 
   2002  1.1  riastrad static int ni_calculate_sclk_params(struct radeon_device *rdev,
   2003  1.1  riastrad 				    u32 engine_clock,
   2004  1.1  riastrad 				    NISLANDS_SMC_SCLK_VALUE *sclk)
   2005  1.1  riastrad {
   2006  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2007  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2008  1.1  riastrad 	struct atom_clock_dividers dividers;
   2009  1.1  riastrad 	u32 spll_func_cntl = ni_pi->clock_registers.cg_spll_func_cntl;
   2010  1.1  riastrad 	u32 spll_func_cntl_2 = ni_pi->clock_registers.cg_spll_func_cntl_2;
   2011  1.1  riastrad 	u32 spll_func_cntl_3 = ni_pi->clock_registers.cg_spll_func_cntl_3;
   2012  1.1  riastrad 	u32 spll_func_cntl_4 = ni_pi->clock_registers.cg_spll_func_cntl_4;
   2013  1.1  riastrad 	u32 cg_spll_spread_spectrum = ni_pi->clock_registers.cg_spll_spread_spectrum;
   2014  1.1  riastrad 	u32 cg_spll_spread_spectrum_2 = ni_pi->clock_registers.cg_spll_spread_spectrum_2;
   2015  1.1  riastrad 	u64 tmp;
   2016  1.1  riastrad 	u32 reference_clock = rdev->clock.spll.reference_freq;
   2017  1.1  riastrad 	u32 reference_divider;
   2018  1.1  riastrad 	u32 fbdiv;
   2019  1.1  riastrad 	int ret;
   2020  1.1  riastrad 
   2021  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
   2022  1.1  riastrad 					     engine_clock, false, &dividers);
   2023  1.1  riastrad 	if (ret)
   2024  1.1  riastrad 		return ret;
   2025  1.1  riastrad 
   2026  1.1  riastrad 	reference_divider = 1 + dividers.ref_div;
   2027  1.1  riastrad 
   2028  1.1  riastrad 
   2029  1.1  riastrad 	tmp = (u64) engine_clock * reference_divider * dividers.post_div * 16834;
   2030  1.1  riastrad 	do_div(tmp, reference_clock);
   2031  1.1  riastrad 	fbdiv = (u32) tmp;
   2032  1.1  riastrad 
   2033  1.1  riastrad 	spll_func_cntl &= ~(SPLL_PDIV_A_MASK | SPLL_REF_DIV_MASK);
   2034  1.1  riastrad 	spll_func_cntl |= SPLL_REF_DIV(dividers.ref_div);
   2035  1.1  riastrad 	spll_func_cntl |= SPLL_PDIV_A(dividers.post_div);
   2036  1.1  riastrad 
   2037  1.1  riastrad 	spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
   2038  1.1  riastrad 	spll_func_cntl_2 |= SCLK_MUX_SEL(2);
   2039  1.1  riastrad 
   2040  1.1  riastrad 	spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
   2041  1.1  riastrad 	spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
   2042  1.1  riastrad 	spll_func_cntl_3 |= SPLL_DITHEN;
   2043  1.1  riastrad 
   2044  1.1  riastrad 	if (pi->sclk_ss) {
   2045  1.1  riastrad 		struct radeon_atom_ss ss;
   2046  1.1  riastrad 		u32 vco_freq = engine_clock * dividers.post_div;
   2047  1.1  riastrad 
   2048  1.1  riastrad 		if (radeon_atombios_get_asic_ss_info(rdev, &ss,
   2049  1.1  riastrad 						     ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
   2050  1.1  riastrad 			u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
   2051  1.1  riastrad 			u32 clk_v = 4 * ss.percentage * fbdiv / (clk_s * 10000);
   2052  1.1  riastrad 
   2053  1.1  riastrad 			cg_spll_spread_spectrum &= ~CLK_S_MASK;
   2054  1.1  riastrad 			cg_spll_spread_spectrum |= CLK_S(clk_s);
   2055  1.1  riastrad 			cg_spll_spread_spectrum |= SSEN;
   2056  1.1  riastrad 
   2057  1.1  riastrad 			cg_spll_spread_spectrum_2 &= ~CLK_V_MASK;
   2058  1.1  riastrad 			cg_spll_spread_spectrum_2 |= CLK_V(clk_v);
   2059  1.1  riastrad 		}
   2060  1.1  riastrad 	}
   2061  1.1  riastrad 
   2062  1.1  riastrad 	sclk->sclk_value = engine_clock;
   2063  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL = spll_func_cntl;
   2064  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL_2 = spll_func_cntl_2;
   2065  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL_3 = spll_func_cntl_3;
   2066  1.1  riastrad 	sclk->vCG_SPLL_FUNC_CNTL_4 = spll_func_cntl_4;
   2067  1.1  riastrad 	sclk->vCG_SPLL_SPREAD_SPECTRUM = cg_spll_spread_spectrum;
   2068  1.1  riastrad 	sclk->vCG_SPLL_SPREAD_SPECTRUM_2 = cg_spll_spread_spectrum_2;
   2069  1.1  riastrad 
   2070  1.1  riastrad 	return 0;
   2071  1.1  riastrad }
   2072  1.1  riastrad 
   2073  1.1  riastrad static int ni_populate_sclk_value(struct radeon_device *rdev,
   2074  1.1  riastrad 				  u32 engine_clock,
   2075  1.1  riastrad 				  NISLANDS_SMC_SCLK_VALUE *sclk)
   2076  1.1  riastrad {
   2077  1.1  riastrad 	NISLANDS_SMC_SCLK_VALUE sclk_tmp;
   2078  1.1  riastrad 	int ret;
   2079  1.1  riastrad 
   2080  1.1  riastrad 	ret = ni_calculate_sclk_params(rdev, engine_clock, &sclk_tmp);
   2081  1.1  riastrad 	if (!ret) {
   2082  1.1  riastrad 		sclk->sclk_value = cpu_to_be32(sclk_tmp.sclk_value);
   2083  1.1  riastrad 		sclk->vCG_SPLL_FUNC_CNTL = cpu_to_be32(sclk_tmp.vCG_SPLL_FUNC_CNTL);
   2084  1.1  riastrad 		sclk->vCG_SPLL_FUNC_CNTL_2 = cpu_to_be32(sclk_tmp.vCG_SPLL_FUNC_CNTL_2);
   2085  1.1  riastrad 		sclk->vCG_SPLL_FUNC_CNTL_3 = cpu_to_be32(sclk_tmp.vCG_SPLL_FUNC_CNTL_3);
   2086  1.1  riastrad 		sclk->vCG_SPLL_FUNC_CNTL_4 = cpu_to_be32(sclk_tmp.vCG_SPLL_FUNC_CNTL_4);
   2087  1.1  riastrad 		sclk->vCG_SPLL_SPREAD_SPECTRUM = cpu_to_be32(sclk_tmp.vCG_SPLL_SPREAD_SPECTRUM);
   2088  1.1  riastrad 		sclk->vCG_SPLL_SPREAD_SPECTRUM_2 = cpu_to_be32(sclk_tmp.vCG_SPLL_SPREAD_SPECTRUM_2);
   2089  1.1  riastrad 	}
   2090  1.1  riastrad 
   2091  1.1  riastrad 	return ret;
   2092  1.1  riastrad }
   2093  1.1  riastrad 
   2094  1.1  riastrad static int ni_init_smc_spll_table(struct radeon_device *rdev)
   2095  1.1  riastrad {
   2096  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2097  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2098  1.1  riastrad 	SMC_NISLANDS_SPLL_DIV_TABLE *spll_table;
   2099  1.1  riastrad 	NISLANDS_SMC_SCLK_VALUE sclk_params;
   2100  1.1  riastrad 	u32 fb_div;
   2101  1.1  riastrad 	u32 p_div;
   2102  1.1  riastrad 	u32 clk_s;
   2103  1.1  riastrad 	u32 clk_v;
   2104  1.1  riastrad 	u32 sclk = 0;
   2105  1.1  riastrad 	int i, ret;
   2106  1.1  riastrad 	u32 tmp;
   2107  1.1  riastrad 
   2108  1.1  riastrad 	if (ni_pi->spll_table_start == 0)
   2109  1.1  riastrad 		return -EINVAL;
   2110  1.1  riastrad 
   2111  1.1  riastrad 	spll_table = kzalloc(sizeof(SMC_NISLANDS_SPLL_DIV_TABLE), GFP_KERNEL);
   2112  1.1  riastrad 	if (spll_table == NULL)
   2113  1.1  riastrad 		return -ENOMEM;
   2114  1.1  riastrad 
   2115  1.1  riastrad 	for (i = 0; i < 256; i++) {
   2116  1.1  riastrad 		ret = ni_calculate_sclk_params(rdev, sclk, &sclk_params);
   2117  1.1  riastrad 		if (ret)
   2118  1.1  riastrad 			break;
   2119  1.1  riastrad 
   2120  1.1  riastrad 		p_div = (sclk_params.vCG_SPLL_FUNC_CNTL & SPLL_PDIV_A_MASK) >> SPLL_PDIV_A_SHIFT;
   2121  1.1  riastrad 		fb_div = (sclk_params.vCG_SPLL_FUNC_CNTL_3 & SPLL_FB_DIV_MASK) >> SPLL_FB_DIV_SHIFT;
   2122  1.1  riastrad 		clk_s = (sclk_params.vCG_SPLL_SPREAD_SPECTRUM & CLK_S_MASK) >> CLK_S_SHIFT;
   2123  1.1  riastrad 		clk_v = (sclk_params.vCG_SPLL_SPREAD_SPECTRUM_2 & CLK_V_MASK) >> CLK_V_SHIFT;
   2124  1.1  riastrad 
   2125  1.1  riastrad 		fb_div &= ~0x00001FFF;
   2126  1.1  riastrad 		fb_div >>= 1;
   2127  1.1  riastrad 		clk_v >>= 6;
   2128  1.1  riastrad 
   2129  1.1  riastrad 		if (p_div & ~(SMC_NISLANDS_SPLL_DIV_TABLE_PDIV_MASK >> SMC_NISLANDS_SPLL_DIV_TABLE_PDIV_SHIFT))
   2130  1.1  riastrad 			ret = -EINVAL;
   2131  1.1  riastrad 
   2132  1.1  riastrad 		if (clk_s & ~(SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_MASK >> SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_SHIFT))
   2133  1.1  riastrad 			ret = -EINVAL;
   2134  1.1  riastrad 
   2135  1.1  riastrad 		if (clk_s & ~(SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_MASK >> SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_SHIFT))
   2136  1.1  riastrad 			ret = -EINVAL;
   2137  1.1  riastrad 
   2138  1.1  riastrad 		if (clk_v & ~(SMC_NISLANDS_SPLL_DIV_TABLE_CLKV_MASK >> SMC_NISLANDS_SPLL_DIV_TABLE_CLKV_SHIFT))
   2139  1.1  riastrad 			ret = -EINVAL;
   2140  1.1  riastrad 
   2141  1.1  riastrad 		if (ret)
   2142  1.1  riastrad 			break;
   2143  1.1  riastrad 
   2144  1.1  riastrad 		tmp = ((fb_div << SMC_NISLANDS_SPLL_DIV_TABLE_FBDIV_SHIFT) & SMC_NISLANDS_SPLL_DIV_TABLE_FBDIV_MASK) |
   2145  1.1  riastrad 			((p_div << SMC_NISLANDS_SPLL_DIV_TABLE_PDIV_SHIFT) & SMC_NISLANDS_SPLL_DIV_TABLE_PDIV_MASK);
   2146  1.1  riastrad 		spll_table->freq[i] = cpu_to_be32(tmp);
   2147  1.1  riastrad 
   2148  1.1  riastrad 		tmp = ((clk_v << SMC_NISLANDS_SPLL_DIV_TABLE_CLKV_SHIFT) & SMC_NISLANDS_SPLL_DIV_TABLE_CLKV_MASK) |
   2149  1.1  riastrad 			((clk_s << SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_SHIFT) & SMC_NISLANDS_SPLL_DIV_TABLE_CLKS_MASK);
   2150  1.1  riastrad 		spll_table->ss[i] = cpu_to_be32(tmp);
   2151  1.1  riastrad 
   2152  1.1  riastrad 		sclk += 512;
   2153  1.1  riastrad 	}
   2154  1.1  riastrad 
   2155  1.1  riastrad 	if (!ret)
   2156  1.1  riastrad 		ret = rv770_copy_bytes_to_smc(rdev, ni_pi->spll_table_start, (u8 *)spll_table,
   2157  1.1  riastrad 					      sizeof(SMC_NISLANDS_SPLL_DIV_TABLE), pi->sram_end);
   2158  1.1  riastrad 
   2159  1.1  riastrad 	kfree(spll_table);
   2160  1.1  riastrad 
   2161  1.1  riastrad 	return ret;
   2162  1.1  riastrad }
   2163  1.1  riastrad 
   2164  1.1  riastrad static int ni_populate_mclk_value(struct radeon_device *rdev,
   2165  1.1  riastrad 				  u32 engine_clock,
   2166  1.1  riastrad 				  u32 memory_clock,
   2167  1.1  riastrad 				  NISLANDS_SMC_MCLK_VALUE *mclk,
   2168  1.1  riastrad 				  bool strobe_mode,
   2169  1.1  riastrad 				  bool dll_state_on)
   2170  1.1  riastrad {
   2171  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2172  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2173  1.1  riastrad 	u32 mpll_ad_func_cntl = ni_pi->clock_registers.mpll_ad_func_cntl;
   2174  1.1  riastrad 	u32 mpll_ad_func_cntl_2 = ni_pi->clock_registers.mpll_ad_func_cntl_2;
   2175  1.1  riastrad 	u32 mpll_dq_func_cntl = ni_pi->clock_registers.mpll_dq_func_cntl;
   2176  1.1  riastrad 	u32 mpll_dq_func_cntl_2 = ni_pi->clock_registers.mpll_dq_func_cntl_2;
   2177  1.1  riastrad 	u32 mclk_pwrmgt_cntl = ni_pi->clock_registers.mclk_pwrmgt_cntl;
   2178  1.1  riastrad 	u32 dll_cntl = ni_pi->clock_registers.dll_cntl;
   2179  1.1  riastrad 	u32 mpll_ss1 = ni_pi->clock_registers.mpll_ss1;
   2180  1.1  riastrad 	u32 mpll_ss2 = ni_pi->clock_registers.mpll_ss2;
   2181  1.1  riastrad 	struct atom_clock_dividers dividers;
   2182  1.1  riastrad 	u32 ibias;
   2183  1.1  riastrad 	u32 dll_speed;
   2184  1.1  riastrad 	int ret;
   2185  1.1  riastrad 	u32 mc_seq_misc7;
   2186  1.1  riastrad 
   2187  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_MEMORY_PLL_PARAM,
   2188  1.1  riastrad 					     memory_clock, strobe_mode, &dividers);
   2189  1.1  riastrad 	if (ret)
   2190  1.1  riastrad 		return ret;
   2191  1.1  riastrad 
   2192  1.1  riastrad 	if (!strobe_mode) {
   2193  1.1  riastrad 		mc_seq_misc7 = RREG32(MC_SEQ_MISC7);
   2194  1.1  riastrad 
   2195  1.1  riastrad 		if (mc_seq_misc7 & 0x8000000)
   2196  1.1  riastrad 			dividers.post_div = 1;
   2197  1.1  riastrad 	}
   2198  1.1  riastrad 
   2199  1.1  riastrad 	ibias = cypress_map_clkf_to_ibias(rdev, dividers.whole_fb_div);
   2200  1.1  riastrad 
   2201  1.1  riastrad 	mpll_ad_func_cntl &= ~(CLKR_MASK |
   2202  1.1  riastrad 			       YCLK_POST_DIV_MASK |
   2203  1.1  riastrad 			       CLKF_MASK |
   2204  1.1  riastrad 			       CLKFRAC_MASK |
   2205  1.1  riastrad 			       IBIAS_MASK);
   2206  1.1  riastrad 	mpll_ad_func_cntl |= CLKR(dividers.ref_div);
   2207  1.1  riastrad 	mpll_ad_func_cntl |= YCLK_POST_DIV(dividers.post_div);
   2208  1.1  riastrad 	mpll_ad_func_cntl |= CLKF(dividers.whole_fb_div);
   2209  1.1  riastrad 	mpll_ad_func_cntl |= CLKFRAC(dividers.frac_fb_div);
   2210  1.1  riastrad 	mpll_ad_func_cntl |= IBIAS(ibias);
   2211  1.1  riastrad 
   2212  1.1  riastrad 	if (dividers.vco_mode)
   2213  1.1  riastrad 		mpll_ad_func_cntl_2 |= VCO_MODE;
   2214  1.1  riastrad 	else
   2215  1.1  riastrad 		mpll_ad_func_cntl_2 &= ~VCO_MODE;
   2216  1.1  riastrad 
   2217  1.1  riastrad 	if (pi->mem_gddr5) {
   2218  1.1  riastrad 		mpll_dq_func_cntl &= ~(CLKR_MASK |
   2219  1.1  riastrad 				       YCLK_POST_DIV_MASK |
   2220  1.1  riastrad 				       CLKF_MASK |
   2221  1.1  riastrad 				       CLKFRAC_MASK |
   2222  1.1  riastrad 				       IBIAS_MASK);
   2223  1.1  riastrad 		mpll_dq_func_cntl |= CLKR(dividers.ref_div);
   2224  1.1  riastrad 		mpll_dq_func_cntl |= YCLK_POST_DIV(dividers.post_div);
   2225  1.1  riastrad 		mpll_dq_func_cntl |= CLKF(dividers.whole_fb_div);
   2226  1.1  riastrad 		mpll_dq_func_cntl |= CLKFRAC(dividers.frac_fb_div);
   2227  1.1  riastrad 		mpll_dq_func_cntl |= IBIAS(ibias);
   2228  1.1  riastrad 
   2229  1.1  riastrad 		if (strobe_mode)
   2230  1.1  riastrad 			mpll_dq_func_cntl &= ~PDNB;
   2231  1.1  riastrad 		else
   2232  1.1  riastrad 			mpll_dq_func_cntl |= PDNB;
   2233  1.1  riastrad 
   2234  1.1  riastrad 		if (dividers.vco_mode)
   2235  1.1  riastrad 			mpll_dq_func_cntl_2 |= VCO_MODE;
   2236  1.1  riastrad 		else
   2237  1.1  riastrad 			mpll_dq_func_cntl_2 &= ~VCO_MODE;
   2238  1.1  riastrad 	}
   2239  1.1  riastrad 
   2240  1.1  riastrad 	if (pi->mclk_ss) {
   2241  1.1  riastrad 		struct radeon_atom_ss ss;
   2242  1.1  riastrad 		u32 vco_freq = memory_clock * dividers.post_div;
   2243  1.1  riastrad 
   2244  1.1  riastrad 		if (radeon_atombios_get_asic_ss_info(rdev, &ss,
   2245  1.1  riastrad 						     ASIC_INTERNAL_MEMORY_SS, vco_freq)) {
   2246  1.1  riastrad 			u32 reference_clock = rdev->clock.mpll.reference_freq;
   2247  1.1  riastrad 			u32 decoded_ref = rv740_get_decoded_reference_divider(dividers.ref_div);
   2248  1.1  riastrad 			u32 clk_s = reference_clock * 5 / (decoded_ref * ss.rate);
   2249  1.1  riastrad 			u32 clk_v = ss.percentage *
   2250  1.1  riastrad 				(0x4000 * dividers.whole_fb_div + 0x800 * dividers.frac_fb_div) / (clk_s * 625);
   2251  1.1  riastrad 
   2252  1.1  riastrad 			mpll_ss1 &= ~CLKV_MASK;
   2253  1.1  riastrad 			mpll_ss1 |= CLKV(clk_v);
   2254  1.1  riastrad 
   2255  1.1  riastrad 			mpll_ss2 &= ~CLKS_MASK;
   2256  1.1  riastrad 			mpll_ss2 |= CLKS(clk_s);
   2257  1.1  riastrad 		}
   2258  1.1  riastrad 	}
   2259  1.1  riastrad 
   2260  1.1  riastrad 	dll_speed = rv740_get_dll_speed(pi->mem_gddr5,
   2261  1.1  riastrad 					memory_clock);
   2262  1.1  riastrad 
   2263  1.1  riastrad 	mclk_pwrmgt_cntl &= ~DLL_SPEED_MASK;
   2264  1.1  riastrad 	mclk_pwrmgt_cntl |= DLL_SPEED(dll_speed);
   2265  1.1  riastrad 	if (dll_state_on)
   2266  1.1  riastrad 		mclk_pwrmgt_cntl |= (MRDCKA0_PDNB |
   2267  1.1  riastrad 				     MRDCKA1_PDNB |
   2268  1.1  riastrad 				     MRDCKB0_PDNB |
   2269  1.1  riastrad 				     MRDCKB1_PDNB |
   2270  1.1  riastrad 				     MRDCKC0_PDNB |
   2271  1.1  riastrad 				     MRDCKC1_PDNB |
   2272  1.1  riastrad 				     MRDCKD0_PDNB |
   2273  1.1  riastrad 				     MRDCKD1_PDNB);
   2274  1.1  riastrad 	else
   2275  1.1  riastrad 		mclk_pwrmgt_cntl &= ~(MRDCKA0_PDNB |
   2276  1.1  riastrad 				      MRDCKA1_PDNB |
   2277  1.1  riastrad 				      MRDCKB0_PDNB |
   2278  1.1  riastrad 				      MRDCKB1_PDNB |
   2279  1.1  riastrad 				      MRDCKC0_PDNB |
   2280  1.1  riastrad 				      MRDCKC1_PDNB |
   2281  1.1  riastrad 				      MRDCKD0_PDNB |
   2282  1.1  riastrad 				      MRDCKD1_PDNB);
   2283  1.1  riastrad 
   2284  1.1  riastrad 
   2285  1.1  riastrad 	mclk->mclk_value = cpu_to_be32(memory_clock);
   2286  1.1  riastrad 	mclk->vMPLL_AD_FUNC_CNTL = cpu_to_be32(mpll_ad_func_cntl);
   2287  1.1  riastrad 	mclk->vMPLL_AD_FUNC_CNTL_2 = cpu_to_be32(mpll_ad_func_cntl_2);
   2288  1.1  riastrad 	mclk->vMPLL_DQ_FUNC_CNTL = cpu_to_be32(mpll_dq_func_cntl);
   2289  1.1  riastrad 	mclk->vMPLL_DQ_FUNC_CNTL_2 = cpu_to_be32(mpll_dq_func_cntl_2);
   2290  1.1  riastrad 	mclk->vMCLK_PWRMGT_CNTL = cpu_to_be32(mclk_pwrmgt_cntl);
   2291  1.1  riastrad 	mclk->vDLL_CNTL = cpu_to_be32(dll_cntl);
   2292  1.1  riastrad 	mclk->vMPLL_SS = cpu_to_be32(mpll_ss1);
   2293  1.1  riastrad 	mclk->vMPLL_SS2 = cpu_to_be32(mpll_ss2);
   2294  1.1  riastrad 
   2295  1.1  riastrad 	return 0;
   2296  1.1  riastrad }
   2297  1.1  riastrad 
   2298  1.1  riastrad static void ni_populate_smc_sp(struct radeon_device *rdev,
   2299  1.1  riastrad 			       struct radeon_ps *radeon_state,
   2300  1.1  riastrad 			       NISLANDS_SMC_SWSTATE *smc_state)
   2301  1.1  riastrad {
   2302  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(radeon_state);
   2303  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2304  1.1  riastrad 	int i;
   2305  1.1  riastrad 
   2306  1.1  riastrad 	for (i = 0; i < ps->performance_level_count - 1; i++)
   2307  1.1  riastrad 		smc_state->levels[i].bSP = cpu_to_be32(pi->dsp);
   2308  1.1  riastrad 
   2309  1.1  riastrad 	smc_state->levels[ps->performance_level_count - 1].bSP =
   2310  1.1  riastrad 		cpu_to_be32(pi->psp);
   2311  1.1  riastrad }
   2312  1.1  riastrad 
   2313  1.1  riastrad static int ni_convert_power_level_to_smc(struct radeon_device *rdev,
   2314  1.1  riastrad 					 struct rv7xx_pl *pl,
   2315  1.1  riastrad 					 NISLANDS_SMC_HW_PERFORMANCE_LEVEL *level)
   2316  1.1  riastrad {
   2317  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2318  1.2  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   2319  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2320  1.1  riastrad 	int ret;
   2321  1.1  riastrad 	bool dll_state_on;
   2322  1.1  riastrad 	u16 std_vddc;
   2323  1.1  riastrad 	u32 tmp = RREG32(DC_STUTTER_CNTL);
   2324  1.1  riastrad 
   2325  1.1  riastrad 	level->gen2PCIE = pi->pcie_gen2 ?
   2326  1.1  riastrad 		((pl->flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2) ? 1 : 0) : 0;
   2327  1.1  riastrad 
   2328  1.1  riastrad 	ret = ni_populate_sclk_value(rdev, pl->sclk, &level->sclk);
   2329  1.1  riastrad 	if (ret)
   2330  1.1  riastrad 		return ret;
   2331  1.1  riastrad 
   2332  1.1  riastrad 	level->mcFlags =  0;
   2333  1.1  riastrad 	if (pi->mclk_stutter_mode_threshold &&
   2334  1.1  riastrad 	    (pl->mclk <= pi->mclk_stutter_mode_threshold) &&
   2335  1.1  riastrad 	    !eg_pi->uvd_enabled &&
   2336  1.1  riastrad 	    (tmp & DC_STUTTER_ENABLE_A) &&
   2337  1.1  riastrad 	    (tmp & DC_STUTTER_ENABLE_B))
   2338  1.1  riastrad 		level->mcFlags |= NISLANDS_SMC_MC_STUTTER_EN;
   2339  1.1  riastrad 
   2340  1.1  riastrad 	if (pi->mem_gddr5) {
   2341  1.1  riastrad 		if (pl->mclk > pi->mclk_edc_enable_threshold)
   2342  1.1  riastrad 			level->mcFlags |= NISLANDS_SMC_MC_EDC_RD_FLAG;
   2343  1.1  riastrad 		if (pl->mclk > eg_pi->mclk_edc_wr_enable_threshold)
   2344  1.1  riastrad 			level->mcFlags |= NISLANDS_SMC_MC_EDC_WR_FLAG;
   2345  1.1  riastrad 
   2346  1.1  riastrad 		level->strobeMode = cypress_get_strobe_mode_settings(rdev, pl->mclk);
   2347  1.1  riastrad 
   2348  1.1  riastrad 		if (level->strobeMode & NISLANDS_SMC_STROBE_ENABLE) {
   2349  1.1  riastrad 			if (cypress_get_mclk_frequency_ratio(rdev, pl->mclk, true) >=
   2350  1.1  riastrad 			    ((RREG32(MC_SEQ_MISC7) >> 16) & 0xf))
   2351  1.1  riastrad 				dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
   2352  1.1  riastrad 			else
   2353  1.1  riastrad 				dll_state_on = ((RREG32(MC_SEQ_MISC6) >> 1) & 0x1) ? true : false;
   2354  1.1  riastrad 		} else {
   2355  1.1  riastrad 			dll_state_on = false;
   2356  1.1  riastrad 			if (pl->mclk > ni_pi->mclk_rtt_mode_threshold)
   2357  1.1  riastrad 				level->mcFlags |= NISLANDS_SMC_MC_RTT_ENABLE;
   2358  1.1  riastrad 		}
   2359  1.1  riastrad 
   2360  1.1  riastrad 		ret = ni_populate_mclk_value(rdev, pl->sclk, pl->mclk,
   2361  1.1  riastrad 					     &level->mclk,
   2362  1.1  riastrad 					     (level->strobeMode & NISLANDS_SMC_STROBE_ENABLE) != 0,
   2363  1.1  riastrad 					     dll_state_on);
   2364  1.1  riastrad 	} else
   2365  1.1  riastrad 		ret = ni_populate_mclk_value(rdev, pl->sclk, pl->mclk, &level->mclk, 1, 1);
   2366  1.1  riastrad 
   2367  1.1  riastrad 	if (ret)
   2368  1.1  riastrad 		return ret;
   2369  1.1  riastrad 
   2370  1.1  riastrad 	ret = ni_populate_voltage_value(rdev, &eg_pi->vddc_voltage_table,
   2371  1.1  riastrad 					pl->vddc, &level->vddc);
   2372  1.1  riastrad 	if (ret)
   2373  1.1  riastrad 		return ret;
   2374  1.1  riastrad 
   2375  1.1  riastrad 	ret = ni_get_std_voltage_value(rdev, &level->vddc, &std_vddc);
   2376  1.1  riastrad 	if (ret)
   2377  1.1  riastrad 		return ret;
   2378  1.1  riastrad 
   2379  1.1  riastrad 	ni_populate_std_voltage_value(rdev, std_vddc,
   2380  1.1  riastrad 				      level->vddc.index, &level->std_vddc);
   2381  1.1  riastrad 
   2382  1.1  riastrad 	if (eg_pi->vddci_control) {
   2383  1.1  riastrad 		ret = ni_populate_voltage_value(rdev, &eg_pi->vddci_voltage_table,
   2384  1.1  riastrad 						pl->vddci, &level->vddci);
   2385  1.1  riastrad 		if (ret)
   2386  1.1  riastrad 			return ret;
   2387  1.1  riastrad 	}
   2388  1.1  riastrad 
   2389  1.1  riastrad 	ni_populate_mvdd_value(rdev, pl->mclk, &level->mvdd);
   2390  1.1  riastrad 
   2391  1.1  riastrad 	return ret;
   2392  1.1  riastrad }
   2393  1.1  riastrad 
   2394  1.1  riastrad static int ni_populate_smc_t(struct radeon_device *rdev,
   2395  1.1  riastrad 			     struct radeon_ps *radeon_state,
   2396  1.1  riastrad 			     NISLANDS_SMC_SWSTATE *smc_state)
   2397  1.1  riastrad {
   2398  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2399  1.2  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   2400  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   2401  1.1  riastrad 	u32 a_t;
   2402  1.1  riastrad 	u32 t_l, t_h;
   2403  1.1  riastrad 	u32 high_bsp;
   2404  1.1  riastrad 	int i, ret;
   2405  1.1  riastrad 
   2406  1.1  riastrad 	if (state->performance_level_count >= 9)
   2407  1.1  riastrad 		return -EINVAL;
   2408  1.1  riastrad 
   2409  1.1  riastrad 	if (state->performance_level_count < 2) {
   2410  1.1  riastrad 		a_t = CG_R(0xffff) | CG_L(0);
   2411  1.1  riastrad 		smc_state->levels[0].aT = cpu_to_be32(a_t);
   2412  1.1  riastrad 		return 0;
   2413  1.1  riastrad 	}
   2414  1.1  riastrad 
   2415  1.1  riastrad 	smc_state->levels[0].aT = cpu_to_be32(0);
   2416  1.1  riastrad 
   2417  1.1  riastrad 	for (i = 0; i <= state->performance_level_count - 2; i++) {
   2418  1.1  riastrad 		if (eg_pi->uvd_enabled)
   2419  1.1  riastrad 			ret = r600_calculate_at(
   2420  1.1  riastrad 				1000 * (i * (eg_pi->smu_uvd_hs ? 2 : 8) + 2),
   2421  1.1  riastrad 				100 * R600_AH_DFLT,
   2422  1.1  riastrad 				state->performance_levels[i + 1].sclk,
   2423  1.1  riastrad 				state->performance_levels[i].sclk,
   2424  1.1  riastrad 				&t_l,
   2425  1.1  riastrad 				&t_h);
   2426  1.1  riastrad 		else
   2427  1.1  riastrad 			ret = r600_calculate_at(
   2428  1.1  riastrad 				1000 * (i + 1),
   2429  1.1  riastrad 				100 * R600_AH_DFLT,
   2430  1.1  riastrad 				state->performance_levels[i + 1].sclk,
   2431  1.1  riastrad 				state->performance_levels[i].sclk,
   2432  1.1  riastrad 				&t_l,
   2433  1.1  riastrad 				&t_h);
   2434  1.1  riastrad 
   2435  1.1  riastrad 		if (ret) {
   2436  1.1  riastrad 			t_h = (i + 1) * 1000 - 50 * R600_AH_DFLT;
   2437  1.1  riastrad 			t_l = (i + 1) * 1000 + 50 * R600_AH_DFLT;
   2438  1.1  riastrad 		}
   2439  1.1  riastrad 
   2440  1.1  riastrad 		a_t = be32_to_cpu(smc_state->levels[i].aT) & ~CG_R_MASK;
   2441  1.1  riastrad 		a_t |= CG_R(t_l * pi->bsp / 20000);
   2442  1.1  riastrad 		smc_state->levels[i].aT = cpu_to_be32(a_t);
   2443  1.1  riastrad 
   2444  1.1  riastrad 		high_bsp = (i == state->performance_level_count - 2) ?
   2445  1.1  riastrad 			pi->pbsp : pi->bsp;
   2446  1.1  riastrad 
   2447  1.1  riastrad 		a_t = CG_R(0xffff) | CG_L(t_h * high_bsp / 20000);
   2448  1.1  riastrad 		smc_state->levels[i + 1].aT = cpu_to_be32(a_t);
   2449  1.1  riastrad 	}
   2450  1.1  riastrad 
   2451  1.1  riastrad 	return 0;
   2452  1.1  riastrad }
   2453  1.1  riastrad 
   2454  1.1  riastrad static int ni_populate_power_containment_values(struct radeon_device *rdev,
   2455  1.1  riastrad 						struct radeon_ps *radeon_state,
   2456  1.1  riastrad 						NISLANDS_SMC_SWSTATE *smc_state)
   2457  1.1  riastrad {
   2458  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2459  1.2  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   2460  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2461  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   2462  1.1  riastrad 	u32 prev_sclk;
   2463  1.1  riastrad 	u32 max_sclk;
   2464  1.1  riastrad 	u32 min_sclk;
   2465  1.1  riastrad 	int i, ret;
   2466  1.1  riastrad 	u32 tdp_limit;
   2467  1.1  riastrad 	u32 near_tdp_limit;
   2468  1.1  riastrad 	u32 power_boost_limit;
   2469  1.1  riastrad 	u8 max_ps_percent;
   2470  1.1  riastrad 
   2471  1.1  riastrad 	if (ni_pi->enable_power_containment == false)
   2472  1.1  riastrad 		return 0;
   2473  1.1  riastrad 
   2474  1.1  riastrad 	if (state->performance_level_count == 0)
   2475  1.1  riastrad 		return -EINVAL;
   2476  1.1  riastrad 
   2477  1.1  riastrad 	if (smc_state->levelCount != state->performance_level_count)
   2478  1.1  riastrad 		return -EINVAL;
   2479  1.1  riastrad 
   2480  1.1  riastrad 	ret = ni_calculate_adjusted_tdp_limits(rdev,
   2481  1.1  riastrad 					       false, /* ??? */
   2482  1.1  riastrad 					       rdev->pm.dpm.tdp_adjustment,
   2483  1.1  riastrad 					       &tdp_limit,
   2484  1.1  riastrad 					       &near_tdp_limit);
   2485  1.1  riastrad 	if (ret)
   2486  1.1  riastrad 		return ret;
   2487  1.1  riastrad 
   2488  1.1  riastrad 	power_boost_limit = ni_calculate_power_boost_limit(rdev, radeon_state, near_tdp_limit);
   2489  1.1  riastrad 
   2490  1.1  riastrad 	ret = rv770_write_smc_sram_dword(rdev,
   2491  1.1  riastrad 					 pi->state_table_start +
   2492  1.1  riastrad 					 offsetof(NISLANDS_SMC_STATETABLE, dpm2Params) +
   2493  1.1  riastrad 					 offsetof(PP_NIslands_DPM2Parameters, PowerBoostLimit),
   2494  1.1  riastrad 					 ni_scale_power_for_smc(power_boost_limit, ni_get_smc_power_scaling_factor(rdev)),
   2495  1.1  riastrad 					 pi->sram_end);
   2496  1.1  riastrad 	if (ret)
   2497  1.1  riastrad 		power_boost_limit = 0;
   2498  1.1  riastrad 
   2499  1.1  riastrad 	smc_state->levels[0].dpm2.MaxPS = 0;
   2500  1.1  riastrad 	smc_state->levels[0].dpm2.NearTDPDec = 0;
   2501  1.1  riastrad 	smc_state->levels[0].dpm2.AboveSafeInc = 0;
   2502  1.1  riastrad 	smc_state->levels[0].dpm2.BelowSafeInc = 0;
   2503  1.1  riastrad 	smc_state->levels[0].stateFlags |= power_boost_limit ? PPSMC_STATEFLAG_POWERBOOST : 0;
   2504  1.1  riastrad 
   2505  1.1  riastrad 	for (i = 1; i < state->performance_level_count; i++) {
   2506  1.1  riastrad 		prev_sclk = state->performance_levels[i-1].sclk;
   2507  1.1  riastrad 		max_sclk  = state->performance_levels[i].sclk;
   2508  1.1  riastrad 		max_ps_percent = (i != (state->performance_level_count - 1)) ?
   2509  1.1  riastrad 			NISLANDS_DPM2_MAXPS_PERCENT_M : NISLANDS_DPM2_MAXPS_PERCENT_H;
   2510  1.1  riastrad 
   2511  1.1  riastrad 		if (max_sclk < prev_sclk)
   2512  1.1  riastrad 			return -EINVAL;
   2513  1.1  riastrad 
   2514  1.1  riastrad 		if ((max_ps_percent == 0) || (prev_sclk == max_sclk) || eg_pi->uvd_enabled)
   2515  1.1  riastrad 			min_sclk = max_sclk;
   2516  1.1  riastrad 		else if (1 == i)
   2517  1.1  riastrad 			min_sclk = prev_sclk;
   2518  1.1  riastrad 		else
   2519  1.1  riastrad 			min_sclk = (prev_sclk * (u32)max_ps_percent) / 100;
   2520  1.1  riastrad 
   2521  1.1  riastrad 		if (min_sclk < state->performance_levels[0].sclk)
   2522  1.1  riastrad 			min_sclk = state->performance_levels[0].sclk;
   2523  1.1  riastrad 
   2524  1.1  riastrad 		if (min_sclk == 0)
   2525  1.1  riastrad 			return -EINVAL;
   2526  1.1  riastrad 
   2527  1.1  riastrad 		smc_state->levels[i].dpm2.MaxPS =
   2528  1.1  riastrad 			(u8)((NISLANDS_DPM2_MAX_PULSE_SKIP * (max_sclk - min_sclk)) / max_sclk);
   2529  1.1  riastrad 		smc_state->levels[i].dpm2.NearTDPDec = NISLANDS_DPM2_NEAR_TDP_DEC;
   2530  1.1  riastrad 		smc_state->levels[i].dpm2.AboveSafeInc = NISLANDS_DPM2_ABOVE_SAFE_INC;
   2531  1.1  riastrad 		smc_state->levels[i].dpm2.BelowSafeInc = NISLANDS_DPM2_BELOW_SAFE_INC;
   2532  1.1  riastrad 		smc_state->levels[i].stateFlags |=
   2533  1.1  riastrad 			((i != (state->performance_level_count - 1)) && power_boost_limit) ?
   2534  1.1  riastrad 			PPSMC_STATEFLAG_POWERBOOST : 0;
   2535  1.1  riastrad 	}
   2536  1.1  riastrad 
   2537  1.1  riastrad 	return 0;
   2538  1.1  riastrad }
   2539  1.1  riastrad 
   2540  1.1  riastrad static int ni_populate_sq_ramping_values(struct radeon_device *rdev,
   2541  1.1  riastrad 					 struct radeon_ps *radeon_state,
   2542  1.1  riastrad 					 NISLANDS_SMC_SWSTATE *smc_state)
   2543  1.1  riastrad {
   2544  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2545  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   2546  1.1  riastrad 	u32 sq_power_throttle;
   2547  1.1  riastrad 	u32 sq_power_throttle2;
   2548  1.1  riastrad 	bool enable_sq_ramping = ni_pi->enable_sq_ramping;
   2549  1.1  riastrad 	int i;
   2550  1.1  riastrad 
   2551  1.1  riastrad 	if (state->performance_level_count == 0)
   2552  1.1  riastrad 		return -EINVAL;
   2553  1.1  riastrad 
   2554  1.1  riastrad 	if (smc_state->levelCount != state->performance_level_count)
   2555  1.1  riastrad 		return -EINVAL;
   2556  1.1  riastrad 
   2557  1.1  riastrad 	if (rdev->pm.dpm.sq_ramping_threshold == 0)
   2558  1.1  riastrad 		return -EINVAL;
   2559  1.1  riastrad 
   2560  1.1  riastrad 	if (NISLANDS_DPM2_SQ_RAMP_MAX_POWER > (MAX_POWER_MASK >> MAX_POWER_SHIFT))
   2561  1.1  riastrad 		enable_sq_ramping = false;
   2562  1.1  riastrad 
   2563  1.1  riastrad 	if (NISLANDS_DPM2_SQ_RAMP_MIN_POWER > (MIN_POWER_MASK >> MIN_POWER_SHIFT))
   2564  1.1  riastrad 		enable_sq_ramping = false;
   2565  1.1  riastrad 
   2566  1.1  riastrad 	if (NISLANDS_DPM2_SQ_RAMP_MAX_POWER_DELTA > (MAX_POWER_DELTA_MASK >> MAX_POWER_DELTA_SHIFT))
   2567  1.1  riastrad 		enable_sq_ramping = false;
   2568  1.1  riastrad 
   2569  1.1  riastrad 	if (NISLANDS_DPM2_SQ_RAMP_STI_SIZE > (STI_SIZE_MASK >> STI_SIZE_SHIFT))
   2570  1.1  riastrad 		enable_sq_ramping = false;
   2571  1.1  riastrad 
   2572  1.1  riastrad 	if (NISLANDS_DPM2_SQ_RAMP_LTI_RATIO > (LTI_RATIO_MASK >> LTI_RATIO_SHIFT))
   2573  1.1  riastrad 		enable_sq_ramping = false;
   2574  1.1  riastrad 
   2575  1.1  riastrad 	for (i = 0; i < state->performance_level_count; i++) {
   2576  1.1  riastrad 		sq_power_throttle  = 0;
   2577  1.1  riastrad 		sq_power_throttle2 = 0;
   2578  1.1  riastrad 
   2579  1.1  riastrad 		if ((state->performance_levels[i].sclk >= rdev->pm.dpm.sq_ramping_threshold) &&
   2580  1.1  riastrad 		    enable_sq_ramping) {
   2581  1.1  riastrad 			sq_power_throttle |= MAX_POWER(NISLANDS_DPM2_SQ_RAMP_MAX_POWER);
   2582  1.1  riastrad 			sq_power_throttle |= MIN_POWER(NISLANDS_DPM2_SQ_RAMP_MIN_POWER);
   2583  1.1  riastrad 			sq_power_throttle2 |= MAX_POWER_DELTA(NISLANDS_DPM2_SQ_RAMP_MAX_POWER_DELTA);
   2584  1.1  riastrad 			sq_power_throttle2 |= STI_SIZE(NISLANDS_DPM2_SQ_RAMP_STI_SIZE);
   2585  1.1  riastrad 			sq_power_throttle2 |= LTI_RATIO(NISLANDS_DPM2_SQ_RAMP_LTI_RATIO);
   2586  1.1  riastrad 		} else {
   2587  1.1  riastrad 			sq_power_throttle |= MAX_POWER_MASK | MIN_POWER_MASK;
   2588  1.1  riastrad 			sq_power_throttle2 |= MAX_POWER_DELTA_MASK | STI_SIZE_MASK | LTI_RATIO_MASK;
   2589  1.1  riastrad 		}
   2590  1.1  riastrad 
   2591  1.1  riastrad 		smc_state->levels[i].SQPowerThrottle   = cpu_to_be32(sq_power_throttle);
   2592  1.1  riastrad 		smc_state->levels[i].SQPowerThrottle_2 = cpu_to_be32(sq_power_throttle2);
   2593  1.1  riastrad 	}
   2594  1.1  riastrad 
   2595  1.1  riastrad 	return 0;
   2596  1.1  riastrad }
   2597  1.1  riastrad 
   2598  1.1  riastrad static int ni_enable_power_containment(struct radeon_device *rdev,
   2599  1.1  riastrad 				       struct radeon_ps *radeon_new_state,
   2600  1.1  riastrad 				       bool enable)
   2601  1.1  riastrad {
   2602  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2603  1.1  riastrad 	PPSMC_Result smc_result;
   2604  1.1  riastrad 	int ret = 0;
   2605  1.1  riastrad 
   2606  1.1  riastrad 	if (ni_pi->enable_power_containment) {
   2607  1.1  riastrad 		if (enable) {
   2608  1.1  riastrad 			if (!r600_is_uvd_state(radeon_new_state->class, radeon_new_state->class2)) {
   2609  1.1  riastrad 				smc_result = rv770_send_msg_to_smc(rdev, PPSMC_TDPClampingActive);
   2610  1.1  riastrad 				if (smc_result != PPSMC_Result_OK) {
   2611  1.1  riastrad 					ret = -EINVAL;
   2612  1.1  riastrad 					ni_pi->pc_enabled = false;
   2613  1.1  riastrad 				} else {
   2614  1.1  riastrad 					ni_pi->pc_enabled = true;
   2615  1.1  riastrad 				}
   2616  1.1  riastrad 			}
   2617  1.1  riastrad 		} else {
   2618  1.1  riastrad 			smc_result = rv770_send_msg_to_smc(rdev, PPSMC_TDPClampingInactive);
   2619  1.1  riastrad 			if (smc_result != PPSMC_Result_OK)
   2620  1.1  riastrad 				ret = -EINVAL;
   2621  1.1  riastrad 			ni_pi->pc_enabled = false;
   2622  1.1  riastrad 		}
   2623  1.1  riastrad 	}
   2624  1.1  riastrad 
   2625  1.1  riastrad 	return ret;
   2626  1.1  riastrad }
   2627  1.1  riastrad 
   2628  1.1  riastrad static int ni_convert_power_state_to_smc(struct radeon_device *rdev,
   2629  1.1  riastrad 					 struct radeon_ps *radeon_state,
   2630  1.1  riastrad 					 NISLANDS_SMC_SWSTATE *smc_state)
   2631  1.1  riastrad {
   2632  1.2  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   2633  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2634  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   2635  1.1  riastrad 	int i, ret;
   2636  1.1  riastrad 	u32 threshold = state->performance_levels[state->performance_level_count - 1].sclk * 100 / 100;
   2637  1.1  riastrad 
   2638  1.1  riastrad 	if (!(radeon_state->caps & ATOM_PPLIB_DISALLOW_ON_DC))
   2639  1.1  riastrad 		smc_state->flags |= PPSMC_SWSTATE_FLAG_DC;
   2640  1.1  riastrad 
   2641  1.1  riastrad 	smc_state->levelCount = 0;
   2642  1.1  riastrad 
   2643  1.1  riastrad 	if (state->performance_level_count > NISLANDS_MAX_SMC_PERFORMANCE_LEVELS_PER_SWSTATE)
   2644  1.1  riastrad 		return -EINVAL;
   2645  1.1  riastrad 
   2646  1.1  riastrad 	for (i = 0; i < state->performance_level_count; i++) {
   2647  1.1  riastrad 		ret = ni_convert_power_level_to_smc(rdev, &state->performance_levels[i],
   2648  1.1  riastrad 						    &smc_state->levels[i]);
   2649  1.1  riastrad 		smc_state->levels[i].arbRefreshState =
   2650  1.1  riastrad 			(u8)(NISLANDS_DRIVER_STATE_ARB_INDEX + i);
   2651  1.1  riastrad 
   2652  1.1  riastrad 		if (ret)
   2653  1.1  riastrad 			return ret;
   2654  1.1  riastrad 
   2655  1.1  riastrad 		if (ni_pi->enable_power_containment)
   2656  1.1  riastrad 			smc_state->levels[i].displayWatermark =
   2657  1.1  riastrad 				(state->performance_levels[i].sclk < threshold) ?
   2658  1.1  riastrad 				PPSMC_DISPLAY_WATERMARK_LOW : PPSMC_DISPLAY_WATERMARK_HIGH;
   2659  1.1  riastrad 		else
   2660  1.1  riastrad 			smc_state->levels[i].displayWatermark = (i < 2) ?
   2661  1.1  riastrad 				PPSMC_DISPLAY_WATERMARK_LOW : PPSMC_DISPLAY_WATERMARK_HIGH;
   2662  1.1  riastrad 
   2663  1.1  riastrad 		if (eg_pi->dynamic_ac_timing)
   2664  1.1  riastrad 			smc_state->levels[i].ACIndex = NISLANDS_MCREGISTERTABLE_FIRST_DRIVERSTATE_SLOT + i;
   2665  1.1  riastrad 		else
   2666  1.1  riastrad 			smc_state->levels[i].ACIndex = 0;
   2667  1.1  riastrad 
   2668  1.1  riastrad 		smc_state->levelCount++;
   2669  1.1  riastrad 	}
   2670  1.1  riastrad 
   2671  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_watermark_threshold,
   2672  1.1  riastrad 				      cpu_to_be32(threshold / 512));
   2673  1.1  riastrad 
   2674  1.1  riastrad 	ni_populate_smc_sp(rdev, radeon_state, smc_state);
   2675  1.1  riastrad 
   2676  1.1  riastrad 	ret = ni_populate_power_containment_values(rdev, radeon_state, smc_state);
   2677  1.1  riastrad 	if (ret)
   2678  1.1  riastrad 		ni_pi->enable_power_containment = false;
   2679  1.1  riastrad 
   2680  1.1  riastrad 	ret = ni_populate_sq_ramping_values(rdev, radeon_state, smc_state);
   2681  1.1  riastrad 	if (ret)
   2682  1.1  riastrad 		ni_pi->enable_sq_ramping = false;
   2683  1.1  riastrad 
   2684  1.1  riastrad 	return ni_populate_smc_t(rdev, radeon_state, smc_state);
   2685  1.1  riastrad }
   2686  1.1  riastrad 
   2687  1.1  riastrad static int ni_upload_sw_state(struct radeon_device *rdev,
   2688  1.1  riastrad 			      struct radeon_ps *radeon_new_state)
   2689  1.1  riastrad {
   2690  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2691  1.1  riastrad 	u16 address = pi->state_table_start +
   2692  1.1  riastrad 		offsetof(NISLANDS_SMC_STATETABLE, driverState);
   2693  1.1  riastrad 	u16 state_size = sizeof(NISLANDS_SMC_SWSTATE) +
   2694  1.1  riastrad 		((NISLANDS_MAX_SMC_PERFORMANCE_LEVELS_PER_SWSTATE - 1) * sizeof(NISLANDS_SMC_HW_PERFORMANCE_LEVEL));
   2695  1.1  riastrad 	int ret;
   2696  1.1  riastrad 	NISLANDS_SMC_SWSTATE *smc_state = kzalloc(state_size, GFP_KERNEL);
   2697  1.1  riastrad 
   2698  1.1  riastrad 	if (smc_state == NULL)
   2699  1.1  riastrad 		return -ENOMEM;
   2700  1.1  riastrad 
   2701  1.1  riastrad 	ret = ni_convert_power_state_to_smc(rdev, radeon_new_state, smc_state);
   2702  1.1  riastrad 	if (ret)
   2703  1.1  riastrad 		goto done;
   2704  1.1  riastrad 
   2705  1.1  riastrad 	ret = rv770_copy_bytes_to_smc(rdev, address, (u8 *)smc_state, state_size, pi->sram_end);
   2706  1.1  riastrad 
   2707  1.1  riastrad done:
   2708  1.1  riastrad 	kfree(smc_state);
   2709  1.1  riastrad 
   2710  1.1  riastrad 	return ret;
   2711  1.1  riastrad }
   2712  1.1  riastrad 
   2713  1.1  riastrad static int ni_set_mc_special_registers(struct radeon_device *rdev,
   2714  1.1  riastrad 				       struct ni_mc_reg_table *table)
   2715  1.1  riastrad {
   2716  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   2717  1.1  riastrad 	u8 i, j, k;
   2718  1.1  riastrad 	u32 temp_reg;
   2719  1.1  riastrad 
   2720  1.1  riastrad 	for (i = 0, j = table->last; i < table->last; i++) {
   2721  1.1  riastrad 		switch (table->mc_reg_address[i].s1) {
   2722  1.1  riastrad 		case MC_SEQ_MISC1 >> 2:
   2723  1.1  riastrad 			if (j >= SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2724  1.1  riastrad 				return -EINVAL;
   2725  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_EMRS);
   2726  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_EMRS >> 2;
   2727  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
   2728  1.1  riastrad 			for (k = 0; k < table->num_entries; k++)
   2729  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   2730  1.1  riastrad 					((temp_reg & 0xffff0000)) |
   2731  1.1  riastrad 					((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
   2732  1.1  riastrad 			j++;
   2733  1.1  riastrad 			if (j >= SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2734  1.1  riastrad 				return -EINVAL;
   2735  1.1  riastrad 
   2736  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_MRS);
   2737  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS >> 2;
   2738  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS_LP >> 2;
   2739  1.1  riastrad 			for(k = 0; k < table->num_entries; k++) {
   2740  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   2741  1.1  riastrad 					(temp_reg & 0xffff0000) |
   2742  1.1  riastrad 					(table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
   2743  1.1  riastrad 				if (!pi->mem_gddr5)
   2744  1.1  riastrad 					table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
   2745  1.1  riastrad 			}
   2746  1.1  riastrad 			j++;
   2747  1.1  riastrad 			if (j > SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2748  1.1  riastrad 				return -EINVAL;
   2749  1.1  riastrad 			break;
   2750  1.1  riastrad 		case MC_SEQ_RESERVE_M >> 2:
   2751  1.1  riastrad 			temp_reg = RREG32(MC_PMG_CMD_MRS1);
   2752  1.1  riastrad 			table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS1 >> 2;
   2753  1.1  riastrad 			table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
   2754  1.1  riastrad 			for (k = 0; k < table->num_entries; k++)
   2755  1.1  riastrad 				table->mc_reg_table_entry[k].mc_data[j] =
   2756  1.1  riastrad 					(temp_reg & 0xffff0000) |
   2757  1.1  riastrad 					(table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
   2758  1.1  riastrad 			j++;
   2759  1.1  riastrad 			if (j > SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2760  1.1  riastrad 				return -EINVAL;
   2761  1.1  riastrad 			break;
   2762  1.1  riastrad 		default:
   2763  1.1  riastrad 			break;
   2764  1.1  riastrad 		}
   2765  1.1  riastrad 	}
   2766  1.1  riastrad 
   2767  1.1  riastrad 	table->last = j;
   2768  1.1  riastrad 
   2769  1.1  riastrad 	return 0;
   2770  1.1  riastrad }
   2771  1.1  riastrad 
   2772  1.1  riastrad static bool ni_check_s0_mc_reg_index(u16 in_reg, u16 *out_reg)
   2773  1.1  riastrad {
   2774  1.1  riastrad 	bool result = true;
   2775  1.1  riastrad 
   2776  1.1  riastrad 	switch (in_reg) {
   2777  1.2  riastrad 	case  MC_SEQ_RAS_TIMING >> 2:
   2778  1.1  riastrad 		*out_reg = MC_SEQ_RAS_TIMING_LP >> 2;
   2779  1.1  riastrad 		break;
   2780  1.2  riastrad 	case MC_SEQ_CAS_TIMING >> 2:
   2781  1.1  riastrad 		*out_reg = MC_SEQ_CAS_TIMING_LP >> 2;
   2782  1.1  riastrad 		break;
   2783  1.2  riastrad 	case MC_SEQ_MISC_TIMING >> 2:
   2784  1.1  riastrad 		*out_reg = MC_SEQ_MISC_TIMING_LP >> 2;
   2785  1.1  riastrad 		break;
   2786  1.2  riastrad 	case MC_SEQ_MISC_TIMING2 >> 2:
   2787  1.1  riastrad 		*out_reg = MC_SEQ_MISC_TIMING2_LP >> 2;
   2788  1.1  riastrad 		break;
   2789  1.2  riastrad 	case MC_SEQ_RD_CTL_D0 >> 2:
   2790  1.1  riastrad 		*out_reg = MC_SEQ_RD_CTL_D0_LP >> 2;
   2791  1.1  riastrad 		break;
   2792  1.2  riastrad 	case MC_SEQ_RD_CTL_D1 >> 2:
   2793  1.1  riastrad 		*out_reg = MC_SEQ_RD_CTL_D1_LP >> 2;
   2794  1.1  riastrad 		break;
   2795  1.2  riastrad 	case MC_SEQ_WR_CTL_D0 >> 2:
   2796  1.1  riastrad 		*out_reg = MC_SEQ_WR_CTL_D0_LP >> 2;
   2797  1.1  riastrad 		break;
   2798  1.2  riastrad 	case MC_SEQ_WR_CTL_D1 >> 2:
   2799  1.1  riastrad 		*out_reg = MC_SEQ_WR_CTL_D1_LP >> 2;
   2800  1.1  riastrad 		break;
   2801  1.2  riastrad 	case MC_PMG_CMD_EMRS >> 2:
   2802  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
   2803  1.1  riastrad 		break;
   2804  1.2  riastrad 	case MC_PMG_CMD_MRS >> 2:
   2805  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS_LP >> 2;
   2806  1.1  riastrad 		break;
   2807  1.2  riastrad 	case MC_PMG_CMD_MRS1 >> 2:
   2808  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
   2809  1.1  riastrad 		break;
   2810  1.2  riastrad 	case MC_SEQ_PMG_TIMING >> 2:
   2811  1.1  riastrad 		*out_reg = MC_SEQ_PMG_TIMING_LP >> 2;
   2812  1.1  riastrad 		break;
   2813  1.2  riastrad 	case MC_PMG_CMD_MRS2 >> 2:
   2814  1.1  riastrad 		*out_reg = MC_SEQ_PMG_CMD_MRS2_LP >> 2;
   2815  1.1  riastrad 		break;
   2816  1.2  riastrad 	default:
   2817  1.1  riastrad 		result = false;
   2818  1.1  riastrad 		break;
   2819  1.1  riastrad 	}
   2820  1.1  riastrad 
   2821  1.1  riastrad 	return result;
   2822  1.1  riastrad }
   2823  1.1  riastrad 
   2824  1.1  riastrad static void ni_set_valid_flag(struct ni_mc_reg_table *table)
   2825  1.1  riastrad {
   2826  1.1  riastrad 	u8 i, j;
   2827  1.1  riastrad 
   2828  1.1  riastrad 	for (i = 0; i < table->last; i++) {
   2829  1.1  riastrad 		for (j = 1; j < table->num_entries; j++) {
   2830  1.1  riastrad 			if (table->mc_reg_table_entry[j-1].mc_data[i] != table->mc_reg_table_entry[j].mc_data[i]) {
   2831  1.1  riastrad 				table->valid_flag |= 1 << i;
   2832  1.1  riastrad 				break;
   2833  1.1  riastrad 			}
   2834  1.1  riastrad 		}
   2835  1.1  riastrad 	}
   2836  1.1  riastrad }
   2837  1.1  riastrad 
   2838  1.1  riastrad static void ni_set_s0_mc_reg_index(struct ni_mc_reg_table *table)
   2839  1.1  riastrad {
   2840  1.1  riastrad 	u32 i;
   2841  1.1  riastrad 	u16 address;
   2842  1.1  riastrad 
   2843  1.1  riastrad 	for (i = 0; i < table->last; i++)
   2844  1.1  riastrad 		table->mc_reg_address[i].s0 =
   2845  1.1  riastrad 			ni_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address) ?
   2846  1.1  riastrad 			address : table->mc_reg_address[i].s1;
   2847  1.1  riastrad }
   2848  1.1  riastrad 
   2849  1.1  riastrad static int ni_copy_vbios_mc_reg_table(struct atom_mc_reg_table *table,
   2850  1.1  riastrad 				      struct ni_mc_reg_table *ni_table)
   2851  1.1  riastrad {
   2852  1.1  riastrad 	u8 i, j;
   2853  1.1  riastrad 
   2854  1.1  riastrad 	if (table->last > SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2855  1.1  riastrad 		return -EINVAL;
   2856  1.1  riastrad 	if (table->num_entries > MAX_AC_TIMING_ENTRIES)
   2857  1.1  riastrad 		return -EINVAL;
   2858  1.1  riastrad 
   2859  1.1  riastrad 	for (i = 0; i < table->last; i++)
   2860  1.1  riastrad 		ni_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
   2861  1.1  riastrad 	ni_table->last = table->last;
   2862  1.1  riastrad 
   2863  1.1  riastrad 	for (i = 0; i < table->num_entries; i++) {
   2864  1.1  riastrad 		ni_table->mc_reg_table_entry[i].mclk_max =
   2865  1.1  riastrad 			table->mc_reg_table_entry[i].mclk_max;
   2866  1.1  riastrad 		for (j = 0; j < table->last; j++)
   2867  1.1  riastrad 			ni_table->mc_reg_table_entry[i].mc_data[j] =
   2868  1.1  riastrad 				table->mc_reg_table_entry[i].mc_data[j];
   2869  1.1  riastrad 	}
   2870  1.1  riastrad 	ni_table->num_entries = table->num_entries;
   2871  1.1  riastrad 
   2872  1.1  riastrad 	return 0;
   2873  1.1  riastrad }
   2874  1.1  riastrad 
   2875  1.1  riastrad static int ni_initialize_mc_reg_table(struct radeon_device *rdev)
   2876  1.1  riastrad {
   2877  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2878  1.1  riastrad 	int ret;
   2879  1.1  riastrad 	struct atom_mc_reg_table *table;
   2880  1.1  riastrad 	struct ni_mc_reg_table *ni_table = &ni_pi->mc_reg_table;
   2881  1.1  riastrad 	u8 module_index = rv770_get_memory_module_index(rdev);
   2882  1.1  riastrad 
   2883  1.2  riastrad 	table = kzalloc(sizeof(struct atom_mc_reg_table), GFP_KERNEL);
   2884  1.2  riastrad 	if (!table)
   2885  1.2  riastrad 		return -ENOMEM;
   2886  1.1  riastrad 
   2887  1.1  riastrad 	WREG32(MC_SEQ_RAS_TIMING_LP, RREG32(MC_SEQ_RAS_TIMING));
   2888  1.1  riastrad 	WREG32(MC_SEQ_CAS_TIMING_LP, RREG32(MC_SEQ_CAS_TIMING));
   2889  1.1  riastrad 	WREG32(MC_SEQ_MISC_TIMING_LP, RREG32(MC_SEQ_MISC_TIMING));
   2890  1.1  riastrad 	WREG32(MC_SEQ_MISC_TIMING2_LP, RREG32(MC_SEQ_MISC_TIMING2));
   2891  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_EMRS_LP, RREG32(MC_PMG_CMD_EMRS));
   2892  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS_LP, RREG32(MC_PMG_CMD_MRS));
   2893  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS1_LP, RREG32(MC_PMG_CMD_MRS1));
   2894  1.1  riastrad 	WREG32(MC_SEQ_WR_CTL_D0_LP, RREG32(MC_SEQ_WR_CTL_D0));
   2895  1.1  riastrad 	WREG32(MC_SEQ_WR_CTL_D1_LP, RREG32(MC_SEQ_WR_CTL_D1));
   2896  1.1  riastrad 	WREG32(MC_SEQ_RD_CTL_D0_LP, RREG32(MC_SEQ_RD_CTL_D0));
   2897  1.1  riastrad 	WREG32(MC_SEQ_RD_CTL_D1_LP, RREG32(MC_SEQ_RD_CTL_D1));
   2898  1.1  riastrad 	WREG32(MC_SEQ_PMG_TIMING_LP, RREG32(MC_SEQ_PMG_TIMING));
   2899  1.1  riastrad 	WREG32(MC_SEQ_PMG_CMD_MRS2_LP, RREG32(MC_PMG_CMD_MRS2));
   2900  1.1  riastrad 
   2901  1.1  riastrad 	ret = radeon_atom_init_mc_reg_table(rdev, module_index, table);
   2902  1.1  riastrad 
   2903  1.2  riastrad 	if (ret)
   2904  1.2  riastrad 		goto init_mc_done;
   2905  1.1  riastrad 
   2906  1.1  riastrad 	ret = ni_copy_vbios_mc_reg_table(table, ni_table);
   2907  1.1  riastrad 
   2908  1.2  riastrad 	if (ret)
   2909  1.2  riastrad 		goto init_mc_done;
   2910  1.1  riastrad 
   2911  1.1  riastrad 	ni_set_s0_mc_reg_index(ni_table);
   2912  1.1  riastrad 
   2913  1.1  riastrad 	ret = ni_set_mc_special_registers(rdev, ni_table);
   2914  1.1  riastrad 
   2915  1.2  riastrad 	if (ret)
   2916  1.2  riastrad 		goto init_mc_done;
   2917  1.1  riastrad 
   2918  1.1  riastrad 	ni_set_valid_flag(ni_table);
   2919  1.1  riastrad 
   2920  1.1  riastrad init_mc_done:
   2921  1.2  riastrad 	kfree(table);
   2922  1.1  riastrad 
   2923  1.1  riastrad 	return ret;
   2924  1.1  riastrad }
   2925  1.1  riastrad 
   2926  1.1  riastrad static void ni_populate_mc_reg_addresses(struct radeon_device *rdev,
   2927  1.1  riastrad 					 SMC_NIslands_MCRegisters *mc_reg_table)
   2928  1.1  riastrad {
   2929  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2930  1.1  riastrad 	u32 i, j;
   2931  1.1  riastrad 
   2932  1.1  riastrad 	for (i = 0, j = 0; j < ni_pi->mc_reg_table.last; j++) {
   2933  1.1  riastrad 		if (ni_pi->mc_reg_table.valid_flag & (1 << j)) {
   2934  1.1  riastrad 			if (i >= SMC_NISLANDS_MC_REGISTER_ARRAY_SIZE)
   2935  1.1  riastrad 				break;
   2936  1.1  riastrad 			mc_reg_table->address[i].s0 =
   2937  1.1  riastrad 				cpu_to_be16(ni_pi->mc_reg_table.mc_reg_address[j].s0);
   2938  1.1  riastrad 			mc_reg_table->address[i].s1 =
   2939  1.1  riastrad 				cpu_to_be16(ni_pi->mc_reg_table.mc_reg_address[j].s1);
   2940  1.1  riastrad 			i++;
   2941  1.1  riastrad 		}
   2942  1.1  riastrad 	}
   2943  1.1  riastrad 	mc_reg_table->last = (u8)i;
   2944  1.1  riastrad }
   2945  1.1  riastrad 
   2946  1.1  riastrad 
   2947  1.1  riastrad static void ni_convert_mc_registers(struct ni_mc_reg_entry *entry,
   2948  1.1  riastrad 				    SMC_NIslands_MCRegisterSet *data,
   2949  1.1  riastrad 				    u32 num_entries, u32 valid_flag)
   2950  1.1  riastrad {
   2951  1.1  riastrad 	u32 i, j;
   2952  1.1  riastrad 
   2953  1.1  riastrad 	for (i = 0, j = 0; j < num_entries; j++) {
   2954  1.1  riastrad 		if (valid_flag & (1 << j)) {
   2955  1.1  riastrad 			data->value[i] = cpu_to_be32(entry->mc_data[j]);
   2956  1.1  riastrad 			i++;
   2957  1.1  riastrad 		}
   2958  1.1  riastrad 	}
   2959  1.1  riastrad }
   2960  1.1  riastrad 
   2961  1.1  riastrad static void ni_convert_mc_reg_table_entry_to_smc(struct radeon_device *rdev,
   2962  1.1  riastrad 						 struct rv7xx_pl *pl,
   2963  1.1  riastrad 						 SMC_NIslands_MCRegisterSet *mc_reg_table_data)
   2964  1.1  riastrad {
   2965  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   2966  1.1  riastrad 	u32 i = 0;
   2967  1.1  riastrad 
   2968  1.1  riastrad 	for (i = 0; i < ni_pi->mc_reg_table.num_entries; i++) {
   2969  1.1  riastrad 		if (pl->mclk <= ni_pi->mc_reg_table.mc_reg_table_entry[i].mclk_max)
   2970  1.1  riastrad 			break;
   2971  1.1  riastrad 	}
   2972  1.1  riastrad 
   2973  1.1  riastrad 	if ((i == ni_pi->mc_reg_table.num_entries) && (i > 0))
   2974  1.1  riastrad 		--i;
   2975  1.1  riastrad 
   2976  1.1  riastrad 	ni_convert_mc_registers(&ni_pi->mc_reg_table.mc_reg_table_entry[i],
   2977  1.1  riastrad 				mc_reg_table_data,
   2978  1.1  riastrad 				ni_pi->mc_reg_table.last,
   2979  1.1  riastrad 				ni_pi->mc_reg_table.valid_flag);
   2980  1.1  riastrad }
   2981  1.1  riastrad 
   2982  1.1  riastrad static void ni_convert_mc_reg_table_to_smc(struct radeon_device *rdev,
   2983  1.1  riastrad 					   struct radeon_ps *radeon_state,
   2984  1.1  riastrad 					   SMC_NIslands_MCRegisters *mc_reg_table)
   2985  1.1  riastrad {
   2986  1.1  riastrad 	struct ni_ps *state = ni_get_ps(radeon_state);
   2987  1.1  riastrad 	int i;
   2988  1.1  riastrad 
   2989  1.1  riastrad 	for (i = 0; i < state->performance_level_count; i++) {
   2990  1.1  riastrad 		ni_convert_mc_reg_table_entry_to_smc(rdev,
   2991  1.1  riastrad 						     &state->performance_levels[i],
   2992  1.1  riastrad 						     &mc_reg_table->data[NISLANDS_MCREGISTERTABLE_FIRST_DRIVERSTATE_SLOT + i]);
   2993  1.1  riastrad 	}
   2994  1.1  riastrad }
   2995  1.1  riastrad 
   2996  1.1  riastrad static int ni_populate_mc_reg_table(struct radeon_device *rdev,
   2997  1.1  riastrad 				    struct radeon_ps *radeon_boot_state)
   2998  1.1  riastrad {
   2999  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3000  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3001  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3002  1.1  riastrad 	struct ni_ps *boot_state = ni_get_ps(radeon_boot_state);
   3003  1.1  riastrad 	SMC_NIslands_MCRegisters *mc_reg_table = &ni_pi->smc_mc_reg_table;
   3004  1.1  riastrad 
   3005  1.1  riastrad 	memset(mc_reg_table, 0, sizeof(SMC_NIslands_MCRegisters));
   3006  1.1  riastrad 
   3007  1.1  riastrad 	rv770_write_smc_soft_register(rdev, NI_SMC_SOFT_REGISTER_seq_index, 1);
   3008  1.1  riastrad 
   3009  1.1  riastrad 	ni_populate_mc_reg_addresses(rdev, mc_reg_table);
   3010  1.1  riastrad 
   3011  1.1  riastrad 	ni_convert_mc_reg_table_entry_to_smc(rdev, &boot_state->performance_levels[0],
   3012  1.1  riastrad 					     &mc_reg_table->data[0]);
   3013  1.1  riastrad 
   3014  1.1  riastrad 	ni_convert_mc_registers(&ni_pi->mc_reg_table.mc_reg_table_entry[0],
   3015  1.1  riastrad 				&mc_reg_table->data[1],
   3016  1.1  riastrad 				ni_pi->mc_reg_table.last,
   3017  1.1  riastrad 				ni_pi->mc_reg_table.valid_flag);
   3018  1.1  riastrad 
   3019  1.1  riastrad 	ni_convert_mc_reg_table_to_smc(rdev, radeon_boot_state, mc_reg_table);
   3020  1.1  riastrad 
   3021  1.1  riastrad 	return rv770_copy_bytes_to_smc(rdev, eg_pi->mc_reg_table_start,
   3022  1.1  riastrad 				       (u8 *)mc_reg_table,
   3023  1.1  riastrad 				       sizeof(SMC_NIslands_MCRegisters),
   3024  1.1  riastrad 				       pi->sram_end);
   3025  1.1  riastrad }
   3026  1.1  riastrad 
   3027  1.1  riastrad static int ni_upload_mc_reg_table(struct radeon_device *rdev,
   3028  1.1  riastrad 				  struct radeon_ps *radeon_new_state)
   3029  1.1  riastrad {
   3030  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3031  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3032  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3033  1.1  riastrad 	struct ni_ps *ni_new_state = ni_get_ps(radeon_new_state);
   3034  1.1  riastrad 	SMC_NIslands_MCRegisters *mc_reg_table = &ni_pi->smc_mc_reg_table;
   3035  1.1  riastrad 	u16 address;
   3036  1.1  riastrad 
   3037  1.1  riastrad 	memset(mc_reg_table, 0, sizeof(SMC_NIslands_MCRegisters));
   3038  1.1  riastrad 
   3039  1.1  riastrad 	ni_convert_mc_reg_table_to_smc(rdev, radeon_new_state, mc_reg_table);
   3040  1.1  riastrad 
   3041  1.1  riastrad 	address = eg_pi->mc_reg_table_start +
   3042  1.1  riastrad 		(u16)offsetof(SMC_NIslands_MCRegisters, data[NISLANDS_MCREGISTERTABLE_FIRST_DRIVERSTATE_SLOT]);
   3043  1.1  riastrad 
   3044  1.1  riastrad 	return rv770_copy_bytes_to_smc(rdev, address,
   3045  1.1  riastrad 				       (u8 *)&mc_reg_table->data[NISLANDS_MCREGISTERTABLE_FIRST_DRIVERSTATE_SLOT],
   3046  1.1  riastrad 				       sizeof(SMC_NIslands_MCRegisterSet) * ni_new_state->performance_level_count,
   3047  1.1  riastrad 				       pi->sram_end);
   3048  1.1  riastrad }
   3049  1.1  riastrad 
   3050  1.1  riastrad static int ni_init_driver_calculated_leakage_table(struct radeon_device *rdev,
   3051  1.1  riastrad 						   PP_NIslands_CACTABLES *cac_tables)
   3052  1.1  riastrad {
   3053  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3054  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3055  1.1  riastrad 	u32 leakage = 0;
   3056  1.1  riastrad 	unsigned int i, j, table_size;
   3057  1.1  riastrad 	s32 t;
   3058  1.1  riastrad 	u32 smc_leakage, max_leakage = 0;
   3059  1.1  riastrad 	u32 scaling_factor;
   3060  1.1  riastrad 
   3061  1.1  riastrad 	table_size = eg_pi->vddc_voltage_table.count;
   3062  1.1  riastrad 
   3063  1.1  riastrad 	if (SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES < table_size)
   3064  1.1  riastrad 		table_size = SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES;
   3065  1.1  riastrad 
   3066  1.1  riastrad 	scaling_factor = ni_get_smc_power_scaling_factor(rdev);
   3067  1.1  riastrad 
   3068  1.1  riastrad 	for (i = 0; i < SMC_NISLANDS_LKGE_LUT_NUM_OF_TEMP_ENTRIES; i++) {
   3069  1.1  riastrad 		for (j = 0; j < table_size; j++) {
   3070  1.1  riastrad 			t = (1000 * ((i + 1) * 8));
   3071  1.1  riastrad 
   3072  1.1  riastrad 			if (t < ni_pi->cac_data.leakage_minimum_temperature)
   3073  1.1  riastrad 				t = ni_pi->cac_data.leakage_minimum_temperature;
   3074  1.1  riastrad 
   3075  1.1  riastrad 			ni_calculate_leakage_for_v_and_t(rdev,
   3076  1.1  riastrad 							 &ni_pi->cac_data.leakage_coefficients,
   3077  1.1  riastrad 							 eg_pi->vddc_voltage_table.entries[j].value,
   3078  1.1  riastrad 							 t,
   3079  1.1  riastrad 							 ni_pi->cac_data.i_leakage,
   3080  1.1  riastrad 							 &leakage);
   3081  1.1  riastrad 
   3082  1.1  riastrad 			smc_leakage = ni_scale_power_for_smc(leakage, scaling_factor) / 1000;
   3083  1.1  riastrad 			if (smc_leakage > max_leakage)
   3084  1.1  riastrad 				max_leakage = smc_leakage;
   3085  1.1  riastrad 
   3086  1.1  riastrad 			cac_tables->cac_lkge_lut[i][j] = cpu_to_be32(smc_leakage);
   3087  1.1  riastrad 		}
   3088  1.1  riastrad 	}
   3089  1.1  riastrad 
   3090  1.1  riastrad 	for (j = table_size; j < SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES; j++) {
   3091  1.1  riastrad 		for (i = 0; i < SMC_NISLANDS_LKGE_LUT_NUM_OF_TEMP_ENTRIES; i++)
   3092  1.1  riastrad 			cac_tables->cac_lkge_lut[i][j] = cpu_to_be32(max_leakage);
   3093  1.1  riastrad 	}
   3094  1.1  riastrad 	return 0;
   3095  1.1  riastrad }
   3096  1.1  riastrad 
   3097  1.1  riastrad static int ni_init_simplified_leakage_table(struct radeon_device *rdev,
   3098  1.1  riastrad 					    PP_NIslands_CACTABLES *cac_tables)
   3099  1.1  riastrad {
   3100  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3101  1.1  riastrad 	struct radeon_cac_leakage_table *leakage_table =
   3102  1.1  riastrad 		&rdev->pm.dpm.dyn_state.cac_leakage_table;
   3103  1.1  riastrad 	u32 i, j, table_size;
   3104  1.1  riastrad 	u32 smc_leakage, max_leakage = 0;
   3105  1.1  riastrad 	u32 scaling_factor;
   3106  1.1  riastrad 
   3107  1.1  riastrad 	if (!leakage_table)
   3108  1.1  riastrad 		return -EINVAL;
   3109  1.1  riastrad 
   3110  1.1  riastrad 	table_size = leakage_table->count;
   3111  1.1  riastrad 
   3112  1.1  riastrad 	if (eg_pi->vddc_voltage_table.count != table_size)
   3113  1.1  riastrad 		table_size = (eg_pi->vddc_voltage_table.count < leakage_table->count) ?
   3114  1.1  riastrad 			eg_pi->vddc_voltage_table.count : leakage_table->count;
   3115  1.1  riastrad 
   3116  1.1  riastrad 	if (SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES < table_size)
   3117  1.1  riastrad 		table_size = SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES;
   3118  1.1  riastrad 
   3119  1.1  riastrad 	if (table_size == 0)
   3120  1.1  riastrad 		return -EINVAL;
   3121  1.1  riastrad 
   3122  1.1  riastrad 	scaling_factor = ni_get_smc_power_scaling_factor(rdev);
   3123  1.1  riastrad 
   3124  1.1  riastrad 	for (j = 0; j < table_size; j++) {
   3125  1.1  riastrad 		smc_leakage = leakage_table->entries[j].leakage;
   3126  1.1  riastrad 
   3127  1.1  riastrad 		if (smc_leakage > max_leakage)
   3128  1.1  riastrad 			max_leakage = smc_leakage;
   3129  1.1  riastrad 
   3130  1.1  riastrad 		for (i = 0; i < SMC_NISLANDS_LKGE_LUT_NUM_OF_TEMP_ENTRIES; i++)
   3131  1.1  riastrad 			cac_tables->cac_lkge_lut[i][j] =
   3132  1.1  riastrad 				cpu_to_be32(ni_scale_power_for_smc(smc_leakage, scaling_factor));
   3133  1.1  riastrad 	}
   3134  1.1  riastrad 
   3135  1.1  riastrad 	for (j = table_size; j < SMC_NISLANDS_LKGE_LUT_NUM_OF_VOLT_ENTRIES; j++) {
   3136  1.1  riastrad 		for (i = 0; i < SMC_NISLANDS_LKGE_LUT_NUM_OF_TEMP_ENTRIES; i++)
   3137  1.1  riastrad 			cac_tables->cac_lkge_lut[i][j] =
   3138  1.1  riastrad 				cpu_to_be32(ni_scale_power_for_smc(max_leakage, scaling_factor));
   3139  1.1  riastrad 	}
   3140  1.1  riastrad 	return 0;
   3141  1.1  riastrad }
   3142  1.1  riastrad 
   3143  1.1  riastrad static int ni_initialize_smc_cac_tables(struct radeon_device *rdev)
   3144  1.1  riastrad {
   3145  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3146  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3147  1.1  riastrad 	PP_NIslands_CACTABLES *cac_tables = NULL;
   3148  1.1  riastrad 	int i, ret;
   3149  1.2  riastrad 	u32 reg;
   3150  1.1  riastrad 
   3151  1.1  riastrad 	if (ni_pi->enable_cac == false)
   3152  1.1  riastrad 		return 0;
   3153  1.1  riastrad 
   3154  1.1  riastrad 	cac_tables = kzalloc(sizeof(PP_NIslands_CACTABLES), GFP_KERNEL);
   3155  1.1  riastrad 	if (!cac_tables)
   3156  1.1  riastrad 		return -ENOMEM;
   3157  1.1  riastrad 
   3158  1.1  riastrad 	reg = RREG32(CG_CAC_CTRL) & ~(TID_CNT_MASK | TID_UNIT_MASK);
   3159  1.1  riastrad 	reg |= (TID_CNT(ni_pi->cac_weights->tid_cnt) |
   3160  1.1  riastrad 		TID_UNIT(ni_pi->cac_weights->tid_unit));
   3161  1.1  riastrad 	WREG32(CG_CAC_CTRL, reg);
   3162  1.1  riastrad 
   3163  1.1  riastrad 	for (i = 0; i < NISLANDS_DCCAC_MAX_LEVELS; i++)
   3164  1.1  riastrad 		ni_pi->dc_cac_table[i] = ni_pi->cac_weights->dc_cac[i];
   3165  1.1  riastrad 
   3166  1.1  riastrad 	for (i = 0; i < SMC_NISLANDS_BIF_LUT_NUM_OF_ENTRIES; i++)
   3167  1.1  riastrad 		cac_tables->cac_bif_lut[i] = ni_pi->cac_weights->pcie_cac[i];
   3168  1.1  riastrad 
   3169  1.1  riastrad 	ni_pi->cac_data.i_leakage = rdev->pm.dpm.cac_leakage;
   3170  1.1  riastrad 	ni_pi->cac_data.pwr_const = 0;
   3171  1.1  riastrad 	ni_pi->cac_data.dc_cac_value = ni_pi->dc_cac_table[NISLANDS_DCCAC_LEVEL_0];
   3172  1.1  riastrad 	ni_pi->cac_data.bif_cac_value = 0;
   3173  1.1  riastrad 	ni_pi->cac_data.mc_wr_weight = ni_pi->cac_weights->mc_write_weight;
   3174  1.1  riastrad 	ni_pi->cac_data.mc_rd_weight = ni_pi->cac_weights->mc_read_weight;
   3175  1.1  riastrad 	ni_pi->cac_data.allow_ovrflw = 0;
   3176  1.1  riastrad 	ni_pi->cac_data.l2num_win_tdp = ni_pi->lta_window_size;
   3177  1.1  riastrad 	ni_pi->cac_data.num_win_tdp = 0;
   3178  1.1  riastrad 	ni_pi->cac_data.lts_truncate_n = ni_pi->lts_truncate;
   3179  1.1  riastrad 
   3180  1.1  riastrad 	if (ni_pi->driver_calculate_cac_leakage)
   3181  1.1  riastrad 		ret = ni_init_driver_calculated_leakage_table(rdev, cac_tables);
   3182  1.1  riastrad 	else
   3183  1.1  riastrad 		ret = ni_init_simplified_leakage_table(rdev, cac_tables);
   3184  1.1  riastrad 
   3185  1.1  riastrad 	if (ret)
   3186  1.1  riastrad 		goto done_free;
   3187  1.1  riastrad 
   3188  1.1  riastrad 	cac_tables->pwr_const      = cpu_to_be32(ni_pi->cac_data.pwr_const);
   3189  1.1  riastrad 	cac_tables->dc_cacValue    = cpu_to_be32(ni_pi->cac_data.dc_cac_value);
   3190  1.1  riastrad 	cac_tables->bif_cacValue   = cpu_to_be32(ni_pi->cac_data.bif_cac_value);
   3191  1.1  riastrad 	cac_tables->AllowOvrflw    = ni_pi->cac_data.allow_ovrflw;
   3192  1.1  riastrad 	cac_tables->MCWrWeight     = ni_pi->cac_data.mc_wr_weight;
   3193  1.1  riastrad 	cac_tables->MCRdWeight     = ni_pi->cac_data.mc_rd_weight;
   3194  1.1  riastrad 	cac_tables->numWin_TDP     = ni_pi->cac_data.num_win_tdp;
   3195  1.1  riastrad 	cac_tables->l2numWin_TDP   = ni_pi->cac_data.l2num_win_tdp;
   3196  1.1  riastrad 	cac_tables->lts_truncate_n = ni_pi->cac_data.lts_truncate_n;
   3197  1.1  riastrad 
   3198  1.1  riastrad 	ret = rv770_copy_bytes_to_smc(rdev, ni_pi->cac_table_start, (u8 *)cac_tables,
   3199  1.1  riastrad 				      sizeof(PP_NIslands_CACTABLES), pi->sram_end);
   3200  1.1  riastrad 
   3201  1.1  riastrad done_free:
   3202  1.1  riastrad 	if (ret) {
   3203  1.1  riastrad 		ni_pi->enable_cac = false;
   3204  1.1  riastrad 		ni_pi->enable_power_containment = false;
   3205  1.1  riastrad 	}
   3206  1.1  riastrad 
   3207  1.1  riastrad 	kfree(cac_tables);
   3208  1.1  riastrad 
   3209  1.1  riastrad 	return 0;
   3210  1.1  riastrad }
   3211  1.1  riastrad 
   3212  1.1  riastrad static int ni_initialize_hardware_cac_manager(struct radeon_device *rdev)
   3213  1.1  riastrad {
   3214  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3215  1.1  riastrad 	u32 reg;
   3216  1.1  riastrad 
   3217  1.1  riastrad 	if (!ni_pi->enable_cac ||
   3218  1.1  riastrad 	    !ni_pi->cac_configuration_required)
   3219  1.1  riastrad 		return 0;
   3220  1.1  riastrad 
   3221  1.1  riastrad 	if (ni_pi->cac_weights == NULL)
   3222  1.1  riastrad 		return -EINVAL;
   3223  1.1  riastrad 
   3224  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_1_WEIGHT_0) & ~(WEIGHT_TCP_SIG0_MASK |
   3225  1.1  riastrad 						      WEIGHT_TCP_SIG1_MASK |
   3226  1.1  riastrad 						      WEIGHT_TA_SIG_MASK);
   3227  1.1  riastrad 	reg |= (WEIGHT_TCP_SIG0(ni_pi->cac_weights->weight_tcp_sig0) |
   3228  1.1  riastrad 		WEIGHT_TCP_SIG1(ni_pi->cac_weights->weight_tcp_sig1) |
   3229  1.1  riastrad 		WEIGHT_TA_SIG(ni_pi->cac_weights->weight_ta_sig));
   3230  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_1_WEIGHT_0, reg);
   3231  1.1  riastrad 
   3232  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_1_WEIGHT_1) & ~(WEIGHT_TCC_EN0_MASK |
   3233  1.1  riastrad 						      WEIGHT_TCC_EN1_MASK |
   3234  1.1  riastrad 						      WEIGHT_TCC_EN2_MASK);
   3235  1.1  riastrad 	reg |= (WEIGHT_TCC_EN0(ni_pi->cac_weights->weight_tcc_en0) |
   3236  1.1  riastrad 		WEIGHT_TCC_EN1(ni_pi->cac_weights->weight_tcc_en1) |
   3237  1.1  riastrad 		WEIGHT_TCC_EN2(ni_pi->cac_weights->weight_tcc_en2));
   3238  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_1_WEIGHT_1, reg);
   3239  1.1  riastrad 
   3240  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_2_WEIGHT_0) & ~(WEIGHT_CB_EN0_MASK |
   3241  1.1  riastrad 						      WEIGHT_CB_EN1_MASK |
   3242  1.1  riastrad 						      WEIGHT_CB_EN2_MASK |
   3243  1.1  riastrad 						      WEIGHT_CB_EN3_MASK);
   3244  1.1  riastrad 	reg |= (WEIGHT_CB_EN0(ni_pi->cac_weights->weight_cb_en0) |
   3245  1.1  riastrad 		WEIGHT_CB_EN1(ni_pi->cac_weights->weight_cb_en1) |
   3246  1.1  riastrad 		WEIGHT_CB_EN2(ni_pi->cac_weights->weight_cb_en2) |
   3247  1.1  riastrad 		WEIGHT_CB_EN3(ni_pi->cac_weights->weight_cb_en3));
   3248  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_2_WEIGHT_0, reg);
   3249  1.1  riastrad 
   3250  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_2_WEIGHT_1) & ~(WEIGHT_DB_SIG0_MASK |
   3251  1.1  riastrad 						      WEIGHT_DB_SIG1_MASK |
   3252  1.1  riastrad 						      WEIGHT_DB_SIG2_MASK |
   3253  1.1  riastrad 						      WEIGHT_DB_SIG3_MASK);
   3254  1.1  riastrad 	reg |= (WEIGHT_DB_SIG0(ni_pi->cac_weights->weight_db_sig0) |
   3255  1.1  riastrad 		WEIGHT_DB_SIG1(ni_pi->cac_weights->weight_db_sig1) |
   3256  1.1  riastrad 		WEIGHT_DB_SIG2(ni_pi->cac_weights->weight_db_sig2) |
   3257  1.1  riastrad 		WEIGHT_DB_SIG3(ni_pi->cac_weights->weight_db_sig3));
   3258  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_2_WEIGHT_1, reg);
   3259  1.1  riastrad 
   3260  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_2_WEIGHT_2) & ~(WEIGHT_SXM_SIG0_MASK |
   3261  1.1  riastrad 						      WEIGHT_SXM_SIG1_MASK |
   3262  1.1  riastrad 						      WEIGHT_SXM_SIG2_MASK |
   3263  1.1  riastrad 						      WEIGHT_SXS_SIG0_MASK |
   3264  1.1  riastrad 						      WEIGHT_SXS_SIG1_MASK);
   3265  1.1  riastrad 	reg |= (WEIGHT_SXM_SIG0(ni_pi->cac_weights->weight_sxm_sig0) |
   3266  1.1  riastrad 		WEIGHT_SXM_SIG1(ni_pi->cac_weights->weight_sxm_sig1) |
   3267  1.1  riastrad 		WEIGHT_SXM_SIG2(ni_pi->cac_weights->weight_sxm_sig2) |
   3268  1.1  riastrad 		WEIGHT_SXS_SIG0(ni_pi->cac_weights->weight_sxs_sig0) |
   3269  1.1  riastrad 		WEIGHT_SXS_SIG1(ni_pi->cac_weights->weight_sxs_sig1));
   3270  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_2_WEIGHT_2, reg);
   3271  1.1  riastrad 
   3272  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_3_WEIGHT_0) & ~(WEIGHT_XBR_0_MASK |
   3273  1.1  riastrad 						      WEIGHT_XBR_1_MASK |
   3274  1.1  riastrad 						      WEIGHT_XBR_2_MASK |
   3275  1.1  riastrad 						      WEIGHT_SPI_SIG0_MASK);
   3276  1.1  riastrad 	reg |= (WEIGHT_XBR_0(ni_pi->cac_weights->weight_xbr_0) |
   3277  1.1  riastrad 		WEIGHT_XBR_1(ni_pi->cac_weights->weight_xbr_1) |
   3278  1.1  riastrad 		WEIGHT_XBR_2(ni_pi->cac_weights->weight_xbr_2) |
   3279  1.1  riastrad 		WEIGHT_SPI_SIG0(ni_pi->cac_weights->weight_spi_sig0));
   3280  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_3_WEIGHT_0, reg);
   3281  1.1  riastrad 
   3282  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_3_WEIGHT_1) & ~(WEIGHT_SPI_SIG1_MASK |
   3283  1.1  riastrad 						      WEIGHT_SPI_SIG2_MASK |
   3284  1.1  riastrad 						      WEIGHT_SPI_SIG3_MASK |
   3285  1.1  riastrad 						      WEIGHT_SPI_SIG4_MASK |
   3286  1.1  riastrad 						      WEIGHT_SPI_SIG5_MASK);
   3287  1.1  riastrad 	reg |= (WEIGHT_SPI_SIG1(ni_pi->cac_weights->weight_spi_sig1) |
   3288  1.1  riastrad 		WEIGHT_SPI_SIG2(ni_pi->cac_weights->weight_spi_sig2) |
   3289  1.1  riastrad 		WEIGHT_SPI_SIG3(ni_pi->cac_weights->weight_spi_sig3) |
   3290  1.1  riastrad 		WEIGHT_SPI_SIG4(ni_pi->cac_weights->weight_spi_sig4) |
   3291  1.1  riastrad 		WEIGHT_SPI_SIG5(ni_pi->cac_weights->weight_spi_sig5));
   3292  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_3_WEIGHT_1, reg);
   3293  1.1  riastrad 
   3294  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_4_WEIGHT_0) & ~(WEIGHT_LDS_SIG0_MASK |
   3295  1.1  riastrad 						      WEIGHT_LDS_SIG1_MASK |
   3296  1.1  riastrad 						      WEIGHT_SC_MASK);
   3297  1.1  riastrad 	reg |= (WEIGHT_LDS_SIG0(ni_pi->cac_weights->weight_lds_sig0) |
   3298  1.1  riastrad 		WEIGHT_LDS_SIG1(ni_pi->cac_weights->weight_lds_sig1) |
   3299  1.1  riastrad 		WEIGHT_SC(ni_pi->cac_weights->weight_sc));
   3300  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_4_WEIGHT_0, reg);
   3301  1.1  riastrad 
   3302  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_4_WEIGHT_1) & ~(WEIGHT_BIF_MASK |
   3303  1.1  riastrad 						      WEIGHT_CP_MASK |
   3304  1.1  riastrad 						      WEIGHT_PA_SIG0_MASK |
   3305  1.1  riastrad 						      WEIGHT_PA_SIG1_MASK |
   3306  1.1  riastrad 						      WEIGHT_VGT_SIG0_MASK);
   3307  1.1  riastrad 	reg |= (WEIGHT_BIF(ni_pi->cac_weights->weight_bif) |
   3308  1.1  riastrad 		WEIGHT_CP(ni_pi->cac_weights->weight_cp) |
   3309  1.1  riastrad 		WEIGHT_PA_SIG0(ni_pi->cac_weights->weight_pa_sig0) |
   3310  1.1  riastrad 		WEIGHT_PA_SIG1(ni_pi->cac_weights->weight_pa_sig1) |
   3311  1.1  riastrad 		WEIGHT_VGT_SIG0(ni_pi->cac_weights->weight_vgt_sig0));
   3312  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_4_WEIGHT_1, reg);
   3313  1.1  riastrad 
   3314  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_4_WEIGHT_2) & ~(WEIGHT_VGT_SIG1_MASK |
   3315  1.1  riastrad 						      WEIGHT_VGT_SIG2_MASK |
   3316  1.1  riastrad 						      WEIGHT_DC_SIG0_MASK |
   3317  1.1  riastrad 						      WEIGHT_DC_SIG1_MASK |
   3318  1.1  riastrad 						      WEIGHT_DC_SIG2_MASK);
   3319  1.1  riastrad 	reg |= (WEIGHT_VGT_SIG1(ni_pi->cac_weights->weight_vgt_sig1) |
   3320  1.1  riastrad 		WEIGHT_VGT_SIG2(ni_pi->cac_weights->weight_vgt_sig2) |
   3321  1.1  riastrad 		WEIGHT_DC_SIG0(ni_pi->cac_weights->weight_dc_sig0) |
   3322  1.1  riastrad 		WEIGHT_DC_SIG1(ni_pi->cac_weights->weight_dc_sig1) |
   3323  1.1  riastrad 		WEIGHT_DC_SIG2(ni_pi->cac_weights->weight_dc_sig2));
   3324  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_4_WEIGHT_2, reg);
   3325  1.1  riastrad 
   3326  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_4_WEIGHT_3) & ~(WEIGHT_DC_SIG3_MASK |
   3327  1.1  riastrad 						      WEIGHT_UVD_SIG0_MASK |
   3328  1.1  riastrad 						      WEIGHT_UVD_SIG1_MASK |
   3329  1.1  riastrad 						      WEIGHT_SPARE0_MASK |
   3330  1.1  riastrad 						      WEIGHT_SPARE1_MASK);
   3331  1.1  riastrad 	reg |= (WEIGHT_DC_SIG3(ni_pi->cac_weights->weight_dc_sig3) |
   3332  1.1  riastrad 		WEIGHT_UVD_SIG0(ni_pi->cac_weights->weight_uvd_sig0) |
   3333  1.1  riastrad 		WEIGHT_UVD_SIG1(ni_pi->cac_weights->weight_uvd_sig1) |
   3334  1.1  riastrad 		WEIGHT_SPARE0(ni_pi->cac_weights->weight_spare0) |
   3335  1.1  riastrad 		WEIGHT_SPARE1(ni_pi->cac_weights->weight_spare1));
   3336  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_4_WEIGHT_3, reg);
   3337  1.1  riastrad 
   3338  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_5_WEIGHT_0) & ~(WEIGHT_SQ_VSP_MASK |
   3339  1.1  riastrad 						      WEIGHT_SQ_VSP0_MASK);
   3340  1.1  riastrad 	reg |= (WEIGHT_SQ_VSP(ni_pi->cac_weights->weight_sq_vsp) |
   3341  1.1  riastrad 		WEIGHT_SQ_VSP0(ni_pi->cac_weights->weight_sq_vsp0));
   3342  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_5_WEIGHT_0, reg);
   3343  1.1  riastrad 
   3344  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_5_WEIGHT_1) & ~(WEIGHT_SQ_GPR_MASK);
   3345  1.1  riastrad 	reg |= WEIGHT_SQ_GPR(ni_pi->cac_weights->weight_sq_gpr);
   3346  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_5_WEIGHT_1, reg);
   3347  1.1  riastrad 
   3348  1.1  riastrad 	reg = RREG32_CG(CG_CAC_REGION_4_OVERRIDE_4) & ~(OVR_MODE_SPARE_0_MASK |
   3349  1.1  riastrad 							OVR_VAL_SPARE_0_MASK |
   3350  1.1  riastrad 							OVR_MODE_SPARE_1_MASK |
   3351  1.1  riastrad 							OVR_VAL_SPARE_1_MASK);
   3352  1.1  riastrad 	reg |= (OVR_MODE_SPARE_0(ni_pi->cac_weights->ovr_mode_spare_0) |
   3353  1.1  riastrad 		OVR_VAL_SPARE_0(ni_pi->cac_weights->ovr_val_spare_0) |
   3354  1.1  riastrad 		OVR_MODE_SPARE_1(ni_pi->cac_weights->ovr_mode_spare_1) |
   3355  1.1  riastrad 		OVR_VAL_SPARE_1(ni_pi->cac_weights->ovr_val_spare_1));
   3356  1.1  riastrad 	WREG32_CG(CG_CAC_REGION_4_OVERRIDE_4, reg);
   3357  1.1  riastrad 
   3358  1.1  riastrad 	reg = RREG32(SQ_CAC_THRESHOLD) & ~(VSP_MASK |
   3359  1.1  riastrad 					   VSP0_MASK |
   3360  1.1  riastrad 					   GPR_MASK);
   3361  1.1  riastrad 	reg |= (VSP(ni_pi->cac_weights->vsp) |
   3362  1.1  riastrad 		VSP0(ni_pi->cac_weights->vsp0) |
   3363  1.1  riastrad 		GPR(ni_pi->cac_weights->gpr));
   3364  1.1  riastrad 	WREG32(SQ_CAC_THRESHOLD, reg);
   3365  1.1  riastrad 
   3366  1.1  riastrad 	reg = (MCDW_WR_ENABLE |
   3367  1.1  riastrad 	       MCDX_WR_ENABLE |
   3368  1.1  riastrad 	       MCDY_WR_ENABLE |
   3369  1.1  riastrad 	       MCDZ_WR_ENABLE |
   3370  1.1  riastrad 	       INDEX(0x09D4));
   3371  1.1  riastrad 	WREG32(MC_CG_CONFIG, reg);
   3372  1.1  riastrad 
   3373  1.1  riastrad 	reg = (READ_WEIGHT(ni_pi->cac_weights->mc_read_weight) |
   3374  1.1  riastrad 	       WRITE_WEIGHT(ni_pi->cac_weights->mc_write_weight) |
   3375  1.1  riastrad 	       ALLOW_OVERFLOW);
   3376  1.1  riastrad 	WREG32(MC_CG_DATAPORT, reg);
   3377  1.1  riastrad 
   3378  1.1  riastrad 	return 0;
   3379  1.1  riastrad }
   3380  1.1  riastrad 
   3381  1.1  riastrad static int ni_enable_smc_cac(struct radeon_device *rdev,
   3382  1.1  riastrad 			     struct radeon_ps *radeon_new_state,
   3383  1.1  riastrad 			     bool enable)
   3384  1.1  riastrad {
   3385  1.1  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3386  1.1  riastrad 	int ret = 0;
   3387  1.1  riastrad 	PPSMC_Result smc_result;
   3388  1.1  riastrad 
   3389  1.1  riastrad 	if (ni_pi->enable_cac) {
   3390  1.1  riastrad 		if (enable) {
   3391  1.1  riastrad 			if (!r600_is_uvd_state(radeon_new_state->class, radeon_new_state->class2)) {
   3392  1.1  riastrad 				smc_result = rv770_send_msg_to_smc(rdev, PPSMC_MSG_CollectCAC_PowerCorreln);
   3393  1.1  riastrad 
   3394  1.1  riastrad 				if (ni_pi->support_cac_long_term_average) {
   3395  1.1  riastrad 					smc_result = rv770_send_msg_to_smc(rdev, PPSMC_CACLongTermAvgEnable);
   3396  1.1  riastrad 					if (PPSMC_Result_OK != smc_result)
   3397  1.1  riastrad 						ni_pi->support_cac_long_term_average = false;
   3398  1.1  riastrad 				}
   3399  1.1  riastrad 
   3400  1.1  riastrad 				smc_result = rv770_send_msg_to_smc(rdev, PPSMC_MSG_EnableCac);
   3401  1.1  riastrad 				if (PPSMC_Result_OK != smc_result)
   3402  1.1  riastrad 					ret = -EINVAL;
   3403  1.1  riastrad 
   3404  1.1  riastrad 				ni_pi->cac_enabled = (PPSMC_Result_OK == smc_result) ? true : false;
   3405  1.1  riastrad 			}
   3406  1.1  riastrad 		} else if (ni_pi->cac_enabled) {
   3407  1.1  riastrad 			smc_result = rv770_send_msg_to_smc(rdev, PPSMC_MSG_DisableCac);
   3408  1.1  riastrad 
   3409  1.1  riastrad 			ni_pi->cac_enabled = false;
   3410  1.1  riastrad 
   3411  1.1  riastrad 			if (ni_pi->support_cac_long_term_average) {
   3412  1.1  riastrad 				smc_result = rv770_send_msg_to_smc(rdev, PPSMC_CACLongTermAvgDisable);
   3413  1.1  riastrad 				if (PPSMC_Result_OK != smc_result)
   3414  1.1  riastrad 					ni_pi->support_cac_long_term_average = false;
   3415  1.1  riastrad 			}
   3416  1.1  riastrad 		}
   3417  1.1  riastrad 	}
   3418  1.1  riastrad 
   3419  1.1  riastrad 	return ret;
   3420  1.1  riastrad }
   3421  1.1  riastrad 
   3422  1.1  riastrad static int ni_pcie_performance_request(struct radeon_device *rdev,
   3423  1.1  riastrad 				       u8 perf_req, bool advertise)
   3424  1.1  riastrad {
   3425  1.1  riastrad #if defined(CONFIG_ACPI)
   3426  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3427  1.1  riastrad 
   3428  1.1  riastrad 	if ((perf_req == PCIE_PERF_REQ_PECI_GEN1) ||
   3429  1.2  riastrad 	    (perf_req == PCIE_PERF_REQ_PECI_GEN2)) {
   3430  1.1  riastrad 		if (eg_pi->pcie_performance_request_registered == false)
   3431  1.1  riastrad 			radeon_acpi_pcie_notify_device_ready(rdev);
   3432  1.1  riastrad 		eg_pi->pcie_performance_request_registered = true;
   3433  1.1  riastrad 		return radeon_acpi_pcie_performance_request(rdev, perf_req, advertise);
   3434  1.1  riastrad 	} else if ((perf_req == PCIE_PERF_REQ_REMOVE_REGISTRY) &&
   3435  1.2  riastrad 		    eg_pi->pcie_performance_request_registered) {
   3436  1.1  riastrad 		eg_pi->pcie_performance_request_registered = false;
   3437  1.1  riastrad 		return radeon_acpi_pcie_performance_request(rdev, perf_req, advertise);
   3438  1.1  riastrad 	}
   3439  1.1  riastrad #endif
   3440  1.1  riastrad 	return 0;
   3441  1.1  riastrad }
   3442  1.1  riastrad 
   3443  1.1  riastrad static int ni_advertise_gen2_capability(struct radeon_device *rdev)
   3444  1.1  riastrad {
   3445  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3446  1.1  riastrad 	u32 tmp;
   3447  1.1  riastrad 
   3448  1.2  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
   3449  1.1  riastrad 
   3450  1.2  riastrad 	if ((tmp & LC_OTHER_SIDE_EVER_SENT_GEN2) &&
   3451  1.2  riastrad 	    (tmp & LC_OTHER_SIDE_SUPPORTS_GEN2))
   3452  1.2  riastrad 		pi->pcie_gen2 = true;
   3453  1.2  riastrad 	else
   3454  1.1  riastrad 		pi->pcie_gen2 = false;
   3455  1.1  riastrad 
   3456  1.1  riastrad 	if (!pi->pcie_gen2)
   3457  1.1  riastrad 		ni_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN2, true);
   3458  1.1  riastrad 
   3459  1.1  riastrad 	return 0;
   3460  1.1  riastrad }
   3461  1.1  riastrad 
   3462  1.1  riastrad static void ni_enable_bif_dynamic_pcie_gen2(struct radeon_device *rdev,
   3463  1.1  riastrad 					    bool enable)
   3464  1.1  riastrad {
   3465  1.2  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3466  1.2  riastrad 	u32 tmp, bif;
   3467  1.1  riastrad 
   3468  1.1  riastrad 	tmp = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL);
   3469  1.1  riastrad 
   3470  1.1  riastrad 	if ((tmp & LC_OTHER_SIDE_EVER_SENT_GEN2) &&
   3471  1.1  riastrad 	    (tmp & LC_OTHER_SIDE_SUPPORTS_GEN2)) {
   3472  1.1  riastrad 		if (enable) {
   3473  1.1  riastrad 			if (!pi->boot_in_gen2) {
   3474  1.1  riastrad 				bif = RREG32(CG_BIF_REQ_AND_RSP) & ~CG_CLIENT_REQ_MASK;
   3475  1.1  riastrad 				bif |= CG_CLIENT_REQ(0xd);
   3476  1.1  riastrad 				WREG32(CG_BIF_REQ_AND_RSP, bif);
   3477  1.1  riastrad 			}
   3478  1.1  riastrad 			tmp &= ~LC_HW_VOLTAGE_IF_CONTROL_MASK;
   3479  1.1  riastrad 			tmp |= LC_HW_VOLTAGE_IF_CONTROL(1);
   3480  1.1  riastrad 			tmp |= LC_GEN2_EN_STRAP;
   3481  1.1  riastrad 
   3482  1.1  riastrad 			tmp |= LC_CLR_FAILED_SPD_CHANGE_CNT;
   3483  1.1  riastrad 			WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
   3484  1.1  riastrad 			udelay(10);
   3485  1.1  riastrad 			tmp &= ~LC_CLR_FAILED_SPD_CHANGE_CNT;
   3486  1.1  riastrad 			WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
   3487  1.1  riastrad 		} else {
   3488  1.1  riastrad 			if (!pi->boot_in_gen2) {
   3489  1.1  riastrad 				bif = RREG32(CG_BIF_REQ_AND_RSP) & ~CG_CLIENT_REQ_MASK;
   3490  1.1  riastrad 				bif |= CG_CLIENT_REQ(0xd);
   3491  1.1  riastrad 				WREG32(CG_BIF_REQ_AND_RSP, bif);
   3492  1.1  riastrad 
   3493  1.1  riastrad 				tmp &= ~LC_HW_VOLTAGE_IF_CONTROL_MASK;
   3494  1.1  riastrad 				tmp &= ~LC_GEN2_EN_STRAP;
   3495  1.1  riastrad 			}
   3496  1.1  riastrad 			WREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL, tmp);
   3497  1.1  riastrad 		}
   3498  1.1  riastrad 	}
   3499  1.1  riastrad }
   3500  1.1  riastrad 
   3501  1.1  riastrad static void ni_enable_dynamic_pcie_gen2(struct radeon_device *rdev,
   3502  1.1  riastrad 					bool enable)
   3503  1.1  riastrad {
   3504  1.1  riastrad 	ni_enable_bif_dynamic_pcie_gen2(rdev, enable);
   3505  1.1  riastrad 
   3506  1.1  riastrad 	if (enable)
   3507  1.1  riastrad 		WREG32_P(GENERAL_PWRMGT, ENABLE_GEN2PCIE, ~ENABLE_GEN2PCIE);
   3508  1.1  riastrad 	else
   3509  1.2  riastrad 		WREG32_P(GENERAL_PWRMGT, 0, ~ENABLE_GEN2PCIE);
   3510  1.1  riastrad }
   3511  1.1  riastrad 
   3512  1.1  riastrad void ni_set_uvd_clock_before_set_eng_clock(struct radeon_device *rdev,
   3513  1.1  riastrad 					   struct radeon_ps *new_ps,
   3514  1.1  riastrad 					   struct radeon_ps *old_ps)
   3515  1.1  riastrad {
   3516  1.1  riastrad 	struct ni_ps *new_state = ni_get_ps(new_ps);
   3517  1.1  riastrad 	struct ni_ps *current_state = ni_get_ps(old_ps);
   3518  1.1  riastrad 
   3519  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   3520  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   3521  1.1  riastrad 		return;
   3522  1.1  riastrad 
   3523  1.1  riastrad 	if (new_state->performance_levels[new_state->performance_level_count - 1].sclk >=
   3524  1.1  riastrad 	    current_state->performance_levels[current_state->performance_level_count - 1].sclk)
   3525  1.1  riastrad 		return;
   3526  1.1  riastrad 
   3527  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   3528  1.1  riastrad }
   3529  1.1  riastrad 
   3530  1.1  riastrad void ni_set_uvd_clock_after_set_eng_clock(struct radeon_device *rdev,
   3531  1.1  riastrad 					  struct radeon_ps *new_ps,
   3532  1.1  riastrad 					  struct radeon_ps *old_ps)
   3533  1.1  riastrad {
   3534  1.1  riastrad 	struct ni_ps *new_state = ni_get_ps(new_ps);
   3535  1.1  riastrad 	struct ni_ps *current_state = ni_get_ps(old_ps);
   3536  1.1  riastrad 
   3537  1.1  riastrad 	if ((new_ps->vclk == old_ps->vclk) &&
   3538  1.1  riastrad 	    (new_ps->dclk == old_ps->dclk))
   3539  1.1  riastrad 		return;
   3540  1.1  riastrad 
   3541  1.1  riastrad 	if (new_state->performance_levels[new_state->performance_level_count - 1].sclk <
   3542  1.1  riastrad 	    current_state->performance_levels[current_state->performance_level_count - 1].sclk)
   3543  1.1  riastrad 		return;
   3544  1.1  riastrad 
   3545  1.1  riastrad 	radeon_set_uvd_clocks(rdev, new_ps->vclk, new_ps->dclk);
   3546  1.1  riastrad }
   3547  1.1  riastrad 
   3548  1.1  riastrad void ni_dpm_setup_asic(struct radeon_device *rdev)
   3549  1.1  riastrad {
   3550  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3551  1.1  riastrad 	int r;
   3552  1.1  riastrad 
   3553  1.1  riastrad 	r = ni_mc_load_microcode(rdev);
   3554  1.1  riastrad 	if (r)
   3555  1.1  riastrad 		DRM_ERROR("Failed to load MC firmware!\n");
   3556  1.1  riastrad 	ni_read_clock_registers(rdev);
   3557  1.1  riastrad 	btc_read_arb_registers(rdev);
   3558  1.1  riastrad 	rv770_get_memory_type(rdev);
   3559  1.1  riastrad 	if (eg_pi->pcie_performance_request)
   3560  1.1  riastrad 		ni_advertise_gen2_capability(rdev);
   3561  1.1  riastrad 	rv770_get_pcie_gen2_status(rdev);
   3562  1.1  riastrad 	rv770_enable_acpi_pm(rdev);
   3563  1.1  riastrad }
   3564  1.1  riastrad 
   3565  1.1  riastrad void ni_update_current_ps(struct radeon_device *rdev,
   3566  1.1  riastrad 			  struct radeon_ps *rps)
   3567  1.1  riastrad {
   3568  1.1  riastrad 	struct ni_ps *new_ps = ni_get_ps(rps);
   3569  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3570  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3571  1.1  riastrad 
   3572  1.1  riastrad 	eg_pi->current_rps = *rps;
   3573  1.1  riastrad 	ni_pi->current_ps = *new_ps;
   3574  1.1  riastrad 	eg_pi->current_rps.ps_priv = &ni_pi->current_ps;
   3575  1.1  riastrad }
   3576  1.1  riastrad 
   3577  1.1  riastrad void ni_update_requested_ps(struct radeon_device *rdev,
   3578  1.1  riastrad 			    struct radeon_ps *rps)
   3579  1.1  riastrad {
   3580  1.1  riastrad 	struct ni_ps *new_ps = ni_get_ps(rps);
   3581  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3582  1.2  riastrad 	struct ni_power_info *ni_pi = ni_get_pi(rdev);
   3583  1.1  riastrad 
   3584  1.1  riastrad 	eg_pi->requested_rps = *rps;
   3585  1.1  riastrad 	ni_pi->requested_ps = *new_ps;
   3586  1.1  riastrad 	eg_pi->requested_rps.ps_priv = &ni_pi->requested_ps;
   3587  1.1  riastrad }
   3588  1.1  riastrad 
   3589  1.1  riastrad int ni_dpm_enable(struct radeon_device *rdev)
   3590  1.1  riastrad {
   3591  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3592  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3593  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   3594  1.1  riastrad 	int ret;
   3595  1.1  riastrad 
   3596  1.1  riastrad 	if (pi->gfx_clock_gating)
   3597  1.1  riastrad 		ni_cg_clockgating_default(rdev);
   3598  1.2  riastrad 	if (btc_dpm_enabled(rdev))
   3599  1.2  riastrad 		return -EINVAL;
   3600  1.1  riastrad 	if (pi->mg_clock_gating)
   3601  1.1  riastrad 		ni_mg_clockgating_default(rdev);
   3602  1.1  riastrad 	if (eg_pi->ls_clock_gating)
   3603  1.1  riastrad 		ni_ls_clockgating_default(rdev);
   3604  1.1  riastrad 	if (pi->voltage_control) {
   3605  1.1  riastrad 		rv770_enable_voltage_control(rdev, true);
   3606  1.1  riastrad 		ret = cypress_construct_voltage_tables(rdev);
   3607  1.1  riastrad 		if (ret) {
   3608  1.1  riastrad 			DRM_ERROR("cypress_construct_voltage_tables failed\n");
   3609  1.1  riastrad 			return ret;
   3610  1.1  riastrad 		}
   3611  1.1  riastrad 	}
   3612  1.1  riastrad 	if (eg_pi->dynamic_ac_timing) {
   3613  1.1  riastrad 		ret = ni_initialize_mc_reg_table(rdev);
   3614  1.1  riastrad 		if (ret)
   3615  1.1  riastrad 			eg_pi->dynamic_ac_timing = false;
   3616  1.1  riastrad 	}
   3617  1.1  riastrad 	if (pi->dynamic_ss)
   3618  1.1  riastrad 		cypress_enable_spread_spectrum(rdev, true);
   3619  1.1  riastrad 	if (pi->thermal_protection)
   3620  1.1  riastrad 		rv770_enable_thermal_protection(rdev, true);
   3621  1.1  riastrad 	rv770_setup_bsp(rdev);
   3622  1.1  riastrad 	rv770_program_git(rdev);
   3623  1.1  riastrad 	rv770_program_tp(rdev);
   3624  1.1  riastrad 	rv770_program_tpp(rdev);
   3625  1.1  riastrad 	rv770_program_sstp(rdev);
   3626  1.1  riastrad 	cypress_enable_display_gap(rdev);
   3627  1.1  riastrad 	rv770_program_vc(rdev);
   3628  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   3629  1.1  riastrad 		ni_enable_dynamic_pcie_gen2(rdev, true);
   3630  1.1  riastrad 	ret = rv770_upload_firmware(rdev);
   3631  1.1  riastrad 	if (ret) {
   3632  1.1  riastrad 		DRM_ERROR("rv770_upload_firmware failed\n");
   3633  1.1  riastrad 		return ret;
   3634  1.1  riastrad 	}
   3635  1.1  riastrad 	ret = ni_process_firmware_header(rdev);
   3636  1.1  riastrad 	if (ret) {
   3637  1.1  riastrad 		DRM_ERROR("ni_process_firmware_header failed\n");
   3638  1.1  riastrad 		return ret;
   3639  1.1  riastrad 	}
   3640  1.1  riastrad 	ret = ni_initial_switch_from_arb_f0_to_f1(rdev);
   3641  1.1  riastrad 	if (ret) {
   3642  1.1  riastrad 		DRM_ERROR("ni_initial_switch_from_arb_f0_to_f1 failed\n");
   3643  1.1  riastrad 		return ret;
   3644  1.1  riastrad 	}
   3645  1.1  riastrad 	ret = ni_init_smc_table(rdev);
   3646  1.1  riastrad 	if (ret) {
   3647  1.1  riastrad 		DRM_ERROR("ni_init_smc_table failed\n");
   3648  1.1  riastrad 		return ret;
   3649  1.1  riastrad 	}
   3650  1.1  riastrad 	ret = ni_init_smc_spll_table(rdev);
   3651  1.1  riastrad 	if (ret) {
   3652  1.1  riastrad 		DRM_ERROR("ni_init_smc_spll_table failed\n");
   3653  1.1  riastrad 		return ret;
   3654  1.1  riastrad 	}
   3655  1.1  riastrad 	ret = ni_init_arb_table_index(rdev);
   3656  1.1  riastrad 	if (ret) {
   3657  1.1  riastrad 		DRM_ERROR("ni_init_arb_table_index failed\n");
   3658  1.1  riastrad 		return ret;
   3659  1.1  riastrad 	}
   3660  1.1  riastrad 	if (eg_pi->dynamic_ac_timing) {
   3661  1.1  riastrad 		ret = ni_populate_mc_reg_table(rdev, boot_ps);
   3662  1.1  riastrad 		if (ret) {
   3663  1.1  riastrad 			DRM_ERROR("ni_populate_mc_reg_table failed\n");
   3664  1.1  riastrad 			return ret;
   3665  1.1  riastrad 		}
   3666  1.1  riastrad 	}
   3667  1.1  riastrad 	ret = ni_initialize_smc_cac_tables(rdev);
   3668  1.1  riastrad 	if (ret) {
   3669  1.1  riastrad 		DRM_ERROR("ni_initialize_smc_cac_tables failed\n");
   3670  1.1  riastrad 		return ret;
   3671  1.1  riastrad 	}
   3672  1.1  riastrad 	ret = ni_initialize_hardware_cac_manager(rdev);
   3673  1.1  riastrad 	if (ret) {
   3674  1.1  riastrad 		DRM_ERROR("ni_initialize_hardware_cac_manager failed\n");
   3675  1.1  riastrad 		return ret;
   3676  1.1  riastrad 	}
   3677  1.1  riastrad 	ret = ni_populate_smc_tdp_limits(rdev, boot_ps);
   3678  1.1  riastrad 	if (ret) {
   3679  1.1  riastrad 		DRM_ERROR("ni_populate_smc_tdp_limits failed\n");
   3680  1.1  riastrad 		return ret;
   3681  1.1  riastrad 	}
   3682  1.1  riastrad 	ni_program_response_times(rdev);
   3683  1.1  riastrad 	r7xx_start_smc(rdev);
   3684  1.1  riastrad 	ret = cypress_notify_smc_display_change(rdev, false);
   3685  1.1  riastrad 	if (ret) {
   3686  1.1  riastrad 		DRM_ERROR("cypress_notify_smc_display_change failed\n");
   3687  1.1  riastrad 		return ret;
   3688  1.1  riastrad 	}
   3689  1.1  riastrad 	cypress_enable_sclk_control(rdev, true);
   3690  1.1  riastrad 	if (eg_pi->memory_transition)
   3691  1.1  riastrad 		cypress_enable_mclk_control(rdev, true);
   3692  1.1  riastrad 	cypress_start_dpm(rdev);
   3693  1.1  riastrad 	if (pi->gfx_clock_gating)
   3694  1.1  riastrad 		ni_gfx_clockgating_enable(rdev, true);
   3695  1.1  riastrad 	if (pi->mg_clock_gating)
   3696  1.1  riastrad 		ni_mg_clockgating_enable(rdev, true);
   3697  1.1  riastrad 	if (eg_pi->ls_clock_gating)
   3698  1.1  riastrad 		ni_ls_clockgating_enable(rdev, true);
   3699  1.1  riastrad 
   3700  1.1  riastrad 	rv770_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
   3701  1.1  riastrad 
   3702  1.1  riastrad 	ni_update_current_ps(rdev, boot_ps);
   3703  1.1  riastrad 
   3704  1.1  riastrad 	return 0;
   3705  1.1  riastrad }
   3706  1.1  riastrad 
   3707  1.1  riastrad void ni_dpm_disable(struct radeon_device *rdev)
   3708  1.1  riastrad {
   3709  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3710  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3711  1.1  riastrad 	struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
   3712  1.1  riastrad 
   3713  1.1  riastrad 	if (!btc_dpm_enabled(rdev))
   3714  1.1  riastrad 		return;
   3715  1.1  riastrad 	rv770_clear_vc(rdev);
   3716  1.1  riastrad 	if (pi->thermal_protection)
   3717  1.1  riastrad 		rv770_enable_thermal_protection(rdev, false);
   3718  1.1  riastrad 	ni_enable_power_containment(rdev, boot_ps, false);
   3719  1.1  riastrad 	ni_enable_smc_cac(rdev, boot_ps, false);
   3720  1.1  riastrad 	cypress_enable_spread_spectrum(rdev, false);
   3721  1.1  riastrad 	rv770_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, false);
   3722  1.1  riastrad 	if (pi->dynamic_pcie_gen2)
   3723  1.1  riastrad 		ni_enable_dynamic_pcie_gen2(rdev, false);
   3724  1.1  riastrad 
   3725  1.1  riastrad 	if (rdev->irq.installed &&
   3726  1.1  riastrad 	    r600_is_internal_thermal_sensor(rdev->pm.int_thermal_type)) {
   3727  1.1  riastrad 		rdev->irq.dpm_thermal = false;
   3728  1.1  riastrad 		radeon_irq_set(rdev);
   3729  1.1  riastrad 	}
   3730  1.1  riastrad 
   3731  1.1  riastrad 	if (pi->gfx_clock_gating)
   3732  1.1  riastrad 		ni_gfx_clockgating_enable(rdev, false);
   3733  1.1  riastrad 	if (pi->mg_clock_gating)
   3734  1.1  riastrad 		ni_mg_clockgating_enable(rdev, false);
   3735  1.1  riastrad 	if (eg_pi->ls_clock_gating)
   3736  1.1  riastrad 		ni_ls_clockgating_enable(rdev, false);
   3737  1.1  riastrad 	ni_stop_dpm(rdev);
   3738  1.1  riastrad 	btc_reset_to_default(rdev);
   3739  1.1  riastrad 	ni_stop_smc(rdev);
   3740  1.1  riastrad 	ni_force_switch_to_arb_f0(rdev);
   3741  1.1  riastrad 
   3742  1.1  riastrad 	ni_update_current_ps(rdev, boot_ps);
   3743  1.1  riastrad }
   3744  1.1  riastrad 
   3745  1.1  riastrad static int ni_power_control_set_level(struct radeon_device *rdev)
   3746  1.1  riastrad {
   3747  1.1  riastrad 	struct radeon_ps *new_ps = rdev->pm.dpm.requested_ps;
   3748  1.1  riastrad 	int ret;
   3749  1.1  riastrad 
   3750  1.1  riastrad 	ret = ni_restrict_performance_levels_before_switch(rdev);
   3751  1.1  riastrad 	if (ret)
   3752  1.1  riastrad 		return ret;
   3753  1.1  riastrad 	ret = rv770_halt_smc(rdev);
   3754  1.1  riastrad 	if (ret)
   3755  1.1  riastrad 		return ret;
   3756  1.1  riastrad 	ret = ni_populate_smc_tdp_limits(rdev, new_ps);
   3757  1.1  riastrad 	if (ret)
   3758  1.1  riastrad 		return ret;
   3759  1.1  riastrad 	ret = rv770_resume_smc(rdev);
   3760  1.1  riastrad 	if (ret)
   3761  1.1  riastrad 		return ret;
   3762  1.1  riastrad 	ret = rv770_set_sw_state(rdev);
   3763  1.1  riastrad 	if (ret)
   3764  1.1  riastrad 		return ret;
   3765  1.1  riastrad 
   3766  1.1  riastrad 	return 0;
   3767  1.1  riastrad }
   3768  1.1  riastrad 
   3769  1.1  riastrad int ni_dpm_pre_set_power_state(struct radeon_device *rdev)
   3770  1.1  riastrad {
   3771  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3772  1.1  riastrad 	struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
   3773  1.1  riastrad 	struct radeon_ps *new_ps = &requested_ps;
   3774  1.1  riastrad 
   3775  1.1  riastrad 	ni_update_requested_ps(rdev, new_ps);
   3776  1.1  riastrad 
   3777  1.1  riastrad 	ni_apply_state_adjust_rules(rdev, &eg_pi->requested_rps);
   3778  1.1  riastrad 
   3779  1.1  riastrad 	return 0;
   3780  1.1  riastrad }
   3781  1.1  riastrad 
   3782  1.1  riastrad int ni_dpm_set_power_state(struct radeon_device *rdev)
   3783  1.1  riastrad {
   3784  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3785  1.1  riastrad 	struct radeon_ps *new_ps = &eg_pi->requested_rps;
   3786  1.1  riastrad 	struct radeon_ps *old_ps = &eg_pi->current_rps;
   3787  1.1  riastrad 	int ret;
   3788  1.1  riastrad 
   3789  1.1  riastrad 	ret = ni_restrict_performance_levels_before_switch(rdev);
   3790  1.1  riastrad 	if (ret) {
   3791  1.1  riastrad 		DRM_ERROR("ni_restrict_performance_levels_before_switch failed\n");
   3792  1.1  riastrad 		return ret;
   3793  1.1  riastrad 	}
   3794  1.1  riastrad 	ni_set_uvd_clock_before_set_eng_clock(rdev, new_ps, old_ps);
   3795  1.1  riastrad 	ret = ni_enable_power_containment(rdev, new_ps, false);
   3796  1.1  riastrad 	if (ret) {
   3797  1.1  riastrad 		DRM_ERROR("ni_enable_power_containment failed\n");
   3798  1.1  riastrad 		return ret;
   3799  1.1  riastrad 	}
   3800  1.1  riastrad 	ret = ni_enable_smc_cac(rdev, new_ps, false);
   3801  1.1  riastrad 	if (ret) {
   3802  1.1  riastrad 		DRM_ERROR("ni_enable_smc_cac failed\n");
   3803  1.1  riastrad 		return ret;
   3804  1.1  riastrad 	}
   3805  1.1  riastrad 	ret = rv770_halt_smc(rdev);
   3806  1.1  riastrad 	if (ret) {
   3807  1.1  riastrad 		DRM_ERROR("rv770_halt_smc failed\n");
   3808  1.1  riastrad 		return ret;
   3809  1.1  riastrad 	}
   3810  1.1  riastrad 	if (eg_pi->smu_uvd_hs)
   3811  1.1  riastrad 		btc_notify_uvd_to_smc(rdev, new_ps);
   3812  1.1  riastrad 	ret = ni_upload_sw_state(rdev, new_ps);
   3813  1.1  riastrad 	if (ret) {
   3814  1.1  riastrad 		DRM_ERROR("ni_upload_sw_state failed\n");
   3815  1.1  riastrad 		return ret;
   3816  1.1  riastrad 	}
   3817  1.1  riastrad 	if (eg_pi->dynamic_ac_timing) {
   3818  1.1  riastrad 		ret = ni_upload_mc_reg_table(rdev, new_ps);
   3819  1.1  riastrad 		if (ret) {
   3820  1.1  riastrad 			DRM_ERROR("ni_upload_mc_reg_table failed\n");
   3821  1.1  riastrad 			return ret;
   3822  1.1  riastrad 		}
   3823  1.1  riastrad 	}
   3824  1.1  riastrad 	ret = ni_program_memory_timing_parameters(rdev, new_ps);
   3825  1.1  riastrad 	if (ret) {
   3826  1.1  riastrad 		DRM_ERROR("ni_program_memory_timing_parameters failed\n");
   3827  1.1  riastrad 		return ret;
   3828  1.1  riastrad 	}
   3829  1.1  riastrad 	ret = rv770_resume_smc(rdev);
   3830  1.1  riastrad 	if (ret) {
   3831  1.1  riastrad 		DRM_ERROR("rv770_resume_smc failed\n");
   3832  1.1  riastrad 		return ret;
   3833  1.1  riastrad 	}
   3834  1.1  riastrad 	ret = rv770_set_sw_state(rdev);
   3835  1.1  riastrad 	if (ret) {
   3836  1.1  riastrad 		DRM_ERROR("rv770_set_sw_state failed\n");
   3837  1.1  riastrad 		return ret;
   3838  1.1  riastrad 	}
   3839  1.1  riastrad 	ni_set_uvd_clock_after_set_eng_clock(rdev, new_ps, old_ps);
   3840  1.1  riastrad 	ret = ni_enable_smc_cac(rdev, new_ps, true);
   3841  1.1  riastrad 	if (ret) {
   3842  1.1  riastrad 		DRM_ERROR("ni_enable_smc_cac failed\n");
   3843  1.1  riastrad 		return ret;
   3844  1.1  riastrad 	}
   3845  1.1  riastrad 	ret = ni_enable_power_containment(rdev, new_ps, true);
   3846  1.1  riastrad 	if (ret) {
   3847  1.1  riastrad 		DRM_ERROR("ni_enable_power_containment failed\n");
   3848  1.1  riastrad 		return ret;
   3849  1.1  riastrad 	}
   3850  1.1  riastrad 
   3851  1.1  riastrad 	/* update tdp */
   3852  1.1  riastrad 	ret = ni_power_control_set_level(rdev);
   3853  1.1  riastrad 	if (ret) {
   3854  1.1  riastrad 		DRM_ERROR("ni_power_control_set_level failed\n");
   3855  1.1  riastrad 		return ret;
   3856  1.1  riastrad 	}
   3857  1.1  riastrad 
   3858  1.1  riastrad 	return 0;
   3859  1.1  riastrad }
   3860  1.1  riastrad 
   3861  1.1  riastrad void ni_dpm_post_set_power_state(struct radeon_device *rdev)
   3862  1.1  riastrad {
   3863  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3864  1.1  riastrad 	struct radeon_ps *new_ps = &eg_pi->requested_rps;
   3865  1.1  riastrad 
   3866  1.1  riastrad 	ni_update_current_ps(rdev, new_ps);
   3867  1.1  riastrad }
   3868  1.1  riastrad 
   3869  1.1  riastrad #if 0
   3870  1.1  riastrad void ni_dpm_reset_asic(struct radeon_device *rdev)
   3871  1.1  riastrad {
   3872  1.1  riastrad 	ni_restrict_performance_levels_before_switch(rdev);
   3873  1.1  riastrad 	rv770_set_boot_state(rdev);
   3874  1.1  riastrad }
   3875  1.1  riastrad #endif
   3876  1.1  riastrad 
   3877  1.1  riastrad union power_info {
   3878  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO info;
   3879  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
   3880  1.1  riastrad 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
   3881  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
   3882  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
   3883  1.1  riastrad 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
   3884  1.1  riastrad };
   3885  1.1  riastrad 
   3886  1.1  riastrad union pplib_clock_info {
   3887  1.1  riastrad 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
   3888  1.1  riastrad 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
   3889  1.1  riastrad 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
   3890  1.1  riastrad 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
   3891  1.1  riastrad };
   3892  1.1  riastrad 
   3893  1.1  riastrad union pplib_power_state {
   3894  1.1  riastrad 	struct _ATOM_PPLIB_STATE v1;
   3895  1.1  riastrad 	struct _ATOM_PPLIB_STATE_V2 v2;
   3896  1.1  riastrad };
   3897  1.1  riastrad 
   3898  1.1  riastrad static void ni_parse_pplib_non_clock_info(struct radeon_device *rdev,
   3899  1.1  riastrad 					  struct radeon_ps *rps,
   3900  1.1  riastrad 					  struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
   3901  1.1  riastrad 					  u8 table_rev)
   3902  1.1  riastrad {
   3903  1.1  riastrad 	rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
   3904  1.1  riastrad 	rps->class = le16_to_cpu(non_clock_info->usClassification);
   3905  1.1  riastrad 	rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
   3906  1.1  riastrad 
   3907  1.1  riastrad 	if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
   3908  1.1  riastrad 		rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
   3909  1.1  riastrad 		rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
   3910  1.1  riastrad 	} else if (r600_is_uvd_state(rps->class, rps->class2)) {
   3911  1.1  riastrad 		rps->vclk = RV770_DEFAULT_VCLK_FREQ;
   3912  1.1  riastrad 		rps->dclk = RV770_DEFAULT_DCLK_FREQ;
   3913  1.1  riastrad 	} else {
   3914  1.1  riastrad 		rps->vclk = 0;
   3915  1.1  riastrad 		rps->dclk = 0;
   3916  1.1  riastrad 	}
   3917  1.1  riastrad 
   3918  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
   3919  1.1  riastrad 		rdev->pm.dpm.boot_ps = rps;
   3920  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
   3921  1.1  riastrad 		rdev->pm.dpm.uvd_ps = rps;
   3922  1.1  riastrad }
   3923  1.1  riastrad 
   3924  1.1  riastrad static void ni_parse_pplib_clock_info(struct radeon_device *rdev,
   3925  1.1  riastrad 				      struct radeon_ps *rps, int index,
   3926  1.1  riastrad 				      union pplib_clock_info *clock_info)
   3927  1.1  riastrad {
   3928  1.1  riastrad 	struct rv7xx_power_info *pi = rv770_get_pi(rdev);
   3929  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   3930  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
   3931  1.1  riastrad 	struct rv7xx_pl *pl = &ps->performance_levels[index];
   3932  1.1  riastrad 
   3933  1.1  riastrad 	ps->performance_level_count = index + 1;
   3934  1.1  riastrad 
   3935  1.1  riastrad 	pl->sclk = le16_to_cpu(clock_info->evergreen.usEngineClockLow);
   3936  1.1  riastrad 	pl->sclk |= clock_info->evergreen.ucEngineClockHigh << 16;
   3937  1.1  riastrad 	pl->mclk = le16_to_cpu(clock_info->evergreen.usMemoryClockLow);
   3938  1.1  riastrad 	pl->mclk |= clock_info->evergreen.ucMemoryClockHigh << 16;
   3939  1.1  riastrad 
   3940  1.1  riastrad 	pl->vddc = le16_to_cpu(clock_info->evergreen.usVDDC);
   3941  1.1  riastrad 	pl->vddci = le16_to_cpu(clock_info->evergreen.usVDDCI);
   3942  1.1  riastrad 	pl->flags = le32_to_cpu(clock_info->evergreen.ulFlags);
   3943  1.1  riastrad 
   3944  1.1  riastrad 	/* patch up vddc if necessary */
   3945  1.1  riastrad 	if (pl->vddc == 0xff01) {
   3946  1.1  riastrad 		if (pi->max_vddc)
   3947  1.1  riastrad 			pl->vddc = pi->max_vddc;
   3948  1.1  riastrad 	}
   3949  1.1  riastrad 
   3950  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) {
   3951  1.1  riastrad 		pi->acpi_vddc = pl->vddc;
   3952  1.1  riastrad 		eg_pi->acpi_vddci = pl->vddci;
   3953  1.1  riastrad 		if (ps->performance_levels[0].flags & ATOM_PPLIB_R600_FLAGS_PCIEGEN2)
   3954  1.1  riastrad 			pi->acpi_pcie_gen2 = true;
   3955  1.1  riastrad 		else
   3956  1.1  riastrad 			pi->acpi_pcie_gen2 = false;
   3957  1.1  riastrad 	}
   3958  1.1  riastrad 
   3959  1.1  riastrad 	if (rps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) {
   3960  1.1  riastrad 		eg_pi->ulv.supported = true;
   3961  1.1  riastrad 		eg_pi->ulv.pl = pl;
   3962  1.1  riastrad 	}
   3963  1.1  riastrad 
   3964  1.1  riastrad 	if (pi->min_vddc_in_table > pl->vddc)
   3965  1.1  riastrad 		pi->min_vddc_in_table = pl->vddc;
   3966  1.1  riastrad 
   3967  1.1  riastrad 	if (pi->max_vddc_in_table < pl->vddc)
   3968  1.1  riastrad 		pi->max_vddc_in_table = pl->vddc;
   3969  1.1  riastrad 
   3970  1.1  riastrad 	/* patch up boot state */
   3971  1.1  riastrad 	if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
   3972  1.1  riastrad 		u16 vddc, vddci, mvdd;
   3973  1.1  riastrad 		radeon_atombios_get_default_voltages(rdev, &vddc, &vddci, &mvdd);
   3974  1.1  riastrad 		pl->mclk = rdev->clock.default_mclk;
   3975  1.1  riastrad 		pl->sclk = rdev->clock.default_sclk;
   3976  1.1  riastrad 		pl->vddc = vddc;
   3977  1.1  riastrad 		pl->vddci = vddci;
   3978  1.1  riastrad 	}
   3979  1.1  riastrad 
   3980  1.1  riastrad 	if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
   3981  1.1  riastrad 	    ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
   3982  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk = pl->sclk;
   3983  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk = pl->mclk;
   3984  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddc = pl->vddc;
   3985  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddci = pl->vddci;
   3986  1.1  riastrad 	}
   3987  1.1  riastrad }
   3988  1.1  riastrad 
   3989  1.1  riastrad static int ni_parse_power_table(struct radeon_device *rdev)
   3990  1.1  riastrad {
   3991  1.1  riastrad 	struct radeon_mode_info *mode_info = &rdev->mode_info;
   3992  1.1  riastrad 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
   3993  1.1  riastrad 	union pplib_power_state *power_state;
   3994  1.1  riastrad 	int i, j;
   3995  1.1  riastrad 	union pplib_clock_info *clock_info;
   3996  1.1  riastrad 	union power_info *power_info;
   3997  1.1  riastrad 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
   3998  1.2  riastrad 	u16 data_offset;
   3999  1.1  riastrad 	u8 frev, crev;
   4000  1.1  riastrad 	struct ni_ps *ps;
   4001  1.1  riastrad 
   4002  1.1  riastrad 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
   4003  1.1  riastrad 				   &frev, &crev, &data_offset))
   4004  1.1  riastrad 		return -EINVAL;
   4005  1.1  riastrad 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
   4006  1.1  riastrad 
   4007  1.2  riastrad 	rdev->pm.dpm.ps = kcalloc(power_info->pplib.ucNumStates,
   4008  1.2  riastrad 				  sizeof(struct radeon_ps),
   4009  1.2  riastrad 				  GFP_KERNEL);
   4010  1.1  riastrad 	if (!rdev->pm.dpm.ps)
   4011  1.1  riastrad 		return -ENOMEM;
   4012  1.1  riastrad 
   4013  1.1  riastrad 	for (i = 0; i < power_info->pplib.ucNumStates; i++) {
   4014  1.1  riastrad 		power_state = (union pplib_power_state *)
   4015  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   4016  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usStateArrayOffset) +
   4017  1.1  riastrad 			 i * power_info->pplib.ucStateEntrySize);
   4018  1.1  riastrad 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
   4019  1.1  riastrad 			(mode_info->atom_context->bios + data_offset +
   4020  1.1  riastrad 			 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset) +
   4021  1.1  riastrad 			 (power_state->v1.ucNonClockStateIndex *
   4022  1.1  riastrad 			  power_info->pplib.ucNonClockSize));
   4023  1.1  riastrad 		if (power_info->pplib.ucStateEntrySize - 1) {
   4024  1.1  riastrad 			u8 *idx;
   4025  1.1  riastrad 			ps = kzalloc(sizeof(struct ni_ps), GFP_KERNEL);
   4026  1.1  riastrad 			if (ps == NULL) {
   4027  1.1  riastrad 				kfree(rdev->pm.dpm.ps);
   4028  1.1  riastrad 				return -ENOMEM;
   4029  1.1  riastrad 			}
   4030  1.1  riastrad 			rdev->pm.dpm.ps[i].ps_priv = ps;
   4031  1.1  riastrad 			ni_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
   4032  1.1  riastrad 							 non_clock_info,
   4033  1.1  riastrad 							 power_info->pplib.ucNonClockSize);
   4034  1.1  riastrad 			idx = (u8 *)&power_state->v1.ucClockStateIndices[0];
   4035  1.1  riastrad 			for (j = 0; j < (power_info->pplib.ucStateEntrySize - 1); j++) {
   4036  1.1  riastrad 				clock_info = (union pplib_clock_info *)
   4037  1.1  riastrad 					(mode_info->atom_context->bios + data_offset +
   4038  1.1  riastrad 					 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset) +
   4039  1.1  riastrad 					 (idx[j] * power_info->pplib.ucClockInfoSize));
   4040  1.1  riastrad 				ni_parse_pplib_clock_info(rdev,
   4041  1.1  riastrad 							  &rdev->pm.dpm.ps[i], j,
   4042  1.1  riastrad 							  clock_info);
   4043  1.1  riastrad 			}
   4044  1.1  riastrad 		}
   4045  1.1  riastrad 	}
   4046  1.1  riastrad 	rdev->pm.dpm.num_ps = power_info->pplib.ucNumStates;
   4047  1.1  riastrad 	return 0;
   4048  1.1  riastrad }
   4049  1.1  riastrad 
   4050  1.1  riastrad int ni_dpm_init(struct radeon_device *rdev)
   4051  1.1  riastrad {
   4052  1.1  riastrad 	struct rv7xx_power_info *pi;
   4053  1.1  riastrad 	struct evergreen_power_info *eg_pi;
   4054  1.1  riastrad 	struct ni_power_info *ni_pi;
   4055  1.1  riastrad 	struct atom_clock_dividers dividers;
   4056  1.1  riastrad 	int ret;
   4057  1.1  riastrad 
   4058  1.1  riastrad 	ni_pi = kzalloc(sizeof(struct ni_power_info), GFP_KERNEL);
   4059  1.1  riastrad 	if (ni_pi == NULL)
   4060  1.1  riastrad 		return -ENOMEM;
   4061  1.1  riastrad 	rdev->pm.dpm.priv = ni_pi;
   4062  1.1  riastrad 	eg_pi = &ni_pi->eg;
   4063  1.1  riastrad 	pi = &eg_pi->rv7xx;
   4064  1.1  riastrad 
   4065  1.1  riastrad 	rv770_get_max_vddc(rdev);
   4066  1.1  riastrad 
   4067  1.1  riastrad 	eg_pi->ulv.supported = false;
   4068  1.1  riastrad 	pi->acpi_vddc = 0;
   4069  1.1  riastrad 	eg_pi->acpi_vddci = 0;
   4070  1.1  riastrad 	pi->min_vddc_in_table = 0;
   4071  1.1  riastrad 	pi->max_vddc_in_table = 0;
   4072  1.1  riastrad 
   4073  1.1  riastrad 	ret = r600_get_platform_caps(rdev);
   4074  1.1  riastrad 	if (ret)
   4075  1.1  riastrad 		return ret;
   4076  1.1  riastrad 
   4077  1.1  riastrad 	ret = ni_parse_power_table(rdev);
   4078  1.1  riastrad 	if (ret)
   4079  1.1  riastrad 		return ret;
   4080  1.1  riastrad 	ret = r600_parse_extended_power_table(rdev);
   4081  1.1  riastrad 	if (ret)
   4082  1.1  riastrad 		return ret;
   4083  1.1  riastrad 
   4084  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries =
   4085  1.2  riastrad 		kcalloc(4,
   4086  1.2  riastrad 			sizeof(struct radeon_clock_voltage_dependency_entry),
   4087  1.2  riastrad 			GFP_KERNEL);
   4088  1.1  riastrad 	if (!rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries) {
   4089  1.1  riastrad 		r600_free_extended_power_table(rdev);
   4090  1.1  riastrad 		return -ENOMEM;
   4091  1.1  riastrad 	}
   4092  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.count = 4;
   4093  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].clk = 0;
   4094  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].v = 0;
   4095  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].clk = 36000;
   4096  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].v = 720;
   4097  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].clk = 54000;
   4098  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].v = 810;
   4099  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].clk = 72000;
   4100  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].v = 900;
   4101  1.1  riastrad 
   4102  1.1  riastrad 	ni_patch_dependency_tables_based_on_leakage(rdev);
   4103  1.1  riastrad 
   4104  1.1  riastrad 	if (rdev->pm.dpm.voltage_response_time == 0)
   4105  1.1  riastrad 		rdev->pm.dpm.voltage_response_time = R600_VOLTAGERESPONSETIME_DFLT;
   4106  1.1  riastrad 	if (rdev->pm.dpm.backbias_response_time == 0)
   4107  1.1  riastrad 		rdev->pm.dpm.backbias_response_time = R600_BACKBIASRESPONSETIME_DFLT;
   4108  1.1  riastrad 
   4109  1.1  riastrad 	ret = radeon_atom_get_clock_dividers(rdev, COMPUTE_ENGINE_PLL_PARAM,
   4110  1.1  riastrad 					     0, false, &dividers);
   4111  1.1  riastrad 	if (ret)
   4112  1.1  riastrad 		pi->ref_div = dividers.ref_div + 1;
   4113  1.1  riastrad 	else
   4114  1.1  riastrad 		pi->ref_div = R600_REFERENCEDIVIDER_DFLT;
   4115  1.1  riastrad 
   4116  1.1  riastrad 	pi->rlp = RV770_RLP_DFLT;
   4117  1.1  riastrad 	pi->rmp = RV770_RMP_DFLT;
   4118  1.1  riastrad 	pi->lhp = RV770_LHP_DFLT;
   4119  1.1  riastrad 	pi->lmp = RV770_LMP_DFLT;
   4120  1.1  riastrad 
   4121  1.1  riastrad 	eg_pi->ats[0].rlp = RV770_RLP_DFLT;
   4122  1.1  riastrad 	eg_pi->ats[0].rmp = RV770_RMP_DFLT;
   4123  1.1  riastrad 	eg_pi->ats[0].lhp = RV770_LHP_DFLT;
   4124  1.1  riastrad 	eg_pi->ats[0].lmp = RV770_LMP_DFLT;
   4125  1.1  riastrad 
   4126  1.1  riastrad 	eg_pi->ats[1].rlp = BTC_RLP_UVD_DFLT;
   4127  1.1  riastrad 	eg_pi->ats[1].rmp = BTC_RMP_UVD_DFLT;
   4128  1.1  riastrad 	eg_pi->ats[1].lhp = BTC_LHP_UVD_DFLT;
   4129  1.1  riastrad 	eg_pi->ats[1].lmp = BTC_LMP_UVD_DFLT;
   4130  1.1  riastrad 
   4131  1.1  riastrad 	eg_pi->smu_uvd_hs = true;
   4132  1.1  riastrad 
   4133  1.1  riastrad 	if (rdev->pdev->device == 0x6707) {
   4134  1.1  riastrad 		pi->mclk_strobe_mode_threshold = 55000;
   4135  1.1  riastrad 		pi->mclk_edc_enable_threshold = 55000;
   4136  1.1  riastrad 		eg_pi->mclk_edc_wr_enable_threshold = 55000;
   4137  1.1  riastrad 	} else {
   4138  1.1  riastrad 		pi->mclk_strobe_mode_threshold = 40000;
   4139  1.1  riastrad 		pi->mclk_edc_enable_threshold = 40000;
   4140  1.1  riastrad 		eg_pi->mclk_edc_wr_enable_threshold = 40000;
   4141  1.1  riastrad 	}
   4142  1.1  riastrad 	ni_pi->mclk_rtt_mode_threshold = eg_pi->mclk_edc_wr_enable_threshold;
   4143  1.1  riastrad 
   4144  1.1  riastrad 	pi->voltage_control =
   4145  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_VDDC, 0);
   4146  1.1  riastrad 
   4147  1.1  riastrad 	pi->mvdd_control =
   4148  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_MVDDC, 0);
   4149  1.1  riastrad 
   4150  1.1  riastrad 	eg_pi->vddci_control =
   4151  1.1  riastrad 		radeon_atom_is_voltage_gpio(rdev, SET_VOLTAGE_TYPE_ASIC_VDDCI, 0);
   4152  1.1  riastrad 
   4153  1.1  riastrad 	rv770_get_engine_memory_ss(rdev);
   4154  1.1  riastrad 
   4155  1.1  riastrad 	pi->asi = RV770_ASI_DFLT;
   4156  1.1  riastrad 	pi->pasi = CYPRESS_HASI_DFLT;
   4157  1.1  riastrad 	pi->vrc = CYPRESS_VRC_DFLT;
   4158  1.1  riastrad 
   4159  1.1  riastrad 	pi->power_gating = false;
   4160  1.1  riastrad 
   4161  1.1  riastrad 	pi->gfx_clock_gating = true;
   4162  1.1  riastrad 
   4163  1.1  riastrad 	pi->mg_clock_gating = true;
   4164  1.1  riastrad 	pi->mgcgtssm = true;
   4165  1.1  riastrad 	eg_pi->ls_clock_gating = false;
   4166  1.1  riastrad 	eg_pi->sclk_deep_sleep = false;
   4167  1.1  riastrad 
   4168  1.1  riastrad 	pi->dynamic_pcie_gen2 = true;
   4169  1.1  riastrad 
   4170  1.1  riastrad 	if (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE)
   4171  1.1  riastrad 		pi->thermal_protection = true;
   4172  1.1  riastrad 	else
   4173  1.1  riastrad 		pi->thermal_protection = false;
   4174  1.1  riastrad 
   4175  1.1  riastrad 	pi->display_gap = true;
   4176  1.1  riastrad 
   4177  1.1  riastrad 	pi->dcodt = true;
   4178  1.1  riastrad 
   4179  1.1  riastrad 	pi->ulps = true;
   4180  1.1  riastrad 
   4181  1.1  riastrad 	eg_pi->dynamic_ac_timing = true;
   4182  1.1  riastrad 	eg_pi->abm = true;
   4183  1.1  riastrad 	eg_pi->mcls = true;
   4184  1.1  riastrad 	eg_pi->light_sleep = true;
   4185  1.1  riastrad 	eg_pi->memory_transition = true;
   4186  1.1  riastrad #if defined(CONFIG_ACPI)
   4187  1.1  riastrad 	eg_pi->pcie_performance_request =
   4188  1.1  riastrad 		radeon_acpi_is_pcie_performance_request_supported(rdev);
   4189  1.1  riastrad #else
   4190  1.1  riastrad 	eg_pi->pcie_performance_request = false;
   4191  1.1  riastrad #endif
   4192  1.1  riastrad 
   4193  1.1  riastrad 	eg_pi->dll_default_on = false;
   4194  1.1  riastrad 
   4195  1.1  riastrad 	eg_pi->sclk_deep_sleep = false;
   4196  1.1  riastrad 
   4197  1.1  riastrad 	pi->mclk_stutter_mode_threshold = 0;
   4198  1.1  riastrad 
   4199  1.1  riastrad 	pi->sram_end = SMC_RAM_END;
   4200  1.1  riastrad 
   4201  1.1  riastrad 	rdev->pm.dpm.dyn_state.mclk_sclk_ratio = 3;
   4202  1.1  riastrad 	rdev->pm.dpm.dyn_state.vddc_vddci_delta = 200;
   4203  1.1  riastrad 	rdev->pm.dpm.dyn_state.min_vddc_for_pcie_gen2 = 900;
   4204  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_sclk_values.count = ARRAY_SIZE(btc_valid_sclk);
   4205  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_sclk_values.values = btc_valid_sclk;
   4206  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_mclk_values.count = 0;
   4207  1.1  riastrad 	rdev->pm.dpm.dyn_state.valid_mclk_values.values = NULL;
   4208  1.1  riastrad 	rdev->pm.dpm.dyn_state.sclk_mclk_delta = 12500;
   4209  1.1  riastrad 
   4210  1.1  riastrad 	ni_pi->cac_data.leakage_coefficients.at = 516;
   4211  1.1  riastrad 	ni_pi->cac_data.leakage_coefficients.bt = 18;
   4212  1.1  riastrad 	ni_pi->cac_data.leakage_coefficients.av = 51;
   4213  1.1  riastrad 	ni_pi->cac_data.leakage_coefficients.bv = 2957;
   4214  1.1  riastrad 
   4215  1.1  riastrad 	switch (rdev->pdev->device) {
   4216  1.1  riastrad 	case 0x6700:
   4217  1.1  riastrad 	case 0x6701:
   4218  1.1  riastrad 	case 0x6702:
   4219  1.1  riastrad 	case 0x6703:
   4220  1.1  riastrad 	case 0x6718:
   4221  1.1  riastrad 		ni_pi->cac_weights = &cac_weights_cayman_xt;
   4222  1.1  riastrad 		break;
   4223  1.1  riastrad 	case 0x6705:
   4224  1.1  riastrad 	case 0x6719:
   4225  1.1  riastrad 	case 0x671D:
   4226  1.1  riastrad 	case 0x671C:
   4227  1.1  riastrad 	default:
   4228  1.1  riastrad 		ni_pi->cac_weights = &cac_weights_cayman_pro;
   4229  1.1  riastrad 		break;
   4230  1.1  riastrad 	case 0x6704:
   4231  1.1  riastrad 	case 0x6706:
   4232  1.1  riastrad 	case 0x6707:
   4233  1.1  riastrad 	case 0x6708:
   4234  1.1  riastrad 	case 0x6709:
   4235  1.1  riastrad 		ni_pi->cac_weights = &cac_weights_cayman_le;
   4236  1.1  riastrad 		break;
   4237  1.1  riastrad 	}
   4238  1.1  riastrad 
   4239  1.1  riastrad 	if (ni_pi->cac_weights->enable_power_containment_by_default) {
   4240  1.1  riastrad 		ni_pi->enable_power_containment = true;
   4241  1.1  riastrad 		ni_pi->enable_cac = true;
   4242  1.1  riastrad 		ni_pi->enable_sq_ramping = true;
   4243  1.1  riastrad 	} else {
   4244  1.1  riastrad 		ni_pi->enable_power_containment = false;
   4245  1.1  riastrad 		ni_pi->enable_cac = false;
   4246  1.1  riastrad 		ni_pi->enable_sq_ramping = false;
   4247  1.1  riastrad 	}
   4248  1.1  riastrad 
   4249  1.1  riastrad 	ni_pi->driver_calculate_cac_leakage = false;
   4250  1.1  riastrad 	ni_pi->cac_configuration_required = true;
   4251  1.1  riastrad 
   4252  1.1  riastrad 	if (ni_pi->cac_configuration_required) {
   4253  1.1  riastrad 		ni_pi->support_cac_long_term_average = true;
   4254  1.1  riastrad 		ni_pi->lta_window_size = ni_pi->cac_weights->l2_lta_window_size;
   4255  1.1  riastrad 		ni_pi->lts_truncate = ni_pi->cac_weights->lts_truncate;
   4256  1.1  riastrad 	} else {
   4257  1.1  riastrad 		ni_pi->support_cac_long_term_average = false;
   4258  1.1  riastrad 		ni_pi->lta_window_size = 0;
   4259  1.1  riastrad 		ni_pi->lts_truncate = 0;
   4260  1.1  riastrad 	}
   4261  1.1  riastrad 
   4262  1.1  riastrad 	ni_pi->use_power_boost_limit = true;
   4263  1.1  riastrad 
   4264  1.1  riastrad 	/* make sure dc limits are valid */
   4265  1.1  riastrad 	if ((rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.sclk == 0) ||
   4266  1.1  riastrad 	    (rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.mclk == 0))
   4267  1.1  riastrad 		rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc =
   4268  1.1  riastrad 			rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
   4269  1.1  riastrad 
   4270  1.1  riastrad 	return 0;
   4271  1.1  riastrad }
   4272  1.1  riastrad 
   4273  1.1  riastrad void ni_dpm_fini(struct radeon_device *rdev)
   4274  1.1  riastrad {
   4275  1.1  riastrad 	int i;
   4276  1.1  riastrad 
   4277  1.1  riastrad 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
   4278  1.1  riastrad 		kfree(rdev->pm.dpm.ps[i].ps_priv);
   4279  1.1  riastrad 	}
   4280  1.1  riastrad 	kfree(rdev->pm.dpm.ps);
   4281  1.1  riastrad 	kfree(rdev->pm.dpm.priv);
   4282  1.1  riastrad 	kfree(rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries);
   4283  1.1  riastrad 	r600_free_extended_power_table(rdev);
   4284  1.1  riastrad }
   4285  1.1  riastrad 
   4286  1.1  riastrad void ni_dpm_print_power_state(struct radeon_device *rdev,
   4287  1.1  riastrad 			      struct radeon_ps *rps)
   4288  1.1  riastrad {
   4289  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
   4290  1.1  riastrad 	struct rv7xx_pl *pl;
   4291  1.1  riastrad 	int i;
   4292  1.1  riastrad 
   4293  1.1  riastrad 	r600_dpm_print_class_info(rps->class, rps->class2);
   4294  1.1  riastrad 	r600_dpm_print_cap_info(rps->caps);
   4295  1.1  riastrad 	printk("\tuvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   4296  1.1  riastrad 	for (i = 0; i < ps->performance_level_count; i++) {
   4297  1.1  riastrad 		pl = &ps->performance_levels[i];
   4298  1.1  riastrad 		if (rdev->family >= CHIP_TAHITI)
   4299  1.1  riastrad 			printk("\t\tpower level %d    sclk: %u mclk: %u vddc: %u vddci: %u pcie gen: %u\n",
   4300  1.1  riastrad 			       i, pl->sclk, pl->mclk, pl->vddc, pl->vddci, pl->pcie_gen + 1);
   4301  1.1  riastrad 		else
   4302  1.1  riastrad 			printk("\t\tpower level %d    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   4303  1.1  riastrad 			       i, pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   4304  1.1  riastrad 	}
   4305  1.1  riastrad 	r600_dpm_print_ps_status(rdev, rps);
   4306  1.1  riastrad }
   4307  1.1  riastrad 
   4308  1.1  riastrad #ifdef CONFIG_DEBUG_FS
   4309  1.1  riastrad void ni_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
   4310  1.1  riastrad 						    struct seq_file *m)
   4311  1.1  riastrad {
   4312  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   4313  1.1  riastrad 	struct radeon_ps *rps = &eg_pi->current_rps;
   4314  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
   4315  1.1  riastrad 	struct rv7xx_pl *pl;
   4316  1.1  riastrad 	u32 current_index =
   4317  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_STATE_INDEX_MASK) >>
   4318  1.1  riastrad 		CURRENT_STATE_INDEX_SHIFT;
   4319  1.1  riastrad 
   4320  1.1  riastrad 	if (current_index >= ps->performance_level_count) {
   4321  1.1  riastrad 		seq_printf(m, "invalid dpm profile %d\n", current_index);
   4322  1.1  riastrad 	} else {
   4323  1.1  riastrad 		pl = &ps->performance_levels[current_index];
   4324  1.1  riastrad 		seq_printf(m, "uvd    vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
   4325  1.1  riastrad 		seq_printf(m, "power level %d    sclk: %u mclk: %u vddc: %u vddci: %u\n",
   4326  1.1  riastrad 			   current_index, pl->sclk, pl->mclk, pl->vddc, pl->vddci);
   4327  1.1  riastrad 	}
   4328  1.1  riastrad }
   4329  1.1  riastrad #endif	/* CONFIG_DEBUG_FS */
   4330  1.1  riastrad 
   4331  1.1  riastrad u32 ni_dpm_get_current_sclk(struct radeon_device *rdev)
   4332  1.1  riastrad {
   4333  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   4334  1.1  riastrad 	struct radeon_ps *rps = &eg_pi->current_rps;
   4335  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
   4336  1.1  riastrad 	struct rv7xx_pl *pl;
   4337  1.1  riastrad 	u32 current_index =
   4338  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_STATE_INDEX_MASK) >>
   4339  1.1  riastrad 		CURRENT_STATE_INDEX_SHIFT;
   4340  1.1  riastrad 
   4341  1.1  riastrad 	if (current_index >= ps->performance_level_count) {
   4342  1.1  riastrad 		return 0;
   4343  1.1  riastrad 	} else {
   4344  1.1  riastrad 		pl = &ps->performance_levels[current_index];
   4345  1.1  riastrad 		return pl->sclk;
   4346  1.1  riastrad 	}
   4347  1.1  riastrad }
   4348  1.1  riastrad 
   4349  1.1  riastrad u32 ni_dpm_get_current_mclk(struct radeon_device *rdev)
   4350  1.1  riastrad {
   4351  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   4352  1.1  riastrad 	struct radeon_ps *rps = &eg_pi->current_rps;
   4353  1.1  riastrad 	struct ni_ps *ps = ni_get_ps(rps);
   4354  1.1  riastrad 	struct rv7xx_pl *pl;
   4355  1.1  riastrad 	u32 current_index =
   4356  1.1  riastrad 		(RREG32(TARGET_AND_CURRENT_PROFILE_INDEX) & CURRENT_STATE_INDEX_MASK) >>
   4357  1.1  riastrad 		CURRENT_STATE_INDEX_SHIFT;
   4358  1.1  riastrad 
   4359  1.1  riastrad 	if (current_index >= ps->performance_level_count) {
   4360  1.1  riastrad 		return 0;
   4361  1.1  riastrad 	} else {
   4362  1.1  riastrad 		pl = &ps->performance_levels[current_index];
   4363  1.1  riastrad 		return pl->mclk;
   4364  1.1  riastrad 	}
   4365  1.1  riastrad }
   4366  1.1  riastrad 
   4367  1.1  riastrad u32 ni_dpm_get_sclk(struct radeon_device *rdev, bool low)
   4368  1.1  riastrad {
   4369  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   4370  1.1  riastrad 	struct ni_ps *requested_state = ni_get_ps(&eg_pi->requested_rps);
   4371  1.1  riastrad 
   4372  1.1  riastrad 	if (low)
   4373  1.1  riastrad 		return requested_state->performance_levels[0].sclk;
   4374  1.1  riastrad 	else
   4375  1.1  riastrad 		return requested_state->performance_levels[requested_state->performance_level_count - 1].sclk;
   4376  1.1  riastrad }
   4377  1.1  riastrad 
   4378  1.1  riastrad u32 ni_dpm_get_mclk(struct radeon_device *rdev, bool low)
   4379  1.1  riastrad {
   4380  1.1  riastrad 	struct evergreen_power_info *eg_pi = evergreen_get_pi(rdev);
   4381  1.1  riastrad 	struct ni_ps *requested_state = ni_get_ps(&eg_pi->requested_rps);
   4382  1.1  riastrad 
   4383  1.1  riastrad 	if (low)
   4384  1.1  riastrad 		return requested_state->performance_levels[0].mclk;
   4385  1.1  riastrad 	else
   4386  1.1  riastrad 		return requested_state->performance_levels[requested_state->performance_level_count - 1].mclk;
   4387  1.1  riastrad }
   4388  1.1  riastrad 
   4389