apic_64.c 37 KB

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