alloc.c 10 KB

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