io_apic.c 53 KB

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