apic_32.c 51 KB

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