apic.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Fixes
  7. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  8. * thanks to Eric Gilmore
  9. * and Rolf G. Tews
  10. * for testing these extensively.
  11. * Maciej W. Rozycki : Various updates and fixes.
  12. * Mikael Pettersson : Power Management for UP-APIC.
  13. * Pavel Machek and
  14. * Mikael Pettersson : PM converted to driver model.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/delay.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mc146818rtc.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/cpu.h>
  26. #include <linux/clockchips.h>
  27. #include <linux/acpi_pmtmr.h>
  28. #include <linux/module.h>
  29. #include <linux/dmi.h>
  30. #include <asm/atomic.h>
  31. #include <asm/smp.h>
  32. #include <asm/mtrr.h>
  33. #include <asm/mpspec.h>
  34. #include <asm/desc.h>
  35. #include <asm/arch_hooks.h>
  36. #include <asm/hpet.h>
  37. #include <asm/i8253.h>
  38. #include <asm/nmi.h>
  39. #include <mach_apic.h>
  40. #include <mach_apicdef.h>
  41. #include <mach_ipi.h>
  42. #include "io_ports.h"
  43. /*
  44. * Sanity check
  45. */
  46. #if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
  47. # error SPURIOUS_APIC_VECTOR definition error
  48. #endif
  49. /*
  50. * Knob to control our willingness to enable the local APIC.
  51. *
  52. * -1=force-disable, +1=force-enable
  53. */
  54. static int enable_local_apic __initdata = 0;
  55. /* Local APIC timer verification ok */
  56. static int local_apic_timer_verify_ok;
  57. /* Disable local APIC timer from the kernel commandline or via dmi quirk */
  58. static int local_apic_timer_disabled;
  59. /* Local APIC timer works in C2 */
  60. int local_apic_timer_c2_ok;
  61. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  62. /*
  63. * Debug level, exported for io_apic.c
  64. */
  65. int apic_verbosity;
  66. static unsigned int calibration_result;
  67. static int lapic_next_event(unsigned long delta,
  68. struct clock_event_device *evt);
  69. static void lapic_timer_setup(enum clock_event_mode mode,
  70. struct clock_event_device *evt);
  71. static void lapic_timer_broadcast(cpumask_t mask);
  72. static void apic_pm_activate(void);
  73. /*
  74. * The local apic timer can be used for any function which is CPU local.
  75. */
  76. static struct clock_event_device lapic_clockevent = {
  77. .name = "lapic",
  78. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  79. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  80. .shift = 32,
  81. .set_mode = lapic_timer_setup,
  82. .set_next_event = lapic_next_event,
  83. .broadcast = lapic_timer_broadcast,
  84. .rating = 100,
  85. .irq = -1,
  86. };
  87. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  88. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  89. static int enabled_via_apicbase;
  90. /*
  91. * Get the LAPIC version
  92. */
  93. static inline int lapic_get_version(void)
  94. {
  95. return GET_APIC_VERSION(apic_read(APIC_LVR));
  96. }
  97. /*
  98. * Check, if the APIC is integrated or a seperate chip
  99. */
  100. static inline int lapic_is_integrated(void)
  101. {
  102. return APIC_INTEGRATED(lapic_get_version());
  103. }
  104. /*
  105. * Check, whether this is a modern or a first generation APIC
  106. */
  107. static int modern_apic(void)
  108. {
  109. /* AMD systems use old APIC versions, so check the CPU */
  110. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  111. boot_cpu_data.x86 >= 0xf)
  112. return 1;
  113. return lapic_get_version() >= 0x14;
  114. }
  115. /**
  116. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  117. */
  118. void enable_NMI_through_LVT0 (void * dummy)
  119. {
  120. unsigned int v = APIC_DM_NMI;
  121. /* Level triggered for 82489DX */
  122. if (!lapic_is_integrated())
  123. v |= APIC_LVT_LEVEL_TRIGGER;
  124. apic_write_around(APIC_LVT0, v);
  125. }
  126. /**
  127. * get_physical_broadcast - Get number of physical broadcast IDs
  128. */
  129. int get_physical_broadcast(void)
  130. {
  131. return modern_apic() ? 0xff : 0xf;
  132. }
  133. /**
  134. * lapic_get_maxlvt - get the maximum number of local vector table entries
  135. */
  136. int lapic_get_maxlvt(void)
  137. {
  138. unsigned int v = apic_read(APIC_LVR);
  139. /* 82489DXs do not report # of LVT entries. */
  140. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  141. }
  142. /*
  143. * Local APIC timer
  144. */
  145. /* Clock divisor is set to 16 */
  146. #define APIC_DIVISOR 16
  147. /*
  148. * This function sets up the local APIC timer, with a timeout of
  149. * 'clocks' APIC bus clock. During calibration we actually call
  150. * this function twice on the boot CPU, once with a bogus timeout
  151. * value, second time for real. The other (noncalibrating) CPUs
  152. * call this function only once, with the real, calibrated value.
  153. *
  154. * We do reads before writes even if unnecessary, to get around the
  155. * P5 APIC double write bug.
  156. */
  157. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  158. {
  159. unsigned int lvtt_value, tmp_value;
  160. lvtt_value = LOCAL_TIMER_VECTOR;
  161. if (!oneshot)
  162. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  163. if (!lapic_is_integrated())
  164. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  165. if (!irqen)
  166. lvtt_value |= APIC_LVT_MASKED;
  167. apic_write_around(APIC_LVTT, lvtt_value);
  168. /*
  169. * Divide PICLK by 16
  170. */
  171. tmp_value = apic_read(APIC_TDCR);
  172. apic_write_around(APIC_TDCR, (tmp_value
  173. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  174. | APIC_TDR_DIV_16);
  175. if (!oneshot)
  176. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  177. }
  178. /*
  179. * Program the next event, relative to now
  180. */
  181. static int lapic_next_event(unsigned long delta,
  182. struct clock_event_device *evt)
  183. {
  184. apic_write_around(APIC_TMICT, delta);
  185. return 0;
  186. }
  187. /*
  188. * Setup the lapic timer in periodic or oneshot mode
  189. */
  190. static void lapic_timer_setup(enum clock_event_mode mode,
  191. struct clock_event_device *evt)
  192. {
  193. unsigned long flags;
  194. unsigned int v;
  195. /* Lapic used for broadcast ? */
  196. if (!local_apic_timer_verify_ok)
  197. return;
  198. local_irq_save(flags);
  199. switch (mode) {
  200. case CLOCK_EVT_MODE_PERIODIC:
  201. case CLOCK_EVT_MODE_ONESHOT:
  202. __setup_APIC_LVTT(calibration_result,
  203. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  204. break;
  205. case CLOCK_EVT_MODE_UNUSED:
  206. case CLOCK_EVT_MODE_SHUTDOWN:
  207. v = apic_read(APIC_LVTT);
  208. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  209. apic_write_around(APIC_LVTT, v);
  210. break;
  211. }
  212. local_irq_restore(flags);
  213. }
  214. /*
  215. * Local APIC timer broadcast function
  216. */
  217. static void lapic_timer_broadcast(cpumask_t mask)
  218. {
  219. #ifdef CONFIG_SMP
  220. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  221. #endif
  222. }
  223. /*
  224. * Setup the local APIC timer for this CPU. Copy the initilized values
  225. * of the boot CPU and register the clock event in the framework.
  226. */
  227. static void __devinit setup_APIC_timer(void)
  228. {
  229. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  230. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  231. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  232. clockevents_register_device(levt);
  233. }
  234. /*
  235. * In this functions we calibrate APIC bus clocks to the external timer.
  236. *
  237. * We want to do the calibration only once since we want to have local timer
  238. * irqs syncron. CPUs connected by the same APIC bus have the very same bus
  239. * frequency.
  240. *
  241. * This was previously done by reading the PIT/HPET and waiting for a wrap
  242. * around to find out, that a tick has elapsed. I have a box, where the PIT
  243. * readout is broken, so it never gets out of the wait loop again. This was
  244. * also reported by others.
  245. *
  246. * Monitoring the jiffies value is inaccurate and the clockevents
  247. * infrastructure allows us to do a simple substitution of the interrupt
  248. * handler.
  249. *
  250. * The calibration routine also uses the pm_timer when possible, as the PIT
  251. * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
  252. * back to normal later in the boot process).
  253. */
  254. #define LAPIC_CAL_LOOPS (HZ/10)
  255. static __initdata volatile int lapic_cal_loops = -1;
  256. static __initdata long lapic_cal_t1, lapic_cal_t2;
  257. static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
  258. static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
  259. static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
  260. /*
  261. * Temporary interrupt handler.
  262. */
  263. static void __init lapic_cal_handler(struct clock_event_device *dev)
  264. {
  265. unsigned long long tsc = 0;
  266. long tapic = apic_read(APIC_TMCCT);
  267. unsigned long pm = acpi_pm_read_early();
  268. if (cpu_has_tsc)
  269. rdtscll(tsc);
  270. switch (lapic_cal_loops++) {
  271. case 0:
  272. lapic_cal_t1 = tapic;
  273. lapic_cal_tsc1 = tsc;
  274. lapic_cal_pm1 = pm;
  275. lapic_cal_j1 = jiffies;
  276. break;
  277. case LAPIC_CAL_LOOPS:
  278. lapic_cal_t2 = tapic;
  279. lapic_cal_tsc2 = tsc;
  280. if (pm < lapic_cal_pm1)
  281. pm += ACPI_PM_OVRRUN;
  282. lapic_cal_pm2 = pm;
  283. lapic_cal_j2 = jiffies;
  284. break;
  285. }
  286. }
  287. /*
  288. * Setup the boot APIC
  289. *
  290. * Calibrate and verify the result.
  291. */
  292. void __init setup_boot_APIC_clock(void)
  293. {
  294. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  295. const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
  296. const long pm_thresh = pm_100ms/100;
  297. void (*real_handler)(struct clock_event_device *dev);
  298. unsigned long deltaj;
  299. long delta, deltapm;
  300. int pm_referenced = 0;
  301. if (boot_cpu_has(X86_FEATURE_LAPIC_TIMER_BROKEN))
  302. local_apic_timer_disabled = 1;
  303. /*
  304. * The local apic timer can be disabled via the kernel
  305. * commandline or from the test above. Register the lapic
  306. * timer as a dummy clock event source on SMP systems, so the
  307. * broadcast mechanism is used. On UP systems simply ignore it.
  308. */
  309. if (local_apic_timer_disabled) {
  310. /* No broadcast on UP ! */
  311. if (num_possible_cpus() > 1)
  312. setup_APIC_timer();
  313. return;
  314. }
  315. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  316. "calibrating APIC timer ...\n");
  317. local_irq_disable();
  318. /* Replace the global interrupt handler */
  319. real_handler = global_clock_event->event_handler;
  320. global_clock_event->event_handler = lapic_cal_handler;
  321. /*
  322. * Setup the APIC counter to 1e9. There is no way the lapic
  323. * can underflow in the 100ms detection time frame
  324. */
  325. __setup_APIC_LVTT(1000000000, 0, 0);
  326. /* Let the interrupts run */
  327. local_irq_enable();
  328. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  329. cpu_relax();
  330. local_irq_disable();
  331. /* Restore the real event handler */
  332. global_clock_event->event_handler = real_handler;
  333. /* Build delta t1-t2 as apic timer counts down */
  334. delta = lapic_cal_t1 - lapic_cal_t2;
  335. apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
  336. /* Check, if the PM timer is available */
  337. deltapm = lapic_cal_pm2 - lapic_cal_pm1;
  338. apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
  339. if (deltapm) {
  340. unsigned long mult;
  341. u64 res;
  342. mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
  343. if (deltapm > (pm_100ms - pm_thresh) &&
  344. deltapm < (pm_100ms + pm_thresh)) {
  345. apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
  346. } else {
  347. res = (((u64) deltapm) * mult) >> 22;
  348. do_div(res, 1000000);
  349. printk(KERN_WARNING "APIC calibration not consistent "
  350. "with PM Timer: %ldms instead of 100ms\n",
  351. (long)res);
  352. /* Correct the lapic counter value */
  353. res = (((u64) delta ) * pm_100ms);
  354. do_div(res, deltapm);
  355. printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
  356. "%lu (%ld)\n", (unsigned long) res, delta);
  357. delta = (long) res;
  358. }
  359. pm_referenced = 1;
  360. }
  361. /* Calculate the scaled math multiplication factor */
  362. lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
  363. lapic_clockevent.max_delta_ns =
  364. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  365. lapic_clockevent.min_delta_ns =
  366. clockevent_delta2ns(0xF, &lapic_clockevent);
  367. calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
  368. apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
  369. apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
  370. apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
  371. calibration_result);
  372. if (cpu_has_tsc) {
  373. delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
  374. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  375. "%ld.%04ld MHz.\n",
  376. (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
  377. (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
  378. }
  379. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  380. "%u.%04u MHz.\n",
  381. calibration_result / (1000000 / HZ),
  382. calibration_result % (1000000 / HZ));
  383. local_apic_timer_verify_ok = 1;
  384. /* We trust the pm timer based calibration */
  385. if (!pm_referenced) {
  386. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  387. /*
  388. * Setup the apic timer manually
  389. */
  390. levt->event_handler = lapic_cal_handler;
  391. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  392. lapic_cal_loops = -1;
  393. /* Let the interrupts run */
  394. local_irq_enable();
  395. while(lapic_cal_loops <= LAPIC_CAL_LOOPS)
  396. cpu_relax();
  397. local_irq_disable();
  398. /* Stop the lapic timer */
  399. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  400. local_irq_enable();
  401. /* Jiffies delta */
  402. deltaj = lapic_cal_j2 - lapic_cal_j1;
  403. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  404. /* Check, if the jiffies result is consistent */
  405. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  406. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  407. else
  408. local_apic_timer_verify_ok = 0;
  409. } else
  410. local_irq_enable();
  411. if (!local_apic_timer_verify_ok) {
  412. printk(KERN_WARNING
  413. "APIC timer disabled due to verification failure.\n");
  414. /* No broadcast on UP ! */
  415. if (num_possible_cpus() == 1)
  416. return;
  417. } else {
  418. /*
  419. * If nmi_watchdog is set to IO_APIC, we need the
  420. * PIT/HPET going. Otherwise register lapic as a dummy
  421. * device.
  422. */
  423. if (nmi_watchdog != NMI_IO_APIC)
  424. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  425. }
  426. /* Setup the lapic or request the broadcast */
  427. setup_APIC_timer();
  428. }
  429. void __devinit setup_secondary_APIC_clock(void)
  430. {
  431. setup_APIC_timer();
  432. }
  433. /*
  434. * The guts of the apic timer interrupt
  435. */
  436. static void local_apic_timer_interrupt(void)
  437. {
  438. int cpu = smp_processor_id();
  439. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  440. /*
  441. * Normally we should not be here till LAPIC has been initialized but
  442. * in some cases like kdump, its possible that there is a pending LAPIC
  443. * timer interrupt from previous kernel's context and is delivered in
  444. * new kernel the moment interrupts are enabled.
  445. *
  446. * Interrupts are enabled early and LAPIC is setup much later, hence
  447. * its possible that when we get here evt->event_handler is NULL.
  448. * Check for event_handler being NULL and discard the interrupt as
  449. * spurious.
  450. */
  451. if (!evt->event_handler) {
  452. printk(KERN_WARNING
  453. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  454. /* Switch it off */
  455. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  456. return;
  457. }
  458. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  459. evt->event_handler(evt);
  460. }
  461. /*
  462. * Local APIC timer interrupt. This is the most natural way for doing
  463. * local interrupts, but local timer interrupts can be emulated by
  464. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  465. *
  466. * [ if a single-CPU system runs an SMP kernel then we call the local
  467. * interrupt as well. Thus we cannot inline the local irq ... ]
  468. */
  469. void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
  470. {
  471. struct pt_regs *old_regs = set_irq_regs(regs);
  472. /*
  473. * NOTE! We'd better ACK the irq immediately,
  474. * because timer handling can be slow.
  475. */
  476. ack_APIC_irq();
  477. /*
  478. * update_process_times() expects us to have done irq_enter().
  479. * Besides, if we don't timer interrupts ignore the global
  480. * interrupt lock, which is the WrongThing (tm) to do.
  481. */
  482. irq_enter();
  483. local_apic_timer_interrupt();
  484. irq_exit();
  485. set_irq_regs(old_regs);
  486. }
  487. int setup_profiling_timer(unsigned int multiplier)
  488. {
  489. return -EINVAL;
  490. }
  491. /*
  492. * Local APIC start and shutdown
  493. */
  494. /**
  495. * clear_local_APIC - shutdown the local APIC
  496. *
  497. * This is called, when a CPU is disabled and before rebooting, so the state of
  498. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  499. * leftovers during boot.
  500. */
  501. void clear_local_APIC(void)
  502. {
  503. int maxlvt = lapic_get_maxlvt();
  504. unsigned long v;
  505. /*
  506. * Masking an LVT entry can trigger a local APIC error
  507. * if the vector is zero. Mask LVTERR first to prevent this.
  508. */
  509. if (maxlvt >= 3) {
  510. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  511. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  512. }
  513. /*
  514. * Careful: we have to set masks only first to deassert
  515. * any level-triggered sources.
  516. */
  517. v = apic_read(APIC_LVTT);
  518. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  519. v = apic_read(APIC_LVT0);
  520. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  521. v = apic_read(APIC_LVT1);
  522. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  523. if (maxlvt >= 4) {
  524. v = apic_read(APIC_LVTPC);
  525. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  526. }
  527. /* lets not touch this if we didn't frob it */
  528. #ifdef CONFIG_X86_MCE_P4THERMAL
  529. if (maxlvt >= 5) {
  530. v = apic_read(APIC_LVTTHMR);
  531. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  532. }
  533. #endif
  534. /*
  535. * Clean APIC state for other OSs:
  536. */
  537. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  538. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  539. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  540. if (maxlvt >= 3)
  541. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  542. if (maxlvt >= 4)
  543. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  544. #ifdef CONFIG_X86_MCE_P4THERMAL
  545. if (maxlvt >= 5)
  546. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  547. #endif
  548. /* Integrated APIC (!82489DX) ? */
  549. if (lapic_is_integrated()) {
  550. if (maxlvt > 3)
  551. /* Clear ESR due to Pentium errata 3AP and 11AP */
  552. apic_write(APIC_ESR, 0);
  553. apic_read(APIC_ESR);
  554. }
  555. }
  556. /**
  557. * disable_local_APIC - clear and disable the local APIC
  558. */
  559. void disable_local_APIC(void)
  560. {
  561. unsigned long value;
  562. clear_local_APIC();
  563. /*
  564. * Disable APIC (implies clearing of registers
  565. * for 82489DX!).
  566. */
  567. value = apic_read(APIC_SPIV);
  568. value &= ~APIC_SPIV_APIC_ENABLED;
  569. apic_write_around(APIC_SPIV, value);
  570. /*
  571. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  572. * restore the disabled state.
  573. */
  574. if (enabled_via_apicbase) {
  575. unsigned int l, h;
  576. rdmsr(MSR_IA32_APICBASE, l, h);
  577. l &= ~MSR_IA32_APICBASE_ENABLE;
  578. wrmsr(MSR_IA32_APICBASE, l, h);
  579. }
  580. }
  581. /*
  582. * If Linux enabled the LAPIC against the BIOS default disable it down before
  583. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  584. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  585. * for the case where Linux didn't enable the LAPIC.
  586. */
  587. void lapic_shutdown(void)
  588. {
  589. unsigned long flags;
  590. if (!cpu_has_apic)
  591. return;
  592. local_irq_save(flags);
  593. clear_local_APIC();
  594. if (enabled_via_apicbase)
  595. disable_local_APIC();
  596. local_irq_restore(flags);
  597. }
  598. /*
  599. * This is to verify that we're looking at a real local APIC.
  600. * Check these against your board if the CPUs aren't getting
  601. * started for no apparent reason.
  602. */
  603. int __init verify_local_APIC(void)
  604. {
  605. unsigned int reg0, reg1;
  606. /*
  607. * The version register is read-only in a real APIC.
  608. */
  609. reg0 = apic_read(APIC_LVR);
  610. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  611. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  612. reg1 = apic_read(APIC_LVR);
  613. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  614. /*
  615. * The two version reads above should print the same
  616. * numbers. If the second one is different, then we
  617. * poke at a non-APIC.
  618. */
  619. if (reg1 != reg0)
  620. return 0;
  621. /*
  622. * Check if the version looks reasonably.
  623. */
  624. reg1 = GET_APIC_VERSION(reg0);
  625. if (reg1 == 0x00 || reg1 == 0xff)
  626. return 0;
  627. reg1 = lapic_get_maxlvt();
  628. if (reg1 < 0x02 || reg1 == 0xff)
  629. return 0;
  630. /*
  631. * The ID register is read/write in a real APIC.
  632. */
  633. reg0 = apic_read(APIC_ID);
  634. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  635. /*
  636. * The next two are just to see if we have sane values.
  637. * They're only really relevant if we're in Virtual Wire
  638. * compatibility mode, but most boxes are anymore.
  639. */
  640. reg0 = apic_read(APIC_LVT0);
  641. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  642. reg1 = apic_read(APIC_LVT1);
  643. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  644. return 1;
  645. }
  646. /**
  647. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  648. */
  649. void __init sync_Arb_IDs(void)
  650. {
  651. /*
  652. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  653. * needed on AMD.
  654. */
  655. if (modern_apic())
  656. return;
  657. /*
  658. * Wait for idle.
  659. */
  660. apic_wait_icr_idle();
  661. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  662. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  663. | APIC_DM_INIT);
  664. }
  665. /*
  666. * An initial setup of the virtual wire mode.
  667. */
  668. void __init init_bsp_APIC(void)
  669. {
  670. unsigned long value;
  671. /*
  672. * Don't do the setup now if we have a SMP BIOS as the
  673. * through-I/O-APIC virtual wire mode might be active.
  674. */
  675. if (smp_found_config || !cpu_has_apic)
  676. return;
  677. /*
  678. * Do not trust the local APIC being empty at bootup.
  679. */
  680. clear_local_APIC();
  681. /*
  682. * Enable APIC.
  683. */
  684. value = apic_read(APIC_SPIV);
  685. value &= ~APIC_VECTOR_MASK;
  686. value |= APIC_SPIV_APIC_ENABLED;
  687. /* This bit is reserved on P4/Xeon and should be cleared */
  688. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  689. (boot_cpu_data.x86 == 15))
  690. value &= ~APIC_SPIV_FOCUS_DISABLED;
  691. else
  692. value |= APIC_SPIV_FOCUS_DISABLED;
  693. value |= SPURIOUS_APIC_VECTOR;
  694. apic_write_around(APIC_SPIV, value);
  695. /*
  696. * Set up the virtual wire mode.
  697. */
  698. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  699. value = APIC_DM_NMI;
  700. if (!lapic_is_integrated()) /* 82489DX */
  701. value |= APIC_LVT_LEVEL_TRIGGER;
  702. apic_write_around(APIC_LVT1, value);
  703. }
  704. /**
  705. * setup_local_APIC - setup the local APIC
  706. */
  707. void __devinit setup_local_APIC(void)
  708. {
  709. unsigned long oldvalue, value, maxlvt, integrated;
  710. int i, j;
  711. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  712. if (esr_disable) {
  713. apic_write(APIC_ESR, 0);
  714. apic_write(APIC_ESR, 0);
  715. apic_write(APIC_ESR, 0);
  716. apic_write(APIC_ESR, 0);
  717. }
  718. integrated = lapic_is_integrated();
  719. /*
  720. * Double-check whether this APIC is really registered.
  721. */
  722. if (!apic_id_registered())
  723. BUG();
  724. /*
  725. * Intel recommends to set DFR, LDR and TPR before enabling
  726. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  727. * document number 292116). So here it goes...
  728. */
  729. init_apic_ldr();
  730. /*
  731. * Set Task Priority to 'accept all'. We never change this
  732. * later on.
  733. */
  734. value = apic_read(APIC_TASKPRI);
  735. value &= ~APIC_TPRI_MASK;
  736. apic_write_around(APIC_TASKPRI, value);
  737. /*
  738. * After a crash, we no longer service the interrupts and a pending
  739. * interrupt from previous kernel might still have ISR bit set.
  740. *
  741. * Most probably by now CPU has serviced that pending interrupt and
  742. * it might not have done the ack_APIC_irq() because it thought,
  743. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  744. * does not clear the ISR bit and cpu thinks it has already serivced
  745. * the interrupt. Hence a vector might get locked. It was noticed
  746. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  747. */
  748. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  749. value = apic_read(APIC_ISR + i*0x10);
  750. for (j = 31; j >= 0; j--) {
  751. if (value & (1<<j))
  752. ack_APIC_irq();
  753. }
  754. }
  755. /*
  756. * Now that we are all set up, enable the APIC
  757. */
  758. value = apic_read(APIC_SPIV);
  759. value &= ~APIC_VECTOR_MASK;
  760. /*
  761. * Enable APIC
  762. */
  763. value |= APIC_SPIV_APIC_ENABLED;
  764. /*
  765. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  766. * certain networking cards. If high frequency interrupts are
  767. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  768. * entry is masked/unmasked at a high rate as well then sooner or
  769. * later IOAPIC line gets 'stuck', no more interrupts are received
  770. * from the device. If focus CPU is disabled then the hang goes
  771. * away, oh well :-(
  772. *
  773. * [ This bug can be reproduced easily with a level-triggered
  774. * PCI Ne2000 networking cards and PII/PIII processors, dual
  775. * BX chipset. ]
  776. */
  777. /*
  778. * Actually disabling the focus CPU check just makes the hang less
  779. * frequent as it makes the interrupt distributon model be more
  780. * like LRU than MRU (the short-term load is more even across CPUs).
  781. * See also the comment in end_level_ioapic_irq(). --macro
  782. */
  783. /* Enable focus processor (bit==0) */
  784. value &= ~APIC_SPIV_FOCUS_DISABLED;
  785. /*
  786. * Set spurious IRQ vector
  787. */
  788. value |= SPURIOUS_APIC_VECTOR;
  789. apic_write_around(APIC_SPIV, value);
  790. /*
  791. * Set up LVT0, LVT1:
  792. *
  793. * set up through-local-APIC on the BP's LINT0. This is not
  794. * strictly necessery in pure symmetric-IO mode, but sometimes
  795. * we delegate interrupts to the 8259A.
  796. */
  797. /*
  798. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  799. */
  800. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  801. if (!smp_processor_id() && (pic_mode || !value)) {
  802. value = APIC_DM_EXTINT;
  803. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  804. smp_processor_id());
  805. } else {
  806. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  807. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  808. smp_processor_id());
  809. }
  810. apic_write_around(APIC_LVT0, value);
  811. /*
  812. * only the BP should see the LINT1 NMI signal, obviously.
  813. */
  814. if (!smp_processor_id())
  815. value = APIC_DM_NMI;
  816. else
  817. value = APIC_DM_NMI | APIC_LVT_MASKED;
  818. if (!integrated) /* 82489DX */
  819. value |= APIC_LVT_LEVEL_TRIGGER;
  820. apic_write_around(APIC_LVT1, value);
  821. if (integrated && !esr_disable) { /* !82489DX */
  822. maxlvt = lapic_get_maxlvt();
  823. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  824. apic_write(APIC_ESR, 0);
  825. oldvalue = apic_read(APIC_ESR);
  826. /* enables sending errors */
  827. value = ERROR_APIC_VECTOR;
  828. apic_write_around(APIC_LVTERR, value);
  829. /*
  830. * spec says clear errors after enabling vector.
  831. */
  832. if (maxlvt > 3)
  833. apic_write(APIC_ESR, 0);
  834. value = apic_read(APIC_ESR);
  835. if (value != oldvalue)
  836. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  837. "vector: 0x%08lx after: 0x%08lx\n",
  838. oldvalue, value);
  839. } else {
  840. if (esr_disable)
  841. /*
  842. * Something untraceble is creating bad interrupts on
  843. * secondary quads ... for the moment, just leave the
  844. * ESR disabled - we can't do anything useful with the
  845. * errors anyway - mbligh
  846. */
  847. printk(KERN_INFO "Leaving ESR disabled.\n");
  848. else
  849. printk(KERN_INFO "No ESR for 82489DX.\n");
  850. }
  851. /* Disable the local apic timer */
  852. value = apic_read(APIC_LVTT);
  853. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  854. apic_write_around(APIC_LVTT, value);
  855. setup_apic_nmi_watchdog(NULL);
  856. apic_pm_activate();
  857. }
  858. /*
  859. * Detect and initialize APIC
  860. */
  861. static int __init detect_init_APIC (void)
  862. {
  863. u32 h, l, features;
  864. /* Disabled by kernel option? */
  865. if (enable_local_apic < 0)
  866. return -1;
  867. switch (boot_cpu_data.x86_vendor) {
  868. case X86_VENDOR_AMD:
  869. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  870. (boot_cpu_data.x86 == 15))
  871. break;
  872. goto no_apic;
  873. case X86_VENDOR_INTEL:
  874. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  875. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  876. break;
  877. goto no_apic;
  878. default:
  879. goto no_apic;
  880. }
  881. if (!cpu_has_apic) {
  882. /*
  883. * Over-ride BIOS and try to enable the local APIC only if
  884. * "lapic" specified.
  885. */
  886. if (enable_local_apic <= 0) {
  887. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  888. "you can enable it with \"lapic\"\n");
  889. return -1;
  890. }
  891. /*
  892. * Some BIOSes disable the local APIC in the APIC_BASE
  893. * MSR. This can only be done in software for Intel P6 or later
  894. * and AMD K7 (Model > 1) or later.
  895. */
  896. rdmsr(MSR_IA32_APICBASE, l, h);
  897. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  898. printk(KERN_INFO
  899. "Local APIC disabled by BIOS -- reenabling.\n");
  900. l &= ~MSR_IA32_APICBASE_BASE;
  901. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  902. wrmsr(MSR_IA32_APICBASE, l, h);
  903. enabled_via_apicbase = 1;
  904. }
  905. }
  906. /*
  907. * The APIC feature bit should now be enabled
  908. * in `cpuid'
  909. */
  910. features = cpuid_edx(1);
  911. if (!(features & (1 << X86_FEATURE_APIC))) {
  912. printk(KERN_WARNING "Could not enable APIC!\n");
  913. return -1;
  914. }
  915. set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  916. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  917. /* The BIOS may have set up the APIC at some other address */
  918. rdmsr(MSR_IA32_APICBASE, l, h);
  919. if (l & MSR_IA32_APICBASE_ENABLE)
  920. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  921. if (nmi_watchdog != NMI_NONE)
  922. nmi_watchdog = NMI_LOCAL_APIC;
  923. printk(KERN_INFO "Found and enabled local APIC!\n");
  924. apic_pm_activate();
  925. return 0;
  926. no_apic:
  927. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  928. return -1;
  929. }
  930. /**
  931. * init_apic_mappings - initialize APIC mappings
  932. */
  933. void __init init_apic_mappings(void)
  934. {
  935. unsigned long apic_phys;
  936. /*
  937. * If no local APIC can be found then set up a fake all
  938. * zeroes page to simulate the local APIC and another
  939. * one for the IO-APIC.
  940. */
  941. if (!smp_found_config && detect_init_APIC()) {
  942. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  943. apic_phys = __pa(apic_phys);
  944. } else
  945. apic_phys = mp_lapic_addr;
  946. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  947. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  948. apic_phys);
  949. /*
  950. * Fetch the APIC ID of the BSP in case we have a
  951. * default configuration (or the MP table is broken).
  952. */
  953. if (boot_cpu_physical_apicid == -1U)
  954. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  955. #ifdef CONFIG_X86_IO_APIC
  956. {
  957. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  958. int i;
  959. for (i = 0; i < nr_ioapics; i++) {
  960. if (smp_found_config) {
  961. ioapic_phys = mp_ioapics[i].mpc_apicaddr;
  962. if (!ioapic_phys) {
  963. printk(KERN_ERR
  964. "WARNING: bogus zero IO-APIC "
  965. "address found in MPTABLE, "
  966. "disabling IO/APIC support!\n");
  967. smp_found_config = 0;
  968. skip_ioapic_setup = 1;
  969. goto fake_ioapic_page;
  970. }
  971. } else {
  972. fake_ioapic_page:
  973. ioapic_phys = (unsigned long)
  974. alloc_bootmem_pages(PAGE_SIZE);
  975. ioapic_phys = __pa(ioapic_phys);
  976. }
  977. set_fixmap_nocache(idx, ioapic_phys);
  978. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  979. __fix_to_virt(idx), ioapic_phys);
  980. idx++;
  981. }
  982. }
  983. #endif
  984. }
  985. /*
  986. * This initializes the IO-APIC and APIC hardware if this is
  987. * a UP kernel.
  988. */
  989. int __init APIC_init_uniprocessor (void)
  990. {
  991. if (enable_local_apic < 0)
  992. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  993. if (!smp_found_config && !cpu_has_apic)
  994. return -1;
  995. /*
  996. * Complain if the BIOS pretends there is one.
  997. */
  998. if (!cpu_has_apic &&
  999. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1000. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1001. boot_cpu_physical_apicid);
  1002. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1003. return -1;
  1004. }
  1005. verify_local_APIC();
  1006. connect_bsp_APIC();
  1007. /*
  1008. * Hack: In case of kdump, after a crash, kernel might be booting
  1009. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1010. * might be zero if read from MP tables. Get it from LAPIC.
  1011. */
  1012. #ifdef CONFIG_CRASH_DUMP
  1013. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  1014. #endif
  1015. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1016. setup_local_APIC();
  1017. #ifdef CONFIG_X86_IO_APIC
  1018. if (smp_found_config)
  1019. if (!skip_ioapic_setup && nr_ioapics)
  1020. setup_IO_APIC();
  1021. #endif
  1022. setup_boot_clock();
  1023. return 0;
  1024. }
  1025. /*
  1026. * APIC command line parameters
  1027. */
  1028. static int __init parse_lapic(char *arg)
  1029. {
  1030. enable_local_apic = 1;
  1031. return 0;
  1032. }
  1033. early_param("lapic", parse_lapic);
  1034. static int __init parse_nolapic(char *arg)
  1035. {
  1036. enable_local_apic = -1;
  1037. clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
  1038. return 0;
  1039. }
  1040. early_param("nolapic", parse_nolapic);
  1041. static int __init parse_disable_lapic_timer(char *arg)
  1042. {
  1043. local_apic_timer_disabled = 1;
  1044. return 0;
  1045. }
  1046. early_param("nolapic_timer", parse_disable_lapic_timer);
  1047. static int __init parse_lapic_timer_c2_ok(char *arg)
  1048. {
  1049. local_apic_timer_c2_ok = 1;
  1050. return 0;
  1051. }
  1052. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1053. static int __init apic_set_verbosity(char *str)
  1054. {
  1055. if (strcmp("debug", str) == 0)
  1056. apic_verbosity = APIC_DEBUG;
  1057. else if (strcmp("verbose", str) == 0)
  1058. apic_verbosity = APIC_VERBOSE;
  1059. return 1;
  1060. }
  1061. __setup("apic=", apic_set_verbosity);
  1062. /*
  1063. * Local APIC interrupts
  1064. */
  1065. /*
  1066. * This interrupt should _never_ happen with our APIC/SMP architecture
  1067. */
  1068. void smp_spurious_interrupt(struct pt_regs *regs)
  1069. {
  1070. unsigned long v;
  1071. irq_enter();
  1072. /*
  1073. * Check if this really is a spurious interrupt and ACK it
  1074. * if it is a vectored one. Just in case...
  1075. * Spurious interrupts should not be ACKed.
  1076. */
  1077. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1078. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1079. ack_APIC_irq();
  1080. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1081. printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
  1082. "should never happen.\n", smp_processor_id());
  1083. irq_exit();
  1084. }
  1085. /*
  1086. * This interrupt should never happen with our APIC/SMP architecture
  1087. */
  1088. void smp_error_interrupt(struct pt_regs *regs)
  1089. {
  1090. unsigned long v, v1;
  1091. irq_enter();
  1092. /* First tickle the hardware, only then report what went on. -- REW */
  1093. v = apic_read(APIC_ESR);
  1094. apic_write(APIC_ESR, 0);
  1095. v1 = apic_read(APIC_ESR);
  1096. ack_APIC_irq();
  1097. atomic_inc(&irq_err_count);
  1098. /* Here is what the APIC error bits mean:
  1099. 0: Send CS error
  1100. 1: Receive CS error
  1101. 2: Send accept error
  1102. 3: Receive accept error
  1103. 4: Reserved
  1104. 5: Send illegal vector
  1105. 6: Received illegal vector
  1106. 7: Illegal register address
  1107. */
  1108. printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
  1109. smp_processor_id(), v , v1);
  1110. irq_exit();
  1111. }
  1112. /*
  1113. * Initialize APIC interrupts
  1114. */
  1115. void __init apic_intr_init(void)
  1116. {
  1117. #ifdef CONFIG_SMP
  1118. smp_intr_init();
  1119. #endif
  1120. /* self generated IPI for local APIC timer */
  1121. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  1122. /* IPI vectors for APIC spurious and error interrupts */
  1123. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  1124. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  1125. /* thermal monitor LVT interrupt */
  1126. #ifdef CONFIG_X86_MCE_P4THERMAL
  1127. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  1128. #endif
  1129. }
  1130. /**
  1131. * connect_bsp_APIC - attach the APIC to the interrupt system
  1132. */
  1133. void __init connect_bsp_APIC(void)
  1134. {
  1135. if (pic_mode) {
  1136. /*
  1137. * Do not trust the local APIC being empty at bootup.
  1138. */
  1139. clear_local_APIC();
  1140. /*
  1141. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1142. * local APIC to INT and NMI lines.
  1143. */
  1144. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1145. "enabling APIC mode.\n");
  1146. outb(0x70, 0x22);
  1147. outb(0x01, 0x23);
  1148. }
  1149. enable_apic_mode();
  1150. }
  1151. /**
  1152. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1153. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1154. *
  1155. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1156. * APIC is disabled.
  1157. */
  1158. void disconnect_bsp_APIC(int virt_wire_setup)
  1159. {
  1160. if (pic_mode) {
  1161. /*
  1162. * Put the board back into PIC mode (has an effect only on
  1163. * certain older boards). Note that APIC interrupts, including
  1164. * IPIs, won't work beyond this point! The only exception are
  1165. * INIT IPIs.
  1166. */
  1167. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1168. "entering PIC mode.\n");
  1169. outb(0x70, 0x22);
  1170. outb(0x00, 0x23);
  1171. } else {
  1172. /* Go back to Virtual Wire compatibility mode */
  1173. unsigned long value;
  1174. /* For the spurious interrupt use vector F, and enable it */
  1175. value = apic_read(APIC_SPIV);
  1176. value &= ~APIC_VECTOR_MASK;
  1177. value |= APIC_SPIV_APIC_ENABLED;
  1178. value |= 0xf;
  1179. apic_write_around(APIC_SPIV, value);
  1180. if (!virt_wire_setup) {
  1181. /*
  1182. * For LVT0 make it edge triggered, active high,
  1183. * external and enabled
  1184. */
  1185. value = apic_read(APIC_LVT0);
  1186. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1187. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1188. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
  1189. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1190. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1191. apic_write_around(APIC_LVT0, value);
  1192. } else {
  1193. /* Disable LVT0 */
  1194. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  1195. }
  1196. /*
  1197. * For LVT1 make it edge triggered, active high, nmi and
  1198. * enabled
  1199. */
  1200. value = apic_read(APIC_LVT1);
  1201. value &= ~(
  1202. APIC_MODE_MASK | APIC_SEND_PENDING |
  1203. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1204. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1205. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1206. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1207. apic_write_around(APIC_LVT1, value);
  1208. }
  1209. }
  1210. /*
  1211. * Power management
  1212. */
  1213. #ifdef CONFIG_PM
  1214. static struct {
  1215. int active;
  1216. /* r/w apic fields */
  1217. unsigned int apic_id;
  1218. unsigned int apic_taskpri;
  1219. unsigned int apic_ldr;
  1220. unsigned int apic_dfr;
  1221. unsigned int apic_spiv;
  1222. unsigned int apic_lvtt;
  1223. unsigned int apic_lvtpc;
  1224. unsigned int apic_lvt0;
  1225. unsigned int apic_lvt1;
  1226. unsigned int apic_lvterr;
  1227. unsigned int apic_tmict;
  1228. unsigned int apic_tdcr;
  1229. unsigned int apic_thmr;
  1230. } apic_pm_state;
  1231. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1232. {
  1233. unsigned long flags;
  1234. int maxlvt;
  1235. if (!apic_pm_state.active)
  1236. return 0;
  1237. maxlvt = lapic_get_maxlvt();
  1238. apic_pm_state.apic_id = apic_read(APIC_ID);
  1239. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1240. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1241. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1242. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1243. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1244. if (maxlvt >= 4)
  1245. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1246. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1247. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1248. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1249. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1250. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1251. #ifdef CONFIG_X86_MCE_P4THERMAL
  1252. if (maxlvt >= 5)
  1253. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1254. #endif
  1255. local_irq_save(flags);
  1256. disable_local_APIC();
  1257. local_irq_restore(flags);
  1258. return 0;
  1259. }
  1260. static int lapic_resume(struct sys_device *dev)
  1261. {
  1262. unsigned int l, h;
  1263. unsigned long flags;
  1264. int maxlvt;
  1265. if (!apic_pm_state.active)
  1266. return 0;
  1267. maxlvt = lapic_get_maxlvt();
  1268. local_irq_save(flags);
  1269. /*
  1270. * Make sure the APICBASE points to the right address
  1271. *
  1272. * FIXME! This will be wrong if we ever support suspend on
  1273. * SMP! We'll need to do this as part of the CPU restore!
  1274. */
  1275. rdmsr(MSR_IA32_APICBASE, l, h);
  1276. l &= ~MSR_IA32_APICBASE_BASE;
  1277. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1278. wrmsr(MSR_IA32_APICBASE, l, h);
  1279. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1280. apic_write(APIC_ID, apic_pm_state.apic_id);
  1281. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1282. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1283. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1284. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1285. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1286. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1287. #ifdef CONFIG_X86_MCE_P4THERMAL
  1288. if (maxlvt >= 5)
  1289. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1290. #endif
  1291. if (maxlvt >= 4)
  1292. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1293. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1294. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1295. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1296. apic_write(APIC_ESR, 0);
  1297. apic_read(APIC_ESR);
  1298. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1299. apic_write(APIC_ESR, 0);
  1300. apic_read(APIC_ESR);
  1301. local_irq_restore(flags);
  1302. return 0;
  1303. }
  1304. /*
  1305. * This device has no shutdown method - fully functioning local APICs
  1306. * are needed on every CPU up until machine_halt/restart/poweroff.
  1307. */
  1308. static struct sysdev_class lapic_sysclass = {
  1309. set_kset_name("lapic"),
  1310. .resume = lapic_resume,
  1311. .suspend = lapic_suspend,
  1312. };
  1313. static struct sys_device device_lapic = {
  1314. .id = 0,
  1315. .cls = &lapic_sysclass,
  1316. };
  1317. static void __devinit apic_pm_activate(void)
  1318. {
  1319. apic_pm_state.active = 1;
  1320. }
  1321. static int __init init_lapic_sysfs(void)
  1322. {
  1323. int error;
  1324. if (!cpu_has_apic)
  1325. return 0;
  1326. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1327. error = sysdev_class_register(&lapic_sysclass);
  1328. if (!error)
  1329. error = sysdev_register(&device_lapic);
  1330. return error;
  1331. }
  1332. device_initcall(init_lapic_sysfs);
  1333. #else /* CONFIG_PM */
  1334. static void apic_pm_activate(void) { }
  1335. #endif /* CONFIG_PM */