io_apic.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  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 int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  532. int old_vector = -1;
  533. int cpu;
  534. BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
  535. if (irq_vector[irq] > 0)
  536. old_vector = irq_vector[irq];
  537. if (old_vector > 0) {
  538. cpus_and(*result, irq_domain[irq], mask);
  539. if (!cpus_empty(*result))
  540. return old_vector;
  541. }
  542. for_each_cpu_mask(cpu, mask) {
  543. cpumask_t domain;
  544. int new_cpu;
  545. int vector, offset;
  546. domain = vector_allocation_domain(cpu);
  547. vector = current_vector;
  548. offset = current_offset;
  549. next:
  550. vector += 8;
  551. if (vector >= FIRST_SYSTEM_VECTOR) {
  552. /* If we run out of vectors on large boxen, must share them. */
  553. offset = (offset + 1) % 8;
  554. vector = FIRST_DEVICE_VECTOR + offset;
  555. }
  556. if (unlikely(current_vector == vector))
  557. continue;
  558. if (vector == IA32_SYSCALL_VECTOR)
  559. goto next;
  560. for_each_cpu_mask(new_cpu, domain)
  561. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  562. goto next;
  563. /* Found one! */
  564. current_vector = vector;
  565. current_offset = offset;
  566. if (old_vector >= 0) {
  567. int old_cpu;
  568. for_each_cpu_mask(old_cpu, irq_domain[irq])
  569. per_cpu(vector_irq, old_cpu)[old_vector] = -1;
  570. }
  571. for_each_cpu_mask(new_cpu, domain)
  572. per_cpu(vector_irq, new_cpu)[vector] = irq;
  573. irq_vector[irq] = vector;
  574. irq_domain[irq] = domain;
  575. cpus_and(*result, domain, mask);
  576. return vector;
  577. }
  578. return -ENOSPC;
  579. }
  580. static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
  581. {
  582. int vector;
  583. unsigned long flags;
  584. spin_lock_irqsave(&vector_lock, flags);
  585. vector = __assign_irq_vector(irq, mask, result);
  586. spin_unlock_irqrestore(&vector_lock, flags);
  587. return vector;
  588. }
  589. extern void (*interrupt[NR_IRQS])(void);
  590. static struct irq_chip ioapic_chip;
  591. #define IOAPIC_AUTO -1
  592. #define IOAPIC_EDGE 0
  593. #define IOAPIC_LEVEL 1
  594. static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  595. {
  596. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  597. trigger == IOAPIC_LEVEL)
  598. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  599. handle_fasteoi_irq, "fasteoi");
  600. else
  601. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  602. handle_edge_irq, "edge");
  603. }
  604. static void __init setup_IO_APIC_irqs(void)
  605. {
  606. struct IO_APIC_route_entry entry;
  607. int apic, pin, idx, irq, first_notcon = 1, vector;
  608. unsigned long flags;
  609. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  610. for (apic = 0; apic < nr_ioapics; apic++) {
  611. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  612. /*
  613. * add it to the IO-APIC irq-routing table:
  614. */
  615. memset(&entry,0,sizeof(entry));
  616. entry.delivery_mode = INT_DELIVERY_MODE;
  617. entry.dest_mode = INT_DEST_MODE;
  618. entry.mask = 0; /* enable IRQ */
  619. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  620. idx = find_irq_entry(apic,pin,mp_INT);
  621. if (idx == -1) {
  622. if (first_notcon) {
  623. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  624. first_notcon = 0;
  625. } else
  626. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  627. continue;
  628. }
  629. entry.trigger = irq_trigger(idx);
  630. entry.polarity = irq_polarity(idx);
  631. if (irq_trigger(idx)) {
  632. entry.trigger = 1;
  633. entry.mask = 1;
  634. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  635. }
  636. irq = pin_2_irq(idx, apic, pin);
  637. add_pin_to_irq(irq, apic, pin);
  638. if (!apic && !IO_APIC_IRQ(irq))
  639. continue;
  640. if (IO_APIC_IRQ(irq)) {
  641. cpumask_t mask;
  642. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  643. if (vector < 0)
  644. continue;
  645. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  646. entry.vector = vector;
  647. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  648. if (!apic && (irq < 16))
  649. disable_8259A_irq(irq);
  650. }
  651. ioapic_write_entry(apic, pin, entry);
  652. spin_lock_irqsave(&ioapic_lock, flags);
  653. set_native_irq_info(irq, TARGET_CPUS);
  654. spin_unlock_irqrestore(&ioapic_lock, flags);
  655. }
  656. }
  657. if (!first_notcon)
  658. apic_printk(APIC_VERBOSE," not connected.\n");
  659. }
  660. /*
  661. * Set up the 8259A-master output pin as broadcast to all
  662. * CPUs.
  663. */
  664. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  665. {
  666. struct IO_APIC_route_entry entry;
  667. unsigned long flags;
  668. memset(&entry,0,sizeof(entry));
  669. disable_8259A_irq(0);
  670. /* mask LVT0 */
  671. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  672. /*
  673. * We use logical delivery to get the timer IRQ
  674. * to the first CPU.
  675. */
  676. entry.dest_mode = INT_DEST_MODE;
  677. entry.mask = 0; /* unmask IRQ now */
  678. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  679. entry.delivery_mode = INT_DELIVERY_MODE;
  680. entry.polarity = 0;
  681. entry.trigger = 0;
  682. entry.vector = vector;
  683. /*
  684. * The timer IRQ doesn't have to know that behind the
  685. * scene we have a 8259A-master in AEOI mode ...
  686. */
  687. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  688. /*
  689. * Add it to the IO-APIC irq-routing table:
  690. */
  691. spin_lock_irqsave(&ioapic_lock, flags);
  692. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  693. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  694. spin_unlock_irqrestore(&ioapic_lock, flags);
  695. enable_8259A_irq(0);
  696. }
  697. void __init UNEXPECTED_IO_APIC(void)
  698. {
  699. }
  700. void __apicdebuginit print_IO_APIC(void)
  701. {
  702. int apic, i;
  703. union IO_APIC_reg_00 reg_00;
  704. union IO_APIC_reg_01 reg_01;
  705. union IO_APIC_reg_02 reg_02;
  706. unsigned long flags;
  707. if (apic_verbosity == APIC_QUIET)
  708. return;
  709. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  710. for (i = 0; i < nr_ioapics; i++)
  711. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  712. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  713. /*
  714. * We are a bit conservative about what we expect. We have to
  715. * know about every hardware change ASAP.
  716. */
  717. printk(KERN_INFO "testing the IO APIC.......................\n");
  718. for (apic = 0; apic < nr_ioapics; apic++) {
  719. spin_lock_irqsave(&ioapic_lock, flags);
  720. reg_00.raw = io_apic_read(apic, 0);
  721. reg_01.raw = io_apic_read(apic, 1);
  722. if (reg_01.bits.version >= 0x10)
  723. reg_02.raw = io_apic_read(apic, 2);
  724. spin_unlock_irqrestore(&ioapic_lock, flags);
  725. printk("\n");
  726. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  727. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  728. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  729. if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
  730. UNEXPECTED_IO_APIC();
  731. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  732. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  733. if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
  734. (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
  735. (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
  736. (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
  737. (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
  738. (reg_01.bits.entries != 0x2E) &&
  739. (reg_01.bits.entries != 0x3F) &&
  740. (reg_01.bits.entries != 0x03)
  741. )
  742. UNEXPECTED_IO_APIC();
  743. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  744. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  745. if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
  746. (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
  747. (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
  748. (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
  749. (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
  750. (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
  751. )
  752. UNEXPECTED_IO_APIC();
  753. if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
  754. UNEXPECTED_IO_APIC();
  755. if (reg_01.bits.version >= 0x10) {
  756. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  757. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  758. if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
  759. UNEXPECTED_IO_APIC();
  760. }
  761. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  762. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  763. " Stat Dest Deli Vect: \n");
  764. for (i = 0; i <= reg_01.bits.entries; i++) {
  765. struct IO_APIC_route_entry entry;
  766. entry = ioapic_read_entry(apic, i);
  767. printk(KERN_DEBUG " %02x %03X %02X ",
  768. i,
  769. entry.dest.logical.logical_dest,
  770. entry.dest.physical.physical_dest
  771. );
  772. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  773. entry.mask,
  774. entry.trigger,
  775. entry.irr,
  776. entry.polarity,
  777. entry.delivery_status,
  778. entry.dest_mode,
  779. entry.delivery_mode,
  780. entry.vector
  781. );
  782. }
  783. }
  784. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  785. for (i = 0; i < NR_IRQS; i++) {
  786. struct irq_pin_list *entry = irq_2_pin + i;
  787. if (entry->pin < 0)
  788. continue;
  789. printk(KERN_DEBUG "IRQ%d ", i);
  790. for (;;) {
  791. printk("-> %d:%d", entry->apic, entry->pin);
  792. if (!entry->next)
  793. break;
  794. entry = irq_2_pin + entry->next;
  795. }
  796. printk("\n");
  797. }
  798. printk(KERN_INFO ".................................... done.\n");
  799. return;
  800. }
  801. #if 0
  802. static __apicdebuginit void print_APIC_bitfield (int base)
  803. {
  804. unsigned int v;
  805. int i, j;
  806. if (apic_verbosity == APIC_QUIET)
  807. return;
  808. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  809. for (i = 0; i < 8; i++) {
  810. v = apic_read(base + i*0x10);
  811. for (j = 0; j < 32; j++) {
  812. if (v & (1<<j))
  813. printk("1");
  814. else
  815. printk("0");
  816. }
  817. printk("\n");
  818. }
  819. }
  820. void __apicdebuginit print_local_APIC(void * dummy)
  821. {
  822. unsigned int v, ver, maxlvt;
  823. if (apic_verbosity == APIC_QUIET)
  824. return;
  825. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  826. smp_processor_id(), hard_smp_processor_id());
  827. v = apic_read(APIC_ID);
  828. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  829. v = apic_read(APIC_LVR);
  830. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  831. ver = GET_APIC_VERSION(v);
  832. maxlvt = get_maxlvt();
  833. v = apic_read(APIC_TASKPRI);
  834. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  835. v = apic_read(APIC_ARBPRI);
  836. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  837. v & APIC_ARBPRI_MASK);
  838. v = apic_read(APIC_PROCPRI);
  839. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  840. v = apic_read(APIC_EOI);
  841. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  842. v = apic_read(APIC_RRR);
  843. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  844. v = apic_read(APIC_LDR);
  845. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  846. v = apic_read(APIC_DFR);
  847. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  848. v = apic_read(APIC_SPIV);
  849. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  850. printk(KERN_DEBUG "... APIC ISR field:\n");
  851. print_APIC_bitfield(APIC_ISR);
  852. printk(KERN_DEBUG "... APIC TMR field:\n");
  853. print_APIC_bitfield(APIC_TMR);
  854. printk(KERN_DEBUG "... APIC IRR field:\n");
  855. print_APIC_bitfield(APIC_IRR);
  856. v = apic_read(APIC_ESR);
  857. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  858. v = apic_read(APIC_ICR);
  859. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  860. v = apic_read(APIC_ICR2);
  861. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  862. v = apic_read(APIC_LVTT);
  863. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  864. if (maxlvt > 3) { /* PC is LVT#4. */
  865. v = apic_read(APIC_LVTPC);
  866. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  867. }
  868. v = apic_read(APIC_LVT0);
  869. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  870. v = apic_read(APIC_LVT1);
  871. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  872. if (maxlvt > 2) { /* ERR is LVT#3. */
  873. v = apic_read(APIC_LVTERR);
  874. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  875. }
  876. v = apic_read(APIC_TMICT);
  877. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  878. v = apic_read(APIC_TMCCT);
  879. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  880. v = apic_read(APIC_TDCR);
  881. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  882. printk("\n");
  883. }
  884. void print_all_local_APICs (void)
  885. {
  886. on_each_cpu(print_local_APIC, NULL, 1, 1);
  887. }
  888. void __apicdebuginit print_PIC(void)
  889. {
  890. unsigned int v;
  891. unsigned long flags;
  892. if (apic_verbosity == APIC_QUIET)
  893. return;
  894. printk(KERN_DEBUG "\nprinting PIC contents\n");
  895. spin_lock_irqsave(&i8259A_lock, flags);
  896. v = inb(0xa1) << 8 | inb(0x21);
  897. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  898. v = inb(0xa0) << 8 | inb(0x20);
  899. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  900. outb(0x0b,0xa0);
  901. outb(0x0b,0x20);
  902. v = inb(0xa0) << 8 | inb(0x20);
  903. outb(0x0a,0xa0);
  904. outb(0x0a,0x20);
  905. spin_unlock_irqrestore(&i8259A_lock, flags);
  906. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  907. v = inb(0x4d1) << 8 | inb(0x4d0);
  908. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  909. }
  910. #endif /* 0 */
  911. static void __init enable_IO_APIC(void)
  912. {
  913. union IO_APIC_reg_01 reg_01;
  914. int i8259_apic, i8259_pin;
  915. int i, apic;
  916. unsigned long flags;
  917. for (i = 0; i < PIN_MAP_SIZE; i++) {
  918. irq_2_pin[i].pin = -1;
  919. irq_2_pin[i].next = 0;
  920. }
  921. /*
  922. * The number of IO-APIC IRQ registers (== #pins):
  923. */
  924. for (apic = 0; apic < nr_ioapics; apic++) {
  925. spin_lock_irqsave(&ioapic_lock, flags);
  926. reg_01.raw = io_apic_read(apic, 1);
  927. spin_unlock_irqrestore(&ioapic_lock, flags);
  928. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  929. }
  930. for(apic = 0; apic < nr_ioapics; apic++) {
  931. int pin;
  932. /* See if any of the pins is in ExtINT mode */
  933. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  934. struct IO_APIC_route_entry entry;
  935. entry = ioapic_read_entry(apic, pin);
  936. /* If the interrupt line is enabled and in ExtInt mode
  937. * I have found the pin where the i8259 is connected.
  938. */
  939. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  940. ioapic_i8259.apic = apic;
  941. ioapic_i8259.pin = pin;
  942. goto found_i8259;
  943. }
  944. }
  945. }
  946. found_i8259:
  947. /* Look to see what if the MP table has reported the ExtINT */
  948. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  949. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  950. /* Trust the MP table if nothing is setup in the hardware */
  951. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  952. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  953. ioapic_i8259.pin = i8259_pin;
  954. ioapic_i8259.apic = i8259_apic;
  955. }
  956. /* Complain if the MP table and the hardware disagree */
  957. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  958. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  959. {
  960. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  961. }
  962. /*
  963. * Do not trust the IO-APIC being empty at bootup
  964. */
  965. clear_IO_APIC();
  966. }
  967. /*
  968. * Not an __init, needed by the reboot code
  969. */
  970. void disable_IO_APIC(void)
  971. {
  972. /*
  973. * Clear the IO-APIC before rebooting:
  974. */
  975. clear_IO_APIC();
  976. /*
  977. * If the i8259 is routed through an IOAPIC
  978. * Put that IOAPIC in virtual wire mode
  979. * so legacy interrupts can be delivered.
  980. */
  981. if (ioapic_i8259.pin != -1) {
  982. struct IO_APIC_route_entry entry;
  983. memset(&entry, 0, sizeof(entry));
  984. entry.mask = 0; /* Enabled */
  985. entry.trigger = 0; /* Edge */
  986. entry.irr = 0;
  987. entry.polarity = 0; /* High */
  988. entry.delivery_status = 0;
  989. entry.dest_mode = 0; /* Physical */
  990. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  991. entry.vector = 0;
  992. entry.dest.physical.physical_dest =
  993. GET_APIC_ID(apic_read(APIC_ID));
  994. /*
  995. * Add it to the IO-APIC irq-routing table:
  996. */
  997. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  998. }
  999. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1000. }
  1001. /*
  1002. * There is a nasty bug in some older SMP boards, their mptable lies
  1003. * about the timer IRQ. We do the following to work around the situation:
  1004. *
  1005. * - timer IRQ defaults to IO-APIC IRQ
  1006. * - if this function detects that timer IRQs are defunct, then we fall
  1007. * back to ISA timer IRQs
  1008. */
  1009. static int __init timer_irq_works(void)
  1010. {
  1011. unsigned long t1 = jiffies;
  1012. local_irq_enable();
  1013. /* Let ten ticks pass... */
  1014. mdelay((10 * 1000) / HZ);
  1015. /*
  1016. * Expect a few ticks at least, to be sure some possible
  1017. * glue logic does not lock up after one or two first
  1018. * ticks in a non-ExtINT mode. Also the local APIC
  1019. * might have cached one ExtINT interrupt. Finally, at
  1020. * least one tick may be lost due to delays.
  1021. */
  1022. /* jiffies wrap? */
  1023. if (jiffies - t1 > 4)
  1024. return 1;
  1025. return 0;
  1026. }
  1027. /*
  1028. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1029. * number of pending IRQ events unhandled. These cases are very rare,
  1030. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1031. * better to do it this way as thus we do not have to be aware of
  1032. * 'pending' interrupts in the IRQ path, except at this point.
  1033. */
  1034. /*
  1035. * Edge triggered needs to resend any interrupt
  1036. * that was delayed but this is now handled in the device
  1037. * independent code.
  1038. */
  1039. /*
  1040. * Starting up a edge-triggered IO-APIC interrupt is
  1041. * nasty - we need to make sure that we get the edge.
  1042. * If it is already asserted for some reason, we need
  1043. * return 1 to indicate that is was pending.
  1044. *
  1045. * This is not complete - we should be able to fake
  1046. * an edge even if it isn't on the 8259A...
  1047. */
  1048. static unsigned int startup_ioapic_irq(unsigned int irq)
  1049. {
  1050. int was_pending = 0;
  1051. unsigned long flags;
  1052. spin_lock_irqsave(&ioapic_lock, flags);
  1053. if (irq < 16) {
  1054. disable_8259A_irq(irq);
  1055. if (i8259A_irq_pending(irq))
  1056. was_pending = 1;
  1057. }
  1058. __unmask_IO_APIC_irq(irq);
  1059. spin_unlock_irqrestore(&ioapic_lock, flags);
  1060. return was_pending;
  1061. }
  1062. static int ioapic_retrigger_irq(unsigned int irq)
  1063. {
  1064. cpumask_t mask;
  1065. unsigned vector;
  1066. unsigned long flags;
  1067. spin_lock_irqsave(&vector_lock, flags);
  1068. vector = irq_vector[irq];
  1069. cpus_clear(mask);
  1070. cpu_set(first_cpu(irq_domain[irq]), mask);
  1071. send_IPI_mask(mask, vector);
  1072. spin_unlock_irqrestore(&vector_lock, flags);
  1073. return 1;
  1074. }
  1075. /*
  1076. * Level and edge triggered IO-APIC interrupts need different handling,
  1077. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1078. * handled with the level-triggered descriptor, but that one has slightly
  1079. * more overhead. Level-triggered interrupts cannot be handled with the
  1080. * edge-triggered handler, without risking IRQ storms and other ugly
  1081. * races.
  1082. */
  1083. static void ack_apic_edge(unsigned int irq)
  1084. {
  1085. move_native_irq(irq);
  1086. ack_APIC_irq();
  1087. }
  1088. static void ack_apic_level(unsigned int irq)
  1089. {
  1090. int do_unmask_irq = 0;
  1091. #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
  1092. /* If we are moving the irq we need to mask it */
  1093. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1094. do_unmask_irq = 1;
  1095. mask_IO_APIC_irq(irq);
  1096. }
  1097. #endif
  1098. /*
  1099. * We must acknowledge the irq before we move it or the acknowledge will
  1100. * not propogate properly.
  1101. */
  1102. ack_APIC_irq();
  1103. /* Now we can move and renable the irq */
  1104. move_masked_irq(irq);
  1105. if (unlikely(do_unmask_irq))
  1106. unmask_IO_APIC_irq(irq);
  1107. }
  1108. static struct irq_chip ioapic_chip __read_mostly = {
  1109. .name = "IO-APIC",
  1110. .startup = startup_ioapic_irq,
  1111. .mask = mask_IO_APIC_irq,
  1112. .unmask = unmask_IO_APIC_irq,
  1113. .ack = ack_apic_edge,
  1114. .eoi = ack_apic_level,
  1115. #ifdef CONFIG_SMP
  1116. .set_affinity = set_ioapic_affinity_irq,
  1117. #endif
  1118. .retrigger = ioapic_retrigger_irq,
  1119. };
  1120. static inline void init_IO_APIC_traps(void)
  1121. {
  1122. int irq;
  1123. /*
  1124. * NOTE! The local APIC isn't very good at handling
  1125. * multiple interrupts at the same interrupt level.
  1126. * As the interrupt level is determined by taking the
  1127. * vector number and shifting that right by 4, we
  1128. * want to spread these out a bit so that they don't
  1129. * all fall in the same interrupt level.
  1130. *
  1131. * Also, we've got to be careful not to trash gate
  1132. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1133. */
  1134. for (irq = 0; irq < NR_IRQS ; irq++) {
  1135. int tmp = irq;
  1136. if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
  1137. /*
  1138. * Hmm.. We don't have an entry for this,
  1139. * so default to an old-fashioned 8259
  1140. * interrupt if we can..
  1141. */
  1142. if (irq < 16)
  1143. make_8259A_irq(irq);
  1144. else
  1145. /* Strange. Oh, well.. */
  1146. irq_desc[irq].chip = &no_irq_chip;
  1147. }
  1148. }
  1149. }
  1150. static void enable_lapic_irq (unsigned int irq)
  1151. {
  1152. unsigned long v;
  1153. v = apic_read(APIC_LVT0);
  1154. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1155. }
  1156. static void disable_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 ack_lapic_irq (unsigned int irq)
  1163. {
  1164. ack_APIC_irq();
  1165. }
  1166. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1167. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1168. .typename = "local-APIC-edge",
  1169. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1170. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1171. .enable = enable_lapic_irq,
  1172. .disable = disable_lapic_irq,
  1173. .ack = ack_lapic_irq,
  1174. .end = end_lapic_irq,
  1175. };
  1176. static void setup_nmi (void)
  1177. {
  1178. /*
  1179. * Dirty trick to enable the NMI watchdog ...
  1180. * We put the 8259A master into AEOI mode and
  1181. * unmask on all local APICs LVT0 as NMI.
  1182. *
  1183. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1184. * is from Maciej W. Rozycki - so we do not have to EOI from
  1185. * the NMI handler or the timer interrupt.
  1186. */
  1187. printk(KERN_INFO "activating NMI Watchdog ...");
  1188. enable_NMI_through_LVT0(NULL);
  1189. printk(" done.\n");
  1190. }
  1191. /*
  1192. * This looks a bit hackish but it's about the only one way of sending
  1193. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1194. * not support the ExtINT mode, unfortunately. We need to send these
  1195. * cycles as some i82489DX-based boards have glue logic that keeps the
  1196. * 8259A interrupt line asserted until INTA. --macro
  1197. */
  1198. static inline void unlock_ExtINT_logic(void)
  1199. {
  1200. int apic, pin, i;
  1201. struct IO_APIC_route_entry entry0, entry1;
  1202. unsigned char save_control, save_freq_select;
  1203. unsigned long flags;
  1204. pin = find_isa_irq_pin(8, mp_INT);
  1205. apic = find_isa_irq_apic(8, mp_INT);
  1206. if (pin == -1)
  1207. return;
  1208. spin_lock_irqsave(&ioapic_lock, flags);
  1209. *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1210. *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1211. spin_unlock_irqrestore(&ioapic_lock, flags);
  1212. clear_IO_APIC_pin(apic, pin);
  1213. memset(&entry1, 0, sizeof(entry1));
  1214. entry1.dest_mode = 0; /* physical delivery */
  1215. entry1.mask = 0; /* unmask IRQ now */
  1216. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1217. entry1.delivery_mode = dest_ExtINT;
  1218. entry1.polarity = entry0.polarity;
  1219. entry1.trigger = 0;
  1220. entry1.vector = 0;
  1221. spin_lock_irqsave(&ioapic_lock, flags);
  1222. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1223. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1224. spin_unlock_irqrestore(&ioapic_lock, flags);
  1225. save_control = CMOS_READ(RTC_CONTROL);
  1226. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1227. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1228. RTC_FREQ_SELECT);
  1229. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1230. i = 100;
  1231. while (i-- > 0) {
  1232. mdelay(10);
  1233. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1234. i -= 10;
  1235. }
  1236. CMOS_WRITE(save_control, RTC_CONTROL);
  1237. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1238. clear_IO_APIC_pin(apic, pin);
  1239. spin_lock_irqsave(&ioapic_lock, flags);
  1240. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1241. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1242. spin_unlock_irqrestore(&ioapic_lock, flags);
  1243. }
  1244. /*
  1245. * This code may look a bit paranoid, but it's supposed to cooperate with
  1246. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1247. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1248. * fanatically on his truly buggy board.
  1249. *
  1250. * FIXME: really need to revamp this for modern platforms only.
  1251. */
  1252. static inline void check_timer(void)
  1253. {
  1254. int apic1, pin1, apic2, pin2;
  1255. int vector;
  1256. cpumask_t mask;
  1257. /*
  1258. * get/set the timer IRQ vector:
  1259. */
  1260. disable_8259A_irq(0);
  1261. vector = assign_irq_vector(0, TARGET_CPUS, &mask);
  1262. /*
  1263. * Subtle, code in do_timer_interrupt() expects an AEOI
  1264. * mode for the 8259A whenever interrupts are routed
  1265. * through I/O APICs. Also IRQ0 has to be enabled in
  1266. * the 8259A which implies the virtual wire has to be
  1267. * disabled in the local APIC.
  1268. */
  1269. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1270. init_8259A(1);
  1271. if (timer_over_8254 > 0)
  1272. enable_8259A_irq(0);
  1273. pin1 = find_isa_irq_pin(0, mp_INT);
  1274. apic1 = find_isa_irq_apic(0, mp_INT);
  1275. pin2 = ioapic_i8259.pin;
  1276. apic2 = ioapic_i8259.apic;
  1277. apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1278. vector, apic1, pin1, apic2, pin2);
  1279. if (pin1 != -1) {
  1280. /*
  1281. * Ok, does IRQ0 through the IOAPIC work?
  1282. */
  1283. unmask_IO_APIC_irq(0);
  1284. if (!no_timer_check && timer_irq_works()) {
  1285. nmi_watchdog_default();
  1286. if (nmi_watchdog == NMI_IO_APIC) {
  1287. disable_8259A_irq(0);
  1288. setup_nmi();
  1289. enable_8259A_irq(0);
  1290. }
  1291. if (disable_timer_pin_1 > 0)
  1292. clear_IO_APIC_pin(0, pin1);
  1293. return;
  1294. }
  1295. clear_IO_APIC_pin(apic1, pin1);
  1296. apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
  1297. "connected to IO-APIC\n");
  1298. }
  1299. apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
  1300. "through the 8259A ... ");
  1301. if (pin2 != -1) {
  1302. apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
  1303. apic2, pin2);
  1304. /*
  1305. * legacy devices should be connected to IO APIC #0
  1306. */
  1307. setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
  1308. if (timer_irq_works()) {
  1309. apic_printk(APIC_VERBOSE," works.\n");
  1310. nmi_watchdog_default();
  1311. if (nmi_watchdog == NMI_IO_APIC) {
  1312. setup_nmi();
  1313. }
  1314. return;
  1315. }
  1316. /*
  1317. * Cleanup, just in case ...
  1318. */
  1319. clear_IO_APIC_pin(apic2, pin2);
  1320. }
  1321. apic_printk(APIC_VERBOSE," failed.\n");
  1322. if (nmi_watchdog == NMI_IO_APIC) {
  1323. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1324. nmi_watchdog = 0;
  1325. }
  1326. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1327. disable_8259A_irq(0);
  1328. irq_desc[0].chip = &lapic_irq_type;
  1329. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1330. enable_8259A_irq(0);
  1331. if (timer_irq_works()) {
  1332. apic_printk(APIC_VERBOSE," works.\n");
  1333. return;
  1334. }
  1335. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1336. apic_printk(APIC_VERBOSE," failed.\n");
  1337. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1338. init_8259A(0);
  1339. make_8259A_irq(0);
  1340. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1341. unlock_ExtINT_logic();
  1342. if (timer_irq_works()) {
  1343. apic_printk(APIC_VERBOSE," works.\n");
  1344. return;
  1345. }
  1346. apic_printk(APIC_VERBOSE," failed :(.\n");
  1347. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1348. }
  1349. static int __init notimercheck(char *s)
  1350. {
  1351. no_timer_check = 1;
  1352. return 1;
  1353. }
  1354. __setup("no_timer_check", notimercheck);
  1355. /*
  1356. *
  1357. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1358. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1359. * Linux doesn't really care, as it's not actually used
  1360. * for any interrupt handling anyway.
  1361. */
  1362. #define PIC_IRQS (1<<2)
  1363. void __init setup_IO_APIC(void)
  1364. {
  1365. enable_IO_APIC();
  1366. if (acpi_ioapic)
  1367. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1368. else
  1369. io_apic_irqs = ~PIC_IRQS;
  1370. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1371. sync_Arb_IDs();
  1372. setup_IO_APIC_irqs();
  1373. init_IO_APIC_traps();
  1374. check_timer();
  1375. if (!acpi_ioapic)
  1376. print_IO_APIC();
  1377. }
  1378. struct sysfs_ioapic_data {
  1379. struct sys_device dev;
  1380. struct IO_APIC_route_entry entry[0];
  1381. };
  1382. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1383. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1384. {
  1385. struct IO_APIC_route_entry *entry;
  1386. struct sysfs_ioapic_data *data;
  1387. int i;
  1388. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1389. entry = data->entry;
  1390. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1391. *entry = ioapic_read_entry(dev->id, i);
  1392. return 0;
  1393. }
  1394. static int ioapic_resume(struct sys_device *dev)
  1395. {
  1396. struct IO_APIC_route_entry *entry;
  1397. struct sysfs_ioapic_data *data;
  1398. unsigned long flags;
  1399. union IO_APIC_reg_00 reg_00;
  1400. int i;
  1401. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1402. entry = data->entry;
  1403. spin_lock_irqsave(&ioapic_lock, flags);
  1404. reg_00.raw = io_apic_read(dev->id, 0);
  1405. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1406. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1407. io_apic_write(dev->id, 0, reg_00.raw);
  1408. }
  1409. spin_unlock_irqrestore(&ioapic_lock, flags);
  1410. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1411. ioapic_write_entry(dev->id, i, entry[i]);
  1412. return 0;
  1413. }
  1414. static struct sysdev_class ioapic_sysdev_class = {
  1415. set_kset_name("ioapic"),
  1416. .suspend = ioapic_suspend,
  1417. .resume = ioapic_resume,
  1418. };
  1419. static int __init ioapic_init_sysfs(void)
  1420. {
  1421. struct sys_device * dev;
  1422. int i, size, error = 0;
  1423. error = sysdev_class_register(&ioapic_sysdev_class);
  1424. if (error)
  1425. return error;
  1426. for (i = 0; i < nr_ioapics; i++ ) {
  1427. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1428. * sizeof(struct IO_APIC_route_entry);
  1429. mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
  1430. if (!mp_ioapic_data[i]) {
  1431. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1432. continue;
  1433. }
  1434. memset(mp_ioapic_data[i], 0, size);
  1435. dev = &mp_ioapic_data[i]->dev;
  1436. dev->id = i;
  1437. dev->cls = &ioapic_sysdev_class;
  1438. error = sysdev_register(dev);
  1439. if (error) {
  1440. kfree(mp_ioapic_data[i]);
  1441. mp_ioapic_data[i] = NULL;
  1442. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1443. continue;
  1444. }
  1445. }
  1446. return 0;
  1447. }
  1448. device_initcall(ioapic_init_sysfs);
  1449. /*
  1450. * Dynamic irq allocate and deallocation
  1451. */
  1452. int create_irq(void)
  1453. {
  1454. /* Allocate an unused irq */
  1455. int irq;
  1456. int new;
  1457. int vector = 0;
  1458. unsigned long flags;
  1459. cpumask_t mask;
  1460. irq = -ENOSPC;
  1461. spin_lock_irqsave(&vector_lock, flags);
  1462. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1463. if (platform_legacy_irq(new))
  1464. continue;
  1465. if (irq_vector[new] != 0)
  1466. continue;
  1467. vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
  1468. if (likely(vector > 0))
  1469. irq = new;
  1470. break;
  1471. }
  1472. spin_unlock_irqrestore(&vector_lock, flags);
  1473. if (irq >= 0) {
  1474. dynamic_irq_init(irq);
  1475. }
  1476. return irq;
  1477. }
  1478. void destroy_irq(unsigned int irq)
  1479. {
  1480. unsigned long flags;
  1481. dynamic_irq_cleanup(irq);
  1482. spin_lock_irqsave(&vector_lock, flags);
  1483. irq_vector[irq] = 0;
  1484. spin_unlock_irqrestore(&vector_lock, flags);
  1485. }
  1486. /*
  1487. * MSI mesage composition
  1488. */
  1489. #ifdef CONFIG_PCI_MSI
  1490. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1491. {
  1492. int vector;
  1493. unsigned dest;
  1494. cpumask_t tmp;
  1495. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1496. if (vector >= 0) {
  1497. dest = cpu_mask_to_apicid(tmp);
  1498. msg->address_hi = MSI_ADDR_BASE_HI;
  1499. msg->address_lo =
  1500. MSI_ADDR_BASE_LO |
  1501. ((INT_DEST_MODE == 0) ?
  1502. MSI_ADDR_DEST_MODE_PHYSICAL:
  1503. MSI_ADDR_DEST_MODE_LOGICAL) |
  1504. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1505. MSI_ADDR_REDIRECTION_CPU:
  1506. MSI_ADDR_REDIRECTION_LOWPRI) |
  1507. MSI_ADDR_DEST_ID(dest);
  1508. msg->data =
  1509. MSI_DATA_TRIGGER_EDGE |
  1510. MSI_DATA_LEVEL_ASSERT |
  1511. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1512. MSI_DATA_DELIVERY_FIXED:
  1513. MSI_DATA_DELIVERY_LOWPRI) |
  1514. MSI_DATA_VECTOR(vector);
  1515. }
  1516. return vector;
  1517. }
  1518. #ifdef CONFIG_SMP
  1519. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1520. {
  1521. struct msi_msg msg;
  1522. unsigned int dest;
  1523. cpumask_t tmp;
  1524. int vector;
  1525. cpus_and(tmp, mask, cpu_online_map);
  1526. if (cpus_empty(tmp))
  1527. tmp = TARGET_CPUS;
  1528. cpus_and(mask, tmp, CPU_MASK_ALL);
  1529. vector = assign_irq_vector(irq, mask, &tmp);
  1530. if (vector < 0)
  1531. return;
  1532. dest = cpu_mask_to_apicid(tmp);
  1533. read_msi_msg(irq, &msg);
  1534. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1535. msg.data |= MSI_DATA_VECTOR(vector);
  1536. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1537. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1538. write_msi_msg(irq, &msg);
  1539. set_native_irq_info(irq, mask);
  1540. }
  1541. #endif /* CONFIG_SMP */
  1542. /*
  1543. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  1544. * which implement the MSI or MSI-X Capability Structure.
  1545. */
  1546. static struct irq_chip msi_chip = {
  1547. .name = "PCI-MSI",
  1548. .unmask = unmask_msi_irq,
  1549. .mask = mask_msi_irq,
  1550. .ack = ack_apic_edge,
  1551. #ifdef CONFIG_SMP
  1552. .set_affinity = set_msi_irq_affinity,
  1553. #endif
  1554. .retrigger = ioapic_retrigger_irq,
  1555. };
  1556. int arch_setup_msi_irq(unsigned int irq, struct pci_dev *dev)
  1557. {
  1558. struct msi_msg msg;
  1559. int ret;
  1560. ret = msi_compose_msg(dev, irq, &msg);
  1561. if (ret < 0)
  1562. return ret;
  1563. write_msi_msg(irq, &msg);
  1564. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  1565. return 0;
  1566. }
  1567. void arch_teardown_msi_irq(unsigned int irq)
  1568. {
  1569. return;
  1570. }
  1571. #endif /* CONFIG_PCI_MSI */
  1572. /*
  1573. * Hypertransport interrupt support
  1574. */
  1575. #ifdef CONFIG_HT_IRQ
  1576. #ifdef CONFIG_SMP
  1577. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  1578. {
  1579. u32 low, high;
  1580. low = read_ht_irq_low(irq);
  1581. high = read_ht_irq_high(irq);
  1582. low &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  1583. high &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  1584. low |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  1585. high |= HT_IRQ_HIGH_DEST_ID(dest);
  1586. write_ht_irq_low(irq, low);
  1587. write_ht_irq_high(irq, high);
  1588. }
  1589. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  1590. {
  1591. unsigned int dest;
  1592. cpumask_t tmp;
  1593. int vector;
  1594. cpus_and(tmp, mask, cpu_online_map);
  1595. if (cpus_empty(tmp))
  1596. tmp = TARGET_CPUS;
  1597. cpus_and(mask, tmp, CPU_MASK_ALL);
  1598. vector = assign_irq_vector(irq, mask, &tmp);
  1599. if (vector < 0)
  1600. return;
  1601. dest = cpu_mask_to_apicid(tmp);
  1602. target_ht_irq(irq, dest, vector & 0xff);
  1603. set_native_irq_info(irq, mask);
  1604. }
  1605. #endif
  1606. static struct irq_chip ht_irq_chip = {
  1607. .name = "PCI-HT",
  1608. .mask = mask_ht_irq,
  1609. .unmask = unmask_ht_irq,
  1610. .ack = ack_apic_edge,
  1611. #ifdef CONFIG_SMP
  1612. .set_affinity = set_ht_irq_affinity,
  1613. #endif
  1614. .retrigger = ioapic_retrigger_irq,
  1615. };
  1616. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  1617. {
  1618. int vector;
  1619. cpumask_t tmp;
  1620. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1621. if (vector >= 0) {
  1622. u32 low, high;
  1623. unsigned dest;
  1624. dest = cpu_mask_to_apicid(tmp);
  1625. high = HT_IRQ_HIGH_DEST_ID(dest);
  1626. low = HT_IRQ_LOW_BASE |
  1627. HT_IRQ_LOW_DEST_ID(dest) |
  1628. HT_IRQ_LOW_VECTOR(vector) |
  1629. ((INT_DEST_MODE == 0) ?
  1630. HT_IRQ_LOW_DM_PHYSICAL :
  1631. HT_IRQ_LOW_DM_LOGICAL) |
  1632. HT_IRQ_LOW_RQEOI_EDGE |
  1633. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1634. HT_IRQ_LOW_MT_FIXED :
  1635. HT_IRQ_LOW_MT_ARBITRATED);
  1636. write_ht_irq_low(irq, low);
  1637. write_ht_irq_high(irq, high);
  1638. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  1639. handle_edge_irq, "edge");
  1640. }
  1641. return vector;
  1642. }
  1643. #endif /* CONFIG_HT_IRQ */
  1644. /* --------------------------------------------------------------------------
  1645. ACPI-based IOAPIC Configuration
  1646. -------------------------------------------------------------------------- */
  1647. #ifdef CONFIG_ACPI
  1648. #define IO_APIC_MAX_ID 0xFE
  1649. int __init io_apic_get_redir_entries (int ioapic)
  1650. {
  1651. union IO_APIC_reg_01 reg_01;
  1652. unsigned long flags;
  1653. spin_lock_irqsave(&ioapic_lock, flags);
  1654. reg_01.raw = io_apic_read(ioapic, 1);
  1655. spin_unlock_irqrestore(&ioapic_lock, flags);
  1656. return reg_01.bits.entries;
  1657. }
  1658. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1659. {
  1660. struct IO_APIC_route_entry entry;
  1661. unsigned long flags;
  1662. int vector;
  1663. cpumask_t mask;
  1664. if (!IO_APIC_IRQ(irq)) {
  1665. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1666. ioapic);
  1667. return -EINVAL;
  1668. }
  1669. /*
  1670. * IRQs < 16 are already in the irq_2_pin[] map
  1671. */
  1672. if (irq >= 16)
  1673. add_pin_to_irq(irq, ioapic, pin);
  1674. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  1675. if (vector < 0)
  1676. return vector;
  1677. /*
  1678. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  1679. * Note that we mask (disable) IRQs now -- these get enabled when the
  1680. * corresponding device driver registers for this IRQ.
  1681. */
  1682. memset(&entry,0,sizeof(entry));
  1683. entry.delivery_mode = INT_DELIVERY_MODE;
  1684. entry.dest_mode = INT_DEST_MODE;
  1685. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  1686. entry.trigger = triggering;
  1687. entry.polarity = polarity;
  1688. entry.mask = 1; /* Disabled (masked) */
  1689. entry.vector = vector & 0xff;
  1690. apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
  1691. "IRQ %d Mode:%i Active:%i)\n", ioapic,
  1692. mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
  1693. triggering, polarity);
  1694. ioapic_register_intr(irq, entry.vector, triggering);
  1695. if (!ioapic && (irq < 16))
  1696. disable_8259A_irq(irq);
  1697. ioapic_write_entry(ioapic, pin, entry);
  1698. spin_lock_irqsave(&ioapic_lock, flags);
  1699. set_native_irq_info(irq, TARGET_CPUS);
  1700. spin_unlock_irqrestore(&ioapic_lock, flags);
  1701. return 0;
  1702. }
  1703. #endif /* CONFIG_ACPI */
  1704. /*
  1705. * This function currently is only a helper for the i386 smp boot process where
  1706. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1707. * so mask in all cases should simply be TARGET_CPUS
  1708. */
  1709. #ifdef CONFIG_SMP
  1710. void __init setup_ioapic_dest(void)
  1711. {
  1712. int pin, ioapic, irq, irq_entry;
  1713. if (skip_ioapic_setup == 1)
  1714. return;
  1715. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1716. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1717. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1718. if (irq_entry == -1)
  1719. continue;
  1720. irq = pin_2_irq(irq_entry, ioapic, pin);
  1721. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1722. }
  1723. }
  1724. }
  1725. #endif