blob: de452d781dc293b649a218dd774e40c064985711 [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>
Gabe Black19e7e7d2011-10-01 04:27:32 -070022#include <cpu/x86/car.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
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 Durbin2ad6bd22013-05-10 00:45:37 -050058
Vadim Bendebury32da8be2011-09-29 17:27:15 -070059#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 */
67static u8 static_console[40000];
Vadim Bendebury32da8be2011-09-29 17:27:15 -070068#endif
69
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050070static inline struct cbmem_console *current_console(void)
Vadim Bendebury32da8be2011-09-29 17:27:15 -070071{
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050072#if CONFIG_CAR_MIGRATION || !defined(__PRE_RAM__)
73 return car_get_var(cbmem_console_p);
Vadim Bendebury32da8be2011-09-29 17:27:15 -070074#else
75 /*
Vadim Bendebury32da8be2011-09-29 17:27:15 -070076 * 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 Durbin2ad6bd22013-05-10 00:45:37 -050082 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
89static 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 Bendebury32da8be2011-09-29 17:27:15 -070095#endif
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050096}
97
98static 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
108void 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
121void cbmemc_tx_byte(unsigned char data)
122{
123 struct cbmem_console *cbm_cons_p = current_console();
124 u32 cursor;
125
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700126 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 Rothcbf2bd72013-07-09 21:51:14 -0600141 * overflow and the number of dropped characters.
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700142 */
143static void copy_console_buffer(struct cbmem_console *new_cons_p)
144{
145 u32 copy_size;
146 u32 cursor = new_cons_p->buffer_cursor;
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500147 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 Bendebury32da8be2011-09-29 17:27:15 -0700153
154 copy_size = overflow ?
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500155 old_cons_p->buffer_size : old_cons_p->buffer_cursor;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700156
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500157 memcpy(new_cons_p->buffer_body + cursor, old_cons_p->buffer_body,
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700158 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 Durbin2ad6bd22013-05-10 00:45:37 -0500165 u32 dropped_chars = old_cons_p->buffer_cursor - copy_size;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700166
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
196void cbmemc_reinit(void)
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 Durbin2ad6bd22013-05-10 00:45:37 -0500204 current_console_set(NULL);
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700205 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 Bendebury32da8be2011-09-29 17:27:15 -0700212#else
213 cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
214
215 if (!cbm_cons_p)
216 return;
217
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500218#endif
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700219 copy_console_buffer(cbm_cons_p);
220
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500221 current_console_set(cbm_cons_p);
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700222}
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500223
224/* Call cbmemc_reinit() at CAR migration time. */
225CAR_MIGRATE(cbmemc_reinit)