hns_roce_alloc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2016 Hisilicon Limited.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/platform_device.h>
  34. #include <linux/vmalloc.h>
  35. #include "hns_roce_device.h"
  36. int hns_roce_bitmap_alloc(struct hns_roce_bitmap *bitmap, unsigned long *obj)
  37. {
  38. int ret = 0;
  39. spin_lock(&bitmap->lock);
  40. *obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last);
  41. if (*obj >= bitmap->max) {
  42. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  43. & bitmap->mask;
  44. *obj = find_first_zero_bit(bitmap->table, bitmap->max);
  45. }
  46. if (*obj < bitmap->max) {
  47. set_bit(*obj, bitmap->table);
  48. bitmap->last = (*obj + 1);
  49. if (bitmap->last == bitmap->max)
  50. bitmap->last = 0;
  51. *obj |= bitmap->top;
  52. } else {
  53. ret = -1;
  54. }
  55. spin_unlock(&bitmap->lock);
  56. return ret;
  57. }
  58. void hns_roce_bitmap_free(struct hns_roce_bitmap *bitmap, unsigned long obj,
  59. int rr)
  60. {
  61. hns_roce_bitmap_free_range(bitmap, obj, 1, rr);
  62. }
  63. EXPORT_SYMBOL_GPL(hns_roce_bitmap_free);
  64. int hns_roce_bitmap_alloc_range(struct hns_roce_bitmap *bitmap, int cnt,
  65. int align, unsigned long *obj)
  66. {
  67. int ret = 0;
  68. int i;
  69. if (likely(cnt == 1 && align == 1))
  70. return hns_roce_bitmap_alloc(bitmap, obj);
  71. spin_lock(&bitmap->lock);
  72. *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max,
  73. bitmap->last, cnt, align - 1);
  74. if (*obj >= bitmap->max) {
  75. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  76. & bitmap->mask;
  77. *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max, 0,
  78. cnt, align - 1);
  79. }
  80. if (*obj < bitmap->max) {
  81. for (i = 0; i < cnt; i++)
  82. set_bit(*obj + i, bitmap->table);
  83. if (*obj == bitmap->last) {
  84. bitmap->last = (*obj + cnt);
  85. if (bitmap->last >= bitmap->max)
  86. bitmap->last = 0;
  87. }
  88. *obj |= bitmap->top;
  89. } else {
  90. ret = -1;
  91. }
  92. spin_unlock(&bitmap->lock);
  93. return ret;
  94. }
  95. void hns_roce_bitmap_free_range(struct hns_roce_bitmap *bitmap,
  96. unsigned long obj, int cnt,
  97. int rr)
  98. {
  99. int i;
  100. obj &= bitmap->max + bitmap->reserved_top - 1;
  101. spin_lock(&bitmap->lock);
  102. for (i = 0; i < cnt; i++)
  103. clear_bit(obj + i, bitmap->table);
  104. if (!rr)
  105. bitmap->last = min(bitmap->last, obj);
  106. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  107. & bitmap->mask;
  108. spin_unlock(&bitmap->lock);
  109. }
  110. int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
  111. u32 reserved_bot, u32 reserved_top)
  112. {
  113. u32 i;
  114. if (num != roundup_pow_of_two(num))
  115. return -EINVAL;
  116. bitmap->last = 0;
  117. bitmap->top = 0;
  118. bitmap->max = num - reserved_top;
  119. bitmap->mask = mask;
  120. bitmap->reserved_top = reserved_top;
  121. spin_lock_init(&bitmap->lock);
  122. bitmap->table = kcalloc(BITS_TO_LONGS(bitmap->max), sizeof(long),
  123. GFP_KERNEL);
  124. if (!bitmap->table)
  125. return -ENOMEM;
  126. for (i = 0; i < reserved_bot; ++i)
  127. set_bit(i, bitmap->table);
  128. return 0;
  129. }
  130. void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap)
  131. {
  132. kfree(bitmap->table);
  133. }
  134. void hns_roce_buf_free(struct hns_roce_dev *hr_dev, u32 size,
  135. struct hns_roce_buf *buf)
  136. {
  137. int i;
  138. struct device *dev = hr_dev->dev;
  139. if (buf->nbufs == 1) {
  140. dma_free_coherent(dev, size, buf->direct.buf, buf->direct.map);
  141. } else {
  142. for (i = 0; i < buf->nbufs; ++i)
  143. if (buf->page_list[i].buf)
  144. dma_free_coherent(dev, 1 << buf->page_shift,
  145. buf->page_list[i].buf,
  146. buf->page_list[i].map);
  147. kfree(buf->page_list);
  148. }
  149. }
  150. EXPORT_SYMBOL_GPL(hns_roce_buf_free);
  151. int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
  152. struct hns_roce_buf *buf, u32 page_shift)
  153. {
  154. int i = 0;
  155. dma_addr_t t;
  156. struct device *dev = hr_dev->dev;
  157. u32 page_size = 1 << page_shift;
  158. u32 order;
  159. /* SQ/RQ buf lease than one page, SQ + RQ = 8K */
  160. if (size <= max_direct) {
  161. buf->nbufs = 1;
  162. /* Npages calculated by page_size */
  163. order = get_order(size);
  164. if (order <= page_shift - PAGE_SHIFT)
  165. order = 0;
  166. else
  167. order -= page_shift - PAGE_SHIFT;
  168. buf->npages = 1 << order;
  169. buf->page_shift = page_shift;
  170. /* MTT PA must be recorded in 4k alignment, t is 4k aligned */
  171. buf->direct.buf = dma_zalloc_coherent(dev,
  172. size, &t, GFP_KERNEL);
  173. if (!buf->direct.buf)
  174. return -ENOMEM;
  175. buf->direct.map = t;
  176. while (t & ((1 << buf->page_shift) - 1)) {
  177. --buf->page_shift;
  178. buf->npages *= 2;
  179. }
  180. } else {
  181. buf->nbufs = (size + page_size - 1) / page_size;
  182. buf->npages = buf->nbufs;
  183. buf->page_shift = page_shift;
  184. buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
  185. GFP_KERNEL);
  186. if (!buf->page_list)
  187. return -ENOMEM;
  188. for (i = 0; i < buf->nbufs; ++i) {
  189. buf->page_list[i].buf = dma_zalloc_coherent(dev,
  190. page_size, &t,
  191. GFP_KERNEL);
  192. if (!buf->page_list[i].buf)
  193. goto err_free;
  194. buf->page_list[i].map = t;
  195. }
  196. }
  197. return 0;
  198. err_free:
  199. hns_roce_buf_free(hr_dev, size, buf);
  200. return -ENOMEM;
  201. }
  202. void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
  203. {
  204. hns_roce_cleanup_qp_table(hr_dev);
  205. hns_roce_cleanup_cq_table(hr_dev);
  206. hns_roce_cleanup_mr_table(hr_dev);
  207. hns_roce_cleanup_pd_table(hr_dev);
  208. hns_roce_cleanup_uar_table(hr_dev);
  209. }