x86/platform/intel/iosf_mbi Rewrite locking
[linux-2.6-microblaze.git] / drivers / infiniband / hw / mlx5 / mem.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 #include <linux/module.h>
34 #include <rdma/ib_umem.h>
35 #include <rdma/ib_umem_odp.h>
36 #include "mlx5_ib.h"
37
38 /* @umem: umem object to scan
39  * @addr: ib virtual address requested by the user
40  * @max_page_shift: high limit for page_shift - 0 means no limit
41  * @count: number of PAGE_SIZE pages covered by umem
42  * @shift: page shift for the compound pages found in the region
43  * @ncont: number of compund pages
44  * @order: log2 of the number of compound pages
45  */
46 void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
47                         unsigned long max_page_shift,
48                         int *count, int *shift,
49                         int *ncont, int *order)
50 {
51         unsigned long tmp;
52         unsigned long m;
53         u64 base = ~0, p = 0;
54         u64 len, pfn;
55         int i = 0;
56         struct scatterlist *sg;
57         int entry;
58
59         if (umem->is_odp) {
60                 unsigned int page_shift = to_ib_umem_odp(umem)->page_shift;
61
62                 *ncont = ib_umem_page_count(umem);
63                 *count = *ncont << (page_shift - PAGE_SHIFT);
64                 *shift = page_shift;
65                 if (order)
66                         *order = ilog2(roundup_pow_of_two(*ncont));
67
68                 return;
69         }
70
71         addr = addr >> PAGE_SHIFT;
72         tmp = (unsigned long)addr;
73         m = find_first_bit(&tmp, BITS_PER_LONG);
74         if (max_page_shift)
75                 m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
76
77         for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
78                 len = sg_dma_len(sg) >> PAGE_SHIFT;
79                 pfn = sg_dma_address(sg) >> PAGE_SHIFT;
80                 if (base + p != pfn) {
81                         /* If either the offset or the new
82                          * base are unaligned update m
83                          */
84                         tmp = (unsigned long)(pfn | p);
85                         if (!IS_ALIGNED(tmp, 1 << m))
86                                 m = find_first_bit(&tmp, BITS_PER_LONG);
87
88                         base = pfn;
89                         p = 0;
90                 }
91
92                 p += len;
93                 i += len;
94         }
95
96         if (i) {
97                 m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
98
99                 if (order)
100                         *order = ilog2(roundup_pow_of_two(i) >> m);
101
102                 *ncont = DIV_ROUND_UP(i, (1 << m));
103         } else {
104                 m  = 0;
105
106                 if (order)
107                         *order = 0;
108
109                 *ncont = 0;
110         }
111         *shift = PAGE_SHIFT + m;
112         *count = i;
113 }
114
115 static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
116 {
117         u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
118
119         if (umem_dma & ODP_READ_ALLOWED_BIT)
120                 mtt_entry |= MLX5_IB_MTT_READ;
121         if (umem_dma & ODP_WRITE_ALLOWED_BIT)
122                 mtt_entry |= MLX5_IB_MTT_WRITE;
123
124         return mtt_entry;
125 }
126
127 /*
128  * Populate the given array with bus addresses from the umem.
129  *
130  * dev - mlx5_ib device
131  * umem - umem to use to fill the pages
132  * page_shift - determines the page size used in the resulting array
133  * offset - offset into the umem to start from,
134  *          only implemented for ODP umems
135  * num_pages - total number of pages to fill
136  * pas - bus addresses array to fill
137  * access_flags - access flags to set on all present pages.
138                   use enum mlx5_ib_mtt_access_flags for this.
139  */
140 void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
141                             int page_shift, size_t offset, size_t num_pages,
142                             __be64 *pas, int access_flags)
143 {
144         int shift = page_shift - PAGE_SHIFT;
145         int mask = (1 << shift) - 1;
146         int i, k, idx;
147         u64 cur = 0;
148         u64 base;
149         int len;
150         struct scatterlist *sg;
151         int entry;
152
153         if (umem->is_odp) {
154                 WARN_ON(shift != 0);
155                 WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
156
157                 for (i = 0; i < num_pages; ++i) {
158                         dma_addr_t pa =
159                                 to_ib_umem_odp(umem)->dma_list[offset + i];
160
161                         pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
162                 }
163                 return;
164         }
165
166         i = 0;
167         for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
168                 len = sg_dma_len(sg) >> PAGE_SHIFT;
169                 base = sg_dma_address(sg);
170
171                 /* Skip elements below offset */
172                 if (i + len < offset << shift) {
173                         i += len;
174                         continue;
175                 }
176
177                 /* Skip pages below offset */
178                 if (i < offset << shift) {
179                         k = (offset << shift) - i;
180                         i = offset << shift;
181                 } else {
182                         k = 0;
183                 }
184
185                 for (; k < len; k++) {
186                         if (!(i & mask)) {
187                                 cur = base + (k << PAGE_SHIFT);
188                                 cur |= access_flags;
189                                 idx = (i >> shift) - offset;
190
191                                 pas[idx] = cpu_to_be64(cur);
192                                 mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
193                                             i >> shift, be64_to_cpu(pas[idx]));
194                         }
195                         i++;
196
197                         /* Stop after num_pages reached */
198                         if (i >> shift >= offset + num_pages)
199                                 return;
200                 }
201         }
202 }
203
204 void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
205                           int page_shift, __be64 *pas, int access_flags)
206 {
207         return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
208                                       ib_umem_num_pages(umem), pas,
209                                       access_flags);
210 }
211 int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
212 {
213         u64 page_size;
214         u64 page_mask;
215         u64 off_size;
216         u64 off_mask;
217         u64 buf_off;
218
219         page_size = (u64)1 << page_shift;
220         page_mask = page_size - 1;
221         buf_off = addr & page_mask;
222         off_size = page_size >> 6;
223         off_mask = off_size - 1;
224
225         if (buf_off & off_mask)
226                 return -EINVAL;
227
228         *offset = buf_off >> ilog2(off_size);
229         return 0;
230 }