platform.c 8.2 KB

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