blob: 15d32b5c85434ca628b0bbca3e2ba3d773ac7c82 [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/**
Andrew Scull80871322018-08-06 12:04:09 +010073 * Advances a physical address.
Andrew Scull8dce4982018-08-06 13:02:20 +010074 */
Andrew Scull80871322018-08-06 12:04:09 +010075static inline paddr_t pa_add(paddr_t pa, size_t n)
Andrew Scull8dce4982018-08-06 13:02:20 +010076{
Andrew Scull80871322018-08-06 12:04:09 +010077 return pa_init(pa_addr(pa) + n);
Andrew Scull8dce4982018-08-06 13:02:20 +010078}
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/**
Andrew Scull1b8d0442018-08-06 15:47:04 +010089 * Casts a physical address to an intermediate physical address.
90 */
91static inline ipaddr_t ipa_from_pa(paddr_t pa)
92{
93 return ipa_init(pa_addr(pa));
94}
95
96/**
Andrew Scull8dce4982018-08-06 13:02:20 +010097 * Casts a virtual address to a physical address.
98 */
99static inline paddr_t pa_from_va(vaddr_t va)
100{
101 return pa_init(va_addr(va));
102}
103
104/**
105 * Casts a pointer to a virtual address.
106 */
107static inline vaddr_t va_from_ptr(const void *p)
108{
109 return (vaddr_t){.va = (uintvaddr_t)p};
110}
111
112/**
113 * Casts a virtual address to a pointer. Only use when the virtual address is
114 * mapped for the calling context.
115 * TODO: check the mapping for a range and return a memiter?
116 */
117static inline void *ptr_from_va(vaddr_t va)
118{
119 return (void *)va_addr(va);
120}
121
122#endif /* _ADDR_H */