apic_32.c 44 KB

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