mce.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <arch.h>
  13. #include <arch_helpers.h>
  14. #include <common/bl_common.h>
  15. #include <common/debug.h>
  16. #include <context.h>
  17. #include <denver.h>
  18. #include <lib/el3_runtime/context_mgmt.h>
  19. #include <lib/mmio.h>
  20. #include <mce.h>
  21. #include <mce_private.h>
  22. #include <t18x_ari.h>
  23. #include <tegra_def.h>
  24. #include <tegra_platform.h>
  25. /* NVG functions handlers */
  26. static arch_mce_ops_t nvg_mce_ops = {
  27. .enter_cstate = nvg_enter_cstate,
  28. .update_cstate_info = nvg_update_cstate_info,
  29. .update_crossover_time = nvg_update_crossover_time,
  30. .read_cstate_stats = nvg_read_cstate_stats,
  31. .write_cstate_stats = nvg_write_cstate_stats,
  32. .call_enum_misc = ari_enumeration_misc,
  33. .is_ccx_allowed = nvg_is_ccx_allowed,
  34. .is_sc7_allowed = nvg_is_sc7_allowed,
  35. .online_core = nvg_online_core,
  36. .cc3_ctrl = nvg_cc3_ctrl,
  37. .update_reset_vector = ari_reset_vector_update,
  38. .roc_flush_cache = ari_roc_flush_cache,
  39. .roc_flush_cache_trbits = ari_roc_flush_cache_trbits,
  40. .roc_clean_cache = ari_roc_clean_cache,
  41. .read_write_mca = ari_read_write_mca,
  42. .update_ccplex_gsc = ari_update_ccplex_gsc,
  43. .enter_ccplex_state = ari_enter_ccplex_state,
  44. .read_write_uncore_perfmon = ari_read_write_uncore_perfmon,
  45. .misc_ccplex = ari_misc_ccplex
  46. };
  47. /* ARI functions handlers */
  48. static arch_mce_ops_t ari_mce_ops = {
  49. .enter_cstate = ari_enter_cstate,
  50. .update_cstate_info = ari_update_cstate_info,
  51. .update_crossover_time = ari_update_crossover_time,
  52. .read_cstate_stats = ari_read_cstate_stats,
  53. .write_cstate_stats = ari_write_cstate_stats,
  54. .call_enum_misc = ari_enumeration_misc,
  55. .is_ccx_allowed = ari_is_ccx_allowed,
  56. .is_sc7_allowed = ari_is_sc7_allowed,
  57. .online_core = ari_online_core,
  58. .cc3_ctrl = ari_cc3_ctrl,
  59. .update_reset_vector = ari_reset_vector_update,
  60. .roc_flush_cache = ari_roc_flush_cache,
  61. .roc_flush_cache_trbits = ari_roc_flush_cache_trbits,
  62. .roc_clean_cache = ari_roc_clean_cache,
  63. .read_write_mca = ari_read_write_mca,
  64. .update_ccplex_gsc = ari_update_ccplex_gsc,
  65. .enter_ccplex_state = ari_enter_ccplex_state,
  66. .read_write_uncore_perfmon = ari_read_write_uncore_perfmon,
  67. .misc_ccplex = ari_misc_ccplex
  68. };
  69. typedef struct {
  70. uint32_t ari_base;
  71. arch_mce_ops_t *ops;
  72. } mce_config_t;
  73. /* Table to hold the per-CPU ARI base address and function handlers */
  74. static mce_config_t mce_cfg_table[MCE_ARI_APERTURES_MAX] = {
  75. {
  76. /* A57 Core 0 */
  77. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_0_OFFSET,
  78. .ops = &ari_mce_ops,
  79. },
  80. {
  81. /* A57 Core 1 */
  82. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_1_OFFSET,
  83. .ops = &ari_mce_ops,
  84. },
  85. {
  86. /* A57 Core 2 */
  87. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_2_OFFSET,
  88. .ops = &ari_mce_ops,
  89. },
  90. {
  91. /* A57 Core 3 */
  92. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_3_OFFSET,
  93. .ops = &ari_mce_ops,
  94. },
  95. {
  96. /* D15 Core 0 */
  97. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_4_OFFSET,
  98. .ops = &nvg_mce_ops,
  99. },
  100. {
  101. /* D15 Core 1 */
  102. .ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_5_OFFSET,
  103. .ops = &nvg_mce_ops,
  104. }
  105. };
  106. static uint32_t mce_get_curr_cpu_ari_base(void)
  107. {
  108. uint64_t mpidr = read_mpidr();
  109. uint64_t cpuid = mpidr & MPIDR_CPU_MASK;
  110. uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK;
  111. /*
  112. * T186 has 2 CPU clusters, one with Denver CPUs and the other with
  113. * ARM CortexA-57 CPUs. Each cluster consists of 4 CPUs and the CPU
  114. * numbers start from 0. In order to get the proper arch_mce_ops_t
  115. * struct, we have to convert the Denver CPU ids to the corresponding
  116. * indices in the mce_ops_table array.
  117. */
  118. if (impl == DENVER_IMPL) {
  119. cpuid |= 0x4U;
  120. }
  121. return mce_cfg_table[cpuid].ari_base;
  122. }
  123. static arch_mce_ops_t *mce_get_curr_cpu_ops(void)
  124. {
  125. uint64_t mpidr = read_mpidr();
  126. uint64_t cpuid = mpidr & MPIDR_CPU_MASK;
  127. uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) &
  128. MIDR_IMPL_MASK;
  129. /*
  130. * T186 has 2 CPU clusters, one with Denver CPUs and the other with
  131. * ARM CortexA-57 CPUs. Each cluster consists of 4 CPUs and the CPU
  132. * numbers start from 0. In order to get the proper arch_mce_ops_t
  133. * struct, we have to convert the Denver CPU ids to the corresponding
  134. * indices in the mce_ops_table array.
  135. */
  136. if (impl == DENVER_IMPL) {
  137. cpuid |= 0x4U;
  138. }
  139. return mce_cfg_table[cpuid].ops;
  140. }
  141. /*******************************************************************************
  142. * Common handler for all MCE commands
  143. ******************************************************************************/
  144. int32_t mce_command_handler(uint64_t cmd, uint64_t arg0, uint64_t arg1,
  145. uint64_t arg2)
  146. {
  147. const arch_mce_ops_t *ops;
  148. gp_regs_t *gp_regs = get_gpregs_ctx(cm_get_context(NON_SECURE));
  149. uint32_t cpu_ari_base;
  150. uint64_t ret64 = 0, arg3, arg4, arg5;
  151. int32_t ret = 0;
  152. assert(gp_regs != NULL);
  153. /* get a pointer to the CPU's arch_mce_ops_t struct */
  154. ops = mce_get_curr_cpu_ops();
  155. /* get the CPU's ARI base address */
  156. cpu_ari_base = mce_get_curr_cpu_ari_base();
  157. switch (cmd) {
  158. case (uint64_t)MCE_CMD_ENTER_CSTATE:
  159. ret = ops->enter_cstate(cpu_ari_base, arg0, arg1);
  160. break;
  161. case (uint64_t)MCE_CMD_UPDATE_CSTATE_INFO:
  162. /*
  163. * get the parameters required for the update cstate info
  164. * command
  165. */
  166. arg3 = read_ctx_reg(gp_regs, CTX_GPREG_X4);
  167. arg4 = read_ctx_reg(gp_regs, CTX_GPREG_X5);
  168. arg5 = read_ctx_reg(gp_regs, CTX_GPREG_X6);
  169. ret = ops->update_cstate_info(cpu_ari_base, (uint32_t)arg0,
  170. (uint32_t)arg1, (uint32_t)arg2, (uint8_t)arg3,
  171. (uint32_t)arg4, (uint8_t)arg5);
  172. write_ctx_reg(gp_regs, CTX_GPREG_X4, (0ULL));
  173. write_ctx_reg(gp_regs, CTX_GPREG_X5, (0ULL));
  174. write_ctx_reg(gp_regs, CTX_GPREG_X6, (0ULL));
  175. break;
  176. case (uint64_t)MCE_CMD_UPDATE_CROSSOVER_TIME:
  177. ret = ops->update_crossover_time(cpu_ari_base, arg0, arg1);
  178. break;
  179. case (uint64_t)MCE_CMD_READ_CSTATE_STATS:
  180. ret64 = ops->read_cstate_stats(cpu_ari_base, arg0);
  181. /* update context to return cstate stats value */
  182. write_ctx_reg(gp_regs, CTX_GPREG_X1, (ret64));
  183. write_ctx_reg(gp_regs, CTX_GPREG_X2, (ret64));
  184. break;
  185. case (uint64_t)MCE_CMD_WRITE_CSTATE_STATS:
  186. ret = ops->write_cstate_stats(cpu_ari_base, arg0, arg1);
  187. break;
  188. case (uint64_t)MCE_CMD_IS_CCX_ALLOWED:
  189. ret = ops->is_ccx_allowed(cpu_ari_base, arg0, arg1);
  190. /* update context to return CCx status value */
  191. write_ctx_reg(gp_regs, CTX_GPREG_X1, (uint64_t)(ret));
  192. break;
  193. case (uint64_t)MCE_CMD_IS_SC7_ALLOWED:
  194. ret = ops->is_sc7_allowed(cpu_ari_base, arg0, arg1);
  195. /* update context to return SC7 status value */
  196. write_ctx_reg(gp_regs, CTX_GPREG_X1, (uint64_t)(ret));
  197. write_ctx_reg(gp_regs, CTX_GPREG_X3, (uint64_t)(ret));
  198. break;
  199. case (uint64_t)MCE_CMD_ONLINE_CORE:
  200. ret = ops->online_core(cpu_ari_base, arg0);
  201. break;
  202. case (uint64_t)MCE_CMD_CC3_CTRL:
  203. ret = ops->cc3_ctrl(cpu_ari_base, arg0, arg1, arg2);
  204. break;
  205. case (uint64_t)MCE_CMD_ECHO_DATA:
  206. ret64 = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_ECHO,
  207. arg0);
  208. /* update context to return if echo'd data matched source */
  209. write_ctx_reg(gp_regs, CTX_GPREG_X1, ((ret64 == arg0) ?
  210. 1ULL : 0ULL));
  211. write_ctx_reg(gp_regs, CTX_GPREG_X2, ((ret64 == arg0) ?
  212. 1ULL : 0ULL));
  213. break;
  214. case (uint64_t)MCE_CMD_READ_VERSIONS:
  215. ret64 = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_VERSION,
  216. arg0);
  217. /*
  218. * version = minor(63:32) | major(31:0). Update context
  219. * to return major and minor version number.
  220. */
  221. write_ctx_reg(gp_regs, CTX_GPREG_X1, (ret64));
  222. write_ctx_reg(gp_regs, CTX_GPREG_X2, (ret64 >> 32ULL));
  223. break;
  224. case (uint64_t)MCE_CMD_ENUM_FEATURES:
  225. ret64 = ops->call_enum_misc(cpu_ari_base,
  226. TEGRA_ARI_MISC_FEATURE_LEAF_0, arg0);
  227. /* update context to return features value */
  228. write_ctx_reg(gp_regs, CTX_GPREG_X1, (ret64));
  229. break;
  230. case (uint64_t)MCE_CMD_ROC_FLUSH_CACHE_TRBITS:
  231. ret = ops->roc_flush_cache_trbits(cpu_ari_base);
  232. break;
  233. case (uint64_t)MCE_CMD_ROC_FLUSH_CACHE:
  234. ret = ops->roc_flush_cache(cpu_ari_base);
  235. break;
  236. case (uint64_t)MCE_CMD_ROC_CLEAN_CACHE:
  237. ret = ops->roc_clean_cache(cpu_ari_base);
  238. break;
  239. case (uint64_t)MCE_CMD_ENUM_READ_MCA:
  240. ret64 = ops->read_write_mca(cpu_ari_base, arg0, &arg1);
  241. /* update context to return MCA data/error */
  242. write_ctx_reg(gp_regs, CTX_GPREG_X1, (ret64));
  243. write_ctx_reg(gp_regs, CTX_GPREG_X2, (arg1));
  244. write_ctx_reg(gp_regs, CTX_GPREG_X3, (ret64));
  245. break;
  246. case (uint64_t)MCE_CMD_ENUM_WRITE_MCA:
  247. ret64 = ops->read_write_mca(cpu_ari_base, arg0, &arg1);
  248. /* update context to return MCA error */
  249. write_ctx_reg(gp_regs, CTX_GPREG_X1, (ret64));
  250. write_ctx_reg(gp_regs, CTX_GPREG_X3, (ret64));
  251. break;
  252. #if ENABLE_CHIP_VERIFICATION_HARNESS
  253. case (uint64_t)MCE_CMD_ENABLE_LATIC:
  254. /*
  255. * This call is not for production use. The constant value,
  256. * 0xFFFF0000, is specific to allowing for enabling LATIC on
  257. * pre-production parts for the chip verification harness.
  258. *
  259. * Enabling LATIC allows S/W to read the MINI ISPs in the
  260. * CCPLEX. The ISMs are used for various measurements relevant
  261. * to particular locations in the Silicon. They are small
  262. * counters which can be polled to determine how fast a
  263. * particular location in the Silicon is.
  264. */
  265. ops->enter_ccplex_state(mce_get_curr_cpu_ari_base(),
  266. 0xFFFF0000);
  267. break;
  268. #endif
  269. case (uint64_t)MCE_CMD_UNCORE_PERFMON_REQ:
  270. ret = ops->read_write_uncore_perfmon(cpu_ari_base, arg0, &arg1);
  271. /* update context to return data */
  272. write_ctx_reg(gp_regs, CTX_GPREG_X1, (arg1));
  273. break;
  274. case (uint64_t)MCE_CMD_MISC_CCPLEX:
  275. ops->misc_ccplex(cpu_ari_base, arg0, arg1);
  276. break;
  277. default:
  278. ERROR("unknown MCE command (%" PRIu64 ")\n", cmd);
  279. ret = EINVAL;
  280. break;
  281. }
  282. return ret;
  283. }
  284. /*******************************************************************************
  285. * Handler to update the reset vector for CPUs
  286. ******************************************************************************/
  287. int32_t mce_update_reset_vector(void)
  288. {
  289. const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
  290. ops->update_reset_vector(mce_get_curr_cpu_ari_base());
  291. return 0;
  292. }
  293. static int32_t mce_update_ccplex_gsc(tegra_ari_gsc_index_t gsc_idx)
  294. {
  295. const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
  296. ops->update_ccplex_gsc(mce_get_curr_cpu_ari_base(), gsc_idx);
  297. return 0;
  298. }
  299. /*******************************************************************************
  300. * Handler to update carveout values for Video Memory Carveout region
  301. ******************************************************************************/
  302. int32_t mce_update_gsc_videomem(void)
  303. {
  304. return mce_update_ccplex_gsc(TEGRA_ARI_GSC_VPR_IDX);
  305. }
  306. /*******************************************************************************
  307. * Handler to update carveout values for TZDRAM aperture
  308. ******************************************************************************/
  309. int32_t mce_update_gsc_tzdram(void)
  310. {
  311. return mce_update_ccplex_gsc(TEGRA_ARI_GSC_TZ_DRAM_IDX);
  312. }
  313. /*******************************************************************************
  314. * Handler to shutdown/reset the entire system
  315. ******************************************************************************/
  316. __dead2 void mce_enter_ccplex_state(uint32_t state_idx)
  317. {
  318. const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
  319. /* sanity check state value */
  320. if ((state_idx != TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF) &&
  321. (state_idx != TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT)) {
  322. panic();
  323. }
  324. ops->enter_ccplex_state(mce_get_curr_cpu_ari_base(), state_idx);
  325. /* wait till the CCPLEX powers down */
  326. for (;;) {
  327. ;
  328. }
  329. }
  330. /*******************************************************************************
  331. * Handler to issue the UPDATE_CSTATE_INFO request
  332. ******************************************************************************/
  333. void mce_update_cstate_info(const mce_cstate_info_t *cstate)
  334. {
  335. const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
  336. /* issue the UPDATE_CSTATE_INFO request */
  337. ops->update_cstate_info(mce_get_curr_cpu_ari_base(), cstate->cluster,
  338. cstate->ccplex, cstate->system, cstate->system_state_force,
  339. cstate->wake_mask, cstate->update_wake_mask);
  340. }
  341. /*******************************************************************************
  342. * Handler to read the MCE firmware version and check if it is compatible
  343. * with interface header the BL3-1 was compiled against
  344. ******************************************************************************/
  345. void mce_verify_firmware_version(void)
  346. {
  347. const arch_mce_ops_t *ops;
  348. uint32_t cpu_ari_base;
  349. uint64_t version;
  350. uint32_t major, minor;
  351. /*
  352. * MCE firmware is not supported on simulation platforms.
  353. */
  354. if (tegra_platform_is_emulation()) {
  355. INFO("MCE firmware is not supported\n");
  356. } else {
  357. /* get a pointer to the CPU's arch_mce_ops_t struct */
  358. ops = mce_get_curr_cpu_ops();
  359. /* get the CPU's ARI base address */
  360. cpu_ari_base = mce_get_curr_cpu_ari_base();
  361. /*
  362. * Read the MCE firmware version and extract the major and minor
  363. * version fields
  364. */
  365. version = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_VERSION, 0);
  366. major = (uint32_t)version;
  367. minor = (uint32_t)(version >> 32);
  368. INFO("MCE Version - HW=%d:%d, SW=%d:%d\n", major, minor,
  369. TEGRA_ARI_VERSION_MAJOR, TEGRA_ARI_VERSION_MINOR);
  370. /*
  371. * Verify that the MCE firmware version and the interface header
  372. * match
  373. */
  374. if (major != TEGRA_ARI_VERSION_MAJOR) {
  375. ERROR("ARI major version mismatch\n");
  376. panic();
  377. }
  378. if (minor < TEGRA_ARI_VERSION_MINOR) {
  379. ERROR("ARI minor version mismatch\n");
  380. panic();
  381. }
  382. }
  383. }