blob: 0e7606e842c34a4bf928946687c774b3f03017d3 [file] [log] [blame]
Steve Kondikf7652b32013-11-26 15:20:51 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/err.h>
15#include <linux/file.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19
20#include <asm/current.h>
21
22#include "kgsl_sync.h"
23
24struct sync_pt *kgsl_sync_pt_create(struct sync_timeline *timeline,
25 unsigned int timestamp)
26{
27 struct sync_pt *pt;
28 pt = sync_pt_create(timeline, (int) sizeof(struct kgsl_sync_pt));
29 if (pt) {
30 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
31 kpt->timestamp = timestamp;
32 }
33 return pt;
34}
35
36/*
37 * This should only be called on sync_pts which have been created but
38 * not added to a fence.
39 */
40void kgsl_sync_pt_destroy(struct sync_pt *pt)
41{
42 sync_pt_free(pt);
43}
44
45static struct sync_pt *kgsl_sync_pt_dup(struct sync_pt *pt)
46{
47 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
48 return kgsl_sync_pt_create(pt->parent, kpt->timestamp);
49}
50
51static int kgsl_sync_pt_has_signaled(struct sync_pt *pt)
52{
53 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
54 struct kgsl_sync_timeline *ktimeline =
55 (struct kgsl_sync_timeline *) pt->parent;
56 unsigned int ts = kpt->timestamp;
57 unsigned int last_ts = ktimeline->last_timestamp;
58 if (timestamp_cmp(last_ts, ts) >= 0) {
59 /* signaled */
60 return 1;
61 }
62 return 0;
63}
64
65static int kgsl_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
66{
67 struct kgsl_sync_pt *kpt_a = (struct kgsl_sync_pt *) a;
68 struct kgsl_sync_pt *kpt_b = (struct kgsl_sync_pt *) b;
69 unsigned int ts_a = kpt_a->timestamp;
70 unsigned int ts_b = kpt_b->timestamp;
71 return timestamp_cmp(ts_a, ts_b);
72}
73
74struct kgsl_fence_event_priv {
75 struct kgsl_context *context;
76 unsigned int timestamp;
77};
78
79/**
80 * kgsl_fence_event_cb - Event callback for a fence timestamp event
81 * @device - The KGSL device that expired the timestamp
82 * @priv - private data for the event
83 * @context_id - the context id that goes with the timestamp
84 * @timestamp - the timestamp that triggered the event
85 *
86 * Signal a fence following the expiration of a timestamp
87 */
88
89static inline void kgsl_fence_event_cb(struct kgsl_device *device,
90 void *priv, u32 context_id, u32 timestamp, u32 type)
91{
92 struct kgsl_fence_event_priv *ev = priv;
93
94 /* Signal time timeline for every event type */
95 kgsl_sync_timeline_signal(ev->context->timeline, timestamp);
96 kgsl_context_put(ev->context);
97 kfree(ev);
98}
99
100/**
101 * kgsl_add_fence_event - Create a new fence event
102 * @device - KGSL device to create the event on
103 * @timestamp - Timestamp to trigger the event
104 * @data - Return fence fd stored in struct kgsl_timestamp_event_fence
105 * @len - length of the fence event
106 * @owner - driver instance that owns this event
107 * @returns 0 on success or error code on error
108 *
109 * Create a fence and register an event to signal the fence when
110 * the timestamp expires
111 */
112
113int kgsl_add_fence_event(struct kgsl_device *device,
114 u32 context_id, u32 timestamp, void __user *data, int len,
115 struct kgsl_device_private *owner)
116{
117 struct kgsl_fence_event_priv *event;
118 struct kgsl_timestamp_event_fence priv;
119 struct kgsl_context *context;
120 struct sync_pt *pt;
121 struct sync_fence *fence = NULL;
122 int ret = -EINVAL;
123
124 if (len != sizeof(priv))
125 return -EINVAL;
126
127 event = kzalloc(sizeof(*event), GFP_KERNEL);
128 if (event == NULL)
129 return -ENOMEM;
130
131 context = kgsl_context_get_owner(owner, context_id);
132
133 if (context == NULL) {
134 kfree(event);
135 return -EINVAL;
136 }
137
138 event->context = context;
139 event->timestamp = timestamp;
140
141 pt = kgsl_sync_pt_create(context->timeline, timestamp);
142 if (pt == NULL) {
143 KGSL_DRV_ERR(device, "kgsl_sync_pt_create failed\n");
144 ret = -ENOMEM;
145 goto fail_pt;
146 }
147
148 fence = sync_fence_create("kgsl-fence", pt);
149 if (fence == NULL) {
150 /* only destroy pt when not added to fence */
151 kgsl_sync_pt_destroy(pt);
152 KGSL_DRV_ERR(device, "sync_fence_create failed\n");
153 ret = -ENOMEM;
154 goto fail_fence;
155 }
156
157 priv.fence_fd = get_unused_fd_flags(0);
158 if (priv.fence_fd < 0) {
159 KGSL_DRV_ERR(device, "invalid fence fd\n");
160 ret = -EINVAL;
161 goto fail_fd;
162 }
163 sync_fence_install(fence, priv.fence_fd);
164
165 if (copy_to_user(data, &priv, sizeof(priv))) {
166 ret = -EFAULT;
167 goto fail_copy_fd;
168 }
169
170 /*
171 * Hold the context ref-count for the event - it will get released in
172 * the callback
173 */
174 ret = kgsl_add_event(device, context_id, timestamp,
175 kgsl_fence_event_cb, event, owner);
176 if (ret)
177 goto fail_event;
178
179 return 0;
180
181fail_event:
182fail_copy_fd:
183 /* clean up sync_fence_install */
184 put_unused_fd(priv.fence_fd);
185fail_fd:
186 /* clean up sync_fence_create */
187 sync_fence_put(fence);
188fail_fence:
189fail_pt:
190 kgsl_context_put(context);
191 kfree(event);
192 return ret;
193}
194
195static unsigned int kgsl_sync_get_timestamp(
196 struct kgsl_sync_timeline *ktimeline, enum kgsl_timestamp_type type)
197{
198 struct kgsl_context *context = idr_find(&ktimeline->device->context_idr,
199 ktimeline->context_id);
200 if (context == NULL)
201 return 0;
202
203 return kgsl_readtimestamp(ktimeline->device, context, type);
204}
205
206static void kgsl_sync_timeline_value_str(struct sync_timeline *sync_timeline,
207 char *str, int size)
208{
209 struct kgsl_sync_timeline *ktimeline =
210 (struct kgsl_sync_timeline *) sync_timeline;
211 unsigned int timestamp_retired = kgsl_sync_get_timestamp(ktimeline,
212 KGSL_TIMESTAMP_RETIRED);
213 snprintf(str, size, "%u retired:%u", ktimeline->last_timestamp,
214 timestamp_retired);
215}
216
217static void kgsl_sync_pt_value_str(struct sync_pt *sync_pt,
218 char *str, int size)
219{
220 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) sync_pt;
221 snprintf(str, size, "%u", kpt->timestamp);
222}
223
224static void kgsl_sync_timeline_release_obj(struct sync_timeline *sync_timeline)
225{
226 /*
227 * Make sure to free the timeline only after destroy flag is set.
228 * This is to avoid further accessing to the timeline from KGSL and
229 * also to catch any unbalanced kref of timeline.
230 */
231 BUG_ON(sync_timeline && (sync_timeline->destroyed != true));
232}
233static const struct sync_timeline_ops kgsl_sync_timeline_ops = {
234 .driver_name = "kgsl-timeline",
235 .dup = kgsl_sync_pt_dup,
236 .has_signaled = kgsl_sync_pt_has_signaled,
237 .compare = kgsl_sync_pt_compare,
238 .timeline_value_str = kgsl_sync_timeline_value_str,
239 .pt_value_str = kgsl_sync_pt_value_str,
240 .release_obj = kgsl_sync_timeline_release_obj,
241};
242
243int kgsl_sync_timeline_create(struct kgsl_context *context)
244{
245 struct kgsl_sync_timeline *ktimeline;
246
247 /* Generate a name which includes the thread name, thread id, process
248 * name, process id, and context id. This makes it possible to
249 * identify the context of a timeline in the sync dump. */
250 char ktimeline_name[sizeof(context->timeline->name)] = {};
251 snprintf(ktimeline_name, sizeof(ktimeline_name),
252 "%s_%.15s(%d)-%.15s(%d)-%d",
253 context->device->name,
254 current->group_leader->comm, current->group_leader->pid,
255 current->comm, current->pid, context->id);
256
257 context->timeline = sync_timeline_create(&kgsl_sync_timeline_ops,
258 (int) sizeof(struct kgsl_sync_timeline), ktimeline_name);
259 if (context->timeline == NULL)
260 return -EINVAL;
261
262 ktimeline = (struct kgsl_sync_timeline *) context->timeline;
263 ktimeline->last_timestamp = 0;
264 ktimeline->device = context->dev_priv->device;
265 ktimeline->context_id = context->id;
266
267 return 0;
268}
269
270void kgsl_sync_timeline_signal(struct sync_timeline *timeline,
271 unsigned int timestamp)
272{
273 struct kgsl_sync_timeline *ktimeline =
274 (struct kgsl_sync_timeline *) timeline;
275
276 if (timestamp_cmp(timestamp, ktimeline->last_timestamp) > 0)
277 ktimeline->last_timestamp = timestamp;
278 sync_timeline_signal(timeline);
279}
280
281void kgsl_sync_timeline_destroy(struct kgsl_context *context)
282{
283 sync_timeline_destroy(context->timeline);
284}
285
286static void kgsl_sync_callback(struct sync_fence *fence,
287 struct sync_fence_waiter *waiter)
288{
289 struct kgsl_sync_fence_waiter *kwaiter =
290 (struct kgsl_sync_fence_waiter *) waiter;
291 kwaiter->func(kwaiter->priv);
292 sync_fence_put(kwaiter->fence);
293 kfree(kwaiter);
294}
295
296struct kgsl_sync_fence_waiter *kgsl_sync_fence_async_wait(int fd,
297 void (*func)(void *priv), void *priv)
298{
299 struct kgsl_sync_fence_waiter *kwaiter;
300 struct sync_fence *fence;
301 int status;
302
303 fence = sync_fence_fdget(fd);
304 if (fence == NULL)
305 return ERR_PTR(-EINVAL);
306
307 /* create the waiter */
308 kwaiter = kzalloc(sizeof(*kwaiter), GFP_KERNEL);
309 if (kwaiter == NULL) {
310 sync_fence_put(fence);
311 return ERR_PTR(-ENOMEM);
312 }
313 kwaiter->fence = fence;
314 kwaiter->priv = priv;
315 kwaiter->func = func;
316 sync_fence_waiter_init((struct sync_fence_waiter *) kwaiter,
317 kgsl_sync_callback);
318
319 /* if status then error or signaled */
320 status = sync_fence_wait_async(fence,
321 (struct sync_fence_waiter *) kwaiter);
322 if (status) {
323 kfree(kwaiter);
324 sync_fence_put(fence);
325 if (status < 0)
326 kwaiter = ERR_PTR(status);
327 else
328 kwaiter = NULL;
329 }
330
331 return kwaiter;
332}
333
334int kgsl_sync_fence_async_cancel(struct kgsl_sync_fence_waiter *kwaiter)
335{
336 if (kwaiter == NULL)
337 return 0;
338
339 if(sync_fence_cancel_async(kwaiter->fence,
340 (struct sync_fence_waiter *) kwaiter) == 0) {
341 sync_fence_put(kwaiter->fence);
342 kfree(kwaiter);
343 return 1;
344 }
345 return 0;
346}