1 /* $NetBSD: radeon_semaphore.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $ */
2
3 /*
4 * Copyright 2011 Christian König.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 *
27 */
28 /*
29 * Authors:
30 * Christian König <deathsimple@vodafone.de>
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: radeon_semaphore.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
35
36 #include "radeon.h"
37 #include "radeon_trace.h"
38
radeon_semaphore_create(struct radeon_device * rdev,struct radeon_semaphore ** semaphore)39 int radeon_semaphore_create(struct radeon_device *rdev,
40 struct radeon_semaphore **semaphore)
41 {
42 int r;
43
44 *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
45 if (*semaphore == NULL) {
46 return -ENOMEM;
47 }
48 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
49 &(*semaphore)->sa_bo, 8, 8);
50 if (r) {
51 kfree(*semaphore);
52 *semaphore = NULL;
53 return r;
54 }
55 (*semaphore)->waiters = 0;
56 (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
57
58 *((uint64_t *)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
59
60 return 0;
61 }
62
radeon_semaphore_emit_signal(struct radeon_device * rdev,int ridx,struct radeon_semaphore * semaphore)63 bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
64 struct radeon_semaphore *semaphore)
65 {
66 struct radeon_ring *ring = &rdev->ring[ridx];
67
68 trace_radeon_semaphore_signale(ridx, semaphore);
69
70 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
71 --semaphore->waiters;
72
73 /* for debugging lockup only, used by sysfs debug files */
74 ring->last_semaphore_signal_addr = semaphore->gpu_addr;
75 return true;
76 }
77 return false;
78 }
79
radeon_semaphore_emit_wait(struct radeon_device * rdev,int ridx,struct radeon_semaphore * semaphore)80 bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
81 struct radeon_semaphore *semaphore)
82 {
83 struct radeon_ring *ring = &rdev->ring[ridx];
84
85 trace_radeon_semaphore_wait(ridx, semaphore);
86
87 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
88 ++semaphore->waiters;
89
90 /* for debugging lockup only, used by sysfs debug files */
91 ring->last_semaphore_wait_addr = semaphore->gpu_addr;
92 return true;
93 }
94 return false;
95 }
96
radeon_semaphore_free(struct radeon_device * rdev,struct radeon_semaphore ** semaphore,struct radeon_fence * fence)97 void radeon_semaphore_free(struct radeon_device *rdev,
98 struct radeon_semaphore **semaphore,
99 struct radeon_fence *fence)
100 {
101 if (semaphore == NULL || *semaphore == NULL) {
102 return;
103 }
104 if ((*semaphore)->waiters > 0) {
105 dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
106 " hardware lockup imminent!\n", *semaphore);
107 }
108 radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
109 kfree(*semaphore);
110 *semaphore = NULL;
111 }
112