Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
/* Copyright (c) 2015 - 2021 Intel Corporation */
#include "osdep.h"
#include "hmc.h"
#include "defs.h"
#include "type.h"
#include "protos.h"
#include "pble.h"

static int add_pble_prm(struct irdma_hmc_pble_rsrc *pble_rsrc);

/**
 * irdma_destroy_pble_prm - destroy prm during module unload
 * @pble_rsrc: pble resources
 */
void irdma_destroy_pble_prm(struct irdma_hmc_pble_rsrc *pble_rsrc)
{
	struct irdma_chunk *chunk;
	struct irdma_pble_prm *pinfo = &pble_rsrc->pinfo;

	while (!list_empty(&pinfo->clist)) {
		chunk = (struct irdma_chunk *) pinfo->clist.next;
		list_del(&chunk->list);
		if (chunk->type == PBLE_SD_PAGED)
			irdma_pble_free_paged_mem(chunk);
		bitmap_free(chunk->bitmapbuf);
		kfree(chunk->chunkmem.va);
	}
}

/**
 * irdma_hmc_init_pble - Initialize pble resources during module load
 * @dev: irdma_sc_dev struct
 * @pble_rsrc: pble resources
 */
int irdma_hmc_init_pble(struct irdma_sc_dev *dev,
			struct irdma_hmc_pble_rsrc *pble_rsrc)
{
	struct irdma_hmc_info *hmc_info;
	u32 fpm_idx = 0;
	int status = 0;

	hmc_info = dev->hmc_info;
	pble_rsrc->dev = dev;
	pble_rsrc->fpm_base_addr = hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].base;
	/* Start pble' on 4k boundary */
	if (pble_rsrc->fpm_base_addr & 0xfff)
		fpm_idx = (4096 - (pble_rsrc->fpm_base_addr & 0xfff)) >> 3;
	pble_rsrc->unallocated_pble =
		hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt - fpm_idx;
	pble_rsrc->next_fpm_addr = pble_rsrc->fpm_base_addr + (fpm_idx << 3);
	pble_rsrc->pinfo.pble_shift = PBLE_SHIFT;

	mutex_init(&pble_rsrc->pble_mutex_lock);

	spin_lock_init(&pble_rsrc->pinfo.prm_lock);
	INIT_LIST_HEAD(&pble_rsrc->pinfo.clist);
	if (add_pble_prm(pble_rsrc)) {
		irdma_destroy_pble_prm(pble_rsrc);
		status = -ENOMEM;
	}

	return status;
}

/**
 * get_sd_pd_idx -  Returns sd index, pd index and rel_pd_idx from fpm address
 * @pble_rsrc: structure containing fpm address
 * @idx: where to return indexes
 */
static void get_sd_pd_idx(struct irdma_hmc_pble_rsrc *pble_rsrc,
			  struct sd_pd_idx *idx)
{
	idx->sd_idx = (u32)pble_rsrc->next_fpm_addr / IRDMA_HMC_DIRECT_BP_SIZE;
	idx->pd_idx = (u32)(pble_rsrc->next_fpm_addr / IRDMA_HMC_PAGED_BP_SIZE);
	idx->rel_pd_idx = (idx->pd_idx % IRDMA_HMC_PD_CNT_IN_SD);
}

/**
 * add_sd_direct - add sd direct for pble
 * @pble_rsrc: pble resource ptr
 * @info: page info for sd
 */
static int add_sd_direct(struct irdma_hmc_pble_rsrc *pble_rsrc,
			 struct irdma_add_page_info *info)
{
	struct irdma_sc_dev *dev = pble_rsrc->dev;
	int ret_code = 0;
	struct sd_pd_idx *idx = &info->idx;
	struct irdma_chunk *chunk = info->chunk;
	struct irdma_hmc_info *hmc_info = info->hmc_info;
	struct irdma_hmc_sd_entry *sd_entry = info->sd_entry;
	u32 offset = 0;

	if (!sd_entry->valid) {
		ret_code = irdma_add_sd_table_entry(dev->hw, hmc_info,
						    info->idx.sd_idx,
						    IRDMA_SD_TYPE_DIRECT,
						    IRDMA_HMC_DIRECT_BP_SIZE);
		if (ret_code)
			return ret_code;

		chunk->type = PBLE_SD_CONTIGOUS;
	}

	offset = idx->rel_pd_idx << HMC_PAGED_BP_SHIFT;
	chunk->size = info->pages << HMC_PAGED_BP_SHIFT;
	chunk->vaddr = sd_entry->u.bp.addr.va + offset;
	chunk->fpm_addr = pble_rsrc->next_fpm_addr;
	ibdev_dbg(to_ibdev(dev),
		  "PBLE: chunk_size[%lld] = 0x%llx vaddr=0x%pK fpm_addr = %llx\n",
		  chunk->size, chunk->size, chunk->vaddr, chunk->fpm_addr);

	return 0;
}

/**
 * fpm_to_idx - given fpm address, get pble index
 * @pble_rsrc: pble resource management
 * @addr: fpm address for index
 */
static u32 fpm_to_idx(struct irdma_hmc_pble_rsrc *pble_rsrc, u64 addr)
{
	u64 idx;

	idx = (addr - (pble_rsrc->fpm_base_addr)) >> 3;

	return (u32)idx;
}

/**
 * add_bp_pages - add backing pages for sd
 * @pble_rsrc: pble resource management
 * @info: page info for sd
 */
static int add_bp_pages(struct irdma_hmc_pble_rsrc *pble_rsrc,
			struct irdma_add_page_info *info)
{
	struct irdma_sc_dev *dev = pble_rsrc->dev;
	u8 *addr;
	struct irdma_dma_mem mem;
	struct irdma_hmc_pd_entry *pd_entry;
	struct irdma_hmc_sd_entry *sd_entry = info->sd_entry;
	struct irdma_hmc_info *hmc_info = info->hmc_info;
	struct irdma_chunk *chunk = info->chunk;
	int status = 0;
	u32 rel_pd_idx = info->idx.rel_pd_idx;
	u32 pd_idx = info->idx.pd_idx;
	u32 i;

	if (irdma_pble_get_paged_mem(chunk, info->pages))
		return -ENOMEM;

	status = irdma_add_sd_table_entry(dev->hw, hmc_info, info->idx.sd_idx,
					  IRDMA_SD_TYPE_PAGED,
					  IRDMA_HMC_DIRECT_BP_SIZE);
	if (status)
		goto error;

	addr = chunk->vaddr;
	for (i = 0; i < info->pages; i++) {
		mem.pa = (u64)chunk->dmainfo.dmaaddrs[i];
		mem.size = 4096;
		mem.va = addr;
		pd_entry = &sd_entry->u.pd_table.pd_entry[rel_pd_idx++];
		if (!pd_entry->valid) {
			status = irdma_add_pd_table_entry(dev, hmc_info,
							  pd_idx++, &mem);
			if (status)
				goto error;

			addr += 4096;
		}
	}

	chunk->fpm_addr = pble_rsrc->next_fpm_addr;
	return 0;

error:
	irdma_pble_free_paged_mem(chunk);

	return status;
}

/**
 * irdma_get_type - add a sd entry type for sd
 * @dev: irdma_sc_dev struct
 * @idx: index of sd
 * @pages: pages in the sd
 */
static enum irdma_sd_entry_type irdma_get_type(struct irdma_sc_dev *dev,
					       struct sd_pd_idx *idx, u32 pages)
{
	enum irdma_sd_entry_type sd_entry_type;

	sd_entry_type = !idx->rel_pd_idx && pages == IRDMA_HMC_PD_CNT_IN_SD ?
			IRDMA_SD_TYPE_DIRECT : IRDMA_SD_TYPE_PAGED;
	return sd_entry_type;
}

/**
 * add_pble_prm - add a sd entry for pble resoure
 * @pble_rsrc: pble resource management
 */
static int add_pble_prm(struct irdma_hmc_pble_rsrc *pble_rsrc)
{
	struct irdma_sc_dev *dev = pble_rsrc->dev;
	struct irdma_hmc_sd_entry *sd_entry;
	struct irdma_hmc_info *hmc_info;
	struct irdma_chunk *chunk;
	struct irdma_add_page_info info;
	struct sd_pd_idx *idx = &info.idx;
	int ret_code = 0;
	enum irdma_sd_entry_type sd_entry_type;
	u64 sd_reg_val = 0;
	struct irdma_virt_mem chunkmem;
	u32 pages;

	if (pble_rsrc->unallocated_pble < PBLE_PER_PAGE)
		return -ENOMEM;

	if (pble_rsrc->next_fpm_addr & 0xfff)
		return -EINVAL;

	chunkmem.size = sizeof(*chunk);
	chunkmem.va = kzalloc(chunkmem.size, GFP_KERNEL);
	if (!chunkmem.va)
		return -ENOMEM;

	chunk = chunkmem.va;
	chunk->chunkmem = chunkmem;
	hmc_info = dev->hmc_info;
	chunk->dev = dev;
	chunk->fpm_addr = pble_rsrc->next_fpm_addr;
	get_sd_pd_idx(pble_rsrc, idx);
	sd_entry = &hmc_info->sd_table.sd_entry[idx->sd_idx];
	pages = (idx->rel_pd_idx) ? (IRDMA_HMC_PD_CNT_IN_SD - idx->rel_pd_idx) :
				    IRDMA_HMC_PD_CNT_IN_SD;
	pages = min(pages, pble_rsrc->unallocated_pble >> PBLE_512_SHIFT);
	info.chunk = chunk;
	info.hmc_info = hmc_info;
	info.pages = pages;
	info.sd_entry = sd_entry;
	if (!sd_entry->valid)
		sd_entry_type = irdma_get_type(dev, idx, pages);
	else
		sd_entry_type = sd_entry->entry_type;

	ibdev_dbg(to_ibdev(dev),
		  "PBLE: pages = %d, unallocated_pble[%d] current_fpm_addr = %llx\n",
		  pages, pble_rsrc->unallocated_pble,
		  pble_rsrc->next_fpm_addr);
	ibdev_dbg(to_ibdev(dev), "PBLE: sd_entry_type = %d\n", sd_entry_type);
	if (sd_entry_type == IRDMA_SD_TYPE_DIRECT)
		ret_code = add_sd_direct(pble_rsrc, &info);

	if (ret_code)
		sd_entry_type = IRDMA_SD_TYPE_PAGED;
	else
		pble_rsrc->stats_direct_sds++;

	if (sd_entry_type == IRDMA_SD_TYPE_PAGED) {
		ret_code = add_bp_pages(pble_rsrc, &info);
		if (ret_code)
			goto error;
		else
			pble_rsrc->stats_paged_sds++;
	}

	ret_code = irdma_prm_add_pble_mem(&pble_rsrc->pinfo, chunk);
	if (ret_code)
		goto error;

	pble_rsrc->next_fpm_addr += chunk->size;
	ibdev_dbg(to_ibdev(dev),
		  "PBLE: next_fpm_addr = %llx chunk_size[%llu] = 0x%llx\n",
		  pble_rsrc->next_fpm_addr, chunk->size, chunk->size);
	pble_rsrc->unallocated_pble -= (u32)(chunk->size >> 3);
	sd_reg_val = (sd_entry_type == IRDMA_SD_TYPE_PAGED) ?
			     sd_entry->u.pd_table.pd_page_addr.pa :
			     sd_entry->u.bp.addr.pa;

	if (!sd_entry->valid) {
		ret_code = irdma_hmc_sd_one(dev, hmc_info->hmc_fn_id, sd_reg_val,
					    idx->sd_idx, sd_entry->entry_type, true);
		if (ret_code)
			goto error;
	}

	list_add(&chunk->list, &pble_rsrc->pinfo.clist);
	sd_entry->valid = true;
	return 0;

error:
	bitmap_free(chunk->bitmapbuf);
	kfree(chunk->chunkmem.va);

	return ret_code;
}

/**
 * free_lvl2 - fee level 2 pble
 * @pble_rsrc: pble resource management
 * @palloc: level 2 pble allocation
 */
static void free_lvl2(struct irdma_hmc_pble_rsrc *pble_rsrc,
		      struct irdma_pble_alloc *palloc)
{
	u32 i;
	struct irdma_pble_level2 *lvl2 = &palloc->level2;
	struct irdma_pble_info *root = &lvl2->root;
	struct irdma_pble_info *leaf = lvl2->leaf;

	for (i = 0; i < lvl2->leaf_cnt; i++, leaf++) {
		if (leaf->addr)
			irdma_prm_return_pbles(&pble_rsrc->pinfo,
					       &leaf->chunkinfo);
		else
			break;
	}

	if (root->addr)
		irdma_prm_return_pbles(&pble_rsrc->pinfo, &root->chunkinfo);

	kfree(lvl2->leafmem.va);
	lvl2->leaf = NULL;
}

/**
 * get_lvl2_pble - get level 2 pble resource
 * @pble_rsrc: pble resource management
 * @palloc: level 2 pble allocation
 */
static int get_lvl2_pble(struct irdma_hmc_pble_rsrc *pble_rsrc,
			 struct irdma_pble_alloc *palloc)
{
	u32 lf4k, lflast, total, i;
	u32 pblcnt = PBLE_PER_PAGE;
	u64 *addr;
	struct irdma_pble_level2 *lvl2 = &palloc->level2;
	struct irdma_pble_info *root = &lvl2->root;
	struct irdma_pble_info *leaf;
	int ret_code;
	u64 fpm_addr;

	/* number of full 512 (4K) leafs) */
	lf4k = palloc->total_cnt >> 9;
	lflast = palloc->total_cnt % PBLE_PER_PAGE;
	total = (lflast == 0) ? lf4k : lf4k + 1;
	lvl2->leaf_cnt = total;

	lvl2->leafmem.size = (sizeof(*leaf) * total);
	lvl2->leafmem.va = kzalloc(lvl2->leafmem.size, GFP_KERNEL);
	if (!lvl2->leafmem.va)
		return -ENOMEM;

	lvl2->leaf = lvl2->leafmem.va;
	leaf = lvl2->leaf;
	ret_code = irdma_prm_get_pbles(&pble_rsrc->pinfo, &root->chunkinfo,
				       total << 3, &root->addr, &fpm_addr);
	if (ret_code) {
		kfree(lvl2->leafmem.va);
		lvl2->leaf = NULL;
		return -ENOMEM;
	}

	root->idx = fpm_to_idx(pble_rsrc, fpm_addr);
	root->cnt = total;
	addr = root->addr;
	for (i = 0; i < total; i++, leaf++) {
		pblcnt = (lflast && ((i + 1) == total)) ?
				lflast : PBLE_PER_PAGE;
		ret_code = irdma_prm_get_pbles(&pble_rsrc->pinfo,
					       &leaf->chunkinfo, pblcnt << 3,
					       &leaf->addr, &fpm_addr);
		if (ret_code)
			goto error;

		leaf->idx = fpm_to_idx(pble_rsrc, fpm_addr);

		leaf->cnt = pblcnt;
		*addr = (u64)leaf->idx;
		addr++;
	}

	palloc->level = PBLE_LEVEL_2;
	pble_rsrc->stats_lvl2++;
	return 0;

error:
	free_lvl2(pble_rsrc, palloc);

	return -ENOMEM;
}

/**
 * get_lvl1_pble - get level 1 pble resource
 * @pble_rsrc: pble resource management
 * @palloc: level 1 pble allocation
 */
static int get_lvl1_pble(struct irdma_hmc_pble_rsrc *pble_rsrc,
			 struct irdma_pble_alloc *palloc)
{
	int ret_code;
	u64 fpm_addr;
	struct irdma_pble_info *lvl1 = &palloc->level1;

	ret_code = irdma_prm_get_pbles(&pble_rsrc->pinfo, &lvl1->chunkinfo,
				       palloc->total_cnt << 3, &lvl1->addr,
				       &fpm_addr);
	if (ret_code)
		return -ENOMEM;

	palloc->level = PBLE_LEVEL_1;
	lvl1->idx = fpm_to_idx(pble_rsrc, fpm_addr);
	lvl1->cnt = palloc->total_cnt;
	pble_rsrc->stats_lvl1++;

	return 0;
}

/**
 * get_lvl1_lvl2_pble - calls get_lvl1 and get_lvl2 pble routine
 * @pble_rsrc: pble resources
 * @palloc: contains all inforamtion regarding pble (idx + pble addr)
 * @lvl: Bitmask for requested pble level
 */
static int get_lvl1_lvl2_pble(struct irdma_hmc_pble_rsrc *pble_rsrc,
			      struct irdma_pble_alloc *palloc, u8 lvl)
{
	int status = 0;

	status = get_lvl1_pble(pble_rsrc, palloc);
	if (!status || lvl == PBLE_LEVEL_1 || palloc->total_cnt <= PBLE_PER_PAGE)
		return status;

	status = get_lvl2_pble(pble_rsrc, palloc);

	return status;
}

/**
 * irdma_get_pble - allocate pbles from the prm
 * @pble_rsrc: pble resources
 * @palloc: contains all inforamtion regarding pble (idx + pble addr)
 * @pble_cnt: #of pbles requested
 * @lvl: requested pble level mask
 */
int irdma_get_pble(struct irdma_hmc_pble_rsrc *pble_rsrc,
		   struct irdma_pble_alloc *palloc, u32 pble_cnt,
		   u8 lvl)
{
	int status = 0;
	int max_sds = 0;
	int i;

	palloc->total_cnt = pble_cnt;
	palloc->level = PBLE_LEVEL_0;

	mutex_lock(&pble_rsrc->pble_mutex_lock);

	/*check first to see if we can get pble's without acquiring
	 * additional sd's
	 */
	status = get_lvl1_lvl2_pble(pble_rsrc, palloc, lvl);
	if (!status)
		goto exit;

	max_sds = (palloc->total_cnt >> 18) + 1;
	for (i = 0; i < max_sds; i++) {
		status = add_pble_prm(pble_rsrc);
		if (status)
			break;

		status = get_lvl1_lvl2_pble(pble_rsrc, palloc, lvl);
		/* if level1_only, only go through it once */
		if (!status || lvl)
			break;
	}

exit:
	if (!status) {
		pble_rsrc->allocdpbles += pble_cnt;
		pble_rsrc->stats_alloc_ok++;
	} else {
		pble_rsrc->stats_alloc_fail++;
	}
	mutex_unlock(&pble_rsrc->pble_mutex_lock);

	return status;
}

/**
 * irdma_free_pble - put pbles back into prm
 * @pble_rsrc: pble resources
 * @palloc: contains all information regarding pble resource being freed
 */
void irdma_free_pble(struct irdma_hmc_pble_rsrc *pble_rsrc,
		     struct irdma_pble_alloc *palloc)
{
	pble_rsrc->freedpbles += palloc->total_cnt;

	if (palloc->level == PBLE_LEVEL_2)
		free_lvl2(pble_rsrc, palloc);
	else
		irdma_prm_return_pbles(&pble_rsrc->pinfo,
				       &palloc->level1.chunkinfo);
	pble_rsrc->stats_alloc_freed++;
}