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