apic_32.c 40 KB

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