powerpc/eeh: Block CFG upon frozen Shiner adapter
[deliverable/linux.git] / arch / powerpc / sysdev / msi_bitmap.c
CommitLineData
7e302869
ME
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
5a0e3ad6 11#include <linux/slab.h>
7e302869
ME
12#include <linux/kernel.h>
13#include <linux/bitmap.h>
14#include <asm/msi_bitmap.h>
ae3a197e 15#include <asm/setup.h>
7e302869
ME
16
17int 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);
b0345bbc
IM
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);
7e302869
ME
30 spin_unlock_irqrestore(&bmp->lock, flags);
31
b0345bbc 32 pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
7e302869
ME
33
34 return offset;
b0345bbc
IM
35err:
36 spin_unlock_irqrestore(&bmp->lock, flags);
37 return -ENOMEM;
7e302869 38}
b0345bbc 39EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
7e302869
ME
40
41void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
42 unsigned int num)
43{
44 unsigned long flags;
7e302869 45
b0345bbc
IM
46 pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
47 num, offset);
7e302869
ME
48
49 spin_lock_irqsave(&bmp->lock, flags);
b0345bbc 50 bitmap_clear(bmp->bitmap, offset, num);
7e302869
ME
51 spin_unlock_irqrestore(&bmp->lock, flags);
52}
b0345bbc 53EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
7e302869
ME
54
55void 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 **/
77int 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
114int 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
139void 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#define check(x) \
149 if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
150
e51df2c1 151static void __init test_basics(void)
7e302869
ME
152{
153 struct msi_bitmap bmp;
154 int i, size = 512;
155
156 /* Can't allocate a bitmap of 0 irqs */
157 check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);
158
159 /* of_node may be NULL */
160 check(0 == msi_bitmap_alloc(&bmp, size, NULL));
161
162 /* Should all be free by default */
163 check(0 == bitmap_find_free_region(bmp.bitmap, size,
164 get_count_order(size)));
165 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
166
167 /* With no node, there's no msi-available-ranges, so expect > 0 */
168 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
169
170 /* Should all still be free */
171 check(0 == bitmap_find_free_region(bmp.bitmap, size,
172 get_count_order(size)));
173 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
174
175 /* Check we can fill it up and then no more */
176 for (i = 0; i < size; i++)
177 check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
178
179 check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
180
181 /* Should all be allocated */
182 check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);
183
184 /* And if we free one we can then allocate another */
185 msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
186 check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
187
b0345bbc
IM
188 /* Check we get a naturally aligned offset */
189 check(msi_bitmap_alloc_hwirqs(&bmp, 2) % 2 == 0);
190 check(msi_bitmap_alloc_hwirqs(&bmp, 4) % 4 == 0);
191 check(msi_bitmap_alloc_hwirqs(&bmp, 8) % 8 == 0);
192 check(msi_bitmap_alloc_hwirqs(&bmp, 9) % 16 == 0);
193 check(msi_bitmap_alloc_hwirqs(&bmp, 3) % 4 == 0);
194 check(msi_bitmap_alloc_hwirqs(&bmp, 7) % 8 == 0);
195 check(msi_bitmap_alloc_hwirqs(&bmp, 121) % 128 == 0);
196
7e302869
ME
197 msi_bitmap_free(&bmp);
198
199 /* Clients may check bitmap == NULL for "not-allocated" */
200 check(bmp.bitmap == NULL);
201
202 kfree(bmp.bitmap);
203}
204
e51df2c1 205static void __init test_of_node(void)
7e302869
ME
206{
207 u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
208 const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
209 char *prop_name = "msi-available-ranges";
210 char *node_name = "/fakenode";
211 struct device_node of_node;
212 struct property prop;
213 struct msi_bitmap bmp;
214 int size = 256;
215 DECLARE_BITMAP(expected, size);
216
217 /* There should really be a struct device_node allocator */
218 memset(&of_node, 0, sizeof(of_node));
e47ff70a 219 of_node_init(&of_node);
7e302869
ME
220 of_node.full_name = node_name;
221
222 check(0 == msi_bitmap_alloc(&bmp, size, &of_node));
223
224 /* No msi-available-ranges, so expect > 0 */
225 check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
226
227 /* Should all still be free */
228 check(0 == bitmap_find_free_region(bmp.bitmap, size,
229 get_count_order(size)));
230 bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
231
232 /* Now create a fake msi-available-ranges property */
233
234 /* There should really .. oh whatever */
235 memset(&prop, 0, sizeof(prop));
236 prop.name = prop_name;
237 prop.value = &prop_data;
238 prop.length = sizeof(prop_data);
239
240 of_node.properties = &prop;
241
242 /* msi-available-ranges, so expect == 0 */
243 check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);
244
245 /* Check we got the expected result */
246 check(0 == bitmap_parselist(expected_str, expected, size));
247 check(bitmap_equal(expected, bmp.bitmap, size));
248
249 msi_bitmap_free(&bmp);
250 kfree(bmp.bitmap);
251}
252
e51df2c1 253static int __init msi_bitmap_selftest(void)
7e302869
ME
254{
255 printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
256
257 test_basics();
258 test_of_node();
259
260 return 0;
261}
262late_initcall(msi_bitmap_selftest);
263#endif /* CONFIG_MSI_BITMAP_SELFTEST */
This page took 0.44356 seconds and 5 git commands to generate.