io_apic.c 80 KB

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