io_apic_64.c 76 KB

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