18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/slab.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/kmemleak.h>
98c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
108c2ecf20Sopenharmony_ci#include <linux/memblock.h>
118c2ecf20Sopenharmony_ci#include <asm/msi_bitmap.h>
128c2ecf20Sopenharmony_ci#include <asm/setup.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciint msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	unsigned long flags;
178c2ecf20Sopenharmony_ci	int offset, order = get_count_order(num);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
228c2ecf20Sopenharmony_ci					    num, (1 << order) - 1);
238c2ecf20Sopenharmony_ci	if (offset > bmp->irq_count)
248c2ecf20Sopenharmony_ci		goto err;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	bitmap_set(bmp->bitmap, offset, num);
278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	return offset;
328c2ecf20Sopenharmony_cierr:
338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
348c2ecf20Sopenharmony_ci	return -ENOMEM;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
398c2ecf20Sopenharmony_ci			    unsigned int num)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	unsigned long flags;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
448c2ecf20Sopenharmony_ci		 num, offset);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
478c2ecf20Sopenharmony_ci	bitmap_clear(bmp->bitmap, offset, num);
488c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(msi_bitmap_free_hwirqs);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_civoid msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned long flags;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
598c2ecf20Sopenharmony_ci	bitmap_allocate_region(bmp->bitmap, hwirq, 0);
608c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
658c2ecf20Sopenharmony_ci * @bmp: pointer to the MSI bitmap.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Looks in the device tree to see if there is a property specifying which
688c2ecf20Sopenharmony_ci * irqs can be used for MSI. If found those irqs reserved in the device tree
698c2ecf20Sopenharmony_ci * are reserved in the bitmap.
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * Returns 0 for success, < 0 if there was an error, and > 0 if no property
728c2ecf20Sopenharmony_ci * was found in the device tree.
738c2ecf20Sopenharmony_ci **/
748c2ecf20Sopenharmony_ciint msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int i, j, len;
778c2ecf20Sopenharmony_ci	const u32 *p;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (!bmp->of_node)
808c2ecf20Sopenharmony_ci		return 1;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
838c2ecf20Sopenharmony_ci	if (!p) {
848c2ecf20Sopenharmony_ci		pr_debug("msi_bitmap: no msi-available-ranges property " \
858c2ecf20Sopenharmony_ci			 "found on %pOF\n", bmp->of_node);
868c2ecf20Sopenharmony_ci		return 1;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (len % (2 * sizeof(u32)) != 0) {
908c2ecf20Sopenharmony_ci		printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
918c2ecf20Sopenharmony_ci		       " property on %pOF\n", bmp->of_node);
928c2ecf20Sopenharmony_ci		return -EINVAL;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	spin_lock(&bmp->lock);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* Format is: (<u32 start> <u32 count>)+ */
1008c2ecf20Sopenharmony_ci	len /= 2 * sizeof(u32);
1018c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++, p += 2) {
1028c2ecf20Sopenharmony_ci		for (j = 0; j < *(p + 1); j++)
1038c2ecf20Sopenharmony_ci			bitmap_release_region(bmp->bitmap, *p + j, 0);
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	spin_unlock(&bmp->lock);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciint __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
1128c2ecf20Sopenharmony_ci		     struct device_node *of_node)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	int size;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (!irq_count)
1178c2ecf20Sopenharmony_ci		return -EINVAL;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	size = BITS_TO_LONGS(irq_count) * sizeof(long);
1208c2ecf20Sopenharmony_ci	pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	bmp->bitmap_from_slab = slab_is_available();
1238c2ecf20Sopenharmony_ci	if (bmp->bitmap_from_slab)
1248c2ecf20Sopenharmony_ci		bmp->bitmap = kzalloc(size, GFP_KERNEL);
1258c2ecf20Sopenharmony_ci	else {
1268c2ecf20Sopenharmony_ci		bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES);
1278c2ecf20Sopenharmony_ci		if (!bmp->bitmap)
1288c2ecf20Sopenharmony_ci			panic("%s: Failed to allocate %u bytes\n", __func__,
1298c2ecf20Sopenharmony_ci			      size);
1308c2ecf20Sopenharmony_ci		/* the bitmap won't be freed from memblock allocator */
1318c2ecf20Sopenharmony_ci		kmemleak_not_leak(bmp->bitmap);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (!bmp->bitmap) {
1358c2ecf20Sopenharmony_ci		pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
1368c2ecf20Sopenharmony_ci		return -ENOMEM;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* We zalloc'ed the bitmap, so all irqs are free by default */
1408c2ecf20Sopenharmony_ci	spin_lock_init(&bmp->lock);
1418c2ecf20Sopenharmony_ci	bmp->of_node = of_node_get(of_node);
1428c2ecf20Sopenharmony_ci	bmp->irq_count = irq_count;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_civoid msi_bitmap_free(struct msi_bitmap *bmp)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	if (bmp->bitmap_from_slab)
1508c2ecf20Sopenharmony_ci		kfree(bmp->bitmap);
1518c2ecf20Sopenharmony_ci	of_node_put(bmp->of_node);
1528c2ecf20Sopenharmony_ci	bmp->bitmap = NULL;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#ifdef CONFIG_MSI_BITMAP_SELFTEST
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic void __init test_basics(void)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct msi_bitmap bmp;
1608c2ecf20Sopenharmony_ci	int rc, i, size = 512;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/* Can't allocate a bitmap of 0 irqs */
1638c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* of_node may be NULL */
1668c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, size, NULL));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* Should all be free by default */
1698c2ecf20Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1708c2ecf20Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* With no node, there's no msi-available-ranges, so expect > 0 */
1738c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Should all still be free */
1768c2ecf20Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
1778c2ecf20Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/* Check we can fill it up and then no more */
1808c2ecf20Sopenharmony_ci	for (i = 0; i < size; i++)
1818c2ecf20Sopenharmony_ci		WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Should all be allocated */
1868c2ecf20Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* And if we free one we can then allocate another */
1898c2ecf20Sopenharmony_ci	msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
1908c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Free most of them for the alignment tests */
1938c2ecf20Sopenharmony_ci	msi_bitmap_free_hwirqs(&bmp, 3, size - 3);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/* Check we get a naturally aligned offset */
1968c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 2);
1978c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 2 != 0);
1988c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 4);
1998c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 4 != 0);
2008c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 8);
2018c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 8 != 0);
2028c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 9);
2038c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 16 != 0);
2048c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 3);
2058c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 4 != 0);
2068c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 7);
2078c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 8 != 0);
2088c2ecf20Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 121);
2098c2ecf20Sopenharmony_ci	WARN_ON(rc < 0 && rc % 128 != 0);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	msi_bitmap_free(&bmp);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* Clients may WARN_ON bitmap == NULL for "not-allocated" */
2148c2ecf20Sopenharmony_ci	WARN_ON(bmp.bitmap != NULL);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void __init test_of_node(void)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
2208c2ecf20Sopenharmony_ci	const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
2218c2ecf20Sopenharmony_ci	char *prop_name = "msi-available-ranges";
2228c2ecf20Sopenharmony_ci	char *node_name = "/fakenode";
2238c2ecf20Sopenharmony_ci	struct device_node of_node;
2248c2ecf20Sopenharmony_ci	struct property prop;
2258c2ecf20Sopenharmony_ci	struct msi_bitmap bmp;
2268c2ecf20Sopenharmony_ci#define SIZE_EXPECTED 256
2278c2ecf20Sopenharmony_ci	DECLARE_BITMAP(expected, SIZE_EXPECTED);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* There should really be a struct device_node allocator */
2308c2ecf20Sopenharmony_ci	memset(&of_node, 0, sizeof(of_node));
2318c2ecf20Sopenharmony_ci	of_node_init(&of_node);
2328c2ecf20Sopenharmony_ci	of_node.full_name = node_name;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, SIZE_EXPECTED, &of_node));
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* No msi-available-ranges, so expect > 0 */
2378c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Should all still be free */
2408c2ecf20Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, SIZE_EXPECTED,
2418c2ecf20Sopenharmony_ci					get_count_order(SIZE_EXPECTED)));
2428c2ecf20Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(SIZE_EXPECTED));
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* Now create a fake msi-available-ranges property */
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* There should really .. oh whatever */
2478c2ecf20Sopenharmony_ci	memset(&prop, 0, sizeof(prop));
2488c2ecf20Sopenharmony_ci	prop.name = prop_name;
2498c2ecf20Sopenharmony_ci	prop.value = &prop_data;
2508c2ecf20Sopenharmony_ci	prop.length = sizeof(prop_data);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	of_node.properties = &prop;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* msi-available-ranges, so expect == 0 */
2558c2ecf20Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp));
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* Check we got the expected result */
2588c2ecf20Sopenharmony_ci	WARN_ON(bitmap_parselist(expected_str, expected, SIZE_EXPECTED));
2598c2ecf20Sopenharmony_ci	WARN_ON(!bitmap_equal(expected, bmp.bitmap, SIZE_EXPECTED));
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	msi_bitmap_free(&bmp);
2628c2ecf20Sopenharmony_ci	kfree(bmp.bitmap);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int __init msi_bitmap_selftest(void)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	test_basics();
2708c2ecf20Sopenharmony_ci	test_of_node();
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_cilate_initcall(msi_bitmap_selftest);
2758c2ecf20Sopenharmony_ci#endif /* CONFIG_MSI_BITMAP_SELFTEST */
276