io_apic.c 81 KB

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