apic_64.c 43 KB

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