io_apic.c 77 KB

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