1 /*
2 * Copyright 2018 The Hafnium Authors.
3 *
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.
7 */
8
9 #pragma once
10
11 #include <stddef.h>
12 #include <stdint.h>
13
14 #include "hf/arch/types.h"
15
16 #include "hf/assert.h"
17
18 /** An opaque type for a physical address. */
19 typedef struct {
20 uintpaddr_t pa;
21 } paddr_t;
22
23 /** An opaque type for an intermediate physical address. */
24 typedef struct {
25 uintpaddr_t ipa;
26 } ipaddr_t;
27
28 /** An opaque type for a virtual address. */
29 typedef struct {
30 uintvaddr_t va;
31 } vaddr_t;
32
33 /**
34 * Initializes a physical address.
35 */
pa_init(uintpaddr_t p)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 */
pa_addr(paddr_t pa)44 static inline uintpaddr_t pa_addr(paddr_t pa)
45 {
46 return pa.pa;
47 }
48
49 /**
50 * Advances a physical address.
51 */
pa_add(paddr_t pa,size_t n)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 /**
58 * Move backward physical address.
59 */
pa_subtract(paddr_t pa,size_t n)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 /**
66 * Returns the difference between two physical addresses.
67 */
pa_difference(paddr_t start,paddr_t end)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 /**
74 * Initializes an intermediate physical address.
75 */
ipa_init(uintpaddr_t ipa)76 static inline ipaddr_t ipa_init(uintpaddr_t ipa)
77 {
78 return (ipaddr_t){.ipa = ipa};
79 }
80
81 /**
82 * Subtract from a physical address.
83 */
pa_sub(paddr_t pa,size_t n)84 static inline paddr_t pa_sub(paddr_t pa, size_t n)
85 {
86 assert((uintptr_t)pa_addr(pa) >= n);
87 return pa_init(pa_addr(pa) - n);
88 }
89
90 /**
91 * Extracts the absolute intermediate physical address.
92 */
ipa_addr(ipaddr_t ipa)93 static inline uintpaddr_t ipa_addr(ipaddr_t ipa)
94 {
95 return ipa.ipa;
96 }
97
98 /**
99 * Advances an intermediate physical address.
100 */
ipa_add(ipaddr_t ipa,size_t n)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 /**
107 * Initializes a virtual address.
108 */
va_init(uintvaddr_t v)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 */
va_addr(vaddr_t va)117 static inline uintvaddr_t va_addr(vaddr_t va)
118 {
119 return va.va;
120 }
121
122 /**
123 * Casts a physical address to a virtual address.
124 */
va_from_pa(paddr_t pa)125 static inline vaddr_t va_from_pa(paddr_t pa)
126 {
127 return va_init(pa_addr(pa));
128 }
129
130 /**
131 * Casts a physical address to an intermediate physical address.
132 */
ipa_from_pa(paddr_t pa)133 static inline ipaddr_t ipa_from_pa(paddr_t pa)
134 {
135 return ipa_init(pa_addr(pa));
136 }
137
138 /**
139 * Casts a virtual address to a physical address.
140 */
pa_from_va(vaddr_t va)141 static inline paddr_t pa_from_va(vaddr_t va)
142 {
143 return pa_init(va_addr(va));
144 }
145
146 /**
147 * Casts an intermediate physical address to a physical address.
148 */
pa_from_ipa(ipaddr_t ipa)149 static inline paddr_t pa_from_ipa(ipaddr_t ipa)
150 {
151 return pa_init(ipa_addr(ipa));
152 }
153
154 /**
155 * Casts a pointer to a virtual address.
156 */
va_from_ptr(const void * p)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 */
ptr_from_va(vaddr_t va)167 static inline void *ptr_from_va(vaddr_t va)
168 {
169 return (void *)va_addr(va);
170 }
171
172 /**
173 * Advances a virtual address.
174 */
va_add(vaddr_t va,size_t n)175 static inline vaddr_t va_add(vaddr_t va, size_t n)
176 {
177 return va_init(va_addr(va) + n);
178 }
179