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.
27 #include <linux/types.h>
28 #include <linux/proc_fs.h>
29 #include <linux/module.h>
30 #include <linux/smp.h>
31 #include <linux/smp_lock.h>
32 #include <linux/timer.h>
33 #include <linux/vmalloc.h>
35 #include <asm/semaphore.h>
37 #include <asm/uaccess.h>
39 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
40 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
41 MODULE_LICENSE("GPL");
43 static int salinfo_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
);
46 const char *name
; /* name of the proc entry */
47 unsigned long feature
; /* feature bit */
48 struct proc_dir_entry
*entry
; /* registered entry (removal) */
52 * List {name,feature} pairs for every entry in /proc/sal/<feature>
53 * that this module exports
55 static salinfo_entry_t salinfo_entries
[]={
56 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK
, },
57 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT
, },
58 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT
, },
59 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT
, },
62 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
64 static char *salinfo_log_name
[] = {
71 static struct proc_dir_entry
*salinfo_proc_entries
[
72 ARRAY_SIZE(salinfo_entries
) + /* /proc/sal/bus_lock */
73 ARRAY_SIZE(salinfo_log_name
) + /* /proc/sal/{mca,...} */
74 (2 * ARRAY_SIZE(salinfo_log_name
)) + /* /proc/sal/mca/{event,data} */
77 /* Some records we get ourselves, some are accessed as saved data in buffers
78 * that are owned by mca.c.
80 struct salinfo_data_saved
{
87 /* State transitions. Actions are :-
88 * Write "read <cpunum>" to the data file.
89 * Write "clear <cpunum>" to the data file.
90 * Write "oemdata <cpunum> <offset> to the data file.
91 * Read from the data file.
92 * Close the data file.
94 * Start state is NO_DATA.
97 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
98 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
99 * write "oemdata <cpunum> <offset> -> return -EINVAL.
100 * read data -> return EOF.
101 * close -> unchanged. Free record areas.
104 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
105 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
106 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
107 * read data -> return the INIT/MCA/CMC/CPE record.
108 * close -> unchanged. Keep record areas.
111 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
112 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
113 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
114 * read data -> return the formatted oemdata.
115 * close -> unchanged. Keep record areas.
117 * Closing the data file does not change the state. This allows shell scripts
118 * to manipulate salinfo data, each shell redirection opens the file, does one
119 * action then closes it again. The record areas are only freed at close when
120 * the state is NO_DATA.
128 struct salinfo_data
{
129 volatile cpumask_t cpu_event
; /* which cpus have outstanding events */
130 struct semaphore sem
; /* count of cpus with outstanding events (bits set in cpu_event) */
133 u8
*oemdata
; /* decoded oem data */
135 int open
; /* single-open to prevent races */
137 u8 saved_num
; /* using a saved record? */
138 enum salinfo_state state
:8; /* processing state */
140 int cpu_check
; /* next CPU to check */
141 struct salinfo_data_saved data_saved
[5];/* save last 5 records from mca.c, must be < 255 */
144 static struct salinfo_data salinfo_data
[ARRAY_SIZE(salinfo_log_name
)];
146 static DEFINE_SPINLOCK(data_lock
);
147 static DEFINE_SPINLOCK(data_saved_lock
);
149 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
151 * @sect_header: pointer to the start of the section to decode.
152 * @oemdata: returns vmalloc area containing the decded output.
153 * @oemdata_size: returns length of decoded output (strlen).
155 * Description: If user space asks for oem data to be decoded by the kernel
156 * and/or prom and the platform has set salinfo_platform_oemdata to the address
157 * of a platform specific routine then call that routine. salinfo_platform_oemdata
158 * vmalloc's and formats its output area, returning the address of the text
159 * and its strlen. Returns 0 for success, -ve for error. The callback is
160 * invoked on the cpu that generated the error record.
162 int (*salinfo_platform_oemdata
)(const u8
*sect_header
, u8
**oemdata
, u64
*oemdata_size
);
164 struct salinfo_platform_oemdata_parms
{
172 salinfo_platform_oemdata_cpu(void *context
)
174 struct salinfo_platform_oemdata_parms
*parms
= context
;
175 parms
->ret
= salinfo_platform_oemdata(parms
->efi_guid
, parms
->oemdata
, parms
->oemdata_size
);
179 shift1_data_saved (struct salinfo_data
*data
, int shift
)
181 memcpy(data
->data_saved
+shift
, data
->data_saved
+shift
+1,
182 (ARRAY_SIZE(data
->data_saved
) - (shift
+1)) * sizeof(data
->data_saved
[0]));
183 memset(data
->data_saved
+ ARRAY_SIZE(data
->data_saved
) - 1, 0,
184 sizeof(data
->data_saved
[0]));
187 /* This routine is invoked in interrupt context. Note: mca.c enables
188 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
189 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
190 * MCA and INIT records are recorded, a timer event will look for any
191 * outstanding events and wake up the user space code.
193 * The buffer passed from mca.c points to the output from ia64_log_get. This is
194 * a persistent buffer but its contents can change between the interrupt and
195 * when user space processes the record. Save the record id to identify
199 salinfo_log_wakeup(int type
, u8
*buffer
, u64 size
, int irqsafe
)
201 struct salinfo_data
*data
= salinfo_data
+ type
;
202 struct salinfo_data_saved
*data_saved
;
203 unsigned long flags
= 0;
205 int saved_size
= ARRAY_SIZE(data
->data_saved
);
207 BUG_ON(type
>= ARRAY_SIZE(salinfo_log_name
));
210 spin_lock_irqsave(&data_saved_lock
, flags
);
211 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
212 if (!data_saved
->buffer
)
215 if (i
== saved_size
) {
216 if (!data
->saved_num
) {
217 shift1_data_saved(data
, 0);
218 data_saved
= data
->data_saved
+ saved_size
- 1;
223 data_saved
->cpu
= smp_processor_id();
224 data_saved
->id
= ((sal_log_record_header_t
*)buffer
)->id
;
225 data_saved
->size
= size
;
226 data_saved
->buffer
= buffer
;
229 spin_unlock_irqrestore(&data_saved_lock
, flags
);
231 if (!test_and_set_bit(smp_processor_id(), &data
->cpu_event
)) {
237 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
238 #define SALINFO_TIMER_DELAY (60*HZ)
239 static struct timer_list salinfo_timer
;
242 salinfo_timeout_check(struct salinfo_data
*data
)
247 for (i
= 0; i
< NR_CPUS
; ++i
) {
248 if (test_bit(i
, &data
->cpu_event
)) {
249 /* double up() is not a problem, user space will see no
250 * records for the additional "events".
258 salinfo_timeout (unsigned long arg
)
260 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_MCA
);
261 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_INIT
);
262 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
263 add_timer(&salinfo_timer
);
267 salinfo_event_open(struct inode
*inode
, struct file
*file
)
269 if (!capable(CAP_SYS_ADMIN
))
275 salinfo_event_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
277 struct inode
*inode
= file
->f_dentry
->d_inode
;
278 struct proc_dir_entry
*entry
= PDE(inode
);
279 struct salinfo_data
*data
= entry
->data
;
285 if (down_trylock(&data
->sem
)) {
286 if (file
->f_flags
& O_NONBLOCK
)
288 if (down_interruptible(&data
->sem
))
293 for (i
= 0; i
< NR_CPUS
; i
++) {
294 if (test_bit(n
, &data
->cpu_event
)) {
305 /* events are sticky until the user says "clear" */
308 /* for next read, start checking at next CPU */
309 data
->cpu_check
= cpu
;
310 if (++data
->cpu_check
== NR_CPUS
)
313 snprintf(cmd
, sizeof(cmd
), "read %d\n", cpu
);
318 if (copy_to_user(buffer
, cmd
, size
))
324 static struct file_operations salinfo_event_fops
= {
325 .open
= salinfo_event_open
,
326 .read
= salinfo_event_read
,
330 salinfo_log_open(struct inode
*inode
, struct file
*file
)
332 struct proc_dir_entry
*entry
= PDE(inode
);
333 struct salinfo_data
*data
= entry
->data
;
335 if (!capable(CAP_SYS_ADMIN
))
338 spin_lock(&data_lock
);
340 spin_unlock(&data_lock
);
344 spin_unlock(&data_lock
);
346 if (data
->state
== STATE_NO_DATA
&&
347 !(data
->log_buffer
= vmalloc(ia64_sal_get_state_info_size(data
->type
)))) {
356 salinfo_log_release(struct inode
*inode
, struct file
*file
)
358 struct proc_dir_entry
*entry
= PDE(inode
);
359 struct salinfo_data
*data
= entry
->data
;
361 if (data
->state
== STATE_NO_DATA
) {
362 vfree(data
->log_buffer
);
363 vfree(data
->oemdata
);
364 data
->log_buffer
= NULL
;
365 data
->oemdata
= NULL
;
367 spin_lock(&data_lock
);
369 spin_unlock(&data_lock
);
374 call_on_cpu(int cpu
, void (*fn
)(void *), void *arg
)
376 cpumask_t save_cpus_allowed
, new_cpus_allowed
;
377 memcpy(&save_cpus_allowed
, ¤t
->cpus_allowed
, sizeof(save_cpus_allowed
));
378 memset(&new_cpus_allowed
, 0, sizeof(new_cpus_allowed
));
379 set_bit(cpu
, &new_cpus_allowed
);
380 set_cpus_allowed(current
, new_cpus_allowed
);
382 set_cpus_allowed(current
, save_cpus_allowed
);
386 salinfo_log_read_cpu(void *context
)
388 struct salinfo_data
*data
= context
;
389 sal_log_record_header_t
*rh
;
390 data
->log_size
= ia64_sal_get_state_info(data
->type
, (u64
*) data
->log_buffer
);
391 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
392 /* Clear corrected errors as they are read from SAL */
393 if (rh
->severity
== sal_log_severity_corrected
)
394 ia64_sal_clear_state_info(data
->type
);
398 salinfo_log_new_read(int cpu
, struct salinfo_data
*data
)
400 struct salinfo_data_saved
*data_saved
;
403 int saved_size
= ARRAY_SIZE(data
->data_saved
);
406 spin_lock_irqsave(&data_saved_lock
, flags
);
408 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
409 if (data_saved
->buffer
&& data_saved
->cpu
== cpu
) {
410 sal_log_record_header_t
*rh
= (sal_log_record_header_t
*)(data_saved
->buffer
);
411 data
->log_size
= data_saved
->size
;
412 memcpy(data
->log_buffer
, rh
, data
->log_size
);
413 barrier(); /* id check must not be moved */
414 if (rh
->id
== data_saved
->id
) {
415 data
->saved_num
= i
+1;
418 /* saved record changed by mca.c since interrupt, discard it */
419 shift1_data_saved(data
, i
);
423 spin_unlock_irqrestore(&data_saved_lock
, flags
);
425 if (!data
->saved_num
)
426 call_on_cpu(cpu
, salinfo_log_read_cpu
, data
);
427 if (!data
->log_size
) {
428 data
->state
= STATE_NO_DATA
;
429 clear_bit(cpu
, &data
->cpu_event
);
431 data
->state
= STATE_LOG_RECORD
;
436 salinfo_log_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
438 struct inode
*inode
= file
->f_dentry
->d_inode
;
439 struct proc_dir_entry
*entry
= PDE(inode
);
440 struct salinfo_data
*data
= entry
->data
;
444 if (data
->state
== STATE_LOG_RECORD
) {
445 buf
= data
->log_buffer
;
446 bufsize
= data
->log_size
;
447 } else if (data
->state
== STATE_OEMDATA
) {
449 bufsize
= data
->oemdata_size
;
454 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, bufsize
);
458 salinfo_log_clear_cpu(void *context
)
460 struct salinfo_data
*data
= context
;
461 ia64_sal_clear_state_info(data
->type
);
465 salinfo_log_clear(struct salinfo_data
*data
, int cpu
)
467 sal_log_record_header_t
*rh
;
468 data
->state
= STATE_NO_DATA
;
469 if (!test_bit(cpu
, &data
->cpu_event
))
472 clear_bit(cpu
, &data
->cpu_event
);
473 if (data
->saved_num
) {
475 spin_lock_irqsave(&data_saved_lock
, flags
);
476 shift1_data_saved(data
, data
->saved_num
- 1 );
478 spin_unlock_irqrestore(&data_saved_lock
, flags
);
480 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
481 /* Corrected errors have already been cleared from SAL */
482 if (rh
->severity
!= sal_log_severity_corrected
)
483 call_on_cpu(cpu
, salinfo_log_clear_cpu
, data
);
484 /* clearing a record may make a new record visible */
485 salinfo_log_new_read(cpu
, data
);
486 if (data
->state
== STATE_LOG_RECORD
&&
487 !test_and_set_bit(cpu
, &data
->cpu_event
))
493 salinfo_log_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
495 struct inode
*inode
= file
->f_dentry
->d_inode
;
496 struct proc_dir_entry
*entry
= PDE(inode
);
497 struct salinfo_data
*data
= entry
->data
;
506 if (copy_from_user(cmd
, buffer
, size
))
509 if (sscanf(cmd
, "read %d", &cpu
) == 1) {
510 salinfo_log_new_read(cpu
, data
);
511 } else if (sscanf(cmd
, "clear %d", &cpu
) == 1) {
513 if ((ret
= salinfo_log_clear(data
, cpu
)))
515 } else if (sscanf(cmd
, "oemdata %d %d", &cpu
, &offset
) == 2) {
516 if (data
->state
!= STATE_LOG_RECORD
&& data
->state
!= STATE_OEMDATA
)
518 if (offset
> data
->log_size
- sizeof(efi_guid_t
))
520 data
->state
= STATE_OEMDATA
;
521 if (salinfo_platform_oemdata
) {
522 struct salinfo_platform_oemdata_parms parms
= {
523 .efi_guid
= data
->log_buffer
+ offset
,
524 .oemdata
= &data
->oemdata
,
525 .oemdata_size
= &data
->oemdata_size
527 call_on_cpu(cpu
, salinfo_platform_oemdata_cpu
, &parms
);
531 data
->oemdata_size
= 0;
538 static struct file_operations salinfo_data_fops
= {
539 .open
= salinfo_log_open
,
540 .release
= salinfo_log_release
,
541 .read
= salinfo_log_read
,
542 .write
= salinfo_log_write
,
548 struct proc_dir_entry
*salinfo_dir
; /* /proc/sal dir entry */
549 struct proc_dir_entry
**sdir
= salinfo_proc_entries
; /* keeps track of every entry */
550 struct proc_dir_entry
*dir
, *entry
;
551 struct salinfo_data
*data
;
554 salinfo_dir
= proc_mkdir("sal", NULL
);
558 for (i
=0; i
< NR_SALINFO_ENTRIES
; i
++) {
559 /* pass the feature bit in question as misc data */
560 *sdir
++ = create_proc_read_entry (salinfo_entries
[i
].name
, 0, salinfo_dir
,
561 salinfo_read
, (void *)salinfo_entries
[i
].feature
);
564 for (i
= 0; i
< ARRAY_SIZE(salinfo_log_name
); i
++) {
565 data
= salinfo_data
+ i
;
567 sema_init(&data
->sem
, 0);
568 dir
= proc_mkdir(salinfo_log_name
[i
], salinfo_dir
);
572 entry
= create_proc_entry("event", S_IRUSR
, dir
);
576 entry
->proc_fops
= &salinfo_event_fops
;
579 entry
= create_proc_entry("data", S_IRUSR
| S_IWUSR
, dir
);
583 entry
->proc_fops
= &salinfo_data_fops
;
586 /* we missed any events before now */
588 for (j
= 0; j
< NR_CPUS
; j
++)
590 set_bit(j
, &data
->cpu_event
);
593 sema_init(&data
->sem
, online
);
598 *sdir
++ = salinfo_dir
;
600 init_timer(&salinfo_timer
);
601 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
602 salinfo_timer
.function
= &salinfo_timeout
;
603 add_timer(&salinfo_timer
);
609 * 'data' contains an integer that corresponds to the feature we're
613 salinfo_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
617 len
= sprintf(page
, (sal_platform_features
& (unsigned long)data
) ? "1\n" : "0\n");
619 if (len
<= off
+count
) *eof
= 1;
624 if (len
>count
) len
= count
;
630 module_init(salinfo_init
);