io_apic.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866
  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/msi.h>
  34. #include <linux/htirq.h>
  35. #include <linux/freezer.h>
  36. #include <linux/kthread.h>
  37. #include <linux/jiffies.h> /* time_after() */
  38. #include <linux/slab.h>
  39. #include <linux/bootmem.h>
  40. #include <linux/dmar.h>
  41. #include <linux/hpet.h>
  42. #include <asm/idle.h>
  43. #include <asm/io.h>
  44. #include <asm/smp.h>
  45. #include <asm/cpu.h>
  46. #include <asm/desc.h>
  47. #include <asm/proto.h>
  48. #include <asm/acpi.h>
  49. #include <asm/dma.h>
  50. #include <asm/timer.h>
  51. #include <asm/i8259.h>
  52. #include <asm/msidef.h>
  53. #include <asm/hypertransport.h>
  54. #include <asm/setup.h>
  55. #include <asm/irq_remapping.h>
  56. #include <asm/hpet.h>
  57. #include <asm/hw_irq.h>
  58. #include <asm/apic.h>
  59. #define __apicdebuginit(type) static type __init
  60. #define for_each_irq_pin(entry, head) \
  61. for (entry = head; entry; entry = entry->next)
  62. /*
  63. * Is the SiS APIC rmw bug present ?
  64. * -1 = don't know, 0 = no, 1 = yes
  65. */
  66. int sis_apic_bug = -1;
  67. static DEFINE_RAW_SPINLOCK(ioapic_lock);
  68. static DEFINE_RAW_SPINLOCK(vector_lock);
  69. static struct ioapic {
  70. /*
  71. * # of IRQ routing registers
  72. */
  73. int nr_registers;
  74. /*
  75. * Saved state during suspend/resume, or while enabling intr-remap.
  76. */
  77. struct IO_APIC_route_entry *saved_registers;
  78. /* I/O APIC config */
  79. struct mpc_ioapic mp_config;
  80. /* IO APIC gsi routing info */
  81. struct mp_ioapic_gsi gsi_config;
  82. DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
  83. } ioapics[MAX_IO_APICS];
  84. #define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
  85. int mpc_ioapic_id(int ioapic_idx)
  86. {
  87. return ioapics[ioapic_idx].mp_config.apicid;
  88. }
  89. unsigned int mpc_ioapic_addr(int ioapic_idx)
  90. {
  91. return ioapics[ioapic_idx].mp_config.apicaddr;
  92. }
  93. struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
  94. {
  95. return &ioapics[ioapic_idx].gsi_config;
  96. }
  97. int nr_ioapics;
  98. /* The one past the highest gsi number used */
  99. u32 gsi_top;
  100. /* MP IRQ source entries */
  101. struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
  102. /* # of MP IRQ source entries */
  103. int mp_irq_entries;
  104. /* GSI interrupts */
  105. static int nr_irqs_gsi = NR_IRQS_LEGACY;
  106. #ifdef CONFIG_EISA
  107. int mp_bus_id_to_type[MAX_MP_BUSSES];
  108. #endif
  109. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  110. int skip_ioapic_setup;
  111. /**
  112. * disable_ioapic_support() - disables ioapic support at runtime
  113. */
  114. void disable_ioapic_support(void)
  115. {
  116. #ifdef CONFIG_PCI
  117. noioapicquirk = 1;
  118. noioapicreroute = -1;
  119. #endif
  120. skip_ioapic_setup = 1;
  121. }
  122. static int __init parse_noapic(char *str)
  123. {
  124. /* disable IO-APIC */
  125. disable_ioapic_support();
  126. return 0;
  127. }
  128. early_param("noapic", parse_noapic);
  129. static int io_apic_setup_irq_pin(unsigned int irq, int node,
  130. struct io_apic_irq_attr *attr);
  131. /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
  132. void mp_save_irq(struct mpc_intsrc *m)
  133. {
  134. int i;
  135. apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
  136. " IRQ %02x, APIC ID %x, APIC INT %02x\n",
  137. m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
  138. m->srcbusirq, m->dstapic, m->dstirq);
  139. for (i = 0; i < mp_irq_entries; i++) {
  140. if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
  141. return;
  142. }
  143. memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
  144. if (++mp_irq_entries == MAX_IRQ_SOURCES)
  145. panic("Max # of irq sources exceeded!!\n");
  146. }
  147. struct irq_pin_list {
  148. int apic, pin;
  149. struct irq_pin_list *next;
  150. };
  151. static struct irq_pin_list *alloc_irq_pin_list(int node)
  152. {
  153. return kzalloc_node(sizeof(struct irq_pin_list), GFP_KERNEL, node);
  154. }
  155. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  156. static struct irq_cfg irq_cfgx[NR_IRQS_LEGACY];
  157. int __init arch_early_irq_init(void)
  158. {
  159. struct irq_cfg *cfg;
  160. int count, node, i;
  161. if (!legacy_pic->nr_legacy_irqs)
  162. io_apic_irqs = ~0UL;
  163. for (i = 0; i < nr_ioapics; i++) {
  164. ioapics[i].saved_registers =
  165. kzalloc(sizeof(struct IO_APIC_route_entry) *
  166. ioapics[i].nr_registers, GFP_KERNEL);
  167. if (!ioapics[i].saved_registers)
  168. pr_err("IOAPIC %d: suspend/resume impossible!\n", i);
  169. }
  170. cfg = irq_cfgx;
  171. count = ARRAY_SIZE(irq_cfgx);
  172. node = cpu_to_node(0);
  173. /* Make sure the legacy interrupts are marked in the bitmap */
  174. irq_reserve_irqs(0, legacy_pic->nr_legacy_irqs);
  175. for (i = 0; i < count; i++) {
  176. irq_set_chip_data(i, &cfg[i]);
  177. zalloc_cpumask_var_node(&cfg[i].domain, GFP_KERNEL, node);
  178. zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_KERNEL, node);
  179. /*
  180. * For legacy IRQ's, start with assigning irq0 to irq15 to
  181. * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
  182. */
  183. if (i < legacy_pic->nr_legacy_irqs) {
  184. cfg[i].vector = IRQ0_VECTOR + i;
  185. cpumask_setall(cfg[i].domain);
  186. }
  187. }
  188. return 0;
  189. }
  190. static struct irq_cfg *irq_cfg(unsigned int irq)
  191. {
  192. return irq_get_chip_data(irq);
  193. }
  194. static struct irq_cfg *alloc_irq_cfg(unsigned int irq, int node)
  195. {
  196. struct irq_cfg *cfg;
  197. cfg = kzalloc_node(sizeof(*cfg), GFP_KERNEL, node);
  198. if (!cfg)
  199. return NULL;
  200. if (!zalloc_cpumask_var_node(&cfg->domain, GFP_KERNEL, node))
  201. goto out_cfg;
  202. if (!zalloc_cpumask_var_node(&cfg->old_domain, GFP_KERNEL, node))
  203. goto out_domain;
  204. return cfg;
  205. out_domain:
  206. free_cpumask_var(cfg->domain);
  207. out_cfg:
  208. kfree(cfg);
  209. return NULL;
  210. }
  211. static void free_irq_cfg(unsigned int at, struct irq_cfg *cfg)
  212. {
  213. if (!cfg)
  214. return;
  215. irq_set_chip_data(at, NULL);
  216. free_cpumask_var(cfg->domain);
  217. free_cpumask_var(cfg->old_domain);
  218. kfree(cfg);
  219. }
  220. static struct irq_cfg *alloc_irq_and_cfg_at(unsigned int at, int node)
  221. {
  222. int res = irq_alloc_desc_at(at, node);
  223. struct irq_cfg *cfg;
  224. if (res < 0) {
  225. if (res != -EEXIST)
  226. return NULL;
  227. cfg = irq_get_chip_data(at);
  228. if (cfg)
  229. return cfg;
  230. }
  231. cfg = alloc_irq_cfg(at, node);
  232. if (cfg)
  233. irq_set_chip_data(at, cfg);
  234. else
  235. irq_free_desc(at);
  236. return cfg;
  237. }
  238. static int alloc_irqs_from(unsigned int from, unsigned int count, int node)
  239. {
  240. return irq_alloc_descs_from(from, count, node);
  241. }
  242. static void free_irq_at(unsigned int at, struct irq_cfg *cfg)
  243. {
  244. free_irq_cfg(at, cfg);
  245. irq_free_desc(at);
  246. }
  247. struct io_apic {
  248. unsigned int index;
  249. unsigned int unused[3];
  250. unsigned int data;
  251. unsigned int unused2[11];
  252. unsigned int eoi;
  253. };
  254. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  255. {
  256. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  257. + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
  258. }
  259. void io_apic_eoi(unsigned int apic, unsigned int vector)
  260. {
  261. struct io_apic __iomem *io_apic = io_apic_base(apic);
  262. writel(vector, &io_apic->eoi);
  263. }
  264. unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
  265. {
  266. struct io_apic __iomem *io_apic = io_apic_base(apic);
  267. writel(reg, &io_apic->index);
  268. return readl(&io_apic->data);
  269. }
  270. void native_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  271. {
  272. struct io_apic __iomem *io_apic = io_apic_base(apic);
  273. writel(reg, &io_apic->index);
  274. writel(value, &io_apic->data);
  275. }
  276. /*
  277. * Re-write a value: to be used for read-modify-write
  278. * cycles where the read already set up the index register.
  279. *
  280. * Older SiS APIC requires we rewrite the index register
  281. */
  282. void native_io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  283. {
  284. struct io_apic __iomem *io_apic = io_apic_base(apic);
  285. if (sis_apic_bug)
  286. writel(reg, &io_apic->index);
  287. writel(value, &io_apic->data);
  288. }
  289. union entry_union {
  290. struct { u32 w1, w2; };
  291. struct IO_APIC_route_entry entry;
  292. };
  293. static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
  294. {
  295. union entry_union eu;
  296. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  297. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  298. return eu.entry;
  299. }
  300. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  301. {
  302. union entry_union eu;
  303. unsigned long flags;
  304. raw_spin_lock_irqsave(&ioapic_lock, flags);
  305. eu.entry = __ioapic_read_entry(apic, pin);
  306. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  307. return eu.entry;
  308. }
  309. /*
  310. * When we write a new IO APIC routing entry, we need to write the high
  311. * word first! If the mask bit in the low word is clear, we will enable
  312. * the interrupt, and we need to make sure the entry is fully populated
  313. * before that happens.
  314. */
  315. static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  316. {
  317. union entry_union eu = {{0, 0}};
  318. eu.entry = e;
  319. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  320. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  321. }
  322. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  323. {
  324. unsigned long flags;
  325. raw_spin_lock_irqsave(&ioapic_lock, flags);
  326. __ioapic_write_entry(apic, pin, e);
  327. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  328. }
  329. /*
  330. * When we mask an IO APIC routing entry, we need to write the low
  331. * word first, in order to set the mask bit before we change the
  332. * high bits!
  333. */
  334. static void ioapic_mask_entry(int apic, int pin)
  335. {
  336. unsigned long flags;
  337. union entry_union eu = { .entry.mask = 1 };
  338. raw_spin_lock_irqsave(&ioapic_lock, flags);
  339. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  340. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  341. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  342. }
  343. /*
  344. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  345. * shared ISA-space IRQs, so we have to support them. We are super
  346. * fast in the common case, and fast for shared ISA-space IRQs.
  347. */
  348. static int __add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
  349. {
  350. struct irq_pin_list **last, *entry;
  351. /* don't allow duplicates */
  352. last = &cfg->irq_2_pin;
  353. for_each_irq_pin(entry, cfg->irq_2_pin) {
  354. if (entry->apic == apic && entry->pin == pin)
  355. return 0;
  356. last = &entry->next;
  357. }
  358. entry = alloc_irq_pin_list(node);
  359. if (!entry) {
  360. pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
  361. node, apic, pin);
  362. return -ENOMEM;
  363. }
  364. entry->apic = apic;
  365. entry->pin = pin;
  366. *last = entry;
  367. return 0;
  368. }
  369. static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
  370. {
  371. if (__add_pin_to_irq_node(cfg, node, apic, pin))
  372. panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
  373. }
  374. /*
  375. * Reroute an IRQ to a different pin.
  376. */
  377. static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
  378. int oldapic, int oldpin,
  379. int newapic, int newpin)
  380. {
  381. struct irq_pin_list *entry;
  382. for_each_irq_pin(entry, cfg->irq_2_pin) {
  383. if (entry->apic == oldapic && entry->pin == oldpin) {
  384. entry->apic = newapic;
  385. entry->pin = newpin;
  386. /* every one is different, right? */
  387. return;
  388. }
  389. }
  390. /* old apic/pin didn't exist, so just add new ones */
  391. add_pin_to_irq_node(cfg, node, newapic, newpin);
  392. }
  393. static void __io_apic_modify_irq(struct irq_pin_list *entry,
  394. int mask_and, int mask_or,
  395. void (*final)(struct irq_pin_list *entry))
  396. {
  397. unsigned int reg, pin;
  398. pin = entry->pin;
  399. reg = io_apic_read(entry->apic, 0x10 + pin * 2);
  400. reg &= mask_and;
  401. reg |= mask_or;
  402. io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
  403. if (final)
  404. final(entry);
  405. }
  406. static void io_apic_modify_irq(struct irq_cfg *cfg,
  407. int mask_and, int mask_or,
  408. void (*final)(struct irq_pin_list *entry))
  409. {
  410. struct irq_pin_list *entry;
  411. for_each_irq_pin(entry, cfg->irq_2_pin)
  412. __io_apic_modify_irq(entry, mask_and, mask_or, final);
  413. }
  414. static void io_apic_sync(struct irq_pin_list *entry)
  415. {
  416. /*
  417. * Synchronize the IO-APIC and the CPU by doing
  418. * a dummy read from the IO-APIC
  419. */
  420. struct io_apic __iomem *io_apic;
  421. io_apic = io_apic_base(entry->apic);
  422. readl(&io_apic->data);
  423. }
  424. static void mask_ioapic(struct irq_cfg *cfg)
  425. {
  426. unsigned long flags;
  427. raw_spin_lock_irqsave(&ioapic_lock, flags);
  428. io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
  429. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  430. }
  431. static void mask_ioapic_irq(struct irq_data *data)
  432. {
  433. mask_ioapic(data->chip_data);
  434. }
  435. static void __unmask_ioapic(struct irq_cfg *cfg)
  436. {
  437. io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
  438. }
  439. static void unmask_ioapic(struct irq_cfg *cfg)
  440. {
  441. unsigned long flags;
  442. raw_spin_lock_irqsave(&ioapic_lock, flags);
  443. __unmask_ioapic(cfg);
  444. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  445. }
  446. static void unmask_ioapic_irq(struct irq_data *data)
  447. {
  448. unmask_ioapic(data->chip_data);
  449. }
  450. /*
  451. * IO-APIC versions below 0x20 don't support EOI register.
  452. * For the record, here is the information about various versions:
  453. * 0Xh 82489DX
  454. * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
  455. * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
  456. * 30h-FFh Reserved
  457. *
  458. * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
  459. * version as 0x2. This is an error with documentation and these ICH chips
  460. * use io-apic's of version 0x20.
  461. *
  462. * For IO-APIC's with EOI register, we use that to do an explicit EOI.
  463. * Otherwise, we simulate the EOI message manually by changing the trigger
  464. * mode to edge and then back to level, with RTE being masked during this.
  465. */
  466. void native_eoi_ioapic_pin(int apic, int pin, int vector)
  467. {
  468. if (mpc_ioapic_ver(apic) >= 0x20) {
  469. io_apic_eoi(apic, vector);
  470. } else {
  471. struct IO_APIC_route_entry entry, entry1;
  472. entry = entry1 = __ioapic_read_entry(apic, pin);
  473. /*
  474. * Mask the entry and change the trigger mode to edge.
  475. */
  476. entry1.mask = 1;
  477. entry1.trigger = IOAPIC_EDGE;
  478. __ioapic_write_entry(apic, pin, entry1);
  479. /*
  480. * Restore the previous level triggered entry.
  481. */
  482. __ioapic_write_entry(apic, pin, entry);
  483. }
  484. }
  485. void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
  486. {
  487. struct irq_pin_list *entry;
  488. unsigned long flags;
  489. raw_spin_lock_irqsave(&ioapic_lock, flags);
  490. for_each_irq_pin(entry, cfg->irq_2_pin)
  491. x86_io_apic_ops.eoi_ioapic_pin(entry->apic, entry->pin,
  492. cfg->vector);
  493. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  494. }
  495. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  496. {
  497. struct IO_APIC_route_entry entry;
  498. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  499. entry = ioapic_read_entry(apic, pin);
  500. if (entry.delivery_mode == dest_SMI)
  501. return;
  502. /*
  503. * Make sure the entry is masked and re-read the contents to check
  504. * if it is a level triggered pin and if the remote-IRR is set.
  505. */
  506. if (!entry.mask) {
  507. entry.mask = 1;
  508. ioapic_write_entry(apic, pin, entry);
  509. entry = ioapic_read_entry(apic, pin);
  510. }
  511. if (entry.irr) {
  512. unsigned long flags;
  513. /*
  514. * Make sure the trigger mode is set to level. Explicit EOI
  515. * doesn't clear the remote-IRR if the trigger mode is not
  516. * set to level.
  517. */
  518. if (!entry.trigger) {
  519. entry.trigger = IOAPIC_LEVEL;
  520. ioapic_write_entry(apic, pin, entry);
  521. }
  522. raw_spin_lock_irqsave(&ioapic_lock, flags);
  523. x86_io_apic_ops.eoi_ioapic_pin(apic, pin, entry.vector);
  524. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  525. }
  526. /*
  527. * Clear the rest of the bits in the IO-APIC RTE except for the mask
  528. * bit.
  529. */
  530. ioapic_mask_entry(apic, pin);
  531. entry = ioapic_read_entry(apic, pin);
  532. if (entry.irr)
  533. pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
  534. mpc_ioapic_id(apic), pin);
  535. }
  536. static void clear_IO_APIC (void)
  537. {
  538. int apic, pin;
  539. for (apic = 0; apic < nr_ioapics; apic++)
  540. for (pin = 0; pin < ioapics[apic].nr_registers; pin++)
  541. clear_IO_APIC_pin(apic, pin);
  542. }
  543. #ifdef CONFIG_X86_32
  544. /*
  545. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  546. * specific CPU-side IRQs.
  547. */
  548. #define MAX_PIRQS 8
  549. static int pirq_entries[MAX_PIRQS] = {
  550. [0 ... MAX_PIRQS - 1] = -1
  551. };
  552. static int __init ioapic_pirq_setup(char *str)
  553. {
  554. int i, max;
  555. int ints[MAX_PIRQS+1];
  556. get_options(str, ARRAY_SIZE(ints), ints);
  557. apic_printk(APIC_VERBOSE, KERN_INFO
  558. "PIRQ redirection, working around broken MP-BIOS.\n");
  559. max = MAX_PIRQS;
  560. if (ints[0] < MAX_PIRQS)
  561. max = ints[0];
  562. for (i = 0; i < max; i++) {
  563. apic_printk(APIC_VERBOSE, KERN_DEBUG
  564. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  565. /*
  566. * PIRQs are mapped upside down, usually.
  567. */
  568. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  569. }
  570. return 1;
  571. }
  572. __setup("pirq=", ioapic_pirq_setup);
  573. #endif /* CONFIG_X86_32 */
  574. /*
  575. * Saves all the IO-APIC RTE's
  576. */
  577. int save_ioapic_entries(void)
  578. {
  579. int apic, pin;
  580. int err = 0;
  581. for (apic = 0; apic < nr_ioapics; apic++) {
  582. if (!ioapics[apic].saved_registers) {
  583. err = -ENOMEM;
  584. continue;
  585. }
  586. for (pin = 0; pin < ioapics[apic].nr_registers; pin++)
  587. ioapics[apic].saved_registers[pin] =
  588. ioapic_read_entry(apic, pin);
  589. }
  590. return err;
  591. }
  592. /*
  593. * Mask all IO APIC entries.
  594. */
  595. void mask_ioapic_entries(void)
  596. {
  597. int apic, pin;
  598. for (apic = 0; apic < nr_ioapics; apic++) {
  599. if (!ioapics[apic].saved_registers)
  600. continue;
  601. for (pin = 0; pin < ioapics[apic].nr_registers; pin++) {
  602. struct IO_APIC_route_entry entry;
  603. entry = ioapics[apic].saved_registers[pin];
  604. if (!entry.mask) {
  605. entry.mask = 1;
  606. ioapic_write_entry(apic, pin, entry);
  607. }
  608. }
  609. }
  610. }
  611. /*
  612. * Restore IO APIC entries which was saved in the ioapic structure.
  613. */
  614. int restore_ioapic_entries(void)
  615. {
  616. int apic, pin;
  617. for (apic = 0; apic < nr_ioapics; apic++) {
  618. if (!ioapics[apic].saved_registers)
  619. continue;
  620. for (pin = 0; pin < ioapics[apic].nr_registers; pin++)
  621. ioapic_write_entry(apic, pin,
  622. ioapics[apic].saved_registers[pin]);
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Find the IRQ entry number of a certain pin.
  628. */
  629. static int find_irq_entry(int ioapic_idx, int pin, int type)
  630. {
  631. int i;
  632. for (i = 0; i < mp_irq_entries; i++)
  633. if (mp_irqs[i].irqtype == type &&
  634. (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
  635. mp_irqs[i].dstapic == MP_APIC_ALL) &&
  636. mp_irqs[i].dstirq == pin)
  637. return i;
  638. return -1;
  639. }
  640. /*
  641. * Find the pin to which IRQ[irq] (ISA) is connected
  642. */
  643. static int __init find_isa_irq_pin(int irq, int type)
  644. {
  645. int i;
  646. for (i = 0; i < mp_irq_entries; i++) {
  647. int lbus = mp_irqs[i].srcbus;
  648. if (test_bit(lbus, mp_bus_not_pci) &&
  649. (mp_irqs[i].irqtype == type) &&
  650. (mp_irqs[i].srcbusirq == irq))
  651. return mp_irqs[i].dstirq;
  652. }
  653. return -1;
  654. }
  655. static int __init find_isa_irq_apic(int irq, int type)
  656. {
  657. int i;
  658. for (i = 0; i < mp_irq_entries; i++) {
  659. int lbus = mp_irqs[i].srcbus;
  660. if (test_bit(lbus, mp_bus_not_pci) &&
  661. (mp_irqs[i].irqtype == type) &&
  662. (mp_irqs[i].srcbusirq == irq))
  663. break;
  664. }
  665. if (i < mp_irq_entries) {
  666. int ioapic_idx;
  667. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++)
  668. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
  669. return ioapic_idx;
  670. }
  671. return -1;
  672. }
  673. #ifdef CONFIG_EISA
  674. /*
  675. * EISA Edge/Level control register, ELCR
  676. */
  677. static int EISA_ELCR(unsigned int irq)
  678. {
  679. if (irq < legacy_pic->nr_legacy_irqs) {
  680. unsigned int port = 0x4d0 + (irq >> 3);
  681. return (inb(port) >> (irq & 7)) & 1;
  682. }
  683. apic_printk(APIC_VERBOSE, KERN_INFO
  684. "Broken MPtable reports ISA irq %d\n", irq);
  685. return 0;
  686. }
  687. #endif
  688. /* ISA interrupts are always polarity zero edge triggered,
  689. * when listed as conforming in the MP table. */
  690. #define default_ISA_trigger(idx) (0)
  691. #define default_ISA_polarity(idx) (0)
  692. /* EISA interrupts are always polarity zero and can be edge or level
  693. * trigger depending on the ELCR value. If an interrupt is listed as
  694. * EISA conforming in the MP table, that means its trigger type must
  695. * be read in from the ELCR */
  696. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
  697. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  698. /* PCI interrupts are always polarity one level triggered,
  699. * when listed as conforming in the MP table. */
  700. #define default_PCI_trigger(idx) (1)
  701. #define default_PCI_polarity(idx) (1)
  702. static int irq_polarity(int idx)
  703. {
  704. int bus = mp_irqs[idx].srcbus;
  705. int polarity;
  706. /*
  707. * Determine IRQ line polarity (high active or low active):
  708. */
  709. switch (mp_irqs[idx].irqflag & 3)
  710. {
  711. case 0: /* conforms, ie. bus-type dependent polarity */
  712. if (test_bit(bus, mp_bus_not_pci))
  713. polarity = default_ISA_polarity(idx);
  714. else
  715. polarity = default_PCI_polarity(idx);
  716. break;
  717. case 1: /* high active */
  718. {
  719. polarity = 0;
  720. break;
  721. }
  722. case 2: /* reserved */
  723. {
  724. pr_warn("broken BIOS!!\n");
  725. polarity = 1;
  726. break;
  727. }
  728. case 3: /* low active */
  729. {
  730. polarity = 1;
  731. break;
  732. }
  733. default: /* invalid */
  734. {
  735. pr_warn("broken BIOS!!\n");
  736. polarity = 1;
  737. break;
  738. }
  739. }
  740. return polarity;
  741. }
  742. static int irq_trigger(int idx)
  743. {
  744. int bus = mp_irqs[idx].srcbus;
  745. int trigger;
  746. /*
  747. * Determine IRQ trigger mode (edge or level sensitive):
  748. */
  749. switch ((mp_irqs[idx].irqflag>>2) & 3)
  750. {
  751. case 0: /* conforms, ie. bus-type dependent */
  752. if (test_bit(bus, mp_bus_not_pci))
  753. trigger = default_ISA_trigger(idx);
  754. else
  755. trigger = default_PCI_trigger(idx);
  756. #ifdef CONFIG_EISA
  757. switch (mp_bus_id_to_type[bus]) {
  758. case MP_BUS_ISA: /* ISA pin */
  759. {
  760. /* set before the switch */
  761. break;
  762. }
  763. case MP_BUS_EISA: /* EISA pin */
  764. {
  765. trigger = default_EISA_trigger(idx);
  766. break;
  767. }
  768. case MP_BUS_PCI: /* PCI pin */
  769. {
  770. /* set before the switch */
  771. break;
  772. }
  773. default:
  774. {
  775. pr_warn("broken BIOS!!\n");
  776. trigger = 1;
  777. break;
  778. }
  779. }
  780. #endif
  781. break;
  782. case 1: /* edge */
  783. {
  784. trigger = 0;
  785. break;
  786. }
  787. case 2: /* reserved */
  788. {
  789. pr_warn("broken BIOS!!\n");
  790. trigger = 1;
  791. break;
  792. }
  793. case 3: /* level */
  794. {
  795. trigger = 1;
  796. break;
  797. }
  798. default: /* invalid */
  799. {
  800. pr_warn("broken BIOS!!\n");
  801. trigger = 0;
  802. break;
  803. }
  804. }
  805. return trigger;
  806. }
  807. static int pin_2_irq(int idx, int apic, int pin)
  808. {
  809. int irq;
  810. int bus = mp_irqs[idx].srcbus;
  811. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(apic);
  812. /*
  813. * Debugging check, we are in big trouble if this message pops up!
  814. */
  815. if (mp_irqs[idx].dstirq != pin)
  816. pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
  817. if (test_bit(bus, mp_bus_not_pci)) {
  818. irq = mp_irqs[idx].srcbusirq;
  819. } else {
  820. u32 gsi = gsi_cfg->gsi_base + pin;
  821. if (gsi >= NR_IRQS_LEGACY)
  822. irq = gsi;
  823. else
  824. irq = gsi_top + gsi;
  825. }
  826. #ifdef CONFIG_X86_32
  827. /*
  828. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  829. */
  830. if ((pin >= 16) && (pin <= 23)) {
  831. if (pirq_entries[pin-16] != -1) {
  832. if (!pirq_entries[pin-16]) {
  833. apic_printk(APIC_VERBOSE, KERN_DEBUG
  834. "disabling PIRQ%d\n", pin-16);
  835. } else {
  836. irq = pirq_entries[pin-16];
  837. apic_printk(APIC_VERBOSE, KERN_DEBUG
  838. "using PIRQ%d -> IRQ %d\n",
  839. pin-16, irq);
  840. }
  841. }
  842. }
  843. #endif
  844. return irq;
  845. }
  846. /*
  847. * Find a specific PCI IRQ entry.
  848. * Not an __init, possibly needed by modules
  849. */
  850. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
  851. struct io_apic_irq_attr *irq_attr)
  852. {
  853. int ioapic_idx, i, best_guess = -1;
  854. apic_printk(APIC_DEBUG,
  855. "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  856. bus, slot, pin);
  857. if (test_bit(bus, mp_bus_not_pci)) {
  858. apic_printk(APIC_VERBOSE,
  859. "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  860. return -1;
  861. }
  862. for (i = 0; i < mp_irq_entries; i++) {
  863. int lbus = mp_irqs[i].srcbus;
  864. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++)
  865. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
  866. mp_irqs[i].dstapic == MP_APIC_ALL)
  867. break;
  868. if (!test_bit(lbus, mp_bus_not_pci) &&
  869. !mp_irqs[i].irqtype &&
  870. (bus == lbus) &&
  871. (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
  872. int irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq);
  873. if (!(ioapic_idx || IO_APIC_IRQ(irq)))
  874. continue;
  875. if (pin == (mp_irqs[i].srcbusirq & 3)) {
  876. set_io_apic_irq_attr(irq_attr, ioapic_idx,
  877. mp_irqs[i].dstirq,
  878. irq_trigger(i),
  879. irq_polarity(i));
  880. return irq;
  881. }
  882. /*
  883. * Use the first all-but-pin matching entry as a
  884. * best-guess fuzzy result for broken mptables.
  885. */
  886. if (best_guess < 0) {
  887. set_io_apic_irq_attr(irq_attr, ioapic_idx,
  888. mp_irqs[i].dstirq,
  889. irq_trigger(i),
  890. irq_polarity(i));
  891. best_guess = irq;
  892. }
  893. }
  894. }
  895. return best_guess;
  896. }
  897. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  898. void lock_vector_lock(void)
  899. {
  900. /* Used to the online set of cpus does not change
  901. * during assign_irq_vector.
  902. */
  903. raw_spin_lock(&vector_lock);
  904. }
  905. void unlock_vector_lock(void)
  906. {
  907. raw_spin_unlock(&vector_lock);
  908. }
  909. static int
  910. __assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
  911. {
  912. /*
  913. * NOTE! The local APIC isn't very good at handling
  914. * multiple interrupts at the same interrupt level.
  915. * As the interrupt level is determined by taking the
  916. * vector number and shifting that right by 4, we
  917. * want to spread these out a bit so that they don't
  918. * all fall in the same interrupt level.
  919. *
  920. * Also, we've got to be careful not to trash gate
  921. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  922. */
  923. static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
  924. static int current_offset = VECTOR_OFFSET_START % 16;
  925. int cpu, err;
  926. cpumask_var_t tmp_mask;
  927. if (cfg->move_in_progress)
  928. return -EBUSY;
  929. if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
  930. return -ENOMEM;
  931. /* Only try and allocate irqs on cpus that are present */
  932. err = -ENOSPC;
  933. cpumask_clear(cfg->old_domain);
  934. cpu = cpumask_first_and(mask, cpu_online_mask);
  935. while (cpu < nr_cpu_ids) {
  936. int new_cpu, vector, offset;
  937. apic->vector_allocation_domain(cpu, tmp_mask, mask);
  938. if (cpumask_subset(tmp_mask, cfg->domain)) {
  939. err = 0;
  940. if (cpumask_equal(tmp_mask, cfg->domain))
  941. break;
  942. /*
  943. * New cpumask using the vector is a proper subset of
  944. * the current in use mask. So cleanup the vector
  945. * allocation for the members that are not used anymore.
  946. */
  947. cpumask_andnot(cfg->old_domain, cfg->domain, tmp_mask);
  948. cfg->move_in_progress =
  949. cpumask_intersects(cfg->old_domain, cpu_online_mask);
  950. cpumask_and(cfg->domain, cfg->domain, tmp_mask);
  951. break;
  952. }
  953. vector = current_vector;
  954. offset = current_offset;
  955. next:
  956. vector += 16;
  957. if (vector >= first_system_vector) {
  958. offset = (offset + 1) % 16;
  959. vector = FIRST_EXTERNAL_VECTOR + offset;
  960. }
  961. if (unlikely(current_vector == vector)) {
  962. cpumask_or(cfg->old_domain, cfg->old_domain, tmp_mask);
  963. cpumask_andnot(tmp_mask, mask, cfg->old_domain);
  964. cpu = cpumask_first_and(tmp_mask, cpu_online_mask);
  965. continue;
  966. }
  967. if (test_bit(vector, used_vectors))
  968. goto next;
  969. for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask) {
  970. if (per_cpu(vector_irq, new_cpu)[vector] > VECTOR_UNDEFINED)
  971. goto next;
  972. }
  973. /* Found one! */
  974. current_vector = vector;
  975. current_offset = offset;
  976. if (cfg->vector) {
  977. cpumask_copy(cfg->old_domain, cfg->domain);
  978. cfg->move_in_progress =
  979. cpumask_intersects(cfg->old_domain, cpu_online_mask);
  980. }
  981. for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
  982. per_cpu(vector_irq, new_cpu)[vector] = irq;
  983. cfg->vector = vector;
  984. cpumask_copy(cfg->domain, tmp_mask);
  985. err = 0;
  986. break;
  987. }
  988. free_cpumask_var(tmp_mask);
  989. return err;
  990. }
  991. int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
  992. {
  993. int err;
  994. unsigned long flags;
  995. raw_spin_lock_irqsave(&vector_lock, flags);
  996. err = __assign_irq_vector(irq, cfg, mask);
  997. raw_spin_unlock_irqrestore(&vector_lock, flags);
  998. return err;
  999. }
  1000. static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
  1001. {
  1002. int cpu, vector;
  1003. BUG_ON(!cfg->vector);
  1004. vector = cfg->vector;
  1005. for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
  1006. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
  1007. cfg->vector = 0;
  1008. cpumask_clear(cfg->domain);
  1009. if (likely(!cfg->move_in_progress))
  1010. return;
  1011. for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
  1012. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1013. if (per_cpu(vector_irq, cpu)[vector] != irq)
  1014. continue;
  1015. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
  1016. break;
  1017. }
  1018. }
  1019. cfg->move_in_progress = 0;
  1020. }
  1021. void __setup_vector_irq(int cpu)
  1022. {
  1023. /* Initialize vector_irq on a new cpu */
  1024. int irq, vector;
  1025. struct irq_cfg *cfg;
  1026. /*
  1027. * vector_lock will make sure that we don't run into irq vector
  1028. * assignments that might be happening on another cpu in parallel,
  1029. * while we setup our initial vector to irq mappings.
  1030. */
  1031. raw_spin_lock(&vector_lock);
  1032. /* Mark the inuse vectors */
  1033. for_each_active_irq(irq) {
  1034. cfg = irq_get_chip_data(irq);
  1035. if (!cfg)
  1036. continue;
  1037. if (!cpumask_test_cpu(cpu, cfg->domain))
  1038. continue;
  1039. vector = cfg->vector;
  1040. per_cpu(vector_irq, cpu)[vector] = irq;
  1041. }
  1042. /* Mark the free vectors */
  1043. for (vector = 0; vector < NR_VECTORS; ++vector) {
  1044. irq = per_cpu(vector_irq, cpu)[vector];
  1045. if (irq <= VECTOR_UNDEFINED)
  1046. continue;
  1047. cfg = irq_cfg(irq);
  1048. if (!cpumask_test_cpu(cpu, cfg->domain))
  1049. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
  1050. }
  1051. raw_spin_unlock(&vector_lock);
  1052. }
  1053. static struct irq_chip ioapic_chip;
  1054. #ifdef CONFIG_X86_32
  1055. static inline int IO_APIC_irq_trigger(int irq)
  1056. {
  1057. int apic, idx, pin;
  1058. for (apic = 0; apic < nr_ioapics; apic++) {
  1059. for (pin = 0; pin < ioapics[apic].nr_registers; pin++) {
  1060. idx = find_irq_entry(apic, pin, mp_INT);
  1061. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  1062. return irq_trigger(idx);
  1063. }
  1064. }
  1065. /*
  1066. * nonexistent IRQs are edge default
  1067. */
  1068. return 0;
  1069. }
  1070. #else
  1071. static inline int IO_APIC_irq_trigger(int irq)
  1072. {
  1073. return 1;
  1074. }
  1075. #endif
  1076. static void ioapic_register_intr(unsigned int irq, struct irq_cfg *cfg,
  1077. unsigned long trigger)
  1078. {
  1079. struct irq_chip *chip = &ioapic_chip;
  1080. irq_flow_handler_t hdl;
  1081. bool fasteoi;
  1082. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1083. trigger == IOAPIC_LEVEL) {
  1084. irq_set_status_flags(irq, IRQ_LEVEL);
  1085. fasteoi = true;
  1086. } else {
  1087. irq_clear_status_flags(irq, IRQ_LEVEL);
  1088. fasteoi = false;
  1089. }
  1090. if (setup_remapped_irq(irq, cfg, chip))
  1091. fasteoi = trigger != 0;
  1092. hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
  1093. irq_set_chip_and_handler_name(irq, chip, hdl,
  1094. fasteoi ? "fasteoi" : "edge");
  1095. }
  1096. int native_setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
  1097. unsigned int destination, int vector,
  1098. struct io_apic_irq_attr *attr)
  1099. {
  1100. memset(entry, 0, sizeof(*entry));
  1101. entry->delivery_mode = apic->irq_delivery_mode;
  1102. entry->dest_mode = apic->irq_dest_mode;
  1103. entry->dest = destination;
  1104. entry->vector = vector;
  1105. entry->mask = 0; /* enable IRQ */
  1106. entry->trigger = attr->trigger;
  1107. entry->polarity = attr->polarity;
  1108. /*
  1109. * Mask level triggered irqs.
  1110. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  1111. */
  1112. if (attr->trigger)
  1113. entry->mask = 1;
  1114. return 0;
  1115. }
  1116. static void setup_ioapic_irq(unsigned int irq, struct irq_cfg *cfg,
  1117. struct io_apic_irq_attr *attr)
  1118. {
  1119. struct IO_APIC_route_entry entry;
  1120. unsigned int dest;
  1121. if (!IO_APIC_IRQ(irq))
  1122. return;
  1123. if (assign_irq_vector(irq, cfg, apic->target_cpus()))
  1124. return;
  1125. if (apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus(),
  1126. &dest)) {
  1127. pr_warn("Failed to obtain apicid for ioapic %d, pin %d\n",
  1128. mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
  1129. __clear_irq_vector(irq, cfg);
  1130. return;
  1131. }
  1132. apic_printk(APIC_VERBOSE,KERN_DEBUG
  1133. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  1134. "IRQ %d Mode:%i Active:%i Dest:%d)\n",
  1135. attr->ioapic, mpc_ioapic_id(attr->ioapic), attr->ioapic_pin,
  1136. cfg->vector, irq, attr->trigger, attr->polarity, dest);
  1137. if (x86_io_apic_ops.setup_entry(irq, &entry, dest, cfg->vector, attr)) {
  1138. pr_warn("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  1139. mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
  1140. __clear_irq_vector(irq, cfg);
  1141. return;
  1142. }
  1143. ioapic_register_intr(irq, cfg, attr->trigger);
  1144. if (irq < legacy_pic->nr_legacy_irqs)
  1145. legacy_pic->mask(irq);
  1146. ioapic_write_entry(attr->ioapic, attr->ioapic_pin, entry);
  1147. }
  1148. static bool __init io_apic_pin_not_connected(int idx, int ioapic_idx, int pin)
  1149. {
  1150. if (idx != -1)
  1151. return false;
  1152. apic_printk(APIC_VERBOSE, KERN_DEBUG " apic %d pin %d not connected\n",
  1153. mpc_ioapic_id(ioapic_idx), pin);
  1154. return true;
  1155. }
  1156. static void __init __io_apic_setup_irqs(unsigned int ioapic_idx)
  1157. {
  1158. int idx, node = cpu_to_node(0);
  1159. struct io_apic_irq_attr attr;
  1160. unsigned int pin, irq;
  1161. for (pin = 0; pin < ioapics[ioapic_idx].nr_registers; pin++) {
  1162. idx = find_irq_entry(ioapic_idx, pin, mp_INT);
  1163. if (io_apic_pin_not_connected(idx, ioapic_idx, pin))
  1164. continue;
  1165. irq = pin_2_irq(idx, ioapic_idx, pin);
  1166. if ((ioapic_idx > 0) && (irq > 16))
  1167. continue;
  1168. /*
  1169. * Skip the timer IRQ if there's a quirk handler
  1170. * installed and if it returns 1:
  1171. */
  1172. if (apic->multi_timer_check &&
  1173. apic->multi_timer_check(ioapic_idx, irq))
  1174. continue;
  1175. set_io_apic_irq_attr(&attr, ioapic_idx, pin, irq_trigger(idx),
  1176. irq_polarity(idx));
  1177. io_apic_setup_irq_pin(irq, node, &attr);
  1178. }
  1179. }
  1180. static void __init setup_IO_APIC_irqs(void)
  1181. {
  1182. unsigned int ioapic_idx;
  1183. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1184. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++)
  1185. __io_apic_setup_irqs(ioapic_idx);
  1186. }
  1187. /*
  1188. * for the gsit that is not in first ioapic
  1189. * but could not use acpi_register_gsi()
  1190. * like some special sci in IBM x3330
  1191. */
  1192. void setup_IO_APIC_irq_extra(u32 gsi)
  1193. {
  1194. int ioapic_idx = 0, pin, idx, irq, node = cpu_to_node(0);
  1195. struct io_apic_irq_attr attr;
  1196. /*
  1197. * Convert 'gsi' to 'ioapic.pin'.
  1198. */
  1199. ioapic_idx = mp_find_ioapic(gsi);
  1200. if (ioapic_idx < 0)
  1201. return;
  1202. pin = mp_find_ioapic_pin(ioapic_idx, gsi);
  1203. idx = find_irq_entry(ioapic_idx, pin, mp_INT);
  1204. if (idx == -1)
  1205. return;
  1206. irq = pin_2_irq(idx, ioapic_idx, pin);
  1207. /* Only handle the non legacy irqs on secondary ioapics */
  1208. if (ioapic_idx == 0 || irq < NR_IRQS_LEGACY)
  1209. return;
  1210. set_io_apic_irq_attr(&attr, ioapic_idx, pin, irq_trigger(idx),
  1211. irq_polarity(idx));
  1212. io_apic_setup_irq_pin_once(irq, node, &attr);
  1213. }
  1214. /*
  1215. * Set up the timer pin, possibly with the 8259A-master behind.
  1216. */
  1217. static void __init setup_timer_IRQ0_pin(unsigned int ioapic_idx,
  1218. unsigned int pin, int vector)
  1219. {
  1220. struct IO_APIC_route_entry entry;
  1221. unsigned int dest;
  1222. memset(&entry, 0, sizeof(entry));
  1223. /*
  1224. * We use logical delivery to get the timer IRQ
  1225. * to the first CPU.
  1226. */
  1227. if (unlikely(apic->cpu_mask_to_apicid_and(apic->target_cpus(),
  1228. apic->target_cpus(), &dest)))
  1229. dest = BAD_APICID;
  1230. entry.dest_mode = apic->irq_dest_mode;
  1231. entry.mask = 0; /* don't mask IRQ for edge */
  1232. entry.dest = dest;
  1233. entry.delivery_mode = apic->irq_delivery_mode;
  1234. entry.polarity = 0;
  1235. entry.trigger = 0;
  1236. entry.vector = vector;
  1237. /*
  1238. * The timer IRQ doesn't have to know that behind the
  1239. * scene we may have a 8259A-master in AEOI mode ...
  1240. */
  1241. irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
  1242. "edge");
  1243. /*
  1244. * Add it to the IO-APIC irq-routing table:
  1245. */
  1246. ioapic_write_entry(ioapic_idx, pin, entry);
  1247. }
  1248. void native_io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
  1249. {
  1250. int i;
  1251. pr_debug(" NR Dst Mask Trig IRR Pol Stat Dmod Deli Vect:\n");
  1252. for (i = 0; i <= nr_entries; i++) {
  1253. struct IO_APIC_route_entry entry;
  1254. entry = ioapic_read_entry(apic, i);
  1255. pr_debug(" %02x %02X ", i, entry.dest);
  1256. pr_cont("%1d %1d %1d %1d %1d "
  1257. "%1d %1d %02X\n",
  1258. entry.mask,
  1259. entry.trigger,
  1260. entry.irr,
  1261. entry.polarity,
  1262. entry.delivery_status,
  1263. entry.dest_mode,
  1264. entry.delivery_mode,
  1265. entry.vector);
  1266. }
  1267. }
  1268. void intel_ir_io_apic_print_entries(unsigned int apic,
  1269. unsigned int nr_entries)
  1270. {
  1271. int i;
  1272. pr_debug(" NR Indx Fmt Mask Trig IRR Pol Stat Indx2 Zero Vect:\n");
  1273. for (i = 0; i <= nr_entries; i++) {
  1274. struct IR_IO_APIC_route_entry *ir_entry;
  1275. struct IO_APIC_route_entry entry;
  1276. entry = ioapic_read_entry(apic, i);
  1277. ir_entry = (struct IR_IO_APIC_route_entry *)&entry;
  1278. pr_debug(" %02x %04X ", i, ir_entry->index);
  1279. pr_cont("%1d %1d %1d %1d %1d "
  1280. "%1d %1d %X %02X\n",
  1281. ir_entry->format,
  1282. ir_entry->mask,
  1283. ir_entry->trigger,
  1284. ir_entry->irr,
  1285. ir_entry->polarity,
  1286. ir_entry->delivery_status,
  1287. ir_entry->index2,
  1288. ir_entry->zero,
  1289. ir_entry->vector);
  1290. }
  1291. }
  1292. void ioapic_zap_locks(void)
  1293. {
  1294. raw_spin_lock_init(&ioapic_lock);
  1295. }
  1296. __apicdebuginit(void) print_IO_APIC(int ioapic_idx)
  1297. {
  1298. union IO_APIC_reg_00 reg_00;
  1299. union IO_APIC_reg_01 reg_01;
  1300. union IO_APIC_reg_02 reg_02;
  1301. union IO_APIC_reg_03 reg_03;
  1302. unsigned long flags;
  1303. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1304. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1305. reg_01.raw = io_apic_read(ioapic_idx, 1);
  1306. if (reg_01.bits.version >= 0x10)
  1307. reg_02.raw = io_apic_read(ioapic_idx, 2);
  1308. if (reg_01.bits.version >= 0x20)
  1309. reg_03.raw = io_apic_read(ioapic_idx, 3);
  1310. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1311. printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
  1312. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1313. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1314. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1315. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1316. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1317. printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
  1318. reg_01.bits.entries);
  1319. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1320. printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
  1321. reg_01.bits.version);
  1322. /*
  1323. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1324. * but the value of reg_02 is read as the previous read register
  1325. * value, so ignore it if reg_02 == reg_01.
  1326. */
  1327. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1328. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1329. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1330. }
  1331. /*
  1332. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1333. * or reg_03, but the value of reg_0[23] is read as the previous read
  1334. * register value, so ignore it if reg_03 == reg_0[12].
  1335. */
  1336. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1337. reg_03.raw != reg_01.raw) {
  1338. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1339. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1340. }
  1341. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1342. x86_io_apic_ops.print_entries(ioapic_idx, reg_01.bits.entries);
  1343. }
  1344. __apicdebuginit(void) print_IO_APICs(void)
  1345. {
  1346. int ioapic_idx;
  1347. struct irq_cfg *cfg;
  1348. unsigned int irq;
  1349. struct irq_chip *chip;
  1350. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1351. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++)
  1352. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1353. mpc_ioapic_id(ioapic_idx),
  1354. ioapics[ioapic_idx].nr_registers);
  1355. /*
  1356. * We are a bit conservative about what we expect. We have to
  1357. * know about every hardware change ASAP.
  1358. */
  1359. printk(KERN_INFO "testing the IO APIC.......................\n");
  1360. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++)
  1361. print_IO_APIC(ioapic_idx);
  1362. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1363. for_each_active_irq(irq) {
  1364. struct irq_pin_list *entry;
  1365. chip = irq_get_chip(irq);
  1366. if (chip != &ioapic_chip)
  1367. continue;
  1368. cfg = irq_get_chip_data(irq);
  1369. if (!cfg)
  1370. continue;
  1371. entry = cfg->irq_2_pin;
  1372. if (!entry)
  1373. continue;
  1374. printk(KERN_DEBUG "IRQ%d ", irq);
  1375. for_each_irq_pin(entry, cfg->irq_2_pin)
  1376. pr_cont("-> %d:%d", entry->apic, entry->pin);
  1377. pr_cont("\n");
  1378. }
  1379. printk(KERN_INFO ".................................... done.\n");
  1380. }
  1381. __apicdebuginit(void) print_APIC_field(int base)
  1382. {
  1383. int i;
  1384. printk(KERN_DEBUG);
  1385. for (i = 0; i < 8; i++)
  1386. pr_cont("%08x", apic_read(base + i*0x10));
  1387. pr_cont("\n");
  1388. }
  1389. __apicdebuginit(void) print_local_APIC(void *dummy)
  1390. {
  1391. unsigned int i, v, ver, maxlvt;
  1392. u64 icr;
  1393. printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1394. smp_processor_id(), hard_smp_processor_id());
  1395. v = apic_read(APIC_ID);
  1396. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1397. v = apic_read(APIC_LVR);
  1398. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1399. ver = GET_APIC_VERSION(v);
  1400. maxlvt = lapic_get_maxlvt();
  1401. v = apic_read(APIC_TASKPRI);
  1402. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1403. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1404. if (!APIC_XAPIC(ver)) {
  1405. v = apic_read(APIC_ARBPRI);
  1406. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1407. v & APIC_ARBPRI_MASK);
  1408. }
  1409. v = apic_read(APIC_PROCPRI);
  1410. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1411. }
  1412. /*
  1413. * Remote read supported only in the 82489DX and local APIC for
  1414. * Pentium processors.
  1415. */
  1416. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1417. v = apic_read(APIC_RRR);
  1418. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1419. }
  1420. v = apic_read(APIC_LDR);
  1421. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1422. if (!x2apic_enabled()) {
  1423. v = apic_read(APIC_DFR);
  1424. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1425. }
  1426. v = apic_read(APIC_SPIV);
  1427. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1428. printk(KERN_DEBUG "... APIC ISR field:\n");
  1429. print_APIC_field(APIC_ISR);
  1430. printk(KERN_DEBUG "... APIC TMR field:\n");
  1431. print_APIC_field(APIC_TMR);
  1432. printk(KERN_DEBUG "... APIC IRR field:\n");
  1433. print_APIC_field(APIC_IRR);
  1434. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1435. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1436. apic_write(APIC_ESR, 0);
  1437. v = apic_read(APIC_ESR);
  1438. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1439. }
  1440. icr = apic_icr_read();
  1441. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1442. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1443. v = apic_read(APIC_LVTT);
  1444. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1445. if (maxlvt > 3) { /* PC is LVT#4. */
  1446. v = apic_read(APIC_LVTPC);
  1447. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1448. }
  1449. v = apic_read(APIC_LVT0);
  1450. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1451. v = apic_read(APIC_LVT1);
  1452. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1453. if (maxlvt > 2) { /* ERR is LVT#3. */
  1454. v = apic_read(APIC_LVTERR);
  1455. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1456. }
  1457. v = apic_read(APIC_TMICT);
  1458. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1459. v = apic_read(APIC_TMCCT);
  1460. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1461. v = apic_read(APIC_TDCR);
  1462. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1463. if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
  1464. v = apic_read(APIC_EFEAT);
  1465. maxlvt = (v >> 16) & 0xff;
  1466. printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
  1467. v = apic_read(APIC_ECTRL);
  1468. printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
  1469. for (i = 0; i < maxlvt; i++) {
  1470. v = apic_read(APIC_EILVTn(i));
  1471. printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
  1472. }
  1473. }
  1474. pr_cont("\n");
  1475. }
  1476. __apicdebuginit(void) print_local_APICs(int maxcpu)
  1477. {
  1478. int cpu;
  1479. if (!maxcpu)
  1480. return;
  1481. preempt_disable();
  1482. for_each_online_cpu(cpu) {
  1483. if (cpu >= maxcpu)
  1484. break;
  1485. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1486. }
  1487. preempt_enable();
  1488. }
  1489. __apicdebuginit(void) print_PIC(void)
  1490. {
  1491. unsigned int v;
  1492. unsigned long flags;
  1493. if (!legacy_pic->nr_legacy_irqs)
  1494. return;
  1495. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1496. raw_spin_lock_irqsave(&i8259A_lock, flags);
  1497. v = inb(0xa1) << 8 | inb(0x21);
  1498. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1499. v = inb(0xa0) << 8 | inb(0x20);
  1500. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1501. outb(0x0b,0xa0);
  1502. outb(0x0b,0x20);
  1503. v = inb(0xa0) << 8 | inb(0x20);
  1504. outb(0x0a,0xa0);
  1505. outb(0x0a,0x20);
  1506. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  1507. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1508. v = inb(0x4d1) << 8 | inb(0x4d0);
  1509. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1510. }
  1511. static int __initdata show_lapic = 1;
  1512. static __init int setup_show_lapic(char *arg)
  1513. {
  1514. int num = -1;
  1515. if (strcmp(arg, "all") == 0) {
  1516. show_lapic = CONFIG_NR_CPUS;
  1517. } else {
  1518. get_option(&arg, &num);
  1519. if (num >= 0)
  1520. show_lapic = num;
  1521. }
  1522. return 1;
  1523. }
  1524. __setup("show_lapic=", setup_show_lapic);
  1525. __apicdebuginit(int) print_ICs(void)
  1526. {
  1527. if (apic_verbosity == APIC_QUIET)
  1528. return 0;
  1529. print_PIC();
  1530. /* don't print out if apic is not there */
  1531. if (!cpu_has_apic && !apic_from_smp_config())
  1532. return 0;
  1533. print_local_APICs(show_lapic);
  1534. print_IO_APICs();
  1535. return 0;
  1536. }
  1537. late_initcall(print_ICs);
  1538. /* Where if anywhere is the i8259 connect in external int mode */
  1539. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1540. void __init enable_IO_APIC(void)
  1541. {
  1542. int i8259_apic, i8259_pin;
  1543. int apic;
  1544. if (!legacy_pic->nr_legacy_irqs)
  1545. return;
  1546. for(apic = 0; apic < nr_ioapics; apic++) {
  1547. int pin;
  1548. /* See if any of the pins is in ExtINT mode */
  1549. for (pin = 0; pin < ioapics[apic].nr_registers; pin++) {
  1550. struct IO_APIC_route_entry entry;
  1551. entry = ioapic_read_entry(apic, pin);
  1552. /* If the interrupt line is enabled and in ExtInt mode
  1553. * I have found the pin where the i8259 is connected.
  1554. */
  1555. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1556. ioapic_i8259.apic = apic;
  1557. ioapic_i8259.pin = pin;
  1558. goto found_i8259;
  1559. }
  1560. }
  1561. }
  1562. found_i8259:
  1563. /* Look to see what if the MP table has reported the ExtINT */
  1564. /* If we could not find the appropriate pin by looking at the ioapic
  1565. * the i8259 probably is not connected the ioapic but give the
  1566. * mptable a chance anyway.
  1567. */
  1568. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1569. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1570. /* Trust the MP table if nothing is setup in the hardware */
  1571. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1572. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1573. ioapic_i8259.pin = i8259_pin;
  1574. ioapic_i8259.apic = i8259_apic;
  1575. }
  1576. /* Complain if the MP table and the hardware disagree */
  1577. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1578. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1579. {
  1580. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1581. }
  1582. /*
  1583. * Do not trust the IO-APIC being empty at bootup
  1584. */
  1585. clear_IO_APIC();
  1586. }
  1587. void native_disable_io_apic(void)
  1588. {
  1589. /*
  1590. * If the i8259 is routed through an IOAPIC
  1591. * Put that IOAPIC in virtual wire mode
  1592. * so legacy interrupts can be delivered.
  1593. */
  1594. if (ioapic_i8259.pin != -1) {
  1595. struct IO_APIC_route_entry entry;
  1596. memset(&entry, 0, sizeof(entry));
  1597. entry.mask = 0; /* Enabled */
  1598. entry.trigger = 0; /* Edge */
  1599. entry.irr = 0;
  1600. entry.polarity = 0; /* High */
  1601. entry.delivery_status = 0;
  1602. entry.dest_mode = 0; /* Physical */
  1603. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1604. entry.vector = 0;
  1605. entry.dest = read_apic_id();
  1606. /*
  1607. * Add it to the IO-APIC irq-routing table:
  1608. */
  1609. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1610. }
  1611. if (cpu_has_apic || apic_from_smp_config())
  1612. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1613. }
  1614. /*
  1615. * Not an __init, needed by the reboot code
  1616. */
  1617. void disable_IO_APIC(void)
  1618. {
  1619. /*
  1620. * Clear the IO-APIC before rebooting:
  1621. */
  1622. clear_IO_APIC();
  1623. if (!legacy_pic->nr_legacy_irqs)
  1624. return;
  1625. x86_io_apic_ops.disable();
  1626. }
  1627. #ifdef CONFIG_X86_32
  1628. /*
  1629. * function to set the IO-APIC physical IDs based on the
  1630. * values stored in the MPC table.
  1631. *
  1632. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1633. */
  1634. void __init setup_ioapic_ids_from_mpc_nocheck(void)
  1635. {
  1636. union IO_APIC_reg_00 reg_00;
  1637. physid_mask_t phys_id_present_map;
  1638. int ioapic_idx;
  1639. int i;
  1640. unsigned char old_id;
  1641. unsigned long flags;
  1642. /*
  1643. * This is broken; anything with a real cpu count has to
  1644. * circumvent this idiocy regardless.
  1645. */
  1646. apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
  1647. /*
  1648. * Set the IOAPIC ID to the value stored in the MPC table.
  1649. */
  1650. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  1651. /* Read the register 0 value */
  1652. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1653. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1654. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1655. old_id = mpc_ioapic_id(ioapic_idx);
  1656. if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
  1657. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1658. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1659. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1660. reg_00.bits.ID);
  1661. ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
  1662. }
  1663. /*
  1664. * Sanity check, is the ID really free? Every APIC in a
  1665. * system must have a unique ID or we get lots of nice
  1666. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1667. */
  1668. if (apic->check_apicid_used(&phys_id_present_map,
  1669. mpc_ioapic_id(ioapic_idx))) {
  1670. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1671. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1672. for (i = 0; i < get_physical_broadcast(); i++)
  1673. if (!physid_isset(i, phys_id_present_map))
  1674. break;
  1675. if (i >= get_physical_broadcast())
  1676. panic("Max APIC ID exceeded!\n");
  1677. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1678. i);
  1679. physid_set(i, phys_id_present_map);
  1680. ioapics[ioapic_idx].mp_config.apicid = i;
  1681. } else {
  1682. physid_mask_t tmp;
  1683. apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
  1684. &tmp);
  1685. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1686. "phys_id_present_map\n",
  1687. mpc_ioapic_id(ioapic_idx));
  1688. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1689. }
  1690. /*
  1691. * We need to adjust the IRQ routing table
  1692. * if the ID changed.
  1693. */
  1694. if (old_id != mpc_ioapic_id(ioapic_idx))
  1695. for (i = 0; i < mp_irq_entries; i++)
  1696. if (mp_irqs[i].dstapic == old_id)
  1697. mp_irqs[i].dstapic
  1698. = mpc_ioapic_id(ioapic_idx);
  1699. /*
  1700. * Update the ID register according to the right value
  1701. * from the MPC table if they are different.
  1702. */
  1703. if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
  1704. continue;
  1705. apic_printk(APIC_VERBOSE, KERN_INFO
  1706. "...changing IO-APIC physical APIC ID to %d ...",
  1707. mpc_ioapic_id(ioapic_idx));
  1708. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  1709. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1710. io_apic_write(ioapic_idx, 0, reg_00.raw);
  1711. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1712. /*
  1713. * Sanity check
  1714. */
  1715. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1716. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1717. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1718. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
  1719. pr_cont("could not set ID!\n");
  1720. else
  1721. apic_printk(APIC_VERBOSE, " ok.\n");
  1722. }
  1723. }
  1724. void __init setup_ioapic_ids_from_mpc(void)
  1725. {
  1726. if (acpi_ioapic)
  1727. return;
  1728. /*
  1729. * Don't check I/O APIC IDs for xAPIC systems. They have
  1730. * no meaning without the serial APIC bus.
  1731. */
  1732. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1733. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1734. return;
  1735. setup_ioapic_ids_from_mpc_nocheck();
  1736. }
  1737. #endif
  1738. int no_timer_check __initdata;
  1739. static int __init notimercheck(char *s)
  1740. {
  1741. no_timer_check = 1;
  1742. return 1;
  1743. }
  1744. __setup("no_timer_check", notimercheck);
  1745. /*
  1746. * There is a nasty bug in some older SMP boards, their mptable lies
  1747. * about the timer IRQ. We do the following to work around the situation:
  1748. *
  1749. * - timer IRQ defaults to IO-APIC IRQ
  1750. * - if this function detects that timer IRQs are defunct, then we fall
  1751. * back to ISA timer IRQs
  1752. */
  1753. static int __init timer_irq_works(void)
  1754. {
  1755. unsigned long t1 = jiffies;
  1756. unsigned long flags;
  1757. if (no_timer_check)
  1758. return 1;
  1759. local_save_flags(flags);
  1760. local_irq_enable();
  1761. /* Let ten ticks pass... */
  1762. mdelay((10 * 1000) / HZ);
  1763. local_irq_restore(flags);
  1764. /*
  1765. * Expect a few ticks at least, to be sure some possible
  1766. * glue logic does not lock up after one or two first
  1767. * ticks in a non-ExtINT mode. Also the local APIC
  1768. * might have cached one ExtINT interrupt. Finally, at
  1769. * least one tick may be lost due to delays.
  1770. */
  1771. /* jiffies wrap? */
  1772. if (time_after(jiffies, t1 + 4))
  1773. return 1;
  1774. return 0;
  1775. }
  1776. /*
  1777. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1778. * number of pending IRQ events unhandled. These cases are very rare,
  1779. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1780. * better to do it this way as thus we do not have to be aware of
  1781. * 'pending' interrupts in the IRQ path, except at this point.
  1782. */
  1783. /*
  1784. * Edge triggered needs to resend any interrupt
  1785. * that was delayed but this is now handled in the device
  1786. * independent code.
  1787. */
  1788. /*
  1789. * Starting up a edge-triggered IO-APIC interrupt is
  1790. * nasty - we need to make sure that we get the edge.
  1791. * If it is already asserted for some reason, we need
  1792. * return 1 to indicate that is was pending.
  1793. *
  1794. * This is not complete - we should be able to fake
  1795. * an edge even if it isn't on the 8259A...
  1796. */
  1797. static unsigned int startup_ioapic_irq(struct irq_data *data)
  1798. {
  1799. int was_pending = 0, irq = data->irq;
  1800. unsigned long flags;
  1801. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1802. if (irq < legacy_pic->nr_legacy_irqs) {
  1803. legacy_pic->mask(irq);
  1804. if (legacy_pic->irq_pending(irq))
  1805. was_pending = 1;
  1806. }
  1807. __unmask_ioapic(data->chip_data);
  1808. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1809. return was_pending;
  1810. }
  1811. static int ioapic_retrigger_irq(struct irq_data *data)
  1812. {
  1813. struct irq_cfg *cfg = data->chip_data;
  1814. unsigned long flags;
  1815. int cpu;
  1816. raw_spin_lock_irqsave(&vector_lock, flags);
  1817. cpu = cpumask_first_and(cfg->domain, cpu_online_mask);
  1818. apic->send_IPI_mask(cpumask_of(cpu), cfg->vector);
  1819. raw_spin_unlock_irqrestore(&vector_lock, flags);
  1820. return 1;
  1821. }
  1822. /*
  1823. * Level and edge triggered IO-APIC interrupts need different handling,
  1824. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1825. * handled with the level-triggered descriptor, but that one has slightly
  1826. * more overhead. Level-triggered interrupts cannot be handled with the
  1827. * edge-triggered handler, without risking IRQ storms and other ugly
  1828. * races.
  1829. */
  1830. #ifdef CONFIG_SMP
  1831. void send_cleanup_vector(struct irq_cfg *cfg)
  1832. {
  1833. cpumask_var_t cleanup_mask;
  1834. if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
  1835. unsigned int i;
  1836. for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
  1837. apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
  1838. } else {
  1839. cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
  1840. apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1841. free_cpumask_var(cleanup_mask);
  1842. }
  1843. cfg->move_in_progress = 0;
  1844. }
  1845. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1846. {
  1847. unsigned vector, me;
  1848. ack_APIC_irq();
  1849. irq_enter();
  1850. exit_idle();
  1851. me = smp_processor_id();
  1852. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1853. int irq;
  1854. unsigned int irr;
  1855. struct irq_desc *desc;
  1856. struct irq_cfg *cfg;
  1857. irq = __this_cpu_read(vector_irq[vector]);
  1858. if (irq <= VECTOR_UNDEFINED)
  1859. continue;
  1860. desc = irq_to_desc(irq);
  1861. if (!desc)
  1862. continue;
  1863. cfg = irq_cfg(irq);
  1864. if (!cfg)
  1865. continue;
  1866. raw_spin_lock(&desc->lock);
  1867. /*
  1868. * Check if the irq migration is in progress. If so, we
  1869. * haven't received the cleanup request yet for this irq.
  1870. */
  1871. if (cfg->move_in_progress)
  1872. goto unlock;
  1873. if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
  1874. goto unlock;
  1875. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  1876. /*
  1877. * Check if the vector that needs to be cleanedup is
  1878. * registered at the cpu's IRR. If so, then this is not
  1879. * the best time to clean it up. Lets clean it up in the
  1880. * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
  1881. * to myself.
  1882. */
  1883. if (irr & (1 << (vector % 32))) {
  1884. apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
  1885. goto unlock;
  1886. }
  1887. __this_cpu_write(vector_irq[vector], -1);
  1888. unlock:
  1889. raw_spin_unlock(&desc->lock);
  1890. }
  1891. irq_exit();
  1892. }
  1893. static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
  1894. {
  1895. unsigned me;
  1896. if (likely(!cfg->move_in_progress))
  1897. return;
  1898. me = smp_processor_id();
  1899. if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
  1900. send_cleanup_vector(cfg);
  1901. }
  1902. static void irq_complete_move(struct irq_cfg *cfg)
  1903. {
  1904. __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
  1905. }
  1906. void irq_force_complete_move(int irq)
  1907. {
  1908. struct irq_cfg *cfg = irq_get_chip_data(irq);
  1909. if (!cfg)
  1910. return;
  1911. __irq_complete_move(cfg, cfg->vector);
  1912. }
  1913. #else
  1914. static inline void irq_complete_move(struct irq_cfg *cfg) { }
  1915. #endif
  1916. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
  1917. {
  1918. int apic, pin;
  1919. struct irq_pin_list *entry;
  1920. u8 vector = cfg->vector;
  1921. for_each_irq_pin(entry, cfg->irq_2_pin) {
  1922. unsigned int reg;
  1923. apic = entry->apic;
  1924. pin = entry->pin;
  1925. io_apic_write(apic, 0x11 + pin*2, dest);
  1926. reg = io_apic_read(apic, 0x10 + pin*2);
  1927. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  1928. reg |= vector;
  1929. io_apic_modify(apic, 0x10 + pin*2, reg);
  1930. }
  1931. }
  1932. /*
  1933. * Either sets data->affinity to a valid value, and returns
  1934. * ->cpu_mask_to_apicid of that in dest_id, or returns -1 and
  1935. * leaves data->affinity untouched.
  1936. */
  1937. int __ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  1938. unsigned int *dest_id)
  1939. {
  1940. struct irq_cfg *cfg = data->chip_data;
  1941. unsigned int irq = data->irq;
  1942. int err;
  1943. if (!config_enabled(CONFIG_SMP))
  1944. return -1;
  1945. if (!cpumask_intersects(mask, cpu_online_mask))
  1946. return -EINVAL;
  1947. err = assign_irq_vector(irq, cfg, mask);
  1948. if (err)
  1949. return err;
  1950. err = apic->cpu_mask_to_apicid_and(mask, cfg->domain, dest_id);
  1951. if (err) {
  1952. if (assign_irq_vector(irq, cfg, data->affinity))
  1953. pr_err("Failed to recover vector for irq %d\n", irq);
  1954. return err;
  1955. }
  1956. cpumask_copy(data->affinity, mask);
  1957. return 0;
  1958. }
  1959. int native_ioapic_set_affinity(struct irq_data *data,
  1960. const struct cpumask *mask,
  1961. bool force)
  1962. {
  1963. unsigned int dest, irq = data->irq;
  1964. unsigned long flags;
  1965. int ret;
  1966. if (!config_enabled(CONFIG_SMP))
  1967. return -1;
  1968. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1969. ret = __ioapic_set_affinity(data, mask, &dest);
  1970. if (!ret) {
  1971. /* Only the high 8 bits are valid. */
  1972. dest = SET_APIC_LOGICAL_ID(dest);
  1973. __target_IO_APIC_irq(irq, dest, data->chip_data);
  1974. ret = IRQ_SET_MASK_OK_NOCOPY;
  1975. }
  1976. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1977. return ret;
  1978. }
  1979. static void ack_apic_edge(struct irq_data *data)
  1980. {
  1981. irq_complete_move(data->chip_data);
  1982. irq_move_irq(data);
  1983. ack_APIC_irq();
  1984. }
  1985. atomic_t irq_mis_count;
  1986. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1987. static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
  1988. {
  1989. struct irq_pin_list *entry;
  1990. unsigned long flags;
  1991. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1992. for_each_irq_pin(entry, cfg->irq_2_pin) {
  1993. unsigned int reg;
  1994. int pin;
  1995. pin = entry->pin;
  1996. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  1997. /* Is the remote IRR bit set? */
  1998. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  1999. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2000. return true;
  2001. }
  2002. }
  2003. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2004. return false;
  2005. }
  2006. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  2007. {
  2008. /* If we are moving the irq we need to mask it */
  2009. if (unlikely(irqd_is_setaffinity_pending(data))) {
  2010. mask_ioapic(cfg);
  2011. return true;
  2012. }
  2013. return false;
  2014. }
  2015. static inline void ioapic_irqd_unmask(struct irq_data *data,
  2016. struct irq_cfg *cfg, bool masked)
  2017. {
  2018. if (unlikely(masked)) {
  2019. /* Only migrate the irq if the ack has been received.
  2020. *
  2021. * On rare occasions the broadcast level triggered ack gets
  2022. * delayed going to ioapics, and if we reprogram the
  2023. * vector while Remote IRR is still set the irq will never
  2024. * fire again.
  2025. *
  2026. * To prevent this scenario we read the Remote IRR bit
  2027. * of the ioapic. This has two effects.
  2028. * - On any sane system the read of the ioapic will
  2029. * flush writes (and acks) going to the ioapic from
  2030. * this cpu.
  2031. * - We get to see if the ACK has actually been delivered.
  2032. *
  2033. * Based on failed experiments of reprogramming the
  2034. * ioapic entry from outside of irq context starting
  2035. * with masking the ioapic entry and then polling until
  2036. * Remote IRR was clear before reprogramming the
  2037. * ioapic I don't trust the Remote IRR bit to be
  2038. * completey accurate.
  2039. *
  2040. * However there appears to be no other way to plug
  2041. * this race, so if the Remote IRR bit is not
  2042. * accurate and is causing problems then it is a hardware bug
  2043. * and you can go talk to the chipset vendor about it.
  2044. */
  2045. if (!io_apic_level_ack_pending(cfg))
  2046. irq_move_masked_irq(data);
  2047. unmask_ioapic(cfg);
  2048. }
  2049. }
  2050. #else
  2051. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  2052. {
  2053. return false;
  2054. }
  2055. static inline void ioapic_irqd_unmask(struct irq_data *data,
  2056. struct irq_cfg *cfg, bool masked)
  2057. {
  2058. }
  2059. #endif
  2060. static void ack_apic_level(struct irq_data *data)
  2061. {
  2062. struct irq_cfg *cfg = data->chip_data;
  2063. int i, irq = data->irq;
  2064. unsigned long v;
  2065. bool masked;
  2066. irq_complete_move(cfg);
  2067. masked = ioapic_irqd_mask(data, cfg);
  2068. /*
  2069. * It appears there is an erratum which affects at least version 0x11
  2070. * of I/O APIC (that's the 82093AA and cores integrated into various
  2071. * chipsets). Under certain conditions a level-triggered interrupt is
  2072. * erroneously delivered as edge-triggered one but the respective IRR
  2073. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  2074. * message but it will never arrive and further interrupts are blocked
  2075. * from the source. The exact reason is so far unknown, but the
  2076. * phenomenon was observed when two consecutive interrupt requests
  2077. * from a given source get delivered to the same CPU and the source is
  2078. * temporarily disabled in between.
  2079. *
  2080. * A workaround is to simulate an EOI message manually. We achieve it
  2081. * by setting the trigger mode to edge and then to level when the edge
  2082. * trigger mode gets detected in the TMR of a local APIC for a
  2083. * level-triggered interrupt. We mask the source for the time of the
  2084. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  2085. * The idea is from Manfred Spraul. --macro
  2086. *
  2087. * Also in the case when cpu goes offline, fixup_irqs() will forward
  2088. * any unhandled interrupt on the offlined cpu to the new cpu
  2089. * destination that is handling the corresponding interrupt. This
  2090. * interrupt forwarding is done via IPI's. Hence, in this case also
  2091. * level-triggered io-apic interrupt will be seen as an edge
  2092. * interrupt in the IRR. And we can't rely on the cpu's EOI
  2093. * to be broadcasted to the IO-APIC's which will clear the remoteIRR
  2094. * corresponding to the level-triggered interrupt. Hence on IO-APIC's
  2095. * supporting EOI register, we do an explicit EOI to clear the
  2096. * remote IRR and on IO-APIC's which don't have an EOI register,
  2097. * we use the above logic (mask+edge followed by unmask+level) from
  2098. * Manfred Spraul to clear the remote IRR.
  2099. */
  2100. i = cfg->vector;
  2101. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  2102. /*
  2103. * We must acknowledge the irq before we move it or the acknowledge will
  2104. * not propagate properly.
  2105. */
  2106. ack_APIC_irq();
  2107. /*
  2108. * Tail end of clearing remote IRR bit (either by delivering the EOI
  2109. * message via io-apic EOI register write or simulating it using
  2110. * mask+edge followed by unnask+level logic) manually when the
  2111. * level triggered interrupt is seen as the edge triggered interrupt
  2112. * at the cpu.
  2113. */
  2114. if (!(v & (1 << (i & 0x1f)))) {
  2115. atomic_inc(&irq_mis_count);
  2116. eoi_ioapic_irq(irq, cfg);
  2117. }
  2118. ioapic_irqd_unmask(data, cfg, masked);
  2119. }
  2120. static struct irq_chip ioapic_chip __read_mostly = {
  2121. .name = "IO-APIC",
  2122. .irq_startup = startup_ioapic_irq,
  2123. .irq_mask = mask_ioapic_irq,
  2124. .irq_unmask = unmask_ioapic_irq,
  2125. .irq_ack = ack_apic_edge,
  2126. .irq_eoi = ack_apic_level,
  2127. .irq_set_affinity = native_ioapic_set_affinity,
  2128. .irq_retrigger = ioapic_retrigger_irq,
  2129. };
  2130. static inline void init_IO_APIC_traps(void)
  2131. {
  2132. struct irq_cfg *cfg;
  2133. unsigned int irq;
  2134. /*
  2135. * NOTE! The local APIC isn't very good at handling
  2136. * multiple interrupts at the same interrupt level.
  2137. * As the interrupt level is determined by taking the
  2138. * vector number and shifting that right by 4, we
  2139. * want to spread these out a bit so that they don't
  2140. * all fall in the same interrupt level.
  2141. *
  2142. * Also, we've got to be careful not to trash gate
  2143. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  2144. */
  2145. for_each_active_irq(irq) {
  2146. cfg = irq_get_chip_data(irq);
  2147. if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
  2148. /*
  2149. * Hmm.. We don't have an entry for this,
  2150. * so default to an old-fashioned 8259
  2151. * interrupt if we can..
  2152. */
  2153. if (irq < legacy_pic->nr_legacy_irqs)
  2154. legacy_pic->make_irq(irq);
  2155. else
  2156. /* Strange. Oh, well.. */
  2157. irq_set_chip(irq, &no_irq_chip);
  2158. }
  2159. }
  2160. }
  2161. /*
  2162. * The local APIC irq-chip implementation:
  2163. */
  2164. static void mask_lapic_irq(struct irq_data *data)
  2165. {
  2166. unsigned long v;
  2167. v = apic_read(APIC_LVT0);
  2168. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  2169. }
  2170. static void unmask_lapic_irq(struct irq_data *data)
  2171. {
  2172. unsigned long v;
  2173. v = apic_read(APIC_LVT0);
  2174. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  2175. }
  2176. static void ack_lapic_irq(struct irq_data *data)
  2177. {
  2178. ack_APIC_irq();
  2179. }
  2180. static struct irq_chip lapic_chip __read_mostly = {
  2181. .name = "local-APIC",
  2182. .irq_mask = mask_lapic_irq,
  2183. .irq_unmask = unmask_lapic_irq,
  2184. .irq_ack = ack_lapic_irq,
  2185. };
  2186. static void lapic_register_intr(int irq)
  2187. {
  2188. irq_clear_status_flags(irq, IRQ_LEVEL);
  2189. irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  2190. "edge");
  2191. }
  2192. /*
  2193. * This looks a bit hackish but it's about the only one way of sending
  2194. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  2195. * not support the ExtINT mode, unfortunately. We need to send these
  2196. * cycles as some i82489DX-based boards have glue logic that keeps the
  2197. * 8259A interrupt line asserted until INTA. --macro
  2198. */
  2199. static inline void __init unlock_ExtINT_logic(void)
  2200. {
  2201. int apic, pin, i;
  2202. struct IO_APIC_route_entry entry0, entry1;
  2203. unsigned char save_control, save_freq_select;
  2204. pin = find_isa_irq_pin(8, mp_INT);
  2205. if (pin == -1) {
  2206. WARN_ON_ONCE(1);
  2207. return;
  2208. }
  2209. apic = find_isa_irq_apic(8, mp_INT);
  2210. if (apic == -1) {
  2211. WARN_ON_ONCE(1);
  2212. return;
  2213. }
  2214. entry0 = ioapic_read_entry(apic, pin);
  2215. clear_IO_APIC_pin(apic, pin);
  2216. memset(&entry1, 0, sizeof(entry1));
  2217. entry1.dest_mode = 0; /* physical delivery */
  2218. entry1.mask = 0; /* unmask IRQ now */
  2219. entry1.dest = hard_smp_processor_id();
  2220. entry1.delivery_mode = dest_ExtINT;
  2221. entry1.polarity = entry0.polarity;
  2222. entry1.trigger = 0;
  2223. entry1.vector = 0;
  2224. ioapic_write_entry(apic, pin, entry1);
  2225. save_control = CMOS_READ(RTC_CONTROL);
  2226. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  2227. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  2228. RTC_FREQ_SELECT);
  2229. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  2230. i = 100;
  2231. while (i-- > 0) {
  2232. mdelay(10);
  2233. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  2234. i -= 10;
  2235. }
  2236. CMOS_WRITE(save_control, RTC_CONTROL);
  2237. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  2238. clear_IO_APIC_pin(apic, pin);
  2239. ioapic_write_entry(apic, pin, entry0);
  2240. }
  2241. static int disable_timer_pin_1 __initdata;
  2242. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  2243. static int __init disable_timer_pin_setup(char *arg)
  2244. {
  2245. disable_timer_pin_1 = 1;
  2246. return 0;
  2247. }
  2248. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  2249. int timer_through_8259 __initdata;
  2250. /*
  2251. * This code may look a bit paranoid, but it's supposed to cooperate with
  2252. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  2253. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  2254. * fanatically on his truly buggy board.
  2255. *
  2256. * FIXME: really need to revamp this for all platforms.
  2257. */
  2258. static inline void __init check_timer(void)
  2259. {
  2260. struct irq_cfg *cfg = irq_get_chip_data(0);
  2261. int node = cpu_to_node(0);
  2262. int apic1, pin1, apic2, pin2;
  2263. unsigned long flags;
  2264. int no_pin1 = 0;
  2265. local_irq_save(flags);
  2266. /*
  2267. * get/set the timer IRQ vector:
  2268. */
  2269. legacy_pic->mask(0);
  2270. assign_irq_vector(0, cfg, apic->target_cpus());
  2271. /*
  2272. * As IRQ0 is to be enabled in the 8259A, the virtual
  2273. * wire has to be disabled in the local APIC. Also
  2274. * timer interrupts need to be acknowledged manually in
  2275. * the 8259A for the i82489DX when using the NMI
  2276. * watchdog as that APIC treats NMIs as level-triggered.
  2277. * The AEOI mode will finish them in the 8259A
  2278. * automatically.
  2279. */
  2280. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  2281. legacy_pic->init(1);
  2282. pin1 = find_isa_irq_pin(0, mp_INT);
  2283. apic1 = find_isa_irq_apic(0, mp_INT);
  2284. pin2 = ioapic_i8259.pin;
  2285. apic2 = ioapic_i8259.apic;
  2286. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  2287. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  2288. cfg->vector, apic1, pin1, apic2, pin2);
  2289. /*
  2290. * Some BIOS writers are clueless and report the ExtINTA
  2291. * I/O APIC input from the cascaded 8259A as the timer
  2292. * interrupt input. So just in case, if only one pin
  2293. * was found above, try it both directly and through the
  2294. * 8259A.
  2295. */
  2296. if (pin1 == -1) {
  2297. panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
  2298. pin1 = pin2;
  2299. apic1 = apic2;
  2300. no_pin1 = 1;
  2301. } else if (pin2 == -1) {
  2302. pin2 = pin1;
  2303. apic2 = apic1;
  2304. }
  2305. if (pin1 != -1) {
  2306. /*
  2307. * Ok, does IRQ0 through the IOAPIC work?
  2308. */
  2309. if (no_pin1) {
  2310. add_pin_to_irq_node(cfg, node, apic1, pin1);
  2311. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  2312. } else {
  2313. /* for edge trigger, setup_ioapic_irq already
  2314. * leave it unmasked.
  2315. * so only need to unmask if it is level-trigger
  2316. * do we really have level trigger timer?
  2317. */
  2318. int idx;
  2319. idx = find_irq_entry(apic1, pin1, mp_INT);
  2320. if (idx != -1 && irq_trigger(idx))
  2321. unmask_ioapic(cfg);
  2322. }
  2323. if (timer_irq_works()) {
  2324. if (disable_timer_pin_1 > 0)
  2325. clear_IO_APIC_pin(0, pin1);
  2326. goto out;
  2327. }
  2328. panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
  2329. local_irq_disable();
  2330. clear_IO_APIC_pin(apic1, pin1);
  2331. if (!no_pin1)
  2332. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  2333. "8254 timer not connected to IO-APIC\n");
  2334. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  2335. "(IRQ0) through the 8259A ...\n");
  2336. apic_printk(APIC_QUIET, KERN_INFO
  2337. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  2338. /*
  2339. * legacy devices should be connected to IO APIC #0
  2340. */
  2341. replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
  2342. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  2343. legacy_pic->unmask(0);
  2344. if (timer_irq_works()) {
  2345. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  2346. timer_through_8259 = 1;
  2347. goto out;
  2348. }
  2349. /*
  2350. * Cleanup, just in case ...
  2351. */
  2352. local_irq_disable();
  2353. legacy_pic->mask(0);
  2354. clear_IO_APIC_pin(apic2, pin2);
  2355. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  2356. }
  2357. apic_printk(APIC_QUIET, KERN_INFO
  2358. "...trying to set up timer as Virtual Wire IRQ...\n");
  2359. lapic_register_intr(0);
  2360. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  2361. legacy_pic->unmask(0);
  2362. if (timer_irq_works()) {
  2363. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2364. goto out;
  2365. }
  2366. local_irq_disable();
  2367. legacy_pic->mask(0);
  2368. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  2369. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  2370. apic_printk(APIC_QUIET, KERN_INFO
  2371. "...trying to set up timer as ExtINT IRQ...\n");
  2372. legacy_pic->init(0);
  2373. legacy_pic->make_irq(0);
  2374. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  2375. unlock_ExtINT_logic();
  2376. if (timer_irq_works()) {
  2377. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  2378. goto out;
  2379. }
  2380. local_irq_disable();
  2381. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  2382. if (x2apic_preenabled)
  2383. apic_printk(APIC_QUIET, KERN_INFO
  2384. "Perhaps problem with the pre-enabled x2apic mode\n"
  2385. "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
  2386. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  2387. "report. Then try booting with the 'noapic' option.\n");
  2388. out:
  2389. local_irq_restore(flags);
  2390. }
  2391. /*
  2392. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  2393. * to devices. However there may be an I/O APIC pin available for
  2394. * this interrupt regardless. The pin may be left unconnected, but
  2395. * typically it will be reused as an ExtINT cascade interrupt for
  2396. * the master 8259A. In the MPS case such a pin will normally be
  2397. * reported as an ExtINT interrupt in the MP table. With ACPI
  2398. * there is no provision for ExtINT interrupts, and in the absence
  2399. * of an override it would be treated as an ordinary ISA I/O APIC
  2400. * interrupt, that is edge-triggered and unmasked by default. We
  2401. * used to do this, but it caused problems on some systems because
  2402. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  2403. * the same ExtINT cascade interrupt to drive the local APIC of the
  2404. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  2405. * the I/O APIC in all cases now. No actual device should request
  2406. * it anyway. --macro
  2407. */
  2408. #define PIC_IRQS (1UL << PIC_CASCADE_IR)
  2409. void __init setup_IO_APIC(void)
  2410. {
  2411. /*
  2412. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2413. */
  2414. io_apic_irqs = legacy_pic->nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
  2415. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2416. /*
  2417. * Set up IO-APIC IRQ routing.
  2418. */
  2419. x86_init.mpparse.setup_ioapic_ids();
  2420. sync_Arb_IDs();
  2421. setup_IO_APIC_irqs();
  2422. init_IO_APIC_traps();
  2423. if (legacy_pic->nr_legacy_irqs)
  2424. check_timer();
  2425. }
  2426. /*
  2427. * Called after all the initialization is done. If we didn't find any
  2428. * APIC bugs then we can allow the modify fast path
  2429. */
  2430. static int __init io_apic_bug_finalize(void)
  2431. {
  2432. if (sis_apic_bug == -1)
  2433. sis_apic_bug = 0;
  2434. return 0;
  2435. }
  2436. late_initcall(io_apic_bug_finalize);
  2437. static void resume_ioapic_id(int ioapic_idx)
  2438. {
  2439. unsigned long flags;
  2440. union IO_APIC_reg_00 reg_00;
  2441. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2442. reg_00.raw = io_apic_read(ioapic_idx, 0);
  2443. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
  2444. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  2445. io_apic_write(ioapic_idx, 0, reg_00.raw);
  2446. }
  2447. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2448. }
  2449. static void ioapic_resume(void)
  2450. {
  2451. int ioapic_idx;
  2452. for (ioapic_idx = nr_ioapics - 1; ioapic_idx >= 0; ioapic_idx--)
  2453. resume_ioapic_id(ioapic_idx);
  2454. restore_ioapic_entries();
  2455. }
  2456. static struct syscore_ops ioapic_syscore_ops = {
  2457. .suspend = save_ioapic_entries,
  2458. .resume = ioapic_resume,
  2459. };
  2460. static int __init ioapic_init_ops(void)
  2461. {
  2462. register_syscore_ops(&ioapic_syscore_ops);
  2463. return 0;
  2464. }
  2465. device_initcall(ioapic_init_ops);
  2466. /*
  2467. * Dynamic irq allocate and deallocation
  2468. */
  2469. unsigned int __create_irqs(unsigned int from, unsigned int count, int node)
  2470. {
  2471. struct irq_cfg **cfg;
  2472. unsigned long flags;
  2473. int irq, i;
  2474. if (from < nr_irqs_gsi)
  2475. from = nr_irqs_gsi;
  2476. cfg = kzalloc_node(count * sizeof(cfg[0]), GFP_KERNEL, node);
  2477. if (!cfg)
  2478. return 0;
  2479. irq = alloc_irqs_from(from, count, node);
  2480. if (irq < 0)
  2481. goto out_cfgs;
  2482. for (i = 0; i < count; i++) {
  2483. cfg[i] = alloc_irq_cfg(irq + i, node);
  2484. if (!cfg[i])
  2485. goto out_irqs;
  2486. }
  2487. raw_spin_lock_irqsave(&vector_lock, flags);
  2488. for (i = 0; i < count; i++)
  2489. if (__assign_irq_vector(irq + i, cfg[i], apic->target_cpus()))
  2490. goto out_vecs;
  2491. raw_spin_unlock_irqrestore(&vector_lock, flags);
  2492. for (i = 0; i < count; i++) {
  2493. irq_set_chip_data(irq + i, cfg[i]);
  2494. irq_clear_status_flags(irq + i, IRQ_NOREQUEST);
  2495. }
  2496. kfree(cfg);
  2497. return irq;
  2498. out_vecs:
  2499. for (i--; i >= 0; i--)
  2500. __clear_irq_vector(irq + i, cfg[i]);
  2501. raw_spin_unlock_irqrestore(&vector_lock, flags);
  2502. out_irqs:
  2503. for (i = 0; i < count; i++)
  2504. free_irq_at(irq + i, cfg[i]);
  2505. out_cfgs:
  2506. kfree(cfg);
  2507. return 0;
  2508. }
  2509. unsigned int create_irq_nr(unsigned int from, int node)
  2510. {
  2511. return __create_irqs(from, 1, node);
  2512. }
  2513. int create_irq(void)
  2514. {
  2515. int node = cpu_to_node(0);
  2516. unsigned int irq_want;
  2517. int irq;
  2518. irq_want = nr_irqs_gsi;
  2519. irq = create_irq_nr(irq_want, node);
  2520. if (irq == 0)
  2521. irq = -1;
  2522. return irq;
  2523. }
  2524. void destroy_irq(unsigned int irq)
  2525. {
  2526. struct irq_cfg *cfg = irq_get_chip_data(irq);
  2527. unsigned long flags;
  2528. irq_set_status_flags(irq, IRQ_NOREQUEST|IRQ_NOPROBE);
  2529. free_remapped_irq(irq);
  2530. raw_spin_lock_irqsave(&vector_lock, flags);
  2531. __clear_irq_vector(irq, cfg);
  2532. raw_spin_unlock_irqrestore(&vector_lock, flags);
  2533. free_irq_at(irq, cfg);
  2534. }
  2535. void destroy_irqs(unsigned int irq, unsigned int count)
  2536. {
  2537. unsigned int i;
  2538. for (i = 0; i < count; i++)
  2539. destroy_irq(irq + i);
  2540. }
  2541. /*
  2542. * MSI message composition
  2543. */
  2544. void native_compose_msi_msg(struct pci_dev *pdev,
  2545. unsigned int irq, unsigned int dest,
  2546. struct msi_msg *msg, u8 hpet_id)
  2547. {
  2548. struct irq_cfg *cfg = irq_cfg(irq);
  2549. msg->address_hi = MSI_ADDR_BASE_HI;
  2550. if (x2apic_enabled())
  2551. msg->address_hi |= MSI_ADDR_EXT_DEST_ID(dest);
  2552. msg->address_lo =
  2553. MSI_ADDR_BASE_LO |
  2554. ((apic->irq_dest_mode == 0) ?
  2555. MSI_ADDR_DEST_MODE_PHYSICAL:
  2556. MSI_ADDR_DEST_MODE_LOGICAL) |
  2557. ((apic->irq_delivery_mode != dest_LowestPrio) ?
  2558. MSI_ADDR_REDIRECTION_CPU:
  2559. MSI_ADDR_REDIRECTION_LOWPRI) |
  2560. MSI_ADDR_DEST_ID(dest);
  2561. msg->data =
  2562. MSI_DATA_TRIGGER_EDGE |
  2563. MSI_DATA_LEVEL_ASSERT |
  2564. ((apic->irq_delivery_mode != dest_LowestPrio) ?
  2565. MSI_DATA_DELIVERY_FIXED:
  2566. MSI_DATA_DELIVERY_LOWPRI) |
  2567. MSI_DATA_VECTOR(cfg->vector);
  2568. }
  2569. #ifdef CONFIG_PCI_MSI
  2570. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
  2571. struct msi_msg *msg, u8 hpet_id)
  2572. {
  2573. struct irq_cfg *cfg;
  2574. int err;
  2575. unsigned dest;
  2576. if (disable_apic)
  2577. return -ENXIO;
  2578. cfg = irq_cfg(irq);
  2579. err = assign_irq_vector(irq, cfg, apic->target_cpus());
  2580. if (err)
  2581. return err;
  2582. err = apic->cpu_mask_to_apicid_and(cfg->domain,
  2583. apic->target_cpus(), &dest);
  2584. if (err)
  2585. return err;
  2586. x86_msi.compose_msi_msg(pdev, irq, dest, msg, hpet_id);
  2587. return 0;
  2588. }
  2589. static int
  2590. msi_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
  2591. {
  2592. struct irq_cfg *cfg = data->chip_data;
  2593. struct msi_msg msg;
  2594. unsigned int dest;
  2595. if (__ioapic_set_affinity(data, mask, &dest))
  2596. return -1;
  2597. __get_cached_msi_msg(data->msi_desc, &msg);
  2598. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2599. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2600. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2601. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2602. __write_msi_msg(data->msi_desc, &msg);
  2603. return IRQ_SET_MASK_OK_NOCOPY;
  2604. }
  2605. /*
  2606. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2607. * which implement the MSI or MSI-X Capability Structure.
  2608. */
  2609. static struct irq_chip msi_chip = {
  2610. .name = "PCI-MSI",
  2611. .irq_unmask = unmask_msi_irq,
  2612. .irq_mask = mask_msi_irq,
  2613. .irq_ack = ack_apic_edge,
  2614. .irq_set_affinity = msi_set_affinity,
  2615. .irq_retrigger = ioapic_retrigger_irq,
  2616. };
  2617. int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc,
  2618. unsigned int irq_base, unsigned int irq_offset)
  2619. {
  2620. struct irq_chip *chip = &msi_chip;
  2621. struct msi_msg msg;
  2622. unsigned int irq = irq_base + irq_offset;
  2623. int ret;
  2624. ret = msi_compose_msg(dev, irq, &msg, -1);
  2625. if (ret < 0)
  2626. return ret;
  2627. irq_set_msi_desc_off(irq_base, irq_offset, msidesc);
  2628. /*
  2629. * MSI-X message is written per-IRQ, the offset is always 0.
  2630. * MSI message denotes a contiguous group of IRQs, written for 0th IRQ.
  2631. */
  2632. if (!irq_offset)
  2633. write_msi_msg(irq, &msg);
  2634. setup_remapped_irq(irq, irq_get_chip_data(irq), chip);
  2635. irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
  2636. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
  2637. return 0;
  2638. }
  2639. int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2640. {
  2641. unsigned int irq, irq_want;
  2642. struct msi_desc *msidesc;
  2643. int node, ret;
  2644. /* Multiple MSI vectors only supported with interrupt remapping */
  2645. if (type == PCI_CAP_ID_MSI && nvec > 1)
  2646. return 1;
  2647. node = dev_to_node(&dev->dev);
  2648. irq_want = nr_irqs_gsi;
  2649. list_for_each_entry(msidesc, &dev->msi_list, list) {
  2650. irq = create_irq_nr(irq_want, node);
  2651. if (irq == 0)
  2652. return -ENOSPC;
  2653. irq_want = irq + 1;
  2654. ret = setup_msi_irq(dev, msidesc, irq, 0);
  2655. if (ret < 0)
  2656. goto error;
  2657. }
  2658. return 0;
  2659. error:
  2660. destroy_irq(irq);
  2661. return ret;
  2662. }
  2663. void native_teardown_msi_irq(unsigned int irq)
  2664. {
  2665. destroy_irq(irq);
  2666. }
  2667. #ifdef CONFIG_DMAR_TABLE
  2668. static int
  2669. dmar_msi_set_affinity(struct irq_data *data, const struct cpumask *mask,
  2670. bool force)
  2671. {
  2672. struct irq_cfg *cfg = data->chip_data;
  2673. unsigned int dest, irq = data->irq;
  2674. struct msi_msg msg;
  2675. if (__ioapic_set_affinity(data, mask, &dest))
  2676. return -1;
  2677. dmar_msi_read(irq, &msg);
  2678. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2679. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2680. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2681. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2682. msg.address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(dest);
  2683. dmar_msi_write(irq, &msg);
  2684. return IRQ_SET_MASK_OK_NOCOPY;
  2685. }
  2686. static struct irq_chip dmar_msi_type = {
  2687. .name = "DMAR_MSI",
  2688. .irq_unmask = dmar_msi_unmask,
  2689. .irq_mask = dmar_msi_mask,
  2690. .irq_ack = ack_apic_edge,
  2691. .irq_set_affinity = dmar_msi_set_affinity,
  2692. .irq_retrigger = ioapic_retrigger_irq,
  2693. };
  2694. int arch_setup_dmar_msi(unsigned int irq)
  2695. {
  2696. int ret;
  2697. struct msi_msg msg;
  2698. ret = msi_compose_msg(NULL, irq, &msg, -1);
  2699. if (ret < 0)
  2700. return ret;
  2701. dmar_msi_write(irq, &msg);
  2702. irq_set_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2703. "edge");
  2704. return 0;
  2705. }
  2706. #endif
  2707. #ifdef CONFIG_HPET_TIMER
  2708. static int hpet_msi_set_affinity(struct irq_data *data,
  2709. const struct cpumask *mask, bool force)
  2710. {
  2711. struct irq_cfg *cfg = data->chip_data;
  2712. struct msi_msg msg;
  2713. unsigned int dest;
  2714. if (__ioapic_set_affinity(data, mask, &dest))
  2715. return -1;
  2716. hpet_msi_read(data->handler_data, &msg);
  2717. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2718. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2719. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2720. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2721. hpet_msi_write(data->handler_data, &msg);
  2722. return IRQ_SET_MASK_OK_NOCOPY;
  2723. }
  2724. static struct irq_chip hpet_msi_type = {
  2725. .name = "HPET_MSI",
  2726. .irq_unmask = hpet_msi_unmask,
  2727. .irq_mask = hpet_msi_mask,
  2728. .irq_ack = ack_apic_edge,
  2729. .irq_set_affinity = hpet_msi_set_affinity,
  2730. .irq_retrigger = ioapic_retrigger_irq,
  2731. };
  2732. int default_setup_hpet_msi(unsigned int irq, unsigned int id)
  2733. {
  2734. struct irq_chip *chip = &hpet_msi_type;
  2735. struct msi_msg msg;
  2736. int ret;
  2737. ret = msi_compose_msg(NULL, irq, &msg, id);
  2738. if (ret < 0)
  2739. return ret;
  2740. hpet_msi_write(irq_get_handler_data(irq), &msg);
  2741. irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
  2742. setup_remapped_irq(irq, irq_get_chip_data(irq), chip);
  2743. irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
  2744. return 0;
  2745. }
  2746. #endif
  2747. #endif /* CONFIG_PCI_MSI */
  2748. /*
  2749. * Hypertransport interrupt support
  2750. */
  2751. #ifdef CONFIG_HT_IRQ
  2752. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  2753. {
  2754. struct ht_irq_msg msg;
  2755. fetch_ht_irq_msg(irq, &msg);
  2756. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  2757. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2758. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  2759. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2760. write_ht_irq_msg(irq, &msg);
  2761. }
  2762. static int
  2763. ht_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
  2764. {
  2765. struct irq_cfg *cfg = data->chip_data;
  2766. unsigned int dest;
  2767. if (__ioapic_set_affinity(data, mask, &dest))
  2768. return -1;
  2769. target_ht_irq(data->irq, dest, cfg->vector);
  2770. return IRQ_SET_MASK_OK_NOCOPY;
  2771. }
  2772. static struct irq_chip ht_irq_chip = {
  2773. .name = "PCI-HT",
  2774. .irq_mask = mask_ht_irq,
  2775. .irq_unmask = unmask_ht_irq,
  2776. .irq_ack = ack_apic_edge,
  2777. .irq_set_affinity = ht_set_affinity,
  2778. .irq_retrigger = ioapic_retrigger_irq,
  2779. };
  2780. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2781. {
  2782. struct irq_cfg *cfg;
  2783. struct ht_irq_msg msg;
  2784. unsigned dest;
  2785. int err;
  2786. if (disable_apic)
  2787. return -ENXIO;
  2788. cfg = irq_cfg(irq);
  2789. err = assign_irq_vector(irq, cfg, apic->target_cpus());
  2790. if (err)
  2791. return err;
  2792. err = apic->cpu_mask_to_apicid_and(cfg->domain,
  2793. apic->target_cpus(), &dest);
  2794. if (err)
  2795. return err;
  2796. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2797. msg.address_lo =
  2798. HT_IRQ_LOW_BASE |
  2799. HT_IRQ_LOW_DEST_ID(dest) |
  2800. HT_IRQ_LOW_VECTOR(cfg->vector) |
  2801. ((apic->irq_dest_mode == 0) ?
  2802. HT_IRQ_LOW_DM_PHYSICAL :
  2803. HT_IRQ_LOW_DM_LOGICAL) |
  2804. HT_IRQ_LOW_RQEOI_EDGE |
  2805. ((apic->irq_delivery_mode != dest_LowestPrio) ?
  2806. HT_IRQ_LOW_MT_FIXED :
  2807. HT_IRQ_LOW_MT_ARBITRATED) |
  2808. HT_IRQ_LOW_IRQ_MASKED;
  2809. write_ht_irq_msg(irq, &msg);
  2810. irq_set_chip_and_handler_name(irq, &ht_irq_chip,
  2811. handle_edge_irq, "edge");
  2812. dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
  2813. return 0;
  2814. }
  2815. #endif /* CONFIG_HT_IRQ */
  2816. static int
  2817. io_apic_setup_irq_pin(unsigned int irq, int node, struct io_apic_irq_attr *attr)
  2818. {
  2819. struct irq_cfg *cfg = alloc_irq_and_cfg_at(irq, node);
  2820. int ret;
  2821. if (!cfg)
  2822. return -EINVAL;
  2823. ret = __add_pin_to_irq_node(cfg, node, attr->ioapic, attr->ioapic_pin);
  2824. if (!ret)
  2825. setup_ioapic_irq(irq, cfg, attr);
  2826. return ret;
  2827. }
  2828. int io_apic_setup_irq_pin_once(unsigned int irq, int node,
  2829. struct io_apic_irq_attr *attr)
  2830. {
  2831. unsigned int ioapic_idx = attr->ioapic, pin = attr->ioapic_pin;
  2832. int ret;
  2833. struct IO_APIC_route_entry orig_entry;
  2834. /* Avoid redundant programming */
  2835. if (test_bit(pin, ioapics[ioapic_idx].pin_programmed)) {
  2836. pr_debug("Pin %d-%d already programmed\n", mpc_ioapic_id(ioapic_idx), pin);
  2837. orig_entry = ioapic_read_entry(attr->ioapic, pin);
  2838. if (attr->trigger == orig_entry.trigger && attr->polarity == orig_entry.polarity)
  2839. return 0;
  2840. return -EBUSY;
  2841. }
  2842. ret = io_apic_setup_irq_pin(irq, node, attr);
  2843. if (!ret)
  2844. set_bit(pin, ioapics[ioapic_idx].pin_programmed);
  2845. return ret;
  2846. }
  2847. static int __init io_apic_get_redir_entries(int ioapic)
  2848. {
  2849. union IO_APIC_reg_01 reg_01;
  2850. unsigned long flags;
  2851. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2852. reg_01.raw = io_apic_read(ioapic, 1);
  2853. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2854. /* The register returns the maximum index redir index
  2855. * supported, which is one less than the total number of redir
  2856. * entries.
  2857. */
  2858. return reg_01.bits.entries + 1;
  2859. }
  2860. static void __init probe_nr_irqs_gsi(void)
  2861. {
  2862. int nr;
  2863. nr = gsi_top + NR_IRQS_LEGACY;
  2864. if (nr > nr_irqs_gsi)
  2865. nr_irqs_gsi = nr;
  2866. printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
  2867. }
  2868. int get_nr_irqs_gsi(void)
  2869. {
  2870. return nr_irqs_gsi;
  2871. }
  2872. int __init arch_probe_nr_irqs(void)
  2873. {
  2874. int nr;
  2875. if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
  2876. nr_irqs = NR_VECTORS * nr_cpu_ids;
  2877. nr = nr_irqs_gsi + 8 * nr_cpu_ids;
  2878. #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
  2879. /*
  2880. * for MSI and HT dyn irq
  2881. */
  2882. nr += nr_irqs_gsi * 16;
  2883. #endif
  2884. if (nr < nr_irqs)
  2885. nr_irqs = nr;
  2886. return NR_IRQS_LEGACY;
  2887. }
  2888. int io_apic_set_pci_routing(struct device *dev, int irq,
  2889. struct io_apic_irq_attr *irq_attr)
  2890. {
  2891. int node;
  2892. if (!IO_APIC_IRQ(irq)) {
  2893. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2894. irq_attr->ioapic);
  2895. return -EINVAL;
  2896. }
  2897. node = dev ? dev_to_node(dev) : cpu_to_node(0);
  2898. return io_apic_setup_irq_pin_once(irq, node, irq_attr);
  2899. }
  2900. #ifdef CONFIG_X86_32
  2901. static int __init io_apic_get_unique_id(int ioapic, int apic_id)
  2902. {
  2903. union IO_APIC_reg_00 reg_00;
  2904. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2905. physid_mask_t tmp;
  2906. unsigned long flags;
  2907. int i = 0;
  2908. /*
  2909. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2910. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2911. * supports up to 16 on one shared APIC bus.
  2912. *
  2913. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2914. * advantage of new APIC bus architecture.
  2915. */
  2916. if (physids_empty(apic_id_map))
  2917. apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
  2918. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2919. reg_00.raw = io_apic_read(ioapic, 0);
  2920. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2921. if (apic_id >= get_physical_broadcast()) {
  2922. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2923. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2924. apic_id = reg_00.bits.ID;
  2925. }
  2926. /*
  2927. * Every APIC in a system must have a unique ID or we get lots of nice
  2928. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2929. */
  2930. if (apic->check_apicid_used(&apic_id_map, apic_id)) {
  2931. for (i = 0; i < get_physical_broadcast(); i++) {
  2932. if (!apic->check_apicid_used(&apic_id_map, i))
  2933. break;
  2934. }
  2935. if (i == get_physical_broadcast())
  2936. panic("Max apic_id exceeded!\n");
  2937. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2938. "trying %d\n", ioapic, apic_id, i);
  2939. apic_id = i;
  2940. }
  2941. apic->apicid_to_cpu_present(apic_id, &tmp);
  2942. physids_or(apic_id_map, apic_id_map, tmp);
  2943. if (reg_00.bits.ID != apic_id) {
  2944. reg_00.bits.ID = apic_id;
  2945. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2946. io_apic_write(ioapic, 0, reg_00.raw);
  2947. reg_00.raw = io_apic_read(ioapic, 0);
  2948. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2949. /* Sanity check */
  2950. if (reg_00.bits.ID != apic_id) {
  2951. pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
  2952. ioapic);
  2953. return -1;
  2954. }
  2955. }
  2956. apic_printk(APIC_VERBOSE, KERN_INFO
  2957. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2958. return apic_id;
  2959. }
  2960. static u8 __init io_apic_unique_id(u8 id)
  2961. {
  2962. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  2963. !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  2964. return io_apic_get_unique_id(nr_ioapics, id);
  2965. else
  2966. return id;
  2967. }
  2968. #else
  2969. static u8 __init io_apic_unique_id(u8 id)
  2970. {
  2971. int i;
  2972. DECLARE_BITMAP(used, 256);
  2973. bitmap_zero(used, 256);
  2974. for (i = 0; i < nr_ioapics; i++) {
  2975. __set_bit(mpc_ioapic_id(i), used);
  2976. }
  2977. if (!test_bit(id, used))
  2978. return id;
  2979. return find_first_zero_bit(used, 256);
  2980. }
  2981. #endif
  2982. static int __init io_apic_get_version(int ioapic)
  2983. {
  2984. union IO_APIC_reg_01 reg_01;
  2985. unsigned long flags;
  2986. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2987. reg_01.raw = io_apic_read(ioapic, 1);
  2988. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2989. return reg_01.bits.version;
  2990. }
  2991. int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
  2992. {
  2993. int ioapic, pin, idx;
  2994. if (skip_ioapic_setup)
  2995. return -1;
  2996. ioapic = mp_find_ioapic(gsi);
  2997. if (ioapic < 0)
  2998. return -1;
  2999. pin = mp_find_ioapic_pin(ioapic, gsi);
  3000. if (pin < 0)
  3001. return -1;
  3002. idx = find_irq_entry(ioapic, pin, mp_INT);
  3003. if (idx < 0)
  3004. return -1;
  3005. *trigger = irq_trigger(idx);
  3006. *polarity = irq_polarity(idx);
  3007. return 0;
  3008. }
  3009. /*
  3010. * This function currently is only a helper for the i386 smp boot process where
  3011. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  3012. * so mask in all cases should simply be apic->target_cpus()
  3013. */
  3014. #ifdef CONFIG_SMP
  3015. void __init setup_ioapic_dest(void)
  3016. {
  3017. int pin, ioapic, irq, irq_entry;
  3018. const struct cpumask *mask;
  3019. struct irq_data *idata;
  3020. if (skip_ioapic_setup == 1)
  3021. return;
  3022. for (ioapic = 0; ioapic < nr_ioapics; ioapic++)
  3023. for (pin = 0; pin < ioapics[ioapic].nr_registers; pin++) {
  3024. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  3025. if (irq_entry == -1)
  3026. continue;
  3027. irq = pin_2_irq(irq_entry, ioapic, pin);
  3028. if ((ioapic > 0) && (irq > 16))
  3029. continue;
  3030. idata = irq_get_irq_data(irq);
  3031. /*
  3032. * Honour affinities which have been set in early boot
  3033. */
  3034. if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
  3035. mask = idata->affinity;
  3036. else
  3037. mask = apic->target_cpus();
  3038. x86_io_apic_ops.set_affinity(idata, mask, false);
  3039. }
  3040. }
  3041. #endif
  3042. #define IOAPIC_RESOURCE_NAME_SIZE 11
  3043. static struct resource *ioapic_resources;
  3044. static struct resource * __init ioapic_setup_resources(int nr_ioapics)
  3045. {
  3046. unsigned long n;
  3047. struct resource *res;
  3048. char *mem;
  3049. int i;
  3050. if (nr_ioapics <= 0)
  3051. return NULL;
  3052. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  3053. n *= nr_ioapics;
  3054. mem = alloc_bootmem(n);
  3055. res = (void *)mem;
  3056. mem += sizeof(struct resource) * nr_ioapics;
  3057. for (i = 0; i < nr_ioapics; i++) {
  3058. res[i].name = mem;
  3059. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  3060. snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
  3061. mem += IOAPIC_RESOURCE_NAME_SIZE;
  3062. }
  3063. ioapic_resources = res;
  3064. return res;
  3065. }
  3066. void __init native_io_apic_init_mappings(void)
  3067. {
  3068. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  3069. struct resource *ioapic_res;
  3070. int i;
  3071. ioapic_res = ioapic_setup_resources(nr_ioapics);
  3072. for (i = 0; i < nr_ioapics; i++) {
  3073. if (smp_found_config) {
  3074. ioapic_phys = mpc_ioapic_addr(i);
  3075. #ifdef CONFIG_X86_32
  3076. if (!ioapic_phys) {
  3077. printk(KERN_ERR
  3078. "WARNING: bogus zero IO-APIC "
  3079. "address found in MPTABLE, "
  3080. "disabling IO/APIC support!\n");
  3081. smp_found_config = 0;
  3082. skip_ioapic_setup = 1;
  3083. goto fake_ioapic_page;
  3084. }
  3085. #endif
  3086. } else {
  3087. #ifdef CONFIG_X86_32
  3088. fake_ioapic_page:
  3089. #endif
  3090. ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
  3091. ioapic_phys = __pa(ioapic_phys);
  3092. }
  3093. set_fixmap_nocache(idx, ioapic_phys);
  3094. apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
  3095. __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
  3096. ioapic_phys);
  3097. idx++;
  3098. ioapic_res->start = ioapic_phys;
  3099. ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
  3100. ioapic_res++;
  3101. }
  3102. probe_nr_irqs_gsi();
  3103. }
  3104. void __init ioapic_insert_resources(void)
  3105. {
  3106. int i;
  3107. struct resource *r = ioapic_resources;
  3108. if (!r) {
  3109. if (nr_ioapics > 0)
  3110. printk(KERN_ERR
  3111. "IO APIC resources couldn't be allocated.\n");
  3112. return;
  3113. }
  3114. for (i = 0; i < nr_ioapics; i++) {
  3115. insert_resource(&iomem_resource, r);
  3116. r++;
  3117. }
  3118. }
  3119. int mp_find_ioapic(u32 gsi)
  3120. {
  3121. int i = 0;
  3122. if (nr_ioapics == 0)
  3123. return -1;
  3124. /* Find the IOAPIC that manages this GSI. */
  3125. for (i = 0; i < nr_ioapics; i++) {
  3126. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
  3127. if ((gsi >= gsi_cfg->gsi_base)
  3128. && (gsi <= gsi_cfg->gsi_end))
  3129. return i;
  3130. }
  3131. printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
  3132. return -1;
  3133. }
  3134. int mp_find_ioapic_pin(int ioapic, u32 gsi)
  3135. {
  3136. struct mp_ioapic_gsi *gsi_cfg;
  3137. if (WARN_ON(ioapic == -1))
  3138. return -1;
  3139. gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  3140. if (WARN_ON(gsi > gsi_cfg->gsi_end))
  3141. return -1;
  3142. return gsi - gsi_cfg->gsi_base;
  3143. }
  3144. static __init int bad_ioapic(unsigned long address)
  3145. {
  3146. if (nr_ioapics >= MAX_IO_APICS) {
  3147. pr_warn("WARNING: Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
  3148. MAX_IO_APICS, nr_ioapics);
  3149. return 1;
  3150. }
  3151. if (!address) {
  3152. pr_warn("WARNING: Bogus (zero) I/O APIC address found in table, skipping!\n");
  3153. return 1;
  3154. }
  3155. return 0;
  3156. }
  3157. static __init int bad_ioapic_register(int idx)
  3158. {
  3159. union IO_APIC_reg_00 reg_00;
  3160. union IO_APIC_reg_01 reg_01;
  3161. union IO_APIC_reg_02 reg_02;
  3162. reg_00.raw = io_apic_read(idx, 0);
  3163. reg_01.raw = io_apic_read(idx, 1);
  3164. reg_02.raw = io_apic_read(idx, 2);
  3165. if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
  3166. pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
  3167. mpc_ioapic_addr(idx));
  3168. return 1;
  3169. }
  3170. return 0;
  3171. }
  3172. void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
  3173. {
  3174. int idx = 0;
  3175. int entries;
  3176. struct mp_ioapic_gsi *gsi_cfg;
  3177. if (bad_ioapic(address))
  3178. return;
  3179. idx = nr_ioapics;
  3180. ioapics[idx].mp_config.type = MP_IOAPIC;
  3181. ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
  3182. ioapics[idx].mp_config.apicaddr = address;
  3183. set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
  3184. if (bad_ioapic_register(idx)) {
  3185. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  3186. return;
  3187. }
  3188. ioapics[idx].mp_config.apicid = io_apic_unique_id(id);
  3189. ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
  3190. /*
  3191. * Build basic GSI lookup table to facilitate gsi->io_apic lookups
  3192. * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
  3193. */
  3194. entries = io_apic_get_redir_entries(idx);
  3195. gsi_cfg = mp_ioapic_gsi_routing(idx);
  3196. gsi_cfg->gsi_base = gsi_base;
  3197. gsi_cfg->gsi_end = gsi_base + entries - 1;
  3198. /*
  3199. * The number of IO-APIC IRQ registers (== #pins):
  3200. */
  3201. ioapics[idx].nr_registers = entries;
  3202. if (gsi_cfg->gsi_end >= gsi_top)
  3203. gsi_top = gsi_cfg->gsi_end + 1;
  3204. pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
  3205. idx, mpc_ioapic_id(idx),
  3206. mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
  3207. gsi_cfg->gsi_base, gsi_cfg->gsi_end);
  3208. nr_ioapics++;
  3209. }
  3210. /* Enable IOAPIC early just for system timer */
  3211. void __init pre_init_apic_IRQ0(void)
  3212. {
  3213. struct io_apic_irq_attr attr = { 0, 0, 0, 0 };
  3214. printk(KERN_INFO "Early APIC setup for system timer0\n");
  3215. #ifndef CONFIG_SMP
  3216. physid_set_mask_of_physid(boot_cpu_physical_apicid,
  3217. &phys_cpu_present_map);
  3218. #endif
  3219. setup_local_APIC();
  3220. io_apic_setup_irq_pin(0, 0, &attr);
  3221. irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
  3222. "edge");
  3223. }