io_apic_64.c 57 KB

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