Path: blob/master/drivers/firmware/google/coreboot_table.c
26428 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* coreboot_table.c3*4* Module providing coreboot table access.5*6* Copyright 2017 Google Inc.7* Copyright 2017 Samuel Holland <[email protected]>8*/910#include <linux/acpi.h>11#include <linux/device.h>12#include <linux/err.h>13#include <linux/init.h>14#include <linux/io.h>15#include <linux/kernel.h>16#include <linux/module.h>17#include <linux/of.h>18#include <linux/platform_device.h>19#include <linux/slab.h>2021#include "coreboot_table.h"2223#define CB_DEV(d) container_of(d, struct coreboot_device, dev)24#define CB_DRV(d) container_of_const(d, struct coreboot_driver, drv)2526static int coreboot_bus_match(struct device *dev, const struct device_driver *drv)27{28struct coreboot_device *device = CB_DEV(dev);29const struct coreboot_driver *driver = CB_DRV(drv);30const struct coreboot_device_id *id;3132if (!driver->id_table)33return 0;3435for (id = driver->id_table; id->tag; id++) {36if (device->entry.tag == id->tag)37return 1;38}3940return 0;41}4243static int coreboot_bus_probe(struct device *dev)44{45int ret = -ENODEV;46struct coreboot_device *device = CB_DEV(dev);47struct coreboot_driver *driver = CB_DRV(dev->driver);4849if (driver->probe)50ret = driver->probe(device);5152return ret;53}5455static void coreboot_bus_remove(struct device *dev)56{57struct coreboot_device *device = CB_DEV(dev);58struct coreboot_driver *driver = CB_DRV(dev->driver);5960if (driver->remove)61driver->remove(device);62}6364static int coreboot_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)65{66struct coreboot_device *device = CB_DEV(dev);67u32 tag = device->entry.tag;6869return add_uevent_var(env, "MODALIAS=coreboot:t%08X", tag);70}7172static const struct bus_type coreboot_bus_type = {73.name = "coreboot",74.match = coreboot_bus_match,75.probe = coreboot_bus_probe,76.remove = coreboot_bus_remove,77.uevent = coreboot_bus_uevent,78};7980static void coreboot_device_release(struct device *dev)81{82struct coreboot_device *device = CB_DEV(dev);8384kfree(device);85}8687int __coreboot_driver_register(struct coreboot_driver *driver,88struct module *owner)89{90driver->drv.bus = &coreboot_bus_type;91driver->drv.owner = owner;9293return driver_register(&driver->drv);94}95EXPORT_SYMBOL(__coreboot_driver_register);9697void coreboot_driver_unregister(struct coreboot_driver *driver)98{99driver_unregister(&driver->drv);100}101EXPORT_SYMBOL(coreboot_driver_unregister);102103static int coreboot_table_populate(struct device *dev, void *ptr)104{105int i, ret;106void *ptr_entry;107struct coreboot_device *device;108struct coreboot_table_entry *entry;109struct coreboot_table_header *header = ptr;110111ptr_entry = ptr + header->header_bytes;112for (i = 0; i < header->table_entries; i++) {113entry = ptr_entry;114115if (entry->size < sizeof(*entry)) {116dev_warn(dev, "coreboot table entry too small!\n");117return -EINVAL;118}119120device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);121if (!device)122return -ENOMEM;123124device->dev.parent = dev;125device->dev.bus = &coreboot_bus_type;126device->dev.release = coreboot_device_release;127memcpy(device->raw, ptr_entry, entry->size);128129switch (device->entry.tag) {130case LB_TAG_CBMEM_ENTRY:131dev_set_name(&device->dev, "cbmem-%08x",132device->cbmem_entry.id);133break;134default:135dev_set_name(&device->dev, "coreboot%d", i);136break;137}138139ret = device_register(&device->dev);140if (ret) {141put_device(&device->dev);142return ret;143}144145ptr_entry += entry->size;146}147148return 0;149}150151static int coreboot_table_probe(struct platform_device *pdev)152{153resource_size_t len;154struct coreboot_table_header *header;155struct resource *res;156struct device *dev = &pdev->dev;157void *ptr;158int ret;159160res = platform_get_resource(pdev, IORESOURCE_MEM, 0);161if (!res)162return -EINVAL;163164len = resource_size(res);165if (!res->start || !len)166return -EINVAL;167168/* Check just the header first to make sure things are sane */169header = memremap(res->start, sizeof(*header), MEMREMAP_WB);170if (!header)171return -ENOMEM;172173len = header->header_bytes + header->table_bytes;174ret = strncmp(header->signature, "LBIO", sizeof(header->signature));175memunmap(header);176if (ret) {177dev_warn(dev, "coreboot table missing or corrupt!\n");178return -ENODEV;179}180181ptr = memremap(res->start, len, MEMREMAP_WB);182if (!ptr)183return -ENOMEM;184185ret = coreboot_table_populate(dev, ptr);186187memunmap(ptr);188189return ret;190}191192static int __cb_dev_unregister(struct device *dev, void *dummy)193{194device_unregister(dev);195return 0;196}197198static void coreboot_table_remove(struct platform_device *pdev)199{200bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);201}202203#ifdef CONFIG_ACPI204static const struct acpi_device_id cros_coreboot_acpi_match[] = {205{ "GOOGCB00", 0 },206{ "BOOT0000", 0 },207{ }208};209MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);210#endif211212#ifdef CONFIG_OF213static const struct of_device_id coreboot_of_match[] = {214{ .compatible = "coreboot" },215{}216};217MODULE_DEVICE_TABLE(of, coreboot_of_match);218#endif219220static struct platform_driver coreboot_table_driver = {221.probe = coreboot_table_probe,222.remove = coreboot_table_remove,223.driver = {224.name = "coreboot_table",225.acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),226.of_match_table = of_match_ptr(coreboot_of_match),227},228};229230static int __init coreboot_table_driver_init(void)231{232int ret;233234ret = bus_register(&coreboot_bus_type);235if (ret)236return ret;237238ret = platform_driver_register(&coreboot_table_driver);239if (ret) {240bus_unregister(&coreboot_bus_type);241return ret;242}243244return 0;245}246247static void __exit coreboot_table_driver_exit(void)248{249platform_driver_unregister(&coreboot_table_driver);250bus_unregister(&coreboot_bus_type);251}252253module_init(coreboot_table_driver_init);254module_exit(coreboot_table_driver_exit);255256MODULE_AUTHOR("Google, Inc.");257MODULE_DESCRIPTION("Module providing coreboot table access");258MODULE_LICENSE("GPL");259260261