apic_64.c 34 KB

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