io_apic_64.c 78 KB

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