io_apic.c 53 KB

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