apic_64.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. 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. .icr_read = xapic_icr_read,
  144. .icr_write = xapic_icr_write,
  145. .wait_icr_idle = xapic_wait_icr_idle,
  146. .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
  147. };
  148. struct apic_ops __read_mostly *apic_ops = &xapic_ops;
  149. EXPORT_SYMBOL_GPL(apic_ops);
  150. static void x2apic_wait_icr_idle(void)
  151. {
  152. /* no need to wait for icr idle in x2apic */
  153. return;
  154. }
  155. static u32 safe_x2apic_wait_icr_idle(void)
  156. {
  157. /* no need to wait for icr idle in x2apic */
  158. return 0;
  159. }
  160. void x2apic_icr_write(u32 low, u32 id)
  161. {
  162. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  163. }
  164. u64 x2apic_icr_read(void)
  165. {
  166. unsigned long val;
  167. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  168. return val;
  169. }
  170. static struct apic_ops x2apic_ops = {
  171. .read = native_apic_msr_read,
  172. .write = native_apic_msr_write,
  173. .icr_read = x2apic_icr_read,
  174. .icr_write = x2apic_icr_write,
  175. .wait_icr_idle = x2apic_wait_icr_idle,
  176. .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
  177. };
  178. /**
  179. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  180. */
  181. void __cpuinit enable_NMI_through_LVT0(void)
  182. {
  183. unsigned int v;
  184. /* unmask and set to NMI */
  185. v = APIC_DM_NMI;
  186. apic_write(APIC_LVT0, v);
  187. }
  188. /**
  189. * lapic_get_maxlvt - get the maximum number of local vector table entries
  190. */
  191. int lapic_get_maxlvt(void)
  192. {
  193. unsigned int v, maxlvt;
  194. v = apic_read(APIC_LVR);
  195. maxlvt = GET_APIC_MAXLVT(v);
  196. return maxlvt;
  197. }
  198. /*
  199. * This function sets up the local APIC timer, with a timeout of
  200. * 'clocks' APIC bus clock. During calibration we actually call
  201. * this function twice on the boot CPU, once with a bogus timeout
  202. * value, second time for real. The other (noncalibrating) CPUs
  203. * call this function only once, with the real, calibrated value.
  204. *
  205. * We do reads before writes even if unnecessary, to get around the
  206. * P5 APIC double write bug.
  207. */
  208. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  209. {
  210. unsigned int lvtt_value, tmp_value;
  211. lvtt_value = LOCAL_TIMER_VECTOR;
  212. if (!oneshot)
  213. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  214. if (!irqen)
  215. lvtt_value |= APIC_LVT_MASKED;
  216. apic_write(APIC_LVTT, lvtt_value);
  217. /*
  218. * Divide PICLK by 16
  219. */
  220. tmp_value = apic_read(APIC_TDCR);
  221. apic_write(APIC_TDCR, (tmp_value
  222. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  223. | APIC_TDR_DIV_16);
  224. if (!oneshot)
  225. apic_write(APIC_TMICT, clocks);
  226. }
  227. /*
  228. * Setup extended LVT, AMD specific (K8, family 10h)
  229. *
  230. * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
  231. * MCE interrupts are supported. Thus MCE offset must be set to 0.
  232. */
  233. #define APIC_EILVT_LVTOFF_MCE 0
  234. #define APIC_EILVT_LVTOFF_IBS 1
  235. static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
  236. {
  237. unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
  238. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  239. apic_write(reg, v);
  240. }
  241. u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
  242. {
  243. setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
  244. return APIC_EILVT_LVTOFF_MCE;
  245. }
  246. u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
  247. {
  248. setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
  249. return APIC_EILVT_LVTOFF_IBS;
  250. }
  251. /*
  252. * Program the next event, relative to now
  253. */
  254. static int lapic_next_event(unsigned long delta,
  255. struct clock_event_device *evt)
  256. {
  257. apic_write(APIC_TMICT, delta);
  258. return 0;
  259. }
  260. /*
  261. * Setup the lapic timer in periodic or oneshot mode
  262. */
  263. static void lapic_timer_setup(enum clock_event_mode mode,
  264. struct clock_event_device *evt)
  265. {
  266. unsigned long flags;
  267. unsigned int v;
  268. /* Lapic used as dummy for broadcast ? */
  269. if (evt->features & CLOCK_EVT_FEAT_DUMMY)
  270. return;
  271. local_irq_save(flags);
  272. switch (mode) {
  273. case CLOCK_EVT_MODE_PERIODIC:
  274. case CLOCK_EVT_MODE_ONESHOT:
  275. __setup_APIC_LVTT(calibration_result,
  276. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  277. break;
  278. case CLOCK_EVT_MODE_UNUSED:
  279. case CLOCK_EVT_MODE_SHUTDOWN:
  280. v = apic_read(APIC_LVTT);
  281. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  282. apic_write(APIC_LVTT, v);
  283. break;
  284. case CLOCK_EVT_MODE_RESUME:
  285. /* Nothing to do here */
  286. break;
  287. }
  288. local_irq_restore(flags);
  289. }
  290. /*
  291. * Local APIC timer broadcast function
  292. */
  293. static void lapic_timer_broadcast(cpumask_t mask)
  294. {
  295. #ifdef CONFIG_SMP
  296. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  297. #endif
  298. }
  299. /*
  300. * Setup the local APIC timer for this CPU. Copy the initilized values
  301. * of the boot CPU and register the clock event in the framework.
  302. */
  303. static void setup_APIC_timer(void)
  304. {
  305. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  306. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  307. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  308. clockevents_register_device(levt);
  309. }
  310. /*
  311. * In this function we calibrate APIC bus clocks to the external
  312. * timer. Unfortunately we cannot use jiffies and the timer irq
  313. * to calibrate, since some later bootup code depends on getting
  314. * the first irq? Ugh.
  315. *
  316. * We want to do the calibration only once since we
  317. * want to have local timer irqs syncron. CPUs connected
  318. * by the same APIC bus have the very same bus frequency.
  319. * And we want to have irqs off anyways, no accidental
  320. * APIC irq that way.
  321. */
  322. #define TICK_COUNT 100000000
  323. static int __init calibrate_APIC_clock(void)
  324. {
  325. unsigned apic, apic_start;
  326. unsigned long tsc, tsc_start;
  327. int result;
  328. local_irq_disable();
  329. /*
  330. * Put whatever arbitrary (but long enough) timeout
  331. * value into the APIC clock, we just want to get the
  332. * counter running for calibration.
  333. *
  334. * No interrupt enable !
  335. */
  336. __setup_APIC_LVTT(250000000, 0, 0);
  337. apic_start = apic_read(APIC_TMCCT);
  338. #ifdef CONFIG_X86_PM_TIMER
  339. if (apic_calibrate_pmtmr && pmtmr_ioport) {
  340. pmtimer_wait(5000); /* 5ms wait */
  341. apic = apic_read(APIC_TMCCT);
  342. result = (apic_start - apic) * 1000L / 5;
  343. } else
  344. #endif
  345. {
  346. rdtscll(tsc_start);
  347. do {
  348. apic = apic_read(APIC_TMCCT);
  349. rdtscll(tsc);
  350. } while ((tsc - tsc_start) < TICK_COUNT &&
  351. (apic_start - apic) < TICK_COUNT);
  352. result = (apic_start - apic) * 1000L * tsc_khz /
  353. (tsc - tsc_start);
  354. }
  355. local_irq_enable();
  356. printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
  357. printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
  358. result / 1000 / 1000, result / 1000 % 1000);
  359. /* Calculate the scaled math multiplication factor */
  360. lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
  361. lapic_clockevent.shift);
  362. lapic_clockevent.max_delta_ns =
  363. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  364. lapic_clockevent.min_delta_ns =
  365. clockevent_delta2ns(0xF, &lapic_clockevent);
  366. calibration_result = result / HZ;
  367. /*
  368. * Do a sanity check on the APIC calibration result
  369. */
  370. if (calibration_result < (1000000 / HZ)) {
  371. printk(KERN_WARNING
  372. "APIC frequency too slow, disabling apic timer\n");
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. /*
  378. * Setup the boot APIC
  379. *
  380. * Calibrate and verify the result.
  381. */
  382. void __init setup_boot_APIC_clock(void)
  383. {
  384. /*
  385. * The local apic timer can be disabled via the kernel commandline.
  386. * Register the lapic timer as a dummy clock event source on SMP
  387. * systems, so the broadcast mechanism is used. On UP systems simply
  388. * ignore it.
  389. */
  390. if (disable_apic_timer) {
  391. printk(KERN_INFO "Disabling APIC timer\n");
  392. /* No broadcast on UP ! */
  393. if (num_possible_cpus() > 1) {
  394. lapic_clockevent.mult = 1;
  395. setup_APIC_timer();
  396. }
  397. return;
  398. }
  399. printk(KERN_INFO "Using local APIC timer interrupts.\n");
  400. if (calibrate_APIC_clock()) {
  401. /* No broadcast on UP ! */
  402. if (num_possible_cpus() > 1)
  403. setup_APIC_timer();
  404. return;
  405. }
  406. /*
  407. * If nmi_watchdog is set to IO_APIC, we need the
  408. * PIT/HPET going. Otherwise register lapic as a dummy
  409. * device.
  410. */
  411. if (nmi_watchdog != NMI_IO_APIC)
  412. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  413. else
  414. printk(KERN_WARNING "APIC timer registered as dummy,"
  415. " due to nmi_watchdog=%d!\n", nmi_watchdog);
  416. setup_APIC_timer();
  417. }
  418. void __cpuinit setup_secondary_APIC_clock(void)
  419. {
  420. setup_APIC_timer();
  421. }
  422. /*
  423. * The guts of the apic timer interrupt
  424. */
  425. static void local_apic_timer_interrupt(void)
  426. {
  427. int cpu = smp_processor_id();
  428. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  429. /*
  430. * Normally we should not be here till LAPIC has been initialized but
  431. * in some cases like kdump, its possible that there is a pending LAPIC
  432. * timer interrupt from previous kernel's context and is delivered in
  433. * new kernel the moment interrupts are enabled.
  434. *
  435. * Interrupts are enabled early and LAPIC is setup much later, hence
  436. * its possible that when we get here evt->event_handler is NULL.
  437. * Check for event_handler being NULL and discard the interrupt as
  438. * spurious.
  439. */
  440. if (!evt->event_handler) {
  441. printk(KERN_WARNING
  442. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  443. /* Switch it off */
  444. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  445. return;
  446. }
  447. /*
  448. * the NMI deadlock-detector uses this.
  449. */
  450. add_pda(apic_timer_irqs, 1);
  451. evt->event_handler(evt);
  452. }
  453. /*
  454. * Local APIC timer interrupt. This is the most natural way for doing
  455. * local interrupts, but local timer interrupts can be emulated by
  456. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  457. *
  458. * [ if a single-CPU system runs an SMP kernel then we call the local
  459. * interrupt as well. Thus we cannot inline the local irq ... ]
  460. */
  461. void smp_apic_timer_interrupt(struct pt_regs *regs)
  462. {
  463. struct pt_regs *old_regs = set_irq_regs(regs);
  464. /*
  465. * NOTE! We'd better ACK the irq immediately,
  466. * because timer handling can be slow.
  467. */
  468. ack_APIC_irq();
  469. /*
  470. * update_process_times() expects us to have done irq_enter().
  471. * Besides, if we don't timer interrupts ignore the global
  472. * interrupt lock, which is the WrongThing (tm) to do.
  473. */
  474. exit_idle();
  475. irq_enter();
  476. local_apic_timer_interrupt();
  477. irq_exit();
  478. set_irq_regs(old_regs);
  479. }
  480. int setup_profiling_timer(unsigned int multiplier)
  481. {
  482. return -EINVAL;
  483. }
  484. /*
  485. * Local APIC start and shutdown
  486. */
  487. /**
  488. * clear_local_APIC - shutdown the local APIC
  489. *
  490. * This is called, when a CPU is disabled and before rebooting, so the state of
  491. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  492. * leftovers during boot.
  493. */
  494. void clear_local_APIC(void)
  495. {
  496. int maxlvt;
  497. u32 v;
  498. /* APIC hasn't been mapped yet */
  499. if (!apic_phys)
  500. return;
  501. maxlvt = lapic_get_maxlvt();
  502. /*
  503. * Masking an LVT entry can trigger a local APIC error
  504. * if the vector is zero. Mask LVTERR first to prevent this.
  505. */
  506. if (maxlvt >= 3) {
  507. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  508. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  509. }
  510. /*
  511. * Careful: we have to set masks only first to deassert
  512. * any level-triggered sources.
  513. */
  514. v = apic_read(APIC_LVTT);
  515. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  516. v = apic_read(APIC_LVT0);
  517. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  518. v = apic_read(APIC_LVT1);
  519. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  520. if (maxlvt >= 4) {
  521. v = apic_read(APIC_LVTPC);
  522. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  523. }
  524. /*
  525. * Clean APIC state for other OSs:
  526. */
  527. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  528. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  529. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  530. if (maxlvt >= 3)
  531. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  532. if (maxlvt >= 4)
  533. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  534. apic_write(APIC_ESR, 0);
  535. apic_read(APIC_ESR);
  536. }
  537. /**
  538. * disable_local_APIC - clear and disable the local APIC
  539. */
  540. void disable_local_APIC(void)
  541. {
  542. unsigned int value;
  543. clear_local_APIC();
  544. /*
  545. * Disable APIC (implies clearing of registers
  546. * for 82489DX!).
  547. */
  548. value = apic_read(APIC_SPIV);
  549. value &= ~APIC_SPIV_APIC_ENABLED;
  550. apic_write(APIC_SPIV, value);
  551. }
  552. void lapic_shutdown(void)
  553. {
  554. unsigned long flags;
  555. if (!cpu_has_apic)
  556. return;
  557. local_irq_save(flags);
  558. disable_local_APIC();
  559. local_irq_restore(flags);
  560. }
  561. /*
  562. * This is to verify that we're looking at a real local APIC.
  563. * Check these against your board if the CPUs aren't getting
  564. * started for no apparent reason.
  565. */
  566. int __init verify_local_APIC(void)
  567. {
  568. unsigned int reg0, reg1;
  569. /*
  570. * The version register is read-only in a real APIC.
  571. */
  572. reg0 = apic_read(APIC_LVR);
  573. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  574. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  575. reg1 = apic_read(APIC_LVR);
  576. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  577. /*
  578. * The two version reads above should print the same
  579. * numbers. If the second one is different, then we
  580. * poke at a non-APIC.
  581. */
  582. if (reg1 != reg0)
  583. return 0;
  584. /*
  585. * Check if the version looks reasonably.
  586. */
  587. reg1 = GET_APIC_VERSION(reg0);
  588. if (reg1 == 0x00 || reg1 == 0xff)
  589. return 0;
  590. reg1 = lapic_get_maxlvt();
  591. if (reg1 < 0x02 || reg1 == 0xff)
  592. return 0;
  593. /*
  594. * The ID register is read/write in a real APIC.
  595. */
  596. reg0 = apic_read(APIC_ID);
  597. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  598. apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
  599. reg1 = apic_read(APIC_ID);
  600. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  601. apic_write(APIC_ID, reg0);
  602. if (reg1 != (reg0 ^ APIC_ID_MASK))
  603. return 0;
  604. /*
  605. * The next two are just to see if we have sane values.
  606. * They're only really relevant if we're in Virtual Wire
  607. * compatibility mode, but most boxes are anymore.
  608. */
  609. reg0 = apic_read(APIC_LVT0);
  610. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  611. reg1 = apic_read(APIC_LVT1);
  612. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  613. return 1;
  614. }
  615. /**
  616. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  617. */
  618. void __init sync_Arb_IDs(void)
  619. {
  620. /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
  621. if (modern_apic())
  622. return;
  623. /*
  624. * Wait for idle.
  625. */
  626. apic_wait_icr_idle();
  627. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  628. apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  629. | APIC_DM_INIT);
  630. }
  631. /*
  632. * An initial setup of the virtual wire mode.
  633. */
  634. void __init init_bsp_APIC(void)
  635. {
  636. unsigned int value;
  637. /*
  638. * Don't do the setup now if we have a SMP BIOS as the
  639. * through-I/O-APIC virtual wire mode might be active.
  640. */
  641. if (smp_found_config || !cpu_has_apic)
  642. return;
  643. value = apic_read(APIC_LVR);
  644. /*
  645. * Do not trust the local APIC being empty at bootup.
  646. */
  647. clear_local_APIC();
  648. /*
  649. * Enable APIC.
  650. */
  651. value = apic_read(APIC_SPIV);
  652. value &= ~APIC_VECTOR_MASK;
  653. value |= APIC_SPIV_APIC_ENABLED;
  654. value |= APIC_SPIV_FOCUS_DISABLED;
  655. value |= SPURIOUS_APIC_VECTOR;
  656. apic_write(APIC_SPIV, value);
  657. /*
  658. * Set up the virtual wire mode.
  659. */
  660. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  661. value = APIC_DM_NMI;
  662. apic_write(APIC_LVT1, value);
  663. }
  664. /**
  665. * setup_local_APIC - setup the local APIC
  666. */
  667. void __cpuinit setup_local_APIC(void)
  668. {
  669. unsigned int value;
  670. int i, j;
  671. preempt_disable();
  672. value = apic_read(APIC_LVR);
  673. BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
  674. /*
  675. * Double-check whether this APIC is really registered.
  676. * This is meaningless in clustered apic mode, so we skip it.
  677. */
  678. if (!apic_id_registered())
  679. BUG();
  680. /*
  681. * Intel recommends to set DFR, LDR and TPR before enabling
  682. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  683. * document number 292116). So here it goes...
  684. */
  685. init_apic_ldr();
  686. /*
  687. * Set Task Priority to 'accept all'. We never change this
  688. * later on.
  689. */
  690. value = apic_read(APIC_TASKPRI);
  691. value &= ~APIC_TPRI_MASK;
  692. apic_write(APIC_TASKPRI, value);
  693. /*
  694. * After a crash, we no longer service the interrupts and a pending
  695. * interrupt from previous kernel might still have ISR bit set.
  696. *
  697. * Most probably by now CPU has serviced that pending interrupt and
  698. * it might not have done the ack_APIC_irq() because it thought,
  699. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  700. * does not clear the ISR bit and cpu thinks it has already serivced
  701. * the interrupt. Hence a vector might get locked. It was noticed
  702. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  703. */
  704. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  705. value = apic_read(APIC_ISR + i*0x10);
  706. for (j = 31; j >= 0; j--) {
  707. if (value & (1<<j))
  708. ack_APIC_irq();
  709. }
  710. }
  711. /*
  712. * Now that we are all set up, enable the APIC
  713. */
  714. value = apic_read(APIC_SPIV);
  715. value &= ~APIC_VECTOR_MASK;
  716. /*
  717. * Enable APIC
  718. */
  719. value |= APIC_SPIV_APIC_ENABLED;
  720. /* We always use processor focus */
  721. /*
  722. * Set spurious IRQ vector
  723. */
  724. value |= SPURIOUS_APIC_VECTOR;
  725. apic_write(APIC_SPIV, value);
  726. /*
  727. * Set up LVT0, LVT1:
  728. *
  729. * set up through-local-APIC on the BP's LINT0. This is not
  730. * strictly necessary in pure symmetric-IO mode, but sometimes
  731. * we delegate interrupts to the 8259A.
  732. */
  733. /*
  734. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  735. */
  736. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  737. if (!smp_processor_id() && !value) {
  738. value = APIC_DM_EXTINT;
  739. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  740. smp_processor_id());
  741. } else {
  742. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  743. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  744. smp_processor_id());
  745. }
  746. apic_write(APIC_LVT0, value);
  747. /*
  748. * only the BP should see the LINT1 NMI signal, obviously.
  749. */
  750. if (!smp_processor_id())
  751. value = APIC_DM_NMI;
  752. else
  753. value = APIC_DM_NMI | APIC_LVT_MASKED;
  754. apic_write(APIC_LVT1, value);
  755. preempt_enable();
  756. }
  757. static void __cpuinit lapic_setup_esr(void)
  758. {
  759. unsigned maxlvt = lapic_get_maxlvt();
  760. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
  761. /*
  762. * spec says clear errors after enabling vector.
  763. */
  764. if (maxlvt > 3)
  765. apic_write(APIC_ESR, 0);
  766. }
  767. void __cpuinit end_local_APIC_setup(void)
  768. {
  769. lapic_setup_esr();
  770. setup_apic_nmi_watchdog(NULL);
  771. apic_pm_activate();
  772. }
  773. void check_x2apic(void)
  774. {
  775. int msr, msr2;
  776. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  777. if (msr & X2APIC_ENABLE) {
  778. printk("x2apic enabled by BIOS, switching to x2apic ops\n");
  779. x2apic_preenabled = x2apic = 1;
  780. apic_ops = &x2apic_ops;
  781. }
  782. }
  783. void enable_x2apic(void)
  784. {
  785. int msr, msr2;
  786. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  787. if (!(msr & X2APIC_ENABLE)) {
  788. printk("Enabling x2apic\n");
  789. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  790. }
  791. }
  792. void enable_IR_x2apic(void)
  793. {
  794. #ifdef CONFIG_INTR_REMAP
  795. int ret;
  796. unsigned long flags;
  797. if (!cpu_has_x2apic)
  798. return;
  799. if (!x2apic_preenabled && disable_x2apic) {
  800. printk(KERN_INFO
  801. "Skipped enabling x2apic and Interrupt-remapping "
  802. "because of nox2apic\n");
  803. return;
  804. }
  805. if (x2apic_preenabled && disable_x2apic)
  806. panic("Bios already enabled x2apic, can't enforce nox2apic");
  807. if (!x2apic_preenabled && skip_ioapic_setup) {
  808. printk(KERN_INFO
  809. "Skipped enabling x2apic and Interrupt-remapping "
  810. "because of skipping io-apic setup\n");
  811. return;
  812. }
  813. ret = dmar_table_init();
  814. if (ret) {
  815. printk(KERN_INFO
  816. "dmar_table_init() failed with %d:\n", ret);
  817. if (x2apic_preenabled)
  818. panic("x2apic enabled by bios. But IR enabling failed");
  819. else
  820. printk(KERN_INFO
  821. "Not enabling x2apic,Intr-remapping\n");
  822. return;
  823. }
  824. local_irq_save(flags);
  825. mask_8259A();
  826. save_mask_IO_APIC_setup();
  827. ret = enable_intr_remapping(1);
  828. if (ret && x2apic_preenabled) {
  829. local_irq_restore(flags);
  830. panic("x2apic enabled by bios. But IR enabling failed");
  831. }
  832. if (ret)
  833. goto end;
  834. if (!x2apic) {
  835. x2apic = 1;
  836. apic_ops = &x2apic_ops;
  837. enable_x2apic();
  838. }
  839. end:
  840. if (ret)
  841. /*
  842. * IR enabling failed
  843. */
  844. restore_IO_APIC_setup();
  845. else
  846. reinit_intr_remapped_IO_APIC(x2apic_preenabled);
  847. unmask_8259A();
  848. local_irq_restore(flags);
  849. if (!ret) {
  850. if (!x2apic_preenabled)
  851. printk(KERN_INFO
  852. "Enabled x2apic and interrupt-remapping\n");
  853. else
  854. printk(KERN_INFO
  855. "Enabled Interrupt-remapping\n");
  856. } else
  857. printk(KERN_ERR
  858. "Failed to enable Interrupt-remapping and x2apic\n");
  859. #else
  860. if (!cpu_has_x2apic)
  861. return;
  862. if (x2apic_preenabled)
  863. panic("x2apic enabled prior OS handover,"
  864. " enable CONFIG_INTR_REMAP");
  865. printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
  866. " and x2apic\n");
  867. #endif
  868. return;
  869. }
  870. /*
  871. * Detect and enable local APICs on non-SMP boards.
  872. * Original code written by Keir Fraser.
  873. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  874. * not correctly set up (usually the APIC timer won't work etc.)
  875. */
  876. static int __init detect_init_APIC(void)
  877. {
  878. if (!cpu_has_apic) {
  879. printk(KERN_INFO "No local APIC present\n");
  880. return -1;
  881. }
  882. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  883. boot_cpu_physical_apicid = 0;
  884. return 0;
  885. }
  886. void __init early_init_lapic_mapping(void)
  887. {
  888. unsigned long phys_addr;
  889. /*
  890. * If no local APIC can be found then go out
  891. * : it means there is no mpatable and MADT
  892. */
  893. if (!smp_found_config)
  894. return;
  895. phys_addr = mp_lapic_addr;
  896. set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
  897. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  898. APIC_BASE, phys_addr);
  899. /*
  900. * Fetch the APIC ID of the BSP in case we have a
  901. * default configuration (or the MP table is broken).
  902. */
  903. boot_cpu_physical_apicid = read_apic_id();
  904. }
  905. /**
  906. * init_apic_mappings - initialize APIC mappings
  907. */
  908. void __init init_apic_mappings(void)
  909. {
  910. if (x2apic) {
  911. boot_cpu_physical_apicid = read_apic_id();
  912. return;
  913. }
  914. /*
  915. * If no local APIC can be found then set up a fake all
  916. * zeroes page to simulate the local APIC and another
  917. * one for the IO-APIC.
  918. */
  919. if (!smp_found_config && detect_init_APIC()) {
  920. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  921. apic_phys = __pa(apic_phys);
  922. } else
  923. apic_phys = mp_lapic_addr;
  924. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  925. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  926. APIC_BASE, apic_phys);
  927. /*
  928. * Fetch the APIC ID of the BSP in case we have a
  929. * default configuration (or the MP table is broken).
  930. */
  931. boot_cpu_physical_apicid = read_apic_id();
  932. }
  933. /*
  934. * This initializes the IO-APIC and APIC hardware if this is
  935. * a UP kernel.
  936. */
  937. int __init APIC_init_uniprocessor(void)
  938. {
  939. if (disable_apic) {
  940. printk(KERN_INFO "Apic disabled\n");
  941. return -1;
  942. }
  943. if (!cpu_has_apic) {
  944. disable_apic = 1;
  945. printk(KERN_INFO "Apic disabled by BIOS\n");
  946. return -1;
  947. }
  948. enable_IR_x2apic();
  949. setup_apic_routing();
  950. verify_local_APIC();
  951. connect_bsp_APIC();
  952. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  953. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  954. setup_local_APIC();
  955. /*
  956. * Now enable IO-APICs, actually call clear_IO_APIC
  957. * We need clear_IO_APIC before enabling vector on BP
  958. */
  959. if (!skip_ioapic_setup && nr_ioapics)
  960. enable_IO_APIC();
  961. if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
  962. localise_nmi_watchdog();
  963. end_local_APIC_setup();
  964. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  965. setup_IO_APIC();
  966. else
  967. nr_ioapics = 0;
  968. setup_boot_APIC_clock();
  969. check_nmi_watchdog();
  970. return 0;
  971. }
  972. /*
  973. * Local APIC interrupts
  974. */
  975. /*
  976. * This interrupt should _never_ happen with our APIC/SMP architecture
  977. */
  978. asmlinkage void smp_spurious_interrupt(void)
  979. {
  980. unsigned int v;
  981. exit_idle();
  982. irq_enter();
  983. /*
  984. * Check if this really is a spurious interrupt and ACK it
  985. * if it is a vectored one. Just in case...
  986. * Spurious interrupts should not be ACKed.
  987. */
  988. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  989. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  990. ack_APIC_irq();
  991. add_pda(irq_spurious_count, 1);
  992. irq_exit();
  993. }
  994. /*
  995. * This interrupt should never happen with our APIC/SMP architecture
  996. */
  997. asmlinkage void smp_error_interrupt(void)
  998. {
  999. unsigned int v, v1;
  1000. exit_idle();
  1001. irq_enter();
  1002. /* First tickle the hardware, only then report what went on. -- REW */
  1003. v = apic_read(APIC_ESR);
  1004. apic_write(APIC_ESR, 0);
  1005. v1 = apic_read(APIC_ESR);
  1006. ack_APIC_irq();
  1007. atomic_inc(&irq_err_count);
  1008. /* Here is what the APIC error bits mean:
  1009. 0: Send CS error
  1010. 1: Receive CS error
  1011. 2: Send accept error
  1012. 3: Receive accept error
  1013. 4: Reserved
  1014. 5: Send illegal vector
  1015. 6: Received illegal vector
  1016. 7: Illegal register address
  1017. */
  1018. printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
  1019. smp_processor_id(), v , v1);
  1020. irq_exit();
  1021. }
  1022. /**
  1023. * * connect_bsp_APIC - attach the APIC to the interrupt system
  1024. * */
  1025. void __init connect_bsp_APIC(void)
  1026. {
  1027. enable_apic_mode();
  1028. }
  1029. void disconnect_bsp_APIC(int virt_wire_setup)
  1030. {
  1031. /* Go back to Virtual Wire compatibility mode */
  1032. unsigned long value;
  1033. /* For the spurious interrupt use vector F, and enable it */
  1034. value = apic_read(APIC_SPIV);
  1035. value &= ~APIC_VECTOR_MASK;
  1036. value |= APIC_SPIV_APIC_ENABLED;
  1037. value |= 0xf;
  1038. apic_write(APIC_SPIV, value);
  1039. if (!virt_wire_setup) {
  1040. /*
  1041. * For LVT0 make it edge triggered, active high,
  1042. * external and enabled
  1043. */
  1044. value = apic_read(APIC_LVT0);
  1045. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1046. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1047. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1048. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1049. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1050. apic_write(APIC_LVT0, value);
  1051. } else {
  1052. /* Disable LVT0 */
  1053. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1054. }
  1055. /* For LVT1 make it edge triggered, active high, nmi and enabled */
  1056. value = apic_read(APIC_LVT1);
  1057. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1058. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1059. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1060. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1061. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1062. apic_write(APIC_LVT1, value);
  1063. }
  1064. void __cpuinit generic_processor_info(int apicid, int version)
  1065. {
  1066. int cpu;
  1067. cpumask_t tmp_map;
  1068. if (num_processors >= NR_CPUS) {
  1069. printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
  1070. " Processor ignored.\n", NR_CPUS);
  1071. return;
  1072. }
  1073. if (num_processors >= maxcpus) {
  1074. printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
  1075. " Processor ignored.\n", maxcpus);
  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);