apic_64.c 33 KB

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