apic_64.c 33 KB

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