| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * coreboot_table.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Copyright 2017 Google Inc. |
|---|
| 7 | 8 | * Copyright 2017 Samuel Holland <samuel@sholland.org> |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 10 | | - * it under the terms of the GNU General Public License v2.0 as published by |
|---|
| 11 | | - * the Free Software Foundation. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | | - * GNU General Public License for more details. |
|---|
| 17 | 9 | */ |
|---|
| 18 | 10 | |
|---|
| 11 | +#include <linux/acpi.h> |
|---|
| 19 | 12 | #include <linux/device.h> |
|---|
| 20 | 13 | #include <linux/err.h> |
|---|
| 21 | 14 | #include <linux/init.h> |
|---|
| 22 | 15 | #include <linux/io.h> |
|---|
| 23 | 16 | #include <linux/kernel.h> |
|---|
| 24 | 17 | #include <linux/module.h> |
|---|
| 18 | +#include <linux/of.h> |
|---|
| 19 | +#include <linux/platform_device.h> |
|---|
| 25 | 20 | #include <linux/slab.h> |
|---|
| 26 | 21 | |
|---|
| 27 | 22 | #include "coreboot_table.h" |
|---|
| 28 | 23 | |
|---|
| 29 | 24 | #define CB_DEV(d) container_of(d, struct coreboot_device, dev) |
|---|
| 30 | 25 | #define CB_DRV(d) container_of(d, struct coreboot_driver, drv) |
|---|
| 31 | | - |
|---|
| 32 | | -static struct coreboot_table_header __iomem *ptr_header; |
|---|
| 33 | 26 | |
|---|
| 34 | 27 | static int coreboot_bus_match(struct device *dev, struct device_driver *drv) |
|---|
| 35 | 28 | { |
|---|
| .. | .. |
|---|
| 70 | 63 | .remove = coreboot_bus_remove, |
|---|
| 71 | 64 | }; |
|---|
| 72 | 65 | |
|---|
| 73 | | -static int __init coreboot_bus_init(void) |
|---|
| 74 | | -{ |
|---|
| 75 | | - return bus_register(&coreboot_bus_type); |
|---|
| 76 | | -} |
|---|
| 77 | | -module_init(coreboot_bus_init); |
|---|
| 78 | | - |
|---|
| 79 | 66 | static void coreboot_device_release(struct device *dev) |
|---|
| 80 | 67 | { |
|---|
| 81 | 68 | struct coreboot_device *device = CB_DEV(dev); |
|---|
| .. | .. |
|---|
| 97 | 84 | } |
|---|
| 98 | 85 | EXPORT_SYMBOL(coreboot_driver_unregister); |
|---|
| 99 | 86 | |
|---|
| 100 | | -int coreboot_table_init(struct device *dev, void __iomem *ptr) |
|---|
| 87 | +static int coreboot_table_populate(struct device *dev, void *ptr) |
|---|
| 101 | 88 | { |
|---|
| 102 | 89 | int i, ret; |
|---|
| 103 | 90 | void *ptr_entry; |
|---|
| 104 | 91 | struct coreboot_device *device; |
|---|
| 105 | | - struct coreboot_table_entry entry; |
|---|
| 106 | | - struct coreboot_table_header header; |
|---|
| 92 | + struct coreboot_table_entry *entry; |
|---|
| 93 | + struct coreboot_table_header *header = ptr; |
|---|
| 107 | 94 | |
|---|
| 108 | | - ptr_header = ptr; |
|---|
| 109 | | - memcpy_fromio(&header, ptr_header, sizeof(header)); |
|---|
| 95 | + ptr_entry = ptr + header->header_bytes; |
|---|
| 96 | + for (i = 0; i < header->table_entries; i++) { |
|---|
| 97 | + entry = ptr_entry; |
|---|
| 110 | 98 | |
|---|
| 111 | | - if (strncmp(header.signature, "LBIO", sizeof(header.signature))) { |
|---|
| 112 | | - pr_warn("coreboot_table: coreboot table missing or corrupt!\n"); |
|---|
| 113 | | - ret = -ENODEV; |
|---|
| 114 | | - goto out; |
|---|
| 115 | | - } |
|---|
| 116 | | - |
|---|
| 117 | | - ptr_entry = (void *)ptr_header + header.header_bytes; |
|---|
| 118 | | - for (i = 0; i < header.table_entries; i++) { |
|---|
| 119 | | - memcpy_fromio(&entry, ptr_entry, sizeof(entry)); |
|---|
| 120 | | - |
|---|
| 121 | | - device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL); |
|---|
| 122 | | - if (!device) { |
|---|
| 123 | | - ret = -ENOMEM; |
|---|
| 124 | | - break; |
|---|
| 125 | | - } |
|---|
| 99 | + device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL); |
|---|
| 100 | + if (!device) |
|---|
| 101 | + return -ENOMEM; |
|---|
| 126 | 102 | |
|---|
| 127 | 103 | dev_set_name(&device->dev, "coreboot%d", i); |
|---|
| 128 | 104 | device->dev.parent = dev; |
|---|
| 129 | 105 | device->dev.bus = &coreboot_bus_type; |
|---|
| 130 | 106 | device->dev.release = coreboot_device_release; |
|---|
| 131 | | - memcpy_fromio(&device->entry, ptr_entry, entry.size); |
|---|
| 107 | + memcpy(&device->entry, ptr_entry, entry->size); |
|---|
| 132 | 108 | |
|---|
| 133 | 109 | ret = device_register(&device->dev); |
|---|
| 134 | 110 | if (ret) { |
|---|
| 135 | 111 | put_device(&device->dev); |
|---|
| 136 | | - break; |
|---|
| 112 | + return ret; |
|---|
| 137 | 113 | } |
|---|
| 138 | 114 | |
|---|
| 139 | | - ptr_entry += entry.size; |
|---|
| 140 | | - } |
|---|
| 141 | | -out: |
|---|
| 142 | | - iounmap(ptr); |
|---|
| 143 | | - return ret; |
|---|
| 144 | | -} |
|---|
| 145 | | -EXPORT_SYMBOL(coreboot_table_init); |
|---|
| 146 | | - |
|---|
| 147 | | -int coreboot_table_exit(void) |
|---|
| 148 | | -{ |
|---|
| 149 | | - if (ptr_header) { |
|---|
| 150 | | - bus_unregister(&coreboot_bus_type); |
|---|
| 151 | | - ptr_header = NULL; |
|---|
| 115 | + ptr_entry += entry->size; |
|---|
| 152 | 116 | } |
|---|
| 153 | 117 | |
|---|
| 154 | 118 | return 0; |
|---|
| 155 | 119 | } |
|---|
| 156 | | -EXPORT_SYMBOL(coreboot_table_exit); |
|---|
| 120 | + |
|---|
| 121 | +static int coreboot_table_probe(struct platform_device *pdev) |
|---|
| 122 | +{ |
|---|
| 123 | + resource_size_t len; |
|---|
| 124 | + struct coreboot_table_header *header; |
|---|
| 125 | + struct resource *res; |
|---|
| 126 | + struct device *dev = &pdev->dev; |
|---|
| 127 | + void *ptr; |
|---|
| 128 | + int ret; |
|---|
| 129 | + |
|---|
| 130 | + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 131 | + if (!res) |
|---|
| 132 | + return -EINVAL; |
|---|
| 133 | + |
|---|
| 134 | + len = resource_size(res); |
|---|
| 135 | + if (!res->start || !len) |
|---|
| 136 | + return -EINVAL; |
|---|
| 137 | + |
|---|
| 138 | + /* Check just the header first to make sure things are sane */ |
|---|
| 139 | + header = memremap(res->start, sizeof(*header), MEMREMAP_WB); |
|---|
| 140 | + if (!header) |
|---|
| 141 | + return -ENOMEM; |
|---|
| 142 | + |
|---|
| 143 | + len = header->header_bytes + header->table_bytes; |
|---|
| 144 | + ret = strncmp(header->signature, "LBIO", sizeof(header->signature)); |
|---|
| 145 | + memunmap(header); |
|---|
| 146 | + if (ret) { |
|---|
| 147 | + dev_warn(dev, "coreboot table missing or corrupt!\n"); |
|---|
| 148 | + return -ENODEV; |
|---|
| 149 | + } |
|---|
| 150 | + |
|---|
| 151 | + ptr = memremap(res->start, len, MEMREMAP_WB); |
|---|
| 152 | + if (!ptr) |
|---|
| 153 | + return -ENOMEM; |
|---|
| 154 | + |
|---|
| 155 | + ret = coreboot_table_populate(dev, ptr); |
|---|
| 156 | + |
|---|
| 157 | + memunmap(ptr); |
|---|
| 158 | + |
|---|
| 159 | + return ret; |
|---|
| 160 | +} |
|---|
| 161 | + |
|---|
| 162 | +static int __cb_dev_unregister(struct device *dev, void *dummy) |
|---|
| 163 | +{ |
|---|
| 164 | + device_unregister(dev); |
|---|
| 165 | + return 0; |
|---|
| 166 | +} |
|---|
| 167 | + |
|---|
| 168 | +static int coreboot_table_remove(struct platform_device *pdev) |
|---|
| 169 | +{ |
|---|
| 170 | + bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister); |
|---|
| 171 | + return 0; |
|---|
| 172 | +} |
|---|
| 173 | + |
|---|
| 174 | +#ifdef CONFIG_ACPI |
|---|
| 175 | +static const struct acpi_device_id cros_coreboot_acpi_match[] = { |
|---|
| 176 | + { "GOOGCB00", 0 }, |
|---|
| 177 | + { "BOOT0000", 0 }, |
|---|
| 178 | + { } |
|---|
| 179 | +}; |
|---|
| 180 | +MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match); |
|---|
| 181 | +#endif |
|---|
| 182 | + |
|---|
| 183 | +#ifdef CONFIG_OF |
|---|
| 184 | +static const struct of_device_id coreboot_of_match[] = { |
|---|
| 185 | + { .compatible = "coreboot" }, |
|---|
| 186 | + {} |
|---|
| 187 | +}; |
|---|
| 188 | +MODULE_DEVICE_TABLE(of, coreboot_of_match); |
|---|
| 189 | +#endif |
|---|
| 190 | + |
|---|
| 191 | +static struct platform_driver coreboot_table_driver = { |
|---|
| 192 | + .probe = coreboot_table_probe, |
|---|
| 193 | + .remove = coreboot_table_remove, |
|---|
| 194 | + .driver = { |
|---|
| 195 | + .name = "coreboot_table", |
|---|
| 196 | + .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match), |
|---|
| 197 | + .of_match_table = of_match_ptr(coreboot_of_match), |
|---|
| 198 | + }, |
|---|
| 199 | +}; |
|---|
| 200 | + |
|---|
| 201 | +static int __init coreboot_table_driver_init(void) |
|---|
| 202 | +{ |
|---|
| 203 | + int ret; |
|---|
| 204 | + |
|---|
| 205 | + ret = bus_register(&coreboot_bus_type); |
|---|
| 206 | + if (ret) |
|---|
| 207 | + return ret; |
|---|
| 208 | + |
|---|
| 209 | + ret = platform_driver_register(&coreboot_table_driver); |
|---|
| 210 | + if (ret) { |
|---|
| 211 | + bus_unregister(&coreboot_bus_type); |
|---|
| 212 | + return ret; |
|---|
| 213 | + } |
|---|
| 214 | + |
|---|
| 215 | + return 0; |
|---|
| 216 | +} |
|---|
| 217 | + |
|---|
| 218 | +static void __exit coreboot_table_driver_exit(void) |
|---|
| 219 | +{ |
|---|
| 220 | + platform_driver_unregister(&coreboot_table_driver); |
|---|
| 221 | + bus_unregister(&coreboot_bus_type); |
|---|
| 222 | +} |
|---|
| 223 | + |
|---|
| 224 | +module_init(coreboot_table_driver_init); |
|---|
| 225 | +module_exit(coreboot_table_driver_exit); |
|---|
| 157 | 226 | |
|---|
| 158 | 227 | MODULE_AUTHOR("Google, Inc."); |
|---|
| 159 | 228 | MODULE_LICENSE("GPL"); |
|---|