apic_64.c 45 KB

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