io_apic.c 99 KB

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