1 // SPDX-License-Identifier: GPL-2.0-only
5 * Module providing coreboot table access.
7 * Copyright 2017 Google Inc.
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #include "coreboot_table.h"
24 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
25 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
27 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
29 struct coreboot_device *device = CB_DEV(dev);
30 struct coreboot_driver *driver = CB_DRV(drv);
32 return device->entry.tag == driver->tag;
35 static int coreboot_bus_probe(struct device *dev)
38 struct coreboot_device *device = CB_DEV(dev);
39 struct coreboot_driver *driver = CB_DRV(dev->driver);
42 ret = driver->probe(device);
47 static void coreboot_bus_remove(struct device *dev)
49 struct coreboot_device *device = CB_DEV(dev);
50 struct coreboot_driver *driver = CB_DRV(dev->driver);
53 driver->remove(device);
56 static struct bus_type coreboot_bus_type = {
58 .match = coreboot_bus_match,
59 .probe = coreboot_bus_probe,
60 .remove = coreboot_bus_remove,
63 static void coreboot_device_release(struct device *dev)
65 struct coreboot_device *device = CB_DEV(dev);
70 int coreboot_driver_register(struct coreboot_driver *driver)
72 driver->drv.bus = &coreboot_bus_type;
74 return driver_register(&driver->drv);
76 EXPORT_SYMBOL(coreboot_driver_register);
78 void coreboot_driver_unregister(struct coreboot_driver *driver)
80 driver_unregister(&driver->drv);
82 EXPORT_SYMBOL(coreboot_driver_unregister);
84 static int coreboot_table_populate(struct device *dev, void *ptr)
88 struct coreboot_device *device;
89 struct coreboot_table_entry *entry;
90 struct coreboot_table_header *header = ptr;
92 ptr_entry = ptr + header->header_bytes;
93 for (i = 0; i < header->table_entries; i++) {
96 if (entry->size < sizeof(*entry)) {
97 dev_warn(dev, "coreboot table entry too small!\n");
101 device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);
105 device->dev.parent = dev;
106 device->dev.bus = &coreboot_bus_type;
107 device->dev.release = coreboot_device_release;
108 memcpy(device->raw, ptr_entry, entry->size);
110 switch (device->entry.tag) {
111 case LB_TAG_CBMEM_ENTRY:
112 dev_set_name(&device->dev, "cbmem-%08x",
113 device->cbmem_entry.id);
116 dev_set_name(&device->dev, "coreboot%d", i);
120 ret = device_register(&device->dev);
122 put_device(&device->dev);
126 ptr_entry += entry->size;
132 static int coreboot_table_probe(struct platform_device *pdev)
135 struct coreboot_table_header *header;
136 struct resource *res;
137 struct device *dev = &pdev->dev;
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 len = resource_size(res);
146 if (!res->start || !len)
149 /* Check just the header first to make sure things are sane */
150 header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
154 len = header->header_bytes + header->table_bytes;
155 ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
158 dev_warn(dev, "coreboot table missing or corrupt!\n");
162 ptr = memremap(res->start, len, MEMREMAP_WB);
166 ret = coreboot_table_populate(dev, ptr);
173 static int __cb_dev_unregister(struct device *dev, void *dummy)
175 device_unregister(dev);
179 static int coreboot_table_remove(struct platform_device *pdev)
181 bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
186 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
191 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
195 static const struct of_device_id coreboot_of_match[] = {
196 { .compatible = "coreboot" },
199 MODULE_DEVICE_TABLE(of, coreboot_of_match);
202 static struct platform_driver coreboot_table_driver = {
203 .probe = coreboot_table_probe,
204 .remove = coreboot_table_remove,
206 .name = "coreboot_table",
207 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
208 .of_match_table = of_match_ptr(coreboot_of_match),
212 static int __init coreboot_table_driver_init(void)
216 ret = bus_register(&coreboot_bus_type);
220 ret = platform_driver_register(&coreboot_table_driver);
222 bus_unregister(&coreboot_bus_type);
229 static void __exit coreboot_table_driver_exit(void)
231 platform_driver_unregister(&coreboot_table_driver);
232 bus_unregister(&coreboot_bus_type);
235 module_init(coreboot_table_driver_init);
236 module_exit(coreboot_table_driver_exit);
238 MODULE_AUTHOR("Google, Inc.");
239 MODULE_LICENSE("GPL");