xref: /openbmc/linux/drivers/infiniband/hw/mthca/mthca_profile.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  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  * $Id: mthca_profile.c 1349 2004-12-16 21:09:43Z roland $
33  */
34 
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 
38 #include "mthca_profile.h"
39 
40 enum {
41 	MTHCA_RES_QP,
42 	MTHCA_RES_EEC,
43 	MTHCA_RES_SRQ,
44 	MTHCA_RES_CQ,
45 	MTHCA_RES_EQP,
46 	MTHCA_RES_EEEC,
47 	MTHCA_RES_EQ,
48 	MTHCA_RES_RDB,
49 	MTHCA_RES_MCG,
50 	MTHCA_RES_MPT,
51 	MTHCA_RES_MTT,
52 	MTHCA_RES_UAR,
53 	MTHCA_RES_UDAV,
54 	MTHCA_RES_UARC,
55 	MTHCA_RES_NUM
56 };
57 
58 enum {
59 	MTHCA_NUM_EQS = 32,
60 	MTHCA_NUM_PDS = 1 << 15
61 };
62 
63 u64 mthca_make_profile(struct mthca_dev *dev,
64 		       struct mthca_profile *request,
65 		       struct mthca_dev_lim *dev_lim,
66 		       struct mthca_init_hca_param *init_hca)
67 {
68 	struct mthca_resource {
69 		u64 size;
70 		u64 start;
71 		int type;
72 		int num;
73 		int log_num;
74 	};
75 
76 	u64 mem_base, mem_avail;
77 	u64 total_size = 0;
78 	struct mthca_resource *profile;
79 	struct mthca_resource tmp;
80 	int i, j;
81 
82 	profile = kmalloc(MTHCA_RES_NUM * sizeof *profile, GFP_KERNEL);
83 	if (!profile)
84 		return -ENOMEM;
85 
86 	memset(profile, 0, MTHCA_RES_NUM * sizeof *profile);
87 
88 	profile[MTHCA_RES_QP].size   = dev_lim->qpc_entry_sz;
89 	profile[MTHCA_RES_EEC].size  = dev_lim->eec_entry_sz;
90 	profile[MTHCA_RES_SRQ].size  = dev_lim->srq_entry_sz;
91 	profile[MTHCA_RES_CQ].size   = dev_lim->cqc_entry_sz;
92 	profile[MTHCA_RES_EQP].size  = dev_lim->eqpc_entry_sz;
93 	profile[MTHCA_RES_EEEC].size = dev_lim->eeec_entry_sz;
94 	profile[MTHCA_RES_EQ].size   = dev_lim->eqc_entry_sz;
95 	profile[MTHCA_RES_RDB].size  = MTHCA_RDB_ENTRY_SIZE;
96 	profile[MTHCA_RES_MCG].size  = MTHCA_MGM_ENTRY_SIZE;
97 	profile[MTHCA_RES_MPT].size  = dev_lim->mpt_entry_sz;
98 	profile[MTHCA_RES_MTT].size  = dev_lim->mtt_seg_sz;
99 	profile[MTHCA_RES_UAR].size  = dev_lim->uar_scratch_entry_sz;
100 	profile[MTHCA_RES_UDAV].size = MTHCA_AV_SIZE;
101 	profile[MTHCA_RES_UARC].size = request->uarc_size;
102 
103 	profile[MTHCA_RES_QP].num    = request->num_qp;
104 	profile[MTHCA_RES_EQP].num   = request->num_qp;
105 	profile[MTHCA_RES_RDB].num   = request->num_qp * request->rdb_per_qp;
106 	profile[MTHCA_RES_CQ].num    = request->num_cq;
107 	profile[MTHCA_RES_EQ].num    = MTHCA_NUM_EQS;
108 	profile[MTHCA_RES_MCG].num   = request->num_mcg;
109 	profile[MTHCA_RES_MPT].num   = request->num_mpt;
110 	profile[MTHCA_RES_MTT].num   = request->num_mtt;
111 	profile[MTHCA_RES_UAR].num   = request->num_uar;
112 	profile[MTHCA_RES_UARC].num  = request->num_uar;
113 	profile[MTHCA_RES_UDAV].num  = request->num_udav;
114 
115 	for (i = 0; i < MTHCA_RES_NUM; ++i) {
116 		profile[i].type     = i;
117 		profile[i].log_num  = max(ffs(profile[i].num) - 1, 0);
118 		profile[i].size    *= profile[i].num;
119 		if (dev->hca_type == ARBEL_NATIVE)
120 			profile[i].size = max(profile[i].size, (u64) PAGE_SIZE);
121 	}
122 
123 	if (dev->hca_type == ARBEL_NATIVE) {
124 		mem_base  = 0;
125 		mem_avail = dev_lim->hca.arbel.max_icm_sz;
126 	} else {
127 		mem_base  = dev->ddr_start;
128 		mem_avail = dev->fw.tavor.fw_start - dev->ddr_start;
129 	}
130 
131 	/*
132 	 * Sort the resources in decreasing order of size.  Since they
133 	 * all have sizes that are powers of 2, we'll be able to keep
134 	 * resources aligned to their size and pack them without gaps
135 	 * using the sorted order.
136 	 */
137 	for (i = MTHCA_RES_NUM; i > 0; --i)
138 		for (j = 1; j < i; ++j) {
139 			if (profile[j].size > profile[j - 1].size) {
140 				tmp            = profile[j];
141 				profile[j]     = profile[j - 1];
142 				profile[j - 1] = tmp;
143 			}
144 		}
145 
146 	for (i = 0; i < MTHCA_RES_NUM; ++i) {
147 		if (profile[i].size) {
148 			profile[i].start = mem_base + total_size;
149 			total_size      += profile[i].size;
150 		}
151 		if (total_size > mem_avail) {
152 			mthca_err(dev, "Profile requires 0x%llx bytes; "
153 				  "won't in 0x%llx bytes of context memory.\n",
154 				  (unsigned long long) total_size,
155 				  (unsigned long long) mem_avail);
156 			kfree(profile);
157 			return -ENOMEM;
158 		}
159 
160 		if (profile[i].size)
161 			mthca_dbg(dev, "profile[%2d]--%2d/%2d @ 0x%16llx "
162 				  "(size 0x%8llx)\n",
163 				  i, profile[i].type, profile[i].log_num,
164 				  (unsigned long long) profile[i].start,
165 				  (unsigned long long) profile[i].size);
166 	}
167 
168 	if (dev->hca_type == ARBEL_NATIVE)
169 		mthca_dbg(dev, "HCA context memory: reserving %d KB\n",
170 			  (int) (total_size >> 10));
171 	else
172 		mthca_dbg(dev, "HCA memory: allocated %d KB/%d KB (%d KB free)\n",
173 			  (int) (total_size >> 10), (int) (mem_avail >> 10),
174 			  (int) ((mem_avail - total_size) >> 10));
175 
176 	for (i = 0; i < MTHCA_RES_NUM; ++i) {
177 		switch (profile[i].type) {
178 		case MTHCA_RES_QP:
179 			dev->limits.num_qps   = profile[i].num;
180 			init_hca->qpc_base    = profile[i].start;
181 			init_hca->log_num_qps = profile[i].log_num;
182 			break;
183 		case MTHCA_RES_EEC:
184 			dev->limits.num_eecs   = profile[i].num;
185 			init_hca->eec_base     = profile[i].start;
186 			init_hca->log_num_eecs = profile[i].log_num;
187 			break;
188 		case MTHCA_RES_SRQ:
189 			dev->limits.num_srqs   = profile[i].num;
190 			init_hca->srqc_base    = profile[i].start;
191 			init_hca->log_num_srqs = profile[i].log_num;
192 			break;
193 		case MTHCA_RES_CQ:
194 			dev->limits.num_cqs   = profile[i].num;
195 			init_hca->cqc_base    = profile[i].start;
196 			init_hca->log_num_cqs = profile[i].log_num;
197 			break;
198 		case MTHCA_RES_EQP:
199 			init_hca->eqpc_base = profile[i].start;
200 			break;
201 		case MTHCA_RES_EEEC:
202 			init_hca->eeec_base = profile[i].start;
203 			break;
204 		case MTHCA_RES_EQ:
205 			dev->limits.num_eqs   = profile[i].num;
206 			init_hca->eqc_base    = profile[i].start;
207 			init_hca->log_num_eqs = profile[i].log_num;
208 			break;
209 		case MTHCA_RES_RDB:
210 			for (dev->qp_table.rdb_shift = 0;
211 			     profile[MTHCA_RES_QP].num << dev->qp_table.rdb_shift <
212 				     profile[i].num;
213 			     ++dev->qp_table.rdb_shift)
214 				; /* nothing */
215 			dev->qp_table.rdb_base    = (u32) profile[i].start;
216 			init_hca->rdb_base        = profile[i].start;
217 			break;
218 		case MTHCA_RES_MCG:
219 			dev->limits.num_mgms      = profile[i].num >> 1;
220 			dev->limits.num_amgms     = profile[i].num >> 1;
221 			init_hca->mc_base         = profile[i].start;
222 			init_hca->log_mc_entry_sz = ffs(MTHCA_MGM_ENTRY_SIZE) - 1;
223 			init_hca->log_mc_table_sz = profile[i].log_num;
224 			init_hca->mc_hash_sz      = 1 << (profile[i].log_num - 1);
225 			break;
226 		case MTHCA_RES_MPT:
227 			dev->limits.num_mpts = profile[i].num;
228 			init_hca->mpt_base   = profile[i].start;
229 			init_hca->log_mpt_sz = profile[i].log_num;
230 			break;
231 		case MTHCA_RES_MTT:
232 			dev->limits.num_mtt_segs = profile[i].num;
233 			dev->limits.mtt_seg_size = dev_lim->mtt_seg_sz;
234 			dev->mr_table.mtt_base   = profile[i].start;
235 			init_hca->mtt_base       = profile[i].start;
236 			init_hca->mtt_seg_sz     = ffs(dev_lim->mtt_seg_sz) - 7;
237 			break;
238 		case MTHCA_RES_UAR:
239 			dev->limits.num_uars       = profile[i].num;
240 			init_hca->uar_scratch_base = profile[i].start;
241 			break;
242 		case MTHCA_RES_UDAV:
243 			dev->av_table.ddr_av_base = profile[i].start;
244 			dev->av_table.num_ddr_avs = profile[i].num;
245 			break;
246 		case MTHCA_RES_UARC:
247 			dev->uar_table.uarc_size = request->uarc_size;
248 			dev->uar_table.uarc_base = profile[i].start;
249 			init_hca->uarc_base   	 = profile[i].start;
250 			init_hca->log_uarc_sz 	 = ffs(request->uarc_size) - 13;
251 			init_hca->log_uar_sz  	 = ffs(request->num_uar) - 1;
252 			break;
253 		default:
254 			break;
255 		}
256 	}
257 
258 	/*
259 	 * PDs don't take any HCA memory, but we assign them as part
260 	 * of the HCA profile anyway.
261 	 */
262 	dev->limits.num_pds = MTHCA_NUM_PDS;
263 
264 	kfree(profile);
265 	return total_size;
266 }
267