summaryrefslogtreecommitdiffstats
path: root/sys/contrib/octeon-sdk/cvmx-power-throttle.c
blob: 2f7de3c83cecbed8cdf12a54ec2e7e8909f2af31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/***********************license start***************
 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
 * reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.

 *   * Neither the name of Cavium Inc. nor the names of
 *     its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written
 *     permission.

 * This Software, including technical data, may be subject to U.S. export  control
 * laws, including the U.S. Export Administration Act and its  associated
 * regulations, and may be subject to export or import  regulations in other
 * countries.

 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
 ***********************license end**************************************/

/**
 * @file
 *
 * Interface to power-throttle control, measurement, and debugging
 * facilities.
 *
 * <hr>$Revision: 70030 $<hr>
 *
 */

#include "cvmx.h"
#include "cvmx-asm.h"
#include "cvmx-coremask.h"
#include "cvmx-power-throttle.h"


#define CVMX_PTH_GET_MASK(len, pos)	\
	((((uint64_t)1 << (len)) - 1) << (pos))

#define CVMX_PTH_AVAILABLE		\
    (cvmx_power_throttle_get_register(0) != (uint64_t)-1)

/**
 * a field of the POWTHROTTLE register
 */
static struct cvmx_power_throttle_rfield_t {
	char	name[16];	/* the field's name */
	int32_t	pos;		/* position of the field's LSb */
	int32_t	len;		/* the field's length */
	int	present;	/* 1 for present */
} cvmx_power_throttle_rfield[] = {
	{"MAXPOW",   56,  8, 0},
	{"POWER" ,   48,  8, 0},
	{"THROTT",   40,  8, 0},
	{"Reserved", 28, 12, 0},
	{"DISTAG",   27,  1, 0},
	{"PERIOD",   24,  3, 0},
	{"POWLIM",   16,  8, 0},
	{"MAXTHR",    8,  8, 0},
	{"MINTHR",    0,  8, 0},
	{"HRMPOWADJ",32,  8, 0},
	{"OVRRD",    28,  1, 0}
};

static uint64_t cvmx_power_throttle_csr_addr(int ppid);

static int cvmx_power_throttle_initialized;

/**
 * @INTERNAL
 * Initialize cvmx_power_throttle_rfield[] based on model.
 */
static void cvmx_power_throttle_init(void)
{
    /*
     * Turn on the fields for a model
     */
    if (OCTEON_IS_MODEL(OCTEON_CN6XXX))
    {
        int i;
	struct cvmx_power_throttle_rfield_t *p;

        for (i = 0; i < CVMX_PTH_INDEX_MAX; i++)
	    cvmx_power_throttle_rfield[i].present = 1;

        if (OCTEON_IS_MODEL(OCTEON_CN63XX))
	{
	    /*
	     * These fields do not come with o63
	     */
	    p = &cvmx_power_throttle_rfield[CVMX_PTH_INDEX_HRMPOWADJ];
	    p->present = 0;
	    p = &cvmx_power_throttle_rfield[CVMX_PTH_INDEX_OVRRD];
	    p->present = 0;
	}
	else
	{
	    /*
	     * The reserved field shrinks in models newer than o63
	     */
	    p = &cvmx_power_throttle_rfield[CVMX_PTH_INDEX_RESERVED];
	    p->pos = 29;
	    p->len = 3;
	}
    }
}

uint64_t cvmx_power_throttle_get_field(uint64_t r,
    cvmx_power_throttle_field_index_t i)
{
    uint64_t m;
    struct cvmx_power_throttle_rfield_t *p;

    assert(i < CVMX_PTH_INDEX_MAX);
    p = &cvmx_power_throttle_rfield[i];
    if (!p->present)
        return (uint64_t) -1;
    m = CVMX_PTH_GET_MASK(p->len, p->pos);
    
    return((r & m) >> p->pos);
}

/**
 * @INTERNAL
 * Set the i'th field of power-throttle register r to v.
 */
static int cvmx_power_throttle_set_field(int i, uint64_t r, uint64_t v)
{
    if (OCTEON_IS_MODEL(OCTEON_CN6XXX))
    {
        uint64_t m;
        struct cvmx_power_throttle_rfield_t *p;

        assert(i < CVMX_PTH_INDEX_MAX);

        p = &cvmx_power_throttle_rfield[i];
        m = CVMX_PTH_GET_MASK(p->len, p->pos);

        return((~m & r) | ((v << p->pos) & m));
    }
    return 0;
}

/**
 * @INTERNAL
 * Set the POWLIM field as percentage% of the MAXPOW field in r.
 */
static uint64_t cvmx_power_throttle_set_powlim(int ppid,
    uint8_t percentage)
{
    if (OCTEON_IS_MODEL(OCTEON_CN6XXX))
    {
        uint64_t t, csr_addr, r;

        assert(percentage < 101);
        csr_addr = cvmx_power_throttle_csr_addr(ppid);
        r = cvmx_read_csr(csr_addr);

        t = cvmx_power_throttle_get_field(r, CVMX_PTH_INDEX_MAXPOW);
	if (!OCTEON_IS_MODEL(OCTEON_CN63XX))
	{
	    uint64_t s;

	    s = cvmx_power_throttle_get_field(r, CVMX_PTH_INDEX_HRMPOWADJ);
	    assert(t > s);
	    t = t - s;
	}

	t = percentage * t / 100;
        r = cvmx_power_throttle_set_field(CVMX_PTH_INDEX_POWLIM, r, t);

        cvmx_write_csr(csr_addr, r);
        return r;
    }
    return 0;
}

/**
 * @INTERNAL
 * Given ppid, calculate its PowThrottle register's L2C_COP0_MAP CSR
 * address. (ppid == PTH_PPID_BCAST is for broadcasting)
 */
static uint64_t cvmx_power_throttle_csr_addr(int ppid)
{
    if (OCTEON_IS_MODEL(OCTEON_CN6XXX))
    {
        uint64_t csr_addr, reg_num, reg_reg, reg_sel;

        assert(ppid < CVMX_MAX_CORES);

        /*
         * register 11 selection 6
         */
        reg_reg = 11;
        reg_sel = 6;
        reg_num = (ppid << 8) + (reg_reg << 3) + reg_sel;
        csr_addr = CVMX_L2C_COP0_MAPX(0) + ((reg_num) << 3);

        return csr_addr;
    }
    return 0;
}

int cvmx_power_throttle_self(uint8_t percentage)
{
    if (!CVMX_PTH_AVAILABLE)
        return -1;

    if (cvmx_power_throttle_set_powlim(cvmx_get_core_num(),
        percentage) == 0)
	return -1;

    return 0;
}

int cvmx_power_throttle(uint8_t percentage, uint64_t coremask)
{
    int ppid;
    int ret;

    if (!CVMX_PTH_AVAILABLE)
        return -1;

    ret = 0;
    for (ppid = 0; ppid < CVMX_MAX_CORES; ppid++)
    {
        if ((((uint64_t) 1) << ppid) & coremask)
	{
            if (cvmx_power_throttle_set_powlim(ppid, percentage) == 0)
	        ret = -2;
        }
    }

    return ret;
}

int cvmx_power_throttle_bmp(uint8_t percentage, struct cvmx_coremask *pcm)
{
    int ppid;
    int ret;

    if (!CVMX_PTH_AVAILABLE)
        return -1;

    ret = 0;
    CVMX_COREMASK_FOR_EACH_CORE_BEGIN(pcm, ppid)
    {
        if (cvmx_power_throttle_set_powlim(ppid, percentage) == 0)
	    ret = -2;
    } CVMX_COREMASK_FOR_EACH_CORE_END;

    return ret;
}

uint64_t cvmx_power_throttle_get_register(int ppid)
{
    uint64_t csr_addr;

    if (!cvmx_power_throttle_initialized)
    {
	cvmx_power_throttle_init();
	cvmx_power_throttle_initialized = 1;
    }

    csr_addr = cvmx_power_throttle_csr_addr(ppid);

    if (csr_addr == 0)
        return -1;

    return cvmx_read_csr(csr_addr);
}
OpenPOWER on IntegriCloud