apic.c 56 KB

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