apic_64.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. DEFINE_PER_CPU(u16, x86_bios_cpu_apicid) = BAD_APICID;
  77. EXPORT_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  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, 32);
  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. /*
  312. * Setup the boot APIC
  313. *
  314. * Calibrate and verify the result.
  315. */
  316. void __init setup_boot_APIC_clock(void)
  317. {
  318. /*
  319. * The local apic timer can be disabled via the kernel commandline.
  320. * Register the lapic timer as a dummy clock event source on SMP
  321. * systems, so the broadcast mechanism is used. On UP systems simply
  322. * ignore it.
  323. */
  324. if (disable_apic_timer) {
  325. printk(KERN_INFO "Disabling APIC timer\n");
  326. /* No broadcast on UP ! */
  327. if (num_possible_cpus() > 1) {
  328. lapic_clockevent.mult = 1;
  329. setup_APIC_timer();
  330. }
  331. return;
  332. }
  333. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  334. calibrate_APIC_clock();
  335. /*
  336. * Do a sanity check on the APIC calibration result
  337. */
  338. if (calibration_result < (1000000 / HZ)) {
  339. printk(KERN_WARNING
  340. "APIC frequency too slow, disabling apic timer\n");
  341. /* No broadcast on UP ! */
  342. if (num_possible_cpus() > 1)
  343. setup_APIC_timer();
  344. return;
  345. }
  346. /*
  347. * If nmi_watchdog is set to IO_APIC, we need the
  348. * PIT/HPET going. Otherwise register lapic as a dummy
  349. * device.
  350. */
  351. if (nmi_watchdog != NMI_IO_APIC)
  352. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  353. else
  354. printk(KERN_WARNING "APIC timer registered as dummy,"
  355. " due to nmi_watchdog=1!\n");
  356. setup_APIC_timer();
  357. }
  358. /*
  359. * AMD C1E enabled CPUs have a real nasty problem: Some BIOSes set the
  360. * C1E flag only in the secondary CPU, so when we detect the wreckage
  361. * we already have enabled the boot CPU local apic timer. Check, if
  362. * disable_apic_timer is set and the DUMMY flag is cleared. If yes,
  363. * set the DUMMY flag again and force the broadcast mode in the
  364. * clockevents layer.
  365. */
  366. static void __cpuinit check_boot_apic_timer_broadcast(void)
  367. {
  368. if (!disable_apic_timer ||
  369. (lapic_clockevent.features & CLOCK_EVT_FEAT_DUMMY))
  370. return;
  371. printk(KERN_INFO "AMD C1E detected late. Force timer broadcast.\n");
  372. lapic_clockevent.features |= CLOCK_EVT_FEAT_DUMMY;
  373. local_irq_enable();
  374. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  375. &boot_cpu_physical_apicid);
  376. local_irq_disable();
  377. }
  378. void __cpuinit setup_secondary_APIC_clock(void)
  379. {
  380. check_boot_apic_timer_broadcast();
  381. setup_APIC_timer();
  382. }
  383. /*
  384. * The guts of the apic timer interrupt
  385. */
  386. static void local_apic_timer_interrupt(void)
  387. {
  388. int cpu = smp_processor_id();
  389. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  390. /*
  391. * Normally we should not be here till LAPIC has been initialized but
  392. * in some cases like kdump, its possible that there is a pending LAPIC
  393. * timer interrupt from previous kernel's context and is delivered in
  394. * new kernel the moment interrupts are enabled.
  395. *
  396. * Interrupts are enabled early and LAPIC is setup much later, hence
  397. * its possible that when we get here evt->event_handler is NULL.
  398. * Check for event_handler being NULL and discard the interrupt as
  399. * spurious.
  400. */
  401. if (!evt->event_handler) {
  402. printk(KERN_WARNING
  403. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  404. /* Switch it off */
  405. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  406. return;
  407. }
  408. /*
  409. * the NMI deadlock-detector uses this.
  410. */
  411. add_pda(apic_timer_irqs, 1);
  412. evt->event_handler(evt);
  413. }
  414. /*
  415. * Local APIC timer interrupt. This is the most natural way for doing
  416. * local interrupts, but local timer interrupts can be emulated by
  417. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  418. *
  419. * [ if a single-CPU system runs an SMP kernel then we call the local
  420. * interrupt as well. Thus we cannot inline the local irq ... ]
  421. */
  422. void smp_apic_timer_interrupt(struct pt_regs *regs)
  423. {
  424. struct pt_regs *old_regs = set_irq_regs(regs);
  425. /*
  426. * NOTE! We'd better ACK the irq immediately,
  427. * because timer handling can be slow.
  428. */
  429. ack_APIC_irq();
  430. /*
  431. * update_process_times() expects us to have done irq_enter().
  432. * Besides, if we don't timer interrupts ignore the global
  433. * interrupt lock, which is the WrongThing (tm) to do.
  434. */
  435. exit_idle();
  436. irq_enter();
  437. local_apic_timer_interrupt();
  438. irq_exit();
  439. set_irq_regs(old_regs);
  440. }
  441. int setup_profiling_timer(unsigned int multiplier)
  442. {
  443. return -EINVAL;
  444. }
  445. /*
  446. * Local APIC start and shutdown
  447. */
  448. /**
  449. * clear_local_APIC - shutdown the local APIC
  450. *
  451. * This is called, when a CPU is disabled and before rebooting, so the state of
  452. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  453. * leftovers during boot.
  454. */
  455. void clear_local_APIC(void)
  456. {
  457. int maxlvt = lapic_get_maxlvt();
  458. u32 v;
  459. /* APIC hasn't been mapped yet */
  460. if (!apic_phys)
  461. return;
  462. maxlvt = lapic_get_maxlvt();
  463. /*
  464. * Masking an LVT entry can trigger a local APIC error
  465. * if the vector is zero. Mask LVTERR first to prevent this.
  466. */
  467. if (maxlvt >= 3) {
  468. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  469. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  470. }
  471. /*
  472. * Careful: we have to set masks only first to deassert
  473. * any level-triggered sources.
  474. */
  475. v = apic_read(APIC_LVTT);
  476. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  477. v = apic_read(APIC_LVT0);
  478. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  479. v = apic_read(APIC_LVT1);
  480. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  481. if (maxlvt >= 4) {
  482. v = apic_read(APIC_LVTPC);
  483. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  484. }
  485. /*
  486. * Clean APIC state for other OSs:
  487. */
  488. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  489. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  490. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  491. if (maxlvt >= 3)
  492. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  493. if (maxlvt >= 4)
  494. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  495. apic_write(APIC_ESR, 0);
  496. apic_read(APIC_ESR);
  497. }
  498. /**
  499. * disable_local_APIC - clear and disable the local APIC
  500. */
  501. void disable_local_APIC(void)
  502. {
  503. unsigned int value;
  504. clear_local_APIC();
  505. /*
  506. * Disable APIC (implies clearing of registers
  507. * for 82489DX!).
  508. */
  509. value = apic_read(APIC_SPIV);
  510. value &= ~APIC_SPIV_APIC_ENABLED;
  511. apic_write(APIC_SPIV, value);
  512. }
  513. void lapic_shutdown(void)
  514. {
  515. unsigned long flags;
  516. if (!cpu_has_apic)
  517. return;
  518. local_irq_save(flags);
  519. disable_local_APIC();
  520. local_irq_restore(flags);
  521. }
  522. /*
  523. * This is to verify that we're looking at a real local APIC.
  524. * Check these against your board if the CPUs aren't getting
  525. * started for no apparent reason.
  526. */
  527. int __init verify_local_APIC(void)
  528. {
  529. unsigned int reg0, reg1;
  530. /*
  531. * The version register is read-only in a real APIC.
  532. */
  533. reg0 = apic_read(APIC_LVR);
  534. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  535. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  536. reg1 = apic_read(APIC_LVR);
  537. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  538. /*
  539. * The two version reads above should print the same
  540. * numbers. If the second one is different, then we
  541. * poke at a non-APIC.
  542. */
  543. if (reg1 != reg0)
  544. return 0;
  545. /*
  546. * Check if the version looks reasonably.
  547. */
  548. reg1 = GET_APIC_VERSION(reg0);
  549. if (reg1 == 0x00 || reg1 == 0xff)
  550. return 0;
  551. reg1 = lapic_get_maxlvt();
  552. if (reg1 < 0x02 || reg1 == 0xff)
  553. return 0;
  554. /*
  555. * The ID register is read/write in a real APIC.
  556. */
  557. reg0 = read_apic_id();
  558. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  559. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  560. reg1 = read_apic_id();
  561. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  562. apic_write(APIC_ID, reg0);
  563. if (reg1 != (reg0 ^ APIC_ID_MASK))
  564. return 0;
  565. /*
  566. * The next two are just to see if we have sane values.
  567. * They're only really relevant if we're in Virtual Wire
  568. * compatibility mode, but most boxes are anymore.
  569. */
  570. reg0 = apic_read(APIC_LVT0);
  571. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  572. reg1 = apic_read(APIC_LVT1);
  573. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  574. return 1;
  575. }
  576. /**
  577. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  578. */
  579. void __init sync_Arb_IDs(void)
  580. {
  581. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  582. if (modern_apic())
  583. return;
  584. /*
  585. * Wait for idle.
  586. */
  587. apic_wait_icr_idle();
  588. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  589. apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  590. | APIC_DM_INIT);
  591. }
  592. /*
  593. * An initial setup of the virtual wire mode.
  594. */
  595. void __init init_bsp_APIC(void)
  596. {
  597. unsigned int value;
  598. /*
  599. * Don't do the setup now if we have a SMP BIOS as the
  600. * through-I/O-APIC virtual wire mode might be active.
  601. */
  602. if (smp_found_config || !cpu_has_apic)
  603. return;
  604. value = apic_read(APIC_LVR);
  605. /*
  606. * Do not trust the local APIC being empty at bootup.
  607. */
  608. clear_local_APIC();
  609. /*
  610. * Enable APIC.
  611. */
  612. value = apic_read(APIC_SPIV);
  613. value &= ~APIC_VECTOR_MASK;
  614. value |= APIC_SPIV_APIC_ENABLED;
  615. value |= APIC_SPIV_FOCUS_DISABLED;
  616. value |= SPURIOUS_APIC_VECTOR;
  617. apic_write(APIC_SPIV, value);
  618. /*
  619. * Set up the virtual wire mode.
  620. */
  621. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  622. value = APIC_DM_NMI;
  623. apic_write(APIC_LVT1, value);
  624. }
  625. /**
  626. * setup_local_APIC - setup the local APIC
  627. */
  628. void __cpuinit setup_local_APIC(void)
  629. {
  630. unsigned int value;
  631. int i, j;
  632. preempt_disable();
  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. preempt_enable();
  717. }
  718. static void __cpuinit lapic_setup_esr(void)
  719. {
  720. unsigned maxlvt = lapic_get_maxlvt();
  721. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
  722. /*
  723. * spec says clear errors after enabling vector.
  724. */
  725. if (maxlvt > 3)
  726. apic_write(APIC_ESR, 0);
  727. }
  728. void __cpuinit end_local_APIC_setup(void)
  729. {
  730. lapic_setup_esr();
  731. nmi_watchdog_default();
  732. setup_apic_nmi_watchdog(NULL);
  733. apic_pm_activate();
  734. }
  735. /*
  736. * Detect and enable local APICs on non-SMP boards.
  737. * Original code written by Keir Fraser.
  738. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  739. * not correctly set up (usually the APIC timer won't work etc.)
  740. */
  741. static int __init detect_init_APIC(void)
  742. {
  743. if (!cpu_has_apic) {
  744. printk(KERN_INFO "No local APIC present\n");
  745. return -1;
  746. }
  747. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  748. boot_cpu_physical_apicid = 0;
  749. return 0;
  750. }
  751. void __init early_init_lapic_mapping(void)
  752. {
  753. unsigned long apic_phys;
  754. /*
  755. * If no local APIC can be found then go out
  756. * : it means there is no mpatable and MADT
  757. */
  758. if (!smp_found_config)
  759. return;
  760. apic_phys = mp_lapic_addr;
  761. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  762. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  763. APIC_BASE, apic_phys);
  764. /*
  765. * Fetch the APIC ID of the BSP in case we have a
  766. * default configuration (or the MP table is broken).
  767. */
  768. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  769. }
  770. /**
  771. * init_apic_mappings - initialize APIC mappings
  772. */
  773. void __init init_apic_mappings(void)
  774. {
  775. /*
  776. * If no local APIC can be found then set up a fake all
  777. * zeroes page to simulate the local APIC and another
  778. * one for the IO-APIC.
  779. */
  780. if (!smp_found_config && detect_init_APIC()) {
  781. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  782. apic_phys = __pa(apic_phys);
  783. } else
  784. apic_phys = mp_lapic_addr;
  785. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  786. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  787. APIC_BASE, apic_phys);
  788. /*
  789. * Fetch the APIC ID of the BSP in case we have a
  790. * default configuration (or the MP table is broken).
  791. */
  792. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  793. }
  794. /*
  795. * This initializes the IO-APIC and APIC hardware if this is
  796. * a UP kernel.
  797. */
  798. int __init APIC_init_uniprocessor(void)
  799. {
  800. if (disable_apic) {
  801. printk(KERN_INFO "Apic disabled\n");
  802. return -1;
  803. }
  804. if (!cpu_has_apic) {
  805. disable_apic = 1;
  806. printk(KERN_INFO "Apic disabled by BIOS\n");
  807. return -1;
  808. }
  809. verify_local_APIC();
  810. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  811. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  812. setup_local_APIC();
  813. /*
  814. * Now enable IO-APICs, actually call clear_IO_APIC
  815. * We need clear_IO_APIC before enabling vector on BP
  816. */
  817. if (!skip_ioapic_setup && nr_ioapics)
  818. enable_IO_APIC();
  819. end_local_APIC_setup();
  820. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  821. setup_IO_APIC();
  822. else
  823. nr_ioapics = 0;
  824. setup_boot_APIC_clock();
  825. check_nmi_watchdog();
  826. return 0;
  827. }
  828. /*
  829. * Local APIC interrupts
  830. */
  831. /*
  832. * This interrupt should _never_ happen with our APIC/SMP architecture
  833. */
  834. asmlinkage void smp_spurious_interrupt(void)
  835. {
  836. unsigned int v;
  837. exit_idle();
  838. irq_enter();
  839. /*
  840. * Check if this really is a spurious interrupt and ACK it
  841. * if it is a vectored one. Just in case...
  842. * Spurious interrupts should not be ACKed.
  843. */
  844. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  845. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  846. ack_APIC_irq();
  847. add_pda(irq_spurious_count, 1);
  848. irq_exit();
  849. }
  850. /*
  851. * This interrupt should never happen with our APIC/SMP architecture
  852. */
  853. asmlinkage void smp_error_interrupt(void)
  854. {
  855. unsigned int v, v1;
  856. exit_idle();
  857. irq_enter();
  858. /* First tickle the hardware, only then report what went on. -- REW */
  859. v = apic_read(APIC_ESR);
  860. apic_write(APIC_ESR, 0);
  861. v1 = apic_read(APIC_ESR);
  862. ack_APIC_irq();
  863. atomic_inc(&irq_err_count);
  864. /* Here is what the APIC error bits mean:
  865. 0: Send CS error
  866. 1: Receive CS error
  867. 2: Send accept error
  868. 3: Receive accept error
  869. 4: Reserved
  870. 5: Send illegal vector
  871. 6: Received illegal vector
  872. 7: Illegal register address
  873. */
  874. printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  875. smp_processor_id(), v , v1);
  876. irq_exit();
  877. }
  878. void disconnect_bsp_APIC(int virt_wire_setup)
  879. {
  880. /* Go back to Virtual Wire compatibility mode */
  881. unsigned long value;
  882. /* For the spurious interrupt use vector F, and enable it */
  883. value = apic_read(APIC_SPIV);
  884. value &= ~APIC_VECTOR_MASK;
  885. value |= APIC_SPIV_APIC_ENABLED;
  886. value |= 0xf;
  887. apic_write(APIC_SPIV, value);
  888. if (!virt_wire_setup) {
  889. /*
  890. * For LVT0 make it edge triggered, active high,
  891. * external and enabled
  892. */
  893. value = apic_read(APIC_LVT0);
  894. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  895. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  896. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  897. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  898. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  899. apic_write(APIC_LVT0, value);
  900. } else {
  901. /* Disable LVT0 */
  902. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  903. }
  904. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  905. value = apic_read(APIC_LVT1);
  906. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  907. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  908. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  909. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  910. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  911. apic_write(APIC_LVT1, value);
  912. }
  913. void __cpuinit generic_processor_info(int apicid, int version)
  914. {
  915. int cpu;
  916. cpumask_t tmp_map;
  917. if (num_processors >= NR_CPUS) {
  918. printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
  919. " Processor ignored.\n", NR_CPUS);
  920. return;
  921. }
  922. if (num_processors >= maxcpus) {
  923. printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
  924. " Processor ignored.\n", maxcpus);
  925. return;
  926. }
  927. num_processors++;
  928. cpus_complement(tmp_map, cpu_present_map);
  929. cpu = first_cpu(tmp_map);
  930. physid_set(apicid, phys_cpu_present_map);
  931. if (apicid == boot_cpu_physical_apicid) {
  932. /*
  933. * x86_bios_cpu_apicid is required to have processors listed
  934. * in same order as logical cpu numbers. Hence the first
  935. * entry is BSP, and so on.
  936. */
  937. cpu = 0;
  938. }
  939. /* are we being called early in kernel startup? */
  940. if (x86_cpu_to_apicid_early_ptr) {
  941. u16 *cpu_to_apicid = x86_cpu_to_apicid_early_ptr;
  942. u16 *bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
  943. cpu_to_apicid[cpu] = apicid;
  944. bios_cpu_apicid[cpu] = apicid;
  945. } else {
  946. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  947. per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  948. }
  949. cpu_set(cpu, cpu_possible_map);
  950. cpu_set(cpu, cpu_present_map);
  951. }
  952. /*
  953. * Power management
  954. */
  955. #ifdef CONFIG_PM
  956. static struct {
  957. /* 'active' is true if the local APIC was enabled by us and
  958. not the BIOS; this signifies that we are also responsible
  959. for disabling it before entering apm/acpi suspend */
  960. int active;
  961. /* r/w apic fields */
  962. unsigned int apic_id;
  963. unsigned int apic_taskpri;
  964. unsigned int apic_ldr;
  965. unsigned int apic_dfr;
  966. unsigned int apic_spiv;
  967. unsigned int apic_lvtt;
  968. unsigned int apic_lvtpc;
  969. unsigned int apic_lvt0;
  970. unsigned int apic_lvt1;
  971. unsigned int apic_lvterr;
  972. unsigned int apic_tmict;
  973. unsigned int apic_tdcr;
  974. unsigned int apic_thmr;
  975. } apic_pm_state;
  976. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  977. {
  978. unsigned long flags;
  979. int maxlvt;
  980. if (!apic_pm_state.active)
  981. return 0;
  982. maxlvt = lapic_get_maxlvt();
  983. apic_pm_state.apic_id = read_apic_id();
  984. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  985. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  986. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  987. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  988. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  989. if (maxlvt >= 4)
  990. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  991. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  992. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  993. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  994. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  995. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  996. #ifdef CONFIG_X86_MCE_INTEL
  997. if (maxlvt >= 5)
  998. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  999. #endif
  1000. local_irq_save(flags);
  1001. disable_local_APIC();
  1002. local_irq_restore(flags);
  1003. return 0;
  1004. }
  1005. static int lapic_resume(struct sys_device *dev)
  1006. {
  1007. unsigned int l, h;
  1008. unsigned long flags;
  1009. int maxlvt;
  1010. if (!apic_pm_state.active)
  1011. return 0;
  1012. maxlvt = lapic_get_maxlvt();
  1013. local_irq_save(flags);
  1014. rdmsr(MSR_IA32_APICBASE, l, h);
  1015. l &= ~MSR_IA32_APICBASE_BASE;
  1016. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1017. wrmsr(MSR_IA32_APICBASE, l, h);
  1018. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1019. apic_write(APIC_ID, apic_pm_state.apic_id);
  1020. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1021. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1022. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1023. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1024. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1025. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1026. #ifdef CONFIG_X86_MCE_INTEL
  1027. if (maxlvt >= 5)
  1028. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1029. #endif
  1030. if (maxlvt >= 4)
  1031. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1032. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1033. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1034. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1035. apic_write(APIC_ESR, 0);
  1036. apic_read(APIC_ESR);
  1037. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1038. apic_write(APIC_ESR, 0);
  1039. apic_read(APIC_ESR);
  1040. local_irq_restore(flags);
  1041. return 0;
  1042. }
  1043. static struct sysdev_class lapic_sysclass = {
  1044. .name = "lapic",
  1045. .resume = lapic_resume,
  1046. .suspend = lapic_suspend,
  1047. };
  1048. static struct sys_device device_lapic = {
  1049. .id = 0,
  1050. .cls = &lapic_sysclass,
  1051. };
  1052. static void __cpuinit apic_pm_activate(void)
  1053. {
  1054. apic_pm_state.active = 1;
  1055. }
  1056. static int __init init_lapic_sysfs(void)
  1057. {
  1058. int error;
  1059. if (!cpu_has_apic)
  1060. return 0;
  1061. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1062. error = sysdev_class_register(&lapic_sysclass);
  1063. if (!error)
  1064. error = sysdev_register(&device_lapic);
  1065. return error;
  1066. }
  1067. device_initcall(init_lapic_sysfs);
  1068. #else /* CONFIG_PM */
  1069. static void apic_pm_activate(void) { }
  1070. #endif /* CONFIG_PM */
  1071. /*
  1072. * apic_is_clustered_box() -- Check if we can expect good TSC
  1073. *
  1074. * Thus far, the major user of this is IBM's Summit2 series:
  1075. *
  1076. * Clustered boxes may have unsynced TSC problems if they are
  1077. * multi-chassis. Use available data to take a good guess.
  1078. * If in doubt, go HPET.
  1079. */
  1080. __cpuinit int apic_is_clustered_box(void)
  1081. {
  1082. int i, clusters, zeros;
  1083. unsigned id;
  1084. u16 *bios_cpu_apicid;
  1085. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1086. /*
  1087. * there is not this kind of box with AMD CPU yet.
  1088. * Some AMD box with quadcore cpu and 8 sockets apicid
  1089. * will be [4, 0x23] or [8, 0x27] could be thought to
  1090. * vsmp box still need checking...
  1091. */
  1092. if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
  1093. return 0;
  1094. bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
  1095. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1096. for (i = 0; i < NR_CPUS; i++) {
  1097. /* are we being called early in kernel startup? */
  1098. if (bios_cpu_apicid) {
  1099. id = bios_cpu_apicid[i];
  1100. }
  1101. else if (i < nr_cpu_ids) {
  1102. if (cpu_present(i))
  1103. id = per_cpu(x86_bios_cpu_apicid, i);
  1104. else
  1105. continue;
  1106. }
  1107. else
  1108. break;
  1109. if (id != BAD_APICID)
  1110. __set_bit(APIC_CLUSTERID(id), clustermap);
  1111. }
  1112. /* Problem: Partially populated chassis may not have CPUs in some of
  1113. * the APIC clusters they have been allocated. Only present CPUs have
  1114. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1115. * Since clusters are allocated sequentially, count zeros only if
  1116. * they are bounded by ones.
  1117. */
  1118. clusters = 0;
  1119. zeros = 0;
  1120. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1121. if (test_bit(i, clustermap)) {
  1122. clusters += 1 + zeros;
  1123. zeros = 0;
  1124. } else
  1125. ++zeros;
  1126. }
  1127. /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1128. * not guaranteed to be synced between boards
  1129. */
  1130. if (is_vsmp_box() && clusters > 1)
  1131. return 1;
  1132. /*
  1133. * If clusters > 2, then should be multi-chassis.
  1134. * May have to revisit this when multi-core + hyperthreaded CPUs come
  1135. * out, but AFAIK this will work even for them.
  1136. */
  1137. return (clusters > 2);
  1138. }
  1139. /*
  1140. * APIC command line parameters
  1141. */
  1142. static int __init apic_set_verbosity(char *str)
  1143. {
  1144. if (str == NULL) {
  1145. skip_ioapic_setup = 0;
  1146. ioapic_force = 1;
  1147. return 0;
  1148. }
  1149. if (strcmp("debug", str) == 0)
  1150. apic_verbosity = APIC_DEBUG;
  1151. else if (strcmp("verbose", str) == 0)
  1152. apic_verbosity = APIC_VERBOSE;
  1153. else {
  1154. printk(KERN_WARNING "APIC Verbosity level %s not recognised"
  1155. " use apic=verbose or apic=debug\n", str);
  1156. return -EINVAL;
  1157. }
  1158. return 0;
  1159. }
  1160. early_param("apic", apic_set_verbosity);
  1161. static __init int setup_disableapic(char *str)
  1162. {
  1163. disable_apic = 1;
  1164. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1165. return 0;
  1166. }
  1167. early_param("disableapic", setup_disableapic);
  1168. /* same as disableapic, for compatibility */
  1169. static __init int setup_nolapic(char *str)
  1170. {
  1171. return setup_disableapic(str);
  1172. }
  1173. early_param("nolapic", setup_nolapic);
  1174. static int __init parse_lapic_timer_c2_ok(char *arg)
  1175. {
  1176. local_apic_timer_c2_ok = 1;
  1177. return 0;
  1178. }
  1179. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1180. static __init int setup_noapictimer(char *str)
  1181. {
  1182. if (str[0] != ' ' && str[0] != 0)
  1183. return 0;
  1184. disable_apic_timer = 1;
  1185. return 1;
  1186. }
  1187. __setup("noapictimer", setup_noapictimer);
  1188. static __init int setup_apicpmtimer(char *s)
  1189. {
  1190. apic_calibrate_pmtmr = 1;
  1191. notsc_setup(NULL);
  1192. return 0;
  1193. }
  1194. __setup("apicpmtimer", setup_apicpmtimer);
  1195. static int __init lapic_insert_resource(void)
  1196. {
  1197. if (!apic_phys)
  1198. return -1;
  1199. /* Put local APIC into the resource map. */
  1200. lapic_resource.start = apic_phys;
  1201. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  1202. insert_resource(&iomem_resource, &lapic_resource);
  1203. return 0;
  1204. }
  1205. /*
  1206. * need call insert after e820_reserve_resources()
  1207. * that is using request_resource
  1208. */
  1209. late_initcall(lapic_insert_resource);