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.
24 #include <linux/types.h>
25 #include <linux/proc_fs.h>
26 #include <linux/module.h>
27 #include <linux/smp.h>
28 #include <linux/smp_lock.h>
29 #include <linux/timer.h>
30 #include <linux/vmalloc.h>
32 #include <asm/semaphore.h>
34 #include <asm/uaccess.h>
36 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
37 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
38 MODULE_LICENSE("GPL");
40 static int salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data);
43 const char *name; /* name of the proc entry */
44 unsigned long feature; /* feature bit */
45 struct proc_dir_entry *entry; /* registered entry (removal) */
49 * List {name,feature} pairs for every entry in /proc/sal/<feature>
50 * that this module exports
52 static salinfo_entry_t salinfo_entries[]={
53 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK, },
54 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT, },
55 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT, },
56 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT, },
59 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
61 static char *salinfo_log_name[] = {
68 static struct proc_dir_entry *salinfo_proc_entries[
69 ARRAY_SIZE(salinfo_entries) + /* /proc/sal/bus_lock */
70 ARRAY_SIZE(salinfo_log_name) + /* /proc/sal/{mca,...} */
71 (2 * ARRAY_SIZE(salinfo_log_name)) + /* /proc/sal/mca/{event,data} */
74 /* Some records we get ourselves, some are accessed as saved data in buffers
75 * that are owned by mca.c.
77 struct salinfo_data_saved {
84 /* State transitions. Actions are :-
85 * Write "read <cpunum>" to the data file.
86 * Write "clear <cpunum>" to the data file.
87 * Write "oemdata <cpunum> <offset> to the data file.
88 * Read from the data file.
89 * Close the data file.
91 * Start state is NO_DATA.
94 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
95 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
96 * write "oemdata <cpunum> <offset> -> return -EINVAL.
97 * read data -> return EOF.
98 * close -> unchanged. Free record areas.
101 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
102 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
103 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
104 * read data -> return the INIT/MCA/CMC/CPE record.
105 * close -> unchanged. Keep record areas.
108 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
109 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
110 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
111 * read data -> return the formatted oemdata.
112 * close -> unchanged. Keep record areas.
114 * Closing the data file does not change the state. This allows shell scripts
115 * to manipulate salinfo data, each shell redirection opens the file, does one
116 * action then closes it again. The record areas are only freed at close when
117 * the state is NO_DATA.
125 struct salinfo_data {
126 volatile cpumask_t cpu_event; /* which cpus have outstanding events */
127 struct semaphore sem; /* count of cpus with outstanding events (bits set in cpu_event) */
130 u8 *oemdata; /* decoded oem data */
132 int open; /* single-open to prevent races */
134 u8 saved_num; /* using a saved record? */
135 enum salinfo_state state :8; /* processing state */
137 int cpu_check; /* next CPU to check */
138 struct salinfo_data_saved data_saved[5];/* save last 5 records from mca.c, must be < 255 */
141 static struct salinfo_data salinfo_data[ARRAY_SIZE(salinfo_log_name)];
143 static spinlock_t data_lock, data_saved_lock;
145 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
147 * @sect_header: pointer to the start of the section to decode.
148 * @oemdata: returns vmalloc area containing the decded output.
149 * @oemdata_size: returns length of decoded output (strlen).
151 * Description: If user space asks for oem data to be decoded by the kernel
152 * and/or prom and the platform has set salinfo_platform_oemdata to the address
153 * of a platform specific routine then call that routine. salinfo_platform_oemdata
154 * vmalloc's and formats its output area, returning the address of the text
155 * and its strlen. Returns 0 for success, -ve for error. The callback is
156 * invoked on the cpu that generated the error record.
158 int (*salinfo_platform_oemdata)(const u8 *sect_header, u8 **oemdata, u64 *oemdata_size);
160 struct salinfo_platform_oemdata_parms {
168 salinfo_platform_oemdata_cpu(void *context)
170 struct salinfo_platform_oemdata_parms *parms = context;
171 parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
175 shift1_data_saved (struct salinfo_data *data, int shift)
177 memcpy(data->data_saved+shift, data->data_saved+shift+1,
178 (ARRAY_SIZE(data->data_saved) - (shift+1)) * sizeof(data->data_saved[0]));
179 memset(data->data_saved + ARRAY_SIZE(data->data_saved) - 1, 0,
180 sizeof(data->data_saved[0]));
183 /* This routine is invoked in interrupt context. Note: mca.c enables
184 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
185 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
186 * MCA and INIT records are recorded, a timer event will look for any
187 * outstanding events and wake up the user space code.
189 * The buffer passed from mca.c points to the output from ia64_log_get. This is
190 * a persistent buffer but its contents can change between the interrupt and
191 * when user space processes the record. Save the record id to identify
195 salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
197 struct salinfo_data *data = salinfo_data + type;
198 struct salinfo_data_saved *data_saved;
199 unsigned long flags = 0;
201 int saved_size = ARRAY_SIZE(data->data_saved);
203 BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
206 spin_lock_irqsave(&data_saved_lock, flags);
207 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
208 if (!data_saved->buffer)
211 if (i == saved_size) {
212 if (!data->saved_num) {
213 shift1_data_saved(data, 0);
214 data_saved = data->data_saved + saved_size - 1;
219 data_saved->cpu = smp_processor_id();
220 data_saved->id = ((sal_log_record_header_t *)buffer)->id;
221 data_saved->size = size;
222 data_saved->buffer = buffer;
225 spin_unlock_irqrestore(&data_saved_lock, flags);
227 if (!test_and_set_bit(smp_processor_id(), &data->cpu_event)) {
233 /* Check for outstanding MCA/INIT records every 5 minutes (arbitrary) */
234 #define SALINFO_TIMER_DELAY (5*60*HZ)
235 static struct timer_list salinfo_timer;
238 salinfo_timeout_check(struct salinfo_data *data)
243 for (i = 0; i < NR_CPUS; ++i) {
244 if (test_bit(i, &data->cpu_event)) {
245 /* double up() is not a problem, user space will see no
246 * records for the additional "events".
254 salinfo_timeout (unsigned long arg)
256 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
257 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
258 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
259 add_timer(&salinfo_timer);
263 salinfo_event_open(struct inode *inode, struct file *file)
265 if (!capable(CAP_SYS_ADMIN))
271 salinfo_event_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
273 struct inode *inode = file->f_dentry->d_inode;
274 struct proc_dir_entry *entry = PDE(inode);
275 struct salinfo_data *data = entry->data;
281 if (down_trylock(&data->sem)) {
282 if (file->f_flags & O_NONBLOCK)
284 if (down_interruptible(&data->sem))
289 for (i = 0; i < NR_CPUS; i++) {
290 if (test_bit(n, &data->cpu_event)) {
301 /* events are sticky until the user says "clear" */
304 /* for next read, start checking at next CPU */
305 data->cpu_check = cpu;
306 if (++data->cpu_check == NR_CPUS)
309 snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
314 if (copy_to_user(buffer, cmd, size))
320 static struct file_operations salinfo_event_fops = {
321 .open = salinfo_event_open,
322 .read = salinfo_event_read,
326 salinfo_log_open(struct inode *inode, struct file *file)
328 struct proc_dir_entry *entry = PDE(inode);
329 struct salinfo_data *data = entry->data;
331 if (!capable(CAP_SYS_ADMIN))
334 spin_lock(&data_lock);
336 spin_unlock(&data_lock);
340 spin_unlock(&data_lock);
342 if (data->state == STATE_NO_DATA &&
343 !(data->log_buffer = vmalloc(ia64_sal_get_state_info_size(data->type)))) {
352 salinfo_log_release(struct inode *inode, struct file *file)
354 struct proc_dir_entry *entry = PDE(inode);
355 struct salinfo_data *data = entry->data;
357 if (data->state == STATE_NO_DATA) {
358 vfree(data->log_buffer);
359 vfree(data->oemdata);
360 data->log_buffer = NULL;
361 data->oemdata = NULL;
363 spin_lock(&data_lock);
365 spin_unlock(&data_lock);
370 call_on_cpu(int cpu, void (*fn)(void *), void *arg)
372 cpumask_t save_cpus_allowed, new_cpus_allowed;
373 memcpy(&save_cpus_allowed, ¤t->cpus_allowed, sizeof(save_cpus_allowed));
374 memset(&new_cpus_allowed, 0, sizeof(new_cpus_allowed));
375 set_bit(cpu, &new_cpus_allowed);
376 set_cpus_allowed(current, new_cpus_allowed);
378 set_cpus_allowed(current, save_cpus_allowed);
382 salinfo_log_read_cpu(void *context)
384 struct salinfo_data *data = context;
385 data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
386 if (data->type == SAL_INFO_TYPE_CPE || data->type == SAL_INFO_TYPE_CMC)
387 ia64_sal_clear_state_info(data->type);
391 salinfo_log_new_read(int cpu, struct salinfo_data *data)
393 struct salinfo_data_saved *data_saved;
396 int saved_size = ARRAY_SIZE(data->data_saved);
399 spin_lock_irqsave(&data_saved_lock, flags);
401 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
402 if (data_saved->buffer && data_saved->cpu == cpu) {
403 sal_log_record_header_t *rh = (sal_log_record_header_t *)(data_saved->buffer);
404 data->log_size = data_saved->size;
405 memcpy(data->log_buffer, rh, data->log_size);
406 barrier(); /* id check must not be moved */
407 if (rh->id == data_saved->id) {
408 data->saved_num = i+1;
411 /* saved record changed by mca.c since interrupt, discard it */
412 shift1_data_saved(data, i);
416 spin_unlock_irqrestore(&data_saved_lock, flags);
418 if (!data->saved_num)
419 call_on_cpu(cpu, salinfo_log_read_cpu, data);
420 data->state = data->log_size ? STATE_LOG_RECORD : STATE_NO_DATA;
424 salinfo_log_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
426 struct inode *inode = file->f_dentry->d_inode;
427 struct proc_dir_entry *entry = PDE(inode);
428 struct salinfo_data *data = entry->data;
432 if (data->state == STATE_LOG_RECORD) {
433 buf = data->log_buffer;
434 bufsize = data->log_size;
435 } else if (data->state == STATE_OEMDATA) {
437 bufsize = data->oemdata_size;
442 return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
446 salinfo_log_clear_cpu(void *context)
448 struct salinfo_data *data = context;
449 ia64_sal_clear_state_info(data->type);
453 salinfo_log_clear(struct salinfo_data *data, int cpu)
455 data->state = STATE_NO_DATA;
456 if (!test_bit(cpu, &data->cpu_event))
459 clear_bit(cpu, &data->cpu_event);
460 if (data->saved_num) {
462 spin_lock_irqsave(&data_saved_lock, flags);
463 shift1_data_saved(data, data->saved_num - 1 );
465 spin_unlock_irqrestore(&data_saved_lock, flags);
467 /* ia64_mca_log_sal_error_record or salinfo_log_read_cpu already cleared
470 if (data->type != SAL_INFO_TYPE_CPE && data->type != SAL_INFO_TYPE_CMC)
471 call_on_cpu(cpu, salinfo_log_clear_cpu, data);
472 /* clearing a record may make a new record visible */
473 salinfo_log_new_read(cpu, data);
474 if (data->state == STATE_LOG_RECORD &&
475 !test_and_set_bit(cpu, &data->cpu_event))
481 salinfo_log_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
483 struct inode *inode = file->f_dentry->d_inode;
484 struct proc_dir_entry *entry = PDE(inode);
485 struct salinfo_data *data = entry->data;
494 if (copy_from_user(cmd, buffer, size))
497 if (sscanf(cmd, "read %d", &cpu) == 1) {
498 salinfo_log_new_read(cpu, data);
499 } else if (sscanf(cmd, "clear %d", &cpu) == 1) {
501 if ((ret = salinfo_log_clear(data, cpu)))
503 } else if (sscanf(cmd, "oemdata %d %d", &cpu, &offset) == 2) {
504 if (data->state != STATE_LOG_RECORD && data->state != STATE_OEMDATA)
506 if (offset > data->log_size - sizeof(efi_guid_t))
508 data->state = STATE_OEMDATA;
509 if (salinfo_platform_oemdata) {
510 struct salinfo_platform_oemdata_parms parms = {
511 .efi_guid = data->log_buffer + offset,
512 .oemdata = &data->oemdata,
513 .oemdata_size = &data->oemdata_size
515 call_on_cpu(cpu, salinfo_platform_oemdata_cpu, &parms);
519 data->oemdata_size = 0;
526 static struct file_operations salinfo_data_fops = {
527 .open = salinfo_log_open,
528 .release = salinfo_log_release,
529 .read = salinfo_log_read,
530 .write = salinfo_log_write,
536 struct proc_dir_entry *salinfo_dir; /* /proc/sal dir entry */
537 struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
538 struct proc_dir_entry *dir, *entry;
539 struct salinfo_data *data;
542 salinfo_dir = proc_mkdir("sal", NULL);
546 for (i=0; i < NR_SALINFO_ENTRIES; i++) {
547 /* pass the feature bit in question as misc data */
548 *sdir++ = create_proc_read_entry (salinfo_entries[i].name, 0, salinfo_dir,
549 salinfo_read, (void *)salinfo_entries[i].feature);
552 for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
553 data = salinfo_data + i;
555 sema_init(&data->sem, 0);
556 dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
560 entry = create_proc_entry("event", S_IRUSR, dir);
564 entry->proc_fops = &salinfo_event_fops;
567 entry = create_proc_entry("data", S_IRUSR | S_IWUSR, dir);
571 entry->proc_fops = &salinfo_data_fops;
574 /* we missed any events before now */
576 for (j = 0; j < NR_CPUS; j++)
578 set_bit(j, &data->cpu_event);
581 sema_init(&data->sem, online);
586 *sdir++ = salinfo_dir;
588 init_timer(&salinfo_timer);
589 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
590 salinfo_timer.function = &salinfo_timeout;
591 add_timer(&salinfo_timer);
597 * 'data' contains an integer that corresponds to the feature we're
601 salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
605 len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
607 if (len <= off+count) *eof = 1;
612 if (len>count) len = count;
618 module_init(salinfo_init);