io_apic.c 99 KB

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