f950ec41a3f0574f69b621426350b797ba2876f2
[linux-2.6-microblaze.git] / drivers / infiniband / hw / hns / hns_roce_alloc.c
1 /*
2  * Copyright (c) 2016 Hisilicon Limited.
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
34 #include <linux/platform_device.h>
35 #include <linux/vmalloc.h>
36 #include "hns_roce_device.h"
37 #include <rdma/ib_umem.h>
38
39 int hns_roce_bitmap_alloc(struct hns_roce_bitmap *bitmap, unsigned long *obj)
40 {
41         int ret = 0;
42
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
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                 ret = -EINVAL;
59         }
60
61         spin_unlock(&bitmap->lock);
62
63         return ret;
64 }
65
66 void hns_roce_bitmap_free(struct hns_roce_bitmap *bitmap, unsigned long obj,
67                           int rr)
68 {
69         obj &= bitmap->max + bitmap->reserved_top - 1;
70
71         spin_lock(&bitmap->lock);
72         clear_bit(obj, bitmap->table);
73
74         if (!rr)
75                 bitmap->last = min(bitmap->last, obj);
76         bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
77                        & bitmap->mask;
78         spin_unlock(&bitmap->lock);
79 }
80
81 int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
82                          u32 reserved_bot, u32 reserved_top)
83 {
84         u32 i;
85
86         if (num != roundup_pow_of_two(num))
87                 return -EINVAL;
88
89         bitmap->last = 0;
90         bitmap->top = 0;
91         bitmap->max = num - reserved_top;
92         bitmap->mask = mask;
93         bitmap->reserved_top = reserved_top;
94         spin_lock_init(&bitmap->lock);
95         bitmap->table = kcalloc(BITS_TO_LONGS(bitmap->max), sizeof(long),
96                                 GFP_KERNEL);
97         if (!bitmap->table)
98                 return -ENOMEM;
99
100         for (i = 0; i < reserved_bot; ++i)
101                 set_bit(i, bitmap->table);
102
103         return 0;
104 }
105
106 void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap)
107 {
108         kfree(bitmap->table);
109 }
110
111 void hns_roce_buf_free(struct hns_roce_dev *hr_dev, struct hns_roce_buf *buf)
112 {
113         struct hns_roce_buf_list *trunks;
114         u32 i;
115
116         if (!buf)
117                 return;
118
119         trunks = buf->trunk_list;
120         if (trunks) {
121                 buf->trunk_list = NULL;
122                 for (i = 0; i < buf->ntrunks; i++)
123                         dma_free_coherent(hr_dev->dev, 1 << buf->trunk_shift,
124                                           trunks[i].buf, trunks[i].map);
125
126                 kfree(trunks);
127         }
128
129         kfree(buf);
130 }
131
132 /*
133  * Allocate the dma buffer for storing ROCEE table entries
134  *
135  * @size: required size
136  * @page_shift: the unit size in a continuous dma address range
137  * @flags: HNS_ROCE_BUF_ flags to control the allocation flow.
138  */
139 struct hns_roce_buf *hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size,
140                                         u32 page_shift, u32 flags)
141 {
142         u32 trunk_size, page_size, alloced_size;
143         struct hns_roce_buf_list *trunks;
144         struct hns_roce_buf *buf;
145         gfp_t gfp_flags;
146         u32 ntrunk, i;
147
148         /* The minimum shift of the page accessed by hw is HNS_HW_PAGE_SHIFT */
149         if (WARN_ON(page_shift < HNS_HW_PAGE_SHIFT))
150                 return ERR_PTR(-EINVAL);
151
152         gfp_flags = (flags & HNS_ROCE_BUF_NOSLEEP) ? GFP_ATOMIC : GFP_KERNEL;
153         buf = kzalloc(sizeof(*buf), gfp_flags);
154         if (!buf)
155                 return ERR_PTR(-ENOMEM);
156
157         buf->page_shift = page_shift;
158         page_size = 1 << buf->page_shift;
159
160         /* Calc the trunk size and num by required size and page_shift */
161         if (flags & HNS_ROCE_BUF_DIRECT) {
162                 buf->trunk_shift = order_base_2(ALIGN(size, PAGE_SIZE));
163                 ntrunk = 1;
164         } else {
165                 buf->trunk_shift = order_base_2(ALIGN(page_size, PAGE_SIZE));
166                 ntrunk = DIV_ROUND_UP(size, 1 << buf->trunk_shift);
167         }
168
169         trunks = kcalloc(ntrunk, sizeof(*trunks), gfp_flags);
170         if (!trunks) {
171                 kfree(buf);
172                 return ERR_PTR(-ENOMEM);
173         }
174
175         trunk_size = 1 << buf->trunk_shift;
176         alloced_size = 0;
177         for (i = 0; i < ntrunk; i++) {
178                 trunks[i].buf = dma_alloc_coherent(hr_dev->dev, trunk_size,
179                                                    &trunks[i].map, gfp_flags);
180                 if (!trunks[i].buf)
181                         break;
182
183                 alloced_size += trunk_size;
184         }
185
186         buf->ntrunks = i;
187
188         /* In nofail mode, it's only failed when the alloced size is 0 */
189         if ((flags & HNS_ROCE_BUF_NOFAIL) ? i == 0 : i != ntrunk) {
190                 for (i = 0; i < buf->ntrunks; i++)
191                         dma_free_coherent(hr_dev->dev, trunk_size,
192                                           trunks[i].buf, trunks[i].map);
193
194                 kfree(trunks);
195                 kfree(buf);
196                 return ERR_PTR(-ENOMEM);
197         }
198
199         buf->npages = DIV_ROUND_UP(alloced_size, page_size);
200         buf->trunk_list = trunks;
201
202         return buf;
203 }
204
205 int hns_roce_get_kmem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
206                            int buf_cnt, struct hns_roce_buf *buf,
207                            unsigned int page_shift)
208 {
209         unsigned int offset, max_size;
210         int total = 0;
211         int i;
212
213         if (page_shift > buf->trunk_shift) {
214                 dev_err(hr_dev->dev, "failed to check kmem buf shift %u > %u\n",
215                         page_shift, buf->trunk_shift);
216                 return -EINVAL;
217         }
218
219         offset = 0;
220         max_size = buf->ntrunks << buf->trunk_shift;
221         for (i = 0; i < buf_cnt && offset < max_size; i++) {
222                 bufs[total++] = hns_roce_buf_dma_addr(buf, offset);
223                 offset += (1 << page_shift);
224         }
225
226         return total;
227 }
228
229 int hns_roce_get_umem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
230                            int buf_cnt, struct ib_umem *umem,
231                            unsigned int page_shift)
232 {
233         struct ib_block_iter biter;
234         int total = 0;
235
236         /* convert system page cnt to hw page cnt */
237         rdma_umem_for_each_dma_block(umem, &biter, 1 << page_shift) {
238                 bufs[total++] = rdma_block_iter_dma_address(&biter);
239                 if (total >= buf_cnt)
240                         goto done;
241         }
242
243 done:
244         return total;
245 }
246
247 void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
248 {
249         if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
250                 hns_roce_cleanup_xrcd_table(hr_dev);
251
252         if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
253                 hns_roce_cleanup_srq_table(hr_dev);
254         hns_roce_cleanup_qp_table(hr_dev);
255         hns_roce_cleanup_cq_table(hr_dev);
256         hns_roce_cleanup_mr_table(hr_dev);
257         hns_roce_cleanup_pd_table(hr_dev);
258         hns_roce_cleanup_uar_table(hr_dev);
259 }