blob: c3a99fa9477d589900b02fbfaf3be31349a03904 [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
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
18static 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 Gupta9debb132022-12-13 14:38:49 +000027unsigned long granule_addr(const struct granule *g)
Soby Mathewb4c6df42022-11-09 11:13:29 +000028{
29 unsigned long idx;
30
31 assert(g != NULL);
Soby Mathewe02e0bd2023-01-18 10:57:18 +010032 assert(ALIGNED_TO_ARRAY(g, granules));
Soby Mathewb4c6df42022-11-09 11:13:29 +000033
AlexeiFedorov7c8bf6e2023-08-29 17:02:28 +010034 idx = ((unsigned long)g - (unsigned long)granules) /
35 sizeof(struct granule);
Soby Mathewb4c6df42022-11-09 11:13:29 +000036
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 */
46static 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 */
59struct 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 */
83struct 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 */
112struct 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
129struct granule_set {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000130 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 */
139static void sort_granules(struct granule_set *granules,
140 unsigned long n)
141{
142 unsigned long i;
143
144 for (i = 1UL; i < n; i++) {
145 struct granule_set temp = granules[i];
146 unsigned long j = i;
147
148 while ((j > 0UL) && (granules[j - 1].addr > temp.addr)) {
149 granules[j] = granules[j - 1];
150 j--;
151 }
152 if (i != j) {
153 granules[j] = temp;
154 }
155 }
156}
157
158/*
159 * Find a set of granules and lock them in order of their address.
160 *
161 * @granules: Pointer to array of @n items. Each item must be pre-populated
162 * with ->addr set to the granule's address, and ->state set to
163 * the expected state of the granule, and ->g_ret pointing to
164 * a valid 'struct granule *'.
165 * This function sorts the supplied array in place.
166 * @n: Number of struct granule_set in array pointed to by @granules
167 *
168 * Returns:
169 * True if all granules in @granules were successfully locked.
170 *
171 * False if any two entries in @granules have the same ->addr, or
172 * if, for any entry in @granules, any of the following is true:
173 * - entry->addr is not aligned to the size of a granule
174 * - entry->addr is out of range
175 * - the state of the granule at entry->addr is not entry->state
176 *
177 * Locking only succeeds if the granules are in their expected states as per the
178 * locking rules in granule_types.h.
179 *
180 * If the function succeeds, for all items in @granules, ->g points to a locked
181 * granule in ->state and *->g_ret is set to the pointer value.
182 *
183 * If the function fails, no lock is held and no *->g_ret pointers are
184 * modified.
185 */
186static bool find_lock_granules(struct granule_set *granules,
187 unsigned long n)
188{
189 long i;
190
Soby Mathewb4c6df42022-11-09 11:13:29 +0000191 sort_granules(granules, n);
192
193 for (i = 0L; i < n; i++) {
194 /* Check for duplicates */
195 if ((i > 0L) && (granules[i].addr == granules[i - 1].addr)) {
196 goto out_err;
197 }
198
199 granules[i].g = find_lock_granule(granules[i].addr,
200 granules[i].state);
201 if (granules[i].g == NULL) {
202 goto out_err;
203 }
204 }
205
206 for (i = 0L; i < n; i++) {
207 *granules[i].g_ret = granules[i].g;
208 }
209
210 return true;
211
212out_err:
213 for (i = i - 1; i >= 0L; i--) {
214 granule_unlock(granules[i].g);
215 }
216
217 return false;
218}
219
220/*
221 * Find two granules and lock them in order of their address.
222 *
223 * See find_lock_granules().
224 */
225bool find_lock_two_granules(
226 unsigned long addr1,
227 enum granule_state expected_state1,
228 struct granule **g1,
229 unsigned long addr2,
230 enum granule_state expected_state2,
231 struct granule **g2)
232{
Shruti Gupta9debb132022-12-13 14:38:49 +0000233 struct granule_set gs[] = {
shaxio0173f21122023-07-21 16:30:31 +0100234 {addr1, expected_state1, NULL, g1},
235 {addr2, expected_state2, NULL, g2}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000236 };
237
238 assert((g1 != NULL) && (g2 != NULL));
239
Shruti Gupta9debb132022-12-13 14:38:49 +0000240 return find_lock_granules(gs, ARRAY_SIZE(gs));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000241}
242
243void granule_memzero(struct granule *g, enum buffer_slot slot)
244{
245 unsigned long *buf;
246
247 assert(g != NULL);
248
249 buf = granule_map(g, slot);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100250 assert(buf != NULL);
251
Soby Mathewb4c6df42022-11-09 11:13:29 +0000252 (void)memset(buf, 0, GRANULE_SIZE);
253 buffer_unmap(buf);
254}
255
256void granule_memzero_mapped(void *buf)
257{
258 (void)memset(buf, 0, GRANULE_SIZE);
259}