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
__add_page(struct mlx5_context * context)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
57 pp = ps / context->cache_line_size;
58 nlong = (pp + 8 * sizeof(long) - 1) / (8 * sizeof(long));
59
60 page = malloc(sizeof *page + nlong * sizeof(long));
61 if (!page)
62 return NULL;
63
64 if (mlx5_alloc_buf(&page->buf, ps, ps)) {
65 free(page);
66 return NULL;
67 }
68
69 page->num_db = pp;
70 page->use_cnt = 0;
71 for (i = 0; i < nlong; ++i)
72 page->free[i] = ~0;
73
74 page->prev = NULL;
75 page->next = context->db_list;
76 context->db_list = page;
77 if (page->next)
78 page->next->prev = page;
79
80 return page;
81 }
82
mlx5_alloc_dbrec(struct mlx5_context * context)83 uint32_t *mlx5_alloc_dbrec(struct mlx5_context *context)
84 {
85 struct mlx5_db_page *page;
86 uint32_t *db = NULL;
87 int i, j;
88
89 pthread_mutex_lock(&context->db_list_mutex);
90
91 for (page = context->db_list; page; page = page->next)
92 if (page->use_cnt < page->num_db)
93 goto found;
94
95 page = __add_page(context);
96 if (!page)
97 goto out;
98
99 found:
100 ++page->use_cnt;
101
102 for (i = 0; !page->free[i]; ++i)
103 /* nothing */;
104
105 j = ffsl(page->free[i]);
106 --j;
107 page->free[i] &= ~(1UL << j);
108 db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size;
109
110 out:
111 pthread_mutex_unlock(&context->db_list_mutex);
112
113 return db;
114 }
115
mlx5_free_db(struct mlx5_context * context,uint32_t * db)116 void mlx5_free_db(struct mlx5_context *context, uint32_t *db)
117 {
118 struct mlx5_db_page *page;
119 uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
120 int i;
121
122 pthread_mutex_lock(&context->db_list_mutex);
123
124 for (page = context->db_list; page; page = page->next)
125 if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
126 break;
127
128 if (!page)
129 goto out;
130
131 i = ((void *) db - page->buf.buf) / context->cache_line_size;
132 page->free[i / (8 * sizeof(long))] |= 1UL << (i % (8 * sizeof(long)));
133
134 if (!--page->use_cnt) {
135 if (page->prev)
136 page->prev->next = page->next;
137 else
138 context->db_list = page->next;
139 if (page->next)
140 page->next->prev = page->prev;
141
142 mlx5_free_buf(&page->buf);
143 free(page);
144 }
145
146 out:
147 pthread_mutex_unlock(&context->db_list_mutex);
148 }
149