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 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 21 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 22 | #include "hf/addr.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 23 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 24 | /* |
| 25 | * A page table entry (PTE) will take one of the following forms: |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 26 | * |
| 27 | * 1. absent : There is no mapping. |
| 28 | * 2. invalid block : Represents a block that is not in the address space. |
| 29 | * 3. valid block : Represents a block that is in the address space. |
| 30 | * 4. table : Represents a reference to a table of PTEs. |
| 31 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 32 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Creates an absent PTE. |
Andrew Walbran | 2513374 | 2018-09-28 16:28:02 +0100 | [diff] [blame] | 35 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 36 | pte_t arch_mm_absent_pte(uint8_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 39 | * Creates a table PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 40 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 41 | pte_t arch_mm_table_pte(uint8_t level, paddr_t pa); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Creates a block PTE. |
| 45 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 46 | pte_t arch_mm_block_pte(uint8_t level, paddr_t pa, uint64_t attrs); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 49 | * 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] | 50 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 51 | bool arch_mm_is_block_allowed(uint8_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Determines if a PTE is present i.e. it contains information and therefore |
| 55 | * needs to exist in the page table. Any non-absent PTE is present. |
| 56 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 57 | bool arch_mm_pte_is_present(pte_t pte, uint8_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Determines if a PTE is valid i.e. it can affect the address space. Tables and |
| 61 | * valid blocks fall into this category. Invalid blocks do not as they hold |
| 62 | * information about blocks that are not in the address space. |
| 63 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 64 | bool arch_mm_pte_is_valid(pte_t pte, uint8_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Determines if a PTE is a block and represents an address range, valid or |
| 68 | * invalid. |
| 69 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 70 | bool arch_mm_pte_is_block(pte_t pte, uint8_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Determines if a PTE represents a reference to a table of PTEs. |
| 74 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 75 | bool arch_mm_pte_is_table(pte_t pte, uint8_t level); |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 76 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Clears the bits of an address that are ignored by the page table. In effect, |
| 79 | * the address is rounded down to the start of the corresponding PTE range. |
| 80 | */ |
Andrew Scull | 459d3b5 | 2018-12-07 16:37:12 +0000 | [diff] [blame] | 81 | paddr_t arch_mm_clear_pa(paddr_t pa); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 84 | * Extracts the start address of the PTE range. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 85 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 86 | paddr_t arch_mm_block_from_pte(pte_t pte, uint8_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 89 | * Extracts the address of the table referenced by the PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 90 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 91 | paddr_t arch_mm_table_from_pte(pte_t pte, uint8_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 94 | * Extracts the attributes of the PTE. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 95 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 96 | uint64_t arch_mm_pte_attrs(pte_t pte, uint8_t level); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 99 | * Merges the attributes of a block into those of its containing table. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 100 | */ |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 101 | uint64_t arch_mm_combine_table_entry_attrs(uint64_t table_attrs, |
| 102 | uint64_t block_attrs); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 105 | * Invalidates the given range of stage-1 TLB. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 106 | */ |
Andrew Scull | 459d3b5 | 2018-12-07 16:37:12 +0000 | [diff] [blame] | 107 | void arch_mm_invalidate_stage1_range(vaddr_t va_begin, vaddr_t va_end); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
Andrew Scull | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 110 | * Invalidates the given range of stage-2 TLB. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 111 | */ |
Andrew Scull | 459d3b5 | 2018-12-07 16:37:12 +0000 | [diff] [blame] | 112 | void arch_mm_invalidate_stage2_range(ipaddr_t va_begin, ipaddr_t va_end); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Writes the given range of virtual memory back to the point of unification so |
| 116 | * all cores and devices will see the updated values. |
| 117 | */ |
Andrew Scull | 459d3b5 | 2018-12-07 16:37:12 +0000 | [diff] [blame] | 118 | void arch_mm_write_back_dcache(void *base, size_t size); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 121 | * Gets the maximum level allowed in the page table for stage-1. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 122 | */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 123 | uint8_t arch_mm_stage1_max_level(void); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 126 | * Gets the maximum level allowed in the page table for stage-2. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 127 | */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 128 | uint8_t arch_mm_stage2_max_level(void); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 129 | |
| 130 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 131 | * Gets the number of concatenated page tables used at the root for stage-1. |
| 132 | * |
| 133 | * Tables are concatenated at the root to avoid introducing another level in the |
| 134 | * page table meaning the table is shallow and wide. Each level is an extra |
| 135 | * memory access when walking the table so keeping it shallow reduces the memory |
| 136 | * accesses to aid performance. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 137 | */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 138 | uint8_t arch_mm_stage1_root_table_count(void); |
| 139 | |
| 140 | /** |
| 141 | * Gets the number of concatenated page tables used at the root for stage-2. |
| 142 | */ |
| 143 | uint8_t arch_mm_stage2_root_table_count(void); |
| 144 | |
| 145 | /** |
| 146 | * Converts the mode into stage-1 attributes for a block PTE. |
| 147 | */ |
| 148 | uint64_t arch_mm_mode_to_stage1_attrs(int mode); |
| 149 | |
| 150 | /** |
| 151 | * Converts the mode into stage-2 attributes for a block PTE. |
| 152 | */ |
| 153 | uint64_t arch_mm_mode_to_stage2_attrs(int mode); |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 156 | * Converts the stage-2 block attributes back to the corresponding mode. |
| 157 | */ |
| 158 | int arch_mm_stage2_attrs_to_mode(uint64_t attrs); |
| 159 | |
| 160 | /** |
Andrew Scull | c280bee | 2019-08-14 11:11:03 +0100 | [diff] [blame^] | 161 | * Initializes the arch specific memory management. |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 162 | */ |
Andrew Scull | c280bee | 2019-08-14 11:11:03 +0100 | [diff] [blame^] | 163 | bool arch_mm_init(void); |
| 164 | |
| 165 | /** |
| 166 | * Enables the current CPU with arch specific memory management state. |
| 167 | */ |
| 168 | void arch_mm_enable(paddr_t table); |