io_apic.c 77 KB

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