mpic.c 51 KB

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