io_apic.c 53 KB

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