io_apic.c 92 KB

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