io_apic.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. /*
  2. * Intel IO-APIC support for multi-Pentium hosts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
  5. *
  6. * Many thanks to Stig Venaas for trying out countless experimental
  7. * patches and reporting/debugging problems patiently!
  8. *
  9. * (c) 1999, Multiple IO-APIC support, developed by
  10. * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
  11. * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
  12. * further tested and cleaned up by Zach Brown <zab@redhat.com>
  13. * and Ingo Molnar <mingo@redhat.com>
  14. *
  15. * Fixes
  16. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  17. * thanks to Eric Gilmore
  18. * and Rolf G. Tews
  19. * for testing these extensively
  20. * Paul Diefenbaugh : Added full ACPI support
  21. */
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/config.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <linux/acpi.h>
  31. #include <linux/sysdev.h>
  32. #ifdef CONFIG_ACPI
  33. #include <acpi/acpi_bus.h>
  34. #endif
  35. #include <asm/io.h>
  36. #include <asm/smp.h>
  37. #include <asm/desc.h>
  38. #include <asm/proto.h>
  39. #include <asm/mach_apic.h>
  40. #include <asm/acpi.h>
  41. #include <asm/dma.h>
  42. #define __apicdebuginit __init
  43. int sis_apic_bug; /* not actually supported, dummy for compile */
  44. static int no_timer_check;
  45. int disable_timer_pin_1 __initdata;
  46. int timer_over_8254 __initdata = 0;
  47. /* Where if anywhere is the i8259 connect in external int mode */
  48. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  49. static DEFINE_SPINLOCK(ioapic_lock);
  50. /*
  51. * # of IRQ routing registers
  52. */
  53. int nr_ioapic_registers[MAX_IO_APICS];
  54. /*
  55. * Rough estimation of how many shared IRQs there are, can
  56. * be changed anytime.
  57. */
  58. #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
  59. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  60. /*
  61. * This is performance-critical, we want to do it O(1)
  62. *
  63. * the indexing order of this array favors 1:1 mappings
  64. * between pins and IRQs.
  65. */
  66. static struct irq_pin_list {
  67. short apic, pin, next;
  68. } irq_2_pin[PIN_MAP_SIZE];
  69. int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
  70. #ifdef CONFIG_PCI_MSI
  71. #define vector_to_irq(vector) \
  72. (platform_legacy_irq(vector) ? vector : vector_irq[vector])
  73. #else
  74. #define vector_to_irq(vector) (vector)
  75. #endif
  76. #define __DO_ACTION(R, ACTION, FINAL) \
  77. \
  78. { \
  79. int pin; \
  80. struct irq_pin_list *entry = irq_2_pin + irq; \
  81. \
  82. BUG_ON(irq >= NR_IRQS); \
  83. for (;;) { \
  84. unsigned int reg; \
  85. pin = entry->pin; \
  86. if (pin == -1) \
  87. break; \
  88. reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
  89. reg ACTION; \
  90. io_apic_modify(entry->apic, reg); \
  91. if (!entry->next) \
  92. break; \
  93. entry = irq_2_pin + entry->next; \
  94. } \
  95. FINAL; \
  96. }
  97. #ifdef CONFIG_SMP
  98. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  99. {
  100. unsigned long flags;
  101. unsigned int dest;
  102. cpumask_t tmp;
  103. cpus_and(tmp, mask, cpu_online_map);
  104. if (cpus_empty(tmp))
  105. tmp = TARGET_CPUS;
  106. cpus_and(mask, tmp, CPU_MASK_ALL);
  107. dest = cpu_mask_to_apicid(mask);
  108. /*
  109. * Only the high 8 bits are valid.
  110. */
  111. dest = SET_APIC_LOGICAL_ID(dest);
  112. spin_lock_irqsave(&ioapic_lock, flags);
  113. __DO_ACTION(1, = dest, )
  114. set_irq_info(irq, mask);
  115. spin_unlock_irqrestore(&ioapic_lock, flags);
  116. }
  117. #endif
  118. static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
  119. /*
  120. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  121. * shared ISA-space IRQs, so we have to support them. We are super
  122. * fast in the common case, and fast for shared ISA-space IRQs.
  123. */
  124. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  125. {
  126. static int first_free_entry = NR_IRQS;
  127. struct irq_pin_list *entry = irq_2_pin + irq;
  128. BUG_ON(irq >= NR_IRQS);
  129. while (entry->next)
  130. entry = irq_2_pin + entry->next;
  131. if (entry->pin != -1) {
  132. entry->next = first_free_entry;
  133. entry = irq_2_pin + entry->next;
  134. if (++first_free_entry >= PIN_MAP_SIZE)
  135. panic("io_apic.c: ran out of irq_2_pin entries!");
  136. }
  137. entry->apic = apic;
  138. entry->pin = pin;
  139. }
  140. #define DO_ACTION(name,R,ACTION, FINAL) \
  141. \
  142. static void name##_IO_APIC_irq (unsigned int irq) \
  143. __DO_ACTION(R, ACTION, FINAL)
  144. DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
  145. /* mask = 1 */
  146. DO_ACTION( __unmask, 0, &= 0xfffeffff, )
  147. /* mask = 0 */
  148. static void mask_IO_APIC_irq (unsigned int irq)
  149. {
  150. unsigned long flags;
  151. spin_lock_irqsave(&ioapic_lock, flags);
  152. __mask_IO_APIC_irq(irq);
  153. spin_unlock_irqrestore(&ioapic_lock, flags);
  154. }
  155. static void unmask_IO_APIC_irq (unsigned int irq)
  156. {
  157. unsigned long flags;
  158. spin_lock_irqsave(&ioapic_lock, flags);
  159. __unmask_IO_APIC_irq(irq);
  160. spin_unlock_irqrestore(&ioapic_lock, flags);
  161. }
  162. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  163. {
  164. struct IO_APIC_route_entry entry;
  165. unsigned long flags;
  166. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  167. spin_lock_irqsave(&ioapic_lock, flags);
  168. *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  169. *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  170. spin_unlock_irqrestore(&ioapic_lock, flags);
  171. if (entry.delivery_mode == dest_SMI)
  172. return;
  173. /*
  174. * Disable it in the IO-APIC irq-routing table:
  175. */
  176. memset(&entry, 0, sizeof(entry));
  177. entry.mask = 1;
  178. spin_lock_irqsave(&ioapic_lock, flags);
  179. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
  180. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
  181. spin_unlock_irqrestore(&ioapic_lock, flags);
  182. }
  183. static void clear_IO_APIC (void)
  184. {
  185. int apic, pin;
  186. for (apic = 0; apic < nr_ioapics; apic++)
  187. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  188. clear_IO_APIC_pin(apic, pin);
  189. }
  190. /*
  191. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  192. * specific CPU-side IRQs.
  193. */
  194. #define MAX_PIRQS 8
  195. static int pirq_entries [MAX_PIRQS];
  196. static int pirqs_enabled;
  197. int skip_ioapic_setup;
  198. int ioapic_force;
  199. /* dummy parsing: see setup.c */
  200. static int __init disable_ioapic_setup(char *str)
  201. {
  202. skip_ioapic_setup = 1;
  203. return 1;
  204. }
  205. static int __init enable_ioapic_setup(char *str)
  206. {
  207. ioapic_force = 1;
  208. skip_ioapic_setup = 0;
  209. return 1;
  210. }
  211. __setup("noapic", disable_ioapic_setup);
  212. __setup("apic", enable_ioapic_setup);
  213. static int __init setup_disable_8254_timer(char *s)
  214. {
  215. timer_over_8254 = -1;
  216. return 1;
  217. }
  218. static int __init setup_enable_8254_timer(char *s)
  219. {
  220. timer_over_8254 = 2;
  221. return 1;
  222. }
  223. __setup("disable_8254_timer", setup_disable_8254_timer);
  224. __setup("enable_8254_timer", setup_enable_8254_timer);
  225. #include <asm/pci-direct.h>
  226. #include <linux/pci_ids.h>
  227. #include <linux/pci.h>
  228. /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
  229. off. Check for an Nvidia or VIA PCI bridge and turn it off.
  230. Use pci direct infrastructure because this runs before the PCI subsystem.
  231. Can be overwritten with "apic"
  232. And another hack to disable the IOMMU on VIA chipsets.
  233. ... and others. Really should move this somewhere else.
  234. Kludge-O-Rama. */
  235. void __init check_ioapic(void)
  236. {
  237. int num,slot,func;
  238. /* Poor man's PCI discovery */
  239. for (num = 0; num < 32; num++) {
  240. for (slot = 0; slot < 32; slot++) {
  241. for (func = 0; func < 8; func++) {
  242. u32 class;
  243. u32 vendor;
  244. u8 type;
  245. class = read_pci_config(num,slot,func,
  246. PCI_CLASS_REVISION);
  247. if (class == 0xffffffff)
  248. break;
  249. if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
  250. continue;
  251. vendor = read_pci_config(num, slot, func,
  252. PCI_VENDOR_ID);
  253. vendor &= 0xffff;
  254. switch (vendor) {
  255. case PCI_VENDOR_ID_VIA:
  256. #ifdef CONFIG_GART_IOMMU
  257. if ((end_pfn > MAX_DMA32_PFN ||
  258. force_iommu) &&
  259. !iommu_aperture_allowed) {
  260. printk(KERN_INFO
  261. "Looks like a VIA chipset. Disabling IOMMU. Override with \"iommu=allowed\"\n");
  262. iommu_aperture_disabled = 1;
  263. }
  264. #endif
  265. return;
  266. case PCI_VENDOR_ID_NVIDIA:
  267. #ifdef CONFIG_ACPI
  268. /* All timer overrides on Nvidia
  269. seem to be wrong. Skip them. */
  270. acpi_skip_timer_override = 1;
  271. printk(KERN_INFO
  272. "Nvidia board detected. Ignoring ACPI timer override.\n");
  273. #endif
  274. /* RED-PEN skip them on mptables too? */
  275. return;
  276. /* This should be actually default, but
  277. for 2.6.16 let's do it for ATI only where
  278. it's really needed. */
  279. case PCI_VENDOR_ID_ATI:
  280. if (timer_over_8254 == 1) {
  281. timer_over_8254 = 0;
  282. printk(KERN_INFO
  283. "ATI board detected. Disabling timer routing over 8254.\n");
  284. }
  285. return;
  286. }
  287. /* No multi-function device? */
  288. type = read_pci_config_byte(num,slot,func,
  289. PCI_HEADER_TYPE);
  290. if (!(type & 0x80))
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. static int __init ioapic_pirq_setup(char *str)
  297. {
  298. int i, max;
  299. int ints[MAX_PIRQS+1];
  300. get_options(str, ARRAY_SIZE(ints), ints);
  301. for (i = 0; i < MAX_PIRQS; i++)
  302. pirq_entries[i] = -1;
  303. pirqs_enabled = 1;
  304. apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
  305. max = MAX_PIRQS;
  306. if (ints[0] < MAX_PIRQS)
  307. max = ints[0];
  308. for (i = 0; i < max; i++) {
  309. apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  310. /*
  311. * PIRQs are mapped upside down, usually.
  312. */
  313. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  314. }
  315. return 1;
  316. }
  317. __setup("pirq=", ioapic_pirq_setup);
  318. /*
  319. * Find the IRQ entry number of a certain pin.
  320. */
  321. static int find_irq_entry(int apic, int pin, int type)
  322. {
  323. int i;
  324. for (i = 0; i < mp_irq_entries; i++)
  325. if (mp_irqs[i].mpc_irqtype == type &&
  326. (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  327. mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  328. mp_irqs[i].mpc_dstirq == pin)
  329. return i;
  330. return -1;
  331. }
  332. /*
  333. * Find the pin to which IRQ[irq] (ISA) is connected
  334. */
  335. static int __init find_isa_irq_pin(int irq, int type)
  336. {
  337. int i;
  338. for (i = 0; i < mp_irq_entries; i++) {
  339. int lbus = mp_irqs[i].mpc_srcbus;
  340. if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
  341. mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
  342. mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
  343. (mp_irqs[i].mpc_irqtype == type) &&
  344. (mp_irqs[i].mpc_srcbusirq == irq))
  345. return mp_irqs[i].mpc_dstirq;
  346. }
  347. return -1;
  348. }
  349. static int __init find_isa_irq_apic(int irq, int type)
  350. {
  351. int i;
  352. for (i = 0; i < mp_irq_entries; i++) {
  353. int lbus = mp_irqs[i].mpc_srcbus;
  354. if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
  355. mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
  356. mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
  357. (mp_irqs[i].mpc_irqtype == type) &&
  358. (mp_irqs[i].mpc_srcbusirq == irq))
  359. break;
  360. }
  361. if (i < mp_irq_entries) {
  362. int apic;
  363. for(apic = 0; apic < nr_ioapics; apic++) {
  364. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
  365. return apic;
  366. }
  367. }
  368. return -1;
  369. }
  370. /*
  371. * Find a specific PCI IRQ entry.
  372. * Not an __init, possibly needed by modules
  373. */
  374. static int pin_2_irq(int idx, int apic, int pin);
  375. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  376. {
  377. int apic, i, best_guess = -1;
  378. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  379. bus, slot, pin);
  380. if (mp_bus_id_to_pci_bus[bus] == -1) {
  381. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  382. return -1;
  383. }
  384. for (i = 0; i < mp_irq_entries; i++) {
  385. int lbus = mp_irqs[i].mpc_srcbus;
  386. for (apic = 0; apic < nr_ioapics; apic++)
  387. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  388. mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  389. break;
  390. if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
  391. !mp_irqs[i].mpc_irqtype &&
  392. (bus == lbus) &&
  393. (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  394. int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
  395. if (!(apic || IO_APIC_IRQ(irq)))
  396. continue;
  397. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  398. return irq;
  399. /*
  400. * Use the first all-but-pin matching entry as a
  401. * best-guess fuzzy result for broken mptables.
  402. */
  403. if (best_guess < 0)
  404. best_guess = irq;
  405. }
  406. }
  407. BUG_ON(best_guess >= NR_IRQS);
  408. return best_guess;
  409. }
  410. /*
  411. * EISA Edge/Level control register, ELCR
  412. */
  413. static int EISA_ELCR(unsigned int irq)
  414. {
  415. if (irq < 16) {
  416. unsigned int port = 0x4d0 + (irq >> 3);
  417. return (inb(port) >> (irq & 7)) & 1;
  418. }
  419. apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
  420. return 0;
  421. }
  422. /* EISA interrupts are always polarity zero and can be edge or level
  423. * trigger depending on the ELCR value. If an interrupt is listed as
  424. * EISA conforming in the MP table, that means its trigger type must
  425. * be read in from the ELCR */
  426. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
  427. #define default_EISA_polarity(idx) (0)
  428. /* ISA interrupts are always polarity zero edge triggered,
  429. * when listed as conforming in the MP table. */
  430. #define default_ISA_trigger(idx) (0)
  431. #define default_ISA_polarity(idx) (0)
  432. /* PCI interrupts are always polarity one level triggered,
  433. * when listed as conforming in the MP table. */
  434. #define default_PCI_trigger(idx) (1)
  435. #define default_PCI_polarity(idx) (1)
  436. /* MCA interrupts are always polarity zero level triggered,
  437. * when listed as conforming in the MP table. */
  438. #define default_MCA_trigger(idx) (1)
  439. #define default_MCA_polarity(idx) (0)
  440. static int __init MPBIOS_polarity(int idx)
  441. {
  442. int bus = mp_irqs[idx].mpc_srcbus;
  443. int polarity;
  444. /*
  445. * Determine IRQ line polarity (high active or low active):
  446. */
  447. switch (mp_irqs[idx].mpc_irqflag & 3)
  448. {
  449. case 0: /* conforms, ie. bus-type dependent polarity */
  450. {
  451. switch (mp_bus_id_to_type[bus])
  452. {
  453. case MP_BUS_ISA: /* ISA pin */
  454. {
  455. polarity = default_ISA_polarity(idx);
  456. break;
  457. }
  458. case MP_BUS_EISA: /* EISA pin */
  459. {
  460. polarity = default_EISA_polarity(idx);
  461. break;
  462. }
  463. case MP_BUS_PCI: /* PCI pin */
  464. {
  465. polarity = default_PCI_polarity(idx);
  466. break;
  467. }
  468. case MP_BUS_MCA: /* MCA pin */
  469. {
  470. polarity = default_MCA_polarity(idx);
  471. break;
  472. }
  473. default:
  474. {
  475. printk(KERN_WARNING "broken BIOS!!\n");
  476. polarity = 1;
  477. break;
  478. }
  479. }
  480. break;
  481. }
  482. case 1: /* high active */
  483. {
  484. polarity = 0;
  485. break;
  486. }
  487. case 2: /* reserved */
  488. {
  489. printk(KERN_WARNING "broken BIOS!!\n");
  490. polarity = 1;
  491. break;
  492. }
  493. case 3: /* low active */
  494. {
  495. polarity = 1;
  496. break;
  497. }
  498. default: /* invalid */
  499. {
  500. printk(KERN_WARNING "broken BIOS!!\n");
  501. polarity = 1;
  502. break;
  503. }
  504. }
  505. return polarity;
  506. }
  507. static int MPBIOS_trigger(int idx)
  508. {
  509. int bus = mp_irqs[idx].mpc_srcbus;
  510. int trigger;
  511. /*
  512. * Determine IRQ trigger mode (edge or level sensitive):
  513. */
  514. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
  515. {
  516. case 0: /* conforms, ie. bus-type dependent */
  517. {
  518. switch (mp_bus_id_to_type[bus])
  519. {
  520. case MP_BUS_ISA: /* ISA pin */
  521. {
  522. trigger = default_ISA_trigger(idx);
  523. break;
  524. }
  525. case MP_BUS_EISA: /* EISA pin */
  526. {
  527. trigger = default_EISA_trigger(idx);
  528. break;
  529. }
  530. case MP_BUS_PCI: /* PCI pin */
  531. {
  532. trigger = default_PCI_trigger(idx);
  533. break;
  534. }
  535. case MP_BUS_MCA: /* MCA pin */
  536. {
  537. trigger = default_MCA_trigger(idx);
  538. break;
  539. }
  540. default:
  541. {
  542. printk(KERN_WARNING "broken BIOS!!\n");
  543. trigger = 1;
  544. break;
  545. }
  546. }
  547. break;
  548. }
  549. case 1: /* edge */
  550. {
  551. trigger = 0;
  552. break;
  553. }
  554. case 2: /* reserved */
  555. {
  556. printk(KERN_WARNING "broken BIOS!!\n");
  557. trigger = 1;
  558. break;
  559. }
  560. case 3: /* level */
  561. {
  562. trigger = 1;
  563. break;
  564. }
  565. default: /* invalid */
  566. {
  567. printk(KERN_WARNING "broken BIOS!!\n");
  568. trigger = 0;
  569. break;
  570. }
  571. }
  572. return trigger;
  573. }
  574. static inline int irq_polarity(int idx)
  575. {
  576. return MPBIOS_polarity(idx);
  577. }
  578. static inline int irq_trigger(int idx)
  579. {
  580. return MPBIOS_trigger(idx);
  581. }
  582. static int next_irq = 16;
  583. /*
  584. * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
  585. * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
  586. * from ACPI, which can reach 800 in large boxen.
  587. *
  588. * Compact the sparse GSI space into a sequential IRQ series and reuse
  589. * vectors if possible.
  590. */
  591. int gsi_irq_sharing(int gsi)
  592. {
  593. int i, tries, vector;
  594. BUG_ON(gsi >= NR_IRQ_VECTORS);
  595. if (platform_legacy_irq(gsi))
  596. return gsi;
  597. if (gsi_2_irq[gsi] != 0xFF)
  598. return (int)gsi_2_irq[gsi];
  599. tries = NR_IRQS;
  600. try_again:
  601. vector = assign_irq_vector(gsi);
  602. /*
  603. * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
  604. * use of vector and if found, return that IRQ. However, we never want
  605. * to share legacy IRQs, which usually have a different trigger mode
  606. * than PCI.
  607. */
  608. for (i = 0; i < NR_IRQS; i++)
  609. if (IO_APIC_VECTOR(i) == vector)
  610. break;
  611. if (platform_legacy_irq(i)) {
  612. if (--tries >= 0) {
  613. IO_APIC_VECTOR(i) = 0;
  614. goto try_again;
  615. }
  616. panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
  617. }
  618. if (i < NR_IRQS) {
  619. gsi_2_irq[gsi] = i;
  620. printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
  621. gsi, vector, i);
  622. return i;
  623. }
  624. i = next_irq++;
  625. BUG_ON(i >= NR_IRQS);
  626. gsi_2_irq[gsi] = i;
  627. IO_APIC_VECTOR(i) = vector;
  628. printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
  629. gsi, vector, i);
  630. return i;
  631. }
  632. static int pin_2_irq(int idx, int apic, int pin)
  633. {
  634. int irq, i;
  635. int bus = mp_irqs[idx].mpc_srcbus;
  636. /*
  637. * Debugging check, we are in big trouble if this message pops up!
  638. */
  639. if (mp_irqs[idx].mpc_dstirq != pin)
  640. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  641. switch (mp_bus_id_to_type[bus])
  642. {
  643. case MP_BUS_ISA: /* ISA pin */
  644. case MP_BUS_EISA:
  645. case MP_BUS_MCA:
  646. {
  647. irq = mp_irqs[idx].mpc_srcbusirq;
  648. break;
  649. }
  650. case MP_BUS_PCI: /* PCI pin */
  651. {
  652. /*
  653. * PCI IRQs are mapped in order
  654. */
  655. i = irq = 0;
  656. while (i < apic)
  657. irq += nr_ioapic_registers[i++];
  658. irq += pin;
  659. irq = gsi_irq_sharing(irq);
  660. break;
  661. }
  662. default:
  663. {
  664. printk(KERN_ERR "unknown bus type %d.\n",bus);
  665. irq = 0;
  666. break;
  667. }
  668. }
  669. BUG_ON(irq >= NR_IRQS);
  670. /*
  671. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  672. */
  673. if ((pin >= 16) && (pin <= 23)) {
  674. if (pirq_entries[pin-16] != -1) {
  675. if (!pirq_entries[pin-16]) {
  676. apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
  677. } else {
  678. irq = pirq_entries[pin-16];
  679. apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
  680. pin-16, irq);
  681. }
  682. }
  683. }
  684. BUG_ON(irq >= NR_IRQS);
  685. return irq;
  686. }
  687. static inline int IO_APIC_irq_trigger(int irq)
  688. {
  689. int apic, idx, pin;
  690. for (apic = 0; apic < nr_ioapics; apic++) {
  691. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  692. idx = find_irq_entry(apic,pin,mp_INT);
  693. if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
  694. return irq_trigger(idx);
  695. }
  696. }
  697. /*
  698. * nonexistent IRQs are edge default
  699. */
  700. return 0;
  701. }
  702. /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
  703. u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
  704. int assign_irq_vector(int irq)
  705. {
  706. static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
  707. BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
  708. if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
  709. return IO_APIC_VECTOR(irq);
  710. next:
  711. current_vector += 8;
  712. if (current_vector == IA32_SYSCALL_VECTOR)
  713. goto next;
  714. if (current_vector >= FIRST_SYSTEM_VECTOR) {
  715. /* If we run out of vectors on large boxen, must share them. */
  716. offset = (offset + 1) % 8;
  717. current_vector = FIRST_DEVICE_VECTOR + offset;
  718. }
  719. vector_irq[current_vector] = irq;
  720. if (irq != AUTO_ASSIGN)
  721. IO_APIC_VECTOR(irq) = current_vector;
  722. return current_vector;
  723. }
  724. extern void (*interrupt[NR_IRQS])(void);
  725. static struct hw_interrupt_type ioapic_level_type;
  726. static struct hw_interrupt_type ioapic_edge_type;
  727. #define IOAPIC_AUTO -1
  728. #define IOAPIC_EDGE 0
  729. #define IOAPIC_LEVEL 1
  730. static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  731. {
  732. if (use_pci_vector() && !platform_legacy_irq(irq)) {
  733. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  734. trigger == IOAPIC_LEVEL)
  735. irq_desc[vector].handler = &ioapic_level_type;
  736. else
  737. irq_desc[vector].handler = &ioapic_edge_type;
  738. set_intr_gate(vector, interrupt[vector]);
  739. } else {
  740. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  741. trigger == IOAPIC_LEVEL)
  742. irq_desc[irq].handler = &ioapic_level_type;
  743. else
  744. irq_desc[irq].handler = &ioapic_edge_type;
  745. set_intr_gate(vector, interrupt[irq]);
  746. }
  747. }
  748. static void __init setup_IO_APIC_irqs(void)
  749. {
  750. struct IO_APIC_route_entry entry;
  751. int apic, pin, idx, irq, first_notcon = 1, vector;
  752. unsigned long flags;
  753. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  754. for (apic = 0; apic < nr_ioapics; apic++) {
  755. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  756. /*
  757. * add it to the IO-APIC irq-routing table:
  758. */
  759. memset(&entry,0,sizeof(entry));
  760. entry.delivery_mode = INT_DELIVERY_MODE;
  761. entry.dest_mode = INT_DEST_MODE;
  762. entry.mask = 0; /* enable IRQ */
  763. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  764. idx = find_irq_entry(apic,pin,mp_INT);
  765. if (idx == -1) {
  766. if (first_notcon) {
  767. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  768. first_notcon = 0;
  769. } else
  770. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  771. continue;
  772. }
  773. entry.trigger = irq_trigger(idx);
  774. entry.polarity = irq_polarity(idx);
  775. if (irq_trigger(idx)) {
  776. entry.trigger = 1;
  777. entry.mask = 1;
  778. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  779. }
  780. irq = pin_2_irq(idx, apic, pin);
  781. add_pin_to_irq(irq, apic, pin);
  782. if (!apic && !IO_APIC_IRQ(irq))
  783. continue;
  784. if (IO_APIC_IRQ(irq)) {
  785. vector = assign_irq_vector(irq);
  786. entry.vector = vector;
  787. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  788. if (!apic && (irq < 16))
  789. disable_8259A_irq(irq);
  790. }
  791. spin_lock_irqsave(&ioapic_lock, flags);
  792. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  793. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  794. set_native_irq_info(irq, TARGET_CPUS);
  795. spin_unlock_irqrestore(&ioapic_lock, flags);
  796. }
  797. }
  798. if (!first_notcon)
  799. apic_printk(APIC_VERBOSE," not connected.\n");
  800. }
  801. /*
  802. * Set up the 8259A-master output pin as broadcast to all
  803. * CPUs.
  804. */
  805. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  806. {
  807. struct IO_APIC_route_entry entry;
  808. unsigned long flags;
  809. memset(&entry,0,sizeof(entry));
  810. disable_8259A_irq(0);
  811. /* mask LVT0 */
  812. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  813. /*
  814. * We use logical delivery to get the timer IRQ
  815. * to the first CPU.
  816. */
  817. entry.dest_mode = INT_DEST_MODE;
  818. entry.mask = 0; /* unmask IRQ now */
  819. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  820. entry.delivery_mode = INT_DELIVERY_MODE;
  821. entry.polarity = 0;
  822. entry.trigger = 0;
  823. entry.vector = vector;
  824. /*
  825. * The timer IRQ doesn't have to know that behind the
  826. * scene we have a 8259A-master in AEOI mode ...
  827. */
  828. irq_desc[0].handler = &ioapic_edge_type;
  829. /*
  830. * Add it to the IO-APIC irq-routing table:
  831. */
  832. spin_lock_irqsave(&ioapic_lock, flags);
  833. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  834. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  835. spin_unlock_irqrestore(&ioapic_lock, flags);
  836. enable_8259A_irq(0);
  837. }
  838. void __init UNEXPECTED_IO_APIC(void)
  839. {
  840. }
  841. void __apicdebuginit print_IO_APIC(void)
  842. {
  843. int apic, i;
  844. union IO_APIC_reg_00 reg_00;
  845. union IO_APIC_reg_01 reg_01;
  846. union IO_APIC_reg_02 reg_02;
  847. unsigned long flags;
  848. if (apic_verbosity == APIC_QUIET)
  849. return;
  850. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  851. for (i = 0; i < nr_ioapics; i++)
  852. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  853. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  854. /*
  855. * We are a bit conservative about what we expect. We have to
  856. * know about every hardware change ASAP.
  857. */
  858. printk(KERN_INFO "testing the IO APIC.......................\n");
  859. for (apic = 0; apic < nr_ioapics; apic++) {
  860. spin_lock_irqsave(&ioapic_lock, flags);
  861. reg_00.raw = io_apic_read(apic, 0);
  862. reg_01.raw = io_apic_read(apic, 1);
  863. if (reg_01.bits.version >= 0x10)
  864. reg_02.raw = io_apic_read(apic, 2);
  865. spin_unlock_irqrestore(&ioapic_lock, flags);
  866. printk("\n");
  867. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  868. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  869. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  870. if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
  871. UNEXPECTED_IO_APIC();
  872. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  873. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  874. if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
  875. (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
  876. (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
  877. (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
  878. (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
  879. (reg_01.bits.entries != 0x2E) &&
  880. (reg_01.bits.entries != 0x3F) &&
  881. (reg_01.bits.entries != 0x03)
  882. )
  883. UNEXPECTED_IO_APIC();
  884. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  885. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  886. if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
  887. (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
  888. (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
  889. (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
  890. (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
  891. (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
  892. )
  893. UNEXPECTED_IO_APIC();
  894. if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
  895. UNEXPECTED_IO_APIC();
  896. if (reg_01.bits.version >= 0x10) {
  897. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  898. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  899. if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
  900. UNEXPECTED_IO_APIC();
  901. }
  902. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  903. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  904. " Stat Dest Deli Vect: \n");
  905. for (i = 0; i <= reg_01.bits.entries; i++) {
  906. struct IO_APIC_route_entry entry;
  907. spin_lock_irqsave(&ioapic_lock, flags);
  908. *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
  909. *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
  910. spin_unlock_irqrestore(&ioapic_lock, flags);
  911. printk(KERN_DEBUG " %02x %03X %02X ",
  912. i,
  913. entry.dest.logical.logical_dest,
  914. entry.dest.physical.physical_dest
  915. );
  916. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  917. entry.mask,
  918. entry.trigger,
  919. entry.irr,
  920. entry.polarity,
  921. entry.delivery_status,
  922. entry.dest_mode,
  923. entry.delivery_mode,
  924. entry.vector
  925. );
  926. }
  927. }
  928. if (use_pci_vector())
  929. printk(KERN_INFO "Using vector-based indexing\n");
  930. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  931. for (i = 0; i < NR_IRQS; i++) {
  932. struct irq_pin_list *entry = irq_2_pin + i;
  933. if (entry->pin < 0)
  934. continue;
  935. if (use_pci_vector() && !platform_legacy_irq(i))
  936. printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
  937. else
  938. printk(KERN_DEBUG "IRQ%d ", i);
  939. for (;;) {
  940. printk("-> %d:%d", entry->apic, entry->pin);
  941. if (!entry->next)
  942. break;
  943. entry = irq_2_pin + entry->next;
  944. }
  945. printk("\n");
  946. }
  947. printk(KERN_INFO ".................................... done.\n");
  948. return;
  949. }
  950. #if 0
  951. static __apicdebuginit void print_APIC_bitfield (int base)
  952. {
  953. unsigned int v;
  954. int i, j;
  955. if (apic_verbosity == APIC_QUIET)
  956. return;
  957. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  958. for (i = 0; i < 8; i++) {
  959. v = apic_read(base + i*0x10);
  960. for (j = 0; j < 32; j++) {
  961. if (v & (1<<j))
  962. printk("1");
  963. else
  964. printk("0");
  965. }
  966. printk("\n");
  967. }
  968. }
  969. void __apicdebuginit print_local_APIC(void * dummy)
  970. {
  971. unsigned int v, ver, maxlvt;
  972. if (apic_verbosity == APIC_QUIET)
  973. return;
  974. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  975. smp_processor_id(), hard_smp_processor_id());
  976. v = apic_read(APIC_ID);
  977. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  978. v = apic_read(APIC_LVR);
  979. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  980. ver = GET_APIC_VERSION(v);
  981. maxlvt = get_maxlvt();
  982. v = apic_read(APIC_TASKPRI);
  983. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  984. v = apic_read(APIC_ARBPRI);
  985. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  986. v & APIC_ARBPRI_MASK);
  987. v = apic_read(APIC_PROCPRI);
  988. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  989. v = apic_read(APIC_EOI);
  990. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  991. v = apic_read(APIC_RRR);
  992. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  993. v = apic_read(APIC_LDR);
  994. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  995. v = apic_read(APIC_DFR);
  996. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  997. v = apic_read(APIC_SPIV);
  998. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  999. printk(KERN_DEBUG "... APIC ISR field:\n");
  1000. print_APIC_bitfield(APIC_ISR);
  1001. printk(KERN_DEBUG "... APIC TMR field:\n");
  1002. print_APIC_bitfield(APIC_TMR);
  1003. printk(KERN_DEBUG "... APIC IRR field:\n");
  1004. print_APIC_bitfield(APIC_IRR);
  1005. v = apic_read(APIC_ESR);
  1006. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1007. v = apic_read(APIC_ICR);
  1008. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  1009. v = apic_read(APIC_ICR2);
  1010. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  1011. v = apic_read(APIC_LVTT);
  1012. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1013. if (maxlvt > 3) { /* PC is LVT#4. */
  1014. v = apic_read(APIC_LVTPC);
  1015. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1016. }
  1017. v = apic_read(APIC_LVT0);
  1018. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1019. v = apic_read(APIC_LVT1);
  1020. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1021. if (maxlvt > 2) { /* ERR is LVT#3. */
  1022. v = apic_read(APIC_LVTERR);
  1023. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1024. }
  1025. v = apic_read(APIC_TMICT);
  1026. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1027. v = apic_read(APIC_TMCCT);
  1028. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1029. v = apic_read(APIC_TDCR);
  1030. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1031. printk("\n");
  1032. }
  1033. void print_all_local_APICs (void)
  1034. {
  1035. on_each_cpu(print_local_APIC, NULL, 1, 1);
  1036. }
  1037. void __apicdebuginit print_PIC(void)
  1038. {
  1039. unsigned int v;
  1040. unsigned long flags;
  1041. if (apic_verbosity == APIC_QUIET)
  1042. return;
  1043. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1044. spin_lock_irqsave(&i8259A_lock, flags);
  1045. v = inb(0xa1) << 8 | inb(0x21);
  1046. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1047. v = inb(0xa0) << 8 | inb(0x20);
  1048. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1049. outb(0x0b,0xa0);
  1050. outb(0x0b,0x20);
  1051. v = inb(0xa0) << 8 | inb(0x20);
  1052. outb(0x0a,0xa0);
  1053. outb(0x0a,0x20);
  1054. spin_unlock_irqrestore(&i8259A_lock, flags);
  1055. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1056. v = inb(0x4d1) << 8 | inb(0x4d0);
  1057. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1058. }
  1059. #endif /* 0 */
  1060. static void __init enable_IO_APIC(void)
  1061. {
  1062. union IO_APIC_reg_01 reg_01;
  1063. int i8259_apic, i8259_pin;
  1064. int i, apic;
  1065. unsigned long flags;
  1066. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1067. irq_2_pin[i].pin = -1;
  1068. irq_2_pin[i].next = 0;
  1069. }
  1070. if (!pirqs_enabled)
  1071. for (i = 0; i < MAX_PIRQS; i++)
  1072. pirq_entries[i] = -1;
  1073. /*
  1074. * The number of IO-APIC IRQ registers (== #pins):
  1075. */
  1076. for (apic = 0; apic < nr_ioapics; apic++) {
  1077. spin_lock_irqsave(&ioapic_lock, flags);
  1078. reg_01.raw = io_apic_read(apic, 1);
  1079. spin_unlock_irqrestore(&ioapic_lock, flags);
  1080. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1081. }
  1082. for(apic = 0; apic < nr_ioapics; apic++) {
  1083. int pin;
  1084. /* See if any of the pins is in ExtINT mode */
  1085. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1086. struct IO_APIC_route_entry entry;
  1087. spin_lock_irqsave(&ioapic_lock, flags);
  1088. *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1089. *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1090. spin_unlock_irqrestore(&ioapic_lock, flags);
  1091. /* If the interrupt line is enabled and in ExtInt mode
  1092. * I have found the pin where the i8259 is connected.
  1093. */
  1094. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1095. ioapic_i8259.apic = apic;
  1096. ioapic_i8259.pin = pin;
  1097. goto found_i8259;
  1098. }
  1099. }
  1100. }
  1101. found_i8259:
  1102. /* Look to see what if the MP table has reported the ExtINT */
  1103. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1104. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1105. /* Trust the MP table if nothing is setup in the hardware */
  1106. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1107. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1108. ioapic_i8259.pin = i8259_pin;
  1109. ioapic_i8259.apic = i8259_apic;
  1110. }
  1111. /* Complain if the MP table and the hardware disagree */
  1112. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1113. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1114. {
  1115. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1116. }
  1117. /*
  1118. * Do not trust the IO-APIC being empty at bootup
  1119. */
  1120. clear_IO_APIC();
  1121. }
  1122. /*
  1123. * Not an __init, needed by the reboot code
  1124. */
  1125. void disable_IO_APIC(void)
  1126. {
  1127. /*
  1128. * Clear the IO-APIC before rebooting:
  1129. */
  1130. clear_IO_APIC();
  1131. /*
  1132. * If the i8259 is routed through an IOAPIC
  1133. * Put that IOAPIC in virtual wire mode
  1134. * so legacy interrupts can be delivered.
  1135. */
  1136. if (ioapic_i8259.pin != -1) {
  1137. struct IO_APIC_route_entry entry;
  1138. unsigned long flags;
  1139. memset(&entry, 0, sizeof(entry));
  1140. entry.mask = 0; /* Enabled */
  1141. entry.trigger = 0; /* Edge */
  1142. entry.irr = 0;
  1143. entry.polarity = 0; /* High */
  1144. entry.delivery_status = 0;
  1145. entry.dest_mode = 0; /* Physical */
  1146. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1147. entry.vector = 0;
  1148. entry.dest.physical.physical_dest =
  1149. GET_APIC_ID(apic_read(APIC_ID));
  1150. /*
  1151. * Add it to the IO-APIC irq-routing table:
  1152. */
  1153. spin_lock_irqsave(&ioapic_lock, flags);
  1154. io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
  1155. *(((int *)&entry)+1));
  1156. io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
  1157. *(((int *)&entry)+0));
  1158. spin_unlock_irqrestore(&ioapic_lock, flags);
  1159. }
  1160. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1161. }
  1162. /*
  1163. * function to set the IO-APIC physical IDs based on the
  1164. * values stored in the MPC table.
  1165. *
  1166. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1167. */
  1168. static void __init setup_ioapic_ids_from_mpc (void)
  1169. {
  1170. union IO_APIC_reg_00 reg_00;
  1171. int apic;
  1172. int i;
  1173. unsigned char old_id;
  1174. unsigned long flags;
  1175. /*
  1176. * Set the IOAPIC ID to the value stored in the MPC table.
  1177. */
  1178. for (apic = 0; apic < nr_ioapics; apic++) {
  1179. /* Read the register 0 value */
  1180. spin_lock_irqsave(&ioapic_lock, flags);
  1181. reg_00.raw = io_apic_read(apic, 0);
  1182. spin_unlock_irqrestore(&ioapic_lock, flags);
  1183. old_id = mp_ioapics[apic].mpc_apicid;
  1184. printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
  1185. /*
  1186. * We need to adjust the IRQ routing table
  1187. * if the ID changed.
  1188. */
  1189. if (old_id != mp_ioapics[apic].mpc_apicid)
  1190. for (i = 0; i < mp_irq_entries; i++)
  1191. if (mp_irqs[i].mpc_dstapic == old_id)
  1192. mp_irqs[i].mpc_dstapic
  1193. = mp_ioapics[apic].mpc_apicid;
  1194. /*
  1195. * Read the right value from the MPC table and
  1196. * write it into the ID register.
  1197. */
  1198. apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
  1199. mp_ioapics[apic].mpc_apicid);
  1200. reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
  1201. spin_lock_irqsave(&ioapic_lock, flags);
  1202. io_apic_write(apic, 0, reg_00.raw);
  1203. spin_unlock_irqrestore(&ioapic_lock, flags);
  1204. /*
  1205. * Sanity check
  1206. */
  1207. spin_lock_irqsave(&ioapic_lock, flags);
  1208. reg_00.raw = io_apic_read(apic, 0);
  1209. spin_unlock_irqrestore(&ioapic_lock, flags);
  1210. if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
  1211. printk("could not set ID!\n");
  1212. else
  1213. apic_printk(APIC_VERBOSE," ok.\n");
  1214. }
  1215. }
  1216. /*
  1217. * There is a nasty bug in some older SMP boards, their mptable lies
  1218. * about the timer IRQ. We do the following to work around the situation:
  1219. *
  1220. * - timer IRQ defaults to IO-APIC IRQ
  1221. * - if this function detects that timer IRQs are defunct, then we fall
  1222. * back to ISA timer IRQs
  1223. */
  1224. static int __init timer_irq_works(void)
  1225. {
  1226. unsigned long t1 = jiffies;
  1227. local_irq_enable();
  1228. /* Let ten ticks pass... */
  1229. mdelay((10 * 1000) / HZ);
  1230. /*
  1231. * Expect a few ticks at least, to be sure some possible
  1232. * glue logic does not lock up after one or two first
  1233. * ticks in a non-ExtINT mode. Also the local APIC
  1234. * might have cached one ExtINT interrupt. Finally, at
  1235. * least one tick may be lost due to delays.
  1236. */
  1237. /* jiffies wrap? */
  1238. if (jiffies - t1 > 4)
  1239. return 1;
  1240. return 0;
  1241. }
  1242. /*
  1243. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1244. * number of pending IRQ events unhandled. These cases are very rare,
  1245. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1246. * better to do it this way as thus we do not have to be aware of
  1247. * 'pending' interrupts in the IRQ path, except at this point.
  1248. */
  1249. /*
  1250. * Edge triggered needs to resend any interrupt
  1251. * that was delayed but this is now handled in the device
  1252. * independent code.
  1253. */
  1254. /*
  1255. * Starting up a edge-triggered IO-APIC interrupt is
  1256. * nasty - we need to make sure that we get the edge.
  1257. * If it is already asserted for some reason, we need
  1258. * return 1 to indicate that is was pending.
  1259. *
  1260. * This is not complete - we should be able to fake
  1261. * an edge even if it isn't on the 8259A...
  1262. */
  1263. static unsigned int startup_edge_ioapic_irq(unsigned int irq)
  1264. {
  1265. int was_pending = 0;
  1266. unsigned long flags;
  1267. spin_lock_irqsave(&ioapic_lock, flags);
  1268. if (irq < 16) {
  1269. disable_8259A_irq(irq);
  1270. if (i8259A_irq_pending(irq))
  1271. was_pending = 1;
  1272. }
  1273. __unmask_IO_APIC_irq(irq);
  1274. spin_unlock_irqrestore(&ioapic_lock, flags);
  1275. return was_pending;
  1276. }
  1277. /*
  1278. * Once we have recorded IRQ_PENDING already, we can mask the
  1279. * interrupt for real. This prevents IRQ storms from unhandled
  1280. * devices.
  1281. */
  1282. static void ack_edge_ioapic_irq(unsigned int irq)
  1283. {
  1284. move_irq(irq);
  1285. if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
  1286. == (IRQ_PENDING | IRQ_DISABLED))
  1287. mask_IO_APIC_irq(irq);
  1288. ack_APIC_irq();
  1289. }
  1290. /*
  1291. * Level triggered interrupts can just be masked,
  1292. * and shutting down and starting up the interrupt
  1293. * is the same as enabling and disabling them -- except
  1294. * with a startup need to return a "was pending" value.
  1295. *
  1296. * Level triggered interrupts are special because we
  1297. * do not touch any IO-APIC register while handling
  1298. * them. We ack the APIC in the end-IRQ handler, not
  1299. * in the start-IRQ-handler. Protection against reentrance
  1300. * from the same interrupt is still provided, both by the
  1301. * generic IRQ layer and by the fact that an unacked local
  1302. * APIC does not accept IRQs.
  1303. */
  1304. static unsigned int startup_level_ioapic_irq (unsigned int irq)
  1305. {
  1306. unmask_IO_APIC_irq(irq);
  1307. return 0; /* don't check for pending */
  1308. }
  1309. static void end_level_ioapic_irq (unsigned int irq)
  1310. {
  1311. move_irq(irq);
  1312. ack_APIC_irq();
  1313. }
  1314. #ifdef CONFIG_PCI_MSI
  1315. static unsigned int startup_edge_ioapic_vector(unsigned int vector)
  1316. {
  1317. int irq = vector_to_irq(vector);
  1318. return startup_edge_ioapic_irq(irq);
  1319. }
  1320. static void ack_edge_ioapic_vector(unsigned int vector)
  1321. {
  1322. int irq = vector_to_irq(vector);
  1323. move_native_irq(vector);
  1324. ack_edge_ioapic_irq(irq);
  1325. }
  1326. static unsigned int startup_level_ioapic_vector (unsigned int vector)
  1327. {
  1328. int irq = vector_to_irq(vector);
  1329. return startup_level_ioapic_irq (irq);
  1330. }
  1331. static void end_level_ioapic_vector (unsigned int vector)
  1332. {
  1333. int irq = vector_to_irq(vector);
  1334. move_native_irq(vector);
  1335. end_level_ioapic_irq(irq);
  1336. }
  1337. static void mask_IO_APIC_vector (unsigned int vector)
  1338. {
  1339. int irq = vector_to_irq(vector);
  1340. mask_IO_APIC_irq(irq);
  1341. }
  1342. static void unmask_IO_APIC_vector (unsigned int vector)
  1343. {
  1344. int irq = vector_to_irq(vector);
  1345. unmask_IO_APIC_irq(irq);
  1346. }
  1347. #ifdef CONFIG_SMP
  1348. static void set_ioapic_affinity_vector (unsigned int vector,
  1349. cpumask_t cpu_mask)
  1350. {
  1351. int irq = vector_to_irq(vector);
  1352. set_native_irq_info(vector, cpu_mask);
  1353. set_ioapic_affinity_irq(irq, cpu_mask);
  1354. }
  1355. #endif // CONFIG_SMP
  1356. #endif // CONFIG_PCI_MSI
  1357. /*
  1358. * Level and edge triggered IO-APIC interrupts need different handling,
  1359. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1360. * handled with the level-triggered descriptor, but that one has slightly
  1361. * more overhead. Level-triggered interrupts cannot be handled with the
  1362. * edge-triggered handler, without risking IRQ storms and other ugly
  1363. * races.
  1364. */
  1365. static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
  1366. .typename = "IO-APIC-edge",
  1367. .startup = startup_edge_ioapic,
  1368. .shutdown = shutdown_edge_ioapic,
  1369. .enable = enable_edge_ioapic,
  1370. .disable = disable_edge_ioapic,
  1371. .ack = ack_edge_ioapic,
  1372. .end = end_edge_ioapic,
  1373. #ifdef CONFIG_SMP
  1374. .set_affinity = set_ioapic_affinity,
  1375. #endif
  1376. };
  1377. static struct hw_interrupt_type ioapic_level_type __read_mostly = {
  1378. .typename = "IO-APIC-level",
  1379. .startup = startup_level_ioapic,
  1380. .shutdown = shutdown_level_ioapic,
  1381. .enable = enable_level_ioapic,
  1382. .disable = disable_level_ioapic,
  1383. .ack = mask_and_ack_level_ioapic,
  1384. .end = end_level_ioapic,
  1385. #ifdef CONFIG_SMP
  1386. .set_affinity = set_ioapic_affinity,
  1387. #endif
  1388. };
  1389. static inline void init_IO_APIC_traps(void)
  1390. {
  1391. int irq;
  1392. /*
  1393. * NOTE! The local APIC isn't very good at handling
  1394. * multiple interrupts at the same interrupt level.
  1395. * As the interrupt level is determined by taking the
  1396. * vector number and shifting that right by 4, we
  1397. * want to spread these out a bit so that they don't
  1398. * all fall in the same interrupt level.
  1399. *
  1400. * Also, we've got to be careful not to trash gate
  1401. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1402. */
  1403. for (irq = 0; irq < NR_IRQS ; irq++) {
  1404. int tmp = irq;
  1405. if (use_pci_vector()) {
  1406. if (!platform_legacy_irq(tmp))
  1407. if ((tmp = vector_to_irq(tmp)) == -1)
  1408. continue;
  1409. }
  1410. if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
  1411. /*
  1412. * Hmm.. We don't have an entry for this,
  1413. * so default to an old-fashioned 8259
  1414. * interrupt if we can..
  1415. */
  1416. if (irq < 16)
  1417. make_8259A_irq(irq);
  1418. else
  1419. /* Strange. Oh, well.. */
  1420. irq_desc[irq].handler = &no_irq_type;
  1421. }
  1422. }
  1423. }
  1424. static void enable_lapic_irq (unsigned int irq)
  1425. {
  1426. unsigned long v;
  1427. v = apic_read(APIC_LVT0);
  1428. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1429. }
  1430. static void disable_lapic_irq (unsigned int irq)
  1431. {
  1432. unsigned long v;
  1433. v = apic_read(APIC_LVT0);
  1434. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1435. }
  1436. static void ack_lapic_irq (unsigned int irq)
  1437. {
  1438. ack_APIC_irq();
  1439. }
  1440. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1441. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1442. .typename = "local-APIC-edge",
  1443. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1444. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1445. .enable = enable_lapic_irq,
  1446. .disable = disable_lapic_irq,
  1447. .ack = ack_lapic_irq,
  1448. .end = end_lapic_irq,
  1449. };
  1450. static void setup_nmi (void)
  1451. {
  1452. /*
  1453. * Dirty trick to enable the NMI watchdog ...
  1454. * We put the 8259A master into AEOI mode and
  1455. * unmask on all local APICs LVT0 as NMI.
  1456. *
  1457. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1458. * is from Maciej W. Rozycki - so we do not have to EOI from
  1459. * the NMI handler or the timer interrupt.
  1460. */
  1461. printk(KERN_INFO "activating NMI Watchdog ...");
  1462. enable_NMI_through_LVT0(NULL);
  1463. printk(" done.\n");
  1464. }
  1465. /*
  1466. * This looks a bit hackish but it's about the only one way of sending
  1467. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1468. * not support the ExtINT mode, unfortunately. We need to send these
  1469. * cycles as some i82489DX-based boards have glue logic that keeps the
  1470. * 8259A interrupt line asserted until INTA. --macro
  1471. */
  1472. static inline void unlock_ExtINT_logic(void)
  1473. {
  1474. int apic, pin, i;
  1475. struct IO_APIC_route_entry entry0, entry1;
  1476. unsigned char save_control, save_freq_select;
  1477. unsigned long flags;
  1478. pin = find_isa_irq_pin(8, mp_INT);
  1479. apic = find_isa_irq_apic(8, mp_INT);
  1480. if (pin == -1)
  1481. return;
  1482. spin_lock_irqsave(&ioapic_lock, flags);
  1483. *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1484. *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1485. spin_unlock_irqrestore(&ioapic_lock, flags);
  1486. clear_IO_APIC_pin(apic, pin);
  1487. memset(&entry1, 0, sizeof(entry1));
  1488. entry1.dest_mode = 0; /* physical delivery */
  1489. entry1.mask = 0; /* unmask IRQ now */
  1490. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1491. entry1.delivery_mode = dest_ExtINT;
  1492. entry1.polarity = entry0.polarity;
  1493. entry1.trigger = 0;
  1494. entry1.vector = 0;
  1495. spin_lock_irqsave(&ioapic_lock, flags);
  1496. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1497. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1498. spin_unlock_irqrestore(&ioapic_lock, flags);
  1499. save_control = CMOS_READ(RTC_CONTROL);
  1500. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1501. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1502. RTC_FREQ_SELECT);
  1503. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1504. i = 100;
  1505. while (i-- > 0) {
  1506. mdelay(10);
  1507. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1508. i -= 10;
  1509. }
  1510. CMOS_WRITE(save_control, RTC_CONTROL);
  1511. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1512. clear_IO_APIC_pin(apic, pin);
  1513. spin_lock_irqsave(&ioapic_lock, flags);
  1514. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1515. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1516. spin_unlock_irqrestore(&ioapic_lock, flags);
  1517. }
  1518. /*
  1519. * This code may look a bit paranoid, but it's supposed to cooperate with
  1520. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1521. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1522. * fanatically on his truly buggy board.
  1523. *
  1524. * FIXME: really need to revamp this for modern platforms only.
  1525. */
  1526. static inline void check_timer(void)
  1527. {
  1528. int apic1, pin1, apic2, pin2;
  1529. int vector;
  1530. /*
  1531. * get/set the timer IRQ vector:
  1532. */
  1533. disable_8259A_irq(0);
  1534. vector = assign_irq_vector(0);
  1535. set_intr_gate(vector, interrupt[0]);
  1536. /*
  1537. * Subtle, code in do_timer_interrupt() expects an AEOI
  1538. * mode for the 8259A whenever interrupts are routed
  1539. * through I/O APICs. Also IRQ0 has to be enabled in
  1540. * the 8259A which implies the virtual wire has to be
  1541. * disabled in the local APIC.
  1542. */
  1543. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1544. init_8259A(1);
  1545. if (timer_over_8254 > 0)
  1546. enable_8259A_irq(0);
  1547. pin1 = find_isa_irq_pin(0, mp_INT);
  1548. apic1 = find_isa_irq_apic(0, mp_INT);
  1549. pin2 = ioapic_i8259.pin;
  1550. apic2 = ioapic_i8259.apic;
  1551. apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1552. vector, apic1, pin1, apic2, pin2);
  1553. if (pin1 != -1) {
  1554. /*
  1555. * Ok, does IRQ0 through the IOAPIC work?
  1556. */
  1557. unmask_IO_APIC_irq(0);
  1558. if (!no_timer_check && timer_irq_works()) {
  1559. nmi_watchdog_default();
  1560. if (nmi_watchdog == NMI_IO_APIC) {
  1561. disable_8259A_irq(0);
  1562. setup_nmi();
  1563. enable_8259A_irq(0);
  1564. }
  1565. if (disable_timer_pin_1 > 0)
  1566. clear_IO_APIC_pin(0, pin1);
  1567. return;
  1568. }
  1569. clear_IO_APIC_pin(apic1, pin1);
  1570. apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
  1571. "connected to IO-APIC\n");
  1572. }
  1573. apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
  1574. "through the 8259A ... ");
  1575. if (pin2 != -1) {
  1576. apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
  1577. apic2, pin2);
  1578. /*
  1579. * legacy devices should be connected to IO APIC #0
  1580. */
  1581. setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
  1582. if (timer_irq_works()) {
  1583. apic_printk(APIC_VERBOSE," works.\n");
  1584. nmi_watchdog_default();
  1585. if (nmi_watchdog == NMI_IO_APIC) {
  1586. setup_nmi();
  1587. }
  1588. return;
  1589. }
  1590. /*
  1591. * Cleanup, just in case ...
  1592. */
  1593. clear_IO_APIC_pin(apic2, pin2);
  1594. }
  1595. apic_printk(APIC_VERBOSE," failed.\n");
  1596. if (nmi_watchdog == NMI_IO_APIC) {
  1597. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1598. nmi_watchdog = 0;
  1599. }
  1600. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1601. disable_8259A_irq(0);
  1602. irq_desc[0].handler = &lapic_irq_type;
  1603. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1604. enable_8259A_irq(0);
  1605. if (timer_irq_works()) {
  1606. apic_printk(APIC_VERBOSE," works.\n");
  1607. return;
  1608. }
  1609. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1610. apic_printk(APIC_VERBOSE," failed.\n");
  1611. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1612. init_8259A(0);
  1613. make_8259A_irq(0);
  1614. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1615. unlock_ExtINT_logic();
  1616. if (timer_irq_works()) {
  1617. apic_printk(APIC_VERBOSE," works.\n");
  1618. return;
  1619. }
  1620. apic_printk(APIC_VERBOSE," failed :(.\n");
  1621. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1622. }
  1623. static int __init notimercheck(char *s)
  1624. {
  1625. no_timer_check = 1;
  1626. return 1;
  1627. }
  1628. __setup("no_timer_check", notimercheck);
  1629. /*
  1630. *
  1631. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1632. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1633. * Linux doesn't really care, as it's not actually used
  1634. * for any interrupt handling anyway.
  1635. */
  1636. #define PIC_IRQS (1<<2)
  1637. void __init setup_IO_APIC(void)
  1638. {
  1639. enable_IO_APIC();
  1640. if (acpi_ioapic)
  1641. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1642. else
  1643. io_apic_irqs = ~PIC_IRQS;
  1644. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1645. /*
  1646. * Set up the IO-APIC IRQ routing table.
  1647. */
  1648. if (!acpi_ioapic)
  1649. setup_ioapic_ids_from_mpc();
  1650. sync_Arb_IDs();
  1651. setup_IO_APIC_irqs();
  1652. init_IO_APIC_traps();
  1653. check_timer();
  1654. if (!acpi_ioapic)
  1655. print_IO_APIC();
  1656. }
  1657. struct sysfs_ioapic_data {
  1658. struct sys_device dev;
  1659. struct IO_APIC_route_entry entry[0];
  1660. };
  1661. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1662. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1663. {
  1664. struct IO_APIC_route_entry *entry;
  1665. struct sysfs_ioapic_data *data;
  1666. unsigned long flags;
  1667. int i;
  1668. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1669. entry = data->entry;
  1670. spin_lock_irqsave(&ioapic_lock, flags);
  1671. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
  1672. *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
  1673. *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
  1674. }
  1675. spin_unlock_irqrestore(&ioapic_lock, flags);
  1676. return 0;
  1677. }
  1678. static int ioapic_resume(struct sys_device *dev)
  1679. {
  1680. struct IO_APIC_route_entry *entry;
  1681. struct sysfs_ioapic_data *data;
  1682. unsigned long flags;
  1683. union IO_APIC_reg_00 reg_00;
  1684. int i;
  1685. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1686. entry = data->entry;
  1687. spin_lock_irqsave(&ioapic_lock, flags);
  1688. reg_00.raw = io_apic_read(dev->id, 0);
  1689. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1690. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1691. io_apic_write(dev->id, 0, reg_00.raw);
  1692. }
  1693. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
  1694. io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
  1695. io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
  1696. }
  1697. spin_unlock_irqrestore(&ioapic_lock, flags);
  1698. return 0;
  1699. }
  1700. static struct sysdev_class ioapic_sysdev_class = {
  1701. set_kset_name("ioapic"),
  1702. .suspend = ioapic_suspend,
  1703. .resume = ioapic_resume,
  1704. };
  1705. static int __init ioapic_init_sysfs(void)
  1706. {
  1707. struct sys_device * dev;
  1708. int i, size, error = 0;
  1709. error = sysdev_class_register(&ioapic_sysdev_class);
  1710. if (error)
  1711. return error;
  1712. for (i = 0; i < nr_ioapics; i++ ) {
  1713. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1714. * sizeof(struct IO_APIC_route_entry);
  1715. mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
  1716. if (!mp_ioapic_data[i]) {
  1717. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1718. continue;
  1719. }
  1720. memset(mp_ioapic_data[i], 0, size);
  1721. dev = &mp_ioapic_data[i]->dev;
  1722. dev->id = i;
  1723. dev->cls = &ioapic_sysdev_class;
  1724. error = sysdev_register(dev);
  1725. if (error) {
  1726. kfree(mp_ioapic_data[i]);
  1727. mp_ioapic_data[i] = NULL;
  1728. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1729. continue;
  1730. }
  1731. }
  1732. return 0;
  1733. }
  1734. device_initcall(ioapic_init_sysfs);
  1735. /* --------------------------------------------------------------------------
  1736. ACPI-based IOAPIC Configuration
  1737. -------------------------------------------------------------------------- */
  1738. #ifdef CONFIG_ACPI
  1739. #define IO_APIC_MAX_ID 0xFE
  1740. int __init io_apic_get_version (int ioapic)
  1741. {
  1742. union IO_APIC_reg_01 reg_01;
  1743. unsigned long flags;
  1744. spin_lock_irqsave(&ioapic_lock, flags);
  1745. reg_01.raw = io_apic_read(ioapic, 1);
  1746. spin_unlock_irqrestore(&ioapic_lock, flags);
  1747. return reg_01.bits.version;
  1748. }
  1749. int __init io_apic_get_redir_entries (int ioapic)
  1750. {
  1751. union IO_APIC_reg_01 reg_01;
  1752. unsigned long flags;
  1753. spin_lock_irqsave(&ioapic_lock, flags);
  1754. reg_01.raw = io_apic_read(ioapic, 1);
  1755. spin_unlock_irqrestore(&ioapic_lock, flags);
  1756. return reg_01.bits.entries;
  1757. }
  1758. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1759. {
  1760. struct IO_APIC_route_entry entry;
  1761. unsigned long flags;
  1762. if (!IO_APIC_IRQ(irq)) {
  1763. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1764. ioapic);
  1765. return -EINVAL;
  1766. }
  1767. /*
  1768. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  1769. * Note that we mask (disable) IRQs now -- these get enabled when the
  1770. * corresponding device driver registers for this IRQ.
  1771. */
  1772. memset(&entry,0,sizeof(entry));
  1773. entry.delivery_mode = INT_DELIVERY_MODE;
  1774. entry.dest_mode = INT_DEST_MODE;
  1775. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  1776. entry.trigger = triggering;
  1777. entry.polarity = polarity;
  1778. entry.mask = 1; /* Disabled (masked) */
  1779. irq = gsi_irq_sharing(irq);
  1780. /*
  1781. * IRQs < 16 are already in the irq_2_pin[] map
  1782. */
  1783. if (irq >= 16)
  1784. add_pin_to_irq(irq, ioapic, pin);
  1785. entry.vector = assign_irq_vector(irq);
  1786. apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
  1787. "IRQ %d Mode:%i Active:%i)\n", ioapic,
  1788. mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
  1789. triggering, polarity);
  1790. ioapic_register_intr(irq, entry.vector, triggering);
  1791. if (!ioapic && (irq < 16))
  1792. disable_8259A_irq(irq);
  1793. spin_lock_irqsave(&ioapic_lock, flags);
  1794. io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
  1795. io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
  1796. set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
  1797. spin_unlock_irqrestore(&ioapic_lock, flags);
  1798. return 0;
  1799. }
  1800. #endif /* CONFIG_ACPI */
  1801. /*
  1802. * This function currently is only a helper for the i386 smp boot process where
  1803. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1804. * so mask in all cases should simply be TARGET_CPUS
  1805. */
  1806. #ifdef CONFIG_SMP
  1807. void __init setup_ioapic_dest(void)
  1808. {
  1809. int pin, ioapic, irq, irq_entry;
  1810. if (skip_ioapic_setup == 1)
  1811. return;
  1812. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1813. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1814. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1815. if (irq_entry == -1)
  1816. continue;
  1817. irq = pin_2_irq(irq_entry, ioapic, pin);
  1818. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1819. }
  1820. }
  1821. }
  1822. #endif