io_apic_64.c 58 KB

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