io_apic.c 95 KB

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