1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
6 #include <linux/slab.h>
7 #include <linux/kernel.h>
8 #include <linux/kmemleak.h>
9 #include <linux/bitmap.h>
10 #include <linux/memblock.h>
12 #include <asm/msi_bitmap.h>
13 #include <asm/setup.h>
15 int msi_bitmap_alloc_hwirqs(struct msi_bitmap
*bmp
, int num
)
18 int offset
, order
= get_count_order(num
);
20 spin_lock_irqsave(&bmp
->lock
, flags
);
22 offset
= bitmap_find_next_zero_area(bmp
->bitmap
, bmp
->irq_count
, 0,
23 num
, (1 << order
) - 1);
24 if (offset
> bmp
->irq_count
)
27 bitmap_set(bmp
->bitmap
, offset
, num
);
28 spin_unlock_irqrestore(&bmp
->lock
, flags
);
30 pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num
, offset
);
34 spin_unlock_irqrestore(&bmp
->lock
, flags
);
37 EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs
);
39 void msi_bitmap_free_hwirqs(struct msi_bitmap
*bmp
, unsigned int offset
,
44 pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
47 spin_lock_irqsave(&bmp
->lock
, flags
);
48 bitmap_clear(bmp
->bitmap
, offset
, num
);
49 spin_unlock_irqrestore(&bmp
->lock
, flags
);
51 EXPORT_SYMBOL(msi_bitmap_free_hwirqs
);
53 void msi_bitmap_reserve_hwirq(struct msi_bitmap
*bmp
, unsigned int hwirq
)
57 pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq
);
59 spin_lock_irqsave(&bmp
->lock
, flags
);
60 bitmap_allocate_region(bmp
->bitmap
, hwirq
, 0);
61 spin_unlock_irqrestore(&bmp
->lock
, flags
);
65 * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
66 * @bmp: pointer to the MSI bitmap.
68 * Looks in the device tree to see if there is a property specifying which
69 * irqs can be used for MSI. If found those irqs reserved in the device tree
70 * are reserved in the bitmap.
72 * Returns 0 for success, < 0 if there was an error, and > 0 if no property
73 * was found in the device tree.
75 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap
*bmp
)
83 p
= of_get_property(bmp
->of_node
, "msi-available-ranges", &len
);
85 pr_debug("msi_bitmap: no msi-available-ranges property " \
86 "found on %pOF\n", bmp
->of_node
);
90 if (len
% (2 * sizeof(u32
)) != 0) {
91 printk(KERN_WARNING
"msi_bitmap: Malformed msi-available-ranges"
92 " property on %pOF\n", bmp
->of_node
);
96 bitmap_allocate_region(bmp
->bitmap
, 0, get_count_order(bmp
->irq_count
));
98 spin_lock(&bmp
->lock
);
100 /* Format is: (<u32 start> <u32 count>)+ */
101 len
/= 2 * sizeof(u32
);
102 for (i
= 0; i
< len
; i
++, p
+= 2) {
103 for (j
= 0; j
< *(p
+ 1); j
++)
104 bitmap_release_region(bmp
->bitmap
, *p
+ j
, 0);
107 spin_unlock(&bmp
->lock
);
112 int __ref
msi_bitmap_alloc(struct msi_bitmap
*bmp
, unsigned int irq_count
,
113 struct device_node
*of_node
)
120 size
= BITS_TO_LONGS(irq_count
) * sizeof(long);
121 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size
);
123 bmp
->bitmap_from_slab
= slab_is_available();
124 if (bmp
->bitmap_from_slab
)
125 bmp
->bitmap
= kzalloc(size
, GFP_KERNEL
);
127 bmp
->bitmap
= memblock_alloc_or_panic(size
, SMP_CACHE_BYTES
);
128 /* the bitmap won't be freed from memblock allocator */
129 kmemleak_not_leak(bmp
->bitmap
);
133 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
137 /* We zalloc'ed the bitmap, so all irqs are free by default */
138 spin_lock_init(&bmp
->lock
);
139 bmp
->of_node
= of_node_get(of_node
);
140 bmp
->irq_count
= irq_count
;
145 void msi_bitmap_free(struct msi_bitmap
*bmp
)
147 if (bmp
->bitmap_from_slab
)
149 of_node_put(bmp
->of_node
);
153 #ifdef CONFIG_MSI_BITMAP_SELFTEST
155 static void __init
test_basics(void)
157 struct msi_bitmap bmp
;
158 int rc
, i
, size
= 512;
160 /* Can't allocate a bitmap of 0 irqs */
161 WARN_ON(msi_bitmap_alloc(&bmp
, 0, NULL
) == 0);
163 /* of_node may be NULL */
164 WARN_ON(msi_bitmap_alloc(&bmp
, size
, NULL
));
166 /* Should all be free by default */
167 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, get_count_order(size
)));
168 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(size
));
170 /* With no node, there's no msi-available-ranges, so expect > 0 */
171 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
) <= 0);
173 /* Should all still be free */
174 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, get_count_order(size
)));
175 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(size
));
177 /* Check we can fill it up and then no more */
178 for (i
= 0; i
< size
; i
++)
179 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) < 0);
181 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) >= 0);
183 /* Should all be allocated */
184 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, size
, 0) >= 0);
186 /* And if we free one we can then allocate another */
187 msi_bitmap_free_hwirqs(&bmp
, size
/ 2, 1);
188 WARN_ON(msi_bitmap_alloc_hwirqs(&bmp
, 1) != size
/ 2);
190 /* Free most of them for the alignment tests */
191 msi_bitmap_free_hwirqs(&bmp
, 3, size
- 3);
193 /* Check we get a naturally aligned offset */
194 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 2);
195 WARN_ON(rc
< 0 && rc
% 2 != 0);
196 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 4);
197 WARN_ON(rc
< 0 && rc
% 4 != 0);
198 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 8);
199 WARN_ON(rc
< 0 && rc
% 8 != 0);
200 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 9);
201 WARN_ON(rc
< 0 && rc
% 16 != 0);
202 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 3);
203 WARN_ON(rc
< 0 && rc
% 4 != 0);
204 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 7);
205 WARN_ON(rc
< 0 && rc
% 8 != 0);
206 rc
= msi_bitmap_alloc_hwirqs(&bmp
, 121);
207 WARN_ON(rc
< 0 && rc
% 128 != 0);
209 msi_bitmap_free(&bmp
);
211 /* Clients may WARN_ON bitmap == NULL for "not-allocated" */
212 WARN_ON(bmp
.bitmap
!= NULL
);
215 static void __init
test_of_node(void)
217 u32 prop_data
[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
218 const char *expected_str
= "0-9,20-24,28-39,41-99,220-255";
219 char *prop_name
= "msi-available-ranges";
220 char *node_name
= "/fakenode";
221 struct device_node of_node
;
222 struct property prop
;
223 struct msi_bitmap bmp
;
224 #define SIZE_EXPECTED 256
225 DECLARE_BITMAP(expected
, SIZE_EXPECTED
);
227 /* There should really be a struct device_node allocator */
228 memset(&of_node
, 0, sizeof(of_node
));
229 of_node_init(&of_node
);
230 of_node
.full_name
= node_name
;
232 WARN_ON(msi_bitmap_alloc(&bmp
, SIZE_EXPECTED
, &of_node
));
234 /* No msi-available-ranges, so expect > 0 */
235 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
) <= 0);
237 /* Should all still be free */
238 WARN_ON(bitmap_find_free_region(bmp
.bitmap
, SIZE_EXPECTED
,
239 get_count_order(SIZE_EXPECTED
)));
240 bitmap_release_region(bmp
.bitmap
, 0, get_count_order(SIZE_EXPECTED
));
242 /* Now create a fake msi-available-ranges property */
244 /* There should really .. oh whatever */
245 memset(&prop
, 0, sizeof(prop
));
246 prop
.name
= prop_name
;
247 prop
.value
= &prop_data
;
248 prop
.length
= sizeof(prop_data
);
250 of_node
.properties
= &prop
;
252 /* msi-available-ranges, so expect == 0 */
253 WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp
));
255 /* Check we got the expected result */
256 WARN_ON(bitmap_parselist(expected_str
, expected
, SIZE_EXPECTED
));
257 WARN_ON(!bitmap_equal(expected
, bmp
.bitmap
, SIZE_EXPECTED
));
259 msi_bitmap_free(&bmp
);
263 static int __init
msi_bitmap_selftest(void)
265 printk(KERN_DEBUG
"Running MSI bitmap self-tests ...\n");
272 late_initcall(msi_bitmap_selftest
);
273 #endif /* CONFIG_MSI_BITMAP_SELFTEST */