io_apic.c 77 KB

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