blob: 7a6bf9ffc5a30dff795e33cda956da8243c4ef4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Airlie6795c982005-07-10 18:20:09 +100026 */
27
28/**
29 * \file mga_dma.c
30 * DMA support for MGA G200 / G400.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100031 *
Dave Airlie6795c982005-07-10 18:20:09 +100032 * \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 Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
38#include "drmP.h"
39#include "drm.h"
Dave Airlie6795c982005-07-10 18:20:09 +100040#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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 Airlie7ccf8002005-11-11 23:11:34 +110047#define MINIMAL_CLEANUP 0
48#define FULL_CLEANUP 1
Dave Airlieeddca552007-07-11 16:09:54 +100049static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* ================================================================
52 * Engine control
53 */
54
Dave Airlieb5e89ed2005-09-25 14:28:13 +100055int mga_do_wait_for_idle(drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 u32 status = 0;
58 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 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 Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69
70#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 DRM_ERROR("failed!\n");
72 DRM_INFO(" status=0x%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100074 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077static int mga_do_dma_reset(drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
80 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
81
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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 Airlieb5e89ed2005-09-25 14:28:13 +1000105void mga_do_dma_flush(drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
108 u32 head, tail;
109 u32 status = 0;
110 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 DMA_LOCALS;
112 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122 if (primary->tail == primary->last_flush) {
123 DRM_DEBUG(" bailing out...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 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 Airlieb5e89ed2005-09-25 14:28:13 +1000133 BEGIN_DMA(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
136 MGA_DMAPAD, 0x00000000,
137 MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 ADVANCE_DMA();
140
141 primary->last_flush = primary->tail;
142
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 if (head <= tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 primary->space = primary->size - primary->tail;
147 } else {
148 primary->space = head - tail;
149 }
150
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100151 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
152 DRM_DEBUG(" tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 DRM_DEBUG(" space = 0x%06x\n", primary->space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000156 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161void mga_do_dma_wrap_start(drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
164 u32 head, tail;
165 DMA_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 BEGIN_DMA_WRAP();
169
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
171 MGA_DMAPAD, 0x00000000,
172 MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
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 Airlieb5e89ed2005-09-25 14:28:13 +1000182 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 if (head == dev_priv->primary->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 primary->space = primary->size;
186 } else {
187 primary->space = head - dev_priv->primary->offset;
188 }
189
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100190 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 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 Torvalds1da177e2005-04-16 15:20:36 -0700194
195 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000196 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 set_bit(0, &primary->wrapped);
199 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202void mga_do_dma_wrap_end(drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
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 Airlieb5e89ed2005-09-25 14:28:13 +1000207 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 sarea_priv->last_wrap++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 mga_flush_write_combine();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 clear_bit(0, &primary->wrapped);
216 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/* ================================================================
220 * Freelist management
221 */
222
223#define MGA_BUFFER_USED ~0
224#define MGA_BUFFER_FREE 0
225
226#if MGA_FREELIST_DEBUG
Dave Airlieeddca552007-07-11 16:09:54 +1000227static void mga_freelist_print(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 drm_mga_private_t *dev_priv = dev->dev_private;
230 drm_mga_freelist_t *entry;
231
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 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 Torvalds1da177e2005-04-16 15:20:36 -0700238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 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,
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100242 (unsigned long)(entry->age.head - dev_priv->primary->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 DRM_INFO("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246#endif
247
Dave Airlieeddca552007-07-11 16:09:54 +1000248static int mga_freelist_init(struct drm_device * dev, drm_mga_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dave Airliecdd55a22007-07-11 16:32:08 +1000250 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000251 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 drm_mga_buf_priv_t *buf_priv;
253 drm_mga_freelist_t *entry;
254 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255 DRM_DEBUG("count=%d\n", dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 dev_priv->head = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
258 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000259 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 memset(dev_priv->head, 0, sizeof(drm_mga_freelist_t));
262 SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 entry = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
269 if (entry == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000270 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 memset(entry, 0, sizeof(drm_mga_freelist_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 entry->next = dev_priv->head->next;
275 entry->prev = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 entry->buf = buf;
278
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 if (dev_priv->head->next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dev_priv->head->next->prev = entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 if (entry->next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 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 Airlieeddca552007-07-11 16:09:54 +1000294static void mga_freelist_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 drm_mga_private_t *dev_priv = dev->dev_private;
297 drm_mga_freelist_t *entry;
298 drm_mga_freelist_t *next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 entry = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 next = entry->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 drm_free(entry, sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 entry = next;
306 }
307
308 dev_priv->head = dev_priv->tail = NULL;
309}
310
311#if 0
312/* FIXME: Still needed?
313 */
Dave Airlieeddca552007-07-11 16:09:54 +1000314static void mga_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Dave Airliecdd55a22007-07-11 16:32:08 +1000316 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000317 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 drm_mga_buf_priv_t *buf_priv;
319 int i;
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 buf_priv = buf->dev_private;
324 SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326}
327#endif
328
Dave Airlie056219e2007-07-11 16:17:42 +1000329static struct drm_buf *mga_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
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 Airlieb5e89ed2005-09-25 14:28:13 +1000336 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 wrap = dev_priv->sarea_priv->last_wrap;
340
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 DRM_DEBUG(" tail=0x%06lx %d\n",
342 tail->age.head ?
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100343 (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 tail->age.wrap);
345 DRM_DEBUG(" head=0x%06lx %d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100346 (unsigned long)(head - dev_priv->primary->offset), wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 if (TEST_AGE(&tail->age, head, wrap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 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 Airlieb5e89ed2005-09-25 14:28:13 +1000354 SET_AGE(&next->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return next->buf;
356 }
357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return NULL;
360}
361
Dave Airlie056219e2007-07-11 16:17:42 +1000362int mga_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
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 Airlieb5e89ed2005-09-25 14:28:13 +1000368 DRM_DEBUG("age=0x%06lx wrap=%d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100369 (unsigned long)(buf_priv->list_entry->age.head -
370 dev_priv->primary->offset),
371 buf_priv->list_entry->age.wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 entry = buf_priv->list_entry;
374 head = dev_priv->head;
375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
377 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 prev = dev_priv->tail;
379 prev->next = entry;
380 entry->prev = prev;
381 entry->next = NULL;
382 } else {
383 prev = head->next;
384 head->next = entry;
385 prev->prev = entry;
386 entry->prev = head;
387 entry->next = prev;
388 }
389
390 return 0;
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/* ================================================================
394 * DMA initialization, cleanup
395 */
396
Dave Airlieeddca552007-07-11 16:09:54 +1000397int mga_driver_load(struct drm_device * dev, unsigned long flags)
Dave Airlie6795c982005-07-10 18:20:09 +1000398{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399 drm_mga_private_t *dev_priv;
Keith Packard52440212008-11-18 09:30:25 -0800400 int ret;
Dave Airlie6795c982005-07-10 18:20:09 +1000401
402 dev_priv = drm_alloc(sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
403 if (!dev_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000404 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000405
406 dev->dev_private = (void *)dev_priv;
407 memset(dev_priv, 0, sizeof(drm_mga_private_t));
408
409 dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
410 dev_priv->chipset = flags;
411
Dave Airlie22eae942005-11-10 22:16:34 +1100412 dev_priv->mmio_base = drm_get_resource_start(dev, 1);
413 dev_priv->mmio_size = drm_get_resource_len(dev, 1);
414
415 dev->counters += 3;
416 dev->types[6] = _DRM_STAT_IRQ;
417 dev->types[7] = _DRM_STAT_PRIMARY;
418 dev->types[8] = _DRM_STAT_SECONDARY;
419
Keith Packard52440212008-11-18 09:30:25 -0800420 ret = drm_vblank_init(dev, 1);
421
422 if (ret) {
423 (void) mga_driver_unload(dev);
424 return ret;
425 }
426
Dave Airlie6795c982005-07-10 18:20:09 +1000427 return 0;
428}
429
Dave Airlie908f9c42005-09-05 21:51:30 +1000430#if __OS_HAS_AGP
Dave Airlie6795c982005-07-10 18:20:09 +1000431/**
432 * Bootstrap the driver for AGP DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 *
Dave Airlie6795c982005-07-10 18:20:09 +1000434 * \todo
435 * Investigate whether there is any benifit to storing the WARP microcode in
436 * AGP memory. If not, the microcode may as well always be put in PCI
437 * memory.
438 *
439 * \todo
440 * This routine needs to set dma_bs->agp_mode to the mode actually configured
441 * in the hardware. Looking just at the Linux AGP driver code, I don't see
442 * an easy way to determine this.
443 *
444 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
445 */
Dave Airlieeddca552007-07-11 16:09:54 +1000446static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000447 drm_mga_dma_bootstrap_t * dma_bs)
448{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 drm_mga_private_t *const dev_priv =
450 (drm_mga_private_t *) dev->dev_private;
Dave Airlie11909d62005-10-19 21:23:51 -0700451 unsigned int warp_size = mga_warp_microcode_size(dev_priv);
Dave Airlie6795c982005-07-10 18:20:09 +1000452 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 unsigned offset;
Dave Airlie6795c982005-07-10 18:20:09 +1000454 const unsigned secondary_size = dma_bs->secondary_bin_count
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 * dma_bs->secondary_bin_size;
Dave Airlie6795c982005-07-10 18:20:09 +1000456 const unsigned agp_size = (dma_bs->agp_size << 20);
Dave Airlieeddca552007-07-11 16:09:54 +1000457 struct drm_buf_desc req;
458 struct drm_agp_mode mode;
459 struct drm_agp_info info;
460 struct drm_agp_buffer agp_req;
461 struct drm_agp_binding bind_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000462
Dave Airlie6795c982005-07-10 18:20:09 +1000463 /* Acquire AGP. */
464 err = drm_agp_acquire(dev);
465 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100466 DRM_ERROR("Unable to acquire AGP: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000467 return err;
468 }
469
470 err = drm_agp_info(dev, &info);
471 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100472 DRM_ERROR("Unable to get AGP info: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000473 return err;
474 }
475
476 mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
477 err = drm_agp_enable(dev, mode);
478 if (err) {
479 DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
480 return err;
481 }
482
Dave Airlie6795c982005-07-10 18:20:09 +1000483 /* In addition to the usual AGP mode configuration, the G200 AGP cards
484 * need to have the AGP mode "manually" set.
485 */
486
487 if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
488 if (mode.mode & 0x02) {
489 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 } else {
Dave Airlie6795c982005-07-10 18:20:09 +1000491 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
492 }
493 }
494
Dave Airlie6795c982005-07-10 18:20:09 +1000495 /* Allocate and bind AGP memory. */
Dave Airlie7ccf8002005-11-11 23:11:34 +1100496 agp_req.size = agp_size;
497 agp_req.type = 0;
498 err = drm_agp_alloc(dev, &agp_req);
499 if (err) {
500 dev_priv->agp_size = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000501 DRM_ERROR("Unable to allocate %uMB AGP memory\n",
502 dma_bs->agp_size);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100503 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000504 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000505
Dave Airlie7ccf8002005-11-11 23:11:34 +1100506 dev_priv->agp_size = agp_size;
507 dev_priv->agp_handle = agp_req.handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508
Dave Airlie7ccf8002005-11-11 23:11:34 +1100509 bind_req.handle = agp_req.handle;
510 bind_req.offset = 0;
511 err = drm_agp_bind(dev, &bind_req);
Dave Airlie6795c982005-07-10 18:20:09 +1000512 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100513 DRM_ERROR("Unable to bind AGP memory: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000514 return err;
515 }
516
Dave Airlie11909d62005-10-19 21:23:51 -0700517 /* Make drm_addbufs happy by not trying to create a mapping for less
518 * than a page.
519 */
520 if (warp_size < PAGE_SIZE)
521 warp_size = PAGE_SIZE;
522
Dave Airlie6795c982005-07-10 18:20:09 +1000523 offset = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 err = drm_addmap(dev, offset, warp_size,
525 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
Dave Airlie6795c982005-07-10 18:20:09 +1000526 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100527 DRM_ERROR("Unable to map WARP microcode: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000528 return err;
529 }
530
531 offset += warp_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 err = drm_addmap(dev, offset, dma_bs->primary_size,
533 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
Dave Airlie6795c982005-07-10 18:20:09 +1000534 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100535 DRM_ERROR("Unable to map primary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000536 return err;
537 }
538
539 offset += dma_bs->primary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 err = drm_addmap(dev, offset, secondary_size,
541 _DRM_AGP, 0, &dev->agp_buffer_map);
Dave Airlie6795c982005-07-10 18:20:09 +1000542 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100543 DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000544 return err;
545 }
546
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000548 req.count = dma_bs->secondary_bin_count;
549 req.size = dma_bs->secondary_bin_size;
550 req.flags = _DRM_AGP_BUFFER;
551 req.agp_start = offset;
552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 err = drm_addbufs_agp(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000554 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100555 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000556 return err;
557 }
558
Dave Airlie7ccf8002005-11-11 23:11:34 +1100559 {
Dave Airlie55910512007-07-11 16:53:40 +1000560 struct drm_map_list *_entry;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100561 unsigned long agp_token = 0;
Dave Airliebc5f4522007-11-05 12:50:58 +1000562
Dave Airliebd1b3312007-05-26 05:01:51 +1000563 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100564 if (_entry->map == dev->agp_buffer_map)
565 agp_token = _entry->user_token;
566 }
567 if (!agp_token)
568 return -EFAULT;
569
570 dev->agp_buffer_token = agp_token;
571 }
572
Dave Airlie6795c982005-07-10 18:20:09 +1000573 offset += secondary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 err = drm_addmap(dev, offset, agp_size - offset,
575 _DRM_AGP, 0, &dev_priv->agp_textures);
Dave Airlie6795c982005-07-10 18:20:09 +1000576 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100577 DRM_ERROR("Unable to map AGP texture region %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000578 return err;
579 }
580
581 drm_core_ioremap(dev_priv->warp, dev);
582 drm_core_ioremap(dev_priv->primary, dev);
583 drm_core_ioremap(dev->agp_buffer_map, dev);
584
585 if (!dev_priv->warp->handle ||
586 !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
587 DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
588 dev_priv->warp->handle, dev_priv->primary->handle,
589 dev->agp_buffer_map->handle);
Eric Anholt20caafa2007-08-25 19:22:43 +1000590 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000591 }
592
593 dev_priv->dma_access = MGA_PAGPXFER;
594 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
595
596 DRM_INFO("Initialized card for AGP DMA.\n");
597 return 0;
598}
Dave Airlie908f9c42005-09-05 21:51:30 +1000599#else
Dave Airlieeddca552007-07-11 16:09:54 +1000600static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
Dave Airlie908f9c42005-09-05 21:51:30 +1000601 drm_mga_dma_bootstrap_t * dma_bs)
602{
603 return -EINVAL;
604}
605#endif
Dave Airlie6795c982005-07-10 18:20:09 +1000606
607/**
608 * Bootstrap the driver for PCI DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 *
Dave Airlie6795c982005-07-10 18:20:09 +1000610 * \todo
611 * The algorithm for decreasing the size of the primary DMA buffer could be
612 * better. The size should be rounded up to the nearest page size, then
613 * decrease the request size by a single page each pass through the loop.
614 *
615 * \todo
616 * Determine whether the maximum address passed to drm_pci_alloc is correct.
617 * The same goes for drm_addbufs_pci.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 *
Dave Airlie6795c982005-07-10 18:20:09 +1000619 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
620 */
Dave Airlieeddca552007-07-11 16:09:54 +1000621static int mga_do_pci_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000622 drm_mga_dma_bootstrap_t * dma_bs)
623{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 drm_mga_private_t *const dev_priv =
625 (drm_mga_private_t *) dev->dev_private;
Dave Airlie11909d62005-10-19 21:23:51 -0700626 unsigned int warp_size = mga_warp_microcode_size(dev_priv);
Dave Airlie6795c982005-07-10 18:20:09 +1000627 unsigned int primary_size;
628 unsigned int bin_count;
629 int err;
Dave Airlieeddca552007-07-11 16:09:54 +1000630 struct drm_buf_desc req;
Dave Airlie6795c982005-07-10 18:20:09 +1000631
Dave Airlie6795c982005-07-10 18:20:09 +1000632 if (dev->dma == NULL) {
633 DRM_ERROR("dev->dma is NULL\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000634 return -EFAULT;
Dave Airlie6795c982005-07-10 18:20:09 +1000635 }
636
Dave Airlie11909d62005-10-19 21:23:51 -0700637 /* Make drm_addbufs happy by not trying to create a mapping for less
638 * than a page.
639 */
640 if (warp_size < PAGE_SIZE)
641 warp_size = PAGE_SIZE;
642
Dave Airlie6795c982005-07-10 18:20:09 +1000643 /* The proper alignment is 0x100 for this mapping */
644 err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
645 _DRM_READ_ONLY, &dev_priv->warp);
646 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100647 DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
648 err);
Dave Airlie6795c982005-07-10 18:20:09 +1000649 return err;
650 }
651
652 /* Other than the bottom two bits being used to encode other
653 * information, there don't appear to be any restrictions on the
654 * alignment of the primary or secondary DMA buffers.
655 */
656
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 for (primary_size = dma_bs->primary_size; primary_size != 0;
658 primary_size >>= 1) {
Dave Airlie6795c982005-07-10 18:20:09 +1000659 /* The proper alignment for this mapping is 0x04 */
660 err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
661 _DRM_READ_ONLY, &dev_priv->primary);
662 if (!err)
663 break;
664 }
665
666 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100667 DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
Eric Anholt20caafa2007-08-25 19:22:43 +1000668 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000669 }
670
671 if (dev_priv->primary->size != dma_bs->primary_size) {
672 DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 dma_bs->primary_size,
674 (unsigned)dev_priv->primary->size);
Dave Airlie6795c982005-07-10 18:20:09 +1000675 dma_bs->primary_size = dev_priv->primary->size;
676 }
677
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
679 bin_count--) {
680 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000681 req.count = bin_count;
682 req.size = dma_bs->secondary_bin_size;
683
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 err = drm_addbufs_pci(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000685 if (!err) {
686 break;
687 }
688 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689
Dave Airlie6795c982005-07-10 18:20:09 +1000690 if (bin_count == 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100691 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000692 return err;
693 }
694
695 if (bin_count != dma_bs->secondary_bin_count) {
696 DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
697 "to %u.\n", dma_bs->secondary_bin_count, bin_count);
698
699 dma_bs->secondary_bin_count = bin_count;
700 }
701
702 dev_priv->dma_access = 0;
703 dev_priv->wagp_enable = 0;
704
705 dma_bs->agp_mode = 0;
706
707 DRM_INFO("Initialized card for PCI DMA.\n");
708 return 0;
709}
710
Dave Airlieeddca552007-07-11 16:09:54 +1000711static int mga_do_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000712 drm_mga_dma_bootstrap_t * dma_bs)
713{
714 const int is_agp = (dma_bs->agp_mode != 0) && drm_device_is_agp(dev);
715 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 drm_mga_private_t *const dev_priv =
717 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000718
719 dev_priv->used_new_dma_init = 1;
720
721 /* The first steps are the same for both PCI and AGP based DMA. Map
722 * the cards MMIO registers and map a status page.
723 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
725 _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio);
Dave Airlie6795c982005-07-10 18:20:09 +1000726 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100727 DRM_ERROR("Unable to map MMIO region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000728 return err;
729 }
730
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
732 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
733 &dev_priv->status);
Dave Airlie6795c982005-07-10 18:20:09 +1000734 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100735 DRM_ERROR("Unable to map status region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000736 return err;
737 }
738
Dave Airlie6795c982005-07-10 18:20:09 +1000739 /* The DMA initialization procedure is slightly different for PCI and
740 * AGP cards. AGP cards just allocate a large block of AGP memory and
741 * carve off portions of it for internal uses. The remaining memory
742 * is returned to user-mode to be used for AGP textures.
743 */
Dave Airlie6795c982005-07-10 18:20:09 +1000744 if (is_agp) {
745 err = mga_do_agp_dma_bootstrap(dev, dma_bs);
746 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747
Dave Airlie6795c982005-07-10 18:20:09 +1000748 /* If we attempted to initialize the card for AGP DMA but failed,
749 * clean-up any mess that may have been created.
750 */
751
752 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100753 mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +1000754 }
755
Dave Airlie6795c982005-07-10 18:20:09 +1000756 /* Not only do we want to try and initialized PCI cards for PCI DMA,
757 * but we also try to initialized AGP cards that could not be
758 * initialized for AGP DMA. This covers the case where we have an AGP
759 * card in a system with an unsupported AGP chipset. In that case the
760 * card will be detected as AGP, but we won't be able to allocate any
761 * AGP memory, etc.
762 */
763
764 if (!is_agp || err) {
765 err = mga_do_pci_dma_bootstrap(dev, dma_bs);
766 }
767
Dave Airlie6795c982005-07-10 18:20:09 +1000768 return err;
769}
770
Eric Anholtc153f452007-09-03 12:06:45 +1000771int mga_dma_bootstrap(struct drm_device *dev, void *data,
772 struct drm_file *file_priv)
Dave Airlie6795c982005-07-10 18:20:09 +1000773{
Eric Anholtc153f452007-09-03 12:06:45 +1000774 drm_mga_dma_bootstrap_t *bootstrap = data;
Dave Airlie6795c982005-07-10 18:20:09 +1000775 int err;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100776 static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
777 const drm_mga_private_t *const dev_priv =
778 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000779
Eric Anholtc153f452007-09-03 12:06:45 +1000780 err = mga_do_dma_bootstrap(dev, bootstrap);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100781 if (err) {
782 mga_do_cleanup_dma(dev, FULL_CLEANUP);
783 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000784 }
785
Dave Airlie7ccf8002005-11-11 23:11:34 +1100786 if (dev_priv->agp_textures != NULL) {
Eric Anholtc153f452007-09-03 12:06:45 +1000787 bootstrap->texture_handle = dev_priv->agp_textures->offset;
788 bootstrap->texture_size = dev_priv->agp_textures->size;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100789 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000790 bootstrap->texture_handle = 0;
791 bootstrap->texture_size = 0;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100792 }
793
Eric Anholtc153f452007-09-03 12:06:45 +1000794 bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
Dave Airlie7ccf8002005-11-11 23:11:34 +1100795
Dave Airlie6795c982005-07-10 18:20:09 +1000796 return err;
797}
798
Dave Airlieeddca552007-07-11 16:09:54 +1000799static int mga_do_init_dma(struct drm_device * dev, drm_mga_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 drm_mga_private_t *dev_priv;
802 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Dave Airlie6795c982005-07-10 18:20:09 +1000805 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Airlie6795c982005-07-10 18:20:09 +1000807 if (init->sgram) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
809 } else {
810 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
811 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 dev_priv->maccess = init->maccess;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 dev_priv->fb_cpp = init->fb_cpp;
815 dev_priv->front_offset = init->front_offset;
816 dev_priv->front_pitch = init->front_pitch;
817 dev_priv->back_offset = init->back_offset;
818 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 dev_priv->depth_cpp = init->depth_cpp;
821 dev_priv->depth_offset = init->depth_offset;
822 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* FIXME: Need to support AGP textures...
825 */
826 dev_priv->texture_offset = init->texture_offset[0];
827 dev_priv->texture_size = init->texture_size[0];
828
Dave Airlieda509d72007-05-26 05:04:51 +1000829 dev_priv->sarea = drm_getsarea(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000830 if (!dev_priv->sarea) {
831 DRM_ERROR("failed to find sarea!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 if (!dev_priv->used_new_dma_init) {
Dave Airlie11909d62005-10-19 21:23:51 -0700836
837 dev_priv->dma_access = MGA_PAGPXFER;
838 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
839
Dave Airlie6795c982005-07-10 18:20:09 +1000840 dev_priv->status = drm_core_findmap(dev, init->status_offset);
841 if (!dev_priv->status) {
842 DRM_ERROR("failed to find status page!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000843 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000844 }
845 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
846 if (!dev_priv->mmio) {
847 DRM_ERROR("failed to find mmio region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000848 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000849 }
850 dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
851 if (!dev_priv->warp) {
852 DRM_ERROR("failed to find warp microcode region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000853 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000854 }
855 dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
856 if (!dev_priv->primary) {
857 DRM_ERROR("failed to find primary dma region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000858 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000859 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000860 dev->agp_buffer_token = init->buffers_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 dev->agp_buffer_map =
862 drm_core_findmap(dev, init->buffers_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000863 if (!dev->agp_buffer_map) {
864 DRM_ERROR("failed to find dma buffer region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000865 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000866 }
867
868 drm_core_ioremap(dev_priv->warp, dev);
869 drm_core_ioremap(dev_priv->primary, dev);
870 drm_core_ioremap(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
875 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dave Airlie6795c982005-07-10 18:20:09 +1000877 if (!dev_priv->warp->handle ||
878 !dev_priv->primary->handle ||
879 ((dev_priv->dma_access != 0) &&
880 ((dev->agp_buffer_map == NULL) ||
881 (dev->agp_buffer_map->handle == NULL)))) {
882 DRM_ERROR("failed to ioremap agp regions!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000883 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Dave Airlie6795c982005-07-10 18:20:09 +1000886 ret = mga_warp_install_microcode(dev_priv);
887 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100888 DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return ret;
890 }
891
Dave Airlie6795c982005-07-10 18:20:09 +1000892 ret = mga_warp_init(dev_priv);
893 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100894 DRM_ERROR("failed to init WARP engine!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896 }
897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 dev_priv->prim.status = (u32 *) dev_priv->status->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Init the primary DMA registers.
903 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000904 MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#if 0
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
907 MGA_PRIMPTREN1); /* DWGSYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#endif
909
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
911 dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 + dev_priv->primary->size);
913 dev_priv->prim.size = dev_priv->primary->size;
914
915 dev_priv->prim.tail = 0;
916 dev_priv->prim.space = dev_priv->prim.size;
917 dev_priv->prim.wrapped = 0;
918
919 dev_priv->prim.last_flush = 0;
920 dev_priv->prim.last_wrap = 0;
921
922 dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
923
924 dev_priv->prim.status[0] = dev_priv->primary->offset;
925 dev_priv->prim.status[1] = 0;
926
927 dev_priv->sarea_priv->last_wrap = 0;
928 dev_priv->sarea_priv->last_frame.head = 0;
929 dev_priv->sarea_priv->last_frame.wrap = 0;
930
Dave Airlie6795c982005-07-10 18:20:09 +1000931 if (mga_freelist_init(dev, dev_priv) < 0) {
932 DRM_ERROR("could not initialize freelist\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000933 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
Dave Airlieeddca552007-07-11 16:09:54 +1000939static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Dave Airlie6795c982005-07-10 18:20:09 +1000941 int err = 0;
942 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* Make sure interrupts are disabled here because the uninstall ioctl
945 * may not have been called from userspace and after dev_private
946 * is freed, it's too late.
947 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 if (dev->irq_enabled)
949 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 drm_mga_private_t *dev_priv = dev->dev_private;
953
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 if ((dev_priv->warp != NULL)
Dave Airlie11909d62005-10-19 21:23:51 -0700955 && (dev_priv->warp->type != _DRM_CONSISTENT))
Dave Airlie6795c982005-07-10 18:20:09 +1000956 drm_core_ioremapfree(dev_priv->warp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 if ((dev_priv->primary != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000959 && (dev_priv->primary->type != _DRM_CONSISTENT))
960 drm_core_ioremapfree(dev_priv->primary, dev);
961
962 if (dev->agp_buffer_map != NULL)
963 drm_core_ioremapfree(dev->agp_buffer_map, dev);
964
965 if (dev_priv->used_new_dma_init) {
Dave Airlie908f9c42005-09-05 21:51:30 +1000966#if __OS_HAS_AGP
Dave Airlie7ccf8002005-11-11 23:11:34 +1100967 if (dev_priv->agp_handle != 0) {
Dave Airlieeddca552007-07-11 16:09:54 +1000968 struct drm_agp_binding unbind_req;
969 struct drm_agp_buffer free_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000970
Dave Airlie7ccf8002005-11-11 23:11:34 +1100971 unbind_req.handle = dev_priv->agp_handle;
972 drm_agp_unbind(dev, &unbind_req);
973
974 free_req.handle = dev_priv->agp_handle;
975 drm_agp_free(dev, &free_req);
Dave Airliebc5f4522007-11-05 12:50:58 +1000976
Dave Airlie7ccf8002005-11-11 23:11:34 +1100977 dev_priv->agp_textures = NULL;
978 dev_priv->agp_size = 0;
979 dev_priv->agp_handle = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000980 }
981
982 if ((dev->agp != NULL) && dev->agp->acquired) {
983 err = drm_agp_release(dev);
984 }
Dave Airlie908f9c42005-09-05 21:51:30 +1000985#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Dave Airlie6795c982005-07-10 18:20:09 +1000988 dev_priv->warp = NULL;
989 dev_priv->primary = NULL;
Dave Airlie6795c982005-07-10 18:20:09 +1000990 dev_priv->sarea = NULL;
991 dev_priv->sarea_priv = NULL;
992 dev->agp_buffer_map = NULL;
993
Dave Airlie7ccf8002005-11-11 23:11:34 +1100994 if (full_cleanup) {
995 dev_priv->mmio = NULL;
996 dev_priv->status = NULL;
997 dev_priv->used_new_dma_init = 0;
998 }
999
Dave Airlie6795c982005-07-10 18:20:09 +10001000 memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
1001 dev_priv->warp_pipe = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001002 memset(dev_priv->warp_pipe_phys, 0,
1003 sizeof(dev_priv->warp_pipe_phys));
Dave Airlie6795c982005-07-10 18:20:09 +10001004
1005 if (dev_priv->head != NULL) {
1006 mga_freelist_cleanup(dev);
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Jesper Juhla96ca102007-12-17 09:47:17 +10001010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Eric Anholtc153f452007-09-03 12:06:45 +10001013int mga_dma_init(struct drm_device *dev, void *data,
1014 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Eric Anholtc153f452007-09-03 12:06:45 +10001016 drm_mga_init_t *init = data;
Dave Airlie6795c982005-07-10 18:20:09 +10001017 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Eric Anholt6c340ea2007-08-25 20:23:09 +10001019 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Eric Anholtc153f452007-09-03 12:06:45 +10001021 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case MGA_INIT_DMA:
Eric Anholtc153f452007-09-03 12:06:45 +10001023 err = mga_do_init_dma(dev, init);
Dave Airlie6795c982005-07-10 18:20:09 +10001024 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +11001025 (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +10001026 }
1027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case MGA_CLEANUP_DMA:
Dave Airlie7ccf8002005-11-11 23:11:34 +11001029 return mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
Eric Anholt20caafa2007-08-25 19:22:43 +10001032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/* ================================================================
1036 * Primary DMA stream management
1037 */
1038
Eric Anholtc153f452007-09-03 12:06:45 +10001039int mga_dma_flush(struct drm_device *dev, void *data,
1040 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001043 struct drm_lock *lock = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Eric Anholt6c340ea2007-08-25 20:23:09 +10001045 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 DRM_DEBUG("%s%s%s\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001048 (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
1049 (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
1050 (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 WRAP_WAIT_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric Anholtc153f452007-09-03 12:06:45 +10001054 if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 mga_do_dma_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Eric Anholtc153f452007-09-03 12:06:45 +10001058 if (lock->flags & _DRM_LOCK_QUIESCENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 int ret = mga_do_wait_for_idle(dev_priv);
1061 if (ret < 0)
Márton Németh3e684ea2008-01-24 15:58:57 +10001062 DRM_INFO("-EBUSY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return ret;
1064#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#endif
1067 } else {
1068 return 0;
1069 }
1070}
1071
Eric Anholtc153f452007-09-03 12:06:45 +10001072int mga_dma_reset(struct drm_device *dev, void *data,
1073 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Eric Anholt6c340ea2007-08-25 20:23:09 +10001077 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 return mga_do_dma_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/* ================================================================
1083 * DMA buffer management
1084 */
1085
Eric Anholt6c340ea2007-08-25 20:23:09 +10001086static int mga_dma_get_buffers(struct drm_device * dev,
1087 struct drm_file *file_priv, struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Dave Airlie056219e2007-07-11 16:17:42 +10001089 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int i;
1091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 for (i = d->granted_count; i < d->request_count; i++) {
1093 buf = mga_freelist_get(dev);
1094 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001095 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Eric Anholt6c340ea2007-08-25 20:23:09 +10001097 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 if (DRM_COPY_TO_USER(&d->request_indices[i],
1100 &buf->idx, sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001101 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 if (DRM_COPY_TO_USER(&d->request_sizes[i],
1103 &buf->total, sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001104 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 d->granted_count++;
1107 }
1108 return 0;
1109}
1110
Eric Anholtc153f452007-09-03 12:06:45 +10001111int mga_dma_buffers(struct drm_device *dev, void *data,
1112 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Dave Airliecdd55a22007-07-11 16:32:08 +10001114 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001116 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int ret = 0;
1118
Eric Anholt6c340ea2007-08-25 20:23:09 +10001119 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Please don't send us buffers.
1122 */
Eric Anholtc153f452007-09-03 12:06:45 +10001123 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001125 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 /* We'll send you buffers.
1130 */
Eric Anholtc153f452007-09-03 12:06:45 +10001131 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001133 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137 WRAP_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Eric Anholtc153f452007-09-03 12:06:45 +10001139 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Eric Anholtc153f452007-09-03 12:06:45 +10001141 if (d->request_count) {
1142 ret = mga_dma_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return ret;
1146}
1147
Dave Airlie6795c982005-07-10 18:20:09 +10001148/**
1149 * Called just before the module is unloaded.
1150 */
Dave Airlieeddca552007-07-11 16:09:54 +10001151int mga_driver_unload(struct drm_device * dev)
Dave Airlie6795c982005-07-10 18:20:09 +10001152{
1153 drm_free(dev->dev_private, sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
1154 dev->dev_private = NULL;
1155
1156 return 0;
1157}
1158
1159/**
1160 * Called when the last opener of the device is closed.
1161 */
Dave Airlieeddca552007-07-11 16:09:54 +10001162void mga_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Dave Airlie7ccf8002005-11-11 23:11:34 +11001164 mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Dave Airlieeddca552007-07-11 16:09:54 +10001167int mga_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 drm_mga_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}