io_apic.c 47 KB

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