blob: 599e46e74feeecc56fe595bf23dad45bc488dfa4 [file] [log] [blame]
Varun Wadekara0352ab2017-03-14 14:24:35 -07001/*
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +01002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Varun Wadekara0352ab2017-03-14 14:24:35 -07003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekara0352ab2017-03-14 14:24:35 -07005 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <errno.h>
9
Varun Wadekara0352ab2017-03-14 14:24:35 -070010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
13#include <drivers/delay_timer.h>
Varun Wadekara0352ab2017-03-14 14:24:35 -070014#include <denver.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <lib/mmio.h>
16#include <plat/common/platform.h>
17
Varun Wadekarb5568282016-12-13 18:04:35 -080018#include <mce_private.h>
Varun Wadekara0352ab2017-03-14 14:24:35 -070019#include <t18x_ari.h>
20
21/*******************************************************************************
22 * Register offsets for ARI request/results
23 ******************************************************************************/
Anthony Zhou1ab31402017-03-06 16:06:45 +080024#define ARI_REQUEST 0x0U
25#define ARI_REQUEST_EVENT_MASK 0x4U
26#define ARI_STATUS 0x8U
27#define ARI_REQUEST_DATA_LO 0xCU
28#define ARI_REQUEST_DATA_HI 0x10U
29#define ARI_RESPONSE_DATA_LO 0x14U
30#define ARI_RESPONSE_DATA_HI 0x18U
Varun Wadekara0352ab2017-03-14 14:24:35 -070031
32/* Status values for the current request */
Steven Kaod346dca2016-12-23 16:17:18 +080033#define ARI_REQ_PENDING 1U
34#define ARI_REQ_ONGOING 3U
35#define ARI_REQUEST_VALID_BIT (1U << 8)
36#define ARI_EVT_MASK_STANDBYWFI_BIT (1U << 7)
37
38/* default timeout (ms) to wait for ARI completion */
39#define ARI_MAX_RETRY_COUNT 2000
Varun Wadekara0352ab2017-03-14 14:24:35 -070040
41/*******************************************************************************
42 * ARI helper functions
43 ******************************************************************************/
44static inline uint32_t ari_read_32(uint32_t ari_base, uint32_t reg)
45{
Anthony Zhou1ab31402017-03-06 16:06:45 +080046 return mmio_read_32((uint64_t)ari_base + (uint64_t)reg);
Varun Wadekara0352ab2017-03-14 14:24:35 -070047}
48
49static inline void ari_write_32(uint32_t ari_base, uint32_t val, uint32_t reg)
50{
Anthony Zhou1ab31402017-03-06 16:06:45 +080051 mmio_write_32((uint64_t)ari_base + (uint64_t)reg, val);
Varun Wadekara0352ab2017-03-14 14:24:35 -070052}
53
54static inline uint32_t ari_get_request_low(uint32_t ari_base)
55{
56 return ari_read_32(ari_base, ARI_REQUEST_DATA_LO);
57}
58
59static inline uint32_t ari_get_request_high(uint32_t ari_base)
60{
61 return ari_read_32(ari_base, ARI_REQUEST_DATA_HI);
62}
63
64static inline uint32_t ari_get_response_low(uint32_t ari_base)
65{
66 return ari_read_32(ari_base, ARI_RESPONSE_DATA_LO);
67}
68
69static inline uint32_t ari_get_response_high(uint32_t ari_base)
70{
71 return ari_read_32(ari_base, ARI_RESPONSE_DATA_HI);
72}
73
74static inline void ari_clobber_response(uint32_t ari_base)
75{
76 ari_write_32(ari_base, 0, ARI_RESPONSE_DATA_LO);
77 ari_write_32(ari_base, 0, ARI_RESPONSE_DATA_HI);
78}
79
Anthony Zhou1ab31402017-03-06 16:06:45 +080080static int32_t ari_request_wait(uint32_t ari_base, uint32_t evt_mask, uint32_t req,
Varun Wadekara0352ab2017-03-14 14:24:35 -070081 uint32_t lo, uint32_t hi)
82{
Steven Kaod346dca2016-12-23 16:17:18 +080083 uint32_t retries = ARI_MAX_RETRY_COUNT;
84 uint32_t status;
Anthony Zhou1ab31402017-03-06 16:06:45 +080085 int32_t ret = 0;
Varun Wadekara0352ab2017-03-14 14:24:35 -070086
87 /* program the request, event_mask, hi and lo registers */
88 ari_write_32(ari_base, lo, ARI_REQUEST_DATA_LO);
89 ari_write_32(ari_base, hi, ARI_REQUEST_DATA_HI);
90 ari_write_32(ari_base, evt_mask, ARI_REQUEST_EVENT_MASK);
91 ari_write_32(ari_base, req | ARI_REQUEST_VALID_BIT, ARI_REQUEST);
92
93 /*
94 * For commands that have an event trigger, we should bypass
95 * ARI_STATUS polling, since MCE is waiting for SW to trigger
96 * the event.
97 */
Anthony Zhou1ab31402017-03-06 16:06:45 +080098 if (evt_mask != 0U) {
99 ret = 0;
100 } else {
101 /* For shutdown/reboot commands, we dont have to check for timeouts */
Anthony Zhou3b804502017-06-26 20:33:34 +0800102 if ((req == TEGRA_ARI_MISC_CCPLEX) &&
103 ((lo == TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF) ||
104 (lo == TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT))) {
Anthony Zhou1ab31402017-03-06 16:06:45 +0800105 ret = 0;
106 } else {
107 /*
108 * Wait for the command response for not more than the timeout
109 */
110 while (retries != 0U) {
Varun Wadekara0352ab2017-03-14 14:24:35 -0700111
Anthony Zhou1ab31402017-03-06 16:06:45 +0800112 /* read the command status */
113 status = ari_read_32(ari_base, ARI_STATUS);
114 if ((status & (ARI_REQ_ONGOING | ARI_REQ_PENDING)) == 0U) {
115 break;
116 }
Steven Kaod346dca2016-12-23 16:17:18 +0800117
Anthony Zhou1ab31402017-03-06 16:06:45 +0800118 /* delay 1 ms */
119 mdelay(1);
Steven Kaod346dca2016-12-23 16:17:18 +0800120
Anthony Zhou1ab31402017-03-06 16:06:45 +0800121 /* decrement the retry count */
122 retries--;
123 }
Steven Kaod346dca2016-12-23 16:17:18 +0800124
Anthony Zhou1ab31402017-03-06 16:06:45 +0800125 /* assert if the command timed out */
126 if (retries == 0U) {
127 ERROR("ARI request timed out: req %d on CPU %d\n",
128 req, plat_my_core_pos());
129 assert(retries != 0U);
130 }
131 }
Steven Kaod346dca2016-12-23 16:17:18 +0800132 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700133
Anthony Zhou1ab31402017-03-06 16:06:45 +0800134 return ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700135}
136
Anthony Zhou1ab31402017-03-06 16:06:45 +0800137int32_t ari_enter_cstate(uint32_t ari_base, uint32_t state, uint32_t wake_time)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700138{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800139 int32_t ret = 0;
140
Varun Wadekara0352ab2017-03-14 14:24:35 -0700141 /* check for allowed power state */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800142 if ((state != TEGRA_ARI_CORE_C0) &&
143 (state != TEGRA_ARI_CORE_C1) &&
144 (state != TEGRA_ARI_CORE_C6) &&
145 (state != TEGRA_ARI_CORE_C7)) {
Varun Wadekara0352ab2017-03-14 14:24:35 -0700146 ERROR("%s: unknown cstate (%d)\n", __func__, state);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800147 ret = EINVAL;
148 } else {
149 /* clean the previous response state */
150 ari_clobber_response(ari_base);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700151
Anthony Zhou1ab31402017-03-06 16:06:45 +0800152 /* Enter the cstate, to be woken up after wake_time (TSC ticks) */
153 ret = ari_request_wait(ari_base, ARI_EVT_MASK_STANDBYWFI_BIT,
Varun Wadekara0352ab2017-03-14 14:24:35 -0700154 TEGRA_ARI_ENTER_CSTATE, state, wake_time);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800155 }
156
157 return ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700158}
159
Anthony Zhou1ab31402017-03-06 16:06:45 +0800160int32_t ari_update_cstate_info(uint32_t ari_base, uint32_t cluster, uint32_t ccplex,
Varun Wadekara0352ab2017-03-14 14:24:35 -0700161 uint32_t system, uint8_t sys_state_force, uint32_t wake_mask,
162 uint8_t update_wake_mask)
163{
Anthony Zhou3b804502017-06-26 20:33:34 +0800164 uint64_t val = 0U;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700165
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700166 /* clean the previous response state */
167 ari_clobber_response(ari_base);
168
Varun Wadekara0352ab2017-03-14 14:24:35 -0700169 /* update CLUSTER_CSTATE? */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800170 if (cluster != 0U) {
Anthony Zhou3b804502017-06-26 20:33:34 +0800171 val |= (cluster & CLUSTER_CSTATE_MASK) |
172 CLUSTER_CSTATE_UPDATE_BIT;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800173 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700174
175 /* update CCPLEX_CSTATE? */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800176 if (ccplex != 0U) {
Anthony Zhou3b804502017-06-26 20:33:34 +0800177 val |= ((ccplex & CCPLEX_CSTATE_MASK) << CCPLEX_CSTATE_SHIFT) |
178 CCPLEX_CSTATE_UPDATE_BIT;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800179 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700180
181 /* update SYSTEM_CSTATE? */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800182 if (system != 0U) {
Anthony Zhou3b804502017-06-26 20:33:34 +0800183 val |= ((system & SYSTEM_CSTATE_MASK) << SYSTEM_CSTATE_SHIFT) |
184 (((uint64_t)sys_state_force << SYSTEM_CSTATE_FORCE_UPDATE_SHIFT) |
185 SYSTEM_CSTATE_UPDATE_BIT);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800186 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700187
188 /* update wake mask value? */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800189 if (update_wake_mask != 0U) {
Anthony Zhou3b804502017-06-26 20:33:34 +0800190 val |= CSTATE_WAKE_MASK_UPDATE_BIT;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800191 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700192
193 /* set the updated cstate info */
Anthony Zhou3b804502017-06-26 20:33:34 +0800194 return ari_request_wait(ari_base, 0U, TEGRA_ARI_UPDATE_CSTATE_INFO,
195 (uint32_t)val, wake_mask);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700196}
197
Anthony Zhou1ab31402017-03-06 16:06:45 +0800198int32_t ari_update_crossover_time(uint32_t ari_base, uint32_t type, uint32_t time)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700199{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800200 int32_t ret = 0;
201
Varun Wadekara0352ab2017-03-14 14:24:35 -0700202 /* sanity check crossover type */
203 if ((type == TEGRA_ARI_CROSSOVER_C1_C6) ||
Anthony Zhou1ab31402017-03-06 16:06:45 +0800204 (type > TEGRA_ARI_CROSSOVER_CCP3_SC1)) {
205 ret = EINVAL;
206 } else {
207 /* clean the previous response state */
208 ari_clobber_response(ari_base);
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700209
Anthony Zhou1ab31402017-03-06 16:06:45 +0800210 /* update crossover threshold time */
211 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_UPDATE_CROSSOVER,
Varun Wadekara0352ab2017-03-14 14:24:35 -0700212 type, time);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800213 }
214
215 return ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700216}
217
218uint64_t ari_read_cstate_stats(uint32_t ari_base, uint32_t state)
219{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800220 int32_t ret;
221 uint64_t result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700222
223 /* sanity check crossover type */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800224 if (state == 0U) {
225 result = EINVAL;
226 } else {
227 /* clean the previous response state */
228 ari_clobber_response(ari_base);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700229
Anthony Zhou1ab31402017-03-06 16:06:45 +0800230 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_CSTATE_STATS, state, 0U);
231 if (ret != 0) {
232 result = EINVAL;
233 } else {
234 result = (uint64_t)ari_get_response_low(ari_base);
235 }
236 }
237 return result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700238}
239
Anthony Zhou1ab31402017-03-06 16:06:45 +0800240int32_t ari_write_cstate_stats(uint32_t ari_base, uint32_t state, uint32_t stats)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700241{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700242 /* clean the previous response state */
243 ari_clobber_response(ari_base);
244
Varun Wadekara0352ab2017-03-14 14:24:35 -0700245 /* write the cstate stats */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800246 return ari_request_wait(ari_base, 0U, TEGRA_ARI_WRITE_CSTATE_STATS, state,
Varun Wadekara0352ab2017-03-14 14:24:35 -0700247 stats);
248}
249
250uint64_t ari_enumeration_misc(uint32_t ari_base, uint32_t cmd, uint32_t data)
251{
252 uint64_t resp;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800253 int32_t ret;
254 uint32_t local_data = data;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700255
256 /* clean the previous response state */
257 ari_clobber_response(ari_base);
258
259 /* ARI_REQUEST_DATA_HI is reserved for commands other than 'ECHO' */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800260 if (cmd != TEGRA_ARI_MISC_ECHO) {
261 local_data = 0U;
262 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700263
Anthony Zhou1ab31402017-03-06 16:06:45 +0800264 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_MISC, cmd, local_data);
265 if (ret != 0) {
266 resp = (uint64_t)ret;
267 } else {
268 /* get the command response */
269 resp = ari_get_response_low(ari_base);
270 resp |= ((uint64_t)ari_get_response_high(ari_base) << 32);
271 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700272
273 return resp;
274}
275
Anthony Zhou1ab31402017-03-06 16:06:45 +0800276int32_t ari_is_ccx_allowed(uint32_t ari_base, uint32_t state, uint32_t wake_time)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700277{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800278 int32_t ret;
279 uint32_t result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700280
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700281 /* clean the previous response state */
282 ari_clobber_response(ari_base);
283
Anthony Zhou1ab31402017-03-06 16:06:45 +0800284 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_IS_CCX_ALLOWED, state & 0x7U,
Varun Wadekara0352ab2017-03-14 14:24:35 -0700285 wake_time);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800286 if (ret != 0) {
Varun Wadekara0352ab2017-03-14 14:24:35 -0700287 ERROR("%s: failed (%d)\n", __func__, ret);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800288 result = 0U;
289 } else {
290 result = ari_get_response_low(ari_base) & 0x1U;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700291 }
292
293 /* 1 = CCx allowed, 0 = CCx not allowed */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800294 return (int32_t)result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700295}
296
Anthony Zhou1ab31402017-03-06 16:06:45 +0800297int32_t ari_is_sc7_allowed(uint32_t ari_base, uint32_t state, uint32_t wake_time)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700298{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800299 int32_t ret, result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700300
301 /* check for allowed power state */
Anthony Zhou3b804502017-06-26 20:33:34 +0800302 if ((state != TEGRA_ARI_CORE_C0) && (state != TEGRA_ARI_CORE_C1) &&
303 (state != TEGRA_ARI_CORE_C6) && (state != TEGRA_ARI_CORE_C7)) {
Varun Wadekara0352ab2017-03-14 14:24:35 -0700304 ERROR("%s: unknown cstate (%d)\n", __func__, state);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800305 result = EINVAL;
306 } else {
307 /* clean the previous response state */
308 ari_clobber_response(ari_base);
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700309
Anthony Zhou1ab31402017-03-06 16:06:45 +0800310 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_IS_SC7_ALLOWED, state,
311 wake_time);
312 if (ret != 0) {
313 ERROR("%s: failed (%d)\n", __func__, ret);
314 result = 0;
315 } else {
316 /* 1 = SC7 allowed, 0 = SC7 not allowed */
317 result = (ari_get_response_low(ari_base) != 0U) ? 1 : 0;
318 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700319 }
320
Anthony Zhou1ab31402017-03-06 16:06:45 +0800321 return result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700322}
323
Anthony Zhou1ab31402017-03-06 16:06:45 +0800324int32_t ari_online_core(uint32_t ari_base, uint32_t core)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700325{
Anthony Zhou3b804502017-06-26 20:33:34 +0800326 uint64_t cpu = read_mpidr() & (MPIDR_CPU_MASK);
327 uint64_t cluster = (read_mpidr() & (MPIDR_CLUSTER_MASK)) >>
328 (MPIDR_AFFINITY_BITS);
329 uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800330 int32_t ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700331
332 /* construct the current CPU # */
333 cpu |= (cluster << 2);
334
335 /* sanity check target core id */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800336 if ((core >= MCE_CORE_ID_MAX) || (cpu == (uint64_t)core)) {
Varun Wadekara0352ab2017-03-14 14:24:35 -0700337 ERROR("%s: unsupported core id (%d)\n", __func__, core);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800338 ret = EINVAL;
339 } else {
340 /*
341 * The Denver cluster has 2 CPUs only - 0, 1.
342 */
Anthony Zhou3b804502017-06-26 20:33:34 +0800343 if ((impl == DENVER_IMPL) && ((core == 2U) || (core == 3U))) {
Anthony Zhou1ab31402017-03-06 16:06:45 +0800344 ERROR("%s: unknown core id (%d)\n", __func__, core);
345 ret = EINVAL;
346 } else {
347 /* clean the previous response state */
348 ari_clobber_response(ari_base);
349 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_ONLINE_CORE, core, 0U);
350 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700351 }
352
Anthony Zhou1ab31402017-03-06 16:06:45 +0800353 return ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700354}
355
Anthony Zhou1ab31402017-03-06 16:06:45 +0800356int32_t ari_cc3_ctrl(uint32_t ari_base, uint32_t freq, uint32_t volt, uint8_t enable)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700357{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800358 uint32_t val;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700359
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700360 /* clean the previous response state */
361 ari_clobber_response(ari_base);
362
Varun Wadekara0352ab2017-03-14 14:24:35 -0700363 /*
364 * If the enable bit is cleared, Auto-CC3 will be disabled by setting
365 * the SW visible voltage/frequency request registers for all non
366 * floorswept cores valid independent of StandbyWFI and disabling
367 * the IDLE voltage/frequency request register. If set, Auto-CC3
368 * will be enabled by setting the ARM SW visible voltage/frequency
369 * request registers for all non floorswept cores to be enabled by
370 * StandbyWFI or the equivalent signal, and always keeping the IDLE
371 * voltage/frequency request register enabled.
372 */
373 val = (((freq & MCE_AUTO_CC3_FREQ_MASK) << MCE_AUTO_CC3_FREQ_SHIFT) |\
374 ((volt & MCE_AUTO_CC3_VTG_MASK) << MCE_AUTO_CC3_VTG_SHIFT) |\
Anthony Zhou1ab31402017-03-06 16:06:45 +0800375 ((enable != 0U) ? MCE_AUTO_CC3_ENABLE_BIT : 0U));
Varun Wadekara0352ab2017-03-14 14:24:35 -0700376
Anthony Zhou1ab31402017-03-06 16:06:45 +0800377 return ari_request_wait(ari_base, 0U, TEGRA_ARI_CC3_CTRL, val, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700378}
379
Anthony Zhou1ab31402017-03-06 16:06:45 +0800380int32_t ari_reset_vector_update(uint32_t ari_base)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700381{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700382 /* clean the previous response state */
383 ari_clobber_response(ari_base);
384
Varun Wadekara0352ab2017-03-14 14:24:35 -0700385 /*
386 * Need to program the CPU reset vector one time during cold boot
387 * and SC7 exit
388 */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800389 (void)ari_request_wait(ari_base, 0U, TEGRA_ARI_COPY_MISCREG_AA64_RST, 0U, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700390
391 return 0;
392}
393
Anthony Zhou1ab31402017-03-06 16:06:45 +0800394int32_t ari_roc_flush_cache_trbits(uint32_t ari_base)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700395{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700396 /* clean the previous response state */
397 ari_clobber_response(ari_base);
398
Anthony Zhou1ab31402017-03-06 16:06:45 +0800399 return ari_request_wait(ari_base, 0U, TEGRA_ARI_ROC_FLUSH_CACHE_TRBITS,
400 0U, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700401}
402
Anthony Zhou1ab31402017-03-06 16:06:45 +0800403int32_t ari_roc_flush_cache(uint32_t ari_base)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700404{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700405 /* clean the previous response state */
406 ari_clobber_response(ari_base);
407
Anthony Zhou1ab31402017-03-06 16:06:45 +0800408 return ari_request_wait(ari_base, 0U, TEGRA_ARI_ROC_FLUSH_CACHE_ONLY,
409 0U, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700410}
411
Anthony Zhou1ab31402017-03-06 16:06:45 +0800412int32_t ari_roc_clean_cache(uint32_t ari_base)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700413{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700414 /* clean the previous response state */
415 ari_clobber_response(ari_base);
416
Anthony Zhou1ab31402017-03-06 16:06:45 +0800417 return ari_request_wait(ari_base, 0U, TEGRA_ARI_ROC_CLEAN_CACHE_ONLY,
418 0U, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700419}
420
Anthony Zhou1ab31402017-03-06 16:06:45 +0800421uint64_t ari_read_write_mca(uint32_t ari_base, uint64_t cmd, uint64_t *data)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700422{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800423 uint64_t mca_arg_data, result = 0;
424 uint32_t resp_lo, resp_hi;
425 uint32_t mca_arg_err, mca_arg_finish;
426 int32_t ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700427
428 /* Set data (write) */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800429 mca_arg_data = (data != NULL) ? *data : 0ULL;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700430
431 /* Set command */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800432 ari_write_32(ari_base, (uint32_t)cmd, ARI_RESPONSE_DATA_LO);
433 ari_write_32(ari_base, (uint32_t)(cmd >> 32U), ARI_RESPONSE_DATA_HI);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700434
Anthony Zhou1ab31402017-03-06 16:06:45 +0800435 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_MCA,
436 (uint32_t)mca_arg_data,
Antonio Nino Diazf94e40d2017-09-14 15:57:44 +0100437 (uint32_t)(mca_arg_data >> 32U));
Anthony Zhou1ab31402017-03-06 16:06:45 +0800438 if (ret == 0) {
439 resp_lo = ari_get_response_low(ari_base);
440 resp_hi = ari_get_response_high(ari_base);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700441
Anthony Zhou1ab31402017-03-06 16:06:45 +0800442 mca_arg_err = resp_lo & MCA_ARG_ERROR_MASK;
443 mca_arg_finish = (resp_hi >> MCA_ARG_FINISH_SHIFT) &
444 MCA_ARG_FINISH_MASK;
445
446 if (mca_arg_finish == 0U) {
447 result = (uint64_t)mca_arg_err;
448 } else {
449 if (data != NULL) {
450 resp_lo = ari_get_request_low(ari_base);
451 resp_hi = ari_get_request_high(ari_base);
Antonio Nino Diazf94e40d2017-09-14 15:57:44 +0100452 *data = ((uint64_t)resp_hi << 32U) |
Anthony Zhou1ab31402017-03-06 16:06:45 +0800453 (uint64_t)resp_lo;
454 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700455 }
456 }
457
Anthony Zhou1ab31402017-03-06 16:06:45 +0800458 return result;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700459}
460
Anthony Zhou1ab31402017-03-06 16:06:45 +0800461int32_t ari_update_ccplex_gsc(uint32_t ari_base, uint32_t gsc_idx)
Varun Wadekara0352ab2017-03-14 14:24:35 -0700462{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800463 int32_t ret = 0;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700464 /* sanity check GSC ID */
Anthony Zhou3b804502017-06-26 20:33:34 +0800465 if (gsc_idx > TEGRA_ARI_GSC_VPR_IDX) {
Anthony Zhou1ab31402017-03-06 16:06:45 +0800466 ret = EINVAL;
467 } else {
468 /* clean the previous response state */
469 ari_clobber_response(ari_base);
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700470
Anthony Zhou1ab31402017-03-06 16:06:45 +0800471 /*
472 * The MCE code will read the GSC carveout value, corrseponding to
473 * the ID, from the MC registers and update the internal GSC registers
474 * of the CCPLEX.
475 */
476 (void)ari_request_wait(ari_base, 0U, TEGRA_ARI_UPDATE_CCPLEX_GSC, gsc_idx, 0U);
477 }
Varun Wadekara0352ab2017-03-14 14:24:35 -0700478
Anthony Zhou1ab31402017-03-06 16:06:45 +0800479 return ret;
Varun Wadekara0352ab2017-03-14 14:24:35 -0700480}
481
482void ari_enter_ccplex_state(uint32_t ari_base, uint32_t state_idx)
483{
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700484 /* clean the previous response state */
485 ari_clobber_response(ari_base);
486
Varun Wadekara0352ab2017-03-14 14:24:35 -0700487 /*
488 * The MCE will shutdown or restart the entire system
489 */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800490 (void)ari_request_wait(ari_base, 0U, TEGRA_ARI_MISC_CCPLEX, state_idx, 0U);
Varun Wadekara0352ab2017-03-14 14:24:35 -0700491}
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700492
Anthony Zhou1ab31402017-03-06 16:06:45 +0800493int32_t ari_read_write_uncore_perfmon(uint32_t ari_base, uint64_t req,
494 uint64_t *data)
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700495{
Anthony Zhou1ab31402017-03-06 16:06:45 +0800496 int32_t ret, result;
Anthony Zhou0a013212017-05-08 20:17:08 +0800497 uint32_t val, req_status;
498 uint8_t req_cmd;
Anthony Zhou1ab31402017-03-06 16:06:45 +0800499
500 req_cmd = (uint8_t)(req >> UNCORE_PERFMON_CMD_SHIFT);
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700501
Krishna Sitaraman282e5832016-07-28 13:54:29 -0700502 /* clean the previous response state */
503 ari_clobber_response(ari_base);
504
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700505 /* sanity check input parameters */
Anthony Zhou1ab31402017-03-06 16:06:45 +0800506 if ((req_cmd == UNCORE_PERFMON_CMD_READ) && (data == NULL)) {
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700507 ERROR("invalid parameters\n");
Anthony Zhou1ab31402017-03-06 16:06:45 +0800508 result = EINVAL;
509 } else {
510 /*
511 * For "write" commands get the value that has to be written
512 * to the uncore perfmon registers
513 */
514 val = (req_cmd == UNCORE_PERFMON_CMD_WRITE) ?
Antonio Nino Diazf94e40d2017-09-14 15:57:44 +0100515 (uint32_t)*data : 0U;
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700516
Anthony Zhou1ab31402017-03-06 16:06:45 +0800517 ret = ari_request_wait(ari_base, 0U, TEGRA_ARI_PERFMON, val,
518 (uint32_t)req);
519 if (ret != 0) {
520 result = ret;
521 } else {
522 /* read the command status value */
Anthony Zhou0a013212017-05-08 20:17:08 +0800523 req_status = ari_get_response_high(ari_base) &
Anthony Zhou1ab31402017-03-06 16:06:45 +0800524 UNCORE_PERFMON_RESP_STATUS_MASK;
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700525
Anthony Zhou1ab31402017-03-06 16:06:45 +0800526 /*
527 * For "read" commands get the data from the uncore
528 * perfmon registers
529 */
530 req_status >>= UNCORE_PERFMON_RESP_STATUS_SHIFT;
531 if ((req_status == 0U) && (req_cmd == UNCORE_PERFMON_CMD_READ)) {
532 *data = ari_get_response_low(ari_base);
533 }
534 result = (int32_t)req_status;
535 }
536 }
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700537
Anthony Zhou1ab31402017-03-06 16:06:45 +0800538 return result;
Varun Wadekar4ff3e8d2016-04-29 10:40:02 -0700539}
Krishna Sitaramanb429d562016-07-19 16:36:13 -0700540
541void ari_misc_ccplex(uint32_t ari_base, uint32_t index, uint32_t value)
542{
543 /*
544 * This invokes the ARI_MISC_CCPLEX commands. This can be
545 * used to enable/disable coresight clock gating.
546 */
547
Rich Wiley24e99392017-01-04 10:45:44 -0800548 if ((index > TEGRA_ARI_MISC_CCPLEX_EDBGREQ) ||
Krishna Sitaramanb429d562016-07-19 16:36:13 -0700549 ((index == TEGRA_ARI_MISC_CCPLEX_CORESIGHT_CG_CTRL) &&
Anthony Zhou1ab31402017-03-06 16:06:45 +0800550 (value > 1U))) {
Krishna Sitaramanb429d562016-07-19 16:36:13 -0700551 ERROR("%s: invalid parameters \n", __func__);
Anthony Zhou1ab31402017-03-06 16:06:45 +0800552 } else {
553 /* clean the previous response state */
554 ari_clobber_response(ari_base);
555 (void)ari_request_wait(ari_base, 0U, TEGRA_ARI_MISC_CCPLEX, index, value);
Krishna Sitaramanb429d562016-07-19 16:36:13 -0700556 }
Krishna Sitaramanb429d562016-07-19 16:36:13 -0700557}