apic_32.c 43 KB

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