apic_32.c 43 KB

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