plat_setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2015-2019, 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 <arch_helpers.h>
  9. #include <bl31/bl31.h>
  10. #include <bl31/interrupt_mgmt.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <common/ep_info.h>
  14. #include <common/interrupt_props.h>
  15. #include <context.h>
  16. #include <cortex_a57.h>
  17. #include <denver.h>
  18. #include <drivers/arm/gic_common.h>
  19. #include <drivers/arm/gicv2.h>
  20. #include <drivers/console.h>
  21. #include <lib/el3_runtime/context_mgmt.h>
  22. #include <lib/utils.h>
  23. #include <lib/xlat_tables/xlat_tables_v2.h>
  24. #include <plat/common/platform.h>
  25. #include <mce.h>
  26. #include <memctrl.h>
  27. #include <smmu.h>
  28. #include <tegra_def.h>
  29. #include <tegra_platform.h>
  30. #include <tegra_private.h>
  31. extern void memcpy16(void *dest, const void *src, unsigned int length);
  32. /*******************************************************************************
  33. * Tegra186 CPU numbers in cluster #0
  34. *******************************************************************************
  35. */
  36. #define TEGRA186_CLUSTER0_CORE2 2U
  37. #define TEGRA186_CLUSTER0_CORE3 3U
  38. /*******************************************************************************
  39. * The Tegra power domain tree has a single system level power domain i.e. a
  40. * single root node. The first entry in the power domain descriptor specifies
  41. * the number of power domains at the highest power level.
  42. *******************************************************************************
  43. */
  44. static const uint8_t tegra_power_domain_tree_desc[] = {
  45. /* No of root nodes */
  46. 1,
  47. /* No of clusters */
  48. PLATFORM_CLUSTER_COUNT,
  49. /* No of CPU cores - cluster0 */
  50. PLATFORM_MAX_CPUS_PER_CLUSTER,
  51. /* No of CPU cores - cluster1 */
  52. PLATFORM_MAX_CPUS_PER_CLUSTER
  53. };
  54. /*******************************************************************************
  55. * This function returns the Tegra default topology tree information.
  56. ******************************************************************************/
  57. const uint8_t *plat_get_power_domain_tree_desc(void)
  58. {
  59. return tegra_power_domain_tree_desc;
  60. }
  61. /*
  62. * Table of regions to map using the MMU.
  63. */
  64. static const mmap_region_t tegra_mmap[] = {
  65. MAP_REGION_FLAT(TEGRA_MISC_BASE, 0x10000U, /* 64KB */
  66. MT_DEVICE | MT_RW | MT_SECURE),
  67. MAP_REGION_FLAT(TEGRA_TSA_BASE, 0x20000U, /* 128KB */
  68. MT_DEVICE | MT_RW | MT_SECURE),
  69. MAP_REGION_FLAT(TEGRA_MC_STREAMID_BASE, 0x10000U, /* 64KB */
  70. MT_DEVICE | MT_RW | MT_SECURE),
  71. MAP_REGION_FLAT(TEGRA_MC_BASE, 0x10000U, /* 64KB */
  72. MT_DEVICE | MT_RW | MT_SECURE),
  73. MAP_REGION_FLAT(TEGRA_UARTA_BASE, 0x20000U, /* 128KB - UART A, B*/
  74. MT_DEVICE | MT_RW | MT_SECURE),
  75. MAP_REGION_FLAT(TEGRA_UARTC_BASE, 0x20000U, /* 128KB - UART C, G */
  76. MT_DEVICE | MT_RW | MT_SECURE),
  77. MAP_REGION_FLAT(TEGRA_UARTD_BASE, 0x30000U, /* 192KB - UART D, E, F */
  78. MT_DEVICE | MT_RW | MT_SECURE),
  79. MAP_REGION_FLAT(TEGRA_FUSE_BASE, 0x10000U, /* 64KB */
  80. MT_DEVICE | MT_RW | MT_SECURE),
  81. MAP_REGION_FLAT(TEGRA_GICD_BASE, 0x20000U, /* 128KB */
  82. MT_DEVICE | MT_RW | MT_SECURE),
  83. MAP_REGION_FLAT(TEGRA_SE0_BASE, 0x10000U, /* 64KB */
  84. MT_DEVICE | MT_RW | MT_SECURE),
  85. MAP_REGION_FLAT(TEGRA_PKA1_BASE, 0x10000U, /* 64KB */
  86. MT_DEVICE | MT_RW | MT_SECURE),
  87. MAP_REGION_FLAT(TEGRA_RNG1_BASE, 0x10000U, /* 64KB */
  88. MT_DEVICE | MT_RW | MT_SECURE),
  89. MAP_REGION_FLAT(TEGRA_CAR_RESET_BASE, 0x10000U, /* 64KB */
  90. MT_DEVICE | MT_RW | MT_SECURE),
  91. MAP_REGION_FLAT(TEGRA_PMC_BASE, 0x40000U, /* 256KB */
  92. MT_DEVICE | MT_RW | MT_SECURE),
  93. MAP_REGION_FLAT(TEGRA_TMRUS_BASE, 0x1000U, /* 4KB */
  94. MT_DEVICE | MT_RO | MT_SECURE),
  95. MAP_REGION_FLAT(TEGRA_SCRATCH_BASE, 0x10000U, /* 64KB */
  96. MT_DEVICE | MT_RW | MT_SECURE),
  97. MAP_REGION_FLAT(TEGRA_MMCRAB_BASE, 0x60000U, /* 384KB */
  98. MT_DEVICE | MT_RW | MT_SECURE),
  99. MAP_REGION_FLAT(TEGRA_ARM_ACTMON_CTR_BASE, 0x20000U, /* 128KB - ARM/Denver */
  100. MT_DEVICE | MT_RW | MT_SECURE),
  101. MAP_REGION_FLAT(TEGRA_SMMU0_BASE, 0x1000000U, /* 64KB */
  102. MT_DEVICE | MT_RW | MT_SECURE),
  103. MAP_REGION_FLAT(TEGRA_HSP_DBELL_BASE, 0x10000U, /* 64KB */
  104. MT_DEVICE | MT_RW | MT_SECURE),
  105. MAP_REGION_FLAT(TEGRA_BPMP_IPC_TX_PHYS_BASE, TEGRA_BPMP_IPC_CH_MAP_SIZE, /* 4KB */
  106. MT_DEVICE | MT_RW | MT_SECURE),
  107. MAP_REGION_FLAT(TEGRA_BPMP_IPC_RX_PHYS_BASE, TEGRA_BPMP_IPC_CH_MAP_SIZE, /* 4KB */
  108. MT_DEVICE | MT_RW | MT_SECURE),
  109. {0}
  110. };
  111. /*******************************************************************************
  112. * Set up the pagetables as per the platform memory map & initialize the MMU
  113. ******************************************************************************/
  114. const mmap_region_t *plat_get_mmio_map(void)
  115. {
  116. /* MMIO space */
  117. return tegra_mmap;
  118. }
  119. /*******************************************************************************
  120. * Handler to get the System Counter Frequency
  121. ******************************************************************************/
  122. uint32_t plat_get_syscnt_freq2(void)
  123. {
  124. return 31250000;
  125. }
  126. /*******************************************************************************
  127. * Maximum supported UART controllers
  128. ******************************************************************************/
  129. #define TEGRA186_MAX_UART_PORTS 7
  130. /*******************************************************************************
  131. * This variable holds the UART port base addresses
  132. ******************************************************************************/
  133. static uint32_t tegra186_uart_addresses[TEGRA186_MAX_UART_PORTS + 1] = {
  134. 0, /* undefined - treated as an error case */
  135. TEGRA_UARTA_BASE,
  136. TEGRA_UARTB_BASE,
  137. TEGRA_UARTC_BASE,
  138. TEGRA_UARTD_BASE,
  139. TEGRA_UARTE_BASE,
  140. TEGRA_UARTF_BASE,
  141. TEGRA_UARTG_BASE,
  142. };
  143. /*******************************************************************************
  144. * Enable console corresponding to the console ID
  145. ******************************************************************************/
  146. void plat_enable_console(int32_t id)
  147. {
  148. static console_t uart_console;
  149. uint32_t console_clock;
  150. if ((id > 0) && (id < TEGRA186_MAX_UART_PORTS)) {
  151. /*
  152. * Reference clock used by the FPGAs is a lot slower.
  153. */
  154. if (tegra_platform_is_fpga()) {
  155. console_clock = TEGRA_BOOT_UART_CLK_13_MHZ;
  156. } else {
  157. console_clock = TEGRA_BOOT_UART_CLK_408_MHZ;
  158. }
  159. (void)console_16550_register(tegra186_uart_addresses[id],
  160. console_clock,
  161. TEGRA_CONSOLE_BAUDRATE,
  162. &uart_console);
  163. console_set_scope(&uart_console, CONSOLE_FLAG_BOOT |
  164. CONSOLE_FLAG_RUNTIME | CONSOLE_FLAG_CRASH);
  165. }
  166. }
  167. /*******************************************************************************
  168. * Handler for early platform setup
  169. ******************************************************************************/
  170. void plat_early_platform_setup(void)
  171. {
  172. uint64_t impl, val;
  173. const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
  174. const struct tegra_bl31_params *arg_from_bl2 = plat_get_bl31_params();
  175. /* Verify chip id is t186 */
  176. assert(tegra_chipid_is_t186());
  177. /* sanity check MCE firmware compatibility */
  178. mce_verify_firmware_version();
  179. /*
  180. * Do initial security configuration to allow DRAM/device access.
  181. */
  182. tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
  183. (uint32_t)plat_params->tzdram_size);
  184. impl = (read_midr() >> MIDR_IMPL_SHIFT) & (uint64_t)MIDR_IMPL_MASK;
  185. /*
  186. * Enable ECC and Parity Protection for Cortex-A57 CPUs (Tegra186
  187. * A02p and beyond).
  188. */
  189. if ((plat_params->l2_ecc_parity_prot_dis != 1) &&
  190. (impl != (uint64_t)DENVER_IMPL)) {
  191. val = read_l2ctlr_el1();
  192. val |= CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT;
  193. write_l2ctlr_el1(val);
  194. }
  195. /*
  196. * The previous bootloader might not have placed the BL32 image
  197. * inside the TZDRAM. Platform handler to allow relocation of BL32
  198. * image to TZDRAM memory. This behavior might change per platform.
  199. */
  200. plat_relocate_bl32_image(arg_from_bl2->bl32_image_info);
  201. }
  202. /*******************************************************************************
  203. * Handler for late platform setup
  204. ******************************************************************************/
  205. void plat_late_platform_setup(void)
  206. {
  207. ; /* do nothing */
  208. }
  209. /* Secure IRQs for Tegra186 */
  210. static const interrupt_prop_t tegra186_interrupt_props[] = {
  211. INTR_PROP_DESC(TEGRA_SDEI_SGI_PRIVATE, PLAT_SDEI_CRITICAL_PRI,
  212. GICV2_INTR_GROUP0, GIC_INTR_CFG_EDGE),
  213. INTR_PROP_DESC(TEGRA186_TOP_WDT_IRQ, PLAT_TEGRA_WDT_PRIO,
  214. GICV2_INTR_GROUP0, GIC_INTR_CFG_EDGE),
  215. INTR_PROP_DESC(TEGRA186_AON_WDT_IRQ, PLAT_TEGRA_WDT_PRIO,
  216. GICV2_INTR_GROUP0, GIC_INTR_CFG_EDGE)
  217. };
  218. /*******************************************************************************
  219. * Initialize the GIC and SGIs
  220. ******************************************************************************/
  221. void plat_gic_setup(void)
  222. {
  223. tegra_gic_setup(tegra186_interrupt_props, ARRAY_SIZE(tegra186_interrupt_props));
  224. tegra_gic_init();
  225. /*
  226. * Initialize the FIQ handler only if the platform supports any
  227. * FIQ interrupt sources.
  228. */
  229. tegra_fiq_handler_setup();
  230. }
  231. /*******************************************************************************
  232. * Return pointer to the BL31 params from previous bootloader
  233. ******************************************************************************/
  234. struct tegra_bl31_params *plat_get_bl31_params(void)
  235. {
  236. uint32_t val;
  237. val = mmio_read_32(TEGRA_SCRATCH_BASE + SCRATCH_BL31_PARAMS_ADDR);
  238. return (struct tegra_bl31_params *)(uintptr_t)val;
  239. }
  240. /*******************************************************************************
  241. * Return pointer to the BL31 platform params from previous bootloader
  242. ******************************************************************************/
  243. plat_params_from_bl2_t *plat_get_bl31_plat_params(void)
  244. {
  245. uint32_t val;
  246. val = mmio_read_32(TEGRA_SCRATCH_BASE + SCRATCH_BL31_PLAT_PARAMS_ADDR);
  247. return (plat_params_from_bl2_t *)(uintptr_t)val;
  248. }
  249. /*******************************************************************************
  250. * This function implements a part of the critical interface between the psci
  251. * generic layer and the platform that allows the former to query the platform
  252. * to convert an MPIDR to a unique linear index. An error code (-1) is returned
  253. * in case the MPIDR is invalid.
  254. ******************************************************************************/
  255. int32_t plat_core_pos_by_mpidr(u_register_t mpidr)
  256. {
  257. u_register_t cluster_id, cpu_id, pos;
  258. int32_t ret;
  259. cluster_id = (mpidr >> (u_register_t)MPIDR_AFF1_SHIFT) & (u_register_t)MPIDR_AFFLVL_MASK;
  260. cpu_id = (mpidr >> (u_register_t)MPIDR_AFF0_SHIFT) & (u_register_t)MPIDR_AFFLVL_MASK;
  261. /*
  262. * Validate cluster_id by checking whether it represents
  263. * one of the two clusters present on the platform.
  264. * Validate cpu_id by checking whether it represents a CPU in
  265. * one of the two clusters present on the platform.
  266. */
  267. if ((cluster_id >= (u_register_t)PLATFORM_CLUSTER_COUNT) ||
  268. (cpu_id >= (u_register_t)PLATFORM_MAX_CPUS_PER_CLUSTER)) {
  269. ret = -1;
  270. } else {
  271. /* calculate the core position */
  272. pos = cpu_id + (cluster_id << 2U);
  273. /* check for non-existent CPUs */
  274. if ((pos == TEGRA186_CLUSTER0_CORE2) || (pos == TEGRA186_CLUSTER0_CORE3)) {
  275. ret = -1;
  276. } else {
  277. ret = (int32_t)pos;
  278. }
  279. }
  280. return ret;
  281. }
  282. /*******************************************************************************
  283. * Handler to relocate BL32 image to TZDRAM
  284. ******************************************************************************/
  285. void plat_relocate_bl32_image(const image_info_t *bl32_img_info)
  286. {
  287. const plat_params_from_bl2_t *plat_bl31_params = plat_get_bl31_plat_params();
  288. const entry_point_info_t *bl32_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
  289. uint64_t tzdram_start, tzdram_end, bl32_start, bl32_end;
  290. if ((bl32_img_info != NULL) && (bl32_ep_info != NULL)) {
  291. /* Relocate BL32 if it resides outside of the TZDRAM */
  292. tzdram_start = plat_bl31_params->tzdram_base;
  293. tzdram_end = plat_bl31_params->tzdram_base +
  294. plat_bl31_params->tzdram_size;
  295. bl32_start = bl32_img_info->image_base;
  296. bl32_end = bl32_img_info->image_base + bl32_img_info->image_size;
  297. assert(tzdram_end > tzdram_start);
  298. assert(bl32_end > bl32_start);
  299. assert(bl32_ep_info->pc > tzdram_start);
  300. assert(bl32_ep_info->pc < tzdram_end);
  301. /* relocate BL32 */
  302. if ((bl32_start >= tzdram_end) || (bl32_end <= tzdram_start)) {
  303. INFO("Relocate BL32 to TZDRAM\n");
  304. (void)memcpy16((void *)(uintptr_t)bl32_ep_info->pc,
  305. (void *)(uintptr_t)bl32_start,
  306. bl32_img_info->image_size);
  307. /* clean up non-secure intermediate buffer */
  308. zeromem((void *)(uintptr_t)bl32_start,
  309. bl32_img_info->image_size);
  310. }
  311. }
  312. }
  313. /*******************************************************************************
  314. * Handler to indicate support for System Suspend
  315. ******************************************************************************/
  316. bool plat_supports_system_suspend(void)
  317. {
  318. return true;
  319. }
  320. /*******************************************************************************
  321. * Platform specific runtime setup.
  322. ******************************************************************************/
  323. void plat_runtime_setup(void)
  324. {
  325. /*
  326. * During cold boot, it is observed that the arbitration
  327. * bit is set in the Memory controller leading to false
  328. * error interrupts in the non-secure world. To avoid
  329. * this, clean the interrupt status register before
  330. * booting into the non-secure world
  331. */
  332. tegra_memctrl_clear_pending_interrupts();
  333. /*
  334. * During boot, USB3 and flash media (SDMMC/SATA) devices need
  335. * access to IRAM. Because these clients connect to the MC and
  336. * do not have a direct path to the IRAM, the MC implements AHB
  337. * redirection during boot to allow path to IRAM. In this mode
  338. * accesses to a programmed memory address aperture are directed
  339. * to the AHB bus, allowing access to the IRAM. This mode must be
  340. * disabled before we jump to the non-secure world.
  341. */
  342. tegra_memctrl_disable_ahb_redirection();
  343. /*
  344. * Verify the integrity of the previously configured SMMU(s)
  345. * settings
  346. */
  347. tegra_smmu_verify();
  348. }