io_apic.c 98 KB

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