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