2 * Copyright 2006-2008, 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/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/bitmap.h>
14 #include <asm/msi_bitmap.h>
15 #include <asm/setup.h>
17 int msi_bitmap_alloc_hwirqs(struct msi_bitmap
*bmp
, int num
)
20 int offset
, order
= get_count_order(num
);
22 spin_lock_irqsave(&bmp
->lock
, flags
);
24 offset
= bitmap_find_next_zero_area(bmp
->bitmap
, bmp
->irq_count
, 0,
25 num
, (1 << order
) - 1);
26 if (offset
> bmp
->irq_count
)
29 bitmap_set(bmp
->bitmap
, offset
, num
);
30 spin_unlock_irqrestore(&bmp
->lock
, flags
);
32 pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num
, offset
);
36 spin_unlock_irqrestore(&bmp
->lock
, flags
);
39 EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs
);
41 void msi_bitmap_free_hwirqs(struct msi_bitmap
*bmp
, unsigned int offset
,
46 pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
49 spin_lock_irqsave(&bmp
->lock
, flags
);
50 bitmap_clear(bmp
->bitmap
, offset
, num
);
51 spin_unlock_irqrestore(&bmp
->lock
, flags
);
53 EXPORT_SYMBOL(msi_bitmap_free_hwirqs
);
55 void msi_bitmap_reserve_hwirq(struct msi_bitmap
*bmp
, unsigned int hwirq
)
59 pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq
);
61 spin_lock_irqsave(&bmp
->lock
, flags
);
62 bitmap_allocate_region(bmp
->bitmap
, hwirq
, 0);
63 spin_unlock_irqrestore(&bmp
->lock
, flags
);
67 * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
68 * @bmp: pointer to the MSI bitmap.
70 * Looks in the device tree to see if there is a property specifying which
71 * irqs can be used for MSI. If found those irqs reserved in the device tree
72 * are reserved in the bitmap.
74 * Returns 0 for success, < 0 if there was an error, and > 0 if no property
75 * was found in the device tree.
77 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap
*bmp
)
85 p
= of_get_property(bmp
->of_node
, "msi-available-ranges", &len
);
87 pr_debug("msi_bitmap: no msi-available-ranges property " \
88 "found on %s\n", bmp
->of_node
->full_name
);
92 if (len
% (2 * sizeof(u32
)) != 0) {
93 printk(KERN_WARNING
"msi_bitmap: Malformed msi-available-ranges"
94 " property on %s\n", bmp
->of_node
->full_name
);
98 bitmap_allocate_region(bmp
->bitmap
, 0, get_count_order(bmp
->irq_count
));
100 spin_lock(&bmp
->lock
);
102 /* Format is: (<u32 start> <u32 count>)+ */
103 len
/= 2 * sizeof(u32
);
104 for (i
= 0; i
< len
; i
++, p
+= 2) {
105 for (j
= 0; j
< *(p
+ 1); j
++)
106 bitmap_release_region(bmp
->bitmap
, *p
+ j
, 0);
109 spin_unlock(&bmp
->lock
);
114 int msi_bitmap_alloc(struct msi_bitmap
*bmp
, unsigned int irq_count
,
115 struct device_node
*of_node
)
122 size
= BITS_TO_LONGS(irq_count
) * sizeof(long);
123 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size
);
125 bmp
->bitmap
= zalloc_maybe_bootmem(size
, GFP_KERNEL
);
127 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
131 /* We zalloc'ed the bitmap, so all irqs are free by default */
132 spin_lock_init(&bmp
->lock
);
133 bmp
->of_node
= of_node_get(of_node
);
134 bmp
->irq_count
= irq_count
;
139 void msi_bitmap_free(struct msi_bitmap
*bmp
)
141 /* we can't free the bitmap we don't know if it's bootmem etc. */
142 of_node_put(bmp
->of_node
);
146 #ifdef CONFIG_MSI_BITMAP_SELFTEST
148 static void __init
test_basics(void)
150 struct msi_bitmap bmp
;
151 int rc
, i
, size
= 512;
153 /* Can't allocate a bitmap of 0 irqs */
154 WARN_ON(msi_bitmap_alloc(&bmp
, 0, NULL
) == 0);
156 /* of_node may be NULL */
157 WARN_ON(msi_bitmap_alloc(&bmp
, size
, NULL
));
159 /* Should all be free by default */
160 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, get_count_order(size
)));
161 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(size
));
163 /* With no node, there's no msi-available-ranges, so expect > 0 */
164 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
) <= 0);
166 /* Should all still be free */
167 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, get_count_order(size
)));
168 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(size
));
170 /* Check we can fill it up and then no more */
171 for (i
= 0; i
< size
; i
++)
172 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) < 0);
174 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) >= 0);
176 /* Should all be allocated */
177 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, 0) >= 0);
179 /* And if we free one we can then allocate another */
180 msi_bitmap_free_hwirqs(&bmp
, size
/ 2, 1);
181 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) != size
/ 2);
183 /* Free most of them for the alignment tests */
184 msi_bitmap_free_hwirqs(&bmp
, 3, size
- 3);
186 /* Check we get a naturally aligned offset */
187 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 2);
188 WARN_ON(rc
< 0 && rc
% 2 != 0);
189 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 4);
190 WARN_ON(rc
< 0 && rc
% 4 != 0);
191 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 8);
192 WARN_ON(rc
< 0 && rc
% 8 != 0);
193 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 9);
194 WARN_ON(rc
< 0 && rc
% 16 != 0);
195 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 3);
196 WARN_ON(rc
< 0 && rc
% 4 != 0);
197 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 7);
198 WARN_ON(rc
< 0 && rc
% 8 != 0);
199 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 121);
200 WARN_ON(rc
< 0 && rc
% 128 != 0);
202 msi_bitmap_free(&bmp
);
204 /* Clients may WARN_ON bitmap == NULL for "not-allocated" */
205 WARN_ON(bmp
.bitmap
!= NULL
);
210 static void __init
test_of_node(void)
212 u32 prop_data
[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
213 const char *expected_str
= "0-9,20-24,28-39,41-99,220-255";
214 char *prop_name
= "msi-available-ranges";
215 char *node_name
= "/fakenode";
216 struct device_node of_node
;
217 struct property prop
;
218 struct msi_bitmap bmp
;
220 DECLARE_BITMAP(expected
, size
);
222 /* There should really be a struct device_node allocator */
223 memset(&of_node
, 0, sizeof(of_node
));
224 of_node_init(&of_node
);
225 of_node
.full_name
= node_name
;
227 WARN_ON(msi_bitmap_alloc(&bmp
, size
, &of_node
));
229 /* No msi-available-ranges, so expect > 0 */
230 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
) <= 0);
232 /* Should all still be free */
233 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, get_count_order(size
)));
234 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(size
));
236 /* Now create a fake msi-available-ranges property */
238 /* There should really .. oh whatever */
239 memset(&prop
, 0, sizeof(prop
));
240 prop
.name
= prop_name
;
241 prop
.value
= &prop_data
;
242 prop
.length
= sizeof(prop_data
);
244 of_node
.properties
= &prop
;
246 /* msi-available-ranges, so expect == 0 */
247 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
));
249 /* Check we got the expected result */
250 WARN_ON(bitmap_parselist(expected_str
, expected
, size
));
251 WARN_ON(!bitmap_equal(expected
, bmp
.bitmap
, size
));
253 msi_bitmap_free(&bmp
);
257 static int __init
msi_bitmap_selftest(void)
259 printk(KERN_DEBUG
"Running MSI bitmap self-tests ...\n");
266 late_initcall(msi_bitmap_selftest
);
267 #endif /* CONFIG_MSI_BITMAP_SELFTEST */