io_apic.c 99 KB

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