Path: blob/master/arch/powerpc/sysdev/msi_bitmap.c
10817 views
/*1* Copyright 2006-2008, Michael Ellerman, IBM Corporation.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation; version 2 of the6* License.7*8*/910#include <linux/slab.h>11#include <linux/kernel.h>12#include <linux/bitmap.h>13#include <asm/msi_bitmap.h>1415int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)16{17unsigned long flags;18int offset, order = get_count_order(num);1920spin_lock_irqsave(&bmp->lock, flags);21/*22* This is fast, but stricter than we need. We might want to add23* a fallback routine which does a linear search with no alignment.24*/25offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order);26spin_unlock_irqrestore(&bmp->lock, flags);2728pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n",29num, order, offset);3031return offset;32}3334void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,35unsigned int num)36{37unsigned long flags;38int order = get_count_order(num);3940pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",41num, order, offset);4243spin_lock_irqsave(&bmp->lock, flags);44bitmap_release_region(bmp->bitmap, offset, order);45spin_unlock_irqrestore(&bmp->lock, flags);46}4748void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)49{50unsigned long flags;5152pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);5354spin_lock_irqsave(&bmp->lock, flags);55bitmap_allocate_region(bmp->bitmap, hwirq, 0);56spin_unlock_irqrestore(&bmp->lock, flags);57}5859/**60* msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.61* @bmp: pointer to the MSI bitmap.62*63* Looks in the device tree to see if there is a property specifying which64* irqs can be used for MSI. If found those irqs reserved in the device tree65* are reserved in the bitmap.66*67* Returns 0 for success, < 0 if there was an error, and > 0 if no property68* was found in the device tree.69**/70int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)71{72int i, j, len;73const u32 *p;7475if (!bmp->of_node)76return 1;7778p = of_get_property(bmp->of_node, "msi-available-ranges", &len);79if (!p) {80pr_debug("msi_bitmap: no msi-available-ranges property " \81"found on %s\n", bmp->of_node->full_name);82return 1;83}8485if (len % (2 * sizeof(u32)) != 0) {86printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"87" property on %s\n", bmp->of_node->full_name);88return -EINVAL;89}9091bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));9293spin_lock(&bmp->lock);9495/* Format is: (<u32 start> <u32 count>)+ */96len /= 2 * sizeof(u32);97for (i = 0; i < len; i++, p += 2) {98for (j = 0; j < *(p + 1); j++)99bitmap_release_region(bmp->bitmap, *p + j, 0);100}101102spin_unlock(&bmp->lock);103104return 0;105}106107int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,108struct device_node *of_node)109{110int size;111112if (!irq_count)113return -EINVAL;114115size = BITS_TO_LONGS(irq_count) * sizeof(long);116pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);117118bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL);119if (!bmp->bitmap) {120pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");121return -ENOMEM;122}123124/* We zalloc'ed the bitmap, so all irqs are free by default */125spin_lock_init(&bmp->lock);126bmp->of_node = of_node_get(of_node);127bmp->irq_count = irq_count;128129return 0;130}131132void msi_bitmap_free(struct msi_bitmap *bmp)133{134/* we can't free the bitmap we don't know if it's bootmem etc. */135of_node_put(bmp->of_node);136bmp->bitmap = NULL;137}138139#ifdef CONFIG_MSI_BITMAP_SELFTEST140141#define check(x) \142if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);143144void __init test_basics(void)145{146struct msi_bitmap bmp;147int i, size = 512;148149/* Can't allocate a bitmap of 0 irqs */150check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);151152/* of_node may be NULL */153check(0 == msi_bitmap_alloc(&bmp, size, NULL));154155/* Should all be free by default */156check(0 == bitmap_find_free_region(bmp.bitmap, size,157get_count_order(size)));158bitmap_release_region(bmp.bitmap, 0, get_count_order(size));159160/* With no node, there's no msi-available-ranges, so expect > 0 */161check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);162163/* Should all still be free */164check(0 == bitmap_find_free_region(bmp.bitmap, size,165get_count_order(size)));166bitmap_release_region(bmp.bitmap, 0, get_count_order(size));167168/* Check we can fill it up and then no more */169for (i = 0; i < size; i++)170check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);171172check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);173174/* Should all be allocated */175check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);176177/* And if we free one we can then allocate another */178msi_bitmap_free_hwirqs(&bmp, size / 2, 1);179check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);180181msi_bitmap_free(&bmp);182183/* Clients may check bitmap == NULL for "not-allocated" */184check(bmp.bitmap == NULL);185186kfree(bmp.bitmap);187}188189void __init test_of_node(void)190{191u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };192const char *expected_str = "0-9,20-24,28-39,41-99,220-255";193char *prop_name = "msi-available-ranges";194char *node_name = "/fakenode";195struct device_node of_node;196struct property prop;197struct msi_bitmap bmp;198int size = 256;199DECLARE_BITMAP(expected, size);200201/* There should really be a struct device_node allocator */202memset(&of_node, 0, sizeof(of_node));203kref_init(&of_node.kref);204of_node.full_name = node_name;205206check(0 == msi_bitmap_alloc(&bmp, size, &of_node));207208/* No msi-available-ranges, so expect > 0 */209check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);210211/* Should all still be free */212check(0 == bitmap_find_free_region(bmp.bitmap, size,213get_count_order(size)));214bitmap_release_region(bmp.bitmap, 0, get_count_order(size));215216/* Now create a fake msi-available-ranges property */217218/* There should really .. oh whatever */219memset(&prop, 0, sizeof(prop));220prop.name = prop_name;221prop.value = &prop_data;222prop.length = sizeof(prop_data);223224of_node.properties = ∝225226/* msi-available-ranges, so expect == 0 */227check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);228229/* Check we got the expected result */230check(0 == bitmap_parselist(expected_str, expected, size));231check(bitmap_equal(expected, bmp.bitmap, size));232233msi_bitmap_free(&bmp);234kfree(bmp.bitmap);235}236237int __init msi_bitmap_selftest(void)238{239printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");240241test_basics();242test_of_node();243244return 0;245}246late_initcall(msi_bitmap_selftest);247#endif /* CONFIG_MSI_BITMAP_SELFTEST */248249250