coreboot_table.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(d, struct coreboot_driver, drv)
  23. static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
  24. {
  25. struct coreboot_device *device = CB_DEV(dev);
  26. struct coreboot_driver *driver = CB_DRV(drv);
  27. return device->entry.tag == driver->tag;
  28. }
  29. static int coreboot_bus_probe(struct device *dev)
  30. {
  31. int ret = -ENODEV;
  32. struct coreboot_device *device = CB_DEV(dev);
  33. struct coreboot_driver *driver = CB_DRV(dev->driver);
  34. if (driver->probe)
  35. ret = driver->probe(device);
  36. return ret;
  37. }
  38. static int coreboot_bus_remove(struct device *dev)
  39. {
  40. int ret = 0;
  41. struct coreboot_device *device = CB_DEV(dev);
  42. struct coreboot_driver *driver = CB_DRV(dev->driver);
  43. if (driver->remove)
  44. ret = driver->remove(device);
  45. return ret;
  46. }
  47. static struct bus_type coreboot_bus_type = {
  48. .name = "coreboot",
  49. .match = coreboot_bus_match,
  50. .probe = coreboot_bus_probe,
  51. .remove = coreboot_bus_remove,
  52. };
  53. static void coreboot_device_release(struct device *dev)
  54. {
  55. struct coreboot_device *device = CB_DEV(dev);
  56. kfree(device);
  57. }
  58. int coreboot_driver_register(struct coreboot_driver *driver)
  59. {
  60. driver->drv.bus = &coreboot_bus_type;
  61. return driver_register(&driver->drv);
  62. }
  63. EXPORT_SYMBOL(coreboot_driver_register);
  64. void coreboot_driver_unregister(struct coreboot_driver *driver)
  65. {
  66. driver_unregister(&driver->drv);
  67. }
  68. EXPORT_SYMBOL(coreboot_driver_unregister);
  69. static int coreboot_table_populate(struct device *dev, void *ptr)
  70. {
  71. int i, ret;
  72. void *ptr_entry;
  73. struct coreboot_device *device;
  74. struct coreboot_table_entry *entry;
  75. struct coreboot_table_header *header = ptr;
  76. ptr_entry = ptr + header->header_bytes;
  77. for (i = 0; i < header->table_entries; i++) {
  78. entry = ptr_entry;
  79. device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
  80. if (!device)
  81. return -ENOMEM;
  82. dev_set_name(&device->dev, "coreboot%d", i);
  83. device->dev.parent = dev;
  84. device->dev.bus = &coreboot_bus_type;
  85. device->dev.release = coreboot_device_release;
  86. memcpy(&device->entry, ptr_entry, entry->size);
  87. ret = device_register(&device->dev);
  88. if (ret) {
  89. put_device(&device->dev);
  90. return ret;
  91. }
  92. ptr_entry += entry->size;
  93. }
  94. return 0;
  95. }
  96. static int coreboot_table_probe(struct platform_device *pdev)
  97. {
  98. resource_size_t len;
  99. struct coreboot_table_header *header;
  100. struct resource *res;
  101. struct device *dev = &pdev->dev;
  102. void *ptr;
  103. int ret;
  104. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. if (!res)
  106. return -EINVAL;
  107. len = resource_size(res);
  108. if (!res->start || !len)
  109. return -EINVAL;
  110. /* Check just the header first to make sure things are sane */
  111. header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
  112. if (!header)
  113. return -ENOMEM;
  114. len = header->header_bytes + header->table_bytes;
  115. ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
  116. memunmap(header);
  117. if (ret) {
  118. dev_warn(dev, "coreboot table missing or corrupt!\n");
  119. return -ENODEV;
  120. }
  121. ptr = memremap(res->start, len, MEMREMAP_WB);
  122. if (!ptr)
  123. return -ENOMEM;
  124. ret = bus_register(&coreboot_bus_type);
  125. if (!ret) {
  126. ret = coreboot_table_populate(dev, ptr);
  127. if (ret)
  128. bus_unregister(&coreboot_bus_type);
  129. }
  130. memunmap(ptr);
  131. return ret;
  132. }
  133. static int __cb_dev_unregister(struct device *dev, void *dummy)
  134. {
  135. device_unregister(dev);
  136. return 0;
  137. }
  138. static int coreboot_table_remove(struct platform_device *pdev)
  139. {
  140. bus_for_each_dev(&coreboot_bus_type, NULL, NULL, __cb_dev_unregister);
  141. bus_unregister(&coreboot_bus_type);
  142. return 0;
  143. }
  144. #ifdef CONFIG_ACPI
  145. static const struct acpi_device_id cros_coreboot_acpi_match[] = {
  146. { "GOOGCB00", 0 },
  147. { "BOOT0000", 0 },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
  151. #endif
  152. #ifdef CONFIG_OF
  153. static const struct of_device_id coreboot_of_match[] = {
  154. { .compatible = "coreboot" },
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(of, coreboot_of_match);
  158. #endif
  159. static struct platform_driver coreboot_table_driver = {
  160. .probe = coreboot_table_probe,
  161. .remove = coreboot_table_remove,
  162. .driver = {
  163. .name = "coreboot_table",
  164. .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
  165. .of_match_table = of_match_ptr(coreboot_of_match),
  166. },
  167. };
  168. module_platform_driver(coreboot_table_driver);
  169. MODULE_AUTHOR("Google, Inc.");
  170. MODULE_LICENSE("GPL");