summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/as.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common/as.c')
-rw-r--r--drivers/gpu/nvgpu/common/as.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/as.c b/drivers/gpu/nvgpu/common/as.c
new file mode 100644
index 00000000..7ca754e1
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/as.c
@@ -0,0 +1,165 @@
1/*
2 * GK20A Address Spaces
3 *
4 * Copyright (c) 2011-2017, NVIDIA CORPORATION. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <trace/events/gk20a.h>
26#include <uapi/linux/nvgpu.h>
27
28#include <nvgpu/kmem.h>
29#include <nvgpu/vm.h>
30
31#include "gk20a/gk20a.h"
32
33/* dumb allocator... */
34static int generate_as_share_id(struct gk20a_as *as)
35{
36 gk20a_dbg_fn("");
37 return ++as->last_share_id;
38}
39/* still dumb */
40static void release_as_share_id(struct gk20a_as *as, int id)
41{
42 gk20a_dbg_fn("");
43 return;
44}
45
46/* address space interfaces for the gk20a module */
47static int gk20a_vm_alloc_share(struct gk20a_as_share *as_share,
48 u32 big_page_size, u32 flags)
49{
50 struct gk20a_as *as = as_share->as;
51 struct gk20a *g = gk20a_from_as(as);
52 struct mm_gk20a *mm = &g->mm;
53 struct vm_gk20a *vm;
54 char name[32];
55 const bool userspace_managed =
56 (flags & NVGPU_GPU_IOCTL_ALLOC_AS_FLAGS_USERSPACE_MANAGED) != 0;
57
58 gk20a_dbg_fn("");
59
60 if (big_page_size == 0) {
61 big_page_size = g->ops.mm.get_default_big_page_size();
62 } else {
63 if (!is_power_of_2(big_page_size))
64 return -EINVAL;
65
66 if (!(big_page_size & nvgpu_mm_get_available_big_page_sizes(g)))
67 return -EINVAL;
68 }
69
70 snprintf(name, sizeof(name), "as_%d", as_share->id);
71
72 vm = nvgpu_vm_init(g, big_page_size,
73 big_page_size << 10,
74 mm->channel.kernel_size,
75 mm->channel.user_size + mm->channel.kernel_size,
76 !mm->disable_bigpage, userspace_managed, name);
77 if (!vm)
78 return -ENOMEM;
79
80 as_share->vm = vm;
81 vm->as_share = as_share;
82 vm->enable_ctag = true;
83
84 return 0;
85}
86
87int gk20a_as_alloc_share(struct gk20a *g,
88 u32 big_page_size, u32 flags,
89 struct gk20a_as_share **out)
90{
91 struct gk20a_as_share *as_share;
92 int err = 0;
93
94 gk20a_dbg_fn("");
95 g = gk20a_get(g);
96 if (!g)
97 return -ENODEV;
98
99 *out = NULL;
100 as_share = nvgpu_kzalloc(g, sizeof(*as_share));
101 if (!as_share)
102 return -ENOMEM;
103
104 as_share->as = &g->as;
105 as_share->id = generate_as_share_id(as_share->as);
106
107 /* this will set as_share->vm. */
108 err = gk20a_busy(g);
109 if (err)
110 goto failed;
111 err = gk20a_vm_alloc_share(as_share, big_page_size, flags);
112 gk20a_idle(g);
113
114 if (err)
115 goto failed;
116
117 *out = as_share;
118 return 0;
119
120failed:
121 nvgpu_kfree(g, as_share);
122 return err;
123}
124
125int gk20a_vm_release_share(struct gk20a_as_share *as_share)
126{
127 struct vm_gk20a *vm = as_share->vm;
128
129 gk20a_dbg_fn("");
130
131 vm->as_share = NULL;
132 as_share->vm = NULL;
133
134 nvgpu_vm_put(vm);
135
136 return 0;
137}
138
139/*
140 * channels and the device nodes call this to release.
141 * once the ref_cnt hits zero the share is deleted.
142 */
143int gk20a_as_release_share(struct gk20a_as_share *as_share)
144{
145 struct gk20a *g = as_share->vm->mm->g;
146 int err;
147
148 gk20a_dbg_fn("");
149
150 err = gk20a_busy(g);
151
152 if (err)
153 goto release_fail;
154
155 err = gk20a_vm_release_share(as_share);
156
157 gk20a_idle(g);
158
159release_fail:
160 release_as_share_id(as_share->as, as_share->id);
161 gk20a_put(g);
162 nvgpu_kfree(g, as_share);
163
164 return err;
165}