1 // SPDX-License-Identifier: GPL-2.0-only
5 * This module exports the DMI tables read-only to userspace through the
8 * Data is currently found below
9 * /sys/firmware/dmi/...
11 * DMI attributes are presented in attribute files with names
12 * formatted using %d-%d, so that the first integer indicates the
13 * structure type (0-255), and the second field is the instance of that
16 * Copyright 2011 Google, Inc.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kobject.h>
24 #include <linux/dmi.h>
25 #include <linux/capability.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
31 #define MAX_ENTRY_TYPE 255 /* Most of these aren't used, but we consider
32 the top entry type is only 8 bits */
34 struct dmi_sysfs_entry
{
39 struct list_head list
;
40 struct kobject
*child
;
44 * Global list of dmi_sysfs_entry. Even though this should only be
45 * manipulated at setup and teardown, the lazy nature of the kobject
46 * system means we get lazy removes.
48 static LIST_HEAD(entry_list
);
49 static DEFINE_SPINLOCK(entry_list_lock
);
51 /* dmi_sysfs_attribute - Top level attribute. used by all entries. */
52 struct dmi_sysfs_attribute
{
53 struct attribute attr
;
54 ssize_t (*show
)(struct dmi_sysfs_entry
*entry
, char *buf
);
57 #define DMI_SYSFS_ATTR(_entry, _name) \
58 struct dmi_sysfs_attribute dmi_sysfs_attr_##_entry##_##_name = { \
59 .attr = {.name = __stringify(_name), .mode = 0400}, \
60 .show = dmi_sysfs_##_entry##_##_name, \
64 * dmi_sysfs_mapped_attribute - Attribute where we require the entry be
65 * mapped in. Use in conjunction with dmi_sysfs_specialize_attr_ops.
67 struct dmi_sysfs_mapped_attribute
{
68 struct attribute attr
;
69 ssize_t (*show
)(struct dmi_sysfs_entry
*entry
,
70 const struct dmi_header
*dh
,
74 #define DMI_SYSFS_MAPPED_ATTR(_entry, _name) \
75 struct dmi_sysfs_mapped_attribute dmi_sysfs_attr_##_entry##_##_name = { \
76 .attr = {.name = __stringify(_name), .mode = 0400}, \
77 .show = dmi_sysfs_##_entry##_##_name, \
80 /*************************************************
81 * Generic DMI entry support.
82 *************************************************/
83 static void dmi_entry_free(struct kobject
*kobj
)
88 static struct dmi_sysfs_entry
*to_entry(struct kobject
*kobj
)
90 return container_of(kobj
, struct dmi_sysfs_entry
, kobj
);
93 static struct dmi_sysfs_attribute
*to_attr(struct attribute
*attr
)
95 return container_of(attr
, struct dmi_sysfs_attribute
, attr
);
98 static ssize_t
dmi_sysfs_attr_show(struct kobject
*kobj
,
99 struct attribute
*_attr
, char *buf
)
101 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
102 struct dmi_sysfs_attribute
*attr
= to_attr(_attr
);
104 /* DMI stuff is only ever admin visible */
105 if (!capable(CAP_SYS_ADMIN
))
108 return attr
->show(entry
, buf
);
111 static const struct sysfs_ops dmi_sysfs_attr_ops
= {
112 .show
= dmi_sysfs_attr_show
,
115 typedef ssize_t (*dmi_callback
)(struct dmi_sysfs_entry
*,
116 const struct dmi_header
*dh
, void *);
118 struct find_dmi_data
{
119 struct dmi_sysfs_entry
*entry
;
120 dmi_callback callback
;
122 int instance_countdown
;
126 static void find_dmi_entry_helper(const struct dmi_header
*dh
,
129 struct find_dmi_data
*data
= _data
;
130 struct dmi_sysfs_entry
*entry
= data
->entry
;
132 /* Is this the entry we want? */
133 if (dh
->type
!= entry
->dh
.type
)
136 if (data
->instance_countdown
!= 0) {
137 /* try the next instance? */
138 data
->instance_countdown
--;
143 * Don't ever revisit the instance. Short circuit later
144 * instances by letting the instance_countdown run negative
146 data
->instance_countdown
--;
148 /* Found the entry */
149 data
->ret
= data
->callback(entry
, dh
, data
->private);
152 /* State for passing the read parameters through dmi_find_entry() */
153 struct dmi_read_state
{
159 static ssize_t
find_dmi_entry(struct dmi_sysfs_entry
*entry
,
160 dmi_callback callback
, void *private)
162 struct find_dmi_data data
= {
164 .callback
= callback
,
166 .instance_countdown
= entry
->instance
,
167 .ret
= -EIO
, /* To signal the entry disappeared */
171 ret
= dmi_walk(find_dmi_entry_helper
, &data
);
172 /* This shouldn't happen, but just in case. */
179 * Calculate and return the byte length of the dmi entry identified by
180 * dh. This includes both the formatted portion as well as the
181 * unformatted string space, including the two trailing nul characters.
183 static size_t dmi_entry_length(const struct dmi_header
*dh
)
185 const char *p
= (const char *)dh
;
192 return 2 + p
- (const char *)dh
;
195 /*************************************************
196 * Support bits for specialized DMI entry support
197 *************************************************/
198 struct dmi_entry_attr_show_data
{
199 struct attribute
*attr
;
203 static ssize_t
dmi_entry_attr_show_helper(struct dmi_sysfs_entry
*entry
,
204 const struct dmi_header
*dh
,
207 struct dmi_entry_attr_show_data
*data
= _data
;
208 struct dmi_sysfs_mapped_attribute
*attr
;
210 attr
= container_of(data
->attr
,
211 struct dmi_sysfs_mapped_attribute
, attr
);
212 return attr
->show(entry
, dh
, data
->buf
);
215 static ssize_t
dmi_entry_attr_show(struct kobject
*kobj
,
216 struct attribute
*attr
,
219 struct dmi_entry_attr_show_data data
= {
223 /* Find the entry according to our parent and call the
224 * normalized show method hanging off of the attribute */
225 return find_dmi_entry(to_entry(kobj
->parent
),
226 dmi_entry_attr_show_helper
, &data
);
229 static const struct sysfs_ops dmi_sysfs_specialize_attr_ops
= {
230 .show
= dmi_entry_attr_show
,
233 /*************************************************
234 * Specialized DMI entry support.
235 *************************************************/
237 /*** Type 15 - System Event Table ***/
239 #define DMI_SEL_ACCESS_METHOD_IO8 0x00
240 #define DMI_SEL_ACCESS_METHOD_IO2x8 0x01
241 #define DMI_SEL_ACCESS_METHOD_IO16 0x02
242 #define DMI_SEL_ACCESS_METHOD_PHYS32 0x03
243 #define DMI_SEL_ACCESS_METHOD_GPNV 0x04
245 struct dmi_system_event_log
{
246 struct dmi_header header
;
248 u16 header_start_offset
;
249 u16 data_start_offset
;
260 u32 access_method_address
;
263 u8 type_descriptors_supported_count
;
264 u8 per_log_type_descriptor_length
;
265 u8 supported_log_type_descriptos
[];
268 #define DMI_SYSFS_SEL_FIELD(_field) \
269 static ssize_t dmi_sysfs_sel_##_field(struct dmi_sysfs_entry *entry, \
270 const struct dmi_header *dh, \
273 struct dmi_system_event_log sel; \
274 if (sizeof(sel) > dmi_entry_length(dh)) \
276 memcpy(&sel, dh, sizeof(sel)); \
277 return sprintf(buf, "%u\n", sel._field); \
279 static DMI_SYSFS_MAPPED_ATTR(sel, _field)
281 DMI_SYSFS_SEL_FIELD(area_length
);
282 DMI_SYSFS_SEL_FIELD(header_start_offset
);
283 DMI_SYSFS_SEL_FIELD(data_start_offset
);
284 DMI_SYSFS_SEL_FIELD(access_method
);
285 DMI_SYSFS_SEL_FIELD(status
);
286 DMI_SYSFS_SEL_FIELD(change_token
);
287 DMI_SYSFS_SEL_FIELD(access_method_address
);
288 DMI_SYSFS_SEL_FIELD(header_format
);
289 DMI_SYSFS_SEL_FIELD(type_descriptors_supported_count
);
290 DMI_SYSFS_SEL_FIELD(per_log_type_descriptor_length
);
292 static struct attribute
*dmi_sysfs_sel_attrs
[] = {
293 &dmi_sysfs_attr_sel_area_length
.attr
,
294 &dmi_sysfs_attr_sel_header_start_offset
.attr
,
295 &dmi_sysfs_attr_sel_data_start_offset
.attr
,
296 &dmi_sysfs_attr_sel_access_method
.attr
,
297 &dmi_sysfs_attr_sel_status
.attr
,
298 &dmi_sysfs_attr_sel_change_token
.attr
,
299 &dmi_sysfs_attr_sel_access_method_address
.attr
,
300 &dmi_sysfs_attr_sel_header_format
.attr
,
301 &dmi_sysfs_attr_sel_type_descriptors_supported_count
.attr
,
302 &dmi_sysfs_attr_sel_per_log_type_descriptor_length
.attr
,
305 ATTRIBUTE_GROUPS(dmi_sysfs_sel
);
307 static const struct kobj_type dmi_system_event_log_ktype
= {
308 .release
= dmi_entry_free
,
309 .sysfs_ops
= &dmi_sysfs_specialize_attr_ops
,
310 .default_groups
= dmi_sysfs_sel_groups
,
313 #ifdef CONFIG_HAS_IOPORT
314 typedef u8 (*sel_io_reader
)(const struct dmi_system_event_log
*sel
,
317 static DEFINE_MUTEX(io_port_lock
);
319 static u8
read_sel_8bit_indexed_io(const struct dmi_system_event_log
*sel
,
324 mutex_lock(&io_port_lock
);
325 outb((u8
)offset
, sel
->io
.index_addr
);
326 ret
= inb(sel
->io
.data_addr
);
327 mutex_unlock(&io_port_lock
);
331 static u8
read_sel_2x8bit_indexed_io(const struct dmi_system_event_log
*sel
,
336 mutex_lock(&io_port_lock
);
337 outb((u8
)offset
, sel
->io
.index_addr
);
338 outb((u8
)(offset
>> 8), sel
->io
.index_addr
+ 1);
339 ret
= inb(sel
->io
.data_addr
);
340 mutex_unlock(&io_port_lock
);
344 static u8
read_sel_16bit_indexed_io(const struct dmi_system_event_log
*sel
,
349 mutex_lock(&io_port_lock
);
350 outw((u16
)offset
, sel
->io
.index_addr
);
351 ret
= inb(sel
->io
.data_addr
);
352 mutex_unlock(&io_port_lock
);
356 static sel_io_reader sel_io_readers
[] = {
357 [DMI_SEL_ACCESS_METHOD_IO8
] = read_sel_8bit_indexed_io
,
358 [DMI_SEL_ACCESS_METHOD_IO2x8
] = read_sel_2x8bit_indexed_io
,
359 [DMI_SEL_ACCESS_METHOD_IO16
] = read_sel_16bit_indexed_io
,
362 static ssize_t
dmi_sel_raw_read_io(struct dmi_sysfs_entry
*entry
,
363 const struct dmi_system_event_log
*sel
,
364 char *buf
, loff_t pos
, size_t count
)
368 sel_io_reader io_reader
= sel_io_readers
[sel
->access_method
];
370 while (count
&& pos
< sel
->area_length
) {
372 *(buf
++) = io_reader(sel
, pos
++);
380 static ssize_t
dmi_sel_raw_read_phys32(struct dmi_sysfs_entry
*entry
,
381 const struct dmi_system_event_log
*sel
,
382 char *buf
, loff_t pos
, size_t count
)
387 mapped
= dmi_remap(sel
->access_method_address
, sel
->area_length
);
391 while (count
&& pos
< sel
->area_length
) {
393 *(buf
++) = readb(mapped
+ pos
++);
401 static ssize_t
dmi_sel_raw_read_helper(struct dmi_sysfs_entry
*entry
,
402 const struct dmi_header
*dh
,
405 struct dmi_read_state
*state
= _state
;
406 struct dmi_system_event_log sel
;
408 if (sizeof(sel
) > dmi_entry_length(dh
))
411 memcpy(&sel
, dh
, sizeof(sel
));
413 switch (sel
.access_method
) {
414 #ifdef CONFIG_HAS_IOPORT
415 case DMI_SEL_ACCESS_METHOD_IO8
:
416 case DMI_SEL_ACCESS_METHOD_IO2x8
:
417 case DMI_SEL_ACCESS_METHOD_IO16
:
418 return dmi_sel_raw_read_io(entry
, &sel
, state
->buf
,
419 state
->pos
, state
->count
);
421 case DMI_SEL_ACCESS_METHOD_PHYS32
:
422 return dmi_sel_raw_read_phys32(entry
, &sel
, state
->buf
,
423 state
->pos
, state
->count
);
424 case DMI_SEL_ACCESS_METHOD_GPNV
:
425 pr_info_ratelimited("dmi-sysfs: GPNV support missing.\n");
428 pr_info_ratelimited("dmi-sysfs: Unknown access method %02x\n",
434 static ssize_t
dmi_sel_raw_read(struct file
*filp
, struct kobject
*kobj
,
435 struct bin_attribute
*bin_attr
,
436 char *buf
, loff_t pos
, size_t count
)
438 struct dmi_sysfs_entry
*entry
= to_entry(kobj
->parent
);
439 struct dmi_read_state state
= {
445 return find_dmi_entry(entry
, dmi_sel_raw_read_helper
, &state
);
448 static struct bin_attribute dmi_sel_raw_attr
= {
449 .attr
= {.name
= "raw_event_log", .mode
= 0400},
450 .read
= dmi_sel_raw_read
,
453 static int dmi_system_event_log(struct dmi_sysfs_entry
*entry
)
457 entry
->child
= kzalloc(sizeof(*entry
->child
), GFP_KERNEL
);
460 ret
= kobject_init_and_add(entry
->child
,
461 &dmi_system_event_log_ktype
,
467 ret
= sysfs_create_bin_file(entry
->child
, &dmi_sel_raw_attr
);
474 kobject_del(entry
->child
);
480 /*************************************************
481 * Generic DMI entry support.
482 *************************************************/
484 static ssize_t
dmi_sysfs_entry_length(struct dmi_sysfs_entry
*entry
, char *buf
)
486 return sprintf(buf
, "%d\n", entry
->dh
.length
);
489 static ssize_t
dmi_sysfs_entry_handle(struct dmi_sysfs_entry
*entry
, char *buf
)
491 return sprintf(buf
, "%d\n", entry
->dh
.handle
);
494 static ssize_t
dmi_sysfs_entry_type(struct dmi_sysfs_entry
*entry
, char *buf
)
496 return sprintf(buf
, "%d\n", entry
->dh
.type
);
499 static ssize_t
dmi_sysfs_entry_instance(struct dmi_sysfs_entry
*entry
,
502 return sprintf(buf
, "%d\n", entry
->instance
);
505 static ssize_t
dmi_sysfs_entry_position(struct dmi_sysfs_entry
*entry
,
508 return sprintf(buf
, "%d\n", entry
->position
);
511 static DMI_SYSFS_ATTR(entry
, length
);
512 static DMI_SYSFS_ATTR(entry
, handle
);
513 static DMI_SYSFS_ATTR(entry
, type
);
514 static DMI_SYSFS_ATTR(entry
, instance
);
515 static DMI_SYSFS_ATTR(entry
, position
);
517 static struct attribute
*dmi_sysfs_entry_attrs
[] = {
518 &dmi_sysfs_attr_entry_length
.attr
,
519 &dmi_sysfs_attr_entry_handle
.attr
,
520 &dmi_sysfs_attr_entry_type
.attr
,
521 &dmi_sysfs_attr_entry_instance
.attr
,
522 &dmi_sysfs_attr_entry_position
.attr
,
525 ATTRIBUTE_GROUPS(dmi_sysfs_entry
);
527 static ssize_t
dmi_entry_raw_read_helper(struct dmi_sysfs_entry
*entry
,
528 const struct dmi_header
*dh
,
531 struct dmi_read_state
*state
= _state
;
534 entry_length
= dmi_entry_length(dh
);
536 return memory_read_from_buffer(state
->buf
, state
->count
,
537 &state
->pos
, dh
, entry_length
);
540 static ssize_t
dmi_entry_raw_read(struct file
*filp
,
541 struct kobject
*kobj
,
542 struct bin_attribute
*bin_attr
,
543 char *buf
, loff_t pos
, size_t count
)
545 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
546 struct dmi_read_state state
= {
552 return find_dmi_entry(entry
, dmi_entry_raw_read_helper
, &state
);
555 static const struct bin_attribute dmi_entry_raw_attr
= {
556 .attr
= {.name
= "raw", .mode
= 0400},
557 .read
= dmi_entry_raw_read
,
560 static void dmi_sysfs_entry_release(struct kobject
*kobj
)
562 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
564 spin_lock(&entry_list_lock
);
565 list_del(&entry
->list
);
566 spin_unlock(&entry_list_lock
);
570 static const struct kobj_type dmi_sysfs_entry_ktype
= {
571 .release
= dmi_sysfs_entry_release
,
572 .sysfs_ops
= &dmi_sysfs_attr_ops
,
573 .default_groups
= dmi_sysfs_entry_groups
,
576 static struct kset
*dmi_kset
;
578 /* Global count of all instances seen. Only for setup */
579 static int __initdata instance_counts
[MAX_ENTRY_TYPE
+ 1];
581 /* Global positional count of all entries seen. Only for setup */
582 static int __initdata position_count
;
584 static void __init
dmi_sysfs_register_handle(const struct dmi_header
*dh
,
587 struct dmi_sysfs_entry
*entry
;
590 /* If a previous entry saw an error, short circuit */
594 /* Allocate and register a new entry into the entries set */
595 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
602 memcpy(&entry
->dh
, dh
, sizeof(*dh
));
603 entry
->instance
= instance_counts
[dh
->type
]++;
604 entry
->position
= position_count
++;
606 entry
->kobj
.kset
= dmi_kset
;
607 *ret
= kobject_init_and_add(&entry
->kobj
, &dmi_sysfs_entry_ktype
, NULL
,
608 "%d-%d", dh
->type
, entry
->instance
);
610 /* Thread on the global list for cleanup */
611 spin_lock(&entry_list_lock
);
612 list_add_tail(&entry
->list
, &entry_list
);
613 spin_unlock(&entry_list_lock
);
616 kobject_put(&entry
->kobj
);
620 /* Handle specializations by type */
622 case DMI_ENTRY_SYSTEM_EVENT_LOG
:
623 *ret
= dmi_system_event_log(entry
);
626 /* No specialization */
632 /* Create the raw binary file to access the entry */
633 *ret
= sysfs_create_bin_file(&entry
->kobj
, &dmi_entry_raw_attr
);
639 kobject_put(entry
->child
);
640 kobject_put(&entry
->kobj
);
644 static void cleanup_entry_list(void)
646 struct dmi_sysfs_entry
*entry
, *next
;
648 /* No locks, we are on our way out */
649 list_for_each_entry_safe(entry
, next
, &entry_list
, list
) {
650 kobject_put(entry
->child
);
651 kobject_put(&entry
->kobj
);
655 static int __init
dmi_sysfs_init(void)
661 pr_debug("dmi-sysfs: dmi entry is absent.\n");
666 dmi_kset
= kset_create_and_add("entries", NULL
, dmi_kobj
);
673 error
= dmi_walk(dmi_sysfs_register_handle
, &val
);
681 pr_debug("dmi-sysfs: loaded.\n");
685 cleanup_entry_list();
686 kset_unregister(dmi_kset
);
690 /* clean up everything. */
691 static void __exit
dmi_sysfs_exit(void)
693 pr_debug("dmi-sysfs: unloading.\n");
694 cleanup_entry_list();
695 kset_unregister(dmi_kset
);
698 module_init(dmi_sysfs_init
);
699 module_exit(dmi_sysfs_exit
);
701 MODULE_AUTHOR("Mike Waychison <mikew@google.com>");
702 MODULE_DESCRIPTION("DMI sysfs support");
703 MODULE_LICENSE("GPL");