Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 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 | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #include "hf/arch/barriers.h" |
| 15 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 16 | #include "hf/check.h" |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 17 | |
| 18 | /* Opaque types for different sized fields of memory mapped IO. */ |
| 19 | |
| 20 | typedef struct { |
| 21 | volatile uint8_t *ptr; |
| 22 | } io8_t; |
| 23 | |
| 24 | typedef struct { |
| 25 | volatile uint16_t *ptr; |
| 26 | } io16_t; |
| 27 | |
| 28 | typedef struct { |
| 29 | volatile uint32_t *ptr; |
| 30 | } io32_t; |
| 31 | |
| 32 | typedef struct { |
| 33 | volatile uint64_t *ptr; |
| 34 | } io64_t; |
| 35 | |
| 36 | typedef struct { |
| 37 | volatile uint8_t *base; |
| 38 | size_t count; |
| 39 | } io8_array_t; |
| 40 | |
| 41 | typedef struct { |
| 42 | volatile uint16_t *base; |
| 43 | size_t count; |
| 44 | } io16_array_t; |
| 45 | |
| 46 | typedef struct { |
| 47 | volatile uint32_t *base; |
| 48 | size_t count; |
| 49 | } io32_array_t; |
| 50 | |
| 51 | typedef struct { |
| 52 | volatile uint64_t *base; |
| 53 | size_t count; |
| 54 | } io64_array_t; |
| 55 | |
| 56 | /* Contructors for literals. */ |
| 57 | |
| 58 | #define IO8_C(addr) ((io8_t){.ptr = (volatile uint8_t *)(addr)}) |
| 59 | #define IO16_C(addr) ((io16_t){.ptr = (volatile uint16_t *)(addr)}) |
| 60 | #define IO32_C(addr) ((io32_t){.ptr = (volatile uint32_t *)(addr)}) |
| 61 | #define IO64_C(addr) ((io64_t){.ptr = (volatile uint64_t *)(addr)}) |
| 62 | |
| 63 | #define IO8_ARRAY_C(addr, cnt) \ |
| 64 | ((io8_array_t){.base = (volatile uint8_t *)(addr), .count = (cnt)}) |
| 65 | #define IO16_ARRAY_C(addr, cnt) \ |
| 66 | ((io16_array_t){.base = (volatile uint16_t *)(addr), .count = (cnt)}) |
| 67 | #define IO32_ARRAY_C(addr, cnt) \ |
| 68 | ((io32_array_t){.base = (volatile uint32_t *)(addr), .count = (cnt)}) |
| 69 | #define IO64_ARRAY_C(addr, cnt) \ |
| 70 | ((io64_array_t){.base = (volatile uint64_t *)(addr), .count = (cnt)}) |
| 71 | |
| 72 | /** Read from memory-mapped IO. */ |
| 73 | |
| 74 | static inline uint8_t io_read8(io8_t io) |
| 75 | { |
| 76 | return *io.ptr; |
| 77 | } |
| 78 | |
| 79 | static inline uint16_t io_read16(io16_t io) |
| 80 | { |
| 81 | return *io.ptr; |
| 82 | } |
| 83 | |
| 84 | static inline uint32_t io_read32(io32_t io) |
| 85 | { |
| 86 | return *io.ptr; |
| 87 | } |
| 88 | |
| 89 | static inline uint64_t io_read64(io64_t io) |
| 90 | { |
| 91 | return *io.ptr; |
| 92 | } |
| 93 | |
| 94 | static inline uint8_t io_read8_array(io8_array_t io, size_t n) |
| 95 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 96 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 97 | return io.base[n]; |
| 98 | } |
| 99 | |
| 100 | static inline uint16_t io_read16_array(io16_array_t io, size_t n) |
| 101 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 102 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 103 | return io.base[n]; |
| 104 | } |
| 105 | |
| 106 | static inline uint32_t io_read32_array(io32_array_t io, size_t n) |
| 107 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 108 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 109 | return io.base[n]; |
| 110 | } |
| 111 | |
| 112 | static inline uint64_t io_read64_array(io64_array_t io, size_t n) |
| 113 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 114 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 115 | return io.base[n]; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Read from memory-mapped IO with memory barrier. |
| 120 | * |
| 121 | * The read is ordered before subsequent memory accesses. |
| 122 | */ |
| 123 | |
| 124 | static inline uint8_t io_read8_mb(io8_t io) |
| 125 | { |
| 126 | uint8_t v = io_read8(io); |
| 127 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 128 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 129 | return v; |
| 130 | } |
| 131 | |
| 132 | static inline uint16_t io_read16_mb(io16_t io) |
| 133 | { |
| 134 | uint16_t v = io_read16(io); |
| 135 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 136 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 137 | return v; |
| 138 | } |
| 139 | |
| 140 | static inline uint32_t io_read32_mb(io32_t io) |
| 141 | { |
| 142 | uint32_t v = io_read32(io); |
| 143 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 144 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 145 | return v; |
| 146 | } |
| 147 | |
| 148 | static inline uint64_t io_read64_mb(io64_t io) |
| 149 | { |
| 150 | uint64_t v = io_read64(io); |
| 151 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 152 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 153 | return v; |
| 154 | } |
| 155 | |
| 156 | static inline uint8_t io_read8_array_mb(io8_array_t io, size_t n) |
| 157 | { |
| 158 | uint8_t v = io_read8_array(io, n); |
| 159 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 160 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 161 | return v; |
| 162 | } |
| 163 | |
| 164 | static inline uint16_t io_read16_array_mb(io16_array_t io, size_t n) |
| 165 | { |
| 166 | uint16_t v = io_read16_array(io, n); |
| 167 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 168 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 169 | return v; |
| 170 | } |
| 171 | |
| 172 | static inline uint32_t io_read32_array_mb(io32_array_t io, size_t n) |
| 173 | { |
| 174 | uint32_t v = io_read32_array(io, n); |
| 175 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 176 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 177 | return v; |
| 178 | } |
| 179 | |
| 180 | static inline uint64_t io_read64_array_mb(io64_array_t io, size_t n) |
| 181 | { |
| 182 | uint64_t v = io_read64_array(io, n); |
| 183 | |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 184 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 185 | return v; |
| 186 | } |
| 187 | |
| 188 | /* Write to memory-mapped IO. */ |
| 189 | |
| 190 | static inline void io_write8(io8_t io, uint8_t v) |
| 191 | { |
| 192 | *io.ptr = v; |
| 193 | } |
| 194 | |
| 195 | static inline void io_write16(io16_t io, uint16_t v) |
| 196 | { |
| 197 | *io.ptr = v; |
| 198 | } |
| 199 | |
| 200 | static inline void io_write32(io32_t io, uint32_t v) |
| 201 | { |
| 202 | *io.ptr = v; |
| 203 | } |
| 204 | |
| 205 | static inline void io_write64(io64_t io, uint64_t v) |
| 206 | { |
| 207 | *io.ptr = v; |
| 208 | } |
| 209 | |
| 210 | static inline void io_write8_array(io8_array_t io, size_t n, uint8_t v) |
| 211 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 212 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 213 | io.base[n] = v; |
| 214 | } |
| 215 | |
| 216 | static inline void io_write16_array(io16_array_t io, size_t n, uint16_t v) |
| 217 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 218 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 219 | io.base[n] = v; |
| 220 | } |
| 221 | |
| 222 | static inline void io_write32_array(io32_array_t io, size_t n, uint32_t v) |
| 223 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 224 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 225 | io.base[n] = v; |
| 226 | } |
| 227 | |
| 228 | static inline void io_write64_array(io64_array_t io, size_t n, uint64_t v) |
| 229 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 230 | CHECK(n < io.count); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 231 | io.base[n] = v; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Write to memory-mapped IO with memory barrier. |
| 236 | * |
| 237 | * The write is ordered after previous memory accesses. |
| 238 | */ |
| 239 | |
| 240 | static inline void io_write8_mb(io8_t io, uint8_t v) |
| 241 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 242 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 243 | io_write8(io, v); |
| 244 | } |
| 245 | |
| 246 | static inline void io_write16_mb(io16_t io, uint16_t v) |
| 247 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 248 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 249 | io_write16(io, v); |
| 250 | } |
| 251 | |
| 252 | static inline void io_write32_mb(io32_t io, uint32_t v) |
| 253 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 254 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 255 | io_write32(io, v); |
| 256 | } |
| 257 | |
| 258 | static inline void io_write64_mb(io64_t io, uint64_t v) |
| 259 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 260 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 261 | io_write64(io, v); |
| 262 | } |
| 263 | |
| 264 | static inline void io_write8_array_mb(io8_array_t io, size_t n, uint8_t v) |
| 265 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 266 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 267 | io_write8_array(io, n, v); |
| 268 | } |
| 269 | |
| 270 | static inline void io_write16_array_mb(io16_array_t io, size_t n, uint16_t v) |
| 271 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 272 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 273 | io_write16_array(io, n, v); |
| 274 | } |
| 275 | |
| 276 | static inline void io_write32_array_mb(io32_array_t io, size_t n, uint32_t v) |
| 277 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 278 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 279 | io_write32_array(io, n, v); |
| 280 | } |
| 281 | |
| 282 | static inline void io_write64_array_mb(io64_array_t io, size_t n, uint64_t v) |
| 283 | { |
David Brazdil | 851948e | 2019-08-09 12:02:12 +0100 | [diff] [blame] | 284 | data_sync_barrier(); |
Andrew Scull | e22b32f | 2019-05-13 14:46:02 +0100 | [diff] [blame] | 285 | io_write64_array(io, n, v); |
| 286 | } |