io_apic.c 50 KB

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