io_apic.c 99 KB

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