alloc.c 8.4 KB

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