io_apic.c 99 KB

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