io_apic.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  1. /*
  2. * Intel IO-APIC support for multi-Pentium hosts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000, 2009 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/pci.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/compiler.h>
  30. #include <linux/acpi.h>
  31. #include <linux/module.h>
  32. #include <linux/syscore_ops.h>
  33. #include <linux/irqdomain.h>
  34. #include <linux/freezer.h>
  35. #include <linux/kthread.h>
  36. #include <linux/jiffies.h> /* time_after() */
  37. #include <linux/slab.h>
  38. #include <linux/bootmem.h>
  39. #include <asm/idle.h>
  40. #include <asm/io.h>
  41. #include <asm/smp.h>
  42. #include <asm/cpu.h>
  43. #include <asm/desc.h>
  44. #include <asm/proto.h>
  45. #include <asm/acpi.h>
  46. #include <asm/dma.h>
  47. #include <asm/timer.h>
  48. #include <asm/i8259.h>
  49. #include <asm/setup.h>
  50. #include <asm/irq_remapping.h>
  51. #include <asm/hw_irq.h>
  52. #include <asm/apic.h>
  53. #define for_each_ioapic(idx) \
  54. for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
  55. #define for_each_ioapic_reverse(idx) \
  56. for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
  57. #define for_each_pin(idx, pin) \
  58. for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
  59. #define for_each_ioapic_pin(idx, pin) \
  60. for_each_ioapic((idx)) \
  61. for_each_pin((idx), (pin))
  62. #define for_each_irq_pin(entry, head) \
  63. list_for_each_entry(entry, &head, list)
  64. /*
  65. * Is the SiS APIC rmw bug present ?
  66. * -1 = don't know, 0 = no, 1 = yes
  67. */
  68. int sis_apic_bug = -1;
  69. static DEFINE_RAW_SPINLOCK(ioapic_lock);
  70. static DEFINE_MUTEX(ioapic_mutex);
  71. static unsigned int ioapic_dynirq_base;
  72. static int ioapic_initialized;
  73. struct mp_pin_info {
  74. int trigger;
  75. int polarity;
  76. int node;
  77. int set;
  78. u32 count;
  79. };
  80. static struct ioapic {
  81. /*
  82. * # of IRQ routing registers
  83. */
  84. int nr_registers;
  85. /*
  86. * Saved state during suspend/resume, or while enabling intr-remap.
  87. */
  88. struct IO_APIC_route_entry *saved_registers;
  89. /* I/O APIC config */
  90. struct mpc_ioapic mp_config;
  91. /* IO APIC gsi routing info */
  92. struct mp_ioapic_gsi gsi_config;
  93. struct ioapic_domain_cfg irqdomain_cfg;
  94. struct irq_domain *irqdomain;
  95. struct mp_pin_info *pin_info;
  96. struct resource *iomem_res;
  97. } ioapics[MAX_IO_APICS];
  98. #define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver
  99. int mpc_ioapic_id(int ioapic_idx)
  100. {
  101. return ioapics[ioapic_idx].mp_config.apicid;
  102. }
  103. unsigned int mpc_ioapic_addr(int ioapic_idx)
  104. {
  105. return ioapics[ioapic_idx].mp_config.apicaddr;
  106. }
  107. struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
  108. {
  109. return &ioapics[ioapic_idx].gsi_config;
  110. }
  111. static inline int mp_ioapic_pin_count(int ioapic)
  112. {
  113. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  114. return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
  115. }
  116. u32 mp_pin_to_gsi(int ioapic, int pin)
  117. {
  118. return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
  119. }
  120. /*
  121. * Initialize all legacy IRQs and all pins on the first IOAPIC
  122. * if we have legacy interrupt controller. Kernel boot option "pirq="
  123. * may rely on non-legacy pins on the first IOAPIC.
  124. */
  125. static inline int mp_init_irq_at_boot(int ioapic, int irq)
  126. {
  127. if (!nr_legacy_irqs())
  128. return 0;
  129. return ioapic == 0 || (irq >= 0 && irq < nr_legacy_irqs());
  130. }
  131. static inline struct mp_pin_info *mp_pin_info(int ioapic_idx, int pin)
  132. {
  133. return ioapics[ioapic_idx].pin_info + pin;
  134. }
  135. static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic)
  136. {
  137. return ioapics[ioapic].irqdomain;
  138. }
  139. int nr_ioapics;
  140. /* The one past the highest gsi number used */
  141. u32 gsi_top;
  142. /* MP IRQ source entries */
  143. struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
  144. /* # of MP IRQ source entries */
  145. int mp_irq_entries;
  146. #ifdef CONFIG_EISA
  147. int mp_bus_id_to_type[MAX_MP_BUSSES];
  148. #endif
  149. DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
  150. int skip_ioapic_setup;
  151. /**
  152. * disable_ioapic_support() - disables ioapic support at runtime
  153. */
  154. void disable_ioapic_support(void)
  155. {
  156. #ifdef CONFIG_PCI
  157. noioapicquirk = 1;
  158. noioapicreroute = -1;
  159. #endif
  160. skip_ioapic_setup = 1;
  161. }
  162. static int __init parse_noapic(char *str)
  163. {
  164. /* disable IO-APIC */
  165. disable_ioapic_support();
  166. return 0;
  167. }
  168. early_param("noapic", parse_noapic);
  169. /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
  170. void mp_save_irq(struct mpc_intsrc *m)
  171. {
  172. int i;
  173. apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
  174. " IRQ %02x, APIC ID %x, APIC INT %02x\n",
  175. m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
  176. m->srcbusirq, m->dstapic, m->dstirq);
  177. for (i = 0; i < mp_irq_entries; i++) {
  178. if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
  179. return;
  180. }
  181. memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
  182. if (++mp_irq_entries == MAX_IRQ_SOURCES)
  183. panic("Max # of irq sources exceeded!!\n");
  184. }
  185. struct irq_pin_list {
  186. struct list_head list;
  187. int apic, pin;
  188. };
  189. static struct irq_pin_list *alloc_irq_pin_list(int node)
  190. {
  191. return kzalloc_node(sizeof(struct irq_pin_list), GFP_KERNEL, node);
  192. }
  193. static void alloc_ioapic_saved_registers(int idx)
  194. {
  195. size_t size;
  196. if (ioapics[idx].saved_registers)
  197. return;
  198. size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers;
  199. ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL);
  200. if (!ioapics[idx].saved_registers)
  201. pr_err("IOAPIC %d: suspend/resume impossible!\n", idx);
  202. }
  203. static void free_ioapic_saved_registers(int idx)
  204. {
  205. kfree(ioapics[idx].saved_registers);
  206. ioapics[idx].saved_registers = NULL;
  207. }
  208. int __init arch_early_irq_init(void)
  209. {
  210. struct irq_cfg *cfg;
  211. int i, node = cpu_to_node(0);
  212. if (!nr_legacy_irqs())
  213. io_apic_irqs = ~0UL;
  214. for_each_ioapic(i)
  215. alloc_ioapic_saved_registers(i);
  216. /*
  217. * For legacy IRQ's, start with assigning irq0 to irq15 to
  218. * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
  219. */
  220. for (i = 0; i < nr_legacy_irqs(); i++) {
  221. cfg = alloc_irq_and_cfg_at(i, node);
  222. cfg->vector = IRQ0_VECTOR + i;
  223. cpumask_setall(cfg->domain);
  224. }
  225. return 0;
  226. }
  227. struct io_apic {
  228. unsigned int index;
  229. unsigned int unused[3];
  230. unsigned int data;
  231. unsigned int unused2[11];
  232. unsigned int eoi;
  233. };
  234. static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
  235. {
  236. return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
  237. + (mpc_ioapic_addr(idx) & ~PAGE_MASK);
  238. }
  239. void io_apic_eoi(unsigned int apic, unsigned int vector)
  240. {
  241. struct io_apic __iomem *io_apic = io_apic_base(apic);
  242. writel(vector, &io_apic->eoi);
  243. }
  244. unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
  245. {
  246. struct io_apic __iomem *io_apic = io_apic_base(apic);
  247. writel(reg, &io_apic->index);
  248. return readl(&io_apic->data);
  249. }
  250. void native_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
  251. {
  252. struct io_apic __iomem *io_apic = io_apic_base(apic);
  253. writel(reg, &io_apic->index);
  254. writel(value, &io_apic->data);
  255. }
  256. /*
  257. * Re-write a value: to be used for read-modify-write
  258. * cycles where the read already set up the index register.
  259. *
  260. * Older SiS APIC requires we rewrite the index register
  261. */
  262. void native_io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
  263. {
  264. struct io_apic __iomem *io_apic = io_apic_base(apic);
  265. if (sis_apic_bug)
  266. writel(reg, &io_apic->index);
  267. writel(value, &io_apic->data);
  268. }
  269. union entry_union {
  270. struct { u32 w1, w2; };
  271. struct IO_APIC_route_entry entry;
  272. };
  273. static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
  274. {
  275. union entry_union eu;
  276. eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
  277. eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
  278. return eu.entry;
  279. }
  280. static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
  281. {
  282. union entry_union eu;
  283. unsigned long flags;
  284. raw_spin_lock_irqsave(&ioapic_lock, flags);
  285. eu.entry = __ioapic_read_entry(apic, pin);
  286. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  287. return eu.entry;
  288. }
  289. /*
  290. * When we write a new IO APIC routing entry, we need to write the high
  291. * word first! If the mask bit in the low word is clear, we will enable
  292. * the interrupt, and we need to make sure the entry is fully populated
  293. * before that happens.
  294. */
  295. static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  296. {
  297. union entry_union eu = {{0, 0}};
  298. eu.entry = e;
  299. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  300. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  301. }
  302. static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
  303. {
  304. unsigned long flags;
  305. raw_spin_lock_irqsave(&ioapic_lock, flags);
  306. __ioapic_write_entry(apic, pin, e);
  307. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  308. }
  309. /*
  310. * When we mask an IO APIC routing entry, we need to write the low
  311. * word first, in order to set the mask bit before we change the
  312. * high bits!
  313. */
  314. static void ioapic_mask_entry(int apic, int pin)
  315. {
  316. unsigned long flags;
  317. union entry_union eu = { .entry.mask = 1 };
  318. raw_spin_lock_irqsave(&ioapic_lock, flags);
  319. io_apic_write(apic, 0x10 + 2*pin, eu.w1);
  320. io_apic_write(apic, 0x11 + 2*pin, eu.w2);
  321. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  322. }
  323. /*
  324. * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
  325. * shared ISA-space IRQs, so we have to support them. We are super
  326. * fast in the common case, and fast for shared ISA-space IRQs.
  327. */
  328. static int __add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
  329. {
  330. struct irq_pin_list *entry;
  331. /* don't allow duplicates */
  332. for_each_irq_pin(entry, cfg->irq_2_pin)
  333. if (entry->apic == apic && entry->pin == pin)
  334. return 0;
  335. entry = alloc_irq_pin_list(node);
  336. if (!entry) {
  337. pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
  338. node, apic, pin);
  339. return -ENOMEM;
  340. }
  341. entry->apic = apic;
  342. entry->pin = pin;
  343. list_add_tail(&entry->list, &cfg->irq_2_pin);
  344. return 0;
  345. }
  346. static void __remove_pin_from_irq(struct irq_cfg *cfg, int apic, int pin)
  347. {
  348. struct irq_pin_list *tmp, *entry;
  349. list_for_each_entry_safe(entry, tmp, &cfg->irq_2_pin, list)
  350. if (entry->apic == apic && entry->pin == pin) {
  351. list_del(&entry->list);
  352. kfree(entry);
  353. return;
  354. }
  355. }
  356. static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
  357. {
  358. if (__add_pin_to_irq_node(cfg, node, apic, pin))
  359. panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
  360. }
  361. /*
  362. * Reroute an IRQ to a different pin.
  363. */
  364. static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
  365. int oldapic, int oldpin,
  366. int newapic, int newpin)
  367. {
  368. struct irq_pin_list *entry;
  369. for_each_irq_pin(entry, cfg->irq_2_pin) {
  370. if (entry->apic == oldapic && entry->pin == oldpin) {
  371. entry->apic = newapic;
  372. entry->pin = newpin;
  373. /* every one is different, right? */
  374. return;
  375. }
  376. }
  377. /* old apic/pin didn't exist, so just add new ones */
  378. add_pin_to_irq_node(cfg, node, newapic, newpin);
  379. }
  380. static void __io_apic_modify_irq(struct irq_pin_list *entry,
  381. int mask_and, int mask_or,
  382. void (*final)(struct irq_pin_list *entry))
  383. {
  384. unsigned int reg, pin;
  385. pin = entry->pin;
  386. reg = io_apic_read(entry->apic, 0x10 + pin * 2);
  387. reg &= mask_and;
  388. reg |= mask_or;
  389. io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
  390. if (final)
  391. final(entry);
  392. }
  393. static void io_apic_modify_irq(struct irq_cfg *cfg,
  394. int mask_and, int mask_or,
  395. void (*final)(struct irq_pin_list *entry))
  396. {
  397. struct irq_pin_list *entry;
  398. for_each_irq_pin(entry, cfg->irq_2_pin)
  399. __io_apic_modify_irq(entry, mask_and, mask_or, final);
  400. }
  401. static void io_apic_sync(struct irq_pin_list *entry)
  402. {
  403. /*
  404. * Synchronize the IO-APIC and the CPU by doing
  405. * a dummy read from the IO-APIC
  406. */
  407. struct io_apic __iomem *io_apic;
  408. io_apic = io_apic_base(entry->apic);
  409. readl(&io_apic->data);
  410. }
  411. static void mask_ioapic(struct irq_cfg *cfg)
  412. {
  413. unsigned long flags;
  414. raw_spin_lock_irqsave(&ioapic_lock, flags);
  415. io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
  416. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  417. }
  418. static void mask_ioapic_irq(struct irq_data *data)
  419. {
  420. mask_ioapic(data->chip_data);
  421. }
  422. static void __unmask_ioapic(struct irq_cfg *cfg)
  423. {
  424. io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
  425. }
  426. static void unmask_ioapic(struct irq_cfg *cfg)
  427. {
  428. unsigned long flags;
  429. raw_spin_lock_irqsave(&ioapic_lock, flags);
  430. __unmask_ioapic(cfg);
  431. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  432. }
  433. static void unmask_ioapic_irq(struct irq_data *data)
  434. {
  435. unmask_ioapic(data->chip_data);
  436. }
  437. /*
  438. * IO-APIC versions below 0x20 don't support EOI register.
  439. * For the record, here is the information about various versions:
  440. * 0Xh 82489DX
  441. * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
  442. * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
  443. * 30h-FFh Reserved
  444. *
  445. * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
  446. * version as 0x2. This is an error with documentation and these ICH chips
  447. * use io-apic's of version 0x20.
  448. *
  449. * For IO-APIC's with EOI register, we use that to do an explicit EOI.
  450. * Otherwise, we simulate the EOI message manually by changing the trigger
  451. * mode to edge and then back to level, with RTE being masked during this.
  452. */
  453. void native_eoi_ioapic_pin(int apic, int pin, int vector)
  454. {
  455. if (mpc_ioapic_ver(apic) >= 0x20) {
  456. io_apic_eoi(apic, vector);
  457. } else {
  458. struct IO_APIC_route_entry entry, entry1;
  459. entry = entry1 = __ioapic_read_entry(apic, pin);
  460. /*
  461. * Mask the entry and change the trigger mode to edge.
  462. */
  463. entry1.mask = 1;
  464. entry1.trigger = IOAPIC_EDGE;
  465. __ioapic_write_entry(apic, pin, entry1);
  466. /*
  467. * Restore the previous level triggered entry.
  468. */
  469. __ioapic_write_entry(apic, pin, entry);
  470. }
  471. }
  472. void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
  473. {
  474. struct irq_pin_list *entry;
  475. unsigned long flags;
  476. raw_spin_lock_irqsave(&ioapic_lock, flags);
  477. for_each_irq_pin(entry, cfg->irq_2_pin)
  478. x86_io_apic_ops.eoi_ioapic_pin(entry->apic, entry->pin,
  479. cfg->vector);
  480. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  481. }
  482. static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
  483. {
  484. struct IO_APIC_route_entry entry;
  485. /* Check delivery_mode to be sure we're not clearing an SMI pin */
  486. entry = ioapic_read_entry(apic, pin);
  487. if (entry.delivery_mode == dest_SMI)
  488. return;
  489. /*
  490. * Make sure the entry is masked and re-read the contents to check
  491. * if it is a level triggered pin and if the remote-IRR is set.
  492. */
  493. if (!entry.mask) {
  494. entry.mask = 1;
  495. ioapic_write_entry(apic, pin, entry);
  496. entry = ioapic_read_entry(apic, pin);
  497. }
  498. if (entry.irr) {
  499. unsigned long flags;
  500. /*
  501. * Make sure the trigger mode is set to level. Explicit EOI
  502. * doesn't clear the remote-IRR if the trigger mode is not
  503. * set to level.
  504. */
  505. if (!entry.trigger) {
  506. entry.trigger = IOAPIC_LEVEL;
  507. ioapic_write_entry(apic, pin, entry);
  508. }
  509. raw_spin_lock_irqsave(&ioapic_lock, flags);
  510. x86_io_apic_ops.eoi_ioapic_pin(apic, pin, entry.vector);
  511. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  512. }
  513. /*
  514. * Clear the rest of the bits in the IO-APIC RTE except for the mask
  515. * bit.
  516. */
  517. ioapic_mask_entry(apic, pin);
  518. entry = ioapic_read_entry(apic, pin);
  519. if (entry.irr)
  520. pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
  521. mpc_ioapic_id(apic), pin);
  522. }
  523. static void clear_IO_APIC (void)
  524. {
  525. int apic, pin;
  526. for_each_ioapic_pin(apic, pin)
  527. clear_IO_APIC_pin(apic, pin);
  528. }
  529. #ifdef CONFIG_X86_32
  530. /*
  531. * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
  532. * specific CPU-side IRQs.
  533. */
  534. #define MAX_PIRQS 8
  535. static int pirq_entries[MAX_PIRQS] = {
  536. [0 ... MAX_PIRQS - 1] = -1
  537. };
  538. static int __init ioapic_pirq_setup(char *str)
  539. {
  540. int i, max;
  541. int ints[MAX_PIRQS+1];
  542. get_options(str, ARRAY_SIZE(ints), ints);
  543. apic_printk(APIC_VERBOSE, KERN_INFO
  544. "PIRQ redirection, working around broken MP-BIOS.\n");
  545. max = MAX_PIRQS;
  546. if (ints[0] < MAX_PIRQS)
  547. max = ints[0];
  548. for (i = 0; i < max; i++) {
  549. apic_printk(APIC_VERBOSE, KERN_DEBUG
  550. "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
  551. /*
  552. * PIRQs are mapped upside down, usually.
  553. */
  554. pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
  555. }
  556. return 1;
  557. }
  558. __setup("pirq=", ioapic_pirq_setup);
  559. #endif /* CONFIG_X86_32 */
  560. /*
  561. * Saves all the IO-APIC RTE's
  562. */
  563. int save_ioapic_entries(void)
  564. {
  565. int apic, pin;
  566. int err = 0;
  567. for_each_ioapic(apic) {
  568. if (!ioapics[apic].saved_registers) {
  569. err = -ENOMEM;
  570. continue;
  571. }
  572. for_each_pin(apic, pin)
  573. ioapics[apic].saved_registers[pin] =
  574. ioapic_read_entry(apic, pin);
  575. }
  576. return err;
  577. }
  578. /*
  579. * Mask all IO APIC entries.
  580. */
  581. void mask_ioapic_entries(void)
  582. {
  583. int apic, pin;
  584. for_each_ioapic(apic) {
  585. if (!ioapics[apic].saved_registers)
  586. continue;
  587. for_each_pin(apic, pin) {
  588. struct IO_APIC_route_entry entry;
  589. entry = ioapics[apic].saved_registers[pin];
  590. if (!entry.mask) {
  591. entry.mask = 1;
  592. ioapic_write_entry(apic, pin, entry);
  593. }
  594. }
  595. }
  596. }
  597. /*
  598. * Restore IO APIC entries which was saved in the ioapic structure.
  599. */
  600. int restore_ioapic_entries(void)
  601. {
  602. int apic, pin;
  603. for_each_ioapic(apic) {
  604. if (!ioapics[apic].saved_registers)
  605. continue;
  606. for_each_pin(apic, pin)
  607. ioapic_write_entry(apic, pin,
  608. ioapics[apic].saved_registers[pin]);
  609. }
  610. return 0;
  611. }
  612. /*
  613. * Find the IRQ entry number of a certain pin.
  614. */
  615. static int find_irq_entry(int ioapic_idx, int pin, int type)
  616. {
  617. int i;
  618. for (i = 0; i < mp_irq_entries; i++)
  619. if (mp_irqs[i].irqtype == type &&
  620. (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
  621. mp_irqs[i].dstapic == MP_APIC_ALL) &&
  622. mp_irqs[i].dstirq == pin)
  623. return i;
  624. return -1;
  625. }
  626. /*
  627. * Find the pin to which IRQ[irq] (ISA) is connected
  628. */
  629. static int __init find_isa_irq_pin(int irq, int type)
  630. {
  631. int i;
  632. for (i = 0; i < mp_irq_entries; i++) {
  633. int lbus = mp_irqs[i].srcbus;
  634. if (test_bit(lbus, mp_bus_not_pci) &&
  635. (mp_irqs[i].irqtype == type) &&
  636. (mp_irqs[i].srcbusirq == irq))
  637. return mp_irqs[i].dstirq;
  638. }
  639. return -1;
  640. }
  641. static int __init find_isa_irq_apic(int irq, int type)
  642. {
  643. int i;
  644. for (i = 0; i < mp_irq_entries; i++) {
  645. int lbus = mp_irqs[i].srcbus;
  646. if (test_bit(lbus, mp_bus_not_pci) &&
  647. (mp_irqs[i].irqtype == type) &&
  648. (mp_irqs[i].srcbusirq == irq))
  649. break;
  650. }
  651. if (i < mp_irq_entries) {
  652. int ioapic_idx;
  653. for_each_ioapic(ioapic_idx)
  654. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
  655. return ioapic_idx;
  656. }
  657. return -1;
  658. }
  659. #ifdef CONFIG_EISA
  660. /*
  661. * EISA Edge/Level control register, ELCR
  662. */
  663. static int EISA_ELCR(unsigned int irq)
  664. {
  665. if (irq < nr_legacy_irqs()) {
  666. unsigned int port = 0x4d0 + (irq >> 3);
  667. return (inb(port) >> (irq & 7)) & 1;
  668. }
  669. apic_printk(APIC_VERBOSE, KERN_INFO
  670. "Broken MPtable reports ISA irq %d\n", irq);
  671. return 0;
  672. }
  673. #endif
  674. /* ISA interrupts are always polarity zero edge triggered,
  675. * when listed as conforming in the MP table. */
  676. #define default_ISA_trigger(idx) (0)
  677. #define default_ISA_polarity(idx) (0)
  678. /* EISA interrupts are always polarity zero and can be edge or level
  679. * trigger depending on the ELCR value. If an interrupt is listed as
  680. * EISA conforming in the MP table, that means its trigger type must
  681. * be read in from the ELCR */
  682. #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
  683. #define default_EISA_polarity(idx) default_ISA_polarity(idx)
  684. /* PCI interrupts are always polarity one level triggered,
  685. * when listed as conforming in the MP table. */
  686. #define default_PCI_trigger(idx) (1)
  687. #define default_PCI_polarity(idx) (1)
  688. static int irq_polarity(int idx)
  689. {
  690. int bus = mp_irqs[idx].srcbus;
  691. int polarity;
  692. /*
  693. * Determine IRQ line polarity (high active or low active):
  694. */
  695. switch (mp_irqs[idx].irqflag & 3)
  696. {
  697. case 0: /* conforms, ie. bus-type dependent polarity */
  698. if (test_bit(bus, mp_bus_not_pci))
  699. polarity = default_ISA_polarity(idx);
  700. else
  701. polarity = default_PCI_polarity(idx);
  702. break;
  703. case 1: /* high active */
  704. {
  705. polarity = 0;
  706. break;
  707. }
  708. case 2: /* reserved */
  709. {
  710. pr_warn("broken BIOS!!\n");
  711. polarity = 1;
  712. break;
  713. }
  714. case 3: /* low active */
  715. {
  716. polarity = 1;
  717. break;
  718. }
  719. default: /* invalid */
  720. {
  721. pr_warn("broken BIOS!!\n");
  722. polarity = 1;
  723. break;
  724. }
  725. }
  726. return polarity;
  727. }
  728. static int irq_trigger(int idx)
  729. {
  730. int bus = mp_irqs[idx].srcbus;
  731. int trigger;
  732. /*
  733. * Determine IRQ trigger mode (edge or level sensitive):
  734. */
  735. switch ((mp_irqs[idx].irqflag>>2) & 3)
  736. {
  737. case 0: /* conforms, ie. bus-type dependent */
  738. if (test_bit(bus, mp_bus_not_pci))
  739. trigger = default_ISA_trigger(idx);
  740. else
  741. trigger = default_PCI_trigger(idx);
  742. #ifdef CONFIG_EISA
  743. switch (mp_bus_id_to_type[bus]) {
  744. case MP_BUS_ISA: /* ISA pin */
  745. {
  746. /* set before the switch */
  747. break;
  748. }
  749. case MP_BUS_EISA: /* EISA pin */
  750. {
  751. trigger = default_EISA_trigger(idx);
  752. break;
  753. }
  754. case MP_BUS_PCI: /* PCI pin */
  755. {
  756. /* set before the switch */
  757. break;
  758. }
  759. default:
  760. {
  761. pr_warn("broken BIOS!!\n");
  762. trigger = 1;
  763. break;
  764. }
  765. }
  766. #endif
  767. break;
  768. case 1: /* edge */
  769. {
  770. trigger = 0;
  771. break;
  772. }
  773. case 2: /* reserved */
  774. {
  775. pr_warn("broken BIOS!!\n");
  776. trigger = 1;
  777. break;
  778. }
  779. case 3: /* level */
  780. {
  781. trigger = 1;
  782. break;
  783. }
  784. default: /* invalid */
  785. {
  786. pr_warn("broken BIOS!!\n");
  787. trigger = 0;
  788. break;
  789. }
  790. }
  791. return trigger;
  792. }
  793. static int alloc_irq_from_domain(struct irq_domain *domain, u32 gsi, int pin)
  794. {
  795. int irq = -1;
  796. int ioapic = (int)(long)domain->host_data;
  797. int type = ioapics[ioapic].irqdomain_cfg.type;
  798. switch (type) {
  799. case IOAPIC_DOMAIN_LEGACY:
  800. /*
  801. * Dynamically allocate IRQ number for non-ISA IRQs in the first 16
  802. * GSIs on some weird platforms.
  803. */
  804. if (gsi < nr_legacy_irqs())
  805. irq = irq_create_mapping(domain, pin);
  806. else if (irq_create_strict_mappings(domain, gsi, pin, 1) == 0)
  807. irq = gsi;
  808. break;
  809. case IOAPIC_DOMAIN_STRICT:
  810. if (irq_create_strict_mappings(domain, gsi, pin, 1) == 0)
  811. irq = gsi;
  812. break;
  813. case IOAPIC_DOMAIN_DYNAMIC:
  814. irq = irq_create_mapping(domain, pin);
  815. break;
  816. default:
  817. WARN(1, "ioapic: unknown irqdomain type %d\n", type);
  818. break;
  819. }
  820. return irq > 0 ? irq : -1;
  821. }
  822. static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin,
  823. unsigned int flags)
  824. {
  825. int irq;
  826. struct irq_domain *domain = mp_ioapic_irqdomain(ioapic);
  827. struct mp_pin_info *info = mp_pin_info(ioapic, pin);
  828. if (!domain)
  829. return -1;
  830. mutex_lock(&ioapic_mutex);
  831. /*
  832. * Don't use irqdomain to manage ISA IRQs because there may be
  833. * multiple IOAPIC pins sharing the same ISA IRQ number and
  834. * irqdomain only supports 1:1 mapping between IOAPIC pin and
  835. * IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are used
  836. * for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H).
  837. * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are
  838. * available, and some BIOSes may use MP Interrupt Source records
  839. * to override IRQ numbers for PIRQs instead of reprogramming
  840. * the interrupt routing logic. Thus there may be multiple pins
  841. * sharing the same legacy IRQ number when ACPI is disabled.
  842. */
  843. if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) {
  844. irq = mp_irqs[idx].srcbusirq;
  845. if (flags & IOAPIC_MAP_ALLOC) {
  846. if (info->count == 0 &&
  847. mp_irqdomain_map(domain, irq, pin) != 0)
  848. irq = -1;
  849. /* special handling for timer IRQ0 */
  850. if (irq == 0)
  851. info->count++;
  852. }
  853. } else {
  854. irq = irq_find_mapping(domain, pin);
  855. if (irq <= 0 && (flags & IOAPIC_MAP_ALLOC))
  856. irq = alloc_irq_from_domain(domain, gsi, pin);
  857. }
  858. if (flags & IOAPIC_MAP_ALLOC) {
  859. /* special handling for legacy IRQs */
  860. if (irq < nr_legacy_irqs() && info->count == 1 &&
  861. mp_irqdomain_map(domain, irq, pin) != 0)
  862. irq = -1;
  863. if (irq > 0)
  864. info->count++;
  865. else if (info->count == 0)
  866. info->set = 0;
  867. }
  868. mutex_unlock(&ioapic_mutex);
  869. return irq > 0 ? irq : -1;
  870. }
  871. static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags)
  872. {
  873. u32 gsi = mp_pin_to_gsi(ioapic, pin);
  874. /*
  875. * Debugging check, we are in big trouble if this message pops up!
  876. */
  877. if (mp_irqs[idx].dstirq != pin)
  878. pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
  879. #ifdef CONFIG_X86_32
  880. /*
  881. * PCI IRQ command line redirection. Yes, limits are hardcoded.
  882. */
  883. if ((pin >= 16) && (pin <= 23)) {
  884. if (pirq_entries[pin-16] != -1) {
  885. if (!pirq_entries[pin-16]) {
  886. apic_printk(APIC_VERBOSE, KERN_DEBUG
  887. "disabling PIRQ%d\n", pin-16);
  888. } else {
  889. int irq = pirq_entries[pin-16];
  890. apic_printk(APIC_VERBOSE, KERN_DEBUG
  891. "using PIRQ%d -> IRQ %d\n",
  892. pin-16, irq);
  893. return irq;
  894. }
  895. }
  896. }
  897. #endif
  898. return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags);
  899. }
  900. int mp_map_gsi_to_irq(u32 gsi, unsigned int flags)
  901. {
  902. int ioapic, pin, idx;
  903. ioapic = mp_find_ioapic(gsi);
  904. if (ioapic < 0)
  905. return -1;
  906. pin = mp_find_ioapic_pin(ioapic, gsi);
  907. idx = find_irq_entry(ioapic, pin, mp_INT);
  908. if ((flags & IOAPIC_MAP_CHECK) && idx < 0)
  909. return -1;
  910. return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags);
  911. }
  912. void mp_unmap_irq(int irq)
  913. {
  914. struct irq_data *data = irq_get_irq_data(irq);
  915. struct mp_pin_info *info;
  916. int ioapic, pin;
  917. if (!data || !data->domain)
  918. return;
  919. ioapic = (int)(long)data->domain->host_data;
  920. pin = (int)data->hwirq;
  921. info = mp_pin_info(ioapic, pin);
  922. mutex_lock(&ioapic_mutex);
  923. if (--info->count == 0) {
  924. info->set = 0;
  925. if (irq < nr_legacy_irqs() &&
  926. ioapics[ioapic].irqdomain_cfg.type == IOAPIC_DOMAIN_LEGACY)
  927. mp_irqdomain_unmap(data->domain, irq);
  928. else
  929. irq_dispose_mapping(irq);
  930. }
  931. mutex_unlock(&ioapic_mutex);
  932. }
  933. /*
  934. * Find a specific PCI IRQ entry.
  935. * Not an __init, possibly needed by modules
  936. */
  937. int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
  938. {
  939. int irq, i, best_ioapic = -1, best_idx = -1;
  940. apic_printk(APIC_DEBUG,
  941. "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
  942. bus, slot, pin);
  943. if (test_bit(bus, mp_bus_not_pci)) {
  944. apic_printk(APIC_VERBOSE,
  945. "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
  946. return -1;
  947. }
  948. for (i = 0; i < mp_irq_entries; i++) {
  949. int lbus = mp_irqs[i].srcbus;
  950. int ioapic_idx, found = 0;
  951. if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
  952. slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
  953. continue;
  954. for_each_ioapic(ioapic_idx)
  955. if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
  956. mp_irqs[i].dstapic == MP_APIC_ALL) {
  957. found = 1;
  958. break;
  959. }
  960. if (!found)
  961. continue;
  962. /* Skip ISA IRQs */
  963. irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0);
  964. if (irq > 0 && !IO_APIC_IRQ(irq))
  965. continue;
  966. if (pin == (mp_irqs[i].srcbusirq & 3)) {
  967. best_idx = i;
  968. best_ioapic = ioapic_idx;
  969. goto out;
  970. }
  971. /*
  972. * Use the first all-but-pin matching entry as a
  973. * best-guess fuzzy result for broken mptables.
  974. */
  975. if (best_idx < 0) {
  976. best_idx = i;
  977. best_ioapic = ioapic_idx;
  978. }
  979. }
  980. if (best_idx < 0)
  981. return -1;
  982. out:
  983. return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq,
  984. IOAPIC_MAP_ALLOC);
  985. }
  986. EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
  987. static struct irq_chip ioapic_chip;
  988. #ifdef CONFIG_X86_32
  989. static inline int IO_APIC_irq_trigger(int irq)
  990. {
  991. int apic, idx, pin;
  992. for_each_ioapic_pin(apic, pin) {
  993. idx = find_irq_entry(apic, pin, mp_INT);
  994. if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin, 0)))
  995. return irq_trigger(idx);
  996. }
  997. /*
  998. * nonexistent IRQs are edge default
  999. */
  1000. return 0;
  1001. }
  1002. #else
  1003. static inline int IO_APIC_irq_trigger(int irq)
  1004. {
  1005. return 1;
  1006. }
  1007. #endif
  1008. static void ioapic_register_intr(unsigned int irq, struct irq_cfg *cfg,
  1009. unsigned long trigger)
  1010. {
  1011. struct irq_chip *chip = &ioapic_chip;
  1012. irq_flow_handler_t hdl;
  1013. bool fasteoi;
  1014. if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
  1015. trigger == IOAPIC_LEVEL) {
  1016. irq_set_status_flags(irq, IRQ_LEVEL);
  1017. fasteoi = true;
  1018. } else {
  1019. irq_clear_status_flags(irq, IRQ_LEVEL);
  1020. fasteoi = false;
  1021. }
  1022. if (setup_remapped_irq(irq, cfg, chip))
  1023. fasteoi = trigger != 0;
  1024. hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
  1025. irq_set_chip_and_handler_name(irq, chip, hdl,
  1026. fasteoi ? "fasteoi" : "edge");
  1027. }
  1028. int native_setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
  1029. unsigned int destination, int vector,
  1030. struct io_apic_irq_attr *attr)
  1031. {
  1032. memset(entry, 0, sizeof(*entry));
  1033. entry->delivery_mode = apic->irq_delivery_mode;
  1034. entry->dest_mode = apic->irq_dest_mode;
  1035. entry->dest = destination;
  1036. entry->vector = vector;
  1037. entry->mask = 0; /* enable IRQ */
  1038. entry->trigger = attr->trigger;
  1039. entry->polarity = attr->polarity;
  1040. /*
  1041. * Mask level triggered irqs.
  1042. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  1043. */
  1044. if (attr->trigger)
  1045. entry->mask = 1;
  1046. return 0;
  1047. }
  1048. static void setup_ioapic_irq(unsigned int irq, struct irq_cfg *cfg,
  1049. struct io_apic_irq_attr *attr)
  1050. {
  1051. struct IO_APIC_route_entry entry;
  1052. unsigned int dest;
  1053. if (!IO_APIC_IRQ(irq))
  1054. return;
  1055. if (assign_irq_vector(irq, cfg, apic->target_cpus()))
  1056. return;
  1057. if (apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus(),
  1058. &dest)) {
  1059. pr_warn("Failed to obtain apicid for ioapic %d, pin %d\n",
  1060. mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
  1061. clear_irq_vector(irq, cfg);
  1062. return;
  1063. }
  1064. apic_printk(APIC_VERBOSE,KERN_DEBUG
  1065. "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
  1066. "IRQ %d Mode:%i Active:%i Dest:%d)\n",
  1067. attr->ioapic, mpc_ioapic_id(attr->ioapic), attr->ioapic_pin,
  1068. cfg->vector, irq, attr->trigger, attr->polarity, dest);
  1069. if (x86_io_apic_ops.setup_entry(irq, &entry, dest, cfg->vector, attr)) {
  1070. pr_warn("Failed to setup ioapic entry for ioapic %d, pin %d\n",
  1071. mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
  1072. clear_irq_vector(irq, cfg);
  1073. return;
  1074. }
  1075. ioapic_register_intr(irq, cfg, attr->trigger);
  1076. if (irq < nr_legacy_irqs())
  1077. legacy_pic->mask(irq);
  1078. ioapic_write_entry(attr->ioapic, attr->ioapic_pin, entry);
  1079. }
  1080. static void __init setup_IO_APIC_irqs(void)
  1081. {
  1082. unsigned int ioapic, pin;
  1083. int idx;
  1084. apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
  1085. for_each_ioapic_pin(ioapic, pin) {
  1086. idx = find_irq_entry(ioapic, pin, mp_INT);
  1087. if (idx < 0)
  1088. apic_printk(APIC_VERBOSE,
  1089. KERN_DEBUG " apic %d pin %d not connected\n",
  1090. mpc_ioapic_id(ioapic), pin);
  1091. else
  1092. pin_2_irq(idx, ioapic, pin,
  1093. ioapic ? 0 : IOAPIC_MAP_ALLOC);
  1094. }
  1095. }
  1096. /*
  1097. * Set up the timer pin, possibly with the 8259A-master behind.
  1098. */
  1099. static void __init setup_timer_IRQ0_pin(unsigned int ioapic_idx,
  1100. unsigned int pin, int vector)
  1101. {
  1102. struct IO_APIC_route_entry entry;
  1103. unsigned int dest;
  1104. memset(&entry, 0, sizeof(entry));
  1105. /*
  1106. * We use logical delivery to get the timer IRQ
  1107. * to the first CPU.
  1108. */
  1109. if (unlikely(apic->cpu_mask_to_apicid_and(apic->target_cpus(),
  1110. apic->target_cpus(), &dest)))
  1111. dest = BAD_APICID;
  1112. entry.dest_mode = apic->irq_dest_mode;
  1113. entry.mask = 0; /* don't mask IRQ for edge */
  1114. entry.dest = dest;
  1115. entry.delivery_mode = apic->irq_delivery_mode;
  1116. entry.polarity = 0;
  1117. entry.trigger = 0;
  1118. entry.vector = vector;
  1119. /*
  1120. * The timer IRQ doesn't have to know that behind the
  1121. * scene we may have a 8259A-master in AEOI mode ...
  1122. */
  1123. irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
  1124. "edge");
  1125. /*
  1126. * Add it to the IO-APIC irq-routing table:
  1127. */
  1128. ioapic_write_entry(ioapic_idx, pin, entry);
  1129. }
  1130. void native_io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
  1131. {
  1132. int i;
  1133. pr_debug(" NR Dst Mask Trig IRR Pol Stat Dmod Deli Vect:\n");
  1134. for (i = 0; i <= nr_entries; i++) {
  1135. struct IO_APIC_route_entry entry;
  1136. entry = ioapic_read_entry(apic, i);
  1137. pr_debug(" %02x %02X ", i, entry.dest);
  1138. pr_cont("%1d %1d %1d %1d %1d "
  1139. "%1d %1d %02X\n",
  1140. entry.mask,
  1141. entry.trigger,
  1142. entry.irr,
  1143. entry.polarity,
  1144. entry.delivery_status,
  1145. entry.dest_mode,
  1146. entry.delivery_mode,
  1147. entry.vector);
  1148. }
  1149. }
  1150. void intel_ir_io_apic_print_entries(unsigned int apic,
  1151. unsigned int nr_entries)
  1152. {
  1153. int i;
  1154. pr_debug(" NR Indx Fmt Mask Trig IRR Pol Stat Indx2 Zero Vect:\n");
  1155. for (i = 0; i <= nr_entries; i++) {
  1156. struct IR_IO_APIC_route_entry *ir_entry;
  1157. struct IO_APIC_route_entry entry;
  1158. entry = ioapic_read_entry(apic, i);
  1159. ir_entry = (struct IR_IO_APIC_route_entry *)&entry;
  1160. pr_debug(" %02x %04X ", i, ir_entry->index);
  1161. pr_cont("%1d %1d %1d %1d %1d "
  1162. "%1d %1d %X %02X\n",
  1163. ir_entry->format,
  1164. ir_entry->mask,
  1165. ir_entry->trigger,
  1166. ir_entry->irr,
  1167. ir_entry->polarity,
  1168. ir_entry->delivery_status,
  1169. ir_entry->index2,
  1170. ir_entry->zero,
  1171. ir_entry->vector);
  1172. }
  1173. }
  1174. void ioapic_zap_locks(void)
  1175. {
  1176. raw_spin_lock_init(&ioapic_lock);
  1177. }
  1178. static void __init print_IO_APIC(int ioapic_idx)
  1179. {
  1180. union IO_APIC_reg_00 reg_00;
  1181. union IO_APIC_reg_01 reg_01;
  1182. union IO_APIC_reg_02 reg_02;
  1183. union IO_APIC_reg_03 reg_03;
  1184. unsigned long flags;
  1185. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1186. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1187. reg_01.raw = io_apic_read(ioapic_idx, 1);
  1188. if (reg_01.bits.version >= 0x10)
  1189. reg_02.raw = io_apic_read(ioapic_idx, 2);
  1190. if (reg_01.bits.version >= 0x20)
  1191. reg_03.raw = io_apic_read(ioapic_idx, 3);
  1192. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1193. printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
  1194. printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
  1195. printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
  1196. printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
  1197. printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
  1198. printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
  1199. printk(KERN_DEBUG "....... : max redirection entries: %02X\n",
  1200. reg_01.bits.entries);
  1201. printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
  1202. printk(KERN_DEBUG "....... : IO APIC version: %02X\n",
  1203. reg_01.bits.version);
  1204. /*
  1205. * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
  1206. * but the value of reg_02 is read as the previous read register
  1207. * value, so ignore it if reg_02 == reg_01.
  1208. */
  1209. if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
  1210. printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
  1211. printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
  1212. }
  1213. /*
  1214. * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
  1215. * or reg_03, but the value of reg_0[23] is read as the previous read
  1216. * register value, so ignore it if reg_03 == reg_0[12].
  1217. */
  1218. if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
  1219. reg_03.raw != reg_01.raw) {
  1220. printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
  1221. printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
  1222. }
  1223. printk(KERN_DEBUG ".... IRQ redirection table:\n");
  1224. x86_io_apic_ops.print_entries(ioapic_idx, reg_01.bits.entries);
  1225. }
  1226. void __init print_IO_APICs(void)
  1227. {
  1228. int ioapic_idx;
  1229. struct irq_cfg *cfg;
  1230. unsigned int irq;
  1231. struct irq_chip *chip;
  1232. printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
  1233. for_each_ioapic(ioapic_idx)
  1234. printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
  1235. mpc_ioapic_id(ioapic_idx),
  1236. ioapics[ioapic_idx].nr_registers);
  1237. /*
  1238. * We are a bit conservative about what we expect. We have to
  1239. * know about every hardware change ASAP.
  1240. */
  1241. printk(KERN_INFO "testing the IO APIC.......................\n");
  1242. for_each_ioapic(ioapic_idx)
  1243. print_IO_APIC(ioapic_idx);
  1244. printk(KERN_DEBUG "IRQ to pin mappings:\n");
  1245. for_each_active_irq(irq) {
  1246. struct irq_pin_list *entry;
  1247. chip = irq_get_chip(irq);
  1248. if (chip != &ioapic_chip)
  1249. continue;
  1250. cfg = irq_cfg(irq);
  1251. if (!cfg)
  1252. continue;
  1253. if (list_empty(&cfg->irq_2_pin))
  1254. continue;
  1255. printk(KERN_DEBUG "IRQ%d ", irq);
  1256. for_each_irq_pin(entry, cfg->irq_2_pin)
  1257. pr_cont("-> %d:%d", entry->apic, entry->pin);
  1258. pr_cont("\n");
  1259. }
  1260. printk(KERN_INFO ".................................... done.\n");
  1261. }
  1262. /* Where if anywhere is the i8259 connect in external int mode */
  1263. static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
  1264. void __init enable_IO_APIC(void)
  1265. {
  1266. int i8259_apic, i8259_pin;
  1267. int apic, pin;
  1268. if (!nr_legacy_irqs())
  1269. return;
  1270. for_each_ioapic_pin(apic, pin) {
  1271. /* See if any of the pins is in ExtINT mode */
  1272. struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
  1273. /* If the interrupt line is enabled and in ExtInt mode
  1274. * I have found the pin where the i8259 is connected.
  1275. */
  1276. if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
  1277. ioapic_i8259.apic = apic;
  1278. ioapic_i8259.pin = pin;
  1279. goto found_i8259;
  1280. }
  1281. }
  1282. found_i8259:
  1283. /* Look to see what if the MP table has reported the ExtINT */
  1284. /* If we could not find the appropriate pin by looking at the ioapic
  1285. * the i8259 probably is not connected the ioapic but give the
  1286. * mptable a chance anyway.
  1287. */
  1288. i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
  1289. i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
  1290. /* Trust the MP table if nothing is setup in the hardware */
  1291. if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
  1292. printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
  1293. ioapic_i8259.pin = i8259_pin;
  1294. ioapic_i8259.apic = i8259_apic;
  1295. }
  1296. /* Complain if the MP table and the hardware disagree */
  1297. if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
  1298. (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
  1299. {
  1300. printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
  1301. }
  1302. /*
  1303. * Do not trust the IO-APIC being empty at bootup
  1304. */
  1305. clear_IO_APIC();
  1306. }
  1307. void native_disable_io_apic(void)
  1308. {
  1309. /*
  1310. * If the i8259 is routed through an IOAPIC
  1311. * Put that IOAPIC in virtual wire mode
  1312. * so legacy interrupts can be delivered.
  1313. */
  1314. if (ioapic_i8259.pin != -1) {
  1315. struct IO_APIC_route_entry entry;
  1316. memset(&entry, 0, sizeof(entry));
  1317. entry.mask = 0; /* Enabled */
  1318. entry.trigger = 0; /* Edge */
  1319. entry.irr = 0;
  1320. entry.polarity = 0; /* High */
  1321. entry.delivery_status = 0;
  1322. entry.dest_mode = 0; /* Physical */
  1323. entry.delivery_mode = dest_ExtINT; /* ExtInt */
  1324. entry.vector = 0;
  1325. entry.dest = read_apic_id();
  1326. /*
  1327. * Add it to the IO-APIC irq-routing table:
  1328. */
  1329. ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
  1330. }
  1331. if (cpu_has_apic || apic_from_smp_config())
  1332. disconnect_bsp_APIC(ioapic_i8259.pin != -1);
  1333. }
  1334. /*
  1335. * Not an __init, needed by the reboot code
  1336. */
  1337. void disable_IO_APIC(void)
  1338. {
  1339. /*
  1340. * Clear the IO-APIC before rebooting:
  1341. */
  1342. clear_IO_APIC();
  1343. if (!nr_legacy_irqs())
  1344. return;
  1345. x86_io_apic_ops.disable();
  1346. }
  1347. #ifdef CONFIG_X86_32
  1348. /*
  1349. * function to set the IO-APIC physical IDs based on the
  1350. * values stored in the MPC table.
  1351. *
  1352. * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
  1353. */
  1354. void __init setup_ioapic_ids_from_mpc_nocheck(void)
  1355. {
  1356. union IO_APIC_reg_00 reg_00;
  1357. physid_mask_t phys_id_present_map;
  1358. int ioapic_idx;
  1359. int i;
  1360. unsigned char old_id;
  1361. unsigned long flags;
  1362. /*
  1363. * This is broken; anything with a real cpu count has to
  1364. * circumvent this idiocy regardless.
  1365. */
  1366. apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
  1367. /*
  1368. * Set the IOAPIC ID to the value stored in the MPC table.
  1369. */
  1370. for_each_ioapic(ioapic_idx) {
  1371. /* Read the register 0 value */
  1372. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1373. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1374. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1375. old_id = mpc_ioapic_id(ioapic_idx);
  1376. if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
  1377. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
  1378. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1379. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1380. reg_00.bits.ID);
  1381. ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
  1382. }
  1383. /*
  1384. * Sanity check, is the ID really free? Every APIC in a
  1385. * system must have a unique ID or we get lots of nice
  1386. * 'stuck on smp_invalidate_needed IPI wait' messages.
  1387. */
  1388. if (apic->check_apicid_used(&phys_id_present_map,
  1389. mpc_ioapic_id(ioapic_idx))) {
  1390. printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
  1391. ioapic_idx, mpc_ioapic_id(ioapic_idx));
  1392. for (i = 0; i < get_physical_broadcast(); i++)
  1393. if (!physid_isset(i, phys_id_present_map))
  1394. break;
  1395. if (i >= get_physical_broadcast())
  1396. panic("Max APIC ID exceeded!\n");
  1397. printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
  1398. i);
  1399. physid_set(i, phys_id_present_map);
  1400. ioapics[ioapic_idx].mp_config.apicid = i;
  1401. } else {
  1402. physid_mask_t tmp;
  1403. apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
  1404. &tmp);
  1405. apic_printk(APIC_VERBOSE, "Setting %d in the "
  1406. "phys_id_present_map\n",
  1407. mpc_ioapic_id(ioapic_idx));
  1408. physids_or(phys_id_present_map, phys_id_present_map, tmp);
  1409. }
  1410. /*
  1411. * We need to adjust the IRQ routing table
  1412. * if the ID changed.
  1413. */
  1414. if (old_id != mpc_ioapic_id(ioapic_idx))
  1415. for (i = 0; i < mp_irq_entries; i++)
  1416. if (mp_irqs[i].dstapic == old_id)
  1417. mp_irqs[i].dstapic
  1418. = mpc_ioapic_id(ioapic_idx);
  1419. /*
  1420. * Update the ID register according to the right value
  1421. * from the MPC table if they are different.
  1422. */
  1423. if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
  1424. continue;
  1425. apic_printk(APIC_VERBOSE, KERN_INFO
  1426. "...changing IO-APIC physical APIC ID to %d ...",
  1427. mpc_ioapic_id(ioapic_idx));
  1428. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  1429. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1430. io_apic_write(ioapic_idx, 0, reg_00.raw);
  1431. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1432. /*
  1433. * Sanity check
  1434. */
  1435. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1436. reg_00.raw = io_apic_read(ioapic_idx, 0);
  1437. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1438. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
  1439. pr_cont("could not set ID!\n");
  1440. else
  1441. apic_printk(APIC_VERBOSE, " ok.\n");
  1442. }
  1443. }
  1444. void __init setup_ioapic_ids_from_mpc(void)
  1445. {
  1446. if (acpi_ioapic)
  1447. return;
  1448. /*
  1449. * Don't check I/O APIC IDs for xAPIC systems. They have
  1450. * no meaning without the serial APIC bus.
  1451. */
  1452. if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  1453. || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  1454. return;
  1455. setup_ioapic_ids_from_mpc_nocheck();
  1456. }
  1457. #endif
  1458. int no_timer_check __initdata;
  1459. static int __init notimercheck(char *s)
  1460. {
  1461. no_timer_check = 1;
  1462. return 1;
  1463. }
  1464. __setup("no_timer_check", notimercheck);
  1465. /*
  1466. * There is a nasty bug in some older SMP boards, their mptable lies
  1467. * about the timer IRQ. We do the following to work around the situation:
  1468. *
  1469. * - timer IRQ defaults to IO-APIC IRQ
  1470. * - if this function detects that timer IRQs are defunct, then we fall
  1471. * back to ISA timer IRQs
  1472. */
  1473. static int __init timer_irq_works(void)
  1474. {
  1475. unsigned long t1 = jiffies;
  1476. unsigned long flags;
  1477. if (no_timer_check)
  1478. return 1;
  1479. local_save_flags(flags);
  1480. local_irq_enable();
  1481. /* Let ten ticks pass... */
  1482. mdelay((10 * 1000) / HZ);
  1483. local_irq_restore(flags);
  1484. /*
  1485. * Expect a few ticks at least, to be sure some possible
  1486. * glue logic does not lock up after one or two first
  1487. * ticks in a non-ExtINT mode. Also the local APIC
  1488. * might have cached one ExtINT interrupt. Finally, at
  1489. * least one tick may be lost due to delays.
  1490. */
  1491. /* jiffies wrap? */
  1492. if (time_after(jiffies, t1 + 4))
  1493. return 1;
  1494. return 0;
  1495. }
  1496. /*
  1497. * In the SMP+IOAPIC case it might happen that there are an unspecified
  1498. * number of pending IRQ events unhandled. These cases are very rare,
  1499. * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
  1500. * better to do it this way as thus we do not have to be aware of
  1501. * 'pending' interrupts in the IRQ path, except at this point.
  1502. */
  1503. /*
  1504. * Edge triggered needs to resend any interrupt
  1505. * that was delayed but this is now handled in the device
  1506. * independent code.
  1507. */
  1508. /*
  1509. * Starting up a edge-triggered IO-APIC interrupt is
  1510. * nasty - we need to make sure that we get the edge.
  1511. * If it is already asserted for some reason, we need
  1512. * return 1 to indicate that is was pending.
  1513. *
  1514. * This is not complete - we should be able to fake
  1515. * an edge even if it isn't on the 8259A...
  1516. */
  1517. static unsigned int startup_ioapic_irq(struct irq_data *data)
  1518. {
  1519. int was_pending = 0, irq = data->irq;
  1520. unsigned long flags;
  1521. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1522. if (irq < nr_legacy_irqs()) {
  1523. legacy_pic->mask(irq);
  1524. if (legacy_pic->irq_pending(irq))
  1525. was_pending = 1;
  1526. }
  1527. __unmask_ioapic(data->chip_data);
  1528. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1529. return was_pending;
  1530. }
  1531. /*
  1532. * Level and edge triggered IO-APIC interrupts need different handling,
  1533. * so we use two separate IRQ descriptors. Edge triggered IRQs can be
  1534. * handled with the level-triggered descriptor, but that one has slightly
  1535. * more overhead. Level-triggered interrupts cannot be handled with the
  1536. * edge-triggered handler, without risking IRQ storms and other ugly
  1537. * races.
  1538. */
  1539. static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
  1540. {
  1541. int apic, pin;
  1542. struct irq_pin_list *entry;
  1543. u8 vector = cfg->vector;
  1544. for_each_irq_pin(entry, cfg->irq_2_pin) {
  1545. unsigned int reg;
  1546. apic = entry->apic;
  1547. pin = entry->pin;
  1548. io_apic_write(apic, 0x11 + pin*2, dest);
  1549. reg = io_apic_read(apic, 0x10 + pin*2);
  1550. reg &= ~IO_APIC_REDIR_VECTOR_MASK;
  1551. reg |= vector;
  1552. io_apic_modify(apic, 0x10 + pin*2, reg);
  1553. }
  1554. }
  1555. int native_ioapic_set_affinity(struct irq_data *data,
  1556. const struct cpumask *mask,
  1557. bool force)
  1558. {
  1559. unsigned int dest, irq = data->irq;
  1560. unsigned long flags;
  1561. int ret;
  1562. if (!config_enabled(CONFIG_SMP))
  1563. return -EPERM;
  1564. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1565. ret = apic_set_affinity(data, mask, &dest);
  1566. if (!ret) {
  1567. /* Only the high 8 bits are valid. */
  1568. dest = SET_APIC_LOGICAL_ID(dest);
  1569. __target_IO_APIC_irq(irq, dest, data->chip_data);
  1570. ret = IRQ_SET_MASK_OK_NOCOPY;
  1571. }
  1572. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1573. return ret;
  1574. }
  1575. atomic_t irq_mis_count;
  1576. #ifdef CONFIG_GENERIC_PENDING_IRQ
  1577. static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
  1578. {
  1579. struct irq_pin_list *entry;
  1580. unsigned long flags;
  1581. raw_spin_lock_irqsave(&ioapic_lock, flags);
  1582. for_each_irq_pin(entry, cfg->irq_2_pin) {
  1583. unsigned int reg;
  1584. int pin;
  1585. pin = entry->pin;
  1586. reg = io_apic_read(entry->apic, 0x10 + pin*2);
  1587. /* Is the remote IRR bit set? */
  1588. if (reg & IO_APIC_REDIR_REMOTE_IRR) {
  1589. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1590. return true;
  1591. }
  1592. }
  1593. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  1594. return false;
  1595. }
  1596. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  1597. {
  1598. /* If we are moving the irq we need to mask it */
  1599. if (unlikely(irqd_is_setaffinity_pending(data))) {
  1600. mask_ioapic(cfg);
  1601. return true;
  1602. }
  1603. return false;
  1604. }
  1605. static inline void ioapic_irqd_unmask(struct irq_data *data,
  1606. struct irq_cfg *cfg, bool masked)
  1607. {
  1608. if (unlikely(masked)) {
  1609. /* Only migrate the irq if the ack has been received.
  1610. *
  1611. * On rare occasions the broadcast level triggered ack gets
  1612. * delayed going to ioapics, and if we reprogram the
  1613. * vector while Remote IRR is still set the irq will never
  1614. * fire again.
  1615. *
  1616. * To prevent this scenario we read the Remote IRR bit
  1617. * of the ioapic. This has two effects.
  1618. * - On any sane system the read of the ioapic will
  1619. * flush writes (and acks) going to the ioapic from
  1620. * this cpu.
  1621. * - We get to see if the ACK has actually been delivered.
  1622. *
  1623. * Based on failed experiments of reprogramming the
  1624. * ioapic entry from outside of irq context starting
  1625. * with masking the ioapic entry and then polling until
  1626. * Remote IRR was clear before reprogramming the
  1627. * ioapic I don't trust the Remote IRR bit to be
  1628. * completey accurate.
  1629. *
  1630. * However there appears to be no other way to plug
  1631. * this race, so if the Remote IRR bit is not
  1632. * accurate and is causing problems then it is a hardware bug
  1633. * and you can go talk to the chipset vendor about it.
  1634. */
  1635. if (!io_apic_level_ack_pending(cfg))
  1636. irq_move_masked_irq(data);
  1637. unmask_ioapic(cfg);
  1638. }
  1639. }
  1640. #else
  1641. static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
  1642. {
  1643. return false;
  1644. }
  1645. static inline void ioapic_irqd_unmask(struct irq_data *data,
  1646. struct irq_cfg *cfg, bool masked)
  1647. {
  1648. }
  1649. #endif
  1650. static void ack_ioapic_level(struct irq_data *data)
  1651. {
  1652. struct irq_cfg *cfg = data->chip_data;
  1653. int i, irq = data->irq;
  1654. unsigned long v;
  1655. bool masked;
  1656. irq_complete_move(cfg);
  1657. masked = ioapic_irqd_mask(data, cfg);
  1658. /*
  1659. * It appears there is an erratum which affects at least version 0x11
  1660. * of I/O APIC (that's the 82093AA and cores integrated into various
  1661. * chipsets). Under certain conditions a level-triggered interrupt is
  1662. * erroneously delivered as edge-triggered one but the respective IRR
  1663. * bit gets set nevertheless. As a result the I/O unit expects an EOI
  1664. * message but it will never arrive and further interrupts are blocked
  1665. * from the source. The exact reason is so far unknown, but the
  1666. * phenomenon was observed when two consecutive interrupt requests
  1667. * from a given source get delivered to the same CPU and the source is
  1668. * temporarily disabled in between.
  1669. *
  1670. * A workaround is to simulate an EOI message manually. We achieve it
  1671. * by setting the trigger mode to edge and then to level when the edge
  1672. * trigger mode gets detected in the TMR of a local APIC for a
  1673. * level-triggered interrupt. We mask the source for the time of the
  1674. * operation to prevent an edge-triggered interrupt escaping meanwhile.
  1675. * The idea is from Manfred Spraul. --macro
  1676. *
  1677. * Also in the case when cpu goes offline, fixup_irqs() will forward
  1678. * any unhandled interrupt on the offlined cpu to the new cpu
  1679. * destination that is handling the corresponding interrupt. This
  1680. * interrupt forwarding is done via IPI's. Hence, in this case also
  1681. * level-triggered io-apic interrupt will be seen as an edge
  1682. * interrupt in the IRR. And we can't rely on the cpu's EOI
  1683. * to be broadcasted to the IO-APIC's which will clear the remoteIRR
  1684. * corresponding to the level-triggered interrupt. Hence on IO-APIC's
  1685. * supporting EOI register, we do an explicit EOI to clear the
  1686. * remote IRR and on IO-APIC's which don't have an EOI register,
  1687. * we use the above logic (mask+edge followed by unmask+level) from
  1688. * Manfred Spraul to clear the remote IRR.
  1689. */
  1690. i = cfg->vector;
  1691. v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
  1692. /*
  1693. * We must acknowledge the irq before we move it or the acknowledge will
  1694. * not propagate properly.
  1695. */
  1696. ack_APIC_irq();
  1697. /*
  1698. * Tail end of clearing remote IRR bit (either by delivering the EOI
  1699. * message via io-apic EOI register write or simulating it using
  1700. * mask+edge followed by unnask+level logic) manually when the
  1701. * level triggered interrupt is seen as the edge triggered interrupt
  1702. * at the cpu.
  1703. */
  1704. if (!(v & (1 << (i & 0x1f)))) {
  1705. atomic_inc(&irq_mis_count);
  1706. eoi_ioapic_irq(irq, cfg);
  1707. }
  1708. ioapic_irqd_unmask(data, cfg, masked);
  1709. }
  1710. static struct irq_chip ioapic_chip __read_mostly = {
  1711. .name = "IO-APIC",
  1712. .irq_startup = startup_ioapic_irq,
  1713. .irq_mask = mask_ioapic_irq,
  1714. .irq_unmask = unmask_ioapic_irq,
  1715. .irq_ack = apic_ack_edge,
  1716. .irq_eoi = ack_ioapic_level,
  1717. .irq_set_affinity = native_ioapic_set_affinity,
  1718. .irq_retrigger = apic_retrigger_irq,
  1719. .flags = IRQCHIP_SKIP_SET_WAKE,
  1720. };
  1721. static inline void init_IO_APIC_traps(void)
  1722. {
  1723. struct irq_cfg *cfg;
  1724. unsigned int irq;
  1725. for_each_active_irq(irq) {
  1726. cfg = irq_cfg(irq);
  1727. if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
  1728. /*
  1729. * Hmm.. We don't have an entry for this,
  1730. * so default to an old-fashioned 8259
  1731. * interrupt if we can..
  1732. */
  1733. if (irq < nr_legacy_irqs())
  1734. legacy_pic->make_irq(irq);
  1735. else
  1736. /* Strange. Oh, well.. */
  1737. irq_set_chip(irq, &no_irq_chip);
  1738. }
  1739. }
  1740. }
  1741. /*
  1742. * The local APIC irq-chip implementation:
  1743. */
  1744. static void mask_lapic_irq(struct irq_data *data)
  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(struct irq_data *data)
  1751. {
  1752. unsigned long v;
  1753. v = apic_read(APIC_LVT0);
  1754. apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
  1755. }
  1756. static void ack_lapic_irq(struct irq_data *data)
  1757. {
  1758. ack_APIC_irq();
  1759. }
  1760. static struct irq_chip lapic_chip __read_mostly = {
  1761. .name = "local-APIC",
  1762. .irq_mask = mask_lapic_irq,
  1763. .irq_unmask = unmask_lapic_irq,
  1764. .irq_ack = ack_lapic_irq,
  1765. };
  1766. static void lapic_register_intr(int irq)
  1767. {
  1768. irq_clear_status_flags(irq, IRQ_LEVEL);
  1769. irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
  1770. "edge");
  1771. }
  1772. /*
  1773. * This looks a bit hackish but it's about the only one way of sending
  1774. * a few INTA cycles to 8259As and any associated glue logic. ICR does
  1775. * not support the ExtINT mode, unfortunately. We need to send these
  1776. * cycles as some i82489DX-based boards have glue logic that keeps the
  1777. * 8259A interrupt line asserted until INTA. --macro
  1778. */
  1779. static inline void __init unlock_ExtINT_logic(void)
  1780. {
  1781. int apic, pin, i;
  1782. struct IO_APIC_route_entry entry0, entry1;
  1783. unsigned char save_control, save_freq_select;
  1784. pin = find_isa_irq_pin(8, mp_INT);
  1785. if (pin == -1) {
  1786. WARN_ON_ONCE(1);
  1787. return;
  1788. }
  1789. apic = find_isa_irq_apic(8, mp_INT);
  1790. if (apic == -1) {
  1791. WARN_ON_ONCE(1);
  1792. return;
  1793. }
  1794. entry0 = ioapic_read_entry(apic, pin);
  1795. clear_IO_APIC_pin(apic, pin);
  1796. memset(&entry1, 0, sizeof(entry1));
  1797. entry1.dest_mode = 0; /* physical delivery */
  1798. entry1.mask = 0; /* unmask IRQ now */
  1799. entry1.dest = hard_smp_processor_id();
  1800. entry1.delivery_mode = dest_ExtINT;
  1801. entry1.polarity = entry0.polarity;
  1802. entry1.trigger = 0;
  1803. entry1.vector = 0;
  1804. ioapic_write_entry(apic, pin, entry1);
  1805. save_control = CMOS_READ(RTC_CONTROL);
  1806. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1807. CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
  1808. RTC_FREQ_SELECT);
  1809. CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
  1810. i = 100;
  1811. while (i-- > 0) {
  1812. mdelay(10);
  1813. if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
  1814. i -= 10;
  1815. }
  1816. CMOS_WRITE(save_control, RTC_CONTROL);
  1817. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1818. clear_IO_APIC_pin(apic, pin);
  1819. ioapic_write_entry(apic, pin, entry0);
  1820. }
  1821. static int disable_timer_pin_1 __initdata;
  1822. /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
  1823. static int __init disable_timer_pin_setup(char *arg)
  1824. {
  1825. disable_timer_pin_1 = 1;
  1826. return 0;
  1827. }
  1828. early_param("disable_timer_pin_1", disable_timer_pin_setup);
  1829. /*
  1830. * This code may look a bit paranoid, but it's supposed to cooperate with
  1831. * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
  1832. * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
  1833. * fanatically on his truly buggy board.
  1834. *
  1835. * FIXME: really need to revamp this for all platforms.
  1836. */
  1837. static inline void __init check_timer(void)
  1838. {
  1839. struct irq_cfg *cfg = irq_cfg(0);
  1840. int node = cpu_to_node(0);
  1841. int apic1, pin1, apic2, pin2;
  1842. unsigned long flags;
  1843. int no_pin1 = 0;
  1844. local_irq_save(flags);
  1845. /*
  1846. * get/set the timer IRQ vector:
  1847. */
  1848. legacy_pic->mask(0);
  1849. assign_irq_vector(0, cfg, apic->target_cpus());
  1850. /*
  1851. * As IRQ0 is to be enabled in the 8259A, the virtual
  1852. * wire has to be disabled in the local APIC. Also
  1853. * timer interrupts need to be acknowledged manually in
  1854. * the 8259A for the i82489DX when using the NMI
  1855. * watchdog as that APIC treats NMIs as level-triggered.
  1856. * The AEOI mode will finish them in the 8259A
  1857. * automatically.
  1858. */
  1859. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
  1860. legacy_pic->init(1);
  1861. pin1 = find_isa_irq_pin(0, mp_INT);
  1862. apic1 = find_isa_irq_apic(0, mp_INT);
  1863. pin2 = ioapic_i8259.pin;
  1864. apic2 = ioapic_i8259.apic;
  1865. apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
  1866. "apic1=%d pin1=%d apic2=%d pin2=%d\n",
  1867. cfg->vector, apic1, pin1, apic2, pin2);
  1868. /*
  1869. * Some BIOS writers are clueless and report the ExtINTA
  1870. * I/O APIC input from the cascaded 8259A as the timer
  1871. * interrupt input. So just in case, if only one pin
  1872. * was found above, try it both directly and through the
  1873. * 8259A.
  1874. */
  1875. if (pin1 == -1) {
  1876. panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
  1877. pin1 = pin2;
  1878. apic1 = apic2;
  1879. no_pin1 = 1;
  1880. } else if (pin2 == -1) {
  1881. pin2 = pin1;
  1882. apic2 = apic1;
  1883. }
  1884. if (pin1 != -1) {
  1885. /*
  1886. * Ok, does IRQ0 through the IOAPIC work?
  1887. */
  1888. if (no_pin1) {
  1889. add_pin_to_irq_node(cfg, node, apic1, pin1);
  1890. setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
  1891. } else {
  1892. /* for edge trigger, setup_ioapic_irq already
  1893. * leave it unmasked.
  1894. * so only need to unmask if it is level-trigger
  1895. * do we really have level trigger timer?
  1896. */
  1897. int idx;
  1898. idx = find_irq_entry(apic1, pin1, mp_INT);
  1899. if (idx != -1 && irq_trigger(idx))
  1900. unmask_ioapic(cfg);
  1901. }
  1902. if (timer_irq_works()) {
  1903. if (disable_timer_pin_1 > 0)
  1904. clear_IO_APIC_pin(0, pin1);
  1905. goto out;
  1906. }
  1907. panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
  1908. local_irq_disable();
  1909. clear_IO_APIC_pin(apic1, pin1);
  1910. if (!no_pin1)
  1911. apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
  1912. "8254 timer not connected to IO-APIC\n");
  1913. apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
  1914. "(IRQ0) through the 8259A ...\n");
  1915. apic_printk(APIC_QUIET, KERN_INFO
  1916. "..... (found apic %d pin %d) ...\n", apic2, pin2);
  1917. /*
  1918. * legacy devices should be connected to IO APIC #0
  1919. */
  1920. replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
  1921. setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
  1922. legacy_pic->unmask(0);
  1923. if (timer_irq_works()) {
  1924. apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
  1925. goto out;
  1926. }
  1927. /*
  1928. * Cleanup, just in case ...
  1929. */
  1930. local_irq_disable();
  1931. legacy_pic->mask(0);
  1932. clear_IO_APIC_pin(apic2, pin2);
  1933. apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
  1934. }
  1935. apic_printk(APIC_QUIET, KERN_INFO
  1936. "...trying to set up timer as Virtual Wire IRQ...\n");
  1937. lapic_register_intr(0);
  1938. apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
  1939. legacy_pic->unmask(0);
  1940. if (timer_irq_works()) {
  1941. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1942. goto out;
  1943. }
  1944. local_irq_disable();
  1945. legacy_pic->mask(0);
  1946. apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
  1947. apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
  1948. apic_printk(APIC_QUIET, KERN_INFO
  1949. "...trying to set up timer as ExtINT IRQ...\n");
  1950. legacy_pic->init(0);
  1951. legacy_pic->make_irq(0);
  1952. apic_write(APIC_LVT0, APIC_DM_EXTINT);
  1953. unlock_ExtINT_logic();
  1954. if (timer_irq_works()) {
  1955. apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
  1956. goto out;
  1957. }
  1958. local_irq_disable();
  1959. apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
  1960. if (x2apic_preenabled)
  1961. apic_printk(APIC_QUIET, KERN_INFO
  1962. "Perhaps problem with the pre-enabled x2apic mode\n"
  1963. "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
  1964. panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
  1965. "report. Then try booting with the 'noapic' option.\n");
  1966. out:
  1967. local_irq_restore(flags);
  1968. }
  1969. /*
  1970. * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
  1971. * to devices. However there may be an I/O APIC pin available for
  1972. * this interrupt regardless. The pin may be left unconnected, but
  1973. * typically it will be reused as an ExtINT cascade interrupt for
  1974. * the master 8259A. In the MPS case such a pin will normally be
  1975. * reported as an ExtINT interrupt in the MP table. With ACPI
  1976. * there is no provision for ExtINT interrupts, and in the absence
  1977. * of an override it would be treated as an ordinary ISA I/O APIC
  1978. * interrupt, that is edge-triggered and unmasked by default. We
  1979. * used to do this, but it caused problems on some systems because
  1980. * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
  1981. * the same ExtINT cascade interrupt to drive the local APIC of the
  1982. * bootstrap processor. Therefore we refrain from routing IRQ2 to
  1983. * the I/O APIC in all cases now. No actual device should request
  1984. * it anyway. --macro
  1985. */
  1986. #define PIC_IRQS (1UL << PIC_CASCADE_IR)
  1987. static int mp_irqdomain_create(int ioapic)
  1988. {
  1989. size_t size;
  1990. int hwirqs = mp_ioapic_pin_count(ioapic);
  1991. struct ioapic *ip = &ioapics[ioapic];
  1992. struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg;
  1993. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  1994. size = sizeof(struct mp_pin_info) * mp_ioapic_pin_count(ioapic);
  1995. ip->pin_info = kzalloc(size, GFP_KERNEL);
  1996. if (!ip->pin_info)
  1997. return -ENOMEM;
  1998. if (cfg->type == IOAPIC_DOMAIN_INVALID)
  1999. return 0;
  2000. ip->irqdomain = irq_domain_add_linear(cfg->dev, hwirqs, cfg->ops,
  2001. (void *)(long)ioapic);
  2002. if(!ip->irqdomain) {
  2003. kfree(ip->pin_info);
  2004. ip->pin_info = NULL;
  2005. return -ENOMEM;
  2006. }
  2007. if (cfg->type == IOAPIC_DOMAIN_LEGACY ||
  2008. cfg->type == IOAPIC_DOMAIN_STRICT)
  2009. ioapic_dynirq_base = max(ioapic_dynirq_base,
  2010. gsi_cfg->gsi_end + 1);
  2011. if (gsi_cfg->gsi_base == 0)
  2012. irq_set_default_host(ip->irqdomain);
  2013. return 0;
  2014. }
  2015. static void ioapic_destroy_irqdomain(int idx)
  2016. {
  2017. if (ioapics[idx].irqdomain) {
  2018. irq_domain_remove(ioapics[idx].irqdomain);
  2019. ioapics[idx].irqdomain = NULL;
  2020. }
  2021. kfree(ioapics[idx].pin_info);
  2022. ioapics[idx].pin_info = NULL;
  2023. }
  2024. void __init setup_IO_APIC(void)
  2025. {
  2026. int ioapic;
  2027. /*
  2028. * calling enable_IO_APIC() is moved to setup_local_APIC for BP
  2029. */
  2030. io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL;
  2031. apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
  2032. for_each_ioapic(ioapic)
  2033. BUG_ON(mp_irqdomain_create(ioapic));
  2034. /*
  2035. * Set up IO-APIC IRQ routing.
  2036. */
  2037. x86_init.mpparse.setup_ioapic_ids();
  2038. sync_Arb_IDs();
  2039. setup_IO_APIC_irqs();
  2040. init_IO_APIC_traps();
  2041. if (nr_legacy_irqs())
  2042. check_timer();
  2043. ioapic_initialized = 1;
  2044. }
  2045. /*
  2046. * Called after all the initialization is done. If we didn't find any
  2047. * APIC bugs then we can allow the modify fast path
  2048. */
  2049. static int __init io_apic_bug_finalize(void)
  2050. {
  2051. if (sis_apic_bug == -1)
  2052. sis_apic_bug = 0;
  2053. return 0;
  2054. }
  2055. late_initcall(io_apic_bug_finalize);
  2056. static void resume_ioapic_id(int ioapic_idx)
  2057. {
  2058. unsigned long flags;
  2059. union IO_APIC_reg_00 reg_00;
  2060. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2061. reg_00.raw = io_apic_read(ioapic_idx, 0);
  2062. if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
  2063. reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
  2064. io_apic_write(ioapic_idx, 0, reg_00.raw);
  2065. }
  2066. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2067. }
  2068. static void ioapic_resume(void)
  2069. {
  2070. int ioapic_idx;
  2071. for_each_ioapic_reverse(ioapic_idx)
  2072. resume_ioapic_id(ioapic_idx);
  2073. restore_ioapic_entries();
  2074. }
  2075. static struct syscore_ops ioapic_syscore_ops = {
  2076. .suspend = save_ioapic_entries,
  2077. .resume = ioapic_resume,
  2078. };
  2079. static int __init ioapic_init_ops(void)
  2080. {
  2081. register_syscore_ops(&ioapic_syscore_ops);
  2082. return 0;
  2083. }
  2084. device_initcall(ioapic_init_ops);
  2085. static int
  2086. io_apic_setup_irq_pin(unsigned int irq, int node, struct io_apic_irq_attr *attr)
  2087. {
  2088. struct irq_cfg *cfg = alloc_irq_and_cfg_at(irq, node);
  2089. int ret;
  2090. if (!cfg)
  2091. return -EINVAL;
  2092. ret = __add_pin_to_irq_node(cfg, node, attr->ioapic, attr->ioapic_pin);
  2093. if (!ret)
  2094. setup_ioapic_irq(irq, cfg, attr);
  2095. return ret;
  2096. }
  2097. static int io_apic_get_redir_entries(int ioapic)
  2098. {
  2099. union IO_APIC_reg_01 reg_01;
  2100. unsigned long flags;
  2101. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2102. reg_01.raw = io_apic_read(ioapic, 1);
  2103. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2104. /* The register returns the maximum index redir index
  2105. * supported, which is one less than the total number of redir
  2106. * entries.
  2107. */
  2108. return reg_01.bits.entries + 1;
  2109. }
  2110. unsigned int arch_dynirq_lower_bound(unsigned int from)
  2111. {
  2112. /*
  2113. * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use
  2114. * gsi_top if ioapic_dynirq_base hasn't been initialized yet.
  2115. */
  2116. return ioapic_initialized ? ioapic_dynirq_base : gsi_top;
  2117. }
  2118. int __init arch_probe_nr_irqs(void)
  2119. {
  2120. int nr;
  2121. if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
  2122. nr_irqs = NR_VECTORS * nr_cpu_ids;
  2123. nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
  2124. #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
  2125. /*
  2126. * for MSI and HT dyn irq
  2127. */
  2128. nr += gsi_top * 16;
  2129. #endif
  2130. if (nr < nr_irqs)
  2131. nr_irqs = nr;
  2132. return 0;
  2133. }
  2134. #ifdef CONFIG_X86_32
  2135. static int io_apic_get_unique_id(int ioapic, int apic_id)
  2136. {
  2137. union IO_APIC_reg_00 reg_00;
  2138. static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
  2139. physid_mask_t tmp;
  2140. unsigned long flags;
  2141. int i = 0;
  2142. /*
  2143. * The P4 platform supports up to 256 APIC IDs on two separate APIC
  2144. * buses (one for LAPICs, one for IOAPICs), where predecessors only
  2145. * supports up to 16 on one shared APIC bus.
  2146. *
  2147. * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
  2148. * advantage of new APIC bus architecture.
  2149. */
  2150. if (physids_empty(apic_id_map))
  2151. apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
  2152. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2153. reg_00.raw = io_apic_read(ioapic, 0);
  2154. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2155. if (apic_id >= get_physical_broadcast()) {
  2156. printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
  2157. "%d\n", ioapic, apic_id, reg_00.bits.ID);
  2158. apic_id = reg_00.bits.ID;
  2159. }
  2160. /*
  2161. * Every APIC in a system must have a unique ID or we get lots of nice
  2162. * 'stuck on smp_invalidate_needed IPI wait' messages.
  2163. */
  2164. if (apic->check_apicid_used(&apic_id_map, apic_id)) {
  2165. for (i = 0; i < get_physical_broadcast(); i++) {
  2166. if (!apic->check_apicid_used(&apic_id_map, i))
  2167. break;
  2168. }
  2169. if (i == get_physical_broadcast())
  2170. panic("Max apic_id exceeded!\n");
  2171. printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
  2172. "trying %d\n", ioapic, apic_id, i);
  2173. apic_id = i;
  2174. }
  2175. apic->apicid_to_cpu_present(apic_id, &tmp);
  2176. physids_or(apic_id_map, apic_id_map, tmp);
  2177. if (reg_00.bits.ID != apic_id) {
  2178. reg_00.bits.ID = apic_id;
  2179. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2180. io_apic_write(ioapic, 0, reg_00.raw);
  2181. reg_00.raw = io_apic_read(ioapic, 0);
  2182. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2183. /* Sanity check */
  2184. if (reg_00.bits.ID != apic_id) {
  2185. pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
  2186. ioapic);
  2187. return -1;
  2188. }
  2189. }
  2190. apic_printk(APIC_VERBOSE, KERN_INFO
  2191. "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
  2192. return apic_id;
  2193. }
  2194. static u8 io_apic_unique_id(int idx, u8 id)
  2195. {
  2196. if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
  2197. !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
  2198. return io_apic_get_unique_id(idx, id);
  2199. else
  2200. return id;
  2201. }
  2202. #else
  2203. static u8 io_apic_unique_id(int idx, u8 id)
  2204. {
  2205. union IO_APIC_reg_00 reg_00;
  2206. DECLARE_BITMAP(used, 256);
  2207. unsigned long flags;
  2208. u8 new_id;
  2209. int i;
  2210. bitmap_zero(used, 256);
  2211. for_each_ioapic(i)
  2212. __set_bit(mpc_ioapic_id(i), used);
  2213. /* Hand out the requested id if available */
  2214. if (!test_bit(id, used))
  2215. return id;
  2216. /*
  2217. * Read the current id from the ioapic and keep it if
  2218. * available.
  2219. */
  2220. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2221. reg_00.raw = io_apic_read(idx, 0);
  2222. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2223. new_id = reg_00.bits.ID;
  2224. if (!test_bit(new_id, used)) {
  2225. apic_printk(APIC_VERBOSE, KERN_INFO
  2226. "IOAPIC[%d]: Using reg apic_id %d instead of %d\n",
  2227. idx, new_id, id);
  2228. return new_id;
  2229. }
  2230. /*
  2231. * Get the next free id and write it to the ioapic.
  2232. */
  2233. new_id = find_first_zero_bit(used, 256);
  2234. reg_00.bits.ID = new_id;
  2235. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2236. io_apic_write(idx, 0, reg_00.raw);
  2237. reg_00.raw = io_apic_read(idx, 0);
  2238. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2239. /* Sanity check */
  2240. BUG_ON(reg_00.bits.ID != new_id);
  2241. return new_id;
  2242. }
  2243. #endif
  2244. static int io_apic_get_version(int ioapic)
  2245. {
  2246. union IO_APIC_reg_01 reg_01;
  2247. unsigned long flags;
  2248. raw_spin_lock_irqsave(&ioapic_lock, flags);
  2249. reg_01.raw = io_apic_read(ioapic, 1);
  2250. raw_spin_unlock_irqrestore(&ioapic_lock, flags);
  2251. return reg_01.bits.version;
  2252. }
  2253. int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
  2254. {
  2255. int ioapic, pin, idx;
  2256. if (skip_ioapic_setup)
  2257. return -1;
  2258. ioapic = mp_find_ioapic(gsi);
  2259. if (ioapic < 0)
  2260. return -1;
  2261. pin = mp_find_ioapic_pin(ioapic, gsi);
  2262. if (pin < 0)
  2263. return -1;
  2264. idx = find_irq_entry(ioapic, pin, mp_INT);
  2265. if (idx < 0)
  2266. return -1;
  2267. *trigger = irq_trigger(idx);
  2268. *polarity = irq_polarity(idx);
  2269. return 0;
  2270. }
  2271. /*
  2272. * This function currently is only a helper for the i386 smp boot process where
  2273. * we need to reprogram the ioredtbls to cater for the cpus which have come online
  2274. * so mask in all cases should simply be apic->target_cpus()
  2275. */
  2276. #ifdef CONFIG_SMP
  2277. void __init setup_ioapic_dest(void)
  2278. {
  2279. int pin, ioapic, irq, irq_entry;
  2280. const struct cpumask *mask;
  2281. struct irq_data *idata;
  2282. if (skip_ioapic_setup == 1)
  2283. return;
  2284. for_each_ioapic_pin(ioapic, pin) {
  2285. irq_entry = find_irq_entry(ioapic, pin, mp_INT);
  2286. if (irq_entry == -1)
  2287. continue;
  2288. irq = pin_2_irq(irq_entry, ioapic, pin, 0);
  2289. if (irq < 0 || !mp_init_irq_at_boot(ioapic, irq))
  2290. continue;
  2291. idata = irq_get_irq_data(irq);
  2292. /*
  2293. * Honour affinities which have been set in early boot
  2294. */
  2295. if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
  2296. mask = idata->affinity;
  2297. else
  2298. mask = apic->target_cpus();
  2299. x86_io_apic_ops.set_affinity(idata, mask, false);
  2300. }
  2301. }
  2302. #endif
  2303. #define IOAPIC_RESOURCE_NAME_SIZE 11
  2304. static struct resource *ioapic_resources;
  2305. static struct resource * __init ioapic_setup_resources(void)
  2306. {
  2307. unsigned long n;
  2308. struct resource *res;
  2309. char *mem;
  2310. int i, num = 0;
  2311. for_each_ioapic(i)
  2312. num++;
  2313. if (num == 0)
  2314. return NULL;
  2315. n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
  2316. n *= num;
  2317. mem = alloc_bootmem(n);
  2318. res = (void *)mem;
  2319. mem += sizeof(struct resource) * num;
  2320. num = 0;
  2321. for_each_ioapic(i) {
  2322. res[num].name = mem;
  2323. res[num].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  2324. snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
  2325. mem += IOAPIC_RESOURCE_NAME_SIZE;
  2326. num++;
  2327. ioapics[i].iomem_res = res;
  2328. }
  2329. ioapic_resources = res;
  2330. return res;
  2331. }
  2332. void __init native_io_apic_init_mappings(void)
  2333. {
  2334. unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
  2335. struct resource *ioapic_res;
  2336. int i;
  2337. ioapic_res = ioapic_setup_resources();
  2338. for_each_ioapic(i) {
  2339. if (smp_found_config) {
  2340. ioapic_phys = mpc_ioapic_addr(i);
  2341. #ifdef CONFIG_X86_32
  2342. if (!ioapic_phys) {
  2343. printk(KERN_ERR
  2344. "WARNING: bogus zero IO-APIC "
  2345. "address found in MPTABLE, "
  2346. "disabling IO/APIC support!\n");
  2347. smp_found_config = 0;
  2348. skip_ioapic_setup = 1;
  2349. goto fake_ioapic_page;
  2350. }
  2351. #endif
  2352. } else {
  2353. #ifdef CONFIG_X86_32
  2354. fake_ioapic_page:
  2355. #endif
  2356. ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
  2357. ioapic_phys = __pa(ioapic_phys);
  2358. }
  2359. set_fixmap_nocache(idx, ioapic_phys);
  2360. apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
  2361. __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
  2362. ioapic_phys);
  2363. idx++;
  2364. ioapic_res->start = ioapic_phys;
  2365. ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
  2366. ioapic_res++;
  2367. }
  2368. }
  2369. void __init ioapic_insert_resources(void)
  2370. {
  2371. int i;
  2372. struct resource *r = ioapic_resources;
  2373. if (!r) {
  2374. if (nr_ioapics > 0)
  2375. printk(KERN_ERR
  2376. "IO APIC resources couldn't be allocated.\n");
  2377. return;
  2378. }
  2379. for_each_ioapic(i) {
  2380. insert_resource(&iomem_resource, r);
  2381. r++;
  2382. }
  2383. }
  2384. int mp_find_ioapic(u32 gsi)
  2385. {
  2386. int i;
  2387. if (nr_ioapics == 0)
  2388. return -1;
  2389. /* Find the IOAPIC that manages this GSI. */
  2390. for_each_ioapic(i) {
  2391. struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
  2392. if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
  2393. return i;
  2394. }
  2395. printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
  2396. return -1;
  2397. }
  2398. int mp_find_ioapic_pin(int ioapic, u32 gsi)
  2399. {
  2400. struct mp_ioapic_gsi *gsi_cfg;
  2401. if (WARN_ON(ioapic < 0))
  2402. return -1;
  2403. gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  2404. if (WARN_ON(gsi > gsi_cfg->gsi_end))
  2405. return -1;
  2406. return gsi - gsi_cfg->gsi_base;
  2407. }
  2408. static int bad_ioapic_register(int idx)
  2409. {
  2410. union IO_APIC_reg_00 reg_00;
  2411. union IO_APIC_reg_01 reg_01;
  2412. union IO_APIC_reg_02 reg_02;
  2413. reg_00.raw = io_apic_read(idx, 0);
  2414. reg_01.raw = io_apic_read(idx, 1);
  2415. reg_02.raw = io_apic_read(idx, 2);
  2416. if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
  2417. pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
  2418. mpc_ioapic_addr(idx));
  2419. return 1;
  2420. }
  2421. return 0;
  2422. }
  2423. static int find_free_ioapic_entry(void)
  2424. {
  2425. int idx;
  2426. for (idx = 0; idx < MAX_IO_APICS; idx++)
  2427. if (ioapics[idx].nr_registers == 0)
  2428. return idx;
  2429. return MAX_IO_APICS;
  2430. }
  2431. /**
  2432. * mp_register_ioapic - Register an IOAPIC device
  2433. * @id: hardware IOAPIC ID
  2434. * @address: physical address of IOAPIC register area
  2435. * @gsi_base: base of GSI associated with the IOAPIC
  2436. * @cfg: configuration information for the IOAPIC
  2437. */
  2438. int mp_register_ioapic(int id, u32 address, u32 gsi_base,
  2439. struct ioapic_domain_cfg *cfg)
  2440. {
  2441. bool hotplug = !!ioapic_initialized;
  2442. struct mp_ioapic_gsi *gsi_cfg;
  2443. int idx, ioapic, entries;
  2444. u32 gsi_end;
  2445. if (!address) {
  2446. pr_warn("Bogus (zero) I/O APIC address found, skipping!\n");
  2447. return -EINVAL;
  2448. }
  2449. for_each_ioapic(ioapic)
  2450. if (ioapics[ioapic].mp_config.apicaddr == address) {
  2451. pr_warn("address 0x%x conflicts with IOAPIC%d\n",
  2452. address, ioapic);
  2453. return -EEXIST;
  2454. }
  2455. idx = find_free_ioapic_entry();
  2456. if (idx >= MAX_IO_APICS) {
  2457. pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
  2458. MAX_IO_APICS, idx);
  2459. return -ENOSPC;
  2460. }
  2461. ioapics[idx].mp_config.type = MP_IOAPIC;
  2462. ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
  2463. ioapics[idx].mp_config.apicaddr = address;
  2464. set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
  2465. if (bad_ioapic_register(idx)) {
  2466. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2467. return -ENODEV;
  2468. }
  2469. ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id);
  2470. ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
  2471. /*
  2472. * Build basic GSI lookup table to facilitate gsi->io_apic lookups
  2473. * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
  2474. */
  2475. entries = io_apic_get_redir_entries(idx);
  2476. gsi_end = gsi_base + entries - 1;
  2477. for_each_ioapic(ioapic) {
  2478. gsi_cfg = mp_ioapic_gsi_routing(ioapic);
  2479. if ((gsi_base >= gsi_cfg->gsi_base &&
  2480. gsi_base <= gsi_cfg->gsi_end) ||
  2481. (gsi_end >= gsi_cfg->gsi_base &&
  2482. gsi_end <= gsi_cfg->gsi_end)) {
  2483. pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n",
  2484. gsi_base, gsi_end,
  2485. gsi_cfg->gsi_base, gsi_cfg->gsi_end);
  2486. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2487. return -ENOSPC;
  2488. }
  2489. }
  2490. gsi_cfg = mp_ioapic_gsi_routing(idx);
  2491. gsi_cfg->gsi_base = gsi_base;
  2492. gsi_cfg->gsi_end = gsi_end;
  2493. ioapics[idx].irqdomain = NULL;
  2494. ioapics[idx].irqdomain_cfg = *cfg;
  2495. /*
  2496. * If mp_register_ioapic() is called during early boot stage when
  2497. * walking ACPI/SFI/DT tables, it's too early to create irqdomain,
  2498. * we are still using bootmem allocator. So delay it to setup_IO_APIC().
  2499. */
  2500. if (hotplug) {
  2501. if (mp_irqdomain_create(idx)) {
  2502. clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
  2503. return -ENOMEM;
  2504. }
  2505. alloc_ioapic_saved_registers(idx);
  2506. }
  2507. if (gsi_cfg->gsi_end >= gsi_top)
  2508. gsi_top = gsi_cfg->gsi_end + 1;
  2509. if (nr_ioapics <= idx)
  2510. nr_ioapics = idx + 1;
  2511. /* Set nr_registers to mark entry present */
  2512. ioapics[idx].nr_registers = entries;
  2513. pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
  2514. idx, mpc_ioapic_id(idx),
  2515. mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
  2516. gsi_cfg->gsi_base, gsi_cfg->gsi_end);
  2517. return 0;
  2518. }
  2519. int mp_unregister_ioapic(u32 gsi_base)
  2520. {
  2521. int ioapic, pin;
  2522. int found = 0;
  2523. struct mp_pin_info *pin_info;
  2524. for_each_ioapic(ioapic)
  2525. if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) {
  2526. found = 1;
  2527. break;
  2528. }
  2529. if (!found) {
  2530. pr_warn("can't find IOAPIC for GSI %d\n", gsi_base);
  2531. return -ENODEV;
  2532. }
  2533. for_each_pin(ioapic, pin) {
  2534. pin_info = mp_pin_info(ioapic, pin);
  2535. if (pin_info->count) {
  2536. pr_warn("pin%d on IOAPIC%d is still in use.\n",
  2537. pin, ioapic);
  2538. return -EBUSY;
  2539. }
  2540. }
  2541. /* Mark entry not present */
  2542. ioapics[ioapic].nr_registers = 0;
  2543. ioapic_destroy_irqdomain(ioapic);
  2544. free_ioapic_saved_registers(ioapic);
  2545. if (ioapics[ioapic].iomem_res)
  2546. release_resource(ioapics[ioapic].iomem_res);
  2547. clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic);
  2548. memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic]));
  2549. return 0;
  2550. }
  2551. int mp_ioapic_registered(u32 gsi_base)
  2552. {
  2553. int ioapic;
  2554. for_each_ioapic(ioapic)
  2555. if (ioapics[ioapic].gsi_config.gsi_base == gsi_base)
  2556. return 1;
  2557. return 0;
  2558. }
  2559. static inline void set_io_apic_irq_attr(struct io_apic_irq_attr *irq_attr,
  2560. int ioapic, int ioapic_pin,
  2561. int trigger, int polarity)
  2562. {
  2563. irq_attr->ioapic = ioapic;
  2564. irq_attr->ioapic_pin = ioapic_pin;
  2565. irq_attr->trigger = trigger;
  2566. irq_attr->polarity = polarity;
  2567. }
  2568. int mp_irqdomain_map(struct irq_domain *domain, unsigned int virq,
  2569. irq_hw_number_t hwirq)
  2570. {
  2571. int ioapic = (int)(long)domain->host_data;
  2572. struct mp_pin_info *info = mp_pin_info(ioapic, hwirq);
  2573. struct io_apic_irq_attr attr;
  2574. /* Get default attribute if not set by caller yet */
  2575. if (!info->set) {
  2576. u32 gsi = mp_pin_to_gsi(ioapic, hwirq);
  2577. if (acpi_get_override_irq(gsi, &info->trigger,
  2578. &info->polarity) < 0) {
  2579. /*
  2580. * PCI interrupts are always polarity one level
  2581. * triggered.
  2582. */
  2583. info->trigger = 1;
  2584. info->polarity = 1;
  2585. }
  2586. info->node = NUMA_NO_NODE;
  2587. /*
  2588. * setup_IO_APIC_irqs() programs all legacy IRQs with default
  2589. * trigger and polarity attributes. Don't set the flag for that
  2590. * case so the first legacy IRQ user could reprogram the pin
  2591. * with real trigger and polarity attributes.
  2592. */
  2593. if (virq >= nr_legacy_irqs() || info->count)
  2594. info->set = 1;
  2595. }
  2596. set_io_apic_irq_attr(&attr, ioapic, hwirq, info->trigger,
  2597. info->polarity);
  2598. return io_apic_setup_irq_pin(virq, info->node, &attr);
  2599. }
  2600. void mp_irqdomain_unmap(struct irq_domain *domain, unsigned int virq)
  2601. {
  2602. struct irq_data *data = irq_get_irq_data(virq);
  2603. struct irq_cfg *cfg = irq_cfg(virq);
  2604. int ioapic = (int)(long)domain->host_data;
  2605. int pin = (int)data->hwirq;
  2606. ioapic_mask_entry(ioapic, pin);
  2607. __remove_pin_from_irq(cfg, ioapic, pin);
  2608. WARN_ON(!list_empty(&cfg->irq_2_pin));
  2609. arch_teardown_hwirq(virq);
  2610. }
  2611. int mp_set_gsi_attr(u32 gsi, int trigger, int polarity, int node)
  2612. {
  2613. int ret = 0;
  2614. int ioapic, pin;
  2615. struct mp_pin_info *info;
  2616. ioapic = mp_find_ioapic(gsi);
  2617. if (ioapic < 0)
  2618. return -ENODEV;
  2619. pin = mp_find_ioapic_pin(ioapic, gsi);
  2620. info = mp_pin_info(ioapic, pin);
  2621. trigger = trigger ? 1 : 0;
  2622. polarity = polarity ? 1 : 0;
  2623. mutex_lock(&ioapic_mutex);
  2624. if (!info->set) {
  2625. info->trigger = trigger;
  2626. info->polarity = polarity;
  2627. info->node = node;
  2628. info->set = 1;
  2629. } else if (info->trigger != trigger || info->polarity != polarity) {
  2630. ret = -EBUSY;
  2631. }
  2632. mutex_unlock(&ioapic_mutex);
  2633. return ret;
  2634. }
  2635. /* Enable IOAPIC early just for system timer */
  2636. void __init pre_init_apic_IRQ0(void)
  2637. {
  2638. struct io_apic_irq_attr attr = { 0, 0, 0, 0 };
  2639. printk(KERN_INFO "Early APIC setup for system timer0\n");
  2640. #ifndef CONFIG_SMP
  2641. physid_set_mask_of_physid(boot_cpu_physical_apicid,
  2642. &phys_cpu_present_map);
  2643. #endif
  2644. setup_local_APIC();
  2645. io_apic_setup_irq_pin(0, 0, &attr);
  2646. irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
  2647. "edge");
  2648. }