io_apic.c 53 KB

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