xref: /openbmc/linux/arch/powerpc/sysdev/msi_bitmap.c (revision 514c6032)
17e302869SMichael Ellerman /*
27e302869SMichael Ellerman  * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
37e302869SMichael Ellerman  *
47e302869SMichael Ellerman  * This program is free software; you can redistribute it and/or
57e302869SMichael Ellerman  * modify it under the terms of the GNU General Public License
67e302869SMichael Ellerman  * as published by the Free Software Foundation; version 2 of the
77e302869SMichael Ellerman  * License.
87e302869SMichael Ellerman  *
97e302869SMichael Ellerman  */
107e302869SMichael Ellerman 
115a0e3ad6STejun Heo #include <linux/slab.h>
127e302869SMichael Ellerman #include <linux/kernel.h>
13514c6032SRandy Dunlap #include <linux/kmemleak.h>
147e302869SMichael Ellerman #include <linux/bitmap.h>
15cb2d3883SDenis Kirjanov #include <linux/bootmem.h>
167e302869SMichael Ellerman #include <asm/msi_bitmap.h>
17ae3a197eSDavid Howells #include <asm/setup.h>
187e302869SMichael Ellerman 
197e302869SMichael Ellerman int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
207e302869SMichael Ellerman {
217e302869SMichael Ellerman 	unsigned long flags;
227e302869SMichael Ellerman 	int offset, order = get_count_order(num);
237e302869SMichael Ellerman 
247e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
25b0345bbcSIan Munsie 
26b0345bbcSIan Munsie 	offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
27b0345bbcSIan Munsie 					    num, (1 << order) - 1);
28b0345bbcSIan Munsie 	if (offset > bmp->irq_count)
29b0345bbcSIan Munsie 		goto err;
30b0345bbcSIan Munsie 
31b0345bbcSIan Munsie 	bitmap_set(bmp->bitmap, offset, num);
327e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
337e302869SMichael Ellerman 
34b0345bbcSIan Munsie 	pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
357e302869SMichael Ellerman 
367e302869SMichael Ellerman 	return offset;
37b0345bbcSIan Munsie err:
38b0345bbcSIan Munsie 	spin_unlock_irqrestore(&bmp->lock, flags);
39b0345bbcSIan Munsie 	return -ENOMEM;
407e302869SMichael Ellerman }
41b0345bbcSIan Munsie EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
427e302869SMichael Ellerman 
437e302869SMichael Ellerman void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
447e302869SMichael Ellerman 			    unsigned int num)
457e302869SMichael Ellerman {
467e302869SMichael Ellerman 	unsigned long flags;
477e302869SMichael Ellerman 
48b0345bbcSIan Munsie 	pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
49b0345bbcSIan Munsie 		 num, offset);
507e302869SMichael Ellerman 
517e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
52b0345bbcSIan Munsie 	bitmap_clear(bmp->bitmap, offset, num);
537e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
547e302869SMichael Ellerman }
55b0345bbcSIan Munsie EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
567e302869SMichael Ellerman 
577e302869SMichael Ellerman void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
587e302869SMichael Ellerman {
597e302869SMichael Ellerman 	unsigned long flags;
607e302869SMichael Ellerman 
617e302869SMichael Ellerman 	pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
627e302869SMichael Ellerman 
637e302869SMichael Ellerman 	spin_lock_irqsave(&bmp->lock, flags);
647e302869SMichael Ellerman 	bitmap_allocate_region(bmp->bitmap, hwirq, 0);
657e302869SMichael Ellerman 	spin_unlock_irqrestore(&bmp->lock, flags);
667e302869SMichael Ellerman }
677e302869SMichael Ellerman 
687e302869SMichael Ellerman /**
697e302869SMichael Ellerman  * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
707e302869SMichael Ellerman  * @bmp: pointer to the MSI bitmap.
717e302869SMichael Ellerman  *
727e302869SMichael Ellerman  * Looks in the device tree to see if there is a property specifying which
737e302869SMichael Ellerman  * irqs can be used for MSI. If found those irqs reserved in the device tree
747e302869SMichael Ellerman  * are reserved in the bitmap.
757e302869SMichael Ellerman  *
767e302869SMichael Ellerman  * Returns 0 for success, < 0 if there was an error, and > 0 if no property
777e302869SMichael Ellerman  * was found in the device tree.
787e302869SMichael Ellerman  **/
797e302869SMichael Ellerman int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
807e302869SMichael Ellerman {
817e302869SMichael Ellerman 	int i, j, len;
827e302869SMichael Ellerman 	const u32 *p;
837e302869SMichael Ellerman 
847e302869SMichael Ellerman 	if (!bmp->of_node)
857e302869SMichael Ellerman 		return 1;
867e302869SMichael Ellerman 
877e302869SMichael Ellerman 	p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
887e302869SMichael Ellerman 	if (!p) {
897e302869SMichael Ellerman 		pr_debug("msi_bitmap: no msi-available-ranges property " \
90b7c670d6SRob Herring 			 "found on %pOF\n", bmp->of_node);
917e302869SMichael Ellerman 		return 1;
927e302869SMichael Ellerman 	}
937e302869SMichael Ellerman 
947e302869SMichael Ellerman 	if (len % (2 * sizeof(u32)) != 0) {
957e302869SMichael Ellerman 		printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
96b7c670d6SRob Herring 		       " property on %pOF\n", bmp->of_node);
977e302869SMichael Ellerman 		return -EINVAL;
987e302869SMichael Ellerman 	}
997e302869SMichael Ellerman 
1007e302869SMichael Ellerman 	bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
1017e302869SMichael Ellerman 
1027e302869SMichael Ellerman 	spin_lock(&bmp->lock);
1037e302869SMichael Ellerman 
1047e302869SMichael Ellerman 	/* Format is: (<u32 start> <u32 count>)+ */
1057e302869SMichael Ellerman 	len /= 2 * sizeof(u32);
1067e302869SMichael Ellerman 	for (i = 0; i < len; i++, p += 2) {
1077e302869SMichael Ellerman 		for (j = 0; j < *(p + 1); j++)
1087e302869SMichael Ellerman 			bitmap_release_region(bmp->bitmap, *p + j, 0);
1097e302869SMichael Ellerman 	}
1107e302869SMichael Ellerman 
1117e302869SMichael Ellerman 	spin_unlock(&bmp->lock);
1127e302869SMichael Ellerman 
1137e302869SMichael Ellerman 	return 0;
1147e302869SMichael Ellerman }
1157e302869SMichael Ellerman 
116bd721ea7SFabian Frederick int __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
1177e302869SMichael Ellerman 		     struct device_node *of_node)
1187e302869SMichael Ellerman {
1197e302869SMichael Ellerman 	int size;
1207e302869SMichael Ellerman 
1217e302869SMichael Ellerman 	if (!irq_count)
1227e302869SMichael Ellerman 		return -EINVAL;
1237e302869SMichael Ellerman 
1247e302869SMichael Ellerman 	size = BITS_TO_LONGS(irq_count) * sizeof(long);
1257e302869SMichael Ellerman 	pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
1267e302869SMichael Ellerman 
127cb2d3883SDenis Kirjanov 	bmp->bitmap_from_slab = slab_is_available();
128cb2d3883SDenis Kirjanov 	if (bmp->bitmap_from_slab)
129cb2d3883SDenis Kirjanov 		bmp->bitmap = kzalloc(size, GFP_KERNEL);
130cb2d3883SDenis Kirjanov 	else {
131cb2d3883SDenis Kirjanov 		bmp->bitmap = memblock_virt_alloc(size, 0);
132cb2d3883SDenis Kirjanov 		/* the bitmap won't be freed from memblock allocator */
133cb2d3883SDenis Kirjanov 		kmemleak_not_leak(bmp->bitmap);
134cb2d3883SDenis Kirjanov 	}
135cb2d3883SDenis Kirjanov 
1367e302869SMichael Ellerman 	if (!bmp->bitmap) {
1377e302869SMichael Ellerman 		pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
1387e302869SMichael Ellerman 		return -ENOMEM;
1397e302869SMichael Ellerman 	}
1407e302869SMichael Ellerman 
1417e302869SMichael Ellerman 	/* We zalloc'ed the bitmap, so all irqs are free by default */
1427e302869SMichael Ellerman 	spin_lock_init(&bmp->lock);
1437e302869SMichael Ellerman 	bmp->of_node = of_node_get(of_node);
1447e302869SMichael Ellerman 	bmp->irq_count = irq_count;
1457e302869SMichael Ellerman 
1467e302869SMichael Ellerman 	return 0;
1477e302869SMichael Ellerman }
1487e302869SMichael Ellerman 
1497e302869SMichael Ellerman void msi_bitmap_free(struct msi_bitmap *bmp)
1507e302869SMichael Ellerman {
151cb2d3883SDenis Kirjanov 	if (bmp->bitmap_from_slab)
152cb2d3883SDenis Kirjanov 		kfree(bmp->bitmap);
1537e302869SMichael Ellerman 	of_node_put(bmp->of_node);
1547e302869SMichael Ellerman 	bmp->bitmap = NULL;
1557e302869SMichael Ellerman }
1567e302869SMichael Ellerman 
1577e302869SMichael Ellerman #ifdef CONFIG_MSI_BITMAP_SELFTEST
1587e302869SMichael Ellerman 
159e51df2c1SAnton Blanchard static void __init test_basics(void)
1607e302869SMichael Ellerman {
1617e302869SMichael Ellerman 	struct msi_bitmap bmp;
162695911fbSMichael Ellerman 	int rc, i, size = 512;
1637e302869SMichael Ellerman 
1647e302869SMichael Ellerman 	/* Can't allocate a bitmap of 0 irqs */
1654a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0);
1667e302869SMichael Ellerman 
1677e302869SMichael Ellerman 	/* of_node may be NULL */
1684a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc(&bmp, size, NULL));
1697e302869SMichael Ellerman 
1707e302869SMichael Ellerman 	/* Should all be free by default */
1714a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1727e302869SMichael Ellerman 	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1737e302869SMichael Ellerman 
1747e302869SMichael Ellerman 	/* With no node, there's no msi-available-ranges, so expect > 0 */
1754a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
1767e302869SMichael Ellerman 
1777e302869SMichael Ellerman 	/* Should all still be free */
1784a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1797e302869SMichael Ellerman 	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1807e302869SMichael Ellerman 
1817e302869SMichael Ellerman 	/* Check we can fill it up and then no more */
1827e302869SMichael Ellerman 	for (i = 0; i < size; i++)
1834a77f2bdSMichael Ellerman 		WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
1847e302869SMichael Ellerman 
1854a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
1867e302869SMichael Ellerman 
1877e302869SMichael Ellerman 	/* Should all be allocated */
1884a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0);
1897e302869SMichael Ellerman 
1907e302869SMichael Ellerman 	/* And if we free one we can then allocate another */
1917e302869SMichael Ellerman 	msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
1924a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2);
1937e302869SMichael Ellerman 
194695911fbSMichael Ellerman 	/* Free most of them for the alignment tests */
195695911fbSMichael Ellerman 	msi_bitmap_free_hwirqs(&bmp, 3, size - 3);
196695911fbSMichael Ellerman 
197b0345bbcSIan Munsie 	/* Check we get a naturally aligned offset */
198695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 2);
1994a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 2 != 0);
200695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 4);
2014a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 4 != 0);
202695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 8);
2034a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 8 != 0);
204695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 9);
2054a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 16 != 0);
206695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 3);
2074a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 4 != 0);
208695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 7);
2094a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 8 != 0);
210695911fbSMichael Ellerman 	rc = msi_bitmap_alloc_hwirqs(&bmp, 121);
2114a77f2bdSMichael Ellerman 	WARN_ON(rc < 0 && rc % 128 != 0);
212b0345bbcSIan Munsie 
2137e302869SMichael Ellerman 	msi_bitmap_free(&bmp);
2147e302869SMichael Ellerman 
2154a77f2bdSMichael Ellerman 	/* Clients may WARN_ON bitmap == NULL for "not-allocated" */
2164a77f2bdSMichael Ellerman 	WARN_ON(bmp.bitmap != NULL);
2177e302869SMichael Ellerman }
2187e302869SMichael Ellerman 
219e51df2c1SAnton Blanchard static void __init test_of_node(void)
2207e302869SMichael Ellerman {
2217e302869SMichael Ellerman 	u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
2227e302869SMichael Ellerman 	const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
2237e302869SMichael Ellerman 	char *prop_name = "msi-available-ranges";
2247e302869SMichael Ellerman 	char *node_name = "/fakenode";
2257e302869SMichael Ellerman 	struct device_node of_node;
2267e302869SMichael Ellerman 	struct property prop;
2277e302869SMichael Ellerman 	struct msi_bitmap bmp;
2287e302869SMichael Ellerman 	int size = 256;
2297e302869SMichael Ellerman 	DECLARE_BITMAP(expected, size);
2307e302869SMichael Ellerman 
2317e302869SMichael Ellerman 	/* There should really be a struct device_node allocator */
2327e302869SMichael Ellerman 	memset(&of_node, 0, sizeof(of_node));
233e47ff70aSLi Zhong 	of_node_init(&of_node);
2347e302869SMichael Ellerman 	of_node.full_name = node_name;
2357e302869SMichael Ellerman 
2364a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_alloc(&bmp, size, &of_node));
2377e302869SMichael Ellerman 
2387e302869SMichael Ellerman 	/* No msi-available-ranges, so expect > 0 */
2394a77f2bdSMichael Ellerman 	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
2407e302869SMichael Ellerman 
2417e302869SMichael Ellerman 	/* Should all still be free */
2424a77f2bdSMichael Ellerman 	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
2437e302869SMichael Ellerman 	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
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 */
2594a77f2bdSMichael Ellerman 	WARN_ON(bitmap_parselist(expected_str, expected, size));
2604a77f2bdSMichael Ellerman 	WARN_ON(!bitmap_equal(expected, bmp.bitmap, size));
2617e302869SMichael Ellerman 
2627e302869SMichael Ellerman 	msi_bitmap_free(&bmp);
2637e302869SMichael Ellerman 	kfree(bmp.bitmap);
2647e302869SMichael Ellerman }
2657e302869SMichael Ellerman 
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