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/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21
22 #include "coreboot_table.h"
23
24 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
25 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
26
coreboot_bus_match(struct device * dev,struct device_driver * drv)27 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
28 {
29 struct coreboot_device *device = CB_DEV(dev);
30 struct coreboot_driver *driver = CB_DRV(drv);
31
32 return device->entry.tag == driver->tag;
33 }
34
coreboot_bus_probe(struct device * dev)35 static int coreboot_bus_probe(struct device *dev)
36 {
37 int ret = -ENODEV;
38 struct coreboot_device *device = CB_DEV(dev);
39 struct coreboot_driver *driver = CB_DRV(dev->driver);
40
41 if (driver->probe)
42 ret = driver->probe(device);
43
44 return ret;
45 }
46
coreboot_bus_remove(struct device * dev)47 static void coreboot_bus_remove(struct device *dev)
48 {
49 struct coreboot_device *device = CB_DEV(dev);
50 struct coreboot_driver *driver = CB_DRV(dev->driver);
51
52 if (driver->remove)
53 driver->remove(device);
54 }
55
56 static struct bus_type coreboot_bus_type = {
57 .name = "coreboot",
58 .match = coreboot_bus_match,
59 .probe = coreboot_bus_probe,
60 .remove = coreboot_bus_remove,
61 };
62
coreboot_device_release(struct device * dev)63 static void coreboot_device_release(struct device *dev)
64 {
65 struct coreboot_device *device = CB_DEV(dev);
66
67 kfree(device);
68 }
69
coreboot_driver_register(struct coreboot_driver * driver)70 int coreboot_driver_register(struct coreboot_driver *driver)
71 {
72 driver->drv.bus = &coreboot_bus_type;
73
74 return driver_register(&driver->drv);
75 }
76 EXPORT_SYMBOL(coreboot_driver_register);
77
coreboot_driver_unregister(struct coreboot_driver * driver)78 void coreboot_driver_unregister(struct coreboot_driver *driver)
79 {
80 driver_unregister(&driver->drv);
81 }
82 EXPORT_SYMBOL(coreboot_driver_unregister);
83
coreboot_table_populate(struct device * dev,void * ptr)84 static int coreboot_table_populate(struct device *dev, void *ptr)
85 {
86 int i, ret;
87 void *ptr_entry;
88 struct coreboot_device *device;
89 struct coreboot_table_entry *entry;
90 struct coreboot_table_header *header = ptr;
91
92 ptr_entry = ptr + header->header_bytes;
93 for (i = 0; i < header->table_entries; i++) {
94 entry = ptr_entry;
95
96 if (entry->size < sizeof(*entry)) {
97 dev_warn(dev, "coreboot table entry too small!\n");
98 return -EINVAL;
99 }
100
101 device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);
102 if (!device)
103 return -ENOMEM;
104
105 dev_set_name(&device->dev, "coreboot%d", i);
106 device->dev.parent = dev;
107 device->dev.bus = &coreboot_bus_type;
108 device->dev.release = coreboot_device_release;
109 memcpy(device->raw, ptr_entry, entry->size);
110
111 ret = device_register(&device->dev);
112 if (ret) {
113 put_device(&device->dev);
114 return ret;
115 }
116
117 ptr_entry += entry->size;
118 }
119
120 return 0;
121 }
122
coreboot_table_probe(struct platform_device * pdev)123 static int coreboot_table_probe(struct platform_device *pdev)
124 {
125 resource_size_t len;
126 struct coreboot_table_header *header;
127 struct resource *res;
128 struct device *dev = &pdev->dev;
129 void *ptr;
130 int ret;
131
132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 if (!res)
134 return -EINVAL;
135
136 len = resource_size(res);
137 if (!res->start || !len)
138 return -EINVAL;
139
140 /* Check just the header first to make sure things are sane */
141 header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
142 if (!header)
143 return -ENOMEM;
144
145 len = header->header_bytes + header->table_bytes;
146 ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
147 memunmap(header);
148 if (ret) {
149 dev_warn(dev, "coreboot table missing or corrupt!\n");
150 return -ENODEV;
151 }
152
153 ptr = memremap(res->start, len, MEMREMAP_WB);
154 if (!ptr)
155 return -ENOMEM;
156
157 ret = coreboot_table_populate(dev, ptr);
158
159 memunmap(ptr);
160
161 return ret;
162 }
163
__cb_dev_unregister(struct device * dev,void * dummy)164 static int __cb_dev_unregister(struct device *dev, void *dummy)
165 {
166 device_unregister(dev);
167 return 0;
168 }
169
coreboot_table_remove(struct platform_device * pdev)170 static int coreboot_table_remove(struct platform_device *pdev)
171 {
172 bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
173 return 0;
174 }
175
176 #ifdef CONFIG_ACPI
177 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
178 { "GOOGCB00", 0 },
179 { "BOOT0000", 0 },
180 { }
181 };
182 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
183 #endif
184
185 #ifdef CONFIG_OF
186 static const struct of_device_id coreboot_of_match[] = {
187 { .compatible = "coreboot" },
188 {}
189 };
190 MODULE_DEVICE_TABLE(of, coreboot_of_match);
191 #endif
192
193 static struct platform_driver coreboot_table_driver = {
194 .probe = coreboot_table_probe,
195 .remove = coreboot_table_remove,
196 .driver = {
197 .name = "coreboot_table",
198 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
199 .of_match_table = of_match_ptr(coreboot_of_match),
200 },
201 };
202
coreboot_table_driver_init(void)203 static int __init coreboot_table_driver_init(void)
204 {
205 int ret;
206
207 ret = bus_register(&coreboot_bus_type);
208 if (ret)
209 return ret;
210
211 ret = platform_driver_register(&coreboot_table_driver);
212 if (ret) {
213 bus_unregister(&coreboot_bus_type);
214 return ret;
215 }
216
217 return 0;
218 }
219
coreboot_table_driver_exit(void)220 static void __exit coreboot_table_driver_exit(void)
221 {
222 platform_driver_unregister(&coreboot_table_driver);
223 bus_unregister(&coreboot_bus_type);
224 }
225
226 module_init(coreboot_table_driver_init);
227 module_exit(coreboot_table_driver_exit);
228
229 MODULE_AUTHOR("Google, Inc.");
230 MODULE_LICENSE("GPL");
231