io_apic.c 77 KB

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