io_apic_64.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  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 <linux/dmar.h>
  40. #include <asm/idle.h>
  41. #include <asm/io.h>
  42. #include <asm/smp.h>
  43. #include <asm/desc.h>
  44. #include <asm/proto.h>
  45. #include <asm/acpi.h>
  46. #include <asm/dma.h>
  47. #include <asm/i8259.h>
  48. #include <asm/nmi.h>
  49. #include <asm/msidef.h>
  50. #include <asm/hypertransport.h>
  51. #include <asm/irq_remapping.h>
  52. #include <mach_ipi.h>
  53. #include <mach_apic.h>
  54. struct irq_cfg {
  55. cpumask_t domain;
  56. cpumask_t old_domain;
  57. unsigned move_cleanup_count;
  58. u8 vector;
  59. u8 move_in_progress : 1;
  60. };
  61. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  62. static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  63. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  64. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  65. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  66. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  67. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  68. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  69. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  70. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  71. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  72. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  73. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  74. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  75. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  76. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  77. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  78. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  79. };
  80. static int assign_irq_vector(int irq, cpumask_t mask);
  81. int first_system_vector = 0xfe;
  82. char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
  83. #define __apicdebuginit __init
  84. int sis_apic_bug; /* not actually supported, dummy for compile */
  85. static int no_timer_check;
  86. static int disable_timer_pin_1 __initdata;
  87. int timer_through_8259 __initdata;
  88. /* Where if anywhere is the i8259 connect in external int mode */
  89. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  90. static DEFINE_SPINLOCK(ioapic_lock);
  91. DEFINE_SPINLOCK(vector_lock);
  92. /*
  93. * # of IRQ routing registers
  94. */
  95. int nr_ioapic_registers[MAX_IO_APICS];
  96. /* I/O APIC RTE contents at the OS boot up */
  97. struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
  98. /* I/O APIC entries */
  99. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  100. int nr_ioapics;
  101. /* MP IRQ source entries */
  102. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  103. /* # of MP IRQ source entries */
  104. int mp_irq_entries;
  105. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  106. /*
  107. * Rough estimation of how many shared IRQs there are, can
  108. * be changed anytime.
  109. */
  110. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  111. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  112. /*
  113. * This is performance-critical, we want to do it O(1)
  114. *
  115. * the indexing order of this array favors 1:1 mappings
  116. * between pins and IRQs.
  117. */
  118. static struct irq_pin_list {
  119. short apic, pin, next;
  120. } irq_2_pin[PIN_MAP_SIZE];
  121. struct io_apic {
  122. unsigned int index;
  123. unsigned int unused[3];
  124. unsigned int data;
  125. };
  126. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  127. {
  128. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  129. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  130. }
  131. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  132. {
  133. struct io_apic __iomem *io_apic = io_apic_base(apic);
  134. writel(reg, &io_apic->index);
  135. return readl(&io_apic->data);
  136. }
  137. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  138. {
  139. struct io_apic __iomem *io_apic = io_apic_base(apic);
  140. writel(reg, &io_apic->index);
  141. writel(value, &io_apic->data);
  142. }
  143. /*
  144. * Re-write a value: to be used for read-modify-write
  145. * cycles where the read already set up the index register.
  146. */
  147. static inline void io_apic_modify(unsigned int apic, unsigned int value)
  148. {
  149. struct io_apic __iomem *io_apic = io_apic_base(apic);
  150. writel(value, &io_apic->data);
  151. }
  152. static bool io_apic_level_ack_pending(unsigned int irq)
  153. {
  154. struct irq_pin_list *entry;
  155. unsigned long flags;
  156. spin_lock_irqsave(&ioapic_lock, flags);
  157. entry = irq_2_pin + irq;
  158. for (;;) {
  159. unsigned int reg;
  160. int pin;
  161. pin = entry->pin;
  162. if (pin == -1)
  163. break;
  164. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  165. /* Is the remote IRR bit set? */
  166. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  167. spin_unlock_irqrestore(&ioapic_lock, flags);
  168. return true;
  169. }
  170. if (!entry->next)
  171. break;
  172. entry = irq_2_pin + entry->next;
  173. }
  174. spin_unlock_irqrestore(&ioapic_lock, flags);
  175. return false;
  176. }
  177. /*
  178. * Synchronize the IO-APIC and the CPU by doing
  179. * a dummy read from the IO-APIC
  180. */
  181. static inline void io_apic_sync(unsigned int apic)
  182. {
  183. struct io_apic __iomem *io_apic = io_apic_base(apic);
  184. readl(&io_apic->data);
  185. }
  186. #define __DO_ACTION(R, ACTION, FINAL) \
  187. \
  188. { \
  189. int pin; \
  190. struct irq_pin_list *entry = irq_2_pin + irq; \
  191. \
  192. BUG_ON(irq >= NR_IRQS); \
  193. for (;;) { \
  194. unsigned int reg; \
  195. pin = entry->pin; \
  196. if (pin == -1) \
  197. break; \
  198. reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
  199. reg ACTION; \
  200. io_apic_modify(entry->apic, reg); \
  201. FINAL; \
  202. if (!entry->next) \
  203. break; \
  204. entry = irq_2_pin + entry->next; \
  205. } \
  206. }
  207. union entry_union {
  208. struct { u32 w1, w2; };
  209. struct IO_APIC_route_entry entry;
  210. };
  211. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  212. {
  213. union entry_union eu;
  214. unsigned long flags;
  215. spin_lock_irqsave(&ioapic_lock, flags);
  216. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  217. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  218. spin_unlock_irqrestore(&ioapic_lock, flags);
  219. return eu.entry;
  220. }
  221. /*
  222. * When we write a new IO APIC routing entry, we need to write the high
  223. * word first! If the mask bit in the low word is clear, we will enable
  224. * the interrupt, and we need to make sure the entry is fully populated
  225. * before that happens.
  226. */
  227. static void
  228. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  229. {
  230. union entry_union eu;
  231. eu.entry = e;
  232. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  233. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  234. }
  235. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  236. {
  237. unsigned long flags;
  238. spin_lock_irqsave(&ioapic_lock, flags);
  239. __ioapic_write_entry(apic, pin, e);
  240. spin_unlock_irqrestore(&ioapic_lock, flags);
  241. }
  242. /*
  243. * When we mask an IO APIC routing entry, we need to write the low
  244. * word first, in order to set the mask bit before we change the
  245. * high bits!
  246. */
  247. static void ioapic_mask_entry(int apic, int pin)
  248. {
  249. unsigned long flags;
  250. union entry_union eu = { .entry.mask = 1 };
  251. spin_lock_irqsave(&ioapic_lock, flags);
  252. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  253. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  254. spin_unlock_irqrestore(&ioapic_lock, flags);
  255. }
  256. #ifdef CONFIG_SMP
  257. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
  258. {
  259. int apic, pin;
  260. struct irq_pin_list *entry = irq_2_pin + irq;
  261. BUG_ON(irq >= NR_IRQS);
  262. for (;;) {
  263. unsigned int reg;
  264. apic = entry->apic;
  265. pin = entry->pin;
  266. if (pin == -1)
  267. break;
  268. /*
  269. * With interrupt-remapping, destination information comes
  270. * from interrupt-remapping table entry.
  271. */
  272. if (!irq_remapped(irq))
  273. io_apic_write(apic, 0x11 + pin*2, dest);
  274. reg = io_apic_read(apic, 0x10 + pin*2);
  275. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  276. reg |= vector;
  277. io_apic_modify(apic, reg);
  278. if (!entry->next)
  279. break;
  280. entry = irq_2_pin + entry->next;
  281. }
  282. }
  283. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  284. {
  285. struct irq_cfg *cfg = irq_cfg + irq;
  286. unsigned long flags;
  287. unsigned int dest;
  288. cpumask_t tmp;
  289. cpus_and(tmp, mask, cpu_online_map);
  290. if (cpus_empty(tmp))
  291. return;
  292. if (assign_irq_vector(irq, mask))
  293. return;
  294. cpus_and(tmp, cfg->domain, mask);
  295. dest = cpu_mask_to_apicid(tmp);
  296. /*
  297. * Only the high 8 bits are valid.
  298. */
  299. dest = SET_APIC_LOGICAL_ID(dest);
  300. spin_lock_irqsave(&ioapic_lock, flags);
  301. __target_IO_APIC_irq(irq, dest, cfg->vector);
  302. irq_desc[irq].affinity = mask;
  303. spin_unlock_irqrestore(&ioapic_lock, flags);
  304. }
  305. #endif
  306. /*
  307. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  308. * shared ISA-space IRQs, so we have to support them. We are super
  309. * fast in the common case, and fast for shared ISA-space IRQs.
  310. */
  311. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  312. {
  313. static int first_free_entry = NR_IRQS;
  314. struct irq_pin_list *entry = irq_2_pin + irq;
  315. BUG_ON(irq >= NR_IRQS);
  316. while (entry->next)
  317. entry = irq_2_pin + entry->next;
  318. if (entry->pin != -1) {
  319. entry->next = first_free_entry;
  320. entry = irq_2_pin + entry->next;
  321. if (++first_free_entry >= PIN_MAP_SIZE)
  322. panic("io_apic.c: ran out of irq_2_pin entries!");
  323. }
  324. entry->apic = apic;
  325. entry->pin = pin;
  326. }
  327. /*
  328. * Reroute an IRQ to a different pin.
  329. */
  330. static void __init replace_pin_at_irq(unsigned int irq,
  331. int oldapic, int oldpin,
  332. int newapic, int newpin)
  333. {
  334. struct irq_pin_list *entry = irq_2_pin + irq;
  335. while (1) {
  336. if (entry->apic == oldapic && entry->pin == oldpin) {
  337. entry->apic = newapic;
  338. entry->pin = newpin;
  339. }
  340. if (!entry->next)
  341. break;
  342. entry = irq_2_pin + entry->next;
  343. }
  344. }
  345. #define DO_ACTION(name,R,ACTION, FINAL) \
  346. \
  347. static void name##_IO_APIC_irq (unsigned int irq) \
  348. __DO_ACTION(R, ACTION, FINAL)
  349. /* mask = 1 */
  350. DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
  351. /* mask = 0 */
  352. DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
  353. static void mask_IO_APIC_irq (unsigned int irq)
  354. {
  355. unsigned long flags;
  356. spin_lock_irqsave(&ioapic_lock, flags);
  357. __mask_IO_APIC_irq(irq);
  358. spin_unlock_irqrestore(&ioapic_lock, flags);
  359. }
  360. static void unmask_IO_APIC_irq (unsigned int irq)
  361. {
  362. unsigned long flags;
  363. spin_lock_irqsave(&ioapic_lock, flags);
  364. __unmask_IO_APIC_irq(irq);
  365. spin_unlock_irqrestore(&ioapic_lock, flags);
  366. }
  367. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  368. {
  369. struct IO_APIC_route_entry entry;
  370. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  371. entry = ioapic_read_entry(apic, pin);
  372. if (entry.delivery_mode == dest_SMI)
  373. return;
  374. /*
  375. * Disable it in the IO-APIC irq-routing table:
  376. */
  377. ioapic_mask_entry(apic, pin);
  378. }
  379. static void clear_IO_APIC (void)
  380. {
  381. int apic, pin;
  382. for (apic = 0; apic < nr_ioapics; apic++)
  383. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  384. clear_IO_APIC_pin(apic, pin);
  385. }
  386. /*
  387. * Saves and masks all the unmasked IO-APIC RTE's
  388. */
  389. int save_mask_IO_APIC_setup(void)
  390. {
  391. union IO_APIC_reg_01 reg_01;
  392. unsigned long flags;
  393. int apic, pin;
  394. /*
  395. * The number of IO-APIC IRQ registers (== #pins):
  396. */
  397. for (apic = 0; apic < nr_ioapics; apic++) {
  398. spin_lock_irqsave(&ioapic_lock, flags);
  399. reg_01.raw = io_apic_read(apic, 1);
  400. spin_unlock_irqrestore(&ioapic_lock, flags);
  401. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  402. }
  403. for (apic = 0; apic < nr_ioapics; apic++) {
  404. early_ioapic_entries[apic] =
  405. kzalloc(sizeof(struct IO_APIC_route_entry) *
  406. nr_ioapic_registers[apic], GFP_KERNEL);
  407. if (!early_ioapic_entries[apic])
  408. return -ENOMEM;
  409. }
  410. for (apic = 0; apic < nr_ioapics; apic++)
  411. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  412. struct IO_APIC_route_entry entry;
  413. entry = early_ioapic_entries[apic][pin] =
  414. ioapic_read_entry(apic, pin);
  415. if (!entry.mask) {
  416. entry.mask = 1;
  417. ioapic_write_entry(apic, pin, entry);
  418. }
  419. }
  420. return 0;
  421. }
  422. void restore_IO_APIC_setup(void)
  423. {
  424. int apic, pin;
  425. for (apic = 0; apic < nr_ioapics; apic++)
  426. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  427. ioapic_write_entry(apic, pin,
  428. early_ioapic_entries[apic][pin]);
  429. }
  430. void reinit_intr_remapped_IO_APIC(int intr_remapping)
  431. {
  432. /*
  433. * for now plain restore of previous settings.
  434. * TBD: In the case of OS enabling interrupt-remapping,
  435. * IO-APIC RTE's need to be setup to point to interrupt-remapping
  436. * table entries. for now, do a plain restore, and wait for
  437. * the setup_IO_APIC_irqs() to do proper initialization.
  438. */
  439. restore_IO_APIC_setup();
  440. }
  441. int skip_ioapic_setup;
  442. int ioapic_force;
  443. static int __init parse_noapic(char *str)
  444. {
  445. disable_ioapic_setup();
  446. return 0;
  447. }
  448. early_param("noapic", parse_noapic);
  449. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  450. static int __init disable_timer_pin_setup(char *arg)
  451. {
  452. disable_timer_pin_1 = 1;
  453. return 1;
  454. }
  455. __setup("disable_timer_pin_1", disable_timer_pin_setup);
  456. /*
  457. * Find the IRQ entry number of a certain pin.
  458. */
  459. static int find_irq_entry(int apic, int pin, int type)
  460. {
  461. int i;
  462. for (i = 0; i < mp_irq_entries; i++)
  463. if (mp_irqs[i].mp_irqtype == type &&
  464. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  465. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  466. mp_irqs[i].mp_dstirq == pin)
  467. return i;
  468. return -1;
  469. }
  470. /*
  471. * Find the pin to which IRQ[irq] (ISA) is connected
  472. */
  473. static int __init find_isa_irq_pin(int irq, int type)
  474. {
  475. int i;
  476. for (i = 0; i < mp_irq_entries; i++) {
  477. int lbus = mp_irqs[i].mp_srcbus;
  478. if (test_bit(lbus, mp_bus_not_pci) &&
  479. (mp_irqs[i].mp_irqtype == type) &&
  480. (mp_irqs[i].mp_srcbusirq == irq))
  481. return mp_irqs[i].mp_dstirq;
  482. }
  483. return -1;
  484. }
  485. static int __init find_isa_irq_apic(int irq, int type)
  486. {
  487. int i;
  488. for (i = 0; i < mp_irq_entries; i++) {
  489. int lbus = mp_irqs[i].mp_srcbus;
  490. if (test_bit(lbus, mp_bus_not_pci) &&
  491. (mp_irqs[i].mp_irqtype == type) &&
  492. (mp_irqs[i].mp_srcbusirq == irq))
  493. break;
  494. }
  495. if (i < mp_irq_entries) {
  496. int apic;
  497. for(apic = 0; apic < nr_ioapics; apic++) {
  498. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  499. return apic;
  500. }
  501. }
  502. return -1;
  503. }
  504. /*
  505. * Find a specific PCI IRQ entry.
  506. * Not an __init, possibly needed by modules
  507. */
  508. static int pin_2_irq(int idx, int apic, int pin);
  509. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  510. {
  511. int apic, i, best_guess = -1;
  512. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  513. bus, slot, pin);
  514. if (test_bit(bus, mp_bus_not_pci)) {
  515. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  516. return -1;
  517. }
  518. for (i = 0; i < mp_irq_entries; i++) {
  519. int lbus = mp_irqs[i].mp_srcbus;
  520. for (apic = 0; apic < nr_ioapics; apic++)
  521. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  522. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  523. break;
  524. if (!test_bit(lbus, mp_bus_not_pci) &&
  525. !mp_irqs[i].mp_irqtype &&
  526. (bus == lbus) &&
  527. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  528. int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
  529. if (!(apic || IO_APIC_IRQ(irq)))
  530. continue;
  531. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  532. return irq;
  533. /*
  534. * Use the first all-but-pin matching entry as a
  535. * best-guess fuzzy result for broken mptables.
  536. */
  537. if (best_guess < 0)
  538. best_guess = irq;
  539. }
  540. }
  541. BUG_ON(best_guess >= NR_IRQS);
  542. return best_guess;
  543. }
  544. /* ISA interrupts are always polarity zero edge triggered,
  545. * when listed as conforming in the MP table. */
  546. #define default_ISA_trigger(idx) (0)
  547. #define default_ISA_polarity(idx) (0)
  548. /* PCI interrupts are always polarity one level triggered,
  549. * when listed as conforming in the MP table. */
  550. #define default_PCI_trigger(idx) (1)
  551. #define default_PCI_polarity(idx) (1)
  552. static int MPBIOS_polarity(int idx)
  553. {
  554. int bus = mp_irqs[idx].mp_srcbus;
  555. int polarity;
  556. /*
  557. * Determine IRQ line polarity (high active or low active):
  558. */
  559. switch (mp_irqs[idx].mp_irqflag & 3)
  560. {
  561. case 0: /* conforms, ie. bus-type dependent polarity */
  562. if (test_bit(bus, mp_bus_not_pci))
  563. polarity = default_ISA_polarity(idx);
  564. else
  565. polarity = default_PCI_polarity(idx);
  566. break;
  567. case 1: /* high active */
  568. {
  569. polarity = 0;
  570. break;
  571. }
  572. case 2: /* reserved */
  573. {
  574. printk(KERN_WARNING "broken BIOS!!\n");
  575. polarity = 1;
  576. break;
  577. }
  578. case 3: /* low active */
  579. {
  580. polarity = 1;
  581. break;
  582. }
  583. default: /* invalid */
  584. {
  585. printk(KERN_WARNING "broken BIOS!!\n");
  586. polarity = 1;
  587. break;
  588. }
  589. }
  590. return polarity;
  591. }
  592. static int MPBIOS_trigger(int idx)
  593. {
  594. int bus = mp_irqs[idx].mp_srcbus;
  595. int trigger;
  596. /*
  597. * Determine IRQ trigger mode (edge or level sensitive):
  598. */
  599. switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
  600. {
  601. case 0: /* conforms, ie. bus-type dependent */
  602. if (test_bit(bus, mp_bus_not_pci))
  603. trigger = default_ISA_trigger(idx);
  604. else
  605. trigger = default_PCI_trigger(idx);
  606. break;
  607. case 1: /* edge */
  608. {
  609. trigger = 0;
  610. break;
  611. }
  612. case 2: /* reserved */
  613. {
  614. printk(KERN_WARNING "broken BIOS!!\n");
  615. trigger = 1;
  616. break;
  617. }
  618. case 3: /* level */
  619. {
  620. trigger = 1;
  621. break;
  622. }
  623. default: /* invalid */
  624. {
  625. printk(KERN_WARNING "broken BIOS!!\n");
  626. trigger = 0;
  627. break;
  628. }
  629. }
  630. return trigger;
  631. }
  632. static inline int irq_polarity(int idx)
  633. {
  634. return MPBIOS_polarity(idx);
  635. }
  636. static inline int irq_trigger(int idx)
  637. {
  638. return MPBIOS_trigger(idx);
  639. }
  640. static int pin_2_irq(int idx, int apic, int pin)
  641. {
  642. int irq, i;
  643. int bus = mp_irqs[idx].mp_srcbus;
  644. /*
  645. * Debugging check, we are in big trouble if this message pops up!
  646. */
  647. if (mp_irqs[idx].mp_dstirq != pin)
  648. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  649. if (test_bit(bus, mp_bus_not_pci)) {
  650. irq = mp_irqs[idx].mp_srcbusirq;
  651. } else {
  652. /*
  653. * PCI IRQs are mapped in order
  654. */
  655. i = irq = 0;
  656. while (i < apic)
  657. irq += nr_ioapic_registers[i++];
  658. irq += pin;
  659. }
  660. BUG_ON(irq >= NR_IRQS);
  661. return irq;
  662. }
  663. static int __assign_irq_vector(int irq, cpumask_t mask)
  664. {
  665. /*
  666. * NOTE! The local APIC isn't very good at handling
  667. * multiple interrupts at the same interrupt level.
  668. * As the interrupt level is determined by taking the
  669. * vector number and shifting that right by 4, we
  670. * want to spread these out a bit so that they don't
  671. * all fall in the same interrupt level.
  672. *
  673. * Also, we've got to be careful not to trash gate
  674. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  675. */
  676. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  677. unsigned int old_vector;
  678. int cpu;
  679. struct irq_cfg *cfg;
  680. BUG_ON((unsigned)irq >= NR_IRQS);
  681. cfg = &irq_cfg[irq];
  682. /* Only try and allocate irqs on cpus that are present */
  683. cpus_and(mask, mask, cpu_online_map);
  684. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  685. return -EBUSY;
  686. old_vector = cfg->vector;
  687. if (old_vector) {
  688. cpumask_t tmp;
  689. cpus_and(tmp, cfg->domain, mask);
  690. if (!cpus_empty(tmp))
  691. return 0;
  692. }
  693. for_each_cpu_mask(cpu, mask) {
  694. cpumask_t domain, new_mask;
  695. int new_cpu;
  696. int vector, offset;
  697. domain = vector_allocation_domain(cpu);
  698. cpus_and(new_mask, domain, cpu_online_map);
  699. vector = current_vector;
  700. offset = current_offset;
  701. next:
  702. vector += 8;
  703. if (vector >= first_system_vector) {
  704. /* If we run out of vectors on large boxen, must share them. */
  705. offset = (offset + 1) % 8;
  706. vector = FIRST_DEVICE_VECTOR + offset;
  707. }
  708. if (unlikely(current_vector == vector))
  709. continue;
  710. if (vector == IA32_SYSCALL_VECTOR)
  711. goto next;
  712. for_each_cpu_mask(new_cpu, new_mask)
  713. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  714. goto next;
  715. /* Found one! */
  716. current_vector = vector;
  717. current_offset = offset;
  718. if (old_vector) {
  719. cfg->move_in_progress = 1;
  720. cfg->old_domain = cfg->domain;
  721. }
  722. for_each_cpu_mask(new_cpu, new_mask)
  723. per_cpu(vector_irq, new_cpu)[vector] = irq;
  724. cfg->vector = vector;
  725. cfg->domain = domain;
  726. return 0;
  727. }
  728. return -ENOSPC;
  729. }
  730. static int assign_irq_vector(int irq, cpumask_t mask)
  731. {
  732. int err;
  733. unsigned long flags;
  734. spin_lock_irqsave(&vector_lock, flags);
  735. err = __assign_irq_vector(irq, mask);
  736. spin_unlock_irqrestore(&vector_lock, flags);
  737. return err;
  738. }
  739. static void __clear_irq_vector(int irq)
  740. {
  741. struct irq_cfg *cfg;
  742. cpumask_t mask;
  743. int cpu, vector;
  744. BUG_ON((unsigned)irq >= NR_IRQS);
  745. cfg = &irq_cfg[irq];
  746. BUG_ON(!cfg->vector);
  747. vector = cfg->vector;
  748. cpus_and(mask, cfg->domain, cpu_online_map);
  749. for_each_cpu_mask(cpu, mask)
  750. per_cpu(vector_irq, cpu)[vector] = -1;
  751. cfg->vector = 0;
  752. cpus_clear(cfg->domain);
  753. }
  754. static void __setup_vector_irq(int cpu)
  755. {
  756. /* Initialize vector_irq on a new cpu */
  757. /* This function must be called with vector_lock held */
  758. int irq, vector;
  759. /* Mark the inuse vectors */
  760. for (irq = 0; irq < NR_IRQS; ++irq) {
  761. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  762. continue;
  763. vector = irq_cfg[irq].vector;
  764. per_cpu(vector_irq, cpu)[vector] = irq;
  765. }
  766. /* Mark the free vectors */
  767. for (vector = 0; vector < NR_VECTORS; ++vector) {
  768. irq = per_cpu(vector_irq, cpu)[vector];
  769. if (irq < 0)
  770. continue;
  771. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  772. per_cpu(vector_irq, cpu)[vector] = -1;
  773. }
  774. }
  775. void setup_vector_irq(int cpu)
  776. {
  777. spin_lock(&vector_lock);
  778. __setup_vector_irq(smp_processor_id());
  779. spin_unlock(&vector_lock);
  780. }
  781. static struct irq_chip ioapic_chip;
  782. #ifdef CONFIG_INTR_REMAP
  783. static struct irq_chip ir_ioapic_chip;
  784. #endif
  785. static void ioapic_register_intr(int irq, unsigned long trigger)
  786. {
  787. if (trigger)
  788. irq_desc[irq].status |= IRQ_LEVEL;
  789. else
  790. irq_desc[irq].status &= ~IRQ_LEVEL;
  791. #ifdef CONFIG_INTR_REMAP
  792. if (irq_remapped(irq)) {
  793. irq_desc[irq].status |= IRQ_MOVE_PCNTXT;
  794. if (trigger)
  795. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  796. handle_fasteoi_irq,
  797. "fasteoi");
  798. else
  799. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  800. handle_edge_irq, "edge");
  801. return;
  802. }
  803. #endif
  804. if (trigger)
  805. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  806. handle_fasteoi_irq,
  807. "fasteoi");
  808. else
  809. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  810. handle_edge_irq, "edge");
  811. }
  812. static int setup_ioapic_entry(int apic, int irq,
  813. struct IO_APIC_route_entry *entry,
  814. unsigned int destination, int trigger,
  815. int polarity, int vector)
  816. {
  817. /*
  818. * add it to the IO-APIC irq-routing table:
  819. */
  820. memset(entry,0,sizeof(*entry));
  821. #ifdef CONFIG_INTR_REMAP
  822. if (intr_remapping_enabled) {
  823. struct intel_iommu *iommu = map_ioapic_to_ir(apic);
  824. struct irte irte;
  825. struct IR_IO_APIC_route_entry *ir_entry =
  826. (struct IR_IO_APIC_route_entry *) entry;
  827. int index;
  828. if (!iommu)
  829. panic("No mapping iommu for ioapic %d\n", apic);
  830. index = alloc_irte(iommu, irq, 1);
  831. if (index < 0)
  832. panic("Failed to allocate IRTE for ioapic %d\n", apic);
  833. memset(&irte, 0, sizeof(irte));
  834. irte.present = 1;
  835. irte.dst_mode = INT_DEST_MODE;
  836. irte.trigger_mode = trigger;
  837. irte.dlvry_mode = INT_DELIVERY_MODE;
  838. irte.vector = vector;
  839. irte.dest_id = IRTE_DEST(destination);
  840. modify_irte(irq, &irte);
  841. ir_entry->index2 = (index >> 15) & 0x1;
  842. ir_entry->zero = 0;
  843. ir_entry->format = 1;
  844. ir_entry->index = (index & 0x7fff);
  845. } else
  846. #endif
  847. {
  848. entry->delivery_mode = INT_DELIVERY_MODE;
  849. entry->dest_mode = INT_DEST_MODE;
  850. entry->dest = destination;
  851. }
  852. entry->mask = 0; /* enable IRQ */
  853. entry->trigger = trigger;
  854. entry->polarity = polarity;
  855. entry->vector = vector;
  856. /* Mask level triggered irqs.
  857. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  858. */
  859. if (trigger)
  860. entry->mask = 1;
  861. return 0;
  862. }
  863. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  864. int trigger, int polarity)
  865. {
  866. struct irq_cfg *cfg = irq_cfg + irq;
  867. struct IO_APIC_route_entry entry;
  868. cpumask_t mask;
  869. if (!IO_APIC_IRQ(irq))
  870. return;
  871. mask = TARGET_CPUS;
  872. if (assign_irq_vector(irq, mask))
  873. return;
  874. cpus_and(mask, cfg->domain, mask);
  875. apic_printk(APIC_VERBOSE,KERN_DEBUG
  876. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  877. "IRQ %d Mode:%i Active:%i)\n",
  878. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  879. irq, trigger, polarity);
  880. if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
  881. cpu_mask_to_apicid(mask), trigger, polarity,
  882. cfg->vector)) {
  883. printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  884. mp_ioapics[apic].mp_apicid, pin);
  885. __clear_irq_vector(irq);
  886. return;
  887. }
  888. ioapic_register_intr(irq, trigger);
  889. if (irq < 16)
  890. disable_8259A_irq(irq);
  891. ioapic_write_entry(apic, pin, entry);
  892. }
  893. static void __init setup_IO_APIC_irqs(void)
  894. {
  895. int apic, pin, idx, irq, first_notcon = 1;
  896. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  897. for (apic = 0; apic < nr_ioapics; apic++) {
  898. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  899. idx = find_irq_entry(apic,pin,mp_INT);
  900. if (idx == -1) {
  901. if (first_notcon) {
  902. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
  903. first_notcon = 0;
  904. } else
  905. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
  906. continue;
  907. }
  908. if (!first_notcon) {
  909. apic_printk(APIC_VERBOSE, " not connected.\n");
  910. first_notcon = 1;
  911. }
  912. irq = pin_2_irq(idx, apic, pin);
  913. add_pin_to_irq(irq, apic, pin);
  914. setup_IO_APIC_irq(apic, pin, irq,
  915. irq_trigger(idx), irq_polarity(idx));
  916. }
  917. }
  918. if (!first_notcon)
  919. apic_printk(APIC_VERBOSE, " not connected.\n");
  920. }
  921. /*
  922. * Set up the timer pin, possibly with the 8259A-master behind.
  923. */
  924. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  925. int vector)
  926. {
  927. struct IO_APIC_route_entry entry;
  928. if (intr_remapping_enabled)
  929. return;
  930. memset(&entry, 0, sizeof(entry));
  931. /*
  932. * We use logical delivery to get the timer IRQ
  933. * to the first CPU.
  934. */
  935. entry.dest_mode = INT_DEST_MODE;
  936. entry.mask = 1; /* mask IRQ now */
  937. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  938. entry.delivery_mode = INT_DELIVERY_MODE;
  939. entry.polarity = 0;
  940. entry.trigger = 0;
  941. entry.vector = vector;
  942. /*
  943. * The timer IRQ doesn't have to know that behind the
  944. * scene we may have a 8259A-master in AEOI mode ...
  945. */
  946. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  947. /*
  948. * Add it to the IO-APIC irq-routing table:
  949. */
  950. ioapic_write_entry(apic, pin, entry);
  951. }
  952. void __apicdebuginit print_IO_APIC(void)
  953. {
  954. int apic, i;
  955. union IO_APIC_reg_00 reg_00;
  956. union IO_APIC_reg_01 reg_01;
  957. union IO_APIC_reg_02 reg_02;
  958. unsigned long flags;
  959. if (apic_verbosity == APIC_QUIET)
  960. return;
  961. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  962. for (i = 0; i < nr_ioapics; i++)
  963. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  964. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  965. /*
  966. * We are a bit conservative about what we expect. We have to
  967. * know about every hardware change ASAP.
  968. */
  969. printk(KERN_INFO "testing the IO APIC.......................\n");
  970. for (apic = 0; apic < nr_ioapics; apic++) {
  971. spin_lock_irqsave(&ioapic_lock, flags);
  972. reg_00.raw = io_apic_read(apic, 0);
  973. reg_01.raw = io_apic_read(apic, 1);
  974. if (reg_01.bits.version >= 0x10)
  975. reg_02.raw = io_apic_read(apic, 2);
  976. spin_unlock_irqrestore(&ioapic_lock, flags);
  977. printk("\n");
  978. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  979. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  980. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  981. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  982. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  983. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  984. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  985. if (reg_01.bits.version >= 0x10) {
  986. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  987. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  988. }
  989. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  990. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  991. " Stat Dmod Deli Vect: \n");
  992. for (i = 0; i <= reg_01.bits.entries; i++) {
  993. struct IO_APIC_route_entry entry;
  994. entry = ioapic_read_entry(apic, i);
  995. printk(KERN_DEBUG " %02x %03X ",
  996. i,
  997. entry.dest
  998. );
  999. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1000. entry.mask,
  1001. entry.trigger,
  1002. entry.irr,
  1003. entry.polarity,
  1004. entry.delivery_status,
  1005. entry.dest_mode,
  1006. entry.delivery_mode,
  1007. entry.vector
  1008. );
  1009. }
  1010. }
  1011. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1012. for (i = 0; i < NR_IRQS; i++) {
  1013. struct irq_pin_list *entry = irq_2_pin + i;
  1014. if (entry->pin < 0)
  1015. continue;
  1016. printk(KERN_DEBUG "IRQ%d ", i);
  1017. for (;;) {
  1018. printk("-> %d:%d", entry->apic, entry->pin);
  1019. if (!entry->next)
  1020. break;
  1021. entry = irq_2_pin + entry->next;
  1022. }
  1023. printk("\n");
  1024. }
  1025. printk(KERN_INFO ".................................... done.\n");
  1026. return;
  1027. }
  1028. #if 0
  1029. static __apicdebuginit void print_APIC_bitfield (int base)
  1030. {
  1031. unsigned int v;
  1032. int i, j;
  1033. if (apic_verbosity == APIC_QUIET)
  1034. return;
  1035. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1036. for (i = 0; i < 8; i++) {
  1037. v = apic_read(base + i*0x10);
  1038. for (j = 0; j < 32; j++) {
  1039. if (v & (1<<j))
  1040. printk("1");
  1041. else
  1042. printk("0");
  1043. }
  1044. printk("\n");
  1045. }
  1046. }
  1047. void __apicdebuginit print_local_APIC(void * dummy)
  1048. {
  1049. unsigned int v, ver, maxlvt;
  1050. unsigned long icr;
  1051. if (apic_verbosity == APIC_QUIET)
  1052. return;
  1053. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1054. smp_processor_id(), hard_smp_processor_id());
  1055. v = apic_read(APIC_ID);
  1056. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1057. v = apic_read(APIC_LVR);
  1058. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1059. ver = GET_APIC_VERSION(v);
  1060. maxlvt = lapic_get_maxlvt();
  1061. v = apic_read(APIC_TASKPRI);
  1062. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1063. v = apic_read(APIC_ARBPRI);
  1064. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1065. v & APIC_ARBPRI_MASK);
  1066. v = apic_read(APIC_PROCPRI);
  1067. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1068. v = apic_read(APIC_EOI);
  1069. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1070. v = apic_read(APIC_RRR);
  1071. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1072. v = apic_read(APIC_LDR);
  1073. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1074. v = apic_read(APIC_DFR);
  1075. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1076. v = apic_read(APIC_SPIV);
  1077. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1078. printk(KERN_DEBUG "... APIC ISR field:\n");
  1079. print_APIC_bitfield(APIC_ISR);
  1080. printk(KERN_DEBUG "... APIC TMR field:\n");
  1081. print_APIC_bitfield(APIC_TMR);
  1082. printk(KERN_DEBUG "... APIC IRR field:\n");
  1083. print_APIC_bitfield(APIC_IRR);
  1084. v = apic_read(APIC_ESR);
  1085. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1086. icr = apic_icr_read();
  1087. printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
  1088. printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
  1089. v = apic_read(APIC_LVTT);
  1090. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1091. if (maxlvt > 3) { /* PC is LVT#4. */
  1092. v = apic_read(APIC_LVTPC);
  1093. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1094. }
  1095. v = apic_read(APIC_LVT0);
  1096. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1097. v = apic_read(APIC_LVT1);
  1098. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1099. if (maxlvt > 2) { /* ERR is LVT#3. */
  1100. v = apic_read(APIC_LVTERR);
  1101. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1102. }
  1103. v = apic_read(APIC_TMICT);
  1104. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1105. v = apic_read(APIC_TMCCT);
  1106. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1107. v = apic_read(APIC_TDCR);
  1108. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1109. printk("\n");
  1110. }
  1111. void print_all_local_APICs (void)
  1112. {
  1113. on_each_cpu(print_local_APIC, NULL, 1);
  1114. }
  1115. void __apicdebuginit print_PIC(void)
  1116. {
  1117. unsigned int v;
  1118. unsigned long flags;
  1119. if (apic_verbosity == APIC_QUIET)
  1120. return;
  1121. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1122. spin_lock_irqsave(&i8259A_lock, flags);
  1123. v = inb(0xa1) << 8 | inb(0x21);
  1124. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1125. v = inb(0xa0) << 8 | inb(0x20);
  1126. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1127. outb(0x0b,0xa0);
  1128. outb(0x0b,0x20);
  1129. v = inb(0xa0) << 8 | inb(0x20);
  1130. outb(0x0a,0xa0);
  1131. outb(0x0a,0x20);
  1132. spin_unlock_irqrestore(&i8259A_lock, flags);
  1133. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1134. v = inb(0x4d1) << 8 | inb(0x4d0);
  1135. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1136. }
  1137. #endif /* 0 */
  1138. void __init enable_IO_APIC(void)
  1139. {
  1140. union IO_APIC_reg_01 reg_01;
  1141. int i8259_apic, i8259_pin;
  1142. int i, apic;
  1143. unsigned long flags;
  1144. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1145. irq_2_pin[i].pin = -1;
  1146. irq_2_pin[i].next = 0;
  1147. }
  1148. /*
  1149. * The number of IO-APIC IRQ registers (== #pins):
  1150. */
  1151. for (apic = 0; apic < nr_ioapics; apic++) {
  1152. spin_lock_irqsave(&ioapic_lock, flags);
  1153. reg_01.raw = io_apic_read(apic, 1);
  1154. spin_unlock_irqrestore(&ioapic_lock, flags);
  1155. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1156. }
  1157. for(apic = 0; apic < nr_ioapics; apic++) {
  1158. int pin;
  1159. /* See if any of the pins is in ExtINT mode */
  1160. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1161. struct IO_APIC_route_entry entry;
  1162. entry = ioapic_read_entry(apic, pin);
  1163. /* If the interrupt line is enabled and in ExtInt mode
  1164. * I have found the pin where the i8259 is connected.
  1165. */
  1166. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1167. ioapic_i8259.apic = apic;
  1168. ioapic_i8259.pin = pin;
  1169. goto found_i8259;
  1170. }
  1171. }
  1172. }
  1173. found_i8259:
  1174. /* Look to see what if the MP table has reported the ExtINT */
  1175. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1176. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1177. /* Trust the MP table if nothing is setup in the hardware */
  1178. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1179. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1180. ioapic_i8259.pin = i8259_pin;
  1181. ioapic_i8259.apic = i8259_apic;
  1182. }
  1183. /* Complain if the MP table and the hardware disagree */
  1184. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1185. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1186. {
  1187. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1188. }
  1189. /*
  1190. * Do not trust the IO-APIC being empty at bootup
  1191. */
  1192. clear_IO_APIC();
  1193. }
  1194. /*
  1195. * Not an __init, needed by the reboot code
  1196. */
  1197. void disable_IO_APIC(void)
  1198. {
  1199. /*
  1200. * Clear the IO-APIC before rebooting:
  1201. */
  1202. clear_IO_APIC();
  1203. /*
  1204. * If the i8259 is routed through an IOAPIC
  1205. * Put that IOAPIC in virtual wire mode
  1206. * so legacy interrupts can be delivered.
  1207. */
  1208. if (ioapic_i8259.pin != -1) {
  1209. struct IO_APIC_route_entry entry;
  1210. memset(&entry, 0, sizeof(entry));
  1211. entry.mask = 0; /* Enabled */
  1212. entry.trigger = 0; /* Edge */
  1213. entry.irr = 0;
  1214. entry.polarity = 0; /* High */
  1215. entry.delivery_status = 0;
  1216. entry.dest_mode = 0; /* Physical */
  1217. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1218. entry.vector = 0;
  1219. entry.dest = read_apic_id();
  1220. /*
  1221. * Add it to the IO-APIC irq-routing table:
  1222. */
  1223. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1224. }
  1225. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1226. }
  1227. /*
  1228. * There is a nasty bug in some older SMP boards, their mptable lies
  1229. * about the timer IRQ. We do the following to work around the situation:
  1230. *
  1231. * - timer IRQ defaults to IO-APIC IRQ
  1232. * - if this function detects that timer IRQs are defunct, then we fall
  1233. * back to ISA timer IRQs
  1234. */
  1235. static int __init timer_irq_works(void)
  1236. {
  1237. unsigned long t1 = jiffies;
  1238. unsigned long flags;
  1239. local_save_flags(flags);
  1240. local_irq_enable();
  1241. /* Let ten ticks pass... */
  1242. mdelay((10 * 1000) / HZ);
  1243. local_irq_restore(flags);
  1244. /*
  1245. * Expect a few ticks at least, to be sure some possible
  1246. * glue logic does not lock up after one or two first
  1247. * ticks in a non-ExtINT mode. Also the local APIC
  1248. * might have cached one ExtINT interrupt. Finally, at
  1249. * least one tick may be lost due to delays.
  1250. */
  1251. /* jiffies wrap? */
  1252. if (time_after(jiffies, t1 + 4))
  1253. return 1;
  1254. return 0;
  1255. }
  1256. /*
  1257. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1258. * number of pending IRQ events unhandled. These cases are very rare,
  1259. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1260. * better to do it this way as thus we do not have to be aware of
  1261. * 'pending' interrupts in the IRQ path, except at this point.
  1262. */
  1263. /*
  1264. * Edge triggered needs to resend any interrupt
  1265. * that was delayed but this is now handled in the device
  1266. * independent code.
  1267. */
  1268. /*
  1269. * Starting up a edge-triggered IO-APIC interrupt is
  1270. * nasty - we need to make sure that we get the edge.
  1271. * If it is already asserted for some reason, we need
  1272. * return 1 to indicate that is was pending.
  1273. *
  1274. * This is not complete - we should be able to fake
  1275. * an edge even if it isn't on the 8259A...
  1276. */
  1277. static unsigned int startup_ioapic_irq(unsigned int irq)
  1278. {
  1279. int was_pending = 0;
  1280. unsigned long flags;
  1281. spin_lock_irqsave(&ioapic_lock, flags);
  1282. if (irq < 16) {
  1283. disable_8259A_irq(irq);
  1284. if (i8259A_irq_pending(irq))
  1285. was_pending = 1;
  1286. }
  1287. __unmask_IO_APIC_irq(irq);
  1288. spin_unlock_irqrestore(&ioapic_lock, flags);
  1289. return was_pending;
  1290. }
  1291. static int ioapic_retrigger_irq(unsigned int irq)
  1292. {
  1293. struct irq_cfg *cfg = &irq_cfg[irq];
  1294. cpumask_t mask;
  1295. unsigned long flags;
  1296. spin_lock_irqsave(&vector_lock, flags);
  1297. mask = cpumask_of_cpu(first_cpu(cfg->domain));
  1298. send_IPI_mask(mask, cfg->vector);
  1299. spin_unlock_irqrestore(&vector_lock, flags);
  1300. return 1;
  1301. }
  1302. /*
  1303. * Level and edge triggered IO-APIC interrupts need different handling,
  1304. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1305. * handled with the level-triggered descriptor, but that one has slightly
  1306. * more overhead. Level-triggered interrupts cannot be handled with the
  1307. * edge-triggered handler, without risking IRQ storms and other ugly
  1308. * races.
  1309. */
  1310. #ifdef CONFIG_SMP
  1311. #ifdef CONFIG_INTR_REMAP
  1312. static void ir_irq_migration(struct work_struct *work);
  1313. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1314. /*
  1315. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1316. *
  1317. * For edge triggered, irq migration is a simple atomic update(of vector
  1318. * and cpu destination) of IRTE and flush the hardware cache.
  1319. *
  1320. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1321. * vector information, along with modifying IRTE with vector and destination.
  1322. * So irq migration for level triggered is little bit more complex compared to
  1323. * edge triggered migration. But the good news is, we use the same algorithm
  1324. * for level triggered migration as we have today, only difference being,
  1325. * we now initiate the irq migration from process context instead of the
  1326. * interrupt context.
  1327. *
  1328. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1329. * suppression) to the IO-APIC, level triggered irq migration will also be
  1330. * as simple as edge triggered migration and we can do the irq migration
  1331. * with a simple atomic update to IO-APIC RTE.
  1332. */
  1333. static void migrate_ioapic_irq(int irq, cpumask_t mask)
  1334. {
  1335. struct irq_cfg *cfg = irq_cfg + irq;
  1336. struct irq_desc *desc = irq_desc + irq;
  1337. cpumask_t tmp, cleanup_mask;
  1338. struct irte irte;
  1339. int modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1340. unsigned int dest;
  1341. unsigned long flags;
  1342. cpus_and(tmp, mask, cpu_online_map);
  1343. if (cpus_empty(tmp))
  1344. return;
  1345. if (get_irte(irq, &irte))
  1346. return;
  1347. if (assign_irq_vector(irq, mask))
  1348. return;
  1349. cpus_and(tmp, cfg->domain, mask);
  1350. dest = cpu_mask_to_apicid(tmp);
  1351. if (modify_ioapic_rte) {
  1352. spin_lock_irqsave(&ioapic_lock, flags);
  1353. __target_IO_APIC_irq(irq, dest, cfg->vector);
  1354. spin_unlock_irqrestore(&ioapic_lock, flags);
  1355. }
  1356. irte.vector = cfg->vector;
  1357. irte.dest_id = IRTE_DEST(dest);
  1358. /*
  1359. * Modified the IRTE and flushes the Interrupt entry cache.
  1360. */
  1361. modify_irte(irq, &irte);
  1362. if (cfg->move_in_progress) {
  1363. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1364. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1365. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1366. cfg->move_in_progress = 0;
  1367. }
  1368. irq_desc[irq].affinity = mask;
  1369. }
  1370. static int migrate_irq_remapped_level(int irq)
  1371. {
  1372. int ret = -1;
  1373. mask_IO_APIC_irq(irq);
  1374. if (io_apic_level_ack_pending(irq)) {
  1375. /*
  1376. * Interrupt in progress. Migrating irq now will change the
  1377. * vector information in the IO-APIC RTE and that will confuse
  1378. * the EOI broadcast performed by cpu.
  1379. * So, delay the irq migration to the next instance.
  1380. */
  1381. schedule_delayed_work(&ir_migration_work, 1);
  1382. goto unmask;
  1383. }
  1384. /* everthing is clear. we have right of way */
  1385. migrate_ioapic_irq(irq, irq_desc[irq].pending_mask);
  1386. ret = 0;
  1387. irq_desc[irq].status &= ~IRQ_MOVE_PENDING;
  1388. cpus_clear(irq_desc[irq].pending_mask);
  1389. unmask:
  1390. unmask_IO_APIC_irq(irq);
  1391. return ret;
  1392. }
  1393. static void ir_irq_migration(struct work_struct *work)
  1394. {
  1395. int irq;
  1396. for (irq = 0; irq < NR_IRQS; irq++) {
  1397. struct irq_desc *desc = irq_desc + irq;
  1398. if (desc->status & IRQ_MOVE_PENDING) {
  1399. unsigned long flags;
  1400. spin_lock_irqsave(&desc->lock, flags);
  1401. if (!desc->chip->set_affinity ||
  1402. !(desc->status & IRQ_MOVE_PENDING)) {
  1403. desc->status &= ~IRQ_MOVE_PENDING;
  1404. spin_unlock_irqrestore(&desc->lock, flags);
  1405. continue;
  1406. }
  1407. desc->chip->set_affinity(irq,
  1408. irq_desc[irq].pending_mask);
  1409. spin_unlock_irqrestore(&desc->lock, flags);
  1410. }
  1411. }
  1412. }
  1413. /*
  1414. * Migrates the IRQ destination in the process context.
  1415. */
  1416. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1417. {
  1418. if (irq_desc[irq].status & IRQ_LEVEL) {
  1419. irq_desc[irq].status |= IRQ_MOVE_PENDING;
  1420. irq_desc[irq].pending_mask = mask;
  1421. migrate_irq_remapped_level(irq);
  1422. return;
  1423. }
  1424. migrate_ioapic_irq(irq, mask);
  1425. }
  1426. #endif
  1427. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1428. {
  1429. unsigned vector, me;
  1430. ack_APIC_irq();
  1431. exit_idle();
  1432. irq_enter();
  1433. me = smp_processor_id();
  1434. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1435. unsigned int irq;
  1436. struct irq_desc *desc;
  1437. struct irq_cfg *cfg;
  1438. irq = __get_cpu_var(vector_irq)[vector];
  1439. if (irq >= NR_IRQS)
  1440. continue;
  1441. desc = irq_desc + irq;
  1442. cfg = irq_cfg + irq;
  1443. spin_lock(&desc->lock);
  1444. if (!cfg->move_cleanup_count)
  1445. goto unlock;
  1446. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1447. goto unlock;
  1448. __get_cpu_var(vector_irq)[vector] = -1;
  1449. cfg->move_cleanup_count--;
  1450. unlock:
  1451. spin_unlock(&desc->lock);
  1452. }
  1453. irq_exit();
  1454. }
  1455. static void irq_complete_move(unsigned int irq)
  1456. {
  1457. struct irq_cfg *cfg = irq_cfg + irq;
  1458. unsigned vector, me;
  1459. if (likely(!cfg->move_in_progress))
  1460. return;
  1461. vector = ~get_irq_regs()->orig_ax;
  1462. me = smp_processor_id();
  1463. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1464. cpumask_t cleanup_mask;
  1465. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1466. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1467. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1468. cfg->move_in_progress = 0;
  1469. }
  1470. }
  1471. #else
  1472. static inline void irq_complete_move(unsigned int irq) {}
  1473. #endif
  1474. #ifdef CONFIG_INTR_REMAP
  1475. static void ack_x2apic_level(unsigned int irq)
  1476. {
  1477. ack_x2APIC_irq();
  1478. }
  1479. static void ack_x2apic_edge(unsigned int irq)
  1480. {
  1481. ack_x2APIC_irq();
  1482. }
  1483. #endif
  1484. static void ack_apic_edge(unsigned int irq)
  1485. {
  1486. irq_complete_move(irq);
  1487. move_native_irq(irq);
  1488. ack_APIC_irq();
  1489. }
  1490. static void ack_apic_level(unsigned int irq)
  1491. {
  1492. int do_unmask_irq = 0;
  1493. irq_complete_move(irq);
  1494. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1495. /* If we are moving the irq we need to mask it */
  1496. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1497. do_unmask_irq = 1;
  1498. mask_IO_APIC_irq(irq);
  1499. }
  1500. #endif
  1501. /*
  1502. * We must acknowledge the irq before we move it or the acknowledge will
  1503. * not propagate properly.
  1504. */
  1505. ack_APIC_irq();
  1506. /* Now we can move and renable the irq */
  1507. if (unlikely(do_unmask_irq)) {
  1508. /* Only migrate the irq if the ack has been received.
  1509. *
  1510. * On rare occasions the broadcast level triggered ack gets
  1511. * delayed going to ioapics, and if we reprogram the
  1512. * vector while Remote IRR is still set the irq will never
  1513. * fire again.
  1514. *
  1515. * To prevent this scenario we read the Remote IRR bit
  1516. * of the ioapic. This has two effects.
  1517. * - On any sane system the read of the ioapic will
  1518. * flush writes (and acks) going to the ioapic from
  1519. * this cpu.
  1520. * - We get to see if the ACK has actually been delivered.
  1521. *
  1522. * Based on failed experiments of reprogramming the
  1523. * ioapic entry from outside of irq context starting
  1524. * with masking the ioapic entry and then polling until
  1525. * Remote IRR was clear before reprogramming the
  1526. * ioapic I don't trust the Remote IRR bit to be
  1527. * completey accurate.
  1528. *
  1529. * However there appears to be no other way to plug
  1530. * this race, so if the Remote IRR bit is not
  1531. * accurate and is causing problems then it is a hardware bug
  1532. * and you can go talk to the chipset vendor about it.
  1533. */
  1534. if (!io_apic_level_ack_pending(irq))
  1535. move_masked_irq(irq);
  1536. unmask_IO_APIC_irq(irq);
  1537. }
  1538. }
  1539. static struct irq_chip ioapic_chip __read_mostly = {
  1540. .name = "IO-APIC",
  1541. .startup = startup_ioapic_irq,
  1542. .mask = mask_IO_APIC_irq,
  1543. .unmask = unmask_IO_APIC_irq,
  1544. .ack = ack_apic_edge,
  1545. .eoi = ack_apic_level,
  1546. #ifdef CONFIG_SMP
  1547. .set_affinity = set_ioapic_affinity_irq,
  1548. #endif
  1549. .retrigger = ioapic_retrigger_irq,
  1550. };
  1551. #ifdef CONFIG_INTR_REMAP
  1552. static struct irq_chip ir_ioapic_chip __read_mostly = {
  1553. .name = "IR-IO-APIC",
  1554. .startup = startup_ioapic_irq,
  1555. .mask = mask_IO_APIC_irq,
  1556. .unmask = unmask_IO_APIC_irq,
  1557. .ack = ack_x2apic_edge,
  1558. .eoi = ack_x2apic_level,
  1559. #ifdef CONFIG_SMP
  1560. .set_affinity = set_ir_ioapic_affinity_irq,
  1561. #endif
  1562. .retrigger = ioapic_retrigger_irq,
  1563. };
  1564. #endif
  1565. static inline void init_IO_APIC_traps(void)
  1566. {
  1567. int irq;
  1568. /*
  1569. * NOTE! The local APIC isn't very good at handling
  1570. * multiple interrupts at the same interrupt level.
  1571. * As the interrupt level is determined by taking the
  1572. * vector number and shifting that right by 4, we
  1573. * want to spread these out a bit so that they don't
  1574. * all fall in the same interrupt level.
  1575. *
  1576. * Also, we've got to be careful not to trash gate
  1577. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1578. */
  1579. for (irq = 0; irq < NR_IRQS ; irq++) {
  1580. if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
  1581. /*
  1582. * Hmm.. We don't have an entry for this,
  1583. * so default to an old-fashioned 8259
  1584. * interrupt if we can..
  1585. */
  1586. if (irq < 16)
  1587. make_8259A_irq(irq);
  1588. else
  1589. /* Strange. Oh, well.. */
  1590. irq_desc[irq].chip = &no_irq_chip;
  1591. }
  1592. }
  1593. }
  1594. static void unmask_lapic_irq(unsigned int irq)
  1595. {
  1596. unsigned long v;
  1597. v = apic_read(APIC_LVT0);
  1598. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1599. }
  1600. static void mask_lapic_irq(unsigned int irq)
  1601. {
  1602. unsigned long v;
  1603. v = apic_read(APIC_LVT0);
  1604. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1605. }
  1606. static void ack_lapic_irq (unsigned int irq)
  1607. {
  1608. ack_APIC_irq();
  1609. }
  1610. static struct irq_chip lapic_chip __read_mostly = {
  1611. .name = "local-APIC",
  1612. .mask = mask_lapic_irq,
  1613. .unmask = unmask_lapic_irq,
  1614. .ack = ack_lapic_irq,
  1615. };
  1616. static void lapic_register_intr(int irq)
  1617. {
  1618. irq_desc[irq].status &= ~IRQ_LEVEL;
  1619. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1620. "edge");
  1621. }
  1622. static void __init setup_nmi(void)
  1623. {
  1624. /*
  1625. * Dirty trick to enable the NMI watchdog ...
  1626. * We put the 8259A master into AEOI mode and
  1627. * unmask on all local APICs LVT0 as NMI.
  1628. *
  1629. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1630. * is from Maciej W. Rozycki - so we do not have to EOI from
  1631. * the NMI handler or the timer interrupt.
  1632. */
  1633. printk(KERN_INFO "activating NMI Watchdog ...");
  1634. enable_NMI_through_LVT0();
  1635. printk(" done.\n");
  1636. }
  1637. /*
  1638. * This looks a bit hackish but it's about the only one way of sending
  1639. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1640. * not support the ExtINT mode, unfortunately. We need to send these
  1641. * cycles as some i82489DX-based boards have glue logic that keeps the
  1642. * 8259A interrupt line asserted until INTA. --macro
  1643. */
  1644. static inline void __init unlock_ExtINT_logic(void)
  1645. {
  1646. int apic, pin, i;
  1647. struct IO_APIC_route_entry entry0, entry1;
  1648. unsigned char save_control, save_freq_select;
  1649. pin = find_isa_irq_pin(8, mp_INT);
  1650. apic = find_isa_irq_apic(8, mp_INT);
  1651. if (pin == -1)
  1652. return;
  1653. entry0 = ioapic_read_entry(apic, pin);
  1654. clear_IO_APIC_pin(apic, pin);
  1655. memset(&entry1, 0, sizeof(entry1));
  1656. entry1.dest_mode = 0; /* physical delivery */
  1657. entry1.mask = 0; /* unmask IRQ now */
  1658. entry1.dest = hard_smp_processor_id();
  1659. entry1.delivery_mode = dest_ExtINT;
  1660. entry1.polarity = entry0.polarity;
  1661. entry1.trigger = 0;
  1662. entry1.vector = 0;
  1663. ioapic_write_entry(apic, pin, entry1);
  1664. save_control = CMOS_READ(RTC_CONTROL);
  1665. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1666. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1667. RTC_FREQ_SELECT);
  1668. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1669. i = 100;
  1670. while (i-- > 0) {
  1671. mdelay(10);
  1672. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1673. i -= 10;
  1674. }
  1675. CMOS_WRITE(save_control, RTC_CONTROL);
  1676. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1677. clear_IO_APIC_pin(apic, pin);
  1678. ioapic_write_entry(apic, pin, entry0);
  1679. }
  1680. /*
  1681. * This code may look a bit paranoid, but it's supposed to cooperate with
  1682. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1683. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1684. * fanatically on his truly buggy board.
  1685. *
  1686. * FIXME: really need to revamp this for modern platforms only.
  1687. */
  1688. static inline void __init check_timer(void)
  1689. {
  1690. struct irq_cfg *cfg = irq_cfg + 0;
  1691. int apic1, pin1, apic2, pin2;
  1692. unsigned long flags;
  1693. int no_pin1 = 0;
  1694. local_irq_save(flags);
  1695. /*
  1696. * get/set the timer IRQ vector:
  1697. */
  1698. disable_8259A_irq(0);
  1699. assign_irq_vector(0, TARGET_CPUS);
  1700. /*
  1701. * As IRQ0 is to be enabled in the 8259A, the virtual
  1702. * wire has to be disabled in the local APIC.
  1703. */
  1704. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1705. init_8259A(1);
  1706. pin1 = find_isa_irq_pin(0, mp_INT);
  1707. apic1 = find_isa_irq_apic(0, mp_INT);
  1708. pin2 = ioapic_i8259.pin;
  1709. apic2 = ioapic_i8259.apic;
  1710. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1711. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1712. cfg->vector, apic1, pin1, apic2, pin2);
  1713. /*
  1714. * Some BIOS writers are clueless and report the ExtINTA
  1715. * I/O APIC input from the cascaded 8259A as the timer
  1716. * interrupt input. So just in case, if only one pin
  1717. * was found above, try it both directly and through the
  1718. * 8259A.
  1719. */
  1720. if (pin1 == -1) {
  1721. if (intr_remapping_enabled)
  1722. panic("BIOS bug: timer not connected to IO-APIC");
  1723. pin1 = pin2;
  1724. apic1 = apic2;
  1725. no_pin1 = 1;
  1726. } else if (pin2 == -1) {
  1727. pin2 = pin1;
  1728. apic2 = apic1;
  1729. }
  1730. if (pin1 != -1) {
  1731. /*
  1732. * Ok, does IRQ0 through the IOAPIC work?
  1733. */
  1734. if (no_pin1) {
  1735. add_pin_to_irq(0, apic1, pin1);
  1736. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  1737. }
  1738. unmask_IO_APIC_irq(0);
  1739. if (!no_timer_check && timer_irq_works()) {
  1740. if (nmi_watchdog == NMI_IO_APIC) {
  1741. setup_nmi();
  1742. enable_8259A_irq(0);
  1743. }
  1744. if (disable_timer_pin_1 > 0)
  1745. clear_IO_APIC_pin(0, pin1);
  1746. goto out;
  1747. }
  1748. if (intr_remapping_enabled)
  1749. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  1750. clear_IO_APIC_pin(apic1, pin1);
  1751. if (!no_pin1)
  1752. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1753. "8254 timer not connected to IO-APIC\n");
  1754. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1755. "(IRQ0) through the 8259A ...\n");
  1756. apic_printk(APIC_QUIET, KERN_INFO
  1757. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1758. /*
  1759. * legacy devices should be connected to IO APIC #0
  1760. */
  1761. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1762. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  1763. unmask_IO_APIC_irq(0);
  1764. enable_8259A_irq(0);
  1765. if (timer_irq_works()) {
  1766. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1767. timer_through_8259 = 1;
  1768. if (nmi_watchdog == NMI_IO_APIC) {
  1769. disable_8259A_irq(0);
  1770. setup_nmi();
  1771. enable_8259A_irq(0);
  1772. }
  1773. goto out;
  1774. }
  1775. /*
  1776. * Cleanup, just in case ...
  1777. */
  1778. disable_8259A_irq(0);
  1779. clear_IO_APIC_pin(apic2, pin2);
  1780. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1781. }
  1782. if (nmi_watchdog == NMI_IO_APIC) {
  1783. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  1784. "through the IO-APIC - disabling NMI Watchdog!\n");
  1785. nmi_watchdog = NMI_NONE;
  1786. }
  1787. apic_printk(APIC_QUIET, KERN_INFO
  1788. "...trying to set up timer as Virtual Wire IRQ...\n");
  1789. lapic_register_intr(0);
  1790. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1791. enable_8259A_irq(0);
  1792. if (timer_irq_works()) {
  1793. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1794. goto out;
  1795. }
  1796. disable_8259A_irq(0);
  1797. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1798. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1799. apic_printk(APIC_QUIET, KERN_INFO
  1800. "...trying to set up timer as ExtINT IRQ...\n");
  1801. init_8259A(0);
  1802. make_8259A_irq(0);
  1803. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1804. unlock_ExtINT_logic();
  1805. if (timer_irq_works()) {
  1806. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1807. goto out;
  1808. }
  1809. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1810. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1811. "report. Then try booting with the 'noapic' option.\n");
  1812. out:
  1813. local_irq_restore(flags);
  1814. }
  1815. static int __init notimercheck(char *s)
  1816. {
  1817. no_timer_check = 1;
  1818. return 1;
  1819. }
  1820. __setup("no_timer_check", notimercheck);
  1821. /*
  1822. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1823. * to devices. However there may be an I/O APIC pin available for
  1824. * this interrupt regardless. The pin may be left unconnected, but
  1825. * typically it will be reused as an ExtINT cascade interrupt for
  1826. * the master 8259A. In the MPS case such a pin will normally be
  1827. * reported as an ExtINT interrupt in the MP table. With ACPI
  1828. * there is no provision for ExtINT interrupts, and in the absence
  1829. * of an override it would be treated as an ordinary ISA I/O APIC
  1830. * interrupt, that is edge-triggered and unmasked by default. We
  1831. * used to do this, but it caused problems on some systems because
  1832. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1833. * the same ExtINT cascade interrupt to drive the local APIC of the
  1834. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1835. * the I/O APIC in all cases now. No actual device should request
  1836. * it anyway. --macro
  1837. */
  1838. #define PIC_IRQS (1<<2)
  1839. void __init setup_IO_APIC(void)
  1840. {
  1841. /*
  1842. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  1843. */
  1844. io_apic_irqs = ~PIC_IRQS;
  1845. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1846. sync_Arb_IDs();
  1847. setup_IO_APIC_irqs();
  1848. init_IO_APIC_traps();
  1849. check_timer();
  1850. if (!acpi_ioapic)
  1851. print_IO_APIC();
  1852. }
  1853. struct sysfs_ioapic_data {
  1854. struct sys_device dev;
  1855. struct IO_APIC_route_entry entry[0];
  1856. };
  1857. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1858. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1859. {
  1860. struct IO_APIC_route_entry *entry;
  1861. struct sysfs_ioapic_data *data;
  1862. int i;
  1863. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1864. entry = data->entry;
  1865. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1866. *entry = ioapic_read_entry(dev->id, i);
  1867. return 0;
  1868. }
  1869. static int ioapic_resume(struct sys_device *dev)
  1870. {
  1871. struct IO_APIC_route_entry *entry;
  1872. struct sysfs_ioapic_data *data;
  1873. unsigned long flags;
  1874. union IO_APIC_reg_00 reg_00;
  1875. int i;
  1876. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1877. entry = data->entry;
  1878. spin_lock_irqsave(&ioapic_lock, flags);
  1879. reg_00.raw = io_apic_read(dev->id, 0);
  1880. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  1881. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  1882. io_apic_write(dev->id, 0, reg_00.raw);
  1883. }
  1884. spin_unlock_irqrestore(&ioapic_lock, flags);
  1885. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1886. ioapic_write_entry(dev->id, i, entry[i]);
  1887. return 0;
  1888. }
  1889. static struct sysdev_class ioapic_sysdev_class = {
  1890. .name = "ioapic",
  1891. .suspend = ioapic_suspend,
  1892. .resume = ioapic_resume,
  1893. };
  1894. static int __init ioapic_init_sysfs(void)
  1895. {
  1896. struct sys_device * dev;
  1897. int i, size, error;
  1898. error = sysdev_class_register(&ioapic_sysdev_class);
  1899. if (error)
  1900. return error;
  1901. for (i = 0; i < nr_ioapics; i++ ) {
  1902. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1903. * sizeof(struct IO_APIC_route_entry);
  1904. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  1905. if (!mp_ioapic_data[i]) {
  1906. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1907. continue;
  1908. }
  1909. dev = &mp_ioapic_data[i]->dev;
  1910. dev->id = i;
  1911. dev->cls = &ioapic_sysdev_class;
  1912. error = sysdev_register(dev);
  1913. if (error) {
  1914. kfree(mp_ioapic_data[i]);
  1915. mp_ioapic_data[i] = NULL;
  1916. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1917. continue;
  1918. }
  1919. }
  1920. return 0;
  1921. }
  1922. device_initcall(ioapic_init_sysfs);
  1923. /*
  1924. * Dynamic irq allocate and deallocation
  1925. */
  1926. int create_irq(void)
  1927. {
  1928. /* Allocate an unused irq */
  1929. int irq;
  1930. int new;
  1931. unsigned long flags;
  1932. irq = -ENOSPC;
  1933. spin_lock_irqsave(&vector_lock, flags);
  1934. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1935. if (platform_legacy_irq(new))
  1936. continue;
  1937. if (irq_cfg[new].vector != 0)
  1938. continue;
  1939. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  1940. irq = new;
  1941. break;
  1942. }
  1943. spin_unlock_irqrestore(&vector_lock, flags);
  1944. if (irq >= 0) {
  1945. dynamic_irq_init(irq);
  1946. }
  1947. return irq;
  1948. }
  1949. void destroy_irq(unsigned int irq)
  1950. {
  1951. unsigned long flags;
  1952. dynamic_irq_cleanup(irq);
  1953. #ifdef CONFIG_INTR_REMAP
  1954. free_irte(irq);
  1955. #endif
  1956. spin_lock_irqsave(&vector_lock, flags);
  1957. __clear_irq_vector(irq);
  1958. spin_unlock_irqrestore(&vector_lock, flags);
  1959. }
  1960. /*
  1961. * MSI message composition
  1962. */
  1963. #ifdef CONFIG_PCI_MSI
  1964. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1965. {
  1966. struct irq_cfg *cfg = irq_cfg + irq;
  1967. int err;
  1968. unsigned dest;
  1969. cpumask_t tmp;
  1970. tmp = TARGET_CPUS;
  1971. err = assign_irq_vector(irq, tmp);
  1972. if (err)
  1973. return err;
  1974. cpus_and(tmp, cfg->domain, tmp);
  1975. dest = cpu_mask_to_apicid(tmp);
  1976. #ifdef CONFIG_INTR_REMAP
  1977. if (irq_remapped(irq)) {
  1978. struct irte irte;
  1979. int ir_index;
  1980. u16 sub_handle;
  1981. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  1982. BUG_ON(ir_index == -1);
  1983. memset (&irte, 0, sizeof(irte));
  1984. irte.present = 1;
  1985. irte.dst_mode = INT_DEST_MODE;
  1986. irte.trigger_mode = 0; /* edge */
  1987. irte.dlvry_mode = INT_DELIVERY_MODE;
  1988. irte.vector = cfg->vector;
  1989. irte.dest_id = IRTE_DEST(dest);
  1990. modify_irte(irq, &irte);
  1991. msg->address_hi = MSI_ADDR_BASE_HI;
  1992. msg->data = sub_handle;
  1993. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  1994. MSI_ADDR_IR_SHV |
  1995. MSI_ADDR_IR_INDEX1(ir_index) |
  1996. MSI_ADDR_IR_INDEX2(ir_index);
  1997. } else
  1998. #endif
  1999. {
  2000. msg->address_hi = MSI_ADDR_BASE_HI;
  2001. msg->address_lo =
  2002. MSI_ADDR_BASE_LO |
  2003. ((INT_DEST_MODE == 0) ?
  2004. MSI_ADDR_DEST_MODE_PHYSICAL:
  2005. MSI_ADDR_DEST_MODE_LOGICAL) |
  2006. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2007. MSI_ADDR_REDIRECTION_CPU:
  2008. MSI_ADDR_REDIRECTION_LOWPRI) |
  2009. MSI_ADDR_DEST_ID(dest);
  2010. msg->data =
  2011. MSI_DATA_TRIGGER_EDGE |
  2012. MSI_DATA_LEVEL_ASSERT |
  2013. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2014. MSI_DATA_DELIVERY_FIXED:
  2015. MSI_DATA_DELIVERY_LOWPRI) |
  2016. MSI_DATA_VECTOR(cfg->vector);
  2017. }
  2018. return err;
  2019. }
  2020. #ifdef CONFIG_SMP
  2021. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2022. {
  2023. struct irq_cfg *cfg = irq_cfg + irq;
  2024. struct msi_msg msg;
  2025. unsigned int dest;
  2026. cpumask_t tmp;
  2027. cpus_and(tmp, mask, cpu_online_map);
  2028. if (cpus_empty(tmp))
  2029. return;
  2030. if (assign_irq_vector(irq, mask))
  2031. return;
  2032. cpus_and(tmp, cfg->domain, mask);
  2033. dest = cpu_mask_to_apicid(tmp);
  2034. read_msi_msg(irq, &msg);
  2035. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2036. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2037. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2038. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2039. write_msi_msg(irq, &msg);
  2040. irq_desc[irq].affinity = mask;
  2041. }
  2042. #ifdef CONFIG_INTR_REMAP
  2043. /*
  2044. * Migrate the MSI irq to another cpumask. This migration is
  2045. * done in the process context using interrupt-remapping hardware.
  2046. */
  2047. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2048. {
  2049. struct irq_cfg *cfg = irq_cfg + irq;
  2050. unsigned int dest;
  2051. cpumask_t tmp, cleanup_mask;
  2052. struct irte irte;
  2053. cpus_and(tmp, mask, cpu_online_map);
  2054. if (cpus_empty(tmp))
  2055. return;
  2056. if (get_irte(irq, &irte))
  2057. return;
  2058. if (assign_irq_vector(irq, mask))
  2059. return;
  2060. cpus_and(tmp, cfg->domain, mask);
  2061. dest = cpu_mask_to_apicid(tmp);
  2062. irte.vector = cfg->vector;
  2063. irte.dest_id = IRTE_DEST(dest);
  2064. /*
  2065. * atomically update the IRTE with the new destination and vector.
  2066. */
  2067. modify_irte(irq, &irte);
  2068. /*
  2069. * After this point, all the interrupts will start arriving
  2070. * at the new destination. So, time to cleanup the previous
  2071. * vector allocation.
  2072. */
  2073. if (cfg->move_in_progress) {
  2074. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2075. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2076. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2077. cfg->move_in_progress = 0;
  2078. }
  2079. irq_desc[irq].affinity = mask;
  2080. }
  2081. #endif
  2082. #endif /* CONFIG_SMP */
  2083. /*
  2084. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2085. * which implement the MSI or MSI-X Capability Structure.
  2086. */
  2087. static struct irq_chip msi_chip = {
  2088. .name = "PCI-MSI",
  2089. .unmask = unmask_msi_irq,
  2090. .mask = mask_msi_irq,
  2091. .ack = ack_apic_edge,
  2092. #ifdef CONFIG_SMP
  2093. .set_affinity = set_msi_irq_affinity,
  2094. #endif
  2095. .retrigger = ioapic_retrigger_irq,
  2096. };
  2097. #ifdef CONFIG_INTR_REMAP
  2098. static struct irq_chip msi_ir_chip = {
  2099. .name = "IR-PCI-MSI",
  2100. .unmask = unmask_msi_irq,
  2101. .mask = mask_msi_irq,
  2102. .ack = ack_x2apic_edge,
  2103. #ifdef CONFIG_SMP
  2104. .set_affinity = ir_set_msi_irq_affinity,
  2105. #endif
  2106. .retrigger = ioapic_retrigger_irq,
  2107. };
  2108. /*
  2109. * Map the PCI dev to the corresponding remapping hardware unit
  2110. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2111. * in it.
  2112. */
  2113. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2114. {
  2115. struct intel_iommu *iommu;
  2116. int index;
  2117. iommu = map_dev_to_ir(dev);
  2118. if (!iommu) {
  2119. printk(KERN_ERR
  2120. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2121. return -ENOENT;
  2122. }
  2123. index = alloc_irte(iommu, irq, nvec);
  2124. if (index < 0) {
  2125. printk(KERN_ERR
  2126. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2127. pci_name(dev));
  2128. return -ENOSPC;
  2129. }
  2130. return index;
  2131. }
  2132. #endif
  2133. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
  2134. {
  2135. int ret;
  2136. struct msi_msg msg;
  2137. ret = msi_compose_msg(dev, irq, &msg);
  2138. if (ret < 0)
  2139. return ret;
  2140. set_irq_msi(irq, desc);
  2141. write_msi_msg(irq, &msg);
  2142. #ifdef CONFIG_INTR_REMAP
  2143. if (irq_remapped(irq)) {
  2144. struct irq_desc *desc = irq_desc + irq;
  2145. /*
  2146. * irq migration in process context
  2147. */
  2148. desc->status |= IRQ_MOVE_PCNTXT;
  2149. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2150. } else
  2151. #endif
  2152. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2153. return 0;
  2154. }
  2155. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2156. {
  2157. int irq, ret;
  2158. irq = create_irq();
  2159. if (irq < 0)
  2160. return irq;
  2161. #ifdef CONFIG_INTR_REMAP
  2162. if (!intr_remapping_enabled)
  2163. goto no_ir;
  2164. ret = msi_alloc_irte(dev, irq, 1);
  2165. if (ret < 0)
  2166. goto error;
  2167. no_ir:
  2168. #endif
  2169. ret = setup_msi_irq(dev, desc, irq);
  2170. if (ret < 0) {
  2171. destroy_irq(irq);
  2172. return ret;
  2173. }
  2174. return 0;
  2175. #ifdef CONFIG_INTR_REMAP
  2176. error:
  2177. destroy_irq(irq);
  2178. return ret;
  2179. #endif
  2180. }
  2181. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2182. {
  2183. int irq, ret, sub_handle;
  2184. struct msi_desc *desc;
  2185. #ifdef CONFIG_INTR_REMAP
  2186. struct intel_iommu *iommu = 0;
  2187. int index = 0;
  2188. #endif
  2189. sub_handle = 0;
  2190. list_for_each_entry(desc, &dev->msi_list, list) {
  2191. irq = create_irq();
  2192. if (irq < 0)
  2193. return irq;
  2194. #ifdef CONFIG_INTR_REMAP
  2195. if (!intr_remapping_enabled)
  2196. goto no_ir;
  2197. if (!sub_handle) {
  2198. /*
  2199. * allocate the consecutive block of IRTE's
  2200. * for 'nvec'
  2201. */
  2202. index = msi_alloc_irte(dev, irq, nvec);
  2203. if (index < 0) {
  2204. ret = index;
  2205. goto error;
  2206. }
  2207. } else {
  2208. iommu = map_dev_to_ir(dev);
  2209. if (!iommu) {
  2210. ret = -ENOENT;
  2211. goto error;
  2212. }
  2213. /*
  2214. * setup the mapping between the irq and the IRTE
  2215. * base index, the sub_handle pointing to the
  2216. * appropriate interrupt remap table entry.
  2217. */
  2218. set_irte_irq(irq, iommu, index, sub_handle);
  2219. }
  2220. no_ir:
  2221. #endif
  2222. ret = setup_msi_irq(dev, desc, irq);
  2223. if (ret < 0)
  2224. goto error;
  2225. sub_handle++;
  2226. }
  2227. return 0;
  2228. error:
  2229. destroy_irq(irq);
  2230. return ret;
  2231. }
  2232. void arch_teardown_msi_irq(unsigned int irq)
  2233. {
  2234. destroy_irq(irq);
  2235. }
  2236. #ifdef CONFIG_DMAR
  2237. #ifdef CONFIG_SMP
  2238. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2239. {
  2240. struct irq_cfg *cfg = irq_cfg + irq;
  2241. struct msi_msg msg;
  2242. unsigned int dest;
  2243. cpumask_t tmp;
  2244. cpus_and(tmp, mask, cpu_online_map);
  2245. if (cpus_empty(tmp))
  2246. return;
  2247. if (assign_irq_vector(irq, mask))
  2248. return;
  2249. cpus_and(tmp, cfg->domain, mask);
  2250. dest = cpu_mask_to_apicid(tmp);
  2251. dmar_msi_read(irq, &msg);
  2252. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2253. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2254. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2255. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2256. dmar_msi_write(irq, &msg);
  2257. irq_desc[irq].affinity = mask;
  2258. }
  2259. #endif /* CONFIG_SMP */
  2260. struct irq_chip dmar_msi_type = {
  2261. .name = "DMAR_MSI",
  2262. .unmask = dmar_msi_unmask,
  2263. .mask = dmar_msi_mask,
  2264. .ack = ack_apic_edge,
  2265. #ifdef CONFIG_SMP
  2266. .set_affinity = dmar_msi_set_affinity,
  2267. #endif
  2268. .retrigger = ioapic_retrigger_irq,
  2269. };
  2270. int arch_setup_dmar_msi(unsigned int irq)
  2271. {
  2272. int ret;
  2273. struct msi_msg msg;
  2274. ret = msi_compose_msg(NULL, irq, &msg);
  2275. if (ret < 0)
  2276. return ret;
  2277. dmar_msi_write(irq, &msg);
  2278. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2279. "edge");
  2280. return 0;
  2281. }
  2282. #endif
  2283. #endif /* CONFIG_PCI_MSI */
  2284. /*
  2285. * Hypertransport interrupt support
  2286. */
  2287. #ifdef CONFIG_HT_IRQ
  2288. #ifdef CONFIG_SMP
  2289. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  2290. {
  2291. struct ht_irq_msg msg;
  2292. fetch_ht_irq_msg(irq, &msg);
  2293. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  2294. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2295. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  2296. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2297. write_ht_irq_msg(irq, &msg);
  2298. }
  2299. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2300. {
  2301. struct irq_cfg *cfg = irq_cfg + irq;
  2302. unsigned int dest;
  2303. cpumask_t tmp;
  2304. cpus_and(tmp, mask, cpu_online_map);
  2305. if (cpus_empty(tmp))
  2306. return;
  2307. if (assign_irq_vector(irq, mask))
  2308. return;
  2309. cpus_and(tmp, cfg->domain, mask);
  2310. dest = cpu_mask_to_apicid(tmp);
  2311. target_ht_irq(irq, dest, cfg->vector);
  2312. irq_desc[irq].affinity = mask;
  2313. }
  2314. #endif
  2315. static struct irq_chip ht_irq_chip = {
  2316. .name = "PCI-HT",
  2317. .mask = mask_ht_irq,
  2318. .unmask = unmask_ht_irq,
  2319. .ack = ack_apic_edge,
  2320. #ifdef CONFIG_SMP
  2321. .set_affinity = set_ht_irq_affinity,
  2322. #endif
  2323. .retrigger = ioapic_retrigger_irq,
  2324. };
  2325. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2326. {
  2327. struct irq_cfg *cfg = irq_cfg + irq;
  2328. int err;
  2329. cpumask_t tmp;
  2330. tmp = TARGET_CPUS;
  2331. err = assign_irq_vector(irq, tmp);
  2332. if (!err) {
  2333. struct ht_irq_msg msg;
  2334. unsigned dest;
  2335. cpus_and(tmp, cfg->domain, tmp);
  2336. dest = cpu_mask_to_apicid(tmp);
  2337. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2338. msg.address_lo =
  2339. HT_IRQ_LOW_BASE |
  2340. HT_IRQ_LOW_DEST_ID(dest) |
  2341. HT_IRQ_LOW_VECTOR(cfg->vector) |
  2342. ((INT_DEST_MODE == 0) ?
  2343. HT_IRQ_LOW_DM_PHYSICAL :
  2344. HT_IRQ_LOW_DM_LOGICAL) |
  2345. HT_IRQ_LOW_RQEOI_EDGE |
  2346. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2347. HT_IRQ_LOW_MT_FIXED :
  2348. HT_IRQ_LOW_MT_ARBITRATED) |
  2349. HT_IRQ_LOW_IRQ_MASKED;
  2350. write_ht_irq_msg(irq, &msg);
  2351. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2352. handle_edge_irq, "edge");
  2353. }
  2354. return err;
  2355. }
  2356. #endif /* CONFIG_HT_IRQ */
  2357. /* --------------------------------------------------------------------------
  2358. ACPI-based IOAPIC Configuration
  2359. -------------------------------------------------------------------------- */
  2360. #ifdef CONFIG_ACPI
  2361. #define IO_APIC_MAX_ID 0xFE
  2362. int __init io_apic_get_redir_entries (int ioapic)
  2363. {
  2364. union IO_APIC_reg_01 reg_01;
  2365. unsigned long flags;
  2366. spin_lock_irqsave(&ioapic_lock, flags);
  2367. reg_01.raw = io_apic_read(ioapic, 1);
  2368. spin_unlock_irqrestore(&ioapic_lock, flags);
  2369. return reg_01.bits.entries;
  2370. }
  2371. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  2372. {
  2373. if (!IO_APIC_IRQ(irq)) {
  2374. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2375. ioapic);
  2376. return -EINVAL;
  2377. }
  2378. /*
  2379. * IRQs < 16 are already in the irq_2_pin[] map
  2380. */
  2381. if (irq >= 16)
  2382. add_pin_to_irq(irq, ioapic, pin);
  2383. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  2384. return 0;
  2385. }
  2386. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  2387. {
  2388. int i;
  2389. if (skip_ioapic_setup)
  2390. return -1;
  2391. for (i = 0; i < mp_irq_entries; i++)
  2392. if (mp_irqs[i].mp_irqtype == mp_INT &&
  2393. mp_irqs[i].mp_srcbusirq == bus_irq)
  2394. break;
  2395. if (i >= mp_irq_entries)
  2396. return -1;
  2397. *trigger = irq_trigger(i);
  2398. *polarity = irq_polarity(i);
  2399. return 0;
  2400. }
  2401. #endif /* CONFIG_ACPI */
  2402. /*
  2403. * This function currently is only a helper for the i386 smp boot process where
  2404. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  2405. * so mask in all cases should simply be TARGET_CPUS
  2406. */
  2407. #ifdef CONFIG_SMP
  2408. void __init setup_ioapic_dest(void)
  2409. {
  2410. int pin, ioapic, irq, irq_entry;
  2411. if (skip_ioapic_setup == 1)
  2412. return;
  2413. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  2414. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  2415. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  2416. if (irq_entry == -1)
  2417. continue;
  2418. irq = pin_2_irq(irq_entry, ioapic, pin);
  2419. /* setup_IO_APIC_irqs could fail to get vector for some device
  2420. * when you have too many devices, because at that time only boot
  2421. * cpu is online.
  2422. */
  2423. if (!irq_cfg[irq].vector)
  2424. setup_IO_APIC_irq(ioapic, pin, irq,
  2425. irq_trigger(irq_entry),
  2426. irq_polarity(irq_entry));
  2427. #ifdef CONFIG_INTR_REMAP
  2428. else if (intr_remapping_enabled)
  2429. set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
  2430. #endif
  2431. else
  2432. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  2433. }
  2434. }
  2435. }
  2436. #endif
  2437. #define IOAPIC_RESOURCE_NAME_SIZE 11
  2438. static struct resource *ioapic_resources;
  2439. static struct resource * __init ioapic_setup_resources(void)
  2440. {
  2441. unsigned long n;
  2442. struct resource *res;
  2443. char *mem;
  2444. int i;
  2445. if (nr_ioapics <= 0)
  2446. return NULL;
  2447. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  2448. n *= nr_ioapics;
  2449. mem = alloc_bootmem(n);
  2450. res = (void *)mem;
  2451. if (mem != NULL) {
  2452. mem += sizeof(struct resource) * nr_ioapics;
  2453. for (i = 0; i < nr_ioapics; i++) {
  2454. res[i].name = mem;
  2455. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  2456. sprintf(mem, "IOAPIC %u", i);
  2457. mem += IOAPIC_RESOURCE_NAME_SIZE;
  2458. }
  2459. }
  2460. ioapic_resources = res;
  2461. return res;
  2462. }
  2463. void __init ioapic_init_mappings(void)
  2464. {
  2465. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2466. struct resource *ioapic_res;
  2467. int i;
  2468. ioapic_res = ioapic_setup_resources();
  2469. for (i = 0; i < nr_ioapics; i++) {
  2470. if (smp_found_config) {
  2471. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  2472. } else {
  2473. ioapic_phys = (unsigned long)
  2474. alloc_bootmem_pages(PAGE_SIZE);
  2475. ioapic_phys = __pa(ioapic_phys);
  2476. }
  2477. set_fixmap_nocache(idx, ioapic_phys);
  2478. apic_printk(APIC_VERBOSE,
  2479. "mapped IOAPIC to %016lx (%016lx)\n",
  2480. __fix_to_virt(idx), ioapic_phys);
  2481. idx++;
  2482. if (ioapic_res != NULL) {
  2483. ioapic_res->start = ioapic_phys;
  2484. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  2485. ioapic_res++;
  2486. }
  2487. }
  2488. }
  2489. static int __init ioapic_insert_resources(void)
  2490. {
  2491. int i;
  2492. struct resource *r = ioapic_resources;
  2493. if (!r) {
  2494. printk(KERN_ERR
  2495. "IO APIC resources could be not be allocated.\n");
  2496. return -1;
  2497. }
  2498. for (i = 0; i < nr_ioapics; i++) {
  2499. insert_resource(&iomem_resource, r);
  2500. r++;
  2501. }
  2502. return 0;
  2503. }
  2504. /* Insert the IO APIC resources after PCI initialization has occured to handle
  2505. * IO APICS that are mapped in on a BAR in PCI space. */
  2506. late_initcall(ioapic_insert_resources);