coreboot_table.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * coreboot_table.c
  4. *
  5. * Module providing coreboot table access.
  6. *
  7. * Copyright 2017 Google Inc.
  8. * Copyright 2017 Samuel Holland <samuel@sholland.org>
  9. */
  10. #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>
  20. #include "coreboot_table.h"
  21. #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
  22. #define CB_DRV(d) container_of_const(d, struct coreboot_driver, drv)
  23. static int coreboot_bus_match(struct device *dev, const struct device_driver *drv)
  24. {
  25. struct coreboot_device *device = CB_DEV(dev);
  26. const struct coreboot_driver *driver = CB_DRV(drv);
  27. const struct coreboot_device_id *id;
  28. if (!driver->id_table)
  29. return 0;
  30. for (id = driver->id_table; id->tag; id++) {
  31. if (device->entry.tag == id->tag)
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. static int coreboot_bus_probe(struct device *dev)
  37. {
  38. int ret = -ENODEV;
  39. struct coreboot_device *device = CB_DEV(dev);
  40. struct coreboot_driver *driver = CB_DRV(dev->driver);
  41. if (driver->probe)
  42. ret = driver->probe(device);
  43. return ret;
  44. }
  45. static void coreboot_bus_remove(struct device *dev)
  46. {
  47. struct coreboot_device *device = CB_DEV(dev);
  48. struct coreboot_driver *driver = CB_DRV(dev->driver);
  49. if (driver->remove)
  50. driver->remove(device);
  51. }
  52. static int coreboot_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  53. {
  54. struct coreboot_device *device = CB_DEV(dev);
  55. u32 tag = device->entry.tag;
  56. return add_uevent_var(env, "MODALIAS=coreboot:t%08X", tag);
  57. }
  58. static const struct bus_type coreboot_bus_type = {
  59. .name = "coreboot",
  60. .match = coreboot_bus_match,
  61. .probe = coreboot_bus_probe,
  62. .remove = coreboot_bus_remove,
  63. .uevent = coreboot_bus_uevent,
  64. };
  65. static void coreboot_device_release(struct device *dev)
  66. {
  67. struct coreboot_device *device = CB_DEV(dev);
  68. kfree(device);
  69. }
  70. int __coreboot_driver_register(struct coreboot_driver *driver,
  71. struct module *owner)
  72. {
  73. driver->drv.bus = &coreboot_bus_type;
  74. driver->drv.owner = owner;
  75. return driver_register(&driver->drv);
  76. }
  77. EXPORT_SYMBOL(__coreboot_driver_register);
  78. void coreboot_driver_unregister(struct coreboot_driver *driver)
  79. {
  80. driver_unregister(&driver->drv);
  81. }
  82. EXPORT_SYMBOL(coreboot_driver_unregister);
  83. static int coreboot_table_populate(struct device *dev, void *ptr)
  84. {
  85. int i, ret;
  86. void *ptr_entry;
  87. struct coreboot_device *device;
  88. struct coreboot_table_entry *entry;
  89. struct coreboot_table_header *header = ptr;
  90. ptr_entry = ptr + header->header_bytes;
  91. for (i = 0; i < header->table_entries; i++) {
  92. entry = ptr_entry;
  93. if (entry->size < sizeof(*entry)) {
  94. dev_warn(dev, "coreboot table entry too small!\n");
  95. return -EINVAL;
  96. }
  97. device = kzalloc(sizeof(device->dev) + entry->size, GFP_KERNEL);
  98. if (!device)
  99. return -ENOMEM;
  100. device->dev.parent = dev;
  101. device->dev.bus = &coreboot_bus_type;
  102. device->dev.release = coreboot_device_release;
  103. memcpy(device->raw, ptr_entry, entry->size);
  104. switch (device->entry.tag) {
  105. case LB_TAG_CBMEM_ENTRY:
  106. dev_set_name(&device->dev, "cbmem-%08x",
  107. device->cbmem_entry.id);
  108. break;
  109. default:
  110. dev_set_name(&device->dev, "coreboot%d", i);
  111. break;
  112. }
  113. ret = device_register(&device->dev);
  114. if (ret) {
  115. put_device(&device->dev);
  116. return ret;
  117. }
  118. ptr_entry += entry->size;
  119. }
  120. return 0;
  121. }
  122. static int coreboot_table_probe(struct platform_device *pdev)
  123. {
  124. resource_size_t len;
  125. struct coreboot_table_header *header;
  126. struct resource *res;
  127. struct device *dev = &pdev->dev;
  128. void *ptr;
  129. int ret;
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. if (!res)
  132. return -EINVAL;
  133. len = resource_size(res);
  134. if (!res->start || !len)
  135. return -EINVAL;
  136. /* Check just the header first to make sure things are sane */
  137. header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
  138. if (!header)
  139. return -ENOMEM;
  140. len = header->header_bytes + header->table_bytes;
  141. ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
  142. memunmap(header);
  143. if (ret) {
  144. dev_warn(dev, "coreboot table missing or corrupt!\n");
  145. return -ENODEV;
  146. }
  147. ptr = memremap(res->start, len, MEMREMAP_WB);
  148. if (!ptr)
  149. return -ENOMEM;
  150. ret = coreboot_table_populate(dev, ptr);
  151. memunmap(ptr);
  152. return ret;
  153. }
  154. static int __cb_dev_unregister(struct device *dev, void *dummy)
  155. {
  156. device_unregister(dev);
  157. return 0;
  158. }
  159. static void coreboot_table_remove(struct platform_device *pdev)
  160. {
  161. bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
  162. }
  163. #ifdef CONFIG_ACPI
  164. static const struct acpi_device_id cros_coreboot_acpi_match[] = {
  165. { "GOOGCB00", 0 },
  166. { "BOOT0000", 0 },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
  170. #endif
  171. #ifdef CONFIG_OF
  172. static const struct of_device_id coreboot_of_match[] = {
  173. { .compatible = "coreboot" },
  174. {}
  175. };
  176. MODULE_DEVICE_TABLE(of, coreboot_of_match);
  177. #endif
  178. static struct platform_driver coreboot_table_driver = {
  179. .probe = coreboot_table_probe,
  180. .remove = coreboot_table_remove,
  181. .driver = {
  182. .name = "coreboot_table",
  183. .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
  184. .of_match_table = of_match_ptr(coreboot_of_match),
  185. },
  186. };
  187. static int __init coreboot_table_driver_init(void)
  188. {
  189. int ret;
  190. ret = bus_register(&coreboot_bus_type);
  191. if (ret)
  192. return ret;
  193. ret = platform_driver_register(&coreboot_table_driver);
  194. if (ret) {
  195. bus_unregister(&coreboot_bus_type);
  196. return ret;
  197. }
  198. return 0;
  199. }
  200. static void __exit coreboot_table_driver_exit(void)
  201. {
  202. platform_driver_unregister(&coreboot_table_driver);
  203. bus_unregister(&coreboot_bus_type);
  204. }
  205. module_init(coreboot_table_driver_init);
  206. module_exit(coreboot_table_driver_exit);
  207. MODULE_AUTHOR("Google, Inc.");
  208. MODULE_DESCRIPTION("Module providing coreboot table access");
  209. MODULE_LICENSE("GPL");