fpga_bl31_setup.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <arch_features.h>
  9. #include <common/fdt_fixup.h>
  10. #include <common/fdt_wrappers.h>
  11. #include <drivers/arm/gicv3.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/generic_delay_timer.h>
  14. #include <lib/extensions/spe.h>
  15. #include <lib/mmio.h>
  16. #include <libfdt.h>
  17. #include "fpga_private.h"
  18. #include <plat/common/platform.h>
  19. #include <platform_def.h>
  20. static entry_point_info_t bl33_image_ep_info;
  21. static unsigned int system_freq;
  22. volatile uint32_t secondary_core_spinlock;
  23. uintptr_t plat_get_ns_image_entrypoint(void)
  24. {
  25. #ifdef PRELOADED_BL33_BASE
  26. return PRELOADED_BL33_BASE;
  27. #else
  28. return 0ULL;
  29. #endif
  30. }
  31. uint32_t fpga_get_spsr_for_bl33_entry(void)
  32. {
  33. return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  34. }
  35. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  36. u_register_t arg2, u_register_t arg3)
  37. {
  38. /* Add this core to the VALID mpids list */
  39. fpga_valid_mpids[plat_my_core_pos()] = VALID_MPID;
  40. /*
  41. * Notify the secondary CPUs that the C runtime is ready
  42. * so they can announce themselves.
  43. */
  44. secondary_core_spinlock = C_RUNTIME_READY_KEY;
  45. dsbish();
  46. sev();
  47. fpga_console_init();
  48. bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
  49. bl33_image_ep_info.spsr = fpga_get_spsr_for_bl33_entry();
  50. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  51. /* Set x0-x3 for the primary CPU as expected by the kernel */
  52. bl33_image_ep_info.args.arg0 = (u_register_t)FPGA_PRELOADED_DTB_BASE;
  53. bl33_image_ep_info.args.arg1 = 0U;
  54. bl33_image_ep_info.args.arg2 = 0U;
  55. bl33_image_ep_info.args.arg3 = 0U;
  56. }
  57. void bl31_plat_arch_setup(void)
  58. {
  59. }
  60. void bl31_platform_setup(void)
  61. {
  62. /* Write frequency to CNTCRL and initialize timer */
  63. generic_delay_timer_init();
  64. /*
  65. * Before doing anything else, wait for some time to ensure that
  66. * the secondary CPUs have populated the fpga_valid_mpids array.
  67. * As the number of secondary cores is unknown and can even be 0,
  68. * it is not possible to rely on any signal from them, so use a
  69. * delay instead.
  70. */
  71. mdelay(5);
  72. /*
  73. * On the event of a cold reset issued by, for instance, a reset pin
  74. * assertion, we cannot guarantee memory to be initialized to zero.
  75. * In such scenario, if the secondary cores reached
  76. * plat_secondary_cold_boot_setup before the primary one initialized
  77. * .BSS, we could end up having a race condition if the spinlock
  78. * was not cleared before.
  79. *
  80. * Similarly, if there were a reset before the spinlock had been
  81. * cleared, the secondary cores would find the lock opened before
  82. * .BSS is cleared, causing another race condition.
  83. *
  84. * So clean the spinlock as soon as we think it is safe to reduce the
  85. * chances of any race condition on a reset.
  86. */
  87. secondary_core_spinlock = 0UL;
  88. /* Initialize the GIC driver, cpu and distributor interfaces */
  89. plat_fpga_gic_init();
  90. }
  91. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  92. {
  93. entry_point_info_t *next_image_info;
  94. next_image_info = &bl33_image_ep_info;
  95. /* Only expecting BL33: the kernel will run in EL2NS */
  96. assert(type == NON_SECURE);
  97. /* None of the images can have 0x0 as the entrypoint */
  98. if (next_image_info->pc) {
  99. return next_image_info;
  100. } else {
  101. return NULL;
  102. }
  103. }
  104. /*
  105. * Even though we sell the FPGA UART as an SBSA variant, it is actually
  106. * a full fledged PL011. So the baudrate divider registers exist.
  107. */
  108. #ifndef UARTIBRD
  109. #define UARTIBRD 0x024
  110. #define UARTFBRD 0x028
  111. #endif
  112. /* Round an integer to the closest multiple of a value. */
  113. static unsigned int round_multiple(unsigned int x, unsigned int multiple)
  114. {
  115. if (multiple < 2) {
  116. return x;
  117. }
  118. return ((x + (multiple / 2 - 1)) / multiple) * multiple;
  119. }
  120. #define PL011_FRAC_SHIFT 6
  121. #define FPGA_DEFAULT_BAUDRATE 38400
  122. #define PL011_OVERSAMPLING 16
  123. static unsigned int pl011_freq_from_divider(unsigned int divider)
  124. {
  125. unsigned int freq;
  126. freq = divider * FPGA_DEFAULT_BAUDRATE * PL011_OVERSAMPLING;
  127. return freq >> PL011_FRAC_SHIFT;
  128. }
  129. /*
  130. * The FPGAs run most peripherals from one main clock, among them the CPUs,
  131. * the arch timer, and the UART baud base clock.
  132. * The SCP knows this frequency and programs the UART clock divider for a
  133. * 38400 bps baudrate. Recalculate the base input clock from there.
  134. */
  135. static unsigned int fpga_get_system_frequency(void)
  136. {
  137. const void *fdt = (void *)(uintptr_t)FPGA_PRELOADED_DTB_BASE;
  138. int node, err;
  139. /*
  140. * If the arch timer DT node has an explicit clock-frequency property
  141. * set, use that, to allow people overriding auto-detection.
  142. */
  143. node = fdt_node_offset_by_compatible(fdt, 0, "arm,armv8-timer");
  144. if (node >= 0) {
  145. uint32_t freq;
  146. err = fdt_read_uint32(fdt, node, "clock-frequency", &freq);
  147. if (err >= 0) {
  148. return freq;
  149. }
  150. }
  151. node = fdt_node_offset_by_compatible(fdt, 0, "arm,pl011");
  152. if (node >= 0) {
  153. uintptr_t pl011_base;
  154. unsigned int divider;
  155. err = fdt_get_reg_props_by_index(fdt, node, 0,
  156. &pl011_base, NULL);
  157. if (err >= 0) {
  158. divider = mmio_read_32(pl011_base + UARTIBRD);
  159. divider <<= PL011_FRAC_SHIFT;
  160. divider += mmio_read_32(pl011_base + UARTFBRD);
  161. /*
  162. * The result won't be exact, due to rounding errors,
  163. * but the input frequency was a multiple of 250 KHz.
  164. */
  165. return round_multiple(pl011_freq_from_divider(divider),
  166. 250000);
  167. } else {
  168. WARN("Cannot read PL011 MMIO base\n");
  169. }
  170. } else {
  171. WARN("No PL011 DT node\n");
  172. }
  173. /* No PL011 DT node or calculation failed. */
  174. return FPGA_DEFAULT_TIMER_FREQUENCY;
  175. }
  176. unsigned int plat_get_syscnt_freq2(void)
  177. {
  178. if (system_freq == 0U) {
  179. system_freq = fpga_get_system_frequency();
  180. }
  181. return system_freq;
  182. }
  183. static void fpga_dtb_update_clock(void *fdt, unsigned int freq)
  184. {
  185. uint32_t freq_dtb = fdt32_to_cpu(freq);
  186. uint32_t phandle;
  187. int node, err;
  188. node = fdt_node_offset_by_compatible(fdt, 0, "arm,pl011");
  189. if (node < 0) {
  190. WARN("%s(): No PL011 DT node found\n", __func__);
  191. return;
  192. }
  193. err = fdt_read_uint32(fdt, node, "clocks", &phandle);
  194. if (err != 0) {
  195. WARN("Cannot find clocks property\n");
  196. return;
  197. }
  198. node = fdt_node_offset_by_phandle(fdt, phandle);
  199. if (node < 0) {
  200. WARN("Cannot get phandle\n");
  201. return;
  202. }
  203. err = fdt_setprop_inplace(fdt, node,
  204. "clock-frequency",
  205. &freq_dtb,
  206. sizeof(freq_dtb));
  207. if (err < 0) {
  208. WARN("Could not update DT baud clock frequency\n");
  209. return;
  210. }
  211. }
  212. #define CMDLINE_SIGNATURE "CMD:"
  213. static int fpga_dtb_set_commandline(void *fdt, const char *cmdline)
  214. {
  215. int chosen;
  216. const char *eol;
  217. char nul = 0;
  218. int slen, err;
  219. chosen = fdt_add_subnode(fdt, 0, "chosen");
  220. if (chosen == -FDT_ERR_EXISTS) {
  221. chosen = fdt_path_offset(fdt, "/chosen");
  222. }
  223. if (chosen < 0) {
  224. return chosen;
  225. }
  226. /*
  227. * There is most likely an EOL at the end of the
  228. * command line, make sure we terminate the line there.
  229. * We can't replace the EOL with a NUL byte in the
  230. * source, as this is in read-only memory. So we first
  231. * create the property without any termination, then
  232. * append a single NUL byte.
  233. */
  234. eol = strchr(cmdline, '\n');
  235. if (eol == NULL) {
  236. eol = strchr(cmdline, 0);
  237. }
  238. /* Skip the signature and omit the EOL/NUL byte. */
  239. slen = eol - (cmdline + strlen(CMDLINE_SIGNATURE));
  240. /*
  241. * Let's limit the size of the property, just in case
  242. * we find the signature by accident. The Linux kernel
  243. * limits to 4096 characters at most (in fact 2048 for
  244. * arm64), so that sounds like a reasonable number.
  245. */
  246. if (slen > 4095) {
  247. slen = 4095;
  248. }
  249. err = fdt_setprop(fdt, chosen, "bootargs",
  250. cmdline + strlen(CMDLINE_SIGNATURE), slen);
  251. if (err != 0) {
  252. return err;
  253. }
  254. return fdt_appendprop(fdt, chosen, "bootargs", &nul, 1);
  255. }
  256. static void fpga_prepare_dtb(void)
  257. {
  258. void *fdt = (void *)(uintptr_t)FPGA_PRELOADED_DTB_BASE;
  259. const char *cmdline = (void *)(uintptr_t)FPGA_PRELOADED_CMD_LINE;
  260. int err;
  261. err = fdt_open_into(fdt, fdt, FPGA_MAX_DTB_SIZE);
  262. if (err < 0) {
  263. ERROR("cannot open devicetree at %p: %d\n", fdt, err);
  264. panic();
  265. }
  266. /* Reserve memory used by Trusted Firmware. */
  267. if (fdt_add_reserved_memory(fdt, "tf-a@80000000", BL31_BASE,
  268. BL31_LIMIT - BL31_BASE)) {
  269. WARN("Failed to add reserved memory node to DT\n");
  270. }
  271. /* Check for the command line signature. */
  272. if (!strncmp(cmdline, CMDLINE_SIGNATURE, strlen(CMDLINE_SIGNATURE))) {
  273. err = fpga_dtb_set_commandline(fdt, cmdline);
  274. if (err == 0) {
  275. INFO("using command line at 0x%x\n",
  276. FPGA_PRELOADED_CMD_LINE);
  277. } else {
  278. ERROR("failed to put command line into DTB: %d\n", err);
  279. }
  280. }
  281. if (err < 0) {
  282. ERROR("Error %d extending Device Tree\n", err);
  283. panic();
  284. }
  285. err = fdt_add_cpus_node(fdt, FPGA_MAX_PE_PER_CPU,
  286. FPGA_MAX_CPUS_PER_CLUSTER,
  287. FPGA_MAX_CLUSTER_COUNT);
  288. if (err == -EEXIST) {
  289. WARN("Not overwriting already existing /cpus node in DTB\n");
  290. } else {
  291. if (err < 0) {
  292. ERROR("Error %d creating the /cpus DT node\n", err);
  293. panic();
  294. } else {
  295. unsigned int nr_cores = fpga_get_nr_gic_cores();
  296. INFO("Adjusting GICR DT region to cover %u cores\n",
  297. nr_cores);
  298. err = fdt_adjust_gic_redist(fdt, nr_cores,
  299. fpga_get_redist_base(),
  300. fpga_get_redist_size());
  301. if (err < 0) {
  302. ERROR("Error %d fixing up GIC DT node\n", err);
  303. }
  304. }
  305. }
  306. fpga_dtb_update_clock(fdt, system_freq);
  307. /* Check whether we support the SPE PMU. Remove the DT node if not. */
  308. if (!is_feat_spe_supported()) {
  309. int node = fdt_node_offset_by_compatible(fdt, 0,
  310. "arm,statistical-profiling-extension-v1");
  311. if (node >= 0) {
  312. fdt_del_node(fdt, node);
  313. }
  314. }
  315. /* Check whether we have an ITS. Remove the DT node if not. */
  316. if (!fpga_has_its()) {
  317. int node = fdt_node_offset_by_compatible(fdt, 0,
  318. "arm,gic-v3-its");
  319. if (node >= 0) {
  320. fdt_del_node(fdt, node);
  321. }
  322. }
  323. err = fdt_pack(fdt);
  324. if (err < 0) {
  325. ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, err);
  326. }
  327. clean_dcache_range((uintptr_t)fdt, fdt_blob_size(fdt));
  328. }
  329. void bl31_plat_runtime_setup(void)
  330. {
  331. fpga_prepare_dtb();
  332. }
  333. void bl31_plat_enable_mmu(uint32_t flags)
  334. {
  335. /* TODO: determine if MMU needs to be enabled */
  336. }