sbi_domain.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_MMIO (1UL << 3)
  38. #define SBI_DOMAIN_MEMREGION_MMODE (1UL << 4)
  39. unsigned long flags;
  40. };
  41. /** Maximum number of domains */
  42. #define SBI_DOMAIN_MAX_INDEX 32
  43. /** Representation of OpenSBI domain */
  44. struct sbi_domain {
  45. /**
  46. * Logical index of this domain
  47. * Note: This set by sbi_domain_finalize() in the coldboot path
  48. */
  49. u32 index;
  50. /**
  51. * HARTs assigned to this domain
  52. * Note: This set by sbi_domain_init() and sbi_domain_finalize()
  53. * in the coldboot path
  54. */
  55. struct sbi_hartmask assigned_harts;
  56. /** Name of this domain */
  57. char name[64];
  58. /** Possible HARTs in this domain */
  59. const struct sbi_hartmask *possible_harts;
  60. /** Array of memory regions terminated by a region with order zero */
  61. struct sbi_domain_memregion *regions;
  62. /** HART id of the HART booting this domain */
  63. u32 boot_hartid;
  64. /** Arg1 (or 'a1' register) of next booting stage for this domain */
  65. unsigned long next_arg1;
  66. /** Address of next booting stage for this domain */
  67. unsigned long next_addr;
  68. /** Privilege mode of next booting stage for this domain */
  69. unsigned long next_mode;
  70. /** Is domain allowed to reset the system */
  71. bool system_reset_allowed;
  72. };
  73. /** HART id to domain table */
  74. extern struct sbi_domain *hartid_to_domain_table[];
  75. /** Get pointer to sbi_domain from HART id */
  76. #define sbi_hartid_to_domain(__hartid) \
  77. hartid_to_domain_table[__hartid]
  78. /** Get pointer to sbi_domain for current HART */
  79. #define sbi_domain_thishart_ptr() \
  80. sbi_hartid_to_domain(current_hartid())
  81. /** Index to domain table */
  82. extern struct sbi_domain *domidx_to_domain_table[];
  83. /** Get pointer to sbi_domain from index */
  84. #define sbi_index_to_domain(__index) \
  85. domidx_to_domain_table[__index]
  86. /** Iterate over each domain */
  87. #define sbi_domain_for_each(__i, __d) \
  88. for ((__i) = 0; ((__d) = sbi_index_to_domain(__i)); (__i)++)
  89. /** Iterate over each memory region of a domain */
  90. #define sbi_domain_for_each_memregion(__d, __r) \
  91. for ((__r) = (__d)->regions; (__r)->order; (__r)++)
  92. /**
  93. * Check whether given HART is assigned to specified domain
  94. * @param dom pointer to domain
  95. * @param hartid the HART ID
  96. * @return TRUE if HART is assigned to domain otherwise FALSE
  97. */
  98. bool sbi_domain_is_assigned_hart(const struct sbi_domain *dom, u32 hartid);
  99. /**
  100. * Get ulong assigned HART mask for given domain and HART base ID
  101. * @param dom pointer to domain
  102. * @param hbase the HART base ID
  103. * @return ulong possible HART mask
  104. * Note: the return ulong mask will be set to zero on failure.
  105. */
  106. ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
  107. ulong hbase);
  108. /** Initialize a domain memory region as firmware region */
  109. void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);
  110. /**
  111. * Check whether we can access specified address for given mode and
  112. * memory region flags under a domain
  113. * @param dom pointer to domain
  114. * @param addr the address to be checked
  115. * @param mode the privilege mode of access
  116. * @param access_flags bitmask of domain access types (enum sbi_domain_access)
  117. * @return TRUE if access allowed otherwise FALSE
  118. */
  119. bool sbi_domain_check_addr(const struct sbi_domain *dom,
  120. unsigned long addr, unsigned long mode,
  121. unsigned long access_flags);
  122. /** Dump domain details on the console */
  123. void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix);
  124. /** Dump all domain details on the console */
  125. void sbi_domain_dump_all(const char *suffix);
  126. /** Finalize domain tables and startup non-root domains */
  127. int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid);
  128. /** Initialize domains */
  129. int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid);
  130. #endif