Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* mga_dma.c -- DMA support for mga g200/g400 -*- linux-c -*- |
| 2 | * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.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 | * PRECISION INSIGHT 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 OTHER |
| 25 | * DEALINGS IN THE SOFTWARE. |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * \file mga_dma.c |
| 30 | * DMA support for MGA G200 / G400. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 31 | * |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 32 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 33 | * \author Jeff Hartmann <jhartmann@valinux.com> |
| 34 | * \author Keith Whitwell <keith@tungstengraphics.com> |
| 35 | * \author Gareth Hughes <gareth@valinux.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include "drmP.h" |
| 39 | #include "drm.h" |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 40 | #include "drm_sarea.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "mga_drm.h" |
| 42 | #include "mga_drv.h" |
| 43 | |
| 44 | #define MGA_DEFAULT_USEC_TIMEOUT 10000 |
| 45 | #define MGA_FREELIST_DEBUG 0 |
| 46 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 47 | #define MINIMAL_CLEANUP 0 |
| 48 | #define FULL_CLEANUP 1 |
| 49 | static int mga_do_cleanup_dma(drm_device_t *dev, int full_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* ================================================================ |
| 52 | * Engine control |
| 53 | */ |
| 54 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 55 | int mga_do_wait_for_idle(drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | u32 status = 0; |
| 58 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 59 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 61 | for (i = 0; i < dev_priv->usec_timeout; i++) { |
| 62 | status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK; |
| 63 | if (status == MGA_ENDPRDMASTS) { |
| 64 | MGA_WRITE8(MGA_CRTC_INDEX, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 67 | DRM_UDELAY(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #if MGA_DMA_DEBUG |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 71 | DRM_ERROR("failed!\n"); |
| 72 | DRM_INFO(" status=0x%08x\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #endif |
| 74 | return DRM_ERR(EBUSY); |
| 75 | } |
| 76 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 77 | static int mga_do_dma_reset(drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv; |
| 80 | drm_mga_primary_buffer_t *primary = &dev_priv->prim; |
| 81 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 82 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* The primary DMA stream should look like new right about now. |
| 85 | */ |
| 86 | primary->tail = 0; |
| 87 | primary->space = primary->size; |
| 88 | primary->last_flush = 0; |
| 89 | |
| 90 | sarea_priv->last_wrap = 0; |
| 91 | |
| 92 | /* FIXME: Reset counters, buffer ages etc... |
| 93 | */ |
| 94 | |
| 95 | /* FIXME: What else do we need to reinitialize? WARP stuff? |
| 96 | */ |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* ================================================================ |
| 102 | * Primary DMA stream |
| 103 | */ |
| 104 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 105 | void mga_do_dma_flush(drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | drm_mga_primary_buffer_t *primary = &dev_priv->prim; |
| 108 | u32 head, tail; |
| 109 | u32 status = 0; |
| 110 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 111 | DMA_LOCALS; |
| 112 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 114 | /* We need to wait so that we can do an safe flush */ |
| 115 | for (i = 0; i < dev_priv->usec_timeout; i++) { |
| 116 | status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK; |
| 117 | if (status == MGA_ENDPRDMASTS) |
| 118 | break; |
| 119 | DRM_UDELAY(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 122 | if (primary->tail == primary->last_flush) { |
| 123 | DRM_DEBUG(" bailing out...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | tail = primary->tail + dev_priv->primary->offset; |
| 128 | |
| 129 | /* We need to pad the stream between flushes, as the card |
| 130 | * actually (partially?) reads the first of these commands. |
| 131 | * See page 4-16 in the G400 manual, middle of the page or so. |
| 132 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 133 | BEGIN_DMA(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 135 | DMA_BLOCK(MGA_DMAPAD, 0x00000000, |
| 136 | MGA_DMAPAD, 0x00000000, |
| 137 | MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | ADVANCE_DMA(); |
| 140 | |
| 141 | primary->last_flush = primary->tail; |
| 142 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 143 | head = MGA_READ(MGA_PRIMADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 145 | if (head <= tail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | primary->space = primary->size - primary->tail; |
| 147 | } else { |
| 148 | primary->space = head - tail; |
| 149 | } |
| 150 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 151 | DRM_DEBUG(" head = 0x%06lx\n", head - dev_priv->primary->offset); |
| 152 | DRM_DEBUG(" tail = 0x%06lx\n", tail - dev_priv->primary->offset); |
| 153 | DRM_DEBUG(" space = 0x%06x\n", primary->space); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | mga_flush_write_combine(); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 156 | MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 158 | DRM_DEBUG("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 161 | void mga_do_dma_wrap_start(drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | drm_mga_primary_buffer_t *primary = &dev_priv->prim; |
| 164 | u32 head, tail; |
| 165 | DMA_LOCALS; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 166 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | BEGIN_DMA_WRAP(); |
| 169 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 170 | DMA_BLOCK(MGA_DMAPAD, 0x00000000, |
| 171 | MGA_DMAPAD, 0x00000000, |
| 172 | MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | ADVANCE_DMA(); |
| 175 | |
| 176 | tail = primary->tail + dev_priv->primary->offset; |
| 177 | |
| 178 | primary->tail = 0; |
| 179 | primary->last_flush = 0; |
| 180 | primary->last_wrap++; |
| 181 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 182 | head = MGA_READ(MGA_PRIMADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 184 | if (head == dev_priv->primary->offset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | primary->space = primary->size; |
| 186 | } else { |
| 187 | primary->space = head - dev_priv->primary->offset; |
| 188 | } |
| 189 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 190 | DRM_DEBUG(" head = 0x%06lx\n", head - dev_priv->primary->offset); |
| 191 | DRM_DEBUG(" tail = 0x%06x\n", primary->tail); |
| 192 | DRM_DEBUG(" wrap = %d\n", primary->last_wrap); |
| 193 | DRM_DEBUG(" space = 0x%06x\n", primary->space); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | mga_flush_write_combine(); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 196 | MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 198 | set_bit(0, &primary->wrapped); |
| 199 | DRM_DEBUG("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 202 | void mga_do_dma_wrap_end(drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | drm_mga_primary_buffer_t *primary = &dev_priv->prim; |
| 205 | drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv; |
| 206 | u32 head = dev_priv->primary->offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 207 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | sarea_priv->last_wrap++; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 210 | DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | mga_flush_write_combine(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 213 | MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 215 | clear_bit(0, &primary->wrapped); |
| 216 | DRM_DEBUG("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* ================================================================ |
| 220 | * Freelist management |
| 221 | */ |
| 222 | |
| 223 | #define MGA_BUFFER_USED ~0 |
| 224 | #define MGA_BUFFER_FREE 0 |
| 225 | |
| 226 | #if MGA_FREELIST_DEBUG |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 227 | static void mga_freelist_print(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | drm_mga_private_t *dev_priv = dev->dev_private; |
| 230 | drm_mga_freelist_t *entry; |
| 231 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 232 | DRM_INFO("\n"); |
| 233 | DRM_INFO("current dispatch: last=0x%x done=0x%x\n", |
| 234 | dev_priv->sarea_priv->last_dispatch, |
| 235 | (unsigned int)(MGA_READ(MGA_PRIMADDRESS) - |
| 236 | dev_priv->primary->offset)); |
| 237 | DRM_INFO("current freelist:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 239 | for (entry = dev_priv->head->next; entry; entry = entry->next) { |
| 240 | DRM_INFO(" %p idx=%2d age=0x%x 0x%06lx\n", |
| 241 | entry, entry->buf->idx, entry->age.head, |
| 242 | entry->age.head - dev_priv->primary->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 244 | DRM_INFO("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | #endif |
| 247 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 248 | static int mga_freelist_init(drm_device_t * dev, drm_mga_private_t * dev_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | drm_device_dma_t *dma = dev->dma; |
| 251 | drm_buf_t *buf; |
| 252 | drm_mga_buf_priv_t *buf_priv; |
| 253 | drm_mga_freelist_t *entry; |
| 254 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 255 | DRM_DEBUG("count=%d\n", dma->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 257 | dev_priv->head = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER); |
| 258 | if (dev_priv->head == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return DRM_ERR(ENOMEM); |
| 260 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 261 | memset(dev_priv->head, 0, sizeof(drm_mga_freelist_t)); |
| 262 | SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 264 | for (i = 0; i < dma->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | buf = dma->buflist[i]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 266 | buf_priv = buf->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 268 | entry = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER); |
| 269 | if (entry == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return DRM_ERR(ENOMEM); |
| 271 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 272 | memset(entry, 0, sizeof(drm_mga_freelist_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | entry->next = dev_priv->head->next; |
| 275 | entry->prev = dev_priv->head; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 276 | SET_AGE(&entry->age, MGA_BUFFER_FREE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | entry->buf = buf; |
| 278 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 279 | if (dev_priv->head->next != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | dev_priv->head->next->prev = entry; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 281 | if (entry->next == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | dev_priv->tail = entry; |
| 283 | |
| 284 | buf_priv->list_entry = entry; |
| 285 | buf_priv->discard = 0; |
| 286 | buf_priv->dispatched = 0; |
| 287 | |
| 288 | dev_priv->head->next = entry; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 294 | static void mga_freelist_cleanup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
| 296 | drm_mga_private_t *dev_priv = dev->dev_private; |
| 297 | drm_mga_freelist_t *entry; |
| 298 | drm_mga_freelist_t *next; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 299 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | entry = dev_priv->head; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 302 | while (entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | next = entry->next; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 304 | drm_free(entry, sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | entry = next; |
| 306 | } |
| 307 | |
| 308 | dev_priv->head = dev_priv->tail = NULL; |
| 309 | } |
| 310 | |
| 311 | #if 0 |
| 312 | /* FIXME: Still needed? |
| 313 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 314 | static void mga_freelist_reset(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | drm_device_dma_t *dma = dev->dma; |
| 317 | drm_buf_t *buf; |
| 318 | drm_mga_buf_priv_t *buf_priv; |
| 319 | int i; |
| 320 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 321 | for (i = 0; i < dma->buf_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | buf = dma->buflist[i]; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 323 | buf_priv = buf->dev_private; |
| 324 | SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | #endif |
| 328 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 329 | static drm_buf_t *mga_freelist_get(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
| 331 | drm_mga_private_t *dev_priv = dev->dev_private; |
| 332 | drm_mga_freelist_t *next; |
| 333 | drm_mga_freelist_t *prev; |
| 334 | drm_mga_freelist_t *tail = dev_priv->tail; |
| 335 | u32 head, wrap; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 336 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 338 | head = MGA_READ(MGA_PRIMADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | wrap = dev_priv->sarea_priv->last_wrap; |
| 340 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 341 | DRM_DEBUG(" tail=0x%06lx %d\n", |
| 342 | tail->age.head ? |
| 343 | tail->age.head - dev_priv->primary->offset : 0, |
| 344 | tail->age.wrap); |
| 345 | DRM_DEBUG(" head=0x%06lx %d\n", |
| 346 | head - dev_priv->primary->offset, wrap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 348 | if (TEST_AGE(&tail->age, head, wrap)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | prev = dev_priv->tail->prev; |
| 350 | next = dev_priv->tail; |
| 351 | prev->next = NULL; |
| 352 | next->prev = next->next = NULL; |
| 353 | dev_priv->tail = prev; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 354 | SET_AGE(&next->age, MGA_BUFFER_USED, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return next->buf; |
| 356 | } |
| 357 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 358 | DRM_DEBUG("returning NULL!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 362 | int mga_freelist_put(drm_device_t * dev, drm_buf_t * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | drm_mga_private_t *dev_priv = dev->dev_private; |
| 365 | drm_mga_buf_priv_t *buf_priv = buf->dev_private; |
| 366 | drm_mga_freelist_t *head, *entry, *prev; |
| 367 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 368 | DRM_DEBUG("age=0x%06lx wrap=%d\n", |
| 369 | buf_priv->list_entry->age.head - |
| 370 | dev_priv->primary->offset, buf_priv->list_entry->age.wrap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | entry = buf_priv->list_entry; |
| 373 | head = dev_priv->head; |
| 374 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 375 | if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) { |
| 376 | SET_AGE(&entry->age, MGA_BUFFER_FREE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | prev = dev_priv->tail; |
| 378 | prev->next = entry; |
| 379 | entry->prev = prev; |
| 380 | entry->next = NULL; |
| 381 | } else { |
| 382 | prev = head->next; |
| 383 | head->next = entry; |
| 384 | prev->prev = entry; |
| 385 | entry->prev = head; |
| 386 | entry->next = prev; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* ================================================================ |
| 393 | * DMA initialization, cleanup |
| 394 | */ |
| 395 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 396 | int mga_driver_load(drm_device_t * dev, unsigned long flags) |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 397 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 398 | drm_mga_private_t *dev_priv; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 399 | |
| 400 | dev_priv = drm_alloc(sizeof(drm_mga_private_t), DRM_MEM_DRIVER); |
| 401 | if (!dev_priv) |
| 402 | return DRM_ERR(ENOMEM); |
| 403 | |
| 404 | dev->dev_private = (void *)dev_priv; |
| 405 | memset(dev_priv, 0, sizeof(drm_mga_private_t)); |
| 406 | |
| 407 | dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT; |
| 408 | dev_priv->chipset = flags; |
| 409 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 410 | dev_priv->mmio_base = drm_get_resource_start(dev, 1); |
| 411 | dev_priv->mmio_size = drm_get_resource_len(dev, 1); |
| 412 | |
| 413 | dev->counters += 3; |
| 414 | dev->types[6] = _DRM_STAT_IRQ; |
| 415 | dev->types[7] = _DRM_STAT_PRIMARY; |
| 416 | dev->types[8] = _DRM_STAT_SECONDARY; |
| 417 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
Dave Airlie | 908f9c4 | 2005-09-05 21:51:30 +1000 | [diff] [blame] | 421 | #if __OS_HAS_AGP |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 422 | /** |
| 423 | * Bootstrap the driver for AGP DMA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 424 | * |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 425 | * \todo |
| 426 | * Investigate whether there is any benifit to storing the WARP microcode in |
| 427 | * AGP memory. If not, the microcode may as well always be put in PCI |
| 428 | * memory. |
| 429 | * |
| 430 | * \todo |
| 431 | * This routine needs to set dma_bs->agp_mode to the mode actually configured |
| 432 | * in the hardware. Looking just at the Linux AGP driver code, I don't see |
| 433 | * an easy way to determine this. |
| 434 | * |
| 435 | * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap |
| 436 | */ |
| 437 | static int mga_do_agp_dma_bootstrap(drm_device_t * dev, |
| 438 | drm_mga_dma_bootstrap_t * dma_bs) |
| 439 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 440 | drm_mga_private_t *const dev_priv = |
| 441 | (drm_mga_private_t *) dev->dev_private; |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 442 | unsigned int warp_size = mga_warp_microcode_size(dev_priv); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 443 | int err; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 444 | unsigned offset; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 445 | const unsigned secondary_size = dma_bs->secondary_bin_count |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 446 | * dma_bs->secondary_bin_size; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 447 | const unsigned agp_size = (dma_bs->agp_size << 20); |
| 448 | drm_buf_desc_t req; |
| 449 | drm_agp_mode_t mode; |
| 450 | drm_agp_info_t info; |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 451 | drm_agp_buffer_t agp_req; |
| 452 | drm_agp_binding_t bind_req; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 453 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 454 | /* Acquire AGP. */ |
| 455 | err = drm_agp_acquire(dev); |
| 456 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 457 | DRM_ERROR("Unable to acquire AGP: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 458 | return err; |
| 459 | } |
| 460 | |
| 461 | err = drm_agp_info(dev, &info); |
| 462 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 463 | DRM_ERROR("Unable to get AGP info: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 464 | return err; |
| 465 | } |
| 466 | |
| 467 | mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode; |
| 468 | err = drm_agp_enable(dev, mode); |
| 469 | if (err) { |
| 470 | DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode); |
| 471 | return err; |
| 472 | } |
| 473 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 474 | /* In addition to the usual AGP mode configuration, the G200 AGP cards |
| 475 | * need to have the AGP mode "manually" set. |
| 476 | */ |
| 477 | |
| 478 | if (dev_priv->chipset == MGA_CARD_TYPE_G200) { |
| 479 | if (mode.mode & 0x02) { |
| 480 | MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 481 | } else { |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 482 | MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE); |
| 483 | } |
| 484 | } |
| 485 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 486 | /* Allocate and bind AGP memory. */ |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 487 | agp_req.size = agp_size; |
| 488 | agp_req.type = 0; |
| 489 | err = drm_agp_alloc(dev, &agp_req); |
| 490 | if (err) { |
| 491 | dev_priv->agp_size = 0; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 492 | DRM_ERROR("Unable to allocate %uMB AGP memory\n", |
| 493 | dma_bs->agp_size); |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 494 | return err; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 495 | } |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 496 | |
| 497 | dev_priv->agp_size = agp_size; |
| 498 | dev_priv->agp_handle = agp_req.handle; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 499 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 500 | bind_req.handle = agp_req.handle; |
| 501 | bind_req.offset = 0; |
| 502 | err = drm_agp_bind(dev, &bind_req); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 503 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 504 | DRM_ERROR("Unable to bind AGP memory: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 505 | return err; |
| 506 | } |
| 507 | |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 508 | /* Make drm_addbufs happy by not trying to create a mapping for less |
| 509 | * than a page. |
| 510 | */ |
| 511 | if (warp_size < PAGE_SIZE) |
| 512 | warp_size = PAGE_SIZE; |
| 513 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 514 | offset = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 515 | err = drm_addmap(dev, offset, warp_size, |
| 516 | _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 517 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 518 | DRM_ERROR("Unable to map WARP microcode: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 519 | return err; |
| 520 | } |
| 521 | |
| 522 | offset += warp_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 523 | err = drm_addmap(dev, offset, dma_bs->primary_size, |
| 524 | _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 525 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 526 | DRM_ERROR("Unable to map primary DMA region: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 527 | return err; |
| 528 | } |
| 529 | |
| 530 | offset += dma_bs->primary_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 531 | err = drm_addmap(dev, offset, secondary_size, |
| 532 | _DRM_AGP, 0, &dev->agp_buffer_map); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 533 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 534 | DRM_ERROR("Unable to map secondary DMA region: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 535 | return err; |
| 536 | } |
| 537 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 538 | (void)memset(&req, 0, sizeof(req)); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 539 | req.count = dma_bs->secondary_bin_count; |
| 540 | req.size = dma_bs->secondary_bin_size; |
| 541 | req.flags = _DRM_AGP_BUFFER; |
| 542 | req.agp_start = offset; |
| 543 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 544 | err = drm_addbufs_agp(dev, &req); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 545 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 546 | DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 547 | return err; |
| 548 | } |
| 549 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 550 | { |
| 551 | drm_map_list_t *_entry; |
| 552 | unsigned long agp_token = 0; |
| 553 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 554 | list_for_each_entry(_entry, &dev->maplist, head) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 555 | if (_entry->map == dev->agp_buffer_map) |
| 556 | agp_token = _entry->user_token; |
| 557 | } |
| 558 | if (!agp_token) |
| 559 | return -EFAULT; |
| 560 | |
| 561 | dev->agp_buffer_token = agp_token; |
| 562 | } |
| 563 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 564 | offset += secondary_size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 565 | err = drm_addmap(dev, offset, agp_size - offset, |
| 566 | _DRM_AGP, 0, &dev_priv->agp_textures); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 567 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 568 | DRM_ERROR("Unable to map AGP texture region %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 569 | return err; |
| 570 | } |
| 571 | |
| 572 | drm_core_ioremap(dev_priv->warp, dev); |
| 573 | drm_core_ioremap(dev_priv->primary, dev); |
| 574 | drm_core_ioremap(dev->agp_buffer_map, dev); |
| 575 | |
| 576 | if (!dev_priv->warp->handle || |
| 577 | !dev_priv->primary->handle || !dev->agp_buffer_map->handle) { |
| 578 | DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n", |
| 579 | dev_priv->warp->handle, dev_priv->primary->handle, |
| 580 | dev->agp_buffer_map->handle); |
| 581 | return DRM_ERR(ENOMEM); |
| 582 | } |
| 583 | |
| 584 | dev_priv->dma_access = MGA_PAGPXFER; |
| 585 | dev_priv->wagp_enable = MGA_WAGP_ENABLE; |
| 586 | |
| 587 | DRM_INFO("Initialized card for AGP DMA.\n"); |
| 588 | return 0; |
| 589 | } |
Dave Airlie | 908f9c4 | 2005-09-05 21:51:30 +1000 | [diff] [blame] | 590 | #else |
| 591 | static int mga_do_agp_dma_bootstrap(drm_device_t * dev, |
| 592 | drm_mga_dma_bootstrap_t * dma_bs) |
| 593 | { |
| 594 | return -EINVAL; |
| 595 | } |
| 596 | #endif |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * Bootstrap the driver for PCI DMA. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 600 | * |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 601 | * \todo |
| 602 | * The algorithm for decreasing the size of the primary DMA buffer could be |
| 603 | * better. The size should be rounded up to the nearest page size, then |
| 604 | * decrease the request size by a single page each pass through the loop. |
| 605 | * |
| 606 | * \todo |
| 607 | * Determine whether the maximum address passed to drm_pci_alloc is correct. |
| 608 | * The same goes for drm_addbufs_pci. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 609 | * |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 610 | * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap |
| 611 | */ |
| 612 | static int mga_do_pci_dma_bootstrap(drm_device_t * dev, |
| 613 | drm_mga_dma_bootstrap_t * dma_bs) |
| 614 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 615 | drm_mga_private_t *const dev_priv = |
| 616 | (drm_mga_private_t *) dev->dev_private; |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 617 | unsigned int warp_size = mga_warp_microcode_size(dev_priv); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 618 | unsigned int primary_size; |
| 619 | unsigned int bin_count; |
| 620 | int err; |
| 621 | drm_buf_desc_t req; |
| 622 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 623 | if (dev->dma == NULL) { |
| 624 | DRM_ERROR("dev->dma is NULL\n"); |
| 625 | return DRM_ERR(EFAULT); |
| 626 | } |
| 627 | |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 628 | /* Make drm_addbufs happy by not trying to create a mapping for less |
| 629 | * than a page. |
| 630 | */ |
| 631 | if (warp_size < PAGE_SIZE) |
| 632 | warp_size = PAGE_SIZE; |
| 633 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 634 | /* The proper alignment is 0x100 for this mapping */ |
| 635 | err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT, |
| 636 | _DRM_READ_ONLY, &dev_priv->warp); |
| 637 | if (err != 0) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 638 | DRM_ERROR("Unable to create mapping for WARP microcode: %d\n", |
| 639 | err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 640 | return err; |
| 641 | } |
| 642 | |
| 643 | /* Other than the bottom two bits being used to encode other |
| 644 | * information, there don't appear to be any restrictions on the |
| 645 | * alignment of the primary or secondary DMA buffers. |
| 646 | */ |
| 647 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 648 | for (primary_size = dma_bs->primary_size; primary_size != 0; |
| 649 | primary_size >>= 1) { |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 650 | /* The proper alignment for this mapping is 0x04 */ |
| 651 | err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT, |
| 652 | _DRM_READ_ONLY, &dev_priv->primary); |
| 653 | if (!err) |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | if (err != 0) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 658 | DRM_ERROR("Unable to allocate primary DMA region: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 659 | return DRM_ERR(ENOMEM); |
| 660 | } |
| 661 | |
| 662 | if (dev_priv->primary->size != dma_bs->primary_size) { |
| 663 | DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n", |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 664 | dma_bs->primary_size, |
| 665 | (unsigned)dev_priv->primary->size); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 666 | dma_bs->primary_size = dev_priv->primary->size; |
| 667 | } |
| 668 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 669 | for (bin_count = dma_bs->secondary_bin_count; bin_count > 0; |
| 670 | bin_count--) { |
| 671 | (void)memset(&req, 0, sizeof(req)); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 672 | req.count = bin_count; |
| 673 | req.size = dma_bs->secondary_bin_size; |
| 674 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 675 | err = drm_addbufs_pci(dev, &req); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 676 | if (!err) { |
| 677 | break; |
| 678 | } |
| 679 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 680 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 681 | if (bin_count == 0) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 682 | DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 683 | return err; |
| 684 | } |
| 685 | |
| 686 | if (bin_count != dma_bs->secondary_bin_count) { |
| 687 | DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u " |
| 688 | "to %u.\n", dma_bs->secondary_bin_count, bin_count); |
| 689 | |
| 690 | dma_bs->secondary_bin_count = bin_count; |
| 691 | } |
| 692 | |
| 693 | dev_priv->dma_access = 0; |
| 694 | dev_priv->wagp_enable = 0; |
| 695 | |
| 696 | dma_bs->agp_mode = 0; |
| 697 | |
| 698 | DRM_INFO("Initialized card for PCI DMA.\n"); |
| 699 | return 0; |
| 700 | } |
| 701 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 702 | static int mga_do_dma_bootstrap(drm_device_t * dev, |
| 703 | drm_mga_dma_bootstrap_t * dma_bs) |
| 704 | { |
| 705 | const int is_agp = (dma_bs->agp_mode != 0) && drm_device_is_agp(dev); |
| 706 | int err; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 707 | drm_mga_private_t *const dev_priv = |
| 708 | (drm_mga_private_t *) dev->dev_private; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 709 | |
| 710 | dev_priv->used_new_dma_init = 1; |
| 711 | |
| 712 | /* The first steps are the same for both PCI and AGP based DMA. Map |
| 713 | * the cards MMIO registers and map a status page. |
| 714 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 715 | err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size, |
| 716 | _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 717 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 718 | DRM_ERROR("Unable to map MMIO region: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 719 | return err; |
| 720 | } |
| 721 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 722 | err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM, |
| 723 | _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL, |
| 724 | &dev_priv->status); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 725 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 726 | DRM_ERROR("Unable to map status region: %d\n", err); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 727 | return err; |
| 728 | } |
| 729 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 730 | /* The DMA initialization procedure is slightly different for PCI and |
| 731 | * AGP cards. AGP cards just allocate a large block of AGP memory and |
| 732 | * carve off portions of it for internal uses. The remaining memory |
| 733 | * is returned to user-mode to be used for AGP textures. |
| 734 | */ |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 735 | if (is_agp) { |
| 736 | err = mga_do_agp_dma_bootstrap(dev, dma_bs); |
| 737 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 738 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 739 | /* If we attempted to initialize the card for AGP DMA but failed, |
| 740 | * clean-up any mess that may have been created. |
| 741 | */ |
| 742 | |
| 743 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 744 | mga_do_cleanup_dma(dev, MINIMAL_CLEANUP); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 745 | } |
| 746 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 747 | /* Not only do we want to try and initialized PCI cards for PCI DMA, |
| 748 | * but we also try to initialized AGP cards that could not be |
| 749 | * initialized for AGP DMA. This covers the case where we have an AGP |
| 750 | * card in a system with an unsupported AGP chipset. In that case the |
| 751 | * card will be detected as AGP, but we won't be able to allocate any |
| 752 | * AGP memory, etc. |
| 753 | */ |
| 754 | |
| 755 | if (!is_agp || err) { |
| 756 | err = mga_do_pci_dma_bootstrap(dev, dma_bs); |
| 757 | } |
| 758 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 759 | return err; |
| 760 | } |
| 761 | |
| 762 | int mga_dma_bootstrap(DRM_IOCTL_ARGS) |
| 763 | { |
| 764 | DRM_DEVICE; |
| 765 | drm_mga_dma_bootstrap_t bootstrap; |
| 766 | int err; |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 767 | static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 }; |
| 768 | const drm_mga_private_t *const dev_priv = |
| 769 | (drm_mga_private_t *) dev->dev_private; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 770 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 771 | DRM_COPY_FROM_USER_IOCTL(bootstrap, |
| 772 | (drm_mga_dma_bootstrap_t __user *) data, |
| 773 | sizeof(bootstrap)); |
| 774 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 775 | err = mga_do_dma_bootstrap(dev, &bootstrap); |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 776 | if (err) { |
| 777 | mga_do_cleanup_dma(dev, FULL_CLEANUP); |
| 778 | return err; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 779 | } |
| 780 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 781 | if (dev_priv->agp_textures != NULL) { |
| 782 | bootstrap.texture_handle = dev_priv->agp_textures->offset; |
| 783 | bootstrap.texture_size = dev_priv->agp_textures->size; |
| 784 | } else { |
| 785 | bootstrap.texture_handle = 0; |
| 786 | bootstrap.texture_size = 0; |
| 787 | } |
| 788 | |
| 789 | bootstrap.agp_mode = modes[bootstrap.agp_mode & 0x07]; |
| 790 | DRM_COPY_TO_USER_IOCTL((drm_mga_dma_bootstrap_t __user *)data, |
| 791 | bootstrap, sizeof(bootstrap)); |
| 792 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 793 | return err; |
| 794 | } |
| 795 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 796 | static int mga_do_init_dma(drm_device_t * dev, drm_mga_init_t * init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
| 798 | drm_mga_private_t *dev_priv; |
| 799 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 800 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 802 | dev_priv = dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 804 | if (init->sgram) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK; |
| 806 | } else { |
| 807 | dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR; |
| 808 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 809 | dev_priv->maccess = init->maccess; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 811 | dev_priv->fb_cpp = init->fb_cpp; |
| 812 | dev_priv->front_offset = init->front_offset; |
| 813 | dev_priv->front_pitch = init->front_pitch; |
| 814 | dev_priv->back_offset = init->back_offset; |
| 815 | dev_priv->back_pitch = init->back_pitch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 817 | dev_priv->depth_cpp = init->depth_cpp; |
| 818 | dev_priv->depth_offset = init->depth_offset; |
| 819 | dev_priv->depth_pitch = init->depth_pitch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | /* FIXME: Need to support AGP textures... |
| 822 | */ |
| 823 | dev_priv->texture_offset = init->texture_offset[0]; |
| 824 | dev_priv->texture_size = init->texture_size[0]; |
| 825 | |
Dave Airlie | da509d7 | 2007-05-26 05:04:51 +1000 | [diff] [blame] | 826 | dev_priv->sarea = drm_getsarea(dev); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 827 | if (!dev_priv->sarea) { |
| 828 | DRM_ERROR("failed to find sarea!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return DRM_ERR(EINVAL); |
| 830 | } |
| 831 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 832 | if (!dev_priv->used_new_dma_init) { |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 833 | |
| 834 | dev_priv->dma_access = MGA_PAGPXFER; |
| 835 | dev_priv->wagp_enable = MGA_WAGP_ENABLE; |
| 836 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 837 | dev_priv->status = drm_core_findmap(dev, init->status_offset); |
| 838 | if (!dev_priv->status) { |
| 839 | DRM_ERROR("failed to find status page!\n"); |
| 840 | return DRM_ERR(EINVAL); |
| 841 | } |
| 842 | dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset); |
| 843 | if (!dev_priv->mmio) { |
| 844 | DRM_ERROR("failed to find mmio region!\n"); |
| 845 | return DRM_ERR(EINVAL); |
| 846 | } |
| 847 | dev_priv->warp = drm_core_findmap(dev, init->warp_offset); |
| 848 | if (!dev_priv->warp) { |
| 849 | DRM_ERROR("failed to find warp microcode region!\n"); |
| 850 | return DRM_ERR(EINVAL); |
| 851 | } |
| 852 | dev_priv->primary = drm_core_findmap(dev, init->primary_offset); |
| 853 | if (!dev_priv->primary) { |
| 854 | DRM_ERROR("failed to find primary dma region!\n"); |
| 855 | return DRM_ERR(EINVAL); |
| 856 | } |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 857 | dev->agp_buffer_token = init->buffers_offset; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 858 | dev->agp_buffer_map = |
| 859 | drm_core_findmap(dev, init->buffers_offset); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 860 | if (!dev->agp_buffer_map) { |
| 861 | DRM_ERROR("failed to find dma buffer region!\n"); |
| 862 | return DRM_ERR(EINVAL); |
| 863 | } |
| 864 | |
| 865 | drm_core_ioremap(dev_priv->warp, dev); |
| 866 | drm_core_ioremap(dev_priv->primary, dev); |
| 867 | drm_core_ioremap(dev->agp_buffer_map, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | dev_priv->sarea_priv = |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 871 | (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle + |
| 872 | init->sarea_priv_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 874 | if (!dev_priv->warp->handle || |
| 875 | !dev_priv->primary->handle || |
| 876 | ((dev_priv->dma_access != 0) && |
| 877 | ((dev->agp_buffer_map == NULL) || |
| 878 | (dev->agp_buffer_map->handle == NULL)))) { |
| 879 | DRM_ERROR("failed to ioremap agp regions!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | return DRM_ERR(ENOMEM); |
| 881 | } |
| 882 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 883 | ret = mga_warp_install_microcode(dev_priv); |
| 884 | if (ret < 0) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 885 | DRM_ERROR("failed to install WARP ucode!: %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 889 | ret = mga_warp_init(dev_priv); |
| 890 | if (ret < 0) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 891 | DRM_ERROR("failed to init WARP engine!: %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return ret; |
| 893 | } |
| 894 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 895 | dev_priv->prim.status = (u32 *) dev_priv->status->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 897 | mga_do_wait_for_idle(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | /* Init the primary DMA registers. |
| 900 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 901 | MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | #if 0 |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 903 | MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */ |
| 904 | MGA_PRIMPTREN1); /* DWGSYNC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | #endif |
| 906 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 907 | dev_priv->prim.start = (u8 *) dev_priv->primary->handle; |
| 908 | dev_priv->prim.end = ((u8 *) dev_priv->primary->handle |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | + dev_priv->primary->size); |
| 910 | dev_priv->prim.size = dev_priv->primary->size; |
| 911 | |
| 912 | dev_priv->prim.tail = 0; |
| 913 | dev_priv->prim.space = dev_priv->prim.size; |
| 914 | dev_priv->prim.wrapped = 0; |
| 915 | |
| 916 | dev_priv->prim.last_flush = 0; |
| 917 | dev_priv->prim.last_wrap = 0; |
| 918 | |
| 919 | dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE; |
| 920 | |
| 921 | dev_priv->prim.status[0] = dev_priv->primary->offset; |
| 922 | dev_priv->prim.status[1] = 0; |
| 923 | |
| 924 | dev_priv->sarea_priv->last_wrap = 0; |
| 925 | dev_priv->sarea_priv->last_frame.head = 0; |
| 926 | dev_priv->sarea_priv->last_frame.wrap = 0; |
| 927 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 928 | if (mga_freelist_init(dev, dev_priv) < 0) { |
| 929 | DRM_ERROR("could not initialize freelist\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return DRM_ERR(ENOMEM); |
| 931 | } |
| 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 936 | static int mga_do_cleanup_dma(drm_device_t *dev, int full_cleanup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 938 | int err = 0; |
| 939 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | /* Make sure interrupts are disabled here because the uninstall ioctl |
| 942 | * may not have been called from userspace and after dev_private |
| 943 | * is freed, it's too late. |
| 944 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 945 | if (dev->irq_enabled) |
| 946 | drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 948 | if (dev->dev_private) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | drm_mga_private_t *dev_priv = dev->dev_private; |
| 950 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 951 | if ((dev_priv->warp != NULL) |
Dave Airlie | 11909d6 | 2005-10-19 21:23:51 -0700 | [diff] [blame] | 952 | && (dev_priv->warp->type != _DRM_CONSISTENT)) |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 953 | drm_core_ioremapfree(dev_priv->warp, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 955 | if ((dev_priv->primary != NULL) |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 956 | && (dev_priv->primary->type != _DRM_CONSISTENT)) |
| 957 | drm_core_ioremapfree(dev_priv->primary, dev); |
| 958 | |
| 959 | if (dev->agp_buffer_map != NULL) |
| 960 | drm_core_ioremapfree(dev->agp_buffer_map, dev); |
| 961 | |
| 962 | if (dev_priv->used_new_dma_init) { |
Dave Airlie | 908f9c4 | 2005-09-05 21:51:30 +1000 | [diff] [blame] | 963 | #if __OS_HAS_AGP |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 964 | if (dev_priv->agp_handle != 0) { |
| 965 | drm_agp_binding_t unbind_req; |
| 966 | drm_agp_buffer_t free_req; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 967 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 968 | unbind_req.handle = dev_priv->agp_handle; |
| 969 | drm_agp_unbind(dev, &unbind_req); |
| 970 | |
| 971 | free_req.handle = dev_priv->agp_handle; |
| 972 | drm_agp_free(dev, &free_req); |
| 973 | |
| 974 | dev_priv->agp_textures = NULL; |
| 975 | dev_priv->agp_size = 0; |
| 976 | dev_priv->agp_handle = 0; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | if ((dev->agp != NULL) && dev->agp->acquired) { |
| 980 | err = drm_agp_release(dev); |
| 981 | } |
Dave Airlie | 908f9c4 | 2005-09-05 21:51:30 +1000 | [diff] [blame] | 982 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 985 | dev_priv->warp = NULL; |
| 986 | dev_priv->primary = NULL; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 987 | dev_priv->sarea = NULL; |
| 988 | dev_priv->sarea_priv = NULL; |
| 989 | dev->agp_buffer_map = NULL; |
| 990 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 991 | if (full_cleanup) { |
| 992 | dev_priv->mmio = NULL; |
| 993 | dev_priv->status = NULL; |
| 994 | dev_priv->used_new_dma_init = 0; |
| 995 | } |
| 996 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 997 | memset(&dev_priv->prim, 0, sizeof(dev_priv->prim)); |
| 998 | dev_priv->warp_pipe = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 999 | memset(dev_priv->warp_pipe_phys, 0, |
| 1000 | sizeof(dev_priv->warp_pipe_phys)); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1001 | |
| 1002 | if (dev_priv->head != NULL) { |
| 1003 | mga_freelist_cleanup(dev); |
| 1004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 1007 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1010 | int mga_dma_init(DRM_IOCTL_ARGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
| 1012 | DRM_DEVICE; |
| 1013 | drm_mga_init_t init; |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1014 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1016 | LOCK_TEST_WITH_RETURN(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1018 | DRM_COPY_FROM_USER_IOCTL(init, (drm_mga_init_t __user *) data, |
| 1019 | sizeof(init)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1021 | switch (init.func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | case MGA_INIT_DMA: |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1023 | err = mga_do_init_dma(dev, &init); |
| 1024 | if (err) { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 1025 | (void)mga_do_cleanup_dma(dev, FULL_CLEANUP); |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1026 | } |
| 1027 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | case MGA_CLEANUP_DMA: |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 1029 | return mga_do_cleanup_dma(dev, FULL_CLEANUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | return DRM_ERR(EINVAL); |
| 1033 | } |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | /* ================================================================ |
| 1036 | * Primary DMA stream management |
| 1037 | */ |
| 1038 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1039 | int mga_dma_flush(DRM_IOCTL_ARGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | { |
| 1041 | DRM_DEVICE; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1042 | drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | drm_lock_t lock; |
| 1044 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1045 | LOCK_TEST_WITH_RETURN(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1047 | DRM_COPY_FROM_USER_IOCTL(lock, (drm_lock_t __user *) data, |
| 1048 | sizeof(lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1050 | DRM_DEBUG("%s%s%s\n", |
| 1051 | (lock.flags & _DRM_LOCK_FLUSH) ? "flush, " : "", |
| 1052 | (lock.flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "", |
| 1053 | (lock.flags & _DRM_LOCK_QUIESCENT) ? "idle, " : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1055 | WRAP_WAIT_WITH_RETURN(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1057 | if (lock.flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL)) { |
| 1058 | mga_do_dma_flush(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1061 | if (lock.flags & _DRM_LOCK_QUIESCENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | #if MGA_DMA_DEBUG |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1063 | int ret = mga_do_wait_for_idle(dev_priv); |
| 1064 | if (ret < 0) |
| 1065 | DRM_INFO("%s: -EBUSY\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | return ret; |
| 1067 | #else |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1068 | return mga_do_wait_for_idle(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | #endif |
| 1070 | } else { |
| 1071 | return 0; |
| 1072 | } |
| 1073 | } |
| 1074 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1075 | int mga_dma_reset(DRM_IOCTL_ARGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | { |
| 1077 | DRM_DEVICE; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1078 | drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1080 | LOCK_TEST_WITH_RETURN(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1082 | return mga_do_dma_reset(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | /* ================================================================ |
| 1086 | * DMA buffer management |
| 1087 | */ |
| 1088 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1089 | static int mga_dma_get_buffers(DRMFILE filp, drm_device_t * dev, drm_dma_t * d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
| 1091 | drm_buf_t *buf; |
| 1092 | int i; |
| 1093 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1094 | for (i = d->granted_count; i < d->request_count; i++) { |
| 1095 | buf = mga_freelist_get(dev); |
| 1096 | if (!buf) |
| 1097 | return DRM_ERR(EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
| 1099 | buf->filp = filp; |
| 1100 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1101 | if (DRM_COPY_TO_USER(&d->request_indices[i], |
| 1102 | &buf->idx, sizeof(buf->idx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return DRM_ERR(EFAULT); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1104 | if (DRM_COPY_TO_USER(&d->request_sizes[i], |
| 1105 | &buf->total, sizeof(buf->total))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return DRM_ERR(EFAULT); |
| 1107 | |
| 1108 | d->granted_count++; |
| 1109 | } |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1113 | int mga_dma_buffers(DRM_IOCTL_ARGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
| 1115 | DRM_DEVICE; |
| 1116 | drm_device_dma_t *dma = dev->dma; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1117 | drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | drm_dma_t __user *argp = (void __user *)data; |
| 1119 | drm_dma_t d; |
| 1120 | int ret = 0; |
| 1121 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1122 | LOCK_TEST_WITH_RETURN(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1124 | DRM_COPY_FROM_USER_IOCTL(d, argp, sizeof(d)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
| 1126 | /* Please don't send us buffers. |
| 1127 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1128 | if (d.send_count != 0) { |
| 1129 | DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n", |
| 1130 | DRM_CURRENTPID, d.send_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | return DRM_ERR(EINVAL); |
| 1132 | } |
| 1133 | |
| 1134 | /* We'll send you buffers. |
| 1135 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1136 | if (d.request_count < 0 || d.request_count > dma->buf_count) { |
| 1137 | DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n", |
| 1138 | DRM_CURRENTPID, d.request_count, dma->buf_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | return DRM_ERR(EINVAL); |
| 1140 | } |
| 1141 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1142 | WRAP_TEST_WITH_RETURN(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | d.granted_count = 0; |
| 1145 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1146 | if (d.request_count) { |
| 1147 | ret = mga_dma_get_buffers(filp, dev, &d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1150 | DRM_COPY_TO_USER_IOCTL(argp, d, sizeof(d)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1155 | /** |
| 1156 | * Called just before the module is unloaded. |
| 1157 | */ |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 1158 | int mga_driver_unload(drm_device_t * dev) |
Dave Airlie | 6795c98 | 2005-07-10 18:20:09 +1000 | [diff] [blame] | 1159 | { |
| 1160 | drm_free(dev->dev_private, sizeof(drm_mga_private_t), DRM_MEM_DRIVER); |
| 1161 | dev->dev_private = NULL; |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * Called when the last opener of the device is closed. |
| 1168 | */ |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 1169 | void mga_driver_lastclose(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | { |
Dave Airlie | 7ccf800 | 2005-11-11 23:11:34 +1100 | [diff] [blame] | 1171 | mga_do_cleanup_dma(dev, FULL_CLEANUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1174 | int mga_driver_dma_quiescent(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | { |
| 1176 | drm_mga_private_t *dev_priv = dev->dev_private; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1177 | return mga_do_wait_for_idle(dev_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |