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> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 22 | #include <arch/early_variables.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 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 48 | #else |
| 49 | |
| 50 | /* |
| 51 | * When running from RAM, a lot of console output is generated before CBMEM is |
| 52 | * reinitialized. This static buffer is used to store that output temporarily, |
| 53 | * to be concatenated with the CBMEM console buffer contents accumulated |
| 54 | * during the ROM stage, once CBMEM becomes available at RAM stage. |
| 55 | */ |
Stefan Reinauer | 19d06b2 | 2013-09-09 11:22:18 -0700 | [diff] [blame] | 56 | |
| 57 | #if CONFIG_DYNAMIC_CBMEM |
| 58 | #define STATIC_CONSOLE_SIZE 1024 |
| 59 | #else |
Kyösti Mälkki | 44c0e4e | 2013-11-27 22:54:11 +0200 | [diff] [blame] | 60 | #define STATIC_CONSOLE_SIZE CONFIG_CONSOLE_CBMEM_BUFFER_SIZE |
Stefan Reinauer | 19d06b2 | 2013-09-09 11:22:18 -0700 | [diff] [blame] | 61 | #endif |
| 62 | static u8 static_console[STATIC_CONSOLE_SIZE]; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 63 | #endif |
| 64 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 65 | static inline struct cbmem_console *current_console(void) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 66 | { |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 67 | return car_get_var(cbmem_console_p); |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline void current_console_set(struct cbmem_console *new_console_p) |
| 71 | { |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 72 | car_set_var(cbmem_console_p, new_console_p); |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void init_console_ptr(void *storage, u32 total_space) |
| 76 | { |
| 77 | struct cbmem_console *cbm_cons_p = storage; |
| 78 | |
| 79 | /* Initialize the cache-as-ram pointer and underlying structure. */ |
| 80 | car_set_var(cbmem_console_p, cbm_cons_p); |
| 81 | cbm_cons_p->buffer_size = total_space - sizeof(struct cbmem_console); |
| 82 | cbm_cons_p->buffer_cursor = 0; |
| 83 | } |
| 84 | |
| 85 | void cbmemc_init(void) |
| 86 | { |
| 87 | #ifdef __PRE_RAM__ |
| 88 | init_console_ptr(&car_cbmem_console, CONFIG_CONSOLE_CAR_BUFFER_SIZE); |
| 89 | #else |
| 90 | /* |
| 91 | * Initializing before CBMEM is available, use static buffer to store |
| 92 | * the log. |
| 93 | */ |
| 94 | init_console_ptr(static_console, sizeof(static_console)); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | void cbmemc_tx_byte(unsigned char data) |
| 99 | { |
| 100 | struct cbmem_console *cbm_cons_p = current_console(); |
| 101 | u32 cursor; |
| 102 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 103 | if (!cbm_cons_p) |
| 104 | return; |
| 105 | |
| 106 | cursor = cbm_cons_p->buffer_cursor++; |
| 107 | if (cursor < cbm_cons_p->buffer_size) |
| 108 | cbm_cons_p->buffer_body[cursor] = data; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Copy the current console buffer (either from the cache as RAM area, or from |
| 113 | * the static buffer, pointed at by cbmem_console_p) into the CBMEM console |
| 114 | * buffer space (pointed at by new_cons_p), concatenating the copied data with |
| 115 | * the CBMEM console buffer contents. |
| 116 | * |
| 117 | * 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] | 118 | * overflow and the number of dropped characters. |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 119 | */ |
| 120 | static void copy_console_buffer(struct cbmem_console *new_cons_p) |
| 121 | { |
Kyösti Mälkki | a3c5ba3 | 2013-11-27 17:51:31 +0200 | [diff] [blame] | 122 | u32 copy_size, dropped_chars; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 123 | u32 cursor = new_cons_p->buffer_cursor; |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 124 | struct cbmem_console *old_cons_p; |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 125 | |
| 126 | old_cons_p = current_console(); |
| 127 | |
Kyösti Mälkki | a3c5ba3 | 2013-11-27 17:51:31 +0200 | [diff] [blame] | 128 | if (old_cons_p->buffer_cursor < old_cons_p->buffer_size) |
| 129 | copy_size = old_cons_p->buffer_cursor; |
| 130 | else |
| 131 | copy_size = old_cons_p->buffer_size; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 132 | |
Kyösti Mälkki | a3c5ba3 | 2013-11-27 17:51:31 +0200 | [diff] [blame] | 133 | if (cursor > new_cons_p->buffer_size) |
| 134 | copy_size = 0; |
| 135 | else if (cursor + copy_size > new_cons_p->buffer_size) |
| 136 | copy_size = new_cons_p->buffer_size - cursor; |
| 137 | |
| 138 | dropped_chars = old_cons_p->buffer_cursor - copy_size; |
| 139 | if (dropped_chars) { |
| 140 | /* Reserve 80 chars to report overflow, if possible. */ |
| 141 | if (copy_size < 80) |
| 142 | return; |
| 143 | copy_size -= 80; |
| 144 | dropped_chars += 80; |
| 145 | } |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 146 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 147 | memcpy(new_cons_p->buffer_body + cursor, old_cons_p->buffer_body, |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 148 | copy_size); |
| 149 | |
| 150 | cursor += copy_size; |
| 151 | |
Kyösti Mälkki | a3c5ba3 | 2013-11-27 17:51:31 +0200 | [diff] [blame] | 152 | if (dropped_chars) { |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 153 | const char loss_str1[] = "\n\n*** Log truncated, "; |
| 154 | const char loss_str2[] = " characters dropped. ***\n\n"; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * When running from ROM sprintf is not available, a simple |
| 158 | * itoa implementation is used instead. |
| 159 | */ |
| 160 | int got_first_digit = 0; |
| 161 | |
| 162 | /* Way more than possible number of dropped characters. */ |
| 163 | u32 mult = 100000; |
| 164 | |
| 165 | strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1); |
| 166 | cursor += sizeof(loss_str1) - 1; |
| 167 | |
| 168 | while (mult) { |
| 169 | int digit = dropped_chars / mult; |
| 170 | if (got_first_digit || digit) { |
| 171 | new_cons_p->buffer_body[cursor++] = digit + '0'; |
| 172 | dropped_chars %= mult; |
| 173 | /* Excessive, but keeps it simple */ |
| 174 | got_first_digit = 1; |
| 175 | } |
| 176 | mult /= 10; |
| 177 | } |
| 178 | |
| 179 | strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2); |
| 180 | cursor += sizeof(loss_str2) - 1; |
| 181 | } |
| 182 | new_cons_p->buffer_cursor = cursor; |
| 183 | } |
| 184 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 185 | void cbmemc_reinit(void) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 186 | { |
Kyösti Mälkki | 52a2722 | 2013-09-09 01:31:22 +0300 | [diff] [blame] | 187 | struct cbmem_console *cbm_cons_p = NULL; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 188 | |
Kyösti Mälkki | 52a2722 | 2013-09-09 01:31:22 +0300 | [diff] [blame] | 189 | #ifndef __PRE_RAM__ |
| 190 | cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE); |
| 191 | #endif |
| 192 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 193 | if (!cbm_cons_p) { |
Kyösti Mälkki | 52a2722 | 2013-09-09 01:31:22 +0300 | [diff] [blame] | 194 | cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE, |
| 195 | CONFIG_CONSOLE_CBMEM_BUFFER_SIZE); |
| 196 | |
| 197 | if (!cbm_cons_p) { |
| 198 | current_console_set(NULL); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE - |
| 203 | sizeof(struct cbmem_console); |
| 204 | |
| 205 | cbm_cons_p->buffer_cursor = 0; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 208 | copy_console_buffer(cbm_cons_p); |
| 209 | |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 210 | current_console_set(cbm_cons_p); |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 211 | } |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 212 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 213 | /* Call cbmemc_reinit() at CAR migration time. */ |
| 214 | CAR_MIGRATE(cbmemc_reinit) |