io_apic_64.c 57 KB

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