io_apic_64.c 58 KB

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