Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 1 | /* |
| 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 Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 35 | #include <reef/reef.h> |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 36 | #include <reef/mailbox.h> |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 37 | #include <uapi/ipc.h> |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 38 | #include <platform/platform.h> |
| 39 | #include <stdint.h> |
| 40 | #include <stdlib.h> |
| 41 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 42 | |
| 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 Bossart | f945809 | 2017-11-09 15:24:07 -0600 | [diff] [blame] | 95 | /* dump data area at addr and size count at mailbox offset or shared memory */ |
Liam Girdwood | 60bb574 | 2017-10-19 17:28:02 +0100 | [diff] [blame] | 96 | #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 Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 105 | /* 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 Girdwood | 60bb574 | 2017-10-19 17:28:02 +0100 | [diff] [blame] | 115 | #define dump_object_ptr_at(__o, __at) \ |
| 116 | dbg(); \ |
| 117 | dump_at(__o, sizeof(*(__o)) >> 2, __at); |
| 118 | |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 119 | #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 Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 130 | /* dump stack as part of panic */ |
Liam Girdwood | a23e246 | 2018-02-25 20:23:29 +0000 | [diff] [blame^] | 131 | static inline uint32_t dump_stack(uint32_t p, void *addr, size_t offset, |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 132 | size_t limit) |
| 133 | { |
| 134 | extern void *__stack; |
| 135 | extern void *_stack_sentry; |
Liam Girdwood | bbad6ec | 2018-02-23 13:49:32 +0000 | [diff] [blame] | 136 | void *stack_bottom = (void *)&__stack - sizeof(void *); |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 137 | 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 Girdwood | a23e246 | 2018-02-25 20:23:29 +0000 | [diff] [blame^] | 145 | return p; |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 146 | } |
| 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 Girdwood | a23e246 | 2018-02-25 20:23:29 +0000 | [diff] [blame^] | 155 | return p; |
Liam Girdwood | e52ce68 | 2018-02-21 15:36:25 +0000 | [diff] [blame] | 156 | } |
Liam Girdwood | c0dfb4e | 2016-09-21 15:57:22 +0100 | [diff] [blame] | 157 | |
| 158 | #endif |