4 * This module exports the DMI tables read-only to userspace through the
7 * Data is currently found below
8 * /sys/firmware/dmi/...
10 * DMI attributes are presented in attribute files with names
11 * formatted using %d-%d, so that the first integer indicates the
12 * structure type (0-255), and the second field is the instance of that
15 * Copyright 2011 Google, Inc.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kobject.h>
23 #include <linux/dmi.h>
24 #include <linux/capability.h>
25 #include <linux/slab.h>
26 #include <linux/list.h>
29 #define MAX_ENTRY_TYPE 255 /* Most of these aren't used, but we consider
30 the top entry type is only 8 bits */
32 struct dmi_sysfs_entry
{
37 struct list_head list
;
38 struct kobject
*child
;
42 * Global list of dmi_sysfs_entry. Even though this should only be
43 * manipulated at setup and teardown, the lazy nature of the kobject
44 * system means we get lazy removes.
46 static LIST_HEAD(entry_list
);
47 static DEFINE_SPINLOCK(entry_list_lock
);
49 /* dmi_sysfs_attribute - Top level attribute. used by all entries. */
50 struct dmi_sysfs_attribute
{
51 struct attribute attr
;
52 ssize_t (*show
)(struct dmi_sysfs_entry
*entry
, char *buf
);
55 #define DMI_SYSFS_ATTR(_entry, _name) \
56 struct dmi_sysfs_attribute dmi_sysfs_attr_##_entry##_##_name = { \
57 .attr = {.name = __stringify(_name), .mode = 0400}, \
58 .show = dmi_sysfs_##_entry##_##_name, \
62 * dmi_sysfs_mapped_attribute - Attribute where we require the entry be
63 * mapped in. Use in conjunction with dmi_sysfs_specialize_attr_ops.
65 struct dmi_sysfs_mapped_attribute
{
66 struct attribute attr
;
67 ssize_t (*show
)(struct dmi_sysfs_entry
*entry
,
68 const struct dmi_header
*dh
,
72 #define DMI_SYSFS_MAPPED_ATTR(_entry, _name) \
73 struct dmi_sysfs_mapped_attribute dmi_sysfs_attr_##_entry##_##_name = { \
74 .attr = {.name = __stringify(_name), .mode = 0400}, \
75 .show = dmi_sysfs_##_entry##_##_name, \
78 /*************************************************
79 * Generic DMI entry support.
80 *************************************************/
81 static void dmi_entry_free(struct kobject
*kobj
)
86 static struct dmi_sysfs_entry
*to_entry(struct kobject
*kobj
)
88 return container_of(kobj
, struct dmi_sysfs_entry
, kobj
);
91 static struct dmi_sysfs_attribute
*to_attr(struct attribute
*attr
)
93 return container_of(attr
, struct dmi_sysfs_attribute
, attr
);
96 static ssize_t
dmi_sysfs_attr_show(struct kobject
*kobj
,
97 struct attribute
*_attr
, char *buf
)
99 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
100 struct dmi_sysfs_attribute
*attr
= to_attr(_attr
);
102 /* DMI stuff is only ever admin visible */
103 if (!capable(CAP_SYS_ADMIN
))
106 return attr
->show(entry
, buf
);
109 static const struct sysfs_ops dmi_sysfs_attr_ops
= {
110 .show
= dmi_sysfs_attr_show
,
113 typedef ssize_t (*dmi_callback
)(struct dmi_sysfs_entry
*,
114 const struct dmi_header
*dh
, void *);
116 struct find_dmi_data
{
117 struct dmi_sysfs_entry
*entry
;
118 dmi_callback callback
;
120 int instance_countdown
;
124 static void find_dmi_entry_helper(const struct dmi_header
*dh
,
127 struct find_dmi_data
*data
= _data
;
128 struct dmi_sysfs_entry
*entry
= data
->entry
;
130 /* Is this the entry we want? */
131 if (dh
->type
!= entry
->dh
.type
)
134 if (data
->instance_countdown
!= 0) {
135 /* try the next instance? */
136 data
->instance_countdown
--;
141 * Don't ever revisit the instance. Short circuit later
142 * instances by letting the instance_countdown run negative
144 data
->instance_countdown
--;
146 /* Found the entry */
147 data
->ret
= data
->callback(entry
, dh
, data
->private);
150 /* State for passing the read parameters through dmi_find_entry() */
151 struct dmi_read_state
{
157 static ssize_t
find_dmi_entry(struct dmi_sysfs_entry
*entry
,
158 dmi_callback callback
, void *private)
160 struct find_dmi_data data
= {
162 .callback
= callback
,
164 .instance_countdown
= entry
->instance
,
165 .ret
= -EIO
, /* To signal the entry disappeared */
169 ret
= dmi_walk(find_dmi_entry_helper
, &data
);
170 /* This shouldn't happen, but just in case. */
177 * Calculate and return the byte length of the dmi entry identified by
178 * dh. This includes both the formatted portion as well as the
179 * unformatted string space, including the two trailing nul characters.
181 static size_t dmi_entry_length(const struct dmi_header
*dh
)
183 const char *p
= (const char *)dh
;
190 return 2 + p
- (const char *)dh
;
193 /*************************************************
194 * Support bits for specialized DMI entry support
195 *************************************************/
196 struct dmi_entry_attr_show_data
{
197 struct attribute
*attr
;
201 static ssize_t
dmi_entry_attr_show_helper(struct dmi_sysfs_entry
*entry
,
202 const struct dmi_header
*dh
,
205 struct dmi_entry_attr_show_data
*data
= _data
;
206 struct dmi_sysfs_mapped_attribute
*attr
;
208 attr
= container_of(data
->attr
,
209 struct dmi_sysfs_mapped_attribute
, attr
);
210 return attr
->show(entry
, dh
, data
->buf
);
213 static ssize_t
dmi_entry_attr_show(struct kobject
*kobj
,
214 struct attribute
*attr
,
217 struct dmi_entry_attr_show_data data
= {
221 /* Find the entry according to our parent and call the
222 * normalized show method hanging off of the attribute */
223 return find_dmi_entry(to_entry(kobj
->parent
),
224 dmi_entry_attr_show_helper
, &data
);
227 static const struct sysfs_ops dmi_sysfs_specialize_attr_ops
= {
228 .show
= dmi_entry_attr_show
,
231 /*************************************************
232 * Specialized DMI entry support.
233 *************************************************/
235 /*** Type 15 - System Event Table ***/
237 #define DMI_SEL_ACCESS_METHOD_IO8 0x00
238 #define DMI_SEL_ACCESS_METHOD_IO2x8 0x01
239 #define DMI_SEL_ACCESS_METHOD_IO16 0x02
240 #define DMI_SEL_ACCESS_METHOD_PHYS32 0x03
241 #define DMI_SEL_ACCESS_METHOD_GPNV 0x04
243 struct dmi_system_event_log
{
244 struct dmi_header header
;
246 u16 header_start_offset
;
247 u16 data_start_offset
;
258 u32 access_method_address
;
261 u8 type_descriptors_supported_count
;
262 u8 per_log_type_descriptor_length
;
263 u8 supported_log_type_descriptos
[0];
266 #define DMI_SYSFS_SEL_FIELD(_field) \
267 static ssize_t dmi_sysfs_sel_##_field(struct dmi_sysfs_entry *entry, \
268 const struct dmi_header *dh, \
271 struct dmi_system_event_log sel; \
272 if (sizeof(sel) > dmi_entry_length(dh)) \
274 memcpy(&sel, dh, sizeof(sel)); \
275 return sprintf(buf, "%u\n", sel._field); \
277 static DMI_SYSFS_MAPPED_ATTR(sel, _field)
279 DMI_SYSFS_SEL_FIELD(area_length
);
280 DMI_SYSFS_SEL_FIELD(header_start_offset
);
281 DMI_SYSFS_SEL_FIELD(data_start_offset
);
282 DMI_SYSFS_SEL_FIELD(access_method
);
283 DMI_SYSFS_SEL_FIELD(status
);
284 DMI_SYSFS_SEL_FIELD(change_token
);
285 DMI_SYSFS_SEL_FIELD(access_method_address
);
286 DMI_SYSFS_SEL_FIELD(header_format
);
287 DMI_SYSFS_SEL_FIELD(type_descriptors_supported_count
);
288 DMI_SYSFS_SEL_FIELD(per_log_type_descriptor_length
);
290 static struct attribute
*dmi_sysfs_sel_attrs
[] = {
291 &dmi_sysfs_attr_sel_area_length
.attr
,
292 &dmi_sysfs_attr_sel_header_start_offset
.attr
,
293 &dmi_sysfs_attr_sel_data_start_offset
.attr
,
294 &dmi_sysfs_attr_sel_access_method
.attr
,
295 &dmi_sysfs_attr_sel_status
.attr
,
296 &dmi_sysfs_attr_sel_change_token
.attr
,
297 &dmi_sysfs_attr_sel_access_method_address
.attr
,
298 &dmi_sysfs_attr_sel_header_format
.attr
,
299 &dmi_sysfs_attr_sel_type_descriptors_supported_count
.attr
,
300 &dmi_sysfs_attr_sel_per_log_type_descriptor_length
.attr
,
305 static struct kobj_type dmi_system_event_log_ktype
= {
306 .release
= dmi_entry_free
,
307 .sysfs_ops
= &dmi_sysfs_specialize_attr_ops
,
308 .default_attrs
= dmi_sysfs_sel_attrs
,
311 typedef u8 (*sel_io_reader
)(const struct dmi_system_event_log
*sel
,
314 static DEFINE_MUTEX(io_port_lock
);
316 static u8
read_sel_8bit_indexed_io(const struct dmi_system_event_log
*sel
,
321 mutex_lock(&io_port_lock
);
322 outb((u8
)offset
, sel
->io
.index_addr
);
323 ret
= inb(sel
->io
.data_addr
);
324 mutex_unlock(&io_port_lock
);
328 static u8
read_sel_2x8bit_indexed_io(const struct dmi_system_event_log
*sel
,
333 mutex_lock(&io_port_lock
);
334 outb((u8
)offset
, sel
->io
.index_addr
);
335 outb((u8
)(offset
>> 8), sel
->io
.index_addr
+ 1);
336 ret
= inb(sel
->io
.data_addr
);
337 mutex_unlock(&io_port_lock
);
341 static u8
read_sel_16bit_indexed_io(const struct dmi_system_event_log
*sel
,
346 mutex_lock(&io_port_lock
);
347 outw((u16
)offset
, sel
->io
.index_addr
);
348 ret
= inb(sel
->io
.data_addr
);
349 mutex_unlock(&io_port_lock
);
353 static sel_io_reader sel_io_readers
[] = {
354 [DMI_SEL_ACCESS_METHOD_IO8
] = read_sel_8bit_indexed_io
,
355 [DMI_SEL_ACCESS_METHOD_IO2x8
] = read_sel_2x8bit_indexed_io
,
356 [DMI_SEL_ACCESS_METHOD_IO16
] = read_sel_16bit_indexed_io
,
359 static ssize_t
dmi_sel_raw_read_io(struct dmi_sysfs_entry
*entry
,
360 const struct dmi_system_event_log
*sel
,
361 char *buf
, loff_t pos
, size_t count
)
365 sel_io_reader io_reader
= sel_io_readers
[sel
->access_method
];
367 while (count
&& pos
< sel
->area_length
) {
369 *(buf
++) = io_reader(sel
, pos
++);
376 static ssize_t
dmi_sel_raw_read_phys32(struct dmi_sysfs_entry
*entry
,
377 const struct dmi_system_event_log
*sel
,
378 char *buf
, loff_t pos
, size_t count
)
383 mapped
= ioremap(sel
->access_method_address
, sel
->area_length
);
387 while (count
&& pos
< sel
->area_length
) {
389 *(buf
++) = readb(mapped
+ pos
++);
397 static ssize_t
dmi_sel_raw_read_helper(struct dmi_sysfs_entry
*entry
,
398 const struct dmi_header
*dh
,
401 struct dmi_read_state
*state
= _state
;
402 struct dmi_system_event_log sel
;
404 if (sizeof(sel
) > dmi_entry_length(dh
))
407 memcpy(&sel
, dh
, sizeof(sel
));
409 switch (sel
.access_method
) {
410 case DMI_SEL_ACCESS_METHOD_IO8
:
411 case DMI_SEL_ACCESS_METHOD_IO2x8
:
412 case DMI_SEL_ACCESS_METHOD_IO16
:
413 return dmi_sel_raw_read_io(entry
, &sel
, state
->buf
,
414 state
->pos
, state
->count
);
415 case DMI_SEL_ACCESS_METHOD_PHYS32
:
416 return dmi_sel_raw_read_phys32(entry
, &sel
, state
->buf
,
417 state
->pos
, state
->count
);
418 case DMI_SEL_ACCESS_METHOD_GPNV
:
419 pr_info("dmi-sysfs: GPNV support missing.\n");
422 pr_info("dmi-sysfs: Unknown access method %02x\n",
428 static ssize_t
dmi_sel_raw_read(struct file
*filp
, struct kobject
*kobj
,
429 struct bin_attribute
*bin_attr
,
430 char *buf
, loff_t pos
, size_t count
)
432 struct dmi_sysfs_entry
*entry
= to_entry(kobj
->parent
);
433 struct dmi_read_state state
= {
439 return find_dmi_entry(entry
, dmi_sel_raw_read_helper
, &state
);
442 static struct bin_attribute dmi_sel_raw_attr
= {
443 .attr
= {.name
= "raw_event_log", .mode
= 0400},
444 .read
= dmi_sel_raw_read
,
447 static int dmi_system_event_log(struct dmi_sysfs_entry
*entry
)
451 entry
->child
= kzalloc(sizeof(*entry
->child
), GFP_KERNEL
);
454 ret
= kobject_init_and_add(entry
->child
,
455 &dmi_system_event_log_ktype
,
461 ret
= sysfs_create_bin_file(entry
->child
, &dmi_sel_raw_attr
);
468 kobject_del(entry
->child
);
474 /*************************************************
475 * Generic DMI entry support.
476 *************************************************/
478 static ssize_t
dmi_sysfs_entry_length(struct dmi_sysfs_entry
*entry
, char *buf
)
480 return sprintf(buf
, "%d\n", entry
->dh
.length
);
483 static ssize_t
dmi_sysfs_entry_handle(struct dmi_sysfs_entry
*entry
, char *buf
)
485 return sprintf(buf
, "%d\n", entry
->dh
.handle
);
488 static ssize_t
dmi_sysfs_entry_type(struct dmi_sysfs_entry
*entry
, char *buf
)
490 return sprintf(buf
, "%d\n", entry
->dh
.type
);
493 static ssize_t
dmi_sysfs_entry_instance(struct dmi_sysfs_entry
*entry
,
496 return sprintf(buf
, "%d\n", entry
->instance
);
499 static ssize_t
dmi_sysfs_entry_position(struct dmi_sysfs_entry
*entry
,
502 return sprintf(buf
, "%d\n", entry
->position
);
505 static DMI_SYSFS_ATTR(entry
, length
);
506 static DMI_SYSFS_ATTR(entry
, handle
);
507 static DMI_SYSFS_ATTR(entry
, type
);
508 static DMI_SYSFS_ATTR(entry
, instance
);
509 static DMI_SYSFS_ATTR(entry
, position
);
511 static struct attribute
*dmi_sysfs_entry_attrs
[] = {
512 &dmi_sysfs_attr_entry_length
.attr
,
513 &dmi_sysfs_attr_entry_handle
.attr
,
514 &dmi_sysfs_attr_entry_type
.attr
,
515 &dmi_sysfs_attr_entry_instance
.attr
,
516 &dmi_sysfs_attr_entry_position
.attr
,
520 static ssize_t
dmi_entry_raw_read_helper(struct dmi_sysfs_entry
*entry
,
521 const struct dmi_header
*dh
,
524 struct dmi_read_state
*state
= _state
;
527 entry_length
= dmi_entry_length(dh
);
529 return memory_read_from_buffer(state
->buf
, state
->count
,
530 &state
->pos
, dh
, entry_length
);
533 static ssize_t
dmi_entry_raw_read(struct file
*filp
,
534 struct kobject
*kobj
,
535 struct bin_attribute
*bin_attr
,
536 char *buf
, loff_t pos
, size_t count
)
538 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
539 struct dmi_read_state state
= {
545 return find_dmi_entry(entry
, dmi_entry_raw_read_helper
, &state
);
548 static const struct bin_attribute dmi_entry_raw_attr
= {
549 .attr
= {.name
= "raw", .mode
= 0400},
550 .read
= dmi_entry_raw_read
,
553 static void dmi_sysfs_entry_release(struct kobject
*kobj
)
555 struct dmi_sysfs_entry
*entry
= to_entry(kobj
);
557 spin_lock(&entry_list_lock
);
558 list_del(&entry
->list
);
559 spin_unlock(&entry_list_lock
);
563 static struct kobj_type dmi_sysfs_entry_ktype
= {
564 .release
= dmi_sysfs_entry_release
,
565 .sysfs_ops
= &dmi_sysfs_attr_ops
,
566 .default_attrs
= dmi_sysfs_entry_attrs
,
569 static struct kset
*dmi_kset
;
571 /* Global count of all instances seen. Only for setup */
572 static int __initdata instance_counts
[MAX_ENTRY_TYPE
+ 1];
574 /* Global positional count of all entries seen. Only for setup */
575 static int __initdata position_count
;
577 static void __init
dmi_sysfs_register_handle(const struct dmi_header
*dh
,
580 struct dmi_sysfs_entry
*entry
;
583 /* If a previous entry saw an error, short circuit */
587 /* Allocate and register a new entry into the entries set */
588 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
595 memcpy(&entry
->dh
, dh
, sizeof(*dh
));
596 entry
->instance
= instance_counts
[dh
->type
]++;
597 entry
->position
= position_count
++;
599 entry
->kobj
.kset
= dmi_kset
;
600 *ret
= kobject_init_and_add(&entry
->kobj
, &dmi_sysfs_entry_ktype
, NULL
,
601 "%d-%d", dh
->type
, entry
->instance
);
608 /* Thread on the global list for cleanup */
609 spin_lock(&entry_list_lock
);
610 list_add_tail(&entry
->list
, &entry_list
);
611 spin_unlock(&entry_list_lock
);
613 /* Handle specializations by type */
615 case DMI_ENTRY_SYSTEM_EVENT_LOG
:
616 *ret
= dmi_system_event_log(entry
);
619 /* No specialization */
625 /* Create the raw binary file to access the entry */
626 *ret
= sysfs_create_bin_file(&entry
->kobj
, &dmi_entry_raw_attr
);
632 kobject_put(entry
->child
);
633 kobject_put(&entry
->kobj
);
637 static void cleanup_entry_list(void)
639 struct dmi_sysfs_entry
*entry
, *next
;
641 /* No locks, we are on our way out */
642 list_for_each_entry_safe(entry
, next
, &entry_list
, list
) {
643 kobject_put(entry
->child
);
644 kobject_put(&entry
->kobj
);
648 static int __init
dmi_sysfs_init(void)
654 pr_err("dmi-sysfs: dmi entry is absent.\n");
659 dmi_kset
= kset_create_and_add("entries", NULL
, dmi_kobj
);
666 error
= dmi_walk(dmi_sysfs_register_handle
, &val
);
674 pr_debug("dmi-sysfs: loaded.\n");
678 cleanup_entry_list();
679 kset_unregister(dmi_kset
);
683 /* clean up everything. */
684 static void __exit
dmi_sysfs_exit(void)
686 pr_debug("dmi-sysfs: unloading.\n");
687 cleanup_entry_list();
688 kset_unregister(dmi_kset
);
691 module_init(dmi_sysfs_init
);
692 module_exit(dmi_sysfs_exit
);
694 MODULE_AUTHOR("Mike Waychison <mikew@google.com>");
695 MODULE_DESCRIPTION("DMI sysfs support");
696 MODULE_LICENSE("GPL");