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 
47570d30c2SSamuel Holland static int coreboot_bus_remove(struct device *dev)
48570d30c2SSamuel Holland {
49570d30c2SSamuel Holland 	int ret = 0;
50570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
51570d30c2SSamuel Holland 	struct coreboot_driver *driver = CB_DRV(dev->driver);
52570d30c2SSamuel Holland 
53570d30c2SSamuel Holland 	if (driver->remove)
54570d30c2SSamuel Holland 		ret = driver->remove(device);
55570d30c2SSamuel Holland 
56570d30c2SSamuel Holland 	return ret;
57570d30c2SSamuel Holland }
58570d30c2SSamuel Holland 
59570d30c2SSamuel Holland static struct bus_type coreboot_bus_type = {
60570d30c2SSamuel Holland 	.name		= "coreboot",
61570d30c2SSamuel Holland 	.match		= coreboot_bus_match,
62570d30c2SSamuel Holland 	.probe		= coreboot_bus_probe,
63570d30c2SSamuel Holland 	.remove		= coreboot_bus_remove,
64570d30c2SSamuel Holland };
65570d30c2SSamuel Holland 
66570d30c2SSamuel Holland static void coreboot_device_release(struct device *dev)
67570d30c2SSamuel Holland {
68570d30c2SSamuel Holland 	struct coreboot_device *device = CB_DEV(dev);
69570d30c2SSamuel Holland 
70570d30c2SSamuel Holland 	kfree(device);
71570d30c2SSamuel Holland }
72570d30c2SSamuel Holland 
73570d30c2SSamuel Holland int coreboot_driver_register(struct coreboot_driver *driver)
74570d30c2SSamuel Holland {
75570d30c2SSamuel Holland 	driver->drv.bus = &coreboot_bus_type;
76570d30c2SSamuel Holland 
77570d30c2SSamuel Holland 	return driver_register(&driver->drv);
78570d30c2SSamuel Holland }
79570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_register);
80570d30c2SSamuel Holland 
81570d30c2SSamuel Holland void coreboot_driver_unregister(struct coreboot_driver *driver)
82570d30c2SSamuel Holland {
83570d30c2SSamuel Holland 	driver_unregister(&driver->drv);
84570d30c2SSamuel Holland }
85570d30c2SSamuel Holland EXPORT_SYMBOL(coreboot_driver_unregister);
86570d30c2SSamuel Holland 
877adb05bbSStephen Boyd static int coreboot_table_populate(struct device *dev, void *ptr)
88d384d6f4SThierry Escande {
89570d30c2SSamuel Holland 	int i, ret;
90570d30c2SSamuel Holland 	void *ptr_entry;
91570d30c2SSamuel Holland 	struct coreboot_device *device;
92a7d9b5f0SStephen Boyd 	struct coreboot_table_entry *entry;
937adb05bbSStephen Boyd 	struct coreboot_table_header *header = ptr;
94d384d6f4SThierry Escande 
957adb05bbSStephen Boyd 	ptr_entry = ptr + header->header_bytes;
96a7d9b5f0SStephen Boyd 	for (i = 0; i < header->table_entries; i++) {
97a7d9b5f0SStephen Boyd 		entry = ptr_entry;
98570d30c2SSamuel Holland 
99a7d9b5f0SStephen Boyd 		device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
1007adb05bbSStephen Boyd 		if (!device)
1017adb05bbSStephen Boyd 			return -ENOMEM;
102570d30c2SSamuel Holland 
103570d30c2SSamuel Holland 		dev_set_name(&device->dev, "coreboot%d", i);
104570d30c2SSamuel Holland 		device->dev.parent = dev;
105570d30c2SSamuel Holland 		device->dev.bus = &coreboot_bus_type;
106570d30c2SSamuel Holland 		device->dev.release = coreboot_device_release;
107a7d9b5f0SStephen Boyd 		memcpy(&device->entry, ptr_entry, entry->size);
108570d30c2SSamuel Holland 
109570d30c2SSamuel Holland 		ret = device_register(&device->dev);
110570d30c2SSamuel Holland 		if (ret) {
111570d30c2SSamuel Holland 			put_device(&device->dev);
1127adb05bbSStephen Boyd 			return ret;
113570d30c2SSamuel Holland 		}
114570d30c2SSamuel Holland 
115a7d9b5f0SStephen Boyd 		ptr_entry += entry->size;
116570d30c2SSamuel Holland 	}
117b81e3140SStephen Boyd 
1187adb05bbSStephen Boyd 	return 0;
119d384d6f4SThierry Escande }
120d384d6f4SThierry Escande 
121a28aad66SStephen Boyd static int coreboot_table_probe(struct platform_device *pdev)
122a28aad66SStephen Boyd {
123a28aad66SStephen Boyd 	resource_size_t len;
124a7d9b5f0SStephen Boyd 	struct coreboot_table_header *header;
125a28aad66SStephen Boyd 	struct resource *res;
1267adb05bbSStephen Boyd 	struct device *dev = &pdev->dev;
127a7d9b5f0SStephen Boyd 	void *ptr;
1287adb05bbSStephen Boyd 	int ret;
129a28aad66SStephen Boyd 
130a28aad66SStephen Boyd 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131a28aad66SStephen Boyd 	if (!res)
132a28aad66SStephen Boyd 		return -EINVAL;
133a28aad66SStephen Boyd 
134a28aad66SStephen Boyd 	len = resource_size(res);
135a28aad66SStephen Boyd 	if (!res->start || !len)
136a28aad66SStephen Boyd 		return -EINVAL;
137a28aad66SStephen Boyd 
1387adb05bbSStephen Boyd 	/* Check just the header first to make sure things are sane */
139a7d9b5f0SStephen Boyd 	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
1407adb05bbSStephen Boyd 	if (!header)
141a28aad66SStephen Boyd 		return -ENOMEM;
142a28aad66SStephen Boyd 
1437adb05bbSStephen Boyd 	len = header->header_bytes + header->table_bytes;
1447adb05bbSStephen Boyd 	ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
145a7d9b5f0SStephen Boyd 	memunmap(header);
1467adb05bbSStephen Boyd 	if (ret) {
1477adb05bbSStephen Boyd 		dev_warn(dev, "coreboot table missing or corrupt!\n");
1487adb05bbSStephen Boyd 		return -ENODEV;
1497adb05bbSStephen Boyd 	}
1507adb05bbSStephen Boyd 
1517adb05bbSStephen Boyd 	ptr = memremap(res->start, len, MEMREMAP_WB);
152a28aad66SStephen Boyd 	if (!ptr)
153a28aad66SStephen Boyd 		return -ENOMEM;
154a28aad66SStephen Boyd 
1557adb05bbSStephen Boyd 	ret = bus_register(&coreboot_bus_type);
1567adb05bbSStephen Boyd 	if (!ret) {
1577adb05bbSStephen Boyd 		ret = coreboot_table_populate(dev, ptr);
1587adb05bbSStephen Boyd 		if (ret)
1597adb05bbSStephen Boyd 			bus_unregister(&coreboot_bus_type);
1607adb05bbSStephen Boyd 	}
1617adb05bbSStephen Boyd 	memunmap(ptr);
1627adb05bbSStephen Boyd 
1637adb05bbSStephen Boyd 	return ret;
164a28aad66SStephen Boyd }
165a28aad66SStephen Boyd 
166a28aad66SStephen Boyd static int coreboot_table_remove(struct platform_device *pdev)
167d384d6f4SThierry Escande {
168570d30c2SSamuel Holland 	bus_unregister(&coreboot_bus_type);
169d384d6f4SThierry Escande 	return 0;
170d384d6f4SThierry Escande }
171d384d6f4SThierry Escande 
172a28aad66SStephen Boyd #ifdef CONFIG_ACPI
173a28aad66SStephen Boyd static const struct acpi_device_id cros_coreboot_acpi_match[] = {
174a28aad66SStephen Boyd 	{ "GOOGCB00", 0 },
175a28aad66SStephen Boyd 	{ "BOOT0000", 0 },
176a28aad66SStephen Boyd 	{ }
177a28aad66SStephen Boyd };
178a28aad66SStephen Boyd MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
179a28aad66SStephen Boyd #endif
180a28aad66SStephen Boyd 
181a28aad66SStephen Boyd #ifdef CONFIG_OF
182a28aad66SStephen Boyd static const struct of_device_id coreboot_of_match[] = {
183a28aad66SStephen Boyd 	{ .compatible = "coreboot" },
184a28aad66SStephen Boyd 	{}
185a28aad66SStephen Boyd };
186a28aad66SStephen Boyd MODULE_DEVICE_TABLE(of, coreboot_of_match);
187a28aad66SStephen Boyd #endif
188a28aad66SStephen Boyd 
189a28aad66SStephen Boyd static struct platform_driver coreboot_table_driver = {
190a28aad66SStephen Boyd 	.probe = coreboot_table_probe,
191a28aad66SStephen Boyd 	.remove = coreboot_table_remove,
192a28aad66SStephen Boyd 	.driver = {
193a28aad66SStephen Boyd 		.name = "coreboot_table",
194a28aad66SStephen Boyd 		.acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
195a28aad66SStephen Boyd 		.of_match_table = of_match_ptr(coreboot_of_match),
196a28aad66SStephen Boyd 	},
197a28aad66SStephen Boyd };
198a28aad66SStephen Boyd module_platform_driver(coreboot_table_driver);
199d384d6f4SThierry Escande MODULE_AUTHOR("Google, Inc.");
200d384d6f4SThierry Escande MODULE_LICENSE("GPL");
201