hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/drivers/firmware/google/coreboot_table.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * coreboot_table.c
34 *
....@@ -5,31 +6,23 @@
56 *
67 * Copyright 2017 Google Inc.
78 * 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.
179 */
1810
11
+#include <linux/acpi.h>
1912 #include <linux/device.h>
2013 #include <linux/err.h>
2114 #include <linux/init.h>
2215 #include <linux/io.h>
2316 #include <linux/kernel.h>
2417 #include <linux/module.h>
18
+#include <linux/of.h>
19
+#include <linux/platform_device.h>
2520 #include <linux/slab.h>
2621
2722 #include "coreboot_table.h"
2823
2924 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
3025 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
31
-
32
-static struct coreboot_table_header __iomem *ptr_header;
3326
3427 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
3528 {
....@@ -70,12 +63,6 @@
7063 .remove = coreboot_bus_remove,
7164 };
7265
73
-static int __init coreboot_bus_init(void)
74
-{
75
- return bus_register(&coreboot_bus_type);
76
-}
77
-module_init(coreboot_bus_init);
78
-
7966 static void coreboot_device_release(struct device *dev)
8067 {
8168 struct coreboot_device *device = CB_DEV(dev);
....@@ -97,63 +84,145 @@
9784 }
9885 EXPORT_SYMBOL(coreboot_driver_unregister);
9986
100
-int coreboot_table_init(struct device *dev, void __iomem *ptr)
87
+static int coreboot_table_populate(struct device *dev, void *ptr)
10188 {
10289 int i, ret;
10390 void *ptr_entry;
10491 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;
10794
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;
11098
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;
126102
127103 dev_set_name(&device->dev, "coreboot%d", i);
128104 device->dev.parent = dev;
129105 device->dev.bus = &coreboot_bus_type;
130106 device->dev.release = coreboot_device_release;
131
- memcpy_fromio(&device->entry, ptr_entry, entry.size);
107
+ memcpy(&device->entry, ptr_entry, entry->size);
132108
133109 ret = device_register(&device->dev);
134110 if (ret) {
135111 put_device(&device->dev);
136
- break;
112
+ return ret;
137113 }
138114
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;
152116 }
153117
154118 return 0;
155119 }
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);
157226
158227 MODULE_AUTHOR("Google, Inc.");
159228 MODULE_LICENSE("GPL");