io_apic_64.c 57 KB

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