apic_64.c 38 KB

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