io_apic_64.c 71 KB

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