io_apic_32.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842
  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/mc146818rtc.h>
  28. #include <linux/compiler.h>
  29. #include <linux/acpi.h>
  30. #include <linux/module.h>
  31. #include <linux/sysdev.h>
  32. #include <linux/pci.h>
  33. #include <linux/msi.h>
  34. #include <linux/htirq.h>
  35. #include <linux/freezer.h>
  36. #include <linux/kthread.h>
  37. #include <linux/jiffies.h> /* time_after() */
  38. #include <asm/io.h>
  39. #include <asm/smp.h>
  40. #include <asm/desc.h>
  41. #include <asm/timer.h>
  42. #include <asm/i8259.h>
  43. #include <asm/nmi.h>
  44. #include <asm/msidef.h>
  45. #include <asm/hypertransport.h>
  46. #include <mach_apic.h>
  47. #include <mach_apicdef.h>
  48. int (*ioapic_renumber_irq)(int ioapic, int irq);
  49. atomic_t irq_mis_count;
  50. /* Where if anywhere is the i8259 connect in external int mode */
  51. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  52. static DEFINE_SPINLOCK(ioapic_lock);
  53. static DEFINE_SPINLOCK(vector_lock);
  54. int timer_through_8259 __initdata;
  55. /*
  56. * Is the SiS APIC rmw bug present ?
  57. * -1 = don't know, 0 = no, 1 = yes
  58. */
  59. int sis_apic_bug = -1;
  60. /*
  61. * # of IRQ routing registers
  62. */
  63. int nr_ioapic_registers[MAX_IO_APICS];
  64. /* I/O APIC entries */
  65. struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
  66. int nr_ioapics;
  67. /* MP IRQ source entries */
  68. struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
  69. /* # of MP IRQ source entries */
  70. int mp_irq_entries;
  71. static int disable_timer_pin_1 __initdata;
  72. /*
  73. * Rough estimation of how many shared IRQs there are, can
  74. * be changed anytime.
  75. */
  76. #define MAX_PLUS_SHARED_IRQS NR_IRQS
  77. #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
  78. /*
  79. * This is performance-critical, we want to do it O(1)
  80. *
  81. * the indexing order of this array favors 1:1 mappings
  82. * between pins and IRQs.
  83. */
  84. static struct irq_pin_list {
  85. int apic, pin, next;
  86. } irq_2_pin[PIN_MAP_SIZE];
  87. struct io_apic {
  88. unsigned int index;
  89. unsigned int unused[3];
  90. unsigned int data;
  91. };
  92. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  93. {
  94. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  95. + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
  96. }
  97. static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
  98. {
  99. struct io_apic __iomem *io_apic = io_apic_base(apic);
  100. writel(reg, &io_apic->index);
  101. return readl(&io_apic->data);
  102. }
  103. static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  104. {
  105. struct io_apic __iomem *io_apic = io_apic_base(apic);
  106. writel(reg, &io_apic->index);
  107. writel(value, &io_apic->data);
  108. }
  109. /*
  110. * Re-write a value: to be used for read-modify-write
  111. * cycles where the read already set up the index register.
  112. *
  113. * Older SiS APIC requires we rewrite the index register
  114. */
  115. static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  116. {
  117. volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
  118. if (sis_apic_bug)
  119. writel(reg, &io_apic->index);
  120. writel(value, &io_apic->data);
  121. }
  122. union entry_union {
  123. struct { u32 w1, w2; };
  124. struct IO_APIC_route_entry entry;
  125. };
  126. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  127. {
  128. union entry_union eu;
  129. unsigned long flags;
  130. spin_lock_irqsave(&ioapic_lock, flags);
  131. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  132. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  133. spin_unlock_irqrestore(&ioapic_lock, flags);
  134. return eu.entry;
  135. }
  136. /*
  137. * When we write a new IO APIC routing entry, we need to write the high
  138. * word first! If the mask bit in the low word is clear, we will enable
  139. * the interrupt, and we need to make sure the entry is fully populated
  140. * before that happens.
  141. */
  142. static void
  143. __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  144. {
  145. union entry_union eu;
  146. eu.entry = e;
  147. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  148. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  149. }
  150. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  151. {
  152. unsigned long flags;
  153. spin_lock_irqsave(&ioapic_lock, flags);
  154. __ioapic_write_entry(apic, pin, e);
  155. spin_unlock_irqrestore(&ioapic_lock, flags);
  156. }
  157. /*
  158. * When we mask an IO APIC routing entry, we need to write the low
  159. * word first, in order to set the mask bit before we change the
  160. * high bits!
  161. */
  162. static void ioapic_mask_entry(int apic, int pin)
  163. {
  164. unsigned long flags;
  165. union entry_union eu = { .entry.mask = 1 };
  166. spin_lock_irqsave(&ioapic_lock, flags);
  167. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  168. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  169. spin_unlock_irqrestore(&ioapic_lock, flags);
  170. }
  171. /*
  172. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  173. * shared ISA-space IRQs, so we have to support them. We are super
  174. * fast in the common case, and fast for shared ISA-space IRQs.
  175. */
  176. static void add_pin_to_irq(unsigned int irq, int apic, int pin)
  177. {
  178. static int first_free_entry = NR_IRQS;
  179. struct irq_pin_list *entry = irq_2_pin + irq;
  180. while (entry->next)
  181. entry = irq_2_pin + entry->next;
  182. if (entry->pin != -1) {
  183. entry->next = first_free_entry;
  184. entry = irq_2_pin + entry->next;
  185. if (++first_free_entry >= PIN_MAP_SIZE)
  186. panic("io_apic.c: whoops");
  187. }
  188. entry->apic = apic;
  189. entry->pin = pin;
  190. }
  191. /*
  192. * Reroute an IRQ to a different pin.
  193. */
  194. static void __init replace_pin_at_irq(unsigned int irq,
  195. int oldapic, int oldpin,
  196. int newapic, int newpin)
  197. {
  198. struct irq_pin_list *entry = irq_2_pin + irq;
  199. while (1) {
  200. if (entry->apic == oldapic && entry->pin == oldpin) {
  201. entry->apic = newapic;
  202. entry->pin = newpin;
  203. }
  204. if (!entry->next)
  205. break;
  206. entry = irq_2_pin + entry->next;
  207. }
  208. }
  209. static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
  210. {
  211. struct irq_pin_list *entry = irq_2_pin + irq;
  212. unsigned int pin, reg;
  213. for (;;) {
  214. pin = entry->pin;
  215. if (pin == -1)
  216. break;
  217. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  218. reg &= ~disable;
  219. reg |= enable;
  220. io_apic_modify(entry->apic, 0x10 + pin*2, reg);
  221. if (!entry->next)
  222. break;
  223. entry = irq_2_pin + entry->next;
  224. }
  225. }
  226. /* mask = 1 */
  227. static void __mask_IO_APIC_irq(unsigned int irq)
  228. {
  229. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
  230. }
  231. /* mask = 0 */
  232. static void __unmask_IO_APIC_irq(unsigned int irq)
  233. {
  234. __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
  235. }
  236. /* mask = 1, trigger = 0 */
  237. static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
  238. {
  239. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
  240. IO_APIC_REDIR_LEVEL_TRIGGER);
  241. }
  242. /* mask = 0, trigger = 1 */
  243. static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
  244. {
  245. __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
  246. IO_APIC_REDIR_MASKED);
  247. }
  248. static void mask_IO_APIC_irq(unsigned int irq)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&ioapic_lock, flags);
  252. __mask_IO_APIC_irq(irq);
  253. spin_unlock_irqrestore(&ioapic_lock, flags);
  254. }
  255. static void unmask_IO_APIC_irq(unsigned int irq)
  256. {
  257. unsigned long flags;
  258. spin_lock_irqsave(&ioapic_lock, flags);
  259. __unmask_IO_APIC_irq(irq);
  260. spin_unlock_irqrestore(&ioapic_lock, flags);
  261. }
  262. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  263. {
  264. struct IO_APIC_route_entry entry;
  265. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  266. entry = ioapic_read_entry(apic, pin);
  267. if (entry.delivery_mode == dest_SMI)
  268. return;
  269. /*
  270. * Disable it in the IO-APIC irq-routing table:
  271. */
  272. ioapic_mask_entry(apic, pin);
  273. }
  274. static void clear_IO_APIC(void)
  275. {
  276. int apic, pin;
  277. for (apic = 0; apic < nr_ioapics; apic++)
  278. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
  279. clear_IO_APIC_pin(apic, pin);
  280. }
  281. #ifdef CONFIG_SMP
  282. static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
  283. {
  284. unsigned long flags;
  285. int pin;
  286. struct irq_pin_list *entry = irq_2_pin + irq;
  287. unsigned int apicid_value;
  288. cpumask_t tmp;
  289. cpus_and(tmp, cpumask, cpu_online_map);
  290. if (cpus_empty(tmp))
  291. tmp = TARGET_CPUS;
  292. cpus_and(cpumask, tmp, CPU_MASK_ALL);
  293. apicid_value = cpu_mask_to_apicid(cpumask);
  294. /* Prepare to do the io_apic_write */
  295. apicid_value = apicid_value << 24;
  296. spin_lock_irqsave(&ioapic_lock, flags);
  297. for (;;) {
  298. pin = entry->pin;
  299. if (pin == -1)
  300. break;
  301. io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
  302. if (!entry->next)
  303. break;
  304. entry = irq_2_pin + entry->next;
  305. }
  306. irq_desc[irq].affinity = cpumask;
  307. spin_unlock_irqrestore(&ioapic_lock, flags);
  308. }
  309. #if defined(CONFIG_IRQBALANCE)
  310. # include <asm/processor.h> /* kernel_thread() */
  311. # include <linux/kernel_stat.h> /* kstat */
  312. # include <linux/slab.h> /* kmalloc() */
  313. # include <linux/timer.h>
  314. #define IRQBALANCE_CHECK_ARCH -999
  315. #define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
  316. #define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
  317. #define BALANCED_IRQ_MORE_DELTA (HZ/10)
  318. #define BALANCED_IRQ_LESS_DELTA (HZ)
  319. static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
  320. static int physical_balance __read_mostly;
  321. static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
  322. static struct irq_cpu_info {
  323. unsigned long *last_irq;
  324. unsigned long *irq_delta;
  325. unsigned long irq;
  326. } irq_cpu_data[NR_CPUS];
  327. #define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
  328. #define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
  329. #define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
  330. #define IDLE_ENOUGH(cpu,now) \
  331. (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
  332. #define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
  333. #define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
  334. static cpumask_t balance_irq_affinity[NR_IRQS] = {
  335. [0 ... NR_IRQS-1] = CPU_MASK_ALL
  336. };
  337. void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
  338. {
  339. balance_irq_affinity[irq] = mask;
  340. }
  341. static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
  342. unsigned long now, int direction)
  343. {
  344. int search_idle = 1;
  345. int cpu = curr_cpu;
  346. goto inside;
  347. do {
  348. if (unlikely(cpu == curr_cpu))
  349. search_idle = 0;
  350. inside:
  351. if (direction == 1) {
  352. cpu++;
  353. if (cpu >= NR_CPUS)
  354. cpu = 0;
  355. } else {
  356. cpu--;
  357. if (cpu == -1)
  358. cpu = NR_CPUS-1;
  359. }
  360. } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
  361. (search_idle && !IDLE_ENOUGH(cpu, now)));
  362. return cpu;
  363. }
  364. static inline void balance_irq(int cpu, int irq)
  365. {
  366. unsigned long now = jiffies;
  367. cpumask_t allowed_mask;
  368. unsigned int new_cpu;
  369. if (irqbalance_disabled)
  370. return;
  371. cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
  372. new_cpu = move(cpu, allowed_mask, now, 1);
  373. if (cpu != new_cpu)
  374. set_pending_irq(irq, cpumask_of_cpu(new_cpu));
  375. }
  376. static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
  377. {
  378. int i, j;
  379. for_each_online_cpu(i) {
  380. for (j = 0; j < NR_IRQS; j++) {
  381. if (!irq_desc[j].action)
  382. continue;
  383. /* Is it a significant load ? */
  384. if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
  385. useful_load_threshold)
  386. continue;
  387. balance_irq(i, j);
  388. }
  389. }
  390. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  391. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  392. return;
  393. }
  394. static void do_irq_balance(void)
  395. {
  396. int i, j;
  397. unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
  398. unsigned long move_this_load = 0;
  399. int max_loaded = 0, min_loaded = 0;
  400. int load;
  401. unsigned long useful_load_threshold = balanced_irq_interval + 10;
  402. int selected_irq;
  403. int tmp_loaded, first_attempt = 1;
  404. unsigned long tmp_cpu_irq;
  405. unsigned long imbalance = 0;
  406. cpumask_t allowed_mask, target_cpu_mask, tmp;
  407. for_each_possible_cpu(i) {
  408. int package_index;
  409. CPU_IRQ(i) = 0;
  410. if (!cpu_online(i))
  411. continue;
  412. package_index = CPU_TO_PACKAGEINDEX(i);
  413. for (j = 0; j < NR_IRQS; j++) {
  414. unsigned long value_now, delta;
  415. /* Is this an active IRQ or balancing disabled ? */
  416. if (!irq_desc[j].action || irq_balancing_disabled(j))
  417. continue;
  418. if (package_index == i)
  419. IRQ_DELTA(package_index, j) = 0;
  420. /* Determine the total count per processor per IRQ */
  421. value_now = (unsigned long) kstat_cpu(i).irqs[j];
  422. /* Determine the activity per processor per IRQ */
  423. delta = value_now - LAST_CPU_IRQ(i, j);
  424. /* Update last_cpu_irq[][] for the next time */
  425. LAST_CPU_IRQ(i, j) = value_now;
  426. /* Ignore IRQs whose rate is less than the clock */
  427. if (delta < useful_load_threshold)
  428. continue;
  429. /* update the load for the processor or package total */
  430. IRQ_DELTA(package_index, j) += delta;
  431. /* Keep track of the higher numbered sibling as well */
  432. if (i != package_index)
  433. CPU_IRQ(i) += delta;
  434. /*
  435. * We have sibling A and sibling B in the package
  436. *
  437. * cpu_irq[A] = load for cpu A + load for cpu B
  438. * cpu_irq[B] = load for cpu B
  439. */
  440. CPU_IRQ(package_index) += delta;
  441. }
  442. }
  443. /* Find the least loaded processor package */
  444. for_each_online_cpu(i) {
  445. if (i != CPU_TO_PACKAGEINDEX(i))
  446. continue;
  447. if (min_cpu_irq > CPU_IRQ(i)) {
  448. min_cpu_irq = CPU_IRQ(i);
  449. min_loaded = i;
  450. }
  451. }
  452. max_cpu_irq = ULONG_MAX;
  453. tryanothercpu:
  454. /*
  455. * Look for heaviest loaded processor.
  456. * We may come back to get the next heaviest loaded processor.
  457. * Skip processors with trivial loads.
  458. */
  459. tmp_cpu_irq = 0;
  460. tmp_loaded = -1;
  461. for_each_online_cpu(i) {
  462. if (i != CPU_TO_PACKAGEINDEX(i))
  463. continue;
  464. if (max_cpu_irq <= CPU_IRQ(i))
  465. continue;
  466. if (tmp_cpu_irq < CPU_IRQ(i)) {
  467. tmp_cpu_irq = CPU_IRQ(i);
  468. tmp_loaded = i;
  469. }
  470. }
  471. if (tmp_loaded == -1) {
  472. /*
  473. * In the case of small number of heavy interrupt sources,
  474. * loading some of the cpus too much. We use Ingo's original
  475. * approach to rotate them around.
  476. */
  477. if (!first_attempt && imbalance >= useful_load_threshold) {
  478. rotate_irqs_among_cpus(useful_load_threshold);
  479. return;
  480. }
  481. goto not_worth_the_effort;
  482. }
  483. first_attempt = 0; /* heaviest search */
  484. max_cpu_irq = tmp_cpu_irq; /* load */
  485. max_loaded = tmp_loaded; /* processor */
  486. imbalance = (max_cpu_irq - min_cpu_irq) / 2;
  487. /*
  488. * if imbalance is less than approx 10% of max load, then
  489. * observe diminishing returns action. - quit
  490. */
  491. if (imbalance < (max_cpu_irq >> 3))
  492. goto not_worth_the_effort;
  493. tryanotherirq:
  494. /* if we select an IRQ to move that can't go where we want, then
  495. * see if there is another one to try.
  496. */
  497. move_this_load = 0;
  498. selected_irq = -1;
  499. for (j = 0; j < NR_IRQS; j++) {
  500. /* Is this an active IRQ? */
  501. if (!irq_desc[j].action)
  502. continue;
  503. if (imbalance <= IRQ_DELTA(max_loaded, j))
  504. continue;
  505. /* Try to find the IRQ that is closest to the imbalance
  506. * without going over.
  507. */
  508. if (move_this_load < IRQ_DELTA(max_loaded, j)) {
  509. move_this_load = IRQ_DELTA(max_loaded, j);
  510. selected_irq = j;
  511. }
  512. }
  513. if (selected_irq == -1)
  514. goto tryanothercpu;
  515. imbalance = move_this_load;
  516. /* For physical_balance case, we accumulated both load
  517. * values in the one of the siblings cpu_irq[],
  518. * to use the same code for physical and logical processors
  519. * as much as possible.
  520. *
  521. * NOTE: the cpu_irq[] array holds the sum of the load for
  522. * sibling A and sibling B in the slot for the lowest numbered
  523. * sibling (A), _AND_ the load for sibling B in the slot for
  524. * the higher numbered sibling.
  525. *
  526. * We seek the least loaded sibling by making the comparison
  527. * (A+B)/2 vs B
  528. */
  529. load = CPU_IRQ(min_loaded) >> 1;
  530. for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
  531. if (load > CPU_IRQ(j)) {
  532. /* This won't change cpu_sibling_map[min_loaded] */
  533. load = CPU_IRQ(j);
  534. min_loaded = j;
  535. }
  536. }
  537. cpus_and(allowed_mask,
  538. cpu_online_map,
  539. balance_irq_affinity[selected_irq]);
  540. target_cpu_mask = cpumask_of_cpu(min_loaded);
  541. cpus_and(tmp, target_cpu_mask, allowed_mask);
  542. if (!cpus_empty(tmp)) {
  543. /* mark for change destination */
  544. set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
  545. /* Since we made a change, come back sooner to
  546. * check for more variation.
  547. */
  548. balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
  549. balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
  550. return;
  551. }
  552. goto tryanotherirq;
  553. not_worth_the_effort:
  554. /*
  555. * if we did not find an IRQ to move, then adjust the time interval
  556. * upward
  557. */
  558. balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
  559. balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
  560. return;
  561. }
  562. static int balanced_irq(void *unused)
  563. {
  564. int i;
  565. unsigned long prev_balance_time = jiffies;
  566. long time_remaining = balanced_irq_interval;
  567. /* push everything to CPU 0 to give us a starting point. */
  568. for (i = 0 ; i < NR_IRQS ; i++) {
  569. irq_desc[i].pending_mask = cpumask_of_cpu(0);
  570. set_pending_irq(i, cpumask_of_cpu(0));
  571. }
  572. set_freezable();
  573. for ( ; ; ) {
  574. time_remaining = schedule_timeout_interruptible(time_remaining);
  575. try_to_freeze();
  576. if (time_after(jiffies,
  577. prev_balance_time+balanced_irq_interval)) {
  578. preempt_disable();
  579. do_irq_balance();
  580. prev_balance_time = jiffies;
  581. time_remaining = balanced_irq_interval;
  582. preempt_enable();
  583. }
  584. }
  585. return 0;
  586. }
  587. static int __init balanced_irq_init(void)
  588. {
  589. int i;
  590. struct cpuinfo_x86 *c;
  591. cpumask_t tmp;
  592. cpus_shift_right(tmp, cpu_online_map, 2);
  593. c = &boot_cpu_data;
  594. /* When not overwritten by the command line ask subarchitecture. */
  595. if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
  596. irqbalance_disabled = NO_BALANCE_IRQ;
  597. if (irqbalance_disabled)
  598. return 0;
  599. /* disable irqbalance completely if there is only one processor online */
  600. if (num_online_cpus() < 2) {
  601. irqbalance_disabled = 1;
  602. return 0;
  603. }
  604. /*
  605. * Enable physical balance only if more than 1 physical processor
  606. * is present
  607. */
  608. if (smp_num_siblings > 1 && !cpus_empty(tmp))
  609. physical_balance = 1;
  610. for_each_online_cpu(i) {
  611. irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
  612. irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
  613. if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
  614. printk(KERN_ERR "balanced_irq_init: out of memory");
  615. goto failed;
  616. }
  617. }
  618. printk(KERN_INFO "Starting balanced_irq\n");
  619. if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
  620. return 0;
  621. printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
  622. failed:
  623. for_each_possible_cpu(i) {
  624. kfree(irq_cpu_data[i].irq_delta);
  625. irq_cpu_data[i].irq_delta = NULL;
  626. kfree(irq_cpu_data[i].last_irq);
  627. irq_cpu_data[i].last_irq = NULL;
  628. }
  629. return 0;
  630. }
  631. int __devinit irqbalance_disable(char *str)
  632. {
  633. irqbalance_disabled = 1;
  634. return 1;
  635. }
  636. __setup("noirqbalance", irqbalance_disable);
  637. late_initcall(balanced_irq_init);
  638. #endif /* CONFIG_IRQBALANCE */
  639. #endif /* CONFIG_SMP */
  640. #ifndef CONFIG_SMP
  641. void send_IPI_self(int vector)
  642. {
  643. unsigned int cfg;
  644. /*
  645. * Wait for idle.
  646. */
  647. apic_wait_icr_idle();
  648. cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
  649. /*
  650. * Send the IPI. The write to APIC_ICR fires this off.
  651. */
  652. apic_write_around(APIC_ICR, cfg);
  653. }
  654. #endif /* !CONFIG_SMP */
  655. /*
  656. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  657. * specific CPU-side IRQs.
  658. */
  659. #define MAX_PIRQS 8
  660. static int pirq_entries [MAX_PIRQS];
  661. static int pirqs_enabled;
  662. int skip_ioapic_setup;
  663. static int __init ioapic_pirq_setup(char *str)
  664. {
  665. int i, max;
  666. int ints[MAX_PIRQS+1];
  667. get_options(str, ARRAY_SIZE(ints), ints);
  668. for (i = 0; i < MAX_PIRQS; i++)
  669. pirq_entries[i] = -1;
  670. pirqs_enabled = 1;
  671. apic_printk(APIC_VERBOSE, KERN_INFO
  672. "PIRQ redirection, working around broken MP-BIOS.\n");
  673. max = MAX_PIRQS;
  674. if (ints[0] < MAX_PIRQS)
  675. max = ints[0];
  676. for (i = 0; i < max; i++) {
  677. apic_printk(APIC_VERBOSE, KERN_DEBUG
  678. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  679. /*
  680. * PIRQs are mapped upside down, usually.
  681. */
  682. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  683. }
  684. return 1;
  685. }
  686. __setup("pirq=", ioapic_pirq_setup);
  687. /*
  688. * Find the IRQ entry number of a certain pin.
  689. */
  690. static int find_irq_entry(int apic, int pin, int type)
  691. {
  692. int i;
  693. for (i = 0; i < mp_irq_entries; i++)
  694. if (mp_irqs[i].mpc_irqtype == type &&
  695. (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
  696. mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
  697. mp_irqs[i].mpc_dstirq == pin)
  698. return i;
  699. return -1;
  700. }
  701. /*
  702. * Find the pin to which IRQ[irq] (ISA) is connected
  703. */
  704. static int __init find_isa_irq_pin(int irq, int type)
  705. {
  706. int i;
  707. for (i = 0; i < mp_irq_entries; i++) {
  708. int lbus = mp_irqs[i].mpc_srcbus;
  709. if (test_bit(lbus, mp_bus_not_pci) &&
  710. (mp_irqs[i].mpc_irqtype == type) &&
  711. (mp_irqs[i].mpc_srcbusirq == irq))
  712. return mp_irqs[i].mpc_dstirq;
  713. }
  714. return -1;
  715. }
  716. static int __init find_isa_irq_apic(int irq, int type)
  717. {
  718. int i;
  719. for (i = 0; i < mp_irq_entries; i++) {
  720. int lbus = mp_irqs[i].mpc_srcbus;
  721. if (test_bit(lbus, mp_bus_not_pci) &&
  722. (mp_irqs[i].mpc_irqtype == type) &&
  723. (mp_irqs[i].mpc_srcbusirq == irq))
  724. break;
  725. }
  726. if (i < mp_irq_entries) {
  727. int apic;
  728. for (apic = 0; apic < nr_ioapics; apic++) {
  729. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
  730. return apic;
  731. }
  732. }
  733. return -1;
  734. }
  735. /*
  736. * Find a specific PCI IRQ entry.
  737. * Not an __init, possibly needed by modules
  738. */
  739. static int pin_2_irq(int idx, int apic, int pin);
  740. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  741. {
  742. int apic, i, best_guess = -1;
  743. apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
  744. "slot:%d, pin:%d.\n", bus, slot, pin);
  745. if (mp_bus_id_to_pci_bus[bus] == -1) {
  746. printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  747. return -1;
  748. }
  749. for (i = 0; i < mp_irq_entries; i++) {
  750. int lbus = mp_irqs[i].mpc_srcbus;
  751. for (apic = 0; apic < nr_ioapics; apic++)
  752. if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
  753. mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
  754. break;
  755. if (!test_bit(lbus, mp_bus_not_pci) &&
  756. !mp_irqs[i].mpc_irqtype &&
  757. (bus == lbus) &&
  758. (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
  759. int irq = pin_2_irq(i, apic, mp_irqs[i].mpc_dstirq);
  760. if (!(apic || IO_APIC_IRQ(irq)))
  761. continue;
  762. if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
  763. return irq;
  764. /*
  765. * Use the first all-but-pin matching entry as a
  766. * best-guess fuzzy result for broken mptables.
  767. */
  768. if (best_guess < 0)
  769. best_guess = irq;
  770. }
  771. }
  772. return best_guess;
  773. }
  774. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  775. /*
  776. * This function currently is only a helper for the i386 smp boot process where
  777. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  778. * so mask in all cases should simply be TARGET_CPUS
  779. */
  780. #ifdef CONFIG_SMP
  781. void __init setup_ioapic_dest(void)
  782. {
  783. int pin, ioapic, irq, irq_entry;
  784. if (skip_ioapic_setup == 1)
  785. return;
  786. for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
  787. for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
  788. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  789. if (irq_entry == -1)
  790. continue;
  791. irq = pin_2_irq(irq_entry, ioapic, pin);
  792. set_ioapic_affinity_irq(irq, TARGET_CPUS);
  793. }
  794. }
  795. }
  796. #endif
  797. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  798. /*
  799. * EISA Edge/Level control register, ELCR
  800. */
  801. static int EISA_ELCR(unsigned int irq)
  802. {
  803. if (irq < 16) {
  804. unsigned int port = 0x4d0 + (irq >> 3);
  805. return (inb(port) >> (irq & 7)) & 1;
  806. }
  807. apic_printk(APIC_VERBOSE, KERN_INFO
  808. "Broken MPtable reports ISA irq %d\n", irq);
  809. return 0;
  810. }
  811. #endif
  812. /* ISA interrupts are always polarity zero edge triggered,
  813. * when listed as conforming in the MP table. */
  814. #define default_ISA_trigger(idx) (0)
  815. #define default_ISA_polarity(idx) (0)
  816. /* EISA interrupts are always polarity zero and can be edge or level
  817. * trigger depending on the ELCR value. If an interrupt is listed as
  818. * EISA conforming in the MP table, that means its trigger type must
  819. * be read in from the ELCR */
  820. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
  821. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  822. /* PCI interrupts are always polarity one level triggered,
  823. * when listed as conforming in the MP table. */
  824. #define default_PCI_trigger(idx) (1)
  825. #define default_PCI_polarity(idx) (1)
  826. /* MCA interrupts are always polarity zero level triggered,
  827. * when listed as conforming in the MP table. */
  828. #define default_MCA_trigger(idx) (1)
  829. #define default_MCA_polarity(idx) default_ISA_polarity(idx)
  830. static int MPBIOS_polarity(int idx)
  831. {
  832. int bus = mp_irqs[idx].mpc_srcbus;
  833. int polarity;
  834. /*
  835. * Determine IRQ line polarity (high active or low active):
  836. */
  837. switch (mp_irqs[idx].mpc_irqflag & 3) {
  838. case 0: /* conforms, ie. bus-type dependent polarity */
  839. {
  840. polarity = test_bit(bus, mp_bus_not_pci)?
  841. default_ISA_polarity(idx):
  842. default_PCI_polarity(idx);
  843. break;
  844. }
  845. case 1: /* high active */
  846. {
  847. polarity = 0;
  848. break;
  849. }
  850. case 2: /* reserved */
  851. {
  852. printk(KERN_WARNING "broken BIOS!!\n");
  853. polarity = 1;
  854. break;
  855. }
  856. case 3: /* low active */
  857. {
  858. polarity = 1;
  859. break;
  860. }
  861. default: /* invalid */
  862. {
  863. printk(KERN_WARNING "broken BIOS!!\n");
  864. polarity = 1;
  865. break;
  866. }
  867. }
  868. return polarity;
  869. }
  870. static int MPBIOS_trigger(int idx)
  871. {
  872. int bus = mp_irqs[idx].mpc_srcbus;
  873. int trigger;
  874. /*
  875. * Determine IRQ trigger mode (edge or level sensitive):
  876. */
  877. switch ((mp_irqs[idx].mpc_irqflag>>2) & 3) {
  878. case 0: /* conforms, ie. bus-type dependent */
  879. {
  880. trigger = test_bit(bus, mp_bus_not_pci)?
  881. default_ISA_trigger(idx):
  882. default_PCI_trigger(idx);
  883. #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
  884. switch (mp_bus_id_to_type[bus]) {
  885. case MP_BUS_ISA: /* ISA pin */
  886. {
  887. /* set before the switch */
  888. break;
  889. }
  890. case MP_BUS_EISA: /* EISA pin */
  891. {
  892. trigger = default_EISA_trigger(idx);
  893. break;
  894. }
  895. case MP_BUS_PCI: /* PCI pin */
  896. {
  897. /* set before the switch */
  898. break;
  899. }
  900. case MP_BUS_MCA: /* MCA pin */
  901. {
  902. trigger = default_MCA_trigger(idx);
  903. break;
  904. }
  905. default:
  906. {
  907. printk(KERN_WARNING "broken BIOS!!\n");
  908. trigger = 1;
  909. break;
  910. }
  911. }
  912. #endif
  913. break;
  914. }
  915. case 1: /* edge */
  916. {
  917. trigger = 0;
  918. break;
  919. }
  920. case 2: /* reserved */
  921. {
  922. printk(KERN_WARNING "broken BIOS!!\n");
  923. trigger = 1;
  924. break;
  925. }
  926. case 3: /* level */
  927. {
  928. trigger = 1;
  929. break;
  930. }
  931. default: /* invalid */
  932. {
  933. printk(KERN_WARNING "broken BIOS!!\n");
  934. trigger = 0;
  935. break;
  936. }
  937. }
  938. return trigger;
  939. }
  940. static inline int irq_polarity(int idx)
  941. {
  942. return MPBIOS_polarity(idx);
  943. }
  944. static inline int irq_trigger(int idx)
  945. {
  946. return MPBIOS_trigger(idx);
  947. }
  948. static int pin_2_irq(int idx, int apic, int pin)
  949. {
  950. int irq, i;
  951. int bus = mp_irqs[idx].mpc_srcbus;
  952. /*
  953. * Debugging check, we are in big trouble if this message pops up!
  954. */
  955. if (mp_irqs[idx].mpc_dstirq != pin)
  956. printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
  957. if (test_bit(bus, mp_bus_not_pci))
  958. irq = mp_irqs[idx].mpc_srcbusirq;
  959. else {
  960. /*
  961. * PCI IRQs are mapped in order
  962. */
  963. i = irq = 0;
  964. while (i < apic)
  965. irq += nr_ioapic_registers[i++];
  966. irq += pin;
  967. /*
  968. * For MPS mode, so far only needed by ES7000 platform
  969. */
  970. if (ioapic_renumber_irq)
  971. irq = ioapic_renumber_irq(apic, irq);
  972. }
  973. /*
  974. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  975. */
  976. if ((pin >= 16) && (pin <= 23)) {
  977. if (pirq_entries[pin-16] != -1) {
  978. if (!pirq_entries[pin-16]) {
  979. apic_printk(APIC_VERBOSE, KERN_DEBUG
  980. "disabling PIRQ%d\n", pin-16);
  981. } else {
  982. irq = pirq_entries[pin-16];
  983. apic_printk(APIC_VERBOSE, KERN_DEBUG
  984. "using PIRQ%d -> IRQ %d\n",
  985. pin-16, irq);
  986. }
  987. }
  988. }
  989. return irq;
  990. }
  991. static inline int IO_APIC_irq_trigger(int irq)
  992. {
  993. int apic, idx, pin;
  994. for (apic = 0; apic < nr_ioapics; apic++) {
  995. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  996. idx = find_irq_entry(apic, pin, mp_INT);
  997. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
  998. return irq_trigger(idx);
  999. }
  1000. }
  1001. /*
  1002. * nonexistent IRQs are edge default
  1003. */
  1004. return 0;
  1005. }
  1006. /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
  1007. static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
  1008. static int __assign_irq_vector(int irq)
  1009. {
  1010. static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
  1011. int vector, offset;
  1012. BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
  1013. if (irq_vector[irq] > 0)
  1014. return irq_vector[irq];
  1015. vector = current_vector;
  1016. offset = current_offset;
  1017. next:
  1018. vector += 8;
  1019. if (vector >= first_system_vector) {
  1020. offset = (offset + 1) % 8;
  1021. vector = FIRST_DEVICE_VECTOR + offset;
  1022. }
  1023. if (vector == current_vector)
  1024. return -ENOSPC;
  1025. if (test_and_set_bit(vector, used_vectors))
  1026. goto next;
  1027. current_vector = vector;
  1028. current_offset = offset;
  1029. irq_vector[irq] = vector;
  1030. return vector;
  1031. }
  1032. static int assign_irq_vector(int irq)
  1033. {
  1034. unsigned long flags;
  1035. int vector;
  1036. spin_lock_irqsave(&vector_lock, flags);
  1037. vector = __assign_irq_vector(irq);
  1038. spin_unlock_irqrestore(&vector_lock, flags);
  1039. return vector;
  1040. }
  1041. static struct irq_chip ioapic_chip;
  1042. #define IOAPIC_AUTO -1
  1043. #define IOAPIC_EDGE 0
  1044. #define IOAPIC_LEVEL 1
  1045. static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
  1046. {
  1047. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1048. trigger == IOAPIC_LEVEL) {
  1049. irq_desc[irq].status |= IRQ_LEVEL;
  1050. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1051. handle_fasteoi_irq, "fasteoi");
  1052. } else {
  1053. irq_desc[irq].status &= ~IRQ_LEVEL;
  1054. set_irq_chip_and_handler_name(irq, &ioapic_chip,
  1055. handle_edge_irq, "edge");
  1056. }
  1057. set_intr_gate(vector, interrupt[irq]);
  1058. }
  1059. static void __init setup_IO_APIC_irqs(void)
  1060. {
  1061. struct IO_APIC_route_entry entry;
  1062. int apic, pin, idx, irq, first_notcon = 1, vector;
  1063. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1064. for (apic = 0; apic < nr_ioapics; apic++) {
  1065. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1066. /*
  1067. * add it to the IO-APIC irq-routing table:
  1068. */
  1069. memset(&entry, 0, sizeof(entry));
  1070. entry.delivery_mode = INT_DELIVERY_MODE;
  1071. entry.dest_mode = INT_DEST_MODE;
  1072. entry.mask = 0; /* enable IRQ */
  1073. entry.dest.logical.logical_dest =
  1074. cpu_mask_to_apicid(TARGET_CPUS);
  1075. idx = find_irq_entry(apic, pin, mp_INT);
  1076. if (idx == -1) {
  1077. if (first_notcon) {
  1078. apic_printk(APIC_VERBOSE, KERN_DEBUG
  1079. " IO-APIC (apicid-pin) %d-%d",
  1080. mp_ioapics[apic].mpc_apicid,
  1081. pin);
  1082. first_notcon = 0;
  1083. } else
  1084. apic_printk(APIC_VERBOSE, ", %d-%d",
  1085. mp_ioapics[apic].mpc_apicid, pin);
  1086. continue;
  1087. }
  1088. if (!first_notcon) {
  1089. apic_printk(APIC_VERBOSE, " not connected.\n");
  1090. first_notcon = 1;
  1091. }
  1092. entry.trigger = irq_trigger(idx);
  1093. entry.polarity = irq_polarity(idx);
  1094. if (irq_trigger(idx)) {
  1095. entry.trigger = 1;
  1096. entry.mask = 1;
  1097. }
  1098. irq = pin_2_irq(idx, apic, pin);
  1099. /*
  1100. * skip adding the timer int on secondary nodes, which causes
  1101. * a small but painful rift in the time-space continuum
  1102. */
  1103. if (multi_timer_check(apic, irq))
  1104. continue;
  1105. else
  1106. add_pin_to_irq(irq, apic, pin);
  1107. if (!apic && !IO_APIC_IRQ(irq))
  1108. continue;
  1109. if (IO_APIC_IRQ(irq)) {
  1110. vector = assign_irq_vector(irq);
  1111. entry.vector = vector;
  1112. ioapic_register_intr(irq, vector, IOAPIC_AUTO);
  1113. if (!apic && (irq < 16))
  1114. disable_8259A_irq(irq);
  1115. }
  1116. ioapic_write_entry(apic, pin, entry);
  1117. }
  1118. }
  1119. if (!first_notcon)
  1120. apic_printk(APIC_VERBOSE, " not connected.\n");
  1121. }
  1122. /*
  1123. * Set up the timer pin, possibly with the 8259A-master behind.
  1124. */
  1125. static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
  1126. int vector)
  1127. {
  1128. struct IO_APIC_route_entry entry;
  1129. memset(&entry, 0, sizeof(entry));
  1130. /*
  1131. * We use logical delivery to get the timer IRQ
  1132. * to the first CPU.
  1133. */
  1134. entry.dest_mode = INT_DEST_MODE;
  1135. entry.mask = 1; /* mask IRQ now */
  1136. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  1137. entry.delivery_mode = INT_DELIVERY_MODE;
  1138. entry.polarity = 0;
  1139. entry.trigger = 0;
  1140. entry.vector = vector;
  1141. /*
  1142. * The timer IRQ doesn't have to know that behind the
  1143. * scene we may have a 8259A-master in AEOI mode ...
  1144. */
  1145. ioapic_register_intr(0, vector, IOAPIC_EDGE);
  1146. /*
  1147. * Add it to the IO-APIC irq-routing table:
  1148. */
  1149. ioapic_write_entry(apic, pin, entry);
  1150. }
  1151. void __init print_IO_APIC(void)
  1152. {
  1153. int apic, i;
  1154. union IO_APIC_reg_00 reg_00;
  1155. union IO_APIC_reg_01 reg_01;
  1156. union IO_APIC_reg_02 reg_02;
  1157. union IO_APIC_reg_03 reg_03;
  1158. unsigned long flags;
  1159. if (apic_verbosity == APIC_QUIET)
  1160. return;
  1161. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1162. for (i = 0; i < nr_ioapics; i++)
  1163. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1164. mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
  1165. /*
  1166. * We are a bit conservative about what we expect. We have to
  1167. * know about every hardware change ASAP.
  1168. */
  1169. printk(KERN_INFO "testing the IO APIC.......................\n");
  1170. for (apic = 0; apic < nr_ioapics; apic++) {
  1171. spin_lock_irqsave(&ioapic_lock, flags);
  1172. reg_00.raw = io_apic_read(apic, 0);
  1173. reg_01.raw = io_apic_read(apic, 1);
  1174. if (reg_01.bits.version >= 0x10)
  1175. reg_02.raw = io_apic_read(apic, 2);
  1176. if (reg_01.bits.version >= 0x20)
  1177. reg_03.raw = io_apic_read(apic, 3);
  1178. spin_unlock_irqrestore(&ioapic_lock, flags);
  1179. printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
  1180. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1181. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1182. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1183. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1184. printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
  1185. printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
  1186. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1187. printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
  1188. /*
  1189. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1190. * but the value of reg_02 is read as the previous read register
  1191. * value, so ignore it if reg_02 == reg_01.
  1192. */
  1193. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1194. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1195. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1196. }
  1197. /*
  1198. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1199. * or reg_03, but the value of reg_0[23] is read as the previous read
  1200. * register value, so ignore it if reg_03 == reg_0[12].
  1201. */
  1202. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1203. reg_03.raw != reg_01.raw) {
  1204. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1205. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1206. }
  1207. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1208. printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
  1209. " Stat Dest Deli Vect: \n");
  1210. for (i = 0; i <= reg_01.bits.entries; i++) {
  1211. struct IO_APIC_route_entry entry;
  1212. entry = ioapic_read_entry(apic, i);
  1213. printk(KERN_DEBUG " %02x %03X %02X ",
  1214. i,
  1215. entry.dest.logical.logical_dest,
  1216. entry.dest.physical.physical_dest
  1217. );
  1218. printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
  1219. entry.mask,
  1220. entry.trigger,
  1221. entry.irr,
  1222. entry.polarity,
  1223. entry.delivery_status,
  1224. entry.dest_mode,
  1225. entry.delivery_mode,
  1226. entry.vector
  1227. );
  1228. }
  1229. }
  1230. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1231. for (i = 0; i < NR_IRQS; i++) {
  1232. struct irq_pin_list *entry = irq_2_pin + i;
  1233. if (entry->pin < 0)
  1234. continue;
  1235. printk(KERN_DEBUG "IRQ%d ", i);
  1236. for (;;) {
  1237. printk("-> %d:%d", entry->apic, entry->pin);
  1238. if (!entry->next)
  1239. break;
  1240. entry = irq_2_pin + entry->next;
  1241. }
  1242. printk("\n");
  1243. }
  1244. printk(KERN_INFO ".................................... done.\n");
  1245. return;
  1246. }
  1247. #if 0
  1248. static void print_APIC_bitfield(int base)
  1249. {
  1250. unsigned int v;
  1251. int i, j;
  1252. if (apic_verbosity == APIC_QUIET)
  1253. return;
  1254. printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
  1255. for (i = 0; i < 8; i++) {
  1256. v = apic_read(base + i*0x10);
  1257. for (j = 0; j < 32; j++) {
  1258. if (v & (1<<j))
  1259. printk("1");
  1260. else
  1261. printk("0");
  1262. }
  1263. printk("\n");
  1264. }
  1265. }
  1266. void /*__init*/ print_local_APIC(void *dummy)
  1267. {
  1268. unsigned int v, ver, maxlvt;
  1269. if (apic_verbosity == APIC_QUIET)
  1270. return;
  1271. printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
  1272. smp_processor_id(), hard_smp_processor_id());
  1273. v = apic_read(APIC_ID);
  1274. printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
  1275. GET_APIC_ID(read_apic_id()));
  1276. v = apic_read(APIC_LVR);
  1277. printk(KERN_INFO "... APIC VERSION: %08x\n", v);
  1278. ver = GET_APIC_VERSION(v);
  1279. maxlvt = lapic_get_maxlvt();
  1280. v = apic_read(APIC_TASKPRI);
  1281. printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1282. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1283. v = apic_read(APIC_ARBPRI);
  1284. printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
  1285. v & APIC_ARBPRI_MASK);
  1286. v = apic_read(APIC_PROCPRI);
  1287. printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
  1288. }
  1289. v = apic_read(APIC_EOI);
  1290. printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
  1291. v = apic_read(APIC_RRR);
  1292. printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
  1293. v = apic_read(APIC_LDR);
  1294. printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
  1295. v = apic_read(APIC_DFR);
  1296. printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
  1297. v = apic_read(APIC_SPIV);
  1298. printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
  1299. printk(KERN_DEBUG "... APIC ISR field:\n");
  1300. print_APIC_bitfield(APIC_ISR);
  1301. printk(KERN_DEBUG "... APIC TMR field:\n");
  1302. print_APIC_bitfield(APIC_TMR);
  1303. printk(KERN_DEBUG "... APIC IRR field:\n");
  1304. print_APIC_bitfield(APIC_IRR);
  1305. if (APIC_INTEGRATED(ver)) { /* !82489DX */
  1306. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  1307. apic_write(APIC_ESR, 0);
  1308. v = apic_read(APIC_ESR);
  1309. printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
  1310. }
  1311. v = apic_read(APIC_ICR);
  1312. printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
  1313. v = apic_read(APIC_ICR2);
  1314. printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
  1315. v = apic_read(APIC_LVTT);
  1316. printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
  1317. if (maxlvt > 3) { /* PC is LVT#4. */
  1318. v = apic_read(APIC_LVTPC);
  1319. printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
  1320. }
  1321. v = apic_read(APIC_LVT0);
  1322. printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
  1323. v = apic_read(APIC_LVT1);
  1324. printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
  1325. if (maxlvt > 2) { /* ERR is LVT#3. */
  1326. v = apic_read(APIC_LVTERR);
  1327. printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
  1328. }
  1329. v = apic_read(APIC_TMICT);
  1330. printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
  1331. v = apic_read(APIC_TMCCT);
  1332. printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
  1333. v = apic_read(APIC_TDCR);
  1334. printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
  1335. printk("\n");
  1336. }
  1337. void print_all_local_APICs(void)
  1338. {
  1339. on_each_cpu(print_local_APIC, NULL, 1, 1);
  1340. }
  1341. void /*__init*/ print_PIC(void)
  1342. {
  1343. unsigned int v;
  1344. unsigned long flags;
  1345. if (apic_verbosity == APIC_QUIET)
  1346. return;
  1347. printk(KERN_DEBUG "\nprinting PIC contents\n");
  1348. spin_lock_irqsave(&i8259A_lock, flags);
  1349. v = inb(0xa1) << 8 | inb(0x21);
  1350. printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
  1351. v = inb(0xa0) << 8 | inb(0x20);
  1352. printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
  1353. outb(0x0b, 0xa0);
  1354. outb(0x0b, 0x20);
  1355. v = inb(0xa0) << 8 | inb(0x20);
  1356. outb(0x0a, 0xa0);
  1357. outb(0x0a, 0x20);
  1358. spin_unlock_irqrestore(&i8259A_lock, flags);
  1359. printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
  1360. v = inb(0x4d1) << 8 | inb(0x4d0);
  1361. printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
  1362. }
  1363. #endif /* 0 */
  1364. static void __init enable_IO_APIC(void)
  1365. {
  1366. union IO_APIC_reg_01 reg_01;
  1367. int i8259_apic, i8259_pin;
  1368. int i, apic;
  1369. unsigned long flags;
  1370. for (i = 0; i < PIN_MAP_SIZE; i++) {
  1371. irq_2_pin[i].pin = -1;
  1372. irq_2_pin[i].next = 0;
  1373. }
  1374. if (!pirqs_enabled)
  1375. for (i = 0; i < MAX_PIRQS; i++)
  1376. pirq_entries[i] = -1;
  1377. /*
  1378. * The number of IO-APIC IRQ registers (== #pins):
  1379. */
  1380. for (apic = 0; apic < nr_ioapics; apic++) {
  1381. spin_lock_irqsave(&ioapic_lock, flags);
  1382. reg_01.raw = io_apic_read(apic, 1);
  1383. spin_unlock_irqrestore(&ioapic_lock, flags);
  1384. nr_ioapic_registers[apic] = reg_01.bits.entries+1;
  1385. }
  1386. for (apic = 0; apic < nr_ioapics; apic++) {
  1387. int pin;
  1388. /* See if any of the pins is in ExtINT mode */
  1389. for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
  1390. struct IO_APIC_route_entry entry;
  1391. entry = ioapic_read_entry(apic, pin);
  1392. /* If the interrupt line is enabled and in ExtInt mode
  1393. * I have found the pin where the i8259 is connected.
  1394. */
  1395. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1396. ioapic_i8259.apic = apic;
  1397. ioapic_i8259.pin = pin;
  1398. goto found_i8259;
  1399. }
  1400. }
  1401. }
  1402. found_i8259:
  1403. /* Look to see what if the MP table has reported the ExtINT */
  1404. /* If we could not find the appropriate pin by looking at the ioapic
  1405. * the i8259 probably is not connected the ioapic but give the
  1406. * mptable a chance anyway.
  1407. */
  1408. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1409. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1410. /* Trust the MP table if nothing is setup in the hardware */
  1411. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1412. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1413. ioapic_i8259.pin = i8259_pin;
  1414. ioapic_i8259.apic = i8259_apic;
  1415. }
  1416. /* Complain if the MP table and the hardware disagree */
  1417. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1418. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1419. {
  1420. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1421. }
  1422. /*
  1423. * Do not trust the IO-APIC being empty at bootup
  1424. */
  1425. clear_IO_APIC();
  1426. }
  1427. /*
  1428. * Not an __init, needed by the reboot code
  1429. */
  1430. void disable_IO_APIC(void)
  1431. {
  1432. /*
  1433. * Clear the IO-APIC before rebooting:
  1434. */
  1435. clear_IO_APIC();
  1436. /*
  1437. * If the i8259 is routed through an IOAPIC
  1438. * Put that IOAPIC in virtual wire mode
  1439. * so legacy interrupts can be delivered.
  1440. */
  1441. if (ioapic_i8259.pin != -1) {
  1442. struct IO_APIC_route_entry entry;
  1443. memset(&entry, 0, sizeof(entry));
  1444. entry.mask = 0; /* Enabled */
  1445. entry.trigger = 0; /* Edge */
  1446. entry.irr = 0;
  1447. entry.polarity = 0; /* High */
  1448. entry.delivery_status = 0;
  1449. entry.dest_mode = 0; /* Physical */
  1450. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1451. entry.vector = 0;
  1452. entry.dest.physical.physical_dest =
  1453. GET_APIC_ID(read_apic_id());
  1454. /*
  1455. * Add it to the IO-APIC irq-routing table:
  1456. */
  1457. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1458. }
  1459. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1460. }
  1461. /*
  1462. * function to set the IO-APIC physical IDs based on the
  1463. * values stored in the MPC table.
  1464. *
  1465. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1466. */
  1467. #ifndef CONFIG_X86_NUMAQ
  1468. static void __init setup_ioapic_ids_from_mpc(void)
  1469. {
  1470. union IO_APIC_reg_00 reg_00;
  1471. physid_mask_t phys_id_present_map;
  1472. int apic;
  1473. int i;
  1474. unsigned char old_id;
  1475. unsigned long flags;
  1476. /*
  1477. * Don't check I/O APIC IDs for xAPIC systems. They have
  1478. * no meaning without the serial APIC bus.
  1479. */
  1480. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1481. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1482. return;
  1483. /*
  1484. * This is broken; anything with a real cpu count has to
  1485. * circumvent this idiocy regardless.
  1486. */
  1487. phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
  1488. /*
  1489. * Set the IOAPIC ID to the value stored in the MPC table.
  1490. */
  1491. for (apic = 0; apic < nr_ioapics; apic++) {
  1492. /* Read the register 0 value */
  1493. spin_lock_irqsave(&ioapic_lock, flags);
  1494. reg_00.raw = io_apic_read(apic, 0);
  1495. spin_unlock_irqrestore(&ioapic_lock, flags);
  1496. old_id = mp_ioapics[apic].mpc_apicid;
  1497. if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
  1498. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1499. apic, mp_ioapics[apic].mpc_apicid);
  1500. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1501. reg_00.bits.ID);
  1502. mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
  1503. }
  1504. /*
  1505. * Sanity check, is the ID really free? Every APIC in a
  1506. * system must have a unique ID or we get lots of nice
  1507. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1508. */
  1509. if (check_apicid_used(phys_id_present_map,
  1510. mp_ioapics[apic].mpc_apicid)) {
  1511. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1512. apic, mp_ioapics[apic].mpc_apicid);
  1513. for (i = 0; i < get_physical_broadcast(); i++)
  1514. if (!physid_isset(i, phys_id_present_map))
  1515. break;
  1516. if (i >= get_physical_broadcast())
  1517. panic("Max APIC ID exceeded!\n");
  1518. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1519. i);
  1520. physid_set(i, phys_id_present_map);
  1521. mp_ioapics[apic].mpc_apicid = i;
  1522. } else {
  1523. physid_mask_t tmp;
  1524. tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
  1525. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1526. "phys_id_present_map\n",
  1527. mp_ioapics[apic].mpc_apicid);
  1528. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1529. }
  1530. /*
  1531. * We need to adjust the IRQ routing table
  1532. * if the ID changed.
  1533. */
  1534. if (old_id != mp_ioapics[apic].mpc_apicid)
  1535. for (i = 0; i < mp_irq_entries; i++)
  1536. if (mp_irqs[i].mpc_dstapic == old_id)
  1537. mp_irqs[i].mpc_dstapic
  1538. = mp_ioapics[apic].mpc_apicid;
  1539. /*
  1540. * Read the right value from the MPC table and
  1541. * write it into the ID register.
  1542. */
  1543. apic_printk(APIC_VERBOSE, KERN_INFO
  1544. "...changing IO-APIC physical APIC ID to %d ...",
  1545. mp_ioapics[apic].mpc_apicid);
  1546. reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
  1547. spin_lock_irqsave(&ioapic_lock, flags);
  1548. io_apic_write(apic, 0, reg_00.raw);
  1549. spin_unlock_irqrestore(&ioapic_lock, flags);
  1550. /*
  1551. * Sanity check
  1552. */
  1553. spin_lock_irqsave(&ioapic_lock, flags);
  1554. reg_00.raw = io_apic_read(apic, 0);
  1555. spin_unlock_irqrestore(&ioapic_lock, flags);
  1556. if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
  1557. printk("could not set ID!\n");
  1558. else
  1559. apic_printk(APIC_VERBOSE, " ok.\n");
  1560. }
  1561. }
  1562. #else
  1563. static void __init setup_ioapic_ids_from_mpc(void) { }
  1564. #endif
  1565. int no_timer_check __initdata;
  1566. static int __init notimercheck(char *s)
  1567. {
  1568. no_timer_check = 1;
  1569. return 1;
  1570. }
  1571. __setup("no_timer_check", notimercheck);
  1572. /*
  1573. * There is a nasty bug in some older SMP boards, their mptable lies
  1574. * about the timer IRQ. We do the following to work around the situation:
  1575. *
  1576. * - timer IRQ defaults to IO-APIC IRQ
  1577. * - if this function detects that timer IRQs are defunct, then we fall
  1578. * back to ISA timer IRQs
  1579. */
  1580. static int __init timer_irq_works(void)
  1581. {
  1582. unsigned long t1 = jiffies;
  1583. unsigned long flags;
  1584. if (no_timer_check)
  1585. return 1;
  1586. local_save_flags(flags);
  1587. local_irq_enable();
  1588. /* Let ten ticks pass... */
  1589. mdelay((10 * 1000) / HZ);
  1590. local_irq_restore(flags);
  1591. /*
  1592. * Expect a few ticks at least, to be sure some possible
  1593. * glue logic does not lock up after one or two first
  1594. * ticks in a non-ExtINT mode. Also the local APIC
  1595. * might have cached one ExtINT interrupt. Finally, at
  1596. * least one tick may be lost due to delays.
  1597. */
  1598. if (time_after(jiffies, t1 + 4))
  1599. return 1;
  1600. return 0;
  1601. }
  1602. /*
  1603. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1604. * number of pending IRQ events unhandled. These cases are very rare,
  1605. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1606. * better to do it this way as thus we do not have to be aware of
  1607. * 'pending' interrupts in the IRQ path, except at this point.
  1608. */
  1609. /*
  1610. * Edge triggered needs to resend any interrupt
  1611. * that was delayed but this is now handled in the device
  1612. * independent code.
  1613. */
  1614. /*
  1615. * Startup quirk:
  1616. *
  1617. * Starting up a edge-triggered IO-APIC interrupt is
  1618. * nasty - we need to make sure that we get the edge.
  1619. * If it is already asserted for some reason, we need
  1620. * return 1 to indicate that is was pending.
  1621. *
  1622. * This is not complete - we should be able to fake
  1623. * an edge even if it isn't on the 8259A...
  1624. *
  1625. * (We do this for level-triggered IRQs too - it cannot hurt.)
  1626. */
  1627. static unsigned int startup_ioapic_irq(unsigned int irq)
  1628. {
  1629. int was_pending = 0;
  1630. unsigned long flags;
  1631. spin_lock_irqsave(&ioapic_lock, flags);
  1632. if (irq < 16) {
  1633. disable_8259A_irq(irq);
  1634. if (i8259A_irq_pending(irq))
  1635. was_pending = 1;
  1636. }
  1637. __unmask_IO_APIC_irq(irq);
  1638. spin_unlock_irqrestore(&ioapic_lock, flags);
  1639. return was_pending;
  1640. }
  1641. static void ack_ioapic_irq(unsigned int irq)
  1642. {
  1643. move_native_irq(irq);
  1644. ack_APIC_irq();
  1645. }
  1646. static void ack_ioapic_quirk_irq(unsigned int irq)
  1647. {
  1648. unsigned long v;
  1649. int i;
  1650. move_native_irq(irq);
  1651. /*
  1652. * It appears there is an erratum which affects at least version 0x11
  1653. * of I/O APIC (that's the 82093AA and cores integrated into various
  1654. * chipsets). Under certain conditions a level-triggered interrupt is
  1655. * erroneously delivered as edge-triggered one but the respective IRR
  1656. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1657. * message but it will never arrive and further interrupts are blocked
  1658. * from the source. The exact reason is so far unknown, but the
  1659. * phenomenon was observed when two consecutive interrupt requests
  1660. * from a given source get delivered to the same CPU and the source is
  1661. * temporarily disabled in between.
  1662. *
  1663. * A workaround is to simulate an EOI message manually. We achieve it
  1664. * by setting the trigger mode to edge and then to level when the edge
  1665. * trigger mode gets detected in the TMR of a local APIC for a
  1666. * level-triggered interrupt. We mask the source for the time of the
  1667. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1668. * The idea is from Manfred Spraul. --macro
  1669. */
  1670. i = irq_vector[irq];
  1671. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1672. ack_APIC_irq();
  1673. if (!(v & (1 << (i & 0x1f)))) {
  1674. atomic_inc(&irq_mis_count);
  1675. spin_lock(&ioapic_lock);
  1676. __mask_and_edge_IO_APIC_irq(irq);
  1677. __unmask_and_level_IO_APIC_irq(irq);
  1678. spin_unlock(&ioapic_lock);
  1679. }
  1680. }
  1681. static int ioapic_retrigger_irq(unsigned int irq)
  1682. {
  1683. send_IPI_self(irq_vector[irq]);
  1684. return 1;
  1685. }
  1686. static struct irq_chip ioapic_chip __read_mostly = {
  1687. .name = "IO-APIC",
  1688. .startup = startup_ioapic_irq,
  1689. .mask = mask_IO_APIC_irq,
  1690. .unmask = unmask_IO_APIC_irq,
  1691. .ack = ack_ioapic_irq,
  1692. .eoi = ack_ioapic_quirk_irq,
  1693. #ifdef CONFIG_SMP
  1694. .set_affinity = set_ioapic_affinity_irq,
  1695. #endif
  1696. .retrigger = ioapic_retrigger_irq,
  1697. };
  1698. static inline void init_IO_APIC_traps(void)
  1699. {
  1700. int irq;
  1701. /*
  1702. * NOTE! The local APIC isn't very good at handling
  1703. * multiple interrupts at the same interrupt level.
  1704. * As the interrupt level is determined by taking the
  1705. * vector number and shifting that right by 4, we
  1706. * want to spread these out a bit so that they don't
  1707. * all fall in the same interrupt level.
  1708. *
  1709. * Also, we've got to be careful not to trash gate
  1710. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  1711. */
  1712. for (irq = 0; irq < NR_IRQS ; irq++) {
  1713. if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
  1714. /*
  1715. * Hmm.. We don't have an entry for this,
  1716. * so default to an old-fashioned 8259
  1717. * interrupt if we can..
  1718. */
  1719. if (irq < 16)
  1720. make_8259A_irq(irq);
  1721. else
  1722. /* Strange. Oh, well.. */
  1723. irq_desc[irq].chip = &no_irq_chip;
  1724. }
  1725. }
  1726. }
  1727. /*
  1728. * The local APIC irq-chip implementation:
  1729. */
  1730. static void ack_apic(unsigned int irq)
  1731. {
  1732. ack_APIC_irq();
  1733. }
  1734. static void mask_lapic_irq(unsigned int irq)
  1735. {
  1736. unsigned long v;
  1737. v = apic_read(APIC_LVT0);
  1738. apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
  1739. }
  1740. static void unmask_lapic_irq(unsigned int irq)
  1741. {
  1742. unsigned long v;
  1743. v = apic_read(APIC_LVT0);
  1744. apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1745. }
  1746. static struct irq_chip lapic_chip __read_mostly = {
  1747. .name = "local-APIC",
  1748. .mask = mask_lapic_irq,
  1749. .unmask = unmask_lapic_irq,
  1750. .eoi = ack_apic,
  1751. };
  1752. static void __init setup_nmi(void)
  1753. {
  1754. /*
  1755. * Dirty trick to enable the NMI watchdog ...
  1756. * We put the 8259A master into AEOI mode and
  1757. * unmask on all local APICs LVT0 as NMI.
  1758. *
  1759. * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
  1760. * is from Maciej W. Rozycki - so we do not have to EOI from
  1761. * the NMI handler or the timer interrupt.
  1762. */
  1763. apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
  1764. enable_NMI_through_LVT0();
  1765. apic_printk(APIC_VERBOSE, " done.\n");
  1766. }
  1767. /*
  1768. * This looks a bit hackish but it's about the only one way of sending
  1769. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1770. * not support the ExtINT mode, unfortunately. We need to send these
  1771. * cycles as some i82489DX-based boards have glue logic that keeps the
  1772. * 8259A interrupt line asserted until INTA. --macro
  1773. */
  1774. static inline void __init unlock_ExtINT_logic(void)
  1775. {
  1776. int apic, pin, i;
  1777. struct IO_APIC_route_entry entry0, entry1;
  1778. unsigned char save_control, save_freq_select;
  1779. pin = find_isa_irq_pin(8, mp_INT);
  1780. if (pin == -1) {
  1781. WARN_ON_ONCE(1);
  1782. return;
  1783. }
  1784. apic = find_isa_irq_apic(8, mp_INT);
  1785. if (apic == -1) {
  1786. WARN_ON_ONCE(1);
  1787. return;
  1788. }
  1789. entry0 = ioapic_read_entry(apic, pin);
  1790. clear_IO_APIC_pin(apic, pin);
  1791. memset(&entry1, 0, sizeof(entry1));
  1792. entry1.dest_mode = 0; /* physical delivery */
  1793. entry1.mask = 0; /* unmask IRQ now */
  1794. entry1.dest.physical.physical_dest = hard_smp_processor_id();
  1795. entry1.delivery_mode = dest_ExtINT;
  1796. entry1.polarity = entry0.polarity;
  1797. entry1.trigger = 0;
  1798. entry1.vector = 0;
  1799. ioapic_write_entry(apic, pin, entry1);
  1800. save_control = CMOS_READ(RTC_CONTROL);
  1801. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1802. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1803. RTC_FREQ_SELECT);
  1804. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1805. i = 100;
  1806. while (i-- > 0) {
  1807. mdelay(10);
  1808. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1809. i -= 10;
  1810. }
  1811. CMOS_WRITE(save_control, RTC_CONTROL);
  1812. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1813. clear_IO_APIC_pin(apic, pin);
  1814. ioapic_write_entry(apic, pin, entry0);
  1815. }
  1816. /*
  1817. * This code may look a bit paranoid, but it's supposed to cooperate with
  1818. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1819. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1820. * fanatically on his truly buggy board.
  1821. */
  1822. static inline void __init check_timer(void)
  1823. {
  1824. int apic1, pin1, apic2, pin2;
  1825. int no_pin1 = 0;
  1826. int vector;
  1827. unsigned int ver;
  1828. unsigned long flags;
  1829. local_irq_save(flags);
  1830. ver = apic_read(APIC_LVR);
  1831. ver = GET_APIC_VERSION(ver);
  1832. /*
  1833. * get/set the timer IRQ vector:
  1834. */
  1835. disable_8259A_irq(0);
  1836. vector = assign_irq_vector(0);
  1837. set_intr_gate(vector, interrupt[0]);
  1838. /*
  1839. * As IRQ0 is to be enabled in the 8259A, the virtual
  1840. * wire has to be disabled in the local APIC. Also
  1841. * timer interrupts need to be acknowledged manually in
  1842. * the 8259A for the i82489DX when using the NMI
  1843. * watchdog as that APIC treats NMIs as level-triggered.
  1844. * The AEOI mode will finish them in the 8259A
  1845. * automatically.
  1846. */
  1847. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1848. init_8259A(1);
  1849. timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
  1850. pin1 = find_isa_irq_pin(0, mp_INT);
  1851. apic1 = find_isa_irq_apic(0, mp_INT);
  1852. pin2 = ioapic_i8259.pin;
  1853. apic2 = ioapic_i8259.apic;
  1854. printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1855. vector, apic1, pin1, apic2, pin2);
  1856. /*
  1857. * Some BIOS writers are clueless and report the ExtINTA
  1858. * I/O APIC input from the cascaded 8259A as the timer
  1859. * interrupt input. So just in case, if only one pin
  1860. * was found above, try it both directly and through the
  1861. * 8259A.
  1862. */
  1863. if (pin1 == -1) {
  1864. pin1 = pin2;
  1865. apic1 = apic2;
  1866. no_pin1 = 1;
  1867. } else if (pin2 == -1) {
  1868. pin2 = pin1;
  1869. apic2 = apic1;
  1870. }
  1871. if (pin1 != -1) {
  1872. /*
  1873. * Ok, does IRQ0 through the IOAPIC work?
  1874. */
  1875. if (no_pin1) {
  1876. add_pin_to_irq(0, apic1, pin1);
  1877. setup_timer_IRQ0_pin(apic1, pin1, vector);
  1878. }
  1879. unmask_IO_APIC_irq(0);
  1880. if (timer_irq_works()) {
  1881. if (nmi_watchdog == NMI_IO_APIC) {
  1882. setup_nmi();
  1883. enable_8259A_irq(0);
  1884. }
  1885. if (disable_timer_pin_1 > 0)
  1886. clear_IO_APIC_pin(0, pin1);
  1887. goto out;
  1888. }
  1889. clear_IO_APIC_pin(apic1, pin1);
  1890. if (!no_pin1)
  1891. printk(KERN_ERR "..MP-BIOS bug: "
  1892. "8254 timer not connected to IO-APIC\n");
  1893. printk(KERN_INFO "...trying to set up timer (IRQ0) "
  1894. "through the 8259A ... ");
  1895. printk("\n..... (found pin %d) ...", pin2);
  1896. /*
  1897. * legacy devices should be connected to IO APIC #0
  1898. */
  1899. replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
  1900. setup_timer_IRQ0_pin(apic2, pin2, vector);
  1901. unmask_IO_APIC_irq(0);
  1902. enable_8259A_irq(0);
  1903. if (timer_irq_works()) {
  1904. printk("works.\n");
  1905. timer_through_8259 = 1;
  1906. if (nmi_watchdog == NMI_IO_APIC) {
  1907. disable_8259A_irq(0);
  1908. setup_nmi();
  1909. enable_8259A_irq(0);
  1910. }
  1911. goto out;
  1912. }
  1913. /*
  1914. * Cleanup, just in case ...
  1915. */
  1916. disable_8259A_irq(0);
  1917. clear_IO_APIC_pin(apic2, pin2);
  1918. printk(" failed.\n");
  1919. }
  1920. if (nmi_watchdog == NMI_IO_APIC) {
  1921. printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
  1922. nmi_watchdog = NMI_NONE;
  1923. }
  1924. timer_ack = 0;
  1925. printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
  1926. set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
  1927. "fasteoi");
  1928. apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
  1929. enable_8259A_irq(0);
  1930. if (timer_irq_works()) {
  1931. printk(" works.\n");
  1932. goto out;
  1933. }
  1934. disable_8259A_irq(0);
  1935. apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
  1936. printk(" failed.\n");
  1937. printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
  1938. init_8259A(0);
  1939. make_8259A_irq(0);
  1940. apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
  1941. unlock_ExtINT_logic();
  1942. if (timer_irq_works()) {
  1943. printk(" works.\n");
  1944. goto out;
  1945. }
  1946. printk(" failed :(.\n");
  1947. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1948. "report. Then try booting with the 'noapic' option");
  1949. out:
  1950. local_irq_restore(flags);
  1951. }
  1952. /*
  1953. *
  1954. * IRQ's that are handled by the PIC in the MPS IOAPIC case.
  1955. * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
  1956. * Linux doesn't really care, as it's not actually used
  1957. * for any interrupt handling anyway.
  1958. */
  1959. #define PIC_IRQS (1 << PIC_CASCADE_IR)
  1960. void __init setup_IO_APIC(void)
  1961. {
  1962. int i;
  1963. /* Reserve all the system vectors. */
  1964. for (i = first_system_vector; i < NR_VECTORS; i++)
  1965. set_bit(i, used_vectors);
  1966. enable_IO_APIC();
  1967. if (acpi_ioapic)
  1968. io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
  1969. else
  1970. io_apic_irqs = ~PIC_IRQS;
  1971. printk("ENABLING IO-APIC IRQs\n");
  1972. /*
  1973. * Set up IO-APIC IRQ routing.
  1974. */
  1975. if (!acpi_ioapic)
  1976. setup_ioapic_ids_from_mpc();
  1977. sync_Arb_IDs();
  1978. setup_IO_APIC_irqs();
  1979. init_IO_APIC_traps();
  1980. check_timer();
  1981. if (!acpi_ioapic)
  1982. print_IO_APIC();
  1983. }
  1984. /*
  1985. * Called after all the initialization is done. If we didnt find any
  1986. * APIC bugs then we can allow the modify fast path
  1987. */
  1988. static int __init io_apic_bug_finalize(void)
  1989. {
  1990. if (sis_apic_bug == -1)
  1991. sis_apic_bug = 0;
  1992. return 0;
  1993. }
  1994. late_initcall(io_apic_bug_finalize);
  1995. struct sysfs_ioapic_data {
  1996. struct sys_device dev;
  1997. struct IO_APIC_route_entry entry[0];
  1998. };
  1999. static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
  2000. static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
  2001. {
  2002. struct IO_APIC_route_entry *entry;
  2003. struct sysfs_ioapic_data *data;
  2004. int i;
  2005. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2006. entry = data->entry;
  2007. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2008. entry[i] = ioapic_read_entry(dev->id, i);
  2009. return 0;
  2010. }
  2011. static int ioapic_resume(struct sys_device *dev)
  2012. {
  2013. struct IO_APIC_route_entry *entry;
  2014. struct sysfs_ioapic_data *data;
  2015. unsigned long flags;
  2016. union IO_APIC_reg_00 reg_00;
  2017. int i;
  2018. data = container_of(dev, struct sysfs_ioapic_data, dev);
  2019. entry = data->entry;
  2020. spin_lock_irqsave(&ioapic_lock, flags);
  2021. reg_00.raw = io_apic_read(dev->id, 0);
  2022. if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
  2023. reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
  2024. io_apic_write(dev->id, 0, reg_00.raw);
  2025. }
  2026. spin_unlock_irqrestore(&ioapic_lock, flags);
  2027. for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
  2028. ioapic_write_entry(dev->id, i, entry[i]);
  2029. return 0;
  2030. }
  2031. static struct sysdev_class ioapic_sysdev_class = {
  2032. .name = "ioapic",
  2033. .suspend = ioapic_suspend,
  2034. .resume = ioapic_resume,
  2035. };
  2036. static int __init ioapic_init_sysfs(void)
  2037. {
  2038. struct sys_device *dev;
  2039. int i, size, error = 0;
  2040. error = sysdev_class_register(&ioapic_sysdev_class);
  2041. if (error)
  2042. return error;
  2043. for (i = 0; i < nr_ioapics; i++) {
  2044. size = sizeof(struct sys_device) + nr_ioapic_registers[i]
  2045. * sizeof(struct IO_APIC_route_entry);
  2046. mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
  2047. if (!mp_ioapic_data[i]) {
  2048. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2049. continue;
  2050. }
  2051. dev = &mp_ioapic_data[i]->dev;
  2052. dev->id = i;
  2053. dev->cls = &ioapic_sysdev_class;
  2054. error = sysdev_register(dev);
  2055. if (error) {
  2056. kfree(mp_ioapic_data[i]);
  2057. mp_ioapic_data[i] = NULL;
  2058. printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
  2059. continue;
  2060. }
  2061. }
  2062. return 0;
  2063. }
  2064. device_initcall(ioapic_init_sysfs);
  2065. /*
  2066. * Dynamic irq allocate and deallocation
  2067. */
  2068. int create_irq(void)
  2069. {
  2070. /* Allocate an unused irq */
  2071. int irq, new, vector = 0;
  2072. unsigned long flags;
  2073. irq = -ENOSPC;
  2074. spin_lock_irqsave(&vector_lock, flags);
  2075. for (new = (NR_IRQS - 1); new >= 0; new--) {
  2076. if (platform_legacy_irq(new))
  2077. continue;
  2078. if (irq_vector[new] != 0)
  2079. continue;
  2080. vector = __assign_irq_vector(new);
  2081. if (likely(vector > 0))
  2082. irq = new;
  2083. break;
  2084. }
  2085. spin_unlock_irqrestore(&vector_lock, flags);
  2086. if (irq >= 0) {
  2087. set_intr_gate(vector, interrupt[irq]);
  2088. dynamic_irq_init(irq);
  2089. }
  2090. return irq;
  2091. }
  2092. void destroy_irq(unsigned int irq)
  2093. {
  2094. unsigned long flags;
  2095. dynamic_irq_cleanup(irq);
  2096. spin_lock_irqsave(&vector_lock, flags);
  2097. clear_bit(irq_vector[irq], used_vectors);
  2098. irq_vector[irq] = 0;
  2099. spin_unlock_irqrestore(&vector_lock, flags);
  2100. }
  2101. /*
  2102. * MSI message composition
  2103. */
  2104. #ifdef CONFIG_PCI_MSI
  2105. static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
  2106. {
  2107. int vector;
  2108. unsigned dest;
  2109. vector = assign_irq_vector(irq);
  2110. if (vector >= 0) {
  2111. dest = cpu_mask_to_apicid(TARGET_CPUS);
  2112. msg->address_hi = MSI_ADDR_BASE_HI;
  2113. msg->address_lo =
  2114. MSI_ADDR_BASE_LO |
  2115. ((INT_DEST_MODE == 0) ?
  2116. MSI_ADDR_DEST_MODE_PHYSICAL:
  2117. MSI_ADDR_DEST_MODE_LOGICAL) |
  2118. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2119. MSI_ADDR_REDIRECTION_CPU:
  2120. MSI_ADDR_REDIRECTION_LOWPRI) |
  2121. MSI_ADDR_DEST_ID(dest);
  2122. msg->data =
  2123. MSI_DATA_TRIGGER_EDGE |
  2124. MSI_DATA_LEVEL_ASSERT |
  2125. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2126. MSI_DATA_DELIVERY_FIXED:
  2127. MSI_DATA_DELIVERY_LOWPRI) |
  2128. MSI_DATA_VECTOR(vector);
  2129. }
  2130. return vector;
  2131. }
  2132. #ifdef CONFIG_SMP
  2133. static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
  2134. {
  2135. struct msi_msg msg;
  2136. unsigned int dest;
  2137. cpumask_t tmp;
  2138. int vector;
  2139. cpus_and(tmp, mask, cpu_online_map);
  2140. if (cpus_empty(tmp))
  2141. tmp = TARGET_CPUS;
  2142. vector = assign_irq_vector(irq);
  2143. if (vector < 0)
  2144. return;
  2145. dest = cpu_mask_to_apicid(mask);
  2146. read_msi_msg(irq, &msg);
  2147. msg.data &= ~MSI_DATA_VECTOR_MASK;
  2148. msg.data |= MSI_DATA_VECTOR(vector);
  2149. msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
  2150. msg.address_lo |= MSI_ADDR_DEST_ID(dest);
  2151. write_msi_msg(irq, &msg);
  2152. irq_desc[irq].affinity = mask;
  2153. }
  2154. #endif /* CONFIG_SMP */
  2155. /*
  2156. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  2157. * which implement the MSI or MSI-X Capability Structure.
  2158. */
  2159. static struct irq_chip msi_chip = {
  2160. .name = "PCI-MSI",
  2161. .unmask = unmask_msi_irq,
  2162. .mask = mask_msi_irq,
  2163. .ack = ack_ioapic_irq,
  2164. #ifdef CONFIG_SMP
  2165. .set_affinity = set_msi_irq_affinity,
  2166. #endif
  2167. .retrigger = ioapic_retrigger_irq,
  2168. };
  2169. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  2170. {
  2171. struct msi_msg msg;
  2172. int irq, ret;
  2173. irq = create_irq();
  2174. if (irq < 0)
  2175. return irq;
  2176. ret = msi_compose_msg(dev, irq, &msg);
  2177. if (ret < 0) {
  2178. destroy_irq(irq);
  2179. return ret;
  2180. }
  2181. set_irq_msi(irq, desc);
  2182. write_msi_msg(irq, &msg);
  2183. set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
  2184. "edge");
  2185. return 0;
  2186. }
  2187. void arch_teardown_msi_irq(unsigned int irq)
  2188. {
  2189. destroy_irq(irq);
  2190. }
  2191. #endif /* CONFIG_PCI_MSI */
  2192. /*
  2193. * Hypertransport interrupt support
  2194. */
  2195. #ifdef CONFIG_HT_IRQ
  2196. #ifdef CONFIG_SMP
  2197. static void target_ht_irq(unsigned int irq, unsigned int dest)
  2198. {
  2199. struct ht_irq_msg msg;
  2200. fetch_ht_irq_msg(irq, &msg);
  2201. msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
  2202. msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
  2203. msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
  2204. msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
  2205. write_ht_irq_msg(irq, &msg);
  2206. }
  2207. static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
  2208. {
  2209. unsigned int dest;
  2210. cpumask_t tmp;
  2211. cpus_and(tmp, mask, cpu_online_map);
  2212. if (cpus_empty(tmp))
  2213. tmp = TARGET_CPUS;
  2214. cpus_and(mask, tmp, CPU_MASK_ALL);
  2215. dest = cpu_mask_to_apicid(mask);
  2216. target_ht_irq(irq, dest);
  2217. irq_desc[irq].affinity = mask;
  2218. }
  2219. #endif
  2220. static struct irq_chip ht_irq_chip = {
  2221. .name = "PCI-HT",
  2222. .mask = mask_ht_irq,
  2223. .unmask = unmask_ht_irq,
  2224. .ack = ack_ioapic_irq,
  2225. #ifdef CONFIG_SMP
  2226. .set_affinity = set_ht_irq_affinity,
  2227. #endif
  2228. .retrigger = ioapic_retrigger_irq,
  2229. };
  2230. int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
  2231. {
  2232. int vector;
  2233. vector = assign_irq_vector(irq);
  2234. if (vector >= 0) {
  2235. struct ht_irq_msg msg;
  2236. unsigned dest;
  2237. cpumask_t tmp;
  2238. cpus_clear(tmp);
  2239. cpu_set(vector >> 8, tmp);
  2240. dest = cpu_mask_to_apicid(tmp);
  2241. msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
  2242. msg.address_lo =
  2243. HT_IRQ_LOW_BASE |
  2244. HT_IRQ_LOW_DEST_ID(dest) |
  2245. HT_IRQ_LOW_VECTOR(vector) |
  2246. ((INT_DEST_MODE == 0) ?
  2247. HT_IRQ_LOW_DM_PHYSICAL :
  2248. HT_IRQ_LOW_DM_LOGICAL) |
  2249. HT_IRQ_LOW_RQEOI_EDGE |
  2250. ((INT_DELIVERY_MODE != dest_LowestPrio) ?
  2251. HT_IRQ_LOW_MT_FIXED :
  2252. HT_IRQ_LOW_MT_ARBITRATED) |
  2253. HT_IRQ_LOW_IRQ_MASKED;
  2254. write_ht_irq_msg(irq, &msg);
  2255. set_irq_chip_and_handler_name(irq, &ht_irq_chip,
  2256. handle_edge_irq, "edge");
  2257. }
  2258. return vector;
  2259. }
  2260. #endif /* CONFIG_HT_IRQ */
  2261. /* --------------------------------------------------------------------------
  2262. ACPI-based IOAPIC Configuration
  2263. -------------------------------------------------------------------------- */
  2264. #ifdef CONFIG_ACPI
  2265. int __init io_apic_get_unique_id(int ioapic, int apic_id)
  2266. {
  2267. union IO_APIC_reg_00 reg_00;
  2268. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2269. physid_mask_t tmp;
  2270. unsigned long flags;
  2271. int i = 0;
  2272. /*
  2273. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2274. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2275. * supports up to 16 on one shared APIC bus.
  2276. *
  2277. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2278. * advantage of new APIC bus architecture.
  2279. */
  2280. if (physids_empty(apic_id_map))
  2281. apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
  2282. spin_lock_irqsave(&ioapic_lock, flags);
  2283. reg_00.raw = io_apic_read(ioapic, 0);
  2284. spin_unlock_irqrestore(&ioapic_lock, flags);
  2285. if (apic_id >= get_physical_broadcast()) {
  2286. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2287. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2288. apic_id = reg_00.bits.ID;
  2289. }
  2290. /*
  2291. * Every APIC in a system must have a unique ID or we get lots of nice
  2292. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2293. */
  2294. if (check_apicid_used(apic_id_map, apic_id)) {
  2295. for (i = 0; i < get_physical_broadcast(); i++) {
  2296. if (!check_apicid_used(apic_id_map, i))
  2297. break;
  2298. }
  2299. if (i == get_physical_broadcast())
  2300. panic("Max apic_id exceeded!\n");
  2301. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2302. "trying %d\n", ioapic, apic_id, i);
  2303. apic_id = i;
  2304. }
  2305. tmp = apicid_to_cpu_present(apic_id);
  2306. physids_or(apic_id_map, apic_id_map, tmp);
  2307. if (reg_00.bits.ID != apic_id) {
  2308. reg_00.bits.ID = apic_id;
  2309. spin_lock_irqsave(&ioapic_lock, flags);
  2310. io_apic_write(ioapic, 0, reg_00.raw);
  2311. reg_00.raw = io_apic_read(ioapic, 0);
  2312. spin_unlock_irqrestore(&ioapic_lock, flags);
  2313. /* Sanity check */
  2314. if (reg_00.bits.ID != apic_id) {
  2315. printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
  2316. return -1;
  2317. }
  2318. }
  2319. apic_printk(APIC_VERBOSE, KERN_INFO
  2320. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2321. return apic_id;
  2322. }
  2323. int __init io_apic_get_version(int ioapic)
  2324. {
  2325. union IO_APIC_reg_01 reg_01;
  2326. unsigned long flags;
  2327. spin_lock_irqsave(&ioapic_lock, flags);
  2328. reg_01.raw = io_apic_read(ioapic, 1);
  2329. spin_unlock_irqrestore(&ioapic_lock, flags);
  2330. return reg_01.bits.version;
  2331. }
  2332. int __init io_apic_get_redir_entries(int ioapic)
  2333. {
  2334. union IO_APIC_reg_01 reg_01;
  2335. unsigned long flags;
  2336. spin_lock_irqsave(&ioapic_lock, flags);
  2337. reg_01.raw = io_apic_read(ioapic, 1);
  2338. spin_unlock_irqrestore(&ioapic_lock, flags);
  2339. return reg_01.bits.entries;
  2340. }
  2341. int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
  2342. {
  2343. struct IO_APIC_route_entry entry;
  2344. if (!IO_APIC_IRQ(irq)) {
  2345. printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
  2346. ioapic);
  2347. return -EINVAL;
  2348. }
  2349. /*
  2350. * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
  2351. * Note that we mask (disable) IRQs now -- these get enabled when the
  2352. * corresponding device driver registers for this IRQ.
  2353. */
  2354. memset(&entry, 0, sizeof(entry));
  2355. entry.delivery_mode = INT_DELIVERY_MODE;
  2356. entry.dest_mode = INT_DEST_MODE;
  2357. entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
  2358. entry.trigger = edge_level;
  2359. entry.polarity = active_high_low;
  2360. entry.mask = 1;
  2361. /*
  2362. * IRQs < 16 are already in the irq_2_pin[] map
  2363. */
  2364. if (irq >= 16)
  2365. add_pin_to_irq(irq, ioapic, pin);
  2366. entry.vector = assign_irq_vector(irq);
  2367. apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
  2368. "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
  2369. mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
  2370. edge_level, active_high_low);
  2371. ioapic_register_intr(irq, entry.vector, edge_level);
  2372. if (!ioapic && (irq < 16))
  2373. disable_8259A_irq(irq);
  2374. ioapic_write_entry(ioapic, pin, entry);
  2375. return 0;
  2376. }
  2377. int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
  2378. {
  2379. int i;
  2380. if (skip_ioapic_setup)
  2381. return -1;
  2382. for (i = 0; i < mp_irq_entries; i++)
  2383. if (mp_irqs[i].mpc_irqtype == mp_INT &&
  2384. mp_irqs[i].mpc_srcbusirq == bus_irq)
  2385. break;
  2386. if (i >= mp_irq_entries)
  2387. return -1;
  2388. *trigger = irq_trigger(i);
  2389. *polarity = irq_polarity(i);
  2390. return 0;
  2391. }
  2392. #endif /* CONFIG_ACPI */
  2393. static int __init parse_disable_timer_pin_1(char *arg)
  2394. {
  2395. disable_timer_pin_1 = 1;
  2396. return 0;
  2397. }
  2398. early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
  2399. static int __init parse_enable_timer_pin_1(char *arg)
  2400. {
  2401. disable_timer_pin_1 = -1;
  2402. return 0;
  2403. }
  2404. early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
  2405. static int __init parse_noapic(char *arg)
  2406. {
  2407. /* disable IO-APIC */
  2408. disable_ioapic_setup();
  2409. return 0;
  2410. }
  2411. early_param("noapic", parse_noapic);