platform.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <libfdt.h>
  10. #include <platform_override.h>
  11. #include <sbi/riscv_asm.h>
  12. #include <sbi/sbi_hartmask.h>
  13. #include <sbi/sbi_platform.h>
  14. #include <sbi/sbi_string.h>
  15. #include <sbi_utils/fdt/fdt_domain.h>
  16. #include <sbi_utils/fdt/fdt_fixup.h>
  17. #include <sbi_utils/fdt/fdt_helper.h>
  18. #include <sbi_utils/fdt/fdt_pmu.h>
  19. #include <sbi_utils/irqchip/fdt_irqchip.h>
  20. #include <sbi_utils/irqchip/imsic.h>
  21. #include <sbi_utils/serial/fdt_serial.h>
  22. #include <sbi_utils/timer/fdt_timer.h>
  23. #include <sbi_utils/ipi/fdt_ipi.h>
  24. #include <sbi_utils/reset/fdt_reset.h>
  25. #include <sbi_utils/serial/semihosting.h>
  26. /* List of platform override modules generated at compile time */
  27. extern const struct platform_override *platform_override_modules[];
  28. extern unsigned long platform_override_modules_size;
  29. static const struct platform_override *generic_plat = NULL;
  30. static const struct fdt_match *generic_plat_match = NULL;
  31. static void fw_platform_lookup_special(void *fdt, int root_offset)
  32. {
  33. const struct platform_override *plat;
  34. const struct fdt_match *match;
  35. int pos;
  36. for (pos = 0; pos < platform_override_modules_size; pos++) {
  37. plat = platform_override_modules[pos];
  38. if (!plat->match_table)
  39. continue;
  40. match = fdt_match_node(fdt, root_offset, plat->match_table);
  41. if (!match)
  42. continue;
  43. generic_plat = plat;
  44. generic_plat_match = match;
  45. break;
  46. }
  47. }
  48. extern struct sbi_platform platform;
  49. static bool platform_has_mlevel_imsic = false;
  50. static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
  51. /*
  52. * The fw_platform_init() function is called very early on the boot HART
  53. * OpenSBI reference firmwares so that platform specific code get chance
  54. * to update "platform" instance before it is used.
  55. *
  56. * The arguments passed to fw_platform_init() function are boot time state
  57. * of A0 to A4 register. The "arg0" will be boot HART id and "arg1" will
  58. * be address of FDT passed by previous booting stage.
  59. *
  60. * The return value of fw_platform_init() function is the FDT location. If
  61. * FDT is unchanged (or FDT is modified in-place) then fw_platform_init()
  62. * can always return the original FDT location (i.e. 'arg1') unmodified.
  63. */
  64. unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
  65. unsigned long arg2, unsigned long arg3,
  66. unsigned long arg4)
  67. {
  68. const char *model;
  69. void *fdt = (void *)arg1;
  70. u32 hartid, hart_count = 0;
  71. int rc, root_offset, cpus_offset, cpu_offset, len;
  72. root_offset = fdt_path_offset(fdt, "/");
  73. if (root_offset < 0)
  74. goto fail;
  75. fw_platform_lookup_special(fdt, root_offset);
  76. if (generic_plat && generic_plat->fw_init)
  77. generic_plat->fw_init(fdt, generic_plat_match);
  78. model = fdt_getprop(fdt, root_offset, "model", &len);
  79. if (model)
  80. sbi_strncpy(platform.name, model, sizeof(platform.name) - 1);
  81. if (generic_plat && generic_plat->features)
  82. platform.features = generic_plat->features(generic_plat_match);
  83. cpus_offset = fdt_path_offset(fdt, "/cpus");
  84. if (cpus_offset < 0)
  85. goto fail;
  86. fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
  87. rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
  88. if (rc)
  89. continue;
  90. if (SBI_HARTMASK_MAX_BITS <= hartid)
  91. continue;
  92. if (!fdt_node_is_enabled(fdt, cpu_offset))
  93. continue;
  94. generic_hart_index2id[hart_count++] = hartid;
  95. }
  96. platform.hart_count = hart_count;
  97. platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
  98. /* Return original FDT pointer */
  99. return arg1;
  100. fail:
  101. while (1)
  102. wfi();
  103. }
  104. static bool generic_cold_boot_allowed(u32 hartid)
  105. {
  106. if (generic_plat && generic_plat->cold_boot_allowed)
  107. return generic_plat->cold_boot_allowed(
  108. hartid, generic_plat_match);
  109. return true;
  110. }
  111. static int generic_nascent_init(void)
  112. {
  113. if (platform_has_mlevel_imsic)
  114. imsic_local_irqchip_init();
  115. return 0;
  116. }
  117. static int generic_early_init(bool cold_boot)
  118. {
  119. if (!generic_plat || !generic_plat->early_init)
  120. return 0;
  121. return generic_plat->early_init(cold_boot, generic_plat_match);
  122. }
  123. static int generic_final_init(bool cold_boot)
  124. {
  125. void *fdt;
  126. int rc;
  127. if (cold_boot)
  128. fdt_reset_init();
  129. if (generic_plat && generic_plat->final_init) {
  130. rc = generic_plat->final_init(cold_boot, generic_plat_match);
  131. if (rc)
  132. return rc;
  133. }
  134. if (!cold_boot)
  135. return 0;
  136. fdt = fdt_get_address();
  137. fdt_cpu_fixup(fdt);
  138. fdt_fixups(fdt);
  139. fdt_domain_fixup(fdt);
  140. if (generic_plat && generic_plat->fdt_fixup) {
  141. rc = generic_plat->fdt_fixup(fdt, generic_plat_match);
  142. if (rc)
  143. return rc;
  144. }
  145. return 0;
  146. }
  147. static bool generic_vendor_ext_check(void)
  148. {
  149. return (generic_plat && generic_plat->vendor_ext_provider) ?
  150. true : false;
  151. }
  152. static int generic_vendor_ext_provider(long extid, long funcid,
  153. const struct sbi_trap_regs *regs,
  154. unsigned long *out_value,
  155. struct sbi_trap_info *out_trap)
  156. {
  157. return generic_plat->vendor_ext_provider(extid, funcid, regs,
  158. out_value, out_trap,
  159. generic_plat_match);
  160. }
  161. static void generic_early_exit(void)
  162. {
  163. if (generic_plat && generic_plat->early_exit)
  164. generic_plat->early_exit(generic_plat_match);
  165. }
  166. static void generic_final_exit(void)
  167. {
  168. if (generic_plat && generic_plat->final_exit)
  169. generic_plat->final_exit(generic_plat_match);
  170. }
  171. static int generic_extensions_init(struct sbi_hart_features *hfeatures)
  172. {
  173. if (generic_plat && generic_plat->extensions_init)
  174. return generic_plat->extensions_init(generic_plat_match,
  175. hfeatures);
  176. return 0;
  177. }
  178. static int generic_domains_init(void)
  179. {
  180. return fdt_domains_populate(fdt_get_address());
  181. }
  182. static u64 generic_tlbr_flush_limit(void)
  183. {
  184. if (generic_plat && generic_plat->tlbr_flush_limit)
  185. return generic_plat->tlbr_flush_limit(generic_plat_match);
  186. return SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT;
  187. }
  188. static int generic_pmu_init(void)
  189. {
  190. return fdt_pmu_setup(fdt_get_address());
  191. }
  192. static uint64_t generic_pmu_xlate_to_mhpmevent(uint32_t event_idx,
  193. uint64_t data)
  194. {
  195. uint64_t evt_val = 0;
  196. /* data is valid only for raw events and is equal to event selector */
  197. if (event_idx == SBI_PMU_EVENT_RAW_IDX)
  198. evt_val = data;
  199. else {
  200. /**
  201. * Generic platform follows the SBI specification recommendation
  202. * i.e. zero extended event_idx is used as mhpmevent value for
  203. * hardware general/cache events if platform does't define one.
  204. */
  205. evt_val = fdt_pmu_get_select_value(event_idx);
  206. if (!evt_val)
  207. evt_val = (uint64_t)event_idx;
  208. }
  209. return evt_val;
  210. }
  211. static int generic_console_init(void)
  212. {
  213. if (semihosting_enabled())
  214. return semihosting_init();
  215. else
  216. return fdt_serial_init();
  217. }
  218. const struct sbi_platform_operations platform_ops = {
  219. .cold_boot_allowed = generic_cold_boot_allowed,
  220. .nascent_init = generic_nascent_init,
  221. .early_init = generic_early_init,
  222. .final_init = generic_final_init,
  223. .early_exit = generic_early_exit,
  224. .final_exit = generic_final_exit,
  225. .extensions_init = generic_extensions_init,
  226. .domains_init = generic_domains_init,
  227. .console_init = generic_console_init,
  228. .irqchip_init = fdt_irqchip_init,
  229. .irqchip_exit = fdt_irqchip_exit,
  230. .ipi_init = fdt_ipi_init,
  231. .ipi_exit = fdt_ipi_exit,
  232. .pmu_init = generic_pmu_init,
  233. .pmu_xlate_to_mhpmevent = generic_pmu_xlate_to_mhpmevent,
  234. .get_tlbr_flush_limit = generic_tlbr_flush_limit,
  235. .timer_init = fdt_timer_init,
  236. .timer_exit = fdt_timer_exit,
  237. .vendor_ext_check = generic_vendor_ext_check,
  238. .vendor_ext_provider = generic_vendor_ext_provider,
  239. };
  240. struct sbi_platform platform = {
  241. .opensbi_version = OPENSBI_VERSION,
  242. .platform_version =
  243. SBI_PLATFORM_VERSION(CONFIG_PLATFORM_GENERIC_MAJOR_VER,
  244. CONFIG_PLATFORM_GENERIC_MINOR_VER),
  245. .name = CONFIG_PLATFORM_GENERIC_NAME,
  246. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  247. .hart_count = SBI_HARTMASK_MAX_BITS,
  248. .hart_index2id = generic_hart_index2id,
  249. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  250. .platform_ops_addr = (unsigned long)&platform_ops
  251. };