io_apic_64.c 74 KB

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