apic_64.c 48 KB

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