apic_32.c 43 KB

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