io_apic.c 77 KB

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