alloc.c 9.8 KB

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