io_apic.c 52 KB

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