summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c')
-rw-r--r--drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c212
1 files changed, 212 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c b/drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c
new file mode 100644
index 00000000..ebd779c0
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/mm/nvgpu_allocator.c
@@ -0,0 +1,212 @@
1/*
2 * gk20a allocator
3 *
4 * Copyright (c) 2011-2016, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/kernel.h>
20#include <linux/slab.h>
21
22#include <nvgpu/allocator.h>
23
24#include "gk20a/gk20a.h"
25#include "gk20a/mm_gk20a.h"
26#include "gk20a/platform_gk20a.h"
27
28u32 nvgpu_alloc_tracing_on;
29
30u64 nvgpu_alloc_length(struct nvgpu_allocator *a)
31{
32 if (a->ops->length)
33 return a->ops->length(a);
34
35 return 0;
36}
37
38u64 nvgpu_alloc_base(struct nvgpu_allocator *a)
39{
40 if (a->ops->base)
41 return a->ops->base(a);
42
43 return 0;
44}
45
46u64 nvgpu_alloc_initialized(struct nvgpu_allocator *a)
47{
48 if (!a->ops || !a->ops->inited)
49 return 0;
50
51 return a->ops->inited(a);
52}
53
54u64 nvgpu_alloc_end(struct nvgpu_allocator *a)
55{
56 if (a->ops->end)
57 return a->ops->end(a);
58
59 return 0;
60}
61
62u64 nvgpu_alloc_space(struct nvgpu_allocator *a)
63{
64 if (a->ops->space)
65 return a->ops->space(a);
66
67 return 0;
68}
69
70u64 nvgpu_alloc(struct nvgpu_allocator *a, u64 len)
71{
72 return a->ops->alloc(a, len);
73}
74
75void nvgpu_free(struct nvgpu_allocator *a, u64 addr)
76{
77 a->ops->free(a, addr);
78}
79
80u64 nvgpu_alloc_fixed(struct nvgpu_allocator *a, u64 base, u64 len)
81{
82 if (a->ops->alloc_fixed)
83 return a->ops->alloc_fixed(a, base, len);
84
85 return 0;
86}
87
88void nvgpu_free_fixed(struct nvgpu_allocator *a, u64 base, u64 len)
89{
90 /*
91 * If this operation is not defined for the allocator then just do
92 * nothing. The alternative would be to fall back on the regular
93 * free but that may be harmful in unexpected ways.
94 */
95 if (a->ops->free_fixed)
96 a->ops->free_fixed(a, base, len);
97}
98
99int nvgpu_alloc_reserve_carveout(struct nvgpu_allocator *a,
100 struct nvgpu_alloc_carveout *co)
101{
102 if (a->ops->reserve_carveout)
103 return a->ops->reserve_carveout(a, co);
104
105 return -ENODEV;
106}
107
108void nvgpu_alloc_release_carveout(struct nvgpu_allocator *a,
109 struct nvgpu_alloc_carveout *co)
110{
111 if (a->ops->release_carveout)
112 a->ops->release_carveout(a, co);
113}
114
115void nvgpu_alloc_destroy(struct nvgpu_allocator *a)
116{
117 a->ops->fini(a);
118 memset(a, 0, sizeof(*a));
119}
120
121/*
122 * Handle the common init stuff for a nvgpu_allocator.
123 */
124int __nvgpu_alloc_common_init(struct nvgpu_allocator *a,
125 const char *name, void *priv, bool dbg,
126 const struct nvgpu_allocator_ops *ops)
127{
128 if (!ops)
129 return -EINVAL;
130
131 /*
132 * This is the bare minimum operations required for a sensible
133 * allocator.
134 */
135 if (!ops->alloc || !ops->free || !ops->fini)
136 return -EINVAL;
137
138 a->ops = ops;
139 a->priv = priv;
140 a->debug = dbg;
141
142 mutex_init(&a->lock);
143
144 strlcpy(a->name, name, sizeof(a->name));
145
146 return 0;
147}
148
149void nvgpu_alloc_print_stats(struct nvgpu_allocator *__a,
150 struct seq_file *s, int lock)
151{
152 __a->ops->print_stats(__a, s, lock);
153}
154
155#ifdef CONFIG_DEBUG_FS
156static int __alloc_show(struct seq_file *s, void *unused)
157{
158 struct nvgpu_allocator *a = s->private;
159
160 nvgpu_alloc_print_stats(a, s, 1);
161
162 return 0;
163}
164
165static int __alloc_open(struct inode *inode, struct file *file)
166{
167 return single_open(file, __alloc_show, inode->i_private);
168}
169
170static const struct file_operations __alloc_fops = {
171 .open = __alloc_open,
172 .read = seq_read,
173 .llseek = seq_lseek,
174 .release = single_release,
175};
176#endif
177
178void nvgpu_init_alloc_debug(struct gk20a *g, struct nvgpu_allocator *a)
179{
180#ifdef CONFIG_DEBUG_FS
181 if (!g->debugfs_allocators)
182 return;
183
184 a->debugfs_entry = debugfs_create_file(a->name, S_IRUGO,
185 g->debugfs_allocators,
186 a, &__alloc_fops);
187#endif
188}
189
190void nvgpu_fini_alloc_debug(struct nvgpu_allocator *a)
191{
192#ifdef CONFIG_DEBUG_FS
193 if (!IS_ERR_OR_NULL(a->debugfs_entry))
194 debugfs_remove(a->debugfs_entry);
195#endif
196}
197
198void nvgpu_alloc_debugfs_init(struct device *dev)
199{
200#ifdef CONFIG_DEBUG_FS
201 struct gk20a_platform *platform = dev_get_drvdata(dev);
202 struct dentry *gpu_root = platform->debugfs;
203 struct gk20a *g = get_gk20a(dev);
204
205 g->debugfs_allocators = debugfs_create_dir("allocators", gpu_root);
206 if (IS_ERR_OR_NULL(g->debugfs_allocators))
207 return;
208
209 debugfs_create_u32("tracing", 0664, g->debugfs_allocators,
210 &nvgpu_alloc_tracing_on);
211#endif
212}