Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <console/console.h> |
| 21 | #include <cbmem.h> |
Gabe Black | 19e7e7d | 2011-10-01 04:27:32 -0700 | [diff] [blame] | 22 | #include <cpu/x86/car.h> |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | /* |
| 26 | * Structure describing console buffer. It is overlaid on a flat memory area, |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 27 | * with buffer_body covering the extent of the memory. Once the buffer is |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 28 | * full, the cursor keeps going but the data is dropped on the floor. This |
| 29 | * allows to tell how much data was lost in the process. |
| 30 | */ |
| 31 | struct cbmem_console { |
| 32 | u32 buffer_size; |
| 33 | u32 buffer_cursor; |
| 34 | u8 buffer_body[0]; |
| 35 | } __attribute__ ((__packed__)); |
| 36 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 37 | static struct cbmem_console *cbmem_console_p CAR_GLOBAL; |
| 38 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 39 | #ifdef __PRE_RAM__ |
| 40 | /* |
| 41 | * While running from ROM, before DRAM is initialized, some area in cache as |
| 42 | * ram space is used for the console buffer storage. The size and location of |
| 43 | * the area are defined in the config. |
| 44 | */ |
Gabe Black | 19e7e7d | 2011-10-01 04:27:32 -0700 | [diff] [blame] | 45 | |
| 46 | static struct cbmem_console car_cbmem_console CAR_CBMEM; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Once DRAM is initialized and the cache as ram mode is disabled, while still |
| 50 | * running from ROM, the console buffer in the cache as RAM area becomes |
| 51 | * unavailable. |
| 52 | * |
| 53 | * By this time the console log buffer is already available in |
| 54 | * CBMEM. The location at 0x600 is used as the redirect pointer allowing to |
| 55 | * find out where the actual console log buffer is. |
| 56 | */ |
| 57 | #define CBMEM_CONSOLE_REDIRECT (*((struct cbmem_console **)0x600)) |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 58 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 59 | #else |
| 60 | |
| 61 | /* |
| 62 | * When running from RAM, a lot of console output is generated before CBMEM is |
| 63 | * reinitialized. This static buffer is used to store that output temporarily, |
| 64 | * to be concatenated with the CBMEM console buffer contents accumulated |
| 65 | * during the ROM stage, once CBMEM becomes available at RAM stage. |
| 66 | */ |
| 67 | static u8 static_console[40000]; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 70 | static inline struct cbmem_console *current_console(void) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 71 | { |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 72 | #if CONFIG_CAR_MIGRATION || !defined(__PRE_RAM__) |
| 73 | return car_get_var(cbmem_console_p); |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 74 | #else |
| 75 | /* |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 76 | * This check allows to tell if the cache as RAM mode has been exited |
| 77 | * or not. If it has been exited, the real memory is being used |
| 78 | * (resulting in the variable on the stack located below |
| 79 | * DCACHE_RAM_BASE), use the redirect pointer to find out where the |
| 80 | * actual console buffer is. |
| 81 | */ |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 82 | if ((uintptr_t)__builtin_frame_address(0) < |
| 83 | (uintptr_t)CONFIG_DCACHE_RAM_BASE) |
| 84 | return CBMEM_CONSOLE_REDIRECT; |
| 85 | return car_get_var(cbmem_console_p); |
| 86 | #endif /* CONFIG_CAR_MIGRATION */ |
| 87 | } |
| 88 | |
| 89 | static inline void current_console_set(struct cbmem_console *new_console_p) |
| 90 | { |
| 91 | #if CONFIG_CAR_MIGRATION || !defined(__PRE_RAM__) |
| 92 | car_set_var(cbmem_console_p, new_console_p); |
| 93 | #else |
| 94 | CBMEM_CONSOLE_REDIRECT = new_console_p; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 95 | #endif |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline void init_console_ptr(void *storage, u32 total_space) |
| 99 | { |
| 100 | struct cbmem_console *cbm_cons_p = storage; |
| 101 | |
| 102 | /* Initialize the cache-as-ram pointer and underlying structure. */ |
| 103 | car_set_var(cbmem_console_p, cbm_cons_p); |
| 104 | cbm_cons_p->buffer_size = total_space - sizeof(struct cbmem_console); |
| 105 | cbm_cons_p->buffer_cursor = 0; |
| 106 | } |
| 107 | |
| 108 | void cbmemc_init(void) |
| 109 | { |
| 110 | #ifdef __PRE_RAM__ |
| 111 | init_console_ptr(&car_cbmem_console, CONFIG_CONSOLE_CAR_BUFFER_SIZE); |
| 112 | #else |
| 113 | /* |
| 114 | * Initializing before CBMEM is available, use static buffer to store |
| 115 | * the log. |
| 116 | */ |
| 117 | init_console_ptr(static_console, sizeof(static_console)); |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | void cbmemc_tx_byte(unsigned char data) |
| 122 | { |
| 123 | struct cbmem_console *cbm_cons_p = current_console(); |
| 124 | u32 cursor; |
| 125 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 126 | if (!cbm_cons_p) |
| 127 | return; |
| 128 | |
| 129 | cursor = cbm_cons_p->buffer_cursor++; |
| 130 | if (cursor < cbm_cons_p->buffer_size) |
| 131 | cbm_cons_p->buffer_body[cursor] = data; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Copy the current console buffer (either from the cache as RAM area, or from |
| 136 | * the static buffer, pointed at by cbmem_console_p) into the CBMEM console |
| 137 | * buffer space (pointed at by new_cons_p), concatenating the copied data with |
| 138 | * the CBMEM console buffer contents. |
| 139 | * |
| 140 | * If there is overflow - add to the destination area a string, reporting the |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 141 | * overflow and the number of dropped characters. |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 142 | */ |
| 143 | static void copy_console_buffer(struct cbmem_console *new_cons_p) |
| 144 | { |
| 145 | u32 copy_size; |
| 146 | u32 cursor = new_cons_p->buffer_cursor; |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 147 | struct cbmem_console *old_cons_p; |
| 148 | int overflow; |
| 149 | |
| 150 | old_cons_p = current_console(); |
| 151 | |
| 152 | overflow = old_cons_p->buffer_cursor > old_cons_p->buffer_size; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 153 | |
| 154 | copy_size = overflow ? |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 155 | old_cons_p->buffer_size : old_cons_p->buffer_cursor; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 156 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 157 | memcpy(new_cons_p->buffer_body + cursor, old_cons_p->buffer_body, |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 158 | copy_size); |
| 159 | |
| 160 | cursor += copy_size; |
| 161 | |
| 162 | if (overflow) { |
| 163 | const char loss_str1[] = "\n\n*** Log truncated, "; |
| 164 | const char loss_str2[] = " characters dropped. ***\n\n"; |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 165 | u32 dropped_chars = old_cons_p->buffer_cursor - copy_size; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * When running from ROM sprintf is not available, a simple |
| 169 | * itoa implementation is used instead. |
| 170 | */ |
| 171 | int got_first_digit = 0; |
| 172 | |
| 173 | /* Way more than possible number of dropped characters. */ |
| 174 | u32 mult = 100000; |
| 175 | |
| 176 | strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1); |
| 177 | cursor += sizeof(loss_str1) - 1; |
| 178 | |
| 179 | while (mult) { |
| 180 | int digit = dropped_chars / mult; |
| 181 | if (got_first_digit || digit) { |
| 182 | new_cons_p->buffer_body[cursor++] = digit + '0'; |
| 183 | dropped_chars %= mult; |
| 184 | /* Excessive, but keeps it simple */ |
| 185 | got_first_digit = 1; |
| 186 | } |
| 187 | mult /= 10; |
| 188 | } |
| 189 | |
| 190 | strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2); |
| 191 | cursor += sizeof(loss_str2) - 1; |
| 192 | } |
| 193 | new_cons_p->buffer_cursor = cursor; |
| 194 | } |
| 195 | |
Kyösti Mälkki | c65001f | 2013-06-19 10:41:18 +0300 | [diff] [blame^] | 196 | static void cbmemc_reinit_(void) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 197 | { |
| 198 | struct cbmem_console *cbm_cons_p; |
| 199 | |
| 200 | #ifdef __PRE_RAM__ |
| 201 | cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE, |
| 202 | CONFIG_CONSOLE_CBMEM_BUFFER_SIZE); |
| 203 | if (!cbm_cons_p) { |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 204 | current_console_set(NULL); |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE - |
| 209 | sizeof(struct cbmem_console); |
| 210 | |
| 211 | cbm_cons_p->buffer_cursor = 0; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 212 | #else |
| 213 | cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE); |
| 214 | |
| 215 | if (!cbm_cons_p) |
| 216 | return; |
| 217 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 218 | #endif |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 219 | copy_console_buffer(cbm_cons_p); |
| 220 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 221 | current_console_set(cbm_cons_p); |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 222 | } |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 223 | |
Kyösti Mälkki | c65001f | 2013-06-19 10:41:18 +0300 | [diff] [blame^] | 224 | void cbmemc_reinit(void) |
| 225 | { |
| 226 | #if !CONFIG_CAR_MIGRATION || !defined(__PRE_RAM__) |
| 227 | cbmemc_reinit_(); |
| 228 | #endif |
| 229 | } |
| 230 | |
| 231 | /* Call cbmemc_reinit_() at CAR migration time. */ |
| 232 | CAR_MIGRATE(cbmemc_reinit_) |