apic_64.c 43 KB

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