1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
/*
* Copyright (C) 2023, STMicroelectronics - All Rights Reserved
*/
#define LOG_CATEGORY UCLASS_NOP
#include <dm.h>
#include <asm/io.h>
#include <dm/device.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <linux/bitfield.h>
#include <mach/rif.h>
/* RIFSC offset register */
#define RIFSC_RISC_SECCFGR0(id) (0x10 + 0x4 * (id))
#define RIFSC_RISC_PER0_CIDCFGR(id) (0x100 + 0x8 * (id))
#define RIFSC_RISC_PER0_SEMCR(id) (0x104 + 0x8 * (id))
/*
* SEMCR register
*/
#define SEMCR_MUTEX BIT(0)
/* RIFSC miscellaneous */
#define RIFSC_RISC_SCID_MASK GENMASK(6, 4)
#define RIFSC_RISC_SEMWL_MASK GENMASK(23, 16)
#define IDS_PER_RISC_SEC_PRIV_REGS 32
/*
* CIDCFGR register fields
*/
#define CIDCFGR_CFEN BIT(0)
#define CIDCFGR_SEMEN BIT(1)
#define SEMWL_SHIFT 16
#define STM32MP25_RIFSC_ENTRIES 178
/* Compartiment IDs */
#define RIF_CID0 0x0
#define RIF_CID1 0x1
/*
* struct stm32_rifsc_plat: Information about RIFSC device
*
* @base: Base address of RIFSC
*/
struct stm32_rifsc_plat {
void *base;
};
/*
* struct stm32_rifsc_child_plat: Information about each child
*
* @domain_id: Domain id
*/
struct stm32_rifsc_child_plat {
u32 domain_id;
};
static bool stm32_rif_is_semaphore_available(void *base, u32 id)
{
void *addr = base + RIFSC_RISC_PER0_SEMCR(id);
return !(readl(addr) & SEMCR_MUTEX);
}
static int stm32_rif_acquire_semaphore(void *base, u32 id)
{
void *addr = base + RIFSC_RISC_PER0_SEMCR(id);
/* Check that the semaphore is available */
if (!stm32_rif_is_semaphore_available(base, id))
return -EACCES;
setbits_le32(addr, SEMCR_MUTEX);
/* Check that CID1 has the semaphore */
if (stm32_rif_is_semaphore_available(base, id) ||
FIELD_GET(RIFSC_RISC_SCID_MASK, (readl(addr)) != RIF_CID1))
return -EACCES;
return 0;
}
static int stm32_rif_release_semaphore(void *base, u32 id)
{
void *addr = base + RIFSC_RISC_PER0_SEMCR(id);
if (stm32_rif_is_semaphore_available(base, id))
return 0;
clrbits_le32(addr, SEMCR_MUTEX);
/* Ok if another compartment takes the semaphore before the check */
if (!stm32_rif_is_semaphore_available(base, id) &&
FIELD_GET(RIFSC_RISC_SCID_MASK, (readl(addr)) == RIF_CID1))
return -EACCES;
return 0;
}
static int rifsc_parse_access_controller(ofnode node, struct ofnode_phandle_args *args)
{
int ret;
ret = ofnode_parse_phandle_with_args(node, "access-controllers",
"#access-controller-cells", 0,
0, args);
if (ret) {
log_debug("failed to parse access-controller (%d)\n", ret);
return ret;
}
if (args->args_count != 1) {
log_debug("invalid domain args_count: %d\n", args->args_count);
return -EINVAL;
}
if (args->args[0] >= STM32MP25_RIFSC_ENTRIES) {
log_err("Invalid sys bus ID for %s\n", ofnode_get_name(node));
return -EINVAL;
}
return 0;
}
static int rifsc_check_access(void *base, u32 id)
{
u32 reg_offset, reg_id, sec_reg_value, cid_reg_value, sem_reg_value;
/*
* RIFSC_RISC_PRIVCFGRx and RIFSC_RISC_SECCFGRx both handle configuration access for
* 32 peripherals. On the other hand, there is one _RIFSC_RISC_PERx_CIDCFGR register
* per peripheral
*/
reg_id = id / IDS_PER_RISC_SEC_PRIV_REGS;
reg_offset = id % IDS_PER_RISC_SEC_PRIV_REGS;
sec_reg_value = readl(base + RIFSC_RISC_SECCFGR0(reg_id));
cid_reg_value = readl(base + RIFSC_RISC_PER0_CIDCFGR(id));
sem_reg_value = readl(base + RIFSC_RISC_PER0_SEMCR(id));
/*
* First check conditions for semaphore mode, which doesn't take into
* account static CID.
*/
if (cid_reg_value & CIDCFGR_SEMEN)
goto skip_cid_check;
/*
* Skip cid check if CID filtering isn't enabled or filtering is enabled on CID0, which
* corresponds to whatever CID.
*/
if (!(cid_reg_value & CIDCFGR_CFEN) ||
FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) == RIF_CID0)
goto skip_cid_check;
/* Coherency check with the CID configuration */
if (FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) != RIF_CID1) {
log_debug("Invalid CID configuration for peripheral %d\n", id);
return -EACCES;
}
/* Check semaphore accesses */
if (cid_reg_value & CIDCFGR_SEMEN) {
if (!(FIELD_GET(RIFSC_RISC_SEMWL_MASK, cid_reg_value) & BIT(RIF_CID1))) {
log_debug("Not in semaphore whitelist for peripheral %d\n", id);
return -EACCES;
}
if (!stm32_rif_is_semaphore_available(base, id) &&
!(FIELD_GET(RIFSC_RISC_SCID_MASK, sem_reg_value) & BIT(RIF_CID1))) {
log_debug("Semaphore unavailable for peripheral %d\n", id);
return -EACCES;
}
}
skip_cid_check:
/* Check security configuration */
if (sec_reg_value & BIT(reg_offset)) {
log_debug("Invalid security configuration for peripheral %d\n", id);
return -EACCES;
}
return 0;
}
int stm32_rifsc_check_access_by_id(ofnode device_node, u32 id)
{
struct ofnode_phandle_args args;
int err;
if (id >= STM32MP25_RIFSC_ENTRIES)
return -EINVAL;
err = rifsc_parse_access_controller(device_node, &args);
if (err)
return err;
return rifsc_check_access((void *)ofnode_get_addr(args.node), id);
}
int stm32_rifsc_check_access(ofnode device_node)
{
struct ofnode_phandle_args args;
int err;
err = rifsc_parse_access_controller(device_node, &args);
if (err)
return err;
return rifsc_check_access((void *)ofnode_get_addr(args.node), args.args[0]);
}
static int stm32_rifsc_child_pre_probe(struct udevice *dev)
{
struct stm32_rifsc_plat *plat = dev_get_plat(dev->parent);
struct stm32_rifsc_child_plat *child_plat = dev_get_parent_plat(dev);
u32 cid_reg_value;
int err;
u32 id = child_plat->domain_id;
cid_reg_value = readl(plat->base + RIFSC_RISC_PER0_CIDCFGR(id));
/*
* If the peripheral is in semaphore mode, take the semaphore so that
* the CID1 has the ownership.
*/
if (cid_reg_value & CIDCFGR_SEMEN &&
(FIELD_GET(RIFSC_RISC_SEMWL_MASK, cid_reg_value) & BIT(RIF_CID1))) {
err = stm32_rif_acquire_semaphore(plat->base, id);
if (err) {
dev_err(dev, "Couldn't acquire RIF semaphore for peripheral %d (%d)\n",
id, err);
return err;
}
dev_dbg(dev, "Acquiring semaphore for peripheral %d\n", id);
}
return 0;
}
static int stm32_rifsc_child_post_remove(struct udevice *dev)
{
struct stm32_rifsc_plat *plat = dev_get_plat(dev->parent);
struct stm32_rifsc_child_plat *child_plat = dev_get_parent_plat(dev);
u32 cid_reg_value;
int err;
u32 id = child_plat->domain_id;
cid_reg_value = readl(plat->base + RIFSC_RISC_PER0_CIDCFGR(id));
/*
* If the peripheral is in semaphore mode, release the semaphore so that
* there's no ownership.
*/
if (cid_reg_value & CIDCFGR_SEMEN &&
(FIELD_GET(RIFSC_RISC_SEMWL_MASK, cid_reg_value) & BIT(RIF_CID1))) {
err = stm32_rif_release_semaphore(plat->base, id);
if (err)
dev_err(dev, "Couldn't release rif semaphore for peripheral %d (%d)\n",
id, err);
}
return 0;
}
static int stm32_rifsc_child_post_bind(struct udevice *dev)
{
struct stm32_rifsc_child_plat *child_plat = dev_get_parent_plat(dev);
struct ofnode_phandle_args args;
int ret;
if (!dev_has_ofnode(dev))
return -EPERM;
ret = rifsc_parse_access_controller(dev_ofnode(dev), &args);
if (ret)
return ret;
child_plat->domain_id = args.args[0];
return 0;
}
static int stm32_rifsc_bind(struct udevice *dev)
{
struct stm32_rifsc_plat *plat = dev_get_plat(dev);
struct ofnode_phandle_args args;
int ret = 0, err = 0;
ofnode node;
plat->base = dev_read_addr_ptr(dev);
if (!plat->base) {
dev_err(dev, "can't get registers base address\n");
return -ENOENT;
}
for (node = ofnode_first_subnode(dev_ofnode(dev));
ofnode_valid(node);
node = ofnode_next_subnode(node)) {
const char *node_name = ofnode_get_name(node);
if (!ofnode_is_enabled(node))
continue;
err = rifsc_parse_access_controller(node, &args);
if (err) {
dev_dbg(dev, "%s failed to parse child on bus (%d)\n", node_name, err);
continue;
}
err = rifsc_check_access(plat->base, args.args[0]);
if (err) {
dev_info(dev, "%s not allowed on bus (%d)\n", node_name, err);
continue;
}
err = lists_bind_fdt(dev, node, NULL, NULL,
gd->flags & GD_FLG_RELOC ? false : true);
if (err && !ret) {
ret = err;
dev_err(dev, "%s failed to bind on bus (%d)\n", node_name, ret);
}
}
if (ret)
dev_err(dev, "Some child failed to bind (%d)\n", ret);
return ret;
}
static int stm32_rifsc_remove(struct udevice *bus)
{
struct udevice *dev;
/* Deactivate all child devices not yet removed */
for (device_find_first_child(bus, &dev); dev; device_find_next_child(&dev))
if (device_active(dev))
stm32_rifsc_child_post_remove(dev);
return 0;
}
static const struct udevice_id stm32_rifsc_ids[] = {
{ .compatible = "st,stm32mp25-rifsc" },
{},
};
U_BOOT_DRIVER(stm32_rifsc) = {
.name = "stm32_rifsc",
.id = UCLASS_NOP,
.of_match = stm32_rifsc_ids,
.bind = stm32_rifsc_bind,
.remove = stm32_rifsc_remove,
.child_post_bind = stm32_rifsc_child_post_bind,
.child_pre_probe = stm32_rifsc_child_pre_probe,
.child_post_remove = stm32_rifsc_child_post_remove,
.plat_auto = sizeof(struct stm32_rifsc_plat),
.per_child_plat_auto = sizeof(struct stm32_rifsc_child_plat),
.flags = DM_FLAG_OS_PREPARE,
};
|