xref: /openbmc/linux/arch/ia64/kernel/mca.c (revision 4668f0cd0abd41a908ec7aa292e91fd158e0c8f8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * File:	mca.c
31da177e4SLinus Torvalds  * Purpose:	Generic MCA handling layer
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Updated for latest kernel
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.
101da177e4SLinus Torvalds  * Copyright (C) Matt Domsch (Matt_Domsch@dell.com)
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (C) 2002 Intel
131da177e4SLinus Torvalds  * Copyright (C) Jenna Hall (jenna.s.hall@intel.com)
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Copyright (C) 2001 Intel
161da177e4SLinus Torvalds  * Copyright (C) Fred Lewis (frederick.v.lewis@intel.com)
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Copyright (C) 2000 Intel
191da177e4SLinus Torvalds  * Copyright (C) Chuck Fleckenstein (cfleck@co.intel.com)
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Copyright (C) 1999, 2004 Silicon Graphics, Inc.
221da177e4SLinus Torvalds  * Copyright (C) Vijay Chander(vijay@engr.sgi.com)
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * 03/04/15 D. Mosberger Added INIT backtrace support.
251da177e4SLinus Torvalds  * 02/03/25 M. Domsch	GUID cleanups
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * 02/01/04 J. Hall	Aligned MCA stack to 16 bytes, added platform vs. CPU
281da177e4SLinus Torvalds  *			error flag, set SAL default return values, changed
291da177e4SLinus Torvalds  *			error record structure to linked list, added init call
301da177e4SLinus Torvalds  *			to sal_get_state_info_size().
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 01/01/03 F. Lewis    Added setup of CMCI and CPEI IRQs, logging of corrected
331da177e4SLinus Torvalds  *                      platform errors, completed code for logging of
341da177e4SLinus Torvalds  *                      corrected & uncorrected machine check errors, and
351da177e4SLinus Torvalds  *                      updated for conformance with Nov. 2000 revision of the
361da177e4SLinus Torvalds  *                      SAL 3.0 spec.
371da177e4SLinus Torvalds  * 00/03/29 C. Fleckenstein  Fixed PAL/SAL update issues, began MCA bug fixes, logging issues,
381da177e4SLinus Torvalds  *                           added min save state dump, added INIT handler.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * 2003-12-08 Keith Owens <kaos@sgi.com>
411da177e4SLinus Torvalds  *            smp_call_function() must not be called from interrupt context (can
421da177e4SLinus Torvalds  *            deadlock on tasklist_lock).  Use keventd to call smp_call_function().
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  * 2004-02-01 Keith Owens <kaos@sgi.com>
451da177e4SLinus Torvalds  *            Avoid deadlock when using printk() for MCA and INIT records.
461da177e4SLinus Torvalds  *            Delete all record printing code, moved to salinfo_decode in user space.
471da177e4SLinus Torvalds  *            Mark variables and functions static where possible.
481da177e4SLinus Torvalds  *            Delete dead variables and functions.
491da177e4SLinus Torvalds  *            Reorder to remove the need for forward declarations and to consolidate
501da177e4SLinus Torvalds  *            related code.
517f613c7dSKeith Owens  *
527f613c7dSKeith Owens  * 2005-08-12 Keith Owens <kaos@sgi.com>
537f613c7dSKeith Owens  *	      Convert MCA/INIT handlers to use per event stacks and SAL/OS state.
549138d581SKeith Owens  *
559138d581SKeith Owens  * 2005-10-07 Keith Owens <kaos@sgi.com>
569138d581SKeith Owens  *	      Add notify_die() hooks.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds #include <linux/config.h>
591da177e4SLinus Torvalds #include <linux/types.h>
601da177e4SLinus Torvalds #include <linux/init.h>
611da177e4SLinus Torvalds #include <linux/sched.h>
621da177e4SLinus Torvalds #include <linux/interrupt.h>
631da177e4SLinus Torvalds #include <linux/irq.h>
641da177e4SLinus Torvalds #include <linux/smp_lock.h>
651da177e4SLinus Torvalds #include <linux/bootmem.h>
661da177e4SLinus Torvalds #include <linux/acpi.h>
671da177e4SLinus Torvalds #include <linux/timer.h>
681da177e4SLinus Torvalds #include <linux/module.h>
691da177e4SLinus Torvalds #include <linux/kernel.h>
701da177e4SLinus Torvalds #include <linux/smp.h>
711da177e4SLinus Torvalds #include <linux/workqueue.h>
72*4668f0cdSAkinobu Mita #include <linux/cpumask.h>
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #include <asm/delay.h>
759138d581SKeith Owens #include <asm/kdebug.h>
761da177e4SLinus Torvalds #include <asm/machvec.h>
771da177e4SLinus Torvalds #include <asm/meminit.h>
781da177e4SLinus Torvalds #include <asm/page.h>
791da177e4SLinus Torvalds #include <asm/ptrace.h>
801da177e4SLinus Torvalds #include <asm/system.h>
811da177e4SLinus Torvalds #include <asm/sal.h>
821da177e4SLinus Torvalds #include <asm/mca.h>
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #include <asm/irq.h>
851da177e4SLinus Torvalds #include <asm/hw_irq.h>
861da177e4SLinus Torvalds 
87d2a28ad9SRuss Anderson #include "mca_drv.h"
887f613c7dSKeith Owens #include "entry.h"
897f613c7dSKeith Owens 
901da177e4SLinus Torvalds #if defined(IA64_MCA_DEBUG_INFO)
911da177e4SLinus Torvalds # define IA64_MCA_DEBUG(fmt...)	printk(fmt)
921da177e4SLinus Torvalds #else
931da177e4SLinus Torvalds # define IA64_MCA_DEBUG(fmt...)
941da177e4SLinus Torvalds #endif
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Used by mca_asm.S */
977f613c7dSKeith Owens u32				ia64_mca_serialize;
981da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_data); /* == __per_cpu_mca[smp_processor_id()] */
991da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_per_cpu_pte); /* PTE to map per-CPU area */
1001da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_pal_pte);	    /* PTE to map PAL code */
1011da177e4SLinus Torvalds DEFINE_PER_CPU(u64, ia64_mca_pal_base);    /* vaddr PAL code granule */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds unsigned long __per_cpu_mca[NR_CPUS];
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* In mca_asm.S */
1067f613c7dSKeith Owens extern void			ia64_os_init_dispatch_monarch (void);
1077f613c7dSKeith Owens extern void			ia64_os_init_dispatch_slave (void);
1087f613c7dSKeith Owens 
1097f613c7dSKeith Owens static int monarch_cpu = -1;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static ia64_mc_info_t		ia64_mc_info;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define MAX_CPE_POLL_INTERVAL (15*60*HZ) /* 15 minutes */
1141da177e4SLinus Torvalds #define MIN_CPE_POLL_INTERVAL (2*60*HZ)  /* 2 minutes */
1151da177e4SLinus Torvalds #define CMC_POLL_INTERVAL     (1*60*HZ)  /* 1 minute */
1161da177e4SLinus Torvalds #define CPE_HISTORY_LENGTH    5
1171da177e4SLinus Torvalds #define CMC_HISTORY_LENGTH    5
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static struct timer_list cpe_poll_timer;
1201da177e4SLinus Torvalds static struct timer_list cmc_poll_timer;
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * This variable tells whether we are currently in polling mode.
1231da177e4SLinus Torvalds  * Start with this in the wrong state so we won't play w/ timers
1241da177e4SLinus Torvalds  * before the system is ready.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds static int cmc_polling_enabled = 1;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * Clearing this variable prevents CPE polling from getting activated
1301da177e4SLinus Torvalds  * in mca_late_init.  Use it if your system doesn't provide a CPEI,
1311da177e4SLinus Torvalds  * but encounters problems retrieving CPE logs.  This should only be
1321da177e4SLinus Torvalds  * necessary for debugging.
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds static int cpe_poll_enabled = 1;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds extern void salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe);
1371da177e4SLinus Torvalds 
1380881fc8dSChen, Kenneth W static int mca_init __initdata;
1391da177e4SLinus Torvalds 
1409138d581SKeith Owens 
1419138d581SKeith Owens static void inline
1429138d581SKeith Owens ia64_mca_spin(const char *func)
1439138d581SKeith Owens {
1449138d581SKeith Owens 	printk(KERN_EMERG "%s: spinning here, not returning to SAL\n", func);
1459138d581SKeith Owens 	while (1)
1469138d581SKeith Owens 		cpu_relax();
1479138d581SKeith Owens }
1481da177e4SLinus Torvalds /*
1491da177e4SLinus Torvalds  * IA64_MCA log support
1501da177e4SLinus Torvalds  */
1511da177e4SLinus Torvalds #define IA64_MAX_LOGS		2	/* Double-buffering for nested MCAs */
1521da177e4SLinus Torvalds #define IA64_MAX_LOG_TYPES      4   /* MCA, INIT, CMC, CPE */
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds typedef struct ia64_state_log_s
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	spinlock_t	isl_lock;
1571da177e4SLinus Torvalds 	int		isl_index;
1581da177e4SLinus Torvalds 	unsigned long	isl_count;
1591da177e4SLinus Torvalds 	ia64_err_rec_t  *isl_log[IA64_MAX_LOGS]; /* need space to store header + error log */
1601da177e4SLinus Torvalds } ia64_state_log_t;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static ia64_state_log_t ia64_state_log[IA64_MAX_LOG_TYPES];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define IA64_LOG_ALLOCATE(it, size) \
1651da177e4SLinus Torvalds 	{ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)] = \
1661da177e4SLinus Torvalds 		(ia64_err_rec_t *)alloc_bootmem(size); \
1671da177e4SLinus Torvalds 	ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)] = \
1681da177e4SLinus Torvalds 		(ia64_err_rec_t *)alloc_bootmem(size);}
1691da177e4SLinus Torvalds #define IA64_LOG_LOCK_INIT(it) spin_lock_init(&ia64_state_log[it].isl_lock)
1701da177e4SLinus Torvalds #define IA64_LOG_LOCK(it)      spin_lock_irqsave(&ia64_state_log[it].isl_lock, s)
1711da177e4SLinus Torvalds #define IA64_LOG_UNLOCK(it)    spin_unlock_irqrestore(&ia64_state_log[it].isl_lock,s)
1721da177e4SLinus Torvalds #define IA64_LOG_NEXT_INDEX(it)    ia64_state_log[it].isl_index
1731da177e4SLinus Torvalds #define IA64_LOG_CURR_INDEX(it)    1 - ia64_state_log[it].isl_index
1741da177e4SLinus Torvalds #define IA64_LOG_INDEX_INC(it) \
1751da177e4SLinus Torvalds     {ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index; \
1761da177e4SLinus Torvalds     ia64_state_log[it].isl_count++;}
1771da177e4SLinus Torvalds #define IA64_LOG_INDEX_DEC(it) \
1781da177e4SLinus Torvalds     ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index
1791da177e4SLinus Torvalds #define IA64_LOG_NEXT_BUFFER(it)   (void *)((ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)]))
1801da177e4SLinus Torvalds #define IA64_LOG_CURR_BUFFER(it)   (void *)((ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)]))
1811da177e4SLinus Torvalds #define IA64_LOG_COUNT(it)         ia64_state_log[it].isl_count
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds  * ia64_log_init
1851da177e4SLinus Torvalds  *	Reset the OS ia64 log buffer
1861da177e4SLinus Torvalds  * Inputs   :   info_type   (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
1871da177e4SLinus Torvalds  * Outputs	:	None
1881da177e4SLinus Torvalds  */
1890881fc8dSChen, Kenneth W static void __init
1901da177e4SLinus Torvalds ia64_log_init(int sal_info_type)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	u64	max_size = 0;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	IA64_LOG_NEXT_INDEX(sal_info_type) = 0;
1951da177e4SLinus Torvalds 	IA64_LOG_LOCK_INIT(sal_info_type);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	// SAL will tell us the maximum size of any error record of this type
1981da177e4SLinus Torvalds 	max_size = ia64_sal_get_state_info_size(sal_info_type);
1991da177e4SLinus Torvalds 	if (!max_size)
2001da177e4SLinus Torvalds 		/* alloc_bootmem() doesn't like zero-sized allocations! */
2011da177e4SLinus Torvalds 		return;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	// set up OS data structures to hold error info
2041da177e4SLinus Torvalds 	IA64_LOG_ALLOCATE(sal_info_type, max_size);
2051da177e4SLinus Torvalds 	memset(IA64_LOG_CURR_BUFFER(sal_info_type), 0, max_size);
2061da177e4SLinus Torvalds 	memset(IA64_LOG_NEXT_BUFFER(sal_info_type), 0, max_size);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
2101da177e4SLinus Torvalds  * ia64_log_get
2111da177e4SLinus Torvalds  *
2121da177e4SLinus Torvalds  *	Get the current MCA log from SAL and copy it into the OS log buffer.
2131da177e4SLinus Torvalds  *
2141da177e4SLinus Torvalds  *  Inputs  :   info_type   (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
2151da177e4SLinus Torvalds  *              irq_safe    whether you can use printk at this point
2161da177e4SLinus Torvalds  *  Outputs :   size        (total record length)
2171da177e4SLinus Torvalds  *              *buffer     (ptr to error record)
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds static u64
2211da177e4SLinus Torvalds ia64_log_get(int sal_info_type, u8 **buffer, int irq_safe)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	sal_log_record_header_t     *log_buffer;
2241da177e4SLinus Torvalds 	u64                         total_len = 0;
2251da177e4SLinus Torvalds 	int                         s;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	IA64_LOG_LOCK(sal_info_type);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* Get the process state information */
2301da177e4SLinus Torvalds 	log_buffer = IA64_LOG_NEXT_BUFFER(sal_info_type);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	total_len = ia64_sal_get_state_info(sal_info_type, (u64 *)log_buffer);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (total_len) {
2351da177e4SLinus Torvalds 		IA64_LOG_INDEX_INC(sal_info_type);
2361da177e4SLinus Torvalds 		IA64_LOG_UNLOCK(sal_info_type);
2371da177e4SLinus Torvalds 		if (irq_safe) {
2381da177e4SLinus Torvalds 			IA64_MCA_DEBUG("%s: SAL error record type %d retrieved. "
2391da177e4SLinus Torvalds 				       "Record length = %ld\n", __FUNCTION__, sal_info_type, total_len);
2401da177e4SLinus Torvalds 		}
2411da177e4SLinus Torvalds 		*buffer = (u8 *) log_buffer;
2421da177e4SLinus Torvalds 		return total_len;
2431da177e4SLinus Torvalds 	} else {
2441da177e4SLinus Torvalds 		IA64_LOG_UNLOCK(sal_info_type);
2451da177e4SLinus Torvalds 		return 0;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /*
2501da177e4SLinus Torvalds  *  ia64_mca_log_sal_error_record
2511da177e4SLinus Torvalds  *
2521da177e4SLinus Torvalds  *  This function retrieves a specified error record type from SAL
2531da177e4SLinus Torvalds  *  and wakes up any processes waiting for error records.
2541da177e4SLinus Torvalds  *
2557f613c7dSKeith Owens  *  Inputs  :   sal_info_type   (Type of error record MCA/CMC/CPE)
2567f613c7dSKeith Owens  *              FIXME: remove MCA and irq_safe.
2571da177e4SLinus Torvalds  */
2581da177e4SLinus Torvalds static void
2591da177e4SLinus Torvalds ia64_mca_log_sal_error_record(int sal_info_type)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	u8 *buffer;
2621da177e4SLinus Torvalds 	sal_log_record_header_t *rh;
2631da177e4SLinus Torvalds 	u64 size;
2647f613c7dSKeith Owens 	int irq_safe = sal_info_type != SAL_INFO_TYPE_MCA;
2651da177e4SLinus Torvalds #ifdef IA64_MCA_DEBUG_INFO
2661da177e4SLinus Torvalds 	static const char * const rec_name[] = { "MCA", "INIT", "CMC", "CPE" };
2671da177e4SLinus Torvalds #endif
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	size = ia64_log_get(sal_info_type, &buffer, irq_safe);
2701da177e4SLinus Torvalds 	if (!size)
2711da177e4SLinus Torvalds 		return;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	salinfo_log_wakeup(sal_info_type, buffer, size, irq_safe);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	if (irq_safe)
2761da177e4SLinus Torvalds 		IA64_MCA_DEBUG("CPU %d: SAL log contains %s error record\n",
2771da177e4SLinus Torvalds 			smp_processor_id(),
2781da177e4SLinus Torvalds 			sal_info_type < ARRAY_SIZE(rec_name) ? rec_name[sal_info_type] : "UNKNOWN");
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Clear logs from corrected errors in case there's no user-level logger */
2811da177e4SLinus Torvalds 	rh = (sal_log_record_header_t *)buffer;
2821da177e4SLinus Torvalds 	if (rh->severity == sal_log_severity_corrected)
2831da177e4SLinus Torvalds 		ia64_sal_clear_state_info(sal_info_type);
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
286d2a28ad9SRuss Anderson /*
287d2a28ad9SRuss Anderson  * search_mca_table
288d2a28ad9SRuss Anderson  *  See if the MCA surfaced in an instruction range
289d2a28ad9SRuss Anderson  *  that has been tagged as recoverable.
290d2a28ad9SRuss Anderson  *
291d2a28ad9SRuss Anderson  *  Inputs
292d2a28ad9SRuss Anderson  *	first	First address range to check
293d2a28ad9SRuss Anderson  *	last	Last address range to check
294d2a28ad9SRuss Anderson  *	ip	Instruction pointer, address we are looking for
295d2a28ad9SRuss Anderson  *
296d2a28ad9SRuss Anderson  * Return value:
297d2a28ad9SRuss Anderson  *      1 on Success (in the table)/ 0 on Failure (not in the  table)
298d2a28ad9SRuss Anderson  */
299d2a28ad9SRuss Anderson int
300d2a28ad9SRuss Anderson search_mca_table (const struct mca_table_entry *first,
301d2a28ad9SRuss Anderson                 const struct mca_table_entry *last,
302d2a28ad9SRuss Anderson                 unsigned long ip)
303d2a28ad9SRuss Anderson {
304d2a28ad9SRuss Anderson         const struct mca_table_entry *curr;
305d2a28ad9SRuss Anderson         u64 curr_start, curr_end;
306d2a28ad9SRuss Anderson 
307d2a28ad9SRuss Anderson         curr = first;
308d2a28ad9SRuss Anderson         while (curr <= last) {
309d2a28ad9SRuss Anderson                 curr_start = (u64) &curr->start_addr + curr->start_addr;
310d2a28ad9SRuss Anderson                 curr_end = (u64) &curr->end_addr + curr->end_addr;
311d2a28ad9SRuss Anderson 
312d2a28ad9SRuss Anderson                 if ((ip >= curr_start) && (ip <= curr_end)) {
313d2a28ad9SRuss Anderson                         return 1;
314d2a28ad9SRuss Anderson                 }
315d2a28ad9SRuss Anderson                 curr++;
316d2a28ad9SRuss Anderson         }
317d2a28ad9SRuss Anderson         return 0;
318d2a28ad9SRuss Anderson }
319d2a28ad9SRuss Anderson 
320d2a28ad9SRuss Anderson /* Given an address, look for it in the mca tables. */
321d2a28ad9SRuss Anderson int mca_recover_range(unsigned long addr)
322d2a28ad9SRuss Anderson {
323d2a28ad9SRuss Anderson 	extern struct mca_table_entry __start___mca_table[];
324d2a28ad9SRuss Anderson 	extern struct mca_table_entry __stop___mca_table[];
325d2a28ad9SRuss Anderson 
326d2a28ad9SRuss Anderson 	return search_mca_table(__start___mca_table, __stop___mca_table-1, addr);
327d2a28ad9SRuss Anderson }
328d2a28ad9SRuss Anderson EXPORT_SYMBOL_GPL(mca_recover_range);
329d2a28ad9SRuss Anderson 
3301da177e4SLinus Torvalds #ifdef CONFIG_ACPI
3311da177e4SLinus Torvalds 
33255e59c51SAshok Raj int cpe_vector = -1;
333ff741906SAshok Raj int ia64_cpe_irq = -1;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static irqreturn_t
3361da177e4SLinus Torvalds ia64_mca_cpe_int_handler (int cpe_irq, void *arg, struct pt_regs *ptregs)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	static unsigned long	cpe_history[CPE_HISTORY_LENGTH];
3391da177e4SLinus Torvalds 	static int		index;
3401da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(cpe_history_lock);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
3431da177e4SLinus Torvalds 		       __FUNCTION__, cpe_irq, smp_processor_id());
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	/* SAL spec states this should run w/ interrupts enabled */
3461da177e4SLinus Torvalds 	local_irq_enable();
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	/* Get the CPE error record and log it */
3491da177e4SLinus Torvalds 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CPE);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	spin_lock(&cpe_history_lock);
3521da177e4SLinus Torvalds 	if (!cpe_poll_enabled && cpe_vector >= 0) {
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 		int i, count = 1; /* we know 1 happened now */
3551da177e4SLinus Torvalds 		unsigned long now = jiffies;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		for (i = 0; i < CPE_HISTORY_LENGTH; i++) {
3581da177e4SLinus Torvalds 			if (now - cpe_history[i] <= HZ)
3591da177e4SLinus Torvalds 				count++;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		IA64_MCA_DEBUG(KERN_INFO "CPE threshold %d/%d\n", count, CPE_HISTORY_LENGTH);
3631da177e4SLinus Torvalds 		if (count >= CPE_HISTORY_LENGTH) {
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 			cpe_poll_enabled = 1;
3661da177e4SLinus Torvalds 			spin_unlock(&cpe_history_lock);
3671da177e4SLinus Torvalds 			disable_irq_nosync(local_vector_to_irq(IA64_CPE_VECTOR));
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 			/*
3701da177e4SLinus Torvalds 			 * Corrected errors will still be corrected, but
3711da177e4SLinus Torvalds 			 * make sure there's a log somewhere that indicates
3721da177e4SLinus Torvalds 			 * something is generating more than we can handle.
3731da177e4SLinus Torvalds 			 */
3741da177e4SLinus Torvalds 			printk(KERN_WARNING "WARNING: Switching to polling CPE handler; error records may be lost\n");
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 			mod_timer(&cpe_poll_timer, jiffies + MIN_CPE_POLL_INTERVAL);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 			/* lock already released, get out now */
3791da177e4SLinus Torvalds 			return IRQ_HANDLED;
3801da177e4SLinus Torvalds 		} else {
3811da177e4SLinus Torvalds 			cpe_history[index++] = now;
3821da177e4SLinus Torvalds 			if (index == CPE_HISTORY_LENGTH)
3831da177e4SLinus Torvalds 				index = 0;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	spin_unlock(&cpe_history_lock);
3871da177e4SLinus Torvalds 	return IRQ_HANDLED;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds #endif /* CONFIG_ACPI */
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds #ifdef CONFIG_ACPI
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  * ia64_mca_register_cpev
3951da177e4SLinus Torvalds  *
3961da177e4SLinus Torvalds  *  Register the corrected platform error vector with SAL.
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  *  Inputs
3991da177e4SLinus Torvalds  *      cpev        Corrected Platform Error Vector number
4001da177e4SLinus Torvalds  *
4011da177e4SLinus Torvalds  *  Outputs
4021da177e4SLinus Torvalds  *      None
4031da177e4SLinus Torvalds  */
4040881fc8dSChen, Kenneth W static void __init
4051da177e4SLinus Torvalds ia64_mca_register_cpev (int cpev)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	/* Register the CPE interrupt vector with SAL */
4081da177e4SLinus Torvalds 	struct ia64_sal_retval isrv;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_CPE_INT, SAL_MC_PARAM_MECHANISM_INT, cpev, 0, 0);
4111da177e4SLinus Torvalds 	if (isrv.status) {
4121da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register Corrected Platform "
4131da177e4SLinus Torvalds 		       "Error interrupt vector with SAL (status %ld)\n", isrv.status);
4141da177e4SLinus Torvalds 		return;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: corrected platform error "
4181da177e4SLinus Torvalds 		       "vector %#x registered\n", __FUNCTION__, cpev);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds #endif /* CONFIG_ACPI */
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /*
4231da177e4SLinus Torvalds  * ia64_mca_cmc_vector_setup
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  *  Setup the corrected machine check vector register in the processor.
4261da177e4SLinus Torvalds  *  (The interrupt is masked on boot. ia64_mca_late_init unmask this.)
4271da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  * Inputs
4301da177e4SLinus Torvalds  *      None
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  * Outputs
4331da177e4SLinus Torvalds  *	None
4341da177e4SLinus Torvalds  */
4350881fc8dSChen, Kenneth W void __cpuinit
4361da177e4SLinus Torvalds ia64_mca_cmc_vector_setup (void)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	cmcv.cmcv_regval	= 0;
4411da177e4SLinus Torvalds 	cmcv.cmcv_mask		= 1;        /* Mask/disable interrupt at first */
4421da177e4SLinus Torvalds 	cmcv.cmcv_vector	= IA64_CMC_VECTOR;
4431da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CPU %d corrected "
4461da177e4SLinus Torvalds 		       "machine check vector %#x registered.\n",
4471da177e4SLinus Torvalds 		       __FUNCTION__, smp_processor_id(), IA64_CMC_VECTOR);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CPU %d CMCV = %#016lx\n",
4501da177e4SLinus Torvalds 		       __FUNCTION__, smp_processor_id(), ia64_getreg(_IA64_REG_CR_CMCV));
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds /*
4541da177e4SLinus Torvalds  * ia64_mca_cmc_vector_disable
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  *  Mask the corrected machine check vector register in the processor.
4571da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
4581da177e4SLinus Torvalds  *
4591da177e4SLinus Torvalds  * Inputs
4601da177e4SLinus Torvalds  *      dummy(unused)
4611da177e4SLinus Torvalds  *
4621da177e4SLinus Torvalds  * Outputs
4631da177e4SLinus Torvalds  *	None
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds static void
4661da177e4SLinus Torvalds ia64_mca_cmc_vector_disable (void *dummy)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	cmcv.cmcv_mask = 1; /* Mask/disable interrupt */
4731da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CPU %d corrected "
4761da177e4SLinus Torvalds 		       "machine check vector %#x disabled.\n",
4771da177e4SLinus Torvalds 		       __FUNCTION__, smp_processor_id(), cmcv.cmcv_vector);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /*
4811da177e4SLinus Torvalds  * ia64_mca_cmc_vector_enable
4821da177e4SLinus Torvalds  *
4831da177e4SLinus Torvalds  *  Unmask the corrected machine check vector register in the processor.
4841da177e4SLinus Torvalds  *  This function is invoked on a per-processor basis.
4851da177e4SLinus Torvalds  *
4861da177e4SLinus Torvalds  * Inputs
4871da177e4SLinus Torvalds  *      dummy(unused)
4881da177e4SLinus Torvalds  *
4891da177e4SLinus Torvalds  * Outputs
4901da177e4SLinus Torvalds  *	None
4911da177e4SLinus Torvalds  */
4921da177e4SLinus Torvalds static void
4931da177e4SLinus Torvalds ia64_mca_cmc_vector_enable (void *dummy)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	cmcv_reg_t	cmcv;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	cmcv.cmcv_mask = 0; /* Unmask/enable interrupt */
5001da177e4SLinus Torvalds 	ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CPU %d corrected "
5031da177e4SLinus Torvalds 		       "machine check vector %#x enabled.\n",
5041da177e4SLinus Torvalds 		       __FUNCTION__, smp_processor_id(), cmcv.cmcv_vector);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /*
5081da177e4SLinus Torvalds  * ia64_mca_cmc_vector_disable_keventd
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * Called via keventd (smp_call_function() is not safe in interrupt context) to
5111da177e4SLinus Torvalds  * disable the cmc interrupt vector.
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds static void
5141da177e4SLinus Torvalds ia64_mca_cmc_vector_disable_keventd(void *unused)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	on_each_cpu(ia64_mca_cmc_vector_disable, NULL, 1, 0);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /*
5201da177e4SLinus Torvalds  * ia64_mca_cmc_vector_enable_keventd
5211da177e4SLinus Torvalds  *
5221da177e4SLinus Torvalds  * Called via keventd (smp_call_function() is not safe in interrupt context) to
5231da177e4SLinus Torvalds  * enable the cmc interrupt vector.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds static void
5261da177e4SLinus Torvalds ia64_mca_cmc_vector_enable_keventd(void *unused)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	on_each_cpu(ia64_mca_cmc_vector_enable, NULL, 1, 0);
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds /*
5321da177e4SLinus Torvalds  * ia64_mca_wakeup
5331da177e4SLinus Torvalds  *
5341da177e4SLinus Torvalds  *	Send an inter-cpu interrupt to wake-up a particular cpu
5351da177e4SLinus Torvalds  *	and mark that cpu to be out of rendez.
5361da177e4SLinus Torvalds  *
5371da177e4SLinus Torvalds  *  Inputs  :   cpuid
5381da177e4SLinus Torvalds  *  Outputs :   None
5391da177e4SLinus Torvalds  */
5401da177e4SLinus Torvalds static void
5411da177e4SLinus Torvalds ia64_mca_wakeup(int cpu)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	platform_send_ipi(cpu, IA64_MCA_WAKEUP_VECTOR, IA64_IPI_DM_INT, 0);
5441da177e4SLinus Torvalds 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /*
5491da177e4SLinus Torvalds  * ia64_mca_wakeup_all
5501da177e4SLinus Torvalds  *
5511da177e4SLinus Torvalds  *	Wakeup all the cpus which have rendez'ed previously.
5521da177e4SLinus Torvalds  *
5531da177e4SLinus Torvalds  *  Inputs  :   None
5541da177e4SLinus Torvalds  *  Outputs :   None
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds static void
5571da177e4SLinus Torvalds ia64_mca_wakeup_all(void)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	int cpu;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/* Clear the Rendez checkin flag for all cpus */
562ddf6d0a0Shawkes@sgi.com 	for_each_online_cpu(cpu) {
5631da177e4SLinus Torvalds 		if (ia64_mc_info.imi_rendez_checkin[cpu] == IA64_MCA_RENDEZ_CHECKIN_DONE)
5641da177e4SLinus Torvalds 			ia64_mca_wakeup(cpu);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /*
5701da177e4SLinus Torvalds  * ia64_mca_rendez_interrupt_handler
5711da177e4SLinus Torvalds  *
5721da177e4SLinus Torvalds  *	This is handler used to put slave processors into spinloop
5731da177e4SLinus Torvalds  *	while the monarch processor does the mca handling and later
5741da177e4SLinus Torvalds  *	wake each slave up once the monarch is done.
5751da177e4SLinus Torvalds  *
5761da177e4SLinus Torvalds  *  Inputs  :   None
5771da177e4SLinus Torvalds  *  Outputs :   None
5781da177e4SLinus Torvalds  */
5791da177e4SLinus Torvalds static irqreturn_t
5809138d581SKeith Owens ia64_mca_rendez_int_handler(int rendez_irq, void *arg, struct pt_regs *regs)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	unsigned long flags;
5831da177e4SLinus Torvalds 	int cpu = smp_processor_id();
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/* Mask all interrupts */
5861da177e4SLinus Torvalds 	local_irq_save(flags);
5879138d581SKeith Owens 	if (notify_die(DIE_MCA_RENDZVOUS_ENTER, "MCA", regs, 0, 0, 0)
5889138d581SKeith Owens 			== NOTIFY_STOP)
5899138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_DONE;
5921da177e4SLinus Torvalds 	/* Register with the SAL monarch that the slave has
5931da177e4SLinus Torvalds 	 * reached SAL
5941da177e4SLinus Torvalds 	 */
5951da177e4SLinus Torvalds 	ia64_sal_mc_rendez();
5961da177e4SLinus Torvalds 
5979138d581SKeith Owens 	if (notify_die(DIE_MCA_RENDZVOUS_PROCESS, "MCA", regs, 0, 0, 0)
5989138d581SKeith Owens 			== NOTIFY_STOP)
5999138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
6009138d581SKeith Owens 
6017f613c7dSKeith Owens 	/* Wait for the monarch cpu to exit. */
6027f613c7dSKeith Owens 	while (monarch_cpu != -1)
6037f613c7dSKeith Owens 	       cpu_relax();	/* spin until monarch leaves */
6041da177e4SLinus Torvalds 
6059138d581SKeith Owens 	if (notify_die(DIE_MCA_RENDZVOUS_LEAVE, "MCA", regs, 0, 0, 0)
6069138d581SKeith Owens 			== NOTIFY_STOP)
6079138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
6089138d581SKeith Owens 
6091da177e4SLinus Torvalds 	/* Enable all interrupts */
6101da177e4SLinus Torvalds 	local_irq_restore(flags);
6111da177e4SLinus Torvalds 	return IRQ_HANDLED;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /*
6151da177e4SLinus Torvalds  * ia64_mca_wakeup_int_handler
6161da177e4SLinus Torvalds  *
6171da177e4SLinus Torvalds  *	The interrupt handler for processing the inter-cpu interrupt to the
6181da177e4SLinus Torvalds  *	slave cpu which was spinning in the rendez loop.
6191da177e4SLinus Torvalds  *	Since this spinning is done by turning off the interrupts and
6201da177e4SLinus Torvalds  *	polling on the wakeup-interrupt bit in the IRR, there is
6211da177e4SLinus Torvalds  *	nothing useful to be done in the handler.
6221da177e4SLinus Torvalds  *
6231da177e4SLinus Torvalds  *  Inputs  :   wakeup_irq  (Wakeup-interrupt bit)
6241da177e4SLinus Torvalds  *	arg		(Interrupt handler specific argument)
6251da177e4SLinus Torvalds  *	ptregs		(Exception frame at the time of the interrupt)
6261da177e4SLinus Torvalds  *  Outputs :   None
6271da177e4SLinus Torvalds  *
6281da177e4SLinus Torvalds  */
6291da177e4SLinus Torvalds static irqreturn_t
6301da177e4SLinus Torvalds ia64_mca_wakeup_int_handler(int wakeup_irq, void *arg, struct pt_regs *ptregs)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	return IRQ_HANDLED;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds /* Function pointer for extra MCA recovery */
6361da177e4SLinus Torvalds int (*ia64_mca_ucmc_extension)
6377f613c7dSKeith Owens 	(void*,struct ia64_sal_os_state*)
6381da177e4SLinus Torvalds 	= NULL;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds int
6417f613c7dSKeith Owens ia64_reg_MCA_extension(int (*fn)(void *, struct ia64_sal_os_state *))
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	if (ia64_mca_ucmc_extension)
6441da177e4SLinus Torvalds 		return 1;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	ia64_mca_ucmc_extension = fn;
6471da177e4SLinus Torvalds 	return 0;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds void
6511da177e4SLinus Torvalds ia64_unreg_MCA_extension(void)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	if (ia64_mca_ucmc_extension)
6541da177e4SLinus Torvalds 		ia64_mca_ucmc_extension = NULL;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds EXPORT_SYMBOL(ia64_reg_MCA_extension);
6581da177e4SLinus Torvalds EXPORT_SYMBOL(ia64_unreg_MCA_extension);
6591da177e4SLinus Torvalds 
6607f613c7dSKeith Owens 
6617f613c7dSKeith Owens static inline void
6627f613c7dSKeith Owens copy_reg(const u64 *fr, u64 fnat, u64 *tr, u64 *tnat)
6637f613c7dSKeith Owens {
6647f613c7dSKeith Owens 	u64 fslot, tslot, nat;
6657f613c7dSKeith Owens 	*tr = *fr;
6667f613c7dSKeith Owens 	fslot = ((unsigned long)fr >> 3) & 63;
6677f613c7dSKeith Owens 	tslot = ((unsigned long)tr >> 3) & 63;
6687f613c7dSKeith Owens 	*tnat &= ~(1UL << tslot);
6697f613c7dSKeith Owens 	nat = (fnat >> fslot) & 1;
6707f613c7dSKeith Owens 	*tnat |= (nat << tslot);
6717f613c7dSKeith Owens }
6727f613c7dSKeith Owens 
673e9ac054dSKeith Owens /* Change the comm field on the MCA/INT task to include the pid that
674e9ac054dSKeith Owens  * was interrupted, it makes for easier debugging.  If that pid was 0
675e9ac054dSKeith Owens  * (swapper or nested MCA/INIT) then use the start of the previous comm
676e9ac054dSKeith Owens  * field suffixed with its cpu.
677e9ac054dSKeith Owens  */
678e9ac054dSKeith Owens 
679e9ac054dSKeith Owens static void
680e9ac054dSKeith Owens ia64_mca_modify_comm(const task_t *previous_current)
681e9ac054dSKeith Owens {
682e9ac054dSKeith Owens 	char *p, comm[sizeof(current->comm)];
683e9ac054dSKeith Owens 	if (previous_current->pid)
684e9ac054dSKeith Owens 		snprintf(comm, sizeof(comm), "%s %d",
685e9ac054dSKeith Owens 			current->comm, previous_current->pid);
686e9ac054dSKeith Owens 	else {
687e9ac054dSKeith Owens 		int l;
688e9ac054dSKeith Owens 		if ((p = strchr(previous_current->comm, ' ')))
689e9ac054dSKeith Owens 			l = p - previous_current->comm;
690e9ac054dSKeith Owens 		else
691e9ac054dSKeith Owens 			l = strlen(previous_current->comm);
692e9ac054dSKeith Owens 		snprintf(comm, sizeof(comm), "%s %*s %d",
693e9ac054dSKeith Owens 			current->comm, l, previous_current->comm,
694e9ac054dSKeith Owens 			task_thread_info(previous_current)->cpu);
695e9ac054dSKeith Owens 	}
696e9ac054dSKeith Owens 	memcpy(current->comm, comm, sizeof(current->comm));
697e9ac054dSKeith Owens }
698e9ac054dSKeith Owens 
6997f613c7dSKeith Owens /* On entry to this routine, we are running on the per cpu stack, see
7007f613c7dSKeith Owens  * mca_asm.h.  The original stack has not been touched by this event.  Some of
7017f613c7dSKeith Owens  * the original stack's registers will be in the RBS on this stack.  This stack
7027f613c7dSKeith Owens  * also contains a partial pt_regs and switch_stack, the rest of the data is in
7037f613c7dSKeith Owens  * PAL minstate.
7047f613c7dSKeith Owens  *
7057f613c7dSKeith Owens  * The first thing to do is modify the original stack to look like a blocked
7067f613c7dSKeith Owens  * task so we can run backtrace on the original task.  Also mark the per cpu
7077f613c7dSKeith Owens  * stack as current to ensure that we use the correct task state, it also means
7087f613c7dSKeith Owens  * that we can do backtrace on the MCA/INIT handler code itself.
7097f613c7dSKeith Owens  */
7107f613c7dSKeith Owens 
7117f613c7dSKeith Owens static task_t *
7127f613c7dSKeith Owens ia64_mca_modify_original_stack(struct pt_regs *regs,
7137f613c7dSKeith Owens 		const struct switch_stack *sw,
7147f613c7dSKeith Owens 		struct ia64_sal_os_state *sos,
7157f613c7dSKeith Owens 		const char *type)
7167f613c7dSKeith Owens {
717e9ac054dSKeith Owens 	char *p;
7187f613c7dSKeith Owens 	ia64_va va;
7197f613c7dSKeith Owens 	extern char ia64_leave_kernel[];	/* Need asm address, not function descriptor */
7207f613c7dSKeith Owens 	const pal_min_state_area_t *ms = sos->pal_min_state;
7217f613c7dSKeith Owens 	task_t *previous_current;
7227f613c7dSKeith Owens 	struct pt_regs *old_regs;
7237f613c7dSKeith Owens 	struct switch_stack *old_sw;
7247f613c7dSKeith Owens 	unsigned size = sizeof(struct pt_regs) +
7257f613c7dSKeith Owens 			sizeof(struct switch_stack) + 16;
7267f613c7dSKeith Owens 	u64 *old_bspstore, *old_bsp;
7277f613c7dSKeith Owens 	u64 *new_bspstore, *new_bsp;
7287f613c7dSKeith Owens 	u64 old_unat, old_rnat, new_rnat, nat;
7297f613c7dSKeith Owens 	u64 slots, loadrs = regs->loadrs;
7307f613c7dSKeith Owens 	u64 r12 = ms->pmsa_gr[12-1], r13 = ms->pmsa_gr[13-1];
7317f613c7dSKeith Owens 	u64 ar_bspstore = regs->ar_bspstore;
7327f613c7dSKeith Owens 	u64 ar_bsp = regs->ar_bspstore + (loadrs >> 16);
7337f613c7dSKeith Owens 	const u64 *bank;
7347f613c7dSKeith Owens 	const char *msg;
7357f613c7dSKeith Owens 	int cpu = smp_processor_id();
7367f613c7dSKeith Owens 
7377f613c7dSKeith Owens 	previous_current = curr_task(cpu);
7387f613c7dSKeith Owens 	set_curr_task(cpu, current);
7397f613c7dSKeith Owens 	if ((p = strchr(current->comm, ' ')))
7407f613c7dSKeith Owens 		*p = '\0';
7417f613c7dSKeith Owens 
7427f613c7dSKeith Owens 	/* Best effort attempt to cope with MCA/INIT delivered while in
7437f613c7dSKeith Owens 	 * physical mode.
7447f613c7dSKeith Owens 	 */
7457f613c7dSKeith Owens 	regs->cr_ipsr = ms->pmsa_ipsr;
7467f613c7dSKeith Owens 	if (ia64_psr(regs)->dt == 0) {
7477f613c7dSKeith Owens 		va.l = r12;
7487f613c7dSKeith Owens 		if (va.f.reg == 0) {
7497f613c7dSKeith Owens 			va.f.reg = 7;
7507f613c7dSKeith Owens 			r12 = va.l;
7517f613c7dSKeith Owens 		}
7527f613c7dSKeith Owens 		va.l = r13;
7537f613c7dSKeith Owens 		if (va.f.reg == 0) {
7547f613c7dSKeith Owens 			va.f.reg = 7;
7557f613c7dSKeith Owens 			r13 = va.l;
7567f613c7dSKeith Owens 		}
7577f613c7dSKeith Owens 	}
7587f613c7dSKeith Owens 	if (ia64_psr(regs)->rt == 0) {
7597f613c7dSKeith Owens 		va.l = ar_bspstore;
7607f613c7dSKeith Owens 		if (va.f.reg == 0) {
7617f613c7dSKeith Owens 			va.f.reg = 7;
7627f613c7dSKeith Owens 			ar_bspstore = va.l;
7637f613c7dSKeith Owens 		}
7647f613c7dSKeith Owens 		va.l = ar_bsp;
7657f613c7dSKeith Owens 		if (va.f.reg == 0) {
7667f613c7dSKeith Owens 			va.f.reg = 7;
7677f613c7dSKeith Owens 			ar_bsp = va.l;
7687f613c7dSKeith Owens 		}
7697f613c7dSKeith Owens 	}
7707f613c7dSKeith Owens 
7717f613c7dSKeith Owens 	/* mca_asm.S ia64_old_stack() cannot assume that the dirty registers
7727f613c7dSKeith Owens 	 * have been copied to the old stack, the old stack may fail the
7737f613c7dSKeith Owens 	 * validation tests below.  So ia64_old_stack() must restore the dirty
7747f613c7dSKeith Owens 	 * registers from the new stack.  The old and new bspstore probably
7757f613c7dSKeith Owens 	 * have different alignments, so loadrs calculated on the old bsp
7767f613c7dSKeith Owens 	 * cannot be used to restore from the new bsp.  Calculate a suitable
7777f613c7dSKeith Owens 	 * loadrs for the new stack and save it in the new pt_regs, where
7787f613c7dSKeith Owens 	 * ia64_old_stack() can get it.
7797f613c7dSKeith Owens 	 */
7807f613c7dSKeith Owens 	old_bspstore = (u64 *)ar_bspstore;
7817f613c7dSKeith Owens 	old_bsp = (u64 *)ar_bsp;
7827f613c7dSKeith Owens 	slots = ia64_rse_num_regs(old_bspstore, old_bsp);
7837f613c7dSKeith Owens 	new_bspstore = (u64 *)((u64)current + IA64_RBS_OFFSET);
7847f613c7dSKeith Owens 	new_bsp = ia64_rse_skip_regs(new_bspstore, slots);
7857f613c7dSKeith Owens 	regs->loadrs = (new_bsp - new_bspstore) * 8 << 16;
7867f613c7dSKeith Owens 
7877f613c7dSKeith Owens 	/* Verify the previous stack state before we change it */
7887f613c7dSKeith Owens 	if (user_mode(regs)) {
7897f613c7dSKeith Owens 		msg = "occurred in user space";
790e9ac054dSKeith Owens 		/* previous_current is guaranteed to be valid when the task was
791e9ac054dSKeith Owens 		 * in user space, so ...
792e9ac054dSKeith Owens 		 */
793e9ac054dSKeith Owens 		ia64_mca_modify_comm(previous_current);
7947f613c7dSKeith Owens 		goto no_mod;
7957f613c7dSKeith Owens 	}
796d2a28ad9SRuss Anderson 
797d2a28ad9SRuss Anderson 	if (!mca_recover_range(ms->pmsa_iip)) {
7987f613c7dSKeith Owens 		if (r13 != sos->prev_IA64_KR_CURRENT) {
7997f613c7dSKeith Owens 			msg = "inconsistent previous current and r13";
8007f613c7dSKeith Owens 			goto no_mod;
8017f613c7dSKeith Owens 		}
8027f613c7dSKeith Owens 		if ((r12 - r13) >= KERNEL_STACK_SIZE) {
8037f613c7dSKeith Owens 			msg = "inconsistent r12 and r13";
8047f613c7dSKeith Owens 			goto no_mod;
8057f613c7dSKeith Owens 		}
8067f613c7dSKeith Owens 		if ((ar_bspstore - r13) >= KERNEL_STACK_SIZE) {
8077f613c7dSKeith Owens 			msg = "inconsistent ar.bspstore and r13";
8087f613c7dSKeith Owens 			goto no_mod;
8097f613c7dSKeith Owens 		}
8107f613c7dSKeith Owens 		va.p = old_bspstore;
8117f613c7dSKeith Owens 		if (va.f.reg < 5) {
8127f613c7dSKeith Owens 			msg = "old_bspstore is in the wrong region";
8137f613c7dSKeith Owens 			goto no_mod;
8147f613c7dSKeith Owens 		}
8157f613c7dSKeith Owens 		if ((ar_bsp - r13) >= KERNEL_STACK_SIZE) {
8167f613c7dSKeith Owens 			msg = "inconsistent ar.bsp and r13";
8177f613c7dSKeith Owens 			goto no_mod;
8187f613c7dSKeith Owens 		}
8197f613c7dSKeith Owens 		size += (ia64_rse_skip_regs(old_bspstore, slots) - old_bspstore) * 8;
8207f613c7dSKeith Owens 		if (ar_bspstore + size > r12) {
8217f613c7dSKeith Owens 			msg = "no room for blocked state";
8227f613c7dSKeith Owens 			goto no_mod;
8237f613c7dSKeith Owens 		}
824d2a28ad9SRuss Anderson 	}
8257f613c7dSKeith Owens 
826e9ac054dSKeith Owens 	ia64_mca_modify_comm(previous_current);
8277f613c7dSKeith Owens 
8287f613c7dSKeith Owens 	/* Make the original task look blocked.  First stack a struct pt_regs,
8297f613c7dSKeith Owens 	 * describing the state at the time of interrupt.  mca_asm.S built a
8307f613c7dSKeith Owens 	 * partial pt_regs, copy it and fill in the blanks using minstate.
8317f613c7dSKeith Owens 	 */
8327f613c7dSKeith Owens 	p = (char *)r12 - sizeof(*regs);
8337f613c7dSKeith Owens 	old_regs = (struct pt_regs *)p;
8347f613c7dSKeith Owens 	memcpy(old_regs, regs, sizeof(*regs));
8357f613c7dSKeith Owens 	/* If ipsr.ic then use pmsa_{iip,ipsr,ifs}, else use
8367f613c7dSKeith Owens 	 * pmsa_{xip,xpsr,xfs}
8377f613c7dSKeith Owens 	 */
8387f613c7dSKeith Owens 	if (ia64_psr(regs)->ic) {
8397f613c7dSKeith Owens 		old_regs->cr_iip = ms->pmsa_iip;
8407f613c7dSKeith Owens 		old_regs->cr_ipsr = ms->pmsa_ipsr;
8417f613c7dSKeith Owens 		old_regs->cr_ifs = ms->pmsa_ifs;
8427f613c7dSKeith Owens 	} else {
8437f613c7dSKeith Owens 		old_regs->cr_iip = ms->pmsa_xip;
8447f613c7dSKeith Owens 		old_regs->cr_ipsr = ms->pmsa_xpsr;
8457f613c7dSKeith Owens 		old_regs->cr_ifs = ms->pmsa_xfs;
8467f613c7dSKeith Owens 	}
8477f613c7dSKeith Owens 	old_regs->pr = ms->pmsa_pr;
8487f613c7dSKeith Owens 	old_regs->b0 = ms->pmsa_br0;
8497f613c7dSKeith Owens 	old_regs->loadrs = loadrs;
8507f613c7dSKeith Owens 	old_regs->ar_rsc = ms->pmsa_rsc;
8517f613c7dSKeith Owens 	old_unat = old_regs->ar_unat;
8527f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[1-1], ms->pmsa_nat_bits, &old_regs->r1, &old_unat);
8537f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[2-1], ms->pmsa_nat_bits, &old_regs->r2, &old_unat);
8547f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[3-1], ms->pmsa_nat_bits, &old_regs->r3, &old_unat);
8557f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[8-1], ms->pmsa_nat_bits, &old_regs->r8, &old_unat);
8567f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[9-1], ms->pmsa_nat_bits, &old_regs->r9, &old_unat);
8577f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[10-1], ms->pmsa_nat_bits, &old_regs->r10, &old_unat);
8587f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[11-1], ms->pmsa_nat_bits, &old_regs->r11, &old_unat);
8597f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[12-1], ms->pmsa_nat_bits, &old_regs->r12, &old_unat);
8607f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[13-1], ms->pmsa_nat_bits, &old_regs->r13, &old_unat);
8617f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[14-1], ms->pmsa_nat_bits, &old_regs->r14, &old_unat);
8627f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[15-1], ms->pmsa_nat_bits, &old_regs->r15, &old_unat);
8637f613c7dSKeith Owens 	if (ia64_psr(old_regs)->bn)
8647f613c7dSKeith Owens 		bank = ms->pmsa_bank1_gr;
8657f613c7dSKeith Owens 	else
8667f613c7dSKeith Owens 		bank = ms->pmsa_bank0_gr;
8677f613c7dSKeith Owens 	copy_reg(&bank[16-16], ms->pmsa_nat_bits, &old_regs->r16, &old_unat);
8687f613c7dSKeith Owens 	copy_reg(&bank[17-16], ms->pmsa_nat_bits, &old_regs->r17, &old_unat);
8697f613c7dSKeith Owens 	copy_reg(&bank[18-16], ms->pmsa_nat_bits, &old_regs->r18, &old_unat);
8707f613c7dSKeith Owens 	copy_reg(&bank[19-16], ms->pmsa_nat_bits, &old_regs->r19, &old_unat);
8717f613c7dSKeith Owens 	copy_reg(&bank[20-16], ms->pmsa_nat_bits, &old_regs->r20, &old_unat);
8727f613c7dSKeith Owens 	copy_reg(&bank[21-16], ms->pmsa_nat_bits, &old_regs->r21, &old_unat);
8737f613c7dSKeith Owens 	copy_reg(&bank[22-16], ms->pmsa_nat_bits, &old_regs->r22, &old_unat);
8747f613c7dSKeith Owens 	copy_reg(&bank[23-16], ms->pmsa_nat_bits, &old_regs->r23, &old_unat);
8757f613c7dSKeith Owens 	copy_reg(&bank[24-16], ms->pmsa_nat_bits, &old_regs->r24, &old_unat);
8767f613c7dSKeith Owens 	copy_reg(&bank[25-16], ms->pmsa_nat_bits, &old_regs->r25, &old_unat);
8777f613c7dSKeith Owens 	copy_reg(&bank[26-16], ms->pmsa_nat_bits, &old_regs->r26, &old_unat);
8787f613c7dSKeith Owens 	copy_reg(&bank[27-16], ms->pmsa_nat_bits, &old_regs->r27, &old_unat);
8797f613c7dSKeith Owens 	copy_reg(&bank[28-16], ms->pmsa_nat_bits, &old_regs->r28, &old_unat);
8807f613c7dSKeith Owens 	copy_reg(&bank[29-16], ms->pmsa_nat_bits, &old_regs->r29, &old_unat);
8817f613c7dSKeith Owens 	copy_reg(&bank[30-16], ms->pmsa_nat_bits, &old_regs->r30, &old_unat);
8827f613c7dSKeith Owens 	copy_reg(&bank[31-16], ms->pmsa_nat_bits, &old_regs->r31, &old_unat);
8837f613c7dSKeith Owens 
8847f613c7dSKeith Owens 	/* Next stack a struct switch_stack.  mca_asm.S built a partial
8857f613c7dSKeith Owens 	 * switch_stack, copy it and fill in the blanks using pt_regs and
8867f613c7dSKeith Owens 	 * minstate.
8877f613c7dSKeith Owens 	 *
8887f613c7dSKeith Owens 	 * In the synthesized switch_stack, b0 points to ia64_leave_kernel,
8897f613c7dSKeith Owens 	 * ar.pfs is set to 0.
8907f613c7dSKeith Owens 	 *
8917f613c7dSKeith Owens 	 * unwind.c::unw_unwind() does special processing for interrupt frames.
8927f613c7dSKeith Owens 	 * It checks if the PRED_NON_SYSCALL predicate is set, if the predicate
8937f613c7dSKeith Owens 	 * is clear then unw_unwind() does _not_ adjust bsp over pt_regs.  Not
8947f613c7dSKeith Owens 	 * that this is documented, of course.  Set PRED_NON_SYSCALL in the
8957f613c7dSKeith Owens 	 * switch_stack on the original stack so it will unwind correctly when
8967f613c7dSKeith Owens 	 * unwind.c reads pt_regs.
8977f613c7dSKeith Owens 	 *
8987f613c7dSKeith Owens 	 * thread.ksp is updated to point to the synthesized switch_stack.
8997f613c7dSKeith Owens 	 */
9007f613c7dSKeith Owens 	p -= sizeof(struct switch_stack);
9017f613c7dSKeith Owens 	old_sw = (struct switch_stack *)p;
9027f613c7dSKeith Owens 	memcpy(old_sw, sw, sizeof(*sw));
9037f613c7dSKeith Owens 	old_sw->caller_unat = old_unat;
9047f613c7dSKeith Owens 	old_sw->ar_fpsr = old_regs->ar_fpsr;
9057f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[4-1], ms->pmsa_nat_bits, &old_sw->r4, &old_unat);
9067f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[5-1], ms->pmsa_nat_bits, &old_sw->r5, &old_unat);
9077f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[6-1], ms->pmsa_nat_bits, &old_sw->r6, &old_unat);
9087f613c7dSKeith Owens 	copy_reg(&ms->pmsa_gr[7-1], ms->pmsa_nat_bits, &old_sw->r7, &old_unat);
9097f613c7dSKeith Owens 	old_sw->b0 = (u64)ia64_leave_kernel;
9107f613c7dSKeith Owens 	old_sw->b1 = ms->pmsa_br1;
9117f613c7dSKeith Owens 	old_sw->ar_pfs = 0;
9127f613c7dSKeith Owens 	old_sw->ar_unat = old_unat;
9137f613c7dSKeith Owens 	old_sw->pr = old_regs->pr | (1UL << PRED_NON_SYSCALL);
9147f613c7dSKeith Owens 	previous_current->thread.ksp = (u64)p - 16;
9157f613c7dSKeith Owens 
9167f613c7dSKeith Owens 	/* Finally copy the original stack's registers back to its RBS.
9177f613c7dSKeith Owens 	 * Registers from ar.bspstore through ar.bsp at the time of the event
9187f613c7dSKeith Owens 	 * are in the current RBS, copy them back to the original stack.  The
9197f613c7dSKeith Owens 	 * copy must be done register by register because the original bspstore
9207f613c7dSKeith Owens 	 * and the current one have different alignments, so the saved RNAT
9217f613c7dSKeith Owens 	 * data occurs at different places.
9227f613c7dSKeith Owens 	 *
9237f613c7dSKeith Owens 	 * mca_asm does cover, so the old_bsp already includes all registers at
9247f613c7dSKeith Owens 	 * the time of MCA/INIT.  It also does flushrs, so all registers before
9257f613c7dSKeith Owens 	 * this function have been written to backing store on the MCA/INIT
9267f613c7dSKeith Owens 	 * stack.
9277f613c7dSKeith Owens 	 */
9287f613c7dSKeith Owens 	new_rnat = ia64_get_rnat(ia64_rse_rnat_addr(new_bspstore));
9297f613c7dSKeith Owens 	old_rnat = regs->ar_rnat;
9307f613c7dSKeith Owens 	while (slots--) {
9317f613c7dSKeith Owens 		if (ia64_rse_is_rnat_slot(new_bspstore)) {
9327f613c7dSKeith Owens 			new_rnat = ia64_get_rnat(new_bspstore++);
9337f613c7dSKeith Owens 		}
9347f613c7dSKeith Owens 		if (ia64_rse_is_rnat_slot(old_bspstore)) {
9357f613c7dSKeith Owens 			*old_bspstore++ = old_rnat;
9367f613c7dSKeith Owens 			old_rnat = 0;
9377f613c7dSKeith Owens 		}
9387f613c7dSKeith Owens 		nat = (new_rnat >> ia64_rse_slot_num(new_bspstore)) & 1UL;
9397f613c7dSKeith Owens 		old_rnat &= ~(1UL << ia64_rse_slot_num(old_bspstore));
9407f613c7dSKeith Owens 		old_rnat |= (nat << ia64_rse_slot_num(old_bspstore));
9417f613c7dSKeith Owens 		*old_bspstore++ = *new_bspstore++;
9427f613c7dSKeith Owens 	}
9437f613c7dSKeith Owens 	old_sw->ar_bspstore = (unsigned long)old_bspstore;
9447f613c7dSKeith Owens 	old_sw->ar_rnat = old_rnat;
9457f613c7dSKeith Owens 
9467f613c7dSKeith Owens 	sos->prev_task = previous_current;
9477f613c7dSKeith Owens 	return previous_current;
9487f613c7dSKeith Owens 
9497f613c7dSKeith Owens no_mod:
9507f613c7dSKeith Owens 	printk(KERN_INFO "cpu %d, %s %s, original stack not modified\n",
9517f613c7dSKeith Owens 			smp_processor_id(), type, msg);
9527f613c7dSKeith Owens 	return previous_current;
9537f613c7dSKeith Owens }
9547f613c7dSKeith Owens 
9557f613c7dSKeith Owens /* The monarch/slave interaction is based on monarch_cpu and requires that all
9567f613c7dSKeith Owens  * slaves have entered rendezvous before the monarch leaves.  If any cpu has
9577f613c7dSKeith Owens  * not entered rendezvous yet then wait a bit.  The assumption is that any
9587f613c7dSKeith Owens  * slave that has not rendezvoused after a reasonable time is never going to do
9597f613c7dSKeith Owens  * so.  In this context, slave includes cpus that respond to the MCA rendezvous
9607f613c7dSKeith Owens  * interrupt, as well as cpus that receive the INIT slave event.
9617f613c7dSKeith Owens  */
9627f613c7dSKeith Owens 
9637f613c7dSKeith Owens static void
9647f613c7dSKeith Owens ia64_wait_for_slaves(int monarch)
9657f613c7dSKeith Owens {
9669336b083SKeith Owens 	int c, wait = 0, missing = 0;
9677f613c7dSKeith Owens 	for_each_online_cpu(c) {
9687f613c7dSKeith Owens 		if (c == monarch)
9697f613c7dSKeith Owens 			continue;
9707f613c7dSKeith Owens 		if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE) {
9717f613c7dSKeith Owens 			udelay(1000);		/* short wait first */
9727f613c7dSKeith Owens 			wait = 1;
9737f613c7dSKeith Owens 			break;
9747f613c7dSKeith Owens 		}
9757f613c7dSKeith Owens 	}
9767f613c7dSKeith Owens 	if (!wait)
9779336b083SKeith Owens 		goto all_in;
9787f613c7dSKeith Owens 	for_each_online_cpu(c) {
9797f613c7dSKeith Owens 		if (c == monarch)
9807f613c7dSKeith Owens 			continue;
9817f613c7dSKeith Owens 		if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE) {
9827f613c7dSKeith Owens 			udelay(5*1000000);	/* wait 5 seconds for slaves (arbitrary) */
9839336b083SKeith Owens 			if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE)
9849336b083SKeith Owens 				missing = 1;
9857f613c7dSKeith Owens 			break;
9867f613c7dSKeith Owens 		}
9877f613c7dSKeith Owens 	}
9889336b083SKeith Owens 	if (!missing)
9899336b083SKeith Owens 		goto all_in;
9909336b083SKeith Owens 	printk(KERN_INFO "OS MCA slave did not rendezvous on cpu");
9919336b083SKeith Owens 	for_each_online_cpu(c) {
9929336b083SKeith Owens 		if (c == monarch)
9939336b083SKeith Owens 			continue;
9949336b083SKeith Owens 		if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE)
9959336b083SKeith Owens 			printk(" %d", c);
9969336b083SKeith Owens 	}
9979336b083SKeith Owens 	printk("\n");
9989336b083SKeith Owens 	return;
9999336b083SKeith Owens 
10009336b083SKeith Owens all_in:
10019336b083SKeith Owens 	printk(KERN_INFO "All OS MCA slaves have reached rendezvous\n");
10029336b083SKeith Owens 	return;
10037f613c7dSKeith Owens }
10047f613c7dSKeith Owens 
10051da177e4SLinus Torvalds /*
10067f613c7dSKeith Owens  * ia64_mca_handler
10071da177e4SLinus Torvalds  *
10081da177e4SLinus Torvalds  *	This is uncorrectable machine check handler called from OS_MCA
10091da177e4SLinus Torvalds  *	dispatch code which is in turn called from SAL_CHECK().
10101da177e4SLinus Torvalds  *	This is the place where the core of OS MCA handling is done.
10111da177e4SLinus Torvalds  *	Right now the logs are extracted and displayed in a well-defined
10121da177e4SLinus Torvalds  *	format. This handler code is supposed to be run only on the
10131da177e4SLinus Torvalds  *	monarch processor. Once the monarch is done with MCA handling
10141da177e4SLinus Torvalds  *	further MCA logging is enabled by clearing logs.
10151da177e4SLinus Torvalds  *	Monarch also has the duty of sending wakeup-IPIs to pull the
10161da177e4SLinus Torvalds  *	slave processors out of rendezvous spinloop.
10171da177e4SLinus Torvalds  */
10181da177e4SLinus Torvalds void
10197f613c7dSKeith Owens ia64_mca_handler(struct pt_regs *regs, struct switch_stack *sw,
10207f613c7dSKeith Owens 		 struct ia64_sal_os_state *sos)
10211da177e4SLinus Torvalds {
10221da177e4SLinus Torvalds 	pal_processor_state_info_t *psp = (pal_processor_state_info_t *)
10237f613c7dSKeith Owens 		&sos->proc_state_param;
10247f613c7dSKeith Owens 	int recover, cpu = smp_processor_id();
10257f613c7dSKeith Owens 	task_t *previous_current;
10267f613c7dSKeith Owens 
10277f613c7dSKeith Owens 	oops_in_progress = 1;	/* FIXME: make printk NMI/MCA/INIT safe */
10289336b083SKeith Owens 	console_loglevel = 15;	/* make sure printks make it to console */
10299336b083SKeith Owens 	printk(KERN_INFO "Entered OS MCA handler. PSP=%lx cpu=%d monarch=%ld\n",
10309336b083SKeith Owens 		sos->proc_state_param, cpu, sos->monarch);
10319336b083SKeith Owens 
10327f613c7dSKeith Owens 	previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "MCA");
10337f613c7dSKeith Owens 	monarch_cpu = cpu;
10349138d581SKeith Owens 	if (notify_die(DIE_MCA_MONARCH_ENTER, "MCA", regs, 0, 0, 0)
10359138d581SKeith Owens 			== NOTIFY_STOP)
10369138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
10377f613c7dSKeith Owens 	ia64_wait_for_slaves(cpu);
10387f613c7dSKeith Owens 
10397f613c7dSKeith Owens 	/* Wakeup all the processors which are spinning in the rendezvous loop.
10407f613c7dSKeith Owens 	 * They will leave SAL, then spin in the OS with interrupts disabled
10417f613c7dSKeith Owens 	 * until this monarch cpu leaves the MCA handler.  That gets control
10427f613c7dSKeith Owens 	 * back to the OS so we can backtrace the other cpus, backtrace when
10437f613c7dSKeith Owens 	 * spinning in SAL does not work.
10447f613c7dSKeith Owens 	 */
10457f613c7dSKeith Owens 	ia64_mca_wakeup_all();
10469138d581SKeith Owens 	if (notify_die(DIE_MCA_MONARCH_PROCESS, "MCA", regs, 0, 0, 0)
10479138d581SKeith Owens 			== NOTIFY_STOP)
10489138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/* Get the MCA error record and log it */
10511da177e4SLinus Torvalds 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_MCA);
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	/* TLB error is only exist in this SAL error record */
10541da177e4SLinus Torvalds 	recover = (psp->tc && !(psp->cc || psp->bc || psp->rc || psp->uc))
10551da177e4SLinus Torvalds 	/* other error recovery */
10561da177e4SLinus Torvalds 	   || (ia64_mca_ucmc_extension
10571da177e4SLinus Torvalds 		&& ia64_mca_ucmc_extension(
10581da177e4SLinus Torvalds 			IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA),
10597f613c7dSKeith Owens 			sos));
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	if (recover) {
10621da177e4SLinus Torvalds 		sal_log_record_header_t *rh = IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA);
10631da177e4SLinus Torvalds 		rh->severity = sal_log_severity_corrected;
10641da177e4SLinus Torvalds 		ia64_sal_clear_state_info(SAL_INFO_TYPE_MCA);
10657f613c7dSKeith Owens 		sos->os_status = IA64_MCA_CORRECTED;
10661da177e4SLinus Torvalds 	}
10679138d581SKeith Owens 	if (notify_die(DIE_MCA_MONARCH_LEAVE, "MCA", regs, 0, 0, recover)
10689138d581SKeith Owens 			== NOTIFY_STOP)
10699138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
10701da177e4SLinus Torvalds 
10717f613c7dSKeith Owens 	set_curr_task(cpu, previous_current);
10727f613c7dSKeith Owens 	monarch_cpu = -1;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds static DECLARE_WORK(cmc_disable_work, ia64_mca_cmc_vector_disable_keventd, NULL);
10761da177e4SLinus Torvalds static DECLARE_WORK(cmc_enable_work, ia64_mca_cmc_vector_enable_keventd, NULL);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds /*
10791da177e4SLinus Torvalds  * ia64_mca_cmc_int_handler
10801da177e4SLinus Torvalds  *
10811da177e4SLinus Torvalds  *  This is corrected machine check interrupt handler.
10821da177e4SLinus Torvalds  *	Right now the logs are extracted and displayed in a well-defined
10831da177e4SLinus Torvalds  *	format.
10841da177e4SLinus Torvalds  *
10851da177e4SLinus Torvalds  * Inputs
10861da177e4SLinus Torvalds  *      interrupt number
10871da177e4SLinus Torvalds  *      client data arg ptr
10881da177e4SLinus Torvalds  *      saved registers ptr
10891da177e4SLinus Torvalds  *
10901da177e4SLinus Torvalds  * Outputs
10911da177e4SLinus Torvalds  *	None
10921da177e4SLinus Torvalds  */
10931da177e4SLinus Torvalds static irqreturn_t
10941da177e4SLinus Torvalds ia64_mca_cmc_int_handler(int cmc_irq, void *arg, struct pt_regs *ptregs)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	static unsigned long	cmc_history[CMC_HISTORY_LENGTH];
10971da177e4SLinus Torvalds 	static int		index;
10981da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(cmc_history_lock);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
11011da177e4SLinus Torvalds 		       __FUNCTION__, cmc_irq, smp_processor_id());
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	/* SAL spec states this should run w/ interrupts enabled */
11041da177e4SLinus Torvalds 	local_irq_enable();
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	/* Get the CMC error record and log it */
11071da177e4SLinus Torvalds 	ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CMC);
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	spin_lock(&cmc_history_lock);
11101da177e4SLinus Torvalds 	if (!cmc_polling_enabled) {
11111da177e4SLinus Torvalds 		int i, count = 1; /* we know 1 happened now */
11121da177e4SLinus Torvalds 		unsigned long now = jiffies;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 		for (i = 0; i < CMC_HISTORY_LENGTH; i++) {
11151da177e4SLinus Torvalds 			if (now - cmc_history[i] <= HZ)
11161da177e4SLinus Torvalds 				count++;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		IA64_MCA_DEBUG(KERN_INFO "CMC threshold %d/%d\n", count, CMC_HISTORY_LENGTH);
11201da177e4SLinus Torvalds 		if (count >= CMC_HISTORY_LENGTH) {
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 			cmc_polling_enabled = 1;
11231da177e4SLinus Torvalds 			spin_unlock(&cmc_history_lock);
112476e677e2SBryan Sutula 			/* If we're being hit with CMC interrupts, we won't
112576e677e2SBryan Sutula 			 * ever execute the schedule_work() below.  Need to
112676e677e2SBryan Sutula 			 * disable CMC interrupts on this processor now.
112776e677e2SBryan Sutula 			 */
112876e677e2SBryan Sutula 			ia64_mca_cmc_vector_disable(NULL);
11291da177e4SLinus Torvalds 			schedule_work(&cmc_disable_work);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 			/*
11321da177e4SLinus Torvalds 			 * Corrected errors will still be corrected, but
11331da177e4SLinus Torvalds 			 * make sure there's a log somewhere that indicates
11341da177e4SLinus Torvalds 			 * something is generating more than we can handle.
11351da177e4SLinus Torvalds 			 */
11361da177e4SLinus Torvalds 			printk(KERN_WARNING "WARNING: Switching to polling CMC handler; error records may be lost\n");
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 			mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 			/* lock already released, get out now */
11411da177e4SLinus Torvalds 			return IRQ_HANDLED;
11421da177e4SLinus Torvalds 		} else {
11431da177e4SLinus Torvalds 			cmc_history[index++] = now;
11441da177e4SLinus Torvalds 			if (index == CMC_HISTORY_LENGTH)
11451da177e4SLinus Torvalds 				index = 0;
11461da177e4SLinus Torvalds 		}
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 	spin_unlock(&cmc_history_lock);
11491da177e4SLinus Torvalds 	return IRQ_HANDLED;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds /*
11531da177e4SLinus Torvalds  *  ia64_mca_cmc_int_caller
11541da177e4SLinus Torvalds  *
11551da177e4SLinus Torvalds  * 	Triggered by sw interrupt from CMC polling routine.  Calls
11561da177e4SLinus Torvalds  * 	real interrupt handler and either triggers a sw interrupt
11571da177e4SLinus Torvalds  * 	on the next cpu or does cleanup at the end.
11581da177e4SLinus Torvalds  *
11591da177e4SLinus Torvalds  * Inputs
11601da177e4SLinus Torvalds  *	interrupt number
11611da177e4SLinus Torvalds  *	client data arg ptr
11621da177e4SLinus Torvalds  *	saved registers ptr
11631da177e4SLinus Torvalds  * Outputs
11641da177e4SLinus Torvalds  * 	handled
11651da177e4SLinus Torvalds  */
11661da177e4SLinus Torvalds static irqreturn_t
11671da177e4SLinus Torvalds ia64_mca_cmc_int_caller(int cmc_irq, void *arg, struct pt_regs *ptregs)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	static int start_count = -1;
11701da177e4SLinus Torvalds 	unsigned int cpuid;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	cpuid = smp_processor_id();
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/* If first cpu, update count */
11751da177e4SLinus Torvalds 	if (start_count == -1)
11761da177e4SLinus Torvalds 		start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CMC);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	ia64_mca_cmc_int_handler(cmc_irq, arg, ptregs);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	for (++cpuid ; cpuid < NR_CPUS && !cpu_online(cpuid) ; cpuid++);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	if (cpuid < NR_CPUS) {
11831da177e4SLinus Torvalds 		platform_send_ipi(cpuid, IA64_CMCP_VECTOR, IA64_IPI_DM_INT, 0);
11841da177e4SLinus Torvalds 	} else {
11851da177e4SLinus Torvalds 		/* If no log record, switch out of polling mode */
11861da177e4SLinus Torvalds 		if (start_count == IA64_LOG_COUNT(SAL_INFO_TYPE_CMC)) {
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			printk(KERN_WARNING "Returning to interrupt driven CMC handler\n");
11891da177e4SLinus Torvalds 			schedule_work(&cmc_enable_work);
11901da177e4SLinus Torvalds 			cmc_polling_enabled = 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 		} else {
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 			mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		start_count = -1;
11981da177e4SLinus Torvalds 	}
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	return IRQ_HANDLED;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds /*
12041da177e4SLinus Torvalds  *  ia64_mca_cmc_poll
12051da177e4SLinus Torvalds  *
12061da177e4SLinus Torvalds  *	Poll for Corrected Machine Checks (CMCs)
12071da177e4SLinus Torvalds  *
12081da177e4SLinus Torvalds  * Inputs   :   dummy(unused)
12091da177e4SLinus Torvalds  * Outputs  :   None
12101da177e4SLinus Torvalds  *
12111da177e4SLinus Torvalds  */
12121da177e4SLinus Torvalds static void
12131da177e4SLinus Torvalds ia64_mca_cmc_poll (unsigned long dummy)
12141da177e4SLinus Torvalds {
12151da177e4SLinus Torvalds 	/* Trigger a CMC interrupt cascade  */
12161da177e4SLinus Torvalds 	platform_send_ipi(first_cpu(cpu_online_map), IA64_CMCP_VECTOR, IA64_IPI_DM_INT, 0);
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds /*
12201da177e4SLinus Torvalds  *  ia64_mca_cpe_int_caller
12211da177e4SLinus Torvalds  *
12221da177e4SLinus Torvalds  * 	Triggered by sw interrupt from CPE polling routine.  Calls
12231da177e4SLinus Torvalds  * 	real interrupt handler and either triggers a sw interrupt
12241da177e4SLinus Torvalds  * 	on the next cpu or does cleanup at the end.
12251da177e4SLinus Torvalds  *
12261da177e4SLinus Torvalds  * Inputs
12271da177e4SLinus Torvalds  *	interrupt number
12281da177e4SLinus Torvalds  *	client data arg ptr
12291da177e4SLinus Torvalds  *	saved registers ptr
12301da177e4SLinus Torvalds  * Outputs
12311da177e4SLinus Torvalds  * 	handled
12321da177e4SLinus Torvalds  */
12331da177e4SLinus Torvalds #ifdef CONFIG_ACPI
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds static irqreturn_t
12361da177e4SLinus Torvalds ia64_mca_cpe_int_caller(int cpe_irq, void *arg, struct pt_regs *ptregs)
12371da177e4SLinus Torvalds {
12381da177e4SLinus Torvalds 	static int start_count = -1;
12391da177e4SLinus Torvalds 	static int poll_time = MIN_CPE_POLL_INTERVAL;
12401da177e4SLinus Torvalds 	unsigned int cpuid;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	cpuid = smp_processor_id();
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	/* If first cpu, update count */
12451da177e4SLinus Torvalds 	if (start_count == -1)
12461da177e4SLinus Torvalds 		start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CPE);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	ia64_mca_cpe_int_handler(cpe_irq, arg, ptregs);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	for (++cpuid ; cpuid < NR_CPUS && !cpu_online(cpuid) ; cpuid++);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	if (cpuid < NR_CPUS) {
12531da177e4SLinus Torvalds 		platform_send_ipi(cpuid, IA64_CPEP_VECTOR, IA64_IPI_DM_INT, 0);
12541da177e4SLinus Torvalds 	} else {
12551da177e4SLinus Torvalds 		/*
12561da177e4SLinus Torvalds 		 * If a log was recorded, increase our polling frequency,
12571da177e4SLinus Torvalds 		 * otherwise, backoff or return to interrupt mode.
12581da177e4SLinus Torvalds 		 */
12591da177e4SLinus Torvalds 		if (start_count != IA64_LOG_COUNT(SAL_INFO_TYPE_CPE)) {
12601da177e4SLinus Torvalds 			poll_time = max(MIN_CPE_POLL_INTERVAL, poll_time / 2);
12611da177e4SLinus Torvalds 		} else if (cpe_vector < 0) {
12621da177e4SLinus Torvalds 			poll_time = min(MAX_CPE_POLL_INTERVAL, poll_time * 2);
12631da177e4SLinus Torvalds 		} else {
12641da177e4SLinus Torvalds 			poll_time = MIN_CPE_POLL_INTERVAL;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 			printk(KERN_WARNING "Returning to interrupt driven CPE handler\n");
12671da177e4SLinus Torvalds 			enable_irq(local_vector_to_irq(IA64_CPE_VECTOR));
12681da177e4SLinus Torvalds 			cpe_poll_enabled = 0;
12691da177e4SLinus Torvalds 		}
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		if (cpe_poll_enabled)
12721da177e4SLinus Torvalds 			mod_timer(&cpe_poll_timer, jiffies + poll_time);
12731da177e4SLinus Torvalds 		start_count = -1;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	return IRQ_HANDLED;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /*
12801da177e4SLinus Torvalds  *  ia64_mca_cpe_poll
12811da177e4SLinus Torvalds  *
12821da177e4SLinus Torvalds  *	Poll for Corrected Platform Errors (CPEs), trigger interrupt
12831da177e4SLinus Torvalds  *	on first cpu, from there it will trickle through all the cpus.
12841da177e4SLinus Torvalds  *
12851da177e4SLinus Torvalds  * Inputs   :   dummy(unused)
12861da177e4SLinus Torvalds  * Outputs  :   None
12871da177e4SLinus Torvalds  *
12881da177e4SLinus Torvalds  */
12891da177e4SLinus Torvalds static void
12901da177e4SLinus Torvalds ia64_mca_cpe_poll (unsigned long dummy)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	/* Trigger a CPE interrupt cascade  */
12931da177e4SLinus Torvalds 	platform_send_ipi(first_cpu(cpu_online_map), IA64_CPEP_VECTOR, IA64_IPI_DM_INT, 0);
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296b655913bSPeter Chubb #endif /* CONFIG_ACPI */
1297b655913bSPeter Chubb 
12989138d581SKeith Owens static int
12999138d581SKeith Owens default_monarch_init_process(struct notifier_block *self, unsigned long val, void *data)
13009138d581SKeith Owens {
13019138d581SKeith Owens 	int c;
13029138d581SKeith Owens 	struct task_struct *g, *t;
13039138d581SKeith Owens 	if (val != DIE_INIT_MONARCH_PROCESS)
13049138d581SKeith Owens 		return NOTIFY_DONE;
13059138d581SKeith Owens 	printk(KERN_ERR "Processes interrupted by INIT -");
13069138d581SKeith Owens 	for_each_online_cpu(c) {
13079138d581SKeith Owens 		struct ia64_sal_os_state *s;
13089138d581SKeith Owens 		t = __va(__per_cpu_mca[c] + IA64_MCA_CPU_INIT_STACK_OFFSET);
13099138d581SKeith Owens 		s = (struct ia64_sal_os_state *)((char *)t + MCA_SOS_OFFSET);
13109138d581SKeith Owens 		g = s->prev_task;
13119138d581SKeith Owens 		if (g) {
13129138d581SKeith Owens 			if (g->pid)
13139138d581SKeith Owens 				printk(" %d", g->pid);
13149138d581SKeith Owens 			else
13159138d581SKeith Owens 				printk(" %d (cpu %d task 0x%p)", g->pid, task_cpu(g), g);
13169138d581SKeith Owens 		}
13179138d581SKeith Owens 	}
13189138d581SKeith Owens 	printk("\n\n");
13199138d581SKeith Owens 	if (read_trylock(&tasklist_lock)) {
13209138d581SKeith Owens 		do_each_thread (g, t) {
13219138d581SKeith Owens 			printk("\nBacktrace of pid %d (%s)\n", t->pid, t->comm);
13229138d581SKeith Owens 			show_stack(t, NULL);
13239138d581SKeith Owens 		} while_each_thread (g, t);
13249138d581SKeith Owens 		read_unlock(&tasklist_lock);
13259138d581SKeith Owens 	}
13269138d581SKeith Owens 	return NOTIFY_DONE;
13279138d581SKeith Owens }
13289138d581SKeith Owens 
13291da177e4SLinus Torvalds /*
13301da177e4SLinus Torvalds  * C portion of the OS INIT handler
13311da177e4SLinus Torvalds  *
13327f613c7dSKeith Owens  * Called from ia64_os_init_dispatch
13331da177e4SLinus Torvalds  *
13347f613c7dSKeith Owens  * Inputs: pointer to pt_regs where processor info was saved.  SAL/OS state for
13357f613c7dSKeith Owens  * this event.  This code is used for both monarch and slave INIT events, see
13367f613c7dSKeith Owens  * sos->monarch.
13371da177e4SLinus Torvalds  *
13387f613c7dSKeith Owens  * All INIT events switch to the INIT stack and change the previous process to
13397f613c7dSKeith Owens  * blocked status.  If one of the INIT events is the monarch then we are
13407f613c7dSKeith Owens  * probably processing the nmi button/command.  Use the monarch cpu to dump all
13417f613c7dSKeith Owens  * the processes.  The slave INIT events all spin until the monarch cpu
13427f613c7dSKeith Owens  * returns.  We can also get INIT slave events for MCA, in which case the MCA
13437f613c7dSKeith Owens  * process is the monarch.
13441da177e4SLinus Torvalds  */
13451da177e4SLinus Torvalds 
13467f613c7dSKeith Owens void
13477f613c7dSKeith Owens ia64_init_handler(struct pt_regs *regs, struct switch_stack *sw,
13487f613c7dSKeith Owens 		  struct ia64_sal_os_state *sos)
13497f613c7dSKeith Owens {
13507f613c7dSKeith Owens 	static atomic_t slaves;
13517f613c7dSKeith Owens 	static atomic_t monarchs;
13527f613c7dSKeith Owens 	task_t *previous_current;
13539138d581SKeith Owens 	int cpu = smp_processor_id();
13547f613c7dSKeith Owens 
13557f613c7dSKeith Owens 	oops_in_progress = 1;	/* FIXME: make printk NMI/MCA/INIT safe */
13561da177e4SLinus Torvalds 	console_loglevel = 15;	/* make sure printks make it to console */
13571da177e4SLinus Torvalds 
13587f613c7dSKeith Owens 	printk(KERN_INFO "Entered OS INIT handler. PSP=%lx cpu=%d monarch=%ld\n",
13597f613c7dSKeith Owens 		sos->proc_state_param, cpu, sos->monarch);
13607f613c7dSKeith Owens 	salinfo_log_wakeup(SAL_INFO_TYPE_INIT, NULL, 0, 0);
13617f613c7dSKeith Owens 
13627f613c7dSKeith Owens 	previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "INIT");
13637f613c7dSKeith Owens 	sos->os_status = IA64_INIT_RESUME;
13647f613c7dSKeith Owens 
13657f613c7dSKeith Owens 	/* FIXME: Workaround for broken proms that drive all INIT events as
13667f613c7dSKeith Owens 	 * slaves.  The last slave that enters is promoted to be a monarch.
13677f613c7dSKeith Owens 	 * Remove this code in September 2006, that gives platforms a year to
13687f613c7dSKeith Owens 	 * fix their proms and get their customers updated.
13697f613c7dSKeith Owens 	 */
13707f613c7dSKeith Owens 	if (!sos->monarch && atomic_add_return(1, &slaves) == num_online_cpus()) {
13717f613c7dSKeith Owens 		printk(KERN_WARNING "%s: Promoting cpu %d to monarch.\n",
13727f613c7dSKeith Owens 		       __FUNCTION__, cpu);
13737f613c7dSKeith Owens 		atomic_dec(&slaves);
13747f613c7dSKeith Owens 		sos->monarch = 1;
13757f613c7dSKeith Owens 	}
13767f613c7dSKeith Owens 
13777f613c7dSKeith Owens 	/* FIXME: Workaround for broken proms that drive all INIT events as
13787f613c7dSKeith Owens 	 * monarchs.  Second and subsequent monarchs are demoted to slaves.
13797f613c7dSKeith Owens 	 * Remove this code in September 2006, that gives platforms a year to
13807f613c7dSKeith Owens 	 * fix their proms and get their customers updated.
13817f613c7dSKeith Owens 	 */
13827f613c7dSKeith Owens 	if (sos->monarch && atomic_add_return(1, &monarchs) > 1) {
13837f613c7dSKeith Owens 		printk(KERN_WARNING "%s: Demoting cpu %d to slave.\n",
13847f613c7dSKeith Owens 			       __FUNCTION__, cpu);
13857f613c7dSKeith Owens 		atomic_dec(&monarchs);
13867f613c7dSKeith Owens 		sos->monarch = 0;
13877f613c7dSKeith Owens 	}
13887f613c7dSKeith Owens 
13897f613c7dSKeith Owens 	if (!sos->monarch) {
13907f613c7dSKeith Owens 		ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_INIT;
13917f613c7dSKeith Owens 		while (monarch_cpu == -1)
13927f613c7dSKeith Owens 		       cpu_relax();	/* spin until monarch enters */
13939138d581SKeith Owens 		if (notify_die(DIE_INIT_SLAVE_ENTER, "INIT", regs, 0, 0, 0)
13949138d581SKeith Owens 				== NOTIFY_STOP)
13959138d581SKeith Owens 			ia64_mca_spin(__FUNCTION__);
13969138d581SKeith Owens 		if (notify_die(DIE_INIT_SLAVE_PROCESS, "INIT", regs, 0, 0, 0)
13979138d581SKeith Owens 				== NOTIFY_STOP)
13989138d581SKeith Owens 			ia64_mca_spin(__FUNCTION__);
13997f613c7dSKeith Owens 		while (monarch_cpu != -1)
14007f613c7dSKeith Owens 		       cpu_relax();	/* spin until monarch leaves */
14019138d581SKeith Owens 		if (notify_die(DIE_INIT_SLAVE_LEAVE, "INIT", regs, 0, 0, 0)
14029138d581SKeith Owens 				== NOTIFY_STOP)
14039138d581SKeith Owens 			ia64_mca_spin(__FUNCTION__);
14047f613c7dSKeith Owens 		printk("Slave on cpu %d returning to normal service.\n", cpu);
14057f613c7dSKeith Owens 		set_curr_task(cpu, previous_current);
14067f613c7dSKeith Owens 		ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
14077f613c7dSKeith Owens 		atomic_dec(&slaves);
14087f613c7dSKeith Owens 		return;
14097f613c7dSKeith Owens 	}
14107f613c7dSKeith Owens 
14117f613c7dSKeith Owens 	monarch_cpu = cpu;
14129138d581SKeith Owens 	if (notify_die(DIE_INIT_MONARCH_ENTER, "INIT", regs, 0, 0, 0)
14139138d581SKeith Owens 			== NOTIFY_STOP)
14149138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/*
14177f613c7dSKeith Owens 	 * Wait for a bit.  On some machines (e.g., HP's zx2000 and zx6000, INIT can be
14187f613c7dSKeith Owens 	 * generated via the BMC's command-line interface, but since the console is on the
14197f613c7dSKeith Owens 	 * same serial line, the user will need some time to switch out of the BMC before
14207f613c7dSKeith Owens 	 * the dump begins.
14211da177e4SLinus Torvalds 	 */
14227f613c7dSKeith Owens 	printk("Delaying for 5 seconds...\n");
14237f613c7dSKeith Owens 	udelay(5*1000000);
14247f613c7dSKeith Owens 	ia64_wait_for_slaves(cpu);
14259138d581SKeith Owens 	/* If nobody intercepts DIE_INIT_MONARCH_PROCESS then we drop through
14269138d581SKeith Owens 	 * to default_monarch_init_process() above and just print all the
14279138d581SKeith Owens 	 * tasks.
14289138d581SKeith Owens 	 */
14299138d581SKeith Owens 	if (notify_die(DIE_INIT_MONARCH_PROCESS, "INIT", regs, 0, 0, 0)
14309138d581SKeith Owens 			== NOTIFY_STOP)
14319138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
14329138d581SKeith Owens 	if (notify_die(DIE_INIT_MONARCH_LEAVE, "INIT", regs, 0, 0, 0)
14339138d581SKeith Owens 			== NOTIFY_STOP)
14349138d581SKeith Owens 		ia64_mca_spin(__FUNCTION__);
14357f613c7dSKeith Owens 	printk("\nINIT dump complete.  Monarch on cpu %d returning to normal service.\n", cpu);
14367f613c7dSKeith Owens 	atomic_dec(&monarchs);
14377f613c7dSKeith Owens 	set_curr_task(cpu, previous_current);
14387f613c7dSKeith Owens 	monarch_cpu = -1;
14397f613c7dSKeith Owens 	return;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds static int __init
14431da177e4SLinus Torvalds ia64_mca_disable_cpe_polling(char *str)
14441da177e4SLinus Torvalds {
14451da177e4SLinus Torvalds 	cpe_poll_enabled = 0;
14461da177e4SLinus Torvalds 	return 1;
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds __setup("disable_cpe_poll", ia64_mca_disable_cpe_polling);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds static struct irqaction cmci_irqaction = {
14521da177e4SLinus Torvalds 	.handler =	ia64_mca_cmc_int_handler,
14531da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14541da177e4SLinus Torvalds 	.name =		"cmc_hndlr"
14551da177e4SLinus Torvalds };
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds static struct irqaction cmcp_irqaction = {
14581da177e4SLinus Torvalds 	.handler =	ia64_mca_cmc_int_caller,
14591da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14601da177e4SLinus Torvalds 	.name =		"cmc_poll"
14611da177e4SLinus Torvalds };
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds static struct irqaction mca_rdzv_irqaction = {
14641da177e4SLinus Torvalds 	.handler =	ia64_mca_rendez_int_handler,
14651da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14661da177e4SLinus Torvalds 	.name =		"mca_rdzv"
14671da177e4SLinus Torvalds };
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds static struct irqaction mca_wkup_irqaction = {
14701da177e4SLinus Torvalds 	.handler =	ia64_mca_wakeup_int_handler,
14711da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14721da177e4SLinus Torvalds 	.name =		"mca_wkup"
14731da177e4SLinus Torvalds };
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds #ifdef CONFIG_ACPI
14761da177e4SLinus Torvalds static struct irqaction mca_cpe_irqaction = {
14771da177e4SLinus Torvalds 	.handler =	ia64_mca_cpe_int_handler,
14781da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14791da177e4SLinus Torvalds 	.name =		"cpe_hndlr"
14801da177e4SLinus Torvalds };
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds static struct irqaction mca_cpep_irqaction = {
14831da177e4SLinus Torvalds 	.handler =	ia64_mca_cpe_int_caller,
14841da177e4SLinus Torvalds 	.flags =	SA_INTERRUPT,
14851da177e4SLinus Torvalds 	.name =		"cpe_poll"
14861da177e4SLinus Torvalds };
14871da177e4SLinus Torvalds #endif /* CONFIG_ACPI */
14881da177e4SLinus Torvalds 
14897f613c7dSKeith Owens /* Minimal format of the MCA/INIT stacks.  The pseudo processes that run on
14907f613c7dSKeith Owens  * these stacks can never sleep, they cannot return from the kernel to user
14917f613c7dSKeith Owens  * space, they do not appear in a normal ps listing.  So there is no need to
14927f613c7dSKeith Owens  * format most of the fields.
14937f613c7dSKeith Owens  */
14947f613c7dSKeith Owens 
14950881fc8dSChen, Kenneth W static void __cpuinit
14967f613c7dSKeith Owens format_mca_init_stack(void *mca_data, unsigned long offset,
14977f613c7dSKeith Owens 		const char *type, int cpu)
14987f613c7dSKeith Owens {
14997f613c7dSKeith Owens 	struct task_struct *p = (struct task_struct *)((char *)mca_data + offset);
15007f613c7dSKeith Owens 	struct thread_info *ti;
15017f613c7dSKeith Owens 	memset(p, 0, KERNEL_STACK_SIZE);
1502ab03591dSAl Viro 	ti = task_thread_info(p);
15037f613c7dSKeith Owens 	ti->flags = _TIF_MCA_INIT;
15047f613c7dSKeith Owens 	ti->preempt_count = 1;
15057f613c7dSKeith Owens 	ti->task = p;
15067f613c7dSKeith Owens 	ti->cpu = cpu;
15077f613c7dSKeith Owens 	p->thread_info = ti;
15087f613c7dSKeith Owens 	p->state = TASK_UNINTERRUPTIBLE;
1509*4668f0cdSAkinobu Mita 	cpu_set(cpu, p->cpus_allowed);
15107f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->tasks);
15117f613c7dSKeith Owens 	p->parent = p->real_parent = p->group_leader = p;
15127f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->children);
15137f613c7dSKeith Owens 	INIT_LIST_HEAD(&p->sibling);
15147f613c7dSKeith Owens 	strncpy(p->comm, type, sizeof(p->comm)-1);
15157f613c7dSKeith Owens }
15167f613c7dSKeith Owens 
15171da177e4SLinus Torvalds /* Do per-CPU MCA-related initialization.  */
15181da177e4SLinus Torvalds 
15190881fc8dSChen, Kenneth W void __cpuinit
15201da177e4SLinus Torvalds ia64_mca_cpu_init(void *cpu_data)
15211da177e4SLinus Torvalds {
15221da177e4SLinus Torvalds 	void *pal_vaddr;
1523ff741906SAshok Raj 	static int first_time = 1;
15241da177e4SLinus Torvalds 
1525ff741906SAshok Raj 	if (first_time) {
15261da177e4SLinus Torvalds 		void *mca_data;
15271da177e4SLinus Torvalds 		int cpu;
15281da177e4SLinus Torvalds 
1529ff741906SAshok Raj 		first_time = 0;
15301da177e4SLinus Torvalds 		mca_data = alloc_bootmem(sizeof(struct ia64_mca_cpu)
15317f613c7dSKeith Owens 					 * NR_CPUS + KERNEL_STACK_SIZE);
15327f613c7dSKeith Owens 		mca_data = (void *)(((unsigned long)mca_data +
15337f613c7dSKeith Owens 					KERNEL_STACK_SIZE - 1) &
15347f613c7dSKeith Owens 				(-KERNEL_STACK_SIZE));
15351da177e4SLinus Torvalds 		for (cpu = 0; cpu < NR_CPUS; cpu++) {
15367f613c7dSKeith Owens 			format_mca_init_stack(mca_data,
15377f613c7dSKeith Owens 					offsetof(struct ia64_mca_cpu, mca_stack),
15387f613c7dSKeith Owens 					"MCA", cpu);
15397f613c7dSKeith Owens 			format_mca_init_stack(mca_data,
15407f613c7dSKeith Owens 					offsetof(struct ia64_mca_cpu, init_stack),
15417f613c7dSKeith Owens 					"INIT", cpu);
15421da177e4SLinus Torvalds 			__per_cpu_mca[cpu] = __pa(mca_data);
15431da177e4SLinus Torvalds 			mca_data += sizeof(struct ia64_mca_cpu);
15441da177e4SLinus Torvalds 		}
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	/*
15481da177e4SLinus Torvalds 	 * The MCA info structure was allocated earlier and its
15491da177e4SLinus Torvalds 	 * physical address saved in __per_cpu_mca[cpu].  Copy that
15501da177e4SLinus Torvalds 	 * address * to ia64_mca_data so we can access it as a per-CPU
15511da177e4SLinus Torvalds 	 * variable.
15521da177e4SLinus Torvalds 	 */
15531da177e4SLinus Torvalds 	__get_cpu_var(ia64_mca_data) = __per_cpu_mca[smp_processor_id()];
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/*
15561da177e4SLinus Torvalds 	 * Stash away a copy of the PTE needed to map the per-CPU page.
15571da177e4SLinus Torvalds 	 * We may need it during MCA recovery.
15581da177e4SLinus Torvalds 	 */
15591da177e4SLinus Torvalds 	__get_cpu_var(ia64_mca_per_cpu_pte) =
15601da177e4SLinus Torvalds 		pte_val(mk_pte_phys(__pa(cpu_data), PAGE_KERNEL));
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/*
15631da177e4SLinus Torvalds 	 * Also, stash away a copy of the PAL address and the PTE
15641da177e4SLinus Torvalds 	 * needed to map it.
15651da177e4SLinus Torvalds 	 */
15661da177e4SLinus Torvalds 	pal_vaddr = efi_get_pal_addr();
15671da177e4SLinus Torvalds 	if (!pal_vaddr)
15681da177e4SLinus Torvalds 		return;
15691da177e4SLinus Torvalds 	__get_cpu_var(ia64_mca_pal_base) =
15701da177e4SLinus Torvalds 		GRANULEROUNDDOWN((unsigned long) pal_vaddr);
15711da177e4SLinus Torvalds 	__get_cpu_var(ia64_mca_pal_pte) = pte_val(mk_pte_phys(__pa(pal_vaddr),
15721da177e4SLinus Torvalds 							      PAGE_KERNEL));
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /*
15761da177e4SLinus Torvalds  * ia64_mca_init
15771da177e4SLinus Torvalds  *
15781da177e4SLinus Torvalds  *  Do all the system level mca specific initialization.
15791da177e4SLinus Torvalds  *
15801da177e4SLinus Torvalds  *	1. Register spinloop and wakeup request interrupt vectors
15811da177e4SLinus Torvalds  *
15821da177e4SLinus Torvalds  *	2. Register OS_MCA handler entry point
15831da177e4SLinus Torvalds  *
15841da177e4SLinus Torvalds  *	3. Register OS_INIT handler entry point
15851da177e4SLinus Torvalds  *
15861da177e4SLinus Torvalds  *  4. Initialize MCA/CMC/INIT related log buffers maintained by the OS.
15871da177e4SLinus Torvalds  *
15881da177e4SLinus Torvalds  *  Note that this initialization is done very early before some kernel
15891da177e4SLinus Torvalds  *  services are available.
15901da177e4SLinus Torvalds  *
15911da177e4SLinus Torvalds  *  Inputs  :   None
15921da177e4SLinus Torvalds  *
15931da177e4SLinus Torvalds  *  Outputs :   None
15941da177e4SLinus Torvalds  */
15951da177e4SLinus Torvalds void __init
15961da177e4SLinus Torvalds ia64_mca_init(void)
15971da177e4SLinus Torvalds {
15987f613c7dSKeith Owens 	ia64_fptr_t *init_hldlr_ptr_monarch = (ia64_fptr_t *)ia64_os_init_dispatch_monarch;
15997f613c7dSKeith Owens 	ia64_fptr_t *init_hldlr_ptr_slave = (ia64_fptr_t *)ia64_os_init_dispatch_slave;
16001da177e4SLinus Torvalds 	ia64_fptr_t *mca_hldlr_ptr = (ia64_fptr_t *)ia64_os_mca_dispatch;
16011da177e4SLinus Torvalds 	int i;
16021da177e4SLinus Torvalds 	s64 rc;
16031da177e4SLinus Torvalds 	struct ia64_sal_retval isrv;
16041da177e4SLinus Torvalds 	u64 timeout = IA64_MCA_RENDEZ_TIMEOUT;	/* platform specific */
16059138d581SKeith Owens 	static struct notifier_block default_init_monarch_nb = {
16069138d581SKeith Owens 		.notifier_call = default_monarch_init_process,
16079138d581SKeith Owens 		.priority = 0/* we need to notified last */
16089138d581SKeith Owens 	};
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: begin\n", __FUNCTION__);
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	/* Clear the Rendez checkin flag for all cpus */
16131da177e4SLinus Torvalds 	for(i = 0 ; i < NR_CPUS; i++)
16141da177e4SLinus Torvalds 		ia64_mc_info.imi_rendez_checkin[i] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	/*
16171da177e4SLinus Torvalds 	 * Register the rendezvous spinloop and wakeup mechanism with SAL
16181da177e4SLinus Torvalds 	 */
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	/* Register the rendezvous interrupt vector with SAL */
16211da177e4SLinus Torvalds 	while (1) {
16221da177e4SLinus Torvalds 		isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_INT,
16231da177e4SLinus Torvalds 					      SAL_MC_PARAM_MECHANISM_INT,
16241da177e4SLinus Torvalds 					      IA64_MCA_RENDEZ_VECTOR,
16251da177e4SLinus Torvalds 					      timeout,
16261da177e4SLinus Torvalds 					      SAL_MC_PARAM_RZ_ALWAYS);
16271da177e4SLinus Torvalds 		rc = isrv.status;
16281da177e4SLinus Torvalds 		if (rc == 0)
16291da177e4SLinus Torvalds 			break;
16301da177e4SLinus Torvalds 		if (rc == -2) {
16311da177e4SLinus Torvalds 			printk(KERN_INFO "Increasing MCA rendezvous timeout from "
16321da177e4SLinus Torvalds 				"%ld to %ld milliseconds\n", timeout, isrv.v0);
16331da177e4SLinus Torvalds 			timeout = isrv.v0;
16341da177e4SLinus Torvalds 			continue;
16351da177e4SLinus Torvalds 		}
16361da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register rendezvous interrupt "
16371da177e4SLinus Torvalds 		       "with SAL (status %ld)\n", rc);
16381da177e4SLinus Torvalds 		return;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/* Register the wakeup interrupt vector with SAL */
16421da177e4SLinus Torvalds 	isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_WAKEUP,
16431da177e4SLinus Torvalds 				      SAL_MC_PARAM_MECHANISM_INT,
16441da177e4SLinus Torvalds 				      IA64_MCA_WAKEUP_VECTOR,
16451da177e4SLinus Torvalds 				      0, 0);
16461da177e4SLinus Torvalds 	rc = isrv.status;
16471da177e4SLinus Torvalds 	if (rc) {
16481da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register wakeup interrupt with SAL "
16491da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
16501da177e4SLinus Torvalds 		return;
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: registered MCA rendezvous spinloop and wakeup mech.\n", __FUNCTION__);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	ia64_mc_info.imi_mca_handler        = ia64_tpa(mca_hldlr_ptr->fp);
16561da177e4SLinus Torvalds 	/*
16571da177e4SLinus Torvalds 	 * XXX - disable SAL checksum by setting size to 0; should be
16581da177e4SLinus Torvalds 	 *	ia64_tpa(ia64_os_mca_dispatch_end) - ia64_tpa(ia64_os_mca_dispatch);
16591da177e4SLinus Torvalds 	 */
16601da177e4SLinus Torvalds 	ia64_mc_info.imi_mca_handler_size	= 0;
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	/* Register the os mca handler with SAL */
16631da177e4SLinus Torvalds 	if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_MCA,
16641da177e4SLinus Torvalds 				       ia64_mc_info.imi_mca_handler,
16651da177e4SLinus Torvalds 				       ia64_tpa(mca_hldlr_ptr->gp),
16661da177e4SLinus Torvalds 				       ia64_mc_info.imi_mca_handler_size,
16671da177e4SLinus Torvalds 				       0, 0, 0)))
16681da177e4SLinus Torvalds 	{
16691da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register OS MCA handler with SAL "
16701da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
16711da177e4SLinus Torvalds 		return;
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: registered OS MCA handler with SAL at 0x%lx, gp = 0x%lx\n", __FUNCTION__,
16751da177e4SLinus Torvalds 		       ia64_mc_info.imi_mca_handler, ia64_tpa(mca_hldlr_ptr->gp));
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	/*
16781da177e4SLinus Torvalds 	 * XXX - disable SAL checksum by setting size to 0, should be
16791da177e4SLinus Torvalds 	 * size of the actual init handler in mca_asm.S.
16801da177e4SLinus Torvalds 	 */
16817f613c7dSKeith Owens 	ia64_mc_info.imi_monarch_init_handler		= ia64_tpa(init_hldlr_ptr_monarch->fp);
16821da177e4SLinus Torvalds 	ia64_mc_info.imi_monarch_init_handler_size	= 0;
16837f613c7dSKeith Owens 	ia64_mc_info.imi_slave_init_handler		= ia64_tpa(init_hldlr_ptr_slave->fp);
16841da177e4SLinus Torvalds 	ia64_mc_info.imi_slave_init_handler_size	= 0;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: OS INIT handler at %lx\n", __FUNCTION__,
16871da177e4SLinus Torvalds 		       ia64_mc_info.imi_monarch_init_handler);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	/* Register the os init handler with SAL */
16901da177e4SLinus Torvalds 	if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_INIT,
16911da177e4SLinus Torvalds 				       ia64_mc_info.imi_monarch_init_handler,
16921da177e4SLinus Torvalds 				       ia64_tpa(ia64_getreg(_IA64_REG_GP)),
16931da177e4SLinus Torvalds 				       ia64_mc_info.imi_monarch_init_handler_size,
16941da177e4SLinus Torvalds 				       ia64_mc_info.imi_slave_init_handler,
16951da177e4SLinus Torvalds 				       ia64_tpa(ia64_getreg(_IA64_REG_GP)),
16961da177e4SLinus Torvalds 				       ia64_mc_info.imi_slave_init_handler_size)))
16971da177e4SLinus Torvalds 	{
16981da177e4SLinus Torvalds 		printk(KERN_ERR "Failed to register m/s INIT handlers with SAL "
16991da177e4SLinus Torvalds 		       "(status %ld)\n", rc);
17001da177e4SLinus Torvalds 		return;
17011da177e4SLinus Torvalds 	}
17029138d581SKeith Owens 	if (register_die_notifier(&default_init_monarch_nb)) {
17039138d581SKeith Owens 		printk(KERN_ERR "Failed to register default monarch INIT process\n");
17049138d581SKeith Owens 		return;
17059138d581SKeith Owens 	}
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: registered OS INIT handler with SAL\n", __FUNCTION__);
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	/*
17101da177e4SLinus Torvalds 	 *  Configure the CMCI/P vector and handler. Interrupts for CMC are
17111da177e4SLinus Torvalds 	 *  per-processor, so AP CMC interrupts are setup in smp_callin() (smpboot.c).
17121da177e4SLinus Torvalds 	 */
17131da177e4SLinus Torvalds 	register_percpu_irq(IA64_CMC_VECTOR, &cmci_irqaction);
17141da177e4SLinus Torvalds 	register_percpu_irq(IA64_CMCP_VECTOR, &cmcp_irqaction);
17151da177e4SLinus Torvalds 	ia64_mca_cmc_vector_setup();       /* Setup vector on BSP */
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	/* Setup the MCA rendezvous interrupt vector */
17181da177e4SLinus Torvalds 	register_percpu_irq(IA64_MCA_RENDEZ_VECTOR, &mca_rdzv_irqaction);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	/* Setup the MCA wakeup interrupt vector */
17211da177e4SLinus Torvalds 	register_percpu_irq(IA64_MCA_WAKEUP_VECTOR, &mca_wkup_irqaction);
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds #ifdef CONFIG_ACPI
1724bb68c12bSRuss Anderson 	/* Setup the CPEI/P handler */
17251da177e4SLinus Torvalds 	register_percpu_irq(IA64_CPEP_VECTOR, &mca_cpep_irqaction);
17261da177e4SLinus Torvalds #endif
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	/* Initialize the areas set aside by the OS to buffer the
17291da177e4SLinus Torvalds 	 * platform/processor error states for MCA/INIT/CMC
17301da177e4SLinus Torvalds 	 * handling.
17311da177e4SLinus Torvalds 	 */
17321da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_MCA);
17331da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_INIT);
17341da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_CMC);
17351da177e4SLinus Torvalds 	ia64_log_init(SAL_INFO_TYPE_CPE);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	mca_init = 1;
17381da177e4SLinus Torvalds 	printk(KERN_INFO "MCA related initialization done\n");
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /*
17421da177e4SLinus Torvalds  * ia64_mca_late_init
17431da177e4SLinus Torvalds  *
17441da177e4SLinus Torvalds  *	Opportunity to setup things that require initialization later
17451da177e4SLinus Torvalds  *	than ia64_mca_init.  Setup a timer to poll for CPEs if the
17461da177e4SLinus Torvalds  *	platform doesn't support an interrupt driven mechanism.
17471da177e4SLinus Torvalds  *
17481da177e4SLinus Torvalds  *  Inputs  :   None
17491da177e4SLinus Torvalds  *  Outputs :   Status
17501da177e4SLinus Torvalds  */
17511da177e4SLinus Torvalds static int __init
17521da177e4SLinus Torvalds ia64_mca_late_init(void)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	if (!mca_init)
17551da177e4SLinus Torvalds 		return 0;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	/* Setup the CMCI/P vector and handler */
17581da177e4SLinus Torvalds 	init_timer(&cmc_poll_timer);
17591da177e4SLinus Torvalds 	cmc_poll_timer.function = ia64_mca_cmc_poll;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/* Unmask/enable the vector */
17621da177e4SLinus Torvalds 	cmc_polling_enabled = 0;
17631da177e4SLinus Torvalds 	schedule_work(&cmc_enable_work);
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	IA64_MCA_DEBUG("%s: CMCI/P setup and enabled.\n", __FUNCTION__);
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds #ifdef CONFIG_ACPI
17681da177e4SLinus Torvalds 	/* Setup the CPEI/P vector and handler */
1769bb68c12bSRuss Anderson 	cpe_vector = acpi_request_vector(ACPI_INTERRUPT_CPEI);
17701da177e4SLinus Torvalds 	init_timer(&cpe_poll_timer);
17711da177e4SLinus Torvalds 	cpe_poll_timer.function = ia64_mca_cpe_poll;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	{
17741da177e4SLinus Torvalds 		irq_desc_t *desc;
17751da177e4SLinus Torvalds 		unsigned int irq;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 		if (cpe_vector >= 0) {
17781da177e4SLinus Torvalds 			/* If platform supports CPEI, enable the irq. */
17791da177e4SLinus Torvalds 			cpe_poll_enabled = 0;
17801da177e4SLinus Torvalds 			for (irq = 0; irq < NR_IRQS; ++irq)
17811da177e4SLinus Torvalds 				if (irq_to_vector(irq) == cpe_vector) {
17821da177e4SLinus Torvalds 					desc = irq_descp(irq);
17831da177e4SLinus Torvalds 					desc->status |= IRQ_PER_CPU;
17841da177e4SLinus Torvalds 					setup_irq(irq, &mca_cpe_irqaction);
1785ff741906SAshok Raj 					ia64_cpe_irq = irq;
17861da177e4SLinus Torvalds 				}
17871da177e4SLinus Torvalds 			ia64_mca_register_cpev(cpe_vector);
17881da177e4SLinus Torvalds 			IA64_MCA_DEBUG("%s: CPEI/P setup and enabled.\n", __FUNCTION__);
17891da177e4SLinus Torvalds 		} else {
17901da177e4SLinus Torvalds 			/* If platform doesn't support CPEI, get the timer going. */
17911da177e4SLinus Torvalds 			if (cpe_poll_enabled) {
17921da177e4SLinus Torvalds 				ia64_mca_cpe_poll(0UL);
17931da177e4SLinus Torvalds 				IA64_MCA_DEBUG("%s: CPEP setup and enabled.\n", __FUNCTION__);
17941da177e4SLinus Torvalds 			}
17951da177e4SLinus Torvalds 		}
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds #endif
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	return 0;
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds device_initcall(ia64_mca_late_init);
1803