2 * Copyright 2006-2007, Michael Ellerman, IBM Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; version 2 of the
11 #include <linux/irq.h>
12 #include <linux/bitmap.h>
13 #include <linux/msi.h>
16 #include <asm/hw_irq.h>
17 #include <asm/ppc-pci.h>
20 static void __mpic_msi_reserve_hwirq(struct mpic
*mpic
, irq_hw_number_t hwirq
)
22 pr_debug("mpic: reserving hwirq 0x%lx\n", hwirq
);
23 bitmap_allocate_region(mpic
->hwirq_bitmap
, hwirq
, 0);
26 void mpic_msi_reserve_hwirq(struct mpic
*mpic
, irq_hw_number_t hwirq
)
30 /* The mpic calls this even when there is no allocator setup */
31 if (!mpic
->hwirq_bitmap
)
34 spin_lock_irqsave(&mpic
->bitmap_lock
, flags
);
35 __mpic_msi_reserve_hwirq(mpic
, hwirq
);
36 spin_unlock_irqrestore(&mpic
->bitmap_lock
, flags
);
39 irq_hw_number_t
mpic_msi_alloc_hwirqs(struct mpic
*mpic
, int num
)
42 int offset
, order
= get_count_order(num
);
44 spin_lock_irqsave(&mpic
->bitmap_lock
, flags
);
46 * This is fast, but stricter than we need. We might want to add
47 * a fallback routine which does a linear search with no alignment.
49 offset
= bitmap_find_free_region(mpic
->hwirq_bitmap
, mpic
->irq_count
,
51 spin_unlock_irqrestore(&mpic
->bitmap_lock
, flags
);
53 pr_debug("mpic: allocated 0x%x (2^%d) at offset 0x%x\n",
59 void mpic_msi_free_hwirqs(struct mpic
*mpic
, int offset
, int num
)
62 int order
= get_count_order(num
);
64 pr_debug("mpic: freeing 0x%x (2^%d) at offset 0x%x\n",
67 spin_lock_irqsave(&mpic
->bitmap_lock
, flags
);
68 bitmap_release_region(mpic
->hwirq_bitmap
, offset
, order
);
69 spin_unlock_irqrestore(&mpic
->bitmap_lock
, flags
);
72 #ifdef CONFIG_MPIC_U3_HT_IRQS
73 static int mpic_msi_reserve_u3_hwirqs(struct mpic
*mpic
)
75 irq_hw_number_t hwirq
;
76 struct irq_host_ops
*ops
= mpic
->irqhost
->ops
;
77 struct device_node
*np
;
81 pr_debug("mpic: found U3, guessing msi allocator setup\n");
83 /* Reserve source numbers we know are reserved in the HW */
84 for (i
= 0; i
< 8; i
++)
85 __mpic_msi_reserve_hwirq(mpic
, i
);
87 for (i
= 42; i
< 46; i
++)
88 __mpic_msi_reserve_hwirq(mpic
, i
);
90 for (i
= 100; i
< 105; i
++)
91 __mpic_msi_reserve_hwirq(mpic
, i
);
94 while ((np
= of_find_all_nodes(np
))) {
95 pr_debug("mpic: mapping hwirqs for %s\n", np
->full_name
);
98 while (of_irq_map_one(np
, index
++, &oirq
) == 0) {
99 ops
->xlate(mpic
->irqhost
, NULL
, oirq
.specifier
,
100 oirq
.size
, &hwirq
, &flags
);
101 __mpic_msi_reserve_hwirq(mpic
, hwirq
);
108 static int mpic_msi_reserve_u3_hwirqs(struct mpic
*mpic
)
114 static int mpic_msi_reserve_dt_hwirqs(struct mpic
*mpic
)
119 p
= of_get_property(mpic
->irqhost
->of_node
,
120 "msi-available-ranges", &len
);
122 pr_debug("mpic: no msi-available-ranges property found on %s\n",
123 mpic
->irqhost
->of_node
->full_name
);
128 printk(KERN_WARNING
"mpic: Malformed msi-available-ranges "
129 "property on %s\n", mpic
->irqhost
->of_node
->full_name
);
133 bitmap_allocate_region(mpic
->hwirq_bitmap
, 0,
134 get_count_order(mpic
->irq_count
));
136 /* Format is: (<u32 start> <u32 count>)+ */
138 for (i
= 0; i
< len
/ 2; i
++, p
+= 2)
139 mpic_msi_free_hwirqs(mpic
, *p
, *(p
+ 1));
144 int mpic_msi_init_allocator(struct mpic
*mpic
)
148 BUG_ON(mpic
->hwirq_bitmap
);
149 spin_lock_init(&mpic
->bitmap_lock
);
151 size
= BITS_TO_LONGS(mpic
->irq_count
) * sizeof(long);
152 pr_debug("mpic: allocator bitmap size is 0x%x bytes\n", size
);
154 mpic
->hwirq_bitmap
= alloc_maybe_bootmem(size
, GFP_KERNEL
);
156 if (!mpic
->hwirq_bitmap
) {
157 pr_debug("mpic: ENOMEM allocating allocator bitmap!\n");
161 memset(mpic
->hwirq_bitmap
, 0, size
);
163 rc
= mpic_msi_reserve_dt_hwirqs(mpic
);
165 if (mpic
->flags
& MPIC_U3_HT_IRQS
)
166 rc
= mpic_msi_reserve_u3_hwirqs(mpic
);
176 kfree(mpic
->hwirq_bitmap
);
178 mpic
->hwirq_bitmap
= NULL
;