alloc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  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/errno.h>
  34. #include <linux/slab.h>
  35. #include <linux/bitmap.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/vmalloc.h>
  38. #include "mlx4.h"
  39. u32 mlx4_bitmap_alloc(struct mlx4_bitmap *bitmap)
  40. {
  41. u32 obj;
  42. spin_lock(&bitmap->lock);
  43. obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last);
  44. if (obj >= bitmap->max) {
  45. bitmap->top = (bitmap->top + bitmap->max) & bitmap->mask;
  46. obj = find_first_zero_bit(bitmap->table, bitmap->max);
  47. }
  48. if (obj < bitmap->max) {
  49. set_bit(obj, bitmap->table);
  50. bitmap->last = (obj + 1) & (bitmap->max - 1);
  51. obj |= bitmap->top;
  52. } else
  53. obj = -1;
  54. spin_unlock(&bitmap->lock);
  55. return obj;
  56. }
  57. void mlx4_bitmap_free(struct mlx4_bitmap *bitmap, u32 obj)
  58. {
  59. mlx4_bitmap_free_range(bitmap, obj, 1);
  60. }
  61. static unsigned long find_aligned_range(unsigned long *bitmap,
  62. u32 start, u32 nbits,
  63. int len, int align)
  64. {
  65. unsigned long end, i;
  66. again:
  67. start = ALIGN(start, align);
  68. while ((start < nbits) && test_bit(start, bitmap))
  69. start += align;
  70. if (start >= nbits)
  71. return -1;
  72. end = start+len;
  73. if (end > nbits)
  74. return -1;
  75. for (i = start + 1; i < end; i++) {
  76. if (test_bit(i, bitmap)) {
  77. start = i + 1;
  78. goto again;
  79. }
  80. }
  81. return start;
  82. }
  83. u32 mlx4_bitmap_alloc_range(struct mlx4_bitmap *bitmap, int cnt, int align)
  84. {
  85. u32 obj, i;
  86. if (likely(cnt == 1 && align == 1))
  87. return mlx4_bitmap_alloc(bitmap);
  88. spin_lock(&bitmap->lock);
  89. obj = find_aligned_range(bitmap->table, bitmap->last,
  90. bitmap->max, cnt, align);
  91. if (obj >= bitmap->max) {
  92. bitmap->top = (bitmap->top + bitmap->max) & bitmap->mask;
  93. obj = find_aligned_range(bitmap->table, 0,
  94. bitmap->max,
  95. cnt, align);
  96. }
  97. if (obj < bitmap->max) {
  98. for (i = 0; i < cnt; i++)
  99. set_bit(obj + i, bitmap->table);
  100. if (obj == bitmap->last) {
  101. bitmap->last = (obj + cnt);
  102. if (bitmap->last >= bitmap->max)
  103. bitmap->last = 0;
  104. }
  105. obj |= bitmap->top;
  106. } else
  107. obj = -1;
  108. spin_unlock(&bitmap->lock);
  109. return obj;
  110. }
  111. void mlx4_bitmap_free_range(struct mlx4_bitmap *bitmap, u32 obj, int cnt)
  112. {
  113. u32 i;
  114. obj &= bitmap->max - 1;
  115. spin_lock(&bitmap->lock);
  116. for (i = 0; i < cnt; i++)
  117. clear_bit(obj + i, bitmap->table);
  118. bitmap->last = min(bitmap->last, obj);
  119. bitmap->top = (bitmap->top + bitmap->max) & bitmap->mask;
  120. spin_unlock(&bitmap->lock);
  121. }
  122. int mlx4_bitmap_init(struct mlx4_bitmap *bitmap, u32 num, u32 mask, u32 reserved)
  123. {
  124. int i;
  125. /* num must be a power of 2 */
  126. if (num != roundup_pow_of_two(num))
  127. return -EINVAL;
  128. bitmap->last = 0;
  129. bitmap->top = 0;
  130. bitmap->max = num;
  131. bitmap->mask = mask;
  132. spin_lock_init(&bitmap->lock);
  133. bitmap->table = kzalloc(BITS_TO_LONGS(num) * sizeof (long), GFP_KERNEL);
  134. if (!bitmap->table)
  135. return -ENOMEM;
  136. for (i = 0; i < reserved; ++i)
  137. set_bit(i, bitmap->table);
  138. return 0;
  139. }
  140. void mlx4_bitmap_cleanup(struct mlx4_bitmap *bitmap)
  141. {
  142. kfree(bitmap->table);
  143. }
  144. /*
  145. * Handling for queue buffers -- we allocate a bunch of memory and
  146. * register it in a memory region at HCA virtual address 0. If the
  147. * requested size is > max_direct, we split the allocation into
  148. * multiple pages, so we don't require too much contiguous memory.
  149. */
  150. int mlx4_buf_alloc(struct mlx4_dev *dev, int size, int max_direct,
  151. struct mlx4_buf *buf)
  152. {
  153. dma_addr_t t;
  154. if (size <= max_direct) {
  155. buf->nbufs = 1;
  156. buf->npages = 1;
  157. buf->page_shift = get_order(size) + PAGE_SHIFT;
  158. buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
  159. size, &t, GFP_KERNEL);
  160. if (!buf->direct.buf)
  161. return -ENOMEM;
  162. buf->direct.map = t;
  163. while (t & ((1 << buf->page_shift) - 1)) {
  164. --buf->page_shift;
  165. buf->npages *= 2;
  166. }
  167. memset(buf->direct.buf, 0, size);
  168. } else {
  169. int i;
  170. buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  171. buf->npages = buf->nbufs;
  172. buf->page_shift = PAGE_SHIFT;
  173. buf->page_list = kzalloc(buf->nbufs * sizeof *buf->page_list,
  174. GFP_KERNEL);
  175. if (!buf->page_list)
  176. return -ENOMEM;
  177. for (i = 0; i < buf->nbufs; ++i) {
  178. buf->page_list[i].buf =
  179. dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  180. &t, GFP_KERNEL);
  181. if (!buf->page_list[i].buf)
  182. goto err_free;
  183. buf->page_list[i].map = t;
  184. memset(buf->page_list[i].buf, 0, PAGE_SIZE);
  185. }
  186. if (BITS_PER_LONG == 64) {
  187. struct page **pages;
  188. pages = kmalloc(sizeof *pages * buf->nbufs, GFP_KERNEL);
  189. if (!pages)
  190. goto err_free;
  191. for (i = 0; i < buf->nbufs; ++i)
  192. pages[i] = virt_to_page(buf->page_list[i].buf);
  193. buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
  194. kfree(pages);
  195. if (!buf->direct.buf)
  196. goto err_free;
  197. }
  198. }
  199. return 0;
  200. err_free:
  201. mlx4_buf_free(dev, size, buf);
  202. return -ENOMEM;
  203. }
  204. EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
  205. void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
  206. {
  207. int i;
  208. if (buf->nbufs == 1)
  209. dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
  210. buf->direct.map);
  211. else {
  212. if (BITS_PER_LONG == 64)
  213. vunmap(buf->direct.buf);
  214. for (i = 0; i < buf->nbufs; ++i)
  215. if (buf->page_list[i].buf)
  216. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  217. buf->page_list[i].buf,
  218. buf->page_list[i].map);
  219. kfree(buf->page_list);
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(mlx4_buf_free);
  223. static struct mlx4_db_pgdir *mlx4_alloc_db_pgdir(struct device *dma_device)
  224. {
  225. struct mlx4_db_pgdir *pgdir;
  226. pgdir = kzalloc(sizeof *pgdir, GFP_KERNEL);
  227. if (!pgdir)
  228. return NULL;
  229. bitmap_fill(pgdir->order1, MLX4_DB_PER_PAGE / 2);
  230. pgdir->bits[0] = pgdir->order0;
  231. pgdir->bits[1] = pgdir->order1;
  232. pgdir->db_page = dma_alloc_coherent(dma_device, PAGE_SIZE,
  233. &pgdir->db_dma, GFP_KERNEL);
  234. if (!pgdir->db_page) {
  235. kfree(pgdir);
  236. return NULL;
  237. }
  238. return pgdir;
  239. }
  240. static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir *pgdir,
  241. struct mlx4_db *db, int order)
  242. {
  243. int o;
  244. int i;
  245. for (o = order; o <= 1; ++o) {
  246. i = find_first_bit(pgdir->bits[o], MLX4_DB_PER_PAGE >> o);
  247. if (i < MLX4_DB_PER_PAGE >> o)
  248. goto found;
  249. }
  250. return -ENOMEM;
  251. found:
  252. clear_bit(i, pgdir->bits[o]);
  253. i <<= o;
  254. if (o > order)
  255. set_bit(i ^ 1, pgdir->bits[order]);
  256. db->u.pgdir = pgdir;
  257. db->index = i;
  258. db->db = pgdir->db_page + db->index;
  259. db->dma = pgdir->db_dma + db->index * 4;
  260. db->order = order;
  261. return 0;
  262. }
  263. int mlx4_db_alloc(struct mlx4_dev *dev, struct mlx4_db *db, int order)
  264. {
  265. struct mlx4_priv *priv = mlx4_priv(dev);
  266. struct mlx4_db_pgdir *pgdir;
  267. int ret = 0;
  268. mutex_lock(&priv->pgdir_mutex);
  269. list_for_each_entry(pgdir, &priv->pgdir_list, list)
  270. if (!mlx4_alloc_db_from_pgdir(pgdir, db, order))
  271. goto out;
  272. pgdir = mlx4_alloc_db_pgdir(&(dev->pdev->dev));
  273. if (!pgdir) {
  274. ret = -ENOMEM;
  275. goto out;
  276. }
  277. list_add(&pgdir->list, &priv->pgdir_list);
  278. /* This should never fail -- we just allocated an empty page: */
  279. WARN_ON(mlx4_alloc_db_from_pgdir(pgdir, db, order));
  280. out:
  281. mutex_unlock(&priv->pgdir_mutex);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL_GPL(mlx4_db_alloc);
  285. void mlx4_db_free(struct mlx4_dev *dev, struct mlx4_db *db)
  286. {
  287. struct mlx4_priv *priv = mlx4_priv(dev);
  288. int o;
  289. int i;
  290. mutex_lock(&priv->pgdir_mutex);
  291. o = db->order;
  292. i = db->index;
  293. if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
  294. clear_bit(i ^ 1, db->u.pgdir->order0);
  295. ++o;
  296. }
  297. i >>= o;
  298. set_bit(i, db->u.pgdir->bits[o]);
  299. if (bitmap_full(db->u.pgdir->order1, MLX4_DB_PER_PAGE / 2)) {
  300. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  301. db->u.pgdir->db_page, db->u.pgdir->db_dma);
  302. list_del(&db->u.pgdir->list);
  303. kfree(db->u.pgdir);
  304. }
  305. mutex_unlock(&priv->pgdir_mutex);
  306. }
  307. EXPORT_SYMBOL_GPL(mlx4_db_free);
  308. int mlx4_alloc_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  309. int size, int max_direct)
  310. {
  311. int err;
  312. err = mlx4_db_alloc(dev, &wqres->db, 1);
  313. if (err)
  314. return err;
  315. *wqres->db.db = 0;
  316. err = mlx4_buf_alloc(dev, size, max_direct, &wqres->buf);
  317. if (err)
  318. goto err_db;
  319. err = mlx4_mtt_init(dev, wqres->buf.npages, wqres->buf.page_shift,
  320. &wqres->mtt);
  321. if (err)
  322. goto err_buf;
  323. err = mlx4_buf_write_mtt(dev, &wqres->mtt, &wqres->buf);
  324. if (err)
  325. goto err_mtt;
  326. return 0;
  327. err_mtt:
  328. mlx4_mtt_cleanup(dev, &wqres->mtt);
  329. err_buf:
  330. mlx4_buf_free(dev, size, &wqres->buf);
  331. err_db:
  332. mlx4_db_free(dev, &wqres->db);
  333. return err;
  334. }
  335. EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
  336. void mlx4_free_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  337. int size)
  338. {
  339. mlx4_mtt_cleanup(dev, &wqres->mtt);
  340. mlx4_buf_free(dev, size, &wqres->buf);
  341. mlx4_db_free(dev, &wqres->db);
  342. }
  343. EXPORT_SYMBOL_GPL(mlx4_free_hwq_res);