io_apic_64.c 77 KB

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