Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 1 | #ifndef _ARCH_MM_H |
| 2 | #define _ARCH_MM_H |
| 3 | |
| 4 | #include <stdbool.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | /* A phypiscal address. */ |
| 9 | typedef size_t paddr_t; |
| 10 | |
| 11 | /* A virtual address. */ |
| 12 | typedef size_t vaddr_t; |
| 13 | |
| 14 | /* A page table entry. */ |
| 15 | typedef size_t pte_t; |
| 16 | |
| 17 | #define PAGE_LEVEL_BITS 9 |
| 18 | #define PAGE_BITS 12 |
| 19 | |
| 20 | struct arch_mm_ptable { |
| 21 | int max_level; |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * Initialises the architecture-dependents aspects of the page table. |
| 26 | */ |
| 27 | static inline void arch_mm_ptable_init(struct arch_mm_ptable *t) |
| 28 | { |
| 29 | t->max_level = 2; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Determines the maximum level supported by the given page table. |
| 34 | */ |
| 35 | static inline int arch_mm_max_level(struct arch_mm_ptable *t) |
| 36 | { |
| 37 | return t->max_level; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Converts a physical address to a table PTE. |
| 42 | * |
| 43 | * The spec says that 'Table descriptors for stage 2 translations do not |
| 44 | * include any attribute field', so we don't take any attributes as arguments. |
| 45 | */ |
| 46 | static inline pte_t arch_mm_pa_to_table_pte(paddr_t pa) |
| 47 | { |
| 48 | return pa | 0x3; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Converts a physical address to a block PTE. |
| 53 | */ |
| 54 | static inline pte_t arch_mm_pa_to_block_pte(paddr_t pa, uint64_t attrs) |
| 55 | { |
| 56 | return pa | attrs; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Converts a physical address to a page PTE. |
| 61 | */ |
| 62 | static inline pte_t arch_mm_pa_to_page_pte(paddr_t pa, uint64_t attrs) |
| 63 | { |
| 64 | return pa | attrs | ((attrs & 1) << 1); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Converts a block PTE to a page PTE. |
| 69 | */ |
| 70 | static inline pte_t arch_mm_block_to_page_pte(pte_t pte) |
| 71 | { |
| 72 | return pte | 2; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Specifies whether block mappings are acceptable at the given level. |
| 77 | */ |
| 78 | static inline bool arch_mm_is_block_allowed(int level) |
| 79 | { |
| 80 | return level == 1 || level == 2; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the encoding of a page table entry that isn't present. |
| 85 | */ |
| 86 | static inline pte_t arch_mm_absent_pte(void) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Determines if the given pte is present, i.e., if it points to another table, |
| 93 | * to a page, or a block of pages. |
| 94 | */ |
| 95 | static inline bool arch_mm_pte_is_present(pte_t pte) |
| 96 | { |
| 97 | return (pte & 1) != 0; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Determines if the given pte references another table. |
| 102 | */ |
| 103 | static inline bool arch_mm_pte_is_table(pte_t pte) |
| 104 | { |
| 105 | return (pte & 3) == 3; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Determines if the given pte references a block of pages. |
| 110 | */ |
| 111 | static inline bool arch_mm_pte_is_block(pte_t pte) |
| 112 | { |
| 113 | return (pte & 3) == 1; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Clears the given virtual address, i.e., sets the ignored bits (from a page |
| 118 | * table perspective) to zero. |
| 119 | */ |
| 120 | static inline vaddr_t arch_mm_clear_va(vaddr_t addr) |
| 121 | { |
| 122 | return addr & ~((1ull << PAGE_BITS) - 1) & ((1ull << 48) - 1); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Clears the given physical address, i.e., sets the ignored bits (from a page |
| 127 | * table perspective) to zero. |
| 128 | */ |
| 129 | static inline paddr_t arch_mm_clear_pa(paddr_t addr) |
| 130 | { |
| 131 | return addr & ~((1ull << PAGE_BITS) - 1) & ((1ull << 48) - 1); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Extracts the physical address from a page table entry. |
| 136 | */ |
| 137 | static inline paddr_t arch_mm_pte_to_paddr(pte_t pte) |
| 138 | { |
| 139 | return arch_mm_clear_pa(pte); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Extracts a page table pointer from the given page table entry. |
| 144 | */ |
| 145 | static inline pte_t *arch_mm_pte_to_table(pte_t pte) |
| 146 | { |
| 147 | return (pte_t *)arch_mm_pte_to_paddr(pte); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Invalidates stage-1 TLB entries referring to the given virtual address range. |
| 152 | */ |
| 153 | static inline void arch_mm_invalidate_stage1_range(vaddr_t begin, vaddr_t end) |
| 154 | { |
| 155 | vaddr_t it; |
| 156 | |
| 157 | begin >>= 12; |
| 158 | end >>= 12; |
| 159 | |
| 160 | __asm__ volatile("dsb ishst"); |
| 161 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame^] | 162 | for (it = begin; it < end; it += (1ull << (PAGE_BITS - 12))) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 163 | __asm__("tlbi vae2is, %0" : : "r"(it)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame^] | 164 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 165 | |
| 166 | __asm__ volatile("dsb ish"); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Invalidates stage-2 TLB entries referring to the given virtual address range. |
| 171 | */ |
| 172 | static inline void arch_mm_invalidate_stage2_range(vaddr_t begin, vaddr_t end) |
| 173 | { |
| 174 | vaddr_t it; |
| 175 | |
| 176 | begin >>= 12; |
| 177 | end >>= 12; |
| 178 | |
| 179 | __asm__ volatile("dsb ishst"); |
| 180 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame^] | 181 | for (it = begin; it < end; it += (1ull << (PAGE_BITS - 12))) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 182 | __asm__("tlbi ipas2e1, %0" : : "r"(it)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame^] | 183 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 184 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 185 | __asm__ volatile( |
| 186 | "dsb ish\n" |
| 187 | "tlbi vmalle1is\n" |
| 188 | "dsb ish\n"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | uint64_t arch_mm_mode_to_attrs(int mode); |
| 192 | void arch_mm_init(paddr_t table); |
| 193 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 194 | #endif /* _ARCH_MM_H */ |