2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include "fence_gk20a.h"
16 #include <linux/gk20a.h>
17 #include <linux/file.h>
20 #include "semaphore_gk20a.h"
21 #include "channel_gk20a.h"
22 #include "sync_gk20a.h"
25 #include "../../../staging/android/sync.h"
28 #ifdef CONFIG_TEGRA_GK20A
29 #include <linux/nvhost.h>
30 #include <linux/nvhost_ioctl.h>
33 struct gk20a_fence_ops {
34 int (*wait)(struct gk20a_fence *, int timeout);
35 bool (*is_expired)(struct gk20a_fence *);
36 void *(*free)(struct kref *);
39 static void gk20a_fence_free(struct kref *ref)
41 struct gk20a_fence *f =
42 container_of(ref, struct gk20a_fence, ref);
45 sync_fence_put(f->sync_fence);
48 gk20a_semaphore_put(f->semaphore);
52 void gk20a_fence_put(struct gk20a_fence *f)
55 kref_put(&f->ref, gk20a_fence_free);
58 struct gk20a_fence *gk20a_fence_get(struct gk20a_fence *f)
65 int gk20a_fence_wait(struct gk20a_fence *f, int timeout)
67 return f->ops->wait(f, timeout);
70 bool gk20a_fence_is_expired(struct gk20a_fence *f)
72 return f->ops->is_expired(f);
75 int gk20a_fence_install_fd(struct gk20a_fence *f)
87 sync_fence_get(f->sync_fence);
88 sync_fence_install(f->sync_fence, fd);
95 static struct gk20a_fence *alloc_fence(const struct gk20a_fence_ops *ops,
96 struct sync_fence *sync_fence, bool wfi)
98 struct gk20a_fence *f = kzalloc(sizeof(*f), GFP_KERNEL);
103 f->sync_fence = sync_fence;
109 /* Fences that are backed by GPU semaphores: */
111 static int gk20a_semaphore_fence_wait(struct gk20a_fence *f, int timeout)
115 if (!gk20a_semaphore_is_acquired(f->semaphore))
118 remain = wait_event_interruptible_timeout(
120 !gk20a_semaphore_is_acquired(f->semaphore),
122 if (remain == 0 && gk20a_semaphore_is_acquired(f->semaphore))
129 static bool gk20a_semaphore_fence_is_expired(struct gk20a_fence *f)
131 return !gk20a_semaphore_is_acquired(f->semaphore);
134 static const struct gk20a_fence_ops gk20a_semaphore_fence_ops = {
135 .wait = &gk20a_semaphore_fence_wait,
136 .is_expired = &gk20a_semaphore_fence_is_expired,
139 /* This function takes ownership of the semaphore */
140 struct gk20a_fence *gk20a_fence_from_semaphore(
141 struct sync_timeline *timeline,
142 struct gk20a_semaphore *semaphore,
143 wait_queue_head_t *semaphore_wq,
144 struct sync_fence *dependency,
147 struct gk20a_fence *f;
148 struct sync_fence *sync_fence = NULL;
151 sync_fence = gk20a_sync_fence_create(timeline, semaphore,
152 dependency, "fence");
157 f = alloc_fence(&gk20a_semaphore_fence_ops, sync_fence, wfi);
160 sync_fence_put(sync_fence);
165 f->semaphore = semaphore;
166 f->semaphore_wq = semaphore_wq;
170 #ifdef CONFIG_TEGRA_GK20A
171 /* Fences that are backed by host1x syncpoints: */
173 static int gk20a_syncpt_fence_wait(struct gk20a_fence *f, int timeout)
175 return nvhost_syncpt_wait_timeout_ext(
176 f->host1x_pdev, f->syncpt_id, f->syncpt_value,
177 timeout, NULL, NULL);
180 static bool gk20a_syncpt_fence_is_expired(struct gk20a_fence *f)
182 return nvhost_syncpt_is_expired_ext(f->host1x_pdev, f->syncpt_id,
186 static const struct gk20a_fence_ops gk20a_syncpt_fence_ops = {
187 .wait = &gk20a_syncpt_fence_wait,
188 .is_expired = &gk20a_syncpt_fence_is_expired,
191 struct gk20a_fence *gk20a_fence_from_syncpt(struct platform_device *host1x_pdev,
192 u32 id, u32 value, bool wfi)
194 struct gk20a_fence *f;
195 struct sync_fence *sync_fence = NULL;
198 struct nvhost_ctrl_sync_fence_info pt = {
203 sync_fence = nvhost_sync_create_fence(host1x_pdev, &pt, 1,
205 if (IS_ERR(sync_fence))
209 f = alloc_fence(&gk20a_syncpt_fence_ops, sync_fence, wfi);
212 sync_fence_put(sync_fence);
216 f->host1x_pdev = host1x_pdev;
218 f->syncpt_value = value;
222 struct gk20a_fence *gk20a_fence_from_syncpt(struct platform_device *host1x_pdev,
223 u32 id, u32 value, bool wfi)