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 11 #include <linux/slab.h> 12 #include <linux/kernel.h> 13 #include <linux/bitmap.h> 14 #include <asm/msi_bitmap.h> 15 16 int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num) 17 { 18 unsigned long flags; 19 int offset, order = get_count_order(num); 20 21 spin_lock_irqsave(&bmp->lock, flags); 22 /* 23 * This is fast, but stricter than we need. We might want to add 24 * a fallback routine which does a linear search with no alignment. 25 */ 26 offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order); 27 spin_unlock_irqrestore(&bmp->lock, flags); 28 29 pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n", 30 num, order, offset); 31 32 return offset; 33 } 34 35 void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset, 36 unsigned int num) 37 { 38 unsigned long flags; 39 int order = get_count_order(num); 40 41 pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n", 42 num, order, offset); 43 44 spin_lock_irqsave(&bmp->lock, flags); 45 bitmap_release_region(bmp->bitmap, offset, order); 46 spin_unlock_irqrestore(&bmp->lock, flags); 47 } 48 49 void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq) 50 { 51 unsigned long flags; 52 53 pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq); 54 55 spin_lock_irqsave(&bmp->lock, flags); 56 bitmap_allocate_region(bmp->bitmap, hwirq, 0); 57 spin_unlock_irqrestore(&bmp->lock, flags); 58 } 59 60 /** 61 * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree. 62 * @bmp: pointer to the MSI bitmap. 63 * 64 * Looks in the device tree to see if there is a property specifying which 65 * irqs can be used for MSI. If found those irqs reserved in the device tree 66 * are reserved in the bitmap. 67 * 68 * Returns 0 for success, < 0 if there was an error, and > 0 if no property 69 * was found in the device tree. 70 **/ 71 int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp) 72 { 73 int i, j, len; 74 const u32 *p; 75 76 if (!bmp->of_node) 77 return 1; 78 79 p = of_get_property(bmp->of_node, "msi-available-ranges", &len); 80 if (!p) { 81 pr_debug("msi_bitmap: no msi-available-ranges property " \ 82 "found on %s\n", bmp->of_node->full_name); 83 return 1; 84 } 85 86 if (len % (2 * sizeof(u32)) != 0) { 87 printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges" 88 " property on %s\n", bmp->of_node->full_name); 89 return -EINVAL; 90 } 91 92 bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count)); 93 94 spin_lock(&bmp->lock); 95 96 /* Format is: (<u32 start> <u32 count>)+ */ 97 len /= 2 * sizeof(u32); 98 for (i = 0; i < len; i++, p += 2) { 99 for (j = 0; j < *(p + 1); j++) 100 bitmap_release_region(bmp->bitmap, *p + j, 0); 101 } 102 103 spin_unlock(&bmp->lock); 104 105 return 0; 106 } 107 108 int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count, 109 struct device_node *of_node) 110 { 111 int size; 112 113 if (!irq_count) 114 return -EINVAL; 115 116 size = BITS_TO_LONGS(irq_count) * sizeof(long); 117 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size); 118 119 bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL); 120 if (!bmp->bitmap) { 121 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n"); 122 return -ENOMEM; 123 } 124 125 /* We zalloc'ed the bitmap, so all irqs are free by default */ 126 spin_lock_init(&bmp->lock); 127 bmp->of_node = of_node_get(of_node); 128 bmp->irq_count = irq_count; 129 130 return 0; 131 } 132 133 void msi_bitmap_free(struct msi_bitmap *bmp) 134 { 135 /* we can't free the bitmap we don't know if it's bootmem etc. */ 136 of_node_put(bmp->of_node); 137 bmp->bitmap = NULL; 138 } 139 140 #ifdef CONFIG_MSI_BITMAP_SELFTEST 141 142 #define check(x) \ 143 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__); 144 145 void __init test_basics(void) 146 { 147 struct msi_bitmap bmp; 148 int i, size = 512; 149 150 /* Can't allocate a bitmap of 0 irqs */ 151 check(msi_bitmap_alloc(&bmp, 0, NULL) != 0); 152 153 /* of_node may be NULL */ 154 check(0 == msi_bitmap_alloc(&bmp, size, NULL)); 155 156 /* Should all be free by default */ 157 check(0 == bitmap_find_free_region(bmp.bitmap, size, 158 get_count_order(size))); 159 bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); 160 161 /* With no node, there's no msi-available-ranges, so expect > 0 */ 162 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0); 163 164 /* Should all still be free */ 165 check(0 == bitmap_find_free_region(bmp.bitmap, size, 166 get_count_order(size))); 167 bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); 168 169 /* Check we can fill it up and then no more */ 170 for (i = 0; i < size; i++) 171 check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0); 172 173 check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0); 174 175 /* Should all be allocated */ 176 check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0); 177 178 /* And if we free one we can then allocate another */ 179 msi_bitmap_free_hwirqs(&bmp, size / 2, 1); 180 check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2); 181 182 msi_bitmap_free(&bmp); 183 184 /* Clients may check bitmap == NULL for "not-allocated" */ 185 check(bmp.bitmap == NULL); 186 187 kfree(bmp.bitmap); 188 } 189 190 void __init test_of_node(void) 191 { 192 u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 }; 193 const char *expected_str = "0-9,20-24,28-39,41-99,220-255"; 194 char *prop_name = "msi-available-ranges"; 195 char *node_name = "/fakenode"; 196 struct device_node of_node; 197 struct property prop; 198 struct msi_bitmap bmp; 199 int size = 256; 200 DECLARE_BITMAP(expected, size); 201 202 /* There should really be a struct device_node allocator */ 203 memset(&of_node, 0, sizeof(of_node)); 204 kref_init(&of_node.kref); 205 of_node.full_name = node_name; 206 207 check(0 == msi_bitmap_alloc(&bmp, size, &of_node)); 208 209 /* No msi-available-ranges, so expect > 0 */ 210 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0); 211 212 /* Should all still be free */ 213 check(0 == bitmap_find_free_region(bmp.bitmap, size, 214 get_count_order(size))); 215 bitmap_release_region(bmp.bitmap, 0, get_count_order(size)); 216 217 /* Now create a fake msi-available-ranges property */ 218 219 /* There should really .. oh whatever */ 220 memset(&prop, 0, sizeof(prop)); 221 prop.name = prop_name; 222 prop.value = &prop_data; 223 prop.length = sizeof(prop_data); 224 225 of_node.properties = ∝ 226 227 /* msi-available-ranges, so expect == 0 */ 228 check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0); 229 230 /* Check we got the expected result */ 231 check(0 == bitmap_parselist(expected_str, expected, size)); 232 check(bitmap_equal(expected, bmp.bitmap, size)); 233 234 msi_bitmap_free(&bmp); 235 kfree(bmp.bitmap); 236 } 237 238 int __init msi_bitmap_selftest(void) 239 { 240 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n"); 241 242 test_basics(); 243 test_of_node(); 244 245 return 0; 246 } 247 late_initcall(msi_bitmap_selftest); 248 #endif /* CONFIG_MSI_BITMAP_SELFTEST */ 249