blob: 025d4cdb9a734b279f49c574fa71fa6a6c4413be [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
61struct kgsl_fence_event_priv {
62 struct kgsl_context *context;
63};
64
65/**
66 * kgsl_fence_event_cb - Event callback for a fence timestamp event
67 * @device - The KGSL device that expired the timestamp
68 * @priv - private data for the event
69 * @context_id - the context id that goes with the timestamp
70 * @timestamp - the timestamp that triggered the event
71 *
72 * Signal a fence following the expiration of a timestamp
73 */
74
75static inline void kgsl_fence_event_cb(struct kgsl_device *device,
76 void *priv, u32 context_id, u32 timestamp)
77{
78 struct kgsl_fence_event_priv *ev = priv;
79 kgsl_sync_timeline_signal(ev->context->timeline, timestamp);
80 kgsl_context_put(ev->context);
81 kfree(ev);
82}
83
84/**
85 * kgsl_add_fence_event - Create a new fence event
86 * @device - KGSL device to create the event on
87 * @timestamp - Timestamp to trigger the event
88 * @data - Return fence fd stored in struct kgsl_timestamp_event_fence
89 * @len - length of the fence event
90 * @owner - driver instance that owns this event
91 * @returns 0 on success or error code on error
92 *
93 * Create a fence and register an event to signal the fence when
94 * the timestamp expires
95 */
96
97int kgsl_add_fence_event(struct kgsl_device *device,
98 u32 context_id, u32 timestamp, void __user *data, int len,
99 struct kgsl_device_private *owner)
100{
101 struct kgsl_fence_event_priv *event;
102 struct kgsl_timestamp_event_fence priv;
103 struct kgsl_context *context;
104 struct sync_pt *pt;
105 struct sync_fence *fence = NULL;
106 int ret = -EINVAL;
107
108 if (len != sizeof(priv))
109 return -EINVAL;
110
111 context = kgsl_find_context(owner, context_id);
112 if (context == NULL)
113 return -EINVAL;
114
115 event = kzalloc(sizeof(*event), GFP_KERNEL);
116 if (event == NULL)
117 return -ENOMEM;
118 event->context = context;
119 kgsl_context_get(context);
120
121 pt = kgsl_sync_pt_create(context->timeline, timestamp);
122 if (pt == NULL) {
123 KGSL_DRV_ERR(device, "kgsl_sync_pt_create failed\n");
124 ret = -ENOMEM;
125 goto fail_pt;
126 }
127
128 fence = sync_fence_create("kgsl-fence", pt);
129 if (fence == NULL) {
130 /* only destroy pt when not added to fence */
131 kgsl_sync_pt_destroy(pt);
132 KGSL_DRV_ERR(device, "sync_fence_create failed\n");
133 ret = -ENOMEM;
134 goto fail_fence;
135 }
136
137 priv.fence_fd = get_unused_fd_flags(0);
138 if (priv.fence_fd < 0) {
139 KGSL_DRV_ERR(device, "invalid fence fd\n");
140 ret = -EINVAL;
141 goto fail_fd;
142 }
143 sync_fence_install(fence, priv.fence_fd);
144
145 if (copy_to_user(data, &priv, sizeof(priv))) {
146 ret = -EFAULT;
147 goto fail_copy_fd;
148 }
149
150 ret = kgsl_add_event(device, context_id, timestamp,
151 kgsl_fence_event_cb, event, owner);
152 if (ret)
153 goto fail_event;
154
155 return 0;
156
157fail_event:
158fail_copy_fd:
159 /* clean up sync_fence_install */
160 sync_fence_put(fence);
161 put_unused_fd(priv.fence_fd);
162fail_fd:
163 /* clean up sync_fence_create */
164 sync_fence_put(fence);
165fail_fence:
166fail_pt:
167 kgsl_context_put(context);
168 kfree(event);
169 return ret;
170}
171
172static const struct sync_timeline_ops kgsl_sync_timeline_ops = {
173 .dup = kgsl_sync_pt_dup,
174 .has_signaled = kgsl_sync_pt_has_signaled,
175};
176
177int kgsl_sync_timeline_create(struct kgsl_context *context)
178{
179 struct kgsl_sync_timeline *ktimeline;
180
181 context->timeline = sync_timeline_create(&kgsl_sync_timeline_ops,
182 (int) sizeof(struct kgsl_sync_timeline), "kgsl-timeline");
183 if (context->timeline == NULL)
184 return -EINVAL;
185
186 ktimeline = (struct kgsl_sync_timeline *) context->timeline;
187 ktimeline->last_timestamp = 0;
188
189 return 0;
190}
191
192void kgsl_sync_timeline_signal(struct sync_timeline *timeline,
193 unsigned int timestamp)
194{
195 struct kgsl_sync_timeline *ktimeline =
196 (struct kgsl_sync_timeline *) timeline;
197 ktimeline->last_timestamp = timestamp;
198 sync_timeline_signal(timeline);
199}
200
201void kgsl_sync_timeline_destroy(struct kgsl_context *context)
202{
203 sync_timeline_destroy(context->timeline);
204}