summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/linux/kmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common/linux/kmem.c')
-rw-r--r--drivers/gpu/nvgpu/common/linux/kmem.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/linux/kmem.c b/drivers/gpu/nvgpu/common/linux/kmem.c
new file mode 100644
index 00000000..24e0ca5d
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/kmem.c
@@ -0,0 +1,83 @@
1/*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/atomic.h>
20
21#include <nvgpu/kmem.h>
22
23/*
24 * Statically declared because this needs to be shared across all nvgpu driver
25 * instances. This makes sure that all kmem caches are _definitely_ uniquely
26 * named.
27 */
28static atomic_t kmem_cache_id;
29
30/*
31 * Linux specific version of the nvgpu_kmem_cache struct. This type is
32 * completely opaque to the rest of the driver.
33 */
34struct nvgpu_kmem_cache {
35 struct gk20a *g;
36 struct kmem_cache *cache;
37
38 /*
39 * Memory to hold the kmem_cache unique name. Only necessary on our
40 * k3.10 kernel when not using the SLUB allocator but it's easier to
41 * just carry this on to newer kernels.
42 */
43 char name[128];
44};
45
46struct nvgpu_kmem_cache *nvgpu_kmem_cache_create(struct gk20a *g, size_t size)
47{
48 struct nvgpu_kmem_cache *cache =
49 kzalloc(sizeof(struct nvgpu_kmem_cache), GFP_KERNEL);
50
51 if (!cache)
52 return NULL;
53
54 cache->g = g;
55
56 snprintf(cache->name, sizeof(cache->name),
57 "nvgpu-cache-0x%p-%d-%d", g, (int)size,
58 atomic_inc_return(&kmem_cache_id));
59 cache->cache = kmem_cache_create(cache->name,
60 size, size, 0, NULL);
61 if (!cache->cache) {
62 kfree(cache);
63 return NULL;
64 }
65
66 return cache;
67}
68
69void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache)
70{
71 kmem_cache_destroy(cache->cache);
72 kfree(cache);
73}
74
75void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache)
76{
77 return kmem_cache_alloc(cache->cache, GFP_KERNEL);
78}
79
80void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr)
81{
82 kmem_cache_free(cache->cache, ptr);
83}