io_apic_32.c 69 KB

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