mpic.c 39 KB

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