io_apic.c 77 KB

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