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