Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <assert.h> |
| 7 | #include <buffer.h> |
| 8 | #include <debug.h> |
| 9 | #include <granule.h> |
| 10 | #include <mmio.h> |
| 11 | #include <platform_api.h> |
| 12 | #include <smc.h> |
| 13 | #include <status.h> |
| 14 | #include <stddef.h> |
Mate Toth-Pal | 2755d69 | 2023-09-28 17:15:58 +0200 | [diff] [blame] | 15 | /* According to the C standard, the memset function used in this file is declared in string.h */ |
| 16 | /* coverity[unnecessary_header: SUPPRESS] */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <utils_def.h> |
| 19 | |
Mate Toth-Pal | 0e93651 | 2023-10-19 15:02:20 +0200 | [diff] [blame] | 20 | IF_NCBMC(static) struct granule granules[RMM_MAX_GRANULES]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Takes a valid pointer to a struct granule, and returns the granule physical |
| 24 | * address. |
| 25 | * |
| 26 | * This is purely a lookup, and provides no guarantees about the attributes of |
| 27 | * the granule (i.e. whether it is locked, its state or its reference count). |
| 28 | */ |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 29 | unsigned long granule_addr(const struct granule *g) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 30 | { |
| 31 | unsigned long idx; |
| 32 | |
| 33 | assert(g != NULL); |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 34 | assert(ALIGNED_TO_ARRAY(g, granules)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 35 | |
AlexeiFedorov | 7c8bf6e | 2023-08-29 17:02:28 +0100 | [diff] [blame] | 36 | idx = ((unsigned long)g - (unsigned long)granules) / |
| 37 | sizeof(struct granule); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 38 | |
| 39 | return plat_granule_idx_to_addr(idx); |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Takes a granule index, and returns a pointer to the struct granule. |
| 44 | * |
| 45 | * This is purely a lookup, and provides no guarantees about the attributes of |
| 46 | * the granule (i.e. whether it is locked, its state or its reference count). |
| 47 | */ |
| 48 | static struct granule *granule_from_idx(unsigned long idx) |
| 49 | { |
| 50 | assert(idx < RMM_MAX_GRANULES); |
| 51 | return &granules[idx]; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Takes an aligned granule address, and returns a pointer to the corresponding |
| 56 | * struct granule. |
| 57 | * |
| 58 | * This is purely a lookup, and provides no guarantees about the attributes of |
| 59 | * the granule (i.e. whether it is locked, its state or its reference count). |
| 60 | */ |
| 61 | struct granule *addr_to_granule(unsigned long addr) |
| 62 | { |
| 63 | unsigned long idx; |
| 64 | |
| 65 | assert(GRANULE_ALIGNED(addr)); |
| 66 | |
| 67 | idx = plat_granule_addr_to_idx(addr); |
| 68 | return granule_from_idx(idx); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Verifies whether @addr is a valid granule physical address, and returns a |
| 73 | * pointer to the corresponding struct granule. |
| 74 | * |
| 75 | * This is purely a lookup, and provides no guarantees w.r.t the state of the |
| 76 | * granule (e.g. locking). |
| 77 | * |
| 78 | * Returns: |
| 79 | * Pointer to the struct granule if @addr is a valid granule physical |
| 80 | * address. |
| 81 | * NULL if any of: |
| 82 | * - @addr is not aligned to the size of a granule. |
| 83 | * - @addr is out of range. |
| 84 | */ |
| 85 | struct granule *find_granule(unsigned long addr) |
| 86 | { |
| 87 | unsigned long idx; |
| 88 | |
| 89 | if (!GRANULE_ALIGNED(addr)) { |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | idx = plat_granule_addr_to_idx(addr); |
| 94 | |
| 95 | if (idx >= RMM_MAX_GRANULES) { |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | return granule_from_idx(idx); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Obtain a pointer to a locked granule at @addr if @addr is a valid granule |
| 104 | * physical address and the state of the granule at @addr is @expected_state. |
| 105 | * |
| 106 | * Returns: |
| 107 | * A valid granule pointer if @addr is a valid granule physical address. |
| 108 | * NULL if any of: |
| 109 | * - @addr is not aligned to the size of a granule. |
| 110 | * - @addr is out of range. |
| 111 | * - if the state of the granule at @addr is not |
| 112 | * @expected_state. |
| 113 | */ |
| 114 | struct granule *find_lock_granule(unsigned long addr, |
AlexeiFedorov | d6d93d8 | 2024-02-13 16:52:11 +0000 | [diff] [blame] | 115 | unsigned char expected_state) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 116 | { |
| 117 | struct granule *g; |
| 118 | |
| 119 | g = find_granule(addr); |
| 120 | if (g == NULL) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | if (!granule_lock_on_state_match(g, expected_state)) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | return g; |
| 129 | } |
| 130 | |
| 131 | struct granule_set { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 132 | unsigned long addr; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 133 | struct granule *g; |
| 134 | struct granule **g_ret; |
AlexeiFedorov | d6d93d8 | 2024-02-13 16:52:11 +0000 | [diff] [blame] | 135 | unsigned char state; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * Sort a set of granules by their address. |
| 140 | */ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 141 | static void sort_granules(struct granule_set *gs, unsigned long n) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 142 | { |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 143 | for (unsigned long i = 1UL; i < n; i++) { |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 144 | struct granule_set temp = gs[i]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 145 | unsigned long j = i; |
| 146 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 147 | while ((j > 0UL) && (gs[j - 1UL].addr > temp.addr)) { |
| 148 | gs[j] = gs[j - 1UL]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 149 | j--; |
| 150 | } |
| 151 | if (i != j) { |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 152 | gs[j] = temp; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Find a set of granules and lock them in order of their address. |
| 159 | * |
| 160 | * @granules: Pointer to array of @n items. Each item must be pre-populated |
| 161 | * with ->addr set to the granule's address, and ->state set to |
| 162 | * the expected state of the granule, and ->g_ret pointing to |
| 163 | * a valid 'struct granule *'. |
| 164 | * This function sorts the supplied array in place. |
| 165 | * @n: Number of struct granule_set in array pointed to by @granules |
| 166 | * |
| 167 | * Returns: |
| 168 | * True if all granules in @granules were successfully locked. |
| 169 | * |
| 170 | * False if any two entries in @granules have the same ->addr, or |
| 171 | * if, for any entry in @granules, any of the following is true: |
| 172 | * - entry->addr is not aligned to the size of a granule |
| 173 | * - entry->addr is out of range |
| 174 | * - the state of the granule at entry->addr is not entry->state |
| 175 | * |
| 176 | * Locking only succeeds if the granules are in their expected states as per the |
| 177 | * locking rules in granule_types.h. |
| 178 | * |
| 179 | * If the function succeeds, for all items in @granules, ->g points to a locked |
| 180 | * granule in ->state and *->g_ret is set to the pointer value. |
| 181 | * |
| 182 | * If the function fails, no lock is held and no *->g_ret pointers are |
| 183 | * modified. |
| 184 | */ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 185 | static bool find_lock_granules(struct granule_set *gs, unsigned long n) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 186 | { |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 187 | unsigned long i; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 188 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 189 | sort_granules(gs, n); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 190 | |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 191 | for (i = 0UL; i < n; i++) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 192 | /* Check for duplicates */ |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 193 | if ((i != 0UL) && |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 194 | (gs[i].addr == gs[i - 1UL].addr)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 195 | goto out_err; |
| 196 | } |
| 197 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 198 | gs[i].g = find_lock_granule(gs[i].addr, gs[i].state); |
| 199 | if (gs[i].g == NULL) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 200 | goto out_err; |
| 201 | } |
| 202 | } |
| 203 | |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 204 | for (i = 0UL; i < n; i++) { |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 205 | *gs[i].g_ret = gs[i].g; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return true; |
| 209 | |
| 210 | out_err: |
AlexeiFedorov | 93f5ec5 | 2023-08-31 14:26:53 +0100 | [diff] [blame] | 211 | while (i-- != 0UL) { |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 212 | granule_unlock(gs[i].g); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Find two granules and lock them in order of their address. |
| 220 | * |
| 221 | * See find_lock_granules(). |
| 222 | */ |
| 223 | bool find_lock_two_granules( |
| 224 | unsigned long addr1, |
AlexeiFedorov | d6d93d8 | 2024-02-13 16:52:11 +0000 | [diff] [blame] | 225 | unsigned char expected_state1, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 226 | struct granule **g1, |
| 227 | unsigned long addr2, |
AlexeiFedorov | d6d93d8 | 2024-02-13 16:52:11 +0000 | [diff] [blame] | 228 | unsigned char expected_state2, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 229 | struct granule **g2) |
| 230 | { |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 231 | struct granule_set gs[] = { |
AlexeiFedorov | d6d93d8 | 2024-02-13 16:52:11 +0000 | [diff] [blame] | 232 | {addr1, NULL, g1, expected_state1}, |
| 233 | {addr2, NULL, g2, expected_state2} |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | assert((g1 != NULL) && (g2 != NULL)); |
| 237 | |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 238 | return find_lock_granules(gs, ARRAY_SIZE(gs)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void granule_memzero(struct granule *g, enum buffer_slot slot) |
| 242 | { |
| 243 | unsigned long *buf; |
| 244 | |
| 245 | assert(g != NULL); |
| 246 | |
| 247 | buf = granule_map(g, slot); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 248 | assert(buf != NULL); |
| 249 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 250 | (void)memset(buf, 0, GRANULE_SIZE); |
| 251 | buffer_unmap(buf); |
| 252 | } |
| 253 | |
| 254 | void granule_memzero_mapped(void *buf) |
| 255 | { |
| 256 | (void)memset(buf, 0, GRANULE_SIZE); |
| 257 | } |
Arunachalam Ganapathy | 40b3bf0 | 2023-06-12 12:19:55 +0100 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * The parent REC granules lock is expected to be acquired before functions |
| 261 | * aux_granules_map() and aux_granules_unmap() are called. |
| 262 | */ |
| 263 | void *aux_granules_map(struct granule *rec_aux_pages[], unsigned int num_aux) |
| 264 | { |
| 265 | void *rec_aux = NULL; |
| 266 | |
| 267 | assert(rec_aux_pages != NULL); |
| 268 | assert(num_aux <= MAX_REC_AUX_GRANULES); |
| 269 | |
| 270 | for (unsigned int i = 0U; i < num_aux; i++) { |
| 271 | void *aux = granule_map(rec_aux_pages[i], |
| 272 | (enum buffer_slot)((unsigned int) |
| 273 | SLOT_REC_AUX0 + i)); |
| 274 | |
| 275 | assert(aux != NULL); |
| 276 | |
| 277 | if (i == 0UL) { |
| 278 | rec_aux = aux; |
| 279 | } |
| 280 | } |
| 281 | return rec_aux; |
| 282 | } |
| 283 | |
| 284 | void aux_granules_unmap(void *rec_aux, unsigned int num_aux) |
| 285 | { |
| 286 | unsigned char *rec_aux_vaddr = (unsigned char *)rec_aux; |
| 287 | |
| 288 | assert(rec_aux != NULL); |
| 289 | assert(num_aux <= MAX_REC_AUX_GRANULES); |
| 290 | |
| 291 | for (unsigned int i = 0U; i < num_aux; i++) { |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 292 | buffer_unmap((void *)((uintptr_t)rec_aux_vaddr + (i * GRANULE_SIZE))); |
Arunachalam Ganapathy | 40b3bf0 | 2023-06-12 12:19:55 +0100 | [diff] [blame] | 293 | } |
| 294 | } |