apic_64.c 37 KB

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