io_apic_64.c 57 KB

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