io_apic.c 53 KB

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