io_apic.c 78 KB

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