Linux 4.19.133
[linux/fpc-iii.git] / drivers / firmware / google / coreboot_table.c
blob898bb9abc41f153204284a625edea91e4631b3fd
1 /*
2 * coreboot_table.c
4 * Module providing coreboot table access.
6 * Copyright 2017 Google Inc.
7 * Copyright 2017 Samuel Holland <samuel@sholland.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License v2.0 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include "coreboot_table.h"
29 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
30 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
32 static struct coreboot_table_header __iomem *ptr_header;
34 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
36 struct coreboot_device *device = CB_DEV(dev);
37 struct coreboot_driver *driver = CB_DRV(drv);
39 return device->entry.tag == driver->tag;
42 static int coreboot_bus_probe(struct device *dev)
44 int ret = -ENODEV;
45 struct coreboot_device *device = CB_DEV(dev);
46 struct coreboot_driver *driver = CB_DRV(dev->driver);
48 if (driver->probe)
49 ret = driver->probe(device);
51 return ret;
54 static int coreboot_bus_remove(struct device *dev)
56 int ret = 0;
57 struct coreboot_device *device = CB_DEV(dev);
58 struct coreboot_driver *driver = CB_DRV(dev->driver);
60 if (driver->remove)
61 ret = driver->remove(device);
63 return ret;
66 static struct bus_type coreboot_bus_type = {
67 .name = "coreboot",
68 .match = coreboot_bus_match,
69 .probe = coreboot_bus_probe,
70 .remove = coreboot_bus_remove,
73 static int __init coreboot_bus_init(void)
75 return bus_register(&coreboot_bus_type);
77 module_init(coreboot_bus_init);
79 static void coreboot_device_release(struct device *dev)
81 struct coreboot_device *device = CB_DEV(dev);
83 kfree(device);
86 int coreboot_driver_register(struct coreboot_driver *driver)
88 driver->drv.bus = &coreboot_bus_type;
90 return driver_register(&driver->drv);
92 EXPORT_SYMBOL(coreboot_driver_register);
94 void coreboot_driver_unregister(struct coreboot_driver *driver)
96 driver_unregister(&driver->drv);
98 EXPORT_SYMBOL(coreboot_driver_unregister);
100 int coreboot_table_init(struct device *dev, void __iomem *ptr)
102 int i, ret;
103 void *ptr_entry;
104 struct coreboot_device *device;
105 struct coreboot_table_entry entry;
106 struct coreboot_table_header header;
108 ptr_header = ptr;
109 memcpy_fromio(&header, ptr_header, sizeof(header));
111 if (strncmp(header.signature, "LBIO", sizeof(header.signature))) {
112 pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
113 ret = -ENODEV;
114 goto out;
117 ptr_entry = (void *)ptr_header + header.header_bytes;
118 for (i = 0; i < header.table_entries; i++) {
119 memcpy_fromio(&entry, ptr_entry, sizeof(entry));
121 device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL);
122 if (!device) {
123 ret = -ENOMEM;
124 break;
127 dev_set_name(&device->dev, "coreboot%d", i);
128 device->dev.parent = dev;
129 device->dev.bus = &coreboot_bus_type;
130 device->dev.release = coreboot_device_release;
131 memcpy_fromio(&device->entry, ptr_entry, entry.size);
133 ret = device_register(&device->dev);
134 if (ret) {
135 put_device(&device->dev);
136 break;
139 ptr_entry += entry.size;
141 out:
142 iounmap(ptr);
143 return ret;
145 EXPORT_SYMBOL(coreboot_table_init);
147 int coreboot_table_exit(void)
149 if (ptr_header) {
150 bus_unregister(&coreboot_bus_type);
151 ptr_header = NULL;
154 return 0;
156 EXPORT_SYMBOL(coreboot_table_exit);
158 MODULE_AUTHOR("Google, Inc.");
159 MODULE_LICENSE("GPL");