2 * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <linux/module.h>
29 #include <rdma/ib_umem.h>
32 CTASSERT(sizeof(uintptr_t) == sizeof(unsigned long));
34 /* @umem: umem object to scan
35 * @addr: ib virtual address requested by the user
36 * @count: number of PAGE_SIZE pages covered by umem
37 * @shift: page shift for the compound pages found in the region
38 * @ncont: number of compund pages
39 * @order: log2 of the number of compound pages
42 void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr, int *count, int *shift,
43 int *ncont, int *order)
45 struct ib_umem_chunk *chunk;
55 struct scatterlist *sg;
56 unsigned long page_shift = ilog2(umem->page_size);
58 addr = addr >> page_shift;
59 tmp = (uintptr_t)addr;
60 m = find_first_bit(&tmp, 8 * sizeof(tmp));
65 list_for_each_entry(chunk, &umem->chunk_list, list) {
66 for (j = 0; j < chunk->nmap; j++) {
67 sg = chunk->page_list + j;
68 len = sg_dma_len(sg) >> page_shift;
69 pfn = sg_dma_address(sg) >> page_shift;
70 for (k = 0; k < len; k++) {
73 m = min_t(unsigned long, m,
74 find_first_bit(&tmp, 8 * sizeof(tmp)));
80 if (base + p != pfn) {
82 m = find_first_bit(&tmp, 8 * sizeof(tmp));
96 m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
99 *order = ilog2(roundup_pow_of_two(i) >> m);
101 *ncont = DIV_ROUND_UP(i, (1 << m));
110 *shift = page_shift + m;
115 * Populate the given array with bus addresses from the umem.
117 * dev - mlx5_ib device
118 * umem - umem to use to fill the pages
119 * page_shift - determines the page size used in the resulting array
120 * offset - offset into the umem to start from,
121 * only implemented for ODP umems
122 * num_pages - total number of pages to fill
123 * pas - bus addresses array to fill
124 * access_flags - access flags to set on all present pages.
125 use enum mlx5_ib_mtt_access_flags for this.
127 static void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
128 int page_shift, size_t offset,
129 __be64 *pas, int access_flags)
131 unsigned long umem_page_shift = ilog2(umem->page_size);
132 struct ib_umem_chunk *chunk;
133 int shift = page_shift - umem_page_shift;
134 int mask = (1 << shift) - 1;
139 struct scatterlist *sg;
142 list_for_each_entry(chunk, &umem->chunk_list, list) {
143 for (j = 0; j < chunk->nmap; j++) {
144 sg = chunk->page_list + j;
145 len = sg_dma_len(sg) >> umem_page_shift;
146 base = sg_dma_address(sg);
147 for (k = 0; k < len; k++) {
149 cur = base + (k << umem_page_shift);
152 pas[i >> shift] = cpu_to_be64(cur);
153 mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
154 i >> shift, (unsigned long long)
155 be64_to_cpu(pas[i >> shift]));
157 mlx5_ib_dbg(dev, "=====> 0x%llx\n",
159 (base + (k << umem_page_shift)));
166 void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
167 int page_shift, __be64 *pas, int access_flags)
169 return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
174 int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
182 page_size = (u64)1 << page_shift;
183 page_mask = page_size - 1;
184 buf_off = addr & page_mask;
185 off_size = page_size >> 6;
186 off_mask = off_size - 1;
188 if (buf_off & off_mask)
191 *offset = (u32)(buf_off >> ilog2(off_size));