io_apic_64.c 93 KB

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