xref: /freebsd/contrib/ofed/libmlx5/dbrec.c (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
1 /*
2  * Copyright (c) 2012 Mellanox Technologies, Inc.  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 #define _GNU_SOURCE
33 #include <config.h>
34 
35 #include <stdlib.h>
36 #include <pthread.h>
37 #include <string.h>
38 
39 #include "mlx5.h"
40 
41 struct mlx5_db_page {
42 	struct mlx5_db_page	       *prev, *next;
43 	struct mlx5_buf			buf;
44 	int				num_db;
45 	int				use_cnt;
46 	unsigned long			free[0];
47 };
48 
49 static struct mlx5_db_page *__add_page(struct mlx5_context *context)
50 {
51 	struct mlx5_db_page *page;
52 	int ps = to_mdev(context->ibv_ctx.device)->page_size;
53 	int pp;
54 	int i;
55 	int nlong;
56 	int ret;
57 
58 	pp = ps / context->cache_line_size;
59 	nlong = (pp + 8 * sizeof(long) - 1) / (8 * sizeof(long));
60 
61 	page = malloc(sizeof *page + nlong * sizeof(long));
62 	if (!page)
63 		return NULL;
64 
65 	if (mlx5_is_extern_alloc(context))
66 		ret = mlx5_alloc_buf_extern(context, &page->buf, ps);
67 	else
68 		ret = mlx5_alloc_buf(&page->buf, ps, ps);
69 	if (ret) {
70 		free(page);
71 		return NULL;
72 	}
73 
74 	page->num_db  = pp;
75 	page->use_cnt = 0;
76 	for (i = 0; i < nlong; ++i)
77 		page->free[i] = ~0;
78 
79 	page->prev = NULL;
80 	page->next = context->db_list;
81 	context->db_list = page;
82 	if (page->next)
83 		page->next->prev = page;
84 
85 	return page;
86 }
87 
88 __be32 *mlx5_alloc_dbrec(struct mlx5_context *context)
89 {
90 	struct mlx5_db_page *page;
91 	__be32 *db = NULL;
92 	int i, j;
93 
94 	pthread_mutex_lock(&context->db_list_mutex);
95 
96 	for (page = context->db_list; page; page = page->next)
97 		if (page->use_cnt < page->num_db)
98 			goto found;
99 
100 	page = __add_page(context);
101 	if (!page)
102 		goto out;
103 
104 found:
105 	++page->use_cnt;
106 
107 	for (i = 0; !page->free[i]; ++i)
108 		/* nothing */;
109 
110 	j = ffsl(page->free[i]);
111 	--j;
112 	page->free[i] &= ~(1UL << j);
113 	db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size;
114 
115 out:
116 	pthread_mutex_unlock(&context->db_list_mutex);
117 
118 	return db;
119 }
120 
121 void mlx5_free_db(struct mlx5_context *context, __be32 *db)
122 {
123 	struct mlx5_db_page *page;
124 	uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
125 	int i;
126 
127 	pthread_mutex_lock(&context->db_list_mutex);
128 
129 	for (page = context->db_list; page; page = page->next) {
130 		uintptr_t base = (uintptr_t) page->buf.buf;
131 
132 		if ((uintptr_t) db >= base && (uintptr_t) db - base < ps)
133 			break;
134 	}
135 
136 	if (!page)
137 		goto out;
138 
139 	i = ((void *) db - page->buf.buf) / context->cache_line_size;
140 	page->free[i / (8 * sizeof(long))] |= 1UL << (i % (8 * sizeof(long)));
141 
142 	if (!--page->use_cnt) {
143 		if (page->prev)
144 			page->prev->next = page->next;
145 		else
146 			context->db_list = page->next;
147 		if (page->next)
148 			page->next->prev = page->prev;
149 
150 		if (page->buf.type == MLX5_ALLOC_TYPE_EXTERNAL)
151 			mlx5_free_buf_extern(context, &page->buf);
152 		else
153 			mlx5_free_buf(&page->buf);
154 
155 		free(page);
156 	}
157 
158 out:
159 	pthread_mutex_unlock(&context->db_list_mutex);
160 }
161