1d9523678SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d384d6f4SThierry Escande /*
3d384d6f4SThierry Escande  * coreboot_table.c
4d384d6f4SThierry Escande  *
5d384d6f4SThierry Escande  * Module providing coreboot table access.
6d384d6f4SThierry Escande  *
7d384d6f4SThierry Escande  * Copyright 2017 Google Inc.
8570d30c2SSamuel Holland  * Copyright 2017 Samuel Holland <samuel@sholland.org>
9d384d6f4SThierry Escande  */
10d384d6f4SThierry Escande 
11a28aad66SStephen Boyd #include <linux/acpi.h>
12570d30c2SSamuel Holland #include <linux/device.h>
13d384d6f4SThierry Escande #include <linux/err.h>
14d384d6f4SThierry Escande #include <linux/init.h>
15d384d6f4SThierry Escande #include <linux/io.h>
16d384d6f4SThierry Escande #include <linux/kernel.h>
17d384d6f4SThierry Escande #include <linux/module.h>
18a28aad66SStephen Boyd #include <linux/of.h>
19a28aad66SStephen Boyd #include <linux/platform_device.h>
20570d30c2SSamuel Holland #include <linux/slab.h>
21d384d6f4SThierry Escande 
22d384d6f4SThierry Escande #include "coreboot_table.h"
23d384d6f4SThierry Escande 
24570d30c2SSamuel Holland #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
25570d30c2SSamuel Holland #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
26d384d6f4SThierry Escande 
27570d30c2SSamuel Holland static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
28570d30c2SSamuel Holland {
29570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
30570d30c2SSamuel Holland 	struct coreboot_driver *driver = CB_DRV(drv);
31570d30c2SSamuel Holland 
32570d30c2SSamuel Holland 	return device->entry.tag == driver->tag;
33570d30c2SSamuel Holland }
34570d30c2SSamuel Holland 
35570d30c2SSamuel Holland static int coreboot_bus_probe(struct device *dev)
36570d30c2SSamuel Holland {
37570d30c2SSamuel Holland 	int ret = -ENODEV;
38570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
39570d30c2SSamuel Holland 	struct coreboot_driver *driver = CB_DRV(dev->driver);
40570d30c2SSamuel Holland 
41570d30c2SSamuel Holland 	if (driver->probe)
42570d30c2SSamuel Holland 		ret = driver->probe(device);
43570d30c2SSamuel Holland 
44570d30c2SSamuel Holland 	return ret;
45570d30c2SSamuel Holland }
46570d30c2SSamuel Holland 
47*fc7a6209SUwe Kleine-König static void coreboot_bus_remove(struct device *dev)
48570d30c2SSamuel Holland {
49570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
50570d30c2SSamuel Holland 	struct coreboot_driver *driver = CB_DRV(dev->driver);
51570d30c2SSamuel Holland 
52570d30c2SSamuel Holland 	if (driver->remove)
535f680532SUwe Kleine-König 		driver->remove(device);
54570d30c2SSamuel Holland }
55570d30c2SSamuel Holland 
56570d30c2SSamuel Holland static struct bus_type coreboot_bus_type = {
57570d30c2SSamuel Holland 	.name		= "coreboot",
58570d30c2SSamuel Holland 	.match		= coreboot_bus_match,
59570d30c2SSamuel Holland 	.probe		= coreboot_bus_probe,
60570d30c2SSamuel Holland 	.remove		= coreboot_bus_remove,
61570d30c2SSamuel Holland };
62570d30c2SSamuel Holland 
63570d30c2SSamuel Holland static void coreboot_device_release(struct device *dev)
64570d30c2SSamuel Holland {
65570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
66570d30c2SSamuel Holland 
67570d30c2SSamuel Holland 	kfree(device);
68570d30c2SSamuel Holland }
69570d30c2SSamuel Holland 
70570d30c2SSamuel Holland int coreboot_driver_register(struct coreboot_driver *driver)
71570d30c2SSamuel Holland {
72570d30c2SSamuel Holland 	driver->drv.bus = &coreboot_bus_type;
73570d30c2SSamuel Holland 
74570d30c2SSamuel Holland 	return driver_register(&driver->drv);
75570d30c2SSamuel Holland }
76570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_register);
77570d30c2SSamuel Holland 
78570d30c2SSamuel Holland void coreboot_driver_unregister(struct coreboot_driver *driver)
79570d30c2SSamuel Holland {
80570d30c2SSamuel Holland 	driver_unregister(&driver->drv);
81570d30c2SSamuel Holland }
82570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_unregister);
83570d30c2SSamuel Holland 
847adb05bbSStephen Boyd static int coreboot_table_populate(struct device *dev, void *ptr)
85d384d6f4SThierry Escande {
86570d30c2SSamuel Holland 	int i, ret;
87570d30c2SSamuel Holland 	void *ptr_entry;
88570d30c2SSamuel Holland 	struct coreboot_device *device;
89a7d9b5f0SStephen Boyd 	struct coreboot_table_entry *entry;
907adb05bbSStephen Boyd 	struct coreboot_table_header *header = ptr;
91d384d6f4SThierry Escande 
927adb05bbSStephen Boyd 	ptr_entry = ptr + header->header_bytes;
93a7d9b5f0SStephen Boyd 	for (i = 0; i < header->table_entries; i++) {
94a7d9b5f0SStephen Boyd 		entry = ptr_entry;
95570d30c2SSamuel Holland 
96a7d9b5f0SStephen Boyd 		device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
977adb05bbSStephen Boyd 		if (!device)
987adb05bbSStephen Boyd 			return -ENOMEM;
99570d30c2SSamuel Holland 
100570d30c2SSamuel Holland 		dev_set_name(&device->dev, "coreboot%d", i);
101570d30c2SSamuel Holland 		device->dev.parent = dev;
102570d30c2SSamuel Holland 		device->dev.bus = &coreboot_bus_type;
103570d30c2SSamuel Holland 		device->dev.release = coreboot_device_release;
104a7d9b5f0SStephen Boyd 		memcpy(&device->entry, ptr_entry, entry->size);
105570d30c2SSamuel Holland 
106570d30c2SSamuel Holland 		ret = device_register(&device->dev);
107570d30c2SSamuel Holland 		if (ret) {
108570d30c2SSamuel Holland 			put_device(&device->dev);
1097adb05bbSStephen Boyd 			return ret;
110570d30c2SSamuel Holland 		}
111570d30c2SSamuel Holland 
112a7d9b5f0SStephen Boyd 		ptr_entry += entry->size;
113570d30c2SSamuel Holland 	}
114b81e3140SStephen Boyd 
1157adb05bbSStephen Boyd 	return 0;
116d384d6f4SThierry Escande }
117d384d6f4SThierry Escande 
118a28aad66SStephen Boyd static int coreboot_table_probe(struct platform_device *pdev)
119a28aad66SStephen Boyd {
120a28aad66SStephen Boyd 	resource_size_t len;
121a7d9b5f0SStephen Boyd 	struct coreboot_table_header *header;
122a28aad66SStephen Boyd 	struct resource *res;
1237adb05bbSStephen Boyd 	struct device *dev = &pdev->dev;
124a7d9b5f0SStephen Boyd 	void *ptr;
1257adb05bbSStephen Boyd 	int ret;
126a28aad66SStephen Boyd 
127a28aad66SStephen Boyd 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128a28aad66SStephen Boyd 	if (!res)
129a28aad66SStephen Boyd 		return -EINVAL;
130a28aad66SStephen Boyd 
131a28aad66SStephen Boyd 	len = resource_size(res);
132a28aad66SStephen Boyd 	if (!res->start || !len)
133a28aad66SStephen Boyd 		return -EINVAL;
134a28aad66SStephen Boyd 
1357adb05bbSStephen Boyd 	/* Check just the header first to make sure things are sane */
136a7d9b5f0SStephen Boyd 	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
1377adb05bbSStephen Boyd 	if (!header)
138a28aad66SStephen Boyd 		return -ENOMEM;
139a28aad66SStephen Boyd 
1407adb05bbSStephen Boyd 	len = header->header_bytes + header->table_bytes;
1417adb05bbSStephen Boyd 	ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
142a7d9b5f0SStephen Boyd 	memunmap(header);
1437adb05bbSStephen Boyd 	if (ret) {
1447adb05bbSStephen Boyd 		dev_warn(dev, "coreboot table missing or corrupt!\n");
1457adb05bbSStephen Boyd 		return -ENODEV;
1467adb05bbSStephen Boyd 	}
1477adb05bbSStephen Boyd 
1487adb05bbSStephen Boyd 	ptr = memremap(res->start, len, MEMREMAP_WB);
149a28aad66SStephen Boyd 	if (!ptr)
150a28aad66SStephen Boyd 		return -ENOMEM;
151a28aad66SStephen Boyd 
1527adb05bbSStephen Boyd 	ret = bus_register(&coreboot_bus_type);
1537adb05bbSStephen Boyd 	if (!ret) {
1547adb05bbSStephen Boyd 		ret = coreboot_table_populate(dev, ptr);
1557adb05bbSStephen Boyd 		if (ret)
1567adb05bbSStephen Boyd 			bus_unregister(&coreboot_bus_type);
1577adb05bbSStephen Boyd 	}
1587adb05bbSStephen Boyd 	memunmap(ptr);
1597adb05bbSStephen Boyd 
1607adb05bbSStephen Boyd 	return ret;
161a28aad66SStephen Boyd }
162a28aad66SStephen Boyd 
163cae0970eSPatrick Rudolph static int __cb_dev_unregister(struct device *dev, void *dummy)
164cae0970eSPatrick Rudolph {
165cae0970eSPatrick Rudolph 	device_unregister(dev);
166cae0970eSPatrick Rudolph 	return 0;
167cae0970eSPatrick Rudolph }
168cae0970eSPatrick Rudolph 
169a28aad66SStephen Boyd static int coreboot_table_remove(struct platform_device *pdev)
170d384d6f4SThierry Escande {
171cae0970eSPatrick Rudolph 	bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
172570d30c2SSamuel Holland 	bus_unregister(&coreboot_bus_type);
173d384d6f4SThierry Escande 	return 0;
174d384d6f4SThierry Escande }
175d384d6f4SThierry Escande 
176a28aad66SStephen Boyd #ifdef CONFIG_ACPI
177a28aad66SStephen Boyd static const struct acpi_device_id cros_coreboot_acpi_match[] = {
178a28aad66SStephen Boyd 	{ "GOOGCB00", 0 },
179a28aad66SStephen Boyd 	{ "BOOT0000", 0 },
180a28aad66SStephen Boyd 	{ }
181a28aad66SStephen Boyd };
182a28aad66SStephen Boyd MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
183a28aad66SStephen Boyd #endif
184a28aad66SStephen Boyd 
185a28aad66SStephen Boyd #ifdef CONFIG_OF
186a28aad66SStephen Boyd static const struct of_device_id coreboot_of_match[] = {
187a28aad66SStephen Boyd 	{ .compatible = "coreboot" },
188a28aad66SStephen Boyd 	{}
189a28aad66SStephen Boyd };
190a28aad66SStephen Boyd MODULE_DEVICE_TABLE(of, coreboot_of_match);
191a28aad66SStephen Boyd #endif
192a28aad66SStephen Boyd 
193a28aad66SStephen Boyd static struct platform_driver coreboot_table_driver = {
194a28aad66SStephen Boyd 	.probe = coreboot_table_probe,
195a28aad66SStephen Boyd 	.remove = coreboot_table_remove,
196a28aad66SStephen Boyd 	.driver = {
197a28aad66SStephen Boyd 		.name = "coreboot_table",
198a28aad66SStephen Boyd 		.acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
199a28aad66SStephen Boyd 		.of_match_table = of_match_ptr(coreboot_of_match),
200a28aad66SStephen Boyd 	},
201a28aad66SStephen Boyd };
202a28aad66SStephen Boyd module_platform_driver(coreboot_table_driver);
203d384d6f4SThierry Escande MODULE_AUTHOR("Google, Inc.");
204d384d6f4SThierry Escande MODULE_LICENSE("GPL");
205