CHROMIUM: Extend Google memory console to work with coreboot.
[cascardo/linux.git] / drivers / firmware / google / memconsole.c
1 /*
2  * memconsole.c
3  *
4  * Infrastructure for importing the BIOS memory based console
5  * into the kernel log ringbuffer.
6  *
7  * Copyright 2010 Google Inc. All rights reserved.
8  */
9
10 #include <linux/ctype.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/sysfs.h>
15 #include <linux/kobject.h>
16 #include <linux/module.h>
17 #include <linux/dmi.h>
18 #include <linux/mm.h>
19 #include <asm/bios_ebda.h>
20 #include <asm/e820.h>
21
22 #define BIOS_MEMCONSOLE_V1_MAGIC        0xDEADBABE
23 #define BIOS_MEMCONSOLE_V2_MAGIC        (('M')|('C'<<8)|('O'<<16)|('N'<<24))
24
25 struct biosmemcon_ebda {
26         u32 signature;
27         union {
28                 struct {
29                         u8  enabled;
30                         u32 buffer_addr;
31                         u16 start;
32                         u16 end;
33                         u16 num_chars;
34                         u8  wrapped;
35                 } __packed v1;
36                 struct {
37                         u32 buffer_addr;
38                         /* Misdocumented as number of pages! */
39                         u16 num_bytes;
40                         u16 start;
41                         u16 end;
42                 } __packed v2;
43         };
44 } __packed;
45
46 static char *memconsole_baseaddr;
47 static size_t memconsole_length;
48 static bool coreboot_system;
49
50 static int set_coreboot_system_flag(const struct dmi_system_id *unused)
51 {
52         coreboot_system = true;
53         return true;
54 }
55
56 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
57                                struct bin_attribute *bin_attr, char *buf,
58                                loff_t pos, size_t count)
59 {
60         return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr,
61                                        memconsole_length);
62 }
63
64 static struct bin_attribute memconsole_bin_attr = {
65         .attr = {.name = "log", .mode = 0444},
66         .read = memconsole_read,
67 };
68
69
70 static void found_v1_header(struct biosmemcon_ebda *hdr)
71 {
72         printk(KERN_INFO "BIOS console v1 EBDA structure found at %p\n", hdr);
73         printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
74                "start = %d, end = %d, num = %d\n",
75                hdr->v1.buffer_addr, hdr->v1.start,
76                hdr->v1.end, hdr->v1.num_chars);
77
78         memconsole_length = hdr->v1.num_chars;
79         memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr);
80 }
81
82 static void found_v2_header(struct biosmemcon_ebda *hdr)
83 {
84         printk(KERN_INFO "BIOS console v2 EBDA structure found at %p\n", hdr);
85         printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
86                "start = %d, end = %d, num_bytes = %d\n",
87                hdr->v2.buffer_addr, hdr->v2.start,
88                hdr->v2.end, hdr->v2.num_bytes);
89
90         memconsole_length = hdr->v2.end - hdr->v2.start;
91         memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr
92                                            + hdr->v2.start);
93 }
94
95 /*
96  * The function below tries to find the firmware console log saved in the
97  * appropriate coreboot CBMEM section, using the structures and constants from
98  * coreboot code.
99  *
100  * Coreboot's CBMEM is a structure allocated by coreboot above usable memory
101  * at a 128K aligned address. One of the CBMEM sections could contain the
102  * firmare console log.
103  */
104
105 /* Coreboot CBMEM section descriptor. */
106 struct cbmem_entry {
107         u32 magic;
108         u32 id;
109         u64 base;
110         u64 size;
111 } __packed;
112
113 /* CBMEM firmware console log descriptor. */
114 struct cbmem_cons {
115         u32 buffer_size;
116         u32 buffer_cursor;
117         u8  buffer_body[0];
118 } __packed;
119
120 struct cbmem_cons __iomem *cbmem_console;
121
122 static bool check_cbmem(void)
123 {
124         struct sysinfo sysi;
125         phys_addr_t top_of_ram, scan_addr;
126
127         /* coreboot CBMEM constants */
128 #define CBMEM_ALIGNMENT   (128 * 1024)
129 #define MEMORY_BOUNDARY   (1024 * 1024 * 1024)
130 #define MAX_CBMEM_ENTRIES 16
131 #define CBMEM_ENTRY_MAGIC 0x434f5245
132 #define CBMEM_CONSOLE_ID  0x434f4e53
133
134         /*
135          * Determine where to start looking for CBMEM signature: take the top
136          * of usable memory and align it up to 128K boundary.
137          */
138         si_meminfo(&sysi);
139         top_of_ram = (phys_addr_t) sysi.totalram << PAGE_SHIFT;
140         scan_addr = ALIGN(top_of_ram, CBMEM_ALIGNMENT) + CBMEM_ALIGNMENT;
141
142         while (scan_addr % MEMORY_BOUNDARY) {
143                 struct cbmem_entry __iomem *pcbm;
144                 int i, remap_size = sizeof(struct cbmem_entry) * 16;
145
146                 /*
147                  * See if we reached reserved memory. Bail out if so, as it is
148                  * not mappable and is above the region where the CBMEM could
149                  * be.
150                  */
151                 if (e820_any_mapped(scan_addr,
152                                     scan_addr + remap_size,
153                                     E820_RESERVED))
154                         break;
155
156                 pcbm = ioremap(scan_addr, remap_size);
157                 if (pcbm->magic != CBMEM_ENTRY_MAGIC) {
158                         iounmap(pcbm);
159                         scan_addr += CBMEM_ALIGNMENT;
160                         continue;
161                 }
162
163                 /* CBMEM found. Is the console log there? */
164                 for (i = 1; i < MAX_CBMEM_ENTRIES; i++) {
165                         if ((pcbm[i].magic == CBMEM_ENTRY_MAGIC) &&
166                             (pcbm[i].id == CBMEM_CONSOLE_ID)) {
167                                 /* Yes, map the log. */
168                                 cbmem_console = ioremap(pcbm[i].base,
169                                                         pcbm[i].size);
170                                 break;
171                         }
172                 }
173                 iounmap(pcbm);
174                 break;
175         }
176
177         if (cbmem_console) {
178                 memconsole_baseaddr = cbmem_console->buffer_body;
179                 memconsole_length = min(cbmem_console->buffer_cursor,
180                                         cbmem_console->buffer_size);
181                 return true;
182         }
183
184         printk(KERN_INFO "CBMEM console structure not found!\n");
185         return false;
186 }
187
188 /*
189  * Search through the EBDA for the BIOS Memory Console, and
190  * set the global variables to point to it.  Return true if found.
191  */
192 static bool found_memconsole(void)
193 {
194         unsigned int address;
195         size_t length, cur;
196
197         /* Is it communicated through CBMEM? */
198         if (coreboot_system && check_cbmem())
199                 return true;
200
201         address = get_bios_ebda();
202         if (!address) {
203                 printk(KERN_INFO "BIOS EBDA non-existent.\n");
204                 return false;
205         }
206
207         /* EBDA length is byte 0 of EBDA (in KB) */
208         length = *(u8 *)phys_to_virt(address);
209         length <<= 10; /* convert to bytes */
210
211         /*
212          * Search through EBDA for BIOS memory console structure
213          * note: signature is not necessarily dword-aligned
214          */
215         for (cur = 0; cur < length; cur++) {
216                 struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
217
218                 /* memconsole v1 */
219                 if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
220                         found_v1_header(hdr);
221                         return true;
222                 }
223
224                 /* memconsole v2 */
225                 if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
226                         found_v2_header(hdr);
227                         return true;
228                 }
229         }
230
231         printk(KERN_INFO "BIOS console EBDA structure not found!\n");
232         return false;
233 }
234
235 static struct dmi_system_id memconsole_dmi_table[] __initdata = {
236         {
237                 .ident = "Google Board",
238                 .matches = {
239                         DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
240                 },
241         },
242         {
243                 .ident = "Google Board",
244                 .callback = set_coreboot_system_flag,
245                 .matches = {
246                         DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
247                 },
248         },
249         {}
250 };
251 MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
252
253 static int __init memconsole_init(void)
254 {
255         int ret;
256
257         if (!dmi_check_system(memconsole_dmi_table))
258                 return -ENODEV;
259
260         if (!found_memconsole())
261                 return -ENODEV;
262
263         memconsole_bin_attr.size = memconsole_length;
264
265         ret = sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr);
266
267         return ret;
268 }
269
270 static void __exit memconsole_exit(void)
271 {
272         if (cbmem_console)
273                 iounmap(cbmem_console);
274
275         sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
276 }
277
278 module_init(memconsole_init);
279 module_exit(memconsole_exit);
280
281 MODULE_AUTHOR("Google, Inc.");
282 MODULE_LICENSE("GPL");