io_apic.c 53 KB

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