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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Texas Instruments' K3 Secure proxy Driver
*
* Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
* Lokesh Vutla <lokeshvutla@ti.com>
*/
#include <common.h>
#include <asm/io.h>
#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/soc/ti/k3-sec-proxy.h>
#include <dm.h>
#include <mailbox-uclass.h>
DECLARE_GLOBAL_DATA_PTR;
/* SEC PROXY RT THREAD STATUS */
#define RT_THREAD_STATUS 0x0
#define RT_THREAD_THRESHOLD 0x4
#define RT_THREAD_STATUS_ERROR_SHIFT 31
#define RT_THREAD_STATUS_ERROR_MASK BIT(31)
#define RT_THREAD_STATUS_CUR_CNT_SHIFT 0
#define RT_THREAD_STATUS_CUR_CNT_MASK GENMASK(7, 0)
/* SEC PROXY SCFG THREAD CTRL */
#define SCFG_THREAD_CTRL 0x1000
#define SCFG_THREAD_CTRL_DIR_SHIFT 31
#define SCFG_THREAD_CTRL_DIR_MASK BIT(31)
#define SEC_PROXY_THREAD(base, x) ((base) + (0x1000 * (x)))
#define THREAD_IS_RX 1
#define THREAD_IS_TX 0
/**
* struct k3_sec_proxy_desc - Description of secure proxy integration.
* @thread_count: Number of Threads.
* @max_msg_size: Message size in bytes.
* @data_start_offset: Offset of the First data register of the thread
* @data_end_offset: Offset of the Last data register of the thread
* @valid_threads: List of Valid threads that the processor can access
* @num_valid_threads: Number of valid threads.
*/
struct k3_sec_proxy_desc {
u16 thread_count;
u16 max_msg_size;
u16 data_start_offset;
u16 data_end_offset;
const u32 *valid_threads;
u32 num_valid_threads;
};
/**
* struct k3_sec_proxy_thread - Description of a secure proxy Thread
* @id: Thread ID
* @data: Thread Data path region for target
* @scfg: Secure Config Region for Thread
* @rt: RealTime Region for Thread
* @rx_buf: Receive buffer data, max message size.
*/
struct k3_sec_proxy_thread {
u32 id;
void __iomem *data;
void __iomem *scfg;
void __iomem *rt;
u32 *rx_buf;
};
/**
* struct k3_sec_proxy_mbox - Description of a Secure Proxy Instance
* @chan: Mailbox Channel
* @desc: Description of the SoC integration
* @chans: Array for valid thread instances
* @target_data: Secure Proxy region for Target Data
* @scfg: Secure Proxy Region for Secure configuration.
* @rt: Secure proxy Region for Real Time Region.
*/
struct k3_sec_proxy_mbox {
struct mbox_chan chan;
struct k3_sec_proxy_desc *desc;
struct k3_sec_proxy_thread *chans;
phys_addr_t target_data;
phys_addr_t scfg;
phys_addr_t rt;
};
static inline u32 sp_readl(void __iomem *addr, unsigned int offset)
{
return readl(addr + offset);
}
static inline void sp_writel(void __iomem *addr, unsigned int offset, u32 data)
{
writel(data, addr + offset);
}
/**
* k3_sec_proxy_of_xlate() - Translation of phandle to channel
* @chan: Mailbox channel
* @args: Phandle Pointer
*
* Translates the phandle args and fills up the Mailbox channel from client.
* Return: 0 if all goes good, else return corresponding error message.
*/
static int k3_sec_proxy_of_xlate(struct mbox_chan *chan,
struct ofnode_phandle_args *args)
{
struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
int ind, i;
debug("%s(chan=%p)\n", __func__, chan);
if (args->args_count != 1) {
debug("Invaild args_count: %d\n", args->args_count);
return -EINVAL;
}
ind = args->args[0];
for (i = 0; i < spm->desc->num_valid_threads; i++)
if (spm->chans[i].id == ind) {
chan->id = ind;
chan->con_priv = &spm->chans[i];
return 0;
}
dev_err(chan->dev, "%s: Invalid Thread ID %d\n", __func__, ind);
return -ENOENT;
}
/**
* k3_sec_proxy_request() - Request for mailbox channel
* @chan: Channel Pointer
*/
static int k3_sec_proxy_request(struct mbox_chan *chan)
{
debug("%s(chan=%p)\n", __func__, chan);
return 0;
}
/**
* k3_sec_proxy_free() - Free the mailbox channel
* @chan: Channel Pointer
*/
static int k3_sec_proxy_free(struct mbox_chan *chan)
{
debug("%s(chan=%p)\n", __func__, chan);
return 0;
}
/**
* k3_sec_proxy_verify_thread() - Verify thread status before
* sending/receiving data.
* @spt: pointer to secure proxy thread description
* @dir: Direction of the thread
*
* Return: 0 if all goes good, else appropriate error message.
*/
static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt,
u8 dir)
{
/* Check for any errors already available */
if (sp_readl(spt->rt, RT_THREAD_STATUS) &
RT_THREAD_STATUS_ERROR_MASK) {
printf("%s: Thread %d is corrupted, cannot send data.\n",
__func__, spt->id);
return -EINVAL;
}
/* Make sure thread is configured for right direction */
if ((sp_readl(spt->scfg, SCFG_THREAD_CTRL)
& SCFG_THREAD_CTRL_DIR_MASK) >> SCFG_THREAD_CTRL_DIR_SHIFT != dir) {
if (dir)
printf("%s: Trying to receive data on tx Thread %d\n",
__func__, spt->id);
else
printf("%s: Trying to send data on rx Thread %d\n",
__func__, spt->id);
return -EINVAL;
}
/* Check the message queue before sending/receiving data */
if (!(sp_readl(spt->rt, RT_THREAD_STATUS) &
RT_THREAD_STATUS_CUR_CNT_MASK))
return -ENODATA;
return 0;
}
/**
* k3_sec_proxy_send() - Send data via mailbox channel
* @chan: Channel Pointer
* @data: Pointer to k3_sec_proxy_msg
*
* Return: 0 if all goes good, else appropriate error message.
*/
static int k3_sec_proxy_send(struct mbox_chan *chan, const void *data)
{
const struct k3_sec_proxy_msg *msg = (struct k3_sec_proxy_msg *)data;
struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
struct k3_sec_proxy_thread *spt = chan->con_priv;
int num_words, trail_bytes, ret;
void __iomem *data_reg;
u32 *word_data;
debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX);
if (ret) {
dev_err(dev, "%s: Thread%d verification failed. ret = %d\n",
__func__, spt->id, ret);
return ret;
}
/* Check the message size. */
if (msg->len > spm->desc->max_msg_size) {
printf("%s: Thread %ld message length %zu > max msg size %d\n",
__func__, chan->id, msg->len, spm->desc->max_msg_size);
return -EINVAL;
}
/* Send the message */
data_reg = spt->data + spm->desc->data_start_offset;
for (num_words = msg->len / sizeof(u32), word_data = (u32 *)msg->buf;
num_words;
num_words--, data_reg += sizeof(u32), word_data++)
writel(*word_data, data_reg);
trail_bytes = msg->len % sizeof(u32);
if (trail_bytes) {
u32 data_trail = *word_data;
/* Ensure all unused data is 0 */
data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
writel(data_trail, data_reg);
data_reg++;
}
/*
* 'data_reg' indicates next register to write. If we did not already
* write on tx complete reg(last reg), we must do so for transmit
*/
if (data_reg <= (spt->data + spm->desc->data_end_offset))
sp_writel(spt->data, spm->desc->data_end_offset, 0);
debug("%s: Message successfully sent on thread %ld\n",
__func__, chan->id);
return 0;
}
/**
* k3_sec_proxy_recv() - Receive data via mailbox channel
* @chan: Channel Pointer
* @data: Pointer to k3_sec_proxy_msg
*
* Return: 0 if all goes good, else appropriate error message.
*/
static int k3_sec_proxy_recv(struct mbox_chan *chan, void *data)
{
struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
struct k3_sec_proxy_thread *spt = chan->con_priv;
struct k3_sec_proxy_msg *msg = data;
void __iomem *data_reg;
int num_words, ret;
u32 *word_data;
debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_RX);
if (ret)
return ret;
msg->len = spm->desc->max_msg_size;
msg->buf = spt->rx_buf;
data_reg = spt->data + spm->desc->data_start_offset;
word_data = spt->rx_buf;
for (num_words = spm->desc->max_msg_size / sizeof(u32);
num_words;
num_words--, data_reg += sizeof(u32), word_data++)
*word_data = readl(data_reg);
debug("%s: Message successfully received from thread %ld\n",
__func__, chan->id);
return 0;
}
struct mbox_ops k3_sec_proxy_mbox_ops = {
.of_xlate = k3_sec_proxy_of_xlate,
.request = k3_sec_proxy_request,
.free = k3_sec_proxy_free,
.send = k3_sec_proxy_send,
.recv = k3_sec_proxy_recv,
};
/**
* k3_sec_proxy_of_to_priv() - generate private data from device tree
* @dev: corresponding k3 secure proxy device
* @spm: pointer to driver specific private data
*
* Return: 0 if all went ok, else corresponding error message.
*/
static int k3_sec_proxy_of_to_priv(struct udevice *dev,
struct k3_sec_proxy_mbox *spm)
{
const void *blob = gd->fdt_blob;
if (!blob) {
debug("'%s' no dt?\n", dev->name);
return -ENODEV;
}
spm->target_data = devfdt_get_addr_name(dev, "target_data");
if (spm->target_data == FDT_ADDR_T_NONE) {
dev_err(dev, "No reg property for target data base\n");
return -EINVAL;
}
spm->scfg = devfdt_get_addr_name(dev, "scfg");
if (spm->rt == FDT_ADDR_T_NONE) {
dev_err(dev, "No reg property for Secure Cfg base\n");
return -EINVAL;
}
spm->rt = devfdt_get_addr_name(dev, "rt");
if (spm->rt == FDT_ADDR_T_NONE) {
dev_err(dev, "No reg property for Real Time Cfg base\n");
return -EINVAL;
}
return 0;
}
/**
* k3_sec_proxy_thread_setup - Initialize the parameters for all valid threads
* @spm: Mailbox instance for which threads needs to be initialized
*
* Return: 0 if all went ok, else corresponding error message
*/
static int k3_sec_proxy_thread_setup(struct k3_sec_proxy_mbox *spm)
{
struct k3_sec_proxy_thread *spt;
int i, ind;
for (i = 0; i < spm->desc->num_valid_threads; i++) {
spt = &spm->chans[i];
ind = spm->desc->valid_threads[i];
spt->id = ind;
spt->data = (void *)SEC_PROXY_THREAD(spm->target_data, ind);
spt->scfg = (void *)SEC_PROXY_THREAD(spm->scfg, ind);
spt->rt = (void *)SEC_PROXY_THREAD(spm->rt, ind);
spt->rx_buf = calloc(1, spm->desc->max_msg_size);
if (!spt->rx_buf)
return -ENOMEM;
}
return 0;
}
/**
* k3_sec_proxy_probe() - Basic probe
* @dev: corresponding mailbox device
*
* Return: 0 if all went ok, else corresponding error message
*/
static int k3_sec_proxy_probe(struct udevice *dev)
{
struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
int ret;
debug("%s(dev=%p)\n", __func__, dev);
ret = k3_sec_proxy_of_to_priv(dev, spm);
if (ret)
return ret;
spm->desc = (void *)dev_get_driver_data(dev);
spm->chans = calloc(spm->desc->num_valid_threads,
sizeof(struct k3_sec_proxy_thread));
if (!spm->chans)
return -ENOMEM;
ret = k3_sec_proxy_thread_setup(spm);
if (ret) {
debug("%s: secure proxy thread setup failed\n", __func__);
return ret;
}
return 0;
}
static int k3_sec_proxy_remove(struct udevice *dev)
{
struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
debug("%s(dev=%p)\n", __func__, dev);
free(spm->chans);
return 0;
}
/*
* Thread ID #4: ROM request
* Thread ID #5: ROM response, SYSFW notify
* Thread ID #6: SYSFW request response
* Thread ID #7: SYSFW request high priority
* Thread ID #8: SYSFW request low priority
* Thread ID #9: SYSFW notify response
*/
static const u32 am6x_valid_threads[] = { 4, 5, 6, 7, 8, 9, 11, 13 };
static const struct k3_sec_proxy_desc am654_desc = {
.thread_count = 90,
.max_msg_size = 60,
.data_start_offset = 0x4,
.data_end_offset = 0x3C,
.valid_threads = am6x_valid_threads,
.num_valid_threads = ARRAY_SIZE(am6x_valid_threads),
};
static const struct udevice_id k3_sec_proxy_ids[] = {
{ .compatible = "ti,am654-secure-proxy", .data = (ulong)&am654_desc},
{ }
};
U_BOOT_DRIVER(k3_sec_proxy) = {
.name = "k3-secure-proxy",
.id = UCLASS_MAILBOX,
.of_match = k3_sec_proxy_ids,
.probe = k3_sec_proxy_probe,
.remove = k3_sec_proxy_remove,
.priv_auto_alloc_size = sizeof(struct k3_sec_proxy_mbox),
.ops = &k3_sec_proxy_mbox_ops,
};
|