apic_64.c 34 KB

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