1/*
2 * Copyright 2018, 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#pragma once
13
14#include <errno.h>
15#include <sel4/sel4.h>
16#include <sel4/sel4_arch/mapping.h>
17#include <utils/util.h>
18#include <vspace/arch/page.h>
19
20typedef seL4_Error(*vspace_map_fn_t)(seL4_CPtr cap, seL4_CPtr vspace_root, seL4_Word vaddr,
21                                     seL4_Word attr);
22typedef seL4_Error(*vspace_map_page_fn_t)(seL4_CPtr cap, seL4_CPtr vspace_root, seL4_Word vaddr,
23                                          seL4_CapRights_t rights, seL4_Word attr);
24
25static inline seL4_Error vspace_iospace_map_page(seL4_CPtr cap, seL4_CPtr root, seL4_Word vaddr,
26                                                 seL4_CapRights_t rights, UNUSED seL4_Word attr)
27{
28#if defined(CONFIG_IOMMU) || defined(CONFIG_TK1_SMMU)
29    return seL4_ARCH_Page_MapIO(cap, root, rights, vaddr);
30#else
31    return -1;
32#endif
33}
34
35/* Map object: defines a size, type and function for mapping an architecture specific
36 * page table */
37typedef struct {
38    /* type to pass to untype retype to create this object */
39    seL4_Word type;
40    /* size_bits of this object */
41    seL4_Word size_bits;
42    /* function that can be used to map this object */
43    vspace_map_fn_t map_fn;
44} vspace_map_obj_t;
45
46typedef int (*vspace_get_map_obj_fn)(seL4_Word failed_bits, vspace_map_obj_t *obj);
47
48/*
49 * Populate a map object for a specific number of failed bits.
50 *
51 * @param failed_bits: the failed_bits returned by seL4_MappingFailedLookupLevel() after a failed
52 *                     frame mapping operation.
53 * @param obj:         object to populate with details.
54 * @return 0 on success, EINVAL if failed_bits is invalid.
55 */
56int vspace_get_map_obj(seL4_Word failed_bits, vspace_map_obj_t *obj);
57/* As per vspace_get_map_obj but returns operations and sizes for an iospace (IOMMU). */
58int vspace_get_iospace_map_obj(UNUSED seL4_Word failed_bits, vspace_map_obj_t *obj);
59/* As per vspace_get_map_obj but returns operations and sizes for virtual page tables */
60int vspace_get_ept_map_obj(seL4_Word failed_bits, vspace_map_obj_t *obj);
61
62static inline seL4_Error vspace_map_obj(vspace_map_obj_t *obj, seL4_CPtr cap,
63                                        seL4_CPtr vspace, seL4_Word vaddr, seL4_Word attr)
64{
65    ZF_LOGF_IF(obj == NULL, "obj must not be NULL");
66    ZF_LOGF_IF(obj->map_fn == NULL, "obj must be populated");
67    return obj->map_fn(cap, vspace, vaddr, attr);
68}
69