blob: 5eafe46fd4d073d2ce155856a65b2894b66a72de [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 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +05306 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.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;
Matthias Schiffer47331932023-09-27 15:33:34 +020086 void *target_data;
87 void *scfg;
88 void *rt;
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +053089};
90
91static inline u32 sp_readl(void __iomem *addr, unsigned int offset)
92{
93 return readl(addr + offset);
94}
95
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +053096/**
97 * k3_sec_proxy_of_xlate() - Translation of phandle to channel
98 * @chan: Mailbox channel
99 * @args: Phandle Pointer
100 *
101 * Translates the phandle args and fills up the Mailbox channel from client.
102 * Return: 0 if all goes good, else return corresponding error message.
103 */
104static int k3_sec_proxy_of_xlate(struct mbox_chan *chan,
105 struct ofnode_phandle_args *args)
106{
107 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
108 int ind, i;
109
110 debug("%s(chan=%p)\n", __func__, chan);
111
112 if (args->args_count != 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500113 debug("Invalid args_count: %d\n", args->args_count);
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530114 return -EINVAL;
115 }
116 ind = args->args[0];
117
118 for (i = 0; i < spm->desc->num_valid_threads; i++)
119 if (spm->chans[i].id == ind) {
120 chan->id = ind;
121 chan->con_priv = &spm->chans[i];
122 return 0;
123 }
124
125 dev_err(chan->dev, "%s: Invalid Thread ID %d\n", __func__, ind);
126 return -ENOENT;
127}
128
129/**
130 * k3_sec_proxy_request() - Request for mailbox channel
131 * @chan: Channel Pointer
132 */
133static int k3_sec_proxy_request(struct mbox_chan *chan)
134{
135 debug("%s(chan=%p)\n", __func__, chan);
136
137 return 0;
138}
139
140/**
141 * k3_sec_proxy_free() - Free the mailbox channel
142 * @chan: Channel Pointer
143 */
144static int k3_sec_proxy_free(struct mbox_chan *chan)
145{
146 debug("%s(chan=%p)\n", __func__, chan);
147
148 return 0;
149}
150
151/**
152 * k3_sec_proxy_verify_thread() - Verify thread status before
153 * sending/receiving data.
154 * @spt: pointer to secure proxy thread description
155 * @dir: Direction of the thread
156 *
157 * Return: 0 if all goes good, else appropriate error message.
158 */
159static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt,
160 u8 dir)
161{
162 /* Check for any errors already available */
163 if (sp_readl(spt->rt, RT_THREAD_STATUS) &
164 RT_THREAD_STATUS_ERROR_MASK) {
165 printf("%s: Thread %d is corrupted, cannot send data.\n",
166 __func__, spt->id);
167 return -EINVAL;
168 }
169
170 /* Make sure thread is configured for right direction */
171 if ((sp_readl(spt->scfg, SCFG_THREAD_CTRL)
172 & SCFG_THREAD_CTRL_DIR_MASK) >> SCFG_THREAD_CTRL_DIR_SHIFT != dir) {
173 if (dir)
174 printf("%s: Trying to receive data on tx Thread %d\n",
175 __func__, spt->id);
176 else
177 printf("%s: Trying to send data on rx Thread %d\n",
178 __func__, spt->id);
179 return -EINVAL;
180 }
181
182 /* Check the message queue before sending/receiving data */
183 if (!(sp_readl(spt->rt, RT_THREAD_STATUS) &
184 RT_THREAD_STATUS_CUR_CNT_MASK))
185 return -ENODATA;
186
187 return 0;
188}
189
190/**
191 * k3_sec_proxy_send() - Send data via mailbox channel
192 * @chan: Channel Pointer
193 * @data: Pointer to k3_sec_proxy_msg
194 *
195 * Return: 0 if all goes good, else appropriate error message.
196 */
197static int k3_sec_proxy_send(struct mbox_chan *chan, const void *data)
198{
199 const struct k3_sec_proxy_msg *msg = (struct k3_sec_proxy_msg *)data;
200 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
201 struct k3_sec_proxy_thread *spt = chan->con_priv;
202 int num_words, trail_bytes, ret;
203 void __iomem *data_reg;
204 u32 *word_data;
205
206 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
207
208 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX);
209 if (ret) {
Sean Anderson96da63d2020-09-15 10:44:48 -0400210 dev_err(chan->dev,
211 "%s: Thread%d verification failed. ret = %d\n",
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530212 __func__, spt->id, ret);
213 return ret;
214 }
215
216 /* Check the message size. */
217 if (msg->len > spm->desc->max_msg_size) {
Sean Anderson96da63d2020-09-15 10:44:48 -0400218 dev_err(chan->dev,
219 "%s: Thread %ld message length %zu > max msg size %d\n",
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530220 __func__, chan->id, msg->len, spm->desc->max_msg_size);
221 return -EINVAL;
222 }
223
224 /* Send the message */
225 data_reg = spt->data + spm->desc->data_start_offset;
226 for (num_words = msg->len / sizeof(u32), word_data = (u32 *)msg->buf;
227 num_words;
228 num_words--, data_reg += sizeof(u32), word_data++)
229 writel(*word_data, data_reg);
230
231 trail_bytes = msg->len % sizeof(u32);
232 if (trail_bytes) {
233 u32 data_trail = *word_data;
234
235 /* Ensure all unused data is 0 */
236 data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
237 writel(data_trail, data_reg);
Nishanth Menonce399f82023-06-20 13:17:09 -0500238 data_reg += sizeof(u32);
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530239 }
240
241 /*
242 * 'data_reg' indicates next register to write. If we did not already
243 * write on tx complete reg(last reg), we must do so for transmit
Nishanth Menonce399f82023-06-20 13:17:09 -0500244 * In addition, we also need to make sure all intermediate data
245 * registers(if any required), are reset to 0 for TISCI backward
246 * compatibility to be maintained.
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530247 */
Nishanth Menonce399f82023-06-20 13:17:09 -0500248 while (data_reg <= (spt->data + spm->desc->data_end_offset)) {
249 writel(0x0, data_reg);
250 data_reg += sizeof(u32);
251 }
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530252
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
Matthias Schiffer47331932023-09-27 15:33:34 +0200321 spm->target_data = dev_read_addr_name_ptr(dev, "target_data");
322 if (!spm->target_data) {
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530323 dev_err(dev, "No reg property for target data base\n");
324 return -EINVAL;
325 }
326
Matthias Schiffer47331932023-09-27 15:33:34 +0200327 spm->scfg = dev_read_addr_name_ptr(dev, "scfg");
328 if (!spm->scfg) {
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530329 dev_err(dev, "No reg property for Secure Cfg base\n");
330 return -EINVAL;
331 }
332
Matthias Schiffer47331932023-09-27 15:33:34 +0200333 spm->rt = dev_read_addr_name_ptr(dev, "rt");
334 if (!spm->rt) {
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530335 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
Vignesh Raghavendra44ac36b2021-06-07 19:47:47 +0530411static const u32 am6x_valid_threads[] = { 0, 1, 4, 5, 6, 7, 8, 9, 11, 12, 13, 20, 21, 22, 23 };
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530412
413static const struct k3_sec_proxy_desc am654_desc = {
414 .thread_count = 90,
415 .max_msg_size = 60,
416 .data_start_offset = 0x4,
417 .data_end_offset = 0x3C,
418 .valid_threads = am6x_valid_threads,
419 .num_valid_threads = ARRAY_SIZE(am6x_valid_threads),
420};
421
422static const struct udevice_id k3_sec_proxy_ids[] = {
423 { .compatible = "ti,am654-secure-proxy", .data = (ulong)&am654_desc},
424 { }
425};
426
427U_BOOT_DRIVER(k3_sec_proxy) = {
428 .name = "k3-secure-proxy",
429 .id = UCLASS_MAILBOX,
430 .of_match = k3_sec_proxy_ids,
431 .probe = k3_sec_proxy_probe,
432 .remove = k3_sec_proxy_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700433 .priv_auto = sizeof(struct k3_sec_proxy_mbox),
Lokesh Vutlabb6d08d2018-08-27 15:57:48 +0530434 .ops = &k3_sec_proxy_mbox_ops,
435};