nvg.c 6.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
3
4
5
6
7
8
9
10
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch.h>
#include <arch_helpers.h>
#include <common/debug.h>
#include <denver.h>
Steven Kao's avatar
Steven Kao committed
11
#include <errno.h>
12
13
#include <lib/mmio.h>
#include <mce_private.h>
Steven Kao's avatar
Steven Kao committed
14
15
#include <platform_def.h>
#include <t194_nvg.h>
16
#include <tegra_private.h>
17

18
19
#define	ID_AFR0_EL1_CACHE_OPS_SHIFT	12
#define	ID_AFR0_EL1_CACHE_OPS_MASK	0xFU
Steven Kao's avatar
Steven Kao committed
20
21
22
23
24
25
26
/*
 * Reports the major and minor version of this interface.
 *
 * NVGDATA[0:31]: SW(R) Minor Version
 * NVGDATA[32:63]: SW(R) Major Version
 */
uint64_t nvg_get_version(void)
27
{
28
	nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_VERSION);
29

Steven Kao's avatar
Steven Kao committed
30
31
	return (uint64_t)nvg_get_result();
}
32

Steven Kao's avatar
Steven Kao committed
33
34
35
36
37
38
39
/*
 * Enable the perf per watt mode.
 *
 * NVGDATA[0]: SW(RW), 1 = enable perf per watt mode
 */
int32_t nvg_enable_power_perf_mode(void)
{
40
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_PERF, 1U);
Steven Kao's avatar
Steven Kao committed
41
42
43
44
45
46
47
48
49
50
51

	return 0;
}

/*
 * Disable the perf per watt mode.
 *
 * NVGDATA[0]: SW(RW), 0 = disable perf per watt mode
 */
int32_t nvg_disable_power_perf_mode(void)
{
52
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_PERF, 0U);
Steven Kao's avatar
Steven Kao committed
53
54
55
56
57
58
59
60
61
62
63

	return 0;
}

/*
 * Enable the battery saver mode.
 *
 * NVGDATA[2]: SW(RW), 1 = enable battery saver mode
 */
int32_t nvg_enable_power_saver_modes(void)
{
64
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_MODES, 1U);
Steven Kao's avatar
Steven Kao committed
65
66
67
68
69
70
71
72
73
74
75

	return 0;
}

/*
 * Disable the battery saver mode.
 *
 * NVGDATA[2]: SW(RW), 0 = disable battery saver mode
 */
int32_t nvg_disable_power_saver_modes(void)
{
76
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_MODES, 0U);
77
78
79
80

	return 0;
}

Steven Kao's avatar
Steven Kao committed
81
82
83
84
85
86
87
88
89
/*
 * Set the expected wake time in TSC ticks for the next low-power state the
 * core enters.
 *
 * NVGDATA[0:31]: SW(RW), WAKE_TIME
 */
void nvg_set_wake_time(uint32_t wake_time)
{
	/* time (TSC ticks) until the core is expected to get a wake event */
90
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_WAKE_TIME, (uint64_t)wake_time);
Steven Kao's avatar
Steven Kao committed
91
92
}

93
94
95
/*
 * This request allows updating of CLUSTER_CSTATE, CCPLEX_CSTATE and
 * SYSTEM_CSTATE values.
Steven Kao's avatar
Steven Kao committed
96
97
98
 *
 * NVGDATA[0:2]: SW(RW), CLUSTER_CSTATE
 * NVGDATA[7]: SW(W), update cluster flag
99
 * NVGDATA[8:10]: SW(RW), CG_CSTATE
Steven Kao's avatar
Steven Kao committed
100
101
102
103
104
 * NVGDATA[15]: SW(W), update ccplex flag
 * NVGDATA[16:19]: SW(RW), SYSTEM_CSTATE
 * NVGDATA[23]: SW(W), update system flag
 * NVGDATA[31]: SW(W), update wake mask flag
 * NVGDATA[32:63]: SW(RW), WAKE_MASK
105
 */
Steven Kao's avatar
Steven Kao committed
106
107
void nvg_update_cstate_info(uint32_t cluster, uint32_t ccplex,
		uint32_t system, uint32_t wake_mask, uint8_t update_wake_mask)
108
109
110
111
{
	uint64_t val = 0;

	/* update CLUSTER_CSTATE? */
Steven Kao's avatar
Steven Kao committed
112
113
114
115
	if (cluster != 0U) {
		val |= ((uint64_t)cluster & CLUSTER_CSTATE_MASK) |
				CLUSTER_CSTATE_UPDATE_BIT;
	}
116
117

	/* update CCPLEX_CSTATE? */
Steven Kao's avatar
Steven Kao committed
118
119
120
121
	if (ccplex != 0U) {
		val |= (((uint64_t)ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
				CCPLEX_CSTATE_UPDATE_BIT;
	}
122
123

	/* update SYSTEM_CSTATE? */
Steven Kao's avatar
Steven Kao committed
124
125
126
127
	if (system != 0U) {
		val |= (((uint64_t)system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
				SYSTEM_CSTATE_UPDATE_BIT;
	}
128
129

	/* update wake mask value? */
Steven Kao's avatar
Steven Kao committed
130
	if (update_wake_mask != 0U) {
131
		val |= CSTATE_WAKE_MASK_UPDATE_BIT;
Steven Kao's avatar
Steven Kao committed
132
	}
133
134

	/* set the wake mask */
Steven Kao's avatar
Steven Kao committed
135
	val |= ((uint64_t)wake_mask & CSTATE_WAKE_MASK_CLEAR) << CSTATE_WAKE_MASK_SHIFT;
136
137

	/* set the updated cstate info */
138
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CSTATE_INFO, val);
139
140
}

Steven Kao's avatar
Steven Kao committed
141
142
143
144
145
146
/*
 * Return a non-zero value if the CCPLEX is able to enter SC7
 *
 * NVGDATA[0]: SW(R), Is allowed result
 */
int32_t nvg_is_sc7_allowed(void)
147
{
Steven Kao's avatar
Steven Kao committed
148
	/* issue command to check if SC7 is allowed */
149
	nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_IS_SC7_ALLOWED);
Steven Kao's avatar
Steven Kao committed
150
151
152

	/* 1 = SC7 allowed, 0 = SC7 not allowed */
	return (int32_t)nvg_get_result();
153
154
}

Steven Kao's avatar
Steven Kao committed
155
156
157
158
159
160
161
/*
 * Wake an offlined logical core. Note that a core is offlined by entering
 * a C-state where the WAKE_MASK is all 0.
 *
 * NVGDATA[0:3]: SW(W) logical core to online
 */
int32_t nvg_online_core(uint32_t core)
162
{
Steven Kao's avatar
Steven Kao committed
163
	int32_t ret = 0;
164

Steven Kao's avatar
Steven Kao committed
165
166
167
	/* sanity check the core ID value */
	if (core > (uint32_t)PLATFORM_CORE_COUNT) {
		ERROR("%s: unknown core id (%d)\n", __func__, core);
168
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
169
170
	} else {
		/* get a core online */
171
172
		nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_ONLINE_CORE,
					(uint64_t)core & MCE_CORE_ID_MASK);
173
174
	}

Steven Kao's avatar
Steven Kao committed
175
176
177
178
179
180
181
182
183
184
	return ret;
}

/*
 * MC GSC (General Security Carveout) register values are expected to be
 * changed by TrustZone ARM code after boot.
 *
 * NVGDATA[0:15] SW(R) GSC enun
 */
int32_t nvg_update_ccplex_gsc(uint32_t gsc_idx)
185
{
186
	int32_t ret = 0;
Steven Kao's avatar
Steven Kao committed
187
188

	/* sanity check GSC ID */
189
190
	if (gsc_idx > (uint32_t)TEGRA_NVG_CHANNEL_UPDATE_GSC_VPR) {
		ERROR("%s: unknown gsc_idx (%u)\n", __func__, gsc_idx);
191
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
192
	} else {
193
		nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_UPDATE_CCPLEX_GSC,
194
				     (uint64_t)gsc_idx);
195
196
	}

Steven Kao's avatar
Steven Kao committed
197
198
	return ret;
}
199

Steven Kao's avatar
Steven Kao committed
200
201
202
203
204
/*
 * Cache clean operation for all CCPLEX caches.
 */
int32_t nvg_roc_clean_cache(void)
{
205
	int32_t ret = 0;
206

207
208
209
210
211
	/* check if cache flush through mts is supported */
	if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
			ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
		if (nvg_cache_clean() == 0U) {
			ERROR("%s: failed\n", __func__);
212
			ret = -ENODEV;
213
214
		}
	} else {
215
		ret = -ENOTSUP;
216
	}
217

218
	return ret;
219
220
}

Steven Kao's avatar
Steven Kao committed
221
222
223
224
/*
 * Cache clean and invalidate operation for all CCPLEX caches.
 */
int32_t nvg_roc_flush_cache(void)
225
{
226
	int32_t ret = 0;
227

228
229
230
231
232
	/* check if cache flush through mts is supported */
	if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
			ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
		if (nvg_cache_clean_inval() == 0U) {
			ERROR("%s: failed\n", __func__);
233
			ret = -ENODEV;
234
235
		}
	} else {
236
		ret = -ENOTSUP;
237
	}
238

239
	return ret;
Steven Kao's avatar
Steven Kao committed
240
}
241

Steven Kao's avatar
Steven Kao committed
242
243
244
245
246
/*
 * Cache clean and invalidate, clear TR-bit operation for all CCPLEX caches.
 */
int32_t nvg_roc_clean_cache_trbits(void)
{
247
	int32_t ret = 0;
248

249
250
251
252
253
	/* check if cache flush through mts is supported */
	if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
			ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
		if (nvg_cache_inval_all() == 0U) {
			ERROR("%s: failed\n", __func__);
254
			ret = -ENODEV;
255
256
		}
	} else {
257
		ret = -ENOTSUP;
258
	}
259

260
	return ret;
261
}
Steven Kao's avatar
Steven Kao committed
262
263
264
265
266
267
268

/*
 * Set the power state for a core
 */
int32_t nvg_enter_cstate(uint32_t state, uint32_t wake_time)
{
	int32_t ret = 0;
269
	uint64_t val = 0ULL;
Steven Kao's avatar
Steven Kao committed
270
271
272
273
274
275
276

	/* check for allowed power state */
	if ((state != (uint32_t)TEGRA_NVG_CORE_C0) &&
		(state != (uint32_t)TEGRA_NVG_CORE_C1) &&
	    (state != (uint32_t)TEGRA_NVG_CORE_C6) &&
		(state != (uint32_t)TEGRA_NVG_CORE_C7))
	{
277
278
		ERROR("%s: unknown cstate (%u)\n", __func__, state);
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
279
280
281
282
283
	} else {
		/* time (TSC ticks) until the core is expected to get a wake event */
		nvg_set_wake_time(wake_time);

		/* set the core cstate */
284
285
		val = read_actlr_el1() & ~ACTLR_EL1_PMSTATE_MASK;
		write_actlr_el1(val | (uint64_t)state);
Steven Kao's avatar
Steven Kao committed
286
287
288
289
	}

	return ret;
}
290
291
292
293
294
295
296
297
298
299
300
301
302

/*
 * Enable strict checking mode
 *
 * NVGDATA[3] strict_check ON + lock
 */
void nvg_enable_strict_checking_mode(void)
{
	uint64_t params = (uint64_t)(STRICT_CHECKING_ENABLED_SET |
				     STRICT_CHECKING_LOCKED_SET);

	nvg_set_request_data(TEGRA_NVG_CHANNEL_SECURITY_CONFIG, params);
}