xref: /freebsd/contrib/ofed/libbnxtre/memory.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*
2  * Copyright (c) 2024, Broadcom. All rights reserved.  The term
3  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/mman.h>
31 
32 #include <malloc.h>
33 #include <string.h>
34 
35 #include "main.h"
36 
37 void bnxt_re_free_mem(struct bnxt_re_mem *mem)
38 {
39 	if (mem->va_head) {
40 		ibv_dofork_range(mem->va_head, mem->size);
41 		munmap(mem->va_head, mem->size);
42 	}
43 
44 	free(mem);
45 }
46 
47 void *bnxt_re_alloc_mem(size_t size, uint32_t pg_size)
48 {
49 	struct bnxt_re_mem *mem;
50 
51 	mem = calloc(1, sizeof(*mem));
52 	if (!mem)
53 		return NULL;
54 
55 	size = get_aligned(size, pg_size);
56 	mem->size = size;
57 	mem->va_head = mmap(NULL, size, PROT_READ | PROT_WRITE,
58 			    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
59 	if (mem->va_head == MAP_FAILED)
60 		goto bail;
61 
62 	if (ibv_dontfork_range(mem->va_head, size))
63 		goto unmap;
64 
65 	mem->head = 0;
66 	mem->tail = 0;
67 	mem->va_tail = (void *)((char *)mem->va_head + size);
68 	return mem;
69 unmap:
70 	munmap(mem->va_head, size);
71 bail:
72 	free(mem);
73 	return NULL;
74 }
75 
76 void *bnxt_re_get_obj(struct bnxt_re_mem *mem, size_t req)
77 {
78 	void *va;
79 
80 	if ((mem->size - mem->tail - req) < mem->head)
81 		return NULL;
82 	mem->tail += req;
83 	va = (void *)((char *)mem->va_tail - mem->tail);
84 	return va;
85 }
86 
87 void *bnxt_re_get_ring(struct bnxt_re_mem *mem, size_t req)
88 {
89 	void *va;
90 
91 	if ((mem->head + req) > (mem->size - mem->tail))
92 		return NULL;
93 	va = (void *)((char *)mem->va_head + mem->head);
94 	mem->head += req;
95 	return va;
96 }
97