xref: /linux/drivers/firmware/google/coreboot_table.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * coreboot_table.c
4  *
5  * Module providing coreboot table access.
6  *
7  * Copyright 2017 Google Inc.
8  * Copyright 2017 Samuel Holland <samuel@sholland.org>
9  */
10 
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 
23 #include "coreboot_table.h"
24 
25 /* Coreboot table header structure */
26 struct coreboot_table_header {
27 	char signature[4];
28 	u32 header_bytes;
29 	u32 header_checksum;
30 	u32 table_bytes;
31 	u32 table_checksum;
32 	u32 table_entries;
33 };
34 
35 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
36 #define CB_DRV(d) container_of_const(d, struct coreboot_driver, drv)
37 
38 static int coreboot_bus_match(struct device *dev, const struct device_driver *drv)
39 {
40 	struct coreboot_device *device = CB_DEV(dev);
41 	const struct coreboot_driver *driver = CB_DRV(drv);
42 	const struct coreboot_device_id *id;
43 
44 	if (!driver->id_table)
45 		return 0;
46 
47 	for (id = driver->id_table; id->tag; id++) {
48 		if (device->entry.tag == id->tag)
49 			return 1;
50 	}
51 
52 	return 0;
53 }
54 
55 static int coreboot_bus_probe(struct device *dev)
56 {
57 	int ret = -ENODEV;
58 	struct coreboot_device *device = CB_DEV(dev);
59 	struct coreboot_driver *driver = CB_DRV(dev->driver);
60 
61 	if (driver->probe)
62 		ret = driver->probe(device);
63 
64 	return ret;
65 }
66 
67 static void coreboot_bus_remove(struct device *dev)
68 {
69 	struct coreboot_device *device = CB_DEV(dev);
70 	struct coreboot_driver *driver = CB_DRV(dev->driver);
71 
72 	if (driver->remove)
73 		driver->remove(device);
74 }
75 
76 static int coreboot_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
77 {
78 	struct coreboot_device *device = CB_DEV(dev);
79 	u32 tag = device->entry.tag;
80 
81 	return add_uevent_var(env, "MODALIAS=coreboot:t%08X", tag);
82 }
83 
84 static const struct bus_type coreboot_bus_type = {
85 	.name		= "coreboot",
86 	.match		= coreboot_bus_match,
87 	.probe		= coreboot_bus_probe,
88 	.remove		= coreboot_bus_remove,
89 	.uevent		= coreboot_bus_uevent,
90 };
91 
92 static void coreboot_device_release(struct device *dev)
93 {
94 	struct coreboot_device *device = CB_DEV(dev);
95 
96 	kfree(device);
97 }
98 
99 int __coreboot_driver_register(struct coreboot_driver *driver,
100 			       struct module *owner)
101 {
102 	driver->drv.bus = &coreboot_bus_type;
103 	driver->drv.owner = owner;
104 
105 	return driver_register(&driver->drv);
106 }
107 EXPORT_SYMBOL(__coreboot_driver_register);
108 
109 void coreboot_driver_unregister(struct coreboot_driver *driver)
110 {
111 	driver_unregister(&driver->drv);
112 }
113 EXPORT_SYMBOL(coreboot_driver_unregister);
114 
115 static int coreboot_table_populate(struct device *dev, void *ptr)
116 {
117 	int i, ret;
118 	void *ptr_entry;
119 	struct coreboot_device *device;
120 	struct coreboot_table_entry *entry;
121 	struct coreboot_table_header *header = ptr;
122 
123 	ptr_entry = ptr + header->header_bytes;
124 	for (i = 0; i < header->table_entries; i++) {
125 		entry = ptr_entry;
126 
127 		if (entry->size < sizeof(*entry)) {
128 			dev_warn(dev, "coreboot table entry too small!\n");
129 			return -EINVAL;
130 		}
131 
132 		device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);
133 		if (!device)
134 			return -ENOMEM;
135 
136 		device->dev.parent = dev;
137 		device->dev.bus = &coreboot_bus_type;
138 		device->dev.release = coreboot_device_release;
139 		memcpy(device->raw, ptr_entry, entry->size);
140 
141 		switch (device->entry.tag) {
142 		case LB_TAG_CBMEM_ENTRY:
143 			dev_set_name(&device->dev, "cbmem-%08x",
144 				     device->cbmem_entry.id);
145 			break;
146 		default:
147 			dev_set_name(&device->dev, "coreboot%d", i);
148 			break;
149 		}
150 
151 		ret = device_register(&device->dev);
152 		if (ret) {
153 			put_device(&device->dev);
154 			return ret;
155 		}
156 
157 		ptr_entry += entry->size;
158 	}
159 
160 	return 0;
161 }
162 
163 static int coreboot_table_probe(struct platform_device *pdev)
164 {
165 	resource_size_t len;
166 	struct coreboot_table_header *header;
167 	struct resource *res;
168 	struct device *dev = &pdev->dev;
169 	void *ptr;
170 	int ret;
171 
172 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 	if (!res)
174 		return -EINVAL;
175 
176 	len = resource_size(res);
177 	if (!res->start || !len)
178 		return -EINVAL;
179 
180 	/* Check just the header first to make sure things are sane */
181 	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
182 	if (!header)
183 		return -ENOMEM;
184 
185 	len = header->header_bytes + header->table_bytes;
186 	ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
187 	memunmap(header);
188 	if (ret) {
189 		dev_warn(dev, "coreboot table missing or corrupt!\n");
190 		return -ENODEV;
191 	}
192 
193 	ptr = memremap(res->start, len, MEMREMAP_WB);
194 	if (!ptr)
195 		return -ENOMEM;
196 
197 	ret = coreboot_table_populate(dev, ptr);
198 
199 	memunmap(ptr);
200 
201 	return ret;
202 }
203 
204 static int __cb_dev_unregister(struct device *dev, void *dummy)
205 {
206 	device_unregister(dev);
207 	return 0;
208 }
209 
210 static void coreboot_table_remove(struct platform_device *pdev)
211 {
212 	bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
213 }
214 
215 #ifdef CONFIG_ACPI
216 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
217 	{ "GOOGCB00", 0 },
218 	{ "BOOT0000", 0 },
219 	{ }
220 };
221 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
222 #endif
223 
224 #ifdef CONFIG_OF
225 static const struct of_device_id coreboot_of_match[] = {
226 	{ .compatible = "coreboot" },
227 	{}
228 };
229 MODULE_DEVICE_TABLE(of, coreboot_of_match);
230 #endif
231 
232 static struct platform_driver coreboot_table_driver = {
233 	.probe = coreboot_table_probe,
234 	.remove = coreboot_table_remove,
235 	.driver = {
236 		.name = "coreboot_table",
237 		.acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
238 		.of_match_table = of_match_ptr(coreboot_of_match),
239 	},
240 };
241 
242 static int __init coreboot_table_driver_init(void)
243 {
244 	int ret;
245 
246 	ret = bus_register(&coreboot_bus_type);
247 	if (ret)
248 		return ret;
249 
250 	ret = platform_driver_register(&coreboot_table_driver);
251 	if (ret) {
252 		bus_unregister(&coreboot_bus_type);
253 		return ret;
254 	}
255 
256 	return 0;
257 }
258 
259 static void __exit coreboot_table_driver_exit(void)
260 {
261 	platform_driver_unregister(&coreboot_table_driver);
262 	bus_unregister(&coreboot_bus_type);
263 }
264 
265 subsys_initcall(coreboot_table_driver_init);
266 module_exit(coreboot_table_driver_exit);
267 
268 MODULE_AUTHOR("Google, Inc.");
269 MODULE_DESCRIPTION("Module providing coreboot table access");
270 MODULE_LICENSE("GPL");
271