apic_32.c 45 KB

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