Merge tag 'for-linus-5.12b-rc3-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / firmware / google / coreboot_table.c
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
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
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
47 static int 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         return 0;
56 }
57
58 static struct bus_type coreboot_bus_type = {
59         .name           = "coreboot",
60         .match          = coreboot_bus_match,
61         .probe          = coreboot_bus_probe,
62         .remove         = coreboot_bus_remove,
63 };
64
65 static void coreboot_device_release(struct device *dev)
66 {
67         struct coreboot_device *device = CB_DEV(dev);
68
69         kfree(device);
70 }
71
72 int coreboot_driver_register(struct coreboot_driver *driver)
73 {
74         driver->drv.bus = &coreboot_bus_type;
75
76         return driver_register(&driver->drv);
77 }
78 EXPORT_SYMBOL(coreboot_driver_register);
79
80 void coreboot_driver_unregister(struct coreboot_driver *driver)
81 {
82         driver_unregister(&driver->drv);
83 }
84 EXPORT_SYMBOL(coreboot_driver_unregister);
85
86 static int coreboot_table_populate(struct device *dev, void *ptr)
87 {
88         int i, ret;
89         void *ptr_entry;
90         struct coreboot_device *device;
91         struct coreboot_table_entry *entry;
92         struct coreboot_table_header *header = ptr;
93
94         ptr_entry = ptr + header->header_bytes;
95         for (i = 0; i < header->table_entries; i++) {
96                 entry = ptr_entry;
97
98                 device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
99                 if (!device)
100                         return -ENOMEM;
101
102                 dev_set_name(&device->dev, "coreboot%d", i);
103                 device->dev.parent = dev;
104                 device->dev.bus = &coreboot_bus_type;
105                 device->dev.release = coreboot_device_release;
106                 memcpy(&device->entry, ptr_entry, entry->size);
107
108                 ret = device_register(&device->dev);
109                 if (ret) {
110                         put_device(&device->dev);
111                         return ret;
112                 }
113
114                 ptr_entry += entry->size;
115         }
116
117         return 0;
118 }
119
120 static int coreboot_table_probe(struct platform_device *pdev)
121 {
122         resource_size_t len;
123         struct coreboot_table_header *header;
124         struct resource *res;
125         struct device *dev = &pdev->dev;
126         void *ptr;
127         int ret;
128
129         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130         if (!res)
131                 return -EINVAL;
132
133         len = resource_size(res);
134         if (!res->start || !len)
135                 return -EINVAL;
136
137         /* Check just the header first to make sure things are sane */
138         header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
139         if (!header)
140                 return -ENOMEM;
141
142         len = header->header_bytes + header->table_bytes;
143         ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
144         memunmap(header);
145         if (ret) {
146                 dev_warn(dev, "coreboot table missing or corrupt!\n");
147                 return -ENODEV;
148         }
149
150         ptr = memremap(res->start, len, MEMREMAP_WB);
151         if (!ptr)
152                 return -ENOMEM;
153
154         ret = bus_register(&coreboot_bus_type);
155         if (!ret) {
156                 ret = coreboot_table_populate(dev, ptr);
157                 if (ret)
158                         bus_unregister(&coreboot_bus_type);
159         }
160         memunmap(ptr);
161
162         return ret;
163 }
164
165 static int __cb_dev_unregister(struct device *dev, void *dummy)
166 {
167         device_unregister(dev);
168         return 0;
169 }
170
171 static int coreboot_table_remove(struct platform_device *pdev)
172 {
173         bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
174         bus_unregister(&coreboot_bus_type);
175         return 0;
176 }
177
178 #ifdef CONFIG_ACPI
179 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
180         { "GOOGCB00", 0 },
181         { "BOOT0000", 0 },
182         { }
183 };
184 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
185 #endif
186
187 #ifdef CONFIG_OF
188 static const struct of_device_id coreboot_of_match[] = {
189         { .compatible = "coreboot" },
190         {}
191 };
192 MODULE_DEVICE_TABLE(of, coreboot_of_match);
193 #endif
194
195 static struct platform_driver coreboot_table_driver = {
196         .probe = coreboot_table_probe,
197         .remove = coreboot_table_remove,
198         .driver = {
199                 .name = "coreboot_table",
200                 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
201                 .of_match_table = of_match_ptr(coreboot_of_match),
202         },
203 };
204 module_platform_driver(coreboot_table_driver);
205 MODULE_AUTHOR("Google, Inc.");
206 MODULE_LICENSE("GPL");