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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
/* iva_mcu.c
*
* Copyright (C) 2016 Samsung Electronics Co.Ltd
* Authors:
* Ilkwan Kim <ilkwan.kim@samsung.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/time.h>
#include "regs/iva_base_addr.h"
#include "iva_mcu.h"
#include "iva_pmu.h"
#include "iva_sh_mem.h"
#include "iva_ram_dump.h"
#include "iva_mbox.h"
#include "iva_ipc_queue.h"
#include "iva_vdma.h"
//#define MEASURE_MCU_BOOT_PERF
#define MCU_SRAM_VIA_MEMCPY
#define PRINT_CHARS_ALIGN_SIZE (sizeof(uint32_t))
#define PRINT_CHARS_ALIGN_MASK (PRINT_CHARS_ALIGN_SIZE - 1)
#define PRINT_CHARS_ALIGN_POS(a) (a & (~PRINT_CHARS_ALIGN_MASK))
static inline void iva_mcu_print_putchar(struct device *dev, int ch)
{
#define MCU_PRINT_BUF_SIZE (512)
static char mcu_print_buf[MCU_PRINT_BUF_SIZE];
static size_t mcu_print_pos = 0;
static const char *mcu_tag = " [mcu] ";
if (ch != 0 && ch != '\n') {
if (mcu_print_pos == 0) {
strncpy(mcu_print_buf, mcu_tag, MCU_PRINT_BUF_SIZE - 1);
mcu_print_pos = strlen(mcu_tag);
}
mcu_print_buf[mcu_print_pos] = ch;
mcu_print_pos++;
if (mcu_print_pos == (sizeof(mcu_print_buf) - 1))/* full */
goto print;
return;
}
if (ch == 0) {
if (mcu_print_pos == strlen(mcu_tag))
return;
}
/* the case that: if (ch =='\n'): throw it. */
print:
/* print out first */
if (mcu_print_pos != 0) {
mcu_print_buf[mcu_print_pos] = 0;
dev_info(dev, "%s\n", mcu_print_buf);
}
strncpy(mcu_print_buf, mcu_tag, MCU_PRINT_BUF_SIZE - 1);
mcu_print_pos = strlen(mcu_tag);
}
/* put max 4 bytes to output */
static int iva_mcu_put_uint_chars(struct device *dev, uint32_t chars,
uint32_t pos_s/*0-3*/, uint32_t pos_e /*0-3*/)
{
int put_num = 0;
char ch;
if (pos_s > sizeof(chars) - 1) {
dev_err(dev, "%s() error in pos_s(%d), pos_e(%d)\n",
__func__, pos_s, pos_e);
return -1;
}
if (pos_e > sizeof(chars) - 1) {
dev_err(dev, "%s() error in pos_s(%d), pos_e(%d)\n",
__func__, pos_s, pos_e);
return -1;
}
if (pos_e < pos_s)
return 0;
while (pos_s <= pos_e) {
ch = (chars >> (pos_s << 3)) & 0xFF;
iva_mcu_print_putchar(dev, ch);
pos_s++;
put_num++;
}
return put_num;
};
static void iva_mcu_print_emit_pending_chars_nolock(struct mcu_print_info *pi)
{
u32 ch_vals;
uint32_t pos_s, pos_e;
int align_ch_pos;
u32 head;
if (!pi)
return;
head = readl(&pi->log_pos->head);
if (head >= pi->log_buf_size) { /* wrong value */
struct device *dev = pi->dev;
struct iva_dev_data *iva = dev_get_drvdata(dev);
struct sh_mem_info *sh_mem = sh_mem_get_sm_pointer(iva);
dev_err(dev, "%s() magic3(0x%x), head(%d, %d), tail(%d), "
"size(%d)\n", __func__,
readl(&sh_mem->magic3),
(int) readl(&sh_mem->log_pos.head),
(int) head,
(int) readl(&sh_mem->log_pos.tail),
(int) readl(&sh_mem->log_buf_size));
return;
}
if (pi->ch_pos == head)
return;
if (pi->ch_pos > head) {
while (pi->ch_pos < pi->log_buf_size) {
align_ch_pos = PRINT_CHARS_ALIGN_POS(pi->ch_pos); /* 4 bytes */
/* aligned access */
ch_vals = readl(&pi->log_buf[align_ch_pos]);
pos_s = pi->ch_pos - align_ch_pos;
pi->ch_pos += iva_mcu_put_uint_chars(pi->dev, ch_vals,
pos_s, PRINT_CHARS_ALIGN_SIZE - 1);
}
pi->ch_pos = 0;
}
while (pi->ch_pos < head) {
align_ch_pos = PRINT_CHARS_ALIGN_POS(pi->ch_pos);
/* aligned access */
ch_vals = readl(&pi->log_buf[align_ch_pos]);
pos_s = pi->ch_pos - align_ch_pos;
pos_e = (align_ch_pos + PRINT_CHARS_ALIGN_SIZE <= head) ?
PRINT_CHARS_ALIGN_SIZE - 1 :
head - align_ch_pos - 1;
pi->ch_pos += iva_mcu_put_uint_chars(pi->dev, ch_vals, pos_s, pos_e);
}
}
static void iva_mcu_print_worker(struct work_struct *work)
{
struct mcu_print_info *pi = container_of(work, struct mcu_print_info,
dwork.work);
/* emit all pending log */
iva_mcu_print_emit_pending_chars_nolock(pi);
queue_delayed_work(system_wq, &pi->dwork, pi->delay_jiffies);
}
static void iva_mcu_print_start(struct iva_dev_data *iva)
{
struct sh_mem_info *sh_mem = sh_mem_get_sm_pointer(iva);
struct buf_pos __iomem *log_pos = &sh_mem->log_pos;
struct mcu_print_info *pi;
struct device *dev = iva->dev;
u32 tail;
int lb_sz;
if (iva->mcu_print_delay == 0) {
dev_dbg(dev, "%s() will not print out iva log\n", __func__);
return;
}
lb_sz = readl(&sh_mem->log_buf_size);
if (lb_sz & PRINT_CHARS_ALIGN_MASK) {
dev_err(dev, "%s() log_buf_size(0x%x) is not aligned to %d. "
"check the mcu shmem.\n",
__func__, lb_sz, (int) PRINT_CHARS_ALIGN_SIZE);
return;
}
if (((long) sh_mem->log_buf) & PRINT_CHARS_ALIGN_MASK) {
dev_err(dev, "%s() log_buf(%p) is not aligned to %d. "
"check the mcu shmem.\n",
__func__, sh_mem->log_buf,
(int) PRINT_CHARS_ALIGN_SIZE);
return;
}
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
if (!pi) {
dev_err(dev, "%s() fail to allocate memory\n", __func__);
return;
}
pi->dev = iva->dev;
pi->delay_jiffies= usecs_to_jiffies(iva->mcu_print_delay);
pi->log_buf_size= lb_sz;
pi->log_buf = &sh_mem->log_buf[0];
pi->log_pos = log_pos;
tail = readl(&log_pos->tail);
if (tail >= pi->log_buf_size)
pi->ch_pos = 0;
else
pi->ch_pos = tail;
INIT_DELAYED_WORK(&pi->dwork, iva_mcu_print_worker);
queue_delayed_work(system_wq, &pi->dwork, msecs_to_jiffies(1));
iva->mcu_print = pi;
dev_dbg(dev, "%s() delay_jiffies(%ld) success!!!\n", __func__,
pi->delay_jiffies);
}
static void iva_mcu_print_stop(struct iva_dev_data *iva)
{
struct device *dev = iva->dev;
if (iva->mcu_print) {
struct mcu_print_info *pi = iva->mcu_print;
cancel_delayed_work_sync(&pi->dwork);
devm_kfree(dev, pi);
iva->mcu_print = NULL;
}
}
static noinline_for_stack uint32_t iva_mcu_file_size(struct file *file)
{
struct kstat st;
if (vfs_getattr(&file->f_path, &st))
return 0;
if (!S_ISREG(st.mode))
return 0;
return (uint32_t) st.size;
}
void iva_mcu_handle_panic_k(struct iva_dev_data *iva)
{
#if 0 /* entrusts the log to work queue */
iva_mcu_print_emit_pending_chars_nolock(iva->mcu_print);
#endif
}
int32_t iva_mcu_wait_ready(struct iva_dev_data *iva)
{
#define WAIT_DELAY_US (1000)
int ret = sh_mem_wait_mcu_ready(iva, WAIT_DELAY_US);
if (ret) {
dev_err(iva->dev, "%s() mcu is not ready within %d us.\n",
__func__, WAIT_DELAY_US);
return ret;
}
return 0;
}
static inline s32 __maybe_unused timeval_diff_us(struct timeval *a,
struct timeval *b)
{
return (a->tv_sec - b->tv_sec) * USEC_PER_SEC +
(a->tv_usec - b->tv_usec);
}
static inline void iva_mcu_update_iva_sram(struct iva_dev_data *iva,
struct mcu_binary *mcu_bin)
{
#ifdef CONFIG_ION_EXYNOS
uint32_t vdma_hd = ID_CH_TO_HANDLE(vdma_id_1, vdma_ch_2);
dev_dbg(iva->dev, "%s() prepare vdma iova(0x%x) size(0x%x, 0x%x)\n",
__func__, (uint32_t) mcu_bin->io_va,
mcu_bin->file_size, ALIGN(mcu_bin->file_size, 16));
iva_vdma_enable(iva, vdma_id_1, true);
iva_vdma_config_dram_to_mcu(iva, vdma_hd,
mcu_bin->io_va, 0x0, ALIGN(mcu_bin->file_size, 16),
true);
iva_vdma_wait_done(iva, vdma_hd);
iva_vdma_enable(iva, vdma_id_1, false);
#else
void __iomem *mcu_va = iva->iva_va + IVA_VMCU_MEM_BASE_ADDR;
memcpy(mcu_va, mcu_bin->bin, mcu_bin->bin_size);
#endif
}
static int32_t iva_mcu_boot(struct iva_dev_data *iva, struct mcu_binary *mcu_bin,
int32_t wait_ready)
{
int ret;
struct device *dev = iva->dev;
if (!mcu_bin) {
dev_err(dev, "%s() check null pointer\n", __func__);
return -EINVAL;
}
#if 0
if (mcu_bin_size != VMCU_MEM_SIZE) {
dev_err(dev, "%s() mcu binary seems to have wrong size(%d)\n",
__func__, mcu_bin_size);
return -EINVAL;
}
#endif
iva_pmu_prepare_mcu_sram(iva);
iva_mcu_update_iva_sram(iva, mcu_bin);
/* start to boot */
iva_pmu_reset_mcu(iva);
/* monitor shared memory initialization from mcu */
ret = sh_mem_init(iva);
if (ret) {
dev_err(dev, "%s() mcu seems to fail to boot.\n", __func__);
return ret;
}
/* enable mcu print */
iva_mcu_print_start(iva);
if (wait_ready)
iva_mcu_wait_ready(iva);
/* enable ipq queue */
ret = iva_ipcq_init(&iva->mcu_ipcq,
&sh_mem_get_sm_pointer(iva)->ap_cmd_q,
&sh_mem_get_sm_pointer(iva)->ap_rsp_q);
if (ret < 0) {
dev_err(dev, "%s() fail to init ipcq (%d)\n", __func__, ret);
goto err_ipcq;
}
return 0;
err_ipcq:
sh_mem_deinit(iva);
iva_mcu_print_stop(iva);
return ret;
}
int32_t iva_mcu_boot_file(struct iva_dev_data *iva,
const char *mcu_file, int32_t wait_ready)
{
struct device *dev = iva->dev;
struct file *mcu_fp;
uint32_t mcu_size;
struct mcu_binary *mcu_bin = iva->mcu_bin;
int ret = 0;
int read_bytes;
dev_dbg(dev, "%s() try to boot with %s", __func__, mcu_file);
mcu_fp = filp_open(mcu_file, O_RDONLY, 0);
if (IS_ERR_OR_NULL(mcu_fp)) {
dev_err(dev, "%s() unable to open mcu binary: %s\n",
__func__, mcu_file);
return -EINVAL;
}
mcu_size = iva_mcu_file_size(mcu_fp);
if (mcu_size <= 0 || mcu_size > VMCU_MEM_SIZE) {
dev_err(dev, "%s() file size(0x%x) is larger that expected %d\n",
__func__, mcu_size, VMCU_MEM_SIZE);
goto err_mcu_file;
}
/* only one case is not covered, fix it */
if (mcu_size == mcu_bin->file_size &&
strcmp(mcu_file, mcu_bin->file) == 0) {
dev_dbg(dev, "%s() regard the same as previous(%s, 0x%x)\n",
__func__, mcu_file, mcu_size);
goto skip_read;
}
if (mcu_bin->flag & MCU_BIN_ION) {
if (dma_buf_begin_cpu_access(mcu_bin->dmabuf,
0, VMCU_MEM_SIZE, 0)) {
ret = -ENOMEM;
goto err_mcu_file;
}
mcu_bin->bin = dma_buf_kmap(mcu_bin->dmabuf, 0);
if (!mcu_bin->bin) {
dev_err(dev, "%s() fail to kmap ion\n", __func__);
dma_buf_end_cpu_access(mcu_bin->dmabuf, 0,
VMCU_MEM_SIZE, 0);
ret = -ENOMEM;
goto err_mcu_file;
}
}
read_bytes = kernel_read(mcu_fp, 0, mcu_bin->bin,
(unsigned long) mcu_size);
if ((read_bytes <= 0) || ((uint32_t) read_bytes != mcu_size)) {
dev_err(dev, "%s() unable to read %s sucessfully. "
"read(%d) file size(%d)\n",
__func__, mcu_file, read_bytes, mcu_size);
ret = -EIO;
goto err_ion_kmap;
}
mcu_bin->file_size = mcu_size;
strncpy(mcu_bin->file, mcu_file, sizeof(mcu_bin->file) - 1);
mcu_bin->file[sizeof(mcu_bin->file) - 1] = 0x0;
skip_read:
ret = iva_mcu_boot(iva, mcu_bin, wait_ready);
if (ret) {
dev_err(dev, "%s() fail to boot mcu. ret(%d)\n",
__func__, ret);
}
err_ion_kmap:
if (mcu_bin->flag & MCU_BIN_ION) {
if (mcu_bin->bin) {
dma_buf_kunmap(mcu_bin->dmabuf, 0, mcu_bin->bin);
dma_buf_end_cpu_access(mcu_bin->dmabuf, 0, VMCU_MEM_SIZE, 0);
mcu_bin->bin = NULL;
}
}
err_mcu_file:
filp_close(mcu_fp, NULL);
return ret;
}
int32_t iva_mcu_exit(struct iva_dev_data *iva)
{
iva_ipcq_deinit(&iva->mcu_ipcq);
iva_mcu_print_stop(iva);
sh_mem_deinit(iva);
return 0;
}
#ifdef CONFIG_ION_EXYNOS
#include <linux/exynos_ion.h>
#define REQ_ION_HEAP_ID EXYNOS_ION_HEAP_SYSTEM_MASK
#endif
static int32_t iva_mcu_boot_mem_alloc(struct iva_dev_data *iva,
struct mcu_binary *mcu_bin)
{
int32_t ret = 0;
struct device *dev = iva->dev;
#ifdef CONFIG_ION_EXYNOS
struct ion_client *client = iva->ion_client;
struct ion_handle *handle;
struct dma_buf *dmabuf;
struct dma_buf_attachment *attachment;
dma_addr_t io_va;
handle = ion_alloc(client, VMCU_MEM_SIZE, 16, REQ_ION_HEAP_ID,
/*ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC*/ 0);
if (IS_ERR(handle)) {
dev_err(dev, "%s() fail to alloc ion with id(0x%x), "
"size(0x%x),ret(%ld)\n",
__func__, REQ_ION_HEAP_ID,
VMCU_MEM_SIZE, PTR_ERR(handle));
return PTR_ERR(handle);
}
dmabuf = ion_share_dma_buf(client, handle);
if (!dmabuf) {
dev_err(dev, "%s() ion with dma_buf sharing failed.\n",
__func__);
ret = -ENOMEM;
goto err_dmabuf;
}
attachment = dma_buf_attach(dmabuf, dev);
if (!attachment) {
dev_err(dev, "%s() fail to attach dma buf, dmabuf(%p).\n",
__func__, dmabuf);
goto err_attach;
}
io_va = ion_iovmm_map(attachment, 0, VMCU_MEM_SIZE, DMA_TO_DEVICE, 0);
if (!io_va || IS_ERR_VALUE(io_va)) {
dev_err(dev, "%s() fail to map iovm (%ld)\n", __func__,
(unsigned long) io_va);
if (!io_va)
ret = -ENOMEM;
else
ret = (int) io_va;
goto err_get_iova;
}
mcu_bin->flag = MCU_BIN_ION;
mcu_bin->bin_size = VMCU_MEM_SIZE;
mcu_bin->handle = handle;
mcu_bin->dmabuf = dmabuf;
mcu_bin->attachment = attachment;
mcu_bin->io_va = io_va;
return 0;
err_get_iova:
dma_buf_detach(dmabuf, attachment);
err_attach:
dma_buf_put(dmabuf);
err_dmabuf:
ion_free(client, handle);
#else
mcu_bin->bin = kmalloc(VMCU_MEM_SIZE, GFP_KERNEL);
if (!mcu_bin->bin) {
dev_err(dev, "%s() unable to alloc mem for mcu boot\n",
__func__);
return -ENOMEM;
}
mcu_bin->flag = MCU_BIN_MALLOC;
mcu_bin->bin_size = VMCU_MEM_SIZE;
#endif
return ret;
}
static void iva_mcu_boot_mem_free(struct iva_dev_data *iva,
struct mcu_binary *mcu_bin)
{
#ifdef CONFIG_ION_EXYNOS
struct ion_client *client = iva->ion_client;
if (mcu_bin->attachment) {
ion_iovmm_unmap(mcu_bin->attachment, mcu_bin->io_va);
if (mcu_bin->dmabuf)
dma_buf_detach(mcu_bin->dmabuf, mcu_bin->attachment);
mcu_bin->io_va = 0x0;
mcu_bin->attachment = NULL;
}
if (mcu_bin->dmabuf) {
dma_buf_put(mcu_bin->dmabuf);
mcu_bin->dmabuf = NULL;
}
if (mcu_bin->handle) {
ion_free(client, mcu_bin->handle);
mcu_bin->handle = NULL;
mcu_bin->bin_size = 0;
}
#else
struct device *dev = iva->dev;
if (mcu_bin->bin) {
devm_kfree(dev, mcu_bin->bin);
mcu_bin->bin = NULL;
mcu_bin->bin_size = 0;
}
#endif
mcu_bin->flag = 0x0;
return;
}
int32_t iva_mcu_probe(struct iva_dev_data *iva)
{
int32_t ret;
struct device *dev = iva->dev;
iva->mcu_bin = kzalloc(sizeof(*iva->mcu_bin), GFP_KERNEL);
if (!iva->mcu_bin) {
dev_err(iva->dev, "%s() fail to alloc mem for mcu_bin struct\n",
__func__);
return -ENOMEM;
}
ret = iva_mcu_boot_mem_alloc(iva, iva->mcu_bin);
if (ret) {
dev_err(dev, "%s() fail to prepare mcu boot mem ret(%d)\n",
__func__, ret);
goto err_boot_mem;
}
ret = iva_mbox_probe(iva);
if (ret) {
dev_err(dev, "%s() fail to probe iva mbox. ret(%d)\n",
__func__, ret);
goto err_mbox_prb;
}
ret = iva_ipcq_probe(iva);
if (ret) {
dev_err(dev, "%s() fail to probe iva mbox. ret(%d)\n",
__func__, ret);
goto err_ipcq_prb;
}
/* enable ram dump, just registration*/
iva_ram_dump_init(iva);
return 0;
err_ipcq_prb:
iva_mbox_remove(iva);
err_mbox_prb:
iva_mcu_boot_mem_free(iva, iva->mcu_bin);
err_boot_mem:
devm_kfree(iva->dev, iva->mcu_bin);
iva->mcu_bin = NULL;
return ret;
}
void iva_mcu_remove(struct iva_dev_data *iva)
{
iva_ram_dump_deinit(iva);
iva_ipcq_remove(iva);
iva_mbox_remove(iva);
if (iva->mcu_bin) {
iva_mcu_boot_mem_free(iva, iva->mcu_bin);
devm_kfree(iva->dev, iva->mcu_bin);
iva->mcu_bin = NULL;
}
}
void iva_mcu_shutdown(struct iva_dev_data *iva)
{
/* forced to block safely at minimum: - report to mcu? */
iva_mcu_print_stop(iva);
/* forced to block mbox interrupt */
iva_mbox_remove(iva);
}
|