sbi_scratch.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef __SBI_SCRATCH_H__
  10. #define __SBI_SCRATCH_H__
  11. #include <sbi/riscv_asm.h>
  12. /* clang-format off */
  13. /** Offset of fw_start member in sbi_scratch */
  14. #define SBI_SCRATCH_FW_START_OFFSET (0 * __SIZEOF_POINTER__)
  15. /** Offset of fw_size member in sbi_scratch */
  16. #define SBI_SCRATCH_FW_SIZE_OFFSET (1 * __SIZEOF_POINTER__)
  17. /** Offset (in sbi_scratch) of the R/W Offset */
  18. #define SBI_SCRATCH_FW_RW_OFFSET (2 * __SIZEOF_POINTER__)
  19. /** Offset of next_arg1 member in sbi_scratch */
  20. #define SBI_SCRATCH_NEXT_ARG1_OFFSET (3 * __SIZEOF_POINTER__)
  21. /** Offset of next_addr member in sbi_scratch */
  22. #define SBI_SCRATCH_NEXT_ADDR_OFFSET (4 * __SIZEOF_POINTER__)
  23. /** Offset of next_mode member in sbi_scratch */
  24. #define SBI_SCRATCH_NEXT_MODE_OFFSET (5 * __SIZEOF_POINTER__)
  25. /** Offset of warmboot_addr member in sbi_scratch */
  26. #define SBI_SCRATCH_WARMBOOT_ADDR_OFFSET (6 * __SIZEOF_POINTER__)
  27. /** Offset of platform_addr member in sbi_scratch */
  28. #define SBI_SCRATCH_PLATFORM_ADDR_OFFSET (7 * __SIZEOF_POINTER__)
  29. /** Offset of hartid_to_scratch member in sbi_scratch */
  30. #define SBI_SCRATCH_HARTID_TO_SCRATCH_OFFSET (8 * __SIZEOF_POINTER__)
  31. /** Offset of trap_exit member in sbi_scratch */
  32. #define SBI_SCRATCH_TRAP_EXIT_OFFSET (9 * __SIZEOF_POINTER__)
  33. /** Offset of tmp0 member in sbi_scratch */
  34. #define SBI_SCRATCH_TMP0_OFFSET (10 * __SIZEOF_POINTER__)
  35. /** Offset of options member in sbi_scratch */
  36. #define SBI_SCRATCH_OPTIONS_OFFSET (11 * __SIZEOF_POINTER__)
  37. /** Offset of extra space in sbi_scratch */
  38. #define SBI_SCRATCH_EXTRA_SPACE_OFFSET (12 * __SIZEOF_POINTER__)
  39. /** Maximum size of sbi_scratch (4KB) */
  40. #define SBI_SCRATCH_SIZE (0x1000)
  41. /* clang-format on */
  42. #ifndef __ASSEMBLER__
  43. #include <sbi/sbi_types.h>
  44. /** Representation of per-HART scratch space */
  45. struct sbi_scratch {
  46. /** Start (or base) address of firmware linked to OpenSBI library */
  47. unsigned long fw_start;
  48. /** Size (in bytes) of firmware linked to OpenSBI library */
  49. unsigned long fw_size;
  50. /** Offset (in bytes) of the R/W section */
  51. unsigned long fw_rw_offset;
  52. /** Arg1 (or 'a1' register) of next booting stage for this HART */
  53. unsigned long next_arg1;
  54. /** Address of next booting stage for this HART */
  55. unsigned long next_addr;
  56. /** Privilege mode of next booting stage for this HART */
  57. unsigned long next_mode;
  58. /** Warm boot entry point address for this HART */
  59. unsigned long warmboot_addr;
  60. /** Address of sbi_platform */
  61. unsigned long platform_addr;
  62. /** Address of HART ID to sbi_scratch conversion function */
  63. unsigned long hartid_to_scratch;
  64. /** Address of trap exit function */
  65. unsigned long trap_exit;
  66. /** Temporary storage */
  67. unsigned long tmp0;
  68. /** Options for OpenSBI library */
  69. unsigned long options;
  70. };
  71. /**
  72. * Prevent modification of struct sbi_scratch from affecting
  73. * SBI_SCRATCH_xxx_OFFSET
  74. */
  75. _Static_assert(
  76. offsetof(struct sbi_scratch, fw_start)
  77. == SBI_SCRATCH_FW_START_OFFSET,
  78. "struct sbi_scratch definition has changed, please redefine "
  79. "SBI_SCRATCH_FW_START_OFFSET");
  80. _Static_assert(
  81. offsetof(struct sbi_scratch, fw_size)
  82. == SBI_SCRATCH_FW_SIZE_OFFSET,
  83. "struct sbi_scratch definition has changed, please redefine "
  84. "SBI_SCRATCH_FW_SIZE_OFFSET");
  85. _Static_assert(
  86. offsetof(struct sbi_scratch, next_arg1)
  87. == SBI_SCRATCH_NEXT_ARG1_OFFSET,
  88. "struct sbi_scratch definition has changed, please redefine "
  89. "SBI_SCRATCH_NEXT_ARG1_OFFSET");
  90. _Static_assert(
  91. offsetof(struct sbi_scratch, next_addr)
  92. == SBI_SCRATCH_NEXT_ADDR_OFFSET,
  93. "struct sbi_scratch definition has changed, please redefine "
  94. "SBI_SCRATCH_NEXT_ADDR_OFFSET");
  95. _Static_assert(
  96. offsetof(struct sbi_scratch, next_mode)
  97. == SBI_SCRATCH_NEXT_MODE_OFFSET,
  98. "struct sbi_scratch definition has changed, please redefine "
  99. "SBI_SCRATCH_NEXT_MODE_OFFSET");
  100. _Static_assert(
  101. offsetof(struct sbi_scratch, warmboot_addr)
  102. == SBI_SCRATCH_WARMBOOT_ADDR_OFFSET,
  103. "struct sbi_scratch definition has changed, please redefine "
  104. "SBI_SCRATCH_WARMBOOT_ADDR_OFFSET");
  105. _Static_assert(
  106. offsetof(struct sbi_scratch, platform_addr)
  107. == SBI_SCRATCH_PLATFORM_ADDR_OFFSET,
  108. "struct sbi_scratch definition has changed, please redefine "
  109. "SBI_SCRATCH_PLATFORM_ADDR_OFFSET");
  110. _Static_assert(
  111. offsetof(struct sbi_scratch, hartid_to_scratch)
  112. == SBI_SCRATCH_HARTID_TO_SCRATCH_OFFSET,
  113. "struct sbi_scratch definition has changed, please redefine "
  114. "SBI_SCRATCH_HARTID_TO_SCRATCH_OFFSET");
  115. _Static_assert(
  116. offsetof(struct sbi_scratch, trap_exit)
  117. == SBI_SCRATCH_TRAP_EXIT_OFFSET,
  118. "struct sbi_scratch definition has changed, please redefine "
  119. "SBI_SCRATCH_TRAP_EXIT_OFFSET");
  120. _Static_assert(
  121. offsetof(struct sbi_scratch, tmp0)
  122. == SBI_SCRATCH_TMP0_OFFSET,
  123. "struct sbi_scratch definition has changed, please redefine "
  124. "SBI_SCRATCH_TMP0_OFFSET");
  125. _Static_assert(
  126. offsetof(struct sbi_scratch, options)
  127. == SBI_SCRATCH_OPTIONS_OFFSET,
  128. "struct sbi_scratch definition has changed, please redefine "
  129. "SBI_SCRATCH_OPTIONS_OFFSET");
  130. /** Possible options for OpenSBI library */
  131. enum sbi_scratch_options {
  132. /** Disable prints during boot */
  133. SBI_SCRATCH_NO_BOOT_PRINTS = (1 << 0),
  134. /** Enable runtime debug prints */
  135. SBI_SCRATCH_DEBUG_PRINTS = (1 << 1),
  136. };
  137. /** Get pointer to sbi_scratch for current HART */
  138. #define sbi_scratch_thishart_ptr() \
  139. ((struct sbi_scratch *)csr_read(CSR_MSCRATCH))
  140. /** Get Arg1 of next booting stage for current HART */
  141. #define sbi_scratch_thishart_arg1_ptr() \
  142. ((void *)(sbi_scratch_thishart_ptr()->next_arg1))
  143. /** Initialize scratch table and allocator */
  144. int sbi_scratch_init(struct sbi_scratch *scratch);
  145. /**
  146. * Allocate from extra space in sbi_scratch
  147. *
  148. * @return zero on failure and non-zero (>= SBI_SCRATCH_EXTRA_SPACE_OFFSET)
  149. * on success
  150. */
  151. unsigned long sbi_scratch_alloc_offset(unsigned long size);
  152. /** Free-up extra space in sbi_scratch */
  153. void sbi_scratch_free_offset(unsigned long offset);
  154. /** Get pointer from offset in sbi_scratch */
  155. #define sbi_scratch_offset_ptr(scratch, offset) (void *)((char *)(scratch) + (offset))
  156. /** Get pointer from offset in sbi_scratch for current HART */
  157. #define sbi_scratch_thishart_offset_ptr(offset) \
  158. (void *)((char *)sbi_scratch_thishart_ptr() + (offset))
  159. /** HART id to scratch table */
  160. extern struct sbi_scratch *hartid_to_scratch_table[];
  161. /** Get sbi_scratch from HART id */
  162. #define sbi_hartid_to_scratch(__hartid) \
  163. hartid_to_scratch_table[__hartid]
  164. /** Last HART id having a sbi_scratch pointer */
  165. extern u32 last_hartid_having_scratch;
  166. /** Get last HART id having a sbi_scratch pointer */
  167. #define sbi_scratch_last_hartid() last_hartid_having_scratch
  168. #endif
  169. #endif