PageRenderTime 1378ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 1ms

/arch/powerpc/sysdev/mpic.c

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