io_apic.c 52 KB

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