io_apic.c 52 KB

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