msi_bitmap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <linux/bootmem.h>
  14. #include <asm/msi_bitmap.h>
  15. #include <asm/setup.h>
  16. int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num)
  17. {
  18. unsigned long flags;
  19. int offset, order = get_count_order(num);
  20. spin_lock_irqsave(&bmp->lock, flags);
  21. offset = bitmap_find_next_zero_area(bmp->bitmap, bmp->irq_count, 0,
  22. num, (1 << order) - 1);
  23. if (offset > bmp->irq_count)
  24. goto err;
  25. bitmap_set(bmp->bitmap, offset, num);
  26. spin_unlock_irqrestore(&bmp->lock, flags);
  27. pr_debug("msi_bitmap: allocated 0x%x at offset 0x%x\n", num, offset);
  28. return offset;
  29. err:
  30. spin_unlock_irqrestore(&bmp->lock, flags);
  31. return -ENOMEM;
  32. }
  33. EXPORT_SYMBOL(msi_bitmap_alloc_hwirqs);
  34. void msi_bitmap_free_hwirqs(struct msi_bitmap *bmp, unsigned int offset,
  35. unsigned int num)
  36. {
  37. unsigned long flags;
  38. pr_debug("msi_bitmap: freeing 0x%x at offset 0x%x\n",
  39. num, offset);
  40. spin_lock_irqsave(&bmp->lock, flags);
  41. bitmap_clear(bmp->bitmap, offset, num);
  42. spin_unlock_irqrestore(&bmp->lock, flags);
  43. }
  44. EXPORT_SYMBOL(msi_bitmap_free_hwirqs);
  45. void msi_bitmap_reserve_hwirq(struct msi_bitmap *bmp, unsigned int hwirq)
  46. {
  47. unsigned long flags;
  48. pr_debug("msi_bitmap: reserving hwirq 0x%x\n", hwirq);
  49. spin_lock_irqsave(&bmp->lock, flags);
  50. bitmap_allocate_region(bmp->bitmap, hwirq, 0);
  51. spin_unlock_irqrestore(&bmp->lock, flags);
  52. }
  53. /**
  54. * msi_bitmap_reserve_dt_hwirqs - Reserve irqs specified in the device tree.
  55. * @bmp: pointer to the MSI bitmap.
  56. *
  57. * Looks in the device tree to see if there is a property specifying which
  58. * irqs can be used for MSI. If found those irqs reserved in the device tree
  59. * are reserved in the bitmap.
  60. *
  61. * Returns 0 for success, < 0 if there was an error, and > 0 if no property
  62. * was found in the device tree.
  63. **/
  64. int msi_bitmap_reserve_dt_hwirqs(struct msi_bitmap *bmp)
  65. {
  66. int i, j, len;
  67. const u32 *p;
  68. if (!bmp->of_node)
  69. return 1;
  70. p = of_get_property(bmp->of_node, "msi-available-ranges", &len);
  71. if (!p) {
  72. pr_debug("msi_bitmap: no msi-available-ranges property " \
  73. "found on %s\n", bmp->of_node->full_name);
  74. return 1;
  75. }
  76. if (len % (2 * sizeof(u32)) != 0) {
  77. printk(KERN_WARNING "msi_bitmap: Malformed msi-available-ranges"
  78. " property on %s\n", bmp->of_node->full_name);
  79. return -EINVAL;
  80. }
  81. bitmap_allocate_region(bmp->bitmap, 0, get_count_order(bmp->irq_count));
  82. spin_lock(&bmp->lock);
  83. /* Format is: (<u32 start> <u32 count>)+ */
  84. len /= 2 * sizeof(u32);
  85. for (i = 0; i < len; i++, p += 2) {
  86. for (j = 0; j < *(p + 1); j++)
  87. bitmap_release_region(bmp->bitmap, *p + j, 0);
  88. }
  89. spin_unlock(&bmp->lock);
  90. return 0;
  91. }
  92. int __init_refok msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
  93. struct device_node *of_node)
  94. {
  95. int size;
  96. if (!irq_count)
  97. return -EINVAL;
  98. size = BITS_TO_LONGS(irq_count) * sizeof(long);
  99. pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
  100. bmp->bitmap_from_slab = slab_is_available();
  101. if (bmp->bitmap_from_slab)
  102. bmp->bitmap = kzalloc(size, GFP_KERNEL);
  103. else {
  104. bmp->bitmap = memblock_virt_alloc(size, 0);
  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. int size = 256;
  182. DECLARE_BITMAP(expected, size);
  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, &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, get_count_order(size)));
  192. bitmap_release_region(bmp.bitmap, 0, get_count_order(size));
  193. /* Now create a fake msi-available-ranges property */
  194. /* There should really .. oh whatever */
  195. memset(&prop, 0, sizeof(prop));
  196. prop.name = prop_name;
  197. prop.value = &prop_data;
  198. prop.length = sizeof(prop_data);
  199. of_node.properties = &prop;
  200. /* msi-available-ranges, so expect == 0 */
  201. WARN_ON(msi_bitmap_reserve_dt_hwirqs(&bmp));
  202. /* Check we got the expected result */
  203. WARN_ON(bitmap_parselist(expected_str, expected, size));
  204. WARN_ON(!bitmap_equal(expected, bmp.bitmap, size));
  205. msi_bitmap_free(&bmp);
  206. kfree(bmp.bitmap);
  207. }
  208. static int __init msi_bitmap_selftest(void)
  209. {
  210. printk(KERN_DEBUG "Running MSI bitmap self-tests ...\n");
  211. test_basics();
  212. test_of_node();
  213. return 0;
  214. }
  215. late_initcall(msi_bitmap_selftest);
  216. #endif /* CONFIG_MSI_BITMAP_SELFTEST */