io_apic_64.c 58 KB

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