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 |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 10 | |
| 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
| 13 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 14 | #include "hf/arch/types.h" |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 15 | |
Daniel Boulby | 8435071 | 2021-11-26 11:13:20 +0000 | [diff] [blame] | 16 | #include "hf/assert.h" |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 17 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 18 | /** An opaque type for a physical address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 19 | typedef struct { |
| 20 | uintpaddr_t pa; |
| 21 | } paddr_t; |
| 22 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 23 | /** An opaque type for an intermediate physical address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 24 | typedef struct { |
| 25 | uintpaddr_t ipa; |
| 26 | } ipaddr_t; |
| 27 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 28 | /** An opaque type for a virtual address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 29 | typedef struct { |
| 30 | uintvaddr_t va; |
| 31 | } vaddr_t; |
| 32 | |
| 33 | /** |
| 34 | * Initializes a physical address. |
| 35 | */ |
| 36 | static inline paddr_t pa_init(uintpaddr_t p) |
| 37 | { |
| 38 | return (paddr_t){.pa = p}; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Extracts the absolute physical address. |
| 43 | */ |
| 44 | static inline uintpaddr_t pa_addr(paddr_t pa) |
| 45 | { |
| 46 | return pa.pa; |
| 47 | } |
| 48 | |
| 49 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 50 | * Advances a physical address. |
| 51 | */ |
| 52 | static inline paddr_t pa_add(paddr_t pa, size_t n) |
| 53 | { |
| 54 | return pa_init(pa_addr(pa) + n); |
| 55 | } |
| 56 | |
| 57 | /** |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 58 | * Move backward physical address. |
| 59 | */ |
| 60 | static inline paddr_t pa_subtract(paddr_t pa, size_t n) |
| 61 | { |
| 62 | return pa_init(pa_addr(pa) - n); |
| 63 | } |
| 64 | |
| 65 | /** |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 66 | * Returns the difference between two physical addresses. |
| 67 | */ |
| 68 | static inline size_t pa_difference(paddr_t start, paddr_t end) |
| 69 | { |
| 70 | return pa_addr(end) - pa_addr(start); |
| 71 | } |
| 72 | |
| 73 | /** |
Max Shvetsov | 9c0ebe4 | 2020-08-27 12:37:57 +0100 | [diff] [blame] | 74 | * Initializes an intermediate physical address. |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 75 | */ |
Alfredo Mazzinghi | 8cc983e | 2019-02-22 15:12:36 +0000 | [diff] [blame] | 76 | static inline ipaddr_t ipa_init(uintpaddr_t ipa) |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 77 | { |
Alfredo Mazzinghi | 8cc983e | 2019-02-22 15:12:36 +0000 | [diff] [blame] | 78 | return (ipaddr_t){.ipa = ipa}; |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 82 | * Subtract from a physical address. |
| 83 | */ |
| 84 | static inline paddr_t pa_sub(paddr_t pa, size_t n) |
| 85 | { |
Daniel Boulby | 8435071 | 2021-11-26 11:13:20 +0000 | [diff] [blame] | 86 | assert((uintptr_t)pa_addr(pa) >= n); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 87 | return pa_init(pa_addr(pa) - n); |
| 88 | } |
| 89 | |
| 90 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 91 | * Extracts the absolute intermediate physical address. |
| 92 | */ |
| 93 | static inline uintpaddr_t ipa_addr(ipaddr_t ipa) |
| 94 | { |
| 95 | return ipa.ipa; |
| 96 | } |
| 97 | |
| 98 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 99 | * Advances an intermediate physical address. |
| 100 | */ |
| 101 | static inline ipaddr_t ipa_add(ipaddr_t ipa, size_t n) |
| 102 | { |
| 103 | return ipa_init(ipa_addr(ipa) + n); |
| 104 | } |
| 105 | |
| 106 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 107 | * Initializes a virtual address. |
| 108 | */ |
| 109 | static inline vaddr_t va_init(uintvaddr_t v) |
| 110 | { |
| 111 | return (vaddr_t){.va = v}; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Extracts the absolute virtual address. |
| 116 | */ |
| 117 | static inline uintvaddr_t va_addr(vaddr_t va) |
| 118 | { |
| 119 | return va.va; |
| 120 | } |
| 121 | |
| 122 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 123 | * Casts a physical address to a virtual address. |
| 124 | */ |
| 125 | static inline vaddr_t va_from_pa(paddr_t pa) |
| 126 | { |
| 127 | return va_init(pa_addr(pa)); |
| 128 | } |
| 129 | |
| 130 | /** |
Andrew Scull | 1b8d044 | 2018-08-06 15:47:04 +0100 | [diff] [blame] | 131 | * Casts a physical address to an intermediate physical address. |
| 132 | */ |
| 133 | static inline ipaddr_t ipa_from_pa(paddr_t pa) |
| 134 | { |
| 135 | return ipa_init(pa_addr(pa)); |
| 136 | } |
| 137 | |
| 138 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 139 | * Casts a virtual address to a physical address. |
| 140 | */ |
| 141 | static inline paddr_t pa_from_va(vaddr_t va) |
| 142 | { |
| 143 | return pa_init(va_addr(va)); |
| 144 | } |
| 145 | |
| 146 | /** |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 147 | * Casts an intermediate physical address to a physical address. |
| 148 | */ |
| 149 | static inline paddr_t pa_from_ipa(ipaddr_t ipa) |
| 150 | { |
| 151 | return pa_init(ipa_addr(ipa)); |
| 152 | } |
| 153 | |
| 154 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 155 | * Casts a pointer to a virtual address. |
| 156 | */ |
| 157 | static inline vaddr_t va_from_ptr(const void *p) |
| 158 | { |
| 159 | return (vaddr_t){.va = (uintvaddr_t)p}; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Casts a virtual address to a pointer. Only use when the virtual address is |
| 164 | * mapped for the calling context. |
| 165 | * TODO: check the mapping for a range and return a memiter? |
| 166 | */ |
| 167 | static inline void *ptr_from_va(vaddr_t va) |
| 168 | { |
| 169 | return (void *)va_addr(va); |
| 170 | } |
Raghu Krishnamurthy | ea6d25f | 2021-09-14 15:27:06 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Advances a virtual address. |
| 174 | */ |
| 175 | static inline vaddr_t va_add(vaddr_t va, size_t n) |
| 176 | { |
| 177 | return va_init(va_addr(va) + n); |
| 178 | } |