io_apic_64.c 57 KB

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