apic_64.c 43 KB

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