mpic.c 51 KB

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