apic.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. /*
  2. * Local APIC handling, local APIC timers
  3. *
  4. * (c) 1999, 2000, 2009 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/perf_event.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/mc146818rtc.h>
  19. #include <linux/acpi_pmtmr.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/ioport.h>
  25. #include <linux/module.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/timex.h>
  29. #include <linux/dmar.h>
  30. #include <linux/init.h>
  31. #include <linux/cpu.h>
  32. #include <linux/dmi.h>
  33. #include <linux/smp.h>
  34. #include <linux/mm.h>
  35. #include <asm/perf_event.h>
  36. #include <asm/x86_init.h>
  37. #include <asm/pgalloc.h>
  38. #include <asm/atomic.h>
  39. #include <asm/mpspec.h>
  40. #include <asm/i8253.h>
  41. #include <asm/i8259.h>
  42. #include <asm/proto.h>
  43. #include <asm/apic.h>
  44. #include <asm/io_apic.h>
  45. #include <asm/desc.h>
  46. #include <asm/hpet.h>
  47. #include <asm/idle.h>
  48. #include <asm/mtrr.h>
  49. #include <asm/smp.h>
  50. #include <asm/mce.h>
  51. #include <asm/tsc.h>
  52. #include <asm/hypervisor.h>
  53. unsigned int num_processors;
  54. unsigned disabled_cpus __cpuinitdata;
  55. /* Processor that is doing the boot up */
  56. unsigned int boot_cpu_physical_apicid = -1U;
  57. /*
  58. * The highest APIC ID seen during enumeration.
  59. */
  60. unsigned int max_physical_apicid;
  61. /*
  62. * Bitmask of physically existing CPUs:
  63. */
  64. physid_mask_t phys_cpu_present_map;
  65. /*
  66. * Map cpu index to physical APIC ID
  67. */
  68. DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
  69. DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
  70. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  71. EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  72. #ifdef CONFIG_X86_32
  73. /*
  74. * Knob to control our willingness to enable the local APIC.
  75. *
  76. * +1=force-enable
  77. */
  78. static int force_enable_local_apic __initdata;
  79. /*
  80. * APIC command line parameters
  81. */
  82. static int __init parse_lapic(char *arg)
  83. {
  84. force_enable_local_apic = 1;
  85. return 0;
  86. }
  87. early_param("lapic", parse_lapic);
  88. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  89. static int enabled_via_apicbase;
  90. /*
  91. * Handle interrupt mode configuration register (IMCR).
  92. * This register controls whether the interrupt signals
  93. * that reach the BSP come from the master PIC or from the
  94. * local APIC. Before entering Symmetric I/O Mode, either
  95. * the BIOS or the operating system must switch out of
  96. * PIC Mode by changing the IMCR.
  97. */
  98. static inline void imcr_pic_to_apic(void)
  99. {
  100. /* select IMCR register */
  101. outb(0x70, 0x22);
  102. /* NMI and 8259 INTR go through APIC */
  103. outb(0x01, 0x23);
  104. }
  105. static inline void imcr_apic_to_pic(void)
  106. {
  107. /* select IMCR register */
  108. outb(0x70, 0x22);
  109. /* NMI and 8259 INTR go directly to BSP */
  110. outb(0x00, 0x23);
  111. }
  112. #endif
  113. #ifdef CONFIG_X86_64
  114. static int apic_calibrate_pmtmr __initdata;
  115. static __init int setup_apicpmtimer(char *s)
  116. {
  117. apic_calibrate_pmtmr = 1;
  118. notsc_setup(NULL);
  119. return 0;
  120. }
  121. __setup("apicpmtimer", setup_apicpmtimer);
  122. #endif
  123. int x2apic_mode;
  124. #ifdef CONFIG_X86_X2APIC
  125. /* x2apic enabled before OS handover */
  126. static int x2apic_preenabled;
  127. static __init int setup_nox2apic(char *str)
  128. {
  129. if (x2apic_enabled()) {
  130. pr_warning("Bios already enabled x2apic, "
  131. "can't enforce nox2apic");
  132. return 0;
  133. }
  134. setup_clear_cpu_cap(X86_FEATURE_X2APIC);
  135. return 0;
  136. }
  137. early_param("nox2apic", setup_nox2apic);
  138. #endif
  139. unsigned long mp_lapic_addr;
  140. int disable_apic;
  141. /* Disable local APIC timer from the kernel commandline or via dmi quirk */
  142. static int disable_apic_timer __initdata;
  143. /* Local APIC timer works in C2 */
  144. int local_apic_timer_c2_ok;
  145. EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
  146. int first_system_vector = 0xfe;
  147. /*
  148. * Debug level, exported for io_apic.c
  149. */
  150. unsigned int apic_verbosity;
  151. int pic_mode;
  152. /* Have we found an MP table */
  153. int smp_found_config;
  154. static struct resource lapic_resource = {
  155. .name = "Local APIC",
  156. .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
  157. };
  158. static unsigned int calibration_result;
  159. static void apic_pm_activate(void);
  160. static unsigned long apic_phys;
  161. /*
  162. * Get the LAPIC version
  163. */
  164. static inline int lapic_get_version(void)
  165. {
  166. return GET_APIC_VERSION(apic_read(APIC_LVR));
  167. }
  168. /*
  169. * Check, if the APIC is integrated or a separate chip
  170. */
  171. static inline int lapic_is_integrated(void)
  172. {
  173. #ifdef CONFIG_X86_64
  174. return 1;
  175. #else
  176. return APIC_INTEGRATED(lapic_get_version());
  177. #endif
  178. }
  179. /*
  180. * Check, whether this is a modern or a first generation APIC
  181. */
  182. static int modern_apic(void)
  183. {
  184. /* AMD systems use old APIC versions, so check the CPU */
  185. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  186. boot_cpu_data.x86 >= 0xf)
  187. return 1;
  188. return lapic_get_version() >= 0x14;
  189. }
  190. /*
  191. * right after this call apic become NOOP driven
  192. * so apic->write/read doesn't do anything
  193. */
  194. static void __init apic_disable(void)
  195. {
  196. pr_info("APIC: switched to apic NOOP\n");
  197. apic = &apic_noop;
  198. }
  199. void native_apic_wait_icr_idle(void)
  200. {
  201. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  202. cpu_relax();
  203. }
  204. u32 native_safe_apic_wait_icr_idle(void)
  205. {
  206. u32 send_status;
  207. int timeout;
  208. timeout = 0;
  209. do {
  210. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  211. if (!send_status)
  212. break;
  213. udelay(100);
  214. } while (timeout++ < 1000);
  215. return send_status;
  216. }
  217. void native_apic_icr_write(u32 low, u32 id)
  218. {
  219. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
  220. apic_write(APIC_ICR, low);
  221. }
  222. u64 native_apic_icr_read(void)
  223. {
  224. u32 icr1, icr2;
  225. icr2 = apic_read(APIC_ICR2);
  226. icr1 = apic_read(APIC_ICR);
  227. return icr1 | ((u64)icr2 << 32);
  228. }
  229. #ifdef CONFIG_X86_32
  230. /**
  231. * get_physical_broadcast - Get number of physical broadcast IDs
  232. */
  233. int get_physical_broadcast(void)
  234. {
  235. return modern_apic() ? 0xff : 0xf;
  236. }
  237. #endif
  238. /**
  239. * lapic_get_maxlvt - get the maximum number of local vector table entries
  240. */
  241. int lapic_get_maxlvt(void)
  242. {
  243. unsigned int v;
  244. v = apic_read(APIC_LVR);
  245. /*
  246. * - we always have APIC integrated on 64bit mode
  247. * - 82489DXs do not report # of LVT entries
  248. */
  249. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  250. }
  251. /*
  252. * Local APIC timer
  253. */
  254. /* Clock divisor */
  255. #define APIC_DIVISOR 16
  256. /*
  257. * This function sets up the local APIC timer, with a timeout of
  258. * 'clocks' APIC bus clock. During calibration we actually call
  259. * this function twice on the boot CPU, once with a bogus timeout
  260. * value, second time for real. The other (noncalibrating) CPUs
  261. * call this function only once, with the real, calibrated value.
  262. *
  263. * We do reads before writes even if unnecessary, to get around the
  264. * P5 APIC double write bug.
  265. */
  266. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  267. {
  268. unsigned int lvtt_value, tmp_value;
  269. lvtt_value = LOCAL_TIMER_VECTOR;
  270. if (!oneshot)
  271. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  272. if (!lapic_is_integrated())
  273. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  274. if (!irqen)
  275. lvtt_value |= APIC_LVT_MASKED;
  276. apic_write(APIC_LVTT, lvtt_value);
  277. /*
  278. * Divide PICLK by 16
  279. */
  280. tmp_value = apic_read(APIC_TDCR);
  281. apic_write(APIC_TDCR,
  282. (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
  283. APIC_TDR_DIV_16);
  284. if (!oneshot)
  285. apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
  286. }
  287. /*
  288. * Setup extended LVT, AMD specific
  289. *
  290. * Software should use the LVT offsets the BIOS provides. The offsets
  291. * are determined by the subsystems using it like those for MCE
  292. * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
  293. * are supported. Beginning with family 10h at least 4 offsets are
  294. * available.
  295. *
  296. * Since the offsets must be consistent for all cores, we keep track
  297. * of the LVT offsets in software and reserve the offset for the same
  298. * vector also to be used on other cores. An offset is freed by
  299. * setting the entry to APIC_EILVT_MASKED.
  300. *
  301. * If the BIOS is right, there should be no conflicts. Otherwise a
  302. * "[Firmware Bug]: ..." error message is generated. However, if
  303. * software does not properly determines the offsets, it is not
  304. * necessarily a BIOS bug.
  305. */
  306. static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
  307. static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
  308. {
  309. return (old & APIC_EILVT_MASKED)
  310. || (new == APIC_EILVT_MASKED)
  311. || ((new & ~APIC_EILVT_MASKED) == old);
  312. }
  313. static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
  314. {
  315. unsigned int rsvd; /* 0: uninitialized */
  316. if (offset >= APIC_EILVT_NR_MAX)
  317. return ~0;
  318. rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
  319. do {
  320. if (rsvd &&
  321. !eilvt_entry_is_changeable(rsvd, new))
  322. /* may not change if vectors are different */
  323. return rsvd;
  324. rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
  325. } while (rsvd != new);
  326. return new;
  327. }
  328. /*
  329. * If mask=1, the LVT entry does not generate interrupts while mask=0
  330. * enables the vector. See also the BKDGs.
  331. */
  332. int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
  333. {
  334. unsigned long reg = APIC_EILVTn(offset);
  335. unsigned int new, old, reserved;
  336. new = (mask << 16) | (msg_type << 8) | vector;
  337. old = apic_read(reg);
  338. reserved = reserve_eilvt_offset(offset, new);
  339. if (reserved != new) {
  340. pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
  341. "vector 0x%x, but the register is already in use for "
  342. "vector 0x%x on another cpu\n",
  343. smp_processor_id(), reg, offset, new, reserved);
  344. return -EINVAL;
  345. }
  346. if (!eilvt_entry_is_changeable(old, new)) {
  347. pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
  348. "vector 0x%x, but the register is already in use for "
  349. "vector 0x%x on this cpu\n",
  350. smp_processor_id(), reg, offset, new, old);
  351. return -EBUSY;
  352. }
  353. apic_write(reg, new);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
  357. /*
  358. * Program the next event, relative to now
  359. */
  360. static int lapic_next_event(unsigned long delta,
  361. struct clock_event_device *evt)
  362. {
  363. apic_write(APIC_TMICT, delta);
  364. return 0;
  365. }
  366. /*
  367. * Setup the lapic timer in periodic or oneshot mode
  368. */
  369. static void lapic_timer_setup(enum clock_event_mode mode,
  370. struct clock_event_device *evt)
  371. {
  372. unsigned long flags;
  373. unsigned int v;
  374. /* Lapic used as dummy for broadcast ? */
  375. if (evt->features & CLOCK_EVT_FEAT_DUMMY)
  376. return;
  377. local_irq_save(flags);
  378. switch (mode) {
  379. case CLOCK_EVT_MODE_PERIODIC:
  380. case CLOCK_EVT_MODE_ONESHOT:
  381. __setup_APIC_LVTT(calibration_result,
  382. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  383. break;
  384. case CLOCK_EVT_MODE_UNUSED:
  385. case CLOCK_EVT_MODE_SHUTDOWN:
  386. v = apic_read(APIC_LVTT);
  387. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  388. apic_write(APIC_LVTT, v);
  389. apic_write(APIC_TMICT, 0);
  390. break;
  391. case CLOCK_EVT_MODE_RESUME:
  392. /* Nothing to do here */
  393. break;
  394. }
  395. local_irq_restore(flags);
  396. }
  397. /*
  398. * Local APIC timer broadcast function
  399. */
  400. static void lapic_timer_broadcast(const struct cpumask *mask)
  401. {
  402. #ifdef CONFIG_SMP
  403. apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  404. #endif
  405. }
  406. /*
  407. * The local apic timer can be used for any function which is CPU local.
  408. */
  409. static struct clock_event_device lapic_clockevent = {
  410. .name = "lapic",
  411. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  412. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  413. .shift = 32,
  414. .set_mode = lapic_timer_setup,
  415. .set_next_event = lapic_next_event,
  416. .broadcast = lapic_timer_broadcast,
  417. .rating = 100,
  418. .irq = -1,
  419. };
  420. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  421. /*
  422. * Setup the local APIC timer for this CPU. Copy the initialized values
  423. * of the boot CPU and register the clock event in the framework.
  424. */
  425. static void __cpuinit setup_APIC_timer(void)
  426. {
  427. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  428. if (cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_ARAT)) {
  429. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
  430. /* Make LAPIC timer preferrable over percpu HPET */
  431. lapic_clockevent.rating = 150;
  432. }
  433. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  434. levt->cpumask = cpumask_of(smp_processor_id());
  435. clockevents_register_device(levt);
  436. }
  437. /*
  438. * In this functions we calibrate APIC bus clocks to the external timer.
  439. *
  440. * We want to do the calibration only once since we want to have local timer
  441. * irqs syncron. CPUs connected by the same APIC bus have the very same bus
  442. * frequency.
  443. *
  444. * This was previously done by reading the PIT/HPET and waiting for a wrap
  445. * around to find out, that a tick has elapsed. I have a box, where the PIT
  446. * readout is broken, so it never gets out of the wait loop again. This was
  447. * also reported by others.
  448. *
  449. * Monitoring the jiffies value is inaccurate and the clockevents
  450. * infrastructure allows us to do a simple substitution of the interrupt
  451. * handler.
  452. *
  453. * The calibration routine also uses the pm_timer when possible, as the PIT
  454. * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
  455. * back to normal later in the boot process).
  456. */
  457. #define LAPIC_CAL_LOOPS (HZ/10)
  458. static __initdata int lapic_cal_loops = -1;
  459. static __initdata long lapic_cal_t1, lapic_cal_t2;
  460. static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
  461. static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
  462. static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
  463. /*
  464. * Temporary interrupt handler.
  465. */
  466. static void __init lapic_cal_handler(struct clock_event_device *dev)
  467. {
  468. unsigned long long tsc = 0;
  469. long tapic = apic_read(APIC_TMCCT);
  470. unsigned long pm = acpi_pm_read_early();
  471. if (cpu_has_tsc)
  472. rdtscll(tsc);
  473. switch (lapic_cal_loops++) {
  474. case 0:
  475. lapic_cal_t1 = tapic;
  476. lapic_cal_tsc1 = tsc;
  477. lapic_cal_pm1 = pm;
  478. lapic_cal_j1 = jiffies;
  479. break;
  480. case LAPIC_CAL_LOOPS:
  481. lapic_cal_t2 = tapic;
  482. lapic_cal_tsc2 = tsc;
  483. if (pm < lapic_cal_pm1)
  484. pm += ACPI_PM_OVRRUN;
  485. lapic_cal_pm2 = pm;
  486. lapic_cal_j2 = jiffies;
  487. break;
  488. }
  489. }
  490. static int __init
  491. calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
  492. {
  493. const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
  494. const long pm_thresh = pm_100ms / 100;
  495. unsigned long mult;
  496. u64 res;
  497. #ifndef CONFIG_X86_PM_TIMER
  498. return -1;
  499. #endif
  500. apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
  501. /* Check, if the PM timer is available */
  502. if (!deltapm)
  503. return -1;
  504. mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
  505. if (deltapm > (pm_100ms - pm_thresh) &&
  506. deltapm < (pm_100ms + pm_thresh)) {
  507. apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
  508. return 0;
  509. }
  510. res = (((u64)deltapm) * mult) >> 22;
  511. do_div(res, 1000000);
  512. pr_warning("APIC calibration not consistent "
  513. "with PM-Timer: %ldms instead of 100ms\n",(long)res);
  514. /* Correct the lapic counter value */
  515. res = (((u64)(*delta)) * pm_100ms);
  516. do_div(res, deltapm);
  517. pr_info("APIC delta adjusted to PM-Timer: "
  518. "%lu (%ld)\n", (unsigned long)res, *delta);
  519. *delta = (long)res;
  520. /* Correct the tsc counter value */
  521. if (cpu_has_tsc) {
  522. res = (((u64)(*deltatsc)) * pm_100ms);
  523. do_div(res, deltapm);
  524. apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
  525. "PM-Timer: %lu (%ld)\n",
  526. (unsigned long)res, *deltatsc);
  527. *deltatsc = (long)res;
  528. }
  529. return 0;
  530. }
  531. static int __init calibrate_APIC_clock(void)
  532. {
  533. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  534. void (*real_handler)(struct clock_event_device *dev);
  535. unsigned long deltaj;
  536. long delta, deltatsc;
  537. int pm_referenced = 0;
  538. local_irq_disable();
  539. /* Replace the global interrupt handler */
  540. real_handler = global_clock_event->event_handler;
  541. global_clock_event->event_handler = lapic_cal_handler;
  542. /*
  543. * Setup the APIC counter to maximum. There is no way the lapic
  544. * can underflow in the 100ms detection time frame
  545. */
  546. __setup_APIC_LVTT(0xffffffff, 0, 0);
  547. /* Let the interrupts run */
  548. local_irq_enable();
  549. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  550. cpu_relax();
  551. local_irq_disable();
  552. /* Restore the real event handler */
  553. global_clock_event->event_handler = real_handler;
  554. /* Build delta t1-t2 as apic timer counts down */
  555. delta = lapic_cal_t1 - lapic_cal_t2;
  556. apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
  557. deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
  558. /* we trust the PM based calibration if possible */
  559. pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
  560. &delta, &deltatsc);
  561. /* Calculate the scaled math multiplication factor */
  562. lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
  563. lapic_clockevent.shift);
  564. lapic_clockevent.max_delta_ns =
  565. clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
  566. lapic_clockevent.min_delta_ns =
  567. clockevent_delta2ns(0xF, &lapic_clockevent);
  568. calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
  569. apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
  570. apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
  571. apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
  572. calibration_result);
  573. if (cpu_has_tsc) {
  574. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  575. "%ld.%04ld MHz.\n",
  576. (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
  577. (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
  578. }
  579. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  580. "%u.%04u MHz.\n",
  581. calibration_result / (1000000 / HZ),
  582. calibration_result % (1000000 / HZ));
  583. /*
  584. * Do a sanity check on the APIC calibration result
  585. */
  586. if (calibration_result < (1000000 / HZ)) {
  587. local_irq_enable();
  588. pr_warning("APIC frequency too slow, disabling apic timer\n");
  589. return -1;
  590. }
  591. levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
  592. /*
  593. * PM timer calibration failed or not turned on
  594. * so lets try APIC timer based calibration
  595. */
  596. if (!pm_referenced) {
  597. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  598. /*
  599. * Setup the apic timer manually
  600. */
  601. levt->event_handler = lapic_cal_handler;
  602. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  603. lapic_cal_loops = -1;
  604. /* Let the interrupts run */
  605. local_irq_enable();
  606. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  607. cpu_relax();
  608. /* Stop the lapic timer */
  609. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  610. /* Jiffies delta */
  611. deltaj = lapic_cal_j2 - lapic_cal_j1;
  612. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  613. /* Check, if the jiffies result is consistent */
  614. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  615. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  616. else
  617. levt->features |= CLOCK_EVT_FEAT_DUMMY;
  618. } else
  619. local_irq_enable();
  620. if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
  621. pr_warning("APIC timer disabled due to verification failure\n");
  622. return -1;
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Setup the boot APIC
  628. *
  629. * Calibrate and verify the result.
  630. */
  631. void __init setup_boot_APIC_clock(void)
  632. {
  633. /*
  634. * The local apic timer can be disabled via the kernel
  635. * commandline or from the CPU detection code. Register the lapic
  636. * timer as a dummy clock event source on SMP systems, so the
  637. * broadcast mechanism is used. On UP systems simply ignore it.
  638. */
  639. if (disable_apic_timer) {
  640. pr_info("Disabling APIC timer\n");
  641. /* No broadcast on UP ! */
  642. if (num_possible_cpus() > 1) {
  643. lapic_clockevent.mult = 1;
  644. setup_APIC_timer();
  645. }
  646. return;
  647. }
  648. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  649. "calibrating APIC timer ...\n");
  650. if (calibrate_APIC_clock()) {
  651. /* No broadcast on UP ! */
  652. if (num_possible_cpus() > 1)
  653. setup_APIC_timer();
  654. return;
  655. }
  656. /*
  657. * If nmi_watchdog is set to IO_APIC, we need the
  658. * PIT/HPET going. Otherwise register lapic as a dummy
  659. * device.
  660. */
  661. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  662. /* Setup the lapic or request the broadcast */
  663. setup_APIC_timer();
  664. }
  665. void __cpuinit setup_secondary_APIC_clock(void)
  666. {
  667. setup_APIC_timer();
  668. }
  669. /*
  670. * The guts of the apic timer interrupt
  671. */
  672. static void local_apic_timer_interrupt(void)
  673. {
  674. int cpu = smp_processor_id();
  675. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  676. /*
  677. * Normally we should not be here till LAPIC has been initialized but
  678. * in some cases like kdump, its possible that there is a pending LAPIC
  679. * timer interrupt from previous kernel's context and is delivered in
  680. * new kernel the moment interrupts are enabled.
  681. *
  682. * Interrupts are enabled early and LAPIC is setup much later, hence
  683. * its possible that when we get here evt->event_handler is NULL.
  684. * Check for event_handler being NULL and discard the interrupt as
  685. * spurious.
  686. */
  687. if (!evt->event_handler) {
  688. pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  689. /* Switch it off */
  690. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  691. return;
  692. }
  693. /*
  694. * the NMI deadlock-detector uses this.
  695. */
  696. inc_irq_stat(apic_timer_irqs);
  697. evt->event_handler(evt);
  698. }
  699. /*
  700. * Local APIC timer interrupt. This is the most natural way for doing
  701. * local interrupts, but local timer interrupts can be emulated by
  702. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  703. *
  704. * [ if a single-CPU system runs an SMP kernel then we call the local
  705. * interrupt as well. Thus we cannot inline the local irq ... ]
  706. */
  707. void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
  708. {
  709. struct pt_regs *old_regs = set_irq_regs(regs);
  710. /*
  711. * NOTE! We'd better ACK the irq immediately,
  712. * because timer handling can be slow.
  713. */
  714. ack_APIC_irq();
  715. /*
  716. * update_process_times() expects us to have done irq_enter().
  717. * Besides, if we don't timer interrupts ignore the global
  718. * interrupt lock, which is the WrongThing (tm) to do.
  719. */
  720. exit_idle();
  721. irq_enter();
  722. local_apic_timer_interrupt();
  723. irq_exit();
  724. set_irq_regs(old_regs);
  725. }
  726. int setup_profiling_timer(unsigned int multiplier)
  727. {
  728. return -EINVAL;
  729. }
  730. /*
  731. * Local APIC start and shutdown
  732. */
  733. /**
  734. * clear_local_APIC - shutdown the local APIC
  735. *
  736. * This is called, when a CPU is disabled and before rebooting, so the state of
  737. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  738. * leftovers during boot.
  739. */
  740. void clear_local_APIC(void)
  741. {
  742. int maxlvt;
  743. u32 v;
  744. /* APIC hasn't been mapped yet */
  745. if (!x2apic_mode && !apic_phys)
  746. return;
  747. maxlvt = lapic_get_maxlvt();
  748. /*
  749. * Masking an LVT entry can trigger a local APIC error
  750. * if the vector is zero. Mask LVTERR first to prevent this.
  751. */
  752. if (maxlvt >= 3) {
  753. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  754. apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
  755. }
  756. /*
  757. * Careful: we have to set masks only first to deassert
  758. * any level-triggered sources.
  759. */
  760. v = apic_read(APIC_LVTT);
  761. apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
  762. v = apic_read(APIC_LVT0);
  763. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  764. v = apic_read(APIC_LVT1);
  765. apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
  766. if (maxlvt >= 4) {
  767. v = apic_read(APIC_LVTPC);
  768. apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
  769. }
  770. /* lets not touch this if we didn't frob it */
  771. #ifdef CONFIG_X86_THERMAL_VECTOR
  772. if (maxlvt >= 5) {
  773. v = apic_read(APIC_LVTTHMR);
  774. apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  775. }
  776. #endif
  777. #ifdef CONFIG_X86_MCE_INTEL
  778. if (maxlvt >= 6) {
  779. v = apic_read(APIC_LVTCMCI);
  780. if (!(v & APIC_LVT_MASKED))
  781. apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
  782. }
  783. #endif
  784. /*
  785. * Clean APIC state for other OSs:
  786. */
  787. apic_write(APIC_LVTT, APIC_LVT_MASKED);
  788. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  789. apic_write(APIC_LVT1, APIC_LVT_MASKED);
  790. if (maxlvt >= 3)
  791. apic_write(APIC_LVTERR, APIC_LVT_MASKED);
  792. if (maxlvt >= 4)
  793. apic_write(APIC_LVTPC, APIC_LVT_MASKED);
  794. /* Integrated APIC (!82489DX) ? */
  795. if (lapic_is_integrated()) {
  796. if (maxlvt > 3)
  797. /* Clear ESR due to Pentium errata 3AP and 11AP */
  798. apic_write(APIC_ESR, 0);
  799. apic_read(APIC_ESR);
  800. }
  801. }
  802. /**
  803. * disable_local_APIC - clear and disable the local APIC
  804. */
  805. void disable_local_APIC(void)
  806. {
  807. unsigned int value;
  808. /* APIC hasn't been mapped yet */
  809. if (!x2apic_mode && !apic_phys)
  810. return;
  811. clear_local_APIC();
  812. /*
  813. * Disable APIC (implies clearing of registers
  814. * for 82489DX!).
  815. */
  816. value = apic_read(APIC_SPIV);
  817. value &= ~APIC_SPIV_APIC_ENABLED;
  818. apic_write(APIC_SPIV, value);
  819. #ifdef CONFIG_X86_32
  820. /*
  821. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  822. * restore the disabled state.
  823. */
  824. if (enabled_via_apicbase) {
  825. unsigned int l, h;
  826. rdmsr(MSR_IA32_APICBASE, l, h);
  827. l &= ~MSR_IA32_APICBASE_ENABLE;
  828. wrmsr(MSR_IA32_APICBASE, l, h);
  829. }
  830. #endif
  831. }
  832. /*
  833. * If Linux enabled the LAPIC against the BIOS default disable it down before
  834. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  835. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  836. * for the case where Linux didn't enable the LAPIC.
  837. */
  838. void lapic_shutdown(void)
  839. {
  840. unsigned long flags;
  841. if (!cpu_has_apic && !apic_from_smp_config())
  842. return;
  843. local_irq_save(flags);
  844. #ifdef CONFIG_X86_32
  845. if (!enabled_via_apicbase)
  846. clear_local_APIC();
  847. else
  848. #endif
  849. disable_local_APIC();
  850. local_irq_restore(flags);
  851. }
  852. /*
  853. * This is to verify that we're looking at a real local APIC.
  854. * Check these against your board if the CPUs aren't getting
  855. * started for no apparent reason.
  856. */
  857. int __init verify_local_APIC(void)
  858. {
  859. unsigned int reg0, reg1;
  860. /*
  861. * The version register is read-only in a real APIC.
  862. */
  863. reg0 = apic_read(APIC_LVR);
  864. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  865. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  866. reg1 = apic_read(APIC_LVR);
  867. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  868. /*
  869. * The two version reads above should print the same
  870. * numbers. If the second one is different, then we
  871. * poke at a non-APIC.
  872. */
  873. if (reg1 != reg0)
  874. return 0;
  875. /*
  876. * Check if the version looks reasonably.
  877. */
  878. reg1 = GET_APIC_VERSION(reg0);
  879. if (reg1 == 0x00 || reg1 == 0xff)
  880. return 0;
  881. reg1 = lapic_get_maxlvt();
  882. if (reg1 < 0x02 || reg1 == 0xff)
  883. return 0;
  884. /*
  885. * The ID register is read/write in a real APIC.
  886. */
  887. reg0 = apic_read(APIC_ID);
  888. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  889. apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
  890. reg1 = apic_read(APIC_ID);
  891. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
  892. apic_write(APIC_ID, reg0);
  893. if (reg1 != (reg0 ^ apic->apic_id_mask))
  894. return 0;
  895. /*
  896. * The next two are just to see if we have sane values.
  897. * They're only really relevant if we're in Virtual Wire
  898. * compatibility mode, but most boxes are anymore.
  899. */
  900. reg0 = apic_read(APIC_LVT0);
  901. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  902. reg1 = apic_read(APIC_LVT1);
  903. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  904. return 1;
  905. }
  906. /**
  907. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  908. */
  909. void __init sync_Arb_IDs(void)
  910. {
  911. /*
  912. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  913. * needed on AMD.
  914. */
  915. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  916. return;
  917. /*
  918. * Wait for idle.
  919. */
  920. apic_wait_icr_idle();
  921. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  922. apic_write(APIC_ICR, APIC_DEST_ALLINC |
  923. APIC_INT_LEVELTRIG | APIC_DM_INIT);
  924. }
  925. /*
  926. * An initial setup of the virtual wire mode.
  927. */
  928. void __init init_bsp_APIC(void)
  929. {
  930. unsigned int value;
  931. /*
  932. * Don't do the setup now if we have a SMP BIOS as the
  933. * through-I/O-APIC virtual wire mode might be active.
  934. */
  935. if (smp_found_config || !cpu_has_apic)
  936. return;
  937. /*
  938. * Do not trust the local APIC being empty at bootup.
  939. */
  940. clear_local_APIC();
  941. /*
  942. * Enable APIC.
  943. */
  944. value = apic_read(APIC_SPIV);
  945. value &= ~APIC_VECTOR_MASK;
  946. value |= APIC_SPIV_APIC_ENABLED;
  947. #ifdef CONFIG_X86_32
  948. /* This bit is reserved on P4/Xeon and should be cleared */
  949. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  950. (boot_cpu_data.x86 == 15))
  951. value &= ~APIC_SPIV_FOCUS_DISABLED;
  952. else
  953. #endif
  954. value |= APIC_SPIV_FOCUS_DISABLED;
  955. value |= SPURIOUS_APIC_VECTOR;
  956. apic_write(APIC_SPIV, value);
  957. /*
  958. * Set up the virtual wire mode.
  959. */
  960. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  961. value = APIC_DM_NMI;
  962. if (!lapic_is_integrated()) /* 82489DX */
  963. value |= APIC_LVT_LEVEL_TRIGGER;
  964. apic_write(APIC_LVT1, value);
  965. }
  966. static void __cpuinit lapic_setup_esr(void)
  967. {
  968. unsigned int oldvalue, value, maxlvt;
  969. if (!lapic_is_integrated()) {
  970. pr_info("No ESR for 82489DX.\n");
  971. return;
  972. }
  973. if (apic->disable_esr) {
  974. /*
  975. * Something untraceable is creating bad interrupts on
  976. * secondary quads ... for the moment, just leave the
  977. * ESR disabled - we can't do anything useful with the
  978. * errors anyway - mbligh
  979. */
  980. pr_info("Leaving ESR disabled.\n");
  981. return;
  982. }
  983. maxlvt = lapic_get_maxlvt();
  984. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  985. apic_write(APIC_ESR, 0);
  986. oldvalue = apic_read(APIC_ESR);
  987. /* enables sending errors */
  988. value = ERROR_APIC_VECTOR;
  989. apic_write(APIC_LVTERR, value);
  990. /*
  991. * spec says clear errors after enabling vector.
  992. */
  993. if (maxlvt > 3)
  994. apic_write(APIC_ESR, 0);
  995. value = apic_read(APIC_ESR);
  996. if (value != oldvalue)
  997. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  998. "vector: 0x%08x after: 0x%08x\n",
  999. oldvalue, value);
  1000. }
  1001. /**
  1002. * setup_local_APIC - setup the local APIC
  1003. *
  1004. * Used to setup local APIC while initializing BSP or bringin up APs.
  1005. * Always called with preemption disabled.
  1006. */
  1007. void __cpuinit setup_local_APIC(void)
  1008. {
  1009. int cpu = smp_processor_id();
  1010. unsigned int value, queued;
  1011. int i, j, acked = 0;
  1012. unsigned long long tsc = 0, ntsc;
  1013. long long max_loops = cpu_khz;
  1014. if (cpu_has_tsc)
  1015. rdtscll(tsc);
  1016. if (disable_apic) {
  1017. disable_ioapic_support();
  1018. return;
  1019. }
  1020. #ifdef CONFIG_X86_32
  1021. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  1022. if (lapic_is_integrated() && apic->disable_esr) {
  1023. apic_write(APIC_ESR, 0);
  1024. apic_write(APIC_ESR, 0);
  1025. apic_write(APIC_ESR, 0);
  1026. apic_write(APIC_ESR, 0);
  1027. }
  1028. #endif
  1029. perf_events_lapic_init();
  1030. /*
  1031. * Double-check whether this APIC is really registered.
  1032. * This is meaningless in clustered apic mode, so we skip it.
  1033. */
  1034. BUG_ON(!apic->apic_id_registered());
  1035. /*
  1036. * Intel recommends to set DFR, LDR and TPR before enabling
  1037. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  1038. * document number 292116). So here it goes...
  1039. */
  1040. apic->init_apic_ldr();
  1041. /*
  1042. * Set Task Priority to 'accept all'. We never change this
  1043. * later on.
  1044. */
  1045. value = apic_read(APIC_TASKPRI);
  1046. value &= ~APIC_TPRI_MASK;
  1047. apic_write(APIC_TASKPRI, value);
  1048. /*
  1049. * After a crash, we no longer service the interrupts and a pending
  1050. * interrupt from previous kernel might still have ISR bit set.
  1051. *
  1052. * Most probably by now CPU has serviced that pending interrupt and
  1053. * it might not have done the ack_APIC_irq() because it thought,
  1054. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  1055. * does not clear the ISR bit and cpu thinks it has already serivced
  1056. * the interrupt. Hence a vector might get locked. It was noticed
  1057. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  1058. */
  1059. do {
  1060. queued = 0;
  1061. for (i = APIC_ISR_NR - 1; i >= 0; i--)
  1062. queued |= apic_read(APIC_IRR + i*0x10);
  1063. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  1064. value = apic_read(APIC_ISR + i*0x10);
  1065. for (j = 31; j >= 0; j--) {
  1066. if (value & (1<<j)) {
  1067. ack_APIC_irq();
  1068. acked++;
  1069. }
  1070. }
  1071. }
  1072. if (acked > 256) {
  1073. printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
  1074. acked);
  1075. break;
  1076. }
  1077. if (cpu_has_tsc) {
  1078. rdtscll(ntsc);
  1079. max_loops = (cpu_khz << 10) - (ntsc - tsc);
  1080. } else
  1081. max_loops--;
  1082. } while (queued && max_loops > 0);
  1083. WARN_ON(max_loops <= 0);
  1084. /*
  1085. * Now that we are all set up, enable the APIC
  1086. */
  1087. value = apic_read(APIC_SPIV);
  1088. value &= ~APIC_VECTOR_MASK;
  1089. /*
  1090. * Enable APIC
  1091. */
  1092. value |= APIC_SPIV_APIC_ENABLED;
  1093. #ifdef CONFIG_X86_32
  1094. /*
  1095. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  1096. * certain networking cards. If high frequency interrupts are
  1097. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  1098. * entry is masked/unmasked at a high rate as well then sooner or
  1099. * later IOAPIC line gets 'stuck', no more interrupts are received
  1100. * from the device. If focus CPU is disabled then the hang goes
  1101. * away, oh well :-(
  1102. *
  1103. * [ This bug can be reproduced easily with a level-triggered
  1104. * PCI Ne2000 networking cards and PII/PIII processors, dual
  1105. * BX chipset. ]
  1106. */
  1107. /*
  1108. * Actually disabling the focus CPU check just makes the hang less
  1109. * frequent as it makes the interrupt distributon model be more
  1110. * like LRU than MRU (the short-term load is more even across CPUs).
  1111. * See also the comment in end_level_ioapic_irq(). --macro
  1112. */
  1113. /*
  1114. * - enable focus processor (bit==0)
  1115. * - 64bit mode always use processor focus
  1116. * so no need to set it
  1117. */
  1118. value &= ~APIC_SPIV_FOCUS_DISABLED;
  1119. #endif
  1120. /*
  1121. * Set spurious IRQ vector
  1122. */
  1123. value |= SPURIOUS_APIC_VECTOR;
  1124. apic_write(APIC_SPIV, value);
  1125. /*
  1126. * Set up LVT0, LVT1:
  1127. *
  1128. * set up through-local-APIC on the BP's LINT0. This is not
  1129. * strictly necessary in pure symmetric-IO mode, but sometimes
  1130. * we delegate interrupts to the 8259A.
  1131. */
  1132. /*
  1133. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  1134. */
  1135. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  1136. if (!cpu && (pic_mode || !value)) {
  1137. value = APIC_DM_EXTINT;
  1138. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
  1139. } else {
  1140. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  1141. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
  1142. }
  1143. apic_write(APIC_LVT0, value);
  1144. /*
  1145. * only the BP should see the LINT1 NMI signal, obviously.
  1146. */
  1147. if (!cpu)
  1148. value = APIC_DM_NMI;
  1149. else
  1150. value = APIC_DM_NMI | APIC_LVT_MASKED;
  1151. if (!lapic_is_integrated()) /* 82489DX */
  1152. value |= APIC_LVT_LEVEL_TRIGGER;
  1153. apic_write(APIC_LVT1, value);
  1154. #ifdef CONFIG_X86_MCE_INTEL
  1155. /* Recheck CMCI information after local APIC is up on CPU #0 */
  1156. if (!cpu)
  1157. cmci_recheck();
  1158. #endif
  1159. }
  1160. void __cpuinit end_local_APIC_setup(void)
  1161. {
  1162. lapic_setup_esr();
  1163. #ifdef CONFIG_X86_32
  1164. {
  1165. unsigned int value;
  1166. /* Disable the local apic timer */
  1167. value = apic_read(APIC_LVTT);
  1168. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  1169. apic_write(APIC_LVTT, value);
  1170. }
  1171. #endif
  1172. apic_pm_activate();
  1173. }
  1174. void __init bsp_end_local_APIC_setup(void)
  1175. {
  1176. end_local_APIC_setup();
  1177. /*
  1178. * Now that local APIC setup is completed for BP, configure the fault
  1179. * handling for interrupt remapping.
  1180. */
  1181. if (intr_remapping_enabled)
  1182. enable_drhd_fault_handling();
  1183. }
  1184. #ifdef CONFIG_X86_X2APIC
  1185. void check_x2apic(void)
  1186. {
  1187. if (x2apic_enabled()) {
  1188. pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
  1189. x2apic_preenabled = x2apic_mode = 1;
  1190. }
  1191. }
  1192. void enable_x2apic(void)
  1193. {
  1194. int msr, msr2;
  1195. if (!x2apic_mode)
  1196. return;
  1197. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  1198. if (!(msr & X2APIC_ENABLE)) {
  1199. printk_once(KERN_INFO "Enabling x2apic\n");
  1200. wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
  1201. }
  1202. }
  1203. #endif /* CONFIG_X86_X2APIC */
  1204. int __init enable_IR(void)
  1205. {
  1206. #ifdef CONFIG_INTR_REMAP
  1207. if (!intr_remapping_supported()) {
  1208. pr_debug("intr-remapping not supported\n");
  1209. return 0;
  1210. }
  1211. if (!x2apic_preenabled && skip_ioapic_setup) {
  1212. pr_info("Skipped enabling intr-remap because of skipping "
  1213. "io-apic setup\n");
  1214. return 0;
  1215. }
  1216. if (enable_intr_remapping(x2apic_supported()))
  1217. return 0;
  1218. pr_info("Enabled Interrupt-remapping\n");
  1219. return 1;
  1220. #endif
  1221. return 0;
  1222. }
  1223. void __init enable_IR_x2apic(void)
  1224. {
  1225. unsigned long flags;
  1226. struct IO_APIC_route_entry **ioapic_entries;
  1227. int ret, x2apic_enabled = 0;
  1228. int dmar_table_init_ret;
  1229. dmar_table_init_ret = dmar_table_init();
  1230. if (dmar_table_init_ret && !x2apic_supported())
  1231. return;
  1232. ioapic_entries = alloc_ioapic_entries();
  1233. if (!ioapic_entries) {
  1234. pr_err("Allocate ioapic_entries failed\n");
  1235. goto out;
  1236. }
  1237. ret = save_IO_APIC_setup(ioapic_entries);
  1238. if (ret) {
  1239. pr_info("Saving IO-APIC state failed: %d\n", ret);
  1240. goto out;
  1241. }
  1242. local_irq_save(flags);
  1243. legacy_pic->mask_all();
  1244. mask_IO_APIC_setup(ioapic_entries);
  1245. if (dmar_table_init_ret)
  1246. ret = 0;
  1247. else
  1248. ret = enable_IR();
  1249. if (!ret) {
  1250. /* IR is required if there is APIC ID > 255 even when running
  1251. * under KVM
  1252. */
  1253. if (max_physical_apicid > 255 ||
  1254. !hypervisor_x2apic_available())
  1255. goto nox2apic;
  1256. /*
  1257. * without IR all CPUs can be addressed by IOAPIC/MSI
  1258. * only in physical mode
  1259. */
  1260. x2apic_force_phys();
  1261. }
  1262. x2apic_enabled = 1;
  1263. if (x2apic_supported() && !x2apic_mode) {
  1264. x2apic_mode = 1;
  1265. enable_x2apic();
  1266. pr_info("Enabled x2apic\n");
  1267. }
  1268. nox2apic:
  1269. if (!ret) /* IR enabling failed */
  1270. restore_IO_APIC_setup(ioapic_entries);
  1271. legacy_pic->restore_mask();
  1272. local_irq_restore(flags);
  1273. out:
  1274. if (ioapic_entries)
  1275. free_ioapic_entries(ioapic_entries);
  1276. if (x2apic_enabled)
  1277. return;
  1278. if (x2apic_preenabled)
  1279. panic("x2apic: enabled by BIOS but kernel init failed.");
  1280. else if (cpu_has_x2apic)
  1281. pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
  1282. }
  1283. #ifdef CONFIG_X86_64
  1284. /*
  1285. * Detect and enable local APICs on non-SMP boards.
  1286. * Original code written by Keir Fraser.
  1287. * On AMD64 we trust the BIOS - if it says no APIC it is likely
  1288. * not correctly set up (usually the APIC timer won't work etc.)
  1289. */
  1290. static int __init detect_init_APIC(void)
  1291. {
  1292. if (!cpu_has_apic) {
  1293. pr_info("No local APIC present\n");
  1294. return -1;
  1295. }
  1296. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1297. return 0;
  1298. }
  1299. #else
  1300. static int __init apic_verify(void)
  1301. {
  1302. u32 features, h, l;
  1303. /*
  1304. * The APIC feature bit should now be enabled
  1305. * in `cpuid'
  1306. */
  1307. features = cpuid_edx(1);
  1308. if (!(features & (1 << X86_FEATURE_APIC))) {
  1309. pr_warning("Could not enable APIC!\n");
  1310. return -1;
  1311. }
  1312. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1313. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1314. /* The BIOS may have set up the APIC at some other address */
  1315. rdmsr(MSR_IA32_APICBASE, l, h);
  1316. if (l & MSR_IA32_APICBASE_ENABLE)
  1317. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1318. pr_info("Found and enabled local APIC!\n");
  1319. return 0;
  1320. }
  1321. int __init apic_force_enable(unsigned long addr)
  1322. {
  1323. u32 h, l;
  1324. if (disable_apic)
  1325. return -1;
  1326. /*
  1327. * Some BIOSes disable the local APIC in the APIC_BASE
  1328. * MSR. This can only be done in software for Intel P6 or later
  1329. * and AMD K7 (Model > 1) or later.
  1330. */
  1331. rdmsr(MSR_IA32_APICBASE, l, h);
  1332. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  1333. pr_info("Local APIC disabled by BIOS -- reenabling.\n");
  1334. l &= ~MSR_IA32_APICBASE_BASE;
  1335. l |= MSR_IA32_APICBASE_ENABLE | addr;
  1336. wrmsr(MSR_IA32_APICBASE, l, h);
  1337. enabled_via_apicbase = 1;
  1338. }
  1339. return apic_verify();
  1340. }
  1341. /*
  1342. * Detect and initialize APIC
  1343. */
  1344. static int __init detect_init_APIC(void)
  1345. {
  1346. /* Disabled by kernel option? */
  1347. if (disable_apic)
  1348. return -1;
  1349. switch (boot_cpu_data.x86_vendor) {
  1350. case X86_VENDOR_AMD:
  1351. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  1352. (boot_cpu_data.x86 >= 15))
  1353. break;
  1354. goto no_apic;
  1355. case X86_VENDOR_INTEL:
  1356. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  1357. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  1358. break;
  1359. goto no_apic;
  1360. default:
  1361. goto no_apic;
  1362. }
  1363. if (!cpu_has_apic) {
  1364. /*
  1365. * Over-ride BIOS and try to enable the local APIC only if
  1366. * "lapic" specified.
  1367. */
  1368. if (!force_enable_local_apic) {
  1369. pr_info("Local APIC disabled by BIOS -- "
  1370. "you can enable it with \"lapic\"\n");
  1371. return -1;
  1372. }
  1373. if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
  1374. return -1;
  1375. } else {
  1376. if (apic_verify())
  1377. return -1;
  1378. }
  1379. apic_pm_activate();
  1380. return 0;
  1381. no_apic:
  1382. pr_info("No local APIC present or hardware disabled\n");
  1383. return -1;
  1384. }
  1385. #endif
  1386. /**
  1387. * init_apic_mappings - initialize APIC mappings
  1388. */
  1389. void __init init_apic_mappings(void)
  1390. {
  1391. unsigned int new_apicid;
  1392. if (x2apic_mode) {
  1393. boot_cpu_physical_apicid = read_apic_id();
  1394. return;
  1395. }
  1396. /* If no local APIC can be found return early */
  1397. if (!smp_found_config && detect_init_APIC()) {
  1398. /* lets NOP'ify apic operations */
  1399. pr_info("APIC: disable apic facility\n");
  1400. apic_disable();
  1401. } else {
  1402. apic_phys = mp_lapic_addr;
  1403. /*
  1404. * acpi lapic path already maps that address in
  1405. * acpi_register_lapic_address()
  1406. */
  1407. if (!acpi_lapic && !smp_found_config)
  1408. register_lapic_address(apic_phys);
  1409. }
  1410. /*
  1411. * Fetch the APIC ID of the BSP in case we have a
  1412. * default configuration (or the MP table is broken).
  1413. */
  1414. new_apicid = read_apic_id();
  1415. if (boot_cpu_physical_apicid != new_apicid) {
  1416. boot_cpu_physical_apicid = new_apicid;
  1417. /*
  1418. * yeah -- we lie about apic_version
  1419. * in case if apic was disabled via boot option
  1420. * but it's not a problem for SMP compiled kernel
  1421. * since smp_sanity_check is prepared for such a case
  1422. * and disable smp mode
  1423. */
  1424. apic_version[new_apicid] =
  1425. GET_APIC_VERSION(apic_read(APIC_LVR));
  1426. }
  1427. }
  1428. void __init register_lapic_address(unsigned long address)
  1429. {
  1430. mp_lapic_addr = address;
  1431. if (!x2apic_mode) {
  1432. set_fixmap_nocache(FIX_APIC_BASE, address);
  1433. apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
  1434. APIC_BASE, mp_lapic_addr);
  1435. }
  1436. if (boot_cpu_physical_apicid == -1U) {
  1437. boot_cpu_physical_apicid = read_apic_id();
  1438. apic_version[boot_cpu_physical_apicid] =
  1439. GET_APIC_VERSION(apic_read(APIC_LVR));
  1440. }
  1441. }
  1442. /*
  1443. * This initializes the IO-APIC and APIC hardware if this is
  1444. * a UP kernel.
  1445. */
  1446. int apic_version[MAX_LOCAL_APIC];
  1447. int __init APIC_init_uniprocessor(void)
  1448. {
  1449. if (disable_apic) {
  1450. pr_info("Apic disabled\n");
  1451. return -1;
  1452. }
  1453. #ifdef CONFIG_X86_64
  1454. if (!cpu_has_apic) {
  1455. disable_apic = 1;
  1456. pr_info("Apic disabled by BIOS\n");
  1457. return -1;
  1458. }
  1459. #else
  1460. if (!smp_found_config && !cpu_has_apic)
  1461. return -1;
  1462. /*
  1463. * Complain if the BIOS pretends there is one.
  1464. */
  1465. if (!cpu_has_apic &&
  1466. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1467. pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
  1468. boot_cpu_physical_apicid);
  1469. return -1;
  1470. }
  1471. #endif
  1472. default_setup_apic_routing();
  1473. verify_local_APIC();
  1474. connect_bsp_APIC();
  1475. #ifdef CONFIG_X86_64
  1476. apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
  1477. #else
  1478. /*
  1479. * Hack: In case of kdump, after a crash, kernel might be booting
  1480. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1481. * might be zero if read from MP tables. Get it from LAPIC.
  1482. */
  1483. # ifdef CONFIG_CRASH_DUMP
  1484. boot_cpu_physical_apicid = read_apic_id();
  1485. # endif
  1486. #endif
  1487. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  1488. setup_local_APIC();
  1489. #ifdef CONFIG_X86_IO_APIC
  1490. /*
  1491. * Now enable IO-APICs, actually call clear_IO_APIC
  1492. * We need clear_IO_APIC before enabling error vector
  1493. */
  1494. if (!skip_ioapic_setup && nr_ioapics)
  1495. enable_IO_APIC();
  1496. #endif
  1497. bsp_end_local_APIC_setup();
  1498. #ifdef CONFIG_X86_IO_APIC
  1499. if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
  1500. setup_IO_APIC();
  1501. else {
  1502. nr_ioapics = 0;
  1503. }
  1504. #endif
  1505. x86_init.timers.setup_percpu_clockev();
  1506. return 0;
  1507. }
  1508. /*
  1509. * Local APIC interrupts
  1510. */
  1511. /*
  1512. * This interrupt should _never_ happen with our APIC/SMP architecture
  1513. */
  1514. void smp_spurious_interrupt(struct pt_regs *regs)
  1515. {
  1516. u32 v;
  1517. exit_idle();
  1518. irq_enter();
  1519. /*
  1520. * Check if this really is a spurious interrupt and ACK it
  1521. * if it is a vectored one. Just in case...
  1522. * Spurious interrupts should not be ACKed.
  1523. */
  1524. v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
  1525. if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
  1526. ack_APIC_irq();
  1527. inc_irq_stat(irq_spurious_count);
  1528. /* see sw-dev-man vol 3, chapter 7.4.13.5 */
  1529. pr_info("spurious APIC interrupt on CPU#%d, "
  1530. "should never happen.\n", smp_processor_id());
  1531. irq_exit();
  1532. }
  1533. /*
  1534. * This interrupt should never happen with our APIC/SMP architecture
  1535. */
  1536. void smp_error_interrupt(struct pt_regs *regs)
  1537. {
  1538. u32 v, v1;
  1539. exit_idle();
  1540. irq_enter();
  1541. /* First tickle the hardware, only then report what went on. -- REW */
  1542. v = apic_read(APIC_ESR);
  1543. apic_write(APIC_ESR, 0);
  1544. v1 = apic_read(APIC_ESR);
  1545. ack_APIC_irq();
  1546. atomic_inc(&irq_err_count);
  1547. /*
  1548. * Here is what the APIC error bits mean:
  1549. * 0: Send CS error
  1550. * 1: Receive CS error
  1551. * 2: Send accept error
  1552. * 3: Receive accept error
  1553. * 4: Reserved
  1554. * 5: Send illegal vector
  1555. * 6: Received illegal vector
  1556. * 7: Illegal register address
  1557. */
  1558. pr_debug("APIC error on CPU%d: %02x(%02x)\n",
  1559. smp_processor_id(), v , v1);
  1560. irq_exit();
  1561. }
  1562. /**
  1563. * connect_bsp_APIC - attach the APIC to the interrupt system
  1564. */
  1565. void __init connect_bsp_APIC(void)
  1566. {
  1567. #ifdef CONFIG_X86_32
  1568. if (pic_mode) {
  1569. /*
  1570. * Do not trust the local APIC being empty at bootup.
  1571. */
  1572. clear_local_APIC();
  1573. /*
  1574. * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
  1575. * local APIC to INT and NMI lines.
  1576. */
  1577. apic_printk(APIC_VERBOSE, "leaving PIC mode, "
  1578. "enabling APIC mode.\n");
  1579. imcr_pic_to_apic();
  1580. }
  1581. #endif
  1582. if (apic->enable_apic_mode)
  1583. apic->enable_apic_mode();
  1584. }
  1585. /**
  1586. * disconnect_bsp_APIC - detach the APIC from the interrupt system
  1587. * @virt_wire_setup: indicates, whether virtual wire mode is selected
  1588. *
  1589. * Virtual wire mode is necessary to deliver legacy interrupts even when the
  1590. * APIC is disabled.
  1591. */
  1592. void disconnect_bsp_APIC(int virt_wire_setup)
  1593. {
  1594. unsigned int value;
  1595. #ifdef CONFIG_X86_32
  1596. if (pic_mode) {
  1597. /*
  1598. * Put the board back into PIC mode (has an effect only on
  1599. * certain older boards). Note that APIC interrupts, including
  1600. * IPIs, won't work beyond this point! The only exception are
  1601. * INIT IPIs.
  1602. */
  1603. apic_printk(APIC_VERBOSE, "disabling APIC mode, "
  1604. "entering PIC mode.\n");
  1605. imcr_apic_to_pic();
  1606. return;
  1607. }
  1608. #endif
  1609. /* Go back to Virtual Wire compatibility mode */
  1610. /* For the spurious interrupt use vector F, and enable it */
  1611. value = apic_read(APIC_SPIV);
  1612. value &= ~APIC_VECTOR_MASK;
  1613. value |= APIC_SPIV_APIC_ENABLED;
  1614. value |= 0xf;
  1615. apic_write(APIC_SPIV, value);
  1616. if (!virt_wire_setup) {
  1617. /*
  1618. * For LVT0 make it edge triggered, active high,
  1619. * external and enabled
  1620. */
  1621. value = apic_read(APIC_LVT0);
  1622. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1623. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1624. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1625. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1626. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
  1627. apic_write(APIC_LVT0, value);
  1628. } else {
  1629. /* Disable LVT0 */
  1630. apic_write(APIC_LVT0, APIC_LVT_MASKED);
  1631. }
  1632. /*
  1633. * For LVT1 make it edge triggered, active high,
  1634. * nmi and enabled
  1635. */
  1636. value = apic_read(APIC_LVT1);
  1637. value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
  1638. APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
  1639. APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
  1640. value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
  1641. value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
  1642. apic_write(APIC_LVT1, value);
  1643. }
  1644. void __cpuinit generic_processor_info(int apicid, int version)
  1645. {
  1646. int cpu;
  1647. /*
  1648. * Validate version
  1649. */
  1650. if (version == 0x0) {
  1651. pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
  1652. "fixing up to 0x10. (tell your hw vendor)\n",
  1653. version);
  1654. version = 0x10;
  1655. }
  1656. apic_version[apicid] = version;
  1657. if (num_processors >= nr_cpu_ids) {
  1658. int max = nr_cpu_ids;
  1659. int thiscpu = max + disabled_cpus;
  1660. pr_warning(
  1661. "ACPI: NR_CPUS/possible_cpus limit of %i reached."
  1662. " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
  1663. disabled_cpus++;
  1664. return;
  1665. }
  1666. num_processors++;
  1667. cpu = cpumask_next_zero(-1, cpu_present_mask);
  1668. if (version != apic_version[boot_cpu_physical_apicid])
  1669. WARN_ONCE(1,
  1670. "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
  1671. apic_version[boot_cpu_physical_apicid], cpu, version);
  1672. physid_set(apicid, phys_cpu_present_map);
  1673. if (apicid == boot_cpu_physical_apicid) {
  1674. /*
  1675. * x86_bios_cpu_apicid is required to have processors listed
  1676. * in same order as logical cpu numbers. Hence the first
  1677. * entry is BSP, and so on.
  1678. */
  1679. cpu = 0;
  1680. }
  1681. if (apicid > max_physical_apicid)
  1682. max_physical_apicid = apicid;
  1683. #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
  1684. early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  1685. early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
  1686. #endif
  1687. set_cpu_possible(cpu, true);
  1688. set_cpu_present(cpu, true);
  1689. }
  1690. int hard_smp_processor_id(void)
  1691. {
  1692. return read_apic_id();
  1693. }
  1694. void default_init_apic_ldr(void)
  1695. {
  1696. unsigned long val;
  1697. apic_write(APIC_DFR, APIC_DFR_VALUE);
  1698. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  1699. val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
  1700. apic_write(APIC_LDR, val);
  1701. }
  1702. #ifdef CONFIG_X86_32
  1703. int default_apicid_to_node(int logical_apicid)
  1704. {
  1705. #ifdef CONFIG_SMP
  1706. return apicid_2_node[hard_smp_processor_id()];
  1707. #else
  1708. return 0;
  1709. #endif
  1710. }
  1711. #endif
  1712. /*
  1713. * Power management
  1714. */
  1715. #ifdef CONFIG_PM
  1716. static struct {
  1717. /*
  1718. * 'active' is true if the local APIC was enabled by us and
  1719. * not the BIOS; this signifies that we are also responsible
  1720. * for disabling it before entering apm/acpi suspend
  1721. */
  1722. int active;
  1723. /* r/w apic fields */
  1724. unsigned int apic_id;
  1725. unsigned int apic_taskpri;
  1726. unsigned int apic_ldr;
  1727. unsigned int apic_dfr;
  1728. unsigned int apic_spiv;
  1729. unsigned int apic_lvtt;
  1730. unsigned int apic_lvtpc;
  1731. unsigned int apic_lvt0;
  1732. unsigned int apic_lvt1;
  1733. unsigned int apic_lvterr;
  1734. unsigned int apic_tmict;
  1735. unsigned int apic_tdcr;
  1736. unsigned int apic_thmr;
  1737. } apic_pm_state;
  1738. static int lapic_suspend(struct sys_device *dev, pm_message_t state)
  1739. {
  1740. unsigned long flags;
  1741. int maxlvt;
  1742. if (!apic_pm_state.active)
  1743. return 0;
  1744. maxlvt = lapic_get_maxlvt();
  1745. apic_pm_state.apic_id = apic_read(APIC_ID);
  1746. apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
  1747. apic_pm_state.apic_ldr = apic_read(APIC_LDR);
  1748. apic_pm_state.apic_dfr = apic_read(APIC_DFR);
  1749. apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
  1750. apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
  1751. if (maxlvt >= 4)
  1752. apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
  1753. apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
  1754. apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
  1755. apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
  1756. apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
  1757. apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
  1758. #ifdef CONFIG_X86_THERMAL_VECTOR
  1759. if (maxlvt >= 5)
  1760. apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
  1761. #endif
  1762. local_irq_save(flags);
  1763. disable_local_APIC();
  1764. if (intr_remapping_enabled)
  1765. disable_intr_remapping();
  1766. local_irq_restore(flags);
  1767. return 0;
  1768. }
  1769. static int lapic_resume(struct sys_device *dev)
  1770. {
  1771. unsigned int l, h;
  1772. unsigned long flags;
  1773. int maxlvt;
  1774. int ret = 0;
  1775. struct IO_APIC_route_entry **ioapic_entries = NULL;
  1776. if (!apic_pm_state.active)
  1777. return 0;
  1778. local_irq_save(flags);
  1779. if (intr_remapping_enabled) {
  1780. ioapic_entries = alloc_ioapic_entries();
  1781. if (!ioapic_entries) {
  1782. WARN(1, "Alloc ioapic_entries in lapic resume failed.");
  1783. ret = -ENOMEM;
  1784. goto restore;
  1785. }
  1786. ret = save_IO_APIC_setup(ioapic_entries);
  1787. if (ret) {
  1788. WARN(1, "Saving IO-APIC state failed: %d\n", ret);
  1789. free_ioapic_entries(ioapic_entries);
  1790. goto restore;
  1791. }
  1792. mask_IO_APIC_setup(ioapic_entries);
  1793. legacy_pic->mask_all();
  1794. }
  1795. if (x2apic_mode)
  1796. enable_x2apic();
  1797. else {
  1798. /*
  1799. * Make sure the APICBASE points to the right address
  1800. *
  1801. * FIXME! This will be wrong if we ever support suspend on
  1802. * SMP! We'll need to do this as part of the CPU restore!
  1803. */
  1804. rdmsr(MSR_IA32_APICBASE, l, h);
  1805. l &= ~MSR_IA32_APICBASE_BASE;
  1806. l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
  1807. wrmsr(MSR_IA32_APICBASE, l, h);
  1808. }
  1809. maxlvt = lapic_get_maxlvt();
  1810. apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
  1811. apic_write(APIC_ID, apic_pm_state.apic_id);
  1812. apic_write(APIC_DFR, apic_pm_state.apic_dfr);
  1813. apic_write(APIC_LDR, apic_pm_state.apic_ldr);
  1814. apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
  1815. apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
  1816. apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
  1817. apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
  1818. #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
  1819. if (maxlvt >= 5)
  1820. apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
  1821. #endif
  1822. if (maxlvt >= 4)
  1823. apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
  1824. apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
  1825. apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
  1826. apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
  1827. apic_write(APIC_ESR, 0);
  1828. apic_read(APIC_ESR);
  1829. apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
  1830. apic_write(APIC_ESR, 0);
  1831. apic_read(APIC_ESR);
  1832. if (intr_remapping_enabled) {
  1833. reenable_intr_remapping(x2apic_mode);
  1834. legacy_pic->restore_mask();
  1835. restore_IO_APIC_setup(ioapic_entries);
  1836. free_ioapic_entries(ioapic_entries);
  1837. }
  1838. restore:
  1839. local_irq_restore(flags);
  1840. return ret;
  1841. }
  1842. /*
  1843. * This device has no shutdown method - fully functioning local APICs
  1844. * are needed on every CPU up until machine_halt/restart/poweroff.
  1845. */
  1846. static struct sysdev_class lapic_sysclass = {
  1847. .name = "lapic",
  1848. .resume = lapic_resume,
  1849. .suspend = lapic_suspend,
  1850. };
  1851. static struct sys_device device_lapic = {
  1852. .id = 0,
  1853. .cls = &lapic_sysclass,
  1854. };
  1855. static void __cpuinit apic_pm_activate(void)
  1856. {
  1857. apic_pm_state.active = 1;
  1858. }
  1859. static int __init init_lapic_sysfs(void)
  1860. {
  1861. int error;
  1862. if (!cpu_has_apic)
  1863. return 0;
  1864. /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
  1865. error = sysdev_class_register(&lapic_sysclass);
  1866. if (!error)
  1867. error = sysdev_register(&device_lapic);
  1868. return error;
  1869. }
  1870. /* local apic needs to resume before other devices access its registers. */
  1871. core_initcall(init_lapic_sysfs);
  1872. #else /* CONFIG_PM */
  1873. static void apic_pm_activate(void) { }
  1874. #endif /* CONFIG_PM */
  1875. #ifdef CONFIG_X86_64
  1876. static int __cpuinit apic_cluster_num(void)
  1877. {
  1878. int i, clusters, zeros;
  1879. unsigned id;
  1880. u16 *bios_cpu_apicid;
  1881. DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
  1882. bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
  1883. bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
  1884. for (i = 0; i < nr_cpu_ids; i++) {
  1885. /* are we being called early in kernel startup? */
  1886. if (bios_cpu_apicid) {
  1887. id = bios_cpu_apicid[i];
  1888. } else if (i < nr_cpu_ids) {
  1889. if (cpu_present(i))
  1890. id = per_cpu(x86_bios_cpu_apicid, i);
  1891. else
  1892. continue;
  1893. } else
  1894. break;
  1895. if (id != BAD_APICID)
  1896. __set_bit(APIC_CLUSTERID(id), clustermap);
  1897. }
  1898. /* Problem: Partially populated chassis may not have CPUs in some of
  1899. * the APIC clusters they have been allocated. Only present CPUs have
  1900. * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
  1901. * Since clusters are allocated sequentially, count zeros only if
  1902. * they are bounded by ones.
  1903. */
  1904. clusters = 0;
  1905. zeros = 0;
  1906. for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
  1907. if (test_bit(i, clustermap)) {
  1908. clusters += 1 + zeros;
  1909. zeros = 0;
  1910. } else
  1911. ++zeros;
  1912. }
  1913. return clusters;
  1914. }
  1915. static int __cpuinitdata multi_checked;
  1916. static int __cpuinitdata multi;
  1917. static int __cpuinit set_multi(const struct dmi_system_id *d)
  1918. {
  1919. if (multi)
  1920. return 0;
  1921. pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
  1922. multi = 1;
  1923. return 0;
  1924. }
  1925. static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
  1926. {
  1927. .callback = set_multi,
  1928. .ident = "IBM System Summit2",
  1929. .matches = {
  1930. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  1931. DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
  1932. },
  1933. },
  1934. {}
  1935. };
  1936. static void __cpuinit dmi_check_multi(void)
  1937. {
  1938. if (multi_checked)
  1939. return;
  1940. dmi_check_system(multi_dmi_table);
  1941. multi_checked = 1;
  1942. }
  1943. /*
  1944. * apic_is_clustered_box() -- Check if we can expect good TSC
  1945. *
  1946. * Thus far, the major user of this is IBM's Summit2 series:
  1947. * Clustered boxes may have unsynced TSC problems if they are
  1948. * multi-chassis.
  1949. * Use DMI to check them
  1950. */
  1951. __cpuinit int apic_is_clustered_box(void)
  1952. {
  1953. dmi_check_multi();
  1954. if (multi)
  1955. return 1;
  1956. if (!is_vsmp_box())
  1957. return 0;
  1958. /*
  1959. * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
  1960. * not guaranteed to be synced between boards
  1961. */
  1962. if (apic_cluster_num() > 1)
  1963. return 1;
  1964. return 0;
  1965. }
  1966. #endif
  1967. /*
  1968. * APIC command line parameters
  1969. */
  1970. static int __init setup_disableapic(char *arg)
  1971. {
  1972. disable_apic = 1;
  1973. setup_clear_cpu_cap(X86_FEATURE_APIC);
  1974. return 0;
  1975. }
  1976. early_param("disableapic", setup_disableapic);
  1977. /* same as disableapic, for compatibility */
  1978. static int __init setup_nolapic(char *arg)
  1979. {
  1980. return setup_disableapic(arg);
  1981. }
  1982. early_param("nolapic", setup_nolapic);
  1983. static int __init parse_lapic_timer_c2_ok(char *arg)
  1984. {
  1985. local_apic_timer_c2_ok = 1;
  1986. return 0;
  1987. }
  1988. early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
  1989. static int __init parse_disable_apic_timer(char *arg)
  1990. {
  1991. disable_apic_timer = 1;
  1992. return 0;
  1993. }
  1994. early_param("noapictimer", parse_disable_apic_timer);
  1995. static int __init parse_nolapic_timer(char *arg)
  1996. {
  1997. disable_apic_timer = 1;
  1998. return 0;
  1999. }
  2000. early_param("nolapic_timer", parse_nolapic_timer);
  2001. static int __init apic_set_verbosity(char *arg)
  2002. {
  2003. if (!arg) {
  2004. #ifdef CONFIG_X86_64
  2005. skip_ioapic_setup = 0;
  2006. return 0;
  2007. #endif
  2008. return -EINVAL;
  2009. }
  2010. if (strcmp("debug", arg) == 0)
  2011. apic_verbosity = APIC_DEBUG;
  2012. else if (strcmp("verbose", arg) == 0)
  2013. apic_verbosity = APIC_VERBOSE;
  2014. else {
  2015. pr_warning("APIC Verbosity level %s not recognised"
  2016. " use apic=verbose or apic=debug\n", arg);
  2017. return -EINVAL;
  2018. }
  2019. return 0;
  2020. }
  2021. early_param("apic", apic_set_verbosity);
  2022. static int __init lapic_insert_resource(void)
  2023. {
  2024. if (!apic_phys)
  2025. return -1;
  2026. /* Put local APIC into the resource map. */
  2027. lapic_resource.start = apic_phys;
  2028. lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
  2029. insert_resource(&iomem_resource, &lapic_resource);
  2030. return 0;
  2031. }
  2032. /*
  2033. * need call insert after e820_reserve_resources()
  2034. * that is using request_resource
  2035. */
  2036. late_initcall(lapic_insert_resource);