io_apic.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  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 = 1;
  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. 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 int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  532. int old_vector = -1;
  533. int cpu;
  534. BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
  535. /* Only try and allocate irqs on cpus that are present */
  536. cpus_and(mask, mask, cpu_online_map);
  537. if (irq_vector[irq] > 0)
  538. old_vector = irq_vector[irq];
  539. if (old_vector > 0) {
  540. cpus_and(*result, irq_domain[irq], mask);
  541. if (!cpus_empty(*result))
  542. return old_vector;
  543. }
  544. for_each_cpu_mask(cpu, mask) {
  545. cpumask_t domain, new_mask;
  546. int new_cpu;
  547. int vector, offset;
  548. domain = vector_allocation_domain(cpu);
  549. cpus_and(new_mask, domain, cpu_online_map);
  550. vector = current_vector;
  551. offset = current_offset;
  552. next:
  553. vector += 8;
  554. if (vector >= FIRST_SYSTEM_VECTOR) {
  555. /* If we run out of vectors on large boxen, must share them. */
  556. offset = (offset + 1) % 8;
  557. vector = FIRST_DEVICE_VECTOR + offset;
  558. }
  559. if (unlikely(current_vector == vector))
  560. continue;
  561. if (vector == IA32_SYSCALL_VECTOR)
  562. goto next;
  563. for_each_cpu_mask(new_cpu, new_mask)
  564. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  565. goto next;
  566. /* Found one! */
  567. current_vector = vector;
  568. current_offset = offset;
  569. if (old_vector >= 0) {
  570. cpumask_t old_mask;
  571. int old_cpu;
  572. cpus_and(old_mask, irq_domain[irq], cpu_online_map);
  573. for_each_cpu_mask(old_cpu, old_mask)
  574. per_cpu(vector_irq, old_cpu)[old_vector] = -1;
  575. }
  576. for_each_cpu_mask(new_cpu, new_mask)
  577. per_cpu(vector_irq, new_cpu)[vector] = irq;
  578. irq_vector[irq] = vector;
  579. irq_domain[irq] = domain;
  580. cpus_and(*result, domain, mask);
  581. return vector;
  582. }
  583. return -ENOSPC;
  584. }
  585. static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
  586. {
  587. int vector;
  588. unsigned long flags;
  589. spin_lock_irqsave(&vector_lock, flags);
  590. vector = __assign_irq_vector(irq, mask, result);
  591. spin_unlock_irqrestore(&vector_lock, flags);
  592. return vector;
  593. }
  594. void __setup_vector_irq(int cpu)
  595. {
  596. /* Initialize vector_irq on a new cpu */
  597. /* This function must be called with vector_lock held */
  598. unsigned long flags;
  599. int irq, vector;
  600. /* Mark the inuse vectors */
  601. for (irq = 0; irq < NR_IRQ_VECTORS; ++irq) {
  602. if (!cpu_isset(cpu, irq_domain[irq]))
  603. continue;
  604. vector = irq_vector[irq];
  605. per_cpu(vector_irq, cpu)[vector] = irq;
  606. }
  607. /* Mark the free vectors */
  608. for (vector = 0; vector < NR_VECTORS; ++vector) {
  609. irq = per_cpu(vector_irq, cpu)[vector];
  610. if (irq < 0)
  611. continue;
  612. if (!cpu_isset(cpu, irq_domain[irq]))
  613. per_cpu(vector_irq, cpu)[vector] = -1;
  614. }
  615. }
  616. extern void (*interrupt[NR_IRQS])(void);
  617. static struct irq_chip ioapic_chip;
  618. #define IOAPIC_AUTO -1
  619. #define IOAPIC_EDGE 0
  620. #define IOAPIC_LEVEL 1
  621. static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  622. {
  623. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  624. trigger == IOAPIC_LEVEL)
  625. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  626. handle_fasteoi_irq, "fasteoi");
  627. else
  628. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  629. handle_edge_irq, "edge");
  630. }
  631. static void __init setup_IO_APIC_irqs(void)
  632. {
  633. struct IO_APIC_route_entry entry;
  634. int apic, pin, idx, irq, first_notcon = 1, vector;
  635. unsigned long flags;
  636. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  637. for (apic = 0; apic < nr_ioapics; apic++) {
  638. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  639. /*
  640. * add it to the IO-APIC irq-routing table:
  641. */
  642. memset(&entry,0,sizeof(entry));
  643. entry.delivery_mode = INT_DELIVERY_MODE;
  644. entry.dest_mode = INT_DEST_MODE;
  645. entry.mask = 0; /* enable IRQ */
  646. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  647. idx = find_irq_entry(apic,pin,mp_INT);
  648. if (idx == -1) {
  649. if (first_notcon) {
  650. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  651. first_notcon = 0;
  652. } else
  653. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
  654. continue;
  655. }
  656. entry.trigger = irq_trigger(idx);
  657. entry.polarity = irq_polarity(idx);
  658. if (irq_trigger(idx)) {
  659. entry.trigger = 1;
  660. entry.mask = 1;
  661. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  662. }
  663. irq = pin_2_irq(idx, apic, pin);
  664. add_pin_to_irq(irq, apic, pin);
  665. if (!apic && !IO_APIC_IRQ(irq))
  666. continue;
  667. if (IO_APIC_IRQ(irq)) {
  668. cpumask_t mask;
  669. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  670. if (vector < 0)
  671. continue;
  672. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  673. entry.vector = vector;
  674. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  675. if (!apic && (irq < 16))
  676. disable_8259A_irq(irq);
  677. }
  678. ioapic_write_entry(apic, pin, entry);
  679. spin_lock_irqsave(&ioapic_lock, flags);
  680. set_native_irq_info(irq, TARGET_CPUS);
  681. spin_unlock_irqrestore(&ioapic_lock, flags);
  682. }
  683. }
  684. if (!first_notcon)
  685. apic_printk(APIC_VERBOSE," not connected.\n");
  686. }
  687. /*
  688. * Set up the 8259A-master output pin as broadcast to all
  689. * CPUs.
  690. */
  691. static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
  692. {
  693. struct IO_APIC_route_entry entry;
  694. unsigned long flags;
  695. memset(&entry,0,sizeof(entry));
  696. disable_8259A_irq(0);
  697. /* mask LVT0 */
  698. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  699. /*
  700. * We use logical delivery to get the timer IRQ
  701. * to the first CPU.
  702. */
  703. entry.dest_mode = INT_DEST_MODE;
  704. entry.mask = 0; /* unmask IRQ now */
  705. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  706. entry.delivery_mode = INT_DELIVERY_MODE;
  707. entry.polarity = 0;
  708. entry.trigger = 0;
  709. entry.vector = vector;
  710. /*
  711. * The timer IRQ doesn't have to know that behind the
  712. * scene we have a 8259A-master in AEOI mode ...
  713. */
  714. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  715. /*
  716. * Add it to the IO-APIC irq-routing table:
  717. */
  718. spin_lock_irqsave(&ioapic_lock, flags);
  719. io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
  720. io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
  721. spin_unlock_irqrestore(&ioapic_lock, flags);
  722. enable_8259A_irq(0);
  723. }
  724. void __init UNEXPECTED_IO_APIC(void)
  725. {
  726. }
  727. void __apicdebuginit print_IO_APIC(void)
  728. {
  729. int apic, i;
  730. union IO_APIC_reg_00 reg_00;
  731. union IO_APIC_reg_01 reg_01;
  732. union IO_APIC_reg_02 reg_02;
  733. unsigned long flags;
  734. if (apic_verbosity == APIC_QUIET)
  735. return;
  736. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  737. for (i = 0; i < nr_ioapics; i++)
  738. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  739. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  740. /*
  741. * We are a bit conservative about what we expect. We have to
  742. * know about every hardware change ASAP.
  743. */
  744. printk(KERN_INFO "testing the IO APIC.......................\n");
  745. for (apic = 0; apic < nr_ioapics; apic++) {
  746. spin_lock_irqsave(&ioapic_lock, flags);
  747. reg_00.raw = io_apic_read(apic, 0);
  748. reg_01.raw = io_apic_read(apic, 1);
  749. if (reg_01.bits.version >= 0x10)
  750. reg_02.raw = io_apic_read(apic, 2);
  751. spin_unlock_irqrestore(&ioapic_lock, flags);
  752. printk("\n");
  753. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  754. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  755. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  756. if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
  757. UNEXPECTED_IO_APIC();
  758. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  759. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  760. if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
  761. (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
  762. (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
  763. (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
  764. (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
  765. (reg_01.bits.entries != 0x2E) &&
  766. (reg_01.bits.entries != 0x3F) &&
  767. (reg_01.bits.entries != 0x03)
  768. )
  769. UNEXPECTED_IO_APIC();
  770. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  771. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  772. if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
  773. (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
  774. (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
  775. (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
  776. (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
  777. (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
  778. )
  779. UNEXPECTED_IO_APIC();
  780. if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
  781. UNEXPECTED_IO_APIC();
  782. if (reg_01.bits.version >= 0x10) {
  783. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  784. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  785. if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
  786. UNEXPECTED_IO_APIC();
  787. }
  788. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  789. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  790. " Stat Dest Deli Vect: \n");
  791. for (i = 0; i <= reg_01.bits.entries; i++) {
  792. struct IO_APIC_route_entry entry;
  793. entry = ioapic_read_entry(apic, i);
  794. printk(KERN_DEBUG " %02x %03X %02X ",
  795. i,
  796. entry.dest.logical.logical_dest,
  797. entry.dest.physical.physical_dest
  798. );
  799. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  800. entry.mask,
  801. entry.trigger,
  802. entry.irr,
  803. entry.polarity,
  804. entry.delivery_status,
  805. entry.dest_mode,
  806. entry.delivery_mode,
  807. entry.vector
  808. );
  809. }
  810. }
  811. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  812. for (i = 0; i < NR_IRQS; i++) {
  813. struct irq_pin_list *entry = irq_2_pin + i;
  814. if (entry->pin < 0)
  815. continue;
  816. printk(KERN_DEBUG "IRQ%d ", i);
  817. for (;;) {
  818. printk("-> %d:%d", entry->apic, entry->pin);
  819. if (!entry->next)
  820. break;
  821. entry = irq_2_pin + entry->next;
  822. }
  823. printk("\n");
  824. }
  825. printk(KERN_INFO ".................................... done.\n");
  826. return;
  827. }
  828. #if 0
  829. static __apicdebuginit void print_APIC_bitfield (int base)
  830. {
  831. unsigned int v;
  832. int i, j;
  833. if (apic_verbosity == APIC_QUIET)
  834. return;
  835. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  836. for (i = 0; i < 8; i++) {
  837. v = apic_read(base + i*0x10);
  838. for (j = 0; j < 32; j++) {
  839. if (v & (1<<j))
  840. printk("1");
  841. else
  842. printk("0");
  843. }
  844. printk("\n");
  845. }
  846. }
  847. void __apicdebuginit print_local_APIC(void * dummy)
  848. {
  849. unsigned int v, ver, maxlvt;
  850. if (apic_verbosity == APIC_QUIET)
  851. return;
  852. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  853. smp_processor_id(), hard_smp_processor_id());
  854. v = apic_read(APIC_ID);
  855. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
  856. v = apic_read(APIC_LVR);
  857. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  858. ver = GET_APIC_VERSION(v);
  859. maxlvt = get_maxlvt();
  860. v = apic_read(APIC_TASKPRI);
  861. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  862. v = apic_read(APIC_ARBPRI);
  863. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  864. v & APIC_ARBPRI_MASK);
  865. v = apic_read(APIC_PROCPRI);
  866. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  867. v = apic_read(APIC_EOI);
  868. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  869. v = apic_read(APIC_RRR);
  870. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  871. v = apic_read(APIC_LDR);
  872. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  873. v = apic_read(APIC_DFR);
  874. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  875. v = apic_read(APIC_SPIV);
  876. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  877. printk(KERN_DEBUG "... APIC ISR field:\n");
  878. print_APIC_bitfield(APIC_ISR);
  879. printk(KERN_DEBUG "... APIC TMR field:\n");
  880. print_APIC_bitfield(APIC_TMR);
  881. printk(KERN_DEBUG "... APIC IRR field:\n");
  882. print_APIC_bitfield(APIC_IRR);
  883. v = apic_read(APIC_ESR);
  884. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  885. v = apic_read(APIC_ICR);
  886. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  887. v = apic_read(APIC_ICR2);
  888. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  889. v = apic_read(APIC_LVTT);
  890. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  891. if (maxlvt > 3) { /* PC is LVT#4. */
  892. v = apic_read(APIC_LVTPC);
  893. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  894. }
  895. v = apic_read(APIC_LVT0);
  896. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  897. v = apic_read(APIC_LVT1);
  898. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  899. if (maxlvt > 2) { /* ERR is LVT#3. */
  900. v = apic_read(APIC_LVTERR);
  901. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  902. }
  903. v = apic_read(APIC_TMICT);
  904. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  905. v = apic_read(APIC_TMCCT);
  906. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  907. v = apic_read(APIC_TDCR);
  908. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  909. printk("\n");
  910. }
  911. void print_all_local_APICs (void)
  912. {
  913. on_each_cpu(print_local_APIC, NULL, 1, 1);
  914. }
  915. void __apicdebuginit print_PIC(void)
  916. {
  917. unsigned int v;
  918. unsigned long flags;
  919. if (apic_verbosity == APIC_QUIET)
  920. return;
  921. printk(KERN_DEBUG "\nprinting PIC contents\n");
  922. spin_lock_irqsave(&i8259A_lock, flags);
  923. v = inb(0xa1) << 8 | inb(0x21);
  924. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  925. v = inb(0xa0) << 8 | inb(0x20);
  926. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  927. outb(0x0b,0xa0);
  928. outb(0x0b,0x20);
  929. v = inb(0xa0) << 8 | inb(0x20);
  930. outb(0x0a,0xa0);
  931. outb(0x0a,0x20);
  932. spin_unlock_irqrestore(&i8259A_lock, flags);
  933. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  934. v = inb(0x4d1) << 8 | inb(0x4d0);
  935. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  936. }
  937. #endif /* 0 */
  938. static void __init enable_IO_APIC(void)
  939. {
  940. union IO_APIC_reg_01 reg_01;
  941. int i8259_apic, i8259_pin;
  942. int i, apic;
  943. unsigned long flags;
  944. for (i = 0; i < PIN_MAP_SIZE; i++) {
  945. irq_2_pin[i].pin = -1;
  946. irq_2_pin[i].next = 0;
  947. }
  948. /*
  949. * The number of IO-APIC IRQ registers (== #pins):
  950. */
  951. for (apic = 0; apic < nr_ioapics; apic++) {
  952. spin_lock_irqsave(&ioapic_lock, flags);
  953. reg_01.raw = io_apic_read(apic, 1);
  954. spin_unlock_irqrestore(&ioapic_lock, flags);
  955. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  956. }
  957. for(apic = 0; apic < nr_ioapics; apic++) {
  958. int pin;
  959. /* See if any of the pins is in ExtINT mode */
  960. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  961. struct IO_APIC_route_entry entry;
  962. entry = ioapic_read_entry(apic, pin);
  963. /* If the interrupt line is enabled and in ExtInt mode
  964. * I have found the pin where the i8259 is connected.
  965. */
  966. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  967. ioapic_i8259.apic = apic;
  968. ioapic_i8259.pin = pin;
  969. goto found_i8259;
  970. }
  971. }
  972. }
  973. found_i8259:
  974. /* Look to see what if the MP table has reported the ExtINT */
  975. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  976. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  977. /* Trust the MP table if nothing is setup in the hardware */
  978. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  979. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  980. ioapic_i8259.pin = i8259_pin;
  981. ioapic_i8259.apic = i8259_apic;
  982. }
  983. /* Complain if the MP table and the hardware disagree */
  984. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  985. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  986. {
  987. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  988. }
  989. /*
  990. * Do not trust the IO-APIC being empty at bootup
  991. */
  992. clear_IO_APIC();
  993. }
  994. /*
  995. * Not an __init, needed by the reboot code
  996. */
  997. void disable_IO_APIC(void)
  998. {
  999. /*
  1000. * Clear the IO-APIC before rebooting:
  1001. */
  1002. clear_IO_APIC();
  1003. /*
  1004. * If the i8259 is routed through an IOAPIC
  1005. * Put that IOAPIC in virtual wire mode
  1006. * so legacy interrupts can be delivered.
  1007. */
  1008. if (ioapic_i8259.pin != -1) {
  1009. struct IO_APIC_route_entry entry;
  1010. memset(&entry, 0, sizeof(entry));
  1011. entry.mask = 0; /* Enabled */
  1012. entry.trigger = 0; /* Edge */
  1013. entry.irr = 0;
  1014. entry.polarity = 0; /* High */
  1015. entry.delivery_status = 0;
  1016. entry.dest_mode = 0; /* Physical */
  1017. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1018. entry.vector = 0;
  1019. entry.dest.physical.physical_dest =
  1020. GET_APIC_ID(apic_read(APIC_ID));
  1021. /*
  1022. * Add it to the IO-APIC irq-routing table:
  1023. */
  1024. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1025. }
  1026. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1027. }
  1028. /*
  1029. * There is a nasty bug in some older SMP boards, their mptable lies
  1030. * about the timer IRQ. We do the following to work around the situation:
  1031. *
  1032. * - timer IRQ defaults to IO-APIC IRQ
  1033. * - if this function detects that timer IRQs are defunct, then we fall
  1034. * back to ISA timer IRQs
  1035. */
  1036. static int __init timer_irq_works(void)
  1037. {
  1038. unsigned long t1 = jiffies;
  1039. local_irq_enable();
  1040. /* Let ten ticks pass... */
  1041. mdelay((10 * 1000) / HZ);
  1042. /*
  1043. * Expect a few ticks at least, to be sure some possible
  1044. * glue logic does not lock up after one or two first
  1045. * ticks in a non-ExtINT mode. Also the local APIC
  1046. * might have cached one ExtINT interrupt. Finally, at
  1047. * least one tick may be lost due to delays.
  1048. */
  1049. /* jiffies wrap? */
  1050. if (jiffies - t1 > 4)
  1051. return 1;
  1052. return 0;
  1053. }
  1054. /*
  1055. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1056. * number of pending IRQ events unhandled. These cases are very rare,
  1057. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1058. * better to do it this way as thus we do not have to be aware of
  1059. * 'pending' interrupts in the IRQ path, except at this point.
  1060. */
  1061. /*
  1062. * Edge triggered needs to resend any interrupt
  1063. * that was delayed but this is now handled in the device
  1064. * independent code.
  1065. */
  1066. /*
  1067. * Starting up a edge-triggered IO-APIC interrupt is
  1068. * nasty - we need to make sure that we get the edge.
  1069. * If it is already asserted for some reason, we need
  1070. * return 1 to indicate that is was pending.
  1071. *
  1072. * This is not complete - we should be able to fake
  1073. * an edge even if it isn't on the 8259A...
  1074. */
  1075. static unsigned int startup_ioapic_irq(unsigned int irq)
  1076. {
  1077. int was_pending = 0;
  1078. unsigned long flags;
  1079. spin_lock_irqsave(&ioapic_lock, flags);
  1080. if (irq < 16) {
  1081. disable_8259A_irq(irq);
  1082. if (i8259A_irq_pending(irq))
  1083. was_pending = 1;
  1084. }
  1085. __unmask_IO_APIC_irq(irq);
  1086. spin_unlock_irqrestore(&ioapic_lock, flags);
  1087. return was_pending;
  1088. }
  1089. static int ioapic_retrigger_irq(unsigned int irq)
  1090. {
  1091. cpumask_t mask;
  1092. unsigned vector;
  1093. unsigned long flags;
  1094. spin_lock_irqsave(&vector_lock, flags);
  1095. vector = irq_vector[irq];
  1096. cpus_clear(mask);
  1097. cpu_set(first_cpu(irq_domain[irq]), mask);
  1098. send_IPI_mask(mask, vector);
  1099. spin_unlock_irqrestore(&vector_lock, flags);
  1100. return 1;
  1101. }
  1102. /*
  1103. * Level and edge triggered IO-APIC interrupts need different handling,
  1104. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1105. * handled with the level-triggered descriptor, but that one has slightly
  1106. * more overhead. Level-triggered interrupts cannot be handled with the
  1107. * edge-triggered handler, without risking IRQ storms and other ugly
  1108. * races.
  1109. */
  1110. static void ack_apic_edge(unsigned int irq)
  1111. {
  1112. move_native_irq(irq);
  1113. ack_APIC_irq();
  1114. }
  1115. static void ack_apic_level(unsigned int irq)
  1116. {
  1117. int do_unmask_irq = 0;
  1118. #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
  1119. /* If we are moving the irq we need to mask it */
  1120. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1121. do_unmask_irq = 1;
  1122. mask_IO_APIC_irq(irq);
  1123. }
  1124. #endif
  1125. /*
  1126. * We must acknowledge the irq before we move it or the acknowledge will
  1127. * not propogate properly.
  1128. */
  1129. ack_APIC_irq();
  1130. /* Now we can move and renable the irq */
  1131. move_masked_irq(irq);
  1132. if (unlikely(do_unmask_irq))
  1133. unmask_IO_APIC_irq(irq);
  1134. }
  1135. static struct irq_chip ioapic_chip __read_mostly = {
  1136. .name = "IO-APIC",
  1137. .startup = startup_ioapic_irq,
  1138. .mask = mask_IO_APIC_irq,
  1139. .unmask = unmask_IO_APIC_irq,
  1140. .ack = ack_apic_edge,
  1141. .eoi = ack_apic_level,
  1142. #ifdef CONFIG_SMP
  1143. .set_affinity = set_ioapic_affinity_irq,
  1144. #endif
  1145. .retrigger = ioapic_retrigger_irq,
  1146. };
  1147. static inline void init_IO_APIC_traps(void)
  1148. {
  1149. int irq;
  1150. /*
  1151. * NOTE! The local APIC isn't very good at handling
  1152. * multiple interrupts at the same interrupt level.
  1153. * As the interrupt level is determined by taking the
  1154. * vector number and shifting that right by 4, we
  1155. * want to spread these out a bit so that they don't
  1156. * all fall in the same interrupt level.
  1157. *
  1158. * Also, we've got to be careful not to trash gate
  1159. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1160. */
  1161. for (irq = 0; irq < NR_IRQS ; irq++) {
  1162. int tmp = irq;
  1163. if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
  1164. /*
  1165. * Hmm.. We don't have an entry for this,
  1166. * so default to an old-fashioned 8259
  1167. * interrupt if we can..
  1168. */
  1169. if (irq < 16)
  1170. make_8259A_irq(irq);
  1171. else
  1172. /* Strange. Oh, well.. */
  1173. irq_desc[irq].chip = &no_irq_chip;
  1174. }
  1175. }
  1176. }
  1177. static void enable_lapic_irq (unsigned int irq)
  1178. {
  1179. unsigned long v;
  1180. v = apic_read(APIC_LVT0);
  1181. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1182. }
  1183. static void disable_lapic_irq (unsigned int irq)
  1184. {
  1185. unsigned long v;
  1186. v = apic_read(APIC_LVT0);
  1187. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1188. }
  1189. static void ack_lapic_irq (unsigned int irq)
  1190. {
  1191. ack_APIC_irq();
  1192. }
  1193. static void end_lapic_irq (unsigned int i) { /* nothing */ }
  1194. static struct hw_interrupt_type lapic_irq_type __read_mostly = {
  1195. .typename = "local-APIC-edge",
  1196. .startup = NULL, /* startup_irq() not used for IRQ0 */
  1197. .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
  1198. .enable = enable_lapic_irq,
  1199. .disable = disable_lapic_irq,
  1200. .ack = ack_lapic_irq,
  1201. .end = end_lapic_irq,
  1202. };
  1203. static void setup_nmi (void)
  1204. {
  1205. /*
  1206. * Dirty trick to enable the NMI watchdog ...
  1207. * We put the 8259A master into AEOI mode and
  1208. * unmask on all local APICs LVT0 as NMI.
  1209. *
  1210. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1211. * is from Maciej W. Rozycki - so we do not have to EOI from
  1212. * the NMI handler or the timer interrupt.
  1213. */
  1214. printk(KERN_INFO "activating NMI Watchdog ...");
  1215. enable_NMI_through_LVT0(NULL);
  1216. printk(" done.\n");
  1217. }
  1218. /*
  1219. * This looks a bit hackish but it's about the only one way of sending
  1220. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1221. * not support the ExtINT mode, unfortunately. We need to send these
  1222. * cycles as some i82489DX-based boards have glue logic that keeps the
  1223. * 8259A interrupt line asserted until INTA. --macro
  1224. */
  1225. static inline void unlock_ExtINT_logic(void)
  1226. {
  1227. int apic, pin, i;
  1228. struct IO_APIC_route_entry entry0, entry1;
  1229. unsigned char save_control, save_freq_select;
  1230. unsigned long flags;
  1231. pin = find_isa_irq_pin(8, mp_INT);
  1232. apic = find_isa_irq_apic(8, mp_INT);
  1233. if (pin == -1)
  1234. return;
  1235. spin_lock_irqsave(&ioapic_lock, flags);
  1236. *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
  1237. *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
  1238. spin_unlock_irqrestore(&ioapic_lock, flags);
  1239. clear_IO_APIC_pin(apic, pin);
  1240. memset(&entry1, 0, sizeof(entry1));
  1241. entry1.dest_mode = 0; /* physical delivery */
  1242. entry1.mask = 0; /* unmask IRQ now */
  1243. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1244. entry1.delivery_mode = dest_ExtINT;
  1245. entry1.polarity = entry0.polarity;
  1246. entry1.trigger = 0;
  1247. entry1.vector = 0;
  1248. spin_lock_irqsave(&ioapic_lock, flags);
  1249. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
  1250. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
  1251. spin_unlock_irqrestore(&ioapic_lock, flags);
  1252. save_control = CMOS_READ(RTC_CONTROL);
  1253. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1254. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1255. RTC_FREQ_SELECT);
  1256. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1257. i = 100;
  1258. while (i-- > 0) {
  1259. mdelay(10);
  1260. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1261. i -= 10;
  1262. }
  1263. CMOS_WRITE(save_control, RTC_CONTROL);
  1264. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1265. clear_IO_APIC_pin(apic, pin);
  1266. spin_lock_irqsave(&ioapic_lock, flags);
  1267. io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
  1268. io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
  1269. spin_unlock_irqrestore(&ioapic_lock, flags);
  1270. }
  1271. /*
  1272. * This code may look a bit paranoid, but it's supposed to cooperate with
  1273. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1274. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1275. * fanatically on his truly buggy board.
  1276. *
  1277. * FIXME: really need to revamp this for modern platforms only.
  1278. */
  1279. static inline void check_timer(void)
  1280. {
  1281. int apic1, pin1, apic2, pin2;
  1282. int vector;
  1283. cpumask_t mask;
  1284. /*
  1285. * get/set the timer IRQ vector:
  1286. */
  1287. disable_8259A_irq(0);
  1288. vector = assign_irq_vector(0, TARGET_CPUS, &mask);
  1289. /*
  1290. * Subtle, code in do_timer_interrupt() expects an AEOI
  1291. * mode for the 8259A whenever interrupts are routed
  1292. * through I/O APICs. Also IRQ0 has to be enabled in
  1293. * the 8259A which implies the virtual wire has to be
  1294. * disabled in the local APIC.
  1295. */
  1296. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1297. init_8259A(1);
  1298. if (timer_over_8254 > 0)
  1299. enable_8259A_irq(0);
  1300. pin1 = find_isa_irq_pin(0, mp_INT);
  1301. apic1 = find_isa_irq_apic(0, mp_INT);
  1302. pin2 = ioapic_i8259.pin;
  1303. apic2 = ioapic_i8259.apic;
  1304. apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1305. vector, apic1, pin1, apic2, pin2);
  1306. if (pin1 != -1) {
  1307. /*
  1308. * Ok, does IRQ0 through the IOAPIC work?
  1309. */
  1310. unmask_IO_APIC_irq(0);
  1311. if (!no_timer_check && timer_irq_works()) {
  1312. nmi_watchdog_default();
  1313. if (nmi_watchdog == NMI_IO_APIC) {
  1314. disable_8259A_irq(0);
  1315. setup_nmi();
  1316. enable_8259A_irq(0);
  1317. }
  1318. if (disable_timer_pin_1 > 0)
  1319. clear_IO_APIC_pin(0, pin1);
  1320. return;
  1321. }
  1322. clear_IO_APIC_pin(apic1, pin1);
  1323. apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
  1324. "connected to IO-APIC\n");
  1325. }
  1326. apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
  1327. "through the 8259A ... ");
  1328. if (pin2 != -1) {
  1329. apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
  1330. apic2, pin2);
  1331. /*
  1332. * legacy devices should be connected to IO APIC #0
  1333. */
  1334. setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
  1335. if (timer_irq_works()) {
  1336. apic_printk(APIC_VERBOSE," works.\n");
  1337. nmi_watchdog_default();
  1338. if (nmi_watchdog == NMI_IO_APIC) {
  1339. setup_nmi();
  1340. }
  1341. return;
  1342. }
  1343. /*
  1344. * Cleanup, just in case ...
  1345. */
  1346. clear_IO_APIC_pin(apic2, pin2);
  1347. }
  1348. apic_printk(APIC_VERBOSE," failed.\n");
  1349. if (nmi_watchdog == NMI_IO_APIC) {
  1350. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1351. nmi_watchdog = 0;
  1352. }
  1353. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1354. disable_8259A_irq(0);
  1355. irq_desc[0].chip = &lapic_irq_type;
  1356. apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1357. enable_8259A_irq(0);
  1358. if (timer_irq_works()) {
  1359. apic_printk(APIC_VERBOSE," works.\n");
  1360. return;
  1361. }
  1362. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1363. apic_printk(APIC_VERBOSE," failed.\n");
  1364. apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1365. init_8259A(0);
  1366. make_8259A_irq(0);
  1367. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1368. unlock_ExtINT_logic();
  1369. if (timer_irq_works()) {
  1370. apic_printk(APIC_VERBOSE," works.\n");
  1371. return;
  1372. }
  1373. apic_printk(APIC_VERBOSE," failed :(.\n");
  1374. panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
  1375. }
  1376. static int __init notimercheck(char *s)
  1377. {
  1378. no_timer_check = 1;
  1379. return 1;
  1380. }
  1381. __setup("no_timer_check", notimercheck);
  1382. /*
  1383. *
  1384. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1385. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1386. * Linux doesn't really care, as it's not actually used
  1387. * for any interrupt handling anyway.
  1388. */
  1389. #define PIC_IRQS (1<<2)
  1390. void __init setup_IO_APIC(void)
  1391. {
  1392. enable_IO_APIC();
  1393. if (acpi_ioapic)
  1394. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1395. else
  1396. io_apic_irqs = ~PIC_IRQS;
  1397. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1398. sync_Arb_IDs();
  1399. setup_IO_APIC_irqs();
  1400. init_IO_APIC_traps();
  1401. check_timer();
  1402. if (!acpi_ioapic)
  1403. print_IO_APIC();
  1404. }
  1405. struct sysfs_ioapic_data {
  1406. struct sys_device dev;
  1407. struct IO_APIC_route_entry entry[0];
  1408. };
  1409. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1410. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1411. {
  1412. struct IO_APIC_route_entry *entry;
  1413. struct sysfs_ioapic_data *data;
  1414. int i;
  1415. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1416. entry = data->entry;
  1417. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1418. *entry = ioapic_read_entry(dev->id, i);
  1419. return 0;
  1420. }
  1421. static int ioapic_resume(struct sys_device *dev)
  1422. {
  1423. struct IO_APIC_route_entry *entry;
  1424. struct sysfs_ioapic_data *data;
  1425. unsigned long flags;
  1426. union IO_APIC_reg_00 reg_00;
  1427. int i;
  1428. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1429. entry = data->entry;
  1430. spin_lock_irqsave(&ioapic_lock, flags);
  1431. reg_00.raw = io_apic_read(dev->id, 0);
  1432. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  1433. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  1434. io_apic_write(dev->id, 0, reg_00.raw);
  1435. }
  1436. spin_unlock_irqrestore(&ioapic_lock, flags);
  1437. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1438. ioapic_write_entry(dev->id, i, entry[i]);
  1439. return 0;
  1440. }
  1441. static struct sysdev_class ioapic_sysdev_class = {
  1442. set_kset_name("ioapic"),
  1443. .suspend = ioapic_suspend,
  1444. .resume = ioapic_resume,
  1445. };
  1446. static int __init ioapic_init_sysfs(void)
  1447. {
  1448. struct sys_device * dev;
  1449. int i, size, error = 0;
  1450. error = sysdev_class_register(&ioapic_sysdev_class);
  1451. if (error)
  1452. return error;
  1453. for (i = 0; i < nr_ioapics; i++ ) {
  1454. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1455. * sizeof(struct IO_APIC_route_entry);
  1456. mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
  1457. if (!mp_ioapic_data[i]) {
  1458. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1459. continue;
  1460. }
  1461. memset(mp_ioapic_data[i], 0, size);
  1462. dev = &mp_ioapic_data[i]->dev;
  1463. dev->id = i;
  1464. dev->cls = &ioapic_sysdev_class;
  1465. error = sysdev_register(dev);
  1466. if (error) {
  1467. kfree(mp_ioapic_data[i]);
  1468. mp_ioapic_data[i] = NULL;
  1469. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1470. continue;
  1471. }
  1472. }
  1473. return 0;
  1474. }
  1475. device_initcall(ioapic_init_sysfs);
  1476. /*
  1477. * Dynamic irq allocate and deallocation
  1478. */
  1479. int create_irq(void)
  1480. {
  1481. /* Allocate an unused irq */
  1482. int irq;
  1483. int new;
  1484. int vector = 0;
  1485. unsigned long flags;
  1486. cpumask_t mask;
  1487. irq = -ENOSPC;
  1488. spin_lock_irqsave(&vector_lock, flags);
  1489. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1490. if (platform_legacy_irq(new))
  1491. continue;
  1492. if (irq_vector[new] != 0)
  1493. continue;
  1494. vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
  1495. if (likely(vector > 0))
  1496. irq = new;
  1497. break;
  1498. }
  1499. spin_unlock_irqrestore(&vector_lock, flags);
  1500. if (irq >= 0) {
  1501. dynamic_irq_init(irq);
  1502. }
  1503. return irq;
  1504. }
  1505. void destroy_irq(unsigned int irq)
  1506. {
  1507. unsigned long flags;
  1508. dynamic_irq_cleanup(irq);
  1509. spin_lock_irqsave(&vector_lock, flags);
  1510. irq_vector[irq] = 0;
  1511. spin_unlock_irqrestore(&vector_lock, flags);
  1512. }
  1513. /*
  1514. * MSI mesage composition
  1515. */
  1516. #ifdef CONFIG_PCI_MSI
  1517. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1518. {
  1519. int vector;
  1520. unsigned dest;
  1521. cpumask_t tmp;
  1522. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1523. if (vector >= 0) {
  1524. dest = cpu_mask_to_apicid(tmp);
  1525. msg->address_hi = MSI_ADDR_BASE_HI;
  1526. msg->address_lo =
  1527. MSI_ADDR_BASE_LO |
  1528. ((INT_DEST_MODE == 0) ?
  1529. MSI_ADDR_DEST_MODE_PHYSICAL:
  1530. MSI_ADDR_DEST_MODE_LOGICAL) |
  1531. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1532. MSI_ADDR_REDIRECTION_CPU:
  1533. MSI_ADDR_REDIRECTION_LOWPRI) |
  1534. MSI_ADDR_DEST_ID(dest);
  1535. msg->data =
  1536. MSI_DATA_TRIGGER_EDGE |
  1537. MSI_DATA_LEVEL_ASSERT |
  1538. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1539. MSI_DATA_DELIVERY_FIXED:
  1540. MSI_DATA_DELIVERY_LOWPRI) |
  1541. MSI_DATA_VECTOR(vector);
  1542. }
  1543. return vector;
  1544. }
  1545. #ifdef CONFIG_SMP
  1546. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  1547. {
  1548. struct msi_msg msg;
  1549. unsigned int dest;
  1550. cpumask_t tmp;
  1551. int vector;
  1552. cpus_and(tmp, mask, cpu_online_map);
  1553. if (cpus_empty(tmp))
  1554. tmp = TARGET_CPUS;
  1555. cpus_and(mask, tmp, CPU_MASK_ALL);
  1556. vector = assign_irq_vector(irq, mask, &tmp);
  1557. if (vector < 0)
  1558. return;
  1559. dest = cpu_mask_to_apicid(tmp);
  1560. read_msi_msg(irq, &msg);
  1561. msg.data &= ~MSI_DATA_VECTOR_MASK;
  1562. msg.data |= MSI_DATA_VECTOR(vector);
  1563. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  1564. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  1565. write_msi_msg(irq, &msg);
  1566. set_native_irq_info(irq, mask);
  1567. }
  1568. #endif /* CONFIG_SMP */
  1569. /*
  1570. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  1571. * which implement the MSI or MSI-X Capability Structure.
  1572. */
  1573. static struct irq_chip msi_chip = {
  1574. .name = "PCI-MSI",
  1575. .unmask = unmask_msi_irq,
  1576. .mask = mask_msi_irq,
  1577. .ack = ack_apic_edge,
  1578. #ifdef CONFIG_SMP
  1579. .set_affinity = set_msi_irq_affinity,
  1580. #endif
  1581. .retrigger = ioapic_retrigger_irq,
  1582. };
  1583. int arch_setup_msi_irq(unsigned int irq, struct pci_dev *dev)
  1584. {
  1585. struct msi_msg msg;
  1586. int ret;
  1587. ret = msi_compose_msg(dev, irq, &msg);
  1588. if (ret < 0)
  1589. return ret;
  1590. write_msi_msg(irq, &msg);
  1591. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  1592. return 0;
  1593. }
  1594. void arch_teardown_msi_irq(unsigned int irq)
  1595. {
  1596. return;
  1597. }
  1598. #endif /* CONFIG_PCI_MSI */
  1599. /*
  1600. * Hypertransport interrupt support
  1601. */
  1602. #ifdef CONFIG_HT_IRQ
  1603. #ifdef CONFIG_SMP
  1604. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  1605. {
  1606. u32 low, high;
  1607. low = read_ht_irq_low(irq);
  1608. high = read_ht_irq_high(irq);
  1609. low &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  1610. high &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  1611. low |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  1612. high |= HT_IRQ_HIGH_DEST_ID(dest);
  1613. write_ht_irq_low(irq, low);
  1614. write_ht_irq_high(irq, high);
  1615. }
  1616. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  1617. {
  1618. unsigned int dest;
  1619. cpumask_t tmp;
  1620. int vector;
  1621. cpus_and(tmp, mask, cpu_online_map);
  1622. if (cpus_empty(tmp))
  1623. tmp = TARGET_CPUS;
  1624. cpus_and(mask, tmp, CPU_MASK_ALL);
  1625. vector = assign_irq_vector(irq, mask, &tmp);
  1626. if (vector < 0)
  1627. return;
  1628. dest = cpu_mask_to_apicid(tmp);
  1629. target_ht_irq(irq, dest, vector & 0xff);
  1630. set_native_irq_info(irq, mask);
  1631. }
  1632. #endif
  1633. static struct irq_chip ht_irq_chip = {
  1634. .name = "PCI-HT",
  1635. .mask = mask_ht_irq,
  1636. .unmask = unmask_ht_irq,
  1637. .ack = ack_apic_edge,
  1638. #ifdef CONFIG_SMP
  1639. .set_affinity = set_ht_irq_affinity,
  1640. #endif
  1641. .retrigger = ioapic_retrigger_irq,
  1642. };
  1643. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  1644. {
  1645. int vector;
  1646. cpumask_t tmp;
  1647. vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
  1648. if (vector >= 0) {
  1649. u32 low, high;
  1650. unsigned dest;
  1651. dest = cpu_mask_to_apicid(tmp);
  1652. high = HT_IRQ_HIGH_DEST_ID(dest);
  1653. low = HT_IRQ_LOW_BASE |
  1654. HT_IRQ_LOW_DEST_ID(dest) |
  1655. HT_IRQ_LOW_VECTOR(vector) |
  1656. ((INT_DEST_MODE == 0) ?
  1657. HT_IRQ_LOW_DM_PHYSICAL :
  1658. HT_IRQ_LOW_DM_LOGICAL) |
  1659. HT_IRQ_LOW_RQEOI_EDGE |
  1660. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  1661. HT_IRQ_LOW_MT_FIXED :
  1662. HT_IRQ_LOW_MT_ARBITRATED);
  1663. write_ht_irq_low(irq, low);
  1664. write_ht_irq_high(irq, high);
  1665. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  1666. handle_edge_irq, "edge");
  1667. }
  1668. return vector;
  1669. }
  1670. #endif /* CONFIG_HT_IRQ */
  1671. /* --------------------------------------------------------------------------
  1672. ACPI-based IOAPIC Configuration
  1673. -------------------------------------------------------------------------- */
  1674. #ifdef CONFIG_ACPI
  1675. #define IO_APIC_MAX_ID 0xFE
  1676. int __init io_apic_get_redir_entries (int ioapic)
  1677. {
  1678. union IO_APIC_reg_01 reg_01;
  1679. unsigned long flags;
  1680. spin_lock_irqsave(&ioapic_lock, flags);
  1681. reg_01.raw = io_apic_read(ioapic, 1);
  1682. spin_unlock_irqrestore(&ioapic_lock, flags);
  1683. return reg_01.bits.entries;
  1684. }
  1685. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  1686. {
  1687. struct IO_APIC_route_entry entry;
  1688. unsigned long flags;
  1689. int vector;
  1690. cpumask_t mask;
  1691. if (!IO_APIC_IRQ(irq)) {
  1692. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  1693. ioapic);
  1694. return -EINVAL;
  1695. }
  1696. /*
  1697. * IRQs < 16 are already in the irq_2_pin[] map
  1698. */
  1699. if (irq >= 16)
  1700. add_pin_to_irq(irq, ioapic, pin);
  1701. vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
  1702. if (vector < 0)
  1703. return vector;
  1704. /*
  1705. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  1706. * Note that we mask (disable) IRQs now -- these get enabled when the
  1707. * corresponding device driver registers for this IRQ.
  1708. */
  1709. memset(&entry,0,sizeof(entry));
  1710. entry.delivery_mode = INT_DELIVERY_MODE;
  1711. entry.dest_mode = INT_DEST_MODE;
  1712. entry.dest.logical.logical_dest = cpu_mask_to_apicid(mask);
  1713. entry.trigger = triggering;
  1714. entry.polarity = polarity;
  1715. entry.mask = 1; /* Disabled (masked) */
  1716. entry.vector = vector & 0xff;
  1717. apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
  1718. "IRQ %d Mode:%i Active:%i)\n", ioapic,
  1719. mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
  1720. triggering, polarity);
  1721. ioapic_register_intr(irq, entry.vector, triggering);
  1722. if (!ioapic && (irq < 16))
  1723. disable_8259A_irq(irq);
  1724. ioapic_write_entry(ioapic, pin, entry);
  1725. spin_lock_irqsave(&ioapic_lock, flags);
  1726. set_native_irq_info(irq, TARGET_CPUS);
  1727. spin_unlock_irqrestore(&ioapic_lock, flags);
  1728. return 0;
  1729. }
  1730. #endif /* CONFIG_ACPI */
  1731. /*
  1732. * This function currently is only a helper for the i386 smp boot process where
  1733. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  1734. * so mask in all cases should simply be TARGET_CPUS
  1735. */
  1736. #ifdef CONFIG_SMP
  1737. void __init setup_ioapic_dest(void)
  1738. {
  1739. int pin, ioapic, irq, irq_entry;
  1740. if (skip_ioapic_setup == 1)
  1741. return;
  1742. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  1743. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  1744. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  1745. if (irq_entry == -1)
  1746. continue;
  1747. irq = pin_2_irq(irq_entry, ioapic, pin);
  1748. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  1749. }
  1750. }
  1751. }
  1752. #endif