mpic.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. /*
  2. * arch/powerpc/kernel/mpic.c
  3. *
  4. * Driver for interrupt controllers following the OpenPIC standard, the
  5. * common implementation beeing IBM's MPIC. This driver also can deal
  6. * with various broken implementations of this HW.
  7. *
  8. * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
  9. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  10. *
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file COPYING in the main directory of this archive
  13. * for more details.
  14. */
  15. #undef DEBUG
  16. #undef DEBUG_IPI
  17. #undef DEBUG_IRQ
  18. #undef DEBUG_LOW
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/irq.h>
  23. #include <linux/smp.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/pci.h>
  28. #include <linux/slab.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/ratelimit.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/signal.h>
  33. #include <asm/io.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/irq.h>
  36. #include <asm/machdep.h>
  37. #include <asm/mpic.h>
  38. #include <asm/smp.h>
  39. #include "mpic.h"
  40. #ifdef DEBUG
  41. #define DBG(fmt...) printk(fmt)
  42. #else
  43. #define DBG(fmt...)
  44. #endif
  45. static struct mpic *mpics;
  46. static struct mpic *mpic_primary;
  47. static DEFINE_RAW_SPINLOCK(mpic_lock);
  48. #ifdef CONFIG_PPC32 /* XXX for now */
  49. #ifdef CONFIG_IRQ_ALL_CPUS
  50. #define distribute_irqs (1)
  51. #else
  52. #define distribute_irqs (0)
  53. #endif
  54. #endif
  55. #ifdef CONFIG_MPIC_WEIRD
  56. static u32 mpic_infos[][MPIC_IDX_END] = {
  57. [0] = { /* Original OpenPIC compatible MPIC */
  58. MPIC_GREG_BASE,
  59. MPIC_GREG_FEATURE_0,
  60. MPIC_GREG_GLOBAL_CONF_0,
  61. MPIC_GREG_VENDOR_ID,
  62. MPIC_GREG_IPI_VECTOR_PRI_0,
  63. MPIC_GREG_IPI_STRIDE,
  64. MPIC_GREG_SPURIOUS,
  65. MPIC_GREG_TIMER_FREQ,
  66. MPIC_TIMER_BASE,
  67. MPIC_TIMER_STRIDE,
  68. MPIC_TIMER_CURRENT_CNT,
  69. MPIC_TIMER_BASE_CNT,
  70. MPIC_TIMER_VECTOR_PRI,
  71. MPIC_TIMER_DESTINATION,
  72. MPIC_CPU_BASE,
  73. MPIC_CPU_STRIDE,
  74. MPIC_CPU_IPI_DISPATCH_0,
  75. MPIC_CPU_IPI_DISPATCH_STRIDE,
  76. MPIC_CPU_CURRENT_TASK_PRI,
  77. MPIC_CPU_WHOAMI,
  78. MPIC_CPU_INTACK,
  79. MPIC_CPU_EOI,
  80. MPIC_CPU_MCACK,
  81. MPIC_IRQ_BASE,
  82. MPIC_IRQ_STRIDE,
  83. MPIC_IRQ_VECTOR_PRI,
  84. MPIC_VECPRI_VECTOR_MASK,
  85. MPIC_VECPRI_POLARITY_POSITIVE,
  86. MPIC_VECPRI_POLARITY_NEGATIVE,
  87. MPIC_VECPRI_SENSE_LEVEL,
  88. MPIC_VECPRI_SENSE_EDGE,
  89. MPIC_VECPRI_POLARITY_MASK,
  90. MPIC_VECPRI_SENSE_MASK,
  91. MPIC_IRQ_DESTINATION
  92. },
  93. [1] = { /* Tsi108/109 PIC */
  94. TSI108_GREG_BASE,
  95. TSI108_GREG_FEATURE_0,
  96. TSI108_GREG_GLOBAL_CONF_0,
  97. TSI108_GREG_VENDOR_ID,
  98. TSI108_GREG_IPI_VECTOR_PRI_0,
  99. TSI108_GREG_IPI_STRIDE,
  100. TSI108_GREG_SPURIOUS,
  101. TSI108_GREG_TIMER_FREQ,
  102. TSI108_TIMER_BASE,
  103. TSI108_TIMER_STRIDE,
  104. TSI108_TIMER_CURRENT_CNT,
  105. TSI108_TIMER_BASE_CNT,
  106. TSI108_TIMER_VECTOR_PRI,
  107. TSI108_TIMER_DESTINATION,
  108. TSI108_CPU_BASE,
  109. TSI108_CPU_STRIDE,
  110. TSI108_CPU_IPI_DISPATCH_0,
  111. TSI108_CPU_IPI_DISPATCH_STRIDE,
  112. TSI108_CPU_CURRENT_TASK_PRI,
  113. TSI108_CPU_WHOAMI,
  114. TSI108_CPU_INTACK,
  115. TSI108_CPU_EOI,
  116. TSI108_CPU_MCACK,
  117. TSI108_IRQ_BASE,
  118. TSI108_IRQ_STRIDE,
  119. TSI108_IRQ_VECTOR_PRI,
  120. TSI108_VECPRI_VECTOR_MASK,
  121. TSI108_VECPRI_POLARITY_POSITIVE,
  122. TSI108_VECPRI_POLARITY_NEGATIVE,
  123. TSI108_VECPRI_SENSE_LEVEL,
  124. TSI108_VECPRI_SENSE_EDGE,
  125. TSI108_VECPRI_POLARITY_MASK,
  126. TSI108_VECPRI_SENSE_MASK,
  127. TSI108_IRQ_DESTINATION
  128. },
  129. };
  130. #define MPIC_INFO(name) mpic->hw_set[MPIC_IDX_##name]
  131. #else /* CONFIG_MPIC_WEIRD */
  132. #define MPIC_INFO(name) MPIC_##name
  133. #endif /* CONFIG_MPIC_WEIRD */
  134. static inline unsigned int mpic_processor_id(struct mpic *mpic)
  135. {
  136. unsigned int cpu = 0;
  137. if (!(mpic->flags & MPIC_SECONDARY))
  138. cpu = hard_smp_processor_id();
  139. return cpu;
  140. }
  141. /*
  142. * Register accessor functions
  143. */
  144. static inline u32 _mpic_read(enum mpic_reg_type type,
  145. struct mpic_reg_bank *rb,
  146. unsigned int reg)
  147. {
  148. switch(type) {
  149. #ifdef CONFIG_PPC_DCR
  150. case mpic_access_dcr:
  151. return dcr_read(rb->dhost, reg);
  152. #endif
  153. case mpic_access_mmio_be:
  154. return in_be32(rb->base + (reg >> 2));
  155. case mpic_access_mmio_le:
  156. default:
  157. return in_le32(rb->base + (reg >> 2));
  158. }
  159. }
  160. static inline void _mpic_write(enum mpic_reg_type type,
  161. struct mpic_reg_bank *rb,
  162. unsigned int reg, u32 value)
  163. {
  164. switch(type) {
  165. #ifdef CONFIG_PPC_DCR
  166. case mpic_access_dcr:
  167. dcr_write(rb->dhost, reg, value);
  168. break;
  169. #endif
  170. case mpic_access_mmio_be:
  171. out_be32(rb->base + (reg >> 2), value);
  172. break;
  173. case mpic_access_mmio_le:
  174. default:
  175. out_le32(rb->base + (reg >> 2), value);
  176. break;
  177. }
  178. }
  179. static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
  180. {
  181. enum mpic_reg_type type = mpic->reg_type;
  182. unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
  183. (ipi * MPIC_INFO(GREG_IPI_STRIDE));
  184. if ((mpic->flags & MPIC_BROKEN_IPI) && type == mpic_access_mmio_le)
  185. type = mpic_access_mmio_be;
  186. return _mpic_read(type, &mpic->gregs, offset);
  187. }
  188. static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
  189. {
  190. unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
  191. (ipi * MPIC_INFO(GREG_IPI_STRIDE));
  192. _mpic_write(mpic->reg_type, &mpic->gregs, offset, value);
  193. }
  194. static inline u32 _mpic_tm_read(struct mpic *mpic, unsigned int tm)
  195. {
  196. unsigned int offset = MPIC_INFO(TIMER_VECTOR_PRI) +
  197. ((tm & 3) * MPIC_INFO(TIMER_STRIDE));
  198. if (tm >= 4)
  199. offset += 0x1000 / 4;
  200. return _mpic_read(mpic->reg_type, &mpic->tmregs, offset);
  201. }
  202. static inline void _mpic_tm_write(struct mpic *mpic, unsigned int tm, u32 value)
  203. {
  204. unsigned int offset = MPIC_INFO(TIMER_VECTOR_PRI) +
  205. ((tm & 3) * MPIC_INFO(TIMER_STRIDE));
  206. if (tm >= 4)
  207. offset += 0x1000 / 4;
  208. _mpic_write(mpic->reg_type, &mpic->tmregs, offset, value);
  209. }
  210. static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
  211. {
  212. unsigned int cpu = mpic_processor_id(mpic);
  213. return _mpic_read(mpic->reg_type, &mpic->cpuregs[cpu], reg);
  214. }
  215. static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
  216. {
  217. unsigned int cpu = mpic_processor_id(mpic);
  218. _mpic_write(mpic->reg_type, &mpic->cpuregs[cpu], reg, value);
  219. }
  220. static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
  221. {
  222. unsigned int isu = src_no >> mpic->isu_shift;
  223. unsigned int idx = src_no & mpic->isu_mask;
  224. unsigned int val;
  225. val = _mpic_read(mpic->reg_type, &mpic->isus[isu],
  226. reg + (idx * MPIC_INFO(IRQ_STRIDE)));
  227. #ifdef CONFIG_MPIC_BROKEN_REGREAD
  228. if (reg == 0)
  229. val = (val & (MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY)) |
  230. mpic->isu_reg0_shadow[src_no];
  231. #endif
  232. return val;
  233. }
  234. static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
  235. unsigned int reg, u32 value)
  236. {
  237. unsigned int isu = src_no >> mpic->isu_shift;
  238. unsigned int idx = src_no & mpic->isu_mask;
  239. _mpic_write(mpic->reg_type, &mpic->isus[isu],
  240. reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
  241. #ifdef CONFIG_MPIC_BROKEN_REGREAD
  242. if (reg == 0)
  243. mpic->isu_reg0_shadow[src_no] =
  244. value & ~(MPIC_VECPRI_MASK | MPIC_VECPRI_ACTIVITY);
  245. #endif
  246. }
  247. #define mpic_read(b,r) _mpic_read(mpic->reg_type,&(b),(r))
  248. #define mpic_write(b,r,v) _mpic_write(mpic->reg_type,&(b),(r),(v))
  249. #define mpic_ipi_read(i) _mpic_ipi_read(mpic,(i))
  250. #define mpic_ipi_write(i,v) _mpic_ipi_write(mpic,(i),(v))
  251. #define mpic_tm_read(i) _mpic_tm_read(mpic,(i))
  252. #define mpic_tm_write(i,v) _mpic_tm_write(mpic,(i),(v))
  253. #define mpic_cpu_read(i) _mpic_cpu_read(mpic,(i))
  254. #define mpic_cpu_write(i,v) _mpic_cpu_write(mpic,(i),(v))
  255. #define mpic_irq_read(s,r) _mpic_irq_read(mpic,(s),(r))
  256. #define mpic_irq_write(s,r,v) _mpic_irq_write(mpic,(s),(r),(v))
  257. /*
  258. * Low level utility functions
  259. */
  260. static void _mpic_map_mmio(struct mpic *mpic, phys_addr_t phys_addr,
  261. struct mpic_reg_bank *rb, unsigned int offset,
  262. unsigned int size)
  263. {
  264. rb->base = ioremap(phys_addr + offset, size);
  265. BUG_ON(rb->base == NULL);
  266. }
  267. #ifdef CONFIG_PPC_DCR
  268. static void _mpic_map_dcr(struct mpic *mpic, struct device_node *node,
  269. struct mpic_reg_bank *rb,
  270. unsigned int offset, unsigned int size)
  271. {
  272. const u32 *dbasep;
  273. dbasep = of_get_property(node, "dcr-reg", NULL);
  274. rb->dhost = dcr_map(node, *dbasep + offset, size);
  275. BUG_ON(!DCR_MAP_OK(rb->dhost));
  276. }
  277. static inline void mpic_map(struct mpic *mpic, struct device_node *node,
  278. phys_addr_t phys_addr, struct mpic_reg_bank *rb,
  279. unsigned int offset, unsigned int size)
  280. {
  281. if (mpic->flags & MPIC_USES_DCR)
  282. _mpic_map_dcr(mpic, node, rb, offset, size);
  283. else
  284. _mpic_map_mmio(mpic, phys_addr, rb, offset, size);
  285. }
  286. #else /* CONFIG_PPC_DCR */
  287. #define mpic_map(m,n,p,b,o,s) _mpic_map_mmio(m,p,b,o,s)
  288. #endif /* !CONFIG_PPC_DCR */
  289. /* Check if we have one of those nice broken MPICs with a flipped endian on
  290. * reads from IPI registers
  291. */
  292. static void __init mpic_test_broken_ipi(struct mpic *mpic)
  293. {
  294. u32 r;
  295. mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
  296. r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
  297. if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
  298. printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
  299. mpic->flags |= MPIC_BROKEN_IPI;
  300. }
  301. }
  302. #ifdef CONFIG_MPIC_U3_HT_IRQS
  303. /* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
  304. * to force the edge setting on the MPIC and do the ack workaround.
  305. */
  306. static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
  307. {
  308. if (source >= 128 || !mpic->fixups)
  309. return 0;
  310. return mpic->fixups[source].base != NULL;
  311. }
  312. static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
  313. {
  314. struct mpic_irq_fixup *fixup = &mpic->fixups[source];
  315. if (fixup->applebase) {
  316. unsigned int soff = (fixup->index >> 3) & ~3;
  317. unsigned int mask = 1U << (fixup->index & 0x1f);
  318. writel(mask, fixup->applebase + soff);
  319. } else {
  320. raw_spin_lock(&mpic->fixup_lock);
  321. writeb(0x11 + 2 * fixup->index, fixup->base + 2);
  322. writel(fixup->data, fixup->base + 4);
  323. raw_spin_unlock(&mpic->fixup_lock);
  324. }
  325. }
  326. static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
  327. bool level)
  328. {
  329. struct mpic_irq_fixup *fixup = &mpic->fixups[source];
  330. unsigned long flags;
  331. u32 tmp;
  332. if (fixup->base == NULL)
  333. return;
  334. DBG("startup_ht_interrupt(0x%x) index: %d\n",
  335. source, fixup->index);
  336. raw_spin_lock_irqsave(&mpic->fixup_lock, flags);
  337. /* Enable and configure */
  338. writeb(0x10 + 2 * fixup->index, fixup->base + 2);
  339. tmp = readl(fixup->base + 4);
  340. tmp &= ~(0x23U);
  341. if (level)
  342. tmp |= 0x22;
  343. writel(tmp, fixup->base + 4);
  344. raw_spin_unlock_irqrestore(&mpic->fixup_lock, flags);
  345. #ifdef CONFIG_PM
  346. /* use the lowest bit inverted to the actual HW,
  347. * set if this fixup was enabled, clear otherwise */
  348. mpic->save_data[source].fixup_data = tmp | 1;
  349. #endif
  350. }
  351. static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source)
  352. {
  353. struct mpic_irq_fixup *fixup = &mpic->fixups[source];
  354. unsigned long flags;
  355. u32 tmp;
  356. if (fixup->base == NULL)
  357. return;
  358. DBG("shutdown_ht_interrupt(0x%x)\n", source);
  359. /* Disable */
  360. raw_spin_lock_irqsave(&mpic->fixup_lock, flags);
  361. writeb(0x10 + 2 * fixup->index, fixup->base + 2);
  362. tmp = readl(fixup->base + 4);
  363. tmp |= 1;
  364. writel(tmp, fixup->base + 4);
  365. raw_spin_unlock_irqrestore(&mpic->fixup_lock, flags);
  366. #ifdef CONFIG_PM
  367. /* use the lowest bit inverted to the actual HW,
  368. * set if this fixup was enabled, clear otherwise */
  369. mpic->save_data[source].fixup_data = tmp & ~1;
  370. #endif
  371. }
  372. #ifdef CONFIG_PCI_MSI
  373. static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
  374. unsigned int devfn)
  375. {
  376. u8 __iomem *base;
  377. u8 pos, flags;
  378. u64 addr = 0;
  379. for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
  380. pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
  381. u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
  382. if (id == PCI_CAP_ID_HT) {
  383. id = readb(devbase + pos + 3);
  384. if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
  385. break;
  386. }
  387. }
  388. if (pos == 0)
  389. return;
  390. base = devbase + pos;
  391. flags = readb(base + HT_MSI_FLAGS);
  392. if (!(flags & HT_MSI_FLAGS_FIXED)) {
  393. addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
  394. addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
  395. }
  396. printk(KERN_DEBUG "mpic: - HT:%02x.%x %s MSI mapping found @ 0x%llx\n",
  397. PCI_SLOT(devfn), PCI_FUNC(devfn),
  398. flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
  399. if (!(flags & HT_MSI_FLAGS_ENABLE))
  400. writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
  401. }
  402. #else
  403. static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
  404. unsigned int devfn)
  405. {
  406. return;
  407. }
  408. #endif
  409. static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
  410. unsigned int devfn, u32 vdid)
  411. {
  412. int i, irq, n;
  413. u8 __iomem *base;
  414. u32 tmp;
  415. u8 pos;
  416. for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
  417. pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
  418. u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
  419. if (id == PCI_CAP_ID_HT) {
  420. id = readb(devbase + pos + 3);
  421. if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
  422. break;
  423. }
  424. }
  425. if (pos == 0)
  426. return;
  427. base = devbase + pos;
  428. writeb(0x01, base + 2);
  429. n = (readl(base + 4) >> 16) & 0xff;
  430. printk(KERN_INFO "mpic: - HT:%02x.%x [0x%02x] vendor %04x device %04x"
  431. " has %d irqs\n",
  432. devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
  433. for (i = 0; i <= n; i++) {
  434. writeb(0x10 + 2 * i, base + 2);
  435. tmp = readl(base + 4);
  436. irq = (tmp >> 16) & 0xff;
  437. DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
  438. /* mask it , will be unmasked later */
  439. tmp |= 0x1;
  440. writel(tmp, base + 4);
  441. mpic->fixups[irq].index = i;
  442. mpic->fixups[irq].base = base;
  443. /* Apple HT PIC has a non-standard way of doing EOIs */
  444. if ((vdid & 0xffff) == 0x106b)
  445. mpic->fixups[irq].applebase = devbase + 0x60;
  446. else
  447. mpic->fixups[irq].applebase = NULL;
  448. writeb(0x11 + 2 * i, base + 2);
  449. mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
  450. }
  451. }
  452. static void __init mpic_scan_ht_pics(struct mpic *mpic)
  453. {
  454. unsigned int devfn;
  455. u8 __iomem *cfgspace;
  456. printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
  457. /* Allocate fixups array */
  458. mpic->fixups = kzalloc(128 * sizeof(*mpic->fixups), GFP_KERNEL);
  459. BUG_ON(mpic->fixups == NULL);
  460. /* Init spinlock */
  461. raw_spin_lock_init(&mpic->fixup_lock);
  462. /* Map U3 config space. We assume all IO-APICs are on the primary bus
  463. * so we only need to map 64kB.
  464. */
  465. cfgspace = ioremap(0xf2000000, 0x10000);
  466. BUG_ON(cfgspace == NULL);
  467. /* Now we scan all slots. We do a very quick scan, we read the header
  468. * type, vendor ID and device ID only, that's plenty enough
  469. */
  470. for (devfn = 0; devfn < 0x100; devfn++) {
  471. u8 __iomem *devbase = cfgspace + (devfn << 8);
  472. u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
  473. u32 l = readl(devbase + PCI_VENDOR_ID);
  474. u16 s;
  475. DBG("devfn %x, l: %x\n", devfn, l);
  476. /* If no device, skip */
  477. if (l == 0xffffffff || l == 0x00000000 ||
  478. l == 0x0000ffff || l == 0xffff0000)
  479. goto next;
  480. /* Check if is supports capability lists */
  481. s = readw(devbase + PCI_STATUS);
  482. if (!(s & PCI_STATUS_CAP_LIST))
  483. goto next;
  484. mpic_scan_ht_pic(mpic, devbase, devfn, l);
  485. mpic_scan_ht_msi(mpic, devbase, devfn);
  486. next:
  487. /* next device, if function 0 */
  488. if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
  489. devfn += 7;
  490. }
  491. }
  492. #else /* CONFIG_MPIC_U3_HT_IRQS */
  493. static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
  494. {
  495. return 0;
  496. }
  497. static void __init mpic_scan_ht_pics(struct mpic *mpic)
  498. {
  499. }
  500. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  501. /* Find an mpic associated with a given linux interrupt */
  502. static struct mpic *mpic_find(unsigned int irq)
  503. {
  504. if (irq < NUM_ISA_INTERRUPTS)
  505. return NULL;
  506. return irq_get_chip_data(irq);
  507. }
  508. /* Determine if the linux irq is an IPI */
  509. static unsigned int mpic_is_ipi(struct mpic *mpic, unsigned int irq)
  510. {
  511. unsigned int src = virq_to_hw(irq);
  512. return (src >= mpic->ipi_vecs[0] && src <= mpic->ipi_vecs[3]);
  513. }
  514. /* Determine if the linux irq is a timer */
  515. static unsigned int mpic_is_tm(struct mpic *mpic, unsigned int irq)
  516. {
  517. unsigned int src = virq_to_hw(irq);
  518. return (src >= mpic->timer_vecs[0] && src <= mpic->timer_vecs[7]);
  519. }
  520. /* Convert a cpu mask from logical to physical cpu numbers. */
  521. static inline u32 mpic_physmask(u32 cpumask)
  522. {
  523. int i;
  524. u32 mask = 0;
  525. for (i = 0; i < min(32, NR_CPUS); ++i, cpumask >>= 1)
  526. mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
  527. return mask;
  528. }
  529. #ifdef CONFIG_SMP
  530. /* Get the mpic structure from the IPI number */
  531. static inline struct mpic * mpic_from_ipi(struct irq_data *d)
  532. {
  533. return irq_data_get_irq_chip_data(d);
  534. }
  535. #endif
  536. /* Get the mpic structure from the irq number */
  537. static inline struct mpic * mpic_from_irq(unsigned int irq)
  538. {
  539. return irq_get_chip_data(irq);
  540. }
  541. /* Get the mpic structure from the irq data */
  542. static inline struct mpic * mpic_from_irq_data(struct irq_data *d)
  543. {
  544. return irq_data_get_irq_chip_data(d);
  545. }
  546. /* Send an EOI */
  547. static inline void mpic_eoi(struct mpic *mpic)
  548. {
  549. mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
  550. (void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
  551. }
  552. /*
  553. * Linux descriptor level callbacks
  554. */
  555. void mpic_unmask_irq(struct irq_data *d)
  556. {
  557. unsigned int loops = 100000;
  558. struct mpic *mpic = mpic_from_irq_data(d);
  559. unsigned int src = irqd_to_hwirq(d);
  560. DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, d->irq, src);
  561. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  562. mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
  563. ~MPIC_VECPRI_MASK);
  564. /* make sure mask gets to controller before we return to user */
  565. do {
  566. if (!loops--) {
  567. printk(KERN_ERR "%s: timeout on hwirq %u\n",
  568. __func__, src);
  569. break;
  570. }
  571. } while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
  572. }
  573. void mpic_mask_irq(struct irq_data *d)
  574. {
  575. unsigned int loops = 100000;
  576. struct mpic *mpic = mpic_from_irq_data(d);
  577. unsigned int src = irqd_to_hwirq(d);
  578. DBG("%s: disable_irq: %d (src %d)\n", mpic->name, d->irq, src);
  579. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  580. mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
  581. MPIC_VECPRI_MASK);
  582. /* make sure mask gets to controller before we return to user */
  583. do {
  584. if (!loops--) {
  585. printk(KERN_ERR "%s: timeout on hwirq %u\n",
  586. __func__, src);
  587. break;
  588. }
  589. } while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
  590. }
  591. void mpic_end_irq(struct irq_data *d)
  592. {
  593. struct mpic *mpic = mpic_from_irq_data(d);
  594. #ifdef DEBUG_IRQ
  595. DBG("%s: end_irq: %d\n", mpic->name, d->irq);
  596. #endif
  597. /* We always EOI on end_irq() even for edge interrupts since that
  598. * should only lower the priority, the MPIC should have properly
  599. * latched another edge interrupt coming in anyway
  600. */
  601. mpic_eoi(mpic);
  602. }
  603. #ifdef CONFIG_MPIC_U3_HT_IRQS
  604. static void mpic_unmask_ht_irq(struct irq_data *d)
  605. {
  606. struct mpic *mpic = mpic_from_irq_data(d);
  607. unsigned int src = irqd_to_hwirq(d);
  608. mpic_unmask_irq(d);
  609. if (irqd_is_level_type(d))
  610. mpic_ht_end_irq(mpic, src);
  611. }
  612. static unsigned int mpic_startup_ht_irq(struct irq_data *d)
  613. {
  614. struct mpic *mpic = mpic_from_irq_data(d);
  615. unsigned int src = irqd_to_hwirq(d);
  616. mpic_unmask_irq(d);
  617. mpic_startup_ht_interrupt(mpic, src, irqd_is_level_type(d));
  618. return 0;
  619. }
  620. static void mpic_shutdown_ht_irq(struct irq_data *d)
  621. {
  622. struct mpic *mpic = mpic_from_irq_data(d);
  623. unsigned int src = irqd_to_hwirq(d);
  624. mpic_shutdown_ht_interrupt(mpic, src);
  625. mpic_mask_irq(d);
  626. }
  627. static void mpic_end_ht_irq(struct irq_data *d)
  628. {
  629. struct mpic *mpic = mpic_from_irq_data(d);
  630. unsigned int src = irqd_to_hwirq(d);
  631. #ifdef DEBUG_IRQ
  632. DBG("%s: end_irq: %d\n", mpic->name, d->irq);
  633. #endif
  634. /* We always EOI on end_irq() even for edge interrupts since that
  635. * should only lower the priority, the MPIC should have properly
  636. * latched another edge interrupt coming in anyway
  637. */
  638. if (irqd_is_level_type(d))
  639. mpic_ht_end_irq(mpic, src);
  640. mpic_eoi(mpic);
  641. }
  642. #endif /* !CONFIG_MPIC_U3_HT_IRQS */
  643. #ifdef CONFIG_SMP
  644. static void mpic_unmask_ipi(struct irq_data *d)
  645. {
  646. struct mpic *mpic = mpic_from_ipi(d);
  647. unsigned int src = virq_to_hw(d->irq) - mpic->ipi_vecs[0];
  648. DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, d->irq, src);
  649. mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
  650. }
  651. static void mpic_mask_ipi(struct irq_data *d)
  652. {
  653. /* NEVER disable an IPI... that's just plain wrong! */
  654. }
  655. static void mpic_end_ipi(struct irq_data *d)
  656. {
  657. struct mpic *mpic = mpic_from_ipi(d);
  658. /*
  659. * IPIs are marked IRQ_PER_CPU. This has the side effect of
  660. * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
  661. * applying to them. We EOI them late to avoid re-entering.
  662. */
  663. mpic_eoi(mpic);
  664. }
  665. #endif /* CONFIG_SMP */
  666. static void mpic_unmask_tm(struct irq_data *d)
  667. {
  668. struct mpic *mpic = mpic_from_irq_data(d);
  669. unsigned int src = virq_to_hw(d->irq) - mpic->timer_vecs[0];
  670. DBG("%s: enable_tm: %d (tm %d)\n", mpic->name, d->irq, src);
  671. mpic_tm_write(src, mpic_tm_read(src) & ~MPIC_VECPRI_MASK);
  672. mpic_tm_read(src);
  673. }
  674. static void mpic_mask_tm(struct irq_data *d)
  675. {
  676. struct mpic *mpic = mpic_from_irq_data(d);
  677. unsigned int src = virq_to_hw(d->irq) - mpic->timer_vecs[0];
  678. mpic_tm_write(src, mpic_tm_read(src) | MPIC_VECPRI_MASK);
  679. mpic_tm_read(src);
  680. }
  681. int mpic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
  682. bool force)
  683. {
  684. struct mpic *mpic = mpic_from_irq_data(d);
  685. unsigned int src = irqd_to_hwirq(d);
  686. if (mpic->flags & MPIC_SINGLE_DEST_CPU) {
  687. int cpuid = irq_choose_cpu(cpumask);
  688. mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
  689. } else {
  690. u32 mask = cpumask_bits(cpumask)[0];
  691. mask &= cpumask_bits(cpu_online_mask)[0];
  692. mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
  693. mpic_physmask(mask));
  694. }
  695. return 0;
  696. }
  697. static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
  698. {
  699. /* Now convert sense value */
  700. switch(type & IRQ_TYPE_SENSE_MASK) {
  701. case IRQ_TYPE_EDGE_RISING:
  702. return MPIC_INFO(VECPRI_SENSE_EDGE) |
  703. MPIC_INFO(VECPRI_POLARITY_POSITIVE);
  704. case IRQ_TYPE_EDGE_FALLING:
  705. case IRQ_TYPE_EDGE_BOTH:
  706. return MPIC_INFO(VECPRI_SENSE_EDGE) |
  707. MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
  708. case IRQ_TYPE_LEVEL_HIGH:
  709. return MPIC_INFO(VECPRI_SENSE_LEVEL) |
  710. MPIC_INFO(VECPRI_POLARITY_POSITIVE);
  711. case IRQ_TYPE_LEVEL_LOW:
  712. default:
  713. return MPIC_INFO(VECPRI_SENSE_LEVEL) |
  714. MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
  715. }
  716. }
  717. int mpic_set_irq_type(struct irq_data *d, unsigned int flow_type)
  718. {
  719. struct mpic *mpic = mpic_from_irq_data(d);
  720. unsigned int src = irqd_to_hwirq(d);
  721. unsigned int vecpri, vold, vnew;
  722. DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
  723. mpic, d->irq, src, flow_type);
  724. if (src >= mpic->irq_count)
  725. return -EINVAL;
  726. if (flow_type == IRQ_TYPE_NONE)
  727. if (mpic->senses && src < mpic->senses_count)
  728. flow_type = mpic->senses[src];
  729. if (flow_type == IRQ_TYPE_NONE)
  730. flow_type = IRQ_TYPE_LEVEL_LOW;
  731. irqd_set_trigger_type(d, flow_type);
  732. if (mpic_is_ht_interrupt(mpic, src))
  733. vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
  734. MPIC_VECPRI_SENSE_EDGE;
  735. else
  736. vecpri = mpic_type_to_vecpri(mpic, flow_type);
  737. vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
  738. vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
  739. MPIC_INFO(VECPRI_SENSE_MASK));
  740. vnew |= vecpri;
  741. if (vold != vnew)
  742. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
  743. return IRQ_SET_MASK_OK_NOCOPY;
  744. }
  745. void mpic_set_vector(unsigned int virq, unsigned int vector)
  746. {
  747. struct mpic *mpic = mpic_from_irq(virq);
  748. unsigned int src = virq_to_hw(virq);
  749. unsigned int vecpri;
  750. DBG("mpic: set_vector(mpic:@%p,virq:%d,src:%d,vector:0x%x)\n",
  751. mpic, virq, src, vector);
  752. if (src >= mpic->irq_count)
  753. return;
  754. vecpri = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
  755. vecpri = vecpri & ~MPIC_INFO(VECPRI_VECTOR_MASK);
  756. vecpri |= vector;
  757. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
  758. }
  759. void mpic_set_destination(unsigned int virq, unsigned int cpuid)
  760. {
  761. struct mpic *mpic = mpic_from_irq(virq);
  762. unsigned int src = virq_to_hw(virq);
  763. DBG("mpic: set_destination(mpic:@%p,virq:%d,src:%d,cpuid:0x%x)\n",
  764. mpic, virq, src, cpuid);
  765. if (src >= mpic->irq_count)
  766. return;
  767. mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION), 1 << cpuid);
  768. }
  769. static struct irq_chip mpic_irq_chip = {
  770. .irq_mask = mpic_mask_irq,
  771. .irq_unmask = mpic_unmask_irq,
  772. .irq_eoi = mpic_end_irq,
  773. .irq_set_type = mpic_set_irq_type,
  774. };
  775. #ifdef CONFIG_SMP
  776. static struct irq_chip mpic_ipi_chip = {
  777. .irq_mask = mpic_mask_ipi,
  778. .irq_unmask = mpic_unmask_ipi,
  779. .irq_eoi = mpic_end_ipi,
  780. };
  781. #endif /* CONFIG_SMP */
  782. static struct irq_chip mpic_tm_chip = {
  783. .irq_mask = mpic_mask_tm,
  784. .irq_unmask = mpic_unmask_tm,
  785. .irq_eoi = mpic_end_irq,
  786. };
  787. #ifdef CONFIG_MPIC_U3_HT_IRQS
  788. static struct irq_chip mpic_irq_ht_chip = {
  789. .irq_startup = mpic_startup_ht_irq,
  790. .irq_shutdown = mpic_shutdown_ht_irq,
  791. .irq_mask = mpic_mask_irq,
  792. .irq_unmask = mpic_unmask_ht_irq,
  793. .irq_eoi = mpic_end_ht_irq,
  794. .irq_set_type = mpic_set_irq_type,
  795. };
  796. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  797. static int mpic_host_match(struct irq_host *h, struct device_node *node)
  798. {
  799. /* Exact match, unless mpic node is NULL */
  800. return h->of_node == NULL || h->of_node == node;
  801. }
  802. static int mpic_host_map(struct irq_host *h, unsigned int virq,
  803. irq_hw_number_t hw)
  804. {
  805. struct mpic *mpic = h->host_data;
  806. struct irq_chip *chip;
  807. DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
  808. if (hw == mpic->spurious_vec)
  809. return -EINVAL;
  810. if (mpic->protected && test_bit(hw, mpic->protected))
  811. return -EINVAL;
  812. #ifdef CONFIG_SMP
  813. else if (hw >= mpic->ipi_vecs[0]) {
  814. WARN_ON(mpic->flags & MPIC_SECONDARY);
  815. DBG("mpic: mapping as IPI\n");
  816. irq_set_chip_data(virq, mpic);
  817. irq_set_chip_and_handler(virq, &mpic->hc_ipi,
  818. handle_percpu_irq);
  819. return 0;
  820. }
  821. #endif /* CONFIG_SMP */
  822. if (hw >= mpic->timer_vecs[0] && hw <= mpic->timer_vecs[7]) {
  823. WARN_ON(mpic->flags & MPIC_SECONDARY);
  824. DBG("mpic: mapping as timer\n");
  825. irq_set_chip_data(virq, mpic);
  826. irq_set_chip_and_handler(virq, &mpic->hc_tm,
  827. handle_fasteoi_irq);
  828. return 0;
  829. }
  830. if (hw >= mpic->irq_count)
  831. return -EINVAL;
  832. mpic_msi_reserve_hwirq(mpic, hw);
  833. /* Default chip */
  834. chip = &mpic->hc_irq;
  835. #ifdef CONFIG_MPIC_U3_HT_IRQS
  836. /* Check for HT interrupts, override vecpri */
  837. if (mpic_is_ht_interrupt(mpic, hw))
  838. chip = &mpic->hc_ht_irq;
  839. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  840. DBG("mpic: mapping to irq chip @%p\n", chip);
  841. irq_set_chip_data(virq, mpic);
  842. irq_set_chip_and_handler(virq, chip, handle_fasteoi_irq);
  843. /* Set default irq type */
  844. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  845. /* If the MPIC was reset, then all vectors have already been
  846. * initialized. Otherwise, a per source lazy initialization
  847. * is done here.
  848. */
  849. if (!mpic_is_ipi(mpic, hw) && (mpic->flags & MPIC_NO_RESET)) {
  850. mpic_set_vector(virq, hw);
  851. mpic_set_destination(virq, mpic_processor_id(mpic));
  852. mpic_irq_set_priority(virq, 8);
  853. }
  854. return 0;
  855. }
  856. static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
  857. const u32 *intspec, unsigned int intsize,
  858. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  859. {
  860. struct mpic *mpic = h->host_data;
  861. static unsigned char map_mpic_senses[4] = {
  862. IRQ_TYPE_EDGE_RISING,
  863. IRQ_TYPE_LEVEL_LOW,
  864. IRQ_TYPE_LEVEL_HIGH,
  865. IRQ_TYPE_EDGE_FALLING,
  866. };
  867. *out_hwirq = intspec[0];
  868. if (intsize >= 4 && (mpic->flags & MPIC_FSL)) {
  869. /*
  870. * Freescale MPIC with extended intspec:
  871. * First two cells are as usual. Third specifies
  872. * an "interrupt type". Fourth is type-specific data.
  873. *
  874. * See Documentation/devicetree/bindings/powerpc/fsl/mpic.txt
  875. */
  876. switch (intspec[2]) {
  877. case 0:
  878. case 1: /* no EISR/EIMR support for now, treat as shared IRQ */
  879. break;
  880. case 2:
  881. if (intspec[0] >= ARRAY_SIZE(mpic->ipi_vecs))
  882. return -EINVAL;
  883. *out_hwirq = mpic->ipi_vecs[intspec[0]];
  884. break;
  885. case 3:
  886. if (intspec[0] >= ARRAY_SIZE(mpic->timer_vecs))
  887. return -EINVAL;
  888. *out_hwirq = mpic->timer_vecs[intspec[0]];
  889. break;
  890. default:
  891. pr_debug("%s: unknown irq type %u\n",
  892. __func__, intspec[2]);
  893. return -EINVAL;
  894. }
  895. *out_flags = map_mpic_senses[intspec[1] & 3];
  896. } else if (intsize > 1) {
  897. u32 mask = 0x3;
  898. /* Apple invented a new race of encoding on machines with
  899. * an HT APIC. They encode, among others, the index within
  900. * the HT APIC. We don't care about it here since thankfully,
  901. * it appears that they have the APIC already properly
  902. * configured, and thus our current fixup code that reads the
  903. * APIC config works fine. However, we still need to mask out
  904. * bits in the specifier to make sure we only get bit 0 which
  905. * is the level/edge bit (the only sense bit exposed by Apple),
  906. * as their bit 1 means something else.
  907. */
  908. if (machine_is(powermac))
  909. mask = 0x1;
  910. *out_flags = map_mpic_senses[intspec[1] & mask];
  911. } else
  912. *out_flags = IRQ_TYPE_NONE;
  913. DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
  914. intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
  915. return 0;
  916. }
  917. static struct irq_host_ops mpic_host_ops = {
  918. .match = mpic_host_match,
  919. .map = mpic_host_map,
  920. .xlate = mpic_host_xlate,
  921. };
  922. static int mpic_reset_prohibited(struct device_node *node)
  923. {
  924. return node && of_get_property(node, "pic-no-reset", NULL);
  925. }
  926. /*
  927. * Exported functions
  928. */
  929. struct mpic * __init mpic_alloc(struct device_node *node,
  930. phys_addr_t phys_addr,
  931. unsigned int flags,
  932. unsigned int isu_size,
  933. unsigned int irq_count,
  934. const char *name)
  935. {
  936. int i, psize, intvec_top;
  937. struct mpic *mpic;
  938. u32 greg_feature;
  939. const char *vers;
  940. const u32 *psrc;
  941. /* Default MPIC search parameters */
  942. static const struct of_device_id __initconst mpic_device_id[] = {
  943. { .type = "open-pic", },
  944. { .compatible = "open-pic", },
  945. {},
  946. };
  947. /*
  948. * If we were not passed a device-tree node, then perform the default
  949. * search for standardized a standardized OpenPIC.
  950. */
  951. if (node) {
  952. node = of_node_get(node);
  953. } else {
  954. node = of_find_matching_node(NULL, mpic_device_id);
  955. if (!node)
  956. return NULL;
  957. }
  958. /* Pick the physical address from the device tree if unspecified */
  959. if (!phys_addr) {
  960. /* Check if it is DCR-based */
  961. if (of_get_property(node, "dcr-reg", NULL)) {
  962. flags |= MPIC_USES_DCR;
  963. } else {
  964. struct resource r;
  965. if (of_address_to_resource(node, 0, &r))
  966. goto err_of_node_put;
  967. phys_addr = r.start;
  968. }
  969. }
  970. mpic = kzalloc(sizeof(struct mpic), GFP_KERNEL);
  971. if (mpic == NULL)
  972. goto err_of_node_put;
  973. mpic->name = name;
  974. mpic->paddr = phys_addr;
  975. mpic->hc_irq = mpic_irq_chip;
  976. mpic->hc_irq.name = name;
  977. if (!(flags & MPIC_SECONDARY))
  978. mpic->hc_irq.irq_set_affinity = mpic_set_affinity;
  979. #ifdef CONFIG_MPIC_U3_HT_IRQS
  980. mpic->hc_ht_irq = mpic_irq_ht_chip;
  981. mpic->hc_ht_irq.name = name;
  982. if (!(flags & MPIC_SECONDARY))
  983. mpic->hc_ht_irq.irq_set_affinity = mpic_set_affinity;
  984. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  985. #ifdef CONFIG_SMP
  986. mpic->hc_ipi = mpic_ipi_chip;
  987. mpic->hc_ipi.name = name;
  988. #endif /* CONFIG_SMP */
  989. mpic->hc_tm = mpic_tm_chip;
  990. mpic->hc_tm.name = name;
  991. mpic->flags = flags;
  992. mpic->isu_size = isu_size;
  993. mpic->irq_count = irq_count;
  994. mpic->num_sources = 0; /* so far */
  995. if (flags & MPIC_LARGE_VECTORS)
  996. intvec_top = 2047;
  997. else
  998. intvec_top = 255;
  999. mpic->timer_vecs[0] = intvec_top - 12;
  1000. mpic->timer_vecs[1] = intvec_top - 11;
  1001. mpic->timer_vecs[2] = intvec_top - 10;
  1002. mpic->timer_vecs[3] = intvec_top - 9;
  1003. mpic->timer_vecs[4] = intvec_top - 8;
  1004. mpic->timer_vecs[5] = intvec_top - 7;
  1005. mpic->timer_vecs[6] = intvec_top - 6;
  1006. mpic->timer_vecs[7] = intvec_top - 5;
  1007. mpic->ipi_vecs[0] = intvec_top - 4;
  1008. mpic->ipi_vecs[1] = intvec_top - 3;
  1009. mpic->ipi_vecs[2] = intvec_top - 2;
  1010. mpic->ipi_vecs[3] = intvec_top - 1;
  1011. mpic->spurious_vec = intvec_top;
  1012. /* Check for "big-endian" in device-tree */
  1013. if (of_get_property(node, "big-endian", NULL) != NULL)
  1014. mpic->flags |= MPIC_BIG_ENDIAN;
  1015. if (of_device_is_compatible(node, "fsl,mpic"))
  1016. mpic->flags |= MPIC_FSL;
  1017. /* Look for protected sources */
  1018. psrc = of_get_property(node, "protected-sources", &psize);
  1019. if (psrc) {
  1020. /* Allocate a bitmap with one bit per interrupt */
  1021. unsigned int mapsize = BITS_TO_LONGS(intvec_top + 1);
  1022. mpic->protected = kzalloc(mapsize*sizeof(long), GFP_KERNEL);
  1023. BUG_ON(mpic->protected == NULL);
  1024. for (i = 0; i < psize/sizeof(u32); i++) {
  1025. if (psrc[i] > intvec_top)
  1026. continue;
  1027. __set_bit(psrc[i], mpic->protected);
  1028. }
  1029. }
  1030. #ifdef CONFIG_MPIC_WEIRD
  1031. mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
  1032. #endif
  1033. /* default register type */
  1034. if (flags & MPIC_BIG_ENDIAN)
  1035. mpic->reg_type = mpic_access_mmio_be;
  1036. else
  1037. mpic->reg_type = mpic_access_mmio_le;
  1038. /*
  1039. * An MPIC with a "dcr-reg" property must be accessed that way, but
  1040. * only if the kernel includes DCR support.
  1041. */
  1042. #ifdef CONFIG_PPC_DCR
  1043. if (flags & MPIC_USES_DCR)
  1044. mpic->reg_type = mpic_access_dcr;
  1045. #else
  1046. BUG_ON(flags & MPIC_USES_DCR);
  1047. #endif
  1048. /* Map the global registers */
  1049. mpic_map(mpic, node, mpic->paddr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
  1050. mpic_map(mpic, node, mpic->paddr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
  1051. /* Reset */
  1052. /* When using a device-node, reset requests are only honored if the MPIC
  1053. * is allowed to reset.
  1054. */
  1055. if (mpic_reset_prohibited(node))
  1056. mpic->flags |= MPIC_NO_RESET;
  1057. if ((flags & MPIC_WANTS_RESET) && !(mpic->flags & MPIC_NO_RESET)) {
  1058. printk(KERN_DEBUG "mpic: Resetting\n");
  1059. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1060. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1061. | MPIC_GREG_GCONF_RESET);
  1062. while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1063. & MPIC_GREG_GCONF_RESET)
  1064. mb();
  1065. }
  1066. /* CoreInt */
  1067. if (flags & MPIC_ENABLE_COREINT)
  1068. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1069. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1070. | MPIC_GREG_GCONF_COREINT);
  1071. if (flags & MPIC_ENABLE_MCK)
  1072. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1073. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1074. | MPIC_GREG_GCONF_MCK);
  1075. /*
  1076. * Read feature register. For non-ISU MPICs, num sources as well. On
  1077. * ISU MPICs, sources are counted as ISUs are added
  1078. */
  1079. greg_feature = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
  1080. if (isu_size == 0) {
  1081. if (flags & MPIC_BROKEN_FRR_NIRQS)
  1082. mpic->num_sources = mpic->irq_count;
  1083. else
  1084. mpic->num_sources =
  1085. ((greg_feature & MPIC_GREG_FEATURE_LAST_SRC_MASK)
  1086. >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
  1087. }
  1088. /*
  1089. * The MPIC driver will crash if there are more cores than we
  1090. * can initialize, so we may as well catch that problem here.
  1091. */
  1092. BUG_ON(num_possible_cpus() > MPIC_MAX_CPUS);
  1093. /* Map the per-CPU registers */
  1094. for_each_possible_cpu(i) {
  1095. unsigned int cpu = get_hard_smp_processor_id(i);
  1096. mpic_map(mpic, node, mpic->paddr, &mpic->cpuregs[cpu],
  1097. MPIC_INFO(CPU_BASE) + cpu * MPIC_INFO(CPU_STRIDE),
  1098. 0x1000);
  1099. }
  1100. /* Initialize main ISU if none provided */
  1101. if (mpic->isu_size == 0) {
  1102. mpic->isu_size = mpic->num_sources;
  1103. mpic_map(mpic, node, mpic->paddr, &mpic->isus[0],
  1104. MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
  1105. }
  1106. mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
  1107. mpic->isu_mask = (1 << mpic->isu_shift) - 1;
  1108. mpic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
  1109. isu_size ? isu_size : mpic->num_sources,
  1110. &mpic_host_ops,
  1111. flags & MPIC_LARGE_VECTORS ? 2048 : 256);
  1112. /*
  1113. * FIXME: The code leaks the MPIC object and mappings here; this
  1114. * is very unlikely to fail but it ought to be fixed anyways.
  1115. */
  1116. if (mpic->irqhost == NULL)
  1117. return NULL;
  1118. mpic->irqhost->host_data = mpic;
  1119. /* Display version */
  1120. switch (greg_feature & MPIC_GREG_FEATURE_VERSION_MASK) {
  1121. case 1:
  1122. vers = "1.0";
  1123. break;
  1124. case 2:
  1125. vers = "1.2";
  1126. break;
  1127. case 3:
  1128. vers = "1.3";
  1129. break;
  1130. default:
  1131. vers = "<unknown>";
  1132. break;
  1133. }
  1134. printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
  1135. " max %d CPUs\n",
  1136. name, vers, (unsigned long long)mpic->paddr, num_possible_cpus());
  1137. printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
  1138. mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
  1139. mpic->next = mpics;
  1140. mpics = mpic;
  1141. if (!(flags & MPIC_SECONDARY)) {
  1142. mpic_primary = mpic;
  1143. irq_set_default_host(mpic->irqhost);
  1144. }
  1145. of_node_put(node);
  1146. return mpic;
  1147. err_of_node_put:
  1148. of_node_put(node);
  1149. return NULL;
  1150. }
  1151. void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
  1152. phys_addr_t paddr)
  1153. {
  1154. unsigned int isu_first = isu_num * mpic->isu_size;
  1155. BUG_ON(isu_num >= MPIC_MAX_ISU);
  1156. mpic_map(mpic, mpic->irqhost->of_node,
  1157. paddr, &mpic->isus[isu_num], 0,
  1158. MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
  1159. if ((isu_first + mpic->isu_size) > mpic->num_sources)
  1160. mpic->num_sources = isu_first + mpic->isu_size;
  1161. }
  1162. void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
  1163. {
  1164. mpic->senses = senses;
  1165. mpic->senses_count = count;
  1166. }
  1167. void __init mpic_init(struct mpic *mpic)
  1168. {
  1169. int i;
  1170. int cpu;
  1171. BUG_ON(mpic->num_sources == 0);
  1172. printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
  1173. /* Set current processor priority to max */
  1174. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
  1175. /* Initialize timers to our reserved vectors and mask them for now */
  1176. for (i = 0; i < 4; i++) {
  1177. mpic_write(mpic->tmregs,
  1178. i * MPIC_INFO(TIMER_STRIDE) +
  1179. MPIC_INFO(TIMER_DESTINATION),
  1180. 1 << hard_smp_processor_id());
  1181. mpic_write(mpic->tmregs,
  1182. i * MPIC_INFO(TIMER_STRIDE) +
  1183. MPIC_INFO(TIMER_VECTOR_PRI),
  1184. MPIC_VECPRI_MASK |
  1185. (9 << MPIC_VECPRI_PRIORITY_SHIFT) |
  1186. (mpic->timer_vecs[0] + i));
  1187. }
  1188. /* Initialize IPIs to our reserved vectors and mark them disabled for now */
  1189. mpic_test_broken_ipi(mpic);
  1190. for (i = 0; i < 4; i++) {
  1191. mpic_ipi_write(i,
  1192. MPIC_VECPRI_MASK |
  1193. (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
  1194. (mpic->ipi_vecs[0] + i));
  1195. }
  1196. /* Initialize interrupt sources */
  1197. if (mpic->irq_count == 0)
  1198. mpic->irq_count = mpic->num_sources;
  1199. /* Do the HT PIC fixups on U3 broken mpic */
  1200. DBG("MPIC flags: %x\n", mpic->flags);
  1201. if ((mpic->flags & MPIC_U3_HT_IRQS) && !(mpic->flags & MPIC_SECONDARY)) {
  1202. mpic_scan_ht_pics(mpic);
  1203. mpic_u3msi_init(mpic);
  1204. }
  1205. mpic_pasemi_msi_init(mpic);
  1206. cpu = mpic_processor_id(mpic);
  1207. if (!(mpic->flags & MPIC_NO_RESET)) {
  1208. for (i = 0; i < mpic->num_sources; i++) {
  1209. /* start with vector = source number, and masked */
  1210. u32 vecpri = MPIC_VECPRI_MASK | i |
  1211. (8 << MPIC_VECPRI_PRIORITY_SHIFT);
  1212. /* check if protected */
  1213. if (mpic->protected && test_bit(i, mpic->protected))
  1214. continue;
  1215. /* init hw */
  1216. mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
  1217. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION), 1 << cpu);
  1218. }
  1219. }
  1220. /* Init spurious vector */
  1221. mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
  1222. /* Disable 8259 passthrough, if supported */
  1223. if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
  1224. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1225. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1226. | MPIC_GREG_GCONF_8259_PTHROU_DIS);
  1227. if (mpic->flags & MPIC_NO_BIAS)
  1228. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1229. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1230. | MPIC_GREG_GCONF_NO_BIAS);
  1231. /* Set current processor priority to 0 */
  1232. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
  1233. #ifdef CONFIG_PM
  1234. /* allocate memory to save mpic state */
  1235. mpic->save_data = kmalloc(mpic->num_sources * sizeof(*mpic->save_data),
  1236. GFP_KERNEL);
  1237. BUG_ON(mpic->save_data == NULL);
  1238. #endif
  1239. }
  1240. void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
  1241. {
  1242. u32 v;
  1243. v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
  1244. v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
  1245. v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
  1246. mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
  1247. }
  1248. void __init mpic_set_serial_int(struct mpic *mpic, int enable)
  1249. {
  1250. unsigned long flags;
  1251. u32 v;
  1252. raw_spin_lock_irqsave(&mpic_lock, flags);
  1253. v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
  1254. if (enable)
  1255. v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
  1256. else
  1257. v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
  1258. mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
  1259. raw_spin_unlock_irqrestore(&mpic_lock, flags);
  1260. }
  1261. void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
  1262. {
  1263. struct mpic *mpic = mpic_find(irq);
  1264. unsigned int src = virq_to_hw(irq);
  1265. unsigned long flags;
  1266. u32 reg;
  1267. if (!mpic)
  1268. return;
  1269. raw_spin_lock_irqsave(&mpic_lock, flags);
  1270. if (mpic_is_ipi(mpic, irq)) {
  1271. reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
  1272. ~MPIC_VECPRI_PRIORITY_MASK;
  1273. mpic_ipi_write(src - mpic->ipi_vecs[0],
  1274. reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
  1275. } else if (mpic_is_tm(mpic, irq)) {
  1276. reg = mpic_tm_read(src - mpic->timer_vecs[0]) &
  1277. ~MPIC_VECPRI_PRIORITY_MASK;
  1278. mpic_tm_write(src - mpic->timer_vecs[0],
  1279. reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
  1280. } else {
  1281. reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
  1282. & ~MPIC_VECPRI_PRIORITY_MASK;
  1283. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  1284. reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
  1285. }
  1286. raw_spin_unlock_irqrestore(&mpic_lock, flags);
  1287. }
  1288. void mpic_setup_this_cpu(void)
  1289. {
  1290. #ifdef CONFIG_SMP
  1291. struct mpic *mpic = mpic_primary;
  1292. unsigned long flags;
  1293. u32 msk = 1 << hard_smp_processor_id();
  1294. unsigned int i;
  1295. BUG_ON(mpic == NULL);
  1296. DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
  1297. raw_spin_lock_irqsave(&mpic_lock, flags);
  1298. /* let the mpic know we want intrs. default affinity is 0xffffffff
  1299. * until changed via /proc. That's how it's done on x86. If we want
  1300. * it differently, then we should make sure we also change the default
  1301. * values of irq_desc[].affinity in irq.c.
  1302. */
  1303. if (distribute_irqs) {
  1304. for (i = 0; i < mpic->num_sources ; i++)
  1305. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1306. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
  1307. }
  1308. /* Set current processor priority to 0 */
  1309. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
  1310. raw_spin_unlock_irqrestore(&mpic_lock, flags);
  1311. #endif /* CONFIG_SMP */
  1312. }
  1313. int mpic_cpu_get_priority(void)
  1314. {
  1315. struct mpic *mpic = mpic_primary;
  1316. return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
  1317. }
  1318. void mpic_cpu_set_priority(int prio)
  1319. {
  1320. struct mpic *mpic = mpic_primary;
  1321. prio &= MPIC_CPU_TASKPRI_MASK;
  1322. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
  1323. }
  1324. void mpic_teardown_this_cpu(int secondary)
  1325. {
  1326. struct mpic *mpic = mpic_primary;
  1327. unsigned long flags;
  1328. u32 msk = 1 << hard_smp_processor_id();
  1329. unsigned int i;
  1330. BUG_ON(mpic == NULL);
  1331. DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
  1332. raw_spin_lock_irqsave(&mpic_lock, flags);
  1333. /* let the mpic know we don't want intrs. */
  1334. for (i = 0; i < mpic->num_sources ; i++)
  1335. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1336. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
  1337. /* Set current processor priority to max */
  1338. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
  1339. /* We need to EOI the IPI since not all platforms reset the MPIC
  1340. * on boot and new interrupts wouldn't get delivered otherwise.
  1341. */
  1342. mpic_eoi(mpic);
  1343. raw_spin_unlock_irqrestore(&mpic_lock, flags);
  1344. }
  1345. static unsigned int _mpic_get_one_irq(struct mpic *mpic, int reg)
  1346. {
  1347. u32 src;
  1348. src = mpic_cpu_read(reg) & MPIC_INFO(VECPRI_VECTOR_MASK);
  1349. #ifdef DEBUG_LOW
  1350. DBG("%s: get_one_irq(reg 0x%x): %d\n", mpic->name, reg, src);
  1351. #endif
  1352. if (unlikely(src == mpic->spurious_vec)) {
  1353. if (mpic->flags & MPIC_SPV_EOI)
  1354. mpic_eoi(mpic);
  1355. return NO_IRQ;
  1356. }
  1357. if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
  1358. printk_ratelimited(KERN_WARNING "%s: Got protected source %d !\n",
  1359. mpic->name, (int)src);
  1360. mpic_eoi(mpic);
  1361. return NO_IRQ;
  1362. }
  1363. return irq_linear_revmap(mpic->irqhost, src);
  1364. }
  1365. unsigned int mpic_get_one_irq(struct mpic *mpic)
  1366. {
  1367. return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_INTACK));
  1368. }
  1369. unsigned int mpic_get_irq(void)
  1370. {
  1371. struct mpic *mpic = mpic_primary;
  1372. BUG_ON(mpic == NULL);
  1373. return mpic_get_one_irq(mpic);
  1374. }
  1375. unsigned int mpic_get_coreint_irq(void)
  1376. {
  1377. #ifdef CONFIG_BOOKE
  1378. struct mpic *mpic = mpic_primary;
  1379. u32 src;
  1380. BUG_ON(mpic == NULL);
  1381. src = mfspr(SPRN_EPR);
  1382. if (unlikely(src == mpic->spurious_vec)) {
  1383. if (mpic->flags & MPIC_SPV_EOI)
  1384. mpic_eoi(mpic);
  1385. return NO_IRQ;
  1386. }
  1387. if (unlikely(mpic->protected && test_bit(src, mpic->protected))) {
  1388. printk_ratelimited(KERN_WARNING "%s: Got protected source %d !\n",
  1389. mpic->name, (int)src);
  1390. return NO_IRQ;
  1391. }
  1392. return irq_linear_revmap(mpic->irqhost, src);
  1393. #else
  1394. return NO_IRQ;
  1395. #endif
  1396. }
  1397. unsigned int mpic_get_mcirq(void)
  1398. {
  1399. struct mpic *mpic = mpic_primary;
  1400. BUG_ON(mpic == NULL);
  1401. return _mpic_get_one_irq(mpic, MPIC_INFO(CPU_MCACK));
  1402. }
  1403. #ifdef CONFIG_SMP
  1404. void mpic_request_ipis(void)
  1405. {
  1406. struct mpic *mpic = mpic_primary;
  1407. int i;
  1408. BUG_ON(mpic == NULL);
  1409. printk(KERN_INFO "mpic: requesting IPIs...\n");
  1410. for (i = 0; i < 4; i++) {
  1411. unsigned int vipi = irq_create_mapping(mpic->irqhost,
  1412. mpic->ipi_vecs[0] + i);
  1413. if (vipi == NO_IRQ) {
  1414. printk(KERN_ERR "Failed to map %s\n", smp_ipi_name[i]);
  1415. continue;
  1416. }
  1417. smp_request_message_ipi(vipi, i);
  1418. }
  1419. }
  1420. void smp_mpic_message_pass(int cpu, int msg)
  1421. {
  1422. struct mpic *mpic = mpic_primary;
  1423. u32 physmask;
  1424. BUG_ON(mpic == NULL);
  1425. /* make sure we're sending something that translates to an IPI */
  1426. if ((unsigned int)msg > 3) {
  1427. printk("SMP %d: smp_message_pass: unknown msg %d\n",
  1428. smp_processor_id(), msg);
  1429. return;
  1430. }
  1431. #ifdef DEBUG_IPI
  1432. DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, msg);
  1433. #endif
  1434. physmask = 1 << get_hard_smp_processor_id(cpu);
  1435. mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
  1436. msg * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE), physmask);
  1437. }
  1438. int __init smp_mpic_probe(void)
  1439. {
  1440. int nr_cpus;
  1441. DBG("smp_mpic_probe()...\n");
  1442. nr_cpus = cpumask_weight(cpu_possible_mask);
  1443. DBG("nr_cpus: %d\n", nr_cpus);
  1444. if (nr_cpus > 1)
  1445. mpic_request_ipis();
  1446. return nr_cpus;
  1447. }
  1448. void __devinit smp_mpic_setup_cpu(int cpu)
  1449. {
  1450. mpic_setup_this_cpu();
  1451. }
  1452. void mpic_reset_core(int cpu)
  1453. {
  1454. struct mpic *mpic = mpic_primary;
  1455. u32 pir;
  1456. int cpuid = get_hard_smp_processor_id(cpu);
  1457. int i;
  1458. /* Set target bit for core reset */
  1459. pir = mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
  1460. pir |= (1 << cpuid);
  1461. mpic_write(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT), pir);
  1462. mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
  1463. /* Restore target bit after reset complete */
  1464. pir &= ~(1 << cpuid);
  1465. mpic_write(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT), pir);
  1466. mpic_read(mpic->gregs, MPIC_INFO(GREG_PROCESSOR_INIT));
  1467. /* Perform 15 EOI on each reset core to clear pending interrupts.
  1468. * This is required for FSL CoreNet based devices */
  1469. if (mpic->flags & MPIC_FSL) {
  1470. for (i = 0; i < 15; i++) {
  1471. _mpic_write(mpic->reg_type, &mpic->cpuregs[cpuid],
  1472. MPIC_CPU_EOI, 0);
  1473. }
  1474. }
  1475. }
  1476. #endif /* CONFIG_SMP */
  1477. #ifdef CONFIG_PM
  1478. static void mpic_suspend_one(struct mpic *mpic)
  1479. {
  1480. int i;
  1481. for (i = 0; i < mpic->num_sources; i++) {
  1482. mpic->save_data[i].vecprio =
  1483. mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
  1484. mpic->save_data[i].dest =
  1485. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
  1486. }
  1487. }
  1488. static int mpic_suspend(void)
  1489. {
  1490. struct mpic *mpic = mpics;
  1491. while (mpic) {
  1492. mpic_suspend_one(mpic);
  1493. mpic = mpic->next;
  1494. }
  1495. return 0;
  1496. }
  1497. static void mpic_resume_one(struct mpic *mpic)
  1498. {
  1499. int i;
  1500. for (i = 0; i < mpic->num_sources; i++) {
  1501. mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
  1502. mpic->save_data[i].vecprio);
  1503. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1504. mpic->save_data[i].dest);
  1505. #ifdef CONFIG_MPIC_U3_HT_IRQS
  1506. if (mpic->fixups) {
  1507. struct mpic_irq_fixup *fixup = &mpic->fixups[i];
  1508. if (fixup->base) {
  1509. /* we use the lowest bit in an inverted meaning */
  1510. if ((mpic->save_data[i].fixup_data & 1) == 0)
  1511. continue;
  1512. /* Enable and configure */
  1513. writeb(0x10 + 2 * fixup->index, fixup->base + 2);
  1514. writel(mpic->save_data[i].fixup_data & ~1,
  1515. fixup->base + 4);
  1516. }
  1517. }
  1518. #endif
  1519. } /* end for loop */
  1520. }
  1521. static void mpic_resume(void)
  1522. {
  1523. struct mpic *mpic = mpics;
  1524. while (mpic) {
  1525. mpic_resume_one(mpic);
  1526. mpic = mpic->next;
  1527. }
  1528. }
  1529. static struct syscore_ops mpic_syscore_ops = {
  1530. .resume = mpic_resume,
  1531. .suspend = mpic_suspend,
  1532. };
  1533. static int mpic_init_sys(void)
  1534. {
  1535. register_syscore_ops(&mpic_syscore_ops);
  1536. return 0;
  1537. }
  1538. device_initcall(mpic_init_sys);
  1539. #endif