mpic.c 47 KB

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