sbi_init.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <sbi/riscv_asm.h>
  10. #include <sbi/riscv_atomic.h>
  11. #include <sbi/sbi_console.h>
  12. #include <sbi/sbi_ecall.h>
  13. #include <sbi/sbi_hart.h>
  14. #include <sbi/sbi_ipi.h>
  15. #include <sbi/sbi_platform.h>
  16. #include <sbi/sbi_system.h>
  17. #include <sbi/sbi_timer.h>
  18. #include <sbi/sbi_version.h>
  19. #define BANNER \
  20. " ____ _____ ____ _____\n" \
  21. " / __ \\ / ____| _ \\_ _|\n" \
  22. " | | | |_ __ ___ _ __ | (___ | |_) || |\n" \
  23. " | | | | '_ \\ / _ \\ '_ \\ \\___ \\| _ < | |\n" \
  24. " | |__| | |_) | __/ | | |____) | |_) || |_\n" \
  25. " \\____/| .__/ \\___|_| |_|_____/|____/_____|\n" \
  26. " | |\n" \
  27. " |_|\n\n"
  28. static void sbi_boot_prints(struct sbi_scratch *scratch, u32 hartid)
  29. {
  30. char str[64];
  31. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  32. misa_string(str, sizeof(str));
  33. sbi_printf("\nOpenSBI v%d.%d (%s %s)\n", OPENSBI_VERSION_MAJOR,
  34. OPENSBI_VERSION_MINOR, __DATE__, __TIME__);
  35. sbi_printf(BANNER);
  36. /* Platform details */
  37. sbi_printf("Platform Name : %s\n", sbi_platform_name(plat));
  38. sbi_printf("Platform HART Features : RV%d%s\n", misa_xlen(), str);
  39. sbi_printf("Platform Max HARTs : %d\n",
  40. sbi_platform_hart_count(plat));
  41. sbi_printf("Current Hart : %u\n", hartid);
  42. /* Firmware details */
  43. sbi_printf("Firmware Base : 0x%lx\n", scratch->fw_start);
  44. sbi_printf("Firmware Size : %d KB\n",
  45. (u32)(scratch->fw_size / 1024));
  46. /* Generic details */
  47. sbi_printf("Runtime SBI Version : %d.%d\n",
  48. sbi_ecall_version_major(), sbi_ecall_version_minor());
  49. sbi_printf("\n");
  50. sbi_hart_pmp_dump(scratch);
  51. }
  52. static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
  53. {
  54. int rc;
  55. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  56. rc = sbi_system_early_init(scratch, TRUE);
  57. if (rc)
  58. sbi_hart_hang();
  59. rc = sbi_hart_init(scratch, hartid);
  60. if (rc)
  61. sbi_hart_hang();
  62. rc = sbi_console_init(scratch);
  63. if (rc)
  64. sbi_hart_hang();
  65. rc = sbi_platform_irqchip_init(plat, TRUE);
  66. if (rc)
  67. sbi_hart_hang();
  68. rc = sbi_ipi_init(scratch, TRUE);
  69. if (rc)
  70. sbi_hart_hang();
  71. rc = sbi_timer_init(scratch, TRUE);
  72. if (rc)
  73. sbi_hart_hang();
  74. rc = sbi_system_final_init(scratch, TRUE);
  75. if (rc)
  76. sbi_hart_hang();
  77. if (!(scratch->options & SBI_SCRATCH_NO_BOOT_PRINTS))
  78. sbi_boot_prints(scratch, hartid);
  79. if (!sbi_platform_has_hart_hotplug(plat))
  80. sbi_hart_wake_coldboot_harts(scratch, hartid);
  81. sbi_hart_mark_available(hartid);
  82. sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,
  83. scratch->next_mode);
  84. }
  85. static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)
  86. {
  87. int rc;
  88. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  89. if (!sbi_platform_has_hart_hotplug(plat))
  90. sbi_hart_wait_for_coldboot(scratch, hartid);
  91. if (sbi_platform_hart_disabled(plat, hartid))
  92. sbi_hart_hang();
  93. rc = sbi_system_early_init(scratch, FALSE);
  94. if (rc)
  95. sbi_hart_hang();
  96. rc = sbi_hart_init(scratch, hartid);
  97. if (rc)
  98. sbi_hart_hang();
  99. rc = sbi_platform_irqchip_init(plat, FALSE);
  100. if (rc)
  101. sbi_hart_hang();
  102. rc = sbi_ipi_init(scratch, FALSE);
  103. if (rc)
  104. sbi_hart_hang();
  105. rc = sbi_timer_init(scratch, FALSE);
  106. if (rc)
  107. sbi_hart_hang();
  108. rc = sbi_system_final_init(scratch, FALSE);
  109. if (rc)
  110. sbi_hart_hang();
  111. sbi_hart_mark_available(hartid);
  112. if (sbi_platform_has_hart_hotplug(plat))
  113. /* TODO: To be implemented in-future. */
  114. sbi_hart_hang();
  115. else
  116. sbi_hart_switch_mode(hartid, scratch->next_arg1,
  117. scratch->next_addr, scratch->next_mode);
  118. }
  119. static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);
  120. /**
  121. * Initialize OpenSBI library for current HART and jump to next
  122. * booting stage.
  123. *
  124. * The function expects following:
  125. * 1. The 'mscratch' CSR is pointing to sbi_scratch of current HART
  126. * 2. Stack pointer (SP) is setup for current HART
  127. * 3. Interrupts are disabled in MSTATUS CSR
  128. * 4. All interrupts are disabled in MIE CSR
  129. *
  130. * @param scratch pointer to sbi_scratch of current HART
  131. */
  132. void __noreturn sbi_init(struct sbi_scratch *scratch)
  133. {
  134. bool coldboot = FALSE;
  135. u32 hartid = sbi_current_hartid();
  136. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  137. if (sbi_platform_hart_disabled(plat, hartid))
  138. sbi_hart_hang();
  139. if (atomic_add_return(&coldboot_lottery, 1) == 1)
  140. coldboot = TRUE;
  141. if (coldboot)
  142. init_coldboot(scratch, hartid);
  143. else
  144. init_warmboot(scratch, hartid);
  145. }