platform.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/irqchip/fdt_irqchip.h>
  19. #include <sbi_utils/serial/fdt_serial.h>
  20. #include <sbi_utils/timer/fdt_timer.h>
  21. #include <sbi_utils/ipi/fdt_ipi.h>
  22. #include <sbi_utils/reset/fdt_reset.h>
  23. extern const struct platform_override sifive_fu540;
  24. static const struct platform_override *special_platforms[] = {
  25. &sifive_fu540,
  26. };
  27. static const struct platform_override *generic_plat = NULL;
  28. static const struct fdt_match *generic_plat_match = NULL;
  29. static void fw_platform_lookup_special(void *fdt, int root_offset)
  30. {
  31. int pos, noff;
  32. const struct platform_override *plat;
  33. const struct fdt_match *match;
  34. for (pos = 0; pos < array_size(special_platforms); pos++) {
  35. plat = special_platforms[pos];
  36. if (!plat->match_table)
  37. continue;
  38. noff = fdt_find_match(fdt, -1, plat->match_table, &match);
  39. if (noff < 0)
  40. continue;
  41. generic_plat = plat;
  42. generic_plat_match = match;
  43. break;
  44. }
  45. }
  46. extern struct sbi_platform platform;
  47. static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
  48. /*
  49. * The fw_platform_init() function is called very early on the boot HART
  50. * OpenSBI reference firmwares so that platform specific code get chance
  51. * to update "platform" instance before it is used.
  52. *
  53. * The arguments passed to fw_platform_init() function are boot time state
  54. * of A0 to A4 register. The "arg0" will be boot HART id and "arg1" will
  55. * be address of FDT passed by previous booting stage.
  56. *
  57. * The return value of fw_platform_init() function is the FDT location. If
  58. * FDT is unchanged (or FDT is modified in-place) then fw_platform_init()
  59. * can always return the original FDT location (i.e. 'arg1') unmodified.
  60. */
  61. unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
  62. unsigned long arg2, unsigned long arg3,
  63. unsigned long arg4)
  64. {
  65. const char *model;
  66. void *fdt = (void *)arg1;
  67. u32 hartid, hart_count = 0;
  68. int rc, root_offset, cpus_offset, cpu_offset, len;
  69. root_offset = fdt_path_offset(fdt, "/");
  70. if (root_offset < 0)
  71. goto fail;
  72. fw_platform_lookup_special(fdt, root_offset);
  73. model = fdt_getprop(fdt, root_offset, "model", &len);
  74. if (model)
  75. sbi_strncpy(platform.name, model, sizeof(platform.name));
  76. if (generic_plat && generic_plat->features)
  77. platform.features = generic_plat->features(generic_plat_match);
  78. cpus_offset = fdt_path_offset(fdt, "/cpus");
  79. if (cpus_offset < 0)
  80. goto fail;
  81. fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
  82. rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
  83. if (rc)
  84. continue;
  85. if (SBI_HARTMASK_MAX_BITS <= hartid)
  86. continue;
  87. generic_hart_index2id[hart_count++] = hartid;
  88. }
  89. platform.hart_count = hart_count;
  90. /* Return original FDT pointer */
  91. return arg1;
  92. fail:
  93. while (1)
  94. wfi();
  95. }
  96. static int generic_early_init(bool cold_boot)
  97. {
  98. int rc;
  99. if (generic_plat && generic_plat->early_init) {
  100. rc = generic_plat->early_init(cold_boot, generic_plat_match);
  101. if (rc)
  102. return rc;
  103. }
  104. if (!cold_boot)
  105. return 0;
  106. return fdt_reset_init();
  107. }
  108. static int generic_final_init(bool cold_boot)
  109. {
  110. void *fdt;
  111. int rc;
  112. if (generic_plat && generic_plat->final_init) {
  113. rc = generic_plat->final_init(cold_boot, generic_plat_match);
  114. if (rc)
  115. return rc;
  116. }
  117. if (!cold_boot)
  118. return 0;
  119. fdt = sbi_scratch_thishart_arg1_ptr();
  120. fdt_cpu_fixup(fdt);
  121. fdt_fixups(fdt);
  122. fdt_domain_fixup(fdt);
  123. if (generic_plat && generic_plat->fdt_fixup) {
  124. rc = generic_plat->fdt_fixup(fdt, generic_plat_match);
  125. if (rc)
  126. return rc;
  127. }
  128. return 0;
  129. }
  130. static void generic_early_exit(void)
  131. {
  132. if (generic_plat && generic_plat->early_exit)
  133. generic_plat->early_exit(generic_plat_match);
  134. }
  135. static void generic_final_exit(void)
  136. {
  137. if (generic_plat && generic_plat->final_exit)
  138. generic_plat->final_exit(generic_plat_match);
  139. }
  140. static int generic_domains_init(void)
  141. {
  142. return fdt_domains_populate(sbi_scratch_thishart_arg1_ptr());
  143. }
  144. static u64 generic_tlbr_flush_limit(void)
  145. {
  146. if (generic_plat && generic_plat->tlbr_flush_limit)
  147. return generic_plat->tlbr_flush_limit(generic_plat_match);
  148. return SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT;
  149. }
  150. static int generic_system_reset_check(u32 reset_type, u32 reset_reason)
  151. {
  152. if (generic_plat && generic_plat->system_reset_check)
  153. return generic_plat->system_reset_check(reset_type,
  154. reset_reason,
  155. generic_plat_match);
  156. return fdt_system_reset_check(reset_type, reset_reason);
  157. }
  158. static void generic_system_reset(u32 reset_type, u32 reset_reason)
  159. {
  160. if (generic_plat && generic_plat->system_reset) {
  161. generic_plat->system_reset(reset_type, reset_reason,
  162. generic_plat_match);
  163. return;
  164. }
  165. fdt_system_reset(reset_type, reset_reason);
  166. }
  167. const struct sbi_platform_operations platform_ops = {
  168. .early_init = generic_early_init,
  169. .final_init = generic_final_init,
  170. .early_exit = generic_early_exit,
  171. .final_exit = generic_final_exit,
  172. .domains_init = generic_domains_init,
  173. .console_putc = fdt_serial_putc,
  174. .console_getc = fdt_serial_getc,
  175. .console_init = fdt_serial_init,
  176. .irqchip_init = fdt_irqchip_init,
  177. .irqchip_exit = fdt_irqchip_exit,
  178. .ipi_send = fdt_ipi_send,
  179. .ipi_clear = fdt_ipi_clear,
  180. .ipi_init = fdt_ipi_init,
  181. .ipi_exit = fdt_ipi_exit,
  182. .get_tlbr_flush_limit = generic_tlbr_flush_limit,
  183. .timer_value = fdt_timer_value,
  184. .timer_event_stop = fdt_timer_event_stop,
  185. .timer_event_start = fdt_timer_event_start,
  186. .timer_init = fdt_timer_init,
  187. .timer_exit = fdt_timer_exit,
  188. .system_reset_check = generic_system_reset_check,
  189. .system_reset = generic_system_reset,
  190. };
  191. struct sbi_platform platform = {
  192. .opensbi_version = OPENSBI_VERSION,
  193. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x01),
  194. .name = "Generic",
  195. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  196. .hart_count = SBI_HARTMASK_MAX_BITS,
  197. .hart_index2id = generic_hart_index2id,
  198. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  199. .platform_ops_addr = (unsigned long)&platform_ops
  200. };