io_apic.c 54 KB

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