alloc.c 9.7 KB

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