2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: mthca_allocator.c 1349 2004-12-16 21:09:43Z roland $
35 #include <linux/errno.h>
36 #include <linux/slab.h>
37 #include <linux/bitmap.h>
39 #include "mthca_dev.h"
41 /* Trivial bitmap-based allocator */
42 u32 mthca_alloc(struct mthca_alloc *alloc)
46 spin_lock(&alloc->lock);
47 obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
48 if (obj >= alloc->max) {
49 alloc->top = (alloc->top + alloc->max) & alloc->mask;
50 obj = find_first_zero_bit(alloc->table, alloc->max);
53 if (obj < alloc->max) {
54 set_bit(obj, alloc->table);
59 spin_unlock(&alloc->lock);
64 void mthca_free(struct mthca_alloc *alloc, u32 obj)
66 obj &= alloc->max - 1;
67 spin_lock(&alloc->lock);
68 clear_bit(obj, alloc->table);
69 alloc->last = min(alloc->last, obj);
70 alloc->top = (alloc->top + alloc->max) & alloc->mask;
71 spin_unlock(&alloc->lock);
74 int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,
79 /* num must be a power of 2 */
80 if (num != 1 << (ffs(num) - 1))
87 spin_lock_init(&alloc->lock);
88 alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof (long),
93 bitmap_zero(alloc->table, num);
94 for (i = 0; i < reserved; ++i)
95 set_bit(i, alloc->table);
100 void mthca_alloc_cleanup(struct mthca_alloc *alloc)
106 * Array of pointers with lazy allocation of leaf pages. Callers of
107 * _get, _set and _clear methods must use a lock or otherwise
108 * serialize access to the array.
111 #define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)
113 void *mthca_array_get(struct mthca_array *array, int index)
115 int p = (index * sizeof (void *)) >> PAGE_SHIFT;
117 if (array->page_list[p].page)
118 return array->page_list[p].page[index & MTHCA_ARRAY_MASK];
123 int mthca_array_set(struct mthca_array *array, int index, void *value)
125 int p = (index * sizeof (void *)) >> PAGE_SHIFT;
127 /* Allocate with GFP_ATOMIC because we'll be called with locks held. */
128 if (!array->page_list[p].page)
129 array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
131 if (!array->page_list[p].page)
134 array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;
135 ++array->page_list[p].used;
140 void mthca_array_clear(struct mthca_array *array, int index)
142 int p = (index * sizeof (void *)) >> PAGE_SHIFT;
144 if (--array->page_list[p].used == 0) {
145 free_page((unsigned long) array->page_list[p].page);
146 array->page_list[p].page = NULL;
148 array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
150 if (array->page_list[p].used < 0)
151 pr_debug("Array %p index %d page %d with ref count %d < 0\n",
152 array, index, p, array->page_list[p].used);
155 int mthca_array_init(struct mthca_array *array, int nent)
157 int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;
160 array->page_list = kmalloc(npage * sizeof *array->page_list, GFP_KERNEL);
161 if (!array->page_list)
164 for (i = 0; i < npage; ++i) {
165 array->page_list[i].page = NULL;
166 array->page_list[i].used = 0;
172 void mthca_array_cleanup(struct mthca_array *array, int nent)
176 for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
177 free_page((unsigned long) array->page_list[i].page);
179 kfree(array->page_list);
183 * Handling for queue buffers -- we allocate a bunch of memory and
184 * register it in a memory region at HCA virtual address 0. If the
185 * requested size is > max_direct, we split the allocation into
186 * multiple pages, so we don't require too much contiguous memory.
189 int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,
190 union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,
191 int hca_write, struct mthca_mr *mr)
195 u64 *dma_list = NULL;
199 if (size <= max_direct) {
202 shift = get_order(size) + PAGE_SHIFT;
204 buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
205 size, &t, GFP_KERNEL);
206 if (!buf->direct.buf)
209 pci_unmap_addr_set(&buf->direct, mapping, t);
211 memset(buf->direct.buf, 0, size);
213 while (t & ((1 << shift) - 1)) {
218 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
222 for (i = 0; i < npages; ++i)
223 dma_list[i] = t + i * (1 << shift);
226 npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
229 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
233 buf->page_list = kmalloc(npages * sizeof *buf->page_list,
238 for (i = 0; i < npages; ++i)
239 buf->page_list[i].buf = NULL;
241 for (i = 0; i < npages; ++i) {
242 buf->page_list[i].buf =
243 dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
245 if (!buf->page_list[i].buf)
249 pci_unmap_addr_set(&buf->page_list[i], mapping, t);
251 memset(buf->page_list[i].buf, 0, PAGE_SIZE);
255 err = mthca_mr_alloc_phys(dev, pd->pd_num,
256 dma_list, shift, npages,
258 MTHCA_MPT_FLAG_LOCAL_READ |
259 (hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),
269 mthca_buf_free(dev, size, buf, *is_direct, NULL);
277 void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,
278 int is_direct, struct mthca_mr *mr)
283 mthca_free_mr(dev, mr);
286 dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
287 pci_unmap_addr(&buf->direct, mapping));
289 for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
290 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
291 buf->page_list[i].buf,
292 pci_unmap_addr(&buf->page_list[i],
294 kfree(buf->page_list);