Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012-2017, 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 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 24 | static void kgsl_sync_timeline_signal(struct kgsl_sync_timeline *timeline, |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 25 | unsigned int timestamp); |
| 26 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 27 | static const struct fence_ops kgsl_sync_fence_ops; |
| 28 | |
| 29 | static struct kgsl_sync_fence *kgsl_sync_fence_create( |
| 30 | struct kgsl_context *context, |
| 31 | unsigned int timestamp) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 32 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 33 | struct kgsl_sync_fence *kfence; |
| 34 | struct kgsl_sync_timeline *ktimeline = context->ktimeline; |
| 35 | unsigned long flags; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 36 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 37 | /* Get a refcount to the timeline. Put when released */ |
| 38 | if (!kref_get_unless_zero(&ktimeline->kref)) |
| 39 | return NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 40 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 41 | kfence = kzalloc(sizeof(*kfence), GFP_KERNEL); |
| 42 | if (kfence == NULL) { |
| 43 | kgsl_sync_timeline_put(ktimeline); |
| 44 | KGSL_DRV_ERR(context->device, "Couldn't allocate fence\n"); |
| 45 | return NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 46 | } |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 47 | |
| 48 | kfence->parent = ktimeline; |
| 49 | kfence->context_id = context->id; |
| 50 | kfence->timestamp = timestamp; |
| 51 | |
| 52 | fence_init(&kfence->fence, &kgsl_sync_fence_ops, &ktimeline->lock, |
| 53 | ktimeline->fence_context, timestamp); |
| 54 | |
Lynus Vaz | 9d6334b | 2017-06-27 14:57:56 +0530 | [diff] [blame] | 55 | /* |
| 56 | * sync_file_create() takes a refcount to the fence. This refcount is |
| 57 | * put when the fence is signaled. |
| 58 | */ |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 59 | kfence->sync_file = sync_file_create(&kfence->fence); |
| 60 | |
| 61 | if (kfence->sync_file == NULL) { |
| 62 | kgsl_sync_timeline_put(ktimeline); |
| 63 | KGSL_DRV_ERR(context->device, "Create sync_file failed\n"); |
| 64 | kfree(kfence); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 68 | spin_lock_irqsave(&ktimeline->lock, flags); |
| 69 | list_add_tail(&kfence->child_list, &ktimeline->child_list_head); |
| 70 | spin_unlock_irqrestore(&ktimeline->lock, flags); |
| 71 | |
| 72 | return kfence; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 75 | static void kgsl_sync_fence_release(struct fence *fence) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 76 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 77 | struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence; |
| 78 | |
| 79 | kgsl_sync_timeline_put(kfence->parent); |
| 80 | kfree(kfence); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 83 | /* Called with ktimeline->lock held */ |
| 84 | bool kgsl_sync_fence_has_signaled(struct fence *fence) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 85 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 86 | struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence; |
| 87 | struct kgsl_sync_timeline *ktimeline = kfence->parent; |
| 88 | unsigned int ts = kfence->timestamp; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 89 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 90 | return (timestamp_cmp(ktimeline->last_timestamp, ts) >= 0); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 93 | bool kgsl_enable_signaling(struct fence *fence) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 94 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 95 | return !kgsl_sync_fence_has_signaled(fence); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 98 | struct kgsl_sync_fence_event_priv { |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 99 | struct kgsl_context *context; |
| 100 | unsigned int timestamp; |
| 101 | }; |
| 102 | |
| 103 | /** |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 104 | * kgsl_sync_fence_event_cb - Event callback for a fence timestamp event |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 105 | * @device - The KGSL device that expired the timestamp |
| 106 | * @context- Pointer to the context that owns the event |
| 107 | * @priv: Private data for the callback |
| 108 | * @result - Result of the event (retired or canceled) |
| 109 | * |
| 110 | * Signal a fence following the expiration of a timestamp |
| 111 | */ |
| 112 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 113 | static void kgsl_sync_fence_event_cb(struct kgsl_device *device, |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 114 | struct kgsl_event_group *group, void *priv, int result) |
| 115 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 116 | struct kgsl_sync_fence_event_priv *ev = priv; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 117 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 118 | kgsl_sync_timeline_signal(ev->context->ktimeline, ev->timestamp); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 119 | kgsl_context_put(ev->context); |
| 120 | kfree(ev); |
| 121 | } |
| 122 | |
| 123 | static int _add_fence_event(struct kgsl_device *device, |
| 124 | struct kgsl_context *context, unsigned int timestamp) |
| 125 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 126 | struct kgsl_sync_fence_event_priv *event; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 127 | int ret; |
| 128 | |
| 129 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 130 | if (event == NULL) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | /* |
| 134 | * Increase the refcount for the context to keep it through the |
| 135 | * callback |
| 136 | */ |
| 137 | if (!_kgsl_context_get(context)) { |
| 138 | kfree(event); |
| 139 | return -ENOENT; |
| 140 | } |
| 141 | |
| 142 | event->context = context; |
| 143 | event->timestamp = timestamp; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 144 | |
| 145 | ret = kgsl_add_event(device, &context->events, timestamp, |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 146 | kgsl_sync_fence_event_cb, event); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 147 | |
| 148 | if (ret) { |
| 149 | kgsl_context_put(context); |
| 150 | kfree(event); |
| 151 | } |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
Lynus Vaz | c50d620 | 2017-03-23 13:42:58 +0530 | [diff] [blame] | 156 | /* Only to be used if creating a related event failed */ |
| 157 | static void kgsl_sync_cancel(struct kgsl_sync_fence *kfence) |
| 158 | { |
| 159 | spin_lock(&kfence->parent->lock); |
| 160 | if (!list_empty(&kfence->child_list)) { |
| 161 | list_del_init(&kfence->child_list); |
| 162 | fence_put(&kfence->fence); |
| 163 | } |
| 164 | spin_unlock(&kfence->parent->lock); |
| 165 | } |
| 166 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 167 | /** |
| 168 | * kgsl_add_fence_event - Create a new fence event |
| 169 | * @device - KGSL device to create the event on |
| 170 | * @timestamp - Timestamp to trigger the event |
| 171 | * @data - Return fence fd stored in struct kgsl_timestamp_event_fence |
| 172 | * @len - length of the fence event |
| 173 | * @owner - driver instance that owns this event |
| 174 | * @returns 0 on success or error code on error |
| 175 | * |
| 176 | * Create a fence and register an event to signal the fence when |
| 177 | * the timestamp expires |
| 178 | */ |
| 179 | |
| 180 | int kgsl_add_fence_event(struct kgsl_device *device, |
| 181 | u32 context_id, u32 timestamp, void __user *data, int len, |
| 182 | struct kgsl_device_private *owner) |
| 183 | { |
| 184 | struct kgsl_timestamp_event_fence priv; |
| 185 | struct kgsl_context *context; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 186 | struct kgsl_sync_fence *kfence = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 187 | int ret = -EINVAL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 188 | unsigned int cur; |
| 189 | |
| 190 | priv.fence_fd = -1; |
| 191 | |
| 192 | if (len != sizeof(priv)) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | context = kgsl_context_get_owner(owner, context_id); |
| 196 | |
| 197 | if (context == NULL) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | if (test_bit(KGSL_CONTEXT_PRIV_INVALID, &context->priv)) |
| 201 | goto out; |
| 202 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 203 | kfence = kgsl_sync_fence_create(context, timestamp); |
| 204 | if (kfence == NULL) { |
| 205 | KGSL_DRV_CRIT_RATELIMIT(device, |
| 206 | "kgsl_sync_fence_create failed\n"); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 207 | ret = -ENOMEM; |
| 208 | goto out; |
| 209 | } |
| 210 | |
| 211 | priv.fence_fd = get_unused_fd_flags(0); |
| 212 | if (priv.fence_fd < 0) { |
| 213 | KGSL_DRV_CRIT_RATELIMIT(device, |
| 214 | "Unable to get a file descriptor: %d\n", |
| 215 | priv.fence_fd); |
| 216 | ret = priv.fence_fd; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * If the timestamp hasn't expired yet create an event to trigger it. |
| 222 | * Otherwise, just signal the fence - there is no reason to go through |
| 223 | * the effort of creating a fence we don't need. |
| 224 | */ |
| 225 | |
| 226 | kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &cur); |
| 227 | |
| 228 | if (timestamp_cmp(cur, timestamp) >= 0) { |
| 229 | ret = 0; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 230 | kgsl_sync_timeline_signal(context->ktimeline, cur); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 231 | } else { |
| 232 | ret = _add_fence_event(device, context, timestamp); |
| 233 | if (ret) |
| 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | if (copy_to_user(data, &priv, sizeof(priv))) { |
| 238 | ret = -EFAULT; |
| 239 | goto out; |
| 240 | } |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 241 | fd_install(priv.fence_fd, kfence->sync_file->file); |
| 242 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 243 | out: |
| 244 | kgsl_context_put(context); |
| 245 | if (ret) { |
| 246 | if (priv.fence_fd >= 0) |
| 247 | put_unused_fd(priv.fence_fd); |
| 248 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 249 | if (kfence) { |
Lynus Vaz | c50d620 | 2017-03-23 13:42:58 +0530 | [diff] [blame] | 250 | kgsl_sync_cancel(kfence); |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 251 | /* |
| 252 | * Put the refcount of sync file. This will release |
| 253 | * kfence->fence as well. |
| 254 | */ |
| 255 | fput(kfence->sync_file->file); |
| 256 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 257 | } |
| 258 | return ret; |
| 259 | } |
| 260 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 261 | static unsigned int kgsl_sync_fence_get_timestamp( |
| 262 | struct kgsl_sync_timeline *ktimeline, |
| 263 | enum kgsl_timestamp_type type) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 264 | { |
| 265 | unsigned int ret = 0; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 266 | |
| 267 | if (ktimeline->device == NULL) |
| 268 | return 0; |
| 269 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 270 | kgsl_readtimestamp(ktimeline->device, ktimeline->context, type, &ret); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 271 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 272 | return ret; |
| 273 | } |
| 274 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 275 | static void kgsl_sync_timeline_value_str(struct fence *fence, |
| 276 | char *str, int size) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 277 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 278 | struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence; |
| 279 | struct kgsl_sync_timeline *ktimeline = kfence->parent; |
| 280 | |
| 281 | unsigned int timestamp_retired; |
| 282 | unsigned int timestamp_queued; |
| 283 | |
| 284 | if (!kref_get_unless_zero(&ktimeline->kref)) |
| 285 | return; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * This callback can be called before the device and spinlock are |
| 289 | * initialized in struct kgsl_sync_timeline. kgsl_sync_get_timestamp() |
| 290 | * will check if device is NULL and return 0. Queued and retired |
| 291 | * timestamp of the context will be reported as 0, which is correct |
| 292 | * because the context and timeline are just getting initialized. |
| 293 | */ |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 294 | timestamp_retired = kgsl_sync_fence_get_timestamp(ktimeline, |
| 295 | KGSL_TIMESTAMP_RETIRED); |
| 296 | timestamp_queued = kgsl_sync_fence_get_timestamp(ktimeline, |
| 297 | KGSL_TIMESTAMP_QUEUED); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 298 | |
| 299 | snprintf(str, size, "%u queued:%u retired:%u", |
| 300 | ktimeline->last_timestamp, |
| 301 | timestamp_queued, timestamp_retired); |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 302 | |
| 303 | kgsl_sync_timeline_put(ktimeline); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 306 | static void kgsl_sync_fence_value_str(struct fence *fence, char *str, int size) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 307 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 308 | struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 309 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 310 | snprintf(str, size, "%u", kfence->timestamp); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 313 | static const char *kgsl_sync_fence_driver_name(struct fence *fence) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 314 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 315 | return "kgsl-timeline"; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 318 | static const char *kgsl_sync_timeline_name(struct fence *fence) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 319 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 320 | struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence; |
| 321 | struct kgsl_sync_timeline *ktimeline = kfence->parent; |
| 322 | |
| 323 | return ktimeline->name; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 324 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 325 | |
| 326 | int kgsl_sync_timeline_create(struct kgsl_context *context) |
| 327 | { |
| 328 | struct kgsl_sync_timeline *ktimeline; |
| 329 | |
| 330 | /* |
| 331 | * Generate a name which includes the thread name, thread id, process |
| 332 | * name, process id, and context id. This makes it possible to |
| 333 | * identify the context of a timeline in the sync dump. |
| 334 | */ |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 335 | char ktimeline_name[sizeof(ktimeline->name)] = {}; |
| 336 | |
| 337 | /* Put context when timeline is released */ |
| 338 | if (!_kgsl_context_get(context)) |
| 339 | return -ENOENT; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 340 | |
| 341 | snprintf(ktimeline_name, sizeof(ktimeline_name), |
Lynus Vaz | 3c4d38685 | 2017-10-11 18:18:04 +0530 | [diff] [blame] | 342 | "%s_%d-%.15s(%d)-%.15s(%d)", |
| 343 | context->device->name, context->id, |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 344 | current->group_leader->comm, current->group_leader->pid, |
Lynus Vaz | 3c4d38685 | 2017-10-11 18:18:04 +0530 | [diff] [blame] | 345 | current->comm, current->pid); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 346 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 347 | ktimeline = kzalloc(sizeof(*ktimeline), GFP_KERNEL); |
| 348 | if (ktimeline == NULL) { |
| 349 | kgsl_context_put(context); |
| 350 | return -ENOMEM; |
| 351 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 352 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 353 | kref_init(&ktimeline->kref); |
| 354 | strlcpy(ktimeline->name, ktimeline_name, KGSL_TIMELINE_NAME_LEN); |
| 355 | ktimeline->fence_context = fence_context_alloc(1); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 356 | ktimeline->last_timestamp = 0; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 357 | INIT_LIST_HEAD(&ktimeline->child_list_head); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 358 | spin_lock_init(&ktimeline->lock); |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 359 | ktimeline->device = context->device; |
| 360 | ktimeline->context = context; |
| 361 | |
| 362 | context->ktimeline = ktimeline; |
| 363 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 367 | static void kgsl_sync_timeline_signal(struct kgsl_sync_timeline *ktimeline, |
| 368 | unsigned int timestamp) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 369 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 370 | unsigned long flags; |
| 371 | struct kgsl_sync_fence *kfence, *next; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 372 | |
Lynus Vaz | 8795cc6 | 2017-07-28 21:33:11 +0530 | [diff] [blame] | 373 | if (!kref_get_unless_zero(&ktimeline->kref)) |
| 374 | return; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 375 | |
| 376 | spin_lock_irqsave(&ktimeline->lock, flags); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 377 | if (timestamp_cmp(timestamp, ktimeline->last_timestamp) > 0) |
| 378 | ktimeline->last_timestamp = timestamp; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 379 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 380 | list_for_each_entry_safe(kfence, next, &ktimeline->child_list_head, |
| 381 | child_list) { |
| 382 | if (fence_is_signaled_locked(&kfence->fence)) { |
Lynus Vaz | c50d620 | 2017-03-23 13:42:58 +0530 | [diff] [blame] | 383 | list_del_init(&kfence->child_list); |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 384 | fence_put(&kfence->fence); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | spin_unlock_irqrestore(&ktimeline->lock, flags); |
| 389 | kgsl_sync_timeline_put(ktimeline); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void kgsl_sync_timeline_destroy(struct kgsl_context *context) |
| 393 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 394 | kfree(context->ktimeline); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 397 | static void kgsl_sync_timeline_release(struct kref *kref) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 398 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 399 | struct kgsl_sync_timeline *ktimeline = |
| 400 | container_of(kref, struct kgsl_sync_timeline, kref); |
| 401 | |
| 402 | /* |
| 403 | * Only put the context refcount here. The context destroy function |
| 404 | * will call kgsl_sync_timeline_destroy() to kfree it |
| 405 | */ |
| 406 | kgsl_context_put(ktimeline->context); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 409 | void kgsl_sync_timeline_put(struct kgsl_sync_timeline *ktimeline) |
| 410 | { |
| 411 | if (ktimeline) |
| 412 | kref_put(&ktimeline->kref, kgsl_sync_timeline_release); |
| 413 | } |
| 414 | |
| 415 | static const struct fence_ops kgsl_sync_fence_ops = { |
| 416 | .get_driver_name = kgsl_sync_fence_driver_name, |
| 417 | .get_timeline_name = kgsl_sync_timeline_name, |
| 418 | .enable_signaling = kgsl_enable_signaling, |
| 419 | .signaled = kgsl_sync_fence_has_signaled, |
| 420 | .wait = fence_default_wait, |
| 421 | .release = kgsl_sync_fence_release, |
| 422 | |
| 423 | .fence_value_str = kgsl_sync_fence_value_str, |
| 424 | .timeline_value_str = kgsl_sync_timeline_value_str, |
| 425 | }; |
| 426 | |
| 427 | static void kgsl_sync_fence_callback(struct fence *fence, struct fence_cb *cb) |
| 428 | { |
| 429 | struct kgsl_sync_fence_cb *kcb = (struct kgsl_sync_fence_cb *)cb; |
| 430 | |
Lynus Vaz | 27da44d | 2017-07-26 13:50:10 +0530 | [diff] [blame] | 431 | /* |
| 432 | * If the callback is marked for cancellation in a separate thread, |
| 433 | * let the other thread do the cleanup. |
| 434 | */ |
| 435 | if (kcb->func(kcb->priv)) { |
| 436 | fence_put(kcb->fence); |
| 437 | kfree(kcb); |
| 438 | } |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 439 | } |
| 440 | |
Lynus Vaz | e99b92b | 2017-04-24 18:04:54 +0530 | [diff] [blame] | 441 | static void kgsl_get_fence_name(struct fence *fence, |
| 442 | char *fence_name, int name_len) |
| 443 | { |
| 444 | char *ptr = fence_name; |
| 445 | char *last = fence_name + name_len; |
| 446 | |
| 447 | ptr += snprintf(ptr, last - ptr, "%s %s", |
| 448 | fence->ops->get_driver_name(fence), |
| 449 | fence->ops->get_timeline_name(fence)); |
| 450 | |
| 451 | if ((ptr + 2) >= last) |
| 452 | return; |
| 453 | |
| 454 | if (fence->ops->fence_value_str) { |
| 455 | ptr += snprintf(ptr, last - ptr, ": "); |
| 456 | fence->ops->fence_value_str(fence, ptr, last - ptr); |
| 457 | } |
| 458 | } |
| 459 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 460 | struct kgsl_sync_fence_cb *kgsl_sync_fence_async_wait(int fd, |
Lynus Vaz | 27da44d | 2017-07-26 13:50:10 +0530 | [diff] [blame] | 461 | bool (*func)(void *priv), void *priv, char *fence_name, int name_len) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 462 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 463 | struct kgsl_sync_fence_cb *kcb; |
| 464 | struct fence *fence; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 465 | int status; |
| 466 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 467 | fence = sync_file_get_fence(fd); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 468 | if (fence == NULL) |
| 469 | return ERR_PTR(-EINVAL); |
| 470 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 471 | /* create the callback */ |
| 472 | kcb = kzalloc(sizeof(*kcb), GFP_ATOMIC); |
| 473 | if (kcb == NULL) { |
| 474 | fence_put(fence); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 475 | return ERR_PTR(-ENOMEM); |
| 476 | } |
| 477 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 478 | kcb->fence = fence; |
| 479 | kcb->priv = priv; |
| 480 | kcb->func = func; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 481 | |
Lynus Vaz | e99b92b | 2017-04-24 18:04:54 +0530 | [diff] [blame] | 482 | if (fence_name) |
| 483 | kgsl_get_fence_name(fence, fence_name, name_len); |
| 484 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 485 | /* if status then error or signaled */ |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 486 | status = fence_add_callback(fence, &kcb->fence_cb, |
| 487 | kgsl_sync_fence_callback); |
| 488 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 489 | if (status) { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 490 | kfree(kcb); |
Harshdeep Dhatt | 0ed60a4 | 2017-04-20 16:41:50 -0600 | [diff] [blame] | 491 | if (!fence_is_signaled(fence)) |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 492 | kcb = ERR_PTR(status); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 493 | else |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 494 | kcb = NULL; |
| 495 | fence_put(fence); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 498 | return kcb; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Lynus Vaz | 27da44d | 2017-07-26 13:50:10 +0530 | [diff] [blame] | 501 | /* |
| 502 | * Cancel the fence async callback and do the cleanup. The caller must make |
| 503 | * sure that the callback (if run before cancelling) returns false, so that |
| 504 | * no other thread frees the pointer. |
| 505 | */ |
| 506 | void kgsl_sync_fence_async_cancel(struct kgsl_sync_fence_cb *kcb) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 507 | { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 508 | if (kcb == NULL) |
Lynus Vaz | 27da44d | 2017-07-26 13:50:10 +0530 | [diff] [blame] | 509 | return; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 510 | |
Lynus Vaz | 27da44d | 2017-07-26 13:50:10 +0530 | [diff] [blame] | 511 | /* |
| 512 | * After fence_remove_callback() returns, the fence callback is |
| 513 | * either not called at all, or completed without freeing kcb. |
| 514 | * This thread can then put the fence refcount and free kcb. |
| 515 | */ |
| 516 | fence_remove_callback(kcb->fence, &kcb->fence_cb); |
| 517 | fence_put(kcb->fence); |
| 518 | kfree(kcb); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 521 | struct kgsl_syncsource { |
| 522 | struct kref refcount; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 523 | char name[KGSL_TIMELINE_NAME_LEN]; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 524 | int id; |
| 525 | struct kgsl_process_private *private; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 526 | struct list_head child_list_head; |
| 527 | spinlock_t lock; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 528 | }; |
| 529 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 530 | struct kgsl_syncsource_fence { |
| 531 | struct fence fence; |
| 532 | struct kgsl_syncsource *parent; |
| 533 | struct list_head child_list; |
| 534 | }; |
| 535 | |
| 536 | static const struct fence_ops kgsl_syncsource_fence_ops; |
| 537 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 538 | long kgsl_ioctl_syncsource_create(struct kgsl_device_private *dev_priv, |
| 539 | unsigned int cmd, void *data) |
| 540 | { |
| 541 | struct kgsl_syncsource *syncsource = NULL; |
| 542 | struct kgsl_syncsource_create *param = data; |
| 543 | int ret = -EINVAL; |
| 544 | int id = 0; |
| 545 | struct kgsl_process_private *private = dev_priv->process_priv; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 546 | char name[KGSL_TIMELINE_NAME_LEN]; |
| 547 | |
| 548 | if (!kgsl_process_private_get(private)) |
| 549 | return ret; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 550 | |
| 551 | syncsource = kzalloc(sizeof(*syncsource), GFP_KERNEL); |
| 552 | if (syncsource == NULL) { |
| 553 | ret = -ENOMEM; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | snprintf(name, sizeof(name), "kgsl-syncsource-pid-%d", |
| 558 | current->group_leader->pid); |
| 559 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 560 | kref_init(&syncsource->refcount); |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 561 | strlcpy(syncsource->name, name, KGSL_TIMELINE_NAME_LEN); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 562 | syncsource->private = private; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 563 | INIT_LIST_HEAD(&syncsource->child_list_head); |
| 564 | spin_lock_init(&syncsource->lock); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 565 | |
| 566 | idr_preload(GFP_KERNEL); |
| 567 | spin_lock(&private->syncsource_lock); |
| 568 | id = idr_alloc(&private->syncsource_idr, syncsource, 1, 0, GFP_NOWAIT); |
| 569 | if (id > 0) { |
| 570 | syncsource->id = id; |
| 571 | param->id = id; |
| 572 | ret = 0; |
| 573 | } else { |
| 574 | ret = id; |
| 575 | } |
| 576 | |
| 577 | spin_unlock(&private->syncsource_lock); |
| 578 | idr_preload_end(); |
| 579 | |
| 580 | out: |
| 581 | if (ret) { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 582 | kgsl_process_private_put(private); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 583 | kfree(syncsource); |
| 584 | } |
| 585 | |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | static struct kgsl_syncsource * |
| 590 | kgsl_syncsource_get(struct kgsl_process_private *private, int id) |
| 591 | { |
| 592 | int result = 0; |
| 593 | struct kgsl_syncsource *syncsource = NULL; |
| 594 | |
| 595 | spin_lock(&private->syncsource_lock); |
| 596 | |
| 597 | syncsource = idr_find(&private->syncsource_idr, id); |
| 598 | if (syncsource) |
| 599 | result = kref_get_unless_zero(&syncsource->refcount); |
| 600 | |
| 601 | spin_unlock(&private->syncsource_lock); |
| 602 | |
| 603 | return result ? syncsource : NULL; |
| 604 | } |
| 605 | |
| 606 | static void kgsl_syncsource_destroy(struct kref *kref) |
| 607 | { |
| 608 | struct kgsl_syncsource *syncsource = container_of(kref, |
| 609 | struct kgsl_syncsource, |
| 610 | refcount); |
| 611 | |
| 612 | struct kgsl_process_private *private = syncsource->private; |
| 613 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 614 | /* Done with process private. Release the refcount */ |
| 615 | kgsl_process_private_put(private); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 616 | |
| 617 | kfree(syncsource); |
| 618 | } |
| 619 | |
| 620 | void kgsl_syncsource_put(struct kgsl_syncsource *syncsource) |
| 621 | { |
| 622 | if (syncsource) |
| 623 | kref_put(&syncsource->refcount, kgsl_syncsource_destroy); |
| 624 | } |
| 625 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 626 | void kgsl_syncsource_cleanup(struct kgsl_process_private *private, |
| 627 | struct kgsl_syncsource *syncsource) |
| 628 | { |
| 629 | struct kgsl_syncsource_fence *sfence, *next; |
| 630 | |
| 631 | spin_lock(&private->syncsource_lock); |
| 632 | if (syncsource->id != 0) { |
| 633 | idr_remove(&private->syncsource_idr, syncsource->id); |
| 634 | syncsource->id = 0; |
| 635 | } |
| 636 | spin_unlock(&private->syncsource_lock); |
| 637 | |
| 638 | /* Signal all fences to release any callbacks */ |
| 639 | spin_lock(&syncsource->lock); |
| 640 | |
| 641 | list_for_each_entry_safe(sfence, next, &syncsource->child_list_head, |
| 642 | child_list) { |
| 643 | fence_signal_locked(&sfence->fence); |
| 644 | list_del_init(&sfence->child_list); |
| 645 | } |
| 646 | |
| 647 | spin_unlock(&syncsource->lock); |
| 648 | |
| 649 | /* put reference from syncsource creation */ |
| 650 | kgsl_syncsource_put(syncsource); |
| 651 | } |
| 652 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 653 | long kgsl_ioctl_syncsource_destroy(struct kgsl_device_private *dev_priv, |
| 654 | unsigned int cmd, void *data) |
| 655 | { |
| 656 | struct kgsl_syncsource_destroy *param = data; |
| 657 | struct kgsl_syncsource *syncsource = NULL; |
| 658 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 659 | |
| 660 | spin_lock(&private->syncsource_lock); |
| 661 | syncsource = idr_find(&private->syncsource_idr, param->id); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 662 | spin_unlock(&private->syncsource_lock); |
| 663 | |
| 664 | if (syncsource == NULL) |
| 665 | return -EINVAL; |
| 666 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 667 | kgsl_syncsource_cleanup(private, syncsource); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | long kgsl_ioctl_syncsource_create_fence(struct kgsl_device_private *dev_priv, |
| 672 | unsigned int cmd, void *data) |
| 673 | { |
| 674 | struct kgsl_syncsource_create_fence *param = data; |
| 675 | struct kgsl_syncsource *syncsource = NULL; |
| 676 | int ret = -EINVAL; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 677 | struct kgsl_syncsource_fence *sfence = NULL; |
| 678 | struct sync_file *sync_file = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 679 | int fd = -1; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 680 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 681 | /* |
| 682 | * Take a refcount that is released when the fence is released |
| 683 | * (or if fence can't be added to the syncsource). |
| 684 | */ |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 685 | syncsource = kgsl_syncsource_get(dev_priv->process_priv, |
| 686 | param->id); |
| 687 | if (syncsource == NULL) |
| 688 | goto out; |
| 689 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 690 | sfence = kzalloc(sizeof(*sfence), GFP_KERNEL); |
| 691 | if (sfence == NULL) { |
| 692 | ret = -ENOMEM; |
| 693 | goto out; |
| 694 | } |
| 695 | sfence->parent = syncsource; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 696 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 697 | /* Use a new fence context for each fence */ |
| 698 | fence_init(&sfence->fence, &kgsl_syncsource_fence_ops, |
| 699 | &syncsource->lock, fence_context_alloc(1), 1); |
| 700 | |
| 701 | sync_file = sync_file_create(&sfence->fence); |
| 702 | |
| 703 | if (sync_file == NULL) { |
| 704 | KGSL_DRV_ERR(dev_priv->device, "Create sync_file failed\n"); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 705 | ret = -ENOMEM; |
| 706 | goto out; |
| 707 | } |
| 708 | |
| 709 | fd = get_unused_fd_flags(0); |
| 710 | if (fd < 0) { |
| 711 | ret = -EBADF; |
| 712 | goto out; |
| 713 | } |
| 714 | ret = 0; |
| 715 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 716 | fd_install(fd, sync_file->file); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 717 | |
| 718 | param->fence_fd = fd; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 719 | |
| 720 | spin_lock(&syncsource->lock); |
| 721 | list_add_tail(&sfence->child_list, &syncsource->child_list_head); |
| 722 | spin_unlock(&syncsource->lock); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 723 | out: |
Lynus Vaz | 9d6334b | 2017-06-27 14:57:56 +0530 | [diff] [blame] | 724 | /* |
| 725 | * We're transferring ownership of the fence to the sync file. |
| 726 | * The sync file takes an extra refcount when it is created, so put |
| 727 | * our refcount. |
| 728 | */ |
| 729 | if (sync_file) |
| 730 | fence_put(&sfence->fence); |
| 731 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 732 | if (ret) { |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 733 | if (sync_file) |
| 734 | fput(sync_file->file); |
| 735 | else if (sfence) |
| 736 | fence_put(&sfence->fence); |
| 737 | kgsl_syncsource_put(syncsource); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 738 | } |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 739 | |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static int kgsl_syncsource_signal(struct kgsl_syncsource *syncsource, |
| 744 | struct fence *fence) |
| 745 | { |
| 746 | struct kgsl_syncsource_fence *sfence, *next; |
| 747 | int ret = -EINVAL; |
| 748 | |
| 749 | spin_lock(&syncsource->lock); |
| 750 | |
| 751 | list_for_each_entry_safe(sfence, next, &syncsource->child_list_head, |
| 752 | child_list) { |
| 753 | if (fence == &sfence->fence) { |
| 754 | fence_signal_locked(fence); |
| 755 | list_del_init(&sfence->child_list); |
| 756 | |
| 757 | ret = 0; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | spin_unlock(&syncsource->lock); |
| 763 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | long kgsl_ioctl_syncsource_signal_fence(struct kgsl_device_private *dev_priv, |
| 768 | unsigned int cmd, void *data) |
| 769 | { |
| 770 | int ret = -EINVAL; |
| 771 | struct kgsl_syncsource_signal_fence *param = data; |
| 772 | struct kgsl_syncsource *syncsource = NULL; |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 773 | struct fence *fence = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 774 | |
| 775 | syncsource = kgsl_syncsource_get(dev_priv->process_priv, |
| 776 | param->id); |
| 777 | if (syncsource == NULL) |
| 778 | goto out; |
| 779 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 780 | fence = sync_file_get_fence(param->fence_fd); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 781 | if (fence == NULL) { |
| 782 | ret = -EBADF; |
| 783 | goto out; |
| 784 | } |
| 785 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 786 | ret = kgsl_syncsource_signal(syncsource, fence); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 787 | out: |
| 788 | if (fence) |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 789 | fence_put(fence); |
| 790 | if (syncsource) |
| 791 | kgsl_syncsource_put(syncsource); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 792 | return ret; |
| 793 | } |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 794 | |
| 795 | static void kgsl_syncsource_fence_release(struct fence *fence) |
| 796 | { |
| 797 | struct kgsl_syncsource_fence *sfence = |
| 798 | (struct kgsl_syncsource_fence *)fence; |
| 799 | |
| 800 | /* Signal if it's not signaled yet */ |
| 801 | kgsl_syncsource_signal(sfence->parent, fence); |
| 802 | |
| 803 | /* Release the refcount on the syncsource */ |
| 804 | kgsl_syncsource_put(sfence->parent); |
| 805 | |
| 806 | kfree(sfence); |
| 807 | } |
| 808 | |
| 809 | static const char *kgsl_syncsource_get_timeline_name(struct fence *fence) |
| 810 | { |
| 811 | struct kgsl_syncsource_fence *sfence = |
| 812 | (struct kgsl_syncsource_fence *)fence; |
| 813 | struct kgsl_syncsource *syncsource = sfence->parent; |
| 814 | |
| 815 | return syncsource->name; |
| 816 | } |
| 817 | |
| 818 | static bool kgsl_syncsource_enable_signaling(struct fence *fence) |
| 819 | { |
| 820 | return true; |
| 821 | } |
| 822 | |
| 823 | static const char *kgsl_syncsource_driver_name(struct fence *fence) |
| 824 | { |
| 825 | return "kgsl-syncsource-timeline"; |
| 826 | } |
| 827 | |
Lynus Vaz | 6aa3f5a | 2017-04-26 19:29:05 +0530 | [diff] [blame] | 828 | static void kgsl_syncsource_fence_value_str(struct fence *fence, |
| 829 | char *str, int size) |
| 830 | { |
| 831 | /* |
| 832 | * Each fence is independent of the others on the same timeline. |
| 833 | * We use a different context for each of them. |
| 834 | */ |
| 835 | snprintf(str, size, "%llu", fence->context); |
| 836 | } |
| 837 | |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 838 | static const struct fence_ops kgsl_syncsource_fence_ops = { |
| 839 | .get_driver_name = kgsl_syncsource_driver_name, |
| 840 | .get_timeline_name = kgsl_syncsource_get_timeline_name, |
| 841 | .enable_signaling = kgsl_syncsource_enable_signaling, |
| 842 | .wait = fence_default_wait, |
| 843 | .release = kgsl_syncsource_fence_release, |
Lynus Vaz | 6aa3f5a | 2017-04-26 19:29:05 +0530 | [diff] [blame] | 844 | |
| 845 | .fence_value_str = kgsl_syncsource_fence_value_str, |
Lynus Vaz | c031a9b | 2017-01-25 13:00:13 +0530 | [diff] [blame] | 846 | }; |
| 847 | |