hns_roce_alloc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. u32 bits_per_long = BITS_PER_LONG;
  140. if (buf->nbufs == 1) {
  141. dma_free_coherent(dev, size, buf->direct.buf, buf->direct.map);
  142. } else {
  143. if (bits_per_long == 64)
  144. vunmap(buf->direct.buf);
  145. for (i = 0; i < buf->nbufs; ++i)
  146. if (buf->page_list[i].buf)
  147. dma_free_coherent(dev, PAGE_SIZE,
  148. buf->page_list[i].buf,
  149. buf->page_list[i].map);
  150. kfree(buf->page_list);
  151. }
  152. }
  153. EXPORT_SYMBOL_GPL(hns_roce_buf_free);
  154. int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
  155. struct hns_roce_buf *buf)
  156. {
  157. int i = 0;
  158. dma_addr_t t;
  159. struct page **pages;
  160. struct device *dev = hr_dev->dev;
  161. u32 bits_per_long = BITS_PER_LONG;
  162. /* SQ/RQ buf lease than one page, SQ + RQ = 8K */
  163. if (size <= max_direct) {
  164. buf->nbufs = 1;
  165. /* Npages calculated by page_size */
  166. buf->npages = 1 << get_order(size);
  167. buf->page_shift = PAGE_SHIFT;
  168. /* MTT PA must be recorded in 4k alignment, t is 4k aligned */
  169. buf->direct.buf = dma_alloc_coherent(dev, size, &t, GFP_KERNEL);
  170. if (!buf->direct.buf)
  171. return -ENOMEM;
  172. buf->direct.map = t;
  173. while (t & ((1 << buf->page_shift) - 1)) {
  174. --buf->page_shift;
  175. buf->npages *= 2;
  176. }
  177. memset(buf->direct.buf, 0, size);
  178. } else {
  179. buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  180. buf->npages = buf->nbufs;
  181. buf->page_shift = PAGE_SHIFT;
  182. buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
  183. GFP_KERNEL);
  184. if (!buf->page_list)
  185. return -ENOMEM;
  186. for (i = 0; i < buf->nbufs; ++i) {
  187. buf->page_list[i].buf = dma_alloc_coherent(dev,
  188. PAGE_SIZE, &t,
  189. GFP_KERNEL);
  190. if (!buf->page_list[i].buf)
  191. goto err_free;
  192. buf->page_list[i].map = t;
  193. memset(buf->page_list[i].buf, 0, PAGE_SIZE);
  194. }
  195. if (bits_per_long == 64) {
  196. pages = kmalloc_array(buf->nbufs, sizeof(*pages),
  197. GFP_KERNEL);
  198. if (!pages)
  199. goto err_free;
  200. for (i = 0; i < buf->nbufs; ++i)
  201. pages[i] = virt_to_page(buf->page_list[i].buf);
  202. buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP,
  203. PAGE_KERNEL);
  204. kfree(pages);
  205. if (!buf->direct.buf)
  206. goto err_free;
  207. }
  208. }
  209. return 0;
  210. err_free:
  211. hns_roce_buf_free(hr_dev, size, buf);
  212. return -ENOMEM;
  213. }
  214. void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
  215. {
  216. hns_roce_cleanup_qp_table(hr_dev);
  217. hns_roce_cleanup_cq_table(hr_dev);
  218. hns_roce_cleanup_mr_table(hr_dev);
  219. hns_roce_cleanup_pd_table(hr_dev);
  220. hns_roce_cleanup_uar_table(hr_dev);
  221. }