io_apic.c 93 KB

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