apic_32.c 44 KB

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