apic.c 39 KB

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