Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/bitmap.h> |
Denis Kirjanov | cb2d388 | 2015-09-16 22:26:14 +0300 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 15 | #include <asm/msi_bitmap.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 16 | #include <asm/setup.h> |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 17 | |
| 18 | int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num) |
| 19 | { |
| 20 | unsigned long flags; |
| 21 | int offset, order = get_count_order(num); |
| 22 | |
| 23 | spin_lock_irqsave(&bmp->lock, flags); |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 24 | |
| 25 | offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0, |
| 26 | num, (1 << order) - 1); |
| 27 | if (offset > bmp->irq_count) |
| 28 | goto err; |
| 29 | |
| 30 | bitmap_set(bmp->bitmap, offset, num); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 31 | spin_unlock_irqrestore(&bmp->lock, flags); |
| 32 | |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 33 | pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 34 | |
| 35 | return offset; |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 36 | err: |
| 37 | spin_unlock_irqrestore(&bmp->lock, flags); |
| 38 | return -ENOMEM; |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 39 | } |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 40 | EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 41 | |
| 42 | void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset, |
| 43 | unsigned int num) |
| 44 | { |
| 45 | unsigned long flags; |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 46 | |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 47 | pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n", |
| 48 | num, offset); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 49 | |
| 50 | spin_lock_irqsave(&bmp->lock, flags); |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 51 | bitmap_clear(bmp->bitmap, offset, num); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 52 | spin_unlock_irqrestore(&bmp->lock, flags); |
| 53 | } |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 54 | EXPORT_SYMBOL(msi_bitmap_free_hwirqs); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 55 | |
| 56 | void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq) |
| 57 | { |
| 58 | unsigned long flags; |
| 59 | |
| 60 | pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq); |
| 61 | |
| 62 | spin_lock_irqsave(&bmp->lock, flags); |
| 63 | bitmap_allocate_region(bmp->bitmap, hwirq, 0); |
| 64 | spin_unlock_irqrestore(&bmp->lock, flags); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree. |
| 69 | * @bmp: pointer to the MSI bitmap. |
| 70 | * |
| 71 | * Looks in the device tree to see if there is a property specifying which |
| 72 | * irqs can be used for MSI. If found those irqs reserved in the device tree |
| 73 | * are reserved in the bitmap. |
| 74 | * |
| 75 | * Returns 0 for success, < 0 if there was an error, and > 0 if no property |
| 76 | * was found in the device tree. |
| 77 | **/ |
| 78 | int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp) |
| 79 | { |
| 80 | int i, j, len; |
| 81 | const u32 *p; |
| 82 | |
| 83 | if (!bmp->of_node) |
| 84 | return 1; |
| 85 | |
| 86 | p = of_get_property(bmp->of_node, "msi-available-ranges", &len); |
| 87 | if (!p) { |
| 88 | pr_debug("msi_bitmap: no msi-available-ranges property " \ |
| 89 | "found on %s\n", bmp->of_node->full_name); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | if (len % (2 * sizeof(u32)) != 0) { |
| 94 | printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges" |
| 95 | " property on %s\n", bmp->of_node->full_name); |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count)); |
| 100 | |
| 101 | spin_lock(&bmp->lock); |
| 102 | |
| 103 | /* Format is: (<u32 start> <u32 count>)+ */ |
| 104 | len /= 2 * sizeof(u32); |
| 105 | for (i = 0; i < len; i++, p += 2) { |
| 106 | for (j = 0; j < *(p + 1); j++) |
| 107 | bitmap_release_region(bmp->bitmap, *p + j, 0); |
| 108 | } |
| 109 | |
| 110 | spin_unlock(&bmp->lock); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Denis Kirjanov | ccde64b | 2015-10-21 14:29:13 +0300 | [diff] [blame] | 115 | int __init_refok msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count, |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 116 | struct device_node *of_node) |
| 117 | { |
| 118 | int size; |
| 119 | |
| 120 | if (!irq_count) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | size = BITS_TO_LONGS(irq_count) * sizeof(long); |
| 124 | pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size); |
| 125 | |
Denis Kirjanov | cb2d388 | 2015-09-16 22:26:14 +0300 | [diff] [blame] | 126 | bmp->bitmap_from_slab = slab_is_available(); |
| 127 | if (bmp->bitmap_from_slab) |
| 128 | bmp->bitmap = kzalloc(size, GFP_KERNEL); |
| 129 | else { |
| 130 | bmp->bitmap = memblock_virt_alloc(size, 0); |
| 131 | /* the bitmap won't be freed from memblock allocator */ |
| 132 | kmemleak_not_leak(bmp->bitmap); |
| 133 | } |
| 134 | |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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(of_node); |
| 143 | bmp->irq_count = irq_count; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | void msi_bitmap_free(struct msi_bitmap *bmp) |
| 149 | { |
Denis Kirjanov | cb2d388 | 2015-09-16 22:26:14 +0300 | [diff] [blame] | 150 | if (bmp->bitmap_from_slab) |
| 151 | kfree(bmp->bitmap); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 152 | of_node_put(bmp->of_node); |
| 153 | bmp->bitmap = NULL; |
| 154 | } |
| 155 | |
| 156 | #ifdef CONFIG_MSI_BITMAP_SELFTEST |
| 157 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 158 | static void __init test_basics(void) |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 159 | { |
| 160 | struct msi_bitmap bmp; |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 161 | int rc, i, size = 512; |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 162 | |
| 163 | /* Can't allocate a bitmap of 0 irqs */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 164 | WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 165 | |
| 166 | /* of_node may be NULL */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 167 | WARN_ON(msi_bitmap_alloc(&bmp, size, NULL)); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 168 | |
| 169 | /* Should all be free by default */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 170 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size))); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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 */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 174 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 175 | |
| 176 | /* Should all still be free */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 177 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size))); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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++) |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 182 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 183 | |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 184 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 185 | |
| 186 | /* Should all be allocated */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 187 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 188 | |
| 189 | /* And if we free one we can then allocate another */ |
| 190 | msi_bitmap_free_hwirqs(&bmp, size / 2, 1); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 191 | WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 192 | |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 193 | /* Free most of them for the alignment tests */ |
| 194 | msi_bitmap_free_hwirqs(&bmp, 3, size - 3); |
| 195 | |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 196 | /* Check we get a naturally aligned offset */ |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 197 | rc = msi_bitmap_alloc_hwirqs(&bmp, 2); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 198 | WARN_ON(rc < 0 && rc % 2 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 199 | rc = msi_bitmap_alloc_hwirqs(&bmp, 4); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 200 | WARN_ON(rc < 0 && rc % 4 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 201 | rc = msi_bitmap_alloc_hwirqs(&bmp, 8); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 202 | WARN_ON(rc < 0 && rc % 8 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 203 | rc = msi_bitmap_alloc_hwirqs(&bmp, 9); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 204 | WARN_ON(rc < 0 && rc % 16 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 205 | rc = msi_bitmap_alloc_hwirqs(&bmp, 3); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 206 | WARN_ON(rc < 0 && rc % 4 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 207 | rc = msi_bitmap_alloc_hwirqs(&bmp, 7); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 208 | WARN_ON(rc < 0 && rc % 8 != 0); |
Michael Ellerman | 695911f | 2014-10-10 19:04:24 +1100 | [diff] [blame] | 209 | rc = msi_bitmap_alloc_hwirqs(&bmp, 121); |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 210 | WARN_ON(rc < 0 && rc % 128 != 0); |
Ian Munsie | b0345bb | 2014-10-08 19:54:53 +1100 | [diff] [blame] | 211 | |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 212 | msi_bitmap_free(&bmp); |
| 213 | |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 214 | /* Clients may WARN_ON bitmap == NULL for "not-allocated" */ |
| 215 | WARN_ON(bmp.bitmap != NULL); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 216 | } |
| 217 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 218 | static void __init test_of_node(void) |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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 | int size = 256; |
| 228 | DECLARE_BITMAP(expected, size); |
| 229 | |
| 230 | /* There should really be a struct device_node allocator */ |
| 231 | memset(&of_node, 0, sizeof(of_node)); |
Li Zhong | e47ff70 | 2014-04-03 14:58:20 +0800 | [diff] [blame] | 232 | of_node_init(&of_node); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 233 | of_node.full_name = node_name; |
| 234 | |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 235 | WARN_ON(msi_bitmap_alloc(&bmp, size, &of_node)); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 236 | |
| 237 | /* No msi-available-ranges, so expect > 0 */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 238 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 239 | |
| 240 | /* Should all still be free */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 241 | WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size))); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 242 | bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); |
| 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 */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 255 | WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp)); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 256 | |
| 257 | /* Check we got the expected result */ |
Michael Ellerman | 4a77f2b | 2014-10-10 19:04:25 +1100 | [diff] [blame] | 258 | WARN_ON(bitmap_parselist(expected_str, expected, size)); |
| 259 | WARN_ON(!bitmap_equal(expected, bmp.bitmap, size)); |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 260 | |
| 261 | msi_bitmap_free(&bmp); |
| 262 | kfree(bmp.bitmap); |
| 263 | } |
| 264 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 265 | static int __init msi_bitmap_selftest(void) |
Michael Ellerman | 7e30286 | 2008-08-06 09:10:01 +1000 | [diff] [blame] | 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 */ |