1 // 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>
10 #include <log.h>
11 #include <malloc.h>
12 #include <asm/global_data.h>
13 #include <asm/io.h>
14 #include <dm/device_compat.h>
15 #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
21 DECLARE_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 */
49 struct 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 */
66 struct 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 */
83 struct 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
sp_readl(void __iomem * addr,unsigned int offset)92 static inline u32 sp_readl(void __iomem *addr, unsigned int offset)
93 {
94 return readl(addr + offset);
95 }
96
sp_writel(void __iomem * addr,unsigned int offset,u32 data)97 static inline void sp_writel(void __iomem *addr, unsigned int offset, u32 data)
98 {
99 writel(data, addr + offset);
100 }
101
102 /**
103 * k3_sec_proxy_of_xlate() - Translation of phandle to channel
104 * @chan: Mailbox channel
105 * @args: Phandle Pointer
106 *
107 * Translates the phandle args and fills up the Mailbox channel from client.
108 * Return: 0 if all goes good, else return corresponding error message.
109 */
k3_sec_proxy_of_xlate(struct mbox_chan * chan,struct ofnode_phandle_args * args)110 static int k3_sec_proxy_of_xlate(struct mbox_chan *chan,
111 struct ofnode_phandle_args *args)
112 {
113 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
114 int ind, i;
115
116 debug("%s(chan=%p)\n", __func__, chan);
117
118 if (args->args_count != 1) {
119 debug("Invaild args_count: %d\n", args->args_count);
120 return -EINVAL;
121 }
122 ind = args->args[0];
123
124 for (i = 0; i < spm->desc->num_valid_threads; i++)
125 if (spm->chans[i].id == ind) {
126 chan->id = ind;
127 chan->con_priv = &spm->chans[i];
128 return 0;
129 }
130
131 dev_err(chan->dev, "%s: Invalid Thread ID %d\n", __func__, ind);
132 return -ENOENT;
133 }
134
135 /**
136 * k3_sec_proxy_request() - Request for mailbox channel
137 * @chan: Channel Pointer
138 */
k3_sec_proxy_request(struct mbox_chan * chan)139 static int k3_sec_proxy_request(struct mbox_chan *chan)
140 {
141 debug("%s(chan=%p)\n", __func__, chan);
142
143 return 0;
144 }
145
146 /**
147 * k3_sec_proxy_free() - Free the mailbox channel
148 * @chan: Channel Pointer
149 */
k3_sec_proxy_free(struct mbox_chan * chan)150 static int k3_sec_proxy_free(struct mbox_chan *chan)
151 {
152 debug("%s(chan=%p)\n", __func__, chan);
153
154 return 0;
155 }
156
157 /**
158 * k3_sec_proxy_verify_thread() - Verify thread status before
159 * sending/receiving data.
160 * @spt: pointer to secure proxy thread description
161 * @dir: Direction of the thread
162 *
163 * Return: 0 if all goes good, else appropriate error message.
164 */
k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread * spt,u8 dir)165 static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt,
166 u8 dir)
167 {
168 /* Check for any errors already available */
169 if (sp_readl(spt->rt, RT_THREAD_STATUS) &
170 RT_THREAD_STATUS_ERROR_MASK) {
171 printf("%s: Thread %d is corrupted, cannot send data.\n",
172 __func__, spt->id);
173 return -EINVAL;
174 }
175
176 /* Make sure thread is configured for right direction */
177 if ((sp_readl(spt->scfg, SCFG_THREAD_CTRL)
178 & SCFG_THREAD_CTRL_DIR_MASK) >> SCFG_THREAD_CTRL_DIR_SHIFT != dir) {
179 if (dir)
180 printf("%s: Trying to receive data on tx Thread %d\n",
181 __func__, spt->id);
182 else
183 printf("%s: Trying to send data on rx Thread %d\n",
184 __func__, spt->id);
185 return -EINVAL;
186 }
187
188 /* Check the message queue before sending/receiving data */
189 if (!(sp_readl(spt->rt, RT_THREAD_STATUS) &
190 RT_THREAD_STATUS_CUR_CNT_MASK))
191 return -ENODATA;
192
193 return 0;
194 }
195
196 /**
197 * k3_sec_proxy_send() - Send data via mailbox channel
198 * @chan: Channel Pointer
199 * @data: Pointer to k3_sec_proxy_msg
200 *
201 * Return: 0 if all goes good, else appropriate error message.
202 */
k3_sec_proxy_send(struct mbox_chan * chan,const void * data)203 static int k3_sec_proxy_send(struct mbox_chan *chan, const void *data)
204 {
205 const struct k3_sec_proxy_msg *msg = (struct k3_sec_proxy_msg *)data;
206 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
207 struct k3_sec_proxy_thread *spt = chan->con_priv;
208 int num_words, trail_bytes, ret;
209 void __iomem *data_reg;
210 u32 *word_data;
211
212 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
213
214 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX);
215 if (ret) {
216 dev_err(chan->dev,
217 "%s: Thread%d verification failed. ret = %d\n",
218 __func__, spt->id, ret);
219 return ret;
220 }
221
222 /* Check the message size. */
223 if (msg->len > spm->desc->max_msg_size) {
224 dev_err(chan->dev,
225 "%s: Thread %ld message length %zu > max msg size %d\n",
226 __func__, chan->id, msg->len, spm->desc->max_msg_size);
227 return -EINVAL;
228 }
229
230 /* Send the message */
231 data_reg = spt->data + spm->desc->data_start_offset;
232 for (num_words = msg->len / sizeof(u32), word_data = (u32 *)msg->buf;
233 num_words;
234 num_words--, data_reg += sizeof(u32), word_data++)
235 writel(*word_data, data_reg);
236
237 trail_bytes = msg->len % sizeof(u32);
238 if (trail_bytes) {
239 u32 data_trail = *word_data;
240
241 /* Ensure all unused data is 0 */
242 data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
243 writel(data_trail, data_reg);
244 data_reg++;
245 }
246
247 /*
248 * 'data_reg' indicates next register to write. If we did not already
249 * write on tx complete reg(last reg), we must do so for transmit
250 */
251 if (data_reg <= (spt->data + spm->desc->data_end_offset))
252 sp_writel(spt->data, spm->desc->data_end_offset, 0);
253
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 */
k3_sec_proxy_recv(struct mbox_chan * chan,void * data)267 static 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
297 struct mbox_ops k3_sec_proxy_mbox_ops = {
298 .of_xlate = k3_sec_proxy_of_xlate,
299 .request = k3_sec_proxy_request,
300 .rfree = k3_sec_proxy_free,
301 .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 */
k3_sec_proxy_of_to_priv(struct udevice * dev,struct k3_sec_proxy_mbox * spm)312 static 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");
329 if (spm->rt == FDT_ADDR_T_NONE) {
330 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 */
k3_sec_proxy_thread_setup(struct k3_sec_proxy_mbox * spm)349 static 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 */
k3_sec_proxy_probe(struct udevice * dev)375 static 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
k3_sec_proxy_remove(struct udevice * dev)401 static 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
412 /*
413 * Thread ID #4: ROM request
414 * Thread ID #5: ROM response, SYSFW notify
415 * Thread ID #6: SYSFW request response
416 * Thread ID #7: SYSFW request high priority
417 * Thread ID #8: SYSFW request low priority
418 * Thread ID #9: SYSFW notify response
419 */
420 static const u32 am6x_valid_threads[] = { 4, 5, 6, 7, 8, 9, 11, 13 };
421
422 static const struct k3_sec_proxy_desc am654_desc = {
423 .thread_count = 90,
424 .max_msg_size = 60,
425 .data_start_offset = 0x4,
426 .data_end_offset = 0x3C,
427 .valid_threads = am6x_valid_threads,
428 .num_valid_threads = ARRAY_SIZE(am6x_valid_threads),
429 };
430
431 static const struct udevice_id k3_sec_proxy_ids[] = {
432 { .compatible = "ti,am654-secure-proxy", .data = (ulong)&am654_desc},
433 { }
434 };
435
436 U_BOOT_DRIVER(k3_sec_proxy) = {
437 .name = "k3-secure-proxy",
438 .id = UCLASS_MAILBOX,
439 .of_match = k3_sec_proxy_ids,
440 .probe = k3_sec_proxy_probe,
441 .remove = k3_sec_proxy_remove,
442 .priv_auto = sizeof(struct k3_sec_proxy_mbox),
443 .ops = &k3_sec_proxy_mbox_ops,
444 };
445