apic_32.c 48 KB

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