hns_roce_alloc.c 6.6 KB

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