blob: 5ccb65deb83c2f956eb67a1adbf505c8d1e8d382 [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
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +020055int 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
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +020077static 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
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200105void 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
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200145 if (head <= tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 primary->space = primary->size - primary->tail;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200147 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 primary->space = head - tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100150 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
151 DRM_DEBUG(" tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 DRM_DEBUG(" space = 0x%06x\n", primary->space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000155 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200160void mga_do_dma_wrap_start(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
163 u32 head, tail;
164 DMA_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 BEGIN_DMA_WRAP();
168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
170 MGA_DMAPAD, 0x00000000,
171 MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 ADVANCE_DMA();
174
175 tail = primary->tail + dev_priv->primary->offset;
176
177 primary->tail = 0;
178 primary->last_flush = 0;
179 primary->last_wrap++;
180
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200183 if (head == dev_priv->primary->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 primary->space = primary->size;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200185 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 primary->space = head - dev_priv->primary->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100188 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 DRM_DEBUG(" tail = 0x%06x\n", primary->tail);
190 DRM_DEBUG(" wrap = %d\n", primary->last_wrap);
191 DRM_DEBUG(" space = 0x%06x\n", primary->space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000194 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 set_bit(0, &primary->wrapped);
197 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200200void mga_do_dma_wrap_end(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
203 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
204 u32 head = dev_priv->primary->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 sarea_priv->last_wrap++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 mga_flush_write_combine();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 clear_bit(0, &primary->wrapped);
214 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* ================================================================
218 * Freelist management
219 */
220
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200221#define MGA_BUFFER_USED (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#define MGA_BUFFER_FREE 0
223
224#if MGA_FREELIST_DEBUG
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200225static void mga_freelist_print(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 drm_mga_private_t *dev_priv = dev->dev_private;
228 drm_mga_freelist_t *entry;
229
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 DRM_INFO("\n");
231 DRM_INFO("current dispatch: last=0x%x done=0x%x\n",
232 dev_priv->sarea_priv->last_dispatch,
233 (unsigned int)(MGA_READ(MGA_PRIMADDRESS) -
234 dev_priv->primary->offset));
235 DRM_INFO("current freelist:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 for (entry = dev_priv->head->next; entry; entry = entry->next) {
238 DRM_INFO(" %p idx=%2d age=0x%x 0x%06lx\n",
239 entry, entry->buf->idx, entry->age.head,
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100240 (unsigned long)(entry->age.head - dev_priv->primary->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 DRM_INFO("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244#endif
245
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200246static int mga_freelist_init(struct drm_device *dev, drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Dave Airliecdd55a22007-07-11 16:32:08 +1000248 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000249 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 drm_mga_buf_priv_t *buf_priv;
251 drm_mga_freelist_t *entry;
252 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 DRM_DEBUG("count=%d\n", dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Eric Anholt9a298b22009-03-24 12:23:04 -0700255 dev_priv->head = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000257 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Eric Anholt9a298b22009-03-24 12:23:04 -0700265 entry = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 if (entry == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000267 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 entry->next = dev_priv->head->next;
270 entry->prev = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 entry->buf = buf;
273
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 if (dev_priv->head->next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dev_priv->head->next->prev = entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 if (entry->next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dev_priv->tail = entry;
278
279 buf_priv->list_entry = entry;
280 buf_priv->discard = 0;
281 buf_priv->dispatched = 0;
282
283 dev_priv->head->next = entry;
284 }
285
286 return 0;
287}
288
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200289static void mga_freelist_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 drm_mga_private_t *dev_priv = dev->dev_private;
292 drm_mga_freelist_t *entry;
293 drm_mga_freelist_t *next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 entry = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 next = entry->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700299 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 entry = next;
301 }
302
303 dev_priv->head = dev_priv->tail = NULL;
304}
305
306#if 0
307/* FIXME: Still needed?
308 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200309static void mga_freelist_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Dave Airliecdd55a22007-07-11 16:32:08 +1000311 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000312 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 drm_mga_buf_priv_t *buf_priv;
314 int i;
315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 buf_priv = buf->dev_private;
319 SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
322#endif
323
Dave Airlie056219e2007-07-11 16:17:42 +1000324static struct drm_buf *mga_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 drm_mga_private_t *dev_priv = dev->dev_private;
327 drm_mga_freelist_t *next;
328 drm_mga_freelist_t *prev;
329 drm_mga_freelist_t *tail = dev_priv->tail;
330 u32 head, wrap;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 wrap = dev_priv->sarea_priv->last_wrap;
335
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 DRM_DEBUG(" tail=0x%06lx %d\n",
337 tail->age.head ?
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100338 (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 tail->age.wrap);
340 DRM_DEBUG(" head=0x%06lx %d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100341 (unsigned long)(head - dev_priv->primary->offset), wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 if (TEST_AGE(&tail->age, head, wrap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 prev = dev_priv->tail->prev;
345 next = dev_priv->tail;
346 prev->next = NULL;
347 next->prev = next->next = NULL;
348 dev_priv->tail = prev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 SET_AGE(&next->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return next->buf;
351 }
352
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return NULL;
355}
356
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200357int mga_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 drm_mga_private_t *dev_priv = dev->dev_private;
360 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
361 drm_mga_freelist_t *head, *entry, *prev;
362
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 DRM_DEBUG("age=0x%06lx wrap=%d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100364 (unsigned long)(buf_priv->list_entry->age.head -
365 dev_priv->primary->offset),
366 buf_priv->list_entry->age.wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 entry = buf_priv->list_entry;
369 head = dev_priv->head;
370
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
372 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 prev = dev_priv->tail;
374 prev->next = entry;
375 entry->prev = prev;
376 entry->next = NULL;
377 } else {
378 prev = head->next;
379 head->next = entry;
380 prev->prev = entry;
381 entry->prev = head;
382 entry->next = prev;
383 }
384
385 return 0;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* ================================================================
389 * DMA initialization, cleanup
390 */
391
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200392int mga_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie6795c982005-07-10 18:20:09 +1000393{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 drm_mga_private_t *dev_priv;
Keith Packard52440212008-11-18 09:30:25 -0800395 int ret;
Dave Airlie6795c982005-07-10 18:20:09 +1000396
Eric Anholt9a298b22009-03-24 12:23:04 -0700397 dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
Dave Airlie6795c982005-07-10 18:20:09 +1000398 if (!dev_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000399 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000400
401 dev->dev_private = (void *)dev_priv;
Dave Airlie6795c982005-07-10 18:20:09 +1000402
403 dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
404 dev_priv->chipset = flags;
405
Jordan Crouse01d73a62010-05-27 13:40:24 -0600406 dev_priv->mmio_base = pci_resource_start(dev->pdev, 1);
407 dev_priv->mmio_size = pci_resource_len(dev->pdev, 1);
Dave Airlie22eae942005-11-10 22:16:34 +1100408
409 dev->counters += 3;
410 dev->types[6] = _DRM_STAT_IRQ;
411 dev->types[7] = _DRM_STAT_PRIMARY;
412 dev->types[8] = _DRM_STAT_SECONDARY;
413
Keith Packard52440212008-11-18 09:30:25 -0800414 ret = drm_vblank_init(dev, 1);
415
416 if (ret) {
417 (void) mga_driver_unload(dev);
418 return ret;
419 }
420
Dave Airlie6795c982005-07-10 18:20:09 +1000421 return 0;
422}
423
Dave Airlie908f9c42005-09-05 21:51:30 +1000424#if __OS_HAS_AGP
Dave Airlie6795c982005-07-10 18:20:09 +1000425/**
426 * Bootstrap the driver for AGP DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 *
Dave Airlie6795c982005-07-10 18:20:09 +1000428 * \todo
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 * Investigate whether there is any benefit to storing the WARP microcode in
Dave Airlie6795c982005-07-10 18:20:09 +1000430 * AGP memory. If not, the microcode may as well always be put in PCI
431 * memory.
432 *
433 * \todo
434 * This routine needs to set dma_bs->agp_mode to the mode actually configured
435 * in the hardware. Looking just at the Linux AGP driver code, I don't see
436 * an easy way to determine this.
437 *
438 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
439 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200440static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
441 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000442{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 drm_mga_private_t *const dev_priv =
444 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100445 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000446 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 unsigned offset;
Dave Airlie6795c982005-07-10 18:20:09 +1000448 const unsigned secondary_size = dma_bs->secondary_bin_count
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 * dma_bs->secondary_bin_size;
Dave Airlie6795c982005-07-10 18:20:09 +1000450 const unsigned agp_size = (dma_bs->agp_size << 20);
Dave Airlieeddca552007-07-11 16:09:54 +1000451 struct drm_buf_desc req;
452 struct drm_agp_mode mode;
453 struct drm_agp_info info;
454 struct drm_agp_buffer agp_req;
455 struct drm_agp_binding bind_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000456
Dave Airlie6795c982005-07-10 18:20:09 +1000457 /* Acquire AGP. */
458 err = drm_agp_acquire(dev);
459 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100460 DRM_ERROR("Unable to acquire AGP: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000461 return err;
462 }
463
464 err = drm_agp_info(dev, &info);
465 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100466 DRM_ERROR("Unable to get AGP info: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000467 return err;
468 }
469
470 mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
471 err = drm_agp_enable(dev, mode);
472 if (err) {
473 DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
474 return err;
475 }
476
Dave Airlie6795c982005-07-10 18:20:09 +1000477 /* In addition to the usual AGP mode configuration, the G200 AGP cards
478 * need to have the AGP mode "manually" set.
479 */
480
481 if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200482 if (mode.mode & 0x02)
Dave Airlie6795c982005-07-10 18:20:09 +1000483 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200484 else
Dave Airlie6795c982005-07-10 18:20:09 +1000485 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
Dave Airlie6795c982005-07-10 18:20:09 +1000486 }
487
Dave Airlie6795c982005-07-10 18:20:09 +1000488 /* Allocate and bind AGP memory. */
Dave Airlie7ccf8002005-11-11 23:11:34 +1100489 agp_req.size = agp_size;
490 agp_req.type = 0;
491 err = drm_agp_alloc(dev, &agp_req);
492 if (err) {
493 dev_priv->agp_size = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000494 DRM_ERROR("Unable to allocate %uMB AGP memory\n",
495 dma_bs->agp_size);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100496 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000497 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000498
Dave Airlie7ccf8002005-11-11 23:11:34 +1100499 dev_priv->agp_size = agp_size;
500 dev_priv->agp_handle = agp_req.handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501
Dave Airlie7ccf8002005-11-11 23:11:34 +1100502 bind_req.handle = agp_req.handle;
503 bind_req.offset = 0;
504 err = drm_agp_bind(dev, &bind_req);
Dave Airlie6795c982005-07-10 18:20:09 +1000505 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100506 DRM_ERROR("Unable to bind AGP memory: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000507 return err;
508 }
509
Dave Airlie11909d62005-10-19 21:23:51 -0700510 /* Make drm_addbufs happy by not trying to create a mapping for less
511 * than a page.
512 */
513 if (warp_size < PAGE_SIZE)
514 warp_size = PAGE_SIZE;
515
Dave Airlie6795c982005-07-10 18:20:09 +1000516 offset = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 err = drm_addmap(dev, offset, warp_size,
518 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
Dave Airlie6795c982005-07-10 18:20:09 +1000519 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100520 DRM_ERROR("Unable to map WARP microcode: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000521 return err;
522 }
523
524 offset += warp_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 err = drm_addmap(dev, offset, dma_bs->primary_size,
526 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
Dave Airlie6795c982005-07-10 18:20:09 +1000527 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100528 DRM_ERROR("Unable to map primary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000529 return err;
530 }
531
532 offset += dma_bs->primary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 err = drm_addmap(dev, offset, secondary_size,
534 _DRM_AGP, 0, &dev->agp_buffer_map);
Dave Airlie6795c982005-07-10 18:20:09 +1000535 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100536 DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000537 return err;
538 }
539
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000541 req.count = dma_bs->secondary_bin_count;
542 req.size = dma_bs->secondary_bin_size;
543 req.flags = _DRM_AGP_BUFFER;
544 req.agp_start = offset;
545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 err = drm_addbufs_agp(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000547 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100548 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000549 return err;
550 }
551
Dave Airlie7ccf8002005-11-11 23:11:34 +1100552 {
Dave Airlie55910512007-07-11 16:53:40 +1000553 struct drm_map_list *_entry;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100554 unsigned long agp_token = 0;
Dave Airliebc5f4522007-11-05 12:50:58 +1000555
Dave Airliebd1b3312007-05-26 05:01:51 +1000556 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100557 if (_entry->map == dev->agp_buffer_map)
558 agp_token = _entry->user_token;
559 }
560 if (!agp_token)
561 return -EFAULT;
562
563 dev->agp_buffer_token = agp_token;
564 }
565
Dave Airlie6795c982005-07-10 18:20:09 +1000566 offset += secondary_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 err = drm_addmap(dev, offset, agp_size - offset,
568 _DRM_AGP, 0, &dev_priv->agp_textures);
Dave Airlie6795c982005-07-10 18:20:09 +1000569 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100570 DRM_ERROR("Unable to map AGP texture region %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000571 return err;
572 }
573
574 drm_core_ioremap(dev_priv->warp, dev);
575 drm_core_ioremap(dev_priv->primary, dev);
576 drm_core_ioremap(dev->agp_buffer_map, dev);
577
578 if (!dev_priv->warp->handle ||
579 !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
580 DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
581 dev_priv->warp->handle, dev_priv->primary->handle,
582 dev->agp_buffer_map->handle);
Eric Anholt20caafa2007-08-25 19:22:43 +1000583 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000584 }
585
586 dev_priv->dma_access = MGA_PAGPXFER;
587 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
588
589 DRM_INFO("Initialized card for AGP DMA.\n");
590 return 0;
591}
Dave Airlie908f9c42005-09-05 21:51:30 +1000592#else
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200593static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
594 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie908f9c42005-09-05 21:51:30 +1000595{
596 return -EINVAL;
597}
598#endif
Dave Airlie6795c982005-07-10 18:20:09 +1000599
600/**
601 * Bootstrap the driver for PCI DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000602 *
Dave Airlie6795c982005-07-10 18:20:09 +1000603 * \todo
604 * The algorithm for decreasing the size of the primary DMA buffer could be
605 * better. The size should be rounded up to the nearest page size, then
606 * decrease the request size by a single page each pass through the loop.
607 *
608 * \todo
609 * Determine whether the maximum address passed to drm_pci_alloc is correct.
610 * The same goes for drm_addbufs_pci.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 *
Dave Airlie6795c982005-07-10 18:20:09 +1000612 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
613 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200614static int mga_do_pci_dma_bootstrap(struct drm_device *dev,
615 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000616{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 drm_mga_private_t *const dev_priv =
618 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100619 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000620 unsigned int primary_size;
621 unsigned int bin_count;
622 int err;
Dave Airlieeddca552007-07-11 16:09:54 +1000623 struct drm_buf_desc req;
Dave Airlie6795c982005-07-10 18:20:09 +1000624
Dave Airlie6795c982005-07-10 18:20:09 +1000625 if (dev->dma == NULL) {
626 DRM_ERROR("dev->dma is NULL\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000627 return -EFAULT;
Dave Airlie6795c982005-07-10 18:20:09 +1000628 }
629
Dave Airlie11909d62005-10-19 21:23:51 -0700630 /* Make drm_addbufs happy by not trying to create a mapping for less
631 * than a page.
632 */
633 if (warp_size < PAGE_SIZE)
634 warp_size = PAGE_SIZE;
635
Dave Airlie6795c982005-07-10 18:20:09 +1000636 /* The proper alignment is 0x100 for this mapping */
637 err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
638 _DRM_READ_ONLY, &dev_priv->warp);
639 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100640 DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
641 err);
Dave Airlie6795c982005-07-10 18:20:09 +1000642 return err;
643 }
644
645 /* Other than the bottom two bits being used to encode other
646 * information, there don't appear to be any restrictions on the
647 * alignment of the primary or secondary DMA buffers.
648 */
649
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 for (primary_size = dma_bs->primary_size; primary_size != 0;
651 primary_size >>= 1) {
Dave Airlie6795c982005-07-10 18:20:09 +1000652 /* The proper alignment for this mapping is 0x04 */
653 err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
654 _DRM_READ_ONLY, &dev_priv->primary);
655 if (!err)
656 break;
657 }
658
659 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100660 DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
Eric Anholt20caafa2007-08-25 19:22:43 +1000661 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000662 }
663
664 if (dev_priv->primary->size != dma_bs->primary_size) {
665 DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 dma_bs->primary_size,
667 (unsigned)dev_priv->primary->size);
Dave Airlie6795c982005-07-10 18:20:09 +1000668 dma_bs->primary_size = dev_priv->primary->size;
669 }
670
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
672 bin_count--) {
673 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000674 req.count = bin_count;
675 req.size = dma_bs->secondary_bin_size;
676
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 err = drm_addbufs_pci(dev, &req);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200678 if (!err)
Dave Airlie6795c982005-07-10 18:20:09 +1000679 break;
Dave Airlie6795c982005-07-10 18:20:09 +1000680 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681
Dave Airlie6795c982005-07-10 18:20:09 +1000682 if (bin_count == 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100683 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000684 return err;
685 }
686
687 if (bin_count != dma_bs->secondary_bin_count) {
688 DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
689 "to %u.\n", dma_bs->secondary_bin_count, bin_count);
690
691 dma_bs->secondary_bin_count = bin_count;
692 }
693
694 dev_priv->dma_access = 0;
695 dev_priv->wagp_enable = 0;
696
697 dma_bs->agp_mode = 0;
698
699 DRM_INFO("Initialized card for PCI DMA.\n");
700 return 0;
701}
702
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200703static int mga_do_dma_bootstrap(struct drm_device *dev,
704 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000705{
Dave Airlie8410ea32010-12-15 03:16:38 +1000706 const int is_agp = (dma_bs->agp_mode != 0) && drm_pci_device_is_agp(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000707 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000708 drm_mga_private_t *const dev_priv =
709 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000710
711 dev_priv->used_new_dma_init = 1;
712
713 /* The first steps are the same for both PCI and AGP based DMA. Map
714 * the cards MMIO registers and map a status page.
715 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
717 _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio);
Dave Airlie6795c982005-07-10 18:20:09 +1000718 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100719 DRM_ERROR("Unable to map MMIO region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000720 return err;
721 }
722
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
724 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
725 &dev_priv->status);
Dave Airlie6795c982005-07-10 18:20:09 +1000726 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100727 DRM_ERROR("Unable to map status region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000728 return err;
729 }
730
Dave Airlie6795c982005-07-10 18:20:09 +1000731 /* The DMA initialization procedure is slightly different for PCI and
732 * AGP cards. AGP cards just allocate a large block of AGP memory and
733 * carve off portions of it for internal uses. The remaining memory
734 * is returned to user-mode to be used for AGP textures.
735 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200736 if (is_agp)
Dave Airlie6795c982005-07-10 18:20:09 +1000737 err = mga_do_agp_dma_bootstrap(dev, dma_bs);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738
Dave Airlie6795c982005-07-10 18:20:09 +1000739 /* If we attempted to initialize the card for AGP DMA but failed,
740 * clean-up any mess that may have been created.
741 */
742
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200743 if (err)
Dave Airlie7ccf8002005-11-11 23:11:34 +1100744 mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +1000745
Dave Airlie6795c982005-07-10 18:20:09 +1000746 /* Not only do we want to try and initialized PCI cards for PCI DMA,
747 * but we also try to initialized AGP cards that could not be
748 * initialized for AGP DMA. This covers the case where we have an AGP
749 * card in a system with an unsupported AGP chipset. In that case the
750 * card will be detected as AGP, but we won't be able to allocate any
751 * AGP memory, etc.
752 */
753
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200754 if (!is_agp || err)
Dave Airlie6795c982005-07-10 18:20:09 +1000755 err = mga_do_pci_dma_bootstrap(dev, dma_bs);
Dave Airlie6795c982005-07-10 18:20:09 +1000756
Dave Airlie6795c982005-07-10 18:20:09 +1000757 return err;
758}
759
Eric Anholtc153f452007-09-03 12:06:45 +1000760int mga_dma_bootstrap(struct drm_device *dev, void *data,
761 struct drm_file *file_priv)
Dave Airlie6795c982005-07-10 18:20:09 +1000762{
Eric Anholtc153f452007-09-03 12:06:45 +1000763 drm_mga_dma_bootstrap_t *bootstrap = data;
Dave Airlie6795c982005-07-10 18:20:09 +1000764 int err;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100765 static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
766 const drm_mga_private_t *const dev_priv =
767 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000768
Eric Anholtc153f452007-09-03 12:06:45 +1000769 err = mga_do_dma_bootstrap(dev, bootstrap);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100770 if (err) {
771 mga_do_cleanup_dma(dev, FULL_CLEANUP);
772 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000773 }
774
Dave Airlie7ccf8002005-11-11 23:11:34 +1100775 if (dev_priv->agp_textures != NULL) {
Eric Anholtc153f452007-09-03 12:06:45 +1000776 bootstrap->texture_handle = dev_priv->agp_textures->offset;
777 bootstrap->texture_size = dev_priv->agp_textures->size;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100778 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000779 bootstrap->texture_handle = 0;
780 bootstrap->texture_size = 0;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100781 }
782
Eric Anholtc153f452007-09-03 12:06:45 +1000783 bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
Dave Airlie7ccf8002005-11-11 23:11:34 +1100784
Dave Airlie6795c982005-07-10 18:20:09 +1000785 return err;
786}
787
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200788static int mga_do_init_dma(struct drm_device *dev, drm_mga_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 drm_mga_private_t *dev_priv;
791 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000792 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Dave Airlie6795c982005-07-10 18:20:09 +1000794 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200796 if (init->sgram)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200798 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 dev_priv->maccess = init->maccess;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 dev_priv->fb_cpp = init->fb_cpp;
803 dev_priv->front_offset = init->front_offset;
804 dev_priv->front_pitch = init->front_pitch;
805 dev_priv->back_offset = init->back_offset;
806 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 dev_priv->depth_cpp = init->depth_cpp;
809 dev_priv->depth_offset = init->depth_offset;
810 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* FIXME: Need to support AGP textures...
813 */
814 dev_priv->texture_offset = init->texture_offset[0];
815 dev_priv->texture_size = init->texture_size[0];
816
Dave Airlieda509d72007-05-26 05:04:51 +1000817 dev_priv->sarea = drm_getsarea(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000818 if (!dev_priv->sarea) {
819 DRM_ERROR("failed to find sarea!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000820 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 if (!dev_priv->used_new_dma_init) {
Dave Airlie11909d62005-10-19 21:23:51 -0700824
825 dev_priv->dma_access = MGA_PAGPXFER;
826 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
827
Dave Airlie6795c982005-07-10 18:20:09 +1000828 dev_priv->status = drm_core_findmap(dev, init->status_offset);
829 if (!dev_priv->status) {
830 DRM_ERROR("failed to find status page!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000831 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000832 }
833 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
834 if (!dev_priv->mmio) {
835 DRM_ERROR("failed to find mmio region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000836 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000837 }
838 dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
839 if (!dev_priv->warp) {
840 DRM_ERROR("failed to find warp microcode region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000841 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000842 }
843 dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
844 if (!dev_priv->primary) {
845 DRM_ERROR("failed to find primary dma region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000846 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000847 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000848 dev->agp_buffer_token = init->buffers_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 dev->agp_buffer_map =
850 drm_core_findmap(dev, init->buffers_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000851 if (!dev->agp_buffer_map) {
852 DRM_ERROR("failed to find dma buffer region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000853 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000854 }
855
856 drm_core_ioremap(dev_priv->warp, dev);
857 drm_core_ioremap(dev_priv->primary, dev);
858 drm_core_ioremap(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
863 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlie6795c982005-07-10 18:20:09 +1000865 if (!dev_priv->warp->handle ||
866 !dev_priv->primary->handle ||
867 ((dev_priv->dma_access != 0) &&
868 ((dev->agp_buffer_map == NULL) ||
869 (dev->agp_buffer_map->handle == NULL)))) {
870 DRM_ERROR("failed to ioremap agp regions!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000871 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Dave Airlie6795c982005-07-10 18:20:09 +1000874 ret = mga_warp_install_microcode(dev_priv);
875 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100876 DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return ret;
878 }
879
Dave Airlie6795c982005-07-10 18:20:09 +1000880 ret = mga_warp_init(dev_priv);
881 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100882 DRM_ERROR("failed to init WARP engine!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
884 }
885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 dev_priv->prim.status = (u32 *) dev_priv->status->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* Init the primary DMA registers.
891 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#if 0
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
895 MGA_PRIMPTREN1); /* DWGSYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif
897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
899 dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 + dev_priv->primary->size);
901 dev_priv->prim.size = dev_priv->primary->size;
902
903 dev_priv->prim.tail = 0;
904 dev_priv->prim.space = dev_priv->prim.size;
905 dev_priv->prim.wrapped = 0;
906
907 dev_priv->prim.last_flush = 0;
908 dev_priv->prim.last_wrap = 0;
909
910 dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
911
912 dev_priv->prim.status[0] = dev_priv->primary->offset;
913 dev_priv->prim.status[1] = 0;
914
915 dev_priv->sarea_priv->last_wrap = 0;
916 dev_priv->sarea_priv->last_frame.head = 0;
917 dev_priv->sarea_priv->last_frame.wrap = 0;
918
Dave Airlie6795c982005-07-10 18:20:09 +1000919 if (mga_freelist_init(dev, dev_priv) < 0) {
920 DRM_ERROR("could not initialize freelist\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000921 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
Dave Airlieeddca552007-07-11 16:09:54 +1000927static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Dave Airlie6795c982005-07-10 18:20:09 +1000929 int err = 0;
930 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* Make sure interrupts are disabled here because the uninstall ioctl
933 * may not have been called from userspace and after dev_private
934 * is freed, it's too late.
935 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 if (dev->irq_enabled)
937 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 drm_mga_private_t *dev_priv = dev->dev_private;
941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 if ((dev_priv->warp != NULL)
Dave Airlie11909d62005-10-19 21:23:51 -0700943 && (dev_priv->warp->type != _DRM_CONSISTENT))
Dave Airlie6795c982005-07-10 18:20:09 +1000944 drm_core_ioremapfree(dev_priv->warp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000946 if ((dev_priv->primary != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000947 && (dev_priv->primary->type != _DRM_CONSISTENT))
948 drm_core_ioremapfree(dev_priv->primary, dev);
949
950 if (dev->agp_buffer_map != NULL)
951 drm_core_ioremapfree(dev->agp_buffer_map, dev);
952
953 if (dev_priv->used_new_dma_init) {
Dave Airlie908f9c42005-09-05 21:51:30 +1000954#if __OS_HAS_AGP
Dave Airlie7ccf8002005-11-11 23:11:34 +1100955 if (dev_priv->agp_handle != 0) {
Dave Airlieeddca552007-07-11 16:09:54 +1000956 struct drm_agp_binding unbind_req;
957 struct drm_agp_buffer free_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000958
Dave Airlie7ccf8002005-11-11 23:11:34 +1100959 unbind_req.handle = dev_priv->agp_handle;
960 drm_agp_unbind(dev, &unbind_req);
961
962 free_req.handle = dev_priv->agp_handle;
963 drm_agp_free(dev, &free_req);
Dave Airliebc5f4522007-11-05 12:50:58 +1000964
Dave Airlie7ccf8002005-11-11 23:11:34 +1100965 dev_priv->agp_textures = NULL;
966 dev_priv->agp_size = 0;
967 dev_priv->agp_handle = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000968 }
969
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200970 if ((dev->agp != NULL) && dev->agp->acquired)
Dave Airlie6795c982005-07-10 18:20:09 +1000971 err = drm_agp_release(dev);
Dave Airlie908f9c42005-09-05 21:51:30 +1000972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Dave Airlie6795c982005-07-10 18:20:09 +1000975 dev_priv->warp = NULL;
976 dev_priv->primary = NULL;
Dave Airlie6795c982005-07-10 18:20:09 +1000977 dev_priv->sarea = NULL;
978 dev_priv->sarea_priv = NULL;
979 dev->agp_buffer_map = NULL;
980
Dave Airlie7ccf8002005-11-11 23:11:34 +1100981 if (full_cleanup) {
982 dev_priv->mmio = NULL;
983 dev_priv->status = NULL;
984 dev_priv->used_new_dma_init = 0;
985 }
986
Dave Airlie6795c982005-07-10 18:20:09 +1000987 memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
988 dev_priv->warp_pipe = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000989 memset(dev_priv->warp_pipe_phys, 0,
990 sizeof(dev_priv->warp_pipe_phys));
Dave Airlie6795c982005-07-10 18:20:09 +1000991
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200992 if (dev_priv->head != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000993 mga_freelist_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Jesper Juhla96ca102007-12-17 09:47:17 +1000996 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Eric Anholtc153f452007-09-03 12:06:45 +1000999int mga_dma_init(struct drm_device *dev, void *data,
1000 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Eric Anholtc153f452007-09-03 12:06:45 +10001002 drm_mga_init_t *init = data;
Dave Airlie6795c982005-07-10 18:20:09 +10001003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Anholt6c340ea2007-08-25 20:23:09 +10001005 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric Anholtc153f452007-09-03 12:06:45 +10001007 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 case MGA_INIT_DMA:
Eric Anholtc153f452007-09-03 12:06:45 +10001009 err = mga_do_init_dma(dev, init);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001010 if (err)
Dave Airlie7ccf8002005-11-11 23:11:34 +11001011 (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +10001012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case MGA_CLEANUP_DMA:
Dave Airlie7ccf8002005-11-11 23:11:34 +11001014 return mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Eric Anholt20caafa2007-08-25 19:22:43 +10001017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/* ================================================================
1021 * Primary DMA stream management
1022 */
1023
Eric Anholtc153f452007-09-03 12:06:45 +10001024int mga_dma_flush(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001028 struct drm_lock *lock = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric Anholt6c340ea2007-08-25 20:23:09 +10001030 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 DRM_DEBUG("%s%s%s\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001033 (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
1034 (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
1035 (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 WRAP_WAIT_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001039 if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 mga_do_dma_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Eric Anholtc153f452007-09-03 12:06:45 +10001042 if (lock->flags & _DRM_LOCK_QUIESCENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 int ret = mga_do_wait_for_idle(dev_priv);
1045 if (ret < 0)
Márton Németh3e684ea2008-01-24 15:58:57 +10001046 DRM_INFO("-EBUSY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return ret;
1048#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#endif
1051 } else {
1052 return 0;
1053 }
1054}
1055
Eric Anholtc153f452007-09-03 12:06:45 +10001056int mga_dma_reset(struct drm_device *dev, void *data,
1057 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Eric Anholt6c340ea2007-08-25 20:23:09 +10001061 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 return mga_do_dma_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/* ================================================================
1067 * DMA buffer management
1068 */
1069
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001070static int mga_dma_get_buffers(struct drm_device *dev,
1071 struct drm_file *file_priv, struct drm_dma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Dave Airlie056219e2007-07-11 16:17:42 +10001073 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 int i;
1075
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 for (i = d->granted_count; i < d->request_count; i++) {
1077 buf = mga_freelist_get(dev);
1078 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001079 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Anholt6c340ea2007-08-25 20:23:09 +10001081 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 if (DRM_COPY_TO_USER(&d->request_indices[i],
1084 &buf->idx, sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001085 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 if (DRM_COPY_TO_USER(&d->request_sizes[i],
1087 &buf->total, sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001088 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 d->granted_count++;
1091 }
1092 return 0;
1093}
1094
Eric Anholtc153f452007-09-03 12:06:45 +10001095int mga_dma_buffers(struct drm_device *dev, void *data,
1096 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Dave Airliecdd55a22007-07-11 16:32:08 +10001098 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001100 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int ret = 0;
1102
Eric Anholt6c340ea2007-08-25 20:23:09 +10001103 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /* Please don't send us buffers.
1106 */
Eric Anholtc153f452007-09-03 12:06:45 +10001107 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001109 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 /* We'll send you buffers.
1114 */
Eric Anholtc153f452007-09-03 12:06:45 +10001115 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001117 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 WRAP_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Eric Anholtc153f452007-09-03 12:06:45 +10001123 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001125 if (d->request_count)
Eric Anholtc153f452007-09-03 12:06:45 +10001126 ret = mga_dma_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return ret;
1129}
1130
Dave Airlie6795c982005-07-10 18:20:09 +10001131/**
1132 * Called just before the module is unloaded.
1133 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001134int mga_driver_unload(struct drm_device *dev)
Dave Airlie6795c982005-07-10 18:20:09 +10001135{
Eric Anholt9a298b22009-03-24 12:23:04 -07001136 kfree(dev->dev_private);
Dave Airlie6795c982005-07-10 18:20:09 +10001137 dev->dev_private = NULL;
1138
1139 return 0;
1140}
1141
1142/**
1143 * Called when the last opener of the device is closed.
1144 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001145void mga_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Dave Airlie7ccf8002005-11-11 23:11:34 +11001147 mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001150int mga_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 drm_mga_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001153 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}