io_apic.c 98 KB

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