Linux Audio

Check our new training course

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