io_apic.c 52 KB

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