alloc.c 8.5 KB

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