google memconsole: use ioremap_cache()
[cascardo/linux.git] / drivers / firmware / google / memconsole.c
index 2a90ba6..baa327b 100644 (file)
 #include <linux/kobject.h>
 #include <linux/module.h>
 #include <linux/dmi.h>
+#include <linux/mm.h>
 #include <asm/bios_ebda.h>
+#include <asm/e820.h>
+#include <acpi/acpi_bus.h>
 
 #define BIOS_MEMCONSOLE_V1_MAGIC       0xDEADBABE
 #define BIOS_MEMCONSOLE_V2_MAGIC       (('M')|('C'<<8)|('O'<<16)|('N'<<24))
@@ -43,6 +46,13 @@ struct biosmemcon_ebda {
 
 static char *memconsole_baseaddr;
 static size_t memconsole_length;
+static bool coreboot_system;
+
+static int set_coreboot_system_flag(const struct dmi_system_id *unused)
+{
+       coreboot_system = true;
+       return true;
+}
 
 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
                               struct bin_attribute *bin_attr, char *buf,
@@ -83,6 +93,123 @@ static void found_v2_header(struct biosmemcon_ebda *hdr)
                                           + hdr->v2.start);
 }
 
+/*
+ * The function below tries to find the firmware console log saved in the
+ * appropriate coreboot CBMEM section, using the structures and constants from
+ * coreboot code.
+ *
+ * Coreboot's CBMEM is a structure allocated by coreboot above usable memory
+ * at a 128K aligned address. One of the CBMEM sections could contain the
+ * firmare console log.
+ */
+
+/* Coreboot CBMEM section descriptor. */
+struct cbmem_entry {
+       u32 magic;
+       u32 id;
+       u64 base;
+       u64 size;
+} __packed;
+
+/* CBMEM firmware console log descriptor. */
+struct cbmem_cons {
+       u32 buffer_size;
+       u32 buffer_cursor;
+       u8  buffer_body[0];
+} __packed;
+
+struct cbmem_cons __iomem *cbmem_console;
+
+/* coreboot CBMEM constants */
+#define CBMEM_ALIGNMENT   (128 * 1024)
+#define MEMORY_BOUNDARY   (1024 * 1024 * 1024)
+#define MAX_CBMEM_ENTRIES 16
+#define CBMEM_ENTRY_MAGIC 0x434f5245
+#define CBMEM_CONSOLE_ID  0x434f4e53
+#define CBMEM_ACPI_NAME   "\\CMEM"
+
+static bool check_cbmem(void)
+{
+       struct sysinfo sysi;
+       phys_addr_t top_of_ram, scan_addr = 0;
+       acpi_handle handle;
+       acpi_status status;
+       unsigned long long value;
+
+       /*
+        * Attempt to use defined ACPI name to locate CBMEM TOC.
+        */
+       status = acpi_get_handle(NULL, CBMEM_ACPI_NAME, &handle);
+       if (ACPI_SUCCESS(status)) {
+               status = acpi_evaluate_integer(handle, CBMEM_ACPI_NAME,
+                                              NULL, &value);
+               /* Start scan at this address */
+               if (ACPI_SUCCESS(status) && value > 0)
+                       scan_addr = (phys_addr_t) value;
+       }
+
+       /*
+        * Otherwise determine where to start looking for CBMEM signature:
+        * take the top of usable memory and align it up to 128K boundary.
+        */
+       if (!scan_addr) {
+               si_meminfo(&sysi);
+               top_of_ram = (phys_addr_t) sysi.totalram << PAGE_SHIFT;
+               scan_addr = ALIGN(top_of_ram, CBMEM_ALIGNMENT) +
+                       CBMEM_ALIGNMENT;
+       }
+
+       while (scan_addr % MEMORY_BOUNDARY) {
+               struct cbmem_entry __iomem *pcbm;
+               int i, remap_size = sizeof(struct cbmem_entry) * 16;
+
+               /*
+                * See if we reached reserved memory. Bail out if so, as it is
+                * not mappable and is above the region where the CBMEM could
+                * be.
+                */
+               if (e820_any_mapped(scan_addr,
+                                   scan_addr + remap_size,
+                                   E820_RESERVED))
+                       break;
+
+               pcbm = ioremap_cache(scan_addr, remap_size);
+               if (!pcbm) {
+                       scan_addr += CBMEM_ALIGNMENT;
+                       continue;
+               }
+
+               if (pcbm->magic != CBMEM_ENTRY_MAGIC) {
+                       iounmap(pcbm);
+                       scan_addr += CBMEM_ALIGNMENT;
+                       continue;
+               }
+
+               /* CBMEM found. Is the console log there? */
+               for (i = 1; i < MAX_CBMEM_ENTRIES; i++) {
+                       if ((pcbm[i].magic == CBMEM_ENTRY_MAGIC) &&
+                           (pcbm[i].id == CBMEM_CONSOLE_ID)) {
+                               /* Yes, map the log. */
+                               cbmem_console = ioremap_cache(pcbm[i].base,
+                                                       pcbm[i].size);
+                               break;
+                       }
+               }
+               iounmap(pcbm);
+               break;
+       }
+
+       if (cbmem_console) {
+               memconsole_baseaddr = cbmem_console->buffer_body;
+               memconsole_length = min(cbmem_console->buffer_cursor,
+                                       cbmem_console->buffer_size);
+               return true;
+       }
+
+       printk(KERN_INFO "CBMEM console structure not found!\n");
+       return false;
+}
+
 /*
  * Search through the EBDA for the BIOS Memory Console, and
  * set the global variables to point to it.  Return true if found.
@@ -92,6 +219,10 @@ static bool found_memconsole(void)
        unsigned int address;
        size_t length, cur;
 
+       /* Is it communicated through CBMEM? */
+       if (coreboot_system && check_cbmem())
+               return true;
+
        address = get_bios_ebda();
        if (!address) {
                printk(KERN_INFO "BIOS EBDA non-existent.\n");
@@ -133,6 +264,13 @@ static struct dmi_system_id memconsole_dmi_table[] __initdata = {
                        DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
                },
        },
+       {
+               .ident = "Google Board",
+               .callback = set_coreboot_system_flag,
+               .matches = {
+                       DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
+               },
+       },
        {}
 };
 MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
@@ -156,6 +294,9 @@ static int __init memconsole_init(void)
 
 static void __exit memconsole_exit(void)
 {
+       if (cbmem_console)
+               iounmap(cbmem_console);
+
        sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
 }