blob: 27ccc6eab060649de893960978d2d3604925e138 [file] [log] [blame]
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 Secure proxy Driver
4 *
5 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
9#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +053012#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +053014#include <linux/types.h>
15#include <linux/bitops.h>
16#include <linux/soc/ti/k3-sec-proxy.h>
17#include <dm.h>
18#include <mailbox-uclass.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22/* SEC PROXY RT THREAD STATUS */
23#define RT_THREAD_STATUS 0x0
24#define RT_THREAD_THRESHOLD 0x4
25#define RT_THREAD_STATUS_ERROR_SHIFT 31
26#define RT_THREAD_STATUS_ERROR_MASK BIT(31)
27#define RT_THREAD_STATUS_CUR_CNT_SHIFT 0
28#define RT_THREAD_STATUS_CUR_CNT_MASK GENMASK(7, 0)
29
30/* SEC PROXY SCFG THREAD CTRL */
31#define SCFG_THREAD_CTRL 0x1000
32#define SCFG_THREAD_CTRL_DIR_SHIFT 31
33#define SCFG_THREAD_CTRL_DIR_MASK BIT(31)
34
35#define SEC_PROXY_THREAD(base, x) ((base) + (0x1000 * (x)))
36#define THREAD_IS_RX 1
37#define THREAD_IS_TX 0
38
39/**
40 * struct k3_sec_proxy_desc - Description of secure proxy integration.
41 * @thread_count: Number of Threads.
42 * @max_msg_size: Message size in bytes.
43 * @data_start_offset: Offset of the First data register of the thread
44 * @data_end_offset: Offset of the Last data register of the thread
45 * @valid_threads: List of Valid threads that the processor can access
46 * @num_valid_threads: Number of valid threads.
47 */
48struct k3_sec_proxy_desc {
49 u16 thread_count;
50 u16 max_msg_size;
51 u16 data_start_offset;
52 u16 data_end_offset;
53 const u32 *valid_threads;
54 u32 num_valid_threads;
55};
56
57/**
58 * struct k3_sec_proxy_thread - Description of a secure proxy Thread
59 * @id: Thread ID
60 * @data: Thread Data path region for target
61 * @scfg: Secure Config Region for Thread
62 * @rt: RealTime Region for Thread
63 * @rx_buf: Receive buffer data, max message size.
64 */
65struct k3_sec_proxy_thread {
66 u32 id;
67 void __iomem *data;
68 void __iomem *scfg;
69 void __iomem *rt;
70 u32 *rx_buf;
71};
72
73/**
74 * struct k3_sec_proxy_mbox - Description of a Secure Proxy Instance
75 * @chan: Mailbox Channel
76 * @desc: Description of the SoC integration
77 * @chans: Array for valid thread instances
78 * @target_data: Secure Proxy region for Target Data
79 * @scfg: Secure Proxy Region for Secure configuration.
80 * @rt: Secure proxy Region for Real Time Region.
81 */
82struct k3_sec_proxy_mbox {
83 struct mbox_chan chan;
84 struct k3_sec_proxy_desc *desc;
85 struct k3_sec_proxy_thread *chans;
86 phys_addr_t target_data;
87 phys_addr_t scfg;
88 phys_addr_t rt;
89};
90
91static inline u32 sp_readl(void __iomem *addr, unsigned int offset)
92{
93 return readl(addr + offset);
94}
95
96static inline void sp_writel(void __iomem *addr, unsigned int offset, u32 data)
97{
98 writel(data, addr + offset);
99}
100
101/**
102 * k3_sec_proxy_of_xlate() - Translation of phandle to channel
103 * @chan: Mailbox channel
104 * @args: Phandle Pointer
105 *
106 * Translates the phandle args and fills up the Mailbox channel from client.
107 * Return: 0 if all goes good, else return corresponding error message.
108 */
109static int k3_sec_proxy_of_xlate(struct mbox_chan *chan,
110 struct ofnode_phandle_args *args)
111{
112 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
113 int ind, i;
114
115 debug("%s(chan=%p)\n", __func__, chan);
116
117 if (args->args_count != 1) {
118 debug("Invaild args_count: %d\n", args->args_count);
119 return -EINVAL;
120 }
121 ind = args->args[0];
122
123 for (i = 0; i < spm->desc->num_valid_threads; i++)
124 if (spm->chans[i].id == ind) {
125 chan->id = ind;
126 chan->con_priv = &spm->chans[i];
127 return 0;
128 }
129
130 dev_err(chan->dev, "%s: Invalid Thread ID %d\n", __func__, ind);
131 return -ENOENT;
132}
133
134/**
135 * k3_sec_proxy_request() - Request for mailbox channel
136 * @chan: Channel Pointer
137 */
138static int k3_sec_proxy_request(struct mbox_chan *chan)
139{
140 debug("%s(chan=%p)\n", __func__, chan);
141
142 return 0;
143}
144
145/**
146 * k3_sec_proxy_free() - Free the mailbox channel
147 * @chan: Channel Pointer
148 */
149static int k3_sec_proxy_free(struct mbox_chan *chan)
150{
151 debug("%s(chan=%p)\n", __func__, chan);
152
153 return 0;
154}
155
156/**
157 * k3_sec_proxy_verify_thread() - Verify thread status before
158 * sending/receiving data.
159 * @spt: pointer to secure proxy thread description
160 * @dir: Direction of the thread
161 *
162 * Return: 0 if all goes good, else appropriate error message.
163 */
164static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt,
165 u8 dir)
166{
167 /* Check for any errors already available */
168 if (sp_readl(spt->rt, RT_THREAD_STATUS) &
169 RT_THREAD_STATUS_ERROR_MASK) {
170 printf("%s: Thread %d is corrupted, cannot send data.\n",
171 __func__, spt->id);
172 return -EINVAL;
173 }
174
175 /* Make sure thread is configured for right direction */
176 if ((sp_readl(spt->scfg, SCFG_THREAD_CTRL)
177 & SCFG_THREAD_CTRL_DIR_MASK) >> SCFG_THREAD_CTRL_DIR_SHIFT != dir) {
178 if (dir)
179 printf("%s: Trying to receive data on tx Thread %d\n",
180 __func__, spt->id);
181 else
182 printf("%s: Trying to send data on rx Thread %d\n",
183 __func__, spt->id);
184 return -EINVAL;
185 }
186
187 /* Check the message queue before sending/receiving data */
188 if (!(sp_readl(spt->rt, RT_THREAD_STATUS) &
189 RT_THREAD_STATUS_CUR_CNT_MASK))
190 return -ENODATA;
191
192 return 0;
193}
194
195/**
196 * k3_sec_proxy_send() - Send data via mailbox channel
197 * @chan: Channel Pointer
198 * @data: Pointer to k3_sec_proxy_msg
199 *
200 * Return: 0 if all goes good, else appropriate error message.
201 */
202static int k3_sec_proxy_send(struct mbox_chan *chan, const void *data)
203{
204 const struct k3_sec_proxy_msg *msg = (struct k3_sec_proxy_msg *)data;
205 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
206 struct k3_sec_proxy_thread *spt = chan->con_priv;
207 int num_words, trail_bytes, ret;
208 void __iomem *data_reg;
209 u32 *word_data;
210
211 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
212
213 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX);
214 if (ret) {
Sean Anderson96da63d2020-09-15 10:44:48 -0400215 dev_err(chan->dev,
216 "%s: Thread%d verification failed. ret = %d\n",
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530217 __func__, spt->id, ret);
218 return ret;
219 }
220
221 /* Check the message size. */
222 if (msg->len > spm->desc->max_msg_size) {
Sean Anderson96da63d2020-09-15 10:44:48 -0400223 dev_err(chan->dev,
224 "%s: Thread %ld message length %zu > max msg size %d\n",
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530225 __func__, chan->id, msg->len, spm->desc->max_msg_size);
226 return -EINVAL;
227 }
228
229 /* Send the message */
230 data_reg = spt->data + spm->desc->data_start_offset;
231 for (num_words = msg->len / sizeof(u32), word_data = (u32 *)msg->buf;
232 num_words;
233 num_words--, data_reg += sizeof(u32), word_data++)
234 writel(*word_data, data_reg);
235
236 trail_bytes = msg->len % sizeof(u32);
237 if (trail_bytes) {
238 u32 data_trail = *word_data;
239
240 /* Ensure all unused data is 0 */
241 data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
242 writel(data_trail, data_reg);
243 data_reg++;
244 }
245
246 /*
247 * 'data_reg' indicates next register to write. If we did not already
248 * write on tx complete reg(last reg), we must do so for transmit
249 */
250 if (data_reg <= (spt->data + spm->desc->data_end_offset))
251 sp_writel(spt->data, spm->desc->data_end_offset, 0);
252
253 debug("%s: Message successfully sent on thread %ld\n",
254 __func__, chan->id);
255
256 return 0;
257}
258
259/**
260 * k3_sec_proxy_recv() - Receive data via mailbox channel
261 * @chan: Channel Pointer
262 * @data: Pointer to k3_sec_proxy_msg
263 *
264 * Return: 0 if all goes good, else appropriate error message.
265 */
266static int k3_sec_proxy_recv(struct mbox_chan *chan, void *data)
267{
268 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
269 struct k3_sec_proxy_thread *spt = chan->con_priv;
270 struct k3_sec_proxy_msg *msg = data;
271 void __iomem *data_reg;
272 int num_words, ret;
273 u32 *word_data;
274
275 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
276
277 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_RX);
278 if (ret)
279 return ret;
280
281 msg->len = spm->desc->max_msg_size;
282 msg->buf = spt->rx_buf;
283 data_reg = spt->data + spm->desc->data_start_offset;
284 word_data = spt->rx_buf;
285 for (num_words = spm->desc->max_msg_size / sizeof(u32);
286 num_words;
287 num_words--, data_reg += sizeof(u32), word_data++)
288 *word_data = readl(data_reg);
289
290 debug("%s: Message successfully received from thread %ld\n",
291 __func__, chan->id);
292
293 return 0;
294}
295
296struct mbox_ops k3_sec_proxy_mbox_ops = {
297 .of_xlate = k3_sec_proxy_of_xlate,
298 .request = k3_sec_proxy_request,
Simon Glass1ee48192020-02-03 07:35:50 -0700299 .rfree = k3_sec_proxy_free,
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530300 .send = k3_sec_proxy_send,
301 .recv = k3_sec_proxy_recv,
302};
303
304/**
305 * k3_sec_proxy_of_to_priv() - generate private data from device tree
306 * @dev: corresponding k3 secure proxy device
307 * @spm: pointer to driver specific private data
308 *
309 * Return: 0 if all went ok, else corresponding error message.
310 */
311static int k3_sec_proxy_of_to_priv(struct udevice *dev,
312 struct k3_sec_proxy_mbox *spm)
313{
314 const void *blob = gd->fdt_blob;
315
316 if (!blob) {
317 debug("'%s' no dt?\n", dev->name);
318 return -ENODEV;
319 }
320
321 spm->target_data = devfdt_get_addr_name(dev, "target_data");
322 if (spm->target_data == FDT_ADDR_T_NONE) {
323 dev_err(dev, "No reg property for target data base\n");
324 return -EINVAL;
325 }
326
327 spm->scfg = devfdt_get_addr_name(dev, "scfg");
328 if (spm->rt == FDT_ADDR_T_NONE) {
329 dev_err(dev, "No reg property for Secure Cfg base\n");
330 return -EINVAL;
331 }
332
333 spm->rt = devfdt_get_addr_name(dev, "rt");
334 if (spm->rt == FDT_ADDR_T_NONE) {
335 dev_err(dev, "No reg property for Real Time Cfg base\n");
336 return -EINVAL;
337 }
338
339 return 0;
340}
341
342/**
343 * k3_sec_proxy_thread_setup - Initialize the parameters for all valid threads
344 * @spm: Mailbox instance for which threads needs to be initialized
345 *
346 * Return: 0 if all went ok, else corresponding error message
347 */
348static int k3_sec_proxy_thread_setup(struct k3_sec_proxy_mbox *spm)
349{
350 struct k3_sec_proxy_thread *spt;
351 int i, ind;
352
353 for (i = 0; i < spm->desc->num_valid_threads; i++) {
354 spt = &spm->chans[i];
355 ind = spm->desc->valid_threads[i];
356 spt->id = ind;
357 spt->data = (void *)SEC_PROXY_THREAD(spm->target_data, ind);
358 spt->scfg = (void *)SEC_PROXY_THREAD(spm->scfg, ind);
359 spt->rt = (void *)SEC_PROXY_THREAD(spm->rt, ind);
360 spt->rx_buf = calloc(1, spm->desc->max_msg_size);
361 if (!spt->rx_buf)
362 return -ENOMEM;
363 }
364
365 return 0;
366}
367
368/**
369 * k3_sec_proxy_probe() - Basic probe
370 * @dev: corresponding mailbox device
371 *
372 * Return: 0 if all went ok, else corresponding error message
373 */
374static int k3_sec_proxy_probe(struct udevice *dev)
375{
376 struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
377 int ret;
378
379 debug("%s(dev=%p)\n", __func__, dev);
380
381 ret = k3_sec_proxy_of_to_priv(dev, spm);
382 if (ret)
383 return ret;
384
385 spm->desc = (void *)dev_get_driver_data(dev);
386 spm->chans = calloc(spm->desc->num_valid_threads,
387 sizeof(struct k3_sec_proxy_thread));
388 if (!spm->chans)
389 return -ENOMEM;
390
391 ret = k3_sec_proxy_thread_setup(spm);
392 if (ret) {
393 debug("%s: secure proxy thread setup failed\n", __func__);
394 return ret;
395 }
396
397 return 0;
398}
399
400static int k3_sec_proxy_remove(struct udevice *dev)
401{
402 struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
403
404 debug("%s(dev=%p)\n", __func__, dev);
405
406 free(spm->chans);
407
408 return 0;
409}
410
411/*
412 * Thread ID #4: ROM request
413 * Thread ID #5: ROM response, SYSFW notify
414 * Thread ID #6: SYSFW request response
415 * Thread ID #7: SYSFW request high priority
416 * Thread ID #8: SYSFW request low priority
417 * Thread ID #9: SYSFW notify response
418 */
419static const u32 am6x_valid_threads[] = { 4, 5, 6, 7, 8, 9, 11, 13 };
420
421static const struct k3_sec_proxy_desc am654_desc = {
422 .thread_count = 90,
423 .max_msg_size = 60,
424 .data_start_offset = 0x4,
425 .data_end_offset = 0x3C,
426 .valid_threads = am6x_valid_threads,
427 .num_valid_threads = ARRAY_SIZE(am6x_valid_threads),
428};
429
430static const struct udevice_id k3_sec_proxy_ids[] = {
431 { .compatible = "ti,am654-secure-proxy", .data = (ulong)&am654_desc},
432 { }
433};
434
435U_BOOT_DRIVER(k3_sec_proxy) = {
436 .name = "k3-secure-proxy",
437 .id = UCLASS_MAILBOX,
438 .of_match = k3_sec_proxy_ids,
439 .probe = k3_sec_proxy_probe,
440 .remove = k3_sec_proxy_remove,
441 .priv_auto_alloc_size = sizeof(struct k3_sec_proxy_mbox),
442 .ops = &k3_sec_proxy_mbox_ops,
443};