apic.c 39 KB

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