1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <stdint.h>
14#include <sel4/sel4.h>
15#include <sel4/arch/bootinfo_types.h>
16#include <sel4platsupport/pmem.h>
17#include <utils/util.h>
18
19int sel4platsupport_get_num_pmem_regions(simple_t *simple) {
20    seL4_X86_BootInfo_mmap_t data;
21    int error = simple_get_extended_bootinfo(simple, SEL4_BOOTINFO_HEADER_X86_MBMMAP, &data, sizeof(seL4_X86_BootInfo_mmap_t));
22    if (error == -1) {
23        ZF_LOGW("Could not find info");
24        return 0;
25    }
26
27    return data.mmap_length/sizeof(data.mmap[0]);
28}
29
30int sel4platsupport_get_pmem_region_list(simple_t *simple, size_t max_length, pmem_region_t *region_list) {
31    seL4_X86_BootInfo_mmap_t data;
32    int error = simple_get_extended_bootinfo(simple, SEL4_BOOTINFO_HEADER_X86_MBMMAP, &data, sizeof(seL4_X86_BootInfo_mmap_t));
33    if (error == -1) {
34        ZF_LOGW("Could not find info");
35        return -1;
36    }
37    seL4_X86_mb_mmap_t *mmap = (seL4_X86_mb_mmap_t *)((unsigned long)data.mmap);
38    size_t i = 0;
39    for (i = 0; i < max_length && i < (data.mmap_length/sizeof(mmap[0])); i++) {
40        region_list[i].type = mmap[i].type == SEL4_MULTIBOOT_RAM_REGION_TYPE ? PMEM_TYPE_RAM : PMEM_TYPE_UNKNOWN;
41        region_list[i].base_addr = mmap[i].base_addr;
42        region_list[i].length  = mmap[i].length;
43    }
44    return i;
45}
46