sbi_domain.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef __SBI_DOMAIN_H__
  10. #define __SBI_DOMAIN_H__
  11. #include <sbi/sbi_types.h>
  12. #include <sbi/sbi_hartmask.h>
  13. struct sbi_scratch;
  14. /** Domain access types */
  15. enum sbi_domain_access {
  16. SBI_DOMAIN_READ = (1UL << 0),
  17. SBI_DOMAIN_WRITE = (1UL << 1),
  18. SBI_DOMAIN_EXECUTE = (1UL << 2),
  19. SBI_DOMAIN_MMIO = (1UL << 3)
  20. };
  21. /** Representation of OpenSBI domain memory region */
  22. struct sbi_domain_memregion {
  23. /**
  24. * Size of memory region as power of 2
  25. * It has to be minimum 3 and maximum __riscv_xlen
  26. */
  27. unsigned long order;
  28. /**
  29. * Base address of memory region
  30. * It must be 2^order aligned address
  31. */
  32. unsigned long base;
  33. /** Flags representing memory region attributes */
  34. #define SBI_DOMAIN_MEMREGION_READABLE (1UL << 0)
  35. #define SBI_DOMAIN_MEMREGION_WRITEABLE (1UL << 1)
  36. #define SBI_DOMAIN_MEMREGION_EXECUTABLE (1UL << 2)
  37. #define SBI_DOMAIN_MEMREGION_MMODE (1UL << 3)
  38. #define SBI_DOMAIN_MEMREGION_ACCESS_MASK (0xfUL)
  39. #define SBI_DOMAIN_MEMREGION_MMIO (1UL << 31)
  40. unsigned long flags;
  41. };
  42. /** Maximum number of domains */
  43. #define SBI_DOMAIN_MAX_INDEX 32
  44. /** Representation of OpenSBI domain */
  45. struct sbi_domain {
  46. /**
  47. * Logical index of this domain
  48. * Note: This set by sbi_domain_finalize() in the coldboot path
  49. */
  50. u32 index;
  51. /**
  52. * HARTs assigned to this domain
  53. * Note: This set by sbi_domain_init() and sbi_domain_finalize()
  54. * in the coldboot path
  55. */
  56. struct sbi_hartmask assigned_harts;
  57. /** Name of this domain */
  58. char name[64];
  59. /** Possible HARTs in this domain */
  60. const struct sbi_hartmask *possible_harts;
  61. /** Array of memory regions terminated by a region with order zero */
  62. struct sbi_domain_memregion *regions;
  63. /** HART id of the HART booting this domain */
  64. u32 boot_hartid;
  65. /** Arg1 (or 'a1' register) of next booting stage for this domain */
  66. unsigned long next_arg1;
  67. /** Address of next booting stage for this domain */
  68. unsigned long next_addr;
  69. /** Privilege mode of next booting stage for this domain */
  70. unsigned long next_mode;
  71. /** Is domain allowed to reset the system */
  72. bool system_reset_allowed;
  73. };
  74. /** HART id to domain table */
  75. extern struct sbi_domain *hartid_to_domain_table[];
  76. /** Get pointer to sbi_domain from HART id */
  77. #define sbi_hartid_to_domain(__hartid) \
  78. hartid_to_domain_table[__hartid]
  79. /** Get pointer to sbi_domain for current HART */
  80. #define sbi_domain_thishart_ptr() \
  81. sbi_hartid_to_domain(current_hartid())
  82. /** Index to domain table */
  83. extern struct sbi_domain *domidx_to_domain_table[];
  84. /** Get pointer to sbi_domain from index */
  85. #define sbi_index_to_domain(__index) \
  86. domidx_to_domain_table[__index]
  87. /** Iterate over each domain */
  88. #define sbi_domain_for_each(__i, __d) \
  89. for ((__i) = 0; ((__d) = sbi_index_to_domain(__i)); (__i)++)
  90. /** Iterate over each memory region of a domain */
  91. #define sbi_domain_for_each_memregion(__d, __r) \
  92. for ((__r) = (__d)->regions; (__r)->order; (__r)++)
  93. /**
  94. * Check whether given HART is assigned to specified domain
  95. * @param dom pointer to domain
  96. * @param hartid the HART ID
  97. * @return TRUE if HART is assigned to domain otherwise FALSE
  98. */
  99. bool sbi_domain_is_assigned_hart(const struct sbi_domain *dom, u32 hartid);
  100. /**
  101. * Get ulong assigned HART mask for given domain and HART base ID
  102. * @param dom pointer to domain
  103. * @param hbase the HART base ID
  104. * @return ulong possible HART mask
  105. * Note: the return ulong mask will be set to zero on failure.
  106. */
  107. ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
  108. ulong hbase);
  109. /** Initialize a domain memory region as firmware region */
  110. void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);
  111. /**
  112. * Check whether we can access specified address for given mode and
  113. * memory region flags under a domain
  114. * @param dom pointer to domain
  115. * @param addr the address to be checked
  116. * @param mode the privilege mode of access
  117. * @param access_flags bitmask of domain access types (enum sbi_domain_access)
  118. * @return TRUE if access allowed otherwise FALSE
  119. */
  120. bool sbi_domain_check_addr(const struct sbi_domain *dom,
  121. unsigned long addr, unsigned long mode,
  122. unsigned long access_flags);
  123. /** Dump domain details on the console */
  124. void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix);
  125. /** Dump all domain details on the console */
  126. void sbi_domain_dump_all(const char *suffix);
  127. /**
  128. * Register a new domain
  129. * @param dom pointer to domain
  130. * @param assign_mask pointer to HART mask of HARTs assigned to the domain
  131. *
  132. * @return 0 on success and negative error code on failure
  133. */
  134. int sbi_domain_register(struct sbi_domain *dom,
  135. const struct sbi_hartmask *assign_mask);
  136. /** Finalize domain tables and startup non-root domains */
  137. int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid);
  138. /** Initialize domains */
  139. int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid);
  140. #endif