1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <sync.h>
#include <sw_sync.h>
#include "msm_drv.h"
#include "sde_kms.h"
#include "sde_fence.h"
void *sde_sync_get(uint64_t fd)
{
/* force signed compare, fdget accepts an int argument */
return (signed int)fd >= 0 ? sync_fence_fdget(fd) : NULL;
}
void sde_sync_put(void *fence)
{
if (fence)
sync_fence_put(fence);
}
int sde_sync_wait(void *fence, long timeout_ms)
{
if (!fence)
return -EINVAL;
return sync_fence_wait(fence, timeout_ms);
}
uint32_t sde_sync_get_name_prefix(void *fence)
{
char *name;
uint32_t i, prefix;
if (!fence)
return 0x0;
name = ((struct sync_fence *)fence)->name;
prefix = 0x0;
for (i = 0; i < sizeof(uint32_t) && name[i]; ++i)
prefix = (prefix << CHAR_BIT) | name[i];
return prefix;
}
#if IS_ENABLED(CONFIG_SW_SYNC)
/**
* _sde_fence_create_fd - create fence object and return an fd for it
* This function is NOT thread-safe.
* @timeline: Timeline to associate with fence
* @name: Name for fence
* @val: Timeline value at which to signal the fence
* Return: File descriptor on success, or error code on error
*/
static int _sde_fence_create_fd(void *timeline, const char *name, uint32_t val)
{
struct sync_pt *sync_pt;
struct sync_fence *fence;
signed int fd = -EINVAL;
if (!timeline) {
SDE_ERROR("invalid timeline\n");
goto exit;
}
if (!name)
name = "sde_fence";
/* create sync point */
sync_pt = sw_sync_pt_create(timeline, val);
if (sync_pt == NULL) {
SDE_ERROR("failed to create sync point, %s\n", name);
goto exit;
}
/* create fence */
fence = sync_fence_create(name, sync_pt);
if (fence == NULL) {
sync_pt_free(sync_pt);
SDE_ERROR("couldn't create fence, %s\n", name);
goto exit;
}
/* create fd */
fd = get_unused_fd_flags(0);
if (fd < 0) {
SDE_ERROR("failed to get_unused_fd_flags(), %s\n", name);
sync_fence_put(fence);
goto exit;
}
sync_fence_install(fence, fd);
exit:
return fd;
}
/**
* SDE_FENCE_TIMELINE_NAME - macro for accessing s/w timeline's name
* @fence: Pointer to sde fence structure
* @drm_id: ID number of owning DRM Object
* Returns: Pointer to timeline name string
*/
#define SDE_FENCE_TIMELINE_NAME(fence) \
(((struct sw_sync_timeline *)fence->timeline)->obj.name)
int sde_fence_init(struct sde_fence *fence,
const char *name,
uint32_t drm_id)
{
if (!fence) {
SDE_ERROR("invalid argument(s)\n");
return -EINVAL;
}
fence->timeline = sw_sync_timeline_create(name ? name : "sde");
if (!fence->timeline) {
SDE_ERROR("failed to create timeline\n");
return -ENOMEM;
}
fence->commit_count = 0;
fence->done_count = 0;
fence->drm_id = drm_id;
mutex_init(&fence->fence_lock);
return 0;
}
void sde_fence_deinit(struct sde_fence *fence)
{
if (!fence) {
SDE_ERROR("invalid fence\n");
return;
}
mutex_destroy(&fence->fence_lock);
if (fence->timeline)
sync_timeline_destroy(fence->timeline);
}
int sde_fence_prepare(struct sde_fence *fence)
{
if (!fence) {
SDE_ERROR("invalid fence\n");
return -EINVAL;
}
mutex_lock(&fence->fence_lock);
++fence->commit_count;
SDE_EVT32(fence->drm_id, fence->commit_count, fence->done_count);
mutex_unlock(&fence->fence_lock);
return 0;
}
int sde_fence_create(struct sde_fence *fence, uint64_t *val, int offset)
{
uint32_t trigger_value;
int fd, rc = -EINVAL;
if (!fence || !fence->timeline || !val) {
SDE_ERROR("invalid argument(s), fence %pK, pval %pK\n",
fence, val);
} else {
/*
* Allow created fences to have a constant offset with respect
* to the timeline. This allows us to delay the fence signalling
* w.r.t. the commit completion (e.g., an offset of +1 would
* cause fences returned during a particular commit to signal
* after an additional delay of one commit, rather than at the
* end of the current one.
*/
mutex_lock(&fence->fence_lock);
trigger_value = fence->commit_count + (int32_t)offset;
fd = _sde_fence_create_fd(fence->timeline,
SDE_FENCE_TIMELINE_NAME(fence),
trigger_value);
*val = fd;
SDE_EVT32(fence->drm_id, trigger_value, fd);
mutex_unlock(&fence->fence_lock);
if (fd >= 0)
rc = 0;
}
return rc;
}
void sde_fence_signal(struct sde_fence *fence, bool is_error)
{
if (!fence || !fence->timeline) {
SDE_ERROR("invalid fence, %pK\n", fence);
return;
}
mutex_lock(&fence->fence_lock);
if ((fence->done_count - fence->commit_count) < 0)
++fence->done_count;
else
SDE_ERROR("detected extra signal attempt!\n");
/*
* Always advance 'done' counter,
* but only advance timeline if !error
*/
if (!is_error) {
int32_t val;
val = fence->done_count;
val -= ((struct sw_sync_timeline *)
fence->timeline)->value;
if (val < 0)
SDE_ERROR("invalid value\n");
else
sw_sync_timeline_inc(fence->timeline, (int)val);
}
SDE_EVT32(fence->drm_id, fence->done_count,
((struct sw_sync_timeline *) fence->timeline)->value);
mutex_unlock(&fence->fence_lock);
}
#endif
|