apic_64.c 40 KB

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