io_apic.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. /*
  2. * Intel IO-APIC support for multi-Pentium hosts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000, 2009 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. * Historical information which is worth to be preserved:
  23. *
  24. * - SiS APIC rmw bug:
  25. *
  26. * We used to have a workaround for a bug in SiS chips which
  27. * required to rewrite the index register for a read-modify-write
  28. * operation as the chip lost the index information which was
  29. * setup for the read already. We cache the data now, so that
  30. * workaround has been removed.
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/init.h>
  35. #include <linux/delay.h>
  36. #include <linux/sched.h>
  37. #include <linux/pci.h>
  38. #include <linux/mc146818rtc.h>
  39. #include <linux/compiler.h>
  40. #include <linux/acpi.h>
  41. #include <linux/export.h>
  42. #include <linux/syscore_ops.h>
  43. #include <linux/freezer.h>
  44. #include <linux/kthread.h>
  45. #include <linux/jiffies.h> /* time_after() */
  46. #include <linux/slab.h>
  47. #include <linux/bootmem.h>
  48. #include <asm/irqdomain.h>
  49. #include <asm/io.h>
  50. #include <asm/smp.h>
  51. #include <asm/cpu.h>
  52. #include <asm/desc.h>
  53. #include <asm/proto.h>
  54. #include <asm/acpi.h>
  55. #include <asm/dma.h>
  56. #include <asm/timer.h>
  57. #include <asm/i8259.h>
  58. #include <asm/setup.h>
  59. #include <asm/irq_remapping.h>
  60. #include <asm/hw_irq.h>
  61. #include <asm/apic.h>
  62. #define for_each_ioapic(idx) \
  63. for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
  64. #define for_each_ioapic_reverse(idx) \
  65. for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
  66. #define for_each_pin(idx, pin) \
  67. for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
  68. #define for_each_ioapic_pin(idx, pin) \
  69. for_each_ioapic((idx)) \
  70. for_each_pin((idx), (pin))
  71. #define for_each_irq_pin(entry, head) \
  72. list_for_each_entry(entry, &head, list)
  73. static DEFINE_RAW_SPINLOCK(ioapic_lock);
  74. static DEFINE_MUTEX(ioapic_mutex);
  75. static unsigned int ioapic_dynirq_base;
  76. static int ioapic_initialized;
  77. struct irq_pin_list {
  78. struct list_head list;
  79. int apic, pin;
  80. };
  81. struct mp_chip_data {
  82. struct list_head irq_2_pin;
  83. struct IO_APIC_route_entry entry;
  84. int trigger;
  85. int polarity;
  86. u32 count;
  87. bool isa_irq;
  88. };
  89. struct mp_ioapic_gsi {
  90. u32 gsi_base;
  91. u32 gsi_end;
  92. };
  93. static struct ioapic {
  94. /*
  95. * # of IRQ routing registers
  96. */
  97. int nr_registers;
  98. /*
  99. * Saved state during suspend/resume, or while enabling intr-remap.
  100. */
  101. struct IO_APIC_route_entry *saved_registers;
  102. /* I/O APIC config */
  103. struct mpc_ioapic mp_config;
  104. /* IO APIC gsi routing info */
  105. struct mp_ioapic_gsi gsi_config;
  106. struct ioapic_domain_cfg irqdomain_cfg;
  107. struct irq_domain *irqdomain;
  108. struct resource *iomem_res;
  109. } ioapics[MAX_IO_APICS];
  110. #define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
  111. int mpc_ioapic_id(int ioapic_idx)
  112. {
  113. return ioapics[ioapic_idx].mp_config.apicid;
  114. }
  115. unsigned int mpc_ioapic_addr(int ioapic_idx)
  116. {
  117. return ioapics[ioapic_idx].mp_config.apicaddr;
  118. }
  119. static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
  120. {
  121. return &ioapics[ioapic_idx].gsi_config;
  122. }
  123. static inline int mp_ioapic_pin_count(int ioapic)
  124. {
  125. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  126. return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
  127. }
  128. static inline u32 mp_pin_to_gsi(int ioapic, int pin)
  129. {
  130. return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
  131. }
  132. static inline bool mp_is_legacy_irq(int irq)
  133. {
  134. return irq >= 0 && irq < nr_legacy_irqs();
  135. }
  136. /*
  137. * Initialize all legacy IRQs and all pins on the first IOAPIC
  138. * if we have legacy interrupt controller. Kernel boot option "pirq="
  139. * may rely on non-legacy pins on the first IOAPIC.
  140. */
  141. static inline int mp_init_irq_at_boot(int ioapic, int irq)
  142. {
  143. if (!nr_legacy_irqs())
  144. return 0;
  145. return ioapic == 0 || mp_is_legacy_irq(irq);
  146. }
  147. static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
  148. {
  149. return ioapics[ioapic].irqdomain;
  150. }
  151. int nr_ioapics;
  152. /* The one past the highest gsi number used */
  153. u32 gsi_top;
  154. /* MP IRQ source entries */
  155. struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
  156. /* # of MP IRQ source entries */
  157. int mp_irq_entries;
  158. #ifdef CONFIG_EISA
  159. int mp_bus_id_to_type[MAX_MP_BUSSES];
  160. #endif
  161. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  162. int skip_ioapic_setup;
  163. /**
  164. * disable_ioapic_support() - disables ioapic support at runtime
  165. */
  166. void disable_ioapic_support(void)
  167. {
  168. #ifdef CONFIG_PCI
  169. noioapicquirk = 1;
  170. noioapicreroute = -1;
  171. #endif
  172. skip_ioapic_setup = 1;
  173. }
  174. static int __init parse_noapic(char *str)
  175. {
  176. /* disable IO-APIC */
  177. disable_ioapic_support();
  178. return 0;
  179. }
  180. early_param("noapic", parse_noapic);
  181. /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
  182. void mp_save_irq(struct mpc_intsrc *m)
  183. {
  184. int i;
  185. apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
  186. " IRQ %02x, APIC ID %x, APIC INT %02x\n",
  187. m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
  188. m->srcbusirq, m->dstapic, m->dstirq);
  189. for (i = 0; i < mp_irq_entries; i++) {
  190. if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
  191. return;
  192. }
  193. memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
  194. if (++mp_irq_entries == MAX_IRQ_SOURCES)
  195. panic("Max # of irq sources exceeded!!\n");
  196. }
  197. static void alloc_ioapic_saved_registers(int idx)
  198. {
  199. size_t size;
  200. if (ioapics[idx].saved_registers)
  201. return;
  202. size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
  203. ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
  204. if (!ioapics[idx].saved_registers)
  205. pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
  206. }
  207. static void free_ioapic_saved_registers(int idx)
  208. {
  209. kfree(ioapics[idx].saved_registers);
  210. ioapics[idx].saved_registers = NULL;
  211. }
  212. int __init arch_early_ioapic_init(void)
  213. {
  214. int i;
  215. if (!nr_legacy_irqs())
  216. io_apic_irqs = ~0UL;
  217. for_each_ioapic(i)
  218. alloc_ioapic_saved_registers(i);
  219. return 0;
  220. }
  221. struct io_apic {
  222. unsigned int index;
  223. unsigned int unused[3];
  224. unsigned int data;
  225. unsigned int unused2[11];
  226. unsigned int eoi;
  227. };
  228. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  229. {
  230. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  231. + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
  232. }
  233. static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
  234. {
  235. struct io_apic __iomem *io_apic = io_apic_base(apic);
  236. writel(vector, &io_apic->eoi);
  237. }
  238. unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
  239. {
  240. struct io_apic __iomem *io_apic = io_apic_base(apic);
  241. writel(reg, &io_apic->index);
  242. return readl(&io_apic->data);
  243. }
  244. static void io_apic_write(unsigned int apic, unsigned int reg,
  245. unsigned int value)
  246. {
  247. struct io_apic __iomem *io_apic = io_apic_base(apic);
  248. writel(reg, &io_apic->index);
  249. writel(value, &io_apic->data);
  250. }
  251. union entry_union {
  252. struct { u32 w1, w2; };
  253. struct IO_APIC_route_entry entry;
  254. };
  255. static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
  256. {
  257. union entry_union eu;
  258. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  259. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  260. return eu.entry;
  261. }
  262. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  263. {
  264. union entry_union eu;
  265. unsigned long flags;
  266. raw_spin_lock_irqsave(&ioapic_lock, flags);
  267. eu.entry = __ioapic_read_entry(apic, pin);
  268. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  269. return eu.entry;
  270. }
  271. /*
  272. * When we write a new IO APIC routing entry, we need to write the high
  273. * word first! If the mask bit in the low word is clear, we will enable
  274. * the interrupt, and we need to make sure the entry is fully populated
  275. * before that happens.
  276. */
  277. static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  278. {
  279. union entry_union eu = {{0, 0}};
  280. eu.entry = e;
  281. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  282. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  283. }
  284. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  285. {
  286. unsigned long flags;
  287. raw_spin_lock_irqsave(&ioapic_lock, flags);
  288. __ioapic_write_entry(apic, pin, e);
  289. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  290. }
  291. /*
  292. * When we mask an IO APIC routing entry, we need to write the low
  293. * word first, in order to set the mask bit before we change the
  294. * high bits!
  295. */
  296. static void ioapic_mask_entry(int apic, int pin)
  297. {
  298. unsigned long flags;
  299. union entry_union eu = { .entry.mask = IOAPIC_MASKED };
  300. raw_spin_lock_irqsave(&ioapic_lock, flags);
  301. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  302. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  303. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  304. }
  305. /*
  306. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  307. * shared ISA-space IRQs, so we have to support them. We are super
  308. * fast in the common case, and fast for shared ISA-space IRQs.
  309. */
  310. static int __add_pin_to_irq_node(struct mp_chip_data *data,
  311. int node, int apic, int pin)
  312. {
  313. struct irq_pin_list *entry;
  314. /* don't allow duplicates */
  315. for_each_irq_pin(entry, data->irq_2_pin)
  316. if (entry->apic == apic && entry->pin == pin)
  317. return 0;
  318. entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node);
  319. if (!entry) {
  320. pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
  321. node, apic, pin);
  322. return -ENOMEM;
  323. }
  324. entry->apic = apic;
  325. entry->pin = pin;
  326. list_add_tail(&entry->list, &data->irq_2_pin);
  327. return 0;
  328. }
  329. static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin)
  330. {
  331. struct irq_pin_list *tmp, *entry;
  332. list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list)
  333. if (entry->apic == apic && entry->pin == pin) {
  334. list_del(&entry->list);
  335. kfree(entry);
  336. return;
  337. }
  338. }
  339. static void add_pin_to_irq_node(struct mp_chip_data *data,
  340. int node, int apic, int pin)
  341. {
  342. if (__add_pin_to_irq_node(data, node, apic, pin))
  343. panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
  344. }
  345. /*
  346. * Reroute an IRQ to a different pin.
  347. */
  348. static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node,
  349. int oldapic, int oldpin,
  350. int newapic, int newpin)
  351. {
  352. struct irq_pin_list *entry;
  353. for_each_irq_pin(entry, data->irq_2_pin) {
  354. if (entry->apic == oldapic && entry->pin == oldpin) {
  355. entry->apic = newapic;
  356. entry->pin = newpin;
  357. /* every one is different, right? */
  358. return;
  359. }
  360. }
  361. /* old apic/pin didn't exist, so just add new ones */
  362. add_pin_to_irq_node(data, node, newapic, newpin);
  363. }
  364. static void io_apic_modify_irq(struct mp_chip_data *data,
  365. int mask_and, int mask_or,
  366. void (*final)(struct irq_pin_list *entry))
  367. {
  368. union entry_union eu;
  369. struct irq_pin_list *entry;
  370. eu.entry = data->entry;
  371. eu.w1 &= mask_and;
  372. eu.w1 |= mask_or;
  373. data->entry = eu.entry;
  374. for_each_irq_pin(entry, data->irq_2_pin) {
  375. io_apic_write(entry->apic, 0x10 + 2 * entry->pin, eu.w1);
  376. if (final)
  377. final(entry);
  378. }
  379. }
  380. static void io_apic_sync(struct irq_pin_list *entry)
  381. {
  382. /*
  383. * Synchronize the IO-APIC and the CPU by doing
  384. * a dummy read from the IO-APIC
  385. */
  386. struct io_apic __iomem *io_apic;
  387. io_apic = io_apic_base(entry->apic);
  388. readl(&io_apic->data);
  389. }
  390. static void mask_ioapic_irq(struct irq_data *irq_data)
  391. {
  392. struct mp_chip_data *data = irq_data->chip_data;
  393. unsigned long flags;
  394. raw_spin_lock_irqsave(&ioapic_lock, flags);
  395. io_apic_modify_irq(data, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
  396. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  397. }
  398. static void __unmask_ioapic(struct mp_chip_data *data)
  399. {
  400. io_apic_modify_irq(data, ~IO_APIC_REDIR_MASKED, 0, NULL);
  401. }
  402. static void unmask_ioapic_irq(struct irq_data *irq_data)
  403. {
  404. struct mp_chip_data *data = irq_data->chip_data;
  405. unsigned long flags;
  406. raw_spin_lock_irqsave(&ioapic_lock, flags);
  407. __unmask_ioapic(data);
  408. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  409. }
  410. /*
  411. * IO-APIC versions below 0x20 don't support EOI register.
  412. * For the record, here is the information about various versions:
  413. * 0Xh 82489DX
  414. * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
  415. * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
  416. * 30h-FFh Reserved
  417. *
  418. * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
  419. * version as 0x2. This is an error with documentation and these ICH chips
  420. * use io-apic's of version 0x20.
  421. *
  422. * For IO-APIC's with EOI register, we use that to do an explicit EOI.
  423. * Otherwise, we simulate the EOI message manually by changing the trigger
  424. * mode to edge and then back to level, with RTE being masked during this.
  425. */
  426. static void __eoi_ioapic_pin(int apic, int pin, int vector)
  427. {
  428. if (mpc_ioapic_ver(apic) >= 0x20) {
  429. io_apic_eoi(apic, vector);
  430. } else {
  431. struct IO_APIC_route_entry entry, entry1;
  432. entry = entry1 = __ioapic_read_entry(apic, pin);
  433. /*
  434. * Mask the entry and change the trigger mode to edge.
  435. */
  436. entry1.mask = IOAPIC_MASKED;
  437. entry1.trigger = IOAPIC_EDGE;
  438. __ioapic_write_entry(apic, pin, entry1);
  439. /*
  440. * Restore the previous level triggered entry.
  441. */
  442. __ioapic_write_entry(apic, pin, entry);
  443. }
  444. }
  445. static void eoi_ioapic_pin(int vector, struct mp_chip_data *data)
  446. {
  447. unsigned long flags;
  448. struct irq_pin_list *entry;
  449. raw_spin_lock_irqsave(&ioapic_lock, flags);
  450. for_each_irq_pin(entry, data->irq_2_pin)
  451. __eoi_ioapic_pin(entry->apic, entry->pin, vector);
  452. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  453. }
  454. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  455. {
  456. struct IO_APIC_route_entry entry;
  457. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  458. entry = ioapic_read_entry(apic, pin);
  459. if (entry.delivery_mode == dest_SMI)
  460. return;
  461. /*
  462. * Make sure the entry is masked and re-read the contents to check
  463. * if it is a level triggered pin and if the remote-IRR is set.
  464. */
  465. if (entry.mask == IOAPIC_UNMASKED) {
  466. entry.mask = IOAPIC_MASKED;
  467. ioapic_write_entry(apic, pin, entry);
  468. entry = ioapic_read_entry(apic, pin);
  469. }
  470. if (entry.irr) {
  471. unsigned long flags;
  472. /*
  473. * Make sure the trigger mode is set to level. Explicit EOI
  474. * doesn't clear the remote-IRR if the trigger mode is not
  475. * set to level.
  476. */
  477. if (entry.trigger == IOAPIC_EDGE) {
  478. entry.trigger = IOAPIC_LEVEL;
  479. ioapic_write_entry(apic, pin, entry);
  480. }
  481. raw_spin_lock_irqsave(&ioapic_lock, flags);
  482. __eoi_ioapic_pin(apic, pin, entry.vector);
  483. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  484. }
  485. /*
  486. * Clear the rest of the bits in the IO-APIC RTE except for the mask
  487. * bit.
  488. */
  489. ioapic_mask_entry(apic, pin);
  490. entry = ioapic_read_entry(apic, pin);
  491. if (entry.irr)
  492. pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
  493. mpc_ioapic_id(apic), pin);
  494. }
  495. static void clear_IO_APIC (void)
  496. {
  497. int apic, pin;
  498. for_each_ioapic_pin(apic, pin)
  499. clear_IO_APIC_pin(apic, pin);
  500. }
  501. #ifdef CONFIG_X86_32
  502. /*
  503. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  504. * specific CPU-side IRQs.
  505. */
  506. #define MAX_PIRQS 8
  507. static int pirq_entries[MAX_PIRQS] = {
  508. [0 ... MAX_PIRQS - 1] = -1
  509. };
  510. static int __init ioapic_pirq_setup(char *str)
  511. {
  512. int i, max;
  513. int ints[MAX_PIRQS+1];
  514. get_options(str, ARRAY_SIZE(ints), ints);
  515. apic_printk(APIC_VERBOSE, KERN_INFO
  516. "PIRQ redirection, working around broken MP-BIOS.\n");
  517. max = MAX_PIRQS;
  518. if (ints[0] < MAX_PIRQS)
  519. max = ints[0];
  520. for (i = 0; i < max; i++) {
  521. apic_printk(APIC_VERBOSE, KERN_DEBUG
  522. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  523. /*
  524. * PIRQs are mapped upside down, usually.
  525. */
  526. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  527. }
  528. return 1;
  529. }
  530. __setup("pirq=", ioapic_pirq_setup);
  531. #endif /* CONFIG_X86_32 */
  532. /*
  533. * Saves all the IO-APIC RTE's
  534. */
  535. int save_ioapic_entries(void)
  536. {
  537. int apic, pin;
  538. int err = 0;
  539. for_each_ioapic(apic) {
  540. if (!ioapics[apic].saved_registers) {
  541. err = -ENOMEM;
  542. continue;
  543. }
  544. for_each_pin(apic, pin)
  545. ioapics[apic].saved_registers[pin] =
  546. ioapic_read_entry(apic, pin);
  547. }
  548. return err;
  549. }
  550. /*
  551. * Mask all IO APIC entries.
  552. */
  553. void mask_ioapic_entries(void)
  554. {
  555. int apic, pin;
  556. for_each_ioapic(apic) {
  557. if (!ioapics[apic].saved_registers)
  558. continue;
  559. for_each_pin(apic, pin) {
  560. struct IO_APIC_route_entry entry;
  561. entry = ioapics[apic].saved_registers[pin];
  562. if (entry.mask == IOAPIC_UNMASKED) {
  563. entry.mask = IOAPIC_MASKED;
  564. ioapic_write_entry(apic, pin, entry);
  565. }
  566. }
  567. }
  568. }
  569. /*
  570. * Restore IO APIC entries which was saved in the ioapic structure.
  571. */
  572. int restore_ioapic_entries(void)
  573. {
  574. int apic, pin;
  575. for_each_ioapic(apic) {
  576. if (!ioapics[apic].saved_registers)
  577. continue;
  578. for_each_pin(apic, pin)
  579. ioapic_write_entry(apic, pin,
  580. ioapics[apic].saved_registers[pin]);
  581. }
  582. return 0;
  583. }
  584. /*
  585. * Find the IRQ entry number of a certain pin.
  586. */
  587. static int find_irq_entry(int ioapic_idx, int pin, int type)
  588. {
  589. int i;
  590. for (i = 0; i < mp_irq_entries; i++)
  591. if (mp_irqs[i].irqtype == type &&
  592. (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
  593. mp_irqs[i].dstapic == MP_APIC_ALL) &&
  594. mp_irqs[i].dstirq == pin)
  595. return i;
  596. return -1;
  597. }
  598. /*
  599. * Find the pin to which IRQ[irq] (ISA) is connected
  600. */
  601. static int __init find_isa_irq_pin(int irq, int type)
  602. {
  603. int i;
  604. for (i = 0; i < mp_irq_entries; i++) {
  605. int lbus = mp_irqs[i].srcbus;
  606. if (test_bit(lbus, mp_bus_not_pci) &&
  607. (mp_irqs[i].irqtype == type) &&
  608. (mp_irqs[i].srcbusirq == irq))
  609. return mp_irqs[i].dstirq;
  610. }
  611. return -1;
  612. }
  613. static int __init find_isa_irq_apic(int irq, int type)
  614. {
  615. int i;
  616. for (i = 0; i < mp_irq_entries; i++) {
  617. int lbus = mp_irqs[i].srcbus;
  618. if (test_bit(lbus, mp_bus_not_pci) &&
  619. (mp_irqs[i].irqtype == type) &&
  620. (mp_irqs[i].srcbusirq == irq))
  621. break;
  622. }
  623. if (i < mp_irq_entries) {
  624. int ioapic_idx;
  625. for_each_ioapic(ioapic_idx)
  626. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
  627. return ioapic_idx;
  628. }
  629. return -1;
  630. }
  631. #ifdef CONFIG_EISA
  632. /*
  633. * EISA Edge/Level control register, ELCR
  634. */
  635. static int EISA_ELCR(unsigned int irq)
  636. {
  637. if (irq < nr_legacy_irqs()) {
  638. unsigned int port = 0x4d0 + (irq >> 3);
  639. return (inb(port) >> (irq & 7)) & 1;
  640. }
  641. apic_printk(APIC_VERBOSE, KERN_INFO
  642. "Broken MPtable reports ISA irq %d\n", irq);
  643. return 0;
  644. }
  645. #endif
  646. /* ISA interrupts are always active high edge triggered,
  647. * when listed as conforming in the MP table. */
  648. #define default_ISA_trigger(idx) (IOAPIC_EDGE)
  649. #define default_ISA_polarity(idx) (IOAPIC_POL_HIGH)
  650. /* EISA interrupts are always polarity zero and can be edge or level
  651. * trigger depending on the ELCR value. If an interrupt is listed as
  652. * EISA conforming in the MP table, that means its trigger type must
  653. * be read in from the ELCR */
  654. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
  655. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  656. /* PCI interrupts are always active low level triggered,
  657. * when listed as conforming in the MP table. */
  658. #define default_PCI_trigger(idx) (IOAPIC_LEVEL)
  659. #define default_PCI_polarity(idx) (IOAPIC_POL_LOW)
  660. static int irq_polarity(int idx)
  661. {
  662. int bus = mp_irqs[idx].srcbus;
  663. /*
  664. * Determine IRQ line polarity (high active or low active):
  665. */
  666. switch (mp_irqs[idx].irqflag & 0x03) {
  667. case 0:
  668. /* conforms to spec, ie. bus-type dependent polarity */
  669. if (test_bit(bus, mp_bus_not_pci))
  670. return default_ISA_polarity(idx);
  671. else
  672. return default_PCI_polarity(idx);
  673. case 1:
  674. return IOAPIC_POL_HIGH;
  675. case 2:
  676. pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n");
  677. case 3:
  678. default: /* Pointless default required due to do gcc stupidity */
  679. return IOAPIC_POL_LOW;
  680. }
  681. }
  682. #ifdef CONFIG_EISA
  683. static int eisa_irq_trigger(int idx, int bus, int trigger)
  684. {
  685. switch (mp_bus_id_to_type[bus]) {
  686. case MP_BUS_PCI:
  687. case MP_BUS_ISA:
  688. return trigger;
  689. case MP_BUS_EISA:
  690. return default_EISA_trigger(idx);
  691. }
  692. pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus);
  693. return IOAPIC_LEVEL;
  694. }
  695. #else
  696. static inline int eisa_irq_trigger(int idx, int bus, int trigger)
  697. {
  698. return trigger;
  699. }
  700. #endif
  701. static int irq_trigger(int idx)
  702. {
  703. int bus = mp_irqs[idx].srcbus;
  704. int trigger;
  705. /*
  706. * Determine IRQ trigger mode (edge or level sensitive):
  707. */
  708. switch ((mp_irqs[idx].irqflag >> 2) & 0x03) {
  709. case 0:
  710. /* conforms to spec, ie. bus-type dependent trigger mode */
  711. if (test_bit(bus, mp_bus_not_pci))
  712. trigger = default_ISA_trigger(idx);
  713. else
  714. trigger = default_PCI_trigger(idx);
  715. /* Take EISA into account */
  716. return eisa_irq_trigger(idx, bus, trigger);
  717. case 1:
  718. return IOAPIC_EDGE;
  719. case 2:
  720. pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n");
  721. case 3:
  722. default: /* Pointless default required due to do gcc stupidity */
  723. return IOAPIC_LEVEL;
  724. }
  725. }
  726. void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node,
  727. int trigger, int polarity)
  728. {
  729. init_irq_alloc_info(info, NULL);
  730. info->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
  731. info->ioapic_node = node;
  732. info->ioapic_trigger = trigger;
  733. info->ioapic_polarity = polarity;
  734. info->ioapic_valid = 1;
  735. }
  736. #ifndef CONFIG_ACPI
  737. int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity);
  738. #endif
  739. static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst,
  740. struct irq_alloc_info *src,
  741. u32 gsi, int ioapic_idx, int pin)
  742. {
  743. int trigger, polarity;
  744. copy_irq_alloc_info(dst, src);
  745. dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC;
  746. dst->ioapic_id = mpc_ioapic_id(ioapic_idx);
  747. dst->ioapic_pin = pin;
  748. dst->ioapic_valid = 1;
  749. if (src && src->ioapic_valid) {
  750. dst->ioapic_node = src->ioapic_node;
  751. dst->ioapic_trigger = src->ioapic_trigger;
  752. dst->ioapic_polarity = src->ioapic_polarity;
  753. } else {
  754. dst->ioapic_node = NUMA_NO_NODE;
  755. if (acpi_get_override_irq(gsi, &trigger, &polarity) >= 0) {
  756. dst->ioapic_trigger = trigger;
  757. dst->ioapic_polarity = polarity;
  758. } else {
  759. /*
  760. * PCI interrupts are always active low level
  761. * triggered.
  762. */
  763. dst->ioapic_trigger = IOAPIC_LEVEL;
  764. dst->ioapic_polarity = IOAPIC_POL_LOW;
  765. }
  766. }
  767. }
  768. static int ioapic_alloc_attr_node(struct irq_alloc_info *info)
  769. {
  770. return (info && info->ioapic_valid) ? info->ioapic_node : NUMA_NO_NODE;
  771. }
  772. static void mp_register_handler(unsigned int irq, unsigned long trigger)
  773. {
  774. irq_flow_handler_t hdl;
  775. bool fasteoi;
  776. if (trigger) {
  777. irq_set_status_flags(irq, IRQ_LEVEL);
  778. fasteoi = true;
  779. } else {
  780. irq_clear_status_flags(irq, IRQ_LEVEL);
  781. fasteoi = false;
  782. }
  783. hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
  784. __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge");
  785. }
  786. static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info)
  787. {
  788. struct mp_chip_data *data = irq_get_chip_data(irq);
  789. /*
  790. * setup_IO_APIC_irqs() programs all legacy IRQs with default trigger
  791. * and polarity attirbutes. So allow the first user to reprogram the
  792. * pin with real trigger and polarity attributes.
  793. */
  794. if (irq < nr_legacy_irqs() && data->count == 1) {
  795. if (info->ioapic_trigger != data->trigger)
  796. mp_register_handler(irq, info->ioapic_trigger);
  797. data->entry.trigger = data->trigger = info->ioapic_trigger;
  798. data->entry.polarity = data->polarity = info->ioapic_polarity;
  799. }
  800. return data->trigger == info->ioapic_trigger &&
  801. data->polarity == info->ioapic_polarity;
  802. }
  803. static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi,
  804. struct irq_alloc_info *info)
  805. {
  806. bool legacy = false;
  807. int irq = -1;
  808. int type = ioapics[ioapic].irqdomain_cfg.type;
  809. switch (type) {
  810. case IOAPIC_DOMAIN_LEGACY:
  811. /*
  812. * Dynamically allocate IRQ number for non-ISA IRQs in the first
  813. * 16 GSIs on some weird platforms.
  814. */
  815. if (!ioapic_initialized || gsi >= nr_legacy_irqs())
  816. irq = gsi;
  817. legacy = mp_is_legacy_irq(irq);
  818. break;
  819. case IOAPIC_DOMAIN_STRICT:
  820. irq = gsi;
  821. break;
  822. case IOAPIC_DOMAIN_DYNAMIC:
  823. break;
  824. default:
  825. WARN(1, "ioapic: unknown irqdomain type %d\n", type);
  826. return -1;
  827. }
  828. return __irq_domain_alloc_irqs(domain, irq, 1,
  829. ioapic_alloc_attr_node(info),
  830. info, legacy, NULL);
  831. }
  832. /*
  833. * Need special handling for ISA IRQs because there may be multiple IOAPIC pins
  834. * sharing the same ISA IRQ number and irqdomain only supports 1:1 mapping
  835. * between IOAPIC pin and IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are
  836. * used for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
  837. * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are available, and
  838. * some BIOSes may use MP Interrupt Source records to override IRQ numbers for
  839. * PIRQs instead of reprogramming the interrupt routing logic. Thus there may be
  840. * multiple pins sharing the same legacy IRQ number when ACPI is disabled.
  841. */
  842. static int alloc_isa_irq_from_domain(struct irq_domain *domain,
  843. int irq, int ioapic, int pin,
  844. struct irq_alloc_info *info)
  845. {
  846. struct mp_chip_data *data;
  847. struct irq_data *irq_data = irq_get_irq_data(irq);
  848. int node = ioapic_alloc_attr_node(info);
  849. /*
  850. * Legacy ISA IRQ has already been allocated, just add pin to
  851. * the pin list assoicated with this IRQ and program the IOAPIC
  852. * entry. The IOAPIC entry
  853. */
  854. if (irq_data && irq_data->parent_data) {
  855. if (!mp_check_pin_attr(irq, info))
  856. return -EBUSY;
  857. if (__add_pin_to_irq_node(irq_data->chip_data, node, ioapic,
  858. info->ioapic_pin))
  859. return -ENOMEM;
  860. } else {
  861. irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true,
  862. NULL);
  863. if (irq >= 0) {
  864. irq_data = irq_domain_get_irq_data(domain, irq);
  865. data = irq_data->chip_data;
  866. data->isa_irq = true;
  867. }
  868. }
  869. return irq;
  870. }
  871. static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
  872. unsigned int flags, struct irq_alloc_info *info)
  873. {
  874. int irq;
  875. bool legacy = false;
  876. struct irq_alloc_info tmp;
  877. struct mp_chip_data *data;
  878. struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
  879. if (!domain)
  880. return -ENOSYS;
  881. if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
  882. irq = mp_irqs[idx].srcbusirq;
  883. legacy = mp_is_legacy_irq(irq);
  884. }
  885. mutex_lock(&ioapic_mutex);
  886. if (!(flags & IOAPIC_MAP_ALLOC)) {
  887. if (!legacy) {
  888. irq = irq_find_mapping(domain, pin);
  889. if (irq == 0)
  890. irq = -ENOENT;
  891. }
  892. } else {
  893. ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin);
  894. if (legacy)
  895. irq = alloc_isa_irq_from_domain(domain, irq,
  896. ioapic, pin, &tmp);
  897. else if ((irq = irq_find_mapping(domain, pin)) == 0)
  898. irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp);
  899. else if (!mp_check_pin_attr(irq, &tmp))
  900. irq = -EBUSY;
  901. if (irq >= 0) {
  902. data = irq_get_chip_data(irq);
  903. data->count++;
  904. }
  905. }
  906. mutex_unlock(&ioapic_mutex);
  907. return irq;
  908. }
  909. static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
  910. {
  911. u32 gsi = mp_pin_to_gsi(ioapic, pin);
  912. /*
  913. * Debugging check, we are in big trouble if this message pops up!
  914. */
  915. if (mp_irqs[idx].dstirq != pin)
  916. pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
  917. #ifdef CONFIG_X86_32
  918. /*
  919. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  920. */
  921. if ((pin >= 16) && (pin <= 23)) {
  922. if (pirq_entries[pin-16] != -1) {
  923. if (!pirq_entries[pin-16]) {
  924. apic_printk(APIC_VERBOSE, KERN_DEBUG
  925. "disabling PIRQ%d\n", pin-16);
  926. } else {
  927. int irq = pirq_entries[pin-16];
  928. apic_printk(APIC_VERBOSE, KERN_DEBUG
  929. "using PIRQ%d -> IRQ %d\n",
  930. pin-16, irq);
  931. return irq;
  932. }
  933. }
  934. }
  935. #endif
  936. return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL);
  937. }
  938. int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info)
  939. {
  940. int ioapic, pin, idx;
  941. ioapic = mp_find_ioapic(gsi);
  942. if (ioapic < 0)
  943. return -1;
  944. pin = mp_find_ioapic_pin(ioapic, gsi);
  945. idx = find_irq_entry(ioapic, pin, mp_INT);
  946. if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
  947. return -1;
  948. return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info);
  949. }
  950. void mp_unmap_irq(int irq)
  951. {
  952. struct irq_data *irq_data = irq_get_irq_data(irq);
  953. struct mp_chip_data *data;
  954. if (!irq_data || !irq_data->domain)
  955. return;
  956. data = irq_data->chip_data;
  957. if (!data || data->isa_irq)
  958. return;
  959. mutex_lock(&ioapic_mutex);
  960. if (--data->count == 0)
  961. irq_domain_free_irqs(irq, 1);
  962. mutex_unlock(&ioapic_mutex);
  963. }
  964. /*
  965. * Find a specific PCI IRQ entry.
  966. * Not an __init, possibly needed by modules
  967. */
  968. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  969. {
  970. int irq, i, best_ioapic = -1, best_idx = -1;
  971. apic_printk(APIC_DEBUG,
  972. "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  973. bus, slot, pin);
  974. if (test_bit(bus, mp_bus_not_pci)) {
  975. apic_printk(APIC_VERBOSE,
  976. "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  977. return -1;
  978. }
  979. for (i = 0; i < mp_irq_entries; i++) {
  980. int lbus = mp_irqs[i].srcbus;
  981. int ioapic_idx, found = 0;
  982. if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
  983. slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
  984. continue;
  985. for_each_ioapic(ioapic_idx)
  986. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
  987. mp_irqs[i].dstapic == MP_APIC_ALL) {
  988. found = 1;
  989. break;
  990. }
  991. if (!found)
  992. continue;
  993. /* Skip ISA IRQs */
  994. irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
  995. if (irq > 0 && !IO_APIC_IRQ(irq))
  996. continue;
  997. if (pin == (mp_irqs[i].srcbusirq & 3)) {
  998. best_idx = i;
  999. best_ioapic = ioapic_idx;
  1000. goto out;
  1001. }
  1002. /*
  1003. * Use the first all-but-pin matching entry as a
  1004. * best-guess fuzzy result for broken mptables.
  1005. */
  1006. if (best_idx < 0) {
  1007. best_idx = i;
  1008. best_ioapic = ioapic_idx;
  1009. }
  1010. }
  1011. if (best_idx < 0)
  1012. return -1;
  1013. out:
  1014. return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
  1015. IOAPIC_MAP_ALLOC);
  1016. }
  1017. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  1018. static struct irq_chip ioapic_chip, ioapic_ir_chip;
  1019. #ifdef CONFIG_X86_32
  1020. static inline int IO_APIC_irq_trigger(int irq)
  1021. {
  1022. int apic, idx, pin;
  1023. for_each_ioapic_pin(apic, pin) {
  1024. idx = find_irq_entry(apic, pin, mp_INT);
  1025. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin, 0)))
  1026. return irq_trigger(idx);
  1027. }
  1028. /*
  1029. * nonexistent IRQs are edge default
  1030. */
  1031. return 0;
  1032. }
  1033. #else
  1034. static inline int IO_APIC_irq_trigger(int irq)
  1035. {
  1036. return 1;
  1037. }
  1038. #endif
  1039. static void __init setup_IO_APIC_irqs(void)
  1040. {
  1041. unsigned int ioapic, pin;
  1042. int idx;
  1043. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1044. for_each_ioapic_pin(ioapic, pin) {
  1045. idx = find_irq_entry(ioapic, pin, mp_INT);
  1046. if (idx < 0)
  1047. apic_printk(APIC_VERBOSE,
  1048. KERN_DEBUG " apic %d pin %d not connected\n",
  1049. mpc_ioapic_id(ioapic), pin);
  1050. else
  1051. pin_2_irq(idx, ioapic, pin,
  1052. ioapic ? 0 : IOAPIC_MAP_ALLOC);
  1053. }
  1054. }
  1055. void ioapic_zap_locks(void)
  1056. {
  1057. raw_spin_lock_init(&ioapic_lock);
  1058. }
  1059. static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
  1060. {
  1061. int i;
  1062. char buf[256];
  1063. struct IO_APIC_route_entry entry;
  1064. struct IR_IO_APIC_route_entry *ir_entry = (void *)&entry;
  1065. printk(KERN_DEBUG "IOAPIC %d:\n", apic);
  1066. for (i = 0; i <= nr_entries; i++) {
  1067. entry = ioapic_read_entry(apic, i);
  1068. snprintf(buf, sizeof(buf),
  1069. " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)",
  1070. i,
  1071. entry.mask == IOAPIC_MASKED ? "disabled" : "enabled ",
  1072. entry.trigger == IOAPIC_LEVEL ? "level" : "edge ",
  1073. entry.polarity == IOAPIC_POL_LOW ? "low " : "high",
  1074. entry.vector, entry.irr, entry.delivery_status);
  1075. if (ir_entry->format)
  1076. printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n",
  1077. buf, (ir_entry->index << 15) | ir_entry->index,
  1078. ir_entry->zero);
  1079. else
  1080. printk(KERN_DEBUG "%s, %s, D(%02X), M(%1d)\n",
  1081. buf,
  1082. entry.dest_mode == IOAPIC_DEST_MODE_LOGICAL ?
  1083. "logical " : "physical",
  1084. entry.dest, entry.delivery_mode);
  1085. }
  1086. }
  1087. static void __init print_IO_APIC(int ioapic_idx)
  1088. {
  1089. union IO_APIC_reg_00 reg_00;
  1090. union IO_APIC_reg_01 reg_01;
  1091. union IO_APIC_reg_02 reg_02;
  1092. union IO_APIC_reg_03 reg_03;
  1093. unsigned long flags;
  1094. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1095. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1096. reg_01.raw = io_apic_read(ioapic_idx, 1);
  1097. if (reg_01.bits.version >= 0x10)
  1098. reg_02.raw = io_apic_read(ioapic_idx, 2);
  1099. if (reg_01.bits.version >= 0x20)
  1100. reg_03.raw = io_apic_read(ioapic_idx, 3);
  1101. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1102. printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
  1103. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1104. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1105. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1106. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1107. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1108. printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
  1109. reg_01.bits.entries);
  1110. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1111. printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
  1112. reg_01.bits.version);
  1113. /*
  1114. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1115. * but the value of reg_02 is read as the previous read register
  1116. * value, so ignore it if reg_02 == reg_01.
  1117. */
  1118. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1119. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1120. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1121. }
  1122. /*
  1123. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1124. * or reg_03, but the value of reg_0[23] is read as the previous read
  1125. * register value, so ignore it if reg_03 == reg_0[12].
  1126. */
  1127. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1128. reg_03.raw != reg_01.raw) {
  1129. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1130. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1131. }
  1132. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1133. io_apic_print_entries(ioapic_idx, reg_01.bits.entries);
  1134. }
  1135. void __init print_IO_APICs(void)
  1136. {
  1137. int ioapic_idx;
  1138. unsigned int irq;
  1139. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1140. for_each_ioapic(ioapic_idx)
  1141. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1142. mpc_ioapic_id(ioapic_idx),
  1143. ioapics[ioapic_idx].nr_registers);
  1144. /*
  1145. * We are a bit conservative about what we expect. We have to
  1146. * know about every hardware change ASAP.
  1147. */
  1148. printk(KERN_INFO "testing the IO APIC.......................\n");
  1149. for_each_ioapic(ioapic_idx)
  1150. print_IO_APIC(ioapic_idx);
  1151. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1152. for_each_active_irq(irq) {
  1153. struct irq_pin_list *entry;
  1154. struct irq_chip *chip;
  1155. struct mp_chip_data *data;
  1156. chip = irq_get_chip(irq);
  1157. if (chip != &ioapic_chip && chip != &ioapic_ir_chip)
  1158. continue;
  1159. data = irq_get_chip_data(irq);
  1160. if (!data)
  1161. continue;
  1162. if (list_empty(&data->irq_2_pin))
  1163. continue;
  1164. printk(KERN_DEBUG "IRQ%d ", irq);
  1165. for_each_irq_pin(entry, data->irq_2_pin)
  1166. pr_cont("-> %d:%d", entry->apic, entry->pin);
  1167. pr_cont("\n");
  1168. }
  1169. printk(KERN_INFO ".................................... done.\n");
  1170. }
  1171. /* Where if anywhere is the i8259 connect in external int mode */
  1172. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1173. void __init enable_IO_APIC(void)
  1174. {
  1175. int i8259_apic, i8259_pin;
  1176. int apic, pin;
  1177. if (skip_ioapic_setup)
  1178. nr_ioapics = 0;
  1179. if (!nr_legacy_irqs() || !nr_ioapics)
  1180. return;
  1181. for_each_ioapic_pin(apic, pin) {
  1182. /* See if any of the pins is in ExtINT mode */
  1183. struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
  1184. /* If the interrupt line is enabled and in ExtInt mode
  1185. * I have found the pin where the i8259 is connected.
  1186. */
  1187. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1188. ioapic_i8259.apic = apic;
  1189. ioapic_i8259.pin = pin;
  1190. goto found_i8259;
  1191. }
  1192. }
  1193. found_i8259:
  1194. /* Look to see what if the MP table has reported the ExtINT */
  1195. /* If we could not find the appropriate pin by looking at the ioapic
  1196. * the i8259 probably is not connected the ioapic but give the
  1197. * mptable a chance anyway.
  1198. */
  1199. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1200. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1201. /* Trust the MP table if nothing is setup in the hardware */
  1202. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1203. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1204. ioapic_i8259.pin = i8259_pin;
  1205. ioapic_i8259.apic = i8259_apic;
  1206. }
  1207. /* Complain if the MP table and the hardware disagree */
  1208. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1209. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1210. {
  1211. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1212. }
  1213. /*
  1214. * Do not trust the IO-APIC being empty at bootup
  1215. */
  1216. clear_IO_APIC();
  1217. }
  1218. void native_disable_io_apic(void)
  1219. {
  1220. /*
  1221. * If the i8259 is routed through an IOAPIC
  1222. * Put that IOAPIC in virtual wire mode
  1223. * so legacy interrupts can be delivered.
  1224. */
  1225. if (ioapic_i8259.pin != -1) {
  1226. struct IO_APIC_route_entry entry;
  1227. memset(&entry, 0, sizeof(entry));
  1228. entry.mask = IOAPIC_UNMASKED;
  1229. entry.trigger = IOAPIC_EDGE;
  1230. entry.polarity = IOAPIC_POL_HIGH;
  1231. entry.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
  1232. entry.delivery_mode = dest_ExtINT;
  1233. entry.dest = read_apic_id();
  1234. /*
  1235. * Add it to the IO-APIC irq-routing table:
  1236. */
  1237. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1238. }
  1239. if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config())
  1240. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1241. }
  1242. /*
  1243. * Not an __init, needed by the reboot code
  1244. */
  1245. void disable_IO_APIC(void)
  1246. {
  1247. /*
  1248. * Clear the IO-APIC before rebooting:
  1249. */
  1250. clear_IO_APIC();
  1251. if (!nr_legacy_irqs())
  1252. return;
  1253. x86_io_apic_ops.disable();
  1254. }
  1255. #ifdef CONFIG_X86_32
  1256. /*
  1257. * function to set the IO-APIC physical IDs based on the
  1258. * values stored in the MPC table.
  1259. *
  1260. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1261. */
  1262. void __init setup_ioapic_ids_from_mpc_nocheck(void)
  1263. {
  1264. union IO_APIC_reg_00 reg_00;
  1265. physid_mask_t phys_id_present_map;
  1266. int ioapic_idx;
  1267. int i;
  1268. unsigned char old_id;
  1269. unsigned long flags;
  1270. /*
  1271. * This is broken; anything with a real cpu count has to
  1272. * circumvent this idiocy regardless.
  1273. */
  1274. apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
  1275. /*
  1276. * Set the IOAPIC ID to the value stored in the MPC table.
  1277. */
  1278. for_each_ioapic(ioapic_idx) {
  1279. /* Read the register 0 value */
  1280. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1281. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1282. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1283. old_id = mpc_ioapic_id(ioapic_idx);
  1284. if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
  1285. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1286. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1287. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1288. reg_00.bits.ID);
  1289. ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
  1290. }
  1291. /*
  1292. * Sanity check, is the ID really free? Every APIC in a
  1293. * system must have a unique ID or we get lots of nice
  1294. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1295. */
  1296. if (apic->check_apicid_used(&phys_id_present_map,
  1297. mpc_ioapic_id(ioapic_idx))) {
  1298. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1299. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1300. for (i = 0; i < get_physical_broadcast(); i++)
  1301. if (!physid_isset(i, phys_id_present_map))
  1302. break;
  1303. if (i >= get_physical_broadcast())
  1304. panic("Max APIC ID exceeded!\n");
  1305. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1306. i);
  1307. physid_set(i, phys_id_present_map);
  1308. ioapics[ioapic_idx].mp_config.apicid = i;
  1309. } else {
  1310. physid_mask_t tmp;
  1311. apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
  1312. &tmp);
  1313. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1314. "phys_id_present_map\n",
  1315. mpc_ioapic_id(ioapic_idx));
  1316. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1317. }
  1318. /*
  1319. * We need to adjust the IRQ routing table
  1320. * if the ID changed.
  1321. */
  1322. if (old_id != mpc_ioapic_id(ioapic_idx))
  1323. for (i = 0; i < mp_irq_entries; i++)
  1324. if (mp_irqs[i].dstapic == old_id)
  1325. mp_irqs[i].dstapic
  1326. = mpc_ioapic_id(ioapic_idx);
  1327. /*
  1328. * Update the ID register according to the right value
  1329. * from the MPC table if they are different.
  1330. */
  1331. if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
  1332. continue;
  1333. apic_printk(APIC_VERBOSE, KERN_INFO
  1334. "...changing IO-APIC physical APIC ID to %d ...",
  1335. mpc_ioapic_id(ioapic_idx));
  1336. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  1337. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1338. io_apic_write(ioapic_idx, 0, reg_00.raw);
  1339. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1340. /*
  1341. * Sanity check
  1342. */
  1343. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1344. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1345. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1346. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
  1347. pr_cont("could not set ID!\n");
  1348. else
  1349. apic_printk(APIC_VERBOSE, " ok.\n");
  1350. }
  1351. }
  1352. void __init setup_ioapic_ids_from_mpc(void)
  1353. {
  1354. if (acpi_ioapic)
  1355. return;
  1356. /*
  1357. * Don't check I/O APIC IDs for xAPIC systems. They have
  1358. * no meaning without the serial APIC bus.
  1359. */
  1360. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1361. || APIC_XAPIC(boot_cpu_apic_version))
  1362. return;
  1363. setup_ioapic_ids_from_mpc_nocheck();
  1364. }
  1365. #endif
  1366. int no_timer_check __initdata;
  1367. static int __init notimercheck(char *s)
  1368. {
  1369. no_timer_check = 1;
  1370. return 1;
  1371. }
  1372. __setup("no_timer_check", notimercheck);
  1373. /*
  1374. * There is a nasty bug in some older SMP boards, their mptable lies
  1375. * about the timer IRQ. We do the following to work around the situation:
  1376. *
  1377. * - timer IRQ defaults to IO-APIC IRQ
  1378. * - if this function detects that timer IRQs are defunct, then we fall
  1379. * back to ISA timer IRQs
  1380. */
  1381. static int __init timer_irq_works(void)
  1382. {
  1383. unsigned long t1 = jiffies;
  1384. unsigned long flags;
  1385. if (no_timer_check)
  1386. return 1;
  1387. local_save_flags(flags);
  1388. local_irq_enable();
  1389. /* Let ten ticks pass... */
  1390. mdelay((10 * 1000) / HZ);
  1391. local_irq_restore(flags);
  1392. /*
  1393. * Expect a few ticks at least, to be sure some possible
  1394. * glue logic does not lock up after one or two first
  1395. * ticks in a non-ExtINT mode. Also the local APIC
  1396. * might have cached one ExtINT interrupt. Finally, at
  1397. * least one tick may be lost due to delays.
  1398. */
  1399. /* jiffies wrap? */
  1400. if (time_after(jiffies, t1 + 4))
  1401. return 1;
  1402. return 0;
  1403. }
  1404. /*
  1405. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1406. * number of pending IRQ events unhandled. These cases are very rare,
  1407. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1408. * better to do it this way as thus we do not have to be aware of
  1409. * 'pending' interrupts in the IRQ path, except at this point.
  1410. */
  1411. /*
  1412. * Edge triggered needs to resend any interrupt
  1413. * that was delayed but this is now handled in the device
  1414. * independent code.
  1415. */
  1416. /*
  1417. * Starting up a edge-triggered IO-APIC interrupt is
  1418. * nasty - we need to make sure that we get the edge.
  1419. * If it is already asserted for some reason, we need
  1420. * return 1 to indicate that is was pending.
  1421. *
  1422. * This is not complete - we should be able to fake
  1423. * an edge even if it isn't on the 8259A...
  1424. */
  1425. static unsigned int startup_ioapic_irq(struct irq_data *data)
  1426. {
  1427. int was_pending = 0, irq = data->irq;
  1428. unsigned long flags;
  1429. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1430. if (irq < nr_legacy_irqs()) {
  1431. legacy_pic->mask(irq);
  1432. if (legacy_pic->irq_pending(irq))
  1433. was_pending = 1;
  1434. }
  1435. __unmask_ioapic(data->chip_data);
  1436. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1437. return was_pending;
  1438. }
  1439. atomic_t irq_mis_count;
  1440. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1441. static bool io_apic_level_ack_pending(struct mp_chip_data *data)
  1442. {
  1443. struct irq_pin_list *entry;
  1444. unsigned long flags;
  1445. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1446. for_each_irq_pin(entry, data->irq_2_pin) {
  1447. unsigned int reg;
  1448. int pin;
  1449. pin = entry->pin;
  1450. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  1451. /* Is the remote IRR bit set? */
  1452. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  1453. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1454. return true;
  1455. }
  1456. }
  1457. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1458. return false;
  1459. }
  1460. static inline bool ioapic_irqd_mask(struct irq_data *data)
  1461. {
  1462. /* If we are moving the irq we need to mask it */
  1463. if (unlikely(irqd_is_setaffinity_pending(data))) {
  1464. mask_ioapic_irq(data);
  1465. return true;
  1466. }
  1467. return false;
  1468. }
  1469. static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
  1470. {
  1471. if (unlikely(masked)) {
  1472. /* Only migrate the irq if the ack has been received.
  1473. *
  1474. * On rare occasions the broadcast level triggered ack gets
  1475. * delayed going to ioapics, and if we reprogram the
  1476. * vector while Remote IRR is still set the irq will never
  1477. * fire again.
  1478. *
  1479. * To prevent this scenario we read the Remote IRR bit
  1480. * of the ioapic. This has two effects.
  1481. * - On any sane system the read of the ioapic will
  1482. * flush writes (and acks) going to the ioapic from
  1483. * this cpu.
  1484. * - We get to see if the ACK has actually been delivered.
  1485. *
  1486. * Based on failed experiments of reprogramming the
  1487. * ioapic entry from outside of irq context starting
  1488. * with masking the ioapic entry and then polling until
  1489. * Remote IRR was clear before reprogramming the
  1490. * ioapic I don't trust the Remote IRR bit to be
  1491. * completey accurate.
  1492. *
  1493. * However there appears to be no other way to plug
  1494. * this race, so if the Remote IRR bit is not
  1495. * accurate and is causing problems then it is a hardware bug
  1496. * and you can go talk to the chipset vendor about it.
  1497. */
  1498. if (!io_apic_level_ack_pending(data->chip_data))
  1499. irq_move_masked_irq(data);
  1500. unmask_ioapic_irq(data);
  1501. }
  1502. }
  1503. #else
  1504. static inline bool ioapic_irqd_mask(struct irq_data *data)
  1505. {
  1506. return false;
  1507. }
  1508. static inline void ioapic_irqd_unmask(struct irq_data *data, bool masked)
  1509. {
  1510. }
  1511. #endif
  1512. static void ioapic_ack_level(struct irq_data *irq_data)
  1513. {
  1514. struct irq_cfg *cfg = irqd_cfg(irq_data);
  1515. unsigned long v;
  1516. bool masked;
  1517. int i;
  1518. irq_complete_move(cfg);
  1519. masked = ioapic_irqd_mask(irq_data);
  1520. /*
  1521. * It appears there is an erratum which affects at least version 0x11
  1522. * of I/O APIC (that's the 82093AA and cores integrated into various
  1523. * chipsets). Under certain conditions a level-triggered interrupt is
  1524. * erroneously delivered as edge-triggered one but the respective IRR
  1525. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1526. * message but it will never arrive and further interrupts are blocked
  1527. * from the source. The exact reason is so far unknown, but the
  1528. * phenomenon was observed when two consecutive interrupt requests
  1529. * from a given source get delivered to the same CPU and the source is
  1530. * temporarily disabled in between.
  1531. *
  1532. * A workaround is to simulate an EOI message manually. We achieve it
  1533. * by setting the trigger mode to edge and then to level when the edge
  1534. * trigger mode gets detected in the TMR of a local APIC for a
  1535. * level-triggered interrupt. We mask the source for the time of the
  1536. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1537. * The idea is from Manfred Spraul. --macro
  1538. *
  1539. * Also in the case when cpu goes offline, fixup_irqs() will forward
  1540. * any unhandled interrupt on the offlined cpu to the new cpu
  1541. * destination that is handling the corresponding interrupt. This
  1542. * interrupt forwarding is done via IPI's. Hence, in this case also
  1543. * level-triggered io-apic interrupt will be seen as an edge
  1544. * interrupt in the IRR. And we can't rely on the cpu's EOI
  1545. * to be broadcasted to the IO-APIC's which will clear the remoteIRR
  1546. * corresponding to the level-triggered interrupt. Hence on IO-APIC's
  1547. * supporting EOI register, we do an explicit EOI to clear the
  1548. * remote IRR and on IO-APIC's which don't have an EOI register,
  1549. * we use the above logic (mask+edge followed by unmask+level) from
  1550. * Manfred Spraul to clear the remote IRR.
  1551. */
  1552. i = cfg->vector;
  1553. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1554. /*
  1555. * We must acknowledge the irq before we move it or the acknowledge will
  1556. * not propagate properly.
  1557. */
  1558. ack_APIC_irq();
  1559. /*
  1560. * Tail end of clearing remote IRR bit (either by delivering the EOI
  1561. * message via io-apic EOI register write or simulating it using
  1562. * mask+edge followed by unnask+level logic) manually when the
  1563. * level triggered interrupt is seen as the edge triggered interrupt
  1564. * at the cpu.
  1565. */
  1566. if (!(v & (1 << (i & 0x1f)))) {
  1567. atomic_inc(&irq_mis_count);
  1568. eoi_ioapic_pin(cfg->vector, irq_data->chip_data);
  1569. }
  1570. ioapic_irqd_unmask(irq_data, masked);
  1571. }
  1572. static void ioapic_ir_ack_level(struct irq_data *irq_data)
  1573. {
  1574. struct mp_chip_data *data = irq_data->chip_data;
  1575. /*
  1576. * Intr-remapping uses pin number as the virtual vector
  1577. * in the RTE. Actual vector is programmed in
  1578. * intr-remapping table entry. Hence for the io-apic
  1579. * EOI we use the pin number.
  1580. */
  1581. ack_APIC_irq();
  1582. eoi_ioapic_pin(data->entry.vector, data);
  1583. }
  1584. static int ioapic_set_affinity(struct irq_data *irq_data,
  1585. const struct cpumask *mask, bool force)
  1586. {
  1587. struct irq_data *parent = irq_data->parent_data;
  1588. struct mp_chip_data *data = irq_data->chip_data;
  1589. struct irq_pin_list *entry;
  1590. struct irq_cfg *cfg;
  1591. unsigned long flags;
  1592. int ret;
  1593. ret = parent->chip->irq_set_affinity(parent, mask, force);
  1594. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1595. if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
  1596. cfg = irqd_cfg(irq_data);
  1597. data->entry.dest = cfg->dest_apicid;
  1598. data->entry.vector = cfg->vector;
  1599. for_each_irq_pin(entry, data->irq_2_pin)
  1600. __ioapic_write_entry(entry->apic, entry->pin,
  1601. data->entry);
  1602. }
  1603. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1604. return ret;
  1605. }
  1606. static struct irq_chip ioapic_chip __read_mostly = {
  1607. .name = "IO-APIC",
  1608. .irq_startup = startup_ioapic_irq,
  1609. .irq_mask = mask_ioapic_irq,
  1610. .irq_unmask = unmask_ioapic_irq,
  1611. .irq_ack = irq_chip_ack_parent,
  1612. .irq_eoi = ioapic_ack_level,
  1613. .irq_set_affinity = ioapic_set_affinity,
  1614. .irq_retrigger = irq_chip_retrigger_hierarchy,
  1615. .flags = IRQCHIP_SKIP_SET_WAKE,
  1616. };
  1617. static struct irq_chip ioapic_ir_chip __read_mostly = {
  1618. .name = "IR-IO-APIC",
  1619. .irq_startup = startup_ioapic_irq,
  1620. .irq_mask = mask_ioapic_irq,
  1621. .irq_unmask = unmask_ioapic_irq,
  1622. .irq_ack = irq_chip_ack_parent,
  1623. .irq_eoi = ioapic_ir_ack_level,
  1624. .irq_set_affinity = ioapic_set_affinity,
  1625. .irq_retrigger = irq_chip_retrigger_hierarchy,
  1626. .flags = IRQCHIP_SKIP_SET_WAKE,
  1627. };
  1628. static inline void init_IO_APIC_traps(void)
  1629. {
  1630. struct irq_cfg *cfg;
  1631. unsigned int irq;
  1632. for_each_active_irq(irq) {
  1633. cfg = irq_cfg(irq);
  1634. if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
  1635. /*
  1636. * Hmm.. We don't have an entry for this,
  1637. * so default to an old-fashioned 8259
  1638. * interrupt if we can..
  1639. */
  1640. if (irq < nr_legacy_irqs())
  1641. legacy_pic->make_irq(irq);
  1642. else
  1643. /* Strange. Oh, well.. */
  1644. irq_set_chip(irq, &no_irq_chip);
  1645. }
  1646. }
  1647. }
  1648. /*
  1649. * The local APIC irq-chip implementation:
  1650. */
  1651. static void mask_lapic_irq(struct irq_data *data)
  1652. {
  1653. unsigned long v;
  1654. v = apic_read(APIC_LVT0);
  1655. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1656. }
  1657. static void unmask_lapic_irq(struct irq_data *data)
  1658. {
  1659. unsigned long v;
  1660. v = apic_read(APIC_LVT0);
  1661. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1662. }
  1663. static void ack_lapic_irq(struct irq_data *data)
  1664. {
  1665. ack_APIC_irq();
  1666. }
  1667. static struct irq_chip lapic_chip __read_mostly = {
  1668. .name = "local-APIC",
  1669. .irq_mask = mask_lapic_irq,
  1670. .irq_unmask = unmask_lapic_irq,
  1671. .irq_ack = ack_lapic_irq,
  1672. };
  1673. static void lapic_register_intr(int irq)
  1674. {
  1675. irq_clear_status_flags(irq, IRQ_LEVEL);
  1676. irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1677. "edge");
  1678. }
  1679. /*
  1680. * This looks a bit hackish but it's about the only one way of sending
  1681. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1682. * not support the ExtINT mode, unfortunately. We need to send these
  1683. * cycles as some i82489DX-based boards have glue logic that keeps the
  1684. * 8259A interrupt line asserted until INTA. --macro
  1685. */
  1686. static inline void __init unlock_ExtINT_logic(void)
  1687. {
  1688. int apic, pin, i;
  1689. struct IO_APIC_route_entry entry0, entry1;
  1690. unsigned char save_control, save_freq_select;
  1691. pin = find_isa_irq_pin(8, mp_INT);
  1692. if (pin == -1) {
  1693. WARN_ON_ONCE(1);
  1694. return;
  1695. }
  1696. apic = find_isa_irq_apic(8, mp_INT);
  1697. if (apic == -1) {
  1698. WARN_ON_ONCE(1);
  1699. return;
  1700. }
  1701. entry0 = ioapic_read_entry(apic, pin);
  1702. clear_IO_APIC_pin(apic, pin);
  1703. memset(&entry1, 0, sizeof(entry1));
  1704. entry1.dest_mode = IOAPIC_DEST_MODE_PHYSICAL;
  1705. entry1.mask = IOAPIC_UNMASKED;
  1706. entry1.dest = hard_smp_processor_id();
  1707. entry1.delivery_mode = dest_ExtINT;
  1708. entry1.polarity = entry0.polarity;
  1709. entry1.trigger = IOAPIC_EDGE;
  1710. entry1.vector = 0;
  1711. ioapic_write_entry(apic, pin, entry1);
  1712. save_control = CMOS_READ(RTC_CONTROL);
  1713. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1714. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1715. RTC_FREQ_SELECT);
  1716. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1717. i = 100;
  1718. while (i-- > 0) {
  1719. mdelay(10);
  1720. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1721. i -= 10;
  1722. }
  1723. CMOS_WRITE(save_control, RTC_CONTROL);
  1724. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1725. clear_IO_APIC_pin(apic, pin);
  1726. ioapic_write_entry(apic, pin, entry0);
  1727. }
  1728. static int disable_timer_pin_1 __initdata;
  1729. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  1730. static int __init disable_timer_pin_setup(char *arg)
  1731. {
  1732. disable_timer_pin_1 = 1;
  1733. return 0;
  1734. }
  1735. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  1736. static int mp_alloc_timer_irq(int ioapic, int pin)
  1737. {
  1738. int irq = -1;
  1739. struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
  1740. if (domain) {
  1741. struct irq_alloc_info info;
  1742. ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0);
  1743. info.ioapic_id = mpc_ioapic_id(ioapic);
  1744. info.ioapic_pin = pin;
  1745. mutex_lock(&ioapic_mutex);
  1746. irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info);
  1747. mutex_unlock(&ioapic_mutex);
  1748. }
  1749. return irq;
  1750. }
  1751. /*
  1752. * This code may look a bit paranoid, but it's supposed to cooperate with
  1753. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1754. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1755. * fanatically on his truly buggy board.
  1756. *
  1757. * FIXME: really need to revamp this for all platforms.
  1758. */
  1759. static inline void __init check_timer(void)
  1760. {
  1761. struct irq_data *irq_data = irq_get_irq_data(0);
  1762. struct mp_chip_data *data = irq_data->chip_data;
  1763. struct irq_cfg *cfg = irqd_cfg(irq_data);
  1764. int node = cpu_to_node(0);
  1765. int apic1, pin1, apic2, pin2;
  1766. unsigned long flags;
  1767. int no_pin1 = 0;
  1768. local_irq_save(flags);
  1769. /*
  1770. * get/set the timer IRQ vector:
  1771. */
  1772. legacy_pic->mask(0);
  1773. /*
  1774. * As IRQ0 is to be enabled in the 8259A, the virtual
  1775. * wire has to be disabled in the local APIC. Also
  1776. * timer interrupts need to be acknowledged manually in
  1777. * the 8259A for the i82489DX when using the NMI
  1778. * watchdog as that APIC treats NMIs as level-triggered.
  1779. * The AEOI mode will finish them in the 8259A
  1780. * automatically.
  1781. */
  1782. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1783. legacy_pic->init(1);
  1784. pin1 = find_isa_irq_pin(0, mp_INT);
  1785. apic1 = find_isa_irq_apic(0, mp_INT);
  1786. pin2 = ioapic_i8259.pin;
  1787. apic2 = ioapic_i8259.apic;
  1788. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1789. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1790. cfg->vector, apic1, pin1, apic2, pin2);
  1791. /*
  1792. * Some BIOS writers are clueless and report the ExtINTA
  1793. * I/O APIC input from the cascaded 8259A as the timer
  1794. * interrupt input. So just in case, if only one pin
  1795. * was found above, try it both directly and through the
  1796. * 8259A.
  1797. */
  1798. if (pin1 == -1) {
  1799. panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
  1800. pin1 = pin2;
  1801. apic1 = apic2;
  1802. no_pin1 = 1;
  1803. } else if (pin2 == -1) {
  1804. pin2 = pin1;
  1805. apic2 = apic1;
  1806. }
  1807. if (pin1 != -1) {
  1808. /* Ok, does IRQ0 through the IOAPIC work? */
  1809. if (no_pin1) {
  1810. mp_alloc_timer_irq(apic1, pin1);
  1811. } else {
  1812. /*
  1813. * for edge trigger, it's already unmasked,
  1814. * so only need to unmask if it is level-trigger
  1815. * do we really have level trigger timer?
  1816. */
  1817. int idx;
  1818. idx = find_irq_entry(apic1, pin1, mp_INT);
  1819. if (idx != -1 && irq_trigger(idx))
  1820. unmask_ioapic_irq(irq_get_chip_data(0));
  1821. }
  1822. irq_domain_deactivate_irq(irq_data);
  1823. irq_domain_activate_irq(irq_data);
  1824. if (timer_irq_works()) {
  1825. if (disable_timer_pin_1 > 0)
  1826. clear_IO_APIC_pin(0, pin1);
  1827. goto out;
  1828. }
  1829. panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
  1830. local_irq_disable();
  1831. clear_IO_APIC_pin(apic1, pin1);
  1832. if (!no_pin1)
  1833. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1834. "8254 timer not connected to IO-APIC\n");
  1835. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1836. "(IRQ0) through the 8259A ...\n");
  1837. apic_printk(APIC_QUIET, KERN_INFO
  1838. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1839. /*
  1840. * legacy devices should be connected to IO APIC #0
  1841. */
  1842. replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2);
  1843. irq_domain_deactivate_irq(irq_data);
  1844. irq_domain_activate_irq(irq_data);
  1845. legacy_pic->unmask(0);
  1846. if (timer_irq_works()) {
  1847. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1848. goto out;
  1849. }
  1850. /*
  1851. * Cleanup, just in case ...
  1852. */
  1853. local_irq_disable();
  1854. legacy_pic->mask(0);
  1855. clear_IO_APIC_pin(apic2, pin2);
  1856. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1857. }
  1858. apic_printk(APIC_QUIET, KERN_INFO
  1859. "...trying to set up timer as Virtual Wire IRQ...\n");
  1860. lapic_register_intr(0);
  1861. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1862. legacy_pic->unmask(0);
  1863. if (timer_irq_works()) {
  1864. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1865. goto out;
  1866. }
  1867. local_irq_disable();
  1868. legacy_pic->mask(0);
  1869. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1870. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1871. apic_printk(APIC_QUIET, KERN_INFO
  1872. "...trying to set up timer as ExtINT IRQ...\n");
  1873. legacy_pic->init(0);
  1874. legacy_pic->make_irq(0);
  1875. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1876. unlock_ExtINT_logic();
  1877. if (timer_irq_works()) {
  1878. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1879. goto out;
  1880. }
  1881. local_irq_disable();
  1882. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1883. if (apic_is_x2apic_enabled())
  1884. apic_printk(APIC_QUIET, KERN_INFO
  1885. "Perhaps problem with the pre-enabled x2apic mode\n"
  1886. "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
  1887. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1888. "report. Then try booting with the 'noapic' option.\n");
  1889. out:
  1890. local_irq_restore(flags);
  1891. }
  1892. /*
  1893. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1894. * to devices. However there may be an I/O APIC pin available for
  1895. * this interrupt regardless. The pin may be left unconnected, but
  1896. * typically it will be reused as an ExtINT cascade interrupt for
  1897. * the master 8259A. In the MPS case such a pin will normally be
  1898. * reported as an ExtINT interrupt in the MP table. With ACPI
  1899. * there is no provision for ExtINT interrupts, and in the absence
  1900. * of an override it would be treated as an ordinary ISA I/O APIC
  1901. * interrupt, that is edge-triggered and unmasked by default. We
  1902. * used to do this, but it caused problems on some systems because
  1903. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1904. * the same ExtINT cascade interrupt to drive the local APIC of the
  1905. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1906. * the I/O APIC in all cases now. No actual device should request
  1907. * it anyway. --macro
  1908. */
  1909. #define PIC_IRQS (1UL << PIC_CASCADE_IR)
  1910. static int mp_irqdomain_create(int ioapic)
  1911. {
  1912. struct irq_alloc_info info;
  1913. struct irq_domain *parent;
  1914. int hwirqs = mp_ioapic_pin_count(ioapic);
  1915. struct ioapic *ip = &ioapics[ioapic];
  1916. struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
  1917. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  1918. if (cfg->type == IOAPIC_DOMAIN_INVALID)
  1919. return 0;
  1920. init_irq_alloc_info(&info, NULL);
  1921. info.type = X86_IRQ_ALLOC_TYPE_IOAPIC;
  1922. info.ioapic_id = mpc_ioapic_id(ioapic);
  1923. parent = irq_remapping_get_ir_irq_domain(&info);
  1924. if (!parent)
  1925. parent = x86_vector_domain;
  1926. ip->irqdomain = irq_domain_add_linear(cfg->dev, hwirqs, cfg->ops,
  1927. (void *)(long)ioapic);
  1928. if (!ip->irqdomain)
  1929. return -ENOMEM;
  1930. ip->irqdomain->parent = parent;
  1931. if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
  1932. cfg->type == IOAPIC_DOMAIN_STRICT)
  1933. ioapic_dynirq_base = max(ioapic_dynirq_base,
  1934. gsi_cfg->gsi_end + 1);
  1935. return 0;
  1936. }
  1937. static void ioapic_destroy_irqdomain(int idx)
  1938. {
  1939. if (ioapics[idx].irqdomain) {
  1940. irq_domain_remove(ioapics[idx].irqdomain);
  1941. ioapics[idx].irqdomain = NULL;
  1942. }
  1943. }
  1944. void __init setup_IO_APIC(void)
  1945. {
  1946. int ioapic;
  1947. if (skip_ioapic_setup || !nr_ioapics)
  1948. return;
  1949. io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
  1950. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1951. for_each_ioapic(ioapic)
  1952. BUG_ON(mp_irqdomain_create(ioapic));
  1953. /*
  1954. * Set up IO-APIC IRQ routing.
  1955. */
  1956. x86_init.mpparse.setup_ioapic_ids();
  1957. sync_Arb_IDs();
  1958. setup_IO_APIC_irqs();
  1959. init_IO_APIC_traps();
  1960. if (nr_legacy_irqs())
  1961. check_timer();
  1962. ioapic_initialized = 1;
  1963. }
  1964. static void resume_ioapic_id(int ioapic_idx)
  1965. {
  1966. unsigned long flags;
  1967. union IO_APIC_reg_00 reg_00;
  1968. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1969. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1970. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
  1971. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  1972. io_apic_write(ioapic_idx, 0, reg_00.raw);
  1973. }
  1974. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1975. }
  1976. static void ioapic_resume(void)
  1977. {
  1978. int ioapic_idx;
  1979. for_each_ioapic_reverse(ioapic_idx)
  1980. resume_ioapic_id(ioapic_idx);
  1981. restore_ioapic_entries();
  1982. }
  1983. static struct syscore_ops ioapic_syscore_ops = {
  1984. .suspend = save_ioapic_entries,
  1985. .resume = ioapic_resume,
  1986. };
  1987. static int __init ioapic_init_ops(void)
  1988. {
  1989. register_syscore_ops(&ioapic_syscore_ops);
  1990. return 0;
  1991. }
  1992. device_initcall(ioapic_init_ops);
  1993. static int io_apic_get_redir_entries(int ioapic)
  1994. {
  1995. union IO_APIC_reg_01 reg_01;
  1996. unsigned long flags;
  1997. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1998. reg_01.raw = io_apic_read(ioapic, 1);
  1999. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2000. /* The register returns the maximum index redir index
  2001. * supported, which is one less than the total number of redir
  2002. * entries.
  2003. */
  2004. return reg_01.bits.entries + 1;
  2005. }
  2006. unsigned int arch_dynirq_lower_bound(unsigned int from)
  2007. {
  2008. /*
  2009. * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
  2010. * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
  2011. */
  2012. return ioapic_initialized ? ioapic_dynirq_base : gsi_top;
  2013. }
  2014. #ifdef CONFIG_X86_32
  2015. static int io_apic_get_unique_id(int ioapic, int apic_id)
  2016. {
  2017. union IO_APIC_reg_00 reg_00;
  2018. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2019. physid_mask_t tmp;
  2020. unsigned long flags;
  2021. int i = 0;
  2022. /*
  2023. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2024. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2025. * supports up to 16 on one shared APIC bus.
  2026. *
  2027. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2028. * advantage of new APIC bus architecture.
  2029. */
  2030. if (physids_empty(apic_id_map))
  2031. apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
  2032. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2033. reg_00.raw = io_apic_read(ioapic, 0);
  2034. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2035. if (apic_id >= get_physical_broadcast()) {
  2036. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2037. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2038. apic_id = reg_00.bits.ID;
  2039. }
  2040. /*
  2041. * Every APIC in a system must have a unique ID or we get lots of nice
  2042. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2043. */
  2044. if (apic->check_apicid_used(&apic_id_map, apic_id)) {
  2045. for (i = 0; i < get_physical_broadcast(); i++) {
  2046. if (!apic->check_apicid_used(&apic_id_map, i))
  2047. break;
  2048. }
  2049. if (i == get_physical_broadcast())
  2050. panic("Max apic_id exceeded!\n");
  2051. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2052. "trying %d\n", ioapic, apic_id, i);
  2053. apic_id = i;
  2054. }
  2055. apic->apicid_to_cpu_present(apic_id, &tmp);
  2056. physids_or(apic_id_map, apic_id_map, tmp);
  2057. if (reg_00.bits.ID != apic_id) {
  2058. reg_00.bits.ID = apic_id;
  2059. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2060. io_apic_write(ioapic, 0, reg_00.raw);
  2061. reg_00.raw = io_apic_read(ioapic, 0);
  2062. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2063. /* Sanity check */
  2064. if (reg_00.bits.ID != apic_id) {
  2065. pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
  2066. ioapic);
  2067. return -1;
  2068. }
  2069. }
  2070. apic_printk(APIC_VERBOSE, KERN_INFO
  2071. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2072. return apic_id;
  2073. }
  2074. static u8 io_apic_unique_id(int idx, u8 id)
  2075. {
  2076. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  2077. !APIC_XAPIC(boot_cpu_apic_version))
  2078. return io_apic_get_unique_id(idx, id);
  2079. else
  2080. return id;
  2081. }
  2082. #else
  2083. static u8 io_apic_unique_id(int idx, u8 id)
  2084. {
  2085. union IO_APIC_reg_00 reg_00;
  2086. DECLARE_BITMAP(used, 256);
  2087. unsigned long flags;
  2088. u8 new_id;
  2089. int i;
  2090. bitmap_zero(used, 256);
  2091. for_each_ioapic(i)
  2092. __set_bit(mpc_ioapic_id(i), used);
  2093. /* Hand out the requested id if available */
  2094. if (!test_bit(id, used))
  2095. return id;
  2096. /*
  2097. * Read the current id from the ioapic and keep it if
  2098. * available.
  2099. */
  2100. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2101. reg_00.raw = io_apic_read(idx, 0);
  2102. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2103. new_id = reg_00.bits.ID;
  2104. if (!test_bit(new_id, used)) {
  2105. apic_printk(APIC_VERBOSE, KERN_INFO
  2106. "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
  2107. idx, new_id, id);
  2108. return new_id;
  2109. }
  2110. /*
  2111. * Get the next free id and write it to the ioapic.
  2112. */
  2113. new_id = find_first_zero_bit(used, 256);
  2114. reg_00.bits.ID = new_id;
  2115. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2116. io_apic_write(idx, 0, reg_00.raw);
  2117. reg_00.raw = io_apic_read(idx, 0);
  2118. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2119. /* Sanity check */
  2120. BUG_ON(reg_00.bits.ID != new_id);
  2121. return new_id;
  2122. }
  2123. #endif
  2124. static int io_apic_get_version(int ioapic)
  2125. {
  2126. union IO_APIC_reg_01 reg_01;
  2127. unsigned long flags;
  2128. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2129. reg_01.raw = io_apic_read(ioapic, 1);
  2130. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2131. return reg_01.bits.version;
  2132. }
  2133. int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
  2134. {
  2135. int ioapic, pin, idx;
  2136. if (skip_ioapic_setup)
  2137. return -1;
  2138. ioapic = mp_find_ioapic(gsi);
  2139. if (ioapic < 0)
  2140. return -1;
  2141. pin = mp_find_ioapic_pin(ioapic, gsi);
  2142. if (pin < 0)
  2143. return -1;
  2144. idx = find_irq_entry(ioapic, pin, mp_INT);
  2145. if (idx < 0)
  2146. return -1;
  2147. *trigger = irq_trigger(idx);
  2148. *polarity = irq_polarity(idx);
  2149. return 0;
  2150. }
  2151. /*
  2152. * This function currently is only a helper for the i386 smp boot process where
  2153. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  2154. * so mask in all cases should simply be apic->target_cpus()
  2155. */
  2156. #ifdef CONFIG_SMP
  2157. void __init setup_ioapic_dest(void)
  2158. {
  2159. int pin, ioapic, irq, irq_entry;
  2160. const struct cpumask *mask;
  2161. struct irq_desc *desc;
  2162. struct irq_data *idata;
  2163. struct irq_chip *chip;
  2164. if (skip_ioapic_setup == 1)
  2165. return;
  2166. for_each_ioapic_pin(ioapic, pin) {
  2167. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  2168. if (irq_entry == -1)
  2169. continue;
  2170. irq = pin_2_irq(irq_entry, ioapic, pin, 0);
  2171. if (irq < 0 || !mp_init_irq_at_boot(ioapic, irq))
  2172. continue;
  2173. desc = irq_to_desc(irq);
  2174. raw_spin_lock_irq(&desc->lock);
  2175. idata = irq_desc_get_irq_data(desc);
  2176. /*
  2177. * Honour affinities which have been set in early boot
  2178. */
  2179. if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
  2180. mask = irq_data_get_affinity_mask(idata);
  2181. else
  2182. mask = apic->target_cpus();
  2183. chip = irq_data_get_irq_chip(idata);
  2184. /* Might be lapic_chip for irq 0 */
  2185. if (chip->irq_set_affinity)
  2186. chip->irq_set_affinity(idata, mask, false);
  2187. raw_spin_unlock_irq(&desc->lock);
  2188. }
  2189. }
  2190. #endif
  2191. #define IOAPIC_RESOURCE_NAME_SIZE 11
  2192. static struct resource *ioapic_resources;
  2193. static struct resource * __init ioapic_setup_resources(void)
  2194. {
  2195. unsigned long n;
  2196. struct resource *res;
  2197. char *mem;
  2198. int i;
  2199. if (nr_ioapics == 0)
  2200. return NULL;
  2201. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  2202. n *= nr_ioapics;
  2203. mem = alloc_bootmem(n);
  2204. res = (void *)mem;
  2205. mem += sizeof(struct resource) * nr_ioapics;
  2206. for_each_ioapic(i) {
  2207. res[i].name = mem;
  2208. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  2209. snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
  2210. mem += IOAPIC_RESOURCE_NAME_SIZE;
  2211. ioapics[i].iomem_res = &res[i];
  2212. }
  2213. ioapic_resources = res;
  2214. return res;
  2215. }
  2216. void __init io_apic_init_mappings(void)
  2217. {
  2218. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2219. struct resource *ioapic_res;
  2220. int i;
  2221. ioapic_res = ioapic_setup_resources();
  2222. for_each_ioapic(i) {
  2223. if (smp_found_config) {
  2224. ioapic_phys = mpc_ioapic_addr(i);
  2225. #ifdef CONFIG_X86_32
  2226. if (!ioapic_phys) {
  2227. printk(KERN_ERR
  2228. "WARNING: bogus zero IO-APIC "
  2229. "address found in MPTABLE, "
  2230. "disabling IO/APIC support!\n");
  2231. smp_found_config = 0;
  2232. skip_ioapic_setup = 1;
  2233. goto fake_ioapic_page;
  2234. }
  2235. #endif
  2236. } else {
  2237. #ifdef CONFIG_X86_32
  2238. fake_ioapic_page:
  2239. #endif
  2240. ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
  2241. ioapic_phys = __pa(ioapic_phys);
  2242. }
  2243. set_fixmap_nocache(idx, ioapic_phys);
  2244. apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
  2245. __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
  2246. ioapic_phys);
  2247. idx++;
  2248. ioapic_res->start = ioapic_phys;
  2249. ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
  2250. ioapic_res++;
  2251. }
  2252. }
  2253. void __init ioapic_insert_resources(void)
  2254. {
  2255. int i;
  2256. struct resource *r = ioapic_resources;
  2257. if (!r) {
  2258. if (nr_ioapics > 0)
  2259. printk(KERN_ERR
  2260. "IO APIC resources couldn't be allocated.\n");
  2261. return;
  2262. }
  2263. for_each_ioapic(i) {
  2264. insert_resource(&iomem_resource, r);
  2265. r++;
  2266. }
  2267. }
  2268. int mp_find_ioapic(u32 gsi)
  2269. {
  2270. int i;
  2271. if (nr_ioapics == 0)
  2272. return -1;
  2273. /* Find the IOAPIC that manages this GSI. */
  2274. for_each_ioapic(i) {
  2275. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
  2276. if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
  2277. return i;
  2278. }
  2279. printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
  2280. return -1;
  2281. }
  2282. int mp_find_ioapic_pin(int ioapic, u32 gsi)
  2283. {
  2284. struct mp_ioapic_gsi *gsi_cfg;
  2285. if (WARN_ON(ioapic < 0))
  2286. return -1;
  2287. gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  2288. if (WARN_ON(gsi > gsi_cfg->gsi_end))
  2289. return -1;
  2290. return gsi - gsi_cfg->gsi_base;
  2291. }
  2292. static int bad_ioapic_register(int idx)
  2293. {
  2294. union IO_APIC_reg_00 reg_00;
  2295. union IO_APIC_reg_01 reg_01;
  2296. union IO_APIC_reg_02 reg_02;
  2297. reg_00.raw = io_apic_read(idx, 0);
  2298. reg_01.raw = io_apic_read(idx, 1);
  2299. reg_02.raw = io_apic_read(idx, 2);
  2300. if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
  2301. pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
  2302. mpc_ioapic_addr(idx));
  2303. return 1;
  2304. }
  2305. return 0;
  2306. }
  2307. static int find_free_ioapic_entry(void)
  2308. {
  2309. int idx;
  2310. for (idx = 0; idx < MAX_IO_APICS; idx++)
  2311. if (ioapics[idx].nr_registers == 0)
  2312. return idx;
  2313. return MAX_IO_APICS;
  2314. }
  2315. /**
  2316. * mp_register_ioapic - Register an IOAPIC device
  2317. * @id: hardware IOAPIC ID
  2318. * @address: physical address of IOAPIC register area
  2319. * @gsi_base: base of GSI associated with the IOAPIC
  2320. * @cfg: configuration information for the IOAPIC
  2321. */
  2322. int mp_register_ioapic(int id, u32 address, u32 gsi_base,
  2323. struct ioapic_domain_cfg *cfg)
  2324. {
  2325. bool hotplug = !!ioapic_initialized;
  2326. struct mp_ioapic_gsi *gsi_cfg;
  2327. int idx, ioapic, entries;
  2328. u32 gsi_end;
  2329. if (!address) {
  2330. pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
  2331. return -EINVAL;
  2332. }
  2333. for_each_ioapic(ioapic)
  2334. if (ioapics[ioapic].mp_config.apicaddr == address) {
  2335. pr_warn("address 0x%x conflicts with IOAPIC%d\n",
  2336. address, ioapic);
  2337. return -EEXIST;
  2338. }
  2339. idx = find_free_ioapic_entry();
  2340. if (idx >= MAX_IO_APICS) {
  2341. pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
  2342. MAX_IO_APICS, idx);
  2343. return -ENOSPC;
  2344. }
  2345. ioapics[idx].mp_config.type = MP_IOAPIC;
  2346. ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
  2347. ioapics[idx].mp_config.apicaddr = address;
  2348. set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
  2349. if (bad_ioapic_register(idx)) {
  2350. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2351. return -ENODEV;
  2352. }
  2353. ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
  2354. ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
  2355. /*
  2356. * Build basic GSI lookup table to facilitate gsi->io_apic lookups
  2357. * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
  2358. */
  2359. entries = io_apic_get_redir_entries(idx);
  2360. gsi_end = gsi_base + entries - 1;
  2361. for_each_ioapic(ioapic) {
  2362. gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  2363. if ((gsi_base >= gsi_cfg->gsi_base &&
  2364. gsi_base <= gsi_cfg->gsi_end) ||
  2365. (gsi_end >= gsi_cfg->gsi_base &&
  2366. gsi_end <= gsi_cfg->gsi_end)) {
  2367. pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
  2368. gsi_base, gsi_end,
  2369. gsi_cfg->gsi_base, gsi_cfg->gsi_end);
  2370. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2371. return -ENOSPC;
  2372. }
  2373. }
  2374. gsi_cfg = mp_ioapic_gsi_routing(idx);
  2375. gsi_cfg->gsi_base = gsi_base;
  2376. gsi_cfg->gsi_end = gsi_end;
  2377. ioapics[idx].irqdomain = NULL;
  2378. ioapics[idx].irqdomain_cfg = *cfg;
  2379. /*
  2380. * If mp_register_ioapic() is called during early boot stage when
  2381. * walking ACPI/SFI/DT tables, it's too early to create irqdomain,
  2382. * we are still using bootmem allocator. So delay it to setup_IO_APIC().
  2383. */
  2384. if (hotplug) {
  2385. if (mp_irqdomain_create(idx)) {
  2386. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2387. return -ENOMEM;
  2388. }
  2389. alloc_ioapic_saved_registers(idx);
  2390. }
  2391. if (gsi_cfg->gsi_end >= gsi_top)
  2392. gsi_top = gsi_cfg->gsi_end + 1;
  2393. if (nr_ioapics <= idx)
  2394. nr_ioapics = idx + 1;
  2395. /* Set nr_registers to mark entry present */
  2396. ioapics[idx].nr_registers = entries;
  2397. pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
  2398. idx, mpc_ioapic_id(idx),
  2399. mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
  2400. gsi_cfg->gsi_base, gsi_cfg->gsi_end);
  2401. return 0;
  2402. }
  2403. int mp_unregister_ioapic(u32 gsi_base)
  2404. {
  2405. int ioapic, pin;
  2406. int found = 0;
  2407. for_each_ioapic(ioapic)
  2408. if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
  2409. found = 1;
  2410. break;
  2411. }
  2412. if (!found) {
  2413. pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
  2414. return -ENODEV;
  2415. }
  2416. for_each_pin(ioapic, pin) {
  2417. u32 gsi = mp_pin_to_gsi(ioapic, pin);
  2418. int irq = mp_map_gsi_to_irq(gsi, 0, NULL);
  2419. struct mp_chip_data *data;
  2420. if (irq >= 0) {
  2421. data = irq_get_chip_data(irq);
  2422. if (data && data->count) {
  2423. pr_warn("pin%d on IOAPIC%d is still in use.\n",
  2424. pin, ioapic);
  2425. return -EBUSY;
  2426. }
  2427. }
  2428. }
  2429. /* Mark entry not present */
  2430. ioapics[ioapic].nr_registers = 0;
  2431. ioapic_destroy_irqdomain(ioapic);
  2432. free_ioapic_saved_registers(ioapic);
  2433. if (ioapics[ioapic].iomem_res)
  2434. release_resource(ioapics[ioapic].iomem_res);
  2435. clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
  2436. memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
  2437. return 0;
  2438. }
  2439. int mp_ioapic_registered(u32 gsi_base)
  2440. {
  2441. int ioapic;
  2442. for_each_ioapic(ioapic)
  2443. if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
  2444. return 1;
  2445. return 0;
  2446. }
  2447. static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data,
  2448. struct irq_alloc_info *info)
  2449. {
  2450. if (info && info->ioapic_valid) {
  2451. data->trigger = info->ioapic_trigger;
  2452. data->polarity = info->ioapic_polarity;
  2453. } else if (acpi_get_override_irq(gsi, &data->trigger,
  2454. &data->polarity) < 0) {
  2455. /* PCI interrupts are always active low level triggered. */
  2456. data->trigger = IOAPIC_LEVEL;
  2457. data->polarity = IOAPIC_POL_LOW;
  2458. }
  2459. }
  2460. static void mp_setup_entry(struct irq_cfg *cfg, struct mp_chip_data *data,
  2461. struct IO_APIC_route_entry *entry)
  2462. {
  2463. memset(entry, 0, sizeof(*entry));
  2464. entry->delivery_mode = apic->irq_delivery_mode;
  2465. entry->dest_mode = apic->irq_dest_mode;
  2466. entry->dest = cfg->dest_apicid;
  2467. entry->vector = cfg->vector;
  2468. entry->trigger = data->trigger;
  2469. entry->polarity = data->polarity;
  2470. /*
  2471. * Mask level triggered irqs. Edge triggered irqs are masked
  2472. * by the irq core code in case they fire.
  2473. */
  2474. if (data->trigger == IOAPIC_LEVEL)
  2475. entry->mask = IOAPIC_MASKED;
  2476. else
  2477. entry->mask = IOAPIC_UNMASKED;
  2478. }
  2479. int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
  2480. unsigned int nr_irqs, void *arg)
  2481. {
  2482. int ret, ioapic, pin;
  2483. struct irq_cfg *cfg;
  2484. struct irq_data *irq_data;
  2485. struct mp_chip_data *data;
  2486. struct irq_alloc_info *info = arg;
  2487. unsigned long flags;
  2488. if (!info || nr_irqs > 1)
  2489. return -EINVAL;
  2490. irq_data = irq_domain_get_irq_data(domain, virq);
  2491. if (!irq_data)
  2492. return -EINVAL;
  2493. ioapic = mp_irqdomain_ioapic_idx(domain);
  2494. pin = info->ioapic_pin;
  2495. if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0)
  2496. return -EEXIST;
  2497. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2498. if (!data)
  2499. return -ENOMEM;
  2500. info->ioapic_entry = &data->entry;
  2501. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
  2502. if (ret < 0) {
  2503. kfree(data);
  2504. return ret;
  2505. }
  2506. INIT_LIST_HEAD(&data->irq_2_pin);
  2507. irq_data->hwirq = info->ioapic_pin;
  2508. irq_data->chip = (domain->parent == x86_vector_domain) ?
  2509. &ioapic_chip : &ioapic_ir_chip;
  2510. irq_data->chip_data = data;
  2511. mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info);
  2512. cfg = irqd_cfg(irq_data);
  2513. add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin);
  2514. local_irq_save(flags);
  2515. if (info->ioapic_entry)
  2516. mp_setup_entry(cfg, data, info->ioapic_entry);
  2517. mp_register_handler(virq, data->trigger);
  2518. if (virq < nr_legacy_irqs())
  2519. legacy_pic->mask(virq);
  2520. local_irq_restore(flags);
  2521. apic_printk(APIC_VERBOSE, KERN_DEBUG
  2522. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i Dest:%d)\n",
  2523. ioapic, mpc_ioapic_id(ioapic), pin, cfg->vector,
  2524. virq, data->trigger, data->polarity, cfg->dest_apicid);
  2525. return 0;
  2526. }
  2527. void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq,
  2528. unsigned int nr_irqs)
  2529. {
  2530. struct irq_data *irq_data;
  2531. struct mp_chip_data *data;
  2532. BUG_ON(nr_irqs != 1);
  2533. irq_data = irq_domain_get_irq_data(domain, virq);
  2534. if (irq_data && irq_data->chip_data) {
  2535. data = irq_data->chip_data;
  2536. __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain),
  2537. (int)irq_data->hwirq);
  2538. WARN_ON(!list_empty(&data->irq_2_pin));
  2539. kfree(irq_data->chip_data);
  2540. }
  2541. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  2542. }
  2543. void mp_irqdomain_activate(struct irq_domain *domain,
  2544. struct irq_data *irq_data)
  2545. {
  2546. unsigned long flags;
  2547. struct irq_pin_list *entry;
  2548. struct mp_chip_data *data = irq_data->chip_data;
  2549. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2550. for_each_irq_pin(entry, data->irq_2_pin)
  2551. __ioapic_write_entry(entry->apic, entry->pin, data->entry);
  2552. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2553. }
  2554. void mp_irqdomain_deactivate(struct irq_domain *domain,
  2555. struct irq_data *irq_data)
  2556. {
  2557. /* It won't be called for IRQ with multiple IOAPIC pins associated */
  2558. ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain),
  2559. (int)irq_data->hwirq);
  2560. }
  2561. int mp_irqdomain_ioapic_idx(struct irq_domain *domain)
  2562. {
  2563. return (int)(long)domain->host_data;
  2564. }
  2565. const struct irq_domain_ops mp_ioapic_irqdomain_ops = {
  2566. .alloc = mp_irqdomain_alloc,
  2567. .free = mp_irqdomain_free,
  2568. .activate = mp_irqdomain_activate,
  2569. .deactivate = mp_irqdomain_deactivate,
  2570. };