Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
| 21 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame^] | 22 | #include "hf/arch/types.h" |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 23 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 24 | /** An opaque type for a physical address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 25 | typedef struct { |
| 26 | uintpaddr_t pa; |
| 27 | } paddr_t; |
| 28 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 29 | /** An opaque type for an intermediate physical address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 30 | typedef struct { |
| 31 | uintpaddr_t ipa; |
| 32 | } ipaddr_t; |
| 33 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 34 | /** An opaque type for a virtual address. */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 35 | typedef struct { |
| 36 | uintvaddr_t va; |
| 37 | } vaddr_t; |
| 38 | |
| 39 | /** |
| 40 | * Initializes a physical address. |
| 41 | */ |
| 42 | static inline paddr_t pa_init(uintpaddr_t p) |
| 43 | { |
| 44 | return (paddr_t){.pa = p}; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Extracts the absolute physical address. |
| 49 | */ |
| 50 | static inline uintpaddr_t pa_addr(paddr_t pa) |
| 51 | { |
| 52 | return pa.pa; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Initializes an intermeditate physical address. |
| 57 | */ |
| 58 | static inline ipaddr_t ipa_init(uintvaddr_t v) |
| 59 | { |
| 60 | return (ipaddr_t){.ipa = v}; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Extracts the absolute intermediate physical address. |
| 65 | */ |
| 66 | static inline uintpaddr_t ipa_addr(ipaddr_t ipa) |
| 67 | { |
| 68 | return ipa.ipa; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Initializes a virtual address. |
| 73 | */ |
| 74 | static inline vaddr_t va_init(uintvaddr_t v) |
| 75 | { |
| 76 | return (vaddr_t){.va = v}; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Extracts the absolute virtual address. |
| 81 | */ |
| 82 | static inline uintvaddr_t va_addr(vaddr_t va) |
| 83 | { |
| 84 | return va.va; |
| 85 | } |
| 86 | |
| 87 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 88 | * Advances a physical address. |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 89 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 90 | static inline paddr_t pa_add(paddr_t pa, size_t n) |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 91 | { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 92 | return pa_init(pa_addr(pa) + n); |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Casts a physical address to a virtual address. |
| 97 | */ |
| 98 | static inline vaddr_t va_from_pa(paddr_t pa) |
| 99 | { |
| 100 | return va_init(pa_addr(pa)); |
| 101 | } |
| 102 | |
| 103 | /** |
Andrew Scull | 1b8d044 | 2018-08-06 15:47:04 +0100 | [diff] [blame] | 104 | * Casts a physical address to an intermediate physical address. |
| 105 | */ |
| 106 | static inline ipaddr_t ipa_from_pa(paddr_t pa) |
| 107 | { |
| 108 | return ipa_init(pa_addr(pa)); |
| 109 | } |
| 110 | |
| 111 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 112 | * Casts a virtual address to a physical address. |
| 113 | */ |
| 114 | static inline paddr_t pa_from_va(vaddr_t va) |
| 115 | { |
| 116 | return pa_init(va_addr(va)); |
| 117 | } |
| 118 | |
| 119 | /** |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 120 | * Casts an intermediate physical address to a physical address. |
| 121 | */ |
| 122 | static inline paddr_t pa_from_ipa(ipaddr_t ipa) |
| 123 | { |
| 124 | return pa_init(ipa_addr(ipa)); |
| 125 | } |
| 126 | |
| 127 | /** |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 128 | * Casts a pointer to a virtual address. |
| 129 | */ |
| 130 | static inline vaddr_t va_from_ptr(const void *p) |
| 131 | { |
| 132 | return (vaddr_t){.va = (uintvaddr_t)p}; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Casts a virtual address to a pointer. Only use when the virtual address is |
| 137 | * mapped for the calling context. |
| 138 | * TODO: check the mapping for a range and return a memiter? |
| 139 | */ |
| 140 | static inline void *ptr_from_va(vaddr_t va) |
| 141 | { |
| 142 | return (void *)va_addr(va); |
| 143 | } |