io_apic.c 92 KB

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