io_apic.c 94 KB

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