io_apic.c 103 KB

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