io_apic.c 102 KB

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