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