apic.c 54 KB

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