io_apic.c 49 KB

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