Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / powerpc / sysdev / msi_bitmap.c
blob5287e95cec3a5ad79e6e6f9997335a3f0abd2e45
1 /*
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
7 * License.
9 */
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)
18 unsigned long flags;
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",
30 num, order, offset);
32 return offset;
35 void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
36 unsigned int num)
38 unsigned long flags;
39 int order = get_count_order(num);
41 pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",
42 num, order, offset);
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)
51 unsigned long flags;
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);
60 /**
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.
70 **/
71 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
73 int i, j, len;
74 const u32 *p;
76 if (!bmp->of_node)
77 return 1;
79 p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
80 if (!p) {
81 pr_debug("msi_bitmap: no msi-available-ranges property " \
82 "found on %s\n", bmp->of_node->full_name);
83 return 1;
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);
89 return -EINVAL;
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);
105 return 0;
108 int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
109 struct device_node *of_node)
111 int size;
113 if (!irq_count)
114 return -EINVAL;
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);
120 if (!bmp->bitmap) {
121 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
122 return -ENOMEM;
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;
130 return 0;
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);
137 bmp->bitmap = NULL;
140 #ifdef CONFIG_MSI_BITMAP_SELFTEST
142 #define check(x) \
143 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
145 void __init test_basics(void)
147 struct msi_bitmap bmp;
148 int i, size = 512;
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);
187 kfree(bmp.bitmap);
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;
199 int size = 256;
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 = &prop;
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);
235 kfree(bmp.bitmap);
238 int __init msi_bitmap_selftest(void)
240 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
242 test_basics();
243 test_of_node();
245 return 0;
247 late_initcall(msi_bitmap_selftest);
248 #endif /* CONFIG_MSI_BITMAP_SELFTEST */