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#pragma once
14
15#include <inttypes.h>
16#include <stdint.h>
17#include <platsupport/io.h>
18
19typedef enum pmem_type {
20    PMEM_TYPE_RAM,
21    PMEM_TYPE_UNKNOWN,
22    PMEM_TYPE_DEVICE,
23    PMEM_NUM_REGION_TYPES,
24} pmem_type_t;
25
26typedef struct pmem_region {
27    pmem_type_t type;
28    /* these specifically match the grub boot header struct definitions,
29     * so must both be 64 bit on all systems */
30    uint64_t base_addr;
31    uint64_t length;
32} pmem_region_t;
33
34/*
35 * Map a single pmem region.
36 *
37 * @param ops    to use,
38 * @param region to unmap,
39 * @param cached map the mappings cached or not,
40 * @param        flags to pass through to mapper,
41 * @return       vaddr the pmem is mapped to, NULL on failure.
42 */
43static inline void *ps_pmem_map(ps_io_ops_t *ops, pmem_region_t region, bool cached, ps_mem_flags_t flags)
44{
45    void *vaddr = ps_io_map(&ops->io_mapper, region.base_addr, region.length, cached, flags);
46    if (vaddr == NULL) {
47        ZF_LOGE("Failed to map paddr %p length %" PRIu64 "\n", (void *) (uintptr_t) region.base_addr, region.length);
48    }
49    return vaddr;
50}
51
52/*
53 * Unmap a single pmem region.
54 *
55 * @param ops    to use,
56 * @param region to unmap,
57 * @param vaddr  the pmem_region is mapped to,
58 * @param mapper to unmap with.
59 */
60static inline void ps_pmem_unmap(ps_io_ops_t *ops, pmem_region_t region, void *vaddr) {
61    ps_io_unmap(&ops->io_mapper, vaddr, region.length);
62}
63