apic.c 54 KB

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