Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
27static 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
35static 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
47static 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
56static 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
63static void coreboot_device_release(struct device *dev)
64{
65 struct coreboot_device *device = CB_DEV(dev);
66
67 kfree(device);
68}
69
70int coreboot_driver_register(struct coreboot_driver *driver)
71{
72 driver->drv.bus = &coreboot_bus_type;
73
74 return driver_register(&driver->drv);
75}
76EXPORT_SYMBOL(coreboot_driver_register);
77
78void coreboot_driver_unregister(struct coreboot_driver *driver)
79{
80 driver_unregister(&driver->drv);
81}
82EXPORT_SYMBOL(coreboot_driver_unregister);
83
84static 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 device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
97 if (!device)
98 return -ENOMEM;
99
100 device->dev.parent = dev;
101 device->dev.bus = &coreboot_bus_type;
102 device->dev.release = coreboot_device_release;
103 memcpy(&device->entry, ptr_entry, entry->size);
104
105 switch (device->entry.tag) {
106 case LB_TAG_CBMEM_ENTRY:
107 dev_set_name(&device->dev, "cbmem-%08x",
108 device->cbmem_entry.id);
109 break;
110 default:
111 dev_set_name(&device->dev, "coreboot%d", i);
112 break;
113 }
114
115 ret = device_register(&device->dev);
116 if (ret) {
117 put_device(&device->dev);
118 return ret;
119 }
120
121 ptr_entry += entry->size;
122 }
123
124 return 0;
125}
126
127static int coreboot_table_probe(struct platform_device *pdev)
128{
129 resource_size_t len;
130 struct coreboot_table_header *header;
131 struct resource *res;
132 struct device *dev = &pdev->dev;
133 void *ptr;
134 int ret;
135
136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
137 if (!res)
138 return -EINVAL;
139
140 len = resource_size(res);
141 if (!res->start || !len)
142 return -EINVAL;
143
144 /* Check just the header first to make sure things are sane */
145 header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
146 if (!header)
147 return -ENOMEM;
148
149 len = header->header_bytes + header->table_bytes;
150 ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
151 memunmap(header);
152 if (ret) {
153 dev_warn(dev, "coreboot table missing or corrupt!\n");
154 return -ENODEV;
155 }
156
157 ptr = memremap(res->start, len, MEMREMAP_WB);
158 if (!ptr)
159 return -ENOMEM;
160
161 ret = coreboot_table_populate(dev, ptr);
162
163 memunmap(ptr);
164
165 return ret;
166}
167
168static int __cb_dev_unregister(struct device *dev, void *dummy)
169{
170 device_unregister(dev);
171 return 0;
172}
173
174static int coreboot_table_remove(struct platform_device *pdev)
175{
176 bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
177 return 0;
178}
179
180#ifdef CONFIG_ACPI
181static const struct acpi_device_id cros_coreboot_acpi_match[] = {
182 { "GOOGCB00", 0 },
183 { "BOOT0000", 0 },
184 { }
185};
186MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
187#endif
188
189#ifdef CONFIG_OF
190static const struct of_device_id coreboot_of_match[] = {
191 { .compatible = "coreboot" },
192 {}
193};
194MODULE_DEVICE_TABLE(of, coreboot_of_match);
195#endif
196
197static struct platform_driver coreboot_table_driver = {
198 .probe = coreboot_table_probe,
199 .remove = coreboot_table_remove,
200 .driver = {
201 .name = "coreboot_table",
202 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
203 .of_match_table = of_match_ptr(coreboot_of_match),
204 },
205};
206
207static int __init coreboot_table_driver_init(void)
208{
209 int ret;
210
211 ret = bus_register(&coreboot_bus_type);
212 if (ret)
213 return ret;
214
215 ret = platform_driver_register(&coreboot_table_driver);
216 if (ret) {
217 bus_unregister(&coreboot_bus_type);
218 return ret;
219 }
220
221 return 0;
222}
223
224static void __exit coreboot_table_driver_exit(void)
225{
226 platform_driver_unregister(&coreboot_table_driver);
227 bus_unregister(&coreboot_bus_type);
228}
229
230module_init(coreboot_table_driver_init);
231module_exit(coreboot_table_driver_exit);
232
233MODULE_AUTHOR("Google, Inc.");
234MODULE_LICENSE("GPL");