apic_64.c 43 KB

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