msi_bitmap.c 6.5 KB

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