io_apic.c 93 KB

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