Linux-libre 4.19.123-gnu
[librecmc/linux-libre.git] / drivers / firmware / google / coreboot_table.c
1 /*
2  * coreboot_table.c
3  *
4  * Module providing coreboot table access.
5  *
6  * Copyright 2017 Google Inc.
7  * Copyright 2017 Samuel Holland <samuel@sholland.org>
8  *
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.
12  *
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.
17  */
18
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>
26
27 #include "coreboot_table.h"
28
29 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
30 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
31
32 static struct coreboot_table_header __iomem *ptr_header;
33
34 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
35 {
36         struct coreboot_device *device = CB_DEV(dev);
37         struct coreboot_driver *driver = CB_DRV(drv);
38
39         return device->entry.tag == driver->tag;
40 }
41
42 static int coreboot_bus_probe(struct device *dev)
43 {
44         int ret = -ENODEV;
45         struct coreboot_device *device = CB_DEV(dev);
46         struct coreboot_driver *driver = CB_DRV(dev->driver);
47
48         if (driver->probe)
49                 ret = driver->probe(device);
50
51         return ret;
52 }
53
54 static int coreboot_bus_remove(struct device *dev)
55 {
56         int ret = 0;
57         struct coreboot_device *device = CB_DEV(dev);
58         struct coreboot_driver *driver = CB_DRV(dev->driver);
59
60         if (driver->remove)
61                 ret = driver->remove(device);
62
63         return ret;
64 }
65
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,
71 };
72
73 static int __init coreboot_bus_init(void)
74 {
75         return bus_register(&coreboot_bus_type);
76 }
77 module_init(coreboot_bus_init);
78
79 static void coreboot_device_release(struct device *dev)
80 {
81         struct coreboot_device *device = CB_DEV(dev);
82
83         kfree(device);
84 }
85
86 int coreboot_driver_register(struct coreboot_driver *driver)
87 {
88         driver->drv.bus = &coreboot_bus_type;
89
90         return driver_register(&driver->drv);
91 }
92 EXPORT_SYMBOL(coreboot_driver_register);
93
94 void coreboot_driver_unregister(struct coreboot_driver *driver)
95 {
96         driver_unregister(&driver->drv);
97 }
98 EXPORT_SYMBOL(coreboot_driver_unregister);
99
100 int coreboot_table_init(struct device *dev, void __iomem *ptr)
101 {
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;
107
108         ptr_header = ptr;
109         memcpy_fromio(&header, ptr_header, sizeof(header));
110
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;
115         }
116
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));
120
121                 device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL);
122                 if (!device) {
123                         ret = -ENOMEM;
124                         break;
125                 }
126
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);
132
133                 ret = device_register(&device->dev);
134                 if (ret) {
135                         put_device(&device->dev);
136                         break;
137                 }
138
139                 ptr_entry += entry.size;
140         }
141 out:
142         iounmap(ptr);
143         return ret;
144 }
145 EXPORT_SYMBOL(coreboot_table_init);
146
147 int coreboot_table_exit(void)
148 {
149         if (ptr_header) {
150                 bus_unregister(&coreboot_bus_type);
151                 ptr_header = NULL;
152         }
153
154         return 0;
155 }
156 EXPORT_SYMBOL(coreboot_table_exit);
157
158 MODULE_AUTHOR("Google, Inc.");
159 MODULE_LICENSE("GPL");