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