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