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