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