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

source code of linux/arch/powerpc/sysdev/msi_bitmap.c