io_apic.c 97 KB

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