apic_64.c 37 KB

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