io_apic_64.c 58 KB

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