119d54020SJack Rosenthal // SPDX-License-Identifier: GPL-2.0-only
219d54020SJack Rosenthal /*
319d54020SJack Rosenthal * cbmem.c
419d54020SJack Rosenthal *
519d54020SJack Rosenthal * Driver for exporting cbmem entries in sysfs.
619d54020SJack Rosenthal *
719d54020SJack Rosenthal * Copyright 2022 Google LLC
819d54020SJack Rosenthal */
919d54020SJack Rosenthal
1019d54020SJack Rosenthal #include <linux/device.h>
1119d54020SJack Rosenthal #include <linux/init.h>
1219d54020SJack Rosenthal #include <linux/io.h>
1319d54020SJack Rosenthal #include <linux/kernel.h>
1419d54020SJack Rosenthal #include <linux/kobject.h>
1519d54020SJack Rosenthal #include <linux/module.h>
1619d54020SJack Rosenthal #include <linux/platform_device.h>
1719d54020SJack Rosenthal #include <linux/slab.h>
1819d54020SJack Rosenthal #include <linux/sysfs.h>
1919d54020SJack Rosenthal
2019d54020SJack Rosenthal #include "coreboot_table.h"
2119d54020SJack Rosenthal
2219d54020SJack Rosenthal struct cbmem_entry {
2319d54020SJack Rosenthal char *mem_file_buf;
2419d54020SJack Rosenthal u32 size;
2519d54020SJack Rosenthal };
2619d54020SJack Rosenthal
to_cbmem_entry(struct kobject * kobj)2719d54020SJack Rosenthal static struct cbmem_entry *to_cbmem_entry(struct kobject *kobj)
2819d54020SJack Rosenthal {
2919d54020SJack Rosenthal return dev_get_drvdata(kobj_to_dev(kobj));
3019d54020SJack Rosenthal }
3119d54020SJack Rosenthal
mem_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)3219d54020SJack Rosenthal static ssize_t mem_read(struct file *filp, struct kobject *kobj,
3319d54020SJack Rosenthal struct bin_attribute *bin_attr, char *buf, loff_t pos,
3419d54020SJack Rosenthal size_t count)
3519d54020SJack Rosenthal {
3619d54020SJack Rosenthal struct cbmem_entry *entry = to_cbmem_entry(kobj);
3719d54020SJack Rosenthal
3819d54020SJack Rosenthal return memory_read_from_buffer(buf, count, &pos, entry->mem_file_buf,
3919d54020SJack Rosenthal entry->size);
4019d54020SJack Rosenthal }
4119d54020SJack Rosenthal
mem_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t pos,size_t count)4219d54020SJack Rosenthal static ssize_t mem_write(struct file *filp, struct kobject *kobj,
4319d54020SJack Rosenthal struct bin_attribute *bin_attr, char *buf, loff_t pos,
4419d54020SJack Rosenthal size_t count)
4519d54020SJack Rosenthal {
4619d54020SJack Rosenthal struct cbmem_entry *entry = to_cbmem_entry(kobj);
4719d54020SJack Rosenthal
4819d54020SJack Rosenthal if (pos < 0 || pos >= entry->size)
4919d54020SJack Rosenthal return -EINVAL;
5019d54020SJack Rosenthal if (count > entry->size - pos)
5119d54020SJack Rosenthal count = entry->size - pos;
5219d54020SJack Rosenthal
5319d54020SJack Rosenthal memcpy(entry->mem_file_buf + pos, buf, count);
5419d54020SJack Rosenthal return count;
5519d54020SJack Rosenthal }
5619d54020SJack Rosenthal static BIN_ATTR_ADMIN_RW(mem, 0);
5719d54020SJack Rosenthal
address_show(struct device * dev,struct device_attribute * attr,char * buf)5819d54020SJack Rosenthal static ssize_t address_show(struct device *dev, struct device_attribute *attr,
5919d54020SJack Rosenthal char *buf)
6019d54020SJack Rosenthal {
6119d54020SJack Rosenthal struct coreboot_device *cbdev = dev_to_coreboot_device(dev);
6219d54020SJack Rosenthal
6319d54020SJack Rosenthal return sysfs_emit(buf, "0x%llx\n", cbdev->cbmem_entry.address);
6419d54020SJack Rosenthal }
6519d54020SJack Rosenthal static DEVICE_ATTR_RO(address);
6619d54020SJack Rosenthal
size_show(struct device * dev,struct device_attribute * attr,char * buf)6719d54020SJack Rosenthal static ssize_t size_show(struct device *dev, struct device_attribute *attr,
6819d54020SJack Rosenthal char *buf)
6919d54020SJack Rosenthal {
7019d54020SJack Rosenthal struct coreboot_device *cbdev = dev_to_coreboot_device(dev);
7119d54020SJack Rosenthal
7219d54020SJack Rosenthal return sysfs_emit(buf, "0x%x\n", cbdev->cbmem_entry.entry_size);
7319d54020SJack Rosenthal }
7419d54020SJack Rosenthal static DEVICE_ATTR_RO(size);
7519d54020SJack Rosenthal
7619d54020SJack Rosenthal static struct attribute *attrs[] = {
7719d54020SJack Rosenthal &dev_attr_address.attr,
7819d54020SJack Rosenthal &dev_attr_size.attr,
7919d54020SJack Rosenthal NULL,
8019d54020SJack Rosenthal };
8119d54020SJack Rosenthal
8219d54020SJack Rosenthal static struct bin_attribute *bin_attrs[] = {
8319d54020SJack Rosenthal &bin_attr_mem,
8419d54020SJack Rosenthal NULL,
8519d54020SJack Rosenthal };
8619d54020SJack Rosenthal
8719d54020SJack Rosenthal static const struct attribute_group cbmem_entry_group = {
8819d54020SJack Rosenthal .attrs = attrs,
8919d54020SJack Rosenthal .bin_attrs = bin_attrs,
9019d54020SJack Rosenthal };
9119d54020SJack Rosenthal
9219d54020SJack Rosenthal static const struct attribute_group *dev_groups[] = {
9319d54020SJack Rosenthal &cbmem_entry_group,
9419d54020SJack Rosenthal NULL,
9519d54020SJack Rosenthal };
9619d54020SJack Rosenthal
cbmem_entry_probe(struct coreboot_device * dev)9719d54020SJack Rosenthal static int cbmem_entry_probe(struct coreboot_device *dev)
9819d54020SJack Rosenthal {
9919d54020SJack Rosenthal struct cbmem_entry *entry;
10019d54020SJack Rosenthal
10119d54020SJack Rosenthal entry = devm_kzalloc(&dev->dev, sizeof(*entry), GFP_KERNEL);
10219d54020SJack Rosenthal if (!entry)
10319d54020SJack Rosenthal return -ENOMEM;
10419d54020SJack Rosenthal
10519d54020SJack Rosenthal dev_set_drvdata(&dev->dev, entry);
10619d54020SJack Rosenthal entry->mem_file_buf = devm_memremap(&dev->dev, dev->cbmem_entry.address,
10719d54020SJack Rosenthal dev->cbmem_entry.entry_size,
10819d54020SJack Rosenthal MEMREMAP_WB);
109fa1ba41cSPeng Wu if (IS_ERR(entry->mem_file_buf))
110fa1ba41cSPeng Wu return PTR_ERR(entry->mem_file_buf);
11119d54020SJack Rosenthal
11219d54020SJack Rosenthal entry->size = dev->cbmem_entry.entry_size;
11319d54020SJack Rosenthal
11419d54020SJack Rosenthal return 0;
11519d54020SJack Rosenthal }
11619d54020SJack Rosenthal
1178a0a6294SNícolas F. R. A. Prado static const struct coreboot_device_id cbmem_ids[] = {
1188a0a6294SNícolas F. R. A. Prado { .tag = LB_TAG_CBMEM_ENTRY },
1198a0a6294SNícolas F. R. A. Prado { /* sentinel */ }
1208a0a6294SNícolas F. R. A. Prado };
1218a0a6294SNícolas F. R. A. Prado MODULE_DEVICE_TABLE(coreboot, cbmem_ids);
1228a0a6294SNícolas F. R. A. Prado
12319d54020SJack Rosenthal static struct coreboot_driver cbmem_entry_driver = {
12419d54020SJack Rosenthal .probe = cbmem_entry_probe,
12519d54020SJack Rosenthal .drv = {
12619d54020SJack Rosenthal .name = "cbmem",
12719d54020SJack Rosenthal .dev_groups = dev_groups,
12819d54020SJack Rosenthal },
1298a0a6294SNícolas F. R. A. Prado .id_table = cbmem_ids,
13019d54020SJack Rosenthal };
13119d54020SJack Rosenthal module_coreboot_driver(cbmem_entry_driver);
13219d54020SJack Rosenthal
13319d54020SJack Rosenthal MODULE_AUTHOR("Jack Rosenthal <jrosenth@chromium.org>");
134*fc2c1d71SJeff Johnson MODULE_DESCRIPTION("Driver for exporting CBMEM entries in sysfs");
13519d54020SJack Rosenthal MODULE_LICENSE("GPL");
136