apic_32.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. /*
  65. * Debug level, exported for io_apic.c
  66. */
  67. int apic_verbosity;
  68. int pic_mode;
  69. /* Have we found an MP table */
  70. int smp_found_config;
  71. static unsigned int calibration_result;
  72. static int lapic_next_event(unsigned long delta,
  73. struct clock_event_device *evt);
  74. static void lapic_timer_setup(enum clock_event_mode mode,
  75. struct clock_event_device *evt);
  76. static void lapic_timer_broadcast(cpumask_t mask);
  77. static void apic_pm_activate(void);
  78. /*
  79. * The local apic timer can be used for any function which is CPU local.
  80. */
  81. static struct clock_event_device lapic_clockevent = {
  82. .name = "lapic",
  83. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
  84. | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
  85. .shift = 32,
  86. .set_mode = lapic_timer_setup,
  87. .set_next_event = lapic_next_event,
  88. .broadcast = lapic_timer_broadcast,
  89. .rating = 100,
  90. .irq = -1,
  91. };
  92. static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
  93. /* Local APIC was disabled by the BIOS and enabled by the kernel */
  94. static int enabled_via_apicbase;
  95. static unsigned long apic_phys;
  96. /*
  97. * Get the LAPIC version
  98. */
  99. static inline int lapic_get_version(void)
  100. {
  101. return GET_APIC_VERSION(apic_read(APIC_LVR));
  102. }
  103. /*
  104. * Check, if the APIC is integrated or a separate chip
  105. */
  106. static inline int lapic_is_integrated(void)
  107. {
  108. return APIC_INTEGRATED(lapic_get_version());
  109. }
  110. /*
  111. * Check, whether this is a modern or a first generation APIC
  112. */
  113. static int modern_apic(void)
  114. {
  115. /* AMD systems use old APIC versions, so check the CPU */
  116. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  117. boot_cpu_data.x86 >= 0xf)
  118. return 1;
  119. return lapic_get_version() >= 0x14;
  120. }
  121. void apic_wait_icr_idle(void)
  122. {
  123. while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
  124. cpu_relax();
  125. }
  126. u32 safe_apic_wait_icr_idle(void)
  127. {
  128. u32 send_status;
  129. int timeout;
  130. timeout = 0;
  131. do {
  132. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  133. if (!send_status)
  134. break;
  135. udelay(100);
  136. } while (timeout++ < 1000);
  137. return send_status;
  138. }
  139. /**
  140. * enable_NMI_through_LVT0 - enable NMI through local vector table 0
  141. */
  142. void __cpuinit enable_NMI_through_LVT0(void)
  143. {
  144. unsigned int v = APIC_DM_NMI;
  145. /* Level triggered for 82489DX */
  146. if (!lapic_is_integrated())
  147. v |= APIC_LVT_LEVEL_TRIGGER;
  148. apic_write_around(APIC_LVT0, v);
  149. }
  150. /**
  151. * get_physical_broadcast - Get number of physical broadcast IDs
  152. */
  153. int get_physical_broadcast(void)
  154. {
  155. return modern_apic() ? 0xff : 0xf;
  156. }
  157. /**
  158. * lapic_get_maxlvt - get the maximum number of local vector table entries
  159. */
  160. int lapic_get_maxlvt(void)
  161. {
  162. unsigned int v = apic_read(APIC_LVR);
  163. /* 82489DXs do not report # of LVT entries. */
  164. return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
  165. }
  166. /*
  167. * Local APIC timer
  168. */
  169. /* Clock divisor is set to 16 */
  170. #define APIC_DIVISOR 16
  171. /*
  172. * This function sets up the local APIC timer, with a timeout of
  173. * 'clocks' APIC bus clock. During calibration we actually call
  174. * this function twice on the boot CPU, once with a bogus timeout
  175. * value, second time for real. The other (noncalibrating) CPUs
  176. * call this function only once, with the real, calibrated value.
  177. *
  178. * We do reads before writes even if unnecessary, to get around the
  179. * P5 APIC double write bug.
  180. */
  181. static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
  182. {
  183. unsigned int lvtt_value, tmp_value;
  184. lvtt_value = LOCAL_TIMER_VECTOR;
  185. if (!oneshot)
  186. lvtt_value |= APIC_LVT_TIMER_PERIODIC;
  187. if (!lapic_is_integrated())
  188. lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
  189. if (!irqen)
  190. lvtt_value |= APIC_LVT_MASKED;
  191. apic_write_around(APIC_LVTT, lvtt_value);
  192. /*
  193. * Divide PICLK by 16
  194. */
  195. tmp_value = apic_read(APIC_TDCR);
  196. apic_write_around(APIC_TDCR, (tmp_value
  197. & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
  198. | APIC_TDR_DIV_16);
  199. if (!oneshot)
  200. apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
  201. }
  202. /*
  203. * Program the next event, relative to now
  204. */
  205. static int lapic_next_event(unsigned long delta,
  206. struct clock_event_device *evt)
  207. {
  208. apic_write_around(APIC_TMICT, delta);
  209. return 0;
  210. }
  211. /*
  212. * Setup the lapic timer in periodic or oneshot mode
  213. */
  214. static void lapic_timer_setup(enum clock_event_mode mode,
  215. struct clock_event_device *evt)
  216. {
  217. unsigned long flags;
  218. unsigned int v;
  219. /* Lapic used for broadcast ? */
  220. if (!local_apic_timer_verify_ok)
  221. return;
  222. local_irq_save(flags);
  223. switch (mode) {
  224. case CLOCK_EVT_MODE_PERIODIC:
  225. case CLOCK_EVT_MODE_ONESHOT:
  226. __setup_APIC_LVTT(calibration_result,
  227. mode != CLOCK_EVT_MODE_PERIODIC, 1);
  228. break;
  229. case CLOCK_EVT_MODE_UNUSED:
  230. case CLOCK_EVT_MODE_SHUTDOWN:
  231. v = apic_read(APIC_LVTT);
  232. v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  233. apic_write_around(APIC_LVTT, v);
  234. break;
  235. case CLOCK_EVT_MODE_RESUME:
  236. /* Nothing to do here */
  237. break;
  238. }
  239. local_irq_restore(flags);
  240. }
  241. /*
  242. * Local APIC timer broadcast function
  243. */
  244. static void lapic_timer_broadcast(cpumask_t mask)
  245. {
  246. #ifdef CONFIG_SMP
  247. send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
  248. #endif
  249. }
  250. /*
  251. * Setup the local APIC timer for this CPU. Copy the initilized values
  252. * of the boot CPU and register the clock event in the framework.
  253. */
  254. static void __devinit setup_APIC_timer(void)
  255. {
  256. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  257. memcpy(levt, &lapic_clockevent, sizeof(*levt));
  258. levt->cpumask = cpumask_of_cpu(smp_processor_id());
  259. clockevents_register_device(levt);
  260. }
  261. /*
  262. * In this functions we calibrate APIC bus clocks to the external timer.
  263. *
  264. * We want to do the calibration only once since we want to have local timer
  265. * irqs syncron. CPUs connected by the same APIC bus have the very same bus
  266. * frequency.
  267. *
  268. * This was previously done by reading the PIT/HPET and waiting for a wrap
  269. * around to find out, that a tick has elapsed. I have a box, where the PIT
  270. * readout is broken, so it never gets out of the wait loop again. This was
  271. * also reported by others.
  272. *
  273. * Monitoring the jiffies value is inaccurate and the clockevents
  274. * infrastructure allows us to do a simple substitution of the interrupt
  275. * handler.
  276. *
  277. * The calibration routine also uses the pm_timer when possible, as the PIT
  278. * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
  279. * back to normal later in the boot process).
  280. */
  281. #define LAPIC_CAL_LOOPS (HZ/10)
  282. static __initdata int lapic_cal_loops = -1;
  283. static __initdata long lapic_cal_t1, lapic_cal_t2;
  284. static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
  285. static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
  286. static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
  287. /*
  288. * Temporary interrupt handler.
  289. */
  290. static void __init lapic_cal_handler(struct clock_event_device *dev)
  291. {
  292. unsigned long long tsc = 0;
  293. long tapic = apic_read(APIC_TMCCT);
  294. unsigned long pm = acpi_pm_read_early();
  295. if (cpu_has_tsc)
  296. rdtscll(tsc);
  297. switch (lapic_cal_loops++) {
  298. case 0:
  299. lapic_cal_t1 = tapic;
  300. lapic_cal_tsc1 = tsc;
  301. lapic_cal_pm1 = pm;
  302. lapic_cal_j1 = jiffies;
  303. break;
  304. case LAPIC_CAL_LOOPS:
  305. lapic_cal_t2 = tapic;
  306. lapic_cal_tsc2 = tsc;
  307. if (pm < lapic_cal_pm1)
  308. pm += ACPI_PM_OVRRUN;
  309. lapic_cal_pm2 = pm;
  310. lapic_cal_j2 = jiffies;
  311. break;
  312. }
  313. }
  314. /*
  315. * Setup the boot APIC
  316. *
  317. * Calibrate and verify the result.
  318. */
  319. void __init setup_boot_APIC_clock(void)
  320. {
  321. struct clock_event_device *levt = &__get_cpu_var(lapic_events);
  322. const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
  323. const long pm_thresh = pm_100ms/100;
  324. void (*real_handler)(struct clock_event_device *dev);
  325. unsigned long deltaj;
  326. long delta, deltapm;
  327. int pm_referenced = 0;
  328. /*
  329. * The local apic timer can be disabled via the kernel
  330. * commandline or from the CPU detection code. Register the lapic
  331. * timer as a dummy clock event source on SMP systems, so the
  332. * broadcast mechanism is used. On UP systems simply ignore it.
  333. */
  334. if (local_apic_timer_disabled) {
  335. /* No broadcast on UP ! */
  336. if (num_possible_cpus() > 1) {
  337. lapic_clockevent.mult = 1;
  338. setup_APIC_timer();
  339. }
  340. return;
  341. }
  342. apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
  343. "calibrating APIC timer ...\n");
  344. local_irq_disable();
  345. /* Replace the global interrupt handler */
  346. real_handler = global_clock_event->event_handler;
  347. global_clock_event->event_handler = lapic_cal_handler;
  348. /*
  349. * Setup the APIC counter to 1e9. There is no way the lapic
  350. * can underflow in the 100ms detection time frame
  351. */
  352. __setup_APIC_LVTT(1000000000, 0, 0);
  353. /* Let the interrupts run */
  354. local_irq_enable();
  355. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  356. cpu_relax();
  357. local_irq_disable();
  358. /* Restore the real event handler */
  359. global_clock_event->event_handler = real_handler;
  360. /* Build delta t1-t2 as apic timer counts down */
  361. delta = lapic_cal_t1 - lapic_cal_t2;
  362. apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
  363. /* Check, if the PM timer is available */
  364. deltapm = lapic_cal_pm2 - lapic_cal_pm1;
  365. apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
  366. if (deltapm) {
  367. unsigned long mult;
  368. u64 res;
  369. mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
  370. if (deltapm > (pm_100ms - pm_thresh) &&
  371. deltapm < (pm_100ms + pm_thresh)) {
  372. apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
  373. } else {
  374. res = (((u64) deltapm) * mult) >> 22;
  375. do_div(res, 1000000);
  376. printk(KERN_WARNING "APIC calibration not consistent "
  377. "with PM Timer: %ldms instead of 100ms\n",
  378. (long)res);
  379. /* Correct the lapic counter value */
  380. res = (((u64) delta) * pm_100ms);
  381. do_div(res, deltapm);
  382. printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
  383. "%lu (%ld)\n", (unsigned long) res, delta);
  384. delta = (long) res;
  385. }
  386. pm_referenced = 1;
  387. }
  388. /* Calculate the scaled math multiplication factor */
  389. lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
  390. lapic_clockevent.shift);
  391. lapic_clockevent.max_delta_ns =
  392. clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
  393. lapic_clockevent.min_delta_ns =
  394. clockevent_delta2ns(0xF, &lapic_clockevent);
  395. calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
  396. apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
  397. apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
  398. apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
  399. calibration_result);
  400. if (cpu_has_tsc) {
  401. delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
  402. apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
  403. "%ld.%04ld MHz.\n",
  404. (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
  405. (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
  406. }
  407. apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
  408. "%u.%04u MHz.\n",
  409. calibration_result / (1000000 / HZ),
  410. calibration_result % (1000000 / HZ));
  411. local_apic_timer_verify_ok = 1;
  412. /*
  413. * Do a sanity check on the APIC calibration result
  414. */
  415. if (calibration_result < (1000000 / HZ)) {
  416. local_irq_enable();
  417. printk(KERN_WARNING
  418. "APIC frequency too slow, disabling apic timer\n");
  419. /* No broadcast on UP ! */
  420. if (num_possible_cpus() > 1)
  421. setup_APIC_timer();
  422. return;
  423. }
  424. /* We trust the pm timer based calibration */
  425. if (!pm_referenced) {
  426. apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
  427. /*
  428. * Setup the apic timer manually
  429. */
  430. levt->event_handler = lapic_cal_handler;
  431. lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
  432. lapic_cal_loops = -1;
  433. /* Let the interrupts run */
  434. local_irq_enable();
  435. while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
  436. cpu_relax();
  437. local_irq_disable();
  438. /* Stop the lapic timer */
  439. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
  440. local_irq_enable();
  441. /* Jiffies delta */
  442. deltaj = lapic_cal_j2 - lapic_cal_j1;
  443. apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
  444. /* Check, if the jiffies result is consistent */
  445. if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
  446. apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
  447. else
  448. local_apic_timer_verify_ok = 0;
  449. } else
  450. local_irq_enable();
  451. if (!local_apic_timer_verify_ok) {
  452. printk(KERN_WARNING
  453. "APIC timer disabled due to verification failure.\n");
  454. /* No broadcast on UP ! */
  455. if (num_possible_cpus() == 1)
  456. return;
  457. } else {
  458. /*
  459. * If nmi_watchdog is set to IO_APIC, we need the
  460. * PIT/HPET going. Otherwise register lapic as a dummy
  461. * device.
  462. */
  463. if (nmi_watchdog != NMI_IO_APIC)
  464. lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
  465. else
  466. printk(KERN_WARNING "APIC timer registered as dummy,"
  467. " due to nmi_watchdog=1!\n");
  468. }
  469. /* Setup the lapic or request the broadcast */
  470. setup_APIC_timer();
  471. }
  472. void __devinit setup_secondary_APIC_clock(void)
  473. {
  474. setup_APIC_timer();
  475. }
  476. /*
  477. * The guts of the apic timer interrupt
  478. */
  479. static void local_apic_timer_interrupt(void)
  480. {
  481. int cpu = smp_processor_id();
  482. struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
  483. /*
  484. * Normally we should not be here till LAPIC has been initialized but
  485. * in some cases like kdump, its possible that there is a pending LAPIC
  486. * timer interrupt from previous kernel's context and is delivered in
  487. * new kernel the moment interrupts are enabled.
  488. *
  489. * Interrupts are enabled early and LAPIC is setup much later, hence
  490. * its possible that when we get here evt->event_handler is NULL.
  491. * Check for event_handler being NULL and discard the interrupt as
  492. * spurious.
  493. */
  494. if (!evt->event_handler) {
  495. printk(KERN_WARNING
  496. "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
  497. /* Switch it off */
  498. lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
  499. return;
  500. }
  501. /*
  502. * the NMI deadlock-detector uses this.
  503. */
  504. per_cpu(irq_stat, cpu).apic_timer_irqs++;
  505. evt->event_handler(evt);
  506. }
  507. /*
  508. * Local APIC timer interrupt. This is the most natural way for doing
  509. * local interrupts, but local timer interrupts can be emulated by
  510. * broadcast interrupts too. [in case the hw doesn't support APIC timers]
  511. *
  512. * [ if a single-CPU system runs an SMP kernel then we call the local
  513. * interrupt as well. Thus we cannot inline the local irq ... ]
  514. */
  515. void smp_apic_timer_interrupt(struct pt_regs *regs)
  516. {
  517. struct pt_regs *old_regs = set_irq_regs(regs);
  518. /*
  519. * NOTE! We'd better ACK the irq immediately,
  520. * because timer handling can be slow.
  521. */
  522. ack_APIC_irq();
  523. /*
  524. * update_process_times() expects us to have done irq_enter().
  525. * Besides, if we don't timer interrupts ignore the global
  526. * interrupt lock, which is the WrongThing (tm) to do.
  527. */
  528. irq_enter();
  529. local_apic_timer_interrupt();
  530. irq_exit();
  531. set_irq_regs(old_regs);
  532. }
  533. int setup_profiling_timer(unsigned int multiplier)
  534. {
  535. return -EINVAL;
  536. }
  537. /*
  538. * Setup extended LVT, AMD specific (K8, family 10h)
  539. *
  540. * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
  541. * MCE interrupts are supported. Thus MCE offset must be set to 0.
  542. */
  543. #define APIC_EILVT_LVTOFF_MCE 0
  544. #define APIC_EILVT_LVTOFF_IBS 1
  545. static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
  546. {
  547. unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
  548. unsigned int v = (mask << 16) | (msg_type << 8) | vector;
  549. apic_write(reg, v);
  550. }
  551. u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
  552. {
  553. setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
  554. return APIC_EILVT_LVTOFF_MCE;
  555. }
  556. u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
  557. {
  558. setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
  559. return APIC_EILVT_LVTOFF_IBS;
  560. }
  561. /*
  562. * Local APIC start and shutdown
  563. */
  564. /**
  565. * clear_local_APIC - shutdown the local APIC
  566. *
  567. * This is called, when a CPU is disabled and before rebooting, so the state of
  568. * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
  569. * leftovers during boot.
  570. */
  571. void clear_local_APIC(void)
  572. {
  573. int maxlvt;
  574. u32 v;
  575. /* APIC hasn't been mapped yet */
  576. if (!apic_phys)
  577. return;
  578. maxlvt = lapic_get_maxlvt();
  579. /*
  580. * Masking an LVT entry can trigger a local APIC error
  581. * if the vector is zero. Mask LVTERR first to prevent this.
  582. */
  583. if (maxlvt >= 3) {
  584. v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
  585. apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
  586. }
  587. /*
  588. * Careful: we have to set masks only first to deassert
  589. * any level-triggered sources.
  590. */
  591. v = apic_read(APIC_LVTT);
  592. apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
  593. v = apic_read(APIC_LVT0);
  594. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  595. v = apic_read(APIC_LVT1);
  596. apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
  597. if (maxlvt >= 4) {
  598. v = apic_read(APIC_LVTPC);
  599. apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
  600. }
  601. /* lets not touch this if we didn't frob it */
  602. #ifdef CONFIG_X86_MCE_P4THERMAL
  603. if (maxlvt >= 5) {
  604. v = apic_read(APIC_LVTTHMR);
  605. apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
  606. }
  607. #endif
  608. /*
  609. * Clean APIC state for other OSs:
  610. */
  611. apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
  612. apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
  613. apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
  614. if (maxlvt >= 3)
  615. apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
  616. if (maxlvt >= 4)
  617. apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
  618. #ifdef CONFIG_X86_MCE_P4THERMAL
  619. if (maxlvt >= 5)
  620. apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
  621. #endif
  622. /* Integrated APIC (!82489DX) ? */
  623. if (lapic_is_integrated()) {
  624. if (maxlvt > 3)
  625. /* Clear ESR due to Pentium errata 3AP and 11AP */
  626. apic_write(APIC_ESR, 0);
  627. apic_read(APIC_ESR);
  628. }
  629. }
  630. /**
  631. * disable_local_APIC - clear and disable the local APIC
  632. */
  633. void disable_local_APIC(void)
  634. {
  635. unsigned long value;
  636. clear_local_APIC();
  637. /*
  638. * Disable APIC (implies clearing of registers
  639. * for 82489DX!).
  640. */
  641. value = apic_read(APIC_SPIV);
  642. value &= ~APIC_SPIV_APIC_ENABLED;
  643. apic_write_around(APIC_SPIV, value);
  644. /*
  645. * When LAPIC was disabled by the BIOS and enabled by the kernel,
  646. * restore the disabled state.
  647. */
  648. if (enabled_via_apicbase) {
  649. unsigned int l, h;
  650. rdmsr(MSR_IA32_APICBASE, l, h);
  651. l &= ~MSR_IA32_APICBASE_ENABLE;
  652. wrmsr(MSR_IA32_APICBASE, l, h);
  653. }
  654. }
  655. /*
  656. * If Linux enabled the LAPIC against the BIOS default disable it down before
  657. * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
  658. * not power-off. Additionally clear all LVT entries before disable_local_APIC
  659. * for the case where Linux didn't enable the LAPIC.
  660. */
  661. void lapic_shutdown(void)
  662. {
  663. unsigned long flags;
  664. if (!cpu_has_apic)
  665. return;
  666. local_irq_save(flags);
  667. clear_local_APIC();
  668. if (enabled_via_apicbase)
  669. disable_local_APIC();
  670. local_irq_restore(flags);
  671. }
  672. /*
  673. * This is to verify that we're looking at a real local APIC.
  674. * Check these against your board if the CPUs aren't getting
  675. * started for no apparent reason.
  676. */
  677. int __init verify_local_APIC(void)
  678. {
  679. unsigned int reg0, reg1;
  680. /*
  681. * The version register is read-only in a real APIC.
  682. */
  683. reg0 = apic_read(APIC_LVR);
  684. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
  685. apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
  686. reg1 = apic_read(APIC_LVR);
  687. apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
  688. /*
  689. * The two version reads above should print the same
  690. * numbers. If the second one is different, then we
  691. * poke at a non-APIC.
  692. */
  693. if (reg1 != reg0)
  694. return 0;
  695. /*
  696. * Check if the version looks reasonably.
  697. */
  698. reg1 = GET_APIC_VERSION(reg0);
  699. if (reg1 == 0x00 || reg1 == 0xff)
  700. return 0;
  701. reg1 = lapic_get_maxlvt();
  702. if (reg1 < 0x02 || reg1 == 0xff)
  703. return 0;
  704. /*
  705. * The ID register is read/write in a real APIC.
  706. */
  707. reg0 = apic_read(APIC_ID);
  708. apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
  709. /*
  710. * The next two are just to see if we have sane values.
  711. * They're only really relevant if we're in Virtual Wire
  712. * compatibility mode, but most boxes are anymore.
  713. */
  714. reg0 = apic_read(APIC_LVT0);
  715. apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
  716. reg1 = apic_read(APIC_LVT1);
  717. apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
  718. return 1;
  719. }
  720. /**
  721. * sync_Arb_IDs - synchronize APIC bus arbitration IDs
  722. */
  723. void __init sync_Arb_IDs(void)
  724. {
  725. /*
  726. * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
  727. * needed on AMD.
  728. */
  729. if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  730. return;
  731. /*
  732. * Wait for idle.
  733. */
  734. apic_wait_icr_idle();
  735. apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
  736. apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
  737. | APIC_DM_INIT);
  738. }
  739. /*
  740. * An initial setup of the virtual wire mode.
  741. */
  742. void __init init_bsp_APIC(void)
  743. {
  744. unsigned long value;
  745. /*
  746. * Don't do the setup now if we have a SMP BIOS as the
  747. * through-I/O-APIC virtual wire mode might be active.
  748. */
  749. if (smp_found_config || !cpu_has_apic)
  750. return;
  751. /*
  752. * Do not trust the local APIC being empty at bootup.
  753. */
  754. clear_local_APIC();
  755. /*
  756. * Enable APIC.
  757. */
  758. value = apic_read(APIC_SPIV);
  759. value &= ~APIC_VECTOR_MASK;
  760. value |= APIC_SPIV_APIC_ENABLED;
  761. /* This bit is reserved on P4/Xeon and should be cleared */
  762. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  763. (boot_cpu_data.x86 == 15))
  764. value &= ~APIC_SPIV_FOCUS_DISABLED;
  765. else
  766. value |= APIC_SPIV_FOCUS_DISABLED;
  767. value |= SPURIOUS_APIC_VECTOR;
  768. apic_write_around(APIC_SPIV, value);
  769. /*
  770. * Set up the virtual wire mode.
  771. */
  772. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  773. value = APIC_DM_NMI;
  774. if (!lapic_is_integrated()) /* 82489DX */
  775. value |= APIC_LVT_LEVEL_TRIGGER;
  776. apic_write_around(APIC_LVT1, value);
  777. }
  778. static void __cpuinit lapic_setup_esr(void)
  779. {
  780. unsigned long oldvalue, value, maxlvt;
  781. if (lapic_is_integrated() && !esr_disable) {
  782. /* !82489DX */
  783. maxlvt = lapic_get_maxlvt();
  784. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  785. apic_write(APIC_ESR, 0);
  786. oldvalue = apic_read(APIC_ESR);
  787. /* enables sending errors */
  788. value = ERROR_APIC_VECTOR;
  789. apic_write_around(APIC_LVTERR, value);
  790. /*
  791. * spec says clear errors after enabling vector.
  792. */
  793. if (maxlvt > 3)
  794. apic_write(APIC_ESR, 0);
  795. value = apic_read(APIC_ESR);
  796. if (value != oldvalue)
  797. apic_printk(APIC_VERBOSE, "ESR value before enabling "
  798. "vector: 0x%08lx after: 0x%08lx\n",
  799. oldvalue, value);
  800. } else {
  801. if (esr_disable)
  802. /*
  803. * Something untraceable is creating bad interrupts on
  804. * secondary quads ... for the moment, just leave the
  805. * ESR disabled - we can't do anything useful with the
  806. * errors anyway - mbligh
  807. */
  808. printk(KERN_INFO "Leaving ESR disabled.\n");
  809. else
  810. printk(KERN_INFO "No ESR for 82489DX.\n");
  811. }
  812. }
  813. /**
  814. * setup_local_APIC - setup the local APIC
  815. */
  816. void __cpuinit setup_local_APIC(void)
  817. {
  818. unsigned long value, integrated;
  819. int i, j;
  820. /* Pound the ESR really hard over the head with a big hammer - mbligh */
  821. if (esr_disable) {
  822. apic_write(APIC_ESR, 0);
  823. apic_write(APIC_ESR, 0);
  824. apic_write(APIC_ESR, 0);
  825. apic_write(APIC_ESR, 0);
  826. }
  827. integrated = lapic_is_integrated();
  828. /*
  829. * Double-check whether this APIC is really registered.
  830. */
  831. if (!apic_id_registered())
  832. BUG();
  833. /*
  834. * Intel recommends to set DFR, LDR and TPR before enabling
  835. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  836. * document number 292116). So here it goes...
  837. */
  838. init_apic_ldr();
  839. /*
  840. * Set Task Priority to 'accept all'. We never change this
  841. * later on.
  842. */
  843. value = apic_read(APIC_TASKPRI);
  844. value &= ~APIC_TPRI_MASK;
  845. apic_write_around(APIC_TASKPRI, value);
  846. /*
  847. * After a crash, we no longer service the interrupts and a pending
  848. * interrupt from previous kernel might still have ISR bit set.
  849. *
  850. * Most probably by now CPU has serviced that pending interrupt and
  851. * it might not have done the ack_APIC_irq() because it thought,
  852. * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
  853. * does not clear the ISR bit and cpu thinks it has already serivced
  854. * the interrupt. Hence a vector might get locked. It was noticed
  855. * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
  856. */
  857. for (i = APIC_ISR_NR - 1; i >= 0; i--) {
  858. value = apic_read(APIC_ISR + i*0x10);
  859. for (j = 31; j >= 0; j--) {
  860. if (value & (1<<j))
  861. ack_APIC_irq();
  862. }
  863. }
  864. /*
  865. * Now that we are all set up, enable the APIC
  866. */
  867. value = apic_read(APIC_SPIV);
  868. value &= ~APIC_VECTOR_MASK;
  869. /*
  870. * Enable APIC
  871. */
  872. value |= APIC_SPIV_APIC_ENABLED;
  873. /*
  874. * Some unknown Intel IO/APIC (or APIC) errata is biting us with
  875. * certain networking cards. If high frequency interrupts are
  876. * happening on a particular IOAPIC pin, plus the IOAPIC routing
  877. * entry is masked/unmasked at a high rate as well then sooner or
  878. * later IOAPIC line gets 'stuck', no more interrupts are received
  879. * from the device. If focus CPU is disabled then the hang goes
  880. * away, oh well :-(
  881. *
  882. * [ This bug can be reproduced easily with a level-triggered
  883. * PCI Ne2000 networking cards and PII/PIII processors, dual
  884. * BX chipset. ]
  885. */
  886. /*
  887. * Actually disabling the focus CPU check just makes the hang less
  888. * frequent as it makes the interrupt distributon model be more
  889. * like LRU than MRU (the short-term load is more even across CPUs).
  890. * See also the comment in end_level_ioapic_irq(). --macro
  891. */
  892. /* Enable focus processor (bit==0) */
  893. value &= ~APIC_SPIV_FOCUS_DISABLED;
  894. /*
  895. * Set spurious IRQ vector
  896. */
  897. value |= SPURIOUS_APIC_VECTOR;
  898. apic_write_around(APIC_SPIV, value);
  899. /*
  900. * Set up LVT0, LVT1:
  901. *
  902. * set up through-local-APIC on the BP's LINT0. This is not
  903. * strictly necessary in pure symmetric-IO mode, but sometimes
  904. * we delegate interrupts to the 8259A.
  905. */
  906. /*
  907. * TODO: set up through-local-APIC from through-I/O-APIC? --macro
  908. */
  909. value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
  910. if (!smp_processor_id() && (pic_mode || !value)) {
  911. value = APIC_DM_EXTINT;
  912. apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
  913. smp_processor_id());
  914. } else {
  915. value = APIC_DM_EXTINT | APIC_LVT_MASKED;
  916. apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
  917. smp_processor_id());
  918. }
  919. apic_write_around(APIC_LVT0, value);
  920. /*
  921. * only the BP should see the LINT1 NMI signal, obviously.
  922. */
  923. if (!smp_processor_id())
  924. value = APIC_DM_NMI;
  925. else
  926. value = APIC_DM_NMI | APIC_LVT_MASKED;
  927. if (!integrated) /* 82489DX */
  928. value |= APIC_LVT_LEVEL_TRIGGER;
  929. apic_write_around(APIC_LVT1, value);
  930. }
  931. void __cpuinit end_local_APIC_setup(void)
  932. {
  933. unsigned long value;
  934. lapic_setup_esr();
  935. /* Disable the local apic timer */
  936. value = apic_read(APIC_LVTT);
  937. value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
  938. apic_write_around(APIC_LVTT, value);
  939. setup_apic_nmi_watchdog(NULL);
  940. apic_pm_activate();
  941. }
  942. /*
  943. * Detect and initialize APIC
  944. */
  945. static int __init detect_init_APIC(void)
  946. {
  947. u32 h, l, features;
  948. /* Disabled by kernel option? */
  949. if (enable_local_apic < 0)
  950. return -1;
  951. switch (boot_cpu_data.x86_vendor) {
  952. case X86_VENDOR_AMD:
  953. if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
  954. (boot_cpu_data.x86 == 15))
  955. break;
  956. goto no_apic;
  957. case X86_VENDOR_INTEL:
  958. if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
  959. (boot_cpu_data.x86 == 5 && cpu_has_apic))
  960. break;
  961. goto no_apic;
  962. default:
  963. goto no_apic;
  964. }
  965. if (!cpu_has_apic) {
  966. /*
  967. * Over-ride BIOS and try to enable the local APIC only if
  968. * "lapic" specified.
  969. */
  970. if (enable_local_apic <= 0) {
  971. printk(KERN_INFO "Local APIC disabled by BIOS -- "
  972. "you can enable it with \"lapic\"\n");
  973. return -1;
  974. }
  975. /*
  976. * Some BIOSes disable the local APIC in the APIC_BASE
  977. * MSR. This can only be done in software for Intel P6 or later
  978. * and AMD K7 (Model > 1) or later.
  979. */
  980. rdmsr(MSR_IA32_APICBASE, l, h);
  981. if (!(l & MSR_IA32_APICBASE_ENABLE)) {
  982. printk(KERN_INFO
  983. "Local APIC disabled by BIOS -- reenabling.\n");
  984. l &= ~MSR_IA32_APICBASE_BASE;
  985. l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
  986. wrmsr(MSR_IA32_APICBASE, l, h);
  987. enabled_via_apicbase = 1;
  988. }
  989. }
  990. /*
  991. * The APIC feature bit should now be enabled
  992. * in `cpuid'
  993. */
  994. features = cpuid_edx(1);
  995. if (!(features & (1 << X86_FEATURE_APIC))) {
  996. printk(KERN_WARNING "Could not enable APIC!\n");
  997. return -1;
  998. }
  999. set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1000. mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
  1001. /* The BIOS may have set up the APIC at some other address */
  1002. rdmsr(MSR_IA32_APICBASE, l, h);
  1003. if (l & MSR_IA32_APICBASE_ENABLE)
  1004. mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
  1005. if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
  1006. nmi_watchdog = NMI_LOCAL_APIC;
  1007. printk(KERN_INFO "Found and enabled local APIC!\n");
  1008. apic_pm_activate();
  1009. return 0;
  1010. no_apic:
  1011. printk(KERN_INFO "No local APIC present or hardware disabled\n");
  1012. return -1;
  1013. }
  1014. /**
  1015. * init_apic_mappings - initialize APIC mappings
  1016. */
  1017. void __init init_apic_mappings(void)
  1018. {
  1019. /*
  1020. * If no local APIC can be found then set up a fake all
  1021. * zeroes page to simulate the local APIC and another
  1022. * one for the IO-APIC.
  1023. */
  1024. if (!smp_found_config && detect_init_APIC()) {
  1025. apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  1026. apic_phys = __pa(apic_phys);
  1027. } else
  1028. apic_phys = mp_lapic_addr;
  1029. set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
  1030. printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
  1031. apic_phys);
  1032. /*
  1033. * Fetch the APIC ID of the BSP in case we have a
  1034. * default configuration (or the MP table is broken).
  1035. */
  1036. if (boot_cpu_physical_apicid == -1U)
  1037. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  1038. #ifdef CONFIG_X86_IO_APIC
  1039. {
  1040. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  1041. int i;
  1042. for (i = 0; i < nr_ioapics; i++) {
  1043. if (smp_found_config) {
  1044. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  1045. if (!ioapic_phys) {
  1046. printk(KERN_ERR
  1047. "WARNING: bogus zero IO-APIC "
  1048. "address found in MPTABLE, "
  1049. "disabling IO/APIC support!\n");
  1050. smp_found_config = 0;
  1051. skip_ioapic_setup = 1;
  1052. goto fake_ioapic_page;
  1053. }
  1054. } else {
  1055. fake_ioapic_page:
  1056. ioapic_phys = (unsigned long)
  1057. alloc_bootmem_pages(PAGE_SIZE);
  1058. ioapic_phys = __pa(ioapic_phys);
  1059. }
  1060. set_fixmap_nocache(idx, ioapic_phys);
  1061. printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
  1062. __fix_to_virt(idx), ioapic_phys);
  1063. idx++;
  1064. }
  1065. }
  1066. #endif
  1067. }
  1068. /*
  1069. * This initializes the IO-APIC and APIC hardware if this is
  1070. * a UP kernel.
  1071. */
  1072. int apic_version[MAX_APICS];
  1073. int __init APIC_init_uniprocessor(void)
  1074. {
  1075. if (enable_local_apic < 0)
  1076. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1077. if (!smp_found_config && !cpu_has_apic)
  1078. return -1;
  1079. /*
  1080. * Complain if the BIOS pretends there is one.
  1081. */
  1082. if (!cpu_has_apic &&
  1083. APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
  1084. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1085. boot_cpu_physical_apicid);
  1086. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
  1087. return -1;
  1088. }
  1089. verify_local_APIC();
  1090. connect_bsp_APIC();
  1091. /*
  1092. * Hack: In case of kdump, after a crash, kernel might be booting
  1093. * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
  1094. * might be zero if read from MP tables. Get it from LAPIC.
  1095. */
  1096. #ifdef CONFIG_CRASH_DUMP
  1097. boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
  1098. #endif
  1099. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
  1100. setup_local_APIC();
  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. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  1174. /* IPI for invalidation */
  1175. set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
  1176. /* IPI for generic function call */
  1177. set_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. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  1190. /* IPI vectors for APIC spurious and error interrupts */
  1191. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  1192. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  1193. /* thermal monitor LVT interrupt */
  1194. #ifdef CONFIG_X86_MCE_P4THERMAL
  1195. set_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);