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