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