blob: 8a67a8788e8fef839629aa8668ee4b581dda3040 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
AlexeiFedorov862f96c2024-03-01 16:26:48 +00006#include <arch_helpers.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00007#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <debug.h>
9#include <granule.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#include <platform_api.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <stddef.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000012#include <utils_def.h>
13
Shanker Donthineniea0213a2025-01-12 09:28:12 -060014IF_NCBMC(static) struct granule granules[RMM_MAX_GRANULES]
15 IF_NCBMC(__section("granules_memory"));
Soby Mathewb4c6df42022-11-09 11:13:29 +000016
17/*
18 * Takes a valid pointer to a struct granule, and returns the granule physical
19 * address.
20 *
21 * This is purely a lookup, and provides no guarantees about the attributes of
22 * the granule (i.e. whether it is locked, its state or its reference count).
23 */
Shruti Gupta9debb132022-12-13 14:38:49 +000024unsigned long granule_addr(const struct granule *g)
Soby Mathewb4c6df42022-11-09 11:13:29 +000025{
26 unsigned long idx;
27
28 assert(g != NULL);
Soby Mathewe02e0bd2023-01-18 10:57:18 +010029 assert(ALIGNED_TO_ARRAY(g, granules));
Soby Mathewb4c6df42022-11-09 11:13:29 +000030
AlexeiFedorov7c8bf6e2023-08-29 17:02:28 +010031 idx = ((unsigned long)g - (unsigned long)granules) /
32 sizeof(struct granule);
Soby Mathewb4c6df42022-11-09 11:13:29 +000033
34 return plat_granule_idx_to_addr(idx);
35}
36
37/*
38 * Takes a granule index, and returns a pointer to the struct granule.
39 *
40 * This is purely a lookup, and provides no guarantees about the attributes of
41 * the granule (i.e. whether it is locked, its state or its reference count).
42 */
43static struct granule *granule_from_idx(unsigned long idx)
44{
45 assert(idx < RMM_MAX_GRANULES);
46 return &granules[idx];
47}
48
49/*
50 * Takes an aligned granule address, and returns a pointer to the corresponding
51 * struct granule.
52 *
53 * This is purely a lookup, and provides no guarantees about the attributes of
54 * the granule (i.e. whether it is locked, its state or its reference count).
55 */
56struct granule *addr_to_granule(unsigned long addr)
57{
58 unsigned long idx;
59
60 assert(GRANULE_ALIGNED(addr));
61
62 idx = plat_granule_addr_to_idx(addr);
63 return granule_from_idx(idx);
64}
65
66/*
67 * Verifies whether @addr is a valid granule physical address, and returns a
68 * pointer to the corresponding struct granule.
69 *
70 * This is purely a lookup, and provides no guarantees w.r.t the state of the
71 * granule (e.g. locking).
72 *
73 * Returns:
74 * Pointer to the struct granule if @addr is a valid granule physical
75 * address.
76 * NULL if any of:
77 * - @addr is not aligned to the size of a granule.
78 * - @addr is out of range.
79 */
80struct granule *find_granule(unsigned long addr)
81{
82 unsigned long idx;
83
84 if (!GRANULE_ALIGNED(addr)) {
85 return NULL;
86 }
87
88 idx = plat_granule_addr_to_idx(addr);
89
90 if (idx >= RMM_MAX_GRANULES) {
91 return NULL;
92 }
93
94 return granule_from_idx(idx);
95}
96
97/*
98 * Obtain a pointer to a locked granule at @addr if @addr is a valid granule
99 * physical address and the state of the granule at @addr is @expected_state.
100 *
101 * Returns:
102 * A valid granule pointer if @addr is a valid granule physical address.
103 * NULL if any of:
104 * - @addr is not aligned to the size of a granule.
105 * - @addr is out of range.
106 * - if the state of the granule at @addr is not
107 * @expected_state.
108 */
109struct granule *find_lock_granule(unsigned long addr,
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000110 unsigned char expected_state)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000111{
AlexeiFedorov745499d2024-04-25 16:52:44 +0100112 struct granule *g = find_granule(addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000113
Soby Mathewb4c6df42022-11-09 11:13:29 +0000114 if (g == NULL) {
115 return NULL;
116 }
117
118 if (!granule_lock_on_state_match(g, expected_state)) {
119 return NULL;
120 }
121
122 return g;
123}
124
125struct granule_set {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000126 unsigned long addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127 struct granule *g;
128 struct granule **g_ret;
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000129 unsigned char state;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000130};
131
132/*
133 * Sort a set of granules by their address.
134 */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100135static void sort_granules(struct granule_set *gs, unsigned long n)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000136{
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100137 for (unsigned long i = 1UL; i < n; i++) {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100138 struct granule_set temp = gs[i];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139 unsigned long j = i;
140
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100141 while ((j > 0UL) && (gs[j - 1UL].addr > temp.addr)) {
142 gs[j] = gs[j - 1UL];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000143 j--;
144 }
145 if (i != j) {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100146 gs[j] = temp;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000147 }
148 }
149}
150
151/*
152 * Find a set of granules and lock them in order of their address.
153 *
154 * @granules: Pointer to array of @n items. Each item must be pre-populated
155 * with ->addr set to the granule's address, and ->state set to
156 * the expected state of the granule, and ->g_ret pointing to
157 * a valid 'struct granule *'.
158 * This function sorts the supplied array in place.
159 * @n: Number of struct granule_set in array pointed to by @granules
160 *
161 * Returns:
162 * True if all granules in @granules were successfully locked.
163 *
164 * False if any two entries in @granules have the same ->addr, or
165 * if, for any entry in @granules, any of the following is true:
166 * - entry->addr is not aligned to the size of a granule
167 * - entry->addr is out of range
168 * - the state of the granule at entry->addr is not entry->state
169 *
170 * Locking only succeeds if the granules are in their expected states as per the
171 * locking rules in granule_types.h.
172 *
173 * If the function succeeds, for all items in @granules, ->g points to a locked
174 * granule in ->state and *->g_ret is set to the pointer value.
175 *
176 * If the function fails, no lock is held and no *->g_ret pointers are
177 * modified.
178 */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100179static bool find_lock_granules(struct granule_set *gs, unsigned long n)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000180{
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100181 unsigned long i;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000182
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100183 sort_granules(gs, n);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000184
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100185 for (i = 0UL; i < n; i++) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000186 /* Check for duplicates */
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100187 if ((i != 0UL) &&
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100188 (gs[i].addr == gs[i - 1UL].addr)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000189 goto out_err;
190 }
191
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100192 gs[i].g = find_lock_granule(gs[i].addr, gs[i].state);
193 if (gs[i].g == NULL) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000194 goto out_err;
195 }
196 }
197
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100198 for (i = 0UL; i < n; i++) {
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100199 *gs[i].g_ret = gs[i].g;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000200 }
201
202 return true;
203
204out_err:
AlexeiFedorove956db32024-04-04 14:36:21 +0100205 while (i != 0UL) {
206 granule_unlock(gs[--i].g);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000207 }
208
209 return false;
210}
211
212/*
213 * Find two granules and lock them in order of their address.
214 *
215 * See find_lock_granules().
216 */
217bool find_lock_two_granules(
218 unsigned long addr1,
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000219 unsigned char expected_state1,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000220 struct granule **g1,
221 unsigned long addr2,
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000222 unsigned char expected_state2,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000223 struct granule **g2)
224{
Shruti Gupta9debb132022-12-13 14:38:49 +0000225 struct granule_set gs[] = {
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000226 {addr1, NULL, g1, expected_state1},
227 {addr2, NULL, g2, expected_state2}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000228 };
229
230 assert((g1 != NULL) && (g2 != NULL));
231
Shruti Gupta9debb132022-12-13 14:38:49 +0000232 return find_lock_granules(gs, ARRAY_SIZE(gs));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000233}
234
Soby Mathewb4c6df42022-11-09 11:13:29 +0000235void granule_memzero_mapped(void *buf)
236{
AlexeiFedorov862f96c2024-03-01 16:26:48 +0000237 unsigned long dczid_el0 = read_dczid_el0();
238 uintptr_t addr = (uintptr_t)buf;
239 unsigned int log2_size;
240 unsigned int block_size;
241 unsigned int cnt;
242
243 /* Check that use of DC ZVA instructions is permitted */
244 assert((dczid_el0 & DCZID_EL0_DZP_BIT) == 0UL);
245
246 /*
247 * Log2 of the block size in words.
248 * The maximum size supported is 2KB, indicated by value 0b1001.
249 */
250 log2_size = (unsigned int)EXTRACT(DCZID_EL0_BS, dczid_el0) + 2U;
251 block_size = U(1) << log2_size;
252
253 /* Number of iterations */
254 cnt = U(1) << (GRANULE_SHIFT - log2_size);
255
256 for (unsigned int i = 0U; i < cnt; i++) {
257 dczva(addr);
258 addr += block_size;
259 }
AlexeiFedorovd9aa2e72024-03-08 11:43:54 +0000260
261 dsb(ish);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000262}
Arunachalam Ganapathy40b3bf02023-06-12 12:19:55 +0100263