mpic.c 35 KB

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