io_apic.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933
  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 = ARRAY_SIZE(irq_cfg_legacy);
  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. if (!APIC_XAPIC(ver)) {
  1488. v = apic_read(APIC_ARBPRI);
  1489. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1490. v & APIC_ARBPRI_MASK);
  1491. }
  1492. v = apic_read(APIC_PROCPRI);
  1493. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1494. }
  1495. /*
  1496. * Remote read supported only in the 82489DX and local APIC for
  1497. * Pentium processors.
  1498. */
  1499. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1500. v = apic_read(APIC_RRR);
  1501. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1502. }
  1503. v = apic_read(APIC_LDR);
  1504. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1505. if (!x2apic_enabled()) {
  1506. v = apic_read(APIC_DFR);
  1507. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1508. }
  1509. v = apic_read(APIC_SPIV);
  1510. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1511. printk(KERN_DEBUG "... APIC ISR field:\n");
  1512. print_APIC_bitfield(APIC_ISR);
  1513. printk(KERN_DEBUG "... APIC TMR field:\n");
  1514. print_APIC_bitfield(APIC_TMR);
  1515. printk(KERN_DEBUG "... APIC IRR field:\n");
  1516. print_APIC_bitfield(APIC_IRR);
  1517. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1518. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1519. apic_write(APIC_ESR, 0);
  1520. v = apic_read(APIC_ESR);
  1521. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1522. }
  1523. icr = apic_icr_read();
  1524. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1525. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1526. v = apic_read(APIC_LVTT);
  1527. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1528. if (maxlvt > 3) { /* PC is LVT#4. */
  1529. v = apic_read(APIC_LVTPC);
  1530. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1531. }
  1532. v = apic_read(APIC_LVT0);
  1533. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1534. v = apic_read(APIC_LVT1);
  1535. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1536. if (maxlvt > 2) { /* ERR is LVT#3. */
  1537. v = apic_read(APIC_LVTERR);
  1538. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1539. }
  1540. v = apic_read(APIC_TMICT);
  1541. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1542. v = apic_read(APIC_TMCCT);
  1543. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1544. v = apic_read(APIC_TDCR);
  1545. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1546. printk("\n");
  1547. }
  1548. __apicdebuginit(void) print_all_local_APICs(void)
  1549. {
  1550. int cpu;
  1551. preempt_disable();
  1552. for_each_online_cpu(cpu)
  1553. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1554. preempt_enable();
  1555. }
  1556. __apicdebuginit(void) print_PIC(void)
  1557. {
  1558. unsigned int v;
  1559. unsigned long flags;
  1560. if (apic_verbosity == APIC_QUIET)
  1561. return;
  1562. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1563. spin_lock_irqsave(&i8259A_lock, flags);
  1564. v = inb(0xa1) << 8 | inb(0x21);
  1565. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1566. v = inb(0xa0) << 8 | inb(0x20);
  1567. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1568. outb(0x0b,0xa0);
  1569. outb(0x0b,0x20);
  1570. v = inb(0xa0) << 8 | inb(0x20);
  1571. outb(0x0a,0xa0);
  1572. outb(0x0a,0x20);
  1573. spin_unlock_irqrestore(&i8259A_lock, flags);
  1574. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1575. v = inb(0x4d1) << 8 | inb(0x4d0);
  1576. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1577. }
  1578. __apicdebuginit(int) print_all_ICs(void)
  1579. {
  1580. print_PIC();
  1581. print_all_local_APICs();
  1582. print_IO_APIC();
  1583. return 0;
  1584. }
  1585. fs_initcall(print_all_ICs);
  1586. /* Where if anywhere is the i8259 connect in external int mode */
  1587. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1588. void __init enable_IO_APIC(void)
  1589. {
  1590. union IO_APIC_reg_01 reg_01;
  1591. int i8259_apic, i8259_pin;
  1592. int apic;
  1593. unsigned long flags;
  1594. #ifdef CONFIG_X86_32
  1595. int i;
  1596. if (!pirqs_enabled)
  1597. for (i = 0; i < MAX_PIRQS; i++)
  1598. pirq_entries[i] = -1;
  1599. #endif
  1600. /*
  1601. * The number of IO-APIC IRQ registers (== #pins):
  1602. */
  1603. for (apic = 0; apic < nr_ioapics; apic++) {
  1604. spin_lock_irqsave(&ioapic_lock, flags);
  1605. reg_01.raw = io_apic_read(apic, 1);
  1606. spin_unlock_irqrestore(&ioapic_lock, flags);
  1607. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1608. }
  1609. for(apic = 0; apic < nr_ioapics; apic++) {
  1610. int pin;
  1611. /* See if any of the pins is in ExtINT mode */
  1612. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1613. struct IO_APIC_route_entry entry;
  1614. entry = ioapic_read_entry(apic, pin);
  1615. /* If the interrupt line is enabled and in ExtInt mode
  1616. * I have found the pin where the i8259 is connected.
  1617. */
  1618. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1619. ioapic_i8259.apic = apic;
  1620. ioapic_i8259.pin = pin;
  1621. goto found_i8259;
  1622. }
  1623. }
  1624. }
  1625. found_i8259:
  1626. /* Look to see what if the MP table has reported the ExtINT */
  1627. /* If we could not find the appropriate pin by looking at the ioapic
  1628. * the i8259 probably is not connected the ioapic but give the
  1629. * mptable a chance anyway.
  1630. */
  1631. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1632. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1633. /* Trust the MP table if nothing is setup in the hardware */
  1634. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1635. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1636. ioapic_i8259.pin = i8259_pin;
  1637. ioapic_i8259.apic = i8259_apic;
  1638. }
  1639. /* Complain if the MP table and the hardware disagree */
  1640. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1641. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1642. {
  1643. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1644. }
  1645. /*
  1646. * Do not trust the IO-APIC being empty at bootup
  1647. */
  1648. clear_IO_APIC();
  1649. }
  1650. /*
  1651. * Not an __init, needed by the reboot code
  1652. */
  1653. void disable_IO_APIC(void)
  1654. {
  1655. /*
  1656. * Clear the IO-APIC before rebooting:
  1657. */
  1658. clear_IO_APIC();
  1659. /*
  1660. * If the i8259 is routed through an IOAPIC
  1661. * Put that IOAPIC in virtual wire mode
  1662. * so legacy interrupts can be delivered.
  1663. */
  1664. if (ioapic_i8259.pin != -1) {
  1665. struct IO_APIC_route_entry entry;
  1666. memset(&entry, 0, sizeof(entry));
  1667. entry.mask = 0; /* Enabled */
  1668. entry.trigger = 0; /* Edge */
  1669. entry.irr = 0;
  1670. entry.polarity = 0; /* High */
  1671. entry.delivery_status = 0;
  1672. entry.dest_mode = 0; /* Physical */
  1673. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1674. entry.vector = 0;
  1675. entry.dest = read_apic_id();
  1676. /*
  1677. * Add it to the IO-APIC irq-routing table:
  1678. */
  1679. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1680. }
  1681. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1682. }
  1683. #ifdef CONFIG_X86_32
  1684. /*
  1685. * function to set the IO-APIC physical IDs based on the
  1686. * values stored in the MPC table.
  1687. *
  1688. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1689. */
  1690. static void __init setup_ioapic_ids_from_mpc(void)
  1691. {
  1692. union IO_APIC_reg_00 reg_00;
  1693. physid_mask_t phys_id_present_map;
  1694. int apic;
  1695. int i;
  1696. unsigned char old_id;
  1697. unsigned long flags;
  1698. if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
  1699. return;
  1700. /*
  1701. * Don't check I/O APIC IDs for xAPIC systems. They have
  1702. * no meaning without the serial APIC bus.
  1703. */
  1704. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1705. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1706. return;
  1707. /*
  1708. * This is broken; anything with a real cpu count has to
  1709. * circumvent this idiocy regardless.
  1710. */
  1711. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1712. /*
  1713. * Set the IOAPIC ID to the value stored in the MPC table.
  1714. */
  1715. for (apic = 0; apic < nr_ioapics; apic++) {
  1716. /* Read the register 0 value */
  1717. spin_lock_irqsave(&ioapic_lock, flags);
  1718. reg_00.raw = io_apic_read(apic, 0);
  1719. spin_unlock_irqrestore(&ioapic_lock, flags);
  1720. old_id = mp_ioapics[apic].mp_apicid;
  1721. if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
  1722. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1723. apic, mp_ioapics[apic].mp_apicid);
  1724. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1725. reg_00.bits.ID);
  1726. mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
  1727. }
  1728. /*
  1729. * Sanity check, is the ID really free? Every APIC in a
  1730. * system must have a unique ID or we get lots of nice
  1731. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1732. */
  1733. if (check_apicid_used(phys_id_present_map,
  1734. mp_ioapics[apic].mp_apicid)) {
  1735. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1736. apic, mp_ioapics[apic].mp_apicid);
  1737. for (i = 0; i < get_physical_broadcast(); i++)
  1738. if (!physid_isset(i, phys_id_present_map))
  1739. break;
  1740. if (i >= get_physical_broadcast())
  1741. panic("Max APIC ID exceeded!\n");
  1742. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1743. i);
  1744. physid_set(i, phys_id_present_map);
  1745. mp_ioapics[apic].mp_apicid = i;
  1746. } else {
  1747. physid_mask_t tmp;
  1748. tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
  1749. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1750. "phys_id_present_map\n",
  1751. mp_ioapics[apic].mp_apicid);
  1752. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1753. }
  1754. /*
  1755. * We need to adjust the IRQ routing table
  1756. * if the ID changed.
  1757. */
  1758. if (old_id != mp_ioapics[apic].mp_apicid)
  1759. for (i = 0; i < mp_irq_entries; i++)
  1760. if (mp_irqs[i].mp_dstapic == old_id)
  1761. mp_irqs[i].mp_dstapic
  1762. = mp_ioapics[apic].mp_apicid;
  1763. /*
  1764. * Read the right value from the MPC table and
  1765. * write it into the ID register.
  1766. */
  1767. apic_printk(APIC_VERBOSE, KERN_INFO
  1768. "...changing IO-APIC physical APIC ID to %d ...",
  1769. mp_ioapics[apic].mp_apicid);
  1770. reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
  1771. spin_lock_irqsave(&ioapic_lock, flags);
  1772. io_apic_write(apic, 0, reg_00.raw);
  1773. spin_unlock_irqrestore(&ioapic_lock, flags);
  1774. /*
  1775. * Sanity check
  1776. */
  1777. spin_lock_irqsave(&ioapic_lock, flags);
  1778. reg_00.raw = io_apic_read(apic, 0);
  1779. spin_unlock_irqrestore(&ioapic_lock, flags);
  1780. if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
  1781. printk("could not set ID!\n");
  1782. else
  1783. apic_printk(APIC_VERBOSE, " ok.\n");
  1784. }
  1785. }
  1786. #endif
  1787. int no_timer_check __initdata;
  1788. static int __init notimercheck(char *s)
  1789. {
  1790. no_timer_check = 1;
  1791. return 1;
  1792. }
  1793. __setup("no_timer_check", notimercheck);
  1794. /*
  1795. * There is a nasty bug in some older SMP boards, their mptable lies
  1796. * about the timer IRQ. We do the following to work around the situation:
  1797. *
  1798. * - timer IRQ defaults to IO-APIC IRQ
  1799. * - if this function detects that timer IRQs are defunct, then we fall
  1800. * back to ISA timer IRQs
  1801. */
  1802. static int __init timer_irq_works(void)
  1803. {
  1804. unsigned long t1 = jiffies;
  1805. unsigned long flags;
  1806. if (no_timer_check)
  1807. return 1;
  1808. local_save_flags(flags);
  1809. local_irq_enable();
  1810. /* Let ten ticks pass... */
  1811. mdelay((10 * 1000) / HZ);
  1812. local_irq_restore(flags);
  1813. /*
  1814. * Expect a few ticks at least, to be sure some possible
  1815. * glue logic does not lock up after one or two first
  1816. * ticks in a non-ExtINT mode. Also the local APIC
  1817. * might have cached one ExtINT interrupt. Finally, at
  1818. * least one tick may be lost due to delays.
  1819. */
  1820. /* jiffies wrap? */
  1821. if (time_after(jiffies, t1 + 4))
  1822. return 1;
  1823. return 0;
  1824. }
  1825. /*
  1826. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1827. * number of pending IRQ events unhandled. These cases are very rare,
  1828. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1829. * better to do it this way as thus we do not have to be aware of
  1830. * 'pending' interrupts in the IRQ path, except at this point.
  1831. */
  1832. /*
  1833. * Edge triggered needs to resend any interrupt
  1834. * that was delayed but this is now handled in the device
  1835. * independent code.
  1836. */
  1837. /*
  1838. * Starting up a edge-triggered IO-APIC interrupt is
  1839. * nasty - we need to make sure that we get the edge.
  1840. * If it is already asserted for some reason, we need
  1841. * return 1 to indicate that is was pending.
  1842. *
  1843. * This is not complete - we should be able to fake
  1844. * an edge even if it isn't on the 8259A...
  1845. */
  1846. static unsigned int startup_ioapic_irq(unsigned int irq)
  1847. {
  1848. int was_pending = 0;
  1849. unsigned long flags;
  1850. spin_lock_irqsave(&ioapic_lock, flags);
  1851. if (irq < 16) {
  1852. disable_8259A_irq(irq);
  1853. if (i8259A_irq_pending(irq))
  1854. was_pending = 1;
  1855. }
  1856. __unmask_IO_APIC_irq(irq);
  1857. spin_unlock_irqrestore(&ioapic_lock, flags);
  1858. return was_pending;
  1859. }
  1860. #ifdef CONFIG_X86_64
  1861. static int ioapic_retrigger_irq(unsigned int irq)
  1862. {
  1863. struct irq_cfg *cfg = irq_cfg(irq);
  1864. unsigned long flags;
  1865. spin_lock_irqsave(&vector_lock, flags);
  1866. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
  1867. spin_unlock_irqrestore(&vector_lock, flags);
  1868. return 1;
  1869. }
  1870. #else
  1871. static int ioapic_retrigger_irq(unsigned int irq)
  1872. {
  1873. send_IPI_self(irq_cfg(irq)->vector);
  1874. return 1;
  1875. }
  1876. #endif
  1877. /*
  1878. * Level and edge triggered IO-APIC interrupts need different handling,
  1879. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1880. * handled with the level-triggered descriptor, but that one has slightly
  1881. * more overhead. Level-triggered interrupts cannot be handled with the
  1882. * edge-triggered handler, without risking IRQ storms and other ugly
  1883. * races.
  1884. */
  1885. #ifdef CONFIG_SMP
  1886. #ifdef CONFIG_INTR_REMAP
  1887. static void ir_irq_migration(struct work_struct *work);
  1888. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1889. /*
  1890. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1891. *
  1892. * For edge triggered, irq migration is a simple atomic update(of vector
  1893. * and cpu destination) of IRTE and flush the hardware cache.
  1894. *
  1895. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1896. * vector information, along with modifying IRTE with vector and destination.
  1897. * So irq migration for level triggered is little bit more complex compared to
  1898. * edge triggered migration. But the good news is, we use the same algorithm
  1899. * for level triggered migration as we have today, only difference being,
  1900. * we now initiate the irq migration from process context instead of the
  1901. * interrupt context.
  1902. *
  1903. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1904. * suppression) to the IO-APIC, level triggered irq migration will also be
  1905. * as simple as edge triggered migration and we can do the irq migration
  1906. * with a simple atomic update to IO-APIC RTE.
  1907. */
  1908. static void migrate_ioapic_irq(int irq, cpumask_t mask)
  1909. {
  1910. struct irq_cfg *cfg;
  1911. struct irq_desc *desc;
  1912. cpumask_t tmp, cleanup_mask;
  1913. struct irte irte;
  1914. int modify_ioapic_rte;
  1915. unsigned int dest;
  1916. unsigned long flags;
  1917. cpus_and(tmp, mask, cpu_online_map);
  1918. if (cpus_empty(tmp))
  1919. return;
  1920. if (get_irte(irq, &irte))
  1921. return;
  1922. if (assign_irq_vector(irq, mask))
  1923. return;
  1924. cfg = irq_cfg(irq);
  1925. cpus_and(tmp, cfg->domain, mask);
  1926. dest = cpu_mask_to_apicid(tmp);
  1927. desc = irq_to_desc(irq);
  1928. modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1929. if (modify_ioapic_rte) {
  1930. spin_lock_irqsave(&ioapic_lock, flags);
  1931. __target_IO_APIC_irq(irq, dest, cfg->vector);
  1932. spin_unlock_irqrestore(&ioapic_lock, flags);
  1933. }
  1934. irte.vector = cfg->vector;
  1935. irte.dest_id = IRTE_DEST(dest);
  1936. /*
  1937. * Modified the IRTE and flushes the Interrupt entry cache.
  1938. */
  1939. modify_irte(irq, &irte);
  1940. if (cfg->move_in_progress) {
  1941. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1942. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1943. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1944. cfg->move_in_progress = 0;
  1945. }
  1946. desc->affinity = mask;
  1947. }
  1948. static int migrate_irq_remapped_level(int irq)
  1949. {
  1950. int ret = -1;
  1951. struct irq_desc *desc = irq_to_desc(irq);
  1952. mask_IO_APIC_irq(irq);
  1953. if (io_apic_level_ack_pending(irq)) {
  1954. /*
  1955. * Interrupt in progress. Migrating irq now will change the
  1956. * vector information in the IO-APIC RTE and that will confuse
  1957. * the EOI broadcast performed by cpu.
  1958. * So, delay the irq migration to the next instance.
  1959. */
  1960. schedule_delayed_work(&ir_migration_work, 1);
  1961. goto unmask;
  1962. }
  1963. /* everthing is clear. we have right of way */
  1964. migrate_ioapic_irq(irq, desc->pending_mask);
  1965. ret = 0;
  1966. desc->status &= ~IRQ_MOVE_PENDING;
  1967. cpus_clear(desc->pending_mask);
  1968. unmask:
  1969. unmask_IO_APIC_irq(irq);
  1970. return ret;
  1971. }
  1972. static void ir_irq_migration(struct work_struct *work)
  1973. {
  1974. unsigned int irq;
  1975. struct irq_desc *desc;
  1976. for_each_irq_desc(irq, desc) {
  1977. if (desc->status & IRQ_MOVE_PENDING) {
  1978. unsigned long flags;
  1979. spin_lock_irqsave(&desc->lock, flags);
  1980. if (!desc->chip->set_affinity ||
  1981. !(desc->status & IRQ_MOVE_PENDING)) {
  1982. desc->status &= ~IRQ_MOVE_PENDING;
  1983. spin_unlock_irqrestore(&desc->lock, flags);
  1984. continue;
  1985. }
  1986. desc->chip->set_affinity(irq, desc->pending_mask);
  1987. spin_unlock_irqrestore(&desc->lock, flags);
  1988. }
  1989. }
  1990. }
  1991. /*
  1992. * Migrates the IRQ destination in the process context.
  1993. */
  1994. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1995. {
  1996. struct irq_desc *desc = irq_to_desc(irq);
  1997. if (desc->status & IRQ_LEVEL) {
  1998. desc->status |= IRQ_MOVE_PENDING;
  1999. desc->pending_mask = mask;
  2000. migrate_irq_remapped_level(irq);
  2001. return;
  2002. }
  2003. migrate_ioapic_irq(irq, mask);
  2004. }
  2005. #endif
  2006. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  2007. {
  2008. unsigned vector, me;
  2009. ack_APIC_irq();
  2010. #ifdef CONFIG_X86_64
  2011. exit_idle();
  2012. #endif
  2013. irq_enter();
  2014. me = smp_processor_id();
  2015. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  2016. unsigned int irq;
  2017. struct irq_desc *desc;
  2018. struct irq_cfg *cfg;
  2019. irq = __get_cpu_var(vector_irq)[vector];
  2020. desc = irq_to_desc(irq);
  2021. if (!desc)
  2022. continue;
  2023. cfg = irq_cfg(irq);
  2024. spin_lock(&desc->lock);
  2025. if (!cfg->move_cleanup_count)
  2026. goto unlock;
  2027. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  2028. goto unlock;
  2029. __get_cpu_var(vector_irq)[vector] = -1;
  2030. cfg->move_cleanup_count--;
  2031. unlock:
  2032. spin_unlock(&desc->lock);
  2033. }
  2034. irq_exit();
  2035. }
  2036. static void irq_complete_move(unsigned int irq)
  2037. {
  2038. struct irq_cfg *cfg = irq_cfg(irq);
  2039. unsigned vector, me;
  2040. if (likely(!cfg->move_in_progress))
  2041. return;
  2042. vector = ~get_irq_regs()->orig_ax;
  2043. me = smp_processor_id();
  2044. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  2045. cpumask_t cleanup_mask;
  2046. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2047. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2048. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2049. cfg->move_in_progress = 0;
  2050. }
  2051. }
  2052. #else
  2053. static inline void irq_complete_move(unsigned int irq) {}
  2054. #endif
  2055. #ifdef CONFIG_INTR_REMAP
  2056. static void ack_x2apic_level(unsigned int irq)
  2057. {
  2058. ack_x2APIC_irq();
  2059. }
  2060. static void ack_x2apic_edge(unsigned int irq)
  2061. {
  2062. ack_x2APIC_irq();
  2063. }
  2064. #endif
  2065. static void ack_apic_edge(unsigned int irq)
  2066. {
  2067. irq_complete_move(irq);
  2068. move_native_irq(irq);
  2069. ack_APIC_irq();
  2070. }
  2071. #ifdef CONFIG_X86_32
  2072. atomic_t irq_mis_count;
  2073. #endif
  2074. static void ack_apic_level(unsigned int irq)
  2075. {
  2076. #ifdef CONFIG_X86_32
  2077. unsigned long v;
  2078. int i;
  2079. #endif
  2080. int do_unmask_irq = 0;
  2081. irq_complete_move(irq);
  2082. #ifdef CONFIG_GENERIC_PENDING_IRQ
  2083. /* If we are moving the irq we need to mask it */
  2084. if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
  2085. do_unmask_irq = 1;
  2086. mask_IO_APIC_irq(irq);
  2087. }
  2088. #endif
  2089. #ifdef CONFIG_X86_32
  2090. /*
  2091. * It appears there is an erratum which affects at least version 0x11
  2092. * of I/O APIC (that's the 82093AA and cores integrated into various
  2093. * chipsets). Under certain conditions a level-triggered interrupt is
  2094. * erroneously delivered as edge-triggered one but the respective IRR
  2095. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  2096. * message but it will never arrive and further interrupts are blocked
  2097. * from the source. The exact reason is so far unknown, but the
  2098. * phenomenon was observed when two consecutive interrupt requests
  2099. * from a given source get delivered to the same CPU and the source is
  2100. * temporarily disabled in between.
  2101. *
  2102. * A workaround is to simulate an EOI message manually. We achieve it
  2103. * by setting the trigger mode to edge and then to level when the edge
  2104. * trigger mode gets detected in the TMR of a local APIC for a
  2105. * level-triggered interrupt. We mask the source for the time of the
  2106. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  2107. * The idea is from Manfred Spraul. --macro
  2108. */
  2109. i = irq_cfg(irq)->vector;
  2110. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  2111. #endif
  2112. /*
  2113. * We must acknowledge the irq before we move it or the acknowledge will
  2114. * not propagate properly.
  2115. */
  2116. ack_APIC_irq();
  2117. /* Now we can move and renable the irq */
  2118. if (unlikely(do_unmask_irq)) {
  2119. /* Only migrate the irq if the ack has been received.
  2120. *
  2121. * On rare occasions the broadcast level triggered ack gets
  2122. * delayed going to ioapics, and if we reprogram the
  2123. * vector while Remote IRR is still set the irq will never
  2124. * fire again.
  2125. *
  2126. * To prevent this scenario we read the Remote IRR bit
  2127. * of the ioapic. This has two effects.
  2128. * - On any sane system the read of the ioapic will
  2129. * flush writes (and acks) going to the ioapic from
  2130. * this cpu.
  2131. * - We get to see if the ACK has actually been delivered.
  2132. *
  2133. * Based on failed experiments of reprogramming the
  2134. * ioapic entry from outside of irq context starting
  2135. * with masking the ioapic entry and then polling until
  2136. * Remote IRR was clear before reprogramming the
  2137. * ioapic I don't trust the Remote IRR bit to be
  2138. * completey accurate.
  2139. *
  2140. * However there appears to be no other way to plug
  2141. * this race, so if the Remote IRR bit is not
  2142. * accurate and is causing problems then it is a hardware bug
  2143. * and you can go talk to the chipset vendor about it.
  2144. */
  2145. if (!io_apic_level_ack_pending(irq))
  2146. move_masked_irq(irq);
  2147. unmask_IO_APIC_irq(irq);
  2148. }
  2149. #ifdef CONFIG_X86_32
  2150. if (!(v & (1 << (i & 0x1f)))) {
  2151. atomic_inc(&irq_mis_count);
  2152. spin_lock(&ioapic_lock);
  2153. __mask_and_edge_IO_APIC_irq(irq);
  2154. __unmask_and_level_IO_APIC_irq(irq);
  2155. spin_unlock(&ioapic_lock);
  2156. }
  2157. #endif
  2158. }
  2159. static struct irq_chip ioapic_chip __read_mostly = {
  2160. .name = "IO-APIC",
  2161. .startup = startup_ioapic_irq,
  2162. .mask = mask_IO_APIC_irq,
  2163. .unmask = unmask_IO_APIC_irq,
  2164. .ack = ack_apic_edge,
  2165. .eoi = ack_apic_level,
  2166. #ifdef CONFIG_SMP
  2167. .set_affinity = set_ioapic_affinity_irq,
  2168. #endif
  2169. .retrigger = ioapic_retrigger_irq,
  2170. };
  2171. #ifdef CONFIG_INTR_REMAP
  2172. static struct irq_chip ir_ioapic_chip __read_mostly = {
  2173. .name = "IR-IO-APIC",
  2174. .startup = startup_ioapic_irq,
  2175. .mask = mask_IO_APIC_irq,
  2176. .unmask = unmask_IO_APIC_irq,
  2177. .ack = ack_x2apic_edge,
  2178. .eoi = ack_x2apic_level,
  2179. #ifdef CONFIG_SMP
  2180. .set_affinity = set_ir_ioapic_affinity_irq,
  2181. #endif
  2182. .retrigger = ioapic_retrigger_irq,
  2183. };
  2184. #endif
  2185. static inline void init_IO_APIC_traps(void)
  2186. {
  2187. int irq;
  2188. struct irq_desc *desc;
  2189. struct irq_cfg *cfg;
  2190. /*
  2191. * NOTE! The local APIC isn't very good at handling
  2192. * multiple interrupts at the same interrupt level.
  2193. * As the interrupt level is determined by taking the
  2194. * vector number and shifting that right by 4, we
  2195. * want to spread these out a bit so that they don't
  2196. * all fall in the same interrupt level.
  2197. *
  2198. * Also, we've got to be careful not to trash gate
  2199. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  2200. */
  2201. for_each_irq_cfg(irq, cfg) {
  2202. if (IO_APIC_IRQ(irq) && !cfg->vector) {
  2203. /*
  2204. * Hmm.. We don't have an entry for this,
  2205. * so default to an old-fashioned 8259
  2206. * interrupt if we can..
  2207. */
  2208. if (irq < 16)
  2209. make_8259A_irq(irq);
  2210. else {
  2211. desc = irq_to_desc(irq);
  2212. /* Strange. Oh, well.. */
  2213. desc->chip = &no_irq_chip;
  2214. }
  2215. }
  2216. }
  2217. }
  2218. /*
  2219. * The local APIC irq-chip implementation:
  2220. */
  2221. static void mask_lapic_irq(unsigned int irq)
  2222. {
  2223. unsigned long v;
  2224. v = apic_read(APIC_LVT0);
  2225. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  2226. }
  2227. static void unmask_lapic_irq(unsigned int irq)
  2228. {
  2229. unsigned long v;
  2230. v = apic_read(APIC_LVT0);
  2231. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  2232. }
  2233. static void ack_lapic_irq (unsigned int irq)
  2234. {
  2235. ack_APIC_irq();
  2236. }
  2237. static struct irq_chip lapic_chip __read_mostly = {
  2238. .name = "local-APIC",
  2239. .mask = mask_lapic_irq,
  2240. .unmask = unmask_lapic_irq,
  2241. .ack = ack_lapic_irq,
  2242. };
  2243. static void lapic_register_intr(int irq)
  2244. {
  2245. struct irq_desc *desc;
  2246. desc = irq_to_desc(irq);
  2247. desc->status &= ~IRQ_LEVEL;
  2248. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  2249. "edge");
  2250. }
  2251. static void __init setup_nmi(void)
  2252. {
  2253. /*
  2254. * Dirty trick to enable the NMI watchdog ...
  2255. * We put the 8259A master into AEOI mode and
  2256. * unmask on all local APICs LVT0 as NMI.
  2257. *
  2258. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  2259. * is from Maciej W. Rozycki - so we do not have to EOI from
  2260. * the NMI handler or the timer interrupt.
  2261. */
  2262. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  2263. enable_NMI_through_LVT0();
  2264. apic_printk(APIC_VERBOSE, " done.\n");
  2265. }
  2266. /*
  2267. * This looks a bit hackish but it's about the only one way of sending
  2268. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  2269. * not support the ExtINT mode, unfortunately. We need to send these
  2270. * cycles as some i82489DX-based boards have glue logic that keeps the
  2271. * 8259A interrupt line asserted until INTA. --macro
  2272. */
  2273. static inline void __init unlock_ExtINT_logic(void)
  2274. {
  2275. int apic, pin, i;
  2276. struct IO_APIC_route_entry entry0, entry1;
  2277. unsigned char save_control, save_freq_select;
  2278. pin = find_isa_irq_pin(8, mp_INT);
  2279. if (pin == -1) {
  2280. WARN_ON_ONCE(1);
  2281. return;
  2282. }
  2283. apic = find_isa_irq_apic(8, mp_INT);
  2284. if (apic == -1) {
  2285. WARN_ON_ONCE(1);
  2286. return;
  2287. }
  2288. entry0 = ioapic_read_entry(apic, pin);
  2289. clear_IO_APIC_pin(apic, pin);
  2290. memset(&entry1, 0, sizeof(entry1));
  2291. entry1.dest_mode = 0; /* physical delivery */
  2292. entry1.mask = 0; /* unmask IRQ now */
  2293. entry1.dest = hard_smp_processor_id();
  2294. entry1.delivery_mode = dest_ExtINT;
  2295. entry1.polarity = entry0.polarity;
  2296. entry1.trigger = 0;
  2297. entry1.vector = 0;
  2298. ioapic_write_entry(apic, pin, entry1);
  2299. save_control = CMOS_READ(RTC_CONTROL);
  2300. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  2301. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  2302. RTC_FREQ_SELECT);
  2303. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  2304. i = 100;
  2305. while (i-- > 0) {
  2306. mdelay(10);
  2307. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  2308. i -= 10;
  2309. }
  2310. CMOS_WRITE(save_control, RTC_CONTROL);
  2311. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  2312. clear_IO_APIC_pin(apic, pin);
  2313. ioapic_write_entry(apic, pin, entry0);
  2314. }
  2315. static int disable_timer_pin_1 __initdata;
  2316. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  2317. static int __init disable_timer_pin_setup(char *arg)
  2318. {
  2319. disable_timer_pin_1 = 1;
  2320. return 0;
  2321. }
  2322. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  2323. int timer_through_8259 __initdata;
  2324. /*
  2325. * This code may look a bit paranoid, but it's supposed to cooperate with
  2326. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  2327. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  2328. * fanatically on his truly buggy board.
  2329. *
  2330. * FIXME: really need to revamp this for all platforms.
  2331. */
  2332. static inline void __init check_timer(void)
  2333. {
  2334. struct irq_cfg *cfg = irq_cfg(0);
  2335. int apic1, pin1, apic2, pin2;
  2336. unsigned long flags;
  2337. unsigned int ver;
  2338. int no_pin1 = 0;
  2339. local_irq_save(flags);
  2340. ver = apic_read(APIC_LVR);
  2341. ver = GET_APIC_VERSION(ver);
  2342. /*
  2343. * get/set the timer IRQ vector:
  2344. */
  2345. disable_8259A_irq(0);
  2346. assign_irq_vector(0, TARGET_CPUS);
  2347. /*
  2348. * As IRQ0 is to be enabled in the 8259A, the virtual
  2349. * wire has to be disabled in the local APIC. Also
  2350. * timer interrupts need to be acknowledged manually in
  2351. * the 8259A for the i82489DX when using the NMI
  2352. * watchdog as that APIC treats NMIs as level-triggered.
  2353. * The AEOI mode will finish them in the 8259A
  2354. * automatically.
  2355. */
  2356. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  2357. init_8259A(1);
  2358. #ifdef CONFIG_X86_32
  2359. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  2360. #endif
  2361. pin1 = find_isa_irq_pin(0, mp_INT);
  2362. apic1 = find_isa_irq_apic(0, mp_INT);
  2363. pin2 = ioapic_i8259.pin;
  2364. apic2 = ioapic_i8259.apic;
  2365. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  2366. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  2367. cfg->vector, apic1, pin1, apic2, pin2);
  2368. /*
  2369. * Some BIOS writers are clueless and report the ExtINTA
  2370. * I/O APIC input from the cascaded 8259A as the timer
  2371. * interrupt input. So just in case, if only one pin
  2372. * was found above, try it both directly and through the
  2373. * 8259A.
  2374. */
  2375. if (pin1 == -1) {
  2376. #ifdef CONFIG_INTR_REMAP
  2377. if (intr_remapping_enabled)
  2378. panic("BIOS bug: timer not connected to IO-APIC");
  2379. #endif
  2380. pin1 = pin2;
  2381. apic1 = apic2;
  2382. no_pin1 = 1;
  2383. } else if (pin2 == -1) {
  2384. pin2 = pin1;
  2385. apic2 = apic1;
  2386. }
  2387. if (pin1 != -1) {
  2388. /*
  2389. * Ok, does IRQ0 through the IOAPIC work?
  2390. */
  2391. if (no_pin1) {
  2392. add_pin_to_irq(0, apic1, pin1);
  2393. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  2394. }
  2395. unmask_IO_APIC_irq(0);
  2396. if (timer_irq_works()) {
  2397. if (nmi_watchdog == NMI_IO_APIC) {
  2398. setup_nmi();
  2399. enable_8259A_irq(0);
  2400. }
  2401. if (disable_timer_pin_1 > 0)
  2402. clear_IO_APIC_pin(0, pin1);
  2403. goto out;
  2404. }
  2405. #ifdef CONFIG_INTR_REMAP
  2406. if (intr_remapping_enabled)
  2407. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  2408. #endif
  2409. clear_IO_APIC_pin(apic1, pin1);
  2410. if (!no_pin1)
  2411. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  2412. "8254 timer not connected to IO-APIC\n");
  2413. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  2414. "(IRQ0) through the 8259A ...\n");
  2415. apic_printk(APIC_QUIET, KERN_INFO
  2416. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  2417. /*
  2418. * legacy devices should be connected to IO APIC #0
  2419. */
  2420. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  2421. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  2422. unmask_IO_APIC_irq(0);
  2423. enable_8259A_irq(0);
  2424. if (timer_irq_works()) {
  2425. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  2426. timer_through_8259 = 1;
  2427. if (nmi_watchdog == NMI_IO_APIC) {
  2428. disable_8259A_irq(0);
  2429. setup_nmi();
  2430. enable_8259A_irq(0);
  2431. }
  2432. goto out;
  2433. }
  2434. /*
  2435. * Cleanup, just in case ...
  2436. */
  2437. disable_8259A_irq(0);
  2438. clear_IO_APIC_pin(apic2, pin2);
  2439. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  2440. }
  2441. if (nmi_watchdog == NMI_IO_APIC) {
  2442. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  2443. "through the IO-APIC - disabling NMI Watchdog!\n");
  2444. nmi_watchdog = NMI_NONE;
  2445. }
  2446. #ifdef CONFIG_X86_32
  2447. timer_ack = 0;
  2448. #endif
  2449. apic_printk(APIC_QUIET, KERN_INFO
  2450. "...trying to set up timer as Virtual Wire IRQ...\n");
  2451. lapic_register_intr(0);
  2452. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  2453. enable_8259A_irq(0);
  2454. if (timer_irq_works()) {
  2455. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2456. goto out;
  2457. }
  2458. disable_8259A_irq(0);
  2459. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  2460. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  2461. apic_printk(APIC_QUIET, KERN_INFO
  2462. "...trying to set up timer as ExtINT IRQ...\n");
  2463. init_8259A(0);
  2464. make_8259A_irq(0);
  2465. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  2466. unlock_ExtINT_logic();
  2467. if (timer_irq_works()) {
  2468. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2469. goto out;
  2470. }
  2471. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  2472. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  2473. "report. Then try booting with the 'noapic' option.\n");
  2474. out:
  2475. local_irq_restore(flags);
  2476. }
  2477. /*
  2478. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  2479. * to devices. However there may be an I/O APIC pin available for
  2480. * this interrupt regardless. The pin may be left unconnected, but
  2481. * typically it will be reused as an ExtINT cascade interrupt for
  2482. * the master 8259A. In the MPS case such a pin will normally be
  2483. * reported as an ExtINT interrupt in the MP table. With ACPI
  2484. * there is no provision for ExtINT interrupts, and in the absence
  2485. * of an override it would be treated as an ordinary ISA I/O APIC
  2486. * interrupt, that is edge-triggered and unmasked by default. We
  2487. * used to do this, but it caused problems on some systems because
  2488. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2489. * the same ExtINT cascade interrupt to drive the local APIC of the
  2490. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2491. * the I/O APIC in all cases now. No actual device should request
  2492. * it anyway. --macro
  2493. */
  2494. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  2495. void __init setup_IO_APIC(void)
  2496. {
  2497. #ifdef CONFIG_X86_32
  2498. enable_IO_APIC();
  2499. #else
  2500. /*
  2501. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2502. */
  2503. #endif
  2504. io_apic_irqs = ~PIC_IRQS;
  2505. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2506. /*
  2507. * Set up IO-APIC IRQ routing.
  2508. */
  2509. #ifdef CONFIG_X86_32
  2510. if (!acpi_ioapic)
  2511. setup_ioapic_ids_from_mpc();
  2512. #endif
  2513. sync_Arb_IDs();
  2514. setup_IO_APIC_irqs();
  2515. init_IO_APIC_traps();
  2516. check_timer();
  2517. }
  2518. /*
  2519. * Called after all the initialization is done. If we didnt find any
  2520. * APIC bugs then we can allow the modify fast path
  2521. */
  2522. static int __init io_apic_bug_finalize(void)
  2523. {
  2524. if (sis_apic_bug == -1)
  2525. sis_apic_bug = 0;
  2526. return 0;
  2527. }
  2528. late_initcall(io_apic_bug_finalize);
  2529. struct sysfs_ioapic_data {
  2530. struct sys_device dev;
  2531. struct IO_APIC_route_entry entry[0];
  2532. };
  2533. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  2534. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2535. {
  2536. struct IO_APIC_route_entry *entry;
  2537. struct sysfs_ioapic_data *data;
  2538. int i;
  2539. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2540. entry = data->entry;
  2541. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  2542. *entry = ioapic_read_entry(dev->id, i);
  2543. return 0;
  2544. }
  2545. static int ioapic_resume(struct sys_device *dev)
  2546. {
  2547. struct IO_APIC_route_entry *entry;
  2548. struct sysfs_ioapic_data *data;
  2549. unsigned long flags;
  2550. union IO_APIC_reg_00 reg_00;
  2551. int i;
  2552. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2553. entry = data->entry;
  2554. spin_lock_irqsave(&ioapic_lock, flags);
  2555. reg_00.raw = io_apic_read(dev->id, 0);
  2556. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  2557. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  2558. io_apic_write(dev->id, 0, reg_00.raw);
  2559. }
  2560. spin_unlock_irqrestore(&ioapic_lock, flags);
  2561. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2562. ioapic_write_entry(dev->id, i, entry[i]);
  2563. return 0;
  2564. }
  2565. static struct sysdev_class ioapic_sysdev_class = {
  2566. .name = "ioapic",
  2567. .suspend = ioapic_suspend,
  2568. .resume = ioapic_resume,
  2569. };
  2570. static int __init ioapic_init_sysfs(void)
  2571. {
  2572. struct sys_device * dev;
  2573. int i, size, error;
  2574. error = sysdev_class_register(&ioapic_sysdev_class);
  2575. if (error)
  2576. return error;
  2577. for (i = 0; i < nr_ioapics; i++ ) {
  2578. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2579. * sizeof(struct IO_APIC_route_entry);
  2580. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2581. if (!mp_ioapic_data[i]) {
  2582. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2583. continue;
  2584. }
  2585. dev = &mp_ioapic_data[i]->dev;
  2586. dev->id = i;
  2587. dev->cls = &ioapic_sysdev_class;
  2588. error = sysdev_register(dev);
  2589. if (error) {
  2590. kfree(mp_ioapic_data[i]);
  2591. mp_ioapic_data[i] = NULL;
  2592. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2593. continue;
  2594. }
  2595. }
  2596. return 0;
  2597. }
  2598. device_initcall(ioapic_init_sysfs);
  2599. /*
  2600. * Dynamic irq allocate and deallocation
  2601. */
  2602. unsigned int create_irq_nr(unsigned int irq_want)
  2603. {
  2604. /* Allocate an unused irq */
  2605. unsigned int irq;
  2606. unsigned int new;
  2607. unsigned long flags;
  2608. struct irq_cfg *cfg_new;
  2609. #ifndef CONFIG_HAVE_SPARSE_IRQ
  2610. irq_want = nr_irqs - 1;
  2611. #endif
  2612. irq = 0;
  2613. spin_lock_irqsave(&vector_lock, flags);
  2614. for (new = irq_want; new > 0; new--) {
  2615. if (platform_legacy_irq(new))
  2616. continue;
  2617. cfg_new = irq_cfg(new);
  2618. if (cfg_new && cfg_new->vector != 0)
  2619. continue;
  2620. /* check if need to create one */
  2621. if (!cfg_new)
  2622. cfg_new = irq_cfg_alloc(new);
  2623. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  2624. irq = new;
  2625. break;
  2626. }
  2627. spin_unlock_irqrestore(&vector_lock, flags);
  2628. if (irq > 0) {
  2629. dynamic_irq_init(irq);
  2630. }
  2631. return irq;
  2632. }
  2633. int create_irq(void)
  2634. {
  2635. int irq;
  2636. irq = create_irq_nr(nr_irqs - 1);
  2637. if (irq == 0)
  2638. irq = -1;
  2639. return irq;
  2640. }
  2641. void destroy_irq(unsigned int irq)
  2642. {
  2643. unsigned long flags;
  2644. dynamic_irq_cleanup(irq);
  2645. #ifdef CONFIG_INTR_REMAP
  2646. free_irte(irq);
  2647. #endif
  2648. spin_lock_irqsave(&vector_lock, flags);
  2649. __clear_irq_vector(irq);
  2650. spin_unlock_irqrestore(&vector_lock, flags);
  2651. }
  2652. /*
  2653. * MSI message composition
  2654. */
  2655. #ifdef CONFIG_PCI_MSI
  2656. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2657. {
  2658. struct irq_cfg *cfg;
  2659. int err;
  2660. unsigned dest;
  2661. cpumask_t tmp;
  2662. tmp = TARGET_CPUS;
  2663. err = assign_irq_vector(irq, tmp);
  2664. if (err)
  2665. return err;
  2666. cfg = irq_cfg(irq);
  2667. cpus_and(tmp, cfg->domain, tmp);
  2668. dest = cpu_mask_to_apicid(tmp);
  2669. #ifdef CONFIG_INTR_REMAP
  2670. if (irq_remapped(irq)) {
  2671. struct irte irte;
  2672. int ir_index;
  2673. u16 sub_handle;
  2674. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  2675. BUG_ON(ir_index == -1);
  2676. memset (&irte, 0, sizeof(irte));
  2677. irte.present = 1;
  2678. irte.dst_mode = INT_DEST_MODE;
  2679. irte.trigger_mode = 0; /* edge */
  2680. irte.dlvry_mode = INT_DELIVERY_MODE;
  2681. irte.vector = cfg->vector;
  2682. irte.dest_id = IRTE_DEST(dest);
  2683. modify_irte(irq, &irte);
  2684. msg->address_hi = MSI_ADDR_BASE_HI;
  2685. msg->data = sub_handle;
  2686. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  2687. MSI_ADDR_IR_SHV |
  2688. MSI_ADDR_IR_INDEX1(ir_index) |
  2689. MSI_ADDR_IR_INDEX2(ir_index);
  2690. } else
  2691. #endif
  2692. {
  2693. msg->address_hi = MSI_ADDR_BASE_HI;
  2694. msg->address_lo =
  2695. MSI_ADDR_BASE_LO |
  2696. ((INT_DEST_MODE == 0) ?
  2697. MSI_ADDR_DEST_MODE_PHYSICAL:
  2698. MSI_ADDR_DEST_MODE_LOGICAL) |
  2699. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2700. MSI_ADDR_REDIRECTION_CPU:
  2701. MSI_ADDR_REDIRECTION_LOWPRI) |
  2702. MSI_ADDR_DEST_ID(dest);
  2703. msg->data =
  2704. MSI_DATA_TRIGGER_EDGE |
  2705. MSI_DATA_LEVEL_ASSERT |
  2706. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2707. MSI_DATA_DELIVERY_FIXED:
  2708. MSI_DATA_DELIVERY_LOWPRI) |
  2709. MSI_DATA_VECTOR(cfg->vector);
  2710. }
  2711. return err;
  2712. }
  2713. #ifdef CONFIG_SMP
  2714. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2715. {
  2716. struct irq_cfg *cfg;
  2717. struct msi_msg msg;
  2718. unsigned int dest;
  2719. cpumask_t tmp;
  2720. struct irq_desc *desc;
  2721. cpus_and(tmp, mask, cpu_online_map);
  2722. if (cpus_empty(tmp))
  2723. return;
  2724. if (assign_irq_vector(irq, mask))
  2725. return;
  2726. cfg = irq_cfg(irq);
  2727. cpus_and(tmp, cfg->domain, mask);
  2728. dest = cpu_mask_to_apicid(tmp);
  2729. read_msi_msg(irq, &msg);
  2730. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2731. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2732. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2733. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2734. write_msi_msg(irq, &msg);
  2735. desc = irq_to_desc(irq);
  2736. desc->affinity = mask;
  2737. }
  2738. #ifdef CONFIG_INTR_REMAP
  2739. /*
  2740. * Migrate the MSI irq to another cpumask. This migration is
  2741. * done in the process context using interrupt-remapping hardware.
  2742. */
  2743. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2744. {
  2745. struct irq_cfg *cfg;
  2746. unsigned int dest;
  2747. cpumask_t tmp, cleanup_mask;
  2748. struct irte irte;
  2749. struct irq_desc *desc;
  2750. cpus_and(tmp, mask, cpu_online_map);
  2751. if (cpus_empty(tmp))
  2752. return;
  2753. if (get_irte(irq, &irte))
  2754. return;
  2755. if (assign_irq_vector(irq, mask))
  2756. return;
  2757. cfg = irq_cfg(irq);
  2758. cpus_and(tmp, cfg->domain, mask);
  2759. dest = cpu_mask_to_apicid(tmp);
  2760. irte.vector = cfg->vector;
  2761. irte.dest_id = IRTE_DEST(dest);
  2762. /*
  2763. * atomically update the IRTE with the new destination and vector.
  2764. */
  2765. modify_irte(irq, &irte);
  2766. /*
  2767. * After this point, all the interrupts will start arriving
  2768. * at the new destination. So, time to cleanup the previous
  2769. * vector allocation.
  2770. */
  2771. if (cfg->move_in_progress) {
  2772. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2773. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2774. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2775. cfg->move_in_progress = 0;
  2776. }
  2777. desc = irq_to_desc(irq);
  2778. desc->affinity = mask;
  2779. }
  2780. #endif
  2781. #endif /* CONFIG_SMP */
  2782. /*
  2783. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2784. * which implement the MSI or MSI-X Capability Structure.
  2785. */
  2786. static struct irq_chip msi_chip = {
  2787. .name = "PCI-MSI",
  2788. .unmask = unmask_msi_irq,
  2789. .mask = mask_msi_irq,
  2790. .ack = ack_apic_edge,
  2791. #ifdef CONFIG_SMP
  2792. .set_affinity = set_msi_irq_affinity,
  2793. #endif
  2794. .retrigger = ioapic_retrigger_irq,
  2795. };
  2796. #ifdef CONFIG_INTR_REMAP
  2797. static struct irq_chip msi_ir_chip = {
  2798. .name = "IR-PCI-MSI",
  2799. .unmask = unmask_msi_irq,
  2800. .mask = mask_msi_irq,
  2801. .ack = ack_x2apic_edge,
  2802. #ifdef CONFIG_SMP
  2803. .set_affinity = ir_set_msi_irq_affinity,
  2804. #endif
  2805. .retrigger = ioapic_retrigger_irq,
  2806. };
  2807. /*
  2808. * Map the PCI dev to the corresponding remapping hardware unit
  2809. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2810. * in it.
  2811. */
  2812. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2813. {
  2814. struct intel_iommu *iommu;
  2815. int index;
  2816. iommu = map_dev_to_ir(dev);
  2817. if (!iommu) {
  2818. printk(KERN_ERR
  2819. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2820. return -ENOENT;
  2821. }
  2822. index = alloc_irte(iommu, irq, nvec);
  2823. if (index < 0) {
  2824. printk(KERN_ERR
  2825. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2826. pci_name(dev));
  2827. return -ENOSPC;
  2828. }
  2829. return index;
  2830. }
  2831. #endif
  2832. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
  2833. {
  2834. int ret;
  2835. struct msi_msg msg;
  2836. ret = msi_compose_msg(dev, irq, &msg);
  2837. if (ret < 0)
  2838. return ret;
  2839. set_irq_msi(irq, desc);
  2840. write_msi_msg(irq, &msg);
  2841. #ifdef CONFIG_INTR_REMAP
  2842. if (irq_remapped(irq)) {
  2843. struct irq_desc *desc = irq_to_desc(irq);
  2844. /*
  2845. * irq migration in process context
  2846. */
  2847. desc->status |= IRQ_MOVE_PCNTXT;
  2848. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2849. } else
  2850. #endif
  2851. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2852. return 0;
  2853. }
  2854. static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
  2855. {
  2856. unsigned int irq;
  2857. irq = dev->bus->number;
  2858. irq <<= 8;
  2859. irq |= dev->devfn;
  2860. irq <<= 12;
  2861. return irq;
  2862. }
  2863. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2864. {
  2865. unsigned int irq;
  2866. int ret;
  2867. unsigned int irq_want;
  2868. irq_want = build_irq_for_pci_dev(dev) + 0x100;
  2869. irq = create_irq_nr(irq_want);
  2870. if (irq == 0)
  2871. return -1;
  2872. #ifdef CONFIG_INTR_REMAP
  2873. if (!intr_remapping_enabled)
  2874. goto no_ir;
  2875. ret = msi_alloc_irte(dev, irq, 1);
  2876. if (ret < 0)
  2877. goto error;
  2878. no_ir:
  2879. #endif
  2880. ret = setup_msi_irq(dev, desc, irq);
  2881. if (ret < 0) {
  2882. destroy_irq(irq);
  2883. return ret;
  2884. }
  2885. return 0;
  2886. #ifdef CONFIG_INTR_REMAP
  2887. error:
  2888. destroy_irq(irq);
  2889. return ret;
  2890. #endif
  2891. }
  2892. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2893. {
  2894. unsigned int irq;
  2895. int ret, sub_handle;
  2896. struct msi_desc *desc;
  2897. unsigned int irq_want;
  2898. #ifdef CONFIG_INTR_REMAP
  2899. struct intel_iommu *iommu = 0;
  2900. int index = 0;
  2901. #endif
  2902. irq_want = build_irq_for_pci_dev(dev) + 0x100;
  2903. sub_handle = 0;
  2904. list_for_each_entry(desc, &dev->msi_list, list) {
  2905. irq = create_irq_nr(irq_want--);
  2906. if (irq == 0)
  2907. return -1;
  2908. #ifdef CONFIG_INTR_REMAP
  2909. if (!intr_remapping_enabled)
  2910. goto no_ir;
  2911. if (!sub_handle) {
  2912. /*
  2913. * allocate the consecutive block of IRTE's
  2914. * for 'nvec'
  2915. */
  2916. index = msi_alloc_irte(dev, irq, nvec);
  2917. if (index < 0) {
  2918. ret = index;
  2919. goto error;
  2920. }
  2921. } else {
  2922. iommu = map_dev_to_ir(dev);
  2923. if (!iommu) {
  2924. ret = -ENOENT;
  2925. goto error;
  2926. }
  2927. /*
  2928. * setup the mapping between the irq and the IRTE
  2929. * base index, the sub_handle pointing to the
  2930. * appropriate interrupt remap table entry.
  2931. */
  2932. set_irte_irq(irq, iommu, index, sub_handle);
  2933. }
  2934. no_ir:
  2935. #endif
  2936. ret = setup_msi_irq(dev, desc, irq);
  2937. if (ret < 0)
  2938. goto error;
  2939. sub_handle++;
  2940. }
  2941. return 0;
  2942. error:
  2943. destroy_irq(irq);
  2944. return ret;
  2945. }
  2946. void arch_teardown_msi_irq(unsigned int irq)
  2947. {
  2948. destroy_irq(irq);
  2949. }
  2950. #ifdef CONFIG_DMAR
  2951. #ifdef CONFIG_SMP
  2952. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2953. {
  2954. struct irq_cfg *cfg;
  2955. struct msi_msg msg;
  2956. unsigned int dest;
  2957. cpumask_t tmp;
  2958. struct irq_desc *desc;
  2959. cpus_and(tmp, mask, cpu_online_map);
  2960. if (cpus_empty(tmp))
  2961. return;
  2962. if (assign_irq_vector(irq, mask))
  2963. return;
  2964. cfg = irq_cfg(irq);
  2965. cpus_and(tmp, cfg->domain, mask);
  2966. dest = cpu_mask_to_apicid(tmp);
  2967. dmar_msi_read(irq, &msg);
  2968. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2969. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2970. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2971. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2972. dmar_msi_write(irq, &msg);
  2973. desc = irq_to_desc(irq);
  2974. desc->affinity = mask;
  2975. }
  2976. #endif /* CONFIG_SMP */
  2977. struct irq_chip dmar_msi_type = {
  2978. .name = "DMAR_MSI",
  2979. .unmask = dmar_msi_unmask,
  2980. .mask = dmar_msi_mask,
  2981. .ack = ack_apic_edge,
  2982. #ifdef CONFIG_SMP
  2983. .set_affinity = dmar_msi_set_affinity,
  2984. #endif
  2985. .retrigger = ioapic_retrigger_irq,
  2986. };
  2987. int arch_setup_dmar_msi(unsigned int irq)
  2988. {
  2989. int ret;
  2990. struct msi_msg msg;
  2991. ret = msi_compose_msg(NULL, irq, &msg);
  2992. if (ret < 0)
  2993. return ret;
  2994. dmar_msi_write(irq, &msg);
  2995. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2996. "edge");
  2997. return 0;
  2998. }
  2999. #endif
  3000. #endif /* CONFIG_PCI_MSI */
  3001. /*
  3002. * Hypertransport interrupt support
  3003. */
  3004. #ifdef CONFIG_HT_IRQ
  3005. #ifdef CONFIG_SMP
  3006. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  3007. {
  3008. struct ht_irq_msg msg;
  3009. fetch_ht_irq_msg(irq, &msg);
  3010. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  3011. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  3012. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  3013. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  3014. write_ht_irq_msg(irq, &msg);
  3015. }
  3016. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  3017. {
  3018. struct irq_cfg *cfg;
  3019. unsigned int dest;
  3020. cpumask_t tmp;
  3021. struct irq_desc *desc;
  3022. cpus_and(tmp, mask, cpu_online_map);
  3023. if (cpus_empty(tmp))
  3024. return;
  3025. if (assign_irq_vector(irq, mask))
  3026. return;
  3027. cfg = irq_cfg(irq);
  3028. cpus_and(tmp, cfg->domain, mask);
  3029. dest = cpu_mask_to_apicid(tmp);
  3030. target_ht_irq(irq, dest, cfg->vector);
  3031. desc = irq_to_desc(irq);
  3032. desc->affinity = mask;
  3033. }
  3034. #endif
  3035. static struct irq_chip ht_irq_chip = {
  3036. .name = "PCI-HT",
  3037. .mask = mask_ht_irq,
  3038. .unmask = unmask_ht_irq,
  3039. .ack = ack_apic_edge,
  3040. #ifdef CONFIG_SMP
  3041. .set_affinity = set_ht_irq_affinity,
  3042. #endif
  3043. .retrigger = ioapic_retrigger_irq,
  3044. };
  3045. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  3046. {
  3047. struct irq_cfg *cfg;
  3048. int err;
  3049. cpumask_t tmp;
  3050. tmp = TARGET_CPUS;
  3051. err = assign_irq_vector(irq, tmp);
  3052. if (!err) {
  3053. struct ht_irq_msg msg;
  3054. unsigned dest;
  3055. cfg = irq_cfg(irq);
  3056. cpus_and(tmp, cfg->domain, tmp);
  3057. dest = cpu_mask_to_apicid(tmp);
  3058. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  3059. msg.address_lo =
  3060. HT_IRQ_LOW_BASE |
  3061. HT_IRQ_LOW_DEST_ID(dest) |
  3062. HT_IRQ_LOW_VECTOR(cfg->vector) |
  3063. ((INT_DEST_MODE == 0) ?
  3064. HT_IRQ_LOW_DM_PHYSICAL :
  3065. HT_IRQ_LOW_DM_LOGICAL) |
  3066. HT_IRQ_LOW_RQEOI_EDGE |
  3067. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  3068. HT_IRQ_LOW_MT_FIXED :
  3069. HT_IRQ_LOW_MT_ARBITRATED) |
  3070. HT_IRQ_LOW_IRQ_MASKED;
  3071. write_ht_irq_msg(irq, &msg);
  3072. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  3073. handle_edge_irq, "edge");
  3074. }
  3075. return err;
  3076. }
  3077. #endif /* CONFIG_HT_IRQ */
  3078. int __init io_apic_get_redir_entries (int ioapic)
  3079. {
  3080. union IO_APIC_reg_01 reg_01;
  3081. unsigned long flags;
  3082. spin_lock_irqsave(&ioapic_lock, flags);
  3083. reg_01.raw = io_apic_read(ioapic, 1);
  3084. spin_unlock_irqrestore(&ioapic_lock, flags);
  3085. return reg_01.bits.entries;
  3086. }
  3087. int __init probe_nr_irqs(void)
  3088. {
  3089. int idx;
  3090. int nr = 0;
  3091. #ifndef CONFIG_XEN
  3092. int nr_min = 32;
  3093. #else
  3094. int nr_min = NR_IRQS;
  3095. #endif
  3096. for (idx = 0; idx < nr_ioapics; idx++)
  3097. nr += io_apic_get_redir_entries(idx) + 1;
  3098. /* double it for hotplug and msi and nmi */
  3099. nr <<= 1;
  3100. /* something wrong ? */
  3101. if (nr < nr_min)
  3102. nr = nr_min;
  3103. return nr;
  3104. }
  3105. /* --------------------------------------------------------------------------
  3106. ACPI-based IOAPIC Configuration
  3107. -------------------------------------------------------------------------- */
  3108. #ifdef CONFIG_ACPI
  3109. #ifdef CONFIG_X86_32
  3110. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  3111. {
  3112. union IO_APIC_reg_00 reg_00;
  3113. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  3114. physid_mask_t tmp;
  3115. unsigned long flags;
  3116. int i = 0;
  3117. /*
  3118. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  3119. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  3120. * supports up to 16 on one shared APIC bus.
  3121. *
  3122. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  3123. * advantage of new APIC bus architecture.
  3124. */
  3125. if (physids_empty(apic_id_map))
  3126. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  3127. spin_lock_irqsave(&ioapic_lock, flags);
  3128. reg_00.raw = io_apic_read(ioapic, 0);
  3129. spin_unlock_irqrestore(&ioapic_lock, flags);
  3130. if (apic_id >= get_physical_broadcast()) {
  3131. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  3132. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  3133. apic_id = reg_00.bits.ID;
  3134. }
  3135. /*
  3136. * Every APIC in a system must have a unique ID or we get lots of nice
  3137. * 'stuck on smp_invalidate_needed IPI wait' messages.
  3138. */
  3139. if (check_apicid_used(apic_id_map, apic_id)) {
  3140. for (i = 0; i < get_physical_broadcast(); i++) {
  3141. if (!check_apicid_used(apic_id_map, i))
  3142. break;
  3143. }
  3144. if (i == get_physical_broadcast())
  3145. panic("Max apic_id exceeded!\n");
  3146. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  3147. "trying %d\n", ioapic, apic_id, i);
  3148. apic_id = i;
  3149. }
  3150. tmp = apicid_to_cpu_present(apic_id);
  3151. physids_or(apic_id_map, apic_id_map, tmp);
  3152. if (reg_00.bits.ID != apic_id) {
  3153. reg_00.bits.ID = apic_id;
  3154. spin_lock_irqsave(&ioapic_lock, flags);
  3155. io_apic_write(ioapic, 0, reg_00.raw);
  3156. reg_00.raw = io_apic_read(ioapic, 0);
  3157. spin_unlock_irqrestore(&ioapic_lock, flags);
  3158. /* Sanity check */
  3159. if (reg_00.bits.ID != apic_id) {
  3160. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  3161. return -1;
  3162. }
  3163. }
  3164. apic_printk(APIC_VERBOSE, KERN_INFO
  3165. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  3166. return apic_id;
  3167. }
  3168. int __init io_apic_get_version(int ioapic)
  3169. {
  3170. union IO_APIC_reg_01 reg_01;
  3171. unsigned long flags;
  3172. spin_lock_irqsave(&ioapic_lock, flags);
  3173. reg_01.raw = io_apic_read(ioapic, 1);
  3174. spin_unlock_irqrestore(&ioapic_lock, flags);
  3175. return reg_01.bits.version;
  3176. }
  3177. #endif
  3178. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  3179. {
  3180. if (!IO_APIC_IRQ(irq)) {
  3181. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  3182. ioapic);
  3183. return -EINVAL;
  3184. }
  3185. /*
  3186. * IRQs < 16 are already in the irq_2_pin[] map
  3187. */
  3188. if (irq >= 16)
  3189. add_pin_to_irq(irq, ioapic, pin);
  3190. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  3191. return 0;
  3192. }
  3193. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  3194. {
  3195. int i;
  3196. if (skip_ioapic_setup)
  3197. return -1;
  3198. for (i = 0; i < mp_irq_entries; i++)
  3199. if (mp_irqs[i].mp_irqtype == mp_INT &&
  3200. mp_irqs[i].mp_srcbusirq == bus_irq)
  3201. break;
  3202. if (i >= mp_irq_entries)
  3203. return -1;
  3204. *trigger = irq_trigger(i);
  3205. *polarity = irq_polarity(i);
  3206. return 0;
  3207. }
  3208. #endif /* CONFIG_ACPI */
  3209. /*
  3210. * This function currently is only a helper for the i386 smp boot process where
  3211. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  3212. * so mask in all cases should simply be TARGET_CPUS
  3213. */
  3214. #ifdef CONFIG_SMP
  3215. void __init setup_ioapic_dest(void)
  3216. {
  3217. int pin, ioapic, irq, irq_entry;
  3218. struct irq_cfg *cfg;
  3219. if (skip_ioapic_setup == 1)
  3220. return;
  3221. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  3222. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  3223. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  3224. if (irq_entry == -1)
  3225. continue;
  3226. irq = pin_2_irq(irq_entry, ioapic, pin);
  3227. /* setup_IO_APIC_irqs could fail to get vector for some device
  3228. * when you have too many devices, because at that time only boot
  3229. * cpu is online.
  3230. */
  3231. cfg = irq_cfg(irq);
  3232. if (!cfg->vector)
  3233. setup_IO_APIC_irq(ioapic, pin, irq,
  3234. irq_trigger(irq_entry),
  3235. irq_polarity(irq_entry));
  3236. #ifdef CONFIG_INTR_REMAP
  3237. else if (intr_remapping_enabled)
  3238. set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
  3239. #endif
  3240. else
  3241. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  3242. }
  3243. }
  3244. }
  3245. #endif
  3246. #define IOAPIC_RESOURCE_NAME_SIZE 11
  3247. static struct resource *ioapic_resources;
  3248. static struct resource * __init ioapic_setup_resources(void)
  3249. {
  3250. unsigned long n;
  3251. struct resource *res;
  3252. char *mem;
  3253. int i;
  3254. if (nr_ioapics <= 0)
  3255. return NULL;
  3256. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  3257. n *= nr_ioapics;
  3258. mem = alloc_bootmem(n);
  3259. res = (void *)mem;
  3260. if (mem != NULL) {
  3261. mem += sizeof(struct resource) * nr_ioapics;
  3262. for (i = 0; i < nr_ioapics; i++) {
  3263. res[i].name = mem;
  3264. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  3265. sprintf(mem, "IOAPIC %u", i);
  3266. mem += IOAPIC_RESOURCE_NAME_SIZE;
  3267. }
  3268. }
  3269. ioapic_resources = res;
  3270. return res;
  3271. }
  3272. void __init ioapic_init_mappings(void)
  3273. {
  3274. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  3275. int i;
  3276. struct resource *ioapic_res;
  3277. ioapic_res = ioapic_setup_resources();
  3278. for (i = 0; i < nr_ioapics; i++) {
  3279. if (smp_found_config) {
  3280. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  3281. #ifdef CONFIG_X86_32
  3282. if (!ioapic_phys) {
  3283. printk(KERN_ERR
  3284. "WARNING: bogus zero IO-APIC "
  3285. "address found in MPTABLE, "
  3286. "disabling IO/APIC support!\n");
  3287. smp_found_config = 0;
  3288. skip_ioapic_setup = 1;
  3289. goto fake_ioapic_page;
  3290. }
  3291. #endif
  3292. } else {
  3293. #ifdef CONFIG_X86_32
  3294. fake_ioapic_page:
  3295. #endif
  3296. ioapic_phys = (unsigned long)
  3297. alloc_bootmem_pages(PAGE_SIZE);
  3298. ioapic_phys = __pa(ioapic_phys);
  3299. }
  3300. set_fixmap_nocache(idx, ioapic_phys);
  3301. apic_printk(APIC_VERBOSE,
  3302. "mapped IOAPIC to %08lx (%08lx)\n",
  3303. __fix_to_virt(idx), ioapic_phys);
  3304. idx++;
  3305. if (ioapic_res != NULL) {
  3306. ioapic_res->start = ioapic_phys;
  3307. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  3308. ioapic_res++;
  3309. }
  3310. }
  3311. }
  3312. static int __init ioapic_insert_resources(void)
  3313. {
  3314. int i;
  3315. struct resource *r = ioapic_resources;
  3316. if (!r) {
  3317. printk(KERN_ERR
  3318. "IO APIC resources could be not be allocated.\n");
  3319. return -1;
  3320. }
  3321. for (i = 0; i < nr_ioapics; i++) {
  3322. insert_resource(&iomem_resource, r);
  3323. r++;
  3324. }
  3325. return 0;
  3326. }
  3327. /* Insert the IO APIC resources after PCI initialization has occured to handle
  3328. * IO APICS that are mapped in on a BAR in PCI space. */
  3329. late_initcall(ioapic_insert_resources);