msi_bitmap.c 7.4 KB

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