2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/platform_device.h>
35 #include "hns_roce_device.h"
37 int hns_roce_bitmap_alloc(struct hns_roce_bitmap *bitmap, unsigned long *obj)
41 spin_lock(&bitmap->lock);
42 *obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last);
43 if (*obj >= bitmap->max) {
44 bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
46 *obj = find_first_zero_bit(bitmap->table, bitmap->max);
49 if (*obj < bitmap->max) {
50 set_bit(*obj, bitmap->table);
51 bitmap->last = (*obj + 1);
52 if (bitmap->last == bitmap->max)
59 spin_unlock(&bitmap->lock);
64 void hns_roce_bitmap_free(struct hns_roce_bitmap *bitmap, unsigned long obj)
66 hns_roce_bitmap_free_range(bitmap, obj, 1);
69 int hns_roce_bitmap_alloc_range(struct hns_roce_bitmap *bitmap, int cnt,
70 int align, unsigned long *obj)
75 if (likely(cnt == 1 && align == 1))
76 return hns_roce_bitmap_alloc(bitmap, obj);
78 spin_lock(&bitmap->lock);
80 *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max,
81 bitmap->last, cnt, align - 1);
82 if (*obj >= bitmap->max) {
83 bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
85 *obj = bitmap_find_next_zero_area(bitmap->table, bitmap->max, 0,
89 if (*obj < bitmap->max) {
90 for (i = 0; i < cnt; i++)
91 set_bit(*obj + i, bitmap->table);
93 if (*obj == bitmap->last) {
94 bitmap->last = (*obj + cnt);
95 if (bitmap->last >= bitmap->max)
103 spin_unlock(&bitmap->lock);
108 void hns_roce_bitmap_free_range(struct hns_roce_bitmap *bitmap,
109 unsigned long obj, int cnt)
113 obj &= bitmap->max + bitmap->reserved_top - 1;
115 spin_lock(&bitmap->lock);
116 for (i = 0; i < cnt; i++)
117 clear_bit(obj + i, bitmap->table);
119 bitmap->last = min(bitmap->last, obj);
120 bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
122 spin_unlock(&bitmap->lock);
125 int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
126 u32 reserved_bot, u32 reserved_top)
130 if (num != roundup_pow_of_two(num))
135 bitmap->max = num - reserved_top;
137 bitmap->reserved_top = reserved_top;
138 spin_lock_init(&bitmap->lock);
139 bitmap->table = kcalloc(BITS_TO_LONGS(bitmap->max), sizeof(long),
144 for (i = 0; i < reserved_bot; ++i)
145 set_bit(i, bitmap->table);
150 void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap)
152 kfree(bitmap->table);
155 void hns_roce_buf_free(struct hns_roce_dev *hr_dev, u32 size,
156 struct hns_roce_buf *buf)
159 struct device *dev = &hr_dev->pdev->dev;
160 u32 bits_per_long = BITS_PER_LONG;
162 if (buf->nbufs == 1) {
163 dma_free_coherent(dev, size, buf->direct.buf, buf->direct.map);
165 if (bits_per_long == 64)
166 vunmap(buf->direct.buf);
168 for (i = 0; i < buf->nbufs; ++i)
169 if (buf->page_list[i].buf)
170 dma_free_coherent(&hr_dev->pdev->dev, PAGE_SIZE,
171 buf->page_list[i].buf,
172 buf->page_list[i].map);
173 kfree(buf->page_list);
177 int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
178 struct hns_roce_buf *buf)
183 struct device *dev = &hr_dev->pdev->dev;
184 u32 bits_per_long = BITS_PER_LONG;
186 /* SQ/RQ buf lease than one page, SQ + RQ = 8K */
187 if (size <= max_direct) {
189 /* Npages calculated by page_size */
190 buf->npages = 1 << get_order(size);
191 buf->page_shift = PAGE_SHIFT;
192 /* MTT PA must be recorded in 4k alignment, t is 4k aligned */
193 buf->direct.buf = dma_alloc_coherent(dev, size, &t, GFP_KERNEL);
194 if (!buf->direct.buf)
199 while (t & ((1 << buf->page_shift) - 1)) {
204 memset(buf->direct.buf, 0, size);
206 buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
207 buf->npages = buf->nbufs;
208 buf->page_shift = PAGE_SHIFT;
209 buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
215 for (i = 0; i < buf->nbufs; ++i) {
216 buf->page_list[i].buf = dma_alloc_coherent(dev,
220 if (!buf->page_list[i].buf)
223 buf->page_list[i].map = t;
224 memset(buf->page_list[i].buf, 0, PAGE_SIZE);
226 if (bits_per_long == 64) {
227 pages = kmalloc_array(buf->nbufs, sizeof(*pages),
232 for (i = 0; i < buf->nbufs; ++i)
233 pages[i] = virt_to_page(buf->page_list[i].buf);
235 buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP,
238 if (!buf->direct.buf)
246 hns_roce_buf_free(hr_dev, size, buf);
250 void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
252 hns_roce_cleanup_qp_table(hr_dev);
253 hns_roce_cleanup_cq_table(hr_dev);
254 hns_roce_cleanup_mr_table(hr_dev);
255 hns_roce_cleanup_pd_table(hr_dev);
256 hns_roce_cleanup_uar_table(hr_dev);