msi_bitmap.c 7.1 KB

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