blob: 536ed57c009fda325b1d96a4d65bcf1f32fd3f77 [file] [log] [blame]
Varun Wadekarecd6a5a2018-04-09 17:48:58 -07001/*
Varun Wadekar3abd3992020-01-03 14:21:03 -08002 * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
Varun Wadekarecd6a5a2018-04-09 17:48:58 -07003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <common/debug.h>
10#include <denver.h>
Steven Kao2cdb6782017-01-05 17:04:40 +080011#include <errno.h>
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070012#include <lib/mmio.h>
13#include <mce_private.h>
Steven Kao2cdb6782017-01-05 17:04:40 +080014#include <platform_def.h>
15#include <t194_nvg.h>
Steven Kao40359022017-06-22 12:54:06 +080016#include <tegra_private.h>
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070017
Steven Kao238d6d22017-08-16 20:12:00 +080018#define ID_AFR0_EL1_CACHE_OPS_SHIFT 12
19#define ID_AFR0_EL1_CACHE_OPS_MASK 0xFU
Steven Kao2cdb6782017-01-05 17:04:40 +080020/*
21 * Reports the major and minor version of this interface.
22 *
23 * NVGDATA[0:31]: SW(R) Minor Version
24 * NVGDATA[32:63]: SW(R) Major Version
25 */
26uint64_t nvg_get_version(void)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070027{
Anthony Zhouc46150f2017-09-20 17:18:56 +080028 nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_VERSION);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070029
Steven Kao2cdb6782017-01-05 17:04:40 +080030 return (uint64_t)nvg_get_result();
31}
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070032
Steven Kao2cdb6782017-01-05 17:04:40 +080033/*
34 * Enable the perf per watt mode.
35 *
36 * NVGDATA[0]: SW(RW), 1 = enable perf per watt mode
37 */
38int32_t nvg_enable_power_perf_mode(void)
39{
Anthony Zhouc46150f2017-09-20 17:18:56 +080040 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_PERF, 1U);
Steven Kao2cdb6782017-01-05 17:04:40 +080041
42 return 0;
43}
44
45/*
46 * Disable the perf per watt mode.
47 *
48 * NVGDATA[0]: SW(RW), 0 = disable perf per watt mode
49 */
50int32_t nvg_disable_power_perf_mode(void)
51{
Anthony Zhouc46150f2017-09-20 17:18:56 +080052 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_PERF, 0U);
Steven Kao2cdb6782017-01-05 17:04:40 +080053
54 return 0;
55}
56
57/*
58 * Enable the battery saver mode.
59 *
60 * NVGDATA[2]: SW(RW), 1 = enable battery saver mode
61 */
62int32_t nvg_enable_power_saver_modes(void)
63{
Anthony Zhouc46150f2017-09-20 17:18:56 +080064 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_MODES, 1U);
Steven Kao2cdb6782017-01-05 17:04:40 +080065
66 return 0;
67}
68
69/*
70 * Disable the battery saver mode.
71 *
72 * NVGDATA[2]: SW(RW), 0 = disable battery saver mode
73 */
74int32_t nvg_disable_power_saver_modes(void)
75{
Anthony Zhouc46150f2017-09-20 17:18:56 +080076 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_POWER_MODES, 0U);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070077
78 return 0;
79}
80
81/*
Steven Kao2cdb6782017-01-05 17:04:40 +080082 * Set the expected wake time in TSC ticks for the next low-power state the
83 * core enters.
84 *
85 * NVGDATA[0:31]: SW(RW), WAKE_TIME
86 */
87void nvg_set_wake_time(uint32_t wake_time)
88{
89 /* time (TSC ticks) until the core is expected to get a wake event */
Anthony Zhouc46150f2017-09-20 17:18:56 +080090 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_WAKE_TIME, (uint64_t)wake_time);
Steven Kao2cdb6782017-01-05 17:04:40 +080091}
92
93/*
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070094 * This request allows updating of CLUSTER_CSTATE, CCPLEX_CSTATE and
95 * SYSTEM_CSTATE values.
Steven Kao2cdb6782017-01-05 17:04:40 +080096 *
97 * NVGDATA[0:2]: SW(RW), CLUSTER_CSTATE
98 * NVGDATA[7]: SW(W), update cluster flag
Vignesh Radhakrishnan706b9fe2017-11-04 16:36:23 -070099 * NVGDATA[8:10]: SW(RW), CG_CSTATE
Steven Kao2cdb6782017-01-05 17:04:40 +0800100 * NVGDATA[15]: SW(W), update ccplex flag
101 * NVGDATA[16:19]: SW(RW), SYSTEM_CSTATE
102 * NVGDATA[23]: SW(W), update system flag
103 * NVGDATA[31]: SW(W), update wake mask flag
104 * NVGDATA[32:63]: SW(RW), WAKE_MASK
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700105 */
Steven Kao2cdb6782017-01-05 17:04:40 +0800106void nvg_update_cstate_info(uint32_t cluster, uint32_t ccplex,
107 uint32_t system, uint32_t wake_mask, uint8_t update_wake_mask)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700108{
109 uint64_t val = 0;
110
111 /* update CLUSTER_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +0800112 if (cluster != 0U) {
113 val |= ((uint64_t)cluster & CLUSTER_CSTATE_MASK) |
114 CLUSTER_CSTATE_UPDATE_BIT;
115 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700116
117 /* update CCPLEX_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +0800118 if (ccplex != 0U) {
119 val |= (((uint64_t)ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
120 CCPLEX_CSTATE_UPDATE_BIT;
121 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700122
123 /* update SYSTEM_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +0800124 if (system != 0U) {
125 val |= (((uint64_t)system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
126 SYSTEM_CSTATE_UPDATE_BIT;
127 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700128
129 /* update wake mask value? */
Steven Kao2cdb6782017-01-05 17:04:40 +0800130 if (update_wake_mask != 0U) {
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700131 val |= CSTATE_WAKE_MASK_UPDATE_BIT;
Steven Kao2cdb6782017-01-05 17:04:40 +0800132 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700133
134 /* set the wake mask */
Steven Kao2cdb6782017-01-05 17:04:40 +0800135 val |= ((uint64_t)wake_mask & CSTATE_WAKE_MASK_CLEAR) << CSTATE_WAKE_MASK_SHIFT;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700136
137 /* set the updated cstate info */
Anthony Zhouc46150f2017-09-20 17:18:56 +0800138 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CSTATE_INFO, val);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700139}
140
Steven Kao2cdb6782017-01-05 17:04:40 +0800141/*
Steven Kao2cdb6782017-01-05 17:04:40 +0800142 * Return a non-zero value if the CCPLEX is able to enter SC7
143 *
144 * NVGDATA[0]: SW(R), Is allowed result
145 */
146int32_t nvg_is_sc7_allowed(void)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700147{
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700148 /* issue command to check if SC7 is allowed */
Anthony Zhouc46150f2017-09-20 17:18:56 +0800149 nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_IS_SC7_ALLOWED);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700150
151 /* 1 = SC7 allowed, 0 = SC7 not allowed */
Steven Kao2cdb6782017-01-05 17:04:40 +0800152 return (int32_t)nvg_get_result();
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700153}
154
Steven Kao2cdb6782017-01-05 17:04:40 +0800155/*
156 * Wake an offlined logical core. Note that a core is offlined by entering
157 * a C-state where the WAKE_MASK is all 0.
158 *
159 * NVGDATA[0:3]: SW(W) logical core to online
160 */
161int32_t nvg_online_core(uint32_t core)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700162{
Steven Kao2cdb6782017-01-05 17:04:40 +0800163 int32_t ret = 0;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700164
Steven Kao2cdb6782017-01-05 17:04:40 +0800165 /* sanity check the core ID value */
166 if (core > (uint32_t)PLATFORM_CORE_COUNT) {
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700167 ERROR("%s: unknown core id (%d)\n", __func__, core);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800168 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800169 } else {
170 /* get a core online */
Anthony Zhouc46150f2017-09-20 17:18:56 +0800171 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_ONLINE_CORE,
172 (uint64_t)core & MCE_CORE_ID_MASK);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700173 }
174
Steven Kao2cdb6782017-01-05 17:04:40 +0800175 return ret;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700176}
177
Steven Kao2cdb6782017-01-05 17:04:40 +0800178/*
Steven Kao2cdb6782017-01-05 17:04:40 +0800179 * MC GSC (General Security Carveout) register values are expected to be
180 * changed by TrustZone ARM code after boot.
181 *
182 * NVGDATA[0:15] SW(R) GSC enun
183 */
184int32_t nvg_update_ccplex_gsc(uint32_t gsc_idx)
185{
Varun Wadekar3abd3992020-01-03 14:21:03 -0800186 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800187
188 /* sanity check GSC ID */
Steven Kao6f373a22017-09-29 18:09:17 +0800189 if (gsc_idx > (uint32_t)TEGRA_NVG_CHANNEL_UPDATE_GSC_VPR) {
190 ERROR("%s: unknown gsc_idx (%u)\n", __func__, gsc_idx);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800191 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800192 } else {
Anthony Zhouc46150f2017-09-20 17:18:56 +0800193 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_UPDATE_CCPLEX_GSC,
Varun Wadekar3abd3992020-01-03 14:21:03 -0800194 (uint64_t)gsc_idx);
Steven Kao2cdb6782017-01-05 17:04:40 +0800195 }
196
197 return ret;
198}
199
200/*
201 * Cache clean operation for all CCPLEX caches.
Steven Kao2cdb6782017-01-05 17:04:40 +0800202 */
203int32_t nvg_roc_clean_cache(void)
204{
Steven Kao238d6d22017-08-16 20:12:00 +0800205 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800206
Steven Kao238d6d22017-08-16 20:12:00 +0800207 /* check if cache flush through mts is supported */
208 if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
209 ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
210 if (nvg_cache_clean() == 0U) {
211 ERROR("%s: failed\n", __func__);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800212 ret = -ENODEV;
Steven Kao238d6d22017-08-16 20:12:00 +0800213 }
214 } else {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800215 ret = -ENOTSUP;
Steven Kao238d6d22017-08-16 20:12:00 +0800216 }
Varun Wadekar3abd3992020-01-03 14:21:03 -0800217
Steven Kao238d6d22017-08-16 20:12:00 +0800218 return ret;
Steven Kao2cdb6782017-01-05 17:04:40 +0800219}
220
221/*
222 * Cache clean and invalidate operation for all CCPLEX caches.
Steven Kao2cdb6782017-01-05 17:04:40 +0800223 */
224int32_t nvg_roc_flush_cache(void)
225{
Steven Kao238d6d22017-08-16 20:12:00 +0800226 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800227
Steven Kao238d6d22017-08-16 20:12:00 +0800228 /* check if cache flush through mts is supported */
229 if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
230 ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
231 if (nvg_cache_clean_inval() == 0U) {
232 ERROR("%s: failed\n", __func__);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800233 ret = -ENODEV;
Steven Kao238d6d22017-08-16 20:12:00 +0800234 }
235 } else {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800236 ret = -ENOTSUP;
Steven Kao238d6d22017-08-16 20:12:00 +0800237 }
Varun Wadekar3abd3992020-01-03 14:21:03 -0800238
Steven Kao238d6d22017-08-16 20:12:00 +0800239 return ret;
Steven Kao2cdb6782017-01-05 17:04:40 +0800240}
241
242/*
243 * Cache clean and invalidate, clear TR-bit operation for all CCPLEX caches.
Steven Kao2cdb6782017-01-05 17:04:40 +0800244 */
245int32_t nvg_roc_clean_cache_trbits(void)
246{
Steven Kao238d6d22017-08-16 20:12:00 +0800247 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800248
Steven Kao238d6d22017-08-16 20:12:00 +0800249 /* check if cache flush through mts is supported */
250 if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
251 ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
252 if (nvg_cache_inval_all() == 0U) {
253 ERROR("%s: failed\n", __func__);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800254 ret = -ENODEV;
Steven Kao238d6d22017-08-16 20:12:00 +0800255 }
256 } else {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800257 ret = -ENOTSUP;
Steven Kao238d6d22017-08-16 20:12:00 +0800258 }
Varun Wadekar3abd3992020-01-03 14:21:03 -0800259
Steven Kao238d6d22017-08-16 20:12:00 +0800260 return ret;
Steven Kao2cdb6782017-01-05 17:04:40 +0800261}
262
263/*
264 * Set the power state for a core
265 */
266int32_t nvg_enter_cstate(uint32_t state, uint32_t wake_time)
267{
268 int32_t ret = 0;
Steven Kao40359022017-06-22 12:54:06 +0800269 uint64_t val = 0ULL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800270
271 /* check for allowed power state */
272 if ((state != (uint32_t)TEGRA_NVG_CORE_C0) &&
273 (state != (uint32_t)TEGRA_NVG_CORE_C1) &&
274 (state != (uint32_t)TEGRA_NVG_CORE_C6) &&
275 (state != (uint32_t)TEGRA_NVG_CORE_C7))
276 {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800277 ERROR("%s: unknown cstate (%u)\n", __func__, state);
278 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800279 } else {
280 /* time (TSC ticks) until the core is expected to get a wake event */
281 nvg_set_wake_time(wake_time);
282
283 /* set the core cstate */
Steven Kao40359022017-06-22 12:54:06 +0800284 val = read_actlr_el1() & ~ACTLR_EL1_PMSTATE_MASK;
285 write_actlr_el1(val | (uint64_t)state);
Steven Kao2cdb6782017-01-05 17:04:40 +0800286 }
287
288 return ret;
289}
Dilan Lee4e7a63c2017-08-10 16:01:42 +0800290
291/*
292 * Enable strict checking mode
293 *
294 * NVGDATA[3] strict_check ON + lock
295 */
296void nvg_enable_strict_checking_mode(void)
297{
298 uint64_t params = (uint64_t)(STRICT_CHECKING_ENABLED_SET |
299 STRICT_CHECKING_LOCKED_SET);
300
301 nvg_set_request_data(TEGRA_NVG_CHANNEL_SECURITY_CONFIG, params);
302}