io_apic_64.c 58 KB

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