hns_roce_alloc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. int hns_roce_bitmap_alloc_range(struct hns_roce_bitmap *bitmap, int cnt,
  64. int align, unsigned long *obj)
  65. {
  66. int ret = 0;
  67. int i;
  68. if (likely(cnt == 1 && align == 1))
  69. return hns_roce_bitmap_alloc(bitmap, obj);
  70. spin_lock(&bitmap->lock);
  71. *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max,
  72. bitmap->last, cnt, align - 1);
  73. if (*obj >= bitmap->max) {
  74. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  75. & bitmap->mask;
  76. *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max, 0,
  77. cnt, align - 1);
  78. }
  79. if (*obj < bitmap->max) {
  80. for (i = 0; i < cnt; i++)
  81. set_bit(*obj + i, bitmap->table);
  82. if (*obj == bitmap->last) {
  83. bitmap->last = (*obj + cnt);
  84. if (bitmap->last >= bitmap->max)
  85. bitmap->last = 0;
  86. }
  87. *obj |= bitmap->top;
  88. } else {
  89. ret = -1;
  90. }
  91. spin_unlock(&bitmap->lock);
  92. return ret;
  93. }
  94. void hns_roce_bitmap_free_range(struct hns_roce_bitmap *bitmap,
  95. unsigned long obj, int cnt,
  96. int rr)
  97. {
  98. int i;
  99. obj &= bitmap->max + bitmap->reserved_top - 1;
  100. spin_lock(&bitmap->lock);
  101. for (i = 0; i < cnt; i++)
  102. clear_bit(obj + i, bitmap->table);
  103. if (!rr)
  104. bitmap->last = min(bitmap->last, obj);
  105. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  106. & bitmap->mask;
  107. spin_unlock(&bitmap->lock);
  108. }
  109. int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
  110. u32 reserved_bot, u32 reserved_top)
  111. {
  112. u32 i;
  113. if (num != roundup_pow_of_two(num))
  114. return -EINVAL;
  115. bitmap->last = 0;
  116. bitmap->top = 0;
  117. bitmap->max = num - reserved_top;
  118. bitmap->mask = mask;
  119. bitmap->reserved_top = reserved_top;
  120. spin_lock_init(&bitmap->lock);
  121. bitmap->table = kcalloc(BITS_TO_LONGS(bitmap->max), sizeof(long),
  122. GFP_KERNEL);
  123. if (!bitmap->table)
  124. return -ENOMEM;
  125. for (i = 0; i < reserved_bot; ++i)
  126. set_bit(i, bitmap->table);
  127. return 0;
  128. }
  129. void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap)
  130. {
  131. kfree(bitmap->table);
  132. }
  133. void hns_roce_buf_free(struct hns_roce_dev *hr_dev, u32 size,
  134. struct hns_roce_buf *buf)
  135. {
  136. int i;
  137. struct device *dev = &hr_dev->pdev->dev;
  138. u32 bits_per_long = BITS_PER_LONG;
  139. if (buf->nbufs == 1) {
  140. dma_free_coherent(dev, size, buf->direct.buf, buf->direct.map);
  141. } else {
  142. if (bits_per_long == 64)
  143. vunmap(buf->direct.buf);
  144. for (i = 0; i < buf->nbufs; ++i)
  145. if (buf->page_list[i].buf)
  146. dma_free_coherent(&hr_dev->pdev->dev, PAGE_SIZE,
  147. buf->page_list[i].buf,
  148. buf->page_list[i].map);
  149. kfree(buf->page_list);
  150. }
  151. }
  152. int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
  153. struct hns_roce_buf *buf)
  154. {
  155. int i = 0;
  156. dma_addr_t t;
  157. struct page **pages;
  158. struct device *dev = &hr_dev->pdev->dev;
  159. u32 bits_per_long = BITS_PER_LONG;
  160. /* SQ/RQ buf lease than one page, SQ + RQ = 8K */
  161. if (size <= max_direct) {
  162. buf->nbufs = 1;
  163. /* Npages calculated by page_size */
  164. buf->npages = 1 << get_order(size);
  165. buf->page_shift = PAGE_SHIFT;
  166. /* MTT PA must be recorded in 4k alignment, t is 4k aligned */
  167. buf->direct.buf = dma_alloc_coherent(dev, size, &t, GFP_KERNEL);
  168. if (!buf->direct.buf)
  169. return -ENOMEM;
  170. buf->direct.map = t;
  171. while (t & ((1 << buf->page_shift) - 1)) {
  172. --buf->page_shift;
  173. buf->npages *= 2;
  174. }
  175. memset(buf->direct.buf, 0, size);
  176. } else {
  177. buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  178. buf->npages = buf->nbufs;
  179. buf->page_shift = PAGE_SHIFT;
  180. buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
  181. GFP_KERNEL);
  182. if (!buf->page_list)
  183. return -ENOMEM;
  184. for (i = 0; i < buf->nbufs; ++i) {
  185. buf->page_list[i].buf = dma_alloc_coherent(dev,
  186. PAGE_SIZE, &t,
  187. GFP_KERNEL);
  188. if (!buf->page_list[i].buf)
  189. goto err_free;
  190. buf->page_list[i].map = t;
  191. memset(buf->page_list[i].buf, 0, PAGE_SIZE);
  192. }
  193. if (bits_per_long == 64) {
  194. pages = kmalloc_array(buf->nbufs, sizeof(*pages),
  195. GFP_KERNEL);
  196. if (!pages)
  197. goto err_free;
  198. for (i = 0; i < buf->nbufs; ++i)
  199. pages[i] = virt_to_page(buf->page_list[i].buf);
  200. buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP,
  201. PAGE_KERNEL);
  202. kfree(pages);
  203. if (!buf->direct.buf)
  204. goto err_free;
  205. }
  206. }
  207. return 0;
  208. err_free:
  209. hns_roce_buf_free(hr_dev, size, buf);
  210. return -ENOMEM;
  211. }
  212. void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
  213. {
  214. hns_roce_cleanup_qp_table(hr_dev);
  215. hns_roce_cleanup_cq_table(hr_dev);
  216. hns_roce_cleanup_mr_table(hr_dev);
  217. hns_roce_cleanup_pd_table(hr_dev);
  218. hns_roce_cleanup_uar_table(hr_dev);
  219. }