io_apic.c 97 KB

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