blob: 997bb7d9b21b3b5c64d79fd0c8fb4dbde3421aee [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>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020021#include <console/cbmem_console.h>
Vadim Bendebury32da8be2011-09-29 17:27:15 -070022#include <cbmem.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070023#include <arch/early_variables.h>
Vadim Bendebury32da8be2011-09-29 17:27:15 -070024#include <string.h>
25
26/*
27 * Structure describing console buffer. It is overlaid on a flat memory area,
Martin Rothcbf2bd72013-07-09 21:51:14 -060028 * with buffer_body covering the extent of the memory. Once the buffer is
Vadim Bendebury32da8be2011-09-29 17:27:15 -070029 * full, the cursor keeps going but the data is dropped on the floor. This
30 * allows to tell how much data was lost in the process.
31 */
32struct cbmem_console {
33 u32 buffer_size;
34 u32 buffer_cursor;
35 u8 buffer_body[0];
36} __attribute__ ((__packed__));
37
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050038static struct cbmem_console *cbmem_console_p CAR_GLOBAL;
39
Vadim Bendebury32da8be2011-09-29 17:27:15 -070040#ifdef __PRE_RAM__
41/*
42 * While running from ROM, before DRAM is initialized, some area in cache as
43 * ram space is used for the console buffer storage. The size and location of
44 * the area are defined in the config.
45 */
Gabe Black19e7e7d2011-10-01 04:27:32 -070046
Gabe Black35382a62014-04-02 17:57:05 -070047extern struct cbmem_console preram_cbmem_console;
Vadim Bendebury32da8be2011-09-29 17:27:15 -070048
Vadim Bendebury32da8be2011-09-29 17:27:15 -070049#else
50
51/*
52 * When running from RAM, a lot of console output is generated before CBMEM is
53 * reinitialized. This static buffer is used to store that output temporarily,
54 * to be concatenated with the CBMEM console buffer contents accumulated
55 * during the ROM stage, once CBMEM becomes available at RAM stage.
56 */
Stefan Reinauer19d06b22013-09-09 11:22:18 -070057
58#if CONFIG_DYNAMIC_CBMEM
59#define STATIC_CONSOLE_SIZE 1024
60#else
Kyösti Mälkki44c0e4e2013-11-27 22:54:11 +020061#define STATIC_CONSOLE_SIZE CONFIG_CONSOLE_CBMEM_BUFFER_SIZE
Stefan Reinauer19d06b22013-09-09 11:22:18 -070062#endif
63static u8 static_console[STATIC_CONSOLE_SIZE];
Vadim Bendebury32da8be2011-09-29 17:27:15 -070064#endif
65
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050066static inline struct cbmem_console *current_console(void)
Vadim Bendebury32da8be2011-09-29 17:27:15 -070067{
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050068 return car_get_var(cbmem_console_p);
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050069}
70
71static inline void current_console_set(struct cbmem_console *new_console_p)
72{
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050073 car_set_var(cbmem_console_p, new_console_p);
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050074}
75
76static inline void init_console_ptr(void *storage, u32 total_space)
77{
78 struct cbmem_console *cbm_cons_p = storage;
79
80 /* Initialize the cache-as-ram pointer and underlying structure. */
81 car_set_var(cbmem_console_p, cbm_cons_p);
82 cbm_cons_p->buffer_size = total_space - sizeof(struct cbmem_console);
83 cbm_cons_p->buffer_cursor = 0;
84}
85
86void cbmemc_init(void)
87{
88#ifdef __PRE_RAM__
Gabe Black35382a62014-04-02 17:57:05 -070089 init_console_ptr(&preram_cbmem_console,
90 CONFIG_CONSOLE_PRERAM_BUFFER_SIZE);
Aaron Durbin2ad6bd22013-05-10 00:45:37 -050091#else
92 /*
93 * Initializing before CBMEM is available, use static buffer to store
94 * the log.
95 */
96 init_console_ptr(static_console, sizeof(static_console));
97#endif
98}
99
100void cbmemc_tx_byte(unsigned char data)
101{
102 struct cbmem_console *cbm_cons_p = current_console();
103 u32 cursor;
104
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700105 if (!cbm_cons_p)
106 return;
107
108 cursor = cbm_cons_p->buffer_cursor++;
109 if (cursor < cbm_cons_p->buffer_size)
110 cbm_cons_p->buffer_body[cursor] = data;
111}
112
113/*
114 * Copy the current console buffer (either from the cache as RAM area, or from
115 * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
116 * buffer space (pointed at by new_cons_p), concatenating the copied data with
117 * the CBMEM console buffer contents.
118 *
119 * If there is overflow - add to the destination area a string, reporting the
Martin Rothcbf2bd72013-07-09 21:51:14 -0600120 * overflow and the number of dropped characters.
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700121 */
122static void copy_console_buffer(struct cbmem_console *new_cons_p)
123{
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200124 u32 copy_size, dropped_chars;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700125 u32 cursor = new_cons_p->buffer_cursor;
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500126 struct cbmem_console *old_cons_p;
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500127
128 old_cons_p = current_console();
129
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200130 if (old_cons_p->buffer_cursor < old_cons_p->buffer_size)
131 copy_size = old_cons_p->buffer_cursor;
132 else
133 copy_size = old_cons_p->buffer_size;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700134
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200135 if (cursor > new_cons_p->buffer_size)
136 copy_size = 0;
137 else if (cursor + copy_size > new_cons_p->buffer_size)
138 copy_size = new_cons_p->buffer_size - cursor;
139
140 dropped_chars = old_cons_p->buffer_cursor - copy_size;
141 if (dropped_chars) {
142 /* Reserve 80 chars to report overflow, if possible. */
143 if (copy_size < 80)
144 return;
145 copy_size -= 80;
146 dropped_chars += 80;
147 }
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700148
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500149 memcpy(new_cons_p->buffer_body + cursor, old_cons_p->buffer_body,
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700150 copy_size);
151
152 cursor += copy_size;
153
Kyösti Mälkkia3c5ba32013-11-27 17:51:31 +0200154 if (dropped_chars) {
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700155 const char loss_str1[] = "\n\n*** Log truncated, ";
156 const char loss_str2[] = " characters dropped. ***\n\n";
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700157
158 /*
159 * When running from ROM sprintf is not available, a simple
160 * itoa implementation is used instead.
161 */
162 int got_first_digit = 0;
163
164 /* Way more than possible number of dropped characters. */
165 u32 mult = 100000;
166
167 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1);
168 cursor += sizeof(loss_str1) - 1;
169
170 while (mult) {
171 int digit = dropped_chars / mult;
172 if (got_first_digit || digit) {
173 new_cons_p->buffer_body[cursor++] = digit + '0';
174 dropped_chars %= mult;
175 /* Excessive, but keeps it simple */
176 got_first_digit = 1;
177 }
178 mult /= 10;
179 }
180
181 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2);
182 cursor += sizeof(loss_str2) - 1;
183 }
184 new_cons_p->buffer_cursor = cursor;
185}
186
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300187void cbmemc_reinit(void)
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700188{
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300189 struct cbmem_console *cbm_cons_p = NULL;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700190
Kyösti Mälkki71b21452014-11-28 10:13:03 +0200191#ifdef __PRE_RAM__
192 if (IS_ENABLED(CONFIG_BROKEN_CAR_MIGRATE))
193 return;
194#endif
195
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300196#ifndef __PRE_RAM__
197 cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
198#endif
199
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700200 if (!cbm_cons_p) {
Kyösti Mälkki52a27222013-09-09 01:31:22 +0300201 cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE,
202 CONFIG_CONSOLE_CBMEM_BUFFER_SIZE);
203
204 if (!cbm_cons_p) {
205 current_console_set(NULL);
206 return;
207 }
208
209 cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE -
210 sizeof(struct cbmem_console);
211
212 cbm_cons_p->buffer_cursor = 0;
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700213 }
214
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700215 copy_console_buffer(cbm_cons_p);
216
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500217 current_console_set(cbm_cons_p);
Vadim Bendebury32da8be2011-09-29 17:27:15 -0700218}
Aaron Durbin2ad6bd22013-05-10 00:45:37 -0500219
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300220/* Call cbmemc_reinit() at CAR migration time. */
221CAR_MIGRATE(cbmemc_reinit)