blob: 9db671910c56681d2b0c4c7c0df3b71f4da7ab22 [file] [log] [blame]
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001/*
2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00005 */
6
7#ifndef __XLAT_TABLES_V2_H__
8#define __XLAT_TABLES_V2_H__
9
10#include <xlat_tables_defs.h>
11
12#ifndef __ASSEMBLY__
13#include <stddef.h>
14#include <stdint.h>
15#include <xlat_mmu_helpers.h>
16
17/* Helper macro to define entries for mmap_region_t. It creates
18 * identity mappings for each region.
19 */
20#define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr)
21
22/* Helper macro to define entries for mmap_region_t. It allows to
23 * re-map address mappings from 'pa' to 'va' for each region.
24 */
25#define MAP_REGION(pa, va, sz, attr) {(pa), (va), (sz), (attr)}
26
27/*
28 * Shifts and masks to access fields of an mmap_attr_t
29 */
Varun Wadekarc6a11f62017-05-25 18:04:48 -070030#define MT_TYPE_MASK U(0x7)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000031#define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK)
32/* Access permissions (RO/RW) */
Varun Wadekarc6a11f62017-05-25 18:04:48 -070033#define MT_PERM_SHIFT U(3)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000034/* Security state (SECURE/NS) */
Varun Wadekarc6a11f62017-05-25 18:04:48 -070035#define MT_SEC_SHIFT U(4)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000036/* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
Varun Wadekarc6a11f62017-05-25 18:04:48 -070037#define MT_EXECUTE_SHIFT U(5)
Antonio Nino Diazac998032017-02-27 17:23:54 +000038/* All other bits are reserved */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000039
40/*
41 * Memory mapping attributes
42 */
43typedef enum {
44 /*
45 * Memory types supported.
46 * These are organised so that, going down the list, the memory types
47 * are getting weaker; conversely going up the list the memory types are
48 * getting stronger.
49 */
50 MT_DEVICE,
51 MT_NON_CACHEABLE,
52 MT_MEMORY,
53 /* Values up to 7 are reserved to add new memory types in the future */
54
Varun Wadekarc6a11f62017-05-25 18:04:48 -070055 MT_RO = U(0) << MT_PERM_SHIFT,
56 MT_RW = U(1) << MT_PERM_SHIFT,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000057
Varun Wadekarc6a11f62017-05-25 18:04:48 -070058 MT_SECURE = U(0) << MT_SEC_SHIFT,
59 MT_NS = U(1) << MT_SEC_SHIFT,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000060
61 /*
62 * Access permissions for instruction execution are only relevant for
63 * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored
64 * (and potentially overridden) otherwise:
65 * - Device memory is always marked as execute-never.
66 * - Read-write normal memory is always marked as execute-never.
67 */
Varun Wadekarc6a11f62017-05-25 18:04:48 -070068 MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT,
69 MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000070} mmap_attr_t;
71
72#define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE)
73#define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER)
74
75/*
76 * Structure for specifying a single region of memory.
77 */
78typedef struct mmap_region {
79 unsigned long long base_pa;
80 uintptr_t base_va;
81 size_t size;
82 mmap_attr_t attr;
83} mmap_region_t;
84
85/* Generic translation table APIs */
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +010086
87/*
88 * Initialize translation tables from the current list of mmap regions. Calling
89 * this function marks the transition point after which static regions can no
90 * longer be added.
91 */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000092void init_xlat_tables(void);
93
94/*
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +010095 * Add a static region with defined base PA and base VA. This function can only
96 * be used before initializing the translation tables. The region cannot be
97 * removed afterwards.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000098 */
99void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
Sandrine Bailleux04980a32017-04-19 14:02:23 +0100100 size_t size, mmap_attr_t attr);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000101
102/*
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +0100103 * Add a dynamic region with defined base PA and base VA. This type of region
104 * can be added and removed even after the translation tables are initialized.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000105 *
106 * Returns:
107 * 0: Success.
108 * EINVAL: Invalid values were used as arguments.
109 * ERANGE: Memory limits were surpassed.
110 * ENOMEM: Not enough space in the mmap array or not enough free xlat tables.
111 * EPERM: It overlaps another region in an invalid way.
112 */
113int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
Sandrine Bailleux04980a32017-04-19 14:02:23 +0100114 size_t size, mmap_attr_t attr);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000115
116/*
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +0100117 * Add an array of static regions with defined base PA and base VA. This
118 * function can only be used before initializing the translation tables. The
119 * regions cannot be removed afterwards.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000120 */
121void mmap_add(const mmap_region_t *mm);
122
Antonio Nino Diazac998032017-02-27 17:23:54 +0000123/*
124 * Remove a region with the specified base VA and size. Only dynamic regions can
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +0100125 * be removed, and they can be removed even if the translation tables are
126 * initialized.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000127 *
128 * Returns:
129 * 0: Success.
130 * EINVAL: The specified region wasn't found.
131 * EPERM: Trying to remove a static region.
132 */
133int mmap_remove_dynamic_region(uintptr_t base_va, size_t size);
134
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000135#endif /*__ASSEMBLY__*/
136#endif /* __XLAT_TABLES_V2_H__ */