at v4.19-rc4 158 lines 3.8 kB view raw
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 32static struct coreboot_table_header __iomem *ptr_header; 33 34static 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 42static 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 54static 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 66static 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 73static int __init coreboot_bus_init(void) 74{ 75 return bus_register(&coreboot_bus_type); 76} 77module_init(coreboot_bus_init); 78 79static void coreboot_device_release(struct device *dev) 80{ 81 struct coreboot_device *device = CB_DEV(dev); 82 83 kfree(device); 84} 85 86int coreboot_driver_register(struct coreboot_driver *driver) 87{ 88 driver->drv.bus = &coreboot_bus_type; 89 90 return driver_register(&driver->drv); 91} 92EXPORT_SYMBOL(coreboot_driver_register); 93 94void coreboot_driver_unregister(struct coreboot_driver *driver) 95{ 96 driver_unregister(&driver->drv); 97} 98EXPORT_SYMBOL(coreboot_driver_unregister); 99 100int 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 return -ENODEV; 114 } 115 116 ptr_entry = (void *)ptr_header + header.header_bytes; 117 for (i = 0; i < header.table_entries; i++) { 118 memcpy_fromio(&entry, ptr_entry, sizeof(entry)); 119 120 device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL); 121 if (!device) { 122 ret = -ENOMEM; 123 break; 124 } 125 126 dev_set_name(&device->dev, "coreboot%d", i); 127 device->dev.parent = dev; 128 device->dev.bus = &coreboot_bus_type; 129 device->dev.release = coreboot_device_release; 130 memcpy_fromio(&device->entry, ptr_entry, entry.size); 131 132 ret = device_register(&device->dev); 133 if (ret) { 134 put_device(&device->dev); 135 break; 136 } 137 138 ptr_entry += entry.size; 139 } 140 141 return ret; 142} 143EXPORT_SYMBOL(coreboot_table_init); 144 145int coreboot_table_exit(void) 146{ 147 if (ptr_header) { 148 bus_unregister(&coreboot_bus_type); 149 iounmap(ptr_header); 150 ptr_header = NULL; 151 } 152 153 return 0; 154} 155EXPORT_SYMBOL(coreboot_table_exit); 156 157MODULE_AUTHOR("Google, Inc."); 158MODULE_LICENSE("GPL");