apic_64.c 29 KB

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