msi_bitmap.c 6.5 KB

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