io_apic.c 49 KB

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