apic_64.c 43 KB

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