io_apic.c 53 KB

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