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 spinlock_t data_lock
, data_saved_lock
;
148 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
150 * @sect_header: pointer to the start of the section to decode.
151 * @oemdata: returns vmalloc area containing the decded output.
152 * @oemdata_size: returns length of decoded output (strlen).
154 * Description: If user space asks for oem data to be decoded by the kernel
155 * and/or prom and the platform has set salinfo_platform_oemdata to the address
156 * of a platform specific routine then call that routine. salinfo_platform_oemdata
157 * vmalloc's and formats its output area, returning the address of the text
158 * and its strlen. Returns 0 for success, -ve for error. The callback is
159 * invoked on the cpu that generated the error record.
161 int (*salinfo_platform_oemdata
)(const u8
*sect_header
, u8
**oemdata
, u64
*oemdata_size
);
163 struct salinfo_platform_oemdata_parms
{
171 salinfo_platform_oemdata_cpu(void *context
)
173 struct salinfo_platform_oemdata_parms
*parms
= context
;
174 parms
->ret
= salinfo_platform_oemdata(parms
->efi_guid
, parms
->oemdata
, parms
->oemdata_size
);
178 shift1_data_saved (struct salinfo_data
*data
, int shift
)
180 memcpy(data
->data_saved
+shift
, data
->data_saved
+shift
+1,
181 (ARRAY_SIZE(data
->data_saved
) - (shift
+1)) * sizeof(data
->data_saved
[0]));
182 memset(data
->data_saved
+ ARRAY_SIZE(data
->data_saved
) - 1, 0,
183 sizeof(data
->data_saved
[0]));
186 /* This routine is invoked in interrupt context. Note: mca.c enables
187 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
188 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
189 * MCA and INIT records are recorded, a timer event will look for any
190 * outstanding events and wake up the user space code.
192 * The buffer passed from mca.c points to the output from ia64_log_get. This is
193 * a persistent buffer but its contents can change between the interrupt and
194 * when user space processes the record. Save the record id to identify
198 salinfo_log_wakeup(int type
, u8
*buffer
, u64 size
, int irqsafe
)
200 struct salinfo_data
*data
= salinfo_data
+ type
;
201 struct salinfo_data_saved
*data_saved
;
202 unsigned long flags
= 0;
204 int saved_size
= ARRAY_SIZE(data
->data_saved
);
206 BUG_ON(type
>= ARRAY_SIZE(salinfo_log_name
));
209 spin_lock_irqsave(&data_saved_lock
, flags
);
210 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
211 if (!data_saved
->buffer
)
214 if (i
== saved_size
) {
215 if (!data
->saved_num
) {
216 shift1_data_saved(data
, 0);
217 data_saved
= data
->data_saved
+ saved_size
- 1;
222 data_saved
->cpu
= smp_processor_id();
223 data_saved
->id
= ((sal_log_record_header_t
*)buffer
)->id
;
224 data_saved
->size
= size
;
225 data_saved
->buffer
= buffer
;
228 spin_unlock_irqrestore(&data_saved_lock
, flags
);
230 if (!test_and_set_bit(smp_processor_id(), &data
->cpu_event
)) {
236 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
237 #define SALINFO_TIMER_DELAY (60*HZ)
238 static struct timer_list salinfo_timer
;
241 salinfo_timeout_check(struct salinfo_data
*data
)
246 for (i
= 0; i
< NR_CPUS
; ++i
) {
247 if (test_bit(i
, &data
->cpu_event
)) {
248 /* double up() is not a problem, user space will see no
249 * records for the additional "events".
257 salinfo_timeout (unsigned long arg
)
259 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_MCA
);
260 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_INIT
);
261 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
262 add_timer(&salinfo_timer
);
266 salinfo_event_open(struct inode
*inode
, struct file
*file
)
268 if (!capable(CAP_SYS_ADMIN
))
274 salinfo_event_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
276 struct inode
*inode
= file
->f_dentry
->d_inode
;
277 struct proc_dir_entry
*entry
= PDE(inode
);
278 struct salinfo_data
*data
= entry
->data
;
284 if (down_trylock(&data
->sem
)) {
285 if (file
->f_flags
& O_NONBLOCK
)
287 if (down_interruptible(&data
->sem
))
292 for (i
= 0; i
< NR_CPUS
; i
++) {
293 if (test_bit(n
, &data
->cpu_event
)) {
304 /* events are sticky until the user says "clear" */
307 /* for next read, start checking at next CPU */
308 data
->cpu_check
= cpu
;
309 if (++data
->cpu_check
== NR_CPUS
)
312 snprintf(cmd
, sizeof(cmd
), "read %d\n", cpu
);
317 if (copy_to_user(buffer
, cmd
, size
))
323 static struct file_operations salinfo_event_fops
= {
324 .open
= salinfo_event_open
,
325 .read
= salinfo_event_read
,
329 salinfo_log_open(struct inode
*inode
, struct file
*file
)
331 struct proc_dir_entry
*entry
= PDE(inode
);
332 struct salinfo_data
*data
= entry
->data
;
334 if (!capable(CAP_SYS_ADMIN
))
337 spin_lock(&data_lock
);
339 spin_unlock(&data_lock
);
343 spin_unlock(&data_lock
);
345 if (data
->state
== STATE_NO_DATA
&&
346 !(data
->log_buffer
= vmalloc(ia64_sal_get_state_info_size(data
->type
)))) {
355 salinfo_log_release(struct inode
*inode
, struct file
*file
)
357 struct proc_dir_entry
*entry
= PDE(inode
);
358 struct salinfo_data
*data
= entry
->data
;
360 if (data
->state
== STATE_NO_DATA
) {
361 vfree(data
->log_buffer
);
362 vfree(data
->oemdata
);
363 data
->log_buffer
= NULL
;
364 data
->oemdata
= NULL
;
366 spin_lock(&data_lock
);
368 spin_unlock(&data_lock
);
373 call_on_cpu(int cpu
, void (*fn
)(void *), void *arg
)
375 cpumask_t save_cpus_allowed
, new_cpus_allowed
;
376 memcpy(&save_cpus_allowed
, ¤t
->cpus_allowed
, sizeof(save_cpus_allowed
));
377 memset(&new_cpus_allowed
, 0, sizeof(new_cpus_allowed
));
378 set_bit(cpu
, &new_cpus_allowed
);
379 set_cpus_allowed(current
, new_cpus_allowed
);
381 set_cpus_allowed(current
, save_cpus_allowed
);
385 salinfo_log_read_cpu(void *context
)
387 struct salinfo_data
*data
= context
;
388 sal_log_record_header_t
*rh
;
389 data
->log_size
= ia64_sal_get_state_info(data
->type
, (u64
*) data
->log_buffer
);
390 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
391 /* Clear corrected errors as they are read from SAL */
392 if (rh
->severity
== sal_log_severity_corrected
)
393 ia64_sal_clear_state_info(data
->type
);
397 salinfo_log_new_read(int cpu
, struct salinfo_data
*data
)
399 struct salinfo_data_saved
*data_saved
;
402 int saved_size
= ARRAY_SIZE(data
->data_saved
);
405 spin_lock_irqsave(&data_saved_lock
, flags
);
407 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
408 if (data_saved
->buffer
&& data_saved
->cpu
== cpu
) {
409 sal_log_record_header_t
*rh
= (sal_log_record_header_t
*)(data_saved
->buffer
);
410 data
->log_size
= data_saved
->size
;
411 memcpy(data
->log_buffer
, rh
, data
->log_size
);
412 barrier(); /* id check must not be moved */
413 if (rh
->id
== data_saved
->id
) {
414 data
->saved_num
= i
+1;
417 /* saved record changed by mca.c since interrupt, discard it */
418 shift1_data_saved(data
, i
);
422 spin_unlock_irqrestore(&data_saved_lock
, flags
);
424 if (!data
->saved_num
)
425 call_on_cpu(cpu
, salinfo_log_read_cpu
, data
);
426 if (!data
->log_size
) {
427 data
->state
= STATE_NO_DATA
;
428 clear_bit(cpu
, &data
->cpu_event
);
430 data
->state
= STATE_LOG_RECORD
;
435 salinfo_log_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
437 struct inode
*inode
= file
->f_dentry
->d_inode
;
438 struct proc_dir_entry
*entry
= PDE(inode
);
439 struct salinfo_data
*data
= entry
->data
;
443 if (data
->state
== STATE_LOG_RECORD
) {
444 buf
= data
->log_buffer
;
445 bufsize
= data
->log_size
;
446 } else if (data
->state
== STATE_OEMDATA
) {
448 bufsize
= data
->oemdata_size
;
453 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, bufsize
);
457 salinfo_log_clear_cpu(void *context
)
459 struct salinfo_data
*data
= context
;
460 ia64_sal_clear_state_info(data
->type
);
464 salinfo_log_clear(struct salinfo_data
*data
, int cpu
)
466 sal_log_record_header_t
*rh
;
467 data
->state
= STATE_NO_DATA
;
468 if (!test_bit(cpu
, &data
->cpu_event
))
471 clear_bit(cpu
, &data
->cpu_event
);
472 if (data
->saved_num
) {
474 spin_lock_irqsave(&data_saved_lock
, flags
);
475 shift1_data_saved(data
, data
->saved_num
- 1 );
477 spin_unlock_irqrestore(&data_saved_lock
, flags
);
479 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
480 /* Corrected errors have already been cleared from SAL */
481 if (rh
->severity
!= sal_log_severity_corrected
)
482 call_on_cpu(cpu
, salinfo_log_clear_cpu
, data
);
483 /* clearing a record may make a new record visible */
484 salinfo_log_new_read(cpu
, data
);
485 if (data
->state
== STATE_LOG_RECORD
&&
486 !test_and_set_bit(cpu
, &data
->cpu_event
))
492 salinfo_log_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
494 struct inode
*inode
= file
->f_dentry
->d_inode
;
495 struct proc_dir_entry
*entry
= PDE(inode
);
496 struct salinfo_data
*data
= entry
->data
;
505 if (copy_from_user(cmd
, buffer
, size
))
508 if (sscanf(cmd
, "read %d", &cpu
) == 1) {
509 salinfo_log_new_read(cpu
, data
);
510 } else if (sscanf(cmd
, "clear %d", &cpu
) == 1) {
512 if ((ret
= salinfo_log_clear(data
, cpu
)))
514 } else if (sscanf(cmd
, "oemdata %d %d", &cpu
, &offset
) == 2) {
515 if (data
->state
!= STATE_LOG_RECORD
&& data
->state
!= STATE_OEMDATA
)
517 if (offset
> data
->log_size
- sizeof(efi_guid_t
))
519 data
->state
= STATE_OEMDATA
;
520 if (salinfo_platform_oemdata
) {
521 struct salinfo_platform_oemdata_parms parms
= {
522 .efi_guid
= data
->log_buffer
+ offset
,
523 .oemdata
= &data
->oemdata
,
524 .oemdata_size
= &data
->oemdata_size
526 call_on_cpu(cpu
, salinfo_platform_oemdata_cpu
, &parms
);
530 data
->oemdata_size
= 0;
537 static struct file_operations salinfo_data_fops
= {
538 .open
= salinfo_log_open
,
539 .release
= salinfo_log_release
,
540 .read
= salinfo_log_read
,
541 .write
= salinfo_log_write
,
547 struct proc_dir_entry
*salinfo_dir
; /* /proc/sal dir entry */
548 struct proc_dir_entry
**sdir
= salinfo_proc_entries
; /* keeps track of every entry */
549 struct proc_dir_entry
*dir
, *entry
;
550 struct salinfo_data
*data
;
553 salinfo_dir
= proc_mkdir("sal", NULL
);
557 for (i
=0; i
< NR_SALINFO_ENTRIES
; i
++) {
558 /* pass the feature bit in question as misc data */
559 *sdir
++ = create_proc_read_entry (salinfo_entries
[i
].name
, 0, salinfo_dir
,
560 salinfo_read
, (void *)salinfo_entries
[i
].feature
);
563 for (i
= 0; i
< ARRAY_SIZE(salinfo_log_name
); i
++) {
564 data
= salinfo_data
+ i
;
566 sema_init(&data
->sem
, 0);
567 dir
= proc_mkdir(salinfo_log_name
[i
], salinfo_dir
);
571 entry
= create_proc_entry("event", S_IRUSR
, dir
);
575 entry
->proc_fops
= &salinfo_event_fops
;
578 entry
= create_proc_entry("data", S_IRUSR
| S_IWUSR
, dir
);
582 entry
->proc_fops
= &salinfo_data_fops
;
585 /* we missed any events before now */
587 for (j
= 0; j
< NR_CPUS
; j
++)
589 set_bit(j
, &data
->cpu_event
);
592 sema_init(&data
->sem
, online
);
597 *sdir
++ = salinfo_dir
;
599 init_timer(&salinfo_timer
);
600 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
601 salinfo_timer
.function
= &salinfo_timeout
;
602 add_timer(&salinfo_timer
);
608 * 'data' contains an integer that corresponds to the feature we're
612 salinfo_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
616 len
= sprintf(page
, (sal_platform_features
& (unsigned long)data
) ? "1\n" : "0\n");
618 if (len
<= off
+count
) *eof
= 1;
623 if (len
>count
) len
= count
;
629 module_init(salinfo_init
);