io_apic.c 71 KB

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