blob: ac1d8bbd7e78116b6efb1b22cf43d8a2e6ef71ca [file] [log] [blame]
Vadim Bendebury32da8be2011-09-29 17:27:15 -07001/*
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 Reinauerfd4f4132013-06-19 12:25:44 -070022#include <arch/early_variables.h>
Vadim Bendebury32da8be2011-09-29 17:27:15 -070023#include <string.h>
24
25/*
26 * Structure describing console buffer. It is overlaid on a flat memory area,
Martin Rothcbf2bd72013-07-09 21:51:14 -060027 * with buffer_body covering the extent of the memory. Once the buffer is
Vadim Bendebury32da8be2011-09-29 17:27:15 -070028 * 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 */
31struct cbmem_console {
32 u32 buffer_size;
33 u32 buffer_cursor;
34 u8 buffer_body[0];
35} __attribute__ ((__packed__));
36
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050037static struct cbmem_console *cbmem_console_p CAR_GLOBAL;
38
Vadim Bendebury32da8be2011-09-29 17:27:15 -070039#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 Black19e7e7d2011-10-01 04:27:32 -070045
46static struct cbmem_console car_cbmem_console CAR_CBMEM;
Vadim Bendebury32da8be2011-09-29 17:27:15 -070047
Vadim Bendebury32da8be2011-09-29 17:27:15 -070048#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 */
56static u8 static_console[40000];
Vadim Bendebury32da8be2011-09-29 17:27:15 -070057#endif
58
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050059static inline struct cbmem_console *current_console(void)
Vadim Bendebury32da8be2011-09-29 17:27:15 -070060{
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050061 return car_get_var(cbmem_console_p);
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050062}
63
64static inline void current_console_set(struct cbmem_console *new_console_p)
65{
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050066 car_set_var(cbmem_console_p, new_console_p);
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050067}
68
69static inline void init_console_ptr(void *storage, u32 total_space)
70{
71 struct cbmem_console *cbm_cons_p = storage;
72
73 /* Initialize the cache-as-ram pointer and underlying structure. */
74 car_set_var(cbmem_console_p, cbm_cons_p);
75 cbm_cons_p->buffer_size = total_space - sizeof(struct cbmem_console);
76 cbm_cons_p->buffer_cursor = 0;
77}
78
79void cbmemc_init(void)
80{
81#ifdef __PRE_RAM__
82 init_console_ptr(&car_cbmem_console, CONFIG_CONSOLE_CAR_BUFFER_SIZE);
83#else
84 /*
85 * Initializing before CBMEM is available, use static buffer to store
86 * the log.
87 */
88 init_console_ptr(static_console, sizeof(static_console));
89#endif
90}
91
92void cbmemc_tx_byte(unsigned char data)
93{
94 struct cbmem_console *cbm_cons_p = current_console();
95 u32 cursor;
96
Vadim Bendebury32da8be2011-09-29 17:27:15 -070097 if (!cbm_cons_p)
98 return;
99
100 cursor = cbm_cons_p->buffer_cursor++;
101 if (cursor < cbm_cons_p->buffer_size)
102 cbm_cons_p->buffer_body[cursor] = data;
103}
104
105/*
106 * Copy the current console buffer (either from the cache as RAM area, or from
107 * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
108 * buffer space (pointed at by new_cons_p), concatenating the copied data with
109 * the CBMEM console buffer contents.
110 *
111 * If there is overflow - add to the destination area a string, reporting the
Martin Rothcbf2bd72013-07-09 21:51:14 -0600112 * overflow and the number of dropped characters.
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700113 */
114static void copy_console_buffer(struct cbmem_console *new_cons_p)
115{
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200116 u32 copy_size, dropped_chars;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700117 u32 cursor = new_cons_p->buffer_cursor;
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500118 struct cbmem_console *old_cons_p;
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500119
120 old_cons_p = current_console();
121
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200122 if (old_cons_p->buffer_cursor < old_cons_p->buffer_size)
123 copy_size = old_cons_p->buffer_cursor;
124 else
125 copy_size = old_cons_p->buffer_size;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700126
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200127 if (cursor > new_cons_p->buffer_size)
128 copy_size = 0;
129 else if (cursor + copy_size > new_cons_p->buffer_size)
130 copy_size = new_cons_p->buffer_size - cursor;
131
132 dropped_chars = old_cons_p->buffer_cursor - copy_size;
133 if (dropped_chars) {
134 /* Reserve 80 chars to report overflow, if possible. */
135 if (copy_size < 80)
136 return;
137 copy_size -= 80;
138 dropped_chars += 80;
139 }
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700140
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500141 memcpy(new_cons_p->buffer_body + cursor, old_cons_p->buffer_body,
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700142 copy_size);
143
144 cursor += copy_size;
145
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200146 if (dropped_chars) {
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700147 const char loss_str1[] = "\n\n*** Log truncated, ";
148 const char loss_str2[] = " characters dropped. ***\n\n";
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700149
150 /*
151 * When running from ROM sprintf is not available, a simple
152 * itoa implementation is used instead.
153 */
154 int got_first_digit = 0;
155
156 /* Way more than possible number of dropped characters. */
157 u32 mult = 100000;
158
159 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1);
160 cursor += sizeof(loss_str1) - 1;
161
162 while (mult) {
163 int digit = dropped_chars / mult;
164 if (got_first_digit || digit) {
165 new_cons_p->buffer_body[cursor++] = digit + '0';
166 dropped_chars %= mult;
167 /* Excessive, but keeps it simple */
168 got_first_digit = 1;
169 }
170 mult /= 10;
171 }
172
173 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2);
174 cursor += sizeof(loss_str2) - 1;
175 }
176 new_cons_p->buffer_cursor = cursor;
177}
178
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300179void cbmemc_reinit(void)
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700180{
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300181 struct cbmem_console *cbm_cons_p = NULL;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700182
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300183#ifndef __PRE_RAM__
184 cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
185#endif
186
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700187 if (!cbm_cons_p) {
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300188 cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE,
189 CONFIG_CONSOLE_CBMEM_BUFFER_SIZE);
190
191 if (!cbm_cons_p) {
192 current_console_set(NULL);
193 return;
194 }
195
196 cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE -
197 sizeof(struct cbmem_console);
198
199 cbm_cons_p->buffer_cursor = 0;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700200 }
201
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700202 copy_console_buffer(cbm_cons_p);
203
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500204 current_console_set(cbm_cons_p);
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700205}
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500206
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300207/* Call cbmemc_reinit() at CAR migration time. */
208CAR_MIGRATE(cbmemc_reinit)