io_apic_64.c 71 KB

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