apic.c 54 KB

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