4 * Creates entries in /proc/sal for various system features.
6 * Copyright (c) 2003, 2006 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.
31 * Jan 5 2006 kaos@sgi.com
32 * Handle hotplug cpus coming online.
33 * Handle hotplug cpus going offline while they still have outstanding records.
34 * Use the cpu_* macros consistently.
35 * Replace the counting semaphore with a mutex and a test if the cpumask is non-empty.
36 * Modify the locking to make the test for "work to do" an atomic operation.
39 #include <linux/capability.h>
40 #include <linux/cpu.h>
41 #include <linux/types.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <linux/module.h>
45 #include <linux/smp.h>
46 #include <linux/timer.h>
47 #include <linux/vmalloc.h>
48 #include <linux/semaphore.h>
51 #include <linux/uaccess.h>
53 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
54 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
55 MODULE_LICENSE("GPL");
57 static const struct file_operations proc_salinfo_fops
;
60 const char *name
; /* name of the proc entry */
61 unsigned long feature
; /* feature bit */
62 struct proc_dir_entry
*entry
; /* registered entry (removal) */
66 * List {name,feature} pairs for every entry in /proc/sal/<feature>
67 * that this module exports
69 static const salinfo_entry_t salinfo_entries
[]={
70 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK
, },
71 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT
, },
72 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT
, },
73 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT
, },
76 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
78 static char *salinfo_log_name
[] = {
85 static struct proc_dir_entry
*salinfo_proc_entries
[
86 ARRAY_SIZE(salinfo_entries
) + /* /proc/sal/bus_lock */
87 ARRAY_SIZE(salinfo_log_name
) + /* /proc/sal/{mca,...} */
88 (2 * ARRAY_SIZE(salinfo_log_name
)) + /* /proc/sal/mca/{event,data} */
91 /* Some records we get ourselves, some are accessed as saved data in buffers
92 * that are owned by mca.c.
94 struct salinfo_data_saved
{
101 /* State transitions. Actions are :-
102 * Write "read <cpunum>" to the data file.
103 * Write "clear <cpunum>" to the data file.
104 * Write "oemdata <cpunum> <offset> to the data file.
105 * Read from the data file.
106 * Close the data file.
108 * Start state is NO_DATA.
111 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
112 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
113 * write "oemdata <cpunum> <offset> -> return -EINVAL.
114 * read data -> return EOF.
115 * close -> unchanged. Free record areas.
118 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
119 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
120 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
121 * read data -> return the INIT/MCA/CMC/CPE record.
122 * close -> unchanged. Keep record areas.
125 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
126 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
127 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
128 * read data -> return the formatted oemdata.
129 * close -> unchanged. Keep record areas.
131 * Closing the data file does not change the state. This allows shell scripts
132 * to manipulate salinfo data, each shell redirection opens the file, does one
133 * action then closes it again. The record areas are only freed at close when
134 * the state is NO_DATA.
142 struct salinfo_data
{
143 cpumask_t cpu_event
; /* which cpus have outstanding events */
144 wait_queue_head_t read_wait
;
147 u8
*oemdata
; /* decoded oem data */
149 int open
; /* single-open to prevent races */
151 u8 saved_num
; /* using a saved record? */
152 enum salinfo_state state
:8; /* processing state */
154 int cpu_check
; /* next CPU to check */
155 struct salinfo_data_saved data_saved
[5];/* save last 5 records from mca.c, must be < 255 */
158 static struct salinfo_data salinfo_data
[ARRAY_SIZE(salinfo_log_name
)];
160 static DEFINE_SPINLOCK(data_lock
);
161 static DEFINE_SPINLOCK(data_saved_lock
);
163 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
165 * @sect_header: pointer to the start of the section to decode.
166 * @oemdata: returns vmalloc area containing the decoded output.
167 * @oemdata_size: returns length of decoded output (strlen).
169 * Description: If user space asks for oem data to be decoded by the kernel
170 * and/or prom and the platform has set salinfo_platform_oemdata to the address
171 * of a platform specific routine then call that routine. salinfo_platform_oemdata
172 * vmalloc's and formats its output area, returning the address of the text
173 * and its strlen. Returns 0 for success, -ve for error. The callback is
174 * invoked on the cpu that generated the error record.
176 int (*salinfo_platform_oemdata
)(const u8
*sect_header
, u8
**oemdata
, u64
*oemdata_size
);
178 struct salinfo_platform_oemdata_parms
{
185 salinfo_platform_oemdata_cpu(void *context
)
187 struct salinfo_platform_oemdata_parms
*parms
= context
;
189 return salinfo_platform_oemdata(parms
->efi_guid
, parms
->oemdata
, parms
->oemdata_size
);
193 shift1_data_saved (struct salinfo_data
*data
, int shift
)
195 memcpy(data
->data_saved
+shift
, data
->data_saved
+shift
+1,
196 (ARRAY_SIZE(data
->data_saved
) - (shift
+1)) * sizeof(data
->data_saved
[0]));
197 memset(data
->data_saved
+ ARRAY_SIZE(data
->data_saved
) - 1, 0,
198 sizeof(data
->data_saved
[0]));
201 /* This routine is invoked in interrupt context. Note: mca.c enables
202 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
203 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
204 * MCA and INIT records are recorded, a timer event will look for any
205 * outstanding events and wake up the user space code.
207 * The buffer passed from mca.c points to the output from ia64_log_get. This is
208 * a persistent buffer but its contents can change between the interrupt and
209 * when user space processes the record. Save the record id to identify
210 * changes. If the buffer is NULL then just update the bitmap.
213 salinfo_log_wakeup(int type
, u8
*buffer
, u64 size
, int irqsafe
)
215 struct salinfo_data
*data
= salinfo_data
+ type
;
216 struct salinfo_data_saved
*data_saved
;
217 unsigned long flags
= 0;
219 int saved_size
= ARRAY_SIZE(data
->data_saved
);
221 BUG_ON(type
>= ARRAY_SIZE(salinfo_log_name
));
224 spin_lock_irqsave(&data_saved_lock
, flags
);
226 for (i
= 0, data_saved
= data
->data_saved
; i
< saved_size
; ++i
, ++data_saved
) {
227 if (!data_saved
->buffer
)
230 if (i
== saved_size
) {
231 if (!data
->saved_num
) {
232 shift1_data_saved(data
, 0);
233 data_saved
= data
->data_saved
+ saved_size
- 1;
238 data_saved
->cpu
= smp_processor_id();
239 data_saved
->id
= ((sal_log_record_header_t
*)buffer
)->id
;
240 data_saved
->size
= size
;
241 data_saved
->buffer
= buffer
;
244 cpumask_set_cpu(smp_processor_id(), &data
->cpu_event
);
246 wake_up_interruptible(&data
->read_wait
);
247 spin_unlock_irqrestore(&data_saved_lock
, flags
);
251 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
252 #define SALINFO_TIMER_DELAY (60*HZ)
253 static struct timer_list salinfo_timer
;
254 extern void ia64_mlogbuf_dump(void);
257 salinfo_timeout_check(struct salinfo_data
*data
)
261 if (!cpumask_empty(&data
->cpu_event
))
262 wake_up_interruptible(&data
->read_wait
);
266 salinfo_timeout (unsigned long arg
)
269 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_MCA
);
270 salinfo_timeout_check(salinfo_data
+ SAL_INFO_TYPE_INIT
);
271 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
272 add_timer(&salinfo_timer
);
276 salinfo_event_open(struct inode
*inode
, struct file
*file
)
278 if (!capable(CAP_SYS_ADMIN
))
284 salinfo_event_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
286 struct salinfo_data
*data
= PDE_DATA(file_inode(file
));
292 if (cpumask_empty(&data
->cpu_event
)) {
293 if (file
->f_flags
& O_NONBLOCK
)
295 if (wait_event_interruptible(data
->read_wait
,
296 !cpumask_empty(&data
->cpu_event
)))
301 for (i
= 0; i
< nr_cpu_ids
; i
++) {
302 if (cpumask_test_cpu(n
, &data
->cpu_event
)) {
303 if (!cpu_online(n
)) {
304 cpumask_clear_cpu(n
, &data
->cpu_event
);
310 if (++n
== nr_cpu_ids
)
319 /* for next read, start checking at next CPU */
320 data
->cpu_check
= cpu
;
321 if (++data
->cpu_check
== nr_cpu_ids
)
324 snprintf(cmd
, sizeof(cmd
), "read %d\n", cpu
);
329 if (copy_to_user(buffer
, cmd
, size
))
335 static const struct file_operations salinfo_event_fops
= {
336 .open
= salinfo_event_open
,
337 .read
= salinfo_event_read
,
338 .llseek
= noop_llseek
,
342 salinfo_log_open(struct inode
*inode
, struct file
*file
)
344 struct salinfo_data
*data
= PDE_DATA(inode
);
346 if (!capable(CAP_SYS_ADMIN
))
349 spin_lock(&data_lock
);
351 spin_unlock(&data_lock
);
355 spin_unlock(&data_lock
);
357 if (data
->state
== STATE_NO_DATA
&&
358 !(data
->log_buffer
= vmalloc(ia64_sal_get_state_info_size(data
->type
)))) {
367 salinfo_log_release(struct inode
*inode
, struct file
*file
)
369 struct salinfo_data
*data
= PDE_DATA(inode
);
371 if (data
->state
== STATE_NO_DATA
) {
372 vfree(data
->log_buffer
);
373 vfree(data
->oemdata
);
374 data
->log_buffer
= NULL
;
375 data
->oemdata
= NULL
;
377 spin_lock(&data_lock
);
379 spin_unlock(&data_lock
);
384 salinfo_log_read_cpu(void *context
)
386 struct salinfo_data
*data
= context
;
387 sal_log_record_header_t
*rh
;
388 data
->log_size
= ia64_sal_get_state_info(data
->type
, (u64
*) data
->log_buffer
);
389 rh
= (sal_log_record_header_t
*)(data
->log_buffer
);
390 /* Clear corrected errors as they are read from SAL */
391 if (rh
->severity
== sal_log_severity_corrected
)
392 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 work_on_cpu_safe(cpu
, salinfo_log_read_cpu
, data
);
426 if (!data
->log_size
) {
427 data
->state
= STATE_NO_DATA
;
428 cpumask_clear_cpu(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 salinfo_data
*data
= PDE_DATA(file_inode(file
));
441 if (data
->state
== STATE_LOG_RECORD
) {
442 buf
= data
->log_buffer
;
443 bufsize
= data
->log_size
;
444 } else if (data
->state
== STATE_OEMDATA
) {
446 bufsize
= data
->oemdata_size
;
451 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, bufsize
);
455 salinfo_log_clear_cpu(void *context
)
457 struct salinfo_data
*data
= context
;
459 ia64_sal_clear_state_info(data
->type
);
464 salinfo_log_clear(struct salinfo_data
*data
, int cpu
)
466 sal_log_record_header_t
*rh
;
468 spin_lock_irqsave(&data_saved_lock
, flags
);
469 data
->state
= STATE_NO_DATA
;
470 if (!cpumask_test_cpu(cpu
, &data
->cpu_event
)) {
471 spin_unlock_irqrestore(&data_saved_lock
, flags
);
474 cpumask_clear_cpu(cpu
, &data
->cpu_event
);
475 if (data
->saved_num
) {
476 shift1_data_saved(data
, data
->saved_num
- 1);
479 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 work_on_cpu_safe(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 spin_lock_irqsave(&data_saved_lock
, flags
);
488 cpumask_set_cpu(cpu
, &data
->cpu_event
);
489 wake_up_interruptible(&data
->read_wait
);
490 spin_unlock_irqrestore(&data_saved_lock
, flags
);
496 salinfo_log_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
498 struct salinfo_data
*data
= PDE_DATA(file_inode(file
));
507 if (copy_from_user(cmd
, buffer
, size
))
510 if (sscanf(cmd
, "read %d", &cpu
) == 1) {
511 salinfo_log_new_read(cpu
, data
);
512 } else if (sscanf(cmd
, "clear %d", &cpu
) == 1) {
514 if ((ret
= salinfo_log_clear(data
, cpu
)))
516 } else if (sscanf(cmd
, "oemdata %d %d", &cpu
, &offset
) == 2) {
517 if (data
->state
!= STATE_LOG_RECORD
&& data
->state
!= STATE_OEMDATA
)
519 if (offset
> data
->log_size
- sizeof(efi_guid_t
))
521 data
->state
= STATE_OEMDATA
;
522 if (salinfo_platform_oemdata
) {
523 struct salinfo_platform_oemdata_parms parms
= {
524 .efi_guid
= data
->log_buffer
+ offset
,
525 .oemdata
= &data
->oemdata
,
526 .oemdata_size
= &data
->oemdata_size
528 count
= work_on_cpu_safe(cpu
, salinfo_platform_oemdata_cpu
,
531 data
->oemdata_size
= 0;
538 static const 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
,
543 .llseek
= default_llseek
,
546 static int salinfo_cpu_online(unsigned int cpu
)
548 unsigned int i
, end
= ARRAY_SIZE(salinfo_data
);
549 struct salinfo_data
*data
;
551 spin_lock_irq(&data_saved_lock
);
552 for (i
= 0, data
= salinfo_data
; i
< end
; ++i
, ++data
) {
553 cpumask_set_cpu(cpu
, &data
->cpu_event
);
554 wake_up_interruptible(&data
->read_wait
);
556 spin_unlock_irq(&data_saved_lock
);
560 static int salinfo_cpu_pre_down(unsigned int cpu
)
562 unsigned int i
, end
= ARRAY_SIZE(salinfo_data
);
563 struct salinfo_data
*data
;
565 spin_lock_irq(&data_saved_lock
);
566 for (i
= 0, data
= salinfo_data
; i
< end
; ++i
, ++data
) {
567 struct salinfo_data_saved
*data_saved
;
568 int j
= ARRAY_SIZE(data
->data_saved
) - 1;
570 for (data_saved
= data
->data_saved
+ j
; j
>= 0;
572 if (data_saved
->buffer
&& data_saved
->cpu
== cpu
)
573 shift1_data_saved(data
, j
);
575 cpumask_clear_cpu(cpu
, &data
->cpu_event
);
577 spin_unlock_irq(&data_saved_lock
);
584 struct proc_dir_entry
*salinfo_dir
; /* /proc/sal dir entry */
585 struct proc_dir_entry
**sdir
= salinfo_proc_entries
; /* keeps track of every entry */
586 struct proc_dir_entry
*dir
, *entry
;
587 struct salinfo_data
*data
;
590 salinfo_dir
= proc_mkdir("sal", NULL
);
594 for (i
=0; i
< NR_SALINFO_ENTRIES
; i
++) {
595 /* pass the feature bit in question as misc data */
596 *sdir
++ = proc_create_data(salinfo_entries
[i
].name
, 0, salinfo_dir
,
598 (void *)salinfo_entries
[i
].feature
);
601 for (i
= 0; i
< ARRAY_SIZE(salinfo_log_name
); i
++) {
602 data
= salinfo_data
+ i
;
604 init_waitqueue_head(&data
->read_wait
);
605 dir
= proc_mkdir(salinfo_log_name
[i
], salinfo_dir
);
609 entry
= proc_create_data("event", S_IRUSR
, dir
,
610 &salinfo_event_fops
, data
);
615 entry
= proc_create_data("data", S_IRUSR
| S_IWUSR
, dir
,
616 &salinfo_data_fops
, data
);
624 *sdir
++ = salinfo_dir
;
626 init_timer(&salinfo_timer
);
627 salinfo_timer
.expires
= jiffies
+ SALINFO_TIMER_DELAY
;
628 salinfo_timer
.function
= &salinfo_timeout
;
629 add_timer(&salinfo_timer
);
631 i
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "ia64/salinfo:online",
632 salinfo_cpu_online
, salinfo_cpu_pre_down
);
638 * 'data' contains an integer that corresponds to the feature we're
641 static int proc_salinfo_show(struct seq_file
*m
, void *v
)
643 unsigned long data
= (unsigned long)v
;
644 seq_puts(m
, (sal_platform_features
& data
) ? "1\n" : "0\n");
648 static int proc_salinfo_open(struct inode
*inode
, struct file
*file
)
650 return single_open(file
, proc_salinfo_show
, PDE_DATA(inode
));
653 static const struct file_operations proc_salinfo_fops
= {
654 .open
= proc_salinfo_open
,
657 .release
= single_release
,
660 module_init(salinfo_init
);