io_apic_64.c 56 KB

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