#include <sbi/sbi_console.h>
#include <sbi/sbi_domain.h>
#include <sbi/sbi_hartmask.h>
+#include <sbi/sbi_heap.h>
#include <sbi/sbi_hsm.h>
#include <sbi/sbi_math.h>
#include <sbi/sbi_platform.h>
static u32 domain_count = 0;
static bool domain_finalized = false;
-static struct sbi_hartmask root_hmask = { 0 };
-
#define ROOT_REGION_MAX 16
static u32 root_memregs_count = 0;
-static struct sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1] = { 0 };
struct sbi_domain root = {
.name = "root",
- .possible_harts = &root_hmask,
- .regions = root_memregs,
+ .possible_harts = NULL,
+ .regions = NULL,
.system_reset_allowed = true,
.system_suspend_allowed = true,
.fw_region_inited = false,
const struct sbi_platform *plat = sbi_platform_thishart_ptr();
/* Sanity checks */
- if (!reg || domain_finalized ||
- (root.regions != root_memregs) ||
+ if (!reg || domain_finalized || !root.regions ||
(ROOT_REGION_MAX <= root_memregs_count))
return SBI_EINVAL;
}
/* Append the memregion to root memregions */
- nreg = &root_memregs[root_memregs_count];
+ nreg = &root.regions[root_memregs_count];
sbi_memcpy(nreg, reg, sizeof(*reg));
root_memregs_count++;
- root_memregs[root_memregs_count].order = 0;
+ root.regions[root_memregs_count].order = 0;
/* Sort and optimize root regions */
do {
int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
{
u32 i;
+ int rc;
+ struct sbi_hartmask *root_hmask;
+ struct sbi_domain_memregion *root_memregs;
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
if (scratch->fw_rw_offset == 0 ||
return SBI_EINVAL;
}
+ root_memregs = sbi_calloc(sizeof(*root_memregs), ROOT_REGION_MAX + 1);
+ if (!root_memregs) {
+ sbi_printf("%s: no memory for root regions\n", __func__);
+ return SBI_ENOMEM;
+ }
+ root.regions = root_memregs;
+
+ root_hmask = sbi_zalloc(sizeof(*root_hmask));
+ if (!root_hmask) {
+ sbi_printf("%s: no memory for root hartmask\n", __func__);
+ sbi_free(root_memregs);
+ return SBI_ENOMEM;
+ }
+ root.possible_harts = root_hmask;
+
/* Root domain firmware memory region */
sbi_domain_memregion_init(scratch->fw_start, scratch->fw_rw_offset,
(SBI_DOMAIN_MEMREGION_M_READABLE |
for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
if (sbi_platform_hart_invalid(plat, i))
continue;
- sbi_hartmask_set_hart(i, &root_hmask);
+ sbi_hartmask_set_hart(i, root_hmask);
+ }
+
+ /* Finally register the root domain */
+ rc = sbi_domain_register(&root, root_hmask);
+ if (rc) {
+ sbi_free(root_hmask);
+ sbi_free(root_memregs);
+ return rc;
}
- return sbi_domain_register(&root, &root_hmask);
+ return 0;
}