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