diff options
author | Alex Waterman <alexw@nvidia.com> | 2018-01-22 19:30:53 -0500 |
---|---|---|
committer | mobile promotions <svcmobile_promotions@nvidia.com> | 2018-05-07 07:41:26 -0400 |
commit | 6e739d924fe9b778fa82396e0e941143f498acb8 (patch) | |
tree | f112ede8573c2f8bf76e0bdaadf33c6c71343820 /drivers/gpu/nvgpu/common | |
parent | e6b3bb4e6b3d4013f83ba6d31c780947f16cf410 (diff) |
gpu: nvgpu: Userspace POSIX support
Add support for compiling nvgpu in a POSIX compliant userspace.
This code adds all of the necessary abstraction interfaces (mostly
stubbed) to enabled extremely limited and basic functionality in
nvgpu.
The goal of this code is to facilitate unit testing of the nvgpu
common core. By doing this in userspace it is much easier to write
tests that rely on very particular states within nvgpu since a user
can very precisely control the state of nvgpu.
JIRA NVGPU-525
Change-Id: I30e95016df14997d951075777e0585f912dc5960
Signed-off-by: Alex Waterman <alexw@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1683914
GVS: Gerrit_Virtual_Submit
Reviewed-by: Terje Bergstrom <tbergstrom@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
Diffstat (limited to 'drivers/gpu/nvgpu/common')
25 files changed, 2311 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/common/posix/bitmap.c b/drivers/gpu/nvgpu/common/posix/bitmap.c new file mode 100644 index 00000000..51361777 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/bitmap.c | |||
@@ -0,0 +1,268 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | |||
26 | #include <nvgpu/posix/bitops.h> | ||
27 | #include <nvgpu/posix/atomic.h> | ||
28 | |||
29 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) | ||
30 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) | ||
31 | |||
32 | unsigned long __nvgpu_posix_fls(unsigned long word) | ||
33 | { | ||
34 | int num = BITS_PER_LONG - 1; | ||
35 | |||
36 | #if BITS_PER_LONG == 64 | ||
37 | if (!(word & (~0ul << 32))) { | ||
38 | num -= 32; | ||
39 | word <<= 32; | ||
40 | } | ||
41 | #endif | ||
42 | if (!(word & (~0ul << (BITS_PER_LONG-16)))) { | ||
43 | num -= 16; | ||
44 | word <<= 16; | ||
45 | } | ||
46 | if (!(word & (~0ul << (BITS_PER_LONG-8)))) { | ||
47 | num -= 8; | ||
48 | word <<= 8; | ||
49 | } | ||
50 | if (!(word & (~0ul << (BITS_PER_LONG-4)))) { | ||
51 | num -= 4; | ||
52 | word <<= 4; | ||
53 | } | ||
54 | if (!(word & (~0ul << (BITS_PER_LONG-2)))) { | ||
55 | num -= 2; | ||
56 | word <<= 2; | ||
57 | } | ||
58 | if (!(word & (~0ul << (BITS_PER_LONG-1)))) | ||
59 | num -= 1; | ||
60 | return num; | ||
61 | } | ||
62 | |||
63 | unsigned long __nvgpu_posix_ffs(unsigned long word) | ||
64 | { | ||
65 | int num = 0; | ||
66 | |||
67 | #if BITS_PER_LONG == 64 | ||
68 | if ((word & 0xffffffff) == 0) { | ||
69 | num += 32; | ||
70 | word >>= 32; | ||
71 | } | ||
72 | #endif | ||
73 | if ((word & 0xffff) == 0) { | ||
74 | num += 16; | ||
75 | word >>= 16; | ||
76 | } | ||
77 | if ((word & 0xff) == 0) { | ||
78 | num += 8; | ||
79 | word >>= 8; | ||
80 | } | ||
81 | if ((word & 0xf) == 0) { | ||
82 | num += 4; | ||
83 | word >>= 4; | ||
84 | } | ||
85 | if ((word & 0x3) == 0) { | ||
86 | num += 2; | ||
87 | word >>= 2; | ||
88 | } | ||
89 | if ((word & 0x1) == 0) | ||
90 | num += 1; | ||
91 | |||
92 | return num; | ||
93 | } | ||
94 | |||
95 | static unsigned long __find_next_bit(const unsigned long *addr, | ||
96 | unsigned long n, | ||
97 | unsigned long start, | ||
98 | bool invert) | ||
99 | { | ||
100 | unsigned long idx; | ||
101 | unsigned long w; | ||
102 | unsigned long start_mask; | ||
103 | |||
104 | /* | ||
105 | * We make a mask we can XOR into the word so that we can invert the | ||
106 | * word without requiring a branch. I.e instead of doing: | ||
107 | * | ||
108 | * w = invert ? ~addr[idx] : addr[idx] | ||
109 | * | ||
110 | * We can do: | ||
111 | * | ||
112 | * w = addr[idx] ^= invert_mask | ||
113 | * | ||
114 | * This saves us a branch every iteration through the loop. Now we can | ||
115 | * always just look for 1s. | ||
116 | */ | ||
117 | unsigned long invert_mask = invert ? ~0UL : 0UL; | ||
118 | |||
119 | if (start >= n) | ||
120 | return n; | ||
121 | |||
122 | start_mask = ~0UL << (start & (BITS_PER_LONG - 1)); | ||
123 | |||
124 | idx = start / BITS_PER_LONG; | ||
125 | w = (addr[idx] ^ invert_mask) & start_mask; | ||
126 | |||
127 | start = round_up(start, BITS_PER_LONG); | ||
128 | |||
129 | /* | ||
130 | * Find the first non-zero word taking into account start and | ||
131 | * invert. | ||
132 | */ | ||
133 | while (!w) { | ||
134 | idx++; | ||
135 | start += BITS_PER_LONG; | ||
136 | |||
137 | w = addr[idx] ^ invert_mask; | ||
138 | } | ||
139 | |||
140 | return min(n, ffs(w) + idx * BITS_PER_LONG); | ||
141 | } | ||
142 | |||
143 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) | ||
144 | { | ||
145 | return __find_next_bit(addr, size, 0, false); | ||
146 | } | ||
147 | |||
148 | unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) | ||
149 | { | ||
150 | return __find_next_bit(addr, size, 0, true); | ||
151 | } | ||
152 | |||
153 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, | ||
154 | unsigned long offset) | ||
155 | { | ||
156 | return __find_next_bit(addr, size, offset, false); | ||
157 | } | ||
158 | |||
159 | static unsigned long find_next_zero_bit(const unsigned long *addr, | ||
160 | unsigned long size, | ||
161 | unsigned long offset) | ||
162 | { | ||
163 | return __find_next_bit(addr, size, offset, true); | ||
164 | } | ||
165 | |||
166 | void bitmap_set(unsigned long *map, unsigned int start, int len) | ||
167 | { | ||
168 | unsigned int end = start + len; | ||
169 | |||
170 | /* | ||
171 | * Super slow naive implementation. But speed isn't what matters here. | ||
172 | */ | ||
173 | while (start < end) | ||
174 | set_bit(start++, map); | ||
175 | } | ||
176 | |||
177 | void bitmap_clear(unsigned long *map, unsigned int start, int len) | ||
178 | { | ||
179 | unsigned int end = start + len; | ||
180 | |||
181 | while (start < end) | ||
182 | clear_bit(start++, map); | ||
183 | } | ||
184 | |||
185 | /* | ||
186 | * This is essentially a find-first-fit allocator: this searches a bitmap for | ||
187 | * the first space that is large enough to satisfy the requested size of bits. | ||
188 | * That means that this is not a vary smart allocator. But it is fast relative | ||
189 | * to an allocator that goes looking for an optimal location. | ||
190 | */ | ||
191 | unsigned long bitmap_find_next_zero_area_off(unsigned long *map, | ||
192 | unsigned long size, | ||
193 | unsigned long start, | ||
194 | unsigned int nr, | ||
195 | unsigned long align_mask, | ||
196 | unsigned long align_offset) | ||
197 | { | ||
198 | unsigned long offs; | ||
199 | |||
200 | while (start + nr <= size) { | ||
201 | start = find_next_zero_bit(map, size, start); | ||
202 | |||
203 | start = ALIGN_MASK(start + align_offset, align_mask) - | ||
204 | align_offset; | ||
205 | |||
206 | /* | ||
207 | * Not enough space left to satisfy the requested area. | ||
208 | */ | ||
209 | if ((start + nr) > size) | ||
210 | return size; | ||
211 | |||
212 | offs = find_next_bit(map, size, start); | ||
213 | |||
214 | if ((offs - start) >= nr) | ||
215 | return start; | ||
216 | |||
217 | start = offs + 1; | ||
218 | } | ||
219 | |||
220 | return size; | ||
221 | } | ||
222 | |||
223 | unsigned long bitmap_find_next_zero_area(unsigned long *map, | ||
224 | unsigned long size, | ||
225 | unsigned long start, | ||
226 | unsigned int nr, | ||
227 | unsigned long align_mask) | ||
228 | { | ||
229 | return bitmap_find_next_zero_area_off(map, size, start, nr, | ||
230 | align_mask, 0); | ||
231 | } | ||
232 | |||
233 | bool test_bit(int nr, const volatile unsigned long *addr) | ||
234 | { | ||
235 | return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); | ||
236 | } | ||
237 | |||
238 | bool test_and_set_bit(int nr, volatile unsigned long *addr) | ||
239 | { | ||
240 | unsigned long mask = BIT_MASK(nr); | ||
241 | volatile unsigned long *p = addr + BIT_WORD(nr); | ||
242 | |||
243 | return !!(__sync_fetch_and_or(p, mask) & mask); | ||
244 | } | ||
245 | |||
246 | bool test_and_clear_bit(int nr, volatile unsigned long *addr) | ||
247 | { | ||
248 | unsigned long mask = BIT_MASK(nr); | ||
249 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | ||
250 | |||
251 | return !!(__sync_fetch_and_and(p, ~mask) & mask); | ||
252 | } | ||
253 | |||
254 | void set_bit(int nr, volatile unsigned long *addr) | ||
255 | { | ||
256 | unsigned long mask = BIT_MASK(nr); | ||
257 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | ||
258 | |||
259 | __atomic_or(p, mask); | ||
260 | } | ||
261 | |||
262 | void clear_bit(int nr, volatile unsigned long *addr) | ||
263 | { | ||
264 | unsigned long mask = BIT_MASK(nr); | ||
265 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | ||
266 | |||
267 | __atomic_and(p, ~mask); | ||
268 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/bug.c b/drivers/gpu/nvgpu/common/posix/bug.c new file mode 100644 index 00000000..64f4a6f6 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/bug.c | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/log.h> | ||
24 | |||
25 | #include <nvgpu/posix/bug.h> | ||
26 | |||
27 | __attribute__ ((noreturn)) | ||
28 | static void __hang(void) | ||
29 | { | ||
30 | nvgpu_err(NULL, "Hanging!"); | ||
31 | |||
32 | while (1) | ||
33 | ; | ||
34 | } | ||
35 | |||
36 | static void __dump_stack(unsigned int skip_frames) | ||
37 | { | ||
38 | return; | ||
39 | } | ||
40 | |||
41 | void dump_stack(void) | ||
42 | { | ||
43 | __dump_stack(0); | ||
44 | } | ||
45 | |||
46 | /* | ||
47 | * Ahhh! A bug! | ||
48 | */ | ||
49 | void __bug(const char *fmt, ...) | ||
50 | { | ||
51 | nvgpu_err(NULL, "BUG detected!"); | ||
52 | |||
53 | __hang(); | ||
54 | } | ||
55 | |||
56 | bool __warn(bool cond, const char *fmt, ...) | ||
57 | { | ||
58 | if (!cond) | ||
59 | goto done; | ||
60 | |||
61 | nvgpu_warn(NULL, "WARNING detected!"); | ||
62 | |||
63 | dump_stack(); | ||
64 | |||
65 | done: | ||
66 | return cond; | ||
67 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/channel.c b/drivers/gpu/nvgpu/common/posix/channel.c new file mode 100644 index 00000000..05697159 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/channel.c | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include "gk20a/channel_gk20a.h" | ||
24 | |||
25 | u32 nvgpu_get_gpfifo_entry_size(void) | ||
26 | { | ||
27 | /* | ||
28 | * There is no struct nvgpu_gpfifo for us to use yet. But when it's | ||
29 | * defined in userspace this is how big it will be. | ||
30 | */ | ||
31 | return 8; | ||
32 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/clk_arb.c b/drivers/gpu/nvgpu/common/posix/clk_arb.c new file mode 100644 index 00000000..f41383cd --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/clk_arb.c | |||
@@ -0,0 +1,148 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/clk_arb.h> | ||
24 | |||
25 | /** | ||
26 | * Stub imlementation of the clk_arb code. Yikes. Much of this probably could be | ||
27 | * commonized if one were to think through the implementation but that is | ||
28 | * probably weeks of work at a minimum. | ||
29 | * | ||
30 | * So for POSIX it will be stubbed. | ||
31 | */ | ||
32 | |||
33 | int nvgpu_clk_arb_init_arbiter(struct gk20a *g) | ||
34 | { | ||
35 | return -ENOSYS; | ||
36 | } | ||
37 | |||
38 | int nvgpu_clk_arb_get_arbiter_clk_range(struct gk20a *g, u32 api_domain, | ||
39 | u16 *min_mhz, u16 *max_mhz) | ||
40 | { | ||
41 | return -ENOSYS; | ||
42 | } | ||
43 | |||
44 | int nvgpu_clk_arb_get_arbiter_actual_mhz(struct gk20a *g, | ||
45 | u32 api_domain, u16 *actual_mhz) | ||
46 | { | ||
47 | return -ENOSYS; | ||
48 | } | ||
49 | |||
50 | int nvgpu_clk_arb_get_arbiter_effective_mhz(struct gk20a *g, | ||
51 | u32 api_domain, u16 *effective_mhz) | ||
52 | { | ||
53 | return -ENOSYS; | ||
54 | } | ||
55 | |||
56 | |||
57 | int nvgpu_clk_arb_get_arbiter_clk_f_points(struct gk20a *g, | ||
58 | u32 api_domain, | ||
59 | u32 *max_points, u16 *fpoints) | ||
60 | { | ||
61 | return -ENOSYS; | ||
62 | } | ||
63 | |||
64 | u32 nvgpu_clk_arb_get_arbiter_clk_domains(struct gk20a *g) | ||
65 | { | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | bool nvgpu_clk_arb_is_valid_domain(struct gk20a *g, u32 api_domain) | ||
70 | { | ||
71 | return false; | ||
72 | } | ||
73 | |||
74 | void nvgpu_clk_arb_cleanup_arbiter(struct gk20a *g) | ||
75 | { | ||
76 | } | ||
77 | |||
78 | int nvgpu_clk_arb_install_session_fd(struct gk20a *g, | ||
79 | struct nvgpu_clk_session *session) | ||
80 | { | ||
81 | return -ENOSYS; | ||
82 | } | ||
83 | |||
84 | |||
85 | int nvgpu_clk_arb_init_session(struct gk20a *g, | ||
86 | struct nvgpu_clk_session **_session) | ||
87 | { | ||
88 | return -ENOSYS; | ||
89 | } | ||
90 | |||
91 | void nvgpu_clk_arb_release_session(struct gk20a *g, | ||
92 | struct nvgpu_clk_session *session) | ||
93 | { | ||
94 | } | ||
95 | |||
96 | int nvgpu_clk_arb_commit_request_fd(struct gk20a *g, | ||
97 | struct nvgpu_clk_session *session, | ||
98 | int request_fd) | ||
99 | { | ||
100 | return -ENOSYS; | ||
101 | } | ||
102 | |||
103 | int nvgpu_clk_arb_set_session_target_mhz(struct nvgpu_clk_session *session, | ||
104 | int fd, u32 api_domain, u16 target_mhz) | ||
105 | { | ||
106 | return -ENOSYS; | ||
107 | } | ||
108 | |||
109 | int nvgpu_clk_arb_get_session_target_mhz(struct nvgpu_clk_session *session, | ||
110 | u32 api_domain, u16 *target_mhz) | ||
111 | { | ||
112 | return -ENOSYS; | ||
113 | } | ||
114 | |||
115 | int nvgpu_clk_arb_install_event_fd(struct gk20a *g, | ||
116 | struct nvgpu_clk_session *session, | ||
117 | int *event_fd, u32 alarm_mask) | ||
118 | { | ||
119 | return -ENOSYS; | ||
120 | } | ||
121 | |||
122 | int nvgpu_clk_arb_install_request_fd(struct gk20a *g, | ||
123 | struct nvgpu_clk_session *session, | ||
124 | int *event_fd) | ||
125 | { | ||
126 | return -ENOSYS; | ||
127 | } | ||
128 | |||
129 | void nvgpu_clk_arb_schedule_vf_table_update(struct gk20a *g) | ||
130 | { | ||
131 | } | ||
132 | |||
133 | int nvgpu_clk_arb_get_current_pstate(struct gk20a *g) | ||
134 | { | ||
135 | return -ENOSYS; | ||
136 | } | ||
137 | |||
138 | void nvgpu_clk_arb_pstate_change_lock(struct gk20a *g, bool lock) | ||
139 | { | ||
140 | } | ||
141 | |||
142 | void nvgpu_clk_arb_send_thermal_alarm(struct gk20a *g) | ||
143 | { | ||
144 | } | ||
145 | |||
146 | void nvgpu_clk_arb_schedule_alarm(struct gk20a *g, u32 alarm) | ||
147 | { | ||
148 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/cond.c b/drivers/gpu/nvgpu/common/posix/cond.c new file mode 100644 index 00000000..ca8a2c4a --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/cond.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/cond.h> | ||
24 | |||
25 | #include <nvgpu/posix/cond.h> | ||
26 | |||
27 | int nvgpu_cond_init(struct nvgpu_cond *cond) | ||
28 | { | ||
29 | return -ENOSYS; | ||
30 | } | ||
31 | |||
32 | int nvgpu_cond_signal(struct nvgpu_cond *cond) | ||
33 | { | ||
34 | return -ENOSYS; | ||
35 | } | ||
36 | |||
37 | int nvgpu_cond_signal_interruptible(struct nvgpu_cond *cond) | ||
38 | { | ||
39 | return -ENOSYS; | ||
40 | } | ||
41 | |||
42 | int nvgpu_cond_broadcast(struct nvgpu_cond *cond) | ||
43 | { | ||
44 | return -ENOSYS; | ||
45 | } | ||
46 | |||
47 | int nvgpu_cond_broadcast_interruptible(struct nvgpu_cond *cond) | ||
48 | { | ||
49 | return -ENOSYS; | ||
50 | } | ||
51 | |||
52 | void nvgpu_cond_destroy(struct nvgpu_cond *cond) | ||
53 | { | ||
54 | |||
55 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/dma.c b/drivers/gpu/nvgpu/common/posix/dma.c new file mode 100644 index 00000000..23f59501 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/dma.c | |||
@@ -0,0 +1,220 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | |||
25 | #include <nvgpu/mm.h> | ||
26 | #include <nvgpu/vm.h> | ||
27 | #include <nvgpu/bug.h> | ||
28 | #include <nvgpu/dma.h> | ||
29 | #include <nvgpu/gmmu.h> | ||
30 | #include <nvgpu/nvgpu_mem.h> | ||
31 | #include <nvgpu/enabled.h> | ||
32 | |||
33 | /* | ||
34 | * In userspace vidmem vs sysmem is just a difference in what is placed in the | ||
35 | * aperture field. | ||
36 | */ | ||
37 | static int __nvgpu_do_dma_alloc(struct gk20a *g, unsigned long flags, | ||
38 | size_t size, struct nvgpu_mem *mem, | ||
39 | enum nvgpu_aperture ap) | ||
40 | { | ||
41 | void *memory = malloc(mem->aligned_size); | ||
42 | |||
43 | if (memory == NULL) | ||
44 | return -ENOMEM; | ||
45 | |||
46 | mem->cpu_va = memory; | ||
47 | mem->aperture = ap; | ||
48 | mem->size = size; | ||
49 | mem->aligned_size = PAGE_ALIGN(size); | ||
50 | mem->gpu_va = 0ULL; | ||
51 | mem->skip_wmb = true; | ||
52 | mem->vidmem_alloc = NULL; | ||
53 | mem->allocator = NULL; | ||
54 | |||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | bool nvgpu_iommuable(struct gk20a *g) | ||
59 | { | ||
60 | return false; | ||
61 | } | ||
62 | |||
63 | int nvgpu_dma_alloc(struct gk20a *g, size_t size, struct nvgpu_mem *mem) | ||
64 | { | ||
65 | return nvgpu_dma_alloc_flags(g, 0, size, mem); | ||
66 | } | ||
67 | |||
68 | int nvgpu_dma_alloc_flags(struct gk20a *g, unsigned long flags, size_t size, | ||
69 | struct nvgpu_mem *mem) | ||
70 | { | ||
71 | if (!nvgpu_is_enabled(g, NVGPU_MM_UNIFIED_MEMORY)) { | ||
72 | /* | ||
73 | * First try vidmem. Obviously in userspace there's no such | ||
74 | * thing as vidmem per se but we will mark the aperture as | ||
75 | * vidmem. | ||
76 | */ | ||
77 | int err = nvgpu_dma_alloc_flags_vid(g, 0, size, mem); | ||
78 | |||
79 | if (!err) | ||
80 | return 0; | ||
81 | /* | ||
82 | * Fall back to sysmem (which may then also fail) in case | ||
83 | * vidmem is exhausted. | ||
84 | */ | ||
85 | } | ||
86 | |||
87 | return nvgpu_dma_alloc_flags_sys(g, flags, size, mem); | ||
88 | |||
89 | } | ||
90 | |||
91 | int nvgpu_dma_alloc_sys(struct gk20a *g, size_t size, struct nvgpu_mem *mem) | ||
92 | { | ||
93 | return nvgpu_dma_alloc_flags_sys(g, 0, size, mem); | ||
94 | } | ||
95 | |||
96 | int nvgpu_dma_alloc_flags_sys(struct gk20a *g, unsigned long flags, | ||
97 | size_t size, struct nvgpu_mem *mem) | ||
98 | { | ||
99 | return __nvgpu_do_dma_alloc(g, flags, size, mem, APERTURE_SYSMEM); | ||
100 | } | ||
101 | |||
102 | int nvgpu_dma_alloc_vid(struct gk20a *g, size_t size, struct nvgpu_mem *mem) | ||
103 | { | ||
104 | return nvgpu_dma_alloc_flags_vid(g, 0, size, mem); | ||
105 | } | ||
106 | |||
107 | int nvgpu_dma_alloc_flags_vid(struct gk20a *g, unsigned long flags, | ||
108 | size_t size, struct nvgpu_mem *mem) | ||
109 | { | ||
110 | return __nvgpu_do_dma_alloc(g, flags, size, mem, APERTURE_VIDMEM); | ||
111 | } | ||
112 | |||
113 | int nvgpu_dma_alloc_flags_vid_at(struct gk20a *g, unsigned long flags, | ||
114 | size_t size, struct nvgpu_mem *mem, u64 at) | ||
115 | { | ||
116 | BUG(); | ||
117 | |||
118 | return 0; | ||
119 | } | ||
120 | |||
121 | void nvgpu_dma_free(struct gk20a *g, struct nvgpu_mem *mem) | ||
122 | { | ||
123 | if (!(mem->mem_flags & NVGPU_MEM_FLAG_SHADOW_COPY)) | ||
124 | free(mem->cpu_va); | ||
125 | |||
126 | memset(mem, 0, sizeof(*mem)); | ||
127 | } | ||
128 | |||
129 | int nvgpu_dma_alloc_map(struct vm_gk20a *vm, size_t size, | ||
130 | struct nvgpu_mem *mem) | ||
131 | { | ||
132 | return nvgpu_dma_alloc_map_flags(vm, 0, size, mem); | ||
133 | } | ||
134 | |||
135 | int nvgpu_dma_alloc_map_flags(struct vm_gk20a *vm, unsigned long flags, | ||
136 | size_t size, struct nvgpu_mem *mem) | ||
137 | { | ||
138 | if (!nvgpu_is_enabled(gk20a_from_vm(vm), NVGPU_MM_UNIFIED_MEMORY)) { | ||
139 | int err = nvgpu_dma_alloc_map_flags_vid(vm, | ||
140 | flags | NVGPU_DMA_NO_KERNEL_MAPPING, | ||
141 | size, mem); | ||
142 | |||
143 | if (!err) | ||
144 | return 0; | ||
145 | /* | ||
146 | * Fall back to sysmem (which may then also fail) in case | ||
147 | * vidmem is exhausted. | ||
148 | */ | ||
149 | } | ||
150 | |||
151 | return nvgpu_dma_alloc_map_flags_sys(vm, flags, size, mem); | ||
152 | } | ||
153 | |||
154 | int nvgpu_dma_alloc_map_sys(struct vm_gk20a *vm, size_t size, | ||
155 | struct nvgpu_mem *mem) | ||
156 | { | ||
157 | return nvgpu_dma_alloc_map_flags_sys(vm, 0, size, mem); | ||
158 | } | ||
159 | |||
160 | int nvgpu_dma_alloc_map_flags_sys(struct vm_gk20a *vm, unsigned long flags, | ||
161 | size_t size, struct nvgpu_mem *mem) | ||
162 | { | ||
163 | int err = nvgpu_dma_alloc_flags_sys(vm->mm->g, flags, size, mem); | ||
164 | |||
165 | if (err) | ||
166 | return err; | ||
167 | |||
168 | mem->gpu_va = nvgpu_gmmu_map(vm, mem, size, 0, | ||
169 | gk20a_mem_flag_none, false, | ||
170 | mem->aperture); | ||
171 | if (!mem->gpu_va) { | ||
172 | err = -ENOMEM; | ||
173 | goto fail_free; | ||
174 | } | ||
175 | |||
176 | return 0; | ||
177 | |||
178 | fail_free: | ||
179 | nvgpu_dma_free(vm->mm->g, mem); | ||
180 | return err; | ||
181 | } | ||
182 | |||
183 | int nvgpu_dma_alloc_map_vid(struct vm_gk20a *vm, size_t size, | ||
184 | struct nvgpu_mem *mem) | ||
185 | { | ||
186 | return nvgpu_dma_alloc_map_flags_vid(vm, | ||
187 | NVGPU_DMA_NO_KERNEL_MAPPING, size, mem); | ||
188 | } | ||
189 | |||
190 | int nvgpu_dma_alloc_map_flags_vid(struct vm_gk20a *vm, unsigned long flags, | ||
191 | size_t size, struct nvgpu_mem *mem) | ||
192 | { | ||
193 | int err = nvgpu_dma_alloc_flags_vid(vm->mm->g, flags, size, mem); | ||
194 | |||
195 | if (err) | ||
196 | return err; | ||
197 | |||
198 | mem->gpu_va = nvgpu_gmmu_map(vm, mem, size, 0, | ||
199 | gk20a_mem_flag_none, false, | ||
200 | mem->aperture); | ||
201 | if (!mem->gpu_va) { | ||
202 | err = -ENOMEM; | ||
203 | goto fail_free; | ||
204 | } | ||
205 | |||
206 | return 0; | ||
207 | |||
208 | fail_free: | ||
209 | nvgpu_dma_free(vm->mm->g, mem); | ||
210 | return err; | ||
211 | } | ||
212 | |||
213 | void nvgpu_dma_unmap_free(struct vm_gk20a *vm, struct nvgpu_mem *mem) | ||
214 | { | ||
215 | if (mem->gpu_va) | ||
216 | nvgpu_gmmu_unmap(vm, mem, mem->gpu_va); | ||
217 | mem->gpu_va = 0; | ||
218 | |||
219 | nvgpu_dma_free(vm->mm->g, mem); | ||
220 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/error_notifier.c b/drivers/gpu/nvgpu/common/posix/error_notifier.c new file mode 100644 index 00000000..50b4f258 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/error_notifier.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/error_notifier.h> | ||
24 | |||
25 | void nvgpu_set_error_notifier_locked(struct channel_gk20a *ch, u32 error) | ||
26 | { | ||
27 | } | ||
28 | |||
29 | void nvgpu_set_error_notifier(struct channel_gk20a *ch, u32 error) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | void nvgpu_set_error_notifier_if_empty(struct channel_gk20a *ch, u32 error) | ||
34 | { | ||
35 | } | ||
36 | |||
37 | bool nvgpu_is_error_notifier_set(struct channel_gk20a *ch, u32 error_notifier) | ||
38 | { | ||
39 | return false; | ||
40 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/firmware.c b/drivers/gpu/nvgpu/common/posix/firmware.c new file mode 100644 index 00000000..aedfef9f --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/firmware.c | |||
@@ -0,0 +1,35 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/firmware.h> | ||
24 | |||
25 | struct nvgpu_firmware *nvgpu_request_firmware(struct gk20a *g, | ||
26 | const char *fw_name, | ||
27 | int flags) | ||
28 | { | ||
29 | return NULL; | ||
30 | } | ||
31 | |||
32 | void nvgpu_release_firmware(struct gk20a *g, struct nvgpu_firmware *fw) | ||
33 | { | ||
34 | /* Noop. */ | ||
35 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/fuse.c b/drivers/gpu/nvgpu/common/posix/fuse.c new file mode 100644 index 00000000..09ec36dc --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/fuse.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/fuse.h> | ||
24 | |||
25 | int nvgpu_tegra_get_gpu_speedo_id(struct gk20a *g) | ||
26 | { | ||
27 | return 0; | ||
28 | } | ||
29 | |||
30 | void nvgpu_tegra_fuse_write_bypass(struct gk20a *g, u32 val) | ||
31 | { | ||
32 | } | ||
33 | |||
34 | void nvgpu_tegra_fuse_write_access_sw(struct gk20a *g, u32 val) | ||
35 | { | ||
36 | } | ||
37 | |||
38 | void nvgpu_tegra_fuse_write_opt_gpu_tpc0_disable(struct gk20a *g, u32 val) | ||
39 | { | ||
40 | } | ||
41 | |||
42 | void nvgpu_tegra_fuse_write_opt_gpu_tpc1_disable(struct gk20a *g, u32 val) | ||
43 | { | ||
44 | } | ||
45 | |||
46 | int nvgpu_tegra_fuse_read_gcplex_config_fuse(struct gk20a *g, u32 *val) | ||
47 | { | ||
48 | return -ENODEV; | ||
49 | } | ||
50 | |||
51 | int nvgpu_tegra_fuse_read_reserved_calib(struct gk20a *g, u32 *val) | ||
52 | { | ||
53 | return -ENODEV; | ||
54 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/io.c b/drivers/gpu/nvgpu/common/posix/io.c new file mode 100644 index 00000000..ce018940 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/io.c | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/io.h> | ||
24 | #include <nvgpu/io_usermode.h> | ||
25 | #include <nvgpu/bug.h> | ||
26 | |||
27 | /* | ||
28 | * For now none of these make sense to execute in userspace. Eventually we | ||
29 | * may want to use these to verify certain register read/write sequences | ||
30 | * but for now, just hang. | ||
31 | */ | ||
32 | |||
33 | void nvgpu_writel(struct gk20a *g, u32 r, u32 v) | ||
34 | { | ||
35 | BUG(); | ||
36 | } | ||
37 | |||
38 | u32 nvgpu_readl(struct gk20a *g, u32 r) | ||
39 | { | ||
40 | BUG(); | ||
41 | |||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | u32 __nvgpu_readl(struct gk20a *g, u32 r) | ||
46 | { | ||
47 | BUG(); | ||
48 | |||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | void nvgpu_writel_check(struct gk20a *g, u32 r, u32 v) | ||
53 | { | ||
54 | BUG(); | ||
55 | } | ||
56 | |||
57 | void nvgpu_bar1_writel(struct gk20a *g, u32 b, u32 v) | ||
58 | { | ||
59 | BUG(); | ||
60 | } | ||
61 | |||
62 | u32 nvgpu_bar1_readl(struct gk20a *g, u32 b) | ||
63 | { | ||
64 | BUG(); | ||
65 | |||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | bool nvgpu_io_exists(struct gk20a *g) | ||
70 | { | ||
71 | return false; | ||
72 | } | ||
73 | |||
74 | bool nvgpu_io_valid_reg(struct gk20a *g, u32 r) | ||
75 | { | ||
76 | return false; | ||
77 | } | ||
78 | |||
79 | void nvgpu_usermode_writel(struct gk20a *g, u32 r, u32 v) | ||
80 | { | ||
81 | BUG(); | ||
82 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/kmem.c b/drivers/gpu/nvgpu/common/posix/kmem.c new file mode 100644 index 00000000..3c0b9b66 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/kmem.c | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | |||
25 | #include <nvgpu/bug.h> | ||
26 | #include <nvgpu/kmem.h> | ||
27 | #include <nvgpu/types.h> | ||
28 | |||
29 | #include <nvgpu/posix/kmem.h> | ||
30 | |||
31 | struct nvgpu_kmem_cache { | ||
32 | size_t alloc_size; | ||
33 | }; | ||
34 | |||
35 | /* | ||
36 | * kmem cache emulation: basically just do a regular malloc(). This is slower | ||
37 | * but should not affect a user of kmem cache in the slightest bit. | ||
38 | */ | ||
39 | struct nvgpu_kmem_cache *nvgpu_kmem_cache_create(struct gk20a *g, size_t size) | ||
40 | { | ||
41 | struct nvgpu_kmem_cache *cache = | ||
42 | malloc(sizeof(struct nvgpu_kmem_cache)); | ||
43 | |||
44 | if (cache != NULL) | ||
45 | return NULL; | ||
46 | |||
47 | cache->alloc_size = size; | ||
48 | |||
49 | return cache; | ||
50 | } | ||
51 | |||
52 | void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache) | ||
53 | { | ||
54 | free(cache); | ||
55 | } | ||
56 | |||
57 | void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache) | ||
58 | { | ||
59 | return malloc(cache->alloc_size); | ||
60 | } | ||
61 | |||
62 | void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr) | ||
63 | { | ||
64 | free(ptr); | ||
65 | } | ||
66 | |||
67 | void *__nvgpu_kmalloc(struct gk20a *g, size_t size, unsigned long ip) | ||
68 | { | ||
69 | return malloc(size); | ||
70 | } | ||
71 | |||
72 | void *__nvgpu_kzalloc(struct gk20a *g, size_t size, unsigned long ip) | ||
73 | { | ||
74 | return calloc(1, size); | ||
75 | } | ||
76 | |||
77 | void *__nvgpu_kcalloc(struct gk20a *g, size_t n, size_t size, unsigned long ip) | ||
78 | { | ||
79 | /* | ||
80 | * calloc() implicitly zeros mem. So calloc a single member size bytes | ||
81 | * long. | ||
82 | */ | ||
83 | return calloc(n, size); | ||
84 | } | ||
85 | |||
86 | void __nvgpu_kfree(struct gk20a *g, void *addr) | ||
87 | { | ||
88 | free(addr); | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * The concept of vmalloc() does not exist in userspace. | ||
93 | */ | ||
94 | void *__nvgpu_vmalloc(struct gk20a *g, unsigned long size, unsigned long ip) | ||
95 | { | ||
96 | return __nvgpu_kmalloc(g, size, ip); | ||
97 | } | ||
98 | |||
99 | void *__nvgpu_vzalloc(struct gk20a *g, unsigned long size, unsigned long ip) | ||
100 | { | ||
101 | return __nvgpu_kzalloc(g, size, ip); | ||
102 | } | ||
103 | |||
104 | void __nvgpu_vfree(struct gk20a *g, void *addr) | ||
105 | { | ||
106 | __nvgpu_kfree(g, addr); | ||
107 | } | ||
108 | |||
109 | void *__nvgpu_big_alloc(struct gk20a *g, size_t size, bool clear) | ||
110 | { | ||
111 | /* | ||
112 | * Since in userspace vmalloc() == kmalloc() == malloc() we can just | ||
113 | * reuse k[zm]alloc() for this. | ||
114 | */ | ||
115 | return clear ? | ||
116 | __nvgpu_kzalloc(g, size, _THIS_IP_) : | ||
117 | __nvgpu_kmalloc(g, size, _THIS_IP_); | ||
118 | } | ||
119 | |||
120 | void nvgpu_big_free(struct gk20a *g, void *p) | ||
121 | { | ||
122 | __nvgpu_kfree(g, p); | ||
123 | } | ||
124 | |||
125 | int nvgpu_kmem_init(struct gk20a *g) | ||
126 | { | ||
127 | /* Nothing to init at the moment. */ | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | void nvgpu_kmem_fini(struct gk20a *g, int flags) | ||
132 | { | ||
133 | |||
134 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/lock.c b/drivers/gpu/nvgpu/common/posix/lock.c new file mode 100644 index 00000000..bca0f04c --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/lock.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/lock.h> | ||
24 | #include <nvgpu/posix/lock.h> | ||
25 | |||
26 | int nvgpu_mutex_init(struct nvgpu_mutex *mutex) | ||
27 | { | ||
28 | return pthread_mutex_init(&mutex->lock.mutex, NULL); | ||
29 | } | ||
30 | |||
31 | void nvgpu_mutex_acquire(struct nvgpu_mutex *mutex) | ||
32 | { | ||
33 | __nvgpu_posix_lock_acquire(&mutex->lock); | ||
34 | } | ||
35 | |||
36 | void nvgpu_mutex_release(struct nvgpu_mutex *mutex) | ||
37 | { | ||
38 | __nvgpu_posix_lock_release(&mutex->lock); | ||
39 | } | ||
40 | |||
41 | int nvgpu_mutex_tryacquire(struct nvgpu_mutex *mutex) | ||
42 | { | ||
43 | return __nvgpu_posix_lock_try_acquire(&mutex->lock); | ||
44 | } | ||
45 | |||
46 | void nvgpu_mutex_destroy(struct nvgpu_mutex *mutex) | ||
47 | { | ||
48 | pthread_mutex_destroy(&mutex->lock.mutex); | ||
49 | } | ||
50 | |||
51 | void nvgpu_spinlock_init(struct nvgpu_spinlock *spinlock) | ||
52 | { | ||
53 | pthread_mutex_init(&spinlock->lock.mutex, NULL); | ||
54 | } | ||
55 | |||
56 | void nvgpu_spinlock_acquire(struct nvgpu_spinlock *spinlock) | ||
57 | { | ||
58 | __nvgpu_posix_lock_acquire(&spinlock->lock); | ||
59 | } | ||
60 | |||
61 | void nvgpu_spinlock_release(struct nvgpu_spinlock *spinlock) | ||
62 | { | ||
63 | __nvgpu_posix_lock_release(&spinlock->lock); | ||
64 | } | ||
65 | |||
66 | void nvgpu_raw_spinlock_init(struct nvgpu_raw_spinlock *spinlock) | ||
67 | { | ||
68 | pthread_mutex_init(&spinlock->lock.mutex, NULL); | ||
69 | } | ||
70 | |||
71 | void nvgpu_raw_spinlock_acquire(struct nvgpu_raw_spinlock *spinlock) | ||
72 | { | ||
73 | __nvgpu_posix_lock_acquire(&spinlock->lock); | ||
74 | } | ||
75 | |||
76 | void nvgpu_raw_spinlock_release(struct nvgpu_raw_spinlock *spinlock) | ||
77 | { | ||
78 | __nvgpu_posix_lock_release(&spinlock->lock); | ||
79 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/log.c b/drivers/gpu/nvgpu/common/posix/log.c new file mode 100644 index 00000000..6bfb673c --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/log.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/log.h> | ||
24 | #include <nvgpu/types.h> | ||
25 | |||
26 | #include "gk20a/gk20a.h" | ||
27 | |||
28 | /* | ||
29 | * Define a length for log buffers. This is the buffer that the 'fmt, ...' part | ||
30 | * of __nvgpu_do_log_print() prints into. | ||
31 | */ | ||
32 | #define LOG_BUFFER_LENGTH 160 | ||
33 | |||
34 | /* | ||
35 | * Keep this roughly the same as the kernel log format. | ||
36 | */ | ||
37 | #define LOG_FMT "nvgpu: %s %33s:%-4d [%-4s] %s\n" | ||
38 | |||
39 | u64 nvgpu_dbg_mask = NVGPU_DEFAULT_DBG_MASK; | ||
40 | |||
41 | static const char *log_types[] = { | ||
42 | "ERR", | ||
43 | "WRN", | ||
44 | "DBG", | ||
45 | "INFO", | ||
46 | }; | ||
47 | |||
48 | static inline const char *nvgpu_log_name(struct gk20a *g) | ||
49 | { | ||
50 | return "gpu.USS"; | ||
51 | } | ||
52 | |||
53 | static void __nvgpu_really_print_log(const char *gpu_name, | ||
54 | const char *func_name, int line, | ||
55 | enum nvgpu_log_type type, const char *log) | ||
56 | { | ||
57 | const char *name = gpu_name ? gpu_name : ""; | ||
58 | const char *log_type = log_types[type]; | ||
59 | |||
60 | printf(LOG_FMT, name, func_name, line, log_type, log); | ||
61 | } | ||
62 | |||
63 | __attribute__((format (printf, 5, 6))) | ||
64 | void __nvgpu_log_msg(struct gk20a *g, const char *func_name, int line, | ||
65 | enum nvgpu_log_type type, const char *fmt, ...) | ||
66 | { | ||
67 | char log[LOG_BUFFER_LENGTH]; | ||
68 | va_list args; | ||
69 | |||
70 | va_start(args, fmt); | ||
71 | vsnprintf(log, LOG_BUFFER_LENGTH, fmt, args); | ||
72 | va_end(args); | ||
73 | |||
74 | __nvgpu_really_print_log(nvgpu_log_name(g), | ||
75 | func_name, line, type, log); | ||
76 | } | ||
77 | |||
78 | __attribute__((format (printf, 5, 6))) | ||
79 | void __nvgpu_log_dbg(struct gk20a *g, u64 log_mask, | ||
80 | const char *func_name, int line, | ||
81 | const char *fmt, ...) | ||
82 | { | ||
83 | char log[LOG_BUFFER_LENGTH]; | ||
84 | va_list args; | ||
85 | |||
86 | if ((log_mask & g->log_mask) == 0) | ||
87 | return; | ||
88 | |||
89 | va_start(args, fmt); | ||
90 | vsnprintf(log, LOG_BUFFER_LENGTH, fmt, args); | ||
91 | va_end(args); | ||
92 | |||
93 | __nvgpu_really_print_log(nvgpu_log_name(g), | ||
94 | func_name, line, NVGPU_DEBUG, log); | ||
95 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/nvgpu.c b/drivers/gpu/nvgpu/common/posix/nvgpu.c new file mode 100644 index 00000000..6f2a5fe9 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/nvgpu.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <unistd.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <pthread.h> | ||
26 | |||
27 | #include <nvgpu/bug.h> | ||
28 | #include <nvgpu/types.h> | ||
29 | #include <nvgpu/atomic.h> | ||
30 | #include <nvgpu/nvgpu_common.h> | ||
31 | #include <nvgpu/os_sched.h> | ||
32 | |||
33 | #include <nvgpu/posix/probe.h> | ||
34 | |||
35 | #include "os_posix.h" | ||
36 | |||
37 | #include "gk20a/gk20a.h" | ||
38 | |||
39 | void nvgpu_wait_for_deferred_interrupts(struct gk20a *g) | ||
40 | { | ||
41 | /* | ||
42 | * No interrupts in userspace so nothing to wait for. | ||
43 | */ | ||
44 | } | ||
45 | |||
46 | int nvgpu_current_pid(struct gk20a *g) | ||
47 | { | ||
48 | /* | ||
49 | * In the kernel this gets us the PID of the calling process for IOCTLs. | ||
50 | * But since we are in userspace this doesn't quite mean the same thing. | ||
51 | * This simply returns the PID of the currently running process. | ||
52 | */ | ||
53 | return (int)getpid(); | ||
54 | } | ||
55 | |||
56 | int nvgpu_current_tid(struct gk20a *g) | ||
57 | { | ||
58 | /* | ||
59 | * In POSIX thread ID is not the same as a process ID. In Linux threads | ||
60 | * and processes are represented by the same thing, but userspace can't | ||
61 | * really rely on that. | ||
62 | * | ||
63 | * We can, however, get a pthread_t for a given thread. But this | ||
64 | * pthread_t need not have any relation to the underlying system's | ||
65 | * representation of "threads". | ||
66 | */ | ||
67 | return (int)pthread_self(); | ||
68 | } | ||
69 | |||
70 | /* | ||
71 | * Somewhat meaningless in userspace... | ||
72 | */ | ||
73 | void nvgpu_kernel_restart(void *cmd) | ||
74 | { | ||
75 | BUG(); | ||
76 | } | ||
77 | |||
78 | /* | ||
79 | * We have no runtime PM stuff in userspace so these are really just noops. | ||
80 | */ | ||
81 | void gk20a_busy_noresume(struct gk20a *g) | ||
82 | { | ||
83 | } | ||
84 | |||
85 | void gk20a_idle_nosuspend(struct gk20a *g) | ||
86 | { | ||
87 | } | ||
88 | |||
89 | int gk20a_busy(struct gk20a *g) | ||
90 | { | ||
91 | nvgpu_atomic_inc(&g->usage_count); | ||
92 | |||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | void gk20a_idle(struct gk20a *g) | ||
97 | { | ||
98 | nvgpu_atomic_dec(&g->usage_count); | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * This function aims to initialize enough stuff to make unit testing worth | ||
103 | * while. There are several interfaces and APIs that rely on the struct gk20a's | ||
104 | * state in order to function: logging, for example, but there are many other | ||
105 | * things, too. | ||
106 | * | ||
107 | * Initialize as much of that as possible here. This is meant to be equivalent | ||
108 | * to the kernel space driver's probe function. | ||
109 | */ | ||
110 | struct gk20a *nvgpu_posix_probe(void) | ||
111 | { | ||
112 | struct gk20a *g; | ||
113 | struct nvgpu_os_posix *p; | ||
114 | int err; | ||
115 | |||
116 | p = malloc(sizeof(*p)); | ||
117 | if (p == NULL) | ||
118 | return NULL; | ||
119 | |||
120 | g = &p->g; | ||
121 | |||
122 | err = nvgpu_kmem_init(g); | ||
123 | if (err != 0) | ||
124 | goto fail; | ||
125 | |||
126 | return g; | ||
127 | |||
128 | fail: | ||
129 | free(p); | ||
130 | |||
131 | return NULL; | ||
132 | } | ||
133 | |||
134 | void nvgpu_posix_cleanup(struct gk20a *g) | ||
135 | { | ||
136 | nvgpu_kmem_fini(g, 0); | ||
137 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/nvlink.c b/drivers/gpu/nvgpu/common/posix/nvlink.c new file mode 100644 index 00000000..c830d6ed --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/nvlink.c | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/nvlink.h> | ||
24 | |||
25 | int nvgpu_nvlink_train(struct gk20a *g, u32 link_id, bool from_off) | ||
26 | { | ||
27 | return -ENOSYS; | ||
28 | } | ||
29 | |||
30 | int nvgpu_nvlink_enumerate(struct gk20a *g) | ||
31 | { | ||
32 | return -ENOSYS; | ||
33 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/os_posix.h b/drivers/gpu/nvgpu/common/posix/os_posix.h new file mode 100644 index 00000000..955186ef --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/os_posix.h | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #ifndef __NVGPU_OS_POSIX_H__ | ||
24 | #define __NVGPU_OS_POSIX_H__ | ||
25 | |||
26 | #include "gk20a/gk20a.h" | ||
27 | |||
28 | struct nvgpu_os_posix { | ||
29 | struct gk20a g; | ||
30 | }; | ||
31 | |||
32 | #endif | ||
diff --git a/drivers/gpu/nvgpu/common/posix/posix-comptags.c b/drivers/gpu/nvgpu/common/posix/posix-comptags.c new file mode 100644 index 00000000..a00246dd --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/posix-comptags.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/bug.h> | ||
24 | #include <nvgpu/comptags.h> | ||
25 | |||
26 | #include <nvgpu/posix/vm.h> | ||
27 | |||
28 | void gk20a_get_comptags(struct nvgpu_os_buffer *buf, | ||
29 | struct gk20a_comptags *comptags) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | int gk20a_alloc_or_get_comptags(struct gk20a *g, | ||
34 | struct nvgpu_os_buffer *buf, | ||
35 | struct gk20a_comptag_allocator *allocator, | ||
36 | struct gk20a_comptags *comptags) | ||
37 | { | ||
38 | return -ENODEV; | ||
39 | } | ||
40 | |||
41 | bool gk20a_comptags_start_clear(struct nvgpu_os_buffer *buf) | ||
42 | { | ||
43 | return false; | ||
44 | } | ||
45 | |||
46 | void gk20a_comptags_finish_clear(struct nvgpu_os_buffer *buf, | ||
47 | bool clear_successful) | ||
48 | { | ||
49 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/posix-nvgpu_mem.c b/drivers/gpu/nvgpu/common/posix/posix-nvgpu_mem.c new file mode 100644 index 00000000..f8feb6a2 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/posix-nvgpu_mem.c | |||
@@ -0,0 +1,209 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/bug.h> | ||
24 | #include <nvgpu/dma.h> | ||
25 | #include <nvgpu/gmmu.h> | ||
26 | #include <nvgpu/kmem.h> | ||
27 | #include <nvgpu/nvgpu_mem.h> | ||
28 | |||
29 | /* | ||
30 | * DMA memory buffers - obviously we don't really have DMA in userspace but we | ||
31 | * can emulate a lot of the DMA mem functionality for unit testing purposes. | ||
32 | */ | ||
33 | |||
34 | int nvgpu_mem_begin(struct gk20a *g, struct nvgpu_mem *mem) | ||
35 | { | ||
36 | return 0; | ||
37 | } | ||
38 | |||
39 | void nvgpu_mem_end(struct gk20a *g, struct nvgpu_mem *mem) | ||
40 | { | ||
41 | return; | ||
42 | } | ||
43 | |||
44 | u32 nvgpu_mem_rd32(struct gk20a *g, struct nvgpu_mem *mem, u32 w) | ||
45 | { | ||
46 | u32 *mem_ptr = (u32 *)mem->cpu_va; | ||
47 | |||
48 | return mem_ptr[w]; | ||
49 | } | ||
50 | |||
51 | u32 nvgpu_mem_rd(struct gk20a *g, struct nvgpu_mem *mem, u32 offset) | ||
52 | { | ||
53 | if (offset & 0x3) | ||
54 | BUG(); | ||
55 | |||
56 | return nvgpu_mem_rd32(g, mem, offset >> 2); | ||
57 | } | ||
58 | |||
59 | void nvgpu_mem_rd_n(struct gk20a *g, struct nvgpu_mem *mem, u32 offset, | ||
60 | void *dest, u32 size) | ||
61 | { | ||
62 | if (offset & 0x3 || size & 0x3) | ||
63 | BUG(); | ||
64 | |||
65 | memcpy(dest, ((char *)mem->cpu_va) + offset, size); | ||
66 | } | ||
67 | |||
68 | void nvgpu_mem_wr32(struct gk20a *g, struct nvgpu_mem *mem, u32 w, u32 data) | ||
69 | { | ||
70 | u32 *mem_ptr = (u32 *)mem->cpu_va; | ||
71 | |||
72 | mem_ptr[w] = data; | ||
73 | } | ||
74 | |||
75 | void nvgpu_mem_wr(struct gk20a *g, struct nvgpu_mem *mem, u32 offset, u32 data) | ||
76 | { | ||
77 | if (offset & 0x3) | ||
78 | BUG(); | ||
79 | |||
80 | nvgpu_mem_wr32(g, mem, offset >> 2, data); | ||
81 | } | ||
82 | |||
83 | void nvgpu_mem_wr_n(struct gk20a *g, struct nvgpu_mem *mem, u32 offset, | ||
84 | void *src, u32 size) | ||
85 | { | ||
86 | if (offset & 0x3 || size & 0x3) | ||
87 | BUG(); | ||
88 | |||
89 | memcpy(((char *)mem->cpu_va) + offset, src, size); | ||
90 | } | ||
91 | |||
92 | void nvgpu_memset(struct gk20a *g, struct nvgpu_mem *mem, u32 offset, | ||
93 | u32 c, u32 size) | ||
94 | { | ||
95 | memset(((char *)mem->cpu_va) + offset, c, size); | ||
96 | } | ||
97 | |||
98 | /* | ||
99 | * These functions are somewhat meaningless. | ||
100 | */ | ||
101 | u64 nvgpu_mem_get_addr(struct gk20a *g, struct nvgpu_mem *mem) | ||
102 | { | ||
103 | return (u64)(uintptr_t)mem->cpu_va; | ||
104 | } | ||
105 | |||
106 | u64 nvgpu_mem_get_phys_addr(struct gk20a *g, struct nvgpu_mem *mem) | ||
107 | { | ||
108 | return (u64)(uintptr_t)mem->cpu_va; | ||
109 | } | ||
110 | |||
111 | static struct nvgpu_sgl *nvgpu_mem_sgl_next(struct nvgpu_sgl *sgl) | ||
112 | { | ||
113 | return NULL; | ||
114 | } | ||
115 | |||
116 | static u64 nvgpu_mem_sgl_phys(struct gk20a *g, struct nvgpu_sgl *sgl) | ||
117 | { | ||
118 | struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl; | ||
119 | |||
120 | return (u64)(uintptr_t)mem->cpu_va; | ||
121 | } | ||
122 | |||
123 | static u64 nvgpu_mem_sgl_dma(struct nvgpu_sgl *sgl) | ||
124 | { | ||
125 | struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl; | ||
126 | |||
127 | return (u64)(uintptr_t)mem->cpu_va; | ||
128 | } | ||
129 | |||
130 | static u64 nvgpu_mem_sgl_length(struct nvgpu_sgl *sgl) | ||
131 | { | ||
132 | struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl; | ||
133 | |||
134 | return (u64)mem->aligned_size; | ||
135 | } | ||
136 | |||
137 | static u64 nvgpu_mem_sgl_gpu_addr(struct gk20a *g, struct nvgpu_sgl *sgl, | ||
138 | struct nvgpu_gmmu_attrs *attrs) | ||
139 | { | ||
140 | struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl; | ||
141 | |||
142 | return mem->gpu_va; | ||
143 | } | ||
144 | |||
145 | static bool nvgpu_mem_sgt_iommuable(struct gk20a *g, struct nvgpu_sgt *sgt) | ||
146 | { | ||
147 | return nvgpu_iommuable(g); | ||
148 | } | ||
149 | |||
150 | static void nvgpu_mem_sgt_free(struct gk20a *g, struct nvgpu_sgt *sgt) | ||
151 | { | ||
152 | nvgpu_kfree(g, sgt); | ||
153 | } | ||
154 | |||
155 | static struct nvgpu_sgt_ops nvgpu_sgt_posix_ops = { | ||
156 | .sgl_next = nvgpu_mem_sgl_next, | ||
157 | .sgl_phys = nvgpu_mem_sgl_phys, | ||
158 | .sgl_dma = nvgpu_mem_sgl_dma, | ||
159 | .sgl_length = nvgpu_mem_sgl_length, | ||
160 | .sgl_gpu_addr = nvgpu_mem_sgl_gpu_addr, | ||
161 | .sgt_iommuable = nvgpu_mem_sgt_iommuable, | ||
162 | .sgt_free = nvgpu_mem_sgt_free, | ||
163 | }; | ||
164 | |||
165 | struct nvgpu_sgt *nvgpu_sgt_create_from_mem(struct gk20a *g, | ||
166 | struct nvgpu_mem *mem) | ||
167 | { | ||
168 | struct nvgpu_sgt *sgt = nvgpu_kzalloc(g, sizeof(*sgt)); | ||
169 | |||
170 | if (sgt == NULL) | ||
171 | return NULL; | ||
172 | |||
173 | /* | ||
174 | * The userspace implementation is simple: a single 'entry' (which we | ||
175 | * only need the mem struct to describe). Maybe this could be expanded | ||
176 | * to be more interesting some day. | ||
177 | */ | ||
178 | sgt->sgl = (struct nvgpu_sgl *)mem; | ||
179 | sgt->ops = &nvgpu_sgt_posix_ops; | ||
180 | |||
181 | return sgt; | ||
182 | } | ||
183 | |||
184 | int nvgpu_mem_create_from_mem(struct gk20a *g, | ||
185 | struct nvgpu_mem *dest, struct nvgpu_mem *src, | ||
186 | int start_page, int nr_pages) | ||
187 | { | ||
188 | u64 start = start_page * PAGE_SIZE; | ||
189 | u64 size = nr_pages * PAGE_SIZE; | ||
190 | |||
191 | if (src->aperture != APERTURE_SYSMEM) | ||
192 | return -EINVAL; | ||
193 | |||
194 | /* Some silly things a caller might do... */ | ||
195 | if (size > src->size) | ||
196 | return -EINVAL; | ||
197 | if ((start + size) > src->size) | ||
198 | return -EINVAL; | ||
199 | |||
200 | memset(dest, 0, sizeof(*dest)); | ||
201 | |||
202 | dest->cpu_va = ((char *)src->cpu_va) + start; | ||
203 | dest->mem_flags = src->mem_flags | NVGPU_MEM_FLAG_SHADOW_COPY; | ||
204 | dest->aperture = src->aperture; | ||
205 | dest->skip_wmb = src->skip_wmb; | ||
206 | dest->size = size; | ||
207 | |||
208 | return 0; | ||
209 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/posix-vm.c b/drivers/gpu/nvgpu/common/posix/posix-vm.c new file mode 100644 index 00000000..588b956d --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/posix-vm.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | |||
25 | #include <nvgpu/vm.h> | ||
26 | #include <nvgpu/bug.h> | ||
27 | |||
28 | #include <nvgpu/posix/vm.h> | ||
29 | |||
30 | u64 nvgpu_os_buf_get_size(struct nvgpu_os_buffer *os_buf) | ||
31 | { | ||
32 | return os_buf->size; | ||
33 | } | ||
34 | |||
35 | struct nvgpu_mapped_buf *nvgpu_vm_find_mapping(struct vm_gk20a *vm, | ||
36 | struct nvgpu_os_buffer *os_buf, | ||
37 | u64 map_addr, | ||
38 | u32 flags, | ||
39 | int kind) | ||
40 | { | ||
41 | BUG(); | ||
42 | |||
43 | /* | ||
44 | * No map caching for now. | ||
45 | */ | ||
46 | return NULL; | ||
47 | } | ||
48 | |||
49 | void nvgpu_vm_unmap_system(struct nvgpu_mapped_buf *mapped_buffer) | ||
50 | { | ||
51 | free(mapped_buffer->os_priv.buf); | ||
52 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/rwsem.c b/drivers/gpu/nvgpu/common/posix/rwsem.c new file mode 100644 index 00000000..7a696b75 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/rwsem.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/bug.h> | ||
24 | #include <nvgpu/rwsem.h> | ||
25 | #include <nvgpu/timers.h> | ||
26 | |||
27 | #include <nvgpu/posix/rwsem.h> | ||
28 | |||
29 | void nvgpu_rwsem_init(struct nvgpu_rwsem *rwsem) | ||
30 | { | ||
31 | memset(rwsem, 0, sizeof(*rwsem)); | ||
32 | |||
33 | nvgpu_spinlock_init(&rwsem->lock); | ||
34 | } | ||
35 | |||
36 | /* | ||
37 | * Acquire. | ||
38 | */ | ||
39 | void nvgpu_rwsem_down_read(struct nvgpu_rwsem *rwsem) | ||
40 | { | ||
41 | while (true) { | ||
42 | nvgpu_spinlock_acquire(&rwsem->lock); | ||
43 | |||
44 | /* | ||
45 | * If there's a writer try again. | ||
46 | */ | ||
47 | if (rwsem->writers < 0) { | ||
48 | nvgpu_spinlock_release(&rwsem->lock); | ||
49 | nvgpu_msleep(10); | ||
50 | continue; | ||
51 | } | ||
52 | |||
53 | /* | ||
54 | * Otherwise decrement the read counter and return. | ||
55 | */ | ||
56 | rwsem->readers -= 1; | ||
57 | nvgpu_spinlock_release(&rwsem->lock); | ||
58 | return; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * Release. | ||
64 | */ | ||
65 | void nvgpu_rwsem_up_read(struct nvgpu_rwsem *rwsem) | ||
66 | { | ||
67 | nvgpu_spinlock_acquire(&rwsem->lock); | ||
68 | rwsem->readers += 1; | ||
69 | |||
70 | /* | ||
71 | * Can't be any writers if there was a reader. Also can't be | ||
72 | * a positive number of readers. The increments are always | ||
73 | * downward so if we have a positive number then there is a | ||
74 | * balancing bug. | ||
75 | */ | ||
76 | BUG_ON(rwsem->writers < 0); | ||
77 | BUG_ON(rwsem->readers > 0); | ||
78 | |||
79 | nvgpu_spinlock_release(&rwsem->lock); | ||
80 | } | ||
81 | |||
82 | void nvgpu_rwsem_down_write(struct nvgpu_rwsem *rwsem) | ||
83 | { | ||
84 | while (true) { | ||
85 | nvgpu_spinlock_acquire(&rwsem->lock); | ||
86 | |||
87 | /* | ||
88 | * If there's a reader or a writer try again. Note: in this very | ||
89 | * simple implementation it's possible for readers to | ||
90 | * indefinitely starve writers. | ||
91 | */ | ||
92 | if (rwsem->writers < 0 || rwsem->readers < 0) { | ||
93 | nvgpu_spinlock_release(&rwsem->lock); | ||
94 | nvgpu_msleep(10); | ||
95 | continue; | ||
96 | } | ||
97 | |||
98 | rwsem->writers -= 1; | ||
99 | nvgpu_spinlock_release(&rwsem->lock); | ||
100 | return; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | void nvgpu_rwsem_up_write(struct nvgpu_rwsem *rwsem) | ||
105 | { | ||
106 | nvgpu_spinlock_acquire(&rwsem->lock); | ||
107 | rwsem->writers += 1; | ||
108 | |||
109 | /* | ||
110 | * Writers can't be positive: that would be an unbalanced free. Readers | ||
111 | * must be zero - otherwise this writer should never have had access! | ||
112 | */ | ||
113 | BUG_ON(rwsem->writers > 0); | ||
114 | BUG_ON(rwsem->readers != 0); | ||
115 | |||
116 | nvgpu_spinlock_release(&rwsem->lock); | ||
117 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/soc.c b/drivers/gpu/nvgpu/common/posix/soc.c new file mode 100644 index 00000000..0e5c7141 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/soc.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/soc.h> | ||
24 | |||
25 | bool nvgpu_platform_is_silicon(struct gk20a *g) | ||
26 | { | ||
27 | return false; | ||
28 | } | ||
29 | |||
30 | bool nvgpu_platform_is_simulation(struct gk20a *g) | ||
31 | { | ||
32 | return false; | ||
33 | } | ||
34 | |||
35 | bool nvgpu_platform_is_fpga(struct gk20a *g) | ||
36 | { | ||
37 | return false; | ||
38 | } | ||
39 | |||
40 | bool nvgpu_is_hypervisor_mode(struct gk20a *g) | ||
41 | { | ||
42 | return false; | ||
43 | } | ||
44 | |||
45 | bool nvgpu_is_bpmp_running(struct gk20a *g) | ||
46 | { | ||
47 | return false; | ||
48 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/stubs.c b/drivers/gpu/nvgpu/common/posix/stubs.c new file mode 100644 index 00000000..d6270692 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/stubs.c | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * Here lie OS stubs that do not have an implementation yet nor has any plans | ||
25 | * for an implementation. | ||
26 | */ | ||
27 | |||
28 | #include "gk20a/dbg_gpu_gk20a.h" | ||
29 | |||
30 | void nvgpu_dbg_session_post_event(struct dbg_session_gk20a *dbg_s) | ||
31 | { | ||
32 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/thread.c b/drivers/gpu/nvgpu/common/posix/thread.c new file mode 100644 index 00000000..d9476523 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/thread.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <nvgpu/bug.h> | ||
24 | #include <nvgpu/thread.h> | ||
25 | |||
26 | #include <nvgpu/posix/thread.h> | ||
27 | |||
28 | /** | ||
29 | * Use pthreads to mostly emulate the Linux kernel APIs. There are some things | ||
30 | * that are quite different - especially the stop/should_stop notions. In user | ||
31 | * space threads can send signals to one another but of course within the kernel | ||
32 | * that is not as simple. | ||
33 | * | ||
34 | * This could use some nice debugging some day as well. | ||
35 | */ | ||
36 | |||
37 | /* | ||
38 | * nvgpu thread functions return int. POSIX threads return void *. This little | ||
39 | * wrapper takes the int returning nvgpu thread and instead passes that int back | ||
40 | * through the void * pointer. | ||
41 | */ | ||
42 | static void *__nvgpu_posix_thread_wrapper(void *data) | ||
43 | { | ||
44 | struct nvgpu_posix_thread_data *nvgpu = data; | ||
45 | |||
46 | return ERR_PTR(nvgpu->fn(nvgpu->data)); | ||
47 | } | ||
48 | |||
49 | int nvgpu_thread_create(struct nvgpu_thread *thread, | ||
50 | void *data, | ||
51 | int (*threadfn)(void *data), const char *name) | ||
52 | { | ||
53 | int ret; | ||
54 | |||
55 | BUG_ON(thread->running); | ||
56 | |||
57 | memset(thread, 0, sizeof(*thread)); | ||
58 | |||
59 | /* | ||
60 | * By subtracting 1 the above memset ensures that we have a zero | ||
61 | * terminated string. | ||
62 | */ | ||
63 | strncpy(thread->tname, name, NVGPU_THREAD_POSIX_MAX_NAMELEN - 1); | ||
64 | |||
65 | thread->nvgpu.data = data; | ||
66 | thread->nvgpu.fn = threadfn; | ||
67 | |||
68 | ret = pthread_create(&thread->thread, NULL, | ||
69 | __nvgpu_posix_thread_wrapper, | ||
70 | &thread->nvgpu); | ||
71 | if (ret != 0) | ||
72 | return ret; | ||
73 | |||
74 | #ifdef _GNU_SOURCE | ||
75 | pthread_setname_np(thread->thread, thread->tname); | ||
76 | #endif | ||
77 | |||
78 | thread->running = true; | ||
79 | |||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | void nvgpu_thread_stop(struct nvgpu_thread *thread) | ||
84 | { | ||
85 | thread->should_stop = true; | ||
86 | } | ||
87 | |||
88 | bool nvgpu_thread_should_stop(struct nvgpu_thread *thread) | ||
89 | { | ||
90 | return thread->should_stop; | ||
91 | } | ||
92 | |||
93 | bool nvgpu_thread_is_running(struct nvgpu_thread *thread) | ||
94 | { | ||
95 | return thread->running; | ||
96 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/timers.c b/drivers/gpu/nvgpu/common/posix/timers.c new file mode 100644 index 00000000..c84b0de5 --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/timers.c | |||
@@ -0,0 +1,169 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <sys/time.h> | ||
24 | |||
25 | #include <nvgpu/bug.h> | ||
26 | #include <nvgpu/log.h> | ||
27 | #include <nvgpu/timers.h> | ||
28 | |||
29 | static s64 now(void) | ||
30 | { | ||
31 | return nvgpu_current_time_ms(); | ||
32 | } | ||
33 | |||
34 | /* | ||
35 | * Returns true if a > b; | ||
36 | */ | ||
37 | static bool time_after(s64 a, s64 b) | ||
38 | { | ||
39 | return a - b > 0; | ||
40 | } | ||
41 | |||
42 | int nvgpu_timeout_init(struct gk20a *g, struct nvgpu_timeout *timeout, | ||
43 | u32 duration, unsigned long flags) | ||
44 | { | ||
45 | if (flags & ~NVGPU_TIMER_FLAG_MASK) | ||
46 | return -EINVAL; | ||
47 | |||
48 | memset(timeout, 0, sizeof(*timeout)); | ||
49 | |||
50 | timeout->g = g; | ||
51 | timeout->flags = flags; | ||
52 | |||
53 | if (flags & NVGPU_TIMER_RETRY_TIMER) | ||
54 | timeout->retries.max = duration; | ||
55 | else | ||
56 | timeout->time = nvgpu_current_time_ms() + (s64)duration; | ||
57 | |||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | static int __nvgpu_timeout_expired_msg_cpu(struct nvgpu_timeout *timeout, | ||
62 | void *caller, | ||
63 | const char *fmt, va_list args) | ||
64 | { | ||
65 | struct gk20a *g = timeout->g; | ||
66 | |||
67 | if (time_after(now(), timeout->time)) { | ||
68 | if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) { | ||
69 | char buf[128]; | ||
70 | |||
71 | vsnprintf(buf, sizeof(buf), fmt, args); | ||
72 | |||
73 | nvgpu_err(g, "Timeout detected @ %p %s", caller, buf); | ||
74 | } | ||
75 | |||
76 | return -ETIMEDOUT; | ||
77 | } | ||
78 | |||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | static int __nvgpu_timeout_expired_msg_retry(struct nvgpu_timeout *timeout, | ||
83 | void *caller, | ||
84 | const char *fmt, va_list args) | ||
85 | { | ||
86 | struct gk20a *g = timeout->g; | ||
87 | |||
88 | if (timeout->retries.attempted >= timeout->retries.max) { | ||
89 | if (!(timeout->flags & NVGPU_TIMER_SILENT_TIMEOUT)) { | ||
90 | char buf[128]; | ||
91 | |||
92 | vsnprintf(buf, sizeof(buf), fmt, args); | ||
93 | |||
94 | nvgpu_err(g, "No more retries @ %p %s", caller, buf); | ||
95 | } | ||
96 | |||
97 | return -ETIMEDOUT; | ||
98 | } | ||
99 | |||
100 | timeout->retries.attempted++; | ||
101 | |||
102 | return 0; | ||
103 | } | ||
104 | |||
105 | int __nvgpu_timeout_expired_msg(struct nvgpu_timeout *timeout, | ||
106 | void *caller, const char *fmt, ...) | ||
107 | { | ||
108 | int ret; | ||
109 | va_list args; | ||
110 | |||
111 | va_start(args, fmt); | ||
112 | if (timeout->flags & NVGPU_TIMER_RETRY_TIMER) | ||
113 | ret = __nvgpu_timeout_expired_msg_retry(timeout, caller, fmt, | ||
114 | args); | ||
115 | else | ||
116 | ret = __nvgpu_timeout_expired_msg_cpu(timeout, caller, fmt, | ||
117 | args); | ||
118 | va_end(args); | ||
119 | |||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | int nvgpu_timeout_peek_expired(struct nvgpu_timeout *timeout) | ||
124 | { | ||
125 | if (timeout->flags & NVGPU_TIMER_RETRY_TIMER) | ||
126 | return timeout->retries.attempted >= timeout->retries.max; | ||
127 | else | ||
128 | return time_after(now(), timeout->time); | ||
129 | } | ||
130 | |||
131 | void nvgpu_udelay(unsigned int usecs) | ||
132 | { | ||
133 | BUG(); | ||
134 | } | ||
135 | |||
136 | void nvgpu_usleep_range(unsigned int min_us, unsigned int max_us) | ||
137 | { | ||
138 | BUG(); | ||
139 | } | ||
140 | |||
141 | void nvgpu_msleep(unsigned int msecs) | ||
142 | { | ||
143 | BUG(); | ||
144 | } | ||
145 | |||
146 | static inline s64 __nvgpu_current_time_us(void) | ||
147 | { | ||
148 | struct timeval now; | ||
149 | s64 time_now; | ||
150 | int ret; | ||
151 | |||
152 | ret = gettimeofday(&now, NULL); | ||
153 | if (ret != 0) | ||
154 | BUG(); | ||
155 | |||
156 | time_now = ((s64)now.tv_sec * (s64)1000000) + (s64)now.tv_usec; | ||
157 | |||
158 | return time_now; | ||
159 | } | ||
160 | |||
161 | s64 nvgpu_current_time_ms(void) | ||
162 | { | ||
163 | return __nvgpu_current_time_us() / (s64)1000; | ||
164 | } | ||
165 | |||
166 | u64 nvgpu_hr_timestamp(void) | ||
167 | { | ||
168 | return __nvgpu_current_time_us(); | ||
169 | } | ||
diff --git a/drivers/gpu/nvgpu/common/posix/tsg.c b/drivers/gpu/nvgpu/common/posix/tsg.c new file mode 100644 index 00000000..8736123d --- /dev/null +++ b/drivers/gpu/nvgpu/common/posix/tsg.c | |||
@@ -0,0 +1,28 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include "gk20a/tsg_gk20a.h" | ||
24 | |||
25 | void gk20a_tsg_event_id_post_event(struct tsg_gk20a *tsg, | ||
26 | int __event_id) | ||
27 | { | ||
28 | } | ||