apic_32.c 49 KB

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