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

7
8
9
#include <assert.h>
#include <errno.h>

10
11
12
13
14
#include <arch.h>
#include <arch_helpers.h>
#include <common/debug.h>
#include <denver.h>
#include <lib/mmio.h>
15

16
#include <mce_private.h>
Steven Kao's avatar
Steven Kao committed
17
18
#include <platform_def.h>
#include <t194_nvg.h>
19
#include <tegra_private.h>
20

21
22
#define	ID_AFR0_EL1_CACHE_OPS_SHIFT	U(12)
#define	ID_AFR0_EL1_CACHE_OPS_MASK	U(0xF)
Steven Kao's avatar
Steven Kao committed
23
24
25
26
27
28
29
/*
 * 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)
30
{
31
	nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_VERSION);
32

Steven Kao's avatar
Steven Kao committed
33
34
	return (uint64_t)nvg_get_result();
}
35

Steven Kao's avatar
Steven Kao committed
36
37
38
39
40
41
42
43
44
/*
 * 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 */
45
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_WAKE_TIME, (uint64_t)wake_time);
Steven Kao's avatar
Steven Kao committed
46
47
}

48
49
50
/*
 * This request allows updating of CLUSTER_CSTATE, CCPLEX_CSTATE and
 * SYSTEM_CSTATE values.
Steven Kao's avatar
Steven Kao committed
51
52
53
 *
 * NVGDATA[0:2]: SW(RW), CLUSTER_CSTATE
 * NVGDATA[7]: SW(W), update cluster flag
54
 * NVGDATA[8:10]: SW(RW), CG_CSTATE
Steven Kao's avatar
Steven Kao committed
55
56
57
58
59
 * 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
60
 */
Steven Kao's avatar
Steven Kao committed
61
62
void nvg_update_cstate_info(uint32_t cluster, uint32_t ccplex,
		uint32_t system, uint32_t wake_mask, uint8_t update_wake_mask)
63
64
65
66
{
	uint64_t val = 0;

	/* update CLUSTER_CSTATE? */
Steven Kao's avatar
Steven Kao committed
67
68
69
70
	if (cluster != 0U) {
		val |= ((uint64_t)cluster & CLUSTER_CSTATE_MASK) |
				CLUSTER_CSTATE_UPDATE_BIT;
	}
71
72

	/* update CCPLEX_CSTATE? */
Steven Kao's avatar
Steven Kao committed
73
74
75
76
	if (ccplex != 0U) {
		val |= (((uint64_t)ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
				CCPLEX_CSTATE_UPDATE_BIT;
	}
77
78

	/* update SYSTEM_CSTATE? */
Steven Kao's avatar
Steven Kao committed
79
80
81
82
	if (system != 0U) {
		val |= (((uint64_t)system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
				SYSTEM_CSTATE_UPDATE_BIT;
	}
83
84

	/* update wake mask value? */
Steven Kao's avatar
Steven Kao committed
85
	if (update_wake_mask != 0U) {
86
		val |= CSTATE_WAKE_MASK_UPDATE_BIT;
Steven Kao's avatar
Steven Kao committed
87
	}
88
89

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

	/* set the updated cstate info */
93
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CSTATE_INFO, val);
94
95
}

Steven Kao's avatar
Steven Kao committed
96
97
98
99
100
101
/*
 * 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)
102
{
Steven Kao's avatar
Steven Kao committed
103
	/* issue command to check if SC7 is allowed */
104
	nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_IS_SC7_ALLOWED);
Steven Kao's avatar
Steven Kao committed
105
106
107

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

Steven Kao's avatar
Steven Kao committed
110
111
112
113
114
115
116
/*
 * 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)
117
{
Steven Kao's avatar
Steven Kao committed
118
	int32_t ret = 0;
119

Steven Kao's avatar
Steven Kao committed
120
121
122
	/* sanity check the core ID value */
	if (core > (uint32_t)PLATFORM_CORE_COUNT) {
		ERROR("%s: unknown core id (%d)\n", __func__, core);
123
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
124
125
	} else {
		/* get a core online */
126
127
		nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_ONLINE_CORE,
					(uint64_t)core & MCE_CORE_ID_MASK);
128
129
	}

Steven Kao's avatar
Steven Kao committed
130
131
132
133
134
135
136
137
138
139
	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)
140
{
141
	int32_t ret = 0;
Steven Kao's avatar
Steven Kao committed
142
143

	/* sanity check GSC ID */
144
145
	if (gsc_idx > (uint32_t)TEGRA_NVG_CHANNEL_UPDATE_GSC_VPR) {
		ERROR("%s: unknown gsc_idx (%u)\n", __func__, gsc_idx);
146
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
147
	} else {
148
		nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_UPDATE_CCPLEX_GSC,
149
				     (uint64_t)gsc_idx);
150
151
	}

Steven Kao's avatar
Steven Kao committed
152
153
	return ret;
}
154

Steven Kao's avatar
Steven Kao committed
155
156
157
158
159
/*
 * Cache clean and invalidate, clear TR-bit operation for all CCPLEX caches.
 */
int32_t nvg_roc_clean_cache_trbits(void)
{
160
	int32_t ret = 0;
161

162
163
164
165
166
	/* 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__);
167
			ret = -ENODEV;
168
169
		}
	} else {
170
		ret = -ENOTSUP;
171
	}
172

173
	return ret;
174
}
Steven Kao's avatar
Steven Kao committed
175
176
177
178
179
180
181

/*
 * Set the power state for a core
 */
int32_t nvg_enter_cstate(uint32_t state, uint32_t wake_time)
{
	int32_t ret = 0;
182
	uint64_t val = 0ULL;
Steven Kao's avatar
Steven Kao committed
183
184
185
186
187
188
189

	/* 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))
	{
190
191
		ERROR("%s: unknown cstate (%u)\n", __func__, state);
		ret = -EINVAL;
Steven Kao's avatar
Steven Kao committed
192
193
194
195
196
	} else {
		/* time (TSC ticks) until the core is expected to get a wake event */
		nvg_set_wake_time(wake_time);

		/* set the core cstate */
197
198
		val = read_actlr_el1() & ~ACTLR_EL1_PMSTATE_MASK;
		write_actlr_el1(val | (uint64_t)state);
Steven Kao's avatar
Steven Kao committed
199
200
201
202
	}

	return ret;
}
203

204
#if ENABLE_STRICT_CHECKING_MODE
205
206
207
208
209
210
211
212
213
214
/*
 * 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);

215
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_SECURITY_CONFIG, params);
216
}
217
218
219
220
221
222
223
224
225

void nvg_verify_strict_checking_mode(void)
{
	uint64_t params = (uint64_t)(STRICT_CHECKING_ENABLED_SET |
				     STRICT_CHECKING_LOCKED_SET);

	nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_SECURITY_CONFIG);
	assert(params == (uint64_t)nvg_get_result());
}
226
#endif
227
228
229
230
231
232
233
234
235

/*
 * Request a reboot
 *
 * NVGDATA[0]: reboot command
 */
void nvg_system_reboot(void)
{
	/* issue command for reboot */
236
237
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_SHUTDOWN,
			     (uint64_t)TEGRA_NVG_REBOOT);
238
239
240
241
242
243
244
245
246
247
}

/*
 * Request a shutdown
 *
 * NVGDATA[0]: shutdown command
 */
void nvg_system_shutdown(void)
{
	/* issue command for shutdown */
248
249
	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_SHUTDOWN,
			     (uint64_t)TEGRA_NVG_SHUTDOWN);
250
}
251
252
253
254
255
256
257
258
259
260
261
262

/*
 * Request to clear CCPLEX->HSM correctable error signal.
 * NVGDATA[1]: A write of 1 clears the CCPLEX->HSM correctable error signal,
 *             A write of 0 has no effect.
 */
void nvg_clear_hsm_corr_status(void)
{
	nvg_hsm_error_ctrl_channel_t status = { .bits = { .corr = 1U, }, };

	nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_HSM_ERROR_CTRL, status.flat);
}