io_apic.c 98 KB

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