sbi_scratch.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 of next_arg1 member in sbi_scratch */
  18. #define SBI_SCRATCH_NEXT_ARG1_OFFSET (2 * __SIZEOF_POINTER__)
  19. /** Offset of next_addr member in sbi_scratch */
  20. #define SBI_SCRATCH_NEXT_ADDR_OFFSET (3 * __SIZEOF_POINTER__)
  21. /** Offset of next_mode member in sbi_scratch */
  22. #define SBI_SCRATCH_NEXT_MODE_OFFSET (4 * __SIZEOF_POINTER__)
  23. /** Offset of warmboot_addr member in sbi_scratch */
  24. #define SBI_SCRATCH_WARMBOOT_ADDR_OFFSET (5 * __SIZEOF_POINTER__)
  25. /** Offset of platform_addr member in sbi_scratch */
  26. #define SBI_SCRATCH_PLATFORM_ADDR_OFFSET (6 * __SIZEOF_POINTER__)
  27. /** Offset of hartid_to_scratch member in sbi_scratch */
  28. #define SBI_SCRATCH_HARTID_TO_SCRATCH_OFFSET (7 * __SIZEOF_POINTER__)
  29. /** Offset of trap_exit member in sbi_scratch */
  30. #define SBI_SCRATCH_TRAP_EXIT_OFFSET (8 * __SIZEOF_POINTER__)
  31. /** Offset of tmp0 member in sbi_scratch */
  32. #define SBI_SCRATCH_TMP0_OFFSET (9 * __SIZEOF_POINTER__)
  33. /** Offset of options member in sbi_scratch */
  34. #define SBI_SCRATCH_OPTIONS_OFFSET (10 * __SIZEOF_POINTER__)
  35. /** Offset of extra space in sbi_scratch */
  36. #define SBI_SCRATCH_EXTRA_SPACE_OFFSET (11 * __SIZEOF_POINTER__)
  37. /** Maximum size of sbi_scratch (4KB) */
  38. #define SBI_SCRATCH_SIZE (0x1000)
  39. /* clang-format on */
  40. #ifndef __ASSEMBLER__
  41. #include <sbi/sbi_types.h>
  42. /** Representation of per-HART scratch space */
  43. struct sbi_scratch {
  44. /** Start (or base) address of firmware linked to OpenSBI library */
  45. unsigned long fw_start;
  46. /** Size (in bytes) of firmware linked to OpenSBI library */
  47. unsigned long fw_size;
  48. /** Arg1 (or 'a1' register) of next booting stage for this HART */
  49. unsigned long next_arg1;
  50. /** Address of next booting stage for this HART */
  51. unsigned long next_addr;
  52. /** Priviledge mode of next booting stage for this HART */
  53. unsigned long next_mode;
  54. /** Warm boot entry point address for this HART */
  55. unsigned long warmboot_addr;
  56. /** Address of sbi_platform */
  57. unsigned long platform_addr;
  58. /** Address of HART ID to sbi_scratch conversion function */
  59. unsigned long hartid_to_scratch;
  60. /** Address of trap exit function */
  61. unsigned long trap_exit;
  62. /** Temporary storage */
  63. unsigned long tmp0;
  64. /** Options for OpenSBI library */
  65. unsigned long options;
  66. };
  67. /** Possible options for OpenSBI library */
  68. enum sbi_scratch_options {
  69. /** Disable prints during boot */
  70. SBI_SCRATCH_NO_BOOT_PRINTS = (1 << 0),
  71. /** Enable runtime debug prints */
  72. SBI_SCRATCH_DEBUG_PRINTS = (1 << 1),
  73. };
  74. /** Get pointer to sbi_scratch for current HART */
  75. #define sbi_scratch_thishart_ptr() \
  76. ((struct sbi_scratch *)csr_read(CSR_MSCRATCH))
  77. /** Get Arg1 of next booting stage for current HART */
  78. #define sbi_scratch_thishart_arg1_ptr() \
  79. ((void *)(sbi_scratch_thishart_ptr()->next_arg1))
  80. /** Initialize scratch table and allocator */
  81. int sbi_scratch_init(struct sbi_scratch *scratch);
  82. /**
  83. * Allocate from extra space in sbi_scratch
  84. *
  85. * @return zero on failure and non-zero (>= SBI_SCRATCH_EXTRA_SPACE_OFFSET)
  86. * on success
  87. */
  88. unsigned long sbi_scratch_alloc_offset(unsigned long size, const char *owner);
  89. /** Free-up extra space in sbi_scratch */
  90. void sbi_scratch_free_offset(unsigned long offset);
  91. /** Get pointer from offset in sbi_scratch */
  92. #define sbi_scratch_offset_ptr(scratch, offset) ((void *)scratch + (offset))
  93. /** Get pointer from offset in sbi_scratch for current HART */
  94. #define sbi_scratch_thishart_offset_ptr(offset) \
  95. ((void *)sbi_scratch_thishart_ptr() + (offset))
  96. /** HART id to scratch table */
  97. extern struct sbi_scratch *hartid_to_scratch_table[];
  98. /** Get sbi_scratch from HART id */
  99. #define sbi_hartid_to_scratch(__hartid) \
  100. hartid_to_scratch_table[__hartid]
  101. /** Last HART id having a sbi_scratch pointer */
  102. extern u32 last_hartid_having_scratch;
  103. /** Get last HART id having a sbi_scratch pointer */
  104. #define sbi_scratch_last_hartid() last_hartid_having_scratch
  105. #endif
  106. #endif