Skip to content

Commit b0345bb

Browse files
Ian Munsiempe
authored andcommitted
powerpc/msi: Improve IRQ bitmap allocator
Currently msi_bitmap_alloc_hwirqs() will round up any IRQ allocation requests to the nearest power of 2. eg. ask for 5 IRQs and you'll get 8. This wastes a lot of IRQs which can be a scarce resource. For cxl we may require multiple IRQs for every context that is attached to the accelerator. There may be 1000s of contexts attached, hence we can easily run out of IRQs, especially if we are needlessly wasting them. This changes the msi_bitmap_alloc_hwirqs() to allocate only the required number of IRQs, hence avoiding this wastage. It keeps the natural alignment requirement though. Signed-off-by: Ian Munsie <imunsie@au1.ibm.com> Signed-off-by: Michael Neuling <mikey@neuling.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent be3ebfe commit b0345bb

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

‎arch/powerpc/sysdev/msi_bitmap.c‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,37 @@ int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
2020
int offset, order = get_count_order(num);
2121

2222
spin_lock_irqsave(&bmp->lock, flags);
23-
/*
24-
* This is fast, but stricter than we need. We might want to add
25-
* a fallback routine which does a linear search with no alignment.
26-
*/
27-
offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order);
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);
2830
spin_unlock_irqrestore(&bmp->lock, flags);
2931

30-
pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n",
31-
num, order, offset);
32+
pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
3233

3334
return offset;
35+
err:
36+
spin_unlock_irqrestore(&bmp->lock, flags);
37+
return -ENOMEM;
3438
}
39+
EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
3540

3641
void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
3742
unsigned int num)
3843
{
3944
unsigned long flags;
40-
int order = get_count_order(num);
4145

42-
pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",
43-
num, order, offset);
46+
pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
47+
num, offset);
4448

4549
spin_lock_irqsave(&bmp->lock, flags);
46-
bitmap_release_region(bmp->bitmap, offset, order);
50+
bitmap_clear(bmp->bitmap, offset, num);
4751
spin_unlock_irqrestore(&bmp->lock, flags);
4852
}
53+
EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
4954

5055
void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
5156
{
@@ -180,6 +185,15 @@ static void __init test_basics(void)
180185
msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
181186
check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
182187

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+
183197
msi_bitmap_free(&bmp);
184198

185199
/* Clients may check bitmap == NULL for "not-allocated" */

0 commit comments

Comments
 (0)