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