io_apic.c 50 KB

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