io_apic.c 77 KB

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