summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c')
-rw-r--r--drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c466
1 files changed, 0 insertions, 466 deletions
diff --git a/drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c b/drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c
deleted file mode 100644
index 2038e300..00000000
--- a/drivers/gpu/nvgpu/gk20a/semaphore_gk20a.c
+++ /dev/null
@@ -1,466 +0,0 @@
1/*
2 * drivers/video/tegra/host/gk20a/semaphore_gk20a.c
3 *
4 * GK20A Semaphores
5 *
6 * Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 */
17
18#define pr_fmt(fmt) "gpu_sema: " fmt
19
20#include <linux/dma-mapping.h>
21#include <linux/highmem.h>
22#include <linux/slab.h>
23
24#include <asm/pgtable.h>
25
26#include "gk20a.h"
27#include "mm_gk20a.h"
28#include "semaphore_gk20a.h"
29
30#define __lock_sema_sea(s) \
31 do { \
32 gpu_sema_verbose_dbg("Acquiring sema lock..."); \
33 mutex_lock(&s->sea_lock); \
34 gpu_sema_verbose_dbg("Sema lock aquried!"); \
35 } while (0)
36
37#define __unlock_sema_sea(s) \
38 do { \
39 mutex_unlock(&s->sea_lock); \
40 gpu_sema_verbose_dbg("Released sema lock"); \
41 } while (0)
42
43/*
44 * Return the sema_sea pointer.
45 */
46struct gk20a_semaphore_sea *gk20a_semaphore_get_sea(struct gk20a *g)
47{
48 return g->sema_sea;
49}
50
51static int __gk20a_semaphore_sea_grow(struct gk20a_semaphore_sea *sea)
52{
53 int ret = 0;
54 struct gk20a *gk20a = sea->gk20a;
55
56 __lock_sema_sea(sea);
57
58 ret = gk20a_gmmu_alloc_attr_sys(gk20a, DMA_ATTR_NO_KERNEL_MAPPING,
59 PAGE_SIZE * SEMAPHORE_POOL_COUNT,
60 &sea->sea_mem);
61 if (ret)
62 goto out;
63
64 sea->ro_sg_table = sea->sea_mem.sgt;
65 sea->size = SEMAPHORE_POOL_COUNT;
66 sea->map_size = SEMAPHORE_POOL_COUNT * PAGE_SIZE;
67
68out:
69 __unlock_sema_sea(sea);
70 return ret;
71}
72
73/*
74 * Create the semaphore sea. Only create it once - subsequent calls to this will
75 * return the originally created sea pointer.
76 */
77struct gk20a_semaphore_sea *gk20a_semaphore_sea_create(struct gk20a *g)
78{
79 if (g->sema_sea)
80 return g->sema_sea;
81
82 g->sema_sea = kzalloc(sizeof(*g->sema_sea), GFP_KERNEL);
83 if (!g->sema_sea)
84 return NULL;
85
86 g->sema_sea->size = 0;
87 g->sema_sea->page_count = 0;
88 g->sema_sea->gk20a = g;
89 INIT_LIST_HEAD(&g->sema_sea->pool_list);
90 mutex_init(&g->sema_sea->sea_lock);
91
92 if (__gk20a_semaphore_sea_grow(g->sema_sea))
93 goto cleanup;
94
95 gpu_sema_dbg("Created semaphore sea!");
96 return g->sema_sea;
97
98cleanup:
99 kfree(g->sema_sea);
100 g->sema_sea = NULL;
101 gpu_sema_dbg("Failed to creat semaphore sea!");
102 return NULL;
103}
104
105static int __semaphore_bitmap_alloc(unsigned long *bitmap, unsigned long len)
106{
107 unsigned long idx = find_first_zero_bit(bitmap, len);
108
109 if (idx == len)
110 return -ENOSPC;
111
112 set_bit(idx, bitmap);
113
114 return (int)idx;
115}
116
117/*
118 * Allocate a pool from the sea.
119 */
120struct gk20a_semaphore_pool *gk20a_semaphore_pool_alloc(
121 struct gk20a_semaphore_sea *sea)
122{
123 struct gk20a_semaphore_pool *p;
124 unsigned long page_idx;
125 int ret, err = 0;
126
127 p = kzalloc(sizeof(*p), GFP_KERNEL);
128 if (!p)
129 return ERR_PTR(-ENOMEM);
130
131 __lock_sema_sea(sea);
132
133 ret = __semaphore_bitmap_alloc(sea->pools_alloced, SEMAPHORE_POOL_COUNT);
134 if (ret < 0) {
135 err = ret;
136 goto fail;
137 }
138
139 page_idx = (unsigned long)ret;
140
141 p->page = sea->sea_mem.pages[page_idx];
142 p->ro_sg_table = sea->ro_sg_table;
143 p->page_idx = page_idx;
144 p->sema_sea = sea;
145 INIT_LIST_HEAD(&p->hw_semas);
146 kref_init(&p->ref);
147 mutex_init(&p->pool_lock);
148
149 sea->page_count++;
150 list_add(&p->pool_list_entry, &sea->pool_list);
151 __unlock_sema_sea(sea);
152
153 gpu_sema_dbg("Allocated semaphore pool: page-idx=%d", p->page_idx);
154
155 return p;
156
157fail:
158 __unlock_sema_sea(sea);
159 kfree(p);
160 gpu_sema_dbg("Failed to allocate semaphore pool!");
161 return ERR_PTR(err);
162}
163
164/*
165 * Map a pool into the passed vm's address space. This handles both the fixed
166 * global RO mapping and the non-fixed private RW mapping.
167 */
168int gk20a_semaphore_pool_map(struct gk20a_semaphore_pool *p,
169 struct vm_gk20a *vm)
170{
171 int ents, err = 0;
172 u64 addr;
173
174 gpu_sema_dbg("Mapping sempahore pool! (idx=%d)", p->page_idx);
175
176 p->cpu_va = vmap(&p->page, 1, 0,
177 pgprot_writecombine(PAGE_KERNEL));
178
179 gpu_sema_dbg(" %d: CPU VA = 0x%p!", p->page_idx, p->cpu_va);
180
181 /* First do the RW mapping. */
182 p->rw_sg_table = kzalloc(sizeof(*p->rw_sg_table), GFP_KERNEL);
183 if (!p->rw_sg_table)
184 return -ENOMEM;
185
186 err = sg_alloc_table_from_pages(p->rw_sg_table, &p->page, 1, 0,
187 PAGE_SIZE, GFP_KERNEL);
188 if (err) {
189 err = -ENOMEM;
190 goto fail;
191 }
192
193 /* Add IOMMU mapping... */
194 ents = dma_map_sg(dev_from_vm(vm), p->rw_sg_table->sgl, 1,
195 DMA_BIDIRECTIONAL);
196 if (ents != 1) {
197 err = -ENOMEM;
198 goto fail_free_sgt;
199 }
200
201 gpu_sema_dbg(" %d: DMA addr = 0x%pad", p->page_idx,
202 &sg_dma_address(p->rw_sg_table->sgl));
203
204 /* Map into the GPU... Doesn't need to be fixed. */
205 p->gpu_va = gk20a_gmmu_map(vm, &p->rw_sg_table, PAGE_SIZE,
206 0, gk20a_mem_flag_none, false,
207 APERTURE_SYSMEM);
208 if (!p->gpu_va) {
209 err = -ENOMEM;
210 goto fail_unmap_sgt;
211 }
212
213 gpu_sema_dbg(" %d: GPU read-write VA = 0x%llx", p->page_idx,
214 p->gpu_va);
215
216 /*
217 * And now the global mapping. Take the sea lock so that we don't race
218 * with a concurrent remap.
219 */
220 __lock_sema_sea(p->sema_sea);
221
222 BUG_ON(p->mapped);
223 addr = gk20a_gmmu_fixed_map(vm, &p->sema_sea->ro_sg_table,
224 p->sema_sea->gpu_va, p->sema_sea->map_size,
225 0,
226 gk20a_mem_flag_read_only,
227 false,
228 APERTURE_SYSMEM);
229 if (!addr) {
230 err = -ENOMEM;
231 BUG();
232 goto fail_unlock;
233 }
234 p->gpu_va_ro = addr;
235 p->mapped = 1;
236
237 gpu_sema_dbg(" %d: GPU read-only VA = 0x%llx", p->page_idx,
238 p->gpu_va_ro);
239
240 __unlock_sema_sea(p->sema_sea);
241
242 return 0;
243
244fail_unlock:
245 __unlock_sema_sea(p->sema_sea);
246fail_unmap_sgt:
247 dma_unmap_sg(dev_from_vm(vm), p->rw_sg_table->sgl, 1,
248 DMA_BIDIRECTIONAL);
249fail_free_sgt:
250 sg_free_table(p->rw_sg_table);
251fail:
252 kfree(p->rw_sg_table);
253 p->rw_sg_table = NULL;
254 gpu_sema_dbg(" %d: Failed to map semaphore pool!", p->page_idx);
255 return err;
256}
257
258/*
259 * Unmap a semaphore_pool.
260 */
261void gk20a_semaphore_pool_unmap(struct gk20a_semaphore_pool *p,
262 struct vm_gk20a *vm)
263{
264 struct gk20a_semaphore_int *hw_sema;
265
266 kunmap(p->cpu_va);
267
268 /* First the global RO mapping... */
269 __lock_sema_sea(p->sema_sea);
270 gk20a_gmmu_unmap(vm, p->gpu_va_ro,
271 p->sema_sea->map_size, gk20a_mem_flag_none);
272 p->ro_sg_table = NULL;
273 __unlock_sema_sea(p->sema_sea);
274
275 /* And now the private RW mapping. */
276 gk20a_gmmu_unmap(vm, p->gpu_va, PAGE_SIZE, gk20a_mem_flag_none);
277 p->gpu_va = 0;
278
279 dma_unmap_sg(dev_from_vm(vm), p->rw_sg_table->sgl, 1,
280 DMA_BIDIRECTIONAL);
281
282 sg_free_table(p->rw_sg_table);
283 kfree(p->rw_sg_table);
284 p->rw_sg_table = NULL;
285
286 list_for_each_entry(hw_sema, &p->hw_semas, hw_sema_list)
287 /*
288 * Make sure the mem addresses are all NULL so if this gets
289 * reused we will fault.
290 */
291 hw_sema->value = NULL;
292
293 gpu_sema_dbg("Unmapped semaphore pool! (idx=%d)", p->page_idx);
294}
295
296/*
297 * Completely free a sempahore_pool. You should make sure this pool is not
298 * mapped otherwise there's going to be a memory leak.
299 */
300static void gk20a_semaphore_pool_free(struct kref *ref)
301{
302 struct gk20a_semaphore_pool *p =
303 container_of(ref, struct gk20a_semaphore_pool, ref);
304 struct gk20a_semaphore_sea *s = p->sema_sea;
305 struct gk20a_semaphore_int *hw_sema, *tmp;
306
307 WARN_ON(p->gpu_va || p->rw_sg_table || p->ro_sg_table);
308
309 __lock_sema_sea(s);
310 list_del(&p->pool_list_entry);
311 clear_bit(p->page_idx, s->pools_alloced);
312 s->page_count--;
313 __unlock_sema_sea(s);
314
315 list_for_each_entry_safe(hw_sema, tmp, &p->hw_semas, hw_sema_list)
316 kfree(hw_sema);
317
318 gpu_sema_dbg("Freed semaphore pool! (idx=%d)", p->page_idx);
319 kfree(p);
320}
321
322void gk20a_semaphore_pool_get(struct gk20a_semaphore_pool *p)
323{
324 kref_get(&p->ref);
325}
326
327void gk20a_semaphore_pool_put(struct gk20a_semaphore_pool *p)
328{
329 kref_put(&p->ref, gk20a_semaphore_pool_free);
330}
331
332/*
333 * Get the address for a semaphore_pool - if global is true then return the
334 * global RO address instead of the RW address owned by the semaphore's VM.
335 */
336u64 __gk20a_semaphore_pool_gpu_va(struct gk20a_semaphore_pool *p, bool global)
337{
338 if (!global)
339 return p->gpu_va;
340
341 return p->gpu_va_ro + (PAGE_SIZE * p->page_idx);
342}
343
344static int __gk20a_init_hw_sema(struct channel_gk20a *ch)
345{
346 int hw_sema_idx;
347 int ret = 0;
348 struct gk20a_semaphore_int *hw_sema;
349 struct gk20a_semaphore_pool *p = ch->vm->sema_pool;
350
351 BUG_ON(!p);
352
353 mutex_lock(&p->pool_lock);
354
355 /* Find an available HW semaphore. */
356 hw_sema_idx = __semaphore_bitmap_alloc(p->semas_alloced,
357 PAGE_SIZE / SEMAPHORE_SIZE);
358 if (hw_sema_idx < 0) {
359 ret = hw_sema_idx;
360 goto fail;
361 }
362
363 hw_sema = kzalloc(sizeof(struct gk20a_semaphore_int), GFP_KERNEL);
364 if (!hw_sema) {
365 ret = -ENOMEM;
366 goto fail_free_idx;
367 }
368
369 ch->hw_sema = hw_sema;
370 hw_sema->ch = ch;
371 hw_sema->p = p;
372 hw_sema->idx = hw_sema_idx;
373 hw_sema->offset = SEMAPHORE_SIZE * hw_sema_idx;
374 atomic_set(&hw_sema->next_value, 0);
375 hw_sema->value = p->cpu_va + hw_sema->offset;
376 writel(0, hw_sema->value);
377
378 list_add(&hw_sema->hw_sema_list, &p->hw_semas);
379
380 mutex_unlock(&p->pool_lock);
381
382 return 0;
383
384fail_free_idx:
385 clear_bit(hw_sema_idx, p->semas_alloced);
386fail:
387 mutex_unlock(&p->pool_lock);
388 return ret;
389}
390
391/*
392 * Free the channel used semaphore index
393 */
394void gk20a_semaphore_free_hw_sema(struct channel_gk20a *ch)
395{
396 struct gk20a_semaphore_pool *p = ch->vm->sema_pool;
397
398 BUG_ON(!p);
399
400 mutex_lock(&p->pool_lock);
401
402 clear_bit(ch->hw_sema->idx, p->semas_alloced);
403
404 /* Make sure that when the ch is re-opened it will get a new HW sema. */
405 list_del(&ch->hw_sema->hw_sema_list);
406 kfree(ch->hw_sema);
407 ch->hw_sema = NULL;
408
409 mutex_unlock(&p->pool_lock);
410}
411
412/*
413 * Allocate a semaphore from the passed pool.
414 *
415 * Since semaphores are ref-counted there's no explicit free for external code
416 * to use. When the ref-count hits 0 the internal free will happen.
417 */
418struct gk20a_semaphore *gk20a_semaphore_alloc(struct channel_gk20a *ch)
419{
420 struct gk20a_semaphore *s;
421 int ret;
422
423 if (!ch->hw_sema) {
424 ret = __gk20a_init_hw_sema(ch);
425 if (ret)
426 return NULL;
427 }
428
429 s = kzalloc(sizeof(*s), GFP_KERNEL);
430 if (!s)
431 return NULL;
432
433 kref_init(&s->ref);
434 s->hw_sema = ch->hw_sema;
435 atomic_set(&s->value, 0);
436
437 /*
438 * Take a ref on the pool so that we can keep this pool alive for
439 * as long as this semaphore is alive.
440 */
441 gk20a_semaphore_pool_get(s->hw_sema->p);
442
443 gpu_sema_dbg("Allocated semaphore (c=%d)", ch->hw_chid);
444
445 return s;
446}
447
448static void gk20a_semaphore_free(struct kref *ref)
449{
450 struct gk20a_semaphore *s =
451 container_of(ref, struct gk20a_semaphore, ref);
452
453 gk20a_semaphore_pool_put(s->hw_sema->p);
454
455 kfree(s);
456}
457
458void gk20a_semaphore_put(struct gk20a_semaphore *s)
459{
460 kref_put(&s->ref, gk20a_semaphore_free);
461}
462
463void gk20a_semaphore_get(struct gk20a_semaphore *s)
464{
465 kref_get(&s->ref);
466}