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>
16 int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
19 int offset, order = get_count_order(num);
21 spin_lock_irqsave(&bmp->lock, flags);
23 * This is fast, but stricter than we need. We might want to add
24 * a fallback routine which does a linear search with no alignment.
26 offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order);
27 spin_unlock_irqrestore(&bmp->lock, flags);
29 pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n",
35 void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
39 int order = get_count_order(num);
41 pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",
44 spin_lock_irqsave(&bmp->lock, flags);
45 bitmap_release_region(bmp->bitmap, offset, order);
46 spin_unlock_irqrestore(&bmp->lock, flags);
49 void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
53 pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
55 spin_lock_irqsave(&bmp->lock, flags);
56 bitmap_allocate_region(bmp->bitmap, hwirq, 0);
57 spin_unlock_irqrestore(&bmp->lock, flags);
61 * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
62 * @bmp: pointer to the MSI bitmap.
64 * Looks in the device tree to see if there is a property specifying which
65 * irqs can be used for MSI. If found those irqs reserved in the device tree
66 * are reserved in the bitmap.
68 * Returns 0 for success, < 0 if there was an error, and > 0 if no property
69 * was found in the device tree.
71 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
79 p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
81 pr_debug("msi_bitmap: no msi-available-ranges property " \
82 "found on %s\n", bmp->of_node->full_name);
86 if (len % (2 * sizeof(u32)) != 0) {
87 printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
88 " property on %s\n", bmp->of_node->full_name);
92 bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
94 spin_lock(&bmp->lock);
96 /* Format is: (<u32 start> <u32 count>)+ */
97 len /= 2 * sizeof(u32);
98 for (i = 0; i < len; i++, p += 2) {
99 for (j = 0; j < *(p + 1); j++)
100 bitmap_release_region(bmp->bitmap, *p + j, 0);
103 spin_unlock(&bmp->lock);
108 int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
109 struct device_node *of_node)
116 size = BITS_TO_LONGS(irq_count) * sizeof(long);
117 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
119 bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL);
121 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
125 /* We zalloc'ed the bitmap, so all irqs are free by default */
126 spin_lock_init(&bmp->lock);
127 bmp->of_node = of_node_get(of_node);
128 bmp->irq_count = irq_count;
133 void msi_bitmap_free(struct msi_bitmap *bmp)
135 /* we can't free the bitmap we don't know if it's bootmem etc. */
136 of_node_put(bmp->of_node);
140 #ifdef CONFIG_MSI_BITMAP_SELFTEST
143 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
145 void __init test_basics(void)
147 struct msi_bitmap bmp;
150 /* Can't allocate a bitmap of 0 irqs */
151 check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);
153 /* of_node may be NULL */
154 check(0 == msi_bitmap_alloc(&bmp, size, NULL));
156 /* Should all be free by default */
157 check(0 == bitmap_find_free_region(bmp.bitmap, size,
158 get_count_order(size)));
159 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
161 /* With no node, there's no msi-available-ranges, so expect > 0 */
162 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
164 /* Should all still be free */
165 check(0 == bitmap_find_free_region(bmp.bitmap, size,
166 get_count_order(size)));
167 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
169 /* Check we can fill it up and then no more */
170 for (i = 0; i < size; i++)
171 check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
173 check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
175 /* Should all be allocated */
176 check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);
178 /* And if we free one we can then allocate another */
179 msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
180 check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
182 msi_bitmap_free(&bmp);
184 /* Clients may check bitmap == NULL for "not-allocated" */
185 check(bmp.bitmap == NULL);
190 void __init test_of_node(void)
192 u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
193 const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
194 char *prop_name = "msi-available-ranges";
195 char *node_name = "/fakenode";
196 struct device_node of_node;
197 struct property prop;
198 struct msi_bitmap bmp;
200 DECLARE_BITMAP(expected, size);
202 /* There should really be a struct device_node allocator */
203 memset(&of_node, 0, sizeof(of_node));
204 kref_init(&of_node.kref);
205 of_node.full_name = node_name;
207 check(0 == msi_bitmap_alloc(&bmp, size, &of_node));
209 /* No msi-available-ranges, so expect > 0 */
210 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
212 /* Should all still be free */
213 check(0 == bitmap_find_free_region(bmp.bitmap, size,
214 get_count_order(size)));
215 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
217 /* Now create a fake msi-available-ranges property */
219 /* There should really .. oh whatever */
220 memset(&prop, 0, sizeof(prop));
221 prop.name = prop_name;
222 prop.value = &prop_data;
223 prop.length = sizeof(prop_data);
225 of_node.properties = ∝
227 /* msi-available-ranges, so expect == 0 */
228 check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);
230 /* Check we got the expected result */
231 check(0 == bitmap_parselist(expected_str, expected, size));
232 check(bitmap_equal(expected, bmp.bitmap, size));
234 msi_bitmap_free(&bmp);
238 int __init msi_bitmap_selftest(void)
240 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
247 late_initcall(msi_bitmap_selftest);
248 #endif /* CONFIG_MSI_BITMAP_SELFTEST */