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/i8259.h>
  47. #include <asm/nmi.h>
  48. #include <asm/msidef.h>
  49. #include <asm/hypertransport.h>
  50. #include <mach_ipi.h>
  51. #include <mach_apic.h>
  52. #define __apicdebuginit(type) static type __init
  53. struct irq_cfg {
  54. cpumask_t domain;
  55. cpumask_t old_domain;
  56. unsigned move_cleanup_count;
  57. u8 vector;
  58. u8 move_in_progress : 1;
  59. };
  60. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  61. static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  62. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  63. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  64. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  65. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  66. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  67. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  68. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  69. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  70. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  71. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  72. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  73. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  74. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  75. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  76. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  77. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  78. };
  79. static int assign_irq_vector(int irq, cpumask_t mask);
  80. int first_system_vector = 0xfe;
  81. char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
  82. int sis_apic_bug; /* not actually supported, dummy for compile */
  83. static int no_timer_check;
  84. static int disable_timer_pin_1 __initdata;
  85. int timer_through_8259 __initdata;
  86. /* Where if anywhere is the i8259 connect in external int mode */
  87. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  88. static DEFINE_SPINLOCK(ioapic_lock);
  89. DEFINE_SPINLOCK(vector_lock);
  90. /*
  91. * # of IRQ routing registers
  92. */
  93. int nr_ioapic_registers[MAX_IO_APICS];
  94. /* I/O APIC entries */
  95. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  96. int nr_ioapics;
  97. /* MP IRQ source entries */
  98. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  99. /* # of MP IRQ source entries */
  100. int mp_irq_entries;
  101. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  102. /*
  103. * Rough estimation of how many shared IRQs there are, can
  104. * be changed anytime.
  105. */
  106. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  107. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  108. /*
  109. * This is performance-critical, we want to do it O(1)
  110. *
  111. * the indexing order of this array favors 1:1 mappings
  112. * between pins and IRQs.
  113. */
  114. static struct irq_pin_list {
  115. short apic, pin, next;
  116. } irq_2_pin[PIN_MAP_SIZE];
  117. struct io_apic {
  118. unsigned int index;
  119. unsigned int unused[3];
  120. unsigned int data;
  121. };
  122. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  123. {
  124. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  125. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  126. }
  127. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  128. {
  129. struct io_apic __iomem *io_apic = io_apic_base(apic);
  130. writel(reg, &io_apic->index);
  131. return readl(&io_apic->data);
  132. }
  133. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  134. {
  135. struct io_apic __iomem *io_apic = io_apic_base(apic);
  136. writel(reg, &io_apic->index);
  137. writel(value, &io_apic->data);
  138. }
  139. /*
  140. * Re-write a value: to be used for read-modify-write
  141. * cycles where the read already set up the index register.
  142. */
  143. static inline void io_apic_modify(unsigned int apic, unsigned int value)
  144. {
  145. struct io_apic __iomem *io_apic = io_apic_base(apic);
  146. writel(value, &io_apic->data);
  147. }
  148. static bool io_apic_level_ack_pending(unsigned int irq)
  149. {
  150. struct irq_pin_list *entry;
  151. unsigned long flags;
  152. spin_lock_irqsave(&ioapic_lock, flags);
  153. entry = irq_2_pin + irq;
  154. for (;;) {
  155. unsigned int reg;
  156. int pin;
  157. pin = entry->pin;
  158. if (pin == -1)
  159. break;
  160. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  161. /* Is the remote IRR bit set? */
  162. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  163. spin_unlock_irqrestore(&ioapic_lock, flags);
  164. return true;
  165. }
  166. if (!entry->next)
  167. break;
  168. entry = irq_2_pin + entry->next;
  169. }
  170. spin_unlock_irqrestore(&ioapic_lock, flags);
  171. return false;
  172. }
  173. /*
  174. * Synchronize the IO-APIC and the CPU by doing
  175. * a dummy read from the IO-APIC
  176. */
  177. static inline void io_apic_sync(unsigned int apic)
  178. {
  179. struct io_apic __iomem *io_apic = io_apic_base(apic);
  180. readl(&io_apic->data);
  181. }
  182. #define __DO_ACTION(R, ACTION, FINAL) \
  183. \
  184. { \
  185. int pin; \
  186. struct irq_pin_list *entry = irq_2_pin + irq; \
  187. \
  188. BUG_ON(irq >= NR_IRQS); \
  189. for (;;) { \
  190. unsigned int reg; \
  191. pin = entry->pin; \
  192. if (pin == -1) \
  193. break; \
  194. reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
  195. reg ACTION; \
  196. io_apic_modify(entry->apic, reg); \
  197. FINAL; \
  198. if (!entry->next) \
  199. break; \
  200. entry = irq_2_pin + entry->next; \
  201. } \
  202. }
  203. union entry_union {
  204. struct { u32 w1, w2; };
  205. struct IO_APIC_route_entry entry;
  206. };
  207. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  208. {
  209. union entry_union eu;
  210. unsigned long flags;
  211. spin_lock_irqsave(&ioapic_lock, flags);
  212. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  213. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  214. spin_unlock_irqrestore(&ioapic_lock, flags);
  215. return eu.entry;
  216. }
  217. /*
  218. * When we write a new IO APIC routing entry, we need to write the high
  219. * word first! If the mask bit in the low word is clear, we will enable
  220. * the interrupt, and we need to make sure the entry is fully populated
  221. * before that happens.
  222. */
  223. static void
  224. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  225. {
  226. union entry_union eu;
  227. eu.entry = e;
  228. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  229. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  230. }
  231. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  232. {
  233. unsigned long flags;
  234. spin_lock_irqsave(&ioapic_lock, flags);
  235. __ioapic_write_entry(apic, pin, e);
  236. spin_unlock_irqrestore(&ioapic_lock, flags);
  237. }
  238. /*
  239. * When we mask an IO APIC routing entry, we need to write the low
  240. * word first, in order to set the mask bit before we change the
  241. * high bits!
  242. */
  243. static void ioapic_mask_entry(int apic, int pin)
  244. {
  245. unsigned long flags;
  246. union entry_union eu = { .entry.mask = 1 };
  247. spin_lock_irqsave(&ioapic_lock, flags);
  248. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  249. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  250. spin_unlock_irqrestore(&ioapic_lock, flags);
  251. }
  252. #ifdef CONFIG_SMP
  253. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
  254. {
  255. int apic, pin;
  256. struct irq_pin_list *entry = irq_2_pin + irq;
  257. BUG_ON(irq >= NR_IRQS);
  258. for (;;) {
  259. unsigned int reg;
  260. apic = entry->apic;
  261. pin = entry->pin;
  262. if (pin == -1)
  263. break;
  264. io_apic_write(apic, 0x11 + pin*2, dest);
  265. reg = io_apic_read(apic, 0x10 + pin*2);
  266. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  267. reg |= vector;
  268. io_apic_modify(apic, reg);
  269. if (!entry->next)
  270. break;
  271. entry = irq_2_pin + entry->next;
  272. }
  273. }
  274. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  275. {
  276. struct irq_cfg *cfg = irq_cfg + irq;
  277. unsigned long flags;
  278. unsigned int dest;
  279. cpumask_t tmp;
  280. cpus_and(tmp, mask, cpu_online_map);
  281. if (cpus_empty(tmp))
  282. return;
  283. if (assign_irq_vector(irq, mask))
  284. return;
  285. cpus_and(tmp, cfg->domain, mask);
  286. dest = cpu_mask_to_apicid(tmp);
  287. /*
  288. * Only the high 8 bits are valid.
  289. */
  290. dest = SET_APIC_LOGICAL_ID(dest);
  291. spin_lock_irqsave(&ioapic_lock, flags);
  292. __target_IO_APIC_irq(irq, dest, cfg->vector);
  293. irq_desc[irq].affinity = mask;
  294. spin_unlock_irqrestore(&ioapic_lock, flags);
  295. }
  296. #endif
  297. /*
  298. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  299. * shared ISA-space IRQs, so we have to support them. We are super
  300. * fast in the common case, and fast for shared ISA-space IRQs.
  301. */
  302. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  303. {
  304. static int first_free_entry = NR_IRQS;
  305. struct irq_pin_list *entry = irq_2_pin + irq;
  306. BUG_ON(irq >= NR_IRQS);
  307. while (entry->next)
  308. entry = irq_2_pin + entry->next;
  309. if (entry->pin != -1) {
  310. entry->next = first_free_entry;
  311. entry = irq_2_pin + entry->next;
  312. if (++first_free_entry >= PIN_MAP_SIZE)
  313. panic("io_apic.c: ran out of irq_2_pin entries!");
  314. }
  315. entry->apic = apic;
  316. entry->pin = pin;
  317. }
  318. /*
  319. * Reroute an IRQ to a different pin.
  320. */
  321. static void __init replace_pin_at_irq(unsigned int irq,
  322. int oldapic, int oldpin,
  323. int newapic, int newpin)
  324. {
  325. struct irq_pin_list *entry = irq_2_pin + irq;
  326. while (1) {
  327. if (entry->apic == oldapic && entry->pin == oldpin) {
  328. entry->apic = newapic;
  329. entry->pin = newpin;
  330. }
  331. if (!entry->next)
  332. break;
  333. entry = irq_2_pin + entry->next;
  334. }
  335. }
  336. #define DO_ACTION(name,R,ACTION, FINAL) \
  337. \
  338. static void name##_IO_APIC_irq (unsigned int irq) \
  339. __DO_ACTION(R, ACTION, FINAL)
  340. /* mask = 1 */
  341. DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
  342. /* mask = 0 */
  343. DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
  344. static void mask_IO_APIC_irq (unsigned int irq)
  345. {
  346. unsigned long flags;
  347. spin_lock_irqsave(&ioapic_lock, flags);
  348. __mask_IO_APIC_irq(irq);
  349. spin_unlock_irqrestore(&ioapic_lock, flags);
  350. }
  351. static void unmask_IO_APIC_irq (unsigned int irq)
  352. {
  353. unsigned long flags;
  354. spin_lock_irqsave(&ioapic_lock, flags);
  355. __unmask_IO_APIC_irq(irq);
  356. spin_unlock_irqrestore(&ioapic_lock, flags);
  357. }
  358. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  359. {
  360. struct IO_APIC_route_entry entry;
  361. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  362. entry = ioapic_read_entry(apic, pin);
  363. if (entry.delivery_mode == dest_SMI)
  364. return;
  365. /*
  366. * Disable it in the IO-APIC irq-routing table:
  367. */
  368. ioapic_mask_entry(apic, pin);
  369. }
  370. static void clear_IO_APIC (void)
  371. {
  372. int apic, pin;
  373. for (apic = 0; apic < nr_ioapics; apic++)
  374. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  375. clear_IO_APIC_pin(apic, pin);
  376. }
  377. int skip_ioapic_setup;
  378. int ioapic_force;
  379. static int __init parse_noapic(char *str)
  380. {
  381. disable_ioapic_setup();
  382. return 0;
  383. }
  384. early_param("noapic", parse_noapic);
  385. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  386. static int __init disable_timer_pin_setup(char *arg)
  387. {
  388. disable_timer_pin_1 = 1;
  389. return 1;
  390. }
  391. __setup("disable_timer_pin_1", disable_timer_pin_setup);
  392. /*
  393. * Find the IRQ entry number of a certain pin.
  394. */
  395. static int find_irq_entry(int apic, int pin, int type)
  396. {
  397. int i;
  398. for (i = 0; i < mp_irq_entries; i++)
  399. if (mp_irqs[i].mp_irqtype == type &&
  400. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  401. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  402. mp_irqs[i].mp_dstirq == pin)
  403. return i;
  404. return -1;
  405. }
  406. /*
  407. * Find the pin to which IRQ[irq] (ISA) is connected
  408. */
  409. static int __init find_isa_irq_pin(int irq, int type)
  410. {
  411. int i;
  412. for (i = 0; i < mp_irq_entries; i++) {
  413. int lbus = mp_irqs[i].mp_srcbus;
  414. if (test_bit(lbus, mp_bus_not_pci) &&
  415. (mp_irqs[i].mp_irqtype == type) &&
  416. (mp_irqs[i].mp_srcbusirq == irq))
  417. return mp_irqs[i].mp_dstirq;
  418. }
  419. return -1;
  420. }
  421. static int __init find_isa_irq_apic(int irq, int type)
  422. {
  423. int i;
  424. for (i = 0; i < mp_irq_entries; i++) {
  425. int lbus = mp_irqs[i].mp_srcbus;
  426. if (test_bit(lbus, mp_bus_not_pci) &&
  427. (mp_irqs[i].mp_irqtype == type) &&
  428. (mp_irqs[i].mp_srcbusirq == irq))
  429. break;
  430. }
  431. if (i < mp_irq_entries) {
  432. int apic;
  433. for(apic = 0; apic < nr_ioapics; apic++) {
  434. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  435. return apic;
  436. }
  437. }
  438. return -1;
  439. }
  440. /*
  441. * Find a specific PCI IRQ entry.
  442. * Not an __init, possibly needed by modules
  443. */
  444. static int pin_2_irq(int idx, int apic, int pin);
  445. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  446. {
  447. int apic, i, best_guess = -1;
  448. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  449. bus, slot, pin);
  450. if (test_bit(bus, mp_bus_not_pci)) {
  451. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  452. return -1;
  453. }
  454. for (i = 0; i < mp_irq_entries; i++) {
  455. int lbus = mp_irqs[i].mp_srcbus;
  456. for (apic = 0; apic < nr_ioapics; apic++)
  457. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  458. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  459. break;
  460. if (!test_bit(lbus, mp_bus_not_pci) &&
  461. !mp_irqs[i].mp_irqtype &&
  462. (bus == lbus) &&
  463. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  464. int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
  465. if (!(apic || IO_APIC_IRQ(irq)))
  466. continue;
  467. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  468. return irq;
  469. /*
  470. * Use the first all-but-pin matching entry as a
  471. * best-guess fuzzy result for broken mptables.
  472. */
  473. if (best_guess < 0)
  474. best_guess = irq;
  475. }
  476. }
  477. BUG_ON(best_guess >= NR_IRQS);
  478. return best_guess;
  479. }
  480. /* ISA interrupts are always polarity zero edge triggered,
  481. * when listed as conforming in the MP table. */
  482. #define default_ISA_trigger(idx) (0)
  483. #define default_ISA_polarity(idx) (0)
  484. /* PCI interrupts are always polarity one level triggered,
  485. * when listed as conforming in the MP table. */
  486. #define default_PCI_trigger(idx) (1)
  487. #define default_PCI_polarity(idx) (1)
  488. static int MPBIOS_polarity(int idx)
  489. {
  490. int bus = mp_irqs[idx].mp_srcbus;
  491. int polarity;
  492. /*
  493. * Determine IRQ line polarity (high active or low active):
  494. */
  495. switch (mp_irqs[idx].mp_irqflag & 3)
  496. {
  497. case 0: /* conforms, ie. bus-type dependent polarity */
  498. if (test_bit(bus, mp_bus_not_pci))
  499. polarity = default_ISA_polarity(idx);
  500. else
  501. polarity = default_PCI_polarity(idx);
  502. break;
  503. case 1: /* high active */
  504. {
  505. polarity = 0;
  506. break;
  507. }
  508. case 2: /* reserved */
  509. {
  510. printk(KERN_WARNING "broken BIOS!!\n");
  511. polarity = 1;
  512. break;
  513. }
  514. case 3: /* low active */
  515. {
  516. polarity = 1;
  517. break;
  518. }
  519. default: /* invalid */
  520. {
  521. printk(KERN_WARNING "broken BIOS!!\n");
  522. polarity = 1;
  523. break;
  524. }
  525. }
  526. return polarity;
  527. }
  528. static int MPBIOS_trigger(int idx)
  529. {
  530. int bus = mp_irqs[idx].mp_srcbus;
  531. int trigger;
  532. /*
  533. * Determine IRQ trigger mode (edge or level sensitive):
  534. */
  535. switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
  536. {
  537. case 0: /* conforms, ie. bus-type dependent */
  538. if (test_bit(bus, mp_bus_not_pci))
  539. trigger = default_ISA_trigger(idx);
  540. else
  541. trigger = default_PCI_trigger(idx);
  542. break;
  543. case 1: /* edge */
  544. {
  545. trigger = 0;
  546. break;
  547. }
  548. case 2: /* reserved */
  549. {
  550. printk(KERN_WARNING "broken BIOS!!\n");
  551. trigger = 1;
  552. break;
  553. }
  554. case 3: /* level */
  555. {
  556. trigger = 1;
  557. break;
  558. }
  559. default: /* invalid */
  560. {
  561. printk(KERN_WARNING "broken BIOS!!\n");
  562. trigger = 0;
  563. break;
  564. }
  565. }
  566. return trigger;
  567. }
  568. static inline int irq_polarity(int idx)
  569. {
  570. return MPBIOS_polarity(idx);
  571. }
  572. static inline int irq_trigger(int idx)
  573. {
  574. return MPBIOS_trigger(idx);
  575. }
  576. static int pin_2_irq(int idx, int apic, int pin)
  577. {
  578. int irq, i;
  579. int bus = mp_irqs[idx].mp_srcbus;
  580. /*
  581. * Debugging check, we are in big trouble if this message pops up!
  582. */
  583. if (mp_irqs[idx].mp_dstirq != pin)
  584. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  585. if (test_bit(bus, mp_bus_not_pci)) {
  586. irq = mp_irqs[idx].mp_srcbusirq;
  587. } else {
  588. /*
  589. * PCI IRQs are mapped in order
  590. */
  591. i = irq = 0;
  592. while (i < apic)
  593. irq += nr_ioapic_registers[i++];
  594. irq += pin;
  595. }
  596. BUG_ON(irq >= NR_IRQS);
  597. return irq;
  598. }
  599. static int __assign_irq_vector(int irq, cpumask_t mask)
  600. {
  601. /*
  602. * NOTE! The local APIC isn't very good at handling
  603. * multiple interrupts at the same interrupt level.
  604. * As the interrupt level is determined by taking the
  605. * vector number and shifting that right by 4, we
  606. * want to spread these out a bit so that they don't
  607. * all fall in the same interrupt level.
  608. *
  609. * Also, we've got to be careful not to trash gate
  610. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  611. */
  612. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  613. unsigned int old_vector;
  614. int cpu;
  615. struct irq_cfg *cfg;
  616. BUG_ON((unsigned)irq >= NR_IRQS);
  617. cfg = &irq_cfg[irq];
  618. /* Only try and allocate irqs on cpus that are present */
  619. cpus_and(mask, mask, cpu_online_map);
  620. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  621. return -EBUSY;
  622. old_vector = cfg->vector;
  623. if (old_vector) {
  624. cpumask_t tmp;
  625. cpus_and(tmp, cfg->domain, mask);
  626. if (!cpus_empty(tmp))
  627. return 0;
  628. }
  629. for_each_cpu_mask_nr(cpu, mask) {
  630. cpumask_t domain, new_mask;
  631. int new_cpu;
  632. int vector, offset;
  633. domain = vector_allocation_domain(cpu);
  634. cpus_and(new_mask, domain, cpu_online_map);
  635. vector = current_vector;
  636. offset = current_offset;
  637. next:
  638. vector += 8;
  639. if (vector >= first_system_vector) {
  640. /* If we run out of vectors on large boxen, must share them. */
  641. offset = (offset + 1) % 8;
  642. vector = FIRST_DEVICE_VECTOR + offset;
  643. }
  644. if (unlikely(current_vector == vector))
  645. continue;
  646. if (vector == IA32_SYSCALL_VECTOR)
  647. goto next;
  648. for_each_cpu_mask_nr(new_cpu, new_mask)
  649. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  650. goto next;
  651. /* Found one! */
  652. current_vector = vector;
  653. current_offset = offset;
  654. if (old_vector) {
  655. cfg->move_in_progress = 1;
  656. cfg->old_domain = cfg->domain;
  657. }
  658. for_each_cpu_mask_nr(new_cpu, new_mask)
  659. per_cpu(vector_irq, new_cpu)[vector] = irq;
  660. cfg->vector = vector;
  661. cfg->domain = domain;
  662. return 0;
  663. }
  664. return -ENOSPC;
  665. }
  666. static int assign_irq_vector(int irq, cpumask_t mask)
  667. {
  668. int err;
  669. unsigned long flags;
  670. spin_lock_irqsave(&vector_lock, flags);
  671. err = __assign_irq_vector(irq, mask);
  672. spin_unlock_irqrestore(&vector_lock, flags);
  673. return err;
  674. }
  675. static void __clear_irq_vector(int irq)
  676. {
  677. struct irq_cfg *cfg;
  678. cpumask_t mask;
  679. int cpu, vector;
  680. BUG_ON((unsigned)irq >= NR_IRQS);
  681. cfg = &irq_cfg[irq];
  682. BUG_ON(!cfg->vector);
  683. vector = cfg->vector;
  684. cpus_and(mask, cfg->domain, cpu_online_map);
  685. for_each_cpu_mask_nr(cpu, mask)
  686. per_cpu(vector_irq, cpu)[vector] = -1;
  687. cfg->vector = 0;
  688. cpus_clear(cfg->domain);
  689. }
  690. static void __setup_vector_irq(int cpu)
  691. {
  692. /* Initialize vector_irq on a new cpu */
  693. /* This function must be called with vector_lock held */
  694. int irq, vector;
  695. /* Mark the inuse vectors */
  696. for (irq = 0; irq < NR_IRQS; ++irq) {
  697. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  698. continue;
  699. vector = irq_cfg[irq].vector;
  700. per_cpu(vector_irq, cpu)[vector] = irq;
  701. }
  702. /* Mark the free vectors */
  703. for (vector = 0; vector < NR_VECTORS; ++vector) {
  704. irq = per_cpu(vector_irq, cpu)[vector];
  705. if (irq < 0)
  706. continue;
  707. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  708. per_cpu(vector_irq, cpu)[vector] = -1;
  709. }
  710. }
  711. void setup_vector_irq(int cpu)
  712. {
  713. spin_lock(&vector_lock);
  714. __setup_vector_irq(smp_processor_id());
  715. spin_unlock(&vector_lock);
  716. }
  717. static struct irq_chip ioapic_chip;
  718. static void ioapic_register_intr(int irq, unsigned long trigger)
  719. {
  720. if (trigger) {
  721. irq_desc[irq].status |= IRQ_LEVEL;
  722. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  723. handle_fasteoi_irq, "fasteoi");
  724. } else {
  725. irq_desc[irq].status &= ~IRQ_LEVEL;
  726. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  727. handle_edge_irq, "edge");
  728. }
  729. }
  730. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  731. int trigger, int polarity)
  732. {
  733. struct irq_cfg *cfg = irq_cfg + irq;
  734. struct IO_APIC_route_entry entry;
  735. cpumask_t mask;
  736. if (!IO_APIC_IRQ(irq))
  737. return;
  738. mask = TARGET_CPUS;
  739. if (assign_irq_vector(irq, mask))
  740. return;
  741. cpus_and(mask, cfg->domain, mask);
  742. apic_printk(APIC_VERBOSE,KERN_DEBUG
  743. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  744. "IRQ %d Mode:%i Active:%i)\n",
  745. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  746. irq, trigger, polarity);
  747. /*
  748. * add it to the IO-APIC irq-routing table:
  749. */
  750. memset(&entry,0,sizeof(entry));
  751. entry.delivery_mode = INT_DELIVERY_MODE;
  752. entry.dest_mode = INT_DEST_MODE;
  753. entry.dest = cpu_mask_to_apicid(mask);
  754. entry.mask = 0; /* enable IRQ */
  755. entry.trigger = trigger;
  756. entry.polarity = polarity;
  757. entry.vector = cfg->vector;
  758. /* Mask level triggered irqs.
  759. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  760. */
  761. if (trigger)
  762. entry.mask = 1;
  763. ioapic_register_intr(irq, trigger);
  764. if (irq < 16)
  765. disable_8259A_irq(irq);
  766. ioapic_write_entry(apic, pin, entry);
  767. }
  768. static void __init setup_IO_APIC_irqs(void)
  769. {
  770. int apic, pin, idx, irq, first_notcon = 1;
  771. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  772. for (apic = 0; apic < nr_ioapics; apic++) {
  773. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  774. idx = find_irq_entry(apic,pin,mp_INT);
  775. if (idx == -1) {
  776. if (first_notcon) {
  777. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
  778. first_notcon = 0;
  779. } else
  780. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
  781. continue;
  782. }
  783. if (!first_notcon) {
  784. apic_printk(APIC_VERBOSE, " not connected.\n");
  785. first_notcon = 1;
  786. }
  787. irq = pin_2_irq(idx, apic, pin);
  788. add_pin_to_irq(irq, apic, pin);
  789. setup_IO_APIC_irq(apic, pin, irq,
  790. irq_trigger(idx), irq_polarity(idx));
  791. }
  792. }
  793. if (!first_notcon)
  794. apic_printk(APIC_VERBOSE, " not connected.\n");
  795. }
  796. /*
  797. * Set up the timer pin, possibly with the 8259A-master behind.
  798. */
  799. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  800. int vector)
  801. {
  802. struct IO_APIC_route_entry entry;
  803. memset(&entry, 0, sizeof(entry));
  804. /*
  805. * We use logical delivery to get the timer IRQ
  806. * to the first CPU.
  807. */
  808. entry.dest_mode = INT_DEST_MODE;
  809. entry.mask = 1; /* mask IRQ now */
  810. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  811. entry.delivery_mode = INT_DELIVERY_MODE;
  812. entry.polarity = 0;
  813. entry.trigger = 0;
  814. entry.vector = vector;
  815. /*
  816. * The timer IRQ doesn't have to know that behind the
  817. * scene we may have a 8259A-master in AEOI mode ...
  818. */
  819. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  820. /*
  821. * Add it to the IO-APIC irq-routing table:
  822. */
  823. ioapic_write_entry(apic, pin, entry);
  824. }
  825. __apicdebuginit(void) print_IO_APIC(void)
  826. {
  827. int apic, i;
  828. union IO_APIC_reg_00 reg_00;
  829. union IO_APIC_reg_01 reg_01;
  830. union IO_APIC_reg_02 reg_02;
  831. unsigned long flags;
  832. if (apic_verbosity == APIC_QUIET)
  833. return;
  834. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  835. for (i = 0; i < nr_ioapics; i++)
  836. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  837. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  838. /*
  839. * We are a bit conservative about what we expect. We have to
  840. * know about every hardware change ASAP.
  841. */
  842. printk(KERN_INFO "testing the IO APIC.......................\n");
  843. for (apic = 0; apic < nr_ioapics; apic++) {
  844. spin_lock_irqsave(&ioapic_lock, flags);
  845. reg_00.raw = io_apic_read(apic, 0);
  846. reg_01.raw = io_apic_read(apic, 1);
  847. if (reg_01.bits.version >= 0x10)
  848. reg_02.raw = io_apic_read(apic, 2);
  849. spin_unlock_irqrestore(&ioapic_lock, flags);
  850. printk("\n");
  851. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  852. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  853. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  854. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  855. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  856. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  857. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  858. if (reg_01.bits.version >= 0x10) {
  859. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  860. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  861. }
  862. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  863. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  864. " Stat Dmod Deli Vect: \n");
  865. for (i = 0; i <= reg_01.bits.entries; i++) {
  866. struct IO_APIC_route_entry entry;
  867. entry = ioapic_read_entry(apic, i);
  868. printk(KERN_DEBUG " %02x %03X ",
  869. i,
  870. entry.dest
  871. );
  872. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  873. entry.mask,
  874. entry.trigger,
  875. entry.irr,
  876. entry.polarity,
  877. entry.delivery_status,
  878. entry.dest_mode,
  879. entry.delivery_mode,
  880. entry.vector
  881. );
  882. }
  883. }
  884. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  885. for (i = 0; i < NR_IRQS; i++) {
  886. struct irq_pin_list *entry = irq_2_pin + i;
  887. if (entry->pin < 0)
  888. continue;
  889. printk(KERN_DEBUG "IRQ%d ", i);
  890. for (;;) {
  891. printk("-> %d:%d", entry->apic, entry->pin);
  892. if (!entry->next)
  893. break;
  894. entry = irq_2_pin + entry->next;
  895. }
  896. printk("\n");
  897. }
  898. printk(KERN_INFO ".................................... done.\n");
  899. return;
  900. }
  901. __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. __apicdebuginit(void) 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. __apicdebuginit(void) print_all_local_APICs(void)
  984. {
  985. on_each_cpu(print_local_APIC, NULL, 1);
  986. }
  987. __apicdebuginit(void) 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. __apicdebuginit(int) print_all_ICs(void)
  1010. {
  1011. print_PIC();
  1012. print_all_local_APICs();
  1013. print_IO_APIC();
  1014. return 0;
  1015. }
  1016. fs_initcall(print_all_ICs);
  1017. void __init enable_IO_APIC(void)
  1018. {
  1019. union IO_APIC_reg_01 reg_01;
  1020. int i8259_apic, i8259_pin;
  1021. int i, apic;
  1022. unsigned long flags;
  1023. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1024. irq_2_pin[i].pin = -1;
  1025. irq_2_pin[i].next = 0;
  1026. }
  1027. /*
  1028. * The number of IO-APIC IRQ registers (== #pins):
  1029. */
  1030. for (apic = 0; apic < nr_ioapics; apic++) {
  1031. spin_lock_irqsave(&ioapic_lock, flags);
  1032. reg_01.raw = io_apic_read(apic, 1);
  1033. spin_unlock_irqrestore(&ioapic_lock, flags);
  1034. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1035. }
  1036. for(apic = 0; apic < nr_ioapics; apic++) {
  1037. int pin;
  1038. /* See if any of the pins is in ExtINT mode */
  1039. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1040. struct IO_APIC_route_entry entry;
  1041. entry = ioapic_read_entry(apic, pin);
  1042. /* If the interrupt line is enabled and in ExtInt mode
  1043. * I have found the pin where the i8259 is connected.
  1044. */
  1045. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1046. ioapic_i8259.apic = apic;
  1047. ioapic_i8259.pin = pin;
  1048. goto found_i8259;
  1049. }
  1050. }
  1051. }
  1052. found_i8259:
  1053. /* Look to see what if the MP table has reported the ExtINT */
  1054. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1055. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1056. /* Trust the MP table if nothing is setup in the hardware */
  1057. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1058. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1059. ioapic_i8259.pin = i8259_pin;
  1060. ioapic_i8259.apic = i8259_apic;
  1061. }
  1062. /* Complain if the MP table and the hardware disagree */
  1063. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1064. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1065. {
  1066. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1067. }
  1068. /*
  1069. * Do not trust the IO-APIC being empty at bootup
  1070. */
  1071. clear_IO_APIC();
  1072. }
  1073. /*
  1074. * Not an __init, needed by the reboot code
  1075. */
  1076. void disable_IO_APIC(void)
  1077. {
  1078. /*
  1079. * Clear the IO-APIC before rebooting:
  1080. */
  1081. clear_IO_APIC();
  1082. /*
  1083. * If the i8259 is routed through an IOAPIC
  1084. * Put that IOAPIC in virtual wire mode
  1085. * so legacy interrupts can be delivered.
  1086. */
  1087. if (ioapic_i8259.pin != -1) {
  1088. struct IO_APIC_route_entry entry;
  1089. memset(&entry, 0, sizeof(entry));
  1090. entry.mask = 0; /* Enabled */
  1091. entry.trigger = 0; /* Edge */
  1092. entry.irr = 0;
  1093. entry.polarity = 0; /* High */
  1094. entry.delivery_status = 0;
  1095. entry.dest_mode = 0; /* Physical */
  1096. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1097. entry.vector = 0;
  1098. entry.dest = GET_APIC_ID(read_apic_id());
  1099. /*
  1100. * Add it to the IO-APIC irq-routing table:
  1101. */
  1102. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1103. }
  1104. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1105. }
  1106. /*
  1107. * There is a nasty bug in some older SMP boards, their mptable lies
  1108. * about the timer IRQ. We do the following to work around the situation:
  1109. *
  1110. * - timer IRQ defaults to IO-APIC IRQ
  1111. * - if this function detects that timer IRQs are defunct, then we fall
  1112. * back to ISA timer IRQs
  1113. */
  1114. static int __init timer_irq_works(void)
  1115. {
  1116. unsigned long t1 = jiffies;
  1117. unsigned long flags;
  1118. local_save_flags(flags);
  1119. local_irq_enable();
  1120. /* Let ten ticks pass... */
  1121. mdelay((10 * 1000) / HZ);
  1122. local_irq_restore(flags);
  1123. /*
  1124. * Expect a few ticks at least, to be sure some possible
  1125. * glue logic does not lock up after one or two first
  1126. * ticks in a non-ExtINT mode. Also the local APIC
  1127. * might have cached one ExtINT interrupt. Finally, at
  1128. * least one tick may be lost due to delays.
  1129. */
  1130. /* jiffies wrap? */
  1131. if (time_after(jiffies, t1 + 4))
  1132. return 1;
  1133. return 0;
  1134. }
  1135. /*
  1136. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1137. * number of pending IRQ events unhandled. These cases are very rare,
  1138. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1139. * better to do it this way as thus we do not have to be aware of
  1140. * 'pending' interrupts in the IRQ path, except at this point.
  1141. */
  1142. /*
  1143. * Edge triggered needs to resend any interrupt
  1144. * that was delayed but this is now handled in the device
  1145. * independent code.
  1146. */
  1147. /*
  1148. * Starting up a edge-triggered IO-APIC interrupt is
  1149. * nasty - we need to make sure that we get the edge.
  1150. * If it is already asserted for some reason, we need
  1151. * return 1 to indicate that is was pending.
  1152. *
  1153. * This is not complete - we should be able to fake
  1154. * an edge even if it isn't on the 8259A...
  1155. */
  1156. static unsigned int startup_ioapic_irq(unsigned int irq)
  1157. {
  1158. int was_pending = 0;
  1159. unsigned long flags;
  1160. spin_lock_irqsave(&ioapic_lock, flags);
  1161. if (irq < 16) {
  1162. disable_8259A_irq(irq);
  1163. if (i8259A_irq_pending(irq))
  1164. was_pending = 1;
  1165. }
  1166. __unmask_IO_APIC_irq(irq);
  1167. spin_unlock_irqrestore(&ioapic_lock, flags);
  1168. return was_pending;
  1169. }
  1170. static int ioapic_retrigger_irq(unsigned int irq)
  1171. {
  1172. struct irq_cfg *cfg = &irq_cfg[irq];
  1173. unsigned long flags;
  1174. spin_lock_irqsave(&vector_lock, flags);
  1175. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), 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_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1448. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1449. cfg->vector, apic1, pin1, apic2, pin2);
  1450. /*
  1451. * Some BIOS writers are clueless and report the ExtINTA
  1452. * I/O APIC input from the cascaded 8259A as the timer
  1453. * interrupt input. So just in case, if only one pin
  1454. * was found above, try it both directly and through the
  1455. * 8259A.
  1456. */
  1457. if (pin1 == -1) {
  1458. pin1 = pin2;
  1459. apic1 = apic2;
  1460. no_pin1 = 1;
  1461. } else if (pin2 == -1) {
  1462. pin2 = pin1;
  1463. apic2 = apic1;
  1464. }
  1465. if (pin1 != -1) {
  1466. /*
  1467. * Ok, does IRQ0 through the IOAPIC work?
  1468. */
  1469. if (no_pin1) {
  1470. add_pin_to_irq(0, apic1, pin1);
  1471. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  1472. }
  1473. unmask_IO_APIC_irq(0);
  1474. if (!no_timer_check && timer_irq_works()) {
  1475. if (nmi_watchdog == NMI_IO_APIC) {
  1476. setup_nmi();
  1477. enable_8259A_irq(0);
  1478. }
  1479. if (disable_timer_pin_1 > 0)
  1480. clear_IO_APIC_pin(0, pin1);
  1481. goto out;
  1482. }
  1483. clear_IO_APIC_pin(apic1, pin1);
  1484. if (!no_pin1)
  1485. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1486. "8254 timer not connected to IO-APIC\n");
  1487. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1488. "(IRQ0) through the 8259A ...\n");
  1489. apic_printk(APIC_QUIET, KERN_INFO
  1490. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1491. /*
  1492. * legacy devices should be connected to IO APIC #0
  1493. */
  1494. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1495. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  1496. unmask_IO_APIC_irq(0);
  1497. enable_8259A_irq(0);
  1498. if (timer_irq_works()) {
  1499. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1500. timer_through_8259 = 1;
  1501. if (nmi_watchdog == NMI_IO_APIC) {
  1502. disable_8259A_irq(0);
  1503. setup_nmi();
  1504. enable_8259A_irq(0);
  1505. }
  1506. goto out;
  1507. }
  1508. /*
  1509. * Cleanup, just in case ...
  1510. */
  1511. disable_8259A_irq(0);
  1512. clear_IO_APIC_pin(apic2, pin2);
  1513. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1514. }
  1515. if (nmi_watchdog == NMI_IO_APIC) {
  1516. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  1517. "through the IO-APIC - disabling NMI Watchdog!\n");
  1518. nmi_watchdog = NMI_NONE;
  1519. }
  1520. apic_printk(APIC_QUIET, KERN_INFO
  1521. "...trying to set up timer as Virtual Wire IRQ...\n");
  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_QUIET, KERN_INFO "..... 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_QUIET, KERN_INFO "..... failed.\n");
  1532. apic_printk(APIC_QUIET, KERN_INFO
  1533. "...trying to set up timer as ExtINT IRQ...\n");
  1534. init_8259A(0);
  1535. make_8259A_irq(0);
  1536. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1537. unlock_ExtINT_logic();
  1538. if (timer_irq_works()) {
  1539. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1540. goto out;
  1541. }
  1542. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1543. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1544. "report. Then try booting with the 'noapic' option.\n");
  1545. out:
  1546. local_irq_restore(flags);
  1547. }
  1548. static int __init notimercheck(char *s)
  1549. {
  1550. no_timer_check = 1;
  1551. return 1;
  1552. }
  1553. __setup("no_timer_check", notimercheck);
  1554. /*
  1555. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1556. * to devices. However there may be an I/O APIC pin available for
  1557. * this interrupt regardless. The pin may be left unconnected, but
  1558. * typically it will be reused as an ExtINT cascade interrupt for
  1559. * the master 8259A. In the MPS case such a pin will normally be
  1560. * reported as an ExtINT interrupt in the MP table. With ACPI
  1561. * there is no provision for ExtINT interrupts, and in the absence
  1562. * of an override it would be treated as an ordinary ISA I/O APIC
  1563. * interrupt, that is edge-triggered and unmasked by default. We
  1564. * used to do this, but it caused problems on some systems because
  1565. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1566. * the same ExtINT cascade interrupt to drive the local APIC of the
  1567. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1568. * the I/O APIC in all cases now. No actual device should request
  1569. * it anyway. --macro
  1570. */
  1571. #define PIC_IRQS (1<<2)
  1572. void __init setup_IO_APIC(void)
  1573. {
  1574. /*
  1575. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  1576. */
  1577. io_apic_irqs = ~PIC_IRQS;
  1578. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1579. sync_Arb_IDs();
  1580. setup_IO_APIC_irqs();
  1581. init_IO_APIC_traps();
  1582. check_timer();
  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);