apic_64.c 33 KB

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