coreboot_table.c 4.9 KB

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