apic_64.c 37 KB

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