io_apic.c 95 KB

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