io_apic_32.c 71 KB

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