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 * Feb 21 2004 kaos@sgi.com
24 * Copy record contents rather than relying on the mca.c buffers, to cope with
25 * interrupts arriving in mca.c faster than salinfo.c can process them.
28 #include <linux/types.h>
29 #include <linux/proc_fs.h>
30 #include <linux/module.h>
31 #include <linux/smp.h>
32 #include <linux/smp_lock.h>
33 #include <linux/timer.h>
34 #include <linux/vmalloc.h>
36 #include <asm/semaphore.h>
38 #include <asm/uaccess.h>
40 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
41 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
42 MODULE_LICENSE("GPL");
44 static int salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data);
47 const char *name; /* name of the proc entry */
48 unsigned long feature; /* feature bit */
49 struct proc_dir_entry *entry; /* registered entry (removal) */
53 * List {name,feature} pairs for every entry in /proc/sal/<feature>
54 * that this module exports
56 static salinfo_entry_t salinfo_entries[]={
57 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK, },
58 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT, },
59 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT, },
60 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT, },
63 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
65 static char *salinfo_log_name[] = {
72 static struct proc_dir_entry *salinfo_proc_entries[
73 ARRAY_SIZE(salinfo_entries) + /* /proc/sal/bus_lock */
74 ARRAY_SIZE(salinfo_log_name) + /* /proc/sal/{mca,...} */
75 (2 * ARRAY_SIZE(salinfo_log_name)) + /* /proc/sal/mca/{event,data} */
78 /* Allow build with or without large SSI support */
80 #define SCA(x, y) set_cpus_allowed((x), &(y))
82 #define cpumask_t unsigned long
83 #define SCA(x, y) set_cpus_allowed((x), (y))
86 /* Some records we get ourselves, some are accessed as saved data in buffers
87 * that are owned by mca.c.
89 struct salinfo_data_saved {
94 int kmalloced :1; /* buffer was kmalloc'ed */
97 /* State transitions. Actions are :-
98 * Write "read <cpunum>" to the data file.
99 * Write "clear <cpunum>" to the data file.
100 * Write "oemdata <cpunum> <offset> to the data file.
101 * Read from the data file.
102 * Close the data file.
104 * Start state is NO_DATA.
107 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
108 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
109 * write "oemdata <cpunum> <offset> -> return -EINVAL.
110 * read data -> return EOF.
111 * close -> unchanged. Free record areas.
114 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
115 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
116 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
117 * read data -> return the INIT/MCA/CMC/CPE record.
118 * close -> unchanged. Keep record areas.
121 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
122 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
123 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
124 * read data -> return the formatted oemdata.
125 * close -> unchanged. Keep record areas.
127 * Closing the data file does not change the state. This allows shell scripts
128 * to manipulate salinfo data, each shell redirection opens the file, does one
129 * action then closes it again. The record areas are only freed at close when
130 * the state is NO_DATA.
138 struct salinfo_data {
139 volatile cpumask_t cpu_event; /* which cpus have outstanding events */
140 struct semaphore sem; /* count of cpus with outstanding events (bits set in cpu_event) */
143 u8 *oemdata; /* decoded oem data */
145 int open; /* single-open to prevent races */
147 u8 saved_num; /* using a saved record? */
148 enum salinfo_state state :8; /* processing state */
150 int cpu_check; /* next CPU to check */
151 struct salinfo_data_saved data_saved[5];/* save last 5 records from mca.c, must be < 255 */
154 static struct salinfo_data salinfo_data[ARRAY_SIZE(salinfo_log_name)];
156 static spinlock_t data_lock, data_saved_lock;
158 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
160 * @sect_header: pointer to the start of the section to decode.
161 * @oemdata: returns vmalloc area containing the decded output.
162 * @oemdata_size: returns length of decoded output (strlen).
164 * Description: If user space asks for oem data to be decoded by the kernel
165 * and/or prom and the platform has set salinfo_platform_oemdata to the address
166 * of a platform specific routine then call that routine. salinfo_platform_oemdata
167 * vmalloc's and formats its output area, returning the address of the text
168 * and its strlen. Returns 0 for success, -ve for error. The callback is
169 * invoked on the cpu that generated the error record.
171 int (*salinfo_platform_oemdata)(const u8 *sect_header, u8 **oemdata, u64 *oemdata_size);
173 struct salinfo_platform_oemdata_parms {
181 salinfo_platform_oemdata_cpu(void *context)
183 struct salinfo_platform_oemdata_parms *parms = context;
184 parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
188 shift1_data_saved (struct salinfo_data *data, int shift)
190 if (data->data_saved[shift].kmalloced)
191 kfree(data->data_saved[shift].buffer);
192 memcpy(data->data_saved+shift, data->data_saved+shift+1,
193 (ARRAY_SIZE(data->data_saved) - (shift+1)) * sizeof(data->data_saved[0]));
194 memset(data->data_saved + ARRAY_SIZE(data->data_saved) - 1, 0,
195 sizeof(data->data_saved[0]));
198 /* This routine is invoked in interrupt context. Note: mca.c enables
199 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
200 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
201 * MCA and INIT records are recorded, a timer event will look for any
202 * outstanding events and wake up the user space code.
204 * The buffer passed from mca.c points to the output from ia64_log_get. This is
205 * a persistent buffer but its contents can change between the interrupt and
206 * when user space processes the record. Save the record id to identify
210 salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
212 struct salinfo_data *data = salinfo_data + type;
213 struct salinfo_data_saved *data_saved;
214 unsigned long flags = 0;
216 int saved_size = ARRAY_SIZE(data->data_saved);
218 BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
221 spin_lock_irqsave(&data_saved_lock, flags);
222 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
223 if (!data_saved->buffer)
226 if (i == saved_size) {
227 if (!data->saved_num) {
228 shift1_data_saved(data, 0);
229 data_saved = data->data_saved + saved_size - 1;
234 data_saved->cpu = smp_processor_id();
235 data_saved->id = ((sal_log_record_header_t *)buffer)->id;
236 data_saved->size = size;
237 if (irqsafe && (data_saved->buffer = kmalloc(size, GFP_ATOMIC))) {
238 memcpy(data_saved->buffer, buffer, size);
239 data_saved->kmalloced = 1;
241 data_saved->buffer = buffer;
242 data_saved->kmalloced = 0;
246 spin_unlock_irqrestore(&data_saved_lock, flags);
248 if (!test_and_set_bit(smp_processor_id(), &data->cpu_event)) {
254 /* Check for outstanding MCA/INIT records every 5 minutes (arbitrary) */
255 #define SALINFO_TIMER_DELAY (5*60*HZ)
256 static struct timer_list salinfo_timer;
259 salinfo_timeout_check(struct salinfo_data *data)
264 for (i = 0; i < NR_CPUS; ++i) {
265 if (test_bit(i, &data->cpu_event)) {
266 /* double up() is not a problem, user space will see no
267 * records for the additional "events".
275 salinfo_timeout (unsigned long arg)
277 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
278 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
279 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
280 add_timer(&salinfo_timer);
284 salinfo_event_open(struct inode *inode, struct file *file)
286 if (!capable(CAP_SYS_ADMIN))
292 salinfo_event_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
294 struct inode *inode = file->f_dentry->d_inode;
295 struct proc_dir_entry *entry = PDE(inode);
296 struct salinfo_data *data = entry->data;
302 if (down_trylock(&data->sem)) {
303 if (file->f_flags & O_NONBLOCK)
305 if (down_interruptible(&data->sem))
310 for (i = 0; i < NR_CPUS; i++) {
311 if (test_bit(n, &data->cpu_event)) {
322 /* events are sticky until the user says "clear" */
325 /* for next read, start checking at next CPU */
326 data->cpu_check = cpu;
327 if (++data->cpu_check == NR_CPUS)
330 snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
335 if (copy_to_user(buffer, cmd, size))
341 static struct file_operations salinfo_event_fops = {
342 .open = salinfo_event_open,
343 .read = salinfo_event_read,
347 salinfo_log_open(struct inode *inode, struct file *file)
349 struct proc_dir_entry *entry = PDE(inode);
350 struct salinfo_data *data = entry->data;
352 if (!capable(CAP_SYS_ADMIN))
355 spin_lock(&data_lock);
357 spin_unlock(&data_lock);
361 spin_unlock(&data_lock);
363 if (data->state == STATE_NO_DATA &&
364 !(data->log_buffer = vmalloc(ia64_sal_get_state_info_size(data->type)))) {
373 salinfo_log_release(struct inode *inode, struct file *file)
375 struct proc_dir_entry *entry = PDE(inode);
376 struct salinfo_data *data = entry->data;
378 if (data->state == STATE_NO_DATA) {
379 vfree(data->log_buffer);
380 vfree(data->oemdata);
381 data->log_buffer = NULL;
382 data->oemdata = NULL;
384 spin_lock(&data_lock);
386 spin_unlock(&data_lock);
391 call_on_cpu(int cpu, void (*fn)(void *), void *arg)
393 cpumask_t save_cpus_allowed, new_cpus_allowed;
394 memcpy(&save_cpus_allowed, ¤t->cpus_allowed, sizeof(save_cpus_allowed));
395 memset(&new_cpus_allowed, 0, sizeof(new_cpus_allowed));
396 set_bit(cpu, &new_cpus_allowed);
397 SCA(current, new_cpus_allowed);
399 SCA(current, save_cpus_allowed);
403 salinfo_log_read_cpu(void *context)
405 struct salinfo_data *data = context;
406 data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
407 if (data->type == SAL_INFO_TYPE_CPE || data->type == SAL_INFO_TYPE_CMC)
408 ia64_sal_clear_state_info(data->type);
412 salinfo_log_new_read(int cpu, struct salinfo_data *data)
414 struct salinfo_data_saved *data_saved;
417 int saved_size = ARRAY_SIZE(data->data_saved);
420 spin_lock_irqsave(&data_saved_lock, flags);
422 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
423 if (data_saved->buffer && data_saved->cpu == cpu) {
424 sal_log_record_header_t *rh = (sal_log_record_header_t *)(data_saved->buffer);
425 data->log_size = data_saved->size;
426 memcpy(data->log_buffer, rh, data->log_size);
427 barrier(); /* id check must not be moved */
428 if (rh->id == data_saved->id) {
429 data->saved_num = i+1;
432 /* saved record changed by mca.c since interrupt, discard it */
433 shift1_data_saved(data, i);
437 spin_unlock_irqrestore(&data_saved_lock, flags);
439 if (!data->saved_num)
440 call_on_cpu(cpu, salinfo_log_read_cpu, data);
441 data->state = data->log_size ? STATE_LOG_RECORD : STATE_NO_DATA;
445 salinfo_log_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
447 struct inode *inode = file->f_dentry->d_inode;
448 struct proc_dir_entry *entry = PDE(inode);
449 struct salinfo_data *data = entry->data;
456 if (data->state == STATE_LOG_RECORD) {
457 buf = data->log_buffer;
458 bufsize = data->log_size;
459 } else if (data->state == STATE_OEMDATA) {
461 bufsize = data->oemdata_size;
466 if (pos != (unsigned)pos || pos >= bufsize)
470 size = bufsize - pos;
473 if (copy_to_user(buffer, saldata, size))
481 salinfo_log_clear_cpu(void *context)
483 struct salinfo_data *data = context;
484 ia64_sal_clear_state_info(data->type);
488 salinfo_log_clear(struct salinfo_data *data, int cpu)
490 data->state = STATE_NO_DATA;
491 if (!test_bit(cpu, &data->cpu_event))
494 clear_bit(cpu, &data->cpu_event);
495 if (data->saved_num) {
497 spin_lock_irqsave(&data_saved_lock, flags);
498 shift1_data_saved(data, data->saved_num - 1 );
500 spin_unlock_irqrestore(&data_saved_lock, flags);
502 /* ia64_mca_log_sal_error_record or salinfo_log_read_cpu already cleared
505 if (data->type != SAL_INFO_TYPE_CPE && data->type != SAL_INFO_TYPE_CMC)
506 call_on_cpu(cpu, salinfo_log_clear_cpu, data);
507 /* clearing a record may make a new record visible */
508 salinfo_log_new_read(cpu, data);
509 if (data->state == STATE_LOG_RECORD &&
510 !test_and_set_bit(cpu, &data->cpu_event))
516 salinfo_log_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
518 struct inode *inode = file->f_dentry->d_inode;
519 struct proc_dir_entry *entry = PDE(inode);
520 struct salinfo_data *data = entry->data;
529 if (copy_from_user(cmd, buffer, size))
532 if (sscanf(cmd, "read %d", &cpu) == 1) {
533 salinfo_log_new_read(cpu, data);
534 } else if (sscanf(cmd, "clear %d", &cpu) == 1) {
536 if ((ret = salinfo_log_clear(data, cpu)))
538 } else if (sscanf(cmd, "oemdata %d %d", &cpu, &offset) == 2) {
539 if (data->state != STATE_LOG_RECORD && data->state != STATE_OEMDATA)
541 if (offset > data->log_size - sizeof(efi_guid_t))
543 data->state = STATE_OEMDATA;
544 if (salinfo_platform_oemdata) {
545 struct salinfo_platform_oemdata_parms parms = {
546 .efi_guid = data->log_buffer + offset,
547 .oemdata = &data->oemdata,
548 .oemdata_size = &data->oemdata_size
550 call_on_cpu(cpu, salinfo_platform_oemdata_cpu, &parms);
554 data->oemdata_size = 0;
561 static struct file_operations salinfo_data_fops = {
562 .open = salinfo_log_open,
563 .release = salinfo_log_release,
564 .read = salinfo_log_read,
565 .write = salinfo_log_write,
571 struct proc_dir_entry *salinfo_dir; /* /proc/sal dir entry */
572 struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
573 struct proc_dir_entry *dir, *entry;
574 struct salinfo_data *data;
577 salinfo_dir = proc_mkdir("sal", NULL);
581 for (i=0; i < NR_SALINFO_ENTRIES; i++) {
582 /* pass the feature bit in question as misc data */
583 *sdir++ = create_proc_read_entry (salinfo_entries[i].name, 0, salinfo_dir,
584 salinfo_read, (void *)salinfo_entries[i].feature);
587 for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
588 data = salinfo_data + i;
590 sema_init(&data->sem, 0);
591 dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
595 entry = create_proc_entry("event", S_IRUSR, dir);
599 entry->proc_fops = &salinfo_event_fops;
602 entry = create_proc_entry("data", S_IRUSR | S_IWUSR, dir);
606 entry->proc_fops = &salinfo_data_fops;
609 /* we missed any events before now */
611 for (j = 0; j < NR_CPUS; j++)
613 set_bit(j, &data->cpu_event);
616 sema_init(&data->sem, online);
621 *sdir++ = salinfo_dir;
623 init_timer(&salinfo_timer);
624 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
625 salinfo_timer.function = &salinfo_timeout;
626 add_timer(&salinfo_timer);
632 * 'data' contains an integer that corresponds to the feature we're
636 salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
640 len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
642 if (len <= off+count) *eof = 1;
647 if (len>count) len = count;
653 module_init(salinfo_init);