io_apic.c 99 KB

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