msi_bitmap.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2006-2008, Michael Ellerman, IBM Corporation.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/kernel.h>
  7. #include <linux/kmemleak.h>
  8. #include <linux/bitmap.h>
  9. #include <linux/memblock.h>
  10. #include <linux/of.h>
  11. #include <asm/msi_bitmap.h>
  12. #include <asm/setup.h>
  13. int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
  14. {
  15. unsigned long flags;
  16. int offset, order = get_count_order(num);
  17. spin_lock_irqsave(&bmp->lock, flags);
  18. offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
  19. num, (1 << order) - 1);
  20. if (offset > bmp->irq_count)
  21. goto err;
  22. bitmap_set(bmp->bitmap, offset, num);
  23. spin_unlock_irqrestore(&bmp->lock, flags);
  24. pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
  25. return offset;
  26. err:
  27. spin_unlock_irqrestore(&bmp->lock, flags);
  28. return -ENOMEM;
  29. }
  30. EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
  31. void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
  32. unsigned int num)
  33. {
  34. unsigned long flags;
  35. pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
  36. num, offset);
  37. spin_lock_irqsave(&bmp->lock, flags);
  38. bitmap_clear(bmp->bitmap, offset, num);
  39. spin_unlock_irqrestore(&bmp->lock, flags);
  40. }
  41. EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
  42. void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
  43. {
  44. unsigned long flags;
  45. pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
  46. spin_lock_irqsave(&bmp->lock, flags);
  47. bitmap_allocate_region(bmp->bitmap, hwirq, 0);
  48. spin_unlock_irqrestore(&bmp->lock, flags);
  49. }
  50. /**
  51. * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
  52. * @bmp: pointer to the MSI bitmap.
  53. *
  54. * Looks in the device tree to see if there is a property specifying which
  55. * irqs can be used for MSI. If found those irqs reserved in the device tree
  56. * are reserved in the bitmap.
  57. *
  58. * Returns 0 for success, < 0 if there was an error, and > 0 if no property
  59. * was found in the device tree.
  60. **/
  61. int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
  62. {
  63. int i, j, len;
  64. const u32 *p;
  65. if (!bmp->of_node)
  66. return 1;
  67. p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
  68. if (!p) {
  69. pr_debug("msi_bitmap: no msi-available-ranges property " \
  70. "found on %pOF\n", bmp->of_node);
  71. return 1;
  72. }
  73. if (len % (2 * sizeof(u32)) != 0) {
  74. printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
  75. " property on %pOF\n", bmp->of_node);
  76. return -EINVAL;
  77. }
  78. bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
  79. spin_lock(&bmp->lock);
  80. /* Format is: (<u32 start> <u32 count>)+ */
  81. len /= 2 * sizeof(u32);
  82. for (i = 0; i < len; i++, p += 2) {
  83. for (j = 0; j < *(p + 1); j++)
  84. bitmap_release_region(bmp->bitmap, *p + j, 0);
  85. }
  86. spin_unlock(&bmp->lock);
  87. return 0;
  88. }
  89. int __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
  90. struct device_node *of_node)
  91. {
  92. int size;
  93. if (!irq_count)
  94. return -EINVAL;
  95. size = BITS_TO_LONGS(irq_count) * sizeof(long);
  96. pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
  97. bmp->bitmap_from_slab = slab_is_available();
  98. if (bmp->bitmap_from_slab)
  99. bmp->bitmap = kzalloc(size, GFP_KERNEL);
  100. else {
  101. bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES);
  102. if (!bmp->bitmap)
  103. panic("%s: Failed to allocate %u bytes\n", __func__,
  104. size);
  105. /* the bitmap won't be freed from memblock allocator */
  106. kmemleak_not_leak(bmp->bitmap);
  107. }
  108. if (!bmp->bitmap) {
  109. pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
  110. return -ENOMEM;
  111. }
  112. /* We zalloc'ed the bitmap, so all irqs are free by default */
  113. spin_lock_init(&bmp->lock);
  114. bmp->of_node = of_node_get(of_node);
  115. bmp->irq_count = irq_count;
  116. return 0;
  117. }
  118. void msi_bitmap_free(struct msi_bitmap *bmp)
  119. {
  120. if (bmp->bitmap_from_slab)
  121. kfree(bmp->bitmap);
  122. of_node_put(bmp->of_node);
  123. bmp->bitmap = NULL;
  124. }
  125. #ifdef CONFIG_MSI_BITMAP_SELFTEST
  126. static void __init test_basics(void)
  127. {
  128. struct msi_bitmap bmp;
  129. int rc, i, size = 512;
  130. /* Can't allocate a bitmap of 0 irqs */
  131. WARN_ON(msi_bitmap_alloc(&bmp, 0, NULL) == 0);
  132. /* of_node may be NULL */
  133. WARN_ON(msi_bitmap_alloc(&bmp, size, NULL));
  134. /* Should all be free by default */
  135. WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
  136. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  137. /* With no node, there's no msi-available-ranges, so expect > 0 */
  138. WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
  139. /* Should all still be free */
  140. WARN_ON(bitmap_find_free_region(bmp.bitmap, size, get_count_order(size)));
  141. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  142. /* Check we can fill it up and then no more */
  143. for (i = 0; i < size; i++)
  144. WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) < 0);
  145. WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) >= 0);
  146. /* Should all be allocated */
  147. WARN_ON(bitmap_find_free_region(bmp.bitmap, size, 0) >= 0);
  148. /* And if we free one we can then allocate another */
  149. msi_bitmap_free_hwirqs(&bmp, size / 2, 1);
  150. WARN_ON(msi_bitmap_alloc_hwirqs(&bmp, 1) != size / 2);
  151. /* Free most of them for the alignment tests */
  152. msi_bitmap_free_hwirqs(&bmp, 3, size - 3);
  153. /* Check we get a naturally aligned offset */
  154. rc = msi_bitmap_alloc_hwirqs(&bmp, 2);
  155. WARN_ON(rc < 0 && rc % 2 != 0);
  156. rc = msi_bitmap_alloc_hwirqs(&bmp, 4);
  157. WARN_ON(rc < 0 && rc % 4 != 0);
  158. rc = msi_bitmap_alloc_hwirqs(&bmp, 8);
  159. WARN_ON(rc < 0 && rc % 8 != 0);
  160. rc = msi_bitmap_alloc_hwirqs(&bmp, 9);
  161. WARN_ON(rc < 0 && rc % 16 != 0);
  162. rc = msi_bitmap_alloc_hwirqs(&bmp, 3);
  163. WARN_ON(rc < 0 && rc % 4 != 0);
  164. rc = msi_bitmap_alloc_hwirqs(&bmp, 7);
  165. WARN_ON(rc < 0 && rc % 8 != 0);
  166. rc = msi_bitmap_alloc_hwirqs(&bmp, 121);
  167. WARN_ON(rc < 0 && rc % 128 != 0);
  168. msi_bitmap_free(&bmp);
  169. /* Clients may WARN_ON bitmap == NULL for "not-allocated" */
  170. WARN_ON(bmp.bitmap != NULL);
  171. }
  172. static void __init test_of_node(void)
  173. {
  174. u32 prop_data[] = { 10, 10, 25, 3, 40, 1, 100, 100, 200, 20 };
  175. const char *expected_str = "0-9,20-24,28-39,41-99,220-255";
  176. char *prop_name = "msi-available-ranges";
  177. char *node_name = "/fakenode";
  178. struct device_node of_node;
  179. struct property prop;
  180. struct msi_bitmap bmp;
  181. #define SIZE_EXPECTED 256
  182. DECLARE_BITMAP(expected, SIZE_EXPECTED);
  183. /* There should really be a struct device_node allocator */
  184. memset(&of_node, 0, sizeof(of_node));
  185. of_node_init(&of_node);
  186. of_node.full_name = node_name;
  187. WARN_ON(msi_bitmap_alloc(&bmp, SIZE_EXPECTED, &of_node));
  188. /* No msi-available-ranges, so expect > 0 */
  189. WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp) <= 0);
  190. /* Should all still be free */
  191. WARN_ON(bitmap_find_free_region(bmp.bitmap, SIZE_EXPECTED,
  192. get_count_order(SIZE_EXPECTED)));
  193. bitmap_release_region(bmp.bitmap, 0, get_count_order(SIZE_EXPECTED));
  194. /* Now create a fake msi-available-ranges property */
  195. /* There should really .. oh whatever */
  196. memset(&prop, 0, sizeof(prop));
  197. prop.name = prop_name;
  198. prop.value = &prop_data;
  199. prop.length = sizeof(prop_data);
  200. of_node.properties = &prop;
  201. /* msi-available-ranges, so expect == 0 */
  202. WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp));
  203. /* Check we got the expected result */
  204. WARN_ON(bitmap_parselist(expected_str, expected, SIZE_EXPECTED));
  205. WARN_ON(!bitmap_equal(expected, bmp.bitmap, SIZE_EXPECTED));
  206. msi_bitmap_free(&bmp);
  207. kfree(bmp.bitmap);
  208. }
  209. static int __init msi_bitmap_selftest(void)
  210. {
  211. printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
  212. test_basics();
  213. test_of_node();
  214. return 0;
  215. }
  216. late_initcall(msi_bitmap_selftest);
  217. #endif /* CONFIG_MSI_BITMAP_SELFTEST */