mpic.c 47 KB

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