blob: 3c917fb3a60b85398d84d456d36093d02924525b [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
Eric Anholt9a298b22009-03-24 12:23:04 -0700257 dev_priv->head = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 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 SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Eric Anholt9a298b22009-03-24 12:23:04 -0700267 entry = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 if (entry == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000269 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 entry->next = dev_priv->head->next;
272 entry->prev = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 entry->buf = buf;
275
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 if (dev_priv->head->next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dev_priv->head->next->prev = entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 if (entry->next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dev_priv->tail = entry;
280
281 buf_priv->list_entry = entry;
282 buf_priv->discard = 0;
283 buf_priv->dispatched = 0;
284
285 dev_priv->head->next = entry;
286 }
287
288 return 0;
289}
290
Dave Airlieeddca552007-07-11 16:09:54 +1000291static void mga_freelist_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 drm_mga_private_t *dev_priv = dev->dev_private;
294 drm_mga_freelist_t *entry;
295 drm_mga_freelist_t *next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 entry = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 next = entry->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700301 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 entry = next;
303 }
304
305 dev_priv->head = dev_priv->tail = NULL;
306}
307
308#if 0
309/* FIXME: Still needed?
310 */
Dave Airlieeddca552007-07-11 16:09:54 +1000311static void mga_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Dave Airliecdd55a22007-07-11 16:32:08 +1000313 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000314 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 drm_mga_buf_priv_t *buf_priv;
316 int i;
317
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 buf_priv = buf->dev_private;
321 SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323}
324#endif
325
Dave Airlie056219e2007-07-11 16:17:42 +1000326static struct drm_buf *mga_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 drm_mga_private_t *dev_priv = dev->dev_private;
329 drm_mga_freelist_t *next;
330 drm_mga_freelist_t *prev;
331 drm_mga_freelist_t *tail = dev_priv->tail;
332 u32 head, wrap;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 wrap = dev_priv->sarea_priv->last_wrap;
337
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 DRM_DEBUG(" tail=0x%06lx %d\n",
339 tail->age.head ?
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100340 (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 tail->age.wrap);
342 DRM_DEBUG(" head=0x%06lx %d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100343 (unsigned long)(head - dev_priv->primary->offset), wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 if (TEST_AGE(&tail->age, head, wrap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 prev = dev_priv->tail->prev;
347 next = dev_priv->tail;
348 prev->next = NULL;
349 next->prev = next->next = NULL;
350 dev_priv->tail = prev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 SET_AGE(&next->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return next->buf;
353 }
354
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357}
358
Dave Airlie056219e2007-07-11 16:17:42 +1000359int mga_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 drm_mga_private_t *dev_priv = dev->dev_private;
362 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
363 drm_mga_freelist_t *head, *entry, *prev;
364
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 DRM_DEBUG("age=0x%06lx wrap=%d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100366 (unsigned long)(buf_priv->list_entry->age.head -
367 dev_priv->primary->offset),
368 buf_priv->list_entry->age.wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 entry = buf_priv->list_entry;
371 head = dev_priv->head;
372
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
374 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 prev = dev_priv->tail;
376 prev->next = entry;
377 entry->prev = prev;
378 entry->next = NULL;
379 } else {
380 prev = head->next;
381 head->next = entry;
382 prev->prev = entry;
383 entry->prev = head;
384 entry->next = prev;
385 }
386
387 return 0;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* ================================================================
391 * DMA initialization, cleanup
392 */
393
Dave Airlieeddca552007-07-11 16:09:54 +1000394int mga_driver_load(struct drm_device * dev, unsigned long flags)
Dave Airlie6795c982005-07-10 18:20:09 +1000395{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 drm_mga_private_t *dev_priv;
Keith Packard52440212008-11-18 09:30:25 -0800397 int ret;
Dave Airlie6795c982005-07-10 18:20:09 +1000398
Eric Anholt9a298b22009-03-24 12:23:04 -0700399 dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
Dave Airlie6795c982005-07-10 18:20:09 +1000400 if (!dev_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000401 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000402
403 dev->dev_private = (void *)dev_priv;
Dave Airlie6795c982005-07-10 18:20:09 +1000404
405 dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
406 dev_priv->chipset = flags;
407
Dave Airlie22eae942005-11-10 22:16:34 +1100408 dev_priv->mmio_base = drm_get_resource_start(dev, 1);
409 dev_priv->mmio_size = drm_get_resource_len(dev, 1);
410
411 dev->counters += 3;
412 dev->types[6] = _DRM_STAT_IRQ;
413 dev->types[7] = _DRM_STAT_PRIMARY;
414 dev->types[8] = _DRM_STAT_SECONDARY;
415
Keith Packard52440212008-11-18 09:30:25 -0800416 ret = drm_vblank_init(dev, 1);
417
418 if (ret) {
419 (void) mga_driver_unload(dev);
420 return ret;
421 }
422
Dave Airlie6795c982005-07-10 18:20:09 +1000423 return 0;
424}
425
Dave Airlie908f9c42005-09-05 21:51:30 +1000426#if __OS_HAS_AGP
Dave Airlie6795c982005-07-10 18:20:09 +1000427/**
428 * Bootstrap the driver for AGP DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 *
Dave Airlie6795c982005-07-10 18:20:09 +1000430 * \todo
431 * Investigate whether there is any benifit to storing the WARP microcode in
432 * AGP memory. If not, the microcode may as well always be put in PCI
433 * memory.
434 *
435 * \todo
436 * This routine needs to set dma_bs->agp_mode to the mode actually configured
437 * in the hardware. Looking just at the Linux AGP driver code, I don't see
438 * an easy way to determine this.
439 *
440 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
441 */
Dave Airlieeddca552007-07-11 16:09:54 +1000442static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000443 drm_mga_dma_bootstrap_t * dma_bs)
444{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 drm_mga_private_t *const dev_priv =
446 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100447 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000448 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 unsigned offset;
Dave Airlie6795c982005-07-10 18:20:09 +1000450 const unsigned secondary_size = dma_bs->secondary_bin_count
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 * dma_bs->secondary_bin_size;
Dave Airlie6795c982005-07-10 18:20:09 +1000452 const unsigned agp_size = (dma_bs->agp_size << 20);
Dave Airlieeddca552007-07-11 16:09:54 +1000453 struct drm_buf_desc req;
454 struct drm_agp_mode mode;
455 struct drm_agp_info info;
456 struct drm_agp_buffer agp_req;
457 struct drm_agp_binding bind_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000458
Dave Airlie6795c982005-07-10 18:20:09 +1000459 /* Acquire AGP. */
460 err = drm_agp_acquire(dev);
461 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100462 DRM_ERROR("Unable to acquire AGP: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000463 return err;
464 }
465
466 err = drm_agp_info(dev, &info);
467 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100468 DRM_ERROR("Unable to get AGP info: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000469 return err;
470 }
471
472 mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
473 err = drm_agp_enable(dev, mode);
474 if (err) {
475 DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
476 return err;
477 }
478
Dave Airlie6795c982005-07-10 18:20:09 +1000479 /* In addition to the usual AGP mode configuration, the G200 AGP cards
480 * need to have the AGP mode "manually" set.
481 */
482
483 if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
484 if (mode.mode & 0x02) {
485 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 } else {
Dave Airlie6795c982005-07-10 18:20:09 +1000487 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
488 }
489 }
490
Dave Airlie6795c982005-07-10 18:20:09 +1000491 /* Allocate and bind AGP memory. */
Dave Airlie7ccf8002005-11-11 23:11:34 +1100492 agp_req.size = agp_size;
493 agp_req.type = 0;
494 err = drm_agp_alloc(dev, &agp_req);
495 if (err) {
496 dev_priv->agp_size = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000497 DRM_ERROR("Unable to allocate %uMB AGP memory\n",
498 dma_bs->agp_size);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100499 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000500 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000501
Dave Airlie7ccf8002005-11-11 23:11:34 +1100502 dev_priv->agp_size = agp_size;
503 dev_priv->agp_handle = agp_req.handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504
Dave Airlie7ccf8002005-11-11 23:11:34 +1100505 bind_req.handle = agp_req.handle;
506 bind_req.offset = 0;
507 err = drm_agp_bind(dev, &bind_req);
Dave Airlie6795c982005-07-10 18:20:09 +1000508 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100509 DRM_ERROR("Unable to bind AGP memory: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000510 return err;
511 }
512
Dave Airlie11909d62005-10-19 21:23:51 -0700513 /* Make drm_addbufs happy by not trying to create a mapping for less
514 * than a page.
515 */
516 if (warp_size < PAGE_SIZE)
517 warp_size = PAGE_SIZE;
518
Dave Airlie6795c982005-07-10 18:20:09 +1000519 offset = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 err = drm_addmap(dev, offset, warp_size,
521 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
Dave Airlie6795c982005-07-10 18:20:09 +1000522 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100523 DRM_ERROR("Unable to map WARP microcode: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000524 return err;
525 }
526
527 offset += warp_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 err = drm_addmap(dev, offset, dma_bs->primary_size,
529 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
Dave Airlie6795c982005-07-10 18:20:09 +1000530 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100531 DRM_ERROR("Unable to map primary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000532 return err;
533 }
534
535 offset += dma_bs->primary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000536 err = drm_addmap(dev, offset, secondary_size,
537 _DRM_AGP, 0, &dev->agp_buffer_map);
Dave Airlie6795c982005-07-10 18:20:09 +1000538 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100539 DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000540 return err;
541 }
542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000544 req.count = dma_bs->secondary_bin_count;
545 req.size = dma_bs->secondary_bin_size;
546 req.flags = _DRM_AGP_BUFFER;
547 req.agp_start = offset;
548
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 err = drm_addbufs_agp(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000550 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100551 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000552 return err;
553 }
554
Dave Airlie7ccf8002005-11-11 23:11:34 +1100555 {
Dave Airlie55910512007-07-11 16:53:40 +1000556 struct drm_map_list *_entry;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100557 unsigned long agp_token = 0;
Dave Airliebc5f4522007-11-05 12:50:58 +1000558
Dave Airliebd1b3312007-05-26 05:01:51 +1000559 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100560 if (_entry->map == dev->agp_buffer_map)
561 agp_token = _entry->user_token;
562 }
563 if (!agp_token)
564 return -EFAULT;
565
566 dev->agp_buffer_token = agp_token;
567 }
568
Dave Airlie6795c982005-07-10 18:20:09 +1000569 offset += secondary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 err = drm_addmap(dev, offset, agp_size - offset,
571 _DRM_AGP, 0, &dev_priv->agp_textures);
Dave Airlie6795c982005-07-10 18:20:09 +1000572 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100573 DRM_ERROR("Unable to map AGP texture region %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000574 return err;
575 }
576
577 drm_core_ioremap(dev_priv->warp, dev);
578 drm_core_ioremap(dev_priv->primary, dev);
579 drm_core_ioremap(dev->agp_buffer_map, dev);
580
581 if (!dev_priv->warp->handle ||
582 !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
583 DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
584 dev_priv->warp->handle, dev_priv->primary->handle,
585 dev->agp_buffer_map->handle);
Eric Anholt20caafa2007-08-25 19:22:43 +1000586 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000587 }
588
589 dev_priv->dma_access = MGA_PAGPXFER;
590 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
591
592 DRM_INFO("Initialized card for AGP DMA.\n");
593 return 0;
594}
Dave Airlie908f9c42005-09-05 21:51:30 +1000595#else
Dave Airlieeddca552007-07-11 16:09:54 +1000596static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
Dave Airlie908f9c42005-09-05 21:51:30 +1000597 drm_mga_dma_bootstrap_t * dma_bs)
598{
599 return -EINVAL;
600}
601#endif
Dave Airlie6795c982005-07-10 18:20:09 +1000602
603/**
604 * Bootstrap the driver for PCI DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 *
Dave Airlie6795c982005-07-10 18:20:09 +1000606 * \todo
607 * The algorithm for decreasing the size of the primary DMA buffer could be
608 * better. The size should be rounded up to the nearest page size, then
609 * decrease the request size by a single page each pass through the loop.
610 *
611 * \todo
612 * Determine whether the maximum address passed to drm_pci_alloc is correct.
613 * The same goes for drm_addbufs_pci.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 *
Dave Airlie6795c982005-07-10 18:20:09 +1000615 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
616 */
Dave Airlieeddca552007-07-11 16:09:54 +1000617static int mga_do_pci_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000618 drm_mga_dma_bootstrap_t * dma_bs)
619{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 drm_mga_private_t *const dev_priv =
621 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100622 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000623 unsigned int primary_size;
624 unsigned int bin_count;
625 int err;
Dave Airlieeddca552007-07-11 16:09:54 +1000626 struct drm_buf_desc req;
Dave Airlie6795c982005-07-10 18:20:09 +1000627
Dave Airlie6795c982005-07-10 18:20:09 +1000628 if (dev->dma == NULL) {
629 DRM_ERROR("dev->dma is NULL\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000630 return -EFAULT;
Dave Airlie6795c982005-07-10 18:20:09 +1000631 }
632
Dave Airlie11909d62005-10-19 21:23:51 -0700633 /* Make drm_addbufs happy by not trying to create a mapping for less
634 * than a page.
635 */
636 if (warp_size < PAGE_SIZE)
637 warp_size = PAGE_SIZE;
638
Dave Airlie6795c982005-07-10 18:20:09 +1000639 /* The proper alignment is 0x100 for this mapping */
640 err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
641 _DRM_READ_ONLY, &dev_priv->warp);
642 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100643 DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
644 err);
Dave Airlie6795c982005-07-10 18:20:09 +1000645 return err;
646 }
647
648 /* Other than the bottom two bits being used to encode other
649 * information, there don't appear to be any restrictions on the
650 * alignment of the primary or secondary DMA buffers.
651 */
652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 for (primary_size = dma_bs->primary_size; primary_size != 0;
654 primary_size >>= 1) {
Dave Airlie6795c982005-07-10 18:20:09 +1000655 /* The proper alignment for this mapping is 0x04 */
656 err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
657 _DRM_READ_ONLY, &dev_priv->primary);
658 if (!err)
659 break;
660 }
661
662 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100663 DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
Eric Anholt20caafa2007-08-25 19:22:43 +1000664 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000665 }
666
667 if (dev_priv->primary->size != dma_bs->primary_size) {
668 DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 dma_bs->primary_size,
670 (unsigned)dev_priv->primary->size);
Dave Airlie6795c982005-07-10 18:20:09 +1000671 dma_bs->primary_size = dev_priv->primary->size;
672 }
673
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
675 bin_count--) {
676 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000677 req.count = bin_count;
678 req.size = dma_bs->secondary_bin_size;
679
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 err = drm_addbufs_pci(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000681 if (!err) {
682 break;
683 }
684 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685
Dave Airlie6795c982005-07-10 18:20:09 +1000686 if (bin_count == 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100687 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000688 return err;
689 }
690
691 if (bin_count != dma_bs->secondary_bin_count) {
692 DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
693 "to %u.\n", dma_bs->secondary_bin_count, bin_count);
694
695 dma_bs->secondary_bin_count = bin_count;
696 }
697
698 dev_priv->dma_access = 0;
699 dev_priv->wagp_enable = 0;
700
701 dma_bs->agp_mode = 0;
702
703 DRM_INFO("Initialized card for PCI DMA.\n");
704 return 0;
705}
706
Dave Airlieeddca552007-07-11 16:09:54 +1000707static int mga_do_dma_bootstrap(struct drm_device * dev,
Dave Airlie6795c982005-07-10 18:20:09 +1000708 drm_mga_dma_bootstrap_t * dma_bs)
709{
710 const int is_agp = (dma_bs->agp_mode != 0) && drm_device_is_agp(dev);
711 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 drm_mga_private_t *const dev_priv =
713 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000714
715 dev_priv->used_new_dma_init = 1;
716
717 /* The first steps are the same for both PCI and AGP based DMA. Map
718 * the cards MMIO registers and map a status page.
719 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
721 _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio);
Dave Airlie6795c982005-07-10 18:20:09 +1000722 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100723 DRM_ERROR("Unable to map MMIO region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000724 return err;
725 }
726
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
728 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
729 &dev_priv->status);
Dave Airlie6795c982005-07-10 18:20:09 +1000730 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100731 DRM_ERROR("Unable to map status region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000732 return err;
733 }
734
Dave Airlie6795c982005-07-10 18:20:09 +1000735 /* The DMA initialization procedure is slightly different for PCI and
736 * AGP cards. AGP cards just allocate a large block of AGP memory and
737 * carve off portions of it for internal uses. The remaining memory
738 * is returned to user-mode to be used for AGP textures.
739 */
Dave Airlie6795c982005-07-10 18:20:09 +1000740 if (is_agp) {
741 err = mga_do_agp_dma_bootstrap(dev, dma_bs);
742 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743
Dave Airlie6795c982005-07-10 18:20:09 +1000744 /* If we attempted to initialize the card for AGP DMA but failed,
745 * clean-up any mess that may have been created.
746 */
747
748 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100749 mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +1000750 }
751
Dave Airlie6795c982005-07-10 18:20:09 +1000752 /* Not only do we want to try and initialized PCI cards for PCI DMA,
753 * but we also try to initialized AGP cards that could not be
754 * initialized for AGP DMA. This covers the case where we have an AGP
755 * card in a system with an unsupported AGP chipset. In that case the
756 * card will be detected as AGP, but we won't be able to allocate any
757 * AGP memory, etc.
758 */
759
760 if (!is_agp || err) {
761 err = mga_do_pci_dma_bootstrap(dev, dma_bs);
762 }
763
Dave Airlie6795c982005-07-10 18:20:09 +1000764 return err;
765}
766
Eric Anholtc153f452007-09-03 12:06:45 +1000767int mga_dma_bootstrap(struct drm_device *dev, void *data,
768 struct drm_file *file_priv)
Dave Airlie6795c982005-07-10 18:20:09 +1000769{
Eric Anholtc153f452007-09-03 12:06:45 +1000770 drm_mga_dma_bootstrap_t *bootstrap = data;
Dave Airlie6795c982005-07-10 18:20:09 +1000771 int err;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100772 static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
773 const drm_mga_private_t *const dev_priv =
774 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000775
Eric Anholtc153f452007-09-03 12:06:45 +1000776 err = mga_do_dma_bootstrap(dev, bootstrap);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100777 if (err) {
778 mga_do_cleanup_dma(dev, FULL_CLEANUP);
779 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000780 }
781
Dave Airlie7ccf8002005-11-11 23:11:34 +1100782 if (dev_priv->agp_textures != NULL) {
Eric Anholtc153f452007-09-03 12:06:45 +1000783 bootstrap->texture_handle = dev_priv->agp_textures->offset;
784 bootstrap->texture_size = dev_priv->agp_textures->size;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100785 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000786 bootstrap->texture_handle = 0;
787 bootstrap->texture_size = 0;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100788 }
789
Eric Anholtc153f452007-09-03 12:06:45 +1000790 bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
Dave Airlie7ccf8002005-11-11 23:11:34 +1100791
Dave Airlie6795c982005-07-10 18:20:09 +1000792 return err;
793}
794
Dave Airlieeddca552007-07-11 16:09:54 +1000795static int mga_do_init_dma(struct drm_device * dev, drm_mga_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 drm_mga_private_t *dev_priv;
798 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlie6795c982005-07-10 18:20:09 +1000801 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlie6795c982005-07-10 18:20:09 +1000803 if (init->sgram) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
805 } else {
806 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
807 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 dev_priv->maccess = init->maccess;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 dev_priv->fb_cpp = init->fb_cpp;
811 dev_priv->front_offset = init->front_offset;
812 dev_priv->front_pitch = init->front_pitch;
813 dev_priv->back_offset = init->back_offset;
814 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 dev_priv->depth_cpp = init->depth_cpp;
817 dev_priv->depth_offset = init->depth_offset;
818 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* FIXME: Need to support AGP textures...
821 */
822 dev_priv->texture_offset = init->texture_offset[0];
823 dev_priv->texture_size = init->texture_size[0];
824
Dave Airlieda509d72007-05-26 05:04:51 +1000825 dev_priv->sarea = drm_getsarea(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000826 if (!dev_priv->sarea) {
827 DRM_ERROR("failed to find sarea!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000828 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 if (!dev_priv->used_new_dma_init) {
Dave Airlie11909d62005-10-19 21:23:51 -0700832
833 dev_priv->dma_access = MGA_PAGPXFER;
834 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
835
Dave Airlie6795c982005-07-10 18:20:09 +1000836 dev_priv->status = drm_core_findmap(dev, init->status_offset);
837 if (!dev_priv->status) {
838 DRM_ERROR("failed to find status page!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000839 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000840 }
841 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
842 if (!dev_priv->mmio) {
843 DRM_ERROR("failed to find mmio region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000844 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000845 }
846 dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
847 if (!dev_priv->warp) {
848 DRM_ERROR("failed to find warp microcode region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000849 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000850 }
851 dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
852 if (!dev_priv->primary) {
853 DRM_ERROR("failed to find primary dma region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000854 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000855 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000856 dev->agp_buffer_token = init->buffers_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 dev->agp_buffer_map =
858 drm_core_findmap(dev, init->buffers_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000859 if (!dev->agp_buffer_map) {
860 DRM_ERROR("failed to find dma buffer region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000861 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000862 }
863
864 drm_core_ioremap(dev_priv->warp, dev);
865 drm_core_ioremap(dev_priv->primary, dev);
866 drm_core_ioremap(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
871 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Dave Airlie6795c982005-07-10 18:20:09 +1000873 if (!dev_priv->warp->handle ||
874 !dev_priv->primary->handle ||
875 ((dev_priv->dma_access != 0) &&
876 ((dev->agp_buffer_map == NULL) ||
877 (dev->agp_buffer_map->handle == NULL)))) {
878 DRM_ERROR("failed to ioremap agp regions!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000879 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Dave Airlie6795c982005-07-10 18:20:09 +1000882 ret = mga_warp_install_microcode(dev_priv);
883 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100884 DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return ret;
886 }
887
Dave Airlie6795c982005-07-10 18:20:09 +1000888 ret = mga_warp_init(dev_priv);
889 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100890 DRM_ERROR("failed to init WARP engine!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return ret;
892 }
893
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 dev_priv->prim.status = (u32 *) dev_priv->status->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* Init the primary DMA registers.
899 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#if 0
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
903 MGA_PRIMPTREN1); /* DWGSYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#endif
905
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
907 dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 + dev_priv->primary->size);
909 dev_priv->prim.size = dev_priv->primary->size;
910
911 dev_priv->prim.tail = 0;
912 dev_priv->prim.space = dev_priv->prim.size;
913 dev_priv->prim.wrapped = 0;
914
915 dev_priv->prim.last_flush = 0;
916 dev_priv->prim.last_wrap = 0;
917
918 dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
919
920 dev_priv->prim.status[0] = dev_priv->primary->offset;
921 dev_priv->prim.status[1] = 0;
922
923 dev_priv->sarea_priv->last_wrap = 0;
924 dev_priv->sarea_priv->last_frame.head = 0;
925 dev_priv->sarea_priv->last_frame.wrap = 0;
926
Dave Airlie6795c982005-07-10 18:20:09 +1000927 if (mga_freelist_init(dev, dev_priv) < 0) {
928 DRM_ERROR("could not initialize freelist\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000929 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
934
Dave Airlieeddca552007-07-11 16:09:54 +1000935static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Dave Airlie6795c982005-07-10 18:20:09 +1000937 int err = 0;
938 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* Make sure interrupts are disabled here because the uninstall ioctl
941 * may not have been called from userspace and after dev_private
942 * is freed, it's too late.
943 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 if (dev->irq_enabled)
945 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 drm_mga_private_t *dev_priv = dev->dev_private;
949
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 if ((dev_priv->warp != NULL)
Dave Airlie11909d62005-10-19 21:23:51 -0700951 && (dev_priv->warp->type != _DRM_CONSISTENT))
Dave Airlie6795c982005-07-10 18:20:09 +1000952 drm_core_ioremapfree(dev_priv->warp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 if ((dev_priv->primary != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000955 && (dev_priv->primary->type != _DRM_CONSISTENT))
956 drm_core_ioremapfree(dev_priv->primary, dev);
957
958 if (dev->agp_buffer_map != NULL)
959 drm_core_ioremapfree(dev->agp_buffer_map, dev);
960
961 if (dev_priv->used_new_dma_init) {
Dave Airlie908f9c42005-09-05 21:51:30 +1000962#if __OS_HAS_AGP
Dave Airlie7ccf8002005-11-11 23:11:34 +1100963 if (dev_priv->agp_handle != 0) {
Dave Airlieeddca552007-07-11 16:09:54 +1000964 struct drm_agp_binding unbind_req;
965 struct drm_agp_buffer free_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000966
Dave Airlie7ccf8002005-11-11 23:11:34 +1100967 unbind_req.handle = dev_priv->agp_handle;
968 drm_agp_unbind(dev, &unbind_req);
969
970 free_req.handle = dev_priv->agp_handle;
971 drm_agp_free(dev, &free_req);
Dave Airliebc5f4522007-11-05 12:50:58 +1000972
Dave Airlie7ccf8002005-11-11 23:11:34 +1100973 dev_priv->agp_textures = NULL;
974 dev_priv->agp_size = 0;
975 dev_priv->agp_handle = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000976 }
977
978 if ((dev->agp != NULL) && dev->agp->acquired) {
979 err = drm_agp_release(dev);
980 }
Dave Airlie908f9c42005-09-05 21:51:30 +1000981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
Dave Airlie6795c982005-07-10 18:20:09 +1000984 dev_priv->warp = NULL;
985 dev_priv->primary = NULL;
Dave Airlie6795c982005-07-10 18:20:09 +1000986 dev_priv->sarea = NULL;
987 dev_priv->sarea_priv = NULL;
988 dev->agp_buffer_map = NULL;
989
Dave Airlie7ccf8002005-11-11 23:11:34 +1100990 if (full_cleanup) {
991 dev_priv->mmio = NULL;
992 dev_priv->status = NULL;
993 dev_priv->used_new_dma_init = 0;
994 }
995
Dave Airlie6795c982005-07-10 18:20:09 +1000996 memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
997 dev_priv->warp_pipe = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 memset(dev_priv->warp_pipe_phys, 0,
999 sizeof(dev_priv->warp_pipe_phys));
Dave Airlie6795c982005-07-10 18:20:09 +10001000
1001 if (dev_priv->head != NULL) {
1002 mga_freelist_cleanup(dev);
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Jesper Juhla96ca102007-12-17 09:47:17 +10001006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Eric Anholtc153f452007-09-03 12:06:45 +10001009int mga_dma_init(struct drm_device *dev, void *data,
1010 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Eric Anholtc153f452007-09-03 12:06:45 +10001012 drm_mga_init_t *init = data;
Dave Airlie6795c982005-07-10 18:20:09 +10001013 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Anholt6c340ea2007-08-25 20:23:09 +10001015 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Anholtc153f452007-09-03 12:06:45 +10001017 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 case MGA_INIT_DMA:
Eric Anholtc153f452007-09-03 12:06:45 +10001019 err = mga_do_init_dma(dev, init);
Dave Airlie6795c982005-07-10 18:20:09 +10001020 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +11001021 (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +10001022 }
1023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 case MGA_CLEANUP_DMA:
Dave Airlie7ccf8002005-11-11 23:11:34 +11001025 return mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Eric Anholt20caafa2007-08-25 19:22:43 +10001028 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/* ================================================================
1032 * Primary DMA stream management
1033 */
1034
Eric Anholtc153f452007-09-03 12:06:45 +10001035int mga_dma_flush(struct drm_device *dev, void *data,
1036 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001039 struct drm_lock *lock = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Eric Anholt6c340ea2007-08-25 20:23:09 +10001041 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 DRM_DEBUG("%s%s%s\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001044 (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
1045 (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
1046 (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 WRAP_WAIT_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Eric Anholtc153f452007-09-03 12:06:45 +10001050 if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 mga_do_dma_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Eric Anholtc153f452007-09-03 12:06:45 +10001054 if (lock->flags & _DRM_LOCK_QUIESCENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 int ret = mga_do_wait_for_idle(dev_priv);
1057 if (ret < 0)
Márton Németh3e684ea2008-01-24 15:58:57 +10001058 DRM_INFO("-EBUSY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return ret;
1060#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
1063 } else {
1064 return 0;
1065 }
1066}
1067
Eric Anholtc153f452007-09-03 12:06:45 +10001068int mga_dma_reset(struct drm_device *dev, void *data,
1069 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Eric Anholt6c340ea2007-08-25 20:23:09 +10001073 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 return mga_do_dma_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/* ================================================================
1079 * DMA buffer management
1080 */
1081
Eric Anholt6c340ea2007-08-25 20:23:09 +10001082static int mga_dma_get_buffers(struct drm_device * dev,
1083 struct drm_file *file_priv, struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Dave Airlie056219e2007-07-11 16:17:42 +10001085 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int i;
1087
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 for (i = d->granted_count; i < d->request_count; i++) {
1089 buf = mga_freelist_get(dev);
1090 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001091 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Eric Anholt6c340ea2007-08-25 20:23:09 +10001093 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 if (DRM_COPY_TO_USER(&d->request_indices[i],
1096 &buf->idx, sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001097 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001098 if (DRM_COPY_TO_USER(&d->request_sizes[i],
1099 &buf->total, sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001100 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 d->granted_count++;
1103 }
1104 return 0;
1105}
1106
Eric Anholtc153f452007-09-03 12:06:45 +10001107int mga_dma_buffers(struct drm_device *dev, void *data,
1108 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Dave Airliecdd55a22007-07-11 16:32:08 +10001110 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001112 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int ret = 0;
1114
Eric Anholt6c340ea2007-08-25 20:23:09 +10001115 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Please don't send us buffers.
1118 */
Eric Anholtc153f452007-09-03 12:06:45 +10001119 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001121 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 /* We'll send you buffers.
1126 */
Eric Anholtc153f452007-09-03 12:06:45 +10001127 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001129 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 WRAP_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Eric Anholtc153f452007-09-03 12:06:45 +10001135 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Eric Anholtc153f452007-09-03 12:06:45 +10001137 if (d->request_count) {
1138 ret = mga_dma_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return ret;
1142}
1143
Dave Airlie6795c982005-07-10 18:20:09 +10001144/**
1145 * Called just before the module is unloaded.
1146 */
Dave Airlieeddca552007-07-11 16:09:54 +10001147int mga_driver_unload(struct drm_device * dev)
Dave Airlie6795c982005-07-10 18:20:09 +10001148{
Eric Anholt9a298b22009-03-24 12:23:04 -07001149 kfree(dev->dev_private);
Dave Airlie6795c982005-07-10 18:20:09 +10001150 dev->dev_private = NULL;
1151
1152 return 0;
1153}
1154
1155/**
1156 * Called when the last opener of the device is closed.
1157 */
Dave Airlieeddca552007-07-11 16:09:54 +10001158void mga_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Dave Airlie7ccf8002005-11-11 23:11:34 +11001160 mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Dave Airlieeddca552007-07-11 16:09:54 +10001163int mga_driver_dma_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 drm_mga_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001166 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}