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