mpic.c 35 KB

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