io_apic.c 94 KB

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