io_apic_64.c 57 KB

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