apic_64.c 30 KB

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