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>
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
)
45 struct coreboot_device
*device
= CB_DEV(dev
);
46 struct coreboot_driver
*driver
= CB_DRV(dev
->driver
);
49 ret
= driver
->probe(device
);
54 static int coreboot_bus_remove(struct device
*dev
)
57 struct coreboot_device
*device
= CB_DEV(dev
);
58 struct coreboot_driver
*driver
= CB_DRV(dev
->driver
);
61 ret
= driver
->remove(device
);
66 static struct bus_type coreboot_bus_type
= {
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
);
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
)
104 struct coreboot_device
*device
;
105 struct coreboot_table_entry entry
;
106 struct coreboot_table_header header
;
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");
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
);
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
);
135 put_device(&device
->dev
);
139 ptr_entry
+= entry
.size
;
145 EXPORT_SYMBOL(coreboot_table_init
);
147 int coreboot_table_exit(void)
150 bus_unregister(&coreboot_bus_type
);
156 EXPORT_SYMBOL(coreboot_table_exit
);
158 MODULE_AUTHOR("Google, Inc.");
159 MODULE_LICENSE("GPL");