mpic.c 41 KB

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