apic_32.c 43 KB

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