Add infrastructure for global data in the CAR phase of boot
[coreboot.git] / src / lib / cbmem_console.c
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>
22 #include <cpu/x86/car.h>
23 #include <string.h>
24
25 /*
26  * Structure describing console buffer. It is overlaid on a flat memory area,
27  * whith buffer_body covering the extent of the memory. Once the buffer is
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
37 #ifdef __PRE_RAM__
38 /*
39  * While running from ROM, before DRAM is initialized, some area in cache as
40  * ram space is used for the console buffer storage. The size and location of
41  * the area are defined in the config.
42  */
43
44 static struct cbmem_console car_cbmem_console CAR_CBMEM;
45 #define cbmem_console_p (&car_cbmem_console)
46
47 /*
48  * Once DRAM is initialized and the cache as ram mode is disabled, while still
49  * running from ROM, the console buffer in the cache as RAM area becomes
50  * unavailable.
51  *
52  * By this time the console log buffer is already available in
53  * CBMEM. The location at 0x600 is used as the redirect pointer allowing to
54  * find out where the actual console log buffer is.
55  */
56 #define CBMEM_CONSOLE_REDIRECT (*((struct cbmem_console **)0x600))
57 #else
58
59 /*
60  * When running from RAM, a lot of console output is generated before CBMEM is
61  * reinitialized. This static buffer is used to store that output temporarily,
62  * to be concatenated with the CBMEM console buffer contents accumulated
63  * during the ROM stage, once CBMEM becomes available at RAM stage.
64  */
65 static u8 static_console[40000];
66 static struct cbmem_console *cbmem_console_p;
67 #endif
68
69 void cbmemc_init(void)
70 {
71 #ifdef __PRE_RAM__
72         cbmem_console_p->buffer_size = CONFIG_CONSOLE_CAR_BUFFER_SIZE -
73                 sizeof(struct cbmem_console);
74 #else
75         /*
76          * Initializing before CBMEM is available, use static buffer to store
77          * the log.
78          */
79         cbmem_console_p = (struct cbmem_console *) static_console;
80         cbmem_console_p->buffer_size = sizeof(static_console) -
81                 sizeof(struct cbmem_console);
82 #endif
83         cbmem_console_p->buffer_cursor = 0;
84 }
85
86 void cbmemc_tx_byte(unsigned char data)
87 {
88         struct cbmem_console *cbm_cons_p = cbmem_console_p;
89         u32 cursor;
90 #ifdef __PRE_RAM__
91         /*
92          * This check allows to tell if the cache as RAM mode has been exited
93          * or not. If it has been exited, the real memory is being used
94          * (resulting in the variable on the stack located below
95          * DCACHE_RAM_BASE), use the redirect pointer to find out where the
96          * actual console buffer is.
97          */
98         if ((uintptr_t)&cursor < (uintptr_t)&car_cbmem_console)
99                 cbm_cons_p = CBMEM_CONSOLE_REDIRECT;
100 #endif
101         if (!cbm_cons_p)
102                 return;
103
104         cursor = cbm_cons_p->buffer_cursor++;
105         if (cursor < cbm_cons_p->buffer_size)
106                 cbm_cons_p->buffer_body[cursor] = data;
107 }
108
109 /*
110  * Copy the current console buffer (either from the cache as RAM area, or from
111  * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
112  * buffer space (pointed at by new_cons_p), concatenating the copied data with
113  * the CBMEM console buffer contents.
114  *
115  * If there is overflow - add to the destination area a string, reporting the
116  * overflow and the number of dropped charactes.
117  */
118 static void copy_console_buffer(struct cbmem_console *new_cons_p)
119 {
120         u32 copy_size;
121         u32 cursor = new_cons_p->buffer_cursor;
122         int overflow = cbmem_console_p->buffer_cursor >
123                 cbmem_console_p->buffer_size;
124
125         copy_size = overflow ?
126                 cbmem_console_p->buffer_size : cbmem_console_p->buffer_cursor;
127
128         memcpy(new_cons_p->buffer_body + cursor,
129                cbmem_console_p->buffer_body,
130                copy_size);
131
132         cursor += copy_size;
133
134         if (overflow) {
135                 const char loss_str1[] = "\n\n*** Log truncated, ";
136                 const char loss_str2[] = " characters dropped. ***\n\n";
137                 u32 dropped_chars = cbmem_console_p->buffer_cursor - copy_size;
138
139                 /*
140                  * When running from ROM sprintf is not available, a simple
141                  * itoa implementation is used instead.
142                  */
143                 int got_first_digit = 0;
144
145                 /* Way more than possible number of dropped characters. */
146                 u32 mult = 100000;
147
148                 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1);
149                 cursor += sizeof(loss_str1) - 1;
150
151                 while (mult) {
152                         int digit = dropped_chars / mult;
153                         if (got_first_digit || digit) {
154                                 new_cons_p->buffer_body[cursor++] = digit + '0';
155                                 dropped_chars %= mult;
156                                 /* Excessive, but keeps it simple */
157                                 got_first_digit = 1;
158                         }
159                         mult /= 10;
160                 }
161
162                 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2);
163                 cursor += sizeof(loss_str2) - 1;
164         }
165         new_cons_p->buffer_cursor = cursor;
166 }
167
168 void cbmemc_reinit(void)
169 {
170         struct cbmem_console *cbm_cons_p;
171
172 #ifdef __PRE_RAM__
173         cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE,
174                                CONFIG_CONSOLE_CBMEM_BUFFER_SIZE);
175         if (!cbm_cons_p) {
176                 CBMEM_CONSOLE_REDIRECT = NULL;
177                 return;
178         }
179
180         cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE -
181                 sizeof(struct cbmem_console);
182
183         cbm_cons_p->buffer_cursor = 0;
184
185         copy_console_buffer(cbm_cons_p);
186
187         CBMEM_CONSOLE_REDIRECT = cbm_cons_p;
188 #else
189         cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
190
191         if (!cbm_cons_p)
192                 return;
193
194         copy_console_buffer(cbm_cons_p);
195
196         cbmem_console_p = cbm_cons_p;
197 #endif
198 }