io_apic.c 77 KB

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