apic_32.c 44 KB

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