io_apic.c 77 KB

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