blob: 24a910ca49cc2f22db8d301bd51f4efa1b9903b4 [file] [log] [blame]
Andrew Scull8dce4982018-08-06 13:02:20 +01001#ifndef _ADDR_H
2#define _ADDR_H
3
4#include <stddef.h>
5#include <stdint.h>
6
7#include "arch_addr.h"
8
9/* An opaque type for a physical address. */
10typedef struct {
11 uintpaddr_t pa;
12} paddr_t;
13
14/* An opaque type for an intermediate physical address. */
15typedef struct {
16 uintpaddr_t ipa;
17} ipaddr_t;
18
19/* An opaque type for a virtual address. */
20typedef struct {
21 uintvaddr_t va;
22} vaddr_t;
23
24/**
25 * Initializes a physical address.
26 */
27static inline paddr_t pa_init(uintpaddr_t p)
28{
29 return (paddr_t){.pa = p};
30}
31
32/**
33 * Extracts the absolute physical address.
34 */
35static inline uintpaddr_t pa_addr(paddr_t pa)
36{
37 return pa.pa;
38}
39
40/**
41 * Initializes an intermeditate physical address.
42 */
43static inline ipaddr_t ipa_init(uintvaddr_t v)
44{
45 return (ipaddr_t){.ipa = v};
46}
47
48/**
49 * Extracts the absolute intermediate physical address.
50 */
51static inline uintpaddr_t ipa_addr(ipaddr_t ipa)
52{
53 return ipa.ipa;
54}
55
56/**
57 * Initializes a virtual address.
58 */
59static inline vaddr_t va_init(uintvaddr_t v)
60{
61 return (vaddr_t){.va = v};
62}
63
64/**
65 * Extracts the absolute virtual address.
66 */
67static inline uintvaddr_t va_addr(vaddr_t va)
68{
69 return va.va;
70}
71
72/**
73 * Advances a virtual address.
74 */
75static inline vaddr_t va_add(vaddr_t va, size_t n)
76{
77 return va_init(va_addr(va) + n);
78}
79
80/**
81 * Casts a physical address to a virtual address.
82 */
83static inline vaddr_t va_from_pa(paddr_t pa)
84{
85 return va_init(pa_addr(pa));
86}
87
88/**
89 * Casts a virtual address to a physical address.
90 */
91static inline paddr_t pa_from_va(vaddr_t va)
92{
93 return pa_init(va_addr(va));
94}
95
96/**
97 * Casts a pointer to a virtual address.
98 */
99static inline vaddr_t va_from_ptr(const void *p)
100{
101 return (vaddr_t){.va = (uintvaddr_t)p};
102}
103
104/**
105 * Casts a virtual address to a pointer. Only use when the virtual address is
106 * mapped for the calling context.
107 * TODO: check the mapping for a range and return a memiter?
108 */
109static inline void *ptr_from_va(vaddr_t va)
110{
111 return (void *)va_addr(va);
112}
113
114#endif /* _ADDR_H */