blob: 0e7e4a71fb2919a4af9e230310079dfa7aea964a [file] [log] [blame]
Jeff Boodyfe6c39c2012-08-09 13:54:50 -06001/* Copyright (c) 2012, 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/file.h>
15#include <linux/slab.h>
16#include <linux/uaccess.h>
17
18#include "kgsl_sync.h"
19
20struct sync_pt *kgsl_sync_pt_create(struct sync_timeline *timeline,
21 unsigned int timestamp)
22{
23 struct sync_pt *pt;
24 pt = sync_pt_create(timeline, (int) sizeof(struct kgsl_sync_pt));
25 if (pt) {
26 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
27 kpt->timestamp = timestamp;
28 }
29 return pt;
30}
31
32/*
33 * This should only be called on sync_pts which have been created but
34 * not added to a fence.
35 */
36void kgsl_sync_pt_destroy(struct sync_pt *pt)
37{
38 sync_pt_free(pt);
39}
40
41struct sync_pt *kgsl_sync_pt_dup(struct sync_pt *pt)
42{
43 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
44 return kgsl_sync_pt_create(pt->parent, kpt->timestamp);
45}
46
47int kgsl_sync_pt_has_signaled(struct sync_pt *pt)
48{
49 struct kgsl_sync_pt *kpt = (struct kgsl_sync_pt *) pt;
50 struct kgsl_sync_timeline *ktimeline =
51 (struct kgsl_sync_timeline *) pt->parent;
52 unsigned int ts = kpt->timestamp;
53 unsigned int last_ts = ktimeline->last_timestamp;
54 if (timestamp_cmp(last_ts, ts) >= 0) {
55 /* signaled */
56 return 1;
57 }
58 return 0;
59}
60
Jeff Boody0b120d02012-09-19 21:48:44 -060061static int kgsl_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
62{
63 struct kgsl_sync_pt *kpt_a = (struct kgsl_sync_pt *) a;
64 struct kgsl_sync_pt *kpt_b = (struct kgsl_sync_pt *) b;
65 unsigned int ts_a = kpt_a->timestamp;
66 unsigned int ts_b = kpt_b->timestamp;
67 return timestamp_cmp(ts_a, ts_b);
68}
69
Jeff Boodyfe6c39c2012-08-09 13:54:50 -060070struct kgsl_fence_event_priv {
71 struct kgsl_context *context;
72};
73
74/**
75 * kgsl_fence_event_cb - Event callback for a fence timestamp event
76 * @device - The KGSL device that expired the timestamp
77 * @priv - private data for the event
78 * @context_id - the context id that goes with the timestamp
79 * @timestamp - the timestamp that triggered the event
80 *
81 * Signal a fence following the expiration of a timestamp
82 */
83
84static inline void kgsl_fence_event_cb(struct kgsl_device *device,
85 void *priv, u32 context_id, u32 timestamp)
86{
87 struct kgsl_fence_event_priv *ev = priv;
88 kgsl_sync_timeline_signal(ev->context->timeline, timestamp);
89 kgsl_context_put(ev->context);
90 kfree(ev);
91}
92
93/**
94 * kgsl_add_fence_event - Create a new fence event
95 * @device - KGSL device to create the event on
96 * @timestamp - Timestamp to trigger the event
97 * @data - Return fence fd stored in struct kgsl_timestamp_event_fence
98 * @len - length of the fence event
99 * @owner - driver instance that owns this event
100 * @returns 0 on success or error code on error
101 *
102 * Create a fence and register an event to signal the fence when
103 * the timestamp expires
104 */
105
106int kgsl_add_fence_event(struct kgsl_device *device,
107 u32 context_id, u32 timestamp, void __user *data, int len,
108 struct kgsl_device_private *owner)
109{
110 struct kgsl_fence_event_priv *event;
111 struct kgsl_timestamp_event_fence priv;
112 struct kgsl_context *context;
113 struct sync_pt *pt;
114 struct sync_fence *fence = NULL;
115 int ret = -EINVAL;
116
117 if (len != sizeof(priv))
118 return -EINVAL;
119
120 context = kgsl_find_context(owner, context_id);
121 if (context == NULL)
122 return -EINVAL;
123
124 event = kzalloc(sizeof(*event), GFP_KERNEL);
125 if (event == NULL)
126 return -ENOMEM;
127 event->context = context;
128 kgsl_context_get(context);
129
130 pt = kgsl_sync_pt_create(context->timeline, timestamp);
131 if (pt == NULL) {
132 KGSL_DRV_ERR(device, "kgsl_sync_pt_create failed\n");
133 ret = -ENOMEM;
134 goto fail_pt;
135 }
136
137 fence = sync_fence_create("kgsl-fence", pt);
138 if (fence == NULL) {
139 /* only destroy pt when not added to fence */
140 kgsl_sync_pt_destroy(pt);
141 KGSL_DRV_ERR(device, "sync_fence_create failed\n");
142 ret = -ENOMEM;
143 goto fail_fence;
144 }
145
146 priv.fence_fd = get_unused_fd_flags(0);
147 if (priv.fence_fd < 0) {
148 KGSL_DRV_ERR(device, "invalid fence fd\n");
149 ret = -EINVAL;
150 goto fail_fd;
151 }
152 sync_fence_install(fence, priv.fence_fd);
153
154 if (copy_to_user(data, &priv, sizeof(priv))) {
155 ret = -EFAULT;
156 goto fail_copy_fd;
157 }
158
159 ret = kgsl_add_event(device, context_id, timestamp,
160 kgsl_fence_event_cb, event, owner);
161 if (ret)
162 goto fail_event;
163
164 return 0;
165
166fail_event:
167fail_copy_fd:
168 /* clean up sync_fence_install */
169 sync_fence_put(fence);
170 put_unused_fd(priv.fence_fd);
171fail_fd:
172 /* clean up sync_fence_create */
173 sync_fence_put(fence);
174fail_fence:
175fail_pt:
176 kgsl_context_put(context);
177 kfree(event);
178 return ret;
179}
180
181static const struct sync_timeline_ops kgsl_sync_timeline_ops = {
182 .dup = kgsl_sync_pt_dup,
183 .has_signaled = kgsl_sync_pt_has_signaled,
Jeff Boody0b120d02012-09-19 21:48:44 -0600184 .compare = kgsl_sync_pt_compare,
Jeff Boodyfe6c39c2012-08-09 13:54:50 -0600185};
186
187int kgsl_sync_timeline_create(struct kgsl_context *context)
188{
189 struct kgsl_sync_timeline *ktimeline;
190
191 context->timeline = sync_timeline_create(&kgsl_sync_timeline_ops,
192 (int) sizeof(struct kgsl_sync_timeline), "kgsl-timeline");
193 if (context->timeline == NULL)
194 return -EINVAL;
195
196 ktimeline = (struct kgsl_sync_timeline *) context->timeline;
197 ktimeline->last_timestamp = 0;
198
199 return 0;
200}
201
202void kgsl_sync_timeline_signal(struct sync_timeline *timeline,
203 unsigned int timestamp)
204{
205 struct kgsl_sync_timeline *ktimeline =
206 (struct kgsl_sync_timeline *) timeline;
207 ktimeline->last_timestamp = timestamp;
208 sync_timeline_signal(timeline);
209}
210
211void kgsl_sync_timeline_destroy(struct kgsl_context *context)
212{
213 sync_timeline_destroy(context->timeline);
214}