mpic.c 41 KB

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