apic.c 39 KB

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