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
149 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
151 static void __init test_basics(void)
153 struct msi_bitmap bmp;
156 /* Can't allocate a bitmap of 0 irqs */
157 check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);
159 /* of_node may be NULL */
160 check(0 == msi_bitmap_alloc(&bmp, size, NULL));
162 /* Should all be free by default */
163 check(0 == bitmap_find_free_region(bmp.bitmap, size,
164 get_count_order(size)));
165 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
167 /* With no node, there's no msi-available-ranges, so expect > 0 */
168 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
170 /* Should all still be free */
171 check(0 == bitmap_find_free_region(bmp.bitmap, size,
172 get_count_order(size)));
173 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
175 /* Check we can fill it up and then no more */
176 for (i = 0; i < size; i++)
177 check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
179 check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
181 /* Should all be allocated */
182 check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);
184 /* And if we free one we can then allocate another */
185 msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
186 check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
188 /* Check we get a naturally aligned offset */
189 check(msi_bitmap_alloc_hwirqs(&bmp, 2) % 2 == 0);
190 check(msi_bitmap_alloc_hwirqs(&bmp, 4) % 4 == 0);
191 check(msi_bitmap_alloc_hwirqs(&bmp, 8) % 8 == 0);
192 check(msi_bitmap_alloc_hwirqs(&bmp, 9) % 16 == 0);
193 check(msi_bitmap_alloc_hwirqs(&bmp, 3) % 4 == 0);
194 check(msi_bitmap_alloc_hwirqs(&bmp, 7) % 8 == 0);
195 check(msi_bitmap_alloc_hwirqs(&bmp, 121) % 128 == 0);
197 msi_bitmap_free(&bmp);
199 /* Clients may check bitmap == NULL for "not-allocated" */
200 check(bmp.bitmap == NULL);
205 static void __init test_of_node(void)
207 u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
208 const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
209 char *prop_name = "msi-available-ranges";
210 char *node_name = "/fakenode";
211 struct device_node of_node;
212 struct property prop;
213 struct msi_bitmap bmp;
215 DECLARE_BITMAP(expected, size);
217 /* There should really be a struct device_node allocator */
218 memset(&of_node, 0, sizeof(of_node));
219 of_node_init(&of_node);
220 of_node.full_name = node_name;
222 check(0 == msi_bitmap_alloc(&bmp, size, &of_node));
224 /* No msi-available-ranges, so expect > 0 */
225 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
227 /* Should all still be free */
228 check(0 == bitmap_find_free_region(bmp.bitmap, size,
229 get_count_order(size)));
230 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
232 /* Now create a fake msi-available-ranges property */
234 /* There should really .. oh whatever */
235 memset(&prop, 0, sizeof(prop));
236 prop.name = prop_name;
237 prop.value = &prop_data;
238 prop.length = sizeof(prop_data);
240 of_node.properties = ∝
242 /* msi-available-ranges, so expect == 0 */
243 check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);
245 /* Check we got the expected result */
246 check(0 == bitmap_parselist(expected_str, expected, size));
247 check(bitmap_equal(expected, bmp.bitmap, size));
249 msi_bitmap_free(&bmp);
253 static int __init msi_bitmap_selftest(void)
255 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
262 late_initcall(msi_bitmap_selftest);
263 #endif /* CONFIG_MSI_BITMAP_SELFTEST */