mpic.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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_U3_HT_IRQS
  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. #ifdef CONFIG_PM
  304. /* use the lowest bit inverted to the actual HW,
  305. * set if this fixup was enabled, clear otherwise */
  306. mpic->save_data[source].fixup_data = tmp | 1;
  307. #endif
  308. }
  309. static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
  310. unsigned int irqflags)
  311. {
  312. struct mpic_irq_fixup *fixup = &mpic->fixups[source];
  313. unsigned long flags;
  314. u32 tmp;
  315. if (fixup->base == NULL)
  316. return;
  317. DBG("shutdown_ht_interrupt(0x%x, 0x%x)\n", source, irqflags);
  318. /* Disable */
  319. spin_lock_irqsave(&mpic->fixup_lock, flags);
  320. writeb(0x10 + 2 * fixup->index, fixup->base + 2);
  321. tmp = readl(fixup->base + 4);
  322. tmp |= 1;
  323. writel(tmp, fixup->base + 4);
  324. spin_unlock_irqrestore(&mpic->fixup_lock, flags);
  325. #ifdef CONFIG_PM
  326. /* use the lowest bit inverted to the actual HW,
  327. * set if this fixup was enabled, clear otherwise */
  328. mpic->save_data[source].fixup_data = tmp & ~1;
  329. #endif
  330. }
  331. #ifdef CONFIG_PCI_MSI
  332. static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
  333. unsigned int devfn)
  334. {
  335. u8 __iomem *base;
  336. u8 pos, flags;
  337. u64 addr = 0;
  338. for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
  339. pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
  340. u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
  341. if (id == PCI_CAP_ID_HT) {
  342. id = readb(devbase + pos + 3);
  343. if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_MSI_MAPPING)
  344. break;
  345. }
  346. }
  347. if (pos == 0)
  348. return;
  349. base = devbase + pos;
  350. flags = readb(base + HT_MSI_FLAGS);
  351. if (!(flags & HT_MSI_FLAGS_FIXED)) {
  352. addr = readl(base + HT_MSI_ADDR_LO) & HT_MSI_ADDR_LO_MASK;
  353. addr = addr | ((u64)readl(base + HT_MSI_ADDR_HI) << 32);
  354. }
  355. printk(KERN_DEBUG "mpic: - HT:%02x.%x %s MSI mapping found @ 0x%lx\n",
  356. PCI_SLOT(devfn), PCI_FUNC(devfn),
  357. flags & HT_MSI_FLAGS_ENABLE ? "enabled" : "disabled", addr);
  358. if (!(flags & HT_MSI_FLAGS_ENABLE))
  359. writeb(flags | HT_MSI_FLAGS_ENABLE, base + HT_MSI_FLAGS);
  360. }
  361. #else
  362. static void __init mpic_scan_ht_msi(struct mpic *mpic, u8 __iomem *devbase,
  363. unsigned int devfn)
  364. {
  365. return;
  366. }
  367. #endif
  368. static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
  369. unsigned int devfn, u32 vdid)
  370. {
  371. int i, irq, n;
  372. u8 __iomem *base;
  373. u32 tmp;
  374. u8 pos;
  375. for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
  376. pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
  377. u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
  378. if (id == PCI_CAP_ID_HT) {
  379. id = readb(devbase + pos + 3);
  380. if ((id & HT_5BIT_CAP_MASK) == HT_CAPTYPE_IRQ)
  381. break;
  382. }
  383. }
  384. if (pos == 0)
  385. return;
  386. base = devbase + pos;
  387. writeb(0x01, base + 2);
  388. n = (readl(base + 4) >> 16) & 0xff;
  389. printk(KERN_INFO "mpic: - HT:%02x.%x [0x%02x] vendor %04x device %04x"
  390. " has %d irqs\n",
  391. devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
  392. for (i = 0; i <= n; i++) {
  393. writeb(0x10 + 2 * i, base + 2);
  394. tmp = readl(base + 4);
  395. irq = (tmp >> 16) & 0xff;
  396. DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
  397. /* mask it , will be unmasked later */
  398. tmp |= 0x1;
  399. writel(tmp, base + 4);
  400. mpic->fixups[irq].index = i;
  401. mpic->fixups[irq].base = base;
  402. /* Apple HT PIC has a non-standard way of doing EOIs */
  403. if ((vdid & 0xffff) == 0x106b)
  404. mpic->fixups[irq].applebase = devbase + 0x60;
  405. else
  406. mpic->fixups[irq].applebase = NULL;
  407. writeb(0x11 + 2 * i, base + 2);
  408. mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
  409. }
  410. }
  411. static void __init mpic_scan_ht_pics(struct mpic *mpic)
  412. {
  413. unsigned int devfn;
  414. u8 __iomem *cfgspace;
  415. printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
  416. /* Allocate fixups array */
  417. mpic->fixups = alloc_bootmem(128 * sizeof(struct mpic_irq_fixup));
  418. BUG_ON(mpic->fixups == NULL);
  419. memset(mpic->fixups, 0, 128 * sizeof(struct mpic_irq_fixup));
  420. /* Init spinlock */
  421. spin_lock_init(&mpic->fixup_lock);
  422. /* Map U3 config space. We assume all IO-APICs are on the primary bus
  423. * so we only need to map 64kB.
  424. */
  425. cfgspace = ioremap(0xf2000000, 0x10000);
  426. BUG_ON(cfgspace == NULL);
  427. /* Now we scan all slots. We do a very quick scan, we read the header
  428. * type, vendor ID and device ID only, that's plenty enough
  429. */
  430. for (devfn = 0; devfn < 0x100; devfn++) {
  431. u8 __iomem *devbase = cfgspace + (devfn << 8);
  432. u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
  433. u32 l = readl(devbase + PCI_VENDOR_ID);
  434. u16 s;
  435. DBG("devfn %x, l: %x\n", devfn, l);
  436. /* If no device, skip */
  437. if (l == 0xffffffff || l == 0x00000000 ||
  438. l == 0x0000ffff || l == 0xffff0000)
  439. goto next;
  440. /* Check if is supports capability lists */
  441. s = readw(devbase + PCI_STATUS);
  442. if (!(s & PCI_STATUS_CAP_LIST))
  443. goto next;
  444. mpic_scan_ht_pic(mpic, devbase, devfn, l);
  445. mpic_scan_ht_msi(mpic, devbase, devfn);
  446. next:
  447. /* next device, if function 0 */
  448. if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
  449. devfn += 7;
  450. }
  451. }
  452. #else /* CONFIG_MPIC_U3_HT_IRQS */
  453. static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
  454. {
  455. return 0;
  456. }
  457. static void __init mpic_scan_ht_pics(struct mpic *mpic)
  458. {
  459. }
  460. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  461. #define mpic_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
  462. /* Find an mpic associated with a given linux interrupt */
  463. static struct mpic *mpic_find(unsigned int irq, unsigned int *is_ipi)
  464. {
  465. unsigned int src = mpic_irq_to_hw(irq);
  466. struct mpic *mpic;
  467. if (irq < NUM_ISA_INTERRUPTS)
  468. return NULL;
  469. mpic = irq_desc[irq].chip_data;
  470. if (is_ipi)
  471. *is_ipi = (src >= mpic->ipi_vecs[0] &&
  472. src <= mpic->ipi_vecs[3]);
  473. return mpic;
  474. }
  475. /* Convert a cpu mask from logical to physical cpu numbers. */
  476. static inline u32 mpic_physmask(u32 cpumask)
  477. {
  478. int i;
  479. u32 mask = 0;
  480. for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
  481. mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
  482. return mask;
  483. }
  484. #ifdef CONFIG_SMP
  485. /* Get the mpic structure from the IPI number */
  486. static inline struct mpic * mpic_from_ipi(unsigned int ipi)
  487. {
  488. return irq_desc[ipi].chip_data;
  489. }
  490. #endif
  491. /* Get the mpic structure from the irq number */
  492. static inline struct mpic * mpic_from_irq(unsigned int irq)
  493. {
  494. return irq_desc[irq].chip_data;
  495. }
  496. /* Send an EOI */
  497. static inline void mpic_eoi(struct mpic *mpic)
  498. {
  499. mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
  500. (void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
  501. }
  502. #ifdef CONFIG_SMP
  503. static irqreturn_t mpic_ipi_action(int irq, void *dev_id)
  504. {
  505. struct mpic *mpic;
  506. mpic = mpic_find(irq, NULL);
  507. smp_message_recv(mpic_irq_to_hw(irq) - mpic->ipi_vecs[0]);
  508. return IRQ_HANDLED;
  509. }
  510. #endif /* CONFIG_SMP */
  511. /*
  512. * Linux descriptor level callbacks
  513. */
  514. static void mpic_unmask_irq(unsigned int irq)
  515. {
  516. unsigned int loops = 100000;
  517. struct mpic *mpic = mpic_from_irq(irq);
  518. unsigned int src = mpic_irq_to_hw(irq);
  519. DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
  520. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  521. mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
  522. ~MPIC_VECPRI_MASK);
  523. /* make sure mask gets to controller before we return to user */
  524. do {
  525. if (!loops--) {
  526. printk(KERN_ERR "mpic_enable_irq timeout\n");
  527. break;
  528. }
  529. } while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
  530. }
  531. static void mpic_mask_irq(unsigned int irq)
  532. {
  533. unsigned int loops = 100000;
  534. struct mpic *mpic = mpic_from_irq(irq);
  535. unsigned int src = mpic_irq_to_hw(irq);
  536. DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
  537. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  538. mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
  539. MPIC_VECPRI_MASK);
  540. /* make sure mask gets to controller before we return to user */
  541. do {
  542. if (!loops--) {
  543. printk(KERN_ERR "mpic_enable_irq timeout\n");
  544. break;
  545. }
  546. } while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
  547. }
  548. static void mpic_end_irq(unsigned int irq)
  549. {
  550. struct mpic *mpic = mpic_from_irq(irq);
  551. #ifdef DEBUG_IRQ
  552. DBG("%s: end_irq: %d\n", mpic->name, irq);
  553. #endif
  554. /* We always EOI on end_irq() even for edge interrupts since that
  555. * should only lower the priority, the MPIC should have properly
  556. * latched another edge interrupt coming in anyway
  557. */
  558. mpic_eoi(mpic);
  559. }
  560. #ifdef CONFIG_MPIC_U3_HT_IRQS
  561. static void mpic_unmask_ht_irq(unsigned int irq)
  562. {
  563. struct mpic *mpic = mpic_from_irq(irq);
  564. unsigned int src = mpic_irq_to_hw(irq);
  565. mpic_unmask_irq(irq);
  566. if (irq_desc[irq].status & IRQ_LEVEL)
  567. mpic_ht_end_irq(mpic, src);
  568. }
  569. static unsigned int mpic_startup_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. mpic_startup_ht_interrupt(mpic, src, irq_desc[irq].status);
  575. return 0;
  576. }
  577. static void mpic_shutdown_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_shutdown_ht_interrupt(mpic, src, irq_desc[irq].status);
  582. mpic_mask_irq(irq);
  583. }
  584. static void mpic_end_ht_irq(unsigned int irq)
  585. {
  586. struct mpic *mpic = mpic_from_irq(irq);
  587. unsigned int src = mpic_irq_to_hw(irq);
  588. #ifdef DEBUG_IRQ
  589. DBG("%s: end_irq: %d\n", mpic->name, irq);
  590. #endif
  591. /* We always EOI on end_irq() even for edge interrupts since that
  592. * should only lower the priority, the MPIC should have properly
  593. * latched another edge interrupt coming in anyway
  594. */
  595. if (irq_desc[irq].status & IRQ_LEVEL)
  596. mpic_ht_end_irq(mpic, src);
  597. mpic_eoi(mpic);
  598. }
  599. #endif /* !CONFIG_MPIC_U3_HT_IRQS */
  600. #ifdef CONFIG_SMP
  601. static void mpic_unmask_ipi(unsigned int irq)
  602. {
  603. struct mpic *mpic = mpic_from_ipi(irq);
  604. unsigned int src = mpic_irq_to_hw(irq) - mpic->ipi_vecs[0];
  605. DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
  606. mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
  607. }
  608. static void mpic_mask_ipi(unsigned int irq)
  609. {
  610. /* NEVER disable an IPI... that's just plain wrong! */
  611. }
  612. static void mpic_end_ipi(unsigned int irq)
  613. {
  614. struct mpic *mpic = mpic_from_ipi(irq);
  615. /*
  616. * IPIs are marked IRQ_PER_CPU. This has the side effect of
  617. * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
  618. * applying to them. We EOI them late to avoid re-entering.
  619. * We mark IPI's with IRQF_DISABLED as they must run with
  620. * irqs disabled.
  621. */
  622. mpic_eoi(mpic);
  623. }
  624. #endif /* CONFIG_SMP */
  625. static void mpic_set_affinity(unsigned int irq, cpumask_t cpumask)
  626. {
  627. struct mpic *mpic = mpic_from_irq(irq);
  628. unsigned int src = mpic_irq_to_hw(irq);
  629. cpumask_t tmp;
  630. cpus_and(tmp, cpumask, cpu_online_map);
  631. mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
  632. mpic_physmask(cpus_addr(tmp)[0]));
  633. }
  634. static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
  635. {
  636. /* Now convert sense value */
  637. switch(type & IRQ_TYPE_SENSE_MASK) {
  638. case IRQ_TYPE_EDGE_RISING:
  639. return MPIC_INFO(VECPRI_SENSE_EDGE) |
  640. MPIC_INFO(VECPRI_POLARITY_POSITIVE);
  641. case IRQ_TYPE_EDGE_FALLING:
  642. case IRQ_TYPE_EDGE_BOTH:
  643. return MPIC_INFO(VECPRI_SENSE_EDGE) |
  644. MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
  645. case IRQ_TYPE_LEVEL_HIGH:
  646. return MPIC_INFO(VECPRI_SENSE_LEVEL) |
  647. MPIC_INFO(VECPRI_POLARITY_POSITIVE);
  648. case IRQ_TYPE_LEVEL_LOW:
  649. default:
  650. return MPIC_INFO(VECPRI_SENSE_LEVEL) |
  651. MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
  652. }
  653. }
  654. static int mpic_set_irq_type(unsigned int virq, unsigned int flow_type)
  655. {
  656. struct mpic *mpic = mpic_from_irq(virq);
  657. unsigned int src = mpic_irq_to_hw(virq);
  658. struct irq_desc *desc = get_irq_desc(virq);
  659. unsigned int vecpri, vold, vnew;
  660. DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
  661. mpic, virq, src, flow_type);
  662. if (src >= mpic->irq_count)
  663. return -EINVAL;
  664. if (flow_type == IRQ_TYPE_NONE)
  665. if (mpic->senses && src < mpic->senses_count)
  666. flow_type = mpic->senses[src];
  667. if (flow_type == IRQ_TYPE_NONE)
  668. flow_type = IRQ_TYPE_LEVEL_LOW;
  669. desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
  670. desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
  671. if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  672. desc->status |= IRQ_LEVEL;
  673. if (mpic_is_ht_interrupt(mpic, src))
  674. vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
  675. MPIC_VECPRI_SENSE_EDGE;
  676. else
  677. vecpri = mpic_type_to_vecpri(mpic, flow_type);
  678. vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
  679. vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
  680. MPIC_INFO(VECPRI_SENSE_MASK));
  681. vnew |= vecpri;
  682. if (vold != vnew)
  683. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
  684. return 0;
  685. }
  686. static struct irq_chip mpic_irq_chip = {
  687. .mask = mpic_mask_irq,
  688. .unmask = mpic_unmask_irq,
  689. .eoi = mpic_end_irq,
  690. .set_type = mpic_set_irq_type,
  691. };
  692. #ifdef CONFIG_SMP
  693. static struct irq_chip mpic_ipi_chip = {
  694. .mask = mpic_mask_ipi,
  695. .unmask = mpic_unmask_ipi,
  696. .eoi = mpic_end_ipi,
  697. };
  698. #endif /* CONFIG_SMP */
  699. #ifdef CONFIG_MPIC_U3_HT_IRQS
  700. static struct irq_chip mpic_irq_ht_chip = {
  701. .startup = mpic_startup_ht_irq,
  702. .shutdown = mpic_shutdown_ht_irq,
  703. .mask = mpic_mask_irq,
  704. .unmask = mpic_unmask_ht_irq,
  705. .eoi = mpic_end_ht_irq,
  706. .set_type = mpic_set_irq_type,
  707. };
  708. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  709. static int mpic_host_match(struct irq_host *h, struct device_node *node)
  710. {
  711. struct mpic *mpic = h->host_data;
  712. /* Exact match, unless mpic node is NULL */
  713. return mpic->of_node == NULL || mpic->of_node == node;
  714. }
  715. static int mpic_host_map(struct irq_host *h, unsigned int virq,
  716. irq_hw_number_t hw)
  717. {
  718. struct mpic *mpic = h->host_data;
  719. struct irq_chip *chip;
  720. DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
  721. if (hw == mpic->spurious_vec)
  722. return -EINVAL;
  723. #ifdef CONFIG_SMP
  724. else if (hw >= mpic->ipi_vecs[0]) {
  725. WARN_ON(!(mpic->flags & MPIC_PRIMARY));
  726. DBG("mpic: mapping as IPI\n");
  727. set_irq_chip_data(virq, mpic);
  728. set_irq_chip_and_handler(virq, &mpic->hc_ipi,
  729. handle_percpu_irq);
  730. return 0;
  731. }
  732. #endif /* CONFIG_SMP */
  733. if (hw >= mpic->irq_count)
  734. return -EINVAL;
  735. /* Default chip */
  736. chip = &mpic->hc_irq;
  737. #ifdef CONFIG_MPIC_U3_HT_IRQS
  738. /* Check for HT interrupts, override vecpri */
  739. if (mpic_is_ht_interrupt(mpic, hw))
  740. chip = &mpic->hc_ht_irq;
  741. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  742. DBG("mpic: mapping to irq chip @%p\n", chip);
  743. set_irq_chip_data(virq, mpic);
  744. set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
  745. /* Set default irq type */
  746. set_irq_type(virq, IRQ_TYPE_NONE);
  747. return 0;
  748. }
  749. static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
  750. u32 *intspec, unsigned int intsize,
  751. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  752. {
  753. static unsigned char map_mpic_senses[4] = {
  754. IRQ_TYPE_EDGE_RISING,
  755. IRQ_TYPE_LEVEL_LOW,
  756. IRQ_TYPE_LEVEL_HIGH,
  757. IRQ_TYPE_EDGE_FALLING,
  758. };
  759. *out_hwirq = intspec[0];
  760. if (intsize > 1) {
  761. u32 mask = 0x3;
  762. /* Apple invented a new race of encoding on machines with
  763. * an HT APIC. They encode, among others, the index within
  764. * the HT APIC. We don't care about it here since thankfully,
  765. * it appears that they have the APIC already properly
  766. * configured, and thus our current fixup code that reads the
  767. * APIC config works fine. However, we still need to mask out
  768. * bits in the specifier to make sure we only get bit 0 which
  769. * is the level/edge bit (the only sense bit exposed by Apple),
  770. * as their bit 1 means something else.
  771. */
  772. if (machine_is(powermac))
  773. mask = 0x1;
  774. *out_flags = map_mpic_senses[intspec[1] & mask];
  775. } else
  776. *out_flags = IRQ_TYPE_NONE;
  777. DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
  778. intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
  779. return 0;
  780. }
  781. static struct irq_host_ops mpic_host_ops = {
  782. .match = mpic_host_match,
  783. .map = mpic_host_map,
  784. .xlate = mpic_host_xlate,
  785. };
  786. /*
  787. * Exported functions
  788. */
  789. struct mpic * __init mpic_alloc(struct device_node *node,
  790. phys_addr_t phys_addr,
  791. unsigned int flags,
  792. unsigned int isu_size,
  793. unsigned int irq_count,
  794. const char *name)
  795. {
  796. struct mpic *mpic;
  797. u32 reg;
  798. const char *vers;
  799. int i;
  800. int intvec_top;
  801. u64 paddr = phys_addr;
  802. mpic = alloc_bootmem(sizeof(struct mpic));
  803. if (mpic == NULL)
  804. return NULL;
  805. memset(mpic, 0, sizeof(struct mpic));
  806. mpic->name = name;
  807. mpic->of_node = of_node_get(node);
  808. mpic->irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR, isu_size,
  809. &mpic_host_ops,
  810. flags & MPIC_LARGE_VECTORS ? 2048 : 256);
  811. if (mpic->irqhost == NULL) {
  812. of_node_put(node);
  813. return NULL;
  814. }
  815. mpic->irqhost->host_data = mpic;
  816. mpic->hc_irq = mpic_irq_chip;
  817. mpic->hc_irq.typename = name;
  818. if (flags & MPIC_PRIMARY)
  819. mpic->hc_irq.set_affinity = mpic_set_affinity;
  820. #ifdef CONFIG_MPIC_U3_HT_IRQS
  821. mpic->hc_ht_irq = mpic_irq_ht_chip;
  822. mpic->hc_ht_irq.typename = name;
  823. if (flags & MPIC_PRIMARY)
  824. mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
  825. #endif /* CONFIG_MPIC_U3_HT_IRQS */
  826. #ifdef CONFIG_SMP
  827. mpic->hc_ipi = mpic_ipi_chip;
  828. mpic->hc_ipi.typename = name;
  829. #endif /* CONFIG_SMP */
  830. mpic->flags = flags;
  831. mpic->isu_size = isu_size;
  832. mpic->irq_count = irq_count;
  833. mpic->num_sources = 0; /* so far */
  834. if (flags & MPIC_LARGE_VECTORS)
  835. intvec_top = 2047;
  836. else
  837. intvec_top = 255;
  838. mpic->timer_vecs[0] = intvec_top - 8;
  839. mpic->timer_vecs[1] = intvec_top - 7;
  840. mpic->timer_vecs[2] = intvec_top - 6;
  841. mpic->timer_vecs[3] = intvec_top - 5;
  842. mpic->ipi_vecs[0] = intvec_top - 4;
  843. mpic->ipi_vecs[1] = intvec_top - 3;
  844. mpic->ipi_vecs[2] = intvec_top - 2;
  845. mpic->ipi_vecs[3] = intvec_top - 1;
  846. mpic->spurious_vec = intvec_top;
  847. /* Check for "big-endian" in device-tree */
  848. if (node && of_get_property(node, "big-endian", NULL) != NULL)
  849. mpic->flags |= MPIC_BIG_ENDIAN;
  850. #ifdef CONFIG_MPIC_WEIRD
  851. mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
  852. #endif
  853. /* default register type */
  854. mpic->reg_type = (flags & MPIC_BIG_ENDIAN) ?
  855. mpic_access_mmio_be : mpic_access_mmio_le;
  856. /* If no physical address is passed in, a device-node is mandatory */
  857. BUG_ON(paddr == 0 && node == NULL);
  858. /* If no physical address passed in, check if it's dcr based */
  859. if (paddr == 0 && of_get_property(node, "dcr-reg", NULL) != NULL)
  860. mpic->flags |= MPIC_USES_DCR;
  861. #ifdef CONFIG_PPC_DCR
  862. if (mpic->flags & MPIC_USES_DCR) {
  863. const u32 *dbasep;
  864. dbasep = of_get_property(node, "dcr-reg", NULL);
  865. BUG_ON(dbasep == NULL);
  866. mpic->dcr_base = *dbasep;
  867. mpic->reg_type = mpic_access_dcr;
  868. }
  869. #else
  870. BUG_ON (mpic->flags & MPIC_USES_DCR);
  871. #endif /* CONFIG_PPC_DCR */
  872. /* If the MPIC is not DCR based, and no physical address was passed
  873. * in, try to obtain one
  874. */
  875. if (paddr == 0 && !(mpic->flags & MPIC_USES_DCR)) {
  876. const u32 *reg;
  877. reg = of_get_property(node, "reg", NULL);
  878. BUG_ON(reg == NULL);
  879. paddr = of_translate_address(node, reg);
  880. BUG_ON(paddr == OF_BAD_ADDR);
  881. }
  882. /* Map the global registers */
  883. mpic_map(mpic, paddr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
  884. mpic_map(mpic, paddr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
  885. /* Reset */
  886. if (flags & MPIC_WANTS_RESET) {
  887. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  888. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  889. | MPIC_GREG_GCONF_RESET);
  890. while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  891. & MPIC_GREG_GCONF_RESET)
  892. mb();
  893. }
  894. /* Read feature register, calculate num CPUs and, for non-ISU
  895. * MPICs, num sources as well. On ISU MPICs, sources are counted
  896. * as ISUs are added
  897. */
  898. reg = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
  899. mpic->num_cpus = ((reg & MPIC_GREG_FEATURE_LAST_CPU_MASK)
  900. >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
  901. if (isu_size == 0)
  902. mpic->num_sources = ((reg & MPIC_GREG_FEATURE_LAST_SRC_MASK)
  903. >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
  904. /* Map the per-CPU registers */
  905. for (i = 0; i < mpic->num_cpus; i++) {
  906. mpic_map(mpic, paddr, &mpic->cpuregs[i],
  907. MPIC_INFO(CPU_BASE) + i * MPIC_INFO(CPU_STRIDE),
  908. 0x1000);
  909. }
  910. /* Initialize main ISU if none provided */
  911. if (mpic->isu_size == 0) {
  912. mpic->isu_size = mpic->num_sources;
  913. mpic_map(mpic, paddr, &mpic->isus[0],
  914. MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
  915. }
  916. mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
  917. mpic->isu_mask = (1 << mpic->isu_shift) - 1;
  918. /* Display version */
  919. switch (reg & MPIC_GREG_FEATURE_VERSION_MASK) {
  920. case 1:
  921. vers = "1.0";
  922. break;
  923. case 2:
  924. vers = "1.2";
  925. break;
  926. case 3:
  927. vers = "1.3";
  928. break;
  929. default:
  930. vers = "<unknown>";
  931. break;
  932. }
  933. printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %llx,"
  934. " max %d CPUs\n",
  935. name, vers, (unsigned long long)paddr, mpic->num_cpus);
  936. printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n",
  937. mpic->isu_size, mpic->isu_shift, mpic->isu_mask);
  938. mpic->next = mpics;
  939. mpics = mpic;
  940. if (flags & MPIC_PRIMARY) {
  941. mpic_primary = mpic;
  942. irq_set_default_host(mpic->irqhost);
  943. }
  944. return mpic;
  945. }
  946. void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
  947. phys_addr_t paddr)
  948. {
  949. unsigned int isu_first = isu_num * mpic->isu_size;
  950. BUG_ON(isu_num >= MPIC_MAX_ISU);
  951. mpic_map(mpic, paddr, &mpic->isus[isu_num], 0,
  952. MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
  953. if ((isu_first + mpic->isu_size) > mpic->num_sources)
  954. mpic->num_sources = isu_first + mpic->isu_size;
  955. }
  956. void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
  957. {
  958. mpic->senses = senses;
  959. mpic->senses_count = count;
  960. }
  961. void __init mpic_init(struct mpic *mpic)
  962. {
  963. int i;
  964. BUG_ON(mpic->num_sources == 0);
  965. printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
  966. /* Set current processor priority to max */
  967. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
  968. /* Initialize timers: just disable them all */
  969. for (i = 0; i < 4; i++) {
  970. mpic_write(mpic->tmregs,
  971. i * MPIC_INFO(TIMER_STRIDE) +
  972. MPIC_INFO(TIMER_DESTINATION), 0);
  973. mpic_write(mpic->tmregs,
  974. i * MPIC_INFO(TIMER_STRIDE) +
  975. MPIC_INFO(TIMER_VECTOR_PRI),
  976. MPIC_VECPRI_MASK |
  977. (mpic->timer_vecs[0] + i));
  978. }
  979. /* Initialize IPIs to our reserved vectors and mark them disabled for now */
  980. mpic_test_broken_ipi(mpic);
  981. for (i = 0; i < 4; i++) {
  982. mpic_ipi_write(i,
  983. MPIC_VECPRI_MASK |
  984. (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
  985. (mpic->ipi_vecs[0] + i));
  986. }
  987. /* Initialize interrupt sources */
  988. if (mpic->irq_count == 0)
  989. mpic->irq_count = mpic->num_sources;
  990. /* Do the HT PIC fixups on U3 broken mpic */
  991. DBG("MPIC flags: %x\n", mpic->flags);
  992. if ((mpic->flags & MPIC_U3_HT_IRQS) && (mpic->flags & MPIC_PRIMARY))
  993. mpic_scan_ht_pics(mpic);
  994. for (i = 0; i < mpic->num_sources; i++) {
  995. /* start with vector = source number, and masked */
  996. u32 vecpri = MPIC_VECPRI_MASK | i |
  997. (8 << MPIC_VECPRI_PRIORITY_SHIFT);
  998. /* init hw */
  999. mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
  1000. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1001. 1 << hard_smp_processor_id());
  1002. }
  1003. /* Init spurious vector */
  1004. mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), mpic->spurious_vec);
  1005. /* Disable 8259 passthrough, if supported */
  1006. if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
  1007. mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
  1008. mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
  1009. | MPIC_GREG_GCONF_8259_PTHROU_DIS);
  1010. /* Set current processor priority to 0 */
  1011. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
  1012. #ifdef CONFIG_PM
  1013. /* allocate memory to save mpic state */
  1014. mpic->save_data = alloc_bootmem(mpic->num_sources * sizeof(struct mpic_irq_save));
  1015. BUG_ON(mpic->save_data == NULL);
  1016. #endif
  1017. }
  1018. void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
  1019. {
  1020. u32 v;
  1021. v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
  1022. v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
  1023. v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
  1024. mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
  1025. }
  1026. void __init mpic_set_serial_int(struct mpic *mpic, int enable)
  1027. {
  1028. unsigned long flags;
  1029. u32 v;
  1030. spin_lock_irqsave(&mpic_lock, flags);
  1031. v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
  1032. if (enable)
  1033. v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
  1034. else
  1035. v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
  1036. mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
  1037. spin_unlock_irqrestore(&mpic_lock, flags);
  1038. }
  1039. void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
  1040. {
  1041. int is_ipi;
  1042. struct mpic *mpic = mpic_find(irq, &is_ipi);
  1043. unsigned int src = mpic_irq_to_hw(irq);
  1044. unsigned long flags;
  1045. u32 reg;
  1046. spin_lock_irqsave(&mpic_lock, flags);
  1047. if (is_ipi) {
  1048. reg = mpic_ipi_read(src - mpic->ipi_vecs[0]) &
  1049. ~MPIC_VECPRI_PRIORITY_MASK;
  1050. mpic_ipi_write(src - mpic->ipi_vecs[0],
  1051. reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
  1052. } else {
  1053. reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
  1054. & ~MPIC_VECPRI_PRIORITY_MASK;
  1055. mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
  1056. reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
  1057. }
  1058. spin_unlock_irqrestore(&mpic_lock, flags);
  1059. }
  1060. unsigned int mpic_irq_get_priority(unsigned int irq)
  1061. {
  1062. int is_ipi;
  1063. struct mpic *mpic = mpic_find(irq, &is_ipi);
  1064. unsigned int src = mpic_irq_to_hw(irq);
  1065. unsigned long flags;
  1066. u32 reg;
  1067. spin_lock_irqsave(&mpic_lock, flags);
  1068. if (is_ipi)
  1069. reg = mpic_ipi_read(src = mpic->ipi_vecs[0]);
  1070. else
  1071. reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
  1072. spin_unlock_irqrestore(&mpic_lock, flags);
  1073. return (reg & MPIC_VECPRI_PRIORITY_MASK) >> MPIC_VECPRI_PRIORITY_SHIFT;
  1074. }
  1075. void mpic_setup_this_cpu(void)
  1076. {
  1077. #ifdef CONFIG_SMP
  1078. struct mpic *mpic = mpic_primary;
  1079. unsigned long flags;
  1080. u32 msk = 1 << hard_smp_processor_id();
  1081. unsigned int i;
  1082. BUG_ON(mpic == NULL);
  1083. DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
  1084. spin_lock_irqsave(&mpic_lock, flags);
  1085. /* let the mpic know we want intrs. default affinity is 0xffffffff
  1086. * until changed via /proc. That's how it's done on x86. If we want
  1087. * it differently, then we should make sure we also change the default
  1088. * values of irq_desc[].affinity in irq.c.
  1089. */
  1090. if (distribute_irqs) {
  1091. for (i = 0; i < mpic->num_sources ; i++)
  1092. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1093. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
  1094. }
  1095. /* Set current processor priority to 0 */
  1096. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
  1097. spin_unlock_irqrestore(&mpic_lock, flags);
  1098. #endif /* CONFIG_SMP */
  1099. }
  1100. int mpic_cpu_get_priority(void)
  1101. {
  1102. struct mpic *mpic = mpic_primary;
  1103. return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
  1104. }
  1105. void mpic_cpu_set_priority(int prio)
  1106. {
  1107. struct mpic *mpic = mpic_primary;
  1108. prio &= MPIC_CPU_TASKPRI_MASK;
  1109. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
  1110. }
  1111. /*
  1112. * XXX: someone who knows mpic should check this.
  1113. * do we need to eoi the ipi including for kexec cpu here (see xics comments)?
  1114. * or can we reset the mpic in the new kernel?
  1115. */
  1116. void mpic_teardown_this_cpu(int secondary)
  1117. {
  1118. struct mpic *mpic = mpic_primary;
  1119. unsigned long flags;
  1120. u32 msk = 1 << hard_smp_processor_id();
  1121. unsigned int i;
  1122. BUG_ON(mpic == NULL);
  1123. DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
  1124. spin_lock_irqsave(&mpic_lock, flags);
  1125. /* let the mpic know we don't want intrs. */
  1126. for (i = 0; i < mpic->num_sources ; i++)
  1127. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1128. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
  1129. /* Set current processor priority to max */
  1130. mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
  1131. spin_unlock_irqrestore(&mpic_lock, flags);
  1132. }
  1133. void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
  1134. {
  1135. struct mpic *mpic = mpic_primary;
  1136. BUG_ON(mpic == NULL);
  1137. #ifdef DEBUG_IPI
  1138. DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
  1139. #endif
  1140. mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
  1141. ipi_no * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE),
  1142. mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
  1143. }
  1144. unsigned int mpic_get_one_irq(struct mpic *mpic)
  1145. {
  1146. u32 src;
  1147. src = mpic_cpu_read(MPIC_INFO(CPU_INTACK)) & MPIC_INFO(VECPRI_VECTOR_MASK);
  1148. #ifdef DEBUG_LOW
  1149. DBG("%s: get_one_irq(): %d\n", mpic->name, src);
  1150. #endif
  1151. if (unlikely(src == mpic->spurious_vec)) {
  1152. if (mpic->flags & MPIC_SPV_EOI)
  1153. mpic_eoi(mpic);
  1154. return NO_IRQ;
  1155. }
  1156. return irq_linear_revmap(mpic->irqhost, src);
  1157. }
  1158. unsigned int mpic_get_irq(void)
  1159. {
  1160. struct mpic *mpic = mpic_primary;
  1161. BUG_ON(mpic == NULL);
  1162. return mpic_get_one_irq(mpic);
  1163. }
  1164. #ifdef CONFIG_SMP
  1165. void mpic_request_ipis(void)
  1166. {
  1167. struct mpic *mpic = mpic_primary;
  1168. int i;
  1169. static char *ipi_names[] = {
  1170. "IPI0 (call function)",
  1171. "IPI1 (reschedule)",
  1172. "IPI2 (unused)",
  1173. "IPI3 (debugger break)",
  1174. };
  1175. BUG_ON(mpic == NULL);
  1176. printk(KERN_INFO "mpic: requesting IPIs ... \n");
  1177. for (i = 0; i < 4; i++) {
  1178. unsigned int vipi = irq_create_mapping(mpic->irqhost,
  1179. mpic->ipi_vecs[0] + i);
  1180. if (vipi == NO_IRQ) {
  1181. printk(KERN_ERR "Failed to map IPI %d\n", i);
  1182. break;
  1183. }
  1184. request_irq(vipi, mpic_ipi_action, IRQF_DISABLED|IRQF_PERCPU,
  1185. ipi_names[i], mpic);
  1186. }
  1187. }
  1188. void smp_mpic_message_pass(int target, int msg)
  1189. {
  1190. /* make sure we're sending something that translates to an IPI */
  1191. if ((unsigned int)msg > 3) {
  1192. printk("SMP %d: smp_message_pass: unknown msg %d\n",
  1193. smp_processor_id(), msg);
  1194. return;
  1195. }
  1196. switch (target) {
  1197. case MSG_ALL:
  1198. mpic_send_ipi(msg, 0xffffffff);
  1199. break;
  1200. case MSG_ALL_BUT_SELF:
  1201. mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
  1202. break;
  1203. default:
  1204. mpic_send_ipi(msg, 1 << target);
  1205. break;
  1206. }
  1207. }
  1208. int __init smp_mpic_probe(void)
  1209. {
  1210. int nr_cpus;
  1211. DBG("smp_mpic_probe()...\n");
  1212. nr_cpus = cpus_weight(cpu_possible_map);
  1213. DBG("nr_cpus: %d\n", nr_cpus);
  1214. if (nr_cpus > 1)
  1215. mpic_request_ipis();
  1216. return nr_cpus;
  1217. }
  1218. void __devinit smp_mpic_setup_cpu(int cpu)
  1219. {
  1220. mpic_setup_this_cpu();
  1221. }
  1222. #endif /* CONFIG_SMP */
  1223. #ifdef CONFIG_PM
  1224. static int mpic_suspend(struct sys_device *dev, pm_message_t state)
  1225. {
  1226. struct mpic *mpic = container_of(dev, struct mpic, sysdev);
  1227. int i;
  1228. for (i = 0; i < mpic->num_sources; i++) {
  1229. mpic->save_data[i].vecprio =
  1230. mpic_irq_read(i, MPIC_INFO(IRQ_VECTOR_PRI));
  1231. mpic->save_data[i].dest =
  1232. mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION));
  1233. }
  1234. return 0;
  1235. }
  1236. static int mpic_resume(struct sys_device *dev)
  1237. {
  1238. struct mpic *mpic = container_of(dev, struct mpic, sysdev);
  1239. int i;
  1240. for (i = 0; i < mpic->num_sources; i++) {
  1241. mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI),
  1242. mpic->save_data[i].vecprio);
  1243. mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
  1244. mpic->save_data[i].dest);
  1245. #ifdef CONFIG_MPIC_U3_HT_IRQS
  1246. {
  1247. struct mpic_irq_fixup *fixup = &mpic->fixups[i];
  1248. if (fixup->base) {
  1249. /* we use the lowest bit in an inverted meaning */
  1250. if ((mpic->save_data[i].fixup_data & 1) == 0)
  1251. continue;
  1252. /* Enable and configure */
  1253. writeb(0x10 + 2 * fixup->index, fixup->base + 2);
  1254. writel(mpic->save_data[i].fixup_data & ~1,
  1255. fixup->base + 4);
  1256. }
  1257. }
  1258. #endif
  1259. } /* end for loop */
  1260. return 0;
  1261. }
  1262. #endif
  1263. static struct sysdev_class mpic_sysclass = {
  1264. #ifdef CONFIG_PM
  1265. .resume = mpic_resume,
  1266. .suspend = mpic_suspend,
  1267. #endif
  1268. set_kset_name("mpic"),
  1269. };
  1270. static int mpic_init_sys(void)
  1271. {
  1272. struct mpic *mpic = mpics;
  1273. int error, id = 0;
  1274. error = sysdev_class_register(&mpic_sysclass);
  1275. while (mpic && !error) {
  1276. mpic->sysdev.cls = &mpic_sysclass;
  1277. mpic->sysdev.id = id++;
  1278. error = sysdev_register(&mpic->sysdev);
  1279. mpic = mpic->next;
  1280. }
  1281. return error;
  1282. }
  1283. device_initcall(mpic_init_sys);