Commit | Line | Data |
---|---|---|
7e302869 ME |
1 | /* |
2 | * Copyright 2006-2008, Michael Ellerman, IBM Corporation. | |
3 | * | |
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. | |
8 | * | |
9 | */ | |
10 | ||
5a0e3ad6 | 11 | #include <linux/slab.h> |
7e302869 | 12 | #include <linux/kernel.h> |
514c6032 | 13 | #include <linux/kmemleak.h> |
7e302869 | 14 | #include <linux/bitmap.h> |
57c8a661 | 15 | #include <linux/memblock.h> |
7e302869 | 16 | #include <asm/msi_bitmap.h> |
ae3a197e | 17 | #include <asm/setup.h> |
7e302869 ME |
18 | |
19 | int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num) | |
20 | { | |
21 | unsigned long flags; | |
22 | int offset, order = get_count_order(num); | |
23 | ||
24 | spin_lock_irqsave(&bmp->lock, flags); | |
b0345bbc IM |
25 | |
26 | offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0, | |
27 | num, (1 << order) - 1); | |
28 | if (offset > bmp->irq_count) | |
29 | goto err; | |
30 | ||
31 | bitmap_set(bmp->bitmap, offset, num); | |
7e302869 ME |
32 | spin_unlock_irqrestore(&bmp->lock, flags); |
33 | ||
b0345bbc | 34 | pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset); |
7e302869 ME |
35 | |
36 | return offset; | |
b0345bbc IM |
37 | err: |
38 | spin_unlock_irqrestore(&bmp->lock, flags); | |
39 | return -ENOMEM; | |
7e302869 | 40 | } |
b0345bbc | 41 | EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs); |
7e302869 ME |
42 | |
43 | void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset, | |
44 | unsigned int num) | |
45 | { | |
46 | unsigned long flags; | |
7e302869 | 47 | |
b0345bbc IM |
48 | pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n", |
49 | num, offset); | |
7e302869 ME |
50 | |
51 | spin_lock_irqsave(&bmp->lock, flags); | |
b0345bbc | 52 | bitmap_clear(bmp->bitmap, offset, num); |
7e302869 ME |
53 | spin_unlock_irqrestore(&bmp->lock, flags); |
54 | } | |
b0345bbc | 55 | EXPORT_SYMBOL(msi_bitmap_free_hwirqs); |
7e302869 ME |
56 | |
57 | void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq) | |
58 | { | |
59 | unsigned long flags; | |
60 | ||
61 | pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq); | |
62 | ||
63 | spin_lock_irqsave(&bmp->lock, flags); | |
64 | bitmap_allocate_region(bmp->bitmap, hwirq, 0); | |
65 | spin_unlock_irqrestore(&bmp->lock, flags); | |
66 | } | |
67 | ||
68 | /** | |
69 | * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree. | |
70 | * @bmp: pointer to the MSI bitmap. | |
71 | * | |
72 | * Looks in the device tree to see if there is a property specifying which | |
73 | * irqs can be used for MSI. If found those irqs reserved in the device tree | |
74 | * are reserved in the bitmap. | |
75 | * | |
76 | * Returns 0 for success, < 0 if there was an error, and > 0 if no property | |
77 | * was found in the device tree. | |
78 | **/ | |
79 | int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp) | |
80 | { | |
81 | int i, j, len; | |
82 | const u32 *p; | |
83 | ||
84 | if (!bmp->of_node) | |
85 | return 1; | |
86 | ||
87 | p = of_get_property(bmp->of_node, "msi-available-ranges", &len); | |
88 | if (!p) { | |
89 | pr_debug("msi_bitmap: no msi-available-ranges property " \ | |
b7c670d6 | 90 | "found on %pOF\n", bmp->of_node); |
7e302869 ME |
91 | return 1; |
92 | } | |
93 | ||
94 | if (len % (2 * sizeof(u32)) != 0) { | |
95 | printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges" | |
b7c670d6 | 96 | " property on %pOF\n", bmp->of_node); |
7e302869 ME |
97 | return -EINVAL; |
98 | } | |
99 | ||
100 | bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count)); | |
101 | ||
102 | spin_lock(&bmp->lock); | |
103 | ||
104 | /* Format is: (<u32 start> <u32 count>)+ */ | |
105 | len /= 2 * sizeof(u32); | |
106 | for (i = 0; i < len; i++, p += 2) { | |
107 | for (j = 0; j < *(p + 1); j++) | |
108 | bitmap_release_region(bmp->bitmap, *p + j, 0); | |
109 | } | |
110 | ||
111 | spin_unlock(&bmp->lock); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
bd721ea7 | 116 | int __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count, |
7e302869 ME |
117 | struct device_node *of_node) |
118 | { | |
119 | int size; | |
120 | ||
121 | if (!irq_count) | |
122 | return -EINVAL; | |
123 | ||
124 | size = BITS_TO_LONGS(irq_count) * sizeof(long); | |
125 | pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size); | |
126 | ||
cb2d3883 DK |
127 | bmp->bitmap_from_slab = slab_is_available(); |
128 | if (bmp->bitmap_from_slab) | |
129 | bmp->bitmap = kzalloc(size, GFP_KERNEL); | |
130 | else { | |
7e1c4e27 | 131 | bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES); |
8a7f97b9 MR |
132 | if (!bmp->bitmap) |
133 | panic("%s: Failed to allocate %u bytes\n", __func__, | |
134 | size); | |
cb2d3883 DK |
135 | /* the bitmap won't be freed from memblock allocator */ |
136 | kmemleak_not_leak(bmp->bitmap); | |
137 | } | |
138 | ||
7e302869 ME |
139 | if (!bmp->bitmap) { |
140 | pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n"); | |
141 | return -ENOMEM; | |
142 | } | |
143 | ||
144 | /* We zalloc'ed the bitmap, so all irqs are free by default */ | |
145 | spin_lock_init(&bmp->lock); | |
146 | bmp->of_node = of_node_get(of_node); | |
147 | bmp->irq_count = irq_count; | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | void msi_bitmap_free(struct msi_bitmap *bmp) | |
153 | { | |
cb2d3883 DK |
154 | if (bmp->bitmap_from_slab) |
155 | kfree(bmp->bitmap); | |
7e302869 ME |
156 | of_node_put(bmp->of_node); |
157 | bmp->bitmap = NULL; | |
158 | } | |
159 | ||
160 | #ifdef CONFIG_MSI_BITMAP_SELFTEST | |
161 | ||
e51df2c1 | 162 | static void __init test_basics(void) |
7e302869 ME |
163 | { |
164 | struct msi_bitmap bmp; | |
695911fb | 165 | int rc, i, size = 512; |
7e302869 ME |
166 | |
167 | /* Can't allocate a bitmap of 0 irqs */ | |
4a77f2bd | 168 | WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0); |
7e302869 ME |
169 | |
170 | /* of_node may be NULL */ | |
4a77f2bd | 171 | WARN_ON(msi_bitmap_alloc(&bmp, size, NULL)); |
7e302869 ME |
172 | |
173 | /* Should all be free by default */ | |
4a77f2bd | 174 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size))); |
7e302869 ME |
175 | bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); |
176 | ||
177 | /* With no node, there's no msi-available-ranges, so expect > 0 */ | |
4a77f2bd | 178 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0); |
7e302869 ME |
179 | |
180 | /* Should all still be free */ | |
4a77f2bd | 181 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size))); |
7e302869 ME |
182 | bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); |
183 | ||
184 | /* Check we can fill it up and then no more */ | |
185 | for (i = 0; i < size; i++) | |
4a77f2bd | 186 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0); |
7e302869 | 187 | |
4a77f2bd | 188 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0); |
7e302869 ME |
189 | |
190 | /* Should all be allocated */ | |
4a77f2bd | 191 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0); |
7e302869 ME |
192 | |
193 | /* And if we free one we can then allocate another */ | |
194 | msi_bitmap_free_hwirqs(&bmp, size / 2, 1); | |
4a77f2bd | 195 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2); |
7e302869 | 196 | |
695911fb ME |
197 | /* Free most of them for the alignment tests */ |
198 | msi_bitmap_free_hwirqs(&bmp, 3, size - 3); | |
199 | ||
b0345bbc | 200 | /* Check we get a naturally aligned offset */ |
695911fb | 201 | rc = msi_bitmap_alloc_hwirqs(&bmp, 2); |
4a77f2bd | 202 | WARN_ON(rc < 0 && rc % 2 != 0); |
695911fb | 203 | rc = msi_bitmap_alloc_hwirqs(&bmp, 4); |
4a77f2bd | 204 | WARN_ON(rc < 0 && rc % 4 != 0); |
695911fb | 205 | rc = msi_bitmap_alloc_hwirqs(&bmp, 8); |
4a77f2bd | 206 | WARN_ON(rc < 0 && rc % 8 != 0); |
695911fb | 207 | rc = msi_bitmap_alloc_hwirqs(&bmp, 9); |
4a77f2bd | 208 | WARN_ON(rc < 0 && rc % 16 != 0); |
695911fb | 209 | rc = msi_bitmap_alloc_hwirqs(&bmp, 3); |
4a77f2bd | 210 | WARN_ON(rc < 0 && rc % 4 != 0); |
695911fb | 211 | rc = msi_bitmap_alloc_hwirqs(&bmp, 7); |
4a77f2bd | 212 | WARN_ON(rc < 0 && rc % 8 != 0); |
695911fb | 213 | rc = msi_bitmap_alloc_hwirqs(&bmp, 121); |
4a77f2bd | 214 | WARN_ON(rc < 0 && rc % 128 != 0); |
b0345bbc | 215 | |
7e302869 ME |
216 | msi_bitmap_free(&bmp); |
217 | ||
4a77f2bd ME |
218 | /* Clients may WARN_ON bitmap == NULL for "not-allocated" */ |
219 | WARN_ON(bmp.bitmap != NULL); | |
7e302869 ME |
220 | } |
221 | ||
e51df2c1 | 222 | static void __init test_of_node(void) |
7e302869 ME |
223 | { |
224 | u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 }; | |
225 | const char *expected_str = "0-9,20-24,28-39,41-99,220-255"; | |
226 | char *prop_name = "msi-available-ranges"; | |
227 | char *node_name = "/fakenode"; | |
228 | struct device_node of_node; | |
229 | struct property prop; | |
230 | struct msi_bitmap bmp; | |
1b80ac64 KC |
231 | #define SIZE_EXPECTED 256 |
232 | DECLARE_BITMAP(expected, SIZE_EXPECTED); | |
7e302869 ME |
233 | |
234 | /* There should really be a struct device_node allocator */ | |
235 | memset(&of_node, 0, sizeof(of_node)); | |
e47ff70a | 236 | of_node_init(&of_node); |
7e302869 ME |
237 | of_node.full_name = node_name; |
238 | ||
1b80ac64 | 239 | WARN_ON(msi_bitmap_alloc(&bmp, SIZE_EXPECTED, &of_node)); |
7e302869 ME |
240 | |
241 | /* No msi-available-ranges, so expect > 0 */ | |
4a77f2bd | 242 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0); |
7e302869 ME |
243 | |
244 | /* Should all still be free */ | |
1b80ac64 KC |
245 | WARN_ON(bitmap_find_free_region(bmp.bitmap, SIZE_EXPECTED, |
246 | get_count_order(SIZE_EXPECTED))); | |
247 | bitmap_release_region(bmp.bitmap, 0, get_count_order(SIZE_EXPECTED)); | |
7e302869 ME |
248 | |
249 | /* Now create a fake msi-available-ranges property */ | |
250 | ||
251 | /* There should really .. oh whatever */ | |
252 | memset(&prop, 0, sizeof(prop)); | |
253 | prop.name = prop_name; | |
254 | prop.value = &prop_data; | |
255 | prop.length = sizeof(prop_data); | |
256 | ||
257 | of_node.properties = ∝ | |
258 | ||
259 | /* msi-available-ranges, so expect == 0 */ | |
4a77f2bd | 260 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp)); |
7e302869 ME |
261 | |
262 | /* Check we got the expected result */ | |
1b80ac64 KC |
263 | WARN_ON(bitmap_parselist(expected_str, expected, SIZE_EXPECTED)); |
264 | WARN_ON(!bitmap_equal(expected, bmp.bitmap, SIZE_EXPECTED)); | |
7e302869 ME |
265 | |
266 | msi_bitmap_free(&bmp); | |
267 | kfree(bmp.bitmap); | |
268 | } | |
269 | ||
e51df2c1 | 270 | static int __init msi_bitmap_selftest(void) |
7e302869 ME |
271 | { |
272 | printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n"); | |
273 | ||
274 | test_basics(); | |
275 | test_of_node(); | |
276 | ||
277 | return 0; | |
278 | } | |
279 | late_initcall(msi_bitmap_selftest); | |
280 | #endif /* CONFIG_MSI_BITMAP_SELFTEST */ |