Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright © 2011 VMware, Inc., Palo Alto, CA., USA |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 29 | #include "vmwgfx_drv.h" |
| 30 | |
| 31 | #define VMW_FENCE_WRAP (1 << 31) |
| 32 | |
| 33 | struct vmw_fence_manager { |
| 34 | int num_fence_objects; |
| 35 | struct vmw_private *dev_priv; |
| 36 | spinlock_t lock; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 37 | struct list_head fence_list; |
| 38 | struct work_struct work; |
| 39 | u32 user_fence_size; |
| 40 | u32 fence_size; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 41 | u32 event_fence_action_size; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 42 | bool fifo_down; |
| 43 | struct list_head cleanup_list; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 44 | uint32_t pending_actions[VMW_ACTION_MAX]; |
| 45 | struct mutex goal_irq_mutex; |
| 46 | bool goal_irq_on; /* Protected by @goal_irq_mutex */ |
| 47 | bool seqno_valid; /* Protected by @lock, and may not be set to true |
| 48 | without the @goal_irq_mutex held. */ |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | struct vmw_user_fence { |
| 52 | struct ttm_base_object base; |
| 53 | struct vmw_fence_obj fence; |
| 54 | }; |
| 55 | |
| 56 | /** |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 57 | * struct vmw_event_fence_action - fence action that delivers a drm event. |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 58 | * |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 59 | * @e: A struct drm_pending_event that controls the event delivery. |
| 60 | * @action: A struct vmw_fence_action to hook up to a fence. |
| 61 | * @fence: A referenced pointer to the fence to keep it alive while @action |
| 62 | * hangs on it. |
| 63 | * @dev: Pointer to a struct drm_device so we can access the event stuff. |
| 64 | * @kref: Both @e and @action has destructors, so we need to refcount. |
| 65 | * @size: Size accounted for this object. |
| 66 | * @tv_sec: If non-null, the variable pointed to will be assigned |
| 67 | * current time tv_sec val when the fence signals. |
| 68 | * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will |
| 69 | * be assigned the current time tv_usec val when the fence signals. |
| 70 | */ |
| 71 | struct vmw_event_fence_action { |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 72 | struct vmw_fence_action action; |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 73 | struct list_head fpriv_head; |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 74 | |
| 75 | struct drm_pending_event *event; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 76 | struct vmw_fence_obj *fence; |
| 77 | struct drm_device *dev; |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 78 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 79 | uint32_t *tv_sec; |
| 80 | uint32_t *tv_usec; |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * Note on fencing subsystem usage of irqs: |
| 85 | * Typically the vmw_fences_update function is called |
| 86 | * |
| 87 | * a) When a new fence seqno has been submitted by the fifo code. |
| 88 | * b) On-demand when we have waiters. Sleeping waiters will switch on the |
| 89 | * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE |
| 90 | * irq is received. When the last fence waiter is gone, that IRQ is masked |
| 91 | * away. |
| 92 | * |
| 93 | * In situations where there are no waiters and we don't submit any new fences, |
| 94 | * fence objects may not be signaled. This is perfectly OK, since there are |
| 95 | * no consumers of the signaled data, but that is NOT ok when there are fence |
| 96 | * actions attached to a fence. The fencing subsystem then makes use of the |
| 97 | * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence |
| 98 | * which has an action attached, and each time vmw_fences_update is called, |
| 99 | * the subsystem makes sure the fence goal seqno is updated. |
| 100 | * |
| 101 | * The fence goal seqno irq is on as long as there are unsignaled fence |
| 102 | * objects with actions attached to them. |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 103 | */ |
| 104 | |
| 105 | static void vmw_fence_obj_destroy_locked(struct kref *kref) |
| 106 | { |
| 107 | struct vmw_fence_obj *fence = |
| 108 | container_of(kref, struct vmw_fence_obj, kref); |
| 109 | |
| 110 | struct vmw_fence_manager *fman = fence->fman; |
| 111 | unsigned int num_fences; |
| 112 | |
| 113 | list_del_init(&fence->head); |
| 114 | num_fences = --fman->num_fence_objects; |
| 115 | spin_unlock_irq(&fman->lock); |
| 116 | if (fence->destroy) |
| 117 | fence->destroy(fence); |
| 118 | else |
| 119 | kfree(fence); |
| 120 | |
| 121 | spin_lock_irq(&fman->lock); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Execute signal actions on fences recently signaled. |
| 127 | * This is done from a workqueue so we don't have to execute |
| 128 | * signal actions from atomic context. |
| 129 | */ |
| 130 | |
| 131 | static void vmw_fence_work_func(struct work_struct *work) |
| 132 | { |
| 133 | struct vmw_fence_manager *fman = |
| 134 | container_of(work, struct vmw_fence_manager, work); |
| 135 | struct list_head list; |
| 136 | struct vmw_fence_action *action, *next_action; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 137 | bool seqno_valid; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 138 | |
| 139 | do { |
| 140 | INIT_LIST_HEAD(&list); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 141 | mutex_lock(&fman->goal_irq_mutex); |
| 142 | |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 143 | spin_lock_irq(&fman->lock); |
| 144 | list_splice_init(&fman->cleanup_list, &list); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 145 | seqno_valid = fman->seqno_valid; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 146 | spin_unlock_irq(&fman->lock); |
| 147 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 148 | if (!seqno_valid && fman->goal_irq_on) { |
| 149 | fman->goal_irq_on = false; |
| 150 | vmw_goal_waiter_remove(fman->dev_priv); |
| 151 | } |
| 152 | mutex_unlock(&fman->goal_irq_mutex); |
| 153 | |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 154 | if (list_empty(&list)) |
| 155 | return; |
| 156 | |
| 157 | /* |
| 158 | * At this point, only we should be able to manipulate the |
| 159 | * list heads of the actions we have on the private list. |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 160 | * hence fman::lock not held. |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 161 | */ |
| 162 | |
| 163 | list_for_each_entry_safe(action, next_action, &list, head) { |
| 164 | list_del_init(&action->head); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 165 | if (action->cleanup) |
| 166 | action->cleanup(action); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 167 | } |
| 168 | } while (1); |
| 169 | } |
| 170 | |
| 171 | struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv) |
| 172 | { |
| 173 | struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL); |
| 174 | |
| 175 | if (unlikely(fman == NULL)) |
| 176 | return NULL; |
| 177 | |
| 178 | fman->dev_priv = dev_priv; |
| 179 | spin_lock_init(&fman->lock); |
| 180 | INIT_LIST_HEAD(&fman->fence_list); |
| 181 | INIT_LIST_HEAD(&fman->cleanup_list); |
| 182 | INIT_WORK(&fman->work, &vmw_fence_work_func); |
| 183 | fman->fifo_down = true; |
| 184 | fman->user_fence_size = ttm_round_pot(sizeof(struct vmw_user_fence)); |
| 185 | fman->fence_size = ttm_round_pot(sizeof(struct vmw_fence_obj)); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 186 | fman->event_fence_action_size = |
| 187 | ttm_round_pot(sizeof(struct vmw_event_fence_action)); |
| 188 | mutex_init(&fman->goal_irq_mutex); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 189 | |
| 190 | return fman; |
| 191 | } |
| 192 | |
| 193 | void vmw_fence_manager_takedown(struct vmw_fence_manager *fman) |
| 194 | { |
| 195 | unsigned long irq_flags; |
| 196 | bool lists_empty; |
| 197 | |
| 198 | (void) cancel_work_sync(&fman->work); |
| 199 | |
| 200 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 201 | lists_empty = list_empty(&fman->fence_list) && |
| 202 | list_empty(&fman->cleanup_list); |
| 203 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 204 | |
| 205 | BUG_ON(!lists_empty); |
| 206 | kfree(fman); |
| 207 | } |
| 208 | |
| 209 | static int vmw_fence_obj_init(struct vmw_fence_manager *fman, |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 210 | struct vmw_fence_obj *fence, u32 seqno, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 211 | void (*destroy) (struct vmw_fence_obj *fence)) |
| 212 | { |
| 213 | unsigned long irq_flags; |
| 214 | unsigned int num_fences; |
| 215 | int ret = 0; |
| 216 | |
| 217 | fence->seqno = seqno; |
| 218 | INIT_LIST_HEAD(&fence->seq_passed_actions); |
| 219 | fence->fman = fman; |
| 220 | fence->signaled = 0; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 221 | kref_init(&fence->kref); |
| 222 | fence->destroy = destroy; |
| 223 | init_waitqueue_head(&fence->queue); |
| 224 | |
| 225 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 226 | if (unlikely(fman->fifo_down)) { |
| 227 | ret = -EBUSY; |
| 228 | goto out_unlock; |
| 229 | } |
| 230 | list_add_tail(&fence->head, &fman->fence_list); |
| 231 | num_fences = ++fman->num_fence_objects; |
| 232 | |
| 233 | out_unlock: |
| 234 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 235 | return ret; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | struct vmw_fence_obj *vmw_fence_obj_reference(struct vmw_fence_obj *fence) |
| 240 | { |
Thomas Hellstrom | e93daed | 2011-10-04 20:13:29 +0200 | [diff] [blame] | 241 | if (unlikely(fence == NULL)) |
| 242 | return NULL; |
| 243 | |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 244 | kref_get(&fence->kref); |
| 245 | return fence; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * vmw_fence_obj_unreference |
| 250 | * |
| 251 | * Note that this function may not be entered with disabled irqs since |
| 252 | * it may re-enable them in the destroy function. |
| 253 | * |
| 254 | */ |
| 255 | void vmw_fence_obj_unreference(struct vmw_fence_obj **fence_p) |
| 256 | { |
| 257 | struct vmw_fence_obj *fence = *fence_p; |
Thomas Hellstrom | e93daed | 2011-10-04 20:13:29 +0200 | [diff] [blame] | 258 | struct vmw_fence_manager *fman; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 259 | |
Thomas Hellstrom | e93daed | 2011-10-04 20:13:29 +0200 | [diff] [blame] | 260 | if (unlikely(fence == NULL)) |
| 261 | return; |
| 262 | |
| 263 | fman = fence->fman; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 264 | *fence_p = NULL; |
| 265 | spin_lock_irq(&fman->lock); |
| 266 | BUG_ON(atomic_read(&fence->kref.refcount) == 0); |
| 267 | kref_put(&fence->kref, vmw_fence_obj_destroy_locked); |
| 268 | spin_unlock_irq(&fman->lock); |
| 269 | } |
| 270 | |
Rashika Kheria | 94844cf | 2014-01-06 22:21:21 +0530 | [diff] [blame] | 271 | static void vmw_fences_perform_actions(struct vmw_fence_manager *fman, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 272 | struct list_head *list) |
| 273 | { |
| 274 | struct vmw_fence_action *action, *next_action; |
| 275 | |
| 276 | list_for_each_entry_safe(action, next_action, list, head) { |
| 277 | list_del_init(&action->head); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 278 | fman->pending_actions[action->type]--; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 279 | if (action->seq_passed != NULL) |
| 280 | action->seq_passed(action); |
| 281 | |
| 282 | /* |
| 283 | * Add the cleanup action to the cleanup list so that |
| 284 | * it will be performed by a worker task. |
| 285 | */ |
| 286 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 287 | list_add_tail(&action->head, &fman->cleanup_list); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 291 | /** |
| 292 | * vmw_fence_goal_new_locked - Figure out a new device fence goal |
| 293 | * seqno if needed. |
| 294 | * |
| 295 | * @fman: Pointer to a fence manager. |
| 296 | * @passed_seqno: The seqno the device currently signals as passed. |
| 297 | * |
| 298 | * This function should be called with the fence manager lock held. |
| 299 | * It is typically called when we have a new passed_seqno, and |
| 300 | * we might need to update the fence goal. It checks to see whether |
| 301 | * the current fence goal has already passed, and, in that case, |
| 302 | * scans through all unsignaled fences to get the next fence object with an |
| 303 | * action attached, and sets the seqno of that fence as a new fence goal. |
| 304 | * |
| 305 | * returns true if the device goal seqno was updated. False otherwise. |
| 306 | */ |
| 307 | static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman, |
| 308 | u32 passed_seqno) |
| 309 | { |
| 310 | u32 goal_seqno; |
| 311 | __le32 __iomem *fifo_mem; |
| 312 | struct vmw_fence_obj *fence; |
| 313 | |
| 314 | if (likely(!fman->seqno_valid)) |
| 315 | return false; |
| 316 | |
| 317 | fifo_mem = fman->dev_priv->mmio_virt; |
| 318 | goal_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE_GOAL); |
| 319 | if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP)) |
| 320 | return false; |
| 321 | |
| 322 | fman->seqno_valid = false; |
| 323 | list_for_each_entry(fence, &fman->fence_list, head) { |
| 324 | if (!list_empty(&fence->seq_passed_actions)) { |
| 325 | fman->seqno_valid = true; |
| 326 | iowrite32(fence->seqno, |
| 327 | fifo_mem + SVGA_FIFO_FENCE_GOAL); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * vmw_fence_goal_check_locked - Replace the device fence goal seqno if |
| 338 | * needed. |
| 339 | * |
| 340 | * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be |
| 341 | * considered as a device fence goal. |
| 342 | * |
| 343 | * This function should be called with the fence manager lock held. |
| 344 | * It is typically called when an action has been attached to a fence to |
| 345 | * check whether the seqno of that fence should be used for a fence |
| 346 | * goal interrupt. This is typically needed if the current fence goal is |
| 347 | * invalid, or has a higher seqno than that of the current fence object. |
| 348 | * |
| 349 | * returns true if the device goal seqno was updated. False otherwise. |
| 350 | */ |
| 351 | static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence) |
| 352 | { |
| 353 | u32 goal_seqno; |
| 354 | __le32 __iomem *fifo_mem; |
| 355 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 356 | if (fence->signaled) |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 357 | return false; |
| 358 | |
| 359 | fifo_mem = fence->fman->dev_priv->mmio_virt; |
| 360 | goal_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE_GOAL); |
| 361 | if (likely(fence->fman->seqno_valid && |
| 362 | goal_seqno - fence->seqno < VMW_FENCE_WRAP)) |
| 363 | return false; |
| 364 | |
| 365 | iowrite32(fence->seqno, fifo_mem + SVGA_FIFO_FENCE_GOAL); |
| 366 | fence->fman->seqno_valid = true; |
| 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void vmw_fences_update(struct vmw_fence_manager *fman) |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 372 | { |
| 373 | unsigned long flags; |
| 374 | struct vmw_fence_obj *fence, *next_fence; |
| 375 | struct list_head action_list; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 376 | bool needs_rerun; |
| 377 | uint32_t seqno, new_seqno; |
| 378 | __le32 __iomem *fifo_mem = fman->dev_priv->mmio_virt; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 379 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 380 | seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE); |
| 381 | rerun: |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 382 | spin_lock_irqsave(&fman->lock, flags); |
| 383 | list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) { |
| 384 | if (seqno - fence->seqno < VMW_FENCE_WRAP) { |
| 385 | list_del_init(&fence->head); |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 386 | fence->signaled = 1; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 387 | INIT_LIST_HEAD(&action_list); |
| 388 | list_splice_init(&fence->seq_passed_actions, |
| 389 | &action_list); |
| 390 | vmw_fences_perform_actions(fman, &action_list); |
| 391 | wake_up_all(&fence->queue); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 392 | } else |
| 393 | break; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 394 | } |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 395 | |
| 396 | needs_rerun = vmw_fence_goal_new_locked(fman, seqno); |
| 397 | |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 398 | if (!list_empty(&fman->cleanup_list)) |
| 399 | (void) schedule_work(&fman->work); |
| 400 | spin_unlock_irqrestore(&fman->lock, flags); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 401 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 402 | /* |
| 403 | * Rerun if the fence goal seqno was updated, and the |
| 404 | * hardware might have raced with that update, so that |
| 405 | * we missed a fence_goal irq. |
| 406 | */ |
| 407 | |
| 408 | if (unlikely(needs_rerun)) { |
| 409 | new_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE); |
| 410 | if (new_seqno != seqno) { |
| 411 | seqno = new_seqno; |
| 412 | goto rerun; |
| 413 | } |
| 414 | } |
| 415 | } |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 416 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 417 | bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence) |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 418 | { |
| 419 | struct vmw_fence_manager *fman = fence->fman; |
| 420 | unsigned long irq_flags; |
| 421 | uint32_t signaled; |
| 422 | |
| 423 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 424 | signaled = fence->signaled; |
| 425 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 426 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 427 | if (signaled) |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 428 | return 1; |
| 429 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 430 | vmw_fences_update(fman); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 431 | |
| 432 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 433 | signaled = fence->signaled; |
| 434 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 435 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 436 | return signaled; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 439 | int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 440 | bool interruptible, unsigned long timeout) |
| 441 | { |
| 442 | struct vmw_private *dev_priv = fence->fman->dev_priv; |
| 443 | long ret; |
| 444 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 445 | if (likely(vmw_fence_obj_signaled(fence))) |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 446 | return 0; |
| 447 | |
| 448 | vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC); |
| 449 | vmw_seqno_waiter_add(dev_priv); |
| 450 | |
| 451 | if (interruptible) |
| 452 | ret = wait_event_interruptible_timeout |
| 453 | (fence->queue, |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 454 | vmw_fence_obj_signaled(fence), |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 455 | timeout); |
| 456 | else |
| 457 | ret = wait_event_timeout |
| 458 | (fence->queue, |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 459 | vmw_fence_obj_signaled(fence), |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 460 | timeout); |
| 461 | |
| 462 | vmw_seqno_waiter_remove(dev_priv); |
| 463 | |
| 464 | if (unlikely(ret == 0)) |
| 465 | ret = -EBUSY; |
| 466 | else if (likely(ret > 0)) |
| 467 | ret = 0; |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | void vmw_fence_obj_flush(struct vmw_fence_obj *fence) |
| 473 | { |
| 474 | struct vmw_private *dev_priv = fence->fman->dev_priv; |
| 475 | |
| 476 | vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC); |
| 477 | } |
| 478 | |
| 479 | static void vmw_fence_destroy(struct vmw_fence_obj *fence) |
| 480 | { |
| 481 | struct vmw_fence_manager *fman = fence->fman; |
| 482 | |
| 483 | kfree(fence); |
| 484 | /* |
| 485 | * Free kernel space accounting. |
| 486 | */ |
| 487 | ttm_mem_global_free(vmw_mem_glob(fman->dev_priv), |
| 488 | fman->fence_size); |
| 489 | } |
| 490 | |
| 491 | int vmw_fence_create(struct vmw_fence_manager *fman, |
| 492 | uint32_t seqno, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 493 | struct vmw_fence_obj **p_fence) |
| 494 | { |
| 495 | struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv); |
| 496 | struct vmw_fence_obj *fence; |
| 497 | int ret; |
| 498 | |
| 499 | ret = ttm_mem_global_alloc(mem_glob, fman->fence_size, |
| 500 | false, false); |
| 501 | if (unlikely(ret != 0)) |
| 502 | return ret; |
| 503 | |
| 504 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 505 | if (unlikely(fence == NULL)) { |
| 506 | ret = -ENOMEM; |
| 507 | goto out_no_object; |
| 508 | } |
| 509 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 510 | ret = vmw_fence_obj_init(fman, fence, seqno, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 511 | vmw_fence_destroy); |
| 512 | if (unlikely(ret != 0)) |
| 513 | goto out_err_init; |
| 514 | |
| 515 | *p_fence = fence; |
| 516 | return 0; |
| 517 | |
| 518 | out_err_init: |
| 519 | kfree(fence); |
| 520 | out_no_object: |
| 521 | ttm_mem_global_free(mem_glob, fman->fence_size); |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | static void vmw_user_fence_destroy(struct vmw_fence_obj *fence) |
| 527 | { |
| 528 | struct vmw_user_fence *ufence = |
| 529 | container_of(fence, struct vmw_user_fence, fence); |
| 530 | struct vmw_fence_manager *fman = fence->fman; |
| 531 | |
Thomas Hellstrom | 35f62a5 | 2012-11-20 12:16:49 +0000 | [diff] [blame] | 532 | ttm_base_object_kfree(ufence, base); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 533 | /* |
| 534 | * Free kernel space accounting. |
| 535 | */ |
| 536 | ttm_mem_global_free(vmw_mem_glob(fman->dev_priv), |
| 537 | fman->user_fence_size); |
| 538 | } |
| 539 | |
| 540 | static void vmw_user_fence_base_release(struct ttm_base_object **p_base) |
| 541 | { |
| 542 | struct ttm_base_object *base = *p_base; |
| 543 | struct vmw_user_fence *ufence = |
| 544 | container_of(base, struct vmw_user_fence, base); |
| 545 | struct vmw_fence_obj *fence = &ufence->fence; |
| 546 | |
| 547 | *p_base = NULL; |
| 548 | vmw_fence_obj_unreference(&fence); |
| 549 | } |
| 550 | |
| 551 | int vmw_user_fence_create(struct drm_file *file_priv, |
| 552 | struct vmw_fence_manager *fman, |
| 553 | uint32_t seqno, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 554 | struct vmw_fence_obj **p_fence, |
| 555 | uint32_t *p_handle) |
| 556 | { |
| 557 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
| 558 | struct vmw_user_fence *ufence; |
| 559 | struct vmw_fence_obj *tmp; |
| 560 | struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv); |
| 561 | int ret; |
| 562 | |
| 563 | /* |
| 564 | * Kernel memory space accounting, since this object may |
| 565 | * be created by a user-space request. |
| 566 | */ |
| 567 | |
| 568 | ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size, |
| 569 | false, false); |
| 570 | if (unlikely(ret != 0)) |
| 571 | return ret; |
| 572 | |
| 573 | ufence = kzalloc(sizeof(*ufence), GFP_KERNEL); |
| 574 | if (unlikely(ufence == NULL)) { |
| 575 | ret = -ENOMEM; |
| 576 | goto out_no_object; |
| 577 | } |
| 578 | |
| 579 | ret = vmw_fence_obj_init(fman, &ufence->fence, seqno, |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 580 | vmw_user_fence_destroy); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 581 | if (unlikely(ret != 0)) { |
| 582 | kfree(ufence); |
| 583 | goto out_no_object; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * The base object holds a reference which is freed in |
| 588 | * vmw_user_fence_base_release. |
| 589 | */ |
| 590 | tmp = vmw_fence_obj_reference(&ufence->fence); |
| 591 | ret = ttm_base_object_init(tfile, &ufence->base, false, |
| 592 | VMW_RES_FENCE, |
| 593 | &vmw_user_fence_base_release, NULL); |
| 594 | |
| 595 | |
| 596 | if (unlikely(ret != 0)) { |
| 597 | /* |
| 598 | * Free the base object's reference |
| 599 | */ |
| 600 | vmw_fence_obj_unreference(&tmp); |
| 601 | goto out_err; |
| 602 | } |
| 603 | |
| 604 | *p_fence = &ufence->fence; |
| 605 | *p_handle = ufence->base.hash.key; |
| 606 | |
| 607 | return 0; |
| 608 | out_err: |
| 609 | tmp = &ufence->fence; |
| 610 | vmw_fence_obj_unreference(&tmp); |
| 611 | out_no_object: |
| 612 | ttm_mem_global_free(mem_glob, fman->user_fence_size); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /** |
| 618 | * vmw_fence_fifo_down - signal all unsignaled fence objects. |
| 619 | */ |
| 620 | |
| 621 | void vmw_fence_fifo_down(struct vmw_fence_manager *fman) |
| 622 | { |
| 623 | unsigned long irq_flags; |
| 624 | struct list_head action_list; |
| 625 | int ret; |
| 626 | |
| 627 | /* |
| 628 | * The list may be altered while we traverse it, so always |
| 629 | * restart when we've released the fman->lock. |
| 630 | */ |
| 631 | |
| 632 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 633 | fman->fifo_down = true; |
| 634 | while (!list_empty(&fman->fence_list)) { |
| 635 | struct vmw_fence_obj *fence = |
| 636 | list_entry(fman->fence_list.prev, struct vmw_fence_obj, |
| 637 | head); |
| 638 | kref_get(&fence->kref); |
| 639 | spin_unlock_irq(&fman->lock); |
| 640 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 641 | ret = vmw_fence_obj_wait(fence, false, false, |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 642 | VMW_FENCE_WAIT_TIMEOUT); |
| 643 | |
| 644 | if (unlikely(ret != 0)) { |
| 645 | list_del_init(&fence->head); |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 646 | fence->signaled = 1; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 647 | INIT_LIST_HEAD(&action_list); |
| 648 | list_splice_init(&fence->seq_passed_actions, |
| 649 | &action_list); |
| 650 | vmw_fences_perform_actions(fman, &action_list); |
| 651 | wake_up_all(&fence->queue); |
| 652 | } |
| 653 | |
| 654 | spin_lock_irq(&fman->lock); |
| 655 | |
| 656 | BUG_ON(!list_empty(&fence->head)); |
| 657 | kref_put(&fence->kref, vmw_fence_obj_destroy_locked); |
| 658 | } |
| 659 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 660 | } |
| 661 | |
| 662 | void vmw_fence_fifo_up(struct vmw_fence_manager *fman) |
| 663 | { |
| 664 | unsigned long irq_flags; |
| 665 | |
| 666 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 667 | fman->fifo_down = false; |
| 668 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data, |
| 673 | struct drm_file *file_priv) |
| 674 | { |
| 675 | struct drm_vmw_fence_wait_arg *arg = |
| 676 | (struct drm_vmw_fence_wait_arg *)data; |
| 677 | unsigned long timeout; |
| 678 | struct ttm_base_object *base; |
| 679 | struct vmw_fence_obj *fence; |
| 680 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
| 681 | int ret; |
| 682 | uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ); |
| 683 | |
| 684 | /* |
| 685 | * 64-bit division not present on 32-bit systems, so do an |
| 686 | * approximation. (Divide by 1000000). |
| 687 | */ |
| 688 | |
| 689 | wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) - |
| 690 | (wait_timeout >> 26); |
| 691 | |
| 692 | if (!arg->cookie_valid) { |
| 693 | arg->cookie_valid = 1; |
| 694 | arg->kernel_cookie = jiffies + wait_timeout; |
| 695 | } |
| 696 | |
| 697 | base = ttm_base_object_lookup(tfile, arg->handle); |
| 698 | if (unlikely(base == NULL)) { |
| 699 | printk(KERN_ERR "Wait invalid fence object handle " |
| 700 | "0x%08lx.\n", |
| 701 | (unsigned long)arg->handle); |
| 702 | return -EINVAL; |
| 703 | } |
| 704 | |
| 705 | fence = &(container_of(base, struct vmw_user_fence, base)->fence); |
| 706 | |
| 707 | timeout = jiffies; |
| 708 | if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) { |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 709 | ret = ((vmw_fence_obj_signaled(fence)) ? |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 710 | 0 : -EBUSY); |
| 711 | goto out; |
| 712 | } |
| 713 | |
| 714 | timeout = (unsigned long)arg->kernel_cookie - timeout; |
| 715 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 716 | ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 717 | |
| 718 | out: |
| 719 | ttm_base_object_unref(&base); |
| 720 | |
| 721 | /* |
| 722 | * Optionally unref the fence object. |
| 723 | */ |
| 724 | |
| 725 | if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF)) |
| 726 | return ttm_ref_object_base_unref(tfile, arg->handle, |
| 727 | TTM_REF_USAGE); |
| 728 | return ret; |
| 729 | } |
| 730 | |
| 731 | int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data, |
| 732 | struct drm_file *file_priv) |
| 733 | { |
| 734 | struct drm_vmw_fence_signaled_arg *arg = |
| 735 | (struct drm_vmw_fence_signaled_arg *) data; |
| 736 | struct ttm_base_object *base; |
| 737 | struct vmw_fence_obj *fence; |
| 738 | struct vmw_fence_manager *fman; |
| 739 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
| 740 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 741 | |
| 742 | base = ttm_base_object_lookup(tfile, arg->handle); |
| 743 | if (unlikely(base == NULL)) { |
| 744 | printk(KERN_ERR "Fence signaled invalid fence object handle " |
| 745 | "0x%08lx.\n", |
| 746 | (unsigned long)arg->handle); |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | |
| 750 | fence = &(container_of(base, struct vmw_user_fence, base)->fence); |
| 751 | fman = fence->fman; |
| 752 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 753 | arg->signaled = vmw_fence_obj_signaled(fence); |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 754 | spin_lock_irq(&fman->lock); |
| 755 | |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 756 | arg->signaled_flags = arg->flags; |
Thomas Hellstrom | ae2a104 | 2011-09-01 20:18:44 +0000 | [diff] [blame] | 757 | arg->passed_seqno = dev_priv->last_read_seqno; |
| 758 | spin_unlock_irq(&fman->lock); |
| 759 | |
| 760 | ttm_base_object_unref(&base); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | |
| 766 | int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data, |
| 767 | struct drm_file *file_priv) |
| 768 | { |
| 769 | struct drm_vmw_fence_arg *arg = |
| 770 | (struct drm_vmw_fence_arg *) data; |
| 771 | |
| 772 | return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, |
| 773 | arg->handle, |
| 774 | TTM_REF_USAGE); |
| 775 | } |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 776 | |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 777 | /** |
| 778 | * vmw_event_fence_fpriv_gone - Remove references to struct drm_file objects |
| 779 | * |
| 780 | * @fman: Pointer to a struct vmw_fence_manager |
| 781 | * @event_list: Pointer to linked list of struct vmw_event_fence_action objects |
| 782 | * with pointers to a struct drm_file object about to be closed. |
| 783 | * |
| 784 | * This function removes all pending fence events with references to a |
| 785 | * specific struct drm_file object about to be closed. The caller is required |
| 786 | * to pass a list of all struct vmw_event_fence_action objects with such |
| 787 | * events attached. This function is typically called before the |
| 788 | * struct drm_file object's event management is taken down. |
| 789 | */ |
| 790 | void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman, |
| 791 | struct list_head *event_list) |
| 792 | { |
| 793 | struct vmw_event_fence_action *eaction; |
| 794 | struct drm_pending_event *event; |
| 795 | unsigned long irq_flags; |
| 796 | |
| 797 | while (1) { |
| 798 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 799 | if (list_empty(event_list)) |
| 800 | goto out_unlock; |
| 801 | eaction = list_first_entry(event_list, |
| 802 | struct vmw_event_fence_action, |
| 803 | fpriv_head); |
| 804 | list_del_init(&eaction->fpriv_head); |
| 805 | event = eaction->event; |
| 806 | eaction->event = NULL; |
| 807 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 808 | event->destroy(event); |
| 809 | } |
| 810 | out_unlock: |
| 811 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 812 | } |
| 813 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 814 | |
| 815 | /** |
| 816 | * vmw_event_fence_action_seq_passed |
| 817 | * |
| 818 | * @action: The struct vmw_fence_action embedded in a struct |
| 819 | * vmw_event_fence_action. |
| 820 | * |
| 821 | * This function is called when the seqno of the fence where @action is |
| 822 | * attached has passed. It queues the event on the submitter's event list. |
| 823 | * This function is always called from atomic context, and may be called |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 824 | * from irq context. |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 825 | */ |
| 826 | static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action) |
| 827 | { |
| 828 | struct vmw_event_fence_action *eaction = |
| 829 | container_of(action, struct vmw_event_fence_action, action); |
| 830 | struct drm_device *dev = eaction->dev; |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 831 | struct drm_pending_event *event = eaction->event; |
| 832 | struct drm_file *file_priv; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 833 | unsigned long irq_flags; |
| 834 | |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 835 | if (unlikely(event == NULL)) |
| 836 | return; |
| 837 | |
| 838 | file_priv = event->file_priv; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 839 | spin_lock_irqsave(&dev->event_lock, irq_flags); |
| 840 | |
| 841 | if (likely(eaction->tv_sec != NULL)) { |
| 842 | struct timeval tv; |
| 843 | |
| 844 | do_gettimeofday(&tv); |
| 845 | *eaction->tv_sec = tv.tv_sec; |
| 846 | *eaction->tv_usec = tv.tv_usec; |
| 847 | } |
| 848 | |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 849 | list_del_init(&eaction->fpriv_head); |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 850 | list_add_tail(&eaction->event->link, &file_priv->event_list); |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 851 | eaction->event = NULL; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 852 | wake_up_all(&file_priv->event_wait); |
| 853 | spin_unlock_irqrestore(&dev->event_lock, irq_flags); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * vmw_event_fence_action_cleanup |
| 858 | * |
| 859 | * @action: The struct vmw_fence_action embedded in a struct |
| 860 | * vmw_event_fence_action. |
| 861 | * |
| 862 | * This function is the struct vmw_fence_action destructor. It's typically |
| 863 | * called from a workqueue. |
| 864 | */ |
| 865 | static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action) |
| 866 | { |
| 867 | struct vmw_event_fence_action *eaction = |
| 868 | container_of(action, struct vmw_event_fence_action, action); |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 869 | struct vmw_fence_manager *fman = eaction->fence->fman; |
| 870 | unsigned long irq_flags; |
| 871 | |
| 872 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 873 | list_del(&eaction->fpriv_head); |
| 874 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 875 | |
| 876 | vmw_fence_obj_unreference(&eaction->fence); |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 877 | kfree(eaction); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | |
| 881 | /** |
| 882 | * vmw_fence_obj_add_action - Add an action to a fence object. |
| 883 | * |
| 884 | * @fence - The fence object. |
| 885 | * @action - The action to add. |
| 886 | * |
| 887 | * Note that the action callbacks may be executed before this function |
| 888 | * returns. |
| 889 | */ |
Rashika Kheria | 94844cf | 2014-01-06 22:21:21 +0530 | [diff] [blame] | 890 | static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence, |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 891 | struct vmw_fence_action *action) |
| 892 | { |
| 893 | struct vmw_fence_manager *fman = fence->fman; |
| 894 | unsigned long irq_flags; |
| 895 | bool run_update = false; |
| 896 | |
| 897 | mutex_lock(&fman->goal_irq_mutex); |
| 898 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 899 | |
| 900 | fman->pending_actions[action->type]++; |
Maarten Lankhorst | c060a4e | 2014-03-26 13:06:24 +0100 | [diff] [blame^] | 901 | if (fence->signaled) { |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 902 | struct list_head action_list; |
| 903 | |
| 904 | INIT_LIST_HEAD(&action_list); |
| 905 | list_add_tail(&action->head, &action_list); |
| 906 | vmw_fences_perform_actions(fman, &action_list); |
| 907 | } else { |
| 908 | list_add_tail(&action->head, &fence->seq_passed_actions); |
| 909 | |
| 910 | /* |
| 911 | * This function may set fman::seqno_valid, so it must |
| 912 | * be run with the goal_irq_mutex held. |
| 913 | */ |
| 914 | run_update = vmw_fence_goal_check_locked(fence); |
| 915 | } |
| 916 | |
| 917 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 918 | |
| 919 | if (run_update) { |
| 920 | if (!fman->goal_irq_on) { |
| 921 | fman->goal_irq_on = true; |
| 922 | vmw_goal_waiter_add(fman->dev_priv); |
| 923 | } |
| 924 | vmw_fences_update(fman); |
| 925 | } |
| 926 | mutex_unlock(&fman->goal_irq_mutex); |
| 927 | |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * vmw_event_fence_action_create - Post an event for sending when a fence |
| 932 | * object seqno has passed. |
| 933 | * |
| 934 | * @file_priv: The file connection on which the event should be posted. |
| 935 | * @fence: The fence object on which to post the event. |
| 936 | * @event: Event to be posted. This event should've been alloced |
| 937 | * using k[mz]alloc, and should've been completely initialized. |
| 938 | * @interruptible: Interruptible waits if possible. |
| 939 | * |
| 940 | * As a side effect, the object pointed to by @event may have been |
| 941 | * freed when this function returns. If this function returns with |
| 942 | * an error code, the caller needs to free that object. |
| 943 | */ |
| 944 | |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 945 | int vmw_event_fence_action_queue(struct drm_file *file_priv, |
| 946 | struct vmw_fence_obj *fence, |
| 947 | struct drm_pending_event *event, |
| 948 | uint32_t *tv_sec, |
| 949 | uint32_t *tv_usec, |
| 950 | bool interruptible) |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 951 | { |
Dan Carpenter | 0c5d370 | 2011-10-18 09:09:45 +0300 | [diff] [blame] | 952 | struct vmw_event_fence_action *eaction; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 953 | struct vmw_fence_manager *fman = fence->fman; |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 954 | struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv); |
| 955 | unsigned long irq_flags; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 956 | |
| 957 | eaction = kzalloc(sizeof(*eaction), GFP_KERNEL); |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 958 | if (unlikely(eaction == NULL)) |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 959 | return -ENOMEM; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 960 | |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 961 | eaction->event = event; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 962 | |
| 963 | eaction->action.seq_passed = vmw_event_fence_action_seq_passed; |
| 964 | eaction->action.cleanup = vmw_event_fence_action_cleanup; |
| 965 | eaction->action.type = VMW_ACTION_EVENT; |
| 966 | |
| 967 | eaction->fence = vmw_fence_obj_reference(fence); |
| 968 | eaction->dev = fman->dev_priv->dev; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 969 | eaction->tv_sec = tv_sec; |
| 970 | eaction->tv_usec = tv_usec; |
| 971 | |
Thomas Hellstrom | 6b82ef5 | 2012-02-09 16:56:42 +0100 | [diff] [blame] | 972 | spin_lock_irqsave(&fman->lock, irq_flags); |
| 973 | list_add_tail(&eaction->fpriv_head, &vmw_fp->fence_events); |
| 974 | spin_unlock_irqrestore(&fman->lock, irq_flags); |
| 975 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 976 | vmw_fence_obj_add_action(fence, &eaction->action); |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 981 | struct vmw_event_fence_pending { |
| 982 | struct drm_pending_event base; |
| 983 | struct drm_vmw_event_fence event; |
| 984 | }; |
| 985 | |
Rashika Kheria | 94844cf | 2014-01-06 22:21:21 +0530 | [diff] [blame] | 986 | static int vmw_event_fence_action_create(struct drm_file *file_priv, |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 987 | struct vmw_fence_obj *fence, |
| 988 | uint32_t flags, |
| 989 | uint64_t user_data, |
| 990 | bool interruptible) |
| 991 | { |
| 992 | struct vmw_event_fence_pending *event; |
| 993 | struct drm_device *dev = fence->fman->dev_priv->dev; |
| 994 | unsigned long irq_flags; |
| 995 | int ret; |
| 996 | |
| 997 | spin_lock_irqsave(&dev->event_lock, irq_flags); |
| 998 | |
| 999 | ret = (file_priv->event_space < sizeof(event->event)) ? -EBUSY : 0; |
| 1000 | if (likely(ret == 0)) |
| 1001 | file_priv->event_space -= sizeof(event->event); |
| 1002 | |
| 1003 | spin_unlock_irqrestore(&dev->event_lock, irq_flags); |
| 1004 | |
| 1005 | if (unlikely(ret != 0)) { |
| 1006 | DRM_ERROR("Failed to allocate event space for this file.\n"); |
| 1007 | goto out_no_space; |
| 1008 | } |
| 1009 | |
| 1010 | |
Dan Carpenter | 68c4fce | 2012-09-23 19:33:55 +0300 | [diff] [blame] | 1011 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 1012 | if (unlikely(event == NULL)) { |
| 1013 | DRM_ERROR("Failed to allocate an event.\n"); |
| 1014 | ret = -ENOMEM; |
| 1015 | goto out_no_event; |
| 1016 | } |
| 1017 | |
| 1018 | event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED; |
| 1019 | event->event.base.length = sizeof(*event); |
| 1020 | event->event.user_data = user_data; |
| 1021 | |
| 1022 | event->base.event = &event->event.base; |
| 1023 | event->base.file_priv = file_priv; |
| 1024 | event->base.destroy = (void (*) (struct drm_pending_event *)) kfree; |
| 1025 | |
| 1026 | |
| 1027 | if (flags & DRM_VMW_FE_FLAG_REQ_TIME) |
| 1028 | ret = vmw_event_fence_action_queue(file_priv, fence, |
| 1029 | &event->base, |
| 1030 | &event->event.tv_sec, |
| 1031 | &event->event.tv_usec, |
| 1032 | interruptible); |
| 1033 | else |
| 1034 | ret = vmw_event_fence_action_queue(file_priv, fence, |
| 1035 | &event->base, |
| 1036 | NULL, |
| 1037 | NULL, |
| 1038 | interruptible); |
| 1039 | if (ret != 0) |
| 1040 | goto out_no_queue; |
| 1041 | |
| 1042 | out_no_queue: |
| 1043 | event->base.destroy(&event->base); |
| 1044 | out_no_event: |
| 1045 | spin_lock_irqsave(&dev->event_lock, irq_flags); |
| 1046 | file_priv->event_space += sizeof(*event); |
| 1047 | spin_unlock_irqrestore(&dev->event_lock, irq_flags); |
| 1048 | out_no_space: |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1052 | int vmw_fence_event_ioctl(struct drm_device *dev, void *data, |
| 1053 | struct drm_file *file_priv) |
| 1054 | { |
| 1055 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1056 | struct drm_vmw_fence_event_arg *arg = |
| 1057 | (struct drm_vmw_fence_event_arg *) data; |
| 1058 | struct vmw_fence_obj *fence = NULL; |
| 1059 | struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv); |
| 1060 | struct drm_vmw_fence_rep __user *user_fence_rep = |
| 1061 | (struct drm_vmw_fence_rep __user *)(unsigned long) |
| 1062 | arg->fence_rep; |
| 1063 | uint32_t handle; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1064 | int ret; |
| 1065 | |
| 1066 | /* |
| 1067 | * Look up an existing fence object, |
| 1068 | * and if user-space wants a new reference, |
| 1069 | * add one. |
| 1070 | */ |
| 1071 | if (arg->handle) { |
| 1072 | struct ttm_base_object *base = |
Thomas Hellstrom | 05efb1a | 2013-12-18 14:13:29 +0100 | [diff] [blame] | 1073 | ttm_base_object_lookup_for_ref(dev_priv->tdev, |
| 1074 | arg->handle); |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1075 | |
| 1076 | if (unlikely(base == NULL)) { |
| 1077 | DRM_ERROR("Fence event invalid fence object handle " |
| 1078 | "0x%08lx.\n", |
| 1079 | (unsigned long)arg->handle); |
| 1080 | return -EINVAL; |
| 1081 | } |
| 1082 | fence = &(container_of(base, struct vmw_user_fence, |
| 1083 | base)->fence); |
| 1084 | (void) vmw_fence_obj_reference(fence); |
| 1085 | |
| 1086 | if (user_fence_rep != NULL) { |
| 1087 | bool existed; |
| 1088 | |
| 1089 | ret = ttm_ref_object_add(vmw_fp->tfile, base, |
| 1090 | TTM_REF_USAGE, &existed); |
| 1091 | if (unlikely(ret != 0)) { |
| 1092 | DRM_ERROR("Failed to reference a fence " |
| 1093 | "object.\n"); |
| 1094 | goto out_no_ref_obj; |
| 1095 | } |
| 1096 | handle = base->hash.key; |
| 1097 | } |
| 1098 | ttm_base_object_unref(&base); |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Create a new fence object. |
| 1103 | */ |
| 1104 | if (!fence) { |
| 1105 | ret = vmw_execbuf_fence_commands(file_priv, dev_priv, |
| 1106 | &fence, |
| 1107 | (user_fence_rep) ? |
| 1108 | &handle : NULL); |
| 1109 | if (unlikely(ret != 0)) { |
| 1110 | DRM_ERROR("Fence event failed to create fence.\n"); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | BUG_ON(fence == NULL); |
| 1116 | |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1117 | if (arg->flags & DRM_VMW_FE_FLAG_REQ_TIME) |
| 1118 | ret = vmw_event_fence_action_create(file_priv, fence, |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 1119 | arg->flags, |
| 1120 | arg->user_data, |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1121 | true); |
| 1122 | else |
| 1123 | ret = vmw_event_fence_action_create(file_priv, fence, |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 1124 | arg->flags, |
| 1125 | arg->user_data, |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1126 | true); |
| 1127 | |
| 1128 | if (unlikely(ret != 0)) { |
| 1129 | if (ret != -ERESTARTSYS) |
| 1130 | DRM_ERROR("Failed to attach event to fence.\n"); |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 1131 | goto out_no_create; |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence, |
| 1135 | handle); |
| 1136 | vmw_fence_obj_unreference(&fence); |
| 1137 | return 0; |
Jakob Bornecrantz | 8b7de6a | 2012-02-09 16:56:41 +0100 | [diff] [blame] | 1138 | out_no_create: |
Thomas Hellstrom | 57c5ee7 | 2011-10-10 12:23:26 +0200 | [diff] [blame] | 1139 | if (user_fence_rep != NULL) |
| 1140 | ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, |
| 1141 | handle, TTM_REF_USAGE); |
| 1142 | out_no_ref_obj: |
| 1143 | vmw_fence_obj_unreference(&fence); |
| 1144 | return ret; |
| 1145 | } |