162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/slab.h>
762306a36Sopenharmony_ci#include <linux/kernel.h>
862306a36Sopenharmony_ci#include <linux/kmemleak.h>
962306a36Sopenharmony_ci#include <linux/bitmap.h>
1062306a36Sopenharmony_ci#include <linux/memblock.h>
1162306a36Sopenharmony_ci#include <linux/of.h>
1262306a36Sopenharmony_ci#include <asm/msi_bitmap.h>
1362306a36Sopenharmony_ci#include <asm/setup.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciint msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
1662306a36Sopenharmony_ci{
1762306a36Sopenharmony_ci	unsigned long flags;
1862306a36Sopenharmony_ci	int offset, order = get_count_order(num);
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
2362306a36Sopenharmony_ci					    num, (1 << order) - 1);
2462306a36Sopenharmony_ci	if (offset > bmp->irq_count)
2562306a36Sopenharmony_ci		goto err;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	bitmap_set(bmp->bitmap, offset, num);
2862306a36Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	return offset;
3362306a36Sopenharmony_cierr:
3462306a36Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
3562306a36Sopenharmony_ci	return -ENOMEM;
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ciEXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_civoid msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
4062306a36Sopenharmony_ci			    unsigned int num)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	unsigned long flags;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
4562306a36Sopenharmony_ci		 num, offset);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
4862306a36Sopenharmony_ci	bitmap_clear(bmp->bitmap, offset, num);
4962306a36Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ciEXPORT_SYMBOL(msi_bitmap_free_hwirqs);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_civoid msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	unsigned long flags;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	spin_lock_irqsave(&bmp->lock, flags);
6062306a36Sopenharmony_ci	bitmap_allocate_region(bmp->bitmap, hwirq, 0);
6162306a36Sopenharmony_ci	spin_unlock_irqrestore(&bmp->lock, flags);
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/**
6562306a36Sopenharmony_ci * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
6662306a36Sopenharmony_ci * @bmp: pointer to the MSI bitmap.
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * Looks in the device tree to see if there is a property specifying which
6962306a36Sopenharmony_ci * irqs can be used for MSI. If found those irqs reserved in the device tree
7062306a36Sopenharmony_ci * are reserved in the bitmap.
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * Returns 0 for success, < 0 if there was an error, and > 0 if no property
7362306a36Sopenharmony_ci * was found in the device tree.
7462306a36Sopenharmony_ci **/
7562306a36Sopenharmony_ciint msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	int i, j, len;
7862306a36Sopenharmony_ci	const u32 *p;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (!bmp->of_node)
8162306a36Sopenharmony_ci		return 1;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
8462306a36Sopenharmony_ci	if (!p) {
8562306a36Sopenharmony_ci		pr_debug("msi_bitmap: no msi-available-ranges property " \
8662306a36Sopenharmony_ci			 "found on %pOF\n", bmp->of_node);
8762306a36Sopenharmony_ci		return 1;
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	if (len % (2 * sizeof(u32)) != 0) {
9162306a36Sopenharmony_ci		printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
9262306a36Sopenharmony_ci		       " property on %pOF\n", bmp->of_node);
9362306a36Sopenharmony_ci		return -EINVAL;
9462306a36Sopenharmony_ci	}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	spin_lock(&bmp->lock);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/* Format is: (<u32 start> <u32 count>)+ */
10162306a36Sopenharmony_ci	len /= 2 * sizeof(u32);
10262306a36Sopenharmony_ci	for (i = 0; i < len; i++, p += 2) {
10362306a36Sopenharmony_ci		for (j = 0; j < *(p + 1); j++)
10462306a36Sopenharmony_ci			bitmap_release_region(bmp->bitmap, *p + j, 0);
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	spin_unlock(&bmp->lock);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	return 0;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciint __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
11362306a36Sopenharmony_ci		     struct device_node *of_node)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	int size;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	if (!irq_count)
11862306a36Sopenharmony_ci		return -EINVAL;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	size = BITS_TO_LONGS(irq_count) * sizeof(long);
12162306a36Sopenharmony_ci	pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	bmp->bitmap_from_slab = slab_is_available();
12462306a36Sopenharmony_ci	if (bmp->bitmap_from_slab)
12562306a36Sopenharmony_ci		bmp->bitmap = kzalloc(size, GFP_KERNEL);
12662306a36Sopenharmony_ci	else {
12762306a36Sopenharmony_ci		bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES);
12862306a36Sopenharmony_ci		if (!bmp->bitmap)
12962306a36Sopenharmony_ci			panic("%s: Failed to allocate %u bytes\n", __func__,
13062306a36Sopenharmony_ci			      size);
13162306a36Sopenharmony_ci		/* the bitmap won't be freed from memblock allocator */
13262306a36Sopenharmony_ci		kmemleak_not_leak(bmp->bitmap);
13362306a36Sopenharmony_ci	}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	if (!bmp->bitmap) {
13662306a36Sopenharmony_ci		pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
13762306a36Sopenharmony_ci		return -ENOMEM;
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* We zalloc'ed the bitmap, so all irqs are free by default */
14162306a36Sopenharmony_ci	spin_lock_init(&bmp->lock);
14262306a36Sopenharmony_ci	bmp->of_node = of_node_get(of_node);
14362306a36Sopenharmony_ci	bmp->irq_count = irq_count;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	return 0;
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_civoid msi_bitmap_free(struct msi_bitmap *bmp)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	if (bmp->bitmap_from_slab)
15162306a36Sopenharmony_ci		kfree(bmp->bitmap);
15262306a36Sopenharmony_ci	of_node_put(bmp->of_node);
15362306a36Sopenharmony_ci	bmp->bitmap = NULL;
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci#ifdef CONFIG_MSI_BITMAP_SELFTEST
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic void __init test_basics(void)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	struct msi_bitmap bmp;
16162306a36Sopenharmony_ci	int rc, i, size = 512;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	/* Can't allocate a bitmap of 0 irqs */
16462306a36Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	/* of_node may be NULL */
16762306a36Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, size, NULL));
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/* Should all be free by default */
17062306a36Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
17162306a36Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	/* With no node, there's no msi-available-ranges, so expect > 0 */
17462306a36Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	/* Should all still be free */
17762306a36Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
17862306a36Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	/* Check we can fill it up and then no more */
18162306a36Sopenharmony_ci	for (i = 0; i < size; i++)
18262306a36Sopenharmony_ci		WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	/* Should all be allocated */
18762306a36Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	/* And if we free one we can then allocate another */
19062306a36Sopenharmony_ci	msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
19162306a36Sopenharmony_ci	WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	/* Free most of them for the alignment tests */
19462306a36Sopenharmony_ci	msi_bitmap_free_hwirqs(&bmp, 3, size - 3);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	/* Check we get a naturally aligned offset */
19762306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 2);
19862306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 2 != 0);
19962306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 4);
20062306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 4 != 0);
20162306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 8);
20262306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 8 != 0);
20362306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 9);
20462306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 16 != 0);
20562306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 3);
20662306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 4 != 0);
20762306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 7);
20862306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 8 != 0);
20962306a36Sopenharmony_ci	rc = msi_bitmap_alloc_hwirqs(&bmp, 121);
21062306a36Sopenharmony_ci	WARN_ON(rc < 0 && rc % 128 != 0);
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	msi_bitmap_free(&bmp);
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	/* Clients may WARN_ON bitmap == NULL for "not-allocated" */
21562306a36Sopenharmony_ci	WARN_ON(bmp.bitmap != NULL);
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic void __init test_of_node(void)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
22162306a36Sopenharmony_ci	const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
22262306a36Sopenharmony_ci	char *prop_name = "msi-available-ranges";
22362306a36Sopenharmony_ci	char *node_name = "/fakenode";
22462306a36Sopenharmony_ci	struct device_node of_node;
22562306a36Sopenharmony_ci	struct property prop;
22662306a36Sopenharmony_ci	struct msi_bitmap bmp;
22762306a36Sopenharmony_ci#define SIZE_EXPECTED 256
22862306a36Sopenharmony_ci	DECLARE_BITMAP(expected, SIZE_EXPECTED);
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	/* There should really be a struct device_node allocator */
23162306a36Sopenharmony_ci	memset(&of_node, 0, sizeof(of_node));
23262306a36Sopenharmony_ci	of_node_init(&of_node);
23362306a36Sopenharmony_ci	of_node.full_name = node_name;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	WARN_ON(msi_bitmap_alloc(&bmp, SIZE_EXPECTED, &of_node));
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	/* No msi-available-ranges, so expect > 0 */
23862306a36Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	/* Should all still be free */
24162306a36Sopenharmony_ci	WARN_ON(bitmap_find_free_region(bmp.bitmap, SIZE_EXPECTED,
24262306a36Sopenharmony_ci					get_count_order(SIZE_EXPECTED)));
24362306a36Sopenharmony_ci	bitmap_release_region(bmp.bitmap, 0, get_count_order(SIZE_EXPECTED));
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* Now create a fake msi-available-ranges property */
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	/* There should really .. oh whatever */
24862306a36Sopenharmony_ci	memset(&prop, 0, sizeof(prop));
24962306a36Sopenharmony_ci	prop.name = prop_name;
25062306a36Sopenharmony_ci	prop.value = &prop_data;
25162306a36Sopenharmony_ci	prop.length = sizeof(prop_data);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	of_node.properties = &prop;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	/* msi-available-ranges, so expect == 0 */
25662306a36Sopenharmony_ci	WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp));
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	/* Check we got the expected result */
25962306a36Sopenharmony_ci	WARN_ON(bitmap_parselist(expected_str, expected, SIZE_EXPECTED));
26062306a36Sopenharmony_ci	WARN_ON(!bitmap_equal(expected, bmp.bitmap, SIZE_EXPECTED));
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	msi_bitmap_free(&bmp);
26362306a36Sopenharmony_ci	kfree(bmp.bitmap);
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cistatic int __init msi_bitmap_selftest(void)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	test_basics();
27162306a36Sopenharmony_ci	test_of_node();
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return 0;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_cilate_initcall(msi_bitmap_selftest);
27662306a36Sopenharmony_ci#endif /* CONFIG_MSI_BITMAP_SELFTEST */
277