blob: 1012cdf1139eeb0862fe7c01e50a4a8092ba587e [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/*
Steven Kao2cdb6782017-01-05 17:04:40 +080034 * Set the expected wake time in TSC ticks for the next low-power state the
35 * core enters.
36 *
37 * NVGDATA[0:31]: SW(RW), WAKE_TIME
38 */
39void nvg_set_wake_time(uint32_t wake_time)
40{
41 /* time (TSC ticks) until the core is expected to get a wake event */
Anthony Zhouc46150f2017-09-20 17:18:56 +080042 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_WAKE_TIME, (uint64_t)wake_time);
Steven Kao2cdb6782017-01-05 17:04:40 +080043}
44
45/*
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070046 * This request allows updating of CLUSTER_CSTATE, CCPLEX_CSTATE and
47 * SYSTEM_CSTATE values.
Steven Kao2cdb6782017-01-05 17:04:40 +080048 *
49 * NVGDATA[0:2]: SW(RW), CLUSTER_CSTATE
50 * NVGDATA[7]: SW(W), update cluster flag
Vignesh Radhakrishnan706b9fe2017-11-04 16:36:23 -070051 * NVGDATA[8:10]: SW(RW), CG_CSTATE
Steven Kao2cdb6782017-01-05 17:04:40 +080052 * NVGDATA[15]: SW(W), update ccplex flag
53 * NVGDATA[16:19]: SW(RW), SYSTEM_CSTATE
54 * NVGDATA[23]: SW(W), update system flag
55 * NVGDATA[31]: SW(W), update wake mask flag
56 * NVGDATA[32:63]: SW(RW), WAKE_MASK
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070057 */
Steven Kao2cdb6782017-01-05 17:04:40 +080058void nvg_update_cstate_info(uint32_t cluster, uint32_t ccplex,
59 uint32_t system, uint32_t wake_mask, uint8_t update_wake_mask)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070060{
61 uint64_t val = 0;
62
63 /* update CLUSTER_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +080064 if (cluster != 0U) {
65 val |= ((uint64_t)cluster & CLUSTER_CSTATE_MASK) |
66 CLUSTER_CSTATE_UPDATE_BIT;
67 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070068
69 /* update CCPLEX_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +080070 if (ccplex != 0U) {
71 val |= (((uint64_t)ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
72 CCPLEX_CSTATE_UPDATE_BIT;
73 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070074
75 /* update SYSTEM_CSTATE? */
Steven Kao2cdb6782017-01-05 17:04:40 +080076 if (system != 0U) {
77 val |= (((uint64_t)system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
78 SYSTEM_CSTATE_UPDATE_BIT;
79 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070080
81 /* update wake mask value? */
Steven Kao2cdb6782017-01-05 17:04:40 +080082 if (update_wake_mask != 0U) {
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070083 val |= CSTATE_WAKE_MASK_UPDATE_BIT;
Steven Kao2cdb6782017-01-05 17:04:40 +080084 }
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070085
86 /* set the wake mask */
Steven Kao2cdb6782017-01-05 17:04:40 +080087 val |= ((uint64_t)wake_mask & CSTATE_WAKE_MASK_CLEAR) << CSTATE_WAKE_MASK_SHIFT;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070088
89 /* set the updated cstate info */
Anthony Zhouc46150f2017-09-20 17:18:56 +080090 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_CSTATE_INFO, val);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070091}
92
Steven Kao2cdb6782017-01-05 17:04:40 +080093/*
Steven Kao2cdb6782017-01-05 17:04:40 +080094 * Return a non-zero value if the CCPLEX is able to enter SC7
95 *
96 * NVGDATA[0]: SW(R), Is allowed result
97 */
98int32_t nvg_is_sc7_allowed(void)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -070099{
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700100 /* issue command to check if SC7 is allowed */
Anthony Zhouc46150f2017-09-20 17:18:56 +0800101 nvg_set_request((uint64_t)TEGRA_NVG_CHANNEL_IS_SC7_ALLOWED);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700102
103 /* 1 = SC7 allowed, 0 = SC7 not allowed */
Steven Kao2cdb6782017-01-05 17:04:40 +0800104 return (int32_t)nvg_get_result();
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700105}
106
Steven Kao2cdb6782017-01-05 17:04:40 +0800107/*
108 * Wake an offlined logical core. Note that a core is offlined by entering
109 * a C-state where the WAKE_MASK is all 0.
110 *
111 * NVGDATA[0:3]: SW(W) logical core to online
112 */
113int32_t nvg_online_core(uint32_t core)
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700114{
Steven Kao2cdb6782017-01-05 17:04:40 +0800115 int32_t ret = 0;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700116
Steven Kao2cdb6782017-01-05 17:04:40 +0800117 /* sanity check the core ID value */
118 if (core > (uint32_t)PLATFORM_CORE_COUNT) {
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700119 ERROR("%s: unknown core id (%d)\n", __func__, core);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800120 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800121 } else {
122 /* get a core online */
Anthony Zhouc46150f2017-09-20 17:18:56 +0800123 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_ONLINE_CORE,
124 (uint64_t)core & MCE_CORE_ID_MASK);
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700125 }
126
Steven Kao2cdb6782017-01-05 17:04:40 +0800127 return ret;
Varun Wadekarecd6a5a2018-04-09 17:48:58 -0700128}
129
Steven Kao2cdb6782017-01-05 17:04:40 +0800130/*
Steven Kao2cdb6782017-01-05 17:04:40 +0800131 * MC GSC (General Security Carveout) register values are expected to be
132 * changed by TrustZone ARM code after boot.
133 *
134 * NVGDATA[0:15] SW(R) GSC enun
135 */
136int32_t nvg_update_ccplex_gsc(uint32_t gsc_idx)
137{
Varun Wadekar3abd3992020-01-03 14:21:03 -0800138 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800139
140 /* sanity check GSC ID */
Steven Kao6f373a22017-09-29 18:09:17 +0800141 if (gsc_idx > (uint32_t)TEGRA_NVG_CHANNEL_UPDATE_GSC_VPR) {
142 ERROR("%s: unknown gsc_idx (%u)\n", __func__, gsc_idx);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800143 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800144 } else {
Anthony Zhouc46150f2017-09-20 17:18:56 +0800145 nvg_set_request_data((uint64_t)TEGRA_NVG_CHANNEL_UPDATE_CCPLEX_GSC,
Varun Wadekar3abd3992020-01-03 14:21:03 -0800146 (uint64_t)gsc_idx);
Steven Kao2cdb6782017-01-05 17:04:40 +0800147 }
148
149 return ret;
150}
151
152/*
Steven Kao2cdb6782017-01-05 17:04:40 +0800153 * Cache clean and invalidate, clear TR-bit operation for all CCPLEX caches.
Steven Kao2cdb6782017-01-05 17:04:40 +0800154 */
155int32_t nvg_roc_clean_cache_trbits(void)
156{
Steven Kao238d6d22017-08-16 20:12:00 +0800157 int32_t ret = 0;
Steven Kao2cdb6782017-01-05 17:04:40 +0800158
Steven Kao238d6d22017-08-16 20:12:00 +0800159 /* check if cache flush through mts is supported */
160 if (((read_id_afr0_el1() >> ID_AFR0_EL1_CACHE_OPS_SHIFT) &
161 ID_AFR0_EL1_CACHE_OPS_MASK) == 1U) {
162 if (nvg_cache_inval_all() == 0U) {
163 ERROR("%s: failed\n", __func__);
Varun Wadekar3abd3992020-01-03 14:21:03 -0800164 ret = -ENODEV;
Steven Kao238d6d22017-08-16 20:12:00 +0800165 }
166 } else {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800167 ret = -ENOTSUP;
Steven Kao238d6d22017-08-16 20:12:00 +0800168 }
Varun Wadekar3abd3992020-01-03 14:21:03 -0800169
Steven Kao238d6d22017-08-16 20:12:00 +0800170 return ret;
Steven Kao2cdb6782017-01-05 17:04:40 +0800171}
172
173/*
174 * Set the power state for a core
175 */
176int32_t nvg_enter_cstate(uint32_t state, uint32_t wake_time)
177{
178 int32_t ret = 0;
Steven Kao40359022017-06-22 12:54:06 +0800179 uint64_t val = 0ULL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800180
181 /* check for allowed power state */
182 if ((state != (uint32_t)TEGRA_NVG_CORE_C0) &&
183 (state != (uint32_t)TEGRA_NVG_CORE_C1) &&
184 (state != (uint32_t)TEGRA_NVG_CORE_C6) &&
185 (state != (uint32_t)TEGRA_NVG_CORE_C7))
186 {
Varun Wadekar3abd3992020-01-03 14:21:03 -0800187 ERROR("%s: unknown cstate (%u)\n", __func__, state);
188 ret = -EINVAL;
Steven Kao2cdb6782017-01-05 17:04:40 +0800189 } else {
190 /* time (TSC ticks) until the core is expected to get a wake event */
191 nvg_set_wake_time(wake_time);
192
193 /* set the core cstate */
Steven Kao40359022017-06-22 12:54:06 +0800194 val = read_actlr_el1() & ~ACTLR_EL1_PMSTATE_MASK;
195 write_actlr_el1(val | (uint64_t)state);
Steven Kao2cdb6782017-01-05 17:04:40 +0800196 }
197
198 return ret;
199}
Dilan Lee4e7a63c2017-08-10 16:01:42 +0800200
Steven Kao8f4f1022017-12-13 06:39:15 +0800201#if ENABLE_STRICT_CHECKING_MODE
Dilan Lee4e7a63c2017-08-10 16:01:42 +0800202/*
203 * Enable strict checking mode
204 *
205 * NVGDATA[3] strict_check ON + lock
206 */
207void nvg_enable_strict_checking_mode(void)
208{
209 uint64_t params = (uint64_t)(STRICT_CHECKING_ENABLED_SET |
210 STRICT_CHECKING_LOCKED_SET);
211
212 nvg_set_request_data(TEGRA_NVG_CHANNEL_SECURITY_CONFIG, params);
213}
Steven Kao8f4f1022017-12-13 06:39:15 +0800214#endif
Vignesh Radhakrishnan3ad79832017-12-11 13:17:58 -0800215
216/*
217 * Request a reboot
218 *
219 * NVGDATA[0]: reboot command
220 */
221void nvg_system_reboot(void)
222{
223 /* issue command for reboot */
224 nvg_set_request_data(TEGRA_NVG_CHANNEL_SHUTDOWN, TEGRA_NVG_REBOOT);
225}
226
227/*
228 * Request a shutdown
229 *
230 * NVGDATA[0]: shutdown command
231 */
232void nvg_system_shutdown(void)
233{
234 /* issue command for shutdown */
235 nvg_set_request_data(TEGRA_NVG_CHANNEL_SHUTDOWN, TEGRA_NVG_SHUTDOWN);
236}