io_apic_64.c 77 KB

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