blob: 6b7093d9dea78b60075a3f8b9a5dfc1d04766969 [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);
Javier Almansa Sobrinoaecad122023-01-16 13:05:59 +000032 assert(ALIGNED((void *)g, sizeof(struct granule)));
Javier Almansa Sobrino68a593a2022-07-25 09:35:32 +010033 assert(g >= &granules[0]);
Soby Mathewb4c6df42022-11-09 11:13:29 +000034
35 idx = g - &granules[0];
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 */
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 {
130 unsigned int idx;
131 unsigned long addr;
132 enum granule_state state;
133 struct granule *g;
134 struct granule **g_ret;
135};
136
137/*
138 * Sort a set of granules by their address.
139 */
140static void sort_granules(struct granule_set *granules,
141 unsigned long n)
142{
143 unsigned long i;
144
145 for (i = 1UL; i < n; i++) {
146 struct granule_set temp = granules[i];
147 unsigned long j = i;
148
149 while ((j > 0UL) && (granules[j - 1].addr > temp.addr)) {
150 granules[j] = granules[j - 1];
151 j--;
152 }
153 if (i != j) {
154 granules[j] = temp;
155 }
156 }
157}
158
159/*
160 * Find a set of granules and lock them in order of their address.
161 *
162 * @granules: Pointer to array of @n items. Each item must be pre-populated
163 * with ->addr set to the granule's address, and ->state set to
164 * the expected state of the granule, and ->g_ret pointing to
165 * a valid 'struct granule *'.
166 * This function sorts the supplied array in place.
167 * @n: Number of struct granule_set in array pointed to by @granules
168 *
169 * Returns:
170 * True if all granules in @granules were successfully locked.
171 *
172 * False if any two entries in @granules have the same ->addr, or
173 * if, for any entry in @granules, any of the following is true:
174 * - entry->addr is not aligned to the size of a granule
175 * - entry->addr is out of range
176 * - the state of the granule at entry->addr is not entry->state
177 *
178 * Locking only succeeds if the granules are in their expected states as per the
179 * locking rules in granule_types.h.
180 *
181 * If the function succeeds, for all items in @granules, ->g points to a locked
182 * granule in ->state and *->g_ret is set to the pointer value.
183 *
184 * If the function fails, no lock is held and no *->g_ret pointers are
185 * modified.
186 */
187static bool find_lock_granules(struct granule_set *granules,
188 unsigned long n)
189{
190 long i;
191
192 for (i = 0L; i < n; i++) {
193 granules[i].idx = i;
194 }
195
196 sort_granules(granules, n);
197
198 for (i = 0L; i < n; i++) {
199 /* Check for duplicates */
200 if ((i > 0L) && (granules[i].addr == granules[i - 1].addr)) {
201 goto out_err;
202 }
203
204 granules[i].g = find_lock_granule(granules[i].addr,
205 granules[i].state);
206 if (granules[i].g == NULL) {
207 goto out_err;
208 }
209 }
210
211 for (i = 0L; i < n; i++) {
212 *granules[i].g_ret = granules[i].g;
213 }
214
215 return true;
216
217out_err:
218 for (i = i - 1; i >= 0L; i--) {
219 granule_unlock(granules[i].g);
220 }
221
222 return false;
223}
224
225/*
226 * Find two granules and lock them in order of their address.
227 *
228 * See find_lock_granules().
229 */
230bool find_lock_two_granules(
231 unsigned long addr1,
232 enum granule_state expected_state1,
233 struct granule **g1,
234 unsigned long addr2,
235 enum granule_state expected_state2,
236 struct granule **g2)
237{
Shruti Gupta9debb132022-12-13 14:38:49 +0000238 struct granule_set gs[] = {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000239 {0U, addr1, expected_state1, NULL, g1},
240 {1U, addr2, expected_state2, NULL, g2}
241 };
242
243 assert((g1 != NULL) && (g2 != NULL));
244
Shruti Gupta9debb132022-12-13 14:38:49 +0000245 return find_lock_granules(gs, ARRAY_SIZE(gs));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000246}
247
248void granule_memzero(struct granule *g, enum buffer_slot slot)
249{
250 unsigned long *buf;
251
252 assert(g != NULL);
253
254 buf = granule_map(g, slot);
255 (void)memset(buf, 0, GRANULE_SIZE);
256 buffer_unmap(buf);
257}
258
259void granule_memzero_mapped(void *buf)
260{
261 (void)memset(buf, 0, GRANULE_SIZE);
262}