io_apic.c 70 KB

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