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