4 * Creates entries in /proc/sal for various system features.
6 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
7 * Copyright (c) 2003 Hewlett-Packard Co
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
10 * 10/30/2001 jbarnes@sgi.com copied much of Stephane's palinfo
11 * code to create this file
12 * Oct 23 2003 kaos@sgi.com
13 * Replace IPI with set_cpus_allowed() to read a record from the required cpu.
14 * Redesign salinfo log processing to separate interrupt and user space
16 * Cache the record across multi-block reads from user space.
18 * Delete module_exit and MOD_INC/DEC_COUNT, salinfo cannot be a module.
20 * Jan 28 2004 kaos@sgi.com
21 * Periodically check for outstanding MCA or INIT records.
23 * Dec 5 2004 kaos@sgi.com
24 * Standardize which records are cleared automatically.
26 * Aug 18 2005 kaos@sgi.com
27 * mca.c may not pass a buffer, a NULL buffer just indicates that a new
28 * record is available in SAL.
29 * Replace some NR_CPUS by cpus_online, for hotplug cpu.
32 #include <linux/capability.h>
33 #include <linux/types.h>
34 #include <linux/proc_fs.h>
35 #include <linux/module.h>
36 #include <linux/smp.h>
37 #include <linux/smp_lock.h>
38 #include <linux/timer.h>
39 #include <linux/vmalloc.h>
41 #include <asm/semaphore.h>
43 #include <asm/uaccess.h>
45 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
46 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
47 MODULE_LICENSE("GPL");
49 static int salinfo_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
);
52 const char *name
; /* name of the proc entry */
53 unsigned long feature
; /* feature bit */
54 struct proc_dir_entry
*entry
; /* registered entry (removal) */
58 * List {name,feature} pairs for every entry in /proc/sal/<feature>
59 * that this module exports
61 static salinfo_entry_t salinfo_entries
[]={
62 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK
, },
63 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT
, },
64 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT
, },
65 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT
, },
68 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
70 static char *salinfo_log_name
[] = {
77 static struct proc_dir_entry
*salinfo_proc_entries
[
78 ARRAY_SIZE(salinfo_entries
) + /* /proc/sal/bus_lock */
79 ARRAY_SIZE(salinfo_log_name
) + /* /proc/sal/{mca,...} */
80 (2 * ARRAY_SIZE(salinfo_log_name
)) + /* /proc/sal/mca/{event,data} */
83 /* Some records we get ourselves, some are accessed as saved data in buffers
84 * that are owned by mca.c.
86 struct salinfo_data_saved
{
93 /* State transitions. Actions are :-
94 * Write "read <cpunum>" to the data file.
95 * Write "clear <cpunum>" to the data file.
96 * Write "oemdata <cpunum> <offset> to the data file.
97 * Read from the data file.
98 * Close the data file.
100 * Start state is NO_DATA.
103 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
104 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
105 * write "oemdata <cpunum> <offset> -> return -EINVAL.
106 * read data -> return EOF.
107 * close -> unchanged. Free record areas.
110 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
111 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
112 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
113 * read data -> return the INIT/MCA/CMC/CPE record.
114 * close -> unchanged. Keep record areas.
117 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
118 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
119 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
120 * read data -> return the formatted oemdata.
121 * close -> unchanged. Keep record areas.
123 * Closing the data file does not change the state. This allows shell scripts
124 * to manipulate salinfo data, each shell redirection opens the file, does one
125 * action then closes it again. The record areas are only freed at close when
126 * the state is NO_DATA.
134 struct salinfo_data
{
135 volatile cpumask_t cpu_event
; /* which cpus have outstanding events */
136 struct semaphore sem
; /* count of cpus with outstanding events (bits set in cpu_event) */
139 u8
*oemdata
; /* decoded oem data */
141 int open
; /* single-open to prevent races */
143 u8 saved_num
; /* using a saved record? */
144 enum salinfo_state state
:8; /* processing state */
146 int cpu_check
; /* next CPU to check */
147 struct salinfo_data_saved data_saved
[5];/* save last 5 records from mca.c, must be < 255 */
150 static struct salinfo_data salinfo_data
[ARRAY_SIZE(salinfo_log_name
)];
152 static DEFINE_SPINLOCK(data_lock
);
153 static DEFINE_SPINLOCK(data_saved_lock
);
155 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
157 * @sect_header: pointer to the start of the section to decode.
158 * @oemdata: returns vmalloc area containing the decded output.
159 * @oemdata_size: returns length of decoded output (strlen).
161 * Description: If user space asks for oem data to be decoded by the kernel
162 * and/or prom and the platform has set salinfo_platform_oemdata to the address
163 * of a platform specific routine then call that routine. salinfo_platform_oemdata
164 * vmalloc's and formats its output area, returning the address of the text
165 * and its strlen. Returns 0 for success, -ve for error. The callback is
166 * invoked on the cpu that generated the error record.
168 int (*salinfo_platform_oemdata
)(const u8
*sect_header
, u8
**oemdata
, u64
*oemdata_size
);
170 struct salinfo_platform_oemdata_parms
{
178 salinfo_platform_oemdata_cpu(void *context
)
180 struct salinfo_platform_oemdata_parms
*parms
= context
;
181 parms
->ret
= salinfo_platform_oemdata(parms
->efi_guid
, parms
->oemdata
, parms
->oemdata_size
);
185 shift1_data_saved (struct salinfo_data
*data
, int shift
)
187 memcpy(data
->data_saved
+shift
, data
->data_saved
+shift
+1,
188 (ARRAY_SIZE(data
->data_saved
) - (shift
+1)) * sizeof(data
->data_saved
[0]));
189 memset(data
->data_saved
+ ARRAY_SIZE(data
->data_saved
) - 1, 0,
190 sizeof(data
->data_saved
[0]));
193 /* This routine is invoked in interrupt context. Note: mca.c enables
194 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
195 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
196 * MCA and INIT records are recorded, a timer event will look for any
197 * outstanding events and wake up the user space code.
199 * The buffer passed from mca.c points to the output from ia64_log_get. This is
200 * a persistent buffer but its contents can change between the interrupt and
201 * when user space processes the record. Save the record id to identify
202 * changes. If the buffer is NULL then just update the bitmap.
205 salinfo_log_wakeup(int type
, u8
*buffer
, u64 size
, int irqsafe
)
207 struct salinfo_data
*data
= salinfo_data
+ type
;
208 struct salinfo_data_saved
*data_saved
;
209 unsigned long flags
= 0;
211 int saved_size
= ARRAY_SIZE(data
->data_saved
);
213 BUG_ON(type
>= ARRAY_SIZE(salinfo_log_name
));
217 spin_lock_irqsave(&data_saved_lock
, flags
);
218 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
219 if (!data_saved
->buffer
)
222 if (i
== saved_size
) {
223 if (!data
->saved_num
) {
224 shift1_data_saved(data
, 0);
225 data_saved
= data
->data_saved
+ saved_size
- 1;
230 data_saved
->cpu
= smp_processor_id();
231 data_saved
->id
= ((sal_log_record_header_t
*)buffer
)->id
;
232 data_saved
->size
= size
;
233 data_saved
->buffer
= buffer
;
236 spin_unlock_irqrestore(&data_saved_lock
, flags
);
239 if (!test_and_set_bit(smp_processor_id(), &data
->cpu_event
)) {
245 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
246 #define SALINFO_TIMER_DELAY (60*HZ)
247 static struct timer_list salinfo_timer
;
250 salinfo_timeout_check(struct salinfo_data
*data
)
255 for_each_online_cpu(i
) {
256 if (test_bit(i
, &data
->cpu_event
)) {
257 /* double up() is not a problem, user space will see no
258 * records for the additional "events".
266 salinfo_timeout (unsigned long arg
)
268 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_MCA
);
269 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_INIT
);
270 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
271 add_timer(&salinfo_timer
);
275 salinfo_event_open(struct inode
*inode
, struct file
*file
)
277 if (!capable(CAP_SYS_ADMIN
))
283 salinfo_event_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
285 struct inode
*inode
= file
->f_dentry
->d_inode
;
286 struct proc_dir_entry
*entry
= PDE(inode
);
287 struct salinfo_data
*data
= entry
->data
;
293 if (down_trylock(&data
->sem
)) {
294 if (file
->f_flags
& O_NONBLOCK
)
296 if (down_interruptible(&data
->sem
))
301 for (i
= 0; i
< NR_CPUS
; i
++) {
302 if (test_bit(n
, &data
->cpu_event
) && cpu_online(n
)) {
313 /* events are sticky until the user says "clear" */
316 /* for next read, start checking at next CPU */
317 data
->cpu_check
= cpu
;
318 if (++data
->cpu_check
== NR_CPUS
)
321 snprintf(cmd
, sizeof(cmd
), "read %d\n", cpu
);
326 if (copy_to_user(buffer
, cmd
, size
))
332 static struct file_operations salinfo_event_fops
= {
333 .open
= salinfo_event_open
,
334 .read
= salinfo_event_read
,
338 salinfo_log_open(struct inode
*inode
, struct file
*file
)
340 struct proc_dir_entry
*entry
= PDE(inode
);
341 struct salinfo_data
*data
= entry
->data
;
343 if (!capable(CAP_SYS_ADMIN
))
346 spin_lock(&data_lock
);
348 spin_unlock(&data_lock
);
352 spin_unlock(&data_lock
);
354 if (data
->state
== STATE_NO_DATA
&&
355 !(data
->log_buffer
= vmalloc(ia64_sal_get_state_info_size(data
->type
)))) {
364 salinfo_log_release(struct inode
*inode
, struct file
*file
)
366 struct proc_dir_entry
*entry
= PDE(inode
);
367 struct salinfo_data
*data
= entry
->data
;
369 if (data
->state
== STATE_NO_DATA
) {
370 vfree(data
->log_buffer
);
371 vfree(data
->oemdata
);
372 data
->log_buffer
= NULL
;
373 data
->oemdata
= NULL
;
375 spin_lock(&data_lock
);
377 spin_unlock(&data_lock
);
382 call_on_cpu(int cpu
, void (*fn
)(void *), void *arg
)
384 cpumask_t save_cpus_allowed
, new_cpus_allowed
;
385 memcpy(&save_cpus_allowed
, ¤t
->cpus_allowed
, sizeof(save_cpus_allowed
));
386 memset(&new_cpus_allowed
, 0, sizeof(new_cpus_allowed
));
387 set_bit(cpu
, &new_cpus_allowed
);
388 set_cpus_allowed(current
, new_cpus_allowed
);
390 set_cpus_allowed(current
, save_cpus_allowed
);
394 salinfo_log_read_cpu(void *context
)
396 struct salinfo_data
*data
= context
;
397 sal_log_record_header_t
*rh
;
398 data
->log_size
= ia64_sal_get_state_info(data
->type
, (u64
*) data
->log_buffer
);
399 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
400 /* Clear corrected errors as they are read from SAL */
401 if (rh
->severity
== sal_log_severity_corrected
)
402 ia64_sal_clear_state_info(data
->type
);
406 salinfo_log_new_read(int cpu
, struct salinfo_data
*data
)
408 struct salinfo_data_saved
*data_saved
;
411 int saved_size
= ARRAY_SIZE(data
->data_saved
);
414 spin_lock_irqsave(&data_saved_lock
, flags
);
416 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
417 if (data_saved
->buffer
&& data_saved
->cpu
== cpu
) {
418 sal_log_record_header_t
*rh
= (sal_log_record_header_t
*)(data_saved
->buffer
);
419 data
->log_size
= data_saved
->size
;
420 memcpy(data
->log_buffer
, rh
, data
->log_size
);
421 barrier(); /* id check must not be moved */
422 if (rh
->id
== data_saved
->id
) {
423 data
->saved_num
= i
+1;
426 /* saved record changed by mca.c since interrupt, discard it */
427 shift1_data_saved(data
, i
);
431 spin_unlock_irqrestore(&data_saved_lock
, flags
);
433 if (!data
->saved_num
)
434 call_on_cpu(cpu
, salinfo_log_read_cpu
, data
);
435 if (!data
->log_size
) {
436 data
->state
= STATE_NO_DATA
;
437 clear_bit(cpu
, &data
->cpu_event
);
439 data
->state
= STATE_LOG_RECORD
;
444 salinfo_log_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
446 struct inode
*inode
= file
->f_dentry
->d_inode
;
447 struct proc_dir_entry
*entry
= PDE(inode
);
448 struct salinfo_data
*data
= entry
->data
;
452 if (data
->state
== STATE_LOG_RECORD
) {
453 buf
= data
->log_buffer
;
454 bufsize
= data
->log_size
;
455 } else if (data
->state
== STATE_OEMDATA
) {
457 bufsize
= data
->oemdata_size
;
462 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, bufsize
);
466 salinfo_log_clear_cpu(void *context
)
468 struct salinfo_data
*data
= context
;
469 ia64_sal_clear_state_info(data
->type
);
473 salinfo_log_clear(struct salinfo_data
*data
, int cpu
)
475 sal_log_record_header_t
*rh
;
476 data
->state
= STATE_NO_DATA
;
477 if (!test_bit(cpu
, &data
->cpu_event
))
480 clear_bit(cpu
, &data
->cpu_event
);
481 if (data
->saved_num
) {
483 spin_lock_irqsave(&data_saved_lock
, flags
);
484 shift1_data_saved(data
, data
->saved_num
- 1 );
486 spin_unlock_irqrestore(&data_saved_lock
, flags
);
488 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
489 /* Corrected errors have already been cleared from SAL */
490 if (rh
->severity
!= sal_log_severity_corrected
)
491 call_on_cpu(cpu
, salinfo_log_clear_cpu
, data
);
492 /* clearing a record may make a new record visible */
493 salinfo_log_new_read(cpu
, data
);
494 if (data
->state
== STATE_LOG_RECORD
&&
495 !test_and_set_bit(cpu
, &data
->cpu_event
))
501 salinfo_log_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
503 struct inode
*inode
= file
->f_dentry
->d_inode
;
504 struct proc_dir_entry
*entry
= PDE(inode
);
505 struct salinfo_data
*data
= entry
->data
;
514 if (copy_from_user(cmd
, buffer
, size
))
517 if (sscanf(cmd
, "read %d", &cpu
) == 1) {
518 salinfo_log_new_read(cpu
, data
);
519 } else if (sscanf(cmd
, "clear %d", &cpu
) == 1) {
521 if ((ret
= salinfo_log_clear(data
, cpu
)))
523 } else if (sscanf(cmd
, "oemdata %d %d", &cpu
, &offset
) == 2) {
524 if (data
->state
!= STATE_LOG_RECORD
&& data
->state
!= STATE_OEMDATA
)
526 if (offset
> data
->log_size
- sizeof(efi_guid_t
))
528 data
->state
= STATE_OEMDATA
;
529 if (salinfo_platform_oemdata
) {
530 struct salinfo_platform_oemdata_parms parms
= {
531 .efi_guid
= data
->log_buffer
+ offset
,
532 .oemdata
= &data
->oemdata
,
533 .oemdata_size
= &data
->oemdata_size
535 call_on_cpu(cpu
, salinfo_platform_oemdata_cpu
, &parms
);
539 data
->oemdata_size
= 0;
546 static struct file_operations salinfo_data_fops
= {
547 .open
= salinfo_log_open
,
548 .release
= salinfo_log_release
,
549 .read
= salinfo_log_read
,
550 .write
= salinfo_log_write
,
556 struct proc_dir_entry
*salinfo_dir
; /* /proc/sal dir entry */
557 struct proc_dir_entry
**sdir
= salinfo_proc_entries
; /* keeps track of every entry */
558 struct proc_dir_entry
*dir
, *entry
;
559 struct salinfo_data
*data
;
562 salinfo_dir
= proc_mkdir("sal", NULL
);
566 for (i
=0; i
< NR_SALINFO_ENTRIES
; i
++) {
567 /* pass the feature bit in question as misc data */
568 *sdir
++ = create_proc_read_entry (salinfo_entries
[i
].name
, 0, salinfo_dir
,
569 salinfo_read
, (void *)salinfo_entries
[i
].feature
);
572 for (i
= 0; i
< ARRAY_SIZE(salinfo_log_name
); i
++) {
573 data
= salinfo_data
+ i
;
575 sema_init(&data
->sem
, 0);
576 dir
= proc_mkdir(salinfo_log_name
[i
], salinfo_dir
);
580 entry
= create_proc_entry("event", S_IRUSR
, dir
);
584 entry
->proc_fops
= &salinfo_event_fops
;
587 entry
= create_proc_entry("data", S_IRUSR
| S_IWUSR
, dir
);
591 entry
->proc_fops
= &salinfo_data_fops
;
594 /* we missed any events before now */
596 for_each_online_cpu(j
) {
597 set_bit(j
, &data
->cpu_event
);
600 sema_init(&data
->sem
, online
);
605 *sdir
++ = salinfo_dir
;
607 init_timer(&salinfo_timer
);
608 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
609 salinfo_timer
.function
= &salinfo_timeout
;
610 add_timer(&salinfo_timer
);
616 * 'data' contains an integer that corresponds to the feature we're
620 salinfo_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
624 len
= sprintf(page
, (sal_platform_features
& (unsigned long)data
) ? "1\n" : "0\n");
626 if (len
<= off
+count
) *eof
= 1;
631 if (len
>count
) len
= count
;
637 module_init(salinfo_init
);