mpic.c 40 KB

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