io_apic_64.c 57 KB

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