xref: /openbmc/linux/arch/ia64/kernel/mca.c (revision 9cb8f069deeed708bf19486d5893e297dc467ae0)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * File:	mca.c
41da177e4SLinus Torvalds  * Purpose:	Generic MCA handling layer
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 2003 Hewlett-Packard Co
71da177e4SLinus Torvalds  *	David Mosberger-Tang <davidm@hpl.hp.com>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Copyright (C) 2002 Dell Inc.
10fe77efb8SHidetoshi Seto  * Copyright (C) Matt Domsch <Matt_Domsch@dell.com>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (C) 2002 Intel
13fe77efb8SHidetoshi Seto  * Copyright (C) Jenna Hall <jenna.s.hall@intel.com>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Copyright (C) 2001 Intel
16fe77efb8SHidetoshi Seto  * Copyright (C) Fred Lewis <frederick.v.lewis@intel.com>
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Copyright (C) 2000 Intel
19fe77efb8SHidetoshi Seto  * Copyright (C) Chuck Fleckenstein <cfleck@co.intel.com>
201da177e4SLinus Torvalds  *
21785285fcSRuss Anderson  * Copyright (C) 1999, 2004-2008 Silicon Graphics, Inc.
22fe77efb8SHidetoshi Seto  * Copyright (C) Vijay Chander <vijay@engr.sgi.com>
231da177e4SLinus Torvalds  *
24fe77efb8SHidetoshi Seto  * Copyright (C) 2006 FUJITSU LIMITED
25fe77efb8SHidetoshi Seto  * Copyright (C) Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
261da177e4SLinus Torvalds  *
27fe77efb8SHidetoshi Seto  * 2000-03-29 Chuck Fleckenstein <cfleck@co.intel.com>
28fe77efb8SHidetoshi Seto  *	      Fixed PAL/SAL update issues, began MCA bug fixes, logging issues,
291da177e4SLinus Torvalds  *	      added min save state dump, added INIT handler.
301da177e4SLinus Torvalds  *
31fe77efb8SHidetoshi Seto  * 2001-01-03 Fred Lewis <frederick.v.lewis@intel.com>
32fe77efb8SHidetoshi Seto  *	      Added setup of CMCI and CPEI IRQs, logging of corrected platform
33fe77efb8SHidetoshi Seto  *	      errors, completed code for logging of corrected & uncorrected
34fe77efb8SHidetoshi Seto  *	      machine check errors, and updated for conformance with Nov. 2000
35fe77efb8SHidetoshi Seto  *	      revision of the SAL 3.0 spec.
36fe77efb8SHidetoshi Seto  *
37fe77efb8SHidetoshi Seto  * 2002-01-04 Jenna Hall <jenna.s.hall@intel.com>
38fe77efb8SHidetoshi Seto  *	      Aligned MCA stack to 16 bytes, added platform vs. CPU error flag,
39fe77efb8SHidetoshi Seto  *	      set SAL default return values, changed error record structure to
40fe77efb8SHidetoshi Seto  *	      linked list, added init call to sal_get_state_info_size().
41fe77efb8SHidetoshi Seto  *
42fe77efb8SHidetoshi Seto  * 2002-03-25 Matt Domsch <Matt_Domsch@dell.com>
43fe77efb8SHidetoshi Seto  *	      GUID cleanups.
44fe77efb8SHidetoshi Seto  *
45fe77efb8SHidetoshi Seto  * 2003-04-15 David Mosberger-Tang <davidm@hpl.hp.com>
46fe77efb8SHidetoshi Seto  *	      Added INIT backtrace support.
47fe77efb8SHidetoshi Seto  *
481da177e4SLinus Torvalds  * 2003-12-08 Keith Owens <kaos@sgi.com>
49fe77efb8SHidetoshi Seto  *	      smp_call_function() must not be called from interrupt context
50fe77efb8SHidetoshi Seto  *	      (can deadlock on tasklist_lock).
51fe77efb8SHidetoshi Seto  *	      Use keventd to call smp_call_function().
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * 2004-02-01 Keith Owens <kaos@sgi.com>
541da177e4SLinus Torvalds  *	      Avoid deadlock when using printk() for MCA and INIT records.
55fe77efb8SHidetoshi Seto  *	      Delete all record printing code, moved to salinfo_decode in user
56fe77efb8SHidetoshi Seto  *	      space.  Mark variables and functions static where possible.
57fe77efb8SHidetoshi Seto  *	      Delete dead variables and functions.  Reorder to remove the need
58fe77efb8SHidetoshi Seto  *	      for forward declarations and to consolidate related code.
597f613c7dSKeith Owens  *
607f613c7dSKeith Owens  * 2005-08-12 Keith Owens <kaos@sgi.com>
61fe77efb8SHidetoshi Seto  *	      Convert MCA/INIT handlers to use per event stacks and SAL/OS
62fe77efb8SHidetoshi Seto  *	      state.
639138d581SKeith Owens  *
649138d581SKeith Owens  * 2005-10-07 Keith Owens <kaos@sgi.com>
659138d581SKeith Owens  *	      Add notify_die() hooks.
6643ed3bafSHidetoshi Seto  *
6743ed3bafSHidetoshi Seto  * 2006-09-15 Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
6843ed3bafSHidetoshi Seto  *	      Add printing support for MCA/INIT.
691612b18cSRuss Anderson  *
701612b18cSRuss Anderson  * 2007-04-27 Russ Anderson <rja@sgi.com>
711612b18cSRuss Anderson  *	      Support multiple cpus going through OS_MCA in the same event.
721da177e4SLinus Torvalds  */
735cf1f7ceSS.Caglar Onur #include <linux/jiffies.h>
741da177e4SLinus Torvalds #include <linux/types.h>
751da177e4SLinus Torvalds #include <linux/init.h>
763f07c014SIngo Molnar #include <linux/sched/signal.h>
77b17b0153SIngo Molnar #include <linux/sched/debug.h>
7829930025SIngo Molnar #include <linux/sched/task.h>
791da177e4SLinus Torvalds #include <linux/interrupt.h>
801da177e4SLinus Torvalds #include <linux/irq.h>
8157c8a661SMike Rapoport #include <linux/memblock.h>
821da177e4SLinus Torvalds #include <linux/acpi.h>
831da177e4SLinus Torvalds #include <linux/timer.h>
841da177e4SLinus Torvalds #include <linux/module.h>
851da177e4SLinus Torvalds #include <linux/kernel.h>
861da177e4SLinus Torvalds #include <linux/smp.h>
871da177e4SLinus Torvalds #include <linux/workqueue.h>
884668f0cdSAkinobu Mita #include <linux/cpumask.h>
891eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
90ed5d4026SHidetoshi Seto #include <linux/cpu.h>
915a0e3ad6STejun Heo #include <linux/gfp.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include <asm/delay.h>
941da177e4SLinus Torvalds #include <asm/meminit.h>
951da177e4SLinus Torvalds #include <asm/page.h>
961da177e4SLinus Torvalds #include <asm/ptrace.h>
971da177e4SLinus Torvalds #include <asm/sal.h>
981da177e4SLinus Torvalds #include <asm/mca.h>
99a7956113SZou Nan hai #include <asm/kexec.h>
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #include <asm/irq.h>
1021da177e4SLinus Torvalds #include <asm/hw_irq.h>
10396651896SXiantao Zhang #include <asm/tlb.h>
1041da177e4SLinus Torvalds 
105d2a28ad9SRuss Anderson #include "mca_drv.h"
1067f613c7dSKeith Owens #include "entry.h"
10790341cd8Safzal mohammed #include "irq.h"
1087f613c7dSKeith Owens 
1091da177e4SLinus Torvalds #if defined(IA64_MCA_DEBUG_INFO)
1101da177e4SLinus Torvalds # define IA64_MCA_DEBUG(fmt...)	printk(fmt)
1111da177e4SLinus Torvalds #else
1121da177e4SLinus Torvalds # define IA64_MCA_DEBUG(fmt...)
1131da177e4SLinus Torvalds #endif
1141da177e4SLinus Torvalds 
1154fa2f0e6SHidetoshi Seto #define NOTIFY_INIT(event, regs, arg, spin)				\
1164fa2f0e6SHidetoshi Seto do {									\
1174fa2f0e6SHidetoshi Seto 	if ((notify_die((event), "INIT", (regs), (arg), 0, 0)		\
1184fa2f0e6SHidetoshi Seto 			== NOTIFY_STOP) && ((spin) == 1))		\
1194fa2f0e6SHidetoshi Seto 		ia64_mca_spin(__func__);				\
1204fa2f0e6SHidetoshi Seto } while (0)
1214fa2f0e6SHidetoshi Seto 
1224fa2f0e6SHidetoshi Seto #define NOTIFY_MCA(event, regs, arg, spin)				\
1234fa2f0e6SHidetoshi Seto do {									\
1244fa2f0e6SHidetoshi Seto 	if ((notify_die((event), "MCA", (regs), (arg), 0, 0)		\
1254fa2f0e6SHidetoshi Seto 			== NOTIFY_STOP) && ((spin) == 1))		\
1264fa2f0e6SHidetoshi Seto 		ia64_mca_spin(__func__);				\
1274fa2f0e6SHidetoshi Seto } while (0)
1284fa2f0e6SHidetoshi Seto 
1291da177e4SLinus Torvalds /* Used by mca_asm.S */
1301da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_data); /* == __per_cpu_mca[smp_processor_id()] */
1311da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_per_cpu_pte); /* PTE to map per-CPU area */
1321da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_pal_pte);	    /* PTE to map PAL code */
1331da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_pal_base);    /* vaddr PAL code granule */
13496651896SXiantao Zhang DEFINE_PER_CPU(u64, ia64_mca_tr_reload);   /* Flag for TR reload */
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds unsigned long __per_cpu_mca[NR_CPUS];
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* In mca_asm.S */
1397f613c7dSKeith Owens extern void			ia64_os_init_dispatch_monarch (void);
1407f613c7dSKeith Owens extern void			ia64_os_init_dispatch_slave (void);
1417f613c7dSKeith Owens 
1427f613c7dSKeith Owens static int monarch_cpu = -1;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static ia64_mc_info_t		ia64_mc_info;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds #define MAX_CPE_POLL_INTERVAL (15*60*HZ) /* 15 minutes */
1471da177e4SLinus Torvalds #define MIN_CPE_POLL_INTERVAL (2*60*HZ)  /* 2 minutes */
1481da177e4SLinus Torvalds #define CMC_POLL_INTERVAL     (1*60*HZ)  /* 1 minute */
1491da177e4SLinus Torvalds #define CPE_HISTORY_LENGTH    5
1501da177e4SLinus Torvalds #define CMC_HISTORY_LENGTH    5
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static struct timer_list cpe_poll_timer;
1531da177e4SLinus Torvalds static struct timer_list cmc_poll_timer;
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  * This variable tells whether we are currently in polling mode.
1561da177e4SLinus Torvalds  * Start with this in the wrong state so we won't play w/ timers
1571da177e4SLinus Torvalds  * before the system is ready.
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds static int cmc_polling_enabled = 1;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds  * Clearing this variable prevents CPE polling from getting activated
1631da177e4SLinus Torvalds  * in mca_late_init.  Use it if your system doesn't provide a CPEI,
1641da177e4SLinus Torvalds  * but encounters problems retrieving CPE logs.  This should only be
1651da177e4SLinus Torvalds  * necessary for debugging.
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds static int cpe_poll_enabled = 1;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds extern void salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe);
1701da177e4SLinus Torvalds 
1710881fc8dSChen, Kenneth W static int mca_init __initdata;
1721da177e4SLinus Torvalds 
17343ed3bafSHidetoshi Seto /*
17443ed3bafSHidetoshi Seto  * limited & delayed printing support for MCA/INIT handler
17543ed3bafSHidetoshi Seto  */
17643ed3bafSHidetoshi Seto 
17743ed3bafSHidetoshi Seto #define mprintk(fmt...) ia64_mca_printk(fmt)
17843ed3bafSHidetoshi Seto 
17943ed3bafSHidetoshi Seto #define MLOGBUF_SIZE (512+256*NR_CPUS)
18043ed3bafSHidetoshi Seto #define MLOGBUF_MSGMAX 256
18143ed3bafSHidetoshi Seto static char mlogbuf[MLOGBUF_SIZE];
18243ed3bafSHidetoshi Seto static DEFINE_SPINLOCK(mlogbuf_wlock);	/* mca context only */
18343ed3bafSHidetoshi Seto static DEFINE_SPINLOCK(mlogbuf_rlock);	/* normal context only */
18443ed3bafSHidetoshi Seto static unsigned long mlogbuf_start;
18543ed3bafSHidetoshi Seto static unsigned long mlogbuf_end;
18643ed3bafSHidetoshi Seto static unsigned int mlogbuf_finished = 0;
18743ed3bafSHidetoshi Seto static unsigned long mlogbuf_timestamp = 0;
18843ed3bafSHidetoshi Seto 
18943ed3bafSHidetoshi Seto static int loglevel_save = -1;
19043ed3bafSHidetoshi Seto #define BREAK_LOGLEVEL(__console_loglevel)		\
19143ed3bafSHidetoshi Seto 	oops_in_progress = 1;				\
19243ed3bafSHidetoshi Seto 	if (loglevel_save < 0)				\
19343ed3bafSHidetoshi Seto 		loglevel_save = __console_loglevel;	\
19443ed3bafSHidetoshi Seto 	__console_loglevel = 15;
19543ed3bafSHidetoshi Seto 
19643ed3bafSHidetoshi Seto #define RESTORE_LOGLEVEL(__console_loglevel)		\
19743ed3bafSHidetoshi Seto 	if (loglevel_save >= 0) {			\
19843ed3bafSHidetoshi Seto 		__console_loglevel = loglevel_save;	\
19943ed3bafSHidetoshi Seto 		loglevel_save = -1;			\
20043ed3bafSHidetoshi Seto 	}						\
20143ed3bafSHidetoshi Seto 	mlogbuf_finished = 0;				\
20243ed3bafSHidetoshi Seto 	oops_in_progress = 0;
20343ed3bafSHidetoshi Seto 
20443ed3bafSHidetoshi Seto /*
20543ed3bafSHidetoshi Seto  * Push messages into buffer, print them later if not urgent.
20643ed3bafSHidetoshi Seto  */
20743ed3bafSHidetoshi Seto void ia64_mca_printk(const char *fmt, ...)
20843ed3bafSHidetoshi Seto {
20943ed3bafSHidetoshi Seto 	va_list args;
21043ed3bafSHidetoshi Seto 	int printed_len;
21143ed3bafSHidetoshi Seto 	char temp_buf[MLOGBUF_MSGMAX];
21243ed3bafSHidetoshi Seto 	char *p;
21343ed3bafSHidetoshi Seto 
21443ed3bafSHidetoshi Seto 	va_start(args, fmt);
21543ed3bafSHidetoshi Seto 	printed_len = vscnprintf(temp_buf, sizeof(temp_buf), fmt, args);
21643ed3bafSHidetoshi Seto 	va_end(args);
21743ed3bafSHidetoshi Seto 
21843ed3bafSHidetoshi Seto 	/* Copy the output into mlogbuf */
21943ed3bafSHidetoshi Seto 	if (oops_in_progress) {
22043ed3bafSHidetoshi Seto 		/* mlogbuf was abandoned, use printk directly instead. */
2214c332c32SKees Cook 		printk("%s", temp_buf);
22243ed3bafSHidetoshi Seto 	} else {
22343ed3bafSHidetoshi Seto 		spin_lock(&mlogbuf_wlock);
22443ed3bafSHidetoshi Seto 		for (p = temp_buf; *p; p++) {
22543ed3bafSHidetoshi Seto 			unsigned long next = (mlogbuf_end + 1) % MLOGBUF_SIZE;
22643ed3bafSHidetoshi Seto 			if (next != mlogbuf_start) {
22743ed3bafSHidetoshi Seto 				mlogbuf[mlogbuf_end] = *p;
22843ed3bafSHidetoshi Seto 				mlogbuf_end = next;
22943ed3bafSHidetoshi Seto 			} else {
23043ed3bafSHidetoshi Seto 				/* buffer full */
23143ed3bafSHidetoshi Seto 				break;
23243ed3bafSHidetoshi Seto 			}
23343ed3bafSHidetoshi Seto 		}
23443ed3bafSHidetoshi Seto 		mlogbuf[mlogbuf_end] = '\0';
23543ed3bafSHidetoshi Seto 		spin_unlock(&mlogbuf_wlock);
23643ed3bafSHidetoshi Seto 	}
23743ed3bafSHidetoshi Seto }
23843ed3bafSHidetoshi Seto EXPORT_SYMBOL(ia64_mca_printk);
23943ed3bafSHidetoshi Seto 
24043ed3bafSHidetoshi Seto /*
24143ed3bafSHidetoshi Seto  * Print buffered messages.
24243ed3bafSHidetoshi Seto  *  NOTE: call this after returning normal context. (ex. from salinfod)
24343ed3bafSHidetoshi Seto  */
24443ed3bafSHidetoshi Seto void ia64_mlogbuf_dump(void)
24543ed3bafSHidetoshi Seto {
24643ed3bafSHidetoshi Seto 	char temp_buf[MLOGBUF_MSGMAX];
24743ed3bafSHidetoshi Seto 	char *p;
24843ed3bafSHidetoshi Seto 	unsigned long index;
24943ed3bafSHidetoshi Seto 	unsigned long flags;
25043ed3bafSHidetoshi Seto 	unsigned int printed_len;
25143ed3bafSHidetoshi Seto 
25243ed3bafSHidetoshi Seto 	/* Get output from mlogbuf */
25343ed3bafSHidetoshi Seto 	while (mlogbuf_start != mlogbuf_end) {
25443ed3bafSHidetoshi Seto 		temp_buf[0] = '\0';
25543ed3bafSHidetoshi Seto 		p = temp_buf;
25643ed3bafSHidetoshi Seto 		printed_len = 0;
25743ed3bafSHidetoshi Seto 
25843ed3bafSHidetoshi Seto 		spin_lock_irqsave(&mlogbuf_rlock, flags);
25943ed3bafSHidetoshi Seto 
26043ed3bafSHidetoshi Seto 		index = mlogbuf_start;
26143ed3bafSHidetoshi Seto 		while (index != mlogbuf_end) {
26243ed3bafSHidetoshi Seto 			*p = mlogbuf[index];
26343ed3bafSHidetoshi Seto 			index = (index + 1) % MLOGBUF_SIZE;
26443ed3bafSHidetoshi Seto 			if (!*p)
26543ed3bafSHidetoshi Seto 				break;
26643ed3bafSHidetoshi Seto 			p++;
26743ed3bafSHidetoshi Seto 			if (++printed_len >= MLOGBUF_MSGMAX - 1)
26843ed3bafSHidetoshi Seto 				break;
26943ed3bafSHidetoshi Seto 		}
27043ed3bafSHidetoshi Seto 		*p = '\0';
27143ed3bafSHidetoshi Seto 		if (temp_buf[0])
2724c332c32SKees Cook 			printk("%s", temp_buf);
27343ed3bafSHidetoshi Seto 		mlogbuf_start = index;
27443ed3bafSHidetoshi Seto 
27543ed3bafSHidetoshi Seto 		mlogbuf_timestamp = 0;
27643ed3bafSHidetoshi Seto 		spin_unlock_irqrestore(&mlogbuf_rlock, flags);
27743ed3bafSHidetoshi Seto 	}
27843ed3bafSHidetoshi Seto }
27943ed3bafSHidetoshi Seto EXPORT_SYMBOL(ia64_mlogbuf_dump);
28043ed3bafSHidetoshi Seto 
28143ed3bafSHidetoshi Seto /*
28243ed3bafSHidetoshi Seto  * Call this if system is going to down or if immediate flushing messages to
28343ed3bafSHidetoshi Seto  * console is required. (ex. recovery was failed, crash dump is going to be
28443ed3bafSHidetoshi Seto  * invoked, long-wait rendezvous etc.)
28543ed3bafSHidetoshi Seto  *  NOTE: this should be called from monarch.
28643ed3bafSHidetoshi Seto  */
28743ed3bafSHidetoshi Seto static void ia64_mlogbuf_finish(int wait)
28843ed3bafSHidetoshi Seto {
28943ed3bafSHidetoshi Seto 	BREAK_LOGLEVEL(console_loglevel);
29043ed3bafSHidetoshi Seto 
29143ed3bafSHidetoshi Seto 	spin_lock_init(&mlogbuf_rlock);
29243ed3bafSHidetoshi Seto 	ia64_mlogbuf_dump();
29343ed3bafSHidetoshi Seto 	printk(KERN_EMERG "mlogbuf_finish: printing switched to urgent mode, "
29443ed3bafSHidetoshi Seto 		"MCA/INIT might be dodgy or fail.\n");
29543ed3bafSHidetoshi Seto 
29643ed3bafSHidetoshi Seto 	if (!wait)
29743ed3bafSHidetoshi Seto 		return;
29843ed3bafSHidetoshi Seto 
29943ed3bafSHidetoshi Seto 	/* wait for console */
30043ed3bafSHidetoshi Seto 	printk("Delaying for 5 seconds...\n");
30143ed3bafSHidetoshi Seto 	udelay(5*1000000);
30243ed3bafSHidetoshi Seto 
30343ed3bafSHidetoshi Seto 	mlogbuf_finished = 1;
30443ed3bafSHidetoshi Seto }
30543ed3bafSHidetoshi Seto 
30643ed3bafSHidetoshi Seto /*
30743ed3bafSHidetoshi Seto  * Print buffered messages from INIT context.
30843ed3bafSHidetoshi Seto  */
30943ed3bafSHidetoshi Seto static void ia64_mlogbuf_dump_from_init(void)
31043ed3bafSHidetoshi Seto {
31143ed3bafSHidetoshi Seto 	if (mlogbuf_finished)
31243ed3bafSHidetoshi Seto 		return;
31343ed3bafSHidetoshi Seto 
3145cf1f7ceSS.Caglar Onur 	if (mlogbuf_timestamp &&
3155cf1f7ceSS.Caglar Onur 			time_before(jiffies, mlogbuf_timestamp + 30 * HZ)) {
31643ed3bafSHidetoshi Seto 		printk(KERN_ERR "INIT: mlogbuf_dump is interrupted by INIT "
31743ed3bafSHidetoshi Seto 			" and the system seems to be messed up.\n");
31843ed3bafSHidetoshi Seto 		ia64_mlogbuf_finish(0);
31943ed3bafSHidetoshi Seto 		return;
32043ed3bafSHidetoshi Seto 	}
32143ed3bafSHidetoshi Seto 
32243ed3bafSHidetoshi Seto 	if (!spin_trylock(&mlogbuf_rlock)) {
32343ed3bafSHidetoshi Seto 		printk(KERN_ERR "INIT: mlogbuf_dump is interrupted by INIT. "
32443ed3bafSHidetoshi Seto 			"Generated messages other than stack dump will be "
32543ed3bafSHidetoshi Seto 			"buffered to mlogbuf and will be printed later.\n");
32643ed3bafSHidetoshi Seto 		printk(KERN_ERR "INIT: If messages would not printed after "
32743ed3bafSHidetoshi Seto 			"this INIT, wait 30sec and assert INIT again.\n");
32843ed3bafSHidetoshi Seto 		if (!mlogbuf_timestamp)
32943ed3bafSHidetoshi Seto 			mlogbuf_timestamp = jiffies;
33043ed3bafSHidetoshi Seto 		return;
33143ed3bafSHidetoshi Seto 	}
33243ed3bafSHidetoshi Seto 	spin_unlock(&mlogbuf_rlock);
33343ed3bafSHidetoshi Seto 	ia64_mlogbuf_dump();
33443ed3bafSHidetoshi Seto }
3359138d581SKeith Owens 
336d778931dSJoe Perches static inline void
3379138d581SKeith Owens ia64_mca_spin(const char *func)
3389138d581SKeith Owens {
33943ed3bafSHidetoshi Seto 	if (monarch_cpu == smp_processor_id())
34043ed3bafSHidetoshi Seto 		ia64_mlogbuf_finish(0);
34143ed3bafSHidetoshi Seto 	mprintk(KERN_EMERG "%s: spinning here, not returning to SAL\n", func);
3429138d581SKeith Owens 	while (1)
3439138d581SKeith Owens 		cpu_relax();
3449138d581SKeith Owens }
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  * IA64_MCA log support
3471da177e4SLinus Torvalds  */
3481da177e4SLinus Torvalds #define IA64_MAX_LOGS		2	/* Double-buffering for nested MCAs */
3491da177e4SLinus Torvalds #define IA64_MAX_LOG_TYPES      4   /* MCA, INIT, CMC, CPE */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds typedef struct ia64_state_log_s
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	spinlock_t	isl_lock;
3541da177e4SLinus Torvalds 	int		isl_index;
3551da177e4SLinus Torvalds 	unsigned long	isl_count;
3561da177e4SLinus Torvalds 	ia64_err_rec_t  *isl_log[IA64_MAX_LOGS]; /* need space to store header + error log */
3571da177e4SLinus Torvalds } ia64_state_log_t;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static ia64_state_log_t ia64_state_log[IA64_MAX_LOG_TYPES];
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds #define IA64_LOG_LOCK_INIT(it) spin_lock_init(&ia64_state_log[it].isl_lock)
3621da177e4SLinus Torvalds #define IA64_LOG_LOCK(it)      spin_lock_irqsave(&ia64_state_log[it].isl_lock, s)
3631da177e4SLinus Torvalds #define IA64_LOG_UNLOCK(it)    spin_unlock_irqrestore(&ia64_state_log[it].isl_lock,s)
3641da177e4SLinus Torvalds #define IA64_LOG_NEXT_INDEX(it)    ia64_state_log[it].isl_index
3651da177e4SLinus Torvalds #define IA64_LOG_CURR_INDEX(it)    1 - ia64_state_log[it].isl_index
3661da177e4SLinus Torvalds #define IA64_LOG_INDEX_INC(it) \
3671da177e4SLinus Torvalds     {ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index; \
3681da177e4SLinus Torvalds     ia64_state_log[it].isl_count++;}
3691da177e4SLinus Torvalds #define IA64_LOG_INDEX_DEC(it) \
3701da177e4SLinus Torvalds     ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index
3711da177e4SLinus Torvalds #define IA64_LOG_NEXT_BUFFER(it)   (void *)((ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)]))
3721da177e4SLinus Torvalds #define IA64_LOG_CURR_BUFFER(it)   (void *)((ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)]))
3731da177e4SLinus Torvalds #define IA64_LOG_COUNT(it)         ia64_state_log[it].isl_count
3741da177e4SLinus Torvalds 
375d80db5c1SMike Rapoport static inline void ia64_log_allocate(int it, u64 size)
376d80db5c1SMike Rapoport {
377d80db5c1SMike Rapoport 	ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)] =
378d80db5c1SMike Rapoport 		(ia64_err_rec_t *)memblock_alloc(size, SMP_CACHE_BYTES);
379d80db5c1SMike Rapoport 	if (!ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)])
380d80db5c1SMike Rapoport 		panic("%s: Failed to allocate %llu bytes\n", __func__, size);
381d80db5c1SMike Rapoport 
382d80db5c1SMike Rapoport 	ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)] =
383d80db5c1SMike Rapoport 		(ia64_err_rec_t *)memblock_alloc(size, SMP_CACHE_BYTES);
384d80db5c1SMike Rapoport 	if (!ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)])
385d80db5c1SMike Rapoport 		panic("%s: Failed to allocate %llu bytes\n", __func__, size);
386d80db5c1SMike Rapoport }
387d80db5c1SMike Rapoport 
3881da177e4SLinus Torvalds /*
3891da177e4SLinus Torvalds  * ia64_log_init
3901da177e4SLinus Torvalds  *	Reset the OS ia64 log buffer
3911da177e4SLinus Torvalds  * Inputs   :   info_type   (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
3921da177e4SLinus Torvalds  * Outputs	:	None
3931da177e4SLinus Torvalds  */
3940881fc8dSChen, Kenneth W static void __init
3951da177e4SLinus Torvalds ia64_log_init(int sal_info_type)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	u64	max_size = 0;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	IA64_LOG_NEXT_INDEX(sal_info_type) = 0;
4001da177e4SLinus Torvalds 	IA64_LOG_LOCK_INIT(sal_info_type);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	// SAL will tell us the maximum size of any error record of this type
4031da177e4SLinus Torvalds 	max_size = ia64_sal_get_state_info_size(sal_info_type);
4041da177e4SLinus Torvalds 	if (!max_size)
4051da177e4SLinus Torvalds 		/* alloc_bootmem() doesn't like zero-sized allocations! */
4061da177e4SLinus Torvalds 		return;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	// set up OS data structures to hold error info
409d80db5c1SMike Rapoport 	ia64_log_allocate(sal_info_type, max_size);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * ia64_log_get
4141da177e4SLinus Torvalds  *
4151da177e4SLinus Torvalds  *	Get the current MCA log from SAL and copy it into the OS log buffer.
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  *  Inputs  :   info_type   (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
4181da177e4SLinus Torvalds  *              irq_safe    whether you can use printk at this point
4191da177e4SLinus Torvalds  *  Outputs :   size        (total record length)
4201da177e4SLinus Torvalds  *              *buffer     (ptr to error record)
4211da177e4SLinus Torvalds  *
4221da177e4SLinus Torvalds  */
4231da177e4SLinus Torvalds static u64
4241da177e4SLinus Torvalds ia64_log_get(int sal_info_type, u8 **buffer, int irq_safe)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	sal_log_record_header_t     *log_buffer;
4271da177e4SLinus Torvalds 	u64                         total_len = 0;
428c53421b1SAlexey Dobriyan 	unsigned long               s;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	IA64_LOG_LOCK(sal_info_type);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* Get the process state information */
4331da177e4SLinus Torvalds 	log_buffer = IA64_LOG_NEXT_BUFFER(sal_info_type);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	total_len = ia64_sal_get_state_info(sal_info_type, (u64 *)log_buffer);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (total_len) {
4381da177e4SLinus Torvalds 		IA64_LOG_INDEX_INC(sal_info_type);
4391da177e4SLinus Torvalds 		IA64_LOG_UNLOCK(sal_info_type);
4401da177e4SLinus Torvalds 		if (irq_safe) {
441d4ed8084SHarvey Harrison 			IA64_MCA_DEBUG("%s: SAL error record type %d retrieved. Record length = %ld\n",
442d4ed8084SHarvey Harrison 				       __func__, sal_info_type, total_len);
4431da177e4SLinus Torvalds 		}
4441da177e4SLinus Torvalds 		*buffer = (u8 *) log_buffer;
4451da177e4SLinus Torvalds 		return total_len;
4461da177e4SLinus Torvalds 	} else {
4471da177e4SLinus Torvalds 		IA64_LOG_UNLOCK(sal_info_type);
4481da177e4SLinus Torvalds 		return 0;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  *  ia64_mca_log_sal_error_record
4541da177e4SLinus Torvalds  *
4551da177e4SLinus Torvalds  *  This function retrieves a specified error record type from SAL
4561da177e4SLinus Torvalds  *  and wakes up any processes waiting for error records.
4571da177e4SLinus Torvalds  *
4587f613c7dSKeith Owens  *  Inputs  :   sal_info_type   (Type of error record MCA/CMC/CPE)
4597f613c7dSKeith Owens  *              FIXME: remove MCA and irq_safe.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds static void
4621da177e4SLinus Torvalds ia64_mca_log_sal_error_record(int sal_info_type)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	u8 *buffer;
4651da177e4SLinus Torvalds 	sal_log_record_header_t *rh;
4661da177e4SLinus Torvalds 	u64 size;
4677f613c7dSKeith Owens 	int irq_safe = sal_info_type != SAL_INFO_TYPE_MCA;
4681da177e4SLinus Torvalds #ifdef IA64_MCA_DEBUG_INFO
4691da177e4SLinus Torvalds 	static const char * const rec_name[] = { "MCA", "INIT", "CMC", "CPE" };
4701da177e4SLinus Torvalds #endif
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	size = ia64_log_get(sal_info_type, &buffer, irq_safe);
4731da177e4SLinus Torvalds 	if (!size)
4741da177e4SLinus Torvalds 		return;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	salinfo_log_wakeup(sal_info_type, buffer, size, irq_safe);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	if (irq_safe)
4791da177e4SLinus Torvalds 		IA64_MCA_DEBUG("CPU %d: SAL log contains %s error record\n",
4801da177e4SLinus Torvalds 			smp_processor_id(),
4811da177e4SLinus Torvalds 			sal_info_type < ARRAY_SIZE(rec_name) ? rec_name[sal_info_type] : "UNKNOWN");
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	/* Clear logs from corrected errors in case there's no user-level logger */
4841da177e4SLinus Torvalds 	rh = (sal_log_record_header_t *)buffer;
4851da177e4SLinus Torvalds 	if (rh->severity == sal_log_severity_corrected)
4861da177e4SLinus Torvalds 		ia64_sal_clear_state_info(sal_info_type);
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489d2a28ad9SRuss Anderson /*
490d2a28ad9SRuss Anderson  * search_mca_table
491d2a28ad9SRuss Anderson  *  See if the MCA surfaced in an instruction range
492d2a28ad9SRuss Anderson  *  that has been tagged as recoverable.
493d2a28ad9SRuss Anderson  *
494d2a28ad9SRuss Anderson  *  Inputs
495d2a28ad9SRuss Anderson  *	first	First address range to check
496d2a28ad9SRuss Anderson  *	last	Last address range to check
497d2a28ad9SRuss Anderson  *	ip	Instruction pointer, address we are looking for
498d2a28ad9SRuss Anderson  *
499d2a28ad9SRuss Anderson  * Return value:
500d2a28ad9SRuss Anderson  *      1 on Success (in the table)/ 0 on Failure (not in the  table)
501d2a28ad9SRuss Anderson  */
502d2a28ad9SRuss Anderson int
503d2a28ad9SRuss Anderson search_mca_table (const struct mca_table_entry *first,
504d2a28ad9SRuss Anderson                 const struct mca_table_entry *last,
505d2a28ad9SRuss Anderson                 unsigned long ip)
506d2a28ad9SRuss Anderson {
507d2a28ad9SRuss Anderson         const struct mca_table_entry *curr;
508d2a28ad9SRuss Anderson         u64 curr_start, curr_end;
509d2a28ad9SRuss Anderson 
510d2a28ad9SRuss Anderson         curr = first;
511d2a28ad9SRuss Anderson         while (curr <= last) {
512d2a28ad9SRuss Anderson                 curr_start = (u64) &curr->start_addr + curr->start_addr;
513d2a28ad9SRuss Anderson                 curr_end = (u64) &curr->end_addr + curr->end_addr;
514d2a28ad9SRuss Anderson 
515d2a28ad9SRuss Anderson                 if ((ip >= curr_start) && (ip <= curr_end)) {
516d2a28ad9SRuss Anderson                         return 1;
517d2a28ad9SRuss Anderson                 }
518d2a28ad9SRuss Anderson                 curr++;
519d2a28ad9SRuss Anderson         }
520d2a28ad9SRuss Anderson         return 0;
521d2a28ad9SRuss Anderson }
522d2a28ad9SRuss Anderson 
523d2a28ad9SRuss Anderson /* Given an address, look for it in the mca tables. */
524d2a28ad9SRuss Anderson int mca_recover_range(unsigned long addr)
525d2a28ad9SRuss Anderson {
526d2a28ad9SRuss Anderson 	extern struct mca_table_entry __start___mca_table[];
527d2a28ad9SRuss Anderson 	extern struct mca_table_entry __stop___mca_table[];
528d2a28ad9SRuss Anderson 
529d2a28ad9SRuss Anderson 	return search_mca_table(__start___mca_table, __stop___mca_table-1, addr);
530d2a28ad9SRuss Anderson }
531d2a28ad9SRuss Anderson EXPORT_SYMBOL_GPL(mca_recover_range);
532d2a28ad9SRuss Anderson 
53355e59c51SAshok Raj int cpe_vector = -1;
534ff741906SAshok Raj int ia64_cpe_irq = -1;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static irqreturn_t
5377d12e780SDavid Howells ia64_mca_cpe_int_handler (int cpe_irq, void *arg)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	static unsigned long	cpe_history[CPE_HISTORY_LENGTH];
5401da177e4SLinus Torvalds 	static int		index;
5411da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(cpe_history_lock);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
544d4ed8084SHarvey Harrison 		       __func__, cpe_irq, smp_processor_id());
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/* SAL spec states this should run w/ interrupts enabled */
5471da177e4SLinus Torvalds 	local_irq_enable();
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	spin_lock(&cpe_history_lock);
5501da177e4SLinus Torvalds 	if (!cpe_poll_enabled && cpe_vector >= 0) {
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 		int i, count = 1; /* we know 1 happened now */
5531da177e4SLinus Torvalds 		unsigned long now = jiffies;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		for (i = 0; i < CPE_HISTORY_LENGTH; i++) {
5561da177e4SLinus Torvalds 			if (now - cpe_history[i] <= HZ)
5571da177e4SLinus Torvalds 				count++;
5581da177e4SLinus Torvalds 		}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 		IA64_MCA_DEBUG(KERN_INFO "CPE threshold %d/%d\n", count, CPE_HISTORY_LENGTH);
5611da177e4SLinus Torvalds 		if (count >= CPE_HISTORY_LENGTH) {
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 			cpe_poll_enabled = 1;
5641da177e4SLinus Torvalds 			spin_unlock(&cpe_history_lock);
5651da177e4SLinus Torvalds 			disable_irq_nosync(local_vector_to_irq(IA64_CPE_VECTOR));
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 			/*
5681da177e4SLinus Torvalds 			 * Corrected errors will still be corrected, but
5691da177e4SLinus Torvalds 			 * make sure there's a log somewhere that indicates
5701da177e4SLinus Torvalds 			 * something is generating more than we can handle.
5711da177e4SLinus Torvalds 			 */
5721da177e4SLinus Torvalds 			printk(KERN_WARNING "WARNING: Switching to polling CPE handler; error records may be lost\n");
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 			mod_timer(&cpe_poll_timer, jiffies + MIN_CPE_POLL_INTERVAL);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 			/* lock already released, get out now */
577ddb4f0dfSHidetoshi Seto 			goto out;
5781da177e4SLinus Torvalds 		} else {
5791da177e4SLinus Torvalds 			cpe_history[index++] = now;
5801da177e4SLinus Torvalds 			if (index == CPE_HISTORY_LENGTH)
5811da177e4SLinus Torvalds 				index = 0;
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 	spin_unlock(&cpe_history_lock);
585ddb4f0dfSHidetoshi Seto out:
586ddb4f0dfSHidetoshi Seto 	/* Get the CPE error record and log it */
587ddb4f0dfSHidetoshi Seto 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CPE);
588ddb4f0dfSHidetoshi Seto 
589a3967685STony Luck 	local_irq_disable();
590a3967685STony Luck 
5911da177e4SLinus Torvalds 	return IRQ_HANDLED;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /*
5951da177e4SLinus Torvalds  * ia64_mca_register_cpev
5961da177e4SLinus Torvalds  *
5971da177e4SLinus Torvalds  *  Register the corrected platform error vector with SAL.
5981da177e4SLinus Torvalds  *
5991da177e4SLinus Torvalds  *  Inputs
6001da177e4SLinus Torvalds  *      cpev        Corrected Platform Error Vector number
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  *  Outputs
6031da177e4SLinus Torvalds  *      None
6041da177e4SLinus Torvalds  */
6051f3b6045SRuss Anderson void
6061da177e4SLinus Torvalds ia64_mca_register_cpev (int cpev)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	/* Register the CPE interrupt vector with SAL */
6091da177e4SLinus Torvalds 	struct ia64_sal_retval isrv;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_CPE_INT, SAL_MC_PARAM_MECHANISM_INT, cpev, 0, 0);
6121da177e4SLinus Torvalds 	if (isrv.status) {
6131da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register Corrected Platform "
6141da177e4SLinus Torvalds 		       "Error interrupt vector with SAL (status %ld)\n", isrv.status);
6151da177e4SLinus Torvalds 		return;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: corrected platform error "
619d4ed8084SHarvey Harrison 		       "vector %#x registered\n", __func__, cpev);
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /*
6231da177e4SLinus Torvalds  * ia64_mca_cmc_vector_setup
6241da177e4SLinus Torvalds  *
6251da177e4SLinus Torvalds  *  Setup the corrected machine check vector register in the processor.
6261da177e4SLinus Torvalds  *  (The interrupt is masked on boot. ia64_mca_late_init unmask this.)
6271da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
6281da177e4SLinus Torvalds  *
6291da177e4SLinus Torvalds  * Inputs
6301da177e4SLinus Torvalds  *      None
6311da177e4SLinus Torvalds  *
6321da177e4SLinus Torvalds  * Outputs
6331da177e4SLinus Torvalds  *	None
6341da177e4SLinus Torvalds  */
635ccce9bb8SPaul Gortmaker void
6361da177e4SLinus Torvalds ia64_mca_cmc_vector_setup (void)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	cmcv.cmcv_regval	= 0;
6411da177e4SLinus Torvalds 	cmcv.cmcv_mask		= 1;        /* Mask/disable interrupt at first */
6421da177e4SLinus Torvalds 	cmcv.cmcv_vector	= IA64_CMC_VECTOR;
6431da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
6441da177e4SLinus Torvalds 
645d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: CPU %d corrected machine check vector %#x registered.\n",
646d4ed8084SHarvey Harrison 		       __func__, smp_processor_id(), IA64_CMC_VECTOR);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CPU %d CMCV = %#016lx\n",
649d4ed8084SHarvey Harrison 		       __func__, smp_processor_id(), ia64_getreg(_IA64_REG_CR_CMCV));
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /*
6531da177e4SLinus Torvalds  * ia64_mca_cmc_vector_disable
6541da177e4SLinus Torvalds  *
6551da177e4SLinus Torvalds  *  Mask the corrected machine check vector register in the processor.
6561da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
6571da177e4SLinus Torvalds  *
6581da177e4SLinus Torvalds  * Inputs
6591da177e4SLinus Torvalds  *      dummy(unused)
6601da177e4SLinus Torvalds  *
6611da177e4SLinus Torvalds  * Outputs
6621da177e4SLinus Torvalds  *	None
6631da177e4SLinus Torvalds  */
6641da177e4SLinus Torvalds static void
6651da177e4SLinus Torvalds ia64_mca_cmc_vector_disable (void *dummy)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	cmcv.cmcv_mask = 1; /* Mask/disable interrupt */
6721da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
6731da177e4SLinus Torvalds 
674d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: CPU %d corrected machine check vector %#x disabled.\n",
675d4ed8084SHarvey Harrison 		       __func__, smp_processor_id(), cmcv.cmcv_vector);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
6791da177e4SLinus Torvalds  * ia64_mca_cmc_vector_enable
6801da177e4SLinus Torvalds  *
6811da177e4SLinus Torvalds  *  Unmask the corrected machine check vector register in the processor.
6821da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
6831da177e4SLinus Torvalds  *
6841da177e4SLinus Torvalds  * Inputs
6851da177e4SLinus Torvalds  *      dummy(unused)
6861da177e4SLinus Torvalds  *
6871da177e4SLinus Torvalds  * Outputs
6881da177e4SLinus Torvalds  *	None
6891da177e4SLinus Torvalds  */
6901da177e4SLinus Torvalds static void
6911da177e4SLinus Torvalds ia64_mca_cmc_vector_enable (void *dummy)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	cmcv.cmcv_mask = 0; /* Unmask/enable interrupt */
6981da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
6991da177e4SLinus Torvalds 
700d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: CPU %d corrected machine check vector %#x enabled.\n",
701d4ed8084SHarvey Harrison 		       __func__, smp_processor_id(), cmcv.cmcv_vector);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * ia64_mca_cmc_vector_disable_keventd
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  * Called via keventd (smp_call_function() is not safe in interrupt context) to
7081da177e4SLinus Torvalds  * disable the cmc interrupt vector.
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds static void
7116d5aefb8SDavid Howells ia64_mca_cmc_vector_disable_keventd(struct work_struct *unused)
7121da177e4SLinus Torvalds {
71315c8b6c1SJens Axboe 	on_each_cpu(ia64_mca_cmc_vector_disable, NULL, 0);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds /*
7171da177e4SLinus Torvalds  * ia64_mca_cmc_vector_enable_keventd
7181da177e4SLinus Torvalds  *
7191da177e4SLinus Torvalds  * Called via keventd (smp_call_function() is not safe in interrupt context) to
7201da177e4SLinus Torvalds  * enable the cmc interrupt vector.
7211da177e4SLinus Torvalds  */
7221da177e4SLinus Torvalds static void
7236d5aefb8SDavid Howells ia64_mca_cmc_vector_enable_keventd(struct work_struct *unused)
7241da177e4SLinus Torvalds {
72515c8b6c1SJens Axboe 	on_each_cpu(ia64_mca_cmc_vector_enable, NULL, 0);
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /*
7291da177e4SLinus Torvalds  * ia64_mca_wakeup
7301da177e4SLinus Torvalds  *
731e1b1eb01SRuss Anderson  *	Send an inter-cpu interrupt to wake-up a particular cpu.
7321da177e4SLinus Torvalds  *
7331da177e4SLinus Torvalds  *  Inputs  :   cpuid
7341da177e4SLinus Torvalds  *  Outputs :   None
7351da177e4SLinus Torvalds  */
7361da177e4SLinus Torvalds static void
7371da177e4SLinus Torvalds ia64_mca_wakeup(int cpu)
7381da177e4SLinus Torvalds {
73905933aacSChristoph Hellwig 	ia64_send_ipi(cpu, IA64_MCA_WAKEUP_VECTOR, IA64_IPI_DM_INT, 0);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds /*
7431da177e4SLinus Torvalds  * ia64_mca_wakeup_all
7441da177e4SLinus Torvalds  *
745e1b1eb01SRuss Anderson  *	Wakeup all the slave cpus which have rendez'ed previously.
7461da177e4SLinus Torvalds  *
7471da177e4SLinus Torvalds  *  Inputs  :   None
7481da177e4SLinus Torvalds  *  Outputs :   None
7491da177e4SLinus Torvalds  */
7501da177e4SLinus Torvalds static void
7511da177e4SLinus Torvalds ia64_mca_wakeup_all(void)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	int cpu;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* Clear the Rendez checkin flag for all cpus */
756ddf6d0a0Shawkes@sgi.com 	for_each_online_cpu(cpu) {
7571da177e4SLinus Torvalds 		if (ia64_mc_info.imi_rendez_checkin[cpu] == IA64_MCA_RENDEZ_CHECKIN_DONE)
7581da177e4SLinus Torvalds 			ia64_mca_wakeup(cpu);
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  * ia64_mca_rendez_interrupt_handler
7651da177e4SLinus Torvalds  *
7661da177e4SLinus Torvalds  *	This is handler used to put slave processors into spinloop
7671da177e4SLinus Torvalds  *	while the monarch processor does the mca handling and later
768e1b1eb01SRuss Anderson  *	wake each slave up once the monarch is done.  The state
769e1b1eb01SRuss Anderson  *	IA64_MCA_RENDEZ_CHECKIN_DONE indicates the cpu is rendez'ed
770e1b1eb01SRuss Anderson  *	in SAL.  The state IA64_MCA_RENDEZ_CHECKIN_NOTDONE indicates
771e1b1eb01SRuss Anderson  *	the cpu has come out of OS rendezvous.
7721da177e4SLinus Torvalds  *
7731da177e4SLinus Torvalds  *  Inputs  :   None
7741da177e4SLinus Torvalds  *  Outputs :   None
7751da177e4SLinus Torvalds  */
7761da177e4SLinus Torvalds static irqreturn_t
7777d12e780SDavid Howells ia64_mca_rendez_int_handler(int rendez_irq, void *arg)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	unsigned long flags;
7801da177e4SLinus Torvalds 	int cpu = smp_processor_id();
781958b166cSKeith Owens 	struct ia64_mca_notify_die nd =
782958b166cSKeith Owens 		{ .sos = NULL, .monarch_cpu = &monarch_cpu };
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* Mask all interrupts */
7851da177e4SLinus Torvalds 	local_irq_save(flags);
7864fa2f0e6SHidetoshi Seto 
7874fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_RENDZVOUS_ENTER, get_irq_regs(), (long)&nd, 1);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_DONE;
7901da177e4SLinus Torvalds 	/* Register with the SAL monarch that the slave has
7911da177e4SLinus Torvalds 	 * reached SAL
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 	ia64_sal_mc_rendez();
7941da177e4SLinus Torvalds 
7954fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_RENDZVOUS_PROCESS, get_irq_regs(), (long)&nd, 1);
7969138d581SKeith Owens 
7977f613c7dSKeith Owens 	/* Wait for the monarch cpu to exit. */
7987f613c7dSKeith Owens 	while (monarch_cpu != -1)
7997f613c7dSKeith Owens 	       cpu_relax();	/* spin until monarch leaves */
8001da177e4SLinus Torvalds 
8014fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_RENDZVOUS_LEAVE, get_irq_regs(), (long)&nd, 1);
8029138d581SKeith Owens 
803e1b1eb01SRuss Anderson 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
8041da177e4SLinus Torvalds 	/* Enable all interrupts */
8051da177e4SLinus Torvalds 	local_irq_restore(flags);
8061da177e4SLinus Torvalds 	return IRQ_HANDLED;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds /*
8101da177e4SLinus Torvalds  * ia64_mca_wakeup_int_handler
8111da177e4SLinus Torvalds  *
8121da177e4SLinus Torvalds  *	The interrupt handler for processing the inter-cpu interrupt to the
8131da177e4SLinus Torvalds  *	slave cpu which was spinning in the rendez loop.
8141da177e4SLinus Torvalds  *	Since this spinning is done by turning off the interrupts and
8151da177e4SLinus Torvalds  *	polling on the wakeup-interrupt bit in the IRR, there is
8161da177e4SLinus Torvalds  *	nothing useful to be done in the handler.
8171da177e4SLinus Torvalds  *
8181da177e4SLinus Torvalds  *  Inputs  :   wakeup_irq  (Wakeup-interrupt bit)
8191da177e4SLinus Torvalds  *	arg		(Interrupt handler specific argument)
8201da177e4SLinus Torvalds  *  Outputs :   None
8211da177e4SLinus Torvalds  *
8221da177e4SLinus Torvalds  */
8231da177e4SLinus Torvalds static irqreturn_t
8247d12e780SDavid Howells ia64_mca_wakeup_int_handler(int wakeup_irq, void *arg)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	return IRQ_HANDLED;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds /* Function pointer for extra MCA recovery */
8301da177e4SLinus Torvalds int (*ia64_mca_ucmc_extension)
8317f613c7dSKeith Owens 	(void*,struct ia64_sal_os_state*)
8321da177e4SLinus Torvalds 	= NULL;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds int
8357f613c7dSKeith Owens ia64_reg_MCA_extension(int (*fn)(void *, struct ia64_sal_os_state *))
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	if (ia64_mca_ucmc_extension)
8381da177e4SLinus Torvalds 		return 1;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	ia64_mca_ucmc_extension = fn;
8411da177e4SLinus Torvalds 	return 0;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds void
8451da177e4SLinus Torvalds ia64_unreg_MCA_extension(void)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	if (ia64_mca_ucmc_extension)
8481da177e4SLinus Torvalds 		ia64_mca_ucmc_extension = NULL;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds EXPORT_SYMBOL(ia64_reg_MCA_extension);
8521da177e4SLinus Torvalds EXPORT_SYMBOL(ia64_unreg_MCA_extension);
8531da177e4SLinus Torvalds 
8547f613c7dSKeith Owens 
8557f613c7dSKeith Owens static inline void
856e088a4adSMatthew Wilcox copy_reg(const u64 *fr, u64 fnat, unsigned long *tr, unsigned long *tnat)
8577f613c7dSKeith Owens {
8587f613c7dSKeith Owens 	u64 fslot, tslot, nat;
8597f613c7dSKeith Owens 	*tr = *fr;
8607f613c7dSKeith Owens 	fslot = ((unsigned long)fr >> 3) & 63;
8617f613c7dSKeith Owens 	tslot = ((unsigned long)tr >> 3) & 63;
8627f613c7dSKeith Owens 	*tnat &= ~(1UL << tslot);
8637f613c7dSKeith Owens 	nat = (fnat >> fslot) & 1;
8647f613c7dSKeith Owens 	*tnat |= (nat << tslot);
8657f613c7dSKeith Owens }
8667f613c7dSKeith Owens 
867e9ac054dSKeith Owens /* Change the comm field on the MCA/INT task to include the pid that
868e9ac054dSKeith Owens  * was interrupted, it makes for easier debugging.  If that pid was 0
869e9ac054dSKeith Owens  * (swapper or nested MCA/INIT) then use the start of the previous comm
870e9ac054dSKeith Owens  * field suffixed with its cpu.
871e9ac054dSKeith Owens  */
872e9ac054dSKeith Owens 
873e9ac054dSKeith Owens static void
87436c8b586SIngo Molnar ia64_mca_modify_comm(const struct task_struct *previous_current)
875e9ac054dSKeith Owens {
876e9ac054dSKeith Owens 	char *p, comm[sizeof(current->comm)];
877e9ac054dSKeith Owens 	if (previous_current->pid)
878e9ac054dSKeith Owens 		snprintf(comm, sizeof(comm), "%s %d",
879e9ac054dSKeith Owens 			current->comm, previous_current->pid);
880e9ac054dSKeith Owens 	else {
881e9ac054dSKeith Owens 		int l;
882e9ac054dSKeith Owens 		if ((p = strchr(previous_current->comm, ' ')))
883e9ac054dSKeith Owens 			l = p - previous_current->comm;
884e9ac054dSKeith Owens 		else
885e9ac054dSKeith Owens 			l = strlen(previous_current->comm);
886e9ac054dSKeith Owens 		snprintf(comm, sizeof(comm), "%s %*s %d",
887e9ac054dSKeith Owens 			current->comm, l, previous_current->comm,
888e9ac054dSKeith Owens 			task_thread_info(previous_current)->cpu);
889e9ac054dSKeith Owens 	}
890e9ac054dSKeith Owens 	memcpy(current->comm, comm, sizeof(current->comm));
891e9ac054dSKeith Owens }
892e9ac054dSKeith Owens 
89329e4e025STakao Indoh static void
8949ee27c76STakao Indoh finish_pt_regs(struct pt_regs *regs, struct ia64_sal_os_state *sos,
89529e4e025STakao Indoh 		unsigned long *nat)
89629e4e025STakao Indoh {
8979ee27c76STakao Indoh 	const pal_min_state_area_t *ms = sos->pal_min_state;
89829e4e025STakao Indoh 	const u64 *bank;
89929e4e025STakao Indoh 
90029e4e025STakao Indoh 	/* If ipsr.ic then use pmsa_{iip,ipsr,ifs}, else use
90129e4e025STakao Indoh 	 * pmsa_{xip,xpsr,xfs}
90229e4e025STakao Indoh 	 */
90329e4e025STakao Indoh 	if (ia64_psr(regs)->ic) {
90429e4e025STakao Indoh 		regs->cr_iip = ms->pmsa_iip;
90529e4e025STakao Indoh 		regs->cr_ipsr = ms->pmsa_ipsr;
90629e4e025STakao Indoh 		regs->cr_ifs = ms->pmsa_ifs;
90729e4e025STakao Indoh 	} else {
90829e4e025STakao Indoh 		regs->cr_iip = ms->pmsa_xip;
90929e4e025STakao Indoh 		regs->cr_ipsr = ms->pmsa_xpsr;
91029e4e025STakao Indoh 		regs->cr_ifs = ms->pmsa_xfs;
9119ee27c76STakao Indoh 
9129ee27c76STakao Indoh 		sos->iip = ms->pmsa_iip;
9139ee27c76STakao Indoh 		sos->ipsr = ms->pmsa_ipsr;
9149ee27c76STakao Indoh 		sos->ifs = ms->pmsa_ifs;
91529e4e025STakao Indoh 	}
91629e4e025STakao Indoh 	regs->pr = ms->pmsa_pr;
91729e4e025STakao Indoh 	regs->b0 = ms->pmsa_br0;
91829e4e025STakao Indoh 	regs->ar_rsc = ms->pmsa_rsc;
91929e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[1-1], ms->pmsa_nat_bits, &regs->r1, nat);
92029e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[2-1], ms->pmsa_nat_bits, &regs->r2, nat);
92129e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[3-1], ms->pmsa_nat_bits, &regs->r3, nat);
92229e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[8-1], ms->pmsa_nat_bits, &regs->r8, nat);
92329e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[9-1], ms->pmsa_nat_bits, &regs->r9, nat);
92429e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[10-1], ms->pmsa_nat_bits, &regs->r10, nat);
92529e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[11-1], ms->pmsa_nat_bits, &regs->r11, nat);
92629e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[12-1], ms->pmsa_nat_bits, &regs->r12, nat);
92729e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[13-1], ms->pmsa_nat_bits, &regs->r13, nat);
92829e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[14-1], ms->pmsa_nat_bits, &regs->r14, nat);
92929e4e025STakao Indoh 	copy_reg(&ms->pmsa_gr[15-1], ms->pmsa_nat_bits, &regs->r15, nat);
93029e4e025STakao Indoh 	if (ia64_psr(regs)->bn)
93129e4e025STakao Indoh 		bank = ms->pmsa_bank1_gr;
93229e4e025STakao Indoh 	else
93329e4e025STakao Indoh 		bank = ms->pmsa_bank0_gr;
93429e4e025STakao Indoh 	copy_reg(&bank[16-16], ms->pmsa_nat_bits, &regs->r16, nat);
93529e4e025STakao Indoh 	copy_reg(&bank[17-16], ms->pmsa_nat_bits, &regs->r17, nat);
93629e4e025STakao Indoh 	copy_reg(&bank[18-16], ms->pmsa_nat_bits, &regs->r18, nat);
93729e4e025STakao Indoh 	copy_reg(&bank[19-16], ms->pmsa_nat_bits, &regs->r19, nat);
93829e4e025STakao Indoh 	copy_reg(&bank[20-16], ms->pmsa_nat_bits, &regs->r20, nat);
93929e4e025STakao Indoh 	copy_reg(&bank[21-16], ms->pmsa_nat_bits, &regs->r21, nat);
94029e4e025STakao Indoh 	copy_reg(&bank[22-16], ms->pmsa_nat_bits, &regs->r22, nat);
94129e4e025STakao Indoh 	copy_reg(&bank[23-16], ms->pmsa_nat_bits, &regs->r23, nat);
94229e4e025STakao Indoh 	copy_reg(&bank[24-16], ms->pmsa_nat_bits, &regs->r24, nat);
94329e4e025STakao Indoh 	copy_reg(&bank[25-16], ms->pmsa_nat_bits, &regs->r25, nat);
94429e4e025STakao Indoh 	copy_reg(&bank[26-16], ms->pmsa_nat_bits, &regs->r26, nat);
94529e4e025STakao Indoh 	copy_reg(&bank[27-16], ms->pmsa_nat_bits, &regs->r27, nat);
94629e4e025STakao Indoh 	copy_reg(&bank[28-16], ms->pmsa_nat_bits, &regs->r28, nat);
94729e4e025STakao Indoh 	copy_reg(&bank[29-16], ms->pmsa_nat_bits, &regs->r29, nat);
94829e4e025STakao Indoh 	copy_reg(&bank[30-16], ms->pmsa_nat_bits, &regs->r30, nat);
94929e4e025STakao Indoh 	copy_reg(&bank[31-16], ms->pmsa_nat_bits, &regs->r31, nat);
95029e4e025STakao Indoh }
95129e4e025STakao Indoh 
9527f613c7dSKeith Owens /* On entry to this routine, we are running on the per cpu stack, see
9537f613c7dSKeith Owens  * mca_asm.h.  The original stack has not been touched by this event.  Some of
9547f613c7dSKeith Owens  * the original stack's registers will be in the RBS on this stack.  This stack
9557f613c7dSKeith Owens  * also contains a partial pt_regs and switch_stack, the rest of the data is in
9567f613c7dSKeith Owens  * PAL minstate.
9577f613c7dSKeith Owens  *
9587f613c7dSKeith Owens  * The first thing to do is modify the original stack to look like a blocked
9597f613c7dSKeith Owens  * task so we can run backtrace on the original task.  Also mark the per cpu
9607f613c7dSKeith Owens  * stack as current to ensure that we use the correct task state, it also means
9617f613c7dSKeith Owens  * that we can do backtrace on the MCA/INIT handler code itself.
9627f613c7dSKeith Owens  */
9637f613c7dSKeith Owens 
96436c8b586SIngo Molnar static struct task_struct *
9657f613c7dSKeith Owens ia64_mca_modify_original_stack(struct pt_regs *regs,
9667f613c7dSKeith Owens 		const struct switch_stack *sw,
9677f613c7dSKeith Owens 		struct ia64_sal_os_state *sos,
9687f613c7dSKeith Owens 		const char *type)
9697f613c7dSKeith Owens {
970e9ac054dSKeith Owens 	char *p;
9717f613c7dSKeith Owens 	ia64_va va;
9727f613c7dSKeith Owens 	extern char ia64_leave_kernel[];	/* Need asm address, not function descriptor */
9737f613c7dSKeith Owens 	const pal_min_state_area_t *ms = sos->pal_min_state;
97436c8b586SIngo Molnar 	struct task_struct *previous_current;
9757f613c7dSKeith Owens 	struct pt_regs *old_regs;
9767f613c7dSKeith Owens 	struct switch_stack *old_sw;
9777f613c7dSKeith Owens 	unsigned size = sizeof(struct pt_regs) +
9787f613c7dSKeith Owens 			sizeof(struct switch_stack) + 16;
979e088a4adSMatthew Wilcox 	unsigned long *old_bspstore, *old_bsp;
980e088a4adSMatthew Wilcox 	unsigned long *new_bspstore, *new_bsp;
981e088a4adSMatthew Wilcox 	unsigned long old_unat, old_rnat, new_rnat, nat;
9827f613c7dSKeith Owens 	u64 slots, loadrs = regs->loadrs;
9837f613c7dSKeith Owens 	u64 r12 = ms->pmsa_gr[12-1], r13 = ms->pmsa_gr[13-1];
9847f613c7dSKeith Owens 	u64 ar_bspstore = regs->ar_bspstore;
9857f613c7dSKeith Owens 	u64 ar_bsp = regs->ar_bspstore + (loadrs >> 16);
9867f613c7dSKeith Owens 	const char *msg;
9877f613c7dSKeith Owens 	int cpu = smp_processor_id();
9887f613c7dSKeith Owens 
9897f613c7dSKeith Owens 	previous_current = curr_task(cpu);
990a458ae2eSPeter Zijlstra 	ia64_set_curr_task(cpu, current);
9917f613c7dSKeith Owens 	if ((p = strchr(current->comm, ' ')))
9927f613c7dSKeith Owens 		*p = '\0';
9937f613c7dSKeith Owens 
9947f613c7dSKeith Owens 	/* Best effort attempt to cope with MCA/INIT delivered while in
9957f613c7dSKeith Owens 	 * physical mode.
9967f613c7dSKeith Owens 	 */
9977f613c7dSKeith Owens 	regs->cr_ipsr = ms->pmsa_ipsr;
9987f613c7dSKeith Owens 	if (ia64_psr(regs)->dt == 0) {
9997f613c7dSKeith Owens 		va.l = r12;
10007f613c7dSKeith Owens 		if (va.f.reg == 0) {
10017f613c7dSKeith Owens 			va.f.reg = 7;
10027f613c7dSKeith Owens 			r12 = va.l;
10037f613c7dSKeith Owens 		}
10047f613c7dSKeith Owens 		va.l = r13;
10057f613c7dSKeith Owens 		if (va.f.reg == 0) {
10067f613c7dSKeith Owens 			va.f.reg = 7;
10077f613c7dSKeith Owens 			r13 = va.l;
10087f613c7dSKeith Owens 		}
10097f613c7dSKeith Owens 	}
10107f613c7dSKeith Owens 	if (ia64_psr(regs)->rt == 0) {
10117f613c7dSKeith Owens 		va.l = ar_bspstore;
10127f613c7dSKeith Owens 		if (va.f.reg == 0) {
10137f613c7dSKeith Owens 			va.f.reg = 7;
10147f613c7dSKeith Owens 			ar_bspstore = va.l;
10157f613c7dSKeith Owens 		}
10167f613c7dSKeith Owens 		va.l = ar_bsp;
10177f613c7dSKeith Owens 		if (va.f.reg == 0) {
10187f613c7dSKeith Owens 			va.f.reg = 7;
10197f613c7dSKeith Owens 			ar_bsp = va.l;
10207f613c7dSKeith Owens 		}
10217f613c7dSKeith Owens 	}
10227f613c7dSKeith Owens 
10237f613c7dSKeith Owens 	/* mca_asm.S ia64_old_stack() cannot assume that the dirty registers
10247f613c7dSKeith Owens 	 * have been copied to the old stack, the old stack may fail the
10257f613c7dSKeith Owens 	 * validation tests below.  So ia64_old_stack() must restore the dirty
10267f613c7dSKeith Owens 	 * registers from the new stack.  The old and new bspstore probably
10277f613c7dSKeith Owens 	 * have different alignments, so loadrs calculated on the old bsp
10287f613c7dSKeith Owens 	 * cannot be used to restore from the new bsp.  Calculate a suitable
10297f613c7dSKeith Owens 	 * loadrs for the new stack and save it in the new pt_regs, where
10307f613c7dSKeith Owens 	 * ia64_old_stack() can get it.
10317f613c7dSKeith Owens 	 */
1032e088a4adSMatthew Wilcox 	old_bspstore = (unsigned long *)ar_bspstore;
1033e088a4adSMatthew Wilcox 	old_bsp = (unsigned long *)ar_bsp;
10347f613c7dSKeith Owens 	slots = ia64_rse_num_regs(old_bspstore, old_bsp);
1035e088a4adSMatthew Wilcox 	new_bspstore = (unsigned long *)((u64)current + IA64_RBS_OFFSET);
10367f613c7dSKeith Owens 	new_bsp = ia64_rse_skip_regs(new_bspstore, slots);
10377f613c7dSKeith Owens 	regs->loadrs = (new_bsp - new_bspstore) * 8 << 16;
10387f613c7dSKeith Owens 
10397f613c7dSKeith Owens 	/* Verify the previous stack state before we change it */
10407f613c7dSKeith Owens 	if (user_mode(regs)) {
10417f613c7dSKeith Owens 		msg = "occurred in user space";
1042e9ac054dSKeith Owens 		/* previous_current is guaranteed to be valid when the task was
1043e9ac054dSKeith Owens 		 * in user space, so ...
1044e9ac054dSKeith Owens 		 */
1045e9ac054dSKeith Owens 		ia64_mca_modify_comm(previous_current);
10467f613c7dSKeith Owens 		goto no_mod;
10477f613c7dSKeith Owens 	}
1048d2a28ad9SRuss Anderson 
10497f613c7dSKeith Owens 	if (r13 != sos->prev_IA64_KR_CURRENT) {
10507f613c7dSKeith Owens 		msg = "inconsistent previous current and r13";
10517f613c7dSKeith Owens 		goto no_mod;
10527f613c7dSKeith Owens 	}
10531612b18cSRuss Anderson 
10541612b18cSRuss Anderson 	if (!mca_recover_range(ms->pmsa_iip)) {
10557f613c7dSKeith Owens 		if ((r12 - r13) >= KERNEL_STACK_SIZE) {
10567f613c7dSKeith Owens 			msg = "inconsistent r12 and r13";
10577f613c7dSKeith Owens 			goto no_mod;
10587f613c7dSKeith Owens 		}
10597f613c7dSKeith Owens 		if ((ar_bspstore - r13) >= KERNEL_STACK_SIZE) {
10607f613c7dSKeith Owens 			msg = "inconsistent ar.bspstore and r13";
10617f613c7dSKeith Owens 			goto no_mod;
10627f613c7dSKeith Owens 		}
10637f613c7dSKeith Owens 		va.p = old_bspstore;
10647f613c7dSKeith Owens 		if (va.f.reg < 5) {
10657f613c7dSKeith Owens 			msg = "old_bspstore is in the wrong region";
10667f613c7dSKeith Owens 			goto no_mod;
10677f613c7dSKeith Owens 		}
10687f613c7dSKeith Owens 		if ((ar_bsp - r13) >= KERNEL_STACK_SIZE) {
10697f613c7dSKeith Owens 			msg = "inconsistent ar.bsp and r13";
10707f613c7dSKeith Owens 			goto no_mod;
10717f613c7dSKeith Owens 		}
10727f613c7dSKeith Owens 		size += (ia64_rse_skip_regs(old_bspstore, slots) - old_bspstore) * 8;
10737f613c7dSKeith Owens 		if (ar_bspstore + size > r12) {
10747f613c7dSKeith Owens 			msg = "no room for blocked state";
10757f613c7dSKeith Owens 			goto no_mod;
10767f613c7dSKeith Owens 		}
1077d2a28ad9SRuss Anderson 	}
10787f613c7dSKeith Owens 
1079e9ac054dSKeith Owens 	ia64_mca_modify_comm(previous_current);
10807f613c7dSKeith Owens 
10817f613c7dSKeith Owens 	/* Make the original task look blocked.  First stack a struct pt_regs,
10827f613c7dSKeith Owens 	 * describing the state at the time of interrupt.  mca_asm.S built a
10837f613c7dSKeith Owens 	 * partial pt_regs, copy it and fill in the blanks using minstate.
10847f613c7dSKeith Owens 	 */
10857f613c7dSKeith Owens 	p = (char *)r12 - sizeof(*regs);
10867f613c7dSKeith Owens 	old_regs = (struct pt_regs *)p;
10877f613c7dSKeith Owens 	memcpy(old_regs, regs, sizeof(*regs));
10887f613c7dSKeith Owens 	old_regs->loadrs = loadrs;
10897f613c7dSKeith Owens 	old_unat = old_regs->ar_unat;
10909ee27c76STakao Indoh 	finish_pt_regs(old_regs, sos, &old_unat);
10917f613c7dSKeith Owens 
10927f613c7dSKeith Owens 	/* Next stack a struct switch_stack.  mca_asm.S built a partial
10937f613c7dSKeith Owens 	 * switch_stack, copy it and fill in the blanks using pt_regs and
10947f613c7dSKeith Owens 	 * minstate.
10957f613c7dSKeith Owens 	 *
10967f613c7dSKeith Owens 	 * In the synthesized switch_stack, b0 points to ia64_leave_kernel,
10977f613c7dSKeith Owens 	 * ar.pfs is set to 0.
10987f613c7dSKeith Owens 	 *
10997f613c7dSKeith Owens 	 * unwind.c::unw_unwind() does special processing for interrupt frames.
11007f613c7dSKeith Owens 	 * It checks if the PRED_NON_SYSCALL predicate is set, if the predicate
11017f613c7dSKeith Owens 	 * is clear then unw_unwind() does _not_ adjust bsp over pt_regs.  Not
11027f613c7dSKeith Owens 	 * that this is documented, of course.  Set PRED_NON_SYSCALL in the
11037f613c7dSKeith Owens 	 * switch_stack on the original stack so it will unwind correctly when
11047f613c7dSKeith Owens 	 * unwind.c reads pt_regs.
11057f613c7dSKeith Owens 	 *
11067f613c7dSKeith Owens 	 * thread.ksp is updated to point to the synthesized switch_stack.
11077f613c7dSKeith Owens 	 */
11087f613c7dSKeith Owens 	p -= sizeof(struct switch_stack);
11097f613c7dSKeith Owens 	old_sw = (struct switch_stack *)p;
11107f613c7dSKeith Owens 	memcpy(old_sw, sw, sizeof(*sw));
11117f613c7dSKeith Owens 	old_sw->caller_unat = old_unat;
11127f613c7dSKeith Owens 	old_sw->ar_fpsr = old_regs->ar_fpsr;
11137f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[4-1], ms->pmsa_nat_bits, &old_sw->r4, &old_unat);
11147f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[5-1], ms->pmsa_nat_bits, &old_sw->r5, &old_unat);
11157f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[6-1], ms->pmsa_nat_bits, &old_sw->r6, &old_unat);
11167f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[7-1], ms->pmsa_nat_bits, &old_sw->r7, &old_unat);
11177f613c7dSKeith Owens 	old_sw->b0 = (u64)ia64_leave_kernel;
11187f613c7dSKeith Owens 	old_sw->b1 = ms->pmsa_br1;
11197f613c7dSKeith Owens 	old_sw->ar_pfs = 0;
11207f613c7dSKeith Owens 	old_sw->ar_unat = old_unat;
11217f613c7dSKeith Owens 	old_sw->pr = old_regs->pr | (1UL << PRED_NON_SYSCALL);
11227f613c7dSKeith Owens 	previous_current->thread.ksp = (u64)p - 16;
11237f613c7dSKeith Owens 
11247f613c7dSKeith Owens 	/* Finally copy the original stack's registers back to its RBS.
11257f613c7dSKeith Owens 	 * Registers from ar.bspstore through ar.bsp at the time of the event
11267f613c7dSKeith Owens 	 * are in the current RBS, copy them back to the original stack.  The
11277f613c7dSKeith Owens 	 * copy must be done register by register because the original bspstore
11287f613c7dSKeith Owens 	 * and the current one have different alignments, so the saved RNAT
11297f613c7dSKeith Owens 	 * data occurs at different places.
11307f613c7dSKeith Owens 	 *
11317f613c7dSKeith Owens 	 * mca_asm does cover, so the old_bsp already includes all registers at
11327f613c7dSKeith Owens 	 * the time of MCA/INIT.  It also does flushrs, so all registers before
11337f613c7dSKeith Owens 	 * this function have been written to backing store on the MCA/INIT
11347f613c7dSKeith Owens 	 * stack.
11357f613c7dSKeith Owens 	 */
11367f613c7dSKeith Owens 	new_rnat = ia64_get_rnat(ia64_rse_rnat_addr(new_bspstore));
11377f613c7dSKeith Owens 	old_rnat = regs->ar_rnat;
11387f613c7dSKeith Owens 	while (slots--) {
11397f613c7dSKeith Owens 		if (ia64_rse_is_rnat_slot(new_bspstore)) {
11407f613c7dSKeith Owens 			new_rnat = ia64_get_rnat(new_bspstore++);
11417f613c7dSKeith Owens 		}
11427f613c7dSKeith Owens 		if (ia64_rse_is_rnat_slot(old_bspstore)) {
11437f613c7dSKeith Owens 			*old_bspstore++ = old_rnat;
11447f613c7dSKeith Owens 			old_rnat = 0;
11457f613c7dSKeith Owens 		}
11467f613c7dSKeith Owens 		nat = (new_rnat >> ia64_rse_slot_num(new_bspstore)) & 1UL;
11477f613c7dSKeith Owens 		old_rnat &= ~(1UL << ia64_rse_slot_num(old_bspstore));
11487f613c7dSKeith Owens 		old_rnat |= (nat << ia64_rse_slot_num(old_bspstore));
11497f613c7dSKeith Owens 		*old_bspstore++ = *new_bspstore++;
11507f613c7dSKeith Owens 	}
11517f613c7dSKeith Owens 	old_sw->ar_bspstore = (unsigned long)old_bspstore;
11527f613c7dSKeith Owens 	old_sw->ar_rnat = old_rnat;
11537f613c7dSKeith Owens 
11547f613c7dSKeith Owens 	sos->prev_task = previous_current;
11557f613c7dSKeith Owens 	return previous_current;
11567f613c7dSKeith Owens 
11577f613c7dSKeith Owens no_mod:
1158ef23cdbeSHidetoshi Seto 	mprintk(KERN_INFO "cpu %d, %s %s, original stack not modified\n",
11597f613c7dSKeith Owens 			smp_processor_id(), type, msg);
116029e4e025STakao Indoh 	old_unat = regs->ar_unat;
11619ee27c76STakao Indoh 	finish_pt_regs(regs, sos, &old_unat);
11627f613c7dSKeith Owens 	return previous_current;
11637f613c7dSKeith Owens }
11647f613c7dSKeith Owens 
11657f613c7dSKeith Owens /* The monarch/slave interaction is based on monarch_cpu and requires that all
11667f613c7dSKeith Owens  * slaves have entered rendezvous before the monarch leaves.  If any cpu has
11677f613c7dSKeith Owens  * not entered rendezvous yet then wait a bit.  The assumption is that any
11687f613c7dSKeith Owens  * slave that has not rendezvoused after a reasonable time is never going to do
11697f613c7dSKeith Owens  * so.  In this context, slave includes cpus that respond to the MCA rendezvous
11707f613c7dSKeith Owens  * interrupt, as well as cpus that receive the INIT slave event.
11717f613c7dSKeith Owens  */
11727f613c7dSKeith Owens 
11737f613c7dSKeith Owens static void
1174356a5c1cSKeith Owens ia64_wait_for_slaves(int monarch, const char *type)
11757f613c7dSKeith Owens {
11762bc5c282SRuss Anderson 	int c, i , wait;
11772bc5c282SRuss Anderson 
11782bc5c282SRuss Anderson 	/*
11792bc5c282SRuss Anderson 	 * wait 5 seconds total for slaves (arbitrary)
11802bc5c282SRuss Anderson 	 */
11812bc5c282SRuss Anderson 	for (i = 0; i < 5000; i++) {
11822bc5c282SRuss Anderson 		wait = 0;
11837f613c7dSKeith Owens 		for_each_online_cpu(c) {
11847f613c7dSKeith Owens 			if (c == monarch)
11857f613c7dSKeith Owens 				continue;
11862bc5c282SRuss Anderson 			if (ia64_mc_info.imi_rendez_checkin[c]
11872bc5c282SRuss Anderson 					== IA64_MCA_RENDEZ_CHECKIN_NOTDONE) {
11882bc5c282SRuss Anderson 				udelay(1000);		/* short wait */
11897f613c7dSKeith Owens 				wait = 1;
11907f613c7dSKeith Owens 				break;
11917f613c7dSKeith Owens 			}
11927f613c7dSKeith Owens 		}
11937f613c7dSKeith Owens 		if (!wait)
11949336b083SKeith Owens 			goto all_in;
11957f613c7dSKeith Owens 	}
11962bc5c282SRuss Anderson 
119743ed3bafSHidetoshi Seto 	/*
119843ed3bafSHidetoshi Seto 	 * Maybe slave(s) dead. Print buffered messages immediately.
119943ed3bafSHidetoshi Seto 	 */
120043ed3bafSHidetoshi Seto 	ia64_mlogbuf_finish(0);
120143ed3bafSHidetoshi Seto 	mprintk(KERN_INFO "OS %s slave did not rendezvous on cpu", type);
12029336b083SKeith Owens 	for_each_online_cpu(c) {
12039336b083SKeith Owens 		if (c == monarch)
12049336b083SKeith Owens 			continue;
12059336b083SKeith Owens 		if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE)
120643ed3bafSHidetoshi Seto 			mprintk(" %d", c);
12079336b083SKeith Owens 	}
120843ed3bafSHidetoshi Seto 	mprintk("\n");
12099336b083SKeith Owens 	return;
12109336b083SKeith Owens 
12119336b083SKeith Owens all_in:
121243ed3bafSHidetoshi Seto 	mprintk(KERN_INFO "All OS %s slaves have reached rendezvous\n", type);
12139336b083SKeith Owens 	return;
12147f613c7dSKeith Owens }
12157f613c7dSKeith Owens 
121696651896SXiantao Zhang /*  mca_insert_tr
121796651896SXiantao Zhang  *
121896651896SXiantao Zhang  *  Switch rid when TR reload and needed!
121996651896SXiantao Zhang  *  iord: 1: itr, 2: itr;
122096651896SXiantao Zhang  *
122196651896SXiantao Zhang */
122296651896SXiantao Zhang static void mca_insert_tr(u64 iord)
122396651896SXiantao Zhang {
122496651896SXiantao Zhang 
122596651896SXiantao Zhang 	int i;
122696651896SXiantao Zhang 	u64 old_rr;
122796651896SXiantao Zhang 	struct ia64_tr_entry *p;
122896651896SXiantao Zhang 	unsigned long psr;
122996651896SXiantao Zhang 	int cpu = smp_processor_id();
123096651896SXiantao Zhang 
12316c57a332STony Luck 	if (!ia64_idtrs[cpu])
12326c57a332STony Luck 		return;
12336c57a332STony Luck 
123496651896SXiantao Zhang 	psr = ia64_clear_ic();
123596651896SXiantao Zhang 	for (i = IA64_TR_ALLOC_BASE; i < IA64_TR_ALLOC_MAX; i++) {
12366c57a332STony Luck 		p = ia64_idtrs[cpu] + (iord - 1) * IA64_TR_ALLOC_MAX;
123796651896SXiantao Zhang 		if (p->pte & 0x1) {
123896651896SXiantao Zhang 			old_rr = ia64_get_rr(p->ifa);
123996651896SXiantao Zhang 			if (old_rr != p->rr) {
124096651896SXiantao Zhang 				ia64_set_rr(p->ifa, p->rr);
124196651896SXiantao Zhang 				ia64_srlz_d();
124296651896SXiantao Zhang 			}
124396651896SXiantao Zhang 			ia64_ptr(iord, p->ifa, p->itir >> 2);
124496651896SXiantao Zhang 			ia64_srlz_i();
124596651896SXiantao Zhang 			if (iord & 0x1) {
124696651896SXiantao Zhang 				ia64_itr(0x1, i, p->ifa, p->pte, p->itir >> 2);
124796651896SXiantao Zhang 				ia64_srlz_i();
124896651896SXiantao Zhang 			}
124996651896SXiantao Zhang 			if (iord & 0x2) {
125096651896SXiantao Zhang 				ia64_itr(0x2, i, p->ifa, p->pte, p->itir >> 2);
125196651896SXiantao Zhang 				ia64_srlz_i();
125296651896SXiantao Zhang 			}
125396651896SXiantao Zhang 			if (old_rr != p->rr) {
125496651896SXiantao Zhang 				ia64_set_rr(p->ifa, old_rr);
125596651896SXiantao Zhang 				ia64_srlz_d();
125696651896SXiantao Zhang 			}
125796651896SXiantao Zhang 		}
125896651896SXiantao Zhang 	}
125996651896SXiantao Zhang 	ia64_set_psr(psr);
126096651896SXiantao Zhang }
126196651896SXiantao Zhang 
12621da177e4SLinus Torvalds /*
12637f613c7dSKeith Owens  * ia64_mca_handler
12641da177e4SLinus Torvalds  *
12651da177e4SLinus Torvalds  *	This is uncorrectable machine check handler called from OS_MCA
12661da177e4SLinus Torvalds  *	dispatch code which is in turn called from SAL_CHECK().
12671da177e4SLinus Torvalds  *	This is the place where the core of OS MCA handling is done.
12681da177e4SLinus Torvalds  *	Right now the logs are extracted and displayed in a well-defined
12691da177e4SLinus Torvalds  *	format. This handler code is supposed to be run only on the
12701da177e4SLinus Torvalds  *	monarch processor. Once the monarch is done with MCA handling
12711da177e4SLinus Torvalds  *	further MCA logging is enabled by clearing logs.
12721da177e4SLinus Torvalds  *	Monarch also has the duty of sending wakeup-IPIs to pull the
12731da177e4SLinus Torvalds  *	slave processors out of rendezvous spinloop.
12741612b18cSRuss Anderson  *
12751612b18cSRuss Anderson  *	If multiple processors call into OS_MCA, the first will become
12761612b18cSRuss Anderson  *	the monarch.  Subsequent cpus will be recorded in the mca_cpu
12771612b18cSRuss Anderson  *	bitmask.  After the first monarch has processed its MCA, it
12781612b18cSRuss Anderson  *	will wake up the next cpu in the mca_cpu bitmask and then go
12791612b18cSRuss Anderson  *	into the rendezvous loop.  When all processors have serviced
12801612b18cSRuss Anderson  *	their MCA, the last monarch frees up the rest of the processors.
12811da177e4SLinus Torvalds  */
12821da177e4SLinus Torvalds void
12837f613c7dSKeith Owens ia64_mca_handler(struct pt_regs *regs, struct switch_stack *sw,
12847f613c7dSKeith Owens 		 struct ia64_sal_os_state *sos)
12851da177e4SLinus Torvalds {
12867f613c7dSKeith Owens 	int recover, cpu = smp_processor_id();
128736c8b586SIngo Molnar 	struct task_struct *previous_current;
1288958b166cSKeith Owens 	struct ia64_mca_notify_die nd =
12894fa2f0e6SHidetoshi Seto 		{ .sos = sos, .monarch_cpu = &monarch_cpu, .data = &recover };
12901612b18cSRuss Anderson 	static atomic_t mca_count;
12911612b18cSRuss Anderson 	static cpumask_t mca_cpu;
12927f613c7dSKeith Owens 
12931612b18cSRuss Anderson 	if (atomic_add_return(1, &mca_count) == 1) {
12941612b18cSRuss Anderson 		monarch_cpu = cpu;
12951612b18cSRuss Anderson 		sos->monarch = 1;
12961612b18cSRuss Anderson 	} else {
12975d2068daSRusty Russell 		cpumask_set_cpu(cpu, &mca_cpu);
12981612b18cSRuss Anderson 		sos->monarch = 0;
12991612b18cSRuss Anderson 	}
130043ed3bafSHidetoshi Seto 	mprintk(KERN_INFO "Entered OS MCA handler. PSP=%lx cpu=%d "
130143ed3bafSHidetoshi Seto 		"monarch=%ld\n", sos->proc_state_param, cpu, sos->monarch);
13029336b083SKeith Owens 
13037f613c7dSKeith Owens 	previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "MCA");
13041612b18cSRuss Anderson 
13054fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_MONARCH_ENTER, regs, (long)&nd, 1);
1306e1b1eb01SRuss Anderson 
1307e1b1eb01SRuss Anderson 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_CONCURRENT_MCA;
13081612b18cSRuss Anderson 	if (sos->monarch) {
1309356a5c1cSKeith Owens 		ia64_wait_for_slaves(cpu, "MCA");
13107f613c7dSKeith Owens 
1311e1b1eb01SRuss Anderson 		/* Wakeup all the processors which are spinning in the
1312e1b1eb01SRuss Anderson 		 * rendezvous loop.  They will leave SAL, then spin in the OS
1313e1b1eb01SRuss Anderson 		 * with interrupts disabled until this monarch cpu leaves the
1314e1b1eb01SRuss Anderson 		 * MCA handler.  That gets control back to the OS so we can
1315e1b1eb01SRuss Anderson 		 * backtrace the other cpus, backtrace when spinning in SAL
1316e1b1eb01SRuss Anderson 		 * does not work.
13177f613c7dSKeith Owens 		 */
13187f613c7dSKeith Owens 		ia64_mca_wakeup_all();
1319e1b1eb01SRuss Anderson 	} else {
13205d2068daSRusty Russell 		while (cpumask_test_cpu(cpu, &mca_cpu))
1321e1b1eb01SRuss Anderson 			cpu_relax();	/* spin until monarch wakes us */
1322e1b1eb01SRuss Anderson 	}
13231da177e4SLinus Torvalds 
13244fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_MONARCH_PROCESS, regs, (long)&nd, 1);
1325284e5427SHidetoshi Seto 
13261da177e4SLinus Torvalds 	/* Get the MCA error record and log it */
13271da177e4SLinus Torvalds 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_MCA);
13281da177e4SLinus Torvalds 
1329618b206fSRuss Anderson 	/* MCA error recovery */
1330618b206fSRuss Anderson 	recover = (ia64_mca_ucmc_extension
13311da177e4SLinus Torvalds 		&& ia64_mca_ucmc_extension(
13321da177e4SLinus Torvalds 			IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA),
13337f613c7dSKeith Owens 			sos));
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	if (recover) {
13361da177e4SLinus Torvalds 		sal_log_record_header_t *rh = IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA);
13371da177e4SLinus Torvalds 		rh->severity = sal_log_severity_corrected;
13381da177e4SLinus Torvalds 		ia64_sal_clear_state_info(SAL_INFO_TYPE_MCA);
13397f613c7dSKeith Owens 		sos->os_status = IA64_MCA_CORRECTED;
134043ed3bafSHidetoshi Seto 	} else {
134143ed3bafSHidetoshi Seto 		/* Dump buffered message to console */
134243ed3bafSHidetoshi Seto 		ia64_mlogbuf_finish(1);
13431da177e4SLinus Torvalds 	}
1344b0247a55SHidetoshi Seto 
13456065a244SChristoph Lameter 	if (__this_cpu_read(ia64_mca_tr_reload)) {
134696651896SXiantao Zhang 		mca_insert_tr(0x1); /*Reload dynamic itrs*/
134796651896SXiantao Zhang 		mca_insert_tr(0x2); /*Reload dynamic itrs*/
134896651896SXiantao Zhang 	}
134971b264f8STony Luck 
13504fa2f0e6SHidetoshi Seto 	NOTIFY_MCA(DIE_MCA_MONARCH_LEAVE, regs, (long)&nd, 1);
13511da177e4SLinus Torvalds 
13521612b18cSRuss Anderson 	if (atomic_dec_return(&mca_count) > 0) {
13531612b18cSRuss Anderson 		int i;
13541612b18cSRuss Anderson 
13551612b18cSRuss Anderson 		/* wake up the next monarch cpu,
13561612b18cSRuss Anderson 		 * and put this cpu in the rendez loop.
13571612b18cSRuss Anderson 		 */
13581612b18cSRuss Anderson 		for_each_online_cpu(i) {
13595d2068daSRusty Russell 			if (cpumask_test_cpu(i, &mca_cpu)) {
13601612b18cSRuss Anderson 				monarch_cpu = i;
13615d2068daSRusty Russell 				cpumask_clear_cpu(i, &mca_cpu);	/* wake next cpu */
13621612b18cSRuss Anderson 				while (monarch_cpu != -1)
13631612b18cSRuss Anderson 					cpu_relax();	/* spin until last cpu leaves */
1364a458ae2eSPeter Zijlstra 				ia64_set_curr_task(cpu, previous_current);
1365e1b1eb01SRuss Anderson 				ia64_mc_info.imi_rendez_checkin[cpu]
1366e1b1eb01SRuss Anderson 						= IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
13671612b18cSRuss Anderson 				return;
13681612b18cSRuss Anderson 			}
13691612b18cSRuss Anderson 		}
13701612b18cSRuss Anderson 	}
1371a458ae2eSPeter Zijlstra 	ia64_set_curr_task(cpu, previous_current);
1372e1b1eb01SRuss Anderson 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
1373e1b1eb01SRuss Anderson 	monarch_cpu = -1;	/* This frees the slaves and previous monarchs */
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
13766d5aefb8SDavid Howells static DECLARE_WORK(cmc_disable_work, ia64_mca_cmc_vector_disable_keventd);
13776d5aefb8SDavid Howells static DECLARE_WORK(cmc_enable_work, ia64_mca_cmc_vector_enable_keventd);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds /*
13801da177e4SLinus Torvalds  * ia64_mca_cmc_int_handler
13811da177e4SLinus Torvalds  *
13821da177e4SLinus Torvalds  *  This is corrected machine check interrupt handler.
13831da177e4SLinus Torvalds  *	Right now the logs are extracted and displayed in a well-defined
13841da177e4SLinus Torvalds  *	format.
13851da177e4SLinus Torvalds  *
13861da177e4SLinus Torvalds  * Inputs
13871da177e4SLinus Torvalds  *      interrupt number
13881da177e4SLinus Torvalds  *      client data arg ptr
13891da177e4SLinus Torvalds  *
13901da177e4SLinus Torvalds  * Outputs
13911da177e4SLinus Torvalds  *	None
13921da177e4SLinus Torvalds  */
13931da177e4SLinus Torvalds static irqreturn_t
13947d12e780SDavid Howells ia64_mca_cmc_int_handler(int cmc_irq, void *arg)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	static unsigned long	cmc_history[CMC_HISTORY_LENGTH];
13971da177e4SLinus Torvalds 	static int		index;
13981da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(cmc_history_lock);
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
1401d4ed8084SHarvey Harrison 		       __func__, cmc_irq, smp_processor_id());
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	/* SAL spec states this should run w/ interrupts enabled */
14041da177e4SLinus Torvalds 	local_irq_enable();
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	spin_lock(&cmc_history_lock);
14071da177e4SLinus Torvalds 	if (!cmc_polling_enabled) {
14081da177e4SLinus Torvalds 		int i, count = 1; /* we know 1 happened now */
14091da177e4SLinus Torvalds 		unsigned long now = jiffies;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 		for (i = 0; i < CMC_HISTORY_LENGTH; i++) {
14121da177e4SLinus Torvalds 			if (now - cmc_history[i] <= HZ)
14131da177e4SLinus Torvalds 				count++;
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 		IA64_MCA_DEBUG(KERN_INFO "CMC threshold %d/%d\n", count, CMC_HISTORY_LENGTH);
14171da177e4SLinus Torvalds 		if (count >= CMC_HISTORY_LENGTH) {
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 			cmc_polling_enabled = 1;
14201da177e4SLinus Torvalds 			spin_unlock(&cmc_history_lock);
142176e677e2SBryan Sutula 			/* If we're being hit with CMC interrupts, we won't
142276e677e2SBryan Sutula 			 * ever execute the schedule_work() below.  Need to
142376e677e2SBryan Sutula 			 * disable CMC interrupts on this processor now.
142476e677e2SBryan Sutula 			 */
142576e677e2SBryan Sutula 			ia64_mca_cmc_vector_disable(NULL);
14261da177e4SLinus Torvalds 			schedule_work(&cmc_disable_work);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 			/*
14291da177e4SLinus Torvalds 			 * Corrected errors will still be corrected, but
14301da177e4SLinus Torvalds 			 * make sure there's a log somewhere that indicates
14311da177e4SLinus Torvalds 			 * something is generating more than we can handle.
14321da177e4SLinus Torvalds 			 */
14331da177e4SLinus Torvalds 			printk(KERN_WARNING "WARNING: Switching to polling CMC handler; error records may be lost\n");
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 			mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 			/* lock already released, get out now */
1438ddb4f0dfSHidetoshi Seto 			goto out;
14391da177e4SLinus Torvalds 		} else {
14401da177e4SLinus Torvalds 			cmc_history[index++] = now;
14411da177e4SLinus Torvalds 			if (index == CMC_HISTORY_LENGTH)
14421da177e4SLinus Torvalds 				index = 0;
14431da177e4SLinus Torvalds 		}
14441da177e4SLinus Torvalds 	}
14451da177e4SLinus Torvalds 	spin_unlock(&cmc_history_lock);
1446ddb4f0dfSHidetoshi Seto out:
1447ddb4f0dfSHidetoshi Seto 	/* Get the CMC error record and log it */
1448ddb4f0dfSHidetoshi Seto 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CMC);
1449ddb4f0dfSHidetoshi Seto 
14500f261edeSJiri Kosina 	local_irq_disable();
14510f261edeSJiri Kosina 
14521da177e4SLinus Torvalds 	return IRQ_HANDLED;
14531da177e4SLinus Torvalds }
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds /*
14561da177e4SLinus Torvalds  *  ia64_mca_cmc_int_caller
14571da177e4SLinus Torvalds  *
14581da177e4SLinus Torvalds  * 	Triggered by sw interrupt from CMC polling routine.  Calls
14591da177e4SLinus Torvalds  * 	real interrupt handler and either triggers a sw interrupt
14601da177e4SLinus Torvalds  * 	on the next cpu or does cleanup at the end.
14611da177e4SLinus Torvalds  *
14621da177e4SLinus Torvalds  * Inputs
14631da177e4SLinus Torvalds  *	interrupt number
14641da177e4SLinus Torvalds  *	client data arg ptr
14651da177e4SLinus Torvalds  * Outputs
14661da177e4SLinus Torvalds  * 	handled
14671da177e4SLinus Torvalds  */
14681da177e4SLinus Torvalds static irqreturn_t
14697d12e780SDavid Howells ia64_mca_cmc_int_caller(int cmc_irq, void *arg)
14701da177e4SLinus Torvalds {
14711da177e4SLinus Torvalds 	static int start_count = -1;
14721da177e4SLinus Torvalds 	unsigned int cpuid;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	cpuid = smp_processor_id();
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	/* If first cpu, update count */
14771da177e4SLinus Torvalds 	if (start_count == -1)
14781da177e4SLinus Torvalds 		start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CMC);
14791da177e4SLinus Torvalds 
14807d12e780SDavid Howells 	ia64_mca_cmc_int_handler(cmc_irq, arg);
14811da177e4SLinus Torvalds 
14825dd3c994SRusty Russell 	cpuid = cpumask_next(cpuid+1, cpu_online_mask);
14831da177e4SLinus Torvalds 
14845dd3c994SRusty Russell 	if (cpuid < nr_cpu_ids) {
148505933aacSChristoph Hellwig 		ia64_send_ipi(cpuid, IA64_CMCP_VECTOR, IA64_IPI_DM_INT, 0);
14861da177e4SLinus Torvalds 	} else {
14871da177e4SLinus Torvalds 		/* If no log record, switch out of polling mode */
14881da177e4SLinus Torvalds 		if (start_count == IA64_LOG_COUNT(SAL_INFO_TYPE_CMC)) {
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 			printk(KERN_WARNING "Returning to interrupt driven CMC handler\n");
14911da177e4SLinus Torvalds 			schedule_work(&cmc_enable_work);
14921da177e4SLinus Torvalds 			cmc_polling_enabled = 0;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 		} else {
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 			mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
14971da177e4SLinus Torvalds 		}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 		start_count = -1;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	return IRQ_HANDLED;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds /*
15061da177e4SLinus Torvalds  *  ia64_mca_cmc_poll
15071da177e4SLinus Torvalds  *
15081da177e4SLinus Torvalds  *	Poll for Corrected Machine Checks (CMCs)
15091da177e4SLinus Torvalds  *
15101da177e4SLinus Torvalds  * Inputs   :   dummy(unused)
15111da177e4SLinus Torvalds  * Outputs  :   None
15121da177e4SLinus Torvalds  *
15131da177e4SLinus Torvalds  */
15141da177e4SLinus Torvalds static void
15152c513d4fSKees Cook ia64_mca_cmc_poll (struct timer_list *unused)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	/* Trigger a CMC interrupt cascade  */
151805933aacSChristoph Hellwig 	ia64_send_ipi(cpumask_first(cpu_online_mask), IA64_CMCP_VECTOR,
15197d7f9848SSrivatsa S. Bhat 							IA64_IPI_DM_INT, 0);
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /*
15231da177e4SLinus Torvalds  *  ia64_mca_cpe_int_caller
15241da177e4SLinus Torvalds  *
15251da177e4SLinus Torvalds  * 	Triggered by sw interrupt from CPE polling routine.  Calls
15261da177e4SLinus Torvalds  * 	real interrupt handler and either triggers a sw interrupt
15271da177e4SLinus Torvalds  * 	on the next cpu or does cleanup at the end.
15281da177e4SLinus Torvalds  *
15291da177e4SLinus Torvalds  * Inputs
15301da177e4SLinus Torvalds  *	interrupt number
15311da177e4SLinus Torvalds  *	client data arg ptr
15321da177e4SLinus Torvalds  * Outputs
15331da177e4SLinus Torvalds  * 	handled
15341da177e4SLinus Torvalds  */
15351da177e4SLinus Torvalds static irqreturn_t
15367d12e780SDavid Howells ia64_mca_cpe_int_caller(int cpe_irq, void *arg)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	static int start_count = -1;
15391da177e4SLinus Torvalds 	static int poll_time = MIN_CPE_POLL_INTERVAL;
15401da177e4SLinus Torvalds 	unsigned int cpuid;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	cpuid = smp_processor_id();
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/* If first cpu, update count */
15451da177e4SLinus Torvalds 	if (start_count == -1)
15461da177e4SLinus Torvalds 		start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CPE);
15471da177e4SLinus Torvalds 
15487d12e780SDavid Howells 	ia64_mca_cpe_int_handler(cpe_irq, arg);
15491da177e4SLinus Torvalds 
15505dd3c994SRusty Russell 	cpuid = cpumask_next(cpuid+1, cpu_online_mask);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (cpuid < NR_CPUS) {
155305933aacSChristoph Hellwig 		ia64_send_ipi(cpuid, IA64_CPEP_VECTOR, IA64_IPI_DM_INT, 0);
15541da177e4SLinus Torvalds 	} else {
15551da177e4SLinus Torvalds 		/*
15561da177e4SLinus Torvalds 		 * If a log was recorded, increase our polling frequency,
15571da177e4SLinus Torvalds 		 * otherwise, backoff or return to interrupt mode.
15581da177e4SLinus Torvalds 		 */
15591da177e4SLinus Torvalds 		if (start_count != IA64_LOG_COUNT(SAL_INFO_TYPE_CPE)) {
15601da177e4SLinus Torvalds 			poll_time = max(MIN_CPE_POLL_INTERVAL, poll_time / 2);
15611da177e4SLinus Torvalds 		} else if (cpe_vector < 0) {
15621da177e4SLinus Torvalds 			poll_time = min(MAX_CPE_POLL_INTERVAL, poll_time * 2);
15631da177e4SLinus Torvalds 		} else {
15641da177e4SLinus Torvalds 			poll_time = MIN_CPE_POLL_INTERVAL;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 			printk(KERN_WARNING "Returning to interrupt driven CPE handler\n");
15671da177e4SLinus Torvalds 			enable_irq(local_vector_to_irq(IA64_CPE_VECTOR));
15681da177e4SLinus Torvalds 			cpe_poll_enabled = 0;
15691da177e4SLinus Torvalds 		}
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		if (cpe_poll_enabled)
15721da177e4SLinus Torvalds 			mod_timer(&cpe_poll_timer, jiffies + poll_time);
15731da177e4SLinus Torvalds 		start_count = -1;
15741da177e4SLinus Torvalds 	}
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	return IRQ_HANDLED;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds /*
15801da177e4SLinus Torvalds  *  ia64_mca_cpe_poll
15811da177e4SLinus Torvalds  *
15821da177e4SLinus Torvalds  *	Poll for Corrected Platform Errors (CPEs), trigger interrupt
15831da177e4SLinus Torvalds  *	on first cpu, from there it will trickle through all the cpus.
15841da177e4SLinus Torvalds  *
15851da177e4SLinus Torvalds  * Inputs   :   dummy(unused)
15861da177e4SLinus Torvalds  * Outputs  :   None
15871da177e4SLinus Torvalds  *
15881da177e4SLinus Torvalds  */
15891da177e4SLinus Torvalds static void
15902c513d4fSKees Cook ia64_mca_cpe_poll (struct timer_list *unused)
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	/* Trigger a CPE interrupt cascade  */
159305933aacSChristoph Hellwig 	ia64_send_ipi(cpumask_first(cpu_online_mask), IA64_CPEP_VECTOR,
15947d7f9848SSrivatsa S. Bhat 							IA64_IPI_DM_INT, 0);
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15979138d581SKeith Owens static int
15989138d581SKeith Owens default_monarch_init_process(struct notifier_block *self, unsigned long val, void *data)
15999138d581SKeith Owens {
16009138d581SKeith Owens 	int c;
16019138d581SKeith Owens 	struct task_struct *g, *t;
16029138d581SKeith Owens 	if (val != DIE_INIT_MONARCH_PROCESS)
16039138d581SKeith Owens 		return NOTIFY_DONE;
1604311f594dSJay Lan #ifdef CONFIG_KEXEC
1605311f594dSJay Lan 	if (atomic_read(&kdump_in_progress))
1606311f594dSJay Lan 		return NOTIFY_DONE;
1607311f594dSJay Lan #endif
160843ed3bafSHidetoshi Seto 
160943ed3bafSHidetoshi Seto 	/*
161043ed3bafSHidetoshi Seto 	 * FIXME: mlogbuf will brim over with INIT stack dumps.
161143ed3bafSHidetoshi Seto 	 * To enable show_stack from INIT, we use oops_in_progress which should
161243ed3bafSHidetoshi Seto 	 * be used in real oops. This would cause something wrong after INIT.
161343ed3bafSHidetoshi Seto 	 */
161443ed3bafSHidetoshi Seto 	BREAK_LOGLEVEL(console_loglevel);
161543ed3bafSHidetoshi Seto 	ia64_mlogbuf_dump_from_init();
161643ed3bafSHidetoshi Seto 
16179138d581SKeith Owens 	printk(KERN_ERR "Processes interrupted by INIT -");
16189138d581SKeith Owens 	for_each_online_cpu(c) {
16199138d581SKeith Owens 		struct ia64_sal_os_state *s;
16209138d581SKeith Owens 		t = __va(__per_cpu_mca[c] + IA64_MCA_CPU_INIT_STACK_OFFSET);
16219138d581SKeith Owens 		s = (struct ia64_sal_os_state *)((char *)t + MCA_SOS_OFFSET);
16229138d581SKeith Owens 		g = s->prev_task;
16239138d581SKeith Owens 		if (g) {
16249138d581SKeith Owens 			if (g->pid)
16259138d581SKeith Owens 				printk(" %d", g->pid);
16269138d581SKeith Owens 			else
16279138d581SKeith Owens 				printk(" %d (cpu %d task 0x%p)", g->pid, task_cpu(g), g);
16289138d581SKeith Owens 		}
16299138d581SKeith Owens 	}
16309138d581SKeith Owens 	printk("\n\n");
16319138d581SKeith Owens 	if (read_trylock(&tasklist_lock)) {
16329138d581SKeith Owens 		do_each_thread (g, t) {
16339138d581SKeith Owens 			printk("\nBacktrace of pid %d (%s)\n", t->pid, t->comm);
1634*9cb8f069SDmitry Safonov 			show_stack(t, NULL, KERN_DEFAULT);
16359138d581SKeith Owens 		} while_each_thread (g, t);
16369138d581SKeith Owens 		read_unlock(&tasklist_lock);
16379138d581SKeith Owens 	}
163843ed3bafSHidetoshi Seto 	/* FIXME: This will not restore zapped printk locks. */
163943ed3bafSHidetoshi Seto 	RESTORE_LOGLEVEL(console_loglevel);
16409138d581SKeith Owens 	return NOTIFY_DONE;
16419138d581SKeith Owens }
16429138d581SKeith Owens 
16431da177e4SLinus Torvalds /*
16441da177e4SLinus Torvalds  * C portion of the OS INIT handler
16451da177e4SLinus Torvalds  *
16467f613c7dSKeith Owens  * Called from ia64_os_init_dispatch
16471da177e4SLinus Torvalds  *
16487f613c7dSKeith Owens  * Inputs: pointer to pt_regs where processor info was saved.  SAL/OS state for
16497f613c7dSKeith Owens  * this event.  This code is used for both monarch and slave INIT events, see
16507f613c7dSKeith Owens  * sos->monarch.
16511da177e4SLinus Torvalds  *
16527f613c7dSKeith Owens  * All INIT events switch to the INIT stack and change the previous process to
16537f613c7dSKeith Owens  * blocked status.  If one of the INIT events is the monarch then we are
16547f613c7dSKeith Owens  * probably processing the nmi button/command.  Use the monarch cpu to dump all
16557f613c7dSKeith Owens  * the processes.  The slave INIT events all spin until the monarch cpu
16567f613c7dSKeith Owens  * returns.  We can also get INIT slave events for MCA, in which case the MCA
16577f613c7dSKeith Owens  * process is the monarch.
16581da177e4SLinus Torvalds  */
16591da177e4SLinus Torvalds 
16607f613c7dSKeith Owens void
16617f613c7dSKeith Owens ia64_init_handler(struct pt_regs *regs, struct switch_stack *sw,
16627f613c7dSKeith Owens 		  struct ia64_sal_os_state *sos)
16637f613c7dSKeith Owens {
16647f613c7dSKeith Owens 	static atomic_t slaves;
16657f613c7dSKeith Owens 	static atomic_t monarchs;
166636c8b586SIngo Molnar 	struct task_struct *previous_current;
16679138d581SKeith Owens 	int cpu = smp_processor_id();
1668958b166cSKeith Owens 	struct ia64_mca_notify_die nd =
1669958b166cSKeith Owens 		{ .sos = sos, .monarch_cpu = &monarch_cpu };
16707f613c7dSKeith Owens 
16714fa2f0e6SHidetoshi Seto 	NOTIFY_INIT(DIE_INIT_ENTER, regs, (long)&nd, 0);
1672958b166cSKeith Owens 
167343ed3bafSHidetoshi Seto 	mprintk(KERN_INFO "Entered OS INIT handler. PSP=%lx cpu=%d monarch=%ld\n",
16747f613c7dSKeith Owens 		sos->proc_state_param, cpu, sos->monarch);
16757f613c7dSKeith Owens 	salinfo_log_wakeup(SAL_INFO_TYPE_INIT, NULL, 0, 0);
16767f613c7dSKeith Owens 
16777f613c7dSKeith Owens 	previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "INIT");
16787f613c7dSKeith Owens 	sos->os_status = IA64_INIT_RESUME;
16797f613c7dSKeith Owens 
16807f613c7dSKeith Owens 	/* FIXME: Workaround for broken proms that drive all INIT events as
16817f613c7dSKeith Owens 	 * slaves.  The last slave that enters is promoted to be a monarch.
16827f613c7dSKeith Owens 	 * Remove this code in September 2006, that gives platforms a year to
16837f613c7dSKeith Owens 	 * fix their proms and get their customers updated.
16847f613c7dSKeith Owens 	 */
16857f613c7dSKeith Owens 	if (!sos->monarch && atomic_add_return(1, &slaves) == num_online_cpus()) {
168643ed3bafSHidetoshi Seto 		mprintk(KERN_WARNING "%s: Promoting cpu %d to monarch.\n",
1687d4ed8084SHarvey Harrison 		        __func__, cpu);
16887f613c7dSKeith Owens 		atomic_dec(&slaves);
16897f613c7dSKeith Owens 		sos->monarch = 1;
16907f613c7dSKeith Owens 	}
16917f613c7dSKeith Owens 
16927f613c7dSKeith Owens 	/* FIXME: Workaround for broken proms that drive all INIT events as
16937f613c7dSKeith Owens 	 * monarchs.  Second and subsequent monarchs are demoted to slaves.
16947f613c7dSKeith Owens 	 * Remove this code in September 2006, that gives platforms a year to
16957f613c7dSKeith Owens 	 * fix their proms and get their customers updated.
16967f613c7dSKeith Owens 	 */
16977f613c7dSKeith Owens 	if (sos->monarch && atomic_add_return(1, &monarchs) > 1) {
169843ed3bafSHidetoshi Seto 		mprintk(KERN_WARNING "%s: Demoting cpu %d to slave.\n",
1699d4ed8084SHarvey Harrison 			       __func__, cpu);
17007f613c7dSKeith Owens 		atomic_dec(&monarchs);
17017f613c7dSKeith Owens 		sos->monarch = 0;
17027f613c7dSKeith Owens 	}
17037f613c7dSKeith Owens 
17047f613c7dSKeith Owens 	if (!sos->monarch) {
17057f613c7dSKeith Owens 		ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_INIT;
17060cced40eSHidetoshi Seto 
17070cced40eSHidetoshi Seto #ifdef CONFIG_KEXEC
17080cced40eSHidetoshi Seto 		while (monarch_cpu == -1 && !atomic_read(&kdump_in_progress))
17090cced40eSHidetoshi Seto 			udelay(1000);
17100cced40eSHidetoshi Seto #else
17117f613c7dSKeith Owens 		while (monarch_cpu == -1)
17127f613c7dSKeith Owens 			cpu_relax();	/* spin until monarch enters */
17130cced40eSHidetoshi Seto #endif
17144fa2f0e6SHidetoshi Seto 
17154fa2f0e6SHidetoshi Seto 		NOTIFY_INIT(DIE_INIT_SLAVE_ENTER, regs, (long)&nd, 1);
17164fa2f0e6SHidetoshi Seto 		NOTIFY_INIT(DIE_INIT_SLAVE_PROCESS, regs, (long)&nd, 1);
17174fa2f0e6SHidetoshi Seto 
17180cced40eSHidetoshi Seto #ifdef CONFIG_KEXEC
17190cced40eSHidetoshi Seto 		while (monarch_cpu != -1 && !atomic_read(&kdump_in_progress))
17200cced40eSHidetoshi Seto 			udelay(1000);
17210cced40eSHidetoshi Seto #else
17227f613c7dSKeith Owens 		while (monarch_cpu != -1)
17237f613c7dSKeith Owens 			cpu_relax();	/* spin until monarch leaves */
17240cced40eSHidetoshi Seto #endif
17254fa2f0e6SHidetoshi Seto 
17264fa2f0e6SHidetoshi Seto 		NOTIFY_INIT(DIE_INIT_SLAVE_LEAVE, regs, (long)&nd, 1);
17274fa2f0e6SHidetoshi Seto 
172843ed3bafSHidetoshi Seto 		mprintk("Slave on cpu %d returning to normal service.\n", cpu);
1729a458ae2eSPeter Zijlstra 		ia64_set_curr_task(cpu, previous_current);
17307f613c7dSKeith Owens 		ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
17317f613c7dSKeith Owens 		atomic_dec(&slaves);
17327f613c7dSKeith Owens 		return;
17337f613c7dSKeith Owens 	}
17347f613c7dSKeith Owens 
17357f613c7dSKeith Owens 	monarch_cpu = cpu;
17364fa2f0e6SHidetoshi Seto 	NOTIFY_INIT(DIE_INIT_MONARCH_ENTER, regs, (long)&nd, 1);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/*
17397f613c7dSKeith Owens 	 * Wait for a bit.  On some machines (e.g., HP's zx2000 and zx6000, INIT can be
17407f613c7dSKeith Owens 	 * generated via the BMC's command-line interface, but since the console is on the
17417f613c7dSKeith Owens 	 * same serial line, the user will need some time to switch out of the BMC before
17427f613c7dSKeith Owens 	 * the dump begins.
17431da177e4SLinus Torvalds 	 */
174443ed3bafSHidetoshi Seto 	mprintk("Delaying for 5 seconds...\n");
17457f613c7dSKeith Owens 	udelay(5*1000000);
1746356a5c1cSKeith Owens 	ia64_wait_for_slaves(cpu, "INIT");
17479138d581SKeith Owens 	/* If nobody intercepts DIE_INIT_MONARCH_PROCESS then we drop through
17489138d581SKeith Owens 	 * to default_monarch_init_process() above and just print all the
17499138d581SKeith Owens 	 * tasks.
17509138d581SKeith Owens 	 */
17514fa2f0e6SHidetoshi Seto 	NOTIFY_INIT(DIE_INIT_MONARCH_PROCESS, regs, (long)&nd, 1);
17524fa2f0e6SHidetoshi Seto 	NOTIFY_INIT(DIE_INIT_MONARCH_LEAVE, regs, (long)&nd, 1);
17534fa2f0e6SHidetoshi Seto 
175443ed3bafSHidetoshi Seto 	mprintk("\nINIT dump complete.  Monarch on cpu %d returning to normal service.\n", cpu);
17557f613c7dSKeith Owens 	atomic_dec(&monarchs);
1756a458ae2eSPeter Zijlstra 	ia64_set_curr_task(cpu, previous_current);
17577f613c7dSKeith Owens 	monarch_cpu = -1;
17587f613c7dSKeith Owens 	return;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds static int __init
17621da177e4SLinus Torvalds ia64_mca_disable_cpe_polling(char *str)
17631da177e4SLinus Torvalds {
17641da177e4SLinus Torvalds 	cpe_poll_enabled = 0;
17651da177e4SLinus Torvalds 	return 1;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds __setup("disable_cpe_poll", ia64_mca_disable_cpe_polling);
17691da177e4SLinus Torvalds 
17707f613c7dSKeith Owens /* Minimal format of the MCA/INIT stacks.  The pseudo processes that run on
17717f613c7dSKeith Owens  * these stacks can never sleep, they cannot return from the kernel to user
17727f613c7dSKeith Owens  * space, they do not appear in a normal ps listing.  So there is no need to
17737f613c7dSKeith Owens  * format most of the fields.
17747f613c7dSKeith Owens  */
17757f613c7dSKeith Owens 
1776ccce9bb8SPaul Gortmaker static void
17777f613c7dSKeith Owens format_mca_init_stack(void *mca_data, unsigned long offset,
17787f613c7dSKeith Owens 		const char *type, int cpu)
17797f613c7dSKeith Owens {
17807f613c7dSKeith Owens 	struct task_struct *p = (struct task_struct *)((char *)mca_data + offset);
17817f613c7dSKeith Owens 	struct thread_info *ti;
17827f613c7dSKeith Owens 	memset(p, 0, KERNEL_STACK_SIZE);
1783ab03591dSAl Viro 	ti = task_thread_info(p);
17847f613c7dSKeith Owens 	ti->flags = _TIF_MCA_INIT;
17857f613c7dSKeith Owens 	ti->preempt_count = 1;
17867f613c7dSKeith Owens 	ti->task = p;
17877f613c7dSKeith Owens 	ti->cpu = cpu;
1788f7e4217bSRoman Zippel 	p->stack = ti;
17897f613c7dSKeith Owens 	p->state = TASK_UNINTERRUPTIBLE;
17903bd37062SSebastian Andrzej Siewior 	cpumask_set_cpu(cpu, &p->cpus_mask);
17917f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->tasks);
17927f613c7dSKeith Owens 	p->parent = p->real_parent = p->group_leader = p;
17937f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->children);
17947f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->sibling);
17957f613c7dSKeith Owens 	strncpy(p->comm, type, sizeof(p->comm)-1);
17967f613c7dSKeith Owens }
17977f613c7dSKeith Owens 
1798056e6d89SSam Ravnborg /* Caller prevents this from being called after init */
1799bd721ea7SFabian Frederick static void * __ref mca_bootmem(void)
1800056e6d89SSam Ravnborg {
18019415673eSMike Rapoport 	return memblock_alloc(sizeof(struct ia64_mca_cpu), KERNEL_STACK_SIZE);
1802056e6d89SSam Ravnborg }
1803056e6d89SSam Ravnborg 
1804056e6d89SSam Ravnborg /* Do per-CPU MCA-related initialization.  */
1805ccce9bb8SPaul Gortmaker void
18061da177e4SLinus Torvalds ia64_mca_cpu_init(void *cpu_data)
18071da177e4SLinus Torvalds {
18081da177e4SLinus Torvalds 	void *pal_vaddr;
1809785285fcSRuss Anderson 	void *data;
1810785285fcSRuss Anderson 	long sz = sizeof(struct ia64_mca_cpu);
1811785285fcSRuss Anderson 	int cpu = smp_processor_id();
1812ff741906SAshok Raj 	static int first_time = 1;
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	/*
1815785285fcSRuss Anderson 	 * Structure will already be allocated if cpu has been online,
1816785285fcSRuss Anderson 	 * then offlined.
18171da177e4SLinus Torvalds 	 */
1818785285fcSRuss Anderson 	if (__per_cpu_mca[cpu]) {
1819785285fcSRuss Anderson 		data = __va(__per_cpu_mca[cpu]);
1820785285fcSRuss Anderson 	} else {
1821785285fcSRuss Anderson 		if (first_time) {
1822785285fcSRuss Anderson 			data = mca_bootmem();
1823785285fcSRuss Anderson 			first_time = 0;
1824785285fcSRuss Anderson 		} else
1825c1d036c4SJeff Mahoney 			data = (void *)__get_free_pages(GFP_KERNEL,
1826c1d036c4SJeff Mahoney 							get_order(sz));
1827785285fcSRuss Anderson 		if (!data)
1828785285fcSRuss Anderson 			panic("Could not allocate MCA memory for cpu %d\n",
1829785285fcSRuss Anderson 					cpu);
1830785285fcSRuss Anderson 	}
1831785285fcSRuss Anderson 	format_mca_init_stack(data, offsetof(struct ia64_mca_cpu, mca_stack),
1832785285fcSRuss Anderson 		"MCA", cpu);
1833785285fcSRuss Anderson 	format_mca_init_stack(data, offsetof(struct ia64_mca_cpu, init_stack),
1834785285fcSRuss Anderson 		"INIT", cpu);
18356065a244SChristoph Lameter 	__this_cpu_write(ia64_mca_data, (__per_cpu_mca[cpu] = __pa(data)));
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	/*
18381da177e4SLinus Torvalds 	 * Stash away a copy of the PTE needed to map the per-CPU page.
18391da177e4SLinus Torvalds 	 * We may need it during MCA recovery.
18401da177e4SLinus Torvalds 	 */
18416065a244SChristoph Lameter 	__this_cpu_write(ia64_mca_per_cpu_pte,
18426065a244SChristoph Lameter 		pte_val(mk_pte_phys(__pa(cpu_data), PAGE_KERNEL)));
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	/*
18451da177e4SLinus Torvalds 	 * Also, stash away a copy of the PAL address and the PTE
18461da177e4SLinus Torvalds 	 * needed to map it.
18471da177e4SLinus Torvalds 	 */
18481da177e4SLinus Torvalds 	pal_vaddr = efi_get_pal_addr();
18491da177e4SLinus Torvalds 	if (!pal_vaddr)
18501da177e4SLinus Torvalds 		return;
18516065a244SChristoph Lameter 	__this_cpu_write(ia64_mca_pal_base,
18526065a244SChristoph Lameter 		GRANULEROUNDDOWN((unsigned long) pal_vaddr));
18536065a244SChristoph Lameter 	__this_cpu_write(ia64_mca_pal_pte, pte_val(mk_pte_phys(__pa(pal_vaddr),
18546065a244SChristoph Lameter 							      PAGE_KERNEL)));
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18576b8d6422SSebastian Andrzej Siewior static int ia64_mca_cpu_online(unsigned int cpu)
1858ed5d4026SHidetoshi Seto {
1859ed5d4026SHidetoshi Seto 	unsigned long flags;
1860ed5d4026SHidetoshi Seto 
1861ed5d4026SHidetoshi Seto 	local_irq_save(flags);
1862ed5d4026SHidetoshi Seto 	if (!cmc_polling_enabled)
1863ed5d4026SHidetoshi Seto 		ia64_mca_cmc_vector_enable(NULL);
1864ed5d4026SHidetoshi Seto 	local_irq_restore(flags);
18656b8d6422SSebastian Andrzej Siewior 	return 0;
1866ed5d4026SHidetoshi Seto }
1867ed5d4026SHidetoshi Seto 
18681da177e4SLinus Torvalds /*
18691da177e4SLinus Torvalds  * ia64_mca_init
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  *  Do all the system level mca specific initialization.
18721da177e4SLinus Torvalds  *
18731da177e4SLinus Torvalds  *	1. Register spinloop and wakeup request interrupt vectors
18741da177e4SLinus Torvalds  *
18751da177e4SLinus Torvalds  *	2. Register OS_MCA handler entry point
18761da177e4SLinus Torvalds  *
18771da177e4SLinus Torvalds  *	3. Register OS_INIT handler entry point
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  *  4. Initialize MCA/CMC/INIT related log buffers maintained by the OS.
18801da177e4SLinus Torvalds  *
18811da177e4SLinus Torvalds  *  Note that this initialization is done very early before some kernel
18821da177e4SLinus Torvalds  *  services are available.
18831da177e4SLinus Torvalds  *
18841da177e4SLinus Torvalds  *  Inputs  :   None
18851da177e4SLinus Torvalds  *
18861da177e4SLinus Torvalds  *  Outputs :   None
18871da177e4SLinus Torvalds  */
18881da177e4SLinus Torvalds void __init
18891da177e4SLinus Torvalds ia64_mca_init(void)
18901da177e4SLinus Torvalds {
18917f613c7dSKeith Owens 	ia64_fptr_t *init_hldlr_ptr_monarch = (ia64_fptr_t *)ia64_os_init_dispatch_monarch;
18927f613c7dSKeith Owens 	ia64_fptr_t *init_hldlr_ptr_slave = (ia64_fptr_t *)ia64_os_init_dispatch_slave;
18931da177e4SLinus Torvalds 	ia64_fptr_t *mca_hldlr_ptr = (ia64_fptr_t *)ia64_os_mca_dispatch;
18941da177e4SLinus Torvalds 	int i;
1895e088a4adSMatthew Wilcox 	long rc;
18961da177e4SLinus Torvalds 	struct ia64_sal_retval isrv;
1897e088a4adSMatthew Wilcox 	unsigned long timeout = IA64_MCA_RENDEZ_TIMEOUT; /* platform specific */
18989138d581SKeith Owens 	static struct notifier_block default_init_monarch_nb = {
18999138d581SKeith Owens 		.notifier_call = default_monarch_init_process,
19009138d581SKeith Owens 		.priority = 0/* we need to notified last */
19019138d581SKeith Owens 	};
19021da177e4SLinus Torvalds 
1903d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: begin\n", __func__);
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	/* Clear the Rendez checkin flag for all cpus */
19061da177e4SLinus Torvalds 	for(i = 0 ; i < NR_CPUS; i++)
19071da177e4SLinus Torvalds 		ia64_mc_info.imi_rendez_checkin[i] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	/*
19101da177e4SLinus Torvalds 	 * Register the rendezvous spinloop and wakeup mechanism with SAL
19111da177e4SLinus Torvalds 	 */
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	/* Register the rendezvous interrupt vector with SAL */
19141da177e4SLinus Torvalds 	while (1) {
19151da177e4SLinus Torvalds 		isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_INT,
19161da177e4SLinus Torvalds 					      SAL_MC_PARAM_MECHANISM_INT,
19171da177e4SLinus Torvalds 					      IA64_MCA_RENDEZ_VECTOR,
19181da177e4SLinus Torvalds 					      timeout,
19191da177e4SLinus Torvalds 					      SAL_MC_PARAM_RZ_ALWAYS);
19201da177e4SLinus Torvalds 		rc = isrv.status;
19211da177e4SLinus Torvalds 		if (rc == 0)
19221da177e4SLinus Torvalds 			break;
19231da177e4SLinus Torvalds 		if (rc == -2) {
19241da177e4SLinus Torvalds 			printk(KERN_INFO "Increasing MCA rendezvous timeout from "
19251da177e4SLinus Torvalds 				"%ld to %ld milliseconds\n", timeout, isrv.v0);
19261da177e4SLinus Torvalds 			timeout = isrv.v0;
19274fa2f0e6SHidetoshi Seto 			NOTIFY_MCA(DIE_MCA_NEW_TIMEOUT, NULL, timeout, 0);
19281da177e4SLinus Torvalds 			continue;
19291da177e4SLinus Torvalds 		}
19301da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register rendezvous interrupt "
19311da177e4SLinus Torvalds 		       "with SAL (status %ld)\n", rc);
19321da177e4SLinus Torvalds 		return;
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/* Register the wakeup interrupt vector with SAL */
19361da177e4SLinus Torvalds 	isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_WAKEUP,
19371da177e4SLinus Torvalds 				      SAL_MC_PARAM_MECHANISM_INT,
19381da177e4SLinus Torvalds 				      IA64_MCA_WAKEUP_VECTOR,
19391da177e4SLinus Torvalds 				      0, 0);
19401da177e4SLinus Torvalds 	rc = isrv.status;
19411da177e4SLinus Torvalds 	if (rc) {
19421da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register wakeup interrupt with SAL "
19431da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
19441da177e4SLinus Torvalds 		return;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
1947d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: registered MCA rendezvous spinloop and wakeup mech.\n", __func__);
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	ia64_mc_info.imi_mca_handler        = ia64_tpa(mca_hldlr_ptr->fp);
19501da177e4SLinus Torvalds 	/*
19511da177e4SLinus Torvalds 	 * XXX - disable SAL checksum by setting size to 0; should be
19521da177e4SLinus Torvalds 	 *	ia64_tpa(ia64_os_mca_dispatch_end) - ia64_tpa(ia64_os_mca_dispatch);
19531da177e4SLinus Torvalds 	 */
19541da177e4SLinus Torvalds 	ia64_mc_info.imi_mca_handler_size	= 0;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	/* Register the os mca handler with SAL */
19571da177e4SLinus Torvalds 	if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_MCA,
19581da177e4SLinus Torvalds 				       ia64_mc_info.imi_mca_handler,
19591da177e4SLinus Torvalds 				       ia64_tpa(mca_hldlr_ptr->gp),
19601da177e4SLinus Torvalds 				       ia64_mc_info.imi_mca_handler_size,
19611da177e4SLinus Torvalds 				       0, 0, 0)))
19621da177e4SLinus Torvalds 	{
19631da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register OS MCA handler with SAL "
19641da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
19651da177e4SLinus Torvalds 		return;
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 
1968d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: registered OS MCA handler with SAL at 0x%lx, gp = 0x%lx\n", __func__,
19691da177e4SLinus Torvalds 		       ia64_mc_info.imi_mca_handler, ia64_tpa(mca_hldlr_ptr->gp));
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	/*
19721da177e4SLinus Torvalds 	 * XXX - disable SAL checksum by setting size to 0, should be
19731da177e4SLinus Torvalds 	 * size of the actual init handler in mca_asm.S.
19741da177e4SLinus Torvalds 	 */
19757f613c7dSKeith Owens 	ia64_mc_info.imi_monarch_init_handler		= ia64_tpa(init_hldlr_ptr_monarch->fp);
19761da177e4SLinus Torvalds 	ia64_mc_info.imi_monarch_init_handler_size	= 0;
19777f613c7dSKeith Owens 	ia64_mc_info.imi_slave_init_handler		= ia64_tpa(init_hldlr_ptr_slave->fp);
19781da177e4SLinus Torvalds 	ia64_mc_info.imi_slave_init_handler_size	= 0;
19791da177e4SLinus Torvalds 
1980d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: OS INIT handler at %lx\n", __func__,
19811da177e4SLinus Torvalds 		       ia64_mc_info.imi_monarch_init_handler);
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	/* Register the os init handler with SAL */
19841da177e4SLinus Torvalds 	if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_INIT,
19851da177e4SLinus Torvalds 				       ia64_mc_info.imi_monarch_init_handler,
19861da177e4SLinus Torvalds 				       ia64_tpa(ia64_getreg(_IA64_REG_GP)),
19871da177e4SLinus Torvalds 				       ia64_mc_info.imi_monarch_init_handler_size,
19881da177e4SLinus Torvalds 				       ia64_mc_info.imi_slave_init_handler,
19891da177e4SLinus Torvalds 				       ia64_tpa(ia64_getreg(_IA64_REG_GP)),
19901da177e4SLinus Torvalds 				       ia64_mc_info.imi_slave_init_handler_size)))
19911da177e4SLinus Torvalds 	{
19921da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register m/s INIT handlers with SAL "
19931da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
19941da177e4SLinus Torvalds 		return;
19951da177e4SLinus Torvalds 	}
19969138d581SKeith Owens 	if (register_die_notifier(&default_init_monarch_nb)) {
19979138d581SKeith Owens 		printk(KERN_ERR "Failed to register default monarch INIT process\n");
19989138d581SKeith Owens 		return;
19999138d581SKeith Owens 	}
20001da177e4SLinus Torvalds 
2001d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: registered OS INIT handler with SAL\n", __func__);
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	/* Initialize the areas set aside by the OS to buffer the
20041da177e4SLinus Torvalds 	 * platform/processor error states for MCA/INIT/CMC
20051da177e4SLinus Torvalds 	 * handling.
20061da177e4SLinus Torvalds 	 */
20071da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_MCA);
20081da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_INIT);
20091da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_CMC);
20101da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_CPE);
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	mca_init = 1;
20131da177e4SLinus Torvalds 	printk(KERN_INFO "MCA related initialization done\n");
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 
2017c75f2aa1STony Luck /*
2018d303e9e9STony Luck  * These pieces cannot be done in ia64_mca_init() because it is called before
2019d303e9e9STony Luck  * early_irq_init() which would wipe out our percpu irq registrations. But we
2020d303e9e9STony Luck  * cannot leave them until ia64_mca_late_init() because by then all the other
2021d303e9e9STony Luck  * processors have been brought online and have set their own CMC vectors to
2022d303e9e9STony Luck  * point at a non-existant action. Called from arch_early_irq_init().
2023d303e9e9STony Luck  */
2024d303e9e9STony Luck void __init ia64_mca_irq_init(void)
2025d303e9e9STony Luck {
2026d303e9e9STony Luck 	/*
2027c75f2aa1STony Luck 	 *  Configure the CMCI/P vector and handler. Interrupts for CMC are
2028c75f2aa1STony Luck 	 *  per-processor, so AP CMC interrupts are setup in smp_callin() (smpboot.c).
2029c75f2aa1STony Luck 	 */
203090341cd8Safzal mohammed 	register_percpu_irq(IA64_CMC_VECTOR, ia64_mca_cmc_int_handler, 0,
203190341cd8Safzal mohammed 			    "cmc_hndlr");
203290341cd8Safzal mohammed 	register_percpu_irq(IA64_CMCP_VECTOR, ia64_mca_cmc_int_caller, 0,
203390341cd8Safzal mohammed 			    "cmc_poll");
2034c75f2aa1STony Luck 	ia64_mca_cmc_vector_setup();       /* Setup vector on BSP */
2035c75f2aa1STony Luck 
2036c75f2aa1STony Luck 	/* Setup the MCA rendezvous interrupt vector */
203790341cd8Safzal mohammed 	register_percpu_irq(IA64_MCA_RENDEZ_VECTOR, ia64_mca_rendez_int_handler,
203890341cd8Safzal mohammed 			    0, "mca_rdzv");
2039c75f2aa1STony Luck 
2040c75f2aa1STony Luck 	/* Setup the MCA wakeup interrupt vector */
204190341cd8Safzal mohammed 	register_percpu_irq(IA64_MCA_WAKEUP_VECTOR, ia64_mca_wakeup_int_handler,
204290341cd8Safzal mohammed 			    0, "mca_wkup");
2043c75f2aa1STony Luck 
2044c75f2aa1STony Luck 	/* Setup the CPEI/P handler */
204590341cd8Safzal mohammed 	register_percpu_irq(IA64_CPEP_VECTOR, ia64_mca_cpe_int_caller, 0,
204690341cd8Safzal mohammed 			    "cpe_poll");
2047d303e9e9STony Luck }
2048d303e9e9STony Luck 
2049d303e9e9STony Luck /*
2050d303e9e9STony Luck  * ia64_mca_late_init
2051d303e9e9STony Luck  *
2052d303e9e9STony Luck  *	Opportunity to setup things that require initialization later
2053d303e9e9STony Luck  *	than ia64_mca_init.  Setup a timer to poll for CPEs if the
2054d303e9e9STony Luck  *	platform doesn't support an interrupt driven mechanism.
2055d303e9e9STony Luck  *
2056d303e9e9STony Luck  *  Inputs  :   None
2057d303e9e9STony Luck  *  Outputs :   Status
2058d303e9e9STony Luck  */
2059d303e9e9STony Luck static int __init
2060d303e9e9STony Luck ia64_mca_late_init(void)
2061d303e9e9STony Luck {
2062d303e9e9STony Luck 	if (!mca_init)
2063d303e9e9STony Luck 		return 0;
2064c75f2aa1STony Luck 
20651da177e4SLinus Torvalds 	/* Setup the CMCI/P vector and handler */
20662c513d4fSKees Cook 	timer_setup(&cmc_poll_timer, ia64_mca_cmc_poll, 0);
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	/* Unmask/enable the vector */
20691da177e4SLinus Torvalds 	cmc_polling_enabled = 0;
20706b8d6422SSebastian Andrzej Siewior 	cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ia64/mca:online",
20716b8d6422SSebastian Andrzej Siewior 			  ia64_mca_cpu_online, NULL);
2072d4ed8084SHarvey Harrison 	IA64_MCA_DEBUG("%s: CMCI/P setup and enabled.\n", __func__);
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	/* Setup the CPEI/P vector and handler */
2075bb68c12bSRuss Anderson 	cpe_vector = acpi_request_vector(ACPI_INTERRUPT_CPEI);
20762c513d4fSKees Cook 	timer_setup(&cpe_poll_timer, ia64_mca_cpe_poll, 0);
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	{
20791da177e4SLinus Torvalds 		unsigned int irq;
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 		if (cpe_vector >= 0) {
20821da177e4SLinus Torvalds 			/* If platform supports CPEI, enable the irq. */
2083a1287476SRuss Anderson 			irq = local_vector_to_irq(cpe_vector);
2084a1287476SRuss Anderson 			if (irq > 0) {
20851da177e4SLinus Torvalds 				cpe_poll_enabled = 0;
2086a2178334SThomas Gleixner 				irq_set_status_flags(irq, IRQ_PER_CPU);
208790341cd8Safzal mohammed 				if (request_irq(irq, ia64_mca_cpe_int_handler,
208890341cd8Safzal mohammed 						0, "cpe_hndlr", NULL))
208990341cd8Safzal mohammed 					pr_err("Failed to register cpe_hndlr interrupt\n");
2090ff741906SAshok Raj 				ia64_cpe_irq = irq;
20911da177e4SLinus Torvalds 				ia64_mca_register_cpev(cpe_vector);
2092a1287476SRuss Anderson 				IA64_MCA_DEBUG("%s: CPEI/P setup and enabled.\n",
2093d4ed8084SHarvey Harrison 					__func__);
2094a1287476SRuss Anderson 				return 0;
2095a1287476SRuss Anderson 			}
2096a1287476SRuss Anderson 			printk(KERN_ERR "%s: Failed to find irq for CPE "
2097a1287476SRuss Anderson 					"interrupt handler, vector %d\n",
2098d4ed8084SHarvey Harrison 					__func__, cpe_vector);
2099a1287476SRuss Anderson 		}
21001da177e4SLinus Torvalds 		/* If platform doesn't support CPEI, get the timer going. */
21011da177e4SLinus Torvalds 		if (cpe_poll_enabled) {
21021da177e4SLinus Torvalds 			ia64_mca_cpe_poll(0UL);
2103d4ed8084SHarvey Harrison 			IA64_MCA_DEBUG("%s: CPEP setup and enabled.\n", __func__);
21041da177e4SLinus Torvalds 		}
21051da177e4SLinus Torvalds 	}
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	return 0;
21081da177e4SLinus Torvalds }
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds device_initcall(ia64_mca_late_init);
2111