Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* drm_lists.h -- Buffer list handling routines -*- linux-c -*- |
| 2 | * Created: Mon Apr 19 20:54:22 1999 by faith@valinux.com |
| 3 | * |
| 4 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 5 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice (including the next |
| 16 | * paragraph) shall be included in all copies or substantial portions of the |
| 17 | * Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 23 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 24 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 | * OTHER DEALINGS IN THE SOFTWARE. |
| 26 | * |
| 27 | * Authors: |
| 28 | * Rickard E. (Rik) Faith <faith@valinux.com> |
| 29 | * Gareth Hughes <gareth@valinux.com> |
| 30 | */ |
| 31 | |
| 32 | #include "drmP.h" |
| 33 | |
| 34 | |
| 35 | int DRM(waitlist_create)(drm_waitlist_t *bl, int count) |
| 36 | { |
| 37 | if (bl->count) return -EINVAL; |
| 38 | |
| 39 | bl->bufs = DRM(alloc)((bl->count + 2) * sizeof(*bl->bufs), |
| 40 | DRM_MEM_BUFLISTS); |
| 41 | |
| 42 | if(!bl->bufs) return -ENOMEM; |
| 43 | memset(bl->bufs, 0, sizeof(*bl->bufs)); |
| 44 | bl->count = count; |
| 45 | bl->rp = bl->bufs; |
| 46 | bl->wp = bl->bufs; |
| 47 | bl->end = &bl->bufs[bl->count+1]; |
| 48 | spin_lock_init(&bl->write_lock); |
| 49 | spin_lock_init(&bl->read_lock); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int DRM(waitlist_destroy)(drm_waitlist_t *bl) |
| 54 | { |
| 55 | if (bl->rp != bl->wp) return -EINVAL; |
| 56 | if (bl->bufs) DRM(free)(bl->bufs, |
| 57 | (bl->count + 2) * sizeof(*bl->bufs), |
| 58 | DRM_MEM_BUFLISTS); |
| 59 | bl->count = 0; |
| 60 | bl->bufs = NULL; |
| 61 | bl->rp = NULL; |
| 62 | bl->wp = NULL; |
| 63 | bl->end = NULL; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int DRM(waitlist_put)(drm_waitlist_t *bl, drm_buf_t *buf) |
| 68 | { |
| 69 | int left; |
| 70 | unsigned long flags; |
| 71 | |
| 72 | left = DRM_LEFTCOUNT(bl); |
| 73 | if (!left) { |
| 74 | DRM_ERROR("Overflow while adding buffer %d from filp %p\n", |
| 75 | buf->idx, buf->filp); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | buf->list = DRM_LIST_WAIT; |
| 79 | |
| 80 | spin_lock_irqsave(&bl->write_lock, flags); |
| 81 | *bl->wp = buf; |
| 82 | if (++bl->wp >= bl->end) bl->wp = bl->bufs; |
| 83 | spin_unlock_irqrestore(&bl->write_lock, flags); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | drm_buf_t *DRM(waitlist_get)(drm_waitlist_t *bl) |
| 89 | { |
| 90 | drm_buf_t *buf; |
| 91 | unsigned long flags; |
| 92 | |
| 93 | spin_lock_irqsave(&bl->read_lock, flags); |
| 94 | buf = *bl->rp; |
| 95 | if (bl->rp == bl->wp) { |
| 96 | spin_unlock_irqrestore(&bl->read_lock, flags); |
| 97 | return NULL; |
| 98 | } |
| 99 | if (++bl->rp >= bl->end) bl->rp = bl->bufs; |
| 100 | spin_unlock_irqrestore(&bl->read_lock, flags); |
| 101 | |
| 102 | return buf; |
| 103 | } |
| 104 | |
| 105 | int DRM(freelist_create)(drm_freelist_t *bl, int count) |
| 106 | { |
| 107 | atomic_set(&bl->count, 0); |
| 108 | bl->next = NULL; |
| 109 | init_waitqueue_head(&bl->waiting); |
| 110 | bl->low_mark = 0; |
| 111 | bl->high_mark = 0; |
| 112 | atomic_set(&bl->wfh, 0); |
| 113 | spin_lock_init(&bl->lock); |
| 114 | ++bl->initialized; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int DRM(freelist_destroy)(drm_freelist_t *bl) |
| 119 | { |
| 120 | atomic_set(&bl->count, 0); |
| 121 | bl->next = NULL; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int DRM(freelist_put)(drm_device_t *dev, drm_freelist_t *bl, drm_buf_t *buf) |
| 126 | { |
| 127 | drm_device_dma_t *dma = dev->dma; |
| 128 | |
| 129 | if (!dma) { |
| 130 | DRM_ERROR("No DMA support\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | if (buf->waiting || buf->pending || buf->list == DRM_LIST_FREE) { |
| 135 | DRM_ERROR("Freed buffer %d: w%d, p%d, l%d\n", |
| 136 | buf->idx, buf->waiting, buf->pending, buf->list); |
| 137 | } |
| 138 | if (!bl) return 1; |
| 139 | buf->list = DRM_LIST_FREE; |
| 140 | |
| 141 | spin_lock(&bl->lock); |
| 142 | buf->next = bl->next; |
| 143 | bl->next = buf; |
| 144 | spin_unlock(&bl->lock); |
| 145 | |
| 146 | atomic_inc(&bl->count); |
| 147 | if (atomic_read(&bl->count) > dma->buf_count) { |
| 148 | DRM_ERROR("%d of %d buffers free after addition of %d\n", |
| 149 | atomic_read(&bl->count), dma->buf_count, buf->idx); |
| 150 | return 1; |
| 151 | } |
| 152 | /* Check for high water mark */ |
| 153 | if (atomic_read(&bl->wfh) && atomic_read(&bl->count)>=bl->high_mark) { |
| 154 | atomic_set(&bl->wfh, 0); |
| 155 | wake_up_interruptible(&bl->waiting); |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static drm_buf_t *DRM(freelist_try)(drm_freelist_t *bl) |
| 161 | { |
| 162 | drm_buf_t *buf; |
| 163 | |
| 164 | if (!bl) return NULL; |
| 165 | |
| 166 | /* Get buffer */ |
| 167 | spin_lock(&bl->lock); |
| 168 | if (!bl->next) { |
| 169 | spin_unlock(&bl->lock); |
| 170 | return NULL; |
| 171 | } |
| 172 | buf = bl->next; |
| 173 | bl->next = bl->next->next; |
| 174 | spin_unlock(&bl->lock); |
| 175 | |
| 176 | atomic_dec(&bl->count); |
| 177 | buf->next = NULL; |
| 178 | buf->list = DRM_LIST_NONE; |
| 179 | if (buf->waiting || buf->pending) { |
| 180 | DRM_ERROR("Free buffer %d: w%d, p%d, l%d\n", |
| 181 | buf->idx, buf->waiting, buf->pending, buf->list); |
| 182 | } |
| 183 | |
| 184 | return buf; |
| 185 | } |
| 186 | |
| 187 | drm_buf_t *DRM(freelist_get)(drm_freelist_t *bl, int block) |
| 188 | { |
| 189 | drm_buf_t *buf = NULL; |
| 190 | DECLARE_WAITQUEUE(entry, current); |
| 191 | |
| 192 | if (!bl || !bl->initialized) return NULL; |
| 193 | |
| 194 | /* Check for low water mark */ |
| 195 | if (atomic_read(&bl->count) <= bl->low_mark) /* Became low */ |
| 196 | atomic_set(&bl->wfh, 1); |
| 197 | if (atomic_read(&bl->wfh)) { |
| 198 | if (block) { |
| 199 | add_wait_queue(&bl->waiting, &entry); |
| 200 | for (;;) { |
| 201 | current->state = TASK_INTERRUPTIBLE; |
| 202 | if (!atomic_read(&bl->wfh) |
| 203 | && (buf = DRM(freelist_try)(bl))) break; |
| 204 | schedule(); |
| 205 | if (signal_pending(current)) break; |
| 206 | } |
| 207 | current->state = TASK_RUNNING; |
| 208 | remove_wait_queue(&bl->waiting, &entry); |
| 209 | } |
| 210 | return buf; |
| 211 | } |
| 212 | |
| 213 | return DRM(freelist_try)(bl); |
| 214 | } |
| 215 | |