io_apic.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987
  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/compiler.h>
  30. #include <linux/acpi.h>
  31. #include <linux/module.h>
  32. #include <linux/sysdev.h>
  33. #include <linux/msi.h>
  34. #include <linux/htirq.h>
  35. #include <linux/freezer.h>
  36. #include <linux/kthread.h>
  37. #include <linux/jiffies.h> /* time_after() */
  38. #ifdef CONFIG_ACPI
  39. #include <acpi/acpi_bus.h>
  40. #endif
  41. #include <linux/bootmem.h>
  42. #include <linux/dmar.h>
  43. #include <linux/hpet.h>
  44. #include <asm/idle.h>
  45. #include <asm/io.h>
  46. #include <asm/smp.h>
  47. #include <asm/desc.h>
  48. #include <asm/proto.h>
  49. #include <asm/acpi.h>
  50. #include <asm/dma.h>
  51. #include <asm/timer.h>
  52. #include <asm/i8259.h>
  53. #include <asm/nmi.h>
  54. #include <asm/msidef.h>
  55. #include <asm/hypertransport.h>
  56. #include <asm/setup.h>
  57. #include <asm/irq_remapping.h>
  58. #include <asm/hpet.h>
  59. #include <asm/uv/uv_hub.h>
  60. #include <asm/uv/uv_irq.h>
  61. #include <mach_ipi.h>
  62. #include <mach_apic.h>
  63. #include <mach_apicdef.h>
  64. #define __apicdebuginit(type) static type __init
  65. /*
  66. * Is the SiS APIC rmw bug present ?
  67. * -1 = don't know, 0 = no, 1 = yes
  68. */
  69. int sis_apic_bug = -1;
  70. static DEFINE_SPINLOCK(ioapic_lock);
  71. static DEFINE_SPINLOCK(vector_lock);
  72. /*
  73. * # of IRQ routing registers
  74. */
  75. int nr_ioapic_registers[MAX_IO_APICS];
  76. /* I/O APIC entries */
  77. struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
  78. int nr_ioapics;
  79. /* MP IRQ source entries */
  80. struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  81. /* # of MP IRQ source entries */
  82. int mp_irq_entries;
  83. #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
  84. int mp_bus_id_to_type[MAX_MP_BUSSES];
  85. #endif
  86. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  87. int skip_ioapic_setup;
  88. static int __init parse_noapic(char *str)
  89. {
  90. /* disable IO-APIC */
  91. disable_ioapic_setup();
  92. return 0;
  93. }
  94. early_param("noapic", parse_noapic);
  95. struct irq_pin_list;
  96. /*
  97. * This is performance-critical, we want to do it O(1)
  98. *
  99. * the indexing order of this array favors 1:1 mappings
  100. * between pins and IRQs.
  101. */
  102. struct irq_pin_list {
  103. int apic, pin;
  104. struct irq_pin_list *next;
  105. };
  106. static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
  107. {
  108. struct irq_pin_list *pin;
  109. int node;
  110. node = cpu_to_node(cpu);
  111. pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
  112. printk(KERN_DEBUG " alloc irq_2_pin on cpu %d node %d\n", cpu, node);
  113. return pin;
  114. }
  115. struct irq_cfg {
  116. struct irq_pin_list *irq_2_pin;
  117. cpumask_t domain;
  118. cpumask_t old_domain;
  119. unsigned move_cleanup_count;
  120. u8 vector;
  121. u8 move_in_progress : 1;
  122. };
  123. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  124. #ifdef CONFIG_SPARSE_IRQ
  125. static struct irq_cfg irq_cfgx[] = {
  126. #else
  127. static struct irq_cfg irq_cfgx[NR_IRQS] = {
  128. #endif
  129. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  130. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  131. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  132. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  133. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  134. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  135. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  136. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  137. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  138. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  139. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  140. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  141. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  142. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  143. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  144. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  145. };
  146. void __init arch_early_irq_init(void)
  147. {
  148. struct irq_cfg *cfg;
  149. struct irq_desc *desc;
  150. int count;
  151. int i;
  152. cfg = irq_cfgx;
  153. count = ARRAY_SIZE(irq_cfgx);
  154. for (i = 0; i < count; i++) {
  155. desc = irq_to_desc(i);
  156. desc->chip_data = &cfg[i];
  157. }
  158. }
  159. #ifdef CONFIG_SPARSE_IRQ
  160. static struct irq_cfg *irq_cfg(unsigned int irq)
  161. {
  162. struct irq_cfg *cfg = NULL;
  163. struct irq_desc *desc;
  164. desc = irq_to_desc(irq);
  165. if (desc)
  166. cfg = desc->chip_data;
  167. return cfg;
  168. }
  169. static struct irq_cfg *get_one_free_irq_cfg(int cpu)
  170. {
  171. struct irq_cfg *cfg;
  172. int node;
  173. node = cpu_to_node(cpu);
  174. cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
  175. printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
  176. return cfg;
  177. }
  178. void arch_init_chip_data(struct irq_desc *desc, int cpu)
  179. {
  180. struct irq_cfg *cfg;
  181. cfg = desc->chip_data;
  182. if (!cfg) {
  183. desc->chip_data = get_one_free_irq_cfg(cpu);
  184. if (!desc->chip_data) {
  185. printk(KERN_ERR "can not alloc irq_cfg\n");
  186. BUG_ON(1);
  187. }
  188. }
  189. }
  190. #else
  191. static struct irq_cfg *irq_cfg(unsigned int irq)
  192. {
  193. return irq < nr_irqs ? irq_cfgx + irq : NULL;
  194. }
  195. #endif
  196. struct io_apic {
  197. unsigned int index;
  198. unsigned int unused[3];
  199. unsigned int data;
  200. };
  201. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  202. {
  203. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  204. + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
  205. }
  206. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  207. {
  208. struct io_apic __iomem *io_apic = io_apic_base(apic);
  209. writel(reg, &io_apic->index);
  210. return readl(&io_apic->data);
  211. }
  212. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  213. {
  214. struct io_apic __iomem *io_apic = io_apic_base(apic);
  215. writel(reg, &io_apic->index);
  216. writel(value, &io_apic->data);
  217. }
  218. /*
  219. * Re-write a value: to be used for read-modify-write
  220. * cycles where the read already set up the index register.
  221. *
  222. * Older SiS APIC requires we rewrite the index register
  223. */
  224. static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  225. {
  226. struct io_apic __iomem *io_apic = io_apic_base(apic);
  227. if (sis_apic_bug)
  228. writel(reg, &io_apic->index);
  229. writel(value, &io_apic->data);
  230. }
  231. static bool io_apic_level_ack_pending(unsigned int irq)
  232. {
  233. struct irq_pin_list *entry;
  234. unsigned long flags;
  235. struct irq_cfg *cfg = irq_cfg(irq);
  236. spin_lock_irqsave(&ioapic_lock, flags);
  237. entry = cfg->irq_2_pin;
  238. for (;;) {
  239. unsigned int reg;
  240. int pin;
  241. if (!entry)
  242. break;
  243. pin = entry->pin;
  244. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  245. /* Is the remote IRR bit set? */
  246. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  247. spin_unlock_irqrestore(&ioapic_lock, flags);
  248. return true;
  249. }
  250. if (!entry->next)
  251. break;
  252. entry = entry->next;
  253. }
  254. spin_unlock_irqrestore(&ioapic_lock, flags);
  255. return false;
  256. }
  257. union entry_union {
  258. struct { u32 w1, w2; };
  259. struct IO_APIC_route_entry entry;
  260. };
  261. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  262. {
  263. union entry_union eu;
  264. unsigned long flags;
  265. spin_lock_irqsave(&ioapic_lock, flags);
  266. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  267. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  268. spin_unlock_irqrestore(&ioapic_lock, flags);
  269. return eu.entry;
  270. }
  271. /*
  272. * When we write a new IO APIC routing entry, we need to write the high
  273. * word first! If the mask bit in the low word is clear, we will enable
  274. * the interrupt, and we need to make sure the entry is fully populated
  275. * before that happens.
  276. */
  277. static void
  278. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  279. {
  280. union entry_union eu;
  281. eu.entry = e;
  282. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  283. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  284. }
  285. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  286. {
  287. unsigned long flags;
  288. spin_lock_irqsave(&ioapic_lock, flags);
  289. __ioapic_write_entry(apic, pin, e);
  290. spin_unlock_irqrestore(&ioapic_lock, flags);
  291. }
  292. /*
  293. * When we mask an IO APIC routing entry, we need to write the low
  294. * word first, in order to set the mask bit before we change the
  295. * high bits!
  296. */
  297. static void ioapic_mask_entry(int apic, int pin)
  298. {
  299. unsigned long flags;
  300. union entry_union eu = { .entry.mask = 1 };
  301. spin_lock_irqsave(&ioapic_lock, flags);
  302. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  303. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  304. spin_unlock_irqrestore(&ioapic_lock, flags);
  305. }
  306. #ifdef CONFIG_SMP
  307. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
  308. {
  309. int apic, pin;
  310. struct irq_cfg *cfg;
  311. struct irq_pin_list *entry;
  312. cfg = irq_cfg(irq);
  313. entry = cfg->irq_2_pin;
  314. for (;;) {
  315. unsigned int reg;
  316. if (!entry)
  317. break;
  318. apic = entry->apic;
  319. pin = entry->pin;
  320. #ifdef CONFIG_INTR_REMAP
  321. /*
  322. * With interrupt-remapping, destination information comes
  323. * from interrupt-remapping table entry.
  324. */
  325. if (!irq_remapped(irq))
  326. io_apic_write(apic, 0x11 + pin*2, dest);
  327. #else
  328. io_apic_write(apic, 0x11 + pin*2, dest);
  329. #endif
  330. reg = io_apic_read(apic, 0x10 + pin*2);
  331. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  332. reg |= vector;
  333. io_apic_modify(apic, 0x10 + pin*2, reg);
  334. if (!entry->next)
  335. break;
  336. entry = entry->next;
  337. }
  338. }
  339. static int assign_irq_vector(int irq, cpumask_t mask);
  340. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  341. {
  342. struct irq_cfg *cfg;
  343. unsigned long flags;
  344. unsigned int dest;
  345. cpumask_t tmp;
  346. struct irq_desc *desc;
  347. cpus_and(tmp, mask, cpu_online_map);
  348. if (cpus_empty(tmp))
  349. return;
  350. cfg = irq_cfg(irq);
  351. if (assign_irq_vector(irq, mask))
  352. return;
  353. cpus_and(tmp, cfg->domain, mask);
  354. dest = cpu_mask_to_apicid(tmp);
  355. /*
  356. * Only the high 8 bits are valid.
  357. */
  358. dest = SET_APIC_LOGICAL_ID(dest);
  359. desc = irq_to_desc(irq);
  360. spin_lock_irqsave(&ioapic_lock, flags);
  361. __target_IO_APIC_irq(irq, dest, cfg->vector);
  362. desc->affinity = mask;
  363. spin_unlock_irqrestore(&ioapic_lock, flags);
  364. }
  365. #endif /* CONFIG_SMP */
  366. /*
  367. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  368. * shared ISA-space IRQs, so we have to support them. We are super
  369. * fast in the common case, and fast for shared ISA-space IRQs.
  370. */
  371. static void add_pin_to_irq_cpu(unsigned int irq, int cpu, int apic, int pin)
  372. {
  373. struct irq_pin_list *entry;
  374. struct irq_cfg *cfg = irq_cfg(irq);
  375. entry = cfg->irq_2_pin;
  376. if (!entry) {
  377. entry = get_one_free_irq_2_pin(cpu);
  378. if (!entry) {
  379. printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
  380. apic, pin);
  381. return;
  382. }
  383. cfg->irq_2_pin = entry;
  384. entry->apic = apic;
  385. entry->pin = pin;
  386. return;
  387. }
  388. while (entry->next) {
  389. /* not again, please */
  390. if (entry->apic == apic && entry->pin == pin)
  391. return;
  392. entry = entry->next;
  393. }
  394. entry->next = get_one_free_irq_2_pin(cpu);
  395. entry = entry->next;
  396. entry->apic = apic;
  397. entry->pin = pin;
  398. }
  399. /*
  400. * Reroute an IRQ to a different pin.
  401. */
  402. static void __init replace_pin_at_irq(unsigned int irq, int cpu,
  403. int oldapic, int oldpin,
  404. int newapic, int newpin)
  405. {
  406. struct irq_cfg *cfg = irq_cfg(irq);
  407. struct irq_pin_list *entry = cfg->irq_2_pin;
  408. int replaced = 0;
  409. while (entry) {
  410. if (entry->apic == oldapic && entry->pin == oldpin) {
  411. entry->apic = newapic;
  412. entry->pin = newpin;
  413. replaced = 1;
  414. /* every one is different, right? */
  415. break;
  416. }
  417. entry = entry->next;
  418. }
  419. /* why? call replace before add? */
  420. if (!replaced)
  421. add_pin_to_irq_cpu(irq, cpu, newapic, newpin);
  422. }
  423. static inline void io_apic_modify_irq(unsigned int irq,
  424. int mask_and, int mask_or,
  425. void (*final)(struct irq_pin_list *entry))
  426. {
  427. int pin;
  428. struct irq_cfg *cfg;
  429. struct irq_pin_list *entry;
  430. cfg = irq_cfg(irq);
  431. for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
  432. unsigned int reg;
  433. pin = entry->pin;
  434. reg = io_apic_read(entry->apic, 0x10 + pin * 2);
  435. reg &= mask_and;
  436. reg |= mask_or;
  437. io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
  438. if (final)
  439. final(entry);
  440. }
  441. }
  442. static void __unmask_IO_APIC_irq(unsigned int irq)
  443. {
  444. io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
  445. }
  446. #ifdef CONFIG_X86_64
  447. void io_apic_sync(struct irq_pin_list *entry)
  448. {
  449. /*
  450. * Synchronize the IO-APIC and the CPU by doing
  451. * a dummy read from the IO-APIC
  452. */
  453. struct io_apic __iomem *io_apic;
  454. io_apic = io_apic_base(entry->apic);
  455. readl(&io_apic->data);
  456. }
  457. static void __mask_IO_APIC_irq(unsigned int irq)
  458. {
  459. io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
  460. }
  461. #else /* CONFIG_X86_32 */
  462. static void __mask_IO_APIC_irq(unsigned int irq)
  463. {
  464. io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
  465. }
  466. static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
  467. {
  468. io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
  469. IO_APIC_REDIR_MASKED, NULL);
  470. }
  471. static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
  472. {
  473. io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
  474. IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
  475. }
  476. #endif /* CONFIG_X86_32 */
  477. static void mask_IO_APIC_irq (unsigned int irq)
  478. {
  479. unsigned long flags;
  480. spin_lock_irqsave(&ioapic_lock, flags);
  481. __mask_IO_APIC_irq(irq);
  482. spin_unlock_irqrestore(&ioapic_lock, flags);
  483. }
  484. static void unmask_IO_APIC_irq (unsigned int irq)
  485. {
  486. unsigned long flags;
  487. spin_lock_irqsave(&ioapic_lock, flags);
  488. __unmask_IO_APIC_irq(irq);
  489. spin_unlock_irqrestore(&ioapic_lock, flags);
  490. }
  491. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  492. {
  493. struct IO_APIC_route_entry entry;
  494. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  495. entry = ioapic_read_entry(apic, pin);
  496. if (entry.delivery_mode == dest_SMI)
  497. return;
  498. /*
  499. * Disable it in the IO-APIC irq-routing table:
  500. */
  501. ioapic_mask_entry(apic, pin);
  502. }
  503. static void clear_IO_APIC (void)
  504. {
  505. int apic, pin;
  506. for (apic = 0; apic < nr_ioapics; apic++)
  507. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  508. clear_IO_APIC_pin(apic, pin);
  509. }
  510. #if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
  511. void send_IPI_self(int vector)
  512. {
  513. unsigned int cfg;
  514. /*
  515. * Wait for idle.
  516. */
  517. apic_wait_icr_idle();
  518. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  519. /*
  520. * Send the IPI. The write to APIC_ICR fires this off.
  521. */
  522. apic_write(APIC_ICR, cfg);
  523. }
  524. #endif /* !CONFIG_SMP && CONFIG_X86_32*/
  525. #ifdef CONFIG_X86_32
  526. /*
  527. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  528. * specific CPU-side IRQs.
  529. */
  530. #define MAX_PIRQS 8
  531. static int pirq_entries [MAX_PIRQS];
  532. static int pirqs_enabled;
  533. static int __init ioapic_pirq_setup(char *str)
  534. {
  535. int i, max;
  536. int ints[MAX_PIRQS+1];
  537. get_options(str, ARRAY_SIZE(ints), ints);
  538. for (i = 0; i < MAX_PIRQS; i++)
  539. pirq_entries[i] = -1;
  540. pirqs_enabled = 1;
  541. apic_printk(APIC_VERBOSE, KERN_INFO
  542. "PIRQ redirection, working around broken MP-BIOS.\n");
  543. max = MAX_PIRQS;
  544. if (ints[0] < MAX_PIRQS)
  545. max = ints[0];
  546. for (i = 0; i < max; i++) {
  547. apic_printk(APIC_VERBOSE, KERN_DEBUG
  548. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  549. /*
  550. * PIRQs are mapped upside down, usually.
  551. */
  552. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  553. }
  554. return 1;
  555. }
  556. __setup("pirq=", ioapic_pirq_setup);
  557. #endif /* CONFIG_X86_32 */
  558. #ifdef CONFIG_INTR_REMAP
  559. /* I/O APIC RTE contents at the OS boot up */
  560. static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
  561. /*
  562. * Saves and masks all the unmasked IO-APIC RTE's
  563. */
  564. int save_mask_IO_APIC_setup(void)
  565. {
  566. union IO_APIC_reg_01 reg_01;
  567. unsigned long flags;
  568. int apic, pin;
  569. /*
  570. * The number of IO-APIC IRQ registers (== #pins):
  571. */
  572. for (apic = 0; apic < nr_ioapics; apic++) {
  573. spin_lock_irqsave(&ioapic_lock, flags);
  574. reg_01.raw = io_apic_read(apic, 1);
  575. spin_unlock_irqrestore(&ioapic_lock, flags);
  576. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  577. }
  578. for (apic = 0; apic < nr_ioapics; apic++) {
  579. early_ioapic_entries[apic] =
  580. kzalloc(sizeof(struct IO_APIC_route_entry) *
  581. nr_ioapic_registers[apic], GFP_KERNEL);
  582. if (!early_ioapic_entries[apic])
  583. goto nomem;
  584. }
  585. for (apic = 0; apic < nr_ioapics; apic++)
  586. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  587. struct IO_APIC_route_entry entry;
  588. entry = early_ioapic_entries[apic][pin] =
  589. ioapic_read_entry(apic, pin);
  590. if (!entry.mask) {
  591. entry.mask = 1;
  592. ioapic_write_entry(apic, pin, entry);
  593. }
  594. }
  595. return 0;
  596. nomem:
  597. while (apic >= 0)
  598. kfree(early_ioapic_entries[apic--]);
  599. memset(early_ioapic_entries, 0,
  600. ARRAY_SIZE(early_ioapic_entries));
  601. return -ENOMEM;
  602. }
  603. void restore_IO_APIC_setup(void)
  604. {
  605. int apic, pin;
  606. for (apic = 0; apic < nr_ioapics; apic++) {
  607. if (!early_ioapic_entries[apic])
  608. break;
  609. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  610. ioapic_write_entry(apic, pin,
  611. early_ioapic_entries[apic][pin]);
  612. kfree(early_ioapic_entries[apic]);
  613. early_ioapic_entries[apic] = NULL;
  614. }
  615. }
  616. void reinit_intr_remapped_IO_APIC(int intr_remapping)
  617. {
  618. /*
  619. * for now plain restore of previous settings.
  620. * TBD: In the case of OS enabling interrupt-remapping,
  621. * IO-APIC RTE's need to be setup to point to interrupt-remapping
  622. * table entries. for now, do a plain restore, and wait for
  623. * the setup_IO_APIC_irqs() to do proper initialization.
  624. */
  625. restore_IO_APIC_setup();
  626. }
  627. #endif
  628. /*
  629. * Find the IRQ entry number of a certain pin.
  630. */
  631. static int find_irq_entry(int apic, int pin, int type)
  632. {
  633. int i;
  634. for (i = 0; i < mp_irq_entries; i++)
  635. if (mp_irqs[i].mp_irqtype == type &&
  636. (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
  637. mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
  638. mp_irqs[i].mp_dstirq == pin)
  639. return i;
  640. return -1;
  641. }
  642. /*
  643. * Find the pin to which IRQ[irq] (ISA) is connected
  644. */
  645. static int __init find_isa_irq_pin(int irq, int type)
  646. {
  647. int i;
  648. for (i = 0; i < mp_irq_entries; i++) {
  649. int lbus = mp_irqs[i].mp_srcbus;
  650. if (test_bit(lbus, mp_bus_not_pci) &&
  651. (mp_irqs[i].mp_irqtype == type) &&
  652. (mp_irqs[i].mp_srcbusirq == irq))
  653. return mp_irqs[i].mp_dstirq;
  654. }
  655. return -1;
  656. }
  657. static int __init find_isa_irq_apic(int irq, int type)
  658. {
  659. int i;
  660. for (i = 0; i < mp_irq_entries; i++) {
  661. int lbus = mp_irqs[i].mp_srcbus;
  662. if (test_bit(lbus, mp_bus_not_pci) &&
  663. (mp_irqs[i].mp_irqtype == type) &&
  664. (mp_irqs[i].mp_srcbusirq == irq))
  665. break;
  666. }
  667. if (i < mp_irq_entries) {
  668. int apic;
  669. for(apic = 0; apic < nr_ioapics; apic++) {
  670. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
  671. return apic;
  672. }
  673. }
  674. return -1;
  675. }
  676. /*
  677. * Find a specific PCI IRQ entry.
  678. * Not an __init, possibly needed by modules
  679. */
  680. static int pin_2_irq(int idx, int apic, int pin);
  681. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  682. {
  683. int apic, i, best_guess = -1;
  684. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  685. bus, slot, pin);
  686. if (test_bit(bus, mp_bus_not_pci)) {
  687. apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  688. return -1;
  689. }
  690. for (i = 0; i < mp_irq_entries; i++) {
  691. int lbus = mp_irqs[i].mp_srcbus;
  692. for (apic = 0; apic < nr_ioapics; apic++)
  693. if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
  694. mp_irqs[i].mp_dstapic == MP_APIC_ALL)
  695. break;
  696. if (!test_bit(lbus, mp_bus_not_pci) &&
  697. !mp_irqs[i].mp_irqtype &&
  698. (bus == lbus) &&
  699. (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
  700. int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
  701. if (!(apic || IO_APIC_IRQ(irq)))
  702. continue;
  703. if (pin == (mp_irqs[i].mp_srcbusirq & 3))
  704. return irq;
  705. /*
  706. * Use the first all-but-pin matching entry as a
  707. * best-guess fuzzy result for broken mptables.
  708. */
  709. if (best_guess < 0)
  710. best_guess = irq;
  711. }
  712. }
  713. return best_guess;
  714. }
  715. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  716. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  717. /*
  718. * EISA Edge/Level control register, ELCR
  719. */
  720. static int EISA_ELCR(unsigned int irq)
  721. {
  722. if (irq < 16) {
  723. unsigned int port = 0x4d0 + (irq >> 3);
  724. return (inb(port) >> (irq & 7)) & 1;
  725. }
  726. apic_printk(APIC_VERBOSE, KERN_INFO
  727. "Broken MPtable reports ISA irq %d\n", irq);
  728. return 0;
  729. }
  730. #endif
  731. /* ISA interrupts are always polarity zero edge triggered,
  732. * when listed as conforming in the MP table. */
  733. #define default_ISA_trigger(idx) (0)
  734. #define default_ISA_polarity(idx) (0)
  735. /* EISA interrupts are always polarity zero and can be edge or level
  736. * trigger depending on the ELCR value. If an interrupt is listed as
  737. * EISA conforming in the MP table, that means its trigger type must
  738. * be read in from the ELCR */
  739. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
  740. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  741. /* PCI interrupts are always polarity one level triggered,
  742. * when listed as conforming in the MP table. */
  743. #define default_PCI_trigger(idx) (1)
  744. #define default_PCI_polarity(idx) (1)
  745. /* MCA interrupts are always polarity zero level triggered,
  746. * when listed as conforming in the MP table. */
  747. #define default_MCA_trigger(idx) (1)
  748. #define default_MCA_polarity(idx) default_ISA_polarity(idx)
  749. static int MPBIOS_polarity(int idx)
  750. {
  751. int bus = mp_irqs[idx].mp_srcbus;
  752. int polarity;
  753. /*
  754. * Determine IRQ line polarity (high active or low active):
  755. */
  756. switch (mp_irqs[idx].mp_irqflag & 3)
  757. {
  758. case 0: /* conforms, ie. bus-type dependent polarity */
  759. if (test_bit(bus, mp_bus_not_pci))
  760. polarity = default_ISA_polarity(idx);
  761. else
  762. polarity = default_PCI_polarity(idx);
  763. break;
  764. case 1: /* high active */
  765. {
  766. polarity = 0;
  767. break;
  768. }
  769. case 2: /* reserved */
  770. {
  771. printk(KERN_WARNING "broken BIOS!!\n");
  772. polarity = 1;
  773. break;
  774. }
  775. case 3: /* low active */
  776. {
  777. polarity = 1;
  778. break;
  779. }
  780. default: /* invalid */
  781. {
  782. printk(KERN_WARNING "broken BIOS!!\n");
  783. polarity = 1;
  784. break;
  785. }
  786. }
  787. return polarity;
  788. }
  789. static int MPBIOS_trigger(int idx)
  790. {
  791. int bus = mp_irqs[idx].mp_srcbus;
  792. int trigger;
  793. /*
  794. * Determine IRQ trigger mode (edge or level sensitive):
  795. */
  796. switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
  797. {
  798. case 0: /* conforms, ie. bus-type dependent */
  799. if (test_bit(bus, mp_bus_not_pci))
  800. trigger = default_ISA_trigger(idx);
  801. else
  802. trigger = default_PCI_trigger(idx);
  803. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  804. switch (mp_bus_id_to_type[bus]) {
  805. case MP_BUS_ISA: /* ISA pin */
  806. {
  807. /* set before the switch */
  808. break;
  809. }
  810. case MP_BUS_EISA: /* EISA pin */
  811. {
  812. trigger = default_EISA_trigger(idx);
  813. break;
  814. }
  815. case MP_BUS_PCI: /* PCI pin */
  816. {
  817. /* set before the switch */
  818. break;
  819. }
  820. case MP_BUS_MCA: /* MCA pin */
  821. {
  822. trigger = default_MCA_trigger(idx);
  823. break;
  824. }
  825. default:
  826. {
  827. printk(KERN_WARNING "broken BIOS!!\n");
  828. trigger = 1;
  829. break;
  830. }
  831. }
  832. #endif
  833. break;
  834. case 1: /* edge */
  835. {
  836. trigger = 0;
  837. break;
  838. }
  839. case 2: /* reserved */
  840. {
  841. printk(KERN_WARNING "broken BIOS!!\n");
  842. trigger = 1;
  843. break;
  844. }
  845. case 3: /* level */
  846. {
  847. trigger = 1;
  848. break;
  849. }
  850. default: /* invalid */
  851. {
  852. printk(KERN_WARNING "broken BIOS!!\n");
  853. trigger = 0;
  854. break;
  855. }
  856. }
  857. return trigger;
  858. }
  859. static inline int irq_polarity(int idx)
  860. {
  861. return MPBIOS_polarity(idx);
  862. }
  863. static inline int irq_trigger(int idx)
  864. {
  865. return MPBIOS_trigger(idx);
  866. }
  867. int (*ioapic_renumber_irq)(int ioapic, int irq);
  868. static int pin_2_irq(int idx, int apic, int pin)
  869. {
  870. int irq, i;
  871. int bus = mp_irqs[idx].mp_srcbus;
  872. /*
  873. * Debugging check, we are in big trouble if this message pops up!
  874. */
  875. if (mp_irqs[idx].mp_dstirq != pin)
  876. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  877. if (test_bit(bus, mp_bus_not_pci)) {
  878. irq = mp_irqs[idx].mp_srcbusirq;
  879. } else {
  880. /*
  881. * PCI IRQs are mapped in order
  882. */
  883. i = irq = 0;
  884. while (i < apic)
  885. irq += nr_ioapic_registers[i++];
  886. irq += pin;
  887. /*
  888. * For MPS mode, so far only needed by ES7000 platform
  889. */
  890. if (ioapic_renumber_irq)
  891. irq = ioapic_renumber_irq(apic, irq);
  892. }
  893. #ifdef CONFIG_X86_32
  894. /*
  895. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  896. */
  897. if ((pin >= 16) && (pin <= 23)) {
  898. if (pirq_entries[pin-16] != -1) {
  899. if (!pirq_entries[pin-16]) {
  900. apic_printk(APIC_VERBOSE, KERN_DEBUG
  901. "disabling PIRQ%d\n", pin-16);
  902. } else {
  903. irq = pirq_entries[pin-16];
  904. apic_printk(APIC_VERBOSE, KERN_DEBUG
  905. "using PIRQ%d -> IRQ %d\n",
  906. pin-16, irq);
  907. }
  908. }
  909. }
  910. #endif
  911. return irq;
  912. }
  913. void lock_vector_lock(void)
  914. {
  915. /* Used to the online set of cpus does not change
  916. * during assign_irq_vector.
  917. */
  918. spin_lock(&vector_lock);
  919. }
  920. void unlock_vector_lock(void)
  921. {
  922. spin_unlock(&vector_lock);
  923. }
  924. static int __assign_irq_vector(int irq, cpumask_t mask)
  925. {
  926. /*
  927. * NOTE! The local APIC isn't very good at handling
  928. * multiple interrupts at the same interrupt level.
  929. * As the interrupt level is determined by taking the
  930. * vector number and shifting that right by 4, we
  931. * want to spread these out a bit so that they don't
  932. * all fall in the same interrupt level.
  933. *
  934. * Also, we've got to be careful not to trash gate
  935. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  936. */
  937. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  938. unsigned int old_vector;
  939. int cpu;
  940. struct irq_cfg *cfg;
  941. cfg = irq_cfg(irq);
  942. /* Only try and allocate irqs on cpus that are present */
  943. cpus_and(mask, mask, cpu_online_map);
  944. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  945. return -EBUSY;
  946. old_vector = cfg->vector;
  947. if (old_vector) {
  948. cpumask_t tmp;
  949. cpus_and(tmp, cfg->domain, mask);
  950. if (!cpus_empty(tmp))
  951. return 0;
  952. }
  953. for_each_cpu_mask_nr(cpu, mask) {
  954. cpumask_t domain, new_mask;
  955. int new_cpu;
  956. int vector, offset;
  957. domain = vector_allocation_domain(cpu);
  958. cpus_and(new_mask, domain, cpu_online_map);
  959. vector = current_vector;
  960. offset = current_offset;
  961. next:
  962. vector += 8;
  963. if (vector >= first_system_vector) {
  964. /* If we run out of vectors on large boxen, must share them. */
  965. offset = (offset + 1) % 8;
  966. vector = FIRST_DEVICE_VECTOR + offset;
  967. }
  968. if (unlikely(current_vector == vector))
  969. continue;
  970. #ifdef CONFIG_X86_64
  971. if (vector == IA32_SYSCALL_VECTOR)
  972. goto next;
  973. #else
  974. if (vector == SYSCALL_VECTOR)
  975. goto next;
  976. #endif
  977. for_each_cpu_mask_nr(new_cpu, new_mask)
  978. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  979. goto next;
  980. /* Found one! */
  981. current_vector = vector;
  982. current_offset = offset;
  983. if (old_vector) {
  984. cfg->move_in_progress = 1;
  985. cfg->old_domain = cfg->domain;
  986. }
  987. for_each_cpu_mask_nr(new_cpu, new_mask)
  988. per_cpu(vector_irq, new_cpu)[vector] = irq;
  989. cfg->vector = vector;
  990. cfg->domain = domain;
  991. return 0;
  992. }
  993. return -ENOSPC;
  994. }
  995. static int assign_irq_vector(int irq, cpumask_t mask)
  996. {
  997. int err;
  998. unsigned long flags;
  999. spin_lock_irqsave(&vector_lock, flags);
  1000. err = __assign_irq_vector(irq, mask);
  1001. spin_unlock_irqrestore(&vector_lock, flags);
  1002. return err;
  1003. }
  1004. static void __clear_irq_vector(int irq)
  1005. {
  1006. struct irq_cfg *cfg;
  1007. cpumask_t mask;
  1008. int cpu, vector;
  1009. cfg = irq_cfg(irq);
  1010. BUG_ON(!cfg->vector);
  1011. vector = cfg->vector;
  1012. cpus_and(mask, cfg->domain, cpu_online_map);
  1013. for_each_cpu_mask_nr(cpu, mask)
  1014. per_cpu(vector_irq, cpu)[vector] = -1;
  1015. cfg->vector = 0;
  1016. cpus_clear(cfg->domain);
  1017. if (likely(!cfg->move_in_progress))
  1018. return;
  1019. cpus_and(mask, cfg->old_domain, cpu_online_map);
  1020. for_each_cpu_mask_nr(cpu, mask) {
  1021. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
  1022. vector++) {
  1023. if (per_cpu(vector_irq, cpu)[vector] != irq)
  1024. continue;
  1025. per_cpu(vector_irq, cpu)[vector] = -1;
  1026. break;
  1027. }
  1028. }
  1029. cfg->move_in_progress = 0;
  1030. }
  1031. void __setup_vector_irq(int cpu)
  1032. {
  1033. /* Initialize vector_irq on a new cpu */
  1034. /* This function must be called with vector_lock held */
  1035. int irq, vector;
  1036. struct irq_cfg *cfg;
  1037. struct irq_desc *desc;
  1038. /* Mark the inuse vectors */
  1039. for_each_irq_desc(irq, desc) {
  1040. if (!desc)
  1041. continue;
  1042. cfg = desc->chip_data;
  1043. if (!cpu_isset(cpu, cfg->domain))
  1044. continue;
  1045. vector = cfg->vector;
  1046. per_cpu(vector_irq, cpu)[vector] = irq;
  1047. }
  1048. /* Mark the free vectors */
  1049. for (vector = 0; vector < NR_VECTORS; ++vector) {
  1050. irq = per_cpu(vector_irq, cpu)[vector];
  1051. if (irq < 0)
  1052. continue;
  1053. cfg = irq_cfg(irq);
  1054. if (!cpu_isset(cpu, cfg->domain))
  1055. per_cpu(vector_irq, cpu)[vector] = -1;
  1056. }
  1057. }
  1058. static struct irq_chip ioapic_chip;
  1059. #ifdef CONFIG_INTR_REMAP
  1060. static struct irq_chip ir_ioapic_chip;
  1061. #endif
  1062. #define IOAPIC_AUTO -1
  1063. #define IOAPIC_EDGE 0
  1064. #define IOAPIC_LEVEL 1
  1065. #ifdef CONFIG_X86_32
  1066. static inline int IO_APIC_irq_trigger(int irq)
  1067. {
  1068. int apic, idx, pin;
  1069. for (apic = 0; apic < nr_ioapics; apic++) {
  1070. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1071. idx = find_irq_entry(apic, pin, mp_INT);
  1072. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  1073. return irq_trigger(idx);
  1074. }
  1075. }
  1076. /*
  1077. * nonexistent IRQs are edge default
  1078. */
  1079. return 0;
  1080. }
  1081. #else
  1082. static inline int IO_APIC_irq_trigger(int irq)
  1083. {
  1084. return 1;
  1085. }
  1086. #endif
  1087. static void ioapic_register_intr(int irq, unsigned long trigger)
  1088. {
  1089. struct irq_desc *desc;
  1090. desc = irq_to_desc(irq);
  1091. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1092. trigger == IOAPIC_LEVEL)
  1093. desc->status |= IRQ_LEVEL;
  1094. else
  1095. desc->status &= ~IRQ_LEVEL;
  1096. #ifdef CONFIG_INTR_REMAP
  1097. if (irq_remapped(irq)) {
  1098. desc->status |= IRQ_MOVE_PCNTXT;
  1099. if (trigger)
  1100. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1101. handle_fasteoi_irq,
  1102. "fasteoi");
  1103. else
  1104. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  1105. handle_edge_irq, "edge");
  1106. return;
  1107. }
  1108. #endif
  1109. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1110. trigger == IOAPIC_LEVEL)
  1111. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1112. handle_fasteoi_irq,
  1113. "fasteoi");
  1114. else
  1115. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1116. handle_edge_irq, "edge");
  1117. }
  1118. static int setup_ioapic_entry(int apic, int irq,
  1119. struct IO_APIC_route_entry *entry,
  1120. unsigned int destination, int trigger,
  1121. int polarity, int vector)
  1122. {
  1123. /*
  1124. * add it to the IO-APIC irq-routing table:
  1125. */
  1126. memset(entry,0,sizeof(*entry));
  1127. #ifdef CONFIG_INTR_REMAP
  1128. if (intr_remapping_enabled) {
  1129. struct intel_iommu *iommu = map_ioapic_to_ir(apic);
  1130. struct irte irte;
  1131. struct IR_IO_APIC_route_entry *ir_entry =
  1132. (struct IR_IO_APIC_route_entry *) entry;
  1133. int index;
  1134. if (!iommu)
  1135. panic("No mapping iommu for ioapic %d\n", apic);
  1136. index = alloc_irte(iommu, irq, 1);
  1137. if (index < 0)
  1138. panic("Failed to allocate IRTE for ioapic %d\n", apic);
  1139. memset(&irte, 0, sizeof(irte));
  1140. irte.present = 1;
  1141. irte.dst_mode = INT_DEST_MODE;
  1142. irte.trigger_mode = trigger;
  1143. irte.dlvry_mode = INT_DELIVERY_MODE;
  1144. irte.vector = vector;
  1145. irte.dest_id = IRTE_DEST(destination);
  1146. modify_irte(irq, &irte);
  1147. ir_entry->index2 = (index >> 15) & 0x1;
  1148. ir_entry->zero = 0;
  1149. ir_entry->format = 1;
  1150. ir_entry->index = (index & 0x7fff);
  1151. } else
  1152. #endif
  1153. {
  1154. entry->delivery_mode = INT_DELIVERY_MODE;
  1155. entry->dest_mode = INT_DEST_MODE;
  1156. entry->dest = destination;
  1157. }
  1158. entry->mask = 0; /* enable IRQ */
  1159. entry->trigger = trigger;
  1160. entry->polarity = polarity;
  1161. entry->vector = vector;
  1162. /* Mask level triggered irqs.
  1163. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  1164. */
  1165. if (trigger)
  1166. entry->mask = 1;
  1167. return 0;
  1168. }
  1169. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  1170. int trigger, int polarity)
  1171. {
  1172. struct irq_cfg *cfg;
  1173. struct IO_APIC_route_entry entry;
  1174. cpumask_t mask;
  1175. if (!IO_APIC_IRQ(irq))
  1176. return;
  1177. cfg = irq_cfg(irq);
  1178. mask = TARGET_CPUS;
  1179. if (assign_irq_vector(irq, mask))
  1180. return;
  1181. cpus_and(mask, cfg->domain, mask);
  1182. apic_printk(APIC_VERBOSE,KERN_DEBUG
  1183. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  1184. "IRQ %d Mode:%i Active:%i)\n",
  1185. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  1186. irq, trigger, polarity);
  1187. if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
  1188. cpu_mask_to_apicid(mask), trigger, polarity,
  1189. cfg->vector)) {
  1190. printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  1191. mp_ioapics[apic].mp_apicid, pin);
  1192. __clear_irq_vector(irq);
  1193. return;
  1194. }
  1195. ioapic_register_intr(irq, trigger);
  1196. if (irq < 16)
  1197. disable_8259A_irq(irq);
  1198. ioapic_write_entry(apic, pin, entry);
  1199. }
  1200. static void __init setup_IO_APIC_irqs(void)
  1201. {
  1202. int apic, pin, idx, irq;
  1203. int notcon = 0;
  1204. struct irq_desc *desc;
  1205. int cpu = boot_cpu_id;
  1206. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1207. for (apic = 0; apic < nr_ioapics; apic++) {
  1208. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1209. idx = find_irq_entry(apic, pin, mp_INT);
  1210. if (idx == -1) {
  1211. if (!notcon) {
  1212. notcon = 1;
  1213. apic_printk(APIC_VERBOSE,
  1214. KERN_DEBUG " %d-%d",
  1215. mp_ioapics[apic].mp_apicid,
  1216. pin);
  1217. } else
  1218. apic_printk(APIC_VERBOSE, " %d-%d",
  1219. mp_ioapics[apic].mp_apicid,
  1220. pin);
  1221. continue;
  1222. }
  1223. if (notcon) {
  1224. apic_printk(APIC_VERBOSE,
  1225. " (apicid-pin) not connected\n");
  1226. notcon = 0;
  1227. }
  1228. irq = pin_2_irq(idx, apic, pin);
  1229. #ifdef CONFIG_X86_32
  1230. if (multi_timer_check(apic, irq))
  1231. continue;
  1232. #endif
  1233. desc = irq_to_desc_alloc_cpu(irq, cpu);
  1234. if (!desc) {
  1235. printk(KERN_INFO "can not get irq_desc for %d\n", irq);
  1236. continue;
  1237. }
  1238. add_pin_to_irq_cpu(irq, cpu, apic, pin);
  1239. setup_IO_APIC_irq(apic, pin, irq,
  1240. irq_trigger(idx), irq_polarity(idx));
  1241. }
  1242. }
  1243. if (notcon)
  1244. apic_printk(APIC_VERBOSE,
  1245. " (apicid-pin) not connected\n");
  1246. }
  1247. /*
  1248. * Set up the timer pin, possibly with the 8259A-master behind.
  1249. */
  1250. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  1251. int vector)
  1252. {
  1253. struct IO_APIC_route_entry entry;
  1254. #ifdef CONFIG_INTR_REMAP
  1255. if (intr_remapping_enabled)
  1256. return;
  1257. #endif
  1258. memset(&entry, 0, sizeof(entry));
  1259. /*
  1260. * We use logical delivery to get the timer IRQ
  1261. * to the first CPU.
  1262. */
  1263. entry.dest_mode = INT_DEST_MODE;
  1264. entry.mask = 1; /* mask IRQ now */
  1265. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  1266. entry.delivery_mode = INT_DELIVERY_MODE;
  1267. entry.polarity = 0;
  1268. entry.trigger = 0;
  1269. entry.vector = vector;
  1270. /*
  1271. * The timer IRQ doesn't have to know that behind the
  1272. * scene we may have a 8259A-master in AEOI mode ...
  1273. */
  1274. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  1275. /*
  1276. * Add it to the IO-APIC irq-routing table:
  1277. */
  1278. ioapic_write_entry(apic, pin, entry);
  1279. }
  1280. __apicdebuginit(void) print_IO_APIC(void)
  1281. {
  1282. int apic, i;
  1283. union IO_APIC_reg_00 reg_00;
  1284. union IO_APIC_reg_01 reg_01;
  1285. union IO_APIC_reg_02 reg_02;
  1286. union IO_APIC_reg_03 reg_03;
  1287. unsigned long flags;
  1288. struct irq_cfg *cfg;
  1289. struct irq_desc *desc;
  1290. unsigned int irq;
  1291. if (apic_verbosity == APIC_QUIET)
  1292. return;
  1293. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1294. for (i = 0; i < nr_ioapics; i++)
  1295. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1296. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  1297. /*
  1298. * We are a bit conservative about what we expect. We have to
  1299. * know about every hardware change ASAP.
  1300. */
  1301. printk(KERN_INFO "testing the IO APIC.......................\n");
  1302. for (apic = 0; apic < nr_ioapics; apic++) {
  1303. spin_lock_irqsave(&ioapic_lock, flags);
  1304. reg_00.raw = io_apic_read(apic, 0);
  1305. reg_01.raw = io_apic_read(apic, 1);
  1306. if (reg_01.bits.version >= 0x10)
  1307. reg_02.raw = io_apic_read(apic, 2);
  1308. if (reg_01.bits.version >= 0x20)
  1309. reg_03.raw = io_apic_read(apic, 3);
  1310. spin_unlock_irqrestore(&ioapic_lock, flags);
  1311. printk("\n");
  1312. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  1313. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1314. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1315. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1316. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1317. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1318. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1319. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1320. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1321. /*
  1322. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1323. * but the value of reg_02 is read as the previous read register
  1324. * value, so ignore it if reg_02 == reg_01.
  1325. */
  1326. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1327. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1328. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1329. }
  1330. /*
  1331. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1332. * or reg_03, but the value of reg_0[23] is read as the previous read
  1333. * register value, so ignore it if reg_03 == reg_0[12].
  1334. */
  1335. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1336. reg_03.raw != reg_01.raw) {
  1337. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1338. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1339. }
  1340. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1341. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  1342. " Stat Dmod Deli Vect: \n");
  1343. for (i = 0; i <= reg_01.bits.entries; i++) {
  1344. struct IO_APIC_route_entry entry;
  1345. entry = ioapic_read_entry(apic, i);
  1346. printk(KERN_DEBUG " %02x %03X ",
  1347. i,
  1348. entry.dest
  1349. );
  1350. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1351. entry.mask,
  1352. entry.trigger,
  1353. entry.irr,
  1354. entry.polarity,
  1355. entry.delivery_status,
  1356. entry.dest_mode,
  1357. entry.delivery_mode,
  1358. entry.vector
  1359. );
  1360. }
  1361. }
  1362. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1363. for_each_irq_desc(irq, desc) {
  1364. struct irq_pin_list *entry;
  1365. if (!desc)
  1366. continue;
  1367. cfg = desc->chip_data;
  1368. entry = cfg->irq_2_pin;
  1369. if (!entry)
  1370. continue;
  1371. printk(KERN_DEBUG "IRQ%d ", irq);
  1372. for (;;) {
  1373. printk("-> %d:%d", entry->apic, entry->pin);
  1374. if (!entry->next)
  1375. break;
  1376. entry = entry->next;
  1377. }
  1378. printk("\n");
  1379. }
  1380. printk(KERN_INFO ".................................... done.\n");
  1381. return;
  1382. }
  1383. __apicdebuginit(void) print_APIC_bitfield(int base)
  1384. {
  1385. unsigned int v;
  1386. int i, j;
  1387. if (apic_verbosity == APIC_QUIET)
  1388. return;
  1389. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1390. for (i = 0; i < 8; i++) {
  1391. v = apic_read(base + i*0x10);
  1392. for (j = 0; j < 32; j++) {
  1393. if (v & (1<<j))
  1394. printk("1");
  1395. else
  1396. printk("0");
  1397. }
  1398. printk("\n");
  1399. }
  1400. }
  1401. __apicdebuginit(void) print_local_APIC(void *dummy)
  1402. {
  1403. unsigned int v, ver, maxlvt;
  1404. u64 icr;
  1405. if (apic_verbosity == APIC_QUIET)
  1406. return;
  1407. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1408. smp_processor_id(), hard_smp_processor_id());
  1409. v = apic_read(APIC_ID);
  1410. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1411. v = apic_read(APIC_LVR);
  1412. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1413. ver = GET_APIC_VERSION(v);
  1414. maxlvt = lapic_get_maxlvt();
  1415. v = apic_read(APIC_TASKPRI);
  1416. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1417. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1418. if (!APIC_XAPIC(ver)) {
  1419. v = apic_read(APIC_ARBPRI);
  1420. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1421. v & APIC_ARBPRI_MASK);
  1422. }
  1423. v = apic_read(APIC_PROCPRI);
  1424. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1425. }
  1426. /*
  1427. * Remote read supported only in the 82489DX and local APIC for
  1428. * Pentium processors.
  1429. */
  1430. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1431. v = apic_read(APIC_RRR);
  1432. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1433. }
  1434. v = apic_read(APIC_LDR);
  1435. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1436. if (!x2apic_enabled()) {
  1437. v = apic_read(APIC_DFR);
  1438. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1439. }
  1440. v = apic_read(APIC_SPIV);
  1441. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1442. printk(KERN_DEBUG "... APIC ISR field:\n");
  1443. print_APIC_bitfield(APIC_ISR);
  1444. printk(KERN_DEBUG "... APIC TMR field:\n");
  1445. print_APIC_bitfield(APIC_TMR);
  1446. printk(KERN_DEBUG "... APIC IRR field:\n");
  1447. print_APIC_bitfield(APIC_IRR);
  1448. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1449. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1450. apic_write(APIC_ESR, 0);
  1451. v = apic_read(APIC_ESR);
  1452. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1453. }
  1454. icr = apic_icr_read();
  1455. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1456. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1457. v = apic_read(APIC_LVTT);
  1458. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1459. if (maxlvt > 3) { /* PC is LVT#4. */
  1460. v = apic_read(APIC_LVTPC);
  1461. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1462. }
  1463. v = apic_read(APIC_LVT0);
  1464. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1465. v = apic_read(APIC_LVT1);
  1466. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1467. if (maxlvt > 2) { /* ERR is LVT#3. */
  1468. v = apic_read(APIC_LVTERR);
  1469. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1470. }
  1471. v = apic_read(APIC_TMICT);
  1472. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1473. v = apic_read(APIC_TMCCT);
  1474. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1475. v = apic_read(APIC_TDCR);
  1476. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1477. printk("\n");
  1478. }
  1479. __apicdebuginit(void) print_all_local_APICs(void)
  1480. {
  1481. int cpu;
  1482. preempt_disable();
  1483. for_each_online_cpu(cpu)
  1484. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1485. preempt_enable();
  1486. }
  1487. __apicdebuginit(void) print_PIC(void)
  1488. {
  1489. unsigned int v;
  1490. unsigned long flags;
  1491. if (apic_verbosity == APIC_QUIET)
  1492. return;
  1493. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1494. spin_lock_irqsave(&i8259A_lock, flags);
  1495. v = inb(0xa1) << 8 | inb(0x21);
  1496. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1497. v = inb(0xa0) << 8 | inb(0x20);
  1498. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1499. outb(0x0b,0xa0);
  1500. outb(0x0b,0x20);
  1501. v = inb(0xa0) << 8 | inb(0x20);
  1502. outb(0x0a,0xa0);
  1503. outb(0x0a,0x20);
  1504. spin_unlock_irqrestore(&i8259A_lock, flags);
  1505. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1506. v = inb(0x4d1) << 8 | inb(0x4d0);
  1507. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1508. }
  1509. __apicdebuginit(int) print_all_ICs(void)
  1510. {
  1511. print_PIC();
  1512. print_all_local_APICs();
  1513. print_IO_APIC();
  1514. return 0;
  1515. }
  1516. fs_initcall(print_all_ICs);
  1517. /* Where if anywhere is the i8259 connect in external int mode */
  1518. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1519. void __init enable_IO_APIC(void)
  1520. {
  1521. union IO_APIC_reg_01 reg_01;
  1522. int i8259_apic, i8259_pin;
  1523. int apic;
  1524. unsigned long flags;
  1525. #ifdef CONFIG_X86_32
  1526. int i;
  1527. if (!pirqs_enabled)
  1528. for (i = 0; i < MAX_PIRQS; i++)
  1529. pirq_entries[i] = -1;
  1530. #endif
  1531. /*
  1532. * The number of IO-APIC IRQ registers (== #pins):
  1533. */
  1534. for (apic = 0; apic < nr_ioapics; apic++) {
  1535. spin_lock_irqsave(&ioapic_lock, flags);
  1536. reg_01.raw = io_apic_read(apic, 1);
  1537. spin_unlock_irqrestore(&ioapic_lock, flags);
  1538. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1539. }
  1540. for(apic = 0; apic < nr_ioapics; apic++) {
  1541. int pin;
  1542. /* See if any of the pins is in ExtINT mode */
  1543. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1544. struct IO_APIC_route_entry entry;
  1545. entry = ioapic_read_entry(apic, pin);
  1546. /* If the interrupt line is enabled and in ExtInt mode
  1547. * I have found the pin where the i8259 is connected.
  1548. */
  1549. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1550. ioapic_i8259.apic = apic;
  1551. ioapic_i8259.pin = pin;
  1552. goto found_i8259;
  1553. }
  1554. }
  1555. }
  1556. found_i8259:
  1557. /* Look to see what if the MP table has reported the ExtINT */
  1558. /* If we could not find the appropriate pin by looking at the ioapic
  1559. * the i8259 probably is not connected the ioapic but give the
  1560. * mptable a chance anyway.
  1561. */
  1562. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1563. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1564. /* Trust the MP table if nothing is setup in the hardware */
  1565. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1566. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1567. ioapic_i8259.pin = i8259_pin;
  1568. ioapic_i8259.apic = i8259_apic;
  1569. }
  1570. /* Complain if the MP table and the hardware disagree */
  1571. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1572. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1573. {
  1574. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1575. }
  1576. /*
  1577. * Do not trust the IO-APIC being empty at bootup
  1578. */
  1579. clear_IO_APIC();
  1580. }
  1581. /*
  1582. * Not an __init, needed by the reboot code
  1583. */
  1584. void disable_IO_APIC(void)
  1585. {
  1586. /*
  1587. * Clear the IO-APIC before rebooting:
  1588. */
  1589. clear_IO_APIC();
  1590. /*
  1591. * If the i8259 is routed through an IOAPIC
  1592. * Put that IOAPIC in virtual wire mode
  1593. * so legacy interrupts can be delivered.
  1594. */
  1595. if (ioapic_i8259.pin != -1) {
  1596. struct IO_APIC_route_entry entry;
  1597. memset(&entry, 0, sizeof(entry));
  1598. entry.mask = 0; /* Enabled */
  1599. entry.trigger = 0; /* Edge */
  1600. entry.irr = 0;
  1601. entry.polarity = 0; /* High */
  1602. entry.delivery_status = 0;
  1603. entry.dest_mode = 0; /* Physical */
  1604. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1605. entry.vector = 0;
  1606. entry.dest = read_apic_id();
  1607. /*
  1608. * Add it to the IO-APIC irq-routing table:
  1609. */
  1610. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1611. }
  1612. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1613. }
  1614. #ifdef CONFIG_X86_32
  1615. /*
  1616. * function to set the IO-APIC physical IDs based on the
  1617. * values stored in the MPC table.
  1618. *
  1619. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1620. */
  1621. static void __init setup_ioapic_ids_from_mpc(void)
  1622. {
  1623. union IO_APIC_reg_00 reg_00;
  1624. physid_mask_t phys_id_present_map;
  1625. int apic;
  1626. int i;
  1627. unsigned char old_id;
  1628. unsigned long flags;
  1629. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1630. return;
  1631. /*
  1632. * Don't check I/O APIC IDs for xAPIC systems. They have
  1633. * no meaning without the serial APIC bus.
  1634. */
  1635. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1636. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1637. return;
  1638. /*
  1639. * This is broken; anything with a real cpu count has to
  1640. * circumvent this idiocy regardless.
  1641. */
  1642. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1643. /*
  1644. * Set the IOAPIC ID to the value stored in the MPC table.
  1645. */
  1646. for (apic = 0; apic < nr_ioapics; apic++) {
  1647. /* Read the register 0 value */
  1648. spin_lock_irqsave(&ioapic_lock, flags);
  1649. reg_00.raw = io_apic_read(apic, 0);
  1650. spin_unlock_irqrestore(&ioapic_lock, flags);
  1651. old_id = mp_ioapics[apic].mp_apicid;
  1652. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1653. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1654. apic, mp_ioapics[apic].mp_apicid);
  1655. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1656. reg_00.bits.ID);
  1657. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1658. }
  1659. /*
  1660. * Sanity check, is the ID really free? Every APIC in a
  1661. * system must have a unique ID or we get lots of nice
  1662. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1663. */
  1664. if (check_apicid_used(phys_id_present_map,
  1665. mp_ioapics[apic].mp_apicid)) {
  1666. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1667. apic, mp_ioapics[apic].mp_apicid);
  1668. for (i = 0; i < get_physical_broadcast(); i++)
  1669. if (!physid_isset(i, phys_id_present_map))
  1670. break;
  1671. if (i >= get_physical_broadcast())
  1672. panic("Max APIC ID exceeded!\n");
  1673. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1674. i);
  1675. physid_set(i, phys_id_present_map);
  1676. mp_ioapics[apic].mp_apicid = i;
  1677. } else {
  1678. physid_mask_t tmp;
  1679. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1680. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1681. "phys_id_present_map\n",
  1682. mp_ioapics[apic].mp_apicid);
  1683. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1684. }
  1685. /*
  1686. * We need to adjust the IRQ routing table
  1687. * if the ID changed.
  1688. */
  1689. if (old_id != mp_ioapics[apic].mp_apicid)
  1690. for (i = 0; i < mp_irq_entries; i++)
  1691. if (mp_irqs[i].mp_dstapic == old_id)
  1692. mp_irqs[i].mp_dstapic
  1693. = mp_ioapics[apic].mp_apicid;
  1694. /*
  1695. * Read the right value from the MPC table and
  1696. * write it into the ID register.
  1697. */
  1698. apic_printk(APIC_VERBOSE, KERN_INFO
  1699. "...changing IO-APIC physical APIC ID to %d ...",
  1700. mp_ioapics[apic].mp_apicid);
  1701. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1702. spin_lock_irqsave(&ioapic_lock, flags);
  1703. io_apic_write(apic, 0, reg_00.raw);
  1704. spin_unlock_irqrestore(&ioapic_lock, flags);
  1705. /*
  1706. * Sanity check
  1707. */
  1708. spin_lock_irqsave(&ioapic_lock, flags);
  1709. reg_00.raw = io_apic_read(apic, 0);
  1710. spin_unlock_irqrestore(&ioapic_lock, flags);
  1711. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1712. printk("could not set ID!\n");
  1713. else
  1714. apic_printk(APIC_VERBOSE, " ok.\n");
  1715. }
  1716. }
  1717. #endif
  1718. int no_timer_check __initdata;
  1719. static int __init notimercheck(char *s)
  1720. {
  1721. no_timer_check = 1;
  1722. return 1;
  1723. }
  1724. __setup("no_timer_check", notimercheck);
  1725. /*
  1726. * There is a nasty bug in some older SMP boards, their mptable lies
  1727. * about the timer IRQ. We do the following to work around the situation:
  1728. *
  1729. * - timer IRQ defaults to IO-APIC IRQ
  1730. * - if this function detects that timer IRQs are defunct, then we fall
  1731. * back to ISA timer IRQs
  1732. */
  1733. static int __init timer_irq_works(void)
  1734. {
  1735. unsigned long t1 = jiffies;
  1736. unsigned long flags;
  1737. if (no_timer_check)
  1738. return 1;
  1739. local_save_flags(flags);
  1740. local_irq_enable();
  1741. /* Let ten ticks pass... */
  1742. mdelay((10 * 1000) / HZ);
  1743. local_irq_restore(flags);
  1744. /*
  1745. * Expect a few ticks at least, to be sure some possible
  1746. * glue logic does not lock up after one or two first
  1747. * ticks in a non-ExtINT mode. Also the local APIC
  1748. * might have cached one ExtINT interrupt. Finally, at
  1749. * least one tick may be lost due to delays.
  1750. */
  1751. /* jiffies wrap? */
  1752. if (time_after(jiffies, t1 + 4))
  1753. return 1;
  1754. return 0;
  1755. }
  1756. /*
  1757. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1758. * number of pending IRQ events unhandled. These cases are very rare,
  1759. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1760. * better to do it this way as thus we do not have to be aware of
  1761. * 'pending' interrupts in the IRQ path, except at this point.
  1762. */
  1763. /*
  1764. * Edge triggered needs to resend any interrupt
  1765. * that was delayed but this is now handled in the device
  1766. * independent code.
  1767. */
  1768. /*
  1769. * Starting up a edge-triggered IO-APIC interrupt is
  1770. * nasty - we need to make sure that we get the edge.
  1771. * If it is already asserted for some reason, we need
  1772. * return 1 to indicate that is was pending.
  1773. *
  1774. * This is not complete - we should be able to fake
  1775. * an edge even if it isn't on the 8259A...
  1776. */
  1777. static unsigned int startup_ioapic_irq(unsigned int irq)
  1778. {
  1779. int was_pending = 0;
  1780. unsigned long flags;
  1781. struct irq_cfg *cfg;
  1782. spin_lock_irqsave(&ioapic_lock, flags);
  1783. if (irq < 16) {
  1784. disable_8259A_irq(irq);
  1785. if (i8259A_irq_pending(irq))
  1786. was_pending = 1;
  1787. }
  1788. cfg = irq_cfg(irq);
  1789. __unmask_IO_APIC_irq(irq);
  1790. spin_unlock_irqrestore(&ioapic_lock, flags);
  1791. return was_pending;
  1792. }
  1793. #ifdef CONFIG_X86_64
  1794. static int ioapic_retrigger_irq(unsigned int irq)
  1795. {
  1796. struct irq_cfg *cfg = irq_cfg(irq);
  1797. unsigned long flags;
  1798. spin_lock_irqsave(&vector_lock, flags);
  1799. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
  1800. spin_unlock_irqrestore(&vector_lock, flags);
  1801. return 1;
  1802. }
  1803. #else
  1804. static int ioapic_retrigger_irq(unsigned int irq)
  1805. {
  1806. send_IPI_self(irq_cfg(irq)->vector);
  1807. return 1;
  1808. }
  1809. #endif
  1810. /*
  1811. * Level and edge triggered IO-APIC interrupts need different handling,
  1812. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1813. * handled with the level-triggered descriptor, but that one has slightly
  1814. * more overhead. Level-triggered interrupts cannot be handled with the
  1815. * edge-triggered handler, without risking IRQ storms and other ugly
  1816. * races.
  1817. */
  1818. #ifdef CONFIG_SMP
  1819. #ifdef CONFIG_INTR_REMAP
  1820. static void ir_irq_migration(struct work_struct *work);
  1821. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1822. /*
  1823. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1824. *
  1825. * For edge triggered, irq migration is a simple atomic update(of vector
  1826. * and cpu destination) of IRTE and flush the hardware cache.
  1827. *
  1828. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1829. * vector information, along with modifying IRTE with vector and destination.
  1830. * So irq migration for level triggered is little bit more complex compared to
  1831. * edge triggered migration. But the good news is, we use the same algorithm
  1832. * for level triggered migration as we have today, only difference being,
  1833. * we now initiate the irq migration from process context instead of the
  1834. * interrupt context.
  1835. *
  1836. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1837. * suppression) to the IO-APIC, level triggered irq migration will also be
  1838. * as simple as edge triggered migration and we can do the irq migration
  1839. * with a simple atomic update to IO-APIC RTE.
  1840. */
  1841. static void migrate_ioapic_irq(int irq, cpumask_t mask)
  1842. {
  1843. struct irq_cfg *cfg;
  1844. struct irq_desc *desc;
  1845. cpumask_t tmp, cleanup_mask;
  1846. struct irte irte;
  1847. int modify_ioapic_rte;
  1848. unsigned int dest;
  1849. unsigned long flags;
  1850. cpus_and(tmp, mask, cpu_online_map);
  1851. if (cpus_empty(tmp))
  1852. return;
  1853. if (get_irte(irq, &irte))
  1854. return;
  1855. if (assign_irq_vector(irq, mask))
  1856. return;
  1857. cfg = irq_cfg(irq);
  1858. cpus_and(tmp, cfg->domain, mask);
  1859. dest = cpu_mask_to_apicid(tmp);
  1860. desc = irq_to_desc(irq);
  1861. modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1862. if (modify_ioapic_rte) {
  1863. spin_lock_irqsave(&ioapic_lock, flags);
  1864. __target_IO_APIC_irq(irq, dest, cfg->vector);
  1865. spin_unlock_irqrestore(&ioapic_lock, flags);
  1866. }
  1867. irte.vector = cfg->vector;
  1868. irte.dest_id = IRTE_DEST(dest);
  1869. /*
  1870. * Modified the IRTE and flushes the Interrupt entry cache.
  1871. */
  1872. modify_irte(irq, &irte);
  1873. if (cfg->move_in_progress) {
  1874. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1875. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1876. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1877. cfg->move_in_progress = 0;
  1878. }
  1879. desc->affinity = mask;
  1880. }
  1881. static int migrate_irq_remapped_level(int irq)
  1882. {
  1883. int ret = -1;
  1884. struct irq_desc *desc = irq_to_desc(irq);
  1885. mask_IO_APIC_irq(irq);
  1886. if (io_apic_level_ack_pending(irq)) {
  1887. /*
  1888. * Interrupt in progress. Migrating irq now will change the
  1889. * vector information in the IO-APIC RTE and that will confuse
  1890. * the EOI broadcast performed by cpu.
  1891. * So, delay the irq migration to the next instance.
  1892. */
  1893. schedule_delayed_work(&ir_migration_work, 1);
  1894. goto unmask;
  1895. }
  1896. /* everthing is clear. we have right of way */
  1897. migrate_ioapic_irq(irq, desc->pending_mask);
  1898. ret = 0;
  1899. desc->status &= ~IRQ_MOVE_PENDING;
  1900. cpus_clear(desc->pending_mask);
  1901. unmask:
  1902. unmask_IO_APIC_irq(irq);
  1903. return ret;
  1904. }
  1905. static void ir_irq_migration(struct work_struct *work)
  1906. {
  1907. unsigned int irq;
  1908. struct irq_desc *desc;
  1909. for_each_irq_desc(irq, desc) {
  1910. if (!desc)
  1911. continue;
  1912. if (desc->status & IRQ_MOVE_PENDING) {
  1913. unsigned long flags;
  1914. spin_lock_irqsave(&desc->lock, flags);
  1915. if (!desc->chip->set_affinity ||
  1916. !(desc->status & IRQ_MOVE_PENDING)) {
  1917. desc->status &= ~IRQ_MOVE_PENDING;
  1918. spin_unlock_irqrestore(&desc->lock, flags);
  1919. continue;
  1920. }
  1921. desc->chip->set_affinity(irq, desc->pending_mask);
  1922. spin_unlock_irqrestore(&desc->lock, flags);
  1923. }
  1924. }
  1925. }
  1926. /*
  1927. * Migrates the IRQ destination in the process context.
  1928. */
  1929. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1930. {
  1931. struct irq_desc *desc = irq_to_desc(irq);
  1932. if (desc->status & IRQ_LEVEL) {
  1933. desc->status |= IRQ_MOVE_PENDING;
  1934. desc->pending_mask = mask;
  1935. migrate_irq_remapped_level(irq);
  1936. return;
  1937. }
  1938. migrate_ioapic_irq(irq, mask);
  1939. }
  1940. #endif
  1941. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1942. {
  1943. unsigned vector, me;
  1944. ack_APIC_irq();
  1945. #ifdef CONFIG_X86_64
  1946. exit_idle();
  1947. #endif
  1948. irq_enter();
  1949. me = smp_processor_id();
  1950. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1951. unsigned int irq;
  1952. struct irq_desc *desc;
  1953. struct irq_cfg *cfg;
  1954. irq = __get_cpu_var(vector_irq)[vector];
  1955. if (irq == -1)
  1956. continue;
  1957. desc = irq_to_desc(irq);
  1958. if (!desc)
  1959. continue;
  1960. cfg = irq_cfg(irq);
  1961. spin_lock(&desc->lock);
  1962. if (!cfg->move_cleanup_count)
  1963. goto unlock;
  1964. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1965. goto unlock;
  1966. __get_cpu_var(vector_irq)[vector] = -1;
  1967. cfg->move_cleanup_count--;
  1968. unlock:
  1969. spin_unlock(&desc->lock);
  1970. }
  1971. irq_exit();
  1972. }
  1973. static void irq_complete_move(unsigned int irq)
  1974. {
  1975. struct irq_cfg *cfg = irq_cfg(irq);
  1976. unsigned vector, me;
  1977. if (likely(!cfg->move_in_progress))
  1978. return;
  1979. vector = ~get_irq_regs()->orig_ax;
  1980. me = smp_processor_id();
  1981. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1982. cpumask_t cleanup_mask;
  1983. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1984. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1985. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1986. cfg->move_in_progress = 0;
  1987. }
  1988. }
  1989. #else
  1990. static inline void irq_complete_move(unsigned int irq) {}
  1991. #endif
  1992. #ifdef CONFIG_INTR_REMAP
  1993. static void ack_x2apic_level(unsigned int irq)
  1994. {
  1995. ack_x2APIC_irq();
  1996. }
  1997. static void ack_x2apic_edge(unsigned int irq)
  1998. {
  1999. ack_x2APIC_irq();
  2000. }
  2001. #endif
  2002. static void ack_apic_edge(unsigned int irq)
  2003. {
  2004. irq_complete_move(irq);
  2005. move_native_irq(irq);
  2006. ack_APIC_irq();
  2007. }
  2008. atomic_t irq_mis_count;
  2009. static void ack_apic_level(unsigned int irq)
  2010. {
  2011. #ifdef CONFIG_X86_32
  2012. unsigned long v;
  2013. int i;
  2014. #endif
  2015. int do_unmask_irq = 0;
  2016. irq_complete_move(irq);
  2017. #ifdef CONFIG_GENERIC_PENDING_IRQ
  2018. /* If we are moving the irq we need to mask it */
  2019. if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
  2020. do_unmask_irq = 1;
  2021. mask_IO_APIC_irq(irq);
  2022. }
  2023. #endif
  2024. #ifdef CONFIG_X86_32
  2025. /*
  2026. * It appears there is an erratum which affects at least version 0x11
  2027. * of I/O APIC (that's the 82093AA and cores integrated into various
  2028. * chipsets). Under certain conditions a level-triggered interrupt is
  2029. * erroneously delivered as edge-triggered one but the respective IRR
  2030. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  2031. * message but it will never arrive and further interrupts are blocked
  2032. * from the source. The exact reason is so far unknown, but the
  2033. * phenomenon was observed when two consecutive interrupt requests
  2034. * from a given source get delivered to the same CPU and the source is
  2035. * temporarily disabled in between.
  2036. *
  2037. * A workaround is to simulate an EOI message manually. We achieve it
  2038. * by setting the trigger mode to edge and then to level when the edge
  2039. * trigger mode gets detected in the TMR of a local APIC for a
  2040. * level-triggered interrupt. We mask the source for the time of the
  2041. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  2042. * The idea is from Manfred Spraul. --macro
  2043. */
  2044. i = irq_cfg(irq)->vector;
  2045. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  2046. #endif
  2047. /*
  2048. * We must acknowledge the irq before we move it or the acknowledge will
  2049. * not propagate properly.
  2050. */
  2051. ack_APIC_irq();
  2052. /* Now we can move and renable the irq */
  2053. if (unlikely(do_unmask_irq)) {
  2054. /* Only migrate the irq if the ack has been received.
  2055. *
  2056. * On rare occasions the broadcast level triggered ack gets
  2057. * delayed going to ioapics, and if we reprogram the
  2058. * vector while Remote IRR is still set the irq will never
  2059. * fire again.
  2060. *
  2061. * To prevent this scenario we read the Remote IRR bit
  2062. * of the ioapic. This has two effects.
  2063. * - On any sane system the read of the ioapic will
  2064. * flush writes (and acks) going to the ioapic from
  2065. * this cpu.
  2066. * - We get to see if the ACK has actually been delivered.
  2067. *
  2068. * Based on failed experiments of reprogramming the
  2069. * ioapic entry from outside of irq context starting
  2070. * with masking the ioapic entry and then polling until
  2071. * Remote IRR was clear before reprogramming the
  2072. * ioapic I don't trust the Remote IRR bit to be
  2073. * completey accurate.
  2074. *
  2075. * However there appears to be no other way to plug
  2076. * this race, so if the Remote IRR bit is not
  2077. * accurate and is causing problems then it is a hardware bug
  2078. * and you can go talk to the chipset vendor about it.
  2079. */
  2080. if (!io_apic_level_ack_pending(irq))
  2081. move_masked_irq(irq);
  2082. unmask_IO_APIC_irq(irq);
  2083. }
  2084. #ifdef CONFIG_X86_32
  2085. if (!(v & (1 << (i & 0x1f)))) {
  2086. atomic_inc(&irq_mis_count);
  2087. spin_lock(&ioapic_lock);
  2088. __mask_and_edge_IO_APIC_irq(irq);
  2089. __unmask_and_level_IO_APIC_irq(irq);
  2090. spin_unlock(&ioapic_lock);
  2091. }
  2092. #endif
  2093. }
  2094. static struct irq_chip ioapic_chip __read_mostly = {
  2095. .name = "IO-APIC",
  2096. .startup = startup_ioapic_irq,
  2097. .mask = mask_IO_APIC_irq,
  2098. .unmask = unmask_IO_APIC_irq,
  2099. .ack = ack_apic_edge,
  2100. .eoi = ack_apic_level,
  2101. #ifdef CONFIG_SMP
  2102. .set_affinity = set_ioapic_affinity_irq,
  2103. #endif
  2104. .retrigger = ioapic_retrigger_irq,
  2105. };
  2106. #ifdef CONFIG_INTR_REMAP
  2107. static struct irq_chip ir_ioapic_chip __read_mostly = {
  2108. .name = "IR-IO-APIC",
  2109. .startup = startup_ioapic_irq,
  2110. .mask = mask_IO_APIC_irq,
  2111. .unmask = unmask_IO_APIC_irq,
  2112. .ack = ack_x2apic_edge,
  2113. .eoi = ack_x2apic_level,
  2114. #ifdef CONFIG_SMP
  2115. .set_affinity = set_ir_ioapic_affinity_irq,
  2116. #endif
  2117. .retrigger = ioapic_retrigger_irq,
  2118. };
  2119. #endif
  2120. static inline void init_IO_APIC_traps(void)
  2121. {
  2122. int irq;
  2123. struct irq_desc *desc;
  2124. struct irq_cfg *cfg;
  2125. /*
  2126. * NOTE! The local APIC isn't very good at handling
  2127. * multiple interrupts at the same interrupt level.
  2128. * As the interrupt level is determined by taking the
  2129. * vector number and shifting that right by 4, we
  2130. * want to spread these out a bit so that they don't
  2131. * all fall in the same interrupt level.
  2132. *
  2133. * Also, we've got to be careful not to trash gate
  2134. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  2135. */
  2136. for_each_irq_desc(irq, desc) {
  2137. if (!desc)
  2138. continue;
  2139. cfg = desc->chip_data;
  2140. if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
  2141. /*
  2142. * Hmm.. We don't have an entry for this,
  2143. * so default to an old-fashioned 8259
  2144. * interrupt if we can..
  2145. */
  2146. if (irq < 16)
  2147. make_8259A_irq(irq);
  2148. else
  2149. /* Strange. Oh, well.. */
  2150. desc->chip = &no_irq_chip;
  2151. }
  2152. }
  2153. }
  2154. /*
  2155. * The local APIC irq-chip implementation:
  2156. */
  2157. static void mask_lapic_irq(unsigned int irq)
  2158. {
  2159. unsigned long v;
  2160. v = apic_read(APIC_LVT0);
  2161. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  2162. }
  2163. static void unmask_lapic_irq(unsigned int irq)
  2164. {
  2165. unsigned long v;
  2166. v = apic_read(APIC_LVT0);
  2167. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  2168. }
  2169. static void ack_lapic_irq (unsigned int irq)
  2170. {
  2171. ack_APIC_irq();
  2172. }
  2173. static struct irq_chip lapic_chip __read_mostly = {
  2174. .name = "local-APIC",
  2175. .mask = mask_lapic_irq,
  2176. .unmask = unmask_lapic_irq,
  2177. .ack = ack_lapic_irq,
  2178. };
  2179. static void lapic_register_intr(int irq)
  2180. {
  2181. struct irq_desc *desc;
  2182. desc = irq_to_desc(irq);
  2183. desc->status &= ~IRQ_LEVEL;
  2184. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  2185. "edge");
  2186. }
  2187. static void __init setup_nmi(void)
  2188. {
  2189. /*
  2190. * Dirty trick to enable the NMI watchdog ...
  2191. * We put the 8259A master into AEOI mode and
  2192. * unmask on all local APICs LVT0 as NMI.
  2193. *
  2194. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  2195. * is from Maciej W. Rozycki - so we do not have to EOI from
  2196. * the NMI handler or the timer interrupt.
  2197. */
  2198. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  2199. enable_NMI_through_LVT0();
  2200. apic_printk(APIC_VERBOSE, " done.\n");
  2201. }
  2202. /*
  2203. * This looks a bit hackish but it's about the only one way of sending
  2204. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  2205. * not support the ExtINT mode, unfortunately. We need to send these
  2206. * cycles as some i82489DX-based boards have glue logic that keeps the
  2207. * 8259A interrupt line asserted until INTA. --macro
  2208. */
  2209. static inline void __init unlock_ExtINT_logic(void)
  2210. {
  2211. int apic, pin, i;
  2212. struct IO_APIC_route_entry entry0, entry1;
  2213. unsigned char save_control, save_freq_select;
  2214. pin = find_isa_irq_pin(8, mp_INT);
  2215. if (pin == -1) {
  2216. WARN_ON_ONCE(1);
  2217. return;
  2218. }
  2219. apic = find_isa_irq_apic(8, mp_INT);
  2220. if (apic == -1) {
  2221. WARN_ON_ONCE(1);
  2222. return;
  2223. }
  2224. entry0 = ioapic_read_entry(apic, pin);
  2225. clear_IO_APIC_pin(apic, pin);
  2226. memset(&entry1, 0, sizeof(entry1));
  2227. entry1.dest_mode = 0; /* physical delivery */
  2228. entry1.mask = 0; /* unmask IRQ now */
  2229. entry1.dest = hard_smp_processor_id();
  2230. entry1.delivery_mode = dest_ExtINT;
  2231. entry1.polarity = entry0.polarity;
  2232. entry1.trigger = 0;
  2233. entry1.vector = 0;
  2234. ioapic_write_entry(apic, pin, entry1);
  2235. save_control = CMOS_READ(RTC_CONTROL);
  2236. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  2237. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  2238. RTC_FREQ_SELECT);
  2239. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  2240. i = 100;
  2241. while (i-- > 0) {
  2242. mdelay(10);
  2243. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  2244. i -= 10;
  2245. }
  2246. CMOS_WRITE(save_control, RTC_CONTROL);
  2247. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  2248. clear_IO_APIC_pin(apic, pin);
  2249. ioapic_write_entry(apic, pin, entry0);
  2250. }
  2251. static int disable_timer_pin_1 __initdata;
  2252. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  2253. static int __init disable_timer_pin_setup(char *arg)
  2254. {
  2255. disable_timer_pin_1 = 1;
  2256. return 0;
  2257. }
  2258. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  2259. int timer_through_8259 __initdata;
  2260. /*
  2261. * This code may look a bit paranoid, but it's supposed to cooperate with
  2262. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  2263. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  2264. * fanatically on his truly buggy board.
  2265. *
  2266. * FIXME: really need to revamp this for all platforms.
  2267. */
  2268. static inline void __init check_timer(void)
  2269. {
  2270. struct irq_cfg *cfg = irq_cfg(0);
  2271. int apic1, pin1, apic2, pin2;
  2272. unsigned long flags;
  2273. unsigned int ver;
  2274. int no_pin1 = 0;
  2275. local_irq_save(flags);
  2276. ver = apic_read(APIC_LVR);
  2277. ver = GET_APIC_VERSION(ver);
  2278. /*
  2279. * get/set the timer IRQ vector:
  2280. */
  2281. disable_8259A_irq(0);
  2282. assign_irq_vector(0, TARGET_CPUS);
  2283. /*
  2284. * As IRQ0 is to be enabled in the 8259A, the virtual
  2285. * wire has to be disabled in the local APIC. Also
  2286. * timer interrupts need to be acknowledged manually in
  2287. * the 8259A for the i82489DX when using the NMI
  2288. * watchdog as that APIC treats NMIs as level-triggered.
  2289. * The AEOI mode will finish them in the 8259A
  2290. * automatically.
  2291. */
  2292. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  2293. init_8259A(1);
  2294. #ifdef CONFIG_X86_32
  2295. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  2296. #endif
  2297. pin1 = find_isa_irq_pin(0, mp_INT);
  2298. apic1 = find_isa_irq_apic(0, mp_INT);
  2299. pin2 = ioapic_i8259.pin;
  2300. apic2 = ioapic_i8259.apic;
  2301. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  2302. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  2303. cfg->vector, apic1, pin1, apic2, pin2);
  2304. /*
  2305. * Some BIOS writers are clueless and report the ExtINTA
  2306. * I/O APIC input from the cascaded 8259A as the timer
  2307. * interrupt input. So just in case, if only one pin
  2308. * was found above, try it both directly and through the
  2309. * 8259A.
  2310. */
  2311. if (pin1 == -1) {
  2312. #ifdef CONFIG_INTR_REMAP
  2313. if (intr_remapping_enabled)
  2314. panic("BIOS bug: timer not connected to IO-APIC");
  2315. #endif
  2316. pin1 = pin2;
  2317. apic1 = apic2;
  2318. no_pin1 = 1;
  2319. } else if (pin2 == -1) {
  2320. pin2 = pin1;
  2321. apic2 = apic1;
  2322. }
  2323. if (pin1 != -1) {
  2324. /*
  2325. * Ok, does IRQ0 through the IOAPIC work?
  2326. */
  2327. if (no_pin1) {
  2328. add_pin_to_irq_cpu(0, boot_cpu_id, apic1, pin1);
  2329. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  2330. }
  2331. unmask_IO_APIC_irq(0);
  2332. if (timer_irq_works()) {
  2333. if (nmi_watchdog == NMI_IO_APIC) {
  2334. setup_nmi();
  2335. enable_8259A_irq(0);
  2336. }
  2337. if (disable_timer_pin_1 > 0)
  2338. clear_IO_APIC_pin(0, pin1);
  2339. goto out;
  2340. }
  2341. #ifdef CONFIG_INTR_REMAP
  2342. if (intr_remapping_enabled)
  2343. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  2344. #endif
  2345. clear_IO_APIC_pin(apic1, pin1);
  2346. if (!no_pin1)
  2347. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  2348. "8254 timer not connected to IO-APIC\n");
  2349. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  2350. "(IRQ0) through the 8259A ...\n");
  2351. apic_printk(APIC_QUIET, KERN_INFO
  2352. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  2353. /*
  2354. * legacy devices should be connected to IO APIC #0
  2355. */
  2356. replace_pin_at_irq(0, boot_cpu_id, apic1, pin1, apic2, pin2);
  2357. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  2358. unmask_IO_APIC_irq(0);
  2359. enable_8259A_irq(0);
  2360. if (timer_irq_works()) {
  2361. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  2362. timer_through_8259 = 1;
  2363. if (nmi_watchdog == NMI_IO_APIC) {
  2364. disable_8259A_irq(0);
  2365. setup_nmi();
  2366. enable_8259A_irq(0);
  2367. }
  2368. goto out;
  2369. }
  2370. /*
  2371. * Cleanup, just in case ...
  2372. */
  2373. disable_8259A_irq(0);
  2374. clear_IO_APIC_pin(apic2, pin2);
  2375. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  2376. }
  2377. if (nmi_watchdog == NMI_IO_APIC) {
  2378. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  2379. "through the IO-APIC - disabling NMI Watchdog!\n");
  2380. nmi_watchdog = NMI_NONE;
  2381. }
  2382. #ifdef CONFIG_X86_32
  2383. timer_ack = 0;
  2384. #endif
  2385. apic_printk(APIC_QUIET, KERN_INFO
  2386. "...trying to set up timer as Virtual Wire IRQ...\n");
  2387. lapic_register_intr(0);
  2388. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  2389. enable_8259A_irq(0);
  2390. if (timer_irq_works()) {
  2391. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2392. goto out;
  2393. }
  2394. disable_8259A_irq(0);
  2395. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  2396. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  2397. apic_printk(APIC_QUIET, KERN_INFO
  2398. "...trying to set up timer as ExtINT IRQ...\n");
  2399. init_8259A(0);
  2400. make_8259A_irq(0);
  2401. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  2402. unlock_ExtINT_logic();
  2403. if (timer_irq_works()) {
  2404. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2405. goto out;
  2406. }
  2407. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  2408. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  2409. "report. Then try booting with the 'noapic' option.\n");
  2410. out:
  2411. local_irq_restore(flags);
  2412. }
  2413. /*
  2414. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  2415. * to devices. However there may be an I/O APIC pin available for
  2416. * this interrupt regardless. The pin may be left unconnected, but
  2417. * typically it will be reused as an ExtINT cascade interrupt for
  2418. * the master 8259A. In the MPS case such a pin will normally be
  2419. * reported as an ExtINT interrupt in the MP table. With ACPI
  2420. * there is no provision for ExtINT interrupts, and in the absence
  2421. * of an override it would be treated as an ordinary ISA I/O APIC
  2422. * interrupt, that is edge-triggered and unmasked by default. We
  2423. * used to do this, but it caused problems on some systems because
  2424. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2425. * the same ExtINT cascade interrupt to drive the local APIC of the
  2426. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2427. * the I/O APIC in all cases now. No actual device should request
  2428. * it anyway. --macro
  2429. */
  2430. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  2431. void __init setup_IO_APIC(void)
  2432. {
  2433. #ifdef CONFIG_X86_32
  2434. enable_IO_APIC();
  2435. #else
  2436. /*
  2437. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2438. */
  2439. #endif
  2440. io_apic_irqs = ~PIC_IRQS;
  2441. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2442. /*
  2443. * Set up IO-APIC IRQ routing.
  2444. */
  2445. #ifdef CONFIG_X86_32
  2446. if (!acpi_ioapic)
  2447. setup_ioapic_ids_from_mpc();
  2448. #endif
  2449. sync_Arb_IDs();
  2450. setup_IO_APIC_irqs();
  2451. init_IO_APIC_traps();
  2452. check_timer();
  2453. }
  2454. /*
  2455. * Called after all the initialization is done. If we didnt find any
  2456. * APIC bugs then we can allow the modify fast path
  2457. */
  2458. static int __init io_apic_bug_finalize(void)
  2459. {
  2460. if (sis_apic_bug == -1)
  2461. sis_apic_bug = 0;
  2462. return 0;
  2463. }
  2464. late_initcall(io_apic_bug_finalize);
  2465. struct sysfs_ioapic_data {
  2466. struct sys_device dev;
  2467. struct IO_APIC_route_entry entry[0];
  2468. };
  2469. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  2470. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2471. {
  2472. struct IO_APIC_route_entry *entry;
  2473. struct sysfs_ioapic_data *data;
  2474. int i;
  2475. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2476. entry = data->entry;
  2477. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  2478. *entry = ioapic_read_entry(dev->id, i);
  2479. return 0;
  2480. }
  2481. static int ioapic_resume(struct sys_device *dev)
  2482. {
  2483. struct IO_APIC_route_entry *entry;
  2484. struct sysfs_ioapic_data *data;
  2485. unsigned long flags;
  2486. union IO_APIC_reg_00 reg_00;
  2487. int i;
  2488. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2489. entry = data->entry;
  2490. spin_lock_irqsave(&ioapic_lock, flags);
  2491. reg_00.raw = io_apic_read(dev->id, 0);
  2492. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  2493. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  2494. io_apic_write(dev->id, 0, reg_00.raw);
  2495. }
  2496. spin_unlock_irqrestore(&ioapic_lock, flags);
  2497. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2498. ioapic_write_entry(dev->id, i, entry[i]);
  2499. return 0;
  2500. }
  2501. static struct sysdev_class ioapic_sysdev_class = {
  2502. .name = "ioapic",
  2503. .suspend = ioapic_suspend,
  2504. .resume = ioapic_resume,
  2505. };
  2506. static int __init ioapic_init_sysfs(void)
  2507. {
  2508. struct sys_device * dev;
  2509. int i, size, error;
  2510. error = sysdev_class_register(&ioapic_sysdev_class);
  2511. if (error)
  2512. return error;
  2513. for (i = 0; i < nr_ioapics; i++ ) {
  2514. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2515. * sizeof(struct IO_APIC_route_entry);
  2516. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2517. if (!mp_ioapic_data[i]) {
  2518. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2519. continue;
  2520. }
  2521. dev = &mp_ioapic_data[i]->dev;
  2522. dev->id = i;
  2523. dev->cls = &ioapic_sysdev_class;
  2524. error = sysdev_register(dev);
  2525. if (error) {
  2526. kfree(mp_ioapic_data[i]);
  2527. mp_ioapic_data[i] = NULL;
  2528. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2529. continue;
  2530. }
  2531. }
  2532. return 0;
  2533. }
  2534. device_initcall(ioapic_init_sysfs);
  2535. /*
  2536. * Dynamic irq allocate and deallocation
  2537. */
  2538. unsigned int create_irq_nr(unsigned int irq_want)
  2539. {
  2540. /* Allocate an unused irq */
  2541. unsigned int irq;
  2542. unsigned int new;
  2543. unsigned long flags;
  2544. struct irq_cfg *cfg_new = NULL;
  2545. int cpu = boot_cpu_id;
  2546. struct irq_desc *desc_new = NULL;
  2547. irq = 0;
  2548. spin_lock_irqsave(&vector_lock, flags);
  2549. for (new = irq_want; new > 0; new--) {
  2550. if (platform_legacy_irq(new))
  2551. continue;
  2552. desc_new = irq_to_desc_alloc_cpu(new, cpu);
  2553. if (!desc_new) {
  2554. printk(KERN_INFO "can not get irq_desc for %d\n", new);
  2555. continue;
  2556. }
  2557. cfg_new = desc_new->chip_data;
  2558. if (cfg_new->vector != 0)
  2559. continue;
  2560. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  2561. irq = new;
  2562. break;
  2563. }
  2564. spin_unlock_irqrestore(&vector_lock, flags);
  2565. if (irq > 0) {
  2566. dynamic_irq_init(irq);
  2567. /* restore it, in case dynamic_irq_init clear it */
  2568. if (desc_new)
  2569. desc_new->chip_data = cfg_new;
  2570. }
  2571. return irq;
  2572. }
  2573. int create_irq(void)
  2574. {
  2575. int irq;
  2576. irq = create_irq_nr(nr_irqs - 1);
  2577. if (irq == 0)
  2578. irq = -1;
  2579. return irq;
  2580. }
  2581. void destroy_irq(unsigned int irq)
  2582. {
  2583. unsigned long flags;
  2584. struct irq_cfg *cfg;
  2585. struct irq_desc *desc;
  2586. /* store it, in case dynamic_irq_cleanup clear it */
  2587. desc = irq_to_desc(irq);
  2588. cfg = desc->chip_data;
  2589. dynamic_irq_cleanup(irq);
  2590. /* connect back irq_cfg */
  2591. if (desc)
  2592. desc->chip_data = cfg;
  2593. #ifdef CONFIG_INTR_REMAP
  2594. free_irte(irq);
  2595. #endif
  2596. spin_lock_irqsave(&vector_lock, flags);
  2597. __clear_irq_vector(irq);
  2598. spin_unlock_irqrestore(&vector_lock, flags);
  2599. }
  2600. /*
  2601. * MSI message composition
  2602. */
  2603. #ifdef CONFIG_PCI_MSI
  2604. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2605. {
  2606. struct irq_cfg *cfg;
  2607. int err;
  2608. unsigned dest;
  2609. cpumask_t tmp;
  2610. tmp = TARGET_CPUS;
  2611. err = assign_irq_vector(irq, tmp);
  2612. if (err)
  2613. return err;
  2614. cfg = irq_cfg(irq);
  2615. cpus_and(tmp, cfg->domain, tmp);
  2616. dest = cpu_mask_to_apicid(tmp);
  2617. #ifdef CONFIG_INTR_REMAP
  2618. if (irq_remapped(irq)) {
  2619. struct irte irte;
  2620. int ir_index;
  2621. u16 sub_handle;
  2622. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  2623. BUG_ON(ir_index == -1);
  2624. memset (&irte, 0, sizeof(irte));
  2625. irte.present = 1;
  2626. irte.dst_mode = INT_DEST_MODE;
  2627. irte.trigger_mode = 0; /* edge */
  2628. irte.dlvry_mode = INT_DELIVERY_MODE;
  2629. irte.vector = cfg->vector;
  2630. irte.dest_id = IRTE_DEST(dest);
  2631. modify_irte(irq, &irte);
  2632. msg->address_hi = MSI_ADDR_BASE_HI;
  2633. msg->data = sub_handle;
  2634. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  2635. MSI_ADDR_IR_SHV |
  2636. MSI_ADDR_IR_INDEX1(ir_index) |
  2637. MSI_ADDR_IR_INDEX2(ir_index);
  2638. } else
  2639. #endif
  2640. {
  2641. msg->address_hi = MSI_ADDR_BASE_HI;
  2642. msg->address_lo =
  2643. MSI_ADDR_BASE_LO |
  2644. ((INT_DEST_MODE == 0) ?
  2645. MSI_ADDR_DEST_MODE_PHYSICAL:
  2646. MSI_ADDR_DEST_MODE_LOGICAL) |
  2647. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2648. MSI_ADDR_REDIRECTION_CPU:
  2649. MSI_ADDR_REDIRECTION_LOWPRI) |
  2650. MSI_ADDR_DEST_ID(dest);
  2651. msg->data =
  2652. MSI_DATA_TRIGGER_EDGE |
  2653. MSI_DATA_LEVEL_ASSERT |
  2654. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2655. MSI_DATA_DELIVERY_FIXED:
  2656. MSI_DATA_DELIVERY_LOWPRI) |
  2657. MSI_DATA_VECTOR(cfg->vector);
  2658. }
  2659. return err;
  2660. }
  2661. #ifdef CONFIG_SMP
  2662. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2663. {
  2664. struct irq_cfg *cfg;
  2665. struct msi_msg msg;
  2666. unsigned int dest;
  2667. cpumask_t tmp;
  2668. struct irq_desc *desc;
  2669. cpus_and(tmp, mask, cpu_online_map);
  2670. if (cpus_empty(tmp))
  2671. return;
  2672. if (assign_irq_vector(irq, mask))
  2673. return;
  2674. cfg = irq_cfg(irq);
  2675. cpus_and(tmp, cfg->domain, mask);
  2676. dest = cpu_mask_to_apicid(tmp);
  2677. read_msi_msg(irq, &msg);
  2678. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2679. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2680. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2681. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2682. write_msi_msg(irq, &msg);
  2683. desc = irq_to_desc(irq);
  2684. desc->affinity = mask;
  2685. }
  2686. #ifdef CONFIG_INTR_REMAP
  2687. /*
  2688. * Migrate the MSI irq to another cpumask. This migration is
  2689. * done in the process context using interrupt-remapping hardware.
  2690. */
  2691. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2692. {
  2693. struct irq_cfg *cfg;
  2694. unsigned int dest;
  2695. cpumask_t tmp, cleanup_mask;
  2696. struct irte irte;
  2697. struct irq_desc *desc;
  2698. cpus_and(tmp, mask, cpu_online_map);
  2699. if (cpus_empty(tmp))
  2700. return;
  2701. if (get_irte(irq, &irte))
  2702. return;
  2703. if (assign_irq_vector(irq, mask))
  2704. return;
  2705. cfg = irq_cfg(irq);
  2706. cpus_and(tmp, cfg->domain, mask);
  2707. dest = cpu_mask_to_apicid(tmp);
  2708. irte.vector = cfg->vector;
  2709. irte.dest_id = IRTE_DEST(dest);
  2710. /*
  2711. * atomically update the IRTE with the new destination and vector.
  2712. */
  2713. modify_irte(irq, &irte);
  2714. /*
  2715. * After this point, all the interrupts will start arriving
  2716. * at the new destination. So, time to cleanup the previous
  2717. * vector allocation.
  2718. */
  2719. if (cfg->move_in_progress) {
  2720. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2721. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2722. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2723. cfg->move_in_progress = 0;
  2724. }
  2725. desc = irq_to_desc(irq);
  2726. desc->affinity = mask;
  2727. }
  2728. #endif
  2729. #endif /* CONFIG_SMP */
  2730. /*
  2731. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2732. * which implement the MSI or MSI-X Capability Structure.
  2733. */
  2734. static struct irq_chip msi_chip = {
  2735. .name = "PCI-MSI",
  2736. .unmask = unmask_msi_irq,
  2737. .mask = mask_msi_irq,
  2738. .ack = ack_apic_edge,
  2739. #ifdef CONFIG_SMP
  2740. .set_affinity = set_msi_irq_affinity,
  2741. #endif
  2742. .retrigger = ioapic_retrigger_irq,
  2743. };
  2744. #ifdef CONFIG_INTR_REMAP
  2745. static struct irq_chip msi_ir_chip = {
  2746. .name = "IR-PCI-MSI",
  2747. .unmask = unmask_msi_irq,
  2748. .mask = mask_msi_irq,
  2749. .ack = ack_x2apic_edge,
  2750. #ifdef CONFIG_SMP
  2751. .set_affinity = ir_set_msi_irq_affinity,
  2752. #endif
  2753. .retrigger = ioapic_retrigger_irq,
  2754. };
  2755. /*
  2756. * Map the PCI dev to the corresponding remapping hardware unit
  2757. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2758. * in it.
  2759. */
  2760. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2761. {
  2762. struct intel_iommu *iommu;
  2763. int index;
  2764. iommu = map_dev_to_ir(dev);
  2765. if (!iommu) {
  2766. printk(KERN_ERR
  2767. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2768. return -ENOENT;
  2769. }
  2770. index = alloc_irte(iommu, irq, nvec);
  2771. if (index < 0) {
  2772. printk(KERN_ERR
  2773. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2774. pci_name(dev));
  2775. return -ENOSPC;
  2776. }
  2777. return index;
  2778. }
  2779. #endif
  2780. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
  2781. {
  2782. int ret;
  2783. struct msi_msg msg;
  2784. ret = msi_compose_msg(dev, irq, &msg);
  2785. if (ret < 0)
  2786. return ret;
  2787. set_irq_msi(irq, desc);
  2788. write_msi_msg(irq, &msg);
  2789. #ifdef CONFIG_INTR_REMAP
  2790. if (irq_remapped(irq)) {
  2791. struct irq_desc *desc = irq_to_desc(irq);
  2792. /*
  2793. * irq migration in process context
  2794. */
  2795. desc->status |= IRQ_MOVE_PCNTXT;
  2796. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2797. } else
  2798. #endif
  2799. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2800. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
  2801. return 0;
  2802. }
  2803. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
  2804. {
  2805. unsigned int irq;
  2806. int ret;
  2807. unsigned int irq_want;
  2808. irq_want = nr_irqs - 1;
  2809. irq = create_irq_nr(irq_want);
  2810. if (irq == 0)
  2811. return -1;
  2812. #ifdef CONFIG_INTR_REMAP
  2813. if (!intr_remapping_enabled)
  2814. goto no_ir;
  2815. ret = msi_alloc_irte(dev, irq, 1);
  2816. if (ret < 0)
  2817. goto error;
  2818. no_ir:
  2819. #endif
  2820. ret = setup_msi_irq(dev, msidesc, irq);
  2821. if (ret < 0) {
  2822. destroy_irq(irq);
  2823. return ret;
  2824. }
  2825. return 0;
  2826. #ifdef CONFIG_INTR_REMAP
  2827. error:
  2828. destroy_irq(irq);
  2829. return ret;
  2830. #endif
  2831. }
  2832. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2833. {
  2834. unsigned int irq;
  2835. int ret, sub_handle;
  2836. struct msi_desc *msidesc;
  2837. unsigned int irq_want;
  2838. #ifdef CONFIG_INTR_REMAP
  2839. struct intel_iommu *iommu = 0;
  2840. int index = 0;
  2841. #endif
  2842. irq_want = nr_irqs - 1;
  2843. sub_handle = 0;
  2844. list_for_each_entry(msidesc, &dev->msi_list, list) {
  2845. irq = create_irq_nr(irq_want);
  2846. irq_want--;
  2847. if (irq == 0)
  2848. return -1;
  2849. #ifdef CONFIG_INTR_REMAP
  2850. if (!intr_remapping_enabled)
  2851. goto no_ir;
  2852. if (!sub_handle) {
  2853. /*
  2854. * allocate the consecutive block of IRTE's
  2855. * for 'nvec'
  2856. */
  2857. index = msi_alloc_irte(dev, irq, nvec);
  2858. if (index < 0) {
  2859. ret = index;
  2860. goto error;
  2861. }
  2862. } else {
  2863. iommu = map_dev_to_ir(dev);
  2864. if (!iommu) {
  2865. ret = -ENOENT;
  2866. goto error;
  2867. }
  2868. /*
  2869. * setup the mapping between the irq and the IRTE
  2870. * base index, the sub_handle pointing to the
  2871. * appropriate interrupt remap table entry.
  2872. */
  2873. set_irte_irq(irq, iommu, index, sub_handle);
  2874. }
  2875. no_ir:
  2876. #endif
  2877. ret = setup_msi_irq(dev, msidesc, irq);
  2878. if (ret < 0)
  2879. goto error;
  2880. sub_handle++;
  2881. }
  2882. return 0;
  2883. error:
  2884. destroy_irq(irq);
  2885. return ret;
  2886. }
  2887. void arch_teardown_msi_irq(unsigned int irq)
  2888. {
  2889. destroy_irq(irq);
  2890. }
  2891. #ifdef CONFIG_DMAR
  2892. #ifdef CONFIG_SMP
  2893. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2894. {
  2895. struct irq_cfg *cfg;
  2896. struct msi_msg msg;
  2897. unsigned int dest;
  2898. cpumask_t tmp;
  2899. struct irq_desc *desc;
  2900. cpus_and(tmp, mask, cpu_online_map);
  2901. if (cpus_empty(tmp))
  2902. return;
  2903. if (assign_irq_vector(irq, mask))
  2904. return;
  2905. cfg = irq_cfg(irq);
  2906. cpus_and(tmp, cfg->domain, mask);
  2907. dest = cpu_mask_to_apicid(tmp);
  2908. dmar_msi_read(irq, &msg);
  2909. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2910. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2911. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2912. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2913. dmar_msi_write(irq, &msg);
  2914. desc = irq_to_desc(irq);
  2915. desc->affinity = mask;
  2916. }
  2917. #endif /* CONFIG_SMP */
  2918. struct irq_chip dmar_msi_type = {
  2919. .name = "DMAR_MSI",
  2920. .unmask = dmar_msi_unmask,
  2921. .mask = dmar_msi_mask,
  2922. .ack = ack_apic_edge,
  2923. #ifdef CONFIG_SMP
  2924. .set_affinity = dmar_msi_set_affinity,
  2925. #endif
  2926. .retrigger = ioapic_retrigger_irq,
  2927. };
  2928. int arch_setup_dmar_msi(unsigned int irq)
  2929. {
  2930. int ret;
  2931. struct msi_msg msg;
  2932. ret = msi_compose_msg(NULL, irq, &msg);
  2933. if (ret < 0)
  2934. return ret;
  2935. dmar_msi_write(irq, &msg);
  2936. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2937. "edge");
  2938. return 0;
  2939. }
  2940. #endif
  2941. #ifdef CONFIG_HPET_TIMER
  2942. #ifdef CONFIG_SMP
  2943. static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2944. {
  2945. struct irq_cfg *cfg;
  2946. struct irq_desc *desc;
  2947. struct msi_msg msg;
  2948. unsigned int dest;
  2949. cpumask_t tmp;
  2950. cpus_and(tmp, mask, cpu_online_map);
  2951. if (cpus_empty(tmp))
  2952. return;
  2953. if (assign_irq_vector(irq, mask))
  2954. return;
  2955. cfg = irq_cfg(irq);
  2956. cpus_and(tmp, cfg->domain, mask);
  2957. dest = cpu_mask_to_apicid(tmp);
  2958. hpet_msi_read(irq, &msg);
  2959. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2960. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2961. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2962. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2963. hpet_msi_write(irq, &msg);
  2964. desc = irq_to_desc(irq);
  2965. desc->affinity = mask;
  2966. }
  2967. #endif /* CONFIG_SMP */
  2968. struct irq_chip hpet_msi_type = {
  2969. .name = "HPET_MSI",
  2970. .unmask = hpet_msi_unmask,
  2971. .mask = hpet_msi_mask,
  2972. .ack = ack_apic_edge,
  2973. #ifdef CONFIG_SMP
  2974. .set_affinity = hpet_msi_set_affinity,
  2975. #endif
  2976. .retrigger = ioapic_retrigger_irq,
  2977. };
  2978. int arch_setup_hpet_msi(unsigned int irq)
  2979. {
  2980. int ret;
  2981. struct msi_msg msg;
  2982. ret = msi_compose_msg(NULL, irq, &msg);
  2983. if (ret < 0)
  2984. return ret;
  2985. hpet_msi_write(irq, &msg);
  2986. set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
  2987. "edge");
  2988. return 0;
  2989. }
  2990. #endif
  2991. #endif /* CONFIG_PCI_MSI */
  2992. /*
  2993. * Hypertransport interrupt support
  2994. */
  2995. #ifdef CONFIG_HT_IRQ
  2996. #ifdef CONFIG_SMP
  2997. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  2998. {
  2999. struct ht_irq_msg msg;
  3000. fetch_ht_irq_msg(irq, &msg);
  3001. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  3002. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  3003. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  3004. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  3005. write_ht_irq_msg(irq, &msg);
  3006. }
  3007. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  3008. {
  3009. struct irq_cfg *cfg;
  3010. unsigned int dest;
  3011. cpumask_t tmp;
  3012. struct irq_desc *desc;
  3013. cpus_and(tmp, mask, cpu_online_map);
  3014. if (cpus_empty(tmp))
  3015. return;
  3016. if (assign_irq_vector(irq, mask))
  3017. return;
  3018. cfg = irq_cfg(irq);
  3019. cpus_and(tmp, cfg->domain, mask);
  3020. dest = cpu_mask_to_apicid(tmp);
  3021. target_ht_irq(irq, dest, cfg->vector);
  3022. desc = irq_to_desc(irq);
  3023. desc->affinity = mask;
  3024. }
  3025. #endif
  3026. static struct irq_chip ht_irq_chip = {
  3027. .name = "PCI-HT",
  3028. .mask = mask_ht_irq,
  3029. .unmask = unmask_ht_irq,
  3030. .ack = ack_apic_edge,
  3031. #ifdef CONFIG_SMP
  3032. .set_affinity = set_ht_irq_affinity,
  3033. #endif
  3034. .retrigger = ioapic_retrigger_irq,
  3035. };
  3036. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  3037. {
  3038. struct irq_cfg *cfg;
  3039. int err;
  3040. cpumask_t tmp;
  3041. tmp = TARGET_CPUS;
  3042. err = assign_irq_vector(irq, tmp);
  3043. if (!err) {
  3044. struct ht_irq_msg msg;
  3045. unsigned dest;
  3046. cfg = irq_cfg(irq);
  3047. cpus_and(tmp, cfg->domain, tmp);
  3048. dest = cpu_mask_to_apicid(tmp);
  3049. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  3050. msg.address_lo =
  3051. HT_IRQ_LOW_BASE |
  3052. HT_IRQ_LOW_DEST_ID(dest) |
  3053. HT_IRQ_LOW_VECTOR(cfg->vector) |
  3054. ((INT_DEST_MODE == 0) ?
  3055. HT_IRQ_LOW_DM_PHYSICAL :
  3056. HT_IRQ_LOW_DM_LOGICAL) |
  3057. HT_IRQ_LOW_RQEOI_EDGE |
  3058. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  3059. HT_IRQ_LOW_MT_FIXED :
  3060. HT_IRQ_LOW_MT_ARBITRATED) |
  3061. HT_IRQ_LOW_IRQ_MASKED;
  3062. write_ht_irq_msg(irq, &msg);
  3063. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  3064. handle_edge_irq, "edge");
  3065. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
  3066. }
  3067. return err;
  3068. }
  3069. #endif /* CONFIG_HT_IRQ */
  3070. #ifdef CONFIG_X86_64
  3071. /*
  3072. * Re-target the irq to the specified CPU and enable the specified MMR located
  3073. * on the specified blade to allow the sending of MSIs to the specified CPU.
  3074. */
  3075. int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
  3076. unsigned long mmr_offset)
  3077. {
  3078. const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
  3079. struct irq_cfg *cfg;
  3080. int mmr_pnode;
  3081. unsigned long mmr_value;
  3082. struct uv_IO_APIC_route_entry *entry;
  3083. unsigned long flags;
  3084. int err;
  3085. err = assign_irq_vector(irq, *eligible_cpu);
  3086. if (err != 0)
  3087. return err;
  3088. spin_lock_irqsave(&vector_lock, flags);
  3089. set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
  3090. irq_name);
  3091. spin_unlock_irqrestore(&vector_lock, flags);
  3092. cfg = irq_cfg(irq);
  3093. mmr_value = 0;
  3094. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3095. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3096. entry->vector = cfg->vector;
  3097. entry->delivery_mode = INT_DELIVERY_MODE;
  3098. entry->dest_mode = INT_DEST_MODE;
  3099. entry->polarity = 0;
  3100. entry->trigger = 0;
  3101. entry->mask = 0;
  3102. entry->dest = cpu_mask_to_apicid(*eligible_cpu);
  3103. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3104. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3105. return irq;
  3106. }
  3107. /*
  3108. * Disable the specified MMR located on the specified blade so that MSIs are
  3109. * longer allowed to be sent.
  3110. */
  3111. void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
  3112. {
  3113. unsigned long mmr_value;
  3114. struct uv_IO_APIC_route_entry *entry;
  3115. int mmr_pnode;
  3116. mmr_value = 0;
  3117. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  3118. BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
  3119. entry->mask = 1;
  3120. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  3121. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  3122. }
  3123. #endif /* CONFIG_X86_64 */
  3124. int __init io_apic_get_redir_entries (int ioapic)
  3125. {
  3126. union IO_APIC_reg_01 reg_01;
  3127. unsigned long flags;
  3128. spin_lock_irqsave(&ioapic_lock, flags);
  3129. reg_01.raw = io_apic_read(ioapic, 1);
  3130. spin_unlock_irqrestore(&ioapic_lock, flags);
  3131. return reg_01.bits.entries;
  3132. }
  3133. int __init probe_nr_irqs(void)
  3134. {
  3135. return NR_IRQS;
  3136. }
  3137. /* --------------------------------------------------------------------------
  3138. ACPI-based IOAPIC Configuration
  3139. -------------------------------------------------------------------------- */
  3140. #ifdef CONFIG_ACPI
  3141. #ifdef CONFIG_X86_32
  3142. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  3143. {
  3144. union IO_APIC_reg_00 reg_00;
  3145. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  3146. physid_mask_t tmp;
  3147. unsigned long flags;
  3148. int i = 0;
  3149. /*
  3150. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  3151. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  3152. * supports up to 16 on one shared APIC bus.
  3153. *
  3154. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  3155. * advantage of new APIC bus architecture.
  3156. */
  3157. if (physids_empty(apic_id_map))
  3158. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  3159. spin_lock_irqsave(&ioapic_lock, flags);
  3160. reg_00.raw = io_apic_read(ioapic, 0);
  3161. spin_unlock_irqrestore(&ioapic_lock, flags);
  3162. if (apic_id >= get_physical_broadcast()) {
  3163. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  3164. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  3165. apic_id = reg_00.bits.ID;
  3166. }
  3167. /*
  3168. * Every APIC in a system must have a unique ID or we get lots of nice
  3169. * 'stuck on smp_invalidate_needed IPI wait' messages.
  3170. */
  3171. if (check_apicid_used(apic_id_map, apic_id)) {
  3172. for (i = 0; i < get_physical_broadcast(); i++) {
  3173. if (!check_apicid_used(apic_id_map, i))
  3174. break;
  3175. }
  3176. if (i == get_physical_broadcast())
  3177. panic("Max apic_id exceeded!\n");
  3178. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  3179. "trying %d\n", ioapic, apic_id, i);
  3180. apic_id = i;
  3181. }
  3182. tmp = apicid_to_cpu_present(apic_id);
  3183. physids_or(apic_id_map, apic_id_map, tmp);
  3184. if (reg_00.bits.ID != apic_id) {
  3185. reg_00.bits.ID = apic_id;
  3186. spin_lock_irqsave(&ioapic_lock, flags);
  3187. io_apic_write(ioapic, 0, reg_00.raw);
  3188. reg_00.raw = io_apic_read(ioapic, 0);
  3189. spin_unlock_irqrestore(&ioapic_lock, flags);
  3190. /* Sanity check */
  3191. if (reg_00.bits.ID != apic_id) {
  3192. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  3193. return -1;
  3194. }
  3195. }
  3196. apic_printk(APIC_VERBOSE, KERN_INFO
  3197. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  3198. return apic_id;
  3199. }
  3200. int __init io_apic_get_version(int ioapic)
  3201. {
  3202. union IO_APIC_reg_01 reg_01;
  3203. unsigned long flags;
  3204. spin_lock_irqsave(&ioapic_lock, flags);
  3205. reg_01.raw = io_apic_read(ioapic, 1);
  3206. spin_unlock_irqrestore(&ioapic_lock, flags);
  3207. return reg_01.bits.version;
  3208. }
  3209. #endif
  3210. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  3211. {
  3212. struct irq_desc *desc;
  3213. struct irq_cfg *cfg;
  3214. int cpu = boot_cpu_id;
  3215. if (!IO_APIC_IRQ(irq)) {
  3216. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  3217. ioapic);
  3218. return -EINVAL;
  3219. }
  3220. desc = irq_to_desc_alloc_cpu(irq, cpu);
  3221. if (!desc) {
  3222. printk(KERN_INFO "can not get irq_desc %d\n", irq);
  3223. return 0;
  3224. }
  3225. /*
  3226. * IRQs < 16 are already in the irq_2_pin[] map
  3227. */
  3228. if (irq >= 16) {
  3229. cfg = desc->chip_data;
  3230. add_pin_to_irq_cpu(irq, cpu, ioapic, pin);
  3231. }
  3232. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  3233. return 0;
  3234. }
  3235. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  3236. {
  3237. int i;
  3238. if (skip_ioapic_setup)
  3239. return -1;
  3240. for (i = 0; i < mp_irq_entries; i++)
  3241. if (mp_irqs[i].mp_irqtype == mp_INT &&
  3242. mp_irqs[i].mp_srcbusirq == bus_irq)
  3243. break;
  3244. if (i >= mp_irq_entries)
  3245. return -1;
  3246. *trigger = irq_trigger(i);
  3247. *polarity = irq_polarity(i);
  3248. return 0;
  3249. }
  3250. #endif /* CONFIG_ACPI */
  3251. /*
  3252. * This function currently is only a helper for the i386 smp boot process where
  3253. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  3254. * so mask in all cases should simply be TARGET_CPUS
  3255. */
  3256. #ifdef CONFIG_SMP
  3257. void __init setup_ioapic_dest(void)
  3258. {
  3259. int pin, ioapic, irq, irq_entry;
  3260. struct irq_desc *desc;
  3261. struct irq_cfg *cfg;
  3262. cpumask_t mask;
  3263. if (skip_ioapic_setup == 1)
  3264. return;
  3265. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  3266. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  3267. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  3268. if (irq_entry == -1)
  3269. continue;
  3270. irq = pin_2_irq(irq_entry, ioapic, pin);
  3271. /* setup_IO_APIC_irqs could fail to get vector for some device
  3272. * when you have too many devices, because at that time only boot
  3273. * cpu is online.
  3274. */
  3275. desc = irq_to_desc(irq);
  3276. cfg = desc->chip_data;
  3277. if (!cfg->vector) {
  3278. setup_IO_APIC_irq(ioapic, pin, irq,
  3279. irq_trigger(irq_entry),
  3280. irq_polarity(irq_entry));
  3281. continue;
  3282. }
  3283. /*
  3284. * Honour affinities which have been set in early boot
  3285. */
  3286. if (desc->status &
  3287. (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
  3288. mask = desc->affinity;
  3289. else
  3290. mask = TARGET_CPUS;
  3291. #ifdef CONFIG_INTR_REMAP
  3292. if (intr_remapping_enabled)
  3293. set_ir_ioapic_affinity_irq(irq, mask);
  3294. else
  3295. #endif
  3296. set_ioapic_affinity_irq(irq, mask);
  3297. }
  3298. }
  3299. }
  3300. #endif
  3301. #define IOAPIC_RESOURCE_NAME_SIZE 11
  3302. static struct resource *ioapic_resources;
  3303. static struct resource * __init ioapic_setup_resources(void)
  3304. {
  3305. unsigned long n;
  3306. struct resource *res;
  3307. char *mem;
  3308. int i;
  3309. if (nr_ioapics <= 0)
  3310. return NULL;
  3311. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  3312. n *= nr_ioapics;
  3313. mem = alloc_bootmem(n);
  3314. res = (void *)mem;
  3315. if (mem != NULL) {
  3316. mem += sizeof(struct resource) * nr_ioapics;
  3317. for (i = 0; i < nr_ioapics; i++) {
  3318. res[i].name = mem;
  3319. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  3320. sprintf(mem, "IOAPIC %u", i);
  3321. mem += IOAPIC_RESOURCE_NAME_SIZE;
  3322. }
  3323. }
  3324. ioapic_resources = res;
  3325. return res;
  3326. }
  3327. void __init ioapic_init_mappings(void)
  3328. {
  3329. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  3330. struct resource *ioapic_res;
  3331. int i;
  3332. ioapic_res = ioapic_setup_resources();
  3333. for (i = 0; i < nr_ioapics; i++) {
  3334. if (smp_found_config) {
  3335. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  3336. #ifdef CONFIG_X86_32
  3337. if (!ioapic_phys) {
  3338. printk(KERN_ERR
  3339. "WARNING: bogus zero IO-APIC "
  3340. "address found in MPTABLE, "
  3341. "disabling IO/APIC support!\n");
  3342. smp_found_config = 0;
  3343. skip_ioapic_setup = 1;
  3344. goto fake_ioapic_page;
  3345. }
  3346. #endif
  3347. } else {
  3348. #ifdef CONFIG_X86_32
  3349. fake_ioapic_page:
  3350. #endif
  3351. ioapic_phys = (unsigned long)
  3352. alloc_bootmem_pages(PAGE_SIZE);
  3353. ioapic_phys = __pa(ioapic_phys);
  3354. }
  3355. set_fixmap_nocache(idx, ioapic_phys);
  3356. apic_printk(APIC_VERBOSE,
  3357. "mapped IOAPIC to %08lx (%08lx)\n",
  3358. __fix_to_virt(idx), ioapic_phys);
  3359. idx++;
  3360. if (ioapic_res != NULL) {
  3361. ioapic_res->start = ioapic_phys;
  3362. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  3363. ioapic_res++;
  3364. }
  3365. }
  3366. }
  3367. static int __init ioapic_insert_resources(void)
  3368. {
  3369. int i;
  3370. struct resource *r = ioapic_resources;
  3371. if (!r) {
  3372. printk(KERN_ERR
  3373. "IO APIC resources could be not be allocated.\n");
  3374. return -1;
  3375. }
  3376. for (i = 0; i < nr_ioapics; i++) {
  3377. insert_resource(&iomem_resource, r);
  3378. r++;
  3379. }
  3380. return 0;
  3381. }
  3382. /* Insert the IO APIC resources after PCI initialization has occured to handle
  3383. * IO APICS that are mapped in on a BAR in PCI space. */
  3384. late_initcall(ioapic_insert_resources);