io_apic.c 93 KB

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