mpic.c 42 KB

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