apic.c 55 KB

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