io_apic.c 101 KB

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