[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / arch / powerpc / sysdev / msi_bitmap.c
blob5a32cbef9b6c25b2adb34b7165e12d9398b05587
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/kernel.h>
12 #include <linux/bitmap.h>
13 #include <asm/msi_bitmap.h>
15 int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
17 unsigned long flags;
18 int offset, order = get_count_order(num);
20 spin_lock_irqsave(&bmp->lock, flags);
22 * This is fast, but stricter than we need. We might want to add
23 * a fallback routine which does a linear search with no alignment.
25 offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order);
26 spin_unlock_irqrestore(&bmp->lock, flags);
28 pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n",
29 num, order, offset);
31 return offset;
34 void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
35 unsigned int num)
37 unsigned long flags;
38 int order = get_count_order(num);
40 pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",
41 num, order, offset);
43 spin_lock_irqsave(&bmp->lock, flags);
44 bitmap_release_region(bmp->bitmap, offset, order);
45 spin_unlock_irqrestore(&bmp->lock, flags);
48 void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
50 unsigned long flags;
52 pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
54 spin_lock_irqsave(&bmp->lock, flags);
55 bitmap_allocate_region(bmp->bitmap, hwirq, 0);
56 spin_unlock_irqrestore(&bmp->lock, flags);
59 /**
60 * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
61 * @bmp: pointer to the MSI bitmap.
63 * Looks in the device tree to see if there is a property specifying which
64 * irqs can be used for MSI. If found those irqs reserved in the device tree
65 * are reserved in the bitmap.
67 * Returns 0 for success, < 0 if there was an error, and > 0 if no property
68 * was found in the device tree.
69 **/
70 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
72 int i, j, len;
73 const u32 *p;
75 if (!bmp->of_node)
76 return 1;
78 p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
79 if (!p) {
80 pr_debug("msi_bitmap: no msi-available-ranges property " \
81 "found on %s\n", bmp->of_node->full_name);
82 return 1;
85 if (len % (2 * sizeof(u32)) != 0) {
86 printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
87 " property on %s\n", bmp->of_node->full_name);
88 return -EINVAL;
91 bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
93 spin_lock(&bmp->lock);
95 /* Format is: (<u32 start> <u32 count>)+ */
96 len /= 2 * sizeof(u32);
97 for (i = 0; i < len; i++, p += 2) {
98 for (j = 0; j < *(p + 1); j++)
99 bitmap_release_region(bmp->bitmap, *p + j, 0);
102 spin_unlock(&bmp->lock);
104 return 0;
107 int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
108 struct device_node *of_node)
110 int size;
112 if (!irq_count)
113 return -EINVAL;
115 size = BITS_TO_LONGS(irq_count) * sizeof(long);
116 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
118 bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL);
119 if (!bmp->bitmap) {
120 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
121 return -ENOMEM;
124 /* We zalloc'ed the bitmap, so all irqs are free by default */
125 spin_lock_init(&bmp->lock);
126 bmp->of_node = of_node_get(of_node);
127 bmp->irq_count = irq_count;
129 return 0;
132 void msi_bitmap_free(struct msi_bitmap *bmp)
134 /* we can't free the bitmap we don't know if it's bootmem etc. */
135 of_node_put(bmp->of_node);
136 bmp->bitmap = NULL;
139 #ifdef CONFIG_MSI_BITMAP_SELFTEST
141 #define check(x) \
142 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
144 void __init test_basics(void)
146 struct msi_bitmap bmp;
147 int i, size = 512;
149 /* Can't allocate a bitmap of 0 irqs */
150 check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);
152 /* of_node may be NULL */
153 check(0 == msi_bitmap_alloc(&bmp, size, NULL));
155 /* Should all be free by default */
156 check(0 == bitmap_find_free_region(bmp.bitmap, size,
157 get_count_order(size)));
158 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
160 /* With no node, there's no msi-available-ranges, so expect > 0 */
161 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
163 /* Should all still be free */
164 check(0 == bitmap_find_free_region(bmp.bitmap, size,
165 get_count_order(size)));
166 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
168 /* Check we can fill it up and then no more */
169 for (i = 0; i < size; i++)
170 check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
172 check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
174 /* Should all be allocated */
175 check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);
177 /* And if we free one we can then allocate another */
178 msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
179 check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
181 msi_bitmap_free(&bmp);
183 /* Clients may check bitmap == NULL for "not-allocated" */
184 check(bmp.bitmap == NULL);
186 kfree(bmp.bitmap);
189 void __init test_of_node(void)
191 u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
192 const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
193 char *prop_name = "msi-available-ranges";
194 char *node_name = "/fakenode";
195 struct device_node of_node;
196 struct property prop;
197 struct msi_bitmap bmp;
198 int size = 256;
199 DECLARE_BITMAP(expected, size);
201 /* There should really be a struct device_node allocator */
202 memset(&of_node, 0, sizeof(of_node));
203 kref_init(&of_node.kref);
204 of_node.full_name = node_name;
206 check(0 == msi_bitmap_alloc(&bmp, size, &of_node));
208 /* No msi-available-ranges, so expect > 0 */
209 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
211 /* Should all still be free */
212 check(0 == bitmap_find_free_region(bmp.bitmap, size,
213 get_count_order(size)));
214 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
216 /* Now create a fake msi-available-ranges property */
218 /* There should really .. oh whatever */
219 memset(&prop, 0, sizeof(prop));
220 prop.name = prop_name;
221 prop.value = &prop_data;
222 prop.length = sizeof(prop_data);
224 of_node.properties = &prop;
226 /* msi-available-ranges, so expect == 0 */
227 check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);
229 /* Check we got the expected result */
230 check(0 == bitmap_parselist(expected_str, expected, size));
231 check(bitmap_equal(expected, bmp.bitmap, size));
233 msi_bitmap_free(&bmp);
234 kfree(bmp.bitmap);
237 int __init msi_bitmap_selftest(void)
239 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
241 test_basics();
242 test_of_node();
244 return 0;
246 late_initcall(msi_bitmap_selftest);
247 #endif /* CONFIG_MSI_BITMAP_SELFTEST */