io_apic_64.c 75 KB

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