io_apic.c 99 KB

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