apic.c 57 KB

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