xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_mem.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * Copyright (c) 2013-2020, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #include "opt_rss.h"
27 #include "opt_ratelimit.h"
28 
29 #include <linux/module.h>
30 #include <rdma/ib_umem.h>
31 #include <rdma/ib_umem_odp.h>
32 #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
33 
34 /* @umem: umem object to scan
35  * @addr: ib virtual address requested by the user
36  * @max_page_shift: high limit for page_shift - 0 means no limit
37  * @count: number of PAGE_SIZE pages covered by umem
38  * @shift: page shift for the compound pages found in the region
39  * @ncont: number of compund pages
40  * @order: log2 of the number of compound pages
41  */
42 void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
43 			unsigned long max_page_shift,
44 			int *count, int *shift,
45 			int *ncont, int *order)
46 {
47 	unsigned long tmp;
48 	unsigned long m;
49 	u64 base = ~0, p = 0;
50 	u64 len, pfn;
51 	int i = 0;
52 	struct scatterlist *sg;
53 	int entry;
54 
55 	addr = addr >> PAGE_SHIFT;
56 	tmp = (unsigned long)addr;
57 	m = find_first_bit(&tmp, BITS_PER_LONG);
58 	if (max_page_shift)
59 		m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
60 
61 	for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
62 		len = sg_dma_len(sg) >> PAGE_SHIFT;
63 		pfn = sg_dma_address(sg) >> PAGE_SHIFT;
64 		if (base + p != pfn) {
65 			/* If either the offset or the new
66 			 * base are unaligned update m
67 			 */
68 			tmp = (unsigned long)(pfn | p);
69 			if (!IS_ALIGNED(tmp, 1 << m))
70 				m = find_first_bit(&tmp, BITS_PER_LONG);
71 
72 			base = pfn;
73 			p = 0;
74 		}
75 
76 		p += len;
77 		i += len;
78 	}
79 
80 	if (i) {
81 		m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
82 
83 		if (order)
84 			*order = ilog2(roundup_pow_of_two(i) >> m);
85 
86 		*ncont = DIV_ROUND_UP(i, (1 << m));
87 	} else {
88 		m  = 0;
89 
90 		if (order)
91 			*order = 0;
92 
93 		*ncont = 0;
94 	}
95 	*shift = PAGE_SHIFT + m;
96 	*count = i;
97 }
98 
99 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
100 static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
101 {
102 	u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
103 
104 	if (umem_dma & ODP_READ_ALLOWED_BIT)
105 		mtt_entry |= MLX5_IB_MTT_READ;
106 	if (umem_dma & ODP_WRITE_ALLOWED_BIT)
107 		mtt_entry |= MLX5_IB_MTT_WRITE;
108 
109 	return mtt_entry;
110 }
111 #endif
112 
113 /*
114  * Populate the given array with bus addresses from the umem.
115  *
116  * dev - mlx5_ib device
117  * umem - umem to use to fill the pages
118  * page_shift - determines the page size used in the resulting array
119  * offset - offset into the umem to start from,
120  *          only implemented for ODP umems
121  * num_pages - total number of pages to fill
122  * pas - bus addresses array to fill
123  * access_flags - access flags to set on all present pages.
124 		  use enum mlx5_ib_mtt_access_flags for this.
125  */
126 void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
127 			    int page_shift, size_t offset, size_t num_pages,
128 			    __be64 *pas, int access_flags)
129 {
130 	unsigned long umem_page_shift = ilog2(umem->page_size);
131 	int shift = page_shift - umem_page_shift;
132 	int mask = (1 << shift) - 1;
133 	int i, k;
134 	u64 cur = 0;
135 	u64 base;
136 	int len;
137 	struct scatterlist *sg;
138 	int entry;
139 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
140 	const bool odp = umem->odp_data != NULL;
141 
142 	if (odp) {
143 		WARN_ON(shift != 0);
144 		WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
145 
146 		for (i = 0; i < num_pages; ++i) {
147 			dma_addr_t pa = umem->odp_data->dma_list[offset + i];
148 
149 			pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
150 		}
151 		return;
152 	}
153 #endif
154 
155 	i = 0;
156 	for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
157 		len = sg_dma_len(sg) >> umem_page_shift;
158 		base = sg_dma_address(sg);
159 		for (k = 0; k < len; k++) {
160 			if (!(i & mask)) {
161 				cur = base + (k << umem_page_shift);
162 				cur |= access_flags;
163 
164 				pas[i >> shift] = cpu_to_be64(cur);
165 				mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
166 					    i >> shift, (long long)be64_to_cpu(pas[i >> shift]));
167 			}  else
168 				mlx5_ib_dbg(dev, "=====> 0x%llx\n",
169 					    (long long)(base + (k << umem_page_shift)));
170 			i++;
171 		}
172 	}
173 }
174 
175 void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
176 			  int page_shift, __be64 *pas, int access_flags)
177 {
178 	return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
179 				      ib_umem_num_pages(umem), pas,
180 				      access_flags);
181 }
182 int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
183 {
184 	u64 page_size;
185 	u64 page_mask;
186 	u64 off_size;
187 	u64 off_mask;
188 	u64 buf_off;
189 
190 	page_size = (u64)1 << page_shift;
191 	page_mask = page_size - 1;
192 	buf_off = addr & page_mask;
193 	off_size = page_size >> 6;
194 	off_mask = off_size - 1;
195 
196 	if (buf_off & off_mask)
197 		return -EINVAL;
198 
199 	*offset = buf_off >> ilog2(off_size);
200 	return 0;
201 }
202