xref: /openbmc/linux/arch/powerpc/sysdev/msi_bitmap.c (revision e6f6390a)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27e302869SMichael Ellerman /*
37e302869SMichael Ellerman  * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
47e302869SMichael Ellerman  */
57e302869SMichael Ellerman 
65a0e3ad6STejun Heo #include <linux/slab.h>
77e302869SMichael Ellerman #include <linux/kernel.h>
8514c6032SRandy Dunlap #include <linux/kmemleak.h>
97e302869SMichael Ellerman #include <linux/bitmap.h>
1057c8a661SMike Rapoport #include <linux/memblock.h>
11*e6f6390aSChristophe Leroy #include <linux/of.h>
127e302869SMichael Ellerman #include <asm/msi_bitmap.h>
13ae3a197eSDavid Howells #include <asm/setup.h>
147e302869SMichael Ellerman 
msi_bitmap_alloc_hwirqs(struct msi_bitmap * bmp,int num)157e302869SMichael Ellerman int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
167e302869SMichael Ellerman {
177e302869SMichael Ellerman 	unsigned long flags;
187e302869SMichael Ellerman 	int offset, order = get_count_order(num);
197e302869SMichael Ellerman 
207e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
21b0345bbcSIan Munsie 
22b0345bbcSIan Munsie 	offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
23b0345bbcSIan Munsie 					    num, (1 << order) - 1);
24b0345bbcSIan Munsie 	if (offset > bmp->irq_count)
25b0345bbcSIan Munsie 		goto err;
26b0345bbcSIan Munsie 
27b0345bbcSIan Munsie 	bitmap_set(bmp->bitmap, offset, num);
287e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
297e302869SMichael Ellerman 
30b0345bbcSIan Munsie 	pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
317e302869SMichael Ellerman 
327e302869SMichael Ellerman 	return offset;
33b0345bbcSIan Munsie err:
34b0345bbcSIan Munsie 	spin_unlock_irqrestore(&bmp->lock, flags);
35b0345bbcSIan Munsie 	return -ENOMEM;
367e302869SMichael Ellerman }
37b0345bbcSIan Munsie EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
387e302869SMichael Ellerman 
msi_bitmap_free_hwirqs(struct msi_bitmap * bmp,unsigned int offset,unsigned int num)397e302869SMichael Ellerman void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
407e302869SMichael Ellerman 			    unsigned int num)
417e302869SMichael Ellerman {
427e302869SMichael Ellerman 	unsigned long flags;
437e302869SMichael Ellerman 
44b0345bbcSIan Munsie 	pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
45b0345bbcSIan Munsie 		 num, offset);
467e302869SMichael Ellerman 
477e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
48b0345bbcSIan Munsie 	bitmap_clear(bmp->bitmap, offset, num);
497e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
507e302869SMichael Ellerman }
51b0345bbcSIan Munsie EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
527e302869SMichael Ellerman 
msi_bitmap_reserve_hwirq(struct msi_bitmap * bmp,unsigned int hwirq)537e302869SMichael Ellerman void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
547e302869SMichael Ellerman {
557e302869SMichael Ellerman 	unsigned long flags;
567e302869SMichael Ellerman 
577e302869SMichael Ellerman 	pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
587e302869SMichael Ellerman 
597e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
607e302869SMichael Ellerman 	bitmap_allocate_region(bmp->bitmap, hwirq, 0);
617e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
627e302869SMichael Ellerman }
637e302869SMichael Ellerman 
647e302869SMichael Ellerman /**
657e302869SMichael Ellerman  * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
667e302869SMichael Ellerman  * @bmp: pointer to the MSI bitmap.
677e302869SMichael Ellerman  *
687e302869SMichael Ellerman  * Looks in the device tree to see if there is a property specifying which
697e302869SMichael Ellerman  * irqs can be used for MSI. If found those irqs reserved in the device tree
707e302869SMichael Ellerman  * are reserved in the bitmap.
717e302869SMichael Ellerman  *
727e302869SMichael Ellerman  * Returns 0 for success, < 0 if there was an error, and > 0 if no property
737e302869SMichael Ellerman  * was found in the device tree.
747e302869SMichael Ellerman  **/
msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap * bmp)757e302869SMichael Ellerman int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
767e302869SMichael Ellerman {
777e302869SMichael Ellerman 	int i, j, len;
787e302869SMichael Ellerman 	const u32 *p;
797e302869SMichael Ellerman 
807e302869SMichael Ellerman 	if (!bmp->of_node)
817e302869SMichael Ellerman 		return 1;
827e302869SMichael Ellerman 
837e302869SMichael Ellerman 	p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
847e302869SMichael Ellerman 	if (!p) {
857e302869SMichael Ellerman 		pr_debug("msi_bitmap: no msi-available-ranges property " \
86b7c670d6SRob Herring 			 "found on %pOF\n", bmp->of_node);
877e302869SMichael Ellerman 		return 1;
887e302869SMichael Ellerman 	}
897e302869SMichael Ellerman 
907e302869SMichael Ellerman 	if (len % (2 * sizeof(u32)) != 0) {
917e302869SMichael Ellerman 		printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
92b7c670d6SRob Herring 		       " property on %pOF\n", bmp->of_node);
937e302869SMichael Ellerman 		return -EINVAL;
947e302869SMichael Ellerman 	}
957e302869SMichael Ellerman 
967e302869SMichael Ellerman 	bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
977e302869SMichael Ellerman 
987e302869SMichael Ellerman 	spin_lock(&bmp->lock);
997e302869SMichael Ellerman 
1007e302869SMichael Ellerman 	/* Format is: (<u32 start> <u32 count>)+ */
1017e302869SMichael Ellerman 	len /= 2 * sizeof(u32);
1027e302869SMichael Ellerman 	for (i = 0; i < len; i++, p += 2) {
1037e302869SMichael Ellerman 		for (j = 0; j < *(p + 1); j++)
1047e302869SMichael Ellerman 			bitmap_release_region(bmp->bitmap, *p + j, 0);
1057e302869SMichael Ellerman 	}
1067e302869SMichael Ellerman 
1077e302869SMichael Ellerman 	spin_unlock(&bmp->lock);
1087e302869SMichael Ellerman 
1097e302869SMichael Ellerman 	return 0;
1107e302869SMichael Ellerman }
1117e302869SMichael Ellerman 
msi_bitmap_alloc(struct msi_bitmap * bmp,unsigned int irq_count,struct device_node * of_node)112bd721ea7SFabian Frederick int __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
1137e302869SMichael Ellerman 		     struct device_node *of_node)
1147e302869SMichael Ellerman {
1157e302869SMichael Ellerman 	int size;
1167e302869SMichael Ellerman 
1177e302869SMichael Ellerman 	if (!irq_count)
1187e302869SMichael Ellerman 		return -EINVAL;
1197e302869SMichael Ellerman 
1207e302869SMichael Ellerman 	size = BITS_TO_LONGS(irq_count) * sizeof(long);
1217e302869SMichael Ellerman 	pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
1227e302869SMichael Ellerman 
123cb2d3883SDenis Kirjanov 	bmp->bitmap_from_slab = slab_is_available();
124cb2d3883SDenis Kirjanov 	if (bmp->bitmap_from_slab)
125cb2d3883SDenis Kirjanov 		bmp->bitmap = kzalloc(size, GFP_KERNEL);
126cb2d3883SDenis Kirjanov 	else {
1277e1c4e27SMike Rapoport 		bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES);
1288a7f97b9SMike Rapoport 		if (!bmp->bitmap)
1298a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %u bytes\n", __func__,
1308a7f97b9SMike Rapoport 			      size);
131cb2d3883SDenis Kirjanov 		/* the bitmap won't be freed from memblock allocator */
132cb2d3883SDenis Kirjanov 		kmemleak_not_leak(bmp->bitmap);
133cb2d3883SDenis Kirjanov 	}
134cb2d3883SDenis Kirjanov 
1357e302869SMichael Ellerman 	if (!bmp->bitmap) {
1367e302869SMichael Ellerman 		pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
1377e302869SMichael Ellerman 		return -ENOMEM;
1387e302869SMichael Ellerman 	}
1397e302869SMichael Ellerman 
1407e302869SMichael Ellerman 	/* We zalloc'ed the bitmap, so all irqs are free by default */
1417e302869SMichael Ellerman 	spin_lock_init(&bmp->lock);
1427e302869SMichael Ellerman 	bmp->of_node = of_node_get(of_node);
1437e302869SMichael Ellerman 	bmp->irq_count = irq_count;
1447e302869SMichael Ellerman 
1457e302869SMichael Ellerman 	return 0;
1467e302869SMichael Ellerman }
1477e302869SMichael Ellerman 
msi_bitmap_free(struct msi_bitmap * bmp)1487e302869SMichael Ellerman void msi_bitmap_free(struct msi_bitmap *bmp)
1497e302869SMichael Ellerman {
150cb2d3883SDenis Kirjanov 	if (bmp->bitmap_from_slab)
151cb2d3883SDenis Kirjanov 		kfree(bmp->bitmap);
1527e302869SMichael Ellerman 	of_node_put(bmp->of_node);
1537e302869SMichael Ellerman 	bmp->bitmap = NULL;
1547e302869SMichael Ellerman }
1557e302869SMichael Ellerman 
1567e302869SMichael Ellerman #ifdef CONFIG_MSI_BITMAP_SELFTEST
1577e302869SMichael Ellerman 
test_basics(void)158e51df2c1SAnton Blanchard static void __init test_basics(void)
1597e302869SMichael Ellerman {
1607e302869SMichael Ellerman 	struct msi_bitmap bmp;
161695911fbSMichael Ellerman 	int rc, i, size = 512;
1627e302869SMichael Ellerman 
1637e302869SMichael Ellerman 	/* Can't allocate a bitmap of 0 irqs */
1644a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0);
1657e302869SMichael Ellerman 
1667e302869SMichael Ellerman 	/* of_node may be NULL */
1674a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc(&bmp, size, NULL));
1687e302869SMichael Ellerman 
1697e302869SMichael Ellerman 	/* Should all be free by default */
1704a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1717e302869SMichael Ellerman 	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1727e302869SMichael Ellerman 
1737e302869SMichael Ellerman 	/* With no node, there's no msi-available-ranges, so expect > 0 */
1744a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
1757e302869SMichael Ellerman 
1767e302869SMichael Ellerman 	/* Should all still be free */
1774a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1787e302869SMichael Ellerman 	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1797e302869SMichael Ellerman 
1807e302869SMichael Ellerman 	/* Check we can fill it up and then no more */
1817e302869SMichael Ellerman 	for (i = 0; i < size; i++)
1824a77f2bdSMichael Ellerman 		WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
1837e302869SMichael Ellerman 
1844a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
1857e302869SMichael Ellerman 
1867e302869SMichael Ellerman 	/* Should all be allocated */
1874a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0);
1887e302869SMichael Ellerman 
1897e302869SMichael Ellerman 	/* And if we free one we can then allocate another */
1907e302869SMichael Ellerman 	msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
1914a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2);
1927e302869SMichael Ellerman 
193695911fbSMichael Ellerman 	/* Free most of them for the alignment tests */
194695911fbSMichael Ellerman 	msi_bitmap_free_hwirqs(&bmp, 3, size - 3);
195695911fbSMichael Ellerman 
196b0345bbcSIan Munsie 	/* Check we get a naturally aligned offset */
197695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 2);
1984a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 2 != 0);
199695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 4);
2004a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 4 != 0);
201695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 8);
2024a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 8 != 0);
203695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 9);
2044a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 16 != 0);
205695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 3);
2064a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 4 != 0);
207695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 7);
2084a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 8 != 0);
209695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 121);
2104a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 128 != 0);
211b0345bbcSIan Munsie 
2127e302869SMichael Ellerman 	msi_bitmap_free(&bmp);
2137e302869SMichael Ellerman 
2144a77f2bdSMichael Ellerman 	/* Clients may WARN_ON bitmap == NULL for "not-allocated" */
2154a77f2bdSMichael Ellerman 	WARN_ON(bmp.bitmap != NULL);
2167e302869SMichael Ellerman }
2177e302869SMichael Ellerman 
test_of_node(void)218e51df2c1SAnton Blanchard static void __init test_of_node(void)
2197e302869SMichael Ellerman {
2207e302869SMichael Ellerman 	u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
2217e302869SMichael Ellerman 	const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
2227e302869SMichael Ellerman 	char *prop_name = "msi-available-ranges";
2237e302869SMichael Ellerman 	char *node_name = "/fakenode";
2247e302869SMichael Ellerman 	struct device_node of_node;
2257e302869SMichael Ellerman 	struct property prop;
2267e302869SMichael Ellerman 	struct msi_bitmap bmp;
2271b80ac64SKees Cook #define SIZE_EXPECTED 256
2281b80ac64SKees Cook 	DECLARE_BITMAP(expected, SIZE_EXPECTED);
2297e302869SMichael Ellerman 
2307e302869SMichael Ellerman 	/* There should really be a struct device_node allocator */
2317e302869SMichael Ellerman 	memset(&of_node, 0, sizeof(of_node));
232e47ff70aSLi Zhong 	of_node_init(&of_node);
2337e302869SMichael Ellerman 	of_node.full_name = node_name;
2347e302869SMichael Ellerman 
2351b80ac64SKees Cook 	WARN_ON(msi_bitmap_alloc(&bmp, SIZE_EXPECTED, &of_node));
2367e302869SMichael Ellerman 
2377e302869SMichael Ellerman 	/* No msi-available-ranges, so expect > 0 */
2384a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
2397e302869SMichael Ellerman 
2407e302869SMichael Ellerman 	/* Should all still be free */
2411b80ac64SKees Cook 	WARN_ON(bitmap_find_free_region(bmp.bitmap, SIZE_EXPECTED,
2421b80ac64SKees Cook 					get_count_order(SIZE_EXPECTED)));
2431b80ac64SKees Cook 	bitmap_release_region(bmp.bitmap, 0, get_count_order(SIZE_EXPECTED));
2447e302869SMichael Ellerman 
2457e302869SMichael Ellerman 	/* Now create a fake msi-available-ranges property */
2467e302869SMichael Ellerman 
2477e302869SMichael Ellerman 	/* There should really .. oh whatever */
2487e302869SMichael Ellerman 	memset(&prop, 0, sizeof(prop));
2497e302869SMichael Ellerman 	prop.name = prop_name;
2507e302869SMichael Ellerman 	prop.value = &prop_data;
2517e302869SMichael Ellerman 	prop.length = sizeof(prop_data);
2527e302869SMichael Ellerman 
2537e302869SMichael Ellerman 	of_node.properties = &prop;
2547e302869SMichael Ellerman 
2557e302869SMichael Ellerman 	/* msi-available-ranges, so expect == 0 */
2564a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp));
2577e302869SMichael Ellerman 
2587e302869SMichael Ellerman 	/* Check we got the expected result */
2591b80ac64SKees Cook 	WARN_ON(bitmap_parselist(expected_str, expected, SIZE_EXPECTED));
2601b80ac64SKees Cook 	WARN_ON(!bitmap_equal(expected, bmp.bitmap, SIZE_EXPECTED));
2617e302869SMichael Ellerman 
2627e302869SMichael Ellerman 	msi_bitmap_free(&bmp);
2637e302869SMichael Ellerman 	kfree(bmp.bitmap);
2647e302869SMichael Ellerman }
2657e302869SMichael Ellerman 
msi_bitmap_selftest(void)266e51df2c1SAnton Blanchard static int __init msi_bitmap_selftest(void)
2677e302869SMichael Ellerman {
2687e302869SMichael Ellerman 	printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
2697e302869SMichael Ellerman 
2707e302869SMichael Ellerman 	test_basics();
2717e302869SMichael Ellerman 	test_of_node();
2727e302869SMichael Ellerman 
2737e302869SMichael Ellerman 	return 0;
2747e302869SMichael Ellerman }
2757e302869SMichael Ellerman late_initcall(msi_bitmap_selftest);
2767e302869SMichael Ellerman #endif /* CONFIG_MSI_BITMAP_SELFTEST */
277