io_apic_64.c 55 KB

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