1d9523678SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d384d6f4SThierry Escande /* 3d384d6f4SThierry Escande * coreboot_table.c 4d384d6f4SThierry Escande * 5d384d6f4SThierry Escande * Module providing coreboot table access. 6d384d6f4SThierry Escande * 7d384d6f4SThierry Escande * Copyright 2017 Google Inc. 8570d30c2SSamuel Holland * Copyright 2017 Samuel Holland <samuel@sholland.org> 9d384d6f4SThierry Escande */ 10d384d6f4SThierry Escande 11a28aad66SStephen Boyd #include <linux/acpi.h> 12570d30c2SSamuel Holland #include <linux/device.h> 13d384d6f4SThierry Escande #include <linux/err.h> 14d384d6f4SThierry Escande #include <linux/init.h> 15d384d6f4SThierry Escande #include <linux/io.h> 16d384d6f4SThierry Escande #include <linux/kernel.h> 17d384d6f4SThierry Escande #include <linux/module.h> 18a28aad66SStephen Boyd #include <linux/of.h> 19a28aad66SStephen Boyd #include <linux/platform_device.h> 20570d30c2SSamuel Holland #include <linux/slab.h> 21d384d6f4SThierry Escande 22d384d6f4SThierry Escande #include "coreboot_table.h" 23d384d6f4SThierry Escande 24570d30c2SSamuel Holland #define CB_DEV(d) container_of(d, struct coreboot_device, dev) 25570d30c2SSamuel Holland #define CB_DRV(d) container_of(d, struct coreboot_driver, drv) 26d384d6f4SThierry Escande 27570d30c2SSamuel Holland static int coreboot_bus_match(struct device *dev, struct device_driver *drv) 28570d30c2SSamuel Holland { 29570d30c2SSamuel Holland struct coreboot_device *device = CB_DEV(dev); 30570d30c2SSamuel Holland struct coreboot_driver *driver = CB_DRV(drv); 31570d30c2SSamuel Holland 32570d30c2SSamuel Holland return device->entry.tag == driver->tag; 33570d30c2SSamuel Holland } 34570d30c2SSamuel Holland 35570d30c2SSamuel Holland static int coreboot_bus_probe(struct device *dev) 36570d30c2SSamuel Holland { 37570d30c2SSamuel Holland int ret = -ENODEV; 38570d30c2SSamuel Holland struct coreboot_device *device = CB_DEV(dev); 39570d30c2SSamuel Holland struct coreboot_driver *driver = CB_DRV(dev->driver); 40570d30c2SSamuel Holland 41570d30c2SSamuel Holland if (driver->probe) 42570d30c2SSamuel Holland ret = driver->probe(device); 43570d30c2SSamuel Holland 44570d30c2SSamuel Holland return ret; 45570d30c2SSamuel Holland } 46570d30c2SSamuel Holland 47fc7a6209SUwe Kleine-König static void coreboot_bus_remove(struct device *dev) 48570d30c2SSamuel Holland { 49570d30c2SSamuel Holland struct coreboot_device *device = CB_DEV(dev); 50570d30c2SSamuel Holland struct coreboot_driver *driver = CB_DRV(dev->driver); 51570d30c2SSamuel Holland 52570d30c2SSamuel Holland if (driver->remove) 535f680532SUwe Kleine-König driver->remove(device); 54570d30c2SSamuel Holland } 55570d30c2SSamuel Holland 56570d30c2SSamuel Holland static struct bus_type coreboot_bus_type = { 57570d30c2SSamuel Holland .name = "coreboot", 58570d30c2SSamuel Holland .match = coreboot_bus_match, 59570d30c2SSamuel Holland .probe = coreboot_bus_probe, 60570d30c2SSamuel Holland .remove = coreboot_bus_remove, 61570d30c2SSamuel Holland }; 62570d30c2SSamuel Holland 63570d30c2SSamuel Holland static void coreboot_device_release(struct device *dev) 64570d30c2SSamuel Holland { 65570d30c2SSamuel Holland struct coreboot_device *device = CB_DEV(dev); 66570d30c2SSamuel Holland 67570d30c2SSamuel Holland kfree(device); 68570d30c2SSamuel Holland } 69570d30c2SSamuel Holland 70570d30c2SSamuel Holland int coreboot_driver_register(struct coreboot_driver *driver) 71570d30c2SSamuel Holland { 72570d30c2SSamuel Holland driver->drv.bus = &coreboot_bus_type; 73570d30c2SSamuel Holland 74570d30c2SSamuel Holland return driver_register(&driver->drv); 75570d30c2SSamuel Holland } 76570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_register); 77570d30c2SSamuel Holland 78570d30c2SSamuel Holland void coreboot_driver_unregister(struct coreboot_driver *driver) 79570d30c2SSamuel Holland { 80570d30c2SSamuel Holland driver_unregister(&driver->drv); 81570d30c2SSamuel Holland } 82570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_unregister); 83570d30c2SSamuel Holland 847adb05bbSStephen Boyd static int coreboot_table_populate(struct device *dev, void *ptr) 85d384d6f4SThierry Escande { 86570d30c2SSamuel Holland int i, ret; 87570d30c2SSamuel Holland void *ptr_entry; 88570d30c2SSamuel Holland struct coreboot_device *device; 89a7d9b5f0SStephen Boyd struct coreboot_table_entry *entry; 907adb05bbSStephen Boyd struct coreboot_table_header *header = ptr; 91d384d6f4SThierry Escande 927adb05bbSStephen Boyd ptr_entry = ptr + header->header_bytes; 93a7d9b5f0SStephen Boyd for (i = 0; i < header->table_entries; i++) { 94a7d9b5f0SStephen Boyd entry = ptr_entry; 95570d30c2SSamuel Holland 96a7d9b5f0SStephen Boyd device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL); 977adb05bbSStephen Boyd if (!device) 987adb05bbSStephen Boyd return -ENOMEM; 99570d30c2SSamuel Holland 100570d30c2SSamuel Holland device->dev.parent = dev; 101570d30c2SSamuel Holland device->dev.bus = &coreboot_bus_type; 102570d30c2SSamuel Holland device->dev.release = coreboot_device_release; 103a7d9b5f0SStephen Boyd memcpy(&device->entry, ptr_entry, entry->size); 104570d30c2SSamuel Holland 105*19d54020SJack Rosenthal switch (device->entry.tag) { 106*19d54020SJack Rosenthal case LB_TAG_CBMEM_ENTRY: 107*19d54020SJack Rosenthal dev_set_name(&device->dev, "cbmem-%08x", 108*19d54020SJack Rosenthal device->cbmem_entry.id); 109*19d54020SJack Rosenthal break; 110*19d54020SJack Rosenthal default: 111*19d54020SJack Rosenthal dev_set_name(&device->dev, "coreboot%d", i); 112*19d54020SJack Rosenthal break; 113*19d54020SJack Rosenthal } 114*19d54020SJack Rosenthal 115570d30c2SSamuel Holland ret = device_register(&device->dev); 116570d30c2SSamuel Holland if (ret) { 117570d30c2SSamuel Holland put_device(&device->dev); 1187adb05bbSStephen Boyd return ret; 119570d30c2SSamuel Holland } 120570d30c2SSamuel Holland 121a7d9b5f0SStephen Boyd ptr_entry += entry->size; 122570d30c2SSamuel Holland } 123b81e3140SStephen Boyd 1247adb05bbSStephen Boyd return 0; 125d384d6f4SThierry Escande } 126d384d6f4SThierry Escande 127a28aad66SStephen Boyd static int coreboot_table_probe(struct platform_device *pdev) 128a28aad66SStephen Boyd { 129a28aad66SStephen Boyd resource_size_t len; 130a7d9b5f0SStephen Boyd struct coreboot_table_header *header; 131a28aad66SStephen Boyd struct resource *res; 1327adb05bbSStephen Boyd struct device *dev = &pdev->dev; 133a7d9b5f0SStephen Boyd void *ptr; 1347adb05bbSStephen Boyd int ret; 135a28aad66SStephen Boyd 136a28aad66SStephen Boyd res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 137a28aad66SStephen Boyd if (!res) 138a28aad66SStephen Boyd return -EINVAL; 139a28aad66SStephen Boyd 140a28aad66SStephen Boyd len = resource_size(res); 141a28aad66SStephen Boyd if (!res->start || !len) 142a28aad66SStephen Boyd return -EINVAL; 143a28aad66SStephen Boyd 1447adb05bbSStephen Boyd /* Check just the header first to make sure things are sane */ 145a7d9b5f0SStephen Boyd header = memremap(res->start, sizeof(*header), MEMREMAP_WB); 1467adb05bbSStephen Boyd if (!header) 147a28aad66SStephen Boyd return -ENOMEM; 148a28aad66SStephen Boyd 1497adb05bbSStephen Boyd len = header->header_bytes + header->table_bytes; 1507adb05bbSStephen Boyd ret = strncmp(header->signature, "LBIO", sizeof(header->signature)); 151a7d9b5f0SStephen Boyd memunmap(header); 1527adb05bbSStephen Boyd if (ret) { 1537adb05bbSStephen Boyd dev_warn(dev, "coreboot table missing or corrupt!\n"); 1547adb05bbSStephen Boyd return -ENODEV; 1557adb05bbSStephen Boyd } 1567adb05bbSStephen Boyd 1577adb05bbSStephen Boyd ptr = memremap(res->start, len, MEMREMAP_WB); 158a28aad66SStephen Boyd if (!ptr) 159a28aad66SStephen Boyd return -ENOMEM; 160a28aad66SStephen Boyd 1617adb05bbSStephen Boyd ret = bus_register(&coreboot_bus_type); 1627adb05bbSStephen Boyd if (!ret) { 1637adb05bbSStephen Boyd ret = coreboot_table_populate(dev, ptr); 1647adb05bbSStephen Boyd if (ret) 1657adb05bbSStephen Boyd bus_unregister(&coreboot_bus_type); 1667adb05bbSStephen Boyd } 1677adb05bbSStephen Boyd memunmap(ptr); 1687adb05bbSStephen Boyd 1697adb05bbSStephen Boyd return ret; 170a28aad66SStephen Boyd } 171a28aad66SStephen Boyd 172cae0970eSPatrick Rudolph static int __cb_dev_unregister(struct device *dev, void *dummy) 173cae0970eSPatrick Rudolph { 174cae0970eSPatrick Rudolph device_unregister(dev); 175cae0970eSPatrick Rudolph return 0; 176cae0970eSPatrick Rudolph } 177cae0970eSPatrick Rudolph 178a28aad66SStephen Boyd static int coreboot_table_remove(struct platform_device *pdev) 179d384d6f4SThierry Escande { 180cae0970eSPatrick Rudolph bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister); 181570d30c2SSamuel Holland bus_unregister(&coreboot_bus_type); 182d384d6f4SThierry Escande return 0; 183d384d6f4SThierry Escande } 184d384d6f4SThierry Escande 185a28aad66SStephen Boyd #ifdef CONFIG_ACPI 186a28aad66SStephen Boyd static const struct acpi_device_id cros_coreboot_acpi_match[] = { 187a28aad66SStephen Boyd { "GOOGCB00", 0 }, 188a28aad66SStephen Boyd { "BOOT0000", 0 }, 189a28aad66SStephen Boyd { } 190a28aad66SStephen Boyd }; 191a28aad66SStephen Boyd MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match); 192a28aad66SStephen Boyd #endif 193a28aad66SStephen Boyd 194a28aad66SStephen Boyd #ifdef CONFIG_OF 195a28aad66SStephen Boyd static const struct of_device_id coreboot_of_match[] = { 196a28aad66SStephen Boyd { .compatible = "coreboot" }, 197a28aad66SStephen Boyd {} 198a28aad66SStephen Boyd }; 199a28aad66SStephen Boyd MODULE_DEVICE_TABLE(of, coreboot_of_match); 200a28aad66SStephen Boyd #endif 201a28aad66SStephen Boyd 202a28aad66SStephen Boyd static struct platform_driver coreboot_table_driver = { 203a28aad66SStephen Boyd .probe = coreboot_table_probe, 204a28aad66SStephen Boyd .remove = coreboot_table_remove, 205a28aad66SStephen Boyd .driver = { 206a28aad66SStephen Boyd .name = "coreboot_table", 207a28aad66SStephen Boyd .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match), 208a28aad66SStephen Boyd .of_match_table = of_match_ptr(coreboot_of_match), 209a28aad66SStephen Boyd }, 210a28aad66SStephen Boyd }; 211a28aad66SStephen Boyd module_platform_driver(coreboot_table_driver); 212d384d6f4SThierry Escande MODULE_AUTHOR("Google, Inc."); 213d384d6f4SThierry Escande MODULE_LICENSE("GPL"); 214