io_apic.c 98 KB

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