apic_64.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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/ioport.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/acpi_pmtmr.h>
  27. #include <linux/module.h>
  28. #include <asm/atomic.h>
  29. #include <asm/smp.h>
  30. #include <asm/mtrr.h>
  31. #include <asm/mpspec.h>
  32. #include <asm/hpet.h>
  33. #include <asm/pgalloc.h>
  34. #include <asm/nmi.h>
  35. #include <asm/idle.h>
  36. #include <asm/proto.h>
  37. #include <asm/timex.h>
  38. #include <asm/apic.h>
  39. #include <mach_ipi.h>
  40. #include <mach_apic.h>
  41. static int disable_apic_timer __cpuinitdata;
  42. static int apic_calibrate_pmtmr __initdata;
  43. int disable_apic;
  44. /* Local APIC timer works in C2 */
  45. int local_apic_timer_c2_ok;
  46. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  47. /*
  48. * Debug level, exported for io_apic.c
  49. */
  50. unsigned int apic_verbosity;
  51. /* Have we found an MP table */
  52. int smp_found_config;
  53. static struct resource lapic_resource = {
  54. .name = "Local APIC",
  55. .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
  56. };
  57. static unsigned int calibration_result;
  58. static int lapic_next_event(unsigned long delta,
  59. struct clock_event_device *evt);
  60. static void lapic_timer_setup(enum clock_event_mode mode,
  61. struct clock_event_device *evt);
  62. static void lapic_timer_broadcast(cpumask_t mask);
  63. static void apic_pm_activate(void);
  64. static struct clock_event_device lapic_clockevent = {
  65. .name = "lapic",
  66. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  67. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  68. .shift = 32,
  69. .set_mode = lapic_timer_setup,
  70. .set_next_event = lapic_next_event,
  71. .broadcast = lapic_timer_broadcast,
  72. .rating = 100,
  73. .irq = -1,
  74. };
  75. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  76. static unsigned long apic_phys;
  77. unsigned long mp_lapic_addr;
  78. unsigned int __cpuinitdata maxcpus = NR_CPUS;
  79. /*
  80. * Get the LAPIC version
  81. */
  82. static inline int lapic_get_version(void)
  83. {
  84. return GET_APIC_VERSION(apic_read(APIC_LVR));
  85. }
  86. /*
  87. * Check, if the APIC is integrated or a seperate chip
  88. */
  89. static inline int lapic_is_integrated(void)
  90. {
  91. return 1;
  92. }
  93. /*
  94. * Check, whether this is a modern or a first generation APIC
  95. */
  96. static int modern_apic(void)
  97. {
  98. /* AMD systems use old APIC versions, so check the CPU */
  99. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  100. boot_cpu_data.x86 >= 0xf)
  101. return 1;
  102. return lapic_get_version() >= 0x14;
  103. }
  104. void apic_wait_icr_idle(void)
  105. {
  106. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  107. cpu_relax();
  108. }
  109. u32 safe_apic_wait_icr_idle(void)
  110. {
  111. u32 send_status;
  112. int timeout;
  113. timeout = 0;
  114. do {
  115. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  116. if (!send_status)
  117. break;
  118. udelay(100);
  119. } while (timeout++ < 1000);
  120. return send_status;
  121. }
  122. /**
  123. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  124. */
  125. void __cpuinit enable_NMI_through_LVT0(void)
  126. {
  127. unsigned int v;
  128. /* unmask and set to NMI */
  129. v = APIC_DM_NMI;
  130. apic_write(APIC_LVT0, v);
  131. }
  132. /**
  133. * lapic_get_maxlvt - get the maximum number of local vector table entries
  134. */
  135. int lapic_get_maxlvt(void)
  136. {
  137. unsigned int v, maxlvt;
  138. v = apic_read(APIC_LVR);
  139. maxlvt = GET_APIC_MAXLVT(v);
  140. return maxlvt;
  141. }
  142. /*
  143. * This function sets up the local APIC timer, with a timeout of
  144. * 'clocks' APIC bus clock. During calibration we actually call
  145. * this function twice on the boot CPU, once with a bogus timeout
  146. * value, second time for real. The other (noncalibrating) CPUs
  147. * call this function only once, with the real, calibrated value.
  148. *
  149. * We do reads before writes even if unnecessary, to get around the
  150. * P5 APIC double write bug.
  151. */
  152. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  153. {
  154. unsigned int lvtt_value, tmp_value;
  155. lvtt_value = LOCAL_TIMER_VECTOR;
  156. if (!oneshot)
  157. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  158. if (!irqen)
  159. lvtt_value |= APIC_LVT_MASKED;
  160. apic_write(APIC_LVTT, lvtt_value);
  161. /*
  162. * Divide PICLK by 16
  163. */
  164. tmp_value = apic_read(APIC_TDCR);
  165. apic_write(APIC_TDCR, (tmp_value
  166. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  167. | APIC_TDR_DIV_16);
  168. if (!oneshot)
  169. apic_write(APIC_TMICT, clocks);
  170. }
  171. /*
  172. * Setup extended LVT, AMD specific (K8, family 10h)
  173. *
  174. * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
  175. * MCE interrupts are supported. Thus MCE offset must be set to 0.
  176. *
  177. * If mask=1, the LVT entry does not generate interrupts while mask=0
  178. * enables the vector. See also the BKDGs.
  179. */
  180. #define APIC_EILVT_LVTOFF_MCE 0
  181. #define APIC_EILVT_LVTOFF_IBS 1
  182. static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
  183. {
  184. unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
  185. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  186. apic_write(reg, v);
  187. }
  188. u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
  189. {
  190. setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
  191. return APIC_EILVT_LVTOFF_MCE;
  192. }
  193. u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
  194. {
  195. setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
  196. return APIC_EILVT_LVTOFF_IBS;
  197. }
  198. EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
  199. /*
  200. * Program the next event, relative to now
  201. */
  202. static int lapic_next_event(unsigned long delta,
  203. struct clock_event_device *evt)
  204. {
  205. apic_write(APIC_TMICT, delta);
  206. return 0;
  207. }
  208. /*
  209. * Setup the lapic timer in periodic or oneshot mode
  210. */
  211. static void lapic_timer_setup(enum clock_event_mode mode,
  212. struct clock_event_device *evt)
  213. {
  214. unsigned long flags;
  215. unsigned int v;
  216. /* Lapic used as dummy for broadcast ? */
  217. if (evt->features & CLOCK_EVT_FEAT_DUMMY)
  218. return;
  219. local_irq_save(flags);
  220. switch (mode) {
  221. case CLOCK_EVT_MODE_PERIODIC:
  222. case CLOCK_EVT_MODE_ONESHOT:
  223. __setup_APIC_LVTT(calibration_result,
  224. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  225. break;
  226. case CLOCK_EVT_MODE_UNUSED:
  227. case CLOCK_EVT_MODE_SHUTDOWN:
  228. v = apic_read(APIC_LVTT);
  229. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  230. apic_write(APIC_LVTT, v);
  231. break;
  232. case CLOCK_EVT_MODE_RESUME:
  233. /* Nothing to do here */
  234. break;
  235. }
  236. local_irq_restore(flags);
  237. }
  238. /*
  239. * Local APIC timer broadcast function
  240. */
  241. static void lapic_timer_broadcast(cpumask_t mask)
  242. {
  243. #ifdef CONFIG_SMP
  244. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  245. #endif
  246. }
  247. /*
  248. * Setup the local APIC timer for this CPU. Copy the initilized values
  249. * of the boot CPU and register the clock event in the framework.
  250. */
  251. static void setup_APIC_timer(void)
  252. {
  253. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  254. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  255. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  256. clockevents_register_device(levt);
  257. }
  258. /*
  259. * In this function we calibrate APIC bus clocks to the external
  260. * timer. Unfortunately we cannot use jiffies and the timer irq
  261. * to calibrate, since some later bootup code depends on getting
  262. * the first irq? Ugh.
  263. *
  264. * We want to do the calibration only once since we
  265. * want to have local timer irqs syncron. CPUs connected
  266. * by the same APIC bus have the very same bus frequency.
  267. * And we want to have irqs off anyways, no accidental
  268. * APIC irq that way.
  269. */
  270. #define TICK_COUNT 100000000
  271. static int __init calibrate_APIC_clock(void)
  272. {
  273. unsigned apic, apic_start;
  274. unsigned long tsc, tsc_start;
  275. int result;
  276. local_irq_disable();
  277. /*
  278. * Put whatever arbitrary (but long enough) timeout
  279. * value into the APIC clock, we just want to get the
  280. * counter running for calibration.
  281. *
  282. * No interrupt enable !
  283. */
  284. __setup_APIC_LVTT(250000000, 0, 0);
  285. apic_start = apic_read(APIC_TMCCT);
  286. #ifdef CONFIG_X86_PM_TIMER
  287. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  288. pmtimer_wait(5000); /* 5ms wait */
  289. apic = apic_read(APIC_TMCCT);
  290. result = (apic_start - apic) * 1000L / 5;
  291. } else
  292. #endif
  293. {
  294. rdtscll(tsc_start);
  295. do {
  296. apic = apic_read(APIC_TMCCT);
  297. rdtscll(tsc);
  298. } while ((tsc - tsc_start) < TICK_COUNT &&
  299. (apic_start - apic) < TICK_COUNT);
  300. result = (apic_start - apic) * 1000L * tsc_khz /
  301. (tsc - tsc_start);
  302. }
  303. local_irq_enable();
  304. printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
  305. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  306. result / 1000 / 1000, result / 1000 % 1000);
  307. /* Calculate the scaled math multiplication factor */
  308. lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
  309. lapic_clockevent.shift);
  310. lapic_clockevent.max_delta_ns =
  311. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  312. lapic_clockevent.min_delta_ns =
  313. clockevent_delta2ns(0xF, &lapic_clockevent);
  314. calibration_result = result / HZ;
  315. /*
  316. * Do a sanity check on the APIC calibration result
  317. */
  318. if (calibration_result < (1000000 / HZ)) {
  319. printk(KERN_WARNING
  320. "APIC frequency too slow, disabling apic timer\n");
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Setup the boot APIC
  327. *
  328. * Calibrate and verify the result.
  329. */
  330. void __init setup_boot_APIC_clock(void)
  331. {
  332. /*
  333. * The local apic timer can be disabled via the kernel commandline.
  334. * Register the lapic timer as a dummy clock event source on SMP
  335. * systems, so the broadcast mechanism is used. On UP systems simply
  336. * ignore it.
  337. */
  338. if (disable_apic_timer) {
  339. printk(KERN_INFO "Disabling APIC timer\n");
  340. /* No broadcast on UP ! */
  341. if (num_possible_cpus() > 1) {
  342. lapic_clockevent.mult = 1;
  343. setup_APIC_timer();
  344. }
  345. return;
  346. }
  347. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  348. if (calibrate_APIC_clock()) {
  349. /* No broadcast on UP ! */
  350. if (num_possible_cpus() > 1)
  351. setup_APIC_timer();
  352. return;
  353. }
  354. /*
  355. * If nmi_watchdog is set to IO_APIC, we need the
  356. * PIT/HPET going. Otherwise register lapic as a dummy
  357. * device.
  358. */
  359. if (nmi_watchdog != NMI_IO_APIC)
  360. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  361. else
  362. printk(KERN_WARNING "APIC timer registered as dummy,"
  363. " due to nmi_watchdog=%d!\n", nmi_watchdog);
  364. setup_APIC_timer();
  365. }
  366. void __cpuinit setup_secondary_APIC_clock(void)
  367. {
  368. setup_APIC_timer();
  369. }
  370. /*
  371. * The guts of the apic timer interrupt
  372. */
  373. static void local_apic_timer_interrupt(void)
  374. {
  375. int cpu = smp_processor_id();
  376. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  377. /*
  378. * Normally we should not be here till LAPIC has been initialized but
  379. * in some cases like kdump, its possible that there is a pending LAPIC
  380. * timer interrupt from previous kernel's context and is delivered in
  381. * new kernel the moment interrupts are enabled.
  382. *
  383. * Interrupts are enabled early and LAPIC is setup much later, hence
  384. * its possible that when we get here evt->event_handler is NULL.
  385. * Check for event_handler being NULL and discard the interrupt as
  386. * spurious.
  387. */
  388. if (!evt->event_handler) {
  389. printk(KERN_WARNING
  390. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  391. /* Switch it off */
  392. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  393. return;
  394. }
  395. /*
  396. * the NMI deadlock-detector uses this.
  397. */
  398. add_pda(apic_timer_irqs, 1);
  399. evt->event_handler(evt);
  400. }
  401. /*
  402. * Local APIC timer interrupt. This is the most natural way for doing
  403. * local interrupts, but local timer interrupts can be emulated by
  404. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  405. *
  406. * [ if a single-CPU system runs an SMP kernel then we call the local
  407. * interrupt as well. Thus we cannot inline the local irq ... ]
  408. */
  409. void smp_apic_timer_interrupt(struct pt_regs *regs)
  410. {
  411. struct pt_regs *old_regs = set_irq_regs(regs);
  412. /*
  413. * NOTE! We'd better ACK the irq immediately,
  414. * because timer handling can be slow.
  415. */
  416. ack_APIC_irq();
  417. /*
  418. * update_process_times() expects us to have done irq_enter().
  419. * Besides, if we don't timer interrupts ignore the global
  420. * interrupt lock, which is the WrongThing (tm) to do.
  421. */
  422. exit_idle();
  423. irq_enter();
  424. local_apic_timer_interrupt();
  425. irq_exit();
  426. set_irq_regs(old_regs);
  427. }
  428. int setup_profiling_timer(unsigned int multiplier)
  429. {
  430. return -EINVAL;
  431. }
  432. /*
  433. * Local APIC start and shutdown
  434. */
  435. /**
  436. * clear_local_APIC - shutdown the local APIC
  437. *
  438. * This is called, when a CPU is disabled and before rebooting, so the state of
  439. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  440. * leftovers during boot.
  441. */
  442. void clear_local_APIC(void)
  443. {
  444. int maxlvt;
  445. u32 v;
  446. /* APIC hasn't been mapped yet */
  447. if (!apic_phys)
  448. return;
  449. maxlvt = lapic_get_maxlvt();
  450. /*
  451. * Masking an LVT entry can trigger a local APIC error
  452. * if the vector is zero. Mask LVTERR first to prevent this.
  453. */
  454. if (maxlvt >= 3) {
  455. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  456. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  457. }
  458. /*
  459. * Careful: we have to set masks only first to deassert
  460. * any level-triggered sources.
  461. */
  462. v = apic_read(APIC_LVTT);
  463. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  464. v = apic_read(APIC_LVT0);
  465. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  466. v = apic_read(APIC_LVT1);
  467. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  468. if (maxlvt >= 4) {
  469. v = apic_read(APIC_LVTPC);
  470. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  471. }
  472. /*
  473. * Clean APIC state for other OSs:
  474. */
  475. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  476. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  477. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  478. if (maxlvt >= 3)
  479. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  480. if (maxlvt >= 4)
  481. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  482. apic_write(APIC_ESR, 0);
  483. apic_read(APIC_ESR);
  484. }
  485. /**
  486. * disable_local_APIC - clear and disable the local APIC
  487. */
  488. void disable_local_APIC(void)
  489. {
  490. unsigned int value;
  491. clear_local_APIC();
  492. /*
  493. * Disable APIC (implies clearing of registers
  494. * for 82489DX!).
  495. */
  496. value = apic_read(APIC_SPIV);
  497. value &= ~APIC_SPIV_APIC_ENABLED;
  498. apic_write(APIC_SPIV, value);
  499. }
  500. void lapic_shutdown(void)
  501. {
  502. unsigned long flags;
  503. if (!cpu_has_apic)
  504. return;
  505. local_irq_save(flags);
  506. disable_local_APIC();
  507. local_irq_restore(flags);
  508. }
  509. /*
  510. * This is to verify that we're looking at a real local APIC.
  511. * Check these against your board if the CPUs aren't getting
  512. * started for no apparent reason.
  513. */
  514. int __init verify_local_APIC(void)
  515. {
  516. unsigned int reg0, reg1;
  517. /*
  518. * The version register is read-only in a real APIC.
  519. */
  520. reg0 = apic_read(APIC_LVR);
  521. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  522. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  523. reg1 = apic_read(APIC_LVR);
  524. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  525. /*
  526. * The two version reads above should print the same
  527. * numbers. If the second one is different, then we
  528. * poke at a non-APIC.
  529. */
  530. if (reg1 != reg0)
  531. return 0;
  532. /*
  533. * Check if the version looks reasonably.
  534. */
  535. reg1 = GET_APIC_VERSION(reg0);
  536. if (reg1 == 0x00 || reg1 == 0xff)
  537. return 0;
  538. reg1 = lapic_get_maxlvt();
  539. if (reg1 < 0x02 || reg1 == 0xff)
  540. return 0;
  541. /*
  542. * The ID register is read/write in a real APIC.
  543. */
  544. reg0 = read_apic_id();
  545. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  546. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  547. reg1 = read_apic_id();
  548. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  549. apic_write(APIC_ID, reg0);
  550. if (reg1 != (reg0 ^ APIC_ID_MASK))
  551. return 0;
  552. /*
  553. * The next two are just to see if we have sane values.
  554. * They're only really relevant if we're in Virtual Wire
  555. * compatibility mode, but most boxes are anymore.
  556. */
  557. reg0 = apic_read(APIC_LVT0);
  558. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  559. reg1 = apic_read(APIC_LVT1);
  560. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  561. return 1;
  562. }
  563. /**
  564. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  565. */
  566. void __init sync_Arb_IDs(void)
  567. {
  568. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  569. if (modern_apic())
  570. return;
  571. /*
  572. * Wait for idle.
  573. */
  574. apic_wait_icr_idle();
  575. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  576. apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  577. | APIC_DM_INIT);
  578. }
  579. /*
  580. * An initial setup of the virtual wire mode.
  581. */
  582. void __init init_bsp_APIC(void)
  583. {
  584. unsigned int value;
  585. /*
  586. * Don't do the setup now if we have a SMP BIOS as the
  587. * through-I/O-APIC virtual wire mode might be active.
  588. */
  589. if (smp_found_config || !cpu_has_apic)
  590. return;
  591. value = apic_read(APIC_LVR);
  592. /*
  593. * Do not trust the local APIC being empty at bootup.
  594. */
  595. clear_local_APIC();
  596. /*
  597. * Enable APIC.
  598. */
  599. value = apic_read(APIC_SPIV);
  600. value &= ~APIC_VECTOR_MASK;
  601. value |= APIC_SPIV_APIC_ENABLED;
  602. value |= APIC_SPIV_FOCUS_DISABLED;
  603. value |= SPURIOUS_APIC_VECTOR;
  604. apic_write(APIC_SPIV, value);
  605. /*
  606. * Set up the virtual wire mode.
  607. */
  608. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  609. value = APIC_DM_NMI;
  610. apic_write(APIC_LVT1, value);
  611. }
  612. /**
  613. * setup_local_APIC - setup the local APIC
  614. */
  615. void __cpuinit setup_local_APIC(void)
  616. {
  617. unsigned int value;
  618. int i, j;
  619. preempt_disable();
  620. value = apic_read(APIC_LVR);
  621. BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
  622. /*
  623. * Double-check whether this APIC is really registered.
  624. * This is meaningless in clustered apic mode, so we skip it.
  625. */
  626. if (!apic_id_registered())
  627. BUG();
  628. /*
  629. * Intel recommends to set DFR, LDR and TPR before enabling
  630. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  631. * document number 292116). So here it goes...
  632. */
  633. init_apic_ldr();
  634. /*
  635. * Set Task Priority to 'accept all'. We never change this
  636. * later on.
  637. */
  638. value = apic_read(APIC_TASKPRI);
  639. value &= ~APIC_TPRI_MASK;
  640. apic_write(APIC_TASKPRI, value);
  641. /*
  642. * After a crash, we no longer service the interrupts and a pending
  643. * interrupt from previous kernel might still have ISR bit set.
  644. *
  645. * Most probably by now CPU has serviced that pending interrupt and
  646. * it might not have done the ack_APIC_irq() because it thought,
  647. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  648. * does not clear the ISR bit and cpu thinks it has already serivced
  649. * the interrupt. Hence a vector might get locked. It was noticed
  650. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  651. */
  652. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  653. value = apic_read(APIC_ISR + i*0x10);
  654. for (j = 31; j >= 0; j--) {
  655. if (value & (1<<j))
  656. ack_APIC_irq();
  657. }
  658. }
  659. /*
  660. * Now that we are all set up, enable the APIC
  661. */
  662. value = apic_read(APIC_SPIV);
  663. value &= ~APIC_VECTOR_MASK;
  664. /*
  665. * Enable APIC
  666. */
  667. value |= APIC_SPIV_APIC_ENABLED;
  668. /* We always use processor focus */
  669. /*
  670. * Set spurious IRQ vector
  671. */
  672. value |= SPURIOUS_APIC_VECTOR;
  673. apic_write(APIC_SPIV, value);
  674. /*
  675. * Set up LVT0, LVT1:
  676. *
  677. * set up through-local-APIC on the BP's LINT0. This is not
  678. * strictly necessary in pure symmetric-IO mode, but sometimes
  679. * we delegate interrupts to the 8259A.
  680. */
  681. /*
  682. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  683. */
  684. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  685. if (!smp_processor_id() && !value) {
  686. value = APIC_DM_EXTINT;
  687. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  688. smp_processor_id());
  689. } else {
  690. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  691. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  692. smp_processor_id());
  693. }
  694. apic_write(APIC_LVT0, value);
  695. /*
  696. * only the BP should see the LINT1 NMI signal, obviously.
  697. */
  698. if (!smp_processor_id())
  699. value = APIC_DM_NMI;
  700. else
  701. value = APIC_DM_NMI | APIC_LVT_MASKED;
  702. apic_write(APIC_LVT1, value);
  703. preempt_enable();
  704. }
  705. static void __cpuinit lapic_setup_esr(void)
  706. {
  707. unsigned maxlvt = lapic_get_maxlvt();
  708. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
  709. /*
  710. * spec says clear errors after enabling vector.
  711. */
  712. if (maxlvt > 3)
  713. apic_write(APIC_ESR, 0);
  714. }
  715. void __cpuinit end_local_APIC_setup(void)
  716. {
  717. lapic_setup_esr();
  718. setup_apic_nmi_watchdog(NULL);
  719. apic_pm_activate();
  720. }
  721. /*
  722. * Detect and enable local APICs on non-SMP boards.
  723. * Original code written by Keir Fraser.
  724. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  725. * not correctly set up (usually the APIC timer won't work etc.)
  726. */
  727. static int __init detect_init_APIC(void)
  728. {
  729. if (!cpu_has_apic) {
  730. printk(KERN_INFO "No local APIC present\n");
  731. return -1;
  732. }
  733. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  734. boot_cpu_physical_apicid = 0;
  735. return 0;
  736. }
  737. void __init early_init_lapic_mapping(void)
  738. {
  739. unsigned long phys_addr;
  740. /*
  741. * If no local APIC can be found then go out
  742. * : it means there is no mpatable and MADT
  743. */
  744. if (!smp_found_config)
  745. return;
  746. phys_addr = mp_lapic_addr;
  747. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  748. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  749. APIC_BASE, phys_addr);
  750. /*
  751. * Fetch the APIC ID of the BSP in case we have a
  752. * default configuration (or the MP table is broken).
  753. */
  754. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  755. }
  756. /**
  757. * init_apic_mappings - initialize APIC mappings
  758. */
  759. void __init init_apic_mappings(void)
  760. {
  761. /*
  762. * If no local APIC can be found then set up a fake all
  763. * zeroes page to simulate the local APIC and another
  764. * one for the IO-APIC.
  765. */
  766. if (!smp_found_config && detect_init_APIC()) {
  767. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  768. apic_phys = __pa(apic_phys);
  769. } else
  770. apic_phys = mp_lapic_addr;
  771. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  772. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  773. APIC_BASE, apic_phys);
  774. /*
  775. * Fetch the APIC ID of the BSP in case we have a
  776. * default configuration (or the MP table is broken).
  777. */
  778. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  779. }
  780. /*
  781. * This initializes the IO-APIC and APIC hardware if this is
  782. * a UP kernel.
  783. */
  784. int __init APIC_init_uniprocessor(void)
  785. {
  786. if (disable_apic) {
  787. printk(KERN_INFO "Apic disabled\n");
  788. return -1;
  789. }
  790. if (!cpu_has_apic) {
  791. disable_apic = 1;
  792. printk(KERN_INFO "Apic disabled by BIOS\n");
  793. return -1;
  794. }
  795. verify_local_APIC();
  796. connect_bsp_APIC();
  797. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  798. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  799. setup_local_APIC();
  800. /*
  801. * Now enable IO-APICs, actually call clear_IO_APIC
  802. * We need clear_IO_APIC before enabling vector on BP
  803. */
  804. if (!skip_ioapic_setup && nr_ioapics)
  805. enable_IO_APIC();
  806. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  807. localise_nmi_watchdog();
  808. end_local_APIC_setup();
  809. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  810. setup_IO_APIC();
  811. else
  812. nr_ioapics = 0;
  813. setup_boot_APIC_clock();
  814. check_nmi_watchdog();
  815. return 0;
  816. }
  817. /*
  818. * Local APIC interrupts
  819. */
  820. /*
  821. * This interrupt should _never_ happen with our APIC/SMP architecture
  822. */
  823. asmlinkage void smp_spurious_interrupt(void)
  824. {
  825. unsigned int v;
  826. exit_idle();
  827. irq_enter();
  828. /*
  829. * Check if this really is a spurious interrupt and ACK it
  830. * if it is a vectored one. Just in case...
  831. * Spurious interrupts should not be ACKed.
  832. */
  833. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  834. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  835. ack_APIC_irq();
  836. add_pda(irq_spurious_count, 1);
  837. irq_exit();
  838. }
  839. /*
  840. * This interrupt should never happen with our APIC/SMP architecture
  841. */
  842. asmlinkage void smp_error_interrupt(void)
  843. {
  844. unsigned int v, v1;
  845. exit_idle();
  846. irq_enter();
  847. /* First tickle the hardware, only then report what went on. -- REW */
  848. v = apic_read(APIC_ESR);
  849. apic_write(APIC_ESR, 0);
  850. v1 = apic_read(APIC_ESR);
  851. ack_APIC_irq();
  852. atomic_inc(&irq_err_count);
  853. /* Here is what the APIC error bits mean:
  854. 0: Send CS error
  855. 1: Receive CS error
  856. 2: Send accept error
  857. 3: Receive accept error
  858. 4: Reserved
  859. 5: Send illegal vector
  860. 6: Received illegal vector
  861. 7: Illegal register address
  862. */
  863. printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  864. smp_processor_id(), v , v1);
  865. irq_exit();
  866. }
  867. /**
  868. * * connect_bsp_APIC - attach the APIC to the interrupt system
  869. * */
  870. void __init connect_bsp_APIC(void)
  871. {
  872. enable_apic_mode();
  873. }
  874. void disconnect_bsp_APIC(int virt_wire_setup)
  875. {
  876. /* Go back to Virtual Wire compatibility mode */
  877. unsigned long value;
  878. /* For the spurious interrupt use vector F, and enable it */
  879. value = apic_read(APIC_SPIV);
  880. value &= ~APIC_VECTOR_MASK;
  881. value |= APIC_SPIV_APIC_ENABLED;
  882. value |= 0xf;
  883. apic_write(APIC_SPIV, value);
  884. if (!virt_wire_setup) {
  885. /*
  886. * For LVT0 make it edge triggered, active high,
  887. * external and enabled
  888. */
  889. value = apic_read(APIC_LVT0);
  890. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  891. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  892. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  893. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  894. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  895. apic_write(APIC_LVT0, value);
  896. } else {
  897. /* Disable LVT0 */
  898. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  899. }
  900. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  901. value = apic_read(APIC_LVT1);
  902. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  903. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  904. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  905. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  906. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  907. apic_write(APIC_LVT1, value);
  908. }
  909. void __cpuinit generic_processor_info(int apicid, int version)
  910. {
  911. int cpu;
  912. cpumask_t tmp_map;
  913. if (num_processors >= NR_CPUS) {
  914. printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
  915. " Processor ignored.\n", NR_CPUS);
  916. return;
  917. }
  918. if (num_processors >= maxcpus) {
  919. printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
  920. " Processor ignored.\n", maxcpus);
  921. return;
  922. }
  923. num_processors++;
  924. cpus_complement(tmp_map, cpu_present_map);
  925. cpu = first_cpu(tmp_map);
  926. physid_set(apicid, phys_cpu_present_map);
  927. if (apicid == boot_cpu_physical_apicid) {
  928. /*
  929. * x86_bios_cpu_apicid is required to have processors listed
  930. * in same order as logical cpu numbers. Hence the first
  931. * entry is BSP, and so on.
  932. */
  933. cpu = 0;
  934. }
  935. if (apicid > max_physical_apicid)
  936. max_physical_apicid = apicid;
  937. /* are we being called early in kernel startup? */
  938. if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
  939. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  940. u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  941. cpu_to_apicid[cpu] = apicid;
  942. bios_cpu_apicid[cpu] = apicid;
  943. } else {
  944. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  945. per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  946. }
  947. cpu_set(cpu, cpu_possible_map);
  948. cpu_set(cpu, cpu_present_map);
  949. }
  950. /*
  951. * Power management
  952. */
  953. #ifdef CONFIG_PM
  954. static struct {
  955. /* 'active' is true if the local APIC was enabled by us and
  956. not the BIOS; this signifies that we are also responsible
  957. for disabling it before entering apm/acpi suspend */
  958. int active;
  959. /* r/w apic fields */
  960. unsigned int apic_id;
  961. unsigned int apic_taskpri;
  962. unsigned int apic_ldr;
  963. unsigned int apic_dfr;
  964. unsigned int apic_spiv;
  965. unsigned int apic_lvtt;
  966. unsigned int apic_lvtpc;
  967. unsigned int apic_lvt0;
  968. unsigned int apic_lvt1;
  969. unsigned int apic_lvterr;
  970. unsigned int apic_tmict;
  971. unsigned int apic_tdcr;
  972. unsigned int apic_thmr;
  973. } apic_pm_state;
  974. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  975. {
  976. unsigned long flags;
  977. int maxlvt;
  978. if (!apic_pm_state.active)
  979. return 0;
  980. maxlvt = lapic_get_maxlvt();
  981. apic_pm_state.apic_id = read_apic_id();
  982. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  983. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  984. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  985. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  986. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  987. if (maxlvt >= 4)
  988. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  989. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  990. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  991. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  992. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  993. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  994. #ifdef CONFIG_X86_MCE_INTEL
  995. if (maxlvt >= 5)
  996. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  997. #endif
  998. local_irq_save(flags);
  999. disable_local_APIC();
  1000. local_irq_restore(flags);
  1001. return 0;
  1002. }
  1003. static int lapic_resume(struct sys_device *dev)
  1004. {
  1005. unsigned int l, h;
  1006. unsigned long flags;
  1007. int maxlvt;
  1008. if (!apic_pm_state.active)
  1009. return 0;
  1010. maxlvt = lapic_get_maxlvt();
  1011. local_irq_save(flags);
  1012. rdmsr(MSR_IA32_APICBASE, l, h);
  1013. l &= ~MSR_IA32_APICBASE_BASE;
  1014. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1015. wrmsr(MSR_IA32_APICBASE, l, h);
  1016. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1017. apic_write(APIC_ID, apic_pm_state.apic_id);
  1018. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1019. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1020. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1021. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1022. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1023. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1024. #ifdef CONFIG_X86_MCE_INTEL
  1025. if (maxlvt >= 5)
  1026. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1027. #endif
  1028. if (maxlvt >= 4)
  1029. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1030. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1031. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1032. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1033. apic_write(APIC_ESR, 0);
  1034. apic_read(APIC_ESR);
  1035. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1036. apic_write(APIC_ESR, 0);
  1037. apic_read(APIC_ESR);
  1038. local_irq_restore(flags);
  1039. return 0;
  1040. }
  1041. static struct sysdev_class lapic_sysclass = {
  1042. .name = "lapic",
  1043. .resume = lapic_resume,
  1044. .suspend = lapic_suspend,
  1045. };
  1046. static struct sys_device device_lapic = {
  1047. .id = 0,
  1048. .cls = &lapic_sysclass,
  1049. };
  1050. static void __cpuinit apic_pm_activate(void)
  1051. {
  1052. apic_pm_state.active = 1;
  1053. }
  1054. static int __init init_lapic_sysfs(void)
  1055. {
  1056. int error;
  1057. if (!cpu_has_apic)
  1058. return 0;
  1059. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1060. error = sysdev_class_register(&lapic_sysclass);
  1061. if (!error)
  1062. error = sysdev_register(&device_lapic);
  1063. return error;
  1064. }
  1065. device_initcall(init_lapic_sysfs);
  1066. #else /* CONFIG_PM */
  1067. static void apic_pm_activate(void) { }
  1068. #endif /* CONFIG_PM */
  1069. /*
  1070. * apic_is_clustered_box() -- Check if we can expect good TSC
  1071. *
  1072. * Thus far, the major user of this is IBM's Summit2 series:
  1073. *
  1074. * Clustered boxes may have unsynced TSC problems if they are
  1075. * multi-chassis. Use available data to take a good guess.
  1076. * If in doubt, go HPET.
  1077. */
  1078. __cpuinit int apic_is_clustered_box(void)
  1079. {
  1080. int i, clusters, zeros;
  1081. unsigned id;
  1082. u16 *bios_cpu_apicid;
  1083. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1084. /*
  1085. * there is not this kind of box with AMD CPU yet.
  1086. * Some AMD box with quadcore cpu and 8 sockets apicid
  1087. * will be [4, 0x23] or [8, 0x27] could be thought to
  1088. * vsmp box still need checking...
  1089. */
  1090. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1091. return 0;
  1092. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1093. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1094. for (i = 0; i < NR_CPUS; i++) {
  1095. /* are we being called early in kernel startup? */
  1096. if (bios_cpu_apicid) {
  1097. id = bios_cpu_apicid[i];
  1098. }
  1099. else if (i < nr_cpu_ids) {
  1100. if (cpu_present(i))
  1101. id = per_cpu(x86_bios_cpu_apicid, i);
  1102. else
  1103. continue;
  1104. }
  1105. else
  1106. break;
  1107. if (id != BAD_APICID)
  1108. __set_bit(APIC_CLUSTERID(id), clustermap);
  1109. }
  1110. /* Problem: Partially populated chassis may not have CPUs in some of
  1111. * the APIC clusters they have been allocated. Only present CPUs have
  1112. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1113. * Since clusters are allocated sequentially, count zeros only if
  1114. * they are bounded by ones.
  1115. */
  1116. clusters = 0;
  1117. zeros = 0;
  1118. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1119. if (test_bit(i, clustermap)) {
  1120. clusters += 1 + zeros;
  1121. zeros = 0;
  1122. } else
  1123. ++zeros;
  1124. }
  1125. /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1126. * not guaranteed to be synced between boards
  1127. */
  1128. if (is_vsmp_box() && clusters > 1)
  1129. return 1;
  1130. /*
  1131. * If clusters > 2, then should be multi-chassis.
  1132. * May have to revisit this when multi-core + hyperthreaded CPUs come
  1133. * out, but AFAIK this will work even for them.
  1134. */
  1135. return (clusters > 2);
  1136. }
  1137. /*
  1138. * APIC command line parameters
  1139. */
  1140. static int __init apic_set_verbosity(char *str)
  1141. {
  1142. if (str == NULL) {
  1143. skip_ioapic_setup = 0;
  1144. ioapic_force = 1;
  1145. return 0;
  1146. }
  1147. if (strcmp("debug", str) == 0)
  1148. apic_verbosity = APIC_DEBUG;
  1149. else if (strcmp("verbose", str) == 0)
  1150. apic_verbosity = APIC_VERBOSE;
  1151. else {
  1152. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  1153. " use apic=verbose or apic=debug\n", str);
  1154. return -EINVAL;
  1155. }
  1156. return 0;
  1157. }
  1158. early_param("apic", apic_set_verbosity);
  1159. static __init int setup_disableapic(char *str)
  1160. {
  1161. disable_apic = 1;
  1162. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1163. return 0;
  1164. }
  1165. early_param("disableapic", setup_disableapic);
  1166. /* same as disableapic, for compatibility */
  1167. static __init int setup_nolapic(char *str)
  1168. {
  1169. return setup_disableapic(str);
  1170. }
  1171. early_param("nolapic", setup_nolapic);
  1172. static int __init parse_lapic_timer_c2_ok(char *arg)
  1173. {
  1174. local_apic_timer_c2_ok = 1;
  1175. return 0;
  1176. }
  1177. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1178. static __init int setup_noapictimer(char *str)
  1179. {
  1180. if (str[0] != ' ' && str[0] != 0)
  1181. return 0;
  1182. disable_apic_timer = 1;
  1183. return 1;
  1184. }
  1185. __setup("noapictimer", setup_noapictimer);
  1186. static __init int setup_apicpmtimer(char *s)
  1187. {
  1188. apic_calibrate_pmtmr = 1;
  1189. notsc_setup(NULL);
  1190. return 0;
  1191. }
  1192. __setup("apicpmtimer", setup_apicpmtimer);
  1193. static int __init lapic_insert_resource(void)
  1194. {
  1195. if (!apic_phys)
  1196. return -1;
  1197. /* Put local APIC into the resource map. */
  1198. lapic_resource.start = apic_phys;
  1199. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  1200. insert_resource(&iomem_resource, &lapic_resource);
  1201. return 0;
  1202. }
  1203. /*
  1204. * need call insert after e820_reserve_resources()
  1205. * that is using request_resource
  1206. */
  1207. late_initcall(lapic_insert_resource);