mca.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /*
  2. * File: mca.c
  3. * Purpose: Generic MCA handling layer
  4. *
  5. * Updated for latest kernel
  6. * Copyright (C) 2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. *
  9. * Copyright (C) 2002 Dell Inc.
  10. * Copyright (C) Matt Domsch (Matt_Domsch@dell.com)
  11. *
  12. * Copyright (C) 2002 Intel
  13. * Copyright (C) Jenna Hall (jenna.s.hall@intel.com)
  14. *
  15. * Copyright (C) 2001 Intel
  16. * Copyright (C) Fred Lewis (frederick.v.lewis@intel.com)
  17. *
  18. * Copyright (C) 2000 Intel
  19. * Copyright (C) Chuck Fleckenstein (cfleck@co.intel.com)
  20. *
  21. * Copyright (C) 1999, 2004 Silicon Graphics, Inc.
  22. * Copyright (C) Vijay Chander(vijay@engr.sgi.com)
  23. *
  24. * 03/04/15 D. Mosberger Added INIT backtrace support.
  25. * 02/03/25 M. Domsch GUID cleanups
  26. *
  27. * 02/01/04 J. Hall Aligned MCA stack to 16 bytes, added platform vs. CPU
  28. * error flag, set SAL default return values, changed
  29. * error record structure to linked list, added init call
  30. * to sal_get_state_info_size().
  31. *
  32. * 01/01/03 F. Lewis Added setup of CMCI and CPEI IRQs, logging of corrected
  33. * platform errors, completed code for logging of
  34. * corrected & uncorrected machine check errors, and
  35. * updated for conformance with Nov. 2000 revision of the
  36. * SAL 3.0 spec.
  37. * 00/03/29 C. Fleckenstein Fixed PAL/SAL update issues, began MCA bug fixes, logging issues,
  38. * added min save state dump, added INIT handler.
  39. *
  40. * 2003-12-08 Keith Owens <kaos@sgi.com>
  41. * smp_call_function() must not be called from interrupt context (can
  42. * deadlock on tasklist_lock). Use keventd to call smp_call_function().
  43. *
  44. * 2004-02-01 Keith Owens <kaos@sgi.com>
  45. * Avoid deadlock when using printk() for MCA and INIT records.
  46. * Delete all record printing code, moved to salinfo_decode in user space.
  47. * Mark variables and functions static where possible.
  48. * Delete dead variables and functions.
  49. * Reorder to remove the need for forward declarations and to consolidate
  50. * related code.
  51. *
  52. * 2005-08-12 Keith Owens <kaos@sgi.com>
  53. * Convert MCA/INIT handlers to use per event stacks and SAL/OS state.
  54. *
  55. * 2005-10-07 Keith Owens <kaos@sgi.com>
  56. * Add notify_die() hooks.
  57. *
  58. * 2006-09-15 Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
  59. * Add printing support for MCA/INIT.
  60. *
  61. * 2007-04-27 Russ Anderson <rja@sgi.com>
  62. * Support multiple cpus going through OS_MCA in the same event.
  63. */
  64. #include <linux/types.h>
  65. #include <linux/init.h>
  66. #include <linux/sched.h>
  67. #include <linux/interrupt.h>
  68. #include <linux/irq.h>
  69. #include <linux/bootmem.h>
  70. #include <linux/acpi.h>
  71. #include <linux/timer.h>
  72. #include <linux/module.h>
  73. #include <linux/kernel.h>
  74. #include <linux/smp.h>
  75. #include <linux/workqueue.h>
  76. #include <linux/cpumask.h>
  77. #include <linux/kdebug.h>
  78. #include <asm/delay.h>
  79. #include <asm/machvec.h>
  80. #include <asm/meminit.h>
  81. #include <asm/page.h>
  82. #include <asm/ptrace.h>
  83. #include <asm/system.h>
  84. #include <asm/sal.h>
  85. #include <asm/mca.h>
  86. #include <asm/kexec.h>
  87. #include <asm/irq.h>
  88. #include <asm/hw_irq.h>
  89. #include "mca_drv.h"
  90. #include "entry.h"
  91. #if defined(IA64_MCA_DEBUG_INFO)
  92. # define IA64_MCA_DEBUG(fmt...) printk(fmt)
  93. #else
  94. # define IA64_MCA_DEBUG(fmt...)
  95. #endif
  96. /* Used by mca_asm.S */
  97. DEFINE_PER_CPU(u64, ia64_mca_data); /* == __per_cpu_mca[smp_processor_id()] */
  98. DEFINE_PER_CPU(u64, ia64_mca_per_cpu_pte); /* PTE to map per-CPU area */
  99. DEFINE_PER_CPU(u64, ia64_mca_pal_pte); /* PTE to map PAL code */
  100. DEFINE_PER_CPU(u64, ia64_mca_pal_base); /* vaddr PAL code granule */
  101. unsigned long __per_cpu_mca[NR_CPUS];
  102. /* In mca_asm.S */
  103. extern void ia64_os_init_dispatch_monarch (void);
  104. extern void ia64_os_init_dispatch_slave (void);
  105. static int monarch_cpu = -1;
  106. static ia64_mc_info_t ia64_mc_info;
  107. #define MAX_CPE_POLL_INTERVAL (15*60*HZ) /* 15 minutes */
  108. #define MIN_CPE_POLL_INTERVAL (2*60*HZ) /* 2 minutes */
  109. #define CMC_POLL_INTERVAL (1*60*HZ) /* 1 minute */
  110. #define CPE_HISTORY_LENGTH 5
  111. #define CMC_HISTORY_LENGTH 5
  112. #ifdef CONFIG_ACPI
  113. static struct timer_list cpe_poll_timer;
  114. #endif
  115. static struct timer_list cmc_poll_timer;
  116. /*
  117. * This variable tells whether we are currently in polling mode.
  118. * Start with this in the wrong state so we won't play w/ timers
  119. * before the system is ready.
  120. */
  121. static int cmc_polling_enabled = 1;
  122. /*
  123. * Clearing this variable prevents CPE polling from getting activated
  124. * in mca_late_init. Use it if your system doesn't provide a CPEI,
  125. * but encounters problems retrieving CPE logs. This should only be
  126. * necessary for debugging.
  127. */
  128. static int cpe_poll_enabled = 1;
  129. extern void salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe);
  130. static int mca_init __initdata;
  131. /*
  132. * limited & delayed printing support for MCA/INIT handler
  133. */
  134. #define mprintk(fmt...) ia64_mca_printk(fmt)
  135. #define MLOGBUF_SIZE (512+256*NR_CPUS)
  136. #define MLOGBUF_MSGMAX 256
  137. static char mlogbuf[MLOGBUF_SIZE];
  138. static DEFINE_SPINLOCK(mlogbuf_wlock); /* mca context only */
  139. static DEFINE_SPINLOCK(mlogbuf_rlock); /* normal context only */
  140. static unsigned long mlogbuf_start;
  141. static unsigned long mlogbuf_end;
  142. static unsigned int mlogbuf_finished = 0;
  143. static unsigned long mlogbuf_timestamp = 0;
  144. static int loglevel_save = -1;
  145. #define BREAK_LOGLEVEL(__console_loglevel) \
  146. oops_in_progress = 1; \
  147. if (loglevel_save < 0) \
  148. loglevel_save = __console_loglevel; \
  149. __console_loglevel = 15;
  150. #define RESTORE_LOGLEVEL(__console_loglevel) \
  151. if (loglevel_save >= 0) { \
  152. __console_loglevel = loglevel_save; \
  153. loglevel_save = -1; \
  154. } \
  155. mlogbuf_finished = 0; \
  156. oops_in_progress = 0;
  157. /*
  158. * Push messages into buffer, print them later if not urgent.
  159. */
  160. void ia64_mca_printk(const char *fmt, ...)
  161. {
  162. va_list args;
  163. int printed_len;
  164. char temp_buf[MLOGBUF_MSGMAX];
  165. char *p;
  166. va_start(args, fmt);
  167. printed_len = vscnprintf(temp_buf, sizeof(temp_buf), fmt, args);
  168. va_end(args);
  169. /* Copy the output into mlogbuf */
  170. if (oops_in_progress) {
  171. /* mlogbuf was abandoned, use printk directly instead. */
  172. printk(temp_buf);
  173. } else {
  174. spin_lock(&mlogbuf_wlock);
  175. for (p = temp_buf; *p; p++) {
  176. unsigned long next = (mlogbuf_end + 1) % MLOGBUF_SIZE;
  177. if (next != mlogbuf_start) {
  178. mlogbuf[mlogbuf_end] = *p;
  179. mlogbuf_end = next;
  180. } else {
  181. /* buffer full */
  182. break;
  183. }
  184. }
  185. mlogbuf[mlogbuf_end] = '\0';
  186. spin_unlock(&mlogbuf_wlock);
  187. }
  188. }
  189. EXPORT_SYMBOL(ia64_mca_printk);
  190. /*
  191. * Print buffered messages.
  192. * NOTE: call this after returning normal context. (ex. from salinfod)
  193. */
  194. void ia64_mlogbuf_dump(void)
  195. {
  196. char temp_buf[MLOGBUF_MSGMAX];
  197. char *p;
  198. unsigned long index;
  199. unsigned long flags;
  200. unsigned int printed_len;
  201. /* Get output from mlogbuf */
  202. while (mlogbuf_start != mlogbuf_end) {
  203. temp_buf[0] = '\0';
  204. p = temp_buf;
  205. printed_len = 0;
  206. spin_lock_irqsave(&mlogbuf_rlock, flags);
  207. index = mlogbuf_start;
  208. while (index != mlogbuf_end) {
  209. *p = mlogbuf[index];
  210. index = (index + 1) % MLOGBUF_SIZE;
  211. if (!*p)
  212. break;
  213. p++;
  214. if (++printed_len >= MLOGBUF_MSGMAX - 1)
  215. break;
  216. }
  217. *p = '\0';
  218. if (temp_buf[0])
  219. printk(temp_buf);
  220. mlogbuf_start = index;
  221. mlogbuf_timestamp = 0;
  222. spin_unlock_irqrestore(&mlogbuf_rlock, flags);
  223. }
  224. }
  225. EXPORT_SYMBOL(ia64_mlogbuf_dump);
  226. /*
  227. * Call this if system is going to down or if immediate flushing messages to
  228. * console is required. (ex. recovery was failed, crash dump is going to be
  229. * invoked, long-wait rendezvous etc.)
  230. * NOTE: this should be called from monarch.
  231. */
  232. static void ia64_mlogbuf_finish(int wait)
  233. {
  234. BREAK_LOGLEVEL(console_loglevel);
  235. spin_lock_init(&mlogbuf_rlock);
  236. ia64_mlogbuf_dump();
  237. printk(KERN_EMERG "mlogbuf_finish: printing switched to urgent mode, "
  238. "MCA/INIT might be dodgy or fail.\n");
  239. if (!wait)
  240. return;
  241. /* wait for console */
  242. printk("Delaying for 5 seconds...\n");
  243. udelay(5*1000000);
  244. mlogbuf_finished = 1;
  245. }
  246. /*
  247. * Print buffered messages from INIT context.
  248. */
  249. static void ia64_mlogbuf_dump_from_init(void)
  250. {
  251. if (mlogbuf_finished)
  252. return;
  253. if (mlogbuf_timestamp && (mlogbuf_timestamp + 30*HZ > jiffies)) {
  254. printk(KERN_ERR "INIT: mlogbuf_dump is interrupted by INIT "
  255. " and the system seems to be messed up.\n");
  256. ia64_mlogbuf_finish(0);
  257. return;
  258. }
  259. if (!spin_trylock(&mlogbuf_rlock)) {
  260. printk(KERN_ERR "INIT: mlogbuf_dump is interrupted by INIT. "
  261. "Generated messages other than stack dump will be "
  262. "buffered to mlogbuf and will be printed later.\n");
  263. printk(KERN_ERR "INIT: If messages would not printed after "
  264. "this INIT, wait 30sec and assert INIT again.\n");
  265. if (!mlogbuf_timestamp)
  266. mlogbuf_timestamp = jiffies;
  267. return;
  268. }
  269. spin_unlock(&mlogbuf_rlock);
  270. ia64_mlogbuf_dump();
  271. }
  272. static void inline
  273. ia64_mca_spin(const char *func)
  274. {
  275. if (monarch_cpu == smp_processor_id())
  276. ia64_mlogbuf_finish(0);
  277. mprintk(KERN_EMERG "%s: spinning here, not returning to SAL\n", func);
  278. while (1)
  279. cpu_relax();
  280. }
  281. /*
  282. * IA64_MCA log support
  283. */
  284. #define IA64_MAX_LOGS 2 /* Double-buffering for nested MCAs */
  285. #define IA64_MAX_LOG_TYPES 4 /* MCA, INIT, CMC, CPE */
  286. typedef struct ia64_state_log_s
  287. {
  288. spinlock_t isl_lock;
  289. int isl_index;
  290. unsigned long isl_count;
  291. ia64_err_rec_t *isl_log[IA64_MAX_LOGS]; /* need space to store header + error log */
  292. } ia64_state_log_t;
  293. static ia64_state_log_t ia64_state_log[IA64_MAX_LOG_TYPES];
  294. #define IA64_LOG_ALLOCATE(it, size) \
  295. {ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)] = \
  296. (ia64_err_rec_t *)alloc_bootmem(size); \
  297. ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)] = \
  298. (ia64_err_rec_t *)alloc_bootmem(size);}
  299. #define IA64_LOG_LOCK_INIT(it) spin_lock_init(&ia64_state_log[it].isl_lock)
  300. #define IA64_LOG_LOCK(it) spin_lock_irqsave(&ia64_state_log[it].isl_lock, s)
  301. #define IA64_LOG_UNLOCK(it) spin_unlock_irqrestore(&ia64_state_log[it].isl_lock,s)
  302. #define IA64_LOG_NEXT_INDEX(it) ia64_state_log[it].isl_index
  303. #define IA64_LOG_CURR_INDEX(it) 1 - ia64_state_log[it].isl_index
  304. #define IA64_LOG_INDEX_INC(it) \
  305. {ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index; \
  306. ia64_state_log[it].isl_count++;}
  307. #define IA64_LOG_INDEX_DEC(it) \
  308. ia64_state_log[it].isl_index = 1 - ia64_state_log[it].isl_index
  309. #define IA64_LOG_NEXT_BUFFER(it) (void *)((ia64_state_log[it].isl_log[IA64_LOG_NEXT_INDEX(it)]))
  310. #define IA64_LOG_CURR_BUFFER(it) (void *)((ia64_state_log[it].isl_log[IA64_LOG_CURR_INDEX(it)]))
  311. #define IA64_LOG_COUNT(it) ia64_state_log[it].isl_count
  312. /*
  313. * ia64_log_init
  314. * Reset the OS ia64 log buffer
  315. * Inputs : info_type (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
  316. * Outputs : None
  317. */
  318. static void __init
  319. ia64_log_init(int sal_info_type)
  320. {
  321. u64 max_size = 0;
  322. IA64_LOG_NEXT_INDEX(sal_info_type) = 0;
  323. IA64_LOG_LOCK_INIT(sal_info_type);
  324. // SAL will tell us the maximum size of any error record of this type
  325. max_size = ia64_sal_get_state_info_size(sal_info_type);
  326. if (!max_size)
  327. /* alloc_bootmem() doesn't like zero-sized allocations! */
  328. return;
  329. // set up OS data structures to hold error info
  330. IA64_LOG_ALLOCATE(sal_info_type, max_size);
  331. memset(IA64_LOG_CURR_BUFFER(sal_info_type), 0, max_size);
  332. memset(IA64_LOG_NEXT_BUFFER(sal_info_type), 0, max_size);
  333. }
  334. /*
  335. * ia64_log_get
  336. *
  337. * Get the current MCA log from SAL and copy it into the OS log buffer.
  338. *
  339. * Inputs : info_type (SAL_INFO_TYPE_{MCA,INIT,CMC,CPE})
  340. * irq_safe whether you can use printk at this point
  341. * Outputs : size (total record length)
  342. * *buffer (ptr to error record)
  343. *
  344. */
  345. static u64
  346. ia64_log_get(int sal_info_type, u8 **buffer, int irq_safe)
  347. {
  348. sal_log_record_header_t *log_buffer;
  349. u64 total_len = 0;
  350. unsigned long s;
  351. IA64_LOG_LOCK(sal_info_type);
  352. /* Get the process state information */
  353. log_buffer = IA64_LOG_NEXT_BUFFER(sal_info_type);
  354. total_len = ia64_sal_get_state_info(sal_info_type, (u64 *)log_buffer);
  355. if (total_len) {
  356. IA64_LOG_INDEX_INC(sal_info_type);
  357. IA64_LOG_UNLOCK(sal_info_type);
  358. if (irq_safe) {
  359. IA64_MCA_DEBUG("%s: SAL error record type %d retrieved. "
  360. "Record length = %ld\n", __FUNCTION__, sal_info_type, total_len);
  361. }
  362. *buffer = (u8 *) log_buffer;
  363. return total_len;
  364. } else {
  365. IA64_LOG_UNLOCK(sal_info_type);
  366. return 0;
  367. }
  368. }
  369. /*
  370. * ia64_mca_log_sal_error_record
  371. *
  372. * This function retrieves a specified error record type from SAL
  373. * and wakes up any processes waiting for error records.
  374. *
  375. * Inputs : sal_info_type (Type of error record MCA/CMC/CPE)
  376. * FIXME: remove MCA and irq_safe.
  377. */
  378. static void
  379. ia64_mca_log_sal_error_record(int sal_info_type)
  380. {
  381. u8 *buffer;
  382. sal_log_record_header_t *rh;
  383. u64 size;
  384. int irq_safe = sal_info_type != SAL_INFO_TYPE_MCA;
  385. #ifdef IA64_MCA_DEBUG_INFO
  386. static const char * const rec_name[] = { "MCA", "INIT", "CMC", "CPE" };
  387. #endif
  388. size = ia64_log_get(sal_info_type, &buffer, irq_safe);
  389. if (!size)
  390. return;
  391. salinfo_log_wakeup(sal_info_type, buffer, size, irq_safe);
  392. if (irq_safe)
  393. IA64_MCA_DEBUG("CPU %d: SAL log contains %s error record\n",
  394. smp_processor_id(),
  395. sal_info_type < ARRAY_SIZE(rec_name) ? rec_name[sal_info_type] : "UNKNOWN");
  396. /* Clear logs from corrected errors in case there's no user-level logger */
  397. rh = (sal_log_record_header_t *)buffer;
  398. if (rh->severity == sal_log_severity_corrected)
  399. ia64_sal_clear_state_info(sal_info_type);
  400. }
  401. /*
  402. * search_mca_table
  403. * See if the MCA surfaced in an instruction range
  404. * that has been tagged as recoverable.
  405. *
  406. * Inputs
  407. * first First address range to check
  408. * last Last address range to check
  409. * ip Instruction pointer, address we are looking for
  410. *
  411. * Return value:
  412. * 1 on Success (in the table)/ 0 on Failure (not in the table)
  413. */
  414. int
  415. search_mca_table (const struct mca_table_entry *first,
  416. const struct mca_table_entry *last,
  417. unsigned long ip)
  418. {
  419. const struct mca_table_entry *curr;
  420. u64 curr_start, curr_end;
  421. curr = first;
  422. while (curr <= last) {
  423. curr_start = (u64) &curr->start_addr + curr->start_addr;
  424. curr_end = (u64) &curr->end_addr + curr->end_addr;
  425. if ((ip >= curr_start) && (ip <= curr_end)) {
  426. return 1;
  427. }
  428. curr++;
  429. }
  430. return 0;
  431. }
  432. /* Given an address, look for it in the mca tables. */
  433. int mca_recover_range(unsigned long addr)
  434. {
  435. extern struct mca_table_entry __start___mca_table[];
  436. extern struct mca_table_entry __stop___mca_table[];
  437. return search_mca_table(__start___mca_table, __stop___mca_table-1, addr);
  438. }
  439. EXPORT_SYMBOL_GPL(mca_recover_range);
  440. #ifdef CONFIG_ACPI
  441. int cpe_vector = -1;
  442. int ia64_cpe_irq = -1;
  443. static irqreturn_t
  444. ia64_mca_cpe_int_handler (int cpe_irq, void *arg)
  445. {
  446. static unsigned long cpe_history[CPE_HISTORY_LENGTH];
  447. static int index;
  448. static DEFINE_SPINLOCK(cpe_history_lock);
  449. IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
  450. __FUNCTION__, cpe_irq, smp_processor_id());
  451. /* SAL spec states this should run w/ interrupts enabled */
  452. local_irq_enable();
  453. spin_lock(&cpe_history_lock);
  454. if (!cpe_poll_enabled && cpe_vector >= 0) {
  455. int i, count = 1; /* we know 1 happened now */
  456. unsigned long now = jiffies;
  457. for (i = 0; i < CPE_HISTORY_LENGTH; i++) {
  458. if (now - cpe_history[i] <= HZ)
  459. count++;
  460. }
  461. IA64_MCA_DEBUG(KERN_INFO "CPE threshold %d/%d\n", count, CPE_HISTORY_LENGTH);
  462. if (count >= CPE_HISTORY_LENGTH) {
  463. cpe_poll_enabled = 1;
  464. spin_unlock(&cpe_history_lock);
  465. disable_irq_nosync(local_vector_to_irq(IA64_CPE_VECTOR));
  466. /*
  467. * Corrected errors will still be corrected, but
  468. * make sure there's a log somewhere that indicates
  469. * something is generating more than we can handle.
  470. */
  471. printk(KERN_WARNING "WARNING: Switching to polling CPE handler; error records may be lost\n");
  472. mod_timer(&cpe_poll_timer, jiffies + MIN_CPE_POLL_INTERVAL);
  473. /* lock already released, get out now */
  474. goto out;
  475. } else {
  476. cpe_history[index++] = now;
  477. if (index == CPE_HISTORY_LENGTH)
  478. index = 0;
  479. }
  480. }
  481. spin_unlock(&cpe_history_lock);
  482. out:
  483. /* Get the CPE error record and log it */
  484. ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CPE);
  485. return IRQ_HANDLED;
  486. }
  487. #endif /* CONFIG_ACPI */
  488. #ifdef CONFIG_ACPI
  489. /*
  490. * ia64_mca_register_cpev
  491. *
  492. * Register the corrected platform error vector with SAL.
  493. *
  494. * Inputs
  495. * cpev Corrected Platform Error Vector number
  496. *
  497. * Outputs
  498. * None
  499. */
  500. static void __init
  501. ia64_mca_register_cpev (int cpev)
  502. {
  503. /* Register the CPE interrupt vector with SAL */
  504. struct ia64_sal_retval isrv;
  505. isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_CPE_INT, SAL_MC_PARAM_MECHANISM_INT, cpev, 0, 0);
  506. if (isrv.status) {
  507. printk(KERN_ERR "Failed to register Corrected Platform "
  508. "Error interrupt vector with SAL (status %ld)\n", isrv.status);
  509. return;
  510. }
  511. IA64_MCA_DEBUG("%s: corrected platform error "
  512. "vector %#x registered\n", __FUNCTION__, cpev);
  513. }
  514. #endif /* CONFIG_ACPI */
  515. /*
  516. * ia64_mca_cmc_vector_setup
  517. *
  518. * Setup the corrected machine check vector register in the processor.
  519. * (The interrupt is masked on boot. ia64_mca_late_init unmask this.)
  520. * This function is invoked on a per-processor basis.
  521. *
  522. * Inputs
  523. * None
  524. *
  525. * Outputs
  526. * None
  527. */
  528. void __cpuinit
  529. ia64_mca_cmc_vector_setup (void)
  530. {
  531. cmcv_reg_t cmcv;
  532. cmcv.cmcv_regval = 0;
  533. cmcv.cmcv_mask = 1; /* Mask/disable interrupt at first */
  534. cmcv.cmcv_vector = IA64_CMC_VECTOR;
  535. ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
  536. IA64_MCA_DEBUG("%s: CPU %d corrected "
  537. "machine check vector %#x registered.\n",
  538. __FUNCTION__, smp_processor_id(), IA64_CMC_VECTOR);
  539. IA64_MCA_DEBUG("%s: CPU %d CMCV = %#016lx\n",
  540. __FUNCTION__, smp_processor_id(), ia64_getreg(_IA64_REG_CR_CMCV));
  541. }
  542. /*
  543. * ia64_mca_cmc_vector_disable
  544. *
  545. * Mask the corrected machine check vector register in the processor.
  546. * This function is invoked on a per-processor basis.
  547. *
  548. * Inputs
  549. * dummy(unused)
  550. *
  551. * Outputs
  552. * None
  553. */
  554. static void
  555. ia64_mca_cmc_vector_disable (void *dummy)
  556. {
  557. cmcv_reg_t cmcv;
  558. cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
  559. cmcv.cmcv_mask = 1; /* Mask/disable interrupt */
  560. ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
  561. IA64_MCA_DEBUG("%s: CPU %d corrected "
  562. "machine check vector %#x disabled.\n",
  563. __FUNCTION__, smp_processor_id(), cmcv.cmcv_vector);
  564. }
  565. /*
  566. * ia64_mca_cmc_vector_enable
  567. *
  568. * Unmask the corrected machine check vector register in the processor.
  569. * This function is invoked on a per-processor basis.
  570. *
  571. * Inputs
  572. * dummy(unused)
  573. *
  574. * Outputs
  575. * None
  576. */
  577. static void
  578. ia64_mca_cmc_vector_enable (void *dummy)
  579. {
  580. cmcv_reg_t cmcv;
  581. cmcv.cmcv_regval = ia64_getreg(_IA64_REG_CR_CMCV);
  582. cmcv.cmcv_mask = 0; /* Unmask/enable interrupt */
  583. ia64_setreg(_IA64_REG_CR_CMCV, cmcv.cmcv_regval);
  584. IA64_MCA_DEBUG("%s: CPU %d corrected "
  585. "machine check vector %#x enabled.\n",
  586. __FUNCTION__, smp_processor_id(), cmcv.cmcv_vector);
  587. }
  588. /*
  589. * ia64_mca_cmc_vector_disable_keventd
  590. *
  591. * Called via keventd (smp_call_function() is not safe in interrupt context) to
  592. * disable the cmc interrupt vector.
  593. */
  594. static void
  595. ia64_mca_cmc_vector_disable_keventd(struct work_struct *unused)
  596. {
  597. on_each_cpu(ia64_mca_cmc_vector_disable, NULL, 1, 0);
  598. }
  599. /*
  600. * ia64_mca_cmc_vector_enable_keventd
  601. *
  602. * Called via keventd (smp_call_function() is not safe in interrupt context) to
  603. * enable the cmc interrupt vector.
  604. */
  605. static void
  606. ia64_mca_cmc_vector_enable_keventd(struct work_struct *unused)
  607. {
  608. on_each_cpu(ia64_mca_cmc_vector_enable, NULL, 1, 0);
  609. }
  610. /*
  611. * ia64_mca_wakeup
  612. *
  613. * Send an inter-cpu interrupt to wake-up a particular cpu.
  614. *
  615. * Inputs : cpuid
  616. * Outputs : None
  617. */
  618. static void
  619. ia64_mca_wakeup(int cpu)
  620. {
  621. platform_send_ipi(cpu, IA64_MCA_WAKEUP_VECTOR, IA64_IPI_DM_INT, 0);
  622. }
  623. /*
  624. * ia64_mca_wakeup_all
  625. *
  626. * Wakeup all the slave cpus which have rendez'ed previously.
  627. *
  628. * Inputs : None
  629. * Outputs : None
  630. */
  631. static void
  632. ia64_mca_wakeup_all(void)
  633. {
  634. int cpu;
  635. /* Clear the Rendez checkin flag for all cpus */
  636. for_each_online_cpu(cpu) {
  637. if (ia64_mc_info.imi_rendez_checkin[cpu] == IA64_MCA_RENDEZ_CHECKIN_DONE)
  638. ia64_mca_wakeup(cpu);
  639. }
  640. }
  641. /*
  642. * ia64_mca_rendez_interrupt_handler
  643. *
  644. * This is handler used to put slave processors into spinloop
  645. * while the monarch processor does the mca handling and later
  646. * wake each slave up once the monarch is done. The state
  647. * IA64_MCA_RENDEZ_CHECKIN_DONE indicates the cpu is rendez'ed
  648. * in SAL. The state IA64_MCA_RENDEZ_CHECKIN_NOTDONE indicates
  649. * the cpu has come out of OS rendezvous.
  650. *
  651. * Inputs : None
  652. * Outputs : None
  653. */
  654. static irqreturn_t
  655. ia64_mca_rendez_int_handler(int rendez_irq, void *arg)
  656. {
  657. unsigned long flags;
  658. int cpu = smp_processor_id();
  659. struct ia64_mca_notify_die nd =
  660. { .sos = NULL, .monarch_cpu = &monarch_cpu };
  661. /* Mask all interrupts */
  662. local_irq_save(flags);
  663. if (notify_die(DIE_MCA_RENDZVOUS_ENTER, "MCA", get_irq_regs(),
  664. (long)&nd, 0, 0) == NOTIFY_STOP)
  665. ia64_mca_spin(__FUNCTION__);
  666. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_DONE;
  667. /* Register with the SAL monarch that the slave has
  668. * reached SAL
  669. */
  670. ia64_sal_mc_rendez();
  671. if (notify_die(DIE_MCA_RENDZVOUS_PROCESS, "MCA", get_irq_regs(),
  672. (long)&nd, 0, 0) == NOTIFY_STOP)
  673. ia64_mca_spin(__FUNCTION__);
  674. /* Wait for the monarch cpu to exit. */
  675. while (monarch_cpu != -1)
  676. cpu_relax(); /* spin until monarch leaves */
  677. if (notify_die(DIE_MCA_RENDZVOUS_LEAVE, "MCA", get_irq_regs(),
  678. (long)&nd, 0, 0) == NOTIFY_STOP)
  679. ia64_mca_spin(__FUNCTION__);
  680. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
  681. /* Enable all interrupts */
  682. local_irq_restore(flags);
  683. return IRQ_HANDLED;
  684. }
  685. /*
  686. * ia64_mca_wakeup_int_handler
  687. *
  688. * The interrupt handler for processing the inter-cpu interrupt to the
  689. * slave cpu which was spinning in the rendez loop.
  690. * Since this spinning is done by turning off the interrupts and
  691. * polling on the wakeup-interrupt bit in the IRR, there is
  692. * nothing useful to be done in the handler.
  693. *
  694. * Inputs : wakeup_irq (Wakeup-interrupt bit)
  695. * arg (Interrupt handler specific argument)
  696. * Outputs : None
  697. *
  698. */
  699. static irqreturn_t
  700. ia64_mca_wakeup_int_handler(int wakeup_irq, void *arg)
  701. {
  702. return IRQ_HANDLED;
  703. }
  704. /* Function pointer for extra MCA recovery */
  705. int (*ia64_mca_ucmc_extension)
  706. (void*,struct ia64_sal_os_state*)
  707. = NULL;
  708. int
  709. ia64_reg_MCA_extension(int (*fn)(void *, struct ia64_sal_os_state *))
  710. {
  711. if (ia64_mca_ucmc_extension)
  712. return 1;
  713. ia64_mca_ucmc_extension = fn;
  714. return 0;
  715. }
  716. void
  717. ia64_unreg_MCA_extension(void)
  718. {
  719. if (ia64_mca_ucmc_extension)
  720. ia64_mca_ucmc_extension = NULL;
  721. }
  722. EXPORT_SYMBOL(ia64_reg_MCA_extension);
  723. EXPORT_SYMBOL(ia64_unreg_MCA_extension);
  724. static inline void
  725. copy_reg(const u64 *fr, u64 fnat, u64 *tr, u64 *tnat)
  726. {
  727. u64 fslot, tslot, nat;
  728. *tr = *fr;
  729. fslot = ((unsigned long)fr >> 3) & 63;
  730. tslot = ((unsigned long)tr >> 3) & 63;
  731. *tnat &= ~(1UL << tslot);
  732. nat = (fnat >> fslot) & 1;
  733. *tnat |= (nat << tslot);
  734. }
  735. /* Change the comm field on the MCA/INT task to include the pid that
  736. * was interrupted, it makes for easier debugging. If that pid was 0
  737. * (swapper or nested MCA/INIT) then use the start of the previous comm
  738. * field suffixed with its cpu.
  739. */
  740. static void
  741. ia64_mca_modify_comm(const struct task_struct *previous_current)
  742. {
  743. char *p, comm[sizeof(current->comm)];
  744. if (previous_current->pid)
  745. snprintf(comm, sizeof(comm), "%s %d",
  746. current->comm, previous_current->pid);
  747. else {
  748. int l;
  749. if ((p = strchr(previous_current->comm, ' ')))
  750. l = p - previous_current->comm;
  751. else
  752. l = strlen(previous_current->comm);
  753. snprintf(comm, sizeof(comm), "%s %*s %d",
  754. current->comm, l, previous_current->comm,
  755. task_thread_info(previous_current)->cpu);
  756. }
  757. memcpy(current->comm, comm, sizeof(current->comm));
  758. }
  759. /* On entry to this routine, we are running on the per cpu stack, see
  760. * mca_asm.h. The original stack has not been touched by this event. Some of
  761. * the original stack's registers will be in the RBS on this stack. This stack
  762. * also contains a partial pt_regs and switch_stack, the rest of the data is in
  763. * PAL minstate.
  764. *
  765. * The first thing to do is modify the original stack to look like a blocked
  766. * task so we can run backtrace on the original task. Also mark the per cpu
  767. * stack as current to ensure that we use the correct task state, it also means
  768. * that we can do backtrace on the MCA/INIT handler code itself.
  769. */
  770. static struct task_struct *
  771. ia64_mca_modify_original_stack(struct pt_regs *regs,
  772. const struct switch_stack *sw,
  773. struct ia64_sal_os_state *sos,
  774. const char *type)
  775. {
  776. char *p;
  777. ia64_va va;
  778. extern char ia64_leave_kernel[]; /* Need asm address, not function descriptor */
  779. const pal_min_state_area_t *ms = sos->pal_min_state;
  780. struct task_struct *previous_current;
  781. struct pt_regs *old_regs;
  782. struct switch_stack *old_sw;
  783. unsigned size = sizeof(struct pt_regs) +
  784. sizeof(struct switch_stack) + 16;
  785. u64 *old_bspstore, *old_bsp;
  786. u64 *new_bspstore, *new_bsp;
  787. u64 old_unat, old_rnat, new_rnat, nat;
  788. u64 slots, loadrs = regs->loadrs;
  789. u64 r12 = ms->pmsa_gr[12-1], r13 = ms->pmsa_gr[13-1];
  790. u64 ar_bspstore = regs->ar_bspstore;
  791. u64 ar_bsp = regs->ar_bspstore + (loadrs >> 16);
  792. const u64 *bank;
  793. const char *msg;
  794. int cpu = smp_processor_id();
  795. previous_current = curr_task(cpu);
  796. set_curr_task(cpu, current);
  797. if ((p = strchr(current->comm, ' ')))
  798. *p = '\0';
  799. /* Best effort attempt to cope with MCA/INIT delivered while in
  800. * physical mode.
  801. */
  802. regs->cr_ipsr = ms->pmsa_ipsr;
  803. if (ia64_psr(regs)->dt == 0) {
  804. va.l = r12;
  805. if (va.f.reg == 0) {
  806. va.f.reg = 7;
  807. r12 = va.l;
  808. }
  809. va.l = r13;
  810. if (va.f.reg == 0) {
  811. va.f.reg = 7;
  812. r13 = va.l;
  813. }
  814. }
  815. if (ia64_psr(regs)->rt == 0) {
  816. va.l = ar_bspstore;
  817. if (va.f.reg == 0) {
  818. va.f.reg = 7;
  819. ar_bspstore = va.l;
  820. }
  821. va.l = ar_bsp;
  822. if (va.f.reg == 0) {
  823. va.f.reg = 7;
  824. ar_bsp = va.l;
  825. }
  826. }
  827. /* mca_asm.S ia64_old_stack() cannot assume that the dirty registers
  828. * have been copied to the old stack, the old stack may fail the
  829. * validation tests below. So ia64_old_stack() must restore the dirty
  830. * registers from the new stack. The old and new bspstore probably
  831. * have different alignments, so loadrs calculated on the old bsp
  832. * cannot be used to restore from the new bsp. Calculate a suitable
  833. * loadrs for the new stack and save it in the new pt_regs, where
  834. * ia64_old_stack() can get it.
  835. */
  836. old_bspstore = (u64 *)ar_bspstore;
  837. old_bsp = (u64 *)ar_bsp;
  838. slots = ia64_rse_num_regs(old_bspstore, old_bsp);
  839. new_bspstore = (u64 *)((u64)current + IA64_RBS_OFFSET);
  840. new_bsp = ia64_rse_skip_regs(new_bspstore, slots);
  841. regs->loadrs = (new_bsp - new_bspstore) * 8 << 16;
  842. /* Verify the previous stack state before we change it */
  843. if (user_mode(regs)) {
  844. msg = "occurred in user space";
  845. /* previous_current is guaranteed to be valid when the task was
  846. * in user space, so ...
  847. */
  848. ia64_mca_modify_comm(previous_current);
  849. goto no_mod;
  850. }
  851. if (r13 != sos->prev_IA64_KR_CURRENT) {
  852. msg = "inconsistent previous current and r13";
  853. goto no_mod;
  854. }
  855. if (!mca_recover_range(ms->pmsa_iip)) {
  856. if ((r12 - r13) >= KERNEL_STACK_SIZE) {
  857. msg = "inconsistent r12 and r13";
  858. goto no_mod;
  859. }
  860. if ((ar_bspstore - r13) >= KERNEL_STACK_SIZE) {
  861. msg = "inconsistent ar.bspstore and r13";
  862. goto no_mod;
  863. }
  864. va.p = old_bspstore;
  865. if (va.f.reg < 5) {
  866. msg = "old_bspstore is in the wrong region";
  867. goto no_mod;
  868. }
  869. if ((ar_bsp - r13) >= KERNEL_STACK_SIZE) {
  870. msg = "inconsistent ar.bsp and r13";
  871. goto no_mod;
  872. }
  873. size += (ia64_rse_skip_regs(old_bspstore, slots) - old_bspstore) * 8;
  874. if (ar_bspstore + size > r12) {
  875. msg = "no room for blocked state";
  876. goto no_mod;
  877. }
  878. }
  879. ia64_mca_modify_comm(previous_current);
  880. /* Make the original task look blocked. First stack a struct pt_regs,
  881. * describing the state at the time of interrupt. mca_asm.S built a
  882. * partial pt_regs, copy it and fill in the blanks using minstate.
  883. */
  884. p = (char *)r12 - sizeof(*regs);
  885. old_regs = (struct pt_regs *)p;
  886. memcpy(old_regs, regs, sizeof(*regs));
  887. /* If ipsr.ic then use pmsa_{iip,ipsr,ifs}, else use
  888. * pmsa_{xip,xpsr,xfs}
  889. */
  890. if (ia64_psr(regs)->ic) {
  891. old_regs->cr_iip = ms->pmsa_iip;
  892. old_regs->cr_ipsr = ms->pmsa_ipsr;
  893. old_regs->cr_ifs = ms->pmsa_ifs;
  894. } else {
  895. old_regs->cr_iip = ms->pmsa_xip;
  896. old_regs->cr_ipsr = ms->pmsa_xpsr;
  897. old_regs->cr_ifs = ms->pmsa_xfs;
  898. }
  899. old_regs->pr = ms->pmsa_pr;
  900. old_regs->b0 = ms->pmsa_br0;
  901. old_regs->loadrs = loadrs;
  902. old_regs->ar_rsc = ms->pmsa_rsc;
  903. old_unat = old_regs->ar_unat;
  904. copy_reg(&ms->pmsa_gr[1-1], ms->pmsa_nat_bits, &old_regs->r1, &old_unat);
  905. copy_reg(&ms->pmsa_gr[2-1], ms->pmsa_nat_bits, &old_regs->r2, &old_unat);
  906. copy_reg(&ms->pmsa_gr[3-1], ms->pmsa_nat_bits, &old_regs->r3, &old_unat);
  907. copy_reg(&ms->pmsa_gr[8-1], ms->pmsa_nat_bits, &old_regs->r8, &old_unat);
  908. copy_reg(&ms->pmsa_gr[9-1], ms->pmsa_nat_bits, &old_regs->r9, &old_unat);
  909. copy_reg(&ms->pmsa_gr[10-1], ms->pmsa_nat_bits, &old_regs->r10, &old_unat);
  910. copy_reg(&ms->pmsa_gr[11-1], ms->pmsa_nat_bits, &old_regs->r11, &old_unat);
  911. copy_reg(&ms->pmsa_gr[12-1], ms->pmsa_nat_bits, &old_regs->r12, &old_unat);
  912. copy_reg(&ms->pmsa_gr[13-1], ms->pmsa_nat_bits, &old_regs->r13, &old_unat);
  913. copy_reg(&ms->pmsa_gr[14-1], ms->pmsa_nat_bits, &old_regs->r14, &old_unat);
  914. copy_reg(&ms->pmsa_gr[15-1], ms->pmsa_nat_bits, &old_regs->r15, &old_unat);
  915. if (ia64_psr(old_regs)->bn)
  916. bank = ms->pmsa_bank1_gr;
  917. else
  918. bank = ms->pmsa_bank0_gr;
  919. copy_reg(&bank[16-16], ms->pmsa_nat_bits, &old_regs->r16, &old_unat);
  920. copy_reg(&bank[17-16], ms->pmsa_nat_bits, &old_regs->r17, &old_unat);
  921. copy_reg(&bank[18-16], ms->pmsa_nat_bits, &old_regs->r18, &old_unat);
  922. copy_reg(&bank[19-16], ms->pmsa_nat_bits, &old_regs->r19, &old_unat);
  923. copy_reg(&bank[20-16], ms->pmsa_nat_bits, &old_regs->r20, &old_unat);
  924. copy_reg(&bank[21-16], ms->pmsa_nat_bits, &old_regs->r21, &old_unat);
  925. copy_reg(&bank[22-16], ms->pmsa_nat_bits, &old_regs->r22, &old_unat);
  926. copy_reg(&bank[23-16], ms->pmsa_nat_bits, &old_regs->r23, &old_unat);
  927. copy_reg(&bank[24-16], ms->pmsa_nat_bits, &old_regs->r24, &old_unat);
  928. copy_reg(&bank[25-16], ms->pmsa_nat_bits, &old_regs->r25, &old_unat);
  929. copy_reg(&bank[26-16], ms->pmsa_nat_bits, &old_regs->r26, &old_unat);
  930. copy_reg(&bank[27-16], ms->pmsa_nat_bits, &old_regs->r27, &old_unat);
  931. copy_reg(&bank[28-16], ms->pmsa_nat_bits, &old_regs->r28, &old_unat);
  932. copy_reg(&bank[29-16], ms->pmsa_nat_bits, &old_regs->r29, &old_unat);
  933. copy_reg(&bank[30-16], ms->pmsa_nat_bits, &old_regs->r30, &old_unat);
  934. copy_reg(&bank[31-16], ms->pmsa_nat_bits, &old_regs->r31, &old_unat);
  935. /* Next stack a struct switch_stack. mca_asm.S built a partial
  936. * switch_stack, copy it and fill in the blanks using pt_regs and
  937. * minstate.
  938. *
  939. * In the synthesized switch_stack, b0 points to ia64_leave_kernel,
  940. * ar.pfs is set to 0.
  941. *
  942. * unwind.c::unw_unwind() does special processing for interrupt frames.
  943. * It checks if the PRED_NON_SYSCALL predicate is set, if the predicate
  944. * is clear then unw_unwind() does _not_ adjust bsp over pt_regs. Not
  945. * that this is documented, of course. Set PRED_NON_SYSCALL in the
  946. * switch_stack on the original stack so it will unwind correctly when
  947. * unwind.c reads pt_regs.
  948. *
  949. * thread.ksp is updated to point to the synthesized switch_stack.
  950. */
  951. p -= sizeof(struct switch_stack);
  952. old_sw = (struct switch_stack *)p;
  953. memcpy(old_sw, sw, sizeof(*sw));
  954. old_sw->caller_unat = old_unat;
  955. old_sw->ar_fpsr = old_regs->ar_fpsr;
  956. copy_reg(&ms->pmsa_gr[4-1], ms->pmsa_nat_bits, &old_sw->r4, &old_unat);
  957. copy_reg(&ms->pmsa_gr[5-1], ms->pmsa_nat_bits, &old_sw->r5, &old_unat);
  958. copy_reg(&ms->pmsa_gr[6-1], ms->pmsa_nat_bits, &old_sw->r6, &old_unat);
  959. copy_reg(&ms->pmsa_gr[7-1], ms->pmsa_nat_bits, &old_sw->r7, &old_unat);
  960. old_sw->b0 = (u64)ia64_leave_kernel;
  961. old_sw->b1 = ms->pmsa_br1;
  962. old_sw->ar_pfs = 0;
  963. old_sw->ar_unat = old_unat;
  964. old_sw->pr = old_regs->pr | (1UL << PRED_NON_SYSCALL);
  965. previous_current->thread.ksp = (u64)p - 16;
  966. /* Finally copy the original stack's registers back to its RBS.
  967. * Registers from ar.bspstore through ar.bsp at the time of the event
  968. * are in the current RBS, copy them back to the original stack. The
  969. * copy must be done register by register because the original bspstore
  970. * and the current one have different alignments, so the saved RNAT
  971. * data occurs at different places.
  972. *
  973. * mca_asm does cover, so the old_bsp already includes all registers at
  974. * the time of MCA/INIT. It also does flushrs, so all registers before
  975. * this function have been written to backing store on the MCA/INIT
  976. * stack.
  977. */
  978. new_rnat = ia64_get_rnat(ia64_rse_rnat_addr(new_bspstore));
  979. old_rnat = regs->ar_rnat;
  980. while (slots--) {
  981. if (ia64_rse_is_rnat_slot(new_bspstore)) {
  982. new_rnat = ia64_get_rnat(new_bspstore++);
  983. }
  984. if (ia64_rse_is_rnat_slot(old_bspstore)) {
  985. *old_bspstore++ = old_rnat;
  986. old_rnat = 0;
  987. }
  988. nat = (new_rnat >> ia64_rse_slot_num(new_bspstore)) & 1UL;
  989. old_rnat &= ~(1UL << ia64_rse_slot_num(old_bspstore));
  990. old_rnat |= (nat << ia64_rse_slot_num(old_bspstore));
  991. *old_bspstore++ = *new_bspstore++;
  992. }
  993. old_sw->ar_bspstore = (unsigned long)old_bspstore;
  994. old_sw->ar_rnat = old_rnat;
  995. sos->prev_task = previous_current;
  996. return previous_current;
  997. no_mod:
  998. printk(KERN_INFO "cpu %d, %s %s, original stack not modified\n",
  999. smp_processor_id(), type, msg);
  1000. return previous_current;
  1001. }
  1002. /* The monarch/slave interaction is based on monarch_cpu and requires that all
  1003. * slaves have entered rendezvous before the monarch leaves. If any cpu has
  1004. * not entered rendezvous yet then wait a bit. The assumption is that any
  1005. * slave that has not rendezvoused after a reasonable time is never going to do
  1006. * so. In this context, slave includes cpus that respond to the MCA rendezvous
  1007. * interrupt, as well as cpus that receive the INIT slave event.
  1008. */
  1009. static void
  1010. ia64_wait_for_slaves(int monarch, const char *type)
  1011. {
  1012. int c, i , wait;
  1013. /*
  1014. * wait 5 seconds total for slaves (arbitrary)
  1015. */
  1016. for (i = 0; i < 5000; i++) {
  1017. wait = 0;
  1018. for_each_online_cpu(c) {
  1019. if (c == monarch)
  1020. continue;
  1021. if (ia64_mc_info.imi_rendez_checkin[c]
  1022. == IA64_MCA_RENDEZ_CHECKIN_NOTDONE) {
  1023. udelay(1000); /* short wait */
  1024. wait = 1;
  1025. break;
  1026. }
  1027. }
  1028. if (!wait)
  1029. goto all_in;
  1030. }
  1031. /*
  1032. * Maybe slave(s) dead. Print buffered messages immediately.
  1033. */
  1034. ia64_mlogbuf_finish(0);
  1035. mprintk(KERN_INFO "OS %s slave did not rendezvous on cpu", type);
  1036. for_each_online_cpu(c) {
  1037. if (c == monarch)
  1038. continue;
  1039. if (ia64_mc_info.imi_rendez_checkin[c] == IA64_MCA_RENDEZ_CHECKIN_NOTDONE)
  1040. mprintk(" %d", c);
  1041. }
  1042. mprintk("\n");
  1043. return;
  1044. all_in:
  1045. mprintk(KERN_INFO "All OS %s slaves have reached rendezvous\n", type);
  1046. return;
  1047. }
  1048. /*
  1049. * ia64_mca_handler
  1050. *
  1051. * This is uncorrectable machine check handler called from OS_MCA
  1052. * dispatch code which is in turn called from SAL_CHECK().
  1053. * This is the place where the core of OS MCA handling is done.
  1054. * Right now the logs are extracted and displayed in a well-defined
  1055. * format. This handler code is supposed to be run only on the
  1056. * monarch processor. Once the monarch is done with MCA handling
  1057. * further MCA logging is enabled by clearing logs.
  1058. * Monarch also has the duty of sending wakeup-IPIs to pull the
  1059. * slave processors out of rendezvous spinloop.
  1060. *
  1061. * If multiple processors call into OS_MCA, the first will become
  1062. * the monarch. Subsequent cpus will be recorded in the mca_cpu
  1063. * bitmask. After the first monarch has processed its MCA, it
  1064. * will wake up the next cpu in the mca_cpu bitmask and then go
  1065. * into the rendezvous loop. When all processors have serviced
  1066. * their MCA, the last monarch frees up the rest of the processors.
  1067. */
  1068. void
  1069. ia64_mca_handler(struct pt_regs *regs, struct switch_stack *sw,
  1070. struct ia64_sal_os_state *sos)
  1071. {
  1072. int recover, cpu = smp_processor_id();
  1073. struct task_struct *previous_current;
  1074. struct ia64_mca_notify_die nd =
  1075. { .sos = sos, .monarch_cpu = &monarch_cpu };
  1076. static atomic_t mca_count;
  1077. static cpumask_t mca_cpu;
  1078. if (atomic_add_return(1, &mca_count) == 1) {
  1079. monarch_cpu = cpu;
  1080. sos->monarch = 1;
  1081. } else {
  1082. cpu_set(cpu, mca_cpu);
  1083. sos->monarch = 0;
  1084. }
  1085. mprintk(KERN_INFO "Entered OS MCA handler. PSP=%lx cpu=%d "
  1086. "monarch=%ld\n", sos->proc_state_param, cpu, sos->monarch);
  1087. previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "MCA");
  1088. if (notify_die(DIE_MCA_MONARCH_ENTER, "MCA", regs, (long)&nd, 0, 0)
  1089. == NOTIFY_STOP)
  1090. ia64_mca_spin(__FUNCTION__);
  1091. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_CONCURRENT_MCA;
  1092. if (sos->monarch) {
  1093. ia64_wait_for_slaves(cpu, "MCA");
  1094. /* Wakeup all the processors which are spinning in the
  1095. * rendezvous loop. They will leave SAL, then spin in the OS
  1096. * with interrupts disabled until this monarch cpu leaves the
  1097. * MCA handler. That gets control back to the OS so we can
  1098. * backtrace the other cpus, backtrace when spinning in SAL
  1099. * does not work.
  1100. */
  1101. ia64_mca_wakeup_all();
  1102. if (notify_die(DIE_MCA_MONARCH_PROCESS, "MCA", regs, (long)&nd, 0, 0)
  1103. == NOTIFY_STOP)
  1104. ia64_mca_spin(__FUNCTION__);
  1105. } else {
  1106. while (cpu_isset(cpu, mca_cpu))
  1107. cpu_relax(); /* spin until monarch wakes us */
  1108. }
  1109. /* Get the MCA error record and log it */
  1110. ia64_mca_log_sal_error_record(SAL_INFO_TYPE_MCA);
  1111. /* MCA error recovery */
  1112. recover = (ia64_mca_ucmc_extension
  1113. && ia64_mca_ucmc_extension(
  1114. IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA),
  1115. sos));
  1116. if (recover) {
  1117. sal_log_record_header_t *rh = IA64_LOG_CURR_BUFFER(SAL_INFO_TYPE_MCA);
  1118. rh->severity = sal_log_severity_corrected;
  1119. ia64_sal_clear_state_info(SAL_INFO_TYPE_MCA);
  1120. sos->os_status = IA64_MCA_CORRECTED;
  1121. } else {
  1122. /* Dump buffered message to console */
  1123. ia64_mlogbuf_finish(1);
  1124. #ifdef CONFIG_KEXEC
  1125. atomic_set(&kdump_in_progress, 1);
  1126. monarch_cpu = -1;
  1127. #endif
  1128. }
  1129. if (notify_die(DIE_MCA_MONARCH_LEAVE, "MCA", regs, (long)&nd, 0, recover)
  1130. == NOTIFY_STOP)
  1131. ia64_mca_spin(__FUNCTION__);
  1132. if (atomic_dec_return(&mca_count) > 0) {
  1133. int i;
  1134. /* wake up the next monarch cpu,
  1135. * and put this cpu in the rendez loop.
  1136. */
  1137. for_each_online_cpu(i) {
  1138. if (cpu_isset(i, mca_cpu)) {
  1139. monarch_cpu = i;
  1140. cpu_clear(i, mca_cpu); /* wake next cpu */
  1141. while (monarch_cpu != -1)
  1142. cpu_relax(); /* spin until last cpu leaves */
  1143. set_curr_task(cpu, previous_current);
  1144. ia64_mc_info.imi_rendez_checkin[cpu]
  1145. = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
  1146. return;
  1147. }
  1148. }
  1149. }
  1150. set_curr_task(cpu, previous_current);
  1151. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
  1152. monarch_cpu = -1; /* This frees the slaves and previous monarchs */
  1153. }
  1154. static DECLARE_WORK(cmc_disable_work, ia64_mca_cmc_vector_disable_keventd);
  1155. static DECLARE_WORK(cmc_enable_work, ia64_mca_cmc_vector_enable_keventd);
  1156. /*
  1157. * ia64_mca_cmc_int_handler
  1158. *
  1159. * This is corrected machine check interrupt handler.
  1160. * Right now the logs are extracted and displayed in a well-defined
  1161. * format.
  1162. *
  1163. * Inputs
  1164. * interrupt number
  1165. * client data arg ptr
  1166. *
  1167. * Outputs
  1168. * None
  1169. */
  1170. static irqreturn_t
  1171. ia64_mca_cmc_int_handler(int cmc_irq, void *arg)
  1172. {
  1173. static unsigned long cmc_history[CMC_HISTORY_LENGTH];
  1174. static int index;
  1175. static DEFINE_SPINLOCK(cmc_history_lock);
  1176. IA64_MCA_DEBUG("%s: received interrupt vector = %#x on CPU %d\n",
  1177. __FUNCTION__, cmc_irq, smp_processor_id());
  1178. /* SAL spec states this should run w/ interrupts enabled */
  1179. local_irq_enable();
  1180. spin_lock(&cmc_history_lock);
  1181. if (!cmc_polling_enabled) {
  1182. int i, count = 1; /* we know 1 happened now */
  1183. unsigned long now = jiffies;
  1184. for (i = 0; i < CMC_HISTORY_LENGTH; i++) {
  1185. if (now - cmc_history[i] <= HZ)
  1186. count++;
  1187. }
  1188. IA64_MCA_DEBUG(KERN_INFO "CMC threshold %d/%d\n", count, CMC_HISTORY_LENGTH);
  1189. if (count >= CMC_HISTORY_LENGTH) {
  1190. cmc_polling_enabled = 1;
  1191. spin_unlock(&cmc_history_lock);
  1192. /* If we're being hit with CMC interrupts, we won't
  1193. * ever execute the schedule_work() below. Need to
  1194. * disable CMC interrupts on this processor now.
  1195. */
  1196. ia64_mca_cmc_vector_disable(NULL);
  1197. schedule_work(&cmc_disable_work);
  1198. /*
  1199. * Corrected errors will still be corrected, but
  1200. * make sure there's a log somewhere that indicates
  1201. * something is generating more than we can handle.
  1202. */
  1203. printk(KERN_WARNING "WARNING: Switching to polling CMC handler; error records may be lost\n");
  1204. mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
  1205. /* lock already released, get out now */
  1206. goto out;
  1207. } else {
  1208. cmc_history[index++] = now;
  1209. if (index == CMC_HISTORY_LENGTH)
  1210. index = 0;
  1211. }
  1212. }
  1213. spin_unlock(&cmc_history_lock);
  1214. out:
  1215. /* Get the CMC error record and log it */
  1216. ia64_mca_log_sal_error_record(SAL_INFO_TYPE_CMC);
  1217. return IRQ_HANDLED;
  1218. }
  1219. /*
  1220. * ia64_mca_cmc_int_caller
  1221. *
  1222. * Triggered by sw interrupt from CMC polling routine. Calls
  1223. * real interrupt handler and either triggers a sw interrupt
  1224. * on the next cpu or does cleanup at the end.
  1225. *
  1226. * Inputs
  1227. * interrupt number
  1228. * client data arg ptr
  1229. * Outputs
  1230. * handled
  1231. */
  1232. static irqreturn_t
  1233. ia64_mca_cmc_int_caller(int cmc_irq, void *arg)
  1234. {
  1235. static int start_count = -1;
  1236. unsigned int cpuid;
  1237. cpuid = smp_processor_id();
  1238. /* If first cpu, update count */
  1239. if (start_count == -1)
  1240. start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CMC);
  1241. ia64_mca_cmc_int_handler(cmc_irq, arg);
  1242. for (++cpuid ; cpuid < NR_CPUS && !cpu_online(cpuid) ; cpuid++);
  1243. if (cpuid < NR_CPUS) {
  1244. platform_send_ipi(cpuid, IA64_CMCP_VECTOR, IA64_IPI_DM_INT, 0);
  1245. } else {
  1246. /* If no log record, switch out of polling mode */
  1247. if (start_count == IA64_LOG_COUNT(SAL_INFO_TYPE_CMC)) {
  1248. printk(KERN_WARNING "Returning to interrupt driven CMC handler\n");
  1249. schedule_work(&cmc_enable_work);
  1250. cmc_polling_enabled = 0;
  1251. } else {
  1252. mod_timer(&cmc_poll_timer, jiffies + CMC_POLL_INTERVAL);
  1253. }
  1254. start_count = -1;
  1255. }
  1256. return IRQ_HANDLED;
  1257. }
  1258. /*
  1259. * ia64_mca_cmc_poll
  1260. *
  1261. * Poll for Corrected Machine Checks (CMCs)
  1262. *
  1263. * Inputs : dummy(unused)
  1264. * Outputs : None
  1265. *
  1266. */
  1267. static void
  1268. ia64_mca_cmc_poll (unsigned long dummy)
  1269. {
  1270. /* Trigger a CMC interrupt cascade */
  1271. platform_send_ipi(first_cpu(cpu_online_map), IA64_CMCP_VECTOR, IA64_IPI_DM_INT, 0);
  1272. }
  1273. /*
  1274. * ia64_mca_cpe_int_caller
  1275. *
  1276. * Triggered by sw interrupt from CPE polling routine. Calls
  1277. * real interrupt handler and either triggers a sw interrupt
  1278. * on the next cpu or does cleanup at the end.
  1279. *
  1280. * Inputs
  1281. * interrupt number
  1282. * client data arg ptr
  1283. * Outputs
  1284. * handled
  1285. */
  1286. #ifdef CONFIG_ACPI
  1287. static irqreturn_t
  1288. ia64_mca_cpe_int_caller(int cpe_irq, void *arg)
  1289. {
  1290. static int start_count = -1;
  1291. static int poll_time = MIN_CPE_POLL_INTERVAL;
  1292. unsigned int cpuid;
  1293. cpuid = smp_processor_id();
  1294. /* If first cpu, update count */
  1295. if (start_count == -1)
  1296. start_count = IA64_LOG_COUNT(SAL_INFO_TYPE_CPE);
  1297. ia64_mca_cpe_int_handler(cpe_irq, arg);
  1298. for (++cpuid ; cpuid < NR_CPUS && !cpu_online(cpuid) ; cpuid++);
  1299. if (cpuid < NR_CPUS) {
  1300. platform_send_ipi(cpuid, IA64_CPEP_VECTOR, IA64_IPI_DM_INT, 0);
  1301. } else {
  1302. /*
  1303. * If a log was recorded, increase our polling frequency,
  1304. * otherwise, backoff or return to interrupt mode.
  1305. */
  1306. if (start_count != IA64_LOG_COUNT(SAL_INFO_TYPE_CPE)) {
  1307. poll_time = max(MIN_CPE_POLL_INTERVAL, poll_time / 2);
  1308. } else if (cpe_vector < 0) {
  1309. poll_time = min(MAX_CPE_POLL_INTERVAL, poll_time * 2);
  1310. } else {
  1311. poll_time = MIN_CPE_POLL_INTERVAL;
  1312. printk(KERN_WARNING "Returning to interrupt driven CPE handler\n");
  1313. enable_irq(local_vector_to_irq(IA64_CPE_VECTOR));
  1314. cpe_poll_enabled = 0;
  1315. }
  1316. if (cpe_poll_enabled)
  1317. mod_timer(&cpe_poll_timer, jiffies + poll_time);
  1318. start_count = -1;
  1319. }
  1320. return IRQ_HANDLED;
  1321. }
  1322. /*
  1323. * ia64_mca_cpe_poll
  1324. *
  1325. * Poll for Corrected Platform Errors (CPEs), trigger interrupt
  1326. * on first cpu, from there it will trickle through all the cpus.
  1327. *
  1328. * Inputs : dummy(unused)
  1329. * Outputs : None
  1330. *
  1331. */
  1332. static void
  1333. ia64_mca_cpe_poll (unsigned long dummy)
  1334. {
  1335. /* Trigger a CPE interrupt cascade */
  1336. platform_send_ipi(first_cpu(cpu_online_map), IA64_CPEP_VECTOR, IA64_IPI_DM_INT, 0);
  1337. }
  1338. #endif /* CONFIG_ACPI */
  1339. static int
  1340. default_monarch_init_process(struct notifier_block *self, unsigned long val, void *data)
  1341. {
  1342. int c;
  1343. struct task_struct *g, *t;
  1344. if (val != DIE_INIT_MONARCH_PROCESS)
  1345. return NOTIFY_DONE;
  1346. #ifdef CONFIG_KEXEC
  1347. if (atomic_read(&kdump_in_progress))
  1348. return NOTIFY_DONE;
  1349. #endif
  1350. /*
  1351. * FIXME: mlogbuf will brim over with INIT stack dumps.
  1352. * To enable show_stack from INIT, we use oops_in_progress which should
  1353. * be used in real oops. This would cause something wrong after INIT.
  1354. */
  1355. BREAK_LOGLEVEL(console_loglevel);
  1356. ia64_mlogbuf_dump_from_init();
  1357. printk(KERN_ERR "Processes interrupted by INIT -");
  1358. for_each_online_cpu(c) {
  1359. struct ia64_sal_os_state *s;
  1360. t = __va(__per_cpu_mca[c] + IA64_MCA_CPU_INIT_STACK_OFFSET);
  1361. s = (struct ia64_sal_os_state *)((char *)t + MCA_SOS_OFFSET);
  1362. g = s->prev_task;
  1363. if (g) {
  1364. if (g->pid)
  1365. printk(" %d", g->pid);
  1366. else
  1367. printk(" %d (cpu %d task 0x%p)", g->pid, task_cpu(g), g);
  1368. }
  1369. }
  1370. printk("\n\n");
  1371. if (read_trylock(&tasklist_lock)) {
  1372. do_each_thread (g, t) {
  1373. printk("\nBacktrace of pid %d (%s)\n", t->pid, t->comm);
  1374. show_stack(t, NULL);
  1375. } while_each_thread (g, t);
  1376. read_unlock(&tasklist_lock);
  1377. }
  1378. /* FIXME: This will not restore zapped printk locks. */
  1379. RESTORE_LOGLEVEL(console_loglevel);
  1380. return NOTIFY_DONE;
  1381. }
  1382. /*
  1383. * C portion of the OS INIT handler
  1384. *
  1385. * Called from ia64_os_init_dispatch
  1386. *
  1387. * Inputs: pointer to pt_regs where processor info was saved. SAL/OS state for
  1388. * this event. This code is used for both monarch and slave INIT events, see
  1389. * sos->monarch.
  1390. *
  1391. * All INIT events switch to the INIT stack and change the previous process to
  1392. * blocked status. If one of the INIT events is the monarch then we are
  1393. * probably processing the nmi button/command. Use the monarch cpu to dump all
  1394. * the processes. The slave INIT events all spin until the monarch cpu
  1395. * returns. We can also get INIT slave events for MCA, in which case the MCA
  1396. * process is the monarch.
  1397. */
  1398. void
  1399. ia64_init_handler(struct pt_regs *regs, struct switch_stack *sw,
  1400. struct ia64_sal_os_state *sos)
  1401. {
  1402. static atomic_t slaves;
  1403. static atomic_t monarchs;
  1404. struct task_struct *previous_current;
  1405. int cpu = smp_processor_id();
  1406. struct ia64_mca_notify_die nd =
  1407. { .sos = sos, .monarch_cpu = &monarch_cpu };
  1408. (void) notify_die(DIE_INIT_ENTER, "INIT", regs, (long)&nd, 0, 0);
  1409. mprintk(KERN_INFO "Entered OS INIT handler. PSP=%lx cpu=%d monarch=%ld\n",
  1410. sos->proc_state_param, cpu, sos->monarch);
  1411. salinfo_log_wakeup(SAL_INFO_TYPE_INIT, NULL, 0, 0);
  1412. previous_current = ia64_mca_modify_original_stack(regs, sw, sos, "INIT");
  1413. sos->os_status = IA64_INIT_RESUME;
  1414. /* FIXME: Workaround for broken proms that drive all INIT events as
  1415. * slaves. The last slave that enters is promoted to be a monarch.
  1416. * Remove this code in September 2006, that gives platforms a year to
  1417. * fix their proms and get their customers updated.
  1418. */
  1419. if (!sos->monarch && atomic_add_return(1, &slaves) == num_online_cpus()) {
  1420. mprintk(KERN_WARNING "%s: Promoting cpu %d to monarch.\n",
  1421. __FUNCTION__, cpu);
  1422. atomic_dec(&slaves);
  1423. sos->monarch = 1;
  1424. }
  1425. /* FIXME: Workaround for broken proms that drive all INIT events as
  1426. * monarchs. Second and subsequent monarchs are demoted to slaves.
  1427. * Remove this code in September 2006, that gives platforms a year to
  1428. * fix their proms and get their customers updated.
  1429. */
  1430. if (sos->monarch && atomic_add_return(1, &monarchs) > 1) {
  1431. mprintk(KERN_WARNING "%s: Demoting cpu %d to slave.\n",
  1432. __FUNCTION__, cpu);
  1433. atomic_dec(&monarchs);
  1434. sos->monarch = 0;
  1435. }
  1436. if (!sos->monarch) {
  1437. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_INIT;
  1438. while (monarch_cpu == -1)
  1439. cpu_relax(); /* spin until monarch enters */
  1440. if (notify_die(DIE_INIT_SLAVE_ENTER, "INIT", regs, (long)&nd, 0, 0)
  1441. == NOTIFY_STOP)
  1442. ia64_mca_spin(__FUNCTION__);
  1443. if (notify_die(DIE_INIT_SLAVE_PROCESS, "INIT", regs, (long)&nd, 0, 0)
  1444. == NOTIFY_STOP)
  1445. ia64_mca_spin(__FUNCTION__);
  1446. while (monarch_cpu != -1)
  1447. cpu_relax(); /* spin until monarch leaves */
  1448. if (notify_die(DIE_INIT_SLAVE_LEAVE, "INIT", regs, (long)&nd, 0, 0)
  1449. == NOTIFY_STOP)
  1450. ia64_mca_spin(__FUNCTION__);
  1451. mprintk("Slave on cpu %d returning to normal service.\n", cpu);
  1452. set_curr_task(cpu, previous_current);
  1453. ia64_mc_info.imi_rendez_checkin[cpu] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
  1454. atomic_dec(&slaves);
  1455. return;
  1456. }
  1457. monarch_cpu = cpu;
  1458. if (notify_die(DIE_INIT_MONARCH_ENTER, "INIT", regs, (long)&nd, 0, 0)
  1459. == NOTIFY_STOP)
  1460. ia64_mca_spin(__FUNCTION__);
  1461. /*
  1462. * Wait for a bit. On some machines (e.g., HP's zx2000 and zx6000, INIT can be
  1463. * generated via the BMC's command-line interface, but since the console is on the
  1464. * same serial line, the user will need some time to switch out of the BMC before
  1465. * the dump begins.
  1466. */
  1467. mprintk("Delaying for 5 seconds...\n");
  1468. udelay(5*1000000);
  1469. ia64_wait_for_slaves(cpu, "INIT");
  1470. /* If nobody intercepts DIE_INIT_MONARCH_PROCESS then we drop through
  1471. * to default_monarch_init_process() above and just print all the
  1472. * tasks.
  1473. */
  1474. if (notify_die(DIE_INIT_MONARCH_PROCESS, "INIT", regs, (long)&nd, 0, 0)
  1475. == NOTIFY_STOP)
  1476. ia64_mca_spin(__FUNCTION__);
  1477. if (notify_die(DIE_INIT_MONARCH_LEAVE, "INIT", regs, (long)&nd, 0, 0)
  1478. == NOTIFY_STOP)
  1479. ia64_mca_spin(__FUNCTION__);
  1480. mprintk("\nINIT dump complete. Monarch on cpu %d returning to normal service.\n", cpu);
  1481. atomic_dec(&monarchs);
  1482. set_curr_task(cpu, previous_current);
  1483. monarch_cpu = -1;
  1484. return;
  1485. }
  1486. static int __init
  1487. ia64_mca_disable_cpe_polling(char *str)
  1488. {
  1489. cpe_poll_enabled = 0;
  1490. return 1;
  1491. }
  1492. __setup("disable_cpe_poll", ia64_mca_disable_cpe_polling);
  1493. static struct irqaction cmci_irqaction = {
  1494. .handler = ia64_mca_cmc_int_handler,
  1495. .flags = IRQF_DISABLED,
  1496. .name = "cmc_hndlr"
  1497. };
  1498. static struct irqaction cmcp_irqaction = {
  1499. .handler = ia64_mca_cmc_int_caller,
  1500. .flags = IRQF_DISABLED,
  1501. .name = "cmc_poll"
  1502. };
  1503. static struct irqaction mca_rdzv_irqaction = {
  1504. .handler = ia64_mca_rendez_int_handler,
  1505. .flags = IRQF_DISABLED,
  1506. .name = "mca_rdzv"
  1507. };
  1508. static struct irqaction mca_wkup_irqaction = {
  1509. .handler = ia64_mca_wakeup_int_handler,
  1510. .flags = IRQF_DISABLED,
  1511. .name = "mca_wkup"
  1512. };
  1513. #ifdef CONFIG_ACPI
  1514. static struct irqaction mca_cpe_irqaction = {
  1515. .handler = ia64_mca_cpe_int_handler,
  1516. .flags = IRQF_DISABLED,
  1517. .name = "cpe_hndlr"
  1518. };
  1519. static struct irqaction mca_cpep_irqaction = {
  1520. .handler = ia64_mca_cpe_int_caller,
  1521. .flags = IRQF_DISABLED,
  1522. .name = "cpe_poll"
  1523. };
  1524. #endif /* CONFIG_ACPI */
  1525. /* Minimal format of the MCA/INIT stacks. The pseudo processes that run on
  1526. * these stacks can never sleep, they cannot return from the kernel to user
  1527. * space, they do not appear in a normal ps listing. So there is no need to
  1528. * format most of the fields.
  1529. */
  1530. static void __cpuinit
  1531. format_mca_init_stack(void *mca_data, unsigned long offset,
  1532. const char *type, int cpu)
  1533. {
  1534. struct task_struct *p = (struct task_struct *)((char *)mca_data + offset);
  1535. struct thread_info *ti;
  1536. memset(p, 0, KERNEL_STACK_SIZE);
  1537. ti = task_thread_info(p);
  1538. ti->flags = _TIF_MCA_INIT;
  1539. ti->preempt_count = 1;
  1540. ti->task = p;
  1541. ti->cpu = cpu;
  1542. p->stack = ti;
  1543. p->state = TASK_UNINTERRUPTIBLE;
  1544. cpu_set(cpu, p->cpus_allowed);
  1545. INIT_LIST_HEAD(&p->tasks);
  1546. p->parent = p->real_parent = p->group_leader = p;
  1547. INIT_LIST_HEAD(&p->children);
  1548. INIT_LIST_HEAD(&p->sibling);
  1549. strncpy(p->comm, type, sizeof(p->comm)-1);
  1550. }
  1551. /* Caller prevents this from being called after init */
  1552. static void * __init_refok mca_bootmem(void)
  1553. {
  1554. void *p;
  1555. p = alloc_bootmem(sizeof(struct ia64_mca_cpu) * NR_CPUS +
  1556. KERNEL_STACK_SIZE);
  1557. return (void *)ALIGN((unsigned long)p, KERNEL_STACK_SIZE);
  1558. }
  1559. /* Do per-CPU MCA-related initialization. */
  1560. void __cpuinit
  1561. ia64_mca_cpu_init(void *cpu_data)
  1562. {
  1563. void *pal_vaddr;
  1564. static int first_time = 1;
  1565. if (first_time) {
  1566. void *mca_data;
  1567. int cpu;
  1568. first_time = 0;
  1569. mca_data = mca_bootmem();
  1570. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1571. format_mca_init_stack(mca_data,
  1572. offsetof(struct ia64_mca_cpu, mca_stack),
  1573. "MCA", cpu);
  1574. format_mca_init_stack(mca_data,
  1575. offsetof(struct ia64_mca_cpu, init_stack),
  1576. "INIT", cpu);
  1577. __per_cpu_mca[cpu] = __pa(mca_data);
  1578. mca_data += sizeof(struct ia64_mca_cpu);
  1579. }
  1580. }
  1581. /*
  1582. * The MCA info structure was allocated earlier and its
  1583. * physical address saved in __per_cpu_mca[cpu]. Copy that
  1584. * address * to ia64_mca_data so we can access it as a per-CPU
  1585. * variable.
  1586. */
  1587. __get_cpu_var(ia64_mca_data) = __per_cpu_mca[smp_processor_id()];
  1588. /*
  1589. * Stash away a copy of the PTE needed to map the per-CPU page.
  1590. * We may need it during MCA recovery.
  1591. */
  1592. __get_cpu_var(ia64_mca_per_cpu_pte) =
  1593. pte_val(mk_pte_phys(__pa(cpu_data), PAGE_KERNEL));
  1594. /*
  1595. * Also, stash away a copy of the PAL address and the PTE
  1596. * needed to map it.
  1597. */
  1598. pal_vaddr = efi_get_pal_addr();
  1599. if (!pal_vaddr)
  1600. return;
  1601. __get_cpu_var(ia64_mca_pal_base) =
  1602. GRANULEROUNDDOWN((unsigned long) pal_vaddr);
  1603. __get_cpu_var(ia64_mca_pal_pte) = pte_val(mk_pte_phys(__pa(pal_vaddr),
  1604. PAGE_KERNEL));
  1605. }
  1606. /*
  1607. * ia64_mca_init
  1608. *
  1609. * Do all the system level mca specific initialization.
  1610. *
  1611. * 1. Register spinloop and wakeup request interrupt vectors
  1612. *
  1613. * 2. Register OS_MCA handler entry point
  1614. *
  1615. * 3. Register OS_INIT handler entry point
  1616. *
  1617. * 4. Initialize MCA/CMC/INIT related log buffers maintained by the OS.
  1618. *
  1619. * Note that this initialization is done very early before some kernel
  1620. * services are available.
  1621. *
  1622. * Inputs : None
  1623. *
  1624. * Outputs : None
  1625. */
  1626. void __init
  1627. ia64_mca_init(void)
  1628. {
  1629. ia64_fptr_t *init_hldlr_ptr_monarch = (ia64_fptr_t *)ia64_os_init_dispatch_monarch;
  1630. ia64_fptr_t *init_hldlr_ptr_slave = (ia64_fptr_t *)ia64_os_init_dispatch_slave;
  1631. ia64_fptr_t *mca_hldlr_ptr = (ia64_fptr_t *)ia64_os_mca_dispatch;
  1632. int i;
  1633. s64 rc;
  1634. struct ia64_sal_retval isrv;
  1635. u64 timeout = IA64_MCA_RENDEZ_TIMEOUT; /* platform specific */
  1636. static struct notifier_block default_init_monarch_nb = {
  1637. .notifier_call = default_monarch_init_process,
  1638. .priority = 0/* we need to notified last */
  1639. };
  1640. IA64_MCA_DEBUG("%s: begin\n", __FUNCTION__);
  1641. /* Clear the Rendez checkin flag for all cpus */
  1642. for(i = 0 ; i < NR_CPUS; i++)
  1643. ia64_mc_info.imi_rendez_checkin[i] = IA64_MCA_RENDEZ_CHECKIN_NOTDONE;
  1644. /*
  1645. * Register the rendezvous spinloop and wakeup mechanism with SAL
  1646. */
  1647. /* Register the rendezvous interrupt vector with SAL */
  1648. while (1) {
  1649. isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_INT,
  1650. SAL_MC_PARAM_MECHANISM_INT,
  1651. IA64_MCA_RENDEZ_VECTOR,
  1652. timeout,
  1653. SAL_MC_PARAM_RZ_ALWAYS);
  1654. rc = isrv.status;
  1655. if (rc == 0)
  1656. break;
  1657. if (rc == -2) {
  1658. printk(KERN_INFO "Increasing MCA rendezvous timeout from "
  1659. "%ld to %ld milliseconds\n", timeout, isrv.v0);
  1660. timeout = isrv.v0;
  1661. (void) notify_die(DIE_MCA_NEW_TIMEOUT, "MCA", NULL, timeout, 0, 0);
  1662. continue;
  1663. }
  1664. printk(KERN_ERR "Failed to register rendezvous interrupt "
  1665. "with SAL (status %ld)\n", rc);
  1666. return;
  1667. }
  1668. /* Register the wakeup interrupt vector with SAL */
  1669. isrv = ia64_sal_mc_set_params(SAL_MC_PARAM_RENDEZ_WAKEUP,
  1670. SAL_MC_PARAM_MECHANISM_INT,
  1671. IA64_MCA_WAKEUP_VECTOR,
  1672. 0, 0);
  1673. rc = isrv.status;
  1674. if (rc) {
  1675. printk(KERN_ERR "Failed to register wakeup interrupt with SAL "
  1676. "(status %ld)\n", rc);
  1677. return;
  1678. }
  1679. IA64_MCA_DEBUG("%s: registered MCA rendezvous spinloop and wakeup mech.\n", __FUNCTION__);
  1680. ia64_mc_info.imi_mca_handler = ia64_tpa(mca_hldlr_ptr->fp);
  1681. /*
  1682. * XXX - disable SAL checksum by setting size to 0; should be
  1683. * ia64_tpa(ia64_os_mca_dispatch_end) - ia64_tpa(ia64_os_mca_dispatch);
  1684. */
  1685. ia64_mc_info.imi_mca_handler_size = 0;
  1686. /* Register the os mca handler with SAL */
  1687. if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_MCA,
  1688. ia64_mc_info.imi_mca_handler,
  1689. ia64_tpa(mca_hldlr_ptr->gp),
  1690. ia64_mc_info.imi_mca_handler_size,
  1691. 0, 0, 0)))
  1692. {
  1693. printk(KERN_ERR "Failed to register OS MCA handler with SAL "
  1694. "(status %ld)\n", rc);
  1695. return;
  1696. }
  1697. IA64_MCA_DEBUG("%s: registered OS MCA handler with SAL at 0x%lx, gp = 0x%lx\n", __FUNCTION__,
  1698. ia64_mc_info.imi_mca_handler, ia64_tpa(mca_hldlr_ptr->gp));
  1699. /*
  1700. * XXX - disable SAL checksum by setting size to 0, should be
  1701. * size of the actual init handler in mca_asm.S.
  1702. */
  1703. ia64_mc_info.imi_monarch_init_handler = ia64_tpa(init_hldlr_ptr_monarch->fp);
  1704. ia64_mc_info.imi_monarch_init_handler_size = 0;
  1705. ia64_mc_info.imi_slave_init_handler = ia64_tpa(init_hldlr_ptr_slave->fp);
  1706. ia64_mc_info.imi_slave_init_handler_size = 0;
  1707. IA64_MCA_DEBUG("%s: OS INIT handler at %lx\n", __FUNCTION__,
  1708. ia64_mc_info.imi_monarch_init_handler);
  1709. /* Register the os init handler with SAL */
  1710. if ((rc = ia64_sal_set_vectors(SAL_VECTOR_OS_INIT,
  1711. ia64_mc_info.imi_monarch_init_handler,
  1712. ia64_tpa(ia64_getreg(_IA64_REG_GP)),
  1713. ia64_mc_info.imi_monarch_init_handler_size,
  1714. ia64_mc_info.imi_slave_init_handler,
  1715. ia64_tpa(ia64_getreg(_IA64_REG_GP)),
  1716. ia64_mc_info.imi_slave_init_handler_size)))
  1717. {
  1718. printk(KERN_ERR "Failed to register m/s INIT handlers with SAL "
  1719. "(status %ld)\n", rc);
  1720. return;
  1721. }
  1722. if (register_die_notifier(&default_init_monarch_nb)) {
  1723. printk(KERN_ERR "Failed to register default monarch INIT process\n");
  1724. return;
  1725. }
  1726. IA64_MCA_DEBUG("%s: registered OS INIT handler with SAL\n", __FUNCTION__);
  1727. /*
  1728. * Configure the CMCI/P vector and handler. Interrupts for CMC are
  1729. * per-processor, so AP CMC interrupts are setup in smp_callin() (smpboot.c).
  1730. */
  1731. register_percpu_irq(IA64_CMC_VECTOR, &cmci_irqaction);
  1732. register_percpu_irq(IA64_CMCP_VECTOR, &cmcp_irqaction);
  1733. ia64_mca_cmc_vector_setup(); /* Setup vector on BSP */
  1734. /* Setup the MCA rendezvous interrupt vector */
  1735. register_percpu_irq(IA64_MCA_RENDEZ_VECTOR, &mca_rdzv_irqaction);
  1736. /* Setup the MCA wakeup interrupt vector */
  1737. register_percpu_irq(IA64_MCA_WAKEUP_VECTOR, &mca_wkup_irqaction);
  1738. #ifdef CONFIG_ACPI
  1739. /* Setup the CPEI/P handler */
  1740. register_percpu_irq(IA64_CPEP_VECTOR, &mca_cpep_irqaction);
  1741. #endif
  1742. /* Initialize the areas set aside by the OS to buffer the
  1743. * platform/processor error states for MCA/INIT/CMC
  1744. * handling.
  1745. */
  1746. ia64_log_init(SAL_INFO_TYPE_MCA);
  1747. ia64_log_init(SAL_INFO_TYPE_INIT);
  1748. ia64_log_init(SAL_INFO_TYPE_CMC);
  1749. ia64_log_init(SAL_INFO_TYPE_CPE);
  1750. mca_init = 1;
  1751. printk(KERN_INFO "MCA related initialization done\n");
  1752. }
  1753. /*
  1754. * ia64_mca_late_init
  1755. *
  1756. * Opportunity to setup things that require initialization later
  1757. * than ia64_mca_init. Setup a timer to poll for CPEs if the
  1758. * platform doesn't support an interrupt driven mechanism.
  1759. *
  1760. * Inputs : None
  1761. * Outputs : Status
  1762. */
  1763. static int __init
  1764. ia64_mca_late_init(void)
  1765. {
  1766. if (!mca_init)
  1767. return 0;
  1768. /* Setup the CMCI/P vector and handler */
  1769. init_timer(&cmc_poll_timer);
  1770. cmc_poll_timer.function = ia64_mca_cmc_poll;
  1771. /* Unmask/enable the vector */
  1772. cmc_polling_enabled = 0;
  1773. schedule_work(&cmc_enable_work);
  1774. IA64_MCA_DEBUG("%s: CMCI/P setup and enabled.\n", __FUNCTION__);
  1775. #ifdef CONFIG_ACPI
  1776. /* Setup the CPEI/P vector and handler */
  1777. cpe_vector = acpi_request_vector(ACPI_INTERRUPT_CPEI);
  1778. init_timer(&cpe_poll_timer);
  1779. cpe_poll_timer.function = ia64_mca_cpe_poll;
  1780. {
  1781. irq_desc_t *desc;
  1782. unsigned int irq;
  1783. if (cpe_vector >= 0) {
  1784. /* If platform supports CPEI, enable the irq. */
  1785. irq = local_vector_to_irq(cpe_vector);
  1786. if (irq > 0) {
  1787. cpe_poll_enabled = 0;
  1788. desc = irq_desc + irq;
  1789. desc->status |= IRQ_PER_CPU;
  1790. setup_irq(irq, &mca_cpe_irqaction);
  1791. ia64_cpe_irq = irq;
  1792. ia64_mca_register_cpev(cpe_vector);
  1793. IA64_MCA_DEBUG("%s: CPEI/P setup and enabled.\n",
  1794. __FUNCTION__);
  1795. return 0;
  1796. }
  1797. printk(KERN_ERR "%s: Failed to find irq for CPE "
  1798. "interrupt handler, vector %d\n",
  1799. __FUNCTION__, cpe_vector);
  1800. }
  1801. /* If platform doesn't support CPEI, get the timer going. */
  1802. if (cpe_poll_enabled) {
  1803. ia64_mca_cpe_poll(0UL);
  1804. IA64_MCA_DEBUG("%s: CPEP setup and enabled.\n", __FUNCTION__);
  1805. }
  1806. }
  1807. #endif
  1808. return 0;
  1809. }
  1810. device_initcall(ia64_mca_late_init);