io_apic_64.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974
  1. /*
  2. * Intel IO-APIC support for multi-Pentium hosts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
  5. *
  6. * Many thanks to Stig Venaas for trying out countless experimental
  7. * patches and reporting/debugging problems patiently!
  8. *
  9. * (c) 1999, Multiple IO-APIC support, developed by
  10. * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
  11. * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
  12. * further tested and cleaned up by Zach Brown <zab@redhat.com>
  13. * and Ingo Molnar <mingo@redhat.com>
  14. *
  15. * Fixes
  16. * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
  17. * thanks to Eric Gilmore
  18. * and Rolf G. Tews
  19. * for testing these extensively
  20. * Paul Diefenbaugh : Added full ACPI support
  21. */
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/pci.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/acpi.h>
  30. #include <linux/sysdev.h>
  31. #include <linux/msi.h>
  32. #include <linux/htirq.h>
  33. #include <linux/dmar.h>
  34. #include <linux/jiffies.h>
  35. #ifdef CONFIG_ACPI
  36. #include <acpi/acpi_bus.h>
  37. #endif
  38. #include <linux/bootmem.h>
  39. #include <linux/dmar.h>
  40. #include <asm/idle.h>
  41. #include <asm/io.h>
  42. #include <asm/smp.h>
  43. #include <asm/desc.h>
  44. #include <asm/proto.h>
  45. #include <asm/acpi.h>
  46. #include <asm/dma.h>
  47. #include <asm/i8259.h>
  48. #include <asm/nmi.h>
  49. #include <asm/msidef.h>
  50. #include <asm/hypertransport.h>
  51. #include <asm/irq_remapping.h>
  52. #include <mach_ipi.h>
  53. #include <mach_apic.h>
  54. #define __apicdebuginit(type) static type __init
  55. struct irq_cfg {
  56. cpumask_t domain;
  57. cpumask_t old_domain;
  58. unsigned move_cleanup_count;
  59. u8 vector;
  60. u8 move_in_progress : 1;
  61. };
  62. /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
  63. static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  64. [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
  65. [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
  66. [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
  67. [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
  68. [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
  69. [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
  70. [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
  71. [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
  72. [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
  73. [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
  74. [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
  75. [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
  76. [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
  77. [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
  78. [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
  79. [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
  80. };
  81. static int assign_irq_vector(int irq, cpumask_t mask);
  82. int first_system_vector = 0xfe;
  83. char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
  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. static 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. void lock_vector_lock(void)
  664. {
  665. /* Used to the online set of cpus does not change
  666. * during assign_irq_vector.
  667. */
  668. spin_lock(&vector_lock);
  669. }
  670. void unlock_vector_lock(void)
  671. {
  672. spin_unlock(&vector_lock);
  673. }
  674. static int __assign_irq_vector(int irq, cpumask_t mask)
  675. {
  676. /*
  677. * NOTE! The local APIC isn't very good at handling
  678. * multiple interrupts at the same interrupt level.
  679. * As the interrupt level is determined by taking the
  680. * vector number and shifting that right by 4, we
  681. * want to spread these out a bit so that they don't
  682. * all fall in the same interrupt level.
  683. *
  684. * Also, we've got to be careful not to trash gate
  685. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  686. */
  687. static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
  688. unsigned int old_vector;
  689. int cpu;
  690. struct irq_cfg *cfg;
  691. BUG_ON((unsigned)irq >= NR_IRQS);
  692. cfg = &irq_cfg[irq];
  693. /* Only try and allocate irqs on cpus that are present */
  694. cpus_and(mask, mask, cpu_online_map);
  695. if ((cfg->move_in_progress) || cfg->move_cleanup_count)
  696. return -EBUSY;
  697. old_vector = cfg->vector;
  698. if (old_vector) {
  699. cpumask_t tmp;
  700. cpus_and(tmp, cfg->domain, mask);
  701. if (!cpus_empty(tmp))
  702. return 0;
  703. }
  704. for_each_cpu_mask_nr(cpu, mask) {
  705. cpumask_t domain, new_mask;
  706. int new_cpu;
  707. int vector, offset;
  708. domain = vector_allocation_domain(cpu);
  709. cpus_and(new_mask, domain, cpu_online_map);
  710. vector = current_vector;
  711. offset = current_offset;
  712. next:
  713. vector += 8;
  714. if (vector >= first_system_vector) {
  715. /* If we run out of vectors on large boxen, must share them. */
  716. offset = (offset + 1) % 8;
  717. vector = FIRST_DEVICE_VECTOR + offset;
  718. }
  719. if (unlikely(current_vector == vector))
  720. continue;
  721. if (vector == IA32_SYSCALL_VECTOR)
  722. goto next;
  723. for_each_cpu_mask_nr(new_cpu, new_mask)
  724. if (per_cpu(vector_irq, new_cpu)[vector] != -1)
  725. goto next;
  726. /* Found one! */
  727. current_vector = vector;
  728. current_offset = offset;
  729. if (old_vector) {
  730. cfg->move_in_progress = 1;
  731. cfg->old_domain = cfg->domain;
  732. }
  733. for_each_cpu_mask_nr(new_cpu, new_mask)
  734. per_cpu(vector_irq, new_cpu)[vector] = irq;
  735. cfg->vector = vector;
  736. cfg->domain = domain;
  737. return 0;
  738. }
  739. return -ENOSPC;
  740. }
  741. static int assign_irq_vector(int irq, cpumask_t mask)
  742. {
  743. int err;
  744. unsigned long flags;
  745. spin_lock_irqsave(&vector_lock, flags);
  746. err = __assign_irq_vector(irq, mask);
  747. spin_unlock_irqrestore(&vector_lock, flags);
  748. return err;
  749. }
  750. static void __clear_irq_vector(int irq)
  751. {
  752. struct irq_cfg *cfg;
  753. cpumask_t mask;
  754. int cpu, vector;
  755. BUG_ON((unsigned)irq >= NR_IRQS);
  756. cfg = &irq_cfg[irq];
  757. BUG_ON(!cfg->vector);
  758. vector = cfg->vector;
  759. cpus_and(mask, cfg->domain, cpu_online_map);
  760. for_each_cpu_mask_nr(cpu, mask)
  761. per_cpu(vector_irq, cpu)[vector] = -1;
  762. cfg->vector = 0;
  763. cpus_clear(cfg->domain);
  764. }
  765. void __setup_vector_irq(int cpu)
  766. {
  767. /* Initialize vector_irq on a new cpu */
  768. /* This function must be called with vector_lock held */
  769. int irq, vector;
  770. /* Mark the inuse vectors */
  771. for (irq = 0; irq < NR_IRQS; ++irq) {
  772. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  773. continue;
  774. vector = irq_cfg[irq].vector;
  775. per_cpu(vector_irq, cpu)[vector] = irq;
  776. }
  777. /* Mark the free vectors */
  778. for (vector = 0; vector < NR_VECTORS; ++vector) {
  779. irq = per_cpu(vector_irq, cpu)[vector];
  780. if (irq < 0)
  781. continue;
  782. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  783. per_cpu(vector_irq, cpu)[vector] = -1;
  784. }
  785. }
  786. static struct irq_chip ioapic_chip;
  787. #ifdef CONFIG_INTR_REMAP
  788. static struct irq_chip ir_ioapic_chip;
  789. #endif
  790. static void ioapic_register_intr(int irq, unsigned long trigger)
  791. {
  792. if (trigger)
  793. irq_desc[irq].status |= IRQ_LEVEL;
  794. else
  795. irq_desc[irq].status &= ~IRQ_LEVEL;
  796. #ifdef CONFIG_INTR_REMAP
  797. if (irq_remapped(irq)) {
  798. irq_desc[irq].status |= IRQ_MOVE_PCNTXT;
  799. if (trigger)
  800. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  801. handle_fasteoi_irq,
  802. "fasteoi");
  803. else
  804. set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
  805. handle_edge_irq, "edge");
  806. return;
  807. }
  808. #endif
  809. if (trigger)
  810. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  811. handle_fasteoi_irq,
  812. "fasteoi");
  813. else
  814. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  815. handle_edge_irq, "edge");
  816. }
  817. static int setup_ioapic_entry(int apic, int irq,
  818. struct IO_APIC_route_entry *entry,
  819. unsigned int destination, int trigger,
  820. int polarity, int vector)
  821. {
  822. /*
  823. * add it to the IO-APIC irq-routing table:
  824. */
  825. memset(entry,0,sizeof(*entry));
  826. #ifdef CONFIG_INTR_REMAP
  827. if (intr_remapping_enabled) {
  828. struct intel_iommu *iommu = map_ioapic_to_ir(apic);
  829. struct irte irte;
  830. struct IR_IO_APIC_route_entry *ir_entry =
  831. (struct IR_IO_APIC_route_entry *) entry;
  832. int index;
  833. if (!iommu)
  834. panic("No mapping iommu for ioapic %d\n", apic);
  835. index = alloc_irte(iommu, irq, 1);
  836. if (index < 0)
  837. panic("Failed to allocate IRTE for ioapic %d\n", apic);
  838. memset(&irte, 0, sizeof(irte));
  839. irte.present = 1;
  840. irte.dst_mode = INT_DEST_MODE;
  841. irte.trigger_mode = trigger;
  842. irte.dlvry_mode = INT_DELIVERY_MODE;
  843. irte.vector = vector;
  844. irte.dest_id = IRTE_DEST(destination);
  845. modify_irte(irq, &irte);
  846. ir_entry->index2 = (index >> 15) & 0x1;
  847. ir_entry->zero = 0;
  848. ir_entry->format = 1;
  849. ir_entry->index = (index & 0x7fff);
  850. } else
  851. #endif
  852. {
  853. entry->delivery_mode = INT_DELIVERY_MODE;
  854. entry->dest_mode = INT_DEST_MODE;
  855. entry->dest = destination;
  856. }
  857. entry->mask = 0; /* enable IRQ */
  858. entry->trigger = trigger;
  859. entry->polarity = polarity;
  860. entry->vector = vector;
  861. /* Mask level triggered irqs.
  862. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  863. */
  864. if (trigger)
  865. entry->mask = 1;
  866. return 0;
  867. }
  868. static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
  869. int trigger, int polarity)
  870. {
  871. struct irq_cfg *cfg = irq_cfg + irq;
  872. struct IO_APIC_route_entry entry;
  873. cpumask_t mask;
  874. if (!IO_APIC_IRQ(irq))
  875. return;
  876. mask = TARGET_CPUS;
  877. if (assign_irq_vector(irq, mask))
  878. return;
  879. cpus_and(mask, cfg->domain, mask);
  880. apic_printk(APIC_VERBOSE,KERN_DEBUG
  881. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  882. "IRQ %d Mode:%i Active:%i)\n",
  883. apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
  884. irq, trigger, polarity);
  885. if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
  886. cpu_mask_to_apicid(mask), trigger, polarity,
  887. cfg->vector)) {
  888. printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  889. mp_ioapics[apic].mp_apicid, pin);
  890. __clear_irq_vector(irq);
  891. return;
  892. }
  893. ioapic_register_intr(irq, trigger);
  894. if (irq < 16)
  895. disable_8259A_irq(irq);
  896. ioapic_write_entry(apic, pin, entry);
  897. }
  898. static void __init setup_IO_APIC_irqs(void)
  899. {
  900. int apic, pin, idx, irq, first_notcon = 1;
  901. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  902. for (apic = 0; apic < nr_ioapics; apic++) {
  903. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  904. idx = find_irq_entry(apic,pin,mp_INT);
  905. if (idx == -1) {
  906. if (first_notcon) {
  907. apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
  908. first_notcon = 0;
  909. } else
  910. apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
  911. continue;
  912. }
  913. if (!first_notcon) {
  914. apic_printk(APIC_VERBOSE, " not connected.\n");
  915. first_notcon = 1;
  916. }
  917. irq = pin_2_irq(idx, apic, pin);
  918. add_pin_to_irq(irq, apic, pin);
  919. setup_IO_APIC_irq(apic, pin, irq,
  920. irq_trigger(idx), irq_polarity(idx));
  921. }
  922. }
  923. if (!first_notcon)
  924. apic_printk(APIC_VERBOSE, " not connected.\n");
  925. }
  926. /*
  927. * Set up the timer pin, possibly with the 8259A-master behind.
  928. */
  929. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  930. int vector)
  931. {
  932. struct IO_APIC_route_entry entry;
  933. if (intr_remapping_enabled)
  934. return;
  935. memset(&entry, 0, sizeof(entry));
  936. /*
  937. * We use logical delivery to get the timer IRQ
  938. * to the first CPU.
  939. */
  940. entry.dest_mode = INT_DEST_MODE;
  941. entry.mask = 1; /* mask IRQ now */
  942. entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
  943. entry.delivery_mode = INT_DELIVERY_MODE;
  944. entry.polarity = 0;
  945. entry.trigger = 0;
  946. entry.vector = vector;
  947. /*
  948. * The timer IRQ doesn't have to know that behind the
  949. * scene we may have a 8259A-master in AEOI mode ...
  950. */
  951. set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
  952. /*
  953. * Add it to the IO-APIC irq-routing table:
  954. */
  955. ioapic_write_entry(apic, pin, entry);
  956. }
  957. __apicdebuginit(void) print_IO_APIC(void)
  958. {
  959. int apic, i;
  960. union IO_APIC_reg_00 reg_00;
  961. union IO_APIC_reg_01 reg_01;
  962. union IO_APIC_reg_02 reg_02;
  963. unsigned long flags;
  964. if (apic_verbosity == APIC_QUIET)
  965. return;
  966. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  967. for (i = 0; i < nr_ioapics; i++)
  968. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  969. mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
  970. /*
  971. * We are a bit conservative about what we expect. We have to
  972. * know about every hardware change ASAP.
  973. */
  974. printk(KERN_INFO "testing the IO APIC.......................\n");
  975. for (apic = 0; apic < nr_ioapics; apic++) {
  976. spin_lock_irqsave(&ioapic_lock, flags);
  977. reg_00.raw = io_apic_read(apic, 0);
  978. reg_01.raw = io_apic_read(apic, 1);
  979. if (reg_01.bits.version >= 0x10)
  980. reg_02.raw = io_apic_read(apic, 2);
  981. spin_unlock_irqrestore(&ioapic_lock, flags);
  982. printk("\n");
  983. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
  984. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  985. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  986. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  987. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  988. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  989. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  990. if (reg_01.bits.version >= 0x10) {
  991. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  992. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  993. }
  994. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  995. printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
  996. " Stat Dmod Deli Vect: \n");
  997. for (i = 0; i <= reg_01.bits.entries; i++) {
  998. struct IO_APIC_route_entry entry;
  999. entry = ioapic_read_entry(apic, i);
  1000. printk(KERN_DEBUG " %02x %03X ",
  1001. i,
  1002. entry.dest
  1003. );
  1004. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1005. entry.mask,
  1006. entry.trigger,
  1007. entry.irr,
  1008. entry.polarity,
  1009. entry.delivery_status,
  1010. entry.dest_mode,
  1011. entry.delivery_mode,
  1012. entry.vector
  1013. );
  1014. }
  1015. }
  1016. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1017. for (i = 0; i < NR_IRQS; i++) {
  1018. struct irq_pin_list *entry = irq_2_pin + i;
  1019. if (entry->pin < 0)
  1020. continue;
  1021. printk(KERN_DEBUG "IRQ%d ", i);
  1022. for (;;) {
  1023. printk("-> %d:%d", entry->apic, entry->pin);
  1024. if (!entry->next)
  1025. break;
  1026. entry = irq_2_pin + entry->next;
  1027. }
  1028. printk("\n");
  1029. }
  1030. printk(KERN_INFO ".................................... done.\n");
  1031. return;
  1032. }
  1033. __apicdebuginit(void) print_APIC_bitfield(int base)
  1034. {
  1035. unsigned int v;
  1036. int i, j;
  1037. if (apic_verbosity == APIC_QUIET)
  1038. return;
  1039. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1040. for (i = 0; i < 8; i++) {
  1041. v = apic_read(base + i*0x10);
  1042. for (j = 0; j < 32; j++) {
  1043. if (v & (1<<j))
  1044. printk("1");
  1045. else
  1046. printk("0");
  1047. }
  1048. printk("\n");
  1049. }
  1050. }
  1051. __apicdebuginit(void) print_local_APIC(void *dummy)
  1052. {
  1053. unsigned int v, ver, maxlvt;
  1054. unsigned long icr;
  1055. if (apic_verbosity == APIC_QUIET)
  1056. return;
  1057. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1058. smp_processor_id(), hard_smp_processor_id());
  1059. v = apic_read(APIC_ID);
  1060. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1061. v = apic_read(APIC_LVR);
  1062. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1063. ver = GET_APIC_VERSION(v);
  1064. maxlvt = lapic_get_maxlvt();
  1065. v = apic_read(APIC_TASKPRI);
  1066. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1067. v = apic_read(APIC_ARBPRI);
  1068. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1069. v & APIC_ARBPRI_MASK);
  1070. v = apic_read(APIC_PROCPRI);
  1071. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1072. v = apic_read(APIC_EOI);
  1073. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1074. v = apic_read(APIC_RRR);
  1075. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1076. v = apic_read(APIC_LDR);
  1077. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1078. v = apic_read(APIC_DFR);
  1079. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1080. v = apic_read(APIC_SPIV);
  1081. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1082. printk(KERN_DEBUG "... APIC ISR field:\n");
  1083. print_APIC_bitfield(APIC_ISR);
  1084. printk(KERN_DEBUG "... APIC TMR field:\n");
  1085. print_APIC_bitfield(APIC_TMR);
  1086. printk(KERN_DEBUG "... APIC IRR field:\n");
  1087. print_APIC_bitfield(APIC_IRR);
  1088. v = apic_read(APIC_ESR);
  1089. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1090. icr = apic_icr_read();
  1091. printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
  1092. printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1093. v = apic_read(APIC_LVTT);
  1094. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1095. if (maxlvt > 3) { /* PC is LVT#4. */
  1096. v = apic_read(APIC_LVTPC);
  1097. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1098. }
  1099. v = apic_read(APIC_LVT0);
  1100. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1101. v = apic_read(APIC_LVT1);
  1102. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1103. if (maxlvt > 2) { /* ERR is LVT#3. */
  1104. v = apic_read(APIC_LVTERR);
  1105. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1106. }
  1107. v = apic_read(APIC_TMICT);
  1108. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1109. v = apic_read(APIC_TMCCT);
  1110. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1111. v = apic_read(APIC_TDCR);
  1112. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1113. printk("\n");
  1114. }
  1115. __apicdebuginit(void) print_all_local_APICs(void)
  1116. {
  1117. on_each_cpu(print_local_APIC, NULL, 1);
  1118. }
  1119. __apicdebuginit(void) print_PIC(void)
  1120. {
  1121. unsigned int v;
  1122. unsigned long flags;
  1123. if (apic_verbosity == APIC_QUIET)
  1124. return;
  1125. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1126. spin_lock_irqsave(&i8259A_lock, flags);
  1127. v = inb(0xa1) << 8 | inb(0x21);
  1128. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1129. v = inb(0xa0) << 8 | inb(0x20);
  1130. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1131. outb(0x0b,0xa0);
  1132. outb(0x0b,0x20);
  1133. v = inb(0xa0) << 8 | inb(0x20);
  1134. outb(0x0a,0xa0);
  1135. outb(0x0a,0x20);
  1136. spin_unlock_irqrestore(&i8259A_lock, flags);
  1137. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1138. v = inb(0x4d1) << 8 | inb(0x4d0);
  1139. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1140. }
  1141. __apicdebuginit(int) print_all_ICs(void)
  1142. {
  1143. print_PIC();
  1144. print_all_local_APICs();
  1145. print_IO_APIC();
  1146. return 0;
  1147. }
  1148. fs_initcall(print_all_ICs);
  1149. void __init enable_IO_APIC(void)
  1150. {
  1151. union IO_APIC_reg_01 reg_01;
  1152. int i8259_apic, i8259_pin;
  1153. int i, apic;
  1154. unsigned long flags;
  1155. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1156. irq_2_pin[i].pin = -1;
  1157. irq_2_pin[i].next = 0;
  1158. }
  1159. /*
  1160. * The number of IO-APIC IRQ registers (== #pins):
  1161. */
  1162. for (apic = 0; apic < nr_ioapics; apic++) {
  1163. spin_lock_irqsave(&ioapic_lock, flags);
  1164. reg_01.raw = io_apic_read(apic, 1);
  1165. spin_unlock_irqrestore(&ioapic_lock, flags);
  1166. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1167. }
  1168. for(apic = 0; apic < nr_ioapics; apic++) {
  1169. int pin;
  1170. /* See if any of the pins is in ExtINT mode */
  1171. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1172. struct IO_APIC_route_entry entry;
  1173. entry = ioapic_read_entry(apic, pin);
  1174. /* If the interrupt line is enabled and in ExtInt mode
  1175. * I have found the pin where the i8259 is connected.
  1176. */
  1177. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1178. ioapic_i8259.apic = apic;
  1179. ioapic_i8259.pin = pin;
  1180. goto found_i8259;
  1181. }
  1182. }
  1183. }
  1184. found_i8259:
  1185. /* Look to see what if the MP table has reported the ExtINT */
  1186. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1187. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1188. /* Trust the MP table if nothing is setup in the hardware */
  1189. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1190. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1191. ioapic_i8259.pin = i8259_pin;
  1192. ioapic_i8259.apic = i8259_apic;
  1193. }
  1194. /* Complain if the MP table and the hardware disagree */
  1195. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1196. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1197. {
  1198. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1199. }
  1200. /*
  1201. * Do not trust the IO-APIC being empty at bootup
  1202. */
  1203. clear_IO_APIC();
  1204. }
  1205. /*
  1206. * Not an __init, needed by the reboot code
  1207. */
  1208. void disable_IO_APIC(void)
  1209. {
  1210. /*
  1211. * Clear the IO-APIC before rebooting:
  1212. */
  1213. clear_IO_APIC();
  1214. /*
  1215. * If the i8259 is routed through an IOAPIC
  1216. * Put that IOAPIC in virtual wire mode
  1217. * so legacy interrupts can be delivered.
  1218. */
  1219. if (ioapic_i8259.pin != -1) {
  1220. struct IO_APIC_route_entry entry;
  1221. memset(&entry, 0, sizeof(entry));
  1222. entry.mask = 0; /* Enabled */
  1223. entry.trigger = 0; /* Edge */
  1224. entry.irr = 0;
  1225. entry.polarity = 0; /* High */
  1226. entry.delivery_status = 0;
  1227. entry.dest_mode = 0; /* Physical */
  1228. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1229. entry.vector = 0;
  1230. entry.dest = read_apic_id();
  1231. /*
  1232. * Add it to the IO-APIC irq-routing table:
  1233. */
  1234. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1235. }
  1236. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1237. }
  1238. /*
  1239. * There is a nasty bug in some older SMP boards, their mptable lies
  1240. * about the timer IRQ. We do the following to work around the situation:
  1241. *
  1242. * - timer IRQ defaults to IO-APIC IRQ
  1243. * - if this function detects that timer IRQs are defunct, then we fall
  1244. * back to ISA timer IRQs
  1245. */
  1246. static int __init timer_irq_works(void)
  1247. {
  1248. unsigned long t1 = jiffies;
  1249. unsigned long flags;
  1250. local_save_flags(flags);
  1251. local_irq_enable();
  1252. /* Let ten ticks pass... */
  1253. mdelay((10 * 1000) / HZ);
  1254. local_irq_restore(flags);
  1255. /*
  1256. * Expect a few ticks at least, to be sure some possible
  1257. * glue logic does not lock up after one or two first
  1258. * ticks in a non-ExtINT mode. Also the local APIC
  1259. * might have cached one ExtINT interrupt. Finally, at
  1260. * least one tick may be lost due to delays.
  1261. */
  1262. /* jiffies wrap? */
  1263. if (time_after(jiffies, t1 + 4))
  1264. return 1;
  1265. return 0;
  1266. }
  1267. /*
  1268. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1269. * number of pending IRQ events unhandled. These cases are very rare,
  1270. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1271. * better to do it this way as thus we do not have to be aware of
  1272. * 'pending' interrupts in the IRQ path, except at this point.
  1273. */
  1274. /*
  1275. * Edge triggered needs to resend any interrupt
  1276. * that was delayed but this is now handled in the device
  1277. * independent code.
  1278. */
  1279. /*
  1280. * Starting up a edge-triggered IO-APIC interrupt is
  1281. * nasty - we need to make sure that we get the edge.
  1282. * If it is already asserted for some reason, we need
  1283. * return 1 to indicate that is was pending.
  1284. *
  1285. * This is not complete - we should be able to fake
  1286. * an edge even if it isn't on the 8259A...
  1287. */
  1288. static unsigned int startup_ioapic_irq(unsigned int irq)
  1289. {
  1290. int was_pending = 0;
  1291. unsigned long flags;
  1292. spin_lock_irqsave(&ioapic_lock, flags);
  1293. if (irq < 16) {
  1294. disable_8259A_irq(irq);
  1295. if (i8259A_irq_pending(irq))
  1296. was_pending = 1;
  1297. }
  1298. __unmask_IO_APIC_irq(irq);
  1299. spin_unlock_irqrestore(&ioapic_lock, flags);
  1300. return was_pending;
  1301. }
  1302. static int ioapic_retrigger_irq(unsigned int irq)
  1303. {
  1304. struct irq_cfg *cfg = &irq_cfg[irq];
  1305. unsigned long flags;
  1306. spin_lock_irqsave(&vector_lock, flags);
  1307. send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
  1308. spin_unlock_irqrestore(&vector_lock, flags);
  1309. return 1;
  1310. }
  1311. /*
  1312. * Level and edge triggered IO-APIC interrupts need different handling,
  1313. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1314. * handled with the level-triggered descriptor, but that one has slightly
  1315. * more overhead. Level-triggered interrupts cannot be handled with the
  1316. * edge-triggered handler, without risking IRQ storms and other ugly
  1317. * races.
  1318. */
  1319. #ifdef CONFIG_SMP
  1320. #ifdef CONFIG_INTR_REMAP
  1321. static void ir_irq_migration(struct work_struct *work);
  1322. static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
  1323. /*
  1324. * Migrate the IO-APIC irq in the presence of intr-remapping.
  1325. *
  1326. * For edge triggered, irq migration is a simple atomic update(of vector
  1327. * and cpu destination) of IRTE and flush the hardware cache.
  1328. *
  1329. * For level triggered, we need to modify the io-apic RTE aswell with the update
  1330. * vector information, along with modifying IRTE with vector and destination.
  1331. * So irq migration for level triggered is little bit more complex compared to
  1332. * edge triggered migration. But the good news is, we use the same algorithm
  1333. * for level triggered migration as we have today, only difference being,
  1334. * we now initiate the irq migration from process context instead of the
  1335. * interrupt context.
  1336. *
  1337. * In future, when we do a directed EOI (combined with cpu EOI broadcast
  1338. * suppression) to the IO-APIC, level triggered irq migration will also be
  1339. * as simple as edge triggered migration and we can do the irq migration
  1340. * with a simple atomic update to IO-APIC RTE.
  1341. */
  1342. static void migrate_ioapic_irq(int irq, cpumask_t mask)
  1343. {
  1344. struct irq_cfg *cfg = irq_cfg + irq;
  1345. struct irq_desc *desc = irq_desc + irq;
  1346. cpumask_t tmp, cleanup_mask;
  1347. struct irte irte;
  1348. int modify_ioapic_rte = desc->status & IRQ_LEVEL;
  1349. unsigned int dest;
  1350. unsigned long flags;
  1351. cpus_and(tmp, mask, cpu_online_map);
  1352. if (cpus_empty(tmp))
  1353. return;
  1354. if (get_irte(irq, &irte))
  1355. return;
  1356. if (assign_irq_vector(irq, mask))
  1357. return;
  1358. cpus_and(tmp, cfg->domain, mask);
  1359. dest = cpu_mask_to_apicid(tmp);
  1360. if (modify_ioapic_rte) {
  1361. spin_lock_irqsave(&ioapic_lock, flags);
  1362. __target_IO_APIC_irq(irq, dest, cfg->vector);
  1363. spin_unlock_irqrestore(&ioapic_lock, flags);
  1364. }
  1365. irte.vector = cfg->vector;
  1366. irte.dest_id = IRTE_DEST(dest);
  1367. /*
  1368. * Modified the IRTE and flushes the Interrupt entry cache.
  1369. */
  1370. modify_irte(irq, &irte);
  1371. if (cfg->move_in_progress) {
  1372. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1373. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1374. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1375. cfg->move_in_progress = 0;
  1376. }
  1377. irq_desc[irq].affinity = mask;
  1378. }
  1379. static int migrate_irq_remapped_level(int irq)
  1380. {
  1381. int ret = -1;
  1382. mask_IO_APIC_irq(irq);
  1383. if (io_apic_level_ack_pending(irq)) {
  1384. /*
  1385. * Interrupt in progress. Migrating irq now will change the
  1386. * vector information in the IO-APIC RTE and that will confuse
  1387. * the EOI broadcast performed by cpu.
  1388. * So, delay the irq migration to the next instance.
  1389. */
  1390. schedule_delayed_work(&ir_migration_work, 1);
  1391. goto unmask;
  1392. }
  1393. /* everthing is clear. we have right of way */
  1394. migrate_ioapic_irq(irq, irq_desc[irq].pending_mask);
  1395. ret = 0;
  1396. irq_desc[irq].status &= ~IRQ_MOVE_PENDING;
  1397. cpus_clear(irq_desc[irq].pending_mask);
  1398. unmask:
  1399. unmask_IO_APIC_irq(irq);
  1400. return ret;
  1401. }
  1402. static void ir_irq_migration(struct work_struct *work)
  1403. {
  1404. int irq;
  1405. for (irq = 0; irq < NR_IRQS; irq++) {
  1406. struct irq_desc *desc = irq_desc + irq;
  1407. if (desc->status & IRQ_MOVE_PENDING) {
  1408. unsigned long flags;
  1409. spin_lock_irqsave(&desc->lock, flags);
  1410. if (!desc->chip->set_affinity ||
  1411. !(desc->status & IRQ_MOVE_PENDING)) {
  1412. desc->status &= ~IRQ_MOVE_PENDING;
  1413. spin_unlock_irqrestore(&desc->lock, flags);
  1414. continue;
  1415. }
  1416. desc->chip->set_affinity(irq,
  1417. irq_desc[irq].pending_mask);
  1418. spin_unlock_irqrestore(&desc->lock, flags);
  1419. }
  1420. }
  1421. }
  1422. /*
  1423. * Migrates the IRQ destination in the process context.
  1424. */
  1425. static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
  1426. {
  1427. if (irq_desc[irq].status & IRQ_LEVEL) {
  1428. irq_desc[irq].status |= IRQ_MOVE_PENDING;
  1429. irq_desc[irq].pending_mask = mask;
  1430. migrate_irq_remapped_level(irq);
  1431. return;
  1432. }
  1433. migrate_ioapic_irq(irq, mask);
  1434. }
  1435. #endif
  1436. asmlinkage void smp_irq_move_cleanup_interrupt(void)
  1437. {
  1438. unsigned vector, me;
  1439. ack_APIC_irq();
  1440. exit_idle();
  1441. irq_enter();
  1442. me = smp_processor_id();
  1443. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  1444. unsigned int irq;
  1445. struct irq_desc *desc;
  1446. struct irq_cfg *cfg;
  1447. irq = __get_cpu_var(vector_irq)[vector];
  1448. if (irq >= NR_IRQS)
  1449. continue;
  1450. desc = irq_desc + irq;
  1451. cfg = irq_cfg + irq;
  1452. spin_lock(&desc->lock);
  1453. if (!cfg->move_cleanup_count)
  1454. goto unlock;
  1455. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
  1456. goto unlock;
  1457. __get_cpu_var(vector_irq)[vector] = -1;
  1458. cfg->move_cleanup_count--;
  1459. unlock:
  1460. spin_unlock(&desc->lock);
  1461. }
  1462. irq_exit();
  1463. }
  1464. static void irq_complete_move(unsigned int irq)
  1465. {
  1466. struct irq_cfg *cfg = irq_cfg + irq;
  1467. unsigned vector, me;
  1468. if (likely(!cfg->move_in_progress))
  1469. return;
  1470. vector = ~get_irq_regs()->orig_ax;
  1471. me = smp_processor_id();
  1472. if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
  1473. cpumask_t cleanup_mask;
  1474. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  1475. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  1476. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  1477. cfg->move_in_progress = 0;
  1478. }
  1479. }
  1480. #else
  1481. static inline void irq_complete_move(unsigned int irq) {}
  1482. #endif
  1483. #ifdef CONFIG_INTR_REMAP
  1484. static void ack_x2apic_level(unsigned int irq)
  1485. {
  1486. ack_x2APIC_irq();
  1487. }
  1488. static void ack_x2apic_edge(unsigned int irq)
  1489. {
  1490. ack_x2APIC_irq();
  1491. }
  1492. #endif
  1493. static void ack_apic_edge(unsigned int irq)
  1494. {
  1495. irq_complete_move(irq);
  1496. move_native_irq(irq);
  1497. ack_APIC_irq();
  1498. }
  1499. static void ack_apic_level(unsigned int irq)
  1500. {
  1501. int do_unmask_irq = 0;
  1502. irq_complete_move(irq);
  1503. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1504. /* If we are moving the irq we need to mask it */
  1505. if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
  1506. do_unmask_irq = 1;
  1507. mask_IO_APIC_irq(irq);
  1508. }
  1509. #endif
  1510. /*
  1511. * We must acknowledge the irq before we move it or the acknowledge will
  1512. * not propagate properly.
  1513. */
  1514. ack_APIC_irq();
  1515. /* Now we can move and renable the irq */
  1516. if (unlikely(do_unmask_irq)) {
  1517. /* Only migrate the irq if the ack has been received.
  1518. *
  1519. * On rare occasions the broadcast level triggered ack gets
  1520. * delayed going to ioapics, and if we reprogram the
  1521. * vector while Remote IRR is still set the irq will never
  1522. * fire again.
  1523. *
  1524. * To prevent this scenario we read the Remote IRR bit
  1525. * of the ioapic. This has two effects.
  1526. * - On any sane system the read of the ioapic will
  1527. * flush writes (and acks) going to the ioapic from
  1528. * this cpu.
  1529. * - We get to see if the ACK has actually been delivered.
  1530. *
  1531. * Based on failed experiments of reprogramming the
  1532. * ioapic entry from outside of irq context starting
  1533. * with masking the ioapic entry and then polling until
  1534. * Remote IRR was clear before reprogramming the
  1535. * ioapic I don't trust the Remote IRR bit to be
  1536. * completey accurate.
  1537. *
  1538. * However there appears to be no other way to plug
  1539. * this race, so if the Remote IRR bit is not
  1540. * accurate and is causing problems then it is a hardware bug
  1541. * and you can go talk to the chipset vendor about it.
  1542. */
  1543. if (!io_apic_level_ack_pending(irq))
  1544. move_masked_irq(irq);
  1545. unmask_IO_APIC_irq(irq);
  1546. }
  1547. }
  1548. static struct irq_chip ioapic_chip __read_mostly = {
  1549. .name = "IO-APIC",
  1550. .startup = startup_ioapic_irq,
  1551. .mask = mask_IO_APIC_irq,
  1552. .unmask = unmask_IO_APIC_irq,
  1553. .ack = ack_apic_edge,
  1554. .eoi = ack_apic_level,
  1555. #ifdef CONFIG_SMP
  1556. .set_affinity = set_ioapic_affinity_irq,
  1557. #endif
  1558. .retrigger = ioapic_retrigger_irq,
  1559. };
  1560. #ifdef CONFIG_INTR_REMAP
  1561. static struct irq_chip ir_ioapic_chip __read_mostly = {
  1562. .name = "IR-IO-APIC",
  1563. .startup = startup_ioapic_irq,
  1564. .mask = mask_IO_APIC_irq,
  1565. .unmask = unmask_IO_APIC_irq,
  1566. .ack = ack_x2apic_edge,
  1567. .eoi = ack_x2apic_level,
  1568. #ifdef CONFIG_SMP
  1569. .set_affinity = set_ir_ioapic_affinity_irq,
  1570. #endif
  1571. .retrigger = ioapic_retrigger_irq,
  1572. };
  1573. #endif
  1574. static inline void init_IO_APIC_traps(void)
  1575. {
  1576. int irq;
  1577. /*
  1578. * NOTE! The local APIC isn't very good at handling
  1579. * multiple interrupts at the same interrupt level.
  1580. * As the interrupt level is determined by taking the
  1581. * vector number and shifting that right by 4, we
  1582. * want to spread these out a bit so that they don't
  1583. * all fall in the same interrupt level.
  1584. *
  1585. * Also, we've got to be careful not to trash gate
  1586. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1587. */
  1588. for (irq = 0; irq < NR_IRQS ; irq++) {
  1589. if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
  1590. /*
  1591. * Hmm.. We don't have an entry for this,
  1592. * so default to an old-fashioned 8259
  1593. * interrupt if we can..
  1594. */
  1595. if (irq < 16)
  1596. make_8259A_irq(irq);
  1597. else
  1598. /* Strange. Oh, well.. */
  1599. irq_desc[irq].chip = &no_irq_chip;
  1600. }
  1601. }
  1602. }
  1603. static void unmask_lapic_irq(unsigned int irq)
  1604. {
  1605. unsigned long v;
  1606. v = apic_read(APIC_LVT0);
  1607. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1608. }
  1609. static void mask_lapic_irq(unsigned int irq)
  1610. {
  1611. unsigned long v;
  1612. v = apic_read(APIC_LVT0);
  1613. apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
  1614. }
  1615. static void ack_lapic_irq (unsigned int irq)
  1616. {
  1617. ack_APIC_irq();
  1618. }
  1619. static struct irq_chip lapic_chip __read_mostly = {
  1620. .name = "local-APIC",
  1621. .mask = mask_lapic_irq,
  1622. .unmask = unmask_lapic_irq,
  1623. .ack = ack_lapic_irq,
  1624. };
  1625. static void lapic_register_intr(int irq)
  1626. {
  1627. irq_desc[irq].status &= ~IRQ_LEVEL;
  1628. set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1629. "edge");
  1630. }
  1631. static void __init setup_nmi(void)
  1632. {
  1633. /*
  1634. * Dirty trick to enable the NMI watchdog ...
  1635. * We put the 8259A master into AEOI mode and
  1636. * unmask on all local APICs LVT0 as NMI.
  1637. *
  1638. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1639. * is from Maciej W. Rozycki - so we do not have to EOI from
  1640. * the NMI handler or the timer interrupt.
  1641. */
  1642. printk(KERN_INFO "activating NMI Watchdog ...");
  1643. enable_NMI_through_LVT0();
  1644. printk(" done.\n");
  1645. }
  1646. /*
  1647. * This looks a bit hackish but it's about the only one way of sending
  1648. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1649. * not support the ExtINT mode, unfortunately. We need to send these
  1650. * cycles as some i82489DX-based boards have glue logic that keeps the
  1651. * 8259A interrupt line asserted until INTA. --macro
  1652. */
  1653. static inline void __init unlock_ExtINT_logic(void)
  1654. {
  1655. int apic, pin, i;
  1656. struct IO_APIC_route_entry entry0, entry1;
  1657. unsigned char save_control, save_freq_select;
  1658. pin = find_isa_irq_pin(8, mp_INT);
  1659. apic = find_isa_irq_apic(8, mp_INT);
  1660. if (pin == -1)
  1661. return;
  1662. entry0 = ioapic_read_entry(apic, pin);
  1663. clear_IO_APIC_pin(apic, pin);
  1664. memset(&entry1, 0, sizeof(entry1));
  1665. entry1.dest_mode = 0; /* physical delivery */
  1666. entry1.mask = 0; /* unmask IRQ now */
  1667. entry1.dest = hard_smp_processor_id();
  1668. entry1.delivery_mode = dest_ExtINT;
  1669. entry1.polarity = entry0.polarity;
  1670. entry1.trigger = 0;
  1671. entry1.vector = 0;
  1672. ioapic_write_entry(apic, pin, entry1);
  1673. save_control = CMOS_READ(RTC_CONTROL);
  1674. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1675. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1676. RTC_FREQ_SELECT);
  1677. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1678. i = 100;
  1679. while (i-- > 0) {
  1680. mdelay(10);
  1681. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1682. i -= 10;
  1683. }
  1684. CMOS_WRITE(save_control, RTC_CONTROL);
  1685. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1686. clear_IO_APIC_pin(apic, pin);
  1687. ioapic_write_entry(apic, pin, entry0);
  1688. }
  1689. /*
  1690. * This code may look a bit paranoid, but it's supposed to cooperate with
  1691. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1692. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1693. * fanatically on his truly buggy board.
  1694. *
  1695. * FIXME: really need to revamp this for modern platforms only.
  1696. */
  1697. static inline void __init check_timer(void)
  1698. {
  1699. struct irq_cfg *cfg = irq_cfg + 0;
  1700. int apic1, pin1, apic2, pin2;
  1701. unsigned long flags;
  1702. int no_pin1 = 0;
  1703. local_irq_save(flags);
  1704. /*
  1705. * get/set the timer IRQ vector:
  1706. */
  1707. disable_8259A_irq(0);
  1708. assign_irq_vector(0, TARGET_CPUS);
  1709. /*
  1710. * As IRQ0 is to be enabled in the 8259A, the virtual
  1711. * wire has to be disabled in the local APIC.
  1712. */
  1713. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1714. init_8259A(1);
  1715. pin1 = find_isa_irq_pin(0, mp_INT);
  1716. apic1 = find_isa_irq_apic(0, mp_INT);
  1717. pin2 = ioapic_i8259.pin;
  1718. apic2 = ioapic_i8259.apic;
  1719. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1720. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1721. cfg->vector, apic1, pin1, apic2, pin2);
  1722. /*
  1723. * Some BIOS writers are clueless and report the ExtINTA
  1724. * I/O APIC input from the cascaded 8259A as the timer
  1725. * interrupt input. So just in case, if only one pin
  1726. * was found above, try it both directly and through the
  1727. * 8259A.
  1728. */
  1729. if (pin1 == -1) {
  1730. if (intr_remapping_enabled)
  1731. panic("BIOS bug: timer not connected to IO-APIC");
  1732. pin1 = pin2;
  1733. apic1 = apic2;
  1734. no_pin1 = 1;
  1735. } else if (pin2 == -1) {
  1736. pin2 = pin1;
  1737. apic2 = apic1;
  1738. }
  1739. if (pin1 != -1) {
  1740. /*
  1741. * Ok, does IRQ0 through the IOAPIC work?
  1742. */
  1743. if (no_pin1) {
  1744. add_pin_to_irq(0, apic1, pin1);
  1745. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  1746. }
  1747. unmask_IO_APIC_irq(0);
  1748. if (!no_timer_check && timer_irq_works()) {
  1749. if (nmi_watchdog == NMI_IO_APIC) {
  1750. setup_nmi();
  1751. enable_8259A_irq(0);
  1752. }
  1753. if (disable_timer_pin_1 > 0)
  1754. clear_IO_APIC_pin(0, pin1);
  1755. goto out;
  1756. }
  1757. if (intr_remapping_enabled)
  1758. panic("timer doesn't work through Interrupt-remapped IO-APIC");
  1759. clear_IO_APIC_pin(apic1, pin1);
  1760. if (!no_pin1)
  1761. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1762. "8254 timer not connected to IO-APIC\n");
  1763. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1764. "(IRQ0) through the 8259A ...\n");
  1765. apic_printk(APIC_QUIET, KERN_INFO
  1766. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1767. /*
  1768. * legacy devices should be connected to IO APIC #0
  1769. */
  1770. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1771. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  1772. unmask_IO_APIC_irq(0);
  1773. enable_8259A_irq(0);
  1774. if (timer_irq_works()) {
  1775. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1776. timer_through_8259 = 1;
  1777. if (nmi_watchdog == NMI_IO_APIC) {
  1778. disable_8259A_irq(0);
  1779. setup_nmi();
  1780. enable_8259A_irq(0);
  1781. }
  1782. goto out;
  1783. }
  1784. /*
  1785. * Cleanup, just in case ...
  1786. */
  1787. disable_8259A_irq(0);
  1788. clear_IO_APIC_pin(apic2, pin2);
  1789. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1790. }
  1791. if (nmi_watchdog == NMI_IO_APIC) {
  1792. apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
  1793. "through the IO-APIC - disabling NMI Watchdog!\n");
  1794. nmi_watchdog = NMI_NONE;
  1795. }
  1796. apic_printk(APIC_QUIET, KERN_INFO
  1797. "...trying to set up timer as Virtual Wire IRQ...\n");
  1798. lapic_register_intr(0);
  1799. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1800. enable_8259A_irq(0);
  1801. if (timer_irq_works()) {
  1802. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1803. goto out;
  1804. }
  1805. disable_8259A_irq(0);
  1806. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1807. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1808. apic_printk(APIC_QUIET, KERN_INFO
  1809. "...trying to set up timer as ExtINT IRQ...\n");
  1810. init_8259A(0);
  1811. make_8259A_irq(0);
  1812. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1813. unlock_ExtINT_logic();
  1814. if (timer_irq_works()) {
  1815. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1816. goto out;
  1817. }
  1818. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1819. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1820. "report. Then try booting with the 'noapic' option.\n");
  1821. out:
  1822. local_irq_restore(flags);
  1823. }
  1824. static int __init notimercheck(char *s)
  1825. {
  1826. no_timer_check = 1;
  1827. return 1;
  1828. }
  1829. __setup("no_timer_check", notimercheck);
  1830. /*
  1831. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1832. * to devices. However there may be an I/O APIC pin available for
  1833. * this interrupt regardless. The pin may be left unconnected, but
  1834. * typically it will be reused as an ExtINT cascade interrupt for
  1835. * the master 8259A. In the MPS case such a pin will normally be
  1836. * reported as an ExtINT interrupt in the MP table. With ACPI
  1837. * there is no provision for ExtINT interrupts, and in the absence
  1838. * of an override it would be treated as an ordinary ISA I/O APIC
  1839. * interrupt, that is edge-triggered and unmasked by default. We
  1840. * used to do this, but it caused problems on some systems because
  1841. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1842. * the same ExtINT cascade interrupt to drive the local APIC of the
  1843. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1844. * the I/O APIC in all cases now. No actual device should request
  1845. * it anyway. --macro
  1846. */
  1847. #define PIC_IRQS (1<<2)
  1848. void __init setup_IO_APIC(void)
  1849. {
  1850. /*
  1851. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  1852. */
  1853. io_apic_irqs = ~PIC_IRQS;
  1854. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  1855. sync_Arb_IDs();
  1856. setup_IO_APIC_irqs();
  1857. init_IO_APIC_traps();
  1858. check_timer();
  1859. }
  1860. struct sysfs_ioapic_data {
  1861. struct sys_device dev;
  1862. struct IO_APIC_route_entry entry[0];
  1863. };
  1864. static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
  1865. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  1866. {
  1867. struct IO_APIC_route_entry *entry;
  1868. struct sysfs_ioapic_data *data;
  1869. int i;
  1870. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1871. entry = data->entry;
  1872. for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
  1873. *entry = ioapic_read_entry(dev->id, i);
  1874. return 0;
  1875. }
  1876. static int ioapic_resume(struct sys_device *dev)
  1877. {
  1878. struct IO_APIC_route_entry *entry;
  1879. struct sysfs_ioapic_data *data;
  1880. unsigned long flags;
  1881. union IO_APIC_reg_00 reg_00;
  1882. int i;
  1883. data = container_of(dev, struct sysfs_ioapic_data, dev);
  1884. entry = data->entry;
  1885. spin_lock_irqsave(&ioapic_lock, flags);
  1886. reg_00.raw = io_apic_read(dev->id, 0);
  1887. if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
  1888. reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
  1889. io_apic_write(dev->id, 0, reg_00.raw);
  1890. }
  1891. spin_unlock_irqrestore(&ioapic_lock, flags);
  1892. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  1893. ioapic_write_entry(dev->id, i, entry[i]);
  1894. return 0;
  1895. }
  1896. static struct sysdev_class ioapic_sysdev_class = {
  1897. .name = "ioapic",
  1898. .suspend = ioapic_suspend,
  1899. .resume = ioapic_resume,
  1900. };
  1901. static int __init ioapic_init_sysfs(void)
  1902. {
  1903. struct sys_device * dev;
  1904. int i, size, error;
  1905. error = sysdev_class_register(&ioapic_sysdev_class);
  1906. if (error)
  1907. return error;
  1908. for (i = 0; i < nr_ioapics; i++ ) {
  1909. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  1910. * sizeof(struct IO_APIC_route_entry);
  1911. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  1912. if (!mp_ioapic_data[i]) {
  1913. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1914. continue;
  1915. }
  1916. dev = &mp_ioapic_data[i]->dev;
  1917. dev->id = i;
  1918. dev->cls = &ioapic_sysdev_class;
  1919. error = sysdev_register(dev);
  1920. if (error) {
  1921. kfree(mp_ioapic_data[i]);
  1922. mp_ioapic_data[i] = NULL;
  1923. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  1924. continue;
  1925. }
  1926. }
  1927. return 0;
  1928. }
  1929. device_initcall(ioapic_init_sysfs);
  1930. /*
  1931. * Dynamic irq allocate and deallocation
  1932. */
  1933. int create_irq(void)
  1934. {
  1935. /* Allocate an unused irq */
  1936. int irq;
  1937. int new;
  1938. unsigned long flags;
  1939. irq = -ENOSPC;
  1940. spin_lock_irqsave(&vector_lock, flags);
  1941. for (new = (NR_IRQS - 1); new >= 0; new--) {
  1942. if (platform_legacy_irq(new))
  1943. continue;
  1944. if (irq_cfg[new].vector != 0)
  1945. continue;
  1946. if (__assign_irq_vector(new, TARGET_CPUS) == 0)
  1947. irq = new;
  1948. break;
  1949. }
  1950. spin_unlock_irqrestore(&vector_lock, flags);
  1951. if (irq >= 0) {
  1952. dynamic_irq_init(irq);
  1953. }
  1954. return irq;
  1955. }
  1956. void destroy_irq(unsigned int irq)
  1957. {
  1958. unsigned long flags;
  1959. dynamic_irq_cleanup(irq);
  1960. #ifdef CONFIG_INTR_REMAP
  1961. free_irte(irq);
  1962. #endif
  1963. spin_lock_irqsave(&vector_lock, flags);
  1964. __clear_irq_vector(irq);
  1965. spin_unlock_irqrestore(&vector_lock, flags);
  1966. }
  1967. /*
  1968. * MSI message composition
  1969. */
  1970. #ifdef CONFIG_PCI_MSI
  1971. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  1972. {
  1973. struct irq_cfg *cfg = irq_cfg + irq;
  1974. int err;
  1975. unsigned dest;
  1976. cpumask_t tmp;
  1977. tmp = TARGET_CPUS;
  1978. err = assign_irq_vector(irq, tmp);
  1979. if (err)
  1980. return err;
  1981. cpus_and(tmp, cfg->domain, tmp);
  1982. dest = cpu_mask_to_apicid(tmp);
  1983. #ifdef CONFIG_INTR_REMAP
  1984. if (irq_remapped(irq)) {
  1985. struct irte irte;
  1986. int ir_index;
  1987. u16 sub_handle;
  1988. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  1989. BUG_ON(ir_index == -1);
  1990. memset (&irte, 0, sizeof(irte));
  1991. irte.present = 1;
  1992. irte.dst_mode = INT_DEST_MODE;
  1993. irte.trigger_mode = 0; /* edge */
  1994. irte.dlvry_mode = INT_DELIVERY_MODE;
  1995. irte.vector = cfg->vector;
  1996. irte.dest_id = IRTE_DEST(dest);
  1997. modify_irte(irq, &irte);
  1998. msg->address_hi = MSI_ADDR_BASE_HI;
  1999. msg->data = sub_handle;
  2000. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  2001. MSI_ADDR_IR_SHV |
  2002. MSI_ADDR_IR_INDEX1(ir_index) |
  2003. MSI_ADDR_IR_INDEX2(ir_index);
  2004. } else
  2005. #endif
  2006. {
  2007. msg->address_hi = MSI_ADDR_BASE_HI;
  2008. msg->address_lo =
  2009. MSI_ADDR_BASE_LO |
  2010. ((INT_DEST_MODE == 0) ?
  2011. MSI_ADDR_DEST_MODE_PHYSICAL:
  2012. MSI_ADDR_DEST_MODE_LOGICAL) |
  2013. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2014. MSI_ADDR_REDIRECTION_CPU:
  2015. MSI_ADDR_REDIRECTION_LOWPRI) |
  2016. MSI_ADDR_DEST_ID(dest);
  2017. msg->data =
  2018. MSI_DATA_TRIGGER_EDGE |
  2019. MSI_DATA_LEVEL_ASSERT |
  2020. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2021. MSI_DATA_DELIVERY_FIXED:
  2022. MSI_DATA_DELIVERY_LOWPRI) |
  2023. MSI_DATA_VECTOR(cfg->vector);
  2024. }
  2025. return err;
  2026. }
  2027. #ifdef CONFIG_SMP
  2028. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2029. {
  2030. struct irq_cfg *cfg = irq_cfg + irq;
  2031. struct msi_msg msg;
  2032. unsigned int dest;
  2033. cpumask_t tmp;
  2034. cpus_and(tmp, mask, cpu_online_map);
  2035. if (cpus_empty(tmp))
  2036. return;
  2037. if (assign_irq_vector(irq, mask))
  2038. return;
  2039. cpus_and(tmp, cfg->domain, mask);
  2040. dest = cpu_mask_to_apicid(tmp);
  2041. read_msi_msg(irq, &msg);
  2042. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2043. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2044. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2045. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2046. write_msi_msg(irq, &msg);
  2047. irq_desc[irq].affinity = mask;
  2048. }
  2049. #ifdef CONFIG_INTR_REMAP
  2050. /*
  2051. * Migrate the MSI irq to another cpumask. This migration is
  2052. * done in the process context using interrupt-remapping hardware.
  2053. */
  2054. static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2055. {
  2056. struct irq_cfg *cfg = irq_cfg + irq;
  2057. unsigned int dest;
  2058. cpumask_t tmp, cleanup_mask;
  2059. struct irte irte;
  2060. cpus_and(tmp, mask, cpu_online_map);
  2061. if (cpus_empty(tmp))
  2062. return;
  2063. if (get_irte(irq, &irte))
  2064. return;
  2065. if (assign_irq_vector(irq, mask))
  2066. return;
  2067. cpus_and(tmp, cfg->domain, mask);
  2068. dest = cpu_mask_to_apicid(tmp);
  2069. irte.vector = cfg->vector;
  2070. irte.dest_id = IRTE_DEST(dest);
  2071. /*
  2072. * atomically update the IRTE with the new destination and vector.
  2073. */
  2074. modify_irte(irq, &irte);
  2075. /*
  2076. * After this point, all the interrupts will start arriving
  2077. * at the new destination. So, time to cleanup the previous
  2078. * vector allocation.
  2079. */
  2080. if (cfg->move_in_progress) {
  2081. cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
  2082. cfg->move_cleanup_count = cpus_weight(cleanup_mask);
  2083. send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
  2084. cfg->move_in_progress = 0;
  2085. }
  2086. irq_desc[irq].affinity = mask;
  2087. }
  2088. #endif
  2089. #endif /* CONFIG_SMP */
  2090. /*
  2091. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2092. * which implement the MSI or MSI-X Capability Structure.
  2093. */
  2094. static struct irq_chip msi_chip = {
  2095. .name = "PCI-MSI",
  2096. .unmask = unmask_msi_irq,
  2097. .mask = mask_msi_irq,
  2098. .ack = ack_apic_edge,
  2099. #ifdef CONFIG_SMP
  2100. .set_affinity = set_msi_irq_affinity,
  2101. #endif
  2102. .retrigger = ioapic_retrigger_irq,
  2103. };
  2104. #ifdef CONFIG_INTR_REMAP
  2105. static struct irq_chip msi_ir_chip = {
  2106. .name = "IR-PCI-MSI",
  2107. .unmask = unmask_msi_irq,
  2108. .mask = mask_msi_irq,
  2109. .ack = ack_x2apic_edge,
  2110. #ifdef CONFIG_SMP
  2111. .set_affinity = ir_set_msi_irq_affinity,
  2112. #endif
  2113. .retrigger = ioapic_retrigger_irq,
  2114. };
  2115. /*
  2116. * Map the PCI dev to the corresponding remapping hardware unit
  2117. * and allocate 'nvec' consecutive interrupt-remapping table entries
  2118. * in it.
  2119. */
  2120. static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
  2121. {
  2122. struct intel_iommu *iommu;
  2123. int index;
  2124. iommu = map_dev_to_ir(dev);
  2125. if (!iommu) {
  2126. printk(KERN_ERR
  2127. "Unable to map PCI %s to iommu\n", pci_name(dev));
  2128. return -ENOENT;
  2129. }
  2130. index = alloc_irte(iommu, irq, nvec);
  2131. if (index < 0) {
  2132. printk(KERN_ERR
  2133. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  2134. pci_name(dev));
  2135. return -ENOSPC;
  2136. }
  2137. return index;
  2138. }
  2139. #endif
  2140. static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
  2141. {
  2142. int ret;
  2143. struct msi_msg msg;
  2144. ret = msi_compose_msg(dev, irq, &msg);
  2145. if (ret < 0)
  2146. return ret;
  2147. set_irq_msi(irq, desc);
  2148. write_msi_msg(irq, &msg);
  2149. #ifdef CONFIG_INTR_REMAP
  2150. if (irq_remapped(irq)) {
  2151. struct irq_desc *desc = irq_desc + irq;
  2152. /*
  2153. * irq migration in process context
  2154. */
  2155. desc->status |= IRQ_MOVE_PCNTXT;
  2156. set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
  2157. } else
  2158. #endif
  2159. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
  2160. return 0;
  2161. }
  2162. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2163. {
  2164. int irq, ret;
  2165. irq = create_irq();
  2166. if (irq < 0)
  2167. return irq;
  2168. #ifdef CONFIG_INTR_REMAP
  2169. if (!intr_remapping_enabled)
  2170. goto no_ir;
  2171. ret = msi_alloc_irte(dev, irq, 1);
  2172. if (ret < 0)
  2173. goto error;
  2174. no_ir:
  2175. #endif
  2176. ret = setup_msi_irq(dev, desc, irq);
  2177. if (ret < 0) {
  2178. destroy_irq(irq);
  2179. return ret;
  2180. }
  2181. return 0;
  2182. #ifdef CONFIG_INTR_REMAP
  2183. error:
  2184. destroy_irq(irq);
  2185. return ret;
  2186. #endif
  2187. }
  2188. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  2189. {
  2190. int irq, ret, sub_handle;
  2191. struct msi_desc *desc;
  2192. #ifdef CONFIG_INTR_REMAP
  2193. struct intel_iommu *iommu = 0;
  2194. int index = 0;
  2195. #endif
  2196. sub_handle = 0;
  2197. list_for_each_entry(desc, &dev->msi_list, list) {
  2198. irq = create_irq();
  2199. if (irq < 0)
  2200. return irq;
  2201. #ifdef CONFIG_INTR_REMAP
  2202. if (!intr_remapping_enabled)
  2203. goto no_ir;
  2204. if (!sub_handle) {
  2205. /*
  2206. * allocate the consecutive block of IRTE's
  2207. * for 'nvec'
  2208. */
  2209. index = msi_alloc_irte(dev, irq, nvec);
  2210. if (index < 0) {
  2211. ret = index;
  2212. goto error;
  2213. }
  2214. } else {
  2215. iommu = map_dev_to_ir(dev);
  2216. if (!iommu) {
  2217. ret = -ENOENT;
  2218. goto error;
  2219. }
  2220. /*
  2221. * setup the mapping between the irq and the IRTE
  2222. * base index, the sub_handle pointing to the
  2223. * appropriate interrupt remap table entry.
  2224. */
  2225. set_irte_irq(irq, iommu, index, sub_handle);
  2226. }
  2227. no_ir:
  2228. #endif
  2229. ret = setup_msi_irq(dev, desc, irq);
  2230. if (ret < 0)
  2231. goto error;
  2232. sub_handle++;
  2233. }
  2234. return 0;
  2235. error:
  2236. destroy_irq(irq);
  2237. return ret;
  2238. }
  2239. void arch_teardown_msi_irq(unsigned int irq)
  2240. {
  2241. destroy_irq(irq);
  2242. }
  2243. #ifdef CONFIG_DMAR
  2244. #ifdef CONFIG_SMP
  2245. static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
  2246. {
  2247. struct irq_cfg *cfg = irq_cfg + irq;
  2248. struct msi_msg msg;
  2249. unsigned int dest;
  2250. cpumask_t tmp;
  2251. cpus_and(tmp, mask, cpu_online_map);
  2252. if (cpus_empty(tmp))
  2253. return;
  2254. if (assign_irq_vector(irq, mask))
  2255. return;
  2256. cpus_and(tmp, cfg->domain, mask);
  2257. dest = cpu_mask_to_apicid(tmp);
  2258. dmar_msi_read(irq, &msg);
  2259. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2260. msg.data |= MSI_DATA_VECTOR(cfg->vector);
  2261. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2262. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2263. dmar_msi_write(irq, &msg);
  2264. irq_desc[irq].affinity = mask;
  2265. }
  2266. #endif /* CONFIG_SMP */
  2267. struct irq_chip dmar_msi_type = {
  2268. .name = "DMAR_MSI",
  2269. .unmask = dmar_msi_unmask,
  2270. .mask = dmar_msi_mask,
  2271. .ack = ack_apic_edge,
  2272. #ifdef CONFIG_SMP
  2273. .set_affinity = dmar_msi_set_affinity,
  2274. #endif
  2275. .retrigger = ioapic_retrigger_irq,
  2276. };
  2277. int arch_setup_dmar_msi(unsigned int irq)
  2278. {
  2279. int ret;
  2280. struct msi_msg msg;
  2281. ret = msi_compose_msg(NULL, irq, &msg);
  2282. if (ret < 0)
  2283. return ret;
  2284. dmar_msi_write(irq, &msg);
  2285. set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
  2286. "edge");
  2287. return 0;
  2288. }
  2289. #endif
  2290. #endif /* CONFIG_PCI_MSI */
  2291. /*
  2292. * Hypertransport interrupt support
  2293. */
  2294. #ifdef CONFIG_HT_IRQ
  2295. #ifdef CONFIG_SMP
  2296. static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
  2297. {
  2298. struct ht_irq_msg msg;
  2299. fetch_ht_irq_msg(irq, &msg);
  2300. msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
  2301. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2302. msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
  2303. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2304. write_ht_irq_msg(irq, &msg);
  2305. }
  2306. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2307. {
  2308. struct irq_cfg *cfg = irq_cfg + irq;
  2309. unsigned int dest;
  2310. cpumask_t tmp;
  2311. cpus_and(tmp, mask, cpu_online_map);
  2312. if (cpus_empty(tmp))
  2313. return;
  2314. if (assign_irq_vector(irq, mask))
  2315. return;
  2316. cpus_and(tmp, cfg->domain, mask);
  2317. dest = cpu_mask_to_apicid(tmp);
  2318. target_ht_irq(irq, dest, cfg->vector);
  2319. irq_desc[irq].affinity = mask;
  2320. }
  2321. #endif
  2322. static struct irq_chip ht_irq_chip = {
  2323. .name = "PCI-HT",
  2324. .mask = mask_ht_irq,
  2325. .unmask = unmask_ht_irq,
  2326. .ack = ack_apic_edge,
  2327. #ifdef CONFIG_SMP
  2328. .set_affinity = set_ht_irq_affinity,
  2329. #endif
  2330. .retrigger = ioapic_retrigger_irq,
  2331. };
  2332. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2333. {
  2334. struct irq_cfg *cfg = irq_cfg + irq;
  2335. int err;
  2336. cpumask_t tmp;
  2337. tmp = TARGET_CPUS;
  2338. err = assign_irq_vector(irq, tmp);
  2339. if (!err) {
  2340. struct ht_irq_msg msg;
  2341. unsigned dest;
  2342. cpus_and(tmp, cfg->domain, tmp);
  2343. dest = cpu_mask_to_apicid(tmp);
  2344. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2345. msg.address_lo =
  2346. HT_IRQ_LOW_BASE |
  2347. HT_IRQ_LOW_DEST_ID(dest) |
  2348. HT_IRQ_LOW_VECTOR(cfg->vector) |
  2349. ((INT_DEST_MODE == 0) ?
  2350. HT_IRQ_LOW_DM_PHYSICAL :
  2351. HT_IRQ_LOW_DM_LOGICAL) |
  2352. HT_IRQ_LOW_RQEOI_EDGE |
  2353. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2354. HT_IRQ_LOW_MT_FIXED :
  2355. HT_IRQ_LOW_MT_ARBITRATED) |
  2356. HT_IRQ_LOW_IRQ_MASKED;
  2357. write_ht_irq_msg(irq, &msg);
  2358. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2359. handle_edge_irq, "edge");
  2360. }
  2361. return err;
  2362. }
  2363. #endif /* CONFIG_HT_IRQ */
  2364. /* --------------------------------------------------------------------------
  2365. ACPI-based IOAPIC Configuration
  2366. -------------------------------------------------------------------------- */
  2367. #ifdef CONFIG_ACPI
  2368. #define IO_APIC_MAX_ID 0xFE
  2369. int __init io_apic_get_redir_entries (int ioapic)
  2370. {
  2371. union IO_APIC_reg_01 reg_01;
  2372. unsigned long flags;
  2373. spin_lock_irqsave(&ioapic_lock, flags);
  2374. reg_01.raw = io_apic_read(ioapic, 1);
  2375. spin_unlock_irqrestore(&ioapic_lock, flags);
  2376. return reg_01.bits.entries;
  2377. }
  2378. int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
  2379. {
  2380. if (!IO_APIC_IRQ(irq)) {
  2381. apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2382. ioapic);
  2383. return -EINVAL;
  2384. }
  2385. /*
  2386. * IRQs < 16 are already in the irq_2_pin[] map
  2387. */
  2388. if (irq >= 16)
  2389. add_pin_to_irq(irq, ioapic, pin);
  2390. setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
  2391. return 0;
  2392. }
  2393. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  2394. {
  2395. int i;
  2396. if (skip_ioapic_setup)
  2397. return -1;
  2398. for (i = 0; i < mp_irq_entries; i++)
  2399. if (mp_irqs[i].mp_irqtype == mp_INT &&
  2400. mp_irqs[i].mp_srcbusirq == bus_irq)
  2401. break;
  2402. if (i >= mp_irq_entries)
  2403. return -1;
  2404. *trigger = irq_trigger(i);
  2405. *polarity = irq_polarity(i);
  2406. return 0;
  2407. }
  2408. #endif /* CONFIG_ACPI */
  2409. /*
  2410. * This function currently is only a helper for the i386 smp boot process where
  2411. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  2412. * so mask in all cases should simply be TARGET_CPUS
  2413. */
  2414. #ifdef CONFIG_SMP
  2415. void __init setup_ioapic_dest(void)
  2416. {
  2417. int pin, ioapic, irq, irq_entry;
  2418. if (skip_ioapic_setup == 1)
  2419. return;
  2420. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  2421. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  2422. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  2423. if (irq_entry == -1)
  2424. continue;
  2425. irq = pin_2_irq(irq_entry, ioapic, pin);
  2426. /* setup_IO_APIC_irqs could fail to get vector for some device
  2427. * when you have too many devices, because at that time only boot
  2428. * cpu is online.
  2429. */
  2430. if (!irq_cfg[irq].vector)
  2431. setup_IO_APIC_irq(ioapic, pin, irq,
  2432. irq_trigger(irq_entry),
  2433. irq_polarity(irq_entry));
  2434. #ifdef CONFIG_INTR_REMAP
  2435. else if (intr_remapping_enabled)
  2436. set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
  2437. #endif
  2438. else
  2439. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  2440. }
  2441. }
  2442. }
  2443. #endif
  2444. #define IOAPIC_RESOURCE_NAME_SIZE 11
  2445. static struct resource *ioapic_resources;
  2446. static struct resource * __init ioapic_setup_resources(void)
  2447. {
  2448. unsigned long n;
  2449. struct resource *res;
  2450. char *mem;
  2451. int i;
  2452. if (nr_ioapics <= 0)
  2453. return NULL;
  2454. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  2455. n *= nr_ioapics;
  2456. mem = alloc_bootmem(n);
  2457. res = (void *)mem;
  2458. if (mem != NULL) {
  2459. mem += sizeof(struct resource) * nr_ioapics;
  2460. for (i = 0; i < nr_ioapics; i++) {
  2461. res[i].name = mem;
  2462. res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  2463. sprintf(mem, "IOAPIC %u", i);
  2464. mem += IOAPIC_RESOURCE_NAME_SIZE;
  2465. }
  2466. }
  2467. ioapic_resources = res;
  2468. return res;
  2469. }
  2470. void __init ioapic_init_mappings(void)
  2471. {
  2472. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2473. struct resource *ioapic_res;
  2474. int i;
  2475. ioapic_res = ioapic_setup_resources();
  2476. for (i = 0; i < nr_ioapics; i++) {
  2477. if (smp_found_config) {
  2478. ioapic_phys = mp_ioapics[i].mp_apicaddr;
  2479. } else {
  2480. ioapic_phys = (unsigned long)
  2481. alloc_bootmem_pages(PAGE_SIZE);
  2482. ioapic_phys = __pa(ioapic_phys);
  2483. }
  2484. set_fixmap_nocache(idx, ioapic_phys);
  2485. apic_printk(APIC_VERBOSE,
  2486. "mapped IOAPIC to %016lx (%016lx)\n",
  2487. __fix_to_virt(idx), ioapic_phys);
  2488. idx++;
  2489. if (ioapic_res != NULL) {
  2490. ioapic_res->start = ioapic_phys;
  2491. ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
  2492. ioapic_res++;
  2493. }
  2494. }
  2495. }
  2496. static int __init ioapic_insert_resources(void)
  2497. {
  2498. int i;
  2499. struct resource *r = ioapic_resources;
  2500. if (!r) {
  2501. printk(KERN_ERR
  2502. "IO APIC resources could be not be allocated.\n");
  2503. return -1;
  2504. }
  2505. for (i = 0; i < nr_ioapics; i++) {
  2506. insert_resource(&iomem_resource, r);
  2507. r++;
  2508. }
  2509. return 0;
  2510. }
  2511. /* Insert the IO APIC resources after PCI initialization has occured to handle
  2512. * IO APICS that are mapped in on a BAR in PCI space. */
  2513. late_initcall(ioapic_insert_resources);