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