Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 9 | #pragma once |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 10 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 11 | #include <stddef.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 12 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 13 | #include "hf/addr.h" |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 14 | #include "hf/mm.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 15 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 16 | #include "vmapi/hf/ffa.h" |
| 17 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 18 | /** |
| 19 | * Creates an absent PTE. |
Andrew Walbran | 2513374 | 2018-09-28 16:28:02 +0100 | [diff] [blame] | 20 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 21 | pte_t arch_mm_absent_pte(mm_level_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 24 | * Creates a table PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 25 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 26 | pte_t arch_mm_table_pte(mm_level_t level, paddr_t pa); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Creates a block PTE. |
| 30 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 31 | pte_t arch_mm_block_pte(mm_level_t level, paddr_t pa, mm_attr_t attrs); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 32 | |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 33 | enum mm_pte_type arch_mm_pte_type(pte_t pte, mm_level_t level); |
| 34 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 35 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 36 | * Checks whether a block is allowed at the given level of the page table. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 37 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 38 | bool arch_mm_is_block_allowed(mm_level_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 39 | |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 40 | static inline bool arch_mm_pte_is_absent(pte_t pte, mm_level_t level) |
| 41 | { |
| 42 | return arch_mm_pte_type(pte, level) == PTE_TYPE_ABSENT; |
| 43 | } |
| 44 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Determines if a PTE is present i.e. it contains information and therefore |
| 47 | * needs to exist in the page table. Any non-absent PTE is present. |
| 48 | */ |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 49 | static inline bool arch_mm_pte_is_present(pte_t pte, mm_level_t level) |
| 50 | { |
| 51 | return !arch_mm_pte_is_absent(pte, level); |
| 52 | } |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Determines if a PTE is valid i.e. it can affect the address space. Tables and |
| 56 | * valid blocks fall into this category. Invalid blocks do not as they hold |
| 57 | * information about blocks that are not in the address space. |
| 58 | */ |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 59 | static inline bool arch_mm_pte_is_valid(pte_t pte, mm_level_t level) |
| 60 | { |
| 61 | switch (arch_mm_pte_type(pte, level)) { |
| 62 | case PTE_TYPE_ABSENT: |
| 63 | case PTE_TYPE_INVALID_BLOCK: |
| 64 | return false; |
| 65 | case PTE_TYPE_VALID_BLOCK: |
| 66 | case PTE_TYPE_TABLE: |
| 67 | return true; |
| 68 | } |
| 69 | } |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Determines if a PTE is a block and represents an address range, valid or |
| 73 | * invalid. |
| 74 | */ |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 75 | static inline bool arch_mm_pte_is_block(pte_t pte, mm_level_t level) |
| 76 | { |
| 77 | switch (arch_mm_pte_type(pte, level)) { |
| 78 | case PTE_TYPE_ABSENT: |
| 79 | case PTE_TYPE_TABLE: |
| 80 | return false; |
| 81 | case PTE_TYPE_INVALID_BLOCK: |
| 82 | case PTE_TYPE_VALID_BLOCK: |
| 83 | return true; |
| 84 | } |
| 85 | } |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Determines if a PTE represents a reference to a table of PTEs. |
| 89 | */ |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 90 | static inline bool arch_mm_pte_is_table(pte_t pte, mm_level_t level) |
| 91 | { |
| 92 | return arch_mm_pte_type(pte, level) == PTE_TYPE_TABLE; |
| 93 | } |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 94 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 95 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 96 | * Extracts the start address of the PTE range. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 97 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 98 | paddr_t arch_mm_block_from_pte(pte_t pte, mm_level_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 99 | |
| 100 | /** |
Karl Meakin | aacfd4f | 2025-02-08 19:30:52 +0000 | [diff] [blame] | 101 | * Extracts the table referenced by the PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 102 | */ |
Karl Meakin | aacfd4f | 2025-02-08 19:30:52 +0000 | [diff] [blame] | 103 | struct mm_page_table *arch_mm_table_from_pte(pte_t pte, mm_level_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 106 | * Extracts the attributes of the PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 107 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 108 | mm_attr_t arch_mm_pte_attrs(pte_t pte, mm_level_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
Karl Meakin | 23122e1 | 2025-02-05 14:44:20 +0000 | [diff] [blame] | 111 | * Merges the attributes of a block into those of its parent table. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 112 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 113 | mm_attr_t arch_mm_combine_table_entry_attrs(mm_attr_t table_attrs, |
| 114 | mm_attr_t block_attrs); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 117 | * Invalidates the given range of stage-1 TLB. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 118 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 119 | void arch_mm_invalidate_stage1_range(ffa_id_t asid, vaddr_t va_begin, |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 120 | vaddr_t va_end); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 121 | |
| 122 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 123 | * Invalidates the given range of stage-2 TLB. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 124 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 125 | void arch_mm_invalidate_stage2_range(ffa_id_t vmid, ipaddr_t va_begin, |
Olivier Deprez | 6f40037 | 2022-03-07 09:31:08 +0100 | [diff] [blame] | 126 | ipaddr_t va_end, bool non_secure); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 129 | * Writes back the given range of virtual memory to such a point that all cores |
| 130 | * and devices will see the updated values. The corresponding cache lines are |
| 131 | * also invalidated. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 132 | */ |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 133 | void arch_mm_flush_dcache(void *base, size_t size); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 136 | * Sets the maximum level allowed in the page table for stage-1. |
| 137 | */ |
Karl Meakin | a3a9f95 | 2025-02-08 00:11:16 +0000 | [diff] [blame] | 138 | void arch_mm_stage1_root_level_set(uint32_t pa_bits); |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 141 | * Gets the maximum level allowed in the page table for stage-1. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 142 | */ |
Karl Meakin | a3a9f95 | 2025-02-08 00:11:16 +0000 | [diff] [blame] | 143 | mm_level_t arch_mm_stage1_root_level(void); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 146 | * Gets the maximum level allowed in the page table for stage-2. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 147 | */ |
Karl Meakin | a3a9f95 | 2025-02-08 00:11:16 +0000 | [diff] [blame] | 148 | mm_level_t arch_mm_stage2_root_level(void); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 151 | * Gets the number of concatenated page tables used at the root for stage-1. |
| 152 | * |
| 153 | * Tables are concatenated at the root to avoid introducing another level in the |
| 154 | * page table meaning the table is shallow and wide. Each level is an extra |
| 155 | * memory access when walking the table so keeping it shallow reduces the memory |
| 156 | * accesses to aid performance. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 157 | */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 158 | uint8_t arch_mm_stage1_root_table_count(void); |
| 159 | |
| 160 | /** |
| 161 | * Gets the number of concatenated page tables used at the root for stage-2. |
| 162 | */ |
| 163 | uint8_t arch_mm_stage2_root_table_count(void); |
| 164 | |
| 165 | /** |
| 166 | * Converts the mode into stage-1 attributes for a block PTE. |
| 167 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 168 | mm_attr_t arch_mm_mode_to_stage1_attrs(mm_mode_t mode); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Converts the mode into stage-2 attributes for a block PTE. |
| 172 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 173 | mm_attr_t arch_mm_mode_to_stage2_attrs(mm_mode_t mode); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 176 | * Converts the stage-2 block attributes back to the corresponding mode. |
| 177 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 178 | mm_mode_t arch_mm_stage2_attrs_to_mode(mm_attr_t attrs); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 179 | |
| 180 | /** |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 181 | * Converts the stage-1 block attributes back to the corresponding mode. |
| 182 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 183 | mm_mode_t arch_mm_stage1_attrs_to_mode(mm_attr_t attrs); |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 184 | |
| 185 | /** |
Andrew Scull | c280bee | 2019-08-14 11:11:03 +0100 | [diff] [blame] | 186 | * Initializes the arch specific memory management. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 187 | */ |
Karl Meakin | e1aeb1d | 2025-02-08 00:35:14 +0000 | [diff] [blame] | 188 | bool arch_mm_init(const struct mm_ptable *ptable); |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * Return the arch specific mm mode for send/recv pages of given VM ID. |
| 192 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 193 | mm_mode_t arch_mm_extra_mode_from_vm(ffa_id_t id); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Execute any barriers or synchronization that is required |
| 197 | * by a given architecture, after page table writes. |
| 198 | */ |
| 199 | void arch_mm_sync_table_writes(void); |
Federico Recanati | 4fd065d | 2021-12-13 20:06:23 +0100 | [diff] [blame] | 200 | |
| 201 | /** |
Jens Wiklander | 4f1880c | 2022-10-19 17:00:14 +0200 | [diff] [blame] | 202 | * Returns the maximum supported PA Range index. |
| 203 | */ |
| 204 | uint64_t arch_mm_get_pa_range(void); |
| 205 | |
| 206 | /** |
Federico Recanati | 4fd065d | 2021-12-13 20:06:23 +0100 | [diff] [blame] | 207 | * Returns the maximum supported PA Range in bits. |
| 208 | */ |
Jens Wiklander | 4f1880c | 2022-10-19 17:00:14 +0200 | [diff] [blame] | 209 | uint32_t arch_mm_get_pa_bits(uint64_t pa_range); |
Olivier Deprez | b7f6bd6 | 2022-03-08 10:55:52 +0100 | [diff] [blame] | 210 | |
Maksims Svecovs | 7efb163 | 2022-03-29 17:05:24 +0100 | [diff] [blame] | 211 | /** |
| 212 | * Returns VTCR_EL2 configured in arch_mm_init. |
| 213 | */ |
Olivier Deprez | b7f6bd6 | 2022-03-08 10:55:52 +0100 | [diff] [blame] | 214 | uintptr_t arch_mm_get_vtcr_el2(void); |
| 215 | |
Maksims Svecovs | 7efb163 | 2022-03-29 17:05:24 +0100 | [diff] [blame] | 216 | /** |
| 217 | * Returns VSTCR_EL2 configured in arch_mm_init. |
| 218 | */ |
Olivier Deprez | b7f6bd6 | 2022-03-08 10:55:52 +0100 | [diff] [blame] | 219 | uintptr_t arch_mm_get_vstcr_el2(void); |