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/module.h>
44 #include <linux/smp.h>
45 #include <linux/smp_lock.h>
46 #include <linux/timer.h>
47 #include <linux/vmalloc.h>
49 #include <asm/semaphore.h>
51 #include <asm/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 int salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data);
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 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 struct semaphore mutex;
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 decded 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 /* Kick the mutex that tells user space that there is work to do. Instead of
186 * trying to track the state of the mutex across multiple cpus, in user
187 * context, interrupt context, non-maskable interrupt context and hotplug cpu,
188 * it is far easier just to grab the mutex if it is free then release it.
190 * This routine must be called with data_saved_lock held, to make the down/up
194 salinfo_work_to_do(struct salinfo_data *data)
196 down_trylock(&data->mutex);
201 salinfo_platform_oemdata_cpu(void *context)
203 struct salinfo_platform_oemdata_parms *parms = context;
204 parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
208 shift1_data_saved (struct salinfo_data *data, int shift)
210 memcpy(data->data_saved+shift, data->data_saved+shift+1,
211 (ARRAY_SIZE(data->data_saved) - (shift+1)) * sizeof(data->data_saved[0]));
212 memset(data->data_saved + ARRAY_SIZE(data->data_saved) - 1, 0,
213 sizeof(data->data_saved[0]));
216 /* This routine is invoked in interrupt context. Note: mca.c enables
217 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
218 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
219 * MCA and INIT records are recorded, a timer event will look for any
220 * outstanding events and wake up the user space code.
222 * The buffer passed from mca.c points to the output from ia64_log_get. This is
223 * a persistent buffer but its contents can change between the interrupt and
224 * when user space processes the record. Save the record id to identify
225 * changes. If the buffer is NULL then just update the bitmap.
228 salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
230 struct salinfo_data *data = salinfo_data + type;
231 struct salinfo_data_saved *data_saved;
232 unsigned long flags = 0;
234 int saved_size = ARRAY_SIZE(data->data_saved);
236 BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
239 spin_lock_irqsave(&data_saved_lock, flags);
241 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
242 if (!data_saved->buffer)
245 if (i == saved_size) {
246 if (!data->saved_num) {
247 shift1_data_saved(data, 0);
248 data_saved = data->data_saved + saved_size - 1;
253 data_saved->cpu = smp_processor_id();
254 data_saved->id = ((sal_log_record_header_t *)buffer)->id;
255 data_saved->size = size;
256 data_saved->buffer = buffer;
259 cpu_set(smp_processor_id(), data->cpu_event);
261 salinfo_work_to_do(data);
262 spin_unlock_irqrestore(&data_saved_lock, flags);
266 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
267 #define SALINFO_TIMER_DELAY (60*HZ)
268 static struct timer_list salinfo_timer;
271 salinfo_timeout_check(struct salinfo_data *data)
276 if (!cpus_empty(data->cpu_event)) {
277 spin_lock_irqsave(&data_saved_lock, flags);
278 salinfo_work_to_do(data);
279 spin_unlock_irqrestore(&data_saved_lock, flags);
284 salinfo_timeout (unsigned long arg)
286 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
287 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
288 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
289 add_timer(&salinfo_timer);
293 salinfo_event_open(struct inode *inode, struct file *file)
295 if (!capable(CAP_SYS_ADMIN))
301 salinfo_event_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
303 struct inode *inode = file->f_dentry->d_inode;
304 struct proc_dir_entry *entry = PDE(inode);
305 struct salinfo_data *data = entry->data;
311 if (cpus_empty(data->cpu_event) && down_trylock(&data->mutex)) {
312 if (file->f_flags & O_NONBLOCK)
314 if (down_interruptible(&data->mutex))
319 for (i = 0; i < NR_CPUS; i++) {
320 if (cpu_isset(n, data->cpu_event)) {
321 if (!cpu_online(n)) {
322 cpu_clear(n, data->cpu_event);
335 /* for next read, start checking at next CPU */
336 data->cpu_check = cpu;
337 if (++data->cpu_check == NR_CPUS)
340 snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
345 if (copy_to_user(buffer, cmd, size))
351 static struct file_operations salinfo_event_fops = {
352 .open = salinfo_event_open,
353 .read = salinfo_event_read,
357 salinfo_log_open(struct inode *inode, struct file *file)
359 struct proc_dir_entry *entry = PDE(inode);
360 struct salinfo_data *data = entry->data;
362 if (!capable(CAP_SYS_ADMIN))
365 spin_lock(&data_lock);
367 spin_unlock(&data_lock);
371 spin_unlock(&data_lock);
373 if (data->state == STATE_NO_DATA &&
374 !(data->log_buffer = vmalloc(ia64_sal_get_state_info_size(data->type)))) {
383 salinfo_log_release(struct inode *inode, struct file *file)
385 struct proc_dir_entry *entry = PDE(inode);
386 struct salinfo_data *data = entry->data;
388 if (data->state == STATE_NO_DATA) {
389 vfree(data->log_buffer);
390 vfree(data->oemdata);
391 data->log_buffer = NULL;
392 data->oemdata = NULL;
394 spin_lock(&data_lock);
396 spin_unlock(&data_lock);
401 call_on_cpu(int cpu, void (*fn)(void *), void *arg)
403 cpumask_t save_cpus_allowed = current->cpus_allowed;
404 cpumask_t new_cpus_allowed = cpumask_of_cpu(cpu);
405 set_cpus_allowed(current, new_cpus_allowed);
407 set_cpus_allowed(current, save_cpus_allowed);
411 salinfo_log_read_cpu(void *context)
413 struct salinfo_data *data = context;
414 sal_log_record_header_t *rh;
415 data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
416 rh = (sal_log_record_header_t *)(data->log_buffer);
417 /* Clear corrected errors as they are read from SAL */
418 if (rh->severity == sal_log_severity_corrected)
419 ia64_sal_clear_state_info(data->type);
423 salinfo_log_new_read(int cpu, struct salinfo_data *data)
425 struct salinfo_data_saved *data_saved;
428 int saved_size = ARRAY_SIZE(data->data_saved);
431 spin_lock_irqsave(&data_saved_lock, flags);
433 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
434 if (data_saved->buffer && data_saved->cpu == cpu) {
435 sal_log_record_header_t *rh = (sal_log_record_header_t *)(data_saved->buffer);
436 data->log_size = data_saved->size;
437 memcpy(data->log_buffer, rh, data->log_size);
438 barrier(); /* id check must not be moved */
439 if (rh->id == data_saved->id) {
440 data->saved_num = i+1;
443 /* saved record changed by mca.c since interrupt, discard it */
444 shift1_data_saved(data, i);
448 spin_unlock_irqrestore(&data_saved_lock, flags);
450 if (!data->saved_num)
451 call_on_cpu(cpu, salinfo_log_read_cpu, data);
452 if (!data->log_size) {
453 data->state = STATE_NO_DATA;
454 cpu_clear(cpu, data->cpu_event);
456 data->state = STATE_LOG_RECORD;
461 salinfo_log_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
463 struct inode *inode = file->f_dentry->d_inode;
464 struct proc_dir_entry *entry = PDE(inode);
465 struct salinfo_data *data = entry->data;
469 if (data->state == STATE_LOG_RECORD) {
470 buf = data->log_buffer;
471 bufsize = data->log_size;
472 } else if (data->state == STATE_OEMDATA) {
474 bufsize = data->oemdata_size;
479 return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
483 salinfo_log_clear_cpu(void *context)
485 struct salinfo_data *data = context;
486 ia64_sal_clear_state_info(data->type);
490 salinfo_log_clear(struct salinfo_data *data, int cpu)
492 sal_log_record_header_t *rh;
494 spin_lock_irqsave(&data_saved_lock, flags);
495 data->state = STATE_NO_DATA;
496 if (!cpu_isset(cpu, data->cpu_event)) {
497 spin_unlock_irqrestore(&data_saved_lock, flags);
500 cpu_clear(cpu, data->cpu_event);
501 if (data->saved_num) {
502 shift1_data_saved(data, data->saved_num - 1);
505 spin_unlock_irqrestore(&data_saved_lock, flags);
506 rh = (sal_log_record_header_t *)(data->log_buffer);
507 /* Corrected errors have already been cleared from SAL */
508 if (rh->severity != sal_log_severity_corrected)
509 call_on_cpu(cpu, salinfo_log_clear_cpu, data);
510 /* clearing a record may make a new record visible */
511 salinfo_log_new_read(cpu, data);
512 if (data->state == STATE_LOG_RECORD) {
513 spin_lock_irqsave(&data_saved_lock, flags);
514 cpu_set(cpu, data->cpu_event);
515 salinfo_work_to_do(data);
516 spin_unlock_irqrestore(&data_saved_lock, flags);
522 salinfo_log_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
524 struct inode *inode = file->f_dentry->d_inode;
525 struct proc_dir_entry *entry = PDE(inode);
526 struct salinfo_data *data = entry->data;
535 if (copy_from_user(cmd, buffer, size))
538 if (sscanf(cmd, "read %d", &cpu) == 1) {
539 salinfo_log_new_read(cpu, data);
540 } else if (sscanf(cmd, "clear %d", &cpu) == 1) {
542 if ((ret = salinfo_log_clear(data, cpu)))
544 } else if (sscanf(cmd, "oemdata %d %d", &cpu, &offset) == 2) {
545 if (data->state != STATE_LOG_RECORD && data->state != STATE_OEMDATA)
547 if (offset > data->log_size - sizeof(efi_guid_t))
549 data->state = STATE_OEMDATA;
550 if (salinfo_platform_oemdata) {
551 struct salinfo_platform_oemdata_parms parms = {
552 .efi_guid = data->log_buffer + offset,
553 .oemdata = &data->oemdata,
554 .oemdata_size = &data->oemdata_size
556 call_on_cpu(cpu, salinfo_platform_oemdata_cpu, &parms);
560 data->oemdata_size = 0;
567 static struct file_operations salinfo_data_fops = {
568 .open = salinfo_log_open,
569 .release = salinfo_log_release,
570 .read = salinfo_log_read,
571 .write = salinfo_log_write,
574 #ifdef CONFIG_HOTPLUG_CPU
576 salinfo_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
578 unsigned int i, cpu = (unsigned long)hcpu;
580 struct salinfo_data *data;
583 spin_lock_irqsave(&data_saved_lock, flags);
584 for (i = 0, data = salinfo_data;
585 i < ARRAY_SIZE(salinfo_data);
587 cpu_set(cpu, data->cpu_event);
588 salinfo_work_to_do(data);
590 spin_unlock_irqrestore(&data_saved_lock, flags);
593 spin_lock_irqsave(&data_saved_lock, flags);
594 for (i = 0, data = salinfo_data;
595 i < ARRAY_SIZE(salinfo_data);
597 struct salinfo_data_saved *data_saved;
599 for (j = ARRAY_SIZE(data->data_saved) - 1, data_saved = data->data_saved + j;
602 if (data_saved->buffer && data_saved->cpu == cpu) {
603 shift1_data_saved(data, j);
606 cpu_clear(cpu, data->cpu_event);
608 spin_unlock_irqrestore(&data_saved_lock, flags);
614 static struct notifier_block salinfo_cpu_notifier =
616 .notifier_call = salinfo_cpu_callback,
619 #endif /* CONFIG_HOTPLUG_CPU */
624 struct proc_dir_entry *salinfo_dir; /* /proc/sal dir entry */
625 struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
626 struct proc_dir_entry *dir, *entry;
627 struct salinfo_data *data;
630 salinfo_dir = proc_mkdir("sal", NULL);
634 for (i=0; i < NR_SALINFO_ENTRIES; i++) {
635 /* pass the feature bit in question as misc data */
636 *sdir++ = create_proc_read_entry (salinfo_entries[i].name, 0, salinfo_dir,
637 salinfo_read, (void *)salinfo_entries[i].feature);
640 for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
641 data = salinfo_data + i;
643 init_MUTEX(&data->mutex);
644 dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
648 entry = create_proc_entry("event", S_IRUSR, dir);
652 entry->proc_fops = &salinfo_event_fops;
655 entry = create_proc_entry("data", S_IRUSR | S_IWUSR, dir);
659 entry->proc_fops = &salinfo_data_fops;
662 /* we missed any events before now */
663 for_each_online_cpu(j)
664 cpu_set(j, data->cpu_event);
669 *sdir++ = salinfo_dir;
671 init_timer(&salinfo_timer);
672 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
673 salinfo_timer.function = &salinfo_timeout;
674 add_timer(&salinfo_timer);
676 register_hotcpu_notifier(&salinfo_cpu_notifier);
682 * 'data' contains an integer that corresponds to the feature we're
686 salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
690 len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
692 if (len <= off+count) *eof = 1;
697 if (len>count) len = count;
703 module_init(salinfo_init);