io_apic_64.c 58 KB

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