mpic.c 33 KB

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