cbmem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cbmem.c
  4. *
  5. * Driver for exporting cbmem entries in sysfs.
  6. *
  7. * Copyright 2022 Google LLC
  8. */
  9. #include <linux/device.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kobject.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/sysfs.h>
  18. #include "coreboot_table.h"
  19. struct cbmem_entry {
  20. char *mem_file_buf;
  21. u32 size;
  22. };
  23. static struct cbmem_entry *to_cbmem_entry(struct kobject *kobj)
  24. {
  25. return dev_get_drvdata(kobj_to_dev(kobj));
  26. }
  27. static ssize_t mem_read(struct file *filp, struct kobject *kobj,
  28. const struct bin_attribute *bin_attr, char *buf, loff_t pos,
  29. size_t count)
  30. {
  31. struct cbmem_entry *entry = to_cbmem_entry(kobj);
  32. return memory_read_from_buffer(buf, count, &pos, entry->mem_file_buf,
  33. entry->size);
  34. }
  35. static ssize_t mem_write(struct file *filp, struct kobject *kobj,
  36. const struct bin_attribute *bin_attr, char *buf, loff_t pos,
  37. size_t count)
  38. {
  39. struct cbmem_entry *entry = to_cbmem_entry(kobj);
  40. if (pos < 0 || pos >= entry->size)
  41. return -EINVAL;
  42. if (count > entry->size - pos)
  43. count = entry->size - pos;
  44. memcpy(entry->mem_file_buf + pos, buf, count);
  45. return count;
  46. }
  47. static const BIN_ATTR_ADMIN_RW(mem, 0);
  48. static ssize_t address_show(struct device *dev, struct device_attribute *attr,
  49. char *buf)
  50. {
  51. struct coreboot_device *cbdev = dev_to_coreboot_device(dev);
  52. return sysfs_emit(buf, "0x%llx\n", cbdev->cbmem_entry.address);
  53. }
  54. static DEVICE_ATTR_RO(address);
  55. static ssize_t size_show(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct coreboot_device *cbdev = dev_to_coreboot_device(dev);
  59. return sysfs_emit(buf, "0x%x\n", cbdev->cbmem_entry.entry_size);
  60. }
  61. static DEVICE_ATTR_RO(size);
  62. static struct attribute *attrs[] = {
  63. &dev_attr_address.attr,
  64. &dev_attr_size.attr,
  65. NULL,
  66. };
  67. static const struct bin_attribute *const bin_attrs[] = {
  68. &bin_attr_mem,
  69. NULL,
  70. };
  71. static const struct attribute_group cbmem_entry_group = {
  72. .attrs = attrs,
  73. .bin_attrs = bin_attrs,
  74. };
  75. static const struct attribute_group *dev_groups[] = {
  76. &cbmem_entry_group,
  77. NULL,
  78. };
  79. static int cbmem_entry_probe(struct coreboot_device *dev)
  80. {
  81. struct cbmem_entry *entry;
  82. entry = devm_kzalloc(&dev->dev, sizeof(*entry), GFP_KERNEL);
  83. if (!entry)
  84. return -ENOMEM;
  85. dev_set_drvdata(&dev->dev, entry);
  86. entry->mem_file_buf = devm_memremap(&dev->dev, dev->cbmem_entry.address,
  87. dev->cbmem_entry.entry_size,
  88. MEMREMAP_WB);
  89. if (IS_ERR(entry->mem_file_buf))
  90. return PTR_ERR(entry->mem_file_buf);
  91. entry->size = dev->cbmem_entry.entry_size;
  92. return 0;
  93. }
  94. static const struct coreboot_device_id cbmem_ids[] = {
  95. { .tag = LB_TAG_CBMEM_ENTRY },
  96. { /* sentinel */ }
  97. };
  98. MODULE_DEVICE_TABLE(coreboot, cbmem_ids);
  99. static struct coreboot_driver cbmem_entry_driver = {
  100. .probe = cbmem_entry_probe,
  101. .drv = {
  102. .name = "cbmem",
  103. .dev_groups = dev_groups,
  104. },
  105. .id_table = cbmem_ids,
  106. };
  107. module_coreboot_driver(cbmem_entry_driver);
  108. MODULE_AUTHOR("Jack Rosenthal <jrosenth@chromium.org>");
  109. MODULE_DESCRIPTION("Driver for exporting CBMEM entries in sysfs");
  110. MODULE_LICENSE("GPL");