mca.c 60 KB

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