summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/gk20a/gk20a_allocator_lockless.c
blob: 5b011d8c38f3d86da84911cac77d7e2a0947774f (plain) (blame)
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
/*
 * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/atomic.h>

#include "gk20a_allocator.h"
#include "lockless_allocator_priv.h"

static u64 gk20a_lockless_alloc_length(struct gk20a_allocator *a)
{
	struct gk20a_lockless_allocator *pa = a->priv;

	return pa->length;
}

static u64 gk20a_lockless_alloc_base(struct gk20a_allocator *a)
{
	struct gk20a_lockless_allocator *pa = a->priv;

	return pa->base;
}

static int gk20a_lockless_alloc_inited(struct gk20a_allocator *a)
{
	struct gk20a_lockless_allocator *pa = a->priv;
	int inited = pa->inited;

	rmb();
	return inited;
}

static u64 gk20a_lockless_alloc_end(struct gk20a_allocator *a)
{
	struct gk20a_lockless_allocator *pa = a->priv;

	return pa->base + pa->length;
}

static u64 gk20a_lockless_alloc(struct gk20a_allocator *a, u64 len)
{
	struct gk20a_lockless_allocator *pa = a->priv;
	int head, new_head, ret;
	u64 addr = 0;

	if (len != pa->blk_size)
		return 0;

	head = ACCESS_ONCE(pa->head);
	while (head >= 0) {
		new_head = ACCESS_ONCE(pa->next[head]);
		ret = cmpxchg(&pa->head, head, new_head);
		if (ret == head) {
			addr = pa->base + head * pa->blk_size;
			atomic_inc(&pa->nr_allocs);
			alloc_dbg(a, "Alloc node # %d @ addr 0x%llx\n", head,
				  addr);
			break;
		}
		head = ACCESS_ONCE(pa->head);
	}
	return addr;
}

static void gk20a_lockless_free(struct gk20a_allocator *a, u64 addr)
{
	struct gk20a_lockless_allocator *pa = a->priv;
	int head, ret;
	u64 cur_idx, rem;

	cur_idx = addr - pa->base;
	rem = do_div(cur_idx, pa->blk_size);

	while (1) {
		head = ACCESS_ONCE(pa->head);
		ACCESS_ONCE(pa->next[cur_idx]) = head;
		ret = cmpxchg(&pa->head, head, cur_idx);
		if (ret == head) {
			atomic_dec(&pa->nr_allocs);
			alloc_dbg(a, "Free node # %llu\n", cur_idx);
			break;
		}
	}
}

static void gk20a_lockless_alloc_destroy(struct gk20a_allocator *a)
{
	struct gk20a_lockless_allocator *pa = a->priv;

	gk20a_fini_alloc_debug(a);

	vfree(pa->next);
	kfree(pa);
}

static void gk20a_lockless_print_stats(struct gk20a_allocator *a,
				   struct seq_file *s, int lock)
{
	struct gk20a_lockless_allocator *pa = a->priv;

	__alloc_pstat(s, a, "Lockless allocator params:\n");
	__alloc_pstat(s, a, "  start = 0x%llx\n", pa->base);
	__alloc_pstat(s, a, "  end   = 0x%llx\n", pa->base + pa->length);

	/* Actual stats. */
	__alloc_pstat(s, a, "Stats:\n");
	__alloc_pstat(s, a, "  Number allocs = %d\n",
		      atomic_read(&pa->nr_allocs));
	__alloc_pstat(s, a, "  Number free   = %d\n",
		      pa->nr_nodes - atomic_read(&pa->nr_allocs));
}

static const struct gk20a_allocator_ops pool_ops = {
	.alloc		= gk20a_lockless_alloc,
	.free		= gk20a_lockless_free,

	.base		= gk20a_lockless_alloc_base,
	.length		= gk20a_lockless_alloc_length,
	.end		= gk20a_lockless_alloc_end,
	.inited		= gk20a_lockless_alloc_inited,

	.fini		= gk20a_lockless_alloc_destroy,

	.print_stats	= gk20a_lockless_print_stats,
};

int gk20a_lockless_allocator_init(struct gk20a *g, struct gk20a_allocator *__a,
			      const char *name, u64 base, u64 length,
			      u64 blk_size, u64 flags)
{
	int i;
	int err;
	int nr_nodes;
	u64 count, rem;
	struct gk20a_lockless_allocator *a;

	if (!blk_size)
		return -EINVAL;

	/*
	 * Ensure we have space for atleast one node & there's no overflow.
	 * In order to control memory footprint, we require count < INT_MAX
	 */
	count = length;
	rem = do_div(count, blk_size);
	if (!base || !count || count > INT_MAX)
		return -EINVAL;

	a = kzalloc(sizeof(struct gk20a_lockless_allocator), GFP_KERNEL);
	if (!a)
		return -ENOMEM;

	err = __gk20a_alloc_common_init(__a, name, a, false, &pool_ops);
	if (err)
		goto fail;

	a->next = vzalloc(sizeof(*a->next) * count);
	if (!a->next) {
		err = -ENOMEM;
		goto fail;
	}

	/* chain the elements together to form the initial free list  */
	nr_nodes = (int)count;
	for (i = 0; i < nr_nodes; i++)
		a->next[i] = i + 1;
	a->next[nr_nodes - 1] = -1;

	a->base = base;
	a->length = length;
	a->blk_size = blk_size;
	a->nr_nodes = nr_nodes;
	a->flags = flags;
	atomic_set(&a->nr_allocs, 0);

	wmb();
	a->inited = true;

	gk20a_init_alloc_debug(g, __a);
	alloc_dbg(__a, "New allocator: type          lockless\n");
	alloc_dbg(__a, "               base          0x%llx\n", a->base);
	alloc_dbg(__a, "               nodes         %d\n", a->nr_nodes);
	alloc_dbg(__a, "               blk_size      0x%llx\n", a->blk_size);
	alloc_dbg(__a, "               flags         0x%llx\n", a->flags);

	return 0;

fail:
	kfree(a);
	return err;
}