io_apic.c 96 KB

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