blob: 672fa5bf3e5bd697058ef19890c8cc0ff32c810a [file] [log] [blame]
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +01001/*
2 * Copyright (c) 2016, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Intel Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
29 * Keyon Jie <yang.jie@linux.intel.com>
30 */
31
32#ifndef __INCLUDE_DEBUG__
33#define __INCLUDE_DEBUG__
34
Liam Girdwoode52ce682018-02-21 15:36:25 +000035#include <reef/reef.h>
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010036#include <reef/mailbox.h>
Liam Girdwoode52ce682018-02-21 15:36:25 +000037#include <uapi/ipc.h>
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010038#include <platform/platform.h>
39#include <stdint.h>
40#include <stdlib.h>
41
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +010042
43#define DEBUG
44
45#ifdef DEBUG
46
47/* dump file and line to start of mailbox or shared memory */
48#define dbg() \
49 do { \
50 volatile uint32_t *__m = (uint32_t*)mailbox_get_debug_base(); \
51 *(__m++) = (__FILE__[0] << 24) + (__FILE__[1] << 16) +\
52 (__FILE__[2] << 8) + (__FILE__[3]); \
53 *(__m++) = (__func__[0] << 24) + (__func__[1] << 16) + \
54 (__func__[2] << 8) + (__func__[3]); \
55 *__m = __LINE__; \
56 } while (0);
57
58/* dump file and line to offset in mailbox or shared memory */
59#define dbg_at(__x) \
60 do { \
61 volatile uint32_t *__m = (uint32_t*)mailbox_get_debug_base() + __x; \
62 *(__m++) = (__FILE__[0] << 24) + (__FILE__[1] << 16) +\
63 (__FILE__[2] << 8) + (__FILE__[3]); \
64 *(__m++) = (__func__[0] << 24) + (__func__[1] << 16) + \
65 (__func__[2] << 8) + (__func__[3]); \
66 *__m = __LINE__; \
67 } while (0);
68
69/* dump value to start of mailbox or shared memory */
70#define dbg_val(__v) \
71 do { \
72 volatile uint32_t *__m = \
73 (volatile uint32_t*)mailbox_get_debug_base(); \
74 *__m = __v; \
75 } while (0);
76
77/* dump value to offset in mailbox or shared memory */
78#define dbg_val_at(__v, __x) \
79 do { \
80 volatile uint32_t *__m = \
81 (volatile uint32_t*)mailbox_get_debug_base() + __x; \
82 *__m = __v; \
83 } while (0);
84
85/* dump data area at addr and size count to start of mailbox or shared memory */
86#define dump(addr, count) \
87 do { \
88 volatile uint32_t *__m = (uint32_t*)mailbox_get_debug_base(); \
89 volatile uint32_t *__a = (uint32_t*)addr; \
90 volatile int __c = count; \
91 while (__c--) \
92 *(__m++) = *(__a++); \
93 } while (0);
94
Pierre-Louis Bossartf9458092017-11-09 15:24:07 -060095/* dump data area at addr and size count at mailbox offset or shared memory */
Liam Girdwood60bb5742017-10-19 17:28:02 +010096#define dump_at(addr, count, offset) \
97 do { \
98 volatile uint32_t *__m = (uint32_t*)mailbox_get_debug_base() + offset; \
99 volatile uint32_t *__a = (uint32_t*)addr; \
100 volatile int __c = count; \
101 while (__c--) \
102 *(__m++) = *(__a++); \
103 } while (0);
104
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100105/* dump object to start of mailbox */
106#define dump_object(__o) \
107 dbg(); \
108 dump(&__o, sizeof(__o) >> 2);
109
110/* dump object from pointer at start of mailbox */
111#define dump_object_ptr(__o) \
112 dbg(); \
113 dump(__o, sizeof(*(__o)) >> 2);
114
Liam Girdwood60bb5742017-10-19 17:28:02 +0100115#define dump_object_ptr_at(__o, __at) \
116 dbg(); \
117 dump_at(__o, sizeof(*(__o)) >> 2, __at);
118
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100119#else
120
121#define dbg()
122#define dbg_at(__x)
123#define dbg_val(__v)
124#define dbg_val_at(__v, __x)
125#define dump(addr, count)
126#define dump_object(__o)
127#define dump_object_ptr(__o)
128#endif
129
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100130/* dump stack as part of panic */
Liam Girdwooda23e2462018-02-25 20:23:29 +0000131static inline uint32_t dump_stack(uint32_t p, void *addr, size_t offset,
Liam Girdwoode52ce682018-02-21 15:36:25 +0000132 size_t limit)
133{
134 extern void *__stack;
135 extern void *_stack_sentry;
Liam Girdwoodbbad6ec2018-02-23 13:49:32 +0000136 void *stack_bottom = (void *)&__stack - sizeof(void *);
Liam Girdwoode52ce682018-02-21 15:36:25 +0000137 void *stack_limit = (void *)&_stack_sentry;
138 void *stack_top = arch_get_stack_ptr() + offset;
139 size_t size = stack_bottom - stack_top;
140
141 /* is stack smashed ? */
142 if (stack_top - offset <= stack_limit) {
143 stack_bottom = stack_limit;
144 p = SOF_IPC_PANIC_STACK;
Liam Girdwooda23e2462018-02-25 20:23:29 +0000145 return p;
Liam Girdwoode52ce682018-02-21 15:36:25 +0000146 }
147
148 /* make sure stack size won't overflow dump area */
149 if (size > limit)
150 size = limit;
151
152 /* copy stack contents and writeback */
153 rmemcpy(addr, stack_top, size - sizeof(void *));
154 dcache_writeback_region(addr, size - sizeof(void *));
Liam Girdwooda23e2462018-02-25 20:23:29 +0000155 return p;
Liam Girdwoode52ce682018-02-21 15:36:25 +0000156}
Liam Girdwoodc0dfb4e2016-09-21 15:57:22 +0100157
158#endif