msi_bitmap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; version 2 of the
  7. * License.
  8. *
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/kernel.h>
  12. #include <linux/bitmap.h>
  13. #include <asm/msi_bitmap.h>
  14. int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
  15. {
  16. unsigned long flags;
  17. int offset, order = get_count_order(num);
  18. spin_lock_irqsave(&bmp->lock, flags);
  19. /*
  20. * This is fast, but stricter than we need. We might want to add
  21. * a fallback routine which does a linear search with no alignment.
  22. */
  23. offset = bitmap_find_free_region(bmp->bitmap, bmp->irq_count, order);
  24. spin_unlock_irqrestore(&bmp->lock, flags);
  25. pr_debug("msi_bitmap: allocated 0x%x (2^%d) at offset 0x%x\n",
  26. num, order, offset);
  27. return offset;
  28. }
  29. void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
  30. unsigned int num)
  31. {
  32. unsigned long flags;
  33. int order = get_count_order(num);
  34. pr_debug("msi_bitmap: freeing 0x%x (2^%d) at offset 0x%x\n",
  35. num, order, offset);
  36. spin_lock_irqsave(&bmp->lock, flags);
  37. bitmap_release_region(bmp->bitmap, offset, order);
  38. spin_unlock_irqrestore(&bmp->lock, flags);
  39. }
  40. void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
  41. {
  42. unsigned long flags;
  43. pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
  44. spin_lock_irqsave(&bmp->lock, flags);
  45. bitmap_allocate_region(bmp->bitmap, hwirq, 0);
  46. spin_unlock_irqrestore(&bmp->lock, flags);
  47. }
  48. /**
  49. * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
  50. * @bmp: pointer to the MSI bitmap.
  51. *
  52. * Looks in the device tree to see if there is a property specifying which
  53. * irqs can be used for MSI. If found those irqs reserved in the device tree
  54. * are reserved in the bitmap.
  55. *
  56. * Returns 0 for success, < 0 if there was an error, and > 0 if no property
  57. * was found in the device tree.
  58. **/
  59. int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
  60. {
  61. int i, j, len;
  62. const u32 *p;
  63. if (!bmp->of_node)
  64. return 1;
  65. p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
  66. if (!p) {
  67. pr_debug("msi_bitmap: no msi-available-ranges property " \
  68. "found on %s\n", bmp->of_node->full_name);
  69. return 1;
  70. }
  71. if (len % (2 * sizeof(u32)) != 0) {
  72. printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
  73. " property on %s\n", bmp->of_node->full_name);
  74. return -EINVAL;
  75. }
  76. bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
  77. spin_lock(&bmp->lock);
  78. /* Format is: (<u32 start> <u32 count>)+ */
  79. len /= 2 * sizeof(u32);
  80. for (i = 0; i < len; i++, p += 2) {
  81. for (j = 0; j < *(p + 1); j++)
  82. bitmap_release_region(bmp->bitmap, *p + j, 0);
  83. }
  84. spin_unlock(&bmp->lock);
  85. return 0;
  86. }
  87. int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
  88. struct device_node *of_node)
  89. {
  90. int size;
  91. if (!irq_count)
  92. return -EINVAL;
  93. size = BITS_TO_LONGS(irq_count) * sizeof(long);
  94. pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
  95. bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL);
  96. if (!bmp->bitmap) {
  97. pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
  98. return -ENOMEM;
  99. }
  100. /* We zalloc'ed the bitmap, so all irqs are free by default */
  101. spin_lock_init(&bmp->lock);
  102. bmp->of_node = of_node_get(of_node);
  103. bmp->irq_count = irq_count;
  104. return 0;
  105. }
  106. void msi_bitmap_free(struct msi_bitmap *bmp)
  107. {
  108. /* we can't free the bitmap we don't know if it's bootmem etc. */
  109. of_node_put(bmp->of_node);
  110. bmp->bitmap = NULL;
  111. }
  112. #ifdef CONFIG_MSI_BITMAP_SELFTEST
  113. #define check(x) \
  114. if (!(x)) printk("msi_bitmap: test failed at line %d\n", __LINE__);
  115. void __init test_basics(void)
  116. {
  117. struct msi_bitmap bmp;
  118. int i, size = 512;
  119. /* Can't allocate a bitmap of 0 irqs */
  120. check(msi_bitmap_alloc(&bmp, 0, NULL) != 0);
  121. /* of_node may be NULL */
  122. check(0 == msi_bitmap_alloc(&bmp, size, NULL));
  123. /* Should all be free by default */
  124. check(0 == bitmap_find_free_region(bmp.bitmap, size,
  125. get_count_order(size)));
  126. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  127. /* With no node, there's no msi-available-ranges, so expect > 0 */
  128. check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
  129. /* Should all still be free */
  130. check(0 == bitmap_find_free_region(bmp.bitmap, size,
  131. get_count_order(size)));
  132. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  133. /* Check we can fill it up and then no more */
  134. for (i = 0; i < size; i++)
  135. check(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
  136. check(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
  137. /* Should all be allocated */
  138. check(bitmap_find_free_region(bmp.bitmap, size, 0) < 0);
  139. /* And if we free one we can then allocate another */
  140. msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
  141. check(msi_bitmap_alloc_hwirqs(&bmp, 1) == size / 2);
  142. msi_bitmap_free(&bmp);
  143. /* Clients may check bitmap == NULL for "not-allocated" */
  144. check(bmp.bitmap == NULL);
  145. kfree(bmp.bitmap);
  146. }
  147. void __init test_of_node(void)
  148. {
  149. u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
  150. const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
  151. char *prop_name = "msi-available-ranges";
  152. char *node_name = "/fakenode";
  153. struct device_node of_node;
  154. struct property prop;
  155. struct msi_bitmap bmp;
  156. int size = 256;
  157. DECLARE_BITMAP(expected, size);
  158. /* There should really be a struct device_node allocator */
  159. memset(&of_node, 0, sizeof(of_node));
  160. kref_init(&of_node.kref);
  161. of_node.full_name = node_name;
  162. check(0 == msi_bitmap_alloc(&bmp, size, &of_node));
  163. /* No msi-available-ranges, so expect > 0 */
  164. check(msi_bitmap_reserve_dt_hwirqs(&bmp) > 0);
  165. /* Should all still be free */
  166. check(0 == bitmap_find_free_region(bmp.bitmap, size,
  167. get_count_order(size)));
  168. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  169. /* Now create a fake msi-available-ranges property */
  170. /* There should really .. oh whatever */
  171. memset(&prop, 0, sizeof(prop));
  172. prop.name = prop_name;
  173. prop.value = &prop_data;
  174. prop.length = sizeof(prop_data);
  175. of_node.properties = &prop;
  176. /* msi-available-ranges, so expect == 0 */
  177. check(msi_bitmap_reserve_dt_hwirqs(&bmp) == 0);
  178. /* Check we got the expected result */
  179. check(0 == bitmap_parselist(expected_str, expected, size));
  180. check(bitmap_equal(expected, bmp.bitmap, size));
  181. msi_bitmap_free(&bmp);
  182. kfree(bmp.bitmap);
  183. }
  184. int __init msi_bitmap_selftest(void)
  185. {
  186. printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
  187. test_basics();
  188. test_of_node();
  189. return 0;
  190. }
  191. late_initcall(msi_bitmap_selftest);
  192. #endif /* CONFIG_MSI_BITMAP_SELFTEST */